easy_sqlx_core/sql/builder/
builder.rs1use 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 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 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 fn optional<'e, 'c: 'e, E, O>(
46 &self,
47 executor: E,
48 ) -> impl Future<Output = Result<Option<O>, Error>>
49 where
50 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 fn all<'e, 'c: 'e, E, O>(&self, executor: E) -> impl Future<Output = Result<Vec<O>, Error>>
59 where
60 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 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 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 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 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 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 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 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 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 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}