Ugly progress commit

This commit is contained in:
Timothy Warren 2019-07-19 16:42:36 -04:00
parent 1ec5b3d9ee
commit 25da0ae57e
4 changed files with 73 additions and 15 deletions

View File

@ -44,6 +44,10 @@ impl DatabaseDriver for MySQLDriver {
fn random(&self) -> String { fn random(&self) -> String {
String::from(" RAND() DESC") String::from(" RAND() DESC")
} }
fn query(&self, sql: &str) -> Result<Box<dyn Any>, Box<dyn Any>> {
unimplemented!();
}
} }
#[cfg(test)] #[cfg(test)]

View File

@ -37,18 +37,6 @@ impl SQLiteDriver {
self.connection = RefCell::new(Some(connection)); self.connection = RefCell::new(Some(connection));
} }
pub fn query(&self, sql: &str) -> Result<usize> {
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 { impl DatabaseDriver for SQLiteDriver {
@ -59,4 +47,19 @@ impl DatabaseDriver for SQLiteDriver {
fn random(&self) -> String { fn random(&self) -> String {
String::from(" RANDOM()") String::from(" RANDOM()")
} }
fn query(&self, sql: &str) -> Result<Box<dyn Any>, Box<dyn Any>> {
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!();
}
} }

View File

@ -1,4 +1,5 @@
//! Utility / Helper functions that don't really belong anywhere else //! Utility / Helper functions that don't really belong anywhere else
use std::any::Any;
/// Split a string, apply a closure to each substring, /// Split a string, apply a closure to each substring,
/// then join the string back together /// then join the string back together
@ -23,5 +24,21 @@ pub fn split_map_join<'a>(
.join(split_join_by) .join(split_join_by)
} }
pub fn get_typed_ref<'a, T: 'static>(val: &'a Any) -> Option<&'a T> {
if ! val.is::<T>() {
return None;
}
val.downcast_ref::<T>()
}
pub fn get_typed_mut<'a, T: 'static>(val: &'a mut Any) -> Option<&'a mut T> {
if ! val.is::<T>() {
return None;
}
val.downcast_mut::<T>()
}
#[cfg(test)] #[cfg(test)]
mod tests {} mod tests {}

View File

@ -139,12 +139,12 @@ impl QueryBuilder {
/// // Postgres Driver (If enabled) /// // Postgres Driver (If enabled)
/// let pgDriver = PostgresDriver::new("postgres://"); /// let pgDriver = PostgresDriver::new("postgres://");
/// ///
/// #[cfg(feature = "sqlite")]
/// // SQLite Driver (memory) /// // SQLite Driver (memory)
/// #[cfg(feature = "sqlite")]
/// let liteMemoryDriver = SQLiteDriver::new(":memory:"); /// let liteMemoryDriver = SQLiteDriver::new(":memory:");
/// ///
/// #[cfg(feature = "sqlite")]
/// // SQLite Driver (file) /// // SQLite Driver (file)
/// #[cfg(feature = "sqlite")]
/// let liteDriver = SQLiteDriver::new("/path/to/db.sqlite3"); /// let liteDriver = SQLiteDriver::new("/path/to/db.sqlite3");
/// ///
/// // The variable to the query builder must be mutable /// // 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 /// 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<String, impl Any>) -> &mut Self { pub fn set_map(&mut self, data: HashMap<String, impl Any>) -> &mut Self {
for (key, value) in data { for (key, value) in data {
self.set(&key, value); self.set(&key, value);
@ -783,7 +797,17 @@ impl QueryBuilder {
} }
/// Quotes table column(s)/field(s) accounting for 'as' aliases /// 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! { lazy_static! {
static ref RE: Regex = Regex::new(r"(?i) as ").unwrap(); static ref RE: Regex = Regex::new(r"(?i) as ").unwrap();
}; };
@ -804,6 +828,16 @@ impl QueryBuilder {
} }
/// Quotes table(s), accounting for aliases /// 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 { pub fn quote_table(&mut self, table: &str) -> String {
split_map_join(table, " ", |s| self.driver.quote_identifier(s)) split_map_join(table, " ", |s| self.driver.quote_identifier(s))
} }