.oO SearXNG Developer Documentation Oo.
Loading...
Searching...
No Matches
searx.sqlitedb.SQLiteProperties Class Reference
+ Inheritance diagram for searx.sqlitedb.SQLiteProperties:
+ Collaboration diagram for searx.sqlitedb.SQLiteProperties:

Public Member Functions

 __init__ (self, str db_url)
 
 init (self)
 
 __call__ (self, name, default=None)
 
 set (self, name, value)
 
 row (self, name, default=None)
 
int m_time (self, name, int default=0)
 
 create_schema (self, conn)
 
- Public Member Functions inherited from searx.sqlitedb.SQLiteAppl
sqlite3.Connection connect (self)
 
 register_functions (self, conn)
 
sqlite3.Connection DB (self)
 

Public Attributes

 db_url = db_url
 
 thread_local = threading.local()
 
- Public Attributes inherited from searx.sqlitedb.SQLiteAppl
 db_url = db_url
 
 properties = SQLiteProperties(db_url)
 
 thread_local = threading.local()
 

Static Public Attributes

str SQLITE_JOURNAL_MODE = "WAL"
 
str DDL_PROPERTIES
 
str SQL_GET = "SELECT value FROM properties WHERE name = ?"
 
str SQL_M_TIME = "SELECT m_time FROM properties WHERE name = ?"
 
tuple SQL_SET
 
tuple SQL_TABLE_EXISTS
 
 SQLITE_CONNECT_ARGS = dict(SQLiteAppl.SQLITE_CONNECT_ARGS)
 
- Static Public Attributes inherited from searx.sqlitedb.SQLiteAppl
dict DDL_CREATE_TABLES = {}
 
int DB_SCHEMA = 1
 
dict SQLITE_THREADING_MODE
 
str SQLITE_JOURNAL_MODE = "WAL"
 
dict SQLITE_CONNECT_ARGS
 

Protected Attributes

bool _init_done = False
 
- Protected Attributes inherited from searx.sqlitedb.SQLiteAppl
bool _init_done = False
 
 _DB = None
 

Additional Inherited Members

- Protected Member Functions inherited from searx.sqlitedb.SQLiteAppl
 _compatibility (self)
 

Detailed Description

Simple class to manage properties of a DB application in the DB.  The
object has its own DB connection and transaction area.

.. code:: sql

   CREATE TABLE IF NOT EXISTS properties (
     name       TEXT,
     value      TEXT,
     m_time     INTEGER DEFAULT (strftime('%s', 'now')),
     PRIMARY KEY (name))

Definition at line 224 of file sqlitedb.py.

Constructor & Destructor Documentation

◆ __init__()

searx.sqlitedb.SQLiteProperties.__init__ ( self,
str db_url )

Reimplemented from searx.sqlitedb.SQLiteAppl.

Definition at line 263 of file sqlitedb.py.

263 def __init__(self, db_url: str): # pylint: disable=super-init-not-called
264
265 self.db_url = db_url
266 self.thread_local = threading.local()
267 self._init_done = False
268 self._compatibility()
269

Member Function Documentation

◆ __call__()

searx.sqlitedb.SQLiteProperties.__call__ ( self,
name,
default = None )
Returns the value of the property ``name`` or ``default`` if property
not exists in DB.

Definition at line 282 of file sqlitedb.py.

282 def __call__(self, name, default=None):
283 """Returns the value of the property ``name`` or ``default`` if property
284 not exists in DB."""
285
286 res = self.DB.execute(self.SQL_GET, (name,)).fetchone()
287 if res is None:
288 return default
289 return res[0]
290

References searx.sqlitedb.SQLiteAppl.DB(), and searx.sqlitedb.SQLiteProperties.SQL_GET.

+ Here is the call graph for this function:

◆ create_schema()

searx.sqlitedb.SQLiteProperties.create_schema ( self,
conn )

Reimplemented from searx.sqlitedb.SQLiteAppl.

Definition at line 321 of file sqlitedb.py.

321 def create_schema(self, conn):
322 with conn:
323 conn.execute(self.DDL_PROPERTIES)

References searx.sqlitedb.SQLiteProperties.DDL_PROPERTIES.

Referenced by searx.sqlitedb.SQLiteAppl.init(), and searx.sqlitedb.SQLiteProperties.init().

+ Here is the caller graph for this function:

◆ init()

searx.sqlitedb.SQLiteProperties.init ( self)
Initializes DB schema of the properties in the DB.

Reimplemented from searx.sqlitedb.SQLiteAppl.

Definition at line 270 of file sqlitedb.py.

270 def init(self):
271 """Initializes DB schema of the properties in the DB."""
272
273 if self._init_done:
274 return
275 self._init_done = True
276 logger.debug("init properties of DB: %s", self.db_url)
277 with self.DB as conn:
278 res = conn.execute(self.SQL_TABLE_EXISTS)
279 if res.fetchone() is None: # DB schema needs to be be created
280 self.create_schema(conn)
281

References searx.sqlitedb.SQLiteAppl._init_done, searx.sqlitedb.SQLiteProperties._init_done, searx.sqlitedb.SQLiteAppl.create_schema(), searx.sqlitedb.SQLiteProperties.create_schema(), searx.sqlitedb.SQLiteAppl.DB(), searx.favicons.cache.FaviconCacheConfig.db_url, searx.sqlitedb.SQLiteAppl.db_url, searx.sqlitedb.SQLiteProperties.db_url, and searx.sqlitedb.SQLiteProperties.SQL_TABLE_EXISTS.

Referenced by searx.sqlitedb.SQLiteAppl.connect().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ m_time()

int searx.sqlitedb.SQLiteProperties.m_time ( self,
name,
int default = 0 )
Last modification time of this property.

Definition at line 314 of file sqlitedb.py.

314 def m_time(self, name, default: int = 0) -> int:
315 """Last modification time of this property."""
316 res = self.DB.execute(self.SQL_M_TIME, (name,)).fetchone()
317 if res is None:
318 return default
319 return int(res[0])
320

References searx.sqlitedb.SQLiteAppl.DB(), and searx.sqlitedb.SQLiteProperties.SQL_M_TIME.

+ Here is the call graph for this function:

◆ row()

searx.sqlitedb.SQLiteProperties.row ( self,
name,
default = None )
Returns the DB row of property ``name`` or ``default`` if property
not exists in DB.

Definition at line 302 of file sqlitedb.py.

302 def row(self, name, default=None):
303 """Returns the DB row of property ``name`` or ``default`` if property
304 not exists in DB."""
305
306 cur = self.DB.cursor()
307 cur.execute("SELECT * FROM properties WHERE name = ?", (name,))
308 res = cur.fetchone()
309 if res is None:
310 return default
311 col_names = [column[0] for column in cur.description]
312 return dict(zip(col_names, res))
313

References searx.sqlitedb.SQLiteAppl.DB().

+ Here is the call graph for this function:

◆ set()

searx.sqlitedb.SQLiteProperties.set ( self,
name,
value )
Set ``value`` of property ``name`` in DB.  If property already
exists, update the ``m_time`` (and the value).

Definition at line 291 of file sqlitedb.py.

291 def set(self, name, value):
292 """Set ``value`` of property ``name`` in DB. If property already
293 exists, update the ``m_time`` (and the value)."""
294
295 self.DB.execute(self.SQL_SET, (name, value))
296
297 if sys.version_info <= (3, 12):
298 # Prior Python 3.12 there is no "autocommit" option / lets commit
299 # explicitely.
300 self.DB.commit()
301

References searx.sqlitedb.SQLiteAppl.DB(), and searx.sqlitedb.SQLiteProperties.SQL_SET.

+ Here is the call graph for this function:

Member Data Documentation

◆ _init_done

bool searx.sqlitedb.SQLiteProperties._init_done = False
protected

◆ db_url

searx.sqlitedb.SQLiteProperties.db_url = db_url

◆ DDL_PROPERTIES

searx.sqlitedb.SQLiteProperties.DDL_PROPERTIES
static
Initial value:
= """\
CREATE TABLE IF NOT EXISTS properties (
name TEXT,
value TEXT,
m_time INTEGER DEFAULT (strftime('%s', 'now')), -- last modified (unix epoch) time in sec.
PRIMARY KEY (name))"""

Definition at line 240 of file sqlitedb.py.

Referenced by searx.sqlitedb.SQLiteProperties.create_schema().

◆ SQL_GET

str searx.sqlitedb.SQLiteProperties.SQL_GET = "SELECT value FROM properties WHERE name = ?"
static

Definition at line 249 of file sqlitedb.py.

Referenced by searx.sqlitedb.SQLiteProperties.__call__().

◆ SQL_M_TIME

str searx.sqlitedb.SQLiteProperties.SQL_M_TIME = "SELECT m_time FROM properties WHERE name = ?"
static

Definition at line 250 of file sqlitedb.py.

Referenced by searx.sqlitedb.SQLiteProperties.m_time().

◆ SQL_SET

searx.sqlitedb.SQLiteProperties.SQL_SET
static
Initial value:
= (
"INSERT INTO properties (name, value) VALUES (?, ?)"
" ON CONFLICT(name) DO UPDATE"
" SET value=excluded.value, m_time=strftime('%s', 'now')"
)

Definition at line 251 of file sqlitedb.py.

Referenced by searx.sqlitedb.SQLiteProperties.set().

◆ SQL_TABLE_EXISTS

tuple searx.sqlitedb.SQLiteProperties.SQL_TABLE_EXISTS
static
Initial value:
= (
"SELECT name FROM sqlite_master"
" WHERE type='table' AND name='properties'"
)

Definition at line 256 of file sqlitedb.py.

Referenced by searx.sqlitedb.SQLiteProperties.init().

◆ SQLITE_CONNECT_ARGS

searx.sqlitedb.SQLiteProperties.SQLITE_CONNECT_ARGS = dict(SQLiteAppl.SQLITE_CONNECT_ARGS)
static

Definition at line 260 of file sqlitedb.py.

Referenced by searx.sqlitedb.SQLiteAppl.connect().

◆ SQLITE_JOURNAL_MODE

str searx.sqlitedb.SQLiteProperties.SQLITE_JOURNAL_MODE = "WAL"
static

Definition at line 238 of file sqlitedb.py.

◆ thread_local

searx.sqlitedb.SQLiteProperties.thread_local = threading.local()

Definition at line 266 of file sqlitedb.py.

Referenced by searx.sqlitedb.SQLiteAppl.DB().


The documentation for this class was generated from the following file: