Fix another problem in lead()/lag(). And some errors that could occur
[sqlite.git] / src / parse.y
blob6120533bdf95f3e2f9c2bdb04127746cd2ca6ea4
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) windowdefn_opt(R)
533 orderby_opt(Z) limit_opt(L). {
534 #if SELECTTRACE_ENABLED
535 Token s = S; /*A-overwrites-S*/
536 #endif
537 A = sqlite3SelectNew(pParse,W,X,Y,P,Q,Z,D,L);
538 if( A ){
539 A->pWinDefn = R;
540 }else{
541 sqlite3WindowListDelete(pParse->db, R);
543 #if SELECTTRACE_ENABLED
544 /* Populate the Select.zSelName[] string that is used to help with
545 ** query planner debugging, to differentiate between multiple Select
546 ** objects in a complex query.
548 ** If the SELECT keyword is immediately followed by a C-style comment
549 ** then extract the first few alphanumeric characters from within that
550 ** comment to be the zSelName value. Otherwise, the label is #N where
551 ** is an integer that is incremented with each SELECT statement seen.
553 if( A!=0 ){
554 const char *z = s.z+6;
555 int i;
556 sqlite3_snprintf(sizeof(A->zSelName), A->zSelName,"#%d",++pParse->nSelect);
557 while( z[0]==' ' ) z++;
558 if( z[0]=='/' && z[1]=='*' ){
559 z += 2;
560 while( z[0]==' ' ) z++;
561 for(i=0; sqlite3Isalnum(z[i]); i++){}
562 sqlite3_snprintf(sizeof(A->zSelName), A->zSelName, "%.*s", i, z);
565 #endif /* SELECTRACE_ENABLED */
567 oneselect(A) ::= values(A).
569 %type values {Select*}
570 %destructor values {sqlite3SelectDelete(pParse->db, $$);}
571 values(A) ::= VALUES LP nexprlist(X) RP. {
572 A = sqlite3SelectNew(pParse,X,0,0,0,0,0,SF_Values,0);
574 values(A) ::= values(A) COMMA LP exprlist(Y) RP. {
575 Select *pRight, *pLeft = A;
576 pRight = sqlite3SelectNew(pParse,Y,0,0,0,0,0,SF_Values|SF_MultiValue,0);
577 if( ALWAYS(pLeft) ) pLeft->selFlags &= ~SF_MultiValue;
578 if( pRight ){
579 pRight->op = TK_ALL;
580 pRight->pPrior = pLeft;
581 A = pRight;
582 }else{
583 A = pLeft;
587 // The "distinct" nonterminal is true (1) if the DISTINCT keyword is
588 // present and false (0) if it is not.
590 %type distinct {int}
591 distinct(A) ::= DISTINCT. {A = SF_Distinct;}
592 distinct(A) ::= ALL. {A = SF_All;}
593 distinct(A) ::= . {A = 0;}
595 // selcollist is a list of expressions that are to become the return
596 // values of the SELECT statement. The "*" in statements like
597 // "SELECT * FROM ..." is encoded as a special expression with an
598 // opcode of TK_ASTERISK.
600 %type selcollist {ExprList*}
601 %destructor selcollist {sqlite3ExprListDelete(pParse->db, $$);}
602 %type sclp {ExprList*}
603 %destructor sclp {sqlite3ExprListDelete(pParse->db, $$);}
604 sclp(A) ::= selcollist(A) COMMA.
605 sclp(A) ::= . {A = 0;}
606 selcollist(A) ::= sclp(A) scanpt(B) expr(X) scanpt(Z) as(Y). {
607 A = sqlite3ExprListAppend(pParse, A, X);
608 if( Y.n>0 ) sqlite3ExprListSetName(pParse, A, &Y, 1);
609 sqlite3ExprListSetSpan(pParse,A,B,Z);
611 selcollist(A) ::= sclp(A) scanpt STAR. {
612 Expr *p = sqlite3Expr(pParse->db, TK_ASTERISK, 0);
613 A = sqlite3ExprListAppend(pParse, A, p);
615 selcollist(A) ::= sclp(A) scanpt nm(X) DOT STAR. {
616 Expr *pRight = sqlite3PExpr(pParse, TK_ASTERISK, 0, 0);
617 Expr *pLeft = sqlite3ExprAlloc(pParse->db, TK_ID, &X, 1);
618 Expr *pDot = sqlite3PExpr(pParse, TK_DOT, pLeft, pRight);
619 A = sqlite3ExprListAppend(pParse,A, pDot);
622 // An option "AS <id>" phrase that can follow one of the expressions that
623 // define the result set, or one of the tables in the FROM clause.
625 %type as {Token}
626 as(X) ::= AS nm(Y). {X = Y;}
627 as(X) ::= ids(X).
628 as(X) ::= . {X.n = 0; X.z = 0;}
631 %type seltablist {SrcList*}
632 %destructor seltablist {sqlite3SrcListDelete(pParse->db, $$);}
633 %type stl_prefix {SrcList*}
634 %destructor stl_prefix {sqlite3SrcListDelete(pParse->db, $$);}
635 %type from {SrcList*}
636 %destructor from {sqlite3SrcListDelete(pParse->db, $$);}
638 // A complete FROM clause.
640 from(A) ::= . {A = sqlite3DbMallocZero(pParse->db, sizeof(*A));}
641 from(A) ::= FROM seltablist(X). {
642 A = X;
643 sqlite3SrcListShiftJoinType(A);
646 // "seltablist" is a "Select Table List" - the content of the FROM clause
647 // in a SELECT statement. "stl_prefix" is a prefix of this list.
649 stl_prefix(A) ::= seltablist(A) joinop(Y). {
650 if( ALWAYS(A && A->nSrc>0) ) A->a[A->nSrc-1].fg.jointype = (u8)Y;
652 stl_prefix(A) ::= . {A = 0;}
653 seltablist(A) ::= stl_prefix(A) nm(Y) dbnm(D) as(Z) indexed_opt(I)
654 on_opt(N) using_opt(U). {
655 A = sqlite3SrcListAppendFromTerm(pParse,A,&Y,&D,&Z,0,N,U);
656 sqlite3SrcListIndexedBy(pParse, A, &I);
658 seltablist(A) ::= stl_prefix(A) nm(Y) dbnm(D) LP exprlist(E) RP as(Z)
659 on_opt(N) using_opt(U). {
660 A = sqlite3SrcListAppendFromTerm(pParse,A,&Y,&D,&Z,0,N,U);
661 sqlite3SrcListFuncArgs(pParse, A, E);
663 %ifndef SQLITE_OMIT_SUBQUERY
664 seltablist(A) ::= stl_prefix(A) LP select(S) RP
665 as(Z) on_opt(N) using_opt(U). {
666 A = sqlite3SrcListAppendFromTerm(pParse,A,0,0,&Z,S,N,U);
668 seltablist(A) ::= stl_prefix(A) LP seltablist(F) RP
669 as(Z) on_opt(N) using_opt(U). {
670 if( A==0 && Z.n==0 && N==0 && U==0 ){
671 A = F;
672 }else if( F->nSrc==1 ){
673 A = sqlite3SrcListAppendFromTerm(pParse,A,0,0,&Z,0,N,U);
674 if( A ){
675 struct SrcList_item *pNew = &A->a[A->nSrc-1];
676 struct SrcList_item *pOld = F->a;
677 pNew->zName = pOld->zName;
678 pNew->zDatabase = pOld->zDatabase;
679 pNew->pSelect = pOld->pSelect;
680 pOld->zName = pOld->zDatabase = 0;
681 pOld->pSelect = 0;
683 sqlite3SrcListDelete(pParse->db, F);
684 }else{
685 Select *pSubquery;
686 sqlite3SrcListShiftJoinType(F);
687 pSubquery = sqlite3SelectNew(pParse,0,F,0,0,0,0,SF_NestedFrom,0);
688 A = sqlite3SrcListAppendFromTerm(pParse,A,0,0,&Z,pSubquery,N,U);
691 %endif SQLITE_OMIT_SUBQUERY
693 %type dbnm {Token}
694 dbnm(A) ::= . {A.z=0; A.n=0;}
695 dbnm(A) ::= DOT nm(X). {A = X;}
697 %type fullname {SrcList*}
698 %destructor fullname {sqlite3SrcListDelete(pParse->db, $$);}
699 fullname(A) ::= nm(X).
700 {A = sqlite3SrcListAppend(pParse->db,0,&X,0); /*A-overwrites-X*/}
701 fullname(A) ::= nm(X) DOT nm(Y).
702 {A = sqlite3SrcListAppend(pParse->db,0,&X,&Y); /*A-overwrites-X*/}
704 %type xfullname {SrcList*}
705 %destructor xfullname {sqlite3SrcListDelete(pParse->db, $$);}
706 xfullname(A) ::= nm(X).
707 {A = sqlite3SrcListAppend(pParse->db,0,&X,0); /*A-overwrites-X*/}
708 xfullname(A) ::= nm(X) DOT nm(Y).
709 {A = sqlite3SrcListAppend(pParse->db,0,&X,&Y); /*A-overwrites-X*/}
710 xfullname(A) ::= nm(X) DOT nm(Y) AS nm(Z). {
711 A = sqlite3SrcListAppend(pParse->db,0,&X,&Y); /*A-overwrites-X*/
712 if( A ) A->a[0].zAlias = sqlite3NameFromToken(pParse->db, &Z);
714 xfullname(A) ::= nm(X) AS nm(Z). {
715 A = sqlite3SrcListAppend(pParse->db,0,&X,0); /*A-overwrites-X*/
716 if( A ) A->a[0].zAlias = sqlite3NameFromToken(pParse->db, &Z);
719 %type joinop {int}
720 joinop(X) ::= COMMA|JOIN. { X = JT_INNER; }
721 joinop(X) ::= JOIN_KW(A) JOIN.
722 {X = sqlite3JoinType(pParse,&A,0,0); /*X-overwrites-A*/}
723 joinop(X) ::= JOIN_KW(A) nm(B) JOIN.
724 {X = sqlite3JoinType(pParse,&A,&B,0); /*X-overwrites-A*/}
725 joinop(X) ::= JOIN_KW(A) nm(B) nm(C) JOIN.
726 {X = sqlite3JoinType(pParse,&A,&B,&C);/*X-overwrites-A*/}
728 // There is a parsing abiguity in an upsert statement that uses a
729 // SELECT on the RHS of a the INSERT:
731 // INSERT INTO tab SELECT * FROM aaa JOIN bbb ON CONFLICT ...
732 // here ----^^
734 // When the ON token is encountered, the parser does not know if it is
735 // the beginning of an ON CONFLICT clause, or the beginning of an ON
736 // clause associated with the JOIN. The conflict is resolved in favor
737 // of the JOIN. If an ON CONFLICT clause is intended, insert a dummy
738 // WHERE clause in between, like this:
740 // INSERT INTO tab SELECT * FROM aaa JOIN bbb WHERE true ON CONFLICT ...
742 // The [AND] and [OR] precedence marks in the rules for on_opt cause the
743 // ON in this context to always be interpreted as belonging to the JOIN.
745 %type on_opt {Expr*}
746 %destructor on_opt {sqlite3ExprDelete(pParse->db, $$);}
747 on_opt(N) ::= ON expr(E). {N = E;}
748 on_opt(N) ::= . [OR] {N = 0;}
750 // Note that this block abuses the Token type just a little. If there is
751 // no "INDEXED BY" clause, the returned token is empty (z==0 && n==0). If
752 // there is an INDEXED BY clause, then the token is populated as per normal,
753 // with z pointing to the token data and n containing the number of bytes
754 // in the token.
756 // If there is a "NOT INDEXED" clause, then (z==0 && n==1), which is
757 // normally illegal. The sqlite3SrcListIndexedBy() function
758 // recognizes and interprets this as a special case.
760 %type indexed_opt {Token}
761 indexed_opt(A) ::= . {A.z=0; A.n=0;}
762 indexed_opt(A) ::= INDEXED BY nm(X). {A = X;}
763 indexed_opt(A) ::= NOT INDEXED. {A.z=0; A.n=1;}
765 %type using_opt {IdList*}
766 %destructor using_opt {sqlite3IdListDelete(pParse->db, $$);}
767 using_opt(U) ::= USING LP idlist(L) RP. {U = L;}
768 using_opt(U) ::= . {U = 0;}
771 %type orderby_opt {ExprList*}
772 %destructor orderby_opt {sqlite3ExprListDelete(pParse->db, $$);}
774 // the sortlist non-terminal stores a list of expression where each
775 // expression is optionally followed by ASC or DESC to indicate the
776 // sort order.
778 %type sortlist {ExprList*}
779 %destructor sortlist {sqlite3ExprListDelete(pParse->db, $$);}
781 orderby_opt(A) ::= . {A = 0;}
782 orderby_opt(A) ::= ORDER BY sortlist(X). {A = X;}
783 sortlist(A) ::= sortlist(A) COMMA expr(Y) sortorder(Z). {
784 A = sqlite3ExprListAppend(pParse,A,Y);
785 sqlite3ExprListSetSortOrder(A,Z);
787 sortlist(A) ::= expr(Y) sortorder(Z). {
788 A = sqlite3ExprListAppend(pParse,0,Y); /*A-overwrites-Y*/
789 sqlite3ExprListSetSortOrder(A,Z);
792 %type sortorder {int}
794 sortorder(A) ::= ASC. {A = SQLITE_SO_ASC;}
795 sortorder(A) ::= DESC. {A = SQLITE_SO_DESC;}
796 sortorder(A) ::= . {A = SQLITE_SO_UNDEFINED;}
798 %type groupby_opt {ExprList*}
799 %destructor groupby_opt {sqlite3ExprListDelete(pParse->db, $$);}
800 groupby_opt(A) ::= . {A = 0;}
801 groupby_opt(A) ::= GROUP BY nexprlist(X). {A = X;}
803 %type having_opt {Expr*}
804 %destructor having_opt {sqlite3ExprDelete(pParse->db, $$);}
805 having_opt(A) ::= . {A = 0;}
806 having_opt(A) ::= HAVING expr(X). {A = X;}
808 %type limit_opt {Expr*}
810 // The destructor for limit_opt will never fire in the current grammar.
811 // The limit_opt non-terminal only occurs at the end of a single production
812 // rule for SELECT statements. As soon as the rule that create the
813 // limit_opt non-terminal reduces, the SELECT statement rule will also
814 // reduce. So there is never a limit_opt non-terminal on the stack
815 // except as a transient. So there is never anything to destroy.
817 //%destructor limit_opt {sqlite3ExprDelete(pParse->db, $$);}
818 limit_opt(A) ::= . {A = 0;}
819 limit_opt(A) ::= LIMIT expr(X).
820 {A = sqlite3PExpr(pParse,TK_LIMIT,X,0);}
821 limit_opt(A) ::= LIMIT expr(X) OFFSET expr(Y).
822 {A = sqlite3PExpr(pParse,TK_LIMIT,X,Y);}
823 limit_opt(A) ::= LIMIT expr(X) COMMA expr(Y).
824 {A = sqlite3PExpr(pParse,TK_LIMIT,Y,X);}
826 /////////////////////////// The DELETE statement /////////////////////////////
828 %ifdef SQLITE_ENABLE_UPDATE_DELETE_LIMIT
829 cmd ::= with DELETE FROM xfullname(X) indexed_opt(I) where_opt(W)
830 orderby_opt(O) limit_opt(L). {
831 sqlite3SrcListIndexedBy(pParse, X, &I);
832 sqlite3DeleteFrom(pParse,X,W,O,L);
834 %endif
835 %ifndef SQLITE_ENABLE_UPDATE_DELETE_LIMIT
836 cmd ::= with DELETE FROM xfullname(X) indexed_opt(I) where_opt(W). {
837 sqlite3SrcListIndexedBy(pParse, X, &I);
838 sqlite3DeleteFrom(pParse,X,W,0,0);
840 %endif
842 %type where_opt {Expr*}
843 %destructor where_opt {sqlite3ExprDelete(pParse->db, $$);}
845 where_opt(A) ::= . {A = 0;}
846 where_opt(A) ::= WHERE expr(X). {A = X;}
848 ////////////////////////// The UPDATE command ////////////////////////////////
850 %ifdef SQLITE_ENABLE_UPDATE_DELETE_LIMIT
851 cmd ::= with UPDATE orconf(R) xfullname(X) indexed_opt(I) SET setlist(Y)
852 where_opt(W) orderby_opt(O) limit_opt(L). {
853 sqlite3SrcListIndexedBy(pParse, X, &I);
854 sqlite3ExprListCheckLength(pParse,Y,"set list");
855 sqlite3Update(pParse,X,Y,W,R,O,L,0);
857 %endif
858 %ifndef SQLITE_ENABLE_UPDATE_DELETE_LIMIT
859 cmd ::= with UPDATE orconf(R) xfullname(X) indexed_opt(I) SET setlist(Y)
860 where_opt(W). {
861 sqlite3SrcListIndexedBy(pParse, X, &I);
862 sqlite3ExprListCheckLength(pParse,Y,"set list");
863 sqlite3Update(pParse,X,Y,W,R,0,0,0);
865 %endif
867 %type setlist {ExprList*}
868 %destructor setlist {sqlite3ExprListDelete(pParse->db, $$);}
870 setlist(A) ::= setlist(A) COMMA nm(X) EQ expr(Y). {
871 A = sqlite3ExprListAppend(pParse, A, Y);
872 sqlite3ExprListSetName(pParse, A, &X, 1);
874 setlist(A) ::= setlist(A) COMMA LP idlist(X) RP EQ expr(Y). {
875 A = sqlite3ExprListAppendVector(pParse, A, X, Y);
877 setlist(A) ::= nm(X) EQ expr(Y). {
878 A = sqlite3ExprListAppend(pParse, 0, Y);
879 sqlite3ExprListSetName(pParse, A, &X, 1);
881 setlist(A) ::= LP idlist(X) RP EQ expr(Y). {
882 A = sqlite3ExprListAppendVector(pParse, 0, X, Y);
885 ////////////////////////// The INSERT command /////////////////////////////////
887 cmd ::= with insert_cmd(R) INTO xfullname(X) idlist_opt(F) select(S)
888 upsert(U). {
889 sqlite3Insert(pParse, X, S, F, R, U);
891 cmd ::= with insert_cmd(R) INTO xfullname(X) idlist_opt(F) DEFAULT VALUES.
893 sqlite3Insert(pParse, X, 0, F, R, 0);
896 %type upsert {Upsert*}
898 // Because upsert only occurs at the tip end of the INSERT rule for cmd,
899 // there is never a case where the value of the upsert pointer will not
900 // be destroyed by the cmd action. So comment-out the destructor to
901 // avoid unreachable code.
902 //%destructor upsert {sqlite3UpsertDelete(pParse->db,$$);}
903 upsert(A) ::= . { A = 0; }
904 upsert(A) ::= ON CONFLICT LP sortlist(T) RP where_opt(TW)
905 DO UPDATE SET setlist(Z) where_opt(W).
906 { A = sqlite3UpsertNew(pParse->db,T,TW,Z,W);}
907 upsert(A) ::= ON CONFLICT LP sortlist(T) RP where_opt(TW) DO NOTHING.
908 { A = sqlite3UpsertNew(pParse->db,T,TW,0,0); }
909 upsert(A) ::= ON CONFLICT DO NOTHING.
910 { A = sqlite3UpsertNew(pParse->db,0,0,0,0); }
912 %type insert_cmd {int}
913 insert_cmd(A) ::= INSERT orconf(R). {A = R;}
914 insert_cmd(A) ::= REPLACE. {A = OE_Replace;}
916 %type idlist_opt {IdList*}
917 %destructor idlist_opt {sqlite3IdListDelete(pParse->db, $$);}
918 %type idlist {IdList*}
919 %destructor idlist {sqlite3IdListDelete(pParse->db, $$);}
921 idlist_opt(A) ::= . {A = 0;}
922 idlist_opt(A) ::= LP idlist(X) RP. {A = X;}
923 idlist(A) ::= idlist(A) COMMA nm(Y).
924 {A = sqlite3IdListAppend(pParse->db,A,&Y);}
925 idlist(A) ::= nm(Y).
926 {A = sqlite3IdListAppend(pParse->db,0,&Y); /*A-overwrites-Y*/}
928 /////////////////////////// Expression Processing /////////////////////////////
931 %type expr {Expr*}
932 %destructor expr {sqlite3ExprDelete(pParse->db, $$);}
933 %type term {Expr*}
934 %destructor term {sqlite3ExprDelete(pParse->db, $$);}
936 %include {
938 /* Construct a new Expr object from a single identifier. Use the
939 ** new Expr to populate pOut. Set the span of pOut to be the identifier
940 ** that created the expression.
942 static Expr *tokenExpr(Parse *pParse, int op, Token t){
943 Expr *p = sqlite3DbMallocRawNN(pParse->db, sizeof(Expr)+t.n+1);
944 if( p ){
945 memset(p, 0, sizeof(Expr));
946 p->op = (u8)op;
947 p->flags = EP_Leaf;
948 p->iAgg = -1;
949 p->u.zToken = (char*)&p[1];
950 memcpy(p->u.zToken, t.z, t.n);
951 p->u.zToken[t.n] = 0;
952 if( sqlite3Isquote(p->u.zToken[0]) ){
953 if( p->u.zToken[0]=='"' ) p->flags |= EP_DblQuoted;
954 sqlite3Dequote(p->u.zToken);
956 #if SQLITE_MAX_EXPR_DEPTH>0
957 p->nHeight = 1;
958 #endif
960 return p;
964 expr(A) ::= term(A).
965 expr(A) ::= LP expr(X) RP. {A = X;}
966 expr(A) ::= id(X). {A=tokenExpr(pParse,TK_ID,X); /*A-overwrites-X*/}
967 expr(A) ::= JOIN_KW(X). {A=tokenExpr(pParse,TK_ID,X); /*A-overwrites-X*/}
968 expr(A) ::= nm(X) DOT nm(Y). {
969 Expr *temp1 = sqlite3ExprAlloc(pParse->db, TK_ID, &X, 1);
970 Expr *temp2 = sqlite3ExprAlloc(pParse->db, TK_ID, &Y, 1);
971 A = sqlite3PExpr(pParse, TK_DOT, temp1, temp2);
973 expr(A) ::= nm(X) DOT nm(Y) DOT nm(Z). {
974 Expr *temp1 = sqlite3ExprAlloc(pParse->db, TK_ID, &X, 1);
975 Expr *temp2 = sqlite3ExprAlloc(pParse->db, TK_ID, &Y, 1);
976 Expr *temp3 = sqlite3ExprAlloc(pParse->db, TK_ID, &Z, 1);
977 Expr *temp4 = sqlite3PExpr(pParse, TK_DOT, temp2, temp3);
978 A = sqlite3PExpr(pParse, TK_DOT, temp1, temp4);
980 term(A) ::= NULL|FLOAT|BLOB(X). {A=tokenExpr(pParse,@X,X); /*A-overwrites-X*/}
981 term(A) ::= STRING(X). {A=tokenExpr(pParse,@X,X); /*A-overwrites-X*/}
982 term(A) ::= INTEGER(X). {
983 A = sqlite3ExprAlloc(pParse->db, TK_INTEGER, &X, 1);
985 expr(A) ::= VARIABLE(X). {
986 if( !(X.z[0]=='#' && sqlite3Isdigit(X.z[1])) ){
987 u32 n = X.n;
988 A = tokenExpr(pParse, TK_VARIABLE, X);
989 sqlite3ExprAssignVarNumber(pParse, A, n);
990 }else{
991 /* When doing a nested parse, one can include terms in an expression
992 ** that look like this: #1 #2 ... These terms refer to registers
993 ** in the virtual machine. #N is the N-th register. */
994 Token t = X; /*A-overwrites-X*/
995 assert( t.n>=2 );
996 if( pParse->nested==0 ){
997 sqlite3ErrorMsg(pParse, "near \"%T\": syntax error", &t);
998 A = 0;
999 }else{
1000 A = sqlite3PExpr(pParse, TK_REGISTER, 0, 0);
1001 if( A ) sqlite3GetInt32(&t.z[1], &A->iTable);
1005 expr(A) ::= expr(A) COLLATE ids(C). {
1006 A = sqlite3ExprAddCollateToken(pParse, A, &C, 1);
1008 %ifndef SQLITE_OMIT_CAST
1009 expr(A) ::= CAST LP expr(E) AS typetoken(T) RP. {
1010 A = sqlite3ExprAlloc(pParse->db, TK_CAST, &T, 1);
1011 sqlite3ExprAttachSubtrees(pParse->db, A, E, 0);
1013 %endif SQLITE_OMIT_CAST
1014 expr(A) ::= id(X) LP distinct(D) exprlist(Y) RP over_opt(Z). {
1015 if( Y && Y->nExpr>pParse->db->aLimit[SQLITE_LIMIT_FUNCTION_ARG] ){
1016 sqlite3ErrorMsg(pParse, "too many arguments on function %T", &X);
1018 A = sqlite3ExprFunction(pParse, Y, &X);
1019 sqlite3WindowAttach(pParse, A, Z);
1020 if( D==SF_Distinct && A ){
1021 A->flags |= EP_Distinct;
1024 expr(A) ::= id(X) LP STAR RP over_opt(Z). {
1025 A = sqlite3ExprFunction(pParse, 0, &X);
1026 sqlite3WindowAttach(pParse, A, Z);
1028 term(A) ::= CTIME_KW(OP). {
1029 A = sqlite3ExprFunction(pParse, 0, &OP);
1032 %type windowdefn_opt {Window*}
1033 %destructor windowdefn_opt {sqlite3WindowDelete(pParse->db, $$);}
1034 windowdefn_opt(A) ::= . { A = 0; }
1035 windowdefn_opt(A) ::= WINDOW windowdefn_list(B). { A = B; }
1037 %type windowdefn_list {Window*}
1038 %destructor windowdefn_list {sqlite3WindowDelete(pParse->db, $$);}
1039 windowdefn_list(A) ::= windowdefn(Z). { A = Z; }
1040 windowdefn_list(A) ::= windowdefn_list(Y) COMMA windowdefn(Z). {
1041 if( Z ) Z->pNextWin = Y;
1042 A = Z;
1045 %type windowdefn {Window*}
1046 %destructor windowdefn {sqlite3WindowDelete(pParse->db, $$);}
1047 windowdefn(A) ::= nm(X) AS window(Y). {
1048 if( Y ){
1049 Y->zName = sqlite3DbStrNDup(pParse->db, X.z, X.n);
1051 A = Y;
1054 %type over_opt {Window*}
1055 %destructor over_opt {sqlite3WindowDelete(pParse->db, $$);}
1057 %type window {Window*}
1058 %destructor window {sqlite3WindowDelete(pParse->db, $$);}
1060 %type frame_opt {Window*}
1061 %destructor frame_opt {sqlite3WindowDelete(pParse->db, $$);}
1063 %type window_or_nm {Window*}
1064 %destructor window_or_nm {
1065 sqlite3WindowDelete(pParse->db, $$);}
1067 %type part_opt {ExprList*}
1068 %destructor part_opt {sqlite3ExprListDelete(pParse->db, $$);}
1070 %type filter_opt {Expr*}
1071 %destructor filter_opt {sqlite3ExprDelete(pParse->db, $$);}
1073 %type range_or_rows {int}
1075 %type frame_bound {struct FrameBound}
1076 %destructor frame_bound {sqlite3ExprDelete(pParse->db, $$.pExpr);}
1078 over_opt(A) ::= . { A = 0; }
1079 over_opt(A) ::= filter_opt(W) OVER window_or_nm(Z). {
1080 A = Z;
1081 if( A ) A->pFilter = W;
1084 window_or_nm(A) ::= window(Z). {A = Z;}
1085 window_or_nm(A) ::= nm(Z). {
1086 A = (Window*)sqlite3DbMallocZero(pParse->db, sizeof(Window));
1087 if( A ){
1088 A->zName = sqlite3DbStrNDup(pParse->db, Z.z, Z.n);
1092 window(A) ::= LP part_opt(X) orderby_opt(Y) frame_opt(Z) RP. {
1093 A = Z;
1094 if( A ){
1095 A->pPartition = X;
1096 A->pOrderBy = Y;
1100 part_opt(A) ::= PARTITION BY exprlist(X). { A = X; }
1101 part_opt(A) ::= . { A = 0; }
1102 filter_opt(A) ::= . { A = 0; }
1103 filter_opt(A) ::= FILTER LP WHERE expr(X) RP. { A = X; }
1105 frame_opt(A) ::= . {
1106 A = sqlite3WindowAlloc(pParse, TK_RANGE, TK_UNBOUNDED, 0, TK_CURRENT, 0);
1108 frame_opt(A) ::= range_or_rows(X) frame_bound(Y). {
1109 A = sqlite3WindowAlloc(pParse, X, Y.eType, Y.pExpr, TK_CURRENT, 0);
1111 frame_opt(A) ::= range_or_rows(X) BETWEEN frame_bound(Y) AND frame_bound(Z). {
1112 A = sqlite3WindowAlloc(pParse, X, Y.eType, Y.pExpr, Z.eType, Z.pExpr);
1115 range_or_rows(A) ::= RANGE. { A = TK_RANGE; }
1116 range_or_rows(A) ::= ROWS. { A = TK_ROWS; }
1118 frame_bound(A) ::= UNBOUNDED PRECEDING. { A.eType = TK_UNBOUNDED; A.pExpr = 0; }
1119 frame_bound(A) ::= expr(X) PRECEDING. { A.eType = TK_PRECEDING; A.pExpr = X; }
1120 frame_bound(A) ::= CURRENT ROW. { A.eType = TK_CURRENT ; A.pExpr = 0; }
1121 frame_bound(A) ::= expr(X) FOLLOWING. { A.eType = TK_FOLLOWING; A.pExpr = X; }
1122 frame_bound(A) ::= UNBOUNDED FOLLOWING. { A.eType = TK_UNBOUNDED; A.pExpr = 0; }
1125 expr(A) ::= LP nexprlist(X) COMMA expr(Y) RP. {
1126 ExprList *pList = sqlite3ExprListAppend(pParse, X, Y);
1127 A = sqlite3PExpr(pParse, TK_VECTOR, 0, 0);
1128 if( A ){
1129 A->x.pList = pList;
1130 }else{
1131 sqlite3ExprListDelete(pParse->db, pList);
1135 expr(A) ::= expr(A) AND(OP) expr(Y). {A=sqlite3PExpr(pParse,@OP,A,Y);}
1136 expr(A) ::= expr(A) OR(OP) expr(Y). {A=sqlite3PExpr(pParse,@OP,A,Y);}
1137 expr(A) ::= expr(A) LT|GT|GE|LE(OP) expr(Y).
1138 {A=sqlite3PExpr(pParse,@OP,A,Y);}
1139 expr(A) ::= expr(A) EQ|NE(OP) expr(Y). {A=sqlite3PExpr(pParse,@OP,A,Y);}
1140 expr(A) ::= expr(A) BITAND|BITOR|LSHIFT|RSHIFT(OP) expr(Y).
1141 {A=sqlite3PExpr(pParse,@OP,A,Y);}
1142 expr(A) ::= expr(A) PLUS|MINUS(OP) expr(Y).
1143 {A=sqlite3PExpr(pParse,@OP,A,Y);}
1144 expr(A) ::= expr(A) STAR|SLASH|REM(OP) expr(Y).
1145 {A=sqlite3PExpr(pParse,@OP,A,Y);}
1146 expr(A) ::= expr(A) CONCAT(OP) expr(Y). {A=sqlite3PExpr(pParse,@OP,A,Y);}
1147 %type likeop {Token}
1148 likeop(A) ::= LIKE_KW|MATCH(A).
1149 likeop(A) ::= NOT LIKE_KW|MATCH(X). {A=X; A.n|=0x80000000; /*A-overwrite-X*/}
1150 expr(A) ::= expr(A) likeop(OP) expr(Y). [LIKE_KW] {
1151 ExprList *pList;
1152 int bNot = OP.n & 0x80000000;
1153 OP.n &= 0x7fffffff;
1154 pList = sqlite3ExprListAppend(pParse,0, Y);
1155 pList = sqlite3ExprListAppend(pParse,pList, A);
1156 A = sqlite3ExprFunction(pParse, pList, &OP);
1157 if( bNot ) A = sqlite3PExpr(pParse, TK_NOT, A, 0);
1158 if( A ) A->flags |= EP_InfixFunc;
1160 expr(A) ::= expr(A) likeop(OP) expr(Y) ESCAPE expr(E). [LIKE_KW] {
1161 ExprList *pList;
1162 int bNot = OP.n & 0x80000000;
1163 OP.n &= 0x7fffffff;
1164 pList = sqlite3ExprListAppend(pParse,0, Y);
1165 pList = sqlite3ExprListAppend(pParse,pList, A);
1166 pList = sqlite3ExprListAppend(pParse,pList, E);
1167 A = sqlite3ExprFunction(pParse, pList, &OP);
1168 if( bNot ) A = sqlite3PExpr(pParse, TK_NOT, A, 0);
1169 if( A ) A->flags |= EP_InfixFunc;
1172 expr(A) ::= expr(A) ISNULL|NOTNULL(E). {A = sqlite3PExpr(pParse,@E,A,0);}
1173 expr(A) ::= expr(A) NOT NULL. {A = sqlite3PExpr(pParse,TK_NOTNULL,A,0);}
1175 %include {
1176 /* A routine to convert a binary TK_IS or TK_ISNOT expression into a
1177 ** unary TK_ISNULL or TK_NOTNULL expression. */
1178 static void binaryToUnaryIfNull(Parse *pParse, Expr *pY, Expr *pA, int op){
1179 sqlite3 *db = pParse->db;
1180 if( pA && pY && pY->op==TK_NULL ){
1181 pA->op = (u8)op;
1182 sqlite3ExprDelete(db, pA->pRight);
1183 pA->pRight = 0;
1188 // expr1 IS expr2
1189 // expr1 IS NOT expr2
1191 // If expr2 is NULL then code as TK_ISNULL or TK_NOTNULL. If expr2
1192 // is any other expression, code as TK_IS or TK_ISNOT.
1194 expr(A) ::= expr(A) IS expr(Y). {
1195 A = sqlite3PExpr(pParse,TK_IS,A,Y);
1196 binaryToUnaryIfNull(pParse, Y, A, TK_ISNULL);
1198 expr(A) ::= expr(A) IS NOT expr(Y). {
1199 A = sqlite3PExpr(pParse,TK_ISNOT,A,Y);
1200 binaryToUnaryIfNull(pParse, Y, A, TK_NOTNULL);
1203 expr(A) ::= NOT(B) expr(X).
1204 {A = sqlite3PExpr(pParse, @B, X, 0);/*A-overwrites-B*/}
1205 expr(A) ::= BITNOT(B) expr(X).
1206 {A = sqlite3PExpr(pParse, @B, X, 0);/*A-overwrites-B*/}
1207 expr(A) ::= MINUS expr(X). [BITNOT]
1208 {A = sqlite3PExpr(pParse, TK_UMINUS, X, 0);}
1209 expr(A) ::= PLUS expr(X). [BITNOT]
1210 {A = sqlite3PExpr(pParse, TK_UPLUS, X, 0);}
1212 %type between_op {int}
1213 between_op(A) ::= BETWEEN. {A = 0;}
1214 between_op(A) ::= NOT BETWEEN. {A = 1;}
1215 expr(A) ::= expr(A) between_op(N) expr(X) AND expr(Y). [BETWEEN] {
1216 ExprList *pList = sqlite3ExprListAppend(pParse,0, X);
1217 pList = sqlite3ExprListAppend(pParse,pList, Y);
1218 A = sqlite3PExpr(pParse, TK_BETWEEN, A, 0);
1219 if( A ){
1220 A->x.pList = pList;
1221 }else{
1222 sqlite3ExprListDelete(pParse->db, pList);
1224 if( N ) A = sqlite3PExpr(pParse, TK_NOT, A, 0);
1226 %ifndef SQLITE_OMIT_SUBQUERY
1227 %type in_op {int}
1228 in_op(A) ::= IN. {A = 0;}
1229 in_op(A) ::= NOT IN. {A = 1;}
1230 expr(A) ::= expr(A) in_op(N) LP exprlist(Y) RP. [IN] {
1231 if( Y==0 ){
1232 /* Expressions of the form
1234 ** expr1 IN ()
1235 ** expr1 NOT IN ()
1237 ** simplify to constants 0 (false) and 1 (true), respectively,
1238 ** regardless of the value of expr1.
1240 sqlite3ExprDelete(pParse->db, A);
1241 A = sqlite3ExprAlloc(pParse->db, TK_INTEGER,&sqlite3IntTokens[N],1);
1242 }else if( Y->nExpr==1 ){
1243 /* Expressions of the form:
1245 ** expr1 IN (?1)
1246 ** expr1 NOT IN (?2)
1248 ** with exactly one value on the RHS can be simplified to something
1249 ** like this:
1251 ** expr1 == ?1
1252 ** expr1 <> ?2
1254 ** But, the RHS of the == or <> is marked with the EP_Generic flag
1255 ** so that it may not contribute to the computation of comparison
1256 ** affinity or the collating sequence to use for comparison. Otherwise,
1257 ** the semantics would be subtly different from IN or NOT IN.
1259 Expr *pRHS = Y->a[0].pExpr;
1260 Y->a[0].pExpr = 0;
1261 sqlite3ExprListDelete(pParse->db, Y);
1262 /* pRHS cannot be NULL because a malloc error would have been detected
1263 ** before now and control would have never reached this point */
1264 if( ALWAYS(pRHS) ){
1265 pRHS->flags &= ~EP_Collate;
1266 pRHS->flags |= EP_Generic;
1268 A = sqlite3PExpr(pParse, N ? TK_NE : TK_EQ, A, pRHS);
1269 }else{
1270 A = sqlite3PExpr(pParse, TK_IN, A, 0);
1271 if( A ){
1272 A->x.pList = Y;
1273 sqlite3ExprSetHeightAndFlags(pParse, A);
1274 }else{
1275 sqlite3ExprListDelete(pParse->db, Y);
1277 if( N ) A = sqlite3PExpr(pParse, TK_NOT, A, 0);
1280 expr(A) ::= LP select(X) RP. {
1281 A = sqlite3PExpr(pParse, TK_SELECT, 0, 0);
1282 sqlite3PExprAddSelect(pParse, A, X);
1284 expr(A) ::= expr(A) in_op(N) LP select(Y) RP. [IN] {
1285 A = sqlite3PExpr(pParse, TK_IN, A, 0);
1286 sqlite3PExprAddSelect(pParse, A, Y);
1287 if( N ) A = sqlite3PExpr(pParse, TK_NOT, A, 0);
1289 expr(A) ::= expr(A) in_op(N) nm(Y) dbnm(Z) paren_exprlist(E). [IN] {
1290 SrcList *pSrc = sqlite3SrcListAppend(pParse->db, 0,&Y,&Z);
1291 Select *pSelect = sqlite3SelectNew(pParse, 0,pSrc,0,0,0,0,0,0);
1292 if( E ) sqlite3SrcListFuncArgs(pParse, pSelect ? pSrc : 0, E);
1293 A = sqlite3PExpr(pParse, TK_IN, A, 0);
1294 sqlite3PExprAddSelect(pParse, A, pSelect);
1295 if( N ) A = sqlite3PExpr(pParse, TK_NOT, A, 0);
1297 expr(A) ::= EXISTS LP select(Y) RP. {
1298 Expr *p;
1299 p = A = sqlite3PExpr(pParse, TK_EXISTS, 0, 0);
1300 sqlite3PExprAddSelect(pParse, p, Y);
1302 %endif SQLITE_OMIT_SUBQUERY
1304 /* CASE expressions */
1305 expr(A) ::= CASE case_operand(X) case_exprlist(Y) case_else(Z) END. {
1306 A = sqlite3PExpr(pParse, TK_CASE, X, 0);
1307 if( A ){
1308 A->x.pList = Z ? sqlite3ExprListAppend(pParse,Y,Z) : Y;
1309 sqlite3ExprSetHeightAndFlags(pParse, A);
1310 }else{
1311 sqlite3ExprListDelete(pParse->db, Y);
1312 sqlite3ExprDelete(pParse->db, Z);
1315 %type case_exprlist {ExprList*}
1316 %destructor case_exprlist {sqlite3ExprListDelete(pParse->db, $$);}
1317 case_exprlist(A) ::= case_exprlist(A) WHEN expr(Y) THEN expr(Z). {
1318 A = sqlite3ExprListAppend(pParse,A, Y);
1319 A = sqlite3ExprListAppend(pParse,A, Z);
1321 case_exprlist(A) ::= WHEN expr(Y) THEN expr(Z). {
1322 A = sqlite3ExprListAppend(pParse,0, Y);
1323 A = sqlite3ExprListAppend(pParse,A, Z);
1325 %type case_else {Expr*}
1326 %destructor case_else {sqlite3ExprDelete(pParse->db, $$);}
1327 case_else(A) ::= ELSE expr(X). {A = X;}
1328 case_else(A) ::= . {A = 0;}
1329 %type case_operand {Expr*}
1330 %destructor case_operand {sqlite3ExprDelete(pParse->db, $$);}
1331 case_operand(A) ::= expr(X). {A = X; /*A-overwrites-X*/}
1332 case_operand(A) ::= . {A = 0;}
1334 %type exprlist {ExprList*}
1335 %destructor exprlist {sqlite3ExprListDelete(pParse->db, $$);}
1336 %type nexprlist {ExprList*}
1337 %destructor nexprlist {sqlite3ExprListDelete(pParse->db, $$);}
1339 exprlist(A) ::= nexprlist(A).
1340 exprlist(A) ::= . {A = 0;}
1341 nexprlist(A) ::= nexprlist(A) COMMA expr(Y).
1342 {A = sqlite3ExprListAppend(pParse,A,Y);}
1343 nexprlist(A) ::= expr(Y).
1344 {A = sqlite3ExprListAppend(pParse,0,Y); /*A-overwrites-Y*/}
1346 %ifndef SQLITE_OMIT_SUBQUERY
1347 /* A paren_exprlist is an optional expression list contained inside
1348 ** of parenthesis */
1349 %type paren_exprlist {ExprList*}
1350 %destructor paren_exprlist {sqlite3ExprListDelete(pParse->db, $$);}
1351 paren_exprlist(A) ::= . {A = 0;}
1352 paren_exprlist(A) ::= LP exprlist(X) RP. {A = X;}
1353 %endif SQLITE_OMIT_SUBQUERY
1356 ///////////////////////////// The CREATE INDEX command ///////////////////////
1358 cmd ::= createkw(S) uniqueflag(U) INDEX ifnotexists(NE) nm(X) dbnm(D)
1359 ON nm(Y) LP sortlist(Z) RP where_opt(W). {
1360 sqlite3CreateIndex(pParse, &X, &D,
1361 sqlite3SrcListAppend(pParse->db,0,&Y,0), Z, U,
1362 &S, W, SQLITE_SO_ASC, NE, SQLITE_IDXTYPE_APPDEF);
1365 %type uniqueflag {int}
1366 uniqueflag(A) ::= UNIQUE. {A = OE_Abort;}
1367 uniqueflag(A) ::= . {A = OE_None;}
1370 // The eidlist non-terminal (Expression Id List) generates an ExprList
1371 // from a list of identifiers. The identifier names are in ExprList.a[].zName.
1372 // This list is stored in an ExprList rather than an IdList so that it
1373 // can be easily sent to sqlite3ColumnsExprList().
1375 // eidlist is grouped with CREATE INDEX because it used to be the non-terminal
1376 // used for the arguments to an index. That is just an historical accident.
1378 // IMPORTANT COMPATIBILITY NOTE: Some prior versions of SQLite accepted
1379 // COLLATE clauses and ASC or DESC keywords on ID lists in inappropriate
1380 // places - places that might have been stored in the sqlite_master schema.
1381 // Those extra features were ignored. But because they might be in some
1382 // (busted) old databases, we need to continue parsing them when loading
1383 // historical schemas.
1385 %type eidlist {ExprList*}
1386 %destructor eidlist {sqlite3ExprListDelete(pParse->db, $$);}
1387 %type eidlist_opt {ExprList*}
1388 %destructor eidlist_opt {sqlite3ExprListDelete(pParse->db, $$);}
1390 %include {
1391 /* Add a single new term to an ExprList that is used to store a
1392 ** list of identifiers. Report an error if the ID list contains
1393 ** a COLLATE clause or an ASC or DESC keyword, except ignore the
1394 ** error while parsing a legacy schema.
1396 static ExprList *parserAddExprIdListTerm(
1397 Parse *pParse,
1398 ExprList *pPrior,
1399 Token *pIdToken,
1400 int hasCollate,
1401 int sortOrder
1403 ExprList *p = sqlite3ExprListAppend(pParse, pPrior, 0);
1404 if( (hasCollate || sortOrder!=SQLITE_SO_UNDEFINED)
1405 && pParse->db->init.busy==0
1407 sqlite3ErrorMsg(pParse, "syntax error after column name \"%.*s\"",
1408 pIdToken->n, pIdToken->z);
1410 sqlite3ExprListSetName(pParse, p, pIdToken, 1);
1411 return p;
1413 } // end %include
1415 eidlist_opt(A) ::= . {A = 0;}
1416 eidlist_opt(A) ::= LP eidlist(X) RP. {A = X;}
1417 eidlist(A) ::= eidlist(A) COMMA nm(Y) collate(C) sortorder(Z). {
1418 A = parserAddExprIdListTerm(pParse, A, &Y, C, Z);
1420 eidlist(A) ::= nm(Y) collate(C) sortorder(Z). {
1421 A = parserAddExprIdListTerm(pParse, 0, &Y, C, Z); /*A-overwrites-Y*/
1424 %type collate {int}
1425 collate(C) ::= . {C = 0;}
1426 collate(C) ::= COLLATE ids. {C = 1;}
1429 ///////////////////////////// The DROP INDEX command /////////////////////////
1431 cmd ::= DROP INDEX ifexists(E) fullname(X). {sqlite3DropIndex(pParse, X, E);}
1433 ///////////////////////////// The VACUUM command /////////////////////////////
1435 %ifndef SQLITE_OMIT_VACUUM
1436 %ifndef SQLITE_OMIT_ATTACH
1437 cmd ::= VACUUM. {sqlite3Vacuum(pParse,0);}
1438 cmd ::= VACUUM nm(X). {sqlite3Vacuum(pParse,&X);}
1439 %endif SQLITE_OMIT_ATTACH
1440 %endif SQLITE_OMIT_VACUUM
1442 ///////////////////////////// The PRAGMA command /////////////////////////////
1444 %ifndef SQLITE_OMIT_PRAGMA
1445 cmd ::= PRAGMA nm(X) dbnm(Z). {sqlite3Pragma(pParse,&X,&Z,0,0);}
1446 cmd ::= PRAGMA nm(X) dbnm(Z) EQ nmnum(Y). {sqlite3Pragma(pParse,&X,&Z,&Y,0);}
1447 cmd ::= PRAGMA nm(X) dbnm(Z) LP nmnum(Y) RP. {sqlite3Pragma(pParse,&X,&Z,&Y,0);}
1448 cmd ::= PRAGMA nm(X) dbnm(Z) EQ minus_num(Y).
1449 {sqlite3Pragma(pParse,&X,&Z,&Y,1);}
1450 cmd ::= PRAGMA nm(X) dbnm(Z) LP minus_num(Y) RP.
1451 {sqlite3Pragma(pParse,&X,&Z,&Y,1);}
1453 nmnum(A) ::= plus_num(A).
1454 nmnum(A) ::= nm(A).
1455 nmnum(A) ::= ON(A).
1456 nmnum(A) ::= DELETE(A).
1457 nmnum(A) ::= DEFAULT(A).
1458 %endif SQLITE_OMIT_PRAGMA
1459 %token_class number INTEGER|FLOAT.
1460 plus_num(A) ::= PLUS number(X). {A = X;}
1461 plus_num(A) ::= number(A).
1462 minus_num(A) ::= MINUS number(X). {A = X;}
1463 //////////////////////////// The CREATE TRIGGER command /////////////////////
1465 %ifndef SQLITE_OMIT_TRIGGER
1467 cmd ::= createkw trigger_decl(A) BEGIN trigger_cmd_list(S) END(Z). {
1468 Token all;
1469 all.z = A.z;
1470 all.n = (int)(Z.z - A.z) + Z.n;
1471 sqlite3FinishTrigger(pParse, S, &all);
1474 trigger_decl(A) ::= temp(T) TRIGGER ifnotexists(NOERR) nm(B) dbnm(Z)
1475 trigger_time(C) trigger_event(D)
1476 ON fullname(E) foreach_clause when_clause(G). {
1477 sqlite3BeginTrigger(pParse, &B, &Z, C, D.a, D.b, E, G, T, NOERR);
1478 A = (Z.n==0?B:Z); /*A-overwrites-T*/
1481 %type trigger_time {int}
1482 trigger_time(A) ::= BEFORE|AFTER(X). { A = @X; /*A-overwrites-X*/ }
1483 trigger_time(A) ::= INSTEAD OF. { A = TK_INSTEAD;}
1484 trigger_time(A) ::= . { A = TK_BEFORE; }
1486 %type trigger_event {struct TrigEvent}
1487 %destructor trigger_event {sqlite3IdListDelete(pParse->db, $$.b);}
1488 trigger_event(A) ::= DELETE|INSERT(X). {A.a = @X; /*A-overwrites-X*/ A.b = 0;}
1489 trigger_event(A) ::= UPDATE(X). {A.a = @X; /*A-overwrites-X*/ A.b = 0;}
1490 trigger_event(A) ::= UPDATE OF idlist(X).{A.a = TK_UPDATE; A.b = X;}
1492 foreach_clause ::= .
1493 foreach_clause ::= FOR EACH ROW.
1495 %type when_clause {Expr*}
1496 %destructor when_clause {sqlite3ExprDelete(pParse->db, $$);}
1497 when_clause(A) ::= . { A = 0; }
1498 when_clause(A) ::= WHEN expr(X). { A = X; }
1500 %type trigger_cmd_list {TriggerStep*}
1501 %destructor trigger_cmd_list {sqlite3DeleteTriggerStep(pParse->db, $$);}
1502 trigger_cmd_list(A) ::= trigger_cmd_list(A) trigger_cmd(X) SEMI. {
1503 assert( A!=0 );
1504 A->pLast->pNext = X;
1505 A->pLast = X;
1507 trigger_cmd_list(A) ::= trigger_cmd(A) SEMI. {
1508 assert( A!=0 );
1509 A->pLast = A;
1512 // Disallow qualified table names on INSERT, UPDATE, and DELETE statements
1513 // within a trigger. The table to INSERT, UPDATE, or DELETE is always in
1514 // the same database as the table that the trigger fires on.
1516 %type trnm {Token}
1517 trnm(A) ::= nm(A).
1518 trnm(A) ::= nm DOT nm(X). {
1519 A = X;
1520 sqlite3ErrorMsg(pParse,
1521 "qualified table names are not allowed on INSERT, UPDATE, and DELETE "
1522 "statements within triggers");
1525 // Disallow the INDEX BY and NOT INDEXED clauses on UPDATE and DELETE
1526 // statements within triggers. We make a specific error message for this
1527 // since it is an exception to the default grammar rules.
1529 tridxby ::= .
1530 tridxby ::= INDEXED BY nm. {
1531 sqlite3ErrorMsg(pParse,
1532 "the INDEXED BY clause is not allowed on UPDATE or DELETE statements "
1533 "within triggers");
1535 tridxby ::= NOT INDEXED. {
1536 sqlite3ErrorMsg(pParse,
1537 "the NOT INDEXED clause is not allowed on UPDATE or DELETE statements "
1538 "within triggers");
1543 %type trigger_cmd {TriggerStep*}
1544 %destructor trigger_cmd {sqlite3DeleteTriggerStep(pParse->db, $$);}
1545 // UPDATE
1546 trigger_cmd(A) ::=
1547 UPDATE(B) orconf(R) trnm(X) tridxby SET setlist(Y) where_opt(Z) scanpt(E).
1548 {A = sqlite3TriggerUpdateStep(pParse->db, &X, Y, Z, R, B.z, E);}
1550 // INSERT
1551 trigger_cmd(A) ::= scanpt(B) insert_cmd(R) INTO
1552 trnm(X) idlist_opt(F) select(S) upsert(U) scanpt(Z). {
1553 A = sqlite3TriggerInsertStep(pParse->db,&X,F,S,R,U,B,Z);/*A-overwrites-R*/
1555 // DELETE
1556 trigger_cmd(A) ::= DELETE(B) FROM trnm(X) tridxby where_opt(Y) scanpt(E).
1557 {A = sqlite3TriggerDeleteStep(pParse->db, &X, Y, B.z, E);}
1559 // SELECT
1560 trigger_cmd(A) ::= scanpt(B) select(X) scanpt(E).
1561 {A = sqlite3TriggerSelectStep(pParse->db, X, B, E); /*A-overwrites-X*/}
1563 // The special RAISE expression that may occur in trigger programs
1564 expr(A) ::= RAISE LP IGNORE RP. {
1565 A = sqlite3PExpr(pParse, TK_RAISE, 0, 0);
1566 if( A ){
1567 A->affinity = OE_Ignore;
1570 expr(A) ::= RAISE LP raisetype(T) COMMA nm(Z) RP. {
1571 A = sqlite3ExprAlloc(pParse->db, TK_RAISE, &Z, 1);
1572 if( A ) {
1573 A->affinity = (char)T;
1576 %endif !SQLITE_OMIT_TRIGGER
1578 %type raisetype {int}
1579 raisetype(A) ::= ROLLBACK. {A = OE_Rollback;}
1580 raisetype(A) ::= ABORT. {A = OE_Abort;}
1581 raisetype(A) ::= FAIL. {A = OE_Fail;}
1584 //////////////////////// DROP TRIGGER statement //////////////////////////////
1585 %ifndef SQLITE_OMIT_TRIGGER
1586 cmd ::= DROP TRIGGER ifexists(NOERR) fullname(X). {
1587 sqlite3DropTrigger(pParse,X,NOERR);
1589 %endif !SQLITE_OMIT_TRIGGER
1591 //////////////////////// ATTACH DATABASE file AS name /////////////////////////
1592 %ifndef SQLITE_OMIT_ATTACH
1593 cmd ::= ATTACH database_kw_opt expr(F) AS expr(D) key_opt(K). {
1594 sqlite3Attach(pParse, F, D, K);
1596 cmd ::= DETACH database_kw_opt expr(D). {
1597 sqlite3Detach(pParse, D);
1600 %type key_opt {Expr*}
1601 %destructor key_opt {sqlite3ExprDelete(pParse->db, $$);}
1602 key_opt(A) ::= . { A = 0; }
1603 key_opt(A) ::= KEY expr(X). { A = X; }
1605 database_kw_opt ::= DATABASE.
1606 database_kw_opt ::= .
1607 %endif SQLITE_OMIT_ATTACH
1609 ////////////////////////// REINDEX collation //////////////////////////////////
1610 %ifndef SQLITE_OMIT_REINDEX
1611 cmd ::= REINDEX. {sqlite3Reindex(pParse, 0, 0);}
1612 cmd ::= REINDEX nm(X) dbnm(Y). {sqlite3Reindex(pParse, &X, &Y);}
1613 %endif SQLITE_OMIT_REINDEX
1615 /////////////////////////////////// ANALYZE ///////////////////////////////////
1616 %ifndef SQLITE_OMIT_ANALYZE
1617 cmd ::= ANALYZE. {sqlite3Analyze(pParse, 0, 0);}
1618 cmd ::= ANALYZE nm(X) dbnm(Y). {sqlite3Analyze(pParse, &X, &Y);}
1619 %endif
1621 //////////////////////// ALTER TABLE table ... ////////////////////////////////
1622 %ifndef SQLITE_OMIT_ALTERTABLE
1623 cmd ::= ALTER TABLE fullname(X) RENAME TO nm(Z). {
1624 sqlite3AlterRenameTable(pParse,X,&Z);
1626 cmd ::= ALTER TABLE add_column_fullname
1627 ADD kwcolumn_opt columnname(Y) carglist. {
1628 Y.n = (int)(pParse->sLastToken.z-Y.z) + pParse->sLastToken.n;
1629 sqlite3AlterFinishAddColumn(pParse, &Y);
1631 add_column_fullname ::= fullname(X). {
1632 disableLookaside(pParse);
1633 sqlite3AlterBeginAddColumn(pParse, X);
1635 kwcolumn_opt ::= .
1636 kwcolumn_opt ::= COLUMNKW.
1637 %endif SQLITE_OMIT_ALTERTABLE
1639 //////////////////////// CREATE VIRTUAL TABLE ... /////////////////////////////
1640 %ifndef SQLITE_OMIT_VIRTUALTABLE
1641 cmd ::= create_vtab. {sqlite3VtabFinishParse(pParse,0);}
1642 cmd ::= create_vtab LP vtabarglist RP(X). {sqlite3VtabFinishParse(pParse,&X);}
1643 create_vtab ::= createkw VIRTUAL TABLE ifnotexists(E)
1644 nm(X) dbnm(Y) USING nm(Z). {
1645 sqlite3VtabBeginParse(pParse, &X, &Y, &Z, E);
1647 vtabarglist ::= vtabarg.
1648 vtabarglist ::= vtabarglist COMMA vtabarg.
1649 vtabarg ::= . {sqlite3VtabArgInit(pParse);}
1650 vtabarg ::= vtabarg vtabargtoken.
1651 vtabargtoken ::= ANY(X). {sqlite3VtabArgExtend(pParse,&X);}
1652 vtabargtoken ::= lp anylist RP(X). {sqlite3VtabArgExtend(pParse,&X);}
1653 lp ::= LP(X). {sqlite3VtabArgExtend(pParse,&X);}
1654 anylist ::= .
1655 anylist ::= anylist LP anylist RP.
1656 anylist ::= anylist ANY.
1657 %endif SQLITE_OMIT_VIRTUALTABLE
1660 //////////////////////// COMMON TABLE EXPRESSIONS ////////////////////////////
1661 %type wqlist {With*}
1662 %destructor wqlist {sqlite3WithDelete(pParse->db, $$);}
1664 with ::= .
1665 %ifndef SQLITE_OMIT_CTE
1666 with ::= WITH wqlist(W). { sqlite3WithPush(pParse, W, 1); }
1667 with ::= WITH RECURSIVE wqlist(W). { sqlite3WithPush(pParse, W, 1); }
1669 wqlist(A) ::= nm(X) eidlist_opt(Y) AS LP select(Z) RP. {
1670 A = sqlite3WithAdd(pParse, 0, &X, Y, Z); /*A-overwrites-X*/
1672 wqlist(A) ::= wqlist(A) COMMA nm(X) eidlist_opt(Y) AS LP select(Z) RP. {
1673 A = sqlite3WithAdd(pParse, A, &X, Y, Z);
1675 %endif SQLITE_OMIT_CTE