def _send(self, command, argument=None):
        '''
        ************************************************************
            Function    : _handle_result
            Arguments   : 1) command (int)     : SQL command flag. ServerCmd.QUERY
                          2) argument (string) : SQL command. Select 1
            Return      : packets (string)
            Description : Send a command to the GBase server
        ************************************************************
        '''
        GBASELOGGER.debug("Send packet. command='%s', argument='%s'" %
                          (command, argument))
        if self.unread_result:
            GBASELOGGER.error("Unread result found.")
            raise GBaseError.InternalError("Unread result found.")

        try:
            GBASELOGGER.debug("Make command.")
            packet = self._protocol.make_command(command, argument)
            GBASELOGGER.debug("Send packet.")
            self._socket.send_data(packet, 0)
        except AttributeError:
            GBASELOGGER.error("GBase Connection not available.")
            raise GBaseError.InternalError("GBase Connection not available.")

        GBASELOGGER.debug("Receive packet.")
        packet = self._socket.receive_packet()
        GBASELOGGER.debug("Return receive packet.")
        return packet
    def _open_connection(self):
        '''
        ************************************************************
            Function    : _open_connection
            Arguments   : 
            Return      : 
            Description : Use socket object open connection and make auth check
                          1) receive hello packet (shake hands)
                          2) auth check (user, pass)
                          3) set character
                          4) set auto commit
        ************************************************************
        '''
        # open socket
        GBASELOGGER.debug('Get socket object.')
        self._socket = self._get_connection()

        # hand shake
        GBASELOGGER.debug('Shake hand and send hello packet.')
        self._do_hello()

        # auth check
        GBASELOGGER.debug('Authorize and check user password.')
        if self._do_authcheck():
            # set charset
            GBASELOGGER.debug('Set character set and collation.')
            self.set_charset_collation(charset=self._charset_id)

            GBASELOGGER.debug("Set other initial info. Include "
                              "autocommit, time_zone, sql_mode, etc.")
            self._do_setotherinfo()
        else:
            GBASELOGGER.error('Authorize failure.')
            raise GBaseError.InternalError('Authorize failure.')
    def quit(self):
        '''
        ************************************************************
            Function    : quit
            Arguments   : 
            Return      : string
            Description : Send quit packet to the GBase Server
        ************************************************************
        '''
        if self.unread_result:
            GBASELOGGER.error("Unread result found.")
            raise GBaseError.InternalError("Unread result found.")

        GBASELOGGER.debug("Make command packet.")
        packet = self._protocol.make_command(ServerCmd.QUIT)

        GBASELOGGER.debug("Send command packet.")
        self._socket.send_data(packet, 0)
        return packet
    def get_rows(self, count=None):
        '''
        ************************************************************
            Function    : get_rows
            Arguments   : 1) count (int) : Fetch how much rows
            Return      : a tuple with 2 elements: a list with all rows and
                          the EOF packet.
            Description : Get all rows returned by the GBase server
        ************************************************************
        '''
        if not self.unread_result:
            GBASELOGGER.error("No result set available.")
            raise GBaseError.InternalError("No result set available.")

        GBASELOGGER.debug("Read result set rows.")
        rows = self._protocol.read_result_set(self._socket, count)
        if rows[-1] is not None:
            GBASELOGGER.debug(
                "Trigger next result and set unread_result=False.")
            self._trigger_next_result(rows[-1]['server_flag'])
            self.unread_result = False

        return rows
    def query_iterator(self, statements):
        '''
        ************************************************************
            Function    : query_iterator
            Arguments   : 1) statements (string) : multiple sql string
            Return      : 
            Description : Send one or more statements to the GBase server
        ************************************************************
        '''
        # Handle the first query result
        GBASELOGGER.debug("Handle the first query result '%s'" % statements)
        packet = self._send(ServerCmd.QUERY, statements)
        yield self._handle_result(packet)

        # Handle next results
        while self._have_next_result:
            if self.unread_result:
                GBASELOGGER.error("Unread result found.")
                raise GBaseError.InternalError("Unread result found.")
            else:
                GBASELOGGER.debug("Handle receive packet.")
                packet = self._socket.receive_packet()
                result = self._handle_result(packet)
            yield result