Skip to content

Commit 163aebe

Browse files
tisonkunJichaoS
authored andcommitted
Encapsulate Insert and Delete into specific structs (apache#1224)
Signed-off-by: tison <[email protected]>
1 parent 4d9f6dc commit 163aebe

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
/// ```
@@ -2937,24 +2891,25 @@ impl fmt::Display for Statement {
29372891
}
29382892
Ok(())
29392893
}
2940-
Statement::Insert {
2941-
or,
2942-
ignore,
2943-
into,
2944-
table_name,
2945-
table_alias,
2946-
overwrite,
2947-
partitioned,
2948-
columns,
2949-
after_columns,
2950-
source,
2951-
table,
2952-
on,
2953-
returning,
2954-
replace_into,
2955-
priority,
2956-
insert_alias,
2957-
} => {
2894+
Statement::Insert(insert) => {
2895+
let Insert {
2896+
or,
2897+
ignore,
2898+
into,
2899+
table_name,
2900+
table_alias,
2901+
overwrite,
2902+
partitioned,
2903+
columns,
2904+
after_columns,
2905+
source,
2906+
table,
2907+
on,
2908+
returning,
2909+
replace_into,
2910+
priority,
2911+
insert_alias,
2912+
} = insert;
29582913
let table_name = if let Some(alias) = table_alias {
29592914
format!("{table_name} AS {alias}")
29602915
} else {
@@ -3099,15 +3054,16 @@ impl fmt::Display for Statement {
30993054
}
31003055
Ok(())
31013056
}
3102-
Statement::Delete {
3103-
tables,
3104-
from,
3105-
using,
3106-
selection,
3107-
returning,
3108-
order_by,
3109-
limit,
3110-
} => {
3057+
Statement::Delete(delete) => {
3058+
let Delete {
3059+
tables,
3060+
from,
3061+
using,
3062+
selection,
3063+
returning,
3064+
order_by,
3065+
limit,
3066+
} = delete;
31113067
write!(f, "DELETE ")?;
31123068
if !tables.is_empty() {
31133069
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)