zut.db.Db

class zut.db.Db(connection_or_slug: T_Connection | str | None = None, *, slug: str | None = None, name: str | None = None, host: str | None = None, port: int | str | None = None, user: str | None = None, password: str | DelayedStr | None = None, password_required: bool | None = None, encrypt: bool | str | None = None, no_autocommit: bool | str | None = None, tz: Literal['local', 'utc'] | None = None, migrations_dir: str | os.PathLike | None = None)

Bases: Generic[T_Connection]

__init__(connection_or_slug: T_Connection | str | None = None, *, slug: str | None = None, name: str | None = None, host: str | None = None, port: int | str | None = None, user: str | None = None, password: str | DelayedStr | None = None, password_required: bool | None = None, encrypt: bool | str | None = None, no_autocommit: bool | str | None = None, tz: Literal['local', 'utc'] | None = None, migrations_dir: str | os.PathLike | None = None)
Parameters:

tz – If set, aware datetimes are written to the db as naive datetimes in the given timezone, and naive datetimes read from the db are considered as aware datetimes in the given timezone.

Methods

__init__([connection_or_slug, slug, name, ...])

add_column(table, columns, *[, ...])

Add column(s) to a table.

alter_column_default(table, columns, *[, ...])

build_connection_url()

check_port()

clear_table(table, *[, truncate, if_exists, ...])

close()

commit()

create_connection([autocommit])

create_database(name, *[, if_not_exists, ...])

create_schema(name, *[, if_not_exists, loglevel])

create_table(table, columns, *[, ...])

database_exists(name)

drop_column_default(table, columns, *[, ...])

drop_database(name, *[, if_exists, loglevel])

drop_schema(name, *[, if_exists, loglevel])

drop_table(table, *[, if_exists])

escape_identifier(value)

escape_literal(value)

execute(sql[, params, limit, offset, ...])

execute_file(file[, params, limit, offset, ...])

execute_file_result(file[, params, limit, ...])

execute_function(obj[, params, limit, ...])

execute_function_result(obj[, params, ...])

execute_procedure(obj[, params, limit, ...])

execute_procedure_result(obj[, params, ...])

execute_result(sql[, params, limit, offset, ...])

execute_script(script[, params, limit, ...])

execute_script_result(script[, params, ...])

get_columns(obj)

get_database_name()

Return the name of the database currently associated with this connection.

get_dict(sql[, params, limit, offset])

get_dicts(sql[, params, limit, offset])

get_foreign_keys(table, *[, columns, recurse])

Return the list of foreign keys defined for the given table.

get_headers(obj)

get_last_migration_name()

get_lastrowid(cursor)

The rowid of the last inserted row.

get_now_sql()

get_paginated_and_total_sql(sql, *, limit, ...)

get_paginated_dicts(sql[, params, offset])

Return (rows, total)

get_paginated_rows(sql[, params, offset])

Return (rows, total)

get_python_type(sql_type)

Get the Python type from a SQL type expressed either as a string, or as an integer (oid in type catalog, for postgresql and mariadb)

get_python_value(value)

get_random_table_name([prefix, schema, temp])

get_reversed_foreign_keys(columns, table, *)

get_row(sql[, params, limit, offset])

get_rows(sql[, params, limit, offset])

get_scalar()

get_scalars()

get_sql_type(python_type[, key])

get_sql_value(value)

Prepare a value so that it can be accepted as input by the database engine.

get_sqlutils_path()

get_unique_keys(table)

get_url(*[, hide_password, table])

iter_dicts(sql[, params, limit, offset])

iter_rows(sql[, params, limit, offset])

iter_scalars()

migrate(dir, *[, no_sqlutils])

parse_obj(input)

prepare_file_sql(file, *[, encoding])

prepare_function_sql(obj[, params, procedure])

prepare_sql(sql[, params, limit, offset])

rollback()

schema_exists(name)

single_dict(sql[, params, limit, offset])

single_row(sql[, params, limit, offset])

single_scalar()

table_exists(table)

transaction()

use_database(name)

Attributes

autocommit

bool_sql_type

connection

date_sql_type

datetime_sql_type

decimal_sql_type

dict_sql_type

float_sql_type

in_transaction

inserted_at_default_column

int_sql_type

list_sql_type

sql_type_catalog_by_id

sql_type_catalog_by_name

str_sql_type

updated_at_default_column

uuid_sql_type

varstr_sql_type_pattern

slug

name

host

port

user

password

password_required

encrypt

no_autocommit

tz

migrations_dir

scheme

default_port

strict_types

For sqlite.

add_column(table: str | tuple | type | DbObj, columns: str | Column | Sequence[str | Column], *, ignore_decimal=False, ignore_not_null=False, loglevel=10)

Add column(s) to a table.

NOTE: This method does not intend to manage all cases, but only those usefull for zut library internals.

get_database_name() str | None

Return the name of the database currently associated with this connection.

NOTE: - This can be distinct from this class instance attribute name a statement such as USE has been executed. - This can be None for mysql and mariadb.

get_foreign_keys(table: str | tuple | type | DbObj, *, columns: Iterable[str] | None = None, recurse=False)

Return the list of foreign keys defined for the given table.

Parameters:
  • table – Source of the foreign key relations.

  • columns – If set, restrict the foreign key searching to the given columns.

  • recurse – If True, check recursively if the related primary keys are themselves part of foreign keys (on the related models).

get_lastrowid(cursor: Cursor) int | None

The rowid of the last inserted row. None if no inserted rowid.

get_paginated_dicts(sql: str, params: Mapping[str, Any] | Sequence[Any] | None = None, *, limit: int, offset: int = 0) tuple[list[dict[str, Any]], int]

Return (rows, total)

get_paginated_rows(sql: str, params: Mapping[str, Any] | Sequence[Any] | None = None, *, limit: int, offset: int = 0) tuple[list[tuple], int]

Return (rows, total)

classmethod get_python_type(sql_type: str | int | type | Column | None) type | None

Get the Python type from a SQL type expressed either as a string, or as an integer (oid in type catalog, for postgresql and mariadb)

classmethod get_sql_type(python_type: str | int | type | Column | None, key: bool | float = False) str
Parameters:

key – indicate whether the column is part of a key (primary or unique). If this is a float, indicate the ratio of the max size of a key to use (for multi column keys).

get_sql_value(value: Any) Any

Prepare a value so that it can be accepted as input by the database engine.

strict_types: bool

For sqlite.