sql-middleware 0.9.0

Lightweight async wrappers for tokio-postgres, rusqlite, turso, and tiberius.
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
use std::sync::Arc;

use tokio::sync::Mutex;

use crate::adapters::params::convert_params;
use crate::middleware::{ConversionMode, CustomDbRow, ResultSet, RowValues, SqlMiddlewareDbError};
use crate::types::StatementCacheMode;

use super::params::{Params as TursoParams, TursoParamsBuf};

/// Handle to a prepared Turso statement owned by a pooled connection.
///
/// This exists to reuse a compiled Turso statement outside an explicit
/// transaction. Turso's client lets us keep a connection-bound prepared handle,
/// so we expose it for non-transactional reuse. Other backends (Postgres)
/// prepare statements on their transaction handles instead of exposing a safe
/// connection-level prepared handle, so we don't mirror this type there.
///
/// Instances can be cloned and reused across awaited calls. Internally, the
/// underlying `turso::Statement` is protected by a `tokio::sync::Mutex` so the
/// compiled statement can be shared safely between tasks while still benefiting
/// from Turso's statement caching.
#[derive(Clone)]
pub struct TursoNonTxPreparedStatement {
    _connection: turso::Connection,
    statement: Arc<Mutex<turso::Statement>>,
    columns: Arc<Vec<String>>,
    sql: Arc<String>,
}

impl std::fmt::Debug for TursoNonTxPreparedStatement {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("TursoNonTxPreparedStatement")
            .field("_connection", &"<turso::Connection>")
            .field("statement", &"<turso::Statement>")
            .field("columns", &self.columns)
            .field("sql", &self.sql)
            .finish()
    }
}

impl TursoNonTxPreparedStatement {
    pub(crate) async fn prepare_with_cache_mode(
        connection: turso::Connection,
        sql: &str,
        statement_cache_mode: StatementCacheMode,
    ) -> Result<Self, SqlMiddlewareDbError> {
        let sql_arc = Arc::new(sql.to_owned());
        let statement = match statement_cache_mode {
            StatementCacheMode::Cached => connection.prepare_cached(sql).await,
            StatementCacheMode::Uncached => connection.prepare(sql).await,
        }
        .map_err(|e| SqlMiddlewareDbError::ExecutionError(format!("Turso prepare error: {e}")))?;

        let columns = statement.column_names();

        Ok(Self {
            _connection: connection,
            statement: Arc::new(Mutex::new(statement)),
            columns: Arc::new(columns),
            sql: sql_arc,
        })
    }

    /// Start configuring a prepared SELECT execution.
    #[must_use]
    pub fn select(&self) -> TursoPreparedSelect<'_, '_> {
        TursoPreparedSelect {
            statement: self,
            params: TursoPreparedParams::None,
        }
    }

    /// Start configuring a prepared DML execution.
    #[must_use]
    pub fn execute(&self) -> TursoPreparedExecute<'_, '_> {
        TursoPreparedExecute {
            statement: self,
            params: TursoPreparedParams::None,
        }
    }

    /// Execute the prepared statement as a query and materialise the rows into a [`ResultSet`].
    ///
    /// # Errors
    /// Returns [`SqlMiddlewareDbError`] if parameter conversion fails, the Turso client reports an
    /// execution error, or result decoding cannot be completed.
    pub(crate) async fn query(
        &self,
        params: &[RowValues],
    ) -> Result<ResultSet, SqlMiddlewareDbError> {
        let converted = convert_params::<TursoParams>(params, ConversionMode::Query)?;
        self.query_driver_params(converted.0).await
    }

    /// Execute the prepared statement as a query using a reusable Turso parameter buffer.
    ///
    /// # Errors
    /// Returns [`SqlMiddlewareDbError`] if execution fails or result decoding cannot be completed.
    pub(crate) async fn query_params(
        &self,
        params: &TursoParamsBuf,
    ) -> Result<ResultSet, SqlMiddlewareDbError> {
        self.query_driver_params(params.to_params()).await
    }

    async fn query_driver_params(
        &self,
        params: turso::params::Params,
    ) -> Result<ResultSet, SqlMiddlewareDbError> {
        let rows = {
            let mut stmt = self.statement.lock().await;
            stmt.query(params).await.map_err(|e| {
                SqlMiddlewareDbError::ExecutionError(format!("Turso prepared query error: {e}"))
            })?
        };

        let result = crate::turso::query::build_result_set(rows, Some(self.columns.clone())).await;

        self.reset().await?;
        result
    }

    /// Execute the prepared statement as a query and return the first row, if present.
    ///
    /// # Errors
    /// Returns [`SqlMiddlewareDbError`] if parameter conversion, execution, or result decoding
    /// fails.
    pub(crate) async fn query_optional(
        &self,
        params: &[RowValues],
    ) -> Result<Option<CustomDbRow>, SqlMiddlewareDbError> {
        self.query(params).await.map(ResultSet::into_optional)
    }

    /// Execute the prepared statement with a reusable parameter buffer and return the first row,
    /// if present.
    ///
    /// # Errors
    /// Returns [`SqlMiddlewareDbError`] if parameter conversion, execution, or result decoding
    /// fails.
    pub(crate) async fn query_optional_params(
        &self,
        params: &TursoParamsBuf,
    ) -> Result<Option<CustomDbRow>, SqlMiddlewareDbError> {
        self.query_params(params)
            .await
            .map(ResultSet::into_optional)
    }

    /// Execute the prepared statement as a query and return the first row.
    ///
    /// # Errors
    /// Returns [`SqlMiddlewareDbError`] if execution fails or no row is returned.
    pub(crate) async fn query_one(
        &self,
        params: &[RowValues],
    ) -> Result<CustomDbRow, SqlMiddlewareDbError> {
        self.query(params).await?.into_one()
    }

    /// Execute the prepared statement with a reusable parameter buffer and return the first row.
    ///
    /// # Errors
    /// Returns [`SqlMiddlewareDbError`] if execution fails or no row is returned.
    pub(crate) async fn query_one_params(
        &self,
        params: &TursoParamsBuf,
    ) -> Result<CustomDbRow, SqlMiddlewareDbError> {
        self.query_params(params).await?.into_one()
    }

    /// Execute the prepared statement and map the first native Turso row.
    ///
    /// Use this for hot paths that only need one row and can decode directly from
    /// `turso::Row`, avoiding `ResultSet` materialisation.
    ///
    /// # Errors
    /// Returns [`SqlMiddlewareDbError`] if execution fails, no row is returned, or the mapper
    /// fails.
    pub(crate) async fn query_map_one<T, F>(
        &self,
        params: &[RowValues],
        mapper: F,
    ) -> Result<T, SqlMiddlewareDbError>
    where
        F: FnOnce(&turso::Row) -> Result<T, SqlMiddlewareDbError>,
    {
        self.query_map_optional(params, mapper)
            .await?
            .ok_or_else(|| SqlMiddlewareDbError::ExecutionError("query returned no rows".into()))
    }

    /// Execute the prepared statement with a reusable parameter buffer and map the first native
    /// Turso row.
    ///
    /// # Errors
    /// Returns [`SqlMiddlewareDbError`] if execution fails, no row is returned, or the mapper
    /// fails.
    pub(crate) async fn query_map_one_params<T, F>(
        &self,
        params: &TursoParamsBuf,
        mapper: F,
    ) -> Result<T, SqlMiddlewareDbError>
    where
        F: FnOnce(&turso::Row) -> Result<T, SqlMiddlewareDbError>,
    {
        self.query_map_optional_params(params, mapper)
            .await?
            .ok_or_else(|| SqlMiddlewareDbError::ExecutionError("query returned no rows".into()))
    }

    /// Execute the prepared statement and map the first native Turso row, returning `None` if no
    /// row exists.
    ///
    /// # Errors
    /// Returns [`SqlMiddlewareDbError`] if execution or the mapper fails.
    pub(crate) async fn query_map_optional<T, F>(
        &self,
        params: &[RowValues],
        mapper: F,
    ) -> Result<Option<T>, SqlMiddlewareDbError>
    where
        F: FnOnce(&turso::Row) -> Result<T, SqlMiddlewareDbError>,
    {
        let converted = convert_params::<TursoParams>(params, ConversionMode::Query)?;
        self.query_map_optional_driver_params(converted.0, mapper)
            .await
    }

    /// Execute the prepared statement with a reusable parameter buffer and map the first native
    /// Turso row, returning `None` if no row exists.
    ///
    /// # Errors
    /// Returns [`SqlMiddlewareDbError`] if execution or the mapper fails.
    pub(crate) async fn query_map_optional_params<T, F>(
        &self,
        params: &TursoParamsBuf,
        mapper: F,
    ) -> Result<Option<T>, SqlMiddlewareDbError>
    where
        F: FnOnce(&turso::Row) -> Result<T, SqlMiddlewareDbError>,
    {
        self.query_map_optional_driver_params(params.to_params(), mapper)
            .await
    }

    async fn query_map_optional_driver_params<T, F>(
        &self,
        params: turso::params::Params,
        mapper: F,
    ) -> Result<Option<T>, SqlMiddlewareDbError>
    where
        F: FnOnce(&turso::Row) -> Result<T, SqlMiddlewareDbError>,
    {
        let rows = {
            let mut stmt = self.statement.lock().await;
            stmt.query(params).await.map_err(|e| {
                SqlMiddlewareDbError::ExecutionError(format!("Turso prepared query error: {e}"))
            })?
        };

        let result = crate::turso::query::query_map_optional(rows, mapper).await;
        self.reset().await?;
        result
    }

    /// Execute the prepared statement as a DML (INSERT/UPDATE/DELETE) returning rows affected.
    ///
    /// # Errors
    /// Returns [`SqlMiddlewareDbError`] if parameter conversion fails, Turso returns an execution
    /// error, or the affected-row count cannot be converted into `usize`.
    pub(crate) async fn execute_values(
        &self,
        params: &[RowValues],
    ) -> Result<usize, SqlMiddlewareDbError> {
        let converted = convert_params::<TursoParams>(params, ConversionMode::Execute)?;
        self.execute_driver_params(converted.0).await
    }

    /// Execute the prepared statement as DML using a reusable Turso parameter buffer.
    ///
    /// # Errors
    /// Returns [`SqlMiddlewareDbError`] if Turso returns an execution error, or the affected-row
    /// count cannot be converted into `usize`.
    pub(crate) async fn execute_params(
        &self,
        params: &TursoParamsBuf,
    ) -> Result<usize, SqlMiddlewareDbError> {
        self.execute_driver_params(params.to_params()).await
    }

    async fn execute_driver_params(
        &self,
        params: turso::params::Params,
    ) -> Result<usize, SqlMiddlewareDbError> {
        let affected = {
            let mut stmt = self.statement.lock().await;
            let affected = stmt.execute(params).await.map_err(|e| {
                SqlMiddlewareDbError::ExecutionError(format!("Turso prepared execute error: {e}"))
            })?;
            stmt.reset().map_err(|e| {
                SqlMiddlewareDbError::ExecutionError(format!("Turso reset error: {e}"))
            })?;
            affected
        };

        usize::try_from(affected).map_err(|e| {
            SqlMiddlewareDbError::ExecutionError(format!(
                "Turso affected rows conversion error: {e}"
            ))
        })
    }

    /// Access the raw SQL string of the prepared statement.
    #[must_use]
    pub fn sql(&self) -> &str {
        self.sql.as_str()
    }

    async fn reset(&self) -> Result<(), SqlMiddlewareDbError> {
        let stmt = self.statement.lock().await;
        stmt.reset()
            .map_err(|e| SqlMiddlewareDbError::ExecutionError(format!("Turso reset error: {e}")))
    }
}

/// Builder for executing a prepared Turso DML statement.
pub struct TursoPreparedExecute<'stmt, 'params> {
    statement: &'stmt TursoNonTxPreparedStatement,
    params: TursoPreparedParams<'params>,
}

impl<'stmt, 'params> TursoPreparedExecute<'stmt, 'params> {
    /// Use middleware `RowValues` parameters.
    #[must_use]
    pub fn params<'next>(self, params: &'next [RowValues]) -> TursoPreparedExecute<'stmt, 'next> {
        TursoPreparedExecute {
            statement: self.statement,
            params: TursoPreparedParams::RowValues(params),
        }
    }

    /// Use a reusable Turso parameter buffer.
    #[must_use]
    pub fn params_buf<'next>(
        self,
        params: &'next TursoParamsBuf,
    ) -> TursoPreparedExecute<'stmt, 'next> {
        TursoPreparedExecute {
            statement: self.statement,
            params: TursoPreparedParams::Buffer(params),
        }
    }

    /// Execute the DML statement and return affected rows.
    ///
    /// # Errors
    /// Returns [`SqlMiddlewareDbError`] if execution fails or the row count cannot be converted.
    pub async fn run(self) -> Result<usize, SqlMiddlewareDbError> {
        match self.params {
            TursoPreparedParams::None => self.statement.execute_values(&[]).await,
            TursoPreparedParams::RowValues(params) => self.statement.execute_values(params).await,
            TursoPreparedParams::Buffer(params) => self.statement.execute_params(params).await,
        }
    }
}

enum TursoPreparedParams<'params> {
    None,
    RowValues(&'params [RowValues]),
    Buffer(&'params TursoParamsBuf),
}

/// Builder for executing a prepared Turso SELECT.
pub struct TursoPreparedSelect<'stmt, 'params> {
    statement: &'stmt TursoNonTxPreparedStatement,
    params: TursoPreparedParams<'params>,
}

impl<'stmt, 'params> TursoPreparedSelect<'stmt, 'params> {
    /// Use middleware `RowValues` parameters.
    #[must_use]
    pub fn params<'next>(self, params: &'next [RowValues]) -> TursoPreparedSelect<'stmt, 'next> {
        TursoPreparedSelect {
            statement: self.statement,
            params: TursoPreparedParams::RowValues(params),
        }
    }

    /// Use a reusable Turso parameter buffer.
    #[must_use]
    pub fn params_buf<'next>(
        self,
        params: &'next TursoParamsBuf,
    ) -> TursoPreparedSelect<'stmt, 'next> {
        TursoPreparedSelect {
            statement: self.statement,
            params: TursoPreparedParams::Buffer(params),
        }
    }

    /// Execute and return all rows as a `ResultSet`.
    ///
    /// # Errors
    /// Returns [`SqlMiddlewareDbError`] if execution fails or result decoding cannot be completed.
    pub async fn all(self) -> Result<ResultSet, SqlMiddlewareDbError> {
        match self.params {
            TursoPreparedParams::None => self.statement.query(&[]).await,
            TursoPreparedParams::RowValues(params) => self.statement.query(params).await,
            TursoPreparedParams::Buffer(params) => self.statement.query_params(params).await,
        }
    }

    /// Execute and return the first row, if present.
    ///
    /// # Errors
    /// Returns [`SqlMiddlewareDbError`] if execution or result decoding fails.
    pub async fn optional(self) -> Result<Option<CustomDbRow>, SqlMiddlewareDbError> {
        match self.params {
            TursoPreparedParams::None => self.statement.query_optional(&[]).await,
            TursoPreparedParams::RowValues(params) => self.statement.query_optional(params).await,
            TursoPreparedParams::Buffer(params) => {
                self.statement.query_optional_params(params).await
            }
        }
    }

    /// Execute and return exactly one row.
    ///
    /// # Errors
    /// Returns [`SqlMiddlewareDbError`] if execution fails or no row is returned.
    pub async fn one(self) -> Result<CustomDbRow, SqlMiddlewareDbError> {
        match self.params {
            TursoPreparedParams::None => self.statement.query_one(&[]).await,
            TursoPreparedParams::RowValues(params) => self.statement.query_one(params).await,
            TursoPreparedParams::Buffer(params) => self.statement.query_one_params(params).await,
        }
    }

    /// Execute and map exactly one native Turso row.
    ///
    /// # Errors
    /// Returns [`SqlMiddlewareDbError`] if execution fails, no row is returned, or the mapper fails.
    pub async fn map_one<T, F>(self, mapper: F) -> Result<T, SqlMiddlewareDbError>
    where
        F: FnOnce(&turso::Row) -> Result<T, SqlMiddlewareDbError>,
    {
        match self.params {
            TursoPreparedParams::None => self.statement.query_map_one(&[], mapper).await,
            TursoPreparedParams::RowValues(params) => {
                self.statement.query_map_one(params, mapper).await
            }
            TursoPreparedParams::Buffer(params) => {
                self.statement.query_map_one_params(params, mapper).await
            }
        }
    }

    /// Execute and map the first native Turso row, if present.
    ///
    /// # Errors
    /// Returns [`SqlMiddlewareDbError`] if execution or the mapper fails.
    pub async fn map_optional<T, F>(self, mapper: F) -> Result<Option<T>, SqlMiddlewareDbError>
    where
        F: FnOnce(&turso::Row) -> Result<T, SqlMiddlewareDbError>,
    {
        match self.params {
            TursoPreparedParams::None => self.statement.query_map_optional(&[], mapper).await,
            TursoPreparedParams::RowValues(params) => {
                self.statement.query_map_optional(params, mapper).await
            }
            TursoPreparedParams::Buffer(params) => {
                self.statement
                    .query_map_optional_params(params, mapper)
                    .await
            }
        }
    }
}