7 ParsedData *parsedData;
10 void yyerror(const char* Msg);
26 %token <stringval> STRING FIELD NUMBER_STRING BINARY_STRING DOUBLE OPERATOR PARAMETER
27 %token <stringval> SELECT FROM WHERE BETWEEN IN AND OR NOT
28 %token <stringval> INSERT INTO VALUES
29 %token <stringval> DELETE UPDATE SET NULL_VALUE
30 %token <stringval> CREATE TABLE PRIMARY KEY DEFAULT INDEX ON HASH TREE UNIQUE DROP
31 %token <stringval> INT_TYPE LONG_TYPE SHORT_TYPE DOUBLE_TYPE TIMESTAMP_TYPE DATE_TYPE CHAR_TYPE TIME_TYPE BIGINT_TYPE FLOAT_TYPE TINYINT_TYPE BINARY_TYPE
32 %token <stringval> MIN MAX AVG SUM COUNT GROUP BY
33 %token ';' ',' '(' ')'
34 %type <stringval> ident field value not_opt doub_value num_value
35 %type <predicate> conditions condition
36 %type <Expression> stmt_exp atom
38 command: select_statement { YYACCEPT; }
39 | insert_statement { YYACCEPT; }
40 | delete_statement { YYACCEPT; }
41 | update_statement { YYACCEPT; }
42 | ddl_statement { YYACCEPT; }
45 select_statement: SELECT field_list FROM table_list where_clause_opt group_by_opt semicolon_opt
47 parsedData->setStmtType(SelectStatement);
48 //parsedData->setTableName($4);
53 field_list: field_list field_list_L
56 field_list_L: ',' field
58 table_list: table_list table_list_L
61 table_list_L: ',' table
64 insert_statement: INSERT INTO ident field_list_opt VALUES '(' value_list ')' semicolon_opt
66 parsedData->setStmtType(InsertStatement);
67 parsedData->setTableName($3);
76 value_list: value_list value_list_L
79 parsedData->insertValue((char*)$1);
83 value_list_L: ',' value
85 parsedData->insertValue((char*)$2);
90 invalue_list: invalue_list invalue_list_L
93 parsedData->insertInValue((char*)$1);
97 invalue_list_L: ',' value
99 parsedData->insertInValue((char*)$2);
104 delete_statement: DELETE FROM ident where_clause_opt semicolon_opt
106 parsedData->setStmtType(DeleteStatement);
107 parsedData->setTableName($3);
112 update_statement: UPDATE ident SET assign_list where_clause_opt semicolon_opt
114 parsedData->setStmtType(UpdateStatement);
115 parsedData->setTableName($2);
124 assign_list: assign_list assign_list_L
128 assign_list_L: ',' assign_stmt
130 assign_stmt: ident OPERATOR stmt_exp
132 parsedData->insertUpdateExpression( (char*) $1, (Expression* ) $3);
133 free( $1 ); free($2);
135 | ident OPERATOR value
137 parsedData->insertUpdateValue( (char*) $1, (char*) $3);
138 free( $1 ); free($2);free( $3 );
140 | ident OPERATOR NULL_VALUE
142 parsedData->insertUpdateValue( (char*) $1, (char*) $3);
143 free( $1 ); free($2);free( $3 );
146 stmt_exp: stmt_exp '/' stmt_exp
149 exp=parsedData->insertExpression((Expression *)$1, division, (Expression *)$3);
152 | stmt_exp '%' stmt_exp
155 exp=parsedData->insertExpression((Expression *)$1, modulus, (Expression *)$3);
158 | stmt_exp '*' stmt_exp
161 exp=parsedData->insertExpression((Expression *)$1, multiplication, (Expression *)$3);
164 | stmt_exp '+' stmt_exp
167 exp=parsedData->insertExpression((Expression *)$1, addition, (Expression *)$3);
170 | stmt_exp '-' stmt_exp
173 exp=parsedData->insertExpression((Expression *)$1, subtraction, (Expression *)$3);
180 | '-' stmt_exp %prec UMINUS
184 | '+' stmt_exp %prec UMINUS
193 exp=parsedData->insertExpression((char *)$1);
201 exp=parsedData->insertExpression((char *)$1, flag);
207 where_clause_opt: WHERE conditions
210 group_by_opt: GROUP BY group_field_list
213 group_field_list: group_field_list group_field_list_L
216 group_field_list_L: ',' group_field
220 parsedData->insertGroupField((char*)$1);
224 conditions: conditions OR conditions
227 pred = parsedData->insertPredicate((Predicate*) $1, OpOr, (Predicate*) $3);
228 parsedData->setCondition((Predicate*)pred);
232 | conditions AND conditions
235 pred = parsedData->insertPredicate((Predicate*) $1, OpAnd, (Predicate*) $3);
236 parsedData->setCondition((Predicate*)pred);
239 | '(' conditions ')' { $$=$2; }
240 | NOT '(' conditions ')'
243 pred = parsedData->insertPredicate((Predicate*) $3, OpNot, NULL);
244 parsedData->setCondition((Predicate*)pred);
250 if( $1 == (char*) 1 )
253 pred = parsedData->insertPredicate((Predicate*) $2, OpNot, NULL);
254 parsedData->setCondition((Predicate*)pred);
259 parsedData->setCondition((Predicate*)$2);
264 condition: ident OPERATOR value
266 ComparisionOp op = AllDataType::getComparisionOperator((char*)$2);
269 char *c = (char *)$3;
271 if (*c == '_') *c = '?';
272 else if(*c == '%') *c = '*';
278 void **ptr = parsedData->insertCondValueAndGetPtr((char*)$1, (char*)$3, opLike);
279 pred = parsedData->insertPredicate((char*) $1, op, ptr);
280 free( $1 ); free($2); free( $3 );
283 | ident OPERATOR ident
285 ComparisionOp op = AllDataType::getComparisionOperator((char*)$2);
287 pred = parsedData->insertPredicate((char*) $1, op, (char*) $3);
288 free( $1 ); free($2); free( $3 );
292 | ident not_opt BETWEEN value AND value
294 void **ptr1 = parsedData->insertCondValueAndGetPtr((char*)$1, (char*)$4);
295 void **ptr2 = parsedData->insertCondValueAndGetPtr((char*)$1, (char*)$6);
296 Predicate *finalPred;
297 // pred1 = parsedData->insertPredicate((char*) $1, OpGreaterThanEquals, ptr1);
298 // pred2 = parsedData->insertPredicate((char*) $1, OpLessThanEquals, ptr2);
299 finalPred = parsedData->insertBetPredicate((char*) $1,
300 OpGreaterThanEquals, ptr1, OpLessThanEquals, ptr2);
301 //OpLessThanEquals, ptr2, OpGreaterThanEquals, ptr1);
303 if( $2 == (char*) 1 )
304 finalPred = parsedData->insertPredicate(finalPred, OpNot, NULL);
305 free( $1 ); free( $4 ); free( $6 );
308 | ident not_opt IN '(' invalue_list ')'
310 ListIterator valIter = parsedData->getInValueList().getIterator();
311 FieldValue *value1, *value2;
312 Predicate *pred1, *pred2, *finalPred;
314 if (valIter.hasElement()) {
315 value1 = (FieldValue*) valIter.nextElement();
316 ptr1 = parsedData->insertCondValueAndGetPtr((char*)$1, value1->parsedString);
317 pred1 = parsedData->insertPredicate((char*) $1, OpEquals, ptr1);
320 while (valIter.hasElement()) {
321 value2 = (FieldValue*) valIter.nextElement();
322 ptr2 = parsedData->insertCondValueAndGetPtr((char*)$1, value2->parsedString);
323 pred2 = parsedData->insertPredicate((char*) $1, OpEquals, ptr2);
324 finalPred = parsedData->insertPredicate(pred1, OpOr, pred2);
328 finalPred = parsedData->insertPredicate(finalPred, OpNot, NULL);
342 parsedData->insertTableName((char*)$1);
348 parsedData->insertField((char*)$1);
354 parsedData->insertField((char*)$3, AGG_MIN);
359 parsedData->insertField((char*)$3, AGG_MAX);
364 parsedData->insertField((char*)$3, AGG_SUM);
369 parsedData->insertField((char*)$3, AGG_AVG);
372 | COUNT '(' ident ')'
374 parsedData->insertField((char*)$3, AGG_COUNT);
379 parsedData->insertField("*", AGG_COUNT);
384 parsedData->insertField("*");
387 ident: FIELD { $$ = $1; }
389 value: STRING { $$ = $1; }
391 | BINARY_STRING { $$ = $1; }
393 | PARAMETER { $$ = $1; }
394 | NULL_VALUE { $$ = (char*) 0; }
396 num_value: NUMBER_STRING { $$ = $1; }
397 | '-' NUMBER_STRING %prec UMINUS
406 | '+' NUMBER_STRING %prec UMINUS{ $$ = $2; }
408 doub_value : DOUBLE { $$ = $1; }
409 | '-' DOUBLE %prec UMINUS
418 | '+' DOUBLE %prec UMINUS{ $$ = $2; }
421 ddl_statement: CREATE TABLE ident '(' create_defn_list_con ')' semicolon_opt
423 parsedData->setStmtType(CreateTableStatement);
424 parsedData->setTableName($3);
427 | CREATE INDEX ident ON ident '(' field_list ')' opt_constr_type opt_ind_type semicolon_opt
429 parsedData->setStmtType(CreateIndexStatement);
430 parsedData->setIndexName($3);
431 parsedData->setTableName($5);
437 parsedData->setStmtType(DropTableStatement);
438 parsedData->setTableName($3);
443 parsedData->setStmtType(DropIndexStatement);
444 parsedData->setIndexName($3);
449 | HASH opt_constr_type
451 parsedData->setIndexType(hashIndex);
453 | TREE opt_constr_type
455 parsedData->setIndexType(treeIndex);
459 parsedData->setIndexType(hashIndex);
465 parsedData->setUnique(true);
469 parsedData->setUnique(true);
470 parsedData->setPrimary(true);
474 parsedData->setUnique(false);
475 parsedData->setPrimary(false);
478 create_defn_list_con:create_defn_list
479 |create_defn_list ',' constraint_defn
481 create_defn_list: create_defn_list create_defn_list_L
485 create_defn_list_L: ',' create_defn
488 create_defn: field_defn
490 parsedData->insertFldDef();
494 field_defn: field_name field_type size_opt null_expr_opt default_expr_opt
499 parsedData->setFldName($1);
503 | '(' NUMBER_STRING ')'
505 DbRetVal rv = parsedData->setFldLength(atoi($2)+1);
507 yyerror("Binary field length < 256");
518 parsedData->setDefaultValue($2);
526 parsedData->setFldNotNull(true);
529 constraint_defn: primary_key_constraint
532 primary_key_constraint: PRIMARY KEY '(' field_list ')'
534 parsedData->setPrimary(true);
540 parsedData->setFldType(typeInt);
544 parsedData->setFldType(typeLong);
548 parsedData->setFldType(typeShort);
552 parsedData->setFldType(typeLongLong);
556 parsedData->setFldType(typeByteInt);
560 parsedData->setFldType(typeFloat);
564 parsedData->setFldType(typeDouble);
569 parsedData->setFldType(typeDate);
574 parsedData->setFldType(typeTime);
579 parsedData->setFldType(typeTimeStamp);
584 parsedData->setFldType(typeString);
585 parsedData->setFldLength(2);
590 parsedData->setFldType(typeBinary);
591 parsedData->setFldLength(1);
597 void yyerror(const char* Msg) {
599 fprintf(stderr, "[Parser: %s] %s\n", Msg, yytext);