use super::{Expr, Value};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SortDir {
Asc,
Desc,
}
#[derive(Debug, Clone)]
pub struct SelectQuery {
pub table: String,
pub columns: Vec<String>,
pub filters: Vec<Expr>,
pub order: Vec<(String, SortDir)>,
pub limit: Option<u32>,
pub offset: Option<u32>,
}
impl SelectQuery {
pub fn new(table: impl Into<String>) -> Self {
Self {
table: table.into(),
columns: Vec::new(),
filters: Vec::new(),
order: Vec::new(),
limit: None,
offset: None,
}
}
pub fn columns(mut self, cols: impl IntoIterator<Item = impl Into<String>>) -> Self {
self.columns = cols.into_iter().map(Into::into).collect();
self
}
pub fn filter(mut self, expr: Expr) -> Self {
self.filters.push(expr);
self
}
pub fn order_by(mut self, column: impl Into<String>, dir: SortDir) -> Self {
self.order.push((column.into(), dir));
self
}
pub fn limit(mut self, n: u32) -> Self {
self.limit = Some(n);
self
}
pub fn offset(mut self, n: u32) -> Self {
self.offset = Some(n);
self
}
}
#[derive(Debug, Clone)]
pub struct InsertQuery {
pub table: String,
pub columns: Vec<String>,
pub values: Vec<Value>,
pub returning: Vec<String>,
}
impl InsertQuery {
pub fn new(table: impl Into<String>) -> Self {
Self {
table: table.into(),
columns: Vec::new(),
values: Vec::new(),
returning: Vec::new(),
}
}
pub fn values(
mut self,
data: impl IntoIterator<Item = (impl Into<String>, impl Into<Value>)>,
) -> Self {
let (cols, vals): (Vec<_>, Vec<_>) =
data.into_iter().map(|(c, v)| (c.into(), v.into())).unzip();
self.columns = cols;
self.values = vals;
self
}
pub fn returning(mut self, cols: impl IntoIterator<Item = impl Into<String>>) -> Self {
self.returning = cols.into_iter().map(Into::into).collect();
self
}
pub fn returning_all(mut self) -> Self {
self.returning = vec!["*".to_string()];
self
}
}
#[derive(Debug, Clone)]
pub struct UpdateQuery {
pub table: String,
pub changes: Vec<(String, Value)>,
pub filters: Vec<Expr>,
pub returning: Vec<String>,
}
impl UpdateQuery {
pub fn new(table: impl Into<String>) -> Self {
Self {
table: table.into(),
changes: Vec::new(),
filters: Vec::new(),
returning: Vec::new(),
}
}
pub fn set(
mut self,
data: impl IntoIterator<Item = (impl Into<String>, impl Into<Value>)>,
) -> Self {
self.changes = data
.into_iter()
.map(|(c, v)| (c.into(), v.into()))
.collect();
self
}
pub fn filter(mut self, expr: Expr) -> Self {
self.filters.push(expr);
self
}
pub fn returning(mut self, cols: impl IntoIterator<Item = impl Into<String>>) -> Self {
self.returning = cols.into_iter().map(Into::into).collect();
self
}
pub fn returning_all(mut self) -> Self {
self.returning = vec!["*".to_string()];
self
}
}
#[derive(Debug, Clone)]
pub struct DeleteQuery {
pub table: String,
pub filters: Vec<Expr>,
pub returning: Vec<String>,
}
impl DeleteQuery {
pub fn new(table: impl Into<String>) -> Self {
Self {
table: table.into(),
filters: Vec::new(),
returning: Vec::new(),
}
}
pub fn filter(mut self, expr: Expr) -> Self {
self.filters.push(expr);
self
}
pub fn returning(mut self, cols: impl IntoIterator<Item = impl Into<String>>) -> Self {
self.returning = cols.into_iter().map(Into::into).collect();
self
}
pub fn returning_all(mut self) -> Self {
self.returning = vec!["*".to_string()];
self
}
}