-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqueryutil.py
78 lines (68 loc) · 2.81 KB
/
queryutil.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
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
from database import connector
from datetime import datetime
from utils import utils, logger
import sqlite3
class execute_query:
def __init__(self):
self.log = logger().log_error
self.shortlinkURL = "https://shortlinks.mywebsite.com"
def get_url(self, identifier):
with connector(permission='ro') as conn:
cursor = conn.cursor()
cursor.execute("SELECT full_url FROM url_list WHERE shortlink=?", (identifier,))
return cursor.fetchone()
def get_all_records(self):
with connector() as conn:
for row in conn.cursor().execute("SELECT * FROM url_list"):
print(row)
def store_record(self, full_url, *, expiration_date=None):
with connector() as conn:
shortlink = utils().generate_string()
conn.cursor().execute("""
INSERT INTO url_list VALUES (NULL, ?, ?, ?, ?)
""", [(full_url), (shortlink), (datetime.now().strftime("%Y-%m-%d %H:%M:%S")), (expiration_date)])
conn.commit()
return f'{self.shortlinkURL}/{shortlink}'
def add_api_key(self, owner, permission='ALL'):
with connector() as conn:
apikey = utils().generate_string(24)
cursor = conn.cursor()
cursor.execute("""
INSERT INTO api_keys VALUES (NULL, ?, ?, ?)
""", [(owner), (apikey), (permission)])
conn.commit()
return apikey
def check_api_key(self, key):
with connector() as conn:
cursor = conn.cursor()
cursor.execute("SELECT key FROM api_keys WHERE key=?", (key,))
''.join(cursor.fetchone()) # Trying to join if execute above returns something
return True
def get_table_struct(self):
with connector() as conn:
cursor = conn.cursor()
cursor.execute("SELECT sql FROM sqlite_master")
return ''.join(cursor.fetchone())
def create_url_table(self):
# Creating url table
with connector() as conn:
cursor = conn.cursor()
cursor.execute("""
CREATE TABLE url_list
(id INTEGER PRIMARY KEY AUTOINCREMENT, full_url TEXT, shortlink TEXT UNIQUE, creation_date TEXT, expiration_date TEXT)
""")
conn.commit()
def create_apikey_table(self):
with connector() as conn:
# Creating API key table
cursor = conn.cursor()
cursor.execute("""
CREATE TABLE api_keys
(id INTEGER PRIMARY KEY AUTOINCREMENT, owner TEXT UNIQUE, key TEXT UNIQUE, permissions TEXT)
""")
conn.commit()
def create_default_tables(self):
print("\n\nI am being called!\n\n")
connector().create_db()
self.create_url_table()
self.create_apikey_table()