more ugly progress

This commit is contained in:
Timothy Warren 2019-04-02 17:23:52 -04:00
parent d77e25b800
commit ff5a9354c5
1 changed files with 56 additions and 9 deletions

View File

@ -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<String>,
set_array_keys: Vec<String>,
// Order by clause
order_array: HashMap<String, String>,
@ -42,10 +42,10 @@ struct QueryState {
group_array: HashMap<String, String>,
// Values to apply to prepared statements
pub values: Vec<Box<dyn Any>>,
values: Vec<Box<dyn Any>>,
// Values to apply to where clauses in prepared statements
pub where_values: Vec<Box<dyn Any>>,
where_values: Vec<Box<dyn Any>>,
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<dyn Any>) -> Self {
self.state.set_array_keys.push(key);
pub fn set(mut self, key: &str, value: Box<dyn Any>) -> 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<String, Box<dyn Any>>) -> 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<dyn Any> {
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::<String>());
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);
// }
}