Skip to main content

no_mistakes_postgres_sql/
statement_facts.rs

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