-
Notifications
You must be signed in to change notification settings - Fork 0
/
threads_sql.py
43 lines (32 loc) · 1.51 KB
/
threads_sql.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
from db import db
import users
def sql_edit_thread(thread_id, name):
sql = "UPDATE threads SET name=:name WHERE id=:thread_id"
db.session.execute(sql, {"name": name, "thread_id": thread_id})
db.session.commit()
def sql_get_section_name(section_id):
sql = "SELECT name FROM sections WHERE id=:section_id"
result = db.session.execute(sql, {"section_id": section_id})
return result.fetchone()[0]
def sql_delete_thread(thread_id):
sql = "DELETE FROM threads WHERE id=:thread_id RETURNING section_id"
result = db.session.execute(sql, {"thread_id": thread_id})
db.session.commit()
return result.fetchone()
def sql_new_thread(section_id, creator_id, name):
sql = "INSERT INTO threads (section_id, creator_id, name) " \
"VALUES (:section_id, :creator_id, :name) RETURNING id"
result = db.session.execute(sql, {"section_id": section_id, "creator_id": creator_id, "name": name})
db.session.commit()
return result.fetchone()
def sql_get_threads(section_id):
user_id = users.user_id()
user_role = users.user_role()
sql = "SELECT T.id, T.name, " \
"(SELECT COUNT(M.id) FROM messages M WHERE T.id=M.thread_id), U.username, " \
"(T.creator_id=:user_id OR :user_role>0) " \
"FROM users U, threads T " \
"WHERE T.section_id=:section_id AND U.id=T.creator_id " \
"ORDER BY T.id DESC"
result = db.session.execute(sql, {"section_id": section_id, "user_id": user_id, "user_role": user_role})
return result.fetchall()