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; double fval;
int64_t ival; int64_t ival;
char* sval; char* sval;
uint uintnum; uint uval;
Statement* statement; Statement* statement;
SelectStatement* select_statement; SelectStatement* select_statement;
TableRef* table; TableRef* table;
Expr* expr; Expr* expr;
OrderDescription* order;
OrderType order_type;
LimitDescription* limit;
List<char*>* slist; List<char*>* slist;
List<Expr*>* explist; List<Expr*>* explist;
@ -79,14 +82,14 @@ typedef void* yyscan_t;
/********************************* /*********************************
** Token Definition ** 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 JOIN ON INNER OUTER LEFT RIGHT CROSS USING NATURAL
%token CREATE TABLE DATABASE INDEX %token CREATE TABLE DATABASE INDEX
%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
%token <ival> INT %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) ** 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 <sval> table_name
%type <table> from_clause table_ref table_ref_atomic %type <table> from_clause table_ref table_ref_atomic
%type <expr> expr scalar_expr unary_expr binary_expr function_expr star_expr %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 <expr> comp_expr where_clause
%type <explist> expr_list group_clause select_list %type <explist> expr_list group_clause select_list
%type <tbllist> table_ref_commalist %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: limit
// TODO: order by // TODO: order by
select_statement: 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(); SelectStatement* s = new SelectStatement();
s->select_list = $2; s->select_list = $2;
s->from_table = $3; s->from_table = $3;
s->where_clause = $4; s->where_clause = $4;
s->group_by = $5; s->group_by = $5;
s->having = NULL; // TODO s->order = $6;
s->order = NULL; // TODO s->limit = $7;
s->limit = kNoLimit; // TODO
s->offset = kNoOffset; // TODO
$$ = s; $$ = 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 ** Expressions
******************************/ ******************************/
@ -248,8 +265,16 @@ column_name:
literal: literal:
STRING { $$ = Expr::makeLiteral($1); } STRING { $$ = Expr::makeLiteral($1); }
| FLOAT { $$ = Expr::makeLiteral($1); } | num_literal
| INT { $$ = Expr::makeLiteral($1); } ;
num_literal:
FLOAT { $$ = Expr::makeLiteral($1); }
| int_literal
;
int_literal:
INT { $$ = Expr::makeLiteral($1); }
; ;
star_expr: star_expr:

View File

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