From eb2858595d9c2d5d9f255ee458447dfcf2a9cc16 Mon Sep 17 00:00:00 2001 From: "Timothy J. Warren" Date: Tue, 9 Apr 2019 18:55:53 -0400 Subject: [PATCH] More docs, slight refactor --- src/drivers.rs | 9 ++++--- src/drivers/mssql.rs | 4 +++ src/drivers/mysql.rs | 4 +++ src/drivers/postgres.rs | 4 +++ src/drivers/sqlite.rs | 4 +++ src/lib.rs | 10 +++++++ src/query_builder.rs | 59 +++++++++++++++++++++-------------------- 7 files changed, 61 insertions(+), 33 deletions(-) diff --git a/src/drivers.rs b/src/drivers.rs index 3b2a193..933413b 100644 --- a/src/drivers.rs +++ b/src/drivers.rs @@ -1,4 +1,4 @@ -//! Drivers +//! Database Drivers //! //! Drivers represent a connection to a specific type of database engine use crate::split_map_join; @@ -66,11 +66,12 @@ 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 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(.., "ed); + identifier.replace_range( + .., + &split_map_join(identifier, ",", |part| self.quote_identifier(part.trim())), + ); } let (open_char, close_char) = self._quotes(); diff --git a/src/drivers/mssql.rs b/src/drivers/mssql.rs index c0c908e..4ec1ce1 100644 --- a/src/drivers/mssql.rs +++ b/src/drivers/mssql.rs @@ -1,5 +1,9 @@ +//! Database Driver for MSSQL +//! +//! Contains database-specific query data use super::*; +/// The struct implementing the `DatabaseDriver` trait #[derive(Debug)] pub struct MSSQL; diff --git a/src/drivers/mysql.rs b/src/drivers/mysql.rs index 8434c79..0263d2c 100644 --- a/src/drivers/mysql.rs +++ b/src/drivers/mysql.rs @@ -1,5 +1,9 @@ +//! Database Driver for MySQL +//! +//! Contains database-specific query data use super::*; +/// The struct implementing the `DatabaseDriver` trait #[derive(Debug)] pub struct MySQL; diff --git a/src/drivers/postgres.rs b/src/drivers/postgres.rs index f8b7597..2d1dd9a 100644 --- a/src/drivers/postgres.rs +++ b/src/drivers/postgres.rs @@ -1,5 +1,9 @@ +//! Database Driver for Postgres +//! +//! Contains database-specific query data use super::*; +/// The struct implementing the `DatabaseDriver` trait #[derive(Debug)] pub struct Postgres; diff --git a/src/drivers/sqlite.rs b/src/drivers/sqlite.rs index d00c95c..3804ab9 100644 --- a/src/drivers/sqlite.rs +++ b/src/drivers/sqlite.rs @@ -1,5 +1,9 @@ +//! Database Driver for SQLite +//! +//! Contains database-specific query data use super::*; +/// The struct implementing the `DatabaseDriver` trait #[derive(Debug)] pub struct SQLite; diff --git a/src/lib.rs b/src/lib.rs index 0d20458..9ea6d9c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -7,6 +7,16 @@ pub mod drivers; pub mod query_builder; pub mod types; +/// Split a string, apply a closure to each substring, +/// then join the string back together +/// +/// For example: +/// ``` +/// use stringqb::split_map_join; +/// +/// let result = split_map_join("a\n,b, c\t,d", ",", |s| s.trim().to_string()); +/// assert_eq!("a,b,c,d", result); +/// ``` pub fn split_map_join<'a>( string: &'a str, split_join_by: &str, diff --git a/src/query_builder.rs b/src/query_builder.rs index ca3550b..5f61b8a 100644 --- a/src/query_builder.rs +++ b/src/query_builder.rs @@ -1,4 +1,4 @@ -//! QueryBuilder +//! Query Builder //! //! The QueryBuilder creates sql queries from chained methods use std::any::Any; @@ -26,11 +26,17 @@ pub enum LikeWildcard { /// The type of SQL join #[derive(Debug)] pub enum JoinType { + /// An `INNER` join Inner, + /// An `OUTER` join Outer, + /// A `LEFT` join Left, + /// A `RIGHT` join Right, + /// A `LEFT OUTER` join LeftOuter, + /// A `RIGHT OUTER` join RightOuter, } @@ -141,6 +147,17 @@ impl QueryState { self } + pub fn append_query_map( + &mut self, + clause_type: QueryClauseType, + conj: &str, + s: &str, + ) -> &mut Self { + self.query_map.push(QueryClause::new(clause_type, conj, s)); + + self + } + pub fn set_from_string(&mut self, s: &str) -> &mut Self { self.from_string = s.to_owned(); @@ -353,11 +370,8 @@ impl QueryBuilder { let conjunction = "\n".to_string() + join_type + "JOIN "; - self.state.query_map.push(QueryClause::new( - QueryClauseType::Join, - &conjunction, - &condition, - )); + self.state + .append_query_map(QueryClauseType::Join, &conjunction, &condition); self } @@ -426,14 +440,10 @@ impl QueryBuilder { pub fn group_start(&mut self) -> &mut Self { if self.state.query_map.len() == 0 { self.state - .query_map - .push(QueryClause::new(QueryClauseType::GroupStart, " ", "(")); + .append_query_map(QueryClauseType::GroupStart, " ", "("); } else { - self.state.query_map.push(QueryClause::new( - QueryClauseType::GroupStart, - " WHERE ", - "(", - )); + self.state + .append_query_map(QueryClauseType::GroupStart, " WHERE ", "("); } self @@ -442,15 +452,11 @@ impl QueryBuilder { /// Start a logical grouping, prefixed with `not` pub fn not_group_start(&mut self) -> &mut Self { if self.state.query_map.len() == 0 { - self.state.query_map.push(QueryClause::new( - QueryClauseType::GroupStart, - " WHERE ", - "(", - )); + self.state + .append_query_map(QueryClauseType::GroupStart, " WHERE NOT ", "("); } else { self.state - .query_map - .push(QueryClause::new(QueryClauseType::GroupStart, " AND ", "(")); + .append_query_map(QueryClauseType::GroupStart, " AND NOT ", "("); } self @@ -459,19 +465,15 @@ 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 (")); + .append_query_map(QueryClauseType::GroupStart, "", " OR ("); self } /// Start a logical grouping, prefixed with `or not` pub fn or_not_group_start(&mut self) -> &mut Self { - self.state.query_map.push(QueryClause::new( - QueryClauseType::GroupStart, - "", - " OR NOT (", - )); + self.state + .append_query_map(QueryClauseType::GroupStart, "", " OR NOT ("); self } @@ -479,8 +481,7 @@ impl QueryBuilder { /// End the current logical grouping pub fn group_end(&mut self) -> &mut Self { self.state - .query_map - .push(QueryClause::new(QueryClauseType::GroupEnd, "", ")")); + .append_query_map(QueryClauseType::GroupEnd, "", ")"); self }