From bcb39ee291c8ab283f02e156c6dcb50d286f0e9d Mon Sep 17 00:00:00 2001 From: Pedro Date: Fri, 24 Oct 2014 17:17:40 +0200 Subject: [PATCH] extended unary expr support --- src/bison/bison_parser.y | 5 ++--- src/build_and_run_tests.sh | 3 ++- src/lib/Expr.cpp | 1 + src/lib/Expr.h | 9 ++++++--- src/lib/sqlhelper.cpp | 12 ++++++++---- 5 files changed, 19 insertions(+), 11 deletions(-) diff --git a/src/bison/bison_parser.y b/src/bison/bison_parser.y index af91407..f5839a7 100644 --- a/src/bison/bison_parser.y +++ b/src/bison/bison_parser.y @@ -208,9 +208,8 @@ scalar_expr: ; unary_expr: - '-' expr { $$ = NULL; } // TODO - | '+' expr { $$ = NULL; } - | NOT expr { $$ = NULL; } + '-' expr { $$ = Expr::makeOpUnary(UMINUS, $2); } + | NOT expr { $$ = Expr::makeOpUnary(NOT, $2); } ; binary_expr: diff --git a/src/build_and_run_tests.sh b/src/build_and_run_tests.sh index ddd80da..b5bced0 100644 --- a/src/build_and_run_tests.sh +++ b/src/build_and_run_tests.sh @@ -6,4 +6,5 @@ make clean make execution ./bin/sql_execution "SELECT a FROM foo WHERE a > 12 OR b > 3 AND c = 3" -./bin/sql_execution "SELECT col1, col2, 'test' FROM table, foo WHERE age > 12 AND zipcode = 12345 GROUP BY col1;" \ No newline at end of file +./bin/sql_execution "SELECT col1, col2, 'test' FROM table, foo WHERE age > 12 AND zipcode = 12345 GROUP BY col1;" +./bin/sql_execution "SELECT * from table WHERE NOT a AND a = 12.5" diff --git a/src/lib/Expr.cpp b/src/lib/Expr.cpp index b40beb7..2d0173b 100644 --- a/src/lib/Expr.cpp +++ b/src/lib/Expr.cpp @@ -42,6 +42,7 @@ Expr* Expr::makeOpUnary(OperatorType op, Expr* expr) { ALLOC_EXPR(e, eExprOperator); e->op_type = op; e->expr = expr; + e->expr2 = NULL; return e; } diff --git a/src/lib/Expr.h b/src/lib/Expr.h index 91eacc8..3413a76 100644 --- a/src/lib/Expr.h +++ b/src/lib/Expr.h @@ -23,14 +23,17 @@ typedef enum { */ typedef enum { TRIVIAL_OP, + // Binary NOT_EQUALS, LESS_EQ, GREATER_EQ, LIKE, - ISNULL, - NOT, AND, - OR + OR, + // Unary + NOT, + UMINUS, + ISNULL } OperatorType; diff --git a/src/lib/sqlhelper.cpp b/src/lib/sqlhelper.cpp index a6e8af6..3ef3d44 100644 --- a/src/lib/sqlhelper.cpp +++ b/src/lib/sqlhelper.cpp @@ -27,10 +27,13 @@ void printTableRefInfo(TableRef* table, uint num_indent) { } void printOperatorExpression(Expr* expr, uint num_indent) { + if (expr == NULL) { inprint("null", num_indent); return; } + switch (expr->op_type) { case TRIVIAL_OP: inprint(expr->op_char, num_indent); break; case AND: inprint("AND", num_indent); break; case OR: inprint("OR", num_indent); break; + case NOT: inprint("NOT", num_indent); break; default: inprint(expr->op_type, num_indent); break; } printExpression(expr->expr, num_indent+1); @@ -45,9 +48,7 @@ void printExpression(Expr* expr, uint num_indent) { case eExprLiteralString: inprint(expr->name, num_indent); break; case eExprFunctionRef: /* todo */ break; case eExprOperator: printOperatorExpression(expr, num_indent); break; - default: - fprintf(stderr, "Unrecognized expression type %d\n", expr->type); - break; + default: fprintf(stderr, "Unrecognized expression type %d\n", expr->type); break; } } @@ -60,5 +61,8 @@ void printSelectStatementInfo(SelectStatement* stmt, uint num_indent) { printTableRefInfo(stmt->from_table, num_indent+2); inprint("Search Conditions:", num_indent+1); - printExpression(stmt->where_clause, num_indent+2); + if (stmt->where_clause != NULL) { + printExpression(stmt->where_clause, num_indent+2); + } else inprint("null", num_indent+2); + }