| Class | Sequel::Postgres::Adapter |
| In: |
lib/sequel/adapters/postgres.rb
|
| Parent: | PGconn |
PGconn subclass for connection specific methods used with the pg or postgres-pr driver.
| DISCONNECT_ERROR_CLASSES | = | [IOError, Errno::EPIPE, Errno::ECONNRESET] | The underlying exception classes to reraise as disconnect errors instead of regular database errors. | |
| DISCONNECT_ERROR_RE | = | /\A#{Regexp.union(disconnect_errors)}/ | Since exception class based disconnect checking may not work, also trying parsing the exception message to look for disconnect errors. | |
| CONNECTION_OK | = | -1 | Make postgres-pr look like pg |
| close | -> | finish |
# File lib/sequel/adapters/postgres.rb, line 85
85: def async_exec(sql)
86: PGresult.new(@conn.query(sql))
87: 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, line 108
108: def check_disconnect_errors
109: begin
110: yield
111: rescue *DISCONNECT_ERROR_CLASSES => e
112: disconnect = true
113: raise(Sequel.convert_exception_class(e, Sequel::DatabaseDisconnectError))
114: rescue PGError => e
115: disconnect = false
116: begin
117: s = status
118: rescue PGError
119: disconnect = true
120: end
121: status_ok = (s == Adapter::CONNECTION_OK)
122: disconnect ||= !status_ok
123: disconnect ||= e.message =~ DISCONNECT_ERROR_RE
124: disconnect ? raise(Sequel.convert_exception_class(e, Sequel::DatabaseDisconnectError)) : raise
125: ensure
126: block if status_ok && !disconnect
127: end
128: end
Escape bytea values. Uses historical format instead of hex format for maximum compatibility.
# File lib/sequel/adapters/postgres.rb, line 73
73: def escape_bytea(str)
74: str.gsub(/[\000-\037\047\134\177-\377]/n){|b| "\\#{sprintf('%o', b.each_byte{|x| break x}).rjust(3, '0')}"}
75: end
Escape strings by doubling apostrophes. This only works if standard conforming strings are used.
# File lib/sequel/adapters/postgres.rb, line 79
79: def escape_string(str)
80: str.gsub("'", "''")
81: 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, line 132
132: def execute(sql, args=nil)
133: args = args.map{|v| @db.bound_variable_arg(v, self)} if args
134: q = check_disconnect_errors{execute_query(sql, args)}
135: begin
136: block_given? ? yield(q) : q.cmd_tuples
137: ensure
138: q.clear if q && q.respond_to?(:clear)
139: end
140: end