More docs, slight refactor

This commit is contained in:
Timothy Warren 2019-04-09 18:55:53 -04:00
parent 78117b1345
commit eb2858595d
7 changed files with 61 additions and 33 deletions

View File

@ -1,4 +1,4 @@
//! Drivers //! Database 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_map_join; use crate::split_map_join;
@ -66,11 +66,12 @@ 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 quoted = split_map_join(identifier, ",", |part| self.quote_identifier(part.trim()));
// 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(.., &quoted); identifier.replace_range(
..,
&split_map_join(identifier, ",", |part| self.quote_identifier(part.trim())),
);
} }
let (open_char, close_char) = self._quotes(); let (open_char, close_char) = self._quotes();

View File

@ -1,5 +1,9 @@
//! Database Driver for MSSQL
//!
//! Contains database-specific query data
use super::*; use super::*;
/// The struct implementing the `DatabaseDriver` trait
#[derive(Debug)] #[derive(Debug)]
pub struct MSSQL; pub struct MSSQL;

View File

@ -1,5 +1,9 @@
//! Database Driver for MySQL
//!
//! Contains database-specific query data
use super::*; use super::*;
/// The struct implementing the `DatabaseDriver` trait
#[derive(Debug)] #[derive(Debug)]
pub struct MySQL; pub struct MySQL;

View File

@ -1,5 +1,9 @@
//! Database Driver for Postgres
//!
//! Contains database-specific query data
use super::*; use super::*;
/// The struct implementing the `DatabaseDriver` trait
#[derive(Debug)] #[derive(Debug)]
pub struct Postgres; pub struct Postgres;

View File

@ -1,5 +1,9 @@
//! Database Driver for SQLite
//!
//! Contains database-specific query data
use super::*; use super::*;
/// The struct implementing the `DatabaseDriver` trait
#[derive(Debug)] #[derive(Debug)]
pub struct SQLite; pub struct SQLite;

View File

@ -7,6 +7,16 @@ pub mod drivers;
pub mod query_builder; pub mod query_builder;
pub mod types; 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>( pub fn split_map_join<'a>(
string: &'a str, string: &'a str,
split_join_by: &str, split_join_by: &str,

View File

@ -1,4 +1,4 @@
//! QueryBuilder //! Query Builder
//! //!
//! The QueryBuilder creates sql queries from chained methods //! The QueryBuilder creates sql queries from chained methods
use std::any::Any; use std::any::Any;
@ -26,11 +26,17 @@ pub enum LikeWildcard {
/// The type of SQL join /// The type of SQL join
#[derive(Debug)] #[derive(Debug)]
pub enum JoinType { pub enum JoinType {
/// An `INNER` join
Inner, Inner,
/// An `OUTER` join
Outer, Outer,
/// A `LEFT` join
Left, Left,
/// A `RIGHT` join
Right, Right,
/// A `LEFT OUTER` join
LeftOuter, LeftOuter,
/// A `RIGHT OUTER` join
RightOuter, RightOuter,
} }
@ -141,6 +147,17 @@ impl QueryState {
self 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 { pub fn set_from_string(&mut self, s: &str) -> &mut Self {
self.from_string = s.to_owned(); self.from_string = s.to_owned();
@ -353,11 +370,8 @@ impl QueryBuilder {
let conjunction = "\n".to_string() + join_type + "JOIN "; let conjunction = "\n".to_string() + join_type + "JOIN ";
self.state.query_map.push(QueryClause::new( self.state
QueryClauseType::Join, .append_query_map(QueryClauseType::Join, &conjunction, &condition);
&conjunction,
&condition,
));
self self
} }
@ -426,14 +440,10 @@ impl QueryBuilder {
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 self.state
.query_map .append_query_map(QueryClauseType::GroupStart, " ", "(");
.push(QueryClause::new(QueryClauseType::GroupStart, " ", "("));
} else { } else {
self.state.query_map.push(QueryClause::new( self.state
QueryClauseType::GroupStart, .append_query_map(QueryClauseType::GroupStart, " WHERE ", "(");
" WHERE ",
"(",
));
} }
self self
@ -442,15 +452,11 @@ impl QueryBuilder {
/// Start a logical grouping, prefixed with `not` /// Start a logical grouping, prefixed with `not`
pub fn not_group_start(&mut self) -> &mut Self { pub fn not_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, .append_query_map(QueryClauseType::GroupStart, " WHERE NOT ", "(");
" WHERE ",
"(",
));
} else { } else {
self.state self.state
.query_map .append_query_map(QueryClauseType::GroupStart, " AND NOT ", "(");
.push(QueryClause::new(QueryClauseType::GroupStart, " AND ", "("));
} }
self self
@ -459,19 +465,15 @@ 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 self.state
.query_map .append_query_map(QueryClauseType::GroupStart, "", " OR (");
.push(QueryClause::new(QueryClauseType::GroupStart, "", " OR ("));
self self
} }
/// Start a logical grouping, prefixed with `or not` /// Start a logical grouping, prefixed with `or not`
pub fn or_not_group_start(&mut self) -> &mut Self { pub fn or_not_group_start(&mut self) -> &mut Self {
self.state.query_map.push(QueryClause::new( self.state
QueryClauseType::GroupStart, .append_query_map(QueryClauseType::GroupStart, "", " OR NOT (");
"",
" OR NOT (",
));
self self
} }
@ -479,8 +481,7 @@ 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 self.state
.query_map .append_query_map(QueryClauseType::GroupEnd, "", ")");
.push(QueryClause::new(QueryClauseType::GroupEnd, "", ")"));
self self
} }