prax-sqlite 0.8.2

SQLite database driver for Prax 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
//! SQLite query engine implementing `prax_query::QueryEngine`.

use std::sync::Arc;

use prax_query::error::{QueryError, QueryResult};
use prax_query::filter::FilterValue;
use prax_query::row::FromRow;
use prax_query::traits::{BoxFuture, Model, QueryEngine};
use rusqlite::types::Value as SqlValue;
use tokio_rusqlite::Connection as RusqliteConnection;
use tracing::trace;

use crate::connection::SqliteConnection;
use crate::pool::SqlitePool;
use crate::row_ref::SqliteRowRef;
use crate::types::filter_value_to_sqlite;

/// SQLite query engine backed by `tokio_rusqlite`.
///
/// # Breaking changes (0.7)
///
/// `SqliteEngine` no longer has inherent `query` / `query_one` / `query_opt`
/// methods that returned untyped `RowData` / `serde_json::Value`. It now
/// implements [`prax_query::traits::QueryEngine`], whose row-returning
/// methods are generic over `T: Model + FromRow` and return typed models.
///
/// Migration:
/// - Replace `engine.query(sql, params)` with
///   `engine.query_many::<YourType>(sql, params).await?`, where `YourType`
///   carries `#[derive(prax_orm::Model)]` (which emits both `Model` and
///   `FromRow`) or hand-written `impl Model + impl FromRow`.
/// - For ad-hoc typed queries without a full `Model`, bridge through
///   [`crate::row_ref::SqliteRowRef::from_rusqlite`] inside a custom
///   `FromRow` impl.
/// - For the legacy JSON-blob API, use [`crate::raw::SqliteRawEngine`] +
///   [`crate::raw::SqliteJsonRow`].
/// - To run side-effecting SQL that returns no rows, call
///   [`prax_query::traits::QueryEngine::execute_raw`].
///
/// See `CHANGELOG.md` for the full migration guide.
///
/// # Transaction mode
///
/// `SqliteEngine` has two modes, controlled by `tx_conn`:
///
/// - **Pool mode** (`tx_conn == None`, default): each query acquires
///   a fresh [`SqliteConnection`] from the pool and drops it after
///   the call.
/// - **Transaction mode** (`tx_conn == Some(..)`): each query routes
///   through the same [`tokio_rusqlite::Connection`] handle so the
///   whole BEGIN…COMMIT block serialises onto the connection's
///   background thread in order. The `Arc<SqliteConnection>` keeps
///   the pool permit alive for the transaction's lifetime; dropping
///   the last clone returns the connection to the idle pool.
#[derive(Clone)]
pub struct SqliteEngine {
    pool: SqlitePool,
    /// Present when this engine is bound to an in-flight transaction.
    /// `None` in the normal pool-backed case.
    tx_conn: Option<Arc<SqliteConnection>>,
}

impl SqliteEngine {
    /// Create a new engine with the given pool.
    pub fn new(pool: SqlitePool) -> Self {
        Self {
            pool,
            tx_conn: None,
        }
    }

    /// Get a reference to the connection pool.
    pub fn pool(&self) -> &SqlitePool {
        &self.pool
    }

    /// Resolve which raw [`tokio_rusqlite::Connection`] to run the
    /// query against, along with an optional owned
    /// [`SqliteConnection`] guard whose `Drop` returns the
    /// connection to the idle pool once we're done with it.
    ///
    /// In tx mode the guard is `None` because the pinned connection
    /// is already kept alive by the `Arc<SqliteConnection>` on
    /// `self.tx_conn`. In pool mode we hand back a fresh guard so
    /// the caller can clone the inner handle, drive one `call(..)`
    /// through it, and drop the guard on the way out.
    async fn resolve_conn(&self) -> QueryResult<(RusqliteConnection, Option<SqliteConnection>)> {
        if let Some(tx) = &self.tx_conn {
            Ok((tx.inner().clone(), None))
        } else {
            let conn = self
                .pool
                .get()
                .await
                .map_err(|e| QueryError::connection(e.to_string()).with_source(e))?;
            let handle = conn.inner().clone();
            Ok((handle, Some(conn)))
        }
    }

    fn bind(params: &[FilterValue]) -> Vec<SqlValue> {
        params.iter().map(filter_value_to_sqlite).collect()
    }

    /// Decode multiple rows into typed models.
    ///
    /// # Short-circuit on decode error
    ///
    /// Uses `Result<Vec<T>, _>::collect`, which returns the first decode
    /// error and discards every successfully-decoded row before it. A
    /// row-level type mismatch therefore aborts the whole batch rather
    /// than returning partial results. Callers that want per-row
    /// recovery should manually iterate rows and handle each result.
    async fn query_rows<T: Model + FromRow>(
        &self,
        sql: String,
        params: Vec<FilterValue>,
    ) -> QueryResult<Vec<T>> {
        trace!(sql = %sql, "sqlite query_rows");
        let (handle, _guard) = self.resolve_conn().await?;
        let bound = Self::bind(&params);
        let snapshots: Vec<SqliteRowRef> = handle
            .call(move |c| {
                let mut stmt = c.prepare(&sql)?;
                let refs: Vec<&dyn rusqlite::ToSql> =
                    bound.iter().map(|v| v as &dyn rusqlite::ToSql).collect();
                let mut rows = stmt.query(refs.as_slice())?;
                let mut out = Vec::new();
                while let Some(row) = rows.next()? {
                    out.push(SqliteRowRef::from_rusqlite(row).map_err(|e| {
                        rusqlite::Error::ToSqlConversionFailure(Box::new(std::io::Error::other(
                            e.to_string(),
                        )))
                    })?);
                }
                Ok(out)
            })
            .await
            .map_err(|e| QueryError::database(e.to_string()).with_source(e))?;

        snapshots
            .into_iter()
            .map(|r| {
                T::from_row(&r).map_err(|e| {
                    let msg = e.to_string();
                    QueryError::deserialization(msg).with_source(e)
                })
            })
            .collect()
    }

    /// Stop after the first row so callers that want a single row do not pay
    /// for materializing the tail. Naively routing `query_one`/`query_optional`
    /// through `query_rows` + `.pop()` would decode every matching row and
    /// throw away all but one; a caller who accidentally asked for a single
    /// row from a million-row table would allocate a million typed models.
    async fn query_first_row<T: Model + FromRow>(
        &self,
        sql: String,
        params: Vec<FilterValue>,
    ) -> QueryResult<Option<T>> {
        trace!(sql = %sql, "sqlite query_first_row");
        let (handle, _guard) = self.resolve_conn().await?;
        let bound = Self::bind(&params);
        let snapshot: Option<SqliteRowRef> = handle
            .call(move |c| {
                let mut stmt = c.prepare(&sql)?;
                let refs: Vec<&dyn rusqlite::ToSql> =
                    bound.iter().map(|v| v as &dyn rusqlite::ToSql).collect();
                let mut rows = stmt.query(refs.as_slice())?;
                match rows.next()? {
                    Some(row) => Ok(Some(SqliteRowRef::from_rusqlite(row).map_err(|e| {
                        rusqlite::Error::ToSqlConversionFailure(Box::new(std::io::Error::other(
                            e.to_string(),
                        )))
                    })?)),
                    None => Ok(None),
                }
            })
            .await
            .map_err(|e| QueryError::database(e.to_string()).with_source(e))?;

        snapshot
            .map(|r| {
                T::from_row(&r).map_err(|e| {
                    let msg = e.to_string();
                    QueryError::deserialization(msg).with_source(e)
                })
            })
            .transpose()
    }

    async fn exec_raw(&self, sql: String, params: Vec<FilterValue>) -> QueryResult<u64> {
        let (handle, _guard) = self.resolve_conn().await?;
        let bound = Self::bind(&params);
        let n = handle
            .call(move |c| {
                let refs: Vec<&dyn rusqlite::ToSql> =
                    bound.iter().map(|v| v as &dyn rusqlite::ToSql).collect();
                Ok(c.execute(&sql, refs.as_slice())?)
            })
            .await
            .map_err(|e| QueryError::database(e.to_string()).with_source(e))?;
        Ok(n as u64)
    }

    async fn count_rows(&self, sql: String, params: Vec<FilterValue>) -> QueryResult<u64> {
        let (handle, _guard) = self.resolve_conn().await?;
        let bound = Self::bind(&params);
        let n = handle
            .call(move |c| {
                let refs: Vec<&dyn rusqlite::ToSql> =
                    bound.iter().map(|v| v as &dyn rusqlite::ToSql).collect();
                let mut stmt = c.prepare(&sql)?;
                let n: i64 = stmt.query_row(refs.as_slice(), |r| r.get(0))?;
                Ok(n)
            })
            .await
            .map_err(|e| QueryError::database(e.to_string()).with_source(e))?;
        Ok(n as u64)
    }
}

impl QueryEngine for SqliteEngine {
    fn dialect(&self) -> &dyn prax_query::dialect::SqlDialect {
        &prax_query::dialect::Sqlite
    }

    fn query_many<T: Model + FromRow + Send + 'static>(
        &self,
        sql: &str,
        params: Vec<FilterValue>,
    ) -> BoxFuture<'_, QueryResult<Vec<T>>> {
        let sql = sql.to_string();
        Box::pin(self.query_rows::<T>(sql, params))
    }

    fn query_one<T: Model + FromRow + Send + 'static>(
        &self,
        sql: &str,
        params: Vec<FilterValue>,
    ) -> BoxFuture<'_, QueryResult<T>> {
        let sql = sql.to_string();
        Box::pin(async move {
            self.query_first_row::<T>(sql, params)
                .await?
                .ok_or_else(|| QueryError::not_found(T::MODEL_NAME))
        })
    }

    fn query_optional<T: Model + FromRow + Send + 'static>(
        &self,
        sql: &str,
        params: Vec<FilterValue>,
    ) -> BoxFuture<'_, QueryResult<Option<T>>> {
        let sql = sql.to_string();
        Box::pin(self.query_first_row::<T>(sql, params))
    }

    fn execute_insert<T: Model + FromRow + Send + 'static>(
        &self,
        sql: &str,
        params: Vec<FilterValue>,
    ) -> BoxFuture<'_, QueryResult<T>> {
        // SQLite 3.35+ supports INSERT ... RETURNING. INSERT RETURNING yields
        // at most one row per inserted tuple; query_first_row avoids ever
        // materializing a tail if the caller's SQL yields many (which would
        // be a misuse, but the engine shouldn't punish it with unbounded
        // allocation).
        let sql = sql.to_string();
        Box::pin(async move {
            self.query_first_row::<T>(sql, params)
                .await?
                .ok_or_else(|| QueryError::not_found(T::MODEL_NAME))
        })
    }

    fn execute_update<T: Model + FromRow + Send + 'static>(
        &self,
        sql: &str,
        params: Vec<FilterValue>,
    ) -> BoxFuture<'_, QueryResult<Vec<T>>> {
        let sql = sql.to_string();
        Box::pin(self.query_rows::<T>(sql, params))
    }

    fn execute_delete(
        &self,
        sql: &str,
        params: Vec<FilterValue>,
    ) -> BoxFuture<'_, QueryResult<u64>> {
        let sql = sql.to_string();
        Box::pin(self.exec_raw(sql, params))
    }

    fn execute_raw(&self, sql: &str, params: Vec<FilterValue>) -> BoxFuture<'_, QueryResult<u64>> {
        let sql = sql.to_string();
        Box::pin(self.exec_raw(sql, params))
    }

    fn count(&self, sql: &str, params: Vec<FilterValue>) -> BoxFuture<'_, QueryResult<u64>> {
        let sql = sql.to_string();
        Box::pin(self.count_rows(sql, params))
    }

    fn aggregate_query(
        &self,
        sql: &str,
        params: Vec<FilterValue>,
    ) -> BoxFuture<'_, QueryResult<Vec<std::collections::HashMap<String, FilterValue>>>> {
        let sql = sql.to_string();
        Box::pin(async move {
            trace!(sql = %sql, "sqlite aggregate_query");
            let (handle, _guard) = self.resolve_conn().await?;
            let bound = Self::bind(&params);
            let rows: Vec<std::collections::HashMap<String, FilterValue>> = handle
                .call(move |c| {
                    let mut stmt = c.prepare(&sql)?;
                    let column_names: Vec<String> =
                        stmt.column_names().iter().map(|s| s.to_string()).collect();
                    let refs: Vec<&dyn rusqlite::ToSql> =
                        bound.iter().map(|v| v as &dyn rusqlite::ToSql).collect();
                    let mut rows = stmt.query(refs.as_slice())?;
                    let mut out = Vec::new();
                    while let Some(row) = rows.next()? {
                        let mut map = std::collections::HashMap::new();
                        for (i, name) in column_names.iter().enumerate() {
                            // SQLite storage classes are dynamic per-row:
                            // INTEGER, REAL, TEXT, BLOB, NULL. Pull each
                            // cell as an untyped `Value` and project into
                            // the closest `FilterValue` variant. BLOB
                            // becomes `Null` — aggregate results never
                            // return BLOB in practice, and surfacing raw
                            // bytes through FilterValue doesn't buy
                            // anything for the caller.
                            let v: SqlValue = row.get(i).unwrap_or(SqlValue::Null);
                            let fv = match v {
                                SqlValue::Null => FilterValue::Null,
                                SqlValue::Integer(n) => FilterValue::Int(n),
                                SqlValue::Real(f) => FilterValue::Float(f),
                                SqlValue::Text(s) => FilterValue::String(s),
                                SqlValue::Blob(_) => FilterValue::Null,
                            };
                            map.insert(name.clone(), fv);
                        }
                        out.push(map);
                    }
                    Ok(out)
                })
                .await
                .map_err(|e| QueryError::database(e.to_string()).with_source(e))?;
            Ok(rows)
        })
    }

    fn transaction<'a, R, Fut, F>(&'a self, f: F) -> BoxFuture<'a, QueryResult<R>>
    where
        F: FnOnce(Self) -> Fut + Send + 'a,
        Fut: std::future::Future<Output = QueryResult<R>> + Send + 'a,
        R: Send + 'a,
        Self: Clone,
    {
        Box::pin(async move {
            // Refuse nested transactions until savepoint support
            // lands. Callers can still drive SAVEPOINT / RELEASE
            // manually via `execute_raw` if they need it.
            if self.tx_conn.is_some() {
                return Err(QueryError::internal(
                    "nested transactions not yet implemented \
                     (call .transaction() on the outer engine only, or \
                     issue SAVEPOINT via execute_raw)",
                ));
            }

            // Pin a single pooled connection. Wrapping it in `Arc`
            // keeps the pool permit alive for the whole transaction
            // and makes every engine clone share the same
            // `tokio_rusqlite::Connection` handle — every query
            // dispatched through the closure's engine therefore
            // serialises onto the same background thread as our
            // initial `BEGIN`.
            let conn = self
                .pool
                .get()
                .await
                .map_err(|e| QueryError::connection(e.to_string()).with_source(e))?;
            let handle = conn.inner().clone();
            handle
                .call(|c| {
                    c.execute_batch("BEGIN")?;
                    Ok(())
                })
                .await
                .map_err(|e| QueryError::database(e.to_string()).with_source(e))?;

            let tx_conn = Arc::new(conn);
            let tx_engine = SqliteEngine {
                pool: self.pool.clone(),
                tx_conn: Some(tx_conn.clone()),
            };

            let result = f(tx_engine).await;

            // Finalise: COMMIT on success, best-effort ROLLBACK on
            // failure. Preserve the caller's error if ROLLBACK fails —
            // SQLite's autocommit resumes on the next statement
            // regardless, and dropping the connection releases any
            // lingering tx state.
            match result {
                Ok(v) => {
                    handle
                        .call(|c| {
                            c.execute_batch("COMMIT")?;
                            Ok(())
                        })
                        .await
                        .map_err(|e| QueryError::database(e.to_string()).with_source(e))?;
                    Ok(v)
                }
                Err(e) => {
                    let _ = handle
                        .call(|c| {
                            c.execute_batch("ROLLBACK")?;
                            Ok(())
                        })
                        .await;
                    Err(e)
                }
            }
        })
    }
}