import sqlite3 import threading import os import time lock = threading.Lock() class DB: def __init__(self, database): self.connection = sqlite3.connect(database, check_same_thread=False) self.cursor = self.connection.cursor() self.connection.isolation_level = None def add_user(self, user_id, username): with self.connection: try: lock.acquire(True) self.cursor.execute('SELECT user_id FROM users WHERE user_id=?', (user_id,)) result = self.cursor.fetchone() if result == None: self.cursor.execute('INSERT INTO users (user_id, username, status, pay_count) VALUES (?, ?, ?, ?)', (user_id, username, 'reg', 0)) return else: return except: self.connection.rollback() finally: lock.release() def get_all_users(self): with self.connection: try: lock.acquire(True) self.cursor.execute('SELECT id, user_id FROM users ORDER BY id ASC') result = self.cursor.fetchall() return result except: self.connection.rollback() finally: lock.release() def get_all_goods(self): with self.connection: try: lock.acquire(True) self.cursor.execute('SELECT id FROM goods') result = self.cursor.fetchall() return result except: self.connection.rollback() finally: lock.release() def get_all_instances(self): with self.connection: try: lock.acquire(True) self.cursor.execute('SELECT id FROM goodsInstances') result = self.cursor.fetchall() return result except: self.connection.rollback() finally: lock.release() def get_good_instances(self, goodId): with self.connection: try: lock.acquire(True) self.cursor.execute('SELECT id FROM goodsInstances WHERE goodId=? AND status!="sold"', (goodId,)) result = self.cursor.fetchall() return result except: self.connection.rollback() finally: lock.release() def check_userstat(self, user_id): with self.connection: try: lock.acquire(True) self.cursor.execute('SELECT status FROM users WHERE user_id=?', (user_id,)) result = self.cursor.fetchone() if result == None: return None else: return result[0] except: self.connection.rollback() finally: lock.release() def get_rules(self): with self.connection: try: lock.acquire(True) self.cursor.execute('SELECT text FROM rules') result = self.cursor.fetchone() return result[0] except: self.connection.rollback() finally: lock.release() def change_status(self, user_id, status): with self.connection: try: lock.acquire(True) self.cursor.execute('UPDATE users SET status=? WHERE user_id=?', (status, user_id)) return except: self.connection.rollback() finally: lock.release() def check_ban(self, user_id): with self.connection: try: lock.acquire(True) self.cursor.execute('SELECT status FROM users WHERE user_id=?', (user_id,)) result = self.cursor.fetchone() if result[0] == 'ban': return False else: return True except: self.connection.rollback() finally: lock.release() def get_all_faq(self): with self.connection: try: lock.acquire(True) self.cursor.execute('SELECT id, name FROM faq') result = self.cursor.fetchall() return result except: self.connection.rollback() finally: lock.release() def get_usernamerev(self, user_id): with self.connection: try: lock.acquire(True) self.cursor.execute('SELECT username FROM users WHERE user_id=?', (user_id,)) result = self.cursor.fetchone() return result[0] except: self.connection.rollback() finally: lock.release() def get_user_pay_count(self, user_id): with self.connection: try: lock.acquire(True) self.cursor.execute('SELECT pay_count FROM users WHERE user_id=?', (user_id,)) result = self.cursor.fetchone() if result[0] == None: return 0 return result[0] except: self.connection.rollback() finally: lock.release() def get_user_pay_sum(self, user_id): with self.connection: try: lock.acquire(True) self.cursor.execute('SELECT SUM(price) FROM orders WHERE user_id=?', (user_id,)) result = self.cursor.fetchone() return result[0] except: self.connection.rollback() finally: lock.release() def get_user_status(self, user_id): with self.connection: try: lock.acquire(True) self.cursor.execute('SELECT status FROM users WHERE user_id=?', (user_id,)) result = self.cursor.fetchone() return result[0] except: self.connection.rollback() finally: lock.release() def get_all_cat(self): with self.connection: try: lock.acquire(True) self.cursor.execute('SELECT id, name FROM categories') result = self.cursor.fetchall() return result except: self.connection.rollback() finally: lock.release() def get_subcat(self, catid): with self.connection: try: lock.acquire(True) self.cursor.execute('SELECT id, name FROM subcategories WHERE categoryid=?', (catid,)) result = self.cursor.fetchall() return result except: self.connection.rollback() finally: lock.release() def get_goods_user(self, subcatid): with self.connection: try: lock.acquire(True) self.cursor.execute('SELECT id, name, description, price, photo FROM goods WHERE subcategoryid=?', (subcatid,)) result = self.cursor.fetchall() return result except: self.connection.rollback() finally: lock.release() def get_cat_id_by_subcat_id(self, subcatId): with self.connection: try: lock.acquire(True) self.cursor.execute('SELECT categoryid FROM subcategories WHERE id=?', (subcatId,)) result = self.cursor.fetchone() return result[0] except: self.connection.rollback() finally: lock.release() def get_goodinfo(self, goodid): with self.connection: try: lock.acquire(True) self.cursor.execute(f'SELECT name, description, price, photo FROM goods WHERE id=?', (goodid,)) result = self.cursor.fetchone() return result except: self.connection.rollback() finally: lock.release() def get_promos(self): with self.connection: try: lock.acquire(True) self.cursor.execute('SELECT id, name, percent, activations, actLimit FROM promo') result = self.cursor.fetchall() return result except: self.connection.rollback() finally: lock.release() def add_promo(self, name, percent, actLimit): with self.connection: try: lock.acquire(True) self.cursor.execute('INSERT INTO promo (name, percent, actLimit, activations) VALUES (?, ?, ?, ?)', (name, percent, actLimit, "0")) return except: self.connection.rollback() finally: lock.release() def del_promo(self, promoName=None, promoId=None): with self.connection: try: lock.acquire(True) if promoName != None: self.cursor.execute('DELETE FROM promo WHERE name=?', (promoName,)) else: self.cursor.execute('DELETE FROM promo WHERE id=?', (promoId,)) return except: self.connection.rollback() finally: lock.release() def get_promo_info(self, promo): with self.connection: try: lock.acquire(True) self.cursor.execute('SELECT id, percent, actLimit, activations FROM promo WHERE name=?', (str(promo),)) result = self.cursor.fetchone() return result except: self.connection.rollback() finally: lock.release() def get_promo_info_by_id(self, promoId): with self.connection: try: lock.acquire(True) self.cursor.execute('SELECT name, percent, activations, actLimit FROM promo WHERE id=?', (promoId,)) result = self.cursor.fetchone() return result except: self.connection.rollback() finally: lock.release() def get_promo_from_order(self, orderId): with self.connection: try: lock.acquire(True) self.cursor.execute('SELECT promo FROM orders WHERE id=?', (orderId,)) result = self.cursor.fetchone() return result[0] except: self.connection.rollback() finally: lock.release() def use_promo(self, promo): with self.connection: try: lock.acquire(True) self.cursor.execute('SELECT actLimit, activations FROM promo WHERE name=?', (promo,)) result = self.cursor.fetchone() if int(result[0]) <= int(result[1]) + 1: self.cursor.execute('DELETE FROM promo WHERE name=?', (promo,)) else: self.cursor.execute('UPDATE promo SET activations=activations+1 WHERE name=?', (promo,)) return except: self.connection.rollback() finally: lock.release() def add_order(self, user_id, goodId, promo, price): with self.connection: try: lock.acquire(True) self.cursor.execute('INSERT INTO orders (user_id, status, goodId, promo, price, time_of_creating, paymentLink) VALUES (?, ?, ?, ?, ?, ?, ?)', (user_id, 'created', goodId, promo, price, time.time(), "")) self.cursor.execute('SELECT id FROM orders WHERE user_id=? ORDER BY id DESC LIMIT 1', (user_id,)) result = self.cursor.fetchone() return result[0] except: self.connection.rollback() finally: lock.release() def get_orders(self, timeStamp): with self.connection: try: lock.acquire(True) if timeStamp == "all": timeS = int(time.time()) elif timeStamp == "today": timeS = 86400 elif timeStamp == "week": timeS = 604800 elif timeStamp == "month": timeS = 2678400 self.cursor.execute('SELECT id FROM orders WHERE time_of_creating>? AND status=?', ((int(time.time()) - timeS), "paid")) result1 = self.cursor.fetchall() self.cursor.execute('SELECT SUM(price) FROM orders WHERE time_of_creating>? AND status=?', ((int(time.time()) - timeS), "paid")) result2 = self.cursor.fetchall() return [result1, result2[0][0]] except: self.connection.rollback() finally: lock.release() def remove_old_orders(self): with self.connection: try: lock.acquire(True) self.cursor.execute('DELETE FROM orders WHERE time_of_creating