def __init__(self): if pymysql: self._mysql_version = \ 'server: (not-connected), client: "%s", thread-safe: %s' % \ (pymysql.get_client_info(), pymysql.thread_safe()) else: self._mysql_version = None
def get_connection(self, path, log=None, user=None, password=None, host=None, port=None, params={}): cnx = MySQLConnection(path, log, user, password, host, port, params) if not self.required: self._mysql_version = \ 'server: "%s", client: "%s", thread-safe: %s' \ % (cnx.cnx.get_server_info(), pymysql.get_client_info(), pymysql.thread_safe()) self.required = True return cnx
def __init__(self, user, passwd, *args, **kwargs): """Create a connection to the database. It is strongly recommended that you only use keyword parameters. Consult the MySQL C API documentation for more information. Arguments: user: string, user to connect as. passwd: string, password to use. db: string, database to use. Default same as user. host: string, host to connect to. Default 'localhost'. port: integer, TCP/IP port to connect to. unix_socket: string, location of unix_socket to use. conv: conversion dictionary, see converters module. connect_timeout: number of seconds to wait before the connection attempt fails. compress: bool, enable compression. Not supported by PyMySQL. named_pipe: if set, a named pipe is used to connect Not supported by PyMySQL. init_command: command which is run once the connection is created read_default_file: file from which default client values are read read_default_group: configuration group to use from the default file use_unicode: If True, text-like columns are returned as unicode objects using the connection's character set. Otherwise, text-like columns are returned as strings. Columns are returned as normal strings. Unicode objects will always be encoded to the connection's character set regardless of this setting. charset: If supplied, the connection character set will be changed to this character set (MySQL-4.1 and newer). This enforces use_unicode=True. sql_mode: If supplied, the session SQL mode will be changed to this setting (MySQL-4.1 and newer). For more details and legal values, see the MySQL documentation. client_flag: integer, flags to use or 0. (see MySQL docs or constants/CLIENTS.py) ssl: dictionary or mapping, contains SSL connection parameters; see the MySQL documentation for more details (mysql_ssl_set()). If this is set, and the client does not support SSL, NotSupportedError will be raised. local_infile: bool, True enables LOAD LOCAL INFILE, False disables. Default False There are a number of undocumented, non-standard arguments. See the documentation for the MySQL C API for some hints on what they do. """ # Counters, transaction lock & timer self.counter_transactions = 0 self.counter_queries = 0 self.queries = [] self.transaction_timer = None self.lock = threading.Lock() self._charset = None # PyMySQL connect args mapping kwargs['user'] = user kwargs['passwd'] = passwd kwargs['host'] = kwargs.get('host', 'localhost') kwargs['db'] = kwargs.get('db', user) if 'compress' in kwargs: raise self.NotSupportedError( "PyMySQL doesn't support compression.") if 'named_pipe' in kwargs: raise self.NotSupportedError( "PyMySQL doesn't support named pipes.") self.logger = logging.getLogger('mysql_%s' % kwargs['db']) if kwargs.pop('debug', False): self.debug = True self.logger.setLevel(logging.DEBUG) else: self.debug = False self.logger.setLevel(logging.WARNING) if kwargs.pop('disable_log', False): self.logger.disable_logger = True autocommit = kwargs.pop('autocommit', None) charset = kwargs.pop('charset', 'utf8') sql_mode = kwargs.pop('sql_mode', None) # use_unicode = kwargs.pop('use_unicode', False) or bool(charset) # The following voodoo is necessary to avoid double references that would # prevent a connection object from being finalized and collected properly. db = weakref.proxy(self) # def _GetUnicodeLiteral(): # def UnicodeLiteral(u_string, _dummy=None): # """Returns the SQL (safe) literal for the given unicode object.""" # return db.EscapeValues(u_string.encode(db.charset)) # return UnicodeLiteral def _GetStringDecoder(): def StringDecoder(string): """Returns the unicode codepoints for an encoded bytestream.""" return string.decode(db.charset) return StringDecoder encoders = {} converts = {} conversions = converters.CONVERSIONS self.string_decoder = _GetStringDecoder() # if use_unicode: # decoder = self.string_decoder # conversions[constants.FIELD_TYPE.STRING] = decoder # conversions[constants.FIELD_TYPE.VAR_STRING] = decoder # conversions[constants.FIELD_TYPE.VARCHAR] = decoder # conversions[constants.FIELD_TYPE.BLOB] = decoder for key, value in conversions.items(): if not isinstance(key, int): encoders[key] = value else: if isinstance(value, list): converts[key] = value[:] else: converts[key] = value kwargs.setdefault('conv', {}).update(converts) client_version = tuple( map(int, pymysql.get_client_info().split('.')[:2])) kwargs.setdefault('client_flag', 0) if client_version >= (4, 1): kwargs['client_flag'] |= constants.CLIENT.MULTI_STATEMENTS if client_version >= (5, 0): kwargs['client_flag'] |= constants.CLIENT.MULTI_RESULTS # Done redefining variables for initialization. Engage PyMySQL! super(Connection, self).__init__(*args, **kwargs) self.encoders = encoders # self.encoders[unicode] = self.unicode_literal = _GetUnicodeLiteral() self.converter = conversions self.server_version = tuple( map(int, self.get_server_info().split('.')[:2])) if sql_mode: self.SetSqlMode(sql_mode) self.charset = charset or self.character_set_name() self.transactional = bool(self.server_capabilities & constants.CLIENT.TRANSACTIONS) self._autocommit = None if autocommit is not None: self.autocommit = autocommit else: self.autocommit = not self.transactional
def get_mysql_version(self): """return the mysql version""" return pymysql.get_client_info()
def get_mysql_version(self): pymysql.get_client_info()