deprecate cipher_store_pass
[sqlcipher.git] / src / parse.y
blob44e0b4f343ae9b86a90748d9b7ce505be9d86c24
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 sqlite3 *db = pParse->db;
110 pParse->disableLookaside++;
111 DisableLookaside;
114 #if !defined(SQLITE_ENABLE_UPDATE_DELETE_LIMIT) \
115 && defined(SQLITE_UDL_CAPABLE_PARSER)
117 ** Issue an error message if an ORDER BY or LIMIT clause occurs on an
118 ** UPDATE or DELETE statement.
120 static void updateDeleteLimitError(
121 Parse *pParse,
122 ExprList *pOrderBy,
123 Expr *pLimit
125 if( pOrderBy ){
126 sqlite3ErrorMsg(pParse, "syntax error near \"ORDER BY\"");
127 }else{
128 sqlite3ErrorMsg(pParse, "syntax error near \"LIMIT\"");
130 sqlite3ExprListDelete(pParse->db, pOrderBy);
131 sqlite3ExprDelete(pParse->db, pLimit);
133 #endif /* SQLITE_ENABLE_UPDATE_DELETE_LIMIT */
135 } // end %include
137 // Input is a single SQL command
138 input ::= cmdlist.
139 cmdlist ::= cmdlist ecmd.
140 cmdlist ::= ecmd.
141 ecmd ::= SEMI.
142 ecmd ::= cmdx SEMI.
143 %ifndef SQLITE_OMIT_EXPLAIN
144 ecmd ::= explain cmdx SEMI. {NEVER-REDUCE}
145 explain ::= EXPLAIN. { pParse->explain = 1; }
146 explain ::= EXPLAIN QUERY PLAN. { pParse->explain = 2; }
147 %endif SQLITE_OMIT_EXPLAIN
148 cmdx ::= cmd. { sqlite3FinishCoding(pParse); }
150 ///////////////////// Begin and end transactions. ////////////////////////////
153 cmd ::= BEGIN transtype(Y) trans_opt. {sqlite3BeginTransaction(pParse, Y);}
154 trans_opt ::= .
155 trans_opt ::= TRANSACTION.
156 trans_opt ::= TRANSACTION nm.
157 %type transtype {int}
158 transtype(A) ::= . {A = TK_DEFERRED;}
159 transtype(A) ::= DEFERRED(X). {A = @X; /*A-overwrites-X*/}
160 transtype(A) ::= IMMEDIATE(X). {A = @X; /*A-overwrites-X*/}
161 transtype(A) ::= EXCLUSIVE(X). {A = @X; /*A-overwrites-X*/}
162 cmd ::= COMMIT|END(X) trans_opt. {sqlite3EndTransaction(pParse,@X);}
163 cmd ::= ROLLBACK(X) trans_opt. {sqlite3EndTransaction(pParse,@X);}
165 savepoint_opt ::= SAVEPOINT.
166 savepoint_opt ::= .
167 cmd ::= SAVEPOINT nm(X). {
168 sqlite3Savepoint(pParse, SAVEPOINT_BEGIN, &X);
170 cmd ::= RELEASE savepoint_opt nm(X). {
171 sqlite3Savepoint(pParse, SAVEPOINT_RELEASE, &X);
173 cmd ::= ROLLBACK trans_opt TO savepoint_opt nm(X). {
174 sqlite3Savepoint(pParse, SAVEPOINT_ROLLBACK, &X);
177 ///////////////////// The CREATE TABLE statement ////////////////////////////
179 cmd ::= create_table create_table_args.
180 create_table ::= createkw temp(T) TABLE ifnotexists(E) nm(Y) dbnm(Z). {
181 sqlite3StartTable(pParse,&Y,&Z,T,0,0,E);
183 createkw(A) ::= CREATE(A). {disableLookaside(pParse);}
185 %type ifnotexists {int}
186 ifnotexists(A) ::= . {A = 0;}
187 ifnotexists(A) ::= IF NOT EXISTS. {A = 1;}
188 %type temp {int}
189 %ifndef SQLITE_OMIT_TEMPDB
190 temp(A) ::= TEMP. {A = 1;}
191 %endif SQLITE_OMIT_TEMPDB
192 temp(A) ::= . {A = 0;}
193 create_table_args ::= LP columnlist conslist_opt(X) RP(E) table_options(F). {
194 sqlite3EndTable(pParse,&X,&E,F,0);
196 create_table_args ::= AS select(S). {
197 sqlite3EndTable(pParse,0,0,0,S);
198 sqlite3SelectDelete(pParse->db, S);
200 %type table_options {int}
201 table_options(A) ::= . {A = 0;}
202 table_options(A) ::= WITHOUT nm(X). {
203 if( X.n==5 && sqlite3_strnicmp(X.z,"rowid",5)==0 ){
204 A = TF_WithoutRowid | TF_NoVisibleRowid;
205 }else{
206 A = 0;
207 sqlite3ErrorMsg(pParse, "unknown table option: %.*s", X.n, X.z);
210 columnlist ::= columnlist COMMA columnname carglist.
211 columnlist ::= columnname carglist.
212 columnname(A) ::= nm(A) typetoken(Y). {sqlite3AddColumn(pParse,&A,&Y);}
214 // Declare some tokens early in order to influence their values, to
215 // improve performance and reduce the executable size. The goal here is
216 // to get the "jump" operations in ISNULL through ESCAPE to have numeric
217 // values that are early enough so that all jump operations are clustered
218 // at the beginning.
220 %token ABORT ACTION AFTER ANALYZE ASC ATTACH BEFORE BEGIN BY CASCADE CAST.
221 %token CONFLICT DATABASE DEFERRED DESC DETACH EACH END EXCLUSIVE EXPLAIN FAIL.
222 %token OR AND NOT IS MATCH LIKE_KW BETWEEN IN ISNULL NOTNULL NE EQ.
223 %token GT LE LT GE ESCAPE.
225 // The following directive causes tokens ABORT, AFTER, ASC, etc. to
226 // fallback to ID if they will not parse as their original value.
227 // This obviates the need for the "id" nonterminal.
229 %fallback ID
230 ABORT ACTION AFTER ANALYZE ASC ATTACH BEFORE BEGIN BY CASCADE CAST COLUMNKW
231 CONFLICT DATABASE DEFERRED DESC DETACH DO
232 EACH END EXCLUSIVE EXPLAIN FAIL FOR
233 IGNORE IMMEDIATE INITIALLY INSTEAD LIKE_KW MATCH NO PLAN
234 QUERY KEY OF OFFSET PRAGMA RAISE RECURSIVE RELEASE REPLACE RESTRICT ROW ROWS
235 ROLLBACK SAVEPOINT TEMP TRIGGER VACUUM VIEW VIRTUAL WITH WITHOUT
236 NULLS FIRST LAST
237 %ifdef SQLITE_OMIT_COMPOUND_SELECT
238 EXCEPT INTERSECT UNION
239 %endif SQLITE_OMIT_COMPOUND_SELECT
240 %ifndef SQLITE_OMIT_WINDOWFUNC
241 CURRENT FOLLOWING PARTITION PRECEDING RANGE UNBOUNDED
242 EXCLUDE GROUPS OTHERS TIES
243 %endif SQLITE_OMIT_WINDOWFUNC
244 %ifndef SQLITE_OMIT_GENERATED_COLUMNS
245 GENERATED ALWAYS
246 %endif
247 REINDEX RENAME CTIME_KW IF
249 %wildcard ANY.
251 // Define operator precedence early so that this is the first occurrence
252 // of the operator tokens in the grammer. Keeping the operators together
253 // causes them to be assigned integer values that are close together,
254 // which keeps parser tables smaller.
256 // The token values assigned to these symbols is determined by the order
257 // in which lemon first sees them. It must be the case that ISNULL/NOTNULL,
258 // NE/EQ, GT/LE, and GE/LT are separated by only a single value. See
259 // the sqlite3ExprIfFalse() routine for additional information on this
260 // constraint.
262 %left OR.
263 %left AND.
264 %right NOT.
265 %left IS MATCH LIKE_KW BETWEEN IN ISNULL NOTNULL NE EQ.
266 %left GT LE LT GE.
267 %right ESCAPE.
268 %left BITAND BITOR LSHIFT RSHIFT.
269 %left PLUS MINUS.
270 %left STAR SLASH REM.
271 %left CONCAT.
272 %left COLLATE.
273 %right BITNOT.
274 %nonassoc ON.
276 // An IDENTIFIER can be a generic identifier, or one of several
277 // keywords. Any non-standard keyword can also be an identifier.
279 %token_class id ID|INDEXED.
282 // And "ids" is an identifer-or-string.
284 %token_class ids ID|STRING.
286 // The name of a column or table can be any of the following:
288 %type nm {Token}
289 nm(A) ::= id(A).
290 nm(A) ::= STRING(A).
291 nm(A) ::= JOIN_KW(A).
293 // A typetoken is really zero or more tokens that form a type name such
294 // as can be found after the column name in a CREATE TABLE statement.
295 // Multiple tokens are concatenated to form the value of the typetoken.
297 %type typetoken {Token}
298 typetoken(A) ::= . {A.n = 0; A.z = 0;}
299 typetoken(A) ::= typename(A).
300 typetoken(A) ::= typename(A) LP signed RP(Y). {
301 A.n = (int)(&Y.z[Y.n] - A.z);
303 typetoken(A) ::= typename(A) LP signed COMMA signed RP(Y). {
304 A.n = (int)(&Y.z[Y.n] - A.z);
306 %type typename {Token}
307 typename(A) ::= ids(A).
308 typename(A) ::= typename(A) ids(Y). {A.n=Y.n+(int)(Y.z-A.z);}
309 signed ::= plus_num.
310 signed ::= minus_num.
312 // The scanpt non-terminal takes a value which is a pointer to the
313 // input text just past the last token that has been shifted into
314 // the parser. By surrounding some phrase in the grammar with two
315 // scanpt non-terminals, we can capture the input text for that phrase.
316 // For example:
318 // something ::= .... scanpt(A) phrase scanpt(Z).
320 // The text that is parsed as "phrase" is a string starting at A
321 // and containing (int)(Z-A) characters. There might be some extra
322 // whitespace on either end of the text, but that can be removed in
323 // post-processing, if needed.
325 %type scanpt {const char*}
326 scanpt(A) ::= . {
327 assert( yyLookahead!=YYNOCODE );
328 A = yyLookaheadToken.z;
330 scantok(A) ::= . {
331 assert( yyLookahead!=YYNOCODE );
332 A = yyLookaheadToken;
335 // "carglist" is a list of additional constraints that come after the
336 // column name and column type in a CREATE TABLE statement.
338 carglist ::= carglist ccons.
339 carglist ::= .
340 ccons ::= CONSTRAINT nm(X). {pParse->constraintName = X;}
341 ccons ::= DEFAULT scantok(A) term(X).
342 {sqlite3AddDefaultValue(pParse,X,A.z,&A.z[A.n]);}
343 ccons ::= DEFAULT LP(A) expr(X) RP(Z).
344 {sqlite3AddDefaultValue(pParse,X,A.z+1,Z.z);}
345 ccons ::= DEFAULT PLUS(A) scantok(Z) term(X).
346 {sqlite3AddDefaultValue(pParse,X,A.z,&Z.z[Z.n]);}
347 ccons ::= DEFAULT MINUS(A) scantok(Z) term(X). {
348 Expr *p = sqlite3PExpr(pParse, TK_UMINUS, X, 0);
349 sqlite3AddDefaultValue(pParse,p,A.z,&Z.z[Z.n]);
351 ccons ::= DEFAULT scantok id(X). {
352 Expr *p = tokenExpr(pParse, TK_STRING, X);
353 if( p ){
354 sqlite3ExprIdToTrueFalse(p);
355 testcase( p->op==TK_TRUEFALSE && sqlite3ExprTruthValue(p) );
357 sqlite3AddDefaultValue(pParse,p,X.z,X.z+X.n);
360 // In addition to the type name, we also care about the primary key and
361 // UNIQUE constraints.
363 ccons ::= NULL onconf.
364 ccons ::= NOT NULL onconf(R). {sqlite3AddNotNull(pParse, R);}
365 ccons ::= PRIMARY KEY sortorder(Z) onconf(R) autoinc(I).
366 {sqlite3AddPrimaryKey(pParse,0,R,I,Z);}
367 ccons ::= UNIQUE onconf(R). {sqlite3CreateIndex(pParse,0,0,0,0,R,0,0,0,0,
368 SQLITE_IDXTYPE_UNIQUE);}
369 ccons ::= CHECK LP expr(X) RP. {sqlite3AddCheckConstraint(pParse,X);}
370 ccons ::= REFERENCES nm(T) eidlist_opt(TA) refargs(R).
371 {sqlite3CreateForeignKey(pParse,0,&T,TA,R);}
372 ccons ::= defer_subclause(D). {sqlite3DeferForeignKey(pParse,D);}
373 ccons ::= COLLATE ids(C). {sqlite3AddCollateType(pParse, &C);}
374 ccons ::= GENERATED ALWAYS AS generated.
375 ccons ::= AS generated.
376 generated ::= LP expr(E) RP. {sqlite3AddGenerated(pParse,E,0);}
377 generated ::= LP expr(E) RP ID(TYPE). {sqlite3AddGenerated(pParse,E,&TYPE);}
379 // The optional AUTOINCREMENT keyword
380 %type autoinc {int}
381 autoinc(X) ::= . {X = 0;}
382 autoinc(X) ::= AUTOINCR. {X = 1;}
384 // The next group of rules parses the arguments to a REFERENCES clause
385 // that determine if the referential integrity checking is deferred or
386 // or immediate and which determine what action to take if a ref-integ
387 // check fails.
389 %type refargs {int}
390 refargs(A) ::= . { A = OE_None*0x0101; /* EV: R-19803-45884 */}
391 refargs(A) ::= refargs(A) refarg(Y). { A = (A & ~Y.mask) | Y.value; }
392 %type refarg {struct {int value; int mask;}}
393 refarg(A) ::= MATCH nm. { A.value = 0; A.mask = 0x000000; }
394 refarg(A) ::= ON INSERT refact. { A.value = 0; A.mask = 0x000000; }
395 refarg(A) ::= ON DELETE refact(X). { A.value = X; A.mask = 0x0000ff; }
396 refarg(A) ::= ON UPDATE refact(X). { A.value = X<<8; A.mask = 0x00ff00; }
397 %type refact {int}
398 refact(A) ::= SET NULL. { A = OE_SetNull; /* EV: R-33326-45252 */}
399 refact(A) ::= SET DEFAULT. { A = OE_SetDflt; /* EV: R-33326-45252 */}
400 refact(A) ::= CASCADE. { A = OE_Cascade; /* EV: R-33326-45252 */}
401 refact(A) ::= RESTRICT. { A = OE_Restrict; /* EV: R-33326-45252 */}
402 refact(A) ::= NO ACTION. { A = OE_None; /* EV: R-33326-45252 */}
403 %type defer_subclause {int}
404 defer_subclause(A) ::= NOT DEFERRABLE init_deferred_pred_opt. {A = 0;}
405 defer_subclause(A) ::= DEFERRABLE init_deferred_pred_opt(X). {A = X;}
406 %type init_deferred_pred_opt {int}
407 init_deferred_pred_opt(A) ::= . {A = 0;}
408 init_deferred_pred_opt(A) ::= INITIALLY DEFERRED. {A = 1;}
409 init_deferred_pred_opt(A) ::= INITIALLY IMMEDIATE. {A = 0;}
411 conslist_opt(A) ::= . {A.n = 0; A.z = 0;}
412 conslist_opt(A) ::= COMMA(A) conslist.
413 conslist ::= conslist tconscomma tcons.
414 conslist ::= tcons.
415 tconscomma ::= COMMA. {pParse->constraintName.n = 0;}
416 tconscomma ::= .
417 tcons ::= CONSTRAINT nm(X). {pParse->constraintName = X;}
418 tcons ::= PRIMARY KEY LP sortlist(X) autoinc(I) RP onconf(R).
419 {sqlite3AddPrimaryKey(pParse,X,R,I,0);}
420 tcons ::= UNIQUE LP sortlist(X) RP onconf(R).
421 {sqlite3CreateIndex(pParse,0,0,0,X,R,0,0,0,0,
422 SQLITE_IDXTYPE_UNIQUE);}
423 tcons ::= CHECK LP expr(E) RP onconf.
424 {sqlite3AddCheckConstraint(pParse,E);}
425 tcons ::= FOREIGN KEY LP eidlist(FA) RP
426 REFERENCES nm(T) eidlist_opt(TA) refargs(R) defer_subclause_opt(D). {
427 sqlite3CreateForeignKey(pParse, FA, &T, TA, R);
428 sqlite3DeferForeignKey(pParse, D);
430 %type defer_subclause_opt {int}
431 defer_subclause_opt(A) ::= . {A = 0;}
432 defer_subclause_opt(A) ::= defer_subclause(A).
434 // The following is a non-standard extension that allows us to declare the
435 // default behavior when there is a constraint conflict.
437 %type onconf {int}
438 %type orconf {int}
439 %type resolvetype {int}
440 onconf(A) ::= . {A = OE_Default;}
441 onconf(A) ::= ON CONFLICT resolvetype(X). {A = X;}
442 orconf(A) ::= . {A = OE_Default;}
443 orconf(A) ::= OR resolvetype(X). {A = X;}
444 resolvetype(A) ::= raisetype(A).
445 resolvetype(A) ::= IGNORE. {A = OE_Ignore;}
446 resolvetype(A) ::= REPLACE. {A = OE_Replace;}
448 ////////////////////////// The DROP TABLE /////////////////////////////////////
450 cmd ::= DROP TABLE ifexists(E) fullname(X). {
451 sqlite3DropTable(pParse, X, 0, E);
453 %type ifexists {int}
454 ifexists(A) ::= IF EXISTS. {A = 1;}
455 ifexists(A) ::= . {A = 0;}
457 ///////////////////// The CREATE VIEW statement /////////////////////////////
459 %ifndef SQLITE_OMIT_VIEW
460 cmd ::= createkw(X) temp(T) VIEW ifnotexists(E) nm(Y) dbnm(Z) eidlist_opt(C)
461 AS select(S). {
462 sqlite3CreateView(pParse, &X, &Y, &Z, C, S, T, E);
464 cmd ::= DROP VIEW ifexists(E) fullname(X). {
465 sqlite3DropTable(pParse, X, 1, E);
467 %endif SQLITE_OMIT_VIEW
469 //////////////////////// The SELECT statement /////////////////////////////////
471 cmd ::= select(X). {
472 SelectDest dest = {SRT_Output, 0, 0, 0, 0, 0, 0};
473 sqlite3Select(pParse, X, &dest);
474 sqlite3SelectDelete(pParse->db, X);
477 %type select {Select*}
478 %destructor select {sqlite3SelectDelete(pParse->db, $$);}
479 %type selectnowith {Select*}
480 %destructor selectnowith {sqlite3SelectDelete(pParse->db, $$);}
481 %type oneselect {Select*}
482 %destructor oneselect {sqlite3SelectDelete(pParse->db, $$);}
484 %include {
486 ** For a compound SELECT statement, make sure p->pPrior->pNext==p for
487 ** all elements in the list. And make sure list length does not exceed
488 ** SQLITE_LIMIT_COMPOUND_SELECT.
490 static void parserDoubleLinkSelect(Parse *pParse, Select *p){
491 assert( p!=0 );
492 if( p->pPrior ){
493 Select *pNext = 0, *pLoop;
494 int mxSelect, cnt = 0;
495 for(pLoop=p; pLoop; pNext=pLoop, pLoop=pLoop->pPrior, cnt++){
496 pLoop->pNext = pNext;
497 pLoop->selFlags |= SF_Compound;
499 if( (p->selFlags & SF_MultiValue)==0 &&
500 (mxSelect = pParse->db->aLimit[SQLITE_LIMIT_COMPOUND_SELECT])>0 &&
501 cnt>mxSelect
503 sqlite3ErrorMsg(pParse, "too many terms in compound SELECT");
509 %ifndef SQLITE_OMIT_CTE
510 select(A) ::= WITH wqlist(W) selectnowith(X). {
511 Select *p = X;
512 if( p ){
513 p->pWith = W;
514 parserDoubleLinkSelect(pParse, p);
515 }else{
516 sqlite3WithDelete(pParse->db, W);
518 A = p;
520 select(A) ::= WITH RECURSIVE wqlist(W) selectnowith(X). {
521 Select *p = X;
522 if( p ){
523 p->pWith = W;
524 parserDoubleLinkSelect(pParse, p);
525 }else{
526 sqlite3WithDelete(pParse->db, W);
528 A = p;
530 %endif /* SQLITE_OMIT_CTE */
531 select(A) ::= selectnowith(X). {
532 Select *p = X;
533 if( p ){
534 parserDoubleLinkSelect(pParse, p);
536 A = p; /*A-overwrites-X*/
539 selectnowith(A) ::= oneselect(A).
540 %ifndef SQLITE_OMIT_COMPOUND_SELECT
541 selectnowith(A) ::= selectnowith(A) multiselect_op(Y) oneselect(Z). {
542 Select *pRhs = Z;
543 Select *pLhs = A;
544 if( pRhs && pRhs->pPrior ){
545 SrcList *pFrom;
546 Token x;
547 x.n = 0;
548 parserDoubleLinkSelect(pParse, pRhs);
549 pFrom = sqlite3SrcListAppendFromTerm(pParse,0,0,0,&x,pRhs,0,0);
550 pRhs = sqlite3SelectNew(pParse,0,pFrom,0,0,0,0,0,0);
552 if( pRhs ){
553 pRhs->op = (u8)Y;
554 pRhs->pPrior = pLhs;
555 if( ALWAYS(pLhs) ) pLhs->selFlags &= ~SF_MultiValue;
556 pRhs->selFlags &= ~SF_MultiValue;
557 if( Y!=TK_ALL ) pParse->hasCompound = 1;
558 }else{
559 sqlite3SelectDelete(pParse->db, pLhs);
561 A = pRhs;
563 %type multiselect_op {int}
564 multiselect_op(A) ::= UNION(OP). {A = @OP; /*A-overwrites-OP*/}
565 multiselect_op(A) ::= UNION ALL. {A = TK_ALL;}
566 multiselect_op(A) ::= EXCEPT|INTERSECT(OP). {A = @OP; /*A-overwrites-OP*/}
567 %endif SQLITE_OMIT_COMPOUND_SELECT
569 oneselect(A) ::= SELECT distinct(D) selcollist(W) from(X) where_opt(Y)
570 groupby_opt(P) having_opt(Q)
571 orderby_opt(Z) limit_opt(L). {
572 A = sqlite3SelectNew(pParse,W,X,Y,P,Q,Z,D,L);
574 %ifndef SQLITE_OMIT_WINDOWFUNC
575 oneselect(A) ::= SELECT distinct(D) selcollist(W) from(X) where_opt(Y)
576 groupby_opt(P) having_opt(Q) window_clause(R)
577 orderby_opt(Z) limit_opt(L). {
578 A = sqlite3SelectNew(pParse,W,X,Y,P,Q,Z,D,L);
579 if( A ){
580 A->pWinDefn = R;
581 }else{
582 sqlite3WindowListDelete(pParse->db, R);
585 %endif
588 oneselect(A) ::= values(A).
590 %type values {Select*}
591 %destructor values {sqlite3SelectDelete(pParse->db, $$);}
592 values(A) ::= VALUES LP nexprlist(X) RP. {
593 A = sqlite3SelectNew(pParse,X,0,0,0,0,0,SF_Values,0);
595 values(A) ::= values(A) COMMA LP nexprlist(Y) RP. {
596 Select *pRight, *pLeft = A;
597 pRight = sqlite3SelectNew(pParse,Y,0,0,0,0,0,SF_Values|SF_MultiValue,0);
598 if( ALWAYS(pLeft) ) pLeft->selFlags &= ~SF_MultiValue;
599 if( pRight ){
600 pRight->op = TK_ALL;
601 pRight->pPrior = pLeft;
602 A = pRight;
603 }else{
604 A = pLeft;
608 // The "distinct" nonterminal is true (1) if the DISTINCT keyword is
609 // present and false (0) if it is not.
611 %type distinct {int}
612 distinct(A) ::= DISTINCT. {A = SF_Distinct;}
613 distinct(A) ::= ALL. {A = SF_All;}
614 distinct(A) ::= . {A = 0;}
616 // selcollist is a list of expressions that are to become the return
617 // values of the SELECT statement. The "*" in statements like
618 // "SELECT * FROM ..." is encoded as a special expression with an
619 // opcode of TK_ASTERISK.
621 %type selcollist {ExprList*}
622 %destructor selcollist {sqlite3ExprListDelete(pParse->db, $$);}
623 %type sclp {ExprList*}
624 %destructor sclp {sqlite3ExprListDelete(pParse->db, $$);}
625 sclp(A) ::= selcollist(A) COMMA.
626 sclp(A) ::= . {A = 0;}
627 selcollist(A) ::= sclp(A) scanpt(B) expr(X) scanpt(Z) as(Y). {
628 A = sqlite3ExprListAppend(pParse, A, X);
629 if( Y.n>0 ) sqlite3ExprListSetName(pParse, A, &Y, 1);
630 sqlite3ExprListSetSpan(pParse,A,B,Z);
632 selcollist(A) ::= sclp(A) scanpt STAR. {
633 Expr *p = sqlite3Expr(pParse->db, TK_ASTERISK, 0);
634 A = sqlite3ExprListAppend(pParse, A, p);
636 selcollist(A) ::= sclp(A) scanpt nm(X) DOT STAR. {
637 Expr *pRight = sqlite3PExpr(pParse, TK_ASTERISK, 0, 0);
638 Expr *pLeft = sqlite3ExprAlloc(pParse->db, TK_ID, &X, 1);
639 Expr *pDot = sqlite3PExpr(pParse, TK_DOT, pLeft, pRight);
640 A = sqlite3ExprListAppend(pParse,A, pDot);
643 // An option "AS <id>" phrase that can follow one of the expressions that
644 // define the result set, or one of the tables in the FROM clause.
646 %type as {Token}
647 as(X) ::= AS nm(Y). {X = Y;}
648 as(X) ::= ids(X).
649 as(X) ::= . {X.n = 0; X.z = 0;}
652 %type seltablist {SrcList*}
653 %destructor seltablist {sqlite3SrcListDelete(pParse->db, $$);}
654 %type stl_prefix {SrcList*}
655 %destructor stl_prefix {sqlite3SrcListDelete(pParse->db, $$);}
656 %type from {SrcList*}
657 %destructor from {sqlite3SrcListDelete(pParse->db, $$);}
659 // A complete FROM clause.
661 from(A) ::= . {A = 0;}
662 from(A) ::= FROM seltablist(X). {
663 A = X;
664 sqlite3SrcListShiftJoinType(A);
667 // "seltablist" is a "Select Table List" - the content of the FROM clause
668 // in a SELECT statement. "stl_prefix" is a prefix of this list.
670 stl_prefix(A) ::= seltablist(A) joinop(Y). {
671 if( ALWAYS(A && A->nSrc>0) ) A->a[A->nSrc-1].fg.jointype = (u8)Y;
673 stl_prefix(A) ::= . {A = 0;}
674 seltablist(A) ::= stl_prefix(A) nm(Y) dbnm(D) as(Z) indexed_opt(I)
675 on_opt(N) using_opt(U). {
676 A = sqlite3SrcListAppendFromTerm(pParse,A,&Y,&D,&Z,0,N,U);
677 sqlite3SrcListIndexedBy(pParse, A, &I);
679 seltablist(A) ::= stl_prefix(A) nm(Y) dbnm(D) LP exprlist(E) RP as(Z)
680 on_opt(N) using_opt(U). {
681 A = sqlite3SrcListAppendFromTerm(pParse,A,&Y,&D,&Z,0,N,U);
682 sqlite3SrcListFuncArgs(pParse, A, E);
684 %ifndef SQLITE_OMIT_SUBQUERY
685 seltablist(A) ::= stl_prefix(A) LP select(S) RP
686 as(Z) on_opt(N) using_opt(U). {
687 A = sqlite3SrcListAppendFromTerm(pParse,A,0,0,&Z,S,N,U);
689 seltablist(A) ::= stl_prefix(A) LP seltablist(F) RP
690 as(Z) on_opt(N) using_opt(U). {
691 if( A==0 && Z.n==0 && N==0 && U==0 ){
692 A = F;
693 }else if( F->nSrc==1 ){
694 A = sqlite3SrcListAppendFromTerm(pParse,A,0,0,&Z,0,N,U);
695 if( A ){
696 struct SrcList_item *pNew = &A->a[A->nSrc-1];
697 struct SrcList_item *pOld = F->a;
698 pNew->zName = pOld->zName;
699 pNew->zDatabase = pOld->zDatabase;
700 pNew->pSelect = pOld->pSelect;
701 if( pOld->fg.isTabFunc ){
702 pNew->u1.pFuncArg = pOld->u1.pFuncArg;
703 pOld->u1.pFuncArg = 0;
704 pOld->fg.isTabFunc = 0;
705 pNew->fg.isTabFunc = 1;
707 pOld->zName = pOld->zDatabase = 0;
708 pOld->pSelect = 0;
710 sqlite3SrcListDelete(pParse->db, F);
711 }else{
712 Select *pSubquery;
713 sqlite3SrcListShiftJoinType(F);
714 pSubquery = sqlite3SelectNew(pParse,0,F,0,0,0,0,SF_NestedFrom,0);
715 A = sqlite3SrcListAppendFromTerm(pParse,A,0,0,&Z,pSubquery,N,U);
718 %endif SQLITE_OMIT_SUBQUERY
720 %type dbnm {Token}
721 dbnm(A) ::= . {A.z=0; A.n=0;}
722 dbnm(A) ::= DOT nm(X). {A = X;}
724 %type fullname {SrcList*}
725 %destructor fullname {sqlite3SrcListDelete(pParse->db, $$);}
726 fullname(A) ::= nm(X). {
727 A = sqlite3SrcListAppend(pParse,0,&X,0);
728 if( IN_RENAME_OBJECT && A ) sqlite3RenameTokenMap(pParse, A->a[0].zName, &X);
730 fullname(A) ::= nm(X) DOT nm(Y). {
731 A = sqlite3SrcListAppend(pParse,0,&X,&Y);
732 if( IN_RENAME_OBJECT && A ) sqlite3RenameTokenMap(pParse, A->a[0].zName, &Y);
735 %type xfullname {SrcList*}
736 %destructor xfullname {sqlite3SrcListDelete(pParse->db, $$);}
737 xfullname(A) ::= nm(X).
738 {A = sqlite3SrcListAppend(pParse,0,&X,0); /*A-overwrites-X*/}
739 xfullname(A) ::= nm(X) DOT nm(Y).
740 {A = sqlite3SrcListAppend(pParse,0,&X,&Y); /*A-overwrites-X*/}
741 xfullname(A) ::= nm(X) DOT nm(Y) AS nm(Z). {
742 A = sqlite3SrcListAppend(pParse,0,&X,&Y); /*A-overwrites-X*/
743 if( A ) A->a[0].zAlias = sqlite3NameFromToken(pParse->db, &Z);
745 xfullname(A) ::= nm(X) AS nm(Z). {
746 A = sqlite3SrcListAppend(pParse,0,&X,0); /*A-overwrites-X*/
747 if( A ) A->a[0].zAlias = sqlite3NameFromToken(pParse->db, &Z);
750 %type joinop {int}
751 joinop(X) ::= COMMA|JOIN. { X = JT_INNER; }
752 joinop(X) ::= JOIN_KW(A) JOIN.
753 {X = sqlite3JoinType(pParse,&A,0,0); /*X-overwrites-A*/}
754 joinop(X) ::= JOIN_KW(A) nm(B) JOIN.
755 {X = sqlite3JoinType(pParse,&A,&B,0); /*X-overwrites-A*/}
756 joinop(X) ::= JOIN_KW(A) nm(B) nm(C) JOIN.
757 {X = sqlite3JoinType(pParse,&A,&B,&C);/*X-overwrites-A*/}
759 // There is a parsing abiguity in an upsert statement that uses a
760 // SELECT on the RHS of a the INSERT:
762 // INSERT INTO tab SELECT * FROM aaa JOIN bbb ON CONFLICT ...
763 // here ----^^
765 // When the ON token is encountered, the parser does not know if it is
766 // the beginning of an ON CONFLICT clause, or the beginning of an ON
767 // clause associated with the JOIN. The conflict is resolved in favor
768 // of the JOIN. If an ON CONFLICT clause is intended, insert a dummy
769 // WHERE clause in between, like this:
771 // INSERT INTO tab SELECT * FROM aaa JOIN bbb WHERE true ON CONFLICT ...
773 // The [AND] and [OR] precedence marks in the rules for on_opt cause the
774 // ON in this context to always be interpreted as belonging to the JOIN.
776 %type on_opt {Expr*}
777 %destructor on_opt {sqlite3ExprDelete(pParse->db, $$);}
778 on_opt(N) ::= ON expr(E). {N = E;}
779 on_opt(N) ::= . [OR] {N = 0;}
781 // Note that this block abuses the Token type just a little. If there is
782 // no "INDEXED BY" clause, the returned token is empty (z==0 && n==0). If
783 // there is an INDEXED BY clause, then the token is populated as per normal,
784 // with z pointing to the token data and n containing the number of bytes
785 // in the token.
787 // If there is a "NOT INDEXED" clause, then (z==0 && n==1), which is
788 // normally illegal. The sqlite3SrcListIndexedBy() function
789 // recognizes and interprets this as a special case.
791 %type indexed_opt {Token}
792 indexed_opt(A) ::= . {A.z=0; A.n=0;}
793 indexed_opt(A) ::= INDEXED BY nm(X). {A = X;}
794 indexed_opt(A) ::= NOT INDEXED. {A.z=0; A.n=1;}
796 %type using_opt {IdList*}
797 %destructor using_opt {sqlite3IdListDelete(pParse->db, $$);}
798 using_opt(U) ::= USING LP idlist(L) RP. {U = L;}
799 using_opt(U) ::= . {U = 0;}
802 %type orderby_opt {ExprList*}
803 %destructor orderby_opt {sqlite3ExprListDelete(pParse->db, $$);}
805 // the sortlist non-terminal stores a list of expression where each
806 // expression is optionally followed by ASC or DESC to indicate the
807 // sort order.
809 %type sortlist {ExprList*}
810 %destructor sortlist {sqlite3ExprListDelete(pParse->db, $$);}
812 orderby_opt(A) ::= . {A = 0;}
813 orderby_opt(A) ::= ORDER BY sortlist(X). {A = X;}
814 sortlist(A) ::= sortlist(A) COMMA expr(Y) sortorder(Z) nulls(X). {
815 A = sqlite3ExprListAppend(pParse,A,Y);
816 sqlite3ExprListSetSortOrder(A,Z,X);
818 sortlist(A) ::= expr(Y) sortorder(Z) nulls(X). {
819 A = sqlite3ExprListAppend(pParse,0,Y); /*A-overwrites-Y*/
820 sqlite3ExprListSetSortOrder(A,Z,X);
823 %type sortorder {int}
825 sortorder(A) ::= ASC. {A = SQLITE_SO_ASC;}
826 sortorder(A) ::= DESC. {A = SQLITE_SO_DESC;}
827 sortorder(A) ::= . {A = SQLITE_SO_UNDEFINED;}
829 %type nulls {int}
830 nulls(A) ::= NULLS FIRST. {A = SQLITE_SO_ASC;}
831 nulls(A) ::= NULLS LAST. {A = SQLITE_SO_DESC;}
832 nulls(A) ::= . {A = SQLITE_SO_UNDEFINED;}
834 %type groupby_opt {ExprList*}
835 %destructor groupby_opt {sqlite3ExprListDelete(pParse->db, $$);}
836 groupby_opt(A) ::= . {A = 0;}
837 groupby_opt(A) ::= GROUP BY nexprlist(X). {A = X;}
839 %type having_opt {Expr*}
840 %destructor having_opt {sqlite3ExprDelete(pParse->db, $$);}
841 having_opt(A) ::= . {A = 0;}
842 having_opt(A) ::= HAVING expr(X). {A = X;}
844 %type limit_opt {Expr*}
846 // The destructor for limit_opt will never fire in the current grammar.
847 // The limit_opt non-terminal only occurs at the end of a single production
848 // rule for SELECT statements. As soon as the rule that create the
849 // limit_opt non-terminal reduces, the SELECT statement rule will also
850 // reduce. So there is never a limit_opt non-terminal on the stack
851 // except as a transient. So there is never anything to destroy.
853 //%destructor limit_opt {sqlite3ExprDelete(pParse->db, $$);}
854 limit_opt(A) ::= . {A = 0;}
855 limit_opt(A) ::= LIMIT expr(X).
856 {A = sqlite3PExpr(pParse,TK_LIMIT,X,0);}
857 limit_opt(A) ::= LIMIT expr(X) OFFSET expr(Y).
858 {A = sqlite3PExpr(pParse,TK_LIMIT,X,Y);}
859 limit_opt(A) ::= LIMIT expr(X) COMMA expr(Y).
860 {A = sqlite3PExpr(pParse,TK_LIMIT,Y,X);}
862 /////////////////////////// The DELETE statement /////////////////////////////
864 %if SQLITE_ENABLE_UPDATE_DELETE_LIMIT || SQLITE_UDL_CAPABLE_PARSER
865 cmd ::= with DELETE FROM xfullname(X) indexed_opt(I) where_opt(W)
866 orderby_opt(O) limit_opt(L). {
867 sqlite3SrcListIndexedBy(pParse, X, &I);
868 #ifndef SQLITE_ENABLE_UPDATE_DELETE_LIMIT
869 if( O || L ){
870 updateDeleteLimitError(pParse,O,L);
871 O = 0;
872 L = 0;
874 #endif
875 sqlite3DeleteFrom(pParse,X,W,O,L);
877 %else
878 cmd ::= with DELETE FROM xfullname(X) indexed_opt(I) where_opt(W). {
879 sqlite3SrcListIndexedBy(pParse, X, &I);
880 sqlite3DeleteFrom(pParse,X,W,0,0);
882 %endif
884 %type where_opt {Expr*}
885 %destructor where_opt {sqlite3ExprDelete(pParse->db, $$);}
887 where_opt(A) ::= . {A = 0;}
888 where_opt(A) ::= WHERE expr(X). {A = X;}
890 ////////////////////////// The UPDATE command ////////////////////////////////
892 %if SQLITE_ENABLE_UPDATE_DELETE_LIMIT || SQLITE_UDL_CAPABLE_PARSER
893 cmd ::= with UPDATE orconf(R) xfullname(X) indexed_opt(I) SET setlist(Y) from(F)
894 where_opt(W) orderby_opt(O) limit_opt(L). {
895 sqlite3SrcListIndexedBy(pParse, X, &I);
896 X = sqlite3SrcListAppendList(pParse, X, F);
897 sqlite3ExprListCheckLength(pParse,Y,"set list");
898 #ifndef SQLITE_ENABLE_UPDATE_DELETE_LIMIT
899 if( O || L ){
900 updateDeleteLimitError(pParse,O,L);
901 O = 0;
902 L = 0;
904 #endif
905 sqlite3Update(pParse,X,Y,W,R,O,L,0);
907 %else
908 cmd ::= with UPDATE orconf(R) xfullname(X) indexed_opt(I) SET setlist(Y) from(F)
909 where_opt(W). {
910 sqlite3SrcListIndexedBy(pParse, X, &I);
911 sqlite3ExprListCheckLength(pParse,Y,"set list");
912 X = sqlite3SrcListAppendList(pParse, X, F);
913 sqlite3Update(pParse,X,Y,W,R,0,0,0);
915 %endif
919 %type setlist {ExprList*}
920 %destructor setlist {sqlite3ExprListDelete(pParse->db, $$);}
922 setlist(A) ::= setlist(A) COMMA nm(X) EQ expr(Y). {
923 A = sqlite3ExprListAppend(pParse, A, Y);
924 sqlite3ExprListSetName(pParse, A, &X, 1);
926 setlist(A) ::= setlist(A) COMMA LP idlist(X) RP EQ expr(Y). {
927 A = sqlite3ExprListAppendVector(pParse, A, X, Y);
929 setlist(A) ::= nm(X) EQ expr(Y). {
930 A = sqlite3ExprListAppend(pParse, 0, Y);
931 sqlite3ExprListSetName(pParse, A, &X, 1);
933 setlist(A) ::= LP idlist(X) RP EQ expr(Y). {
934 A = sqlite3ExprListAppendVector(pParse, 0, X, Y);
937 ////////////////////////// The INSERT command /////////////////////////////////
939 cmd ::= with insert_cmd(R) INTO xfullname(X) idlist_opt(F) select(S)
940 upsert(U). {
941 sqlite3Insert(pParse, X, S, F, R, U);
943 cmd ::= with insert_cmd(R) INTO xfullname(X) idlist_opt(F) DEFAULT VALUES.
945 sqlite3Insert(pParse, X, 0, F, R, 0);
948 %type upsert {Upsert*}
950 // Because upsert only occurs at the tip end of the INSERT rule for cmd,
951 // there is never a case where the value of the upsert pointer will not
952 // be destroyed by the cmd action. So comment-out the destructor to
953 // avoid unreachable code.
954 //%destructor upsert {sqlite3UpsertDelete(pParse->db,$$);}
955 upsert(A) ::= . { A = 0; }
956 upsert(A) ::= ON CONFLICT LP sortlist(T) RP where_opt(TW)
957 DO UPDATE SET setlist(Z) where_opt(W).
958 { A = sqlite3UpsertNew(pParse->db,T,TW,Z,W);}
959 upsert(A) ::= ON CONFLICT LP sortlist(T) RP where_opt(TW) DO NOTHING.
960 { A = sqlite3UpsertNew(pParse->db,T,TW,0,0); }
961 upsert(A) ::= ON CONFLICT DO NOTHING.
962 { A = sqlite3UpsertNew(pParse->db,0,0,0,0); }
964 %type insert_cmd {int}
965 insert_cmd(A) ::= INSERT orconf(R). {A = R;}
966 insert_cmd(A) ::= REPLACE. {A = OE_Replace;}
968 %type idlist_opt {IdList*}
969 %destructor idlist_opt {sqlite3IdListDelete(pParse->db, $$);}
970 %type idlist {IdList*}
971 %destructor idlist {sqlite3IdListDelete(pParse->db, $$);}
973 idlist_opt(A) ::= . {A = 0;}
974 idlist_opt(A) ::= LP idlist(X) RP. {A = X;}
975 idlist(A) ::= idlist(A) COMMA nm(Y).
976 {A = sqlite3IdListAppend(pParse,A,&Y);}
977 idlist(A) ::= nm(Y).
978 {A = sqlite3IdListAppend(pParse,0,&Y); /*A-overwrites-Y*/}
980 /////////////////////////// Expression Processing /////////////////////////////
983 %type expr {Expr*}
984 %destructor expr {sqlite3ExprDelete(pParse->db, $$);}
985 %type term {Expr*}
986 %destructor term {sqlite3ExprDelete(pParse->db, $$);}
988 %include {
990 /* Construct a new Expr object from a single identifier. Use the
991 ** new Expr to populate pOut. Set the span of pOut to be the identifier
992 ** that created the expression.
994 static Expr *tokenExpr(Parse *pParse, int op, Token t){
995 Expr *p = sqlite3DbMallocRawNN(pParse->db, sizeof(Expr)+t.n+1);
996 if( p ){
997 /* memset(p, 0, sizeof(Expr)); */
998 p->op = (u8)op;
999 p->affExpr = 0;
1000 p->flags = EP_Leaf;
1001 ExprClearVVAProperties(p);
1002 p->iAgg = -1;
1003 p->pLeft = p->pRight = 0;
1004 p->x.pList = 0;
1005 p->pAggInfo = 0;
1006 p->y.pTab = 0;
1007 p->op2 = 0;
1008 p->iTable = 0;
1009 p->iColumn = 0;
1010 p->u.zToken = (char*)&p[1];
1011 memcpy(p->u.zToken, t.z, t.n);
1012 p->u.zToken[t.n] = 0;
1013 if( sqlite3Isquote(p->u.zToken[0]) ){
1014 sqlite3DequoteExpr(p);
1016 #if SQLITE_MAX_EXPR_DEPTH>0
1017 p->nHeight = 1;
1018 #endif
1019 if( IN_RENAME_OBJECT ){
1020 return (Expr*)sqlite3RenameTokenMap(pParse, (void*)p, &t);
1023 return p;
1028 expr(A) ::= term(A).
1029 expr(A) ::= LP expr(X) RP. {A = X;}
1030 expr(A) ::= id(X). {A=tokenExpr(pParse,TK_ID,X); /*A-overwrites-X*/}
1031 expr(A) ::= JOIN_KW(X). {A=tokenExpr(pParse,TK_ID,X); /*A-overwrites-X*/}
1032 expr(A) ::= nm(X) DOT nm(Y). {
1033 Expr *temp1 = sqlite3ExprAlloc(pParse->db, TK_ID, &X, 1);
1034 Expr *temp2 = sqlite3ExprAlloc(pParse->db, TK_ID, &Y, 1);
1035 if( IN_RENAME_OBJECT ){
1036 sqlite3RenameTokenMap(pParse, (void*)temp2, &Y);
1037 sqlite3RenameTokenMap(pParse, (void*)temp1, &X);
1039 A = sqlite3PExpr(pParse, TK_DOT, temp1, temp2);
1041 expr(A) ::= nm(X) DOT nm(Y) DOT nm(Z). {
1042 Expr *temp1 = sqlite3ExprAlloc(pParse->db, TK_ID, &X, 1);
1043 Expr *temp2 = sqlite3ExprAlloc(pParse->db, TK_ID, &Y, 1);
1044 Expr *temp3 = sqlite3ExprAlloc(pParse->db, TK_ID, &Z, 1);
1045 Expr *temp4 = sqlite3PExpr(pParse, TK_DOT, temp2, temp3);
1046 if( IN_RENAME_OBJECT ){
1047 sqlite3RenameTokenMap(pParse, (void*)temp3, &Z);
1048 sqlite3RenameTokenMap(pParse, (void*)temp2, &Y);
1050 A = sqlite3PExpr(pParse, TK_DOT, temp1, temp4);
1052 term(A) ::= NULL|FLOAT|BLOB(X). {A=tokenExpr(pParse,@X,X); /*A-overwrites-X*/}
1053 term(A) ::= STRING(X). {A=tokenExpr(pParse,@X,X); /*A-overwrites-X*/}
1054 term(A) ::= INTEGER(X). {
1055 A = sqlite3ExprAlloc(pParse->db, TK_INTEGER, &X, 1);
1057 expr(A) ::= VARIABLE(X). {
1058 if( !(X.z[0]=='#' && sqlite3Isdigit(X.z[1])) ){
1059 u32 n = X.n;
1060 A = tokenExpr(pParse, TK_VARIABLE, X);
1061 sqlite3ExprAssignVarNumber(pParse, A, n);
1062 }else{
1063 /* When doing a nested parse, one can include terms in an expression
1064 ** that look like this: #1 #2 ... These terms refer to registers
1065 ** in the virtual machine. #N is the N-th register. */
1066 Token t = X; /*A-overwrites-X*/
1067 assert( t.n>=2 );
1068 if( pParse->nested==0 ){
1069 sqlite3ErrorMsg(pParse, "near \"%T\": syntax error", &t);
1070 A = 0;
1071 }else{
1072 A = sqlite3PExpr(pParse, TK_REGISTER, 0, 0);
1073 if( A ) sqlite3GetInt32(&t.z[1], &A->iTable);
1077 expr(A) ::= expr(A) COLLATE ids(C). {
1078 A = sqlite3ExprAddCollateToken(pParse, A, &C, 1);
1080 %ifndef SQLITE_OMIT_CAST
1081 expr(A) ::= CAST LP expr(E) AS typetoken(T) RP. {
1082 A = sqlite3ExprAlloc(pParse->db, TK_CAST, &T, 1);
1083 sqlite3ExprAttachSubtrees(pParse->db, A, E, 0);
1085 %endif SQLITE_OMIT_CAST
1088 expr(A) ::= id(X) LP distinct(D) exprlist(Y) RP. {
1089 A = sqlite3ExprFunction(pParse, Y, &X, D);
1091 expr(A) ::= id(X) LP STAR RP. {
1092 A = sqlite3ExprFunction(pParse, 0, &X, 0);
1095 %ifndef SQLITE_OMIT_WINDOWFUNC
1096 expr(A) ::= id(X) LP distinct(D) exprlist(Y) RP filter_over(Z). {
1097 A = sqlite3ExprFunction(pParse, Y, &X, D);
1098 sqlite3WindowAttach(pParse, A, Z);
1100 expr(A) ::= id(X) LP STAR RP filter_over(Z). {
1101 A = sqlite3ExprFunction(pParse, 0, &X, 0);
1102 sqlite3WindowAttach(pParse, A, Z);
1104 %endif
1106 term(A) ::= CTIME_KW(OP). {
1107 A = sqlite3ExprFunction(pParse, 0, &OP, 0);
1110 expr(A) ::= LP nexprlist(X) COMMA expr(Y) RP. {
1111 ExprList *pList = sqlite3ExprListAppend(pParse, X, Y);
1112 A = sqlite3PExpr(pParse, TK_VECTOR, 0, 0);
1113 if( A ){
1114 A->x.pList = pList;
1115 if( ALWAYS(pList->nExpr) ){
1116 A->flags |= pList->a[0].pExpr->flags & EP_Propagate;
1118 }else{
1119 sqlite3ExprListDelete(pParse->db, pList);
1123 expr(A) ::= expr(A) AND expr(Y). {A=sqlite3ExprAnd(pParse,A,Y);}
1124 expr(A) ::= expr(A) OR(OP) expr(Y). {A=sqlite3PExpr(pParse,@OP,A,Y);}
1125 expr(A) ::= expr(A) LT|GT|GE|LE(OP) expr(Y).
1126 {A=sqlite3PExpr(pParse,@OP,A,Y);}
1127 expr(A) ::= expr(A) EQ|NE(OP) expr(Y). {A=sqlite3PExpr(pParse,@OP,A,Y);}
1128 expr(A) ::= expr(A) BITAND|BITOR|LSHIFT|RSHIFT(OP) expr(Y).
1129 {A=sqlite3PExpr(pParse,@OP,A,Y);}
1130 expr(A) ::= expr(A) PLUS|MINUS(OP) expr(Y).
1131 {A=sqlite3PExpr(pParse,@OP,A,Y);}
1132 expr(A) ::= expr(A) STAR|SLASH|REM(OP) expr(Y).
1133 {A=sqlite3PExpr(pParse,@OP,A,Y);}
1134 expr(A) ::= expr(A) CONCAT(OP) expr(Y). {A=sqlite3PExpr(pParse,@OP,A,Y);}
1135 %type likeop {Token}
1136 likeop(A) ::= LIKE_KW|MATCH(A).
1137 likeop(A) ::= NOT LIKE_KW|MATCH(X). {A=X; A.n|=0x80000000; /*A-overwrite-X*/}
1138 expr(A) ::= expr(A) likeop(OP) expr(Y). [LIKE_KW] {
1139 ExprList *pList;
1140 int bNot = OP.n & 0x80000000;
1141 OP.n &= 0x7fffffff;
1142 pList = sqlite3ExprListAppend(pParse,0, Y);
1143 pList = sqlite3ExprListAppend(pParse,pList, A);
1144 A = sqlite3ExprFunction(pParse, pList, &OP, 0);
1145 if( bNot ) A = sqlite3PExpr(pParse, TK_NOT, A, 0);
1146 if( A ) A->flags |= EP_InfixFunc;
1148 expr(A) ::= expr(A) likeop(OP) expr(Y) ESCAPE expr(E). [LIKE_KW] {
1149 ExprList *pList;
1150 int bNot = OP.n & 0x80000000;
1151 OP.n &= 0x7fffffff;
1152 pList = sqlite3ExprListAppend(pParse,0, Y);
1153 pList = sqlite3ExprListAppend(pParse,pList, A);
1154 pList = sqlite3ExprListAppend(pParse,pList, E);
1155 A = sqlite3ExprFunction(pParse, pList, &OP, 0);
1156 if( bNot ) A = sqlite3PExpr(pParse, TK_NOT, A, 0);
1157 if( A ) A->flags |= EP_InfixFunc;
1160 expr(A) ::= expr(A) ISNULL|NOTNULL(E). {A = sqlite3PExpr(pParse,@E,A,0);}
1161 expr(A) ::= expr(A) NOT NULL. {A = sqlite3PExpr(pParse,TK_NOTNULL,A,0);}
1163 %include {
1164 /* A routine to convert a binary TK_IS or TK_ISNOT expression into a
1165 ** unary TK_ISNULL or TK_NOTNULL expression. */
1166 static void binaryToUnaryIfNull(Parse *pParse, Expr *pY, Expr *pA, int op){
1167 sqlite3 *db = pParse->db;
1168 if( pA && pY && pY->op==TK_NULL && !IN_RENAME_OBJECT ){
1169 pA->op = (u8)op;
1170 sqlite3ExprDelete(db, pA->pRight);
1171 pA->pRight = 0;
1176 // expr1 IS expr2
1177 // expr1 IS NOT expr2
1179 // If expr2 is NULL then code as TK_ISNULL or TK_NOTNULL. If expr2
1180 // is any other expression, code as TK_IS or TK_ISNOT.
1182 expr(A) ::= expr(A) IS expr(Y). {
1183 A = sqlite3PExpr(pParse,TK_IS,A,Y);
1184 binaryToUnaryIfNull(pParse, Y, A, TK_ISNULL);
1186 expr(A) ::= expr(A) IS NOT expr(Y). {
1187 A = sqlite3PExpr(pParse,TK_ISNOT,A,Y);
1188 binaryToUnaryIfNull(pParse, Y, A, TK_NOTNULL);
1191 expr(A) ::= NOT(B) expr(X).
1192 {A = sqlite3PExpr(pParse, @B, X, 0);/*A-overwrites-B*/}
1193 expr(A) ::= BITNOT(B) expr(X).
1194 {A = sqlite3PExpr(pParse, @B, X, 0);/*A-overwrites-B*/}
1195 expr(A) ::= PLUS|MINUS(B) expr(X). [BITNOT] {
1196 A = sqlite3PExpr(pParse, @B==TK_PLUS ? TK_UPLUS : TK_UMINUS, X, 0);
1197 /*A-overwrites-B*/
1200 %type between_op {int}
1201 between_op(A) ::= BETWEEN. {A = 0;}
1202 between_op(A) ::= NOT BETWEEN. {A = 1;}
1203 expr(A) ::= expr(A) between_op(N) expr(X) AND expr(Y). [BETWEEN] {
1204 ExprList *pList = sqlite3ExprListAppend(pParse,0, X);
1205 pList = sqlite3ExprListAppend(pParse,pList, Y);
1206 A = sqlite3PExpr(pParse, TK_BETWEEN, A, 0);
1207 if( A ){
1208 A->x.pList = pList;
1209 }else{
1210 sqlite3ExprListDelete(pParse->db, pList);
1212 if( N ) A = sqlite3PExpr(pParse, TK_NOT, A, 0);
1214 %ifndef SQLITE_OMIT_SUBQUERY
1215 %type in_op {int}
1216 in_op(A) ::= IN. {A = 0;}
1217 in_op(A) ::= NOT IN. {A = 1;}
1218 expr(A) ::= expr(A) in_op(N) LP exprlist(Y) RP. [IN] {
1219 if( Y==0 ){
1220 /* Expressions of the form
1222 ** expr1 IN ()
1223 ** expr1 NOT IN ()
1225 ** simplify to constants 0 (false) and 1 (true), respectively,
1226 ** regardless of the value of expr1.
1228 sqlite3ExprUnmapAndDelete(pParse, A);
1229 A = sqlite3Expr(pParse->db, TK_INTEGER, N ? "1" : "0");
1230 }else if( Y->nExpr==1 && sqlite3ExprIsConstant(Y->a[0].pExpr) ){
1231 Expr *pRHS = Y->a[0].pExpr;
1232 Y->a[0].pExpr = 0;
1233 sqlite3ExprListDelete(pParse->db, Y);
1234 pRHS = sqlite3PExpr(pParse, TK_UPLUS, pRHS, 0);
1235 A = sqlite3PExpr(pParse, TK_EQ, A, pRHS);
1236 if( N ) A = sqlite3PExpr(pParse, TK_NOT, A, 0);
1237 }else{
1238 A = sqlite3PExpr(pParse, TK_IN, A, 0);
1239 if( A ){
1240 A->x.pList = Y;
1241 sqlite3ExprSetHeightAndFlags(pParse, A);
1242 }else{
1243 sqlite3ExprListDelete(pParse->db, Y);
1245 if( N ) A = sqlite3PExpr(pParse, TK_NOT, A, 0);
1248 expr(A) ::= LP select(X) RP. {
1249 A = sqlite3PExpr(pParse, TK_SELECT, 0, 0);
1250 sqlite3PExprAddSelect(pParse, A, X);
1252 expr(A) ::= expr(A) in_op(N) LP select(Y) RP. [IN] {
1253 A = sqlite3PExpr(pParse, TK_IN, A, 0);
1254 sqlite3PExprAddSelect(pParse, A, Y);
1255 if( N ) A = sqlite3PExpr(pParse, TK_NOT, A, 0);
1257 expr(A) ::= expr(A) in_op(N) nm(Y) dbnm(Z) paren_exprlist(E). [IN] {
1258 SrcList *pSrc = sqlite3SrcListAppend(pParse, 0,&Y,&Z);
1259 Select *pSelect = sqlite3SelectNew(pParse, 0,pSrc,0,0,0,0,0,0);
1260 if( E ) sqlite3SrcListFuncArgs(pParse, pSelect ? pSrc : 0, E);
1261 A = sqlite3PExpr(pParse, TK_IN, A, 0);
1262 sqlite3PExprAddSelect(pParse, A, pSelect);
1263 if( N ) A = sqlite3PExpr(pParse, TK_NOT, A, 0);
1265 expr(A) ::= EXISTS LP select(Y) RP. {
1266 Expr *p;
1267 p = A = sqlite3PExpr(pParse, TK_EXISTS, 0, 0);
1268 sqlite3PExprAddSelect(pParse, p, Y);
1270 %endif SQLITE_OMIT_SUBQUERY
1272 /* CASE expressions */
1273 expr(A) ::= CASE case_operand(X) case_exprlist(Y) case_else(Z) END. {
1274 A = sqlite3PExpr(pParse, TK_CASE, X, 0);
1275 if( A ){
1276 A->x.pList = Z ? sqlite3ExprListAppend(pParse,Y,Z) : Y;
1277 sqlite3ExprSetHeightAndFlags(pParse, A);
1278 }else{
1279 sqlite3ExprListDelete(pParse->db, Y);
1280 sqlite3ExprDelete(pParse->db, Z);
1283 %type case_exprlist {ExprList*}
1284 %destructor case_exprlist {sqlite3ExprListDelete(pParse->db, $$);}
1285 case_exprlist(A) ::= case_exprlist(A) WHEN expr(Y) THEN expr(Z). {
1286 A = sqlite3ExprListAppend(pParse,A, Y);
1287 A = sqlite3ExprListAppend(pParse,A, Z);
1289 case_exprlist(A) ::= WHEN expr(Y) THEN expr(Z). {
1290 A = sqlite3ExprListAppend(pParse,0, Y);
1291 A = sqlite3ExprListAppend(pParse,A, Z);
1293 %type case_else {Expr*}
1294 %destructor case_else {sqlite3ExprDelete(pParse->db, $$);}
1295 case_else(A) ::= ELSE expr(X). {A = X;}
1296 case_else(A) ::= . {A = 0;}
1297 %type case_operand {Expr*}
1298 %destructor case_operand {sqlite3ExprDelete(pParse->db, $$);}
1299 case_operand(A) ::= expr(X). {A = X; /*A-overwrites-X*/}
1300 case_operand(A) ::= . {A = 0;}
1302 %type exprlist {ExprList*}
1303 %destructor exprlist {sqlite3ExprListDelete(pParse->db, $$);}
1304 %type nexprlist {ExprList*}
1305 %destructor nexprlist {sqlite3ExprListDelete(pParse->db, $$);}
1307 exprlist(A) ::= nexprlist(A).
1308 exprlist(A) ::= . {A = 0;}
1309 nexprlist(A) ::= nexprlist(A) COMMA expr(Y).
1310 {A = sqlite3ExprListAppend(pParse,A,Y);}
1311 nexprlist(A) ::= expr(Y).
1312 {A = sqlite3ExprListAppend(pParse,0,Y); /*A-overwrites-Y*/}
1314 %ifndef SQLITE_OMIT_SUBQUERY
1315 /* A paren_exprlist is an optional expression list contained inside
1316 ** of parenthesis */
1317 %type paren_exprlist {ExprList*}
1318 %destructor paren_exprlist {sqlite3ExprListDelete(pParse->db, $$);}
1319 paren_exprlist(A) ::= . {A = 0;}
1320 paren_exprlist(A) ::= LP exprlist(X) RP. {A = X;}
1321 %endif SQLITE_OMIT_SUBQUERY
1324 ///////////////////////////// The CREATE INDEX command ///////////////////////
1326 cmd ::= createkw(S) uniqueflag(U) INDEX ifnotexists(NE) nm(X) dbnm(D)
1327 ON nm(Y) LP sortlist(Z) RP where_opt(W). {
1328 sqlite3CreateIndex(pParse, &X, &D,
1329 sqlite3SrcListAppend(pParse,0,&Y,0), Z, U,
1330 &S, W, SQLITE_SO_ASC, NE, SQLITE_IDXTYPE_APPDEF);
1331 if( IN_RENAME_OBJECT && pParse->pNewIndex ){
1332 sqlite3RenameTokenMap(pParse, pParse->pNewIndex->zName, &Y);
1336 %type uniqueflag {int}
1337 uniqueflag(A) ::= UNIQUE. {A = OE_Abort;}
1338 uniqueflag(A) ::= . {A = OE_None;}
1341 // The eidlist non-terminal (Expression Id List) generates an ExprList
1342 // from a list of identifiers. The identifier names are in ExprList.a[].zName.
1343 // This list is stored in an ExprList rather than an IdList so that it
1344 // can be easily sent to sqlite3ColumnsExprList().
1346 // eidlist is grouped with CREATE INDEX because it used to be the non-terminal
1347 // used for the arguments to an index. That is just an historical accident.
1349 // IMPORTANT COMPATIBILITY NOTE: Some prior versions of SQLite accepted
1350 // COLLATE clauses and ASC or DESC keywords on ID lists in inappropriate
1351 // places - places that might have been stored in the sqlite_schema table.
1352 // Those extra features were ignored. But because they might be in some
1353 // (busted) old databases, we need to continue parsing them when loading
1354 // historical schemas.
1356 %type eidlist {ExprList*}
1357 %destructor eidlist {sqlite3ExprListDelete(pParse->db, $$);}
1358 %type eidlist_opt {ExprList*}
1359 %destructor eidlist_opt {sqlite3ExprListDelete(pParse->db, $$);}
1361 %include {
1362 /* Add a single new term to an ExprList that is used to store a
1363 ** list of identifiers. Report an error if the ID list contains
1364 ** a COLLATE clause or an ASC or DESC keyword, except ignore the
1365 ** error while parsing a legacy schema.
1367 static ExprList *parserAddExprIdListTerm(
1368 Parse *pParse,
1369 ExprList *pPrior,
1370 Token *pIdToken,
1371 int hasCollate,
1372 int sortOrder
1374 ExprList *p = sqlite3ExprListAppend(pParse, pPrior, 0);
1375 if( (hasCollate || sortOrder!=SQLITE_SO_UNDEFINED)
1376 && pParse->db->init.busy==0
1378 sqlite3ErrorMsg(pParse, "syntax error after column name \"%.*s\"",
1379 pIdToken->n, pIdToken->z);
1381 sqlite3ExprListSetName(pParse, p, pIdToken, 1);
1382 return p;
1384 } // end %include
1386 eidlist_opt(A) ::= . {A = 0;}
1387 eidlist_opt(A) ::= LP eidlist(X) RP. {A = X;}
1388 eidlist(A) ::= eidlist(A) COMMA nm(Y) collate(C) sortorder(Z). {
1389 A = parserAddExprIdListTerm(pParse, A, &Y, C, Z);
1391 eidlist(A) ::= nm(Y) collate(C) sortorder(Z). {
1392 A = parserAddExprIdListTerm(pParse, 0, &Y, C, Z); /*A-overwrites-Y*/
1395 %type collate {int}
1396 collate(C) ::= . {C = 0;}
1397 collate(C) ::= COLLATE ids. {C = 1;}
1400 ///////////////////////////// The DROP INDEX command /////////////////////////
1402 cmd ::= DROP INDEX ifexists(E) fullname(X). {sqlite3DropIndex(pParse, X, E);}
1404 ///////////////////////////// The VACUUM command /////////////////////////////
1406 %if !SQLITE_OMIT_VACUUM && !SQLITE_OMIT_ATTACH
1407 %type vinto {Expr*}
1408 %destructor vinto {sqlite3ExprDelete(pParse->db, $$);}
1409 cmd ::= VACUUM vinto(Y). {sqlite3Vacuum(pParse,0,Y);}
1410 cmd ::= VACUUM nm(X) vinto(Y). {sqlite3Vacuum(pParse,&X,Y);}
1411 vinto(A) ::= INTO expr(X). {A = X;}
1412 vinto(A) ::= . {A = 0;}
1413 %endif
1415 ///////////////////////////// The PRAGMA command /////////////////////////////
1417 %ifndef SQLITE_OMIT_PRAGMA
1418 cmd ::= PRAGMA nm(X) dbnm(Z). {sqlite3Pragma(pParse,&X,&Z,0,0);}
1419 cmd ::= PRAGMA nm(X) dbnm(Z) EQ nmnum(Y). {sqlite3Pragma(pParse,&X,&Z,&Y,0);}
1420 cmd ::= PRAGMA nm(X) dbnm(Z) LP nmnum(Y) RP. {sqlite3Pragma(pParse,&X,&Z,&Y,0);}
1421 cmd ::= PRAGMA nm(X) dbnm(Z) EQ minus_num(Y).
1422 {sqlite3Pragma(pParse,&X,&Z,&Y,1);}
1423 cmd ::= PRAGMA nm(X) dbnm(Z) LP minus_num(Y) RP.
1424 {sqlite3Pragma(pParse,&X,&Z,&Y,1);}
1426 nmnum(A) ::= plus_num(A).
1427 nmnum(A) ::= nm(A).
1428 nmnum(A) ::= ON(A).
1429 nmnum(A) ::= DELETE(A).
1430 nmnum(A) ::= DEFAULT(A).
1431 %endif SQLITE_OMIT_PRAGMA
1432 %token_class number INTEGER|FLOAT.
1433 plus_num(A) ::= PLUS number(X). {A = X;}
1434 plus_num(A) ::= number(A).
1435 minus_num(A) ::= MINUS number(X). {A = X;}
1436 //////////////////////////// The CREATE TRIGGER command /////////////////////
1438 %ifndef SQLITE_OMIT_TRIGGER
1440 cmd ::= createkw trigger_decl(A) BEGIN trigger_cmd_list(S) END(Z). {
1441 Token all;
1442 all.z = A.z;
1443 all.n = (int)(Z.z - A.z) + Z.n;
1444 sqlite3FinishTrigger(pParse, S, &all);
1447 trigger_decl(A) ::= temp(T) TRIGGER ifnotexists(NOERR) nm(B) dbnm(Z)
1448 trigger_time(C) trigger_event(D)
1449 ON fullname(E) foreach_clause when_clause(G). {
1450 sqlite3BeginTrigger(pParse, &B, &Z, C, D.a, D.b, E, G, T, NOERR);
1451 A = (Z.n==0?B:Z); /*A-overwrites-T*/
1454 %type trigger_time {int}
1455 trigger_time(A) ::= BEFORE|AFTER(X). { A = @X; /*A-overwrites-X*/ }
1456 trigger_time(A) ::= INSTEAD OF. { A = TK_INSTEAD;}
1457 trigger_time(A) ::= . { A = TK_BEFORE; }
1459 %type trigger_event {struct TrigEvent}
1460 %destructor trigger_event {sqlite3IdListDelete(pParse->db, $$.b);}
1461 trigger_event(A) ::= DELETE|INSERT(X). {A.a = @X; /*A-overwrites-X*/ A.b = 0;}
1462 trigger_event(A) ::= UPDATE(X). {A.a = @X; /*A-overwrites-X*/ A.b = 0;}
1463 trigger_event(A) ::= UPDATE OF idlist(X).{A.a = TK_UPDATE; A.b = X;}
1465 foreach_clause ::= .
1466 foreach_clause ::= FOR EACH ROW.
1468 %type when_clause {Expr*}
1469 %destructor when_clause {sqlite3ExprDelete(pParse->db, $$);}
1470 when_clause(A) ::= . { A = 0; }
1471 when_clause(A) ::= WHEN expr(X). { A = X; }
1473 %type trigger_cmd_list {TriggerStep*}
1474 %destructor trigger_cmd_list {sqlite3DeleteTriggerStep(pParse->db, $$);}
1475 trigger_cmd_list(A) ::= trigger_cmd_list(A) trigger_cmd(X) SEMI. {
1476 assert( A!=0 );
1477 A->pLast->pNext = X;
1478 A->pLast = X;
1480 trigger_cmd_list(A) ::= trigger_cmd(A) SEMI. {
1481 assert( A!=0 );
1482 A->pLast = A;
1485 // Disallow qualified table names on INSERT, UPDATE, and DELETE statements
1486 // within a trigger. The table to INSERT, UPDATE, or DELETE is always in
1487 // the same database as the table that the trigger fires on.
1489 %type trnm {Token}
1490 trnm(A) ::= nm(A).
1491 trnm(A) ::= nm DOT nm(X). {
1492 A = X;
1493 sqlite3ErrorMsg(pParse,
1494 "qualified table names are not allowed on INSERT, UPDATE, and DELETE "
1495 "statements within triggers");
1498 // Disallow the INDEX BY and NOT INDEXED clauses on UPDATE and DELETE
1499 // statements within triggers. We make a specific error message for this
1500 // since it is an exception to the default grammar rules.
1502 tridxby ::= .
1503 tridxby ::= INDEXED BY nm. {
1504 sqlite3ErrorMsg(pParse,
1505 "the INDEXED BY clause is not allowed on UPDATE or DELETE statements "
1506 "within triggers");
1508 tridxby ::= NOT INDEXED. {
1509 sqlite3ErrorMsg(pParse,
1510 "the NOT INDEXED clause is not allowed on UPDATE or DELETE statements "
1511 "within triggers");
1516 %type trigger_cmd {TriggerStep*}
1517 %destructor trigger_cmd {sqlite3DeleteTriggerStep(pParse->db, $$);}
1518 // UPDATE
1519 trigger_cmd(A) ::=
1520 UPDATE(B) orconf(R) trnm(X) tridxby SET setlist(Y) from(F) where_opt(Z) scanpt(E).
1521 {A = sqlite3TriggerUpdateStep(pParse, &X, F, Y, Z, R, B.z, E);}
1523 // INSERT
1524 trigger_cmd(A) ::= scanpt(B) insert_cmd(R) INTO
1525 trnm(X) idlist_opt(F) select(S) upsert(U) scanpt(Z). {
1526 A = sqlite3TriggerInsertStep(pParse,&X,F,S,R,U,B,Z);/*A-overwrites-R*/
1528 // DELETE
1529 trigger_cmd(A) ::= DELETE(B) FROM trnm(X) tridxby where_opt(Y) scanpt(E).
1530 {A = sqlite3TriggerDeleteStep(pParse, &X, Y, B.z, E);}
1532 // SELECT
1533 trigger_cmd(A) ::= scanpt(B) select(X) scanpt(E).
1534 {A = sqlite3TriggerSelectStep(pParse->db, X, B, E); /*A-overwrites-X*/}
1536 // The special RAISE expression that may occur in trigger programs
1537 expr(A) ::= RAISE LP IGNORE RP. {
1538 A = sqlite3PExpr(pParse, TK_RAISE, 0, 0);
1539 if( A ){
1540 A->affExpr = OE_Ignore;
1543 expr(A) ::= RAISE LP raisetype(T) COMMA nm(Z) RP. {
1544 A = sqlite3ExprAlloc(pParse->db, TK_RAISE, &Z, 1);
1545 if( A ) {
1546 A->affExpr = (char)T;
1549 %endif !SQLITE_OMIT_TRIGGER
1551 %type raisetype {int}
1552 raisetype(A) ::= ROLLBACK. {A = OE_Rollback;}
1553 raisetype(A) ::= ABORT. {A = OE_Abort;}
1554 raisetype(A) ::= FAIL. {A = OE_Fail;}
1557 //////////////////////// DROP TRIGGER statement //////////////////////////////
1558 %ifndef SQLITE_OMIT_TRIGGER
1559 cmd ::= DROP TRIGGER ifexists(NOERR) fullname(X). {
1560 sqlite3DropTrigger(pParse,X,NOERR);
1562 %endif !SQLITE_OMIT_TRIGGER
1564 //////////////////////// ATTACH DATABASE file AS name /////////////////////////
1565 %ifndef SQLITE_OMIT_ATTACH
1566 cmd ::= ATTACH database_kw_opt expr(F) AS expr(D) key_opt(K). {
1567 sqlite3Attach(pParse, F, D, K);
1569 cmd ::= DETACH database_kw_opt expr(D). {
1570 sqlite3Detach(pParse, D);
1573 %type key_opt {Expr*}
1574 %destructor key_opt {sqlite3ExprDelete(pParse->db, $$);}
1575 key_opt(A) ::= . { A = 0; }
1576 key_opt(A) ::= KEY expr(X). { A = X; }
1578 database_kw_opt ::= DATABASE.
1579 database_kw_opt ::= .
1580 %endif SQLITE_OMIT_ATTACH
1582 ////////////////////////// REINDEX collation //////////////////////////////////
1583 %ifndef SQLITE_OMIT_REINDEX
1584 cmd ::= REINDEX. {sqlite3Reindex(pParse, 0, 0);}
1585 cmd ::= REINDEX nm(X) dbnm(Y). {sqlite3Reindex(pParse, &X, &Y);}
1586 %endif SQLITE_OMIT_REINDEX
1588 /////////////////////////////////// ANALYZE ///////////////////////////////////
1589 %ifndef SQLITE_OMIT_ANALYZE
1590 cmd ::= ANALYZE. {sqlite3Analyze(pParse, 0, 0);}
1591 cmd ::= ANALYZE nm(X) dbnm(Y). {sqlite3Analyze(pParse, &X, &Y);}
1592 %endif
1594 //////////////////////// ALTER TABLE table ... ////////////////////////////////
1595 %ifndef SQLITE_OMIT_ALTERTABLE
1596 cmd ::= ALTER TABLE fullname(X) RENAME TO nm(Z). {
1597 sqlite3AlterRenameTable(pParse,X,&Z);
1599 cmd ::= ALTER TABLE add_column_fullname
1600 ADD kwcolumn_opt columnname(Y) carglist. {
1601 Y.n = (int)(pParse->sLastToken.z-Y.z) + pParse->sLastToken.n;
1602 sqlite3AlterFinishAddColumn(pParse, &Y);
1604 add_column_fullname ::= fullname(X). {
1605 disableLookaside(pParse);
1606 sqlite3AlterBeginAddColumn(pParse, X);
1608 cmd ::= ALTER TABLE fullname(X) RENAME kwcolumn_opt nm(Y) TO nm(Z). {
1609 sqlite3AlterRenameColumn(pParse, X, &Y, &Z);
1612 kwcolumn_opt ::= .
1613 kwcolumn_opt ::= COLUMNKW.
1615 %endif SQLITE_OMIT_ALTERTABLE
1617 //////////////////////// CREATE VIRTUAL TABLE ... /////////////////////////////
1618 %ifndef SQLITE_OMIT_VIRTUALTABLE
1619 cmd ::= create_vtab. {sqlite3VtabFinishParse(pParse,0);}
1620 cmd ::= create_vtab LP vtabarglist RP(X). {sqlite3VtabFinishParse(pParse,&X);}
1621 create_vtab ::= createkw VIRTUAL TABLE ifnotexists(E)
1622 nm(X) dbnm(Y) USING nm(Z). {
1623 sqlite3VtabBeginParse(pParse, &X, &Y, &Z, E);
1625 vtabarglist ::= vtabarg.
1626 vtabarglist ::= vtabarglist COMMA vtabarg.
1627 vtabarg ::= . {sqlite3VtabArgInit(pParse);}
1628 vtabarg ::= vtabarg vtabargtoken.
1629 vtabargtoken ::= ANY(X). {sqlite3VtabArgExtend(pParse,&X);}
1630 vtabargtoken ::= lp anylist RP(X). {sqlite3VtabArgExtend(pParse,&X);}
1631 lp ::= LP(X). {sqlite3VtabArgExtend(pParse,&X);}
1632 anylist ::= .
1633 anylist ::= anylist LP anylist RP.
1634 anylist ::= anylist ANY.
1635 %endif SQLITE_OMIT_VIRTUALTABLE
1638 //////////////////////// COMMON TABLE EXPRESSIONS ////////////////////////////
1639 %type wqlist {With*}
1640 %destructor wqlist {sqlite3WithDelete(pParse->db, $$);}
1642 with ::= .
1643 %ifndef SQLITE_OMIT_CTE
1644 with ::= WITH wqlist(W). { sqlite3WithPush(pParse, W, 1); }
1645 with ::= WITH RECURSIVE wqlist(W). { sqlite3WithPush(pParse, W, 1); }
1647 wqlist(A) ::= nm(X) eidlist_opt(Y) AS LP select(Z) RP. {
1648 A = sqlite3WithAdd(pParse, 0, &X, Y, Z); /*A-overwrites-X*/
1650 wqlist(A) ::= wqlist(A) COMMA nm(X) eidlist_opt(Y) AS LP select(Z) RP. {
1651 A = sqlite3WithAdd(pParse, A, &X, Y, Z);
1653 %endif SQLITE_OMIT_CTE
1655 //////////////////////// WINDOW FUNCTION EXPRESSIONS /////////////////////////
1656 // These must be at the end of this file. Specifically, the rules that
1657 // introduce tokens WINDOW, OVER and FILTER must appear last. This causes
1658 // the integer values assigned to these tokens to be larger than all other
1659 // tokens that may be output by the tokenizer except TK_SPACE and TK_ILLEGAL.
1661 %ifndef SQLITE_OMIT_WINDOWFUNC
1662 %type windowdefn_list {Window*}
1663 %destructor windowdefn_list {sqlite3WindowListDelete(pParse->db, $$);}
1664 windowdefn_list(A) ::= windowdefn(Z). { A = Z; }
1665 windowdefn_list(A) ::= windowdefn_list(Y) COMMA windowdefn(Z). {
1666 assert( Z!=0 );
1667 sqlite3WindowChain(pParse, Z, Y);
1668 Z->pNextWin = Y;
1669 A = Z;
1672 %type windowdefn {Window*}
1673 %destructor windowdefn {sqlite3WindowDelete(pParse->db, $$);}
1674 windowdefn(A) ::= nm(X) AS LP window(Y) RP. {
1675 if( ALWAYS(Y) ){
1676 Y->zName = sqlite3DbStrNDup(pParse->db, X.z, X.n);
1678 A = Y;
1681 %type window {Window*}
1682 %destructor window {sqlite3WindowDelete(pParse->db, $$);}
1684 %type frame_opt {Window*}
1685 %destructor frame_opt {sqlite3WindowDelete(pParse->db, $$);}
1687 %type part_opt {ExprList*}
1688 %destructor part_opt {sqlite3ExprListDelete(pParse->db, $$);}
1690 %type filter_clause {Expr*}
1691 %destructor filter_clause {sqlite3ExprDelete(pParse->db, $$);}
1693 %type over_clause {Window*}
1694 %destructor over_clause {sqlite3WindowDelete(pParse->db, $$);}
1696 %type filter_over {Window*}
1697 %destructor filter_over {sqlite3WindowDelete(pParse->db, $$);}
1699 %type range_or_rows {int}
1701 %type frame_bound {struct FrameBound}
1702 %destructor frame_bound {sqlite3ExprDelete(pParse->db, $$.pExpr);}
1703 %type frame_bound_s {struct FrameBound}
1704 %destructor frame_bound_s {sqlite3ExprDelete(pParse->db, $$.pExpr);}
1705 %type frame_bound_e {struct FrameBound}
1706 %destructor frame_bound_e {sqlite3ExprDelete(pParse->db, $$.pExpr);}
1708 window(A) ::= PARTITION BY nexprlist(X) orderby_opt(Y) frame_opt(Z). {
1709 A = sqlite3WindowAssemble(pParse, Z, X, Y, 0);
1711 window(A) ::= nm(W) PARTITION BY nexprlist(X) orderby_opt(Y) frame_opt(Z). {
1712 A = sqlite3WindowAssemble(pParse, Z, X, Y, &W);
1714 window(A) ::= ORDER BY sortlist(Y) frame_opt(Z). {
1715 A = sqlite3WindowAssemble(pParse, Z, 0, Y, 0);
1717 window(A) ::= nm(W) ORDER BY sortlist(Y) frame_opt(Z). {
1718 A = sqlite3WindowAssemble(pParse, Z, 0, Y, &W);
1720 window(A) ::= frame_opt(Z). {
1721 A = Z;
1723 window(A) ::= nm(W) frame_opt(Z). {
1724 A = sqlite3WindowAssemble(pParse, Z, 0, 0, &W);
1727 frame_opt(A) ::= . {
1728 A = sqlite3WindowAlloc(pParse, 0, TK_UNBOUNDED, 0, TK_CURRENT, 0, 0);
1730 frame_opt(A) ::= range_or_rows(X) frame_bound_s(Y) frame_exclude_opt(Z). {
1731 A = sqlite3WindowAlloc(pParse, X, Y.eType, Y.pExpr, TK_CURRENT, 0, Z);
1733 frame_opt(A) ::= range_or_rows(X) BETWEEN frame_bound_s(Y) AND
1734 frame_bound_e(Z) frame_exclude_opt(W). {
1735 A = sqlite3WindowAlloc(pParse, X, Y.eType, Y.pExpr, Z.eType, Z.pExpr, W);
1738 range_or_rows(A) ::= RANGE|ROWS|GROUPS(X). {A = @X; /*A-overwrites-X*/}
1740 frame_bound_s(A) ::= frame_bound(X). {A = X;}
1741 frame_bound_s(A) ::= UNBOUNDED(X) PRECEDING. {A.eType = @X; A.pExpr = 0;}
1742 frame_bound_e(A) ::= frame_bound(X). {A = X;}
1743 frame_bound_e(A) ::= UNBOUNDED(X) FOLLOWING. {A.eType = @X; A.pExpr = 0;}
1745 frame_bound(A) ::= expr(X) PRECEDING|FOLLOWING(Y).
1746 {A.eType = @Y; A.pExpr = X;}
1747 frame_bound(A) ::= CURRENT(X) ROW. {A.eType = @X; A.pExpr = 0;}
1749 %type frame_exclude_opt {u8}
1750 frame_exclude_opt(A) ::= . {A = 0;}
1751 frame_exclude_opt(A) ::= EXCLUDE frame_exclude(X). {A = X;}
1753 %type frame_exclude {u8}
1754 frame_exclude(A) ::= NO(X) OTHERS. {A = @X; /*A-overwrites-X*/}
1755 frame_exclude(A) ::= CURRENT(X) ROW. {A = @X; /*A-overwrites-X*/}
1756 frame_exclude(A) ::= GROUP|TIES(X). {A = @X; /*A-overwrites-X*/}
1759 %type window_clause {Window*}
1760 %destructor window_clause {sqlite3WindowListDelete(pParse->db, $$);}
1761 window_clause(A) ::= WINDOW windowdefn_list(B). { A = B; }
1763 filter_over(A) ::= filter_clause(F) over_clause(O). {
1764 O->pFilter = F;
1765 A = O;
1767 filter_over(A) ::= over_clause(O). {
1768 A = O;
1770 filter_over(A) ::= filter_clause(F). {
1771 A = (Window*)sqlite3DbMallocZero(pParse->db, sizeof(Window));
1772 if( A ){
1773 A->eFrmType = TK_FILTER;
1774 A->pFilter = F;
1775 }else{
1776 sqlite3ExprDelete(pParse->db, F);
1780 over_clause(A) ::= OVER LP window(Z) RP. {
1781 A = Z;
1782 assert( A!=0 );
1784 over_clause(A) ::= OVER nm(Z). {
1785 A = (Window*)sqlite3DbMallocZero(pParse->db, sizeof(Window));
1786 if( A ){
1787 A->zName = sqlite3DbStrNDup(pParse->db, Z.z, Z.n);
1791 filter_clause(A) ::= FILTER LP WHERE expr(X) RP. { A = X; }
1792 %endif /* SQLITE_OMIT_WINDOWFUNC */
1795 ** The code generator needs some extra TK_ token values for tokens that
1796 ** are synthesized and do not actually appear in the grammar:
1798 %token
1799 COLUMN /* Reference to a table column */
1800 AGG_FUNCTION /* An aggregate function */
1801 AGG_COLUMN /* An aggregated column */
1802 TRUEFALSE /* True or false keyword */
1803 ISNOT /* Combination of IS and NOT */
1804 FUNCTION /* A function invocation */
1805 UMINUS /* Unary minus */
1806 UPLUS /* Unary plus */
1807 TRUTH /* IS TRUE or IS FALSE or IS NOT TRUE or IS NOT FALSE */
1808 REGISTER /* Reference to a VDBE register */
1809 VECTOR /* Vector */
1810 SELECT_COLUMN /* Choose a single column from a multi-column SELECT */
1811 IF_NULL_ROW /* the if-null-row operator */
1812 ASTERISK /* The "*" in count(*) and similar */
1813 SPAN /* The span operator */
1815 /* There must be no more than 255 tokens defined above. If this grammar
1816 ** is extended with new rules and tokens, they must either be so few in
1817 ** number that TK_SPAN is no more than 255, or else the new tokens must
1818 ** appear after this line.
1820 %include {
1821 #if TK_SPAN>255
1822 # error too many tokens in the grammar
1823 #endif
1827 ** The TK_SPACE and TK_ILLEGAL tokens must be the last two tokens. The
1828 ** parser depends on this. Those tokens are not used in any grammar rule.
1829 ** They are only used by the tokenizer. Declare them last so that they
1830 ** are guaranteed to be the last two tokens
1832 %token SPACE ILLEGAL.