httk.store.query.protocols¶
Typed protocols for the store/searcher query contract.
These protocols define the backend-agnostic query interface shared by httk
data stores: the database layer in httk-store implements them over SQL, and
serving modules (such as httk-serve, whose in-memory store also conforms)
program against them. They mirror the query interface of the httk v1 database
layer (httk.db FilteredCollection searchers), so lightweight fakes can
stand in for a real store in tests.
Attributes¶
The backend field name used for the served entry identifier. |
Exceptions¶
Report that a valid query operation is outside a store's supported profile. |
|
Report that a store cannot provide an exact query count. |
|
Report that a result-set |
|
Report that a result-set |
|
Report that a continuation cursor is malformed, expired, or belongs to another result plan. |
Classes¶
Order a continuation page by one named scalar result projection. |
|
Carry an opaque URL-safe continuation value. |
|
Represent an immutable continuation-page result. |
|
Expose optional continuation-page capability on a frozen result set. |
|
Represent one named result row by position, name, or attribute. |
|
Require named access to one result row. |
|
Require the common operations of a materialized result set. |
|
Require composable backend search expressions. |
|
Expose a queryable field of a search variable. |
|
Bind a query variable to a target type whose attributes yield fields. |
|
Represent one match with declared output values and names. |
|
Build one query and iterate its results. |
|
Require a store that can create a query searcher. |
Module Contents¶
- httk.store.query.protocols.ID_FIELD: Final = '__id'¶
The backend field name used for the served entry identifier.
- exception httk.store.query.protocols.UnsupportedQueryError¶
Bases:
ValueErrorReport that a valid query operation is outside a store’s supported profile.
Bases:
RuntimeErrorReport that a store cannot provide an exact query count.
- exception httk.store.query.protocols.NoResultError¶
Bases:
LookupErrorReport that a result-set
one()operation found no matching result.
- exception httk.store.query.protocols.MultipleResultsError¶
Bases:
LookupErrorReport that a result-set
one()operation found multiple results.
- exception httk.store.query.protocols.PaginationCursorError¶
Bases:
ValueErrorReport that a continuation cursor is malformed, expired, or belongs to another result plan.
- class httk.store.query.protocols.PageOrder¶
Order a continuation page by one named scalar result projection.
nameidentifies the name supplied toresults()(orSearcher.output()), never a backend column object. The result-set implementation validates that it is a root scalar projection before it generates SQL.- Parameters:
name – The declared scalar output name used for ordering.
descending – Whether to order this field in descending order.
nulls – Whether null values sort first or last.
- nulls: Literal['first', 'last'] = 'last'¶
- class httk.store.query.protocols.ContinuationToken¶
Bases:
strCarry an opaque URL-safe continuation value.
It is a
strsubclass so normal JSON serializers preserve it as a scalar value. Applications should pass a token returned by a page back unchanged; data backends validate its version, structure, and result-plan fingerprint before using any decoded value as a bound parameter.- Parameters:
value – The opaque continuation value.
- class httk.store.query.protocols.ResultPage¶
Represent an immutable continuation-page result.
rowsis always a tuple. Returned rows are ordinary persistent result rows, not the expiring proxies produced bySqlResultSet.cursor().totalis populated only when the caller explicitly asks for it.- Parameters:
rows – The persistent rows returned by the page.
next – The token for the next page, if one exists.
previous – The token for the previous page, if one exists.
total – The exact result count when requested, otherwise
None.
- rows: tuple[ResultRowLike, Ellipsis]¶
- next: ContinuationToken | None¶
- previous: ContinuationToken | None¶
- class httk.store.query.protocols.PageableResultSetLike¶
Bases:
ProtocolExpose optional continuation-page capability on a frozen result set.
This deliberately extends neither
ResultSetLikenorSearcher: stores that do not support seek pagination remain fully conforming to the required portable contracts.- page(*, size, order_by, cursor=None, include_total=False)¶
Return one ordered continuation page.
- class httk.store.query.protocols.ResultRow(values, names, resolver=None, guard=None)¶
Represent one named result row by position, name, or attribute.
- Parameters:
- class httk.store.query.protocols.ResultRowLike¶
Bases:
ProtocolRequire named access to one result row.
- class httk.store.query.protocols.ResultSetLike¶
Bases:
ProtocolRequire the common operations of a materialized result set.
- first()¶
Return the first row, or
Nonewhen no row matches.
- one()¶
Return the only row, or raise when the count is not one.
- scalars(name=None)¶
Iterate over one named scalar output.
- class httk.store.query.protocols.SearchExpression¶
Bases:
ProtocolRequire composable backend search expressions.
- class httk.store.query.protocols.SearchField¶
Bases:
ProtocolExpose a queryable field of a search variable.
In addition to the methods below, fields support the rich comparison operators (
==,!=,<,<=,>,>=), returningSearchExpression. The handlers invoke those viagetattr(field, '__eq__')(value)since the comparison dunders cannot be typed as expression-returning.The three string-matching methods take literal text: no wildcard or pattern syntax whatsoever crosses this contract, so
%and_(and any other metacharacter) match themselves. A backend is therefore free to implement them with SQLLIKEover an escaped pattern, with a regular expression, or with a full-text index — the choice is invisible here.- has(value)¶
Match a list field containing
value.
- has_any(*values)¶
Match a list field containing any of
values.
- has_only(*values)¶
Match a list field containing no values outside
values.
- is_in(*values)¶
Match a root scalar field whose value is one of
values.Noneis an explicit member: it matches a null field value, and its negation excludes nulls rather than inheriting SQL’s three-valuedNOT IN (..., NULL)behavior.Backends define the corresponding semantics for child or set fields; for example, a backend may use the existing
has_only-style all-values reading for a child field.
- contains(text)¶
Match values containing
textas a literal substring.
- startswith(prefix)¶
Match values beginning with the literal
prefix.
- endswith(suffix)¶
Match values ending with the literal
suffix.
- class httk.store.query.protocols.SearchVariable¶
Bases:
ProtocolBind a query variable to a target type whose attributes yield fields.
always_true/always_falseare reserved names: they are real methods of the variable, never stored fields resolved through__getattr__. They exist so a translation layer can express a constant truth value without inventing a probe field. Afield == fieldprobe is NULL-unsound, since it yields NULL (not true) for a NULL field.- always_true()¶
An expression that matches every row.
- always_false()¶
An expression that matches no row.
- class httk.store.query.protocols.SearchResult¶
Bases:
NamedTupleRepresent one match with declared output values and names.
valuesholds one entry perSearcher.output()call in declaration order; it is a tuple, sovalues, names = resultandresult[0][0]both work.
- class httk.store.query.protocols.Searcher¶
Bases:
ProtocolBuild one query and iterate its results.
Iteration yields one
SearchResultper match, soitem[0][0]is the first declared output of the match (typically the matched row object). The expressions received byaddare always ones produced by this same backend’s search variables, so implementations may type them as their own expression class; a backend that needs a second (post-filter) evaluation position decides that from the expression itself, not from the caller.- variable(target)¶
Bind a query variable to
target.
- output(variable, name)¶
Declare
variableas a named result output.
- add(expression)¶
Add a filter expression to the query.
- count()¶
Return the exact count of the current query.
- set_limit(limit)¶
Set the query limit.
- add_offset(offset)¶
Add an offset to the query.
- add_sort(field, descending)¶
Add a field sort to the query.
- results(**outputs)¶
Return a result set for the requested named outputs.
- class httk.store.query.protocols.Store¶
Bases:
ProtocolRequire a store that can create a query searcher.
Implementations predating the
as_ofkeyword may omit it and remain usable for current-state queries, but cannot honor historic queries.