示例#1
0
文件: db.py 项目: yourant/ur_cleaner
    def _connect(self):
        try:
            SysLogger().log.info('connect {}...'.format(self.base_name))

            if self.base_name == 'mssql':

                import pymssql
                return pymssql.connect(**config.get_config('mssql'))

            if self.base_name == 'mysql':

                import pymysql
                return pymysql.connect(**config.get_config('mysql'))

            if self.base_name == 'erp':

                import pymysql
                data_base_config = config.get_config('erp')
                if data_base_config:
                    return pymysql.connect(**config.get_config('erp'))

            if self.base_name == 'ibay':
                data_base_config = config.get_config('ibay')
                if data_base_config:
                    import psycopg2
                    return psycopg2.connect(**config.get_config('ibay'))

        except Exception as why:
            SysLogger().log.info('can not connect {} cause of {}'.format(self.base_name, why))
            return None
示例#2
0
 def close(self):
     if self.used_count == 1:
         SysLogger().log.info('close {}...'.format(self.base_name))
         self.connect.close()
     if self.used_count > 1:
         SysLogger().log.info(
             'close {} by decreasing one connection'.format(self.base_name))
         self.used_count -= 1
示例#3
0
文件: db.py 项目: yourant/ur_cleaner
 def close(self):
     try:
         if self.used_count == 1:
             SysLogger().log.info('close {}...'.format(self.base_name))
             self.connect.close()
         if self.used_count > 1:
             SysLogger().log.info('close {} by decreasing one connection'.format(self.base_name))
             self.used_count -= 1
     except Exception as why:
         SysLogger().log.error('fail to close connection cause of {}'.format(why))
示例#4
0
 def __init__(self, base_name):
     self.base_name = base_name
     if not self.connect:
         self.used_count += 1
         SysLogger().log.info('not existing {} connection...'.format(
             self.base_name))
         self.connect = self._connect()
示例#5
0
 def __init__(self):
     self.db_con_container = dict()
     self.logger = SysLogger().log
示例#6
0
class BaseDao(object):
    """
    database singleton connection
    """
    def __init__(self):
        self.db_con_container = dict()
        self.logger = SysLogger().log

    def connect_database(self, base_name):
        try:
            connection = self.db_con_container.get(base_name, None)
            if not connection:
                connection = self.connect(base_name)
                self.db_con_container[base_name] = connection
            return connection
        except Exception as e:
            self.logger.error('Can not connect to database!', exc_info=True)
            self.db_con_container[base_name] = None
            raise Exception('Can not connect to database!', e)

    def get_connection(self, base_name):
        connection = self.db_con_container.get(base_name, None)
        if not connection:
            connection = self.connect_database(base_name)
        return connection

    def connect(self, base_name):
        try:
            self.logger.info('connect {}...'.format(base_name))

            if base_name == 'mssql':
                import pymssql
                return pymssql.connect(**config.get_config('mssql'))

            if base_name == 'mysql':
                return pymysql.connect(**config.get_config('mysql'))

            if base_name == 'erp':
                return pymysql.connect(**config.get_config('erp'))

            if base_name == 'yun':
                return pymysql.connect(**config.get_config('yun'))

            if base_name == 'ibay':
                import psycopg2
                ibay_con = psycopg2.connect(**config.get_config('ibay'))
                ibay_con.set_client_encoding('utf8')
                return ibay_con

        except Exception as why:
            self.logger.error('can not connect {} cause of {}'.format(
                base_name, why))
            raise why

    def get_cur(self, base_name):
        connection = self.get_connection(base_name)
        cur = None
        if connection:
            if base_name == 'mssql':
                cur = connection.cursor(as_dict=True)

            if base_name == 'mysql':
                cur = connection.cursor(pymysql.cursors.DictCursor)

            if base_name == 'erp':
                cur = connection.cursor(pymysql.cursors.DictCursor)

            if base_name == 'ibay':
                cur = connection.cursor()

            if base_name == 'yun':
                cur = connection.cursor()

        return cur

    def close_cur(self, cur):
        try:
            cur.close()
            self.logger.info(f' close cursor')
        except Exception as why:
            self.logger.error(f'can not close cursor cause of {why}')

    def close(self, base_name):
        connection = self.db_con_container.get(base_name, None)
        try:
            if not connection:
                connection.colse()
        except Exception as why:
            self.logger.error('can not close {} cause of {}'.format(
                base_name, why))

        finally:
            self.db_con_container[base_name] = None