-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.py
46 lines (38 loc) · 1.31 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
# Simple abstraction over the DB as a blob store
import psycopg2
from psycopg2 import sql
import json
def connect_db(dsn):
return psycopg2.connect(dsn)
def get(conn, table, id):
cursor = conn.cursor()
cursor.execute("SELECT data FROM {} WHERE id = %s".format(table), (str(id),))
result = cursor.fetchone()
cursor.close()
if result:
return json.loads(result[0]) # Convert JSON string to Python dict
else:
return None
def update_or_create(conn, table, id, data):
try:
cursor = conn.cursor()
json_data = json.dumps(data) # Convert Python dict to JSON string
cursor.execute("INSERT INTO {} (id, data) VALUES (%s, %s) ON CONFLICT (id) DO UPDATE SET data = %s".format(table), (str(id), json_data, json_data))
conn.commit()
cursor.close()
except Exception as e:
print(f"Error: {str(e)}")
class Model:
def __init__(self, conn, table):
self.conn = conn
self.table = table
def get(self, id):
return get(self.conn, self.table, id)
def update_or_create(self, id, data):
return update_or_create(self.conn, self.table, id, data)
class Bot(Model):
def __init__(self, conn):
super().__init__(conn, 'bot')
class Chat(Model):
def __init__(self, conn):
super().__init__(conn, 'chat')