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