Example #1
0
    def send_data(self):
        """Send data packets from the local file to the server"""
        if not self.connection._writer:
            raise InterfaceError("(0, '')")
        conn = self.connection

        try:
            yield from self._open_file()
            with self._file_object:
                chunk_size = MAX_PACKET_LEN
                while True:
                    chunk = yield from self._file_read(chunk_size)
                    if not chunk:
                        break
                    conn.write_packet(chunk)
        finally:
            # send the empty packet to signify we are done sending data
            conn.write_packet(b"")
    def _execute_command(self, command, sql):
        if not self._writer:
            raise InterfaceError("(0, 'Not connected')")

        # If the last query was unbuffered, make sure it finishes before
        # sending new commands
        if self._result is not None:
            if self._result.unbuffered_active:
                warnings.warn("Previous unbuffered result was left incomplete")
                self._result._finish_unbuffered_query()
            while self._result.has_next:
                yield from self.next_result()
            self._result = None

        if isinstance(sql, str):
            sql = sql.encode(self._encoding)

        chunk_size = min(MAX_PACKET_LEN, len(sql) + 1)  # +1 is for command

        prelude = struct.pack('<i', chunk_size) + int2byte(command)
        self._write_bytes(prelude + sql[:chunk_size - 1])
        # logger.debug(dump_packet(prelude + sql))
        if chunk_size < MAX_PACKET_LEN:
            return

        seq_id = 1
        sql = sql[chunk_size - 1:]
        while True:
            chunk_size = min(MAX_PACKET_LEN, len(sql))
            prelude = struct.pack('<i', chunk_size)[:3]
            data = prelude + int2byte(seq_id % 256) + sql[:chunk_size]
            self._write_bytes(data)
            # logger.debug(dump_packet(data))
            sql = sql[chunk_size:]
            if not sql and chunk_size < MAX_PACKET_LEN:
                break
            seq_id += 1
Example #3
0
    def send_data(self):
        """Send data packets from the local file to the server"""
        if not self.connection._writer:
            raise InterfaceError("(0, '')")

        # sequence id is 2 as we already sent a query packet
        seq_id = 2
        try:
            yield from self._open_file()
            chunk_size = MAX_PACKET_LEN
            while True:
                chunk = yield from self._file_read(chunk_size)
                if not chunk:
                    break
                packet = (struct.pack('<i', len(chunk))[:3] + int2byte(seq_id))
                format_str = '!{0}s'.format(len(chunk))
                packet += struct.pack(format_str, chunk)
                self.connection._write_bytes(packet)
                seq_id += 1

        finally:
            # send the empty packet to signify we are done sending data
            packet = struct.pack('<i', 0)[:3] + int2byte(seq_id)
            self.connection._write_bytes(packet)
Example #4
0
 def _ensure_alive(self):
     if not self._writer:
         if self._close_reason is None:
             raise InterfaceError("(0, 'Not connected')")
         else:
             raise InterfaceError(self._close_reason)