1 %include { 2 /* 3 ** 2001-09-15 4 ** 5 ** The author disclaims copyright to this source code. In place of 6 ** a legal notice, here is a blessing: 7 ** 8 ** May you do good and not evil. 9 ** May you find forgiveness for yourself and forgive others. 10 ** May you share freely, never taking more than you give. 11 ** 12 ************************************************************************* 13 ** This file contains SQLite's SQL parser. 14 ** 15 ** The canonical source code to this file ("parse.y") is a Lemon grammar 16 ** file that specifies the input grammar and actions to take while parsing. 17 ** That input file is processed by Lemon to generate a C-language 18 ** implementation of a parser for the given grammer. You might be reading 19 ** this comment as part of the translated C-code. Edits should be made 20 ** to the original parse.y sources. 21 */ 22 } 23 24 // All token codes are small integers with #defines that begin with "TK_" 25 %token_prefix TK_ 26 27 // The type of the data attached to each token is Token. This is also the 28 // default type for non-terminals. 29 // 30 %token_type {Token} 31 %default_type {Token} 32 33 // An extra argument to the constructor for the parser, which is available 34 // to all actions. 35 %extra_context {Parse *pParse} 36 37 // This code runs whenever there is a syntax error 38 // 39 %syntax_error { 40 UNUSED_PARAMETER(yymajor); /* Silence some compiler warnings */ 41 if( TOKEN.z[0] ){ 42 sqlite3ErrorMsg(pParse, "near \"%T\": syntax error", &TOKEN); 43 }else{ 44 sqlite3ErrorMsg(pParse, "incomplete input"); 45 } 46 } 47 %stack_overflow { 48 sqlite3ErrorMsg(pParse, "parser stack overflow"); 49 } 50 51 // The name of the generated procedure that implements the parser 52 // is as follows: 53 %name sqlite3Parser 54 55 // The following text is included near the beginning of the C source 56 // code file that implements the parser. 57 // 58 %include { 59 #include "sqliteInt.h" 60 61 /* 62 ** Disable all error recovery processing in the parser push-down 63 ** automaton. 64 */ 65 #define YYNOERRORRECOVERY 1 66 67 /* 68 ** Make yytestcase() the same as testcase() 69 */ 70 #define yytestcase(X) testcase(X) 71 72 /* 73 ** Indicate that sqlite3ParserFree() will never be called with a null 74 ** pointer. 75 */ 76 #define YYPARSEFREENEVERNULL 1 77 78 /* 79 ** In the amalgamation, the parse.c file generated by lemon and the 80 ** tokenize.c file are concatenated. In that case, sqlite3RunParser() 81 ** has access to the the size of the yyParser object and so the parser 82 ** engine can be allocated from stack. In that case, only the 83 ** sqlite3ParserInit() and sqlite3ParserFinalize() routines are invoked 84 ** and the sqlite3ParserAlloc() and sqlite3ParserFree() routines can be 85 ** omitted. 86 */ 87 #ifdef SQLITE_AMALGAMATION 88 # define sqlite3Parser_ENGINEALWAYSONSTACK 1 89 #endif 90 91 /* 92 ** Alternative datatype for the argument to the malloc() routine passed 93 ** into sqlite3ParserAlloc(). The default is size_t. 94 */ 95 #define YYMALLOCARGTYPE u64 96 97 /* 98 ** An instance of the following structure describes the event of a 99 ** TRIGGER. "a" is the event type, one of TK_UPDATE, TK_INSERT, 100 ** TK_DELETE, or TK_INSTEAD. If the event is of the form 101 ** 102 ** UPDATE ON (a,b,c) 103 ** 104 ** Then the "b" IdList records the list "a,b,c". 105 */ 106 struct TrigEvent { int a; IdList * b; }; 107 108 struct FrameBound { int eType; Expr *pExpr; }; 109 110 /* 111 ** Disable lookaside memory allocation for objects that might be 112 ** shared across database connections. 113 */ 114 static void disableLookaside(Parse *pParse){ 115 sqlite3 *db = pParse->db; 116 pParse->disableLookaside++; 117 DisableLookaside; 118 } 119 120 #if !defined(SQLITE_ENABLE_UPDATE_DELETE_LIMIT) \ 121 && defined(SQLITE_UDL_CAPABLE_PARSER) 122 /* 123 ** Issue an error message if an ORDER BY or LIMIT clause occurs on an 124 ** UPDATE or DELETE statement. 125 */ 126 static void updateDeleteLimitError( 127 Parse *pParse, 128 ExprList *pOrderBy, 129 Expr *pLimit 130 ){ 131 if( pOrderBy ){ 132 sqlite3ErrorMsg(pParse, "syntax error near \"ORDER BY\""); 133 }else{ 134 sqlite3ErrorMsg(pParse, "syntax error near \"LIMIT\""); 135 } 136 sqlite3ExprListDelete(pParse->db, pOrderBy); 137 sqlite3ExprDelete(pParse->db, pLimit); 138 } 139 #endif /* SQLITE_ENABLE_UPDATE_DELETE_LIMIT */ 140 141 } // end %include 142 143 // Input is a single SQL command 144 input ::= cmdlist. 145 cmdlist ::= cmdlist ecmd. 146 cmdlist ::= ecmd. 147 ecmd ::= SEMI. 148 ecmd ::= cmdx SEMI. 149 %ifndef SQLITE_OMIT_EXPLAIN 150 ecmd ::= explain cmdx SEMI. {NEVER-REDUCE} 151 explain ::= EXPLAIN. { pParse->explain = 1; } 152 explain ::= EXPLAIN QUERY PLAN. { pParse->explain = 2; } 153 %endif SQLITE_OMIT_EXPLAIN 154 cmdx ::= cmd. { sqlite3FinishCoding(pParse); } 155 156 ///////////////////// Begin and end transactions. //////////////////////////// 157 // 158 159 cmd ::= BEGIN transtype(Y) trans_opt. {sqlite3BeginTransaction(pParse, Y);} 160 trans_opt ::= . 161 trans_opt ::= TRANSACTION. 162 trans_opt ::= TRANSACTION nm. 163 %type transtype {int} 164 transtype(A) ::= . {A = TK_DEFERRED;} 165 transtype(A) ::= DEFERRED(X). {A = @X; /*A-overwrites-X*/} 166 transtype(A) ::= IMMEDIATE(X). {A = @X; /*A-overwrites-X*/} 167 transtype(A) ::= EXCLUSIVE(X). {A = @X; /*A-overwrites-X*/} 168 cmd ::= COMMIT|END(X) trans_opt. {sqlite3EndTransaction(pParse,@X);} 169 cmd ::= ROLLBACK(X) trans_opt. {sqlite3EndTransaction(pParse,@X);} 170 171 savepoint_opt ::= SAVEPOINT. 172 savepoint_opt ::= . 173 cmd ::= SAVEPOINT nm(X). { 174 sqlite3Savepoint(pParse, SAVEPOINT_BEGIN, &X); 175 } 176 cmd ::= RELEASE savepoint_opt nm(X). { 177 sqlite3Savepoint(pParse, SAVEPOINT_RELEASE, &X); 178 } 179 cmd ::= ROLLBACK trans_opt TO savepoint_opt nm(X). { 180 sqlite3Savepoint(pParse, SAVEPOINT_ROLLBACK, &X); 181 } 182 183 ///////////////////// The CREATE TABLE statement //////////////////////////// 184 // 185 cmd ::= create_table create_table_args. 186 create_table ::= createkw temp(T) TABLE ifnotexists(E) nm(Y) dbnm(Z). { 187 sqlite3StartTable(pParse,&Y,&Z,T,0,0,E); 188 } 189 createkw(A) ::= CREATE(A). {disableLookaside(pParse);} 190 191 %type ifnotexists {int} 192 ifnotexists(A) ::= . {A = 0;} 193 ifnotexists(A) ::= IF NOT EXISTS. {A = 1;} 194 %type temp {int} 195 %ifndef SQLITE_OMIT_TEMPDB 196 temp(A) ::= TEMP. {A = pParse->db->init.busy==0;} 197 %endif SQLITE_OMIT_TEMPDB 198 temp(A) ::= . {A = 0;} 199 create_table_args ::= LP columnlist conslist_opt(X) RP(E) table_options(F). { 200 sqlite3EndTable(pParse,&X,&E,F,0); 201 } 202 create_table_args ::= AS select(S). { 203 sqlite3EndTable(pParse,0,0,0,S); 204 sqlite3SelectDelete(pParse->db, S); 205 } 206 %type table_options {int} 207 table_options(A) ::= . {A = 0;} 208 table_options(A) ::= WITHOUT nm(X). { 209 if( X.n==5 && sqlite3_strnicmp(X.z,"rowid",5)==0 ){ 210 A = TF_WithoutRowid | TF_NoVisibleRowid; 211 }else{ 212 A = 0; 213 sqlite3ErrorMsg(pParse, "unknown table option: %.*s", X.n, X.z); 214 } 215 } 216 columnlist ::= columnlist COMMA columnname carglist. 217 columnlist ::= columnname carglist. 218 columnname(A) ::= nm(A) typetoken(Y). {sqlite3AddColumn(pParse,&A,&Y);} 219 220 // Declare some tokens early in order to influence their values, to 221 // improve performance and reduce the executable size. The goal here is 222 // to get the "jump" operations in ISNULL through ESCAPE to have numeric 223 // values that are early enough so that all jump operations are clustered 224 // at the beginning. 225 // 226 %token ABORT ACTION AFTER ANALYZE ASC ATTACH BEFORE BEGIN BY CASCADE CAST. 227 %token CONFLICT DATABASE DEFERRED DESC DETACH EACH END EXCLUSIVE EXPLAIN FAIL. 228 %token OR AND NOT IS MATCH LIKE_KW BETWEEN IN ISNULL NOTNULL NE EQ. 229 %token GT LE LT GE ESCAPE. 230 231 // The following directive causes tokens ABORT, AFTER, ASC, etc. to 232 // fallback to ID if they will not parse as their original value. 233 // This obviates the need for the "id" nonterminal. 234 // 235 %fallback ID 236 ABORT ACTION AFTER ANALYZE ASC ATTACH BEFORE BEGIN BY CASCADE CAST COLUMNKW 237 CONFLICT DATABASE DEFERRED DESC DETACH DO 238 EACH END EXCLUSIVE EXPLAIN FAIL FOR 239 IGNORE IMMEDIATE INITIALLY INSTEAD LIKE_KW MATCH NO PLAN 240 QUERY KEY OF OFFSET PRAGMA RAISE RECURSIVE RELEASE REPLACE RESTRICT ROW ROWS 241 ROLLBACK SAVEPOINT TEMP TRIGGER VACUUM VIEW VIRTUAL WITH WITHOUT 242 NULLS FIRST LAST 243 %ifdef SQLITE_OMIT_COMPOUND_SELECT 244 EXCEPT INTERSECT UNION 245 %endif SQLITE_OMIT_COMPOUND_SELECT 246 %ifndef SQLITE_OMIT_WINDOWFUNC 247 CURRENT FOLLOWING PARTITION PRECEDING RANGE UNBOUNDED 248 EXCLUDE GROUPS OTHERS TIES 249 %endif SQLITE_OMIT_WINDOWFUNC 250 %ifndef SQLITE_OMIT_GENERATED_COLUMNS 251 GENERATED ALWAYS 252 %endif 253 MATERIALIZED 254 REINDEX RENAME CTIME_KW IF 255 . 256 %wildcard ANY. 257 258 // Define operator precedence early so that this is the first occurrence 259 // of the operator tokens in the grammer. Keeping the operators together 260 // causes them to be assigned integer values that are close together, 261 // which keeps parser tables smaller. 262 // 263 // The token values assigned to these symbols is determined by the order 264 // in which lemon first sees them. It must be the case that ISNULL/NOTNULL, 265 // NE/EQ, GT/LE, and GE/LT are separated by only a single value. See 266 // the sqlite3ExprIfFalse() routine for additional information on this 267 // constraint. 268 // 269 %left OR. 270 %left AND. 271 %right NOT. 272 %left IS MATCH LIKE_KW BETWEEN IN ISNULL NOTNULL NE EQ. 273 %left GT LE LT GE. 274 %right ESCAPE. 275 %left BITAND BITOR LSHIFT RSHIFT. 276 %left PLUS MINUS. 277 %left STAR SLASH REM. 278 %left CONCAT. 279 %left COLLATE. 280 %right BITNOT. 281 %nonassoc ON. 282 283 // An IDENTIFIER can be a generic identifier, or one of several 284 // keywords. Any non-standard keyword can also be an identifier. 285 // 286 %token_class id ID|INDEXED. 287 288 289 // And "ids" is an identifer-or-string. 290 // 291 %token_class ids ID|STRING. 292 293 // The name of a column or table can be any of the following: 294 // 295 %type nm {Token} 296 nm(A) ::= id(A). 297 nm(A) ::= STRING(A). 298 nm(A) ::= JOIN_KW(A). 299 300 // A typetoken is really zero or more tokens that form a type name such 301 // as can be found after the column name in a CREATE TABLE statement. 302 // Multiple tokens are concatenated to form the value of the typetoken. 303 // 304 %type typetoken {Token} 305 typetoken(A) ::= . {A.n = 0; A.z = 0;} 306 typetoken(A) ::= typename(A). 307 typetoken(A) ::= typename(A) LP signed RP(Y). { 308 A.n = (int)(&Y.z[Y.n] - A.z); 309 } 310 typetoken(A) ::= typename(A) LP signed COMMA signed RP(Y). { 311 A.n = (int)(&Y.z[Y.n] - A.z); 312 } 313 %type typename {Token} 314 typename(A) ::= ids(A). 315 typename(A) ::= typename(A) ids(Y). {A.n=Y.n+(int)(Y.z-A.z);} 316 signed ::= plus_num. 317 signed ::= minus_num. 318 319 // The scanpt non-terminal takes a value which is a pointer to the 320 // input text just past the last token that has been shifted into 321 // the parser. By surrounding some phrase in the grammar with two 322 // scanpt non-terminals, we can capture the input text for that phrase. 323 // For example: 324 // 325 // something ::= .... scanpt(A) phrase scanpt(Z). 326 // 327 // The text that is parsed as "phrase" is a string starting at A 328 // and containing (int)(Z-A) characters. There might be some extra 329 // whitespace on either end of the text, but that can be removed in 330 // post-processing, if needed. 331 // 332 %type scanpt {const char*} 333 scanpt(A) ::= . { 334 assert( yyLookahead!=YYNOCODE ); 335 A = yyLookaheadToken.z; 336 } 337 scantok(A) ::= . { 338 assert( yyLookahead!=YYNOCODE ); 339 A = yyLookaheadToken; 340 } 341 342 // "carglist" is a list of additional constraints that come after the 343 // column name and column type in a CREATE TABLE statement. 344 // 345 carglist ::= carglist ccons. 346 carglist ::= . 347 ccons ::= CONSTRAINT nm(X). {pParse->constraintName = X;} 348 ccons ::= DEFAULT scantok(A) term(X). 349 {sqlite3AddDefaultValue(pParse,X,A.z,&A.z[A.n]);} 350 ccons ::= DEFAULT LP(A) expr(X) RP(Z). 351 {sqlite3AddDefaultValue(pParse,X,A.z+1,Z.z);} 352 ccons ::= DEFAULT PLUS(A) scantok(Z) term(X). 353 {sqlite3AddDefaultValue(pParse,X,A.z,&Z.z[Z.n]);} 354 ccons ::= DEFAULT MINUS(A) scantok(Z) term(X). { 355 Expr *p = sqlite3PExpr(pParse, TK_UMINUS, X, 0); 356 sqlite3AddDefaultValue(pParse,p,A.z,&Z.z[Z.n]); 357 } 358 ccons ::= DEFAULT scantok id(X). { 359 Expr *p = tokenExpr(pParse, TK_STRING, X); 360 if( p ){ 361 sqlite3ExprIdToTrueFalse(p); 362 testcase( p->op==TK_TRUEFALSE && sqlite3ExprTruthValue(p) ); 363 } 364 sqlite3AddDefaultValue(pParse,p,X.z,X.z+X.n); 365 } 366 367 // In addition to the type name, we also care about the primary key and 368 // UNIQUE constraints. 369 // 370 ccons ::= NULL onconf. 371 ccons ::= NOT NULL onconf(R). {sqlite3AddNotNull(pParse, R);} 372 ccons ::= PRIMARY KEY sortorder(Z) onconf(R) autoinc(I). 373 {sqlite3AddPrimaryKey(pParse,0,R,I,Z);} 374 ccons ::= UNIQUE onconf(R). {sqlite3CreateIndex(pParse,0,0,0,0,R,0,0,0,0, 375 SQLITE_IDXTYPE_UNIQUE);} 376 ccons ::= CHECK LP(A) expr(X) RP(B). {sqlite3AddCheckConstraint(pParse,X,A.z,B.z);} 377 ccons ::= REFERENCES nm(T) eidlist_opt(TA) refargs(R). 378 {sqlite3CreateForeignKey(pParse,0,&T,TA,R);} 379 ccons ::= defer_subclause(D). {sqlite3DeferForeignKey(pParse,D);} 380 ccons ::= COLLATE ids(C). {sqlite3AddCollateType(pParse, &C);} 381 ccons ::= GENERATED ALWAYS AS generated. 382 ccons ::= AS generated. 383 generated ::= LP expr(E) RP. {sqlite3AddGenerated(pParse,E,0);} 384 generated ::= LP expr(E) RP ID(TYPE). {sqlite3AddGenerated(pParse,E,&TYPE);} 385 386 // The optional AUTOINCREMENT keyword 387 %type autoinc {int} 388 autoinc(X) ::= . {X = 0;} 389 autoinc(X) ::= AUTOINCR. {X = 1;} 390 391 // The next group of rules parses the arguments to a REFERENCES clause 392 // that determine if the referential integrity checking is deferred or 393 // or immediate and which determine what action to take if a ref-integ 394 // check fails. 395 // 396 %type refargs {int} 397 refargs(A) ::= . { A = OE_None*0x0101; /* EV: R-19803-45884 */} 398 refargs(A) ::= refargs(A) refarg(Y). { A = (A & ~Y.mask) | Y.value; } 399 %type refarg {struct {int value; int mask;}} 400 refarg(A) ::= MATCH nm. { A.value = 0; A.mask = 0x000000; } 401 refarg(A) ::= ON INSERT refact. { A.value = 0; A.mask = 0x000000; } 402 refarg(A) ::= ON DELETE refact(X). { A.value = X; A.mask = 0x0000ff; } 403 refarg(A) ::= ON UPDATE refact(X). { A.value = X<<8; A.mask = 0x00ff00; } 404 %type refact {int} 405 refact(A) ::= SET NULL. { A = OE_SetNull; /* EV: R-33326-45252 */} 406 refact(A) ::= SET DEFAULT. { A = OE_SetDflt; /* EV: R-33326-45252 */} 407 refact(A) ::= CASCADE. { A = OE_Cascade; /* EV: R-33326-45252 */} 408 refact(A) ::= RESTRICT. { A = OE_Restrict; /* EV: R-33326-45252 */} 409 refact(A) ::= NO ACTION. { A = OE_None; /* EV: R-33326-45252 */} 410 %type defer_subclause {int} 411 defer_subclause(A) ::= NOT DEFERRABLE init_deferred_pred_opt. {A = 0;} 412 defer_subclause(A) ::= DEFERRABLE init_deferred_pred_opt(X). {A = X;} 413 %type init_deferred_pred_opt {int} 414 init_deferred_pred_opt(A) ::= . {A = 0;} 415 init_deferred_pred_opt(A) ::= INITIALLY DEFERRED. {A = 1;} 416 init_deferred_pred_opt(A) ::= INITIALLY IMMEDIATE. {A = 0;} 417 418 conslist_opt(A) ::= . {A.n = 0; A.z = 0;} 419 conslist_opt(A) ::= COMMA(A) conslist. 420 conslist ::= conslist tconscomma tcons. 421 conslist ::= tcons. 422 tconscomma ::= COMMA. {pParse->constraintName.n = 0;} 423 tconscomma ::= . 424 tcons ::= CONSTRAINT nm(X). {pParse->constraintName = X;} 425 tcons ::= PRIMARY KEY LP sortlist(X) autoinc(I) RP onconf(R). 426 {sqlite3AddPrimaryKey(pParse,X,R,I,0);} 427 tcons ::= UNIQUE LP sortlist(X) RP onconf(R). 428 {sqlite3CreateIndex(pParse,0,0,0,X,R,0,0,0,0, 429 SQLITE_IDXTYPE_UNIQUE);} 430 tcons ::= CHECK LP(A) expr(E) RP(B) onconf. 431 {sqlite3AddCheckConstraint(pParse,E,A.z,B.z);} 432 tcons ::= FOREIGN KEY LP eidlist(FA) RP 433 REFERENCES nm(T) eidlist_opt(TA) refargs(R) defer_subclause_opt(D). { 434 sqlite3CreateForeignKey(pParse, FA, &T, TA, R); 435 sqlite3DeferForeignKey(pParse, D); 436 } 437 %type defer_subclause_opt {int} 438 defer_subclause_opt(A) ::= . {A = 0;} 439 defer_subclause_opt(A) ::= defer_subclause(A). 440 441 // The following is a non-standard extension that allows us to declare the 442 // default behavior when there is a constraint conflict. 443 // 444 %type onconf {int} 445 %type orconf {int} 446 %type resolvetype {int} 447 onconf(A) ::= . {A = OE_Default;} 448 onconf(A) ::= ON CONFLICT resolvetype(X). {A = X;} 449 orconf(A) ::= . {A = OE_Default;} 450 orconf(A) ::= OR resolvetype(X). {A = X;} 451 resolvetype(A) ::= raisetype(A). 452 resolvetype(A) ::= IGNORE. {A = OE_Ignore;} 453 resolvetype(A) ::= REPLACE. {A = OE_Replace;} 454 455 ////////////////////////// The DROP TABLE ///////////////////////////////////// 456 // 457 cmd ::= DROP TABLE ifexists(E) fullname(X). { 458 sqlite3DropTable(pParse, X, 0, E); 459 } 460 %type ifexists {int} 461 ifexists(A) ::= IF EXISTS. {A = 1;} 462 ifexists(A) ::= . {A = 0;} 463 464 ///////////////////// The CREATE VIEW statement ///////////////////////////// 465 // 466 %ifndef SQLITE_OMIT_VIEW 467 cmd ::= createkw(X) temp(T) VIEW ifnotexists(E) nm(Y) dbnm(Z) eidlist_opt(C) 468 AS select(S). { 469 sqlite3CreateView(pParse, &X, &Y, &Z, C, S, T, E); 470 } 471 cmd ::= DROP VIEW ifexists(E) fullname(X). { 472 sqlite3DropTable(pParse, X, 1, E); 473 } 474 %endif SQLITE_OMIT_VIEW 475 476 //////////////////////// The SELECT statement ///////////////////////////////// 477 // 478 cmd ::= select(X). { 479 SelectDest dest = {SRT_Output, 0, 0, 0, 0, 0, 0}; 480 sqlite3Select(pParse, X, &dest); 481 sqlite3SelectDelete(pParse->db, X); 482 } 483 484 %type select {Select*} 485 %destructor select {sqlite3SelectDelete(pParse->db, $$);} 486 %type selectnowith {Select*} 487 %destructor selectnowith {sqlite3SelectDelete(pParse->db, $$);} 488 %type oneselect {Select*} 489 %destructor oneselect {sqlite3SelectDelete(pParse->db, $$);} 490 491 %include { 492 /* 493 ** For a compound SELECT statement, make sure p->pPrior->pNext==p for 494 ** all elements in the list. And make sure list length does not exceed 495 ** SQLITE_LIMIT_COMPOUND_SELECT. 496 */ 497 static void parserDoubleLinkSelect(Parse *pParse, Select *p){ 498 assert( p!=0 ); 499 if( p->pPrior ){ 500 Select *pNext = 0, *pLoop = p; 501 int mxSelect, cnt = 1; 502 while(1){ 503 pLoop->pNext = pNext; 504 pLoop->selFlags |= SF_Compound; 505 pNext = pLoop; 506 pLoop = pLoop->pPrior; 507 if( pLoop==0 ) break; 508 cnt++; 509 if( pLoop->pOrderBy || pLoop->pLimit ){ 510 sqlite3ErrorMsg(pParse,"%s clause should come after %s not before", 511 pLoop->pOrderBy!=0 ? "ORDER BY" : "LIMIT", 512 sqlite3SelectOpName(pNext->op)); 513 break; 514 } 515 } 516 if( (p->selFlags & SF_MultiValue)==0 && 517 (mxSelect = pParse->db->aLimit[SQLITE_LIMIT_COMPOUND_SELECT])>0 && 518 cnt>mxSelect 519 ){ 520 sqlite3ErrorMsg(pParse, "too many terms in compound SELECT"); 521 } 522 } 523 } 524 525 /* Attach a With object describing the WITH clause to a Select 526 ** object describing the query for which the WITH clause is a prefix. 527 */ 528 static Select *attachWithToSelect(Parse *pParse, Select *pSelect, With *pWith){ 529 if( pSelect ){ 530 pSelect->pWith = pWith; 531 parserDoubleLinkSelect(pParse, pSelect); 532 }else{ 533 sqlite3WithDelete(pParse->db, pWith); 534 } 535 return pSelect; 536 } 537 } 538 539 %ifndef SQLITE_OMIT_CTE 540 select(A) ::= WITH wqlist(W) selectnowith(X). {A = attachWithToSelect(pParse,X,W);} 541 select(A) ::= WITH RECURSIVE wqlist(W) selectnowith(X). 542 {A = attachWithToSelect(pParse,X,W);} 543 %endif /* SQLITE_OMIT_CTE */ 544 select(A) ::= selectnowith(X). { 545 Select *p = X; 546 if( p ){ 547 parserDoubleLinkSelect(pParse, p); 548 } 549 A = p; /*A-overwrites-X*/ 550 } 551 552 selectnowith(A) ::= oneselect(A). 553 %ifndef SQLITE_OMIT_COMPOUND_SELECT 554 selectnowith(A) ::= selectnowith(A) multiselect_op(Y) oneselect(Z). { 555 Select *pRhs = Z; 556 Select *pLhs = A; 557 if( pRhs && pRhs->pPrior ){ 558 SrcList *pFrom; 559 Token x; 560 x.n = 0; 561 parserDoubleLinkSelect(pParse, pRhs); 562 pFrom = sqlite3SrcListAppendFromTerm(pParse,0,0,0,&x,pRhs,0,0); 563 pRhs = sqlite3SelectNew(pParse,0,pFrom,0,0,0,0,0,0); 564 } 565 if( pRhs ){ 566 pRhs->op = (u8)Y; 567 pRhs->pPrior = pLhs; 568 if( ALWAYS(pLhs) ) pLhs->selFlags &= ~SF_MultiValue; 569 pRhs->selFlags &= ~SF_MultiValue; 570 if( Y!=TK_ALL ) pParse->hasCompound = 1; 571 }else{ 572 sqlite3SelectDelete(pParse->db, pLhs); 573 } 574 A = pRhs; 575 } 576 %type multiselect_op {int} 577 multiselect_op(A) ::= UNION(OP). {A = @OP; /*A-overwrites-OP*/} 578 multiselect_op(A) ::= UNION ALL. {A = TK_ALL;} 579 multiselect_op(A) ::= EXCEPT|INTERSECT(OP). {A = @OP; /*A-overwrites-OP*/} 580 %endif SQLITE_OMIT_COMPOUND_SELECT 581 582 oneselect(A) ::= SELECT distinct(D) selcollist(W) from(X) where_opt(Y) 583 groupby_opt(P) having_opt(Q) 584 orderby_opt(Z) limit_opt(L). { 585 A = sqlite3SelectNew(pParse,W,X,Y,P,Q,Z,D,L); 586 } 587 %ifndef SQLITE_OMIT_WINDOWFUNC 588 oneselect(A) ::= SELECT distinct(D) selcollist(W) from(X) where_opt(Y) 589 groupby_opt(P) having_opt(Q) window_clause(R) 590 orderby_opt(Z) limit_opt(L). { 591 A = sqlite3SelectNew(pParse,W,X,Y,P,Q,Z,D,L); 592 if( A ){ 593 A->pWinDefn = R; 594 }else{ 595 sqlite3WindowListDelete(pParse->db, R); 596 } 597 } 598 %endif 599 600 601 oneselect(A) ::= values(A). 602 603 %type values {Select*} 604 %destructor values {sqlite3SelectDelete(pParse->db, $$);} 605 values(A) ::= VALUES LP nexprlist(X) RP. { 606 A = sqlite3SelectNew(pParse,X,0,0,0,0,0,SF_Values,0); 607 } 608 values(A) ::= values(A) COMMA LP nexprlist(Y) RP. { 609 Select *pRight, *pLeft = A; 610 pRight = sqlite3SelectNew(pParse,Y,0,0,0,0,0,SF_Values|SF_MultiValue,0); 611 if( ALWAYS(pLeft) ) pLeft->selFlags &= ~SF_MultiValue; 612 if( pRight ){ 613 pRight->op = TK_ALL; 614 pRight->pPrior = pLeft; 615 A = pRight; 616 }else{ 617 A = pLeft; 618 } 619 } 620 621 // The "distinct" nonterminal is true (1) if the DISTINCT keyword is 622 // present and false (0) if it is not. 623 // 624 %type distinct {int} 625 distinct(A) ::= DISTINCT. {A = SF_Distinct;} 626 distinct(A) ::= ALL. {A = SF_All;} 627 distinct(A) ::= . {A = 0;} 628 629 // selcollist is a list of expressions that are to become the return 630 // values of the SELECT statement. The "*" in statements like 631 // "SELECT * FROM ..." is encoded as a special expression with an 632 // opcode of TK_ASTERISK. 633 // 634 %type selcollist {ExprList*} 635 %destructor selcollist {sqlite3ExprListDelete(pParse->db, $$);} 636 %type sclp {ExprList*} 637 %destructor sclp {sqlite3ExprListDelete(pParse->db, $$);} 638 sclp(A) ::= selcollist(A) COMMA. 639 sclp(A) ::= . {A = 0;} 640 selcollist(A) ::= sclp(A) scanpt(B) expr(X) scanpt(Z) as(Y). { 641 A = sqlite3ExprListAppend(pParse, A, X); 642 if( Y.n>0 ) sqlite3ExprListSetName(pParse, A, &Y, 1); 643 sqlite3ExprListSetSpan(pParse,A,B,Z); 644 } 645 selcollist(A) ::= sclp(A) scanpt STAR. { 646 Expr *p = sqlite3Expr(pParse->db, TK_ASTERISK, 0); 647 A = sqlite3ExprListAppend(pParse, A, p); 648 } 649 selcollist(A) ::= sclp(A) scanpt nm(X) DOT STAR. { 650 Expr *pRight = sqlite3PExpr(pParse, TK_ASTERISK, 0, 0); 651 Expr *pLeft = sqlite3ExprAlloc(pParse->db, TK_ID, &X, 1); 652 Expr *pDot = sqlite3PExpr(pParse, TK_DOT, pLeft, pRight); 653 A = sqlite3ExprListAppend(pParse,A, pDot); 654 } 655 656 // An option "AS <id>" phrase that can follow one of the expressions that 657 // define the result set, or one of the tables in the FROM clause. 658 // 659 %type as {Token} 660 as(X) ::= AS nm(Y). {X = Y;} 661 as(X) ::= ids(X). 662 as(X) ::= . {X.n = 0; X.z = 0;} 663 664 665 %type seltablist {SrcList*} 666 %destructor seltablist {sqlite3SrcListDelete(pParse->db, $$);} 667 %type stl_prefix {SrcList*} 668 %destructor stl_prefix {sqlite3SrcListDelete(pParse->db, $$);} 669 %type from {SrcList*} 670 %destructor from {sqlite3SrcListDelete(pParse->db, $$);} 671 672 // A complete FROM clause. 673 // 674 from(A) ::= . {A = 0;} 675 from(A) ::= FROM seltablist(X). { 676 A = X; 677 sqlite3SrcListShiftJoinType(A); 678 } 679 680 // "seltablist" is a "Select Table List" - the content of the FROM clause 681 // in a SELECT statement. "stl_prefix" is a prefix of this list. 682 // 683 stl_prefix(A) ::= seltablist(A) joinop(Y). { 684 if( ALWAYS(A && A->nSrc>0) ) A->a[A->nSrc-1].fg.jointype = (u8)Y; 685 } 686 stl_prefix(A) ::= . {A = 0;} 687 seltablist(A) ::= stl_prefix(A) nm(Y) dbnm(D) as(Z) indexed_opt(I) 688 on_opt(N) using_opt(U). { 689 A = sqlite3SrcListAppendFromTerm(pParse,A,&Y,&D,&Z,0,N,U); 690 sqlite3SrcListIndexedBy(pParse, A, &I); 691 } 692 seltablist(A) ::= stl_prefix(A) nm(Y) dbnm(D) LP exprlist(E) RP as(Z) 693 on_opt(N) using_opt(U). { 694 A = sqlite3SrcListAppendFromTerm(pParse,A,&Y,&D,&Z,0,N,U); 695 sqlite3SrcListFuncArgs(pParse, A, E); 696 } 697 %ifndef SQLITE_OMIT_SUBQUERY 698 seltablist(A) ::= stl_prefix(A) LP select(S) RP 699 as(Z) on_opt(N) using_opt(U). { 700 A = sqlite3SrcListAppendFromTerm(pParse,A,0,0,&Z,S,N,U); 701 } 702 seltablist(A) ::= stl_prefix(A) LP seltablist(F) RP 703 as(Z) on_opt(N) using_opt(U). { 704 if( A==0 && Z.n==0 && N==0 && U==0 ){ 705 A = F; 706 }else if( F->nSrc==1 ){ 707 A = sqlite3SrcListAppendFromTerm(pParse,A,0,0,&Z,0,N,U); 708 if( A ){ 709 SrcItem *pNew = &A->a[A->nSrc-1]; 710 SrcItem *pOld = F->a; 711 pNew->zName = pOld->zName; 712 pNew->zDatabase = pOld->zDatabase; 713 pNew->pSelect = pOld->pSelect; 714 if( pOld->fg.isTabFunc ){ 715 pNew->u1.pFuncArg = pOld->u1.pFuncArg; 716 pOld->u1.pFuncArg = 0; 717 pOld->fg.isTabFunc = 0; 718 pNew->fg.isTabFunc = 1; 719 } 720 pOld->zName = pOld->zDatabase = 0; 721 pOld->pSelect = 0; 722 } 723 sqlite3SrcListDelete(pParse->db, F); 724 }else{ 725 Select *pSubquery; 726 sqlite3SrcListShiftJoinType(F); 727 pSubquery = sqlite3SelectNew(pParse,0,F,0,0,0,0,SF_NestedFrom,0); 728 A = sqlite3SrcListAppendFromTerm(pParse,A,0,0,&Z,pSubquery,N,U); 729 } 730 } 731 %endif SQLITE_OMIT_SUBQUERY 732 733 %type dbnm {Token} 734 dbnm(A) ::= . {A.z=0; A.n=0;} 735 dbnm(A) ::= DOT nm(X). {A = X;} 736 737 %type fullname {SrcList*} 738 %destructor fullname {sqlite3SrcListDelete(pParse->db, $$);} 739 fullname(A) ::= nm(X). { 740 A = sqlite3SrcListAppend(pParse,0,&X,0); 741 if( IN_RENAME_OBJECT && A ) sqlite3RenameTokenMap(pParse, A->a[0].zName, &X); 742 } 743 fullname(A) ::= nm(X) DOT nm(Y). { 744 A = sqlite3SrcListAppend(pParse,0,&X,&Y); 745 if( IN_RENAME_OBJECT && A ) sqlite3RenameTokenMap(pParse, A->a[0].zName, &Y); 746 } 747 748 %type xfullname {SrcList*} 749 %destructor xfullname {sqlite3SrcListDelete(pParse->db, $$);} 750 xfullname(A) ::= nm(X). 751 {A = sqlite3SrcListAppend(pParse,0,&X,0); /*A-overwrites-X*/} 752 xfullname(A) ::= nm(X) DOT nm(Y). 753 {A = sqlite3SrcListAppend(pParse,0,&X,&Y); /*A-overwrites-X*/} 754 xfullname(A) ::= nm(X) DOT nm(Y) AS nm(Z). { 755 A = sqlite3SrcListAppend(pParse,0,&X,&Y); /*A-overwrites-X*/ 756 if( A ) A->a[0].zAlias = sqlite3NameFromToken(pParse->db, &Z); 757 } 758 xfullname(A) ::= nm(X) AS nm(Z). { 759 A = sqlite3SrcListAppend(pParse,0,&X,0); /*A-overwrites-X*/ 760 if( A ) A->a[0].zAlias = sqlite3NameFromToken(pParse->db, &Z); 761 } 762 763 %type joinop {int} 764 joinop(X) ::= COMMA|JOIN. { X = JT_INNER; } 765 joinop(X) ::= JOIN_KW(A) JOIN. 766 {X = sqlite3JoinType(pParse,&A,0,0); /*X-overwrites-A*/} 767 joinop(X) ::= JOIN_KW(A) nm(B) JOIN. 768 {X = sqlite3JoinType(pParse,&A,&B,0); /*X-overwrites-A*/} 769 joinop(X) ::= JOIN_KW(A) nm(B) nm(C) JOIN. 770 {X = sqlite3JoinType(pParse,&A,&B,&C);/*X-overwrites-A*/} 771 772 // There is a parsing abiguity in an upsert statement that uses a 773 // SELECT on the RHS of a the INSERT: 774 // 775 // INSERT INTO tab SELECT * FROM aaa JOIN bbb ON CONFLICT ... 776 // here ----^^ 777 // 778 // When the ON token is encountered, the parser does not know if it is 779 // the beginning of an ON CONFLICT clause, or the beginning of an ON 780 // clause associated with the JOIN. The conflict is resolved in favor 781 // of the JOIN. If an ON CONFLICT clause is intended, insert a dummy 782 // WHERE clause in between, like this: 783 // 784 // INSERT INTO tab SELECT * FROM aaa JOIN bbb WHERE true ON CONFLICT ... 785 // 786 // The [AND] and [OR] precedence marks in the rules for on_opt cause the 787 // ON in this context to always be interpreted as belonging to the JOIN. 788 // 789 %type on_opt {Expr*} 790 %destructor on_opt {sqlite3ExprDelete(pParse->db, $$);} 791 on_opt(N) ::= ON expr(E). {N = E;} 792 on_opt(N) ::= . [OR] {N = 0;} 793 794 // Note that this block abuses the Token type just a little. If there is 795 // no "INDEXED BY" clause, the returned token is empty (z==0 && n==0). If 796 // there is an INDEXED BY clause, then the token is populated as per normal, 797 // with z pointing to the token data and n containing the number of bytes 798 // in the token. 799 // 800 // If there is a "NOT INDEXED" clause, then (z==0 && n==1), which is 801 // normally illegal. The sqlite3SrcListIndexedBy() function 802 // recognizes and interprets this as a special case. 803 // 804 %type indexed_opt {Token} 805 indexed_opt(A) ::= . {A.z=0; A.n=0;} 806 indexed_opt(A) ::= INDEXED BY nm(X). {A = X;} 807 indexed_opt(A) ::= NOT INDEXED. {A.z=0; A.n=1;} 808 809 %type using_opt {IdList*} 810 %destructor using_opt {sqlite3IdListDelete(pParse->db, $$);} 811 using_opt(U) ::= USING LP idlist(L) RP. {U = L;} 812 using_opt(U) ::= . {U = 0;} 813 814 815 %type orderby_opt {ExprList*} 816 %destructor orderby_opt {sqlite3ExprListDelete(pParse->db, $$);} 817 818 // the sortlist non-terminal stores a list of expression where each 819 // expression is optionally followed by ASC or DESC to indicate the 820 // sort order. 821 // 822 %type sortlist {ExprList*} 823 %destructor sortlist {sqlite3ExprListDelete(pParse->db, $$);} 824 825 orderby_opt(A) ::= . {A = 0;} 826 orderby_opt(A) ::= ORDER BY sortlist(X). {A = X;} 827 sortlist(A) ::= sortlist(A) COMMA expr(Y) sortorder(Z) nulls(X). { 828 A = sqlite3ExprListAppend(pParse,A,Y); 829 sqlite3ExprListSetSortOrder(A,Z,X); 830 } 831 sortlist(A) ::= expr(Y) sortorder(Z) nulls(X). { 832 A = sqlite3ExprListAppend(pParse,0,Y); /*A-overwrites-Y*/ 833 sqlite3ExprListSetSortOrder(A,Z,X); 834 } 835 836 %type sortorder {int} 837 838 sortorder(A) ::= ASC. {A = SQLITE_SO_ASC;} 839 sortorder(A) ::= DESC. {A = SQLITE_SO_DESC;} 840 sortorder(A) ::= . {A = SQLITE_SO_UNDEFINED;} 841 842 %type nulls {int} 843 nulls(A) ::= NULLS FIRST. {A = SQLITE_SO_ASC;} 844 nulls(A) ::= NULLS LAST. {A = SQLITE_SO_DESC;} 845 nulls(A) ::= . {A = SQLITE_SO_UNDEFINED;} 846 847 %type groupby_opt {ExprList*} 848 %destructor groupby_opt {sqlite3ExprListDelete(pParse->db, $$);} 849 groupby_opt(A) ::= . {A = 0;} 850 groupby_opt(A) ::= GROUP BY nexprlist(X). {A = X;} 851 852 %type having_opt {Expr*} 853 %destructor having_opt {sqlite3ExprDelete(pParse->db, $$);} 854 having_opt(A) ::= . {A = 0;} 855 having_opt(A) ::= HAVING expr(X). {A = X;} 856 857 %type limit_opt {Expr*} 858 859 // The destructor for limit_opt will never fire in the current grammar. 860 // The limit_opt non-terminal only occurs at the end of a single production 861 // rule for SELECT statements. As soon as the rule that create the 862 // limit_opt non-terminal reduces, the SELECT statement rule will also 863 // reduce. So there is never a limit_opt non-terminal on the stack 864 // except as a transient. So there is never anything to destroy. 865 // 866 //%destructor limit_opt {sqlite3ExprDelete(pParse->db, $$);} 867 limit_opt(A) ::= . {A = 0;} 868 limit_opt(A) ::= LIMIT expr(X). 869 {A = sqlite3PExpr(pParse,TK_LIMIT,X,0);} 870 limit_opt(A) ::= LIMIT expr(X) OFFSET expr(Y). 871 {A = sqlite3PExpr(pParse,TK_LIMIT,X,Y);} 872 limit_opt(A) ::= LIMIT expr(X) COMMA expr(Y). 873 {A = sqlite3PExpr(pParse,TK_LIMIT,Y,X);} 874 875 /////////////////////////// The DELETE statement ///////////////////////////// 876 // 877 %if SQLITE_ENABLE_UPDATE_DELETE_LIMIT || SQLITE_UDL_CAPABLE_PARSER 878 cmd ::= with DELETE FROM xfullname(X) indexed_opt(I) where_opt_ret(W) 879 orderby_opt(O) limit_opt(L). { 880 sqlite3SrcListIndexedBy(pParse, X, &I); 881 #ifndef SQLITE_ENABLE_UPDATE_DELETE_LIMIT 882 if( O || L ){ 883 updateDeleteLimitError(pParse,O,L); 884 O = 0; 885 L = 0; 886 } 887 #endif 888 sqlite3DeleteFrom(pParse,X,W,O,L); 889 } 890 %else 891 cmd ::= with DELETE FROM xfullname(X) indexed_opt(I) where_opt_ret(W). { 892 sqlite3SrcListIndexedBy(pParse, X, &I); 893 sqlite3DeleteFrom(pParse,X,W,0,0); 894 } 895 %endif 896 897 %type where_opt {Expr*} 898 %destructor where_opt {sqlite3ExprDelete(pParse->db, $$);} 899 %type where_opt_ret {Expr*} 900 %destructor where_opt_ret {sqlite3ExprDelete(pParse->db, $$);} 901 902 where_opt(A) ::= . {A = 0;} 903 where_opt(A) ::= WHERE expr(X). {A = X;} 904 where_opt_ret(A) ::= . {A = 0;} 905 where_opt_ret(A) ::= WHERE expr(X). {A = X;} 906 where_opt_ret(A) ::= RETURNING selcollist(X). 907 {sqlite3AddReturning(pParse,X); A = 0;} 908 where_opt_ret(A) ::= WHERE expr(X) RETURNING selcollist(Y). 909 {sqlite3AddReturning(pParse,Y); A = X;} 910 911 ////////////////////////// The UPDATE command //////////////////////////////// 912 // 913 %if SQLITE_ENABLE_UPDATE_DELETE_LIMIT || SQLITE_UDL_CAPABLE_PARSER 914 cmd ::= with UPDATE orconf(R) xfullname(X) indexed_opt(I) SET setlist(Y) from(F) 915 where_opt_ret(W) orderby_opt(O) limit_opt(L). { 916 sqlite3SrcListIndexedBy(pParse, X, &I); 917 X = sqlite3SrcListAppendList(pParse, X, F); 918 sqlite3ExprListCheckLength(pParse,Y,"set list"); 919 #ifndef SQLITE_ENABLE_UPDATE_DELETE_LIMIT 920 if( O || L ){ 921 updateDeleteLimitError(pParse,O,L); 922 O = 0; 923 L = 0; 924 } 925 #endif 926 sqlite3Update(pParse,X,Y,W,R,O,L,0); 927 } 928 %else 929 cmd ::= with UPDATE orconf(R) xfullname(X) indexed_opt(I) SET setlist(Y) from(F) 930 where_opt_ret(W). { 931 sqlite3SrcListIndexedBy(pParse, X, &I); 932 sqlite3ExprListCheckLength(pParse,Y,"set list"); 933 X = sqlite3SrcListAppendList(pParse, X, F); 934 sqlite3Update(pParse,X,Y,W,R,0,0,0); 935 } 936 %endif 937 938 939 940 %type setlist {ExprList*} 941 %destructor setlist {sqlite3ExprListDelete(pParse->db, $$);} 942 943 setlist(A) ::= setlist(A) COMMA nm(X) EQ expr(Y). { 944 A = sqlite3ExprListAppend(pParse, A, Y); 945 sqlite3ExprListSetName(pParse, A, &X, 1); 946 } 947 setlist(A) ::= setlist(A) COMMA LP idlist(X) RP EQ expr(Y). { 948 A = sqlite3ExprListAppendVector(pParse, A, X, Y); 949 } 950 setlist(A) ::= nm(X) EQ expr(Y). { 951 A = sqlite3ExprListAppend(pParse, 0, Y); 952 sqlite3ExprListSetName(pParse, A, &X, 1); 953 } 954 setlist(A) ::= LP idlist(X) RP EQ expr(Y). { 955 A = sqlite3ExprListAppendVector(pParse, 0, X, Y); 956 } 957 958 ////////////////////////// The INSERT command ///////////////////////////////// 959 // 960 cmd ::= with insert_cmd(R) INTO xfullname(X) idlist_opt(F) select(S) 961 upsert(U). { 962 sqlite3Insert(pParse, X, S, F, R, U); 963 } 964 cmd ::= with insert_cmd(R) INTO xfullname(X) idlist_opt(F) DEFAULT VALUES returning. 965 { 966 sqlite3Insert(pParse, X, 0, F, R, 0); 967 } 968 969 %type upsert {Upsert*} 970 971 // Because upsert only occurs at the tip end of the INSERT rule for cmd, 972 // there is never a case where the value of the upsert pointer will not 973 // be destroyed by the cmd action. So comment-out the destructor to 974 // avoid unreachable code. 975 //%destructor upsert {sqlite3UpsertDelete(pParse->db,$$);} 976 upsert(A) ::= . { A = 0; } 977 upsert(A) ::= RETURNING selcollist(X). { A = 0; sqlite3AddReturning(pParse,X); } 978 upsert(A) ::= ON CONFLICT LP sortlist(T) RP where_opt(TW) 979 DO UPDATE SET setlist(Z) where_opt(W) upsert(N). 980 { A = sqlite3UpsertNew(pParse->db,T,TW,Z,W,N);} 981 upsert(A) ::= ON CONFLICT LP sortlist(T) RP where_opt(TW) DO NOTHING upsert(N). 982 { A = sqlite3UpsertNew(pParse->db,T,TW,0,0,N); } 983 upsert(A) ::= ON CONFLICT DO NOTHING returning. 984 { A = sqlite3UpsertNew(pParse->db,0,0,0,0,0); } 985 upsert(A) ::= ON CONFLICT DO UPDATE SET setlist(Z) where_opt(W) returning. 986 { A = sqlite3UpsertNew(pParse->db,0,0,Z,W,0);} 987 988 returning ::= RETURNING selcollist(X). {sqlite3AddReturning(pParse,X);} 989 returning ::= . 990 991 %type insert_cmd {int} 992 insert_cmd(A) ::= INSERT orconf(R). {A = R;} 993 insert_cmd(A) ::= REPLACE. {A = OE_Replace;} 994 995 %type idlist_opt {IdList*} 996 %destructor idlist_opt {sqlite3IdListDelete(pParse->db, $$);} 997 %type idlist {IdList*} 998 %destructor idlist {sqlite3IdListDelete(pParse->db, $$);} 999 1000 idlist_opt(A) ::= . {A = 0;} 1001 idlist_opt(A) ::= LP idlist(X) RP. {A = X;} 1002 idlist(A) ::= idlist(A) COMMA nm(Y). 1003 {A = sqlite3IdListAppend(pParse,A,&Y);} 1004 idlist(A) ::= nm(Y). 1005 {A = sqlite3IdListAppend(pParse,0,&Y); /*A-overwrites-Y*/} 1006 1007 /////////////////////////// Expression Processing ///////////////////////////// 1008 // 1009 1010 %type expr {Expr*} 1011 %destructor expr {sqlite3ExprDelete(pParse->db, $$);} 1012 %type term {Expr*} 1013 %destructor term {sqlite3ExprDelete(pParse->db, $$);} 1014 1015 %include { 1016 1017 /* Construct a new Expr object from a single identifier. Use the 1018 ** new Expr to populate pOut. Set the span of pOut to be the identifier 1019 ** that created the expression. 1020 */ 1021 static Expr *tokenExpr(Parse *pParse, int op, Token t){ 1022 Expr *p = sqlite3DbMallocRawNN(pParse->db, sizeof(Expr)+t.n+1); 1023 if( p ){ 1024 /* memset(p, 0, sizeof(Expr)); */ 1025 p->op = (u8)op; 1026 p->affExpr = 0; 1027 p->flags = EP_Leaf; 1028 ExprClearVVAProperties(p); 1029 p->iAgg = -1; 1030 p->pLeft = p->pRight = 0; 1031 p->x.pList = 0; 1032 p->pAggInfo = 0; 1033 p->y.pTab = 0; 1034 p->op2 = 0; 1035 p->iTable = 0; 1036 p->iColumn = 0; 1037 p->u.zToken = (char*)&p[1]; 1038 memcpy(p->u.zToken, t.z, t.n); 1039 p->u.zToken[t.n] = 0; 1040 if( sqlite3Isquote(p->u.zToken[0]) ){ 1041 sqlite3DequoteExpr(p); 1042 } 1043 #if SQLITE_MAX_EXPR_DEPTH>0 1044 p->nHeight = 1; 1045 #endif 1046 if( IN_RENAME_OBJECT ){ 1047 return (Expr*)sqlite3RenameTokenMap(pParse, (void*)p, &t); 1048 } 1049 } 1050 return p; 1051 } 1052 1053 } 1054 1055 expr(A) ::= term(A). 1056 expr(A) ::= LP expr(X) RP. {A = X;} 1057 expr(A) ::= id(X). {A=tokenExpr(pParse,TK_ID,X); /*A-overwrites-X*/} 1058 expr(A) ::= JOIN_KW(X). {A=tokenExpr(pParse,TK_ID,X); /*A-overwrites-X*/} 1059 expr(A) ::= nm(X) DOT nm(Y). { 1060 Expr *temp1 = sqlite3ExprAlloc(pParse->db, TK_ID, &X, 1); 1061 Expr *temp2 = sqlite3ExprAlloc(pParse->db, TK_ID, &Y, 1); 1062 if( IN_RENAME_OBJECT ){ 1063 sqlite3RenameTokenMap(pParse, (void*)temp2, &Y); 1064 sqlite3RenameTokenMap(pParse, (void*)temp1, &X); 1065 } 1066 A = sqlite3PExpr(pParse, TK_DOT, temp1, temp2); 1067 } 1068 expr(A) ::= nm(X) DOT nm(Y) DOT nm(Z). { 1069 Expr *temp1 = sqlite3ExprAlloc(pParse->db, TK_ID, &X, 1); 1070 Expr *temp2 = sqlite3ExprAlloc(pParse->db, TK_ID, &Y, 1); 1071 Expr *temp3 = sqlite3ExprAlloc(pParse->db, TK_ID, &Z, 1); 1072 Expr *temp4 = sqlite3PExpr(pParse, TK_DOT, temp2, temp3); 1073 if( IN_RENAME_OBJECT ){ 1074 sqlite3RenameTokenMap(pParse, (void*)temp3, &Z); 1075 sqlite3RenameTokenMap(pParse, (void*)temp2, &Y); 1076 } 1077 A = sqlite3PExpr(pParse, TK_DOT, temp1, temp4); 1078 } 1079 term(A) ::= NULL|FLOAT|BLOB(X). {A=tokenExpr(pParse,@X,X); /*A-overwrites-X*/} 1080 term(A) ::= STRING(X). {A=tokenExpr(pParse,@X,X); /*A-overwrites-X*/} 1081 term(A) ::= INTEGER(X). { 1082 A = sqlite3ExprAlloc(pParse->db, TK_INTEGER, &X, 1); 1083 } 1084 expr(A) ::= VARIABLE(X). { 1085 if( !(X.z[0]=='#' && sqlite3Isdigit(X.z[1])) ){ 1086 u32 n = X.n; 1087 A = tokenExpr(pParse, TK_VARIABLE, X); 1088 sqlite3ExprAssignVarNumber(pParse, A, n); 1089 }else{ 1090 /* When doing a nested parse, one can include terms in an expression 1091 ** that look like this: #1 #2 ... These terms refer to registers 1092 ** in the virtual machine. #N is the N-th register. */ 1093 Token t = X; /*A-overwrites-X*/ 1094 assert( t.n>=2 ); 1095 if( pParse->nested==0 ){ 1096 sqlite3ErrorMsg(pParse, "near \"%T\": syntax error", &t); 1097 A = 0; 1098 }else{ 1099 A = sqlite3PExpr(pParse, TK_REGISTER, 0, 0); 1100 if( A ) sqlite3GetInt32(&t.z[1], &A->iTable); 1101 } 1102 } 1103 } 1104 expr(A) ::= expr(A) COLLATE ids(C). { 1105 A = sqlite3ExprAddCollateToken(pParse, A, &C, 1); 1106 } 1107 %ifndef SQLITE_OMIT_CAST 1108 expr(A) ::= CAST LP expr(E) AS typetoken(T) RP. { 1109 A = sqlite3ExprAlloc(pParse->db, TK_CAST, &T, 1); 1110 sqlite3ExprAttachSubtrees(pParse->db, A, E, 0); 1111 } 1112 %endif SQLITE_OMIT_CAST 1113 1114 1115 expr(A) ::= id(X) LP distinct(D) exprlist(Y) RP. { 1116 A = sqlite3ExprFunction(pParse, Y, &X, D); 1117 } 1118 expr(A) ::= id(X) LP STAR RP. { 1119 A = sqlite3ExprFunction(pParse, 0, &X, 0); 1120 } 1121 1122 %ifndef SQLITE_OMIT_WINDOWFUNC 1123 expr(A) ::= id(X) LP distinct(D) exprlist(Y) RP filter_over(Z). { 1124 A = sqlite3ExprFunction(pParse, Y, &X, D); 1125 sqlite3WindowAttach(pParse, A, Z); 1126 } 1127 expr(A) ::= id(X) LP STAR RP filter_over(Z). { 1128 A = sqlite3ExprFunction(pParse, 0, &X, 0); 1129 sqlite3WindowAttach(pParse, A, Z); 1130 } 1131 %endif 1132 1133 term(A) ::= CTIME_KW(OP). { 1134 A = sqlite3ExprFunction(pParse, 0, &OP, 0); 1135 } 1136 1137 expr(A) ::= LP nexprlist(X) COMMA expr(Y) RP. { 1138 ExprList *pList = sqlite3ExprListAppend(pParse, X, Y); 1139 A = sqlite3PExpr(pParse, TK_VECTOR, 0, 0); 1140 if( A ){ 1141 A->x.pList = pList; 1142 if( ALWAYS(pList->nExpr) ){ 1143 A->flags |= pList->a[0].pExpr->flags & EP_Propagate; 1144 } 1145 }else{ 1146 sqlite3ExprListDelete(pParse->db, pList); 1147 } 1148 } 1149 1150 expr(A) ::= expr(A) AND expr(Y). {A=sqlite3ExprAnd(pParse,A,Y);} 1151 expr(A) ::= expr(A) OR(OP) expr(Y). {A=sqlite3PExpr(pParse,@OP,A,Y);} 1152 expr(A) ::= expr(A) LT|GT|GE|LE(OP) expr(Y). 1153 {A=sqlite3PExpr(pParse,@OP,A,Y);} 1154 expr(A) ::= expr(A) EQ|NE(OP) expr(Y). {A=sqlite3PExpr(pParse,@OP,A,Y);} 1155 expr(A) ::= expr(A) BITAND|BITOR|LSHIFT|RSHIFT(OP) expr(Y). 1156 {A=sqlite3PExpr(pParse,@OP,A,Y);} 1157 expr(A) ::= expr(A) PLUS|MINUS(OP) expr(Y). 1158 {A=sqlite3PExpr(pParse,@OP,A,Y);} 1159 expr(A) ::= expr(A) STAR|SLASH|REM(OP) expr(Y). 1160 {A=sqlite3PExpr(pParse,@OP,A,Y);} 1161 expr(A) ::= expr(A) CONCAT(OP) expr(Y). {A=sqlite3PExpr(pParse,@OP,A,Y);} 1162 %type likeop {Token} 1163 likeop(A) ::= LIKE_KW|MATCH(A). 1164 likeop(A) ::= NOT LIKE_KW|MATCH(X). {A=X; A.n|=0x80000000; /*A-overwrite-X*/} 1165 expr(A) ::= expr(A) likeop(OP) expr(Y). [LIKE_KW] { 1166 ExprList *pList; 1167 int bNot = OP.n & 0x80000000; 1168 OP.n &= 0x7fffffff; 1169 pList = sqlite3ExprListAppend(pParse,0, Y); 1170 pList = sqlite3ExprListAppend(pParse,pList, A); 1171 A = sqlite3ExprFunction(pParse, pList, &OP, 0); 1172 if( bNot ) A = sqlite3PExpr(pParse, TK_NOT, A, 0); 1173 if( A ) A->flags |= EP_InfixFunc; 1174 } 1175 expr(A) ::= expr(A) likeop(OP) expr(Y) ESCAPE expr(E). [LIKE_KW] { 1176 ExprList *pList; 1177 int bNot = OP.n & 0x80000000; 1178 OP.n &= 0x7fffffff; 1179 pList = sqlite3ExprListAppend(pParse,0, Y); 1180 pList = sqlite3ExprListAppend(pParse,pList, A); 1181 pList = sqlite3ExprListAppend(pParse,pList, E); 1182 A = sqlite3ExprFunction(pParse, pList, &OP, 0); 1183 if( bNot ) A = sqlite3PExpr(pParse, TK_NOT, A, 0); 1184 if( A ) A->flags |= EP_InfixFunc; 1185 } 1186 1187 expr(A) ::= expr(A) ISNULL|NOTNULL(E). {A = sqlite3PExpr(pParse,@E,A,0);} 1188 expr(A) ::= expr(A) NOT NULL. {A = sqlite3PExpr(pParse,TK_NOTNULL,A,0);} 1189 1190 %include { 1191 /* A routine to convert a binary TK_IS or TK_ISNOT expression into a 1192 ** unary TK_ISNULL or TK_NOTNULL expression. */ 1193 static void binaryToUnaryIfNull(Parse *pParse, Expr *pY, Expr *pA, int op){ 1194 sqlite3 *db = pParse->db; 1195 if( pA && pY && pY->op==TK_NULL && !IN_RENAME_OBJECT ){ 1196 pA->op = (u8)op; 1197 sqlite3ExprDelete(db, pA->pRight); 1198 pA->pRight = 0; 1199 } 1200 } 1201 } 1202 1203 // expr1 IS expr2 1204 // expr1 IS NOT expr2 1205 // 1206 // If expr2 is NULL then code as TK_ISNULL or TK_NOTNULL. If expr2 1207 // is any other expression, code as TK_IS or TK_ISNOT. 1208 // 1209 expr(A) ::= expr(A) IS expr(Y). { 1210 A = sqlite3PExpr(pParse,TK_IS,A,Y); 1211 binaryToUnaryIfNull(pParse, Y, A, TK_ISNULL); 1212 } 1213 expr(A) ::= expr(A) IS NOT expr(Y). { 1214 A = sqlite3PExpr(pParse,TK_ISNOT,A,Y); 1215 binaryToUnaryIfNull(pParse, Y, A, TK_NOTNULL); 1216 } 1217 1218 expr(A) ::= NOT(B) expr(X). 1219 {A = sqlite3PExpr(pParse, @B, X, 0);/*A-overwrites-B*/} 1220 expr(A) ::= BITNOT(B) expr(X). 1221 {A = sqlite3PExpr(pParse, @B, X, 0);/*A-overwrites-B*/} 1222 expr(A) ::= PLUS|MINUS(B) expr(X). [BITNOT] { 1223 A = sqlite3PExpr(pParse, @B==TK_PLUS ? TK_UPLUS : TK_UMINUS, X, 0); 1224 /*A-overwrites-B*/ 1225 } 1226 1227 %type between_op {int} 1228 between_op(A) ::= BETWEEN. {A = 0;} 1229 between_op(A) ::= NOT BETWEEN. {A = 1;} 1230 expr(A) ::= expr(A) between_op(N) expr(X) AND expr(Y). [BETWEEN] { 1231 ExprList *pList = sqlite3ExprListAppend(pParse,0, X); 1232 pList = sqlite3ExprListAppend(pParse,pList, Y); 1233 A = sqlite3PExpr(pParse, TK_BETWEEN, A, 0); 1234 if( A ){ 1235 A->x.pList = pList; 1236 }else{ 1237 sqlite3ExprListDelete(pParse->db, pList); 1238 } 1239 if( N ) A = sqlite3PExpr(pParse, TK_NOT, A, 0); 1240 } 1241 %ifndef SQLITE_OMIT_SUBQUERY 1242 %type in_op {int} 1243 in_op(A) ::= IN. {A = 0;} 1244 in_op(A) ::= NOT IN. {A = 1;} 1245 expr(A) ::= expr(A) in_op(N) LP exprlist(Y) RP. [IN] { 1246 if( Y==0 ){ 1247 /* Expressions of the form 1248 ** 1249 ** expr1 IN () 1250 ** expr1 NOT IN () 1251 ** 1252 ** simplify to constants 0 (false) and 1 (true), respectively, 1253 ** regardless of the value of expr1. 1254 */ 1255 sqlite3ExprUnmapAndDelete(pParse, A); 1256 A = sqlite3Expr(pParse->db, TK_INTEGER, N ? "1" : "0"); 1257 }else{ 1258 Expr *pRHS = Y->a[0].pExpr; 1259 if( Y->nExpr==1 && sqlite3ExprIsConstant(pRHS) && pRHS->op!=TK_VECTOR ){ 1260 Y->a[0].pExpr = 0; 1261 sqlite3ExprListDelete(pParse->db, Y); 1262 pRHS = sqlite3PExpr(pParse, TK_UPLUS, pRHS, 0); 1263 A = sqlite3PExpr(pParse, TK_EQ, A, pRHS); 1264 }else{ 1265 A = sqlite3PExpr(pParse, TK_IN, A, 0); 1266 if( pRHS->op==TK_VECTOR || A==0 ){ 1267 Select *pRHS = sqlite3ExprListToValues(pParse, Y); 1268 if( pRHS ){ 1269 parserDoubleLinkSelect(pParse, pRHS); 1270 sqlite3PExprAddSelect(pParse, A, pRHS); 1271 } 1272 }else{ 1273 A->x.pList = Y; 1274 sqlite3ExprSetHeightAndFlags(pParse, A); 1275 } 1276 } 1277 if( N ) A = sqlite3PExpr(pParse, TK_NOT, A, 0); 1278 } 1279 } 1280 expr(A) ::= LP select(X) RP. { 1281 A = sqlite3PExpr(pParse, TK_SELECT, 0, 0); 1282 sqlite3PExprAddSelect(pParse, A, X); 1283 } 1284 expr(A) ::= expr(A) in_op(N) LP select(Y) RP. [IN] { 1285 A = sqlite3PExpr(pParse, TK_IN, A, 0); 1286 sqlite3PExprAddSelect(pParse, A, Y); 1287 if( N ) A = sqlite3PExpr(pParse, TK_NOT, A, 0); 1288 } 1289 expr(A) ::= expr(A) in_op(N) nm(Y) dbnm(Z) paren_exprlist(E). [IN] { 1290 SrcList *pSrc = sqlite3SrcListAppend(pParse, 0,&Y,&Z); 1291 Select *pSelect = sqlite3SelectNew(pParse, 0,pSrc,0,0,0,0,0,0); 1292 if( E ) sqlite3SrcListFuncArgs(pParse, pSelect ? pSrc : 0, E); 1293 A = sqlite3PExpr(pParse, TK_IN, A, 0); 1294 sqlite3PExprAddSelect(pParse, A, pSelect); 1295 if( N ) A = sqlite3PExpr(pParse, TK_NOT, A, 0); 1296 } 1297 expr(A) ::= EXISTS LP select(Y) RP. { 1298 Expr *p; 1299 p = A = sqlite3PExpr(pParse, TK_EXISTS, 0, 0); 1300 sqlite3PExprAddSelect(pParse, p, Y); 1301 } 1302 %endif SQLITE_OMIT_SUBQUERY 1303 1304 /* CASE expressions */ 1305 expr(A) ::= CASE case_operand(X) case_exprlist(Y) case_else(Z) END. { 1306 A = sqlite3PExpr(pParse, TK_CASE, X, 0); 1307 if( A ){ 1308 A->x.pList = Z ? sqlite3ExprListAppend(pParse,Y,Z) : Y; 1309 sqlite3ExprSetHeightAndFlags(pParse, A); 1310 }else{ 1311 sqlite3ExprListDelete(pParse->db, Y); 1312 sqlite3ExprDelete(pParse->db, Z); 1313 } 1314 } 1315 %type case_exprlist {ExprList*} 1316 %destructor case_exprlist {sqlite3ExprListDelete(pParse->db, $$);} 1317 case_exprlist(A) ::= case_exprlist(A) WHEN expr(Y) THEN expr(Z). { 1318 A = sqlite3ExprListAppend(pParse,A, Y); 1319 A = sqlite3ExprListAppend(pParse,A, Z); 1320 } 1321 case_exprlist(A) ::= WHEN expr(Y) THEN expr(Z). { 1322 A = sqlite3ExprListAppend(pParse,0, Y); 1323 A = sqlite3ExprListAppend(pParse,A, Z); 1324 } 1325 %type case_else {Expr*} 1326 %destructor case_else {sqlite3ExprDelete(pParse->db, $$);} 1327 case_else(A) ::= ELSE expr(X). {A = X;} 1328 case_else(A) ::= . {A = 0;} 1329 %type case_operand {Expr*} 1330 %destructor case_operand {sqlite3ExprDelete(pParse->db, $$);} 1331 case_operand(A) ::= expr(X). {A = X; /*A-overwrites-X*/} 1332 case_operand(A) ::= . {A = 0;} 1333 1334 %type exprlist {ExprList*} 1335 %destructor exprlist {sqlite3ExprListDelete(pParse->db, $$);} 1336 %type nexprlist {ExprList*} 1337 %destructor nexprlist {sqlite3ExprListDelete(pParse->db, $$);} 1338 1339 exprlist(A) ::= nexprlist(A). 1340 exprlist(A) ::= . {A = 0;} 1341 nexprlist(A) ::= nexprlist(A) COMMA expr(Y). 1342 {A = sqlite3ExprListAppend(pParse,A,Y);} 1343 nexprlist(A) ::= expr(Y). 1344 {A = sqlite3ExprListAppend(pParse,0,Y); /*A-overwrites-Y*/} 1345 1346 %ifndef SQLITE_OMIT_SUBQUERY 1347 /* A paren_exprlist is an optional expression list contained inside 1348 ** of parenthesis */ 1349 %type paren_exprlist {ExprList*} 1350 %destructor paren_exprlist {sqlite3ExprListDelete(pParse->db, $$);} 1351 paren_exprlist(A) ::= . {A = 0;} 1352 paren_exprlist(A) ::= LP exprlist(X) RP. {A = X;} 1353 %endif SQLITE_OMIT_SUBQUERY 1354 1355 1356 ///////////////////////////// The CREATE INDEX command /////////////////////// 1357 // 1358 cmd ::= createkw(S) uniqueflag(U) INDEX ifnotexists(NE) nm(X) dbnm(D) 1359 ON nm(Y) LP sortlist(Z) RP where_opt(W). { 1360 sqlite3CreateIndex(pParse, &X, &D, 1361 sqlite3SrcListAppend(pParse,0,&Y,0), Z, U, 1362 &S, W, SQLITE_SO_ASC, NE, SQLITE_IDXTYPE_APPDEF); 1363 if( IN_RENAME_OBJECT && pParse->pNewIndex ){ 1364 sqlite3RenameTokenMap(pParse, pParse->pNewIndex->zName, &Y); 1365 } 1366 } 1367 1368 %type uniqueflag {int} 1369 uniqueflag(A) ::= UNIQUE. {A = OE_Abort;} 1370 uniqueflag(A) ::= . {A = OE_None;} 1371 1372 1373 // The eidlist non-terminal (Expression Id List) generates an ExprList 1374 // from a list of identifiers. The identifier names are in ExprList.a[].zName. 1375 // This list is stored in an ExprList rather than an IdList so that it 1376 // can be easily sent to sqlite3ColumnsExprList(). 1377 // 1378 // eidlist is grouped with CREATE INDEX because it used to be the non-terminal 1379 // used for the arguments to an index. That is just an historical accident. 1380 // 1381 // IMPORTANT COMPATIBILITY NOTE: Some prior versions of SQLite accepted 1382 // COLLATE clauses and ASC or DESC keywords on ID lists in inappropriate 1383 // places - places that might have been stored in the sqlite_schema table. 1384 // Those extra features were ignored. But because they might be in some 1385 // (busted) old databases, we need to continue parsing them when loading 1386 // historical schemas. 1387 // 1388 %type eidlist {ExprList*} 1389 %destructor eidlist {sqlite3ExprListDelete(pParse->db, $$);} 1390 %type eidlist_opt {ExprList*} 1391 %destructor eidlist_opt {sqlite3ExprListDelete(pParse->db, $$);} 1392 1393 %include { 1394 /* Add a single new term to an ExprList that is used to store a 1395 ** list of identifiers. Report an error if the ID list contains 1396 ** a COLLATE clause or an ASC or DESC keyword, except ignore the 1397 ** error while parsing a legacy schema. 1398 */ 1399 static ExprList *parserAddExprIdListTerm( 1400 Parse *pParse, 1401 ExprList *pPrior, 1402 Token *pIdToken, 1403 int hasCollate, 1404 int sortOrder 1405 ){ 1406 ExprList *p = sqlite3ExprListAppend(pParse, pPrior, 0); 1407 if( (hasCollate || sortOrder!=SQLITE_SO_UNDEFINED) 1408 && pParse->db->init.busy==0 1409 ){ 1410 sqlite3ErrorMsg(pParse, "syntax error after column name \"%.*s\"", 1411 pIdToken->n, pIdToken->z); 1412 } 1413 sqlite3ExprListSetName(pParse, p, pIdToken, 1); 1414 return p; 1415 } 1416 } // end %include 1417 1418 eidlist_opt(A) ::= . {A = 0;} 1419 eidlist_opt(A) ::= LP eidlist(X) RP. {A = X;} 1420 eidlist(A) ::= eidlist(A) COMMA nm(Y) collate(C) sortorder(Z). { 1421 A = parserAddExprIdListTerm(pParse, A, &Y, C, Z); 1422 } 1423 eidlist(A) ::= nm(Y) collate(C) sortorder(Z). { 1424 A = parserAddExprIdListTerm(pParse, 0, &Y, C, Z); /*A-overwrites-Y*/ 1425 } 1426 1427 %type collate {int} 1428 collate(C) ::= . {C = 0;} 1429 collate(C) ::= COLLATE ids. {C = 1;} 1430 1431 1432 ///////////////////////////// The DROP INDEX command ///////////////////////// 1433 // 1434 cmd ::= DROP INDEX ifexists(E) fullname(X). {sqlite3DropIndex(pParse, X, E);} 1435 1436 ///////////////////////////// The VACUUM command ///////////////////////////// 1437 // 1438 %if !SQLITE_OMIT_VACUUM && !SQLITE_OMIT_ATTACH 1439 %type vinto {Expr*} 1440 %destructor vinto {sqlite3ExprDelete(pParse->db, $$);} 1441 cmd ::= VACUUM vinto(Y). {sqlite3Vacuum(pParse,0,Y);} 1442 cmd ::= VACUUM nm(X) vinto(Y). {sqlite3Vacuum(pParse,&X,Y);} 1443 vinto(A) ::= INTO expr(X). {A = X;} 1444 vinto(A) ::= . {A = 0;} 1445 %endif 1446 1447 ///////////////////////////// The PRAGMA command ///////////////////////////// 1448 // 1449 %ifndef SQLITE_OMIT_PRAGMA 1450 cmd ::= PRAGMA nm(X) dbnm(Z). {sqlite3Pragma(pParse,&X,&Z,0,0);} 1451 cmd ::= PRAGMA nm(X) dbnm(Z) EQ nmnum(Y). {sqlite3Pragma(pParse,&X,&Z,&Y,0);} 1452 cmd ::= PRAGMA nm(X) dbnm(Z) LP nmnum(Y) RP. {sqlite3Pragma(pParse,&X,&Z,&Y,0);} 1453 cmd ::= PRAGMA nm(X) dbnm(Z) EQ minus_num(Y). 1454 {sqlite3Pragma(pParse,&X,&Z,&Y,1);} 1455 cmd ::= PRAGMA nm(X) dbnm(Z) LP minus_num(Y) RP. 1456 {sqlite3Pragma(pParse,&X,&Z,&Y,1);} 1457 1458 nmnum(A) ::= plus_num(A). 1459 nmnum(A) ::= nm(A). 1460 nmnum(A) ::= ON(A). 1461 nmnum(A) ::= DELETE(A). 1462 nmnum(A) ::= DEFAULT(A). 1463 %endif SQLITE_OMIT_PRAGMA 1464 %token_class number INTEGER|FLOAT. 1465 plus_num(A) ::= PLUS number(X). {A = X;} 1466 plus_num(A) ::= number(A). 1467 minus_num(A) ::= MINUS number(X). {A = X;} 1468 //////////////////////////// The CREATE TRIGGER command ///////////////////// 1469 1470 %ifndef SQLITE_OMIT_TRIGGER 1471 1472 cmd ::= createkw trigger_decl(A) BEGIN trigger_cmd_list(S) END(Z). { 1473 Token all; 1474 all.z = A.z; 1475 all.n = (int)(Z.z - A.z) + Z.n; 1476 sqlite3FinishTrigger(pParse, S, &all); 1477 } 1478 1479 trigger_decl(A) ::= temp(T) TRIGGER ifnotexists(NOERR) nm(B) dbnm(Z) 1480 trigger_time(C) trigger_event(D) 1481 ON fullname(E) foreach_clause when_clause(G). { 1482 sqlite3BeginTrigger(pParse, &B, &Z, C, D.a, D.b, E, G, T, NOERR); 1483 A = (Z.n==0?B:Z); /*A-overwrites-T*/ 1484 } 1485 1486 %type trigger_time {int} 1487 trigger_time(A) ::= BEFORE|AFTER(X). { A = @X; /*A-overwrites-X*/ } 1488 trigger_time(A) ::= INSTEAD OF. { A = TK_INSTEAD;} 1489 trigger_time(A) ::= . { A = TK_BEFORE; } 1490 1491 %type trigger_event {struct TrigEvent} 1492 %destructor trigger_event {sqlite3IdListDelete(pParse->db, $$.b);} 1493 trigger_event(A) ::= DELETE|INSERT(X). {A.a = @X; /*A-overwrites-X*/ A.b = 0;} 1494 trigger_event(A) ::= UPDATE(X). {A.a = @X; /*A-overwrites-X*/ A.b = 0;} 1495 trigger_event(A) ::= UPDATE OF idlist(X).{A.a = TK_UPDATE; A.b = X;} 1496 1497 foreach_clause ::= . 1498 foreach_clause ::= FOR EACH ROW. 1499 1500 %type when_clause {Expr*} 1501 %destructor when_clause {sqlite3ExprDelete(pParse->db, $$);} 1502 when_clause(A) ::= . { A = 0; } 1503 when_clause(A) ::= WHEN expr(X). { A = X; } 1504 1505 %type trigger_cmd_list {TriggerStep*} 1506 %destructor trigger_cmd_list {sqlite3DeleteTriggerStep(pParse->db, $$);} 1507 trigger_cmd_list(A) ::= trigger_cmd_list(A) trigger_cmd(X) SEMI. { 1508 assert( A!=0 ); 1509 A->pLast->pNext = X; 1510 A->pLast = X; 1511 } 1512 trigger_cmd_list(A) ::= trigger_cmd(A) SEMI. { 1513 assert( A!=0 ); 1514 A->pLast = A; 1515 } 1516 1517 // Disallow qualified table names on INSERT, UPDATE, and DELETE statements 1518 // within a trigger. The table to INSERT, UPDATE, or DELETE is always in 1519 // the same database as the table that the trigger fires on. 1520 // 1521 %type trnm {Token} 1522 trnm(A) ::= nm(A). 1523 trnm(A) ::= nm DOT nm(X). { 1524 A = X; 1525 sqlite3ErrorMsg(pParse, 1526 "qualified table names are not allowed on INSERT, UPDATE, and DELETE " 1527 "statements within triggers"); 1528 } 1529 1530 // Disallow the INDEX BY and NOT INDEXED clauses on UPDATE and DELETE 1531 // statements within triggers. We make a specific error message for this 1532 // since it is an exception to the default grammar rules. 1533 // 1534 tridxby ::= . 1535 tridxby ::= INDEXED BY nm. { 1536 sqlite3ErrorMsg(pParse, 1537 "the INDEXED BY clause is not allowed on UPDATE or DELETE statements " 1538 "within triggers"); 1539 } 1540 tridxby ::= NOT INDEXED. { 1541 sqlite3ErrorMsg(pParse, 1542 "the NOT INDEXED clause is not allowed on UPDATE or DELETE statements " 1543 "within triggers"); 1544 } 1545 1546 1547 1548 %type trigger_cmd {TriggerStep*} 1549 %destructor trigger_cmd {sqlite3DeleteTriggerStep(pParse->db, $$);} 1550 // UPDATE 1551 trigger_cmd(A) ::= 1552 UPDATE(B) orconf(R) trnm(X) tridxby SET setlist(Y) from(F) where_opt(Z) scanpt(E). 1553 {A = sqlite3TriggerUpdateStep(pParse, &X, F, Y, Z, R, B.z, E);} 1554 1555 // INSERT 1556 trigger_cmd(A) ::= scanpt(B) insert_cmd(R) INTO 1557 trnm(X) idlist_opt(F) select(S) upsert(U) scanpt(Z). { 1558 A = sqlite3TriggerInsertStep(pParse,&X,F,S,R,U,B,Z);/*A-overwrites-R*/ 1559 } 1560 // DELETE 1561 trigger_cmd(A) ::= DELETE(B) FROM trnm(X) tridxby where_opt(Y) scanpt(E). 1562 {A = sqlite3TriggerDeleteStep(pParse, &X, Y, B.z, E);} 1563 1564 // SELECT 1565 trigger_cmd(A) ::= scanpt(B) select(X) scanpt(E). 1566 {A = sqlite3TriggerSelectStep(pParse->db, X, B, E); /*A-overwrites-X*/} 1567 1568 // The special RAISE expression that may occur in trigger programs 1569 expr(A) ::= RAISE LP IGNORE RP. { 1570 A = sqlite3PExpr(pParse, TK_RAISE, 0, 0); 1571 if( A ){ 1572 A->affExpr = OE_Ignore; 1573 } 1574 } 1575 expr(A) ::= RAISE LP raisetype(T) COMMA nm(Z) RP. { 1576 A = sqlite3ExprAlloc(pParse->db, TK_RAISE, &Z, 1); 1577 if( A ) { 1578 A->affExpr = (char)T; 1579 } 1580 } 1581 %endif !SQLITE_OMIT_TRIGGER 1582 1583 %type raisetype {int} 1584 raisetype(A) ::= ROLLBACK. {A = OE_Rollback;} 1585 raisetype(A) ::= ABORT. {A = OE_Abort;} 1586 raisetype(A) ::= FAIL. {A = OE_Fail;} 1587 1588 1589 //////////////////////// DROP TRIGGER statement ////////////////////////////// 1590 %ifndef SQLITE_OMIT_TRIGGER 1591 cmd ::= DROP TRIGGER ifexists(NOERR) fullname(X). { 1592 sqlite3DropTrigger(pParse,X,NOERR); 1593 } 1594 %endif !SQLITE_OMIT_TRIGGER 1595 1596 //////////////////////// ATTACH DATABASE file AS name ///////////////////////// 1597 %ifndef SQLITE_OMIT_ATTACH 1598 cmd ::= ATTACH database_kw_opt expr(F) AS expr(D) key_opt(K). { 1599 sqlite3Attach(pParse, F, D, K); 1600 } 1601 cmd ::= DETACH database_kw_opt expr(D). { 1602 sqlite3Detach(pParse, D); 1603 } 1604 1605 %type key_opt {Expr*} 1606 %destructor key_opt {sqlite3ExprDelete(pParse->db, $$);} 1607 key_opt(A) ::= . { A = 0; } 1608 key_opt(A) ::= KEY expr(X). { A = X; } 1609 1610 database_kw_opt ::= DATABASE. 1611 database_kw_opt ::= . 1612 %endif SQLITE_OMIT_ATTACH 1613 1614 ////////////////////////// REINDEX collation ////////////////////////////////// 1615 %ifndef SQLITE_OMIT_REINDEX 1616 cmd ::= REINDEX. {sqlite3Reindex(pParse, 0, 0);} 1617 cmd ::= REINDEX nm(X) dbnm(Y). {sqlite3Reindex(pParse, &X, &Y);} 1618 %endif SQLITE_OMIT_REINDEX 1619 1620 /////////////////////////////////// ANALYZE /////////////////////////////////// 1621 %ifndef SQLITE_OMIT_ANALYZE 1622 cmd ::= ANALYZE. {sqlite3Analyze(pParse, 0, 0);} 1623 cmd ::= ANALYZE nm(X) dbnm(Y). {sqlite3Analyze(pParse, &X, &Y);} 1624 %endif 1625 1626 //////////////////////// ALTER TABLE table ... //////////////////////////////// 1627 %ifndef SQLITE_OMIT_ALTERTABLE 1628 cmd ::= ALTER TABLE fullname(X) RENAME TO nm(Z). { 1629 sqlite3AlterRenameTable(pParse,X,&Z); 1630 } 1631 cmd ::= ALTER TABLE add_column_fullname 1632 ADD kwcolumn_opt columnname(Y) carglist. { 1633 Y.n = (int)(pParse->sLastToken.z-Y.z) + pParse->sLastToken.n; 1634 sqlite3AlterFinishAddColumn(pParse, &Y); 1635 } 1636 cmd ::= ALTER TABLE fullname(X) DROP kwcolumn_opt nm(Y). { 1637 sqlite3AlterDropColumn(pParse, X, &Y); 1638 } 1639 1640 add_column_fullname ::= fullname(X). { 1641 disableLookaside(pParse); 1642 sqlite3AlterBeginAddColumn(pParse, X); 1643 } 1644 cmd ::= ALTER TABLE fullname(X) RENAME kwcolumn_opt nm(Y) TO nm(Z). { 1645 sqlite3AlterRenameColumn(pParse, X, &Y, &Z); 1646 } 1647 1648 kwcolumn_opt ::= . 1649 kwcolumn_opt ::= COLUMNKW. 1650 1651 %endif SQLITE_OMIT_ALTERTABLE 1652 1653 //////////////////////// CREATE VIRTUAL TABLE ... ///////////////////////////// 1654 %ifndef SQLITE_OMIT_VIRTUALTABLE 1655 cmd ::= create_vtab. {sqlite3VtabFinishParse(pParse,0);} 1656 cmd ::= create_vtab LP vtabarglist RP(X). {sqlite3VtabFinishParse(pParse,&X);} 1657 create_vtab ::= createkw VIRTUAL TABLE ifnotexists(E) 1658 nm(X) dbnm(Y) USING nm(Z). { 1659 sqlite3VtabBeginParse(pParse, &X, &Y, &Z, E); 1660 } 1661 vtabarglist ::= vtabarg. 1662 vtabarglist ::= vtabarglist COMMA vtabarg. 1663 vtabarg ::= . {sqlite3VtabArgInit(pParse);} 1664 vtabarg ::= vtabarg vtabargtoken. 1665 vtabargtoken ::= ANY(X). {sqlite3VtabArgExtend(pParse,&X);} 1666 vtabargtoken ::= lp anylist RP(X). {sqlite3VtabArgExtend(pParse,&X);} 1667 lp ::= LP(X). {sqlite3VtabArgExtend(pParse,&X);} 1668 anylist ::= . 1669 anylist ::= anylist LP anylist RP. 1670 anylist ::= anylist ANY. 1671 %endif SQLITE_OMIT_VIRTUALTABLE 1672 1673 1674 //////////////////////// COMMON TABLE EXPRESSIONS //////////////////////////// 1675 %type wqlist {With*} 1676 %destructor wqlist {sqlite3WithDelete(pParse->db, $$);} 1677 %type wqitem {Cte*} 1678 // %destructor wqitem {sqlite3CteDelete(pParse->db, $$);} // not reachable 1679 1680 with ::= . 1681 %ifndef SQLITE_OMIT_CTE 1682 with ::= WITH wqlist(W). { sqlite3WithPush(pParse, W, 1); } 1683 with ::= WITH RECURSIVE wqlist(W). { sqlite3WithPush(pParse, W, 1); } 1684 1685 %type wqas {u8} 1686 wqas(A) ::= AS. {A = M10d_Any;} 1687 wqas(A) ::= AS MATERIALIZED. {A = M10d_Yes;} 1688 wqas(A) ::= AS NOT MATERIALIZED. {A = M10d_No;} 1689 wqitem(A) ::= nm(X) eidlist_opt(Y) wqas(M) LP select(Z) RP. { 1690 A = sqlite3CteNew(pParse, &X, Y, Z, M); /*A-overwrites-X*/ 1691 } 1692 wqlist(A) ::= wqitem(X). { 1693 A = sqlite3WithAdd(pParse, 0, X); /*A-overwrites-X*/ 1694 } 1695 wqlist(A) ::= wqlist(A) COMMA wqitem(X). { 1696 A = sqlite3WithAdd(pParse, A, X); 1697 } 1698 %endif SQLITE_OMIT_CTE 1699 1700 //////////////////////// WINDOW FUNCTION EXPRESSIONS ///////////////////////// 1701 // These must be at the end of this file. Specifically, the rules that 1702 // introduce tokens WINDOW, OVER and FILTER must appear last. This causes 1703 // the integer values assigned to these tokens to be larger than all other 1704 // tokens that may be output by the tokenizer except TK_SPACE and TK_ILLEGAL. 1705 // 1706 %ifndef SQLITE_OMIT_WINDOWFUNC 1707 %type windowdefn_list {Window*} 1708 %destructor windowdefn_list {sqlite3WindowListDelete(pParse->db, $$);} 1709 windowdefn_list(A) ::= windowdefn(Z). { A = Z; } 1710 windowdefn_list(A) ::= windowdefn_list(Y) COMMA windowdefn(Z). { 1711 assert( Z!=0 ); 1712 sqlite3WindowChain(pParse, Z, Y); 1713 Z->pNextWin = Y; 1714 A = Z; 1715 } 1716 1717 %type windowdefn {Window*} 1718 %destructor windowdefn {sqlite3WindowDelete(pParse->db, $$);} 1719 windowdefn(A) ::= nm(X) AS LP window(Y) RP. { 1720 if( ALWAYS(Y) ){ 1721 Y->zName = sqlite3DbStrNDup(pParse->db, X.z, X.n); 1722 } 1723 A = Y; 1724 } 1725 1726 %type window {Window*} 1727 %destructor window {sqlite3WindowDelete(pParse->db, $$);} 1728 1729 %type frame_opt {Window*} 1730 %destructor frame_opt {sqlite3WindowDelete(pParse->db, $$);} 1731 1732 %type part_opt {ExprList*} 1733 %destructor part_opt {sqlite3ExprListDelete(pParse->db, $$);} 1734 1735 %type filter_clause {Expr*} 1736 %destructor filter_clause {sqlite3ExprDelete(pParse->db, $$);} 1737 1738 %type over_clause {Window*} 1739 %destructor over_clause {sqlite3WindowDelete(pParse->db, $$);} 1740 1741 %type filter_over {Window*} 1742 %destructor filter_over {sqlite3WindowDelete(pParse->db, $$);} 1743 1744 %type range_or_rows {int} 1745 1746 %type frame_bound {struct FrameBound} 1747 %destructor frame_bound {sqlite3ExprDelete(pParse->db, $$.pExpr);} 1748 %type frame_bound_s {struct FrameBound} 1749 %destructor frame_bound_s {sqlite3ExprDelete(pParse->db, $$.pExpr);} 1750 %type frame_bound_e {struct FrameBound} 1751 %destructor frame_bound_e {sqlite3ExprDelete(pParse->db, $$.pExpr);} 1752 1753 window(A) ::= PARTITION BY nexprlist(X) orderby_opt(Y) frame_opt(Z). { 1754 A = sqlite3WindowAssemble(pParse, Z, X, Y, 0); 1755 } 1756 window(A) ::= nm(W) PARTITION BY nexprlist(X) orderby_opt(Y) frame_opt(Z). { 1757 A = sqlite3WindowAssemble(pParse, Z, X, Y, &W); 1758 } 1759 window(A) ::= ORDER BY sortlist(Y) frame_opt(Z). { 1760 A = sqlite3WindowAssemble(pParse, Z, 0, Y, 0); 1761 } 1762 window(A) ::= nm(W) ORDER BY sortlist(Y) frame_opt(Z). { 1763 A = sqlite3WindowAssemble(pParse, Z, 0, Y, &W); 1764 } 1765 window(A) ::= frame_opt(Z). { 1766 A = Z; 1767 } 1768 window(A) ::= nm(W) frame_opt(Z). { 1769 A = sqlite3WindowAssemble(pParse, Z, 0, 0, &W); 1770 } 1771 1772 frame_opt(A) ::= . { 1773 A = sqlite3WindowAlloc(pParse, 0, TK_UNBOUNDED, 0, TK_CURRENT, 0, 0); 1774 } 1775 frame_opt(A) ::= range_or_rows(X) frame_bound_s(Y) frame_exclude_opt(Z). { 1776 A = sqlite3WindowAlloc(pParse, X, Y.eType, Y.pExpr, TK_CURRENT, 0, Z); 1777 } 1778 frame_opt(A) ::= range_or_rows(X) BETWEEN frame_bound_s(Y) AND 1779 frame_bound_e(Z) frame_exclude_opt(W). { 1780 A = sqlite3WindowAlloc(pParse, X, Y.eType, Y.pExpr, Z.eType, Z.pExpr, W); 1781 } 1782 1783 range_or_rows(A) ::= RANGE|ROWS|GROUPS(X). {A = @X; /*A-overwrites-X*/} 1784 1785 frame_bound_s(A) ::= frame_bound(X). {A = X;} 1786 frame_bound_s(A) ::= UNBOUNDED(X) PRECEDING. {A.eType = @X; A.pExpr = 0;} 1787 frame_bound_e(A) ::= frame_bound(X). {A = X;} 1788 frame_bound_e(A) ::= UNBOUNDED(X) FOLLOWING. {A.eType = @X; A.pExpr = 0;} 1789 1790 frame_bound(A) ::= expr(X) PRECEDING|FOLLOWING(Y). 1791 {A.eType = @Y; A.pExpr = X;} 1792 frame_bound(A) ::= CURRENT(X) ROW. {A.eType = @X; A.pExpr = 0;} 1793 1794 %type frame_exclude_opt {u8} 1795 frame_exclude_opt(A) ::= . {A = 0;} 1796 frame_exclude_opt(A) ::= EXCLUDE frame_exclude(X). {A = X;} 1797 1798 %type frame_exclude {u8} 1799 frame_exclude(A) ::= NO(X) OTHERS. {A = @X; /*A-overwrites-X*/} 1800 frame_exclude(A) ::= CURRENT(X) ROW. {A = @X; /*A-overwrites-X*/} 1801 frame_exclude(A) ::= GROUP|TIES(X). {A = @X; /*A-overwrites-X*/} 1802 1803 1804 %type window_clause {Window*} 1805 %destructor window_clause {sqlite3WindowListDelete(pParse->db, $$);} 1806 window_clause(A) ::= WINDOW windowdefn_list(B). { A = B; } 1807 1808 filter_over(A) ::= filter_clause(F) over_clause(O). { 1809 if( O ){ 1810 O->pFilter = F; 1811 }else{ 1812 sqlite3ExprDelete(pParse->db, F); 1813 } 1814 A = O; 1815 } 1816 filter_over(A) ::= over_clause(O). { 1817 A = O; 1818 } 1819 filter_over(A) ::= filter_clause(F). { 1820 A = (Window*)sqlite3DbMallocZero(pParse->db, sizeof(Window)); 1821 if( A ){ 1822 A->eFrmType = TK_FILTER; 1823 A->pFilter = F; 1824 }else{ 1825 sqlite3ExprDelete(pParse->db, F); 1826 } 1827 } 1828 1829 over_clause(A) ::= OVER LP window(Z) RP. { 1830 A = Z; 1831 assert( A!=0 ); 1832 } 1833 over_clause(A) ::= OVER nm(Z). { 1834 A = (Window*)sqlite3DbMallocZero(pParse->db, sizeof(Window)); 1835 if( A ){ 1836 A->zName = sqlite3DbStrNDup(pParse->db, Z.z, Z.n); 1837 } 1838 } 1839 1840 filter_clause(A) ::= FILTER LP WHERE expr(X) RP. { A = X; } 1841 %endif /* SQLITE_OMIT_WINDOWFUNC */ 1842 1843 /* 1844 ** The code generator needs some extra TK_ token values for tokens that 1845 ** are synthesized and do not actually appear in the grammar: 1846 */ 1847 %token 1848 COLUMN /* Reference to a table column */ 1849 AGG_FUNCTION /* An aggregate function */ 1850 AGG_COLUMN /* An aggregated column */ 1851 TRUEFALSE /* True or false keyword */ 1852 ISNOT /* Combination of IS and NOT */ 1853 FUNCTION /* A function invocation */ 1854 UMINUS /* Unary minus */ 1855 UPLUS /* Unary plus */ 1856 TRUTH /* IS TRUE or IS FALSE or IS NOT TRUE or IS NOT FALSE */ 1857 REGISTER /* Reference to a VDBE register */ 1858 VECTOR /* Vector */ 1859 SELECT_COLUMN /* Choose a single column from a multi-column SELECT */ 1860 IF_NULL_ROW /* the if-null-row operator */ 1861 ASTERISK /* The "*" in count(*) and similar */ 1862 SPAN /* The span operator */ 1863 ERROR /* An expression containing an error */ 1864 . 1865 /* There must be no more than 255 tokens defined above. If this grammar 1866 ** is extended with new rules and tokens, they must either be so few in 1867 ** number that TK_SPAN is no more than 255, or else the new tokens must 1868 ** appear after this line. 1869 */ 1870 %include { 1871 #if TK_SPAN>255 1872 # error too many tokens in the grammar 1873 #endif 1874 } 1875 1876 /* 1877 ** The TK_SPACE and TK_ILLEGAL tokens must be the last two tokens. The 1878 ** parser depends on this. Those tokens are not used in any grammar rule. 1879 ** They are only used by the tokenizer. Declare them last so that they 1880 ** are guaranteed to be the last two tokens 1881 */ 1882 %token SPACE ILLEGAL. 1883