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