Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow overriding the ? query argument #153

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,12 @@ impl Client {

/// Starts a new SELECT/DDL query.
pub fn query(&self, query: &str) -> query::Query {
query::Query::new(self, query)
query::Query::new(self, query, "?")
}

/// Starts a new SELECT/DDL query using the given arg for binding.
pub fn query_with_arg(&self, query: &str, arg: &str) -> query::Query {
serprex marked this conversation as resolved.
Show resolved Hide resolved
query::Query::new(self, query, arg)
}

/// Starts a new WATCH query.
Expand Down
4 changes: 2 additions & 2 deletions src/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ pub struct Query {
}

impl Query {
pub(crate) fn new(client: &Client, template: &str) -> Self {
pub(crate) fn new(client: &Client, template: &str, arg: &str) -> Self {
Self {
client: client.clone(),
sql: SqlBuilder::new(template),
sql: SqlBuilder::new(template, arg),
}
}

Expand Down
22 changes: 11 additions & 11 deletions src/sql/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ pub(crate) enum Part {
}

impl SqlBuilder {
pub(crate) fn new(template: &str) -> Self {
let mut iter = template.split('?');
pub(crate) fn new(template: &str, arg: &str) -> Self {
serprex marked this conversation as resolved.
Show resolved Hide resolved
let mut iter = template.split(arg);
serprex marked this conversation as resolved.
Show resolved Hide resolved
let prefix = String::from(iter.next().unwrap());
let mut parts = vec![Part::Text(prefix)];

Expand Down Expand Up @@ -140,7 +140,7 @@ mod tests {

#[test]
fn bound_args() {
let mut sql = SqlBuilder::new("SELECT ?fields FROM test WHERE a = ? AND b < ?");
let mut sql = SqlBuilder::new("SELECT ?fields FROM test WHERE a = ? AND b < ?", "?");
sql.bind_arg("foo");
sql.bind_arg(42);
sql.bind_fields::<Row>();
Expand All @@ -153,7 +153,7 @@ mod tests {
#[test]
fn in_clause() {
fn t(arg: &[&str], expected: &str) {
let mut sql = SqlBuilder::new("SELECT ?fields FROM test WHERE a IN ?");
let mut sql = SqlBuilder::new("SELECT ?fields FROM test WHERE a IN ?", "?");
sql.bind_arg(arg);
sql.bind_fields::<Row>();
assert_eq!(sql.finish().unwrap(), expected);
Expand All @@ -175,7 +175,7 @@ mod tests {
// See #18.
#[test]
fn question_marks_inside() {
let mut sql = SqlBuilder::new("SELECT 1 FROM test WHERE a IN ? AND b = ?");
let mut sql = SqlBuilder::new("SELECT 1 FROM test WHERE a IN ? AND b = ?", "?");
sql.bind_arg(&["a?b", "c?"][..]);
sql.bind_arg("a?");
assert_eq!(
Expand All @@ -186,38 +186,38 @@ mod tests {

#[test]
fn option_as_null() {
let mut sql = SqlBuilder::new("SELECT 1 FROM test WHERE a = ?");
let mut sql = SqlBuilder::new("SELECT 1 FROM test WHERE a = ?", "?");
sql.bind_arg(None::<u32>);
assert_eq!(sql.finish().unwrap(), r"SELECT 1 FROM test WHERE a = NULL");
}

#[test]
fn option_as_value() {
let mut sql = SqlBuilder::new("SELECT 1 FROM test WHERE a = ?");
let mut sql = SqlBuilder::new("SELECT 1 FROM test WHERE a = ?", "?");
sql.bind_arg(Some(1u32));
assert_eq!(sql.finish().unwrap(), r"SELECT 1 FROM test WHERE a = 1");
}

#[test]
fn failures() {
let mut sql = SqlBuilder::new("SELECT 1");
let mut sql = SqlBuilder::new("SELECT 1", "?");
sql.bind_arg(42);
let err = sql.finish().unwrap_err();
assert!(err.to_string().contains("all arguments are already bound"));

let mut sql = SqlBuilder::new("SELECT ?fields");
let mut sql = SqlBuilder::new("SELECT ?fields", "?");
sql.bind_fields::<Unnamed>();
let err = sql.finish().unwrap_err();
assert!(err
.to_string()
.contains("argument ?fields cannot be used with non-struct row types"));

let mut sql = SqlBuilder::new("SELECT a FROM test WHERE b = ? AND c = ?");
let mut sql = SqlBuilder::new("SELECT a FROM test WHERE b = ? AND c = ?", "?");
sql.bind_arg(42);
let err = sql.finish().unwrap_err();
assert!(err.to_string().contains("unbound query argument"));

let mut sql = SqlBuilder::new("SELECT ?fields FROM test WHERE b = ?");
let mut sql = SqlBuilder::new("SELECT ?fields FROM test WHERE b = ?", "?");
sql.bind_arg(42);
let err = sql.finish().unwrap_err();
assert!(err.to_string().contains("unbound query argument ?fields"));
Expand Down
Loading