This repository has been archived by the owner on Mar 10, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsql.py
62 lines (49 loc) · 1.86 KB
/
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
"""SQL queries for the forwarder."""
import secrets
def init(dbconn):
"""Initialise DB (create table)."""
cursor = dbconn.cursor()
cursor.execute('CREATE TABLE url_entries '
'(identifier TEXT PRIMARY KEY, forward_to TEXT, update_token TEXT)')
def delete_url(dbconn, identifier: str):
"""Delete an entry."""
cursor = dbconn.cursor()
cursor.execute('DELETE FROM url_entries WHERE identifier = ?', (identifier,))
dbconn.commit()
def update_url(dbconn, identifier: str, new_url: str):
"""Update an entry."""
cursor = dbconn.cursor()
cursor.execute('UPDATE url_entries SET forward_to = ? WHERE identifier = ?',
(new_url, identifier))
dbconn.commit()
def get_entry(dbconn, identifier: str) -> str:
"""Retrieve an entry as dict."""
cursor = dbconn.cursor()
cursor.execute('SELECT * FROM url_entries WHERE identifier=?', (identifier,))
raw = cursor.fetchone()
if raw:
data = dict(zip(('identifier', 'forward_to', 'token'), raw))
else:
data = None
return data
def get_url(dbconn, identifier: str) -> str:
"""Get forward URL for an entry."""
cursor = dbconn.cursor()
cursor.execute('SELECT forward_to FROM url_entries WHERE identifier=?', (identifier,))
data = cursor.fetchone()
if data:
data = data[0]
return data
def add_url(dbconn, identifier: str, new_url: str) -> str:
"""Add an entry."""
token = secrets.token_urlsafe(32)
cursor = dbconn.cursor()
cursor.execute('INSERT INTO url_entries VALUES (?, ?, ?)', (identifier, new_url, token))
dbconn.commit()
return token
def get_all(dbconn):
"""Get a list with all (identifier, forward_url) pairs."""
cursor = dbconn.cursor()
cursor.execute('SELECT identifier, forward_to FROM url_entries ORDER BY identifier')
data = cursor.fetchall()
return data