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 37 |
# 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) @print_backtrace_for_all_errors = .fetch(:print_backtrace_for_all_errors, 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
79 80 81 |
# File 'lib/rsconn/postgres.rb', line 79 def clear_error_state @error_occurred = false end |
#drop_table_if_exists(table, options = {}) ⇒ Object
83 84 85 86 |
# File 'lib/rsconn/postgres.rb', line 83 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
88 89 90 91 |
# File 'lib/rsconn/postgres.rb', line 88 def drop_view_if_exists(view, ={}) cascade = .delete(:cascade) ? ' CASCADE' : '' execute("DROP VIEW IF EXISTS #{view}#{cascade};", ) end |
#error_occurred? ⇒ Boolean
75 76 77 |
# File 'lib/rsconn/postgres.rb', line 75 def error_occurred? @error_occurred end |
#execute(sql, options = {}) ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/rsconn/postgres.rb', line 39 def execute(sql, ={}) quiet = .fetch(:quiet, @quiet) log "\n#{sql}\n" unless quiet result = with_error_handling { @conn.exec(sql) } return nil if @error_occurred 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
66 67 68 69 70 71 72 73 |
# File 'lib/rsconn/postgres.rb', line 66 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
56 57 58 59 60 61 62 63 64 |
# File 'lib/rsconn/postgres.rb', line 56 def query(sql, ={}) quiet = .fetch(:quiet, @quiet) log "\n#{sql}\n" unless quiet result = with_error_handling { @conn.exec(sql) } return nil if @error_occurred TypecastResult.new(result).result end |
#table_exists?(schema, table) ⇒ Boolean
93 94 95 96 97 98 99 100 |
# File 'lib/rsconn/postgres.rb', line 93 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" |