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 95 def async_exec(sql) 96 PGresult.new(@conn.query(sql)) 97 end
# File lib/sequel/adapters/postgres.rb 99 def block(timeout=nil) 100 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 118 def check_disconnect_errors 119 begin 120 yield 121 rescue *DISCONNECT_ERROR_CLASSES => e 122 disconnect = true 123 raise(Sequel.convert_exception_class(e, Sequel::DatabaseDisconnectError)) 124 rescue PGError => e 125 disconnect = false 126 begin 127 s = status 128 rescue PGError 129 disconnect = true 130 end 131 status_ok = (s == Adapter::CONNECTION_OK) 132 disconnect ||= !status_ok 133 disconnect ||= e.message =~ DISCONNECT_ERROR_RE 134 disconnect ? raise(Sequel.convert_exception_class(e, Sequel::DatabaseDisconnectError)) : raise 135 ensure 136 block if status_ok && !disconnect 137 end 138 end
Escape bytea values. Uses historical format instead of hex format for maximum compatibility.
# File lib/sequel/adapters/postgres.rb 83 def escape_bytea(str) 84 str.gsub(/[\000-\037\047\134\177-\377]/n){|b| "\\#{sprintf('%o', b.each_byte{|x| break x}).rjust(3, '0')}"} 85 end
Escape strings by doubling apostrophes. This only works if standard conforming strings are used.
# File lib/sequel/adapters/postgres.rb 89 def escape_string(str) 90 str.gsub("'", "''") 91 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 142 def execute(sql, args=nil) 143 args = args.map{|v| @db.bound_variable_arg(v, self)} if args 144 q = check_disconnect_errors{execute_query(sql, args)} 145 begin 146 defined?(yield) ? yield(q) : q.cmd_tuples 147 ensure 148 q.clear if q && q.respond_to?(:clear) 149 end 150 end
# File lib/sequel/adapters/postgres.rb 102 def status 103 CONNECTION_OK 104 end
Private Instance Methods
Return the PGResult containing the query results.
# File lib/sequel/adapters/postgres.rb 155 def execute_query(sql, args) 156 @db.log_connection_yield(sql, self, args){args ? async_exec_params(sql, args) : async_exec(sql)} 157 end