-
Notifications
You must be signed in to change notification settings - Fork 0
/
database.py
147 lines (128 loc) · 5.58 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# database.py
import pathlib
import sys
import os
from configparser import ConfigParser
import psycopg2
from psycopg2 import sql
PATH = pathlib.Path.cwd() / "data.json"
class Database:
QUERY_DICT = {
"CHECK_INBOX": """SELECT count(*)
FROM messages
INNER JOIN users ON messages.receiver_id = users.user_id
WHERE users.username = %s;""",
"READ_INBOX": """SELECT messages.message, sender.username as sender_username, messages.message_id
FROM messages
INNER JOIN users AS receiver ON messages.receiver_id = receiver.user_id
INNER JOIN users AS sender ON messages.sender = sender.user_id
WHERE receiver.username = %s
LIMIT 1;""",
"DELETE_MESSAGE": """DELETE FROM messages
WHERE message_id = %s
RETURNING message_id;""",
"ADD_MESSAGE_TO_INBOX": """INSERT INTO messages (receiver_id, sender, message)
SELECT receiver.user_id, sender.user_id, %s
FROM users AS receiver, users AS sender
WHERE receiver.username = %s AND sender.username = %s
RETURNING message_id;""",
"CHECK_IF_REGISTERED": """SELECT user_id
FROM users
WHERE username = %s;""",
"CHECK_PASSWORD": """SELECT user_id
FROM users
WHERE username = %s AND password = %s;""",
"REGISTER_USER": """INSERT INTO users(username, password, account_type)
VALUES(%s, %s, %s)
RETURNING user_id;""",
"CHECK_ACCOUNT_TYPE": """SELECT user_id
FROM users
WHERE username = %s AND account_type = %s;""",
"DELETE_USER": """DELETE FROM users
WHERE username = %s
RETURNING user_id;""",
}
def __init__(self):
self.init_file = os.path.dirname(os.path.abspath(__file__)) + "/database/database.ini"
self.config = {}
self.load_config("postgresql")
self.create_database("cs_db")
self.load_config("client_server_db")
self.create_tables()
def load_config(self, section):
parser = ConfigParser()
parser.read(self.init_file)
if parser.has_section(section):
params = parser.items(section)
for param in params:
self.config[param[0]] = param[1]
else:
raise Exception(f"Section {section} not found in the {self.init_file} file")
def create_database(self, db_name):
try:
connection = psycopg2.connect(**self.config)
connection.autocommit = True
cursor = connection.cursor()
query = sql.SQL("CREATE DATABASE {}").format(sql.Identifier(db_name))
cursor.execute(query)
cursor.close()
connection.close()
except psycopg2.errors.DuplicateDatabase as e:
pass
except (psycopg2.DatabaseError, Exception) as e:
print(e)
def drop_database(self, db_name):
try:
connection = psycopg2.connect(**self.config)
connection.autocommit = True
cursor = connection.cursor()
query = sql.SQL("DROP DATABASE {}").format(sql.Identifier(db_name))
cursor.execute(query)
cursor.close()
connection.close()
except psycopg2.errors.DuplicateDatabase as e:
pass
except (psycopg2.DatabaseError, Exception) as e:
print(e)
def create_tables(self):
try:
with psycopg2.connect(**self.config) as connection:
with connection.cursor() as cursor:
queries = (
"""
CREATE TABLE users (
user_id SERIAL PRIMARY KEY,
username VARCHAR(50) UNIQUE NOT NULL,
password VARCHAR(50) NOT NULL,
account_type VARCHAR(10) NOT NULL
)
""",
"""
CREATE TABLE messages (
message_id SERIAL PRIMARY KEY,
receiver_id INT NOT NULL REFERENCES users(user_id),
sender INT NOT NULL REFERENCES users(user_id),
message VARCHAR(255)
)
""")
for query in queries:
cursor.execute(query)
except psycopg2.errors.DuplicateTable:
pass
except (psycopg2.DatabaseError, Exception) as e:
print(e)
def execute_query(self, query, parameters):
try:
with psycopg2.connect(**self.config) as connection:
with connection.cursor() as cursor:
cursor.execute(query, parameters)
rows = cursor.fetchall()
if rows:
return rows
else:
return False
except (psycopg2.DatabaseError, Exception) as e:
print(e)
return False
if __name__ == '__main__':
pass