easy-sqlx-core 0.1.7

The Rust Toolkit to easy use sqlx
Documentation
use std::{collections::HashMap, future::Future, hash::Hash};

use sqlx::{Database, Error, Executor, FromRow};

use crate::sql::{
    dialects::page::{PageRequest, PageResult},
    schema::column::Column,
};

pub trait ExecuteBuilder {
    type DB: Database;

    fn execute<C>(
        &self,
        conn: &mut C,
    ) -> impl Future<Output = Result<<Self::DB as Database>::QueryResult, Error>>
    where
        for<'e> &'e mut C: Executor<'e, Database = Self::DB>;

    #[cfg(feature = "postgres")]
    /// 对于修改和删除操作,如果不是删除单条记录,请谨慎的使用此函数
    fn execute_return<'e, 'c: 'e, C, O>(
        &self,
        executor: C,
    ) -> impl Future<Output = Result<Vec<O>, Error>>
    where
        C: 'e + Executor<'c, Database = Self::DB>,
        O: 'e,
        for<'r> O: FromRow<'r, <Self::DB as Database>::Row>,
        O: std::marker::Send,
        O: Unpin;
}

pub trait QueryBuilder<'a> {
    type DB: Database;
    /// 获取一条记录
    fn one<'e, 'c: 'e, E, O>(&self, executor: E) -> impl Future<Output = Result<O, Error>>
    where
        E: 'e + Executor<'c, Database = Self::DB>,
        O: 'e,
        for<'r> O: FromRow<'r, <Self::DB as Database>::Row>,
        O: std::marker::Send,
        O: Unpin;
    /// 获取一条记录,如果不存在返回 None
    fn optional<'e, 'c: 'e, E, O>(
        &self,
        executor: E,
    ) -> impl Future<Output = Result<Option<O>, Error>>
    where
        // 'q: 'e,
        E: 'e + Executor<'c, Database = Self::DB>,
        for<'r> O: FromRow<'r, <Self::DB as Database>::Row>,
        O: 'e,
        O: std::marker::Send,
        O: Unpin;

    /// 获取全部记录
    fn all<'e, 'c: 'e, E, O>(&self, executor: E) -> impl Future<Output = Result<Vec<O>, Error>>
    where
        // 'q: 'e,
        E: 'e + Executor<'c, Database = Self::DB>,
        for<'r> O: FromRow<'r, <Self::DB as Database>::Row>,
        O: 'e,
        O: std::marker::Send,
        O: Unpin;

    /// 以 map 形式返回全部记录
    fn map_all<'e, 'c: 'e, E, O, F, K>(
        &self,
        executor: E,
        key_fn: F,
    ) -> impl Future<Output = Result<HashMap<K, O>, Error>>
    where
        // 'q: 'e,
        E: 'e + Executor<'c, Database = Self::DB>,
        for<'r> O: FromRow<'r, <Self::DB as Database>::Row>,
        O: 'e,
        O: std::marker::Send,
        O: Unpin,
        F: Fn(&O) -> K,
        K: Eq + Hash;

    /// 分页查询
    fn page<'e, 'c: 'e, E, O>(
        &self,
        executor: E,
        page: &PageRequest,
    ) -> impl Future<Output = Result<PageResult<O>, Error>>
    where
        // 'q: 'e,
        E: 'e + Executor<'c, Database = Self::DB>,
        for<'r> O: FromRow<'r, <Self::DB as Database>::Row>,
        O: 'e,
        O: std::marker::Send,
        O: Unpin;

    /// 查询记录数
    fn count<'c, E>(&self, executor: E) -> impl Future<Output = Result<usize, Error>>
    where
        E: 'c + Executor<'c, Database = Self::DB>;

    /// 获取一个标量
    fn one_scalar<'q, 'c, E, O>(
        &self,
        executor: E,
        field: &'q Column,
    ) -> impl Future<Output = Result<O, Error>>
    where
        (O,): for<'r> FromRow<'r, <Self::DB as Database>::Row>,
        E: 'c + Executor<'c, Database = Self::DB>,
        O: Send + Unpin;
    /// 获取一个可选标量,如果不存在返回 None
    fn optional_scalar<'q, 'c, E, O>(
        &self,
        executor: E,
        field: &'q Column,
    ) -> impl Future<Output = Result<Option<O>, Error>>
    where
        (O,): for<'r> FromRow<'r, <Self::DB as Database>::Row>,
        E: 'c + Executor<'c, Database = Self::DB>,
        O: Send + Unpin;
    /// 获取全部标量
    fn all_scalars<'q, 'c, E, O>(
        &self,
        executor: E,
        field: &'q Column,
    ) -> impl Future<Output = Result<Vec<O>, Error>>
    where
        (O,): for<'r> FromRow<'r, <Self::DB as Database>::Row>,
        E: 'c + Executor<'c, Database = Self::DB>,
        O: Send + Unpin;

    /// 分页获取标量
    fn page_scalars<'e, 'c: 'e, E, O>(
        &self,
        executor: E,
        field: &'c Column,
        page: &PageRequest,
    ) -> impl Future<Output = Result<PageResult<O>, Error>>
    where
        (O,): for<'r> FromRow<'r, <Self::DB as Database>::Row>,
        E: 'e + Executor<'c, Database = Self::DB>,
        O: Send + Unpin;
}