-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatabase.py
178 lines (142 loc) · 7.19 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
import sqlite3
import os
class Database:
def __init__(self, db_file):
self.connection = sqlite3.connect(db_file)
self.cursor = self.connection.cursor()
def add_user(self, user_id, username):
with self.connection:
res = self.cursor.execute("""INSERT into users (user_id, username) VALUES (?, ?)""", (user_id, username,))
self.connection.commit()
return res
def user_exists(self, user_id):
with self.connection:
res = self.cursor.execute("""SELECT * from users WHERE user_id = ?""", (user_id,)).fetchall()
return bool(len(res))
def add_balance(self, user_id, amount):
with self.connection:
res = self.cursor.execute("""UPDATE users SET balance = balance + ? WHERE user_id = ?""", (amount, user_id,))
self.connection.commit()
return res
def minus_balance(self, user_id, amount):
with self.connection:
res = self.cursor.execute("""UPDATE users SET balance = balance - ? WHERE user_id = ?""", (amount, user_id,))
self.connection.commit()
return res
def stats(self, user_id):
with self.connection:
return self.cursor.execute("""SELECT * from users WHERE user_id = ?""", (user_id,)).fetchone()
def add_order(self, user_id, username, link):
with self.connection:
res = self.cursor.execute("""INSERT into orders (user_id, username, link) VALUES (?, ?, ?)""", (user_id, username, link,))
self.connection.commit()
return res
def orders(self):
with self.connection:
return self.cursor.execute("""SELECT * from orders""").fetchall()
def add_worker(self, user_id):
with self.connection:
res = self.cursor.execute("""INSERT into workers (user_id) VALUES (?)""", (user_id,))
self.connection.commit()
return res
def up_balance_worker(self, user_id, amount):
with self.connection:
res = self.cursor.execute("""UPDATE workers SET balance = balance + ? WHERE user_id = ?""", (amount, user_id,))
self.connection.commit()
return res
def minus_balance_worker(self, user_id, amount):
with self.connection:
res = self.cursor.execute("""UPDATE workers SET balance = balance - ? WHERE user_id = ?""", (amount, user_id,))
self.connection.commit()
return res
def worker_exists(self, user_id):
with self.connection:
res = self.cursor.execute("""SELECT * from workers WHERE user_id = ?""", (user_id,)).fetchall()
return bool(len(res))
def worker_stats(self, user_id):
with self.connection:
return self.cursor.execute("""SELECT * from workers WHERE user_id = ?""", (user_id,)).fetchone()
def get_order_info(self, id_):
with self.connection:
return self.cursor.execute("""SELECT * from orders WHERE order_id = ?""", (id_,)).fetchone()
def get_accepted_order(self, id_):
with self.connection:
return self.cursor.execute("""SELECT * from accepted_orders WHERE order_id = ?""", (id_,)).fetchone()
def set_order_to_worker(self, worker_id, user_id, id_, link):
with self.connection:
res = self.cursor.execute("""INSERT into accepted_orders (worker_id, user_id, order_id, link) VALUES (?, ?, ?, ?)""", (worker_id, user_id, id_, link,))
self.connection.commit()
return res
def delete_order(self, id_):
with self.connection:
res = self.cursor.execute("""DELETE from orders WHERE order_id = ?""", (id_,))
self.connection.commit()
return res
def delete_accepted_order(self, id_):
with self.connection:
res = self.cursor.execute("""DELETE from accepted_orders WHERE order_id = ?""", (id_,))
self.connection.commit()
return res
def worker_orders(self, worker_id):
with self.connection:
return self.cursor.execute("""SELECT * from accepted_orders WHERE worker_id = ?""", (worker_id,)).fetchall()
def add_worker_balance(self, user_id, amount):
with self.connection:
res = self.cursor.execute("""UPDATE workers SET balance = balance + ? WHERE user_id = ?""", (amount, user_id,))
self.connection.commit()
return res
def workers(self):
with self.connection:
return self.cursor.execute("""SELECT * FROM workers""").fetchall()
def all_users(self):
with self.connection:
return self.cursor.execute("""SELECT count (*) from users""").fetchall()
def search_user(self, username):
with self.connection:
return self.cursor.execute("""SELECT * from users WHERE username = ?""", (username,)).fetchone()
def block_user(self, user_id):
with self.connection:
res = self.cursor.execute("""UPDATE users SET is_banned = 1 WHERE user_id = ?""", (user_id,))
self.connection.commit()
return res
def set_all_money(self, user_id, amount):
with self.connection:
res = self.cursor.execute("""UPDATE users SET all_money = all_money + ? WHERE user_id = ? """, (amount, user_id,))
self.connection.commit()
return res
def set_all_orders(self, user_id):
with self.connection:
res = self.cursor.execute("""UPDATE users SET orders = orders + 1 WHERE user_id = ?""", (user_id,))
self.connection.commit()
return res
def del_worker(self, user_id):
with self.connection:
res = self.cursor.execute("""DELETE from workers WHERE user_id = ?""", (user_id,))
self.connection.commit()
return res
def get_users(self):
with self.connection:
return self.cursor.execute("""SELECT user_id, active from users""").fetchall()
def set_active(self, user_id, active):
with self.connection:
res = self.cursor.execute("""UPDATE users SET active = ? WHERE user_id = ?""", (active, user_id,))
self.connection.commit()
return res
def add_promo(self, promo, amount, activations):
with self.connection:
res = self.cursor.execute("""INSERT into promocodes (promo, amount, activations) VALUES (?, ?, ?)""", (promo, amount, activations))
self.connection.commit()
return res
def minus_activ(self, promo):
with self.connection:
res = self.cursor.execute("""UPDATE promocodes SET activations = activations - 1 WHERE promo = ?""", (promo,))
self.connection.commit()
return res
def delete_promo(self, promo):
with self.connection:
res = self.cursor.execute("""DELETE from promocodes WHERE promo = ?""", (promo,))
self.connection.commit()
return res
def get_promo(self, name):
with self.connection:
return self.cursor.execute("""SELECT * from promocodes WHERE promo = ?""", (name,)).fetchone()