Class: SQLRunner::Adapters::PostgreSQL

Inherits:
Object
  • Object
show all
Defined in:
lib/sql_runner/adapters/postgresql.rb

Constant Summary collapse

InvalidPreparedStatement =
Class.new(StandardError)

Class Method Summary collapse

Instance Method Summary collapse

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

.loadObject



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

Returns:

  • (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

#connectionObject



39
40
41
# File 'lib/sql_runner/adapters/postgresql.rb', line 39

def connection
  @connection
end

#disconnectObject



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

#inspectObject



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

#reconnectObject



34
35
36
37
# File 'lib/sql_runner/adapters/postgresql.rb', line 34

def reconnect
  disconnect
  connect
end

#to_sObject



58
59
60
# File 'lib/sql_runner/adapters/postgresql.rb', line 58

def to_s
  %[#<#{self.class.name} #{"0x00%x" % (object_id << 1)}>]
end