no_mistakes_postgres_sql/statements/facts/
sources.rs1use crate::lock_inventory::{tokens::Segments, Collector};
2use sqlparser::tokenizer::TokenWithSpan;
3use std::sync::Arc;
4
5#[derive(Clone, Default)]
6pub struct StatementPolicySources<'a> {
7 pub bind_values: super::super::bounds::BindValues,
8 pub collect_lock_inventory: bool,
9 pub sql_batch: bool,
10 pub opaque_sql_prefix: bool,
11 pub collect_json_writes: bool,
12 pub collect_duplicate_keyed_lookups: bool,
13 pub collect_lookup_steps: bool,
14 pub collect_view_uses: bool,
15 pub collect_grouping: bool,
16 pub synthetic_fragment_where: bool,
17 pub complete_recovered_selects_only: bool,
18 pub statement_complete: Option<&'a [bool]>,
19 pub collect_index_probes: bool,
20 pub collect_body_bounds: bool,
21 pub share_lock_writes: bool,
22 pub redundant_prelocks: bool,
23 pub tokens: Option<&'a [Option<Arc<[TokenWithSpan]>>]>,
24 pub statement_locations: Option<&'a [sqlparser::tokenizer::Location]>,
25 pub schema: Option<&'a crate::SqlSchemaFileFacts>,
26 pub functions: &'a [crate::SqlFunctionCallFact],
27}
28
29#[derive(Clone, Copy, Default, PartialEq, Eq)]
30pub(super) enum StatementProjection {
31 #[default]
32 Complete,
33 ConflictPolicy,
34 LockScope,
35 BodyBounds,
36 BodyBoundsLockScope,
37}
38
39#[derive(Default)]
40pub(super) struct StatementSources<'a> {
41 pub(super) projection: StatementProjection,
42 pub(super) parse_failures: &'a [crate::SqlUnanalyzableStatement],
43 pub(super) locations: crate::statement_facts::SqlLocationDemand,
44 pub(super) tokens: Option<&'a [Option<Arc<[TokenWithSpan]>>]>,
45 pub(super) policy: StatementPolicySources<'a>,
47 pub(super) recovery: Option<&'a crate::conflict::ConflictChunks<'a>>,
48}
49
50impl StatementSources<'_> {
51 pub(super) fn lookup(
52 &self,
53 statements: &[sqlparser::ast::Statement],
54 positions: super::super::value::PlaceholderPositions<'_>,
55 parse_failed: bool,
56 ) -> Option<super::super::lock_writes::lookup::LookupStep> {
57 super::super::lock_writes::lookup::project(
58 statements,
59 positions,
60 self.policy.collect_lookup_steps && !parse_failed,
61 )
62 }
63
64 pub(super) fn locks(
65 &self,
66 statements: &[sqlparser::ast::Statement],
67 positions: super::super::value::PlaceholderPositions<'_>,
68 prepared: &super::PreparedSql<'_>,
69 inventory: Option<&Collector<'_>>,
70 ) -> super::super::lock_writes::LockWriteFacts {
71 if matches!(
72 self.projection,
73 StatementProjection::LockScope | StatementProjection::BodyBoundsLockScope
74 ) {
75 let supplemental = (self.policy.share_lock_writes && inventory.is_none())
76 .then(|| Segments::new(prepared.tokens()));
77 let modes = self
78 .policy
79 .share_lock_writes
80 .then(|| inventory.map(Collector::segments).or(supplemental.as_ref()))
81 .flatten();
82 super::super::lock_writes::collect::<true>(
83 statements,
84 positions,
85 self.policy.redundant_prelocks,
86 modes,
87 prepared.tokens(),
88 )
89 } else {
90 super::super::lock_writes::collect::<false>(statements, positions, false, None, &[])
91 }
92 }
93}
94
95impl<'a> StatementSources<'a> {
96 pub(super) fn grouping_requested(&self, index: usize) -> bool {
97 self.policy.collect_grouping && self.statement_is_complete(index)
98 }
99
100 pub(super) fn statement_is_complete(&self, index: usize) -> bool {
101 self.recovery
102 .map(|recovery| recovery.complete)
103 .or(self.policy.statement_complete)
104 .is_none_or(|complete| complete.get(index) == Some(&true))
105 }
106
107 pub(super) fn retain_complete_selects(
108 &self,
109 index: usize,
110 first_select: usize,
111 selects: &mut Vec<crate::SqlSelectFact>,
112 ) {
113 if self.policy.complete_recovered_selects_only && !self.statement_is_complete(index) {
114 selects.truncate(first_select);
115 }
116 }
117
118 pub(super) fn location_tokens(
119 &self,
120 index: usize,
121 fallback: &'a [TokenWithSpan],
122 ) -> &'a [TokenWithSpan] {
123 self.tokens
124 .or(self.policy.tokens)
125 .and_then(|tokens| tokens.get(index))
126 .and_then(Option::as_deref)
127 .unwrap_or(fallback)
128 }
129}
130
131pub(super) fn policy_tokens(
132 located: &[crate::parse::LocatedStatement],
133) -> Vec<Option<Arc<[TokenWithSpan]>>> {
134 located
135 .iter()
136 .map(|located| {
137 located
138 .source
139 .clone()
140 .or_else(|| located.chunk_tokens.clone())
141 })
142 .collect()
143}
144
145impl StatementPolicySources<'_> {
146 pub(super) fn lock_inventory<'sql>(
147 &self,
148 prepared: &'sql super::PreparedSql<'_>,
149 statements: &[sqlparser::ast::Statement],
150 ) -> Option<crate::lock_inventory::Collector<'sql>> {
151 self.collect_lock_inventory.then(|| {
152 crate::lock_inventory::Collector::new(
153 prepared,
154 statements,
155 self.statement_locations,
156 self.sql_batch,
157 )
158 })
159 }
160
161 pub(super) fn scope(
162 &self,
163 index: &super::super::TableTokenIndex,
164 statement: &sqlparser::ast::Statement,
165 ) -> super::super::bounds::Scope {
166 use sqlparser::ast::Spanned;
167 super::super::bounds::Scope::with_table_tokens(index.cursor_at(statement.span().start))
168 .with_bodies(self.collect_body_bounds)
169 .with_bind_values(self.bind_values.clone())
170 }
171}