xref: /sqlite-3.40.0/src/parse.y (revision 5665b3ea)
1 /*
2 ** 2001 September 15
3 **
4 ** The author disclaims copyright to this source code.  In place of
5 ** a legal notice, here is a blessing:
6 **
7 **    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.
10 **
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 **
17 ** @(#) $Id: parse.y,v 1.231 2007/06/20 12:18:31 drh Exp $
18 */
19 
20 // All token codes are small integers with #defines that begin with "TK_"
21 %token_prefix TK_
22 
23 // The type of the data attached to each token is Token.  This is also the
24 // default type for non-terminals.
25 //
26 %token_type {Token}
27 %default_type {Token}
28 
29 // The generated parser function takes a 4th argument as follows:
30 %extra_argument {Parse *pParse}
31 
32 // This code runs whenever there is a syntax error
33 //
34 %syntax_error {
35   if( !pParse->parseError ){
36     if( TOKEN.z[0] ){
37       sqlite3ErrorMsg(pParse, "near \"%T\": syntax error", &TOKEN);
38     }else{
39       sqlite3ErrorMsg(pParse, "incomplete SQL statement");
40     }
41     pParse->parseError = 1;
42   }
43 }
44 %stack_overflow {
45   sqlite3ErrorMsg(pParse, "parser stack overflow");
46   pParse->parseError = 1;
47 }
48 
49 // The name of the generated procedure that implements the parser
50 // is as follows:
51 %name sqlite3Parser
52 
53 // The following text is included near the beginning of the C source
54 // code file that implements the parser.
55 //
56 %include {
57 #include "sqliteInt.h"
58 #include "parse.h"
59 
60 /*
61 ** An instance of this structure holds information about the
62 ** LIMIT clause of a SELECT statement.
63 */
64 struct LimitVal {
65   Expr *pLimit;    /* The LIMIT expression.  NULL if there is no limit */
66   Expr *pOffset;   /* The OFFSET expression.  NULL if there is none */
67 };
68 
69 /*
70 ** An instance of this structure is used to store the LIKE,
71 ** GLOB, NOT LIKE, and NOT GLOB operators.
72 */
73 struct LikeOp {
74   Token eOperator;  /* "like" or "glob" or "regexp" */
75   int not;         /* True if the NOT keyword is present */
76 };
77 
78 /*
79 ** An instance of the following structure describes the event of a
80 ** TRIGGER.  "a" is the event type, one of TK_UPDATE, TK_INSERT,
81 ** TK_DELETE, or TK_INSTEAD.  If the event is of the form
82 **
83 **      UPDATE ON (a,b,c)
84 **
85 ** Then the "b" IdList records the list "a,b,c".
86 */
87 struct TrigEvent { int a; IdList * b; };
88 
89 /*
90 ** An instance of this structure holds the ATTACH key and the key type.
91 */
92 struct AttachKey { int type;  Token key; };
93 
94 } // end %include
95 
96 // Input is a single SQL command
97 input ::= cmdlist.
98 cmdlist ::= cmdlist ecmd.
99 cmdlist ::= ecmd.
100 cmdx ::= cmd.           { sqlite3FinishCoding(pParse); }
101 ecmd ::= SEMI.
102 ecmd ::= explain cmdx SEMI.
103 explain ::= .           { sqlite3BeginParse(pParse, 0); }
104 %ifndef SQLITE_OMIT_EXPLAIN
105 explain ::= EXPLAIN.              { sqlite3BeginParse(pParse, 1); }
106 explain ::= EXPLAIN QUERY PLAN.   { sqlite3BeginParse(pParse, 2); }
107 %endif  SQLITE_OMIT_EXPLAIN
108 
109 ///////////////////// Begin and end transactions. ////////////////////////////
110 //
111 
112 cmd ::= BEGIN transtype(Y) trans_opt.  {sqlite3BeginTransaction(pParse, Y);}
113 trans_opt ::= .
114 trans_opt ::= TRANSACTION.
115 trans_opt ::= TRANSACTION nm.
116 %type transtype {int}
117 transtype(A) ::= .             {A = TK_DEFERRED;}
118 transtype(A) ::= DEFERRED(X).  {A = @X;}
119 transtype(A) ::= IMMEDIATE(X). {A = @X;}
120 transtype(A) ::= EXCLUSIVE(X). {A = @X;}
121 cmd ::= COMMIT trans_opt.      {sqlite3CommitTransaction(pParse);}
122 cmd ::= END trans_opt.         {sqlite3CommitTransaction(pParse);}
123 cmd ::= ROLLBACK trans_opt.    {sqlite3RollbackTransaction(pParse);}
124 
125 ///////////////////// The CREATE TABLE statement ////////////////////////////
126 //
127 cmd ::= create_table create_table_args.
128 create_table ::= CREATE temp(T) TABLE ifnotexists(E) nm(Y) dbnm(Z). {
129    sqlite3StartTable(pParse,&Y,&Z,T,0,0,E);
130 }
131 %type ifnotexists {int}
132 ifnotexists(A) ::= .              {A = 0;}
133 ifnotexists(A) ::= IF NOT EXISTS. {A = 1;}
134 %type temp {int}
135 %ifndef SQLITE_OMIT_TEMPDB
136 temp(A) ::= TEMP.  {A = 1;}
137 %endif  SQLITE_OMIT_TEMPDB
138 temp(A) ::= .      {A = 0;}
139 create_table_args ::= LP columnlist conslist_opt(X) RP(Y). {
140   sqlite3EndTable(pParse,&X,&Y,0);
141 }
142 create_table_args ::= AS select(S). {
143   sqlite3EndTable(pParse,0,0,S);
144   sqlite3SelectDelete(S);
145 }
146 columnlist ::= columnlist COMMA column.
147 columnlist ::= column.
148 
149 // A "column" is a complete description of a single column in a
150 // CREATE TABLE statement.  This includes the column name, its
151 // datatype, and other keywords such as PRIMARY KEY, UNIQUE, REFERENCES,
152 // NOT NULL and so forth.
153 //
154 column(A) ::= columnid(X) type carglist. {
155   A.z = X.z;
156   A.n = (pParse->sLastToken.z-X.z) + pParse->sLastToken.n;
157 }
158 columnid(A) ::= nm(X). {
159   sqlite3AddColumn(pParse,&X);
160   A = X;
161 }
162 
163 
164 // An IDENTIFIER can be a generic identifier, or one of several
165 // keywords.  Any non-standard keyword can also be an identifier.
166 //
167 %type id {Token}
168 id(A) ::= ID(X).         {A = X;}
169 
170 // The following directive causes tokens ABORT, AFTER, ASC, etc. to
171 // fallback to ID if they will not parse as their original value.
172 // This obviates the need for the "id" nonterminal.
173 //
174 %fallback ID
175   ABORT AFTER ANALYZE ASC ATTACH BEFORE BEGIN CASCADE CAST CONFLICT
176   DATABASE DEFERRED DESC DETACH EACH END EXCLUSIVE EXPLAIN FAIL FOR
177   IGNORE IMMEDIATE INITIALLY INSTEAD LIKE_KW MATCH PLAN
178   QUERY KEY OF OFFSET PRAGMA RAISE REPLACE RESTRICT ROW
179   TEMP TRIGGER VACUUM VIEW VIRTUAL
180 %ifdef SQLITE_OMIT_COMPOUND_SELECT
181   EXCEPT INTERSECT UNION
182 %endif SQLITE_OMIT_COMPOUND_SELECT
183   REINDEX RENAME CTIME_KW IF
184   .
185 %wildcard ANY.
186 
187 // Define operator precedence early so that this is the first occurance
188 // of the operator tokens in the grammer.  Keeping the operators together
189 // causes them to be assigned integer values that are close together,
190 // which keeps parser tables smaller.
191 //
192 // The token values assigned to these symbols is determined by the order
193 // in which lemon first sees them.  It must be the case that ISNULL/NOTNULL,
194 // NE/EQ, GT/LE, and GE/LT are separated by only a single value.  See
195 // the sqlite3ExprIfFalse() routine for additional information on this
196 // constraint.
197 //
198 %left OR.
199 %left AND.
200 %right NOT.
201 %left IS MATCH LIKE_KW BETWEEN IN ISNULL NOTNULL NE EQ.
202 %left GT LE LT GE.
203 %right ESCAPE.
204 %left BITAND BITOR LSHIFT RSHIFT.
205 %left PLUS MINUS.
206 %left STAR SLASH REM.
207 %left CONCAT.
208 %left COLLATE.
209 %right UMINUS UPLUS BITNOT.
210 
211 // And "ids" is an identifer-or-string.
212 //
213 %type ids {Token}
214 ids(A) ::= ID|STRING(X).   {A = X;}
215 
216 // The name of a column or table can be any of the following:
217 //
218 %type nm {Token}
219 nm(A) ::= ID(X).         {A = X;}
220 nm(A) ::= STRING(X).     {A = X;}
221 nm(A) ::= JOIN_KW(X).    {A = X;}
222 
223 // A typetoken is really one or more tokens that form a type name such
224 // as can be found after the column name in a CREATE TABLE statement.
225 // Multiple tokens are concatenated to form the value of the typetoken.
226 //
227 %type typetoken {Token}
228 type ::= .
229 type ::= typetoken(X).                   {sqlite3AddColumnType(pParse,&X);}
230 typetoken(A) ::= typename(X).   {A = X;}
231 typetoken(A) ::= typename(X) LP signed RP(Y). {
232   A.z = X.z;
233   A.n = &Y.z[Y.n] - X.z;
234 }
235 typetoken(A) ::= typename(X) LP signed COMMA signed RP(Y). {
236   A.z = X.z;
237   A.n = &Y.z[Y.n] - X.z;
238 }
239 %type typename {Token}
240 typename(A) ::= ids(X).             {A = X;}
241 typename(A) ::= typename(X) ids(Y). {A.z=X.z; A.n=Y.n+(Y.z-X.z);}
242 signed ::= plus_num.
243 signed ::= minus_num.
244 
245 // "carglist" is a list of additional constraints that come after the
246 // column name and column type in a CREATE TABLE statement.
247 //
248 carglist ::= carglist carg.
249 carglist ::= .
250 carg ::= CONSTRAINT nm ccons.
251 carg ::= ccons.
252 ccons ::= DEFAULT term(X).            {sqlite3AddDefaultValue(pParse,X);}
253 ccons ::= DEFAULT LP expr(X) RP.      {sqlite3AddDefaultValue(pParse,X);}
254 ccons ::= DEFAULT PLUS term(X).       {sqlite3AddDefaultValue(pParse,X);}
255 ccons ::= DEFAULT MINUS term(X).      {
256   Expr *p = sqlite3Expr(TK_UMINUS, X, 0, 0);
257   sqlite3AddDefaultValue(pParse,p);
258 }
259 ccons ::= DEFAULT id(X).              {
260   Expr *p = sqlite3Expr(TK_STRING, 0, 0, &X);
261   sqlite3AddDefaultValue(pParse,p);
262 }
263 
264 // In addition to the type name, we also care about the primary key and
265 // UNIQUE constraints.
266 //
267 ccons ::= NULL onconf.
268 ccons ::= NOT NULL onconf(R).               {sqlite3AddNotNull(pParse, R);}
269 ccons ::= PRIMARY KEY sortorder(Z) onconf(R) autoinc(I).
270                                      {sqlite3AddPrimaryKey(pParse,0,R,I,Z);}
271 ccons ::= UNIQUE onconf(R).    {sqlite3CreateIndex(pParse,0,0,0,0,R,0,0,0,0);}
272 ccons ::= CHECK LP expr(X) RP.       {sqlite3AddCheckConstraint(pParse,X);}
273 ccons ::= REFERENCES nm(T) idxlist_opt(TA) refargs(R).
274                                 {sqlite3CreateForeignKey(pParse,0,&T,TA,R);}
275 ccons ::= defer_subclause(D).   {sqlite3DeferForeignKey(pParse,D);}
276 ccons ::= COLLATE id(C).  {sqlite3AddCollateType(pParse, (char*)C.z, C.n);}
277 
278 // The optional AUTOINCREMENT keyword
279 %type autoinc {int}
280 autoinc(X) ::= .          {X = 0;}
281 autoinc(X) ::= AUTOINCR.  {X = 1;}
282 
283 // The next group of rules parses the arguments to a REFERENCES clause
284 // that determine if the referential integrity checking is deferred or
285 // or immediate and which determine what action to take if a ref-integ
286 // check fails.
287 //
288 %type refargs {int}
289 refargs(A) ::= .                     { A = OE_Restrict * 0x010101; }
290 refargs(A) ::= refargs(X) refarg(Y). { A = (X & Y.mask) | Y.value; }
291 %type refarg {struct {int value; int mask;}}
292 refarg(A) ::= MATCH nm.              { A.value = 0;     A.mask = 0x000000; }
293 refarg(A) ::= ON DELETE refact(X).   { A.value = X;     A.mask = 0x0000ff; }
294 refarg(A) ::= ON UPDATE refact(X).   { A.value = X<<8;  A.mask = 0x00ff00; }
295 refarg(A) ::= ON INSERT refact(X).   { A.value = X<<16; A.mask = 0xff0000; }
296 %type refact {int}
297 refact(A) ::= SET NULL.              { A = OE_SetNull; }
298 refact(A) ::= SET DEFAULT.           { A = OE_SetDflt; }
299 refact(A) ::= CASCADE.               { A = OE_Cascade; }
300 refact(A) ::= RESTRICT.              { A = OE_Restrict; }
301 %type defer_subclause {int}
302 defer_subclause(A) ::= NOT DEFERRABLE init_deferred_pred_opt(X).  {A = X;}
303 defer_subclause(A) ::= DEFERRABLE init_deferred_pred_opt(X).      {A = X;}
304 %type init_deferred_pred_opt {int}
305 init_deferred_pred_opt(A) ::= .                       {A = 0;}
306 init_deferred_pred_opt(A) ::= INITIALLY DEFERRED.     {A = 1;}
307 init_deferred_pred_opt(A) ::= INITIALLY IMMEDIATE.    {A = 0;}
308 
309 // For the time being, the only constraint we care about is the primary
310 // key and UNIQUE.  Both create indices.
311 //
312 conslist_opt(A) ::= .                   {A.n = 0; A.z = 0;}
313 conslist_opt(A) ::= COMMA(X) conslist.  {A = X;}
314 conslist ::= conslist COMMA tcons.
315 conslist ::= conslist tcons.
316 conslist ::= tcons.
317 tcons ::= CONSTRAINT nm.
318 tcons ::= PRIMARY KEY LP idxlist(X) autoinc(I) RP onconf(R).
319                                          {sqlite3AddPrimaryKey(pParse,X,R,I,0);}
320 tcons ::= UNIQUE LP idxlist(X) RP onconf(R).
321                                  {sqlite3CreateIndex(pParse,0,0,0,X,R,0,0,0,0);}
322 tcons ::= CHECK LP expr(E) RP onconf. {sqlite3AddCheckConstraint(pParse,E);}
323 tcons ::= FOREIGN KEY LP idxlist(FA) RP
324           REFERENCES nm(T) idxlist_opt(TA) refargs(R) defer_subclause_opt(D). {
325     sqlite3CreateForeignKey(pParse, FA, &T, TA, R);
326     sqlite3DeferForeignKey(pParse, D);
327 }
328 %type defer_subclause_opt {int}
329 defer_subclause_opt(A) ::= .                    {A = 0;}
330 defer_subclause_opt(A) ::= defer_subclause(X).  {A = X;}
331 
332 // The following is a non-standard extension that allows us to declare the
333 // default behavior when there is a constraint conflict.
334 //
335 %type onconf {int}
336 %type orconf {int}
337 %type resolvetype {int}
338 onconf(A) ::= .                              {A = OE_Default;}
339 onconf(A) ::= ON CONFLICT resolvetype(X).    {A = X;}
340 orconf(A) ::= .                              {A = OE_Default;}
341 orconf(A) ::= OR resolvetype(X).             {A = X;}
342 resolvetype(A) ::= raisetype(X).             {A = X;}
343 resolvetype(A) ::= IGNORE.                   {A = OE_Ignore;}
344 resolvetype(A) ::= REPLACE.                  {A = OE_Replace;}
345 
346 ////////////////////////// The DROP TABLE /////////////////////////////////////
347 //
348 cmd ::= DROP TABLE ifexists(E) fullname(X). {
349   sqlite3DropTable(pParse, X, 0, E);
350 }
351 %type ifexists {int}
352 ifexists(A) ::= IF EXISTS.   {A = 1;}
353 ifexists(A) ::= .            {A = 0;}
354 
355 ///////////////////// The CREATE VIEW statement /////////////////////////////
356 //
357 %ifndef SQLITE_OMIT_VIEW
358 cmd ::= CREATE(X) temp(T) VIEW ifnotexists(E) nm(Y) dbnm(Z) AS select(S). {
359   sqlite3CreateView(pParse, &X, &Y, &Z, S, T, E);
360 }
361 cmd ::= DROP VIEW ifexists(E) fullname(X). {
362   sqlite3DropTable(pParse, X, 1, E);
363 }
364 %endif  SQLITE_OMIT_VIEW
365 
366 //////////////////////// The SELECT statement /////////////////////////////////
367 //
368 cmd ::= select(X).  {
369   sqlite3Select(pParse, X, SRT_Callback, 0, 0, 0, 0, 0);
370   sqlite3SelectDelete(X);
371 }
372 
373 %type select {Select*}
374 %destructor select {sqlite3SelectDelete($$);}
375 %type oneselect {Select*}
376 %destructor oneselect {sqlite3SelectDelete($$);}
377 
378 select(A) ::= oneselect(X).                      {A = X;}
379 %ifndef SQLITE_OMIT_COMPOUND_SELECT
380 select(A) ::= select(X) multiselect_op(Y) oneselect(Z).  {
381   if( Z ){
382     Z->op = Y;
383     Z->pPrior = X;
384   }else{
385     sqlite3SelectDelete(X);
386   }
387   A = Z;
388 }
389 %type multiselect_op {int}
390 multiselect_op(A) ::= UNION(OP).             {A = @OP;}
391 multiselect_op(A) ::= UNION ALL.             {A = TK_ALL;}
392 multiselect_op(A) ::= EXCEPT|INTERSECT(OP).  {A = @OP;}
393 %endif SQLITE_OMIT_COMPOUND_SELECT
394 oneselect(A) ::= SELECT distinct(D) selcollist(W) from(X) where_opt(Y)
395                  groupby_opt(P) having_opt(Q) orderby_opt(Z) limit_opt(L). {
396   A = sqlite3SelectNew(W,X,Y,P,Q,Z,D,L.pLimit,L.pOffset);
397 }
398 
399 // The "distinct" nonterminal is true (1) if the DISTINCT keyword is
400 // present and false (0) if it is not.
401 //
402 %type distinct {int}
403 distinct(A) ::= DISTINCT.   {A = 1;}
404 distinct(A) ::= ALL.        {A = 0;}
405 distinct(A) ::= .           {A = 0;}
406 
407 // selcollist is a list of expressions that are to become the return
408 // values of the SELECT statement.  The "*" in statements like
409 // "SELECT * FROM ..." is encoded as a special expression with an
410 // opcode of TK_ALL.
411 //
412 %type selcollist {ExprList*}
413 %destructor selcollist {sqlite3ExprListDelete($$);}
414 %type sclp {ExprList*}
415 %destructor sclp {sqlite3ExprListDelete($$);}
416 sclp(A) ::= selcollist(X) COMMA.             {A = X;}
417 sclp(A) ::= .                                {A = 0;}
418 selcollist(A) ::= sclp(P) expr(X) as(Y).     {
419    A = sqlite3ExprListAppend(P,X,Y.n?&Y:0);
420 }
421 selcollist(A) ::= sclp(P) STAR. {
422   A = sqlite3ExprListAppend(P, sqlite3Expr(TK_ALL, 0, 0, 0), 0);
423 }
424 selcollist(A) ::= sclp(P) nm(X) DOT STAR. {
425   Expr *pRight = sqlite3Expr(TK_ALL, 0, 0, 0);
426   Expr *pLeft = sqlite3Expr(TK_ID, 0, 0, &X);
427   A = sqlite3ExprListAppend(P, sqlite3Expr(TK_DOT, pLeft, pRight, 0), 0);
428 }
429 
430 // An option "AS <id>" phrase that can follow one of the expressions that
431 // define the result set, or one of the tables in the FROM clause.
432 //
433 %type as {Token}
434 as(X) ::= AS nm(Y).    {X = Y;}
435 as(X) ::= ids(Y).      {X = Y;}
436 as(X) ::= .            {X.n = 0;}
437 
438 
439 %type seltablist {SrcList*}
440 %destructor seltablist {sqlite3SrcListDelete($$);}
441 %type stl_prefix {SrcList*}
442 %destructor stl_prefix {sqlite3SrcListDelete($$);}
443 %type from {SrcList*}
444 %destructor from {sqlite3SrcListDelete($$);}
445 
446 // A complete FROM clause.
447 //
448 from(A) ::= .                                 {A = sqliteMalloc(sizeof(*A));}
449 from(A) ::= FROM seltablist(X).               {
450   A = X;
451   sqlite3SrcListShiftJoinType(A);
452 }
453 
454 // "seltablist" is a "Select Table List" - the content of the FROM clause
455 // in a SELECT statement.  "stl_prefix" is a prefix of this list.
456 //
457 stl_prefix(A) ::= seltablist(X) joinop(Y).    {
458    A = X;
459    if( A && A->nSrc>0 ) A->a[A->nSrc-1].jointype = Y;
460 }
461 stl_prefix(A) ::= .                           {A = 0;}
462 seltablist(A) ::= stl_prefix(X) nm(Y) dbnm(D) as(Z) on_opt(N) using_opt(U). {
463   A = sqlite3SrcListAppendFromTerm(X,&Y,&D,&Z,0,N,U);
464 }
465 %ifndef SQLITE_OMIT_SUBQUERY
466   seltablist(A) ::= stl_prefix(X) LP seltablist_paren(S) RP
467                     as(Z) on_opt(N) using_opt(U). {
468     A = sqlite3SrcListAppendFromTerm(X,0,0,&Z,S,N,U);
469   }
470 
471   // A seltablist_paren nonterminal represents anything in a FROM that
472   // is contained inside parentheses.  This can be either a subquery or
473   // a grouping of table and subqueries.
474   //
475   %type seltablist_paren {Select*}
476   %destructor seltablist_paren {sqlite3SelectDelete($$);}
477   seltablist_paren(A) ::= select(S).      {A = S;}
478   seltablist_paren(A) ::= seltablist(F).  {
479      sqlite3SrcListShiftJoinType(F);
480      A = sqlite3SelectNew(0,F,0,0,0,0,0,0,0);
481   }
482 %endif  SQLITE_OMIT_SUBQUERY
483 
484 %type dbnm {Token}
485 dbnm(A) ::= .          {A.z=0; A.n=0;}
486 dbnm(A) ::= DOT nm(X). {A = X;}
487 
488 %type fullname {SrcList*}
489 %destructor fullname {sqlite3SrcListDelete($$);}
490 fullname(A) ::= nm(X) dbnm(Y).  {A = sqlite3SrcListAppend(0,&X,&Y);}
491 
492 %type joinop {int}
493 %type joinop2 {int}
494 joinop(X) ::= COMMA|JOIN.              { X = JT_INNER; }
495 joinop(X) ::= JOIN_KW(A) JOIN.         { X = sqlite3JoinType(pParse,&A,0,0); }
496 joinop(X) ::= JOIN_KW(A) nm(B) JOIN.   { X = sqlite3JoinType(pParse,&A,&B,0); }
497 joinop(X) ::= JOIN_KW(A) nm(B) nm(C) JOIN.
498                                        { X = sqlite3JoinType(pParse,&A,&B,&C); }
499 
500 %type on_opt {Expr*}
501 %destructor on_opt {sqlite3ExprDelete($$);}
502 on_opt(N) ::= ON expr(E).   {N = E;}
503 on_opt(N) ::= .             {N = 0;}
504 
505 %type using_opt {IdList*}
506 %destructor using_opt {sqlite3IdListDelete($$);}
507 using_opt(U) ::= USING LP inscollist(L) RP.  {U = L;}
508 using_opt(U) ::= .                        {U = 0;}
509 
510 
511 %type orderby_opt {ExprList*}
512 %destructor orderby_opt {sqlite3ExprListDelete($$);}
513 %type sortlist {ExprList*}
514 %destructor sortlist {sqlite3ExprListDelete($$);}
515 %type sortitem {Expr*}
516 %destructor sortitem {sqlite3ExprDelete($$);}
517 
518 orderby_opt(A) ::= .                          {A = 0;}
519 orderby_opt(A) ::= ORDER BY sortlist(X).      {A = X;}
520 sortlist(A) ::= sortlist(X) COMMA sortitem(Y) sortorder(Z). {
521   A = sqlite3ExprListAppend(X,Y,0);
522   if( A ) A->a[A->nExpr-1].sortOrder = Z;
523 }
524 sortlist(A) ::= sortitem(Y) sortorder(Z). {
525   A = sqlite3ExprListAppend(0,Y,0);
526   if( A && A->a ) A->a[0].sortOrder = Z;
527 }
528 sortitem(A) ::= expr(X).   {A = X;}
529 
530 %type sortorder {int}
531 
532 sortorder(A) ::= ASC.           {A = SQLITE_SO_ASC;}
533 sortorder(A) ::= DESC.          {A = SQLITE_SO_DESC;}
534 sortorder(A) ::= .              {A = SQLITE_SO_ASC;}
535 
536 %type groupby_opt {ExprList*}
537 %destructor groupby_opt {sqlite3ExprListDelete($$);}
538 groupby_opt(A) ::= .                      {A = 0;}
539 groupby_opt(A) ::= GROUP BY nexprlist(X). {A = X;}
540 
541 %type having_opt {Expr*}
542 %destructor having_opt {sqlite3ExprDelete($$);}
543 having_opt(A) ::= .                {A = 0;}
544 having_opt(A) ::= HAVING expr(X).  {A = X;}
545 
546 %type limit_opt {struct LimitVal}
547 
548 // The destructor for limit_opt will never fire in the current grammar.
549 // The limit_opt non-terminal only occurs at the end of a single production
550 // rule for SELECT statements.  As soon as the rule that create the
551 // limit_opt non-terminal reduces, the SELECT statement rule will also
552 // reduce.  So there is never a limit_opt non-terminal on the stack
553 // except as a transient.  So there is never anything to destroy.
554 //
555 //%destructor limit_opt {
556 //  sqlite3ExprDelete($$.pLimit);
557 //  sqlite3ExprDelete($$.pOffset);
558 //}
559 limit_opt(A) ::= .                     {A.pLimit = 0; A.pOffset = 0;}
560 limit_opt(A) ::= LIMIT expr(X).        {A.pLimit = X; A.pOffset = 0;}
561 limit_opt(A) ::= LIMIT expr(X) OFFSET expr(Y).
562                                        {A.pLimit = X; A.pOffset = Y;}
563 limit_opt(A) ::= LIMIT expr(X) COMMA expr(Y).
564                                        {A.pOffset = X; A.pLimit = Y;}
565 
566 /////////////////////////// The DELETE statement /////////////////////////////
567 //
568 cmd ::= DELETE FROM fullname(X) where_opt(Y). {sqlite3DeleteFrom(pParse,X,Y);}
569 
570 %type where_opt {Expr*}
571 %destructor where_opt {sqlite3ExprDelete($$);}
572 
573 where_opt(A) ::= .                    {A = 0;}
574 where_opt(A) ::= WHERE expr(X).       {A = X;}
575 
576 ////////////////////////// The UPDATE command ////////////////////////////////
577 //
578 cmd ::= UPDATE orconf(R) fullname(X) SET setlist(Y) where_opt(Z).  {
579   sqlite3ExprListCheckLength(pParse,Y,SQLITE_MAX_COLUMN,"set list");
580   sqlite3Update(pParse,X,Y,Z,R);
581 }
582 
583 %type setlist {ExprList*}
584 %destructor setlist {sqlite3ExprListDelete($$);}
585 
586 setlist(A) ::= setlist(Z) COMMA nm(X) EQ expr(Y).
587     {A = sqlite3ExprListAppend(Z,Y,&X);}
588 setlist(A) ::= nm(X) EQ expr(Y).   {A = sqlite3ExprListAppend(0,Y,&X);}
589 
590 ////////////////////////// The INSERT command /////////////////////////////////
591 //
592 cmd ::= insert_cmd(R) INTO fullname(X) inscollist_opt(F)
593         VALUES LP itemlist(Y) RP.
594             {sqlite3Insert(pParse, X, Y, 0, F, R);}
595 cmd ::= insert_cmd(R) INTO fullname(X) inscollist_opt(F) select(S).
596             {sqlite3Insert(pParse, X, 0, S, F, R);}
597 cmd ::= insert_cmd(R) INTO fullname(X) inscollist_opt(F) DEFAULT VALUES.
598             {sqlite3Insert(pParse, X, 0, 0, F, R);}
599 
600 %type insert_cmd {int}
601 insert_cmd(A) ::= INSERT orconf(R).   {A = R;}
602 insert_cmd(A) ::= REPLACE.            {A = OE_Replace;}
603 
604 
605 %type itemlist {ExprList*}
606 %destructor itemlist {sqlite3ExprListDelete($$);}
607 
608 itemlist(A) ::= itemlist(X) COMMA expr(Y).  {A = sqlite3ExprListAppend(X,Y,0);}
609 itemlist(A) ::= expr(X).                    {A = sqlite3ExprListAppend(0,X,0);}
610 
611 %type inscollist_opt {IdList*}
612 %destructor inscollist_opt {sqlite3IdListDelete($$);}
613 %type inscollist {IdList*}
614 %destructor inscollist {sqlite3IdListDelete($$);}
615 
616 inscollist_opt(A) ::= .                       {A = 0;}
617 inscollist_opt(A) ::= LP inscollist(X) RP.    {A = X;}
618 inscollist(A) ::= inscollist(X) COMMA nm(Y).  {A = sqlite3IdListAppend(X,&Y);}
619 inscollist(A) ::= nm(Y).                      {A = sqlite3IdListAppend(0,&Y);}
620 
621 /////////////////////////// Expression Processing /////////////////////////////
622 //
623 
624 %type expr {Expr*}
625 %destructor expr {sqlite3ExprDelete($$);}
626 %type term {Expr*}
627 %destructor term {sqlite3ExprDelete($$);}
628 
629 expr(A) ::= term(X).             {A = X;}
630 expr(A) ::= LP(B) expr(X) RP(E). {A = X; sqlite3ExprSpan(A,&B,&E); }
631 term(A) ::= NULL(X).             {A = sqlite3Expr(@X, 0, 0, &X);}
632 expr(A) ::= ID(X).               {A = sqlite3Expr(TK_ID, 0, 0, &X);}
633 expr(A) ::= JOIN_KW(X).          {A = sqlite3Expr(TK_ID, 0, 0, &X);}
634 expr(A) ::= nm(X) DOT nm(Y). {
635   Expr *temp1 = sqlite3Expr(TK_ID, 0, 0, &X);
636   Expr *temp2 = sqlite3Expr(TK_ID, 0, 0, &Y);
637   A = sqlite3Expr(TK_DOT, temp1, temp2, 0);
638 }
639 expr(A) ::= nm(X) DOT nm(Y) DOT nm(Z). {
640   Expr *temp1 = sqlite3Expr(TK_ID, 0, 0, &X);
641   Expr *temp2 = sqlite3Expr(TK_ID, 0, 0, &Y);
642   Expr *temp3 = sqlite3Expr(TK_ID, 0, 0, &Z);
643   Expr *temp4 = sqlite3Expr(TK_DOT, temp2, temp3, 0);
644   A = sqlite3Expr(TK_DOT, temp1, temp4, 0);
645 }
646 term(A) ::= INTEGER|FLOAT|BLOB(X).      {A = sqlite3Expr(@X, 0, 0, &X);}
647 term(A) ::= STRING(X).       {A = sqlite3Expr(@X, 0, 0, &X);}
648 expr(A) ::= REGISTER(X).     {A = sqlite3RegisterExpr(pParse, &X);}
649 expr(A) ::= VARIABLE(X).     {
650   Token *pToken = &X;
651   Expr *pExpr = A = sqlite3Expr(TK_VARIABLE, 0, 0, pToken);
652   sqlite3ExprAssignVarNumber(pParse, pExpr);
653 }
654 expr(A) ::= expr(E) COLLATE id(C). {
655   A = sqlite3ExprSetColl(pParse, E, &C);
656 }
657 %ifndef SQLITE_OMIT_CAST
658 expr(A) ::= CAST(X) LP expr(E) AS typetoken(T) RP(Y). {
659   A = sqlite3Expr(TK_CAST, E, 0, &T);
660   sqlite3ExprSpan(A,&X,&Y);
661 }
662 %endif  SQLITE_OMIT_CAST
663 expr(A) ::= ID(X) LP distinct(D) exprlist(Y) RP(E). {
664   if( Y && Y->nExpr>SQLITE_MAX_FUNCTION_ARG ){
665     sqlite3ErrorMsg(pParse, "too many arguments on function %T", &X);
666   }
667   A = sqlite3ExprFunction(Y, &X);
668   sqlite3ExprSpan(A,&X,&E);
669   if( D && A ){
670     A->flags |= EP_Distinct;
671   }
672 }
673 expr(A) ::= ID(X) LP STAR RP(E). {
674   A = sqlite3ExprFunction(0, &X);
675   sqlite3ExprSpan(A,&X,&E);
676 }
677 term(A) ::= CTIME_KW(OP). {
678   /* The CURRENT_TIME, CURRENT_DATE, and CURRENT_TIMESTAMP values are
679   ** treated as functions that return constants */
680   A = sqlite3ExprFunction(0,&OP);
681   if( A ){
682     A->op = TK_CONST_FUNC;
683     A->span = OP;
684   }
685 }
686 expr(A) ::= expr(X) AND(OP) expr(Y).            {A = sqlite3Expr(@OP, X, Y, 0);}
687 expr(A) ::= expr(X) OR(OP) expr(Y).             {A = sqlite3Expr(@OP, X, Y, 0);}
688 expr(A) ::= expr(X) LT|GT|GE|LE(OP) expr(Y).    {A = sqlite3Expr(@OP, X, Y, 0);}
689 expr(A) ::= expr(X) EQ|NE(OP) expr(Y).          {A = sqlite3Expr(@OP, X, Y, 0);}
690 expr(A) ::= expr(X) BITAND|BITOR|LSHIFT|RSHIFT(OP) expr(Y).
691                                                 {A = sqlite3Expr(@OP, X, Y, 0);}
692 expr(A) ::= expr(X) PLUS|MINUS(OP) expr(Y).     {A = sqlite3Expr(@OP, X, Y, 0);}
693 expr(A) ::= expr(X) STAR|SLASH|REM(OP) expr(Y). {A = sqlite3Expr(@OP, X, Y, 0);}
694 expr(A) ::= expr(X) CONCAT(OP) expr(Y).         {A = sqlite3Expr(@OP, X, Y, 0);}
695 %type likeop {struct LikeOp}
696 likeop(A) ::= LIKE_KW(X).     {A.eOperator = X; A.not = 0;}
697 likeop(A) ::= NOT LIKE_KW(X). {A.eOperator = X; A.not = 1;}
698 likeop(A) ::= MATCH(X).       {A.eOperator = X; A.not = 0;}
699 likeop(A) ::= NOT MATCH(X).   {A.eOperator = X; A.not = 1;}
700 %type escape {Expr*}
701 %destructor escape {sqlite3ExprDelete($$);}
702 escape(X) ::= ESCAPE expr(A). [ESCAPE] {X = A;}
703 escape(X) ::= .               [ESCAPE] {X = 0;}
704 expr(A) ::= expr(X) likeop(OP) expr(Y) escape(E).  [LIKE_KW]  {
705   ExprList *pList;
706   pList = sqlite3ExprListAppend(0, Y, 0);
707   pList = sqlite3ExprListAppend(pList, X, 0);
708   if( E ){
709     pList = sqlite3ExprListAppend(pList, E, 0);
710   }
711   A = sqlite3ExprFunction(pList, &OP.eOperator);
712   if( OP.not ) A = sqlite3Expr(TK_NOT, A, 0, 0);
713   sqlite3ExprSpan(A, &X->span, &Y->span);
714   if( A ) A->flags |= EP_InfixFunc;
715 }
716 
717 expr(A) ::= expr(X) ISNULL|NOTNULL(E). {
718   A = sqlite3Expr(@E, X, 0, 0);
719   sqlite3ExprSpan(A,&X->span,&E);
720 }
721 expr(A) ::= expr(X) IS NULL(E). {
722   A = sqlite3Expr(TK_ISNULL, X, 0, 0);
723   sqlite3ExprSpan(A,&X->span,&E);
724 }
725 expr(A) ::= expr(X) NOT NULL(E). {
726   A = sqlite3Expr(TK_NOTNULL, X, 0, 0);
727   sqlite3ExprSpan(A,&X->span,&E);
728 }
729 expr(A) ::= expr(X) IS NOT NULL(E). {
730   A = sqlite3Expr(TK_NOTNULL, X, 0, 0);
731   sqlite3ExprSpan(A,&X->span,&E);
732 }
733 expr(A) ::= NOT|BITNOT(B) expr(X). {
734   A = sqlite3Expr(@B, X, 0, 0);
735   sqlite3ExprSpan(A,&B,&X->span);
736 }
737 expr(A) ::= MINUS(B) expr(X). [UMINUS] {
738   A = sqlite3Expr(TK_UMINUS, X, 0, 0);
739   sqlite3ExprSpan(A,&B,&X->span);
740 }
741 expr(A) ::= PLUS(B) expr(X). [UPLUS] {
742   A = sqlite3Expr(TK_UPLUS, X, 0, 0);
743   sqlite3ExprSpan(A,&B,&X->span);
744 }
745 %type between_op {int}
746 between_op(A) ::= BETWEEN.     {A = 0;}
747 between_op(A) ::= NOT BETWEEN. {A = 1;}
748 expr(A) ::= expr(W) between_op(N) expr(X) AND expr(Y). [BETWEEN] {
749   ExprList *pList = sqlite3ExprListAppend(0, X, 0);
750   pList = sqlite3ExprListAppend(pList, Y, 0);
751   A = sqlite3Expr(TK_BETWEEN, W, 0, 0);
752   if( A ){
753     A->pList = pList;
754   }else{
755     sqlite3ExprListDelete(pList);
756   }
757   if( N ) A = sqlite3Expr(TK_NOT, A, 0, 0);
758   sqlite3ExprSpan(A,&W->span,&Y->span);
759 }
760 %ifndef SQLITE_OMIT_SUBQUERY
761   %type in_op {int}
762   in_op(A) ::= IN.      {A = 0;}
763   in_op(A) ::= NOT IN.  {A = 1;}
764   expr(A) ::= expr(X) in_op(N) LP exprlist(Y) RP(E). [IN] {
765     A = sqlite3Expr(TK_IN, X, 0, 0);
766     if( A ){
767       A->pList = Y;
768       sqlite3ExprSetHeight(A);
769     }else{
770       sqlite3ExprListDelete(Y);
771     }
772     if( N ) A = sqlite3Expr(TK_NOT, A, 0, 0);
773     sqlite3ExprSpan(A,&X->span,&E);
774   }
775   expr(A) ::= LP(B) select(X) RP(E). {
776     A = sqlite3Expr(TK_SELECT, 0, 0, 0);
777     if( A ){
778       A->pSelect = X;
779       sqlite3ExprSetHeight(A);
780     }else{
781       sqlite3SelectDelete(X);
782     }
783     sqlite3ExprSpan(A,&B,&E);
784   }
785   expr(A) ::= expr(X) in_op(N) LP select(Y) RP(E).  [IN] {
786     A = sqlite3Expr(TK_IN, X, 0, 0);
787     if( A ){
788       A->pSelect = Y;
789       sqlite3ExprSetHeight(A);
790     }else{
791       sqlite3SelectDelete(Y);
792     }
793     if( N ) A = sqlite3Expr(TK_NOT, A, 0, 0);
794     sqlite3ExprSpan(A,&X->span,&E);
795   }
796   expr(A) ::= expr(X) in_op(N) nm(Y) dbnm(Z). [IN] {
797     SrcList *pSrc = sqlite3SrcListAppend(0,&Y,&Z);
798     A = sqlite3Expr(TK_IN, X, 0, 0);
799     if( A ){
800       A->pSelect = sqlite3SelectNew(0,pSrc,0,0,0,0,0,0,0);
801       sqlite3ExprSetHeight(A);
802     }else{
803       sqlite3SrcListDelete(pSrc);
804     }
805     if( N ) A = sqlite3Expr(TK_NOT, A, 0, 0);
806     sqlite3ExprSpan(A,&X->span,Z.z?&Z:&Y);
807   }
808   expr(A) ::= EXISTS(B) LP select(Y) RP(E). {
809     Expr *p = A = sqlite3Expr(TK_EXISTS, 0, 0, 0);
810     if( p ){
811       p->pSelect = Y;
812       sqlite3ExprSpan(p,&B,&E);
813       sqlite3ExprSetHeight(A);
814     }else{
815       sqlite3SelectDelete(Y);
816     }
817   }
818 %endif SQLITE_OMIT_SUBQUERY
819 
820 /* CASE expressions */
821 expr(A) ::= CASE(C) case_operand(X) case_exprlist(Y) case_else(Z) END(E). {
822   A = sqlite3Expr(TK_CASE, X, Z, 0);
823   if( A ){
824     A->pList = Y;
825     sqlite3ExprSetHeight(A);
826   }else{
827     sqlite3ExprListDelete(Y);
828   }
829   sqlite3ExprSpan(A, &C, &E);
830 }
831 %type case_exprlist {ExprList*}
832 %destructor case_exprlist {sqlite3ExprListDelete($$);}
833 case_exprlist(A) ::= case_exprlist(X) WHEN expr(Y) THEN expr(Z). {
834   A = sqlite3ExprListAppend(X, Y, 0);
835   A = sqlite3ExprListAppend(A, Z, 0);
836 }
837 case_exprlist(A) ::= WHEN expr(Y) THEN expr(Z). {
838   A = sqlite3ExprListAppend(0, Y, 0);
839   A = sqlite3ExprListAppend(A, Z, 0);
840 }
841 %type case_else {Expr*}
842 %destructor case_else {sqlite3ExprDelete($$);}
843 case_else(A) ::=  ELSE expr(X).         {A = X;}
844 case_else(A) ::=  .                     {A = 0;}
845 %type case_operand {Expr*}
846 %destructor case_operand {sqlite3ExprDelete($$);}
847 case_operand(A) ::= expr(X).            {A = X;}
848 case_operand(A) ::= .                   {A = 0;}
849 
850 %type exprlist {ExprList*}
851 %destructor exprlist {sqlite3ExprListDelete($$);}
852 %type nexprlist {ExprList*}
853 %destructor nexprlist {sqlite3ExprListDelete($$);}
854 
855 exprlist(A) ::= nexprlist(X).                {A = X;}
856 exprlist(A) ::= .                            {A = 0;}
857 nexprlist(A) ::= nexprlist(X) COMMA expr(Y). {A = sqlite3ExprListAppend(X,Y,0);}
858 nexprlist(A) ::= expr(Y).                    {A = sqlite3ExprListAppend(0,Y,0);}
859 
860 
861 ///////////////////////////// The CREATE INDEX command ///////////////////////
862 //
863 cmd ::= CREATE(S) uniqueflag(U) INDEX ifnotexists(NE) nm(X) dbnm(D)
864         ON nm(Y) LP idxlist(Z) RP(E). {
865   sqlite3CreateIndex(pParse, &X, &D, sqlite3SrcListAppend(0,&Y,0), Z, U,
866                       &S, &E, SQLITE_SO_ASC, NE);
867 }
868 
869 %type uniqueflag {int}
870 uniqueflag(A) ::= UNIQUE.  {A = OE_Abort;}
871 uniqueflag(A) ::= .        {A = OE_None;}
872 
873 %type idxlist {ExprList*}
874 %destructor idxlist {sqlite3ExprListDelete($$);}
875 %type idxlist_opt {ExprList*}
876 %destructor idxlist_opt {sqlite3ExprListDelete($$);}
877 %type idxitem {Token}
878 
879 idxlist_opt(A) ::= .                         {A = 0;}
880 idxlist_opt(A) ::= LP idxlist(X) RP.         {A = X;}
881 idxlist(A) ::= idxlist(X) COMMA idxitem(Y) collate(C) sortorder(Z).  {
882   Expr *p = 0;
883   if( C.n>0 ){
884     p = sqlite3Expr(TK_COLUMN, 0, 0, 0);
885     if( p ) p->pColl = sqlite3LocateCollSeq(pParse, (char*)C.z, C.n);
886   }
887   A = sqlite3ExprListAppend(X, p, &Y);
888   sqlite3ExprListCheckLength(pParse, A, SQLITE_MAX_COLUMN, "index");
889   if( A ) A->a[A->nExpr-1].sortOrder = Z;
890 }
891 idxlist(A) ::= idxitem(Y) collate(C) sortorder(Z). {
892   Expr *p = 0;
893   if( C.n>0 ){
894     p = sqlite3Expr(TK_COLUMN, 0, 0, 0);
895     if( p ) p->pColl = sqlite3LocateCollSeq(pParse, (char*)C.z, C.n);
896   }
897   A = sqlite3ExprListAppend(0, p, &Y);
898   sqlite3ExprListCheckLength(pParse, A, SQLITE_MAX_COLUMN, "index");
899   if( A ) A->a[A->nExpr-1].sortOrder = Z;
900 }
901 idxitem(A) ::= nm(X).              {A = X;}
902 
903 %type collate {Token}
904 collate(C) ::= .                {C.z = 0; C.n = 0;}
905 collate(C) ::= COLLATE id(X).   {C = X;}
906 
907 
908 ///////////////////////////// The DROP INDEX command /////////////////////////
909 //
910 cmd ::= DROP INDEX ifexists(E) fullname(X).   {sqlite3DropIndex(pParse, X, E);}
911 
912 ///////////////////////////// The VACUUM command /////////////////////////////
913 //
914 %ifndef SQLITE_OMIT_VACUUM
915 %ifndef SQLITE_OMIT_ATTACH
916 cmd ::= VACUUM.                {sqlite3Vacuum(pParse);}
917 cmd ::= VACUUM nm.             {sqlite3Vacuum(pParse);}
918 %endif  SQLITE_OMIT_ATTACH
919 %endif  SQLITE_OMIT_VACUUM
920 
921 ///////////////////////////// The PRAGMA command /////////////////////////////
922 //
923 %ifndef SQLITE_OMIT_PRAGMA
924 cmd ::= PRAGMA nm(X) dbnm(Z) EQ nmnum(Y).  {sqlite3Pragma(pParse,&X,&Z,&Y,0);}
925 cmd ::= PRAGMA nm(X) dbnm(Z) EQ ON(Y).  {sqlite3Pragma(pParse,&X,&Z,&Y,0);}
926 cmd ::= PRAGMA nm(X) dbnm(Z) EQ minus_num(Y). {
927   sqlite3Pragma(pParse,&X,&Z,&Y,1);
928 }
929 cmd ::= PRAGMA nm(X) dbnm(Z) LP nmnum(Y) RP. {sqlite3Pragma(pParse,&X,&Z,&Y,0);}
930 cmd ::= PRAGMA nm(X) dbnm(Z).             {sqlite3Pragma(pParse,&X,&Z,0,0);}
931 nmnum(A) ::= plus_num(X).             {A = X;}
932 nmnum(A) ::= nm(X).                   {A = X;}
933 %endif SQLITE_OMIT_PRAGMA
934 plus_num(A) ::= plus_opt number(X).   {A = X;}
935 minus_num(A) ::= MINUS number(X).     {A = X;}
936 number(A) ::= INTEGER|FLOAT(X).       {A = X;}
937 plus_opt ::= PLUS.
938 plus_opt ::= .
939 
940 //////////////////////////// The CREATE TRIGGER command /////////////////////
941 
942 %ifndef SQLITE_OMIT_TRIGGER
943 
944 cmd ::= CREATE trigger_decl(A) BEGIN trigger_cmd_list(S) END(Z). {
945   Token all;
946   all.z = A.z;
947   all.n = (Z.z - A.z) + Z.n;
948   sqlite3FinishTrigger(pParse, S, &all);
949 }
950 
951 trigger_decl(A) ::= temp(T) TRIGGER ifnotexists(NOERR) nm(B) dbnm(Z)
952                     trigger_time(C) trigger_event(D)
953                     ON fullname(E) foreach_clause when_clause(G). {
954   sqlite3BeginTrigger(pParse, &B, &Z, C, D.a, D.b, E, G, T, NOERR);
955   A = (Z.n==0?B:Z);
956 }
957 
958 %type trigger_time  {int}
959 trigger_time(A) ::= BEFORE.      { A = TK_BEFORE; }
960 trigger_time(A) ::= AFTER.       { A = TK_AFTER;  }
961 trigger_time(A) ::= INSTEAD OF.  { A = TK_INSTEAD;}
962 trigger_time(A) ::= .            { A = TK_BEFORE; }
963 
964 %type trigger_event {struct TrigEvent}
965 %destructor trigger_event {sqlite3IdListDelete($$.b);}
966 trigger_event(A) ::= DELETE|INSERT(OP).       {A.a = @OP; A.b = 0;}
967 trigger_event(A) ::= UPDATE(OP).              {A.a = @OP; A.b = 0;}
968 trigger_event(A) ::= UPDATE OF inscollist(X). {A.a = TK_UPDATE; A.b = X;}
969 
970 foreach_clause ::= .
971 foreach_clause ::= FOR EACH ROW.
972 
973 %type when_clause {Expr*}
974 %destructor when_clause {sqlite3ExprDelete($$);}
975 when_clause(A) ::= .             { A = 0; }
976 when_clause(A) ::= WHEN expr(X). { A = X; }
977 
978 %type trigger_cmd_list {TriggerStep*}
979 %destructor trigger_cmd_list {sqlite3DeleteTriggerStep($$);}
980 trigger_cmd_list(A) ::= trigger_cmd_list(Y) trigger_cmd(X) SEMI. {
981   if( Y ){
982     Y->pLast->pNext = X;
983   }else{
984     Y = X;
985   }
986   Y->pLast = X;
987   A = Y;
988 }
989 trigger_cmd_list(A) ::= . { A = 0; }
990 
991 %type trigger_cmd {TriggerStep*}
992 %destructor trigger_cmd {sqlite3DeleteTriggerStep($$);}
993 // UPDATE
994 trigger_cmd(A) ::= UPDATE orconf(R) nm(X) SET setlist(Y) where_opt(Z).
995                { A = sqlite3TriggerUpdateStep(&X, Y, Z, R); }
996 
997 // INSERT
998 trigger_cmd(A) ::= insert_cmd(R) INTO nm(X) inscollist_opt(F)
999                    VALUES LP itemlist(Y) RP.
1000                {A = sqlite3TriggerInsertStep(&X, F, Y, 0, R);}
1001 
1002 trigger_cmd(A) ::= insert_cmd(R) INTO nm(X) inscollist_opt(F) select(S).
1003                {A = sqlite3TriggerInsertStep(&X, F, 0, S, R);}
1004 
1005 // DELETE
1006 trigger_cmd(A) ::= DELETE FROM nm(X) where_opt(Y).
1007                {A = sqlite3TriggerDeleteStep(&X, Y);}
1008 
1009 // SELECT
1010 trigger_cmd(A) ::= select(X).  {A = sqlite3TriggerSelectStep(X); }
1011 
1012 // The special RAISE expression that may occur in trigger programs
1013 expr(A) ::= RAISE(X) LP IGNORE RP(Y).  {
1014   A = sqlite3Expr(TK_RAISE, 0, 0, 0);
1015   if( A ){
1016     A->iColumn = OE_Ignore;
1017     sqlite3ExprSpan(A, &X, &Y);
1018   }
1019 }
1020 expr(A) ::= RAISE(X) LP raisetype(T) COMMA nm(Z) RP(Y).  {
1021   A = sqlite3Expr(TK_RAISE, 0, 0, &Z);
1022   if( A ) {
1023     A->iColumn = T;
1024     sqlite3ExprSpan(A, &X, &Y);
1025   }
1026 }
1027 %endif  !SQLITE_OMIT_TRIGGER
1028 
1029 %type raisetype {int}
1030 raisetype(A) ::= ROLLBACK.  {A = OE_Rollback;}
1031 raisetype(A) ::= ABORT.     {A = OE_Abort;}
1032 raisetype(A) ::= FAIL.      {A = OE_Fail;}
1033 
1034 
1035 ////////////////////////  DROP TRIGGER statement //////////////////////////////
1036 %ifndef SQLITE_OMIT_TRIGGER
1037 cmd ::= DROP TRIGGER ifexists(NOERR) fullname(X). {
1038   sqlite3DropTrigger(pParse,X,NOERR);
1039 }
1040 %endif  !SQLITE_OMIT_TRIGGER
1041 
1042 //////////////////////// ATTACH DATABASE file AS name /////////////////////////
1043 %ifndef SQLITE_OMIT_ATTACH
1044 cmd ::= ATTACH database_kw_opt expr(F) AS expr(D) key_opt(K). {
1045   sqlite3Attach(pParse, F, D, K);
1046 }
1047 cmd ::= DETACH database_kw_opt expr(D). {
1048   sqlite3Detach(pParse, D);
1049 }
1050 
1051 %type key_opt {Expr *}
1052 %destructor key_opt {sqlite3ExprDelete($$);}
1053 key_opt(A) ::= .                     { A = 0; }
1054 key_opt(A) ::= KEY expr(X).          { A = X; }
1055 
1056 database_kw_opt ::= DATABASE.
1057 database_kw_opt ::= .
1058 %endif SQLITE_OMIT_ATTACH
1059 
1060 ////////////////////////// REINDEX collation //////////////////////////////////
1061 %ifndef SQLITE_OMIT_REINDEX
1062 cmd ::= REINDEX.                {sqlite3Reindex(pParse, 0, 0);}
1063 cmd ::= REINDEX nm(X) dbnm(Y).  {sqlite3Reindex(pParse, &X, &Y);}
1064 %endif  SQLITE_OMIT_REINDEX
1065 
1066 /////////////////////////////////// ANALYZE ///////////////////////////////////
1067 %ifndef SQLITE_OMIT_ANALYZE
1068 cmd ::= ANALYZE.                {sqlite3Analyze(pParse, 0, 0);}
1069 cmd ::= ANALYZE nm(X) dbnm(Y).  {sqlite3Analyze(pParse, &X, &Y);}
1070 %endif
1071 
1072 //////////////////////// ALTER TABLE table ... ////////////////////////////////
1073 %ifndef SQLITE_OMIT_ALTERTABLE
1074 cmd ::= ALTER TABLE fullname(X) RENAME TO nm(Z). {
1075   sqlite3AlterRenameTable(pParse,X,&Z);
1076 }
1077 cmd ::= ALTER TABLE add_column_fullname ADD kwcolumn_opt column(Y). {
1078   sqlite3AlterFinishAddColumn(pParse, &Y);
1079 }
1080 add_column_fullname ::= fullname(X). {
1081   sqlite3AlterBeginAddColumn(pParse, X);
1082 }
1083 kwcolumn_opt ::= .
1084 kwcolumn_opt ::= COLUMNKW.
1085 %endif  SQLITE_OMIT_ALTERTABLE
1086 
1087 //////////////////////// CREATE VIRTUAL TABLE ... /////////////////////////////
1088 %ifndef SQLITE_OMIT_VIRTUALTABLE
1089 cmd ::= create_vtab.                       {sqlite3VtabFinishParse(pParse,0);}
1090 cmd ::= create_vtab LP vtabarglist RP(X).  {sqlite3VtabFinishParse(pParse,&X);}
1091 create_vtab ::= CREATE VIRTUAL TABLE nm(X) dbnm(Y) USING nm(Z). {
1092     sqlite3VtabBeginParse(pParse, &X, &Y, &Z);
1093 }
1094 vtabarglist ::= vtabarg.
1095 vtabarglist ::= vtabarglist COMMA vtabarg.
1096 vtabarg ::= .                       {sqlite3VtabArgInit(pParse);}
1097 vtabarg ::= vtabarg vtabargtoken.
1098 vtabargtoken ::= ANY(X).            {sqlite3VtabArgExtend(pParse,&X);}
1099 vtabargtoken ::= lp anylist RP(X).  {sqlite3VtabArgExtend(pParse,&X);}
1100 lp ::= LP(X).                       {sqlite3VtabArgExtend(pParse,&X);}
1101 anylist ::= .
1102 anylist ::= anylist ANY(X).         {sqlite3VtabArgExtend(pParse,&X);}
1103 %endif  SQLITE_OMIT_VIRTUALTABLE
1104