示例#1
0
 def get_connection(self):
     co_result = None
     try:
         co_result = self._engine.connect()
         code_log.info('%s engine pool status is %s' % (self._db_name, self._engine.pool.status()))
         print('%s engine pool status is %s' % (self._db_name, self._engine.pool.status()))
     except Exception, ex:
         if zk.get("/credit_report/%s/sentry_flag" % env.lower())[0].decode('utf8') == 'true':
             sentry_log.captureException(tags={"db": "connection"})
示例#2
0
 def execute(self, cmd, get_all=True):
     """
     Execute a sql and return result, fetch one if not get_all.
     :param cmd: str, raw sql string.
     :param get_all: boolean. True if want to fetch all else fetch one.
     :return: Fetch all if get_all else fetch one.
     """
     try:
         # code_log.info('query cmd is %s' % cmd)
         result = self._connection.execute(cmd)
         try:
             result = result.fetchall() if get_all else result.fetchone()
         except Exception, e:
             if zk.get("/credit_report/%s/sentry_flag" % env.lower())[0].decode('utf8') == 'true':
                 sentry_log.captureException(tags={"db": "execute"})
             code_log.error('db_error: %s, cmd is %s' % (str(e), cmd))
         return result
示例#3
0
        :param cmd: str, raw sql string.
        :param get_all: boolean. True if want to fetch all else fetch one.
        :return: Fetch all if get_all else fetch one.
        """
        try:
            # code_log.info('query cmd is %s' % cmd)
            result = self._connection.execute(cmd)
            try:
                result = result.fetchall() if get_all else result.fetchone()
            except Exception, e:
                if zk.get("/credit_report/%s/sentry_flag" % env.lower())[0].decode('utf8') == 'true':
                    sentry_log.captureException(tags={"db": "execute"})
                code_log.error('db_error: %s, cmd is %s' % (str(e), cmd))
            return result
        except Exception, ex:
            if zk.get("/credit_report/%s/sentry_flag" % env.lower())[0].decode('utf8') == 'true':
                sentry_log.captureException(tags={"db": "connection"})
            raise DBInterfaceError(ex)

    def insert(self, table_name, data_dict):
        """
        Insert data_dict to table_name.
        :param table_name: str, table to insert into.
        :param data_dict: dict, {key: value} for insert.
        :return: primary keys of inserted elements if insert successfully.
        """
        try:
            table = Table(table_name, sqlalchemy.MetaData(), autoload=True, autoload_with=self._engine)
            stmt = table.insert().values(**data_dict)
            result = self._connection.execute(stmt)
            return result.inserted_primary_key[0] if result.inserted_primary_key else 0
示例#4
0
文件: db.py 项目: sxTrp/logTemplate
        self._connection = self.get_connection()
        try:
            code_log.info('query cmd is %s' % cmd)
            result = self._connection.execute(cmd)
            try:
                result = result.fetchall() if get_all else result.fetchone()
            except Exception, e:
                if zk.get("/credit_report/%s/sentry_flag" %
                          env.lower())[0].decode('utf8') == 'true':
                    sentry_log.captureException(tags={"db": "execute"})
                code_log.error('db_error: %s, cmd is %s' % (str(e), cmd))

            return result
        except Exception, ex:
            if zk.get("/credit_report/%s/sentry_flag" %
                      env.lower())[0].decode('utf8') == 'true':
                sentry_log.captureException(tags={"db": "connection"})
            raise DBInterfaceError(ex)
        finally:
            code_log.info('delete connection for db %s' % self._db_name)
            self._connection.close()

    def insert(self, table_name, data_dict):
        """
        Insert data_dict to table_name.
        :param table_name: str, table to insert into.
        :param data_dict: dict, {key: value} for insert.
        :return: primary keys of inserted elements if insert successfully.
        """
        self._connection = self.get_connection()
        try: