nautilus-orm-connector 1.3.1

Database executors and connection management for Nautilus ORM
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
//! Transaction executor for Nautilus.
//!
//! This module provides [`TransactionExecutor`], a single type that wraps a
//! live database transaction for any of the three supported backends
//! (PostgreSQL, MySQL, SQLite).  It replaces the previous per-backend trio
//! `TxPgExecutor` / `TxMysqlExecutor` / `TxSqliteExecutor`, which had
//! identical structure in three copies.
//!
//! ## Architecture note
//!
//! sqlx's `Transaction<'static, Db>` is parameterised by `Db`, making a true
//! Rust generic impossible without fighting GAT lifetime constraints (SQLite's
//! `SqliteArguments<'q>` carries a `'q` lifetime that PG/MySQL arguments do
//! not).  The type instead uses a private `TransactionInner` enum to hold
//! whichever backend's transaction is live, while presenting a uniform public
//! API to all callers.

use std::sync::Arc;
use std::time::Duration;

use futures::future::BoxFuture;
use nautilus_core::Value;
use tokio::sync::Mutex;

use nautilus_dialect::Sql;

use crate::error::{ConnectorError as Error, Result};
use crate::row_stream::RowStream;
use crate::single_row::{fetch_single_row, SingleRowExpectation};
use crate::{Executor, Row};

/// Options for starting a transaction.
#[derive(Debug, Clone)]
pub struct TransactionOptions {
    /// Maximum duration before the transaction is automatically rolled back.
    pub timeout: Duration,
    /// Optional isolation level override.
    pub isolation_level: Option<IsolationLevel>,
}

impl Default for TransactionOptions {
    fn default() -> Self {
        Self {
            timeout: Duration::from_secs(5),
            isolation_level: None,
        }
    }
}

/// Transaction isolation level.
///
/// Re-exported from `nautilus-protocol` for convenience; the connector uses
/// the same enum so callers don't need to depend on the protocol crate.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum IsolationLevel {
    /// Read uncommitted — allows dirty reads.
    ReadUncommitted,
    /// Read committed — default for most databases.
    ReadCommitted,
    /// Repeatable read — prevents non-repeatable reads.
    RepeatableRead,
    /// Serializable — strictest isolation level.
    Serializable,
}

impl IsolationLevel {
    /// Returns the SQL representation (e.g., `"READ COMMITTED"`).
    pub fn as_sql(&self) -> &'static str {
        match self {
            IsolationLevel::ReadUncommitted => "READ UNCOMMITTED",
            IsolationLevel::ReadCommitted => "READ COMMITTED",
            IsolationLevel::RepeatableRead => "REPEATABLE READ",
            IsolationLevel::Serializable => "SERIALIZABLE",
        }
    }
}

/// Per-backend transaction storage.
///
/// This is a private implementation detail — callers always interact with the
/// outer [`TransactionExecutor`] type.
enum TransactionInner {
    Postgres(Arc<Mutex<Option<sqlx::Transaction<'static, sqlx::Postgres>>>>),
    Mysql(Arc<Mutex<Option<sqlx::Transaction<'static, sqlx::MySql>>>>),
    Sqlite(Arc<Mutex<Option<sqlx::Transaction<'static, sqlx::Sqlite>>>>),
}

type TxHandle<DB> = Arc<Mutex<Option<sqlx::Transaction<'static, DB>>>>;

/// An executor that runs queries inside a live database transaction.
///
/// This single type works with PostgreSQL, MySQL, and SQLite, replacing the
/// previous per-backend `TxPgExecutor` / `TxMysqlExecutor` / `TxSqliteExecutor`
/// trio.  Internally it holds a [`TransactionInner`] enum; callers see one
/// consistent API regardless of the backend in use.
///
/// The underlying sqlx transaction is stored behind an
/// `Arc<Mutex<Option<…>>>` so the executor can be shared cheaply through
/// [`crate::client::Client`]'s `Arc<E>` wrapping.
///
/// # Example
///
/// ```no_run
/// # use nautilus_connector::{Client, ConnectorResult};
/// # async fn example() -> ConnectorResult<()> {
/// let client = Client::postgres("postgres://localhost/mydb").await?;
/// let result = client.transaction(Default::default(), |tx| Box::pin(async move {
///     // tx is Client<TransactionExecutor>; all queries run inside the transaction.
///     Ok(42i64)
/// })).await?;
/// # Ok(())
/// # }
/// ```
pub struct TransactionExecutor {
    inner: TransactionInner,
}

impl TransactionExecutor {
    /// Wrap an already-begun PostgreSQL transaction.
    pub fn postgres(tx: sqlx::Transaction<'static, sqlx::Postgres>) -> Self {
        Self {
            inner: TransactionInner::Postgres(Arc::new(Mutex::new(Some(tx)))),
        }
    }

    /// Wrap an already-begun MySQL transaction.
    pub fn mysql(tx: sqlx::Transaction<'static, sqlx::MySql>) -> Self {
        Self {
            inner: TransactionInner::Mysql(Arc::new(Mutex::new(Some(tx)))),
        }
    }

    /// Wrap an already-begun SQLite transaction.
    pub fn sqlite(tx: sqlx::Transaction<'static, sqlx::Sqlite>) -> Self {
        Self {
            inner: TransactionInner::Sqlite(Arc::new(Mutex::new(Some(tx)))),
        }
    }

    async fn take_transaction<DB>(tx_arc: &TxHandle<DB>) -> Result<sqlx::Transaction<'static, DB>>
    where
        DB: sqlx::Database,
    {
        tx_arc
            .lock()
            .await
            .take()
            .ok_or_else(|| Error::database_msg("Transaction already closed"))
    }

    async fn transaction_is_open<DB>(tx_arc: &TxHandle<DB>) -> bool
    where
        DB: sqlx::Database,
    {
        tx_arc.lock().await.is_some()
    }

    fn bind_query<'q, DB, Bind>(
        sql_text: &'q str,
        params: &'q [Value],
        persistent: bool,
        bind: Bind,
    ) -> Result<sqlx::query::Query<'q, DB, <DB as sqlx::Database>::Arguments<'q>>>
    where
        DB: sqlx::Database + sqlx::database::HasStatementCache,
        for<'q2> <DB as sqlx::Database>::Arguments<'q2>: sqlx::IntoArguments<'q2, DB>,
        Bind: Fn(
            sqlx::query::Query<'q, DB, <DB as sqlx::Database>::Arguments<'q>>,
            &'q Value,
        )
            -> Result<sqlx::query::Query<'q, DB, <DB as sqlx::Database>::Arguments<'q>>>,
    {
        let mut query = sqlx::query(sql_text).persistent(persistent);
        for param in params {
            query = bind(query, param)?;
        }
        Ok(query)
    }

    fn execute_affected_on<DB, Bind, RowsAffected>(
        tx_arc: TxHandle<DB>,
        sql_text: String,
        params: Vec<Value>,
        persistent: bool,
        bind: Bind,
        rows_affected: RowsAffected,
    ) -> BoxFuture<'static, Result<usize>>
    where
        DB: sqlx::Database + sqlx::database::HasStatementCache + Send + 'static,
        for<'c> &'c mut <DB as sqlx::Database>::Connection: sqlx::Executor<'c, Database = DB>,
        for<'q> <DB as sqlx::Database>::Arguments<'q>: sqlx::IntoArguments<'q, DB>,
        for<'q> Bind: Fn(
                sqlx::query::Query<'q, DB, <DB as sqlx::Database>::Arguments<'q>>,
                &'q Value,
            )
                -> Result<sqlx::query::Query<'q, DB, <DB as sqlx::Database>::Arguments<'q>>>
            + Copy
            + Send
            + 'static,
        RowsAffected: Fn(<DB as sqlx::Database>::QueryResult) -> u64 + Copy + Send + 'static,
    {
        Box::pin(async move {
            let query = Self::bind_query::<DB, Bind>(&sql_text, &params, persistent, bind)?;
            let mut guard = tx_arc.lock().await;
            let tx = guard
                .as_mut()
                .ok_or_else(|| Error::database_msg("Transaction already closed"))?;

            use sqlx::Executor as _;
            let result = (&mut **tx)
                .execute(query)
                .await
                .map_err(|e| Error::database(e, "Mutation failed"))?;
            Ok(rows_affected(result) as usize)
        })
    }

    fn execute_collect_on<DB, Bind, Decode>(
        tx_arc: TxHandle<DB>,
        sql_text: String,
        params: Vec<Value>,
        persistent: bool,
        bind: Bind,
        decode: Decode,
        query_context: &'static str,
    ) -> BoxFuture<'static, Result<Vec<Row>>>
    where
        DB: sqlx::Database + sqlx::database::HasStatementCache + Send + 'static,
        for<'c> &'c mut <DB as sqlx::Database>::Connection: sqlx::Executor<'c, Database = DB>,
        for<'q> <DB as sqlx::Database>::Arguments<'q>: sqlx::IntoArguments<'q, DB>,
        for<'q> Bind: Fn(
                sqlx::query::Query<'q, DB, <DB as sqlx::Database>::Arguments<'q>>,
                &'q Value,
            )
                -> Result<sqlx::query::Query<'q, DB, <DB as sqlx::Database>::Arguments<'q>>>
            + Copy
            + Send
            + 'static,
        Decode: Fn(<DB as sqlx::Database>::Row) -> Result<Row> + Copy + Send + 'static,
    {
        Box::pin(async move {
            let query = Self::bind_query::<DB, Bind>(&sql_text, &params, persistent, bind)?;
            let mut guard = tx_arc.lock().await;
            let tx = guard
                .as_mut()
                .ok_or_else(|| Error::database_msg("Transaction already closed"))?;

            use sqlx::Executor as _;
            let rows = (&mut **tx)
                .fetch_all(query)
                .await
                .map_err(|e| Error::database(e, query_context))?;
            drop(guard);

            rows.into_iter().map(decode).collect()
        })
    }

    fn execute_and_fetch_collect_on<DB, Bind, Decode>(
        tx_arc: TxHandle<DB>,
        mutation_text: String,
        mutation_params: Vec<Value>,
        fetch_text: String,
        fetch_params: Vec<Value>,
        bind: Bind,
        decode: Decode,
    ) -> BoxFuture<'static, Result<Vec<Row>>>
    where
        DB: sqlx::Database + sqlx::database::HasStatementCache + Send + 'static,
        for<'c> &'c mut <DB as sqlx::Database>::Connection: sqlx::Executor<'c, Database = DB>,
        for<'q> <DB as sqlx::Database>::Arguments<'q>: sqlx::IntoArguments<'q, DB>,
        for<'q> Bind: Fn(
                sqlx::query::Query<'q, DB, <DB as sqlx::Database>::Arguments<'q>>,
                &'q Value,
            )
                -> Result<sqlx::query::Query<'q, DB, <DB as sqlx::Database>::Arguments<'q>>>
            + Copy
            + Send
            + 'static,
        Decode: Fn(<DB as sqlx::Database>::Row) -> Result<Row> + Copy + Send + 'static,
    {
        Box::pin(async move {
            let mutation_query =
                Self::bind_query::<DB, Bind>(&mutation_text, &mutation_params, true, bind)?;
            let fetch_query = Self::bind_query::<DB, Bind>(&fetch_text, &fetch_params, true, bind)?;
            let mut guard = tx_arc.lock().await;
            let tx = guard
                .as_mut()
                .ok_or_else(|| Error::database_msg("Transaction already closed"))?;

            use sqlx::Executor as _;
            (&mut **tx)
                .execute(mutation_query)
                .await
                .map_err(|e| Error::database(e, "Mutation failed"))?;

            let rows = (&mut **tx)
                .fetch_all(fetch_query)
                .await
                .map_err(|e| Error::database(e, "Fetch failed"))?;
            drop(guard);

            rows.into_iter().map(decode).collect()
        })
    }

    fn execute_single_on<DB, Bind, Decode>(
        tx_arc: TxHandle<DB>,
        sql_text: String,
        params: Vec<Value>,
        bind: Bind,
        decode: Decode,
        query_context: &'static str,
        expectation: SingleRowExpectation,
    ) -> BoxFuture<'static, Result<Option<Row>>>
    where
        DB: sqlx::Database + Send + 'static,
        for<'c> &'c mut <DB as sqlx::Database>::Connection: sqlx::Executor<'c, Database = DB>,
        for<'q> <DB as sqlx::Database>::Arguments<'q>: sqlx::IntoArguments<'q, DB>,
        for<'q> Bind: Fn(
                sqlx::query::Query<'q, DB, <DB as sqlx::Database>::Arguments<'q>>,
                &'q Value,
            )
                -> Result<sqlx::query::Query<'q, DB, <DB as sqlx::Database>::Arguments<'q>>>
            + Copy
            + Send
            + 'static,
        Decode: Fn(<DB as sqlx::Database>::Row) -> Result<Row> + Copy + Send + 'static,
    {
        Box::pin(async move {
            let mut guard = tx_arc.lock().await;
            let tx = guard
                .as_mut()
                .ok_or_else(|| Error::database_msg("Transaction already closed"))?;

            fetch_single_row::<DB, _, _, _>(
                &mut **tx,
                &sql_text,
                &params,
                bind,
                decode,
                query_context,
                expectation,
            )
            .await
        })
    }

    /// Commit the transaction. After this, further queries will return an error.
    pub async fn commit(&self) -> Result<()> {
        match &self.inner {
            TransactionInner::Postgres(mx) => {
                let tx = Self::take_transaction(mx).await?;
                tx.commit()
                    .await
                    .map_err(|e| Error::database(e, "Commit failed"))
            }
            TransactionInner::Mysql(mx) => {
                let tx = Self::take_transaction(mx).await?;
                tx.commit()
                    .await
                    .map_err(|e| Error::database(e, "Commit failed"))
            }
            TransactionInner::Sqlite(mx) => {
                let tx = Self::take_transaction(mx).await?;
                tx.commit()
                    .await
                    .map_err(|e| Error::database(e, "Commit failed"))
            }
        }
    }

    /// Rollback the transaction. After this, further queries will return an error.
    pub async fn rollback(&self) -> Result<()> {
        match &self.inner {
            TransactionInner::Postgres(mx) => {
                let tx = Self::take_transaction(mx).await?;
                tx.rollback()
                    .await
                    .map_err(|e| Error::database(e, "Rollback failed"))
            }
            TransactionInner::Mysql(mx) => {
                let tx = Self::take_transaction(mx).await?;
                tx.rollback()
                    .await
                    .map_err(|e| Error::database(e, "Rollback failed"))
            }
            TransactionInner::Sqlite(mx) => {
                let tx = Self::take_transaction(mx).await?;
                tx.rollback()
                    .await
                    .map_err(|e| Error::database(e, "Rollback failed"))
            }
        }
    }

    /// Returns `true` if the transaction has not yet been committed or rolled back.
    pub async fn is_open(&self) -> bool {
        match &self.inner {
            TransactionInner::Postgres(mx) => Self::transaction_is_open(mx).await,
            TransactionInner::Mysql(mx) => Self::transaction_is_open(mx).await,
            TransactionInner::Sqlite(mx) => Self::transaction_is_open(mx).await,
        }
    }

    /// Execute a mutation SQL inside this transaction and return the number of
    /// affected rows.
    ///
    /// Used when `return_data = false` so no RETURNING clause is emitted and
    /// the affected-row count comes from the database execution result.
    pub async fn execute_affected(&self, sql: &Sql) -> Result<usize> {
        match &self.inner {
            TransactionInner::Postgres(tx_arc) => {
                Self::execute_affected_on(
                    Arc::clone(tx_arc),
                    sql.text.clone(),
                    sql.params.clone(),
                    true,
                    crate::postgres::bind_value,
                    |result: sqlx::postgres::PgQueryResult| result.rows_affected(),
                )
                .await
            }
            TransactionInner::Mysql(tx_arc) => {
                Self::execute_affected_on(
                    Arc::clone(tx_arc),
                    sql.text.clone(),
                    sql.params.clone(),
                    true,
                    crate::mysql::bind_value,
                    |result: sqlx::mysql::MySqlQueryResult| result.rows_affected(),
                )
                .await
            }
            TransactionInner::Sqlite(tx_arc) => {
                Self::execute_affected_on(
                    Arc::clone(tx_arc),
                    sql.text.clone(),
                    sql.params.clone(),
                    true,
                    crate::sqlite::bind_value,
                    |result: sqlx::sqlite::SqliteQueryResult| result.rows_affected(),
                )
                .await
            }
        }
    }

    /// Execute a SQL query with sqlx statement persistence disabled.
    ///
    /// Raw/direct query paths use this so they remain compatible with
    /// poolers such as PgBouncer even when they run inside a transaction.
    pub async fn execute_collect_unprepared(&self, sql: &Sql) -> Result<Vec<Row>> {
        match &self.inner {
            TransactionInner::Postgres(tx_arc) => {
                Self::execute_collect_on(
                    Arc::clone(tx_arc),
                    sql.text.clone(),
                    sql.params.clone(),
                    false,
                    crate::postgres::bind_value,
                    crate::postgres_stream::decode_row_internal,
                    "Query failed",
                )
                .await
            }
            TransactionInner::Mysql(tx_arc) => {
                Self::execute_collect_on(
                    Arc::clone(tx_arc),
                    sql.text.clone(),
                    sql.params.clone(),
                    false,
                    crate::mysql::bind_value,
                    crate::mysql_stream::decode_row_internal,
                    "Query failed",
                )
                .await
            }
            TransactionInner::Sqlite(tx_arc) => {
                Self::execute_collect_on(
                    Arc::clone(tx_arc),
                    sql.text.clone(),
                    sql.params.clone(),
                    false,
                    crate::sqlite::bind_value,
                    crate::sqlite_stream::decode_row_internal,
                    "Query failed",
                )
                .await
            }
        }
    }
}

impl Executor for TransactionExecutor {
    type Row<'conn>
        = Row
    where
        Self: 'conn;
    type RowStream<'conn>
        = RowStream<'conn>
    where
        Self: 'conn;

    fn execute<'conn>(&'conn self, sql: &'conn Sql) -> Self::RowStream<'conn> {
        match &self.inner {
            TransactionInner::Postgres(tx_arc) => {
                RowStream::from_rows_future(Self::execute_collect_on(
                    Arc::clone(tx_arc),
                    sql.text.clone(),
                    sql.params.clone(),
                    true,
                    crate::postgres::bind_value,
                    crate::postgres_stream::decode_row_internal,
                    "Query failed",
                ))
            }
            TransactionInner::Mysql(tx_arc) => {
                RowStream::from_rows_future(Self::execute_collect_on(
                    Arc::clone(tx_arc),
                    sql.text.clone(),
                    sql.params.clone(),
                    true,
                    crate::mysql::bind_value,
                    crate::mysql_stream::decode_row_internal,
                    "Query failed",
                ))
            }
            TransactionInner::Sqlite(tx_arc) => {
                RowStream::from_rows_future(Self::execute_collect_on(
                    Arc::clone(tx_arc),
                    sql.text.clone(),
                    sql.params.clone(),
                    true,
                    crate::sqlite::bind_value,
                    crate::sqlite_stream::decode_row_internal,
                    "Query failed",
                ))
            }
        }
    }

    /// Streaming inside a transaction is buffered: a live transaction holds the
    /// connection exclusively, so the worker pattern used for pooled executors
    /// does not apply. We reuse `execute_collect_on` (which already returns a
    /// `'static` future via the `Arc<Mutex<...>>` handle) and adapt it to the
    /// shared `RowStream<'static>` shape so codegen `stream_many` paths work
    /// uniformly across pooled and transactional clients.
    fn execute_owned(&self, sql: Sql) -> RowStream<'static> {
        match &self.inner {
            TransactionInner::Postgres(tx_arc) => {
                RowStream::from_rows_future(Self::execute_collect_on(
                    Arc::clone(tx_arc),
                    sql.text,
                    sql.params,
                    true,
                    crate::postgres::bind_value,
                    crate::postgres_stream::decode_row_internal,
                    "Query failed",
                ))
            }
            TransactionInner::Mysql(tx_arc) => {
                RowStream::from_rows_future(Self::execute_collect_on(
                    Arc::clone(tx_arc),
                    sql.text,
                    sql.params,
                    true,
                    crate::mysql::bind_value,
                    crate::mysql_stream::decode_row_internal,
                    "Query failed",
                ))
            }
            TransactionInner::Sqlite(tx_arc) => {
                RowStream::from_rows_future(Self::execute_collect_on(
                    Arc::clone(tx_arc),
                    sql.text,
                    sql.params,
                    true,
                    crate::sqlite::bind_value,
                    crate::sqlite_stream::decode_row_internal,
                    "Query failed",
                ))
            }
        }
    }

    fn execute_and_fetch<'conn>(
        &'conn self,
        mutation: &'conn Sql,
        fetch: &'conn Sql,
    ) -> Self::RowStream<'conn> {
        match &self.inner {
            TransactionInner::Postgres(tx_arc) => {
                RowStream::from_rows_future(Self::execute_and_fetch_collect_on(
                    Arc::clone(tx_arc),
                    mutation.text.clone(),
                    mutation.params.clone(),
                    fetch.text.clone(),
                    fetch.params.clone(),
                    crate::postgres::bind_value,
                    crate::postgres_stream::decode_row_internal,
                ))
            }
            TransactionInner::Mysql(tx_arc) => {
                RowStream::from_rows_future(Self::execute_and_fetch_collect_on(
                    Arc::clone(tx_arc),
                    mutation.text.clone(),
                    mutation.params.clone(),
                    fetch.text.clone(),
                    fetch.params.clone(),
                    crate::mysql::bind_value,
                    crate::mysql_stream::decode_row_internal,
                ))
            }
            TransactionInner::Sqlite(tx_arc) => {
                RowStream::from_rows_future(Self::execute_and_fetch_collect_on(
                    Arc::clone(tx_arc),
                    mutation.text.clone(),
                    mutation.params.clone(),
                    fetch.text.clone(),
                    fetch.params.clone(),
                    crate::sqlite::bind_value,
                    crate::sqlite_stream::decode_row_internal,
                ))
            }
        }
    }

    fn execute_collect<'conn>(
        &'conn self,
        sql: &'conn Sql,
    ) -> BoxFuture<'conn, Result<Vec<Self::Row<'conn>>>>
    where
        Self: 'conn,
    {
        match &self.inner {
            TransactionInner::Postgres(tx_arc) => Self::execute_collect_on(
                Arc::clone(tx_arc),
                sql.text.clone(),
                sql.params.clone(),
                true,
                crate::postgres::bind_value,
                crate::postgres_stream::decode_row_internal,
                "Query failed",
            ),
            TransactionInner::Mysql(tx_arc) => Self::execute_collect_on(
                Arc::clone(tx_arc),
                sql.text.clone(),
                sql.params.clone(),
                true,
                crate::mysql::bind_value,
                crate::mysql_stream::decode_row_internal,
                "Query failed",
            ),
            TransactionInner::Sqlite(tx_arc) => Self::execute_collect_on(
                Arc::clone(tx_arc),
                sql.text.clone(),
                sql.params.clone(),
                true,
                crate::sqlite::bind_value,
                crate::sqlite_stream::decode_row_internal,
                "Query failed",
            ),
        }
    }

    fn execute_one<'conn>(
        &'conn self,
        sql: &'conn Sql,
    ) -> BoxFuture<'conn, Result<Self::Row<'conn>>>
    where
        Self: 'conn,
    {
        Box::pin(async move {
            let row = match &self.inner {
                TransactionInner::Postgres(tx_arc) => {
                    Self::execute_single_on(
                        Arc::clone(tx_arc),
                        sql.text.clone(),
                        sql.params.clone(),
                        crate::postgres::bind_value,
                        crate::postgres_stream::decode_row_internal,
                        "Query failed",
                        SingleRowExpectation::ExactlyOne,
                    )
                    .await?
                }
                TransactionInner::Mysql(tx_arc) => {
                    Self::execute_single_on(
                        Arc::clone(tx_arc),
                        sql.text.clone(),
                        sql.params.clone(),
                        crate::mysql::bind_value,
                        crate::mysql_stream::decode_row_internal,
                        "Query failed",
                        SingleRowExpectation::ExactlyOne,
                    )
                    .await?
                }
                TransactionInner::Sqlite(tx_arc) => {
                    Self::execute_single_on(
                        Arc::clone(tx_arc),
                        sql.text.clone(),
                        sql.params.clone(),
                        crate::sqlite::bind_value,
                        crate::sqlite_stream::decode_row_internal,
                        "Query failed",
                        SingleRowExpectation::ExactlyOne,
                    )
                    .await?
                }
            };

            // `ExactlyOne` already validated row_count == 1, so `row` is always
            // `Some` here; the fallback keeps this a graceful error, never a panic.
            row.ok_or_else(|| Error::database_msg("Expected exactly one row, got 0"))
        })
    }

    fn execute_optional<'conn>(
        &'conn self,
        sql: &'conn Sql,
    ) -> BoxFuture<'conn, Result<Option<Self::Row<'conn>>>>
    where
        Self: 'conn,
    {
        match &self.inner {
            TransactionInner::Postgres(tx_arc) => Self::execute_single_on(
                Arc::clone(tx_arc),
                sql.text.clone(),
                sql.params.clone(),
                crate::postgres::bind_value,
                crate::postgres_stream::decode_row_internal,
                "Query failed",
                SingleRowExpectation::ZeroOrOne,
            ),
            TransactionInner::Mysql(tx_arc) => Self::execute_single_on(
                Arc::clone(tx_arc),
                sql.text.clone(),
                sql.params.clone(),
                crate::mysql::bind_value,
                crate::mysql_stream::decode_row_internal,
                "Query failed",
                SingleRowExpectation::ZeroOrOne,
            ),
            TransactionInner::Sqlite(tx_arc) => Self::execute_single_on(
                Arc::clone(tx_arc),
                sql.text.clone(),
                sql.params.clone(),
                crate::sqlite::bind_value,
                crate::sqlite_stream::decode_row_internal,
                "Query failed",
                SingleRowExpectation::ZeroOrOne,
            ),
        }
    }
}