From 4262b5b1c0c19ee46398380e911f69b6c64ba186 Mon Sep 17 00:00:00 2001 From: Timothy Warren Date: Wed, 17 Apr 2019 14:10:45 -0400 Subject: [PATCH] Test and fix where method --- src/lib.rs | 4 ---- src/query_builder.rs | 24 +++++++++++++++++++----- tests/integration_test.rs | 13 +++++++++++++ 3 files changed, 32 insertions(+), 9 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 44ef72b..d3f3ede 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -8,10 +8,6 @@ //! //! ``` #![warn(missing_docs)] -// Temporarily silence unused variables and uncalled code warnings -// @TODO remove when most of the code is implemented -#![allow(dead_code)] -#![allow(unused_variables)] #[macro_use] extern crate lazy_static; diff --git a/src/query_builder.rs b/src/query_builder.rs index a0e1385..9d505d9 100644 --- a/src/query_builder.rs +++ b/src/query_builder.rs @@ -656,7 +656,7 @@ impl QueryBuilder { let keys = self._where(key, vec![value]); for k in keys { - self._where_string_key(key, conj); + self._where_string_key(&k, conj); } self @@ -664,9 +664,8 @@ impl QueryBuilder { fn _where_string_key(&mut self, key: &str, conj: &str) { let field = key.trim().split(" ").collect::>(); - let query_map = self.state.get_query_map(); - let last_item = &query_map[query_map.len() - 1]; + let last_item = self.state.get_query_map_last(); let mut item = self.driver.quote_identifier(field[0]); let item2 = if field.len() == 1 { @@ -677,8 +676,13 @@ impl QueryBuilder { item += &item2; - let conj = if last_item.clause_type == QueryClauseType::GroupStart { - String::from("") + let conj = if last_item.is_some() { + let last_item = last_item.unwrap(); + + match last_item.clause_type { + QueryClauseType::GroupStart => String::from(""), + _ => format!(" {} ", conj) + } } else { format!(" {} ", conj) }; @@ -933,6 +937,16 @@ impl QueryState { &self.query_map } + pub fn get_query_map_last(&self) -> Option<&QueryClause> { + if self.query_map.len() == 0 { + return None + } + + let index = self.query_map.len() - 1; + + Some(&self.query_map[index]) + } + pub fn get_select_string(&self) -> &str { &self.select_string } diff --git a/tests/integration_test.rs b/tests/integration_test.rs index 24abbb3..15892b5 100644 --- a/tests/integration_test.rs +++ b/tests/integration_test.rs @@ -47,6 +47,19 @@ fn select_without_from() { qb.get_compiled_select(); } +#[test] +fn select_where() { + let mut qb = QueryBuilder::default(); + + qb.from("test") + .r#where("foo", "bar"); + + let sql = qb.get_compiled_select(); + let expected = "SELECT *\nFROM \"test\" WHERE \"foo\"=?"; + + assert_eq!(sql, expected); +} + #[test] fn select_where_in() { let mut qb = QueryBuilder::default();