taitan_orm/
sql_api.rs

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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
use crate::result::Result;
// use sqlx::{Connection, Database};
use sqlx::Database;
use taitan_orm_trait::paged_list::PagedList;
use taitan_orm_trait::pagination::Pagination;
use taitan_orm_trait::{
    Entity, Location, Mutation, OrderBy, SelectedEntity, TemplateRecord, Unique,
};
// use tracing::debug;

/**
查询语义设计:
返回值语义:
option: 返回至多1个元素
vec:    返回元素数组,数组可能为空
page:  返回元素分页数组

行级筛选语义:
select: 使用唯一键筛选,对应返回值option
search: 使用索引筛选,对应返回值vec/paged
devour: 不使用条件筛选,对应返回值vec/paged

字段级筛选语义:
selection具备返回all_fields的方法

最终查询api设计:
select(selection, unique) -> option<SE>

search(selection, location, order_by)
search_paged(selection, location, page, order_by)

devour(selection, order_by)
devour_paged(selection, page, order_by)
*/
pub trait SqlApi {
    // type G: SqlGenerator + Sync + Debug;
    // fn get_generator(&mut self) -> &Self::G;

    async fn insert(&mut self, entity: &dyn Entity) -> Result<bool>;
    async fn upsert(&mut self, entity: &dyn Entity) -> Result<bool>;
    async fn update<M: Mutation>(&mut self, mutation: &M, unique: &dyn Unique<Mutation = M>) -> Result<bool>;
    async fn change<M: Mutation>(&mut self, mutation: &M, location: &M::Location) -> Result<u64>;
    async fn delete<M: Mutation>(&mut self, unique: &dyn Unique<Mutation = M>) -> Result<bool>;
    async fn purify(&mut self, location: &dyn Location) -> Result<u64>;

    async fn select<DB: Database, SE, M>(
        &mut self,
        selection: &SE::Selection,
        unique: &dyn Unique<Mutation = M>,
    ) -> Result<Option<SE>>
    where
        M: Mutation,
        SE: SelectedEntity<DB> + Send + Unpin;

    async fn search<DB: Database, SE>(
        &mut self,
        selection: &SE::Selection,
        location: &dyn Location,
        order_by: &dyn OrderBy,
    ) -> Result<Vec<SE>>
    where
        SE: SelectedEntity<DB> + Send + Unpin;

    async fn search_paged<DB: Database, SE>(
        &mut self,
        selection: &SE::Selection,
        location: &dyn Location,
        order_by: &dyn OrderBy,
        page: &Pagination,
    ) -> Result<PagedList<DB, SE>>
    where
        SE: SelectedEntity<DB> + Send + Unpin;

    /**
    根据表中所有数据
    */
    async fn devour<DB: Database, SE>(
        &mut self,
        selection: &SE::Selection,
        order_by: &dyn OrderBy,
    ) -> Result<Vec<SE>>
    where
        SE: SelectedEntity<DB> + Send + Unpin;

    async fn devour_paged<DB: Database, SE>(
        &mut self,
        selection: &SE::Selection,
        order_by: &dyn OrderBy,
        page: &Pagination,
    ) -> Result<PagedList<DB, SE>>
    where
        SE: SelectedEntity<DB> + Send + Unpin;

    async fn count(&mut self, location: &dyn Location) -> Result<u64>;

    async fn count_table(&mut self, table_name: &str) -> Result<u64>;

    async fn execute_by_template(&mut self, template: &dyn TemplateRecord) -> Result<usize>;

    // async fn select_by_template<SE>(
    //     &mut self,
    //     template: &dyn TemplateRecord,
    // ) -> Result<Option<SE>>
    // where
    //     SE: SelectedEntity<Self::DB> + Send + Unpin;

    // sqlx is not abstract enough

    // async fn _insert(&mut self, entity: &dyn Entity) -> Result<bool>;
    // async fn _upsert(&mut self, entity: &dyn Entity) -> Result<bool>;
    // async fn _update<M: Mutation>(&mut self, mutation: &M, unique: &M::Primary) -> Result<bool>;
    // async fn _change<M: Mutation>(&mut self, mutation: &M, location: &M::Location) -> Result<u64>;
    // async fn _delete(&mut self, unique: &dyn Unique) -> Result<bool>;
    // async fn _purify(&mut self, location: &dyn Location) -> Result<u64>;
    //
    //
    // async fn _select<SE>(
    //     &mut self,
    //     selection: &SE::Selection,
    //     unique: &dyn Unique,
    // ) -> Result<Option<SE>>
    // where
    //     SE: SelectedEntity<Self::DB> + Send + Unpin;
    //
    // async fn _search<SE>(
    //     &mut self,
    //     selection: &SE::Selection,
    //     location: &dyn Location,
    //     order_by: &dyn OrderBy,
    // ) -> Result<Vec<SE>>
    // where
    //     SE: SelectedEntity<Self::DB> + Send + Unpin;
    //
    // async fn _search_paged<SE>(
    //     &mut self,
    //     selection: &SE::Selection,
    //     location: &dyn Location,
    //     order_by: &dyn OrderBy,
    //     page: &Pagination,
    // ) -> Result<PagedList<Self::DB, SE>>
    // where
    //     SE: SelectedEntity<Self::DB> + Send + Unpin;
    //
    // /**
    // 根据表中所有数据
    // */
    // async fn _devour<SE>(
    //     &mut self,
    //     selection: &SE::Selection,
    //     order_by: &dyn OrderBy,
    // ) -> Result<Vec<SE>>
    // where
    //     SE: SelectedEntity<Self::DB> + Send + Unpin;
    //
    // async fn _devour_paged<SE>(
    //     &mut self,
    //     selection: &SE::Selection,
    //     order_by: &dyn OrderBy,
    //     page: &Pagination,
    // ) -> Result<PagedList<Self::DB, SE>>
    // where
    //     SE: SelectedEntity<Self::DB> + Send + Unpin;
    //
    // async fn _count(&mut self, location: &dyn Location) -> Result<u64>;
    //
    // async fn _count_table(&mut self, table_name: &str) -> Result<u64>;
    //
    // async fn _execute_by_template(&mut self, template: &dyn TemplateRecord) -> Result<usize>;
}