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
use std::future::Future;

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

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

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>;
}

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;

    /// 分页查询
    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> + 'c + Copy,
        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 str,
    ) -> 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 str,
    ) -> 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 str,
    ) -> 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 str,
        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> + 'c + Copy,
        O: Send + Unpin;
}