class Mysql(object): ESCAPE_REGEX = re.compile(r"[\0\n\r\032\'\"\\]") ESCAPE_MAP = {'\0': '\\0', '\n': '\\n', '\r': '\\r', '\032': '\\Z', '\'': '\\\'', '"': '\\"', '\\': '\\\\'} _instances = {} @classmethod def instance(cls, *config): if cls._instances.get(config) is None: cls._instances[config] = cls(*config) return cls._instances.get(config) def __init__(self, *config): """ :param host :param port :param user :param password :param db :param auto_commit :param charset """ self._conn = Connection() self._config = config @property def conn(self): if not self._conn.is_connected(): try: self._conn.connect(*self._config) except socket.gaierror: raise MysqlError('cannot resolve hostname') except socket.error: raise MysqlError('cannot connect to mysql') return self._conn def execute(self, sql, args=None): evt = { 'sql': sql, 'args': args } LOGGER.debug("sql_execute:{}".format(evt)) if not isinstance(args, (tuple, list, set)): args = (args,) try: query = self.conn.query(sql, args) except (socket.error, Error), e: LOGGER.info("connection error:{}".format(e)) self._conn = Connection() query = self.conn.query(sql, args) if query: return Cursor(query) else: return None
def __init__(self, *config): """ :param host :param port :param user :param password :param db :param auto_commit :param charset """ self._conn = Connection() self._config = config
def execute(self, sql, args=None): evt = { 'sql': sql, 'args': args } LOGGER.debug("sql_execute:{}".format(evt)) if not isinstance(args, (tuple, list, set)): args = (args,) try: query = self.conn.query(sql, args) except (socket.error, Error), e: LOGGER.info("connection error:{}".format(e)) self._conn = Connection() query = self.conn.query(sql, args)
class Mysql(object): ESCAPE_REGEX = re.compile(r"[\0\n\r\032\'\"\\]") ESCAPE_MAP = {'\0': '\\0', '\n': '\\n', '\r': '\\r', '\032': '\\Z', '\'': '\\\'', '"': '\\"', '\\': '\\\\'} _instances = {} @classmethod def instance(cls, *config): if cls._instances.get(config) is None: cls._instances[config] = cls(*config) return cls._instances.get(config) def __init__(self, *config): """ :param host :param port :param user :param password :param db :param auto_commit :param charset """ self._conn = Connection() self._config = config @property def conn(self): if not self._conn.is_connected(): self._conn.connect(*self._config) return self._conn def execute(self, sql, args=None): evt = { 'sql': sql, 'args': args } LOGGER.debug("sql_execute:{}".format(evt)) if not isinstance(args, (tuple, list)): args = (args,) query = self.conn.query(sql, args) if query: return Cursor(query) else: return None def rows_found(self): result = Cursor(self.conn.query('SELECT FOUND_ROWS() AS rows_found')) row = result.fetchone() if row: return row['rows_found'] return 0 def escape_string(self, raw_str): if isinstance(raw_str, unicode): raw_str = raw_str.encode('utf8') return ("%s" % (self.ESCAPE_REGEX.sub( lambda match: self.ESCAPE_MAP.get(match.group(0)), raw_str),)) @staticmethod def escape_table(table_name): return "`{}`".format(table_name) def begin(self): self.conn.query("BEGIN") def commit(self): self.conn.query("COMMIT") def rollback(self): if self.conn.is_connected(): self.conn.query("ROLLBACK") def close(self): if self.conn.is_connected(): self.commit() self.conn.close()
class Mysql(object): ESCAPE_REGEX = re.compile(r"[\0\n\r\032\'\"\\]") ESCAPE_MAP = { '\0': '\\0', '\n': '\\n', '\r': '\\r', '\032': '\\Z', '\'': '\\\'', '"': '\\"', '\\': '\\\\' } _instances = {} @classmethod def instance(cls, *config): if cls._instances.get(config) is None: cls._instances[config] = cls(*config) return cls._instances.get(config) def __init__(self, *config): """ :param host :param port :param user :param password :param db :param auto_commit :param charset """ self._conn = Connection() self._config = config @property def conn(self): if not self._conn.is_connected(): self._conn.connect(*self._config) return self._conn def execute(self, sql, args=None): evt = {'sql': sql, 'args': args} LOGGER.debug("sql_execute:{}".format(evt)) if not isinstance(args, (tuple, list)): args = (args, ) query = self.conn.query(sql, args) if query: return Cursor(query) else: return None def rows_found(self): result = Cursor(self.conn.query('SELECT FOUND_ROWS() AS rows_found')) row = result.fetchone() if row: return row['rows_found'] return 0 def escape_string(self, raw_str): if isinstance(raw_str, unicode): raw_str = raw_str.encode('utf8') return ("%s" % (self.ESCAPE_REGEX.sub( lambda match: self.ESCAPE_MAP.get(match.group(0)), raw_str), )) @staticmethod def escape_table(table_name): return "`{}`".format(table_name) def begin(self): self.conn.query("BEGIN") def commit(self): self.conn.query("COMMIT") def rollback(self): if self.conn.is_connected(): self.conn.query("ROLLBACK") def close(self): if self.conn.is_connected(): self.commit() self.conn.close()