-
-
Notifications
You must be signed in to change notification settings - Fork 521
/
cursor.rs
466 lines (426 loc) Β· 13.1 KB
/
cursor.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
use crate::{
ConnectionTrait, DbErr, EntityTrait, FromQueryResult, Identity, IntoIdentity, QueryOrder,
Select, SelectModel, SelectorTrait,
};
use sea_query::{
Condition, DynIden, Expr, IntoValueTuple, Order, OrderedStatement, SeaRc, SelectStatement,
SimpleExpr, Value, ValueTuple,
};
use std::marker::PhantomData;
#[cfg(feature = "with-json")]
use crate::JsonValue;
/// Cursor pagination
#[derive(Debug, Clone)]
pub struct Cursor<S>
where
S: SelectorTrait,
{
pub(crate) query: SelectStatement,
pub(crate) table: DynIden,
pub(crate) order_columns: Identity,
pub(crate) last: bool,
pub(crate) phantom: PhantomData<S>,
}
impl<S> Cursor<S>
where
S: SelectorTrait,
{
/// Initialize a cursor
pub fn new<C>(query: SelectStatement, table: DynIden, order_columns: C) -> Self
where
C: IntoIdentity,
{
Self {
query,
table,
order_columns: order_columns.into_identity(),
last: false,
phantom: PhantomData,
}
}
/// Filter paginated result with corresponding column less than the input value
pub fn before<V>(&mut self, values: V) -> &mut Self
where
V: IntoValueTuple,
{
let condition = self.apply_filter(values, |c, v| {
Expr::tbl(SeaRc::clone(&self.table), SeaRc::clone(c)).lt(v)
});
self.query.cond_where(condition);
self
}
/// Filter paginated result with corresponding column greater than the input value
pub fn after<V>(&mut self, values: V) -> &mut Self
where
V: IntoValueTuple,
{
let condition = self.apply_filter(values, |c, v| {
Expr::tbl(SeaRc::clone(&self.table), SeaRc::clone(c)).gt(v)
});
self.query.cond_where(condition);
self
}
fn apply_filter<V, F>(&self, values: V, f: F) -> Condition
where
V: IntoValueTuple,
F: Fn(&DynIden, Value) -> SimpleExpr,
{
match (&self.order_columns, values.into_value_tuple()) {
(Identity::Unary(c1), ValueTuple::One(v1)) => Condition::all().add(f(c1, v1)),
(Identity::Binary(c1, c2), ValueTuple::Two(v1, v2)) => {
Condition::all().add(f(c1, v1)).add(f(c2, v2))
}
(Identity::Ternary(c1, c2, c3), ValueTuple::Three(v1, v2, v3)) => Condition::all()
.add(f(c1, v1))
.add(f(c2, v2))
.add(f(c3, v3)),
_ => panic!("column arity mismatch"),
}
}
/// Limit result set to only first N rows in ascending order of the order by column
pub fn first(&mut self, num_rows: u64) -> &mut Self {
self.query.limit(num_rows).clear_order_by();
let table = SeaRc::clone(&self.table);
self.apply_order_by(|query, col| {
query.order_by((SeaRc::clone(&table), SeaRc::clone(col)), Order::Asc);
});
self.last = false;
self
}
/// Limit result set to only last N rows in ascending order of the order by column
pub fn last(&mut self, num_rows: u64) -> &mut Self {
self.query.limit(num_rows).clear_order_by();
let table = SeaRc::clone(&self.table);
self.apply_order_by(|query, col| {
query.order_by((SeaRc::clone(&table), SeaRc::clone(col)), Order::Desc);
});
self.last = true;
self
}
fn apply_order_by<F>(&mut self, f: F)
where
F: Fn(&mut SelectStatement, &DynIden),
{
let query = &mut self.query;
match &self.order_columns {
Identity::Unary(c1) => {
f(query, c1);
}
Identity::Binary(c1, c2) => {
f(query, c1);
f(query, c2);
}
Identity::Ternary(c1, c2, c3) => {
f(query, c1);
f(query, c2);
f(query, c3);
}
}
}
/// Fetch the paginated result
pub async fn all<C>(&mut self, db: &C) -> Result<Vec<S::Item>, DbErr>
where
C: ConnectionTrait,
{
let stmt = db.get_database_backend().build(&self.query);
let rows = db.query_all(stmt).await?;
let mut buffer = Vec::with_capacity(rows.len());
for row in rows.into_iter() {
buffer.push(S::from_raw_query_result(row)?);
}
if self.last {
buffer.reverse()
}
Ok(buffer)
}
/// Construct a [Cursor] that fetch any custom struct
pub fn into_model<M>(self) -> Cursor<SelectModel<M>>
where
M: FromQueryResult,
{
Cursor {
query: self.query,
table: self.table,
order_columns: self.order_columns,
last: self.last,
phantom: PhantomData,
}
}
/// Construct a [Cursor] that fetch JSON value
#[cfg(feature = "with-json")]
pub fn into_json(self) -> Cursor<SelectModel<JsonValue>> {
Cursor {
query: self.query,
table: self.table,
order_columns: self.order_columns,
last: self.last,
phantom: PhantomData,
}
}
}
impl<S> QueryOrder for Cursor<S>
where
S: SelectorTrait,
{
type QueryStatement = SelectStatement;
fn query(&mut self) -> &mut SelectStatement {
&mut self.query
}
}
/// A trait for any type that can be turn into a cursor
pub trait CursorTrait {
/// Select operation
type Selector: SelectorTrait + Send + Sync;
/// Convert current type into a cursor
fn cursor_by<C>(self, order_columns: C) -> Cursor<Self::Selector>
where
C: IntoIdentity;
}
impl<E, M> CursorTrait for Select<E>
where
E: EntityTrait<Model = M>,
M: FromQueryResult + Sized + Send + Sync,
{
type Selector = SelectModel<M>;
fn cursor_by<C>(self, order_columns: C) -> Cursor<Self::Selector>
where
C: IntoIdentity,
{
Cursor::new(self.query, SeaRc::new(E::default()), order_columns)
}
}
#[cfg(test)]
#[cfg(feature = "mock")]
mod tests {
use super::*;
use crate::entity::prelude::*;
use crate::tests_cfg::*;
use crate::{DbBackend, MockDatabase, Statement, Transaction};
use pretty_assertions::assert_eq;
#[smol_potat::test]
async fn first_2_before_10() -> Result<(), DbErr> {
use fruit::*;
let models = vec![
Model {
id: 1,
name: "Blueberry".into(),
cake_id: Some(1),
},
Model {
id: 2,
name: "Rasberry".into(),
cake_id: Some(1),
},
];
let db = MockDatabase::new(DbBackend::Postgres)
.append_query_results(vec![models.clone()])
.into_connection();
assert_eq!(
Entity::find()
.cursor_by(Column::Id)
.before(10)
.first(2)
.all(&db)
.await?,
models
);
assert_eq!(
db.into_transaction_log(),
vec![Transaction::many(vec![Statement::from_sql_and_values(
DbBackend::Postgres,
[
r#"SELECT "fruit"."id", "fruit"."name", "fruit"."cake_id""#,
r#"FROM "fruit""#,
r#"WHERE "fruit"."id" < $1"#,
r#"ORDER BY "fruit"."id" ASC"#,
r#"LIMIT $2"#,
]
.join(" ")
.as_str(),
vec![10_i32.into(), 2_u64.into()]
),])]
);
Ok(())
}
#[smol_potat::test]
async fn last_2_after_10() -> Result<(), DbErr> {
use fruit::*;
let db = MockDatabase::new(DbBackend::Postgres)
.append_query_results(vec![vec![
Model {
id: 22,
name: "Rasberry".into(),
cake_id: Some(1),
},
Model {
id: 21,
name: "Blueberry".into(),
cake_id: Some(1),
},
]])
.into_connection();
assert_eq!(
Entity::find()
.cursor_by(Column::Id)
.after(10)
.last(2)
.all(&db)
.await?,
vec![
Model {
id: 21,
name: "Blueberry".into(),
cake_id: Some(1),
},
Model {
id: 22,
name: "Rasberry".into(),
cake_id: Some(1),
},
]
);
assert_eq!(
db.into_transaction_log(),
vec![Transaction::many(vec![Statement::from_sql_and_values(
DbBackend::Postgres,
[
r#"SELECT "fruit"."id", "fruit"."name", "fruit"."cake_id""#,
r#"FROM "fruit""#,
r#"WHERE "fruit"."id" > $1"#,
r#"ORDER BY "fruit"."id" DESC"#,
r#"LIMIT $2"#,
]
.join(" ")
.as_str(),
vec![10_i32.into(), 2_u64.into()]
),])]
);
Ok(())
}
#[smol_potat::test]
async fn last_2_after_25_before_30() -> Result<(), DbErr> {
use fruit::*;
let db = MockDatabase::new(DbBackend::Postgres)
.append_query_results(vec![vec![
Model {
id: 27,
name: "Rasberry".into(),
cake_id: Some(1),
},
Model {
id: 26,
name: "Blueberry".into(),
cake_id: Some(1),
},
]])
.into_connection();
assert_eq!(
Entity::find()
.cursor_by(Column::Id)
.after(25)
.before(30)
.last(2)
.all(&db)
.await?,
vec![
Model {
id: 26,
name: "Blueberry".into(),
cake_id: Some(1),
},
Model {
id: 27,
name: "Rasberry".into(),
cake_id: Some(1),
},
]
);
assert_eq!(
db.into_transaction_log(),
vec![Transaction::many(vec![Statement::from_sql_and_values(
DbBackend::Postgres,
[
r#"SELECT "fruit"."id", "fruit"."name", "fruit"."cake_id""#,
r#"FROM "fruit""#,
r#"WHERE "fruit"."id" > $1"#,
r#"AND "fruit"."id" < $2"#,
r#"ORDER BY "fruit"."id" DESC"#,
r#"LIMIT $3"#,
]
.join(" ")
.as_str(),
vec![25_i32.into(), 30_i32.into(), 2_u64.into()]
),])]
);
Ok(())
}
#[smol_potat::test]
async fn composite_keys() -> Result<(), DbErr> {
use cake_filling::*;
let db = MockDatabase::new(DbBackend::Postgres)
.append_query_results(vec![vec![
Model {
cake_id: 1,
filling_id: 2,
},
Model {
cake_id: 1,
filling_id: 3,
},
Model {
cake_id: 2,
filling_id: 3,
},
]])
.into_connection();
assert_eq!(
Entity::find()
.cursor_by((Column::CakeId, Column::FillingId))
.after((0, 1))
.before((10, 11))
.first(3)
.all(&db)
.await?,
vec![
Model {
cake_id: 1,
filling_id: 2,
},
Model {
cake_id: 1,
filling_id: 3,
},
Model {
cake_id: 2,
filling_id: 3,
},
]
);
assert_eq!(
db.into_transaction_log(),
vec![Transaction::many(vec![Statement::from_sql_and_values(
DbBackend::Postgres,
[
r#"SELECT "cake_filling"."cake_id", "cake_filling"."filling_id""#,
r#"FROM "cake_filling""#,
r#"WHERE "cake_filling"."cake_id" > $1"#,
r#"AND "cake_filling"."filling_id" > $2"#,
r#"AND "cake_filling"."cake_id" < $3"#,
r#"AND "cake_filling"."filling_id" < $4"#,
r#"ORDER BY "cake_filling"."cake_id" ASC, "cake_filling"."filling_id" ASC"#,
r#"LIMIT $5"#,
]
.join(" ")
.as_str(),
vec![
0_i32.into(),
1_i32.into(),
10_i32.into(),
11_i32.into(),
3_u64.into()
]
),])]
);
Ok(())
}
}