Ejemplo n.º 1
0
    def _send_data(self, fp, send_empty_packet=False):
        """Send data to the MySQL server

        This method accepts a file-like object and sends its data
        as is to the MySQL server. If the send_empty_packet is
        True, it will send an extra empty package (for example
        when using LOAD LOCAL DATA INFILE).

        Returns a MySQL packet.
        """
        if self.unread_result:
            raise errors.InternalError("Unread result found.")

        if not hasattr(fp, 'read'):
            raise ValueError("expecting a file-like object")

        try:
            buf = fp.read(4096)
            while buf:
                self._socket.send(buf)
                buf = fp.read(4096)
        except AttributeError:
            raise errors.OperationalError("MySQL Connection not available.")

        if send_empty_packet:
            try:
                self._socket.send('')
            except AttributeError:
                raise errors.OperationalError(
                    "MySQL Connection not available.")

        return self._socket.recv()
Ejemplo n.º 2
0
 def send_plain(self, buf, pktnr):
     pkts = self._prepare_packets(buf,pktnr)
     
     for pkt in pkts:
         try:
             self.sock.sendall(pkt)
         except Exception, e:
             raise errors.OperationalError('%s' % e)
Ejemplo n.º 3
0
 def send(self, buf):
     """Send packets over the socket
     """
     pktlen = len(buf)
     try:
         while pktlen:
             pktlen -= self.sock.send(buf)
     except Exception, e:
         raise errors.OperationalError('%s' % e)
Ejemplo n.º 4
0
    def send_compressed(self, buf, packet_number=None):
        """Send compressed packets to the MySQL server"""
        if packet_number is None:
            self.next_packet_number
        else:
            self._packet_number = packet_number
        pktnr = self._packet_number
        pllen = len(buf)
        zpkts = []
        maxpktlen = constants.MAX_PACKET_LENGTH
        if pllen > maxpktlen:
            pkts = _prepare_packets(buf, pktnr)
            tmpbuf = ''.join(pkts)
            del pkts
            seqid = 0
            zbuf = zlib.compress(tmpbuf[:16384])
            zpkts.append(
                struct.pack('<I', len(zbuf))[0:3] + struct.pack('<B', seqid) +
                '\x00\x40\x00' + zbuf)
            tmpbuf = tmpbuf[16384:]
            pllen = len(tmpbuf)
            seqid = seqid + 1
            while pllen > maxpktlen:
                zbuf = zlib.compress(tmpbuf[:maxpktlen])
                zpkts.append(
                    struct.pack('<I', len(zbuf))[0:3] +
                    struct.pack('<B', seqid) + '\xff\xff\xff' + zbuf)
                tmpbuf = tmpbuf[maxpktlen:]
                pllen = len(tmpbuf)
                seqid = seqid + 1
            if tmpbuf:
                zbuf = zlib.compress(tmpbuf)
                zpkts.append(
                    struct.pack('<I', len(zbuf))[0:3] +
                    struct.pack('<B', seqid) + struct.pack('<I', pllen)[0:3] +
                    zbuf)
            del tmpbuf
        else:
            pkt = (struct.pack('<I', pllen)[0:3] + struct.pack('<B', pktnr) +
                   buf)
            pllen = len(pkt)
            if pllen > 50:
                zbuf = zlib.compress(pkt)
                zpkts.append(
                    struct.pack('<I', len(zbuf))[0:3] + struct.pack('<B', 0) +
                    struct.pack('<I', pllen)[0:3] + zbuf)
            else:
                zpkts.append(
                    struct.pack('<I', pllen)[0:3] + struct.pack('<B', 0) +
                    struct.pack('<I', 0)[0:3] + pkt)

        for zip_packet in zpkts:
            try:
                self.sock.sendall(zip_packet)
            except Exception, err:
                raise errors.OperationalError('%s' % err)
Ejemplo n.º 5
0
    def send_plain(self, buf, pktnr):
        pkts = self._prepare_packets(buf, pktnr)

        for pkt in pkts:
            pktlen = len(pkt)
            try:
                while pktlen:
                    pktlen -= self.sock.send(pkt)
            except Exception, e:
                raise errors.OperationalError('%s' % e)
Ejemplo n.º 6
0
 def send_plain(self, buf, packet_number=None):
     """Send packets to the MySQL server"""
     if packet_number is None:
         self.next_packet_number
     else:
         self._packet_number = packet_number
     packets = _prepare_packets(buf, self._packet_number)
     for packet in packets:
         try:
             self.sock.sendall(packet)
         except Exception, err:
             raise errors.OperationalError(str(err))
Ejemplo n.º 7
0
    def send_compressed(self, buf, pktnr):
        pllen = len(buf)
        zpkts = []
        if pllen > 16777215:
            pkts = self._prepare_packets(buf, pktnr)
            tmpbuf = ''.join(pkts)
            del pkts
            seqid = 0
            zbuf = zlib.compress(tmpbuf[:16384])
            zpkts.append(
                struct.pack('<I', len(zbuf))[0:3] + struct.pack('<B', seqid) +
                '\x00\x40\x00' + zbuf)
            tmpbuf = tmpbuf[16384:]
            pllen = len(tmpbuf)
            seqid = seqid + 1
            while pllen > MAX_PACKET_LENGTH:
                zbuf = zlib.compress(tmpbuf[:MAX_PACKET_LENGTH])
                zpkts.append(
                    struct.pack('<I', len(zbuf))[0:3] +
                    struct.pack('<B', seqid) + '\xff\xff\xff' + zbuf)
                tmpbuf = tmpbuf[MAX_PACKET_LENGTH:]
                pllen = len(tmpbuf)
                seqid = seqid + 1
            if tmpbuf:
                zbuf = zlib.compress(tmpbuf)
                zpkts.append(
                    struct.pack('<I', len(zbuf))[0:3] +
                    struct.pack('<B', seqid) + struct.pack('<I', pllen)[0:3] +
                    zbuf)
            del tmpbuf
        else:
            pkt = (struct.pack('<I', pllen)[0:3] + struct.pack('<B', pktnr) +
                   buf)
            pllen = len(pkt)
            if pllen > 50:
                zbuf = zlib.compress(pkt)
                zpkts.append(
                    struct.pack('<I', len(zbuf))[0:3] + struct.pack('<B', 0) +
                    struct.pack('<I', pllen)[0:3] + zbuf)
            else:
                zpkts.append(
                    struct.pack('<I', pllen)[0:3] + struct.pack('<B', 0) +
                    struct.pack('<I', 0)[0:3] + pkt)

        for zpkt in zpkts:
            zpktlen = len(zpkt)
            try:
                while zpktlen:
                    zpktlen -= self.sock.send(zpkt)
            except Exception, e:
                raise errors.OperationalError('%s' % e)
Ejemplo n.º 8
0
    def _handle_load_data_infile(self, filename):
        """Handle a LOAD DATA INFILE LOCAL request"""
        try:
            fp = open(filename, 'rb')
        except IOError:
            # Send a empty packet to cancel the operation
            try:
                self._socket.send('')
            except AttributeError:
                raise errors.OperationalError(
                    "MySQL Connection not available.")
            raise errors.InterfaceError("File '%s' could not be read" %
                                        filename)

        return self._handle_ok(self._send_data(fp, send_empty_packet=True))
Ejemplo n.º 9
0
    def _send_cmd(self, command, argument=None, packet_number=0):
        """Send a command to the MySQL server

        This method sends a command with an optional argument.

        Returns a MySQL packet
        """
        if self.unread_result:
            raise errors.InternalError("Unread result found.")

        try:
            self._socket.send(self._protocol.make_command(command, argument),
                              packet_number)
        except AttributeError:
            raise errors.OperationalError("MySQL Connection not available.")

        return self._socket.recv()
Ejemplo n.º 10
0
    def cursor(self, buffered=None, raw=None, cursor_class=None):
        """Instantiates and returns a cursor

        By default, MySQLCursor is returned. Depending on the options
        while connecting, a buffered and/or raw cursor instantiated
        instead.

        It is possible to also give a custom cursor through the
        cursor_class paramter, but it needs to be a subclass of
        mysql.connector.cursor.CursorBase.

        Returns a cursor-object
        """
        if not self.is_connected():
            raise errors.OperationalError("MySQL Connection not available.")
        if cursor_class is not None:
            if not issubclass(cursor_class, CursorBase):
                raise errors.ProgrammingError(
                    "Cursor class needs to be subclass of cursor.CursorBase")
            return (cursor_class)(self)

        buffered = buffered or self._buffered
        raw = raw or self._raw

        cursor_type = 0
        if buffered is True:
            cursor_type |= 1
        if raw is True:
            cursor_type |= 2

        types = (
            MySQLCursor,  # 0
            MySQLCursorBuffered,
            MySQLCursorRaw,
            MySQLCursorBufferedRaw,
        )
        return (types[cursor_type])(self)