2017-02-04 00:08:12 +00:00
|
|
|
import threading
|
2020-06-05 22:03:37 +00:00
|
|
|
from typing import Any
|
|
|
|
|
2017-02-04 00:08:12 +00:00
|
|
|
import sqlalchemy as sa
|
|
|
|
import sqlalchemy.orm
|
|
|
|
|
2020-06-05 22:03:37 +00:00
|
|
|
from szurubooru import config
|
2020-06-05 14:02:18 +00:00
|
|
|
|
2017-02-04 00:08:12 +00:00
|
|
|
_data = threading.local()
|
2020-06-05 22:03:37 +00:00
|
|
|
_engine = sa.create_engine(config.config["database"]) # type: Any
|
2020-03-06 23:14:16 +00:00
|
|
|
_sessionmaker = sa.orm.sessionmaker(bind=_engine, autoflush=False) # type: Any
|
|
|
|
session = sa.orm.scoped_session(_sessionmaker) # type: Any
|
2017-02-04 00:08:12 +00:00
|
|
|
|
|
|
|
|
|
|
|
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:
|
2020-06-05 22:03:37 +00:00
|
|
|
_data.query_count = getattr(_data, "query_count", 0) + 1
|
2017-02-04 00:08:12 +00:00
|
|
|
|
|
|
|
|
2020-06-05 22:03:37 +00:00
|
|
|
sa.event.listen(_engine, "after_execute", lambda *args: _bump_query_count())
|