Esempio n. 1
0
    def _prepare_stmt_send_long_data(self, statement, param, data):
        """Prepare long data for prepared statments

        Returns a string.
        """
        packet = (utils.int4store(statement) + utils.int2store(param) + data)
        return packet
Esempio n. 2
0
    def binlog_dump(self, log_file, offset):
        """
        COM_BINLOG_DUMP
        +=============================================+
        | packet header    |  packet length    0 : 3 |   
        |                  +-------------------------+   
        |                  |  sequence number  3 : 1 |
        +============================================+
        | command packet   |  command code     4 : 1 |    COM_BINLOG_DUMP
        |                  +------------------------―+
        |                  |  offset           5 : 4 |
        |                  +-------------------------+
        |                  |  flags            9 : 2 |
        |                  +-------------------------+
        |                  |  server id        11 : 4|
        |                  +-------------------------+
        |                  |  log name         15 : x|
        +============================================+
        """

        payload = ""
        payload += utils.int1store(ServerCmd.BINLOG_DUMP)
        payload += utils.int4store(offset)
        payload += utils.int2store(0)
        payload += utils.int4store(self.get_server_id())
        payload += log_file
        payload += "\x00"
        log.debug("len(payload) = %d" % len(payload))

        # send BIGLOGDUMP command and parse ok packet response.
        self._socket.send(payload, 0)
        ok_packet = self._socket.recv()
        parser = MySQLProtocol()
        ok_packet = parser.parse_ok(ok_packet)
        print ok_packet
Esempio n. 3
0
    def _prepare_binary_timestamp(self, value):
        """Prepare a timestamp object for the MySQL binary protocol

        This method prepares a timestamp of type datetime.datetime or
        datetime.date for sending over the MySQL binary protocol.
        A tuple is returned with the prepared value and field type
        as elements.

        Raises ValueError when the argument value is of invalid type.

        Returns a tuple.
        """
        if isinstance(value, datetime.datetime):
            field_type = FieldType.DATETIME
        elif isinstance(value, datetime.date):
            field_type = FieldType.DATE
        else:
            raise ValueError(
                "Argument must a datetime.datetime or datetime.date")

        packed = (utils.int2store(value.year) +
                  utils.int1store(value.month) +
                  utils.int1store(value.day))

        if isinstance(value, datetime.datetime):
            packed = (packed + utils.int1store(value.hour) +
                      utils.int1store(value.minute) +
                      utils.int1store(value.second))
            if value.microsecond > 0:
                packed += utils.int4store(value.microsecond)

        packed = utils.int1store(len(packed)) + packed
        return (packed, field_type)
Esempio n. 4
0
    def _prepare_binary_timestamp(self, value):
        """Prepare a timestamp object for the MySQL binary protocol

        This method prepares a timestamp of type datetime.datetime or
        datetime.date for sending over the MySQL binary protocol.
        A tuple is returned with the prepared value and field type
        as elements.

        Raises ValueError when the argument value is of invalid type.

        Returns a tuple.
        """
        if isinstance(value, datetime.datetime):
            field_type = FieldType.DATETIME
        elif isinstance(value, datetime.date):
            field_type = FieldType.DATE
        else:
            raise ValueError(
                "Argument must a datetime.datetime or datetime.date")

        packed = (utils.int2store(value.year) +
                  utils.int1store(value.month) +
                  utils.int1store(value.day))

        if isinstance(value, datetime.datetime):
            packed = (packed + utils.int1store(value.hour) +
                      utils.int1store(value.minute) +
                      utils.int1store(value.second))
            if value.microsecond > 0:
                packed += utils.int4store(value.microsecond)

        packed = utils.int1store(len(packed)) + packed
        return (packed, field_type)
Esempio n. 5
0
    def binlog_dump(self, log_file, offset):
        """
        COM_BINLOG_DUMP
        +=============================================+
        | packet header    |  packet length    0 : 3 |   
        |                  +-------------------------+   
        |                  |  sequence number  3 : 1 |
        +============================================+
        | command packet   |  command code     4 : 1 |    COM_BINLOG_DUMP
        |                  +------------------------―+
        |                  |  offset           5 : 4 |
        |                  +-------------------------+
        |                  |  flags            9 : 2 |
        |                  +-------------------------+
        |                  |  server id        11 : 4|
        |                  +-------------------------+
        |                  |  log name         15 : x|
        +============================================+
        """

        payload = ''
        payload += utils.int1store(ServerCmd.BINLOG_DUMP)
        payload += utils.int4store(offset)
        payload += utils.int2store(0)
        payload += utils.int4store(self.get_server_id())
        payload += log_file
        payload += '\x00'
        log.debug("len(payload) = %d" % len(payload))

        # send BIGLOGDUMP command and parse ok packet response.
        self._socket.send(payload, 0)
        ok_packet = self._socket.recv()
        parser = MySQLProtocol()
        ok_packet = parser.parse_ok(ok_packet)
        print ok_packet
    def _prepare_stmt_send_long_data(self, statement, param, data):
        """Prepare long data for prepared statements

        Returns a string.
        """
        packet = utils.int4store(statement) + utils.int2store(param) + data
        return packet
Esempio n. 7
0
    def test_int2store(self):
        """Use int2store to pack an integer (2^16) as a string."""
        data = 2**(16 - 1)
        exp = struct.pack('<H', data)

        try:
            result = utils.int2store(data)
        except ValueError, e:
            self.fail("int2store failed: %s" % e)
Esempio n. 8
0
 def test_int2store(self):
     """Use int2store to pack an integer (2^16) as a string."""
     data = 2**(16-1)
     exp = struct.pack('<H',data)
     
     try:
         result = utils.int2store(data)
     except ValueError, e:
         self.fail("int2store failed: %s" % e)
Esempio n. 9
0
    def test_read_lc_string_2(self):
        """Read a length code string from a buffer ( <= 2^16 bytes)"""
        exp = b"a" * 2**(16 - 1)
        expsize = len(exp)
        lcs = b'\xfc' + utils.int2store(expsize) + exp

        (rest, result) = utils.read_lc_string(lcs)
        if result != exp or len(result) != expsize:
            self.fail("Wrong result. Expected '%d', got '%d'" %\
                expsize, len(result))
Esempio n. 10
0
    def test_read_lc_string_2(self):
        """Read a length code string from a buffer ( <= 2^16 bytes)"""
        exp = "a" * 2**(16-1)
        expsize = len(exp)
        lcs = '\xfc' + utils.int2store(expsize) + exp

        (rest, result) = utils.read_lc_string(lcs)
        if result != exp or len(result) != expsize:
            self.fail("Wrong result. Expected '%d', got '%d'" %\
                expsize, len(result))
    def test_read_lc_string_2(self):
        """Read a length code string from a buffer ( <= 2^16 bytes)"""
        exp = bytearray(b"a" * 2 ** (16 - 1))
        expsize = len(exp)
        lcs = bytearray(b'\xfc') + utils.int2store(expsize) + exp

        (_, result) = utils.read_lc_string(lcs)
        if result != exp or len(result) != expsize:
            self.fail("Wrong result. Expected '{0}', got '{1}'".format(
                expsize, len(result)))
Esempio n. 12
0
    def test_read_lc_string_2(self):
        """Read a length code string from a buffer ( <= 2^16 bytes)"""
        exp = bytearray(b"a" * 2**(16 - 1))
        expsize = len(exp)
        lcs = bytearray(b'\xfc') + utils.int2store(expsize) + exp

        (_, result) = utils.read_lc_string(lcs)
        if result != exp or len(result) != expsize:
            self.fail("Wrong result. Expected '{0}', got '{1}'".format(
                expsize, len(result)))
Esempio n. 13
0
    def make_change_user(self, seed, username=None, password=None,
                         database=None, charset=33, client_flags=0):
        """Make a MySQL packet with the Change User command"""
        if not seed:
            raise errors.ProgrammingError('Seed missing')

        auth = self._prepare_auth(username, password, database,
                                  client_flags, seed)
        data = utils.int1store(ServerCmd.CHANGE_USER) +\
               auth[0] + auth[1] + auth[2] + utils.int2store(charset)
        return data
    def test_int2store(self):
        """Use int2store to pack an integer (2^16) as a string."""
        data = 2 ** (16 - 1)
        exp = struct.pack('<H', data)

        try:
            result = utils.int2store(data)
        except ValueError as err:
            self.fail("int2store failed: {0}".format(str(err)))
        else:
            self._check_int_result(result, exp, data)
Esempio n. 15
0
    def make_change_user(self, seed, username=None, password=None,
                         database=None, charset=33, client_flags=0):
        """Make a MySQL packet with the Change User command"""
        if not seed:
            raise errors.ProgrammingError('Seed missing')

        auth = self._prepare_auth(username, password, database,
                                  client_flags, seed)
        data = utils.int1store(ServerCmd.CHANGE_USER) +\
               auth[0] + auth[1] + auth[2] + utils.int2store(charset)
        return data
Esempio n. 16
0
    def test_int2store(self):
        """Use int2store to pack an integer (2^16) as a string."""
        data = 2**(16 - 1)
        exp = struct.pack('<H', data)

        try:
            result = utils.int2store(data)
        except ValueError as err:
            self.fail("int2store failed: {0}".format(str(err)))
        else:
            self._check_int_result(result, exp, data)
Esempio n. 17
0
    def test_int2store(self):
        """Use int2store to pack an integer (2^16) as a string."""
        data = 2**(16 - 1)
        exp = struct.pack('<H', data)

        try:
            result = utils.int2store(data)
        except ValueError as e:
            self.fail("int2store failed: %s" % e)
        else:
            if not isinstance(result, bytes):
                self.fail("Wrong result. Expected %s, we got %s" %\
                    (type(exp), type(result)))
            elif exp != result:
                self.fail("Wrong result. Expected %s, we got %s" %\
                    (data, result))
Esempio n. 18
0
 def test_int2store(self):
     """Use int2store to pack an integer (2^16) as a string."""
     data = 2**(16-1)
     exp = struct.pack('<H',data)
     
     try:
         result = utils.int2store(data)
     except ValueError as e:
         self.fail("int2store failed: %s" % e)
     else:
         if not isinstance(result, str):
             self.fail("Wrong result. Expected %s, we got %s" %\
                 (type(exp), type(result)))
         elif exp != result:
             self.fail("Wrong result. Expected %s, we got %s" %\
                 (data, result))