Exemple #1
0
class AsyncMySQLBase(MySQLBase):
    """MySQLBase"""

    def _set_connection(self, prtcls=None):
        """Automatically chooses based on configuration which connection type to setup."""
        if self.unix_socket and os.name != 'nt':
#            self.conn = MySQLUnixConnection(prtcls=prtcls,
#                unix_socket=self.unix_socket)
            self.conn = AsyncMySQLUnixConnection(prtcls=prtcls,
                unix_socket=self.unix_socket)
        else:
#            self.conn = MySQLTCPConnection(prtcls=prtcls,
#                host=self.server_host, port=self.server_port)
            self.conn = AsyncMySQLTCPConnection(prtcls=prtcls,
                host=self.server_host, port=self.server_port)
        self.conn.set_connection_timeout(self.connection_timeout)
        
    def _open_connection(self):
        """Opens the connection and sets the appropriated protocol."""
        # We don't know yet the MySQL version we connect too
        self._set_connection()
        try:
#            self.conn.open_connection()
            yield self.conn.open_connection()
            version = self.conn.protocol.server_version
            if version < (4,1):
                raise InterfaceError("MySQL Version %s is not supported." % version)
            else:
#                self.conn.set_protocol(protocol.MySQLProtocol)
                self.conn.set_protocol(AsyncMySQLProtocol)
            self.protocol = self.conn.protocol
#            self.protocol.do_auth(username=self.username, password=self.password,
#                database=self.database)
            yield self.protocol.do_auth(username=self.username, password=self.password,
                database=self.database)
        except:
            raise
        
    def _post_connection(self):
        """Should be called after a connection was established"""
        self.get_characterset_info()
        self.set_converter_class(conversion.MySQLConverter)
        
        try:
#            self.set_charset(self.charset_name)
            yield self.set_charset(self.charset_name)
#            self.set_autocommit(self.autocommit)
            yield self.set_autocommit(self.autocommit)
        except:
            raise
            
    def is_connected(self):
        """
        Check whether we are connected to the MySQL server.
        """
#        return self.protocol.cmd_ping()
        result = yield self.protocol.cmd_ping()
        yield result
    ping = is_connected

    def disconnect(self):
        """
        Disconnect from the MySQL server.
        """
        if not self.conn:
#            return
            yield
            
        if self.conn.sock is not None:
#            self.protocol.cmd_quit()
            yield self.protocol.cmd_quit()
            try:
                self.conn.close_connection()
            except:
                pass
        self.protocol = None
        self.conn = None
    
    def set_charset(self, name):
        """
        Set the character set used for the connection. This is the recommended
        way of change it per connection basis. It does execute SET NAMES
        internally, but it's good not to use this command directly, since we
        are setting some other members accordingly.
        """
        if name not in constants.CharacterSet.get_supported():
            raise errors.ProgrammingError, "Character set '%s' not supported." % name
#            return
            yield
        try:
            info = constants.CharacterSet.get_charset_info(name)
        except errors.ProgrammingError, e:
            raise
        
        try:
#            self.protocol.cmd_query("SET NAMES '%s'" % name)
            yield self.protocol.cmd_query("SET NAMES '%s'" % name)
        except:
            raise
        else:
            (self.charset, self.charset_name, self.collation_name) = info
            self.converter.set_charset(self.charset_name)