added limit and order by to grammar

This commit is contained in:
Pedro 2014-10-27 14:54:15 +01:00
parent b11e52bd6e
commit 794a17c04c
2 changed files with 56 additions and 25 deletions

View File

@ -62,12 +62,15 @@ typedef void* yyscan_t;
double fval;
int64_t ival;
char* sval;
uint uintnum;
uint uval;
Statement* statement;
SelectStatement* select_statement;
TableRef* table;
Expr* expr;
OrderDescription* order;
OrderType order_type;
LimitDescription* limit;
List<char*>* slist;
List<Expr*>* explist;
@ -79,14 +82,14 @@ typedef void* yyscan_t;
/*********************************
** Token Definition
*********************************/
%token SELECT FROM WHERE GROUP BY HAVING
%token SELECT FROM WHERE GROUP BY HAVING ORDER ASC DESC LIMIT
%token JOIN ON INNER OUTER LEFT RIGHT CROSS USING NATURAL
%token CREATE TABLE DATABASE INDEX
%token AS NOT AND OR NULL LIKE
%token <sval> NAME STRING COMPARISON
%token <fval> FLOAT
%token <ival> INT
%token <uintnum> EQUALS NOTEQUALS LESS GREATER LESSEQ GREATEREQ
%token <uval> EQUALS NOTEQUALS LESS GREATER LESSEQ GREATEREQ
/*********************************
** Non-Terminal types (http://www.gnu.org/software/bison/manual/html_node/Type-Decl.html)
@ -96,10 +99,13 @@ typedef void* yyscan_t;
%type <sval> table_name
%type <table> from_clause table_ref table_ref_atomic
%type <expr> expr scalar_expr unary_expr binary_expr function_expr star_expr
%type <expr> column_name literal
%type <expr> column_name literal int_literal num_literal
%type <expr> comp_expr where_clause
%type <explist> expr_list group_clause select_list
%type <tbllist> table_ref_commalist
%type <order> order_by_clause
%type <limit> limit_clause
%type <order_type> order_type
/******************************
@ -152,17 +158,15 @@ statement:
// TODO: limit
// TODO: order by
select_statement:
SELECT select_list from_clause where_clause group_clause
SELECT select_list from_clause where_clause group_clause order_by_clause limit_clause
{
SelectStatement* s = new SelectStatement();
s->select_list = $2;
s->from_table = $3;
s->where_clause = $4;
s->group_by = $5;
s->having = NULL; // TODO
s->order = NULL; // TODO
s->limit = kNoLimit; // TODO
s->offset = kNoOffset; // TODO
s->order = $6;
s->limit = $7;
$$ = s;
}
;
@ -190,6 +194,19 @@ group_clause:
;
order_by_clause:
ORDER BY expr order_type { $$ = new OrderDescription($4, $3); }
| /* empty */ { $$ = NULL; }
order_type:
ASC { $$ = kOrderAsc; }
| DESC { $$ = kOrderDesc; }
| /* empty */ { $$ = kOrderAsc; }
limit_clause:
LIMIT int_literal { $$ = new LimitDescription($2->ival, kNoOffset); delete $2; }
| /* empty */ { $$ = NULL; }
/******************************
** Expressions
******************************/
@ -248,8 +265,16 @@ column_name:
literal:
STRING { $$ = Expr::makeLiteral($1); }
| FLOAT { $$ = Expr::makeLiteral($1); }
| INT { $$ = Expr::makeLiteral($1); }
| num_literal
;
num_literal:
FLOAT { $$ = Expr::makeLiteral($1); }
| int_literal
;
int_literal:
INT { $$ = Expr::makeLiteral($1); }
;
star_expr:

View File

@ -19,9 +19,6 @@ typedef enum {
} StatementType;
const int64_t kNoLimit = -1;
const int64_t kNoOffset = -1;
typedef enum {
kOrderNone,
kOrderAsc,
@ -30,10 +27,19 @@ typedef enum {
struct OrderDescription {
OrderDescription(OrderType type, Expr* expr) : type(type), expr(expr) {}
OrderType type;
Expr* expr;
};
const int64_t kNoLimit = -1;
const int64_t kNoOffset = -1;
struct LimitDescription {
LimitDescription(int64_t limit, int64_t offset) : limit(limit), offset(offset) {}
int64_t limit;
int64_t offset;
};
typedef enum {
kJoinInner,
@ -43,6 +49,8 @@ typedef enum {
} JoinType;
struct Statement {
Statement(StatementType type) : type(type) {};
StatementType type;
@ -57,11 +65,9 @@ struct SelectStatement : Statement {
Expr* where_clause;
List<Expr*>* group_by;
Expr* having;
OrderDescription* order;
int64_t limit;
int64_t offset;
LimitDescription* limit;
};