Formatting fixes

This commit is contained in:
Timothy Warren 2019-04-17 09:09:31 -04:00
parent 9e4b483b2b
commit f34a403584
9 changed files with 54 additions and 36 deletions

View File

@ -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
// ------------------------------------------------------------------------

View File

@ -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 {

View File

@ -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 {

View File

@ -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()")
}
}

View File

@ -24,4 +24,4 @@ pub fn split_map_join<'a>(
}
#[cfg(test)]
mod tests {}
mod tests {}

View File

@ -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")]

View File

@ -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<dyn Any>, position: LikeWildcard) -> &mut Self {
pub fn or_like(
&mut self,
field: &str,
value: Box<dyn Any>,
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<dyn Any>, position: LikeWildcard) -> &mut Self {
pub fn not_like(
&mut self,
field: &str,
value: Box<dyn Any>,
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<dyn Any>, position: LikeWildcard) -> &mut Self {
pub fn or_not_like(
&mut self,
field: &str,
value: Box<dyn Any>,
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<Box<dyn Any>>) -> &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::<Vec<&str>>();
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<Box<dyn Any>>, in_str: &str, conj: &str) -> &mut Self {
fn _where_in(
&mut self,
key: &str,
values: Vec<Box<dyn Any>>,
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 (?,?,?,?,?,?) ";

View File

@ -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
}

View File

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