From f11b55b73d896b86c0dff9f23247843cb982852c Mon Sep 17 00:00:00 2001 From: "Timothy J. Warren" Date: Fri, 5 Apr 2019 14:51:31 -0400 Subject: [PATCH] Rework driver specific stuff a bit --- src/drivers/mod.rs | 10 +++++++++- src/drivers/mysql.rs | 6 ++++-- src/drivers/postgres.rs | 4 ---- src/main.rs | 2 +- src/query_builder.rs | 15 +++++++++++++-- 5 files changed, 27 insertions(+), 10 deletions(-) diff --git a/src/drivers/mod.rs b/src/drivers/mod.rs index b3446c0..f88d913 100644 --- a/src/drivers/mod.rs +++ b/src/drivers/mod.rs @@ -29,6 +29,12 @@ struct DriverBase { /// /// Interface between the database connection library and the query builder pub trait DatabaseDriver: fmt::Debug { + /// Get which characters are used to delimit identifiers + /// such as tables, and columns + fn _quotes(&self) -> (char, char) { + ('"','"') + } + /// Vector version of `quote_identifier` fn quote_identifiers(&self, identifiers: Vec) -> Vec { let mut output: Vec = vec![]; @@ -42,7 +48,9 @@ pub trait DatabaseDriver: fmt::Debug { /// Quote the identifiers passed, so the database does not /// normalize the identifiers (eg, table, column, etc.) - fn quote_identifier(&self, identifier: &str) -> String; + fn quote_identifier(&self, identifier: &str) -> String { + identifier.to_string() + } /// Runs a basic sql query on the database fn query(&self, query: &str) -> Result<(), ()>; diff --git a/src/drivers/mysql.rs b/src/drivers/mysql.rs index b2873d7..a2d7c60 100644 --- a/src/drivers/mysql.rs +++ b/src/drivers/mysql.rs @@ -4,8 +4,10 @@ use super::*; pub struct MySQL; impl DatabaseDriver for MySQL { - fn quote_identifier(&self, identifier: &str) -> String { - String::from(identifier) + /// Get which characters are used to delimit identifiers + /// such as tables, and columns + fn _quotes(&self) -> (char, char) { + ('`','`') } fn query(&self, _query: &str) -> Result<(), ()> { diff --git a/src/drivers/postgres.rs b/src/drivers/postgres.rs index 7e54f2a..6eeedc6 100644 --- a/src/drivers/postgres.rs +++ b/src/drivers/postgres.rs @@ -4,10 +4,6 @@ use super::*; pub struct Postgres; impl DatabaseDriver for Postgres { - 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 index c2958fd..d6116ef 100644 --- a/src/main.rs +++ b/src/main.rs @@ -12,7 +12,7 @@ fn main() { .set("buzz", Box::new((1, 2.0, true, 'q'))); // This just makes me sad - qb.r#where("foo", Box::new(2)); + qb.r#where("foo", "<>", Box::new(2)); println!("QueryBuilder object: {:#?}", &qb); diff --git a/src/query_builder.rs b/src/query_builder.rs index 8ea89d9..192ac7a 100644 --- a/src/query_builder.rs +++ b/src/query_builder.rs @@ -155,10 +155,16 @@ impl QueryBuilder { // ! Select Queries // -------------------------------------------------------------------------- - /// Set the fields to select from the database + /// Set the fields to select from the database as a string pub fn select(&mut self, fields: &str) -> &mut Self { unimplemented!(); } + + /// Set the fields to select from the database as a Vector + pub fn select_vec(&mut self, fields: Vec<&str>) -> &mut Self { + let fields = fields.join(","); + self.select(&fields) + } /// Adds the `distinct` keyword to a query pub fn distinct(&mut self) -> &mut Self { @@ -231,12 +237,17 @@ impl QueryBuilder { // -------------------------------------------------------------------------- /// Specify a condition for the `where` clause of the query - pub fn r#where(&mut self, key: &str, value: Box) -> &mut Self { + pub fn r#where(&mut self, key: &str, op: &str, value: Box) -> &mut Self { // @TODO actually implement setting the keys for the where self.state.where_values.push(value); self } + + // Specify a condition for a `where` clause where a column has a value + pub fn where_eq(&mut self, key: &str, value: Box) -> &mut Self { + self.r#where(key, "=", value) + } /// Specify a condition for the `where` clause of the query, prefixed with `or` pub fn or_where(&mut self, key: &str, value: Box) -> &mut Self {