Beispiel #1
0
    def send_columndef(self, name, _type, length, decimals):
        payload = BytesIO()

        collation = Charset.UTF8_GENERAL_CI if _type == FieldType.VAR_STRING else Charset.BINARY
        flags = 0

        if _type in [FieldType.DECIMAL, FieldType.DOUBLE]:  # total length
            length += decimals
        elif _type == FieldType.VAR_STRING:  # utf8 = char * 3
            length *= 3
        elif _type == FieldType.TIMESTAMP:
            flags = FieldFlag.TIMESTAMP
        elif _type == FieldType.BLOB:
            flags = FieldFlag.BLOB | FieldFlag.BINARY

        payload.write(pack_string("def"))  # catalog
        payload.write(pack_padding())  # schema
        payload.write(pack_padding())  # table

        payload.write(pack_padding())  # org_table

        payload.write(pack_string(name))  # name
        payload.write(pack_padding())  # org_name

        payload.write(pack_byte(0x0c))  # length of fixed-length fields
        payload.write(pack_integer(collation))  # character set
        payload.write(pack_long(length))  # column length
        payload.write(pack_byte(_type))  # type
        payload.write(pack_integer(flags))  # flags
        payload.write(pack_byte(decimals))  # decimals
        payload.write(pack_padding(2))  # filler

        self.queue_packet(payload)
Beispiel #2
0
    def send_eof(self, warnings=0):
        payload = BytesIO()
        payload.write(pack_byte(0xfe))  # header
        payload.write(pack_integer(warnings))  # warnings
        payload.write(pack_integer(STATUS))

        self.queue_packet(payload)
Beispiel #3
0
    def send_error(self, message="", code=1064, state="42000"):
        payload = BytesIO()
        payload.write(pack_byte(0xff))  # header
        payload.write(pack_integer(code))  # error_code

        if self.capabilities & Capability.PROTOCOL_41:
            payload.write(pack_fixedstring("#"))  # sql_state_marker
            payload.write(pack_fixedstring(state))  # sql_state

        payload.write(pack_fixedstring(message))  # error_message

        self.queue_packet(payload, True)
Beispiel #4
0
    def send_handshake(self):
        payload = BytesIO()
        payload.write(pack_byte(10))  # protocol version
        payload.write(pack_nullstring(self.db.version))  # server version
        payload.write(pack_long(self.thread))  # connection id
        payload.write(pack_fixedstring(" " * 8))  # auth-plugin-data-part-1
        payload.write(pack_padding())  # filler
        payload.write(pack_integer(CAPABILITIES))  # capability flags

        payload.write(pack_byte(CHARSET))  # character set
        payload.write(pack_integer(STATUS))  # status flags
        payload.write(pack_integer(CAPABILITIES >> 16))  # capability flags

        payload.write(pack_padding())

        payload.write(pack_padding(10))  # reserved

        # auth-plugin-data-part-2
        if CAPABILITIES & Capability.SECURE_CONNECTION:
            payload.write(pack_fixedstring(" " * 12))
            payload.write(pack_padding())

        self.queue_packet(payload, True)
Beispiel #5
0
    def send_resultset(self, data):
        meta, rows = data
        self.queue_packet(pack_byte(len(meta)))

        for (name, _, _type, length, decimals) in meta:
            self.send_columndef(name, _type, length, decimals)

        self.send_eof()

        for row in rows:
            payload = BytesIO()

            for value in row:
                payload.write(pack_resstring(value))

            self.queue_packet(payload)

        self.send_eof()
        self.send_packets()