From fa4ab61e3bd01cc1e87328f4636682c186c38b48 Mon Sep 17 00:00:00 2001 From: Timothy Warren Date: Thu, 25 Apr 2019 11:57:18 -0400 Subject: [PATCH] More (minor) progress --- src/query_builder.rs | 91 ++++++++++++++++++++++++++------------------ src/types.rs | 15 ++++++++ 2 files changed, 68 insertions(+), 38 deletions(-) diff --git a/src/query_builder.rs b/src/query_builder.rs index 94ede04..7bd382d 100644 --- a/src/query_builder.rs +++ b/src/query_builder.rs @@ -491,8 +491,10 @@ impl QueryBuilder { // -------------------------------------------------------------------------- /// Execute the built query - pub fn get(self) { - unimplemented!(); + pub fn get(&mut self) { + let sql = self.get_compiled_select(); + + self.run(&sql) } /// Count all the rows in the specified database table @@ -502,19 +504,23 @@ impl QueryBuilder { /// Execute the generated insert query pub fn insert(&mut self, table: &str) { - // @TODO determine query result type - unimplemented!(); + let sql = self.get_compiled_insert(table); + + self.run(&sql) } /// Execute the generated update query pub fn update(&mut self, table: &str) { - // @TODO determine query result type - unimplemented!(); + let sql = self.get_compiled_update(table); + + self.run(&sql) } /// Execute the generated delete query pub fn delete(&mut self, table: &str) { - unimplemented!(); + let sql = self.get_compiled_delete(table); + + self.run(&sql) } // -------------------------------------------------------------------------- @@ -582,7 +588,6 @@ impl QueryBuilder { let value: Box = Box::new(value); let string_val = value.downcast_ref::().unwrap(); - // @TODO Properly parse types of `value` for string formatting let value = match position { LikeWildcard::Before => format!("%{}", string_val), LikeWildcard::After => format!("{}%s", string_val), @@ -757,6 +762,16 @@ impl QueryBuilder { QueryType::Delete => format!("DELETE FROM {}", table), } } + + fn run(&mut self, _sql: &str) { + let mut values: Vec> = vec![]; + values.append(self.state.get_values()); + values.append(self.state.get_where_values()); + + // @TODO determine query result type + // @TODO prepare/execute query, and return result + unimplemented!(); + } } #[derive(Debug)] @@ -767,7 +782,7 @@ struct QueryClause { } impl QueryClause { - pub fn new(clause_type: QueryClauseType, conjunction: &str, string: &str) -> Self { + fn new(clause_type: QueryClauseType, conjunction: &str, string: &str) -> Self { QueryClause { clause_type, conjunction: conjunction.to_string(), @@ -775,7 +790,7 @@ impl QueryClause { } } - pub fn to_string(&self) -> String { + fn to_string(&self) -> String { format!("{}{}", self.conjunction, self.string) } } @@ -843,29 +858,29 @@ impl Default for QueryState { } impl QueryState { - pub fn new() -> Self { + fn new() -> Self { QueryState::default() } - pub fn append_select_string(&mut self, s: &str) -> &mut Self { + fn append_select_string(&mut self, s: &str) -> &mut Self { self.select_string += s; self } - pub fn prepend_select_string(&mut self, s: &str) -> &mut Self { + fn prepend_select_string(&mut self, s: &str) -> &mut Self { self.select_string = String::from(s) + &self.select_string; self } - pub fn append_group_array(&mut self, field: &str) -> &mut Self { + fn append_group_array(&mut self, field: &str) -> &mut Self { self.group_array.push(String::from(field)); self } - pub fn append_having_map(&mut self, conj: &str, s: &str) -> &mut Self { + fn append_having_map(&mut self, conj: &str, s: &str) -> &mut Self { let conj = if self.having_map.len() == 0 { String::from(" HAVING ") } else { @@ -878,31 +893,31 @@ impl QueryState { self } - pub fn append_order_map(&mut self, key: &str, dir: &str) -> &mut Self { + fn append_order_map(&mut self, key: &str, dir: &str) -> &mut Self { self.order_map.insert(String::from(key), String::from(dir)); self } - pub fn append_set_array_keys(&mut self, key: &str) -> &mut Self { + fn append_set_array_keys(&mut self, key: &str) -> &mut Self { self.set_array_keys.push(key.to_string()); self } - pub fn append_values(&mut self, val: Box) -> &mut Self { + fn append_values(&mut self, val: Box) -> &mut Self { self.values.push(val); self } - pub fn append_where_values(&mut self, val: Box) -> &mut Self { + fn append_where_values(&mut self, val: Box) -> &mut Self { self.where_values.push(val); self } - pub fn append_query_map( + fn append_query_map( &mut self, clause_type: QueryClauseType, conj: &str, @@ -919,27 +934,27 @@ impl QueryState { self } - pub fn get_from_string(&self) -> &str { + fn get_from_string(&self) -> &str { &self.from_string } - pub fn get_group_array(&self) -> &Vec { + fn get_group_array(&self) -> &Vec { &self.group_array } - pub fn get_group_string(&self) -> &str { + fn get_group_string(&self) -> &str { &self.group_string } - pub fn get_having_map(&self) -> &Vec { + fn get_having_map(&self) -> &Vec { &self.having_map } - pub fn get_query_map(&self) -> &Vec { + fn get_query_map(&self) -> &Vec { &self.query_map } - pub fn get_query_map_last(&self) -> Option<&QueryClause> { + fn get_query_map_last(&self) -> Option<&QueryClause> { if self.query_map.len() == 0 { return None; } @@ -949,47 +964,47 @@ impl QueryState { Some(&self.query_map[index]) } - pub fn get_select_string(&self) -> &str { + fn get_select_string(&self) -> &str { &self.select_string } - pub fn get_set_array_keys(&self) -> &Vec { + fn get_set_array_keys(&self) -> &Vec { &self.set_array_keys } - pub fn get_set_string(&self) -> &str { + fn get_set_string(&self) -> &str { &self.set_string } - pub fn get_order_map(&self) -> &HashMap { + fn get_order_map(&self) -> &HashMap { &self.order_map } - pub fn get_order_string(&self) -> &str { + fn get_order_string(&self) -> &str { &self.order_string } - pub fn get_values(&self) -> &Vec> { - &self.values + fn get_values(&mut self) -> &mut Vec> { + &mut self.values } - pub fn get_where_values(&self) -> &Vec> { - &self.where_values + fn get_where_values(&mut self) -> &mut Vec> { + &mut self.where_values } - pub fn set_from_string(&mut self, s: &str) -> &mut Self { + fn set_from_string(&mut self, s: &str) -> &mut Self { self.from_string = String::from(s); self } - pub fn set_group_string(&mut self, s: &str) -> &mut Self { + fn set_group_string(&mut self, s: &str) -> &mut Self { self.group_string = String::from(s); self } - pub fn set_order_string(&mut self, order_string: &str) -> &mut Self { + fn set_order_string(&mut self, order_string: &str) -> &mut Self { self.order_string = String::from(order_string); self diff --git a/src/types.rs b/src/types.rs index 54b5b28..1d4fb5f 100644 --- a/src/types.rs +++ b/src/types.rs @@ -24,6 +24,21 @@ //! | `&str`/`String` | TEXT | //! | `&[u8]`/`Vec` | BLOB | //! +//! ## MySQL/MariaDB Type Mappings +//! | Rust type(s) | MySQL type(s) | +//! |-------------------------------|-----------------------------------------------| +//! | `i8` | signed TINYINT | +//! | `u8` | unsigned TINYINT | +//! | `i16` | signed SMALLINT | +//! | `u16` | unsigned SMALLINT | +//! | `i32` | signed MEDIUMINT, INT, INTEGER | +//! | `u32` | unsigned MEDIUMINT, INT, INTEGER | +//! | `i64` | signed BIGINT | +//! | `u64` | unsigned BIGINT | +//! | `&str`/`String` | VARCHAR, CHAR, TEXT | +//! | `&[u8]`/`Vec` | BINARY, VARBINARY, BLOB | +//! | `f32` | FLOAT | +//! | `f64` | DOUBLE, DOUBLE PRECISION | //! use std::any::Any; use std::borrow::Cow;