From f34a403584fa6a65549b98281ccfd3d027fce6ad Mon Sep 17 00:00:00 2001 From: Timothy Warren Date: Wed, 17 Apr 2019 09:09:31 -0400 Subject: [PATCH] Formatting fixes --- src/drivers.rs | 3 +- src/drivers/mysql.rs | 2 +- src/drivers/postgres.rs | 2 +- src/drivers/sqlite.rs | 3 +- src/fns.rs | 2 +- src/lib.rs | 2 +- src/query_builder.rs | 53 +++++++++++++++++++++++--------- src/query_builder/query_state.rs | 13 ++------ tests/integration_test.rs | 10 +++--- 9 files changed, 54 insertions(+), 36 deletions(-) diff --git a/src/drivers.rs b/src/drivers.rs index d035fa3..d1c6129 100644 --- a/src/drivers.rs +++ b/src/drivers.rs @@ -39,7 +39,7 @@ impl DefaultDriver { impl DatabaseDriver for DefaultDriver { fn explain(&self, sql: &str) -> String { - return format!("EXPLAIN {}", sql) + format!("EXPLAIN {}", sql) } fn random(&self) -> String { @@ -105,7 +105,6 @@ pub trait DatabaseDriver: fmt::Debug { // Runs a basic sql query on the database // fn query(&self, query: &str) -> Result<(), ()>; - // ------------------------------------------------------------------------ // ! Driver-specific SQL methods // ------------------------------------------------------------------------ diff --git a/src/drivers/mysql.rs b/src/drivers/mysql.rs index 59ecb06..1850c3c 100644 --- a/src/drivers/mysql.rs +++ b/src/drivers/mysql.rs @@ -36,7 +36,7 @@ impl DatabaseDriver for MySQLDriver { } fn explain(&self, sql: &str) -> String { - return format!("EXPLAIN EXTENDED {}", sql) + format!("EXPLAIN EXTENDED {}", sql) } fn random(&self) -> String { diff --git a/src/drivers/postgres.rs b/src/drivers/postgres.rs index 9c73515..5f9ceef 100644 --- a/src/drivers/postgres.rs +++ b/src/drivers/postgres.rs @@ -16,7 +16,7 @@ impl PostgresDriver { impl DatabaseDriver for PostgresDriver { fn explain(&self, sql: &str) -> String { - return format!("EXPLAIN VERBOSE {}", sql) + format!("EXPLAIN VERBOSE {}", sql) } fn random(&self) -> String { diff --git a/src/drivers/sqlite.rs b/src/drivers/sqlite.rs index 735e002..b7cf610 100644 --- a/src/drivers/sqlite.rs +++ b/src/drivers/sqlite.rs @@ -16,11 +16,10 @@ impl SQLiteDriver { impl DatabaseDriver for SQLiteDriver { fn explain(&self, sql: &str) -> String { - return format!("EXPLAIN QUERY PLAN {}", sql) + format!("EXPLAIN QUERY PLAN {}", sql) } fn random(&self) -> String { String::from(" RANDOM()") } } - diff --git a/src/fns.rs b/src/fns.rs index c2aa058..1ba1787 100644 --- a/src/fns.rs +++ b/src/fns.rs @@ -24,4 +24,4 @@ pub fn split_map_join<'a>( } #[cfg(test)] -mod tests {} \ No newline at end of file +mod tests {} diff --git a/src/lib.rs b/src/lib.rs index 161d541..b2c4c69 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -20,8 +20,8 @@ pub mod prelude { //! //! This includes enum types, traits, //! the Query Builder, and individual database drivers. - pub use crate::enums::*; pub use crate::drivers::DatabaseDriver; + pub use crate::enums::*; pub use crate::query_builder::QueryBuilder; #[cfg(feature = "postgres")] diff --git a/src/query_builder.rs b/src/query_builder.rs index 1a5bd1c..181cafd 100644 --- a/src/query_builder.rs +++ b/src/query_builder.rs @@ -10,8 +10,8 @@ use crate::drivers::{DatabaseDriver, DefaultDriver}; use crate::enums::*; use crate::fns::split_map_join; -use regex::Regex; use query_state::QueryState; +use regex::Regex; #[derive(Debug)] enum QueryType { @@ -86,7 +86,7 @@ impl QueryBuilder { }; let fields = split_map_join(fields, ",", |s| { - if ! RE.is_match(s) { + if !RE.is_match(s) { return self.driver.quote_identifier(s.trim()); } @@ -171,17 +171,32 @@ impl QueryBuilder { } /// Generates an OR Like clause - pub fn or_like(&mut self, field: &str, value: Box, position: LikeWildcard) -> &mut Self { + pub fn or_like( + &mut self, + field: &str, + value: Box, + position: LikeWildcard, + ) -> &mut Self { self._like(field, value, position, "LIKE", "OR") } /// Generates a NOI Like clause - pub fn not_like(&mut self, field: &str, value: Box, position: LikeWildcard) -> &mut Self { + pub fn not_like( + &mut self, + field: &str, + value: Box, + position: LikeWildcard, + ) -> &mut Self { self._like(field, value, position, "NOT LIKE", "AND") } /// Generates an OR NOT Like clause - pub fn or_not_like(&mut self, field: &str, value: Box, position: LikeWildcard) -> &mut Self { + pub fn or_not_like( + &mut self, + field: &str, + value: Box, + position: LikeWildcard, + ) -> &mut Self { self._like(field, value, position, "NOT LIKE", "OR") } @@ -242,7 +257,7 @@ impl QueryBuilder { /// Specify a `where in` clause for the query pub fn where_in(&mut self, key: &str, values: Vec>) -> &mut Self { - self._where_in(key, values,"IN", "AND") + self._where_in(key, values, "IN", "AND") } /// Specify a `where in` clause for the query, prefixed with `or` @@ -540,7 +555,7 @@ impl QueryBuilder { self } - fn _having_key(&mut self, key: &str, conj:&str) -> &mut Self { + fn _having_key(&mut self, key: &str, conj: &str) -> &mut Self { let field = key.trim().split(" ").collect::>(); let mut item = self.driver.quote_identifier(field[0]);; @@ -566,7 +581,13 @@ impl QueryBuilder { vec![String::from(key)] } - fn _where_in(&mut self, key: &str, values: Vec>, in_str: &str, conj: &str) -> &mut Self { + fn _where_in( + &mut self, + key: &str, + values: Vec>, + in_str: &str, + conj: &str, + ) -> &mut Self { let key = self.driver.quote_identifier(key); let placeholders = vec!["?"; values.len()]; @@ -576,7 +597,8 @@ impl QueryBuilder { let str = format!("{} {} ({}) ", key, in_str, placeholders.join(",")); - self.state.append_query_map(QueryClauseType::WhereIn, conj, &str); + self.state + .append_query_map(QueryClauseType::WhereIn, conj, &str); self } @@ -612,7 +634,8 @@ impl QueryBuilder { format!(" {} ", conj) }; - self.state.append_query_map(QueryClauseType::Where, &conj, &item); + self.state + .append_query_map(QueryClauseType::Where, &conj, &item); } fn compile(&self, query_type: QueryType, table: &str) -> String { @@ -727,15 +750,17 @@ mod tests { fn set_where_in() { let mut qb = QueryBuilder::default(); - qb.from("test") - .where_in("foo", vec![ + qb.from("test").where_in( + "foo", + vec![ Box::new(0), Box::new(1), Box::new(2), Box::new(3), Box::new(4), - Box::new(5) - ]); + Box::new(5), + ], + ); let sql = qb.get_compiled_select(); let expected = "SELECT *\nFROM \"test\" WHERE \"foo\" IN (?,?,?,?,?,?) "; diff --git a/src/query_builder/query_state.rs b/src/query_builder/query_state.rs index 20bb086..d8e1f21 100644 --- a/src/query_builder/query_state.rs +++ b/src/query_builder/query_state.rs @@ -106,22 +106,15 @@ impl QueryState { self } - pub fn append_having_map( - &mut self, - conj: &str, - s: &str, - ) -> &mut Self { + pub fn append_having_map(&mut self, conj: &str, s: &str) -> &mut Self { let conj = if self.having_map.len() == 0 { String::from(" HAVING ") } else { format!(" {} ", conj) }; - self.having_map.push(QueryClause::new( - QueryClauseType::Having, - &conj, - s - )); + self.having_map + .push(QueryClause::new(QueryClauseType::Having, &conj, s)); self } diff --git a/tests/integration_test.rs b/tests/integration_test.rs index b838866..ffaa23d 100644 --- a/tests/integration_test.rs +++ b/tests/integration_test.rs @@ -51,15 +51,17 @@ fn select_without_from() { fn select_where_in() { let mut qb = QueryBuilder::default(); - qb.from("test") - .where_in("foo", vec![ + qb.from("test").where_in( + "foo", + vec![ Box::new(0), Box::new(1), Box::new(2), Box::new(3), Box::new(4), - Box::new(5) - ]); + Box::new(5), + ], + ); let sql = qb.get_compiled_select(); let expected = "SELECT *\nFROM \"test\" WHERE \"foo\" IN (?,?,?,?,?,?) ";