use std::borrow::Cow;
use std::sync::{Arc, RwLock};
use snodra_messaging::error::MessagingError;
use snodra_messaging::saga::Clock;
use snodra_messaging::store::{StoreError, Tx};
use sqlx::pool::PoolConnection;
use sqlx::{AssertSqlSafe, Row as _, SqlSafeStr};
use sqlx_core::transaction::TransactionManager;
#[cfg(feature = "mysql")]
use sqlx::mysql::{MySql, MySqlConnection, MySqlDatabaseError, MySqlPool, MySqlRow};
#[cfg(feature = "postgres")]
use sqlx::postgres::{PgConnection, PgDatabaseError, PgPool, PgRow, Postgres};
use crate::limits::refuse_nul;
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) enum Dialect {
#[cfg(feature = "postgres")]
Postgres,
#[cfg(feature = "mysql")]
MySql,
}
impl Dialect {
pub(crate) fn name(self) -> &'static str {
match self {
#[cfg(feature = "postgres")]
Dialect::Postgres => "postgresql",
#[cfg(feature = "mysql")]
Dialect::MySql => "mysql",
}
}
}
#[derive(Clone)]
pub(crate) enum Pool {
#[cfg(feature = "postgres")]
Pg(PgPool),
#[cfg(feature = "mysql")]
My(MySqlPool),
}
pub(crate) struct Held<DB: sqlx::Database> {
conn: Option<PoolConnection<DB>>,
session_changed: bool,
}
impl<DB: sqlx::Database> Drop for Held<DB> {
fn drop(&mut self) {
if let Some(mut conn) = self.conn.take() {
conn.close_on_drop();
}
}
}
impl<DB: sqlx::Database> Held<DB> {
pub(crate) fn connection(&mut self) -> Option<&mut DB::Connection> {
self.conn.as_deref_mut()
}
pub(crate) fn is_open(&self) -> bool {
self.conn.is_some()
}
pub(crate) async fn begin(
pool: &sqlx::Pool<DB>,
statement: &'static str,
) -> Result<Held<DB>, StoreError> {
let mut held = Held {
conn: Some(pool.acquire().await.map_err(store_error)?),
session_changed: false,
};
if let Some(conn) = held.conn.as_mut() {
DB::TransactionManager::begin(&mut **conn, Some(statement.into_sql_str()))
.await
.map_err(|e| match e {
sqlx::Error::BeginFailed => storage(format!(
"`{statement}` did not leave the session in a transaction (a pooler in \
statement mode?): the message store needs a connection that keeps one"
)),
other => store_error(other),
})?;
}
Ok(held)
}
pub(crate) async fn end(&mut self, commit: bool) -> Result<(), StoreError> {
let Some(conn) = self.conn.as_mut() else {
return Ok(());
};
let depth = DB::TransactionManager::get_transaction_depth(conn);
if depth != 1 {
if let Some(mut conn) = self.conn.take() {
conn.close_on_drop();
}
return if commit {
Err(storage(if depth == 0 {
"the store's transaction was ended by someone else through sqlx's \
transaction manager (sqlx's depth is 0): it committed or rolled back then, \
not at the store's commit, and the connection is closed (never run COMMIT or \
ROLLBACK on the store's connection: a raw one is not even seen)"
.to_string()
} else {
format!(
"a nested transaction was left open on the store's connection ({} \
levels): the store's transaction is rolled back, nothing of it is committed",
depth - 1
)
}))
} else {
Ok(())
};
}
let ended = if commit {
DB::TransactionManager::commit(&mut **conn).await
} else {
DB::TransactionManager::rollback(&mut **conn).await
};
if let Some(mut conn) = self.conn.take()
&& (ended.is_err() || self.session_changed)
{
conn.close_on_drop();
}
ended.map_err(store_error)
}
}
#[cfg(feature = "postgres")]
impl Held<Postgres> {
pub(crate) async fn commit_checked(&mut self) -> Result<(), StoreError> {
let Some(conn) = self.conn.as_mut() else {
return Ok(());
};
if <Postgres as sqlx::Database>::TransactionManager::get_transaction_depth(conn) == 1
&& let Err(e) = sqlx::raw_sql("SELECT 1").execute(&mut **conn).await
{
if let Some(mut conn) = self.conn.take() {
conn.close_on_drop();
}
return Err(match &e {
sqlx::Error::Database(db) if db.code().as_deref() == Some("25P02") => storage(
"the store's transaction was aborted by a statement that failed on its \
connection (an error ignored by a handler?): it is rolled back, nothing of it \
is committed"
.to_string(),
),
_ => store_error(e),
});
}
self.end(true).await
}
}
macro_rules! held {
($db:ty, $variant:ident) => {
impl Held<$db> {
pub(crate) fn conn(&mut self) -> Option<Conn<'_>> {
let Held {
conn,
session_changed,
} = self;
conn.as_mut().map(|c| Conn {
c: ConnRef::$variant(&mut **c),
session_changed,
})
}
}
};
}
#[cfg(feature = "postgres")]
held!(Postgres, Pg);
#[cfg(feature = "mysql")]
held!(MySql, My);
enum ConnRef<'a> {
#[cfg(feature = "postgres")]
Pg(&'a mut PgConnection),
#[cfg(feature = "mysql")]
My(&'a mut MySqlConnection),
}
pub(crate) struct Conn<'a> {
c: ConnRef<'a>,
session_changed: &'a mut bool,
}
#[derive(Clone, Debug)]
pub(crate) enum Arg {
Text(Option<String>),
Int(Option<i64>),
Int32(Option<i32>),
Bool(Option<bool>),
Time(Option<i64>),
Bytes(Option<Vec<u8>>),
}
impl From<&str> for Arg {
fn from(v: &str) -> Arg {
Arg::Text(Some(v.to_string()))
}
}
impl From<String> for Arg {
fn from(v: String) -> Arg {
Arg::Text(Some(v))
}
}
impl From<&String> for Arg {
fn from(v: &String) -> Arg {
Arg::Text(Some(v.clone()))
}
}
impl From<Option<&str>> for Arg {
fn from(v: Option<&str>) -> Arg {
Arg::Text(v.map(str::to_string))
}
}
impl From<Option<String>> for Arg {
fn from(v: Option<String>) -> Arg {
Arg::Text(v)
}
}
impl From<&Option<String>> for Arg {
fn from(v: &Option<String>) -> Arg {
Arg::Text(v.clone())
}
}
impl From<i64> for Arg {
fn from(v: i64) -> Arg {
Arg::Int(Some(v))
}
}
impl From<Option<i64>> for Arg {
fn from(v: Option<i64>) -> Arg {
Arg::Int(v)
}
}
impl From<i32> for Arg {
fn from(v: i32) -> Arg {
Arg::Int32(Some(v))
}
}
impl From<Option<i32>> for Arg {
fn from(v: Option<i32>) -> Arg {
Arg::Int32(v)
}
}
impl From<bool> for Arg {
fn from(v: bool) -> Arg {
Arg::Bool(Some(v))
}
}
impl From<Option<bool>> for Arg {
fn from(v: Option<bool>) -> Arg {
Arg::Bool(v)
}
}
pub(crate) struct Stmt {
sql: Cow<'static, str>,
args: Vec<(&'static str, Arg)>,
}
impl Stmt {
pub(crate) fn new(sql: &'static str) -> Stmt {
Stmt {
sql: Cow::Borrowed(sql),
args: Vec::new(),
}
}
pub(crate) fn built(sql: String) -> Stmt {
Stmt {
sql: Cow::Owned(sql),
args: Vec::new(),
}
}
pub(crate) fn arg(mut self, name: &'static str, value: impl Into<Arg>) -> Stmt {
self.args.push((name, value.into()));
self
}
pub(crate) fn time(mut self, name: &'static str, ms: i64) -> Stmt {
self.args.push((name, Arg::Time(Some(ms))));
self
}
pub(crate) fn opt_time(mut self, name: &'static str, ms: Option<i64>) -> Stmt {
self.args.push((name, Arg::Time(ms)));
self
}
pub(crate) fn bytes(mut self, name: &'static str, bytes: Option<Vec<u8>>) -> Stmt {
self.args.push((name, Arg::Bytes(bytes)));
self
}
pub(crate) fn args<V: Into<Arg>>(
mut self,
name: &'static str,
values: impl IntoIterator<Item = V>,
) -> Stmt {
self.args
.extend(values.into_iter().map(|v| (name, v.into())));
self
}
fn checked(self) -> Result<Stmt, StoreError> {
for (name, arg) in &self.args {
match arg {
Arg::Text(Some(text)) => refuse_nul(&format!("the parameter {name}"), text)?,
Arg::Time(Some(ms)) if chrono::DateTime::from_timestamp_millis(*ms).is_none() => {
return Err(StoreError::Failed(MessagingError::InvalidMessage(format!(
"The message store cannot keep the instant {ms} ms from the epoch (the parameter {name}): out of range"
))));
}
_ => {}
}
}
Ok(self)
}
fn render(&self, dialect: Dialect) -> String {
match dialect {
#[cfg(feature = "postgres")]
Dialect::Postgres => {
let sql = self
.sql
.replace("{json}", "jsonb")
.replace("{text}", "TEXT");
let mut out = String::with_capacity(sql.len() + 16);
let mut n = 0;
for c in sql.chars() {
if c == '?' {
n += 1;
out.push('$');
out.push_str(&n.to_string());
} else {
out.push(c);
}
}
out
}
#[cfg(feature = "mysql")]
Dialect::MySql => self.sql.replace("{json}", "JSON").replace("{text}", "CHAR"),
}
}
}
pub(crate) fn placeholders(n: usize) -> String {
vec!["?"; n.max(1)].join(", ")
}
#[cfg(feature = "postgres")]
fn pg_time(ms: i64) -> chrono::DateTime<chrono::Utc> {
chrono::DateTime::from_timestamp_millis(ms).unwrap_or_default()
}
#[cfg(feature = "mysql")]
fn my_time(ms: i64) -> chrono::NaiveDateTime {
chrono::DateTime::from_timestamp_millis(ms)
.unwrap_or_default()
.naive_utc()
}
#[cfg(feature = "postgres")]
fn pg_query(
sql: String,
args: Vec<(&'static str, Arg)>,
) -> sqlx::query::Query<'static, Postgres, sqlx::postgres::PgArguments> {
let mut q = sqlx::query(AssertSqlSafe(sql));
for (_, arg) in args {
q = match arg {
Arg::Text(v) => q.bind(v),
Arg::Int(v) => q.bind(v),
Arg::Int32(v) => q.bind(v),
Arg::Bool(v) => q.bind(v),
Arg::Time(v) => q.bind(v.map(pg_time)),
Arg::Bytes(v) => q.bind(v),
};
}
q
}
#[cfg(feature = "mysql")]
fn my_query(
sql: String,
args: Vec<(&'static str, Arg)>,
) -> sqlx::query::Query<'static, MySql, sqlx::mysql::MySqlArguments> {
let mut q = sqlx::query(AssertSqlSafe(sql));
for (_, arg) in args {
q = match arg {
Arg::Text(v) => q.bind(v),
Arg::Int(v) => q.bind(v),
Arg::Int32(v) => q.bind(v),
Arg::Bool(v) => q.bind(v),
Arg::Time(v) => q.bind(v.map(my_time)),
Arg::Bytes(v) => q.bind(v),
};
}
q
}
impl Conn<'_> {
pub(crate) fn dialect(&self) -> Dialect {
match self.c {
#[cfg(feature = "postgres")]
ConnRef::Pg(_) => Dialect::Postgres,
#[cfg(feature = "mysql")]
ConnRef::My(_) => Dialect::MySql,
}
}
#[cfg_attr(not(feature = "mysql"), allow(dead_code))] pub(crate) fn session_changed(&mut self, changed: bool) {
*self.session_changed = changed;
}
pub(crate) async fn try_execute(
&mut self,
stmt: Stmt,
) -> Result<Result<u64, sqlx::Error>, StoreError> {
let stmt = stmt.checked()?;
let sql = stmt.render(self.dialect());
Ok(match &mut self.c {
#[cfg(feature = "postgres")]
ConnRef::Pg(c) => pg_query(sql, stmt.args)
.execute(&mut **c)
.await
.map(|r| r.rows_affected()),
#[cfg(feature = "mysql")]
ConnRef::My(c) => my_query(sql, stmt.args)
.execute(&mut **c)
.await
.map(|r| r.rows_affected()),
})
}
pub(crate) async fn execute(&mut self, stmt: Stmt) -> Result<u64, StoreError> {
self.try_execute(stmt).await?.map_err(store_error)
}
pub(crate) async fn fetch_all(&mut self, stmt: Stmt) -> Result<Vec<Row>, StoreError> {
let stmt = stmt.checked()?;
let sql = stmt.render(self.dialect());
match &mut self.c {
#[cfg(feature = "postgres")]
ConnRef::Pg(c) => Ok(pg_query(sql, stmt.args)
.fetch_all(&mut **c)
.await
.map_err(store_error)?
.into_iter()
.map(Row::Pg)
.collect()),
#[cfg(feature = "mysql")]
ConnRef::My(c) => Ok(my_query(sql, stmt.args)
.fetch_all(&mut **c)
.await
.map_err(store_error)?
.into_iter()
.map(Row::My)
.collect()),
}
}
pub(crate) async fn fetch_optional(&mut self, stmt: Stmt) -> Result<Option<Row>, StoreError> {
Ok(self.fetch_all(stmt).await?.into_iter().next())
}
pub(crate) async fn fetch_one(&mut self, stmt: Stmt) -> Result<Row, StoreError> {
self.fetch_optional(stmt)
.await?
.ok_or_else(|| storage("a statement returned no row".into()))
}
}
pub(crate) enum Row {
#[cfg(feature = "postgres")]
Pg(PgRow),
#[cfg(feature = "mysql")]
My(MySqlRow),
}
macro_rules! get {
($row:expr, $ty:ty, $col:expr) => {
match $row {
#[cfg(feature = "postgres")]
Row::Pg(r) => r.try_get::<$ty, _>($col),
#[cfg(feature = "mysql")]
Row::My(r) => r.try_get::<$ty, _>($col),
}
.map_err(|e| decode_error($col, e))
};
}
fn decode_error(column: &str, e: sqlx::Error) -> StoreError {
storage(format!("cannot read column {column}: {e}"))
}
impl Row {
pub(crate) fn text(&self, col: &str) -> Result<String, StoreError> {
get!(self, String, col)
}
pub(crate) fn opt_text(&self, col: &str) -> Result<Option<String>, StoreError> {
get!(self, Option<String>, col)
}
pub(crate) fn int(&self, col: &str) -> Result<i64, StoreError> {
self.opt_int(col)?
.ok_or_else(|| storage(format!("column {col} is null")))
}
pub(crate) fn opt_int(&self, col: &str) -> Result<Option<i64>, StoreError> {
match get!(self, Option<i64>, col) {
Ok(v) => Ok(v),
Err(_) => get!(self, Option<i32>, col).map(|v| v.map(i64::from)),
}
}
pub(crate) fn int32(&self, col: &str) -> Result<i32, StoreError> {
let n = self.int(col)?;
i32::try_from(n).map_err(|_| storage(format!("column {col} holds {n}, beyond i32")))
}
pub(crate) fn opt_int32(&self, col: &str) -> Result<Option<i32>, StoreError> {
self.opt_int(col)?
.map(|n| {
i32::try_from(n).map_err(|_| storage(format!("column {col} holds {n}, beyond i32")))
})
.transpose()
}
pub(crate) fn bool(&self, col: &str) -> Result<bool, StoreError> {
self.opt_bool(col)?
.ok_or_else(|| storage(format!("column {col} is null")))
}
pub(crate) fn opt_bool(&self, col: &str) -> Result<Option<bool>, StoreError> {
get!(self, Option<bool>, col)
}
pub(crate) fn opt_bytes(&self, col: &str) -> Result<Option<Vec<u8>>, StoreError> {
get!(self, Option<Vec<u8>>, col)
}
pub(crate) fn ms(&self, col: &str) -> Result<i64, StoreError> {
self.opt_ms(col)?
.ok_or_else(|| storage(format!("column {col} is null")))
}
pub(crate) fn opt_ms(&self, col: &str) -> Result<Option<i64>, StoreError> {
match self {
#[cfg(feature = "postgres")]
Row::Pg(r) => r
.try_get::<Option<chrono::DateTime<chrono::Utc>>, _>(col)
.map(|t| t.map(|t| t.timestamp_millis())),
#[cfg(feature = "mysql")]
Row::My(r) => r
.try_get::<Option<chrono::NaiveDateTime>, _>(col)
.map(|t| t.map(|t| t.and_utc().timestamp_millis())),
}
.map_err(|e| decode_error(col, e))
}
}
#[cfg(feature = "postgres")]
pub(crate) const BEGIN: &str = "BEGIN";
#[cfg(feature = "mysql")]
pub(crate) const START: &str = "START TRANSACTION";
pub(crate) struct Db {
pub(crate) pool: Pool,
clock: Arc<RwLock<Clock>>,
}
pub(crate) enum OwnTx {
#[cfg(feature = "postgres")]
Pg(Held<Postgres>),
#[cfg(feature = "mysql")]
My(Held<MySql>),
}
impl OwnTx {
pub(crate) fn conn(&mut self) -> Conn<'_> {
match self {
#[cfg(feature = "postgres")]
OwnTx::Pg(t) => t.conn(),
#[cfg(feature = "mysql")]
OwnTx::My(t) => t.conn(),
}
.expect("the store's own transaction is open until it is finished")
}
pub(crate) async fn finish<T>(
mut self,
result: Result<T, StoreError>,
) -> Result<T, StoreError> {
let commit = result.is_ok();
let ended = match &mut self {
#[cfg(feature = "postgres")]
OwnTx::Pg(t) => t.end(commit).await,
#[cfg(feature = "mysql")]
OwnTx::My(t) => t.end(commit).await,
};
match result {
Ok(value) => ended.map(|()| value),
Err(e) => Err(e),
}
}
}
pub(crate) enum Scope<'t> {
Joined(Conn<'t>),
Own(OwnTx),
}
impl Scope<'_> {
pub(crate) fn conn(&mut self) -> Conn<'_> {
match self {
Scope::Joined(c) => c.reborrow(),
Scope::Own(t) => t.conn(),
}
}
pub(crate) async fn finish<T>(self, result: Result<T, StoreError>) -> Result<T, StoreError> {
match self {
Scope::Joined(_) => result,
Scope::Own(t) => t.finish(result).await,
}
}
}
impl Conn<'_> {
pub(crate) fn reborrow(&mut self) -> Conn<'_> {
Conn {
c: match &mut self.c {
#[cfg(feature = "postgres")]
ConnRef::Pg(c) => ConnRef::Pg(c),
#[cfg(feature = "mysql")]
ConnRef::My(c) => ConnRef::My(c),
},
session_changed: &mut *self.session_changed,
}
}
}
impl Db {
pub(crate) fn new(pool: Pool, clock: Arc<RwLock<Clock>>) -> Db {
Db { pool, clock }
}
pub(crate) fn dialect(&self) -> Dialect {
match self.pool {
#[cfg(feature = "postgres")]
Pool::Pg(_) => Dialect::Postgres,
#[cfg(feature = "mysql")]
Pool::My(_) => Dialect::MySql,
}
}
pub(crate) fn now(&self) -> i64 {
self.clock
.read()
.unwrap_or_else(|poisoned| poisoned.into_inner())
.now_ms()
}
pub(crate) async fn begin(&self) -> Result<OwnTx, StoreError> {
Ok(match &self.pool {
#[cfg(feature = "postgres")]
Pool::Pg(p) => OwnTx::Pg(Held::<Postgres>::begin(p, BEGIN).await?),
#[cfg(feature = "mysql")]
Pool::My(p) => OwnTx::My(Held::<MySql>::begin(p, START).await?),
})
}
pub(crate) async fn own(&self) -> Result<OwnTx, StoreError> {
Ok(match &self.pool {
#[cfg(feature = "postgres")]
Pool::Pg(p) => {
OwnTx::Pg(Held::<Postgres>::begin(p, "BEGIN ISOLATION LEVEL READ COMMITTED").await?)
}
#[cfg(feature = "mysql")]
Pool::My(p) => OwnTx::My(
Held::<MySql>::begin(
p,
"SET TRANSACTION ISOLATION LEVEL READ COMMITTED; START TRANSACTION",
)
.await?,
),
})
}
pub(crate) async fn scope<'t>(&self, tx: &'t mut Tx) -> Result<Scope<'t>, StoreError> {
if tx.is_none() {
return Ok(Scope::Own(self.begin().await?));
}
let dialect = self.dialect();
let conn = match dialect {
#[cfg(feature = "postgres")]
Dialect::Postgres => tx.get_mut::<Held<Postgres>>().map(|held| held.conn()),
#[cfg(feature = "mysql")]
Dialect::MySql => tx.get_mut::<Held<MySql>>().map(|held| held.conn()),
};
match conn {
Some(Some(conn)) => Ok(Scope::Joined(conn)),
Some(None) => Ok(Scope::Own(self.begin().await?)),
None => Err(StoreError::Failed(MessagingError::Storage(format!(
"the SQL message store ({}) was given a transaction that is not one of its own: \
the writes would not commit or roll back with it",
dialect.name()
)))),
}
}
}
pub(crate) fn storage(message: String) -> StoreError {
StoreError::Failed(MessagingError::Storage(message))
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) enum ErrorKind {
Duplicate,
Transient,
LockNotAvailable,
Other,
}
pub(crate) fn error_kind(e: &sqlx::Error) -> ErrorKind {
let sqlx::Error::Database(db) = e else {
return ErrorKind::Other;
};
#[cfg(feature = "postgres")]
if let Some(pg) = db.try_downcast_ref::<PgDatabaseError>() {
return match pg.code() {
"23505" => ErrorKind::Duplicate,
"40001" | "40P01" => ErrorKind::Transient,
"55P03" => ErrorKind::LockNotAvailable,
_ => ErrorKind::Other,
};
}
#[cfg(feature = "mysql")]
if let Some(my) = db.try_downcast_ref::<MySqlDatabaseError>() {
return match my.number() {
1062 | 1586 => ErrorKind::Duplicate,
1213 => ErrorKind::Transient,
1205 | 3572 => ErrorKind::LockNotAvailable,
_ => ErrorKind::Other,
};
}
ErrorKind::Other
}
pub(crate) fn store_error(e: sqlx::Error) -> StoreError {
match error_kind(&e) {
ErrorKind::Other => storage(e.to_string()),
_ => StoreError::Conflict(e.to_string()),
}
}