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 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 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"
# 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 * 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 "-- 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"
echo "\n\n"

View File

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

View File

@ -92,9 +92,10 @@ typedef void* yyscan_t;
/*********************************
** 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 CREATE TABLE DATABASE INDEX
%token DELETE INSERT
%token AS NOT AND OR NULL LIKE
%token <sval> NAME STRING COMPARISON
%token <fval> FLOAT
@ -126,7 +127,7 @@ typedef void* yyscan_t;
%left OR
%left AND
%right NOT
%right '=' EQUALS NOTEQUALS
%right '=' EQUALS NOTEQUALS LIKE
%nonassoc '<' '>' LESS GREATER LESSEQ GREATEREQ
%nonassoc NOTNULL
@ -255,6 +256,8 @@ binary_expr:
| expr '*' expr { $$ = Expr::makeOpBinary($1, '*', $3); }
| expr AND expr { $$ = Expr::makeOpBinary($1, Expr::AND, $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); }
;

View File

@ -41,31 +41,48 @@
/* %option nodefault */
%s COMMENT
/***************************
** 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 */;
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)
DISTINCT TOKEN(DISTINCT)
OFFSET TOKEN(OFFSET)
SELECT TOKEN(SELECT)
INSERT TOKEN(INSERT)
CREATE TOKEN(CREATE)
DELETE TOKEN(DELETE)
HAVING TOKEN(HAVING)
GROUP TOKEN(GROUP)
WHERE TOKEN(WHERE)
LIMIT TOKEN(LIMIT)
ORDER TOKEN(ORDER)
ASC TOKEN(ASC)
DESC TOKEN(DESC)
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(NOTEQUALS)
"<" TOKEN(LESS)