2019-04-09 18:55:53 -04:00
|
|
|
//! Query Builder
|
2019-04-02 16:35:52 -04:00
|
|
|
//!
|
|
|
|
//! The QueryBuilder creates sql queries from chained methods
|
2019-04-02 14:36:11 -04:00
|
|
|
use std::collections::HashMap;
|
|
|
|
|
2019-04-09 17:33:26 -04:00
|
|
|
use crate::drivers::{DatabaseDriver, DefaultDriver};
|
2019-04-10 14:03:28 -04:00
|
|
|
use crate::split_map_join;
|
2019-04-10 20:11:26 -04:00
|
|
|
use crate::types::Wild;
|
2019-04-10 19:49:06 -04:00
|
|
|
|
2019-04-03 16:29:51 -04:00
|
|
|
/// The position of the wildcard(s)
|
|
|
|
/// for a `like` clause
|
2019-04-03 13:29:08 -04:00
|
|
|
#[derive(Debug)]
|
|
|
|
pub enum LikeWildcard {
|
2019-04-03 16:29:51 -04:00
|
|
|
/// Wildcard before search term
|
|
|
|
/// eg. `%foo`
|
2019-04-03 13:29:08 -04:00
|
|
|
Before,
|
2019-04-03 16:29:51 -04:00
|
|
|
|
|
|
|
/// Wildcard after the search term
|
|
|
|
/// eg. `foo%`
|
2019-04-03 13:29:08 -04:00
|
|
|
After,
|
2019-04-03 16:29:51 -04:00
|
|
|
|
|
|
|
/// Wildcards surrounding the search term
|
|
|
|
/// eg. `%foo%`
|
2019-04-03 13:29:08 -04:00
|
|
|
Both,
|
|
|
|
}
|
|
|
|
|
2019-04-03 16:29:51 -04:00
|
|
|
/// The type of SQL join
|
2019-04-03 13:29:08 -04:00
|
|
|
#[derive(Debug)]
|
|
|
|
pub enum JoinType {
|
2019-04-10 14:03:28 -04:00
|
|
|
/// A `CROSS` join
|
|
|
|
Cross,
|
2019-04-09 18:55:53 -04:00
|
|
|
/// An `INNER` join
|
2019-04-03 13:29:08 -04:00
|
|
|
Inner,
|
2019-04-09 18:55:53 -04:00
|
|
|
/// An `OUTER` join
|
2019-04-03 13:29:08 -04:00
|
|
|
Outer,
|
2019-04-10 14:03:28 -04:00
|
|
|
/// A `LEFT (OUTER)` join
|
2019-04-03 13:29:08 -04:00
|
|
|
Left,
|
2019-04-10 14:03:28 -04:00
|
|
|
/// A `RIGHT (OUTER)` join
|
2019-04-03 13:29:08 -04:00
|
|
|
Right,
|
|
|
|
}
|
|
|
|
|
2019-04-03 16:29:51 -04:00
|
|
|
/// The sort direction
|
2019-04-09 14:13:37 -04:00
|
|
|
#[derive(Debug, PartialEq)]
|
2019-04-03 13:29:08 -04:00
|
|
|
pub enum OrderDirection {
|
2019-04-03 16:29:51 -04:00
|
|
|
/// Sort Ascending
|
2019-04-03 13:29:08 -04:00
|
|
|
Asc,
|
2019-04-03 16:29:51 -04:00
|
|
|
/// Sort Descending
|
2019-04-03 13:29:08 -04:00
|
|
|
Desc,
|
2019-04-10 14:03:28 -04:00
|
|
|
/// Random Sort (Not yet implemented!)
|
2019-04-09 14:13:37 -04:00
|
|
|
Rand,
|
2019-04-03 13:29:08 -04:00
|
|
|
}
|
|
|
|
|
2019-04-02 14:36:11 -04:00
|
|
|
#[derive(Debug)]
|
|
|
|
enum QueryClauseType {
|
|
|
|
GroupEnd,
|
|
|
|
GroupStart,
|
2019-04-09 14:13:37 -04:00
|
|
|
Join,
|
2019-04-02 14:36:11 -04:00
|
|
|
Like,
|
|
|
|
Where,
|
|
|
|
WhereIn,
|
|
|
|
}
|
|
|
|
|
2019-04-10 14:03:28 -04:00
|
|
|
#[derive(Debug)]
|
|
|
|
enum QueryType {
|
|
|
|
Select,
|
|
|
|
Insert,
|
|
|
|
Update,
|
|
|
|
Delete,
|
|
|
|
}
|
|
|
|
|
2019-04-02 14:36:11 -04:00
|
|
|
#[derive(Debug)]
|
|
|
|
struct QueryClause {
|
|
|
|
clause_type: QueryClauseType,
|
|
|
|
conjunction: String,
|
|
|
|
string: String,
|
|
|
|
}
|
|
|
|
|
2019-04-03 16:41:51 -04:00
|
|
|
impl QueryClause {
|
2019-04-09 14:13:37 -04:00
|
|
|
pub fn new(clause_type: QueryClauseType, conjunction: &str, string: &str) -> Self {
|
2019-04-03 16:41:51 -04:00
|
|
|
QueryClause {
|
|
|
|
clause_type,
|
2019-04-09 14:13:37 -04:00
|
|
|
conjunction: conjunction.to_string(),
|
|
|
|
string: string.to_string(),
|
2019-04-03 16:41:51 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-03 16:29:51 -04:00
|
|
|
#[derive(Debug)]
|
2019-04-02 14:36:11 -04:00
|
|
|
struct QueryState {
|
|
|
|
select_string: String,
|
|
|
|
from_string: String,
|
|
|
|
set_string: String,
|
|
|
|
order_string: String,
|
|
|
|
group_string: String,
|
|
|
|
|
|
|
|
// Keys for insert/update statement
|
2019-04-02 17:23:52 -04:00
|
|
|
set_array_keys: Vec<String>,
|
2019-04-02 14:36:11 -04:00
|
|
|
|
|
|
|
// Order by clause
|
|
|
|
order_array: HashMap<String, String>,
|
|
|
|
|
|
|
|
// Group by clause
|
2019-04-09 14:13:37 -04:00
|
|
|
group_array: Vec<String>,
|
2019-04-02 14:36:11 -04:00
|
|
|
|
|
|
|
// Values to apply to prepared statements
|
2019-04-10 19:49:06 -04:00
|
|
|
values: Vec<Wild>,
|
2019-04-02 14:36:11 -04:00
|
|
|
|
|
|
|
// Values to apply to where clauses in prepared statements
|
2019-04-10 19:49:06 -04:00
|
|
|
where_values: Vec<Wild>,
|
2019-04-02 14:36:11 -04:00
|
|
|
|
2019-04-04 16:39:05 -04:00
|
|
|
limit: Option<usize>,
|
2019-04-02 14:36:11 -04:00
|
|
|
|
2019-04-04 16:39:05 -04:00
|
|
|
offset: Option<usize>,
|
2019-04-02 14:36:11 -04:00
|
|
|
|
|
|
|
// Query components for complex selects
|
|
|
|
query_map: Vec<QueryClause>,
|
|
|
|
|
|
|
|
// Query components for having clauses
|
|
|
|
having_map: Vec<QueryClause>,
|
|
|
|
}
|
|
|
|
|
2019-04-03 16:29:51 -04:00
|
|
|
impl Default for QueryState {
|
|
|
|
fn default() -> Self {
|
|
|
|
QueryState {
|
|
|
|
select_string: String::from(""),
|
|
|
|
from_string: String::from(""),
|
|
|
|
set_string: String::from(""),
|
|
|
|
order_string: String::from(""),
|
|
|
|
group_string: String::from(""),
|
|
|
|
|
|
|
|
set_array_keys: vec![],
|
|
|
|
order_array: HashMap::new(),
|
2019-04-09 14:13:37 -04:00
|
|
|
group_array: vec![],
|
2019-04-03 16:29:51 -04:00
|
|
|
values: vec![],
|
|
|
|
where_values: vec![],
|
|
|
|
|
|
|
|
limit: None,
|
|
|
|
offset: None,
|
|
|
|
|
|
|
|
query_map: vec![],
|
|
|
|
having_map: vec![],
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-02 14:36:11 -04:00
|
|
|
impl QueryState {
|
2019-04-04 16:39:05 -04:00
|
|
|
pub fn new() -> Self {
|
2019-04-02 14:36:11 -04:00
|
|
|
QueryState::default()
|
|
|
|
}
|
2019-04-09 14:13:37 -04:00
|
|
|
|
2019-04-09 17:33:26 -04:00
|
|
|
pub fn append_select_string(&mut self, s: &str) -> &mut Self {
|
2019-04-09 14:13:37 -04:00
|
|
|
self.select_string += s;
|
|
|
|
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2019-04-10 19:49:06 -04:00
|
|
|
pub fn append_where_values(&mut self, val: Wild) -> &mut Self {
|
2019-04-10 19:37:02 -04:00
|
|
|
self.where_values.push(val);
|
|
|
|
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2019-04-09 18:55:53 -04:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2019-04-09 17:33:26 -04:00
|
|
|
pub fn set_from_string(&mut self, s: &str) -> &mut Self {
|
2019-04-09 14:13:37 -04:00
|
|
|
self.from_string = s.to_owned();
|
|
|
|
|
|
|
|
self
|
|
|
|
}
|
2019-04-02 14:36:11 -04:00
|
|
|
}
|
|
|
|
|
2019-04-02 16:35:52 -04:00
|
|
|
/// The struct representing a query builder
|
2019-04-09 14:13:37 -04:00
|
|
|
#[derive(Debug)]
|
2019-04-02 14:36:11 -04:00
|
|
|
pub struct QueryBuilder {
|
|
|
|
state: QueryState,
|
2019-04-09 14:13:37 -04:00
|
|
|
driver: Box<dyn DatabaseDriver>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for QueryBuilder {
|
|
|
|
/// Creates a new QueryBuilder instance with default driver
|
|
|
|
fn default() -> Self {
|
|
|
|
QueryBuilder {
|
|
|
|
state: QueryState::new(),
|
|
|
|
driver: Box::new(DefaultDriver::new()),
|
|
|
|
}
|
|
|
|
}
|
2019-04-02 14:36:11 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
impl QueryBuilder {
|
2019-04-09 14:13:37 -04:00
|
|
|
/// Create a new QueryBuilder instance with a driver
|
|
|
|
pub fn new(driver: impl DatabaseDriver + 'static) -> Self {
|
2019-04-02 14:36:11 -04:00
|
|
|
QueryBuilder {
|
|
|
|
state: QueryState::new(),
|
2019-04-09 14:13:37 -04:00
|
|
|
driver: Box::new(driver),
|
2019-04-02 14:36:11 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-03 13:29:08 -04:00
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
// ! Select Queries
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
2019-04-05 14:51:31 -04:00
|
|
|
/// Set the fields to select from the database as a string
|
2019-04-04 16:39:05 -04:00
|
|
|
pub fn select(&mut self, fields: &str) -> &mut Self {
|
2019-04-10 14:03:28 -04:00
|
|
|
let fields = split_map_join(fields, ",", |s| s.trim().to_string());
|
|
|
|
|
|
|
|
// Split identifiers on `As` keyword so they can be quoted properly
|
|
|
|
// @TODO split identifiers on `as` keyword (needs to be case-insensitive)
|
|
|
|
|
|
|
|
// Quote the identifiers (where there was an `as` keyword)
|
|
|
|
|
|
|
|
// Rejoin those identifiers
|
|
|
|
|
|
|
|
self.state.append_select_string(&fields);
|
|
|
|
|
|
|
|
self
|
2019-04-02 17:23:52 -04:00
|
|
|
}
|
2019-04-05 20:46:07 -04:00
|
|
|
|
2019-04-05 14:51:31 -04:00
|
|
|
/// Set the fields to select from the database as a Vector
|
|
|
|
pub fn select_vec(&mut self, fields: Vec<&str>) -> &mut Self {
|
|
|
|
let fields = fields.join(",");
|
|
|
|
self.select(&fields)
|
|
|
|
}
|
2019-04-02 17:23:52 -04:00
|
|
|
|
2019-04-03 16:29:51 -04:00
|
|
|
/// Adds the `distinct` keyword to a query
|
2019-04-04 16:39:05 -04:00
|
|
|
pub fn distinct(&mut self) -> &mut Self {
|
2019-04-11 11:44:06 -04:00
|
|
|
self.state.select_string = String::from(" DISTINCT") + &self.state.select_string;
|
|
|
|
|
|
|
|
self
|
2019-04-03 13:29:08 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Specify the database table to select from
|
2019-04-04 16:39:05 -04:00
|
|
|
pub fn from(&mut self, table_name: &str) -> &mut Self {
|
2019-04-11 11:44:06 -04:00
|
|
|
let ident_vec = String::from(table_name)
|
|
|
|
.split(" ")
|
|
|
|
.into_iter()
|
|
|
|
.map(|s| self.driver.quote_identifier(s))
|
|
|
|
.collect::<Vec<String>>();
|
|
|
|
|
|
|
|
self.state.from_string = ident_vec.join(" ");
|
2019-04-03 13:29:08 -04:00
|
|
|
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
// ! 'Like' methods
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
2019-04-03 16:29:51 -04:00
|
|
|
/// Creates a `like` clause in the sql statement
|
2019-04-10 19:49:06 -04:00
|
|
|
pub fn like(&mut self, field: &str, value: Wild, position: LikeWildcard) -> &mut Self {
|
2019-04-10 19:37:02 -04:00
|
|
|
self._like(field, value, position, "LIKE", "AND")
|
2019-04-03 13:29:08 -04:00
|
|
|
}
|
|
|
|
|
2019-04-03 16:29:51 -04:00
|
|
|
/// Generates an OR Like clause
|
2019-04-10 19:49:06 -04:00
|
|
|
pub fn or_like(&mut self, field: &str, value: Wild, position: LikeWildcard) -> &mut Self {
|
2019-04-10 19:37:02 -04:00
|
|
|
self._like(field, value, position, "LIKE", "OR")
|
2019-04-03 13:29:08 -04:00
|
|
|
}
|
|
|
|
|
2019-04-03 16:29:51 -04:00
|
|
|
/// Generates a NOI Like clause
|
2019-04-10 19:49:06 -04:00
|
|
|
pub fn not_like(&mut self, field: &str, value: Wild, position: LikeWildcard) -> &mut Self {
|
2019-04-10 19:37:02 -04:00
|
|
|
self._like(field, value, position, "NOT LIKE", "AND")
|
2019-04-03 13:29:08 -04:00
|
|
|
}
|
|
|
|
|
2019-04-03 16:29:51 -04:00
|
|
|
/// Generates an OR NOT Like clause
|
2019-04-10 19:49:06 -04:00
|
|
|
pub fn or_not_like(&mut self, field: &str, value: Wild, position: LikeWildcard) -> &mut Self {
|
2019-04-10 19:37:02 -04:00
|
|
|
self._like(field, value, position, "NOT LIKE", "OR")
|
2019-04-03 13:29:08 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
// ! Having methods
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
2019-04-03 16:29:51 -04:00
|
|
|
/// Add a `having` clause to the query
|
2019-04-10 19:49:06 -04:00
|
|
|
pub fn having(&mut self, key: &str, value: Wild) -> &mut Self {
|
2019-04-03 13:29:08 -04:00
|
|
|
unimplemented!();
|
|
|
|
}
|
|
|
|
|
2019-04-03 16:29:51 -04:00
|
|
|
/// Add a `having` clause to the query, prefixed with an `or`
|
2019-04-10 19:49:06 -04:00
|
|
|
pub fn or_having(&mut self, key: &str, value: Wild) -> &mut Self {
|
2019-04-03 13:29:08 -04:00
|
|
|
unimplemented!();
|
|
|
|
}
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
// ! 'Where' methods
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
2019-04-03 16:29:51 -04:00
|
|
|
/// Specify a condition for the `where` clause of the query
|
2019-04-10 19:49:06 -04:00
|
|
|
pub fn r#where(&mut self, key: &str, op: &str, value: Wild) -> &mut Self {
|
2019-04-03 20:58:22 -04:00
|
|
|
// @TODO actually implement setting the keys for the where
|
|
|
|
self.state.where_values.push(value);
|
2019-04-04 16:39:05 -04:00
|
|
|
|
2019-04-03 20:58:22 -04:00
|
|
|
self
|
2019-04-03 13:29:08 -04:00
|
|
|
}
|
2019-04-05 20:46:07 -04:00
|
|
|
|
2019-04-10 14:03:28 -04:00
|
|
|
/// Specify a condition for a `where` clause where a column has a value
|
2019-04-10 19:49:06 -04:00
|
|
|
pub fn where_eq(&mut self, key: &str, value: Wild) -> &mut Self {
|
2019-04-05 14:51:31 -04:00
|
|
|
self.r#where(key, "=", value)
|
|
|
|
}
|
2019-04-03 13:29:08 -04:00
|
|
|
|
2019-04-03 16:29:51 -04:00
|
|
|
/// Specify a condition for the `where` clause of the query, prefixed with `or`
|
2019-04-10 19:49:06 -04:00
|
|
|
pub fn or_where(&mut self, key: &str, value: Wild) -> &mut Self {
|
2019-04-03 13:29:08 -04:00
|
|
|
unimplemented!();
|
|
|
|
}
|
|
|
|
|
2019-04-03 16:29:51 -04:00
|
|
|
/// Specify a `where in` clause for the query
|
2019-04-10 19:49:06 -04:00
|
|
|
pub fn where_in(&mut self, key: &str, value: Vec<Wild>) -> &mut Self {
|
2019-04-03 13:29:08 -04:00
|
|
|
unimplemented!();
|
|
|
|
}
|
|
|
|
|
2019-04-03 16:29:51 -04:00
|
|
|
/// Specify a `where in` clause for the query, prefixed with `or`
|
2019-04-10 19:49:06 -04:00
|
|
|
pub fn or_where_in(&mut self, key: &str, value: Vec<Wild>) -> &mut Self {
|
2019-04-03 13:29:08 -04:00
|
|
|
unimplemented!();
|
|
|
|
}
|
|
|
|
|
2019-04-03 16:29:51 -04:00
|
|
|
/// Specify a `where not in` clause for the query
|
2019-04-10 19:49:06 -04:00
|
|
|
pub fn where_not_in(&mut self, key: &str, value: Vec<Wild>) -> &mut Self {
|
2019-04-03 13:29:08 -04:00
|
|
|
unimplemented!();
|
|
|
|
}
|
|
|
|
|
2019-04-03 16:29:51 -04:00
|
|
|
/// Specify a `where not in` clause for the query, prefixed with `or`
|
2019-04-10 19:49:06 -04:00
|
|
|
pub fn or_where_not_in(&mut self, key: &str, value: Vec<Wild>) -> &mut Self {
|
2019-04-03 13:29:08 -04:00
|
|
|
unimplemented!();
|
|
|
|
}
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
// ! Other Query Modifier methods
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
2019-04-02 16:35:52 -04:00
|
|
|
/// Set a key and value for an insert or update query
|
2019-04-10 19:49:06 -04:00
|
|
|
pub fn set(&mut self, key: &str, value: Wild) -> &mut Self {
|
2019-04-02 17:23:52 -04:00
|
|
|
// @TODO figure a way to make this easier to use
|
|
|
|
self.state.set_array_keys.push(key.to_string());
|
2019-04-02 14:36:11 -04:00
|
|
|
self.state.values.push(value);
|
|
|
|
|
|
|
|
self
|
|
|
|
}
|
2019-04-02 17:23:52 -04:00
|
|
|
|
|
|
|
/// Set a map of data for an insert or update query
|
2019-04-10 19:49:06 -04:00
|
|
|
pub fn set_map(&mut self, data: HashMap<String, Wild>) -> &mut Self {
|
2019-04-02 17:23:52 -04:00
|
|
|
for (key, value) in data {
|
2019-04-04 16:39:05 -04:00
|
|
|
self.set(&key, value);
|
2019-04-02 17:23:52 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2019-04-09 14:13:37 -04:00
|
|
|
/// Convenience method for a `left` join
|
|
|
|
pub fn left_join(&mut self, table: &str, col: &str, op: &str, value: &str) -> &mut Self {
|
|
|
|
self.join(table, col, op, value, JoinType::Left)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Convenience method for an `inner` join
|
|
|
|
pub fn inner_join(&mut self, table: &str, col: &str, op: &str, value: &str) -> &mut Self {
|
|
|
|
self.join(table, col, op, value, JoinType::Inner)
|
|
|
|
}
|
|
|
|
|
2019-04-03 16:29:51 -04:00
|
|
|
/// Add a table join to the query
|
2019-04-09 17:33:26 -04:00
|
|
|
pub fn join(
|
|
|
|
&mut self,
|
|
|
|
table: &str,
|
|
|
|
col: &str,
|
|
|
|
op: &str,
|
|
|
|
value: &str,
|
|
|
|
join_type: JoinType,
|
|
|
|
) -> &mut Self {
|
2019-04-09 14:13:37 -04:00
|
|
|
let table = self.driver.quote_identifier(table);
|
|
|
|
let col = self.driver.quote_identifier(col);
|
|
|
|
let condition = table + " ON " + &col + op + value;
|
|
|
|
|
|
|
|
let join_type = match join_type {
|
2019-04-10 14:03:28 -04:00
|
|
|
JoinType::Cross => "CROSS ",
|
2019-04-09 14:13:37 -04:00
|
|
|
JoinType::Left => "LEFT ",
|
|
|
|
JoinType::Inner => "INNER ",
|
|
|
|
JoinType::Outer => "OUTER ",
|
|
|
|
JoinType::Right => "RIGHT ",
|
|
|
|
};
|
|
|
|
|
|
|
|
let conjunction = "\n".to_string() + join_type + "JOIN ";
|
|
|
|
|
2019-04-09 18:55:53 -04:00
|
|
|
self.state
|
|
|
|
.append_query_map(QueryClauseType::Join, &conjunction, &condition);
|
2019-04-09 14:13:37 -04:00
|
|
|
|
|
|
|
self
|
2019-04-03 13:29:08 -04:00
|
|
|
}
|
2019-04-02 17:23:52 -04:00
|
|
|
|
2019-04-03 16:29:51 -04:00
|
|
|
/// Add a group by clause to the query
|
2019-04-04 16:39:05 -04:00
|
|
|
pub fn group_by(&mut self, field: &str) -> &mut Self {
|
2019-04-09 14:13:37 -04:00
|
|
|
self.state.group_array.push(field.to_string());
|
|
|
|
|
|
|
|
self.state.group_string = " GROUP BY ".to_string() + &self.state.group_array.join(",");
|
|
|
|
|
|
|
|
self
|
2019-04-03 13:29:08 -04:00
|
|
|
}
|
|
|
|
|
2019-04-03 16:29:51 -04:00
|
|
|
/// Add an order by clause to the query
|
2019-04-04 16:39:05 -04:00
|
|
|
pub fn order_by(&mut self, field: &str, direction: OrderDirection) -> &mut Self {
|
2019-04-09 14:13:37 -04:00
|
|
|
if direction == OrderDirection::Rand {
|
|
|
|
// @TODO handle random sorting
|
|
|
|
unimplemented!();
|
|
|
|
}
|
|
|
|
|
|
|
|
let field = self.driver.quote_identifier(field);
|
|
|
|
let dir = match direction {
|
|
|
|
OrderDirection::Asc => String::from("ASC"),
|
|
|
|
OrderDirection::Desc => String::from("DESC"),
|
|
|
|
OrderDirection::Rand => String::from("RAND"),
|
|
|
|
};
|
|
|
|
self.state.order_array.insert(field, dir);
|
|
|
|
|
|
|
|
let mut order_clauses: Vec<String> = vec![];
|
|
|
|
|
|
|
|
for (f, dir) in &self.state.order_array {
|
|
|
|
let clause = String::clone(f) + " " + &dir;
|
|
|
|
&order_clauses.push(clause);
|
|
|
|
}
|
|
|
|
|
|
|
|
let order_str = if direction != OrderDirection::Rand {
|
|
|
|
"\nORDER BY ".to_string() + &order_clauses.join(", ")
|
|
|
|
} else {
|
|
|
|
unimplemented!();
|
|
|
|
};
|
|
|
|
|
|
|
|
self.state.order_string = order_str;
|
|
|
|
|
|
|
|
self
|
2019-04-03 13:29:08 -04:00
|
|
|
}
|
|
|
|
|
2019-04-03 16:29:51 -04:00
|
|
|
/// Add a limit to the query
|
2019-04-04 16:39:05 -04:00
|
|
|
pub fn limit(&mut self, limit: usize) -> &mut Self {
|
2019-04-03 16:29:51 -04:00
|
|
|
self.state.limit = Some(limit);
|
|
|
|
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Add an offset to the query
|
2019-04-04 16:39:05 -04:00
|
|
|
pub fn offset(&mut self, offset: usize) -> &mut Self {
|
2019-04-03 16:29:51 -04:00
|
|
|
self.state.offset = Some(offset);
|
|
|
|
|
|
|
|
self
|
2019-04-03 13:29:08 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
// ! Query Grouping Methods
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
2019-04-03 16:29:51 -04:00
|
|
|
/// Start a logical grouping in the current query
|
2019-04-04 16:39:05 -04:00
|
|
|
pub fn group_start(&mut self) -> &mut Self {
|
2019-04-10 14:03:28 -04:00
|
|
|
let conj = if self.state.query_map.len() == 0 {
|
|
|
|
" WHERE "
|
2019-04-09 14:13:37 -04:00
|
|
|
} else {
|
2019-04-10 14:03:28 -04:00
|
|
|
" "
|
|
|
|
};
|
|
|
|
|
|
|
|
self.state
|
|
|
|
.append_query_map(QueryClauseType::GroupStart, conj, "(");
|
2019-04-09 14:13:37 -04:00
|
|
|
|
|
|
|
self
|
2019-04-03 13:29:08 -04:00
|
|
|
}
|
|
|
|
|
2019-04-03 16:29:51 -04:00
|
|
|
/// Start a logical grouping, prefixed with `not`
|
2019-04-04 16:39:05 -04:00
|
|
|
pub fn not_group_start(&mut self) -> &mut Self {
|
2019-04-10 14:03:28 -04:00
|
|
|
let conj = if self.state.query_map.len() == 0 {
|
|
|
|
" WHERE "
|
2019-04-09 14:13:37 -04:00
|
|
|
} else {
|
2019-04-10 14:03:28 -04:00
|
|
|
" AND "
|
|
|
|
};
|
|
|
|
|
|
|
|
self.state
|
|
|
|
.append_query_map(QueryClauseType::GroupStart, conj, "NOT (");
|
2019-04-09 14:13:37 -04:00
|
|
|
|
|
|
|
self
|
2019-04-03 13:29:08 -04:00
|
|
|
}
|
|
|
|
|
2019-04-03 16:29:51 -04:00
|
|
|
/// Start a logical grouping, prefixed with `or`
|
2019-04-04 16:39:05 -04:00
|
|
|
pub fn or_group_start(&mut self) -> &mut Self {
|
2019-04-09 17:33:26 -04:00
|
|
|
self.state
|
2019-04-09 18:55:53 -04:00
|
|
|
.append_query_map(QueryClauseType::GroupStart, "", " OR (");
|
2019-04-09 14:13:37 -04:00
|
|
|
|
|
|
|
self
|
2019-04-03 13:29:08 -04:00
|
|
|
}
|
|
|
|
|
2019-04-03 16:29:51 -04:00
|
|
|
/// Start a logical grouping, prefixed with `or not`
|
2019-04-04 16:39:05 -04:00
|
|
|
pub fn or_not_group_start(&mut self) -> &mut Self {
|
2019-04-09 18:55:53 -04:00
|
|
|
self.state
|
|
|
|
.append_query_map(QueryClauseType::GroupStart, "", " OR NOT (");
|
2019-04-09 14:13:37 -04:00
|
|
|
|
|
|
|
self
|
2019-04-03 13:29:08 -04:00
|
|
|
}
|
|
|
|
|
2019-04-03 16:29:51 -04:00
|
|
|
/// End the current logical grouping
|
2019-04-04 16:39:05 -04:00
|
|
|
pub fn group_end(&mut self) -> &mut Self {
|
2019-04-09 17:33:26 -04:00
|
|
|
self.state
|
2019-04-09 18:55:53 -04:00
|
|
|
.append_query_map(QueryClauseType::GroupEnd, "", ")");
|
2019-04-09 14:13:37 -04:00
|
|
|
|
|
|
|
self
|
2019-04-02 17:23:52 -04:00
|
|
|
}
|
|
|
|
|
2019-04-03 13:29:08 -04:00
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
// ! Query execution methods
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
2019-04-02 17:23:52 -04:00
|
|
|
/// Execute the built query
|
2019-04-10 14:03:28 -04:00
|
|
|
pub fn get(self) {
|
2019-04-02 17:23:52 -04:00
|
|
|
unimplemented!();
|
|
|
|
}
|
2019-04-03 13:29:08 -04:00
|
|
|
|
2019-04-03 16:29:51 -04:00
|
|
|
/// Count all the rows in the specified database table
|
2019-04-04 16:39:05 -04:00
|
|
|
pub fn count_all(self, table: &str) -> usize {
|
2019-04-03 13:29:08 -04:00
|
|
|
unimplemented!();
|
|
|
|
}
|
|
|
|
|
2019-04-03 16:29:51 -04:00
|
|
|
/// Execute the generated insert query
|
2019-04-04 16:39:05 -04:00
|
|
|
pub fn insert(&mut self, table: &str) {
|
2019-04-03 13:29:08 -04:00
|
|
|
// @TODO determine query result type
|
|
|
|
unimplemented!();
|
|
|
|
}
|
|
|
|
|
2019-04-03 16:29:51 -04:00
|
|
|
/// Execute the generated update query
|
2019-04-04 16:39:05 -04:00
|
|
|
pub fn update(&mut self, table: &str) {
|
2019-04-03 13:29:08 -04:00
|
|
|
// @TODO determine query result type
|
|
|
|
unimplemented!();
|
|
|
|
}
|
|
|
|
|
2019-04-03 16:29:51 -04:00
|
|
|
/// Execute the generated delete query
|
2019-04-04 16:39:05 -04:00
|
|
|
pub fn delete(&mut self, table: &str) {
|
2019-04-03 13:29:08 -04:00
|
|
|
unimplemented!();
|
|
|
|
}
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
// ! SQL Returning Methods
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
2019-04-03 16:29:51 -04:00
|
|
|
/// Get the generated SQL for a select query
|
2019-04-03 13:29:08 -04:00
|
|
|
pub fn get_compiled_select(self) -> String {
|
|
|
|
unimplemented!();
|
|
|
|
}
|
|
|
|
|
2019-04-03 16:29:51 -04:00
|
|
|
/// Get the generated SQL for an insert query
|
2019-04-03 13:29:08 -04:00
|
|
|
pub fn get_compiled_insert(self) -> String {
|
|
|
|
unimplemented!();
|
|
|
|
}
|
|
|
|
|
2019-04-03 16:29:51 -04:00
|
|
|
/// Get the generated SQL for an update query
|
2019-04-03 13:29:08 -04:00
|
|
|
pub fn get_compiled_update(self) -> String {
|
|
|
|
unimplemented!();
|
|
|
|
}
|
|
|
|
|
2019-04-03 16:29:51 -04:00
|
|
|
/// Get the generated SQL for a delete query
|
2019-04-03 13:29:08 -04:00
|
|
|
pub fn get_compiled_delete(self) -> String {
|
|
|
|
unimplemented!();
|
|
|
|
}
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
// ! Miscellaneous Methods
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
2019-04-03 16:29:51 -04:00
|
|
|
/// Get a new instance of the query builder
|
2019-04-09 14:13:37 -04:00
|
|
|
pub fn reset_query(mut self) -> Self {
|
|
|
|
self.state = QueryState::new();
|
|
|
|
|
|
|
|
self
|
2019-04-03 13:29:08 -04:00
|
|
|
}
|
2019-04-09 20:45:57 -04:00
|
|
|
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
// ! Implementation Details
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
2019-04-10 19:49:06 -04:00
|
|
|
fn _like(
|
|
|
|
&mut self,
|
|
|
|
field: &str,
|
|
|
|
value: Wild,
|
|
|
|
position: LikeWildcard,
|
|
|
|
like: &str,
|
|
|
|
conj: &str,
|
|
|
|
) -> &mut Self {
|
2019-04-10 19:37:02 -04:00
|
|
|
let field = self.driver.quote_identifier(field);
|
|
|
|
|
|
|
|
let like = format!("{} {} ?", field, like);
|
|
|
|
|
2019-04-10 19:49:06 -04:00
|
|
|
let string_val = value.downcast::<String>().unwrap();
|
|
|
|
|
2019-04-10 19:37:02 -04:00
|
|
|
// @TODO Properly parse types of `value` for string formatting
|
|
|
|
let value = match position {
|
2019-04-10 19:49:06 -04:00
|
|
|
LikeWildcard::Before => format!("%{}", *string_val),
|
|
|
|
LikeWildcard::After => format!("{}%s", *string_val),
|
|
|
|
LikeWildcard::Both => format!("%{}%", *string_val),
|
2019-04-10 19:37:02 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
let conj = if self.state.query_map.len() == 0 {
|
|
|
|
" WHERE "
|
|
|
|
} else {
|
|
|
|
conj
|
|
|
|
};
|
|
|
|
|
2019-04-10 19:49:06 -04:00
|
|
|
self.state
|
|
|
|
.append_query_map(QueryClauseType::Like, conj, &like);
|
2019-04-10 19:37:02 -04:00
|
|
|
self.state.append_where_values(Box::new(value));
|
|
|
|
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2019-04-10 19:49:06 -04:00
|
|
|
fn _where(key: &str, values: Vec<Wild>) -> HashMap<String, Wild> {
|
2019-04-10 19:37:02 -04:00
|
|
|
unimplemented!();
|
|
|
|
}
|
|
|
|
|
2019-04-10 19:49:06 -04:00
|
|
|
fn _where_in(&mut self, key: &str, values: Vec<Wild>) -> &mut Self {
|
2019-04-10 19:37:02 -04:00
|
|
|
unimplemented!();
|
|
|
|
}
|
|
|
|
|
2019-04-10 19:49:06 -04:00
|
|
|
fn _where_in_string(&mut self, key: &str, values: Vec<Wild>) -> &mut Self {
|
2019-04-10 19:37:02 -04:00
|
|
|
unimplemented!();
|
|
|
|
}
|
|
|
|
|
2019-04-10 19:49:06 -04:00
|
|
|
fn _where_string(&mut self, key: &str, value: Wild) -> &mut Self {
|
2019-04-10 19:37:02 -04:00
|
|
|
unimplemented!();
|
|
|
|
}
|
|
|
|
|
2019-04-10 14:03:28 -04:00
|
|
|
fn compile(&self, query_type: QueryType, table: &str) -> String {
|
|
|
|
unimplemented!();
|
|
|
|
}
|
|
|
|
|
|
|
|
fn compile_type(&self, query_type: QueryType, table: &str) -> String {
|
2019-04-09 20:45:57 -04:00
|
|
|
unimplemented!();
|
|
|
|
}
|
2019-04-02 14:36:11 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn set_key_value() {
|
2019-04-09 14:13:37 -04:00
|
|
|
let mut qb = QueryBuilder::default();
|
2019-04-04 16:39:05 -04:00
|
|
|
|
|
|
|
qb.set("foo", Box::new("bar"));
|
2019-04-02 14:36:11 -04:00
|
|
|
|
2019-04-03 16:41:51 -04:00
|
|
|
assert_eq!(qb.state.set_array_keys[0], "foo");
|
|
|
|
assert!(qb.state.values[0].is::<&str>());
|
2019-04-03 16:29:51 -04:00
|
|
|
|
|
|
|
// @TODO find a way to make this kind of operation much more ergonomic
|
2019-04-03 16:41:51 -04:00
|
|
|
assert_eq!(*qb.state.values[0].downcast_ref::<&str>().unwrap(), "bar");
|
2019-04-02 14:36:11 -04:00
|
|
|
}
|
2019-04-02 17:23:52 -04:00
|
|
|
|
2019-04-03 16:29:51 -04:00
|
|
|
#[test]
|
|
|
|
fn set_hashmap() {
|
2019-04-09 14:13:37 -04:00
|
|
|
let mut qb = QueryBuilder::default();
|
2019-04-03 16:29:51 -04:00
|
|
|
|
2019-04-10 19:49:06 -04:00
|
|
|
let mut authors: HashMap<String, Wild> = HashMap::new();
|
2019-04-03 16:29:51 -04:00
|
|
|
authors.insert(
|
|
|
|
String::from("Chinua Achebe"),
|
2019-04-04 16:39:05 -04:00
|
|
|
Box::new(String::from("Nigeria")),
|
|
|
|
);
|
2019-04-03 16:29:51 -04:00
|
|
|
authors.insert(
|
|
|
|
String::from("Rabindranath Tagore"),
|
2019-04-04 16:39:05 -04:00
|
|
|
Box::new(String::from("India")),
|
|
|
|
);
|
|
|
|
authors.insert(String::from("Anita Nair"), Box::new(String::from("India")));
|
2019-04-03 16:29:51 -04:00
|
|
|
|
2019-04-04 16:39:05 -04:00
|
|
|
qb.set_map(authors);
|
2019-04-03 16:29:51 -04:00
|
|
|
|
2019-04-03 16:41:51 -04:00
|
|
|
// assert_eq!(qb.state.set_array_keys[0], "Chinua Achebe");
|
|
|
|
assert_eq!(qb.state.set_array_keys.len(), 3);
|
2019-04-03 16:29:51 -04:00
|
|
|
assert_eq!(qb.state.values.len(), 3);
|
|
|
|
}
|
2019-04-03 20:58:22 -04:00
|
|
|
}
|