blob: bc60a78b65fa37b5ad351e9504b90f1413b07a83 [file] [log] [blame]
drh348784e2000-05-29 20:41:49 +00001/*
drhb19a2bc2001-09-16 00:13:26 +00002** 2001 September 15
drh348784e2000-05-29 20:41:49 +00003**
drhb19a2bc2001-09-16 00:13:26 +00004** The author disclaims copyright to this source code. In place of
5** a legal notice, here is a blessing:
drh348784e2000-05-29 20:41:49 +00006**
drhb19a2bc2001-09-16 00:13:26 +00007** May you do good and not evil.
8** May you find forgiveness for yourself and forgive others.
9** May you share freely, never taking more than you give.
drh348784e2000-05-29 20:41:49 +000010**
11*************************************************************************
12** This file contains SQLite's grammar for SQL. Process this file
13** using the lemon parser generator to generate C code that runs
14** the parser. Lemon will also generate a header file containing
15** numeric codes for all of the tokens.
16**
drhce9079c2002-05-15 14:17:44 +000017** @(#) $Id: parse.y,v 1.65 2002/05/15 14:17:45 drh Exp $
drh348784e2000-05-29 20:41:49 +000018*/
19%token_prefix TK_
20%token_type {Token}
drhf57b14a2001-09-14 18:54:08 +000021%default_type {Token}
drh348784e2000-05-29 20:41:49 +000022%extra_argument {Parse *pParse}
23%syntax_error {
drhc837e702000-06-08 16:26:24 +000024 sqliteSetString(&pParse->zErrMsg,"syntax error",0);
drh348784e2000-05-29 20:41:49 +000025 pParse->sErrToken = TOKEN;
26}
27%name sqliteParser
28%include {
29#include "sqliteInt.h"
30#include "parse.h"
drh9bbca4c2001-11-06 04:00:18 +000031
32/*
33** A structure for holding two integers
34*/
35struct twoint { int a,b; };
danielk1977c3f9bad2002-05-15 08:30:12 +000036
37/*
38** A structure for holding an integer and an IdList
39*/
40struct int_idlist { int a; IdList * b; };
drh348784e2000-05-29 20:41:49 +000041}
42
drh348784e2000-05-29 20:41:49 +000043// These are extra tokens used by the lexer but never seen by the
44// parser. We put them in a rule so that the parser generator will
drh6206d502000-06-19 19:09:08 +000045// add them to the parse.h output file.
drh348784e2000-05-29 20:41:49 +000046//
drhc4a3c772001-04-04 11:48:57 +000047%nonassoc END_OF_FILE ILLEGAL SPACE UNCLOSED_STRING COMMENT FUNCTION
48 COLUMN AGG_FUNCTION.
49
50// Input is zero or more commands.
51input ::= cmdlist.
drh348784e2000-05-29 20:41:49 +000052
53// A list of commands is zero or more commands
54//
55cmdlist ::= ecmd.
drh094b2bb2002-03-13 18:54:07 +000056cmdlist ::= cmdlist ecmd.
57ecmd ::= explain cmd SEMI. {sqliteExec(pParse);}
58ecmd ::= cmd SEMI. {sqliteExec(pParse);}
59ecmd ::= SEMI.
drh348784e2000-05-29 20:41:49 +000060explain ::= EXPLAIN. {pParse->explain = 1;}
61
drh382c0242001-10-06 16:33:02 +000062///////////////////// Begin and end transactions. ////////////////////////////
drhc4a3c772001-04-04 11:48:57 +000063//
drhfa86c412002-02-02 15:01:15 +000064
drh0d65dc02002-02-03 00:56:09 +000065cmd ::= BEGIN trans_opt onconf(R). {sqliteBeginTransaction(pParse,R);}
drhc4a3c772001-04-04 11:48:57 +000066trans_opt ::= .
67trans_opt ::= TRANSACTION.
68trans_opt ::= TRANSACTION ids.
69cmd ::= COMMIT trans_opt. {sqliteCommitTransaction(pParse);}
70cmd ::= END trans_opt. {sqliteCommitTransaction(pParse);}
71cmd ::= ROLLBACK trans_opt. {sqliteRollbackTransaction(pParse);}
72
drh382c0242001-10-06 16:33:02 +000073///////////////////// The CREATE TABLE statement ////////////////////////////
drh348784e2000-05-29 20:41:49 +000074//
75cmd ::= create_table create_table_args.
drh969fa7c2002-02-18 18:30:32 +000076create_table ::= CREATE(X) temp(T) TABLE ids(Y). {
77 sqliteStartTable(pParse,&X,&Y,T);
78}
drhf57b3392001-10-08 13:22:32 +000079%type temp {int}
80temp(A) ::= TEMP. {A = 1;}
81temp(A) ::= . {A = 0;}
drh969fa7c2002-02-18 18:30:32 +000082create_table_args ::= LP columnlist conslist_opt RP(X). {
83 sqliteEndTable(pParse,&X,0);
84}
85create_table_args ::= AS select(S). {
86 sqliteEndTable(pParse,0,S);
87 sqliteSelectDelete(S);
88}
drh348784e2000-05-29 20:41:49 +000089columnlist ::= columnlist COMMA column.
90columnlist ::= column.
91
92// About the only information used for a column is the name of the
93// column. The type is always just "text". But the code will accept
94// an elaborate typename. Perhaps someday we'll do something with it.
95//
96column ::= columnid type carglist.
drhc4a3c772001-04-04 11:48:57 +000097columnid ::= ids(X). {sqliteAddColumn(pParse,&X);}
98
99// An IDENTIFIER can be a generic identifier, or one of several
100// keywords. Any non-standard keyword can also be an identifier.
drh382c0242001-10-06 16:33:02 +0000101// We also make DESC and identifier since it comes up so often (as
102// an abbreviation of "description").
drhc4a3c772001-04-04 11:48:57 +0000103//
drh982cef72000-05-30 16:27:03 +0000104%type id {Token}
drh9cfcf5d2002-01-29 18:41:24 +0000105id(A) ::= ABORT(X). {A = X;}
drhf18543c2002-03-30 15:26:50 +0000106id(A) ::= ASC(X). {A = X;}
107id(A) ::= BEGIN(X). {A = X;}
108id(A) ::= CLUSTER(X). {A = X;}
drh9cfcf5d2002-01-29 18:41:24 +0000109id(A) ::= CONFLICT(X). {A = X;}
drhf18543c2002-03-30 15:26:50 +0000110id(A) ::= COPY(X). {A = X;}
111id(A) ::= DELIMITERS(X). {A = X;}
112id(A) ::= DESC(X). {A = X;}
113id(A) ::= END(X). {A = X;}
114id(A) ::= EXPLAIN(X). {A = X;}
115id(A) ::= FAIL(X). {A = X;}
116id(A) ::= ID(X). {A = X;}
117id(A) ::= IGNORE(X). {A = X;}
118id(A) ::= KEY(X). {A = X;}
119id(A) ::= OFFSET(X). {A = X;}
120id(A) ::= PRAGMA(X). {A = X;}
121id(A) ::= REPLACE(X). {A = X;}
122id(A) ::= TEMP(X). {A = X;}
drhce9079c2002-05-15 14:17:44 +0000123id(A) ::= TRIGGER(X). {A = X;}
drhf18543c2002-03-30 15:26:50 +0000124id(A) ::= VACUUM(X). {A = X;}
125id(A) ::= VIEW(X). {A = X;}
drhc4a3c772001-04-04 11:48:57 +0000126
127// And "ids" is an identifer-or-string.
128//
129%type ids {Token}
130ids(A) ::= id(X). {A = X;}
131ids(A) ::= STRING(X). {A = X;}
132
drh382c0242001-10-06 16:33:02 +0000133type ::= .
134type ::= typename(X). {sqliteAddColumnType(pParse,&X,&X);}
135type ::= typename(X) LP signed RP(Y). {sqliteAddColumnType(pParse,&X,&Y);}
136type ::= typename(X) LP signed COMMA signed RP(Y).
137 {sqliteAddColumnType(pParse,&X,&Y);}
138%type typename {Token}
139typename(A) ::= ids(X). {A = X;}
140typename(A) ::= typename(X) ids. {A = X;}
drh348784e2000-05-29 20:41:49 +0000141signed ::= INTEGER.
142signed ::= PLUS INTEGER.
143signed ::= MINUS INTEGER.
144carglist ::= carglist carg.
145carglist ::= .
drhc4a3c772001-04-04 11:48:57 +0000146carg ::= CONSTRAINT ids ccons.
drh348784e2000-05-29 20:41:49 +0000147carg ::= ccons.
drh7020f652000-06-03 18:06:52 +0000148carg ::= DEFAULT STRING(X). {sqliteAddDefaultValue(pParse,&X,0);}
149carg ::= DEFAULT ID(X). {sqliteAddDefaultValue(pParse,&X,0);}
150carg ::= DEFAULT INTEGER(X). {sqliteAddDefaultValue(pParse,&X,0);}
151carg ::= DEFAULT PLUS INTEGER(X). {sqliteAddDefaultValue(pParse,&X,0);}
152carg ::= DEFAULT MINUS INTEGER(X). {sqliteAddDefaultValue(pParse,&X,1);}
153carg ::= DEFAULT FLOAT(X). {sqliteAddDefaultValue(pParse,&X,0);}
154carg ::= DEFAULT PLUS FLOAT(X). {sqliteAddDefaultValue(pParse,&X,0);}
155carg ::= DEFAULT MINUS FLOAT(X). {sqliteAddDefaultValue(pParse,&X,1);}
156carg ::= DEFAULT NULL.
drh348784e2000-05-29 20:41:49 +0000157
drh382c0242001-10-06 16:33:02 +0000158// In addition to the type name, we also care about the primary key and
159// UNIQUE constraints.
drh348784e2000-05-29 20:41:49 +0000160//
drh9cfcf5d2002-01-29 18:41:24 +0000161ccons ::= NOT NULL onconf(R). {sqliteAddNotNull(pParse, R);}
162ccons ::= PRIMARY KEY sortorder onconf(R). {sqliteAddPrimaryKey(pParse,0,R);}
163ccons ::= UNIQUE onconf(R). {sqliteCreateIndex(pParse,0,0,0,R,0,0);}
164ccons ::= CHECK LP expr RP onconf.
drh348784e2000-05-29 20:41:49 +0000165
166// For the time being, the only constraint we care about is the primary
drh382c0242001-10-06 16:33:02 +0000167// key and UNIQUE. Both create indices.
drh348784e2000-05-29 20:41:49 +0000168//
169conslist_opt ::= .
170conslist_opt ::= COMMA conslist.
171conslist ::= conslist COMMA tcons.
drha2e1bb52001-01-04 14:20:18 +0000172conslist ::= conslist tcons.
drh348784e2000-05-29 20:41:49 +0000173conslist ::= tcons.
drhc4a3c772001-04-04 11:48:57 +0000174tcons ::= CONSTRAINT ids.
drh9cfcf5d2002-01-29 18:41:24 +0000175tcons ::= PRIMARY KEY LP idxlist(X) RP onconf(R).
176 {sqliteAddPrimaryKey(pParse,X,R);}
177tcons ::= UNIQUE LP idxlist(X) RP onconf(R).
178 {sqliteCreateIndex(pParse,0,0,X,R,0,0);}
179tcons ::= CHECK expr onconf.
180
181// The following is a non-standard extension that allows us to declare the
182// default behavior when there is a constraint conflict.
183//
184%type onconf {int}
drh1c928532002-01-31 15:54:21 +0000185%type orconf {int}
186%type resolvetype {int}
187onconf(A) ::= . { A = OE_Default; }
188onconf(A) ::= ON CONFLICT resolvetype(X). { A = X; }
189orconf(A) ::= . { A = OE_Default; }
190orconf(A) ::= OR resolvetype(X). { A = X; }
191resolvetype(A) ::= ROLLBACK. { A = OE_Rollback; }
192resolvetype(A) ::= ABORT. { A = OE_Abort; }
193resolvetype(A) ::= FAIL. { A = OE_Fail; }
194resolvetype(A) ::= IGNORE. { A = OE_Ignore; }
195resolvetype(A) ::= REPLACE. { A = OE_Replace; }
drh348784e2000-05-29 20:41:49 +0000196
drh382c0242001-10-06 16:33:02 +0000197////////////////////////// The DROP TABLE /////////////////////////////////////
drh348784e2000-05-29 20:41:49 +0000198//
drh4ff6dfa2002-03-03 23:06:00 +0000199cmd ::= DROP TABLE ids(X). {sqliteDropTable(pParse,&X,0);}
drh348784e2000-05-29 20:41:49 +0000200
drha76b5df2002-02-23 02:32:10 +0000201///////////////////// The CREATE VIEW statement /////////////////////////////
202//
203cmd ::= CREATE(X) VIEW ids(Y) AS select(S). {
204 sqliteCreateView(pParse, &X, &Y, S);
205}
206cmd ::= DROP VIEW ids(X). {
drh4ff6dfa2002-03-03 23:06:00 +0000207 sqliteDropTable(pParse, &X, 1);
drha76b5df2002-02-23 02:32:10 +0000208}
209
drh382c0242001-10-06 16:33:02 +0000210//////////////////////// The SELECT statement /////////////////////////////////
drh348784e2000-05-29 20:41:49 +0000211//
drh9bb61fe2000-06-05 16:01:39 +0000212cmd ::= select(X). {
drh832508b2002-03-02 17:04:07 +0000213 sqliteSelect(pParse, X, SRT_Callback, 0, 0, 0, 0);
drh9bb61fe2000-06-05 16:01:39 +0000214 sqliteSelectDelete(X);
215}
drhefb72512000-05-31 20:00:52 +0000216
drh9bb61fe2000-06-05 16:01:39 +0000217%type select {Select*}
218%destructor select {sqliteSelectDelete($$);}
drh82c3d632000-06-06 21:56:07 +0000219%type oneselect {Select*}
220%destructor oneselect {sqliteSelectDelete($$);}
drh9bb61fe2000-06-05 16:01:39 +0000221
drh82c3d632000-06-06 21:56:07 +0000222select(A) ::= oneselect(X). {A = X;}
drh0a36c572002-02-18 22:49:59 +0000223select(A) ::= select(X) multiselect_op(Y) oneselect(Z). {
drhdaffd0e2001-04-11 14:28:42 +0000224 if( Z ){
drh82c3d632000-06-06 21:56:07 +0000225 Z->op = Y;
226 Z->pPrior = X;
drhdaffd0e2001-04-11 14:28:42 +0000227 }
228 A = Z;
drh82c3d632000-06-06 21:56:07 +0000229}
drh0a36c572002-02-18 22:49:59 +0000230%type multiselect_op {int}
231multiselect_op(A) ::= UNION. {A = TK_UNION;}
232multiselect_op(A) ::= UNION ALL. {A = TK_ALL;}
233multiselect_op(A) ::= INTERSECT. {A = TK_INTERSECT;}
234multiselect_op(A) ::= EXCEPT. {A = TK_EXCEPT;}
drh82c3d632000-06-06 21:56:07 +0000235oneselect(A) ::= SELECT distinct(D) selcollist(W) from(X) where_opt(Y)
drh9bbca4c2001-11-06 04:00:18 +0000236 groupby_opt(P) having_opt(Q) orderby_opt(Z) limit_opt(L). {
237 A = sqliteSelectNew(W,X,Y,P,Q,Z,D,L.a,L.b);
drh9bb61fe2000-06-05 16:01:39 +0000238}
239
240// The "distinct" nonterminal is true (1) if the DISTINCT keyword is
241// present and false (0) if it is not.
242//
drhefb72512000-05-31 20:00:52 +0000243%type distinct {int}
drhefb72512000-05-31 20:00:52 +0000244distinct(A) ::= DISTINCT. {A = 1;}
drhfef52082000-06-06 01:50:43 +0000245distinct(A) ::= ALL. {A = 0;}
drhefb72512000-05-31 20:00:52 +0000246distinct(A) ::= . {A = 0;}
drh348784e2000-05-29 20:41:49 +0000247
drh9bb61fe2000-06-05 16:01:39 +0000248// selcollist is a list of expressions that are to become the return
drh7c917d12001-12-16 20:05:05 +0000249// values of the SELECT statement. The "*" in statements like
250// "SELECT * FROM ..." is encoded as a special expression with an
251// opcode of TK_ALL.
drh9bb61fe2000-06-05 16:01:39 +0000252//
drh348784e2000-05-29 20:41:49 +0000253%type selcollist {ExprList*}
254%destructor selcollist {sqliteExprListDelete($$);}
255%type sclp {ExprList*}
256%destructor sclp {sqliteExprListDelete($$);}
drh348784e2000-05-29 20:41:49 +0000257sclp(A) ::= selcollist(X) COMMA. {A = X;}
258sclp(A) ::= . {A = 0;}
259selcollist(A) ::= sclp(P) expr(X). {A = sqliteExprListAppend(P,X,0);}
drhc4a3c772001-04-04 11:48:57 +0000260selcollist(A) ::= sclp(P) expr(X) as ids(Y). {A = sqliteExprListAppend(P,X,&Y);}
drh7c917d12001-12-16 20:05:05 +0000261selcollist(A) ::= sclp(P) STAR. {
262 A = sqliteExprListAppend(P, sqliteExpr(TK_ALL, 0, 0, 0), 0);
263}
drh54473222002-04-04 02:10:55 +0000264selcollist(A) ::= sclp(P) ids(X) DOT STAR. {
265 Expr *pRight = sqliteExpr(TK_ALL, 0, 0, 0);
266 Expr *pLeft = sqliteExpr(TK_ID, 0, 0, &X);
267 A = sqliteExprListAppend(P, sqliteExpr(TK_DOT, pLeft, pRight, 0), 0);
268}
drh9bb61fe2000-06-05 16:01:39 +0000269as ::= .
270as ::= AS.
271
drh348784e2000-05-29 20:41:49 +0000272
273%type seltablist {IdList*}
274%destructor seltablist {sqliteIdListDelete($$);}
275%type stl_prefix {IdList*}
276%destructor stl_prefix {sqliteIdListDelete($$);}
277%type from {IdList*}
278%destructor from {sqliteIdListDelete($$);}
279
drhbf3a4fa2002-04-06 13:57:42 +0000280from(A) ::= . {A = sqliteMalloc(sizeof(*A));}
drh348784e2000-05-29 20:41:49 +0000281from(A) ::= FROM seltablist(X). {A = X;}
282stl_prefix(A) ::= seltablist(X) COMMA. {A = X;}
283stl_prefix(A) ::= . {A = 0;}
drhc4a3c772001-04-04 11:48:57 +0000284seltablist(A) ::= stl_prefix(X) ids(Y). {A = sqliteIdListAppend(X,&Y);}
285seltablist(A) ::= stl_prefix(X) ids(Y) as ids(Z). {
286 A = sqliteIdListAppend(X,&Y);
287 sqliteIdListAddAlias(A,&Z);
288}
drh22f70c32002-02-18 01:17:00 +0000289seltablist(A) ::= stl_prefix(X) LP select(S) RP. {
290 A = sqliteIdListAppend(X,0);
291 A->a[A->nId-1].pSelect = S;
drhd5feede2002-05-08 21:46:14 +0000292 if( S->pOrderBy ){
293 sqliteExprListDelete(S->pOrderBy);
294 S->pOrderBy = 0;
295 }
drh22f70c32002-02-18 01:17:00 +0000296}
297seltablist(A) ::= stl_prefix(X) LP select(S) RP as ids(Z). {
298 A = sqliteIdListAppend(X,0);
299 A->a[A->nId-1].pSelect = S;
drhd5feede2002-05-08 21:46:14 +0000300 if( S->pOrderBy ){
301 sqliteExprListDelete(S->pOrderBy);
302 S->pOrderBy = 0;
303 }
drh22f70c32002-02-18 01:17:00 +0000304 sqliteIdListAddAlias(A,&Z);
305}
drh348784e2000-05-29 20:41:49 +0000306
307%type orderby_opt {ExprList*}
308%destructor orderby_opt {sqliteExprListDelete($$);}
309%type sortlist {ExprList*}
310%destructor sortlist {sqliteExprListDelete($$);}
311%type sortitem {Expr*}
312%destructor sortitem {sqliteExprDelete($$);}
313
314orderby_opt(A) ::= . {A = 0;}
315orderby_opt(A) ::= ORDER BY sortlist(X). {A = X;}
drh9bb61fe2000-06-05 16:01:39 +0000316sortlist(A) ::= sortlist(X) COMMA sortitem(Y) sortorder(Z). {
317 A = sqliteExprListAppend(X,Y,0);
drhdaffd0e2001-04-11 14:28:42 +0000318 if( A ) A->a[A->nExpr-1].sortOrder = Z; /* 0=ascending, 1=decending */
drh9bb61fe2000-06-05 16:01:39 +0000319}
320sortlist(A) ::= sortitem(Y) sortorder(Z). {
321 A = sqliteExprListAppend(0,Y,0);
drhdaffd0e2001-04-11 14:28:42 +0000322 if( A ) A->a[0].sortOrder = Z;
drh9bb61fe2000-06-05 16:01:39 +0000323}
drhda9d6c42000-05-31 18:20:14 +0000324sortitem(A) ::= expr(X). {A = X;}
drh348784e2000-05-29 20:41:49 +0000325
326%type sortorder {int}
327
328sortorder(A) ::= ASC. {A = 0;}
329sortorder(A) ::= DESC. {A = 1;}
330sortorder(A) ::= . {A = 0;}
331
drh22827922000-06-06 17:27:05 +0000332%type groupby_opt {ExprList*}
333%destructor groupby_opt {sqliteExprListDelete($$);}
drh6206d502000-06-19 19:09:08 +0000334groupby_opt(A) ::= . {A = 0;}
drh22827922000-06-06 17:27:05 +0000335groupby_opt(A) ::= GROUP BY exprlist(X). {A = X;}
336
337%type having_opt {Expr*}
338%destructor having_opt {sqliteExprDelete($$);}
drh6206d502000-06-19 19:09:08 +0000339having_opt(A) ::= . {A = 0;}
drh22827922000-06-06 17:27:05 +0000340having_opt(A) ::= HAVING expr(X). {A = X;}
341
drh9bbca4c2001-11-06 04:00:18 +0000342%type limit_opt {struct twoint}
343limit_opt(A) ::= . {A.a = -1; A.b = 0;}
344limit_opt(A) ::= LIMIT INTEGER(X). {A.a = atoi(X.z); A.b = 0;}
345limit_opt(A) ::= LIMIT INTEGER(X) limit_sep INTEGER(Y).
346 {A.a = atoi(X.z); A.b = atoi(Y.z);}
347limit_sep ::= OFFSET.
348limit_sep ::= COMMA.
349
drh382c0242001-10-06 16:33:02 +0000350/////////////////////////// The DELETE statement /////////////////////////////
351//
drhc4a3c772001-04-04 11:48:57 +0000352cmd ::= DELETE FROM ids(X) where_opt(Y).
drh348784e2000-05-29 20:41:49 +0000353 {sqliteDeleteFrom(pParse, &X, Y);}
354
355%type where_opt {Expr*}
356%destructor where_opt {sqliteExprDelete($$);}
357
358where_opt(A) ::= . {A = 0;}
359where_opt(A) ::= WHERE expr(X). {A = X;}
360
361%type setlist {ExprList*}
362%destructor setlist {sqliteExprListDelete($$);}
363
drh382c0242001-10-06 16:33:02 +0000364////////////////////////// The UPDATE command ////////////////////////////////
365//
drh1c928532002-01-31 15:54:21 +0000366cmd ::= UPDATE orconf(R) ids(X) SET setlist(Y) where_opt(Z).
drh9cfcf5d2002-01-29 18:41:24 +0000367 {sqliteUpdate(pParse,&X,Y,Z,R);}
drh348784e2000-05-29 20:41:49 +0000368
drhc4a3c772001-04-04 11:48:57 +0000369setlist(A) ::= setlist(Z) COMMA ids(X) EQ expr(Y).
drh348784e2000-05-29 20:41:49 +0000370 {A = sqliteExprListAppend(Z,Y,&X);}
drhc4a3c772001-04-04 11:48:57 +0000371setlist(A) ::= ids(X) EQ expr(Y). {A = sqliteExprListAppend(0,Y,&X);}
drh348784e2000-05-29 20:41:49 +0000372
drh382c0242001-10-06 16:33:02 +0000373////////////////////////// The INSERT command /////////////////////////////////
374//
drhfa86c412002-02-02 15:01:15 +0000375cmd ::= insert_cmd(R) INTO ids(X) inscollist_opt(F) VALUES LP itemlist(Y) RP.
drh9cfcf5d2002-01-29 18:41:24 +0000376 {sqliteInsert(pParse, &X, Y, 0, F, R);}
drhfa86c412002-02-02 15:01:15 +0000377cmd ::= insert_cmd(R) INTO ids(X) inscollist_opt(F) select(S).
drh9cfcf5d2002-01-29 18:41:24 +0000378 {sqliteInsert(pParse, &X, 0, S, F, R);}
drh348784e2000-05-29 20:41:49 +0000379
drhfa86c412002-02-02 15:01:15 +0000380%type insert_cmd {int}
381insert_cmd(A) ::= INSERT orconf(R). {A = R;}
382insert_cmd(A) ::= REPLACE. {A = OE_Replace;}
383
drh348784e2000-05-29 20:41:49 +0000384
385%type itemlist {ExprList*}
386%destructor itemlist {sqliteExprListDelete($$);}
drh348784e2000-05-29 20:41:49 +0000387
drhe64e7b22002-02-18 13:56:36 +0000388itemlist(A) ::= itemlist(X) COMMA expr(Y). {A = sqliteExprListAppend(X,Y,0);}
389itemlist(A) ::= expr(X). {A = sqliteExprListAppend(0,X,0);}
drh348784e2000-05-29 20:41:49 +0000390
drh967e8b72000-06-21 13:59:10 +0000391%type inscollist_opt {IdList*}
392%destructor inscollist_opt {sqliteIdListDelete($$);}
393%type inscollist {IdList*}
394%destructor inscollist {sqliteIdListDelete($$);}
drh348784e2000-05-29 20:41:49 +0000395
drhc4a3c772001-04-04 11:48:57 +0000396inscollist_opt(A) ::= . {A = 0;}
397inscollist_opt(A) ::= LP inscollist(X) RP. {A = X;}
398inscollist(A) ::= inscollist(X) COMMA ids(Y). {A = sqliteIdListAppend(X,&Y);}
399inscollist(A) ::= ids(Y). {A = sqliteIdListAppend(0,&Y);}
drh348784e2000-05-29 20:41:49 +0000400
drh382c0242001-10-06 16:33:02 +0000401/////////////////////////// Expression Processing /////////////////////////////
402//
drh348784e2000-05-29 20:41:49 +0000403%left OR.
404%left AND.
drh8be51132000-06-03 19:19:41 +0000405%right NOT.
drhfef52082000-06-06 01:50:43 +0000406%left EQ NE ISNULL NOTNULL IS LIKE GLOB BETWEEN IN.
drh348784e2000-05-29 20:41:49 +0000407%left GT GE LT LE.
drh81a20f22001-10-12 17:30:04 +0000408%left BITAND BITOR LSHIFT RSHIFT.
drh348784e2000-05-29 20:41:49 +0000409%left PLUS MINUS.
drhbf4133c2001-10-13 02:59:08 +0000410%left STAR SLASH REM.
drh00400772000-06-16 20:51:26 +0000411%left CONCAT.
drh81a20f22001-10-12 17:30:04 +0000412%right UMINUS BITNOT.
drh348784e2000-05-29 20:41:49 +0000413
414%type expr {Expr*}
415%destructor expr {sqliteExprDelete($$);}
416
drhe1b6a5b2000-07-29 13:06:59 +0000417expr(A) ::= LP(B) expr(X) RP(E). {A = X; sqliteExprSpan(A,&B,&E);}
drhe1b6a5b2000-07-29 13:06:59 +0000418expr(A) ::= NULL(X). {A = sqliteExpr(TK_NULL, 0, 0, &X);}
drhc4a3c772001-04-04 11:48:57 +0000419expr(A) ::= id(X). {A = sqliteExpr(TK_ID, 0, 0, &X);}
420expr(A) ::= ids(X) DOT ids(Y). {
drhe1b6a5b2000-07-29 13:06:59 +0000421 Expr *temp1 = sqliteExpr(TK_ID, 0, 0, &X);
422 Expr *temp2 = sqliteExpr(TK_ID, 0, 0, &Y);
423 A = sqliteExpr(TK_DOT, temp1, temp2, 0);
424}
drh348784e2000-05-29 20:41:49 +0000425expr(A) ::= INTEGER(X). {A = sqliteExpr(TK_INTEGER, 0, 0, &X);}
426expr(A) ::= FLOAT(X). {A = sqliteExpr(TK_FLOAT, 0, 0, &X);}
427expr(A) ::= STRING(X). {A = sqliteExpr(TK_STRING, 0, 0, &X);}
drhe1b6a5b2000-07-29 13:06:59 +0000428expr(A) ::= ID(X) LP exprlist(Y) RP(E). {
429 A = sqliteExprFunction(Y, &X);
430 sqliteExprSpan(A,&X,&E);
431}
432expr(A) ::= ID(X) LP STAR RP(E). {
433 A = sqliteExprFunction(0, &X);
434 sqliteExprSpan(A,&X,&E);
435}
drh348784e2000-05-29 20:41:49 +0000436expr(A) ::= expr(X) AND expr(Y). {A = sqliteExpr(TK_AND, X, Y, 0);}
437expr(A) ::= expr(X) OR expr(Y). {A = sqliteExpr(TK_OR, X, Y, 0);}
438expr(A) ::= expr(X) LT expr(Y). {A = sqliteExpr(TK_LT, X, Y, 0);}
439expr(A) ::= expr(X) GT expr(Y). {A = sqliteExpr(TK_GT, X, Y, 0);}
440expr(A) ::= expr(X) LE expr(Y). {A = sqliteExpr(TK_LE, X, Y, 0);}
441expr(A) ::= expr(X) GE expr(Y). {A = sqliteExpr(TK_GE, X, Y, 0);}
442expr(A) ::= expr(X) NE expr(Y). {A = sqliteExpr(TK_NE, X, Y, 0);}
443expr(A) ::= expr(X) EQ expr(Y). {A = sqliteExpr(TK_EQ, X, Y, 0);}
drh81a20f22001-10-12 17:30:04 +0000444expr(A) ::= expr(X) BITAND expr(Y). {A = sqliteExpr(TK_BITAND, X, Y, 0);}
445expr(A) ::= expr(X) BITOR expr(Y). {A = sqliteExpr(TK_BITOR, X, Y, 0);}
446expr(A) ::= expr(X) LSHIFT expr(Y). {A = sqliteExpr(TK_LSHIFT, X, Y, 0);}
447expr(A) ::= expr(X) RSHIFT expr(Y). {A = sqliteExpr(TK_RSHIFT, X, Y, 0);}
drh0ac65892002-04-20 14:24:41 +0000448expr(A) ::= expr(X) likeop(OP) expr(Y). [LIKE] {
449 ExprList *pList = sqliteExprListAppend(0, Y, 0);
450 pList = sqliteExprListAppend(pList, X, 0);
451 A = sqliteExprFunction(pList, &OP);
452 sqliteExprSpan(A, &X->span, &Y->span);
453}
454expr(A) ::= expr(X) NOT likeop(OP) expr(Y). [LIKE] {
455 ExprList *pList = sqliteExprListAppend(0, Y, 0);
456 pList = sqliteExprListAppend(pList, X, 0);
457 A = sqliteExprFunction(pList, &OP);
drh4794b982000-06-06 13:54:14 +0000458 A = sqliteExpr(TK_NOT, A, 0, 0);
drhe1b6a5b2000-07-29 13:06:59 +0000459 sqliteExprSpan(A,&X->span,&Y->span);
drh4794b982000-06-06 13:54:14 +0000460}
drh0ac65892002-04-20 14:24:41 +0000461likeop(A) ::= LIKE(X). {A = X;}
462likeop(A) ::= GLOB(X). {A = X;}
drh348784e2000-05-29 20:41:49 +0000463expr(A) ::= expr(X) PLUS expr(Y). {A = sqliteExpr(TK_PLUS, X, Y, 0);}
464expr(A) ::= expr(X) MINUS expr(Y). {A = sqliteExpr(TK_MINUS, X, Y, 0);}
465expr(A) ::= expr(X) STAR expr(Y). {A = sqliteExpr(TK_STAR, X, Y, 0);}
466expr(A) ::= expr(X) SLASH expr(Y). {A = sqliteExpr(TK_SLASH, X, Y, 0);}
drhbf4133c2001-10-13 02:59:08 +0000467expr(A) ::= expr(X) REM expr(Y). {A = sqliteExpr(TK_REM, X, Y, 0);}
drh00400772000-06-16 20:51:26 +0000468expr(A) ::= expr(X) CONCAT expr(Y). {A = sqliteExpr(TK_CONCAT, X, Y, 0);}
drhe1b6a5b2000-07-29 13:06:59 +0000469expr(A) ::= expr(X) ISNULL(E). {
470 A = sqliteExpr(TK_ISNULL, X, 0, 0);
471 sqliteExprSpan(A,&X->span,&E);
472}
drh33048c02001-10-01 14:29:22 +0000473expr(A) ::= expr(X) IS NULL(E). {
474 A = sqliteExpr(TK_ISNULL, X, 0, 0);
475 sqliteExprSpan(A,&X->span,&E);
476}
drhe1b6a5b2000-07-29 13:06:59 +0000477expr(A) ::= expr(X) NOTNULL(E). {
478 A = sqliteExpr(TK_NOTNULL, X, 0, 0);
479 sqliteExprSpan(A,&X->span,&E);
480}
drh33048c02001-10-01 14:29:22 +0000481expr(A) ::= expr(X) NOT NULL(E). {
482 A = sqliteExpr(TK_NOTNULL, X, 0, 0);
483 sqliteExprSpan(A,&X->span,&E);
484}
drh81a20f22001-10-12 17:30:04 +0000485expr(A) ::= expr(X) IS NOT NULL(E). {
486 A = sqliteExpr(TK_NOTNULL, X, 0, 0);
487 sqliteExprSpan(A,&X->span,&E);
488}
drhe1b6a5b2000-07-29 13:06:59 +0000489expr(A) ::= NOT(B) expr(X). {
490 A = sqliteExpr(TK_NOT, X, 0, 0);
491 sqliteExprSpan(A,&B,&X->span);
492}
drh81a20f22001-10-12 17:30:04 +0000493expr(A) ::= BITNOT(B) expr(X). {
494 A = sqliteExpr(TK_BITNOT, X, 0, 0);
495 sqliteExprSpan(A,&B,&X->span);
496}
drhe1b6a5b2000-07-29 13:06:59 +0000497expr(A) ::= MINUS(B) expr(X). [UMINUS] {
498 A = sqliteExpr(TK_UMINUS, X, 0, 0);
499 sqliteExprSpan(A,&B,&X->span);
500}
501expr(A) ::= PLUS(B) expr(X). [UMINUS] {
502 A = X;
503 sqliteExprSpan(A,&B,&X->span);
504}
505expr(A) ::= LP(B) select(X) RP(E). {
drh19a775c2000-06-05 18:54:46 +0000506 A = sqliteExpr(TK_SELECT, 0, 0, 0);
drhdaffd0e2001-04-11 14:28:42 +0000507 if( A ) A->pSelect = X;
drhe1b6a5b2000-07-29 13:06:59 +0000508 sqliteExprSpan(A,&B,&E);
drh19a775c2000-06-05 18:54:46 +0000509}
drhfef52082000-06-06 01:50:43 +0000510expr(A) ::= expr(W) BETWEEN expr(X) AND expr(Y). {
511 ExprList *pList = sqliteExprListAppend(0, X, 0);
512 pList = sqliteExprListAppend(pList, Y, 0);
513 A = sqliteExpr(TK_BETWEEN, W, 0, 0);
drhdaffd0e2001-04-11 14:28:42 +0000514 if( A ) A->pList = pList;
drhe1b6a5b2000-07-29 13:06:59 +0000515 sqliteExprSpan(A,&W->span,&Y->span);
drhfef52082000-06-06 01:50:43 +0000516}
drh4794b982000-06-06 13:54:14 +0000517expr(A) ::= expr(W) NOT BETWEEN expr(X) AND expr(Y). {
518 ExprList *pList = sqliteExprListAppend(0, X, 0);
519 pList = sqliteExprListAppend(pList, Y, 0);
520 A = sqliteExpr(TK_BETWEEN, W, 0, 0);
drhdaffd0e2001-04-11 14:28:42 +0000521 if( A ) A->pList = pList;
drh4794b982000-06-06 13:54:14 +0000522 A = sqliteExpr(TK_NOT, A, 0, 0);
drhe1b6a5b2000-07-29 13:06:59 +0000523 sqliteExprSpan(A,&W->span,&Y->span);
drh4794b982000-06-06 13:54:14 +0000524}
drhe1b6a5b2000-07-29 13:06:59 +0000525expr(A) ::= expr(X) IN LP exprlist(Y) RP(E). {
drhfef52082000-06-06 01:50:43 +0000526 A = sqliteExpr(TK_IN, X, 0, 0);
drhdaffd0e2001-04-11 14:28:42 +0000527 if( A ) A->pList = Y;
drhe1b6a5b2000-07-29 13:06:59 +0000528 sqliteExprSpan(A,&X->span,&E);
drhfef52082000-06-06 01:50:43 +0000529}
drhe1b6a5b2000-07-29 13:06:59 +0000530expr(A) ::= expr(X) IN LP select(Y) RP(E). {
drhfef52082000-06-06 01:50:43 +0000531 A = sqliteExpr(TK_IN, X, 0, 0);
drhdaffd0e2001-04-11 14:28:42 +0000532 if( A ) A->pSelect = Y;
drhe1b6a5b2000-07-29 13:06:59 +0000533 sqliteExprSpan(A,&X->span,&E);
drhfef52082000-06-06 01:50:43 +0000534}
drhe1b6a5b2000-07-29 13:06:59 +0000535expr(A) ::= expr(X) NOT IN LP exprlist(Y) RP(E). {
drh4794b982000-06-06 13:54:14 +0000536 A = sqliteExpr(TK_IN, X, 0, 0);
drhdaffd0e2001-04-11 14:28:42 +0000537 if( A ) A->pList = Y;
drh4794b982000-06-06 13:54:14 +0000538 A = sqliteExpr(TK_NOT, A, 0, 0);
drhe1b6a5b2000-07-29 13:06:59 +0000539 sqliteExprSpan(A,&X->span,&E);
drh4794b982000-06-06 13:54:14 +0000540}
drhe1b6a5b2000-07-29 13:06:59 +0000541expr(A) ::= expr(X) NOT IN LP select(Y) RP(E). {
drh4794b982000-06-06 13:54:14 +0000542 A = sqliteExpr(TK_IN, X, 0, 0);
drhdaffd0e2001-04-11 14:28:42 +0000543 if( A ) A->pSelect = Y;
drh4794b982000-06-06 13:54:14 +0000544 A = sqliteExpr(TK_NOT, A, 0, 0);
drhe1b6a5b2000-07-29 13:06:59 +0000545 sqliteExprSpan(A,&X->span,&E);
drh4794b982000-06-06 13:54:14 +0000546}
drhfef52082000-06-06 01:50:43 +0000547
drh17a7f8d2002-03-24 13:13:27 +0000548/* CASE expressions */
549expr(A) ::= CASE(C) case_operand(X) case_exprlist(Y) case_else(Z) END(E). {
550 A = sqliteExpr(TK_CASE, X, Z, 0);
551 if( A ) A->pList = Y;
552 sqliteExprSpan(A, &C, &E);
553}
554%type case_exprlist {ExprList*}
555%destructor case_exprlist {sqliteExprListDelete($$);}
556case_exprlist(A) ::= case_exprlist(X) WHEN expr(Y) THEN expr(Z). {
557 A = sqliteExprListAppend(X, Y, 0);
558 A = sqliteExprListAppend(A, Z, 0);
559}
560case_exprlist(A) ::= WHEN expr(Y) THEN expr(Z). {
561 A = sqliteExprListAppend(0, Y, 0);
562 A = sqliteExprListAppend(A, Z, 0);
563}
564%type case_else {Expr*}
565case_else(A) ::= ELSE expr(X). {A = X;}
566case_else(A) ::= . {A = 0;}
567%type case_operand {Expr*}
568case_operand(A) ::= expr(X). {A = X;}
569case_operand(A) ::= . {A = 0;}
drh348784e2000-05-29 20:41:49 +0000570
571%type exprlist {ExprList*}
572%destructor exprlist {sqliteExprListDelete($$);}
573%type expritem {Expr*}
574%destructor expritem {sqliteExprDelete($$);}
575
drh348784e2000-05-29 20:41:49 +0000576exprlist(A) ::= exprlist(X) COMMA expritem(Y).
577 {A = sqliteExprListAppend(X,Y,0);}
578exprlist(A) ::= expritem(X). {A = sqliteExprListAppend(0,X,0);}
579expritem(A) ::= expr(X). {A = X;}
580expritem(A) ::= . {A = 0;}
drhcce7d172000-05-31 15:34:51 +0000581
drh382c0242001-10-06 16:33:02 +0000582///////////////////////////// The CREATE INDEX command ///////////////////////
583//
drh9cfcf5d2002-01-29 18:41:24 +0000584cmd ::= CREATE(S) uniqueflag(U) INDEX ids(X)
585 ON ids(Y) LP idxlist(Z) RP(E) onconf(R). {
586 if( U!=OE_None ) U = R;
587 if( U==OE_Default) U = OE_Abort;
588 sqliteCreateIndex(pParse, &X, &Y, Z, U, &S, &E);
589}
drh717e6402001-09-27 03:22:32 +0000590
591%type uniqueflag {int}
drh9cfcf5d2002-01-29 18:41:24 +0000592uniqueflag(A) ::= UNIQUE. { A = OE_Abort; }
593uniqueflag(A) ::= . { A = OE_None; }
drh348784e2000-05-29 20:41:49 +0000594
595%type idxlist {IdList*}
596%destructor idxlist {sqliteIdListDelete($$);}
597%type idxitem {Token}
598
599idxlist(A) ::= idxlist(X) COMMA idxitem(Y).
600 {A = sqliteIdListAppend(X,&Y);}
601idxlist(A) ::= idxitem(Y).
602 {A = sqliteIdListAppend(0,&Y);}
drhc4a3c772001-04-04 11:48:57 +0000603idxitem(A) ::= ids(X). {A = X;}
drh348784e2000-05-29 20:41:49 +0000604
drh8aff1012001-12-22 14:49:24 +0000605///////////////////////////// The DROP INDEX command /////////////////////////
drh382c0242001-10-06 16:33:02 +0000606//
607
drhc4a3c772001-04-04 11:48:57 +0000608cmd ::= DROP INDEX ids(X). {sqliteDropIndex(pParse, &X);}
drh982cef72000-05-30 16:27:03 +0000609
drh382c0242001-10-06 16:33:02 +0000610
drh8aff1012001-12-22 14:49:24 +0000611///////////////////////////// The COPY command ///////////////////////////////
drh382c0242001-10-06 16:33:02 +0000612//
drh1c928532002-01-31 15:54:21 +0000613cmd ::= COPY orconf(R) ids(X) FROM ids(Y) USING DELIMITERS STRING(Z).
drhb419a922002-01-30 16:17:23 +0000614 {sqliteCopy(pParse,&X,&Y,&Z,R);}
drh1c928532002-01-31 15:54:21 +0000615cmd ::= COPY orconf(R) ids(X) FROM ids(Y).
drhb419a922002-01-30 16:17:23 +0000616 {sqliteCopy(pParse,&X,&Y,0,R);}
drhdce2cbe2000-05-31 02:27:49 +0000617
drh382c0242001-10-06 16:33:02 +0000618///////////////////////////// The VACUUM command /////////////////////////////
619//
drhdce2cbe2000-05-31 02:27:49 +0000620cmd ::= VACUUM. {sqliteVacuum(pParse,0);}
drhc4a3c772001-04-04 11:48:57 +0000621cmd ::= VACUUM ids(X). {sqliteVacuum(pParse,&X);}
drhf57b14a2001-09-14 18:54:08 +0000622
drh382c0242001-10-06 16:33:02 +0000623///////////////////////////// The PRAGMA command /////////////////////////////
624//
drhf57b14a2001-09-14 18:54:08 +0000625cmd ::= PRAGMA ids(X) EQ ids(Y). {sqlitePragma(pParse,&X,&Y,0);}
626cmd ::= PRAGMA ids(X) EQ ON(Y). {sqlitePragma(pParse,&X,&Y,0);}
627cmd ::= PRAGMA ids(X) EQ plus_num(Y). {sqlitePragma(pParse,&X,&Y,0);}
628cmd ::= PRAGMA ids(X) EQ minus_num(Y). {sqlitePragma(pParse,&X,&Y,1);}
drh382c0242001-10-06 16:33:02 +0000629cmd ::= PRAGMA ids(X) LP ids(Y) RP. {sqlitePragma(pParse,&X,&Y,0);}
drh603240c2002-03-05 01:11:12 +0000630cmd ::= PRAGMA ids(X). {sqlitePragma(pParse,&X,&X,0);}
drhf57b14a2001-09-14 18:54:08 +0000631plus_num(A) ::= plus_opt number(X). {A = X;}
632minus_num(A) ::= MINUS number(X). {A = X;}
633number(A) ::= INTEGER(X). {A = X;}
634number(A) ::= FLOAT(X). {A = X;}
635plus_opt ::= PLUS.
636plus_opt ::= .
danielk1977c3f9bad2002-05-15 08:30:12 +0000637
638//////////////////////////// The CREATE TRIGGER command /////////////////////
639cmd ::= CREATE(A) TRIGGER ids(B) trigger_time(C) trigger_event(D) ON ids(E)
640 foreach_clause(F) when_clause(G)
641 BEGIN trigger_cmd_list(S) END(Z). {
642 sqliteCreateTrigger(pParse, &B, C, D.a, D.b, &E, F, G, S,
643 A.z, (int)(Z.z - A.z) + Z.n );
644}
645
646%type trigger_time {int}
647trigger_time(A) ::= BEFORE. { A = TK_BEFORE; }
648trigger_time(A) ::= AFTER. { A = TK_AFTER; }
649trigger_time(A) ::= INSTEAD OF. { A = TK_INSTEAD;}
650trigger_time(A) ::= . { A = TK_BEFORE; }
651
652%type trigger_event {struct int_idlist}
653trigger_event(A) ::= DELETE. { A.a = TK_DELETE; A.b = 0; }
654trigger_event(A) ::= INSERT. { A.a = TK_INSERT; A.b = 0; }
655trigger_event(A) ::= UPDATE. { A.a = TK_UPDATE; A.b = 0;}
656trigger_event(A) ::= UPDATE OF inscollist(X). {A.a = TK_UPDATE; A.b = X; }
657
658%type foreach_clause {int}
659foreach_clause(A) ::= . { A = TK_ROW; }
660foreach_clause(A) ::= FOR EACH ROW. { A = TK_ROW; }
661foreach_clause(A) ::= FOR EACH STATEMENT. { A = TK_STATEMENT; }
662
663%type when_clause {Expr *}
664when_clause(A) ::= . { A = 0; }
665when_clause(A) ::= WHEN expr(X). { A = X; }
666
667%type trigger_cmd_list {TriggerStep *}
668trigger_cmd_list(A) ::= trigger_cmd(X) SEMI trigger_cmd_list(Y). {
669 X->pNext = Y ; A = X; }
670trigger_cmd_list(A) ::= . { A = 0; }
671
672%type trigger_cmd {TriggerStep *}
673// UPDATE
674trigger_cmd(A) ::= UPDATE orconf(R) ids(X) SET setlist(Y) where_opt(Z).
675 { A = sqliteTriggerUpdateStep(&X, Y, Z, R); }
676
677// INSERT
678trigger_cmd(A) ::= INSERT orconf(R) INTO ids(X) inscollist_opt(F)
679 VALUES LP itemlist(Y) RP.
680{A = sqliteTriggerInsertStep(&X, F, Y, 0, R);}
681
682trigger_cmd(A) ::= INSERT orconf(R) INTO ids(X) inscollist_opt(F) select(S).
683 {A = sqliteTriggerInsertStep(&X, F, 0, S, R);}
684
685// DELETE
686trigger_cmd(A) ::= DELETE FROM ids(X) where_opt(Y).
687 {A = sqliteTriggerDeleteStep(&X, Y);}
688
689// SELECT
690trigger_cmd(A) ::= select(X). {A = sqliteTriggerSelectStep(X); }
691
692//////////////////////// DROP TRIGGER statement //////////////////////////////
693cmd ::= DROP TRIGGER ids(X). {
694 sqliteDropTrigger(pParse,&X,0);
695}