summaryrefslogtreecommitdiff
path: root/server/szurubooru/db.py
blob: ed59a70aa2a32a6a9f5d70d9e97e6512803358e6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import threading
from typing import Any

import sqlalchemy as sa
import sqlalchemy.orm

from szurubooru import config

_data = threading.local()
_engine = sa.create_engine(config.config["database"])  # type: Any
_sessionmaker = sa.orm.sessionmaker(bind=_engine, autoflush=False)  # type: Any
session = sa.orm.scoped_session(_sessionmaker)  # type: Any


def get_session() -> Any:
    global session
    return session


def set_sesssion(new_session: Any) -> None:
    global session
    session = new_session


def reset_query_count() -> None:
    _data.query_count = 0


def get_query_count() -> int:
    return _data.query_count


def _bump_query_count() -> None:
    _data.query_count = getattr(_data, "query_count", 0) + 1


sa.event.listen(_engine, "after_execute", lambda *args: _bump_query_count())