sql_from_models_parser/ast/statement/
mod.rs

1mod display;
2
3use super::*;
4/// A top-level statement (SELECT, INSERT, CREATE, etc.)
5
6#[derive(Debug, Clone, PartialEq, Eq, Hash)]
7#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
8pub enum Statement {
9    /// Analyze (Hive)
10    Analyze(Analyze),
11    /// Truncate (Hive)
12    Truncate(Truncate),
13    /// Msck (Hive)
14    Msck(Msck),
15    /// SELECT
16    Query(Box<Query>),
17    /// INSERT
18    Insert(Insert),
19    // TODO: Support ROW FORMAT
20    Directory(Directory),
21    Copy(Copy),
22    /// UPDATE
23    Update(Update),
24    /// DELETE
25    Delete(Delete),
26    /// CREATE VIEW
27    CreateView(CreateView),
28    /// CREATE TABLE
29    CreateTable(Box<CreateTable>),
30    /// SQLite's `CREATE VIRTUAL TABLE .. USING <module_name> (<module_args>)`
31    CreateVirtualTable(CreateVirtualTable),
32    /// CREATE INDEX
33    CreateIndex(CreateIndex),
34    /// ALTER TABLE
35    AlterTable(AlterTable),
36    /// DROP
37    Drop(Drop),
38    /// SET <variable>
39    ///
40    /// Note: this is not a standard SQL statement, but it is supported by at
41    /// least MySQL and PostgreSQL. Not all MySQL-specific syntatic forms are
42    /// supported yet.
43    SetVariable(SetVariable),
44    /// SHOW <variable>
45    ///
46    /// Note: this is a PostgreSQL-specific statement.
47    ShowVariable(ShowVariable),
48    /// SHOW CREATE TABLE
49    ///
50    /// Note: this is a MySQL-specific statement.
51    ShowCreate(ShowCreate),
52    /// SHOW COLUMNS
53    ///
54    /// Note: this is a MySQL-specific statement.
55    ShowColumns(ShowColumns),
56    /// `{ BEGIN [ TRANSACTION | WORK ] | START TRANSACTION } ...`
57    StartTransaction(StartTransaction),
58    /// `SET TRANSACTION ...`
59    SetTransaction(SetTransaction),
60    /// `COMMIT [ TRANSACTION | WORK ] [ AND [ NO ] CHAIN ]`
61    Commit(Commit),
62    /// `ROLLBACK [ TRANSACTION | WORK ] [ AND [ NO ] CHAIN ]`
63    Rollback(Rollback),
64    /// CREATE SCHEMA
65    CreateSchema(CreateSchema),
66    /// CREATE DATABASE
67    CreateDatabase(CreateDatabase),
68    /// `ASSERT <condition> [AS <message>]`
69    Assert(Assert),
70    /// `DEALLOCATE [ PREPARE ] { name | ALL }`
71    ///
72    /// Note: this is a PostgreSQL-specific statement.
73    Deallocate(Deallocate),
74    /// `EXECUTE name [ ( parameter [, ...] ) ]`
75    ///
76    /// Note: this is a PostgreSQL-specific statement.
77    Execute(Execute),
78    /// `PREPARE name [ ( data_type [, ...] ) ] AS statement`
79    ///
80    /// Note: this is a PostgreSQL-specific statement.
81    Prepare(Prepare),
82    /// EXPLAIN
83    Explain(Explain),
84}
85#[derive(Debug, Clone, PartialEq, Eq, Hash)]
86#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
87pub struct Analyze {
88    pub table_name: ObjectName,
89    pub partitions: Option<Vec<Expr>>,
90    pub for_columns: bool,
91    pub columns: Vec<Ident>,
92    pub cache_metadata: bool,
93    pub noscan: bool,
94    pub compute_statistics: bool,
95}
96/// Truncate (Hive)
97#[derive(Debug, Clone, PartialEq, Eq, Hash)]
98#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
99pub struct Truncate {
100    pub table_name: ObjectName,
101    pub partitions: Option<Vec<Expr>>,
102}
103/// Msck (Hive)
104#[derive(Debug, Clone, PartialEq, Eq, Hash)]
105#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
106pub struct Msck {
107    pub table_name: ObjectName,
108    pub repair: bool,
109    pub partition_action: Option<AddDropSync>,
110}
111
112#[derive(Debug, Clone, PartialEq, Eq, Hash)]
113#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
114pub struct Insert {
115    /// Only for Sqlite
116    pub or: Option<SqliteOnConflict>,
117    /// TABLE
118    pub table_name: ObjectName,
119    /// COLUMNS
120    pub columns: Vec<Ident>,
121    /// Overwrite (Hive)
122    pub overwrite: bool,
123    /// A SQL query that specifies what to insert
124    pub source: Box<Query>,
125    /// partitioned insert (Hive)
126    pub partitioned: Option<Vec<Expr>>,
127    /// Columns defined after PARTITION
128    pub after_columns: Vec<Ident>,
129    /// whether the insert has the table keyword (Hive)
130    pub table: bool,
131}
132// TODO: Support ROW FORMAT
133#[derive(Debug, Clone, PartialEq, Eq, Hash)]
134#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
135pub struct Directory {
136    pub overwrite: bool,
137    pub local: bool,
138    pub path: String,
139    pub file_format: Option<FileFormat>,
140    pub source: Box<Query>,
141}
142#[derive(Debug, Clone, PartialEq, Eq, Hash)]
143#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
144pub struct Copy {
145    /// TABLE
146    pub table_name: ObjectName,
147    /// COLUMNS
148    pub columns: Vec<Ident>,
149    /// VALUES a vector of values to be copied
150    pub values: Vec<Option<String>>,
151}
152/// UPDATE
153#[derive(Debug, Clone, PartialEq, Eq, Hash)]
154#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
155pub struct Update {
156    /// TABLE
157    pub table_name: ObjectName,
158    /// Column assignments
159    pub assignments: Vec<Assignment>,
160    /// WHERE
161    pub selection: Option<Expr>,
162}
163/// DELETE
164#[derive(Debug, Clone, PartialEq, Eq, Hash)]
165#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
166pub struct Delete {
167    /// FROM
168    pub table_name: ObjectName,
169    /// WHERE
170    pub selection: Option<Expr>,
171}
172/// CREATE VIEW
173#[derive(Debug, Clone, PartialEq, Eq, Hash)]
174#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
175pub struct CreateView {
176    pub or_replace: bool,
177    pub materialized: bool,
178    /// View name
179    pub name: ObjectName,
180    pub columns: Vec<Ident>,
181    pub query: Box<Query>,
182    pub with_options: Vec<SqlOption>,
183}
184/// CREATE TABLE
185
186#[derive(Debug, Clone, PartialEq, Eq, Hash)]
187#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
188pub struct CreateTable {
189    pub or_replace: bool,
190    pub temporary: bool,
191    pub external: bool,
192    pub if_not_exists: bool,
193    /// Table name
194    pub name: ObjectName,
195    /// Optional schema
196    pub columns: Vec<ColumnDef>,
197    pub constraints: Vec<TableConstraint>,
198    pub hive_distribution: HiveDistributionStyle,
199    pub hive_formats: Option<HiveFormat>,
200    pub table_properties: Vec<SqlOption>,
201    pub with_options: Vec<SqlOption>,
202    pub file_format: Option<FileFormat>,
203    pub location: Option<String>,
204    pub query: Option<Box<Query>>,
205    pub without_rowid: bool,
206    pub like: Option<ObjectName>,
207}
208/// SQLite's `CREATE VIRTUAL TABLE .. USING <module_name> (<module_args>)`
209#[derive(Debug, Clone, PartialEq, Eq, Hash)]
210#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
211pub struct CreateVirtualTable {
212    pub name: ObjectName,
213    pub if_not_exists: bool,
214    pub module_name: Ident,
215    pub module_args: Vec<Ident>,
216}
217/// CREATE INDEX
218#[derive(Debug, Clone, PartialEq, Eq, Hash)]
219#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
220pub struct CreateIndex {
221    /// index name
222    pub name: ObjectName,
223    pub table_name: ObjectName,
224    pub columns: Vec<OrderByExpr>,
225    pub unique: bool,
226    pub if_not_exists: bool,
227}
228/// ALTER TABLE
229#[derive(Debug, Clone, PartialEq, Eq, Hash)]
230#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
231pub struct AlterTable {
232    /// Table name
233    pub name: ObjectName,
234    pub operation: AlterTableOperation,
235}
236/// DROP
237#[derive(Debug, Clone, PartialEq, Eq, Hash)]
238#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
239pub struct Drop {
240    /// The type of the object to drop: TABLE, VIEW, etc.
241    pub object_type: ObjectType,
242    /// An optional `IF EXISTS` clause. (Non-standard.)
243    pub if_exists: bool,
244    /// One or more objects to drop. (ANSI SQL requires exactly one.)
245    pub names: Vec<ObjectName>,
246    /// Whether `CASCADE` was specified. This will be `false` when
247    /// `RESTRICT` or no drop behavior at all was specified.
248    pub cascade: bool,
249    /// Hive allows you specify whether the table's stored data will be
250    /// deleted along with the dropped table
251    pub purge: bool,
252}
253/// SET <variable>
254///
255/// Note: this is not a standard SQL statement, but it is supported by at
256/// least MySQL and PostgreSQL. Not all MySQL-specific syntatic forms are
257/// supported yet.
258#[derive(Debug, Clone, PartialEq, Eq, Hash)]
259#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
260pub struct SetVariable {
261    pub local: bool,
262    pub hivevar: bool,
263    pub variable: Ident,
264    pub value: Vec<SetVariableValue>,
265}
266/// SHOW <variable>
267///
268/// Note: this is a PostgreSQL-specific statement.
269#[derive(Debug, Clone, PartialEq, Eq, Hash)]
270#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
271pub struct ShowVariable {
272    pub variable: Vec<Ident>,
273}
274/// SHOW CREATE TABLE
275///
276/// Note: this is a MySQL-specific statement.
277#[derive(Debug, Clone, PartialEq, Eq, Hash)]
278#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
279pub struct ShowCreate {
280    pub obj_type: ShowCreateObject,
281    pub obj_name: ObjectName,
282}
283/// SHOW COLUMNS
284///
285/// Note: this is a MySQL-specific statement.
286#[derive(Debug, Clone, PartialEq, Eq, Hash)]
287#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
288pub struct ShowColumns {
289    pub extended: bool,
290    pub full: bool,
291    pub table_name: ObjectName,
292    pub filter: Option<ShowStatementFilter>,
293}
294/// `{ BEGIN [ TRANSACTION | WORK ] | START TRANSACTION } ...`
295#[derive(Debug, Clone, PartialEq, Eq, Hash)]
296#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
297pub struct StartTransaction {
298    pub modes: Vec<TransactionMode>,
299}
300/// `SET TRANSACTION ...`
301#[derive(Debug, Clone, PartialEq, Eq, Hash)]
302#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
303pub struct SetTransaction {
304    pub modes: Vec<TransactionMode>,
305}
306/// `COMMIT [ TRANSACTION | WORK ] [ AND [ NO ] CHAIN ]`
307#[derive(Debug, Clone, PartialEq, Eq, Hash)]
308#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
309pub struct Commit {
310    pub chain: bool,
311}
312/// `ROLLBACK [ TRANSACTION | WORK ] [ AND [ NO ] CHAIN ]`
313#[derive(Debug, Clone, PartialEq, Eq, Hash)]
314#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
315pub struct Rollback {
316    pub chain: bool,
317}
318/// CREATE SCHEMA
319#[derive(Debug, Clone, PartialEq, Eq, Hash)]
320#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
321pub struct CreateSchema {
322    pub schema_name: ObjectName,
323    pub if_not_exists: bool,
324}
325/// CREATE DATABASE
326#[derive(Debug, Clone, PartialEq, Eq, Hash)]
327#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
328pub struct CreateDatabase {
329    pub db_name: ObjectName,
330    pub if_not_exists: bool,
331    pub location: Option<String>,
332    pub managed_location: Option<String>,
333}
334/// `ASSERT <condition> [AS <message>]`
335#[derive(Debug, Clone, PartialEq, Eq, Hash)]
336#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
337pub struct Assert {
338    pub condition: Expr,
339    pub message: Option<Expr>,
340}
341/// `DEALLOCATE [ PREPARE ] { name | ALL }`
342///
343/// Note: this is a PostgreSQL-specific statement.
344#[derive(Debug, Clone, PartialEq, Eq, Hash)]
345#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
346pub struct Deallocate {
347    pub name: Ident,
348    pub prepare: bool,
349}
350/// `EXECUTE name [ ( parameter [, ...] ) ]`
351///
352/// Note: this is a PostgreSQL-specific statement.
353#[derive(Debug, Clone, PartialEq, Eq, Hash)]
354#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
355pub struct Execute {
356    pub name: Ident,
357    pub parameters: Vec<Expr>,
358}
359/// `PREPARE name [ ( data_type [, ...] ) ] AS statement`
360///
361/// Note: this is a PostgreSQL-specific statement.
362#[derive(Debug, Clone, PartialEq, Eq, Hash)]
363#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
364pub struct Prepare {
365    pub name: Ident,
366    pub data_types: Vec<DataType>,
367    pub statement: Box<Statement>,
368}
369/// EXPLAIN
370#[derive(Debug, Clone, PartialEq, Eq, Hash)]
371#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
372pub struct Explain {
373    /// Carry out the command and show actual run times and other statistics.
374    pub analyze: bool,
375    // Display additional information regarding the plan.
376    pub verbose: bool,
377    /// A SQL query that specifies what to explain
378    pub statement: Box<Statement>,
379}