-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdb_operations.py
More file actions
58 lines (52 loc) · 2.27 KB
/
db_operations.py
File metadata and controls
58 lines (52 loc) · 2.27 KB
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
# db_operations.py
import psycopg2
from psycopg2.extras import RealDictCursor
from datetime import datetime
class DatabaseManager:
def __init__(self, db_url):
self.db_url = db_url
self.create_tables()
def create_tables(self):
with psycopg2.connect(self.db_url) as conn:
with conn.cursor() as cur:
cur.execute("""
CREATE TABLE IF NOT EXISTS users (
id SERIAL PRIMARY KEY,
username VARCHAR(255) UNIQUE NOT NULL
)
""")
cur.execute("""
CREATE TABLE IF NOT EXISTS chat_history (
id SERIAL PRIMARY KEY,
user_id INTEGER REFERENCES users(id),
role VARCHAR(50) NOT NULL,
content TEXT NOT NULL,
timestamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP
)
""")
conn.commit()
def get_or_create_user(self, username):
with psycopg2.connect(self.db_url) as conn:
with conn.cursor(cursor_factory=RealDictCursor) as cur:
cur.execute("SELECT id FROM users WHERE username = %s", (username,))
user = cur.fetchone()
if user:
return user['id']
cur.execute("INSERT INTO users (username) VALUES (%s) RETURNING id", (username,))
return cur.fetchone()['id']
def add_message(self, user_id, role, content):
with psycopg2.connect(self.db_url) as conn:
with conn.cursor() as cur:
cur.execute(
"INSERT INTO chat_history (user_id, role, content) VALUES (%s, %s, %s)",
(user_id, role, content)
)
conn.commit()
def get_chat_history(self, user_id, limit=50):
with psycopg2.connect(self.db_url) as conn:
with conn.cursor(cursor_factory=RealDictCursor) as cur:
cur.execute(
"SELECT role, content FROM chat_history WHERE user_id = %s ORDER BY timestamp DESC LIMIT %s",
(user_id, limit)
)
return cur.fetchall()[::-1] # Reverse to get chronological order