searxng/searx/engines/sqlite.py
Markus Heiser 10ea2eef3a [enh] utils: implement a context manager for a SQLite cursor
usage in a with statement [2]::

    with SQLiteCursor('test.db') as cur:
        print(cur.execute('select sqlite_version();').fetchall()[0][0])

[1] https://docs.python.org/3/library/stdtypes.html#context-manager-types
[2] https://docs.python.org/3/reference/compound_stmts.html#with

Signed-off-by: Markus Heiser <markus.heiser@darmarit.de>
2021-05-26 14:35:15 +02:00

51 lines
1.4 KiB
Python

# SPDX-License-Identifier: AGPL-3.0-or-later
# lint: pylint
# pylint: disable=missing-function-docstring
"""SQLite database (Offline)
"""
from searx import logger
from searx.utils import SQLiteCursor
logger = logger.getChild('SQLite engine')
engine_type = 'offline'
database = ""
query_str = ""
limit = 10
paging = True
result_template = 'key-value.html'
def init(engine_settings):
if 'query_str' not in engine_settings:
raise ValueError('query_str cannot be empty')
if not engine_settings['query_str'].lower().startswith('select '):
raise ValueError('only SELECT query is supported')
def search(query, params):
global database, query_str, result_template # pylint: disable=global-statement
results = []
query_params = {
'query': query,
'wildcard': r'%' + query.replace(' ',r'%') + r'%',
'limit': limit,
'offset': (params['pageno'] - 1) * limit
}
query_to_run = query_str + ' LIMIT :limit OFFSET :offset'
with SQLiteCursor(database) as cur:
cur.execute(query_to_run, query_params)
col_names = [cn[0] for cn in cur.description]
for row in cur.fetchall():
item = dict( zip(col_names, map(str, row)) )
item['template'] = result_template
logger.debug("append result --> %s", item)
results.append(item)
return results