Skip to main content

no_mistakes_postgres_sql/
statement_facts.rs

1mod bounds;
2mod debug;
3mod grouping;
4mod insert;
5mod iteration;
6mod json_writes;
7mod keyed_lookups;
8mod locations;
9mod parse_failures;
10mod select;
11mod transformed;
12mod writes;
13pub use bounds::{
14    SqlBareRead, SqlBoundFact, SqlBoundInputMode, SqlBoundItem, SqlBoundItemKind, SqlBoundKind,
15    SqlBoundOutput, SqlBoundPin, SqlBoundQuery, SqlGroupKey, SqlPinSource, SqlPossibleTemporary,
16    SqlQualifiedRead, SqlQualifiedScope, SqlScalarReads,
17};
18#[doc(hidden)]
19pub use grouping::SqlGroupingQuery;
20pub use insert::SqlInsertFact;
21pub use iteration::{SqlConjunctFact, SqlCursorBound, SqlLimitFact, SqlLimitValue, SqlSweepFact};
22pub use json_writes::SqlJsonWriteFact;
23pub use keyed_lookups::SqlDuplicateKeyedLookupFact;
24#[doc(hidden)]
25pub use locations::SqlLocationDemand;
26pub use locations::{SqlFactPosition, SqlFactSite, SqlVariantLocations};
27pub use select::{
28    SqlCatalogPredicate, SqlColumnClause, SqlColumnUseFact, SqlCountExistenceFact,
29    SqlExistsSetOpFact, SqlRelationPredicateFact, SqlSelectFact, SqlStarProjectionFact,
30};
31use std::path::PathBuf;
32pub use transformed::{
33    SqlTransformedColumnClause, SqlTransformedColumnProbeFact, SqlTransformedColumnUseFact,
34};
35pub use writes::{SqlWriteColumns, SqlWriteFact};
36mod query_shapes;
37#[doc(hidden)]
38pub use query_shapes::{SqlQueryShape, SqlQueryShapeFact};
39mod lifecycle;
40pub use lifecycle::SqlLifecycleFacts;
41#[doc(hidden)]
42pub use lifecycle::{SqlBoundSourceLines, SqlFunctionBounds};
43#[doc(hidden)]
44pub use lifecycle::{SqlLifecycleBatch, SqlLifecycleStep, SqlViewReads};
45/// Statement facts for one SQL source (file or embedded call).
46#[derive(Clone, PartialEq, Eq, Default)]
47pub struct SqlStatementFileFacts {
48    #[doc(hidden)]
49    pub lock_inventory: Vec<super::lock_inventory::InventoryLockFact>,
50    #[doc(hidden)]
51    pub lock_inventory_unanalyzed: Vec<usize>,
52    #[doc(hidden)]
53    pub lock_writes: super::statements::lock_writes::LockWriteFacts,
54    #[doc(hidden)]
55    pub lookup_step: Option<super::statements::lock_writes::lookup::LookupStep>,
56    /// Request-local provenance for complete embedded alternatives; absent for legacy sources.
57    #[doc(hidden)]
58    pub variant_locations: Option<SqlVariantLocations>,
59    pub path: PathBuf,
60    /// Statement categories shared with schema policy, including static routine DDL.
61    pub statement_kinds: Vec<super::types::SqlStatementKind>,
62    pub setting_uses: Vec<super::types::SqlSettingUse>,
63    #[doc(hidden)]
64    pub clause_uses: Vec<super::SqlPolicyClauseUse>,
65    pub function_calls: Vec<super::SqlFunctionCallFact>,
66    /// Syntactic write targets; catalog resolution happens after preparation.
67    pub writes: Vec<SqlWriteFact>,
68    pub json_writes: Vec<SqlJsonWriteFact>,
69    pub duplicate_keyed_lookups: Vec<SqlDuplicateKeyedLookupFact>,
70    #[doc(hidden)]
71    pub grouping_queries: Vec<SqlGroupingQuery>,
72    pub inserts: Vec<SqlInsertFact>,
73    pub selects: Vec<SqlSelectFact>,
74    #[doc(hidden)]
75    pub index_probes: std::collections::BTreeMap<(usize, usize), Vec<String>>,
76    /// One entry per `UPDATE`. Each entry is that statement's relation instances.
77    pub updates: Vec<Vec<SqlRelationPredicateFact>>,
78    /// One entry per `DELETE`. Each entry is that statement's relation instances.
79    pub deletes: Vec<Vec<SqlRelationPredicateFact>>,
80    pub triggers: Vec<SqlTriggerFact>,
81    /// `RETURNING *` / `RETURNING t.*` on INSERT, UPDATE, and DELETE.
82    pub returning_stars: Vec<SqlStarProjectionFact>,
83    /// Bare column comparisons on UPDATE/DELETE WHERE and JOIN ON predicates.
84    pub mutation_column_uses: Vec<SqlColumnUseFact>,
85    /// Cast and function inputs in WHERE, JOIN ON and HAVING predicates.
86    pub transformed_column_uses: Vec<SqlTransformedColumnUseFact>,
87    /// Executed OFFSET occurrences in source order.
88    pub offset_uses: Vec<super::offset::SqlOffsetFact>,
89    /// Row-count bounds of each executed SELECT, UPDATE and DELETE.
90    pub bounds: Vec<SqlBoundFact>,
91    /// Present only when a lifecycle decision needs per-catalog evidence.
92    #[doc(hidden)]
93    pub lifecycle: Option<SqlLifecycleFacts>,
94    /// Every `LIMIT` / `FETCH FIRST` in the executed statements, in source order.
95    pub limit_uses: Vec<SqlLimitFact>,
96    /// Limited single-table queries ordered by plain columns: pages of a key walk.
97    pub sweeps: Vec<SqlSweepFact>,
98    #[doc(hidden)]
99    pub query_shapes: Vec<SqlQueryShapeFact>,
100    pub parse_failed: bool,
101    /// Source-ordered failed statement starts from the existing recovery pass.
102    pub parse_failures: Vec<super::types::SqlUnanalyzableStatement>,
103    pub insert_keyword_count: usize,
104    pub has_top_level_not_exists: bool,
105    pub origin_line: usize,
106}
107/// `ON CONFLICT` action recovered from an INSERT.
108#[derive(Debug, Clone, PartialEq, Eq)]
109pub struct SqlOnConflictFact {
110    pub action: SqlOnConflictAction,
111    pub arbiter: SqlConflictArbiter,
112    pub assignments: Vec<SqlAssignmentFact>,
113    pub where_proof: SqlConflictWhereProof,
114    #[doc(hidden)]
115    pub copies_conflict_keys_only: bool,
116}
117#[derive(Debug, Clone, Copy, PartialEq, Eq)]
118pub enum SqlOnConflictAction {
119    DoNothing,
120    DoUpdate,
121}
122
123#[derive(Debug, Clone, PartialEq, Eq)]
124pub enum SqlConflictArbiter {
125    Columns(Vec<String>),
126    /// Source expression dependencies; `unknown` means membership cannot be proven.
127    Expressions {
128        columns: Vec<String>,
129        /// Columns that participate in one expression and must move together.
130        groups: Vec<Vec<String>>,
131        unknown: bool,
132    },
133    Constraint(String),
134    Unknown,
135}
136
137/// One SET/VALUES assignment classified without keeping the sqlparser AST.
138#[derive(Debug, Clone, PartialEq, Eq)]
139pub struct SqlAssignmentFact {
140    pub column: String,
141    pub form: SqlValueForm,
142}
143
144#[derive(Debug, Clone, PartialEq, Eq)]
145pub enum SqlValueForm {
146    Literal,
147    Null,
148    Excluded { column: String },
149    SelfRef { column: String },
150    Coalesce { args: Vec<SqlValueForm> },
151    Greatest { args: Vec<SqlValueForm> },
152    Least { args: Vec<SqlValueForm> },
153    Placeholder,
154    Volatile { name: String },
155    Subquery,
156    Other,
157}
158
159/// Conjunctive no-op proofs on `ON CONFLICT … WHERE`.
160#[derive(Debug, Clone, PartialEq, Eq, Default)]
161pub struct SqlConflictWhereProof {
162    pub distinct_from_excluded: Vec<String>,
163    pub null_and_excluded_not_null: Vec<String>,
164    pub disjunctive: bool,
165}
166/// One `CREATE TRIGGER`.
167#[derive(Debug, Clone, PartialEq, Eq)]
168pub struct SqlTriggerFact {
169    pub table: String,
170    pub function: String,
171    pub period: SqlTriggerPeriod,
172    pub for_each_row: bool,
173    pub events: Vec<SqlTriggerEvent>,
174    pub line: usize,
175    pub column: usize,
176    pub name: String,
177    pub removed: bool,
178    pub source_path: PathBuf,
179}
180
181#[derive(Debug, Clone, Copy, PartialEq, Eq)]
182pub enum SqlTriggerPeriod {
183    Before,
184    After,
185    InsteadOf,
186    Other,
187}
188
189#[derive(Debug, Clone, PartialEq, Eq)]
190pub enum SqlTriggerEvent {
191    Insert,
192    Update { columns: Vec<String> },
193    Delete,
194    Truncate,
195}