class Sequel::Postgres::Adapter

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

Constants

CONNECTION_OK

Handle old postgres-pr sequel-postgres-pr already implements this API

DISCONNECT_ERROR_CLASSES

The underlying exception classes to reraise as disconnect errors instead of regular database errors.

DISCONNECT_ERROR_RE

Since exception class based disconnect checking may not work, also trying parsing the exception message to look for disconnect errors.

Public Instance Methods

async_exec(sql) click to toggle source
# File lib/sequel/adapters/postgres.rb, line 110
def async_exec(sql)
  PGresult.new(@conn.query(sql))
end
Also aliased as: async_exec_params
async_exec_params(sql)
Alias for: async_exec
block(timeout=nil) click to toggle source
# File lib/sequel/adapters/postgres.rb, line 114
def block(timeout=nil)
end
check_disconnect_errors() { || ... } click to toggle source

Raise a Sequel::DatabaseDisconnectError if a one of the disconnect error classes is raised, or a PGError is raised and the connection status cannot be determined or it is not OK.

# File lib/sequel/adapters/postgres.rb, line 134
def check_disconnect_errors
  yield
rescue *DISCONNECT_ERROR_CLASSES => e
  disconnect = true
  raise(Sequel.convert_exception_class(e, Sequel::DatabaseDisconnectError))
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 && !disconnect
end
escape_bytea(str) click to toggle source

Escape bytea values. Uses historical format instead of hex format for maximum compatibility.

# File lib/sequel/adapters/postgres.rb, line 98
def escape_bytea(str)
  str.gsub(/[\000-\037\047\134\177-\377]/n){|b| "\\#{sprintf('%o', b.each_byte{|x| break x}).rjust(3, '0')}"}
end
escape_string(str) click to toggle source

Escape strings by doubling apostrophes. This only works if standard conforming strings are used.

# File lib/sequel/adapters/postgres.rb, line 104
def escape_string(str)
  str.gsub("'", "''")
end
execute(sql, args=nil) { || ... } click to toggle source

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

# File lib/sequel/adapters/postgres.rb, line 156
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
    defined?(yield) ? yield(q) : q.cmd_tuples
  ensure
    q.clear if q && q.respond_to?(:clear)
  end
end
status() click to toggle source
# File lib/sequel/adapters/postgres.rb, line 117
def status
  CONNECTION_OK
end

Private Instance Methods

execute_query(sql, args) click to toggle source

Return the PGResult containing the query results.

# File lib/sequel/adapters/postgres.rb, line 169
def execute_query(sql, args)
  @db.log_connection_yield(sql, self, args){args ? async_exec_params(sql, args) : async_exec(sql)}
end