xref: /sqlite-3.40.0/src/parse.y (revision 3ad202dd)
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.212 2006/12/20 02:15:00 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 QUERY KEY
178   OF OFFSET PRAGMA RAISE REPLACE RESTRICT ROW STATEMENT
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 %right UMINUS UPLUS BITNOT.
209 
210 // And "ids" is an identifer-or-string.
211 //
212 %type ids {Token}
213 ids(A) ::= ID|STRING(X).   {A = X;}
214 
215 // The name of a column or table can be any of the following:
216 //
217 %type nm {Token}
218 nm(A) ::= ID(X).         {A = X;}
219 nm(A) ::= STRING(X).     {A = X;}
220 nm(A) ::= JOIN_KW(X).    {A = X;}
221 
222 // A typetoken is really one or more tokens that form a type name such
223 // as can be found after the column name in a CREATE TABLE statement.
224 // Multiple tokens are concatenated to form the value of the typetoken.
225 //
226 %type typetoken {Token}
227 type ::= .
228 type ::= typetoken(X).                   {sqlite3AddColumnType(pParse,&X);}
229 typetoken(A) ::= typename(X).   {A = X;}
230 typetoken(A) ::= typename(X) LP signed RP(Y). {
231   A.z = X.z;
232   A.n = &Y.z[Y.n] - X.z;
233 }
234 typetoken(A) ::= typename(X) LP signed COMMA signed RP(Y). {
235   A.z = X.z;
236   A.n = &Y.z[Y.n] - X.z;
237 }
238 %type typename {Token}
239 typename(A) ::= ids(X).             {A = X;}
240 typename(A) ::= typename(X) ids(Y). {A.z=X.z; A.n=Y.n+(Y.z-X.z);}
241 %type signed {int}
242 signed(A) ::= plus_num(X).    { A = atoi((char*)X.z); }
243 signed(A) ::= minus_num(X).   { A = -atoi((char*)X.z); }
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   }
385   A = Z;
386 }
387 %type multiselect_op {int}
388 multiselect_op(A) ::= UNION(OP).             {A = @OP;}
389 multiselect_op(A) ::= UNION ALL.             {A = TK_ALL;}
390 multiselect_op(A) ::= EXCEPT|INTERSECT(OP).  {A = @OP;}
391 %endif SQLITE_OMIT_COMPOUND_SELECT
392 oneselect(A) ::= SELECT distinct(D) selcollist(W) from(X) where_opt(Y)
393                  groupby_opt(P) having_opt(Q) orderby_opt(Z) limit_opt(L). {
394   A = sqlite3SelectNew(W,X,Y,P,Q,Z,D,L.pLimit,L.pOffset);
395 }
396 
397 // The "distinct" nonterminal is true (1) if the DISTINCT keyword is
398 // present and false (0) if it is not.
399 //
400 %type distinct {int}
401 distinct(A) ::= DISTINCT.   {A = 1;}
402 distinct(A) ::= ALL.        {A = 0;}
403 distinct(A) ::= .           {A = 0;}
404 
405 // selcollist is a list of expressions that are to become the return
406 // values of the SELECT statement.  The "*" in statements like
407 // "SELECT * FROM ..." is encoded as a special expression with an
408 // opcode of TK_ALL.
409 //
410 %type selcollist {ExprList*}
411 %destructor selcollist {sqlite3ExprListDelete($$);}
412 %type sclp {ExprList*}
413 %destructor sclp {sqlite3ExprListDelete($$);}
414 sclp(A) ::= selcollist(X) COMMA.             {A = X;}
415 sclp(A) ::= .                                {A = 0;}
416 selcollist(A) ::= sclp(P) expr(X) as(Y).     {
417    A = sqlite3ExprListAppend(P,X,Y.n?&Y:0);
418 }
419 selcollist(A) ::= sclp(P) STAR. {
420   A = sqlite3ExprListAppend(P, sqlite3Expr(TK_ALL, 0, 0, 0), 0);
421 }
422 selcollist(A) ::= sclp(P) nm(X) DOT STAR. {
423   Expr *pRight = sqlite3Expr(TK_ALL, 0, 0, 0);
424   Expr *pLeft = sqlite3Expr(TK_ID, 0, 0, &X);
425   A = sqlite3ExprListAppend(P, sqlite3Expr(TK_DOT, pLeft, pRight, 0), 0);
426 }
427 
428 // An option "AS <id>" phrase that can follow one of the expressions that
429 // define the result set, or one of the tables in the FROM clause.
430 //
431 %type as {Token}
432 as(X) ::= AS nm(Y).    {X = Y;}
433 as(X) ::= ids(Y).      {X = Y;}
434 as(X) ::= .            {X.n = 0;}
435 
436 
437 %type seltablist {SrcList*}
438 %destructor seltablist {sqlite3SrcListDelete($$);}
439 %type stl_prefix {SrcList*}
440 %destructor stl_prefix {sqlite3SrcListDelete($$);}
441 %type from {SrcList*}
442 %destructor from {sqlite3SrcListDelete($$);}
443 
444 // A complete FROM clause.
445 //
446 from(A) ::= .                                 {A = sqliteMalloc(sizeof(*A));}
447 from(A) ::= FROM seltablist(X).               {
448   A = X;
449   sqlite3SrcListShiftJoinType(A);
450 }
451 
452 // "seltablist" is a "Select Table List" - the content of the FROM clause
453 // in a SELECT statement.  "stl_prefix" is a prefix of this list.
454 //
455 stl_prefix(A) ::= seltablist(X) joinop(Y).    {
456    A = X;
457    if( A && A->nSrc>0 ) A->a[A->nSrc-1].jointype = Y;
458 }
459 stl_prefix(A) ::= .                           {A = 0;}
460 seltablist(A) ::= stl_prefix(X) nm(Y) dbnm(D) as(Z) on_opt(N) using_opt(U). {
461   A = sqlite3SrcListAppendFromTerm(X,&Y,&D,&Z,0,N,U);
462 }
463 %ifndef SQLITE_OMIT_SUBQUERY
464   seltablist(A) ::= stl_prefix(X) LP seltablist_paren(S) RP
465                     as(Z) on_opt(N) using_opt(U). {
466     A = sqlite3SrcListAppendFromTerm(X,0,0,&Z,S,N,U);
467   }
468 
469   // A seltablist_paren nonterminal represents anything in a FROM that
470   // is contained inside parentheses.  This can be either a subquery or
471   // a grouping of table and subqueries.
472   //
473   %type seltablist_paren {Select*}
474   %destructor seltablist_paren {sqlite3SelectDelete($$);}
475   seltablist_paren(A) ::= select(S).      {A = S;}
476   seltablist_paren(A) ::= seltablist(F).  {
477      sqlite3SrcListShiftJoinType(F);
478      A = sqlite3SelectNew(0,F,0,0,0,0,0,0,0);
479   }
480 %endif  SQLITE_OMIT_SUBQUERY
481 
482 %type dbnm {Token}
483 dbnm(A) ::= .          {A.z=0; A.n=0;}
484 dbnm(A) ::= DOT nm(X). {A = X;}
485 
486 %type fullname {SrcList*}
487 %destructor fullname {sqlite3SrcListDelete($$);}
488 fullname(A) ::= nm(X) dbnm(Y).  {A = sqlite3SrcListAppend(0,&X,&Y);}
489 
490 %type joinop {int}
491 %type joinop2 {int}
492 joinop(X) ::= COMMA|JOIN.              { X = JT_INNER; }
493 joinop(X) ::= JOIN_KW(A) JOIN.         { X = sqlite3JoinType(pParse,&A,0,0); }
494 joinop(X) ::= JOIN_KW(A) nm(B) JOIN.   { X = sqlite3JoinType(pParse,&A,&B,0); }
495 joinop(X) ::= JOIN_KW(A) nm(B) nm(C) JOIN.
496                                        { X = sqlite3JoinType(pParse,&A,&B,&C); }
497 
498 %type on_opt {Expr*}
499 %destructor on_opt {sqlite3ExprDelete($$);}
500 on_opt(N) ::= ON expr(E).   {N = E;}
501 on_opt(N) ::= .             {N = 0;}
502 
503 %type using_opt {IdList*}
504 %destructor using_opt {sqlite3IdListDelete($$);}
505 using_opt(U) ::= USING LP inscollist(L) RP.  {U = L;}
506 using_opt(U) ::= .                        {U = 0;}
507 
508 
509 %type orderby_opt {ExprList*}
510 %destructor orderby_opt {sqlite3ExprListDelete($$);}
511 %type sortlist {ExprList*}
512 %destructor sortlist {sqlite3ExprListDelete($$);}
513 %type sortitem {Expr*}
514 %destructor sortitem {sqlite3ExprDelete($$);}
515 
516 orderby_opt(A) ::= .                          {A = 0;}
517 orderby_opt(A) ::= ORDER BY sortlist(X).      {A = X;}
518 sortlist(A) ::= sortlist(X) COMMA sortitem(Y) collate(C) sortorder(Z). {
519   A = sqlite3ExprListAppend(X,Y,C.n>0?&C:0);
520   if( A ) A->a[A->nExpr-1].sortOrder = Z;
521 }
522 sortlist(A) ::= sortitem(Y) collate(C) sortorder(Z). {
523   A = sqlite3ExprListAppend(0,Y,C.n>0?&C:0);
524   if( A && A->a ) A->a[0].sortOrder = Z;
525 }
526 sortitem(A) ::= expr(X).   {A = X;}
527 
528 %type sortorder {int}
529 %type collate {Token}
530 
531 sortorder(A) ::= ASC.           {A = SQLITE_SO_ASC;}
532 sortorder(A) ::= DESC.          {A = SQLITE_SO_DESC;}
533 sortorder(A) ::= .              {A = SQLITE_SO_ASC;}
534 collate(C) ::= .                {C.z = 0; C.n = 0;}
535 collate(C) ::= COLLATE id(X).   {C = X;}
536 
537 %type groupby_opt {ExprList*}
538 %destructor groupby_opt {sqlite3ExprListDelete($$);}
539 groupby_opt(A) ::= .                      {A = 0;}
540 groupby_opt(A) ::= GROUP BY exprlist(X).  {A = X;}
541 
542 %type having_opt {Expr*}
543 %destructor having_opt {sqlite3ExprDelete($$);}
544 having_opt(A) ::= .                {A = 0;}
545 having_opt(A) ::= HAVING expr(X).  {A = X;}
546 
547 %type limit_opt {struct LimitVal}
548 %destructor limit_opt {
549   sqlite3ExprDelete($$.pLimit);
550   sqlite3ExprDelete($$.pOffset);
551 }
552 limit_opt(A) ::= .                     {A.pLimit = 0; A.pOffset = 0;}
553 limit_opt(A) ::= LIMIT expr(X).        {A.pLimit = X; A.pOffset = 0;}
554 limit_opt(A) ::= LIMIT expr(X) OFFSET expr(Y).
555                                        {A.pLimit = X; A.pOffset = Y;}
556 limit_opt(A) ::= LIMIT expr(X) COMMA expr(Y).
557                                        {A.pOffset = X; A.pLimit = Y;}
558 
559 /////////////////////////// The DELETE statement /////////////////////////////
560 //
561 cmd ::= DELETE FROM fullname(X) where_opt(Y). {sqlite3DeleteFrom(pParse,X,Y);}
562 
563 %type where_opt {Expr*}
564 %destructor where_opt {sqlite3ExprDelete($$);}
565 
566 where_opt(A) ::= .                    {A = 0;}
567 where_opt(A) ::= WHERE expr(X).       {A = X;}
568 
569 ////////////////////////// The UPDATE command ////////////////////////////////
570 //
571 cmd ::= UPDATE orconf(R) fullname(X) SET setlist(Y) where_opt(Z).
572     {sqlite3Update(pParse,X,Y,Z,R);}
573 
574 %type setlist {ExprList*}
575 %destructor setlist {sqlite3ExprListDelete($$);}
576 
577 setlist(A) ::= setlist(Z) COMMA nm(X) EQ expr(Y).
578     {A = sqlite3ExprListAppend(Z,Y,&X);}
579 setlist(A) ::= nm(X) EQ expr(Y).   {A = sqlite3ExprListAppend(0,Y,&X);}
580 
581 ////////////////////////// The INSERT command /////////////////////////////////
582 //
583 cmd ::= insert_cmd(R) INTO fullname(X) inscollist_opt(F)
584         VALUES LP itemlist(Y) RP.
585             {sqlite3Insert(pParse, X, Y, 0, F, R);}
586 cmd ::= insert_cmd(R) INTO fullname(X) inscollist_opt(F) select(S).
587             {sqlite3Insert(pParse, X, 0, S, F, R);}
588 cmd ::= insert_cmd(R) INTO fullname(X) inscollist_opt(F) DEFAULT VALUES.
589             {sqlite3Insert(pParse, X, 0, 0, F, R);}
590 
591 %type insert_cmd {int}
592 insert_cmd(A) ::= INSERT orconf(R).   {A = R;}
593 insert_cmd(A) ::= REPLACE.            {A = OE_Replace;}
594 
595 
596 %type itemlist {ExprList*}
597 %destructor itemlist {sqlite3ExprListDelete($$);}
598 
599 itemlist(A) ::= itemlist(X) COMMA expr(Y).  {A = sqlite3ExprListAppend(X,Y,0);}
600 itemlist(A) ::= expr(X).                    {A = sqlite3ExprListAppend(0,X,0);}
601 
602 %type inscollist_opt {IdList*}
603 %destructor inscollist_opt {sqlite3IdListDelete($$);}
604 %type inscollist {IdList*}
605 %destructor inscollist {sqlite3IdListDelete($$);}
606 
607 inscollist_opt(A) ::= .                       {A = 0;}
608 inscollist_opt(A) ::= LP inscollist(X) RP.    {A = X;}
609 inscollist(A) ::= inscollist(X) COMMA nm(Y).  {A = sqlite3IdListAppend(X,&Y);}
610 inscollist(A) ::= nm(Y).                      {A = sqlite3IdListAppend(0,&Y);}
611 
612 /////////////////////////// Expression Processing /////////////////////////////
613 //
614 
615 %type expr {Expr*}
616 %destructor expr {sqlite3ExprDelete($$);}
617 %type term {Expr*}
618 %destructor term {sqlite3ExprDelete($$);}
619 
620 expr(A) ::= term(X).             {A = X;}
621 expr(A) ::= LP(B) expr(X) RP(E). {A = X; sqlite3ExprSpan(A,&B,&E); }
622 term(A) ::= NULL(X).             {A = sqlite3Expr(@X, 0, 0, &X);}
623 expr(A) ::= ID(X).               {A = sqlite3Expr(TK_ID, 0, 0, &X);}
624 expr(A) ::= JOIN_KW(X).          {A = sqlite3Expr(TK_ID, 0, 0, &X);}
625 expr(A) ::= nm(X) DOT nm(Y). {
626   Expr *temp1 = sqlite3Expr(TK_ID, 0, 0, &X);
627   Expr *temp2 = sqlite3Expr(TK_ID, 0, 0, &Y);
628   A = sqlite3Expr(TK_DOT, temp1, temp2, 0);
629 }
630 expr(A) ::= nm(X) DOT nm(Y) DOT nm(Z). {
631   Expr *temp1 = sqlite3Expr(TK_ID, 0, 0, &X);
632   Expr *temp2 = sqlite3Expr(TK_ID, 0, 0, &Y);
633   Expr *temp3 = sqlite3Expr(TK_ID, 0, 0, &Z);
634   Expr *temp4 = sqlite3Expr(TK_DOT, temp2, temp3, 0);
635   A = sqlite3Expr(TK_DOT, temp1, temp4, 0);
636 }
637 term(A) ::= INTEGER|FLOAT|BLOB(X).      {A = sqlite3Expr(@X, 0, 0, &X);}
638 term(A) ::= STRING(X).       {A = sqlite3Expr(@X, 0, 0, &X);}
639 expr(A) ::= REGISTER(X).     {A = sqlite3RegisterExpr(pParse, &X);}
640 expr(A) ::= VARIABLE(X).     {
641   Token *pToken = &X;
642   Expr *pExpr = A = sqlite3Expr(TK_VARIABLE, 0, 0, pToken);
643   sqlite3ExprAssignVarNumber(pParse, pExpr);
644 }
645 %ifndef SQLITE_OMIT_CAST
646 expr(A) ::= CAST(X) LP expr(E) AS typetoken(T) RP(Y). {
647   A = sqlite3Expr(TK_CAST, E, 0, &T);
648   sqlite3ExprSpan(A,&X,&Y);
649 }
650 %endif  SQLITE_OMIT_CAST
651 expr(A) ::= ID(X) LP distinct(D) exprlist(Y) RP(E). {
652   A = sqlite3ExprFunction(Y, &X);
653   sqlite3ExprSpan(A,&X,&E);
654   if( D && A ){
655     A->flags |= EP_Distinct;
656   }
657 }
658 expr(A) ::= ID(X) LP STAR RP(E). {
659   A = sqlite3ExprFunction(0, &X);
660   sqlite3ExprSpan(A,&X,&E);
661 }
662 term(A) ::= CTIME_KW(OP). {
663   /* The CURRENT_TIME, CURRENT_DATE, and CURRENT_TIMESTAMP values are
664   ** treated as functions that return constants */
665   A = sqlite3ExprFunction(0,&OP);
666   if( A ){
667     A->op = TK_CONST_FUNC;
668     A->span = OP;
669   }
670 }
671 expr(A) ::= expr(X) AND(OP) expr(Y).            {A = sqlite3Expr(@OP, X, Y, 0);}
672 expr(A) ::= expr(X) OR(OP) expr(Y).             {A = sqlite3Expr(@OP, X, Y, 0);}
673 expr(A) ::= expr(X) LT|GT|GE|LE(OP) expr(Y).    {A = sqlite3Expr(@OP, X, Y, 0);}
674 expr(A) ::= expr(X) EQ|NE(OP) expr(Y).          {A = sqlite3Expr(@OP, X, Y, 0);}
675 expr(A) ::= expr(X) BITAND|BITOR|LSHIFT|RSHIFT(OP) expr(Y).
676                                                 {A = sqlite3Expr(@OP, X, Y, 0);}
677 expr(A) ::= expr(X) PLUS|MINUS(OP) expr(Y).     {A = sqlite3Expr(@OP, X, Y, 0);}
678 expr(A) ::= expr(X) STAR|SLASH|REM(OP) expr(Y). {A = sqlite3Expr(@OP, X, Y, 0);}
679 expr(A) ::= expr(X) CONCAT(OP) expr(Y).         {A = sqlite3Expr(@OP, X, Y, 0);}
680 %type likeop {struct LikeOp}
681 likeop(A) ::= LIKE_KW(X).     {A.eOperator = X; A.not = 0;}
682 likeop(A) ::= NOT LIKE_KW(X). {A.eOperator = X; A.not = 1;}
683 likeop(A) ::= MATCH(X).       {A.eOperator = X; A.not = 0;}
684 likeop(A) ::= NOT MATCH(X).   {A.eOperator = X; A.not = 1;}
685 %type escape {Expr*}
686 %destructor escape {sqlite3ExprDelete($$);}
687 escape(X) ::= ESCAPE expr(A). [ESCAPE] {X = A;}
688 escape(X) ::= .               [ESCAPE] {X = 0;}
689 expr(A) ::= expr(X) likeop(OP) expr(Y) escape(E).  [LIKE_KW]  {
690   ExprList *pList;
691   pList = sqlite3ExprListAppend(0, Y, 0);
692   pList = sqlite3ExprListAppend(pList, X, 0);
693   if( E ){
694     pList = sqlite3ExprListAppend(pList, E, 0);
695   }
696   A = sqlite3ExprFunction(pList, &OP.eOperator);
697   if( OP.not ) A = sqlite3Expr(TK_NOT, A, 0, 0);
698   sqlite3ExprSpan(A, &X->span, &Y->span);
699   if( A ) A->flags |= EP_InfixFunc;
700 }
701 
702 expr(A) ::= expr(X) ISNULL|NOTNULL(E). {
703   A = sqlite3Expr(@E, X, 0, 0);
704   sqlite3ExprSpan(A,&X->span,&E);
705 }
706 expr(A) ::= expr(X) IS NULL(E). {
707   A = sqlite3Expr(TK_ISNULL, X, 0, 0);
708   sqlite3ExprSpan(A,&X->span,&E);
709 }
710 expr(A) ::= expr(X) NOT NULL(E). {
711   A = sqlite3Expr(TK_NOTNULL, X, 0, 0);
712   sqlite3ExprSpan(A,&X->span,&E);
713 }
714 expr(A) ::= expr(X) IS NOT NULL(E). {
715   A = sqlite3Expr(TK_NOTNULL, X, 0, 0);
716   sqlite3ExprSpan(A,&X->span,&E);
717 }
718 expr(A) ::= NOT|BITNOT(B) expr(X). {
719   A = sqlite3Expr(@B, X, 0, 0);
720   sqlite3ExprSpan(A,&B,&X->span);
721 }
722 expr(A) ::= MINUS(B) expr(X). [UMINUS] {
723   A = sqlite3Expr(TK_UMINUS, X, 0, 0);
724   sqlite3ExprSpan(A,&B,&X->span);
725 }
726 expr(A) ::= PLUS(B) expr(X). [UPLUS] {
727   A = sqlite3Expr(TK_UPLUS, X, 0, 0);
728   sqlite3ExprSpan(A,&B,&X->span);
729 }
730 %type between_op {int}
731 between_op(A) ::= BETWEEN.     {A = 0;}
732 between_op(A) ::= NOT BETWEEN. {A = 1;}
733 expr(A) ::= expr(W) between_op(N) expr(X) AND expr(Y). [BETWEEN] {
734   ExprList *pList = sqlite3ExprListAppend(0, X, 0);
735   pList = sqlite3ExprListAppend(pList, Y, 0);
736   A = sqlite3Expr(TK_BETWEEN, W, 0, 0);
737   if( A ){
738     A->pList = pList;
739   }else{
740     sqlite3ExprListDelete(pList);
741   }
742   if( N ) A = sqlite3Expr(TK_NOT, A, 0, 0);
743   sqlite3ExprSpan(A,&W->span,&Y->span);
744 }
745 %ifndef SQLITE_OMIT_SUBQUERY
746   %type in_op {int}
747   in_op(A) ::= IN.      {A = 0;}
748   in_op(A) ::= NOT IN.  {A = 1;}
749   expr(A) ::= expr(X) in_op(N) LP exprlist(Y) RP(E). [IN] {
750     A = sqlite3Expr(TK_IN, X, 0, 0);
751     if( A ){
752       A->pList = Y;
753     }else{
754       sqlite3ExprListDelete(Y);
755     }
756     if( N ) A = sqlite3Expr(TK_NOT, A, 0, 0);
757     sqlite3ExprSpan(A,&X->span,&E);
758   }
759   expr(A) ::= LP(B) select(X) RP(E). {
760     A = sqlite3Expr(TK_SELECT, 0, 0, 0);
761     if( A ){
762       A->pSelect = X;
763     }else{
764       sqlite3SelectDelete(X);
765     }
766     sqlite3ExprSpan(A,&B,&E);
767   }
768   expr(A) ::= expr(X) in_op(N) LP select(Y) RP(E).  [IN] {
769     A = sqlite3Expr(TK_IN, X, 0, 0);
770     if( A ){
771       A->pSelect = Y;
772     }else{
773       sqlite3SelectDelete(Y);
774     }
775     if( N ) A = sqlite3Expr(TK_NOT, A, 0, 0);
776     sqlite3ExprSpan(A,&X->span,&E);
777   }
778   expr(A) ::= expr(X) in_op(N) nm(Y) dbnm(Z). [IN] {
779     SrcList *pSrc = sqlite3SrcListAppend(0,&Y,&Z);
780     A = sqlite3Expr(TK_IN, X, 0, 0);
781     if( A ){
782       A->pSelect = sqlite3SelectNew(0,pSrc,0,0,0,0,0,0,0);
783     }else{
784       sqlite3SrcListDelete(pSrc);
785     }
786     if( N ) A = sqlite3Expr(TK_NOT, A, 0, 0);
787     sqlite3ExprSpan(A,&X->span,Z.z?&Z:&Y);
788   }
789   expr(A) ::= EXISTS(B) LP select(Y) RP(E). {
790     Expr *p = A = sqlite3Expr(TK_EXISTS, 0, 0, 0);
791     if( p ){
792       p->pSelect = Y;
793       sqlite3ExprSpan(p,&B,&E);
794     }else{
795       sqlite3SelectDelete(Y);
796     }
797   }
798 %endif SQLITE_OMIT_SUBQUERY
799 
800 /* CASE expressions */
801 expr(A) ::= CASE(C) case_operand(X) case_exprlist(Y) case_else(Z) END(E). {
802   A = sqlite3Expr(TK_CASE, X, Z, 0);
803   if( A ){
804     A->pList = Y;
805   }else{
806     sqlite3ExprListDelete(Y);
807   }
808   sqlite3ExprSpan(A, &C, &E);
809 }
810 %type case_exprlist {ExprList*}
811 %destructor case_exprlist {sqlite3ExprListDelete($$);}
812 case_exprlist(A) ::= case_exprlist(X) WHEN expr(Y) THEN expr(Z). {
813   A = sqlite3ExprListAppend(X, Y, 0);
814   A = sqlite3ExprListAppend(A, Z, 0);
815 }
816 case_exprlist(A) ::= WHEN expr(Y) THEN expr(Z). {
817   A = sqlite3ExprListAppend(0, Y, 0);
818   A = sqlite3ExprListAppend(A, Z, 0);
819 }
820 %type case_else {Expr*}
821 %destructor case_else {sqlite3ExprDelete($$);}
822 case_else(A) ::=  ELSE expr(X).         {A = X;}
823 case_else(A) ::=  .                     {A = 0;}
824 %type case_operand {Expr*}
825 %destructor case_operand {sqlite3ExprDelete($$);}
826 case_operand(A) ::= expr(X).            {A = X;}
827 case_operand(A) ::= .                   {A = 0;}
828 
829 %type exprlist {ExprList*}
830 %destructor exprlist {sqlite3ExprListDelete($$);}
831 %type expritem {Expr*}
832 %destructor expritem {sqlite3ExprDelete($$);}
833 
834 exprlist(A) ::= exprlist(X) COMMA expritem(Y).
835                                         {A = sqlite3ExprListAppend(X,Y,0);}
836 exprlist(A) ::= expritem(X).            {A = sqlite3ExprListAppend(0,X,0);}
837 expritem(A) ::= expr(X).                {A = X;}
838 expritem(A) ::= .                       {A = 0;}
839 
840 ///////////////////////////// The CREATE INDEX command ///////////////////////
841 //
842 cmd ::= CREATE(S) uniqueflag(U) INDEX ifnotexists(NE) nm(X) dbnm(D)
843         ON nm(Y) LP idxlist(Z) RP(E). {
844   sqlite3CreateIndex(pParse, &X, &D, sqlite3SrcListAppend(0,&Y,0), Z, U,
845                       &S, &E, SQLITE_SO_ASC, NE);
846 }
847 
848 %type uniqueflag {int}
849 uniqueflag(A) ::= UNIQUE.  {A = OE_Abort;}
850 uniqueflag(A) ::= .        {A = OE_None;}
851 
852 %type idxlist {ExprList*}
853 %destructor idxlist {sqlite3ExprListDelete($$);}
854 %type idxlist_opt {ExprList*}
855 %destructor idxlist_opt {sqlite3ExprListDelete($$);}
856 %type idxitem {Token}
857 
858 idxlist_opt(A) ::= .                         {A = 0;}
859 idxlist_opt(A) ::= LP idxlist(X) RP.         {A = X;}
860 idxlist(A) ::= idxlist(X) COMMA idxitem(Y) collate(C) sortorder(Z).  {
861   Expr *p = 0;
862   if( C.n>0 ){
863     p = sqlite3Expr(TK_COLUMN, 0, 0, 0);
864     if( p ) p->pColl = sqlite3LocateCollSeq(pParse, (char*)C.z, C.n);
865   }
866   A = sqlite3ExprListAppend(X, p, &Y);
867   if( A ) A->a[A->nExpr-1].sortOrder = Z;
868 }
869 idxlist(A) ::= idxitem(Y) collate(C) sortorder(Z). {
870   Expr *p = 0;
871   if( C.n>0 ){
872     p = sqlite3Expr(TK_COLUMN, 0, 0, 0);
873     if( p ) p->pColl = sqlite3LocateCollSeq(pParse, (char*)C.z, C.n);
874   }
875   A = sqlite3ExprListAppend(0, p, &Y);
876   if( A ) A->a[A->nExpr-1].sortOrder = Z;
877 }
878 idxitem(A) ::= nm(X).              {A = X;}
879 
880 
881 ///////////////////////////// The DROP INDEX command /////////////////////////
882 //
883 cmd ::= DROP INDEX ifexists(E) fullname(X).   {sqlite3DropIndex(pParse, X, E);}
884 
885 ///////////////////////////// The VACUUM command /////////////////////////////
886 //
887 %ifndef SQLITE_OMIT_VACUUM
888 cmd ::= VACUUM.                {sqlite3Vacuum(pParse);}
889 cmd ::= VACUUM nm.             {sqlite3Vacuum(pParse);}
890 %endif  SQLITE_OMIT_VACUUM
891 
892 ///////////////////////////// The PRAGMA command /////////////////////////////
893 //
894 %ifndef SQLITE_OMIT_PRAGMA
895 cmd ::= PRAGMA nm(X) dbnm(Z) EQ nm(Y).  {sqlite3Pragma(pParse,&X,&Z,&Y,0);}
896 cmd ::= PRAGMA nm(X) dbnm(Z) EQ ON(Y).  {sqlite3Pragma(pParse,&X,&Z,&Y,0);}
897 cmd ::= PRAGMA nm(X) dbnm(Z) EQ plus_num(Y). {sqlite3Pragma(pParse,&X,&Z,&Y,0);}
898 cmd ::= PRAGMA nm(X) dbnm(Z) EQ minus_num(Y). {
899   sqlite3Pragma(pParse,&X,&Z,&Y,1);
900 }
901 cmd ::= PRAGMA nm(X) dbnm(Z) LP nm(Y) RP. {sqlite3Pragma(pParse,&X,&Z,&Y,0);}
902 cmd ::= PRAGMA nm(X) dbnm(Z).             {sqlite3Pragma(pParse,&X,&Z,0,0);}
903 %endif SQLITE_OMIT_PRAGMA
904 plus_num(A) ::= plus_opt number(X).   {A = X;}
905 minus_num(A) ::= MINUS number(X).     {A = X;}
906 number(A) ::= INTEGER|FLOAT(X).       {A = X;}
907 plus_opt ::= PLUS.
908 plus_opt ::= .
909 
910 //////////////////////////// The CREATE TRIGGER command /////////////////////
911 
912 %ifndef SQLITE_OMIT_TRIGGER
913 
914 cmd ::= CREATE trigger_decl(A) BEGIN trigger_cmd_list(S) END(Z). {
915   Token all;
916   all.z = A.z;
917   all.n = (Z.z - A.z) + Z.n;
918   sqlite3FinishTrigger(pParse, S, &all);
919 }
920 
921 trigger_decl(A) ::= temp(T) TRIGGER ifnotexists(NOERR) nm(B) dbnm(Z)
922                     trigger_time(C) trigger_event(D)
923                     ON fullname(E) foreach_clause(F) when_clause(G). {
924   sqlite3BeginTrigger(pParse, &B, &Z, C, D.a, D.b, E, F, G, T, NOERR);
925   A = (Z.n==0?B:Z);
926 }
927 
928 %type trigger_time  {int}
929 trigger_time(A) ::= BEFORE.      { A = TK_BEFORE; }
930 trigger_time(A) ::= AFTER.       { A = TK_AFTER;  }
931 trigger_time(A) ::= INSTEAD OF.  { A = TK_INSTEAD;}
932 trigger_time(A) ::= .            { A = TK_BEFORE; }
933 
934 %type trigger_event {struct TrigEvent}
935 %destructor trigger_event {sqlite3IdListDelete($$.b);}
936 trigger_event(A) ::= DELETE|INSERT(OP).       {A.a = @OP; A.b = 0;}
937 trigger_event(A) ::= UPDATE(OP).              {A.a = @OP; A.b = 0;}
938 trigger_event(A) ::= UPDATE OF inscollist(X). {A.a = TK_UPDATE; A.b = X;}
939 
940 %type foreach_clause {int}
941 foreach_clause(A) ::= .                   { A = TK_ROW; }
942 foreach_clause(A) ::= FOR EACH ROW.       { A = TK_ROW; }
943 foreach_clause(A) ::= FOR EACH STATEMENT. { A = TK_STATEMENT; }
944 
945 %type when_clause {Expr*}
946 %destructor when_clause {sqlite3ExprDelete($$);}
947 when_clause(A) ::= .             { A = 0; }
948 when_clause(A) ::= WHEN expr(X). { A = X; }
949 
950 %type trigger_cmd_list {TriggerStep*}
951 %destructor trigger_cmd_list {sqlite3DeleteTriggerStep($$);}
952 trigger_cmd_list(A) ::= trigger_cmd_list(Y) trigger_cmd(X) SEMI. {
953   if( Y ){
954     Y->pLast->pNext = X;
955   }else{
956     Y = X;
957   }
958   Y->pLast = X;
959   A = Y;
960 }
961 trigger_cmd_list(A) ::= . { A = 0; }
962 
963 %type trigger_cmd {TriggerStep*}
964 %destructor trigger_cmd {sqlite3DeleteTriggerStep($$);}
965 // UPDATE
966 trigger_cmd(A) ::= UPDATE orconf(R) nm(X) SET setlist(Y) where_opt(Z).
967                { A = sqlite3TriggerUpdateStep(&X, Y, Z, R); }
968 
969 // INSERT
970 trigger_cmd(A) ::= insert_cmd(R) INTO nm(X) inscollist_opt(F)
971                    VALUES LP itemlist(Y) RP.
972                {A = sqlite3TriggerInsertStep(&X, F, Y, 0, R);}
973 
974 trigger_cmd(A) ::= insert_cmd(R) INTO nm(X) inscollist_opt(F) select(S).
975                {A = sqlite3TriggerInsertStep(&X, F, 0, S, R);}
976 
977 // DELETE
978 trigger_cmd(A) ::= DELETE FROM nm(X) where_opt(Y).
979                {A = sqlite3TriggerDeleteStep(&X, Y);}
980 
981 // SELECT
982 trigger_cmd(A) ::= select(X).  {A = sqlite3TriggerSelectStep(X); }
983 
984 // The special RAISE expression that may occur in trigger programs
985 expr(A) ::= RAISE(X) LP IGNORE RP(Y).  {
986   A = sqlite3Expr(TK_RAISE, 0, 0, 0);
987   if( A ){
988     A->iColumn = OE_Ignore;
989     sqlite3ExprSpan(A, &X, &Y);
990   }
991 }
992 expr(A) ::= RAISE(X) LP raisetype(T) COMMA nm(Z) RP(Y).  {
993   A = sqlite3Expr(TK_RAISE, 0, 0, &Z);
994   if( A ) {
995     A->iColumn = T;
996     sqlite3ExprSpan(A, &X, &Y);
997   }
998 }
999 %endif  !SQLITE_OMIT_TRIGGER
1000 
1001 %type raisetype {int}
1002 raisetype(A) ::= ROLLBACK.  {A = OE_Rollback;}
1003 raisetype(A) ::= ABORT.     {A = OE_Abort;}
1004 raisetype(A) ::= FAIL.      {A = OE_Fail;}
1005 
1006 
1007 ////////////////////////  DROP TRIGGER statement //////////////////////////////
1008 %ifndef SQLITE_OMIT_TRIGGER
1009 cmd ::= DROP TRIGGER ifexists(NOERR) fullname(X). {
1010   sqlite3DropTrigger(pParse,X,NOERR);
1011 }
1012 %endif  !SQLITE_OMIT_TRIGGER
1013 
1014 //////////////////////// ATTACH DATABASE file AS name /////////////////////////
1015 cmd ::= ATTACH database_kw_opt expr(F) AS expr(D) key_opt(K). {
1016   sqlite3Attach(pParse, F, D, K);
1017 }
1018 %type key_opt {Expr *}
1019 %destructor key_opt {sqlite3ExprDelete($$);}
1020 key_opt(A) ::= .                     { A = 0; }
1021 key_opt(A) ::= KEY expr(X).          { A = X; }
1022 
1023 database_kw_opt ::= DATABASE.
1024 database_kw_opt ::= .
1025 
1026 //////////////////////// DETACH DATABASE name /////////////////////////////////
1027 cmd ::= DETACH database_kw_opt expr(D). {
1028   sqlite3Detach(pParse, D);
1029 }
1030 
1031 ////////////////////////// REINDEX collation //////////////////////////////////
1032 %ifndef SQLITE_OMIT_REINDEX
1033 cmd ::= REINDEX.                {sqlite3Reindex(pParse, 0, 0);}
1034 cmd ::= REINDEX nm(X) dbnm(Y).  {sqlite3Reindex(pParse, &X, &Y);}
1035 %endif  SQLITE_OMIT_REINDEX
1036 
1037 /////////////////////////////////// ANALYZE ///////////////////////////////////
1038 %ifndef SQLITE_OMIT_ANALYZE
1039 cmd ::= ANALYZE.                {sqlite3Analyze(pParse, 0, 0);}
1040 cmd ::= ANALYZE nm(X) dbnm(Y).  {sqlite3Analyze(pParse, &X, &Y);}
1041 %endif
1042 
1043 //////////////////////// ALTER TABLE table ... ////////////////////////////////
1044 %ifndef SQLITE_OMIT_ALTERTABLE
1045 cmd ::= ALTER TABLE fullname(X) RENAME TO nm(Z). {
1046   sqlite3AlterRenameTable(pParse,X,&Z);
1047 }
1048 cmd ::= ALTER TABLE add_column_fullname ADD kwcolumn_opt column(Y). {
1049   sqlite3AlterFinishAddColumn(pParse, &Y);
1050 }
1051 add_column_fullname ::= fullname(X). {
1052   sqlite3AlterBeginAddColumn(pParse, X);
1053 }
1054 kwcolumn_opt ::= .
1055 kwcolumn_opt ::= COLUMNKW.
1056 %endif  SQLITE_OMIT_ALTERTABLE
1057 
1058 //////////////////////// CREATE VIRTUAL TABLE ... /////////////////////////////
1059 %ifndef SQLITE_OMIT_VIRTUALTABLE
1060 cmd ::= create_vtab.                       {sqlite3VtabFinishParse(pParse,0);}
1061 cmd ::= create_vtab LP vtabarglist RP(X).  {sqlite3VtabFinishParse(pParse,&X);}
1062 create_vtab ::= CREATE VIRTUAL TABLE nm(X) dbnm(Y) USING nm(Z). {
1063     sqlite3VtabBeginParse(pParse, &X, &Y, &Z);
1064 }
1065 vtabarglist ::= vtabarg.
1066 vtabarglist ::= vtabarglist COMMA vtabarg.
1067 vtabarg ::= .                       {sqlite3VtabArgInit(pParse);}
1068 vtabarg ::= vtabarg vtabargtoken.
1069 vtabargtoken ::= ANY(X).            {sqlite3VtabArgExtend(pParse,&X);}
1070 vtabargtoken ::= lp anylist RP(X).  {sqlite3VtabArgExtend(pParse,&X);}
1071 lp ::= LP(X).                       {sqlite3VtabArgExtend(pParse,&X);}
1072 anylist ::= .
1073 anylist ::= anylist ANY(X).         {sqlite3VtabArgExtend(pParse,&X);}
1074 %endif  SQLITE_OMIT_VIRTUALTABLE
1075