use keelson_core::clause::{
Combines, Fetch, HasCombines, HasFetch, HasLimit, HasLocks, HasOffset, HasOrderBy, HasTableRef,
HasWith, Limit, Locks, Offset, OrderBy, TableRef, With,
};
use keelson_core::expr::{Expr, IntoExpr, IntoExprList};
use keelson_core::{Dialect, Error, Expression, Mod, Query, QueryExtensions, QueryType, SqlWriter};
use crate::Psql;
#[derive(Debug, Clone, Default)]
pub struct TableQuery {
pub with: With,
pub table: TableRef,
pub order_by: OrderBy,
pub limit: Limit,
pub offset: Offset,
pub fetch: Fetch,
pub locks: Locks,
pub combines: Combines,
}
impl TableQuery {
pub fn new() -> TableQuery {
TableQuery::default()
}
pub fn apply(&mut self, mods: impl Mod<TableQuery>) {
mods.apply(self);
}
fn has_tail_clauses(&self) -> bool {
!self.order_by.is_empty()
|| !self.limit.is_empty()
|| !self.offset.is_empty()
|| !self.fetch.is_empty()
|| !self.locks.is_empty()
}
}
impl Expression for TableQuery {
fn write_sql(&self, w: &mut SqlWriter<'_>) {
if !self.limit.is_empty() && !self.fetch.is_empty() {
w.record_error(Error::conflicting_clauses("LIMIT", "FETCH"));
return;
}
if self.table.is_empty() {
w.record_error(Error::Incomplete("the table of a TABLE statement"));
return;
}
w.write_if(!self.with.is_empty(), "", &self.with, " ");
let parens = self
.combines
.parenthesises_leading_query(self.has_tail_clauses());
if parens {
w.push_str("(");
}
w.push_str("TABLE ");
w.write_expr(&self.table);
w.write_if(!self.order_by.is_empty(), " ", &self.order_by, "");
w.write_if(!self.limit.is_empty(), " ", &self.limit, "");
w.write_if(!self.offset.is_empty(), " ", &self.offset, "");
w.write_if(!self.fetch.is_empty(), " ", &self.fetch, "");
w.write_if(!self.locks.is_empty(), " ", &self.locks, "");
if parens {
w.push_str(")");
}
w.write_if(!self.combines.is_empty(), " ", &self.combines, "");
}
}
impl Query for TableQuery {
fn query_type(&self) -> QueryType {
QueryType::Select
}
fn dialect(&self) -> &dyn Dialect {
&Psql
}
}
impl<H, L, M> QueryExtensions<H, L, M> for TableQuery {}
impl IntoExpr for TableQuery {
fn into_expr(self) -> Expr {
crate::query(self)
}
}
impl IntoExprList for TableQuery {
fn into_expr_list(self) -> Vec<Expr> {
vec![self.into_expr()]
}
}
impl HasWith for TableQuery {
fn with_mut(&mut self) -> &mut With {
&mut self.with
}
}
impl HasTableRef for TableQuery {
fn table_ref_mut(&mut self) -> &mut TableRef {
&mut self.table
}
}
impl HasOrderBy for TableQuery {
fn order_by_mut(&mut self) -> &mut OrderBy {
&mut self.order_by
}
}
impl HasLimit for TableQuery {
fn limit_mut(&mut self) -> &mut Limit {
&mut self.limit
}
}
impl HasOffset for TableQuery {
fn offset_mut(&mut self) -> &mut Offset {
&mut self.offset
}
}
impl HasFetch for TableQuery {
fn fetch_mut(&mut self) -> &mut Fetch {
&mut self.fetch
}
}
impl HasLocks for TableQuery {
fn locks_mut(&mut self) -> &mut Locks {
&mut self.locks
}
}
impl HasCombines for TableQuery {
fn combines_mut(&mut self) -> &mut Combines {
&mut self.combines
}
}