class Sequel::Postgres::Adapter
PGconn subclass for connection specific methods used with the pg or postgres-pr driver.
Constants
- CONNECTION_OK
Make postgres-pr look like pg
- 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
# File lib/sequel/adapters/postgres.rb 91 def async_exec(sql) 92 PGresult.new(@conn.query(sql)) 93 end
# File lib/sequel/adapters/postgres.rb 95 def block(timeout=nil) 96 end
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 114 def check_disconnect_errors 115 begin 116 yield 117 rescue *DISCONNECT_ERROR_CLASSES => e 118 disconnect = true 119 raise(Sequel.convert_exception_class(e, Sequel::DatabaseDisconnectError)) 120 rescue PGError => e 121 disconnect = false 122 begin 123 s = status 124 rescue PGError 125 disconnect = true 126 end 127 status_ok = (s == Adapter::CONNECTION_OK) 128 disconnect ||= !status_ok 129 disconnect ||= e.message =~ DISCONNECT_ERROR_RE 130 disconnect ? raise(Sequel.convert_exception_class(e, Sequel::DatabaseDisconnectError)) : raise 131 ensure 132 block if status_ok && !disconnect 133 end 134 end
Escape bytea values. Uses historical format instead of hex format for maximum compatibility.
# File lib/sequel/adapters/postgres.rb 79 def escape_bytea(str) 80 str.gsub(/[\000-\037\047\134\177-\377]/n){|b| "\\#{sprintf('%o', b.each_byte{|x| break x}).rjust(3, '0')}"} 81 end
Escape strings by doubling apostrophes. This only works if standard conforming strings are used.
# File lib/sequel/adapters/postgres.rb 85 def escape_string(str) 86 str.gsub("'", "''") 87 end
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 138 def execute(sql, args=nil) 139 args = args.map{|v| @db.bound_variable_arg(v, self)} if args 140 q = check_disconnect_errors{execute_query(sql, args)} 141 begin 142 block_given? ? yield(q) : q.cmd_tuples 143 ensure 144 q.clear if q && q.respond_to?(:clear) 145 end 146 end
# File lib/sequel/adapters/postgres.rb 98 def status 99 CONNECTION_OK 100 end
Private Instance Methods
Return the PGResult containing the query results.
# File lib/sequel/adapters/postgres.rb 151 def execute_query(sql, args) 152 @db.log_connection_yield(sql, self, args){args ? async_exec(sql, args) : async_exec(sql)} 153 end