From 25da0ae57e32dbbc5ffb7a3f08687db286dec448 Mon Sep 17 00:00:00 2001 From: Timothy Warren Date: Fri, 19 Jul 2019 16:42:36 -0400 Subject: [PATCH] Ugly progress commit --- src/drivers/mysql.rs | 4 ++++ src/drivers/sqlite.rs | 27 +++++++++++++++------------ src/fns.rs | 17 +++++++++++++++++ src/query_builder.rs | 40 +++++++++++++++++++++++++++++++++++++--- 4 files changed, 73 insertions(+), 15 deletions(-) diff --git a/src/drivers/mysql.rs b/src/drivers/mysql.rs index 70bbe27..0d31474 100644 --- a/src/drivers/mysql.rs +++ b/src/drivers/mysql.rs @@ -44,6 +44,10 @@ impl DatabaseDriver for MySQLDriver { fn random(&self) -> String { String::from(" RAND() DESC") } + + fn query(&self, sql: &str) -> Result, Box> { + unimplemented!(); + } } #[cfg(test)] diff --git a/src/drivers/sqlite.rs b/src/drivers/sqlite.rs index a5819ef..a26b590 100644 --- a/src/drivers/sqlite.rs +++ b/src/drivers/sqlite.rs @@ -37,18 +37,6 @@ impl SQLiteDriver { self.connection = RefCell::new(Some(connection)); } - - pub fn query(&self, sql: &str) -> Result { - if self.connection.borrow().is_none() { - panic!("No database connection."); - } - - self.connection - .borrow_mut() - .as_mut() - .unwrap() - .execute(sql, NO_PARAMS) - } } impl DatabaseDriver for SQLiteDriver { @@ -59,4 +47,19 @@ impl DatabaseDriver for SQLiteDriver { fn random(&self) -> String { String::from(" RANDOM()") } + + fn query(&self, sql: &str) -> Result, Box> { + if self.connection.borrow().is_none() { + panic!("No database connection."); + } + + self.connection + .borrow_mut() + .as_mut() + .unwrap() + .execute(sql, NO_PARAMS); + + // TODO: map native result to generic result + unimplemented!(); + } } diff --git a/src/fns.rs b/src/fns.rs index 1ba1787..32ae6c8 100644 --- a/src/fns.rs +++ b/src/fns.rs @@ -1,4 +1,5 @@ //! Utility / Helper functions that don't really belong anywhere else +use std::any::Any; /// Split a string, apply a closure to each substring, /// then join the string back together @@ -23,5 +24,21 @@ pub fn split_map_join<'a>( .join(split_join_by) } +pub fn get_typed_ref<'a, T: 'static>(val: &'a Any) -> Option<&'a T> { + if ! val.is::() { + return None; + } + + val.downcast_ref::() +} + +pub fn get_typed_mut<'a, T: 'static>(val: &'a mut Any) -> Option<&'a mut T> { + if ! val.is::() { + return None; + } + + val.downcast_mut::() +} + #[cfg(test)] mod tests {} diff --git a/src/query_builder.rs b/src/query_builder.rs index 6834b43..f66807c 100644 --- a/src/query_builder.rs +++ b/src/query_builder.rs @@ -139,12 +139,12 @@ impl QueryBuilder { /// // Postgres Driver (If enabled) /// let pgDriver = PostgresDriver::new("postgres://"); /// - /// #[cfg(feature = "sqlite")] /// // SQLite Driver (memory) + /// #[cfg(feature = "sqlite")] /// let liteMemoryDriver = SQLiteDriver::new(":memory:"); /// - /// #[cfg(feature = "sqlite")] /// // SQLite Driver (file) + /// #[cfg(feature = "sqlite")] /// let liteDriver = SQLiteDriver::new("/path/to/db.sqlite3"); /// /// // The variable to the query builder must be mutable @@ -466,6 +466,20 @@ impl QueryBuilder { } /// Set a map of data for an insert or update query + /// + /// ```no_run + /// # use stringqb::prelude::*; + /// # let mut qb = QueryBuilder::default(); + /// use std::collections::HashMap; + /// + /// let mut authors = HashMap::new(); + /// authors.insert(String::from("Chinua Achebe"), String::from("Nigeria")); + /// authors.insert(String::from("Rabindranath Tagore"), String::from("India")); + /// authors.insert(String::from("Anita Nair"), String::from("India")); + /// + /// qb.set_map(authors) + /// .insert("authors"); + /// ``` pub fn set_map(&mut self, data: HashMap) -> &mut Self { for (key, value) in data { self.set(&key, value); @@ -783,7 +797,17 @@ impl QueryBuilder { } /// Quotes table column(s)/field(s) accounting for 'as' aliases - fn quote_fields(&mut self, fields: &str) -> String { + /// + /// ``` + /// # use stringqb::prelude::*; + /// # let mut qb = QueryBuilder::default(); + /// let fields = "a.b,c as cplus,d.a as x"; + /// let quoted_fields = qb.quote_fields(fields); + /// let expected = r#""a"."b","c" AS "cplus","d"."a" AS "x""#; + /// + /// assert_eq!(quoted_fields, expected); + /// ``` + pub fn quote_fields(&mut self, fields: &str) -> String { lazy_static! { static ref RE: Regex = Regex::new(r"(?i) as ").unwrap(); }; @@ -804,6 +828,16 @@ impl QueryBuilder { } /// Quotes table(s), accounting for aliases + /// + /// ``` + /// # use stringqb::prelude::*; + /// # let mut qb = QueryBuilder::default(); + /// let tables = "a,b.c,d dprime"; + /// let quoted_tables = qb.quote_table(tables); + /// let expected = r#""a","b"."c","d" "dprime""#; + /// + /// assert_eq!(quoted_tables, expected); + /// ``` pub fn quote_table(&mut self, table: &str) -> String { split_map_join(table, " ", |s| self.driver.quote_identifier(s)) }