Пример #1
0
    def query2(self, sql, sql_params=None, database=None, dict_output=False):
        formatted_sql = format_sql(sql, sql_params)

        self.logger.debug('[INFLUXDB] Query2 {}'.format(
            re.sub('\s+', ' ', formatted_sql, flags=re.M)))

        try:
            db_res = self.client.query(formatted_sql, database=database)

        except Exception as e:
            for line in traceback.format_exc().splitlines():
                self.logger.error(line)

            raise

        else:
            is_list = isinstance(db_res, (list, tuple))
            db_res = toolkit.as_array(db_res)
            db_res_list = None
            if dict_output is False:
                db_res_list = [x.raw for x in db_res]
            else:
                db_res_list = [self.convert_to_dict(x) for x in db_res]

            if is_list:
                return db_res_list
            else:
                return db_res_list[0]
Пример #2
0
    def _trans_execute(self, trans_conn, sql, sql_params=None):
        formatted_sql = format_sql(sql, sql_params)

        if not self.skip_log:
            self.logger.debug('[MYSQL] Trans Query `{}`'.format(
                re.sub('\s+', ' ', formatted_sql, flags=re.M)))

        if not trans_conn:
            raise Exception('Transaction not started')

        conn = trans_conn['conn']
        cur = trans_conn['cur']

        count = cur.execute(formatted_sql)
        db_res = cur.fetchall()

        return list(db_res), count
Пример #3
0
    def query(self, sql, sql_params=None):
        formatted_sql = format_sql(sql, sql_params)

        if not self.skip_log:
            self.logger.debug('[CLICKHOUSE] {}'.format(
                re.sub('\s+', ' ', formatted_sql, flags=re.M)))

        conn = None
        cur = None

        try:
            conn = self.client.connection()
            cur = conn.cursor()

            cur.execute(formatted_sql)
            db_res = cur.fetchall()

            # tuple list -> dict list
            column_names = [c.name for c in cur.description]
            for i, d in enumerate(db_res):
                db_res[i] = dict(zip(column_names, d))

        except Exception as e:
            stack_text = traceback.format_exc()
            stack_text = re.sub('Stack trace:[\s\S]*', '', stack_text).strip()

            for line in stack_text.splitlines():
                self.logger.error(line)

            raise Exception(stack_text)

        else:
            return db_res

        finally:
            if cur:
                cur.close()

            if conn:
                conn.close()
Пример #4
0
    def _execute(self, sql, sql_params=None):
        formatted_sql = format_sql(sql, sql_params)

        if not self.skip_log:
            self.logger.debug('[POSTGRESQL] {}'.format(
                re.sub('\s+', ' ', formatted_sql, flags=re.M)))

        conn = None
        cur = None

        try:
            conn = self.client.connection()
            cur = conn.cursor()

            count = cur.execute(formatted_sql)
            db_res = cur.fetchall()

        except Exception as e:
            for line in traceback.format_exc().splitlines():
                self.logger.error(line)

            if conn:
                conn.rollback()

            raise

        else:
            conn.commit()

            db_res = to_db_res_dict(cur, db_res)
            return list(db_res), count

        finally:
            if cur:
                cur.close()

            if conn:
                conn.close()