@@ -0,0 +1,65 @@
1+# SPDX-License-Identifier: AGPL-3.0-or-later
2+"""
3+ PostgreSQL database (Offline)
4+"""
5+
6+import psycopg2
7+
8+engine_type = 'offline'
9+host = "127.0.0.1"
10+port = "5432"
11+database = ""
12+username = ""
13+password = ""
14+query_str = ""
15+limit = 10
16+paging = True
17+result_template = 'key-value.html'
18+_connection = None
19+
20+
21+def init(engine_settings):
22+ if 'query_str' not in engine_settings:
23+ raise ValueError('query_str cannot be empty')
24+
25+ if not engine_settings['query_str'].lower().startswith('select '):
26+ raise ValueError('only SELECT query is supported')
27+
28+ global _connection
29+ _connection = psycopg2.connect(
30+ database = database,
31+ user = username,
32+ password = password,
33+ host = host,
34+ port = port,
35+ )
36+
37+
38+def search(query, params):
39+ query_params = {'query': query}
40+ query_to_run = query_str + ' LIMIT {0} OFFSET {1}'.format(limit, (params['pageno'] - 1) * limit)
41+
42+ with _connection:
43+ with _connection.cursor() as cur:
44+ cur.execute(query_str, query_params)
45+
46+ return _fetch_results(cur)
47+
48+
49+def _fetch_results(cur):
50+ results = []
51+ titles = []
52+
53+ try:
54+ titles = [column_desc.name for column_desc in cur.description]
55+
56+ for res in cur:
57+ result = dict(zip(titles, map(str, res)))
58+ result['template'] = result_template
59+ results.append(result)
60+
61+ # no results to fetch
62+ except psycopg2.ProgrammingError:
63+ pass
64+
65+ return results