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 expr(A) ::= id(X). {spanExpr(&A,pParse,TK_ID,X); /*A-overwrites-X*/} 879 expr(A) ::= JOIN_KW(X). {spanExpr(&A,pParse,TK_ID,X); /*A-overwrites-X*/} 880 expr(A) ::= nm(X) DOT nm(Y). { 881 Expr *temp1 = sqlite3ExprAlloc(pParse->db, TK_ID, &X, 1); 882 Expr *temp2 = sqlite3ExprAlloc(pParse->db, TK_ID, &Y, 1); 883 spanSet(&A,&X,&Y); /*A-overwrites-X*/ 884 A.pExpr = sqlite3PExpr(pParse, TK_DOT, temp1, temp2); 885 } 886 expr(A) ::= nm(X) DOT nm(Y) DOT nm(Z). { 887 Expr *temp1 = sqlite3ExprAlloc(pParse->db, TK_ID, &X, 1); 888 Expr *temp2 = sqlite3ExprAlloc(pParse->db, TK_ID, &Y, 1); 889 Expr *temp3 = sqlite3ExprAlloc(pParse->db, TK_ID, &Z, 1); 890 Expr *temp4 = sqlite3PExpr(pParse, TK_DOT, temp2, temp3); 891 spanSet(&A,&X,&Z); /*A-overwrites-X*/ 892 A.pExpr = sqlite3PExpr(pParse, TK_DOT, temp1, temp4); 893 } 894 term(A) ::= NULL|FLOAT|BLOB(X). {spanExpr(&A,pParse,@X,X); /*A-overwrites-X*/} 895 term(A) ::= STRING(X). {spanExpr(&A,pParse,@X,X); /*A-overwrites-X*/} 896 term(A) ::= INTEGER(X). { 897 A.pExpr = sqlite3ExprAlloc(pParse->db, TK_INTEGER, &X, 1); 898 A.zStart = X.z; 899 A.zEnd = X.z + X.n; 900 } 901 expr(A) ::= VARIABLE(X). { 902 if( !(X.z[0]=='#' && sqlite3Isdigit(X.z[1])) ){ 903 u32 n = X.n; 904 spanExpr(&A, pParse, TK_VARIABLE, X); 905 sqlite3ExprAssignVarNumber(pParse, A.pExpr, n); 906 }else{ 907 /* When doing a nested parse, one can include terms in an expression 908 ** that look like this: #1 #2 ... These terms refer to registers 909 ** in the virtual machine. #N is the N-th register. */ 910 Token t = X; /*A-overwrites-X*/ 911 assert( t.n>=2 ); 912 spanSet(&A, &t, &t); 913 if( pParse->nested==0 ){ 914 sqlite3ErrorMsg(pParse, "near \"%T\": syntax error", &t); 915 A.pExpr = 0; 916 }else{ 917 A.pExpr = sqlite3PExpr(pParse, TK_REGISTER, 0, 0); 918 if( A.pExpr ) sqlite3GetInt32(&t.z[1], &A.pExpr->iTable); 919 } 920 } 921 } 922 expr(A) ::= expr(A) COLLATE ids(C). { 923 A.pExpr = sqlite3ExprAddCollateToken(pParse, A.pExpr, &C, 1); 924 A.zEnd = &C.z[C.n]; 925 } 926 %ifndef SQLITE_OMIT_CAST 927 expr(A) ::= CAST(X) LP expr(E) AS typetoken(T) RP(Y). { 928 spanSet(&A,&X,&Y); /*A-overwrites-X*/ 929 A.pExpr = sqlite3ExprAlloc(pParse->db, TK_CAST, &T, 1); 930 sqlite3ExprAttachSubtrees(pParse->db, A.pExpr, E.pExpr, 0); 931 } 932 %endif SQLITE_OMIT_CAST 933 expr(A) ::= id(X) LP distinct(D) exprlist(Y) RP(E). { 934 if( Y && Y->nExpr>pParse->db->aLimit[SQLITE_LIMIT_FUNCTION_ARG] ){ 935 sqlite3ErrorMsg(pParse, "too many arguments on function %T", &X); 936 } 937 A.pExpr = sqlite3ExprFunction(pParse, Y, &X); 938 spanSet(&A,&X,&E); 939 if( D==SF_Distinct && A.pExpr ){ 940 A.pExpr->flags |= EP_Distinct; 941 } 942 } 943 expr(A) ::= id(X) LP STAR RP(E). { 944 A.pExpr = sqlite3ExprFunction(pParse, 0, &X); 945 spanSet(&A,&X,&E); 946 } 947 term(A) ::= CTIME_KW(OP). { 948 A.pExpr = sqlite3ExprFunction(pParse, 0, &OP); 949 spanSet(&A, &OP, &OP); 950 } 951 952 %include { 953 /* This routine constructs a binary expression node out of two ExprSpan 954 ** objects and uses the result to populate a new ExprSpan object. 955 */ 956 static void spanBinaryExpr( 957 Parse *pParse, /* The parsing context. Errors accumulate here */ 958 int op, /* The binary operation */ 959 ExprSpan *pLeft, /* The left operand, and output */ 960 ExprSpan *pRight /* The right operand */ 961 ){ 962 pLeft->pExpr = sqlite3PExpr(pParse, op, pLeft->pExpr, pRight->pExpr); 963 pLeft->zEnd = pRight->zEnd; 964 } 965 966 /* If doNot is true, then add a TK_NOT Expr-node wrapper around the 967 ** outside of *ppExpr. 968 */ 969 static void exprNot(Parse *pParse, int doNot, ExprSpan *pSpan){ 970 if( doNot ){ 971 pSpan->pExpr = sqlite3PExpr(pParse, TK_NOT, pSpan->pExpr, 0); 972 } 973 } 974 } 975 976 expr(A) ::= LP(L) nexprlist(X) COMMA expr(Y) RP(R). { 977 ExprList *pList = sqlite3ExprListAppend(pParse, X, Y.pExpr); 978 A.pExpr = sqlite3PExpr(pParse, TK_VECTOR, 0, 0); 979 if( A.pExpr ){ 980 A.pExpr->x.pList = pList; 981 spanSet(&A, &L, &R); 982 }else{ 983 sqlite3ExprListDelete(pParse->db, pList); 984 } 985 } 986 987 expr(A) ::= expr(A) AND(OP) expr(Y). {spanBinaryExpr(pParse,@OP,&A,&Y);} 988 expr(A) ::= expr(A) OR(OP) expr(Y). {spanBinaryExpr(pParse,@OP,&A,&Y);} 989 expr(A) ::= expr(A) LT|GT|GE|LE(OP) expr(Y). 990 {spanBinaryExpr(pParse,@OP,&A,&Y);} 991 expr(A) ::= expr(A) EQ|NE(OP) expr(Y). {spanBinaryExpr(pParse,@OP,&A,&Y);} 992 expr(A) ::= expr(A) BITAND|BITOR|LSHIFT|RSHIFT(OP) expr(Y). 993 {spanBinaryExpr(pParse,@OP,&A,&Y);} 994 expr(A) ::= expr(A) PLUS|MINUS(OP) expr(Y). 995 {spanBinaryExpr(pParse,@OP,&A,&Y);} 996 expr(A) ::= expr(A) STAR|SLASH|REM(OP) expr(Y). 997 {spanBinaryExpr(pParse,@OP,&A,&Y);} 998 expr(A) ::= expr(A) CONCAT(OP) expr(Y). {spanBinaryExpr(pParse,@OP,&A,&Y);} 999 %type likeop {Token} 1000 likeop(A) ::= LIKE_KW|MATCH(A). 1001 likeop(A) ::= NOT LIKE_KW|MATCH(X). {A=X; A.n|=0x80000000; /*A-overwrite-X*/} 1002 expr(A) ::= expr(A) likeop(OP) expr(Y). [LIKE_KW] { 1003 ExprList *pList; 1004 int bNot = OP.n & 0x80000000; 1005 OP.n &= 0x7fffffff; 1006 pList = sqlite3ExprListAppend(pParse,0, Y.pExpr); 1007 pList = sqlite3ExprListAppend(pParse,pList, A.pExpr); 1008 A.pExpr = sqlite3ExprFunction(pParse, pList, &OP); 1009 exprNot(pParse, bNot, &A); 1010 A.zEnd = Y.zEnd; 1011 if( A.pExpr ) A.pExpr->flags |= EP_InfixFunc; 1012 } 1013 expr(A) ::= expr(A) likeop(OP) expr(Y) ESCAPE expr(E). [LIKE_KW] { 1014 ExprList *pList; 1015 int bNot = OP.n & 0x80000000; 1016 OP.n &= 0x7fffffff; 1017 pList = sqlite3ExprListAppend(pParse,0, Y.pExpr); 1018 pList = sqlite3ExprListAppend(pParse,pList, A.pExpr); 1019 pList = sqlite3ExprListAppend(pParse,pList, E.pExpr); 1020 A.pExpr = sqlite3ExprFunction(pParse, pList, &OP); 1021 exprNot(pParse, bNot, &A); 1022 A.zEnd = E.zEnd; 1023 if( A.pExpr ) A.pExpr->flags |= EP_InfixFunc; 1024 } 1025 1026 %include { 1027 /* Construct an expression node for a unary postfix operator 1028 */ 1029 static void spanUnaryPostfix( 1030 Parse *pParse, /* Parsing context to record errors */ 1031 int op, /* The operator */ 1032 ExprSpan *pOperand, /* The operand, and output */ 1033 Token *pPostOp /* The operand token for setting the span */ 1034 ){ 1035 pOperand->pExpr = sqlite3PExpr(pParse, op, pOperand->pExpr, 0); 1036 pOperand->zEnd = &pPostOp->z[pPostOp->n]; 1037 } 1038 } 1039 1040 expr(A) ::= expr(A) ISNULL|NOTNULL(E). {spanUnaryPostfix(pParse,@E,&A,&E);} 1041 expr(A) ::= expr(A) NOT NULL(E). {spanUnaryPostfix(pParse,TK_NOTNULL,&A,&E);} 1042 1043 %include { 1044 /* A routine to convert a binary TK_IS or TK_ISNOT expression into a 1045 ** unary TK_ISNULL or TK_NOTNULL expression. */ 1046 static void binaryToUnaryIfNull(Parse *pParse, Expr *pY, Expr *pA, int op){ 1047 sqlite3 *db = pParse->db; 1048 if( pA && pY && pY->op==TK_NULL ){ 1049 pA->op = (u8)op; 1050 sqlite3ExprDelete(db, pA->pRight); 1051 pA->pRight = 0; 1052 } 1053 } 1054 } 1055 1056 // expr1 IS expr2 1057 // expr1 IS NOT expr2 1058 // 1059 // If expr2 is NULL then code as TK_ISNULL or TK_NOTNULL. If expr2 1060 // is any other expression, code as TK_IS or TK_ISNOT. 1061 // 1062 expr(A) ::= expr(A) IS expr(Y). { 1063 spanBinaryExpr(pParse,TK_IS,&A,&Y); 1064 binaryToUnaryIfNull(pParse, Y.pExpr, A.pExpr, TK_ISNULL); 1065 } 1066 expr(A) ::= expr(A) IS NOT expr(Y). { 1067 spanBinaryExpr(pParse,TK_ISNOT,&A,&Y); 1068 binaryToUnaryIfNull(pParse, Y.pExpr, A.pExpr, TK_NOTNULL); 1069 } 1070 1071 %include { 1072 /* Construct an expression node for a unary prefix operator 1073 */ 1074 static void spanUnaryPrefix( 1075 ExprSpan *pOut, /* Write the new expression node here */ 1076 Parse *pParse, /* Parsing context to record errors */ 1077 int op, /* The operator */ 1078 ExprSpan *pOperand, /* The operand */ 1079 Token *pPreOp /* The operand token for setting the span */ 1080 ){ 1081 pOut->zStart = pPreOp->z; 1082 pOut->pExpr = sqlite3PExpr(pParse, op, pOperand->pExpr, 0); 1083 pOut->zEnd = pOperand->zEnd; 1084 } 1085 } 1086 1087 1088 1089 expr(A) ::= NOT(B) expr(X). 1090 {spanUnaryPrefix(&A,pParse,@B,&X,&B);/*A-overwrites-B*/} 1091 expr(A) ::= BITNOT(B) expr(X). 1092 {spanUnaryPrefix(&A,pParse,@B,&X,&B);/*A-overwrites-B*/} 1093 expr(A) ::= MINUS(B) expr(X). [BITNOT] 1094 {spanUnaryPrefix(&A,pParse,TK_UMINUS,&X,&B);/*A-overwrites-B*/} 1095 expr(A) ::= PLUS(B) expr(X). [BITNOT] 1096 {spanUnaryPrefix(&A,pParse,TK_UPLUS,&X,&B);/*A-overwrites-B*/} 1097 1098 %type between_op {int} 1099 between_op(A) ::= BETWEEN. {A = 0;} 1100 between_op(A) ::= NOT BETWEEN. {A = 1;} 1101 expr(A) ::= expr(A) between_op(N) expr(X) AND expr(Y). [BETWEEN] { 1102 ExprList *pList = sqlite3ExprListAppend(pParse,0, X.pExpr); 1103 pList = sqlite3ExprListAppend(pParse,pList, Y.pExpr); 1104 A.pExpr = sqlite3PExpr(pParse, TK_BETWEEN, A.pExpr, 0); 1105 if( A.pExpr ){ 1106 A.pExpr->x.pList = pList; 1107 }else{ 1108 sqlite3ExprListDelete(pParse->db, pList); 1109 } 1110 exprNot(pParse, N, &A); 1111 A.zEnd = Y.zEnd; 1112 } 1113 %ifndef SQLITE_OMIT_SUBQUERY 1114 %type in_op {int} 1115 in_op(A) ::= IN. {A = 0;} 1116 in_op(A) ::= NOT IN. {A = 1;} 1117 expr(A) ::= expr(A) in_op(N) LP exprlist(Y) RP(E). [IN] { 1118 if( Y==0 ){ 1119 /* Expressions of the form 1120 ** 1121 ** expr1 IN () 1122 ** expr1 NOT IN () 1123 ** 1124 ** simplify to constants 0 (false) and 1 (true), respectively, 1125 ** regardless of the value of expr1. 1126 */ 1127 sqlite3ExprDelete(pParse->db, A.pExpr); 1128 A.pExpr = sqlite3ExprAlloc(pParse->db, TK_INTEGER,&sqlite3IntTokens[N],1); 1129 }else if( Y->nExpr==1 ){ 1130 /* Expressions of the form: 1131 ** 1132 ** expr1 IN (?1) 1133 ** expr1 NOT IN (?2) 1134 ** 1135 ** with exactly one value on the RHS can be simplified to something 1136 ** like this: 1137 ** 1138 ** expr1 == ?1 1139 ** expr1 <> ?2 1140 ** 1141 ** But, the RHS of the == or <> is marked with the EP_Generic flag 1142 ** so that it may not contribute to the computation of comparison 1143 ** affinity or the collating sequence to use for comparison. Otherwise, 1144 ** the semantics would be subtly different from IN or NOT IN. 1145 */ 1146 Expr *pRHS = Y->a[0].pExpr; 1147 Y->a[0].pExpr = 0; 1148 sqlite3ExprListDelete(pParse->db, Y); 1149 /* pRHS cannot be NULL because a malloc error would have been detected 1150 ** before now and control would have never reached this point */ 1151 if( ALWAYS(pRHS) ){ 1152 pRHS->flags &= ~EP_Collate; 1153 pRHS->flags |= EP_Generic; 1154 } 1155 A.pExpr = sqlite3PExpr(pParse, N ? TK_NE : TK_EQ, A.pExpr, pRHS); 1156 }else{ 1157 A.pExpr = sqlite3PExpr(pParse, TK_IN, A.pExpr, 0); 1158 if( A.pExpr ){ 1159 A.pExpr->x.pList = Y; 1160 sqlite3ExprSetHeightAndFlags(pParse, A.pExpr); 1161 }else{ 1162 sqlite3ExprListDelete(pParse->db, Y); 1163 } 1164 exprNot(pParse, N, &A); 1165 } 1166 A.zEnd = &E.z[E.n]; 1167 } 1168 expr(A) ::= LP(B) select(X) RP(E). { 1169 spanSet(&A,&B,&E); /*A-overwrites-B*/ 1170 A.pExpr = sqlite3PExpr(pParse, TK_SELECT, 0, 0); 1171 sqlite3PExprAddSelect(pParse, A.pExpr, X); 1172 } 1173 expr(A) ::= expr(A) in_op(N) LP select(Y) RP(E). [IN] { 1174 A.pExpr = sqlite3PExpr(pParse, TK_IN, A.pExpr, 0); 1175 sqlite3PExprAddSelect(pParse, A.pExpr, Y); 1176 exprNot(pParse, N, &A); 1177 A.zEnd = &E.z[E.n]; 1178 } 1179 expr(A) ::= expr(A) in_op(N) nm(Y) dbnm(Z) paren_exprlist(E). [IN] { 1180 SrcList *pSrc = sqlite3SrcListAppend(pParse->db, 0,&Y,&Z); 1181 Select *pSelect = sqlite3SelectNew(pParse, 0,pSrc,0,0,0,0,0,0,0); 1182 if( E ) sqlite3SrcListFuncArgs(pParse, pSelect ? pSrc : 0, E); 1183 A.pExpr = sqlite3PExpr(pParse, TK_IN, A.pExpr, 0); 1184 sqlite3PExprAddSelect(pParse, A.pExpr, pSelect); 1185 exprNot(pParse, N, &A); 1186 A.zEnd = Z.z ? &Z.z[Z.n] : &Y.z[Y.n]; 1187 } 1188 expr(A) ::= EXISTS(B) LP select(Y) RP(E). { 1189 Expr *p; 1190 spanSet(&A,&B,&E); /*A-overwrites-B*/ 1191 p = A.pExpr = sqlite3PExpr(pParse, TK_EXISTS, 0, 0); 1192 sqlite3PExprAddSelect(pParse, p, Y); 1193 } 1194 %endif SQLITE_OMIT_SUBQUERY 1195 1196 /* CASE expressions */ 1197 expr(A) ::= CASE(C) case_operand(X) case_exprlist(Y) case_else(Z) END(E). { 1198 spanSet(&A,&C,&E); /*A-overwrites-C*/ 1199 A.pExpr = sqlite3PExpr(pParse, TK_CASE, X, 0); 1200 if( A.pExpr ){ 1201 A.pExpr->x.pList = Z ? sqlite3ExprListAppend(pParse,Y,Z) : Y; 1202 sqlite3ExprSetHeightAndFlags(pParse, A.pExpr); 1203 }else{ 1204 sqlite3ExprListDelete(pParse->db, Y); 1205 sqlite3ExprDelete(pParse->db, Z); 1206 } 1207 } 1208 %type case_exprlist {ExprList*} 1209 %destructor case_exprlist {sqlite3ExprListDelete(pParse->db, $$);} 1210 case_exprlist(A) ::= case_exprlist(A) WHEN expr(Y) THEN expr(Z). { 1211 A = sqlite3ExprListAppend(pParse,A, Y.pExpr); 1212 A = sqlite3ExprListAppend(pParse,A, Z.pExpr); 1213 } 1214 case_exprlist(A) ::= WHEN expr(Y) THEN expr(Z). { 1215 A = sqlite3ExprListAppend(pParse,0, Y.pExpr); 1216 A = sqlite3ExprListAppend(pParse,A, Z.pExpr); 1217 } 1218 %type case_else {Expr*} 1219 %destructor case_else {sqlite3ExprDelete(pParse->db, $$);} 1220 case_else(A) ::= ELSE expr(X). {A = X.pExpr;} 1221 case_else(A) ::= . {A = 0;} 1222 %type case_operand {Expr*} 1223 %destructor case_operand {sqlite3ExprDelete(pParse->db, $$);} 1224 case_operand(A) ::= expr(X). {A = X.pExpr; /*A-overwrites-X*/} 1225 case_operand(A) ::= . {A = 0;} 1226 1227 %type exprlist {ExprList*} 1228 %destructor exprlist {sqlite3ExprListDelete(pParse->db, $$);} 1229 %type nexprlist {ExprList*} 1230 %destructor nexprlist {sqlite3ExprListDelete(pParse->db, $$);} 1231 1232 exprlist(A) ::= nexprlist(A). 1233 exprlist(A) ::= . {A = 0;} 1234 nexprlist(A) ::= nexprlist(A) COMMA expr(Y). 1235 {A = sqlite3ExprListAppend(pParse,A,Y.pExpr);} 1236 nexprlist(A) ::= expr(Y). 1237 {A = sqlite3ExprListAppend(pParse,0,Y.pExpr); /*A-overwrites-Y*/} 1238 1239 %ifndef SQLITE_OMIT_SUBQUERY 1240 /* A paren_exprlist is an optional expression list contained inside 1241 ** of parenthesis */ 1242 %type paren_exprlist {ExprList*} 1243 %destructor paren_exprlist {sqlite3ExprListDelete(pParse->db, $$);} 1244 paren_exprlist(A) ::= . {A = 0;} 1245 paren_exprlist(A) ::= LP exprlist(X) RP. {A = X;} 1246 %endif SQLITE_OMIT_SUBQUERY 1247 1248 1249 ///////////////////////////// The CREATE INDEX command /////////////////////// 1250 // 1251 cmd ::= createkw(S) uniqueflag(U) INDEX ifnotexists(NE) nm(X) dbnm(D) 1252 ON nm(Y) LP sortlist(Z) RP where_opt(W). { 1253 sqlite3CreateIndex(pParse, &X, &D, 1254 sqlite3SrcListAppend(pParse->db,0,&Y,0), Z, U, 1255 &S, W, SQLITE_SO_ASC, NE, SQLITE_IDXTYPE_APPDEF); 1256 } 1257 1258 %type uniqueflag {int} 1259 uniqueflag(A) ::= UNIQUE. {A = OE_Abort;} 1260 uniqueflag(A) ::= . {A = OE_None;} 1261 1262 1263 // The eidlist non-terminal (Expression Id List) generates an ExprList 1264 // from a list of identifiers. The identifier names are in ExprList.a[].zName. 1265 // This list is stored in an ExprList rather than an IdList so that it 1266 // can be easily sent to sqlite3ColumnsExprList(). 1267 // 1268 // eidlist is grouped with CREATE INDEX because it used to be the non-terminal 1269 // used for the arguments to an index. That is just an historical accident. 1270 // 1271 // IMPORTANT COMPATIBILITY NOTE: Some prior versions of SQLite accepted 1272 // COLLATE clauses and ASC or DESC keywords on ID lists in inappropriate 1273 // places - places that might have been stored in the sqlite_master schema. 1274 // Those extra features were ignored. But because they might be in some 1275 // (busted) old databases, we need to continue parsing them when loading 1276 // historical schemas. 1277 // 1278 %type eidlist {ExprList*} 1279 %destructor eidlist {sqlite3ExprListDelete(pParse->db, $$);} 1280 %type eidlist_opt {ExprList*} 1281 %destructor eidlist_opt {sqlite3ExprListDelete(pParse->db, $$);} 1282 1283 %include { 1284 /* Add a single new term to an ExprList that is used to store a 1285 ** list of identifiers. Report an error if the ID list contains 1286 ** a COLLATE clause or an ASC or DESC keyword, except ignore the 1287 ** error while parsing a legacy schema. 1288 */ 1289 static ExprList *parserAddExprIdListTerm( 1290 Parse *pParse, 1291 ExprList *pPrior, 1292 Token *pIdToken, 1293 int hasCollate, 1294 int sortOrder 1295 ){ 1296 ExprList *p = sqlite3ExprListAppend(pParse, pPrior, 0); 1297 if( (hasCollate || sortOrder!=SQLITE_SO_UNDEFINED) 1298 && pParse->db->init.busy==0 1299 ){ 1300 sqlite3ErrorMsg(pParse, "syntax error after column name \"%.*s\"", 1301 pIdToken->n, pIdToken->z); 1302 } 1303 sqlite3ExprListSetName(pParse, p, pIdToken, 1); 1304 return p; 1305 } 1306 } // end %include 1307 1308 eidlist_opt(A) ::= . {A = 0;} 1309 eidlist_opt(A) ::= LP eidlist(X) RP. {A = X;} 1310 eidlist(A) ::= eidlist(A) COMMA nm(Y) collate(C) sortorder(Z). { 1311 A = parserAddExprIdListTerm(pParse, A, &Y, C, Z); 1312 } 1313 eidlist(A) ::= nm(Y) collate(C) sortorder(Z). { 1314 A = parserAddExprIdListTerm(pParse, 0, &Y, C, Z); /*A-overwrites-Y*/ 1315 } 1316 1317 %type collate {int} 1318 collate(C) ::= . {C = 0;} 1319 collate(C) ::= COLLATE ids. {C = 1;} 1320 1321 1322 ///////////////////////////// The DROP INDEX command ///////////////////////// 1323 // 1324 cmd ::= DROP INDEX ifexists(E) fullname(X). {sqlite3DropIndex(pParse, X, E);} 1325 1326 ///////////////////////////// The VACUUM command ///////////////////////////// 1327 // 1328 %ifndef SQLITE_OMIT_VACUUM 1329 %ifndef SQLITE_OMIT_ATTACH 1330 cmd ::= VACUUM. {sqlite3Vacuum(pParse,0);} 1331 cmd ::= VACUUM nm(X). {sqlite3Vacuum(pParse,&X);} 1332 %endif SQLITE_OMIT_ATTACH 1333 %endif SQLITE_OMIT_VACUUM 1334 1335 ///////////////////////////// The PRAGMA command ///////////////////////////// 1336 // 1337 %ifndef SQLITE_OMIT_PRAGMA 1338 cmd ::= PRAGMA nm(X) dbnm(Z). {sqlite3Pragma(pParse,&X,&Z,0,0);} 1339 cmd ::= PRAGMA nm(X) dbnm(Z) EQ nmnum(Y). {sqlite3Pragma(pParse,&X,&Z,&Y,0);} 1340 cmd ::= PRAGMA nm(X) dbnm(Z) LP nmnum(Y) RP. {sqlite3Pragma(pParse,&X,&Z,&Y,0);} 1341 cmd ::= PRAGMA nm(X) dbnm(Z) EQ minus_num(Y). 1342 {sqlite3Pragma(pParse,&X,&Z,&Y,1);} 1343 cmd ::= PRAGMA nm(X) dbnm(Z) LP minus_num(Y) RP. 1344 {sqlite3Pragma(pParse,&X,&Z,&Y,1);} 1345 1346 nmnum(A) ::= plus_num(A). 1347 nmnum(A) ::= nm(A). 1348 nmnum(A) ::= ON(A). 1349 nmnum(A) ::= DELETE(A). 1350 nmnum(A) ::= DEFAULT(A). 1351 %endif SQLITE_OMIT_PRAGMA 1352 %token_class number INTEGER|FLOAT. 1353 plus_num(A) ::= PLUS number(X). {A = X;} 1354 plus_num(A) ::= number(A). 1355 minus_num(A) ::= MINUS number(X). {A = X;} 1356 //////////////////////////// The CREATE TRIGGER command ///////////////////// 1357 1358 %ifndef SQLITE_OMIT_TRIGGER 1359 1360 cmd ::= createkw trigger_decl(A) BEGIN trigger_cmd_list(S) END(Z). { 1361 Token all; 1362 all.z = A.z; 1363 all.n = (int)(Z.z - A.z) + Z.n; 1364 sqlite3FinishTrigger(pParse, S, &all); 1365 } 1366 1367 trigger_decl(A) ::= temp(T) TRIGGER ifnotexists(NOERR) nm(B) dbnm(Z) 1368 trigger_time(C) trigger_event(D) 1369 ON fullname(E) foreach_clause when_clause(G). { 1370 sqlite3BeginTrigger(pParse, &B, &Z, C, D.a, D.b, E, G, T, NOERR); 1371 A = (Z.n==0?B:Z); /*A-overwrites-T*/ 1372 } 1373 1374 %type trigger_time {int} 1375 trigger_time(A) ::= BEFORE|AFTER(X). { A = @X; /*A-overwrites-X*/ } 1376 trigger_time(A) ::= INSTEAD OF. { A = TK_INSTEAD;} 1377 trigger_time(A) ::= . { A = TK_BEFORE; } 1378 1379 %type trigger_event {struct TrigEvent} 1380 %destructor trigger_event {sqlite3IdListDelete(pParse->db, $$.b);} 1381 trigger_event(A) ::= DELETE|INSERT(X). {A.a = @X; /*A-overwrites-X*/ A.b = 0;} 1382 trigger_event(A) ::= UPDATE(X). {A.a = @X; /*A-overwrites-X*/ A.b = 0;} 1383 trigger_event(A) ::= UPDATE OF idlist(X).{A.a = TK_UPDATE; A.b = X;} 1384 1385 foreach_clause ::= . 1386 foreach_clause ::= FOR EACH ROW. 1387 1388 %type when_clause {Expr*} 1389 %destructor when_clause {sqlite3ExprDelete(pParse->db, $$);} 1390 when_clause(A) ::= . { A = 0; } 1391 when_clause(A) ::= WHEN expr(X). { A = X.pExpr; } 1392 1393 %type trigger_cmd_list {TriggerStep*} 1394 %destructor trigger_cmd_list {sqlite3DeleteTriggerStep(pParse->db, $$);} 1395 trigger_cmd_list(A) ::= trigger_cmd_list(A) trigger_cmd(X) SEMI. { 1396 assert( A!=0 ); 1397 A->pLast->pNext = X; 1398 A->pLast = X; 1399 } 1400 trigger_cmd_list(A) ::= trigger_cmd(A) SEMI. { 1401 assert( A!=0 ); 1402 A->pLast = A; 1403 } 1404 1405 // Disallow qualified table names on INSERT, UPDATE, and DELETE statements 1406 // within a trigger. The table to INSERT, UPDATE, or DELETE is always in 1407 // the same database as the table that the trigger fires on. 1408 // 1409 %type trnm {Token} 1410 trnm(A) ::= nm(A). 1411 trnm(A) ::= nm DOT nm(X). { 1412 A = X; 1413 sqlite3ErrorMsg(pParse, 1414 "qualified table names are not allowed on INSERT, UPDATE, and DELETE " 1415 "statements within triggers"); 1416 } 1417 1418 // Disallow the INDEX BY and NOT INDEXED clauses on UPDATE and DELETE 1419 // statements within triggers. We make a specific error message for this 1420 // since it is an exception to the default grammar rules. 1421 // 1422 tridxby ::= . 1423 tridxby ::= INDEXED BY nm. { 1424 sqlite3ErrorMsg(pParse, 1425 "the INDEXED BY clause is not allowed on UPDATE or DELETE statements " 1426 "within triggers"); 1427 } 1428 tridxby ::= NOT INDEXED. { 1429 sqlite3ErrorMsg(pParse, 1430 "the NOT INDEXED clause is not allowed on UPDATE or DELETE statements " 1431 "within triggers"); 1432 } 1433 1434 1435 1436 %type trigger_cmd {TriggerStep*} 1437 %destructor trigger_cmd {sqlite3DeleteTriggerStep(pParse->db, $$);} 1438 // UPDATE 1439 trigger_cmd(A) ::= 1440 UPDATE orconf(R) trnm(X) tridxby SET setlist(Y) where_opt(Z). 1441 {A = sqlite3TriggerUpdateStep(pParse->db, &X, Y, Z, R);} 1442 1443 // INSERT 1444 trigger_cmd(A) ::= insert_cmd(R) INTO trnm(X) idlist_opt(F) select(S). 1445 {A = sqlite3TriggerInsertStep(pParse->db, &X, F, S, R);/*A-overwrites-R*/} 1446 1447 // DELETE 1448 trigger_cmd(A) ::= DELETE FROM trnm(X) tridxby where_opt(Y). 1449 {A = sqlite3TriggerDeleteStep(pParse->db, &X, Y);} 1450 1451 // SELECT 1452 trigger_cmd(A) ::= select(X). 1453 {A = sqlite3TriggerSelectStep(pParse->db, X); /*A-overwrites-X*/} 1454 1455 // The special RAISE expression that may occur in trigger programs 1456 expr(A) ::= RAISE(X) LP IGNORE RP(Y). { 1457 spanSet(&A,&X,&Y); /*A-overwrites-X*/ 1458 A.pExpr = sqlite3PExpr(pParse, TK_RAISE, 0, 0); 1459 if( A.pExpr ){ 1460 A.pExpr->affinity = OE_Ignore; 1461 } 1462 } 1463 expr(A) ::= RAISE(X) LP raisetype(T) COMMA nm(Z) RP(Y). { 1464 spanSet(&A,&X,&Y); /*A-overwrites-X*/ 1465 A.pExpr = sqlite3ExprAlloc(pParse->db, TK_RAISE, &Z, 1); 1466 if( A.pExpr ) { 1467 A.pExpr->affinity = (char)T; 1468 } 1469 } 1470 %endif !SQLITE_OMIT_TRIGGER 1471 1472 %type raisetype {int} 1473 raisetype(A) ::= ROLLBACK. {A = OE_Rollback;} 1474 raisetype(A) ::= ABORT. {A = OE_Abort;} 1475 raisetype(A) ::= FAIL. {A = OE_Fail;} 1476 1477 1478 //////////////////////// DROP TRIGGER statement ////////////////////////////// 1479 %ifndef SQLITE_OMIT_TRIGGER 1480 cmd ::= DROP TRIGGER ifexists(NOERR) fullname(X). { 1481 sqlite3DropTrigger(pParse,X,NOERR); 1482 } 1483 %endif !SQLITE_OMIT_TRIGGER 1484 1485 //////////////////////// ATTACH DATABASE file AS name ///////////////////////// 1486 %ifndef SQLITE_OMIT_ATTACH 1487 cmd ::= ATTACH database_kw_opt expr(F) AS expr(D) key_opt(K). { 1488 sqlite3Attach(pParse, F.pExpr, D.pExpr, K); 1489 } 1490 cmd ::= DETACH database_kw_opt expr(D). { 1491 sqlite3Detach(pParse, D.pExpr); 1492 } 1493 1494 %type key_opt {Expr*} 1495 %destructor key_opt {sqlite3ExprDelete(pParse->db, $$);} 1496 key_opt(A) ::= . { A = 0; } 1497 key_opt(A) ::= KEY expr(X). { A = X.pExpr; } 1498 1499 database_kw_opt ::= DATABASE. 1500 database_kw_opt ::= . 1501 %endif SQLITE_OMIT_ATTACH 1502 1503 ////////////////////////// REINDEX collation ////////////////////////////////// 1504 %ifndef SQLITE_OMIT_REINDEX 1505 cmd ::= REINDEX. {sqlite3Reindex(pParse, 0, 0);} 1506 cmd ::= REINDEX nm(X) dbnm(Y). {sqlite3Reindex(pParse, &X, &Y);} 1507 %endif SQLITE_OMIT_REINDEX 1508 1509 /////////////////////////////////// ANALYZE /////////////////////////////////// 1510 %ifndef SQLITE_OMIT_ANALYZE 1511 cmd ::= ANALYZE. {sqlite3Analyze(pParse, 0, 0);} 1512 cmd ::= ANALYZE nm(X) dbnm(Y). {sqlite3Analyze(pParse, &X, &Y);} 1513 %endif 1514 1515 //////////////////////// ALTER TABLE table ... //////////////////////////////// 1516 %ifndef SQLITE_OMIT_ALTERTABLE 1517 cmd ::= ALTER TABLE fullname(X) RENAME TO nm(Z). { 1518 sqlite3AlterRenameTable(pParse,X,&Z); 1519 } 1520 cmd ::= ALTER TABLE add_column_fullname 1521 ADD kwcolumn_opt columnname(Y) carglist. { 1522 Y.n = (int)(pParse->sLastToken.z-Y.z) + pParse->sLastToken.n; 1523 sqlite3AlterFinishAddColumn(pParse, &Y); 1524 } 1525 add_column_fullname ::= fullname(X). { 1526 disableLookaside(pParse); 1527 sqlite3AlterBeginAddColumn(pParse, X); 1528 } 1529 kwcolumn_opt ::= . 1530 kwcolumn_opt ::= COLUMNKW. 1531 %endif SQLITE_OMIT_ALTERTABLE 1532 1533 //////////////////////// CREATE VIRTUAL TABLE ... ///////////////////////////// 1534 %ifndef SQLITE_OMIT_VIRTUALTABLE 1535 cmd ::= create_vtab. {sqlite3VtabFinishParse(pParse,0);} 1536 cmd ::= create_vtab LP vtabarglist RP(X). {sqlite3VtabFinishParse(pParse,&X);} 1537 create_vtab ::= createkw VIRTUAL TABLE ifnotexists(E) 1538 nm(X) dbnm(Y) USING nm(Z). { 1539 sqlite3VtabBeginParse(pParse, &X, &Y, &Z, E); 1540 } 1541 vtabarglist ::= vtabarg. 1542 vtabarglist ::= vtabarglist COMMA vtabarg. 1543 vtabarg ::= . {sqlite3VtabArgInit(pParse);} 1544 vtabarg ::= vtabarg vtabargtoken. 1545 vtabargtoken ::= ANY(X). {sqlite3VtabArgExtend(pParse,&X);} 1546 vtabargtoken ::= lp anylist RP(X). {sqlite3VtabArgExtend(pParse,&X);} 1547 lp ::= LP(X). {sqlite3VtabArgExtend(pParse,&X);} 1548 anylist ::= . 1549 anylist ::= anylist LP anylist RP. 1550 anylist ::= anylist ANY. 1551 %endif SQLITE_OMIT_VIRTUALTABLE 1552 1553 1554 //////////////////////// COMMON TABLE EXPRESSIONS //////////////////////////// 1555 %type with {With*} 1556 %type wqlist {With*} 1557 %destructor with {sqlite3WithDelete(pParse->db, $$);} 1558 %destructor wqlist {sqlite3WithDelete(pParse->db, $$);} 1559 1560 with(A) ::= . {A = 0;} 1561 %ifndef SQLITE_OMIT_CTE 1562 with(A) ::= WITH wqlist(W). { A = W; } 1563 with(A) ::= WITH RECURSIVE wqlist(W). { A = W; } 1564 1565 wqlist(A) ::= nm(X) eidlist_opt(Y) AS LP select(Z) RP. { 1566 A = sqlite3WithAdd(pParse, 0, &X, Y, Z); /*A-overwrites-X*/ 1567 } 1568 wqlist(A) ::= wqlist(A) COMMA nm(X) eidlist_opt(Y) AS LP select(Z) RP. { 1569 A = sqlite3WithAdd(pParse, A, &X, Y, Z); 1570 } 1571 %endif SQLITE_OMIT_CTE 1572