def _handle_ok(self, packet): ''' ************************************************************ Function : _handle_ok Arguments : packet (string) : need handle packet Return : dict Description : Handle ok packet. ************************************************************ ''' if self._protocol.is_ok(packet): GBASELOGGER.debug('Parse ok packet.') ok_packet = self._protocol.parse_ok_res(packet) GBASELOGGER.debug('Get server status from packet.') flag = ok_packet['server_status'] GBASELOGGER.debug('Trigger next result.') self._trigger_next_result(flag) return ok_packet elif self._protocol.is_error(packet): GBASELOGGER.error("No receive ok packet or package is error.") raise GBaseError.InterfaceError( "No receive ok packet or package is error.") GBASELOGGER.error('Packet is not OK packet') raise GBaseError.InterfaceError('Packet is not OK packet')
def _do_authcheck(self): ''' ************************************************************ Function : _do_authcheck Arguments : Return : bool Description : Send authorize check packet (user, pass) ************************************************************ ''' # make auth packet try: GBASELOGGER.debug('Make auth packet.') packet = self._protocol.make_auth(self._user, self._password, self._hello_res['scramble'], self._database, self._charset_id, self._client_flags) except: GBASELOGGER.error('Make auth packet exception.') raise GBaseError.InterfaceError('Make auth packet exception.') # send auth package try: GBASELOGGER.debug("Send auth packet.") self._socket.send_data(packet) except: GBASELOGGER.error('Send auth packet exception.') raise GBaseError.InterfaceError('Send auth packet exception.') # receive server packet try: GBASELOGGER.debug("Receive auth packet.") packet = self._socket.receive_packet() except: GBASELOGGER.error('Receive packet exception.') raise GBaseError.InterfaceError('Receive packet exception.') # judgement eof packet if self._protocol.is_eof(packet): raise GBaseError.NotSupportedError( "Old passwords mode is not supported.") if self._protocol.is_error(packet): res = self._protocol.parse_error_res(packet) GBASELOGGER.error( "Packet is error. Errno=%s ErrMsg=%s SqlState=%s" % (res['errno'], res['errmsg'], res['sqlstate'])) raise GBaseError.get_gbase_exception(res) try: if (not (self._client_flags & ClientFlag.CONNECT_WITH_DB) and self._database): GBASELOGGER.debug('Initial database.') self.initdb(self._database) except: GBASELOGGER.error('Initial database exception.') raise return True
def _handle_result(self, packet): ''' ************************************************************ Function : _handle_result Arguments : packet (string) : need handle packet Return : dict Description : Get a GBase Result ************************************************************ ''' if not packet or len(packet) < 4: GBASELOGGER.error('GBase Server can not response') raise GBaseError.InterfaceError('GBase Server can not response') elif self._protocol.is_ok(packet): GBASELOGGER.debug("Packet is OK.") return self._handle_ok(packet) elif self._protocol.is_eof(packet): GBASELOGGER.error("Packet is EOF.") return self._handle_eof(packet) elif self._protocol.is_error(packet): res = self._protocol.parse_error_res(packet) GBASELOGGER.error( "Packet is error. Errno=%s ErrMsg=%s SqlState=%s" % (res['errno'], res['errmsg'], res['sqlstate'])) raise GBaseError.get_gbase_exception(res) # We have a text result set GBASELOGGER.debug('Parse column count.') column_count = self._protocol.parse_column_count(packet) if not column_count or not isinstance(column_count, int): GBASELOGGER.error('Result set is not valid.') raise GBaseError.InterfaceError('Result set is not valid.') GBASELOGGER.debug("Loop receive column. count:'%s'" % column_count) columns = [ None, ] * column_count for i in xrange(0, column_count): packet = self._socket.receive_packet() GBASELOGGER.debug("Receive column packet and parse packet.") columns[i] = self._protocol.parse_column_res(packet) GBASELOGGER.debug("Recive EOF packet.") packet = self._socket.receive_packet() GBASELOGGER.debug("Handle EOF packet.") eof_packet = self._handle_eof(packet) GBASELOGGER.debug("Set unread_result=True.") self.unread_result = True return {'columns': columns, 'eof': eof_packet}
def _handle_eof(self, packet): ''' ************************************************************ Function : _handle_eof Arguments : packet (string) : need handle packet Return : list Description : Handle a GBase EOF packet ************************************************************ ''' if self._protocol.is_eof(packet): GBASELOGGER.debug('Parse EOF packet.') eof_packet = self._protocol.parse_eof(packet) GBASELOGGER.debug('Get status flag.') server_flag = eof_packet['server_flag'] GBASELOGGER.debug('Trigger next result.') self._trigger_next_result(server_flag) return eof_packet elif self._protocol.is_error(packet): res = self._protocol.parse_error_res(packet) GBASELOGGER.error( "Packet is error. Errno=%s ErrMsg=%s SqlState=%s" % (res['errno'], res['errmsg'], res['sqlstate'])) raise GBaseError.get_gbase_exception(res) GBASELOGGER.error('Expected EOF packet') raise GBaseError.InterfaceError('Expected EOF packet')
def reconnect(self, attempts=1, delay=0): ''' ************************************************************ Function : ping Arguments : 1) attempts (int): Try count 2) delay (int): Delay time when try re-connect Return : Description : Attempt to reconnect to the GBase server. Default only try once. ************************************************************ ''' counter = 0 while counter != attempts: counter = counter + 1 try: GBASELOGGER.debug("Disconnect to GBase server.") self.disconnect() GBASELOGGER.debug("Connect to GBase server.") self.connect() except Exception, err: if counter == attempts: GBASELOGGER.error("Can not reconnect to GBase after %s " "attempt(%s): " % (attempts, err)) raise GBaseError.InterfaceError( "Can not reconnect to GBase after %s " "attempt(%s): " % (attempts, err)) if delay > 0: time.sleep(delay)
def _do_hello(self): ''' ************************************************************ Function : _do_hello Arguments : Return : Description : Receive hello packet (shake hands). Will receive to hello packet when first connect to GBase server ************************************************************ ''' try: GBASELOGGER.debug('Receive hello package.') packet = self._socket.receive_packet() except: GBASELOGGER.error("Receive packet exception") raise GBaseError.InterfaceError("Receive packet exception.") if self._protocol.is_error(packet): res = self._protocol.parse_error_res(packet) GBASELOGGER.error( "Packet is error. Errno=%s ErrMsg=%s SqlState=%s" % (res['errno'], res['errmsg'], res['sqlstate'])) raise GBaseError.get_gbase_exception(res) try: GBASELOGGER.debug('Parse hello package.') hello_res = self._protocol.parse_hello_res(packet) except: GBASELOGGER.error('Parse hello package error.') raise GBaseError.InterfaceError('Parse hello package error.') try: # get server version from package server_version = hello_res['server_version_original'] except: GBASELOGGER.error('Hello package no server_version property.') raise GBaseError.InterfaceError( 'Hello package no server_version property.') GBASELOGGER.debug("Get hello package & server_version info " "from hello package. " "hello_res: '%s' server_version: '%s'" % (hello_res, server_version)) self._hello_res = hello_res self._server_version = server_version
def ping(self, reconnect=False, attempts=1, delay=0): ''' ************************************************************ Function : ping Arguments : 1) reconnect (bool): Is or not re-connect GBase server 2) attempts (int): Try count 3) delay (int): Delay time when try re-connect Return : Description : Send ping command to GBase server ************************************************************ ''' try: packet = self._send(ServerCmd.PING) GBASELOGGER.debug("Send handle ok command.") self._handle_ok(packet) except: if reconnect: GBASELOGGER.debug("Send reconnect to GBase server.") self.reconnect(attempts=attempts, delay=delay) else: GBASELOGGER.error("Connection to GBase is not available.") raise GBaseError.InterfaceError( "Connection to GBase is not available.")