latest.ipv8.database

This module provides basic database functionalty and simple version control.

@author: Boudewijn Schoon @organization: Technical University Delft @contact: dispersy@frayja.com

Module Contents

Classes

Database

Wrapper for SQLite 3 calls.

Functions

db_call(→ Callable[Ellipsis, Any | None])

Wait for the database lock before calling a function and return None if the cursor could not be acquired.

_thread_safe_result_it(→ Iterator[DB_TYPES])

Attributes

DB_TYPES

db_locks

latest.ipv8.database.DB_TYPES
latest.ipv8.database.db_locks: dict[str, threading.RLock]
latest.ipv8.database.db_call(f: Callable[Ellipsis, Any]) Callable[Ellipsis, Any | None]

Wait for the database lock before calling a function and return None if the cursor could not be acquired.

latest.ipv8.database._thread_safe_result_it(result: sqlite3.Cursor, fetch_all: bool = True) Iterator[DB_TYPES]
exception latest.ipv8.database.IgnoreCommits

Bases: Exception

Ignore all commits made within the body of a ‘with database:’ clause.

with database:

# all commit statements are delayed until the database.__exit__ database.commit() database.commit() # raising IgnoreCommits causes all commits to be ignored raise IgnoreCommits()

exception latest.ipv8.database.DatabaseException

Bases: RuntimeError

Exception for database integrity violations.

class latest.ipv8.database.Database(file_path: str)

Wrapper for SQLite 3 calls.

property database_version: int

The current (expected) version of the database.

property file_path: str

The database filename including path.

_assert(condition: bool, message: str = '') None

Check if condition is True, or raise a DatabaseException with a message.

open(initial_statements: bool = True, prepare_visioning: bool = True) bool

Open a connection to the underlying database file.

close(commit: bool = True) bool

Close the connection to the database.

_connect() None
_initial_statements() None
_prepare_version() None
__enter__() typing_extensions.Self

Enters a no-commit state. The commit will be performed by __exit__.

@return: The method self.execute

__exit__(exc_type: type[BaseException] | None, exc_value: BaseException | None, traceback: types.TracebackType | None) bool

Leaves a no-commit state. A commit will be performed if Database.commit() was called while in the no-commit state.

execute(statement: str, bindings: _typeshed.SupportsLenAndGetItem | Mapping[str, Any] = (), get_lastrowid: bool = False, fetch_all: bool = True) int | Iterator[DB_TYPES] | Iterator[list[DB_TYPES]] | None

Execute one SQL statement.

A SQL query must be presented in unicode format. This is to ensure that no unicode exeptions occur when the bindings are merged into the statement.

Furthermore, the bindings may not contain any strings either. For a ‘string’ the unicode type must be used. For a binary string the buffer(…) type must be used.

The SQL query may contain placeholder entries defined with a ‘?’. Each of these placeholders will be used to store one value from bindings. The placeholders are filled by sqlite and all proper escaping is done, making this the preferred way of adding variables to the SQL query.

@param statement: the SQL statement that is to be executed. @type statement: unicode

@param bindings: the values that must be set to the placeholders in statement. @type bindings: list, tuple, dict, or set

@returns: unknown @raise sqlite.Error: unknown

executescript(statements: str, fetch_all: bool = True) Iterator[DB_TYPES]

Execute multiple SQL statements at once.

executemany(statement: str, sequenceofbindings: Iterable[_typeshed.SupportsLenAndGetItem | Mapping[str, Any]], fetch_all: bool = True) Iterator[DB_TYPES]

Execute one SQL statement several times.

All SQL queries must be presented in unicode format. This is to ensure that no unicode exeptions occur when the bindings are merged into the statement.

Furthermore, the bindings may not contain any strings either. For a ‘string’ the unicode type must be used. For a binary string the buffer(…) type must be used.

The SQL query may contain placeholder entries defined with a ‘?’. Each of these placeholders will be used to store one value from bindings. The placeholders are filled by sqlite and all proper escaping is done, making this the preferred way of adding variables to the SQL query.

@param statement: the SQL statement that is to be executed. @type statement: unicode

@param sequenceofbindings: a list, tuple, set, or generator of bindings, where every binding

contains the values that must be set to the placeholders in statement.

@type sequenceofbindings: list, tuple, set or generator

@returns: unknown @raise sqlite.Error: unknown

commit(exiting: bool = False) bool

Attempt to commit the current transaction and return False when the commit needs to wait for pending commits.

abstract check_database(database_version: bytes) int

Check the database and upgrade if required.

This method is called once for each Database instance to ensure that the database structure and version is correct. Each Database must contain one table of the structure below where the database_version is stored. This value is used to keep track of the current database version.

>>> CREATE TABLE option(key TEXT PRIMARY KEY, value BLOB);
>>> INSERT INTO option(key, value) VALUES('database_version', '1');
@param database_version: the current database_version value from the option table. This

value reverts to u’0’ when the table could not be accessed.

@type database_version: unicode