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)
This commit is contained in:
javrucebo 2018-01-22 18:54:52 +01:00
parent 9872603fe7
commit 66804a6281
3 changed files with 43 additions and 1 deletions

View File

@ -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); }

View File

@ -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)

View File

@ -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);
}