Skip to content

Commit bf89b7d

Browse files
authored
Encapsulate Insert and Delete into specific structs (#1224)
Signed-off-by: tison <[email protected]>
1 parent d2c2b15 commit bf89b7d

File tree

7 files changed

+187
-147
lines changed

7 files changed

+187
-147
lines changed

src/ast/dml.rs

+84
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
// Licensed under the Apache License, Version 2.0 (the "License");
2+
// you may not use this file except in compliance with the License.
3+
// You may obtain a copy of the License at
4+
//
5+
// http://www.apache.org/licenses/LICENSE-2.0
6+
//
7+
// Unless required by applicable law or agreed to in writing, software
8+
// distributed under the License is distributed on an "AS IS" BASIS,
9+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
// See the License for the specific language governing permissions and
11+
// limitations under the License.
12+
13+
#[cfg(not(feature = "std"))]
14+
use alloc::{boxed::Box, vec::Vec};
15+
16+
#[cfg(feature = "serde")]
17+
use serde::{Deserialize, Serialize};
18+
#[cfg(feature = "visitor")]
19+
use sqlparser_derive::{Visit, VisitMut};
20+
21+
use super::{
22+
Expr, FromTable, Ident, InsertAliases, MysqlInsertPriority, ObjectName, OnInsert, OrderByExpr,
23+
Query, SelectItem, SqliteOnConflict, TableWithJoins,
24+
};
25+
26+
/// INSERT statement.
27+
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
28+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
29+
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
30+
pub struct Insert {
31+
/// Only for Sqlite
32+
pub or: Option<SqliteOnConflict>,
33+
/// Only for mysql
34+
pub ignore: bool,
35+
/// INTO - optional keyword
36+
pub into: bool,
37+
/// TABLE
38+
#[cfg_attr(feature = "visitor", visit(with = "visit_relation"))]
39+
pub table_name: ObjectName,
40+
/// table_name as foo (for PostgreSQL)
41+
pub table_alias: Option<Ident>,
42+
/// COLUMNS
43+
pub columns: Vec<Ident>,
44+
/// Overwrite (Hive)
45+
pub overwrite: bool,
46+
/// A SQL query that specifies what to insert
47+
pub source: Option<Box<Query>>,
48+
/// partitioned insert (Hive)
49+
pub partitioned: Option<Vec<Expr>>,
50+
/// Columns defined after PARTITION
51+
pub after_columns: Vec<Ident>,
52+
/// whether the insert has the table keyword (Hive)
53+
pub table: bool,
54+
pub on: Option<OnInsert>,
55+
/// RETURNING
56+
pub returning: Option<Vec<SelectItem>>,
57+
/// Only for mysql
58+
pub replace_into: bool,
59+
/// Only for mysql
60+
pub priority: Option<MysqlInsertPriority>,
61+
/// Only for mysql
62+
pub insert_alias: Option<InsertAliases>,
63+
}
64+
65+
/// DELETE statement.
66+
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
67+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
68+
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
69+
pub struct Delete {
70+
/// Multi tables delete are supported in mysql
71+
pub tables: Vec<ObjectName>,
72+
/// FROM
73+
pub from: FromTable,
74+
/// USING (Snowflake, Postgres, MySQL)
75+
pub using: Option<Vec<TableWithJoins>>,
76+
/// WHERE
77+
pub selection: Option<Expr>,
78+
/// RETURNING
79+
pub returning: Option<Vec<SelectItem>>,
80+
/// ORDER BY (MySQL)
81+
pub order_by: Vec<OrderByExpr>,
82+
/// LIMIT (MySQL)
83+
pub limit: Option<Expr>,
84+
}

src/ast/mod.rs

+33-77
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ pub use self::ddl::{
3737
ReferentialAction, TableConstraint, UserDefinedTypeCompositeAttributeDef,
3838
UserDefinedTypeRepresentation, ViewColumnDef,
3939
};
40+
pub use self::dml::{Delete, Insert};
4041
pub use self::operator::{BinaryOperator, UnaryOperator};
4142
pub use self::query::{
4243
Cte, CteAsMaterialized, Distinct, ExceptSelectItem, ExcludeSelectItem, Fetch, ForClause,
@@ -60,6 +61,7 @@ pub use visitor::*;
6061
mod data_type;
6162
mod dcl;
6263
mod ddl;
64+
mod dml;
6365
pub mod helpers;
6466
mod operator;
6567
mod query;
@@ -1800,40 +1802,7 @@ pub enum Statement {
18001802
/// ```sql
18011803
/// INSERT
18021804
/// ```
1803-
Insert {
1804-
/// Only for Sqlite
1805-
or: Option<SqliteOnConflict>,
1806-
/// Only for mysql
1807-
ignore: bool,
1808-
/// INTO - optional keyword
1809-
into: bool,
1810-
/// TABLE
1811-
#[cfg_attr(feature = "visitor", visit(with = "visit_relation"))]
1812-
table_name: ObjectName,
1813-
/// table_name as foo (for PostgreSQL)
1814-
table_alias: Option<Ident>,
1815-
/// COLUMNS
1816-
columns: Vec<Ident>,
1817-
/// Overwrite (Hive)
1818-
overwrite: bool,
1819-
/// A SQL query that specifies what to insert
1820-
source: Option<Box<Query>>,
1821-
/// partitioned insert (Hive)
1822-
partitioned: Option<Vec<Expr>>,
1823-
/// Columns defined after PARTITION
1824-
after_columns: Vec<Ident>,
1825-
/// whether the insert has the table keyword (Hive)
1826-
table: bool,
1827-
on: Option<OnInsert>,
1828-
/// RETURNING
1829-
returning: Option<Vec<SelectItem>>,
1830-
/// Only for mysql
1831-
replace_into: bool,
1832-
/// Only for mysql
1833-
priority: Option<MysqlInsertPriority>,
1834-
/// Only for mysql
1835-
insert_alias: Option<InsertAliases>,
1836-
},
1805+
Insert(Insert),
18371806
/// ```sql
18381807
/// INSTALL
18391808
/// ```
@@ -1923,22 +1892,7 @@ pub enum Statement {
19231892
/// ```sql
19241893
/// DELETE
19251894
/// ```
1926-
Delete {
1927-
/// Multi tables delete are supported in mysql
1928-
tables: Vec<ObjectName>,
1929-
/// FROM
1930-
from: FromTable,
1931-
/// USING (Snowflake, Postgres, MySQL)
1932-
using: Option<Vec<TableWithJoins>>,
1933-
/// WHERE
1934-
selection: Option<Expr>,
1935-
/// RETURNING
1936-
returning: Option<Vec<SelectItem>>,
1937-
/// ORDER BY (MySQL)
1938-
order_by: Vec<OrderByExpr>,
1939-
/// LIMIT (MySQL)
1940-
limit: Option<Expr>,
1941-
},
1895+
Delete(Delete),
19421896
/// ```sql
19431897
/// CREATE VIEW
19441898
/// ```
@@ -2912,24 +2866,25 @@ impl fmt::Display for Statement {
29122866
}
29132867
Ok(())
29142868
}
2915-
Statement::Insert {
2916-
or,
2917-
ignore,
2918-
into,
2919-
table_name,
2920-
table_alias,
2921-
overwrite,
2922-
partitioned,
2923-
columns,
2924-
after_columns,
2925-
source,
2926-
table,
2927-
on,
2928-
returning,
2929-
replace_into,
2930-
priority,
2931-
insert_alias,
2932-
} => {
2869+
Statement::Insert(insert) => {
2870+
let Insert {
2871+
or,
2872+
ignore,
2873+
into,
2874+
table_name,
2875+
table_alias,
2876+
overwrite,
2877+
partitioned,
2878+
columns,
2879+
after_columns,
2880+
source,
2881+
table,
2882+
on,
2883+
returning,
2884+
replace_into,
2885+
priority,
2886+
insert_alias,
2887+
} = insert;
29332888
let table_name = if let Some(alias) = table_alias {
29342889
format!("{table_name} AS {alias}")
29352890
} else {
@@ -3074,15 +3029,16 @@ impl fmt::Display for Statement {
30743029
}
30753030
Ok(())
30763031
}
3077-
Statement::Delete {
3078-
tables,
3079-
from,
3080-
using,
3081-
selection,
3082-
returning,
3083-
order_by,
3084-
limit,
3085-
} => {
3032+
Statement::Delete(delete) => {
3033+
let Delete {
3034+
tables,
3035+
from,
3036+
using,
3037+
selection,
3038+
returning,
3039+
order_by,
3040+
limit,
3041+
} = delete;
30863042
write!(f, "DELETE ")?;
30873043
if !tables.is_empty() {
30883044
write!(f, "{} ", display_comma_separated(tables))?;

src/parser/mod.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -7082,7 +7082,7 @@ impl<'a> Parser<'a> {
70827082
None
70837083
};
70847084

7085-
Ok(Statement::Delete {
7085+
Ok(Statement::Delete(Delete {
70867086
tables,
70877087
from: if with_from_keyword {
70887088
FromTable::WithFromKeyword(from)
@@ -7094,7 +7094,7 @@ impl<'a> Parser<'a> {
70947094
returning,
70957095
order_by,
70967096
limit,
7097-
})
7097+
}))
70987098
}
70997099

71007100
// KILL [CONNECTION | QUERY | MUTATION] processlist_id
@@ -8658,7 +8658,7 @@ impl<'a> Parser<'a> {
86588658
}
86598659

86608660
let insert = &mut self.parse_insert()?;
8661-
if let Statement::Insert { replace_into, .. } = insert {
8661+
if let Statement::Insert(Insert { replace_into, .. }) = insert {
86628662
*replace_into = true;
86638663
}
86648664

@@ -8826,7 +8826,7 @@ impl<'a> Parser<'a> {
88268826
None
88278827
};
88288828

8829-
Ok(Statement::Insert {
8829+
Ok(Statement::Insert(Insert {
88308830
or,
88318831
table_name,
88328832
table_alias,
@@ -8843,7 +8843,7 @@ impl<'a> Parser<'a> {
88438843
replace_into,
88448844
priority,
88458845
insert_alias,
8846-
})
8846+
}))
88478847
}
88488848
}
88498849

tests/sqlparser_bigquery.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -90,10 +90,10 @@ fn parse_raw_literal() {
9090
fn parse_delete_statement() {
9191
let sql = "DELETE \"table\" WHERE 1";
9292
match bigquery_and_generic().verified_stmt(sql) {
93-
Statement::Delete {
93+
Statement::Delete(Delete {
9494
from: FromTable::WithoutKeyword(from),
9595
..
96-
} => {
96+
}) => {
9797
assert_eq!(
9898
TableFactor::Table {
9999
name: ObjectName(vec![Ident::with_quote('"', "table")]),

0 commit comments

Comments
 (0)