-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatabase.py
72 lines (53 loc) · 2.34 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
import sqlite3
from contextlib import closing
class Operator:
def __init__(self, uuid: int, callsign: str, admin: bool):
self.uuid = uuid
self.callsign = callsign
self.admin = admin
class BotDatabase:
def __init__(self, filename):
self.connection = sqlite3.connect(filename)
with closing(self.connection.cursor()) as cursor:
cursor.execute(
'''CREATE TABLE IF NOT EXISTS operators
(uuid INTEGER, callsign TEXT, admin INTEGER)'''
)
def close(self):
self.commit()
self.connection.close()
def commit(self):
self.connection.commit()
def get_total_changes(self):
return self.connection.total_changes
def get_operator(self, operator_id: int):
try:
with closing(self.connection.cursor()) as cursor:
row = cursor.execute('SELECT * FROM operators WHERE uuid = ?', (operator_id,)).fetchall()[0]
except IndexError:
return None
return Operator(row[0], row[1], row[2])
def get_operators(self):
with closing(self.connection.cursor()) as cursor:
rows = cursor.execute('SELECT * FROM operators').fetchall()
operators = []
for row in rows:
uuid = row[0]
callsign = row[1]
admin = row[2]
operators.append(Operator(uuid, callsign, admin))
return operators
def add_operator(self, uuid: int, callsign: str, admin: bool = False):
admin_int = 1 if admin else 0
with closing(self.connection.cursor()) as cursor:
cursor.execute('INSERT INTO operators VALUES (?, ?, ?)',
(uuid, callsign, admin_int))
def delete_operator(self, operator_id: int):
with closing(self.connection.cursor()) as cursor:
cursor.execute('DELETE FROM operators WHERE uuid = ?', (operator_id,))
def update_operator_callsign(self, uuid: int, new_call: str):
with closing(self.connection.cursor()) as cursor:
cursor.execute('UPDATE operators SET callsign = ? WHERE uuid = ?', (new_call, uuid))
def update_operator_uuid(self, callsign: str, new_uuid: int):
with closing(self.connection.cursor()) as cursor:
cursor.execute('UPDATE operators SET uuid = ? WHERE callsign = ?', (new_uuid, callsign))