1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
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;
}