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