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
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(.., &quoted);
identifier.replace_range(
..,
&split_map_join(identifier, ",", |part| self.quote_identifier(part.trim())),
);
}
let (open_char, close_char) = self._quotes();

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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