From a4ea08eca1aea0aa2dfc9d1e2a5ad598a0ecc4ab Mon Sep 17 00:00:00 2001 From: Billy Chan Date: Tue, 21 Jun 2022 10:53:27 +0800 Subject: [PATCH] Cursor pagination with composite keys --- src/executor/cursor.rs | 68 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) diff --git a/src/executor/cursor.rs b/src/executor/cursor.rs index e369e5cba4..b891dbd11c 100644 --- a/src/executor/cursor.rs +++ b/src/executor/cursor.rs @@ -360,4 +360,72 @@ mod tests { 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((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(()) + } }