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