Class: PostgresPR::Connection
- Inherits:
-
Object
- Object
- PostgresPR::Connection
- Defined in:
- lib/postgres-pr/connection.rb
Defined Under Namespace
Classes: Result
Constant Summary collapse
- DEFAULT_PORT =
5432
- DEFAULT_HOST =
'localhost'
- DEFAULT_PATH =
'/tmp'
- DEFAULT_URI =
if RUBY_PLATFORM.include?('win') 'tcp://' + DEFAULT_HOST + ':' + DEFAULT_PORT.to_s else 'unix:' + File.join(DEFAULT_PATH, '.s.PGSQL.' + DEFAULT_PORT.to_s) end
Instance Attribute Summary collapse
-
#notice_processor ⇒ Object
A block which is called with the NoticeResponse object as parameter.
Instance Method Summary collapse
- #close ⇒ Object
-
#initialize(database, user, password = nil, uri = nil) ⇒ Connection
constructor
A new instance of Connection.
- #query(sql) ⇒ Object
-
#transaction_status ⇒ Object
Returns one of the following statuses:.
Constructor Details
#initialize(database, user, password = nil, uri = nil) ⇒ Connection
Returns a new instance of Connection.
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/postgres-pr/connection.rb', line 49 def initialize(database, user, password=nil, uri = nil) uri ||= DEFAULT_URI @transaction_status = nil @params = {} establish_connection(uri) @conn << StartupMessage.new(PROTO_VERSION, 'user' => user, 'database' => database).dump loop do msg = Message.read(@conn) case msg when AuthentificationClearTextPassword raise ArgumentError, "no password specified" if password.nil? @conn << PasswordMessage.new(password).dump when AuthentificationCryptPassword raise ArgumentError, "no password specified" if password.nil? @conn << PasswordMessage.new(password.crypt(msg.salt)).dump when AuthentificationMD5Password raise ArgumentError, "no password specified" if password.nil? require 'digest/md5' m = Digest::MD5.hexdigest(password + user) m = Digest::MD5.hexdigest(m + msg.salt) m = 'md5' + m @conn << PasswordMessage.new(m).dump when AuthentificationKerberosV4, AuthentificationKerberosV5, AuthentificationSCMCredential raise "unsupported authentification" when AuthentificationOk when ErrorResponse raise msg.field_values.join("\t") when NoticeResponse @notice_processor.call(msg) if @notice_processor when ParameterStatus @params[msg.key] = msg.value when BackendKeyData # TODO #p msg when ReadyForQuery @transaction_status = msg.backend_transaction_status_indicator break else raise "unhandled message type" end end end |
Instance Attribute Details
#notice_processor ⇒ Object
A block which is called with the NoticeResponse object as parameter.
22 23 24 |
# File 'lib/postgres-pr/connection.rb', line 22 def notice_processor @notice_processor end |
Instance Method Details
#close ⇒ Object
101 102 103 104 105 |
# File 'lib/postgres-pr/connection.rb', line 101 def close raise "connection already closed" if @conn.nil? @conn.shutdown @conn = nil end |
#query(sql) ⇒ Object
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 |
# File 'lib/postgres-pr/connection.rb', line 114 def query(sql) @conn << Query.dump(sql) result = Result.new errors = [] loop do msg = Message.read(@conn) case msg when DataRow result.rows << msg.columns when CommandComplete result.cmd_tag = msg.cmd_tag when ReadyForQuery @transaction_status = msg.backend_transaction_status_indicator break when RowDescription result.fields = msg.fields when CopyInResponse when CopyOutResponse when EmptyQueryResponse when ErrorResponse # TODO errors << msg when NoticeResponse @notice_processor.call(msg) if @notice_processor else # TODO end end raise DatabaseError.new(errors.map{|e| e.field_values.join("\t") }.join("\n")) unless errors.empty? result 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)
36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/postgres-pr/connection.rb', line 36 def transaction_status case @transaction_status when ?I 0 when ?T 2 when ?E 3 else 4 end end |