pub use axumstart_db_macros::{SqlxInsert, SqlxUpdate, repository};
#[cfg(not(any(feature = "postgres", feature = "mysql", feature = "sqlite")))]
compile_error!(
"axumstart_db requires exactly one of the `postgres`, `mysql`, or `sqlite` features to be enabled"
);
#[cfg(any(
all(feature = "postgres", feature = "mysql"),
all(feature = "postgres", feature = "sqlite"),
all(feature = "mysql", feature = "sqlite"),
))]
compile_error!(
"axumstart_db: enable exactly one of `postgres`, `mysql`, `sqlite` — not more than one"
);
#[cfg(feature = "postgres")]
pub type Db = sqlx::Postgres;
#[cfg(feature = "mysql")]
pub type Db = sqlx::MySql;
#[cfg(feature = "sqlite")]
pub type Db = sqlx::Sqlite;
pub type DbPool = sqlx::Pool<Db>;
pub type DbConnection = <Db as sqlx::Database>::Connection;
#[cfg(feature = "postgres")]
pub const WEEK_START_SQL: &str = "date_trunc('week', CURRENT_TIMESTAMP)";
#[cfg(feature = "mysql")]
pub const WEEK_START_SQL: &str = "DATE_SUB(CURRENT_DATE, INTERVAL WEEKDAY(CURRENT_DATE) DAY)";
#[cfg(feature = "sqlite")]
pub const WEEK_START_SQL: &str = "date(CURRENT_TIMESTAMP, 'weekday 1', '-7 days')";
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Page {
pub limit: i64,
pub offset: i64,
}
pub trait Repository: Send + Sync {
fn pool(&self) -> &DbPool;
}
impl Page {
pub fn new(limit: i64, offset: i64) -> Self {
Self { limit, offset }
}
pub fn number(page: i64, size: i64) -> Self {
Self { limit: size, offset: page * size }
}
}
pub fn push_in_list<'t, T>(qb: &mut sqlx::QueryBuilder<Db>, items: impl IntoIterator<Item = T>)
where
T: sqlx::Encode<'t, Db> + sqlx::Type<Db>,
{
qb.push("(");
{
let mut sep = qb.separated(", ");
for item in items {
sep.push_bind(item);
}
}
qb.push(")");
}