Class: SQLRunner::Adapters::PostgreSQL
- Inherits:
-
Object
- Object
- SQLRunner::Adapters::PostgreSQL
- Defined in:
- lib/sql_runner/adapters/postgresql.rb
Constant Summary collapse
- InvalidPreparedStatement =
Class.new(StandardError)
Class Method Summary collapse
Instance Method Summary collapse
- #active? ⇒ Boolean
- #connect(started = Process.clock_gettime(Process::CLOCK_MONOTONIC)) ⇒ Object
- #connection ⇒ Object
- #disconnect ⇒ Object
- #execute(query, **bind_vars) ⇒ Object
-
#initialize(connection_string) ⇒ PostgreSQL
constructor
A new instance of PostgreSQL.
- #inspect ⇒ Object
- #parse(query) ⇒ Object
- #reconnect ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(connection_string) ⇒ PostgreSQL
Returns a new instance of PostgreSQL.
12 13 14 15 |
# File 'lib/sql_runner/adapters/postgresql.rb', line 12 def initialize(connection_string) @connection_string = connection_string connect end |
Class Method Details
.load ⇒ Object
6 7 8 9 10 |
# File 'lib/sql_runner/adapters/postgresql.rb', line 6 def self.load require "pg" rescue LoadError fail MissingDependency, "make sure the pg gem is available" end |
Instance Method Details
#active? ⇒ Boolean
52 53 54 55 56 |
# File 'lib/sql_runner/adapters/postgresql.rb', line 52 def active? @connection && @connection.status == PG::Connection::CONNECTION_OK rescue PGError false end |
#connect(started = Process.clock_gettime(Process::CLOCK_MONOTONIC)) ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/sql_runner/adapters/postgresql.rb', line 17 def connect(started = Process.clock_gettime(Process::CLOCK_MONOTONIC)) @connection = PG.connect(@connection_string) rescue PG::ConnectionBad ended = Process.clock_gettime(Process::CLOCK_MONOTONIC) if ended - started < SQLRunner.timeout sleep 0.1 connect(started) else raise end end |
#connection ⇒ Object
39 40 41 |
# File 'lib/sql_runner/adapters/postgresql.rb', line 39 def connection @connection end |
#disconnect ⇒ Object
30 31 32 |
# File 'lib/sql_runner/adapters/postgresql.rb', line 30 def disconnect @connection && @connection.close && (@connection = nil) end |
#execute(query, **bind_vars) ⇒ Object
43 44 45 46 47 48 49 50 |
# File 'lib/sql_runner/adapters/postgresql.rb', line 43 def execute(query, **bind_vars) query, bindings = parse(query) args = extract_args(query, bindings, bind_vars) @connection.exec_params(query, args) rescue PG::ConnectionBad reconnect execute(query, **bind_vars) end |
#inspect ⇒ Object
62 63 64 |
# File 'lib/sql_runner/adapters/postgresql.rb', line 62 def inspect to_s end |
#parse(query) ⇒ Object
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/sql_runner/adapters/postgresql.rb', line 66 def parse(query) bindings = {} count = 0 parsed_query = query.gsub(/(:?):([a-zA-Z]\w*)/) do |match| next match if $1 == ":" # skip type casting name = match[1..-1] sym_name = name.to_sym if (!index = bindings[sym_name]) index = (count += 1) bindings[sym_name] = index end "$#{index}" end [parsed_query, bindings] end |
#reconnect ⇒ Object
34 35 36 37 |
# File 'lib/sql_runner/adapters/postgresql.rb', line 34 def reconnect disconnect connect end |
#to_s ⇒ Object
58 59 60 |
# File 'lib/sql_runner/adapters/postgresql.rb', line 58 def to_s %[#<#{self.class.name} #{"0x00%x" % (object_id << 1)}>] end |