.oO SearXNG Developer Documentation Oo.
Loading...
Searching...
No Matches
sqlite.py
Go to the documentation of this file.
1# SPDX-License-Identifier: AGPL-3.0-or-later
2"""SQLite is a small, fast and reliable SQL database engine. It does not require
3any extra dependency.
4
5Example
6=======
7
8.. _MediathekView: https://mediathekview.de/
9
10To demonstrate the power of database engines, here is a more complex example
11which reads from a MediathekView_ (DE) movie database. For this example of the
12SQLite engine download the database:
13
14- https://liste.mediathekview.de/filmliste-v2.db.bz2
15
16and unpack into ``searx/data/filmliste-v2.db``. To search the database use e.g
17Query to test: ``!mediathekview concert``
18
19.. code:: yaml
20
21 - name: mediathekview
22 engine: sqlite
23 disabled: False
24 categories: general
25 result_template: default.html
26 database: searx/data/filmliste-v2.db
27 query_str: >-
28 SELECT title || ' (' || time(duration, 'unixepoch') || ')' AS title,
29 COALESCE( NULLIF(url_video_hd,''), NULLIF(url_video_sd,''), url_video) AS url,
30 description AS content
31 FROM film
32 WHERE title LIKE :wildcard OR description LIKE :wildcard
33 ORDER BY duration DESC
34
35Implementations
36===============
37
38"""
39
40import sqlite3
41import contextlib
42
43engine_type = 'offline'
44
45database = ""
46"""Filename of the SQLite DB."""
47
48query_str = ""
49"""SQL query that returns the result items."""
50
51limit = 10
52paging = True
53result_template = 'key-value.html'
54
55
56def init(engine_settings):
57 if 'query_str' not in engine_settings:
58 raise ValueError('query_str cannot be empty')
59
60 if not engine_settings['query_str'].lower().startswith('select '):
61 raise ValueError('only SELECT query is supported')
62
63
64@contextlib.contextmanager
66 """Implements a :py:obj:`Context Manager <contextlib.contextmanager>` for a
67 :py:obj:`sqlite3.Cursor`.
68
69 Open database in read only mode: if the database doesn't exist. The default
70 mode creates an empty file on the file system. See:
71
72 * https://docs.python.org/3/library/sqlite3.html#sqlite3.connect
73 * https://www.sqlite.org/uri.html
74
75 """
76 uri = 'file:' + database + '?mode=ro'
77 with contextlib.closing(sqlite3.connect(uri, uri=True)) as connect:
78 connect.row_factory = sqlite3.Row
79 with contextlib.closing(connect.cursor()) as cursor:
80 yield cursor
81
82
83def search(query, params):
84 results = []
85
86 query_params = {
87 'query': query,
88 'wildcard': r'%' + query.replace(' ', r'%') + r'%',
89 'limit': limit,
90 'offset': (params['pageno'] - 1) * limit,
91 }
92 query_to_run = query_str + ' LIMIT :limit OFFSET :offset'
93
94 with sqlite_cursor() as cur:
95
96 cur.execute(query_to_run, query_params)
97 col_names = [cn[0] for cn in cur.description]
98
99 for row in cur.fetchall():
100 item = dict(zip(col_names, map(str, row)))
101 item['template'] = result_template
102 logger.debug("append result --> %s", item)
103 results.append(item)
104
105 return results
init(engine_settings)
Definition sqlite.py:56