sqll 0.14.3

Efficient interface to SQLite that doesn't get in your way
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
//! Typed wrapper around [`SendStatement`] that encodes the expected bind
//! parameter and column counts in the type system. This allows for ahead of
//! time checking of bind and column counts, and provides a more ergonomic API
//! for executing statements and retrieving rows.

use core::fmt;
use core::marker::PhantomData;
use core::mem::ManuallyDrop;

use crate::{Bind, Error, Row, SendStatement};

/// A typed wrapper around [`SendStatement`] that encodes the expected bind
/// parameter and column counts in the type system. This allows for ahead of
/// time checking of bind and column counts, and provides a more ergonomic API
/// for executing statements and retrieving rows.
///
/// The typical use case for this is in combination with the [`Statements`]
/// derive macro, which generates a struct of typed statements.
///
/// This encodes invariants of the low level API which makes it more
/// abuse-resistant. For example, it ensures that a statement is appropriately
/// reset between uses which ensures that write transactions are actually
/// persisted.
///
/// [`Statements`]: derive@crate::Statements
///
/// # Examples
///
/// ```
/// use sqll::{Statements, TypedStatement};
///
/// #[derive(Statements)]
/// #[sql(read_only)]
/// struct Read {
///     #[sql = "SELECT name, age FROM users ORDER BY age"]
///     all_users: TypedStatement<(), (String, i32)>,
/// }
///
/// #[derive(Statements)]
/// struct Write {
///     // Reuse every statement in `Read`, accessible as `write.read.all_users`.
///     #[sql(statements)]
///     read: Read,
///     #[sql = "INSERT INTO users (name, age) VALUES (?, ?)"]
///     insert_user: TypedStatement<(String, i32), ()>,
/// }
/// ```
pub struct TypedStatement<I, O> {
    inner: SendStatement,
    _marker: PhantomData<(I, O)>,
}

impl<O> TypedStatement<(), O> {
    /// Return a bound statement that is used only to retrieve rows without any
    /// bind parameters.
    ///
    /// This is the same as calling [`TypedStatement::bind`] with an empty
    /// tuple.
    ///
    /// See [`TypedStatement::bind`] for more details and examples.
    pub fn query(&mut self) -> Result<BoundStatement<'_, (), O>, Error> {
        self.bind(())
    }
}

impl<I> TypedStatement<I, ()>
where
    I: Bind,
{
    /// Execute the statement with the specified bind parameters, this is not
    /// expected to return any rows.
    ///
    /// # Examples
    ///
    /// ```
    /// use sqll::{OpenOptions, Statements, TypedStatement};
    ///
    /// #[derive(Statements)]
    /// struct Write {
    ///     #[sql = "INSERT INTO users (name, age) VALUES (?, ?)"]
    ///     insert_user: TypedStatement<(String, i32), ()>,
    /// }
    ///
    /// let mut c = OpenOptions::new()
    ///     .no_mutex()
    ///     .read_write()
    ///     .create()
    ///     .open_in_memory()?;
    ///
    /// c.execute("CREATE TABLE users (name TEXT, age INTEGER)")?;
    ///
    /// let mut write = Write::build(&mut c)?;
    /// write.insert_user.execute(("Alice".to_string(), 25))?;
    /// # Ok::<(), Box<dyn core::error::Error>>(())
    /// ```
    #[track_caller]
    pub fn execute<B>(&mut self, bind: B) -> Result<(), Error>
    where
        B: Bind,
    {
        const {
            assert!(
                I::COUNT == B::COUNT,
                "unexpected bind parameter count for statement",
            );
        }

        self.inner.reset()?;
        self.inner.bind(bind)?;
        while !self.inner.step()?.is_done() {}
        Ok(())
    }
}

impl<I, O> TypedStatement<I, O>
where
    I: Bind,
{
    /// Bind the given parameters to the statement, returning a [`BoundStatement`]
    /// that can be used to execute the statement or retrieve rows. The bind
    /// parameter count is checked at compile time, so if the provided bind
    /// parameters do not match the expected count, this will result in a
    /// compile time error.
    ///
    /// # Examples
    ///
    /// ```
    /// use sqll::{OpenOptions, Statements, TypedStatement};
    ///
    /// #[derive(Statements)]
    /// #[sql(read_only)]
    /// struct Read {
    ///     #[sql = "SELECT name, age FROM users WHERE age > ? ORDER BY age"]
    ///     users_older_than: TypedStatement<(i32,), (String, i32)>,
    /// }
    ///
    /// let mut c = OpenOptions::new()
    ///     .no_mutex()
    ///     .read_write()
    ///     .create()
    ///     .open_in_memory()?;
    ///
    /// c.execute("CREATE TABLE users (name TEXT, age INTEGER)")?;
    /// c.execute("INSERT INTO users (name, age) VALUES ('Alice', 25), ('Bob', 35), ('Charlie', 30)")?;
    ///
    /// let mut read = Read::build(&mut c)?;
    /// let mut stmt = read.users_older_than.bind((30,))?;
    ///
    /// let mut out = Vec::new();
    ///
    /// while let Some((name, age)) = stmt.next()? {
    ///     out.push((name, age));
    /// }
    ///
    /// assert_eq!(out, vec![("Bob".to_string(), 35)]);
    /// # Ok::<(), Box<dyn core::error::Error>>(())
    /// ```
    #[track_caller]
    pub fn bind<B>(&mut self, bind: B) -> Result<BoundStatement<'_, I, O>, Error>
    where
        B: Bind,
    {
        const {
            assert!(
                I::COUNT == B::COUNT,
                "unexpected bind parameter count for statement",
            );
        }

        self.inner.reset()?;
        self.inner.bind(bind)?;
        Ok(BoundStatement {
            stmt: &mut self.inner,
            _marker: PhantomData,
        })
    }
}

#[derive(Debug)]
enum TryFromSendStatementErrorKind {
    BindParameterCount { expected: i32, actual: i32 },
    ColumnCount { expected: i32, actual: i32 },
}

/// An error that can occur when trying to convert a [`SendStatement`] into a
/// [`TypedStatement`], if the bind parameter count or column count does not
/// match the expected counts encoded in the type parameters of the
/// [`TypedStatement`].
pub struct TryFromSendStatementError {
    kind: TryFromSendStatementErrorKind,
}

impl fmt::Debug for TryFromSendStatementError {
    #[inline]
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        self.kind.fmt(f)
    }
}

impl fmt::Display for TryFromSendStatementError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self.kind {
            TryFromSendStatementErrorKind::BindParameterCount { expected, actual } => write!(
                f,
                "unexpected bind parameter count for statement: expected {expected}, got {actual}",
            ),
            TryFromSendStatementErrorKind::ColumnCount { expected, actual } => write!(
                f,
                "unexpected column count for statement: expected {expected}, got {actual}",
            ),
        }
    }
}

impl core::error::Error for TryFromSendStatementError {}

impl<I, O> TryFrom<SendStatement> for TypedStatement<I, O>
where
    I: Bind,
    O: for<'stmt> Row<'stmt>,
{
    type Error = TryFromSendStatementError;

    fn try_from(inner: SendStatement) -> Result<Self, TryFromSendStatementError> {
        if inner.bind_parameter_count() != I::COUNT as i32 {
            return Err(TryFromSendStatementError {
                kind: TryFromSendStatementErrorKind::BindParameterCount {
                    expected: I::COUNT as i32,
                    actual: inner.bind_parameter_count(),
                },
            });
        }

        if inner.column_count() != O::COUNT as i32 {
            return Err(TryFromSendStatementError {
                kind: TryFromSendStatementErrorKind::ColumnCount {
                    expected: O::COUNT as i32,
                    actual: inner.column_count(),
                },
            });
        }

        Ok(Self {
            inner,
            _marker: PhantomData,
        })
    }
}

/// A [`TypedStatement`] that has been bound with parameters, ready to execute
/// or retrieve rows.
///
/// When a `BoundStatement` is dropped it resets the underlying statement (any
/// reset error is ignored), so the statement can be bound again even if it was
/// only partially read. Use [`reset`] instead when you need to observe a reset
/// error.
///
/// See [`TypedStatement::bind`] for more details and examples.
///
/// # Examples
///
/// Dropping a bound statement mid-iteration resets it, so the next bind replays
/// from the top:
///
/// ```
/// use sqll::{OpenOptions, Statements, TypedStatement};
///
/// #[derive(Statements)]
/// #[sql(read_only)]
/// struct Read {
///     #[sql = "SELECT name, age FROM users ORDER BY age"]
///     all: TypedStatement<(), (String, i32)>,
/// }
///
/// let mut c = OpenOptions::new()
///     .no_mutex()
///     .read_write()
///     .create()
///     .open_in_memory()?;
///
/// c.execute("CREATE TABLE users (name TEXT, age INTEGER)")?;
/// c.execute("INSERT INTO users (name, age) VALUES ('Alice', 25), ('Bob', 35)")?;
///
/// let mut read = Read::build(&mut c)?;
///
/// // Read a single row, then drop the bound statement mid-iteration.
/// let mut stmt = read.all.query()?;
/// assert_eq!(stmt.next()?, Some(("Alice".to_string(), 25)));
/// drop(stmt);
///
/// assert_eq!(read.all.query()?.first()?, Some(("Alice".to_string(), 25)));
/// # Ok::<(), Box<dyn core::error::Error>>(())
/// ```
///
/// [`reset`]: BoundStatement::reset
pub struct BoundStatement<'stmt, I, O> {
    stmt: &'stmt mut SendStatement,
    _marker: PhantomData<(I, O)>,
}

impl<I, O> BoundStatement<'_, I, O> {
    /// Consume the bound statement and return the first row, if any, resetting
    /// the underlying statement. This is a convenient way to execute a query
    /// that is expected to return at most one row, without needing to manually
    /// reset the statement or handle multiple rows.
    ///
    /// # Examples
    ///
    /// `first` returns only the first matching row, discards the rest, and
    /// resets the statement so it can be bound again immediately:
    ///
    /// ```
    /// use sqll::{OpenOptions, Statements, TypedStatement};
    ///
    /// #[derive(Statements)]
    /// #[sql(read_only)]
    /// struct Read {
    ///     #[sql = "SELECT name, age FROM users WHERE age > ? ORDER BY age"]
    ///     older_than: TypedStatement<(i32,), (String, i32)>,
    /// }
    ///
    /// let mut c = OpenOptions::new().no_mutex().read_write().create().open_in_memory()?;
    ///
    /// c.execute("CREATE TABLE users (name TEXT, age INTEGER)")?;
    /// c.execute("INSERT INTO users (name, age) VALUES ('Alice', 25), ('Bob', 35)")?;
    ///
    /// let mut read = Read::build(&mut c)?;
    ///
    /// // Two rows match, but only the first is returned.
    /// let first = read.older_than.bind((10,))?.first()?;
    /// assert_eq!(first, Some(("Alice".to_string(), 25)));
    ///
    /// // The statement was reset, so a fresh bind replays from the top.
    /// let again = read.older_than.bind((10,))?.first()?;
    /// assert_eq!(again, Some(("Alice".to_string(), 25)));
    ///
    /// // When nothing matches, `first` yields `None` rather than erroring.
    /// let none = read.older_than.bind((100,))?.first()?;
    /// assert_eq!(none, None);
    /// # Ok::<(), Box<dyn core::error::Error>>(())
    /// ```
    pub fn first(self) -> Result<Option<O>, Error>
    where
        O: for<'stmt> Row<'stmt>,
    {
        let value = self.stmt.next()?;
        let mut this = ManuallyDrop::new(self);
        this.stmt.reset()?;
        Ok(value)
    }

    /// Return the next row, if any, without resetting the underlying statement.
    ///
    /// This is a convenient way to execute a query that is expected to return
    /// multiple rows, allowing the caller to iterate over them without needing
    /// to manually reset the statement between rows. The caller is responsible
    /// for resetting the statement when finished, either by calling `reset` or
    /// by letting the bound statement go out of scope.
    ///
    /// # Examples
    ///
    /// `next` yields each row in turn, returning `None` once the result set is
    /// exhausted (which terminates the loop below):
    ///
    /// ```
    /// use sqll::{OpenOptions, Statements, TypedStatement};
    ///
    /// #[derive(Statements)]
    /// #[sql(read_only)]
    /// struct Read {
    ///     #[sql = "SELECT name, age FROM users ORDER BY age"]
    ///     all: TypedStatement<(), (String, i32)>,
    /// }
    ///
    /// let mut c = OpenOptions::new().no_mutex().read_write().create().open_in_memory()?;
    ///
    /// c.execute("CREATE TABLE users (name TEXT, age INTEGER)")?;
    /// c.execute("INSERT INTO users (name, age) VALUES ('Alice', 25), ('Bob', 35)")?;
    ///
    /// let mut read = Read::build(&mut c)?;
    /// let mut stmt = read.all.query()?;
    ///
    /// let mut out = Vec::new();
    /// while let Some(row) = stmt.next()? {
    ///     out.push(row);
    /// }
    /// assert_eq!(out, [("Alice".to_string(), 25), ("Bob".to_string(), 35)]);
    /// # Ok::<(), Box<dyn core::error::Error>>(())
    /// ```
    #[inline]
    pub fn next(&mut self) -> Result<Option<O>, Error>
    where
        O: for<'stmt> Row<'stmt>,
    {
        self.stmt.next()
    }

    /// Consume the bound statement and reset the underlying statement,
    /// surfacing any error. Unlike `Drop` (which resets silently), this lets a
    /// caller release the statement between sequential uses without a scope and
    /// still propagate a reset error.
    ///
    /// # Examples
    ///
    /// `reset` releases the statement after a partial read, leaving it ready to
    /// be bound again from the top:
    ///
    /// ```
    /// use sqll::{OpenOptions, Statements, TypedStatement};
    ///
    /// #[derive(Statements)]
    /// #[sql(read_only)]
    /// struct Read {
    ///     #[sql = "SELECT name, age FROM users ORDER BY age"]
    ///     all: TypedStatement<(), (String, i32)>,
    /// }
    ///
    /// let mut c = OpenOptions::new().no_mutex().read_write().create().open_in_memory()?;
    ///
    /// c.execute("CREATE TABLE users (name TEXT, age INTEGER)")?;
    /// c.execute("INSERT INTO users (name, age) VALUES ('Alice', 25), ('Bob', 35)")?;
    ///
    /// let mut read = Read::build(&mut c)?;
    ///
    /// let mut stmt = read.all.query()?;
    /// assert_eq!(stmt.next()?, Some(("Alice".to_string(), 25)));
    /// stmt.reset()?; // stop early and surface any reset error
    ///
    /// // The statement replays from the top on the next bind.
    /// assert_eq!(read.all.query()?.first()?, Some(("Alice".to_string(), 25)));
    /// # Ok::<(), Box<dyn core::error::Error>>(())
    /// ```
    pub fn reset(self) -> Result<(), Error> {
        // Skip the `Drop` reset: we reset here and surface the error instead.
        let mut this = ManuallyDrop::new(self);
        this.stmt.reset()?;
        Ok(())
    }
}

impl<I, O> Drop for BoundStatement<'_, I, O> {
    fn drop(&mut self) {
        // Ensure the statement is reset when the bound statement goes out of
        // scope, so that the underlying statement can be reused.
        let _ = self.stmt.reset();
    }
}