Skip to main content

Module sql

Module sql 

Source
Expand description

Safe SQL builder: identifiers from config only, values as parameters.

Re-exports§

pub use rsql::parse_rsql;
pub use rsql::parse_sort;
pub use rsql::FilterNode;
pub use rsql::RsqlOp;
pub use rsql::SortSpec;
pub use params::*;

Modules§

params
Convert serde_json::Value to bindable SQL parameter values across all dialects.
rsql
RSQL filter and sort parser.

Structs§

IncludeSelect
Describes one include for single-query list: name, direction, related entity, our key column, their key column.
QueryBuf

Functions§

archive
UPDATE by id: stamp archive_field with NOW() where it is currently NULL. Returns the updated row or None (record not found or already archived).
coerce_json_value_for_pg_array
Postgres array columns: API accepts JSON ["a","b"]; bind as array literal + $n::varchar(255)[] etc.
delete
DELETE by id.
insert
INSERT: columns and placeholders from entity; values from body. Excludes PK if has_default. Omits columns with DB default when body does not provide a value (so DB uses default). Uses SQL cast (e.g. $n::timestamptz) for timestamp columns so string values bind correctly. When rls_tenant_id is Some, appends tenant_id column and value (for RLS strategy).
insert_history_snapshot
INSERT INTO {table}_history: copy the current row from the main table before an update/delete. Uses a single INSERT … SELECT so the snapshot is atomic and never goes through the app layer. Binds: $1 = operation text (“update” | “delete”), $2 = pk value.
prune_history
DELETE old history rows beyond keep_versions for a given pk. Binds: $1 = pk value, $2 = keep_versions (bigint).
rsql_to_sql
Convert a FilterNode tree into a SQL WHERE fragment (no leading WHERE). All values are pushed as parameters into q; identifiers come only from config (never from user input) so SQL injection is structurally impossible.
select_by_column_in
SELECT * FROM entity WHERE column IN ($1, $2, …) ORDER BY pk. Used for batch-fetching related rows (to_many or to_one by key).
select_by_id
SELECT by primary key (single column PK only). Caller adds id as sole param.
select_history_by_version
SELECT a specific version from history for a given pk. Binds: $1 = pk value, $2 = version (bigint).
select_history_list
SELECT all history rows for a given pk, ordered newest first. Binds: $1 = pk value.
select_list
SELECT list with optional RSQL filter and sort specs. filter_includes is needed when the filter contains dotted-field conditions (e.g. transport_unit.bay=contains=bay23) that generate EXISTS subqueries. Pass an empty slice when there are no such filters.
select_list_with_includes
SELECT list with includes in a single query: main table aliased as “main”, each include as a scalar subquery (json_agg for to_many, row_to_json for to_one). includes drives the scalar subqueries (response data); filter_includes is the superset used for EXISTS generation when the filter references dotted fields like transport_unit.bay.
unarchive
UPDATE by id: clear archive_field (set to NULL) where it is currently NOT NULL. Returns the updated row or None (record not found or not archived).
update
UPDATE by id: SET only columns present in body (and in entity columns). Uses SQL cast for timestamp columns so string values bind correctly.