maestral.database.orm
A basic object relational mapper for SQLite.
This is a very simple ORM implementation which contains only functionality needed by Maestral. Many operations will still require explicit SQL statements. This module is no alternative to fully featured ORMs such as sqlalchemy but may be useful when system memory is constrained.
Module Contents
- class maestral.database.orm.NoDefault[source]
Class to denote the absence of a default value.
This is distinct from
None
which may be a valid default.
- class maestral.database.orm.Column(sql_type, nullable=True, unique=False, primary_key=False, index=False, default=None)[source]
Bases:
Generic
[T
]Represents a column in a database table.
- Parameters
type – Column type in database table. Python types which don’t have SQLite equivalents, such as
enum.Enum
, will be converted appropriately.nullable (bool) – When set to
False
, will cause the “NOT NULL” phrase to be added when generating the column.unique (bool) – If
True
, sets a unique constraint on the column.primary_key (bool) – If
True
, marks this column as a primary key column. Currently, only a single primary key column is supported.index (bool) – If
True
, create an index on this column.default (T | type[NoDefault] | None) – Default value for the column. Set to
NoDefault
if no default value should be used. Note than None / NULL is a valid default for an SQLite column.sql_type (maestral.database.types.SqlType) –
- render_constraints(self)[source]
Returns a string with constraints for the SQLite column definition.
- Return type
- render_properties(self)[source]
Returns a string with properties for the SQLite column definition.
- Return type
- class maestral.database.orm.Manager(db, model)[source]
Bases:
Generic
[M
]A data mapper interface for a table model.
Creates the table as defined in the model if it doesn’t already exist. Keeps a cache of weak references to all retrieved and created rows to speed up queries. The cache should be cleared manually changes where made to the table from outside this manager.
- Parameters
db (maestral.database.core.Database) – Database to use.
model (type[M]) – Model for database table.
- delete(self, query)[source]
- Parameters
query (maestral.database.query.Query) –
- Return type
None
- select(self, query)[source]
- Parameters
query (maestral.database.query.Query) –
- Return type
list[M]
- select_iter(self, query, size=1000)[source]
- Parameters
query (maestral.database.query.Query) –
size (int) –
- Return type
Generator[list[M], Any, None]
- select_sql(self, sql, *args)[source]
Performs the given SQL query and converts any returned rows to model objects.
- delete_primary_key(self, primary_key)[source]
Delete a model object / row from database by primary key.
- Parameters
primary_key (Any) – Primary key for row.
- Return type
None
- get(self, primary_key)[source]
Gets a model object from database by its primary key. This will return a cached value if available and None if no row with the primary key exists.
- Parameters
primary_key (Any) – Primary key for row.
- Returns
Model object representing the row.
- Return type
M | None
- has(self, primary_key)[source]
Checks if a model object exists in database by its primary key
- Parameters
primary_key (Any) – The primary key.
- Returns
Whether the corresponding row exists in the table.
- Return type
- save(self, obj)[source]
Saves a model object to the database table. If the primary key is None, a new primary key will be generated by SQLite on inserting the row. This key will be retrieved and stored in the primary key property of the object.
- Parameters
obj (M) – Model object to save.
- Returns
Saved model object.
- Return type
M
- class maestral.database.orm.Model(**kwargs)[source]
Abstract object model to represent an SQL table.
Instances of this class are model objects which correspond to rows in the database table.
To define a table, subclass
Model
and define class properties asColumn
. Override the__tablename__
attribute with the SQLite table name to use. The__columns__
attribute will be populated automatically for you.