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    #[doc(hidden)]
61    #[serde(skip)]
62    pub preserve_sql_receiver_identity: bool,
63    #[serde(skip)]
64    pub track_share_lock_writes: bool,
65    #[serde(skip)]
66    pub track_runtime_loop_appends: bool,
67    #[serde(skip)]
68    pub track_lookup_key_bindings: bool,
69    #[serde(skip)]
70    pub track_redundant_prelocks: bool,
71    #[doc(hidden)]
72    #[serde(skip)]
73    pub exact_constant_origins: bool,
74}
75impl EmbeddedSqlOptions {
76    /// A configured module enables standard executor names. Without a module,
77    /// names must be explicit and may match named imports from any module.
78    pub fn configured(import_specifier: &str, executor_names: &[String]) -> Self {
79        let mut names = if executor_names.is_empty() && !import_specifier.is_empty() {
80            DEFAULT_EXECUTOR_NAMES
81                .iter()
82                .map(|name| (*name).to_string())
83                .collect()
84        } else {
85            executor_names.to_vec()
86        };
87        names.sort();
88        names.dedup();
89        Self {
90            import_specifier: import_specifier.to_string(),
91            executor_names: names,
92            exclude_executor_names: Vec::new(),
93            executor_factory_names: Vec::new(),
94            executor_type_names: Vec::new(),
95            transaction_executor_type_names: Vec::new(),
96            executor_option_properties: Vec::new(),
97            trusted_sql_tags: Vec::new(),
98            raw_sql_tags: Vec::new(),
99            sql_identifier_assertions: Vec::new(),
100            sql_literal_helpers: Vec::new(),
101            track_transaction_helpers: false,
102            preserve_sql_receiver_identity: false,
103            track_share_lock_writes: false,
104            track_runtime_loop_appends: false,
105            track_lookup_key_bindings: false,
106            track_redundant_prelocks: false,
107            exact_constant_origins: false,
108        }
109    }
110
111    /// Extend the standard executor imports while retaining all defaults.
112    pub fn with_extra_executor_names(mut self, extras: Option<&[String]>) -> Self {
113        if let Some(extras) = extras {
114            self.executor_names
115                .extend(DEFAULT_EXECUTOR_NAMES.iter().map(|name| (*name).into()));
116            self.executor_names.extend_from_slice(extras);
117            self.executor_names.sort();
118            self.executor_names.dedup();
119        }
120        self
121    }
122
123    /// Remove intentionally excluded imports after replacement or extension.
124    pub fn with_exclude_executor_names(mut self, exclusions: &[String]) -> Self {
125        self.exclude_executor_names = sorted_unique(exclusions);
126        self.executor_names
127            .retain(|name| !exclusions.contains(name));
128        self
129    }
130
131    /// Add scoped executor sources: factory imports and executor type imports.
132    pub fn with_scoped_executors(mut self, factories: &[String], types: &[String]) -> Self {
133        self.executor_factory_names = sorted_unique(factories);
134        self.executor_type_names = sorted_unique(types);
135        self
136    }
137
138    pub fn with_transaction_executor_types(mut self, types: &[String]) -> Self {
139        self.transaction_executor_type_names = sorted_unique(types);
140        self.executor_type_names.extend_from_slice(types);
141        self.executor_type_names.sort();
142        self.executor_type_names.dedup();
143        self
144    }
145
146    /// Select transaction executor overrides from explicit options properties.
147    pub fn with_executor_option_properties(mut self, properties: &[String]) -> Self {
148        self.executor_option_properties = sorted_unique(properties);
149        self
150    }
151
152    pub fn with_executor_options(mut self, options: &[ExecutorOption]) -> Self {
153        self.executor_option_properties
154            .extend(options.iter().map(|option| option.name.clone()));
155        self.executor_option_properties.sort();
156        self.executor_option_properties.dedup();
157        self
158    }
159
160    /// Configure annotation-only wrappers; argument syntax is checked separately.
161    pub fn with_sql_literal_helpers(mut self, helpers: &[String]) -> Self {
162        self.sql_literal_helpers = sorted_unique(helpers);
163        self
164    }
165
166    pub fn with_sql_identifier_assertions(mut self, assertions: &[SqlIdentifierAssertion]) -> Self {
167        self.sql_identifier_assertions = assertions.to_vec();
168        self.sql_identifier_assertions.sort();
169        self.sql_identifier_assertions.dedup();
170        self
171    }
172
173    pub fn with_raw_sql_tags(mut self, tags: &[TrustedSqlTag]) -> Self {
174        self.raw_sql_tags = tags.to_vec();
175        self.raw_sql_tags.sort();
176        self.raw_sql_tags.dedup();
177        self
178    }
179
180    pub fn with_exact_constant_origins(mut self) -> Self {
181        self.exact_constant_origins = true;
182        self
183    }
184
185    /// Opt in to parameterized tags. Order does not affect the profile key.
186    pub fn with_trusted_sql_tags(mut self, tags: &[TrustedSqlTag]) -> Self {
187        let mut tags = tags.to_vec();
188        tags.sort();
189        tags.dedup();
190        self.trusted_sql_tags = tags;
191        self
192    }
193}
194
195fn sorted_unique(names: &[String]) -> Vec<String> {
196    let mut names = names.to_vec();
197    names.sort();
198    names.dedup();
199    names
200}