-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.py
70 lines (56 loc) · 1.63 KB
/
db.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
import os
import psycopg2
DATABASE_URL = os.environ['DATABASE_URL']
conn = psycopg2.connect(DATABASE_URL, sslmode='require')
cursor = conn.cursor()
def execute_non_query(command: str):
cursor.execute(command)
conn.commit()
def execute_query(command: str):
cursor.execute(command)
return cursor.fetchall()
def create_table():
create_command = """
CREATE TABLE urls (
url VARCHAR(2083) NOT NULL,
shrinked_hash VARCHAR(20) NOT NULL UNIQUE,
PRIMARY KEY (url)
)
"""
execute_non_query(create_command)
def get_shrinked_hash(url: str):
query = f"""
SELECT shrinked_hash FROM urls WHERE url = '{url}'
"""
result = execute_query(query)
if len(result) == 0:
return None
return result[0][0]
def get_url(shrinked_hash: str):
query = f"""
SELECT url FROM urls WHERE shrinked_hash = '{shrinked_hash}'
"""
result = execute_query(query)
if len(result) == 0:
return None
return result[0][0]
def insert_url(url: str, shrinked_hash: str):
if get_shrinked_hash(url) is not None or get_url(shrinked_hash) is not None:
print('Insertion failed.')
return False
non_query = f"""
INSERT INTO urls
VALUES ('{url}', '{shrinked_hash}')
"""
execute_non_query(non_query)
return True
if __name__ == '__main__':
create_table()
# insert_url('test1', 'abcdef')
# insert_url('test2', 'abdejjk')
# insert_url('test2', 'adnjdd')
# print(get_shrinked_hash('test1'))
# print(get_shrinked_hash('test2'))
# print(get_url('abcdef'))
cursor.close()
conn.close()