ruprizzle 1.6.0

A schema-first ORM for Rust: typed queries, relations, and automatic migrations
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
//! Native `rusqlite` backend for the ruprizzle runtime.
//!
//! This module is compiled only when the `sqlite-rusqlite` feature is enabled.
//! It provides a synchronous, blocking-pinned SQLite connection pool that is
//! used instead of the `sqlx`-based SQLite backend when the connection URL
//! contains `driver=rusqlite`.

#![cfg(feature = "sqlite-rusqlite")]

use std::borrow::Cow;
use std::fmt;
use std::str::FromStr;
use std::sync::Arc;
use std::sync::atomic::{AtomicUsize, Ordering};
use std::time::{Duration, Instant};

use ::rusqlite::{self, OpenFlags, types::ValueRef};
use ruprizzle_core::ir::Provider;
use ruprizzle_dialect::dialect_for;

use crate::BoxFuture;
use crate::Error;
use crate::executor::{BoxRowStream, Executor, RowBatch};
use crate::pool::PoolConfig;
use crate::value::Value;

pub use ::rusqlite::{Row as RusqliteRow, types, types::Value as RusqliteValue};

/// A single row from the `rusqlite` backend.
///
/// Columns are stored in result-set order as `rusqlite::types::Value` so that
/// decoding can be implemented without holding a borrow of the live
/// `rusqlite::Row`. Column names are stored alongside the values so aggregate
/// result structs can decode by alias.
#[derive(Debug, Clone)]
pub struct Row {
    /// Column values in result-set order.
    pub values: Vec<RusqliteValue>,
    /// Column names in result-set order.
    pub names: Vec<String>,
}

/// A pool of synchronous `rusqlite` connections.
///
/// This type is intentionally cheap to clone and holds a shared set of
/// `std::sync::Mutex<rusqlite::Connection>` handles. Queries run synchronously
/// on the calling task to avoid the dispatch cost of `spawn_blocking`.
#[derive(Clone)]
pub struct RusqlitePool {
    inner: Arc<Inner>,
}

/// Checked-out connection handle that returns the connection on drop.
///
/// This lets one-shot `execute` and `fetch_all` paths share the same pool
/// lifecycle as transactions: the connection is removed from the pool for the
/// duration of the statement and returned afterwards.
struct ConnGuard<'a> {
    pool: &'a RusqlitePool,
    conn: Option<Arc<std::sync::Mutex<rusqlite::Connection>>>,
}

impl ConnGuard<'_> {
    fn conn(&self) -> &Arc<std::sync::Mutex<rusqlite::Connection>> {
        self.conn
            .as_ref()
            .unwrap_or_else(|| unreachable!("conn is present until consumed"))
    }
}

impl<'a> Drop for ConnGuard<'a> {
    fn drop(&mut self) {
        if let Some(conn) = self.conn.take() {
            self.pool.return_conn(conn);
        }
    }
}

struct Inner {
    conns: std::sync::Mutex<Vec<Arc<std::sync::Mutex<rusqlite::Connection>>>>,
    available: std::sync::Condvar,
    next: AtomicUsize,
    size: usize,
    waiters: AtomicUsize,
    /// Maximum time a caller blocks waiting for a free connection before
    /// [`Error::PoolExhausted`] is returned.
    acquire_timeout: Duration,
}

impl RusqlitePool {
    /// Open a new `rusqlite` pool from a SQLite URL and configuration.
    ///
    /// # Errors
    ///
    /// Returns an error if the URL is not a valid SQLite URL or a connection
    /// cannot be opened.
    pub async fn connect(url: &str, config: &PoolConfig) -> Result<Self, Error> {
        let url = url.to_owned();
        let config = config.clone();

        let inner = tokio::task::spawn_blocking(move || -> Result<Inner, Error> {
            // `driver=rusqlite` is a ruprizzle routing parameter that sqlx does
            // not understand, so strip it before parsing the SQLite URL.
            let sqlx_url = strip_driver_param(&url);
            let opts =
                sqlx::sqlite::SqliteConnectOptions::from_str(&sqlx_url).map_err(Error::Sqlx)?;
            let filename = opts.get_filename().to_string_lossy().into_owned();
            let capacity = config.max_connections.max(1) as usize;
            let mut conns = Vec::with_capacity(capacity);

            for _ in 0..capacity {
                let conn = if filename == ":memory:" {
                    rusqlite::Connection::open_in_memory()
                } else {
                    rusqlite::Connection::open_with_flags(
                        &filename,
                        OpenFlags::SQLITE_OPEN_READ_WRITE | OpenFlags::SQLITE_OPEN_CREATE,
                    )
                }
                .map_err(|e| Error::ConnectionFailure {
                    reason: e.to_string(),
                })?;

                // Use a long busy timeout so concurrent writers wait through
                // WAL checkpoints and transient contention rather than returning
                // SQLITE_BUSY. This is the primary defence against the `database
                // is locked` errors seen during long-running `rusqlite` soaks.
                conn.busy_timeout(Duration::from_secs(60)).map_err(|e| {
                    Error::ConnectionFailure {
                        reason: e.to_string(),
                    }
                })?;

                // SQLite leaves foreign keys off by default. Enabling them here
                // matches the sqlx-based SQLite backend and keeps relation tests
                // honest.
                conn.execute("PRAGMA foreign_keys = ON", []).map_err(|e| {
                    Error::ConnectionFailure {
                        reason: e.to_string(),
                    }
                })?;

                apply_pragmas(&conn, filename == ":memory:").map_err(|e| {
                    Error::ConnectionFailure {
                        reason: e.to_string(),
                    }
                })?;

                conns.push(Arc::new(std::sync::Mutex::new(conn)));
            }

            Ok(Inner {
                conns: std::sync::Mutex::new(conns),
                available: std::sync::Condvar::new(),
                next: AtomicUsize::new(0),
                size: capacity,
                waiters: AtomicUsize::new(0),
                acquire_timeout: config.acquire_timeout,
            })
        })
        .await
        .map_err(|e| Error::ConnectionFailure {
            reason: e.to_string(),
        })?;

        let pool = Self {
            inner: Arc::new(inner?),
        };

        tracing::info!(
            target: "ruprizzle::connection",
            event = "connect",
            backend = "rusqlite",
            "rusqlite pool opened"
        );

        Ok(pool)
    }

    /// Check out a connection from the pool, blocking until one is available.
    ///
    /// Callers must return the connection via [`ConnGuard`]'s `Drop` impl or
    /// by calling [`Self::return_conn`]. This replaces the previous round-robin
    /// "share a connection" model, which allowed `PoolExhausted` under heavy
    /// concurrent load because transactions removed connections while one-shot
    /// queries still expected to find one.
    fn checkout(&self) -> Result<ConnGuard<'_>, Error> {
        let conn = self.pop_conn()?;
        self.inner.next.fetch_add(1, Ordering::Relaxed);
        Ok(ConnGuard {
            pool: self,
            conn: Some(conn),
        })
    }

    /// Block until a connection is free, up to `acquire_timeout`.
    ///
    /// The wait is bounded: an unbounded `Condvar::wait` made
    /// [`Error::PoolExhausted`] unreachable and deadlocked callers forever when
    /// every connection was held by a live transaction.
    fn pop_conn(&self) -> Result<Arc<std::sync::Mutex<rusqlite::Connection>>, Error> {
        self.inner.waiters.fetch_add(1, Ordering::Relaxed);
        struct WaiterGuard<'a>(&'a Inner);
        impl Drop for WaiterGuard<'_> {
            fn drop(&mut self) {
                self.0.waiters.fetch_sub(1, Ordering::Relaxed);
            }
        }
        let _guard = WaiterGuard(&self.inner);

        let poisoned = || Error::Message("rusqlite connection pool mutex poisoned".into());
        let exhausted = || Error::PoolExhausted {
            backend: "rusqlite",
        };

        let mut conns = self.inner.conns.lock().map_err(|_| poisoned())?;
        let deadline = Instant::now() + self.inner.acquire_timeout;
        while conns.is_empty() {
            let remaining = deadline.saturating_duration_since(Instant::now());
            if remaining.is_zero() {
                return Err(exhausted());
            }
            let (guard, wait) = self
                .inner
                .available
                .wait_timeout(conns, remaining)
                .map_err(|_| poisoned())?;
            conns = guard;
            if wait.timed_out() && conns.is_empty() {
                return Err(exhausted());
            }
        }
        conns.pop().ok_or_else(exhausted)
    }

    /// Number of connections in the pool.
    #[must_use]
    pub fn num_total(&self) -> usize {
        self.inner.size
    }

    /// Number of idle connections available for checkout.
    #[must_use]
    pub fn num_idle(&self) -> usize {
        self.inner.conns.try_lock().map_or(0, |conns| conns.len())
    }

    /// Number of threads currently waiting for a connection.
    #[must_use]
    pub fn num_waiters(&self) -> usize {
        self.inner.waiters.load(Ordering::Relaxed)
    }

    /// Check out a connection and take ownership of it (for transactions).
    fn checkout_owned(&self) -> Result<Arc<std::sync::Mutex<rusqlite::Connection>>, Error> {
        self.pop_conn()
    }

    /// Return an owned connection to the pool.
    fn return_conn(&self, conn: Arc<std::sync::Mutex<rusqlite::Connection>>) {
        if let Ok(mut conns) = self.inner.conns.lock() {
            conns.push(conn);
        }
        self.inner.available.notify_one();
    }

    /// Take a connection from the pool and start a transaction on it.
    ///
    /// The connection is removed from the pool until the transaction is
    /// committed, rolled back, or dropped, guaranteeing that all transaction
    /// statements run on the same physical SQLite connection.
    pub(crate) async fn begin_transaction(&self) -> Result<RusqliteTransaction, Error> {
        let pool = self.clone();
        tokio::task::spawn_blocking(move || -> Result<RusqliteTransaction, Error> {
            let conn = pool.checkout_owned()?;

            // The connection is out of the pool from here on, and there is no
            // `RusqliteTransaction` yet to drop it back in, so `?` would leak it
            // exactly the way an abandoned transaction used to (BUG-01).
            let started = (|| -> Result<(), Error> {
                let guard = conn
                    .lock()
                    .map_err(|_| Error::Message("rusqlite connection mutex poisoned".into()))?;
                guard
                    .execute("BEGIN", [])
                    .map_err(|e| Error::Message(e.to_string()))?;
                // Each transaction can be the first operation on a connection
                // after another connection changed the schema; refresh so that
                // the transaction sees the current schema.
                force_schema_reload(&guard);
                Ok(())
            })();

            if let Err(error) = started {
                pool.return_conn(conn);
                return Err(error);
            }

            Ok(RusqliteTransaction {
                pool,
                conn: Some(conn),
            })
        })
        .await
        .map_err(|e| Error::Message(e.to_string()))?
    }

    /// Asynchronously fetch and decode rows directly into `Vec<T>`.
    ///
    /// This is the fast path used by `SelectQuery` when running against the
    /// native `rusqlite` backend. The synchronous `rusqlite` work is offloaded
    /// to a blocking thread so the tokio runtime is not pinned during WAL
    /// contention or pool waits.
    pub(crate) async fn fetch_all_sync_decoded<T>(
        &self,
        sql: Cow<'static, str>,
        binds: Vec<Value>,
    ) -> Result<Vec<T>, Error>
    where
        T: FromRusqliteRow + Send + 'static,
    {
        let pool = self.clone();
        tokio::task::spawn_blocking(move || {
            let conn = pool.checkout()?;

            let guard = conn
                .conn()
                .lock()
                .map_err(|_| Error::Message("rusqlite connection mutex poisoned".into()))?;
            if is_ddl(sql.as_ref()) {
                force_schema_reload(&guard);
            }
            let mut stmt = guard
                .prepare_cached(sql.as_ref())
                .map_err(|e| Error::Message(e.to_string()))?;

            let mut out = Vec::new();

            {
                let mut rows = stmt
                    .query(rusqlite::params_from_iter(&binds))
                    .map_err(|e| Error::Message(e.to_string()))?;
                while let Some(row) = rows.next().map_err(|e| Error::Message(e.to_string()))? {
                    out.push(T::from_rusqlite_row(row)?);
                }
            }

            if is_ddl(sql.as_ref()) {
                stmt.discard();
                guard.flush_prepared_statement_cache();
            }

            Ok(out)
        })
        .await
        .map_err(|e| Error::Message(e.to_string()))?
    }
}

/// A `rusqlite` transaction that owns its connection.
///
/// The connection is removed from the [`RusqlitePool`] for the lifetime of the
/// transaction and is returned by [`Self::commit`], [`Self::rollback`], or —
/// for a transaction abandoned without either — the [`Drop`] impl below.
///
/// `conn` is an `Option` purely so `Drop` can distinguish a transaction that
/// was finished explicitly (`None`) from one that was abandoned (`Some`);
/// `commit`/`rollback` consume `self`, so without it `Drop` would run on a
/// finished transaction and return the same connection twice.
///
/// **This type must never be `Clone`.** A transaction owns its connection
/// exclusively, and two handles to one connection would each return it to the
/// pool, after which `begin_transaction` could hand the same physical
/// connection — with one `BEGIN` on it — to two callers who both believe they
/// hold it alone. It carried a `Clone` derive until BUG-06; nothing needed it.
#[derive(Debug)]
pub(crate) struct RusqliteTransaction {
    pool: RusqlitePool,
    conn: Option<Arc<std::sync::Mutex<rusqlite::Connection>>>,
}

impl RusqliteTransaction {
    /// The connection this transaction owns, or an error if it has finished.
    fn conn(&self) -> Result<&Arc<std::sync::Mutex<rusqlite::Connection>>, Error> {
        self.conn
            .as_ref()
            .ok_or_else(|| Error::Message("transaction already finished".into()))
    }

    /// Execute `sql` with `binds`, returning the number of affected rows.
    pub(crate) fn execute_sync(&self, sql: &str, binds: &[Value]) -> Result<u64, Error> {
        let guard = self
            .conn()?
            .lock()
            .map_err(|_| Error::Message("rusqlite connection mutex poisoned".into()))?;
        if is_ddl(sql) {
            force_schema_reload(&guard);
        }
        let mut stmt = guard
            .prepare_cached(sql)
            .map_err(|e| Error::Message(e.to_string()))?;

        let rows = stmt
            .execute(rusqlite::params_from_iter(binds))
            .map_err(|e| Error::Message(e.to_string()))?;

        if is_ddl(sql) {
            stmt.discard();
            guard.flush_prepared_statement_cache();
        }

        Ok(rows as u64)
    }

    /// Fetch all rows from `sql` with `binds`.
    pub(crate) fn fetch_all_sync(&self, sql: &str, binds: &[Value]) -> Result<RowBatch, Error> {
        let guard = self
            .conn()?
            .lock()
            .map_err(|_| Error::Message("rusqlite connection mutex poisoned".into()))?;
        if is_ddl(sql) {
            force_schema_reload(&guard);
        }
        let mut stmt = guard
            .prepare_cached(sql)
            .map_err(|e| Error::Message(e.to_string()))?;

        let column_count = stmt.column_count();
        let column_names: Vec<String> = stmt
            .column_names()
            .into_iter()
            .map(|s| s.to_string())
            .collect();

        let rows = stmt
            .query_map(rusqlite::params_from_iter(binds), |row| {
                let mut values = Vec::with_capacity(column_count);
                for i in 0..column_count {
                    values.push(row.get::<_, RusqliteValue>(i)?);
                }
                Ok(Row {
                    values,
                    names: column_names.clone(),
                })
            })
            .map_err(|e| Error::Message(e.to_string()))?
            .collect::<Result<Vec<_>, _>>()
            .map_err(|e| Error::Message(e.to_string()))?;

        if is_ddl(sql) {
            stmt.discard();
            guard.flush_prepared_statement_cache();
        }

        Ok(RowBatch::Rusqlite(rows))
    }

    /// Commit the transaction and return the connection to the pool.
    pub(crate) fn commit(mut self) -> Result<(), Error> {
        self.finish("COMMIT")
    }

    /// Roll the transaction back and return the connection to the pool.
    pub(crate) fn rollback(mut self) -> Result<(), Error> {
        self.finish("ROLLBACK")
    }

    /// Run `stmt` (`COMMIT` or `ROLLBACK`) and hand the connection back.
    ///
    /// The connection is taken out of the `Option` first, so it is returned to
    /// the pool exactly once and on every path — including a failed `COMMIT`.
    /// Leaking it on the failure path is half of what BUG-01 was.
    fn finish(&mut self, stmt: &'static str) -> Result<(), Error> {
        let Some(conn) = self.conn.take() else {
            return Err(Error::Message("transaction already finished".into()));
        };

        let result = end_transaction(&conn, stmt);
        self.pool.return_conn(conn);
        result
    }
}

/// Roll back and return the connection when a transaction is abandoned.
///
/// `sqlx::Transaction` does this for the `sqlx` backends; without it, dropping
/// a transaction — which `?` does on every early return — removed a connection
/// from the pool permanently (BUG-01).
///
/// This must never panic: a `Drop` that panics during an existing unwind aborts
/// the process. Every failure path here logs and continues, and the connection
/// goes back to the pool regardless.
impl Drop for RusqliteTransaction {
    fn drop(&mut self) {
        let Some(conn) = self.conn.take() else {
            return;
        };

        tracing::warn!(
            target: "ruprizzle::query",
            "transaction dropped without commit or rollback; rolling back"
        );

        if let Err(error) = end_transaction(&conn, "ROLLBACK") {
            tracing::warn!(
                target: "ruprizzle::query",
                error = %error,
                "failed to roll back an abandoned transaction"
            );
        }

        self.pool.return_conn(conn);
    }
}

/// Ends the transaction on `conn` with `stmt`, leaving no transaction open on
/// the connection whatever the outcome.
///
/// This is the last thing to touch a connection before it goes back into the
/// pool, so it must not leave one mid-transaction: a failed `COMMIT` does not
/// end the transaction in SQLite, and handing that connection to the next
/// caller would put their statements inside this transaction.
///
/// Never panics — it is called from `Drop`.
fn end_transaction(
    conn: &std::sync::Mutex<rusqlite::Connection>,
    stmt: &'static str,
) -> Result<(), Error> {
    // A poisoned mutex means another task panicked while holding it. The guard
    // is still recoverable, and ending the transaction matters more here than
    // honouring the poison flag.
    let guard = match conn.lock() {
        Ok(guard) => guard,
        Err(poisoned) => {
            tracing::warn!(
                target: "ruprizzle::query",
                "ending a transaction on a poisoned connection"
            );
            poisoned.into_inner()
        }
    };

    let result = guard
        .execute(stmt, [])
        .map(|_| ())
        .map_err(|e| Error::Message(e.to_string()));

    if result.is_err() && stmt != "ROLLBACK" {
        let _ = guard.execute("ROLLBACK", []);
    }

    // DDL can invalidate cached prepared statements. Flush before the
    // connection is reused so the next statement recompiles against the
    // current schema.
    guard.flush_prepared_statement_cache();

    result
}

impl fmt::Debug for RusqlitePool {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let len = self.inner.conns.try_lock().map_or(0, |conns| conns.len());
        f.debug_struct("RusqlitePool")
            .field("connections", &len)
            .finish_non_exhaustive()
    }
}

impl fmt::Debug for Inner {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let len = self.conns.try_lock().ok().map_or(0, |conns| conns.len());
        f.debug_struct("Inner")
            .field("connections", &len)
            .finish_non_exhaustive()
    }
}

impl Executor for RusqlitePool {
    fn dialect(&self) -> &dyn ruprizzle_dialect::DbDialect {
        dialect_for(Provider::Sqlite)
    }

    fn as_rusqlite(&self) -> Option<&Self> {
        Some(self)
    }

    fn fetch_all_raw(
        &self,
        sql: Cow<'static, str>,
        binds: Vec<Value>,
    ) -> BoxFuture<'_, Result<RowBatch, Error>> {
        let pool = self.clone();
        Box::pin(async move {
            tokio::task::spawn_blocking(move || fetch_all(pool, sql, binds))
                .await
                .map_err(|e| Error::Message(e.to_string()))?
        })
    }

    fn execute_raw(
        &self,
        sql: Cow<'static, str>,
        binds: Vec<Value>,
    ) -> BoxFuture<'_, Result<u64, Error>> {
        let pool = self.clone();
        Box::pin(async move {
            tokio::task::spawn_blocking(move || execute(pool, sql, binds))
                .await
                .map_err(|e| Error::Message(e.to_string()))?
        })
    }

    fn stream_raw(&self, sql: Cow<'static, str>, binds: Vec<Value>) -> BoxRowStream<'_> {
        Box::pin(crate::executor::DeferredRowStream::new(Box::pin(
            async move { self.fetch_all_raw(sql, binds).await },
        )))
    }
}

fn fetch_all(
    pool: RusqlitePool,
    sql: Cow<'static, str>,
    binds: Vec<Value>,
) -> Result<RowBatch, Error> {
    let conn = pool.checkout()?;

    let guard = conn
        .conn()
        .lock()
        .map_err(|_| Error::Message("rusqlite connection mutex poisoned".into()))?;
    if is_ddl(sql.as_ref()) {
        force_schema_reload(&guard);
    }
    let mut stmt = guard
        .prepare_cached(sql.as_ref())
        .map_err(|e| Error::Message(e.to_string()))?;

    let column_count = stmt.column_count();
    let column_names: Vec<String> = stmt
        .column_names()
        .into_iter()
        .map(|s| s.to_string())
        .collect();

    let rows = stmt
        .query_map(rusqlite::params_from_iter(&binds), |row| {
            let mut values = Vec::with_capacity(column_count);
            for i in 0..column_count {
                values.push(row.get::<_, RusqliteValue>(i)?);
            }
            Ok(Row {
                values,
                names: column_names.clone(),
            })
        })
        .map_err(|e| Error::Message(e.to_string()))?
        .collect::<Result<Vec<_>, _>>()
        .map_err(|e| Error::Message(e.to_string()))?;

    if is_ddl(sql.as_ref()) {
        stmt.discard();
        guard.flush_prepared_statement_cache();
    }

    Ok(RowBatch::Rusqlite(rows))
}

fn execute(pool: RusqlitePool, sql: Cow<'static, str>, binds: Vec<Value>) -> Result<u64, Error> {
    let conn = pool.checkout()?;

    let guard = conn
        .conn()
        .lock()
        .map_err(|_| Error::Message("rusqlite connection mutex poisoned".into()))?;
    if is_ddl(sql.as_ref()) {
        force_schema_reload(&guard);
    }
    let mut stmt = guard
        .prepare_cached(sql.as_ref())
        .map_err(|e| Error::Message(e.to_string()))?;

    let rows = stmt
        .execute(rusqlite::params_from_iter(&binds))
        .map_err(|e| Error::Message(e.to_string()))?;

    if is_ddl(sql.as_ref()) {
        stmt.discard();
        guard.flush_prepared_statement_cache();
    }

    Ok(rows as u64)
}

impl Row {
    /// Number of columns in the row.
    #[must_use]
    pub fn len(&self) -> usize {
        self.values.len()
    }

    /// Returns `true` if the row has no columns.
    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.values.is_empty()
    }

    /// Decode the column at `idx` into `T`.
    ///
    /// # Errors
    ///
    /// Returns [`Error::Message`] if the index is out of bounds or the value
    /// cannot be decoded into `T`.
    pub fn get<T: FromValue>(&self, idx: usize) -> Result<T, Error> {
        let value = self
            .values
            .get(idx)
            .ok_or_else(|| Error::Message(format!("column index {idx} out of bounds")))?;
        T::from_value(value)
    }

    /// Decode the column named `name` into `T`, or return the default if the
    /// column is not present.
    ///
    /// This is used by aggregate result structs where the selected columns are a
    /// subset of the generated struct's fields.
    pub fn get_by_name<T: FromValue>(&self, name: &str) -> Result<T, Error> {
        match self.names.iter().position(|n| n == name) {
            Some(idx) => self.get::<T>(idx),
            None => T::from_value(&RusqliteValue::Null),
        }
    }
}

/// A type that can be decoded from a live `rusqlite::Row`.
pub trait FromRusqliteRow: Sized + Send + Sync + 'static {
    /// Decode `self` from an ordered row of `rusqlite` values.
    ///
    /// # Errors
    ///
    /// Returns [`Error::Message`] if the row cannot be decoded.
    fn from_rusqlite_row(row: &RusqliteRow) -> Result<Self, Error>;
}

/// A type that can be decoded from a stored [`Row`].
pub trait FromOwnedRow: Sized + Send + Sync + 'static {
    /// Decode `self` from a stored [`Row`].
    ///
    /// # Errors
    ///
    /// Returns [`Error::Message`] if the row cannot be decoded.
    fn from_owned_row(row: &Row) -> Result<Self, Error>;
}

/// A type that can be decoded from a single `rusqlite::types::Value`.
pub trait FromValue: Sized + Send + Sync + 'static {
    /// Decode `self` from a single `rusqlite` value.
    ///
    /// # Errors
    ///
    /// Returns [`Error::Message`] if the value cannot be decoded into `T`.
    fn from_value(value: &RusqliteValue) -> Result<Self, Error>;
}

impl FromValue for i64 {
    fn from_value(value: &RusqliteValue) -> Result<Self, Error> {
        match *value {
            RusqliteValue::Integer(i) => Ok(i),
            RusqliteValue::Real(f) => Ok(f as i64),
            RusqliteValue::Text(ref s) => s
                .parse()
                .map_err(|e| Error::Message(format!("cannot decode i64 from {s:?}: {e}"))),
            _ => Err(Error::Message(format!("cannot decode i64 from {value:?}"))),
        }
    }
}

impl FromValue for i32 {
    fn from_value(value: &RusqliteValue) -> Result<Self, Error> {
        i64::from_value(value)?
            .try_into()
            .map_err(|e| Error::Message(format!("cannot decode i32 from integer: {e}")))
    }
}

impl FromValue for f64 {
    fn from_value(value: &RusqliteValue) -> Result<Self, Error> {
        match value {
            &RusqliteValue::Real(f) => Ok(f),
            &RusqliteValue::Integer(i) => Ok(i as f64),
            RusqliteValue::Text(s) => s
                .parse()
                .map_err(|e| Error::Message(format!("cannot decode f64 from {s:?}: {e}"))),
            _ => Err(Error::Message(format!("cannot decode f64 from {value:?}"))),
        }
    }
}

impl FromValue for bool {
    fn from_value(value: &RusqliteValue) -> Result<Self, Error> {
        match value {
            &RusqliteValue::Integer(0) => Ok(false),
            &RusqliteValue::Integer(1) => Ok(true),
            RusqliteValue::Text(s) if s == "0" || s.eq_ignore_ascii_case("false") => Ok(false),
            RusqliteValue::Text(s) if s == "1" || s.eq_ignore_ascii_case("true") => Ok(true),
            _ => Err(Error::Message(format!("cannot decode bool from {value:?}"))),
        }
    }
}

impl FromValue for String {
    fn from_value(value: &RusqliteValue) -> Result<Self, Error> {
        match value {
            RusqliteValue::Text(s) => Ok(s.clone()),
            &RusqliteValue::Integer(i) => Ok(i.to_string()),
            &RusqliteValue::Real(f) => Ok(f.to_string()),
            &RusqliteValue::Blob(_) => Err(Error::Message("cannot decode String from blob".into())),
            &RusqliteValue::Null => Err(Error::Message("cannot decode String from NULL".into())),
        }
    }
}

impl FromValue for Vec<u8> {
    fn from_value(value: &RusqliteValue) -> Result<Self, Error> {
        match value {
            RusqliteValue::Blob(b) => Ok(b.clone()),
            _ => Err(Error::Message(format!(
                "cannot decode Vec<u8> from {value:?}"
            ))),
        }
    }
}

impl FromValue for crate::types::Decimal {
    fn from_value(value: &RusqliteValue) -> Result<Self, Error> {
        let s = String::from_value(value)?;
        s.parse()
            .map_err(|e| Error::Message(format!("cannot decode Decimal from {s:?}: {e}")))
    }
}

impl FromValue for crate::types::Uuid {
    fn from_value(value: &RusqliteValue) -> Result<Self, Error> {
        let s = String::from_value(value)?;
        s.parse()
            .map_err(|e| Error::Message(format!("cannot decode Uuid from {s:?}: {e}")))
    }
}

impl FromValue for crate::types::chrono::DateTime<crate::types::chrono::Utc> {
    fn from_value(value: &RusqliteValue) -> Result<Self, Error> {
        let s = String::from_value(value)?;
        s.parse()
            .map_err(|e| Error::Message(format!("cannot decode DateTime from {s:?}: {e}")))
    }
}

impl FromValue for crate::types::chrono::NaiveDate {
    fn from_value(value: &RusqliteValue) -> Result<Self, Error> {
        let s = String::from_value(value)?;
        s.parse()
            .map_err(|e| Error::Message(format!("cannot decode NaiveDate from {s:?}: {e}")))
    }
}

impl FromValue for crate::types::chrono::NaiveTime {
    fn from_value(value: &RusqliteValue) -> Result<Self, Error> {
        let s = String::from_value(value)?;
        s.parse()
            .map_err(|e| Error::Message(format!("cannot decode NaiveTime from {s:?}: {e}")))
    }
}

impl FromValue for serde_json::Value {
    fn from_value(value: &RusqliteValue) -> Result<Self, Error> {
        let s = String::from_value(value)?;
        serde_json::from_str(&s)
            .map_err(|e| Error::Message(format!("cannot decode JSON from {s:?}: {e}")))
    }
}

impl<T: FromValue + crate::serde::de::DeserializeOwned> FromValue for Vec<T> {
    fn from_value(value: &RusqliteValue) -> Result<Self, Error> {
        let s = String::from_value(value)?;
        serde_json::from_str(&s)
            .map_err(|e| Error::Message(format!("cannot decode array from {s:?}: {e}")))
    }
}

impl<T: FromValue> FromValue for Option<T> {
    fn from_value(value: &RusqliteValue) -> Result<Self, Error> {
        match value {
            &RusqliteValue::Null => Ok(None),
            _ => T::from_value(value).map(Some),
        }
    }
}

/// Decode a column into `T` by copying it into an owned `rusqlite::types::Value`.
pub fn get<T: FromValue>(row: &RusqliteRow, idx: usize) -> Result<T, Error> {
    let value = row
        .get::<_, RusqliteValue>(idx)
        .map_err(|e| Error::Message(e.to_string()))?;
    T::from_value(&value)
}

/// Decode an `INTEGER` column.
pub fn get_i64(row: &RusqliteRow, idx: usize) -> Result<i64, Error> {
    row.get::<_, i64>(idx)
        .map_err(|e| Error::Message(e.to_string()))
}

/// Decode an optional `INTEGER` column.
pub fn get_i64_opt(row: &RusqliteRow, idx: usize) -> Result<Option<i64>, Error> {
    row.get::<_, Option<i64>>(idx)
        .map_err(|e| Error::Message(e.to_string()))
}

/// Decode a `REAL` or `INTEGER` column as `f64`.
pub fn get_f64(row: &RusqliteRow, idx: usize) -> Result<f64, Error> {
    row.get::<_, f64>(idx)
        .map_err(|e| Error::Message(e.to_string()))
}

/// Decode an optional `REAL` or `INTEGER` column as `f64`.
pub fn get_f64_opt(row: &RusqliteRow, idx: usize) -> Result<Option<f64>, Error> {
    row.get::<_, Option<f64>>(idx)
        .map_err(|e| Error::Message(e.to_string()))
}

/// Decode a boolean column.
pub fn get_bool(row: &RusqliteRow, idx: usize) -> Result<bool, Error> {
    match row
        .get_ref(idx)
        .map_err(|e| Error::Message(e.to_string()))?
    {
        ValueRef::Null => Err(Error::Message("cannot decode bool from NULL".into())),
        ValueRef::Integer(0) => Ok(false),
        ValueRef::Integer(1) => Ok(true),
        ValueRef::Text(s) => {
            let s = std::str::from_utf8(s).map_err(|e| Error::Message(e.to_string()))?;
            if s == "0" || s.eq_ignore_ascii_case("false") {
                Ok(false)
            } else if s == "1" || s.eq_ignore_ascii_case("true") {
                Ok(true)
            } else {
                Err(Error::Message(format!("cannot decode bool from {s:?}")))
            }
        }
        v => Err(Error::Message(format!("cannot decode bool from {v:?}"))),
    }
}

/// Decode an optional boolean column.
pub fn get_bool_opt(row: &RusqliteRow, idx: usize) -> Result<Option<bool>, Error> {
    match row
        .get_ref(idx)
        .map_err(|e| Error::Message(e.to_string()))?
    {
        ValueRef::Null => Ok(None),
        ValueRef::Integer(0) => Ok(Some(false)),
        ValueRef::Integer(1) => Ok(Some(true)),
        ValueRef::Text(s) => {
            let s = std::str::from_utf8(s).map_err(|e| Error::Message(e.to_string()))?;
            if s == "0" || s.eq_ignore_ascii_case("false") {
                Ok(Some(false))
            } else if s == "1" || s.eq_ignore_ascii_case("true") {
                Ok(Some(true))
            } else {
                Err(Error::Message(format!("cannot decode bool from {s:?}")))
            }
        }
        v => Err(Error::Message(format!("cannot decode bool from {v:?}"))),
    }
}

/// Decode a `TEXT` column into a `String`.
pub fn get_text(row: &RusqliteRow, idx: usize) -> Result<String, Error> {
    row.get::<_, String>(idx)
        .map_err(|e| Error::Message(e.to_string()))
}

/// Decode an optional `TEXT` column.
pub fn get_text_opt(row: &RusqliteRow, idx: usize) -> Result<Option<String>, Error> {
    row.get::<_, Option<String>>(idx)
        .map_err(|e| Error::Message(e.to_string()))
}

/// Decode a `BLOB` column into `Vec<u8>`.
pub fn get_bytes(row: &RusqliteRow, idx: usize) -> Result<Vec<u8>, Error> {
    row.get::<_, Vec<u8>>(idx)
        .map_err(|e| Error::Message(e.to_string()))
}

/// Decode an optional `BLOB` column.
pub fn get_bytes_opt(row: &RusqliteRow, idx: usize) -> Result<Option<Vec<u8>>, Error> {
    row.get::<_, Option<Vec<u8>>>(idx)
        .map_err(|e| Error::Message(e.to_string()))
}

/// Parse a `TEXT` column into `T` using `FromStr`.
pub fn parse<T>(row: &RusqliteRow, idx: usize) -> Result<T, Error>
where
    T: FromStr,
    T::Err: fmt::Display,
{
    let s = row
        .get_ref(idx)
        .map_err(|e| Error::Message(e.to_string()))?
        .as_str()
        .map_err(|e| Error::Message(e.to_string()))?;
    s.parse::<T>()
        .map_err(|e| Error::Message(format!("cannot parse column {idx}: {e}")))
}

/// Parse an optional `TEXT` column into `Option<T>`.
pub fn parse_opt<T>(row: &RusqliteRow, idx: usize) -> Result<Option<T>, Error>
where
    T: FromStr,
    T::Err: fmt::Display,
{
    row.get_ref(idx)
        .map_err(|e| Error::Message(e.to_string()))?
        .as_str_or_null()
        .map_err(|e| Error::Message(e.to_string()))?
        .map(|s| {
            s.parse::<T>()
                .map_err(|e| Error::Message(format!("cannot parse column {idx}: {e}")))
        })
        .transpose()
}

/// Decode a JSON column.
pub fn get_json(row: &RusqliteRow, idx: usize) -> Result<serde_json::Value, Error> {
    let s = row
        .get_ref(idx)
        .map_err(|e| Error::Message(e.to_string()))?
        .as_str()
        .map_err(|e| Error::Message(e.to_string()))?;
    serde_json::from_str(s)
        .map_err(|e| Error::Message(format!("cannot decode JSON from column {idx}: {e}")))
}

/// Decode an optional JSON column.
pub fn get_json_opt(row: &RusqliteRow, idx: usize) -> Result<Option<serde_json::Value>, Error> {
    match row
        .get_ref(idx)
        .map_err(|e| Error::Message(e.to_string()))?
        .as_str_or_null()
        .map_err(|e| Error::Message(e.to_string()))?
    {
        None => Ok(None),
        Some(s) => serde_json::from_str(s)
            .map(Some)
            .map_err(|e| Error::Message(format!("cannot decode JSON from column {idx}: {e}"))),
    }
}

macro_rules! tuple_from_value {
    ($($T:ident $idx:tt),+) => {
        impl<$($T: FromValue),+> FromRusqliteRow for ($($T,)+) {
            fn from_rusqlite_row(row: &RusqliteRow) -> Result<Self, Error> {
                Ok((
                    $(
                        {
                            let value = row
                                .get::<_, RusqliteValue>($idx)
                                .map_err(|e| Error::Message(e.to_string()))?;
                            $T::from_value(&value)?
                        }
                    ,)+
                ))
            }
        }

        impl<$($T: FromValue),+> FromOwnedRow for ($($T,)+) {
            fn from_owned_row(row: &Row) -> Result<Self, Error> {
                Ok((
                    $(
                        $T::from_value(&row.values[$idx])?
                    ,)+
                ))
            }
        }
    };
}

tuple_from_value! { A 0 }
tuple_from_value! { A 0, B 1 }
tuple_from_value! { A 0, B 1, C 2 }
tuple_from_value! { A 0, B 1, C 2, D 3 }
tuple_from_value! { A 0, B 1, C 2, D 3, E 4 }
tuple_from_value! { A 0, B 1, C 2, D 3, E 4, F 5 }
tuple_from_value! { A 0, B 1, C 2, D 3, E 4, F 5, G 6 }
tuple_from_value! { A 0, B 1, C 2, D 3, E 4, F 5, G 6, H 7 }
tuple_from_value! { A 0, B 1, C 2, D 3, E 4, F 5, G 6, H 7, I 8 }
tuple_from_value! { A 0, B 1, C 2, D 3, E 4, F 5, G 6, H 7, I 8, J 9 }
tuple_from_value! { A 0, B 1, C 2, D 3, E 4, F 5, G 6, H 7, I 8, J 9, K 10 }
tuple_from_value! { A 0, B 1, C 2, D 3, E 4, F 5, G 6, H 7, I 8, J 9, K 10, L 11 }

/// Apply performance and correctness PRAGMAs to a fresh `rusqlite` connection.
///
/// WAL mode, a relaxed synchronous setting and a larger page/mmap cache are
/// standard SQLite tuning for local-file workloads.  They are applied per
/// connection; WAL is persistent once set on the database file.
fn apply_pragmas(conn: &rusqlite::Connection, is_memory: bool) -> Result<(), rusqlite::Error> {
    // WAL is not meaningful for in-memory databases and may error.
    if !is_memory {
        // `journal_mode` returns a row, so we need the checking variant.
        let _ = conn.pragma_update_and_check(None, "journal_mode", "WAL", |_| Ok(()));
        // Checkpoint less frequently than the default 1000 pages. The long soak
        // segments run their own explicit TRUNCATE checkpoint at the start of
        // each segment, so mid-segment auto-checkpoints should be large and rare.
        let _ = conn.pragma_update(None, "wal_autocheckpoint", 10_000i64);
    }

    conn.pragma_update(None, "synchronous", "NORMAL")?;
    conn.pragma_update(None, "temp_store", "MEMORY")?;
    conn.pragma_update(None, "cache_size", -64_000i64)?;
    conn.pragma_update(None, "mmap_size", 268_435_456i64)?;

    // Allow more prepared statements to stay live across queries; the DDL-safe
    // flush logic in commit/rollback keeps the cache consistent with schema
    // changes.
    conn.set_prepared_statement_cache_capacity(256);

    Ok(())
}

/// Force `rusqlite` to reload its schema cache for the current connection.
///
/// SQLite's schema cookie is checked when a statement is prepared, but because
/// the native backend may use one connection for a DDL write and a different
/// connection for a subsequent DDL read, the read connection can have a stale
/// schema view.  Querying `sqlite_master` forces a full schema re-parse before
/// the real DDL is prepared.
fn force_schema_reload(conn: &rusqlite::Connection) {
    // The result is unimportant; an empty database simply returns no rows.
    // `query_row` will trigger `sqlite3_prepare_v2()` and `sqlite3_step()`,
    // which is enough to refresh the in-memory schema tables when the schema
    // cookie has changed.
    let _ = conn.query_row("SELECT name FROM sqlite_master LIMIT 1", [], |_| Ok(()));
}

/// Heuristic for whether a statement may change the database schema.
fn is_ddl(sql: &str) -> bool {
    let mut s = sql.trim_start();
    // Skip leading single-line SQL comments so we don't misclassify a
    // commented-out DDL statement as a schema change.
    while s.starts_with("-- ") {
        if let Some(nl) = s.find('\n') {
            s = &s[nl + 1..];
        } else {
            return false;
        }
        s = s.trim_start();
    }

    let first = s
        .split_whitespace()
        .next()
        .unwrap_or("")
        .to_ascii_uppercase();
    matches!(
        first.as_str(),
        "ALTER" | "CREATE" | "DROP" | "REINDEX" | "VACUUM"
    )
}

/// Strips the `driver=rusqlite` routing parameter from a SQLite URL so the
/// remainder can be parsed by `sqlx::sqlite::SqliteConnectOptions`.
fn strip_driver_param(url: &str) -> String {
    if let Some((base, query)) = url.split_once('?') {
        let mut parts = Vec::new();
        for part in query.split('&') {
            if part == "driver=rusqlite" || part.starts_with("driver=rusqlite&") {
                continue;
            }
            parts.push(part);
        }
        if parts.is_empty() {
            base.to_owned()
        } else {
            format!("{base}?{}", parts.join("&"))
        }
    } else {
        url.to_owned()
    }
}