From 66804a628149ad3d0272b3363cb4c28e6aeea854 Mon Sep 17 00:00:00 2001 From: javrucebo Date: Mon, 22 Jan 2018 18:54:52 +0100 Subject: [PATCH] Allow '==' to be parsed as equal operator in expressions. SQLite allows for this and it is in the same spirit as allowing both != and <> for non equality. (see https://sqlite.org/lang_expr.html#collateop) --- src/parser/bison_parser.y | 1 + src/parser/flex_lexer.l | 4 +++- test/select_tests.cpp | 39 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 43 insertions(+), 1 deletion(-) diff --git a/src/parser/bison_parser.y b/src/parser/bison_parser.y index 0d98897..99c0fa0 100755 --- a/src/parser/bison_parser.y +++ b/src/parser/bison_parser.y @@ -814,6 +814,7 @@ exists_expr: comp_expr: operand '=' operand { $$ = Expr::makeOpBinary($1, kOpEquals, $3); } + | operand EQUALS operand { $$ = Expr::makeOpBinary($1, kOpEquals, $3); } | operand NOTEQUALS operand { $$ = Expr::makeOpBinary($1, kOpNotEquals, $3); } | operand '<' operand { $$ = Expr::makeOpBinary($1, kOpLess, $3); } | operand '>' operand { $$ = Expr::makeOpBinary($1, kOpGreater, $3); } diff --git a/src/parser/flex_lexer.l b/src/parser/flex_lexer.l index 66f3552..f39d0ed 100644 --- a/src/parser/flex_lexer.l +++ b/src/parser/flex_lexer.l @@ -175,7 +175,9 @@ ON TOKEN(ON) OR TOKEN(OR) TO TOKEN(TO) -"!=" TOKEN(NOTEQUALS) + /* Allow =/== see https://sqlite.org/lang_expr.html#collateop */ +"==" TOKEN(EQUALS) +"!=" TOKEN(NOTEQUALS) "<>" TOKEN(NOTEQUALS) "<=" TOKEN(LESSEQ) ">=" TOKEN(GREATEREQ) diff --git a/test/select_tests.cpp b/test/select_tests.cpp index 3197a31..ddf2366 100644 --- a/test/select_tests.cpp +++ b/test/select_tests.cpp @@ -347,3 +347,42 @@ TEST(SelectColumnOrder) { ASSERT_STREQ(stmt->fromTable->list->at(2)->alias, "c"); ASSERT_STREQ(stmt->fromTable->list->at(3)->alias, "d"); } + +TEST(Operators) { + SelectStatement* stmt; + SQLParserResult result; + + SQLParser::parse("SELECT * FROM foo where a = 1; \ + SELECT * FROM foo where a == 1; \ + SELECT * FROM foo where a != 1; \ + SELECT * FROM foo where a <> 1; \ + SELECT * FROM foo where a > 1; \ + SELECT * FROM foo where a < 1; \ + SELECT * FROM foo where a >= 1; \ + SELECT * FROM foo where a <= 1;", &result); + + stmt = (SelectStatement*) result.getStatement(0); + ASSERT_EQ(stmt->whereClause->opType, kOpEquals); + + stmt = (SelectStatement*) result.getStatement(1); + ASSERT_EQ(stmt->whereClause->opType, kOpEquals); + + stmt = (SelectStatement*) result.getStatement(2); + ASSERT_EQ(stmt->whereClause->opType, kOpNotEquals); + + stmt = (SelectStatement*) result.getStatement(3); + ASSERT_EQ(stmt->whereClause->opType, kOpNotEquals); + + stmt = (SelectStatement*) result.getStatement(4); + ASSERT_EQ(stmt->whereClause->opType, kOpGreater); + + stmt = (SelectStatement*) result.getStatement(5); + ASSERT_EQ(stmt->whereClause->opType, kOpLess); + + stmt = (SelectStatement*) result.getStatement(6); + ASSERT_EQ(stmt->whereClause->opType, kOpGreaterEq); + + stmt = (SelectStatement*) result.getStatement(7); + ASSERT_EQ(stmt->whereClause->opType, kOpLessEq); +} +