-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathdb.py
378 lines (330 loc) · 16.1 KB
/
db.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
import asyncpg
from config import *
class DB:
def __init__(self):
self.pool = None
async def connect(self):
self.pool = await asyncpg.create_pool(
user=POSTGRES_USER,
password=POSTGRES_PASSWORD,
host=POSTGRES_HOST,
port=POSTGRES_PORT,
database=POSTGRES_DB
)
# Init table creation ==============================================================================================
async def create_tables(self):
async with self.pool.acquire() as connection:
async with connection.transaction():
await connection.execute('''
CREATE TABLE IF NOT EXISTS users (
id SERIAL PRIMARY KEY,
name VARCHAR,
age VARCHAR,
sex VARCHAR,
tg VARCHAR,
connect_with VARCHAR,
last_connect VARCHAR,
chats INT DEFAULT 0,
messages INT DEFAULT 0,
likes INT DEFAULT 0,
dislikes INT DEFAULT 0,
refs INT DEFAULT 0,
points INT DEFAULT 0,
vip_ends VARCHAR(30),
notifications INT DEFAULT 1
);''')
await connection.execute('''
CREATE TABLE IF NOT EXISTS queue (
tg VARCHAR,
sex VARCHAR,
op_sex VARCHAR
);''')
await connection.execute('''
CREATE TABLE IF NOT EXISTS messages (
tg VARCHAR,
username VARCHAR,
message VARCHAR,
stamp VARCHAR
);''')
# SELECT ===========================================================================================================
async def user_exists(self, tg: str) -> bool:
async with self.pool.acquire() as connection:
async with connection.transaction():
result = await connection.fetchrow(
'SELECT tg FROM users WHERE tg = $1', tg
)
return False if result is None else True
async def queue_exists(self, tg: str) -> bool:
async with self.pool.acquire() as connection:
async with connection.transaction():
result = await connection.fetchrow(
'SELECT * FROM queue WHERE tg = $1', tg
)
return False if result is None else True
async def count_users(self) -> str:
async with self.pool.acquire() as connection:
async with connection.transaction():
result = await connection.fetchrow(
'SELECT COUNT(tg) FROM users'
)
return dict(result)['count']
async def find_chat(self, tg: str):
async with self.pool.acquire() as connection:
async with connection.transaction():
result = await connection.fetchrow(
'SELECT tg FROM queue WHERE tg <> $1', tg
)
return dict(result)['tg'] if result is not None else None
async def find_chat_vip(self, tg: str, sex: str, op_sex: str):
async with self.pool.acquire() as connection:
async with connection.transaction():
result = await connection.fetchrow(
'SELECT tg FROM queue WHERE tg <> $1 AND (op_sex = $2 OR op_sex IS null) AND sex = $3', tg, sex, op_sex
)
return dict(result)['tg'] if result is not None else None
async def select_name(self, tg: str):
async with self.pool.acquire() as connection:
async with connection.transaction():
result = await connection.fetchrow(
'SELECT name FROM users WHERE tg = $1', tg
)
return dict(result)['name']
async def select_age(self, tg: str):
async with self.pool.acquire() as connection:
async with connection.transaction():
result = await connection.fetchrow(
'SELECT age FROM users WHERE tg = $1',
tg
)
return dict(result)['age']
async def select_sex(self, tg: str):
async with self.pool.acquire() as connection:
async with connection.transaction():
result = await connection.fetchrow(
'SELECT sex FROM users WHERE tg = $1', tg
)
return dict(result)['sex']
async def select_connect_with(self, tg: str) -> str:
async with self.pool.acquire() as connection:
async with connection.transaction():
result = await connection.fetchrow(
'SELECT connect_with FROM users WHERE tg = $1', tg
)
return dict(result)['connect_with']
async def select_connect_with_self(self, tg: str):
async with self.pool.acquire() as connection:
async with connection.transaction():
result = await connection.fetchrow(
'SELECT tg FROM users WHERE connect_with = $1', tg
)
return dict(result)['tg']
async def select_last_connect(self, tg: str):
async with self.pool.acquire() as connection:
async with connection.transaction():
result = await connection.fetchrow(
'SELECT last_connect FROM users WHERE tg = $1', tg
)
return dict(result)['last_connect']
async def select_chats(self, tg: str):
async with self.pool.acquire() as connection:
async with connection.transaction():
result = await connection.fetchrow(
'SELECT chats FROM users WHERE tg = $1', tg
)
return dict(result)['chats']
async def select_messages(self, tg: str):
async with self.pool.acquire() as connection:
async with connection.transaction():
result = await connection.fetchrow(
'SELECT messages FROM users WHERE tg = $1', tg
)
return dict(result)['messages']
async def select_likes(self, tg: str):
async with self.pool.acquire() as connection:
async with connection.transaction():
result = await connection.fetchrow(
'SELECT likes FROM users WHERE tg = $1', tg
)
return dict(result)['likes']
async def select_dislikes(self, tg: str):
async with self.pool.acquire() as connection:
async with connection.transaction():
result = await connection.fetchrow(
'SELECT dislikes FROM users WHERE tg = $1', tg
)
return dict(result)['dislikes']
async def select_vip_ends(self, tg: str):
async with self.pool.acquire() as connection:
async with connection.transaction():
result = await connection.fetchrow(
'SELECT vip_ends FROM users WHERE tg = $1', tg
)
return dict(result)['vip_ends']
async def select_refs(self, tg: str):
async with self.pool.acquire() as connection:
async with connection.transaction():
result = await connection.fetchrow(
'SELECT refs FROM users WHERE tg = $1', tg
)
return dict(result)['refs']
async def select_points(self, tg: str):
async with self.pool.acquire() as connection:
async with connection.transaction():
result = await connection.fetchrow(
'SELECT points FROM users WHERE tg = $1', tg
)
return dict(result)['points']
async def select_notifications(self, tg: str):
async with self.pool.acquire() as connection:
async with connection.transaction():
result = await connection.fetchrow(
'SELECT notifications FROM users WHERE tg = $1', tg
)
return dict(result)['notifications']
async def top_messages(self):
async with self.pool.acquire() as connection:
async with connection.transaction():
result = await connection.fetch(
'SELECT name, messages FROM users ORDER BY messages DESC LIMIT 5'
)
top_dict = {}
for number, value in enumerate(list(result)):
top_dict[number + 1] = {'name': dict(value)['name'], 'count': dict(value)['messages']}
return top_dict
async def top_refs(self):
async with self.pool.acquire() as connection:
async with connection.transaction():
result = await connection.fetch(
'SELECT name, refs FROM users ORDER BY messages DESC LIMIT 5'
)
top_dict = {}
for number, value in enumerate(list(result)):
top_dict[number + 1] = {'name': dict(value)['name'], 'count': dict(value)['refs']}
return top_dict
async def top_likes(self):
async with self.pool.acquire() as connection:
async with connection.transaction():
result = await connection.fetch(
'SELECT name, likes FROM users ORDER BY messages DESC LIMIT 5'
)
top_dict = {}
for number, value in enumerate(list(result)):
top_dict[number + 1] = {'name': dict(value)['name'], 'count': dict(value)['likes']}
return top_dict
async def count_users(self) -> int:
async with self.pool.acquire() as connection:
async with connection.transaction():
result = await connection.fetchval('SELECT COUNT(tg) FROM users')
return result
# UPDATE ===========================================================================================================
async def update_name(self, tg: str, name: str):
async with self.pool.acquire() as connection:
async with connection.transaction():
await connection.execute(
'UPDATE users SET name = $1 WHERE tg = $2', name, tg
)
async def update_age(self, tg: str, age: str):
async with self.pool.acquire() as connection:
async with connection.transaction():
await connection.execute(
'UPDATE users SET age = $1 WHERE tg = $2', age, tg
)
async def update_sex(self, tg: str, sex: str):
async with self.pool.acquire() as connection:
async with connection.transaction():
await connection.execute(
'UPDATE users SET sex = $1 WHERE tg = $2', sex, tg
)
async def update_connect_with(self, tg: str, connect_with: str | None):
async with self.pool.acquire() as connection:
async with connection.transaction():
if connect_with != tg:
await connection.execute(
'UPDATE users SET connect_with = $1 WHERE tg = $2', connect_with, tg
)
async def update_last_connect(self, tg: str):
async with self.pool.acquire() as connection:
async with connection.transaction():
await connection.execute(
'UPDATE users SET last_connect = connect_with WHERE tg = $1', tg
)
async def update_chats(self, tg: str):
async with self.pool.acquire() as connection:
async with connection.transaction():
await connection.execute(
'UPDATE users SET chats = chats + 1 WHERE tg = $1', tg
)
async def update_messages(self, tg: str):
async with self.pool.acquire() as connection:
async with connection.transaction():
await connection.execute(
'UPDATE users SET messages = messages + 1 WHERE tg = $1', tg
)
async def update_likes(self, tg: str):
async with self.pool.acquire() as connection:
async with connection.transaction():
await connection.execute(
'UPDATE users SET likes = likes + 1 WHERE tg = $1', tg
)
async def update_dislikes(self, tg: str):
async with self.pool.acquire() as connection:
async with connection.transaction():
await connection.execute(
'UPDATE users SET dislikes = dislikes + 1 WHERE tg = $1', tg
)
async def update_refs(self, tg: str):
async with self.pool.acquire() as connection:
async with connection.transaction():
await connection.execute(
'UPDATE users SET refs = refs + 1 WHERE tg = $1', tg
)
async def update_points(self, tg: str, value: int):
async with self.pool.acquire() as connection:
async with connection.transaction():
await connection.execute(
'UPDATE users SET points = points + $1 WHERE tg = $2', value, tg
)
async def update_notifications(self, tg: str, value: int):
async with self.pool.acquire() as connection:
async with connection.transaction():
await connection.execute(
'UPDATE users SET notifications = $1 WHERE tg = $2', value, tg
)
async def update_vip_ends(self, tg: str, time: str):
async with self.pool.acquire() as connection:
async with connection.transaction():
await connection.execute(
'UPDATE users SET vip_ends = $1 WHERE tg = $2', time, tg
)
# INSERT ===========================================================================================================
async def insert_in_users(self, tg: str, name: str, age: str, sex: str, vip_ends: str):
async with self.pool.acquire() as connection:
async with connection.transaction():
await connection.execute(
'INSERT INTO users (tg, name, age, sex, vip_ends) VALUES($1, $2, $3, $4, $5)', tg, name, age, sex, vip_ends
)
async def insert_in_queue(self, tg: str, sex: str):
async with self.pool.acquire() as connection:
async with connection.transaction():
await connection.execute(
'INSERT INTO queue (tg, sex, op_sex) VALUES($1, $2, $3)', tg, sex, None
)
async def insert_in_queue_vip(self, tg: str, sex: str, op_sex: str):
async with self.pool.acquire() as connection:
async with connection.transaction():
await connection.execute(
'INSERT INTO queue (tg, sex, op_sex) VALUES($1, $2, $3)', tg, sex, op_sex
)
async def insert_in_messages(self, tg: str, username: str, message: str, stamp: str):
async with self.pool.acquire() as connection:
async with connection.transaction():
await connection.execute(
'INSERT INTO messages (tg, username, message, stamp) VALUES ($1, $2, $3, $4)', tg, username, message, stamp
)
# DELETE ===========================================================================================================
async def delete_from_queue(self, tg: str):
async with self.pool.acquire() as connection:
async with connection.transaction():
await connection.execute(
'DELETE FROM queue WHERE tg = $1', tg
)