Skip to main content

radixdb_sql/ast/
control.rs

1// Copyright 2026 RadixDB Contributors
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use super::*;
16
17/// BEGIN statement
18#[derive(Debug, Clone, PartialEq)]
19pub struct BeginStatement {
20    pub token: Token,
21    pub isolation_level: Option<SmartString>,
22}
23
24impl fmt::Display for BeginStatement {
25    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
26        let mut result = String::from("BEGIN TRANSACTION");
27        if let Some(ref level) = self.isolation_level {
28            result.push_str(&format!(" ISOLATION LEVEL {}", level));
29        }
30        write!(f, "{}", result)
31    }
32}
33
34/// COMMIT statement
35#[derive(Debug, Clone, PartialEq)]
36pub struct CommitStatement {
37    pub token: Token,
38}
39
40impl fmt::Display for CommitStatement {
41    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
42        write!(f, "COMMIT")
43    }
44}
45
46/// ROLLBACK statement
47#[derive(Debug, Clone, PartialEq)]
48pub struct RollbackStatement {
49    pub token: Token,
50    pub savepoint_name: Option<Identifier>,
51}
52
53impl fmt::Display for RollbackStatement {
54    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
55        if let Some(ref name) = self.savepoint_name {
56            write!(f, "ROLLBACK TO SAVEPOINT {}", name)
57        } else {
58            write!(f, "ROLLBACK")
59        }
60    }
61}
62
63/// SAVEPOINT statement
64#[derive(Debug, Clone, PartialEq)]
65pub struct SavepointStatement {
66    pub token: Token,
67    pub savepoint_name: Identifier,
68}
69
70impl fmt::Display for SavepointStatement {
71    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
72        write!(f, "SAVEPOINT {}", self.savepoint_name)
73    }
74}
75
76/// RELEASE SAVEPOINT statement
77#[derive(Debug, Clone, PartialEq)]
78pub struct ReleaseSavepointStatement {
79    pub token: Token,
80    pub savepoint_name: Identifier,
81}
82
83impl fmt::Display for ReleaseSavepointStatement {
84    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
85        write!(f, "RELEASE SAVEPOINT {}", self.savepoint_name)
86    }
87}
88
89/// SET statement
90#[derive(Debug, Clone, PartialEq)]
91pub struct SetStatement {
92    pub token: Token,
93    pub name: Identifier,
94    pub value: Expression,
95}
96
97impl fmt::Display for SetStatement {
98    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
99        write!(f, "SET {} = {}", self.name, self.value)
100    }
101}
102
103/// PRAGMA statement
104#[derive(Debug, Clone, PartialEq)]
105pub struct PragmaStatement {
106    pub token: Token,
107    pub name: Identifier,
108    pub value: Option<Expression>,
109}
110
111impl fmt::Display for PragmaStatement {
112    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
113        if let Some(ref value) = self.value {
114            write!(f, "PRAGMA {} = {}", self.name, value)
115        } else {
116            write!(f, "PRAGMA {}", self.name)
117        }
118    }
119}
120
121/// SHOW TABLES statement
122#[derive(Debug, Clone, PartialEq)]
123pub struct ShowTablesStatement {
124    pub token: Token,
125}
126
127impl fmt::Display for ShowTablesStatement {
128    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
129        write!(f, "SHOW TABLES")
130    }
131}
132
133/// SHOW VIEWS statement
134#[derive(Debug, Clone, PartialEq)]
135pub struct ShowViewsStatement {
136    pub token: Token,
137}
138
139impl fmt::Display for ShowViewsStatement {
140    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
141        write!(f, "SHOW VIEWS")
142    }
143}
144
145/// SHOW CREATE TABLE statement
146#[derive(Debug, Clone, PartialEq)]
147pub struct ShowCreateTableStatement {
148    pub token: Token,
149    pub table_name: Identifier,
150}
151
152impl fmt::Display for ShowCreateTableStatement {
153    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
154        write!(f, "SHOW CREATE TABLE {}", self.table_name)
155    }
156}
157
158/// SHOW CREATE VIEW statement
159#[derive(Debug, Clone, PartialEq)]
160pub struct ShowCreateViewStatement {
161    pub token: Token,
162    pub view_name: Identifier,
163}
164
165impl fmt::Display for ShowCreateViewStatement {
166    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
167        write!(f, "SHOW CREATE VIEW {}", self.view_name)
168    }
169}
170
171/// SHOW INDEXES statement
172#[derive(Debug, Clone, PartialEq)]
173pub struct ShowIndexesStatement {
174    pub token: Token,
175    pub table_name: Identifier,
176}
177
178impl fmt::Display for ShowIndexesStatement {
179    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
180        write!(f, "SHOW INDEXES FROM {}", self.table_name)
181    }
182}
183
184/// DESCRIBE target.
185#[derive(Debug, Clone, PartialEq)]
186pub enum DescribeTarget {
187    Table(Identifier),
188    Database,
189}
190
191/// DESCRIBE output representation.
192#[derive(Debug, Clone, Copy, PartialEq, Eq)]
193pub enum DescribeFormat {
194    Tabular,
195    Json,
196}
197
198/// DESCRIBE statement - shows table structure or a versioned JSON catalog.
199#[derive(Debug, Clone, PartialEq)]
200pub struct DescribeStatement {
201    pub token: Token,
202    pub target: DescribeTarget,
203    pub format: DescribeFormat,
204}
205
206impl fmt::Display for DescribeStatement {
207    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
208        match (&self.target, self.format) {
209            (DescribeTarget::Table(table), DescribeFormat::Tabular) => {
210                write!(f, "DESCRIBE {table}")
211            }
212            (DescribeTarget::Table(table), DescribeFormat::Json) => {
213                write!(f, "DESCRIBE TABLE {table} FORMAT JSON")
214            }
215            (DescribeTarget::Database, DescribeFormat::Json) => {
216                write!(f, "DESCRIBE DATABASE FORMAT JSON")
217            }
218            (DescribeTarget::Database, DescribeFormat::Tabular) => {
219                write!(f, "DESCRIBE DATABASE")
220            }
221        }
222    }
223}
224
225/// Expression statement (standalone expression)
226#[derive(Debug, Clone, PartialEq)]
227pub struct ExpressionStatement {
228    pub token: Token,
229    pub expression: Expression,
230}
231
232impl fmt::Display for ExpressionStatement {
233    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
234        write!(f, "{}", self.expression)
235    }
236}
237
238/// EXPLAIN statement
239#[derive(Debug, Clone, PartialEq)]
240pub struct ExplainStatement {
241    pub token: Token,
242    pub statement: Box<Statement>,
243    pub analyze: bool,
244}
245
246impl fmt::Display for ExplainStatement {
247    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
248        if self.analyze {
249            write!(f, "EXPLAIN ANALYZE {}", self.statement)
250        } else {
251            write!(f, "EXPLAIN {}", self.statement)
252        }
253    }
254}
255
256/// ANALYZE statement for collecting table statistics
257#[derive(Debug, Clone, PartialEq)]
258pub struct AnalyzeStatement {
259    pub token: Token,
260    /// Table name to analyze (None = analyze all tables)
261    pub table_name: Option<SmartString>,
262}
263
264impl fmt::Display for AnalyzeStatement {
265    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
266        match &self.table_name {
267            Some(name) => write!(f, "ANALYZE {}", name),
268            None => write!(f, "ANALYZE"),
269        }
270    }
271}