.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)
bool init (self, sqlite3.Connection conn)
t.Any __call__ (self, str name, t.Any default=None)
 set (self, str name, str|int value)
int delete (self, str name)
 row (self, str name, t.Any default=None)
int m_time (self, str name, int default=0)
 create_schema (self, sqlite3.Connection conn)
str __str__ (self)
Public Member Functions inherited from searx.sqlitedb.SQLiteAppl
 __init__ (self, str db_url)
sqlite3.Connection connect (self)
 register_functions (self, sqlite3.Connection conn)
sqlite3.Connection DB (self)

Static Public Attributes

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
str SQL_DELETE = "DELETE FROM properties WHERE name = ?"
tuple SQL_TABLE_EXISTS
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

Additional Inherited Members

Public Attributes inherited from searx.sqlitedb.SQLiteAppl
str db_url = db_url
SQLiteProperties properties = SQLiteProperties(db_url)
Protected Member Functions inherited from searx.sqlitedb.SQLiteAppl
 _compatibility (self)
sqlite3.Connection _connect (self)
Protected Attributes inherited from searx.sqlitedb.SQLiteAppl
bool _init_done = False
sqlite3.Connection|None _DB = None

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 319 of file sqlitedb.py.

Constructor & Destructor Documentation

◆ __init__()

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

Definition at line 359 of file sqlitedb.py.

359 def __init__(self, db_url: str): # pyright: ignore[reportMissingSuperCall]
360
361 self.db_url: str = db_url
362 self._init_done: bool = False
363 self._compatibility()
364

Member Function Documentation

◆ __call__()

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

Definition at line 377 of file sqlitedb.py.

377 def __call__(self, name: str, default: t.Any = None) -> t.Any:
378 """Returns the value of the property ``name`` or ``default`` if property
379 not exists in DB."""
380
381 res = self.DB.execute(self.SQL_GET, (name,)).fetchone()
382 if res is None:
383 return default
384 return res[0]
385

References searx.cache.ExpireCacheSQLite.DB, DB, and SQL_GET.

◆ __str__()

str searx.sqlitedb.SQLiteProperties.__str__ ( self)

Definition at line 423 of file sqlitedb.py.

423 def __str__(self) -> str:
424 lines: list[str] = []
425 for row in self.DB.execute("SELECT name, value, m_time FROM properties"):
426 name, value, m_time = row
427 m_time = datetime.datetime.fromtimestamp(m_time).strftime("%Y-%m-%d %H:%M:%S")
428 lines.append(f"[last modified: {m_time}] {name:20s}: {value}")
429 return "\n".join(lines)

References searx.cache.ExpireCacheSQLite.DB, and DB.

◆ create_schema()

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

Reimplemented from searx.sqlitedb.SQLiteAppl.

Definition at line 419 of file sqlitedb.py.

419 def create_schema(self, conn: sqlite3.Connection):
420 with conn:
421 conn.execute(self.DDL_PROPERTIES)
422

References DDL_PROPERTIES.

◆ delete()

int searx.sqlitedb.SQLiteProperties.delete ( self,
str name )
Delete of property ``name`` from DB.

Definition at line 393 of file sqlitedb.py.

393 def delete(self, name: str) -> int:
394 """Delete of property ``name`` from DB."""
395 with self.DB:
396 cur = self.DB.execute(self.SQL_DELETE, (name,))
397 return cur.rowcount
398

References searx.cache.ExpireCacheSQLite.DB, DB, and SQL_DELETE.

◆ init()

bool searx.sqlitedb.SQLiteProperties.init ( self,
sqlite3.Connection conn )
Initializes DB schema of the properties in the DB.

Reimplemented from searx.sqlitedb.SQLiteAppl.

Definition at line 365 of file sqlitedb.py.

365 def init(self, conn: sqlite3.Connection) -> bool:
366 """Initializes DB schema of the properties in the DB."""
367
368 if self._init_done:
369 return False
370 self._init_done = True
371 logger.debug("init properties of DB: %s", self.db_url)
372 res = conn.execute(self.SQL_TABLE_EXISTS)
373 if res.fetchone() is None: # DB schema needs to be be created
374 self.create_schema(conn)
375 return True
376

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

Here is the call graph for this function:

◆ m_time()

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

Definition at line 411 of file sqlitedb.py.

411 def m_time(self, name: str, default: int = 0) -> int:
412 """Last modification time of this property."""
413 res = self.DB.execute(self.SQL_M_TIME, (name,))
414 row = res.fetchone()
415 if row is None:
416 return default
417 return int(row[0])
418

References searx.cache.ExpireCacheSQLite.DB, DB, and SQL_M_TIME.

◆ row()

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

Definition at line 399 of file sqlitedb.py.

399 def row(self, name: str, default: t.Any = None):
400 """Returns the DB row of property ``name`` or ``default`` if property
401 not exists in DB."""
402
403 res = self.DB.execute("SELECT * FROM properties WHERE name = ?", (name,))
404 row = res.fetchone()
405 if row is None:
406 return default
407
408 col_names = [column[0] for column in row.description]
409 return dict(zip(col_names, row))
410

References searx.cache.ExpireCacheSQLite.DB, and DB.

◆ set()

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

Definition at line 386 of file sqlitedb.py.

386 def set(self, name: str, value: str | int):
387 """Set ``value`` of property ``name`` in DB. If property already
388 exists, update the ``m_time`` (and the value)."""
389
390 with self.DB:
391 self.DB.execute(self.SQL_SET, (name, value))
392

Member Data Documentation

◆ 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 335 of file sqlitedb.py.

Referenced by create_schema().

◆ SQL_DELETE

str searx.sqlitedb.SQLiteProperties.SQL_DELETE = "DELETE FROM properties WHERE name = ?"
static

Definition at line 351 of file sqlitedb.py.

Referenced by delete().

◆ SQL_GET

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

Definition at line 344 of file sqlitedb.py.

Referenced by __call__().

◆ SQL_M_TIME

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

Definition at line 345 of file sqlitedb.py.

Referenced by 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 346 of file sqlitedb.py.

◆ 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 352 of file sqlitedb.py.

Referenced by init().


The documentation for this class was generated from the following file:
  • /home/andrew/Documents/code/public/searxng/searx/sqlitedb.py