From 35c06838910e5df29133d17e3e8ead2d0a69686b Mon Sep 17 00:00:00 2001 From: Timothy Warren Date: Thu, 11 Apr 2019 11:44:06 -0400 Subject: [PATCH] Implement from and distinct methods --- Cargo.toml | 7 ++++++- src/drivers.rs | 10 +++++++--- src/drivers/mssql.rs | 6 +++++- src/drivers/mysql.rs | 6 +++++- src/query_builder.rs | 13 ++++++++++--- 5 files changed, 33 insertions(+), 9 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 079f3e0..65c087c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -26,9 +26,14 @@ version="15.1.0" optional=true package="mysql" +[dependencies.ms] +version="0.3.2" +optional=true +package="tiberius" + [features] default=['postgres'] postgres=['pg'] sqlite=['slite'] mysql=['my'] -mssql=[] +mssql=['ms'] diff --git a/src/drivers.rs b/src/drivers.rs index bad0221..1ca3563 100644 --- a/src/drivers.rs +++ b/src/drivers.rs @@ -49,11 +49,11 @@ pub trait DatabaseDriver: fmt::Debug { } /// Vector version of `quote_identifier` - fn quote_identifiers(&self, identifiers: Vec<&str>) -> Vec { + fn quote_identifiers(&self, identifiers: Vec) -> Vec { let mut output: Vec = vec![]; for identifier in identifiers { - output.push(self.quote_identifier(identifier).to_string()); + output.push(self.quote_identifier(&identifier).to_string()); } output @@ -120,7 +120,11 @@ mod tests { let driver = DefaultDriver::new(); assert_eq!( - driver.quote_identifiers(vec!["\tfoo. bar", "baz", "fizz.\n\tbuzz.baz",]), + driver.quote_identifiers(vec![ + "\tfoo. bar".to_string(), + "baz".to_string(), + "fizz.\n\tbuzz.baz".to_string(), + ]), vec![ r#""foo"."bar""#.to_string(), r#""baz""#.to_string(), diff --git a/src/drivers/mssql.rs b/src/drivers/mssql.rs index c7d4ab0..800045a 100644 --- a/src/drivers/mssql.rs +++ b/src/drivers/mssql.rs @@ -45,7 +45,11 @@ mod tests { let driver = MSSQL::new(); assert_eq!( - driver.quote_identifiers(vec!["\tfoo. bar", "baz", "fizz.\n\tbuzz.baz",]), + driver.quote_identifiers(vec![ + "\tfoo. bar".to_string(), + "baz".to_string(), + "fizz.\n\tbuzz.baz".to_string(), + ]), vec![ "[foo].[bar]".to_string(), "[baz]".to_string(), diff --git a/src/drivers/mysql.rs b/src/drivers/mysql.rs index c10a880..faeb3e5 100644 --- a/src/drivers/mysql.rs +++ b/src/drivers/mysql.rs @@ -45,7 +45,11 @@ mod tests { let driver = MySQL::new(); assert_eq!( - driver.quote_identifiers(vec!["\tfoo. bar", "baz", "fizz.\n\tbuzz.baz",]), + driver.quote_identifiers(vec![ + "\tfoo. bar".to_string(), + "baz".to_string(), + "fizz.\n\tbuzz.baz".to_string(), + ]), vec![ "`foo`.`bar`".to_string(), "`baz`".to_string(), diff --git a/src/query_builder.rs b/src/query_builder.rs index d9bec8b..dbf2fd6 100644 --- a/src/query_builder.rs +++ b/src/query_builder.rs @@ -232,13 +232,20 @@ impl QueryBuilder { /// Adds the `distinct` keyword to a query pub fn distinct(&mut self) -> &mut Self { - unimplemented!(); + self.state.select_string = String::from(" DISTINCT") + &self.state.select_string; + + self } /// Specify the database table to select from pub fn from(&mut self, table_name: &str) -> &mut Self { - // @TODO properly escape the table name - self.state.from_string = table_name.to_string(); + let ident_vec = String::from(table_name) + .split(" ") + .into_iter() + .map(|s| self.driver.quote_identifier(s)) + .collect::>(); + + self.state.from_string = ident_vec.join(" "); self }