Class: Sequel::Postgres::Adapter
- Defined in:
- lib/sequel/adapters/postgres.rb
Overview
PGconn subclass for connection specific methods used with the pg or postgres-pr driver.
Defined Under Namespace
Classes: PGresult
Constant Summary collapse
- DISCONNECT_ERROR_CLASSES =
The underlying exception classes to reraise as disconnect errors instead of regular database errors.
[IOError, Errno::EPIPE, Errno::ECONNRESET]
- DISCONNECT_ERROR_RE =
Since exception class based disconnect checking may not work, also trying parsing the exception message to look for disconnect errors.
/\A#{Regexp.union(disconnect_errors)}/
- CONNECTION_OK =
Make postgres-pr look like pg
-1
Instance Attribute Summary collapse
-
#prepared_statements ⇒ Object
readonly
Hash of prepared statements for this connection.
Instance Method Summary collapse
- #async_exec(sql) ⇒ Object
- #block(timeout = nil) ⇒ Object
-
#check_disconnect_errors ⇒ Object
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.
-
#escape_bytea(str) ⇒ Object
Escape bytea values.
-
#escape_string(str) ⇒ Object
Escape strings by doubling apostrophes.
-
#execute(sql, args = nil) ⇒ Object
Execute the given SQL with this connection.
- #status ⇒ Object
Instance Attribute Details
#prepared_statements ⇒ Object (readonly)
Hash of prepared statements for this connection. Keys are string names of the server side prepared statement, and values are SQL strings.
66 67 68 |
# File 'lib/sequel/adapters/postgres.rb', line 66 def prepared_statements @prepared_statements end |
Instance Method Details
#async_exec(sql) ⇒ Object
85 86 87 |
# File 'lib/sequel/adapters/postgres.rb', line 85 def async_exec(sql) PGresult.new(@conn.query(sql)) end |
#block(timeout = nil) ⇒ Object
89 90 |
# File 'lib/sequel/adapters/postgres.rb', line 89 def block(timeout=nil) end |
#check_disconnect_errors ⇒ Object
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.
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/sequel/adapters/postgres.rb', line 108 def check_disconnect_errors begin 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. =~ DISCONNECT_ERROR_RE disconnect ? raise(Sequel.convert_exception_class(e, Sequel::DatabaseDisconnectError)) : raise ensure block if status_ok && !disconnect end end |
#escape_bytea(str) ⇒ Object
Escape bytea values. Uses historical format instead of hex format for maximum compatibility.
73 74 75 |
# File 'lib/sequel/adapters/postgres.rb', line 73 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) ⇒ Object
Escape strings by doubling apostrophes. This only works if standard conforming strings are used.
79 80 81 |
# File 'lib/sequel/adapters/postgres.rb', line 79 def escape_string(str) str.gsub("'", "''") 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.
132 133 134 135 136 137 138 139 140 |
# File 'lib/sequel/adapters/postgres.rb', line 132 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 && q.respond_to?(:clear) end end |
#status ⇒ Object
92 93 94 |
# File 'lib/sequel/adapters/postgres.rb', line 92 def status CONNECTION_OK end |