More (minor) progress

This commit is contained in:
Timothy Warren 2019-04-25 11:57:18 -04:00
parent b39e87698c
commit fa4ab61e3b
2 changed files with 68 additions and 38 deletions

View File

@ -491,8 +491,10 @@ impl QueryBuilder {
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
/// Execute the built query /// Execute the built query
pub fn get(self) { pub fn get(&mut self) {
unimplemented!(); let sql = self.get_compiled_select();
self.run(&sql)
} }
/// Count all the rows in the specified database table /// Count all the rows in the specified database table
@ -502,19 +504,23 @@ impl QueryBuilder {
/// Execute the generated insert query /// Execute the generated insert query
pub fn insert(&mut self, table: &str) { pub fn insert(&mut self, table: &str) {
// @TODO determine query result type let sql = self.get_compiled_insert(table);
unimplemented!();
self.run(&sql)
} }
/// Execute the generated update query /// Execute the generated update query
pub fn update(&mut self, table: &str) { pub fn update(&mut self, table: &str) {
// @TODO determine query result type let sql = self.get_compiled_update(table);
unimplemented!();
self.run(&sql)
} }
/// Execute the generated delete query /// Execute the generated delete query
pub fn delete(&mut self, table: &str) { 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<dyn Any + 'static> = Box::new(value); let value: Box<dyn Any + 'static> = Box::new(value);
let string_val = value.downcast_ref::<String>().unwrap(); let string_val = value.downcast_ref::<String>().unwrap();
// @TODO Properly parse types of `value` for string formatting
let value = match position { let value = match position {
LikeWildcard::Before => format!("%{}", string_val), LikeWildcard::Before => format!("%{}", string_val),
LikeWildcard::After => format!("{}%s", string_val), LikeWildcard::After => format!("{}%s", string_val),
@ -757,6 +762,16 @@ impl QueryBuilder {
QueryType::Delete => format!("DELETE FROM {}", table), QueryType::Delete => format!("DELETE FROM {}", table),
} }
} }
fn run(&mut self, _sql: &str) {
let mut values: Vec<Box<Any>> = 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)] #[derive(Debug)]
@ -767,7 +782,7 @@ struct QueryClause {
} }
impl 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 { QueryClause {
clause_type, clause_type,
conjunction: conjunction.to_string(), 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) format!("{}{}", self.conjunction, self.string)
} }
} }
@ -843,29 +858,29 @@ impl Default for QueryState {
} }
impl QueryState { impl QueryState {
pub fn new() -> Self { fn new() -> Self {
QueryState::default() 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.select_string += s;
self 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.select_string = String::from(s) + &self.select_string;
self 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.group_array.push(String::from(field));
self 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 { let conj = if self.having_map.len() == 0 {
String::from(" HAVING ") String::from(" HAVING ")
} else { } else {
@ -878,31 +893,31 @@ impl QueryState {
self 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.order_map.insert(String::from(key), String::from(dir));
self 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.set_array_keys.push(key.to_string());
self self
} }
pub fn append_values(&mut self, val: Box<Any>) -> &mut Self { fn append_values(&mut self, val: Box<Any>) -> &mut Self {
self.values.push(val); self.values.push(val);
self self
} }
pub fn append_where_values(&mut self, val: Box<Any>) -> &mut Self { fn append_where_values(&mut self, val: Box<Any>) -> &mut Self {
self.where_values.push(val); self.where_values.push(val);
self self
} }
pub fn append_query_map( fn append_query_map(
&mut self, &mut self,
clause_type: QueryClauseType, clause_type: QueryClauseType,
conj: &str, conj: &str,
@ -919,27 +934,27 @@ impl QueryState {
self self
} }
pub fn get_from_string(&self) -> &str { fn get_from_string(&self) -> &str {
&self.from_string &self.from_string
} }
pub fn get_group_array(&self) -> &Vec<String> { fn get_group_array(&self) -> &Vec<String> {
&self.group_array &self.group_array
} }
pub fn get_group_string(&self) -> &str { fn get_group_string(&self) -> &str {
&self.group_string &self.group_string
} }
pub fn get_having_map(&self) -> &Vec<QueryClause> { fn get_having_map(&self) -> &Vec<QueryClause> {
&self.having_map &self.having_map
} }
pub fn get_query_map(&self) -> &Vec<QueryClause> { fn get_query_map(&self) -> &Vec<QueryClause> {
&self.query_map &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 { if self.query_map.len() == 0 {
return None; return None;
} }
@ -949,47 +964,47 @@ impl QueryState {
Some(&self.query_map[index]) Some(&self.query_map[index])
} }
pub fn get_select_string(&self) -> &str { fn get_select_string(&self) -> &str {
&self.select_string &self.select_string
} }
pub fn get_set_array_keys(&self) -> &Vec<String> { fn get_set_array_keys(&self) -> &Vec<String> {
&self.set_array_keys &self.set_array_keys
} }
pub fn get_set_string(&self) -> &str { fn get_set_string(&self) -> &str {
&self.set_string &self.set_string
} }
pub fn get_order_map(&self) -> &HashMap<String, String> { fn get_order_map(&self) -> &HashMap<String, String> {
&self.order_map &self.order_map
} }
pub fn get_order_string(&self) -> &str { fn get_order_string(&self) -> &str {
&self.order_string &self.order_string
} }
pub fn get_values(&self) -> &Vec<Box<Any>> { fn get_values(&mut self) -> &mut Vec<Box<Any>> {
&self.values &mut self.values
} }
pub fn get_where_values(&self) -> &Vec<Box<Any>> { fn get_where_values(&mut self) -> &mut Vec<Box<Any>> {
&self.where_values &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.from_string = String::from(s);
self 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.group_string = String::from(s);
self 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.order_string = String::from(order_string);
self self

View File

@ -24,6 +24,21 @@
//! | `&str`/`String` | TEXT | //! | `&str`/`String` | TEXT |
//! | `&[u8]`/`Vec<u8>` | BLOB | //! | `&[u8]`/`Vec<u8>` | 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<u8>` | BINARY, VARBINARY, BLOB |
//! | `f32` | FLOAT |
//! | `f64` | DOUBLE, DOUBLE PRECISION |
//! //!
use std::any::Any; use std::any::Any;
use std::borrow::Cow; use std::borrow::Cow;