Omit all window-function related code when building with SQLITE_OMIT_WINDOWFUNC.
[sqlite.git] / src / parse.y
blobb253480588fbdcddddec7b6c8e25a48eb1b011b8
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.
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.
18 // All token codes are small integers with #defines that begin with "TK_"
19 %token_prefix TK_
21 // The type of the data attached to each token is Token. This is also the
22 // default type for non-terminals.
24 %token_type {Token}
25 %default_type {Token}
27 // An extra argument to the constructor for the parser, which is available
28 // to all actions.
29 %extra_context {Parse *pParse}
31 // This code runs whenever there is a syntax error
33 %syntax_error {
34 UNUSED_PARAMETER(yymajor); /* Silence some compiler warnings */
35 if( TOKEN.z[0] ){
36 sqlite3ErrorMsg(pParse, "near \"%T\": syntax error", &TOKEN);
37 }else{
38 sqlite3ErrorMsg(pParse, "incomplete input");
41 %stack_overflow {
42 sqlite3ErrorMsg(pParse, "parser stack overflow");
45 // The name of the generated procedure that implements the parser
46 // is as follows:
47 %name sqlite3Parser
49 // The following text is included near the beginning of the C source
50 // code file that implements the parser.
52 %include {
53 #include "sqliteInt.h"
56 ** Disable all error recovery processing in the parser push-down
57 ** automaton.
59 #define YYNOERRORRECOVERY 1
62 ** Make yytestcase() the same as testcase()
64 #define yytestcase(X) testcase(X)
67 ** Indicate that sqlite3ParserFree() will never be called with a null
68 ** pointer.
70 #define YYPARSEFREENEVERNULL 1
73 ** In the amalgamation, the parse.c file generated by lemon and the
74 ** tokenize.c file are concatenated. In that case, sqlite3RunParser()
75 ** has access to the the size of the yyParser object and so the parser
76 ** engine can be allocated from stack. In that case, only the
77 ** sqlite3ParserInit() and sqlite3ParserFinalize() routines are invoked
78 ** and the sqlite3ParserAlloc() and sqlite3ParserFree() routines can be
79 ** omitted.
81 #ifdef SQLITE_AMALGAMATION
82 # define sqlite3Parser_ENGINEALWAYSONSTACK 1
83 #endif
86 ** Alternative datatype for the argument to the malloc() routine passed
87 ** into sqlite3ParserAlloc(). The default is size_t.
89 #define YYMALLOCARGTYPE u64
92 ** An instance of the following structure describes the event of a
93 ** TRIGGER. "a" is the event type, one of TK_UPDATE, TK_INSERT,
94 ** TK_DELETE, or TK_INSTEAD. If the event is of the form
96 ** UPDATE ON (a,b,c)
98 ** Then the "b" IdList records the list "a,b,c".
100 struct TrigEvent { int a; IdList * b; };
102 struct FrameBound { int eType; Expr *pExpr; };
105 ** Disable lookaside memory allocation for objects that might be
106 ** shared across database connections.
108 static void disableLookaside(Parse *pParse){
109 pParse->disableLookaside++;
110 pParse->db->lookaside.bDisable++;
113 } // end %include
115 // Input is a single SQL command
116 input ::= cmdlist.
117 cmdlist ::= cmdlist ecmd.
118 cmdlist ::= ecmd.
119 ecmd ::= SEMI.
120 ecmd ::= cmdx SEMI.
121 %ifndef SQLITE_OMIT_EXPLAIN
122 ecmd ::= explain cmdx.
123 explain ::= EXPLAIN. { pParse->explain = 1; }
124 explain ::= EXPLAIN QUERY PLAN. { pParse->explain = 2; }
125 %endif SQLITE_OMIT_EXPLAIN
126 cmdx ::= cmd. { sqlite3FinishCoding(pParse); }
128 ///////////////////// Begin and end transactions. ////////////////////////////
131 cmd ::= BEGIN transtype(Y) trans_opt. {sqlite3BeginTransaction(pParse, Y);}
132 trans_opt ::= .
133 trans_opt ::= TRANSACTION.
134 trans_opt ::= TRANSACTION nm.
135 %type transtype {int}
136 transtype(A) ::= . {A = TK_DEFERRED;}
137 transtype(A) ::= DEFERRED(X). {A = @X; /*A-overwrites-X*/}
138 transtype(A) ::= IMMEDIATE(X). {A = @X; /*A-overwrites-X*/}
139 transtype(A) ::= EXCLUSIVE(X). {A = @X; /*A-overwrites-X*/}
140 cmd ::= COMMIT|END(X) trans_opt. {sqlite3EndTransaction(pParse,@X);}
141 cmd ::= ROLLBACK(X) trans_opt. {sqlite3EndTransaction(pParse,@X);}
143 savepoint_opt ::= SAVEPOINT.
144 savepoint_opt ::= .
145 cmd ::= SAVEPOINT nm(X). {
146 sqlite3Savepoint(pParse, SAVEPOINT_BEGIN, &X);
148 cmd ::= RELEASE savepoint_opt nm(X). {
149 sqlite3Savepoint(pParse, SAVEPOINT_RELEASE, &X);
151 cmd ::= ROLLBACK trans_opt TO savepoint_opt nm(X). {
152 sqlite3Savepoint(pParse, SAVEPOINT_ROLLBACK, &X);
155 ///////////////////// The CREATE TABLE statement ////////////////////////////
157 cmd ::= create_table create_table_args.
158 create_table ::= createkw temp(T) TABLE ifnotexists(E) nm(Y) dbnm(Z). {
159 sqlite3StartTable(pParse,&Y,&Z,T,0,0,E);
161 createkw(A) ::= CREATE(A). {disableLookaside(pParse);}
163 %type ifnotexists {int}
164 ifnotexists(A) ::= . {A = 0;}
165 ifnotexists(A) ::= IF NOT EXISTS. {A = 1;}
166 %type temp {int}
167 %ifndef SQLITE_OMIT_TEMPDB
168 temp(A) ::= TEMP. {A = 1;}
169 %endif SQLITE_OMIT_TEMPDB
170 temp(A) ::= . {A = 0;}
171 create_table_args ::= LP columnlist conslist_opt(X) RP(E) table_options(F). {
172 sqlite3EndTable(pParse,&X,&E,F,0);
174 create_table_args ::= AS select(S). {
175 sqlite3EndTable(pParse,0,0,0,S);
176 sqlite3SelectDelete(pParse->db, S);
178 %type table_options {int}
179 table_options(A) ::= . {A = 0;}
180 table_options(A) ::= WITHOUT nm(X). {
181 if( X.n==5 && sqlite3_strnicmp(X.z,"rowid",5)==0 ){
182 A = TF_WithoutRowid | TF_NoVisibleRowid;
183 }else{
184 A = 0;
185 sqlite3ErrorMsg(pParse, "unknown table option: %.*s", X.n, X.z);
188 columnlist ::= columnlist COMMA columnname carglist.
189 columnlist ::= columnname carglist.
190 columnname(A) ::= nm(A) typetoken(Y). {sqlite3AddColumn(pParse,&A,&Y);}
192 // Declare some tokens early in order to influence their values, to
193 // improve performance and reduce the executable size. The goal here is
194 // to get the "jump" operations in ISNULL through ESCAPE to have numeric
195 // values that are early enough so that all jump operations are clustered
196 // at the beginning, but also so that the comparison tokens NE through GE
197 // are as large as possible so that they are near to FUNCTION, which is a
198 // token synthesized by addopcodes.tcl.
200 %token ABORT ACTION AFTER ANALYZE ASC ATTACH BEFORE BEGIN BY CASCADE CAST.
201 %token CONFLICT DATABASE DEFERRED DESC DETACH EACH END EXCLUSIVE EXPLAIN FAIL.
202 %token OR AND NOT IS MATCH LIKE_KW BETWEEN IN ISNULL NOTNULL NE EQ.
203 %token GT LE LT GE ESCAPE.
205 // The following directive causes tokens ABORT, AFTER, ASC, etc. to
206 // fallback to ID if they will not parse as their original value.
207 // This obviates the need for the "id" nonterminal.
209 %fallback ID
210 ABORT ACTION AFTER ANALYZE ASC ATTACH BEFORE BEGIN BY CASCADE CAST COLUMNKW
211 CONFLICT DATABASE DEFERRED DESC DETACH DO
212 EACH END EXCLUSIVE EXPLAIN FAIL FOR
213 IGNORE IMMEDIATE INITIALLY INSTEAD LIKE_KW MATCH NO PLAN
214 QUERY KEY OF OFFSET PRAGMA RAISE RECURSIVE RELEASE REPLACE RESTRICT ROW ROWS
215 ROLLBACK SAVEPOINT TEMP TRIGGER VACUUM VIEW VIRTUAL WITH WITHOUT
216 %ifdef SQLITE_OMIT_COMPOUND_SELECT
217 EXCEPT INTERSECT UNION
218 %endif SQLITE_OMIT_COMPOUND_SELECT
219 REINDEX RENAME CTIME_KW IF
221 %wildcard ANY.
223 // Define operator precedence early so that this is the first occurrence
224 // of the operator tokens in the grammer. Keeping the operators together
225 // causes them to be assigned integer values that are close together,
226 // which keeps parser tables smaller.
228 // The token values assigned to these symbols is determined by the order
229 // in which lemon first sees them. It must be the case that ISNULL/NOTNULL,
230 // NE/EQ, GT/LE, and GE/LT are separated by only a single value. See
231 // the sqlite3ExprIfFalse() routine for additional information on this
232 // constraint.
234 %left OR.
235 %left AND.
236 %right NOT.
237 %left IS MATCH LIKE_KW BETWEEN IN ISNULL NOTNULL NE EQ.
238 %left GT LE LT GE.
239 %right ESCAPE.
240 %left BITAND BITOR LSHIFT RSHIFT.
241 %left PLUS MINUS.
242 %left STAR SLASH REM.
243 %left CONCAT.
244 %left COLLATE.
245 %right BITNOT.
246 %nonassoc ON.
248 // An IDENTIFIER can be a generic identifier, or one of several
249 // keywords. Any non-standard keyword can also be an identifier.
251 %token_class id ID|INDEXED.
254 // And "ids" is an identifer-or-string.
256 %token_class ids ID|STRING.
258 // The name of a column or table can be any of the following:
260 %type nm {Token}
261 nm(A) ::= id(A).
262 nm(A) ::= STRING(A).
263 nm(A) ::= JOIN_KW(A).
265 // A typetoken is really zero or more tokens that form a type name such
266 // as can be found after the column name in a CREATE TABLE statement.
267 // Multiple tokens are concatenated to form the value of the typetoken.
269 %type typetoken {Token}
270 typetoken(A) ::= . {A.n = 0; A.z = 0;}
271 typetoken(A) ::= typename(A).
272 typetoken(A) ::= typename(A) LP signed RP(Y). {
273 A.n = (int)(&Y.z[Y.n] - A.z);
275 typetoken(A) ::= typename(A) LP signed COMMA signed RP(Y). {
276 A.n = (int)(&Y.z[Y.n] - A.z);
278 %type typename {Token}
279 typename(A) ::= ids(A).
280 typename(A) ::= typename(A) ids(Y). {A.n=Y.n+(int)(Y.z-A.z);}
281 signed ::= plus_num.
282 signed ::= minus_num.
284 // The scanpt non-terminal takes a value which is a pointer to the
285 // input text just past the last token that has been shifted into
286 // the parser. By surrounding some phrase in the grammar with two
287 // scanpt non-terminals, we can capture the input text for that phrase.
288 // For example:
290 // something ::= .... scanpt(A) phrase scanpt(Z).
292 // The text that is parsed as "phrase" is a string starting at A
293 // and containing (int)(Z-A) characters. There might be some extra
294 // whitespace on either end of the text, but that can be removed in
295 // post-processing, if needed.
297 %type scanpt {const char*}
298 scanpt(A) ::= . {
299 assert( yyLookahead!=YYNOCODE );
300 A = yyLookaheadToken.z;
303 // "carglist" is a list of additional constraints that come after the
304 // column name and column type in a CREATE TABLE statement.
306 carglist ::= carglist ccons.
307 carglist ::= .
308 ccons ::= CONSTRAINT nm(X). {pParse->constraintName = X;}
309 ccons ::= DEFAULT scanpt(A) term(X) scanpt(Z).
310 {sqlite3AddDefaultValue(pParse,X,A,Z);}
311 ccons ::= DEFAULT LP(A) expr(X) RP(Z).
312 {sqlite3AddDefaultValue(pParse,X,A.z+1,Z.z);}
313 ccons ::= DEFAULT PLUS(A) term(X) scanpt(Z).
314 {sqlite3AddDefaultValue(pParse,X,A.z,Z);}
315 ccons ::= DEFAULT MINUS(A) term(X) scanpt(Z). {
316 Expr *p = sqlite3PExpr(pParse, TK_UMINUS, X, 0);
317 sqlite3AddDefaultValue(pParse,p,A.z,Z);
319 ccons ::= DEFAULT scanpt id(X). {
320 Expr *p = tokenExpr(pParse, TK_STRING, X);
321 if( p ){
322 sqlite3ExprIdToTrueFalse(p);
323 testcase( p->op==TK_TRUEFALSE && sqlite3ExprTruthValue(p) );
325 sqlite3AddDefaultValue(pParse,p,X.z,X.z+X.n);
328 // In addition to the type name, we also care about the primary key and
329 // UNIQUE constraints.
331 ccons ::= NULL onconf.
332 ccons ::= NOT NULL onconf(R). {sqlite3AddNotNull(pParse, R);}
333 ccons ::= PRIMARY KEY sortorder(Z) onconf(R) autoinc(I).
334 {sqlite3AddPrimaryKey(pParse,0,R,I,Z);}
335 ccons ::= UNIQUE onconf(R). {sqlite3CreateIndex(pParse,0,0,0,0,R,0,0,0,0,
336 SQLITE_IDXTYPE_UNIQUE);}
337 ccons ::= CHECK LP expr(X) RP. {sqlite3AddCheckConstraint(pParse,X);}
338 ccons ::= REFERENCES nm(T) eidlist_opt(TA) refargs(R).
339 {sqlite3CreateForeignKey(pParse,0,&T,TA,R);}
340 ccons ::= defer_subclause(D). {sqlite3DeferForeignKey(pParse,D);}
341 ccons ::= COLLATE ids(C). {sqlite3AddCollateType(pParse, &C);}
343 // The optional AUTOINCREMENT keyword
344 %type autoinc {int}
345 autoinc(X) ::= . {X = 0;}
346 autoinc(X) ::= AUTOINCR. {X = 1;}
348 // The next group of rules parses the arguments to a REFERENCES clause
349 // that determine if the referential integrity checking is deferred or
350 // or immediate and which determine what action to take if a ref-integ
351 // check fails.
353 %type refargs {int}
354 refargs(A) ::= . { A = OE_None*0x0101; /* EV: R-19803-45884 */}
355 refargs(A) ::= refargs(A) refarg(Y). { A = (A & ~Y.mask) | Y.value; }
356 %type refarg {struct {int value; int mask;}}
357 refarg(A) ::= MATCH nm. { A.value = 0; A.mask = 0x000000; }
358 refarg(A) ::= ON INSERT refact. { A.value = 0; A.mask = 0x000000; }
359 refarg(A) ::= ON DELETE refact(X). { A.value = X; A.mask = 0x0000ff; }
360 refarg(A) ::= ON UPDATE refact(X). { A.value = X<<8; A.mask = 0x00ff00; }
361 %type refact {int}
362 refact(A) ::= SET NULL. { A = OE_SetNull; /* EV: R-33326-45252 */}
363 refact(A) ::= SET DEFAULT. { A = OE_SetDflt; /* EV: R-33326-45252 */}
364 refact(A) ::= CASCADE. { A = OE_Cascade; /* EV: R-33326-45252 */}
365 refact(A) ::= RESTRICT. { A = OE_Restrict; /* EV: R-33326-45252 */}
366 refact(A) ::= NO ACTION. { A = OE_None; /* EV: R-33326-45252 */}
367 %type defer_subclause {int}
368 defer_subclause(A) ::= NOT DEFERRABLE init_deferred_pred_opt. {A = 0;}
369 defer_subclause(A) ::= DEFERRABLE init_deferred_pred_opt(X). {A = X;}
370 %type init_deferred_pred_opt {int}
371 init_deferred_pred_opt(A) ::= . {A = 0;}
372 init_deferred_pred_opt(A) ::= INITIALLY DEFERRED. {A = 1;}
373 init_deferred_pred_opt(A) ::= INITIALLY IMMEDIATE. {A = 0;}
375 conslist_opt(A) ::= . {A.n = 0; A.z = 0;}
376 conslist_opt(A) ::= COMMA(A) conslist.
377 conslist ::= conslist tconscomma tcons.
378 conslist ::= tcons.
379 tconscomma ::= COMMA. {pParse->constraintName.n = 0;}
380 tconscomma ::= .
381 tcons ::= CONSTRAINT nm(X). {pParse->constraintName = X;}
382 tcons ::= PRIMARY KEY LP sortlist(X) autoinc(I) RP onconf(R).
383 {sqlite3AddPrimaryKey(pParse,X,R,I,0);}
384 tcons ::= UNIQUE LP sortlist(X) RP onconf(R).
385 {sqlite3CreateIndex(pParse,0,0,0,X,R,0,0,0,0,
386 SQLITE_IDXTYPE_UNIQUE);}
387 tcons ::= CHECK LP expr(E) RP onconf.
388 {sqlite3AddCheckConstraint(pParse,E);}
389 tcons ::= FOREIGN KEY LP eidlist(FA) RP
390 REFERENCES nm(T) eidlist_opt(TA) refargs(R) defer_subclause_opt(D). {
391 sqlite3CreateForeignKey(pParse, FA, &T, TA, R);
392 sqlite3DeferForeignKey(pParse, D);
394 %type defer_subclause_opt {int}
395 defer_subclause_opt(A) ::= . {A = 0;}
396 defer_subclause_opt(A) ::= defer_subclause(A).
398 // The following is a non-standard extension that allows us to declare the
399 // default behavior when there is a constraint conflict.
401 %type onconf {int}
402 %type orconf {int}
403 %type resolvetype {int}
404 onconf(A) ::= . {A = OE_Default;}
405 onconf(A) ::= ON CONFLICT resolvetype(X). {A = X;}
406 orconf(A) ::= . {A = OE_Default;}
407 orconf(A) ::= OR resolvetype(X). {A = X;}
408 resolvetype(A) ::= raisetype(A).
409 resolvetype(A) ::= IGNORE. {A = OE_Ignore;}
410 resolvetype(A) ::= REPLACE. {A = OE_Replace;}
412 ////////////////////////// The DROP TABLE /////////////////////////////////////
414 cmd ::= DROP TABLE ifexists(E) fullname(X). {
415 sqlite3DropTable(pParse, X, 0, E);
417 %type ifexists {int}
418 ifexists(A) ::= IF EXISTS. {A = 1;}
419 ifexists(A) ::= . {A = 0;}
421 ///////////////////// The CREATE VIEW statement /////////////////////////////
423 %ifndef SQLITE_OMIT_VIEW
424 cmd ::= createkw(X) temp(T) VIEW ifnotexists(E) nm(Y) dbnm(Z) eidlist_opt(C)
425 AS select(S). {
426 sqlite3CreateView(pParse, &X, &Y, &Z, C, S, T, E);
428 cmd ::= DROP VIEW ifexists(E) fullname(X). {
429 sqlite3DropTable(pParse, X, 1, E);
431 %endif SQLITE_OMIT_VIEW
433 //////////////////////// The SELECT statement /////////////////////////////////
435 cmd ::= select(X). {
436 SelectDest dest = {SRT_Output, 0, 0, 0, 0, 0};
437 sqlite3Select(pParse, X, &dest);
438 sqlite3SelectDelete(pParse->db, X);
441 %type select {Select*}
442 %destructor select {sqlite3SelectDelete(pParse->db, $$);}
443 %type selectnowith {Select*}
444 %destructor selectnowith {sqlite3SelectDelete(pParse->db, $$);}
445 %type oneselect {Select*}
446 %destructor oneselect {sqlite3SelectDelete(pParse->db, $$);}
448 %include {
450 ** For a compound SELECT statement, make sure p->pPrior->pNext==p for
451 ** all elements in the list. And make sure list length does not exceed
452 ** SQLITE_LIMIT_COMPOUND_SELECT.
454 static void parserDoubleLinkSelect(Parse *pParse, Select *p){
455 if( p->pPrior ){
456 Select *pNext = 0, *pLoop;
457 int mxSelect, cnt = 0;
458 for(pLoop=p; pLoop; pNext=pLoop, pLoop=pLoop->pPrior, cnt++){
459 pLoop->pNext = pNext;
460 pLoop->selFlags |= SF_Compound;
462 if( (p->selFlags & SF_MultiValue)==0 &&
463 (mxSelect = pParse->db->aLimit[SQLITE_LIMIT_COMPOUND_SELECT])>0 &&
464 cnt>mxSelect
466 sqlite3ErrorMsg(pParse, "too many terms in compound SELECT");
472 %ifndef SQLITE_OMIT_CTE
473 select(A) ::= WITH wqlist(W) selectnowith(X). {
474 Select *p = X;
475 if( p ){
476 p->pWith = W;
477 parserDoubleLinkSelect(pParse, p);
478 }else{
479 sqlite3WithDelete(pParse->db, W);
481 A = p;
483 select(A) ::= WITH RECURSIVE wqlist(W) selectnowith(X). {
484 Select *p = X;
485 if( p ){
486 p->pWith = W;
487 parserDoubleLinkSelect(pParse, p);
488 }else{
489 sqlite3WithDelete(pParse->db, W);
491 A = p;
493 %endif /* SQLITE_OMIT_CTE */
494 select(A) ::= selectnowith(X). {
495 Select *p = X;
496 if( p ){
497 parserDoubleLinkSelect(pParse, p);
499 A = p; /*A-overwrites-X*/
502 selectnowith(A) ::= oneselect(A).
503 %ifndef SQLITE_OMIT_COMPOUND_SELECT
504 selectnowith(A) ::= selectnowith(A) multiselect_op(Y) oneselect(Z). {
505 Select *pRhs = Z;
506 Select *pLhs = A;
507 if( pRhs && pRhs->pPrior ){
508 SrcList *pFrom;
509 Token x;
510 x.n = 0;
511 parserDoubleLinkSelect(pParse, pRhs);
512 pFrom = sqlite3SrcListAppendFromTerm(pParse,0,0,0,&x,pRhs,0,0);
513 pRhs = sqlite3SelectNew(pParse,0,pFrom,0,0,0,0,0,0);
515 if( pRhs ){
516 pRhs->op = (u8)Y;
517 pRhs->pPrior = pLhs;
518 if( ALWAYS(pLhs) ) pLhs->selFlags &= ~SF_MultiValue;
519 pRhs->selFlags &= ~SF_MultiValue;
520 if( Y!=TK_ALL ) pParse->hasCompound = 1;
521 }else{
522 sqlite3SelectDelete(pParse->db, pLhs);
524 A = pRhs;
526 %type multiselect_op {int}
527 multiselect_op(A) ::= UNION(OP). {A = @OP; /*A-overwrites-OP*/}
528 multiselect_op(A) ::= UNION ALL. {A = TK_ALL;}
529 multiselect_op(A) ::= EXCEPT|INTERSECT(OP). {A = @OP; /*A-overwrites-OP*/}
530 %endif SQLITE_OMIT_COMPOUND_SELECT
531 oneselect(A) ::= SELECT(S) distinct(D) selcollist(W) from(X) where_opt(Y)
532 groupby_opt(P) having_opt(Q)
533 %ifndef SQLITE_OMIT_WINDOWFUNC
534 windowdefn_opt(R)
535 %endif
536 orderby_opt(Z) limit_opt(L). {
537 #if SELECTTRACE_ENABLED
538 Token s = S; /*A-overwrites-S*/
539 #endif
540 A = sqlite3SelectNew(pParse,W,X,Y,P,Q,Z,D,L);
541 #ifndef SQLITE_OMIT_WINDOWFUNC
542 if( A ){
543 A->pWinDefn = R;
544 }else{
545 sqlite3WindowListDelete(pParse->db, R);
547 #endif // SQLITE_OMIT_WINDOWFUNC
548 #if SELECTTRACE_ENABLED
549 /* Populate the Select.zSelName[] string that is used to help with
550 ** query planner debugging, to differentiate between multiple Select
551 ** objects in a complex query.
553 ** If the SELECT keyword is immediately followed by a C-style comment
554 ** then extract the first few alphanumeric characters from within that
555 ** comment to be the zSelName value. Otherwise, the label is #N where
556 ** is an integer that is incremented with each SELECT statement seen.
558 if( A!=0 ){
559 const char *z = s.z+6;
560 int i;
561 sqlite3_snprintf(sizeof(A->zSelName), A->zSelName,"#%d",++pParse->nSelect);
562 while( z[0]==' ' ) z++;
563 if( z[0]=='/' && z[1]=='*' ){
564 z += 2;
565 while( z[0]==' ' ) z++;
566 for(i=0; sqlite3Isalnum(z[i]); i++){}
567 sqlite3_snprintf(sizeof(A->zSelName), A->zSelName, "%.*s", i, z);
570 #endif /* SELECTRACE_ENABLED */
572 oneselect(A) ::= values(A).
574 %type values {Select*}
575 %destructor values {sqlite3SelectDelete(pParse->db, $$);}
576 values(A) ::= VALUES LP nexprlist(X) RP. {
577 A = sqlite3SelectNew(pParse,X,0,0,0,0,0,SF_Values,0);
579 values(A) ::= values(A) COMMA LP exprlist(Y) RP. {
580 Select *pRight, *pLeft = A;
581 pRight = sqlite3SelectNew(pParse,Y,0,0,0,0,0,SF_Values|SF_MultiValue,0);
582 if( ALWAYS(pLeft) ) pLeft->selFlags &= ~SF_MultiValue;
583 if( pRight ){
584 pRight->op = TK_ALL;
585 pRight->pPrior = pLeft;
586 A = pRight;
587 }else{
588 A = pLeft;
592 // The "distinct" nonterminal is true (1) if the DISTINCT keyword is
593 // present and false (0) if it is not.
595 %type distinct {int}
596 distinct(A) ::= DISTINCT. {A = SF_Distinct;}
597 distinct(A) ::= ALL. {A = SF_All;}
598 distinct(A) ::= . {A = 0;}
600 // selcollist is a list of expressions that are to become the return
601 // values of the SELECT statement. The "*" in statements like
602 // "SELECT * FROM ..." is encoded as a special expression with an
603 // opcode of TK_ASTERISK.
605 %type selcollist {ExprList*}
606 %destructor selcollist {sqlite3ExprListDelete(pParse->db, $$);}
607 %type sclp {ExprList*}
608 %destructor sclp {sqlite3ExprListDelete(pParse->db, $$);}
609 sclp(A) ::= selcollist(A) COMMA.
610 sclp(A) ::= . {A = 0;}
611 selcollist(A) ::= sclp(A) scanpt(B) expr(X) scanpt(Z) as(Y). {
612 A = sqlite3ExprListAppend(pParse, A, X);
613 if( Y.n>0 ) sqlite3ExprListSetName(pParse, A, &Y, 1);
614 sqlite3ExprListSetSpan(pParse,A,B,Z);
616 selcollist(A) ::= sclp(A) scanpt STAR. {
617 Expr *p = sqlite3Expr(pParse->db, TK_ASTERISK, 0);
618 A = sqlite3ExprListAppend(pParse, A, p);
620 selcollist(A) ::= sclp(A) scanpt nm(X) DOT STAR. {
621 Expr *pRight = sqlite3PExpr(pParse, TK_ASTERISK, 0, 0);
622 Expr *pLeft = sqlite3ExprAlloc(pParse->db, TK_ID, &X, 1);
623 Expr *pDot = sqlite3PExpr(pParse, TK_DOT, pLeft, pRight);
624 A = sqlite3ExprListAppend(pParse,A, pDot);
627 // An option "AS <id>" phrase that can follow one of the expressions that
628 // define the result set, or one of the tables in the FROM clause.
630 %type as {Token}
631 as(X) ::= AS nm(Y). {X = Y;}
632 as(X) ::= ids(X).
633 as(X) ::= . {X.n = 0; X.z = 0;}
636 %type seltablist {SrcList*}
637 %destructor seltablist {sqlite3SrcListDelete(pParse->db, $$);}
638 %type stl_prefix {SrcList*}
639 %destructor stl_prefix {sqlite3SrcListDelete(pParse->db, $$);}
640 %type from {SrcList*}
641 %destructor from {sqlite3SrcListDelete(pParse->db, $$);}
643 // A complete FROM clause.
645 from(A) ::= . {A = sqlite3DbMallocZero(pParse->db, sizeof(*A));}
646 from(A) ::= FROM seltablist(X). {
647 A = X;
648 sqlite3SrcListShiftJoinType(A);
651 // "seltablist" is a "Select Table List" - the content of the FROM clause
652 // in a SELECT statement. "stl_prefix" is a prefix of this list.
654 stl_prefix(A) ::= seltablist(A) joinop(Y). {
655 if( ALWAYS(A && A->nSrc>0) ) A->a[A->nSrc-1].fg.jointype = (u8)Y;
657 stl_prefix(A) ::= . {A = 0;}
658 seltablist(A) ::= stl_prefix(A) nm(Y) dbnm(D) as(Z) indexed_opt(I)
659 on_opt(N) using_opt(U). {
660 A = sqlite3SrcListAppendFromTerm(pParse,A,&Y,&D,&Z,0,N,U);
661 sqlite3SrcListIndexedBy(pParse, A, &I);
663 seltablist(A) ::= stl_prefix(A) nm(Y) dbnm(D) LP exprlist(E) RP as(Z)
664 on_opt(N) using_opt(U). {
665 A = sqlite3SrcListAppendFromTerm(pParse,A,&Y,&D,&Z,0,N,U);
666 sqlite3SrcListFuncArgs(pParse, A, E);
668 %ifndef SQLITE_OMIT_SUBQUERY
669 seltablist(A) ::= stl_prefix(A) LP select(S) RP
670 as(Z) on_opt(N) using_opt(U). {
671 A = sqlite3SrcListAppendFromTerm(pParse,A,0,0,&Z,S,N,U);
673 seltablist(A) ::= stl_prefix(A) LP seltablist(F) RP
674 as(Z) on_opt(N) using_opt(U). {
675 if( A==0 && Z.n==0 && N==0 && U==0 ){
676 A = F;
677 }else if( F->nSrc==1 ){
678 A = sqlite3SrcListAppendFromTerm(pParse,A,0,0,&Z,0,N,U);
679 if( A ){
680 struct SrcList_item *pNew = &A->a[A->nSrc-1];
681 struct SrcList_item *pOld = F->a;
682 pNew->zName = pOld->zName;
683 pNew->zDatabase = pOld->zDatabase;
684 pNew->pSelect = pOld->pSelect;
685 pOld->zName = pOld->zDatabase = 0;
686 pOld->pSelect = 0;
688 sqlite3SrcListDelete(pParse->db, F);
689 }else{
690 Select *pSubquery;
691 sqlite3SrcListShiftJoinType(F);
692 pSubquery = sqlite3SelectNew(pParse,0,F,0,0,0,0,SF_NestedFrom,0);
693 A = sqlite3SrcListAppendFromTerm(pParse,A,0,0,&Z,pSubquery,N,U);
696 %endif SQLITE_OMIT_SUBQUERY
698 %type dbnm {Token}
699 dbnm(A) ::= . {A.z=0; A.n=0;}
700 dbnm(A) ::= DOT nm(X). {A = X;}
702 %type fullname {SrcList*}
703 %destructor fullname {sqlite3SrcListDelete(pParse->db, $$);}
704 fullname(A) ::= nm(X).
705 {A = sqlite3SrcListAppend(pParse->db,0,&X,0); /*A-overwrites-X*/}
706 fullname(A) ::= nm(X) DOT nm(Y).
707 {A = sqlite3SrcListAppend(pParse->db,0,&X,&Y); /*A-overwrites-X*/}
709 %type xfullname {SrcList*}
710 %destructor xfullname {sqlite3SrcListDelete(pParse->db, $$);}
711 xfullname(A) ::= nm(X).
712 {A = sqlite3SrcListAppend(pParse->db,0,&X,0); /*A-overwrites-X*/}
713 xfullname(A) ::= nm(X) DOT nm(Y).
714 {A = sqlite3SrcListAppend(pParse->db,0,&X,&Y); /*A-overwrites-X*/}
715 xfullname(A) ::= nm(X) DOT nm(Y) AS nm(Z). {
716 A = sqlite3SrcListAppend(pParse->db,0,&X,&Y); /*A-overwrites-X*/
717 if( A ) A->a[0].zAlias = sqlite3NameFromToken(pParse->db, &Z);
719 xfullname(A) ::= nm(X) AS nm(Z). {
720 A = sqlite3SrcListAppend(pParse->db,0,&X,0); /*A-overwrites-X*/
721 if( A ) A->a[0].zAlias = sqlite3NameFromToken(pParse->db, &Z);
724 %type joinop {int}
725 joinop(X) ::= COMMA|JOIN. { X = JT_INNER; }
726 joinop(X) ::= JOIN_KW(A) JOIN.
727 {X = sqlite3JoinType(pParse,&A,0,0); /*X-overwrites-A*/}
728 joinop(X) ::= JOIN_KW(A) nm(B) JOIN.
729 {X = sqlite3JoinType(pParse,&A,&B,0); /*X-overwrites-A*/}
730 joinop(X) ::= JOIN_KW(A) nm(B) nm(C) JOIN.
731 {X = sqlite3JoinType(pParse,&A,&B,&C);/*X-overwrites-A*/}
733 // There is a parsing abiguity in an upsert statement that uses a
734 // SELECT on the RHS of a the INSERT:
736 // INSERT INTO tab SELECT * FROM aaa JOIN bbb ON CONFLICT ...
737 // here ----^^
739 // When the ON token is encountered, the parser does not know if it is
740 // the beginning of an ON CONFLICT clause, or the beginning of an ON
741 // clause associated with the JOIN. The conflict is resolved in favor
742 // of the JOIN. If an ON CONFLICT clause is intended, insert a dummy
743 // WHERE clause in between, like this:
745 // INSERT INTO tab SELECT * FROM aaa JOIN bbb WHERE true ON CONFLICT ...
747 // The [AND] and [OR] precedence marks in the rules for on_opt cause the
748 // ON in this context to always be interpreted as belonging to the JOIN.
750 %type on_opt {Expr*}
751 %destructor on_opt {sqlite3ExprDelete(pParse->db, $$);}
752 on_opt(N) ::= ON expr(E). {N = E;}
753 on_opt(N) ::= . [OR] {N = 0;}
755 // Note that this block abuses the Token type just a little. If there is
756 // no "INDEXED BY" clause, the returned token is empty (z==0 && n==0). If
757 // there is an INDEXED BY clause, then the token is populated as per normal,
758 // with z pointing to the token data and n containing the number of bytes
759 // in the token.
761 // If there is a "NOT INDEXED" clause, then (z==0 && n==1), which is
762 // normally illegal. The sqlite3SrcListIndexedBy() function
763 // recognizes and interprets this as a special case.
765 %type indexed_opt {Token}
766 indexed_opt(A) ::= . {A.z=0; A.n=0;}
767 indexed_opt(A) ::= INDEXED BY nm(X). {A = X;}
768 indexed_opt(A) ::= NOT INDEXED. {A.z=0; A.n=1;}
770 %type using_opt {IdList*}
771 %destructor using_opt {sqlite3IdListDelete(pParse->db, $$);}
772 using_opt(U) ::= USING LP idlist(L) RP. {U = L;}
773 using_opt(U) ::= . {U = 0;}
776 %type orderby_opt {ExprList*}
777 %destructor orderby_opt {sqlite3ExprListDelete(pParse->db, $$);}
779 // the sortlist non-terminal stores a list of expression where each
780 // expression is optionally followed by ASC or DESC to indicate the
781 // sort order.
783 %type sortlist {ExprList*}
784 %destructor sortlist {sqlite3ExprListDelete(pParse->db, $$);}
786 orderby_opt(A) ::= . {A = 0;}
787 orderby_opt(A) ::= ORDER BY sortlist(X). {A = X;}
788 sortlist(A) ::= sortlist(A) COMMA expr(Y) sortorder(Z). {
789 A = sqlite3ExprListAppend(pParse,A,Y);
790 sqlite3ExprListSetSortOrder(A,Z);
792 sortlist(A) ::= expr(Y) sortorder(Z). {
793 A = sqlite3ExprListAppend(pParse,0,Y); /*A-overwrites-Y*/
794 sqlite3ExprListSetSortOrder(A,Z);
797 %type sortorder {int}
799 sortorder(A) ::= ASC. {A = SQLITE_SO_ASC;}
800 sortorder(A) ::= DESC. {A = SQLITE_SO_DESC;}
801 sortorder(A) ::= . {A = SQLITE_SO_UNDEFINED;}
803 %type groupby_opt {ExprList*}
804 %destructor groupby_opt {sqlite3ExprListDelete(pParse->db, $$);}
805 groupby_opt(A) ::= . {A = 0;}
806 groupby_opt(A) ::= GROUP BY nexprlist(X). {A = X;}
808 %type having_opt {Expr*}
809 %destructor having_opt {sqlite3ExprDelete(pParse->db, $$);}
810 having_opt(A) ::= . {A = 0;}
811 having_opt(A) ::= HAVING expr(X). {A = X;}
813 %type limit_opt {Expr*}
815 // The destructor for limit_opt will never fire in the current grammar.
816 // The limit_opt non-terminal only occurs at the end of a single production
817 // rule for SELECT statements. As soon as the rule that create the
818 // limit_opt non-terminal reduces, the SELECT statement rule will also
819 // reduce. So there is never a limit_opt non-terminal on the stack
820 // except as a transient. So there is never anything to destroy.
822 //%destructor limit_opt {sqlite3ExprDelete(pParse->db, $$);}
823 limit_opt(A) ::= . {A = 0;}
824 limit_opt(A) ::= LIMIT expr(X).
825 {A = sqlite3PExpr(pParse,TK_LIMIT,X,0);}
826 limit_opt(A) ::= LIMIT expr(X) OFFSET expr(Y).
827 {A = sqlite3PExpr(pParse,TK_LIMIT,X,Y);}
828 limit_opt(A) ::= LIMIT expr(X) COMMA expr(Y).
829 {A = sqlite3PExpr(pParse,TK_LIMIT,Y,X);}
831 /////////////////////////// The DELETE statement /////////////////////////////
833 %ifdef SQLITE_ENABLE_UPDATE_DELETE_LIMIT
834 cmd ::= with DELETE FROM xfullname(X) indexed_opt(I) where_opt(W)
835 orderby_opt(O) limit_opt(L). {
836 sqlite3SrcListIndexedBy(pParse, X, &I);
837 sqlite3DeleteFrom(pParse,X,W,O,L);
839 %endif
840 %ifndef SQLITE_ENABLE_UPDATE_DELETE_LIMIT
841 cmd ::= with DELETE FROM xfullname(X) indexed_opt(I) where_opt(W). {
842 sqlite3SrcListIndexedBy(pParse, X, &I);
843 sqlite3DeleteFrom(pParse,X,W,0,0);
845 %endif
847 %type where_opt {Expr*}
848 %destructor where_opt {sqlite3ExprDelete(pParse->db, $$);}
850 where_opt(A) ::= . {A = 0;}
851 where_opt(A) ::= WHERE expr(X). {A = X;}
853 ////////////////////////// The UPDATE command ////////////////////////////////
855 %ifdef SQLITE_ENABLE_UPDATE_DELETE_LIMIT
856 cmd ::= with UPDATE orconf(R) xfullname(X) indexed_opt(I) SET setlist(Y)
857 where_opt(W) orderby_opt(O) limit_opt(L). {
858 sqlite3SrcListIndexedBy(pParse, X, &I);
859 sqlite3ExprListCheckLength(pParse,Y,"set list");
860 sqlite3Update(pParse,X,Y,W,R,O,L,0);
862 %endif
863 %ifndef SQLITE_ENABLE_UPDATE_DELETE_LIMIT
864 cmd ::= with UPDATE orconf(R) xfullname(X) indexed_opt(I) SET setlist(Y)
865 where_opt(W). {
866 sqlite3SrcListIndexedBy(pParse, X, &I);
867 sqlite3ExprListCheckLength(pParse,Y,"set list");
868 sqlite3Update(pParse,X,Y,W,R,0,0,0);
870 %endif
872 %type setlist {ExprList*}
873 %destructor setlist {sqlite3ExprListDelete(pParse->db, $$);}
875 setlist(A) ::= setlist(A) COMMA nm(X) EQ expr(Y). {
876 A = sqlite3ExprListAppend(pParse, A, Y);
877 sqlite3ExprListSetName(pParse, A, &X, 1);
879 setlist(A) ::= setlist(A) COMMA LP idlist(X) RP EQ expr(Y). {
880 A = sqlite3ExprListAppendVector(pParse, A, X, Y);
882 setlist(A) ::= nm(X) EQ expr(Y). {
883 A = sqlite3ExprListAppend(pParse, 0, Y);
884 sqlite3ExprListSetName(pParse, A, &X, 1);
886 setlist(A) ::= LP idlist(X) RP EQ expr(Y). {
887 A = sqlite3ExprListAppendVector(pParse, 0, X, Y);
890 ////////////////////////// The INSERT command /////////////////////////////////
892 cmd ::= with insert_cmd(R) INTO xfullname(X) idlist_opt(F) select(S)
893 upsert(U). {
894 sqlite3Insert(pParse, X, S, F, R, U);
896 cmd ::= with insert_cmd(R) INTO xfullname(X) idlist_opt(F) DEFAULT VALUES.
898 sqlite3Insert(pParse, X, 0, F, R, 0);
901 %type upsert {Upsert*}
903 // Because upsert only occurs at the tip end of the INSERT rule for cmd,
904 // there is never a case where the value of the upsert pointer will not
905 // be destroyed by the cmd action. So comment-out the destructor to
906 // avoid unreachable code.
907 //%destructor upsert {sqlite3UpsertDelete(pParse->db,$$);}
908 upsert(A) ::= . { A = 0; }
909 upsert(A) ::= ON CONFLICT LP sortlist(T) RP where_opt(TW)
910 DO UPDATE SET setlist(Z) where_opt(W).
911 { A = sqlite3UpsertNew(pParse->db,T,TW,Z,W);}
912 upsert(A) ::= ON CONFLICT LP sortlist(T) RP where_opt(TW) DO NOTHING.
913 { A = sqlite3UpsertNew(pParse->db,T,TW,0,0); }
914 upsert(A) ::= ON CONFLICT DO NOTHING.
915 { A = sqlite3UpsertNew(pParse->db,0,0,0,0); }
917 %type insert_cmd {int}
918 insert_cmd(A) ::= INSERT orconf(R). {A = R;}
919 insert_cmd(A) ::= REPLACE. {A = OE_Replace;}
921 %type idlist_opt {IdList*}
922 %destructor idlist_opt {sqlite3IdListDelete(pParse->db, $$);}
923 %type idlist {IdList*}
924 %destructor idlist {sqlite3IdListDelete(pParse->db, $$);}
926 idlist_opt(A) ::= . {A = 0;}
927 idlist_opt(A) ::= LP idlist(X) RP. {A = X;}
928 idlist(A) ::= idlist(A) COMMA nm(Y).
929 {A = sqlite3IdListAppend(pParse->db,A,&Y);}
930 idlist(A) ::= nm(Y).
931 {A = sqlite3IdListAppend(pParse->db,0,&Y); /*A-overwrites-Y*/}
933 /////////////////////////// Expression Processing /////////////////////////////
936 %type expr {Expr*}
937 %destructor expr {sqlite3ExprDelete(pParse->db, $$);}
938 %type term {Expr*}
939 %destructor term {sqlite3ExprDelete(pParse->db, $$);}
941 %include {
943 /* Construct a new Expr object from a single identifier. Use the
944 ** new Expr to populate pOut. Set the span of pOut to be the identifier
945 ** that created the expression.
947 static Expr *tokenExpr(Parse *pParse, int op, Token t){
948 Expr *p = sqlite3DbMallocRawNN(pParse->db, sizeof(Expr)+t.n+1);
949 if( p ){
950 memset(p, 0, sizeof(Expr));
951 p->op = (u8)op;
952 p->flags = EP_Leaf;
953 p->iAgg = -1;
954 p->u.zToken = (char*)&p[1];
955 memcpy(p->u.zToken, t.z, t.n);
956 p->u.zToken[t.n] = 0;
957 if( sqlite3Isquote(p->u.zToken[0]) ){
958 if( p->u.zToken[0]=='"' ) p->flags |= EP_DblQuoted;
959 sqlite3Dequote(p->u.zToken);
961 #if SQLITE_MAX_EXPR_DEPTH>0
962 p->nHeight = 1;
963 #endif
965 return p;
969 expr(A) ::= term(A).
970 expr(A) ::= LP expr(X) RP. {A = X;}
971 expr(A) ::= id(X). {A=tokenExpr(pParse,TK_ID,X); /*A-overwrites-X*/}
972 expr(A) ::= JOIN_KW(X). {A=tokenExpr(pParse,TK_ID,X); /*A-overwrites-X*/}
973 expr(A) ::= nm(X) DOT nm(Y). {
974 Expr *temp1 = sqlite3ExprAlloc(pParse->db, TK_ID, &X, 1);
975 Expr *temp2 = sqlite3ExprAlloc(pParse->db, TK_ID, &Y, 1);
976 A = sqlite3PExpr(pParse, TK_DOT, temp1, temp2);
978 expr(A) ::= nm(X) DOT nm(Y) DOT nm(Z). {
979 Expr *temp1 = sqlite3ExprAlloc(pParse->db, TK_ID, &X, 1);
980 Expr *temp2 = sqlite3ExprAlloc(pParse->db, TK_ID, &Y, 1);
981 Expr *temp3 = sqlite3ExprAlloc(pParse->db, TK_ID, &Z, 1);
982 Expr *temp4 = sqlite3PExpr(pParse, TK_DOT, temp2, temp3);
983 A = sqlite3PExpr(pParse, TK_DOT, temp1, temp4);
985 term(A) ::= NULL|FLOAT|BLOB(X). {A=tokenExpr(pParse,@X,X); /*A-overwrites-X*/}
986 term(A) ::= STRING(X). {A=tokenExpr(pParse,@X,X); /*A-overwrites-X*/}
987 term(A) ::= INTEGER(X). {
988 A = sqlite3ExprAlloc(pParse->db, TK_INTEGER, &X, 1);
990 expr(A) ::= VARIABLE(X). {
991 if( !(X.z[0]=='#' && sqlite3Isdigit(X.z[1])) ){
992 u32 n = X.n;
993 A = tokenExpr(pParse, TK_VARIABLE, X);
994 sqlite3ExprAssignVarNumber(pParse, A, n);
995 }else{
996 /* When doing a nested parse, one can include terms in an expression
997 ** that look like this: #1 #2 ... These terms refer to registers
998 ** in the virtual machine. #N is the N-th register. */
999 Token t = X; /*A-overwrites-X*/
1000 assert( t.n>=2 );
1001 if( pParse->nested==0 ){
1002 sqlite3ErrorMsg(pParse, "near \"%T\": syntax error", &t);
1003 A = 0;
1004 }else{
1005 A = sqlite3PExpr(pParse, TK_REGISTER, 0, 0);
1006 if( A ) sqlite3GetInt32(&t.z[1], &A->iTable);
1010 expr(A) ::= expr(A) COLLATE ids(C). {
1011 A = sqlite3ExprAddCollateToken(pParse, A, &C, 1);
1013 %ifndef SQLITE_OMIT_CAST
1014 expr(A) ::= CAST LP expr(E) AS typetoken(T) RP. {
1015 A = sqlite3ExprAlloc(pParse->db, TK_CAST, &T, 1);
1016 sqlite3ExprAttachSubtrees(pParse->db, A, E, 0);
1018 %endif SQLITE_OMIT_CAST
1019 expr(A) ::= id(X) LP distinct(D) exprlist(Y) RP
1020 %ifndef SQLITE_OMIT_WINDOWFUNC
1021 over_opt(Z)
1022 %endif
1024 if( Y && Y->nExpr>pParse->db->aLimit[SQLITE_LIMIT_FUNCTION_ARG] ){
1025 sqlite3ErrorMsg(pParse, "too many arguments on function %T", &X);
1027 A = sqlite3ExprFunction(pParse, Y, &X);
1028 sqlite3WindowAttach(pParse, A, Z);
1029 if( D==SF_Distinct && A ){
1030 A->flags |= EP_Distinct;
1033 expr(A) ::= id(X) LP STAR RP
1034 %ifndef SQLITE_OMIT_WINDOWFUNC
1035 over_opt(Z)
1036 %endif
1038 A = sqlite3ExprFunction(pParse, 0, &X);
1039 sqlite3WindowAttach(pParse, A, Z);
1041 term(A) ::= CTIME_KW(OP). {
1042 A = sqlite3ExprFunction(pParse, 0, &OP);
1045 %ifndef SQLITE_OMIT_WINDOWFUNC
1047 %type windowdefn_opt {Window*}
1048 %destructor windowdefn_opt {sqlite3WindowDelete(pParse->db, $$);}
1049 windowdefn_opt(A) ::= . { A = 0; }
1050 windowdefn_opt(A) ::= WINDOW windowdefn_list(B). { A = B; }
1052 %type windowdefn_list {Window*}
1053 %destructor windowdefn_list {sqlite3WindowDelete(pParse->db, $$);}
1054 windowdefn_list(A) ::= windowdefn(Z). { A = Z; }
1055 windowdefn_list(A) ::= windowdefn_list(Y) COMMA windowdefn(Z). {
1056 if( Z ) Z->pNextWin = Y;
1057 A = Z;
1060 %type windowdefn {Window*}
1061 %destructor windowdefn {sqlite3WindowDelete(pParse->db, $$);}
1062 windowdefn(A) ::= nm(X) AS window(Y). {
1063 if( Y ){
1064 Y->zName = sqlite3DbStrNDup(pParse->db, X.z, X.n);
1066 A = Y;
1069 %type over_opt {Window*}
1070 %destructor over_opt {sqlite3WindowDelete(pParse->db, $$);}
1072 %type window {Window*}
1073 %destructor window {sqlite3WindowDelete(pParse->db, $$);}
1075 %type frame_opt {Window*}
1076 %destructor frame_opt {sqlite3WindowDelete(pParse->db, $$);}
1078 %type window_or_nm {Window*}
1079 %destructor window_or_nm {
1080 sqlite3WindowDelete(pParse->db, $$);}
1082 %type part_opt {ExprList*}
1083 %destructor part_opt {sqlite3ExprListDelete(pParse->db, $$);}
1085 %type filter_opt {Expr*}
1086 %destructor filter_opt {sqlite3ExprDelete(pParse->db, $$);}
1088 %type range_or_rows {int}
1090 %type frame_bound {struct FrameBound}
1091 %destructor frame_bound {sqlite3ExprDelete(pParse->db, $$.pExpr);}
1093 over_opt(A) ::= . { A = 0; }
1094 over_opt(A) ::= filter_opt(W) OVER window_or_nm(Z). {
1095 A = Z;
1096 if( A ) A->pFilter = W;
1099 window_or_nm(A) ::= window(Z). {A = Z;}
1100 window_or_nm(A) ::= nm(Z). {
1101 A = (Window*)sqlite3DbMallocZero(pParse->db, sizeof(Window));
1102 if( A ){
1103 A->zName = sqlite3DbStrNDup(pParse->db, Z.z, Z.n);
1107 window(A) ::= LP part_opt(X) orderby_opt(Y) frame_opt(Z) RP. {
1108 A = Z;
1109 if( A ){
1110 A->pPartition = X;
1111 A->pOrderBy = Y;
1115 part_opt(A) ::= PARTITION BY exprlist(X). { A = X; }
1116 part_opt(A) ::= . { A = 0; }
1117 filter_opt(A) ::= . { A = 0; }
1118 filter_opt(A) ::= FILTER LP WHERE expr(X) RP. { A = X; }
1120 frame_opt(A) ::= . {
1121 A = sqlite3WindowAlloc(pParse, TK_RANGE, TK_UNBOUNDED, 0, TK_CURRENT, 0);
1123 frame_opt(A) ::= range_or_rows(X) frame_bound(Y). {
1124 A = sqlite3WindowAlloc(pParse, X, Y.eType, Y.pExpr, TK_CURRENT, 0);
1126 frame_opt(A) ::= range_or_rows(X) BETWEEN frame_bound(Y) AND frame_bound(Z). {
1127 A = sqlite3WindowAlloc(pParse, X, Y.eType, Y.pExpr, Z.eType, Z.pExpr);
1130 range_or_rows(A) ::= RANGE. { A = TK_RANGE; }
1131 range_or_rows(A) ::= ROWS. { A = TK_ROWS; }
1133 frame_bound(A) ::= UNBOUNDED PRECEDING. { A.eType = TK_UNBOUNDED; A.pExpr = 0; }
1134 frame_bound(A) ::= expr(X) PRECEDING. { A.eType = TK_PRECEDING; A.pExpr = X; }
1135 frame_bound(A) ::= CURRENT ROW. { A.eType = TK_CURRENT ; A.pExpr = 0; }
1136 frame_bound(A) ::= expr(X) FOLLOWING. { A.eType = TK_FOLLOWING; A.pExpr = X; }
1137 frame_bound(A) ::= UNBOUNDED FOLLOWING. { A.eType = TK_UNBOUNDED; A.pExpr = 0; }
1139 %endif // SQLITE_OMIT_WINDOWFUNC
1141 expr(A) ::= LP nexprlist(X) COMMA expr(Y) RP. {
1142 ExprList *pList = sqlite3ExprListAppend(pParse, X, Y);
1143 A = sqlite3PExpr(pParse, TK_VECTOR, 0, 0);
1144 if( A ){
1145 A->x.pList = pList;
1146 }else{
1147 sqlite3ExprListDelete(pParse->db, pList);
1151 expr(A) ::= expr(A) AND(OP) expr(Y). {A=sqlite3PExpr(pParse,@OP,A,Y);}
1152 expr(A) ::= expr(A) OR(OP) expr(Y). {A=sqlite3PExpr(pParse,@OP,A,Y);}
1153 expr(A) ::= expr(A) LT|GT|GE|LE(OP) expr(Y).
1154 {A=sqlite3PExpr(pParse,@OP,A,Y);}
1155 expr(A) ::= expr(A) EQ|NE(OP) expr(Y). {A=sqlite3PExpr(pParse,@OP,A,Y);}
1156 expr(A) ::= expr(A) BITAND|BITOR|LSHIFT|RSHIFT(OP) expr(Y).
1157 {A=sqlite3PExpr(pParse,@OP,A,Y);}
1158 expr(A) ::= expr(A) PLUS|MINUS(OP) expr(Y).
1159 {A=sqlite3PExpr(pParse,@OP,A,Y);}
1160 expr(A) ::= expr(A) STAR|SLASH|REM(OP) expr(Y).
1161 {A=sqlite3PExpr(pParse,@OP,A,Y);}
1162 expr(A) ::= expr(A) CONCAT(OP) expr(Y). {A=sqlite3PExpr(pParse,@OP,A,Y);}
1163 %type likeop {Token}
1164 likeop(A) ::= LIKE_KW|MATCH(A).
1165 likeop(A) ::= NOT LIKE_KW|MATCH(X). {A=X; A.n|=0x80000000; /*A-overwrite-X*/}
1166 expr(A) ::= expr(A) likeop(OP) expr(Y). [LIKE_KW] {
1167 ExprList *pList;
1168 int bNot = OP.n & 0x80000000;
1169 OP.n &= 0x7fffffff;
1170 pList = sqlite3ExprListAppend(pParse,0, Y);
1171 pList = sqlite3ExprListAppend(pParse,pList, A);
1172 A = sqlite3ExprFunction(pParse, pList, &OP);
1173 if( bNot ) A = sqlite3PExpr(pParse, TK_NOT, A, 0);
1174 if( A ) A->flags |= EP_InfixFunc;
1176 expr(A) ::= expr(A) likeop(OP) expr(Y) ESCAPE expr(E). [LIKE_KW] {
1177 ExprList *pList;
1178 int bNot = OP.n & 0x80000000;
1179 OP.n &= 0x7fffffff;
1180 pList = sqlite3ExprListAppend(pParse,0, Y);
1181 pList = sqlite3ExprListAppend(pParse,pList, A);
1182 pList = sqlite3ExprListAppend(pParse,pList, E);
1183 A = sqlite3ExprFunction(pParse, pList, &OP);
1184 if( bNot ) A = sqlite3PExpr(pParse, TK_NOT, A, 0);
1185 if( A ) A->flags |= EP_InfixFunc;
1188 expr(A) ::= expr(A) ISNULL|NOTNULL(E). {A = sqlite3PExpr(pParse,@E,A,0);}
1189 expr(A) ::= expr(A) NOT NULL. {A = sqlite3PExpr(pParse,TK_NOTNULL,A,0);}
1191 %include {
1192 /* A routine to convert a binary TK_IS or TK_ISNOT expression into a
1193 ** unary TK_ISNULL or TK_NOTNULL expression. */
1194 static void binaryToUnaryIfNull(Parse *pParse, Expr *pY, Expr *pA, int op){
1195 sqlite3 *db = pParse->db;
1196 if( pA && pY && pY->op==TK_NULL ){
1197 pA->op = (u8)op;
1198 sqlite3ExprDelete(db, pA->pRight);
1199 pA->pRight = 0;
1204 // expr1 IS expr2
1205 // expr1 IS NOT expr2
1207 // If expr2 is NULL then code as TK_ISNULL or TK_NOTNULL. If expr2
1208 // is any other expression, code as TK_IS or TK_ISNOT.
1210 expr(A) ::= expr(A) IS expr(Y). {
1211 A = sqlite3PExpr(pParse,TK_IS,A,Y);
1212 binaryToUnaryIfNull(pParse, Y, A, TK_ISNULL);
1214 expr(A) ::= expr(A) IS NOT expr(Y). {
1215 A = sqlite3PExpr(pParse,TK_ISNOT,A,Y);
1216 binaryToUnaryIfNull(pParse, Y, A, TK_NOTNULL);
1219 expr(A) ::= NOT(B) expr(X).
1220 {A = sqlite3PExpr(pParse, @B, X, 0);/*A-overwrites-B*/}
1221 expr(A) ::= BITNOT(B) expr(X).
1222 {A = sqlite3PExpr(pParse, @B, X, 0);/*A-overwrites-B*/}
1223 expr(A) ::= PLUS|MINUS(B) expr(X). [BITNOT] {
1224 A = sqlite3PExpr(pParse, @B==TK_PLUS ? TK_UPLUS : TK_UMINUS, X, 0);
1225 /*A-overwrites-B*/
1228 %type between_op {int}
1229 between_op(A) ::= BETWEEN. {A = 0;}
1230 between_op(A) ::= NOT BETWEEN. {A = 1;}
1231 expr(A) ::= expr(A) between_op(N) expr(X) AND expr(Y). [BETWEEN] {
1232 ExprList *pList = sqlite3ExprListAppend(pParse,0, X);
1233 pList = sqlite3ExprListAppend(pParse,pList, Y);
1234 A = sqlite3PExpr(pParse, TK_BETWEEN, A, 0);
1235 if( A ){
1236 A->x.pList = pList;
1237 }else{
1238 sqlite3ExprListDelete(pParse->db, pList);
1240 if( N ) A = sqlite3PExpr(pParse, TK_NOT, A, 0);
1242 %ifndef SQLITE_OMIT_SUBQUERY
1243 %type in_op {int}
1244 in_op(A) ::= IN. {A = 0;}
1245 in_op(A) ::= NOT IN. {A = 1;}
1246 expr(A) ::= expr(A) in_op(N) LP exprlist(Y) RP. [IN] {
1247 if( Y==0 ){
1248 /* Expressions of the form
1250 ** expr1 IN ()
1251 ** expr1 NOT IN ()
1253 ** simplify to constants 0 (false) and 1 (true), respectively,
1254 ** regardless of the value of expr1.
1256 sqlite3ExprDelete(pParse->db, A);
1257 A = sqlite3ExprAlloc(pParse->db, TK_INTEGER,&sqlite3IntTokens[N],1);
1258 }else if( Y->nExpr==1 ){
1259 /* Expressions of the form:
1261 ** expr1 IN (?1)
1262 ** expr1 NOT IN (?2)
1264 ** with exactly one value on the RHS can be simplified to something
1265 ** like this:
1267 ** expr1 == ?1
1268 ** expr1 <> ?2
1270 ** But, the RHS of the == or <> is marked with the EP_Generic flag
1271 ** so that it may not contribute to the computation of comparison
1272 ** affinity or the collating sequence to use for comparison. Otherwise,
1273 ** the semantics would be subtly different from IN or NOT IN.
1275 Expr *pRHS = Y->a[0].pExpr;
1276 Y->a[0].pExpr = 0;
1277 sqlite3ExprListDelete(pParse->db, Y);
1278 /* pRHS cannot be NULL because a malloc error would have been detected
1279 ** before now and control would have never reached this point */
1280 if( ALWAYS(pRHS) ){
1281 pRHS->flags &= ~EP_Collate;
1282 pRHS->flags |= EP_Generic;
1284 A = sqlite3PExpr(pParse, N ? TK_NE : TK_EQ, A, pRHS);
1285 }else{
1286 A = sqlite3PExpr(pParse, TK_IN, A, 0);
1287 if( A ){
1288 A->x.pList = Y;
1289 sqlite3ExprSetHeightAndFlags(pParse, A);
1290 }else{
1291 sqlite3ExprListDelete(pParse->db, Y);
1293 if( N ) A = sqlite3PExpr(pParse, TK_NOT, A, 0);
1296 expr(A) ::= LP select(X) RP. {
1297 A = sqlite3PExpr(pParse, TK_SELECT, 0, 0);
1298 sqlite3PExprAddSelect(pParse, A, X);
1300 expr(A) ::= expr(A) in_op(N) LP select(Y) RP. [IN] {
1301 A = sqlite3PExpr(pParse, TK_IN, A, 0);
1302 sqlite3PExprAddSelect(pParse, A, Y);
1303 if( N ) A = sqlite3PExpr(pParse, TK_NOT, A, 0);
1305 expr(A) ::= expr(A) in_op(N) nm(Y) dbnm(Z) paren_exprlist(E). [IN] {
1306 SrcList *pSrc = sqlite3SrcListAppend(pParse->db, 0,&Y,&Z);
1307 Select *pSelect = sqlite3SelectNew(pParse, 0,pSrc,0,0,0,0,0,0);
1308 if( E ) sqlite3SrcListFuncArgs(pParse, pSelect ? pSrc : 0, E);
1309 A = sqlite3PExpr(pParse, TK_IN, A, 0);
1310 sqlite3PExprAddSelect(pParse, A, pSelect);
1311 if( N ) A = sqlite3PExpr(pParse, TK_NOT, A, 0);
1313 expr(A) ::= EXISTS LP select(Y) RP. {
1314 Expr *p;
1315 p = A = sqlite3PExpr(pParse, TK_EXISTS, 0, 0);
1316 sqlite3PExprAddSelect(pParse, p, Y);
1318 %endif SQLITE_OMIT_SUBQUERY
1320 /* CASE expressions */
1321 expr(A) ::= CASE case_operand(X) case_exprlist(Y) case_else(Z) END. {
1322 A = sqlite3PExpr(pParse, TK_CASE, X, 0);
1323 if( A ){
1324 A->x.pList = Z ? sqlite3ExprListAppend(pParse,Y,Z) : Y;
1325 sqlite3ExprSetHeightAndFlags(pParse, A);
1326 }else{
1327 sqlite3ExprListDelete(pParse->db, Y);
1328 sqlite3ExprDelete(pParse->db, Z);
1331 %type case_exprlist {ExprList*}
1332 %destructor case_exprlist {sqlite3ExprListDelete(pParse->db, $$);}
1333 case_exprlist(A) ::= case_exprlist(A) WHEN expr(Y) THEN expr(Z). {
1334 A = sqlite3ExprListAppend(pParse,A, Y);
1335 A = sqlite3ExprListAppend(pParse,A, Z);
1337 case_exprlist(A) ::= WHEN expr(Y) THEN expr(Z). {
1338 A = sqlite3ExprListAppend(pParse,0, Y);
1339 A = sqlite3ExprListAppend(pParse,A, Z);
1341 %type case_else {Expr*}
1342 %destructor case_else {sqlite3ExprDelete(pParse->db, $$);}
1343 case_else(A) ::= ELSE expr(X). {A = X;}
1344 case_else(A) ::= . {A = 0;}
1345 %type case_operand {Expr*}
1346 %destructor case_operand {sqlite3ExprDelete(pParse->db, $$);}
1347 case_operand(A) ::= expr(X). {A = X; /*A-overwrites-X*/}
1348 case_operand(A) ::= . {A = 0;}
1350 %type exprlist {ExprList*}
1351 %destructor exprlist {sqlite3ExprListDelete(pParse->db, $$);}
1352 %type nexprlist {ExprList*}
1353 %destructor nexprlist {sqlite3ExprListDelete(pParse->db, $$);}
1355 exprlist(A) ::= nexprlist(A).
1356 exprlist(A) ::= . {A = 0;}
1357 nexprlist(A) ::= nexprlist(A) COMMA expr(Y).
1358 {A = sqlite3ExprListAppend(pParse,A,Y);}
1359 nexprlist(A) ::= expr(Y).
1360 {A = sqlite3ExprListAppend(pParse,0,Y); /*A-overwrites-Y*/}
1362 %ifndef SQLITE_OMIT_SUBQUERY
1363 /* A paren_exprlist is an optional expression list contained inside
1364 ** of parenthesis */
1365 %type paren_exprlist {ExprList*}
1366 %destructor paren_exprlist {sqlite3ExprListDelete(pParse->db, $$);}
1367 paren_exprlist(A) ::= . {A = 0;}
1368 paren_exprlist(A) ::= LP exprlist(X) RP. {A = X;}
1369 %endif SQLITE_OMIT_SUBQUERY
1372 ///////////////////////////// The CREATE INDEX command ///////////////////////
1374 cmd ::= createkw(S) uniqueflag(U) INDEX ifnotexists(NE) nm(X) dbnm(D)
1375 ON nm(Y) LP sortlist(Z) RP where_opt(W). {
1376 sqlite3CreateIndex(pParse, &X, &D,
1377 sqlite3SrcListAppend(pParse->db,0,&Y,0), Z, U,
1378 &S, W, SQLITE_SO_ASC, NE, SQLITE_IDXTYPE_APPDEF);
1381 %type uniqueflag {int}
1382 uniqueflag(A) ::= UNIQUE. {A = OE_Abort;}
1383 uniqueflag(A) ::= . {A = OE_None;}
1386 // The eidlist non-terminal (Expression Id List) generates an ExprList
1387 // from a list of identifiers. The identifier names are in ExprList.a[].zName.
1388 // This list is stored in an ExprList rather than an IdList so that it
1389 // can be easily sent to sqlite3ColumnsExprList().
1391 // eidlist is grouped with CREATE INDEX because it used to be the non-terminal
1392 // used for the arguments to an index. That is just an historical accident.
1394 // IMPORTANT COMPATIBILITY NOTE: Some prior versions of SQLite accepted
1395 // COLLATE clauses and ASC or DESC keywords on ID lists in inappropriate
1396 // places - places that might have been stored in the sqlite_master schema.
1397 // Those extra features were ignored. But because they might be in some
1398 // (busted) old databases, we need to continue parsing them when loading
1399 // historical schemas.
1401 %type eidlist {ExprList*}
1402 %destructor eidlist {sqlite3ExprListDelete(pParse->db, $$);}
1403 %type eidlist_opt {ExprList*}
1404 %destructor eidlist_opt {sqlite3ExprListDelete(pParse->db, $$);}
1406 %include {
1407 /* Add a single new term to an ExprList that is used to store a
1408 ** list of identifiers. Report an error if the ID list contains
1409 ** a COLLATE clause or an ASC or DESC keyword, except ignore the
1410 ** error while parsing a legacy schema.
1412 static ExprList *parserAddExprIdListTerm(
1413 Parse *pParse,
1414 ExprList *pPrior,
1415 Token *pIdToken,
1416 int hasCollate,
1417 int sortOrder
1419 ExprList *p = sqlite3ExprListAppend(pParse, pPrior, 0);
1420 if( (hasCollate || sortOrder!=SQLITE_SO_UNDEFINED)
1421 && pParse->db->init.busy==0
1423 sqlite3ErrorMsg(pParse, "syntax error after column name \"%.*s\"",
1424 pIdToken->n, pIdToken->z);
1426 sqlite3ExprListSetName(pParse, p, pIdToken, 1);
1427 return p;
1429 } // end %include
1431 eidlist_opt(A) ::= . {A = 0;}
1432 eidlist_opt(A) ::= LP eidlist(X) RP. {A = X;}
1433 eidlist(A) ::= eidlist(A) COMMA nm(Y) collate(C) sortorder(Z). {
1434 A = parserAddExprIdListTerm(pParse, A, &Y, C, Z);
1436 eidlist(A) ::= nm(Y) collate(C) sortorder(Z). {
1437 A = parserAddExprIdListTerm(pParse, 0, &Y, C, Z); /*A-overwrites-Y*/
1440 %type collate {int}
1441 collate(C) ::= . {C = 0;}
1442 collate(C) ::= COLLATE ids. {C = 1;}
1445 ///////////////////////////// The DROP INDEX command /////////////////////////
1447 cmd ::= DROP INDEX ifexists(E) fullname(X). {sqlite3DropIndex(pParse, X, E);}
1449 ///////////////////////////// The VACUUM command /////////////////////////////
1451 %ifndef SQLITE_OMIT_VACUUM
1452 %ifndef SQLITE_OMIT_ATTACH
1453 cmd ::= VACUUM. {sqlite3Vacuum(pParse,0);}
1454 cmd ::= VACUUM nm(X). {sqlite3Vacuum(pParse,&X);}
1455 %endif SQLITE_OMIT_ATTACH
1456 %endif SQLITE_OMIT_VACUUM
1458 ///////////////////////////// The PRAGMA command /////////////////////////////
1460 %ifndef SQLITE_OMIT_PRAGMA
1461 cmd ::= PRAGMA nm(X) dbnm(Z). {sqlite3Pragma(pParse,&X,&Z,0,0);}
1462 cmd ::= PRAGMA nm(X) dbnm(Z) EQ nmnum(Y). {sqlite3Pragma(pParse,&X,&Z,&Y,0);}
1463 cmd ::= PRAGMA nm(X) dbnm(Z) LP nmnum(Y) RP. {sqlite3Pragma(pParse,&X,&Z,&Y,0);}
1464 cmd ::= PRAGMA nm(X) dbnm(Z) EQ minus_num(Y).
1465 {sqlite3Pragma(pParse,&X,&Z,&Y,1);}
1466 cmd ::= PRAGMA nm(X) dbnm(Z) LP minus_num(Y) RP.
1467 {sqlite3Pragma(pParse,&X,&Z,&Y,1);}
1469 nmnum(A) ::= plus_num(A).
1470 nmnum(A) ::= nm(A).
1471 nmnum(A) ::= ON(A).
1472 nmnum(A) ::= DELETE(A).
1473 nmnum(A) ::= DEFAULT(A).
1474 %endif SQLITE_OMIT_PRAGMA
1475 %token_class number INTEGER|FLOAT.
1476 plus_num(A) ::= PLUS number(X). {A = X;}
1477 plus_num(A) ::= number(A).
1478 minus_num(A) ::= MINUS number(X). {A = X;}
1479 //////////////////////////// The CREATE TRIGGER command /////////////////////
1481 %ifndef SQLITE_OMIT_TRIGGER
1483 cmd ::= createkw trigger_decl(A) BEGIN trigger_cmd_list(S) END(Z). {
1484 Token all;
1485 all.z = A.z;
1486 all.n = (int)(Z.z - A.z) + Z.n;
1487 sqlite3FinishTrigger(pParse, S, &all);
1490 trigger_decl(A) ::= temp(T) TRIGGER ifnotexists(NOERR) nm(B) dbnm(Z)
1491 trigger_time(C) trigger_event(D)
1492 ON fullname(E) foreach_clause when_clause(G). {
1493 sqlite3BeginTrigger(pParse, &B, &Z, C, D.a, D.b, E, G, T, NOERR);
1494 A = (Z.n==0?B:Z); /*A-overwrites-T*/
1497 %type trigger_time {int}
1498 trigger_time(A) ::= BEFORE|AFTER(X). { A = @X; /*A-overwrites-X*/ }
1499 trigger_time(A) ::= INSTEAD OF. { A = TK_INSTEAD;}
1500 trigger_time(A) ::= . { A = TK_BEFORE; }
1502 %type trigger_event {struct TrigEvent}
1503 %destructor trigger_event {sqlite3IdListDelete(pParse->db, $$.b);}
1504 trigger_event(A) ::= DELETE|INSERT(X). {A.a = @X; /*A-overwrites-X*/ A.b = 0;}
1505 trigger_event(A) ::= UPDATE(X). {A.a = @X; /*A-overwrites-X*/ A.b = 0;}
1506 trigger_event(A) ::= UPDATE OF idlist(X).{A.a = TK_UPDATE; A.b = X;}
1508 foreach_clause ::= .
1509 foreach_clause ::= FOR EACH ROW.
1511 %type when_clause {Expr*}
1512 %destructor when_clause {sqlite3ExprDelete(pParse->db, $$);}
1513 when_clause(A) ::= . { A = 0; }
1514 when_clause(A) ::= WHEN expr(X). { A = X; }
1516 %type trigger_cmd_list {TriggerStep*}
1517 %destructor trigger_cmd_list {sqlite3DeleteTriggerStep(pParse->db, $$);}
1518 trigger_cmd_list(A) ::= trigger_cmd_list(A) trigger_cmd(X) SEMI. {
1519 assert( A!=0 );
1520 A->pLast->pNext = X;
1521 A->pLast = X;
1523 trigger_cmd_list(A) ::= trigger_cmd(A) SEMI. {
1524 assert( A!=0 );
1525 A->pLast = A;
1528 // Disallow qualified table names on INSERT, UPDATE, and DELETE statements
1529 // within a trigger. The table to INSERT, UPDATE, or DELETE is always in
1530 // the same database as the table that the trigger fires on.
1532 %type trnm {Token}
1533 trnm(A) ::= nm(A).
1534 trnm(A) ::= nm DOT nm(X). {
1535 A = X;
1536 sqlite3ErrorMsg(pParse,
1537 "qualified table names are not allowed on INSERT, UPDATE, and DELETE "
1538 "statements within triggers");
1541 // Disallow the INDEX BY and NOT INDEXED clauses on UPDATE and DELETE
1542 // statements within triggers. We make a specific error message for this
1543 // since it is an exception to the default grammar rules.
1545 tridxby ::= .
1546 tridxby ::= INDEXED BY nm. {
1547 sqlite3ErrorMsg(pParse,
1548 "the INDEXED BY clause is not allowed on UPDATE or DELETE statements "
1549 "within triggers");
1551 tridxby ::= NOT INDEXED. {
1552 sqlite3ErrorMsg(pParse,
1553 "the NOT INDEXED clause is not allowed on UPDATE or DELETE statements "
1554 "within triggers");
1559 %type trigger_cmd {TriggerStep*}
1560 %destructor trigger_cmd {sqlite3DeleteTriggerStep(pParse->db, $$);}
1561 // UPDATE
1562 trigger_cmd(A) ::=
1563 UPDATE(B) orconf(R) trnm(X) tridxby SET setlist(Y) where_opt(Z) scanpt(E).
1564 {A = sqlite3TriggerUpdateStep(pParse->db, &X, Y, Z, R, B.z, E);}
1566 // INSERT
1567 trigger_cmd(A) ::= scanpt(B) insert_cmd(R) INTO
1568 trnm(X) idlist_opt(F) select(S) upsert(U) scanpt(Z). {
1569 A = sqlite3TriggerInsertStep(pParse->db,&X,F,S,R,U,B,Z);/*A-overwrites-R*/
1571 // DELETE
1572 trigger_cmd(A) ::= DELETE(B) FROM trnm(X) tridxby where_opt(Y) scanpt(E).
1573 {A = sqlite3TriggerDeleteStep(pParse->db, &X, Y, B.z, E);}
1575 // SELECT
1576 trigger_cmd(A) ::= scanpt(B) select(X) scanpt(E).
1577 {A = sqlite3TriggerSelectStep(pParse->db, X, B, E); /*A-overwrites-X*/}
1579 // The special RAISE expression that may occur in trigger programs
1580 expr(A) ::= RAISE LP IGNORE RP. {
1581 A = sqlite3PExpr(pParse, TK_RAISE, 0, 0);
1582 if( A ){
1583 A->affinity = OE_Ignore;
1586 expr(A) ::= RAISE LP raisetype(T) COMMA nm(Z) RP. {
1587 A = sqlite3ExprAlloc(pParse->db, TK_RAISE, &Z, 1);
1588 if( A ) {
1589 A->affinity = (char)T;
1592 %endif !SQLITE_OMIT_TRIGGER
1594 %type raisetype {int}
1595 raisetype(A) ::= ROLLBACK. {A = OE_Rollback;}
1596 raisetype(A) ::= ABORT. {A = OE_Abort;}
1597 raisetype(A) ::= FAIL. {A = OE_Fail;}
1600 //////////////////////// DROP TRIGGER statement //////////////////////////////
1601 %ifndef SQLITE_OMIT_TRIGGER
1602 cmd ::= DROP TRIGGER ifexists(NOERR) fullname(X). {
1603 sqlite3DropTrigger(pParse,X,NOERR);
1605 %endif !SQLITE_OMIT_TRIGGER
1607 //////////////////////// ATTACH DATABASE file AS name /////////////////////////
1608 %ifndef SQLITE_OMIT_ATTACH
1609 cmd ::= ATTACH database_kw_opt expr(F) AS expr(D) key_opt(K). {
1610 sqlite3Attach(pParse, F, D, K);
1612 cmd ::= DETACH database_kw_opt expr(D). {
1613 sqlite3Detach(pParse, D);
1616 %type key_opt {Expr*}
1617 %destructor key_opt {sqlite3ExprDelete(pParse->db, $$);}
1618 key_opt(A) ::= . { A = 0; }
1619 key_opt(A) ::= KEY expr(X). { A = X; }
1621 database_kw_opt ::= DATABASE.
1622 database_kw_opt ::= .
1623 %endif SQLITE_OMIT_ATTACH
1625 ////////////////////////// REINDEX collation //////////////////////////////////
1626 %ifndef SQLITE_OMIT_REINDEX
1627 cmd ::= REINDEX. {sqlite3Reindex(pParse, 0, 0);}
1628 cmd ::= REINDEX nm(X) dbnm(Y). {sqlite3Reindex(pParse, &X, &Y);}
1629 %endif SQLITE_OMIT_REINDEX
1631 /////////////////////////////////// ANALYZE ///////////////////////////////////
1632 %ifndef SQLITE_OMIT_ANALYZE
1633 cmd ::= ANALYZE. {sqlite3Analyze(pParse, 0, 0);}
1634 cmd ::= ANALYZE nm(X) dbnm(Y). {sqlite3Analyze(pParse, &X, &Y);}
1635 %endif
1637 //////////////////////// ALTER TABLE table ... ////////////////////////////////
1638 %ifndef SQLITE_OMIT_ALTERTABLE
1639 cmd ::= ALTER TABLE fullname(X) RENAME TO nm(Z). {
1640 sqlite3AlterRenameTable(pParse,X,&Z);
1642 cmd ::= ALTER TABLE add_column_fullname
1643 ADD kwcolumn_opt columnname(Y) carglist. {
1644 Y.n = (int)(pParse->sLastToken.z-Y.z) + pParse->sLastToken.n;
1645 sqlite3AlterFinishAddColumn(pParse, &Y);
1647 add_column_fullname ::= fullname(X). {
1648 disableLookaside(pParse);
1649 sqlite3AlterBeginAddColumn(pParse, X);
1651 kwcolumn_opt ::= .
1652 kwcolumn_opt ::= COLUMNKW.
1653 %endif SQLITE_OMIT_ALTERTABLE
1655 //////////////////////// CREATE VIRTUAL TABLE ... /////////////////////////////
1656 %ifndef SQLITE_OMIT_VIRTUALTABLE
1657 cmd ::= create_vtab. {sqlite3VtabFinishParse(pParse,0);}
1658 cmd ::= create_vtab LP vtabarglist RP(X). {sqlite3VtabFinishParse(pParse,&X);}
1659 create_vtab ::= createkw VIRTUAL TABLE ifnotexists(E)
1660 nm(X) dbnm(Y) USING nm(Z). {
1661 sqlite3VtabBeginParse(pParse, &X, &Y, &Z, E);
1663 vtabarglist ::= vtabarg.
1664 vtabarglist ::= vtabarglist COMMA vtabarg.
1665 vtabarg ::= . {sqlite3VtabArgInit(pParse);}
1666 vtabarg ::= vtabarg vtabargtoken.
1667 vtabargtoken ::= ANY(X). {sqlite3VtabArgExtend(pParse,&X);}
1668 vtabargtoken ::= lp anylist RP(X). {sqlite3VtabArgExtend(pParse,&X);}
1669 lp ::= LP(X). {sqlite3VtabArgExtend(pParse,&X);}
1670 anylist ::= .
1671 anylist ::= anylist LP anylist RP.
1672 anylist ::= anylist ANY.
1673 %endif SQLITE_OMIT_VIRTUALTABLE
1676 //////////////////////// COMMON TABLE EXPRESSIONS ////////////////////////////
1677 %type wqlist {With*}
1678 %destructor wqlist {sqlite3WithDelete(pParse->db, $$);}
1680 with ::= .
1681 %ifndef SQLITE_OMIT_CTE
1682 with ::= WITH wqlist(W). { sqlite3WithPush(pParse, W, 1); }
1683 with ::= WITH RECURSIVE wqlist(W). { sqlite3WithPush(pParse, W, 1); }
1685 wqlist(A) ::= nm(X) eidlist_opt(Y) AS LP select(Z) RP. {
1686 A = sqlite3WithAdd(pParse, 0, &X, Y, Z); /*A-overwrites-X*/
1688 wqlist(A) ::= wqlist(A) COMMA nm(X) eidlist_opt(Y) AS LP select(Z) RP. {
1689 A = sqlite3WithAdd(pParse, A, &X, Y, Z);
1691 %endif SQLITE_OMIT_CTE