aequilibrae.utils.db_utils#

Functions

add_column(conn, table_name, col_name, col_type)

add_column_unless_exists(conn, table_name, ...)

get_schema(conn, table_name)

has_column(conn, table_name, col_name)

has_table(conn, table_name)

list_columns(conn, table_name)

list_tables_in_db(conn)

read_and_close(filepath[, spatial])

A context manager for sqlite connections (alias for commit_and_close(db,commit=False)).

read_sql(sql, filepath, **kwargs)

safe_connect(filepath[, missing_ok])

Classes

AequilibraEConnection(*args, **kwargs)

This custom factory class intends to solve the issue of premature commits when trying to use manual transaction control.

ColumnDef(idx, name, type, not_null, ...)

commit_and_close(db[, commit, missing_ok, ...])

A context manager for sqlite connections which closes and commits.

class aequilibrae.utils.db_utils.AequilibraEConnection(*args, **kwargs)[source]#

This custom factory class intends to solve the issue of premature commits when trying to use manual transaction control.

After manual_transaction is called, context manager enters and exits are tracked via their depth, the sqlite3.Connection is placed into manual transaction control and a transaction is started. If another transaction is already in progress an RuntimeError is raised. When exiting with depth == 0, the normal context manager enter and exit is called.

backup(target, *, pages=-1, progress=None, name='main', sleep=0.25)#

Makes a backup of the database.

blobopen(table, column, row, /, *, readonly=False, name='main')#

Open and return a BLOB object.

table

Table name.

column

Column name.

row

Row index.

readonly

Open the BLOB without write permissions.

name

Database name.

close()#

Close the database connection.

Any pending transaction is not committed implicitly.

commit()#

Commit any pending transaction to the database.

If there is no open transaction, this method is a no-op.

create_aggregate(name, n_arg, aggregate_class)#

Creates a new aggregate.

create_collation(name, callback, /)#

Creates a collation function.

create_function(name, narg, func, *, deterministic=False)#

Creates a new function.

create_window_function(name, num_params, aggregate_class, /)#

Creates or redefines an aggregate window function. Non-standard.

name

The name of the SQL aggregate window function to be created or redefined.

num_params

The number of arguments the step and inverse methods takes.

aggregate_class

A class with step(), finalize(), value(), and inverse() methods. Set to None to clear the window function.

cursor()#

Return a cursor for the connection.

deserialize(data, /, *, name='main')#

Load a serialized database.

data

The serialized database content.

name

Which database to reopen with the deserialization.

The deserialize interface causes the database connection to disconnect from the target database, and then reopen it as an in-memory database based on the given serialized data.

The deserialize interface will fail with SQLITE_BUSY if the database is currently in a read transaction or is involved in a backup operation.

enable_load_extension(enable, /)#

Enable dynamic loading of SQLite extension modules.

execute()#

Executes an SQL statement.

executemany(sql, parameters, /)#

Repeatedly executes an SQL statement.

executescript(sql_script, /)#

Executes multiple SQL statements at once.

getlimit(category, /)#

Get connection run-time limits.

category

The limit category to be queried.

interrupt()#

Abort any pending database operation.

iterdump()#

Returns iterator to the dump of the database in an SQL text format.

load_extension(name, /)#

Load SQLite extension module.

manual_transaction()[source]#
rollback()#

Roll back to the start of any pending transaction.

If there is no open transaction, this method is a no-op.

serialize(*, name='main')#

Serialize a database into a byte string.

name

Which database to serialize.

For an ordinary on-disk database file, the serialization is just a copy of the disk file. For an in-memory database or a “temp” database, the serialization is the same sequence of bytes which would be written to disk if that database were backed up to disk.

set_authorizer(authorizer_callback)#

Sets authorizer callback.

set_progress_handler(progress_handler, n)#

Sets progress handler callback.

set_trace_callback(trace_callback)#

Sets a trace callback called for each SQL statement (passed as unicode).

setlimit(category, limit, /)#

Set connection run-time limits.

category

The limit category to be set.

limit

The new limit. If the new limit is a negative number, the limit is unchanged.

Attempts to increase a limit above its hard upper bound are silently truncated to the hard upper bound. Regardless of whether or not the limit was changed, the prior value of the limit is returned.

DataError#
DatabaseError#
Error#
IntegrityError#
InterfaceError#
InternalError#
NotSupportedError#
OperationalError#
ProgrammingError#
Warning#
in_transaction#
isolation_level#
row_factory#
text_factory#
total_changes#
class aequilibrae.utils.db_utils.ColumnDef(idx: int, name: str, type: str, not_null: bool, default: str, is_pk: bool)[source]#
default: str#
idx: int#
is_pk: bool#
name: str#
not_null: bool#
type: str#
class aequilibrae.utils.db_utils.commit_and_close(db: str | Path | Connection, commit: bool = True, missing_ok: bool = False, spatial=False)[source]#

A context manager for sqlite connections which closes and commits.

aequilibrae.utils.db_utils.add_column(conn, table_name, col_name, col_type, constraints=None)[source]#
aequilibrae.utils.db_utils.add_column_unless_exists(conn, table_name, col_name, col_type, constraints=None)[source]#
aequilibrae.utils.db_utils.get_schema(conn, table_name)[source]#
aequilibrae.utils.db_utils.has_column(conn, table_name, col_name)[source]#
aequilibrae.utils.db_utils.has_table(conn, table_name)[source]#
aequilibrae.utils.db_utils.list_columns(conn, table_name)[source]#
aequilibrae.utils.db_utils.list_tables_in_db(conn: Connection)[source]#
aequilibrae.utils.db_utils.read_and_close(filepath, spatial=False)[source]#

A context manager for sqlite connections (alias for commit_and_close(db,commit=False)).

aequilibrae.utils.db_utils.read_sql(sql, filepath, **kwargs)[source]#
aequilibrae.utils.db_utils.safe_connect(filepath: PathLike, missing_ok=False)[source]#