-
Notifications
You must be signed in to change notification settings - Fork 0
/
sql_adapter.py
36 lines (30 loc) · 1.36 KB
/
sql_adapter.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
from cache_db import CacheDB
class SQLAdapter:
_default_parameters = {
'anonDB': None,
'rawDB': None,
}
_required_parameters = ['anonDB', 'rawDB']
def __init__(self, parameters):
self._parameters = SQLAdapter._default_parameters.copy()
for parameter, value in parameters.items():
if parameter in self._required_parameters:
self._parameters[parameter] = value
for parameter in self._required_parameters:
if parameter not in self._parameters:
raise ValueError(f"Required parameter {parameter} not provided")
self._anon_db = CacheDB(parameters['anonDB']) if parameters['anonDB'] is not None else None
self._raw_db = CacheDB(parameters['rawDB']) if parameters['rawDB'] is not None else None
def queryRaw(self, sql):
if self._raw_db is None:
raise ValueError("RawDB not configured")
return self._raw_db.execute_sql(sql)
def queryDiffix(self, sql):
if self._anon_db is None:
raise ValueError("AnonDB not configured")
return self._anon_db.execute_sql(sql)
def disconnect(self):
if self._raw_db is not None and self._raw_db.is_connected():
self._raw_db.disconnect()
if self._anon_db is not None and self._anon_db.is_connected():
self._anon_db.disconnect()