mool/queries/plan.rs
1//! Rendered SQL and parameter metadata produced from a typed query.
2use indexmap::IndexMap;
3use std::collections::HashMap;
4use std::fmt;
5
6use super::handles::VarId;
7use crate::argvalue::ArgValue;
8
9/// Dialect-specific SQL and parameter metadata produced from a typed query AST.
10#[derive(Clone)]
11pub struct QueryPlan {
12 /// SQL rendered for the selected dialect.
13 pub sql: String,
14 /// Named `var(...)` and generated `val(...)` parameters in encounter order.
15 pub params: IndexMap<String, ParamSpec>,
16 /// Rust type name expected by a row-scanning terminal, when known.
17 pub result_type: Option<&'static str>,
18 /// Number of row payload values already bound before dynamic query params.
19 pub prebound_count: usize,
20 /// Number of `var(...)` and `val(...)` placeholders bound after row values.
21 pub dynamic_bind_count: usize,
22 /// Total number of placeholders in the rendered statement.
23 pub total_bind_count: usize,
24 pub(super) values: HashMap<String, ArgValue>,
25 pub(super) bind_order: Vec<String>,
26}
27
28impl fmt::Debug for QueryPlan {
29 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
30 f.debug_struct("QueryPlan")
31 .field("sql", &self.sql)
32 .field("params", &self.params)
33 .field("result_type", &self.result_type)
34 .field("prebound_count", &self.prebound_count)
35 .field("dynamic_bind_count", &self.dynamic_bind_count)
36 .field("total_bind_count", &self.total_bind_count)
37 .finish_non_exhaustive()
38 }
39}
40
41/// Metadata for one logical planned SQL parameter.
42#[derive(Debug, Clone)]
43pub struct ParamSpec {
44 /// Stable placeholder identity for `var(...)` parameters.
45 pub var_id: Option<VarId>,
46 /// Optional user-facing placeholder name.
47 pub display_name: Option<String>,
48 /// Stable parameter name.
49 pub name: String,
50 /// One-based first placeholder position in the rendered SQL.
51 pub position: usize,
52 /// Every one-based placeholder position for this logical parameter.
53 pub occurrences: Vec<usize>,
54 /// Rust type name inferred from the expression context.
55 pub rust_type: Option<&'static str>,
56 /// Optional SQL type hint reserved for future code generation.
57 pub sql_type: Option<String>,
58 /// Whether this parameter came from `val(...)` or `var(...)`.
59 pub source: ParamSource,
60}
61
62/// Describes whether a parameter came from `val(...)` or `var(...)`.
63#[derive(Debug, Clone, Copy, PartialEq, Eq)]
64pub enum ParamSource {
65 Val,
66 Var,
67}