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