| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394 |
- //! The SQL dialect seam: the one place the SQLite/PostgreSQL divergence lives.
- //!
- //! Resolved once at construction from the pool's connection URL (no query), so
- //! the write paths read a plain enum instead of re-probing the backend. A third
- //! backend becomes a new variant here, not edits across every `impl`.
- use sqlx::{Any, Pool};
- /// Which SQL backend a [`SqlStore`](crate::SqlStore) is talking to.
- #[derive(Clone, Copy, Debug, PartialEq, Eq)]
- pub(crate) enum Dialect {
- /// PostgreSQL: supports `SELECT ... FOR UPDATE` row locking.
- Postgres,
- /// SQLite: no `FOR UPDATE`; it serializes writers itself.
- Sqlite,
- }
- impl Dialect {
- /// Resolve the dialect from the pool's connection URL scheme. Synchronous
- /// and issues no query. Anything that is not `sqlite` is treated as
- /// PostgreSQL, matching the prior runtime probe (which classified any
- /// non-SQLite backend as Postgres).
- pub(crate) fn from_pool(pool: &Pool<Any>) -> Self {
- match pool.connect_options().database_url.scheme() {
- "sqlite" => Self::Sqlite,
- _ => Self::Postgres,
- }
- }
- /// The placeholder for binding an amount, as it must appear in the SQL.
- ///
- /// PostgreSQL stores amounts as `NUMERIC(39,0)` and the value arrives as
- /// text (the `Any` driver has no numeric kind), so the cast is required
- /// rather than cosmetic: `text` to `numeric` is explicit-only in `pg_cast`,
- /// and a bare placeholder is rejected. SQLite binds the 16-byte encoding
- /// straight into a `BLOB`.
- pub(crate) fn money_bind(self, n: u32) -> String {
- match self {
- Self::Postgres => format!("CAST(${n} AS NUMERIC)"),
- Self::Sqlite => format!("${n}"),
- }
- }
- /// How an amount column must be named in a `SELECT` list.
- ///
- /// The cast is mandatory on PostgreSQL, and not only so the value can be
- /// parsed: `AnyRow` converts every column's type before any of them is
- /// read, and it has no mapping for `NUMERIC`, so one uncast amount fails
- /// the whole row even when nothing reads it.
- pub(crate) fn money_read(self, col: &str) -> String {
- match self {
- Self::Postgres => format!("CAST({col} AS TEXT) AS {col}"),
- Self::Sqlite => col.to_string(),
- }
- }
- /// An exact sum over an amount column, as a `SELECT` list entry.
- ///
- /// Both sides are exact and neither wraps: `NUMERIC` is arbitrary
- /// precision, and `kuatia_i128_sum` accumulates with `i128::checked_add`
- /// and raises rather than overflowing. `COALESCE` and the aggregate's
- /// zero-for-no-rows make an empty group a real row, so callers never have
- /// to special-case `NULL`.
- pub(crate) fn money_sum(self, col: &str) -> String {
- match self {
- Self::Postgres => format!("CAST(COALESCE(SUM({col}), 0) AS TEXT)"),
- Self::Sqlite => format!("kuatia_i128_sum({col})"),
- }
- }
- /// The `SELECT` list [`row_to_posting`](crate::row::row_to_posting) reads.
- /// Spelled out rather than `*` because the amount column needs
- /// [`money_read`](Self::money_read); the six columns are shared by
- /// `postings` and `live_postings`.
- /// The explicit column list for a posting read, including the asset
- /// definition joined from the registry: a posting carries an amount, and an
- /// amount carries its asset. Pair it with [`joined`](crate::posting::joined).
- pub(crate) fn posting_columns(self) -> String {
- format!(
- "transfer_id, idx, owner, subaccount, asset, assets.data AS asset_def, {}",
- self.money_read("value")
- )
- }
- /// Row-locking clause appended to a `SELECT` that takes a pessimistic lock:
- /// ` FOR UPDATE` on Postgres, empty on SQLite (which has no such clause and
- /// serializes writers itself).
- pub(crate) fn lock_clause(self) -> &'static str {
- match self {
- Self::Postgres => " FOR UPDATE",
- Self::Sqlite => "",
- }
- }
- }
|