easy_sqlx_core/sql/builder/
builder.rs

1use std::{collections::HashMap, future::Future, hash::Hash};
2
3use sqlx::{Database, Error, Executor, FromRow};
4
5use crate::sql::{
6    dialects::page::{PageRequest, PageResult},
7    schema::column::Column,
8};
9
10pub trait ExecuteBuilder {
11    type DB: Database;
12
13    fn execute<C>(
14        &self,
15        conn: &mut C,
16    ) -> impl Future<Output = Result<<Self::DB as Database>::QueryResult, Error>>
17    where
18        for<'e> &'e mut C: Executor<'e, Database = Self::DB>;
19
20    #[cfg(feature = "postgres")]
21    /// 对于修改和删除操作,如果不是删除单条记录,请谨慎的使用此函数
22    fn execute_return<'e, 'c: 'e, C, O>(
23        &self,
24        executor: C,
25    ) -> impl Future<Output = Result<Vec<O>, Error>>
26    where
27        C: 'e + Executor<'c, Database = Self::DB>,
28        O: 'e,
29        for<'r> O: FromRow<'r, <Self::DB as Database>::Row>,
30        O: std::marker::Send,
31        O: Unpin;
32}
33
34pub trait QueryBuilder<'a> {
35    type DB: Database;
36    /// 获取一条记录
37    fn one<'e, 'c: 'e, E, O>(&self, executor: E) -> impl Future<Output = Result<O, Error>>
38    where
39        E: 'e + Executor<'c, Database = Self::DB>,
40        O: 'e,
41        for<'r> O: FromRow<'r, <Self::DB as Database>::Row>,
42        O: std::marker::Send,
43        O: Unpin;
44    /// 获取一条记录,如果不存在返回 None
45    fn optional<'e, 'c: 'e, E, O>(
46        &self,
47        executor: E,
48    ) -> impl Future<Output = Result<Option<O>, Error>>
49    where
50        // 'q: 'e,
51        E: 'e + Executor<'c, Database = Self::DB>,
52        for<'r> O: FromRow<'r, <Self::DB as Database>::Row>,
53        O: 'e,
54        O: std::marker::Send,
55        O: Unpin;
56
57    /// 获取全部记录
58    fn all<'e, 'c: 'e, E, O>(&self, executor: E) -> impl Future<Output = Result<Vec<O>, Error>>
59    where
60        // 'q: 'e,
61        E: 'e + Executor<'c, Database = Self::DB>,
62        for<'r> O: FromRow<'r, <Self::DB as Database>::Row>,
63        O: 'e,
64        O: std::marker::Send,
65        O: Unpin;
66
67    /// 以 map 形式返回全部记录
68    fn map_all<'e, 'c: 'e, E, O, F, K>(
69        &self,
70        executor: E,
71        key_fn: F,
72    ) -> impl Future<Output = Result<HashMap<K, O>, Error>>
73    where
74        // 'q: 'e,
75        E: 'e + Executor<'c, Database = Self::DB>,
76        for<'r> O: FromRow<'r, <Self::DB as Database>::Row>,
77        O: 'e,
78        O: std::marker::Send,
79        O: Unpin,
80        F: Fn(&O) -> K,
81        K: Eq + Hash;
82
83    /// 分页查询
84    fn page<'e, 'c: 'e, E, O>(
85        &self,
86        executor: E,
87        page: &PageRequest,
88    ) -> impl Future<Output = Result<PageResult<O>, Error>>
89    where
90        // 'q: 'e,
91        E: 'e + Executor<'c, Database = Self::DB>,
92        for<'r> O: FromRow<'r, <Self::DB as Database>::Row>,
93        O: 'e,
94        O: std::marker::Send,
95        O: Unpin;
96
97    /// 查询记录数
98    fn count<'c, E>(&self, executor: E) -> impl Future<Output = Result<usize, Error>>
99    where
100        E: 'c + Executor<'c, Database = Self::DB>;
101
102    /// 获取一个标量
103    fn one_scalar<'q, 'c, E, O>(
104        &self,
105        executor: E,
106        field: &'q Column,
107    ) -> impl Future<Output = Result<O, Error>>
108    where
109        (O,): for<'r> FromRow<'r, <Self::DB as Database>::Row>,
110        E: 'c + Executor<'c, Database = Self::DB>,
111        O: Send + Unpin;
112    /// 获取一个可选标量,如果不存在返回 None
113    fn optional_scalar<'q, 'c, E, O>(
114        &self,
115        executor: E,
116        field: &'q Column,
117    ) -> impl Future<Output = Result<Option<O>, Error>>
118    where
119        (O,): for<'r> FromRow<'r, <Self::DB as Database>::Row>,
120        E: 'c + Executor<'c, Database = Self::DB>,
121        O: Send + Unpin;
122    /// 获取全部标量
123    fn all_scalars<'q, 'c, E, O>(
124        &self,
125        executor: E,
126        field: &'q Column,
127    ) -> impl Future<Output = Result<Vec<O>, Error>>
128    where
129        (O,): for<'r> FromRow<'r, <Self::DB as Database>::Row>,
130        E: 'c + Executor<'c, Database = Self::DB>,
131        O: Send + Unpin;
132
133    /// 分页获取标量
134    fn page_scalars<'e, 'c: 'e, E, O>(
135        &self,
136        executor: E,
137        field: &'c Column,
138        page: &PageRequest,
139    ) -> impl Future<Output = Result<PageResult<O>, Error>>
140    where
141        (O,): for<'r> FromRow<'r, <Self::DB as Database>::Row>,
142        E: 'e + Executor<'c, Database = Self::DB>,
143        O: Send + Unpin;
144}