rename helper method, simplify more logic, formatting
This commit is contained in:
parent
66d158d9c1
commit
78117b1345
@ -1,7 +1,7 @@
|
|||||||
//! Drivers
|
//! Drivers
|
||||||
//!
|
//!
|
||||||
//! Drivers represent a connection to a specific type of database engine
|
//! Drivers represent a connection to a specific type of database engine
|
||||||
use crate::split_join_map;
|
use crate::split_map_join;
|
||||||
use std::fmt;
|
use std::fmt;
|
||||||
|
|
||||||
#[cfg(feature = "postgres")]
|
#[cfg(feature = "postgres")]
|
||||||
@ -66,22 +66,16 @@ pub trait DatabaseDriver: fmt::Debug {
|
|||||||
// If the identifier is actually a comma-separated list,
|
// If the identifier is actually a comma-separated list,
|
||||||
// recurse to quote each identifier in the list
|
// recurse to quote each identifier in the list
|
||||||
if identifier.contains(",") {
|
if identifier.contains(",") {
|
||||||
let mut quoted_parts: Vec<String> = vec![];
|
let quoted = split_map_join(identifier, ",", |part| self.quote_identifier(part.trim()));
|
||||||
|
|
||||||
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());
|
|
||||||
}
|
|
||||||
|
|
||||||
// This was the only way I could figure to get
|
// This was the only way I could figure to get
|
||||||
// around mutable string reference scope hell
|
// around mutable string reference scope hell
|
||||||
identifier.replace_range(.., &mut quoted_parts.join(","));
|
identifier.replace_range(.., "ed);
|
||||||
}
|
}
|
||||||
|
|
||||||
let (open_char, close_char) = self._quotes();
|
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();
|
let tier = tier.trim();
|
||||||
|
|
||||||
// Here where the quoting actually happens. Everything
|
// Here where the quoting actually happens. Everything
|
||||||
|
@ -9,4 +9,4 @@ impl Postgres {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl DatabaseDriver for Postgres { }
|
impl DatabaseDriver for Postgres {}
|
||||||
|
@ -9,4 +9,4 @@ impl SQLite {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl DatabaseDriver for SQLite { }
|
impl DatabaseDriver for SQLite {}
|
||||||
|
13
src/lib.rs
13
src/lib.rs
@ -7,12 +7,13 @@ pub mod drivers;
|
|||||||
pub mod query_builder;
|
pub mod query_builder;
|
||||||
pub mod types;
|
pub mod types;
|
||||||
|
|
||||||
pub fn split_join_map<'a>(
|
pub fn split_map_join<'a>(
|
||||||
string: &'a str,
|
string: &'a str,
|
||||||
split_join_by: &str,
|
split_join_by: &str,
|
||||||
map_fn: impl (FnMut(&'a str) -> String)
|
map_fn: impl (FnMut(&'a str) -> String),
|
||||||
) -> String {
|
) -> String {
|
||||||
string.split(split_join_by)
|
string
|
||||||
|
.split(split_join_by)
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.map(map_fn)
|
.map(map_fn)
|
||||||
.collect::<Vec<String>>()
|
.collect::<Vec<String>>()
|
||||||
@ -24,13 +25,13 @@ mod tests {
|
|||||||
use super::*;
|
use super::*;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_split_join_map() {
|
fn test_split_map_join() {
|
||||||
let start = "a\t,b ,c\n,d";
|
let start = "a\t,b ,c\n,d";
|
||||||
let expected = "a,b,c,d";
|
let expected = "a,b,c,d";
|
||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
split_join_map(start, ",", |s| s.trim().to_string()),
|
split_map_join(start, ",", |s| s.trim().to_string()),
|
||||||
expected
|
expected
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
//! This main file is just for temparary testing
|
//! This main file is just for temparary testing
|
||||||
|
use stringqb::drivers::postgres::Postgres;
|
||||||
use stringqb::query_builder::QueryBuilder;
|
use stringqb::query_builder::QueryBuilder;
|
||||||
use stringqb::types::{SQLType, Type};
|
use stringqb::types::{SQLType, Type};
|
||||||
use stringqb::drivers::postgres::Postgres;
|
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let mut qb = QueryBuilder::new(Postgres::new());
|
let mut qb = QueryBuilder::new(Postgres::new());
|
||||||
|
@ -4,7 +4,7 @@
|
|||||||
use std::any::Any;
|
use std::any::Any;
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
|
|
||||||
use crate::drivers::{ DatabaseDriver, DefaultDriver };
|
use crate::drivers::{DatabaseDriver, DefaultDriver};
|
||||||
|
|
||||||
/// The position of the wildcard(s)
|
/// The position of the wildcard(s)
|
||||||
/// for a `like` clause
|
/// for a `like` clause
|
||||||
@ -135,13 +135,13 @@ impl QueryState {
|
|||||||
QueryState::default()
|
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.select_string += s;
|
||||||
|
|
||||||
self
|
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.from_string = s.to_owned();
|
||||||
|
|
||||||
self
|
self
|
||||||
@ -330,7 +330,14 @@ impl QueryBuilder {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Add a table join to the query
|
/// 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 table = self.driver.quote_identifier(table);
|
||||||
let col = self.driver.quote_identifier(col);
|
let col = self.driver.quote_identifier(col);
|
||||||
let condition = table + " ON " + &col + op + value;
|
let condition = table + " ON " + &col + op + value;
|
||||||
@ -349,7 +356,7 @@ impl QueryBuilder {
|
|||||||
self.state.query_map.push(QueryClause::new(
|
self.state.query_map.push(QueryClause::new(
|
||||||
QueryClauseType::Join,
|
QueryClauseType::Join,
|
||||||
&conjunction,
|
&conjunction,
|
||||||
&condition
|
&condition,
|
||||||
));
|
));
|
||||||
|
|
||||||
self
|
self
|
||||||
@ -418,16 +425,14 @@ impl QueryBuilder {
|
|||||||
/// Start a logical grouping in the current query
|
/// Start a logical grouping in the current query
|
||||||
pub fn group_start(&mut self) -> &mut Self {
|
pub fn group_start(&mut self) -> &mut Self {
|
||||||
if self.state.query_map.len() == 0 {
|
if self.state.query_map.len() == 0 {
|
||||||
self.state.query_map.push(QueryClause::new(
|
self.state
|
||||||
QueryClauseType::GroupStart,
|
.query_map
|
||||||
" ",
|
.push(QueryClause::new(QueryClauseType::GroupStart, " ", "("));
|
||||||
"("
|
|
||||||
));
|
|
||||||
} else {
|
} else {
|
||||||
self.state.query_map.push(QueryClause::new(
|
self.state.query_map.push(QueryClause::new(
|
||||||
QueryClauseType::GroupStart,
|
QueryClauseType::GroupStart,
|
||||||
" WHERE ",
|
" WHERE ",
|
||||||
"("
|
"(",
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -440,14 +445,12 @@ impl QueryBuilder {
|
|||||||
self.state.query_map.push(QueryClause::new(
|
self.state.query_map.push(QueryClause::new(
|
||||||
QueryClauseType::GroupStart,
|
QueryClauseType::GroupStart,
|
||||||
" WHERE ",
|
" WHERE ",
|
||||||
"("
|
"(",
|
||||||
));
|
));
|
||||||
} else {
|
} else {
|
||||||
self.state.query_map.push(QueryClause::new(
|
self.state
|
||||||
QueryClauseType::GroupStart,
|
.query_map
|
||||||
" AND ",
|
.push(QueryClause::new(QueryClauseType::GroupStart, " AND ", "("));
|
||||||
"("
|
|
||||||
));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
self
|
self
|
||||||
@ -455,11 +458,9 @@ impl QueryBuilder {
|
|||||||
|
|
||||||
/// Start a logical grouping, prefixed with `or`
|
/// Start a logical grouping, prefixed with `or`
|
||||||
pub fn or_group_start(&mut self) -> &mut Self {
|
pub fn or_group_start(&mut self) -> &mut Self {
|
||||||
self.state.query_map.push(QueryClause::new(
|
self.state
|
||||||
QueryClauseType::GroupStart,
|
.query_map
|
||||||
"",
|
.push(QueryClause::new(QueryClauseType::GroupStart, "", " OR ("));
|
||||||
" OR ("
|
|
||||||
));
|
|
||||||
|
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
@ -469,7 +470,7 @@ impl QueryBuilder {
|
|||||||
self.state.query_map.push(QueryClause::new(
|
self.state.query_map.push(QueryClause::new(
|
||||||
QueryClauseType::GroupStart,
|
QueryClauseType::GroupStart,
|
||||||
"",
|
"",
|
||||||
" OR NOT ("
|
" OR NOT (",
|
||||||
));
|
));
|
||||||
|
|
||||||
self
|
self
|
||||||
@ -477,11 +478,9 @@ impl QueryBuilder {
|
|||||||
|
|
||||||
/// End the current logical grouping
|
/// End the current logical grouping
|
||||||
pub fn group_end(&mut self) -> &mut Self {
|
pub fn group_end(&mut self) -> &mut Self {
|
||||||
self.state.query_map.push(QueryClause::new(
|
self.state
|
||||||
QueryClauseType::GroupEnd,
|
.query_map
|
||||||
"",
|
.push(QueryClause::new(QueryClauseType::GroupEnd, "", ")"));
|
||||||
")"
|
|
||||||
));
|
|
||||||
|
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user