Skip to main content

no_mistakes_postgres_sql/statements/facts/
parse.rs

1//! One prepared SQL parse feeds every requested statement projection.
2use super::sources::StatementProjection;
3use super::SqlStatementFileFacts;
4use crate::parse::PreparedSql;
5use crate::statement_facts::SqlLocationDemand;
6use crate::statements::value::PlaceholderPositions;
7use sqlparser::ast::Statement;
8
9mod prepared;
10use prepared::extract as extract_sql_statement_facts_with_placeholder_positions;
11
12/// Extract INSERT/SELECT/trigger facts from one SQL source.
13pub fn extract_sql_statement_facts(sql: &str) -> SqlStatementFileFacts {
14    extract_sql_statement_facts_with_placeholder_positions(
15        sql,
16        true,
17        None,
18        SqlLocationDemand::None,
19        StatementProjection::Complete,
20        super::StatementPolicySources {
21            collect_json_writes: true,
22            ..Default::default()
23        },
24    )
25}
26
27/// Extract the requested SQL projections without reparsing for row bounds.
28pub fn extract_sql_statement_facts_with_bounds(
29    sql: &str,
30    collect_bounds: bool,
31) -> SqlStatementFileFacts {
32    extract_sql_statement_facts_with_placeholder_positions(
33        sql,
34        collect_bounds,
35        None,
36        SqlLocationDemand::None,
37        StatementProjection::Complete,
38        Default::default(),
39    )
40}
41
42/// Extract facts while identifying the SQL-local positions of recovered interpolation markers.
43pub fn extract_sql_statement_facts_with_recovered_placeholders(
44    sql: &str,
45    collect_bounds: bool,
46    recovered_placeholder_positions: &[(u32, u32)],
47) -> SqlStatementFileFacts {
48    extract_sql_statement_facts_with_placeholder_positions(
49        sql,
50        collect_bounds,
51        Some(recovered_placeholder_positions),
52        SqlLocationDemand::None,
53        StatementProjection::Complete,
54        Default::default(),
55    )
56}
57
58pub fn extract_sql_variant_statement_facts(
59    sql: &str,
60    collect_bounds: bool,
61    positions: &[(u32, u32)],
62) -> SqlStatementFileFacts {
63    extract_sql_statement_facts_with_placeholder_positions(
64        sql,
65        collect_bounds,
66        Some(positions),
67        SqlLocationDemand::Full,
68        StatementProjection::Complete,
69        Default::default(),
70    )
71}
72
73pub fn extract_sql_statement_facts_with_location_demand(
74    sql: &str,
75    collect_bounds: bool,
76    positions: &[(u32, u32)],
77    demand: SqlLocationDemand,
78) -> SqlStatementFileFacts {
79    extract_sql_statement_facts_with_placeholder_positions(
80        sql,
81        collect_bounds,
82        Some(positions),
83        demand,
84        StatementProjection::Complete,
85        Default::default(),
86    )
87}
88
89pub fn extract_sql_statement_facts_with_json_writes(
90    sql: &str,
91    collect_bounds: bool,
92    positions: PlaceholderPositions<'_>,
93    demand: SqlLocationDemand,
94    collect_json_writes: bool,
95    collect_index_probes: bool,
96    collect_body_bounds: bool,
97) -> SqlStatementFileFacts {
98    extract_sql_statement_facts_with_placeholder_positions(
99        sql,
100        collect_bounds,
101        positions,
102        demand,
103        if collect_body_bounds {
104            StatementProjection::BodyBounds
105        } else {
106            StatementProjection::Complete
107        },
108        super::StatementPolicySources {
109            collect_json_writes,
110            collect_index_probes,
111            ..Default::default()
112        },
113    )
114}
115
116pub fn extract_sql_lock_scope_facts(
117    sql: &str,
118    collect_bounds: bool,
119    positions: &[(u32, u32)],
120    demand: SqlLocationDemand,
121    policy: super::StatementPolicySources<'_>,
122) -> SqlStatementFileFacts {
123    extract_sql_statement_facts_with_placeholder_positions(
124        sql,
125        collect_bounds,
126        Some(positions),
127        demand,
128        if policy.collect_body_bounds {
129            StatementProjection::BodyBoundsLockScope
130        } else {
131            StatementProjection::LockScope
132        },
133        policy,
134    )
135}
136
137/// Prepare only the fields consumed by the sole conflict-ordering policy.
138pub fn extract_sql_conflict_policy_facts(
139    sql: &str,
140    positions: &[(u32, u32)],
141) -> SqlStatementFileFacts {
142    extract_sql_statement_facts_with_placeholder_positions(
143        sql,
144        false,
145        Some(positions),
146        SqlLocationDemand::Conflicts,
147        StatementProjection::ConflictPolicy,
148        Default::default(),
149    )
150}
151
152pub fn extract_from_parsed_with_recovered_placeholders(
153    sql: &str,
154    prepared: &PreparedSql<'_>,
155    statements: &[Statement],
156    parse_failed: bool,
157    collect_bounds: bool,
158    placeholder_positions: PlaceholderPositions<'_>,
159    policy: super::StatementPolicySources<'_>,
160) -> SqlStatementFileFacts {
161    super::extract_from_parsed_and_sources(
162        sql,
163        prepared,
164        statements,
165        super::StatementSources {
166            locations: SqlLocationDemand::None,
167            tokens: None,
168            recovery: None,
169            policy,
170            ..Default::default()
171        },
172        parse_failed,
173        collect_bounds,
174        placeholder_positions,
175    )
176}
177#[cfg(all(test, any(nm_shard_all, nm_shard_2)))]
178mod index_probe_demand_tests;
179#[cfg(all(test, any(nm_shard_all, nm_shard_2)))]
180mod json_write_demand_tests;
181#[cfg(all(test, any(nm_shard_all, nm_shard_1)))]
182mod location_demand_tests;
183mod policy;
184#[cfg(all(test, any(nm_shard_all, nm_shard_1)))]
185mod policy_projection_tests;
186pub use policy::extract_sql_statement_facts_with_policy;
187mod bind_values;
188#[cfg(all(test, any(nm_shard_all, nm_shard_0)))]
189mod grouping_demand_tests;
190pub use bind_values::extract_with_bound_values;
191#[cfg(all(test, any(nm_shard_all, nm_shard_1)))]
192mod grouping_recovery_tests;