use sqlx::{sqlite::SqliteRow, SqlitePool};
use super::executor;
use crate::core::condition::SqlValue;
use crate::core::model::Model;
use crate::core::query::QueryBuilder;
pub trait SqliteModel: Model + for<'r> sqlx::FromRow<'r, SqliteRow> + Send + Unpin {
fn all(
pool: &SqlitePool,
) -> impl std::future::Future<Output = Result<Vec<Self>, sqlx::Error>> + Send
where
Self: Sized,
{
executor::fetch_all(pool, Self::query())
}
fn find_where(
pool: &SqlitePool,
builder: QueryBuilder<Self>,
) -> impl std::future::Future<Output = Result<Vec<Self>, sqlx::Error>> + Send
where
Self: Sized,
{
executor::fetch_all(pool, builder)
}
fn find_by_pk(
pool: &SqlitePool,
id: impl Into<SqlValue> + Send,
) -> impl std::future::Future<Output = Result<Option<Self>, sqlx::Error>> + Send
where
Self: Sized,
{
executor::fetch_optional(pool, Self::find(id))
}
fn count(
pool: &SqlitePool,
) -> impl std::future::Future<Output = Result<i64, sqlx::Error>> + Send
where
Self: Sized,
{
executor::count(pool, Self::query())
}
fn count_where(
pool: &SqlitePool,
builder: QueryBuilder<Self>,
) -> impl std::future::Future<Output = Result<i64, sqlx::Error>> + Send
where
Self: Sized,
{
executor::count(pool, builder)
}
fn create(
pool: &SqlitePool,
data: &[(&str, SqlValue)],
) -> impl std::future::Future<Output = Result<u64, sqlx::Error>> + Send
where
Self: Sized,
{
executor::insert::<Self>(pool, Self::table_name(), data)
}
fn update_by_pk(
pool: &SqlitePool,
id: impl Into<SqlValue> + Send,
data: &[(&str, SqlValue)],
) -> impl std::future::Future<Output = Result<u64, sqlx::Error>> + Send
where
Self: Sized,
{
let builder = Self::find(id);
executor::update::<Self>(pool, builder, data)
}
fn delete_by_pk(
pool: &SqlitePool,
id: impl Into<SqlValue> + Send,
) -> impl std::future::Future<Output = Result<u64, sqlx::Error>> + Send
where
Self: Sized,
{
executor::delete(pool, Self::find(id))
}
fn delete_where(
pool: &SqlitePool,
builder: QueryBuilder<Self>,
) -> impl std::future::Future<Output = Result<u64, sqlx::Error>> + Send
where
Self: Sized,
{
executor::delete(pool, builder)
}
fn update_where(
pool: &SqlitePool,
builder: QueryBuilder<Self>,
data: &[(&str, SqlValue)],
) -> impl std::future::Future<Output = Result<u64, sqlx::Error>> + Send
where
Self: Sized,
{
executor::update::<Self>(pool, builder, data)
}
fn create_returning(
pool: &SqlitePool,
data: &[(&str, SqlValue)],
) -> impl std::future::Future<Output = Result<Self, sqlx::Error>> + Send
where
Self: Sized,
{
executor::insert_returning::<Self>(pool, Self::table_name(), data)
}
}
impl<T> SqliteModel for T where T: Model + for<'r> sqlx::FromRow<'r, SqliteRow> + Send + Unpin {}