Replies: 1 comment
-
Same problem here with this code : async fn find_by(
&self,
criterias: Criterias,
pagination: Pagination,
) -> Result<PaginatedResult<MedicalTreatments>, sqlx::Error> {
// In Rust, you can either have many immutable references,
// or one mutable reference.
let mut query_builder: QueryBuilder<Postgres> =
QueryBuilder::new("SELECT * FROM medical_treatments WHERE ");
let separated: &mut Separated<Postgres, &str> = &mut query_builder.separated(" AND ");
if let Some(id) = criterias.id {
separated.push("id = ").push_bind_unseparated(id);
}
if let Some(name) = criterias.name {
separated.push("name = ").push_bind_unseparated(name);
}
// calculating total before applying pagination.
// error : cannot borrow `query_builder` as mutable more than once at a time [E0499] second mutable borrow occurs here
let mut total: usize = query_builder
.build_query_as::<MedicalTreatments>()
.fetch_all(&self.pool)
.await?
.len();
separated
.push_unseparated(" LIMIT ")
.push_bind_unseparated(pagination.items_per_page)
.push_unseparated(" OFFSET ")
.push_bind_unseparated(pagination.offset());
let medical_treatments: Vec<MedicalTreatments> = query_builder
.build_query_as::<MedicalTreatments>()
.fetch_all(&self.pool)
.await?;
let paginated_result: PaginatedResult<MedicalTreatments> = PaginatedResult {
items: medical_treatments,
total,
};
Ok(paginated_result)
} I don't know if it's technically feasable to derive |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I'm encountering a problem where i have ONE builder which is used for a query with bind parameters,
and i need another query builder for a "total count"
really... i just need one builder and ideally i can call builder.clone() and pass that in.
instead, because i cannot, i have to have 2 separate builders that i push redundant filters in. im not sure if theres a better way...
Beta Was this translation helpful? Give feedback.
All reactions