#[non_exhaustive]pub struct CteSpec {
pub name: String,
pub sql: String,
pub table: String,
pub where_expr: Option<BoolExpr>,
pub params: Vec<DbValue>,
pub columns: Vec<String>,
pub is_recursive: bool,
pub recursive_link: Option<(String, String)>,
}Expand description
Specification for a Common Table Expression (CTE).
A CTE is defined by a name and either a pre-compiled SQL string (raw mode) or a typed WHERE expression compiled at SQL generation time (typed mode). The main query references the CTE by name (typically in its FROM clause). Parameters are prepended to the main query’s parameter list in CTE declaration order.
§Modes
-
Raw mode (via
with_cte_internal):sqlis non-empty,tableandwhere_exprare empty. The SQL is emitted verbatim. Placeholders use the?style and are not converted to provider-specific syntax — suitable for SQLite/MySQL but may produce incorrect$Non PostgreSQL. -
Typed mode (via
with_cte_typed, used bylinq!(with ...)):tableis non-empty,where_exprisSome(...),sqlis empty. The CTE bodySELECT * FROM <table> WHERE <expr>is compiled atto_sql_withtime using the provider’s placeholder syntax, ensuring correct$Nnumbering on all providers.
#[non_exhaustive] prevents direct struct construction outside the crate
so future field additions don’t break downstream code. Use
with_cte_internal (raw mode) or with_cte_typed (typed mode) to create
CTE specifications.
Fields (Non-exhaustive)§
This struct is marked as non-exhaustive
Struct { .. } syntax; cannot be matched against without a wildcard ..; and struct update syntax will not work.name: StringThe CTE name (used as the derived table alias in WITH name AS (...)).
sql: StringRaw mode: the pre-compiled SQL of the CTE body. Empty in typed mode.
table: StringTyped mode: source table name (SELECT * FROM <table> WHERE ...).
Empty in raw mode.
where_expr: Option<BoolExpr>Typed mode: WHERE expression compiled at to_sql_with time with the
provider’s placeholder syntax. None in raw mode.
params: Vec<DbValue>Parameter values bound to the CTE’s placeholders, in order.
In typed mode, these are extracted from where_expr via
collect_bool_expr_values at construction time.
columns: Vec<String>Optional explicit column list (WITH name (c1, c2) AS (...)).
Empty means no explicit column list.
is_recursive: boolRecursive CTE flag. When true, generates
WITH RECURSIVE name AS (anchor UNION ALL SELECT t.* FROM table t JOIN name ON t.fk = name.pk).
recursive_link: Option<(String, String)>Recursive link columns: (fk_column, pk_column). Only meaningful when
is_recursive is true. The recursive member joins the CTE name to the
source table via t.fk = name.pk.