Implement from and distinct methods

This commit is contained in:
Timothy Warren 2019-04-11 11:44:06 -04:00
parent 6281b671ec
commit 35c0683891
5 changed files with 33 additions and 9 deletions

View File

@ -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']

View File

@ -49,11 +49,11 @@ pub trait DatabaseDriver: fmt::Debug {
}
/// Vector version of `quote_identifier`
fn quote_identifiers(&self, identifiers: Vec<&str>) -> Vec<String> {
fn quote_identifiers(&self, identifiers: Vec<String>) -> Vec<String> {
let mut output: Vec<String> = 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(),

View File

@ -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(),

View File

@ -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(),

View File

@ -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::<Vec<String>>();
self.state.from_string = ident_vec.join(" ");
self
}