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