Skip to content

Commit

Permalink
add args to query builder (#2494) (#2506)
Browse files Browse the repository at this point in the history
* add args to query builder (#2494)

* add test

* Update sqlx-core/src/query_builder.rs

Co-authored-by: Austin Bonander <austin.bonander@gmail.com>

* fmt

---------

Co-authored-by: Bastian Schubert <bastian.schubert@dock.financial>
Co-authored-by: Austin Bonander <austin.bonander@gmail.com>
  • Loading branch information
3 people committed Jun 30, 2023
1 parent 3fdb79d commit c2e54ea
Showing 1 changed file with 38 additions and 1 deletion.
39 changes: 38 additions & 1 deletion sqlx-core/src/query_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<A>(init: impl Into<String>, 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!(
Expand Down Expand Up @@ -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"
);
}
}

0 comments on commit c2e54ea

Please sign in to comment.