subgraph/data_sources/sql/services/find_many/
mod.rs1use async_graphql::ErrorExtensions;
2use log::{debug, error, trace};
3use sqlx::{mysql::MySqlArguments, MySql, Row};
4
5use crate::{
6 data_sources::{
7 sql::{PoolEnum, SqlQuery},
8 TotalCount,
9 },
10 sql_value::SqlValue,
11};
12
13use super::{ResponseRow, Services};
14
15impl Services {
16 pub async fn find_many(
17 pool_enum: &PoolEnum,
18 sql_query: &SqlQuery,
19 has_selection_set: &bool,
20 ) -> Result<(Vec<Option<ResponseRow>>, TotalCount), async_graphql::Error> {
21 debug!("Executing Find Many Query");
22 trace!("{:?}", sql_query);
23
24 match pool_enum {
25 PoolEnum::MySql(pool) => {
26 let mut query =
27 sqlx::query(&sql_query.query) as sqlx::query::Query<MySql, MySqlArguments>;
28 let count_query_str = &sql_query.count_query.clone().unwrap();
29 let mut count_query = sqlx::query(&count_query_str);
30
31 for value in &sql_query.where_values {
32 match value {
33 SqlValue::String(v) | SqlValue::ObjectID(v) => {
34 query = query.bind(v);
35 count_query = count_query.bind(v);
36 }
37 SqlValue::Int(v) => {
38 query = query.bind(v);
39 count_query = count_query.bind(v);
40 }
41 SqlValue::Bool(v) => {
42 query = query.bind(v);
43 count_query = count_query.bind(v);
44 }
45 SqlValue::StringList(values) | SqlValue::ObjectIDList(values) => {
46 for string in values {
47 query = query.bind(string);
48 count_query = count_query.bind(string);
49 }
50 }
51 SqlValue::IntList(values) => {
52 for int in values {
53 query = query.bind(int);
54 count_query = count_query.bind(int);
55 }
56 }
57 SqlValue::BoolList(values) => {
58 for bool in values {
59 query = query.bind(bool);
60 count_query = count_query.bind(bool);
61 }
62 }
63 SqlValue::UUID(v) => {
64 query = query.bind(v);
65 count_query = count_query.bind(v);
66 }
67 SqlValue::UUIDList(values) => {
68 for uuid in values {
69 query = query.bind(uuid);
70 count_query = count_query.bind(uuid);
71 }
72 }
73 SqlValue::DateTime(v) => {
74 query = query.bind(v);
75 count_query = count_query.bind(v);
76 }
77 SqlValue::DateTimeList(values) => {
78 for datetime in values {
79 query = query.bind(datetime);
80 count_query = count_query.bind(datetime);
81 }
82 }
83 }
84 }
85
86 let rows = if *has_selection_set {
87 query.fetch_all(pool).await.map_err(|e| {
88 error!("Error executing query: {:?}", e);
89 async_graphql::Error::new("Error executing query.")
90 .extend_with(|_, err| err.set("cause", e.to_string()))
91 })?
92 } else {
93 Vec::new()
94 };
95
96 let mut response_rows = Vec::new();
97 for row in rows {
98 response_rows.push(Some(ResponseRow::MySql(row)));
99 }
100
101 let count = count_query.fetch_one(pool).await.map_err(|e| {
102 error!("Error executing query: {:?} \n Error: {:?}", sql_query, e);
103 async_graphql::Error::new("Error executing query.")
104 .extend_with(|_, err| err.set("cause", e.to_string()))
105 })?;
106
107 let total_count = count.try_get("total_count").unwrap_or(0);
108 trace!("Total Count: {:?}", total_count);
109
110 Ok((response_rows, TotalCount(total_count)))
111 }
112 PoolEnum::Postgres(pool) => {
113 let mut query = sqlx::query(&sql_query.query);
114 let count_query_str = &sql_query.count_query.clone().unwrap();
115 let mut count_query = sqlx::query(&count_query_str);
116
117 for value in &sql_query.where_values {
118 match value {
119 SqlValue::String(v) | SqlValue::ObjectID(v) => {
120 query = query.bind(v);
121 count_query = count_query.bind(v);
122 }
123 SqlValue::Int(v) => {
124 query = query.bind(v);
125 count_query = count_query.bind(v);
126 }
127 SqlValue::Bool(v) => {
128 query = query.bind(v);
129 count_query = count_query.bind(v);
130 }
131 SqlValue::StringList(values) | SqlValue::ObjectIDList(values) => {
132 for string in values {
133 query = query.bind(string);
134 count_query = count_query.bind(string);
135 }
136 }
137 SqlValue::IntList(values) => {
138 for int in values {
139 query = query.bind(int);
140 count_query = count_query.bind(int);
141 }
142 }
143 SqlValue::BoolList(values) => {
144 for bool in values {
145 query = query.bind(bool);
146 count_query = count_query.bind(bool);
147 }
148 }
149 SqlValue::UUID(v) => {
150 query = query.bind(v);
151 count_query = count_query.bind(v);
152 }
153 SqlValue::UUIDList(values) => {
154 for uuid in values {
155 query = query.bind(uuid);
156 count_query = count_query.bind(uuid);
157 }
158 }
159 SqlValue::DateTime(v) => {
160 query = query.bind(v);
161 count_query = count_query.bind(v);
162 }
163 SqlValue::DateTimeList(values) => {
164 for datetime in values {
165 query = query.bind(datetime);
166 count_query = count_query.bind(datetime);
167 }
168 }
169 }
170 }
171
172 let rows = if *has_selection_set {
173 query.fetch_all(pool).await.map_err(|e| {
174 error!("Error executing query: {:?}", e);
175 async_graphql::Error::new("Error executing query.")
176 .extend_with(|_, err| err.set("cause", e.to_string()))
177 })?
178 } else {
179 Vec::new()
180 };
181
182 let mut response_rows = Vec::new();
183 for row in rows {
184 response_rows.push(Some(ResponseRow::Postgres(row)));
185 }
186
187 let count = count_query.fetch_one(pool).await.map_err(|e| {
188 error!(
189 "Error executing count query: {:?} \n Error: {:?}",
190 sql_query, e
191 );
192 async_graphql::Error::new("Error executing query.")
193 .extend_with(|_, err| err.set("cause", e.to_string()))
194 })?;
195
196 let total_count = count.try_get("total_count").unwrap_or(0);
197 trace!("Total Count: {:?}", total_count);
198
199 Ok((response_rows, TotalCount(total_count)))
200 }
201 PoolEnum::SqLite(pool) => {
202 let mut query = sqlx::query(&sql_query.query);
203 let count_query_str = &sql_query.count_query.clone().unwrap();
204 let mut count_query = sqlx::query(&count_query_str);
205
206 for value in &sql_query.where_values {
207 match value {
208 SqlValue::String(v) | SqlValue::ObjectID(v) => {
209 query = query.bind(v);
210 count_query = count_query.bind(v);
211 }
212 SqlValue::Int(v) => {
213 query = query.bind(v);
214 count_query = count_query.bind(v);
215 }
216 SqlValue::Bool(v) => {
217 query = query.bind(v);
218 count_query = count_query.bind(v);
219 }
220 SqlValue::StringList(values) | SqlValue::ObjectIDList(values) => {
221 for string in values {
222 query = query.bind(string);
223 count_query = count_query.bind(string)
224 }
225 }
226 SqlValue::IntList(values) => {
227 for int in values {
228 query = query.bind(int);
229 count_query = count_query.bind(int)
230 }
231 }
232 SqlValue::BoolList(values) => {
233 for bool in values {
234 query = query.bind(bool);
235 count_query = count_query.bind(bool)
236 }
237 }
238 SqlValue::UUID(v) => {
239 query = query.bind(v);
240 count_query = count_query.bind(v);
241 }
242 SqlValue::UUIDList(values) => {
243 for uuid in values {
244 query = query.bind(uuid);
245 count_query = count_query.bind(uuid)
246 }
247 }
248 SqlValue::DateTime(v) => {
249 query = query.bind(v);
250 count_query = count_query.bind(v);
251 }
252 SqlValue::DateTimeList(values) => {
253 for datetime in values {
254 count_query = count_query.bind(datetime);
255 query = query.bind(datetime)
256 }
257 }
258 }
259 }
260
261 let rows = if *has_selection_set {
262 query.fetch_all(pool).await.map_err(|e| {
263 error!("Error executing query: {:?} \n Error: {:?}", sql_query, e);
264 async_graphql::Error::new("Error executing query.")
265 .extend_with(|_, err| err.set("cause", e.to_string()))
266 })?
267 } else {
268 Vec::new()
269 };
270
271 let mut response_rows = Vec::new();
272 for row in rows {
273 response_rows.push(Some(ResponseRow::SqLite(row)));
274 }
275
276 let count = count_query.fetch_one(pool).await.map_err(|e| {
277 error!("Error executing query: {:?} \n Error: {:?}", sql_query, e);
278 async_graphql::Error::new("Error executing query.")
279 .extend_with(|_, err| err.set("cause", e.to_string()))
280 })?;
281
282 let total_count = count.try_get("total_count").unwrap_or(0);
283 trace!("Total Count: {:?}", total_count);
284
285 Ok((response_rows, TotalCount(total_count)))
286 }
287 }
288 }
289}