Class: PostgresPR::Connection
- Inherits:
-
Object
- Object
- PostgresPR::Connection
- Defined in:
- lib/postgres-pr/connection.rb
Constant Summary collapse
- CONNECTION_OK =
-1
Instance Method Summary collapse
- #async_exec(sql) ⇒ Object
- #block(timeout = nil) ⇒ Object
-
#escape_bytea(str) ⇒ Object
Escape bytea values.
-
#escape_string(str) ⇒ Object
Escape strings by doubling apostrophes.
- #finish ⇒ Object
-
#initialize(host, port, _, _, database, user, password) ⇒ Connection
constructor
A new instance of Connection.
- #status ⇒ Object
-
#transaction_status ⇒ Object
Returns one of the following statuses:.
Constructor Details
#initialize(host, port, _, _, database, user, password) ⇒ Connection
Returns a new instance of Connection.
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/postgres-pr/connection.rb', line 41 def initialize(host, port, _, _, database, user, password) @conn = _connection(host, port) @transaction_status = nil @params = {} @conn << StartupMessage.new('user' => user, 'database' => database).dump while true msg = Message.read(@conn) case msg when AuthentificationClearTextPassword check_password!(password) @conn << PasswordMessage.new(password).dump when AuthentificationMD5Password check_password!(password) require 'digest/md5' @conn << PasswordMessage.new("md5#{Digest::MD5.hexdigest(Digest::MD5.hexdigest(password + user) << msg.salt)}").dump when ErrorResponse raise PGError, msg.field_values.join("\t") when ReadyForQuery @transaction_status = msg.backend_transaction_status_indicator break when AuthentificationOk, NoticeResponse, ParameterStatus, BackendKeyData # ignore when UnknownAuthType raise PGError, "unhandled authentication type: #{msg.auth_type}" else raise PGError, "unhandled message type" end end end |
Instance Method Details
#async_exec(sql) ⇒ Object
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/postgres-pr/connection.rb', line 80 def async_exec(sql) check_connection_open! @conn << Query.dump(sql) rows = [] errors = [] while true msg = Message.read(@conn) case msg when DataRow rows << msg.columns when CommandComplete cmd_tag = msg.cmd_tag when ReadyForQuery @transaction_status = msg.backend_transaction_status_indicator break when RowDescription fields = msg.fields when ErrorResponse errors << msg when NoticeResponse, EmptyQueryResponse # ignore else raise PGError, "unhandled message type" end end raise(PGError, errors.map{|e| e.field_values.join("\t") }.join("\n")) unless errors.empty? Result.new(fields||[], rows, cmd_tag) end |
#block(timeout = nil) ⇒ Object
125 126 |
# File 'lib/postgres-pr/connection.rb', line 125 def block(timeout=nil) end |
#escape_bytea(str) ⇒ Object
Escape bytea values. Uses historical format instead of hex format for maximum compatibility.
115 116 117 |
# File 'lib/postgres-pr/connection.rb', line 115 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.
121 122 123 |
# File 'lib/postgres-pr/connection.rb', line 121 def escape_string(str) str.gsub("'", "''") end |
#finish ⇒ Object
74 75 76 77 78 |
# File 'lib/postgres-pr/connection.rb', line 74 def finish check_connection_open! @conn.shutdown @conn = nil end |
#status ⇒ Object
128 129 130 |
# File 'lib/postgres-pr/connection.rb', line 128 def status CONNECTION_OK end |
#transaction_status ⇒ Object
Returns one of the following statuses:
PQTRANS_IDLE = 0 (connection idle)
PQTRANS_INTRANS = 2 (idle, within transaction block)
PQTRANS_INERROR = 3 (idle, within failed transaction)
PQTRANS_UNKNOWN = 4 (cannot determine status)
Not yet implemented is:
PQTRANS_ACTIVE = 1 (command in progress)
28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/postgres-pr/connection.rb', line 28 def transaction_status case @transaction_status when 73 # I 0 when 84 # T 2 when 69 # E 3 else 4 end end |