keelson_mysql/statement/
table.rs1use keelson_core::clause::{
2 HasLimit, HasOffset, HasOrderBy, HasTableRef, Limit, Offset, OrderBy, TableRef,
3};
4use keelson_core::expr::{Expr, IntoExpr, IntoExprList};
5use keelson_core::{Dialect, Error, Expression, Mod, Query, QueryExtensions, QueryType, SqlWriter};
6
7use crate::Mysql;
8
9#[derive(Debug, Clone, Default)]
22pub struct TableQuery {
23 pub table: TableRef,
25 pub order_by: OrderBy,
27 pub limit: Limit,
29 pub offset: Offset,
31}
32
33impl TableQuery {
34 pub fn new() -> TableQuery {
36 TableQuery::default()
37 }
38
39 pub fn apply(&mut self, mods: impl Mod<TableQuery>) {
41 mods.apply(self);
42 }
43}
44
45impl Expression for TableQuery {
46 fn write_sql(&self, w: &mut SqlWriter<'_>) {
47 if self.table.is_empty() {
48 w.record_error(Error::Incomplete("the table of a TABLE statement"));
49 return;
50 }
51
52 w.push_str("TABLE ");
53 w.write_expr(&self.table);
54
55 w.write_if(!self.order_by.is_empty(), " ", &self.order_by, "");
56 w.write_if(!self.limit.is_empty(), " ", &self.limit, "");
57 w.write_if(!self.offset.is_empty(), " ", &self.offset, "");
58 }
59}
60
61impl Query for TableQuery {
62 fn query_type(&self) -> QueryType {
63 QueryType::Select
65 }
66
67 fn dialect(&self) -> &dyn Dialect {
68 &Mysql
69 }
70}
71
72impl<H, L, M> QueryExtensions<H, L, M> for TableQuery {}
73
74impl IntoExpr for TableQuery {
75 fn into_expr(self) -> Expr {
76 crate::query(self)
77 }
78}
79
80impl IntoExprList for TableQuery {
81 fn into_expr_list(self) -> Vec<Expr> {
82 vec![self.into_expr()]
83 }
84}
85
86impl HasTableRef for TableQuery {
87 fn table_ref_mut(&mut self) -> &mut TableRef {
88 &mut self.table
89 }
90}
91
92impl HasOrderBy for TableQuery {
93 fn order_by_mut(&mut self) -> &mut OrderBy {
94 &mut self.order_by
95 }
96}
97
98impl HasLimit for TableQuery {
99 fn limit_mut(&mut self) -> &mut Limit {
100 &mut self.limit
101 }
102}
103
104impl HasOffset for TableQuery {
105 fn offset_mut(&mut self) -> &mut Offset {
106 &mut self.offset
107 }
108}