Class: Sequel::Postgres::Adapter

Inherits:
PGconn
  • Object
show all
Defined in:
lib/sequel/adapters/postgres.rb

Overview

PGconn subclass for connection specific methods used with the pg, postgres, or postgres-pr driver.

Constant Summary collapse

DISCONNECT_ERROR_RE =
/\Acould not receive data from server: Software caused connection abort/

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#prepared_statementsObject (readonly)

Hash of prepared statements for this connection. Keys are string names of the server side prepared statement, and values are SQL strings.



162
163
164
# File 'lib/sequel/adapters/postgres.rb', line 162

def prepared_statements
  @prepared_statements
end

Instance Method Details

#check_disconnect_errorsObject

Raise a Sequel::DatabaseDisconnectError if a PGError is raised and the connection status cannot be determined or it is not OK.



166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/sequel/adapters/postgres.rb', line 166

def check_disconnect_errors
  begin
    yield
  rescue PGError =>e
    disconnect = false
    begin
      s = status
    rescue PGError
      disconnect = true
    end
    status_ok = (s == Adapter::CONNECTION_OK)
    disconnect ||= !status_ok
    disconnect ||= e.message =~ DISCONNECT_ERROR_RE
    disconnect ? raise(Sequel.convert_exception_class(e, Sequel::DatabaseDisconnectError)) : raise
  ensure
    block if status_ok
  end
end

#execute(sql, args = nil) ⇒ Object

Execute the given SQL with this connection. If a block is given, yield the results, otherwise, return the number of changed rows.



187
188
189
190
191
192
193
194
195
# File 'lib/sequel/adapters/postgres.rb', line 187

def execute(sql, args=nil)
  args = args.map{|v| @db.bound_variable_arg(v, self)} if args
  q = check_disconnect_errors{execute_query(sql, args)}
  begin
    block_given? ? yield(q) : q.cmd_tuples
  ensure
    q.clear if q
  end
end