Add the SQLITE_DBCONFIG_RESET_DATABASE control for resetting a corrupt
[sqlite.git] / src / parse.y
blobaca3bfb1c6048138191bda9db2408b8acdc4f28f
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; };
103 ** Disable lookaside memory allocation for objects that might be
104 ** shared across database connections.
106 static void disableLookaside(Parse *pParse){
107 pParse->disableLookaside++;
108 pParse->db->lookaside.bDisable++;
111 } // end %include
113 // Input is a single SQL command
114 input ::= cmdlist.
115 cmdlist ::= cmdlist ecmd.
116 cmdlist ::= ecmd.
117 ecmd ::= SEMI.
118 ecmd ::= cmdx SEMI.
119 %ifndef SQLITE_OMIT_EXPLAIN
120 ecmd ::= explain cmdx.
121 explain ::= EXPLAIN. { pParse->explain = 1; }
122 explain ::= EXPLAIN QUERY PLAN. { pParse->explain = 2; }
123 %endif SQLITE_OMIT_EXPLAIN
124 cmdx ::= cmd. { sqlite3FinishCoding(pParse); }
126 ///////////////////// Begin and end transactions. ////////////////////////////
129 cmd ::= BEGIN transtype(Y) trans_opt. {sqlite3BeginTransaction(pParse, Y);}
130 trans_opt ::= .
131 trans_opt ::= TRANSACTION.
132 trans_opt ::= TRANSACTION nm.
133 %type transtype {int}
134 transtype(A) ::= . {A = TK_DEFERRED;}
135 transtype(A) ::= DEFERRED(X). {A = @X; /*A-overwrites-X*/}
136 transtype(A) ::= IMMEDIATE(X). {A = @X; /*A-overwrites-X*/}
137 transtype(A) ::= EXCLUSIVE(X). {A = @X; /*A-overwrites-X*/}
138 cmd ::= COMMIT|END(X) trans_opt. {sqlite3EndTransaction(pParse,@X);}
139 cmd ::= ROLLBACK(X) trans_opt. {sqlite3EndTransaction(pParse,@X);}
141 savepoint_opt ::= SAVEPOINT.
142 savepoint_opt ::= .
143 cmd ::= SAVEPOINT nm(X). {
144 sqlite3Savepoint(pParse, SAVEPOINT_BEGIN, &X);
146 cmd ::= RELEASE savepoint_opt nm(X). {
147 sqlite3Savepoint(pParse, SAVEPOINT_RELEASE, &X);
149 cmd ::= ROLLBACK trans_opt TO savepoint_opt nm(X). {
150 sqlite3Savepoint(pParse, SAVEPOINT_ROLLBACK, &X);
153 ///////////////////// The CREATE TABLE statement ////////////////////////////
155 cmd ::= create_table create_table_args.
156 create_table ::= createkw temp(T) TABLE ifnotexists(E) nm(Y) dbnm(Z). {
157 sqlite3StartTable(pParse,&Y,&Z,T,0,0,E);
159 createkw(A) ::= CREATE(A). {disableLookaside(pParse);}
161 %type ifnotexists {int}
162 ifnotexists(A) ::= . {A = 0;}
163 ifnotexists(A) ::= IF NOT EXISTS. {A = 1;}
164 %type temp {int}
165 %ifndef SQLITE_OMIT_TEMPDB
166 temp(A) ::= TEMP. {A = 1;}
167 %endif SQLITE_OMIT_TEMPDB
168 temp(A) ::= . {A = 0;}
169 create_table_args ::= LP columnlist conslist_opt(X) RP(E) table_options(F). {
170 sqlite3EndTable(pParse,&X,&E,F,0);
172 create_table_args ::= AS select(S). {
173 sqlite3EndTable(pParse,0,0,0,S);
174 sqlite3SelectDelete(pParse->db, S);
176 %type table_options {int}
177 table_options(A) ::= . {A = 0;}
178 table_options(A) ::= WITHOUT nm(X). {
179 if( X.n==5 && sqlite3_strnicmp(X.z,"rowid",5)==0 ){
180 A = TF_WithoutRowid | TF_NoVisibleRowid;
181 }else{
182 A = 0;
183 sqlite3ErrorMsg(pParse, "unknown table option: %.*s", X.n, X.z);
186 columnlist ::= columnlist COMMA columnname carglist.
187 columnlist ::= columnname carglist.
188 columnname(A) ::= nm(A) typetoken(Y). {sqlite3AddColumn(pParse,&A,&Y);}
190 // Declare some tokens early in order to influence their values, to
191 // improve performance and reduce the executable size. The goal here is
192 // to get the "jump" operations in ISNULL through ESCAPE to have numeric
193 // values that are early enough so that all jump operations are clustered
194 // at the beginning, but also so that the comparison tokens NE through GE
195 // are as large as possible so that they are near to FUNCTION, which is a
196 // token synthesized by addopcodes.tcl.
198 %token ABORT ACTION AFTER ANALYZE ASC ATTACH BEFORE BEGIN BY CASCADE CAST.
199 %token CONFLICT DATABASE DEFERRED DESC DETACH EACH END EXCLUSIVE EXPLAIN FAIL.
200 %token OR AND NOT IS MATCH LIKE_KW BETWEEN IN ISNULL NOTNULL NE EQ.
201 %token GT LE LT GE ESCAPE.
203 // The following directive causes tokens ABORT, AFTER, ASC, etc. to
204 // fallback to ID if they will not parse as their original value.
205 // This obviates the need for the "id" nonterminal.
207 %fallback ID
208 ABORT ACTION AFTER ANALYZE ASC ATTACH BEFORE BEGIN BY CASCADE CAST COLUMNKW
209 CONFLICT DATABASE DEFERRED DESC DETACH DO
210 EACH END EXCLUSIVE EXPLAIN FAIL FOR
211 IGNORE IMMEDIATE INITIALLY INSTEAD LIKE_KW MATCH NO PLAN
212 QUERY KEY OF OFFSET PRAGMA RAISE RECURSIVE RELEASE REPLACE RESTRICT ROW
213 ROLLBACK SAVEPOINT TEMP TRIGGER VACUUM VIEW VIRTUAL WITH WITHOUT
214 %ifdef SQLITE_OMIT_COMPOUND_SELECT
215 EXCEPT INTERSECT UNION
216 %endif SQLITE_OMIT_COMPOUND_SELECT
217 REINDEX RENAME CTIME_KW IF
219 %wildcard ANY.
221 // Define operator precedence early so that this is the first occurrence
222 // of the operator tokens in the grammer. Keeping the operators together
223 // causes them to be assigned integer values that are close together,
224 // which keeps parser tables smaller.
226 // The token values assigned to these symbols is determined by the order
227 // in which lemon first sees them. It must be the case that ISNULL/NOTNULL,
228 // NE/EQ, GT/LE, and GE/LT are separated by only a single value. See
229 // the sqlite3ExprIfFalse() routine for additional information on this
230 // constraint.
232 %left OR.
233 %left AND.
234 %right NOT.
235 %left IS MATCH LIKE_KW BETWEEN IN ISNULL NOTNULL NE EQ.
236 %left GT LE LT GE.
237 %right ESCAPE.
238 %left BITAND BITOR LSHIFT RSHIFT.
239 %left PLUS MINUS.
240 %left STAR SLASH REM.
241 %left CONCAT.
242 %left COLLATE.
243 %right BITNOT.
244 %nonassoc ON.
246 // An IDENTIFIER can be a generic identifier, or one of several
247 // keywords. Any non-standard keyword can also be an identifier.
249 %token_class id ID|INDEXED.
252 // And "ids" is an identifer-or-string.
254 %token_class ids ID|STRING.
256 // The name of a column or table can be any of the following:
258 %type nm {Token}
259 nm(A) ::= id(A).
260 nm(A) ::= STRING(A).
261 nm(A) ::= JOIN_KW(A).
263 // A typetoken is really zero or more tokens that form a type name such
264 // as can be found after the column name in a CREATE TABLE statement.
265 // Multiple tokens are concatenated to form the value of the typetoken.
267 %type typetoken {Token}
268 typetoken(A) ::= . {A.n = 0; A.z = 0;}
269 typetoken(A) ::= typename(A).
270 typetoken(A) ::= typename(A) LP signed RP(Y). {
271 A.n = (int)(&Y.z[Y.n] - A.z);
273 typetoken(A) ::= typename(A) LP signed COMMA signed RP(Y). {
274 A.n = (int)(&Y.z[Y.n] - A.z);
276 %type typename {Token}
277 typename(A) ::= ids(A).
278 typename(A) ::= typename(A) ids(Y). {A.n=Y.n+(int)(Y.z-A.z);}
279 signed ::= plus_num.
280 signed ::= minus_num.
282 // The scanpt non-terminal takes a value which is a pointer to the
283 // input text just past the last token that has been shifted into
284 // the parser. By surrounding some phrase in the grammar with two
285 // scanpt non-terminals, we can capture the input text for that phrase.
286 // For example:
288 // something ::= .... scanpt(A) phrase scanpt(Z).
290 // The text that is parsed as "phrase" is a string starting at A
291 // and containing (int)(Z-A) characters. There might be some extra
292 // whitespace on either end of the text, but that can be removed in
293 // post-processing, if needed.
295 %type scanpt {const char*}
296 scanpt(A) ::= . {
297 assert( yyLookahead!=YYNOCODE );
298 A = yyLookaheadToken.z;
301 // "carglist" is a list of additional constraints that come after the
302 // column name and column type in a CREATE TABLE statement.
304 carglist ::= carglist ccons.
305 carglist ::= .
306 ccons ::= CONSTRAINT nm(X). {pParse->constraintName = X;}
307 ccons ::= DEFAULT scanpt(A) term(X) scanpt(Z).
308 {sqlite3AddDefaultValue(pParse,X,A,Z);}
309 ccons ::= DEFAULT LP(A) expr(X) RP(Z).
310 {sqlite3AddDefaultValue(pParse,X,A.z+1,Z.z);}
311 ccons ::= DEFAULT PLUS(A) term(X) scanpt(Z).
312 {sqlite3AddDefaultValue(pParse,X,A.z,Z);}
313 ccons ::= DEFAULT MINUS(A) term(X) scanpt(Z). {
314 Expr *p = sqlite3PExpr(pParse, TK_UMINUS, X, 0);
315 sqlite3AddDefaultValue(pParse,p,A.z,Z);
317 ccons ::= DEFAULT scanpt id(X). {
318 Expr *p = tokenExpr(pParse, TK_STRING, X);
319 if( p ){
320 sqlite3ExprIdToTrueFalse(p);
321 testcase( p->op==TK_TRUEFALSE && sqlite3ExprTruthValue(p) );
323 sqlite3AddDefaultValue(pParse,p,X.z,X.z+X.n);
326 // In addition to the type name, we also care about the primary key and
327 // UNIQUE constraints.
329 ccons ::= NULL onconf.
330 ccons ::= NOT NULL onconf(R). {sqlite3AddNotNull(pParse, R);}
331 ccons ::= PRIMARY KEY sortorder(Z) onconf(R) autoinc(I).
332 {sqlite3AddPrimaryKey(pParse,0,R,I,Z);}
333 ccons ::= UNIQUE onconf(R). {sqlite3CreateIndex(pParse,0,0,0,0,R,0,0,0,0,
334 SQLITE_IDXTYPE_UNIQUE);}
335 ccons ::= CHECK LP expr(X) RP. {sqlite3AddCheckConstraint(pParse,X);}
336 ccons ::= REFERENCES nm(T) eidlist_opt(TA) refargs(R).
337 {sqlite3CreateForeignKey(pParse,0,&T,TA,R);}
338 ccons ::= defer_subclause(D). {sqlite3DeferForeignKey(pParse,D);}
339 ccons ::= COLLATE ids(C). {sqlite3AddCollateType(pParse, &C);}
341 // The optional AUTOINCREMENT keyword
342 %type autoinc {int}
343 autoinc(X) ::= . {X = 0;}
344 autoinc(X) ::= AUTOINCR. {X = 1;}
346 // The next group of rules parses the arguments to a REFERENCES clause
347 // that determine if the referential integrity checking is deferred or
348 // or immediate and which determine what action to take if a ref-integ
349 // check fails.
351 %type refargs {int}
352 refargs(A) ::= . { A = OE_None*0x0101; /* EV: R-19803-45884 */}
353 refargs(A) ::= refargs(A) refarg(Y). { A = (A & ~Y.mask) | Y.value; }
354 %type refarg {struct {int value; int mask;}}
355 refarg(A) ::= MATCH nm. { A.value = 0; A.mask = 0x000000; }
356 refarg(A) ::= ON INSERT refact. { A.value = 0; A.mask = 0x000000; }
357 refarg(A) ::= ON DELETE refact(X). { A.value = X; A.mask = 0x0000ff; }
358 refarg(A) ::= ON UPDATE refact(X). { A.value = X<<8; A.mask = 0x00ff00; }
359 %type refact {int}
360 refact(A) ::= SET NULL. { A = OE_SetNull; /* EV: R-33326-45252 */}
361 refact(A) ::= SET DEFAULT. { A = OE_SetDflt; /* EV: R-33326-45252 */}
362 refact(A) ::= CASCADE. { A = OE_Cascade; /* EV: R-33326-45252 */}
363 refact(A) ::= RESTRICT. { A = OE_Restrict; /* EV: R-33326-45252 */}
364 refact(A) ::= NO ACTION. { A = OE_None; /* EV: R-33326-45252 */}
365 %type defer_subclause {int}
366 defer_subclause(A) ::= NOT DEFERRABLE init_deferred_pred_opt. {A = 0;}
367 defer_subclause(A) ::= DEFERRABLE init_deferred_pred_opt(X). {A = X;}
368 %type init_deferred_pred_opt {int}
369 init_deferred_pred_opt(A) ::= . {A = 0;}
370 init_deferred_pred_opt(A) ::= INITIALLY DEFERRED. {A = 1;}
371 init_deferred_pred_opt(A) ::= INITIALLY IMMEDIATE. {A = 0;}
373 conslist_opt(A) ::= . {A.n = 0; A.z = 0;}
374 conslist_opt(A) ::= COMMA(A) conslist.
375 conslist ::= conslist tconscomma tcons.
376 conslist ::= tcons.
377 tconscomma ::= COMMA. {pParse->constraintName.n = 0;}
378 tconscomma ::= .
379 tcons ::= CONSTRAINT nm(X). {pParse->constraintName = X;}
380 tcons ::= PRIMARY KEY LP sortlist(X) autoinc(I) RP onconf(R).
381 {sqlite3AddPrimaryKey(pParse,X,R,I,0);}
382 tcons ::= UNIQUE LP sortlist(X) RP onconf(R).
383 {sqlite3CreateIndex(pParse,0,0,0,X,R,0,0,0,0,
384 SQLITE_IDXTYPE_UNIQUE);}
385 tcons ::= CHECK LP expr(E) RP onconf.
386 {sqlite3AddCheckConstraint(pParse,E);}
387 tcons ::= FOREIGN KEY LP eidlist(FA) RP
388 REFERENCES nm(T) eidlist_opt(TA) refargs(R) defer_subclause_opt(D). {
389 sqlite3CreateForeignKey(pParse, FA, &T, TA, R);
390 sqlite3DeferForeignKey(pParse, D);
392 %type defer_subclause_opt {int}
393 defer_subclause_opt(A) ::= . {A = 0;}
394 defer_subclause_opt(A) ::= defer_subclause(A).
396 // The following is a non-standard extension that allows us to declare the
397 // default behavior when there is a constraint conflict.
399 %type onconf {int}
400 %type orconf {int}
401 %type resolvetype {int}
402 onconf(A) ::= . {A = OE_Default;}
403 onconf(A) ::= ON CONFLICT resolvetype(X). {A = X;}
404 orconf(A) ::= . {A = OE_Default;}
405 orconf(A) ::= OR resolvetype(X). {A = X;}
406 resolvetype(A) ::= raisetype(A).
407 resolvetype(A) ::= IGNORE. {A = OE_Ignore;}
408 resolvetype(A) ::= REPLACE. {A = OE_Replace;}
410 ////////////////////////// The DROP TABLE /////////////////////////////////////
412 cmd ::= DROP TABLE ifexists(E) fullname(X). {
413 sqlite3DropTable(pParse, X, 0, E);
415 %type ifexists {int}
416 ifexists(A) ::= IF EXISTS. {A = 1;}
417 ifexists(A) ::= . {A = 0;}
419 ///////////////////// The CREATE VIEW statement /////////////////////////////
421 %ifndef SQLITE_OMIT_VIEW
422 cmd ::= createkw(X) temp(T) VIEW ifnotexists(E) nm(Y) dbnm(Z) eidlist_opt(C)
423 AS select(S). {
424 sqlite3CreateView(pParse, &X, &Y, &Z, C, S, T, E);
426 cmd ::= DROP VIEW ifexists(E) fullname(X). {
427 sqlite3DropTable(pParse, X, 1, E);
429 %endif SQLITE_OMIT_VIEW
431 //////////////////////// The SELECT statement /////////////////////////////////
433 cmd ::= select(X). {
434 SelectDest dest = {SRT_Output, 0, 0, 0, 0, 0};
435 sqlite3Select(pParse, X, &dest);
436 sqlite3SelectDelete(pParse->db, X);
439 %type select {Select*}
440 %destructor select {sqlite3SelectDelete(pParse->db, $$);}
441 %type selectnowith {Select*}
442 %destructor selectnowith {sqlite3SelectDelete(pParse->db, $$);}
443 %type oneselect {Select*}
444 %destructor oneselect {sqlite3SelectDelete(pParse->db, $$);}
446 %include {
448 ** For a compound SELECT statement, make sure p->pPrior->pNext==p for
449 ** all elements in the list. And make sure list length does not exceed
450 ** SQLITE_LIMIT_COMPOUND_SELECT.
452 static void parserDoubleLinkSelect(Parse *pParse, Select *p){
453 if( p->pPrior ){
454 Select *pNext = 0, *pLoop;
455 int mxSelect, cnt = 0;
456 for(pLoop=p; pLoop; pNext=pLoop, pLoop=pLoop->pPrior, cnt++){
457 pLoop->pNext = pNext;
458 pLoop->selFlags |= SF_Compound;
460 if( (p->selFlags & SF_MultiValue)==0 &&
461 (mxSelect = pParse->db->aLimit[SQLITE_LIMIT_COMPOUND_SELECT])>0 &&
462 cnt>mxSelect
464 sqlite3ErrorMsg(pParse, "too many terms in compound SELECT");
470 select(A) ::= WITH wqlist(W) selectnowith(X). {
471 Select *p = X;
472 if( p ){
473 p->pWith = W;
474 parserDoubleLinkSelect(pParse, p);
475 }else{
476 sqlite3WithDelete(pParse->db, W);
478 A = p;
480 select(A) ::= WITH RECURSIVE wqlist(W) selectnowith(X). {
481 Select *p = X;
482 if( p ){
483 p->pWith = W;
484 parserDoubleLinkSelect(pParse, p);
485 }else{
486 sqlite3WithDelete(pParse->db, W);
488 A = p;
490 select(A) ::= selectnowith(X). {
491 Select *p = X;
492 if( p ){
493 parserDoubleLinkSelect(pParse, p);
495 A = p; /*A-overwrites-X*/
498 selectnowith(A) ::= oneselect(A).
499 %ifndef SQLITE_OMIT_COMPOUND_SELECT
500 selectnowith(A) ::= selectnowith(A) multiselect_op(Y) oneselect(Z). {
501 Select *pRhs = Z;
502 Select *pLhs = A;
503 if( pRhs && pRhs->pPrior ){
504 SrcList *pFrom;
505 Token x;
506 x.n = 0;
507 parserDoubleLinkSelect(pParse, pRhs);
508 pFrom = sqlite3SrcListAppendFromTerm(pParse,0,0,0,&x,pRhs,0,0);
509 pRhs = sqlite3SelectNew(pParse,0,pFrom,0,0,0,0,0,0);
511 if( pRhs ){
512 pRhs->op = (u8)Y;
513 pRhs->pPrior = pLhs;
514 if( ALWAYS(pLhs) ) pLhs->selFlags &= ~SF_MultiValue;
515 pRhs->selFlags &= ~SF_MultiValue;
516 if( Y!=TK_ALL ) pParse->hasCompound = 1;
517 }else{
518 sqlite3SelectDelete(pParse->db, pLhs);
520 A = pRhs;
522 %type multiselect_op {int}
523 multiselect_op(A) ::= UNION(OP). {A = @OP; /*A-overwrites-OP*/}
524 multiselect_op(A) ::= UNION ALL. {A = TK_ALL;}
525 multiselect_op(A) ::= EXCEPT|INTERSECT(OP). {A = @OP; /*A-overwrites-OP*/}
526 %endif SQLITE_OMIT_COMPOUND_SELECT
527 oneselect(A) ::= SELECT(S) distinct(D) selcollist(W) from(X) where_opt(Y)
528 groupby_opt(P) having_opt(Q) orderby_opt(Z) limit_opt(L). {
529 #if SELECTTRACE_ENABLED
530 Token s = S; /*A-overwrites-S*/
531 #endif
532 A = sqlite3SelectNew(pParse,W,X,Y,P,Q,Z,D,L);
533 #if SELECTTRACE_ENABLED
534 /* Populate the Select.zSelName[] string that is used to help with
535 ** query planner debugging, to differentiate between multiple Select
536 ** objects in a complex query.
538 ** If the SELECT keyword is immediately followed by a C-style comment
539 ** then extract the first few alphanumeric characters from within that
540 ** comment to be the zSelName value. Otherwise, the label is #N where
541 ** is an integer that is incremented with each SELECT statement seen.
543 if( A!=0 ){
544 const char *z = s.z+6;
545 int i;
546 sqlite3_snprintf(sizeof(A->zSelName), A->zSelName,"#%d",++pParse->nSelect);
547 while( z[0]==' ' ) z++;
548 if( z[0]=='/' && z[1]=='*' ){
549 z += 2;
550 while( z[0]==' ' ) z++;
551 for(i=0; sqlite3Isalnum(z[i]); i++){}
552 sqlite3_snprintf(sizeof(A->zSelName), A->zSelName, "%.*s", i, z);
555 #endif /* SELECTRACE_ENABLED */
557 oneselect(A) ::= values(A).
559 %type values {Select*}
560 %destructor values {sqlite3SelectDelete(pParse->db, $$);}
561 values(A) ::= VALUES LP nexprlist(X) RP. {
562 A = sqlite3SelectNew(pParse,X,0,0,0,0,0,SF_Values,0);
564 values(A) ::= values(A) COMMA LP exprlist(Y) RP. {
565 Select *pRight, *pLeft = A;
566 pRight = sqlite3SelectNew(pParse,Y,0,0,0,0,0,SF_Values|SF_MultiValue,0);
567 if( ALWAYS(pLeft) ) pLeft->selFlags &= ~SF_MultiValue;
568 if( pRight ){
569 pRight->op = TK_ALL;
570 pRight->pPrior = pLeft;
571 A = pRight;
572 }else{
573 A = pLeft;
577 // The "distinct" nonterminal is true (1) if the DISTINCT keyword is
578 // present and false (0) if it is not.
580 %type distinct {int}
581 distinct(A) ::= DISTINCT. {A = SF_Distinct;}
582 distinct(A) ::= ALL. {A = SF_All;}
583 distinct(A) ::= . {A = 0;}
585 // selcollist is a list of expressions that are to become the return
586 // values of the SELECT statement. The "*" in statements like
587 // "SELECT * FROM ..." is encoded as a special expression with an
588 // opcode of TK_ASTERISK.
590 %type selcollist {ExprList*}
591 %destructor selcollist {sqlite3ExprListDelete(pParse->db, $$);}
592 %type sclp {ExprList*}
593 %destructor sclp {sqlite3ExprListDelete(pParse->db, $$);}
594 sclp(A) ::= selcollist(A) COMMA.
595 sclp(A) ::= . {A = 0;}
596 selcollist(A) ::= sclp(A) scanpt(B) expr(X) scanpt(Z) as(Y). {
597 A = sqlite3ExprListAppend(pParse, A, X);
598 if( Y.n>0 ) sqlite3ExprListSetName(pParse, A, &Y, 1);
599 sqlite3ExprListSetSpan(pParse,A,B,Z);
601 selcollist(A) ::= sclp(A) scanpt STAR. {
602 Expr *p = sqlite3Expr(pParse->db, TK_ASTERISK, 0);
603 A = sqlite3ExprListAppend(pParse, A, p);
605 selcollist(A) ::= sclp(A) scanpt nm(X) DOT STAR. {
606 Expr *pRight = sqlite3PExpr(pParse, TK_ASTERISK, 0, 0);
607 Expr *pLeft = sqlite3ExprAlloc(pParse->db, TK_ID, &X, 1);
608 Expr *pDot = sqlite3PExpr(pParse, TK_DOT, pLeft, pRight);
609 A = sqlite3ExprListAppend(pParse,A, pDot);
612 // An option "AS <id>" phrase that can follow one of the expressions that
613 // define the result set, or one of the tables in the FROM clause.
615 %type as {Token}
616 as(X) ::= AS nm(Y). {X = Y;}
617 as(X) ::= ids(X).
618 as(X) ::= . {X.n = 0; X.z = 0;}
621 %type seltablist {SrcList*}
622 %destructor seltablist {sqlite3SrcListDelete(pParse->db, $$);}
623 %type stl_prefix {SrcList*}
624 %destructor stl_prefix {sqlite3SrcListDelete(pParse->db, $$);}
625 %type from {SrcList*}
626 %destructor from {sqlite3SrcListDelete(pParse->db, $$);}
628 // A complete FROM clause.
630 from(A) ::= . {A = sqlite3DbMallocZero(pParse->db, sizeof(*A));}
631 from(A) ::= FROM seltablist(X). {
632 A = X;
633 sqlite3SrcListShiftJoinType(A);
636 // "seltablist" is a "Select Table List" - the content of the FROM clause
637 // in a SELECT statement. "stl_prefix" is a prefix of this list.
639 stl_prefix(A) ::= seltablist(A) joinop(Y). {
640 if( ALWAYS(A && A->nSrc>0) ) A->a[A->nSrc-1].fg.jointype = (u8)Y;
642 stl_prefix(A) ::= . {A = 0;}
643 seltablist(A) ::= stl_prefix(A) nm(Y) dbnm(D) as(Z) indexed_opt(I)
644 on_opt(N) using_opt(U). {
645 A = sqlite3SrcListAppendFromTerm(pParse,A,&Y,&D,&Z,0,N,U);
646 sqlite3SrcListIndexedBy(pParse, A, &I);
648 seltablist(A) ::= stl_prefix(A) nm(Y) dbnm(D) LP exprlist(E) RP as(Z)
649 on_opt(N) using_opt(U). {
650 A = sqlite3SrcListAppendFromTerm(pParse,A,&Y,&D,&Z,0,N,U);
651 sqlite3SrcListFuncArgs(pParse, A, E);
653 %ifndef SQLITE_OMIT_SUBQUERY
654 seltablist(A) ::= stl_prefix(A) LP select(S) RP
655 as(Z) on_opt(N) using_opt(U). {
656 A = sqlite3SrcListAppendFromTerm(pParse,A,0,0,&Z,S,N,U);
658 seltablist(A) ::= stl_prefix(A) LP seltablist(F) RP
659 as(Z) on_opt(N) using_opt(U). {
660 if( A==0 && Z.n==0 && N==0 && U==0 ){
661 A = F;
662 }else if( F->nSrc==1 ){
663 A = sqlite3SrcListAppendFromTerm(pParse,A,0,0,&Z,0,N,U);
664 if( A ){
665 struct SrcList_item *pNew = &A->a[A->nSrc-1];
666 struct SrcList_item *pOld = F->a;
667 pNew->zName = pOld->zName;
668 pNew->zDatabase = pOld->zDatabase;
669 pNew->pSelect = pOld->pSelect;
670 pOld->zName = pOld->zDatabase = 0;
671 pOld->pSelect = 0;
673 sqlite3SrcListDelete(pParse->db, F);
674 }else{
675 Select *pSubquery;
676 sqlite3SrcListShiftJoinType(F);
677 pSubquery = sqlite3SelectNew(pParse,0,F,0,0,0,0,SF_NestedFrom,0);
678 A = sqlite3SrcListAppendFromTerm(pParse,A,0,0,&Z,pSubquery,N,U);
681 %endif SQLITE_OMIT_SUBQUERY
683 %type dbnm {Token}
684 dbnm(A) ::= . {A.z=0; A.n=0;}
685 dbnm(A) ::= DOT nm(X). {A = X;}
687 %type fullname {SrcList*}
688 %destructor fullname {sqlite3SrcListDelete(pParse->db, $$);}
689 fullname(A) ::= nm(X).
690 {A = sqlite3SrcListAppend(pParse->db,0,&X,0); /*A-overwrites-X*/}
691 fullname(A) ::= nm(X) DOT nm(Y).
692 {A = sqlite3SrcListAppend(pParse->db,0,&X,&Y); /*A-overwrites-X*/}
694 %type xfullname {SrcList*}
695 %destructor xfullname {sqlite3SrcListDelete(pParse->db, $$);}
696 xfullname(A) ::= nm(X).
697 {A = sqlite3SrcListAppend(pParse->db,0,&X,0); /*A-overwrites-X*/}
698 xfullname(A) ::= nm(X) DOT nm(Y).
699 {A = sqlite3SrcListAppend(pParse->db,0,&X,&Y); /*A-overwrites-X*/}
700 xfullname(A) ::= nm(X) DOT nm(Y) AS nm(Z). {
701 A = sqlite3SrcListAppend(pParse->db,0,&X,&Y); /*A-overwrites-X*/
702 if( A ) A->a[0].zAlias = sqlite3NameFromToken(pParse->db, &Z);
704 xfullname(A) ::= nm(X) AS nm(Z). {
705 A = sqlite3SrcListAppend(pParse->db,0,&X,0); /*A-overwrites-X*/
706 if( A ) A->a[0].zAlias = sqlite3NameFromToken(pParse->db, &Z);
709 %type joinop {int}
710 joinop(X) ::= COMMA|JOIN. { X = JT_INNER; }
711 joinop(X) ::= JOIN_KW(A) JOIN.
712 {X = sqlite3JoinType(pParse,&A,0,0); /*X-overwrites-A*/}
713 joinop(X) ::= JOIN_KW(A) nm(B) JOIN.
714 {X = sqlite3JoinType(pParse,&A,&B,0); /*X-overwrites-A*/}
715 joinop(X) ::= JOIN_KW(A) nm(B) nm(C) JOIN.
716 {X = sqlite3JoinType(pParse,&A,&B,&C);/*X-overwrites-A*/}
718 // There is a parsing abiguity in an upsert statement that uses a
719 // SELECT on the RHS of a the INSERT:
721 // INSERT INTO tab SELECT * FROM aaa JOIN bbb ON CONFLICT ...
722 // here ----^^
724 // When the ON token is encountered, the parser does not know if it is
725 // the beginning of an ON CONFLICT clause, or the beginning of an ON
726 // clause associated with the JOIN. The conflict is resolved in favor
727 // of the JOIN. If an ON CONFLICT clause is intended, insert a dummy
728 // WHERE clause in between, like this:
730 // INSERT INTO tab SELECT * FROM aaa JOIN bbb WHERE true ON CONFLICT ...
732 // The [AND] and [OR] precedence marks in the rules for on_opt cause the
733 // ON in this context to always be interpreted as belonging to the JOIN.
735 %type on_opt {Expr*}
736 %destructor on_opt {sqlite3ExprDelete(pParse->db, $$);}
737 on_opt(N) ::= ON expr(E). {N = E;}
738 on_opt(N) ::= . [OR] {N = 0;}
740 // Note that this block abuses the Token type just a little. If there is
741 // no "INDEXED BY" clause, the returned token is empty (z==0 && n==0). If
742 // there is an INDEXED BY clause, then the token is populated as per normal,
743 // with z pointing to the token data and n containing the number of bytes
744 // in the token.
746 // If there is a "NOT INDEXED" clause, then (z==0 && n==1), which is
747 // normally illegal. The sqlite3SrcListIndexedBy() function
748 // recognizes and interprets this as a special case.
750 %type indexed_opt {Token}
751 indexed_opt(A) ::= . {A.z=0; A.n=0;}
752 indexed_opt(A) ::= INDEXED BY nm(X). {A = X;}
753 indexed_opt(A) ::= NOT INDEXED. {A.z=0; A.n=1;}
755 %type using_opt {IdList*}
756 %destructor using_opt {sqlite3IdListDelete(pParse->db, $$);}
757 using_opt(U) ::= USING LP idlist(L) RP. {U = L;}
758 using_opt(U) ::= . {U = 0;}
761 %type orderby_opt {ExprList*}
762 %destructor orderby_opt {sqlite3ExprListDelete(pParse->db, $$);}
764 // the sortlist non-terminal stores a list of expression where each
765 // expression is optionally followed by ASC or DESC to indicate the
766 // sort order.
768 %type sortlist {ExprList*}
769 %destructor sortlist {sqlite3ExprListDelete(pParse->db, $$);}
771 orderby_opt(A) ::= . {A = 0;}
772 orderby_opt(A) ::= ORDER BY sortlist(X). {A = X;}
773 sortlist(A) ::= sortlist(A) COMMA expr(Y) sortorder(Z). {
774 A = sqlite3ExprListAppend(pParse,A,Y);
775 sqlite3ExprListSetSortOrder(A,Z);
777 sortlist(A) ::= expr(Y) sortorder(Z). {
778 A = sqlite3ExprListAppend(pParse,0,Y); /*A-overwrites-Y*/
779 sqlite3ExprListSetSortOrder(A,Z);
782 %type sortorder {int}
784 sortorder(A) ::= ASC. {A = SQLITE_SO_ASC;}
785 sortorder(A) ::= DESC. {A = SQLITE_SO_DESC;}
786 sortorder(A) ::= . {A = SQLITE_SO_UNDEFINED;}
788 %type groupby_opt {ExprList*}
789 %destructor groupby_opt {sqlite3ExprListDelete(pParse->db, $$);}
790 groupby_opt(A) ::= . {A = 0;}
791 groupby_opt(A) ::= GROUP BY nexprlist(X). {A = X;}
793 %type having_opt {Expr*}
794 %destructor having_opt {sqlite3ExprDelete(pParse->db, $$);}
795 having_opt(A) ::= . {A = 0;}
796 having_opt(A) ::= HAVING expr(X). {A = X;}
798 %type limit_opt {Expr*}
800 // The destructor for limit_opt will never fire in the current grammar.
801 // The limit_opt non-terminal only occurs at the end of a single production
802 // rule for SELECT statements. As soon as the rule that create the
803 // limit_opt non-terminal reduces, the SELECT statement rule will also
804 // reduce. So there is never a limit_opt non-terminal on the stack
805 // except as a transient. So there is never anything to destroy.
807 //%destructor limit_opt {sqlite3ExprDelete(pParse->db, $$);}
808 limit_opt(A) ::= . {A = 0;}
809 limit_opt(A) ::= LIMIT expr(X).
810 {A = sqlite3PExpr(pParse,TK_LIMIT,X,0);}
811 limit_opt(A) ::= LIMIT expr(X) OFFSET expr(Y).
812 {A = sqlite3PExpr(pParse,TK_LIMIT,X,Y);}
813 limit_opt(A) ::= LIMIT expr(X) COMMA expr(Y).
814 {A = sqlite3PExpr(pParse,TK_LIMIT,Y,X);}
816 /////////////////////////// The DELETE statement /////////////////////////////
818 %ifdef SQLITE_ENABLE_UPDATE_DELETE_LIMIT
819 cmd ::= with DELETE FROM xfullname(X) indexed_opt(I) where_opt(W)
820 orderby_opt(O) limit_opt(L). {
821 sqlite3SrcListIndexedBy(pParse, X, &I);
822 sqlite3DeleteFrom(pParse,X,W,O,L);
824 %endif
825 %ifndef SQLITE_ENABLE_UPDATE_DELETE_LIMIT
826 cmd ::= with DELETE FROM xfullname(X) indexed_opt(I) where_opt(W). {
827 sqlite3SrcListIndexedBy(pParse, X, &I);
828 sqlite3DeleteFrom(pParse,X,W,0,0);
830 %endif
832 %type where_opt {Expr*}
833 %destructor where_opt {sqlite3ExprDelete(pParse->db, $$);}
835 where_opt(A) ::= . {A = 0;}
836 where_opt(A) ::= WHERE expr(X). {A = X;}
838 ////////////////////////// The UPDATE command ////////////////////////////////
840 %ifdef SQLITE_ENABLE_UPDATE_DELETE_LIMIT
841 cmd ::= with UPDATE orconf(R) xfullname(X) indexed_opt(I) SET setlist(Y)
842 where_opt(W) orderby_opt(O) limit_opt(L). {
843 sqlite3SrcListIndexedBy(pParse, X, &I);
844 sqlite3ExprListCheckLength(pParse,Y,"set list");
845 sqlite3Update(pParse,X,Y,W,R,O,L,0);
847 %endif
848 %ifndef SQLITE_ENABLE_UPDATE_DELETE_LIMIT
849 cmd ::= with UPDATE orconf(R) xfullname(X) indexed_opt(I) SET setlist(Y)
850 where_opt(W). {
851 sqlite3SrcListIndexedBy(pParse, X, &I);
852 sqlite3ExprListCheckLength(pParse,Y,"set list");
853 sqlite3Update(pParse,X,Y,W,R,0,0,0);
855 %endif
857 %type setlist {ExprList*}
858 %destructor setlist {sqlite3ExprListDelete(pParse->db, $$);}
860 setlist(A) ::= setlist(A) COMMA nm(X) EQ expr(Y). {
861 A = sqlite3ExprListAppend(pParse, A, Y);
862 sqlite3ExprListSetName(pParse, A, &X, 1);
864 setlist(A) ::= setlist(A) COMMA LP idlist(X) RP EQ expr(Y). {
865 A = sqlite3ExprListAppendVector(pParse, A, X, Y);
867 setlist(A) ::= nm(X) EQ expr(Y). {
868 A = sqlite3ExprListAppend(pParse, 0, Y);
869 sqlite3ExprListSetName(pParse, A, &X, 1);
871 setlist(A) ::= LP idlist(X) RP EQ expr(Y). {
872 A = sqlite3ExprListAppendVector(pParse, 0, X, Y);
875 ////////////////////////// The INSERT command /////////////////////////////////
877 cmd ::= with insert_cmd(R) INTO xfullname(X) idlist_opt(F) select(S)
878 upsert(U). {
879 sqlite3Insert(pParse, X, S, F, R, U);
881 cmd ::= with insert_cmd(R) INTO xfullname(X) idlist_opt(F) DEFAULT VALUES.
883 sqlite3Insert(pParse, X, 0, F, R, 0);
886 %type upsert {Upsert*}
888 // Because upsert only occurs at the tip end of the INSERT rule for cmd,
889 // there is never a case where the value of the upsert pointer will not
890 // be destroyed by the cmd action. So comment-out the destructor to
891 // avoid unreachable code.
892 //%destructor upsert {sqlite3UpsertDelete(pParse->db,$$);}
893 upsert(A) ::= . { A = 0; }
894 upsert(A) ::= ON CONFLICT LP sortlist(T) RP where_opt(TW)
895 DO UPDATE SET setlist(Z) where_opt(W).
896 { A = sqlite3UpsertNew(pParse->db,T,TW,Z,W);}
897 upsert(A) ::= ON CONFLICT LP sortlist(T) RP where_opt(TW) DO NOTHING.
898 { A = sqlite3UpsertNew(pParse->db,T,TW,0,0); }
899 upsert(A) ::= ON CONFLICT DO NOTHING.
900 { A = sqlite3UpsertNew(pParse->db,0,0,0,0); }
902 %type insert_cmd {int}
903 insert_cmd(A) ::= INSERT orconf(R). {A = R;}
904 insert_cmd(A) ::= REPLACE. {A = OE_Replace;}
906 %type idlist_opt {IdList*}
907 %destructor idlist_opt {sqlite3IdListDelete(pParse->db, $$);}
908 %type idlist {IdList*}
909 %destructor idlist {sqlite3IdListDelete(pParse->db, $$);}
911 idlist_opt(A) ::= . {A = 0;}
912 idlist_opt(A) ::= LP idlist(X) RP. {A = X;}
913 idlist(A) ::= idlist(A) COMMA nm(Y).
914 {A = sqlite3IdListAppend(pParse->db,A,&Y);}
915 idlist(A) ::= nm(Y).
916 {A = sqlite3IdListAppend(pParse->db,0,&Y); /*A-overwrites-Y*/}
918 /////////////////////////// Expression Processing /////////////////////////////
921 %type expr {Expr*}
922 %destructor expr {sqlite3ExprDelete(pParse->db, $$);}
923 %type term {Expr*}
924 %destructor term {sqlite3ExprDelete(pParse->db, $$);}
926 %include {
928 /* Construct a new Expr object from a single identifier. Use the
929 ** new Expr to populate pOut. Set the span of pOut to be the identifier
930 ** that created the expression.
932 static Expr *tokenExpr(Parse *pParse, int op, Token t){
933 Expr *p = sqlite3DbMallocRawNN(pParse->db, sizeof(Expr)+t.n+1);
934 if( p ){
935 memset(p, 0, sizeof(Expr));
936 p->op = (u8)op;
937 p->flags = EP_Leaf;
938 p->iAgg = -1;
939 p->u.zToken = (char*)&p[1];
940 memcpy(p->u.zToken, t.z, t.n);
941 p->u.zToken[t.n] = 0;
942 if( sqlite3Isquote(p->u.zToken[0]) ){
943 if( p->u.zToken[0]=='"' ) p->flags |= EP_DblQuoted;
944 sqlite3Dequote(p->u.zToken);
946 #if SQLITE_MAX_EXPR_DEPTH>0
947 p->nHeight = 1;
948 #endif
950 return p;
954 expr(A) ::= term(A).
955 expr(A) ::= LP expr(X) RP. {A = X;}
956 expr(A) ::= id(X). {A=tokenExpr(pParse,TK_ID,X); /*A-overwrites-X*/}
957 expr(A) ::= JOIN_KW(X). {A=tokenExpr(pParse,TK_ID,X); /*A-overwrites-X*/}
958 expr(A) ::= nm(X) DOT nm(Y). {
959 Expr *temp1 = sqlite3ExprAlloc(pParse->db, TK_ID, &X, 1);
960 Expr *temp2 = sqlite3ExprAlloc(pParse->db, TK_ID, &Y, 1);
961 A = sqlite3PExpr(pParse, TK_DOT, temp1, temp2);
963 expr(A) ::= nm(X) DOT nm(Y) DOT nm(Z). {
964 Expr *temp1 = sqlite3ExprAlloc(pParse->db, TK_ID, &X, 1);
965 Expr *temp2 = sqlite3ExprAlloc(pParse->db, TK_ID, &Y, 1);
966 Expr *temp3 = sqlite3ExprAlloc(pParse->db, TK_ID, &Z, 1);
967 Expr *temp4 = sqlite3PExpr(pParse, TK_DOT, temp2, temp3);
968 A = sqlite3PExpr(pParse, TK_DOT, temp1, temp4);
970 term(A) ::= NULL|FLOAT|BLOB(X). {A=tokenExpr(pParse,@X,X); /*A-overwrites-X*/}
971 term(A) ::= STRING(X). {A=tokenExpr(pParse,@X,X); /*A-overwrites-X*/}
972 term(A) ::= INTEGER(X). {
973 A = sqlite3ExprAlloc(pParse->db, TK_INTEGER, &X, 1);
975 expr(A) ::= VARIABLE(X). {
976 if( !(X.z[0]=='#' && sqlite3Isdigit(X.z[1])) ){
977 u32 n = X.n;
978 A = tokenExpr(pParse, TK_VARIABLE, X);
979 sqlite3ExprAssignVarNumber(pParse, A, n);
980 }else{
981 /* When doing a nested parse, one can include terms in an expression
982 ** that look like this: #1 #2 ... These terms refer to registers
983 ** in the virtual machine. #N is the N-th register. */
984 Token t = X; /*A-overwrites-X*/
985 assert( t.n>=2 );
986 if( pParse->nested==0 ){
987 sqlite3ErrorMsg(pParse, "near \"%T\": syntax error", &t);
988 A = 0;
989 }else{
990 A = sqlite3PExpr(pParse, TK_REGISTER, 0, 0);
991 if( A ) sqlite3GetInt32(&t.z[1], &A->iTable);
995 expr(A) ::= expr(A) COLLATE ids(C). {
996 A = sqlite3ExprAddCollateToken(pParse, A, &C, 1);
998 %ifndef SQLITE_OMIT_CAST
999 expr(A) ::= CAST LP expr(E) AS typetoken(T) RP. {
1000 A = sqlite3ExprAlloc(pParse->db, TK_CAST, &T, 1);
1001 sqlite3ExprAttachSubtrees(pParse->db, A, E, 0);
1003 %endif SQLITE_OMIT_CAST
1004 expr(A) ::= id(X) LP distinct(D) exprlist(Y) RP. {
1005 if( Y && Y->nExpr>pParse->db->aLimit[SQLITE_LIMIT_FUNCTION_ARG] ){
1006 sqlite3ErrorMsg(pParse, "too many arguments on function %T", &X);
1008 A = sqlite3ExprFunction(pParse, Y, &X);
1009 if( D==SF_Distinct && A ){
1010 A->flags |= EP_Distinct;
1013 expr(A) ::= id(X) LP STAR RP. {
1014 A = sqlite3ExprFunction(pParse, 0, &X);
1016 term(A) ::= CTIME_KW(OP). {
1017 A = sqlite3ExprFunction(pParse, 0, &OP);
1020 expr(A) ::= LP nexprlist(X) COMMA expr(Y) RP. {
1021 ExprList *pList = sqlite3ExprListAppend(pParse, X, Y);
1022 A = sqlite3PExpr(pParse, TK_VECTOR, 0, 0);
1023 if( A ){
1024 A->x.pList = pList;
1025 }else{
1026 sqlite3ExprListDelete(pParse->db, pList);
1030 expr(A) ::= expr(A) AND(OP) expr(Y). {A=sqlite3PExpr(pParse,@OP,A,Y);}
1031 expr(A) ::= expr(A) OR(OP) expr(Y). {A=sqlite3PExpr(pParse,@OP,A,Y);}
1032 expr(A) ::= expr(A) LT|GT|GE|LE(OP) expr(Y).
1033 {A=sqlite3PExpr(pParse,@OP,A,Y);}
1034 expr(A) ::= expr(A) EQ|NE(OP) expr(Y). {A=sqlite3PExpr(pParse,@OP,A,Y);}
1035 expr(A) ::= expr(A) BITAND|BITOR|LSHIFT|RSHIFT(OP) expr(Y).
1036 {A=sqlite3PExpr(pParse,@OP,A,Y);}
1037 expr(A) ::= expr(A) PLUS|MINUS(OP) expr(Y).
1038 {A=sqlite3PExpr(pParse,@OP,A,Y);}
1039 expr(A) ::= expr(A) STAR|SLASH|REM(OP) expr(Y).
1040 {A=sqlite3PExpr(pParse,@OP,A,Y);}
1041 expr(A) ::= expr(A) CONCAT(OP) expr(Y). {A=sqlite3PExpr(pParse,@OP,A,Y);}
1042 %type likeop {Token}
1043 likeop(A) ::= LIKE_KW|MATCH(A).
1044 likeop(A) ::= NOT LIKE_KW|MATCH(X). {A=X; A.n|=0x80000000; /*A-overwrite-X*/}
1045 expr(A) ::= expr(A) likeop(OP) expr(Y). [LIKE_KW] {
1046 ExprList *pList;
1047 int bNot = OP.n & 0x80000000;
1048 OP.n &= 0x7fffffff;
1049 pList = sqlite3ExprListAppend(pParse,0, Y);
1050 pList = sqlite3ExprListAppend(pParse,pList, A);
1051 A = sqlite3ExprFunction(pParse, pList, &OP);
1052 if( bNot ) A = sqlite3PExpr(pParse, TK_NOT, A, 0);
1053 if( A ) A->flags |= EP_InfixFunc;
1055 expr(A) ::= expr(A) likeop(OP) expr(Y) ESCAPE expr(E). [LIKE_KW] {
1056 ExprList *pList;
1057 int bNot = OP.n & 0x80000000;
1058 OP.n &= 0x7fffffff;
1059 pList = sqlite3ExprListAppend(pParse,0, Y);
1060 pList = sqlite3ExprListAppend(pParse,pList, A);
1061 pList = sqlite3ExprListAppend(pParse,pList, E);
1062 A = sqlite3ExprFunction(pParse, pList, &OP);
1063 if( bNot ) A = sqlite3PExpr(pParse, TK_NOT, A, 0);
1064 if( A ) A->flags |= EP_InfixFunc;
1067 expr(A) ::= expr(A) ISNULL|NOTNULL(E). {A = sqlite3PExpr(pParse,@E,A,0);}
1068 expr(A) ::= expr(A) NOT NULL. {A = sqlite3PExpr(pParse,TK_NOTNULL,A,0);}
1070 %include {
1071 /* A routine to convert a binary TK_IS or TK_ISNOT expression into a
1072 ** unary TK_ISNULL or TK_NOTNULL expression. */
1073 static void binaryToUnaryIfNull(Parse *pParse, Expr *pY, Expr *pA, int op){
1074 sqlite3 *db = pParse->db;
1075 if( pA && pY && pY->op==TK_NULL ){
1076 pA->op = (u8)op;
1077 sqlite3ExprDelete(db, pA->pRight);
1078 pA->pRight = 0;
1083 // expr1 IS expr2
1084 // expr1 IS NOT expr2
1086 // If expr2 is NULL then code as TK_ISNULL or TK_NOTNULL. If expr2
1087 // is any other expression, code as TK_IS or TK_ISNOT.
1089 expr(A) ::= expr(A) IS expr(Y). {
1090 A = sqlite3PExpr(pParse,TK_IS,A,Y);
1091 binaryToUnaryIfNull(pParse, Y, A, TK_ISNULL);
1093 expr(A) ::= expr(A) IS NOT expr(Y). {
1094 A = sqlite3PExpr(pParse,TK_ISNOT,A,Y);
1095 binaryToUnaryIfNull(pParse, Y, A, TK_NOTNULL);
1098 expr(A) ::= NOT(B) expr(X).
1099 {A = sqlite3PExpr(pParse, @B, X, 0);/*A-overwrites-B*/}
1100 expr(A) ::= BITNOT(B) expr(X).
1101 {A = sqlite3PExpr(pParse, @B, X, 0);/*A-overwrites-B*/}
1102 expr(A) ::= MINUS expr(X). [BITNOT]
1103 {A = sqlite3PExpr(pParse, TK_UMINUS, X, 0);}
1104 expr(A) ::= PLUS expr(X). [BITNOT]
1105 {A = sqlite3PExpr(pParse, TK_UPLUS, X, 0);}
1107 %type between_op {int}
1108 between_op(A) ::= BETWEEN. {A = 0;}
1109 between_op(A) ::= NOT BETWEEN. {A = 1;}
1110 expr(A) ::= expr(A) between_op(N) expr(X) AND expr(Y). [BETWEEN] {
1111 ExprList *pList = sqlite3ExprListAppend(pParse,0, X);
1112 pList = sqlite3ExprListAppend(pParse,pList, Y);
1113 A = sqlite3PExpr(pParse, TK_BETWEEN, A, 0);
1114 if( A ){
1115 A->x.pList = pList;
1116 }else{
1117 sqlite3ExprListDelete(pParse->db, pList);
1119 if( N ) A = sqlite3PExpr(pParse, TK_NOT, A, 0);
1121 %ifndef SQLITE_OMIT_SUBQUERY
1122 %type in_op {int}
1123 in_op(A) ::= IN. {A = 0;}
1124 in_op(A) ::= NOT IN. {A = 1;}
1125 expr(A) ::= expr(A) in_op(N) LP exprlist(Y) RP. [IN] {
1126 if( Y==0 ){
1127 /* Expressions of the form
1129 ** expr1 IN ()
1130 ** expr1 NOT IN ()
1132 ** simplify to constants 0 (false) and 1 (true), respectively,
1133 ** regardless of the value of expr1.
1135 sqlite3ExprDelete(pParse->db, A);
1136 A = sqlite3ExprAlloc(pParse->db, TK_INTEGER,&sqlite3IntTokens[N],1);
1137 }else if( Y->nExpr==1 ){
1138 /* Expressions of the form:
1140 ** expr1 IN (?1)
1141 ** expr1 NOT IN (?2)
1143 ** with exactly one value on the RHS can be simplified to something
1144 ** like this:
1146 ** expr1 == ?1
1147 ** expr1 <> ?2
1149 ** But, the RHS of the == or <> is marked with the EP_Generic flag
1150 ** so that it may not contribute to the computation of comparison
1151 ** affinity or the collating sequence to use for comparison. Otherwise,
1152 ** the semantics would be subtly different from IN or NOT IN.
1154 Expr *pRHS = Y->a[0].pExpr;
1155 Y->a[0].pExpr = 0;
1156 sqlite3ExprListDelete(pParse->db, Y);
1157 /* pRHS cannot be NULL because a malloc error would have been detected
1158 ** before now and control would have never reached this point */
1159 if( ALWAYS(pRHS) ){
1160 pRHS->flags &= ~EP_Collate;
1161 pRHS->flags |= EP_Generic;
1163 A = sqlite3PExpr(pParse, N ? TK_NE : TK_EQ, A, pRHS);
1164 }else{
1165 A = sqlite3PExpr(pParse, TK_IN, A, 0);
1166 if( A ){
1167 A->x.pList = Y;
1168 sqlite3ExprSetHeightAndFlags(pParse, A);
1169 }else{
1170 sqlite3ExprListDelete(pParse->db, Y);
1172 if( N ) A = sqlite3PExpr(pParse, TK_NOT, A, 0);
1175 expr(A) ::= LP select(X) RP. {
1176 A = sqlite3PExpr(pParse, TK_SELECT, 0, 0);
1177 sqlite3PExprAddSelect(pParse, A, X);
1179 expr(A) ::= expr(A) in_op(N) LP select(Y) RP. [IN] {
1180 A = sqlite3PExpr(pParse, TK_IN, A, 0);
1181 sqlite3PExprAddSelect(pParse, A, Y);
1182 if( N ) A = sqlite3PExpr(pParse, TK_NOT, A, 0);
1184 expr(A) ::= expr(A) in_op(N) nm(Y) dbnm(Z) paren_exprlist(E). [IN] {
1185 SrcList *pSrc = sqlite3SrcListAppend(pParse->db, 0,&Y,&Z);
1186 Select *pSelect = sqlite3SelectNew(pParse, 0,pSrc,0,0,0,0,0,0);
1187 if( E ) sqlite3SrcListFuncArgs(pParse, pSelect ? pSrc : 0, E);
1188 A = sqlite3PExpr(pParse, TK_IN, A, 0);
1189 sqlite3PExprAddSelect(pParse, A, pSelect);
1190 if( N ) A = sqlite3PExpr(pParse, TK_NOT, A, 0);
1192 expr(A) ::= EXISTS LP select(Y) RP. {
1193 Expr *p;
1194 p = A = sqlite3PExpr(pParse, TK_EXISTS, 0, 0);
1195 sqlite3PExprAddSelect(pParse, p, Y);
1197 %endif SQLITE_OMIT_SUBQUERY
1199 /* CASE expressions */
1200 expr(A) ::= CASE case_operand(X) case_exprlist(Y) case_else(Z) END. {
1201 A = sqlite3PExpr(pParse, TK_CASE, X, 0);
1202 if( A ){
1203 A->x.pList = Z ? sqlite3ExprListAppend(pParse,Y,Z) : Y;
1204 sqlite3ExprSetHeightAndFlags(pParse, A);
1205 }else{
1206 sqlite3ExprListDelete(pParse->db, Y);
1207 sqlite3ExprDelete(pParse->db, Z);
1210 %type case_exprlist {ExprList*}
1211 %destructor case_exprlist {sqlite3ExprListDelete(pParse->db, $$);}
1212 case_exprlist(A) ::= case_exprlist(A) WHEN expr(Y) THEN expr(Z). {
1213 A = sqlite3ExprListAppend(pParse,A, Y);
1214 A = sqlite3ExprListAppend(pParse,A, Z);
1216 case_exprlist(A) ::= WHEN expr(Y) THEN expr(Z). {
1217 A = sqlite3ExprListAppend(pParse,0, Y);
1218 A = sqlite3ExprListAppend(pParse,A, Z);
1220 %type case_else {Expr*}
1221 %destructor case_else {sqlite3ExprDelete(pParse->db, $$);}
1222 case_else(A) ::= ELSE expr(X). {A = X;}
1223 case_else(A) ::= . {A = 0;}
1224 %type case_operand {Expr*}
1225 %destructor case_operand {sqlite3ExprDelete(pParse->db, $$);}
1226 case_operand(A) ::= expr(X). {A = X; /*A-overwrites-X*/}
1227 case_operand(A) ::= . {A = 0;}
1229 %type exprlist {ExprList*}
1230 %destructor exprlist {sqlite3ExprListDelete(pParse->db, $$);}
1231 %type nexprlist {ExprList*}
1232 %destructor nexprlist {sqlite3ExprListDelete(pParse->db, $$);}
1234 exprlist(A) ::= nexprlist(A).
1235 exprlist(A) ::= . {A = 0;}
1236 nexprlist(A) ::= nexprlist(A) COMMA expr(Y).
1237 {A = sqlite3ExprListAppend(pParse,A,Y);}
1238 nexprlist(A) ::= expr(Y).
1239 {A = sqlite3ExprListAppend(pParse,0,Y); /*A-overwrites-Y*/}
1241 %ifndef SQLITE_OMIT_SUBQUERY
1242 /* A paren_exprlist is an optional expression list contained inside
1243 ** of parenthesis */
1244 %type paren_exprlist {ExprList*}
1245 %destructor paren_exprlist {sqlite3ExprListDelete(pParse->db, $$);}
1246 paren_exprlist(A) ::= . {A = 0;}
1247 paren_exprlist(A) ::= LP exprlist(X) RP. {A = X;}
1248 %endif SQLITE_OMIT_SUBQUERY
1251 ///////////////////////////// The CREATE INDEX command ///////////////////////
1253 cmd ::= createkw(S) uniqueflag(U) INDEX ifnotexists(NE) nm(X) dbnm(D)
1254 ON nm(Y) LP sortlist(Z) RP where_opt(W). {
1255 sqlite3CreateIndex(pParse, &X, &D,
1256 sqlite3SrcListAppend(pParse->db,0,&Y,0), Z, U,
1257 &S, W, SQLITE_SO_ASC, NE, SQLITE_IDXTYPE_APPDEF);
1260 %type uniqueflag {int}
1261 uniqueflag(A) ::= UNIQUE. {A = OE_Abort;}
1262 uniqueflag(A) ::= . {A = OE_None;}
1265 // The eidlist non-terminal (Expression Id List) generates an ExprList
1266 // from a list of identifiers. The identifier names are in ExprList.a[].zName.
1267 // This list is stored in an ExprList rather than an IdList so that it
1268 // can be easily sent to sqlite3ColumnsExprList().
1270 // eidlist is grouped with CREATE INDEX because it used to be the non-terminal
1271 // used for the arguments to an index. That is just an historical accident.
1273 // IMPORTANT COMPATIBILITY NOTE: Some prior versions of SQLite accepted
1274 // COLLATE clauses and ASC or DESC keywords on ID lists in inappropriate
1275 // places - places that might have been stored in the sqlite_master schema.
1276 // Those extra features were ignored. But because they might be in some
1277 // (busted) old databases, we need to continue parsing them when loading
1278 // historical schemas.
1280 %type eidlist {ExprList*}
1281 %destructor eidlist {sqlite3ExprListDelete(pParse->db, $$);}
1282 %type eidlist_opt {ExprList*}
1283 %destructor eidlist_opt {sqlite3ExprListDelete(pParse->db, $$);}
1285 %include {
1286 /* Add a single new term to an ExprList that is used to store a
1287 ** list of identifiers. Report an error if the ID list contains
1288 ** a COLLATE clause or an ASC or DESC keyword, except ignore the
1289 ** error while parsing a legacy schema.
1291 static ExprList *parserAddExprIdListTerm(
1292 Parse *pParse,
1293 ExprList *pPrior,
1294 Token *pIdToken,
1295 int hasCollate,
1296 int sortOrder
1298 ExprList *p = sqlite3ExprListAppend(pParse, pPrior, 0);
1299 if( (hasCollate || sortOrder!=SQLITE_SO_UNDEFINED)
1300 && pParse->db->init.busy==0
1302 sqlite3ErrorMsg(pParse, "syntax error after column name \"%.*s\"",
1303 pIdToken->n, pIdToken->z);
1305 sqlite3ExprListSetName(pParse, p, pIdToken, 1);
1306 return p;
1308 } // end %include
1310 eidlist_opt(A) ::= . {A = 0;}
1311 eidlist_opt(A) ::= LP eidlist(X) RP. {A = X;}
1312 eidlist(A) ::= eidlist(A) COMMA nm(Y) collate(C) sortorder(Z). {
1313 A = parserAddExprIdListTerm(pParse, A, &Y, C, Z);
1315 eidlist(A) ::= nm(Y) collate(C) sortorder(Z). {
1316 A = parserAddExprIdListTerm(pParse, 0, &Y, C, Z); /*A-overwrites-Y*/
1319 %type collate {int}
1320 collate(C) ::= . {C = 0;}
1321 collate(C) ::= COLLATE ids. {C = 1;}
1324 ///////////////////////////// The DROP INDEX command /////////////////////////
1326 cmd ::= DROP INDEX ifexists(E) fullname(X). {sqlite3DropIndex(pParse, X, E);}
1328 ///////////////////////////// The VACUUM command /////////////////////////////
1330 %ifndef SQLITE_OMIT_VACUUM
1331 %ifndef SQLITE_OMIT_ATTACH
1332 cmd ::= VACUUM. {sqlite3Vacuum(pParse,0);}
1333 cmd ::= VACUUM nm(X). {sqlite3Vacuum(pParse,&X);}
1334 %endif SQLITE_OMIT_ATTACH
1335 %endif SQLITE_OMIT_VACUUM
1337 ///////////////////////////// The PRAGMA command /////////////////////////////
1339 %ifndef SQLITE_OMIT_PRAGMA
1340 cmd ::= PRAGMA nm(X) dbnm(Z). {sqlite3Pragma(pParse,&X,&Z,0,0);}
1341 cmd ::= PRAGMA nm(X) dbnm(Z) EQ nmnum(Y). {sqlite3Pragma(pParse,&X,&Z,&Y,0);}
1342 cmd ::= PRAGMA nm(X) dbnm(Z) LP nmnum(Y) RP. {sqlite3Pragma(pParse,&X,&Z,&Y,0);}
1343 cmd ::= PRAGMA nm(X) dbnm(Z) EQ minus_num(Y).
1344 {sqlite3Pragma(pParse,&X,&Z,&Y,1);}
1345 cmd ::= PRAGMA nm(X) dbnm(Z) LP minus_num(Y) RP.
1346 {sqlite3Pragma(pParse,&X,&Z,&Y,1);}
1348 nmnum(A) ::= plus_num(A).
1349 nmnum(A) ::= nm(A).
1350 nmnum(A) ::= ON(A).
1351 nmnum(A) ::= DELETE(A).
1352 nmnum(A) ::= DEFAULT(A).
1353 %endif SQLITE_OMIT_PRAGMA
1354 %token_class number INTEGER|FLOAT.
1355 plus_num(A) ::= PLUS number(X). {A = X;}
1356 plus_num(A) ::= number(A).
1357 minus_num(A) ::= MINUS number(X). {A = X;}
1358 //////////////////////////// The CREATE TRIGGER command /////////////////////
1360 %ifndef SQLITE_OMIT_TRIGGER
1362 cmd ::= createkw trigger_decl(A) BEGIN trigger_cmd_list(S) END(Z). {
1363 Token all;
1364 all.z = A.z;
1365 all.n = (int)(Z.z - A.z) + Z.n;
1366 sqlite3FinishTrigger(pParse, S, &all);
1369 trigger_decl(A) ::= temp(T) TRIGGER ifnotexists(NOERR) nm(B) dbnm(Z)
1370 trigger_time(C) trigger_event(D)
1371 ON fullname(E) foreach_clause when_clause(G). {
1372 sqlite3BeginTrigger(pParse, &B, &Z, C, D.a, D.b, E, G, T, NOERR);
1373 A = (Z.n==0?B:Z); /*A-overwrites-T*/
1376 %type trigger_time {int}
1377 trigger_time(A) ::= BEFORE|AFTER(X). { A = @X; /*A-overwrites-X*/ }
1378 trigger_time(A) ::= INSTEAD OF. { A = TK_INSTEAD;}
1379 trigger_time(A) ::= . { A = TK_BEFORE; }
1381 %type trigger_event {struct TrigEvent}
1382 %destructor trigger_event {sqlite3IdListDelete(pParse->db, $$.b);}
1383 trigger_event(A) ::= DELETE|INSERT(X). {A.a = @X; /*A-overwrites-X*/ A.b = 0;}
1384 trigger_event(A) ::= UPDATE(X). {A.a = @X; /*A-overwrites-X*/ A.b = 0;}
1385 trigger_event(A) ::= UPDATE OF idlist(X).{A.a = TK_UPDATE; A.b = X;}
1387 foreach_clause ::= .
1388 foreach_clause ::= FOR EACH ROW.
1390 %type when_clause {Expr*}
1391 %destructor when_clause {sqlite3ExprDelete(pParse->db, $$);}
1392 when_clause(A) ::= . { A = 0; }
1393 when_clause(A) ::= WHEN expr(X). { A = X; }
1395 %type trigger_cmd_list {TriggerStep*}
1396 %destructor trigger_cmd_list {sqlite3DeleteTriggerStep(pParse->db, $$);}
1397 trigger_cmd_list(A) ::= trigger_cmd_list(A) trigger_cmd(X) SEMI. {
1398 assert( A!=0 );
1399 A->pLast->pNext = X;
1400 A->pLast = X;
1402 trigger_cmd_list(A) ::= trigger_cmd(A) SEMI. {
1403 assert( A!=0 );
1404 A->pLast = A;
1407 // Disallow qualified table names on INSERT, UPDATE, and DELETE statements
1408 // within a trigger. The table to INSERT, UPDATE, or DELETE is always in
1409 // the same database as the table that the trigger fires on.
1411 %type trnm {Token}
1412 trnm(A) ::= nm(A).
1413 trnm(A) ::= nm DOT nm(X). {
1414 A = X;
1415 sqlite3ErrorMsg(pParse,
1416 "qualified table names are not allowed on INSERT, UPDATE, and DELETE "
1417 "statements within triggers");
1420 // Disallow the INDEX BY and NOT INDEXED clauses on UPDATE and DELETE
1421 // statements within triggers. We make a specific error message for this
1422 // since it is an exception to the default grammar rules.
1424 tridxby ::= .
1425 tridxby ::= INDEXED BY nm. {
1426 sqlite3ErrorMsg(pParse,
1427 "the INDEXED BY clause is not allowed on UPDATE or DELETE statements "
1428 "within triggers");
1430 tridxby ::= NOT INDEXED. {
1431 sqlite3ErrorMsg(pParse,
1432 "the NOT INDEXED clause is not allowed on UPDATE or DELETE statements "
1433 "within triggers");
1438 %type trigger_cmd {TriggerStep*}
1439 %destructor trigger_cmd {sqlite3DeleteTriggerStep(pParse->db, $$);}
1440 // UPDATE
1441 trigger_cmd(A) ::=
1442 UPDATE(B) orconf(R) trnm(X) tridxby SET setlist(Y) where_opt(Z) scanpt(E).
1443 {A = sqlite3TriggerUpdateStep(pParse->db, &X, Y, Z, R, B.z, E);}
1445 // INSERT
1446 trigger_cmd(A) ::= scanpt(B) insert_cmd(R) INTO
1447 trnm(X) idlist_opt(F) select(S) upsert(U) scanpt(Z). {
1448 A = sqlite3TriggerInsertStep(pParse->db,&X,F,S,R,U,B,Z);/*A-overwrites-R*/
1450 // DELETE
1451 trigger_cmd(A) ::= DELETE(B) FROM trnm(X) tridxby where_opt(Y) scanpt(E).
1452 {A = sqlite3TriggerDeleteStep(pParse->db, &X, Y, B.z, E);}
1454 // SELECT
1455 trigger_cmd(A) ::= scanpt(B) select(X) scanpt(E).
1456 {A = sqlite3TriggerSelectStep(pParse->db, X, B, E); /*A-overwrites-X*/}
1458 // The special RAISE expression that may occur in trigger programs
1459 expr(A) ::= RAISE LP IGNORE RP. {
1460 A = sqlite3PExpr(pParse, TK_RAISE, 0, 0);
1461 if( A ){
1462 A->affinity = OE_Ignore;
1465 expr(A) ::= RAISE LP raisetype(T) COMMA nm(Z) RP. {
1466 A = sqlite3ExprAlloc(pParse->db, TK_RAISE, &Z, 1);
1467 if( A ) {
1468 A->affinity = (char)T;
1471 %endif !SQLITE_OMIT_TRIGGER
1473 %type raisetype {int}
1474 raisetype(A) ::= ROLLBACK. {A = OE_Rollback;}
1475 raisetype(A) ::= ABORT. {A = OE_Abort;}
1476 raisetype(A) ::= FAIL. {A = OE_Fail;}
1479 //////////////////////// DROP TRIGGER statement //////////////////////////////
1480 %ifndef SQLITE_OMIT_TRIGGER
1481 cmd ::= DROP TRIGGER ifexists(NOERR) fullname(X). {
1482 sqlite3DropTrigger(pParse,X,NOERR);
1484 %endif !SQLITE_OMIT_TRIGGER
1486 //////////////////////// ATTACH DATABASE file AS name /////////////////////////
1487 %ifndef SQLITE_OMIT_ATTACH
1488 cmd ::= ATTACH database_kw_opt expr(F) AS expr(D) key_opt(K). {
1489 sqlite3Attach(pParse, F, D, K);
1491 cmd ::= DETACH database_kw_opt expr(D). {
1492 sqlite3Detach(pParse, D);
1495 %type key_opt {Expr*}
1496 %destructor key_opt {sqlite3ExprDelete(pParse->db, $$);}
1497 key_opt(A) ::= . { A = 0; }
1498 key_opt(A) ::= KEY expr(X). { A = X; }
1500 database_kw_opt ::= DATABASE.
1501 database_kw_opt ::= .
1502 %endif SQLITE_OMIT_ATTACH
1504 ////////////////////////// REINDEX collation //////////////////////////////////
1505 %ifndef SQLITE_OMIT_REINDEX
1506 cmd ::= REINDEX. {sqlite3Reindex(pParse, 0, 0);}
1507 cmd ::= REINDEX nm(X) dbnm(Y). {sqlite3Reindex(pParse, &X, &Y);}
1508 %endif SQLITE_OMIT_REINDEX
1510 /////////////////////////////////// ANALYZE ///////////////////////////////////
1511 %ifndef SQLITE_OMIT_ANALYZE
1512 cmd ::= ANALYZE. {sqlite3Analyze(pParse, 0, 0);}
1513 cmd ::= ANALYZE nm(X) dbnm(Y). {sqlite3Analyze(pParse, &X, &Y);}
1514 %endif
1516 //////////////////////// ALTER TABLE table ... ////////////////////////////////
1517 %ifndef SQLITE_OMIT_ALTERTABLE
1518 cmd ::= ALTER TABLE fullname(X) RENAME TO nm(Z). {
1519 sqlite3AlterRenameTable(pParse,X,&Z);
1521 cmd ::= ALTER TABLE add_column_fullname
1522 ADD kwcolumn_opt columnname(Y) carglist. {
1523 Y.n = (int)(pParse->sLastToken.z-Y.z) + pParse->sLastToken.n;
1524 sqlite3AlterFinishAddColumn(pParse, &Y);
1526 add_column_fullname ::= fullname(X). {
1527 disableLookaside(pParse);
1528 sqlite3AlterBeginAddColumn(pParse, X);
1530 kwcolumn_opt ::= .
1531 kwcolumn_opt ::= COLUMNKW.
1532 %endif SQLITE_OMIT_ALTERTABLE
1534 //////////////////////// CREATE VIRTUAL TABLE ... /////////////////////////////
1535 %ifndef SQLITE_OMIT_VIRTUALTABLE
1536 cmd ::= create_vtab. {sqlite3VtabFinishParse(pParse,0);}
1537 cmd ::= create_vtab LP vtabarglist RP(X). {sqlite3VtabFinishParse(pParse,&X);}
1538 create_vtab ::= createkw VIRTUAL TABLE ifnotexists(E)
1539 nm(X) dbnm(Y) USING nm(Z). {
1540 sqlite3VtabBeginParse(pParse, &X, &Y, &Z, E);
1542 vtabarglist ::= vtabarg.
1543 vtabarglist ::= vtabarglist COMMA vtabarg.
1544 vtabarg ::= . {sqlite3VtabArgInit(pParse);}
1545 vtabarg ::= vtabarg vtabargtoken.
1546 vtabargtoken ::= ANY(X). {sqlite3VtabArgExtend(pParse,&X);}
1547 vtabargtoken ::= lp anylist RP(X). {sqlite3VtabArgExtend(pParse,&X);}
1548 lp ::= LP(X). {sqlite3VtabArgExtend(pParse,&X);}
1549 anylist ::= .
1550 anylist ::= anylist LP anylist RP.
1551 anylist ::= anylist ANY.
1552 %endif SQLITE_OMIT_VIRTUALTABLE
1555 //////////////////////// COMMON TABLE EXPRESSIONS ////////////////////////////
1556 %type wqlist {With*}
1557 %destructor wqlist {sqlite3WithDelete(pParse->db, $$);}
1559 with ::= .
1560 %ifndef SQLITE_OMIT_CTE
1561 with ::= WITH wqlist(W). { sqlite3WithPush(pParse, W, 1); }
1562 with ::= WITH RECURSIVE wqlist(W). { sqlite3WithPush(pParse, W, 1); }
1564 wqlist(A) ::= nm(X) eidlist_opt(Y) AS LP select(Z) RP. {
1565 A = sqlite3WithAdd(pParse, 0, &X, Y, Z); /*A-overwrites-X*/
1567 wqlist(A) ::= wqlist(A) COMMA nm(X) eidlist_opt(Y) AS LP select(Z) RP. {
1568 A = sqlite3WithAdd(pParse, A, &X, Y, Z);
1570 %endif SQLITE_OMIT_CTE