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
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<dyn Any + 'static> = Box::new(value);
let string_val = value.downcast_ref::<String>().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<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)]
@ -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<Any>) -> &mut Self {
fn append_values(&mut self, val: Box<Any>) -> &mut Self {
self.values.push(val);
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
}
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<String> {
fn get_group_array(&self) -> &Vec<String> {
&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<QueryClause> {
fn get_having_map(&self) -> &Vec<QueryClause> {
&self.having_map
}
pub fn get_query_map(&self) -> &Vec<QueryClause> {
fn get_query_map(&self) -> &Vec<QueryClause> {
&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<String> {
fn get_set_array_keys(&self) -> &Vec<String> {
&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<String, String> {
fn get_order_map(&self) -> &HashMap<String, String> {
&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<Box<Any>> {
&self.values
fn get_values(&mut self) -> &mut Vec<Box<Any>> {
&mut self.values
}
pub fn get_where_values(&self) -> &Vec<Box<Any>> {
&self.where_values
fn get_where_values(&mut self) -> &mut Vec<Box<Any>> {
&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

View File

@ -24,6 +24,21 @@
//! | `&str`/`String` | TEXT |
//! | `&[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::borrow::Cow;