-
Notifications
You must be signed in to change notification settings - Fork 1
/
database.py
197 lines (164 loc) · 8.05 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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
import mysql.connector
import random
import threading
import time
import log as log_lib
import conf
import json
#TODO:
#Executemany
def log(*message, logger:log_lib.Logger = None):
if not logger == None:
logger.log(*message)
else:
print(message)
def is_safe(*args, logger = None):
invalid_chars = ["\\", "\n", "\t", "\r", "\0", "%", "\b", ";", "\u259e"]
arguments = ""
for argument in args:
arguments += str(argument) if not argument == None else ""
log("arguments", arguments, logger=logger)
for i, char in enumerate(invalid_chars):
if char in arguments:
log(f"[ERROR] Invalid char {char} [{i}]", logger=logger)
return False
return True
class Database():
def __init__(self, logger:log_lib.Logger=None, real=True):
if not real:
return
time.sleep(5)
self.connect()
self.queue = []
self.return_response = []
self.logger = logger
self.file = open("sql.txt", "a")
thread = threading.Thread(target=self.proces_queue)
thread.start()
def connect(self):
conf.init()
self.connection = mysql.connector.connect(
host = conf.database("host"),
user = conf.database("root"),
password = conf.database("root_pwd"),
database = conf.database("database")
)
def stop(self):
queue_id = random.randint(1000000000, 9999999999)
self.queue.append(("s", "stop", queue_id))
def get_users_with_pos(self):
users = self.querry("SELECT user_name, grup, pos FROM users;")
users = [[user[0], user[1], json.loads(user[2])["pos"]] for user in users]
return users
def update_users_pos(self, users:list[list]):
for user in users:
self.execute(f"UPDATE users SET grup = {user[1]} WHERE user_name = '{user[0]}';")
def querry(self, querry:str):
queue_id = random.randint(1000000000, 9999999999)
self.queue.append(("q", querry, queue_id))
while True:
for response in self.return_response:
if response[0] == queue_id:
self.return_response.remove((queue_id, response[1]))
return response[1]
def execute(self, sql:str):
queue_id = random.randint(1000000000, 9999999999)
self.queue.append(("e", sql, queue_id))
while True:
for response in self.return_response:
if response[0] == queue_id:
self.return_response.remove((queue_id, response[1]))
return response[1]
def import_db(self, commands:list):
queue_id = random.randint(1000000000, 9999999999)
self.queue.append(("n", commands, queue_id))
while True:
for response in self.return_response:
if response[0] == queue_id:
self.return_response.remove((queue_id, response[1]))
return response[1]
def proces_queue(self):
log("[STARTED QUEUE PROCESOR]", logger=self.logger)
while True:
if len(self.queue) > 0:
if self.queue[0][0] == "q":
try:
cursor = self.connection.cursor()
cursor.execute(self.queue[0][1])
self.return_response.append((self.queue[0][2], cursor.fetchall()))
except mysql.connector.Error as e:
self.connect()
try:
cursor = self.connection.cursor()
cursor.execute(self.queue[0][1])
self.return_response.append((self.queue[0][2], cursor.fetchall()))
except mysql.connector.Error as e:
log("[ERROR]", e, logger = self.logger)
self.connect()
self.return_response.append((self.queue[0][2], "ERROR"))
elif self.queue[0][0] == "e":
try:
cursor = self.connection.cursor()
cursor.execute(self.queue[0][1])
self.connection.commit()
self.return_response.append((self.queue[0][2], None))
self.file.write(self.queue[0][1]+"\n")
except mysql.connector.Error as e:
self.connect()
try:
cursor = self.connection.cursor()
cursor.execute(self.queue[0][1])
self.connection.commit()
self.return_response.append((self.queue[0][2], None))
self.file.write(self.queue[0][1]+"\n")
except mysql.connector.Error as e:
log("[ERROR]", e, logger = self.logger)
self.connect()
self.return_response.append((self.queue[0][2], "ERROR"))
elif self.queue[0][0] == "n":
try:
cursor = self.connection.cursor()
for command in self.queue[0][1]:
try:
cursor.execute(command)
except mysql.connector.Error as e:
pass
self.connection.commit()
self.return_response.append((self.queue[0][2], "SUCCESS"))
except mysql.connector.Error as e:
try:
self.connect()
cursor = self.connection.cursor()
for command in self.queue[0][1]:
try:
cursor.execute(command)
except mysql.connector.Error as e:
pass
self.connection.commit()
self.return_response.append((self.queue[0][2], "SUCCESS"))
except mysql.connector.Error as e:
log("[ERROR]", e, logger = self.logger)
self.connect()
self.return_response.append((self.queue[0][2], "ERROR"))
elif self.queue[0][0] == "s":
self.queue.pop(0)
break
self.queue.pop(0)
time.sleep(0.1)
def drop(self):
cursor = self.connection.cursor()
cursor.execute("DROP TABLE IF EXISTS posts;")
cursor.execute("DROP TABLE IF EXISTS users;")
cursor.execute("DROP TABLE IF EXISTS grups;")
cursor.execute("DROP TABLE IF EXISTS ips;")
def create(self):
cursor = self.connection.cursor()
cursor.execute("CREATE TABLE IF NOT EXISTS grups(id INT NOT NULL PRIMARY KEY, pos JSON NOT NULL);")
cursor.execute("CREATE TABLE IF NOT EXISTS users(user_name VARCHAR(16) COLLATE ascii_general_ci NOT NULL UNIQUE PRIMARY KEY, public_key VARCHAR(392) COLLATE ascii_general_ci NOT NULL UNIQUE, key_file VARCHAR(1764) COLLATE ascii_general_ci NOT NULL UNIQUE, time_created INT NOT NULL, profile_picture VARCHAR(64) COLLATE ascii_general_ci NOT NULL, info VARCHAR(200), grup INT NOT NULL, pos JSON, FOREIGN KEY (grup) REFERENCES grups (id) ON UPDATE CASCADE);")
cursor.execute("CREATE TABLE IF NOT EXISTS posts(id VARCHAR(23) NOT NULL PRIMARY KEY, user_id VARCHAR(16) COLLATE ascii_general_ci NOT NULL, post VARCHAR(255) NOT NULL, background_color VARCHAR(1) NOT NULL, time_posted INT NOT NULL, signature VARCHAR(344), FOREIGN KEY (user_id) REFERENCES users (user_name));")
cursor.execute("CREATE TABLE IF NOT EXISTS ips(ip VARCHAR(21) NOT NULL PRIMARY KEY, time_connected INT NOT NULL);")
self.connection.commit()
"""if __name__ == "__main__":
db = Database()
db.create()
db.stop()"""