pub mod select_core;
pub use select_core::SelectCore;
use std::default::Default;
use std::marker::PhantomData;
use serde_json::Value as Json;
use crate::traits::ModelAble;
use crate::statements::{StatementAble, Order, Limit, Offset, Lock, helpers};
use crate::nodes::{SqlLiteral};
#[derive(Clone, Debug)]
pub struct SelectStatement<M: ModelAble> {
pub cores: Vec<SelectCore<M>>,
orders: Vec<Order<M>>,
limit: Option<Limit<M>>,
offset: Option<Offset<M>>,
lock: Option<Lock<M>>,
_marker: PhantomData<M>,
}
impl<M> Default for SelectStatement<M> where M: ModelAble {
fn default() -> Self {
Self {
cores: vec![SelectCore::<M>::default()],
orders: vec![],
limit: None,
offset: None,
lock: None,
_marker: PhantomData,
}
}
}
impl<M> SelectStatement<M> where M: ModelAble {
pub fn lock(&mut self, condition: Json) -> &mut Self {
self.lock = Some(Lock::<M>::new(condition));
self
}
pub fn get_lock_sql(&self) -> Option<SqlLiteral> {
if let Some(lock) = &self.lock {
Some(SqlLiteral::new(lock.to_sql()))
} else {
None
}
}
pub fn order(&mut self, condition: Json) -> &mut Self {
self.orders.push(Order::new(condition));
self
}
pub fn get_order_sql(&self) -> Option<SqlLiteral> {
if self.orders.len() == 0 {
None
} else {
Some(SqlLiteral::new(helpers::inject_join(&self.orders, ", ")))
}
}
pub fn limit(&mut self, condition: usize) -> &mut Self {
self.limit = Some(Limit::new(condition));
self
}
pub fn get_limit_sql(&self) -> Option<SqlLiteral> {
if let Some(limit) = &self.limit {
Some(SqlLiteral::new(limit.to_sql()))
} else {
None
}
}
pub fn offset(&mut self, condition: usize) -> &mut Self {
self.offset = Some(Offset::new(condition));
self
}
pub fn get_offset_sql(&self) -> Option<SqlLiteral> {
if let Some(offset) = &self.offset {
Some(SqlLiteral::new(offset.to_sql()))
} else {
None
}
}
}