maestral.utils.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.
- class maestral.utils.orm.SqlType[source]
Bases:
object
Base class to represent Python types in SQLite table
- sql_type = 'TEXT'
- class maestral.utils.orm.SqlString[source]
Bases:
maestral.utils.orm.SqlType
Class to represent Python strings in SQLite table
- sql_type = 'TEXT'
- class maestral.utils.orm.SqlInt[source]
Bases:
maestral.utils.orm.SqlType
Class to represent Python integers in SQLite table
- sql_type = 'INTEGER'
- class maestral.utils.orm.SqlFloat[source]
Bases:
maestral.utils.orm.SqlType
Class to represent Python floats in SQLite table
- sql_type = 'REAL'
- class maestral.utils.orm.SqlPath[source]
Bases:
maestral.utils.orm.SqlType
Class to represent Python paths in SQLite table
This class contains special handling for strings with surrogate escape characters which can appear in badly encoded file names.
- sql_type = 'TEXT'
- class maestral.utils.orm.SqlEnum(enum)[source]
Bases:
maestral.utils.orm.SqlType
Class to represent Python enums in SQLite table
- sql_type = 'TEXT'
- class maestral.utils.orm.Column(type, nullable=True, unique=False, primary_key=False, index=False, default=None)[source]
Bases:
property
Represents a column in a database table.
- Parameters
type (maestral.utils.orm.SqlType) – 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 (Union[str, int, float, enum.Enum, None, Type[NoDefault]]) – 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.
- render_constraints()[source]
Returns a string with constraints for the SQLite column definition.
- Return type
- render_properties()[source]
Returns a string with properties for the SQLite column definition.
- Return type
- class maestral.utils.orm.NoDefault[source]
Bases:
object
Class to denote the absence of a default value.
This is distinct from
None
which may be a valid default.
- class maestral.utils.orm.Database(*args, **kwargs)[source]
Bases:
object
Proxy class to access sqlite3.connect method.
- Return type
None
- property connection: sqlite3.Connection
Returns an existing SQL connection or creates a new one.
- class maestral.utils.orm.Manager(db, model)[source]
Bases:
object
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.utils.orm.Database) – Database to use.
- Return type
None
- all()[source]
Get all model objects / rows from database in a single query.
- Returns
List of model objects.
- Return type
- iter_all(size=1000)[source]
Get all model objects / rows from database in multiple queries.
- Parameters
size (int) – Number of rows to fetch in each query.
- Returns
Iterator over lists of model objects.
- Return type
Generator[List[maestral.utils.orm.Model], Any, None]
- create(**kwargs)[source]
Create a model object from SQL column values
- Parameters
kwargs – Column values.
- Returns
Model object.
- Return type
- delete(obj)[source]
Delete a model object / row from database.
- Parameters
obj (maestral.utils.orm.Model) – Object / row to delete.
- Return type
None
- get(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.
- save(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 (maestral.utils.orm.Model) – Model object to save.
- Returns
Saved model object.
- Return type
- update(obj)[source]
Updates the database table from a model object.
- Parameters
obj (maestral.utils.orm.Model) – The object to update.
- Return type
None
- class maestral.utils.orm.Model(**kwargs)[source]
Bases:
object
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 this Model and define
Column`s as class properties. Override the ``__tablename__`
attribute with the actual table name.Initialise with keyword arguments corresponding to column names and values.
- Parameters
kwargs – Keyword arguments assigning values to table columns.
- Return type
None