From ff5a9354c5655a3fac1a0d2a4769a93f8b670a7e Mon Sep 17 00:00:00 2001 From: Timothy Warren Date: Tue, 2 Apr 2019 17:23:52 -0400 Subject: [PATCH] more ugly progress --- src/query_builder.rs | 65 ++++++++++++++++++++++++++++++++++++++------ 1 file changed, 56 insertions(+), 9 deletions(-) diff --git a/src/query_builder.rs b/src/query_builder.rs index d4c1aa0..b0caefc 100644 --- a/src/query_builder.rs +++ b/src/query_builder.rs @@ -1,7 +1,7 @@ //! QueryBuilder //! //! The QueryBuilder creates sql queries from chained methods -use std::any::{Any, TypeId}; +use std::any::Any; use std::collections::HashMap; use crate::drivers::DatabaseDriver; @@ -33,7 +33,7 @@ struct QueryState { group_string: String, // Keys for insert/update statement - pub set_array_keys: Vec, + set_array_keys: Vec, // Order by clause order_array: HashMap, @@ -42,10 +42,10 @@ struct QueryState { group_array: HashMap, // Values to apply to prepared statements - pub values: Vec>, + values: Vec>, // Values to apply to where clauses in prepared statements - pub where_values: Vec>, + where_values: Vec>, limit: u32, @@ -80,13 +80,41 @@ impl QueryBuilder { } } + /// Set the fields to select from the database + pub fn select(mut self, fields: &str) -> Self { + unimplemented!(); + } + /// Set a key and value for an insert or update query - pub fn set(mut self, key: String, value: Box) -> Self { - self.state.set_array_keys.push(key); + pub fn set(mut self, key: &str, value: Box) -> Self { + // @TODO figure a way to make this easier to use + self.state.set_array_keys.push(key.to_string()); self.state.values.push(value); self } + + /// Set a map of data for an insert or update query + pub fn set_batch(mut self, data: HashMap>) -> Self { + for (key, value) in data { + self = self.set(&key, value); + } + + self + } + + /// Specify the database table to select from + pub fn from(mut self, table_name: &str) -> Self { + // @TODO properly escape the table name + self.state.from_string = table_name.to_string(); + + self + } + + /// Execute the built query + pub fn get(self) -> Box { + unimplemented!(); + } } #[cfg(test)] @@ -95,9 +123,28 @@ mod tests { #[test] fn set_key_value() { - let builder = QueryBuilder::new(); - let builder = builder.set("foo".to_string(), Box::new("bar".to_string())); + let builder = QueryBuilder::new() + .set("foo", Box::new("bar")); - assert!(builder.state.values[0].is::()); + assert!(builder.state.values[0].is::<&str>()); } + +// fn set_hashmap() { +// let qb = QueryBuilder::new(); +// +// let mut authors = HashMap::new(); +// authors.insert( +// String::from("Chinua Achebe"), +// Box::new("Nigeria")); +// authors.insert( +// String::from("Rabindranath Tagore"), +// Box::new("India")); +// authors.insert( +// String::from("Anita Nair"), +// Box::new("India")); +// +// let qb = qb.set_batch(authors); +// +// assert_eq!(qb.state.values.len(), 3); +// } } \ No newline at end of file