Test and fix where method

This commit is contained in:
Timothy Warren 2019-04-17 14:10:45 -04:00
parent 1e7644108b
commit 4262b5b1c0
3 changed files with 32 additions and 9 deletions

View File

@ -8,10 +8,6 @@
//! //!
//! ``` //! ```
#![warn(missing_docs)] #![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] #[macro_use]
extern crate lazy_static; extern crate lazy_static;

View File

@ -656,7 +656,7 @@ impl QueryBuilder {
let keys = self._where(key, vec![value]); let keys = self._where(key, vec![value]);
for k in keys { for k in keys {
self._where_string_key(key, conj); self._where_string_key(&k, conj);
} }
self self
@ -664,9 +664,8 @@ impl QueryBuilder {
fn _where_string_key(&mut self, key: &str, conj: &str) { fn _where_string_key(&mut self, key: &str, conj: &str) {
let field = key.trim().split(" ").collect::<Vec<&str>>(); let field = key.trim().split(" ").collect::<Vec<&str>>();
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 mut item = self.driver.quote_identifier(field[0]);
let item2 = if field.len() == 1 { let item2 = if field.len() == 1 {
@ -677,8 +676,13 @@ impl QueryBuilder {
item += &item2; item += &item2;
let conj = if last_item.clause_type == QueryClauseType::GroupStart { let conj = if last_item.is_some() {
String::from("") let last_item = last_item.unwrap();
match last_item.clause_type {
QueryClauseType::GroupStart => String::from(""),
_ => format!(" {} ", conj)
}
} else { } else {
format!(" {} ", conj) format!(" {} ", conj)
}; };
@ -933,6 +937,16 @@ impl QueryState {
&self.query_map &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 { pub fn get_select_string(&self) -> &str {
&self.select_string &self.select_string
} }

View File

@ -47,6 +47,19 @@ fn select_without_from() {
qb.get_compiled_select(); 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] #[test]
fn select_where_in() { fn select_where_in() {
let mut qb = QueryBuilder::default(); let mut qb = QueryBuilder::default();