-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtimer_database_connector.py
More file actions
75 lines (56 loc) · 2.59 KB
/
timer_database_connector.py
File metadata and controls
75 lines (56 loc) · 2.59 KB
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
74
75
import psycopg2
from database import config
from secrets import token_urlsafe
from database_connector import DatabaseConnector
class TimerDatabaseConnector(DatabaseConnector):
def __init__(self):
super().__init__()
self.connection = self.connect()
def add_session(self, consumable, room_id, is_break):
self.connection = self.connect()
cursor = self.connection.cursor()
session_id = token_urlsafe(8)
cursor.execute("""SELECT consumable FROM session WHERE id = %s""", \
(session_id,))
while cursor.fetchone() is not None:
session_id = token_urlsafe(8)
cursor.execute("""SELECT consumable FROM session WHERE id = %s""", \
(session_id,))
cursor.execute("""INSERT INTO session (id, consumable, room_id, is_break) VALUES(%s, %s, %s, %s)""", \
(session_id, consumable, room_id, is_break))
self.connection.commit()
cursor.close()
return session_id
def add_span(self, session_id, account_id, start_time, listing_id=None):
self.connection = self.connect()
cursor = self.connection.cursor()
span_id = token_urlsafe(8)
cursor.execute("""SELECT listing_id FROM span WHERE id = %s""", \
(span_id,))
while cursor.fetchone() is not None:
span_id = token_urlsafe(8)
cursor.execute("""SELECT listing_id FROM span WHERE id = %s""", \
(span_id,))
if listing_id == None:
cursor.execute("""INSERT INTO span (id, account_id, session_id, start_time) VALUES(%s, %s, %s, %s)""", (span_id, account_id, session_id, start_time,))
else:
cursor.execute("""INSERT INTO span (id, listing_id, account_id, session_id, start_time) VALUES(%s, %s, %s, %s, %s)""", (span_id, listing_id, account_id, session_id, start_time,))
self.connection.commit()
cursor.close()
return span_id
def add_end_time_to_span(self, span_id, end_time):
self.connection = self.connect()
cursor = self.connection.cursor()
cursor.execute("""UPDATE span SET end_time = %s WHERE id = %s""", \
(end_time, span_id,))
self.connection.commit()
cursor.close()
return True
def add_wealth(self, account_id, wealth):
self.connection = self.connect()
cursor = self.connection.cursor()
cursor.execute("""UPDATE account SET wealth = wealth + %s WHERE id = %s""", \
(wealth, account_id,))
self.connection.commit()
cursor.close()
return True