blob: d2998e90ea233bd595347d89d00ad37352eea914 (
plain)
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
|
import sqlite3
class UserRepository:
conn = None
def __init__(self, db_path):
self.__initialize()
def __initialize(self):
cur = self.__getConnection().cursor()
cur.execute("CREATE TABLE IF NOT EXISTS user(chat_id INT)")
cur.execute("CREATE UNIQUE INDEX IF NOT EXISTS chat_id_idx ON user(chat_id)")
def __getConnection(self):
return sqlite3.connect('db.sqlite')
def getUser(self, chat_id):
connection = self.__getConnection()
cur = connection.cursor()
query = "select * from user where chat_id = {chat_id}".format(chat_id = chat_id)
cur.execute(query)
return cur.fetchone()
def getAllUsers(self):
connection = self.__getConnection()
cur = connection.cursor()
query = "select * from user"
cur.execute(query)
return cur.fetchall()
def putUser(self, chat_id):
if not self.getUser(chat_id):
connection = self.__getConnection()
cur = connection.cursor()
query = "INSERT INTO user(chat_id) VALUES({chat_id})".format(chat_id = chat_id)
cur.execute(query)
connection.commit()
else:
print("User {} already exsits, skipping putUser()".format(chat_id))
|