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