Skip to content

Encapsulate Insert and Delete into specific structs #1224

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 84 additions & 0 deletions src/ast/dml.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#[cfg(not(feature = "std"))]
use alloc::{boxed::Box, vec::Vec};

#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
#[cfg(feature = "visitor")]
use sqlparser_derive::{Visit, VisitMut};

use super::{
Expr, FromTable, Ident, InsertAliases, MysqlInsertPriority, ObjectName, OnInsert, OrderByExpr,
Query, SelectItem, SqliteOnConflict, TableWithJoins,
};

/// INSERT statement.
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
pub struct Insert {
/// Only for Sqlite
pub or: Option<SqliteOnConflict>,
/// Only for mysql
pub ignore: bool,
/// INTO - optional keyword
pub into: bool,
/// TABLE
#[cfg_attr(feature = "visitor", visit(with = "visit_relation"))]
pub table_name: ObjectName,
/// table_name as foo (for PostgreSQL)
pub table_alias: Option<Ident>,
/// COLUMNS
pub columns: Vec<Ident>,
/// Overwrite (Hive)
pub overwrite: bool,
/// A SQL query that specifies what to insert
pub source: Option<Box<Query>>,
/// partitioned insert (Hive)
pub partitioned: Option<Vec<Expr>>,
/// Columns defined after PARTITION
pub after_columns: Vec<Ident>,
/// whether the insert has the table keyword (Hive)
pub table: bool,
pub on: Option<OnInsert>,
/// RETURNING
pub returning: Option<Vec<SelectItem>>,
/// Only for mysql
pub replace_into: bool,
/// Only for mysql
pub priority: Option<MysqlInsertPriority>,
/// Only for mysql
pub insert_alias: Option<InsertAliases>,
}

/// DELETE statement.
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
pub struct Delete {
/// Multi tables delete are supported in mysql
pub tables: Vec<ObjectName>,
/// FROM
pub from: FromTable,
/// USING (Snowflake, Postgres, MySQL)
pub using: Option<Vec<TableWithJoins>>,
/// WHERE
pub selection: Option<Expr>,
/// RETURNING
pub returning: Option<Vec<SelectItem>>,
/// ORDER BY (MySQL)
pub order_by: Vec<OrderByExpr>,
/// LIMIT (MySQL)
pub limit: Option<Expr>,
}
110 changes: 33 additions & 77 deletions src/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ pub use self::ddl::{
ReferentialAction, TableConstraint, UserDefinedTypeCompositeAttributeDef,
UserDefinedTypeRepresentation, ViewColumnDef,
};
pub use self::dml::{Delete, Insert};
pub use self::operator::{BinaryOperator, UnaryOperator};
pub use self::query::{
Cte, CteAsMaterialized, Distinct, ExceptSelectItem, ExcludeSelectItem, Fetch, ForClause,
Expand All @@ -60,6 +61,7 @@ pub use visitor::*;
mod data_type;
mod dcl;
mod ddl;
mod dml;
pub mod helpers;
mod operator;
mod query;
Expand Down Expand Up @@ -1800,40 +1802,7 @@ pub enum Statement {
/// ```sql
/// INSERT
/// ```
Insert {
/// Only for Sqlite
or: Option<SqliteOnConflict>,
/// Only for mysql
ignore: bool,
/// INTO - optional keyword
into: bool,
/// TABLE
#[cfg_attr(feature = "visitor", visit(with = "visit_relation"))]
table_name: ObjectName,
/// table_name as foo (for PostgreSQL)
table_alias: Option<Ident>,
/// COLUMNS
columns: Vec<Ident>,
/// Overwrite (Hive)
overwrite: bool,
/// A SQL query that specifies what to insert
source: Option<Box<Query>>,
/// partitioned insert (Hive)
partitioned: Option<Vec<Expr>>,
/// Columns defined after PARTITION
after_columns: Vec<Ident>,
/// whether the insert has the table keyword (Hive)
table: bool,
on: Option<OnInsert>,
/// RETURNING
returning: Option<Vec<SelectItem>>,
/// Only for mysql
replace_into: bool,
/// Only for mysql
priority: Option<MysqlInsertPriority>,
/// Only for mysql
insert_alias: Option<InsertAliases>,
},
Insert(Insert),
/// ```sql
/// INSTALL
/// ```
Expand Down Expand Up @@ -1923,22 +1892,7 @@ pub enum Statement {
/// ```sql
/// DELETE
/// ```
Delete {
/// Multi tables delete are supported in mysql
tables: Vec<ObjectName>,
/// FROM
from: FromTable,
/// USING (Snowflake, Postgres, MySQL)
using: Option<Vec<TableWithJoins>>,
/// WHERE
selection: Option<Expr>,
/// RETURNING
returning: Option<Vec<SelectItem>>,
/// ORDER BY (MySQL)
order_by: Vec<OrderByExpr>,
/// LIMIT (MySQL)
limit: Option<Expr>,
},
Delete(Delete),
/// ```sql
/// CREATE VIEW
/// ```
Expand Down Expand Up @@ -2912,24 +2866,25 @@ impl fmt::Display for Statement {
}
Ok(())
}
Statement::Insert {
or,
ignore,
into,
table_name,
table_alias,
overwrite,
partitioned,
columns,
after_columns,
source,
table,
on,
returning,
replace_into,
priority,
insert_alias,
} => {
Statement::Insert(insert) => {
let Insert {
Comment on lines +2869 to +2870
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think it matters, but just FYI you can do the same thing in the single let statement

Suggested change
Statement::Insert(insert) => {
let Insert {
Statement::Insert(Insert {
..
}) => {

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your comment! I'll remember this and update this snippet when I have some time. You're correct. I just follow the intuitive "refactor" :D

or,
ignore,
into,
table_name,
table_alias,
overwrite,
partitioned,
columns,
after_columns,
source,
table,
on,
returning,
replace_into,
priority,
insert_alias,
} = insert;
let table_name = if let Some(alias) = table_alias {
format!("{table_name} AS {alias}")
} else {
Expand Down Expand Up @@ -3074,15 +3029,16 @@ impl fmt::Display for Statement {
}
Ok(())
}
Statement::Delete {
tables,
from,
using,
selection,
returning,
order_by,
limit,
} => {
Statement::Delete(delete) => {
let Delete {
tables,
from,
using,
selection,
returning,
order_by,
limit,
} = delete;
write!(f, "DELETE ")?;
if !tables.is_empty() {
write!(f, "{} ", display_comma_separated(tables))?;
Expand Down
10 changes: 5 additions & 5 deletions src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7088,7 +7088,7 @@ impl<'a> Parser<'a> {
None
};

Ok(Statement::Delete {
Ok(Statement::Delete(Delete {
tables,
from: if with_from_keyword {
FromTable::WithFromKeyword(from)
Expand All @@ -7100,7 +7100,7 @@ impl<'a> Parser<'a> {
returning,
order_by,
limit,
})
}))
}

// KILL [CONNECTION | QUERY | MUTATION] processlist_id
Expand Down Expand Up @@ -8664,7 +8664,7 @@ impl<'a> Parser<'a> {
}

let insert = &mut self.parse_insert()?;
if let Statement::Insert { replace_into, .. } = insert {
if let Statement::Insert(Insert { replace_into, .. }) = insert {
*replace_into = true;
}

Expand Down Expand Up @@ -8832,7 +8832,7 @@ impl<'a> Parser<'a> {
None
};

Ok(Statement::Insert {
Ok(Statement::Insert(Insert {
or,
table_name,
table_alias,
Expand All @@ -8849,7 +8849,7 @@ impl<'a> Parser<'a> {
replace_into,
priority,
insert_alias,
})
}))
}
}

Expand Down
4 changes: 2 additions & 2 deletions tests/sqlparser_bigquery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,10 @@ fn parse_raw_literal() {
fn parse_delete_statement() {
let sql = "DELETE \"table\" WHERE 1";
match bigquery_and_generic().verified_stmt(sql) {
Statement::Delete {
Statement::Delete(Delete {
from: FromTable::WithoutKeyword(from),
..
} => {
}) => {
assert_eq!(
TableFactor::Table {
name: ObjectName(vec![Ident::with_quote('"', "table")]),
Expand Down
Loading
Loading