mod aggregate;
mod bind;
mod builder;
mod generator;
mod predicate;
mod read;
mod render;
mod write;
use std::collections::HashMap;
use sqlx::mysql::MySqlPool;
use crate::mysql::condition::Condition;
use crate::mysql::field::{FieldType, JoinClause, OrderClause};
pub(crate) use bind::bind_param;
pub(crate) use generator::SqlGenerator;
#[cfg(test)]
pub(crate) use predicate::predicate_value;
#[derive(Debug, Clone, Copy)]
enum UnionOperator {
Distinct,
All,
}
#[derive(Debug, Clone, Copy)]
pub(crate) enum ArithmeticOperator {
Add,
Subtract,
}
impl ArithmeticOperator {
const fn as_sql(self) -> &'static str {
match self {
Self::Add => "+",
Self::Subtract => "-",
}
}
}
enum QueryExecutor<'a> {
Pool(&'a MySqlPool),
Transaction(&'a mut crate::mysql::Transaction),
}
pub struct QueryBuilder<'a> {
executor: QueryExecutor<'a>,
table: String,
pub(crate) fields: Vec<String>,
#[allow(dead_code)]
pub(crate) conditions: Vec<Condition>,
#[allow(dead_code)]
pub(crate) joins: Vec<JoinClause>,
#[allow(dead_code)]
pub(crate) order_by: Vec<OrderClause>,
#[allow(dead_code)]
pub(crate) group_by: Vec<String>,
#[allow(dead_code)]
pub(crate) having_clause: Vec<Condition>,
pub(crate) limit: Option<u64>,
pub(crate) offset: Option<u64>,
pub(crate) distinct: bool,
unions: Vec<(UnionOperator, Box<QueryBuilder<'a>>)>,
pub(crate) field_types: HashMap<String, FieldType>,
expr_assignments: Vec<(String, crate::SqlExpr)>,
select_exprs: Vec<(crate::SqlExpr, String)>,
#[allow(dead_code)]
enable_logging: bool,
}
impl<'a> QueryBuilder<'a> {
pub fn from_pool(pool: &'a MySqlPool, table: &crate::TableRef) -> Self {
Self::new(pool, table.as_str(), false)
}
pub(crate) fn new(pool: &'a MySqlPool, table_name: &str, enable_logging: bool) -> Self {
Self {
executor: QueryExecutor::Pool(pool),
table: table_name.to_string(),
fields: Vec::new(),
conditions: Vec::new(),
joins: Vec::new(),
order_by: Vec::new(),
group_by: Vec::new(),
having_clause: Vec::new(),
limit: None,
offset: None,
distinct: false,
unions: Vec::new(),
field_types: HashMap::new(),
expr_assignments: Vec::new(),
select_exprs: Vec::new(),
enable_logging,
}
}
pub(crate) fn new_transaction(
transaction: &'a mut crate::mysql::Transaction,
table_name: &str,
enable_logging: bool,
) -> Self {
Self {
executor: QueryExecutor::Transaction(transaction),
table: table_name.to_string(),
fields: Vec::new(),
conditions: Vec::new(),
joins: Vec::new(),
order_by: Vec::new(),
group_by: Vec::new(),
having_clause: Vec::new(),
limit: None,
offset: None,
distinct: false,
unions: Vec::new(),
field_types: HashMap::new(),
expr_assignments: Vec::new(),
select_exprs: Vec::new(),
enable_logging,
}
}
}