Added boolean literal support (#103)

* Added boolean support

* Made bool literals int vals

With a flag indicating if they came from boolean literals.

* Add makeLiteral(bool val);
This commit is contained in:
alkim0 2018-11-02 06:42:23 -04:00 committed by mrks
parent a122effd46
commit a59deb43c3
9 changed files with 2216 additions and 2141 deletions

File diff suppressed because it is too large Load Diff

View File

@ -212,14 +212,16 @@ extern int hsql_debug;
SQL_DAY = 388,
SQL_MONTH = 389,
SQL_YEAR = 390,
SQL_EQUALS = 391,
SQL_NOTEQUALS = 392,
SQL_LESS = 393,
SQL_GREATER = 394,
SQL_LESSEQ = 395,
SQL_GREATEREQ = 396,
SQL_NOTNULL = 397,
SQL_UMINUS = 398
SQL_TRUE = 391,
SQL_FALSE = 392,
SQL_EQUALS = 393,
SQL_NOTEQUALS = 394,
SQL_LESS = 395,
SQL_GREATER = 396,
SQL_LESSEQ = 397,
SQL_GREATEREQ = 398,
SQL_NOTNULL = 399,
SQL_UMINUS = 400
};
#endif
@ -270,7 +272,7 @@ union HSQL_STYPE
std::vector<hsql::Expr*>* expr_vec;
std::vector<hsql::OrderDescription*>* order_vec;
#line 274 "bison_parser.h" /* yacc.c:1919 */
#line 276 "bison_parser.h" /* yacc.c:1919 */
};
typedef union HSQL_STYPE HSQL_STYPE;

View File

@ -175,6 +175,7 @@ int yyerror(YYLTYPE* llocp, SQLParserResult* result, yyscan_t scanner, const cha
%token VIEW WHEN WITH ADD ALL AND ASC CSV END FOR INT KEY
%token NOT OFF SET TBL TOP AS BY IF IN IS OF ON OR TO
%token ARRAY CONCAT ILIKE SECOND MINUTE HOUR DAY MONTH YEAR
%token TRUE FALSE
/*********************************
** Non-Terminal types (http://www.gnu.org/software/bison/manual/html_node/Type-Decl.html)
@ -199,7 +200,7 @@ int yyerror(YYLTYPE* llocp, SQLParserResult* result, yyscan_t scanner, const cha
%type <table> join_clause table_ref_name_no_alias
%type <expr> expr operand scalar_expr unary_expr binary_expr logic_expr exists_expr extract_expr
%type <expr> function_expr between_expr expr_alias param_expr
%type <expr> column_name literal int_literal num_literal string_literal
%type <expr> column_name literal int_literal num_literal string_literal bool_literal
%type <expr> comp_expr opt_where join_condition opt_having case_expr case_list in_expr hint
%type <expr> array_expr array_index null_literal
%type <limit> opt_limit opt_top
@ -888,6 +889,7 @@ column_name:
literal:
string_literal
| bool_literal
| num_literal
| null_literal
| param_expr
@ -897,6 +899,10 @@ string_literal:
STRING { $$ = Expr::makeLiteral($1); }
;
bool_literal:
TRUE { $$ = Expr::makeLiteral(true); }
| FALSE { $$ = Expr::makeLiteral(false); }
;
num_literal:
FLOATVAL { $$ = Expr::makeLiteral($1); }

File diff suppressed because it is too large Load Diff

View File

@ -729,7 +729,7 @@ extern int yylex \
#undef yyTABLES_NAME
#endif
#line 229 "flex_lexer.l"
#line 231 "flex_lexer.l"
#line 735 "flex_lexer.h"

View File

@ -184,6 +184,8 @@ HOUR TOKEN(HOUR)
DAY TOKEN(DAY)
MONTH TOKEN(MONTH)
YEAR TOKEN(YEAR)
TRUE TOKEN(TRUE)
FALSE TOKEN(FALSE)
/* Allow =/== see https://sqlite.org/lang_expr.html#collateop */
"==" TOKEN(EQUALS)

View File

@ -1,4 +1,3 @@
#include "Expr.h"
#include <stdio.h>
#include <string.h>
@ -19,6 +18,7 @@ namespace hsql {
ival(0),
ival2(0),
datetimeField(kDatetimeNone),
isBoolLiteral(false),
opType(kOpNone),
distinct(false){};
@ -71,8 +71,8 @@ namespace hsql {
Expr* Expr::makeCaseList(Expr* caseListElement) {
Expr* e = new Expr(kExprOperator);
// Case list expressions are temporary and will be integrated into the case expressions exprList - thus assign
// operator type kOpNone
// Case list expressions are temporary and will be integrated into the case
// expressions exprList - thus assign operator type kOpNone
e->opType = kOpNone;
e->exprList = new std::vector<Expr*>();
e->exprList->push_back(caseListElement);
@ -121,6 +121,13 @@ namespace hsql {
return e;
}
Expr* Expr::makeLiteral(bool val) {
Expr* e = new Expr(kExprLiteralInt);
e->ival = (int)val;
e->isBoolLiteral = true;
return e;
}
Expr* Expr::makeNullLiteral() {
Expr* e = new Expr(kExprLiteralNull);
return e;
@ -217,9 +224,7 @@ namespace hsql {
return e;
}
bool Expr::isType(ExprType exprType) const {
return exprType == type;
}
bool Expr::isType(ExprType exprType) const { return exprType == type; }
bool Expr::isLiteral() const {
return isType(kExprLiteralInt) || isType(kExprLiteralFloat) ||
@ -227,13 +232,9 @@ namespace hsql {
isType(kExprLiteralNull);
}
bool Expr::hasAlias() const {
return alias != nullptr;
}
bool Expr::hasAlias() const { return alias != nullptr; }
bool Expr::hasTable() const {
return table != nullptr;
}
bool Expr::hasTable() const { return table != nullptr; }
const char* Expr::getName() const {
if (alias != nullptr)

View File

@ -102,6 +102,7 @@ namespace hsql {
int64_t ival;
int64_t ival2;
DatetimeField datetimeField;
bool isBoolLiteral;
OperatorType opType;
bool distinct;
@ -142,6 +143,8 @@ namespace hsql {
static Expr* makeLiteral(char* val);
static Expr* makeLiteral(bool val);
static Expr* makeNullLiteral();
static Expr* makeColumnRef(char* name);

View File

@ -428,19 +428,24 @@ TEST(Operators) {
SQLParserResult result;
SQLParser::parse("SELECT * FROM foo where a = 1; \
SELECT * FROM foo where a == 1; \
SELECT * FROM foo where a == 2; \
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);
SELECT * FROM foo where a <= 1; \
SELECT * FROM foo where a = TRUE; \
SELECT * FROM foo where a = false;", &result);
stmt = (SelectStatement*) result.getStatement(0);
ASSERT_EQ(stmt->whereClause->opType, kOpEquals);
ASSERT_EQ(stmt->whereClause->expr2->ival, 1);
ASSERT_EQ(stmt->whereClause->expr2->isBoolLiteral, false);
stmt = (SelectStatement*) result.getStatement(1);
ASSERT_EQ(stmt->whereClause->opType, kOpEquals);
ASSERT_EQ(stmt->whereClause->expr2->ival, 2);
stmt = (SelectStatement*) result.getStatement(2);
ASSERT_EQ(stmt->whereClause->opType, kOpNotEquals);
@ -459,6 +464,16 @@ TEST(Operators) {
stmt = (SelectStatement*) result.getStatement(7);
ASSERT_EQ(stmt->whereClause->opType, kOpLessEq);
stmt = (SelectStatement*) result.getStatement(8);
ASSERT_EQ(stmt->whereClause->opType, kOpEquals);
ASSERT_EQ(stmt->whereClause->expr2->ival, 1);
ASSERT_EQ(stmt->whereClause->expr2->isBoolLiteral, true);
stmt = (SelectStatement*) result.getStatement(9);
ASSERT_EQ(stmt->whereClause->opType, kOpEquals);
ASSERT_EQ(stmt->whereClause->expr2->ival, 0);
ASSERT_EQ(stmt->whereClause->expr2->isBoolLiteral, true);
}
TEST(JoinTypes) {