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