Source

txMysql / txmysql / protocol.py

Full commit
""" protocol.py
"""
import sys
try:
    import cStringIO as stringio
except ImportError:
    import StringIO as stringio
    

from twisted.internet import defer
from twisted.internet import protocol
from twisted.python import log

# CONSTANTS:
# * SERVER CAPABILITIES:
CLIENT_LONG_PASSWORD      = 1 # /* new more secure passwords */
CLIENT_FOUND_ROWS         = 2 # /* Found instead of affected rows */
CLIENT_LONG_FLAG          = 4 # /* Get all column flags */
CLIENT_CONNECT_WITH_DB    = 8 # /* One can specify db on connect */
CLIENT_NO_SCHEMA          = 16 # /* Don't allow database.table.column */
CLIENT_COMPRESS           = 32 # /* Can use compression protocol */
CLIENT_ODBC               = 64 # /* Odbc client */
CLIENT_LOCAL_FILES        = 128 # /* Can use LOAD DATA LOCAL */
CLIENT_IGNORE_SPACE       = 256 # /* Ignore spaces before '(' */
CLIENT_PROTOCOL_          = 41512 # /* New 4.1 protocol */
CLIENT_INTERACTIVE        = 1024 # /* This is an interactive client */
CLIENT_SSL                = 2048 # /* Switch to SSL after handshake */
CLIENT_IGNORE_SIGPIPE     = 4096     # /* IGNORE sigpipes */
CLIENT_TRANSACTIONS       = 8192 # /* Client knows about transactions */
CLIENT_RESERVED           = 16384    # /* Old flag for 4.1 protocol  */
CLIENT_SECURE_CONNECTION  = 32768   # /* New 4.1 authentication */
CLIENT_MULTI_STATEMENTS   = 65536    # /* Enable/disable multi-stmt support */
CLIENT_MULTI_RESULTS      = 131072   # /* Enable/disable multi-results */

# PACKETS:
packetHeaderLength = 4 # bytes

def unpackPacketHeader(bytes):
    return 0, 0

# PROTOCOLS:

class MysqlProtocol(protocol.Protocol):
    def __init__(self):
        self.buffer = ""

    def msg(self, *msg):
        sys.stdout.write("%s"%(" ".join(msg),))
        sys.stdout.flush()

    def dataReceived(self, data):
        self.buffer += data
        self.msg(data)
        
    def connectionMade(self):
        self.msg("Connection made")
        #self.transport.write("CONNECT")

    
class MysqlProtocolFactory(protocol.ClientFactory):
    protocol = MysqlProtocol

    def __init__(self, username,
                 password,
                 database=None,
                 character_set="utf-8",
                 flags=None,
                 max_packet_size=None):
        self.username = username
        self.password = password
        self.database = database
        self.characterSet = character_set
        self.flags = flags
        self.maxPacketSize = max_packet_size
        self.deferred = defer.Deferred()

    def msg(self, *msg):
        sys.stdout.write("%s"%(" ".join(msg),))
        sys.stdout.flush()

    def getDeferred(self):
        return self.deferred
        
    def startedConnecting(self, connector):
        self.msg("started to connect...")

    def clientConnectionFailed(self, connector, reason):
        self.msg("connection failed...")
        self.deferred.errback(reason)

    def clientConnectionLost(self, connector, reason):
        self.msg("connection log.")
        self.deferred.callback(reason)