15 let def_param_name name (id,t) =
18 | Next | Numbered _ -> name
26 %token <string> IDENT TEXT BLOB
27 %token <Stmt.param_id> PARAM
28 %token <Sql.Type.t option> FUNCTION
29 %token LPAREN RPAREN COMMA EOF DOT NULL
31 %token SELECT INSERT OR INTO CREATE UPDATE TABLE VALUES WHERE ASTERISK DISTINCT ALL
32 LIMIT ORDER BY DESC ASC EQUAL DELETE FROM DEFAULT OFFSET SET JOIN LIKE_OP
33 EXCL TILDE NOT TEST_NULL BETWEEN AND ESCAPE USING COMPOUND_OP AS
34 CONCAT_OP JOIN_TYPE1 JOIN_TYPE2 NATURAL REPLACE IN GROUP HAVING
35 %token UNIQUE PRIMARY KEY AUTOINCREMENT ON CONFLICT
36 %token NUM_BINARY_OP PLUS MINUS
37 %token T_INTEGER T_BLOB T_TEXT
42 %nonassoc NUM_BINARY_OP
46 %type <Syntax.expr> expr
48 %start <RA.Scheme.t * Stmt.params * Stmt.kind> input
52 input: statement EOF { $1 } ;
54 statement: CREATE TABLE name=IDENT LPAREN scheme=column_defs RPAREN
55 { let () = Tables.add (name,scheme) in ([],[],Create name) }
56 | CREATE TABLE name=IDENT AS select=select_stmt
63 { let (s,p) = $1 in s,p,Select }
64 | insert_cmd table=IDENT cols=columns_list? VALUES
66 let s = Tables.get_scheme table in
67 let s = match cols with
68 | Some cols -> RA.Scheme.project cols s
71 let p = Syntax.scheme_as_params s in
74 | update_cmd table=IDENT SET assignments=separated_nonempty_list(COMMA,set_column) w=where?
76 let t = Tables.get table in
77 let p2 = get_params_opt [t] w in
78 let (cols,exprs) = List.split assignments in
79 let _ = RA.Scheme.project cols (snd t) in (* validates columns *)
80 let p1 = Syntax.get_params_l [t] exprs in
81 [], p1 @ p2, Update table
83 | DELETE FROM table=IDENT w=where?
85 let p = get_params_opt [Tables.get table] w in
89 columns_list: LPAREN cols=separated_nonempty_list(COMMA,IDENT) RPAREN { cols }
91 select_stmt: select_core list(preceded(COMPOUND_OP,select_core)) o=loption(order) p4=loption(limit)
92 { let (s1,p1,tbls) = $1 in
93 let (s2,p2) = List.split (List.map (fun (s,p,_) -> s,p) $2) in (* ignore tables in compound statements - they cannot be used in ORDER BY *)
94 let p3 = Syntax.get_params_l tbls o in
95 List.fold_left RA.Scheme.compound s1 s2,(p1@(List.flatten p2)@p3@p4) }
97 select_core: SELECT select_type? r=separated_nonempty_list(COMMA,column1)
104 let p1 = Syntax.params_of_columns tbls r in
105 let p3 = Syntax.get_params_opt tbls w in
106 let p4 = Syntax.get_params_l tbls g in
107 let p5 = Syntax.get_params_opt tbls h in
108 (Syntax.get_scheme r tbls, p1 @ p2 @ p3 @ p4 @ p5, tbls)
111 table_list: source join_source*
113 let (s,p) = List.split $2 in
114 (fst $1::s, List.flatten (snd $1::p))
116 join_source: join_op s=source p=loption(join_args)
118 (* FIXME more tables in scope *)
119 (fst s,snd s @ Syntax.get_params_l [fst s] p)
121 source1: IDENT { Tables.get $1,[] }
122 | LPAREN s=select_core RPAREN { let (s,p,_) = s in ("",s),p }
123 source: src=source1 alias=preceded(AS,IDENT)?
126 | Some name -> let ((n,s),p) = src in ((name,s),p)
129 join_op: COMMA | NATURAL? JOIN_TYPE1? JOIN_TYPE2? JOIN { } ;
130 join_args: ON e=expr { [e] }
131 | USING LPAREN l=separated_nonempty_list(COMMA,IDENT) RPAREN { List.map (fun name -> `Column (name,None)) l }
133 insert_cmd: INSERT OR CONFLICT_ALGO INTO | INSERT INTO | REPLACE INTO { }
135 update_cmd: UPDATE {}
136 | UPDATE OR CONFLICT_ALGO {} ;
138 select_type: DISTINCT | ALL { }
140 int_or_param: INTEGER { [] }
141 | PARAM { [($1,Some Sql.Type.Int)] }
143 limit: LIMIT p=int_or_param { p }
144 | LIMIT p1=int_or_param COMMA p2=int_or_param { p1 @ p2 }
145 | LIMIT p1=int_or_param OFFSET p2=int_or_param { p1 @ p2 }
147 order: ORDER BY l=separated_nonempty_list(COMMA,terminated(expr,order_type?)) { l }
148 order_type: DESC | ASC { }
150 where: WHERE e=expr { e }
151 group: GROUP BY l=separated_nonempty_list(COMMA,expr) { l }
152 having: HAVING e=expr { e }
155 | IDENT DOT ASTERISK { Syntax.AllOf $1 }
156 | ASTERISK { Syntax.All }
157 | expr maybe_as { let e = $1 in Syntax.Expr (e,$2) }
159 maybe_as: AS? name=IDENT { Some name }
162 column_defs: separated_nonempty_list(COMMA,column_def1) { $1 }
163 column_def1: IDENT sql_type column_def_extra* { RA.attr $1 $2 } ;
164 column_def_extra: PRIMARY KEY { Some Constraint.PrimaryKey }
165 | NOT NULL { Some Constraint.NotNull }
166 | UNIQUE { Some Constraint.Unique }
167 | AUTOINCREMENT { Some Constraint.Autoincrement }
168 | ON CONFLICT CONFLICT_ALGO { None } ;
169 | DEFAULT INTEGER { None }
171 set_column: name=IDENT EQUAL e=expr { name,e }
173 (* expr: expr1 { $1 >> Syntax.expr_to_string >> prerr_endline; $1 } *)
175 mnot(X): NOT x = X | x = X { x }
178 expr numeric_bin_op expr %prec PLUS { `Func ((Some Int),[$1;$3]) }
179 | expr boolean_bin_op expr %prec AND { `Func ((Some Int),[$1;$3]) }
180 | expr CONCAT_OP expr { `Func ((Some Text),[$1;$3]) }
181 | e1=expr mnot(LIKE_OP) e2=expr e3=escape?
182 { `Func (None,(List.filter_valid [Some e1; Some e2; e3])) }
183 | unary_op expr { $2 }
184 | LPAREN expr RPAREN { $2 }
185 | IDENT { `Column ($1,None) }
186 | t=IDENT DOT c=IDENT
187 | IDENT DOT t=IDENT DOT c=IDENT { `Column (c,Some t) }
188 | INTEGER { `Value Int }
189 | e1=expr mnot(IN) LPAREN l=separated_nonempty_list(COMMA,expr) RPAREN { `Func (None,e1::l) }
190 | e1=expr mnot(IN) LPAREN select=select_stmt RPAREN
192 let (s,p) = select in
193 if (List.length s <> 1) then
194 raise (RA.Scheme.Error (s,"only one column allowed for IN operator"));
195 let l = List.map (fun x -> `Param x) p in
198 | e1=expr IN table=IDENT { Tables.check(table); e1 }
199 (* | FLOAT { `Value Float } *)
200 | TEXT { `Value Text }
201 | BLOB { `Value Blob }
202 | PARAM { `Param ($1,None) }
203 | FUNCTION LPAREN func_params RPAREN { `Func ($1,$3) }
204 | expr TEST_NULL { $1 }
205 | expr BETWEEN expr AND expr { `Func ((Some Int),[$1;$3;$5]) }
207 expr_list: separated_nonempty_list(COMMA,expr) { $1 }
208 func_params: expr_list { $1 }
210 escape: ESCAPE expr { $2 }
211 numeric_bin_op: EQUAL | PLUS | MINUS | ASTERISK | NUM_BINARY_OP { }
212 boolean_bin_op: AND | OR { }
220 sql_type: T_INTEGER { Type.Int }
221 | T_BLOB { Type.Blob }
222 | T_TEXT { Type.Text } ;