Commits

Mike Steder committed 1c52405

Just making a mess...

Comments (0)

Files changed (2)

 def shutdown(_):
     reactor.stop()
 
+def runQuery(proto):
+    d = proto.selectdb("test")
+    #proto.query("select * from counter")
+    #proto.quit()
 
 factory = protocol.MysqlProtocolFactory(
     settings.USERNAME,
     settings.PASSWORD
 )
 d = factory.getDeferred()
-d.addBoth(shutdown)
-
-
+d.addErrback(shutdown)
+d.addCallback(runQuery)
 
 reactor.connectTCP(settings.HOSTNAME, settings.PORT, factory)
 reactor.run()

txmysql/protocol.py

 
 # COMMAND CODES:
 COM_SLEEP = 0x00
-COM_QUIT = 0x01
-
+COM_QUIT = 0x01 #(mysql_close)
+COM_INIT_DB = 0x02 #(mysql_select_db)
+COM_QUERY = 0x03 #(mysql_real_query)
 
 # PROTOCOLS:
 protocolStates = enum.Enum("AWAITING_HANDSHAKE", "AUTHENTICATING", "CONNECTED")
                 if field_count == 0:
                     print "OK!"
                     self.state = protocolStates.CONNECTED
+                    # fire some callback to let users know the protocol is connected
+                    self.factory.deferred.callback(self)
                 else:
                     print "ERROR!"
+        elif self.state == protocolStates.CONNECTED:
+            p = self.getPacket(self.buffer)
+        elif self.state == protocolStates.RUNNING_COMMAND:
+            p = self.getPacket(self.buffer)
+            if p:
+                d = self.deferreds.popleft()
+                self.state = protocolStates.CONNECTED
+                d.callback(self, p)
 
     def quit(self):
         quit_packet = struct.pack("B4s", COM_QUIT, "quit")
         self.write(quit_packet)
 
+    def selectdb(self, dbname):
+        select_db = struct.pack("<i", len(dbname)+1) + struct.pack("B", COM_INIT_DB) + dbname
+        self.write(select_db)
+
+    def query(self, sql):
+        select_packet = struct.pack("<i", len(sql)+1) + struct.pack("B", COM_QUERY) + sql
+        self.write(select_packet)
+        
     def sendAuthentication(self, greetingPacket):
         salt = (greetingPacket.scrambleBuffer +
                 greetingPacket.restOfScrambleBuffer)
 
     def clientConnectionLost(self, connector, reason):
         self.msg("connection lost.")
-        self.deferred.callback(reason)
+