Skip to main content

no_mistakes_postgres_engine/
options.rs

1use serde::{Deserialize, Serialize};
2
3mod executor_selection;
4
5const DEFAULT_EXECUTOR_NAMES: &[&str] = &["query", "read", "write"];
6
7/// A named import the user asserts is a parameterized SQL tag.
8///
9/// Matching is syntactic: `name` imported from `module` or a subpath of it.
10/// Empty `module` or `name` matches nothing.
11#[derive(Debug, Clone, Default, PartialEq, Eq, PartialOrd, Ord, Hash, Deserialize, Serialize)]
12#[serde(rename_all = "camelCase")]
13pub struct TrustedSqlTag {
14    pub module: String,
15    pub name: String,
16}
17
18#[derive(Debug, Clone, Default, PartialEq, Eq, PartialOrd, Ord, Hash, Deserialize, Serialize)]
19#[serde(rename_all = "camelCase")]
20pub struct SqlIdentifierAssertion {
21    pub module: String,
22    pub name: String,
23    pub allowlist_argument: usize,
24}
25
26#[derive(Debug, Clone, Deserialize)]
27#[serde(rename_all = "camelCase", deny_unknown_fields)]
28pub struct ExecutorOption {
29    pub name: String,
30}
31
32/// Configurable executor import matching. Empty defaults select no executors.
33#[derive(Debug, Clone, Default, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize)]
34#[serde(rename_all = "camelCase")]
35pub struct EmbeddedSqlOptions {
36    pub import_specifier: String,
37    pub executor_names: Vec<String>,
38    #[serde(skip_serializing_if = "Vec::is_empty")]
39    pub exclude_executor_names: Vec<String>,
40    /// Named imports whose call result bound to a local is an executor.
41    pub executor_factory_names: Vec<String>,
42    /// Imported type names whose annotated parameters are executors.
43    pub executor_type_names: Vec<String>,
44    #[doc(hidden)]
45    #[serde(skip)]
46    pub transaction_executor_type_names: Vec<String>,
47    #[serde(skip_serializing_if = "Vec::is_empty")]
48    pub executor_option_properties: Vec<String>,
49    /// Named imports trusted as parameterized SQL tags. Empty trusts none.
50    pub trusted_sql_tags: Vec<TrustedSqlTag>,
51    #[serde(skip_serializing_if = "Vec::is_empty")]
52    pub raw_sql_tags: Vec<TrustedSqlTag>,
53    #[serde(skip_serializing_if = "Vec::is_empty")]
54    pub sql_identifier_assertions: Vec<SqlIdentifierAssertion>,
55    /// Exact module#export identities of annotation-only literal wrappers.
56    #[serde(skip_serializing_if = "Vec::is_empty")]
57    pub sql_literal_helpers: Vec<String>,
58    #[serde(skip)]
59    pub track_transaction_helpers: bool,
60    #[serde(skip)]
61    pub track_share_lock_writes: bool,
62    #[serde(skip)]
63    pub track_runtime_loop_appends: bool,
64    #[serde(skip)]
65    pub track_lookup_key_bindings: bool,
66    #[serde(skip)]
67    pub track_redundant_prelocks: bool,
68    #[doc(hidden)]
69    #[serde(skip)]
70    pub exact_constant_origins: bool,
71}
72impl EmbeddedSqlOptions {
73    /// A configured module enables standard executor names. Without a module,
74    /// names must be explicit and may match named imports from any module.
75    pub fn configured(import_specifier: &str, executor_names: &[String]) -> Self {
76        let mut names = if executor_names.is_empty() && !import_specifier.is_empty() {
77            DEFAULT_EXECUTOR_NAMES
78                .iter()
79                .map(|name| (*name).to_string())
80                .collect()
81        } else {
82            executor_names.to_vec()
83        };
84        names.sort();
85        names.dedup();
86        Self {
87            import_specifier: import_specifier.to_string(),
88            executor_names: names,
89            exclude_executor_names: Vec::new(),
90            executor_factory_names: Vec::new(),
91            executor_type_names: Vec::new(),
92            transaction_executor_type_names: Vec::new(),
93            executor_option_properties: Vec::new(),
94            trusted_sql_tags: Vec::new(),
95            raw_sql_tags: Vec::new(),
96            sql_identifier_assertions: Vec::new(),
97            sql_literal_helpers: Vec::new(),
98            track_transaction_helpers: false,
99            track_share_lock_writes: false,
100            track_runtime_loop_appends: false,
101            track_lookup_key_bindings: false,
102            track_redundant_prelocks: false,
103            exact_constant_origins: false,
104        }
105    }
106
107    /// Extend the standard executor imports while retaining all defaults.
108    pub fn with_extra_executor_names(mut self, extras: Option<&[String]>) -> Self {
109        if let Some(extras) = extras {
110            self.executor_names
111                .extend(DEFAULT_EXECUTOR_NAMES.iter().map(|name| (*name).into()));
112            self.executor_names.extend_from_slice(extras);
113            self.executor_names.sort();
114            self.executor_names.dedup();
115        }
116        self
117    }
118
119    /// Remove intentionally excluded imports after replacement or extension.
120    pub fn with_exclude_executor_names(mut self, exclusions: &[String]) -> Self {
121        self.exclude_executor_names = sorted_unique(exclusions);
122        self.executor_names
123            .retain(|name| !exclusions.contains(name));
124        self
125    }
126
127    /// Add scoped executor sources: factory imports and executor type imports.
128    pub fn with_scoped_executors(mut self, factories: &[String], types: &[String]) -> Self {
129        self.executor_factory_names = sorted_unique(factories);
130        self.executor_type_names = sorted_unique(types);
131        self
132    }
133
134    pub fn with_transaction_executor_types(mut self, types: &[String]) -> Self {
135        self.transaction_executor_type_names = sorted_unique(types);
136        self.executor_type_names.extend_from_slice(types);
137        self.executor_type_names.sort();
138        self.executor_type_names.dedup();
139        self
140    }
141
142    /// Select transaction executor overrides from explicit options properties.
143    pub fn with_executor_option_properties(mut self, properties: &[String]) -> Self {
144        self.executor_option_properties = sorted_unique(properties);
145        self
146    }
147
148    pub fn with_executor_options(mut self, options: &[ExecutorOption]) -> Self {
149        self.executor_option_properties
150            .extend(options.iter().map(|option| option.name.clone()));
151        self.executor_option_properties.sort();
152        self.executor_option_properties.dedup();
153        self
154    }
155
156    /// Configure annotation-only wrappers; argument syntax is checked separately.
157    pub fn with_sql_literal_helpers(mut self, helpers: &[String]) -> Self {
158        self.sql_literal_helpers = sorted_unique(helpers);
159        self
160    }
161
162    pub fn with_sql_identifier_assertions(mut self, assertions: &[SqlIdentifierAssertion]) -> Self {
163        self.sql_identifier_assertions = assertions.to_vec();
164        self.sql_identifier_assertions.sort();
165        self.sql_identifier_assertions.dedup();
166        self
167    }
168
169    pub fn with_raw_sql_tags(mut self, tags: &[TrustedSqlTag]) -> Self {
170        self.raw_sql_tags = tags.to_vec();
171        self.raw_sql_tags.sort();
172        self.raw_sql_tags.dedup();
173        self
174    }
175
176    pub fn with_exact_constant_origins(mut self) -> Self {
177        self.exact_constant_origins = true;
178        self
179    }
180
181    /// Opt in to parameterized tags. Order does not affect the profile key.
182    pub fn with_trusted_sql_tags(mut self, tags: &[TrustedSqlTag]) -> Self {
183        let mut tags = tags.to_vec();
184        tags.sort();
185        tags.dedup();
186        self.trusted_sql_tags = tags;
187        self
188    }
189}
190
191fn sorted_unique(names: &[String]) -> Vec<String> {
192    let mut names = names.to_vec();
193    names.sort();
194    names.dedup();
195    names
196}