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