use super::ExecError;
#[cfg(feature = "postgres")]
pub type PgReturningRow = sqlx::postgres::PgRow;
#[cfg(not(feature = "postgres"))]
pub enum PgReturningRow {}
#[cfg(feature = "mysql")]
pub type MyReturningRow = sqlx::mysql::MySqlRow;
#[cfg(not(feature = "mysql"))]
pub enum MyReturningRow {}
#[cfg(feature = "sqlite")]
pub type SqliteReturningRow = sqlx::sqlite::SqliteRow;
#[cfg(not(feature = "sqlite"))]
pub enum SqliteReturningRow {}
#[cfg(feature = "postgres")]
pub fn try_get_returning<'r, T>(row: &'r PgReturningRow, name: &str) -> Result<T, sqlx::Error>
where
T: sqlx::Decode<'r, sqlx::Postgres> + sqlx::Type<sqlx::Postgres>,
{
use sqlx::Row as _;
row.try_get(name)
}
#[cfg(not(feature = "postgres"))]
#[allow(clippy::missing_errors_doc)]
pub fn try_get_returning<T>(row: &PgReturningRow, _name: &str) -> Result<T, sqlx::Error> {
match *row {}
}
#[cfg(feature = "mysql")]
pub fn try_get_returning_my<'r, T>(row: &'r MyReturningRow, name: &str) -> Result<T, sqlx::Error>
where
T: sqlx::Decode<'r, sqlx::MySql> + sqlx::Type<sqlx::MySql>,
{
use sqlx::Row as _;
row.try_get(name)
}
#[cfg(not(feature = "mysql"))]
#[allow(clippy::missing_errors_doc)]
pub fn try_get_returning_my<T>(row: &MyReturningRow, _name: &str) -> Result<T, sqlx::Error> {
match *row {}
}
#[cfg(feature = "sqlite")]
pub fn try_get_returning_sqlite<'r, T>(
row: &'r SqliteReturningRow,
name: &str,
) -> Result<T, sqlx::Error>
where
T: sqlx::Decode<'r, sqlx::Sqlite> + sqlx::Type<sqlx::Sqlite>,
{
use sqlx::Row as _;
row.try_get(name)
}
#[cfg(not(feature = "sqlite"))]
#[allow(clippy::missing_errors_doc)]
pub fn try_get_returning_sqlite<T>(
row: &SqliteReturningRow,
_name: &str,
) -> Result<T, sqlx::Error> {
match *row {}
}
pub trait MysqlAutoIdSet: Sized + sealed::Sealed {
fn rustango_from_mysql_auto_id(id: i64) -> Result<Self, ExecError>;
}
mod sealed {
pub trait Sealed {}
impl Sealed for i64 {}
impl Sealed for i32 {}
impl Sealed for uuid::Uuid {}
impl Sealed for String {}
impl Sealed for chrono::DateTime<chrono::Utc> {}
}
impl MysqlAutoIdSet for i64 {
fn rustango_from_mysql_auto_id(id: i64) -> Result<Self, ExecError> {
Ok(id)
}
}
impl MysqlAutoIdSet for i32 {
fn rustango_from_mysql_auto_id(id: i64) -> Result<Self, ExecError> {
i32::try_from(id).map_err(|_| {
ExecError::Sql(super::SqlError::OperatorNotSupportedInDialect {
op: "i64 → i32 narrowing for AUTO_INCREMENT",
dialect: "mysql",
})
})
}
}
impl MysqlAutoIdSet for uuid::Uuid {
fn rustango_from_mysql_auto_id(_id: i64) -> Result<Self, ExecError> {
Err(ExecError::Sql(
super::SqlError::OperatorNotSupportedInDialect {
op: "Auto<Uuid> assigned by AUTO_INCREMENT",
dialect: "mysql",
},
))
}
}
impl MysqlAutoIdSet for String {
fn rustango_from_mysql_auto_id(_id: i64) -> Result<Self, ExecError> {
Err(ExecError::Sql(
super::SqlError::OperatorNotSupportedInDialect {
op: "Auto<String> assigned by AUTO_INCREMENT",
dialect: "mysql",
},
))
}
}
impl MysqlAutoIdSet for chrono::DateTime<chrono::Utc> {
fn rustango_from_mysql_auto_id(_id: i64) -> Result<Self, ExecError> {
Err(ExecError::Sql(
super::SqlError::OperatorNotSupportedInDialect {
op: "Auto<DateTime> assigned by AUTO_INCREMENT",
dialect: "mysql",
},
))
}
}
#[doc(hidden)]
pub trait AssignAutoPkPool {
fn __rustango_assign_from_pg_row(&mut self, row: &PgReturningRow) -> Result<(), ExecError>;
fn __rustango_assign_from_mysql_id(&mut self, id: i64) -> Result<(), ExecError>;
fn __rustango_assign_from_sqlite_row(
&mut self,
row: &SqliteReturningRow,
) -> Result<(), ExecError>;
}
pub fn apply_auto_pk_pool<M: AssignAutoPkPool + ?Sized>(
result: super::InsertReturningPool,
model: &mut M,
) -> Result<(), ExecError> {
let _ = model;
match result {
#[cfg(feature = "postgres")]
super::InsertReturningPool::PgRow(row) => model.__rustango_assign_from_pg_row(&row),
#[cfg(feature = "mysql")]
super::InsertReturningPool::MySqlAutoId(id) => model.__rustango_assign_from_mysql_id(id),
#[cfg(feature = "sqlite")]
super::InsertReturningPool::SqliteRow(row) => model.__rustango_assign_from_sqlite_row(&row),
}
}