-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatabase.py
73 lines (62 loc) · 2.29 KB
/
database.py
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import os
import shutil
import sqlalchemy
from PyQt5.QtWidgets import QApplication
from cfg import JsonData, Static
from utils.utils import Utils
METADATA = sqlalchemy.MetaData()
THUMBS = sqlalchemy.Table(
"thumbs", METADATA,
sqlalchemy.Column("id", sqlalchemy.Integer, primary_key=True),
sqlalchemy.Column("short_src", sqlalchemy.Text),
sqlalchemy.Column("short_hash", sqlalchemy.Text, comment="Путь к маленькой картинке"),
sqlalchemy.Column("size", sqlalchemy.Integer, comment="Размер"),
sqlalchemy.Column("birth", sqlalchemy.Integer, comment="Дата созд."),
sqlalchemy.Column("mod", sqlalchemy.Integer, comment="Дата изм."),
sqlalchemy.Column("resol", sqlalchemy.TEXT, comment="1920x1080"),
sqlalchemy.Column("coll", sqlalchemy.Text, comment="Имя коллекции"),
sqlalchemy.Column("fav", sqlalchemy.Integer, comment="1 is fav else 0"),
sqlalchemy.Column("brand", sqlalchemy.TEXT, comment="miuz, panacea"),
)
CLMN_NAMES = [
i.name
for i in THUMBS.columns
][1:]
class Dbase:
engine: sqlalchemy.Engine = None
_timeout = 15
_echo = False
_same_thread = False
WAL_ = None
@classmethod
def init(cls) -> sqlalchemy.Engine:
cls.create_engine()
cls.toggle_wal(False)
@classmethod
def create_engine(cls):
if os.path.exists(Static.DB_FILE):
cls.engine = sqlalchemy.create_engine(
f"sqlite:///{Static.DB_FILE}",
echo=cls._echo,
connect_args={
"check_same_thread": cls._same_thread,
"timeout": cls._timeout
}
)
else:
t = "Нет пользовательского файла DB_FILE"
raise Exception(t)
@classmethod
def toggle_wal(cls, value: bool):
with cls.engine.connect() as conn:
if value:
conn.execute(sqlalchemy.text("PRAGMA journal_mode=WAL"))
cls.WAL_ = True
else:
conn.execute(sqlalchemy.text("PRAGMA journal_mode=DELETE"))
cls.WAL_ = False
@classmethod
def vacuum(cls):
conn = cls.engine.connect()
conn.execute(sqlalchemy.text("VACUUM"))
conn.commit()