From 80f85b6579ed56d3aff1b5f2f8e42fb053eda6e9 Mon Sep 17 00:00:00 2001 From: "Timothy J. Warren" Date: Wed, 3 Apr 2019 20:58:22 -0400 Subject: [PATCH] Some type experimentation --- Cargo.toml | 8 +++++++- src/drivers/mod.rs | 6 +++++- src/drivers/mysql.rs | 14 ++++++++++++++ src/main.rs | 22 ++++++++++++++++++++++ src/query_builder.rs | 7 +++++-- src/types.rs | 27 +++++++++++++++++++++++++++ 6 files changed, 80 insertions(+), 4 deletions(-) create mode 100644 src/drivers/mysql.rs create mode 100644 src/main.rs diff --git a/Cargo.toml b/Cargo.toml index aecc25e..5bde318 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -21,7 +21,13 @@ features=["chrono","serde_json","url"] optional=true package="rusqlite" +[dependencies.my] +version="15.1.0" +optional=true +package="mysql" + [features] default=['postgres'] postgres=['pg'] -sqlite=['slite'] \ No newline at end of file +sqlite=['slite'] +mysql=['my'] diff --git a/src/drivers/mod.rs b/src/drivers/mod.rs index bab0fb2..1ec9448 100644 --- a/src/drivers/mod.rs +++ b/src/drivers/mod.rs @@ -3,16 +3,20 @@ //! Drivers represent a connection to a specific type of database engine use std::fmt; -#[cfg(feature="pg")] +#[cfg(feature="postgres")] mod postgres; #[cfg(feature="sqlite")] mod sqlite; +#[cfg(feature="mysql")] +mod mysql; + #[derive(Debug)] struct Connection; +/// Result for a db query #[derive(Debug)] struct QueryResult; diff --git a/src/drivers/mysql.rs b/src/drivers/mysql.rs new file mode 100644 index 0000000..b2873d7 --- /dev/null +++ b/src/drivers/mysql.rs @@ -0,0 +1,14 @@ +use super::*; + +#[derive(Debug)] +pub struct MySQL; + +impl DatabaseDriver for MySQL { + fn quote_identifier(&self, identifier: &str) -> String { + String::from(identifier) + } + + fn query(&self, _query: &str) -> Result<(), ()> { + Ok(()) + } +} diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..5dc93e2 --- /dev/null +++ b/src/main.rs @@ -0,0 +1,22 @@ +//! This main file is just for temparary testing +use stringqb::query_builder::QueryBuilder; +use stringqb::types::{SQLType, Type}; + +fn main() { + let qb = QueryBuilder::new() + .set("foo", Box::new("bar")) + .set("bar", Box::new(12)) + .set("baz", Box::new(false)) + .set("fizz", Box::new(12.38)) + .set("buzz", Box::new((1, 2.0, true, 'q'))); + + + + // This just makes me sad + let qb = qb.r#where("foo", Box::new(2)); + + println!("QueryBuilder object: {:#?}", &qb); + + println!("SQLType mapping: {:#?}", SQLType::SmallInt(32)); + println!("Type: {:#?}", Type(Box::new(1234567890))); +} diff --git a/src/query_builder.rs b/src/query_builder.rs index de2ed77..6fc0f53 100644 --- a/src/query_builder.rs +++ b/src/query_builder.rs @@ -217,7 +217,10 @@ impl QueryBuilder { /// Specify a condition for the `where` clause of the query pub fn r#where(mut self, key: &str, value: Box) -> Self { - unimplemented!(); + // @TODO actually implement setting the keys for the where + self.state.where_values.push(value); + + self } /// Specify a condition for the `where` clause of the query, prefixed with `or` @@ -427,4 +430,4 @@ mod tests { assert_eq!(qb.state.set_array_keys.len(), 3); assert_eq!(qb.state.values.len(), 3); } -} \ No newline at end of file +} diff --git a/src/types.rs b/src/types.rs index 17266d6..69d266c 100644 --- a/src/types.rs +++ b/src/types.rs @@ -1 +1,28 @@ //! Shared Types for different Database drivers +use std::any::Any; + +#[derive(Debug)] +pub struct Type(pub Box); + +/// Enum struct for mapping between database and Rust types +#[derive(Clone, Copy, PartialEq, PartialOrd, Eq, Ord, Debug, Hash)] +pub enum SQLType { + Boolean(T), + SmallInt(T), + BigInt(T), + Text(T), + None, +} + +impl SQLType { + pub fn is_some(&self) -> bool { + match *self { + SQLType::None => false, + _ => true, + } + } + + pub fn is_none(&self) -> bool { + !self.is_some() + } +}