API Reference

datalite3 Module

@datalite3.datalite(db: Union[str, sqlite3.Connection], table_name: Optional[str] = None, *, auto_commit: bool = False, type_overload: Optional[Dict[type, datalite3.commons.SQLType]] = None) → Type[T]

Bind a dataclass to a sqlite3 database. This adds new methods to the class, such as create_entry(), remove_entry() and update_entry().

Parameters
  • db – Path of the database to be binded.

  • table_name – Optional name for the table. The name of the class will be used by default.

  • auto_commit – Enable auto-commit.

  • type_overload – Type overload dictionary.

Returns

The new dataclass.

datalite3.constraints module

datalite3.constraints module introduces constraint

types that can be used to hint field variables, that can be used to signal datalite decorator constraints in the database.

exception datalite3.constraints.ConstraintFailedError

Bases: Exception

This exception is raised when a Constraint fails.

datalite3.constraints.Unique
Dataclass fields hinted with this type signals

datalite that the bound column of this field in the table is part of the PRIMARY KEY.

alias of Union[T, Tuple[T]]

datalite3.fetch module

datalite3.fetch.fetch_all(class_: type, page: int = 0, element_count: int = 10) → tuple

Fetchall the records in the bound database.

Parameters
  • class – Class of the records.

  • page – Which page to retrieve, default all. (0 means closed).

  • element_count – Element count in each page.

Returns

All the records of type class_ in the bound database as a tuple.

datalite3.fetch.fetch_equals(class_: type, field: str, value: Any) → Any

Fetch a class_ type variable from its bound db.

Parameters
  • class – Class to fetch.

  • field – Field to check for, by default, object id.

  • value – Value of the field to check for.

Returns

The object whose data is taken from the database.

datalite3.fetch.fetch_from(class_: type, key: Union[None, int, float, str, bytes, Tuple[Union[None, int, float, str, bytes]]]) → Any

Fetch a class_ type variable from its bound dv.

Parameters
  • class – Class to fetch from.

  • key – Unique key of the object.

Returns

The fetched object.

datalite3.fetch.fetch_if(class_: type, condition: str, page: int = 0, element_count: int = 10) → tuple

Fetch all class_ type variables from the bound db, provided they fit the given condition

Parameters
  • class – Class type to fetch.

  • condition – Condition to check for.

  • page – Which page to retrieve, default all. (0 means closed).

  • element_count – Element count in each page.

Returns

A tuple of records that fit the given condition of given type class_.

datalite3.fetch.fetch_where(class_: type, field: str, value: Any, page: int = 0, element_count: int = 10) → tuple

Fetch all class_ type variables from the bound db, provided that the field of the records fit the given value.

Parameters
  • class – Class of the records.

  • field – Field to check.

  • value – Value to check for.

  • page – Which page to retrieve, default all. (0 means closed).

  • element_count – Element count in each page.

Returns

A tuple of the records.

datalite3.fetch.is_fetchable(class_: type, key: Union[None, int, float, str, bytes, Tuple[Union[None, int, float, str, bytes]]]) → bool

Check if a record is fetchable given its key and class_ type.

Parameters
  • class – Class type of the object.

  • key – Unique key of the object.

Returns

If the object is fetchable.

datalite3.mass_actions module

This module includes functions to insert multiple records to a bound database at one time, with one time open and closing of the database file.

exception datalite3.mass_actions.HeterogeneousCollectionError

Bases: Exception

:raiseif the passed collection is not homogeneous.

ie: If a List or Tuple has elements of multiple types.

datalite3.mass_actions.create_many(objects: Union[List[T], Tuple[T]]) → None

Insert many records corresponding to objects in a tuple or a list.

Parameters

objects – A tuple or a list of objects decorated with datalite.

Returns

None.

datalite3.migrations module

Migrations module deals with migrating data when the object definitions change. This functions deal with Schema Migrations.

datalite3.migrations.basic_migrate(class_: Type[dataclasses.dataclass], column_transfer: dict = None) → None

Given a class, compare its previous table, delete the fields that no longer exist, create new columns for new fields. If the column_flow parameter is given, migrate elements from previous column to the new ones.

Parameters
  • class – Datalite class to migrate.

  • column_transfer – A dictionary showing which columns will be copied to new ones.

Returns

None.