-
Notifications
You must be signed in to change notification settings - Fork 0
/
sqlitehandler.py
105 lines (93 loc) · 2.83 KB
/
sqlitehandler.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import sqlite3
import logging
def sanity(function):
def wrapper(*args):
print(f'\033[93mSainity check {__name__}@{function.__name__}{args}\033[0m')
try:
logging.info(f'{function}:{__name__}@{function.__name__}({args})')
return function(*args)
except Exception as ex:
logging.warning(f'{function}:{__name__}@{function.__name__}({args}): FAILED -> {ex}')
return ex
return wrapper
#--- connection to database ---#
@sanity
def connect(db):
con = sqlite3.connect(db)
return con
#--- make changes in database ---#
@sanity
def commit(con, cmd):
cur = con.cursor()
cur.execute(cmd)
res = con.commit()
return res
#--- search in database ---#
@sanity
def query(con, cmd):
cur = con.cursor()
cur.execute(cmd)
data = cur.fetchall()
return data
#--- Basic serach ---#
@sanity
def search(con, table, key, value):
cur = con.cursor()
cur.execute(f"SELECT * FROM {table} WHERE {key}='{value}'")
data = cur.fetchall()
return data
#--- Fuzzy search ---#
@sanity
def fuzzy_search(con, table, key, value):
cur = con.cursor()
cur.execute(f"SELECT * FROM {table} WHERE {key} LIKE '{value}'")
data = cur.fetchall()
return data
#--- Count elements ---#
@sanity
def count(con, items, table) -> int:
cur = con.cursor()
cur.execute(f"SELECT COUNT(DISTINCT {items}) FROM {table}")
data = cur.fetchall()
return data[0][0]
#--- Count elements from key ---#
@sanity
def distinct_count(con, items, table, key, value) -> int:
cur = con.cursor()
cur.execute(f"SELECT COUNT(DISTINCT {items}) FROM {table} WHERE {key} LIKE'{value}'")
data = cur.fetchall()
return data[0][0]
#--- load table as dictionary ---#
@sanity
def load_table_dict(con, table):
con.row_factory = sqlite3.Row
cur = con.cursor()
cur.execute(f"SELECT * FROM {table}")
data = [dict(row) for row in cur.fetchall()]
return data
#--- distinct search return dictionary ---#
@sanity
def get_as_dict(con, table, key, value):
con.row_factory = sqlite3.Row
cur = con.cursor()
cur.execute(f"SELECT * FROM {table} WHERE {key}='{value}'")
data = [dict(row) for row in cur.fetchall()]
return data
#--- fuzzy search return dictionary ---#
@sanity
def get_fuzzy_dict(con, table, key, value):
con.row_factory = sqlite3.Row
cur = con.cursor()
cur.execute(f"SELECT * FROM {table} WHERE {key} LIKE '{value}'")
data = [dict(row) for row in cur.fetchall()]
return data
#--- Remove duplicate IDs ---#
@sanity
def cleanup(con, table, key):
cmds = [
f"CREATE TABLE table_temp AS SELECT DISTINCT * FROM {table} WHERE {key} NOT IN (SELECT MIN({key}) FROM {table})",
f"DROP TABLE {table}",
f"ALTER TABLE table_temp RENAME TO {table}"]
for cmd in cmds:
commit(con, cmd)
return True