rename helper method, simplify more logic, formatting

This commit is contained in:
Timothy Warren 2019-04-09 17:33:26 -04:00
parent 66d158d9c1
commit 78117b1345
6 changed files with 41 additions and 47 deletions

View File

@ -1,7 +1,7 @@
//! Drivers
//!
//! Drivers represent a connection to a specific type of database engine
use crate::split_join_map;
use crate::split_map_join;
use std::fmt;
#[cfg(feature = "postgres")]
@ -66,22 +66,16 @@ pub trait DatabaseDriver: fmt::Debug {
// If the identifier is actually a comma-separated list,
// recurse to quote each identifier in the list
if identifier.contains(",") {
let mut quoted_parts: Vec<String> = vec![];
for part in identifier.split(",") {
let new_part = part.trim();
let new_part = &self.quote_identifier(new_part);
quoted_parts.push(new_part.to_owned());
}
let quoted = split_map_join(identifier, ",", |part| self.quote_identifier(part.trim()));
// This was the only way I could figure to get
// around mutable string reference scope hell
identifier.replace_range(.., &mut quoted_parts.join(","));
identifier.replace_range(.., &quoted);
}
let (open_char, close_char) = self._quotes();
let trimmed_tiers = split_join_map(identifier, ".", |tier| {
let trimmed_tiers = split_map_join(identifier, ".", |tier| {
let tier = tier.trim();
// Here where the quoting actually happens. Everything

View File

@ -9,4 +9,4 @@ impl Postgres {
}
}
impl DatabaseDriver for Postgres { }
impl DatabaseDriver for Postgres {}

View File

@ -9,4 +9,4 @@ impl SQLite {
}
}
impl DatabaseDriver for SQLite { }
impl DatabaseDriver for SQLite {}

View File

@ -7,12 +7,13 @@ pub mod drivers;
pub mod query_builder;
pub mod types;
pub fn split_join_map<'a>(
pub fn split_map_join<'a>(
string: &'a str,
split_join_by: &str,
map_fn: impl (FnMut(&'a str) -> String)
map_fn: impl (FnMut(&'a str) -> String),
) -> String {
string.split(split_join_by)
string
.split(split_join_by)
.into_iter()
.map(map_fn)
.collect::<Vec<String>>()
@ -24,12 +25,12 @@ mod tests {
use super::*;
#[test]
fn test_split_join_map() {
fn test_split_map_join() {
let start = "a\t,b ,c\n,d";
let expected = "a,b,c,d";
assert_eq!(
split_join_map(start, ",", |s| s.trim().to_string()),
split_map_join(start, ",", |s| s.trim().to_string()),
expected
);
}

View File

@ -1,7 +1,7 @@
//! This main file is just for temparary testing
use stringqb::drivers::postgres::Postgres;
use stringqb::query_builder::QueryBuilder;
use stringqb::types::{SQLType, Type};
use stringqb::drivers::postgres::Postgres;
fn main() {
let mut qb = QueryBuilder::new(Postgres::new());

View File

@ -4,7 +4,7 @@
use std::any::Any;
use std::collections::HashMap;
use crate::drivers::{ DatabaseDriver, DefaultDriver };
use crate::drivers::{DatabaseDriver, DefaultDriver};
/// The position of the wildcard(s)
/// for a `like` clause
@ -135,13 +135,13 @@ impl QueryState {
QueryState::default()
}
pub fn append_select_string(&mut self, s:&str) -> &mut Self {
pub fn append_select_string(&mut self, s: &str) -> &mut Self {
self.select_string += s;
self
}
pub fn set_from_string(&mut self, s:&str) -> &mut Self {
pub fn set_from_string(&mut self, s: &str) -> &mut Self {
self.from_string = s.to_owned();
self
@ -330,7 +330,14 @@ impl QueryBuilder {
}
/// Add a table join to the query
pub fn join(&mut self, table: &str, col: &str, op: &str, value: &str, join_type: JoinType) -> &mut Self {
pub fn join(
&mut self,
table: &str,
col: &str,
op: &str,
value: &str,
join_type: JoinType,
) -> &mut Self {
let table = self.driver.quote_identifier(table);
let col = self.driver.quote_identifier(col);
let condition = table + " ON " + &col + op + value;
@ -349,7 +356,7 @@ impl QueryBuilder {
self.state.query_map.push(QueryClause::new(
QueryClauseType::Join,
&conjunction,
&condition
&condition,
));
self
@ -418,16 +425,14 @@ impl QueryBuilder {
/// Start a logical grouping in the current query
pub fn group_start(&mut self) -> &mut Self {
if self.state.query_map.len() == 0 {
self.state.query_map.push(QueryClause::new(
QueryClauseType::GroupStart,
" ",
"("
));
self.state
.query_map
.push(QueryClause::new(QueryClauseType::GroupStart, " ", "("));
} else {
self.state.query_map.push(QueryClause::new(
QueryClauseType::GroupStart,
" WHERE ",
"("
"(",
));
}
@ -440,14 +445,12 @@ impl QueryBuilder {
self.state.query_map.push(QueryClause::new(
QueryClauseType::GroupStart,
" WHERE ",
"("
"(",
));
} else {
self.state.query_map.push(QueryClause::new(
QueryClauseType::GroupStart,
" AND ",
"("
));
self.state
.query_map
.push(QueryClause::new(QueryClauseType::GroupStart, " AND ", "("));
}
self
@ -455,11 +458,9 @@ impl QueryBuilder {
/// Start a logical grouping, prefixed with `or`
pub fn or_group_start(&mut self) -> &mut Self {
self.state.query_map.push(QueryClause::new(
QueryClauseType::GroupStart,
"",
" OR ("
));
self.state
.query_map
.push(QueryClause::new(QueryClauseType::GroupStart, "", " OR ("));
self
}
@ -469,7 +470,7 @@ impl QueryBuilder {
self.state.query_map.push(QueryClause::new(
QueryClauseType::GroupStart,
"",
" OR NOT ("
" OR NOT (",
));
self
@ -477,11 +478,9 @@ impl QueryBuilder {
/// End the current logical grouping
pub fn group_end(&mut self) -> &mut Self {
self.state.query_map.push(QueryClause::new(
QueryClauseType::GroupEnd,
"",
")"
));
self.state
.query_map
.push(QueryClause::new(QueryClauseType::GroupEnd, "", ")"));
self
}