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