Skip to content

Commit

Permalink
Cursor pagination with composite keys
Browse files Browse the repository at this point in the history
  • Loading branch information
billy1624 committed Jun 21, 2022
1 parent bf882c8 commit a4ea08e
Showing 1 changed file with 68 additions and 0 deletions.
68 changes: 68 additions & 0 deletions src/executor/cursor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(())
}
}

0 comments on commit a4ea08e

Please sign in to comment.