Class: Rsconn::Postgres
- Inherits:
-
Object
- Object
- Rsconn::Postgres
- Defined in:
- lib/rsconn/postgres.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#conn ⇒ Object
readonly
Returns the value of attribute conn.
Instance Method Summary collapse
- #clear_error_state ⇒ Object
- #drop_table_if_exists(table, options = {}) ⇒ Object
- #drop_view_if_exists(view, options = {}) ⇒ Object
- #error_occurred? ⇒ Boolean
- #execute(sql, options = {}) ⇒ Object
- #execute_script(filename, options = {}) ⇒ Object
-
#initialize(host, port, database, user, password, options = {}) ⇒ Postgres
constructor
A new instance of Postgres.
- #query(sql, options = {}) ⇒ Object
- #table_exists?(schema, table) ⇒ Boolean
Constructor Details
#initialize(host, port, database, user, password, options = {}) ⇒ Postgres
Returns a new instance of Postgres.
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/rsconn/postgres.rb', line 18 def initialize(host, port, database, user, password, ={}) fail_if_invalid_connection_credentials(host, port, database, user, password) @host = host @port = port @database = database @user = user @password = password @abort_on_error = .fetch(:abort_on_error, true) @max_retries = .fetch(:max_retries, 3) @quiet = .fetch(:quiet, false) @error_occurred = false @retry_count = 0 secrets = .fetch(:secrets, []) @logger = Logger.new(:secrets => secrets) init_connection end |
Instance Attribute Details
#conn ⇒ Object (readonly)
Returns the value of attribute conn.
16 17 18 |
# File 'lib/rsconn/postgres.rb', line 16 def conn @conn end |
Instance Method Details
#clear_error_state ⇒ Object
75 76 77 |
# File 'lib/rsconn/postgres.rb', line 75 def clear_error_state @error_occurred = false end |
#drop_table_if_exists(table, options = {}) ⇒ Object
79 80 81 82 |
# File 'lib/rsconn/postgres.rb', line 79 def drop_table_if_exists(table, ={}) cascade = .delete(:cascade) ? ' CASCADE' : '' execute("DROP TABLE IF EXISTS #{table}#{cascade};", ) end |
#drop_view_if_exists(view, options = {}) ⇒ Object
84 85 86 87 |
# File 'lib/rsconn/postgres.rb', line 84 def drop_view_if_exists(view, ={}) cascade = .delete(:cascade) ? ' CASCADE' : '' execute("DROP VIEW IF EXISTS #{view}#{cascade};", ) end |
#error_occurred? ⇒ Boolean
71 72 73 |
# File 'lib/rsconn/postgres.rb', line 71 def error_occurred? @error_occurred end |
#execute(sql, options = {}) ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/rsconn/postgres.rb', line 38 def execute(sql, ={}) quiet = .fetch(:quiet, @quiet) log "\n#{sql}\n" unless quiet result = with_error_handling { @conn.exec(sql) } cmd = result.cmd_status.split.first affected_rows = result.cmd_tuples if %w(INSERT UPDATE DELETE MOVE FETCH).include?(cmd) log "Affected #{affected_rows} row(s)." unless quiet end affected_rows end |
#execute_script(filename, options = {}) ⇒ Object
62 63 64 65 66 67 68 69 |
# File 'lib/rsconn/postgres.rb', line 62 def execute_script(filename, ={}) File.open(filename) do |fh| sql = fh.read sql = remove_comments(sql) sql = substitute_variables(sql) execute_each_statement(sql, ) end end |
#query(sql, options = {}) ⇒ Object
54 55 56 57 58 59 60 |
# File 'lib/rsconn/postgres.rb', line 54 def query(sql, ={}) quiet = .fetch(:quiet, @quiet) log "\n#{sql}\n" unless quiet result = with_error_handling { @conn.exec(sql) } TypecastResult.new(result).result end |
#table_exists?(schema, table) ⇒ Boolean
89 90 91 92 93 94 95 96 |
# File 'lib/rsconn/postgres.rb', line 89 def table_exists?(schema, table) sql = " SELECT count(*) FROM pg_tables\n WHERE schemaname = '\#{schema}' AND tablename = '\#{table}'\n ;\n SQL\n query(sql, :quiet => true).first['count'] == 1\nend\n" |