quack_rs/entry_point/
registration.rs1use libduckdb_sys::{duckdb_connection, duckdb_database};
11
12use crate::abi::AbiPolicy;
13use crate::error::ExtensionError;
14
15#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
19#[non_exhaustive]
20pub enum Registration {
21 #[default]
29 Transactional,
30 Autocommit,
37}
38
39#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
62#[must_use]
63pub struct EntryPointOptions {
64 abi_policy: AbiPolicy,
65 registration: Registration,
66}
67
68impl EntryPointOptions {
69 pub const fn new() -> Self {
71 Self {
72 abi_policy: AbiPolicy::Strict,
73 registration: Registration::Transactional,
74 }
75 }
76
77 pub const fn abi_policy(mut self, policy: AbiPolicy) -> Self {
79 self.abi_policy = policy;
80 self
81 }
82
83 pub const fn registration(mut self, registration: Registration) -> Self {
85 self.registration = registration;
86 self
87 }
88
89 #[must_use]
91 pub const fn get_abi_policy(&self) -> AbiPolicy {
92 self.abi_policy
93 }
94
95 #[must_use]
97 pub const fn get_registration(&self) -> Registration {
98 self.registration
99 }
100}
101
102impl From<AbiPolicy> for EntryPointOptions {
103 fn from(policy: AbiPolicy) -> Self {
105 Self::new().abi_policy(policy)
106 }
107}
108
109pub(super) unsafe fn run<F>(
122 con: duckdb_connection,
123 db: duckdb_database,
124 mode: Registration,
125 register: F,
126) -> Result<(), ExtensionError>
127where
128 F: FnOnce() -> Result<(), ExtensionError>,
129{
130 #[cfg(not(feature = "duckdb-1-5"))]
132 let _ = db;
133 if mode == Registration::Autocommit {
134 return super::catch_registration_panic(register);
135 }
136 unsafe { crate::query::execute(con, "BEGIN TRANSACTION") }.map_err(|e| {
138 ExtensionError::new(format!(
139 "could not begin the transaction the extension registers in: {e}"
140 ))
141 })?;
142 let result = {
143 #[cfg(feature = "duckdb-1-5")]
144 let _scope = Scope::enter(con, db);
145 super::catch_registration_panic(register)
146 };
147 match result {
148 Ok(()) => unsafe { crate::query::execute(con, "COMMIT") }
150 .map(drop)
151 .map_err(|e| {
152 ExtensionError::new(format!(
153 "the extension's registrations could not be committed, so none of them took \
154 effect: {e}"
155 ))
156 }),
157 Err(error) => {
158 let _ = unsafe { crate::query::execute(con, "ROLLBACK") };
162 Err(error)
163 }
164 }
165}
166
167#[cfg(feature = "duckdb-1-5")]
176struct ScopeState {
177 con: duckdb_connection,
178 db: duckdb_database,
179 copy_formats: Vec<String>,
180}
181
182#[cfg(feature = "duckdb-1-5")]
183std::thread_local! {
184 static SCOPE: core::cell::RefCell<Option<ScopeState>> =
185 const { core::cell::RefCell::new(None) };
186}
187
188#[cfg(feature = "duckdb-1-5")]
192struct Scope {
193 previous: Option<ScopeState>,
194}
195
196#[cfg(feature = "duckdb-1-5")]
197impl Scope {
198 fn enter(con: duckdb_connection, db: duckdb_database) -> Self {
199 let state = ScopeState {
200 con,
201 db,
202 copy_formats: Vec::new(),
203 };
204 let previous = SCOPE.with(|scope| scope.replace(Some(state)));
205 Self { previous }
206 }
207}
208
209#[cfg(feature = "duckdb-1-5")]
210impl Drop for Scope {
211 fn drop(&mut self) {
212 let previous = self.previous.take();
213 SCOPE.with(|scope| {
214 scope.replace(previous);
215 });
216 }
217}
218
219#[cfg(feature = "duckdb-1-5")]
223pub fn copy_format_scope(con: duckdb_connection, name: &str) -> Option<(duckdb_database, bool)> {
224 SCOPE.with(|scope| {
225 scope.borrow().as_ref().filter(|s| s.con == con).map(|s| {
226 let taken = s.copy_formats.iter().any(|f| f.eq_ignore_ascii_case(name));
227 (s.db, taken)
228 })
229 })
230}
231
232#[cfg(feature = "duckdb-1-5")]
235pub fn record_copy_format(con: duckdb_connection, name: &str) {
236 SCOPE.with(|scope| {
237 if let Some(s) = scope.borrow_mut().as_mut().filter(|s| s.con == con) {
238 s.copy_formats.push(name.to_owned());
239 }
240 });
241}
242
243#[cfg(test)]
244mod tests {
245 use super::{EntryPointOptions, Registration};
246 use crate::abi::AbiPolicy;
247
248 #[test]
249 fn options_default_to_strict_and_transactional() {
250 let options = EntryPointOptions::default();
251 assert_eq!(options, EntryPointOptions::new());
252 assert_eq!(options.get_abi_policy(), AbiPolicy::Strict);
253 assert_eq!(options.get_registration(), Registration::Transactional);
254 assert_eq!(Registration::default(), Registration::Transactional);
255 }
256
257 #[test]
258 fn a_policy_converts_to_options_with_the_default_registration() {
259 let options = EntryPointOptions::from(AbiPolicy::Trust);
260 assert_eq!(options.get_abi_policy(), AbiPolicy::Trust);
261 assert_eq!(options.get_registration(), Registration::Transactional);
262 let options = options.registration(Registration::Autocommit);
263 assert_eq!(options.get_abi_policy(), AbiPolicy::Trust);
264 assert_eq!(options.get_registration(), Registration::Autocommit);
265 }
266
267 #[cfg(feature = "duckdb-1-5")]
270 #[test]
271 fn the_scope_tracks_one_connection_and_nests() {
272 use super::{copy_format_scope, record_copy_format, Scope};
273 let (outer_con, inner_con) = (0x10 as _, 0x20 as _);
274 let (outer_db, inner_db) = (0x11 as _, 0x21 as _);
275 assert_eq!(copy_format_scope(outer_con, "fmt"), None);
276 {
277 let _outer = Scope::enter(outer_con, outer_db);
278 record_copy_format(outer_con, "MyFmt");
279 record_copy_format(inner_con, "elsewhere");
280 assert_eq!(
281 copy_format_scope(outer_con, "myfmt"),
282 Some((outer_db, true))
283 );
284 assert_eq!(
285 copy_format_scope(outer_con, "elsewhere"),
286 Some((outer_db, false))
287 );
288 assert_eq!(copy_format_scope(inner_con, "myfmt"), None);
289 {
290 let _inner = Scope::enter(inner_con, inner_db);
291 assert_eq!(
292 copy_format_scope(inner_con, "myfmt"),
293 Some((inner_db, false))
294 );
295 assert_eq!(copy_format_scope(outer_con, "myfmt"), None);
296 }
297 assert_eq!(
298 copy_format_scope(outer_con, "myfmt"),
299 Some((outer_db, true))
300 );
301 }
302 assert_eq!(copy_format_scope(outer_con, "myfmt"), None);
303 }
304}