From 3bf52714e673f91166cf6e6de73306bd37926949 Mon Sep 17 00:00:00 2001 From: Bastian Schubert Date: Fri, 19 May 2023 07:28:21 +0200 Subject: [PATCH 1/4] add args to query builder (#2494) --- sqlx-core/src/query_builder.rs | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/sqlx-core/src/query_builder.rs b/sqlx-core/src/query_builder.rs index 10dd4d7e9f..1d89e5bbf8 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,20 @@ where } } + pub fn new_with(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!( From 702ac6fd346d26e5c5309a27ea09af1e2f56ec1f Mon Sep 17 00:00:00 2001 From: Bastian Schubert Date: Fri, 19 May 2023 11:02:06 +0200 Subject: [PATCH 2/4] add test --- sqlx-core/src/query_builder.rs | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/sqlx-core/src/query_builder.rs b/sqlx-core/src/query_builder.rs index 1d89e5bbf8..ced67981e4 100644 --- a/sqlx-core/src/query_builder.rs +++ b/sqlx-core/src/query_builder.rs @@ -649,4 +649,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" + ); + } } From 00a47d8334f867ceaa36f2ef68a3e68103bff159 Mon Sep 17 00:00:00 2001 From: Bastian Date: Fri, 23 Jun 2023 08:19:47 +0200 Subject: [PATCH 3/4] Update sqlx-core/src/query_builder.rs Co-authored-by: Austin Bonander --- sqlx-core/src/query_builder.rs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/sqlx-core/src/query_builder.rs b/sqlx-core/src/query_builder.rs index ced67981e4..4a3e9aa6cb 100644 --- a/sqlx-core/src/query_builder.rs +++ b/sqlx-core/src/query_builder.rs @@ -49,7 +49,12 @@ where } } - pub fn new_with(init: impl Into, arguments: A) -> Self + + /// 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>, From a283d861b404d7b9c2b3c5cc16dc2c5748206082 Mon Sep 17 00:00:00 2001 From: Bastian Schubert Date: Fri, 23 Jun 2023 08:20:57 +0200 Subject: [PATCH 4/4] fmt --- sqlx-core/src/query_builder.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/sqlx-core/src/query_builder.rs b/sqlx-core/src/query_builder.rs index 4a3e9aa6cb..a04c98b88f 100644 --- a/sqlx-core/src/query_builder.rs +++ b/sqlx-core/src/query_builder.rs @@ -49,7 +49,6 @@ where } } - /// Construct a `QueryBuilder` with existing SQL and arguments. /// /// ### Note