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:
Nickolay Ponomarev 2019-01-31 00:10:15 +03:00
parent f5bd9c398f
commit 07790fe4c4
3 changed files with 14 additions and 33 deletions

View file

@ -179,7 +179,7 @@ pub enum SQLStatement {
/// DELETE /// DELETE
SQLDelete { SQLDelete {
/// FROM /// FROM
relation: Option<Box<ASTNode>>, table_name: SQLObjectName,
/// WHERE /// WHERE
selection: Option<Box<ASTNode>>, selection: Option<Box<ASTNode>>,
}, },
@ -279,13 +279,10 @@ impl ToString for SQLStatement {
s s
} }
SQLStatement::SQLDelete { SQLStatement::SQLDelete {
relation, table_name,
selection, selection,
} => { } => {
let mut s = String::from("DELETE"); let mut s = format!("DELETE FROM {}", table_name.to_string());
if let Some(relation) = relation {
s += &format!(" FROM {}", relation.as_ref().to_string());
}
if let Some(selection) = selection { if let Some(selection) = selection {
s += &format!(" WHERE {}", selection.as_ref().to_string()); s += &format!(" WHERE {}", selection.as_ref().to_string());
} }

View file

@ -1105,12 +1105,8 @@ impl Parser {
} }
pub fn parse_delete(&mut self) -> Result<SQLStatement, ParserError> { pub fn parse_delete(&mut self) -> Result<SQLStatement, ParserError> {
let relation: Option<Box<ASTNode>> = if self.parse_keyword("FROM") { self.expect_keyword("FROM")?;
Some(Box::new(self.parse_subexpr(0)?)) /* TBD (4) */ let table_name = self.parse_object_name()?;
} else {
None
};
let selection = if self.parse_keyword("WHERE") { let selection = if self.parse_keyword("WHERE") {
Some(Box::new(self.parse_expr()?)) Some(Box::new(self.parse_expr()?))
} else { } else {
@ -1118,7 +1114,7 @@ impl Parser {
}; };
Ok(SQLStatement::SQLDelete { Ok(SQLStatement::SQLDelete {
relation, table_name,
selection, selection,
}) })
} }

View file

@ -8,16 +8,10 @@ use sqlparser::sqltokenizer::*;
#[test] #[test]
fn parse_delete_statement() { fn parse_delete_statement() {
let sql: &str = "DELETE FROM 'table'"; let sql = "DELETE FROM \"table\"";
match verified_stmt(sql) {
match verified_stmt(&sql) { SQLStatement::SQLDelete { table_name, .. } => {
SQLStatement::SQLDelete { relation, .. } => { assert_eq!(SQLObjectName(vec!["\"table\"".to_string()]), table_name);
assert_eq!(
Some(Box::new(ASTNode::SQLValue(Value::SingleQuotedString(
"table".to_string()
)))),
relation
);
} }
_ => assert!(false), _ => assert!(false),
@ -26,23 +20,17 @@ fn parse_delete_statement() {
#[test] #[test]
fn parse_where_delete_statement() { fn parse_where_delete_statement() {
let sql: &str = "DELETE FROM 'table' WHERE name = 5";
use self::ASTNode::*; use self::ASTNode::*;
use self::SQLOperator::*; use self::SQLOperator::*;
match verified_stmt(&sql) { let sql = "DELETE FROM foo WHERE name = 5";
match verified_stmt(sql) {
SQLStatement::SQLDelete { SQLStatement::SQLDelete {
relation, table_name,
selection, selection,
.. ..
} => { } => {
assert_eq!( assert_eq!(SQLObjectName(vec!["foo".to_string()]), table_name);
Some(Box::new(ASTNode::SQLValue(Value::SingleQuotedString(
"table".to_string()
)))),
relation
);
assert_eq!( assert_eq!(
SQLBinaryExpr { SQLBinaryExpr {