rustclamp 1.0.0

A minimal synchronous entrypoint for Clamp applications.
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
//! The database types an app sees: [`Connection`], [`Row`], [`Value`],
//! [`Param`], [`FromColumn`] and [`Error`]. They name no engine, so the same
//! app code can run on another one (ADR 0034).

use std::rc::Rc;

use super::Dialect;
#[cfg(feature = "postgres")]
use super::dialect::to_postgres_placeholders;

/// A database value, as bound to a statement or read from a column.
#[derive(Debug, Clone, PartialEq)]
#[non_exhaustive]
pub enum Value {
    /// SQL `NULL`.
    Null,
    /// A whole number.
    Integer(i64),
    /// A floating-point number.
    Real(f64),
    /// Text.
    Text(String),
    /// Bytes.
    Blob(Vec<u8>),
    /// A boolean. SQLite binds it as the integer `0` or `1`.
    Bool(bool),
}

/// A database error: the engine's message, or why a column could not be read
/// as the type asked for.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Error {
    message: String,
    /// Postgres's five-character SQLSTATE; `None` for SQLite and local errors.
    sqlstate: Option<String>,
    /// SQLite's extended result code; `None` for Postgres and local errors.
    sqlite_code: Option<i32>,
}

impl Error {
    /// An error with `message` that carries a Postgres `sqlstate`.
    pub(super) fn with_sqlstate(message: impl Into<String>, sqlstate: &str) -> Self {
        Self {
            sqlstate: Some(sqlstate.to_owned()),
            ..Self::new(message)
        }
    }

    /// An error with `message`, for a [`FromColumn`] or [`Model`](super::Model)
    /// that rejects what it read.
    pub fn new(message: impl Into<String>) -> Self {
        Self {
            message: message.into(),
            sqlstate: None,
            sqlite_code: None,
        }
    }

    /// The SQLSTATE (`23505` for a unique violation) when Postgres raised
    /// this error; `None` for SQLite, which has result codes instead.
    pub fn sqlstate(&self) -> Option<&str> {
        self.sqlstate.as_deref()
    }

    /// Whether a unique or primary key constraint refused the statement, on
    /// either engine: SQLite's `SQLITE_CONSTRAINT_UNIQUE` and `_PRIMARYKEY`,
    /// Postgres's `23505`.
    pub fn is_unique_violation(&self) -> bool {
        self.sqlstate() == Some("23505")
            || matches!(
                self.sqlite_code,
                Some(
                    rusqlite::ffi::SQLITE_CONSTRAINT_UNIQUE
                        | rusqlite::ffi::SQLITE_CONSTRAINT_PRIMARYKEY
                )
            )
    }
}

impl std::fmt::Display for Error {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str(&self.message)
    }
}

impl std::error::Error for Error {}

impl From<rusqlite::Error> for Error {
    fn from(error: rusqlite::Error) -> Self {
        let sqlite_code = match &error {
            rusqlite::Error::SqliteFailure(failure, _) => Some(failure.extended_code),
            _ => None,
        };
        Self {
            sqlite_code,
            ..Self::new(error.to_string())
        }
    }
}

/// A database result.
pub type Result<T, E = Error> = std::result::Result<T, E>;

/// A value that can be bound to a `?` or `?N` mark. Implement it to store
/// your own type; [`params!`](crate::db::params) collects a list of them.
pub trait Param {
    /// The value to bind.
    fn to_value(&self) -> Value;
}

impl<T: Param + ?Sized> Param for &T {
    fn to_value(&self) -> Value {
        (**self).to_value()
    }
}

impl<T: Param> Param for Option<T> {
    fn to_value(&self) -> Value {
        self.as_ref().map_or(Value::Null, Param::to_value)
    }
}

impl Param for Value {
    fn to_value(&self) -> Value {
        self.clone()
    }
}

impl Param for i64 {
    fn to_value(&self) -> Value {
        Value::Integer(*self)
    }
}

macro_rules! small_integers {
    ($($type:ty),*) => {$(
        impl Param for $type {
            fn to_value(&self) -> Value {
                Value::Integer(i64::from(*self))
            }
        }

        impl FromColumn for $type {
            fn from_value(value: Value) -> Result<Self> {
                let n = i64::from_value(value)?;
                n.try_into()
                    .map_err(|_| Error::new(format!("{n} does not fit in {}", stringify!($type))))
            }
        }
    )*};
}

small_integers!(i32, u32);

impl Param for bool {
    fn to_value(&self) -> Value {
        Value::Bool(*self)
    }
}

impl Param for f64 {
    fn to_value(&self) -> Value {
        Value::Real(*self)
    }
}

impl Param for str {
    fn to_value(&self) -> Value {
        Value::Text(self.to_owned())
    }
}

impl Param for String {
    fn to_value(&self) -> Value {
        Value::Text(self.clone())
    }
}

impl Param for Vec<u8> {
    fn to_value(&self) -> Value {
        Value::Blob(self.clone())
    }
}

/// Stored as its hyphenated text, so it reads well in the database and in
/// `public_id` columns.
impl Param for crate::uuid::Uuid {
    fn to_value(&self) -> Value {
        Value::Text(self.to_string())
    }
}

/// A type a column can be read as, with [`Row::get`].
pub trait FromColumn: Sized {
    /// `value` as `Self`, or why it is not one.
    ///
    /// # Errors
    ///
    /// When `value` has another type, or is out of range.
    fn from_value(value: Value) -> Result<Self>;
}

fn mismatch<T>(value: &Value, wanted: &str) -> Result<T> {
    Err(Error::new(format!("expected {wanted}, found {value:?}")))
}

impl FromColumn for Value {
    fn from_value(value: Value) -> Result<Self> {
        Ok(value)
    }
}

impl<T: FromColumn> FromColumn for Option<T> {
    fn from_value(value: Value) -> Result<Self> {
        match value {
            Value::Null => Ok(None),
            value => T::from_value(value).map(Some),
        }
    }
}

impl FromColumn for i64 {
    fn from_value(value: Value) -> Result<Self> {
        match value {
            Value::Integer(n) => Ok(n),
            value => mismatch(&value, "an integer"),
        }
    }
}

/// A boolean, or an integer: `0` is false, any other integer true.
impl FromColumn for bool {
    fn from_value(value: Value) -> Result<Self> {
        match value {
            Value::Bool(flag) => Ok(flag),
            value => i64::from_value(value).map(|n| n != 0),
        }
    }
}

impl FromColumn for f64 {
    fn from_value(value: Value) -> Result<Self> {
        match value {
            Value::Real(n) => Ok(n),
            // As SQLite reads it: an integer column holding a whole number.
            Value::Integer(n) => Ok(n as f64),
            value => mismatch(&value, "a number"),
        }
    }
}

impl FromColumn for String {
    fn from_value(value: Value) -> Result<Self> {
        match value {
            Value::Text(text) => Ok(text),
            value => mismatch(&value, "text"),
        }
    }
}

impl FromColumn for Vec<u8> {
    fn from_value(value: Value) -> Result<Self> {
        match value {
            Value::Blob(bytes) => Ok(bytes),
            Value::Text(text) => Ok(text.into_bytes()),
            value => mismatch(&value, "bytes"),
        }
    }
}

impl FromColumn for crate::uuid::Uuid {
    fn from_value(value: Value) -> Result<Self> {
        let text = String::from_value(value)?;
        Self::parse(&text).ok_or_else(|| Error::new(format!("not a UUID: {text}")))
    }
}

/// A column of a [`Row`]: its position from 0, or its name.
pub trait ColumnIndex {
    /// The position among `columns`, if there is one.
    fn position(&self, columns: &[String]) -> Option<usize>;
}

impl ColumnIndex for usize {
    fn position(&self, columns: &[String]) -> Option<usize> {
        (*self < columns.len()).then_some(*self)
    }
}

/// Names compare without regard to ASCII case, as SQL does.
impl ColumnIndex for &str {
    fn position(&self, columns: &[String]) -> Option<usize> {
        columns
            .iter()
            .position(|column| column.eq_ignore_ascii_case(self))
    }
}

/// One result row.
#[derive(Debug)]
pub struct Row {
    columns: Rc<[String]>,
    values: Vec<Value>,
}

impl Row {
    /// The column `column` (a name or a position) as a `T`.
    ///
    /// # Errors
    ///
    /// When there is no such column, or its value is not a `T`.
    pub fn get<T: FromColumn>(&self, column: impl ColumnIndex) -> Result<T> {
        let Some(at) = column.position(&self.columns) else {
            return Err(Error::new(format!("no such column in {:?}", self.columns)));
        };
        T::from_value(self.values[at].clone())
            .map_err(|problem| Error::new(format!("column {}: {problem}", self.columns[at])))
    }
}

/// The connection [`Db::with`](super::Db::with), [`Db::read`](super::Db::read)
/// and a [`Tx`](super::Tx) lend. SQL marks are `?` (in order) or `?N` (the
/// `N`th value, from 1), on either engine.
#[derive(Clone, Copy)]
pub struct Connection<'a>(pub(super) Inner<'a>);

/// What a [`Connection`] lends: a SQLite connection, a Postgres client (behind
/// a `RefCell`, since the wire client needs `&mut`), or the reason there is none.
#[derive(Clone, Copy)]
pub(super) enum Inner<'a> {
    Sqlite(&'a rusqlite::Connection),
    #[cfg(feature = "postgres")]
    Postgres(&'a std::cell::RefCell<super::postgres::Client>),
    /// The pool could not lend a client; every call returns this error.
    #[cfg(feature = "postgres")]
    Unavailable(&'a Error),
}

impl std::fmt::Debug for Connection<'_> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("Connection").finish_non_exhaustive()
    }
}

impl rusqlite::ToSql for Value {
    fn to_sql(&self) -> rusqlite::Result<rusqlite::types::ToSqlOutput<'_>> {
        use rusqlite::types::ValueRef;
        Ok(rusqlite::types::ToSqlOutput::Borrowed(match self {
            Self::Null => ValueRef::Null,
            Self::Integer(n) => ValueRef::Integer(*n),
            Self::Real(n) => ValueRef::Real(*n),
            Self::Text(text) => ValueRef::Text(text.as_bytes()),
            Self::Blob(bytes) => ValueRef::Blob(bytes),
            Self::Bool(flag) => ValueRef::Integer((*flag).into()),
        }))
    }
}

fn bind(params: &[&dyn Param]) -> impl rusqlite::Params {
    rusqlite::params_from_iter(params.iter().map(|param| param.to_value()))
}

#[cfg(feature = "postgres")]
impl From<super::postgres::Error> for Error {
    fn from(error: super::postgres::Error) -> Self {
        use super::postgres::Error::{Connection, Server};
        match error {
            Server(server) => Self {
                sqlstate: Some(server.code.clone()),
                ..Self::new(Server(server).to_string())
            },
            Connection(message) => Self::new(message),
        }
    }
}

#[cfg(feature = "postgres")]
impl From<Value> for super::postgres::Datum {
    fn from(value: Value) -> Self {
        match value {
            Value::Null => Self::Null,
            Value::Integer(n) => Self::Integer(n),
            Value::Real(n) => Self::Real(n),
            Value::Text(text) => Self::Text(text),
            Value::Blob(bytes) => Self::Bytes(bytes),
            Value::Bool(flag) => Self::Bool(flag),
        }
    }
}

#[cfg(feature = "postgres")]
impl From<super::postgres::Datum> for Value {
    fn from(datum: super::postgres::Datum) -> Self {
        use super::postgres::Datum;
        match datum {
            Datum::Null => Self::Null,
            Datum::Integer(n) => Self::Integer(n),
            Datum::Real(n) => Self::Real(n),
            Datum::Text(text) => Self::Text(text),
            Datum::Bytes(bytes) => Self::Blob(bytes),
            Datum::Bool(flag) => Self::Bool(flag),
        }
    }
}

impl Connection<'_> {
    /// The engine this connection talks to.
    pub fn dialect(&self) -> Dialect {
        match self.0 {
            Inner::Sqlite(_) => Dialect::Sqlite,
            #[cfg(feature = "postgres")]
            Inner::Postgres(_) | Inner::Unavailable(_) => Dialect::Postgres,
        }
    }

    /// Whether a transaction is open on this Postgres connection. (SQLite code
    /// asks `is_autocommit` of rusqlite itself.)
    pub(super) fn in_transaction(&self) -> bool {
        match self.0 {
            #[cfg(feature = "postgres")]
            Inner::Postgres(client) => client.borrow().in_transaction(),
            _ => false,
        }
    }

    /// Whether this Postgres connection's transaction failed and can only be
    /// rolled back; SQLite keeps going after a failed statement.
    pub(super) fn aborted(&self) -> bool {
        match self.0 {
            #[cfg(feature = "postgres")]
            Inner::Postgres(client) => client.borrow().aborted(),
            _ => false,
        }
    }

    /// The id of the last row inserted on this SQLite connection.
    pub(super) fn last_insert_rowid(&self) -> i64 {
        match self.0 {
            Inner::Sqlite(connection) => connection.last_insert_rowid(),
            #[cfg(feature = "postgres")]
            _ => unreachable!("Postgres inserts read RETURNING id"),
        }
    }

    /// Runs one statement and returns how many rows it changed.
    ///
    /// # Errors
    ///
    /// When the SQL fails.
    pub fn execute(&self, sql: &str, params: &[&dyn Param]) -> Result<usize> {
        match self.0 {
            Inner::Sqlite(connection) => Ok(connection.execute(sql, bind(params))?),
            #[cfg(feature = "postgres")]
            _ => Ok(self.run(sql, params)?.affected as usize),
        }
    }

    /// Runs any number of `;`-separated statements, without parameters.
    ///
    /// # Errors
    ///
    /// When any of them fails; the ones before it stay done.
    pub fn execute_batch(&self, sql: &str) -> Result<()> {
        match self.0 {
            Inner::Sqlite(connection) => Ok(connection.execute_batch(sql)?),
            #[cfg(feature = "postgres")]
            Inner::Postgres(client) => Ok(client.borrow_mut().execute_batch(sql)?),
            #[cfg(feature = "postgres")]
            Inner::Unavailable(error) => Err(error.clone()),
        }
    }

    /// Every row `sql` returns, each turned into a `T` by `map`. For "the
    /// row, if there is one", take `.pop()` of a query that returns at most
    /// one.
    ///
    /// # Errors
    ///
    /// When the SQL or `map` fails.
    pub fn query<T>(
        &self,
        sql: &str,
        params: &[&dyn Param],
        mut map: impl FnMut(&Row) -> Result<T>,
    ) -> Result<Vec<T>> {
        let mut all = Vec::new();
        self.each(sql, params, |row| {
            all.push(map(&row)?);
            Ok(true)
        })?;
        Ok(all)
    }

    /// The first row `sql` returns, turned into a `T` by `map`.
    ///
    /// # Errors
    ///
    /// When the SQL or `map` fails, or there is no row.
    pub fn query_row<T>(
        &self,
        sql: &str,
        params: &[&dyn Param],
        map: impl FnOnce(&Row) -> Result<T>,
    ) -> Result<T> {
        let mut first = None;
        self.each(sql, params, |row| {
            first = Some(row);
            Ok(false)
        })?;
        map(&first.ok_or_else(|| Error::new("the query returned no rows"))?)
    }

    /// One bound statement on Postgres, with the marks numbered `$N`.
    #[cfg(feature = "postgres")]
    fn run(&self, sql: &str, params: &[&dyn Param]) -> Result<super::postgres::Rows> {
        let Inner::Postgres(client) = self.0 else {
            let Inner::Unavailable(error) = self.0 else {
                unreachable!("run is for Postgres")
            };
            return Err(error.clone());
        };
        let values: Vec<_> = params.iter().map(|param| param.to_value().into()).collect();
        // The borrow ends with the statement: rows are buffered, so a `map`
        // that queries again does not find the client taken.
        Ok(client
            .borrow_mut()
            .query(&to_postgres_placeholders(sql), &values)?)
    }

    /// Hands each row to `visit` while it returns `true`.
    fn each(
        &self,
        sql: &str,
        params: &[&dyn Param],
        mut visit: impl FnMut(Row) -> Result<bool>,
    ) -> Result<()> {
        use rusqlite::types::ValueRef;
        #[cfg_attr(
            not(feature = "postgres"),
            allow(clippy::infallible_destructuring_match)
        )]
        let connection = match self.0 {
            Inner::Sqlite(connection) => connection,
            #[cfg(feature = "postgres")]
            _ => {
                let result = self.run(sql, params)?;
                let columns: Rc<[String]> = result.columns.into();
                for values in result.rows {
                    let values = values.into_iter().map(Value::from).collect();
                    let columns = columns.clone();
                    if !visit(Row { columns, values })? {
                        break;
                    }
                }
                return Ok(());
            }
        };
        let mut statement = connection.prepare(sql)?;
        let columns: Rc<[String]> = statement
            .column_names()
            .into_iter()
            .map(String::from)
            .collect();
        let mut rows = statement.query(bind(params))?;
        while let Some(row) = rows.next()? {
            let values = (0..columns.len())
                .map(|at| {
                    Ok(match row.get_ref(at)? {
                        ValueRef::Null => Value::Null,
                        ValueRef::Integer(n) => Value::Integer(n),
                        ValueRef::Real(n) => Value::Real(n),
                        ValueRef::Text(text) => Value::Text(String::from_utf8_lossy(text).into()),
                        ValueRef::Blob(bytes) => Value::Blob(bytes.to_vec()),
                    })
                })
                .collect::<Result<_>>()?;
            let more = visit(Row {
                columns: columns.clone(),
                values,
            })?;
            if !more {
                break;
            }
        }
        Ok(())
    }
}

/// A list of [`Param`]s for a statement, of any types:
/// `params![title, 3, None::<String>]`.
#[doc(hidden)]
#[macro_export]
macro_rules! __db_params {
    ($($value:expr),* $(,)?) => {
        &[$(&$value as &dyn $crate::db::Param),*] as &[&dyn $crate::db::Param]
    };
}

#[cfg(test)]
mod tests {
    use crate::config::Config;
    use crate::db::testing::{each_engine, is_postgres};
    use crate::db::{Db, Error, FromColumn, Value, params};

    #[test]
    fn values_round_trip_and_mismatches_name_the_column() {
        let db = Db::open(&Config::parse("DB_DATABASE=:memory:"));
        let id = crate::uuid::Uuid::v7();
        db.with(|sql| {
            let row = sql.query_row(
                "SELECT ?1 AS n, ?2 AS t, ?3 AS b, ?4 AS r, ?5 AS z, ?6 AS u",
                params![7, "hi", true, 1.5, None::<i64>, id],
                |row| {
                    Ok((
                        row.get::<i64>("N")?,
                        row.get::<String>(1)?,
                        row.get::<bool>("b")?,
                        row.get::<f64>("r")?,
                        row.get::<Option<i64>>("z")?,
                        row.get::<crate::uuid::Uuid>("u")?,
                    ))
                },
            )?;
            assert_eq!(row, (7, "hi".into(), true, 1.5, None, id));
            let error = sql
                .query_row("SELECT 'x' AS n", &[], |row| row.get::<i64>("n"))
                .unwrap_err();
            assert!(
                error.to_string().starts_with("column n: expected"),
                "{error}"
            );
            assert!(
                sql.query_row("SELECT 1", &[], |row| row.get::<i64>("x"))
                    .is_err()
            );
            assert!(
                sql.query_row("SELECT 1 WHERE 0", &[], |row| row.get::<i64>(0))
                    .is_err()
            );
            let blob = sql.query_row("SELECT x'00ff'", &[], |row| row.get::<Value>(0))?;
            assert_eq!(blob, Value::Blob(vec![0, 255]));
            Ok::<_, crate::db::Error>(())
        })
        .unwrap();
    }

    #[test]
    fn bools_bind_as_integers_and_read_back_from_either() {
        use crate::db::Param;
        assert_eq!(true.to_value(), Value::Bool(true));
        assert_eq!(Option::<bool>::None.to_value(), Value::Null);
        assert!(bool::from_value(Value::Bool(true)).unwrap());
        assert!(!bool::from_value(Value::Integer(0)).unwrap());
        assert!(bool::from_value(Value::Integer(2)).unwrap());
        assert!(bool::from_value(Value::Text("t".into())).is_err());
        let db = Db::open(&Config::parse("DB_DATABASE=:memory:"));
        let stored: i64 = db
            .with(|sql| sql.query_row("SELECT ?1", params![true], |row| row.get(0)))
            .unwrap();
        assert_eq!(stored, 1);
    }

    #[test]
    fn unique_violations_are_told_apart_on_both_engines() {
        each_engine(|db| {
            db.with(|sql| {
                sql.execute_batch(
                    "CREATE TABLE t (id INTEGER PRIMARY KEY, n INTEGER UNIQUE, m INTEGER NOT NULL)",
                )?;
                sql.execute("INSERT INTO t VALUES (1, 1, 1)", &[])
            })
            .unwrap();
            let fail = |sql: &str| db.with(|c| c.execute(sql, &[])).unwrap_err();
            let unique = fail("INSERT INTO t VALUES (2, 1, 1)");
            assert!(unique.is_unique_violation(), "{unique}");
            assert!(fail("INSERT INTO t VALUES (1, 2, 1)").is_unique_violation());
            let null = fail("INSERT INTO t VALUES (3, 3, NULL)");
            assert!(!null.is_unique_violation(), "{null}");
            assert!(!fail("NOT SQL").is_unique_violation());
            let postgres = is_postgres(db);
            assert_eq!(unique.sqlstate(), postgres.then_some("23505"));
            assert_eq!(null.sqlstate(), postgres.then_some("23502"));
        });
        let pg = Error {
            message: "duplicate key".into(),
            sqlstate: Some("23505".into()),
            sqlite_code: None,
        };
        assert!(pg.is_unique_violation());
        assert_eq!(pg.sqlstate(), Some("23505"));
        let other = Error {
            sqlstate: Some("23503".into()),
            ..pg
        };
        assert!(!other.is_unique_violation());
        assert!(!Error::new("local").is_unique_violation());
    }

    #[cfg(feature = "postgres")]
    #[test]
    fn values_round_trip_on_postgres() {
        let Some(db) = crate::db::testing::postgres() else {
            return;
        };
        let id = crate::uuid::Uuid::v7();
        db.with(|sql| {
            // Parameters are sent untyped, so the server needs casts to type them.
            let row = sql.query_row(
                "SELECT ?1::bigint AS n, ?2::text AS t, ?3::boolean AS b, ?4::double precision AS r, ?5::bigint AS z, ?6::text AS u, ?7::bytea AS x",
                params![7, "hi", true, 1.5, None::<i64>, id, vec![0u8, 255]],
                |row| {
                    Ok((
                        row.get::<i64>("N")?,
                        row.get::<String>(1)?,
                        row.get::<bool>("b")?,
                        row.get::<f64>("r")?,
                        row.get::<Option<i64>>("z")?,
                        row.get::<crate::uuid::Uuid>("u")?,
                        row.get::<Value>("x")?,
                    ))
                },
            )?;
            assert_eq!(row, (7, "hi".into(), true, 1.5, None, id, Value::Blob(vec![0, 255])));
            let error = sql
                .query_row("SELECT 'x' AS n", &[], |row| row.get::<i64>("n"))
                .unwrap_err();
            assert!(error.to_string().starts_with("column n: expected"), "{error}");
            // Marks in literals and comments are not parameters.
            let marks = sql.query_row("SELECT '?' || ?1::text -- ?", params!["x"], |row| row.get::<String>(0))?;
            assert_eq!(marks, "?x");
            Ok::<_, Error>(())
        })
        .unwrap();
    }
}