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