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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
|
<!DOCTYPE html>
<html lang="en" data-content_root="../../">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>searx.sqlitedb — SearXNG Documentation (2025.1.6+6dab7fe78)</title>
<link rel="stylesheet" type="text/css" href="../../_static/pygments.css?v=4f649999" />
<link rel="stylesheet" type="text/css" href="../../_static/searxng.css?v=52e4ff28" />
<script src="../../_static/documentation_options.js?v=ef740023"></script>
<script src="../../_static/doctools.js?v=9a2dae69"></script>
<script src="../../_static/sphinx_highlight.js?v=dc90522c"></script>
<script data-project="searxng" data-version="2025.1.6+6dab7fe78" src="../../_static/describe_version.js?v=fa7f30d0"></script>
<link rel="index" title="Index" href="../../genindex.html" />
<link rel="search" title="Search" href="../../search.html" />
</head><body>
<div class="related" role="navigation" aria-label="Related">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="../../genindex.html" title="General Index"
accesskey="I">index</a></li>
<li class="right" >
<a href="../../py-modindex.html" title="Python Module Index"
>modules</a> |</li>
<li class="nav-item nav-item-0"><a href="../../index.html">SearXNG Documentation (2025.1.6+6dab7fe78)</a> »</li>
<li class="nav-item nav-item-1"><a href="../index.html" accesskey="U">Module code</a> »</li>
<li class="nav-item nav-item-this"><a href="">searx.sqlitedb</a></li>
</ul>
</div>
<div class="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body" role="main">
<h1>Source code for searx.sqlitedb</h1><div class="highlight"><pre>
<span></span><span class="c1"># SPDX-License-Identifier: AGPL-3.0-or-later</span>
<span class="sd">"""Implementations to make access to SQLite databases a little more convenient.</span>
<span class="sd">:py:obj:`SQLiteAppl`</span>
<span class="sd"> Abstract class with which DB applications can be implemented.</span>
<span class="sd">:py:obj:`SQLiteProperties`:</span>
<span class="sd"> Class to manage properties stored in a database.</span>
<span class="sd">----</span>
<span class="sd">"""</span>
<span class="kn">from</span> <span class="nn">__future__</span> <span class="kn">import</span> <span class="n">annotations</span>
<span class="kn">import</span> <span class="nn">sys</span>
<span class="kn">import</span> <span class="nn">re</span>
<span class="kn">import</span> <span class="nn">sqlite3</span>
<span class="kn">import</span> <span class="nn">threading</span>
<span class="kn">import</span> <span class="nn">abc</span>
<span class="kn">from</span> <span class="nn">searx</span> <span class="kn">import</span> <span class="n">logger</span>
<span class="n">logger</span> <span class="o">=</span> <span class="n">logger</span><span class="o">.</span><span class="n">getChild</span><span class="p">(</span><span class="s1">'sqlitedb'</span><span class="p">)</span>
<div class="viewcode-block" id="SQLiteAppl">
<a class="viewcode-back" href="../../src/searx.sqlitedb.html#searx.sqlitedb.SQLiteAppl">[docs]</a>
<span class="k">class</span> <span class="nc">SQLiteAppl</span><span class="p">(</span><span class="n">abc</span><span class="o">.</span><span class="n">ABC</span><span class="p">):</span>
<span class="w"> </span><span class="sd">"""Abstract base class for implementing convenient DB access in SQLite</span>
<span class="sd"> applications. In the constructor, a :py:obj:`SQLiteProperties` instance is</span>
<span class="sd"> already aggregated under ``self.properties``."""</span>
<span class="n">DDL_CREATE_TABLES</span><span class="p">:</span> <span class="nb">dict</span><span class="p">[</span><span class="nb">str</span><span class="p">,</span> <span class="nb">str</span><span class="p">]</span> <span class="o">=</span> <span class="p">{}</span>
<span class="n">DB_SCHEMA</span><span class="p">:</span> <span class="nb">int</span> <span class="o">=</span> <span class="mi">1</span>
<span class="w"> </span><span class="sd">"""As soon as changes are made to the DB schema, the version number must be</span>
<span class="sd"> increased. Changes to the version number require the DB to be recreated (or</span>
<span class="sd"> migrated / if an migration path exists and is implemented)."""</span>
<span class="n">SQLITE_THREADING_MODE</span> <span class="o">=</span> <span class="p">{</span>
<span class="mi">0</span><span class="p">:</span> <span class="s2">"single-thread"</span><span class="p">,</span>
<span class="mi">1</span><span class="p">:</span> <span class="s2">"multi-thread"</span><span class="p">,</span>
<span class="mi">3</span><span class="p">:</span> <span class="s2">"serialized"</span><span class="p">}[</span><span class="n">sqlite3</span><span class="o">.</span><span class="n">threadsafety</span><span class="p">]</span> <span class="c1"># fmt:skip</span>
<span class="w"> </span><span class="sd">"""Threading mode of the SQLite library. Depends on the options used at</span>
<span class="sd"> compile time and is different for different distributions and architectures.</span>
<span class="sd"> Possible values are 0:``single-thread``, 1:``multi-thread``,</span>
<span class="sd"> 3:``serialized`` (see :py:obj:`sqlite3.threadsafety`). Pre- Python 3.11</span>
<span class="sd"> this value was hard coded to 1.</span>
<span class="sd"> Depending on this value, optimizations are made, e.g. in “serialized” mode</span>
<span class="sd"> it is not necessary to create a separate DB connector for each thread.</span>
<span class="sd"> """</span>
<span class="n">SQLITE_JOURNAL_MODE</span> <span class="o">=</span> <span class="s2">"WAL"</span>
<span class="n">SQLITE_CONNECT_ARGS</span> <span class="o">=</span> <span class="p">{</span>
<span class="c1"># "timeout": 5.0,</span>
<span class="c1"># "detect_types": 0,</span>
<span class="s2">"check_same_thread"</span><span class="p">:</span> <span class="nb">bool</span><span class="p">(</span><span class="n">SQLITE_THREADING_MODE</span> <span class="o">!=</span> <span class="s2">"serialized"</span><span class="p">),</span>
<span class="s2">"cached_statements"</span><span class="p">:</span> <span class="mi">0</span><span class="p">,</span> <span class="c1"># https://github.com/python/cpython/issues/118172</span>
<span class="c1"># "uri": False,</span>
<span class="s2">"autocommit"</span><span class="p">:</span> <span class="kc">False</span><span class="p">,</span>
<span class="p">}</span> <span class="c1"># fmt:skip</span>
<span class="w"> </span><span class="sd">"""Connection arguments (:py:obj:`sqlite3.connect`)</span>
<span class="sd"> ``check_same_thread``:</span>
<span class="sd"> Is disabled by default when :py:obj:`SQLITE_THREADING_MODE` is</span>
<span class="sd"> ``serialized``. The check is more of a hindrance in this case because it</span>
<span class="sd"> would prevent a DB connector from being used in multiple threads.</span>
<span class="sd"> ``autocommit``:</span>
<span class="sd"> Is disabled by default. Note: autocommit option has been added in Python</span>
<span class="sd"> 3.12.</span>
<span class="sd"> ``cached_statements``:</span>
<span class="sd"> Is set to ``0`` by default. Note: Python 3.12+ fetch result are not</span>
<span class="sd"> consistent in multi-threading application and causing an API misuse error.</span>
<span class="sd"> The multithreading use in SQLiteAppl is intended and supported if</span>
<span class="sd"> threadsafety is set to 3 (aka "serialized"). CPython supports “serialized”</span>
<span class="sd"> from version 3.12 on, but unfortunately only with errors:</span>
<span class="sd"> - https://github.com/python/cpython/issues/118172</span>
<span class="sd"> - https://github.com/python/cpython/issues/123873</span>
<span class="sd"> The workaround for SQLite3 multithreading cache inconsistency ist to set</span>
<span class="sd"> option ``cached_statements`` to ``0`` by default.</span>
<span class="sd"> """</span>
<span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">db_url</span><span class="p">):</span>
<span class="bp">self</span><span class="o">.</span><span class="n">db_url</span> <span class="o">=</span> <span class="n">db_url</span>
<span class="bp">self</span><span class="o">.</span><span class="n">properties</span> <span class="o">=</span> <span class="n">SQLiteProperties</span><span class="p">(</span><span class="n">db_url</span><span class="p">)</span>
<span class="bp">self</span><span class="o">.</span><span class="n">thread_local</span> <span class="o">=</span> <span class="n">threading</span><span class="o">.</span><span class="n">local</span><span class="p">()</span>
<span class="bp">self</span><span class="o">.</span><span class="n">_init_done</span> <span class="o">=</span> <span class="kc">False</span>
<span class="bp">self</span><span class="o">.</span><span class="n">_compatibility</span><span class="p">()</span>
<span class="k">def</span> <span class="nf">_compatibility</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">SQLITE_THREADING_MODE</span> <span class="o">==</span> <span class="s2">"serialized"</span><span class="p">:</span>
<span class="bp">self</span><span class="o">.</span><span class="n">_DB</span> <span class="o">=</span> <span class="kc">None</span>
<span class="k">else</span><span class="p">:</span>
<span class="n">msg</span> <span class="o">=</span> <span class="p">(</span>
<span class="sa">f</span><span class="s2">"SQLite library is compiled with </span><span class="si">{</span><span class="bp">self</span><span class="o">.</span><span class="n">SQLITE_THREADING_MODE</span><span class="si">}</span><span class="s2"> mode,"</span>
<span class="s2">" read https://docs.python.org/3/library/sqlite3.html#sqlite3.threadsafety"</span>
<span class="p">)</span>
<span class="k">if</span> <span class="n">threading</span><span class="o">.</span><span class="n">active_count</span><span class="p">()</span> <span class="o">></span> <span class="mi">1</span><span class="p">:</span>
<span class="n">logger</span><span class="o">.</span><span class="n">error</span><span class="p">(</span><span class="n">msg</span><span class="p">)</span>
<span class="k">else</span><span class="p">:</span>
<span class="n">logger</span><span class="o">.</span><span class="n">warning</span><span class="p">(</span><span class="n">msg</span><span class="p">)</span>
<span class="k">if</span> <span class="n">sqlite3</span><span class="o">.</span><span class="n">sqlite_version_info</span> <span class="o"><=</span> <span class="p">(</span><span class="mi">3</span><span class="p">,</span> <span class="mi">35</span><span class="p">):</span>
<span class="c1"># See "Generalize UPSERT:" in https://sqlite.org/releaselog/3_35_0.html</span>
<span class="n">logger</span><span class="o">.</span><span class="n">critical</span><span class="p">(</span>
<span class="s2">"SQLite runtime library version </span><span class="si">%s</span><span class="s2"> is not supported (require >= 3.35)"</span><span class="p">,</span> <span class="n">sqlite3</span><span class="o">.</span><span class="n">sqlite_version</span>
<span class="p">)</span>
<div class="viewcode-block" id="SQLiteAppl.connect">
<a class="viewcode-back" href="../../src/searx.sqlitedb.html#searx.sqlitedb.SQLiteAppl.connect">[docs]</a>
<span class="k">def</span> <span class="nf">connect</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span> <span class="o">-></span> <span class="n">sqlite3</span><span class="o">.</span><span class="n">Connection</span><span class="p">:</span>
<span class="w"> </span><span class="sd">"""Creates a new DB connection (:py:obj:`SQLITE_CONNECT_ARGS`). If not</span>
<span class="sd"> already done, the DB schema is set up</span>
<span class="sd"> """</span>
<span class="k">if</span> <span class="n">sys</span><span class="o">.</span><span class="n">version_info</span> <span class="o"><</span> <span class="p">(</span><span class="mi">3</span><span class="p">,</span> <span class="mi">12</span><span class="p">):</span>
<span class="c1"># Prior Python 3.12 there is no "autocommit" option</span>
<span class="bp">self</span><span class="o">.</span><span class="n">SQLITE_CONNECT_ARGS</span><span class="o">.</span><span class="n">pop</span><span class="p">(</span><span class="s2">"autocommit"</span><span class="p">,</span> <span class="kc">None</span><span class="p">)</span>
<span class="bp">self</span><span class="o">.</span><span class="n">init</span><span class="p">()</span>
<span class="n">logger</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s2">"</span><span class="si">%s</span><span class="s2">: connect to DB: </span><span class="si">%s</span><span class="s2"> // </span><span class="si">%s</span><span class="s2">"</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="vm">__class__</span><span class="o">.</span><span class="vm">__name__</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">db_url</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">SQLITE_CONNECT_ARGS</span><span class="p">)</span>
<span class="n">conn</span> <span class="o">=</span> <span class="n">sqlite3</span><span class="o">.</span><span class="n">Connection</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">db_url</span><span class="p">,</span> <span class="o">**</span><span class="bp">self</span><span class="o">.</span><span class="n">SQLITE_CONNECT_ARGS</span><span class="p">)</span> <span class="c1"># type: ignore</span>
<span class="n">conn</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="sa">f</span><span class="s2">"PRAGMA journal_mode=</span><span class="si">{</span><span class="bp">self</span><span class="o">.</span><span class="n">SQLITE_JOURNAL_MODE</span><span class="si">}</span><span class="s2">"</span><span class="p">)</span>
<span class="bp">self</span><span class="o">.</span><span class="n">register_functions</span><span class="p">(</span><span class="n">conn</span><span class="p">)</span>
<span class="k">return</span> <span class="n">conn</span></div>
<div class="viewcode-block" id="SQLiteAppl.register_functions">
<a class="viewcode-back" href="../../src/searx.sqlitedb.html#searx.sqlitedb.SQLiteAppl.register_functions">[docs]</a>
<span class="k">def</span> <span class="nf">register_functions</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">conn</span><span class="p">):</span>
<span class="w"> </span><span class="sd">"""Create user-defined_ SQL functions.</span>
<span class="sd"> ``REGEXP(<pattern>, <field>)`` : 0 | 1</span>
<span class="sd"> `re.search`_ returns (int) 1 for a match and 0 for none match of</span>
<span class="sd"> ``<pattern>`` in ``<field>``.</span>
<span class="sd"> .. code:: sql</span>
<span class="sd"> SELECT '12' AS field WHERE REGEXP('^[0-9][0-9]$', field)</span>
<span class="sd"> -- 12</span>
<span class="sd"> SELECT REGEXP('[0-9][0-9]', 'X12Y')</span>
<span class="sd"> -- 1</span>
<span class="sd"> SELECT REGEXP('[0-9][0-9]', 'X1Y')</span>
<span class="sd"> -- 0</span>
<span class="sd"> .. _user-defined: https://docs.python.org/3/library/sqlite3.html#sqlite3.Connection.create_function</span>
<span class="sd"> .. _deterministic: https://sqlite.org/deterministic.html</span>
<span class="sd"> .. _re.search: https://docs.python.org/3/library/re.html#re.search</span>
<span class="sd"> """</span>
<span class="n">conn</span><span class="o">.</span><span class="n">create_function</span><span class="p">(</span><span class="s1">'regexp'</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="k">lambda</span> <span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">:</span> <span class="mi">1</span> <span class="k">if</span> <span class="n">re</span><span class="o">.</span><span class="n">search</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">)</span> <span class="k">else</span> <span class="mi">0</span><span class="p">,</span> <span class="n">deterministic</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span></div>
<span class="nd">@property</span>
<span class="k">def</span> <span class="nf">DB</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span> <span class="o">-></span> <span class="n">sqlite3</span><span class="o">.</span><span class="n">Connection</span><span class="p">:</span>
<span class="w"> </span><span class="sd">"""Provides a DB connection. The connection is a *singleton* and</span>
<span class="sd"> therefore well suited for read access. If</span>
<span class="sd"> :py:obj:`SQLITE_THREADING_MODE` is ``serialized`` only one DB connection</span>
<span class="sd"> is created for all threads.</span>
<span class="sd"> .. note::</span>
<span class="sd"> For dedicated `transaction control`_, it is recommended to create a</span>
<span class="sd"> new connection (:py:obj:`SQLiteAppl.connect`).</span>
<span class="sd"> .. _transaction control:</span>
<span class="sd"> https://docs.python.org/3/library/sqlite3.html#sqlite3-controlling-transactions</span>
<span class="sd"> """</span>
<span class="k">if</span> <span class="nb">getattr</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">thread_local</span><span class="p">,</span> <span class="s1">'DB'</span><span class="p">,</span> <span class="kc">None</span><span class="p">)</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
<span class="bp">self</span><span class="o">.</span><span class="n">thread_local</span><span class="o">.</span><span class="n">DB</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">connect</span><span class="p">()</span>
<span class="c1"># Theoretically it is possible to reuse the DB cursor across threads as</span>
<span class="c1"># of Python 3.12, in practice the threading of the cursor seems to me to</span>
<span class="c1"># be so faulty that I prefer to establish one connection per thread</span>
<span class="bp">self</span><span class="o">.</span><span class="n">thread_local</span><span class="o">.</span><span class="n">DB</span><span class="o">.</span><span class="n">commit</span><span class="p">()</span>
<span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">thread_local</span><span class="o">.</span><span class="n">DB</span>
<span class="c1"># In "serialized" mode, SQLite can be safely used by multiple threads</span>
<span class="c1"># with no restriction.</span>
<span class="c1">#</span>
<span class="c1"># if self.SQLITE_THREADING_MODE != "serialized":</span>
<span class="c1"># if getattr(self.thread_local, 'DB', None) is None:</span>
<span class="c1"># self.thread_local.DB = self.connect()</span>
<span class="c1"># return self.thread_local.DB</span>
<span class="c1">#</span>
<span class="c1"># if self._DB is None:</span>
<span class="c1"># self._DB = self.connect() # pylint: disable=attribute-defined-outside-init</span>
<span class="c1"># return self._DB</span>
<div class="viewcode-block" id="SQLiteAppl.init">
<a class="viewcode-back" href="../../src/searx.sqlitedb.html#searx.sqlitedb.SQLiteAppl.init">[docs]</a>
<span class="k">def</span> <span class="nf">init</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="w"> </span><span class="sd">"""Initializes the DB schema and properties, is only executed once even</span>
<span class="sd"> if called several times."""</span>
<span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">_init_done</span><span class="p">:</span>
<span class="k">return</span>
<span class="bp">self</span><span class="o">.</span><span class="n">_init_done</span> <span class="o">=</span> <span class="kc">True</span>
<span class="n">logger</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s2">"init DB: </span><span class="si">%s</span><span class="s2">"</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">db_url</span><span class="p">)</span>
<span class="bp">self</span><span class="o">.</span><span class="n">properties</span><span class="o">.</span><span class="n">init</span><span class="p">()</span>
<span class="n">ver</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">properties</span><span class="p">(</span><span class="s2">"DB_SCHEMA"</span><span class="p">)</span>
<span class="k">if</span> <span class="n">ver</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
<span class="k">with</span> <span class="bp">self</span><span class="o">.</span><span class="n">properties</span><span class="o">.</span><span class="n">DB</span><span class="p">:</span>
<span class="bp">self</span><span class="o">.</span><span class="n">create_schema</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">properties</span><span class="o">.</span><span class="n">DB</span><span class="p">)</span>
<span class="k">else</span><span class="p">:</span>
<span class="n">ver</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="n">ver</span><span class="p">)</span>
<span class="k">if</span> <span class="n">ver</span> <span class="o">!=</span> <span class="bp">self</span><span class="o">.</span><span class="n">DB_SCHEMA</span><span class="p">:</span>
<span class="k">raise</span> <span class="n">sqlite3</span><span class="o">.</span><span class="n">DatabaseError</span><span class="p">(</span><span class="s2">"Expected DB schema v</span><span class="si">%s</span><span class="s2">, DB schema is v</span><span class="si">%s</span><span class="s2">"</span> <span class="o">%</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">DB_SCHEMA</span><span class="p">,</span> <span class="n">ver</span><span class="p">))</span>
<span class="n">logger</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s2">"DB_SCHEMA = </span><span class="si">%s</span><span class="s2">"</span><span class="p">,</span> <span class="n">ver</span><span class="p">)</span></div>
<span class="k">def</span> <span class="nf">create_schema</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">conn</span><span class="p">):</span>
<span class="n">logger</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s2">"create schema .."</span><span class="p">)</span>
<span class="k">with</span> <span class="n">conn</span><span class="p">:</span>
<span class="k">for</span> <span class="n">table_name</span><span class="p">,</span> <span class="n">sql</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">DDL_CREATE_TABLES</span><span class="o">.</span><span class="n">items</span><span class="p">():</span>
<span class="n">conn</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="n">sql</span><span class="p">)</span>
<span class="bp">self</span><span class="o">.</span><span class="n">properties</span><span class="o">.</span><span class="n">set</span><span class="p">(</span><span class="sa">f</span><span class="s2">"Table </span><span class="si">{</span><span class="n">table_name</span><span class="si">}</span><span class="s2"> created"</span><span class="p">,</span> <span class="n">table_name</span><span class="p">)</span>
<span class="bp">self</span><span class="o">.</span><span class="n">properties</span><span class="o">.</span><span class="n">set</span><span class="p">(</span><span class="s2">"DB_SCHEMA"</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">DB_SCHEMA</span><span class="p">)</span>
<span class="bp">self</span><span class="o">.</span><span class="n">properties</span><span class="o">.</span><span class="n">set</span><span class="p">(</span><span class="s2">"LAST_MAINTENANCE"</span><span class="p">,</span> <span class="s2">""</span><span class="p">)</span></div>
<div class="viewcode-block" id="SQLiteProperties">
<a class="viewcode-back" href="../../src/searx.sqlitedb.html#searx.sqlitedb.SQLiteProperties">[docs]</a>
<span class="k">class</span> <span class="nc">SQLiteProperties</span><span class="p">(</span><span class="n">SQLiteAppl</span><span class="p">):</span>
<span class="w"> </span><span class="sd">"""Simple class to manage properties of a DB application in the DB. The</span>
<span class="sd"> object has its own DB connection and transaction area.</span>
<span class="sd"> .. code:: sql</span>
<span class="sd"> CREATE TABLE IF NOT EXISTS properties (</span>
<span class="sd"> name TEXT,</span>
<span class="sd"> value TEXT,</span>
<span class="sd"> m_time INTEGER DEFAULT (strftime('%s', 'now')),</span>
<span class="sd"> PRIMARY KEY (name))</span>
<span class="sd"> """</span>
<span class="n">SQLITE_JOURNAL_MODE</span> <span class="o">=</span> <span class="s2">"WAL"</span>
<span class="n">DDL_PROPERTIES</span> <span class="o">=</span> <span class="s2">"""</span><span class="se">\</span>
<span class="s2">CREATE TABLE IF NOT EXISTS properties (</span>
<span class="s2"> name TEXT,</span>
<span class="s2"> value TEXT,</span>
<span class="s2"> m_time INTEGER DEFAULT (strftime('</span><span class="si">%s</span><span class="s2">', 'now')), -- last modified (unix epoch) time in sec.</span>
<span class="s2"> PRIMARY KEY (name))"""</span>
<span class="w"> </span><span class="sd">"""Table to store properties of the DB application"""</span>
<span class="n">SQL_GET</span> <span class="o">=</span> <span class="s2">"SELECT value FROM properties WHERE name = ?"</span>
<span class="n">SQL_M_TIME</span> <span class="o">=</span> <span class="s2">"SELECT m_time FROM properties WHERE name = ?"</span>
<span class="n">SQL_SET</span> <span class="o">=</span> <span class="p">(</span>
<span class="s2">"INSERT INTO properties (name, value) VALUES (?, ?)"</span>
<span class="s2">" ON CONFLICT(name) DO UPDATE"</span>
<span class="s2">" SET value=excluded.value, m_time=strftime('</span><span class="si">%s</span><span class="s2">', 'now')"</span>
<span class="p">)</span>
<span class="n">SQL_TABLE_EXISTS</span> <span class="o">=</span> <span class="p">(</span>
<span class="s2">"SELECT name FROM sqlite_master"</span>
<span class="s2">" WHERE type='table' AND name='properties'"</span>
<span class="p">)</span> <span class="c1"># fmt:skip</span>
<span class="n">SQLITE_CONNECT_ARGS</span> <span class="o">=</span> <span class="nb">dict</span><span class="p">(</span><span class="n">SQLiteAppl</span><span class="o">.</span><span class="n">SQLITE_CONNECT_ARGS</span><span class="p">)</span>
<span class="n">SQLITE_CONNECT_ARGS</span><span class="p">[</span><span class="s2">"autocommit"</span><span class="p">]</span> <span class="o">=</span> <span class="kc">True</span> <span class="c1"># This option has no effect before Python 3.12</span>
<span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">db_url</span><span class="p">:</span> <span class="nb">str</span><span class="p">):</span> <span class="c1"># pylint: disable=super-init-not-called</span>
<span class="bp">self</span><span class="o">.</span><span class="n">db_url</span> <span class="o">=</span> <span class="n">db_url</span>
<span class="bp">self</span><span class="o">.</span><span class="n">thread_local</span> <span class="o">=</span> <span class="n">threading</span><span class="o">.</span><span class="n">local</span><span class="p">()</span>
<span class="bp">self</span><span class="o">.</span><span class="n">_init_done</span> <span class="o">=</span> <span class="kc">False</span>
<span class="bp">self</span><span class="o">.</span><span class="n">_compatibility</span><span class="p">()</span>
<div class="viewcode-block" id="SQLiteProperties.init">
<a class="viewcode-back" href="../../src/searx.sqlitedb.html#searx.sqlitedb.SQLiteProperties.init">[docs]</a>
<span class="k">def</span> <span class="nf">init</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="w"> </span><span class="sd">"""Initializes DB schema of the properties in the DB."""</span>
<span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">_init_done</span><span class="p">:</span>
<span class="k">return</span>
<span class="bp">self</span><span class="o">.</span><span class="n">_init_done</span> <span class="o">=</span> <span class="kc">True</span>
<span class="n">logger</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s2">"init properties of DB: </span><span class="si">%s</span><span class="s2">"</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">db_url</span><span class="p">)</span>
<span class="k">with</span> <span class="bp">self</span><span class="o">.</span><span class="n">DB</span> <span class="k">as</span> <span class="n">conn</span><span class="p">:</span>
<span class="n">res</span> <span class="o">=</span> <span class="n">conn</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">SQL_TABLE_EXISTS</span><span class="p">)</span>
<span class="k">if</span> <span class="n">res</span><span class="o">.</span><span class="n">fetchone</span><span class="p">()</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span> <span class="c1"># DB schema needs to be be created</span>
<span class="bp">self</span><span class="o">.</span><span class="n">create_schema</span><span class="p">(</span><span class="n">conn</span><span class="p">)</span></div>
<span class="k">def</span> <span class="fm">__call__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">name</span><span class="p">,</span> <span class="n">default</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
<span class="w"> </span><span class="sd">"""Returns the value of the property ``name`` or ``default`` if property</span>
<span class="sd"> not exists in DB."""</span>
<span class="n">res</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">DB</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">SQL_GET</span><span class="p">,</span> <span class="p">(</span><span class="n">name</span><span class="p">,))</span><span class="o">.</span><span class="n">fetchone</span><span class="p">()</span>
<span class="k">if</span> <span class="n">res</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
<span class="k">return</span> <span class="n">default</span>
<span class="k">return</span> <span class="n">res</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
<div class="viewcode-block" id="SQLiteProperties.set">
<a class="viewcode-back" href="../../src/searx.sqlitedb.html#searx.sqlitedb.SQLiteProperties.set">[docs]</a>
<span class="k">def</span> <span class="nf">set</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">name</span><span class="p">,</span> <span class="n">value</span><span class="p">):</span>
<span class="w"> </span><span class="sd">"""Set ``value`` of property ``name`` in DB. If property already</span>
<span class="sd"> exists, update the ``m_time`` (and the value)."""</span>
<span class="bp">self</span><span class="o">.</span><span class="n">DB</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">SQL_SET</span><span class="p">,</span> <span class="p">(</span><span class="n">name</span><span class="p">,</span> <span class="n">value</span><span class="p">))</span>
<span class="k">if</span> <span class="n">sys</span><span class="o">.</span><span class="n">version_info</span> <span class="o"><=</span> <span class="p">(</span><span class="mi">3</span><span class="p">,</span> <span class="mi">12</span><span class="p">):</span>
<span class="c1"># Prior Python 3.12 there is no "autocommit" option / lets commit</span>
<span class="c1"># explicitely.</span>
<span class="bp">self</span><span class="o">.</span><span class="n">DB</span><span class="o">.</span><span class="n">commit</span><span class="p">()</span></div>
<div class="viewcode-block" id="SQLiteProperties.row">
<a class="viewcode-back" href="../../src/searx.sqlitedb.html#searx.sqlitedb.SQLiteProperties.row">[docs]</a>
<span class="k">def</span> <span class="nf">row</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">name</span><span class="p">,</span> <span class="n">default</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
<span class="w"> </span><span class="sd">"""Returns the DB row of property ``name`` or ``default`` if property</span>
<span class="sd"> not exists in DB."""</span>
<span class="n">cur</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">DB</span><span class="o">.</span><span class="n">cursor</span><span class="p">()</span>
<span class="n">cur</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="s2">"SELECT * FROM properties WHERE name = ?"</span><span class="p">,</span> <span class="p">(</span><span class="n">name</span><span class="p">,))</span>
<span class="n">res</span> <span class="o">=</span> <span class="n">cur</span><span class="o">.</span><span class="n">fetchone</span><span class="p">()</span>
<span class="k">if</span> <span class="n">res</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
<span class="k">return</span> <span class="n">default</span>
<span class="n">col_names</span> <span class="o">=</span> <span class="p">[</span><span class="n">column</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="k">for</span> <span class="n">column</span> <span class="ow">in</span> <span class="n">cur</span><span class="o">.</span><span class="n">description</span><span class="p">]</span>
<span class="k">return</span> <span class="nb">dict</span><span class="p">(</span><span class="nb">zip</span><span class="p">(</span><span class="n">col_names</span><span class="p">,</span> <span class="n">res</span><span class="p">))</span></div>
<div class="viewcode-block" id="SQLiteProperties.m_time">
<a class="viewcode-back" href="../../src/searx.sqlitedb.html#searx.sqlitedb.SQLiteProperties.m_time">[docs]</a>
<span class="k">def</span> <span class="nf">m_time</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">name</span><span class="p">,</span> <span class="n">default</span><span class="p">:</span> <span class="nb">int</span> <span class="o">=</span> <span class="mi">0</span><span class="p">)</span> <span class="o">-></span> <span class="nb">int</span><span class="p">:</span>
<span class="w"> </span><span class="sd">"""Last modification time of this property."""</span>
<span class="n">res</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">DB</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">SQL_M_TIME</span><span class="p">,</span> <span class="p">(</span><span class="n">name</span><span class="p">,))</span><span class="o">.</span><span class="n">fetchone</span><span class="p">()</span>
<span class="k">if</span> <span class="n">res</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
<span class="k">return</span> <span class="n">default</span>
<span class="k">return</span> <span class="nb">int</span><span class="p">(</span><span class="n">res</span><span class="p">[</span><span class="mi">0</span><span class="p">])</span></div>
<span class="k">def</span> <span class="nf">create_schema</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">conn</span><span class="p">):</span>
<span class="k">with</span> <span class="n">conn</span><span class="p">:</span>
<span class="n">conn</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">DDL_PROPERTIES</span><span class="p">)</span></div>
</pre></div>
<div class="clearer"></div>
</div>
</div>
</div>
<span id="sidebar-top"></span>
<div class="sphinxsidebar" role="navigation" aria-label="Main">
<div class="sphinxsidebarwrapper">
<p class="logo"><a href="../../index.html">
<img class="logo" src="../../_static/searxng-wordmark.svg" alt="Logo of SearXNG"/>
</a></p>
<h3><a href="../../index.html">Table of Contents</a></h3>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../user/index.html">User information</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../own-instance.html">Why use a private instance?</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../admin/index.html">Administrator documentation</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../dev/index.html">Developer documentation</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../utils/index.html">DevOps tooling box</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../src/index.html">Source-Code</a></li>
</ul>
<h3>Project Links</h3>
<ul>
<li><a href="https://github.com/searxng/searxng/tree/master">Source</a>
<li><a href="https://github.com/searxng/searxng/wiki">Wiki</a>
<li><a href="https://searx.space">Public instances</a>
<li><a href="https://github.com/searxng/searxng/issues">Issue Tracker</a>
</ul><h3>Navigation</h3>
<ul>
<li><a href="../../index.html">Overview</a>
<ul>
<li><a href="../index.html">Module code</a>
</ul>
</li>
</ul>
</li>
</ul>
<search id="searchbox" style="display: none" role="search">
<h3 id="searchlabel">Quick search</h3>
<div class="searchformwrapper">
<form class="search" action="../../search.html" method="get">
<input type="text" name="q" aria-labelledby="searchlabel" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false"/>
<input type="submit" value="Go" />
</form>
</div>
</search>
<script>document.getElementById('searchbox').style.display = "block"</script>
</div>
</div>
<div class="clearer"></div>
</div>
<div class="footer" role="contentinfo">
© Copyright SearXNG team.
</div>
</body>
</html>
|