added support for comments and like operator

This commit is contained in:
Pedro 2014-11-05 15:54:41 +01:00
parent 107dffc0c5
commit 34da5535fe
4 changed files with 55 additions and 30 deletions

View File

@ -13,7 +13,8 @@ echo "\n\n"
./bin/grammar_test "SELECT col1, col2, 'test' FROM table, foo AS t WHERE age > 12 AND zipcode = 12345 GROUP BY col1;" ./bin/grammar_test "SELECT col1, col2, 'test' FROM table, foo AS t WHERE age > 12 AND zipcode = 12345 GROUP BY col1;"
./bin/grammar_test "SELECT age FROM table AS t1, (SELECT * FROM table2) AS t2 ORDER BY age DESC" ./bin/grammar_test "SELECT age FROM table AS t1, (SELECT * FROM table2) AS t2 ORDER BY age DESC"
./bin/grammar_test "SELECT * from table JOIN table2 ON a = b WHERE (b OR NOT a) AND a = 12.5" ./bin/grammar_test "SELECT * from table JOIN table2 ON a = b WHERE (b OR NOT a) AND a = 12.5"
./bin/grammar_test "(SELECT a FROM foo WHERE a > 12 OR b > 3 AND c = 3 LIMIT 10);" ./bin/grammar_test "(SELECT a FROM foo WHERE a > 12 OR b > 3 AND c LIKE 's%' LIMIT 10);"
./bin/grammar_test "(SELECT a FROM foo WHERE a > 12 OR b > 3 AND c NOT LIKE 's%' LIMIT 10);"
./bin/grammar_test "SELECT t1.a, t1.b, t2.c FROM table AS t1 JOIN (SELECT * FROM foo JOIN bar ON foo.id = bar.id) t2 ON t1.a = t2.b WHERE (t1.b OR NOT t1.a) AND t2.c = 12.5" ./bin/grammar_test "SELECT t1.a, t1.b, t2.c FROM table AS t1 JOIN (SELECT * FROM foo JOIN bar ON foo.id = bar.id) t2 ON t1.a = t2.b WHERE (t1.b OR NOT t1.a) AND t2.c = 12.5"
# Error: Where clause in between join statement # Error: Where clause in between join statement
@ -26,6 +27,9 @@ echo "\n\n"
# ./bin/analysis "SELECT col1, col2, 'test' FROM table t1, foo WHERE age > 12 AND zipcode = 12345 GROUP BY col1 ORDER BY col2 DESC LIMIT 100;" # ./bin/analysis "SELECT col1, col2, 'test' FROM table t1, foo WHERE age > 12 AND zipcode = 12345 GROUP BY col1 ORDER BY col2 DESC LIMIT 100;"
# ./bin/analysis "SELECT * from table AS t1 JOIN table2 AS t2 ON t1.a = t2.b WHERE (b OR NOT a) AND a = 12.5" # ./bin/analysis "SELECT * from table AS t1 JOIN table2 AS t2 ON t1.a = t2.b WHERE (b OR NOT a) AND a = 12.5"
./bin/analysis "SELECT t1.a, t1.b, t2.c FROM table AS t1 JOIN (SELECT * FROM foo JOIN bar ON foo.id = bar.id) t2 ON t1.a = t2.b WHERE (t1.b OR NOT t1.a) AND t2.c = 12.5" ./bin/analysis "SELECT t1.a, t1.b, t2.c FROM table AS t1 JOIN (SELECT * FROM foo JOIN bar ON foo.id = bar.id) t2 ON t1.a = t2.b WHERE (t1.b OR NOT t1.a) AND t2.c = 12.5"
./bin/analysis "-- test
SELECT * FROM table WHERE a NOT LIKE '%s' -- inline comment
--my comment"
# ./bin/analysis "SELECT * from table WHERE (b OR NOT a) AND a = 12.5 JOIN table2 ON a = b" # ./bin/analysis "SELECT * from table WHERE (b OR NOT a) AND a = 12.5 JOIN table2 ON a = b"
echo "\n\n" echo "\n\n"

View File

@ -33,6 +33,7 @@ struct Expr {
LESS_EQ, LESS_EQ,
GREATER_EQ, GREATER_EQ,
LIKE, LIKE,
NOT_LIKE,
AND, AND,
OR, OR,
// Unary // Unary

View File

@ -92,9 +92,10 @@ typedef void* yyscan_t;
/********************************* /*********************************
** Token Definition ** Token Definition
*********************************/ *********************************/
%token SELECT FROM WHERE GROUP BY HAVING ORDER ASC DESC LIMIT %token SELECT FROM WHERE GROUP BY HAVING ORDER ASC DESC LIMIT DISTINCT OFFSET
%token JOIN ON INNER OUTER LEFT RIGHT CROSS USING NATURAL %token JOIN ON INNER OUTER LEFT RIGHT CROSS USING NATURAL
%token CREATE TABLE DATABASE INDEX %token CREATE TABLE DATABASE INDEX
%token DELETE INSERT
%token AS NOT AND OR NULL LIKE %token AS NOT AND OR NULL LIKE
%token <sval> NAME STRING COMPARISON %token <sval> NAME STRING COMPARISON
%token <fval> FLOAT %token <fval> FLOAT
@ -126,7 +127,7 @@ typedef void* yyscan_t;
%left OR %left OR
%left AND %left AND
%right NOT %right NOT
%right '=' EQUALS NOTEQUALS %right '=' EQUALS NOTEQUALS LIKE
%nonassoc '<' '>' LESS GREATER LESSEQ GREATEREQ %nonassoc '<' '>' LESS GREATER LESSEQ GREATEREQ
%nonassoc NOTNULL %nonassoc NOTNULL
@ -249,22 +250,24 @@ unary_expr:
binary_expr: binary_expr:
comp_expr comp_expr
| expr '-' expr { $$ = Expr::makeOpBinary($1, '-', $3); } | expr '-' expr { $$ = Expr::makeOpBinary($1, '-', $3); }
| expr '+' expr { $$ = Expr::makeOpBinary($1, '+', $3); } | expr '+' expr { $$ = Expr::makeOpBinary($1, '+', $3); }
| expr '/' expr { $$ = Expr::makeOpBinary($1, '/', $3); } | expr '/' expr { $$ = Expr::makeOpBinary($1, '/', $3); }
| expr '*' expr { $$ = Expr::makeOpBinary($1, '*', $3); } | expr '*' expr { $$ = Expr::makeOpBinary($1, '*', $3); }
| expr AND expr { $$ = Expr::makeOpBinary($1, Expr::AND, $3); } | expr AND expr { $$ = Expr::makeOpBinary($1, Expr::AND, $3); }
| expr OR expr { $$ = Expr::makeOpBinary($1, Expr::OR, $3); } | expr OR expr { $$ = Expr::makeOpBinary($1, Expr::OR, $3); }
| expr LIKE expr { $$ = Expr::makeOpBinary($1, Expr::LIKE, $3); }
| expr NOT LIKE expr { $$ = Expr::makeOpBinary($1, Expr::NOT_LIKE, $4); }
; ;
comp_expr: comp_expr:
expr EQUALS expr { $$ = Expr::makeOpBinary($1, '=', $3); } expr EQUALS expr { $$ = Expr::makeOpBinary($1, '=', $3); }
| expr NOTEQUALS expr { $$ = Expr::makeOpBinary($1, Expr::NOT_EQUALS, $3); } | expr NOTEQUALS expr { $$ = Expr::makeOpBinary($1, Expr::NOT_EQUALS, $3); }
| expr LESS expr { $$ = Expr::makeOpBinary($1, '<', $3); } | expr LESS expr { $$ = Expr::makeOpBinary($1, '<', $3); }
| expr GREATER expr { $$ = Expr::makeOpBinary($1, '>', $3); } | expr GREATER expr { $$ = Expr::makeOpBinary($1, '>', $3); }
| expr LESSEQ expr { $$ = Expr::makeOpBinary($1, Expr::LESS_EQ, $3); } | expr LESSEQ expr { $$ = Expr::makeOpBinary($1, Expr::LESS_EQ, $3); }
| expr GREATEREQ expr { $$ = Expr::makeOpBinary($1, Expr::GREATER_EQ, $3); } | expr GREATEREQ expr { $$ = Expr::makeOpBinary($1, Expr::GREATER_EQ, $3); }
; ;
function_expr: function_expr:

View File

@ -41,30 +41,47 @@
/* %option nodefault */ /* %option nodefault */
%s COMMENT
/*************************** /***************************
** Section 3: Rules ** Section 3: Rules
***************************/ ***************************/
%% %%
-- BEGIN(COMMENT);
<COMMENT>[^\n]* /* skipping comment content until a end of line is read */;
<COMMENT>\n BEGIN(INITIAL);
[ \t\n]+ /* skip whitespace */; [ \t\n]+ /* skip whitespace */;
SELECT TOKEN(SELECT)
FROM TOKEN(FROM)
GROUP TOKEN(GROUP)
BY TOKEN(BY)
WHERE TOKEN(WHERE)
NOT TOKEN(NOT)
AND TOKEN(AND)
OR TOKEN(OR)
AS TOKEN(AS)
LIMIT TOKEN(LIMIT) DISTINCT TOKEN(DISTINCT)
ORDER TOKEN(ORDER) OFFSET TOKEN(OFFSET)
ASC TOKEN(ASC) SELECT TOKEN(SELECT)
DESC TOKEN(DESC) INSERT TOKEN(INSERT)
JOIN TOKEN(JOIN) CREATE TOKEN(CREATE)
ON TOKEN(ON) DELETE TOKEN(DELETE)
HAVING TOKEN(HAVING)
GROUP TOKEN(GROUP)
WHERE TOKEN(WHERE)
LIMIT TOKEN(LIMIT)
ORDER TOKEN(ORDER)
INNER TOKEN(INNER)
OUTER TOKEN(OUTER)
CROSS TOKEN(CROSS)
FROM TOKEN(FROM)
LIKE TOKEN(LIKE)
JOIN TOKEN(JOIN)
DESC TOKEN(DESC)
ASC TOKEN(ASC)
NOT TOKEN(NOT)
AND TOKEN(AND)
BY TOKEN(BY)
OR TOKEN(OR)
AS TOKEN(AS)
ON TOKEN(ON)
"=" TOKEN(EQUALS) "=" TOKEN(EQUALS)
"<>" TOKEN(NOTEQUALS) "<>" TOKEN(NOTEQUALS)