Beispiel #1
0
def query(sql, **kwargs):
    ti = time.time()
    query = text(sql).execution_options(autocommit=False)
    res = conn.execute(query, **kwargs)
    ms = (time.time() - ti) * 1000
    if ms > 100:
        disp = re.sub('\s+', ' ', sql).strip()[:200]
        print("\033[93m[SQL][{}ms] {}\033[0m".format(int(ms), disp))
    return res
Beispiel #2
0
def __query(sql, **kwargs):
    global _trx_active
    if sql == 'START TRANSACTION':
        assert not _trx_active
        _trx_active = True
    elif sql == 'COMMIT':
        assert _trx_active
        _trx_active = False

    _query = text(sql).execution_options(autocommit=False)
    try:
        return conn.execute(_query, **kwargs)
    except Exception as e:
        print("[SQL] Error in query {} ({})".format(sql, kwargs))
        #conn.close() # TODO: check if needed
        logger.exception(e)
        raise e
Beispiel #3
0
def query(sql, **kwargs):
    ti = time.perf_counter()
    query = text(sql).execution_options(autocommit=False)
    try:
        res = conn.execute(query, **kwargs)
        ms = int((time.perf_counter() - ti) * 1000)
        QueryStats.log(sql, ms)
        if ms > 100:
            disp = re.sub('\s+', ' ', sql).strip()[:250]
            print("\033[93m[SQL][{}ms] {}\033[0m".format(ms, disp))
        logger.debug(res)
        return res
    except Exception as e:
        print("[SQL] Error in query {} ({})".format(sql, kwargs))
        conn.close()
        logger.exception(e)
        raise e
Beispiel #4
0
def follower_count(account: str):
    q = select([func.count(hive_follows.c.hive_follows_fk1)]). \
        where(hive_follows.c.following == account). \
        as_scalar()
    return conn.execute(q)
Beispiel #5
0
def get_following(account: str, skip: int, limit: int):
    q = select([hive_follows]). \
        where(hive_follows.c.follower == account). \
        skip(skip).limit(limit)
    return conn.execute(q)
Beispiel #6
0
def query_one(sql):
    res = conn.execute(text(sql))
    row = first(res)
    if row:
        return first(row)
Beispiel #7
0
def query(sql, **kwargs):
    res = conn.execute(text(sql).execution_options(autocommit=False), **kwargs)
    return res