-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatabase_connector.py
76 lines (63 loc) · 2.25 KB
/
database_connector.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
74
75
76
from abc import ABC, abstractmethod
import psycopg2
from database import config
from secrets import token_urlsafe
class DatabaseConnector(ABC):
def connect(self):
connection = None
try:
params = config()
# connect to the PostgreSQL server
connection = psycopg2.connect(**params)
return connection
except (Exception, psycopg2.DatabaseError) as error:
print(error)
return None
def get_id_for_username(self, username):
exists = False
account = []
connection = self.connect()
cursor = connection.cursor()
cursor.execute("""SELECT id FROM account WHERE username = %(username)s""", {'username': username})
userid_pair = cursor.fetchone()
username_exists = userid_pair is not None
if username_exists:
userid = userid_pair[0]
return userid
return None
def get_username_for_id(self, id):
exists = False
account = []
connection = self.connect()
cursor = connection.cursor()
cursor.execute("""SELECT username FROM account WHERE id = %s""", (id,))
userid_pair = cursor.fetchone()
username_exists = userid_pair is not None
if username_exists:
userid = userid_pair[0]
return userid
return None
def get_display_name_for_id(self, id):
exists = False
account = []
connection = self.connect()
cursor = connection.cursor()
cursor.execute("""SELECT display_name FROM account WHERE id = %s""", (id,))
userid_pair = cursor.fetchone()
display_name_exists = userid_pair is not None
if display_name_exists:
userid = userid_pair[0]
return userid
return None
def get_wealth_for_id(self, id):
exists = False
account = []
connection = self.connect()
cursor = connection.cursor()
cursor.execute("""SELECT wealth FROM account WHERE id = %s""", (id,))
userid_pair = cursor.fetchone()
display_name_exists = userid_pair is not None
if display_name_exists:
weath = userid_pair[0]
return weath
return None