Skip to main content

persistant_sqlx/
lib.rs

1// SPDX-License-Identifier: MIT OR Apache-2.0
2//! PersistAnt's relational driver over sqlx (PostgreSQL first).
3//!
4//! [`PgBackend`] implements [`persistant_relational::RelationalBackend`]. The concrete
5//! [`sqlx::PgPool`] is available through [`PgBackend::pool`], so a program that wants
6//! compile-time-checked `sqlx::query!` keeps it (D1, C+): this crate does not hide sqlx.
7//!
8//! Failures are classified from the server's SQLSTATE into [`persistant_relational::RelError`].
9
10mod bounded;
11#[cfg(feature = "bridge")]
12mod bridge;
13mod decode;
14mod migrate;
15#[cfg(feature = "mysql")]
16mod mysql;
17mod options;
18#[cfg(all(feature = "sqlite", windows))]
19mod owner_only;
20#[cfg(feature = "sqlite")]
21mod sqlite;
22
23pub use bounded::{Bounded, BoundedTx, call_bound_from_env, parse_bound};
24#[cfg(feature = "bridge")]
25pub use bridge::{OnConn, OnPool};
26pub use migrate::{FromSqlxError, migrations_from_sqlx};
27#[cfg(feature = "mysql")]
28pub use mysql::{MysqlBackend, MysqlTx};
29pub use options::{ConnectOptions, TlsPolicy};
30pub use persistant_relational::{MigrateError, MigrateReport, Migrates, Migration};
31#[cfg(feature = "sqlite")]
32pub use sqlite::{SqliteBackend, SqliteTx};
33
34use persistant_relational::{
35    Dialect, Isolation, RelError, RelationalBackend, RelationalTx, Row, Value,
36};
37use sqlx::postgres::{PgArguments, PgPoolOptions, PgRow};
38use sqlx::query::Query;
39use sqlx::{Column, Postgres, Row as SqlxRow, TypeInfo, ValueRef};
40
41/// Classify an sqlx error by SQLSTATE (https://www.postgresql.org/docs/current/errcodes-appendix.html).
42pub(crate) fn classify(e: sqlx::Error) -> RelError {
43    match &e {
44        sqlx::Error::Database(db) => {
45            let constraint = db.constraint().map(str::to_string);
46            match db.code().as_deref() {
47                Some("23505") => RelError::UniqueViolation { constraint },
48                Some("23503") => RelError::ForeignKeyViolation { constraint },
49                Some("23514") | Some("23502") => RelError::CheckViolation { constraint },
50                Some("40001") => RelError::Serialization,
51                Some("40P01") => RelError::Deadlock,
52                Some(c) if c.starts_with("08") || c.starts_with("57P") => {
53                    RelError::Unavailable(e.to_string())
54                }
55                _ => RelError::Backend(e.to_string()),
56            }
57        }
58        sqlx::Error::Io(_) | sqlx::Error::PoolTimedOut | sqlx::Error::PoolClosed => {
59            RelError::Unavailable(e.to_string())
60        }
61        _ => RelError::Backend(e.to_string()),
62    }
63}
64
65/// A `NULL` whose type the server infers from the statement. Binding `Option::<String>::None` sends
66/// the type `text`, which the server rejects for a `bigint` column (`expression is of type text`) the
67/// first time a pooled connection prepares that statement, and not on one that already has: a failure
68/// that depends on which connection the pool hands out. Type OID 0 means "unspecified".
69struct UntypedNull;
70
71impl sqlx::Type<Postgres> for UntypedNull {
72    fn type_info() -> sqlx::postgres::PgTypeInfo {
73        sqlx::postgres::PgTypeInfo::with_oid(sqlx::postgres::types::Oid(0))
74    }
75
76    fn compatible(_: &sqlx::postgres::PgTypeInfo) -> bool {
77        true
78    }
79}
80
81impl<'q> sqlx::Encode<'q, Postgres> for UntypedNull {
82    fn encode_by_ref(
83        &self,
84        _: &mut sqlx::postgres::PgArgumentBuffer,
85    ) -> Result<sqlx::encode::IsNull, sqlx::error::BoxDynError> {
86        Ok(sqlx::encode::IsNull::Yes)
87    }
88}
89
90fn bind<'q>(
91    mut q: Query<'q, Postgres, PgArguments>,
92    params: &'q [Value],
93) -> Result<Query<'q, Postgres, PgArguments>, RelError> {
94    for p in params {
95        q = match p {
96            // A NULL of unspecified type: the server infers it from the statement.
97            Value::Null => q.bind(UntypedNull),
98            Value::Bool(b) => q.bind(*b),
99            Value::Int(n) => q.bind(*n),
100            Value::Float(f) => q.bind(*f),
101            Value::Text(s) => q.bind(s.as_str()),
102            Value::Bytes(b) => q.bind(b.as_slice()),
103            other => {
104                return Err(RelError::Unsupported(format!(
105                    "parameter {other:?} has no PostgreSQL binding in this driver"
106                )));
107            }
108        };
109    }
110    Ok(q)
111}
112
113fn decode_failed(e: sqlx::error::BoxDynError) -> RelError {
114    RelError::Backend(e.to_string())
115}
116
117fn malformed(column: &str, ty: &str, why: String) -> RelError {
118    RelError::Backend(format!("column {column:?} ({ty}): {why}"))
119}
120
121fn decode(row: &PgRow) -> Result<Row, RelError> {
122    let mut values = Vec::with_capacity(row.columns().len());
123    for (i, col) in row.columns().iter().enumerate() {
124        let raw = row.try_get_raw(i).map_err(classify)?;
125        if raw.is_null() {
126            values.push(Value::Null);
127            continue;
128        }
129        let name = col.type_info().name().to_string();
130        let v = match name.as_str() {
131            "BOOL" => Value::Bool(row.try_get::<bool, _>(i).map_err(classify)?),
132            "INT2" => Value::Int(i64::from(row.try_get::<i16, _>(i).map_err(classify)?)),
133            "INT4" => Value::Int(i64::from(row.try_get::<i32, _>(i).map_err(classify)?)),
134            "INT8" => Value::Int(row.try_get::<i64, _>(i).map_err(classify)?),
135            "FLOAT4" => Value::Float(decode::f32_shortest(
136                row.try_get::<f32, _>(i).map_err(classify)?,
137            )),
138            "FLOAT8" => Value::Float(row.try_get::<f64, _>(i).map_err(classify)?),
139            // sqlx names `bpchar` (char(n)) "CHAR"; the quoted "CHAR" is the one-byte internal type.
140            "TEXT" | "VARCHAR" | "NAME" | "BPCHAR" | "CHAR" => {
141                Value::Text(row.try_get::<String, _>(i).map_err(classify)?)
142            }
143            "BYTEA" => Value::Bytes(row.try_get::<Vec<u8>, _>(i).map_err(classify)?),
144            // Types with a canonical text form (`decode.rs`): carried as that text.
145            "UUID" => Value::Text(
146                decode::uuid_text(raw.as_bytes().map_err(decode_failed)?)
147                    .map_err(|m| malformed(col.name(), &name, m))?,
148            ),
149            "NUMERIC" => Value::Text(
150                decode::numeric_text(raw.as_bytes().map_err(decode_failed)?)
151                    .map_err(|m| malformed(col.name(), &name, m))?,
152            ),
153            "INET" | "CIDR" => Value::Text(
154                decode::inet_text(raw.as_bytes().map_err(decode_failed)?)
155                    .map_err(|m| malformed(col.name(), &name, m))?,
156            ),
157            "JSON" => Value::Text(
158                String::from_utf8(raw.as_bytes().map_err(decode_failed)?.to_vec())
159                    .map_err(|e| malformed(col.name(), &name, e.to_string()))?,
160            ),
161            "JSONB" => {
162                // Version byte 1, then the JSON text.
163                let b = raw.as_bytes().map_err(decode_failed)?;
164                match b.split_first() {
165                    Some((1, rest)) => Value::Text(
166                        String::from_utf8(rest.to_vec())
167                            .map_err(|e| malformed(col.name(), &name, e.to_string()))?,
168                    ),
169                    _ => return Err(malformed(col.name(), &name, "unknown jsonb version".into())),
170                }
171            }
172            "DATE" => Value::Text(decode::date_text(
173                row.try_get::<sqlx::types::time::Date, _>(i)
174                    .map_err(classify)?,
175            )),
176            "TIME" => Value::Text(decode::time_text(
177                row.try_get::<sqlx::types::time::Time, _>(i)
178                    .map_err(classify)?,
179            )),
180            "TIMESTAMP" => Value::Text(decode::naive_text(
181                row.try_get::<sqlx::types::time::PrimitiveDateTime, _>(i)
182                    .map_err(classify)?,
183            )),
184            "TIMESTAMPTZ" => Value::Text(decode::utc_text(
185                row.try_get::<sqlx::types::time::OffsetDateTime, _>(i)
186                    .map_err(classify)?,
187            )),
188            other => {
189                return Err(RelError::Unsupported(format!(
190                    "column {:?} has type {other}, which Value cannot carry; use PgBackend::pool",
191                    col.name()
192                )));
193            }
194        };
195        values.push(v);
196    }
197    Ok(Row::new(values))
198}
199
200/// A PostgreSQL database behind an sqlx pool.
201#[derive(Debug, Clone)]
202pub struct PgBackend {
203    pool: sqlx::PgPool,
204    version: String,
205}
206
207impl PgBackend {
208    /// Connect with default pool settings and read the server version.
209    ///
210    /// Prefer [`PgBackend::connect_secret`]: a `&str` URL is one `Debug` print from a log file.
211    pub async fn connect(url: &str) -> Result<Self, RelError> {
212        let pool = PgPoolOptions::new()
213            .max_connections(16)
214            .connect(url)
215            .await
216            .map_err(classify)?;
217        Self::from_pool(pool).await
218    }
219
220    /// Connect with a connection string held as a [`persistant_relational::ConnectionSecret`]:
221    /// resolved here, never printed, never part of an error.
222    pub async fn connect_secret(
223        secret: &persistant_relational::ConnectionSecret,
224    ) -> Result<Self, RelError> {
225        let url = secret.resolve()?;
226        Self::connect(url.expose()).await
227    }
228
229    /// Wrap an existing pool (a program that already owns one keeps it).
230    pub async fn from_pool(pool: sqlx::PgPool) -> Result<Self, RelError> {
231        let full: String = sqlx::query_scalar("SHOW server_version")
232            .fetch_one(&pool)
233            .await
234            .map_err(classify)?;
235        // "16.15 (Debian 16.15-1.pgdg13+2)" -> "16.15"
236        let version = full.split_whitespace().next().unwrap_or("").to_string();
237        Ok(PgBackend { pool, version })
238    }
239
240    /// The concrete pool: for `sqlx::query!` and anything this layer does not carry.
241    pub fn pool(&self) -> &sqlx::PgPool {
242        &self.pool
243    }
244}
245
246impl RelationalBackend for PgBackend {
247    type Tx<'a> = PgTx<'a>;
248
249    fn backend(&self) -> &str {
250        "postgres"
251    }
252
253    fn version(&self) -> &str {
254        &self.version
255    }
256
257    fn dialect(&self) -> Dialect {
258        Dialect::Postgres
259    }
260
261    async fn execute(&self, sql: &str, params: &[Value]) -> Result<u64, RelError> {
262        let q = bind(sqlx::query(sqlx::AssertSqlSafe(sql.to_string())), params)?;
263        q.execute(&self.pool)
264            .await
265            .map(|r| r.rows_affected())
266            .map_err(classify)
267    }
268
269    async fn query(&self, sql: &str, params: &[Value]) -> Result<Vec<Row>, RelError> {
270        let q = bind(sqlx::query(sqlx::AssertSqlSafe(sql.to_string())), params)?;
271        let rows = q.fetch_all(&self.pool).await.map_err(classify)?;
272        rows.iter().map(decode).collect()
273    }
274
275    async fn begin(&self, isolation: Isolation) -> Result<PgTx<'_>, RelError> {
276        let statement = match isolation {
277            Isolation::ReadCommitted => "BEGIN",
278            Isolation::RepeatableRead => "BEGIN ISOLATION LEVEL REPEATABLE READ",
279            Isolation::Serializable => "BEGIN ISOLATION LEVEL SERIALIZABLE",
280            other => return Err(RelError::Unsupported(format!("isolation level {other:?}"))),
281        };
282        Ok(PgTx {
283            tx: self.pool.begin_with(statement).await.map_err(classify)?,
284        })
285    }
286}
287
288/// A PostgreSQL transaction.
289#[derive(Debug)]
290pub struct PgTx<'a> {
291    tx: sqlx::Transaction<'a, Postgres>,
292}
293
294impl RelationalTx for PgTx<'_> {
295    async fn execute(&mut self, sql: &str, params: &[Value]) -> Result<u64, RelError> {
296        let q = bind(sqlx::query(sqlx::AssertSqlSafe(sql.to_string())), params)?;
297        q.execute(&mut *self.tx)
298            .await
299            .map(|r| r.rows_affected())
300            .map_err(classify)
301    }
302
303    async fn query(&mut self, sql: &str, params: &[Value]) -> Result<Vec<Row>, RelError> {
304        let q = bind(sqlx::query(sqlx::AssertSqlSafe(sql.to_string())), params)?;
305        let rows = q.fetch_all(&mut *self.tx).await.map_err(classify)?;
306        rows.iter().map(decode).collect()
307    }
308
309    async fn commit(self) -> Result<(), RelError> {
310        self.tx.commit().await.map_err(classify)
311    }
312
313    async fn rollback(self) -> Result<(), RelError> {
314        self.tx.rollback().await.map_err(classify)
315    }
316}