httk.store.db.schema¶
Schema IR: resolve a storable dataclass into the relational schema that drives storage.
A storable class is a plain frozen dataclass declared with the stdlib-only
marker vocabulary from httk-core (Indexed,
Unique, Skip,
IdentitySkip, Shape,
StorageInfo, stored_property).
resolve_schema() reads the class once — dataclass fields, Annotated
markers, stored properties, and the optional class-level or externally
registered StorageInfo — and produces a
TableSchema, the single source of truth from which the SQL layer
derives DDL, inserts, selects, and reconstruction alike.
Resolution rules (field annotation, then the resulting relational shape):
int/str/bool/bytes— one scalar column named after the field (boolis its own column kind, never folded intoint).floatuses a queryDOUBLEplus an exact*_exacthexadecimal text column so signed zero and every other finite binary64 value round-trip unchanged.X | None— the field is optional; all of its columns become nullable.a type with a registered
ValueCodec(fractions.Fraction,FracScalar,SurdScalar,datetime.datetime, …) — the codec’s columns, named by appending each suffix to the field name.Annotated[FracVector, Shape(r, c)]withr >= 1— a fixed-shape tensor stored inline:r*cfloat columnsname_0 .. name_{r*c-1}plus onename_exacttext column holding the canonical exact tensor text.Annotated[FracVector, Shape(0, c)]— variable rows in a child table<parent>_<name>, each rowcfloat columns plus aname_exacttext column with the same exact encoding per row.list[T]/ homogeneoustuple[T, ...]— a child table: scalar or codec-typed elements store their columns per row; storable-dataclass elements store onename_sidforeign-key column per row.another storable frozen dataclass (optionally
| None) — a reference: onename_sidforeign-key column.Annotated[..., Skip()]— omitted from storage (the field must have a default so instances can be reconstructed without it).Annotated[..., IdentitySkip()]— stored normally but omitted from representation identity.Annotated[..., Related(...)]— relationship metadata carried on the resolvedFieldSpec; valid only on reference fields and on lists/tuples of storable classes.a
stored_property— resolved like a field from its return annotation, flagged derived: stored and queryable, recomputed (not passed to__init__) on reconstruction.
Optional child fields also receive a store-managed Boolean <field>_present
parent column, preserving the distinction between None and an empty child
sequence.
The store layer additionally manages a sid integer primary key and a
content_id text column on every table (and <parent>_sid /
<name>_index columns on child tables); those never appear in
TableSchema.fields, and declaring a field named sid or
content_id is an error.
Attributes¶
The five scalar column kinds a storage backend must provide. |
|
How a stored field maps onto the relational model. |
Exceptions¶
A class or field cannot be resolved into a storage schema; the message names both. |
Classes¶
Describe one scalar column of a table. |
|
Describe the out-of-line child table backing a variable-length field. |
|
Describe the resolved storage shape of one stored field or property. |
|
Describe the resolved relational schema of one storable class. |
Functions¶
|
Register an external |
|
Resolve (and cache) the |
Module Contents¶
- type httk.store.db.schema.ScalarKind = Literal['int', 'float', 'str', 'bool', 'bytes'][source]¶
The five scalar column kinds a storage backend must provide.
- type httk.store.db.schema.FieldRole = Literal['scalar', 'encoded', 'fixed_array', 'child', 'reference'][source]¶
How a stored field maps onto the relational model.
- exception httk.store.db.schema.SchemaError[source]¶
Bases:
ExceptionA class or field cannot be resolved into a storage schema; the message names both.
- class httk.store.db.schema.ColumnSpec[source]¶
Describe one scalar column of a table.
- Parameters:
name – The column name.
kind – The scalar storage kind.
nullable – Whether the column accepts NULL.
indexed – Whether a single-column index is requested.
unique – Whether a unique index is requested.
- kind: httk.store.db.codecs.ScalarKind[source]¶
The scalar kind of the column.
- class httk.store.db.schema.ChildTableSpec[source]¶
Describe the out-of-line child table backing a variable-length field.
Only the per-element value columns are listed; the store layer adds the
<parent>_sidforeign key and<name>_indexordering columns.- Parameters:
table_name – The child table name.
element_columns – The value columns of one element row.
target – The storable element class for foreign-key rows, if any.
- element_columns: tuple[ColumnSpec, Ellipsis][source]¶
The value column(s) of one element row.
- class httk.store.db.schema.FieldSpec[source]¶
Describe the resolved storage shape of one stored field or property.
- Parameters:
field – The dataclass field or stored property name.
python_type – The field value type after marker and optionality resolution.
role – How the field maps onto the relational model.
columns – The field columns in the parent table.
codec_name – The codec name for the field or its child elements, if any.
shape – The tensor shape marker, if any.
child – The child table specification, if the field has a child role.
target – The referenced storable class, if any.
related – The relationship marker, if any.
derived – Whether the value is a stored property recomputed on reconstruction.
optional – Whether the annotation permits
None.
- columns: tuple[ColumnSpec, Ellipsis] = ()[source]¶
The field’s columns in the parent table (empty for the child role).
- codec_name: str | None = None[source]¶
The value codec encoding this field (or its child elements), if any.
- shape: httk.core.storage.Shape | None = None[source]¶
The
Shapemarker for tensor-valued fields, if any.
- child: ChildTableSpec | None = None[source]¶
The child table specification for the child role, else None.
- target: type | None = None[source]¶
The referenced storable class for reference (and child-of-storable) fields.
The
Relatedrelationship marker of the field, if any.Only reference fields and child fields of storable elements can carry one; the marker’s metadata flows into the relationships an entry provider emits for the field.
- derived: bool = False[source]¶
stored and queryable, recomputed rather than passed to
__init__on reconstruction.- Type:
True for
stored_propertyvalues
- class httk.store.db.schema.TableSchema[source]¶
Describe the resolved relational schema of one storable class.
- Parameters:
cls – The storable dataclass resolved into this schema.
table_name – The table name used for the class.
fields – The stored fields and properties.
composite_indexes – The resolved composite index declarations.
dedup – The deduplication policy applied on save.
links – The validated relationship declarations.
- table_name: str[source]¶
The table name (
storage_nameor the snake-cased class name).
- fields: tuple[FieldSpec, Ellipsis][source]¶
The stored fields, dataclass fields first (in declaration order), then stored properties.
The store-managed
sidandcontent_idcolumns are not fields and do not appear here.
- composite_indexes: tuple[tuple[str, Ellipsis], Ellipsis][source]¶
The
indexesdeclarations, resolved to column names.
- dedup: httk.core.storage.DedupPolicy[source]¶
The deduplication policy applied when instances are saved.
- links: tuple[httk.core.storage.RelationshipLink, Ellipsis] = ()[source]¶
The class’s
linksrelationship declarations.Each link is validated: every non-
Noneendpoint names an existing reference field of the class (child fields are not valid endpoints — a link row expresses exactly one FROM→TO pair).
- field(name)[source]¶
Return the field specification named
name.- Parameters:
name (str) – The stored field or property name.
- Returns:
The matching field specification.
- Raises:
httk.store.db.schema.SchemaError – If no stored field has that name.
- Return type:
- httk.store.db.schema.register_schema_override(cls, info)[source]¶
Register an external
StorageInfofor a class that cannot declare one.The registered info is used by
resolve_schema()whenever no explicitoverrideargument is passed, and takes precedence over a__httk_storage__declaration on the class itself.- Parameters:
cls (type) – The storable class whose schema should be overridden.
info (httk.core.storage.StorageInfo) – The external storage information to register.
- Returns:
None.
- Return type:
None
- httk.store.db.schema.resolve_schema(cls, *, override=None)[source]¶
Resolve (and cache) the
TableSchemaof a storable dataclass.The effective
StorageInfois, in order of precedence: the explicitoverrideargument, an info registered viaregister_schema_override(), the class’s own__httk_storage__attribute, or defaults. Results are cached per(class, effective override), so repeated calls return the sameTableSchemaobject. Reference cycles (a class referencing itself, or mutually referencing classes) are allowed and resolve without recursion loops.- Parameters:
cls (type) – The storable class to resolve.
override (httk.core.storage.StorageInfo | None) – External storage information taking precedence over declarations.
- Returns:
The cached resolved table schema.
- Raises:
httk.store.db.schema.SchemaError – If the class is not a frozen dataclass or one of its fields cannot be resolved; the diagnostic names the class and field.
- Return type: