stringqb/src/query_builder/query_state.rs

225 lines
4.9 KiB
Rust

use super::*;
#[derive(Debug)]
pub struct QueryClause {
clause_type: QueryClauseType,
conjunction: String,
string: String,
}
impl QueryClause {
pub fn new(clause_type: QueryClauseType, conjunction: &str, string: &str) -> Self {
QueryClause {
clause_type,
conjunction: conjunction.to_string(),
string: string.to_string(),
}
}
pub fn to_string(&self) -> String {
format!("{}{}", self.conjunction, self.string)
}
}
#[derive(Debug)]
pub struct QueryState {
select_string: String,
from_string: String,
set_string: String,
order_string: String,
group_string: String,
// Keys for insert/update statement
set_array_keys: Vec<String>,
// Order by clause
order_map: HashMap<String, String>,
// Group by clause
group_array: Vec<String>,
// Values to apply to prepared statements
values: Vec<Box<Any>>,
// Values to apply to where clauses in prepared statements
where_values: Vec<Box<Any>>,
pub limit: Option<usize>,
pub offset: Option<usize>,
// Query components for complex selects
query_map: Vec<QueryClause>,
// Query components for having clauses
having_map: Vec<QueryClause>,
}
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_map: HashMap::new(),
group_array: vec![],
values: vec![],
where_values: vec![],
limit: None,
offset: None,
query_map: vec![],
having_map: vec![],
}
}
}
impl QueryState {
pub fn new() -> Self {
QueryState::default()
}
pub fn append_select_string(&mut self, s: &str) -> &mut Self {
self.select_string += s;
self
}
pub fn prepend_select_string(&mut self, s: &str) -> &mut Self {
self.select_string = String::from(s) + &self.select_string;
self
}
pub fn append_group_array(&mut self, field: &str) -> &mut Self {
self.group_array.push(String::from(field));
self
}
pub fn append_having_map(
&mut self,
clause_type: QueryClauseType,
conj: &str,
s: &str,
) -> &mut Self {
self.having_map.push(QueryClause::new(clause_type, conj, s));
self
}
pub fn append_order_map(&mut self, key: &str, dir: &str) -> &mut Self {
self.order_map.insert(String::from(key), String::from(dir));
self
}
pub fn append_set_array_keys(&mut self, key: &str) -> &mut Self {
self.set_array_keys.push(key.to_string());
self
}
pub fn append_values(&mut self, val: Box<Any>) -> &mut Self {
self.values.push(val);
self
}
pub fn append_where_values(&mut self, val: Box<Any>) -> &mut Self {
self.where_values.push(val);
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 get_from_string(&self) -> &str {
&self.from_string
}
pub fn get_group_array(&self) -> &Vec<String> {
&self.group_array
}
pub fn get_group_string(&self) -> &str {
&self.group_string
}
pub fn get_having_map(&self) -> &Vec<QueryClause> {
&self.having_map
}
pub fn get_query_map(&self) -> &Vec<QueryClause> {
&self.query_map
}
pub fn get_select_string(&self) -> &str {
&self.select_string
}
pub fn get_set_array_keys(&self) -> &Vec<String> {
&self.set_array_keys
}
pub fn get_set_string(&self) -> &str {
&self.set_string
}
pub fn get_order_map(&self) -> &HashMap<String, String> {
&self.order_map
}
pub fn get_order_string(&self) -> &str {
&self.order_string
}
pub fn get_values(&self) -> &Vec<Box<Any>> {
&self.values
}
pub fn get_where_values(&self) -> &Vec<Box<Any>> {
&self.where_values
}
pub fn having_map_empty(&self) -> bool {
self.having_map.len() == 0
}
pub fn set_from_string(&mut self, s: &str) -> &mut Self {
self.from_string = String::from(s);
self
}
pub fn set_group_string(&mut self, s: &str) -> &mut Self {
self.group_string = String::from(s);
self
}
pub fn set_order_string(&mut self, order_string: &str) -> &mut Self {
self.order_string = String::from(order_string);
self
}
pub fn query_map_empty(&self) -> bool {
self.query_map.len() == 0
}
}