From c2e54eae6f021ba168ab98872f5aa97b0b497c70 Mon Sep 17 00:00:00 2001 From: Bastian Date: Fri, 30 Jun 2023 23:26:25 +0200 Subject: [PATCH] add args to query builder (#2494) (#2506) * add args to query builder (#2494) * add test * Update sqlx-core/src/query_builder.rs Co-authored-by: Austin Bonander * fmt --------- Co-authored-by: Bastian Schubert Co-authored-by: Austin Bonander --- sqlx-core/src/query_builder.rs | 39 +++++++++++++++++++++++++++++++++- 1 file changed, 38 insertions(+), 1 deletion(-) diff --git a/sqlx-core/src/query_builder.rs b/sqlx-core/src/query_builder.rs index 10dd4d7e9f..a04c98b88f 100644 --- a/sqlx-core/src/query_builder.rs +++ b/sqlx-core/src/query_builder.rs @@ -4,7 +4,7 @@ use std::fmt::Display; use std::fmt::Write; use std::marker::PhantomData; -use crate::arguments::Arguments; +use crate::arguments::{Arguments, IntoArguments}; use crate::database::{Database, HasArguments}; use crate::encode::Encode; use crate::from_row::FromRow; @@ -49,6 +49,24 @@ where } } + /// Construct a `QueryBuilder` with existing SQL and arguments. + /// + /// ### Note + /// This does *not* check if `arguments` is valid for the given SQL. + pub fn with_arguments(init: impl Into, arguments: A) -> Self + where + DB: Database, + A: IntoArguments<'args, DB>, + { + let init = init.into(); + + QueryBuilder { + init_len: init.len(), + query: init, + arguments: Some(arguments.into_arguments()), + } + } + #[inline] fn sanity_check(&self) { assert!( @@ -635,4 +653,23 @@ mod test { "SELECT * FROM users WHERE id = 99" ); } + + #[test] + fn test_query_builder_with_args() { + let mut qb: QueryBuilder<'_, Postgres> = QueryBuilder::new(""); + + let query = qb + .push("SELECT * FROM users WHERE id = ") + .push_bind(42i32) + .build(); + + let mut qb: QueryBuilder<'_, Postgres> = + QueryBuilder::new_with(query.sql(), query.take_arguments()); + let query = qb.push("OR membership_level =").push_bind(3i32).build(); + + assert_eq!( + query.sql(), + "SELECT * FROM users WHERE id = $1 OR membership_level = $2" + ); + } }