-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsql.py
More file actions
99 lines (76 loc) · 2.54 KB
/
sql.py
File metadata and controls
99 lines (76 loc) · 2.54 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import sqlite3
import time
from data.my_config import SQL_PATH
def connection_wrapper(func):
def open_close_connection(*args, **kwargs):
con = sqlite3.connect(SQL_PATH)
cur = con.cursor()
func(cur, *args, **kwargs)
con.commit()
con.close()
return open_close_connection
@connection_wrapper
def create_table(cur):
try:
cur.execute('''CREATE TABLE proposals(
network text,
prop_id int,
title text,
voting_end_time int,
option bool,
next_notification int,
msg_id int
)''')
except sqlite3.OperationalError as er:
pass
@connection_wrapper
def save_to_db(cur, data):
cur.execute("insert into proposals values (?, ?, ?, ?, ?, ?, ?)", tuple(data))
def check_dublicates(network, prop_id):
con = sqlite3.connect(SQL_PATH)
cur = con.cursor()
cur.execute("SELECT * FROM proposals WHERE network=? AND prop_id=?", (network, prop_id))
fetched_data = cur.fetchall()
if not fetched_data:
return False
else:
return True
con.commit()
con.close()
def get_rows(network, prop_id):
con = sqlite3.connect(SQL_PATH)
cur = con.cursor()
cur.execute("SELECT * FROM proposals WHERE network=? AND prop_id=?", (network, prop_id))
fetched_data = cur.fetchall().copy()
con.commit()
con.close()
return fetched_data
@connection_wrapper
def drop_rows(cur):
current_time = int(time.time())
cur.execute("""DELETE from proposals where voting_end_time < ?""", (current_time,))
cur.execute("""DELETE from proposals where option = 1""")
@connection_wrapper
def get_outdated_props(cur):
current_time = int(time.time())
cur.execute("SELECT * FROM proposals WHERE voting_end_time < ? OR option = 1", (current_time,))
fetched_data = cur.fetchall().copy()
return fetched_data
def drop_row_by_msg_id(network, prop_id, msg_id):
con = sqlite3.connect(SQL_PATH)
cur = con.cursor()
cur.execute("""DELETE from proposals where msg_id = ? and network = ? and prop_id = ?""", (msg_id, network, prop_id))
con.commit()
con.close()
@connection_wrapper
def set_option(cur, network, prop_id, value):
cur.execute("UPDATE proposals set option = ? where network = ? and prop_id = ?",
(value, network, prop_id))
def get_all_rows():
con = sqlite3.connect(SQL_PATH)
cur = con.cursor()
cur.execute("SELECT * FROM proposals")
fetched_data = cur.fetchall()
con.commit()
con.close()
return fetched_data