000001 %include { 000002 /* 000003 ** 2001-09-15 000004 ** 000005 ** The author disclaims copyright to this source code. In place of 000006 ** a legal notice, here is a blessing: 000007 ** 000008 ** May you do good and not evil. 000009 ** May you find forgiveness for yourself and forgive others. 000010 ** May you share freely, never taking more than you give. 000011 ** 000012 ************************************************************************* 000013 ** This file contains SQLite's SQL parser. 000014 ** 000015 ** The canonical source code to this file ("parse.y") is a Lemon grammar 000016 ** file that specifies the input grammar and actions to take while parsing. 000017 ** That input file is processed by Lemon to generate a C-language 000018 ** implementation of a parser for the given grammar. You might be reading 000019 ** this comment as part of the translated C-code. Edits should be made 000020 ** to the original parse.y sources. 000021 */ 000022 } 000023 000024 // Function used to enlarge the parser stack, if needed 000025 %realloc parserStackRealloc 000026 %free sqlite3_free 000027 000028 // All token codes are small integers with #defines that begin with "TK_" 000029 %token_prefix TK_ 000030 000031 // The type of the data attached to each token is Token. This is also the 000032 // default type for non-terminals. 000033 // 000034 %token_type {Token} 000035 %default_type {Token} 000036 000037 // An extra argument to the constructor for the parser, which is available 000038 // to all actions. 000039 %extra_context {Parse *pParse} 000040 000041 // This code runs whenever there is a syntax error 000042 // 000043 %syntax_error { 000044 UNUSED_PARAMETER(yymajor); /* Silence some compiler warnings */ 000045 if( TOKEN.z[0] ){ 000046 parserSyntaxError(pParse, &TOKEN); 000047 }else{ 000048 sqlite3ErrorMsg(pParse, "incomplete input"); 000049 } 000050 } 000051 %stack_overflow { 000052 sqlite3OomFault(pParse->db); 000053 } 000054 000055 // The name of the generated procedure that implements the parser 000056 // is as follows: 000057 %name sqlite3Parser 000058 000059 // The following text is included near the beginning of the C source 000060 // code file that implements the parser. 000061 // 000062 %include { 000063 #include "sqliteInt.h" 000064 000065 /* 000066 ** Disable all error recovery processing in the parser push-down 000067 ** automaton. 000068 */ 000069 #define YYNOERRORRECOVERY 1 000070 000071 /* 000072 ** Make yytestcase() the same as testcase() 000073 */ 000074 #define yytestcase(X) testcase(X) 000075 000076 /* 000077 ** Indicate that sqlite3ParserFree() will never be called with a null 000078 ** pointer. 000079 */ 000080 #define YYPARSEFREENEVERNULL 1 000081 000082 /* 000083 ** In the amalgamation, the parse.c file generated by lemon and the 000084 ** tokenize.c file are concatenated. In that case, sqlite3RunParser() 000085 ** has access to the the size of the yyParser object and so the parser 000086 ** engine can be allocated from stack. In that case, only the 000087 ** sqlite3ParserInit() and sqlite3ParserFinalize() routines are invoked 000088 ** and the sqlite3ParserAlloc() and sqlite3ParserFree() routines can be 000089 ** omitted. 000090 */ 000091 #ifdef SQLITE_AMALGAMATION 000092 # define sqlite3Parser_ENGINEALWAYSONSTACK 1 000093 #endif 000094 000095 /* 000096 ** Alternative datatype for the argument to the malloc() routine passed 000097 ** into sqlite3ParserAlloc(). The default is size_t. 000098 */ 000099 #define YYMALLOCARGTYPE u64 000100 000101 /* 000102 ** An instance of the following structure describes the event of a 000103 ** TRIGGER. "a" is the event type, one of TK_UPDATE, TK_INSERT, 000104 ** TK_DELETE, or TK_INSTEAD. If the event is of the form 000105 ** 000106 ** UPDATE ON (a,b,c) 000107 ** 000108 ** Then the "b" IdList records the list "a,b,c". 000109 */ 000110 struct TrigEvent { int a; IdList * b; }; 000111 000112 struct FrameBound { int eType; Expr *pExpr; }; 000113 000114 /* 000115 ** Generate a syntax error 000116 */ 000117 static void parserSyntaxError(Parse *pParse, Token *p){ 000118 sqlite3ErrorMsg(pParse, "near \"%T\": syntax error", p); 000119 } 000120 000121 /* 000122 ** Disable lookaside memory allocation for objects that might be 000123 ** shared across database connections. 000124 */ 000125 static void disableLookaside(Parse *pParse){ 000126 sqlite3 *db = pParse->db; 000127 pParse->disableLookaside++; 000128 DisableLookaside; 000129 } 000130 000131 #if !defined(SQLITE_ENABLE_UPDATE_DELETE_LIMIT) \ 000132 && defined(SQLITE_UDL_CAPABLE_PARSER) 000133 /* 000134 ** Issue an error message if an ORDER BY or LIMIT clause occurs on an 000135 ** UPDATE or DELETE statement. 000136 */ 000137 static void updateDeleteLimitError( 000138 Parse *pParse, 000139 ExprList *pOrderBy, 000140 Expr *pLimit 000141 ){ 000142 if( pOrderBy ){ 000143 sqlite3ErrorMsg(pParse, "syntax error near \"ORDER BY\""); 000144 }else{ 000145 sqlite3ErrorMsg(pParse, "syntax error near \"LIMIT\""); 000146 } 000147 sqlite3ExprListDelete(pParse->db, pOrderBy); 000148 sqlite3ExprDelete(pParse->db, pLimit); 000149 } 000150 #endif /* SQLITE_ENABLE_UPDATE_DELETE_LIMIT */ 000151 000152 } // end %include 000153 000154 // Input is a single SQL command 000155 input ::= cmdlist. 000156 cmdlist ::= cmdlist ecmd. 000157 cmdlist ::= ecmd. 000158 ecmd ::= SEMI. 000159 ecmd ::= cmdx SEMI. 000160 %ifndef SQLITE_OMIT_EXPLAIN 000161 ecmd ::= explain cmdx SEMI. {NEVER-REDUCE} 000162 explain ::= EXPLAIN. { if( pParse->pReprepare==0 ) pParse->explain = 1; } 000163 explain ::= EXPLAIN QUERY PLAN. { if( pParse->pReprepare==0 ) pParse->explain = 2; } 000164 %endif SQLITE_OMIT_EXPLAIN 000165 cmdx ::= cmd. { sqlite3FinishCoding(pParse); } 000166 000167 ///////////////////// Begin and end transactions. //////////////////////////// 000168 // 000169 000170 cmd ::= BEGIN transtype(Y) trans_opt. {sqlite3BeginTransaction(pParse, Y);} 000171 trans_opt ::= . 000172 trans_opt ::= TRANSACTION. 000173 trans_opt ::= TRANSACTION nm. 000174 %type transtype {int} 000175 transtype(A) ::= . {A = TK_DEFERRED;} 000176 transtype(A) ::= DEFERRED(X). {A = @X; /*A-overwrites-X*/} 000177 transtype(A) ::= IMMEDIATE(X). {A = @X; /*A-overwrites-X*/} 000178 transtype(A) ::= EXCLUSIVE(X). {A = @X; /*A-overwrites-X*/} 000179 cmd ::= COMMIT|END(X) trans_opt. {sqlite3EndTransaction(pParse,@X);} 000180 cmd ::= ROLLBACK(X) trans_opt. {sqlite3EndTransaction(pParse,@X);} 000181 000182 savepoint_opt ::= SAVEPOINT. 000183 savepoint_opt ::= . 000184 cmd ::= SAVEPOINT nm(X). { 000185 sqlite3Savepoint(pParse, SAVEPOINT_BEGIN, &X); 000186 } 000187 cmd ::= RELEASE savepoint_opt nm(X). { 000188 sqlite3Savepoint(pParse, SAVEPOINT_RELEASE, &X); 000189 } 000190 cmd ::= ROLLBACK trans_opt TO savepoint_opt nm(X). { 000191 sqlite3Savepoint(pParse, SAVEPOINT_ROLLBACK, &X); 000192 } 000193 000194 ///////////////////// The CREATE TABLE statement //////////////////////////// 000195 // 000196 cmd ::= create_table create_table_args. 000197 create_table ::= createkw temp(T) TABLE ifnotexists(E) nm(Y) dbnm(Z). { 000198 sqlite3StartTable(pParse,&Y,&Z,T,0,0,E); 000199 } 000200 createkw(A) ::= CREATE(A). {disableLookaside(pParse);} 000201 000202 %type ifnotexists {int} 000203 ifnotexists(A) ::= . {A = 0;} 000204 ifnotexists(A) ::= IF NOT EXISTS. {A = 1;} 000205 %type temp {int} 000206 %ifndef SQLITE_OMIT_TEMPDB 000207 temp(A) ::= TEMP. {A = pParse->db->init.busy==0;} 000208 %endif SQLITE_OMIT_TEMPDB 000209 temp(A) ::= . {A = 0;} 000210 create_table_args ::= LP columnlist conslist_opt(X) RP(E) table_option_set(F). { 000211 sqlite3EndTable(pParse,&X,&E,F,0); 000212 } 000213 create_table_args ::= AS select(S). { 000214 sqlite3EndTable(pParse,0,0,0,S); 000215 sqlite3SelectDelete(pParse->db, S); 000216 } 000217 %type table_option_set {u32} 000218 %type table_option {u32} 000219 table_option_set(A) ::= . {A = 0;} 000220 table_option_set(A) ::= table_option(A). 000221 table_option_set(A) ::= table_option_set(X) COMMA table_option(Y). {A = X|Y;} 000222 table_option(A) ::= WITHOUT nm(X). { 000223 if( X.n==5 && sqlite3_strnicmp(X.z,"rowid",5)==0 ){ 000224 A = TF_WithoutRowid | TF_NoVisibleRowid; 000225 }else{ 000226 A = 0; 000227 sqlite3ErrorMsg(pParse, "unknown table option: %.*s", X.n, X.z); 000228 } 000229 } 000230 table_option(A) ::= nm(X). { 000231 if( X.n==6 && sqlite3_strnicmp(X.z,"strict",6)==0 ){ 000232 A = TF_Strict; 000233 }else{ 000234 A = 0; 000235 sqlite3ErrorMsg(pParse, "unknown table option: %.*s", X.n, X.z); 000236 } 000237 } 000238 columnlist ::= columnlist COMMA columnname carglist. 000239 columnlist ::= columnname carglist. 000240 columnname(A) ::= nm(A) typetoken(Y). {sqlite3AddColumn(pParse,A,Y);} 000241 000242 // Declare some tokens early in order to influence their values, to 000243 // improve performance and reduce the executable size. The goal here is 000244 // to get the "jump" operations in ISNULL through ESCAPE to have numeric 000245 // values that are early enough so that all jump operations are clustered 000246 // at the beginning. Also, operators like NE and EQ need to be adjacent, 000247 // and all of the comparison operators need to be clustered together. 000248 // Various assert() statements throughout the code enforce these restrictions. 000249 // 000250 %token ABORT ACTION AFTER ANALYZE ASC ATTACH BEFORE BEGIN BY CASCADE CAST. 000251 %token CONFLICT DATABASE DEFERRED DESC DETACH EACH END EXCLUSIVE EXPLAIN FAIL. 000252 %token OR AND NOT IS ISNOT MATCH LIKE_KW BETWEEN IN ISNULL NOTNULL NE EQ. 000253 %token GT LE LT GE ESCAPE. 000254 000255 // The following directive causes tokens ABORT, AFTER, ASC, etc. to 000256 // fallback to ID if they will not parse as their original value. 000257 // This obviates the need for the "id" nonterminal. 000258 // 000259 %fallback ID 000260 ABORT ACTION AFTER ANALYZE ASC ATTACH BEFORE BEGIN BY CASCADE CAST COLUMNKW 000261 CONFLICT DATABASE DEFERRED DESC DETACH DO 000262 EACH END EXCLUSIVE EXPLAIN FAIL FOR 000263 IGNORE IMMEDIATE INITIALLY INSTEAD LIKE_KW MATCH NO PLAN 000264 QUERY KEY OF OFFSET PRAGMA RAISE RECURSIVE RELEASE REPLACE RESTRICT ROW ROWS 000265 ROLLBACK SAVEPOINT TEMP TRIGGER VACUUM VIEW VIRTUAL WITH WITHOUT 000266 NULLS FIRST LAST 000267 %ifdef SQLITE_OMIT_COMPOUND_SELECT 000268 EXCEPT INTERSECT UNION 000269 %endif SQLITE_OMIT_COMPOUND_SELECT 000270 %ifndef SQLITE_OMIT_WINDOWFUNC 000271 CURRENT FOLLOWING PARTITION PRECEDING RANGE UNBOUNDED 000272 EXCLUDE GROUPS OTHERS TIES 000273 %endif SQLITE_OMIT_WINDOWFUNC 000274 %ifdef SQLITE_ENABLE_ORDERED_SET_AGGREGATES 000275 WITHIN 000276 %endif SQLITE_ENABLE_ORDERED_SET_AGGREGATES 000277 %ifndef SQLITE_OMIT_GENERATED_COLUMNS 000278 GENERATED ALWAYS 000279 %endif 000280 MATERIALIZED 000281 REINDEX RENAME CTIME_KW IF 000282 . 000283 %wildcard ANY. 000284 000285 // Define operator precedence early so that this is the first occurrence 000286 // of the operator tokens in the grammar. Keeping the operators together 000287 // causes them to be assigned integer values that are close together, 000288 // which keeps parser tables smaller. 000289 // 000290 // The token values assigned to these symbols is determined by the order 000291 // in which lemon first sees them. It must be the case that ISNULL/NOTNULL, 000292 // NE/EQ, GT/LE, and GE/LT are separated by only a single value. See 000293 // the sqlite3ExprIfFalse() routine for additional information on this 000294 // constraint. 000295 // 000296 %left OR. 000297 %left AND. 000298 %right NOT. 000299 %left IS MATCH LIKE_KW BETWEEN IN ISNULL NOTNULL NE EQ. 000300 %left GT LE LT GE. 000301 %right ESCAPE. 000302 %left BITAND BITOR LSHIFT RSHIFT. 000303 %left PLUS MINUS. 000304 %left STAR SLASH REM. 000305 %left CONCAT PTR. 000306 %left COLLATE. 000307 %right BITNOT. 000308 %nonassoc ON. 000309 000310 // An IDENTIFIER can be a generic identifier, or one of several 000311 // keywords. Any non-standard keyword can also be an identifier. 000312 // 000313 %token_class id ID|INDEXED. 000314 000315 // And "ids" is an identifer-or-string. 000316 // 000317 %token_class ids ID|STRING. 000318 000319 // An identifier or a join-keyword 000320 // 000321 %token_class idj ID|INDEXED|JOIN_KW. 000322 000323 // The name of a column or table can be any of the following: 000324 // 000325 %type nm {Token} 000326 nm(A) ::= idj(A). 000327 nm(A) ::= STRING(A). 000328 000329 // A typetoken is really zero or more tokens that form a type name such 000330 // as can be found after the column name in a CREATE TABLE statement. 000331 // Multiple tokens are concatenated to form the value of the typetoken. 000332 // 000333 %type typetoken {Token} 000334 typetoken(A) ::= . {A.n = 0; A.z = 0;} 000335 typetoken(A) ::= typename(A). 000336 typetoken(A) ::= typename(A) LP signed RP(Y). { 000337 A.n = (int)(&Y.z[Y.n] - A.z); 000338 } 000339 typetoken(A) ::= typename(A) LP signed COMMA signed RP(Y). { 000340 A.n = (int)(&Y.z[Y.n] - A.z); 000341 } 000342 %type typename {Token} 000343 typename(A) ::= ids(A). 000344 typename(A) ::= typename(A) ids(Y). {A.n=Y.n+(int)(Y.z-A.z);} 000345 signed ::= plus_num. 000346 signed ::= minus_num. 000347 000348 // The scanpt non-terminal takes a value which is a pointer to the 000349 // input text just past the last token that has been shifted into 000350 // the parser. By surrounding some phrase in the grammar with two 000351 // scanpt non-terminals, we can capture the input text for that phrase. 000352 // For example: 000353 // 000354 // something ::= .... scanpt(A) phrase scanpt(Z). 000355 // 000356 // The text that is parsed as "phrase" is a string starting at A 000357 // and containing (int)(Z-A) characters. There might be some extra 000358 // whitespace on either end of the text, but that can be removed in 000359 // post-processing, if needed. 000360 // 000361 %type scanpt {const char*} 000362 scanpt(A) ::= . { 000363 assert( yyLookahead!=YYNOCODE ); 000364 A = yyLookaheadToken.z; 000365 } 000366 scantok(A) ::= . { 000367 assert( yyLookahead!=YYNOCODE ); 000368 A = yyLookaheadToken; 000369 } 000370 000371 // "carglist" is a list of additional constraints that come after the 000372 // column name and column type in a CREATE TABLE statement. 000373 // 000374 carglist ::= carglist ccons. 000375 carglist ::= . 000376 ccons ::= CONSTRAINT nm(X). {pParse->constraintName = X;} 000377 ccons ::= DEFAULT scantok(A) term(X). 000378 {sqlite3AddDefaultValue(pParse,X,A.z,&A.z[A.n]);} 000379 ccons ::= DEFAULT LP(A) expr(X) RP(Z). 000380 {sqlite3AddDefaultValue(pParse,X,A.z+1,Z.z);} 000381 ccons ::= DEFAULT PLUS(A) scantok(Z) term(X). 000382 {sqlite3AddDefaultValue(pParse,X,A.z,&Z.z[Z.n]);} 000383 ccons ::= DEFAULT MINUS(A) scantok(Z) term(X). { 000384 Expr *p = sqlite3PExpr(pParse, TK_UMINUS, X, 0); 000385 sqlite3AddDefaultValue(pParse,p,A.z,&Z.z[Z.n]); 000386 } 000387 ccons ::= DEFAULT scantok id(X). { 000388 Expr *p = tokenExpr(pParse, TK_STRING, X); 000389 if( p ){ 000390 sqlite3ExprIdToTrueFalse(p); 000391 testcase( p->op==TK_TRUEFALSE && sqlite3ExprTruthValue(p) ); 000392 } 000393 sqlite3AddDefaultValue(pParse,p,X.z,X.z+X.n); 000394 } 000395 000396 // In addition to the type name, we also care about the primary key and 000397 // UNIQUE constraints. 000398 // 000399 ccons ::= NULL onconf. 000400 ccons ::= NOT NULL onconf(R). {sqlite3AddNotNull(pParse, R);} 000401 ccons ::= PRIMARY KEY sortorder(Z) onconf(R) autoinc(I). 000402 {sqlite3AddPrimaryKey(pParse,0,R,I,Z);} 000403 ccons ::= UNIQUE onconf(R). {sqlite3CreateIndex(pParse,0,0,0,0,R,0,0,0,0, 000404 SQLITE_IDXTYPE_UNIQUE);} 000405 ccons ::= CHECK LP(A) expr(X) RP(B). {sqlite3AddCheckConstraint(pParse,X,A.z,B.z);} 000406 ccons ::= REFERENCES nm(T) eidlist_opt(TA) refargs(R). 000407 {sqlite3CreateForeignKey(pParse,0,&T,TA,R);} 000408 ccons ::= defer_subclause(D). {sqlite3DeferForeignKey(pParse,D);} 000409 ccons ::= COLLATE ids(C). {sqlite3AddCollateType(pParse, &C);} 000410 ccons ::= GENERATED ALWAYS AS generated. 000411 ccons ::= AS generated. 000412 generated ::= LP expr(E) RP. {sqlite3AddGenerated(pParse,E,0);} 000413 generated ::= LP expr(E) RP ID(TYPE). {sqlite3AddGenerated(pParse,E,&TYPE);} 000414 000415 // The optional AUTOINCREMENT keyword 000416 %type autoinc {int} 000417 autoinc(X) ::= . {X = 0;} 000418 autoinc(X) ::= AUTOINCR. {X = 1;} 000419 000420 // The next group of rules parses the arguments to a REFERENCES clause 000421 // that determine if the referential integrity checking is deferred or 000422 // or immediate and which determine what action to take if a ref-integ 000423 // check fails. 000424 // 000425 %type refargs {int} 000426 refargs(A) ::= . { A = OE_None*0x0101; /* EV: R-19803-45884 */} 000427 refargs(A) ::= refargs(A) refarg(Y). { A = (A & ~Y.mask) | Y.value; } 000428 %type refarg {struct {int value; int mask;}} 000429 refarg(A) ::= MATCH nm. { A.value = 0; A.mask = 0x000000; } 000430 refarg(A) ::= ON INSERT refact. { A.value = 0; A.mask = 0x000000; } 000431 refarg(A) ::= ON DELETE refact(X). { A.value = X; A.mask = 0x0000ff; } 000432 refarg(A) ::= ON UPDATE refact(X). { A.value = X<<8; A.mask = 0x00ff00; } 000433 %type refact {int} 000434 refact(A) ::= SET NULL. { A = OE_SetNull; /* EV: R-33326-45252 */} 000435 refact(A) ::= SET DEFAULT. { A = OE_SetDflt; /* EV: R-33326-45252 */} 000436 refact(A) ::= CASCADE. { A = OE_Cascade; /* EV: R-33326-45252 */} 000437 refact(A) ::= RESTRICT. { A = OE_Restrict; /* EV: R-33326-45252 */} 000438 refact(A) ::= NO ACTION. { A = OE_None; /* EV: R-33326-45252 */} 000439 %type defer_subclause {int} 000440 defer_subclause(A) ::= NOT DEFERRABLE init_deferred_pred_opt. {A = 0;} 000441 defer_subclause(A) ::= DEFERRABLE init_deferred_pred_opt(X). {A = X;} 000442 %type init_deferred_pred_opt {int} 000443 init_deferred_pred_opt(A) ::= . {A = 0;} 000444 init_deferred_pred_opt(A) ::= INITIALLY DEFERRED. {A = 1;} 000445 init_deferred_pred_opt(A) ::= INITIALLY IMMEDIATE. {A = 0;} 000446 000447 conslist_opt(A) ::= . {A.n = 0; A.z = 0;} 000448 conslist_opt(A) ::= COMMA(A) conslist. 000449 conslist ::= conslist tconscomma tcons. 000450 conslist ::= tcons. 000451 tconscomma ::= COMMA. {pParse->constraintName.n = 0;} 000452 tconscomma ::= . 000453 tcons ::= CONSTRAINT nm(X). {pParse->constraintName = X;} 000454 tcons ::= PRIMARY KEY LP sortlist(X) autoinc(I) RP onconf(R). 000455 {sqlite3AddPrimaryKey(pParse,X,R,I,0);} 000456 tcons ::= UNIQUE LP sortlist(X) RP onconf(R). 000457 {sqlite3CreateIndex(pParse,0,0,0,X,R,0,0,0,0, 000458 SQLITE_IDXTYPE_UNIQUE);} 000459 tcons ::= CHECK LP(A) expr(E) RP(B) onconf. 000460 {sqlite3AddCheckConstraint(pParse,E,A.z,B.z);} 000461 tcons ::= FOREIGN KEY LP eidlist(FA) RP 000462 REFERENCES nm(T) eidlist_opt(TA) refargs(R) defer_subclause_opt(D). { 000463 sqlite3CreateForeignKey(pParse, FA, &T, TA, R); 000464 sqlite3DeferForeignKey(pParse, D); 000465 } 000466 %type defer_subclause_opt {int} 000467 defer_subclause_opt(A) ::= . {A = 0;} 000468 defer_subclause_opt(A) ::= defer_subclause(A). 000469 000470 // The following is a non-standard extension that allows us to declare the 000471 // default behavior when there is a constraint conflict. 000472 // 000473 %type onconf {int} 000474 %type orconf {int} 000475 %type resolvetype {int} 000476 onconf(A) ::= . {A = OE_Default;} 000477 onconf(A) ::= ON CONFLICT resolvetype(X). {A = X;} 000478 orconf(A) ::= . {A = OE_Default;} 000479 orconf(A) ::= OR resolvetype(X). {A = X;} 000480 resolvetype(A) ::= raisetype(A). 000481 resolvetype(A) ::= IGNORE. {A = OE_Ignore;} 000482 resolvetype(A) ::= REPLACE. {A = OE_Replace;} 000483 000484 ////////////////////////// The DROP TABLE ///////////////////////////////////// 000485 // 000486 cmd ::= DROP TABLE ifexists(E) fullname(X). { 000487 sqlite3DropTable(pParse, X, 0, E); 000488 } 000489 %type ifexists {int} 000490 ifexists(A) ::= IF EXISTS. {A = 1;} 000491 ifexists(A) ::= . {A = 0;} 000492 000493 ///////////////////// The CREATE VIEW statement ///////////////////////////// 000494 // 000495 %ifndef SQLITE_OMIT_VIEW 000496 cmd ::= createkw(X) temp(T) VIEW ifnotexists(E) nm(Y) dbnm(Z) eidlist_opt(C) 000497 AS select(S). { 000498 sqlite3CreateView(pParse, &X, &Y, &Z, C, S, T, E); 000499 } 000500 cmd ::= DROP VIEW ifexists(E) fullname(X). { 000501 sqlite3DropTable(pParse, X, 1, E); 000502 } 000503 %endif SQLITE_OMIT_VIEW 000504 000505 //////////////////////// The SELECT statement ///////////////////////////////// 000506 // 000507 cmd ::= select(X). { 000508 SelectDest dest = {SRT_Output, 0, 0, 0, 0, 0, 0}; 000509 if( (pParse->db->mDbFlags & DBFLAG_EncodingFixed)!=0 000510 || sqlite3ReadSchema(pParse)==SQLITE_OK 000511 ){ 000512 sqlite3Select(pParse, X, &dest); 000513 } 000514 sqlite3SelectDelete(pParse->db, X); 000515 } 000516 000517 %type select {Select*} 000518 %destructor select {sqlite3SelectDelete(pParse->db, $$);} 000519 %type selectnowith {Select*} 000520 %destructor selectnowith {sqlite3SelectDelete(pParse->db, $$);} 000521 %type oneselect {Select*} 000522 %destructor oneselect {sqlite3SelectDelete(pParse->db, $$);} 000523 000524 %include { 000525 /* 000526 ** For a compound SELECT statement, make sure p->pPrior->pNext==p for 000527 ** all elements in the list. And make sure list length does not exceed 000528 ** SQLITE_LIMIT_COMPOUND_SELECT. 000529 */ 000530 static void parserDoubleLinkSelect(Parse *pParse, Select *p){ 000531 assert( p!=0 ); 000532 if( p->pPrior ){ 000533 Select *pNext = 0, *pLoop = p; 000534 int mxSelect, cnt = 1; 000535 while(1){ 000536 pLoop->pNext = pNext; 000537 pLoop->selFlags |= SF_Compound; 000538 pNext = pLoop; 000539 pLoop = pLoop->pPrior; 000540 if( pLoop==0 ) break; 000541 cnt++; 000542 if( pLoop->pOrderBy || pLoop->pLimit ){ 000543 sqlite3ErrorMsg(pParse,"%s clause should come after %s not before", 000544 pLoop->pOrderBy!=0 ? "ORDER BY" : "LIMIT", 000545 sqlite3SelectOpName(pNext->op)); 000546 break; 000547 } 000548 } 000549 if( (p->selFlags & (SF_MultiValue|SF_Values))==0 000550 && (mxSelect = pParse->db->aLimit[SQLITE_LIMIT_COMPOUND_SELECT])>0 000551 && cnt>mxSelect 000552 ){ 000553 sqlite3ErrorMsg(pParse, "too many terms in compound SELECT"); 000554 } 000555 } 000556 } 000557 000558 /* Attach a With object describing the WITH clause to a Select 000559 ** object describing the query for which the WITH clause is a prefix. 000560 */ 000561 static Select *attachWithToSelect(Parse *pParse, Select *pSelect, With *pWith){ 000562 if( pSelect ){ 000563 pSelect->pWith = pWith; 000564 parserDoubleLinkSelect(pParse, pSelect); 000565 }else{ 000566 sqlite3WithDelete(pParse->db, pWith); 000567 } 000568 return pSelect; 000569 } 000570 000571 /* Memory allocator for parser stack resizing. This is a thin wrapper around 000572 ** sqlite3_realloc() that includes a call to sqlite3FaultSim() to facilitate 000573 ** testing. 000574 */ 000575 static void *parserStackRealloc(void *pOld, sqlite3_uint64 newSize){ 000576 return sqlite3FaultSim(700) ? 0 : sqlite3_realloc(pOld, newSize); 000577 } 000578 } 000579 000580 %ifndef SQLITE_OMIT_CTE 000581 select(A) ::= WITH wqlist(W) selectnowith(X). {A = attachWithToSelect(pParse,X,W);} 000582 select(A) ::= WITH RECURSIVE wqlist(W) selectnowith(X). 000583 {A = attachWithToSelect(pParse,X,W);} 000584 000585 %endif /* SQLITE_OMIT_CTE */ 000586 select(A) ::= selectnowith(A). { 000587 Select *p = A; 000588 if( p ){ 000589 parserDoubleLinkSelect(pParse, p); 000590 } 000591 } 000592 000593 selectnowith(A) ::= oneselect(A). 000594 %ifndef SQLITE_OMIT_COMPOUND_SELECT 000595 selectnowith(A) ::= selectnowith(A) multiselect_op(Y) oneselect(Z). { 000596 Select *pRhs = Z; 000597 Select *pLhs = A; 000598 if( pRhs && pRhs->pPrior ){ 000599 SrcList *pFrom; 000600 Token x; 000601 x.n = 0; 000602 parserDoubleLinkSelect(pParse, pRhs); 000603 pFrom = sqlite3SrcListAppendFromTerm(pParse,0,0,0,&x,pRhs,0); 000604 pRhs = sqlite3SelectNew(pParse,0,pFrom,0,0,0,0,0,0); 000605 } 000606 if( pRhs ){ 000607 pRhs->op = (u8)Y; 000608 pRhs->pPrior = pLhs; 000609 if( ALWAYS(pLhs) ) pLhs->selFlags &= ~SF_MultiValue; 000610 pRhs->selFlags &= ~SF_MultiValue; 000611 if( Y!=TK_ALL ) pParse->hasCompound = 1; 000612 }else{ 000613 sqlite3SelectDelete(pParse->db, pLhs); 000614 } 000615 A = pRhs; 000616 } 000617 %type multiselect_op {int} 000618 multiselect_op(A) ::= UNION(OP). {A = @OP; /*A-overwrites-OP*/} 000619 multiselect_op(A) ::= UNION ALL. {A = TK_ALL;} 000620 multiselect_op(A) ::= EXCEPT|INTERSECT(OP). {A = @OP; /*A-overwrites-OP*/} 000621 %endif SQLITE_OMIT_COMPOUND_SELECT 000622 000623 oneselect(A) ::= SELECT distinct(D) selcollist(W) from(X) where_opt(Y) 000624 groupby_opt(P) having_opt(Q) 000625 orderby_opt(Z) limit_opt(L). { 000626 A = sqlite3SelectNew(pParse,W,X,Y,P,Q,Z,D,L); 000627 } 000628 %ifndef SQLITE_OMIT_WINDOWFUNC 000629 oneselect(A) ::= SELECT distinct(D) selcollist(W) from(X) where_opt(Y) 000630 groupby_opt(P) having_opt(Q) window_clause(R) 000631 orderby_opt(Z) limit_opt(L). { 000632 A = sqlite3SelectNew(pParse,W,X,Y,P,Q,Z,D,L); 000633 if( A ){ 000634 A->pWinDefn = R; 000635 }else{ 000636 sqlite3WindowListDelete(pParse->db, R); 000637 } 000638 } 000639 %endif 000640 000641 000642 // Single row VALUES clause. 000643 // 000644 %type values {Select*} 000645 oneselect(A) ::= values(A). 000646 %destructor values {sqlite3SelectDelete(pParse->db, $$);} 000647 values(A) ::= VALUES LP nexprlist(X) RP. { 000648 A = sqlite3SelectNew(pParse,X,0,0,0,0,0,SF_Values,0); 000649 } 000650 000651 // Multiple row VALUES clause. 000652 // 000653 %type mvalues {Select*} 000654 oneselect(A) ::= mvalues(A). { 000655 sqlite3MultiValuesEnd(pParse, A); 000656 } 000657 %destructor mvalues {sqlite3SelectDelete(pParse->db, $$);} 000658 mvalues(A) ::= values(A) COMMA LP nexprlist(Y) RP. { 000659 A = sqlite3MultiValues(pParse, A, Y); 000660 } 000661 mvalues(A) ::= mvalues(A) COMMA LP nexprlist(Y) RP. { 000662 A = sqlite3MultiValues(pParse, A, Y); 000663 } 000664 000665 // The "distinct" nonterminal is true (1) if the DISTINCT keyword is 000666 // present and false (0) if it is not. 000667 // 000668 %type distinct {int} 000669 distinct(A) ::= DISTINCT. {A = SF_Distinct;} 000670 distinct(A) ::= ALL. {A = SF_All;} 000671 distinct(A) ::= . {A = 0;} 000672 000673 // selcollist is a list of expressions that are to become the return 000674 // values of the SELECT statement. The "*" in statements like 000675 // "SELECT * FROM ..." is encoded as a special expression with an 000676 // opcode of TK_ASTERISK. 000677 // 000678 %type selcollist {ExprList*} 000679 %destructor selcollist {sqlite3ExprListDelete(pParse->db, $$);} 000680 %type sclp {ExprList*} 000681 %destructor sclp {sqlite3ExprListDelete(pParse->db, $$);} 000682 sclp(A) ::= selcollist(A) COMMA. 000683 sclp(A) ::= . {A = 0;} 000684 selcollist(A) ::= sclp(A) scanpt(B) expr(X) scanpt(Z) as(Y). { 000685 A = sqlite3ExprListAppend(pParse, A, X); 000686 if( Y.n>0 ) sqlite3ExprListSetName(pParse, A, &Y, 1); 000687 sqlite3ExprListSetSpan(pParse,A,B,Z); 000688 } 000689 selcollist(A) ::= sclp(A) scanpt STAR(X). { 000690 Expr *p = sqlite3Expr(pParse->db, TK_ASTERISK, 0); 000691 sqlite3ExprSetErrorOffset(p, (int)(X.z - pParse->zTail)); 000692 A = sqlite3ExprListAppend(pParse, A, p); 000693 } 000694 selcollist(A) ::= sclp(A) scanpt nm(X) DOT STAR(Y). { 000695 Expr *pRight, *pLeft, *pDot; 000696 pRight = sqlite3PExpr(pParse, TK_ASTERISK, 0, 0); 000697 sqlite3ExprSetErrorOffset(pRight, (int)(Y.z - pParse->zTail)); 000698 pLeft = tokenExpr(pParse, TK_ID, X); 000699 pDot = sqlite3PExpr(pParse, TK_DOT, pLeft, pRight); 000700 A = sqlite3ExprListAppend(pParse,A, pDot); 000701 } 000702 000703 // An option "AS <id>" phrase that can follow one of the expressions that 000704 // define the result set, or one of the tables in the FROM clause. 000705 // 000706 %type as {Token} 000707 as(X) ::= AS nm(Y). {X = Y;} 000708 as(X) ::= ids(X). 000709 as(X) ::= . {X.n = 0; X.z = 0;} 000710 000711 000712 %type seltablist {SrcList*} 000713 %destructor seltablist {sqlite3SrcListDelete(pParse->db, $$);} 000714 %type stl_prefix {SrcList*} 000715 %destructor stl_prefix {sqlite3SrcListDelete(pParse->db, $$);} 000716 %type from {SrcList*} 000717 %destructor from {sqlite3SrcListDelete(pParse->db, $$);} 000718 000719 // A complete FROM clause. 000720 // 000721 from(A) ::= . {A = 0;} 000722 from(A) ::= FROM seltablist(X). { 000723 A = X; 000724 sqlite3SrcListShiftJoinType(pParse,A); 000725 } 000726 000727 // "seltablist" is a "Select Table List" - the content of the FROM clause 000728 // in a SELECT statement. "stl_prefix" is a prefix of this list. 000729 // 000730 stl_prefix(A) ::= seltablist(A) joinop(Y). { 000731 if( ALWAYS(A && A->nSrc>0) ) A->a[A->nSrc-1].fg.jointype = (u8)Y; 000732 } 000733 stl_prefix(A) ::= . {A = 0;} 000734 seltablist(A) ::= stl_prefix(A) nm(Y) dbnm(D) as(Z) on_using(N). { 000735 A = sqlite3SrcListAppendFromTerm(pParse,A,&Y,&D,&Z,0,&N); 000736 } 000737 seltablist(A) ::= stl_prefix(A) nm(Y) dbnm(D) as(Z) indexed_by(I) on_using(N). { 000738 A = sqlite3SrcListAppendFromTerm(pParse,A,&Y,&D,&Z,0,&N); 000739 sqlite3SrcListIndexedBy(pParse, A, &I); 000740 } 000741 seltablist(A) ::= stl_prefix(A) nm(Y) dbnm(D) LP exprlist(E) RP as(Z) on_using(N). { 000742 A = sqlite3SrcListAppendFromTerm(pParse,A,&Y,&D,&Z,0,&N); 000743 sqlite3SrcListFuncArgs(pParse, A, E); 000744 } 000745 %ifndef SQLITE_OMIT_SUBQUERY 000746 seltablist(A) ::= stl_prefix(A) LP select(S) RP as(Z) on_using(N). { 000747 A = sqlite3SrcListAppendFromTerm(pParse,A,0,0,&Z,S,&N); 000748 } 000749 seltablist(A) ::= stl_prefix(A) LP seltablist(F) RP as(Z) on_using(N). { 000750 if( A==0 && Z.n==0 && N.pOn==0 && N.pUsing==0 ){ 000751 A = F; 000752 }else if( ALWAYS(F!=0) && F->nSrc==1 ){ 000753 A = sqlite3SrcListAppendFromTerm(pParse,A,0,0,&Z,0,&N); 000754 if( A ){ 000755 SrcItem *pNew = &A->a[A->nSrc-1]; 000756 SrcItem *pOld = F->a; 000757 assert( pOld->fg.fixedSchema==0 ); 000758 pNew->zName = pOld->zName; 000759 assert( pOld->fg.fixedSchema==0 ); 000760 if( pOld->fg.isSubquery ){ 000761 pNew->fg.isSubquery = 1; 000762 pNew->u4.pSubq = pOld->u4.pSubq; 000763 pOld->u4.pSubq = 0; 000764 pOld->fg.isSubquery = 0; 000765 assert( pNew->u4.pSubq!=0 && pNew->u4.pSubq->pSelect!=0 ); 000766 if( (pNew->u4.pSubq->pSelect->selFlags & SF_NestedFrom)!=0 ){ 000767 pNew->fg.isNestedFrom = 1; 000768 } 000769 }else{ 000770 pNew->u4.zDatabase = pOld->u4.zDatabase; 000771 pOld->u4.zDatabase = 0; 000772 } 000773 if( pOld->fg.isTabFunc ){ 000774 pNew->u1.pFuncArg = pOld->u1.pFuncArg; 000775 pOld->u1.pFuncArg = 0; 000776 pOld->fg.isTabFunc = 0; 000777 pNew->fg.isTabFunc = 1; 000778 } 000779 pOld->zName = 0; 000780 } 000781 sqlite3SrcListDelete(pParse->db, F); 000782 }else{ 000783 Select *pSubquery; 000784 sqlite3SrcListShiftJoinType(pParse,F); 000785 pSubquery = sqlite3SelectNew(pParse,0,F,0,0,0,0,SF_NestedFrom,0); 000786 A = sqlite3SrcListAppendFromTerm(pParse,A,0,0,&Z,pSubquery,&N); 000787 } 000788 } 000789 %endif SQLITE_OMIT_SUBQUERY 000790 000791 %type dbnm {Token} 000792 dbnm(A) ::= . {A.z=0; A.n=0;} 000793 dbnm(A) ::= DOT nm(X). {A = X;} 000794 000795 %type fullname {SrcList*} 000796 %destructor fullname {sqlite3SrcListDelete(pParse->db, $$);} 000797 fullname(A) ::= nm(X). { 000798 A = sqlite3SrcListAppend(pParse,0,&X,0); 000799 if( IN_RENAME_OBJECT && A ) sqlite3RenameTokenMap(pParse, A->a[0].zName, &X); 000800 } 000801 fullname(A) ::= nm(X) DOT nm(Y). { 000802 A = sqlite3SrcListAppend(pParse,0,&X,&Y); 000803 if( IN_RENAME_OBJECT && A ) sqlite3RenameTokenMap(pParse, A->a[0].zName, &Y); 000804 } 000805 000806 %type xfullname {SrcList*} 000807 %destructor xfullname {sqlite3SrcListDelete(pParse->db, $$);} 000808 xfullname(A) ::= nm(X). 000809 {A = sqlite3SrcListAppend(pParse,0,&X,0); /*A-overwrites-X*/} 000810 xfullname(A) ::= nm(X) DOT nm(Y). 000811 {A = sqlite3SrcListAppend(pParse,0,&X,&Y); /*A-overwrites-X*/} 000812 xfullname(A) ::= nm(X) DOT nm(Y) AS nm(Z). { 000813 A = sqlite3SrcListAppend(pParse,0,&X,&Y); /*A-overwrites-X*/ 000814 if( A ) A->a[0].zAlias = sqlite3NameFromToken(pParse->db, &Z); 000815 } 000816 xfullname(A) ::= nm(X) AS nm(Z). { 000817 A = sqlite3SrcListAppend(pParse,0,&X,0); /*A-overwrites-X*/ 000818 if( A ) A->a[0].zAlias = sqlite3NameFromToken(pParse->db, &Z); 000819 } 000820 000821 %type joinop {int} 000822 joinop(X) ::= COMMA|JOIN. { X = JT_INNER; } 000823 joinop(X) ::= JOIN_KW(A) JOIN. 000824 {X = sqlite3JoinType(pParse,&A,0,0); /*X-overwrites-A*/} 000825 joinop(X) ::= JOIN_KW(A) nm(B) JOIN. 000826 {X = sqlite3JoinType(pParse,&A,&B,0); /*X-overwrites-A*/} 000827 joinop(X) ::= JOIN_KW(A) nm(B) nm(C) JOIN. 000828 {X = sqlite3JoinType(pParse,&A,&B,&C);/*X-overwrites-A*/} 000829 000830 // There is a parsing abiguity in an upsert statement that uses a 000831 // SELECT on the RHS of a the INSERT: 000832 // 000833 // INSERT INTO tab SELECT * FROM aaa JOIN bbb ON CONFLICT ... 000834 // here ----^^ 000835 // 000836 // When the ON token is encountered, the parser does not know if it is 000837 // the beginning of an ON CONFLICT clause, or the beginning of an ON 000838 // clause associated with the JOIN. The conflict is resolved in favor 000839 // of the JOIN. If an ON CONFLICT clause is intended, insert a dummy 000840 // WHERE clause in between, like this: 000841 // 000842 // INSERT INTO tab SELECT * FROM aaa JOIN bbb WHERE true ON CONFLICT ... 000843 // 000844 // The [AND] and [OR] precedence marks in the rules for on_using cause the 000845 // ON in this context to always be interpreted as belonging to the JOIN. 000846 // 000847 %type on_using {OnOrUsing} 000848 //%destructor on_using {sqlite3ClearOnOrUsing(pParse->db, &$$);} 000849 on_using(N) ::= ON expr(E). {N.pOn = E; N.pUsing = 0;} 000850 on_using(N) ::= USING LP idlist(L) RP. {N.pOn = 0; N.pUsing = L;} 000851 on_using(N) ::= . [OR] {N.pOn = 0; N.pUsing = 0;} 000852 000853 // Note that this block abuses the Token type just a little. If there is 000854 // no "INDEXED BY" clause, the returned token is empty (z==0 && n==0). If 000855 // there is an INDEXED BY clause, then the token is populated as per normal, 000856 // with z pointing to the token data and n containing the number of bytes 000857 // in the token. 000858 // 000859 // If there is a "NOT INDEXED" clause, then (z==0 && n==1), which is 000860 // normally illegal. The sqlite3SrcListIndexedBy() function 000861 // recognizes and interprets this as a special case. 000862 // 000863 %type indexed_opt {Token} 000864 %type indexed_by {Token} 000865 indexed_opt(A) ::= . {A.z=0; A.n=0;} 000866 indexed_opt(A) ::= indexed_by(A). 000867 indexed_by(A) ::= INDEXED BY nm(X). {A = X;} 000868 indexed_by(A) ::= NOT INDEXED. {A.z=0; A.n=1;} 000869 000870 %type orderby_opt {ExprList*} 000871 %destructor orderby_opt {sqlite3ExprListDelete(pParse->db, $$);} 000872 000873 // the sortlist non-terminal stores a list of expression where each 000874 // expression is optionally followed by ASC or DESC to indicate the 000875 // sort order. 000876 // 000877 %type sortlist {ExprList*} 000878 %destructor sortlist {sqlite3ExprListDelete(pParse->db, $$);} 000879 000880 orderby_opt(A) ::= . {A = 0;} 000881 orderby_opt(A) ::= ORDER BY sortlist(X). {A = X;} 000882 sortlist(A) ::= sortlist(A) COMMA expr(Y) sortorder(Z) nulls(X). { 000883 A = sqlite3ExprListAppend(pParse,A,Y); 000884 sqlite3ExprListSetSortOrder(A,Z,X); 000885 } 000886 sortlist(A) ::= expr(Y) sortorder(Z) nulls(X). { 000887 A = sqlite3ExprListAppend(pParse,0,Y); /*A-overwrites-Y*/ 000888 sqlite3ExprListSetSortOrder(A,Z,X); 000889 } 000890 000891 %type sortorder {int} 000892 000893 sortorder(A) ::= ASC. {A = SQLITE_SO_ASC;} 000894 sortorder(A) ::= DESC. {A = SQLITE_SO_DESC;} 000895 sortorder(A) ::= . {A = SQLITE_SO_UNDEFINED;} 000896 000897 %type nulls {int} 000898 nulls(A) ::= NULLS FIRST. {A = SQLITE_SO_ASC;} 000899 nulls(A) ::= NULLS LAST. {A = SQLITE_SO_DESC;} 000900 nulls(A) ::= . {A = SQLITE_SO_UNDEFINED;} 000901 000902 %type groupby_opt {ExprList*} 000903 %destructor groupby_opt {sqlite3ExprListDelete(pParse->db, $$);} 000904 groupby_opt(A) ::= . {A = 0;} 000905 groupby_opt(A) ::= GROUP BY nexprlist(X). {A = X;} 000906 000907 %type having_opt {Expr*} 000908 %destructor having_opt {sqlite3ExprDelete(pParse->db, $$);} 000909 having_opt(A) ::= . {A = 0;} 000910 having_opt(A) ::= HAVING expr(X). {A = X;} 000911 000912 %type limit_opt {Expr*} 000913 000914 // The destructor for limit_opt will never fire in the current grammar. 000915 // The limit_opt non-terminal only occurs at the end of a single production 000916 // rule for SELECT statements. As soon as the rule that create the 000917 // limit_opt non-terminal reduces, the SELECT statement rule will also 000918 // reduce. So there is never a limit_opt non-terminal on the stack 000919 // except as a transient. So there is never anything to destroy. 000920 // 000921 //%destructor limit_opt {sqlite3ExprDelete(pParse->db, $$);} 000922 limit_opt(A) ::= . {A = 0;} 000923 limit_opt(A) ::= LIMIT expr(X). 000924 {A = sqlite3PExpr(pParse,TK_LIMIT,X,0);} 000925 limit_opt(A) ::= LIMIT expr(X) OFFSET expr(Y). 000926 {A = sqlite3PExpr(pParse,TK_LIMIT,X,Y);} 000927 limit_opt(A) ::= LIMIT expr(X) COMMA expr(Y). 000928 {A = sqlite3PExpr(pParse,TK_LIMIT,Y,X);} 000929 000930 /////////////////////////// The DELETE statement ///////////////////////////// 000931 // 000932 %if SQLITE_ENABLE_UPDATE_DELETE_LIMIT || SQLITE_UDL_CAPABLE_PARSER 000933 cmd ::= with DELETE FROM xfullname(X) indexed_opt(I) where_opt_ret(W) 000934 orderby_opt(O) limit_opt(L). { 000935 sqlite3SrcListIndexedBy(pParse, X, &I); 000936 #ifndef SQLITE_ENABLE_UPDATE_DELETE_LIMIT 000937 if( O || L ){ 000938 updateDeleteLimitError(pParse,O,L); 000939 O = 0; 000940 L = 0; 000941 } 000942 #endif 000943 sqlite3DeleteFrom(pParse,X,W,O,L); 000944 } 000945 %else 000946 cmd ::= with DELETE FROM xfullname(X) indexed_opt(I) where_opt_ret(W). { 000947 sqlite3SrcListIndexedBy(pParse, X, &I); 000948 sqlite3DeleteFrom(pParse,X,W,0,0); 000949 } 000950 %endif 000951 000952 %type where_opt {Expr*} 000953 %destructor where_opt {sqlite3ExprDelete(pParse->db, $$);} 000954 %type where_opt_ret {Expr*} 000955 %destructor where_opt_ret {sqlite3ExprDelete(pParse->db, $$);} 000956 000957 where_opt(A) ::= . {A = 0;} 000958 where_opt(A) ::= WHERE expr(X). {A = X;} 000959 where_opt_ret(A) ::= . {A = 0;} 000960 where_opt_ret(A) ::= WHERE expr(X). {A = X;} 000961 where_opt_ret(A) ::= RETURNING selcollist(X). 000962 {sqlite3AddReturning(pParse,X); A = 0;} 000963 where_opt_ret(A) ::= WHERE expr(X) RETURNING selcollist(Y). 000964 {sqlite3AddReturning(pParse,Y); A = X;} 000965 000966 ////////////////////////// The UPDATE command //////////////////////////////// 000967 // 000968 %if SQLITE_ENABLE_UPDATE_DELETE_LIMIT || SQLITE_UDL_CAPABLE_PARSER 000969 cmd ::= with UPDATE orconf(R) xfullname(X) indexed_opt(I) SET setlist(Y) from(F) 000970 where_opt_ret(W) orderby_opt(O) limit_opt(L). { 000971 sqlite3SrcListIndexedBy(pParse, X, &I); 000972 if( F ){ 000973 SrcList *pFromClause = F; 000974 if( pFromClause->nSrc>1 ){ 000975 Select *pSubquery; 000976 Token as; 000977 pSubquery = sqlite3SelectNew(pParse,0,pFromClause,0,0,0,0,SF_NestedFrom,0); 000978 as.n = 0; 000979 as.z = 0; 000980 pFromClause = sqlite3SrcListAppendFromTerm(pParse,0,0,0,&as,pSubquery,0); 000981 } 000982 X = sqlite3SrcListAppendList(pParse, X, pFromClause); 000983 } 000984 sqlite3ExprListCheckLength(pParse,Y,"set list"); 000985 #ifndef SQLITE_ENABLE_UPDATE_DELETE_LIMIT 000986 if( O || L ){ 000987 updateDeleteLimitError(pParse,O,L); 000988 O = 0; 000989 L = 0; 000990 } 000991 #endif 000992 sqlite3Update(pParse,X,Y,W,R,O,L,0); 000993 } 000994 %else 000995 cmd ::= with UPDATE orconf(R) xfullname(X) indexed_opt(I) SET setlist(Y) from(F) 000996 where_opt_ret(W). { 000997 sqlite3SrcListIndexedBy(pParse, X, &I); 000998 sqlite3ExprListCheckLength(pParse,Y,"set list"); 000999 if( F ){ 001000 SrcList *pFromClause = F; 001001 if( pFromClause->nSrc>1 ){ 001002 Select *pSubquery; 001003 Token as; 001004 pSubquery = sqlite3SelectNew(pParse,0,pFromClause,0,0,0,0,SF_NestedFrom,0); 001005 as.n = 0; 001006 as.z = 0; 001007 pFromClause = sqlite3SrcListAppendFromTerm(pParse,0,0,0,&as,pSubquery,0); 001008 } 001009 X = sqlite3SrcListAppendList(pParse, X, pFromClause); 001010 } 001011 sqlite3Update(pParse,X,Y,W,R,0,0,0); 001012 } 001013 %endif 001014 001015 001016 001017 %type setlist {ExprList*} 001018 %destructor setlist {sqlite3ExprListDelete(pParse->db, $$);} 001019 001020 setlist(A) ::= setlist(A) COMMA nm(X) EQ expr(Y). { 001021 A = sqlite3ExprListAppend(pParse, A, Y); 001022 sqlite3ExprListSetName(pParse, A, &X, 1); 001023 } 001024 setlist(A) ::= setlist(A) COMMA LP idlist(X) RP EQ expr(Y). { 001025 A = sqlite3ExprListAppendVector(pParse, A, X, Y); 001026 } 001027 setlist(A) ::= nm(X) EQ expr(Y). { 001028 A = sqlite3ExprListAppend(pParse, 0, Y); 001029 sqlite3ExprListSetName(pParse, A, &X, 1); 001030 } 001031 setlist(A) ::= LP idlist(X) RP EQ expr(Y). { 001032 A = sqlite3ExprListAppendVector(pParse, 0, X, Y); 001033 } 001034 001035 ////////////////////////// The INSERT command ///////////////////////////////// 001036 // 001037 cmd ::= with insert_cmd(R) INTO xfullname(X) idlist_opt(F) select(S) 001038 upsert(U). { 001039 sqlite3Insert(pParse, X, S, F, R, U); 001040 } 001041 cmd ::= with insert_cmd(R) INTO xfullname(X) idlist_opt(F) DEFAULT VALUES returning. 001042 { 001043 sqlite3Insert(pParse, X, 0, F, R, 0); 001044 } 001045 001046 %type upsert {Upsert*} 001047 001048 // Because upsert only occurs at the tip end of the INSERT rule for cmd, 001049 // there is never a case where the value of the upsert pointer will not 001050 // be destroyed by the cmd action. So comment-out the destructor to 001051 // avoid unreachable code. 001052 //%destructor upsert {sqlite3UpsertDelete(pParse->db,$$);} 001053 upsert(A) ::= . { A = 0; } 001054 upsert(A) ::= RETURNING selcollist(X). { A = 0; sqlite3AddReturning(pParse,X); } 001055 upsert(A) ::= ON CONFLICT LP sortlist(T) RP where_opt(TW) 001056 DO UPDATE SET setlist(Z) where_opt(W) upsert(N). 001057 { A = sqlite3UpsertNew(pParse->db,T,TW,Z,W,N);} 001058 upsert(A) ::= ON CONFLICT LP sortlist(T) RP where_opt(TW) DO NOTHING upsert(N). 001059 { A = sqlite3UpsertNew(pParse->db,T,TW,0,0,N); } 001060 upsert(A) ::= ON CONFLICT DO NOTHING returning. 001061 { A = sqlite3UpsertNew(pParse->db,0,0,0,0,0); } 001062 upsert(A) ::= ON CONFLICT DO UPDATE SET setlist(Z) where_opt(W) returning. 001063 { A = sqlite3UpsertNew(pParse->db,0,0,Z,W,0);} 001064 001065 returning ::= RETURNING selcollist(X). {sqlite3AddReturning(pParse,X);} 001066 returning ::= . 001067 001068 %type insert_cmd {int} 001069 insert_cmd(A) ::= INSERT orconf(R). {A = R;} 001070 insert_cmd(A) ::= REPLACE. {A = OE_Replace;} 001071 001072 %type idlist_opt {IdList*} 001073 %destructor idlist_opt {sqlite3IdListDelete(pParse->db, $$);} 001074 %type idlist {IdList*} 001075 %destructor idlist {sqlite3IdListDelete(pParse->db, $$);} 001076 001077 idlist_opt(A) ::= . {A = 0;} 001078 idlist_opt(A) ::= LP idlist(X) RP. {A = X;} 001079 idlist(A) ::= idlist(A) COMMA nm(Y). 001080 {A = sqlite3IdListAppend(pParse,A,&Y);} 001081 idlist(A) ::= nm(Y). 001082 {A = sqlite3IdListAppend(pParse,0,&Y); /*A-overwrites-Y*/} 001083 001084 /////////////////////////// Expression Processing ///////////////////////////// 001085 // 001086 001087 %type expr {Expr*} 001088 %destructor expr {sqlite3ExprDelete(pParse->db, $$);} 001089 %type term {Expr*} 001090 %destructor term {sqlite3ExprDelete(pParse->db, $$);} 001091 001092 %include { 001093 001094 /* Construct a new Expr object from a single token */ 001095 static Expr *tokenExpr(Parse *pParse, int op, Token t){ 001096 Expr *p = sqlite3DbMallocRawNN(pParse->db, sizeof(Expr)+t.n+1); 001097 if( p ){ 001098 /* memset(p, 0, sizeof(Expr)); */ 001099 p->op = (u8)op; 001100 p->affExpr = 0; 001101 p->flags = EP_Leaf; 001102 ExprClearVVAProperties(p); 001103 /* p->iAgg = -1; // Not required */ 001104 p->pLeft = p->pRight = 0; 001105 p->pAggInfo = 0; 001106 memset(&p->x, 0, sizeof(p->x)); 001107 memset(&p->y, 0, sizeof(p->y)); 001108 p->op2 = 0; 001109 p->iTable = 0; 001110 p->iColumn = 0; 001111 p->u.zToken = (char*)&p[1]; 001112 memcpy(p->u.zToken, t.z, t.n); 001113 p->u.zToken[t.n] = 0; 001114 p->w.iOfst = (int)(t.z - pParse->zTail); 001115 if( sqlite3Isquote(p->u.zToken[0]) ){ 001116 sqlite3DequoteExpr(p); 001117 } 001118 #if SQLITE_MAX_EXPR_DEPTH>0 001119 p->nHeight = 1; 001120 #endif 001121 if( IN_RENAME_OBJECT ){ 001122 return (Expr*)sqlite3RenameTokenMap(pParse, (void*)p, &t); 001123 } 001124 } 001125 return p; 001126 } 001127 001128 } 001129 001130 expr(A) ::= term(A). 001131 expr(A) ::= LP expr(X) RP. {A = X;} 001132 expr(A) ::= idj(X). {A=tokenExpr(pParse,TK_ID,X); /*A-overwrites-X*/} 001133 expr(A) ::= nm(X) DOT nm(Y). { 001134 Expr *temp1 = tokenExpr(pParse,TK_ID,X); 001135 Expr *temp2 = tokenExpr(pParse,TK_ID,Y); 001136 A = sqlite3PExpr(pParse, TK_DOT, temp1, temp2); 001137 } 001138 expr(A) ::= nm(X) DOT nm(Y) DOT nm(Z). { 001139 Expr *temp1 = tokenExpr(pParse,TK_ID,X); 001140 Expr *temp2 = tokenExpr(pParse,TK_ID,Y); 001141 Expr *temp3 = tokenExpr(pParse,TK_ID,Z); 001142 Expr *temp4 = sqlite3PExpr(pParse, TK_DOT, temp2, temp3); 001143 if( IN_RENAME_OBJECT ){ 001144 sqlite3RenameTokenRemap(pParse, 0, temp1); 001145 } 001146 A = sqlite3PExpr(pParse, TK_DOT, temp1, temp4); 001147 } 001148 term(A) ::= NULL|FLOAT|BLOB(X). {A=tokenExpr(pParse,@X,X); /*A-overwrites-X*/} 001149 term(A) ::= STRING(X). {A=tokenExpr(pParse,@X,X); /*A-overwrites-X*/} 001150 term(A) ::= INTEGER(X). { 001151 A = sqlite3ExprAlloc(pParse->db, TK_INTEGER, &X, 1); 001152 if( A ) A->w.iOfst = (int)(X.z - pParse->zTail); 001153 } 001154 expr(A) ::= VARIABLE(X). { 001155 if( !(X.z[0]=='#' && sqlite3Isdigit(X.z[1])) ){ 001156 u32 n = X.n; 001157 A = tokenExpr(pParse, TK_VARIABLE, X); 001158 sqlite3ExprAssignVarNumber(pParse, A, n); 001159 }else{ 001160 /* When doing a nested parse, one can include terms in an expression 001161 ** that look like this: #1 #2 ... These terms refer to registers 001162 ** in the virtual machine. #N is the N-th register. */ 001163 Token t = X; /*A-overwrites-X*/ 001164 assert( t.n>=2 ); 001165 if( pParse->nested==0 ){ 001166 parserSyntaxError(pParse, &t); 001167 A = 0; 001168 }else{ 001169 A = sqlite3PExpr(pParse, TK_REGISTER, 0, 0); 001170 if( A ) sqlite3GetInt32(&t.z[1], &A->iTable); 001171 } 001172 } 001173 } 001174 expr(A) ::= expr(A) COLLATE ids(C). { 001175 A = sqlite3ExprAddCollateToken(pParse, A, &C, 1); 001176 } 001177 %ifndef SQLITE_OMIT_CAST 001178 expr(A) ::= CAST LP expr(E) AS typetoken(T) RP. { 001179 A = sqlite3ExprAlloc(pParse->db, TK_CAST, &T, 1); 001180 sqlite3ExprAttachSubtrees(pParse->db, A, E, 0); 001181 } 001182 %endif SQLITE_OMIT_CAST 001183 001184 001185 expr(A) ::= idj(X) LP distinct(D) exprlist(Y) RP. { 001186 A = sqlite3ExprFunction(pParse, Y, &X, D); 001187 } 001188 expr(A) ::= idj(X) LP distinct(D) exprlist(Y) ORDER BY sortlist(O) RP. { 001189 A = sqlite3ExprFunction(pParse, Y, &X, D); 001190 sqlite3ExprAddFunctionOrderBy(pParse, A, O); 001191 } 001192 expr(A) ::= idj(X) LP STAR RP. { 001193 A = sqlite3ExprFunction(pParse, 0, &X, 0); 001194 } 001195 001196 %ifdef SQLITE_ENABLE_ORDERED_SET_AGGREGATES 001197 %include { 001198 /* Generate an expression node that represents an ordered-set aggregate function. 001199 ** 001200 ** SQLite does not do anything special to evaluate ordered-set aggregates. The 001201 ** aggregate function itself is expected to do any required ordering on its own. 001202 ** This is just syntactic sugar. 001203 ** 001204 ** This syntax: percentile(f) WITHIN GROUP ( ORDER BY y ) 001205 ** 001206 ** Is equivalent to: percentile(y,f) 001207 ** 001208 ** The purpose of this function is to generate an Expr node from the first syntax 001209 ** into a TK_FUNCTION node that looks like it came from the second syntax. 001210 ** 001211 ** Only functions that have the SQLITE_SELFORDER1 perperty are allowed to do this 001212 ** transformation. Because DISTINCT is not allowed in the ordered-set aggregate 001213 ** syntax, an error is raised if DISTINCT is used. 001214 */ 001215 static Expr *sqlite3ExprAddOrderedsetFunction( 001216 Parse *pParse, /* Parsing context */ 001217 Token *pFuncname, /* Name of the function */ 001218 int isDistinct, /* DISTINCT or ALL qualifier */ 001219 ExprList *pOrig, /* Arguments to the function */ 001220 Expr *pOrderby /* Expression in the ORDER BY clause */ 001221 ){ 001222 ExprList *p; /* Modified argument list */ 001223 Expr *pExpr; /* Final result */ 001224 p = sqlite3ExprListAppend(pParse, 0, pOrderby); 001225 if( pOrig ){ 001226 int i; 001227 for(i=0; i<pOrig->nExpr; i++){ 001228 p = sqlite3ExprListAppend(pParse, p, pOrig->a[i].pExpr); 001229 pOrig->a[i].pExpr = 0; 001230 } 001231 sqlite3ExprListDelete(pParse->db, pOrig); 001232 } 001233 pExpr = sqlite3ExprFunction(pParse, p, pFuncname, 0); 001234 if( pParse->nErr==0 ){ 001235 FuncDef *pDef; 001236 u8 enc = ENC(pParse->db); 001237 assert( pExpr!=0 ); /* Because otherwise pParse->nErr would not be zero */ 001238 assert( p!=0 ); /* Because otherwise pParse->nErr would not be zero */ 001239 pDef = sqlite3FindFunction(pParse->db, pExpr->u.zToken, -2, enc, 0); 001240 if( pDef==0 || (pDef->funcFlags & SQLITE_SELFORDER1)==0 ){ 001241 sqlite3ErrorMsg(pParse, "%#T() is not an ordered-set aggregate", pExpr); 001242 }else if( isDistinct==SF_Distinct ){ 001243 sqlite3ErrorMsg(pParse, "DISTINCT not allowed on ordered-set aggregate %T()", 001244 pFuncname); 001245 } 001246 } 001247 return pExpr; 001248 } 001249 } 001250 expr(A) ::= idj(X) LP distinct(D) exprlist(Y) RP WITHIN GROUP LP ORDER BY expr(E) RP. { 001251 A = sqlite3ExprAddOrderedsetFunction(pParse, &X, D, Y, E); 001252 } 001253 %endif SQLITE_ENABLE_ORDERED_SET_AGGREGATES 001254 001255 %ifndef SQLITE_OMIT_WINDOWFUNC 001256 expr(A) ::= idj(X) LP distinct(D) exprlist(Y) RP filter_over(Z). { 001257 A = sqlite3ExprFunction(pParse, Y, &X, D); 001258 sqlite3WindowAttach(pParse, A, Z); 001259 } 001260 expr(A) ::= idj(X) LP distinct(D) exprlist(Y) ORDER BY sortlist(O) RP filter_over(Z). { 001261 A = sqlite3ExprFunction(pParse, Y, &X, D); 001262 sqlite3WindowAttach(pParse, A, Z); 001263 sqlite3ExprAddFunctionOrderBy(pParse, A, O); 001264 } 001265 expr(A) ::= idj(X) LP STAR RP filter_over(Z). { 001266 A = sqlite3ExprFunction(pParse, 0, &X, 0); 001267 sqlite3WindowAttach(pParse, A, Z); 001268 } 001269 %ifdef SQLITE_ENABLE_ORDERED_SET_AGGREGATES 001270 expr(A) ::= idj(X) LP distinct(D) exprlist(Y) RP WITHIN GROUP LP ORDER BY expr(E) RP 001271 filter_over(Z). { 001272 A = sqlite3ExprAddOrderedsetFunction(pParse, &X, D, Y, E); 001273 sqlite3WindowAttach(pParse, A, Z); 001274 } 001275 %endif SQLITE_ENABLE_ORDERED_SET_AGGREGATES 001276 001277 %endif SQLITE_OMIT_WINDOWFUNC 001278 001279 term(A) ::= CTIME_KW(OP). { 001280 A = sqlite3ExprFunction(pParse, 0, &OP, 0); 001281 } 001282 001283 expr(A) ::= LP nexprlist(X) COMMA expr(Y) RP. { 001284 ExprList *pList = sqlite3ExprListAppend(pParse, X, Y); 001285 A = sqlite3PExpr(pParse, TK_VECTOR, 0, 0); 001286 if( A ){ 001287 A->x.pList = pList; 001288 if( ALWAYS(pList->nExpr) ){ 001289 A->flags |= pList->a[0].pExpr->flags & EP_Propagate; 001290 } 001291 }else{ 001292 sqlite3ExprListDelete(pParse->db, pList); 001293 } 001294 } 001295 001296 expr(A) ::= expr(A) AND expr(Y). {A=sqlite3ExprAnd(pParse,A,Y);} 001297 expr(A) ::= expr(A) OR(OP) expr(Y). {A=sqlite3PExpr(pParse,@OP,A,Y);} 001298 expr(A) ::= expr(A) LT|GT|GE|LE(OP) expr(Y). 001299 {A=sqlite3PExpr(pParse,@OP,A,Y);} 001300 expr(A) ::= expr(A) EQ|NE(OP) expr(Y). {A=sqlite3PExpr(pParse,@OP,A,Y);} 001301 expr(A) ::= expr(A) BITAND|BITOR|LSHIFT|RSHIFT(OP) expr(Y). 001302 {A=sqlite3PExpr(pParse,@OP,A,Y);} 001303 expr(A) ::= expr(A) PLUS|MINUS(OP) expr(Y). 001304 {A=sqlite3PExpr(pParse,@OP,A,Y);} 001305 expr(A) ::= expr(A) STAR|SLASH|REM(OP) expr(Y). 001306 {A=sqlite3PExpr(pParse,@OP,A,Y);} 001307 expr(A) ::= expr(A) CONCAT(OP) expr(Y). {A=sqlite3PExpr(pParse,@OP,A,Y);} 001308 %type likeop {Token} 001309 likeop(A) ::= LIKE_KW|MATCH(A). 001310 likeop(A) ::= NOT LIKE_KW|MATCH(X). {A=X; A.n|=0x80000000; /*A-overwrite-X*/} 001311 expr(A) ::= expr(A) likeop(OP) expr(Y). [LIKE_KW] { 001312 ExprList *pList; 001313 int bNot = OP.n & 0x80000000; 001314 OP.n &= 0x7fffffff; 001315 pList = sqlite3ExprListAppend(pParse,0, Y); 001316 pList = sqlite3ExprListAppend(pParse,pList, A); 001317 A = sqlite3ExprFunction(pParse, pList, &OP, 0); 001318 if( bNot ) A = sqlite3PExpr(pParse, TK_NOT, A, 0); 001319 if( A ) A->flags |= EP_InfixFunc; 001320 } 001321 expr(A) ::= expr(A) likeop(OP) expr(Y) ESCAPE expr(E). [LIKE_KW] { 001322 ExprList *pList; 001323 int bNot = OP.n & 0x80000000; 001324 OP.n &= 0x7fffffff; 001325 pList = sqlite3ExprListAppend(pParse,0, Y); 001326 pList = sqlite3ExprListAppend(pParse,pList, A); 001327 pList = sqlite3ExprListAppend(pParse,pList, E); 001328 A = sqlite3ExprFunction(pParse, pList, &OP, 0); 001329 if( bNot ) A = sqlite3PExpr(pParse, TK_NOT, A, 0); 001330 if( A ) A->flags |= EP_InfixFunc; 001331 } 001332 001333 expr(A) ::= expr(A) ISNULL|NOTNULL(E). {A = sqlite3PExpr(pParse,@E,A,0);} 001334 expr(A) ::= expr(A) NOT NULL. {A = sqlite3PExpr(pParse,TK_NOTNULL,A,0);} 001335 001336 %include { 001337 /* A routine to convert a binary TK_IS or TK_ISNOT expression into a 001338 ** unary TK_ISNULL or TK_NOTNULL expression. */ 001339 static void binaryToUnaryIfNull(Parse *pParse, Expr *pY, Expr *pA, int op){ 001340 sqlite3 *db = pParse->db; 001341 if( pA && pY && pY->op==TK_NULL && !IN_RENAME_OBJECT ){ 001342 pA->op = (u8)op; 001343 sqlite3ExprDelete(db, pA->pRight); 001344 pA->pRight = 0; 001345 } 001346 } 001347 } 001348 001349 // expr1 IS expr2 001350 // expr1 IS NOT expr2 001351 // 001352 // If expr2 is NULL then code as TK_ISNULL or TK_NOTNULL. If expr2 001353 // is any other expression, code as TK_IS or TK_ISNOT. 001354 // 001355 expr(A) ::= expr(A) IS expr(Y). { 001356 A = sqlite3PExpr(pParse,TK_IS,A,Y); 001357 binaryToUnaryIfNull(pParse, Y, A, TK_ISNULL); 001358 } 001359 expr(A) ::= expr(A) IS NOT expr(Y). { 001360 A = sqlite3PExpr(pParse,TK_ISNOT,A,Y); 001361 binaryToUnaryIfNull(pParse, Y, A, TK_NOTNULL); 001362 } 001363 expr(A) ::= expr(A) IS NOT DISTINCT FROM expr(Y). { 001364 A = sqlite3PExpr(pParse,TK_IS,A,Y); 001365 binaryToUnaryIfNull(pParse, Y, A, TK_ISNULL); 001366 } 001367 expr(A) ::= expr(A) IS DISTINCT FROM expr(Y). { 001368 A = sqlite3PExpr(pParse,TK_ISNOT,A,Y); 001369 binaryToUnaryIfNull(pParse, Y, A, TK_NOTNULL); 001370 } 001371 001372 expr(A) ::= NOT(B) expr(X). 001373 {A = sqlite3PExpr(pParse, @B, X, 0);/*A-overwrites-B*/} 001374 expr(A) ::= BITNOT(B) expr(X). 001375 {A = sqlite3PExpr(pParse, @B, X, 0);/*A-overwrites-B*/} 001376 expr(A) ::= PLUS|MINUS(B) expr(X). [BITNOT] { 001377 Expr *p = X; 001378 u8 op = @B + (TK_UPLUS-TK_PLUS); 001379 assert( TK_UPLUS>TK_PLUS ); 001380 assert( TK_UMINUS == TK_MINUS + (TK_UPLUS - TK_PLUS) ); 001381 if( p && p->op==TK_UPLUS ){ 001382 p->op = op; 001383 A = p; 001384 }else{ 001385 A = sqlite3PExpr(pParse, op, p, 0); 001386 /*A-overwrites-B*/ 001387 } 001388 } 001389 001390 expr(A) ::= expr(B) PTR(C) expr(D). { 001391 ExprList *pList = sqlite3ExprListAppend(pParse, 0, B); 001392 pList = sqlite3ExprListAppend(pParse, pList, D); 001393 A = sqlite3ExprFunction(pParse, pList, &C, 0); 001394 } 001395 001396 %type between_op {int} 001397 between_op(A) ::= BETWEEN. {A = 0;} 001398 between_op(A) ::= NOT BETWEEN. {A = 1;} 001399 expr(A) ::= expr(A) between_op(N) expr(X) AND expr(Y). [BETWEEN] { 001400 ExprList *pList = sqlite3ExprListAppend(pParse,0, X); 001401 pList = sqlite3ExprListAppend(pParse,pList, Y); 001402 A = sqlite3PExpr(pParse, TK_BETWEEN, A, 0); 001403 if( A ){ 001404 A->x.pList = pList; 001405 }else{ 001406 sqlite3ExprListDelete(pParse->db, pList); 001407 } 001408 if( N ) A = sqlite3PExpr(pParse, TK_NOT, A, 0); 001409 } 001410 %ifndef SQLITE_OMIT_SUBQUERY 001411 %type in_op {int} 001412 in_op(A) ::= IN. {A = 0;} 001413 in_op(A) ::= NOT IN. {A = 1;} 001414 expr(A) ::= expr(A) in_op(N) LP exprlist(Y) RP. [IN] { 001415 if( Y==0 ){ 001416 /* Expressions of the form 001417 ** 001418 ** expr1 IN () 001419 ** expr1 NOT IN () 001420 ** 001421 ** simplify to constants 0 (false) and 1 (true), respectively, 001422 ** regardless of the value of expr1. 001423 */ 001424 sqlite3ExprUnmapAndDelete(pParse, A); 001425 A = sqlite3Expr(pParse->db, TK_STRING, N ? "true" : "false"); 001426 if( A ) sqlite3ExprIdToTrueFalse(A); 001427 }else{ 001428 Expr *pRHS = Y->a[0].pExpr; 001429 if( Y->nExpr==1 && sqlite3ExprIsConstant(pParse,pRHS) && A->op!=TK_VECTOR ){ 001430 Y->a[0].pExpr = 0; 001431 sqlite3ExprListDelete(pParse->db, Y); 001432 pRHS = sqlite3PExpr(pParse, TK_UPLUS, pRHS, 0); 001433 A = sqlite3PExpr(pParse, TK_EQ, A, pRHS); 001434 }else if( Y->nExpr==1 && pRHS->op==TK_SELECT ){ 001435 A = sqlite3PExpr(pParse, TK_IN, A, 0); 001436 sqlite3PExprAddSelect(pParse, A, pRHS->x.pSelect); 001437 pRHS->x.pSelect = 0; 001438 sqlite3ExprListDelete(pParse->db, Y); 001439 }else{ 001440 A = sqlite3PExpr(pParse, TK_IN, A, 0); 001441 if( A==0 ){ 001442 sqlite3ExprListDelete(pParse->db, Y); 001443 }else if( A->pLeft->op==TK_VECTOR ){ 001444 int nExpr = A->pLeft->x.pList->nExpr; 001445 Select *pSelectRHS = sqlite3ExprListToValues(pParse, nExpr, Y); 001446 if( pSelectRHS ){ 001447 parserDoubleLinkSelect(pParse, pSelectRHS); 001448 sqlite3PExprAddSelect(pParse, A, pSelectRHS); 001449 } 001450 }else{ 001451 A->x.pList = Y; 001452 sqlite3ExprSetHeightAndFlags(pParse, A); 001453 } 001454 } 001455 if( N ) A = sqlite3PExpr(pParse, TK_NOT, A, 0); 001456 } 001457 } 001458 expr(A) ::= LP select(X) RP. { 001459 A = sqlite3PExpr(pParse, TK_SELECT, 0, 0); 001460 sqlite3PExprAddSelect(pParse, A, X); 001461 } 001462 expr(A) ::= expr(A) in_op(N) LP select(Y) RP. [IN] { 001463 A = sqlite3PExpr(pParse, TK_IN, A, 0); 001464 sqlite3PExprAddSelect(pParse, A, Y); 001465 if( N ) A = sqlite3PExpr(pParse, TK_NOT, A, 0); 001466 } 001467 expr(A) ::= expr(A) in_op(N) nm(Y) dbnm(Z) paren_exprlist(E). [IN] { 001468 SrcList *pSrc = sqlite3SrcListAppend(pParse, 0,&Y,&Z); 001469 Select *pSelect = sqlite3SelectNew(pParse, 0,pSrc,0,0,0,0,0,0); 001470 if( E ) sqlite3SrcListFuncArgs(pParse, pSelect ? pSrc : 0, E); 001471 A = sqlite3PExpr(pParse, TK_IN, A, 0); 001472 sqlite3PExprAddSelect(pParse, A, pSelect); 001473 if( N ) A = sqlite3PExpr(pParse, TK_NOT, A, 0); 001474 } 001475 expr(A) ::= EXISTS LP select(Y) RP. { 001476 Expr *p; 001477 p = A = sqlite3PExpr(pParse, TK_EXISTS, 0, 0); 001478 sqlite3PExprAddSelect(pParse, p, Y); 001479 } 001480 %endif SQLITE_OMIT_SUBQUERY 001481 001482 /* CASE expressions */ 001483 expr(A) ::= CASE case_operand(X) case_exprlist(Y) case_else(Z) END. { 001484 A = sqlite3PExpr(pParse, TK_CASE, X, 0); 001485 if( A ){ 001486 A->x.pList = Z ? sqlite3ExprListAppend(pParse,Y,Z) : Y; 001487 sqlite3ExprSetHeightAndFlags(pParse, A); 001488 }else{ 001489 sqlite3ExprListDelete(pParse->db, Y); 001490 sqlite3ExprDelete(pParse->db, Z); 001491 } 001492 } 001493 %type case_exprlist {ExprList*} 001494 %destructor case_exprlist {sqlite3ExprListDelete(pParse->db, $$);} 001495 case_exprlist(A) ::= case_exprlist(A) WHEN expr(Y) THEN expr(Z). { 001496 A = sqlite3ExprListAppend(pParse,A, Y); 001497 A = sqlite3ExprListAppend(pParse,A, Z); 001498 } 001499 case_exprlist(A) ::= WHEN expr(Y) THEN expr(Z). { 001500 A = sqlite3ExprListAppend(pParse,0, Y); 001501 A = sqlite3ExprListAppend(pParse,A, Z); 001502 } 001503 %type case_else {Expr*} 001504 %destructor case_else {sqlite3ExprDelete(pParse->db, $$);} 001505 case_else(A) ::= ELSE expr(X). {A = X;} 001506 case_else(A) ::= . {A = 0;} 001507 %type case_operand {Expr*} 001508 %destructor case_operand {sqlite3ExprDelete(pParse->db, $$);} 001509 case_operand(A) ::= expr(A). 001510 case_operand(A) ::= . {A = 0;} 001511 001512 %type exprlist {ExprList*} 001513 %destructor exprlist {sqlite3ExprListDelete(pParse->db, $$);} 001514 %type nexprlist {ExprList*} 001515 %destructor nexprlist {sqlite3ExprListDelete(pParse->db, $$);} 001516 001517 exprlist(A) ::= nexprlist(A). 001518 exprlist(A) ::= . {A = 0;} 001519 nexprlist(A) ::= nexprlist(A) COMMA expr(Y). 001520 {A = sqlite3ExprListAppend(pParse,A,Y);} 001521 nexprlist(A) ::= expr(Y). 001522 {A = sqlite3ExprListAppend(pParse,0,Y); /*A-overwrites-Y*/} 001523 001524 %ifndef SQLITE_OMIT_SUBQUERY 001525 /* A paren_exprlist is an optional expression list contained inside 001526 ** of parenthesis */ 001527 %type paren_exprlist {ExprList*} 001528 %destructor paren_exprlist {sqlite3ExprListDelete(pParse->db, $$);} 001529 paren_exprlist(A) ::= . {A = 0;} 001530 paren_exprlist(A) ::= LP exprlist(X) RP. {A = X;} 001531 %endif SQLITE_OMIT_SUBQUERY 001532 001533 001534 ///////////////////////////// The CREATE INDEX command /////////////////////// 001535 // 001536 cmd ::= createkw(S) uniqueflag(U) INDEX ifnotexists(NE) nm(X) dbnm(D) 001537 ON nm(Y) LP sortlist(Z) RP where_opt(W). { 001538 sqlite3CreateIndex(pParse, &X, &D, 001539 sqlite3SrcListAppend(pParse,0,&Y,0), Z, U, 001540 &S, W, SQLITE_SO_ASC, NE, SQLITE_IDXTYPE_APPDEF); 001541 if( IN_RENAME_OBJECT && pParse->pNewIndex ){ 001542 sqlite3RenameTokenMap(pParse, pParse->pNewIndex->zName, &Y); 001543 } 001544 } 001545 001546 %type uniqueflag {int} 001547 uniqueflag(A) ::= UNIQUE. {A = OE_Abort;} 001548 uniqueflag(A) ::= . {A = OE_None;} 001549 001550 001551 // The eidlist non-terminal (Expression Id List) generates an ExprList 001552 // from a list of identifiers. The identifier names are in ExprList.a[].zName. 001553 // This list is stored in an ExprList rather than an IdList so that it 001554 // can be easily sent to sqlite3ColumnsExprList(). 001555 // 001556 // eidlist is grouped with CREATE INDEX because it used to be the non-terminal 001557 // used for the arguments to an index. That is just an historical accident. 001558 // 001559 // IMPORTANT COMPATIBILITY NOTE: Some prior versions of SQLite accepted 001560 // COLLATE clauses and ASC or DESC keywords on ID lists in inappropriate 001561 // places - places that might have been stored in the sqlite_schema table. 001562 // Those extra features were ignored. But because they might be in some 001563 // (busted) old databases, we need to continue parsing them when loading 001564 // historical schemas. 001565 // 001566 %type eidlist {ExprList*} 001567 %destructor eidlist {sqlite3ExprListDelete(pParse->db, $$);} 001568 %type eidlist_opt {ExprList*} 001569 %destructor eidlist_opt {sqlite3ExprListDelete(pParse->db, $$);} 001570 001571 %include { 001572 /* Add a single new term to an ExprList that is used to store a 001573 ** list of identifiers. Report an error if the ID list contains 001574 ** a COLLATE clause or an ASC or DESC keyword, except ignore the 001575 ** error while parsing a legacy schema. 001576 */ 001577 static ExprList *parserAddExprIdListTerm( 001578 Parse *pParse, 001579 ExprList *pPrior, 001580 Token *pIdToken, 001581 int hasCollate, 001582 int sortOrder 001583 ){ 001584 ExprList *p = sqlite3ExprListAppend(pParse, pPrior, 0); 001585 if( (hasCollate || sortOrder!=SQLITE_SO_UNDEFINED) 001586 && pParse->db->init.busy==0 001587 ){ 001588 sqlite3ErrorMsg(pParse, "syntax error after column name \"%.*s\"", 001589 pIdToken->n, pIdToken->z); 001590 } 001591 sqlite3ExprListSetName(pParse, p, pIdToken, 1); 001592 return p; 001593 } 001594 } // end %include 001595 001596 eidlist_opt(A) ::= . {A = 0;} 001597 eidlist_opt(A) ::= LP eidlist(X) RP. {A = X;} 001598 eidlist(A) ::= eidlist(A) COMMA nm(Y) collate(C) sortorder(Z). { 001599 A = parserAddExprIdListTerm(pParse, A, &Y, C, Z); 001600 } 001601 eidlist(A) ::= nm(Y) collate(C) sortorder(Z). { 001602 A = parserAddExprIdListTerm(pParse, 0, &Y, C, Z); /*A-overwrites-Y*/ 001603 } 001604 001605 %type collate {int} 001606 collate(C) ::= . {C = 0;} 001607 collate(C) ::= COLLATE ids. {C = 1;} 001608 001609 001610 ///////////////////////////// The DROP INDEX command ///////////////////////// 001611 // 001612 cmd ::= DROP INDEX ifexists(E) fullname(X). {sqlite3DropIndex(pParse, X, E);} 001613 001614 ///////////////////////////// The VACUUM command ///////////////////////////// 001615 // 001616 %if !SQLITE_OMIT_VACUUM && !SQLITE_OMIT_ATTACH 001617 %type vinto {Expr*} 001618 %destructor vinto {sqlite3ExprDelete(pParse->db, $$);} 001619 cmd ::= VACUUM vinto(Y). {sqlite3Vacuum(pParse,0,Y);} 001620 cmd ::= VACUUM nm(X) vinto(Y). {sqlite3Vacuum(pParse,&X,Y);} 001621 vinto(A) ::= INTO expr(X). {A = X;} 001622 vinto(A) ::= . {A = 0;} 001623 %endif 001624 001625 ///////////////////////////// The PRAGMA command ///////////////////////////// 001626 // 001627 %ifndef SQLITE_OMIT_PRAGMA 001628 cmd ::= PRAGMA nm(X) dbnm(Z). {sqlite3Pragma(pParse,&X,&Z,0,0);} 001629 cmd ::= PRAGMA nm(X) dbnm(Z) EQ nmnum(Y). {sqlite3Pragma(pParse,&X,&Z,&Y,0);} 001630 cmd ::= PRAGMA nm(X) dbnm(Z) LP nmnum(Y) RP. {sqlite3Pragma(pParse,&X,&Z,&Y,0);} 001631 cmd ::= PRAGMA nm(X) dbnm(Z) EQ minus_num(Y). 001632 {sqlite3Pragma(pParse,&X,&Z,&Y,1);} 001633 cmd ::= PRAGMA nm(X) dbnm(Z) LP minus_num(Y) RP. 001634 {sqlite3Pragma(pParse,&X,&Z,&Y,1);} 001635 001636 nmnum(A) ::= plus_num(A). 001637 nmnum(A) ::= nm(A). 001638 nmnum(A) ::= ON(A). 001639 nmnum(A) ::= DELETE(A). 001640 nmnum(A) ::= DEFAULT(A). 001641 %endif SQLITE_OMIT_PRAGMA 001642 %token_class number INTEGER|FLOAT. 001643 plus_num(A) ::= PLUS number(X). {A = X;} 001644 plus_num(A) ::= number(A). 001645 minus_num(A) ::= MINUS number(X). {A = X;} 001646 //////////////////////////// The CREATE TRIGGER command ///////////////////// 001647 001648 %ifndef SQLITE_OMIT_TRIGGER 001649 001650 cmd ::= createkw trigger_decl(A) BEGIN trigger_cmd_list(S) END(Z). { 001651 Token all; 001652 all.z = A.z; 001653 all.n = (int)(Z.z - A.z) + Z.n; 001654 sqlite3FinishTrigger(pParse, S, &all); 001655 } 001656 001657 trigger_decl(A) ::= temp(T) TRIGGER ifnotexists(NOERR) nm(B) dbnm(Z) 001658 trigger_time(C) trigger_event(D) 001659 ON fullname(E) foreach_clause when_clause(G). { 001660 sqlite3BeginTrigger(pParse, &B, &Z, C, D.a, D.b, E, G, T, NOERR); 001661 A = (Z.n==0?B:Z); /*A-overwrites-T*/ 001662 } 001663 001664 %type trigger_time {int} 001665 trigger_time(A) ::= BEFORE|AFTER(X). { A = @X; /*A-overwrites-X*/ } 001666 trigger_time(A) ::= INSTEAD OF. { A = TK_INSTEAD;} 001667 trigger_time(A) ::= . { A = TK_BEFORE; } 001668 001669 %type trigger_event {struct TrigEvent} 001670 %destructor trigger_event {sqlite3IdListDelete(pParse->db, $$.b);} 001671 trigger_event(A) ::= DELETE|INSERT(X). {A.a = @X; /*A-overwrites-X*/ A.b = 0;} 001672 trigger_event(A) ::= UPDATE(X). {A.a = @X; /*A-overwrites-X*/ A.b = 0;} 001673 trigger_event(A) ::= UPDATE OF idlist(X).{A.a = TK_UPDATE; A.b = X;} 001674 001675 foreach_clause ::= . 001676 foreach_clause ::= FOR EACH ROW. 001677 001678 %type when_clause {Expr*} 001679 %destructor when_clause {sqlite3ExprDelete(pParse->db, $$);} 001680 when_clause(A) ::= . { A = 0; } 001681 when_clause(A) ::= WHEN expr(X). { A = X; } 001682 001683 %type trigger_cmd_list {TriggerStep*} 001684 %destructor trigger_cmd_list {sqlite3DeleteTriggerStep(pParse->db, $$);} 001685 trigger_cmd_list(A) ::= trigger_cmd_list(A) trigger_cmd(X) SEMI. { 001686 assert( A!=0 ); 001687 A->pLast->pNext = X; 001688 A->pLast = X; 001689 } 001690 trigger_cmd_list(A) ::= trigger_cmd(A) SEMI. { 001691 assert( A!=0 ); 001692 A->pLast = A; 001693 } 001694 001695 // Disallow qualified table names on INSERT, UPDATE, and DELETE statements 001696 // within a trigger. The table to INSERT, UPDATE, or DELETE is always in 001697 // the same database as the table that the trigger fires on. 001698 // 001699 %type trnm {Token} 001700 trnm(A) ::= nm(A). 001701 trnm(A) ::= nm DOT nm(X). { 001702 A = X; 001703 sqlite3ErrorMsg(pParse, 001704 "qualified table names are not allowed on INSERT, UPDATE, and DELETE " 001705 "statements within triggers"); 001706 } 001707 001708 // Disallow the INDEX BY and NOT INDEXED clauses on UPDATE and DELETE 001709 // statements within triggers. We make a specific error message for this 001710 // since it is an exception to the default grammar rules. 001711 // 001712 tridxby ::= . 001713 tridxby ::= INDEXED BY nm. { 001714 sqlite3ErrorMsg(pParse, 001715 "the INDEXED BY clause is not allowed on UPDATE or DELETE statements " 001716 "within triggers"); 001717 } 001718 tridxby ::= NOT INDEXED. { 001719 sqlite3ErrorMsg(pParse, 001720 "the NOT INDEXED clause is not allowed on UPDATE or DELETE statements " 001721 "within triggers"); 001722 } 001723 001724 001725 001726 %type trigger_cmd {TriggerStep*} 001727 %destructor trigger_cmd {sqlite3DeleteTriggerStep(pParse->db, $$);} 001728 // UPDATE 001729 trigger_cmd(A) ::= 001730 UPDATE(B) orconf(R) trnm(X) tridxby SET setlist(Y) from(F) where_opt(Z) scanpt(E). 001731 {A = sqlite3TriggerUpdateStep(pParse, &X, F, Y, Z, R, B.z, E);} 001732 001733 // INSERT 001734 trigger_cmd(A) ::= scanpt(B) insert_cmd(R) INTO 001735 trnm(X) idlist_opt(F) select(S) upsert(U) scanpt(Z). { 001736 A = sqlite3TriggerInsertStep(pParse,&X,F,S,R,U,B,Z);/*A-overwrites-R*/ 001737 } 001738 // DELETE 001739 trigger_cmd(A) ::= DELETE(B) FROM trnm(X) tridxby where_opt(Y) scanpt(E). 001740 {A = sqlite3TriggerDeleteStep(pParse, &X, Y, B.z, E);} 001741 001742 // SELECT 001743 trigger_cmd(A) ::= scanpt(B) select(X) scanpt(E). 001744 {A = sqlite3TriggerSelectStep(pParse->db, X, B, E); /*A-overwrites-X*/} 001745 001746 // The special RAISE expression that may occur in trigger programs 001747 expr(A) ::= RAISE LP IGNORE RP. { 001748 A = sqlite3PExpr(pParse, TK_RAISE, 0, 0); 001749 if( A ){ 001750 A->affExpr = OE_Ignore; 001751 } 001752 } 001753 expr(A) ::= RAISE LP raisetype(T) COMMA expr(Z) RP. { 001754 A = sqlite3PExpr(pParse, TK_RAISE, Z, 0); 001755 if( A ) { 001756 A->affExpr = (char)T; 001757 } 001758 } 001759 %endif !SQLITE_OMIT_TRIGGER 001760 001761 %type raisetype {int} 001762 raisetype(A) ::= ROLLBACK. {A = OE_Rollback;} 001763 raisetype(A) ::= ABORT. {A = OE_Abort;} 001764 raisetype(A) ::= FAIL. {A = OE_Fail;} 001765 001766 001767 //////////////////////// DROP TRIGGER statement ////////////////////////////// 001768 %ifndef SQLITE_OMIT_TRIGGER 001769 cmd ::= DROP TRIGGER ifexists(NOERR) fullname(X). { 001770 sqlite3DropTrigger(pParse,X,NOERR); 001771 } 001772 %endif !SQLITE_OMIT_TRIGGER 001773 001774 //////////////////////// ATTACH DATABASE file AS name ///////////////////////// 001775 %ifndef SQLITE_OMIT_ATTACH 001776 cmd ::= ATTACH database_kw_opt expr(F) AS expr(D) key_opt(K). { 001777 sqlite3Attach(pParse, F, D, K); 001778 } 001779 cmd ::= DETACH database_kw_opt expr(D). { 001780 sqlite3Detach(pParse, D); 001781 } 001782 001783 %type key_opt {Expr*} 001784 %destructor key_opt {sqlite3ExprDelete(pParse->db, $$);} 001785 key_opt(A) ::= . { A = 0; } 001786 key_opt(A) ::= KEY expr(X). { A = X; } 001787 001788 database_kw_opt ::= DATABASE. 001789 database_kw_opt ::= . 001790 %endif SQLITE_OMIT_ATTACH 001791 001792 ////////////////////////// REINDEX collation ////////////////////////////////// 001793 %ifndef SQLITE_OMIT_REINDEX 001794 cmd ::= REINDEX. {sqlite3Reindex(pParse, 0, 0);} 001795 cmd ::= REINDEX nm(X) dbnm(Y). {sqlite3Reindex(pParse, &X, &Y);} 001796 %endif SQLITE_OMIT_REINDEX 001797 001798 /////////////////////////////////// ANALYZE /////////////////////////////////// 001799 %ifndef SQLITE_OMIT_ANALYZE 001800 cmd ::= ANALYZE. {sqlite3Analyze(pParse, 0, 0);} 001801 cmd ::= ANALYZE nm(X) dbnm(Y). {sqlite3Analyze(pParse, &X, &Y);} 001802 %endif 001803 001804 //////////////////////// ALTER TABLE table ... //////////////////////////////// 001805 %ifndef SQLITE_OMIT_ALTERTABLE 001806 %ifndef SQLITE_OMIT_VIRTUALTABLE 001807 cmd ::= ALTER TABLE fullname(X) RENAME TO nm(Z). { 001808 sqlite3AlterRenameTable(pParse,X,&Z); 001809 } 001810 cmd ::= ALTER TABLE add_column_fullname 001811 ADD kwcolumn_opt columnname(Y) carglist. { 001812 Y.n = (int)(pParse->sLastToken.z-Y.z) + pParse->sLastToken.n; 001813 sqlite3AlterFinishAddColumn(pParse, &Y); 001814 } 001815 cmd ::= ALTER TABLE fullname(X) DROP kwcolumn_opt nm(Y). { 001816 sqlite3AlterDropColumn(pParse, X, &Y); 001817 } 001818 001819 add_column_fullname ::= fullname(X). { 001820 disableLookaside(pParse); 001821 sqlite3AlterBeginAddColumn(pParse, X); 001822 } 001823 cmd ::= ALTER TABLE fullname(X) RENAME kwcolumn_opt nm(Y) TO nm(Z). { 001824 sqlite3AlterRenameColumn(pParse, X, &Y, &Z); 001825 } 001826 001827 kwcolumn_opt ::= . 001828 kwcolumn_opt ::= COLUMNKW. 001829 001830 %endif SQLITE_OMIT_VIRTUALTABLE 001831 %endif SQLITE_OMIT_ALTERTABLE 001832 001833 //////////////////////// CREATE VIRTUAL TABLE ... ///////////////////////////// 001834 %ifndef SQLITE_OMIT_VIRTUALTABLE 001835 cmd ::= create_vtab. {sqlite3VtabFinishParse(pParse,0);} 001836 cmd ::= create_vtab LP vtabarglist RP(X). {sqlite3VtabFinishParse(pParse,&X);} 001837 create_vtab ::= createkw VIRTUAL TABLE ifnotexists(E) 001838 nm(X) dbnm(Y) USING nm(Z). { 001839 sqlite3VtabBeginParse(pParse, &X, &Y, &Z, E); 001840 } 001841 vtabarglist ::= vtabarg. 001842 vtabarglist ::= vtabarglist COMMA vtabarg. 001843 vtabarg ::= . {sqlite3VtabArgInit(pParse);} 001844 vtabarg ::= vtabarg vtabargtoken. 001845 vtabargtoken ::= ANY(X). {sqlite3VtabArgExtend(pParse,&X);} 001846 vtabargtoken ::= lp anylist RP(X). {sqlite3VtabArgExtend(pParse,&X);} 001847 lp ::= LP(X). {sqlite3VtabArgExtend(pParse,&X);} 001848 anylist ::= . 001849 anylist ::= anylist LP anylist RP. 001850 anylist ::= anylist ANY. 001851 %endif SQLITE_OMIT_VIRTUALTABLE 001852 001853 001854 //////////////////////// COMMON TABLE EXPRESSIONS //////////////////////////// 001855 %type wqlist {With*} 001856 %destructor wqlist {sqlite3WithDelete(pParse->db, $$);} 001857 %type wqitem {Cte*} 001858 // %destructor wqitem {sqlite3CteDelete(pParse->db, $$);} // not reachable 001859 001860 with ::= . 001861 %ifndef SQLITE_OMIT_CTE 001862 with ::= WITH wqlist(W). { sqlite3WithPush(pParse, W, 1); } 001863 with ::= WITH RECURSIVE wqlist(W). { sqlite3WithPush(pParse, W, 1); } 001864 001865 %type wqas {u8} 001866 wqas(A) ::= AS. {A = M10d_Any;} 001867 wqas(A) ::= AS MATERIALIZED. {A = M10d_Yes;} 001868 wqas(A) ::= AS NOT MATERIALIZED. {A = M10d_No;} 001869 wqitem(A) ::= withnm(X) eidlist_opt(Y) wqas(M) LP select(Z) RP. { 001870 A = sqlite3CteNew(pParse, &X, Y, Z, M); /*A-overwrites-X*/ 001871 } 001872 withnm(A) ::= nm(A). {pParse->bHasWith = 1;} 001873 wqlist(A) ::= wqitem(X). { 001874 A = sqlite3WithAdd(pParse, 0, X); /*A-overwrites-X*/ 001875 } 001876 wqlist(A) ::= wqlist(A) COMMA wqitem(X). { 001877 A = sqlite3WithAdd(pParse, A, X); 001878 } 001879 %endif SQLITE_OMIT_CTE 001880 001881 //////////////////////// WINDOW FUNCTION EXPRESSIONS ///////////////////////// 001882 // These must be at the end of this file. Specifically, the rules that 001883 // introduce tokens WINDOW, OVER and FILTER must appear last. This causes 001884 // the integer values assigned to these tokens to be larger than all other 001885 // tokens that may be output by the tokenizer except TK_SPACE, TK_COMMENT, 001886 // and TK_ILLEGAL. 001887 // 001888 %ifndef SQLITE_OMIT_WINDOWFUNC 001889 %type windowdefn_list {Window*} 001890 %destructor windowdefn_list {sqlite3WindowListDelete(pParse->db, $$);} 001891 windowdefn_list(A) ::= windowdefn(A). 001892 windowdefn_list(A) ::= windowdefn_list(Y) COMMA windowdefn(Z). { 001893 assert( Z!=0 ); 001894 sqlite3WindowChain(pParse, Z, Y); 001895 Z->pNextWin = Y; 001896 A = Z; 001897 } 001898 001899 %type windowdefn {Window*} 001900 %destructor windowdefn {sqlite3WindowDelete(pParse->db, $$);} 001901 windowdefn(A) ::= nm(X) AS LP window(Y) RP. { 001902 if( ALWAYS(Y) ){ 001903 Y->zName = sqlite3DbStrNDup(pParse->db, X.z, X.n); 001904 } 001905 A = Y; 001906 } 001907 001908 %type window {Window*} 001909 %destructor window {sqlite3WindowDelete(pParse->db, $$);} 001910 001911 %type frame_opt {Window*} 001912 %destructor frame_opt {sqlite3WindowDelete(pParse->db, $$);} 001913 001914 %type part_opt {ExprList*} 001915 %destructor part_opt {sqlite3ExprListDelete(pParse->db, $$);} 001916 001917 %type filter_clause {Expr*} 001918 %destructor filter_clause {sqlite3ExprDelete(pParse->db, $$);} 001919 001920 %type over_clause {Window*} 001921 %destructor over_clause {sqlite3WindowDelete(pParse->db, $$);} 001922 001923 %type filter_over {Window*} 001924 %destructor filter_over {sqlite3WindowDelete(pParse->db, $$);} 001925 001926 %type range_or_rows {int} 001927 001928 %type frame_bound {struct FrameBound} 001929 %destructor frame_bound {sqlite3ExprDelete(pParse->db, $$.pExpr);} 001930 %type frame_bound_s {struct FrameBound} 001931 %destructor frame_bound_s {sqlite3ExprDelete(pParse->db, $$.pExpr);} 001932 %type frame_bound_e {struct FrameBound} 001933 %destructor frame_bound_e {sqlite3ExprDelete(pParse->db, $$.pExpr);} 001934 001935 window(A) ::= PARTITION BY nexprlist(X) orderby_opt(Y) frame_opt(Z). { 001936 A = sqlite3WindowAssemble(pParse, Z, X, Y, 0); 001937 } 001938 window(A) ::= nm(W) PARTITION BY nexprlist(X) orderby_opt(Y) frame_opt(Z). { 001939 A = sqlite3WindowAssemble(pParse, Z, X, Y, &W); 001940 } 001941 window(A) ::= ORDER BY sortlist(Y) frame_opt(Z). { 001942 A = sqlite3WindowAssemble(pParse, Z, 0, Y, 0); 001943 } 001944 window(A) ::= nm(W) ORDER BY sortlist(Y) frame_opt(Z). { 001945 A = sqlite3WindowAssemble(pParse, Z, 0, Y, &W); 001946 } 001947 window(A) ::= frame_opt(A). 001948 window(A) ::= nm(W) frame_opt(Z). { 001949 A = sqlite3WindowAssemble(pParse, Z, 0, 0, &W); 001950 } 001951 001952 frame_opt(A) ::= . { 001953 A = sqlite3WindowAlloc(pParse, 0, TK_UNBOUNDED, 0, TK_CURRENT, 0, 0); 001954 } 001955 frame_opt(A) ::= range_or_rows(X) frame_bound_s(Y) frame_exclude_opt(Z). { 001956 A = sqlite3WindowAlloc(pParse, X, Y.eType, Y.pExpr, TK_CURRENT, 0, Z); 001957 } 001958 frame_opt(A) ::= range_or_rows(X) BETWEEN frame_bound_s(Y) AND 001959 frame_bound_e(Z) frame_exclude_opt(W). { 001960 A = sqlite3WindowAlloc(pParse, X, Y.eType, Y.pExpr, Z.eType, Z.pExpr, W); 001961 } 001962 001963 range_or_rows(A) ::= RANGE|ROWS|GROUPS(X). {A = @X; /*A-overwrites-X*/} 001964 001965 frame_bound_s(A) ::= frame_bound(X). {A = X;} 001966 frame_bound_s(A) ::= UNBOUNDED(X) PRECEDING. {A.eType = @X; A.pExpr = 0;} 001967 frame_bound_e(A) ::= frame_bound(X). {A = X;} 001968 frame_bound_e(A) ::= UNBOUNDED(X) FOLLOWING. {A.eType = @X; A.pExpr = 0;} 001969 001970 frame_bound(A) ::= expr(X) PRECEDING|FOLLOWING(Y). 001971 {A.eType = @Y; A.pExpr = X;} 001972 frame_bound(A) ::= CURRENT(X) ROW. {A.eType = @X; A.pExpr = 0;} 001973 001974 %type frame_exclude_opt {u8} 001975 frame_exclude_opt(A) ::= . {A = 0;} 001976 frame_exclude_opt(A) ::= EXCLUDE frame_exclude(X). {A = X;} 001977 001978 %type frame_exclude {u8} 001979 frame_exclude(A) ::= NO(X) OTHERS. {A = @X; /*A-overwrites-X*/} 001980 frame_exclude(A) ::= CURRENT(X) ROW. {A = @X; /*A-overwrites-X*/} 001981 frame_exclude(A) ::= GROUP|TIES(X). {A = @X; /*A-overwrites-X*/} 001982 001983 001984 %type window_clause {Window*} 001985 %destructor window_clause {sqlite3WindowListDelete(pParse->db, $$);} 001986 window_clause(A) ::= WINDOW windowdefn_list(B). { A = B; } 001987 001988 filter_over(A) ::= filter_clause(F) over_clause(O). { 001989 if( O ){ 001990 O->pFilter = F; 001991 }else{ 001992 sqlite3ExprDelete(pParse->db, F); 001993 } 001994 A = O; 001995 } 001996 filter_over(A) ::= over_clause(O). { 001997 A = O; 001998 } 001999 filter_over(A) ::= filter_clause(F). { 002000 A = (Window*)sqlite3DbMallocZero(pParse->db, sizeof(Window)); 002001 if( A ){ 002002 A->eFrmType = TK_FILTER; 002003 A->pFilter = F; 002004 }else{ 002005 sqlite3ExprDelete(pParse->db, F); 002006 } 002007 } 002008 002009 over_clause(A) ::= OVER LP window(Z) RP. { 002010 A = Z; 002011 assert( A!=0 ); 002012 } 002013 over_clause(A) ::= OVER nm(Z). { 002014 A = (Window*)sqlite3DbMallocZero(pParse->db, sizeof(Window)); 002015 if( A ){ 002016 A->zName = sqlite3DbStrNDup(pParse->db, Z.z, Z.n); 002017 } 002018 } 002019 002020 filter_clause(A) ::= FILTER LP WHERE expr(X) RP. { A = X; } 002021 %endif /* SQLITE_OMIT_WINDOWFUNC */ 002022 002023 /* 002024 ** The code generator needs some extra TK_ token values for tokens that 002025 ** are synthesized and do not actually appear in the grammar: 002026 */ 002027 %token 002028 COLUMN /* Reference to a table column */ 002029 AGG_FUNCTION /* An aggregate function */ 002030 AGG_COLUMN /* An aggregated column */ 002031 TRUEFALSE /* True or false keyword */ 002032 ISNOT /* Combination of IS and NOT */ 002033 FUNCTION /* A function invocation */ 002034 UPLUS /* Unary plus */ 002035 UMINUS /* Unary minus */ 002036 TRUTH /* IS TRUE or IS FALSE or IS NOT TRUE or IS NOT FALSE */ 002037 REGISTER /* Reference to a VDBE register */ 002038 VECTOR /* Vector */ 002039 SELECT_COLUMN /* Choose a single column from a multi-column SELECT */ 002040 IF_NULL_ROW /* the if-null-row operator */ 002041 ASTERISK /* The "*" in count(*) and similar */ 002042 SPAN /* The span operator */ 002043 ERROR /* An expression containing an error */ 002044 . 002045 002046 term(A) ::= QNUMBER(X). { 002047 A=tokenExpr(pParse,@X,X); 002048 sqlite3DequoteNumber(pParse, A); 002049 } 002050 002051 /* There must be no more than 255 tokens defined above. If this grammar 002052 ** is extended with new rules and tokens, they must either be so few in 002053 ** number that TK_SPAN is no more than 255, or else the new tokens must 002054 ** appear after this line. 002055 */ 002056 %include { 002057 #if TK_SPAN>255 002058 # error too many tokens in the grammar 002059 #endif 002060 } 002061 002062 /* 002063 ** The TK_SPACE, TK_COMMENT, and TK_ILLEGAL tokens must be the last three 002064 ** tokens. The parser depends on this. Those tokens are not used in any 002065 ** grammar rule. They are only used by the tokenizer. Declare them last 002066 ** so that they are guaranteed to be the last three. 002067 */ 002068 %token SPACE COMMENT ILLEGAL.