-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DatabaseDriver.py
68 lines (52 loc) · 1.56 KB
/
DatabaseDriver.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
from DatabaseSchema import Ban
from dotenv import load_dotenv
from sqlalchemy import create_engine, select, URL, desc, func
from sqlalchemy.orm import Session
import os
load_dotenv()
# Mostly based off of the ScamGuard Database Option
class DatabaseDriver():
Database = None
### Initialization/Teardown ###
def __init__(self, *args, **kwargs):
self.Open()
def __del__(self):
self.Close()
def Open(self):
self.Close()
database_url = URL.create(
'sqlite',
username='',
password='',
host='',
database=DatabaseDriver.GetDatabaseFile(),
)
self.Database = Session(create_engine(database_url))
def Close(self):
if (self.IsConnected()):
self.Database.get_bind().dispose()
self.Database = None
def IsConnected(self) -> bool:
if (self.Database is not None):
return True
return False
@staticmethod
def GetDatabaseFile() -> str:
return os.getenv("DATABASE_FILE")
### Lookup Data ###
def DoesBanExist(self, TargetId:int) -> bool:
if (TargetId <= 0):
return False
stmt = select(Ban).where(Ban.discord_user_id==TargetId)
result = self.Database.scalars(stmt).first()
if (result is None):
return False
return True
def GetBanInfo(self, TargetId:int) -> Ban|None:
if (TargetId <= 0):
return None
stmt = select(Ban).where(Ban.discord_user_id==TargetId)
return self.Database.scalars(stmt).first()
def GetNumBans(self) -> int:
stmt = select(func.count()).select_from(Ban)
return self.Database.scalars(stmt).first()