mirror of
https://github.com/apache/datafusion-sqlparser-rs.git
synced 2025-08-31 19:27:21 +00:00
Improve DELETE FROM parsing (4.4/4.4)
Store (and parse) `table_name: SQLObjectName` instead of `relation: Option<Box<ASTNode>>`, which can be an arbitrary expression. Also remove the `Option<>`: the table name is not optional in any dialects I'm familiar with. While the FROM keyword itself _is_ optional in some dialects, there are more things to implement for those dialects, see https://stackoverflow.com/a/4484271/1026
This commit is contained in:
parent
f5bd9c398f
commit
07790fe4c4
3 changed files with 14 additions and 33 deletions
|
@ -179,7 +179,7 @@ pub enum SQLStatement {
|
|||
/// DELETE
|
||||
SQLDelete {
|
||||
/// FROM
|
||||
relation: Option<Box<ASTNode>>,
|
||||
table_name: SQLObjectName,
|
||||
/// WHERE
|
||||
selection: Option<Box<ASTNode>>,
|
||||
},
|
||||
|
@ -279,13 +279,10 @@ impl ToString for SQLStatement {
|
|||
s
|
||||
}
|
||||
SQLStatement::SQLDelete {
|
||||
relation,
|
||||
table_name,
|
||||
selection,
|
||||
} => {
|
||||
let mut s = String::from("DELETE");
|
||||
if let Some(relation) = relation {
|
||||
s += &format!(" FROM {}", relation.as_ref().to_string());
|
||||
}
|
||||
let mut s = format!("DELETE FROM {}", table_name.to_string());
|
||||
if let Some(selection) = selection {
|
||||
s += &format!(" WHERE {}", selection.as_ref().to_string());
|
||||
}
|
||||
|
|
|
@ -1105,12 +1105,8 @@ impl Parser {
|
|||
}
|
||||
|
||||
pub fn parse_delete(&mut self) -> Result<SQLStatement, ParserError> {
|
||||
let relation: Option<Box<ASTNode>> = if self.parse_keyword("FROM") {
|
||||
Some(Box::new(self.parse_subexpr(0)?)) /* TBD (4) */
|
||||
} else {
|
||||
None
|
||||
};
|
||||
|
||||
self.expect_keyword("FROM")?;
|
||||
let table_name = self.parse_object_name()?;
|
||||
let selection = if self.parse_keyword("WHERE") {
|
||||
Some(Box::new(self.parse_expr()?))
|
||||
} else {
|
||||
|
@ -1118,7 +1114,7 @@ impl Parser {
|
|||
};
|
||||
|
||||
Ok(SQLStatement::SQLDelete {
|
||||
relation,
|
||||
table_name,
|
||||
selection,
|
||||
})
|
||||
}
|
||||
|
|
|
@ -8,16 +8,10 @@ use sqlparser::sqltokenizer::*;
|
|||
|
||||
#[test]
|
||||
fn parse_delete_statement() {
|
||||
let sql: &str = "DELETE FROM 'table'";
|
||||
|
||||
match verified_stmt(&sql) {
|
||||
SQLStatement::SQLDelete { relation, .. } => {
|
||||
assert_eq!(
|
||||
Some(Box::new(ASTNode::SQLValue(Value::SingleQuotedString(
|
||||
"table".to_string()
|
||||
)))),
|
||||
relation
|
||||
);
|
||||
let sql = "DELETE FROM \"table\"";
|
||||
match verified_stmt(sql) {
|
||||
SQLStatement::SQLDelete { table_name, .. } => {
|
||||
assert_eq!(SQLObjectName(vec!["\"table\"".to_string()]), table_name);
|
||||
}
|
||||
|
||||
_ => assert!(false),
|
||||
|
@ -26,23 +20,17 @@ fn parse_delete_statement() {
|
|||
|
||||
#[test]
|
||||
fn parse_where_delete_statement() {
|
||||
let sql: &str = "DELETE FROM 'table' WHERE name = 5";
|
||||
|
||||
use self::ASTNode::*;
|
||||
use self::SQLOperator::*;
|
||||
|
||||
match verified_stmt(&sql) {
|
||||
let sql = "DELETE FROM foo WHERE name = 5";
|
||||
match verified_stmt(sql) {
|
||||
SQLStatement::SQLDelete {
|
||||
relation,
|
||||
table_name,
|
||||
selection,
|
||||
..
|
||||
} => {
|
||||
assert_eq!(
|
||||
Some(Box::new(ASTNode::SQLValue(Value::SingleQuotedString(
|
||||
"table".to_string()
|
||||
)))),
|
||||
relation
|
||||
);
|
||||
assert_eq!(SQLObjectName(vec!["foo".to_string()]), table_name);
|
||||
|
||||
assert_eq!(
|
||||
SQLBinaryExpr {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue