pub(crate) mod cursor;
pub(crate) mod join;
pub(crate) mod model;
pub(crate) mod pk;
pub(crate) mod predicate;
use toasty::stmt::{Expr, Value};
use toasty_core::stmt::{ExprLike, VisitMut, visit_mut};
pub(crate) type UntypedInclude = toasty_core::stmt::Include;
pub use toasty::schema::app::VariantId;
pub(crate) use toasty_core::driver::operation::IsolationLevel;
pub(crate) use toasty_core::stmt::Assignments;
pub(crate) async fn count<M: toasty::schema::Model>(
query: toasty::stmt::Query<toasty::stmt::List<M>>,
ex: &mut dyn toasty::Executor,
) -> toasty::Result<u64> {
let untyped = toasty::stmt::IntoStatement::into_statement(query.count()).into_untyped();
let rows = toasty::stmt::Statement::<Option<u64>>::from_untyped_stmt(untyped)
.exec(ex)
.await?;
Ok(rows.unwrap_or(0))
}
pub(crate) fn value_text(value: &Value) -> Option<String> {
Some(match value {
Value::Bool(v) => v.to_string(),
Value::I8(v) => v.to_string(),
Value::I16(v) => v.to_string(),
Value::I32(v) => v.to_string(),
Value::I64(v) => v.to_string(),
Value::U8(v) => v.to_string(),
Value::U16(v) => v.to_string(),
Value::U32(v) => v.to_string(),
Value::U64(v) => v.to_string(),
Value::F32(v) => v.to_string(),
Value::F64(v) => v.to_string(),
Value::String(v) => v.clone(),
Value::Uuid(v) => v.to_string(),
Value::Timestamp(v) => v.to_string(),
Value::Date(v) => v.to_string(),
Value::Time(v) => v.to_string(),
Value::DateTime(v) => v.to_string(),
Value::Zoned(v) => v.to_string(),
Value::Bytes(v) => String::from_utf8(v.clone()).ok()?,
_ => return None,
})
}
pub(crate) fn never() -> Expr<bool> {
Expr::from_untyped(toasty_core::stmt::Expr::from(false))
}
pub(crate) fn case_insensitive_search(db: &toasty::Db, predicate: Expr<bool>) -> Expr<bool> {
if db.capability().native_ilike {
ilike(predicate)
} else {
predicate
}
}
fn ilike(predicate: Expr<bool>) -> Expr<bool> {
struct Ilike;
impl VisitMut for Ilike {
fn visit_expr_like_mut(&mut self, like: &mut ExprLike) {
like.case_insensitive = true;
visit_mut::visit_expr_like_mut(self, like);
}
}
let mut untyped = toasty_core::stmt::Expr::from(predicate);
Ilike.visit_expr_mut(&mut untyped);
Expr::from_untyped(untyped)
}
#[cfg(test)]
mod tests;