1 /* 2 ** 2001 September 15 3 ** 4 ** The author disclaims copyright to this source code. In place of 5 ** a legal notice, here is a blessing: 6 ** 7 ** May you do good and not evil. 8 ** May you find forgiveness for yourself and forgive others. 9 ** May you share freely, never taking more than you give. 10 ** 11 ************************************************************************* 12 ** This file contains SQLite's grammar for SQL. Process this file 13 ** using the lemon parser generator to generate C code that runs 14 ** the parser. Lemon will also generate a header file containing 15 ** numeric codes for all of the tokens. 16 */ 17 18 // All token codes are small integers with #defines that begin with "TK_" 19 %token_prefix TK_ 20 21 // The type of the data attached to each token is Token. This is also the 22 // default type for non-terminals. 23 // 24 %token_type {Token} 25 %default_type {Token} 26 27 // The generated parser function takes a 4th argument as follows: 28 %extra_argument {Parse *pParse} 29 30 // This code runs whenever there is a syntax error 31 // 32 %syntax_error { 33 UNUSED_PARAMETER(yymajor); /* Silence some compiler warnings */ 34 assert( TOKEN.z[0] ); /* The tokenizer always gives us a token */ 35 sqlite3ErrorMsg(pParse, "near \"%T\": syntax error", &TOKEN); 36 } 37 %stack_overflow { 38 sqlite3ErrorMsg(pParse, "parser stack overflow"); 39 } 40 41 // The name of the generated procedure that implements the parser 42 // is as follows: 43 %name sqlite3Parser 44 45 // The following text is included near the beginning of the C source 46 // code file that implements the parser. 47 // 48 %include { 49 #include "sqliteInt.h" 50 51 /* 52 ** Disable all error recovery processing in the parser push-down 53 ** automaton. 54 */ 55 #define YYNOERRORRECOVERY 1 56 57 /* 58 ** Make yytestcase() the same as testcase() 59 */ 60 #define yytestcase(X) testcase(X) 61 62 /* 63 ** Indicate that sqlite3ParserFree() will never be called with a null 64 ** pointer. 65 */ 66 #define YYPARSEFREENEVERNULL 1 67 68 /* 69 ** Alternative datatype for the argument to the malloc() routine passed 70 ** into sqlite3ParserAlloc(). The default is size_t. 71 */ 72 #define YYMALLOCARGTYPE u64 73 74 /* 75 ** An instance of this structure holds information about the 76 ** LIMIT clause of a SELECT statement. 77 */ 78 struct LimitVal { 79 Expr *pLimit; /* The LIMIT expression. NULL if there is no limit */ 80 Expr *pOffset; /* The OFFSET expression. NULL if there is none */ 81 }; 82 83 /* 84 ** An instance of this structure is used to store the LIKE, 85 ** GLOB, NOT LIKE, and NOT GLOB operators. 86 */ 87 struct LikeOp { 88 Token eOperator; /* "like" or "glob" or "regexp" */ 89 int bNot; /* True if the NOT keyword is present */ 90 }; 91 92 /* 93 ** An instance of the following structure describes the event of a 94 ** TRIGGER. "a" is the event type, one of TK_UPDATE, TK_INSERT, 95 ** TK_DELETE, or TK_INSTEAD. If the event is of the form 96 ** 97 ** UPDATE ON (a,b,c) 98 ** 99 ** Then the "b" IdList records the list "a,b,c". 100 */ 101 struct TrigEvent { int a; IdList * b; }; 102 103 /* 104 ** An instance of this structure holds the ATTACH key and the key type. 105 */ 106 struct AttachKey { int type; Token key; }; 107 108 /* 109 ** Disable lookaside memory allocation for objects that might be 110 ** shared across database connections. 111 */ 112 static void disableLookaside(Parse *pParse){ 113 pParse->disableLookaside++; 114 pParse->db->lookaside.bDisable++; 115 } 116 117 } // end %include 118 119 // Input is a single SQL command 120 input ::= cmdlist. 121 cmdlist ::= cmdlist ecmd. 122 cmdlist ::= ecmd. 123 ecmd ::= SEMI. 124 ecmd ::= explain cmdx SEMI. 125 explain ::= . 126 %ifndef SQLITE_OMIT_EXPLAIN 127 explain ::= EXPLAIN. { pParse->explain = 1; } 128 explain ::= EXPLAIN QUERY PLAN. { pParse->explain = 2; } 129 %endif SQLITE_OMIT_EXPLAIN 130 cmdx ::= cmd. { sqlite3FinishCoding(pParse); } 131 132 ///////////////////// Begin and end transactions. //////////////////////////// 133 // 134 135 cmd ::= BEGIN transtype(Y) trans_opt. {sqlite3BeginTransaction(pParse, Y);} 136 trans_opt ::= . 137 trans_opt ::= TRANSACTION. 138 trans_opt ::= TRANSACTION nm. 139 %type transtype {int} 140 transtype(A) ::= . {A = TK_DEFERRED;} 141 transtype(A) ::= DEFERRED(X). {A = @X; /*A-overwrites-X*/} 142 transtype(A) ::= IMMEDIATE(X). {A = @X; /*A-overwrites-X*/} 143 transtype(A) ::= EXCLUSIVE(X). {A = @X; /*A-overwrites-X*/} 144 cmd ::= COMMIT trans_opt. {sqlite3CommitTransaction(pParse);} 145 cmd ::= END trans_opt. {sqlite3CommitTransaction(pParse);} 146 cmd ::= ROLLBACK trans_opt. {sqlite3RollbackTransaction(pParse);} 147 148 savepoint_opt ::= SAVEPOINT. 149 savepoint_opt ::= . 150 cmd ::= SAVEPOINT nm(X). { 151 sqlite3Savepoint(pParse, SAVEPOINT_BEGIN, &X); 152 } 153 cmd ::= RELEASE savepoint_opt nm(X). { 154 sqlite3Savepoint(pParse, SAVEPOINT_RELEASE, &X); 155 } 156 cmd ::= ROLLBACK trans_opt TO savepoint_opt nm(X). { 157 sqlite3Savepoint(pParse, SAVEPOINT_ROLLBACK, &X); 158 } 159 160 ///////////////////// The CREATE TABLE statement //////////////////////////// 161 // 162 cmd ::= create_table create_table_args. 163 create_table ::= createkw temp(T) TABLE ifnotexists(E) nm(Y) dbnm(Z). { 164 sqlite3StartTable(pParse,&Y,&Z,T,0,0,E); 165 } 166 createkw(A) ::= CREATE(A). {disableLookaside(pParse);} 167 168 %type ifnotexists {int} 169 ifnotexists(A) ::= . {A = 0;} 170 ifnotexists(A) ::= IF NOT EXISTS. {A = 1;} 171 %type temp {int} 172 %ifndef SQLITE_OMIT_TEMPDB 173 temp(A) ::= TEMP. {A = 1;} 174 %endif SQLITE_OMIT_TEMPDB 175 temp(A) ::= . {A = 0;} 176 create_table_args ::= LP columnlist conslist_opt(X) RP(E) table_options(F). { 177 sqlite3EndTable(pParse,&X,&E,F,0); 178 } 179 create_table_args ::= AS select(S). { 180 sqlite3EndTable(pParse,0,0,0,S); 181 sqlite3SelectDelete(pParse->db, S); 182 } 183 %type table_options {int} 184 table_options(A) ::= . {A = 0;} 185 table_options(A) ::= WITHOUT nm(X). { 186 if( X.n==5 && sqlite3_strnicmp(X.z,"rowid",5)==0 ){ 187 A = TF_WithoutRowid | TF_NoVisibleRowid; 188 }else{ 189 A = 0; 190 sqlite3ErrorMsg(pParse, "unknown table option: %.*s", X.n, X.z); 191 } 192 } 193 columnlist ::= columnlist COMMA column. 194 columnlist ::= column. 195 196 // A "column" is a complete description of a single column in a 197 // CREATE TABLE statement. This includes the column name, its 198 // datatype, and other keywords such as PRIMARY KEY, UNIQUE, REFERENCES, 199 // NOT NULL and so forth. 200 // 201 column(A) ::= columnid(A) type carglist. { 202 A.n = (int)(pParse->sLastToken.z-A.z) + pParse->sLastToken.n; 203 } 204 columnid(A) ::= nm(A). { 205 sqlite3AddColumn(pParse,&A); 206 pParse->constraintName.n = 0; 207 } 208 209 210 // An IDENTIFIER can be a generic identifier, or one of several 211 // keywords. Any non-standard keyword can also be an identifier. 212 // 213 %token_class id ID|INDEXED. 214 215 // The following directive causes tokens ABORT, AFTER, ASC, etc. to 216 // fallback to ID if they will not parse as their original value. 217 // This obviates the need for the "id" nonterminal. 218 // 219 %fallback ID 220 ABORT ACTION AFTER ANALYZE ASC ATTACH BEFORE BEGIN BY CASCADE CAST COLUMNKW 221 CONFLICT DATABASE DEFERRED DESC DETACH EACH END EXCLUSIVE EXPLAIN FAIL FOR 222 IGNORE IMMEDIATE INITIALLY INSTEAD LIKE_KW MATCH NO PLAN 223 QUERY KEY OF OFFSET PRAGMA RAISE RECURSIVE RELEASE REPLACE RESTRICT ROW 224 ROLLBACK SAVEPOINT TEMP TRIGGER VACUUM VIEW VIRTUAL WITH WITHOUT 225 %ifdef SQLITE_OMIT_COMPOUND_SELECT 226 EXCEPT INTERSECT UNION 227 %endif SQLITE_OMIT_COMPOUND_SELECT 228 REINDEX RENAME CTIME_KW IF 229 . 230 %wildcard ANY. 231 232 // Define operator precedence early so that this is the first occurrence 233 // of the operator tokens in the grammer. Keeping the operators together 234 // causes them to be assigned integer values that are close together, 235 // which keeps parser tables smaller. 236 // 237 // The token values assigned to these symbols is determined by the order 238 // in which lemon first sees them. It must be the case that ISNULL/NOTNULL, 239 // NE/EQ, GT/LE, and GE/LT are separated by only a single value. See 240 // the sqlite3ExprIfFalse() routine for additional information on this 241 // constraint. 242 // 243 %left OR. 244 %left AND. 245 %right NOT. 246 %left IS MATCH LIKE_KW BETWEEN IN ISNULL NOTNULL NE EQ. 247 %left GT LE LT GE. 248 %right ESCAPE. 249 %left BITAND BITOR LSHIFT RSHIFT. 250 %left PLUS MINUS. 251 %left STAR SLASH REM. 252 %left CONCAT. 253 %left COLLATE. 254 %right BITNOT. 255 256 // And "ids" is an identifer-or-string. 257 // 258 %token_class ids ID|STRING. 259 260 // The name of a column or table can be any of the following: 261 // 262 %type nm {Token} 263 nm(A) ::= id(A). 264 nm(A) ::= STRING(A). 265 nm(A) ::= JOIN_KW(A). 266 267 // A typetoken is really one or more tokens that form a type name such 268 // as can be found after the column name in a CREATE TABLE statement. 269 // Multiple tokens are concatenated to form the value of the typetoken. 270 // 271 %type typetoken {Token} 272 type ::= . 273 type ::= typetoken(X). {sqlite3AddColumnType(pParse,&X);} 274 typetoken(A) ::= typename(A). 275 typetoken(A) ::= typename(A) LP signed RP(Y). { 276 A.n = (int)(&Y.z[Y.n] - A.z); 277 } 278 typetoken(A) ::= typename(A) LP signed COMMA signed RP(Y). { 279 A.n = (int)(&Y.z[Y.n] - A.z); 280 } 281 %type typename {Token} 282 typename(A) ::= ids(A). 283 typename(A) ::= typename(A) ids(Y). {A.n=Y.n+(int)(Y.z-A.z);} 284 signed ::= plus_num. 285 signed ::= minus_num. 286 287 // "carglist" is a list of additional constraints that come after the 288 // column name and column type in a CREATE TABLE statement. 289 // 290 carglist ::= carglist ccons. 291 carglist ::= . 292 ccons ::= CONSTRAINT nm(X). {pParse->constraintName = X;} 293 ccons ::= DEFAULT term(X). {sqlite3AddDefaultValue(pParse,&X);} 294 ccons ::= DEFAULT LP expr(X) RP. {sqlite3AddDefaultValue(pParse,&X);} 295 ccons ::= DEFAULT PLUS term(X). {sqlite3AddDefaultValue(pParse,&X);} 296 ccons ::= DEFAULT MINUS(A) term(X). { 297 ExprSpan v; 298 v.pExpr = sqlite3PExpr(pParse, TK_UMINUS, X.pExpr, 0, 0); 299 v.zStart = A.z; 300 v.zEnd = X.zEnd; 301 sqlite3AddDefaultValue(pParse,&v); 302 } 303 ccons ::= DEFAULT id(X). { 304 ExprSpan v; 305 spanExpr(&v, pParse, TK_STRING, X); 306 sqlite3AddDefaultValue(pParse,&v); 307 } 308 309 // In addition to the type name, we also care about the primary key and 310 // UNIQUE constraints. 311 // 312 ccons ::= NULL onconf. 313 ccons ::= NOT NULL onconf(R). {sqlite3AddNotNull(pParse, R);} 314 ccons ::= PRIMARY KEY sortorder(Z) onconf(R) autoinc(I). 315 {sqlite3AddPrimaryKey(pParse,0,R,I,Z);} 316 ccons ::= UNIQUE onconf(R). {sqlite3CreateIndex(pParse,0,0,0,0,R,0,0,0,0);} 317 ccons ::= CHECK LP expr(X) RP. {sqlite3AddCheckConstraint(pParse,X.pExpr);} 318 ccons ::= REFERENCES nm(T) eidlist_opt(TA) refargs(R). 319 {sqlite3CreateForeignKey(pParse,0,&T,TA,R);} 320 ccons ::= defer_subclause(D). {sqlite3DeferForeignKey(pParse,D);} 321 ccons ::= COLLATE ids(C). {sqlite3AddCollateType(pParse, &C);} 322 323 // The optional AUTOINCREMENT keyword 324 %type autoinc {int} 325 autoinc(X) ::= . {X = 0;} 326 autoinc(X) ::= AUTOINCR. {X = 1;} 327 328 // The next group of rules parses the arguments to a REFERENCES clause 329 // that determine if the referential integrity checking is deferred or 330 // or immediate and which determine what action to take if a ref-integ 331 // check fails. 332 // 333 %type refargs {int} 334 refargs(A) ::= . { A = OE_None*0x0101; /* EV: R-19803-45884 */} 335 refargs(A) ::= refargs(A) refarg(Y). { A = (A & ~Y.mask) | Y.value; } 336 %type refarg {struct {int value; int mask;}} 337 refarg(A) ::= MATCH nm. { A.value = 0; A.mask = 0x000000; } 338 refarg(A) ::= ON INSERT refact. { A.value = 0; A.mask = 0x000000; } 339 refarg(A) ::= ON DELETE refact(X). { A.value = X; A.mask = 0x0000ff; } 340 refarg(A) ::= ON UPDATE refact(X). { A.value = X<<8; A.mask = 0x00ff00; } 341 %type refact {int} 342 refact(A) ::= SET NULL. { A = OE_SetNull; /* EV: R-33326-45252 */} 343 refact(A) ::= SET DEFAULT. { A = OE_SetDflt; /* EV: R-33326-45252 */} 344 refact(A) ::= CASCADE. { A = OE_Cascade; /* EV: R-33326-45252 */} 345 refact(A) ::= RESTRICT. { A = OE_Restrict; /* EV: R-33326-45252 */} 346 refact(A) ::= NO ACTION. { A = OE_None; /* EV: R-33326-45252 */} 347 %type defer_subclause {int} 348 defer_subclause(A) ::= NOT DEFERRABLE init_deferred_pred_opt. {A = 0;} 349 defer_subclause(A) ::= DEFERRABLE init_deferred_pred_opt(X). {A = X;} 350 %type init_deferred_pred_opt {int} 351 init_deferred_pred_opt(A) ::= . {A = 0;} 352 init_deferred_pred_opt(A) ::= INITIALLY DEFERRED. {A = 1;} 353 init_deferred_pred_opt(A) ::= INITIALLY IMMEDIATE. {A = 0;} 354 355 conslist_opt(A) ::= . {A.n = 0; A.z = 0;} 356 conslist_opt(A) ::= COMMA(A) conslist. 357 conslist ::= conslist tconscomma tcons. 358 conslist ::= tcons. 359 tconscomma ::= COMMA. {pParse->constraintName.n = 0;} 360 tconscomma ::= . 361 tcons ::= CONSTRAINT nm(X). {pParse->constraintName = X;} 362 tcons ::= PRIMARY KEY LP sortlist(X) autoinc(I) RP onconf(R). 363 {sqlite3AddPrimaryKey(pParse,X,R,I,0);} 364 tcons ::= UNIQUE LP sortlist(X) RP onconf(R). 365 {sqlite3CreateIndex(pParse,0,0,0,X,R,0,0,0,0);} 366 tcons ::= CHECK LP expr(E) RP onconf. 367 {sqlite3AddCheckConstraint(pParse,E.pExpr);} 368 tcons ::= FOREIGN KEY LP eidlist(FA) RP 369 REFERENCES nm(T) eidlist_opt(TA) refargs(R) defer_subclause_opt(D). { 370 sqlite3CreateForeignKey(pParse, FA, &T, TA, R); 371 sqlite3DeferForeignKey(pParse, D); 372 } 373 %type defer_subclause_opt {int} 374 defer_subclause_opt(A) ::= . {A = 0;} 375 defer_subclause_opt(A) ::= defer_subclause(A). 376 377 // The following is a non-standard extension that allows us to declare the 378 // default behavior when there is a constraint conflict. 379 // 380 %type onconf {int} 381 %type orconf {int} 382 %type resolvetype {int} 383 onconf(A) ::= . {A = OE_Default;} 384 onconf(A) ::= ON CONFLICT resolvetype(X). {A = X;} 385 orconf(A) ::= . {A = OE_Default;} 386 orconf(A) ::= OR resolvetype(X). {A = X;} 387 resolvetype(A) ::= raisetype(A). 388 resolvetype(A) ::= IGNORE. {A = OE_Ignore;} 389 resolvetype(A) ::= REPLACE. {A = OE_Replace;} 390 391 ////////////////////////// The DROP TABLE ///////////////////////////////////// 392 // 393 cmd ::= DROP TABLE ifexists(E) fullname(X). { 394 sqlite3DropTable(pParse, X, 0, E); 395 } 396 %type ifexists {int} 397 ifexists(A) ::= IF EXISTS. {A = 1;} 398 ifexists(A) ::= . {A = 0;} 399 400 ///////////////////// The CREATE VIEW statement ///////////////////////////// 401 // 402 %ifndef SQLITE_OMIT_VIEW 403 cmd ::= createkw(X) temp(T) VIEW ifnotexists(E) nm(Y) dbnm(Z) eidlist_opt(C) 404 AS select(S). { 405 sqlite3CreateView(pParse, &X, &Y, &Z, C, S, T, E); 406 } 407 cmd ::= DROP VIEW ifexists(E) fullname(X). { 408 sqlite3DropTable(pParse, X, 1, E); 409 } 410 %endif SQLITE_OMIT_VIEW 411 412 //////////////////////// The SELECT statement ///////////////////////////////// 413 // 414 cmd ::= select(X). { 415 SelectDest dest = {SRT_Output, 0, 0, 0, 0, 0}; 416 sqlite3Select(pParse, X, &dest); 417 sqlite3SelectDelete(pParse->db, X); 418 } 419 420 %type select {Select*} 421 %destructor select {sqlite3SelectDelete(pParse->db, $$);} 422 %type selectnowith {Select*} 423 %destructor selectnowith {sqlite3SelectDelete(pParse->db, $$);} 424 %type oneselect {Select*} 425 %destructor oneselect {sqlite3SelectDelete(pParse->db, $$);} 426 427 %include { 428 /* 429 ** For a compound SELECT statement, make sure p->pPrior->pNext==p for 430 ** all elements in the list. And make sure list length does not exceed 431 ** SQLITE_LIMIT_COMPOUND_SELECT. 432 */ 433 static void parserDoubleLinkSelect(Parse *pParse, Select *p){ 434 if( p->pPrior ){ 435 Select *pNext = 0, *pLoop; 436 int mxSelect, cnt = 0; 437 for(pLoop=p; pLoop; pNext=pLoop, pLoop=pLoop->pPrior, cnt++){ 438 pLoop->pNext = pNext; 439 pLoop->selFlags |= SF_Compound; 440 } 441 if( (p->selFlags & SF_MultiValue)==0 && 442 (mxSelect = pParse->db->aLimit[SQLITE_LIMIT_COMPOUND_SELECT])>0 && 443 cnt>mxSelect 444 ){ 445 sqlite3ErrorMsg(pParse, "too many terms in compound SELECT"); 446 } 447 } 448 } 449 } 450 451 select(A) ::= with(W) selectnowith(X). { 452 Select *p = X; 453 if( p ){ 454 p->pWith = W; 455 parserDoubleLinkSelect(pParse, p); 456 }else{ 457 sqlite3WithDelete(pParse->db, W); 458 } 459 A = p; /*A-overwrites-W*/ 460 } 461 462 selectnowith(A) ::= oneselect(A). 463 %ifndef SQLITE_OMIT_COMPOUND_SELECT 464 selectnowith(A) ::= selectnowith(A) multiselect_op(Y) oneselect(Z). { 465 Select *pRhs = Z; 466 Select *pLhs = A; 467 if( pRhs && pRhs->pPrior ){ 468 SrcList *pFrom; 469 Token x; 470 x.n = 0; 471 parserDoubleLinkSelect(pParse, pRhs); 472 pFrom = sqlite3SrcListAppendFromTerm(pParse,0,0,0,&x,pRhs,0,0); 473 pRhs = sqlite3SelectNew(pParse,0,pFrom,0,0,0,0,0,0,0); 474 } 475 if( pRhs ){ 476 pRhs->op = (u8)Y; 477 pRhs->pPrior = pLhs; 478 if( ALWAYS(pLhs) ) pLhs->selFlags &= ~SF_MultiValue; 479 pRhs->selFlags &= ~SF_MultiValue; 480 if( Y!=TK_ALL ) pParse->hasCompound = 1; 481 }else{ 482 sqlite3SelectDelete(pParse->db, pLhs); 483 } 484 A = pRhs; 485 } 486 %type multiselect_op {int} 487 multiselect_op(A) ::= UNION(OP). {A = @OP; /*A-overwrites-OP*/} 488 multiselect_op(A) ::= UNION ALL. {A = TK_ALL;} 489 multiselect_op(A) ::= EXCEPT|INTERSECT(OP). {A = @OP; /*A-overwrites-OP*/} 490 %endif SQLITE_OMIT_COMPOUND_SELECT 491 oneselect(A) ::= SELECT(S) distinct(D) selcollist(W) from(X) where_opt(Y) 492 groupby_opt(P) having_opt(Q) orderby_opt(Z) limit_opt(L). { 493 #if SELECTTRACE_ENABLED 494 Token s = S; /*A-overwrites-S*/ 495 #endif 496 A = sqlite3SelectNew(pParse,W,X,Y,P,Q,Z,D,L.pLimit,L.pOffset); 497 #if SELECTTRACE_ENABLED 498 /* Populate the Select.zSelName[] string that is used to help with 499 ** query planner debugging, to differentiate between multiple Select 500 ** objects in a complex query. 501 ** 502 ** If the SELECT keyword is immediately followed by a C-style comment 503 ** then extract the first few alphanumeric characters from within that 504 ** comment to be the zSelName value. Otherwise, the label is #N where 505 ** is an integer that is incremented with each SELECT statement seen. 506 */ 507 if( A!=0 ){ 508 const char *z = s.z+6; 509 int i; 510 sqlite3_snprintf(sizeof(A->zSelName), A->zSelName, "#%d", 511 ++pParse->nSelect); 512 while( z[0]==' ' ) z++; 513 if( z[0]=='/' && z[1]=='*' ){ 514 z += 2; 515 while( z[0]==' ' ) z++; 516 for(i=0; sqlite3Isalnum(z[i]); i++){} 517 sqlite3_snprintf(sizeof(A->zSelName), A->zSelName, "%.*s", i, z); 518 } 519 } 520 #endif /* SELECTRACE_ENABLED */ 521 } 522 oneselect(A) ::= values(A). 523 524 %type values {Select*} 525 %destructor values {sqlite3SelectDelete(pParse->db, $$);} 526 values(A) ::= VALUES LP nexprlist(X) RP. { 527 A = sqlite3SelectNew(pParse,X,0,0,0,0,0,SF_Values,0,0); 528 } 529 values(A) ::= values(A) COMMA LP exprlist(Y) RP. { 530 Select *pRight, *pLeft = A; 531 pRight = sqlite3SelectNew(pParse,Y,0,0,0,0,0,SF_Values|SF_MultiValue,0,0); 532 if( ALWAYS(pLeft) ) pLeft->selFlags &= ~SF_MultiValue; 533 if( pRight ){ 534 pRight->op = TK_ALL; 535 pRight->pPrior = pLeft; 536 A = pRight; 537 }else{ 538 A = pLeft; 539 } 540 } 541 542 // The "distinct" nonterminal is true (1) if the DISTINCT keyword is 543 // present and false (0) if it is not. 544 // 545 %type distinct {int} 546 distinct(A) ::= DISTINCT. {A = SF_Distinct;} 547 distinct(A) ::= ALL. {A = SF_All;} 548 distinct(A) ::= . {A = 0;} 549 550 // selcollist is a list of expressions that are to become the return 551 // values of the SELECT statement. The "*" in statements like 552 // "SELECT * FROM ..." is encoded as a special expression with an 553 // opcode of TK_ASTERISK. 554 // 555 %type selcollist {ExprList*} 556 %destructor selcollist {sqlite3ExprListDelete(pParse->db, $$);} 557 %type sclp {ExprList*} 558 %destructor sclp {sqlite3ExprListDelete(pParse->db, $$);} 559 sclp(A) ::= selcollist(A) COMMA. 560 sclp(A) ::= . {A = 0;} 561 selcollist(A) ::= sclp(A) expr(X) as(Y). { 562 A = sqlite3ExprListAppend(pParse, A, X.pExpr); 563 if( Y.n>0 ) sqlite3ExprListSetName(pParse, A, &Y, 1); 564 sqlite3ExprListSetSpan(pParse,A,&X); 565 } 566 selcollist(A) ::= sclp(A) STAR. { 567 Expr *p = sqlite3Expr(pParse->db, TK_ASTERISK, 0); 568 A = sqlite3ExprListAppend(pParse, A, p); 569 } 570 selcollist(A) ::= sclp(A) nm(X) DOT STAR(Y). { 571 Expr *pRight = sqlite3PExpr(pParse, TK_ASTERISK, 0, 0, &Y); 572 Expr *pLeft = sqlite3PExpr(pParse, TK_ID, 0, 0, &X); 573 Expr *pDot = sqlite3PExpr(pParse, TK_DOT, pLeft, pRight, 0); 574 A = sqlite3ExprListAppend(pParse,A, pDot); 575 } 576 577 // An option "AS <id>" phrase that can follow one of the expressions that 578 // define the result set, or one of the tables in the FROM clause. 579 // 580 %type as {Token} 581 as(X) ::= AS nm(Y). {X = Y;} 582 as(X) ::= ids(X). 583 as(X) ::= . {X.n = 0;} 584 585 586 %type seltablist {SrcList*} 587 %destructor seltablist {sqlite3SrcListDelete(pParse->db, $$);} 588 %type stl_prefix {SrcList*} 589 %destructor stl_prefix {sqlite3SrcListDelete(pParse->db, $$);} 590 %type from {SrcList*} 591 %destructor from {sqlite3SrcListDelete(pParse->db, $$);} 592 593 // A complete FROM clause. 594 // 595 from(A) ::= . {A = sqlite3DbMallocZero(pParse->db, sizeof(*A));} 596 from(A) ::= FROM seltablist(X). { 597 A = X; 598 sqlite3SrcListShiftJoinType(A); 599 } 600 601 // "seltablist" is a "Select Table List" - the content of the FROM clause 602 // in a SELECT statement. "stl_prefix" is a prefix of this list. 603 // 604 stl_prefix(A) ::= seltablist(A) joinop(Y). { 605 if( ALWAYS(A && A->nSrc>0) ) A->a[A->nSrc-1].fg.jointype = (u8)Y; 606 } 607 stl_prefix(A) ::= . {A = 0;} 608 seltablist(A) ::= stl_prefix(A) nm(Y) dbnm(D) as(Z) indexed_opt(I) 609 on_opt(N) using_opt(U). { 610 A = sqlite3SrcListAppendFromTerm(pParse,A,&Y,&D,&Z,0,N,U); 611 sqlite3SrcListIndexedBy(pParse, A, &I); 612 } 613 seltablist(A) ::= stl_prefix(A) nm(Y) dbnm(D) LP exprlist(E) RP as(Z) 614 on_opt(N) using_opt(U). { 615 A = sqlite3SrcListAppendFromTerm(pParse,A,&Y,&D,&Z,0,N,U); 616 sqlite3SrcListFuncArgs(pParse, A, E); 617 } 618 %ifndef SQLITE_OMIT_SUBQUERY 619 seltablist(A) ::= stl_prefix(A) LP select(S) RP 620 as(Z) on_opt(N) using_opt(U). { 621 A = sqlite3SrcListAppendFromTerm(pParse,A,0,0,&Z,S,N,U); 622 } 623 seltablist(A) ::= stl_prefix(A) LP seltablist(F) RP 624 as(Z) on_opt(N) using_opt(U). { 625 if( A==0 && Z.n==0 && N==0 && U==0 ){ 626 A = F; 627 }else if( F->nSrc==1 ){ 628 A = sqlite3SrcListAppendFromTerm(pParse,A,0,0,&Z,0,N,U); 629 if( A ){ 630 struct SrcList_item *pNew = &A->a[A->nSrc-1]; 631 struct SrcList_item *pOld = F->a; 632 pNew->zName = pOld->zName; 633 pNew->zDatabase = pOld->zDatabase; 634 pNew->pSelect = pOld->pSelect; 635 pOld->zName = pOld->zDatabase = 0; 636 pOld->pSelect = 0; 637 } 638 sqlite3SrcListDelete(pParse->db, F); 639 }else{ 640 Select *pSubquery; 641 sqlite3SrcListShiftJoinType(F); 642 pSubquery = sqlite3SelectNew(pParse,0,F,0,0,0,0,SF_NestedFrom,0,0); 643 A = sqlite3SrcListAppendFromTerm(pParse,A,0,0,&Z,pSubquery,N,U); 644 } 645 } 646 %endif SQLITE_OMIT_SUBQUERY 647 648 %type dbnm {Token} 649 dbnm(A) ::= . {A.z=0; A.n=0;} 650 dbnm(A) ::= DOT nm(X). {A = X;} 651 652 %type fullname {SrcList*} 653 %destructor fullname {sqlite3SrcListDelete(pParse->db, $$);} 654 fullname(A) ::= nm(X) dbnm(Y). 655 {A = sqlite3SrcListAppend(pParse->db,0,&X,&Y); /*A-overwrites-X*/} 656 657 %type joinop {int} 658 joinop(X) ::= COMMA|JOIN. { X = JT_INNER; } 659 joinop(X) ::= JOIN_KW(A) JOIN. 660 {X = sqlite3JoinType(pParse,&A,0,0); /*X-overwrites-A*/} 661 joinop(X) ::= JOIN_KW(A) nm(B) JOIN. 662 {X = sqlite3JoinType(pParse,&A,&B,0); /*X-overwrites-A*/} 663 joinop(X) ::= JOIN_KW(A) nm(B) nm(C) JOIN. 664 {X = sqlite3JoinType(pParse,&A,&B,&C);/*X-overwrites-A*/} 665 666 %type on_opt {Expr*} 667 %destructor on_opt {sqlite3ExprDelete(pParse->db, $$);} 668 on_opt(N) ::= ON expr(E). {N = E.pExpr;} 669 on_opt(N) ::= . {N = 0;} 670 671 // Note that this block abuses the Token type just a little. If there is 672 // no "INDEXED BY" clause, the returned token is empty (z==0 && n==0). If 673 // there is an INDEXED BY clause, then the token is populated as per normal, 674 // with z pointing to the token data and n containing the number of bytes 675 // in the token. 676 // 677 // If there is a "NOT INDEXED" clause, then (z==0 && n==1), which is 678 // normally illegal. The sqlite3SrcListIndexedBy() function 679 // recognizes and interprets this as a special case. 680 // 681 %type indexed_opt {Token} 682 indexed_opt(A) ::= . {A.z=0; A.n=0;} 683 indexed_opt(A) ::= INDEXED BY nm(X). {A = X;} 684 indexed_opt(A) ::= NOT INDEXED. {A.z=0; A.n=1;} 685 686 %type using_opt {IdList*} 687 %destructor using_opt {sqlite3IdListDelete(pParse->db, $$);} 688 using_opt(U) ::= USING LP idlist(L) RP. {U = L;} 689 using_opt(U) ::= . {U = 0;} 690 691 692 %type orderby_opt {ExprList*} 693 %destructor orderby_opt {sqlite3ExprListDelete(pParse->db, $$);} 694 695 // the sortlist non-terminal stores a list of expression where each 696 // expression is optionally followed by ASC or DESC to indicate the 697 // sort order. 698 // 699 %type sortlist {ExprList*} 700 %destructor sortlist {sqlite3ExprListDelete(pParse->db, $$);} 701 702 orderby_opt(A) ::= . {A = 0;} 703 orderby_opt(A) ::= ORDER BY sortlist(X). {A = X;} 704 sortlist(A) ::= sortlist(A) COMMA expr(Y) sortorder(Z). { 705 A = sqlite3ExprListAppend(pParse,A,Y.pExpr); 706 sqlite3ExprListSetSortOrder(A,Z); 707 } 708 sortlist(A) ::= expr(Y) sortorder(Z). { 709 A = sqlite3ExprListAppend(pParse,0,Y.pExpr); /*A-overwrites-Y*/ 710 sqlite3ExprListSetSortOrder(A,Z); 711 } 712 713 %type sortorder {int} 714 715 sortorder(A) ::= ASC. {A = SQLITE_SO_ASC;} 716 sortorder(A) ::= DESC. {A = SQLITE_SO_DESC;} 717 sortorder(A) ::= . {A = SQLITE_SO_UNDEFINED;} 718 719 %type groupby_opt {ExprList*} 720 %destructor groupby_opt {sqlite3ExprListDelete(pParse->db, $$);} 721 groupby_opt(A) ::= . {A = 0;} 722 groupby_opt(A) ::= GROUP BY nexprlist(X). {A = X;} 723 724 %type having_opt {Expr*} 725 %destructor having_opt {sqlite3ExprDelete(pParse->db, $$);} 726 having_opt(A) ::= . {A = 0;} 727 having_opt(A) ::= HAVING expr(X). {A = X.pExpr;} 728 729 %type limit_opt {struct LimitVal} 730 731 // The destructor for limit_opt will never fire in the current grammar. 732 // The limit_opt non-terminal only occurs at the end of a single production 733 // rule for SELECT statements. As soon as the rule that create the 734 // limit_opt non-terminal reduces, the SELECT statement rule will also 735 // reduce. So there is never a limit_opt non-terminal on the stack 736 // except as a transient. So there is never anything to destroy. 737 // 738 //%destructor limit_opt { 739 // sqlite3ExprDelete(pParse->db, $$.pLimit); 740 // sqlite3ExprDelete(pParse->db, $$.pOffset); 741 //} 742 limit_opt(A) ::= . {A.pLimit = 0; A.pOffset = 0;} 743 limit_opt(A) ::= LIMIT expr(X). {A.pLimit = X.pExpr; A.pOffset = 0;} 744 limit_opt(A) ::= LIMIT expr(X) OFFSET expr(Y). 745 {A.pLimit = X.pExpr; A.pOffset = Y.pExpr;} 746 limit_opt(A) ::= LIMIT expr(X) COMMA expr(Y). 747 {A.pOffset = X.pExpr; A.pLimit = Y.pExpr;} 748 749 /////////////////////////// The DELETE statement ///////////////////////////// 750 // 751 %ifdef SQLITE_ENABLE_UPDATE_DELETE_LIMIT 752 cmd ::= with(C) DELETE FROM fullname(X) indexed_opt(I) where_opt(W) 753 orderby_opt(O) limit_opt(L). { 754 sqlite3WithPush(pParse, C, 1); 755 sqlite3SrcListIndexedBy(pParse, X, &I); 756 W = sqlite3LimitWhere(pParse, X, W, O, L.pLimit, L.pOffset, "DELETE"); 757 sqlite3DeleteFrom(pParse,X,W); 758 } 759 %endif 760 %ifndef SQLITE_ENABLE_UPDATE_DELETE_LIMIT 761 cmd ::= with(C) DELETE FROM fullname(X) indexed_opt(I) where_opt(W). { 762 sqlite3WithPush(pParse, C, 1); 763 sqlite3SrcListIndexedBy(pParse, X, &I); 764 sqlite3DeleteFrom(pParse,X,W); 765 } 766 %endif 767 768 %type where_opt {Expr*} 769 %destructor where_opt {sqlite3ExprDelete(pParse->db, $$);} 770 771 where_opt(A) ::= . {A = 0;} 772 where_opt(A) ::= WHERE expr(X). {A = X.pExpr;} 773 774 ////////////////////////// The UPDATE command //////////////////////////////// 775 // 776 %ifdef SQLITE_ENABLE_UPDATE_DELETE_LIMIT 777 cmd ::= with(C) UPDATE orconf(R) fullname(X) indexed_opt(I) SET setlist(Y) 778 where_opt(W) orderby_opt(O) limit_opt(L). { 779 sqlite3WithPush(pParse, C, 1); 780 sqlite3SrcListIndexedBy(pParse, X, &I); 781 sqlite3ExprListCheckLength(pParse,Y,"set list"); 782 W = sqlite3LimitWhere(pParse, X, W, O, L.pLimit, L.pOffset, "UPDATE"); 783 sqlite3Update(pParse,X,Y,W,R); 784 } 785 %endif 786 %ifndef SQLITE_ENABLE_UPDATE_DELETE_LIMIT 787 cmd ::= with(C) UPDATE orconf(R) fullname(X) indexed_opt(I) SET setlist(Y) 788 where_opt(W). { 789 sqlite3WithPush(pParse, C, 1); 790 sqlite3SrcListIndexedBy(pParse, X, &I); 791 sqlite3ExprListCheckLength(pParse,Y,"set list"); 792 sqlite3Update(pParse,X,Y,W,R); 793 } 794 %endif 795 796 %type setlist {ExprList*} 797 %destructor setlist {sqlite3ExprListDelete(pParse->db, $$);} 798 799 setlist(A) ::= setlist(A) COMMA nm(X) EQ expr(Y). { 800 A = sqlite3ExprListAppend(pParse, A, Y.pExpr); 801 sqlite3ExprListSetName(pParse, A, &X, 1); 802 } 803 setlist(A) ::= nm(X) EQ expr(Y). { 804 A = sqlite3ExprListAppend(pParse, 0, Y.pExpr); 805 sqlite3ExprListSetName(pParse, A, &X, 1); 806 } 807 808 ////////////////////////// The INSERT command ///////////////////////////////// 809 // 810 cmd ::= with(W) insert_cmd(R) INTO fullname(X) idlist_opt(F) select(S). { 811 sqlite3WithPush(pParse, W, 1); 812 sqlite3Insert(pParse, X, S, F, R); 813 } 814 cmd ::= with(W) insert_cmd(R) INTO fullname(X) idlist_opt(F) DEFAULT VALUES. 815 { 816 sqlite3WithPush(pParse, W, 1); 817 sqlite3Insert(pParse, X, 0, F, R); 818 } 819 820 %type insert_cmd {int} 821 insert_cmd(A) ::= INSERT orconf(R). {A = R;} 822 insert_cmd(A) ::= REPLACE. {A = OE_Replace;} 823 824 %type idlist_opt {IdList*} 825 %destructor idlist_opt {sqlite3IdListDelete(pParse->db, $$);} 826 %type idlist {IdList*} 827 %destructor idlist {sqlite3IdListDelete(pParse->db, $$);} 828 829 idlist_opt(A) ::= . {A = 0;} 830 idlist_opt(A) ::= LP idlist(X) RP. {A = X;} 831 idlist(A) ::= idlist(A) COMMA nm(Y). 832 {A = sqlite3IdListAppend(pParse->db,A,&Y);} 833 idlist(A) ::= nm(Y). 834 {A = sqlite3IdListAppend(pParse->db,0,&Y); /*A-overwrites-Y*/} 835 836 /////////////////////////// Expression Processing ///////////////////////////// 837 // 838 839 %type expr {ExprSpan} 840 %destructor expr {sqlite3ExprDelete(pParse->db, $$.pExpr);} 841 %type term {ExprSpan} 842 %destructor term {sqlite3ExprDelete(pParse->db, $$.pExpr);} 843 844 %include { 845 /* This is a utility routine used to set the ExprSpan.zStart and 846 ** ExprSpan.zEnd values of pOut so that the span covers the complete 847 ** range of text beginning with pStart and going to the end of pEnd. 848 */ 849 static void spanSet(ExprSpan *pOut, Token *pStart, Token *pEnd){ 850 pOut->zStart = pStart->z; 851 pOut->zEnd = &pEnd->z[pEnd->n]; 852 } 853 854 /* Construct a new Expr object from a single identifier. Use the 855 ** new Expr to populate pOut. Set the span of pOut to be the identifier 856 ** that created the expression. 857 */ 858 static void spanExpr(ExprSpan *pOut, Parse *pParse, int op, Token t){ 859 pOut->pExpr = sqlite3PExpr(pParse, op, 0, 0, &t); 860 pOut->zStart = t.z; 861 pOut->zEnd = &t.z[t.n]; 862 } 863 } 864 865 expr(A) ::= term(A). 866 expr(A) ::= LP(B) expr(X) RP(E). 867 {spanSet(&A,&B,&E); /*A-overwrites-B*/ A.pExpr = X.pExpr;} 868 term(A) ::= NULL(X). {spanExpr(&A,pParse,@X,X);/*A-overwrites-X*/} 869 expr(A) ::= id(X). {spanExpr(&A,pParse,TK_ID,X); /*A-overwrites-X*/} 870 expr(A) ::= JOIN_KW(X). {spanExpr(&A,pParse,TK_ID,X); /*A-overwrites-X*/} 871 expr(A) ::= nm(X) DOT nm(Y). { 872 Expr *temp1 = sqlite3PExpr(pParse, TK_ID, 0, 0, &X); 873 Expr *temp2 = sqlite3PExpr(pParse, TK_ID, 0, 0, &Y); 874 spanSet(&A,&X,&Y); /*A-overwrites-X*/ 875 A.pExpr = sqlite3PExpr(pParse, TK_DOT, temp1, temp2, 0); 876 } 877 expr(A) ::= nm(X) DOT nm(Y) DOT nm(Z). { 878 Expr *temp1 = sqlite3PExpr(pParse, TK_ID, 0, 0, &X); 879 Expr *temp2 = sqlite3PExpr(pParse, TK_ID, 0, 0, &Y); 880 Expr *temp3 = sqlite3PExpr(pParse, TK_ID, 0, 0, &Z); 881 Expr *temp4 = sqlite3PExpr(pParse, TK_DOT, temp2, temp3, 0); 882 spanSet(&A,&X,&Z); /*A-overwrites-X*/ 883 A.pExpr = sqlite3PExpr(pParse, TK_DOT, temp1, temp4, 0); 884 } 885 term(A) ::= INTEGER|FLOAT|BLOB(X). {spanExpr(&A,pParse,@X,X);/*A-overwrites-X*/} 886 term(A) ::= STRING(X). {spanExpr(&A,pParse,@X,X);/*A-overwrites-X*/} 887 expr(A) ::= VARIABLE(X). { 888 Token t = X; /*A-overwrites-X*/ 889 if( t.n>=2 && t.z[0]=='#' && sqlite3Isdigit(t.z[1]) ){ 890 /* When doing a nested parse, one can include terms in an expression 891 ** that look like this: #1 #2 ... These terms refer to registers 892 ** in the virtual machine. #N is the N-th register. */ 893 spanSet(&A, &t, &t); 894 if( pParse->nested==0 ){ 895 sqlite3ErrorMsg(pParse, "near \"%T\": syntax error", &t); 896 A.pExpr = 0; 897 }else{ 898 A.pExpr = sqlite3PExpr(pParse, TK_REGISTER, 0, 0, &t); 899 if( A.pExpr ) sqlite3GetInt32(&t.z[1], &A.pExpr->iTable); 900 } 901 }else{ 902 spanExpr(&A, pParse, TK_VARIABLE, t); 903 sqlite3ExprAssignVarNumber(pParse, A.pExpr); 904 } 905 } 906 expr(A) ::= expr(A) COLLATE ids(C). { 907 A.pExpr = sqlite3ExprAddCollateToken(pParse, A.pExpr, &C, 1); 908 A.zEnd = &C.z[C.n]; 909 } 910 %ifndef SQLITE_OMIT_CAST 911 expr(A) ::= CAST(X) LP expr(E) AS typetoken(T) RP(Y). { 912 spanSet(&A,&X,&Y); /*A-overwrites-X*/ 913 A.pExpr = sqlite3PExpr(pParse, TK_CAST, E.pExpr, 0, &T); 914 } 915 %endif SQLITE_OMIT_CAST 916 expr(A) ::= id(X) LP distinct(D) exprlist(Y) RP(E). { 917 if( Y && Y->nExpr>pParse->db->aLimit[SQLITE_LIMIT_FUNCTION_ARG] ){ 918 sqlite3ErrorMsg(pParse, "too many arguments on function %T", &X); 919 } 920 A.pExpr = sqlite3ExprFunction(pParse, Y, &X); 921 spanSet(&A,&X,&E); 922 if( D==SF_Distinct && A.pExpr ){ 923 A.pExpr->flags |= EP_Distinct; 924 } 925 } 926 expr(A) ::= id(X) LP STAR RP(E). { 927 A.pExpr = sqlite3ExprFunction(pParse, 0, &X); 928 spanSet(&A,&X,&E); 929 } 930 term(A) ::= CTIME_KW(OP). { 931 A.pExpr = sqlite3ExprFunction(pParse, 0, &OP); 932 spanSet(&A, &OP, &OP); 933 } 934 935 %include { 936 /* This routine constructs a binary expression node out of two ExprSpan 937 ** objects and uses the result to populate a new ExprSpan object. 938 */ 939 static void spanBinaryExpr( 940 Parse *pParse, /* The parsing context. Errors accumulate here */ 941 int op, /* The binary operation */ 942 ExprSpan *pLeft, /* The left operand, and output */ 943 ExprSpan *pRight /* The right operand */ 944 ){ 945 pLeft->pExpr = sqlite3PExpr(pParse, op, pLeft->pExpr, pRight->pExpr, 0); 946 pLeft->zEnd = pRight->zEnd; 947 } 948 949 /* If doNot is true, then add a TK_NOT Expr-node wrapper around the 950 ** outside of *ppExpr. 951 */ 952 static void exprNot(Parse *pParse, int doNot, ExprSpan *pSpan){ 953 if( doNot ){ 954 pSpan->pExpr = sqlite3PExpr(pParse, TK_NOT, pSpan->pExpr, 0, 0); 955 } 956 } 957 } 958 959 expr(A) ::= expr(A) AND(OP) expr(Y). {spanBinaryExpr(pParse,@OP,&A,&Y);} 960 expr(A) ::= expr(A) OR(OP) expr(Y). {spanBinaryExpr(pParse,@OP,&A,&Y);} 961 expr(A) ::= expr(A) LT|GT|GE|LE(OP) expr(Y). 962 {spanBinaryExpr(pParse,@OP,&A,&Y);} 963 expr(A) ::= expr(A) EQ|NE(OP) expr(Y). {spanBinaryExpr(pParse,@OP,&A,&Y);} 964 expr(A) ::= expr(A) BITAND|BITOR|LSHIFT|RSHIFT(OP) expr(Y). 965 {spanBinaryExpr(pParse,@OP,&A,&Y);} 966 expr(A) ::= expr(A) PLUS|MINUS(OP) expr(Y). 967 {spanBinaryExpr(pParse,@OP,&A,&Y);} 968 expr(A) ::= expr(A) STAR|SLASH|REM(OP) expr(Y). 969 {spanBinaryExpr(pParse,@OP,&A,&Y);} 970 expr(A) ::= expr(A) CONCAT(OP) expr(Y). {spanBinaryExpr(pParse,@OP,&A,&Y);} 971 %type likeop {struct LikeOp} 972 likeop(A) ::= LIKE_KW|MATCH(X). {A.eOperator = X; A.bNot = 0;/*A-overwrites-X*/} 973 likeop(A) ::= NOT LIKE_KW|MATCH(X). {A.eOperator = X; A.bNot = 1;} 974 expr(A) ::= expr(A) likeop(OP) expr(Y). [LIKE_KW] { 975 ExprList *pList; 976 pList = sqlite3ExprListAppend(pParse,0, Y.pExpr); 977 pList = sqlite3ExprListAppend(pParse,pList, A.pExpr); 978 A.pExpr = sqlite3ExprFunction(pParse, pList, &OP.eOperator); 979 exprNot(pParse, OP.bNot, &A); 980 A.zEnd = Y.zEnd; 981 if( A.pExpr ) A.pExpr->flags |= EP_InfixFunc; 982 } 983 expr(A) ::= expr(A) likeop(OP) expr(Y) ESCAPE expr(E). [LIKE_KW] { 984 ExprList *pList; 985 pList = sqlite3ExprListAppend(pParse,0, Y.pExpr); 986 pList = sqlite3ExprListAppend(pParse,pList, A.pExpr); 987 pList = sqlite3ExprListAppend(pParse,pList, E.pExpr); 988 A.pExpr = sqlite3ExprFunction(pParse, pList, &OP.eOperator); 989 exprNot(pParse, OP.bNot, &A); 990 A.zEnd = E.zEnd; 991 if( A.pExpr ) A.pExpr->flags |= EP_InfixFunc; 992 } 993 994 %include { 995 /* Construct an expression node for a unary postfix operator 996 */ 997 static void spanUnaryPostfix( 998 Parse *pParse, /* Parsing context to record errors */ 999 int op, /* The operator */ 1000 ExprSpan *pOperand, /* The operand, and output */ 1001 Token *pPostOp /* The operand token for setting the span */ 1002 ){ 1003 pOperand->pExpr = sqlite3PExpr(pParse, op, pOperand->pExpr, 0, 0); 1004 pOperand->zEnd = &pPostOp->z[pPostOp->n]; 1005 } 1006 } 1007 1008 expr(A) ::= expr(A) ISNULL|NOTNULL(E). {spanUnaryPostfix(pParse,@E,&A,&E);} 1009 expr(A) ::= expr(A) NOT NULL(E). {spanUnaryPostfix(pParse,TK_NOTNULL,&A,&E);} 1010 1011 %include { 1012 /* A routine to convert a binary TK_IS or TK_ISNOT expression into a 1013 ** unary TK_ISNULL or TK_NOTNULL expression. */ 1014 static void binaryToUnaryIfNull(Parse *pParse, Expr *pY, Expr *pA, int op){ 1015 sqlite3 *db = pParse->db; 1016 if( pA && pY && pY->op==TK_NULL ){ 1017 pA->op = (u8)op; 1018 sqlite3ExprDelete(db, pA->pRight); 1019 pA->pRight = 0; 1020 } 1021 } 1022 } 1023 1024 // expr1 IS expr2 1025 // expr1 IS NOT expr2 1026 // 1027 // If expr2 is NULL then code as TK_ISNULL or TK_NOTNULL. If expr2 1028 // is any other expression, code as TK_IS or TK_ISNOT. 1029 // 1030 expr(A) ::= expr(A) IS expr(Y). { 1031 spanBinaryExpr(pParse,TK_IS,&A,&Y); 1032 binaryToUnaryIfNull(pParse, Y.pExpr, A.pExpr, TK_ISNULL); 1033 } 1034 expr(A) ::= expr(A) IS NOT expr(Y). { 1035 spanBinaryExpr(pParse,TK_ISNOT,&A,&Y); 1036 binaryToUnaryIfNull(pParse, Y.pExpr, A.pExpr, TK_NOTNULL); 1037 } 1038 1039 %include { 1040 /* Construct an expression node for a unary prefix operator 1041 */ 1042 static void spanUnaryPrefix( 1043 ExprSpan *pOut, /* Write the new expression node here */ 1044 Parse *pParse, /* Parsing context to record errors */ 1045 int op, /* The operator */ 1046 ExprSpan *pOperand, /* The operand */ 1047 Token *pPreOp /* The operand token for setting the span */ 1048 ){ 1049 pOut->zStart = pPreOp->z; 1050 pOut->pExpr = sqlite3PExpr(pParse, op, pOperand->pExpr, 0, 0); 1051 pOut->zEnd = pOperand->zEnd; 1052 } 1053 } 1054 1055 1056 1057 expr(A) ::= NOT(B) expr(X). 1058 {spanUnaryPrefix(&A,pParse,@B,&X,&B);/*A-overwrites-B*/} 1059 expr(A) ::= BITNOT(B) expr(X). 1060 {spanUnaryPrefix(&A,pParse,@B,&X,&B);/*A-overwrites-B*/} 1061 expr(A) ::= MINUS(B) expr(X). [BITNOT] 1062 {spanUnaryPrefix(&A,pParse,TK_UMINUS,&X,&B);/*A-overwrites-B*/} 1063 expr(A) ::= PLUS(B) expr(X). [BITNOT] 1064 {spanUnaryPrefix(&A,pParse,TK_UPLUS,&X,&B);/*A-overwrites-B*/} 1065 1066 %type between_op {int} 1067 between_op(A) ::= BETWEEN. {A = 0;} 1068 between_op(A) ::= NOT BETWEEN. {A = 1;} 1069 expr(A) ::= expr(A) between_op(N) expr(X) AND expr(Y). [BETWEEN] { 1070 ExprList *pList = sqlite3ExprListAppend(pParse,0, X.pExpr); 1071 pList = sqlite3ExprListAppend(pParse,pList, Y.pExpr); 1072 A.pExpr = sqlite3PExpr(pParse, TK_BETWEEN, A.pExpr, 0, 0); 1073 if( A.pExpr ){ 1074 A.pExpr->x.pList = pList; 1075 }else{ 1076 sqlite3ExprListDelete(pParse->db, pList); 1077 } 1078 exprNot(pParse, N, &A); 1079 A.zEnd = Y.zEnd; 1080 } 1081 %ifndef SQLITE_OMIT_SUBQUERY 1082 %type in_op {int} 1083 in_op(A) ::= IN. {A = 0;} 1084 in_op(A) ::= NOT IN. {A = 1;} 1085 expr(A) ::= expr(A) in_op(N) LP exprlist(Y) RP(E). [IN] { 1086 if( Y==0 ){ 1087 /* Expressions of the form 1088 ** 1089 ** expr1 IN () 1090 ** expr1 NOT IN () 1091 ** 1092 ** simplify to constants 0 (false) and 1 (true), respectively, 1093 ** regardless of the value of expr1. 1094 */ 1095 sqlite3ExprDelete(pParse->db, A.pExpr); 1096 A.pExpr = sqlite3PExpr(pParse, TK_INTEGER, 0, 0, &sqlite3IntTokens[N]); 1097 }else if( Y->nExpr==1 ){ 1098 /* Expressions of the form: 1099 ** 1100 ** expr1 IN (?1) 1101 ** expr1 NOT IN (?2) 1102 ** 1103 ** with exactly one value on the RHS can be simplified to something 1104 ** like this: 1105 ** 1106 ** expr1 == ?1 1107 ** expr1 <> ?2 1108 ** 1109 ** But, the RHS of the == or <> is marked with the EP_Generic flag 1110 ** so that it may not contribute to the computation of comparison 1111 ** affinity or the collating sequence to use for comparison. Otherwise, 1112 ** the semantics would be subtly different from IN or NOT IN. 1113 */ 1114 Expr *pRHS = Y->a[0].pExpr; 1115 Y->a[0].pExpr = 0; 1116 sqlite3ExprListDelete(pParse->db, Y); 1117 /* pRHS cannot be NULL because a malloc error would have been detected 1118 ** before now and control would have never reached this point */ 1119 if( ALWAYS(pRHS) ){ 1120 pRHS->flags &= ~EP_Collate; 1121 pRHS->flags |= EP_Generic; 1122 } 1123 A.pExpr = sqlite3PExpr(pParse, N ? TK_NE : TK_EQ, A.pExpr, pRHS, 0); 1124 }else{ 1125 A.pExpr = sqlite3PExpr(pParse, TK_IN, A.pExpr, 0, 0); 1126 if( A.pExpr ){ 1127 A.pExpr->x.pList = Y; 1128 sqlite3ExprSetHeightAndFlags(pParse, A.pExpr); 1129 }else{ 1130 sqlite3ExprListDelete(pParse->db, Y); 1131 } 1132 exprNot(pParse, N, &A); 1133 } 1134 A.zEnd = &E.z[E.n]; 1135 } 1136 expr(A) ::= LP(B) select(X) RP(E). { 1137 spanSet(&A,&B,&E); /*A-overwrites-B*/ 1138 A.pExpr = sqlite3PExpr(pParse, TK_SELECT, 0, 0, 0); 1139 if( A.pExpr ){ 1140 A.pExpr->x.pSelect = X; 1141 ExprSetProperty(A.pExpr, EP_xIsSelect|EP_Subquery); 1142 sqlite3ExprSetHeightAndFlags(pParse, A.pExpr); 1143 }else{ 1144 sqlite3SelectDelete(pParse->db, X); 1145 } 1146 } 1147 expr(A) ::= expr(A) in_op(N) LP select(Y) RP(E). [IN] { 1148 A.pExpr = sqlite3PExpr(pParse, TK_IN, A.pExpr, 0, 0); 1149 if( A.pExpr ){ 1150 A.pExpr->x.pSelect = Y; 1151 ExprSetProperty(A.pExpr, EP_xIsSelect|EP_Subquery); 1152 sqlite3ExprSetHeightAndFlags(pParse, A.pExpr); 1153 }else{ 1154 sqlite3SelectDelete(pParse->db, Y); 1155 } 1156 exprNot(pParse, N, &A); 1157 A.zEnd = &E.z[E.n]; 1158 } 1159 expr(A) ::= expr(A) in_op(N) nm(Y) dbnm(Z). [IN] { 1160 SrcList *pSrc = sqlite3SrcListAppend(pParse->db, 0,&Y,&Z); 1161 A.pExpr = sqlite3PExpr(pParse, TK_IN, A.pExpr, 0, 0); 1162 if( A.pExpr ){ 1163 A.pExpr->x.pSelect = sqlite3SelectNew(pParse, 0,pSrc,0,0,0,0,0,0,0); 1164 ExprSetProperty(A.pExpr, EP_xIsSelect|EP_Subquery); 1165 sqlite3ExprSetHeightAndFlags(pParse, A.pExpr); 1166 }else{ 1167 sqlite3SrcListDelete(pParse->db, pSrc); 1168 } 1169 exprNot(pParse, N, &A); 1170 A.zEnd = Z.z ? &Z.z[Z.n] : &Y.z[Y.n]; 1171 } 1172 expr(A) ::= EXISTS(B) LP select(Y) RP(E). { 1173 Expr *p; 1174 spanSet(&A,&B,&E); /*A-overwrites-B*/ 1175 p = A.pExpr = sqlite3PExpr(pParse, TK_EXISTS, 0, 0, 0); 1176 if( p ){ 1177 p->x.pSelect = Y; 1178 ExprSetProperty(p, EP_xIsSelect|EP_Subquery); 1179 sqlite3ExprSetHeightAndFlags(pParse, p); 1180 }else{ 1181 sqlite3SelectDelete(pParse->db, Y); 1182 } 1183 } 1184 %endif SQLITE_OMIT_SUBQUERY 1185 1186 /* CASE expressions */ 1187 expr(A) ::= CASE(C) case_operand(X) case_exprlist(Y) case_else(Z) END(E). { 1188 spanSet(&A,&C,&E); /*A-overwrites-C*/ 1189 A.pExpr = sqlite3PExpr(pParse, TK_CASE, X, 0, 0); 1190 if( A.pExpr ){ 1191 A.pExpr->x.pList = Z ? sqlite3ExprListAppend(pParse,Y,Z) : Y; 1192 sqlite3ExprSetHeightAndFlags(pParse, A.pExpr); 1193 }else{ 1194 sqlite3ExprListDelete(pParse->db, Y); 1195 sqlite3ExprDelete(pParse->db, Z); 1196 } 1197 } 1198 %type case_exprlist {ExprList*} 1199 %destructor case_exprlist {sqlite3ExprListDelete(pParse->db, $$);} 1200 case_exprlist(A) ::= case_exprlist(A) WHEN expr(Y) THEN expr(Z). { 1201 A = sqlite3ExprListAppend(pParse,A, Y.pExpr); 1202 A = sqlite3ExprListAppend(pParse,A, Z.pExpr); 1203 } 1204 case_exprlist(A) ::= WHEN expr(Y) THEN expr(Z). { 1205 A = sqlite3ExprListAppend(pParse,0, Y.pExpr); 1206 A = sqlite3ExprListAppend(pParse,A, Z.pExpr); 1207 } 1208 %type case_else {Expr*} 1209 %destructor case_else {sqlite3ExprDelete(pParse->db, $$);} 1210 case_else(A) ::= ELSE expr(X). {A = X.pExpr;} 1211 case_else(A) ::= . {A = 0;} 1212 %type case_operand {Expr*} 1213 %destructor case_operand {sqlite3ExprDelete(pParse->db, $$);} 1214 case_operand(A) ::= expr(X). {A = X.pExpr; /*A-overwrites-X*/} 1215 case_operand(A) ::= . {A = 0;} 1216 1217 %type exprlist {ExprList*} 1218 %destructor exprlist {sqlite3ExprListDelete(pParse->db, $$);} 1219 %type nexprlist {ExprList*} 1220 %destructor nexprlist {sqlite3ExprListDelete(pParse->db, $$);} 1221 1222 exprlist(A) ::= nexprlist(A). 1223 exprlist(A) ::= . {A = 0;} 1224 nexprlist(A) ::= nexprlist(A) COMMA expr(Y). 1225 {A = sqlite3ExprListAppend(pParse,A,Y.pExpr);} 1226 nexprlist(A) ::= expr(Y). 1227 {A = sqlite3ExprListAppend(pParse,0,Y.pExpr); /*A-overwrites-Y*/} 1228 1229 1230 ///////////////////////////// The CREATE INDEX command /////////////////////// 1231 // 1232 cmd ::= createkw(S) uniqueflag(U) INDEX ifnotexists(NE) nm(X) dbnm(D) 1233 ON nm(Y) LP sortlist(Z) RP where_opt(W). { 1234 sqlite3CreateIndex(pParse, &X, &D, 1235 sqlite3SrcListAppend(pParse->db,0,&Y,0), Z, U, 1236 &S, W, SQLITE_SO_ASC, NE); 1237 } 1238 1239 %type uniqueflag {int} 1240 uniqueflag(A) ::= UNIQUE. {A = OE_Abort;} 1241 uniqueflag(A) ::= . {A = OE_None;} 1242 1243 1244 // The eidlist non-terminal (Expression Id List) generates an ExprList 1245 // from a list of identifiers. The identifier names are in ExprList.a[].zName. 1246 // This list is stored in an ExprList rather than an IdList so that it 1247 // can be easily sent to sqlite3ColumnsExprList(). 1248 // 1249 // eidlist is grouped with CREATE INDEX because it used to be the non-terminal 1250 // used for the arguments to an index. That is just an historical accident. 1251 // 1252 // IMPORTANT COMPATIBILITY NOTE: Some prior versions of SQLite accepted 1253 // COLLATE clauses and ASC or DESC keywords on ID lists in inappropriate 1254 // places - places that might have been stored in the sqlite_master schema. 1255 // Those extra features were ignored. But because they might be in some 1256 // (busted) old databases, we need to continue parsing them when loading 1257 // historical schemas. 1258 // 1259 %type eidlist {ExprList*} 1260 %destructor eidlist {sqlite3ExprListDelete(pParse->db, $$);} 1261 %type eidlist_opt {ExprList*} 1262 %destructor eidlist_opt {sqlite3ExprListDelete(pParse->db, $$);} 1263 1264 %include { 1265 /* Add a single new term to an ExprList that is used to store a 1266 ** list of identifiers. Report an error if the ID list contains 1267 ** a COLLATE clause or an ASC or DESC keyword, except ignore the 1268 ** error while parsing a legacy schema. 1269 */ 1270 static ExprList *parserAddExprIdListTerm( 1271 Parse *pParse, 1272 ExprList *pPrior, 1273 Token *pIdToken, 1274 int hasCollate, 1275 int sortOrder 1276 ){ 1277 ExprList *p = sqlite3ExprListAppend(pParse, pPrior, 0); 1278 if( (hasCollate || sortOrder!=SQLITE_SO_UNDEFINED) 1279 && pParse->db->init.busy==0 1280 ){ 1281 sqlite3ErrorMsg(pParse, "syntax error after column name \"%.*s\"", 1282 pIdToken->n, pIdToken->z); 1283 } 1284 sqlite3ExprListSetName(pParse, p, pIdToken, 1); 1285 return p; 1286 } 1287 } // end %include 1288 1289 eidlist_opt(A) ::= . {A = 0;} 1290 eidlist_opt(A) ::= LP eidlist(X) RP. {A = X;} 1291 eidlist(A) ::= eidlist(A) COMMA nm(Y) collate(C) sortorder(Z). { 1292 A = parserAddExprIdListTerm(pParse, A, &Y, C, Z); 1293 } 1294 eidlist(A) ::= nm(Y) collate(C) sortorder(Z). { 1295 A = parserAddExprIdListTerm(pParse, 0, &Y, C, Z); /*A-overwrites-Y*/ 1296 } 1297 1298 %type collate {int} 1299 collate(C) ::= . {C = 0;} 1300 collate(C) ::= COLLATE ids. {C = 1;} 1301 1302 1303 ///////////////////////////// The DROP INDEX command ///////////////////////// 1304 // 1305 cmd ::= DROP INDEX ifexists(E) fullname(X). {sqlite3DropIndex(pParse, X, E);} 1306 1307 ///////////////////////////// The VACUUM command ///////////////////////////// 1308 // 1309 %ifndef SQLITE_OMIT_VACUUM 1310 %ifndef SQLITE_OMIT_ATTACH 1311 cmd ::= VACUUM. {sqlite3Vacuum(pParse);} 1312 cmd ::= VACUUM nm. {sqlite3Vacuum(pParse);} 1313 %endif SQLITE_OMIT_ATTACH 1314 %endif SQLITE_OMIT_VACUUM 1315 1316 ///////////////////////////// The PRAGMA command ///////////////////////////// 1317 // 1318 %ifndef SQLITE_OMIT_PRAGMA 1319 cmd ::= PRAGMA nm(X) dbnm(Z). {sqlite3Pragma(pParse,&X,&Z,0,0);} 1320 cmd ::= PRAGMA nm(X) dbnm(Z) EQ nmnum(Y). {sqlite3Pragma(pParse,&X,&Z,&Y,0);} 1321 cmd ::= PRAGMA nm(X) dbnm(Z) LP nmnum(Y) RP. {sqlite3Pragma(pParse,&X,&Z,&Y,0);} 1322 cmd ::= PRAGMA nm(X) dbnm(Z) EQ minus_num(Y). 1323 {sqlite3Pragma(pParse,&X,&Z,&Y,1);} 1324 cmd ::= PRAGMA nm(X) dbnm(Z) LP minus_num(Y) RP. 1325 {sqlite3Pragma(pParse,&X,&Z,&Y,1);} 1326 1327 nmnum(A) ::= plus_num(A). 1328 nmnum(A) ::= nm(A). 1329 nmnum(A) ::= ON(A). 1330 nmnum(A) ::= DELETE(A). 1331 nmnum(A) ::= DEFAULT(A). 1332 %endif SQLITE_OMIT_PRAGMA 1333 %token_class number INTEGER|FLOAT. 1334 plus_num(A) ::= PLUS number(X). {A = X;} 1335 plus_num(A) ::= number(A). 1336 minus_num(A) ::= MINUS number(X). {A = X;} 1337 //////////////////////////// The CREATE TRIGGER command ///////////////////// 1338 1339 %ifndef SQLITE_OMIT_TRIGGER 1340 1341 cmd ::= createkw trigger_decl(A) BEGIN trigger_cmd_list(S) END(Z). { 1342 Token all; 1343 all.z = A.z; 1344 all.n = (int)(Z.z - A.z) + Z.n; 1345 sqlite3FinishTrigger(pParse, S, &all); 1346 } 1347 1348 trigger_decl(A) ::= temp(T) TRIGGER ifnotexists(NOERR) nm(B) dbnm(Z) 1349 trigger_time(C) trigger_event(D) 1350 ON fullname(E) foreach_clause when_clause(G). { 1351 sqlite3BeginTrigger(pParse, &B, &Z, C, D.a, D.b, E, G, T, NOERR); 1352 A = (Z.n==0?B:Z); /*A-overwrites-T*/ 1353 } 1354 1355 %type trigger_time {int} 1356 trigger_time(A) ::= BEFORE. { A = TK_BEFORE; } 1357 trigger_time(A) ::= AFTER. { A = TK_AFTER; } 1358 trigger_time(A) ::= INSTEAD OF. { A = TK_INSTEAD;} 1359 trigger_time(A) ::= . { A = TK_BEFORE; } 1360 1361 %type trigger_event {struct TrigEvent} 1362 %destructor trigger_event {sqlite3IdListDelete(pParse->db, $$.b);} 1363 trigger_event(A) ::= DELETE|INSERT(X). {A.a = @X; /*A-overwrites-X*/ A.b = 0;} 1364 trigger_event(A) ::= UPDATE(X). {A.a = @X; /*A-overwrites-X*/ A.b = 0;} 1365 trigger_event(A) ::= UPDATE OF idlist(X).{A.a = TK_UPDATE; A.b = X;} 1366 1367 foreach_clause ::= . 1368 foreach_clause ::= FOR EACH ROW. 1369 1370 %type when_clause {Expr*} 1371 %destructor when_clause {sqlite3ExprDelete(pParse->db, $$);} 1372 when_clause(A) ::= . { A = 0; } 1373 when_clause(A) ::= WHEN expr(X). { A = X.pExpr; } 1374 1375 %type trigger_cmd_list {TriggerStep*} 1376 %destructor trigger_cmd_list {sqlite3DeleteTriggerStep(pParse->db, $$);} 1377 trigger_cmd_list(A) ::= trigger_cmd_list(A) trigger_cmd(X) SEMI. { 1378 assert( A!=0 ); 1379 A->pLast->pNext = X; 1380 A->pLast = X; 1381 } 1382 trigger_cmd_list(A) ::= trigger_cmd(A) SEMI. { 1383 assert( A!=0 ); 1384 A->pLast = A; 1385 } 1386 1387 // Disallow qualified table names on INSERT, UPDATE, and DELETE statements 1388 // within a trigger. The table to INSERT, UPDATE, or DELETE is always in 1389 // the same database as the table that the trigger fires on. 1390 // 1391 %type trnm {Token} 1392 trnm(A) ::= nm(A). 1393 trnm(A) ::= nm DOT nm(X). { 1394 A = X; 1395 sqlite3ErrorMsg(pParse, 1396 "qualified table names are not allowed on INSERT, UPDATE, and DELETE " 1397 "statements within triggers"); 1398 } 1399 1400 // Disallow the INDEX BY and NOT INDEXED clauses on UPDATE and DELETE 1401 // statements within triggers. We make a specific error message for this 1402 // since it is an exception to the default grammar rules. 1403 // 1404 tridxby ::= . 1405 tridxby ::= INDEXED BY nm. { 1406 sqlite3ErrorMsg(pParse, 1407 "the INDEXED BY clause is not allowed on UPDATE or DELETE statements " 1408 "within triggers"); 1409 } 1410 tridxby ::= NOT INDEXED. { 1411 sqlite3ErrorMsg(pParse, 1412 "the NOT INDEXED clause is not allowed on UPDATE or DELETE statements " 1413 "within triggers"); 1414 } 1415 1416 1417 1418 %type trigger_cmd {TriggerStep*} 1419 %destructor trigger_cmd {sqlite3DeleteTriggerStep(pParse->db, $$);} 1420 // UPDATE 1421 trigger_cmd(A) ::= 1422 UPDATE orconf(R) trnm(X) tridxby SET setlist(Y) where_opt(Z). 1423 {A = sqlite3TriggerUpdateStep(pParse->db, &X, Y, Z, R);} 1424 1425 // INSERT 1426 trigger_cmd(A) ::= insert_cmd(R) INTO trnm(X) idlist_opt(F) select(S). 1427 {A = sqlite3TriggerInsertStep(pParse->db, &X, F, S, R);/*A-overwrites-R*/} 1428 1429 // DELETE 1430 trigger_cmd(A) ::= DELETE FROM trnm(X) tridxby where_opt(Y). 1431 {A = sqlite3TriggerDeleteStep(pParse->db, &X, Y);} 1432 1433 // SELECT 1434 trigger_cmd(A) ::= select(X). 1435 {A = sqlite3TriggerSelectStep(pParse->db, X); /*A-overwrites-X*/} 1436 1437 // The special RAISE expression that may occur in trigger programs 1438 expr(A) ::= RAISE(X) LP IGNORE RP(Y). { 1439 spanSet(&A,&X,&Y); /*A-overwrites-X*/ 1440 A.pExpr = sqlite3PExpr(pParse, TK_RAISE, 0, 0, 0); 1441 if( A.pExpr ){ 1442 A.pExpr->affinity = OE_Ignore; 1443 } 1444 } 1445 expr(A) ::= RAISE(X) LP raisetype(T) COMMA nm(Z) RP(Y). { 1446 spanSet(&A,&X,&Y); /*A-overwrites-X*/ 1447 A.pExpr = sqlite3PExpr(pParse, TK_RAISE, 0, 0, &Z); 1448 if( A.pExpr ) { 1449 A.pExpr->affinity = (char)T; 1450 } 1451 } 1452 %endif !SQLITE_OMIT_TRIGGER 1453 1454 %type raisetype {int} 1455 raisetype(A) ::= ROLLBACK. {A = OE_Rollback;} 1456 raisetype(A) ::= ABORT. {A = OE_Abort;} 1457 raisetype(A) ::= FAIL. {A = OE_Fail;} 1458 1459 1460 //////////////////////// DROP TRIGGER statement ////////////////////////////// 1461 %ifndef SQLITE_OMIT_TRIGGER 1462 cmd ::= DROP TRIGGER ifexists(NOERR) fullname(X). { 1463 sqlite3DropTrigger(pParse,X,NOERR); 1464 } 1465 %endif !SQLITE_OMIT_TRIGGER 1466 1467 //////////////////////// ATTACH DATABASE file AS name ///////////////////////// 1468 %ifndef SQLITE_OMIT_ATTACH 1469 cmd ::= ATTACH database_kw_opt expr(F) AS expr(D) key_opt(K). { 1470 sqlite3Attach(pParse, F.pExpr, D.pExpr, K); 1471 } 1472 cmd ::= DETACH database_kw_opt expr(D). { 1473 sqlite3Detach(pParse, D.pExpr); 1474 } 1475 1476 %type key_opt {Expr*} 1477 %destructor key_opt {sqlite3ExprDelete(pParse->db, $$);} 1478 key_opt(A) ::= . { A = 0; } 1479 key_opt(A) ::= KEY expr(X). { A = X.pExpr; } 1480 1481 database_kw_opt ::= DATABASE. 1482 database_kw_opt ::= . 1483 %endif SQLITE_OMIT_ATTACH 1484 1485 ////////////////////////// REINDEX collation ////////////////////////////////// 1486 %ifndef SQLITE_OMIT_REINDEX 1487 cmd ::= REINDEX. {sqlite3Reindex(pParse, 0, 0);} 1488 cmd ::= REINDEX nm(X) dbnm(Y). {sqlite3Reindex(pParse, &X, &Y);} 1489 %endif SQLITE_OMIT_REINDEX 1490 1491 /////////////////////////////////// ANALYZE /////////////////////////////////// 1492 %ifndef SQLITE_OMIT_ANALYZE 1493 cmd ::= ANALYZE. {sqlite3Analyze(pParse, 0, 0);} 1494 cmd ::= ANALYZE nm(X) dbnm(Y). {sqlite3Analyze(pParse, &X, &Y);} 1495 %endif 1496 1497 //////////////////////// ALTER TABLE table ... //////////////////////////////// 1498 %ifndef SQLITE_OMIT_ALTERTABLE 1499 cmd ::= ALTER TABLE fullname(X) RENAME TO nm(Z). { 1500 sqlite3AlterRenameTable(pParse,X,&Z); 1501 } 1502 cmd ::= ALTER TABLE add_column_fullname ADD kwcolumn_opt column(Y). { 1503 sqlite3AlterFinishAddColumn(pParse, &Y); 1504 } 1505 add_column_fullname ::= fullname(X). { 1506 disableLookaside(pParse); 1507 sqlite3AlterBeginAddColumn(pParse, X); 1508 } 1509 kwcolumn_opt ::= . 1510 kwcolumn_opt ::= COLUMNKW. 1511 %endif SQLITE_OMIT_ALTERTABLE 1512 1513 //////////////////////// CREATE VIRTUAL TABLE ... ///////////////////////////// 1514 %ifndef SQLITE_OMIT_VIRTUALTABLE 1515 cmd ::= create_vtab. {sqlite3VtabFinishParse(pParse,0);} 1516 cmd ::= create_vtab LP vtabarglist RP(X). {sqlite3VtabFinishParse(pParse,&X);} 1517 create_vtab ::= createkw VIRTUAL TABLE ifnotexists(E) 1518 nm(X) dbnm(Y) USING nm(Z). { 1519 sqlite3VtabBeginParse(pParse, &X, &Y, &Z, E); 1520 } 1521 vtabarglist ::= vtabarg. 1522 vtabarglist ::= vtabarglist COMMA vtabarg. 1523 vtabarg ::= . {sqlite3VtabArgInit(pParse);} 1524 vtabarg ::= vtabarg vtabargtoken. 1525 vtabargtoken ::= ANY(X). {sqlite3VtabArgExtend(pParse,&X);} 1526 vtabargtoken ::= lp anylist RP(X). {sqlite3VtabArgExtend(pParse,&X);} 1527 lp ::= LP(X). {sqlite3VtabArgExtend(pParse,&X);} 1528 anylist ::= . 1529 anylist ::= anylist LP anylist RP. 1530 anylist ::= anylist ANY. 1531 %endif SQLITE_OMIT_VIRTUALTABLE 1532 1533 1534 //////////////////////// COMMON TABLE EXPRESSIONS //////////////////////////// 1535 %type with {With*} 1536 %type wqlist {With*} 1537 %destructor with {sqlite3WithDelete(pParse->db, $$);} 1538 %destructor wqlist {sqlite3WithDelete(pParse->db, $$);} 1539 1540 with(A) ::= . {A = 0;} 1541 %ifndef SQLITE_OMIT_CTE 1542 with(A) ::= WITH wqlist(W). { A = W; } 1543 with(A) ::= WITH RECURSIVE wqlist(W). { A = W; } 1544 1545 wqlist(A) ::= nm(X) eidlist_opt(Y) AS LP select(Z) RP. { 1546 A = sqlite3WithAdd(pParse, 0, &X, Y, Z); /*A-overwrites-X*/ 1547 } 1548 wqlist(A) ::= wqlist(A) COMMA nm(X) eidlist_opt(Y) AS LP select(Z) RP. { 1549 A = sqlite3WithAdd(pParse, A, &X, Y, Z); 1550 } 1551 %endif SQLITE_OMIT_CTE 1552