//! Shared row mappers and codecs: how the store turns domain values into the //! columns it stores, and rows back into domain types. //! //! Identifier and payload columns are text, so a row stays legible in any SQL //! client: content-addressed ids as hex `TEXT`, JSON payloads as their `TEXT` //! serialization. Amounts are the exception. They are stored natively so the //! database can sum and order them (ADR-0024), which means their wire form //! depends on the backend, and every amount crosses this module through //! [`bind_money`] and [`row_to_cent`] rather than through `Display`/`FromStr`. use std::str::FromStr; use sqlx::Row; use sqlx::any::{AnyArguments, AnyRow}; use sqlx::query::Query; use kuatia_storage::error::StoreError; use kuatia_types::*; use crate::dialect::Dialect; /// Bind one amount, in whichever form the backend's column takes: the decimal /// string for a PostgreSQL `NUMERIC` (the SQL must wrap the placeholder in /// [`Dialect::money_bind`]), the 16-byte order-preserving encoding for a SQLite /// `BLOB`. `.bind` is typed, so the two arms cannot be collapsed. pub(crate) fn bind_money<'q>( q: Query<'q, sqlx::Any, AnyArguments<'q>>, dialect: Dialect, value: Cent, ) -> Query<'q, sqlx::Any, AnyArguments<'q>> { match dialect { Dialect::Postgres => q.bind(value.to_string()), Dialect::Sqlite => q.bind(value.to_storage_bytes().to_vec()), } } /// Read one amount back. The column must have been selected through /// [`Dialect::money_read`] or [`Dialect::money_sum`]. pub(crate) fn row_to_cent( dialect: Dialect, row: &AnyRow, column: &str, ) -> Result { match dialect { Dialect::Postgres => { let text: String = row .try_get(column) .map_err(|e| StoreError::Internal(e.to_string()))?; Cent::from_str(&text).map_err(|e| StoreError::Internal(e.to_string())) } Dialect::Sqlite => { let bytes: Vec = row .try_get(column) .map_err(|e| StoreError::Internal(e.to_string()))?; let bytes: [u8; 16] = bytes.as_slice().try_into().map_err(|_| { StoreError::Internal(format!( "expected a 16-byte amount in {column}, got {} bytes", bytes.len() )) })?; Cent::from_storage_bytes(&bytes).map_err(|e| StoreError::Internal(e.to_string())) } } } /// Serialize a value to a JSON string. Payload columns store JSON as `TEXT` so /// the database is directly readable for auditing; the ledger never queries into /// the JSON, so no binary or indexed representation is needed. pub(crate) fn serialize_json(val: &T) -> Result { serde_json::to_string(val).map_err(|e| StoreError::Internal(format!("json serialization: {e}"))) } pub(crate) fn deserialize_json(s: &str) -> Result { serde_json::from_str(s).map_err(|e| StoreError::Internal(format!("bad json: {e}"))) } /// Lower-case hex encoding. Binary identifiers (content-addressed hashes) and /// opaque saga bytes are stored as hex `TEXT` so a row is legible in any SQL /// client and matches the hex form used in logs and `Debug` output. pub(crate) fn to_hex(bytes: &[u8]) -> String { const HEX: &[u8; 16] = b"0123456789abcdef"; let mut s = String::with_capacity(bytes.len() * 2); for &b in bytes { s.push(HEX[(b >> 4) as usize] as char); s.push(HEX[(b & 0x0f) as usize] as char); } s } pub(crate) fn from_hex(s: &str) -> Result, StoreError> { if s.len() % 2 != 0 { return Err(StoreError::Internal(format!("odd-length hex: {s:?}"))); } (0..s.len()) .step_by(2) .map(|i| { u8::from_str_radix(&s[i..i + 2], 16) .map_err(|e| StoreError::Internal(format!("bad hex: {e}"))) }) .collect() } pub(crate) fn envelope_id_to_hex(id: &EnvelopeId) -> String { to_hex(&id.0) } pub(crate) fn envelope_id_from_hex(s: &str) -> Result { let bytes = from_hex(s)?; let arr: [u8; 32] = bytes.as_slice().try_into().map_err(|_| { StoreError::Internal(format!("expected 32-byte id, got {} bytes", bytes.len())) })?; Ok(EnvelopeId(arr)) } pub(crate) fn row_to_account(row: &AnyRow) -> Result { let id: i64 = row .try_get("id") .map_err(|e| StoreError::Internal(e.to_string()))?; let subaccount: i64 = row .try_get("subaccount") .map_err(|e| StoreError::Internal(e.to_string()))?; let version: i64 = row .try_get("version") .map_err(|e| StoreError::Internal(e.to_string()))?; let flags_bits: i32 = row .try_get("flags") .map_err(|e| StoreError::Internal(e.to_string()))?; let book: i64 = row .try_get("book") .map_err(|e| StoreError::Internal(e.to_string()))?; let metadata_json: String = row .try_get("metadata") .map_err(|e| StoreError::Internal(e.to_string()))?; Ok(Account { id: AccountId::with_sub(id, subaccount), version: version as u64, flags: AccountFlags::from_bits_truncate(flags_bits as u32), book: BookId::new(book), metadata: deserialize_json(&metadata_json)?, }) } pub(crate) fn row_to_posting(dialect: Dialect, row: &AnyRow) -> Result { let transfer_id: String = row .try_get("transfer_id") .map_err(|e| StoreError::Internal(e.to_string()))?; let idx: i16 = row .try_get("idx") .map_err(|e| StoreError::Internal(e.to_string()))?; let owner: i64 = row .try_get("owner") .map_err(|e| StoreError::Internal(e.to_string()))?; let subaccount: i64 = row .try_get("subaccount") .map_err(|e| StoreError::Internal(e.to_string()))?; let asset: i32 = row .try_get("asset") .map_err(|e| StoreError::Internal(e.to_string()))?; let value = row_to_cent(dialect, row, "value")?; Ok(Posting { id: PostingId { transfer: envelope_id_from_hex(&transfer_id)?, index: idx as u16, }, owner: AccountId::with_sub(owner, subaccount), asset: AssetId::new(asset as u32), value, }) }