krrood.ormatic.field_storage#

Decides how ORMatic stores each field of a mapped class.

Every way of storing a field is a FieldStorage member, and one FieldStorageRule per member states the complete condition under which it applies. The rules form a SpecificityRule family: their conditions exclude each other, so no rule relies on the order in which the rules are checked, and a field claimed by two rules raises AmbiguousRuleError.

Attributes#

Classes#

FieldStorage

The ways ORMatic can store a field.

FieldToStore

A field of a mapped class, together with the ORMatic instance whose mapped classes and

FieldStorageRule

The condition under which a field is stored in one particular way.

NotStoredRule

Fields whose type ORMatic has no way to store.

TypeRule

Fields that hold classes.

BuiltinRule

Single values of a builtin that SQLAlchemy maps by itself.

ManyToOneRule

Single references to an instance of a mapped class.

ManyToManyRule

Collections of instances of a mapped class.

CustomTypeRule

Single values of a type that the type mappings give a column type for.

JSONRule

Values that krrood's JSON serializer writes: collections of builtins, of classes or

FieldClassifier

Decides how each field is stored by asking the FieldStorageRule family.

Module Contents#

krrood.ormatic.field_storage.logger#
class krrood.ormatic.field_storage.FieldStorage(*args, **kwds)#

Bases: enum.Enum

The ways ORMatic can store a field.

NOT_STORED#

The field gets no column.

TYPE#

A column holding a class, for fields annotated with type or Type[...].

BUILTIN#

A column whose type SQLAlchemy derives from the annotation by itself.

MANY_TO_ONE#

A foreign key to the table of a mapped class.

MANY_TO_MANY#

An association table to the table of a mapped class.

CUSTOM_TYPE#

A column of the type the type mappings give for the field’s type.

JSON#

A JSON column written by krrood’s JSON serializer.

class krrood.ormatic.field_storage.FieldToStore#

A field of a mapped class, together with the ORMatic instance whose mapped classes and type mappings decide how it is stored.

wrapped_field: krrood.class_diagrams.wrapped_field.WrappedField#

The field to decide on.

ormatic: krrood.ormatic.ormatic.ORMatic#

The ORMatic instance that stores the field.

sqlalchemy_builtins: ClassVar[tuple[type, ...]]#

The builtins SQLAlchemy maps to a column type by itself.

property type_endpoint: type#
Returns:

The type the field ultimately points to.

property holds_a_sqlalchemy_builtin: bool#
Returns:

True if the field’s type is a builtin SQLAlchemy maps by itself.

is_mapped(clazz: type) → bool#
Returns:

True if a table maps the class.

has_type_mapping(clazz: type) → bool#
Returns:

True if the type mappings give a column type for the class.

has_json_serializer(clazz: type) → bool#
Returns:

True if a JSON serializer is registered for the class hierarchy.

is_stored_as_a_value(clazz: type) → bool#

Whether values of the class are stored whole in their owner’s row, so a free type parameter of the class leaves nothing undecided about how to store them.

Returns:

True if no table maps the class and a type mapping or a JSON serializer stores it.

property cannot_be_stored: bool#

Whether the field’s type leaves ORMatic no way to store it: a dictionary, or a generic class with free type parameters that nothing in the class diagram could fill.

property holds_a_storable_value: bool#
Returns:

True if the field holds values rather than classes, and ORMatic can store them.

class krrood.ormatic.field_storage.FieldStorageRule#

Bases: krrood.patterns.specificity_ranking.SpecificityRule, abc.ABC

The condition under which a field is stored in one particular way.

storage: ClassVar[FieldStorage]#

The way of storing a field that this rule decides on.

classmethod applies(field_to_store: FieldToStore) → bool#
Abstractmethod:

Parameters:

field_to_store – The field to decide on.

Returns:

True if the field is stored the way this rule stands for.

classmethod describe(field_to_store: FieldToStore) → str#
Abstractmethod:

Parameters:

field_to_store – A field this rule applies to.

Returns:

A sentence for the log saying how the field is stored.

class krrood.ormatic.field_storage.NotStoredRule#

Bases: FieldStorageRule

Fields whose type ORMatic has no way to store.

storage#

The way of storing a field that this rule decides on.

classmethod applies(field_to_store: FieldToStore) → bool#
Parameters:

field_to_store – The field to decide on.

Returns:

True if the field is stored the way this rule stands for.

classmethod describe(field_to_store: FieldToStore) → str#
Parameters:

field_to_store – A field this rule applies to.

Returns:

A sentence for the log saying how the field is stored.

class krrood.ormatic.field_storage.TypeRule#

Bases: FieldStorageRule

Fields that hold classes.

storage#

The way of storing a field that this rule decides on.

classmethod applies(field_to_store: FieldToStore) → bool#
Parameters:

field_to_store – The field to decide on.

Returns:

True if the field is stored the way this rule stands for.

classmethod describe(field_to_store: FieldToStore) → str#
Parameters:

field_to_store – A field this rule applies to.

Returns:

A sentence for the log saying how the field is stored.

class krrood.ormatic.field_storage.BuiltinRule#

Bases: FieldStorageRule

Single values of a builtin that SQLAlchemy maps by itself.

storage#

The way of storing a field that this rule decides on.

classmethod applies(field_to_store: FieldToStore) → bool#
Parameters:

field_to_store – The field to decide on.

Returns:

True if the field is stored the way this rule stands for.

classmethod describe(field_to_store: FieldToStore) → str#
Parameters:

field_to_store – A field this rule applies to.

Returns:

A sentence for the log saying how the field is stored.

class krrood.ormatic.field_storage.ManyToOneRule#

Bases: FieldStorageRule

Single references to an instance of a mapped class.

storage#

The way of storing a field that this rule decides on.

classmethod applies(field_to_store: FieldToStore) → bool#
Parameters:

field_to_store – The field to decide on.

Returns:

True if the field is stored the way this rule stands for.

classmethod describe(field_to_store: FieldToStore) → str#
Parameters:

field_to_store – A field this rule applies to.

Returns:

A sentence for the log saying how the field is stored.

class krrood.ormatic.field_storage.ManyToManyRule#

Bases: FieldStorageRule

Collections of instances of a mapped class.

storage#

The way of storing a field that this rule decides on.

classmethod applies(field_to_store: FieldToStore) → bool#
Parameters:

field_to_store – The field to decide on.

Returns:

True if the field is stored the way this rule stands for.

classmethod describe(field_to_store: FieldToStore) → str#
Parameters:

field_to_store – A field this rule applies to.

Returns:

A sentence for the log saying how the field is stored.

class krrood.ormatic.field_storage.CustomTypeRule#

Bases: FieldStorageRule

Single values of a type that the type mappings give a column type for.

storage#

The way of storing a field that this rule decides on.

classmethod applies(field_to_store: FieldToStore) → bool#
Parameters:

field_to_store – The field to decide on.

Returns:

True if the field is stored the way this rule stands for.

classmethod describe(field_to_store: FieldToStore) → str#
Parameters:

field_to_store – A field this rule applies to.

Returns:

A sentence for the log saying how the field is stored.

class krrood.ormatic.field_storage.JSONRule#

Bases: FieldStorageRule

Values that krrood’s JSON serializer writes: collections of builtins, of classes or of mapped types, and values or collections of a type with a JSON serializer of its own.

storage#

The way of storing a field that this rule decides on.

classmethod applies(field_to_store: FieldToStore) → bool#
Parameters:

field_to_store – The field to decide on.

Returns:

True if the field is stored the way this rule stands for.

classmethod describe(field_to_store: FieldToStore) → str#
Parameters:

field_to_store – A field this rule applies to.

Returns:

A sentence for the log saying how the field is stored.

class krrood.ormatic.field_storage.FieldClassifier#

Decides how each field is stored by asking the FieldStorageRule family.

ormatic: krrood.ormatic.ormatic.ORMatic#

The ORMatic instance whose mapped classes and type mappings the rules consult.

classify(wrapped_field: krrood.class_diagrams.wrapped_field.WrappedField) → FieldStorage#
Parameters:

wrapped_field – The field to decide on.

Returns:

How the field is stored, or FieldStorage.NOT_STORED if no rule applies.

Raises:

AmbiguousRuleError – If more than one rule applies.