Class: EventMachine::Protocols::Postgres3
- Inherits:
-
Connection
- Object
- Connection
- EventMachine::Protocols::Postgres3
- Defined in:
- lib/protocols/postgres.rb
Instance Attribute Summary
Attributes inherited from Connection
Instance Method Summary collapse
- #connect(db, user, psw = nil) ⇒ Object
-
#dispatch_conn_message(msg) ⇒ Object
Cloned and modified from the postgres-pr.
-
#dispatch_query_message(msg) ⇒ Object
Cloned and modified from the postgres-pr.
-
#initialize ⇒ Postgres3
constructor
A new instance of Postgres3.
- #query(sql) ⇒ Object
- #receive_data(data) ⇒ Object
- #unbind ⇒ Object
Methods inherited from Connection
#associate_callback_target, #close_connection, #close_connection_after_writing, #comm_inactivity_timeout, #comm_inactivity_timeout=, #connection_completed, #detach, #error?, #get_outbound_data_size, #get_peername, #get_pid, #get_sockname, #get_status, new, #post_init, #reconnect, #send_data, #send_datagram, #send_file_data, #set_comm_inactivity_timeout, #start_tls, #stream_file_data
Constructor Details
#initialize ⇒ Postgres3
Returns a new instance of Postgres3.
124 125 126 127 |
# File 'lib/protocols/postgres.rb', line 124 def initialize @data = "" @params = {} end |
Instance Method Details
#connect(db, user, psw = nil) ⇒ Object
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 |
# File 'lib/protocols/postgres.rb', line 129 def connect db, user, psw=nil d = EM::DefaultDeferrable.new d.timeout 15 if @pending_query || @pending_conn d.succeed false, "Operation already in progress" else @pending_conn = d prms = {"user"=>user, "database"=>db} @user = user if psw @password = psw #prms["password"] = psw end send_data PostgresPR::StartupMessage.new( 3 << 16, prms ).dump end d end |
#dispatch_conn_message(msg) ⇒ Object
Cloned and modified from the postgres-pr.
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 |
# File 'lib/protocols/postgres.rb', line 194 def msg case msg when AuthentificationClearTextPassword raise ArgumentError, "no password specified" if @password.nil? send_data PasswordMessage.new(@password).dump when AuthentificationCryptPassword raise ArgumentError, "no password specified" if @password.nil? send_data 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 send_data 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 # TODO: use transaction status pc,@pending_conn = @pending_conn,nil pc.succeed true else raise "unhandled message type" end end |
#dispatch_query_message(msg) ⇒ Object
Cloned and modified from the postgres-pr.
236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 |
# File 'lib/protocols/postgres.rb', line 236 def msg case msg when DataRow @r.rows << msg.columns when CommandComplete @r.cmd_tag = msg.cmd_tag when ReadyForQuery pq,@pending_query = @pending_query,nil pq.succeed true, @r, @e when RowDescription @r.fields = msg.fields when CopyInResponse when CopyOutResponse when EmptyQueryResponse when ErrorResponse # TODO @e << msg when NoticeResponse @notice_processor.call(msg) if @notice_processor else # TODO end end |
#query(sql) ⇒ Object
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 |
# File 'lib/protocols/postgres.rb', line 149 def query sql d = EM::DefaultDeferrable.new d.timeout 15 if @pending_query || @pending_conn d.succeed false, "Operation already in progress" else @r = PostgresPR::Connection::Result.new @e = [] @pending_query = d send_data PostgresPR::Query.dump(sql) end d end |
#receive_data(data) ⇒ Object
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 |
# File 'lib/protocols/postgres.rb', line 166 def receive_data data @data << data while @data.length >= 5 pktlen = @data[1...5].unpack("N").first if @data.length >= (1 + pktlen) pkt = @data.slice!(0...(1+pktlen)) m = StringIO.open( pkt, "r" ) {|io| PostgresPR::Message.read( io ) } if @pending_conn m elsif @pending_query m else raise "Unexpected message from database" end else break # very important, break out of the while end end end |
#unbind ⇒ Object
187 188 189 190 191 |
# File 'lib/protocols/postgres.rb', line 187 def unbind if o = (@pending_query || @pending_conn) o.succeed false, "lost connection" end end |