Implemented NOT LIKE

This commit is contained in:
Andy Grove 2018-12-16 16:30:32 -07:00
parent 5d62167d6e
commit ee1944b9d9
3 changed files with 48 additions and 15 deletions

View file

@ -16,6 +16,7 @@ pub enum SQLOperator {
Or,
Not,
Like,
NotLike,
}
impl ToString for SQLOperator {
@ -36,6 +37,7 @@ impl ToString for SQLOperator {
SQLOperator::Or => "OR".to_string(),
SQLOperator::Not => "NOT".to_string(),
SQLOperator::Like => "LIKE".to_string(),
SQLOperator::NotLike => "NOT LIKE".to_string(),
}
}
}

View file

@ -275,8 +275,7 @@ impl Parser {
debug!("parsing infix");
match self.next_token() {
Some(tok) => match tok {
Token::Keyword(ref k) => {
if k == "IS" {
Token::Keyword(ref k) if k == "IS" => {
if self.parse_keywords(vec!["NULL"]) {
Ok(Some(ASTNode::SQLIsNull(Box::new(expr))))
} else if self.parse_keywords(vec!["NOT", "NULL"]) {
@ -284,14 +283,23 @@ impl Parser {
} else {
parser_err!("Invalid tokens after IS")
}
} else {
}
Token::Keyword(ref k) if k == "NOT" => {
if self.parse_keywords(vec!["LIKE"]) {
Ok(Some(ASTNode::SQLBinaryExpr {
left: Box::new(expr),
op: SQLOperator::NotLike,
right: Box::new(self.parse_expr(precedence)?),
}))
} else {
parser_err!("Invalid tokens after NOT")
}
}
Token::Keyword(_) => Ok(Some(ASTNode::SQLBinaryExpr {
left: Box::new(expr),
op: self.to_sql_operator(&tok)?,
right: Box::new(self.parse_expr(precedence)?),
}))
}
}
})),
Token::Eq
| Token::Neq
| Token::Gt
@ -333,7 +341,7 @@ impl Parser {
&Token::Mod => Ok(SQLOperator::Modulus),
&Token::Keyword(ref k) if k == "AND" => Ok(SQLOperator::And),
&Token::Keyword(ref k) if k == "OR" => Ok(SQLOperator::Or),
&Token::Keyword(ref k) if k == "NOT" => Ok(SQLOperator::Not),
//&Token::Keyword(ref k) if k == "NOT" => Ok(SQLOperator::Not),
&Token::Keyword(ref k) if k == "LIKE" => Ok(SQLOperator::Like),
_ => parser_err!(format!("Unsupported SQL operator {:?}", tok)),
}
@ -355,6 +363,7 @@ impl Parser {
match tok {
&Token::Keyword(ref k) if k == "OR" => Ok(5),
&Token::Keyword(ref k) if k == "AND" => Ok(10),
&Token::Keyword(ref k) if k == "NOT" => Ok(15),
&Token::Keyword(ref k) if k == "IS" => Ok(15),
&Token::Keyword(ref k) if k == "LIKE" => Ok(20),
&Token::Eq | &Token::Lt | &Token::LtEq | &Token::Neq | &Token::Gt | &Token::GtEq => {

View file

@ -753,3 +753,25 @@ fn parse_like() {
_ => assert!(false),
}
}
#[test]
fn parse_not_like() {
let sql = String::from("SELECT * FROM customers WHERE name NOT LIKE '%a'");
let ast = parse_sql(&sql);
assert_eq!(sql, ast.to_string());
match ast {
ASTNode::SQLSelect { selection, .. } => {
assert_eq!(
ASTNode::SQLBinaryExpr {
left: Box::new(ASTNode::SQLIdentifier("name".to_string())),
op: SQLOperator::NotLike,
right: Box::new(ASTNode::SQLValue(Value::SingleQuotedString(
"%a".to_string()
))),
},
*selection.unwrap()
);
}
_ => assert!(false),
}
}