use sqlx::{Database, Error, Executor};
pub(crate) const PUBLISHED_PRIORITY: i16 = 1;
pub(crate) async fn unreadable<DB>(conn: &mut DB::Connection) -> Result<(), Error>
where
DB: Database,
for<'c> &'c mut DB::Connection: Executor<'c, Database = DB>,
{
sqlx::raw_sql("INSERT INTO unreadable_jobs (payload) VALUES (7)")
.execute(conn)
.await?;
Ok(())
}
pub(crate) mod own_fetch {
use sqlx::{AssertSqlSafe, Error, FromRow};
#[cfg(feature = "mysql")]
use sqlx::{MySqlConnection, mysql::MySqlRow};
#[cfg(feature = "postgres")]
use sqlx::{PgConnection, postgres::PgRow};
#[cfg(feature = "sqlite")]
use sqlx::{SqliteConnection, sqlite::SqliteRow};
pub(super) fn refuse_none(ids: &[i64]) -> Result<(), Error> {
if ids.is_empty() {
return Err(Error::InvalidArgument(
"the claim handed the service's fetch no ids".to_owned(),
));
}
Ok(())
}
#[cfg(any(feature = "mysql", feature = "sqlite"))]
fn listed(columns: &str, ids: &[i64]) -> AssertSqlSafe<String> {
let placeholders = vec!["?"; ids.len()].join(", ");
AssertSqlSafe(format!(
"SELECT {columns} FROM plain_jobs WHERE id IN ({placeholders})"
))
}
#[cfg(feature = "postgres")]
pub(crate) async fn postgres<Row>(
conn: &mut PgConnection,
columns: &str,
ids: &[i64],
) -> Result<Vec<Row>, Error>
where
Row: for<'r> FromRow<'r, PgRow> + Send + Unpin,
{
refuse_none(ids)?;
sqlx::query_as(AssertSqlSafe(format!(
"SELECT {columns} FROM plain_jobs WHERE id = ANY($1)"
)))
.bind(ids)
.fetch_all(conn)
.await
}
#[cfg(feature = "mysql")]
pub(crate) async fn mysql<Row>(
conn: &mut MySqlConnection,
columns: &str,
ids: &[i64],
) -> Result<Vec<Row>, Error>
where
Row: for<'r> FromRow<'r, MySqlRow> + Send + Unpin,
{
refuse_none(ids)?;
let mut select = sqlx::query_as(listed(columns, ids));
for id in ids {
select = select.bind(id);
}
select.fetch_all(conn).await
}
#[cfg(feature = "sqlite")]
pub(crate) async fn sqlite<Row>(
conn: &mut SqliteConnection,
columns: &str,
ids: &[i64],
) -> Result<Vec<Row>, Error>
where
Row: for<'r> FromRow<'r, SqliteRow> + Send + Unpin,
{
refuse_none(ids)?;
let mut select = sqlx::query_as(listed(columns, ids));
for id in ids {
select = select.bind(id);
}
select.fetch_all(conn).await
}
}
pub(crate) mod mail_fetch {
use sqlx::{AssertSqlSafe, Error, FromRow};
#[cfg(feature = "mysql")]
use sqlx::{MySqlConnection, mysql::MySqlRow};
#[cfg(feature = "postgres")]
use sqlx::{PgConnection, postgres::PgRow};
#[cfg(feature = "sqlite")]
use sqlx::{SqliteConnection, sqlite::SqliteRow};
use super::own_fetch::refuse_none;
const KEPT: &str = "(subject IS NULL OR subject <> 'gone')";
#[cfg(any(feature = "mysql", feature = "sqlite"))]
fn listed(columns: &str, ids: &[i64]) -> AssertSqlSafe<String> {
let placeholders = vec!["?"; ids.len()].join(", ");
AssertSqlSafe(format!(
"SELECT {columns} FROM mail_jobs WHERE job_id IN ({placeholders}) AND {KEPT} \
ORDER BY job_id DESC"
))
}
#[cfg(feature = "postgres")]
pub(crate) async fn postgres<Row>(
conn: &mut PgConnection,
columns: &str,
ids: &[i64],
) -> Result<Vec<Row>, Error>
where
Row: for<'r> FromRow<'r, PgRow> + Send + Unpin,
{
refuse_none(ids)?;
sqlx::query_as(AssertSqlSafe(format!(
"SELECT {columns} FROM mail_jobs WHERE job_id = ANY($1) AND {KEPT} \
ORDER BY job_id DESC"
)))
.bind(ids)
.fetch_all(conn)
.await
}
#[cfg(feature = "mysql")]
pub(crate) async fn mysql<Row>(
conn: &mut MySqlConnection,
columns: &str,
ids: &[i64],
) -> Result<Vec<Row>, Error>
where
Row: for<'r> FromRow<'r, MySqlRow> + Send + Unpin,
{
refuse_none(ids)?;
let mut select = sqlx::query_as(listed(columns, ids));
for id in ids {
select = select.bind(id);
}
select.fetch_all(conn).await
}
#[cfg(feature = "sqlite")]
pub(crate) async fn sqlite<Row>(
conn: &mut SqliteConnection,
columns: &str,
ids: &[i64],
) -> Result<Vec<Row>, Error>
where
Row: for<'r> FromRow<'r, SqliteRow> + Send + Unpin,
{
refuse_none(ids)?;
let mut select = sqlx::query_as(listed(columns, ids));
for id in ids {
select = select.bind(id);
}
select.fetch_all(conn).await
}
}
pub(crate) mod order_fetch {
use sqlx::{AssertSqlSafe, Error, FromRow};
#[cfg(feature = "mysql")]
use sqlx::{MySqlConnection, mysql::MySqlRow};
#[cfg(feature = "postgres")]
use sqlx::{PgConnection, postgres::PgRow};
#[cfg(feature = "sqlite")]
use sqlx::{SqliteConnection, sqlite::SqliteRow};
use super::own_fetch::refuse_none;
const JOINED: &str = "headed_jobs j JOIN customer_orders o ON o.id = j.order_id";
#[cfg(any(feature = "mysql", feature = "sqlite"))]
fn listed(columns: &str, ids: &[i64]) -> AssertSqlSafe<String> {
let placeholders = vec!["?"; ids.len()].join(", ");
AssertSqlSafe(format!(
"SELECT {columns} FROM {JOINED} WHERE j.job_id IN ({placeholders})"
))
}
#[cfg(feature = "postgres")]
pub(crate) async fn postgres<Row>(
conn: &mut PgConnection,
columns: &str,
ids: &[i64],
) -> Result<Vec<Row>, Error>
where
Row: for<'r> FromRow<'r, PgRow> + Send + Unpin,
{
refuse_none(ids)?;
sqlx::query_as(AssertSqlSafe(format!(
"SELECT {columns} FROM {JOINED} WHERE j.job_id = ANY($1)"
)))
.bind(ids)
.fetch_all(conn)
.await
}
#[cfg(feature = "mysql")]
pub(crate) async fn mysql<Row>(
conn: &mut MySqlConnection,
columns: &str,
ids: &[i64],
) -> Result<Vec<Row>, Error>
where
Row: for<'r> FromRow<'r, MySqlRow> + Send + Unpin,
{
refuse_none(ids)?;
let mut select = sqlx::query_as(listed(columns, ids));
for id in ids {
select = select.bind(id);
}
select.fetch_all(conn).await
}
#[cfg(feature = "sqlite")]
pub(crate) async fn sqlite<Row>(
conn: &mut SqliteConnection,
columns: &str,
ids: &[i64],
) -> Result<Vec<Row>, Error>
where
Row: for<'r> FromRow<'r, SqliteRow> + Send + Unpin,
{
refuse_none(ids)?;
let mut select = sqlx::query_as(listed(columns, ids));
for id in ids {
select = select.bind(id);
}
select.fetch_all(conn).await
}
}
pub(crate) mod row_lock;
pub(crate) mod lease;
pub(crate) mod advisory;