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

Constructor & Destructor Documentation

◆ __init__()

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

Definition at line 356 of file sqlitedb.py.

356 def __init__(self, db_url: str): # pylint: disable=super-init-not-called
357
358 self.db_url = db_url
359 self._init_done = False
360 self._compatibility()
361

Member Function Documentation

◆ __call__()

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

Definition at line 374 of file sqlitedb.py.

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

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

◆ __str__()

str searx.sqlitedb.SQLiteProperties.__str__ ( self)

Definition at line 420 of file sqlitedb.py.

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

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

◆ create_schema()

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

Reimplemented from searx.sqlitedb.SQLiteAppl.

Definition at line 416 of file sqlitedb.py.

416 def create_schema(self, conn):
417 with conn:
418 conn.execute(self.DDL_PROPERTIES)
419

References DDL_PROPERTIES.

◆ delete()

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

Definition at line 390 of file sqlitedb.py.

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

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

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

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

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

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

◆ row()

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

Definition at line 396 of file sqlitedb.py.

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

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

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

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 333 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 349 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 342 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 343 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 344 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 350 of file sqlitedb.py.

Referenced by init().


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