Application API (autodoc)¶
Generated members most application code needs. For narrative docs, see Public API. Plugin and IR symbols: Plugin and IR API (autodoc).
- class sqlrules.ArrayContains(value)[source]¶
Require array containment (
array_contains).- Parameters:
value (Any)
- class sqlrules.ArrayOverlap(value)[source]¶
Require array overlap (
array_overlap).- Parameters:
value (Any)
- class sqlrules.Compiler(*, registry=None, plugins=None, on_conflict='raise', on_unsupported='raise', dialect=None, cache=True, model_cache=None, emit_type_checks=False)[source]¶
Compile constrained Pydantic models into SQLAlchemy rule dictionaries.
dialectis an optional hint stored onCompilationContextfor custom translators. It does not load dialect plugins or change built-in translations — passplugins=[...]explicitly.emit_type_checks(defaultFalse) extractstype_checkIR from supported scalar annotations. Translation requires a dialect plugin or custom translator (same pattern aspattern).Do not mutate
registryafter construction; registration belongs inplugins/register_constraintduring init. Do not callcompile/bind/compile_modelconcurrently on the same instance.- Parameters:
- bind(model_ir, table, *, column_map=None)[source]¶
Phase 2: resolve columns and translate IR into SQLAlchemy expressions.
- Parameters:
model_ir (ModelIR)
table (Any)
column_map (Mapping[str, ColumnElement[Any]] | None)
- Return type:
dict[str, list[ColumnElement[bool]]]
- compile(model, table, *, column_map=None)[source]¶
Compile
modelconstraints and bind them totablecolumns.- Parameters:
- Returns:
Mapping of Python field names to lists of boolean
ColumnElementexpressions. Unconstrained supported-type fields are omitted.- Raises:
InvalidModelError –
modelis not a usable Pydantic model.MissingColumnError – A constrained field could not bind.
UnsupportedConstraintError – No translator for an operator/type, or
emit_type_checks/patternwithout a translator.TranslatorError – A registered translator failed at bind time.
ConfigurationError – Invalid compiler configuration.
- Return type:
dict[str, list[ColumnElement[bool]]]
- class sqlrules.ConstraintMarker(*args, **kwargs)[source]¶
Protocol for Annotated metadata that becomes a SQLRules IR constraint.
- class sqlrules.FullTextMatch(value)[source]¶
Require a full-text match (
fulltext_match).- Parameters:
value (Any)
- exception sqlrules.InvalidModelError(model: 'Any')[source]¶
- Parameters:
model (Any)
- Return type:
None
- class sqlrules.JsonContains(value)[source]¶
Require JSON/JSONB containment (
json_contains).- Parameters:
value (Any)
- class sqlrules.JsonHasKey(value)[source]¶
Require a JSON object key (
json_has_key).- Parameters:
value (Any)
- exception sqlrules.MissingColumnError(field: 'str')[source]¶
- Parameters:
field (str)
- Return type:
None
- class sqlrules.RangeContains(value)[source]¶
Require range containment (
range_contains).- Parameters:
value (Any)
- class sqlrules.RangeOverlap(value)[source]¶
Require range overlap (
range_overlap).- Parameters:
value (Any)
- exception sqlrules.SQLRulesWarning[source]¶
Warning emitted when an unsupported constraint is skipped.
- exception sqlrules.UnsupportedConstraintError(field: 'str', operator: 'str', value: 'Any' = None, suggestion: 'str | None' = None)[source]¶
- sqlrules.clear_model_cache()[source]¶
Clear the process-wide default Phase-1
ModelIRcache.Use this when creating many ephemeral models (for example
pydantic.create_model) so cached IR does not grow without bound. Compilers that were given a custommodel_cacheare unaffected.- Return type:
None
- sqlrules.compile(model, table, *, column_map=None, on_unsupported='raise', cache=True, emit_type_checks=False)[source]¶
One-shot Application API compile (no plugins).
Compiles Field constraint metadata into SQLAlchemy expressions. Does not apply model instance / request values.
Creates a fresh
Compilerper call so concurrent module-levelcompile()invocations do not share mutable diagnostics state. Phase-1 IR still uses the process-wide cache whencache=True.- Parameters:
table (Any) – SQLAlchemy
Table, selectable, ORM class, or object with.c.column_map (Mapping[str, ColumnElement[Any]] | None) – Optional field-name or alias → column mapping.
on_unsupported (Literal['raise', 'warn', 'ignore']) – Policy for unknown operators (
"raise","warn", or"ignore"). Unsupported types always raise.cache (bool) – Cache Phase-1 model IR (default
True).emit_type_checks (bool) – Emit
type_checkIR for supported scalars (requires a plugin translator — useCompiler(plugins=...)).
- Returns:
Mapping of Python field names to lists of boolean
ColumnElementexpressions.- Raises:
InvalidModelError –
modelis not a usable Pydantic model.MissingColumnError – A constrained field could not bind.
UnsupportedConstraintError – No translator for an operator/type.
TranslatorError – A registered translator failed at bind time.
ConfigurationError – Invalid
on_unsupportedvalue.
- Return type:
dict[str, list[ColumnElement[bool]]]
Note
For plugins, construct
Compiler(plugins=[...])explicitly.
- sqlrules.flatten(rules)[source]¶
Flatten a rules dict into a single list of expressions.
- Parameters:
rules (dict[str, list[ColumnElement[bool]]]) – Mapping of field name to lists of boolean column elements, as returned by
compile/Compiler.compile.- Returns:
Concatenated list suitable for spreading into a SQLAlchemy
whereclause.- Return type:
- sqlrules.where(rules)[source]¶
Flatten a rules dict for a SQLAlchemy
whereclause.Identical to
flatten(). Prefer this name at call sites.- Parameters:
rules (dict[str, list[ColumnElement[bool]]]) – Mapping of field name to lists of boolean column elements.
- Returns:
Concatenated list of expressions.
- Return type: