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 ** @(#) $Id: parse.y,v 1.215 2007/02/02 12:44:37 drh Exp $ 18 */ 19 20 // All token codes are small integers with #defines that begin with "TK_" 21 %token_prefix TK_ 22 23 // The type of the data attached to each token is Token. This is also the 24 // default type for non-terminals. 25 // 26 %token_type {Token} 27 %default_type {Token} 28 29 // The generated parser function takes a 4th argument as follows: 30 %extra_argument {Parse *pParse} 31 32 // This code runs whenever there is a syntax error 33 // 34 %syntax_error { 35 if( !pParse->parseError ){ 36 if( TOKEN.z[0] ){ 37 sqlite3ErrorMsg(pParse, "near \"%T\": syntax error", &TOKEN); 38 }else{ 39 sqlite3ErrorMsg(pParse, "incomplete SQL statement"); 40 } 41 pParse->parseError = 1; 42 } 43 } 44 %stack_overflow { 45 sqlite3ErrorMsg(pParse, "parser stack overflow"); 46 pParse->parseError = 1; 47 } 48 49 // The name of the generated procedure that implements the parser 50 // is as follows: 51 %name sqlite3Parser 52 53 // The following text is included near the beginning of the C source 54 // code file that implements the parser. 55 // 56 %include { 57 #include "sqliteInt.h" 58 #include "parse.h" 59 60 /* 61 ** An instance of this structure holds information about the 62 ** LIMIT clause of a SELECT statement. 63 */ 64 struct LimitVal { 65 Expr *pLimit; /* The LIMIT expression. NULL if there is no limit */ 66 Expr *pOffset; /* The OFFSET expression. NULL if there is none */ 67 }; 68 69 /* 70 ** An instance of this structure is used to store the LIKE, 71 ** GLOB, NOT LIKE, and NOT GLOB operators. 72 */ 73 struct LikeOp { 74 Token eOperator; /* "like" or "glob" or "regexp" */ 75 int not; /* True if the NOT keyword is present */ 76 }; 77 78 /* 79 ** An instance of the following structure describes the event of a 80 ** TRIGGER. "a" is the event type, one of TK_UPDATE, TK_INSERT, 81 ** TK_DELETE, or TK_INSTEAD. If the event is of the form 82 ** 83 ** UPDATE ON (a,b,c) 84 ** 85 ** Then the "b" IdList records the list "a,b,c". 86 */ 87 struct TrigEvent { int a; IdList * b; }; 88 89 /* 90 ** An instance of this structure holds the ATTACH key and the key type. 91 */ 92 struct AttachKey { int type; Token key; }; 93 94 } // end %include 95 96 // Input is a single SQL command 97 input ::= cmdlist. 98 cmdlist ::= cmdlist ecmd. 99 cmdlist ::= ecmd. 100 cmdx ::= cmd. { sqlite3FinishCoding(pParse); } 101 ecmd ::= SEMI. 102 ecmd ::= explain cmdx SEMI. 103 explain ::= . { sqlite3BeginParse(pParse, 0); } 104 %ifndef SQLITE_OMIT_EXPLAIN 105 explain ::= EXPLAIN. { sqlite3BeginParse(pParse, 1); } 106 explain ::= EXPLAIN QUERY PLAN. { sqlite3BeginParse(pParse, 2); } 107 %endif SQLITE_OMIT_EXPLAIN 108 109 ///////////////////// Begin and end transactions. //////////////////////////// 110 // 111 112 cmd ::= BEGIN transtype(Y) trans_opt. {sqlite3BeginTransaction(pParse, Y);} 113 trans_opt ::= . 114 trans_opt ::= TRANSACTION. 115 trans_opt ::= TRANSACTION nm. 116 %type transtype {int} 117 transtype(A) ::= . {A = TK_DEFERRED;} 118 transtype(A) ::= DEFERRED(X). {A = @X;} 119 transtype(A) ::= IMMEDIATE(X). {A = @X;} 120 transtype(A) ::= EXCLUSIVE(X). {A = @X;} 121 cmd ::= COMMIT trans_opt. {sqlite3CommitTransaction(pParse);} 122 cmd ::= END trans_opt. {sqlite3CommitTransaction(pParse);} 123 cmd ::= ROLLBACK trans_opt. {sqlite3RollbackTransaction(pParse);} 124 125 ///////////////////// The CREATE TABLE statement //////////////////////////// 126 // 127 cmd ::= create_table create_table_args. 128 create_table ::= CREATE temp(T) TABLE ifnotexists(E) nm(Y) dbnm(Z). { 129 sqlite3StartTable(pParse,&Y,&Z,T,0,0,E); 130 } 131 %type ifnotexists {int} 132 ifnotexists(A) ::= . {A = 0;} 133 ifnotexists(A) ::= IF NOT EXISTS. {A = 1;} 134 %type temp {int} 135 %ifndef SQLITE_OMIT_TEMPDB 136 temp(A) ::= TEMP. {A = 1;} 137 %endif SQLITE_OMIT_TEMPDB 138 temp(A) ::= . {A = 0;} 139 create_table_args ::= LP columnlist conslist_opt(X) RP(Y). { 140 sqlite3EndTable(pParse,&X,&Y,0); 141 } 142 create_table_args ::= AS select(S). { 143 sqlite3EndTable(pParse,0,0,S); 144 sqlite3SelectDelete(S); 145 } 146 columnlist ::= columnlist COMMA column. 147 columnlist ::= column. 148 149 // A "column" is a complete description of a single column in a 150 // CREATE TABLE statement. This includes the column name, its 151 // datatype, and other keywords such as PRIMARY KEY, UNIQUE, REFERENCES, 152 // NOT NULL and so forth. 153 // 154 column(A) ::= columnid(X) type carglist. { 155 A.z = X.z; 156 A.n = (pParse->sLastToken.z-X.z) + pParse->sLastToken.n; 157 } 158 columnid(A) ::= nm(X). { 159 sqlite3AddColumn(pParse,&X); 160 A = X; 161 } 162 163 164 // An IDENTIFIER can be a generic identifier, or one of several 165 // keywords. Any non-standard keyword can also be an identifier. 166 // 167 %type id {Token} 168 id(A) ::= ID(X). {A = X;} 169 170 // The following directive causes tokens ABORT, AFTER, ASC, etc. to 171 // fallback to ID if they will not parse as their original value. 172 // This obviates the need for the "id" nonterminal. 173 // 174 %fallback ID 175 ABORT AFTER ANALYZE ASC ATTACH BEFORE BEGIN CASCADE CAST CONFLICT 176 DATABASE DEFERRED DESC DETACH EACH END EXCLUSIVE EXPLAIN FAIL FOR 177 IGNORE IMMEDIATE INITIALLY INSTEAD LIKE_KW MATCH PLAN QUERY KEY 178 OF OFFSET PRAGMA RAISE REPLACE RESTRICT ROW STATEMENT 179 TEMP TRIGGER VACUUM VIEW VIRTUAL 180 %ifdef SQLITE_OMIT_COMPOUND_SELECT 181 EXCEPT INTERSECT UNION 182 %endif SQLITE_OMIT_COMPOUND_SELECT 183 REINDEX RENAME CTIME_KW IF 184 . 185 %wildcard ANY. 186 187 // Define operator precedence early so that this is the first occurance 188 // of the operator tokens in the grammer. Keeping the operators together 189 // causes them to be assigned integer values that are close together, 190 // which keeps parser tables smaller. 191 // 192 // The token values assigned to these symbols is determined by the order 193 // in which lemon first sees them. It must be the case that ISNULL/NOTNULL, 194 // NE/EQ, GT/LE, and GE/LT are separated by only a single value. See 195 // the sqlite3ExprIfFalse() routine for additional information on this 196 // constraint. 197 // 198 %left OR. 199 %left AND. 200 %right NOT. 201 %left IS MATCH LIKE_KW BETWEEN IN ISNULL NOTNULL NE EQ. 202 %left GT LE LT GE. 203 %right ESCAPE. 204 %left BITAND BITOR LSHIFT RSHIFT. 205 %left PLUS MINUS. 206 %left STAR SLASH REM. 207 %left CONCAT. 208 %left COLLATE. 209 %right UMINUS UPLUS BITNOT. 210 211 // And "ids" is an identifer-or-string. 212 // 213 %type ids {Token} 214 ids(A) ::= ID|STRING(X). {A = X;} 215 216 // The name of a column or table can be any of the following: 217 // 218 %type nm {Token} 219 nm(A) ::= ID(X). {A = X;} 220 nm(A) ::= STRING(X). {A = X;} 221 nm(A) ::= JOIN_KW(X). {A = X;} 222 223 // A typetoken is really one or more tokens that form a type name such 224 // as can be found after the column name in a CREATE TABLE statement. 225 // Multiple tokens are concatenated to form the value of the typetoken. 226 // 227 %type typetoken {Token} 228 type ::= . 229 type ::= typetoken(X). {sqlite3AddColumnType(pParse,&X);} 230 typetoken(A) ::= typename(X). {A = X;} 231 typetoken(A) ::= typename(X) LP signed RP(Y). { 232 A.z = X.z; 233 A.n = &Y.z[Y.n] - X.z; 234 } 235 typetoken(A) ::= typename(X) LP signed COMMA signed RP(Y). { 236 A.z = X.z; 237 A.n = &Y.z[Y.n] - X.z; 238 } 239 %type typename {Token} 240 typename(A) ::= ids(X). {A = X;} 241 typename(A) ::= typename(X) ids(Y). {A.z=X.z; A.n=Y.n+(Y.z-X.z);} 242 %type signed {int} 243 signed(A) ::= plus_num(X). { A = atoi((char*)X.z); } 244 signed(A) ::= minus_num(X). { A = -atoi((char*)X.z); } 245 246 // "carglist" is a list of additional constraints that come after the 247 // column name and column type in a CREATE TABLE statement. 248 // 249 carglist ::= carglist carg. 250 carglist ::= . 251 carg ::= CONSTRAINT nm ccons. 252 carg ::= ccons. 253 ccons ::= DEFAULT term(X). {sqlite3AddDefaultValue(pParse,X);} 254 ccons ::= DEFAULT LP expr(X) RP. {sqlite3AddDefaultValue(pParse,X);} 255 ccons ::= DEFAULT PLUS term(X). {sqlite3AddDefaultValue(pParse,X);} 256 ccons ::= DEFAULT MINUS term(X). { 257 Expr *p = sqlite3Expr(TK_UMINUS, X, 0, 0); 258 sqlite3AddDefaultValue(pParse,p); 259 } 260 ccons ::= DEFAULT id(X). { 261 Expr *p = sqlite3Expr(TK_STRING, 0, 0, &X); 262 sqlite3AddDefaultValue(pParse,p); 263 } 264 265 // In addition to the type name, we also care about the primary key and 266 // UNIQUE constraints. 267 // 268 ccons ::= NULL onconf. 269 ccons ::= NOT NULL onconf(R). {sqlite3AddNotNull(pParse, R);} 270 ccons ::= PRIMARY KEY sortorder(Z) onconf(R) autoinc(I). 271 {sqlite3AddPrimaryKey(pParse,0,R,I,Z);} 272 ccons ::= UNIQUE onconf(R). {sqlite3CreateIndex(pParse,0,0,0,0,R,0,0,0,0);} 273 ccons ::= CHECK LP expr(X) RP. {sqlite3AddCheckConstraint(pParse,X);} 274 ccons ::= REFERENCES nm(T) idxlist_opt(TA) refargs(R). 275 {sqlite3CreateForeignKey(pParse,0,&T,TA,R);} 276 ccons ::= defer_subclause(D). {sqlite3DeferForeignKey(pParse,D);} 277 ccons ::= COLLATE id(C). {sqlite3AddCollateType(pParse, (char*)C.z, C.n);} 278 279 // The optional AUTOINCREMENT keyword 280 %type autoinc {int} 281 autoinc(X) ::= . {X = 0;} 282 autoinc(X) ::= AUTOINCR. {X = 1;} 283 284 // The next group of rules parses the arguments to a REFERENCES clause 285 // that determine if the referential integrity checking is deferred or 286 // or immediate and which determine what action to take if a ref-integ 287 // check fails. 288 // 289 %type refargs {int} 290 refargs(A) ::= . { A = OE_Restrict * 0x010101; } 291 refargs(A) ::= refargs(X) refarg(Y). { A = (X & Y.mask) | Y.value; } 292 %type refarg {struct {int value; int mask;}} 293 refarg(A) ::= MATCH nm. { A.value = 0; A.mask = 0x000000; } 294 refarg(A) ::= ON DELETE refact(X). { A.value = X; A.mask = 0x0000ff; } 295 refarg(A) ::= ON UPDATE refact(X). { A.value = X<<8; A.mask = 0x00ff00; } 296 refarg(A) ::= ON INSERT refact(X). { A.value = X<<16; A.mask = 0xff0000; } 297 %type refact {int} 298 refact(A) ::= SET NULL. { A = OE_SetNull; } 299 refact(A) ::= SET DEFAULT. { A = OE_SetDflt; } 300 refact(A) ::= CASCADE. { A = OE_Cascade; } 301 refact(A) ::= RESTRICT. { A = OE_Restrict; } 302 %type defer_subclause {int} 303 defer_subclause(A) ::= NOT DEFERRABLE init_deferred_pred_opt(X). {A = X;} 304 defer_subclause(A) ::= DEFERRABLE init_deferred_pred_opt(X). {A = X;} 305 %type init_deferred_pred_opt {int} 306 init_deferred_pred_opt(A) ::= . {A = 0;} 307 init_deferred_pred_opt(A) ::= INITIALLY DEFERRED. {A = 1;} 308 init_deferred_pred_opt(A) ::= INITIALLY IMMEDIATE. {A = 0;} 309 310 // For the time being, the only constraint we care about is the primary 311 // key and UNIQUE. Both create indices. 312 // 313 conslist_opt(A) ::= . {A.n = 0; A.z = 0;} 314 conslist_opt(A) ::= COMMA(X) conslist. {A = X;} 315 conslist ::= conslist COMMA tcons. 316 conslist ::= conslist tcons. 317 conslist ::= tcons. 318 tcons ::= CONSTRAINT nm. 319 tcons ::= PRIMARY KEY LP idxlist(X) autoinc(I) RP onconf(R). 320 {sqlite3AddPrimaryKey(pParse,X,R,I,0);} 321 tcons ::= UNIQUE LP idxlist(X) RP onconf(R). 322 {sqlite3CreateIndex(pParse,0,0,0,X,R,0,0,0,0);} 323 tcons ::= CHECK LP expr(E) RP onconf. {sqlite3AddCheckConstraint(pParse,E);} 324 tcons ::= FOREIGN KEY LP idxlist(FA) RP 325 REFERENCES nm(T) idxlist_opt(TA) refargs(R) defer_subclause_opt(D). { 326 sqlite3CreateForeignKey(pParse, FA, &T, TA, R); 327 sqlite3DeferForeignKey(pParse, D); 328 } 329 %type defer_subclause_opt {int} 330 defer_subclause_opt(A) ::= . {A = 0;} 331 defer_subclause_opt(A) ::= defer_subclause(X). {A = X;} 332 333 // The following is a non-standard extension that allows us to declare the 334 // default behavior when there is a constraint conflict. 335 // 336 %type onconf {int} 337 %type orconf {int} 338 %type resolvetype {int} 339 onconf(A) ::= . {A = OE_Default;} 340 onconf(A) ::= ON CONFLICT resolvetype(X). {A = X;} 341 orconf(A) ::= . {A = OE_Default;} 342 orconf(A) ::= OR resolvetype(X). {A = X;} 343 resolvetype(A) ::= raisetype(X). {A = X;} 344 resolvetype(A) ::= IGNORE. {A = OE_Ignore;} 345 resolvetype(A) ::= REPLACE. {A = OE_Replace;} 346 347 ////////////////////////// The DROP TABLE ///////////////////////////////////// 348 // 349 cmd ::= DROP TABLE ifexists(E) fullname(X). { 350 sqlite3DropTable(pParse, X, 0, E); 351 } 352 %type ifexists {int} 353 ifexists(A) ::= IF EXISTS. {A = 1;} 354 ifexists(A) ::= . {A = 0;} 355 356 ///////////////////// The CREATE VIEW statement ///////////////////////////// 357 // 358 %ifndef SQLITE_OMIT_VIEW 359 cmd ::= CREATE(X) temp(T) VIEW ifnotexists(E) nm(Y) dbnm(Z) AS select(S). { 360 sqlite3CreateView(pParse, &X, &Y, &Z, S, T, E); 361 } 362 cmd ::= DROP VIEW ifexists(E) fullname(X). { 363 sqlite3DropTable(pParse, X, 1, E); 364 } 365 %endif SQLITE_OMIT_VIEW 366 367 //////////////////////// The SELECT statement ///////////////////////////////// 368 // 369 cmd ::= select(X). { 370 sqlite3Select(pParse, X, SRT_Callback, 0, 0, 0, 0, 0); 371 sqlite3SelectDelete(X); 372 } 373 374 %type select {Select*} 375 %destructor select {sqlite3SelectDelete($$);} 376 %type oneselect {Select*} 377 %destructor oneselect {sqlite3SelectDelete($$);} 378 379 select(A) ::= oneselect(X). {A = X;} 380 %ifndef SQLITE_OMIT_COMPOUND_SELECT 381 select(A) ::= select(X) multiselect_op(Y) oneselect(Z). { 382 if( Z ){ 383 Z->op = Y; 384 Z->pPrior = X; 385 } 386 A = Z; 387 } 388 %type multiselect_op {int} 389 multiselect_op(A) ::= UNION(OP). {A = @OP;} 390 multiselect_op(A) ::= UNION ALL. {A = TK_ALL;} 391 multiselect_op(A) ::= EXCEPT|INTERSECT(OP). {A = @OP;} 392 %endif SQLITE_OMIT_COMPOUND_SELECT 393 oneselect(A) ::= SELECT distinct(D) selcollist(W) from(X) where_opt(Y) 394 groupby_opt(P) having_opt(Q) orderby_opt(Z) limit_opt(L). { 395 A = sqlite3SelectNew(W,X,Y,P,Q,Z,D,L.pLimit,L.pOffset); 396 } 397 398 // The "distinct" nonterminal is true (1) if the DISTINCT keyword is 399 // present and false (0) if it is not. 400 // 401 %type distinct {int} 402 distinct(A) ::= DISTINCT. {A = 1;} 403 distinct(A) ::= ALL. {A = 0;} 404 distinct(A) ::= . {A = 0;} 405 406 // selcollist is a list of expressions that are to become the return 407 // values of the SELECT statement. The "*" in statements like 408 // "SELECT * FROM ..." is encoded as a special expression with an 409 // opcode of TK_ALL. 410 // 411 %type selcollist {ExprList*} 412 %destructor selcollist {sqlite3ExprListDelete($$);} 413 %type sclp {ExprList*} 414 %destructor sclp {sqlite3ExprListDelete($$);} 415 sclp(A) ::= selcollist(X) COMMA. {A = X;} 416 sclp(A) ::= . {A = 0;} 417 selcollist(A) ::= sclp(P) expr(X) as(Y). { 418 A = sqlite3ExprListAppend(P,X,Y.n?&Y:0); 419 } 420 selcollist(A) ::= sclp(P) STAR. { 421 A = sqlite3ExprListAppend(P, sqlite3Expr(TK_ALL, 0, 0, 0), 0); 422 } 423 selcollist(A) ::= sclp(P) nm(X) DOT STAR. { 424 Expr *pRight = sqlite3Expr(TK_ALL, 0, 0, 0); 425 Expr *pLeft = sqlite3Expr(TK_ID, 0, 0, &X); 426 A = sqlite3ExprListAppend(P, sqlite3Expr(TK_DOT, pLeft, pRight, 0), 0); 427 } 428 429 // An option "AS <id>" phrase that can follow one of the expressions that 430 // define the result set, or one of the tables in the FROM clause. 431 // 432 %type as {Token} 433 as(X) ::= AS nm(Y). {X = Y;} 434 as(X) ::= ids(Y). {X = Y;} 435 as(X) ::= . {X.n = 0;} 436 437 438 %type seltablist {SrcList*} 439 %destructor seltablist {sqlite3SrcListDelete($$);} 440 %type stl_prefix {SrcList*} 441 %destructor stl_prefix {sqlite3SrcListDelete($$);} 442 %type from {SrcList*} 443 %destructor from {sqlite3SrcListDelete($$);} 444 445 // A complete FROM clause. 446 // 447 from(A) ::= . {A = sqliteMalloc(sizeof(*A));} 448 from(A) ::= FROM seltablist(X). { 449 A = X; 450 sqlite3SrcListShiftJoinType(A); 451 } 452 453 // "seltablist" is a "Select Table List" - the content of the FROM clause 454 // in a SELECT statement. "stl_prefix" is a prefix of this list. 455 // 456 stl_prefix(A) ::= seltablist(X) joinop(Y). { 457 A = X; 458 if( A && A->nSrc>0 ) A->a[A->nSrc-1].jointype = Y; 459 } 460 stl_prefix(A) ::= . {A = 0;} 461 seltablist(A) ::= stl_prefix(X) nm(Y) dbnm(D) as(Z) on_opt(N) using_opt(U). { 462 A = sqlite3SrcListAppendFromTerm(X,&Y,&D,&Z,0,N,U); 463 } 464 %ifndef SQLITE_OMIT_SUBQUERY 465 seltablist(A) ::= stl_prefix(X) LP seltablist_paren(S) RP 466 as(Z) on_opt(N) using_opt(U). { 467 A = sqlite3SrcListAppendFromTerm(X,0,0,&Z,S,N,U); 468 } 469 470 // A seltablist_paren nonterminal represents anything in a FROM that 471 // is contained inside parentheses. This can be either a subquery or 472 // a grouping of table and subqueries. 473 // 474 %type seltablist_paren {Select*} 475 %destructor seltablist_paren {sqlite3SelectDelete($$);} 476 seltablist_paren(A) ::= select(S). {A = S;} 477 seltablist_paren(A) ::= seltablist(F). { 478 sqlite3SrcListShiftJoinType(F); 479 A = sqlite3SelectNew(0,F,0,0,0,0,0,0,0); 480 } 481 %endif SQLITE_OMIT_SUBQUERY 482 483 %type dbnm {Token} 484 dbnm(A) ::= . {A.z=0; A.n=0;} 485 dbnm(A) ::= DOT nm(X). {A = X;} 486 487 %type fullname {SrcList*} 488 %destructor fullname {sqlite3SrcListDelete($$);} 489 fullname(A) ::= nm(X) dbnm(Y). {A = sqlite3SrcListAppend(0,&X,&Y);} 490 491 %type joinop {int} 492 %type joinop2 {int} 493 joinop(X) ::= COMMA|JOIN. { X = JT_INNER; } 494 joinop(X) ::= JOIN_KW(A) JOIN. { X = sqlite3JoinType(pParse,&A,0,0); } 495 joinop(X) ::= JOIN_KW(A) nm(B) JOIN. { X = sqlite3JoinType(pParse,&A,&B,0); } 496 joinop(X) ::= JOIN_KW(A) nm(B) nm(C) JOIN. 497 { X = sqlite3JoinType(pParse,&A,&B,&C); } 498 499 %type on_opt {Expr*} 500 %destructor on_opt {sqlite3ExprDelete($$);} 501 on_opt(N) ::= ON expr(E). {N = E;} 502 on_opt(N) ::= . {N = 0;} 503 504 %type using_opt {IdList*} 505 %destructor using_opt {sqlite3IdListDelete($$);} 506 using_opt(U) ::= USING LP inscollist(L) RP. {U = L;} 507 using_opt(U) ::= . {U = 0;} 508 509 510 %type orderby_opt {ExprList*} 511 %destructor orderby_opt {sqlite3ExprListDelete($$);} 512 %type sortlist {ExprList*} 513 %destructor sortlist {sqlite3ExprListDelete($$);} 514 %type sortitem {Expr*} 515 %destructor sortitem {sqlite3ExprDelete($$);} 516 517 orderby_opt(A) ::= . {A = 0;} 518 orderby_opt(A) ::= ORDER BY sortlist(X). {A = X;} 519 sortlist(A) ::= sortlist(X) COMMA sortitem(Y) sortorder(Z). { 520 A = sqlite3ExprListAppend(X,Y,0); 521 if( A ) A->a[A->nExpr-1].sortOrder = Z; 522 } 523 sortlist(A) ::= sortitem(Y) sortorder(Z). { 524 A = sqlite3ExprListAppend(0,Y,0); 525 if( A && A->a ) A->a[0].sortOrder = Z; 526 } 527 sortitem(A) ::= expr(X). {A = X;} 528 529 %type sortorder {int} 530 531 sortorder(A) ::= ASC. {A = SQLITE_SO_ASC;} 532 sortorder(A) ::= DESC. {A = SQLITE_SO_DESC;} 533 sortorder(A) ::= . {A = SQLITE_SO_ASC;} 534 535 %type groupby_opt {ExprList*} 536 %destructor groupby_opt {sqlite3ExprListDelete($$);} 537 groupby_opt(A) ::= . {A = 0;} 538 groupby_opt(A) ::= GROUP BY exprlist(X). {A = X;} 539 540 %type having_opt {Expr*} 541 %destructor having_opt {sqlite3ExprDelete($$);} 542 having_opt(A) ::= . {A = 0;} 543 having_opt(A) ::= HAVING expr(X). {A = X;} 544 545 %type limit_opt {struct LimitVal} 546 %destructor limit_opt { 547 sqlite3ExprDelete($$.pLimit); 548 sqlite3ExprDelete($$.pOffset); 549 } 550 limit_opt(A) ::= . {A.pLimit = 0; A.pOffset = 0;} 551 limit_opt(A) ::= LIMIT expr(X). {A.pLimit = X; A.pOffset = 0;} 552 limit_opt(A) ::= LIMIT expr(X) OFFSET expr(Y). 553 {A.pLimit = X; A.pOffset = Y;} 554 limit_opt(A) ::= LIMIT expr(X) COMMA expr(Y). 555 {A.pOffset = X; A.pLimit = Y;} 556 557 /////////////////////////// The DELETE statement ///////////////////////////// 558 // 559 cmd ::= DELETE FROM fullname(X) where_opt(Y). {sqlite3DeleteFrom(pParse,X,Y);} 560 561 %type where_opt {Expr*} 562 %destructor where_opt {sqlite3ExprDelete($$);} 563 564 where_opt(A) ::= . {A = 0;} 565 where_opt(A) ::= WHERE expr(X). {A = X;} 566 567 ////////////////////////// The UPDATE command //////////////////////////////// 568 // 569 cmd ::= UPDATE orconf(R) fullname(X) SET setlist(Y) where_opt(Z). 570 {sqlite3Update(pParse,X,Y,Z,R);} 571 572 %type setlist {ExprList*} 573 %destructor setlist {sqlite3ExprListDelete($$);} 574 575 setlist(A) ::= setlist(Z) COMMA nm(X) EQ expr(Y). 576 {A = sqlite3ExprListAppend(Z,Y,&X);} 577 setlist(A) ::= nm(X) EQ expr(Y). {A = sqlite3ExprListAppend(0,Y,&X);} 578 579 ////////////////////////// The INSERT command ///////////////////////////////// 580 // 581 cmd ::= insert_cmd(R) INTO fullname(X) inscollist_opt(F) 582 VALUES LP itemlist(Y) RP. 583 {sqlite3Insert(pParse, X, Y, 0, F, R);} 584 cmd ::= insert_cmd(R) INTO fullname(X) inscollist_opt(F) select(S). 585 {sqlite3Insert(pParse, X, 0, S, F, R);} 586 cmd ::= insert_cmd(R) INTO fullname(X) inscollist_opt(F) DEFAULT VALUES. 587 {sqlite3Insert(pParse, X, 0, 0, F, R);} 588 589 %type insert_cmd {int} 590 insert_cmd(A) ::= INSERT orconf(R). {A = R;} 591 insert_cmd(A) ::= REPLACE. {A = OE_Replace;} 592 593 594 %type itemlist {ExprList*} 595 %destructor itemlist {sqlite3ExprListDelete($$);} 596 597 itemlist(A) ::= itemlist(X) COMMA expr(Y). {A = sqlite3ExprListAppend(X,Y,0);} 598 itemlist(A) ::= expr(X). {A = sqlite3ExprListAppend(0,X,0);} 599 600 %type inscollist_opt {IdList*} 601 %destructor inscollist_opt {sqlite3IdListDelete($$);} 602 %type inscollist {IdList*} 603 %destructor inscollist {sqlite3IdListDelete($$);} 604 605 inscollist_opt(A) ::= . {A = 0;} 606 inscollist_opt(A) ::= LP inscollist(X) RP. {A = X;} 607 inscollist(A) ::= inscollist(X) COMMA nm(Y). {A = sqlite3IdListAppend(X,&Y);} 608 inscollist(A) ::= nm(Y). {A = sqlite3IdListAppend(0,&Y);} 609 610 /////////////////////////// Expression Processing ///////////////////////////// 611 // 612 613 %type expr {Expr*} 614 %destructor expr {sqlite3ExprDelete($$);} 615 %type term {Expr*} 616 %destructor term {sqlite3ExprDelete($$);} 617 618 expr(A) ::= term(X). {A = X;} 619 expr(A) ::= LP(B) expr(X) RP(E). {A = X; sqlite3ExprSpan(A,&B,&E); } 620 term(A) ::= NULL(X). {A = sqlite3Expr(@X, 0, 0, &X);} 621 expr(A) ::= ID(X). {A = sqlite3Expr(TK_ID, 0, 0, &X);} 622 expr(A) ::= JOIN_KW(X). {A = sqlite3Expr(TK_ID, 0, 0, &X);} 623 expr(A) ::= nm(X) DOT nm(Y). { 624 Expr *temp1 = sqlite3Expr(TK_ID, 0, 0, &X); 625 Expr *temp2 = sqlite3Expr(TK_ID, 0, 0, &Y); 626 A = sqlite3Expr(TK_DOT, temp1, temp2, 0); 627 } 628 expr(A) ::= nm(X) DOT nm(Y) DOT nm(Z). { 629 Expr *temp1 = sqlite3Expr(TK_ID, 0, 0, &X); 630 Expr *temp2 = sqlite3Expr(TK_ID, 0, 0, &Y); 631 Expr *temp3 = sqlite3Expr(TK_ID, 0, 0, &Z); 632 Expr *temp4 = sqlite3Expr(TK_DOT, temp2, temp3, 0); 633 A = sqlite3Expr(TK_DOT, temp1, temp4, 0); 634 } 635 term(A) ::= INTEGER|FLOAT|BLOB(X). {A = sqlite3Expr(@X, 0, 0, &X);} 636 term(A) ::= STRING(X). {A = sqlite3Expr(@X, 0, 0, &X);} 637 expr(A) ::= REGISTER(X). {A = sqlite3RegisterExpr(pParse, &X);} 638 expr(A) ::= VARIABLE(X). { 639 Token *pToken = &X; 640 Expr *pExpr = A = sqlite3Expr(TK_VARIABLE, 0, 0, pToken); 641 sqlite3ExprAssignVarNumber(pParse, pExpr); 642 } 643 expr(A) ::= expr(E) COLLATE id(C). { 644 A = sqlite3ExprSetColl(pParse, E, &C); 645 } 646 %ifndef SQLITE_OMIT_CAST 647 expr(A) ::= CAST(X) LP expr(E) AS typetoken(T) RP(Y). { 648 A = sqlite3Expr(TK_CAST, E, 0, &T); 649 sqlite3ExprSpan(A,&X,&Y); 650 } 651 %endif SQLITE_OMIT_CAST 652 expr(A) ::= ID(X) LP distinct(D) exprlist(Y) RP(E). { 653 A = sqlite3ExprFunction(Y, &X); 654 sqlite3ExprSpan(A,&X,&E); 655 if( D && A ){ 656 A->flags |= EP_Distinct; 657 } 658 } 659 expr(A) ::= ID(X) LP STAR RP(E). { 660 A = sqlite3ExprFunction(0, &X); 661 sqlite3ExprSpan(A,&X,&E); 662 } 663 term(A) ::= CTIME_KW(OP). { 664 /* The CURRENT_TIME, CURRENT_DATE, and CURRENT_TIMESTAMP values are 665 ** treated as functions that return constants */ 666 A = sqlite3ExprFunction(0,&OP); 667 if( A ){ 668 A->op = TK_CONST_FUNC; 669 A->span = OP; 670 } 671 } 672 expr(A) ::= expr(X) AND(OP) expr(Y). {A = sqlite3Expr(@OP, X, Y, 0);} 673 expr(A) ::= expr(X) OR(OP) expr(Y). {A = sqlite3Expr(@OP, X, Y, 0);} 674 expr(A) ::= expr(X) LT|GT|GE|LE(OP) expr(Y). {A = sqlite3Expr(@OP, X, Y, 0);} 675 expr(A) ::= expr(X) EQ|NE(OP) expr(Y). {A = sqlite3Expr(@OP, X, Y, 0);} 676 expr(A) ::= expr(X) BITAND|BITOR|LSHIFT|RSHIFT(OP) expr(Y). 677 {A = sqlite3Expr(@OP, X, Y, 0);} 678 expr(A) ::= expr(X) PLUS|MINUS(OP) expr(Y). {A = sqlite3Expr(@OP, X, Y, 0);} 679 expr(A) ::= expr(X) STAR|SLASH|REM(OP) expr(Y). {A = sqlite3Expr(@OP, X, Y, 0);} 680 expr(A) ::= expr(X) CONCAT(OP) expr(Y). {A = sqlite3Expr(@OP, X, Y, 0);} 681 %type likeop {struct LikeOp} 682 likeop(A) ::= LIKE_KW(X). {A.eOperator = X; A.not = 0;} 683 likeop(A) ::= NOT LIKE_KW(X). {A.eOperator = X; A.not = 1;} 684 likeop(A) ::= MATCH(X). {A.eOperator = X; A.not = 0;} 685 likeop(A) ::= NOT MATCH(X). {A.eOperator = X; A.not = 1;} 686 %type escape {Expr*} 687 %destructor escape {sqlite3ExprDelete($$);} 688 escape(X) ::= ESCAPE expr(A). [ESCAPE] {X = A;} 689 escape(X) ::= . [ESCAPE] {X = 0;} 690 expr(A) ::= expr(X) likeop(OP) expr(Y) escape(E). [LIKE_KW] { 691 ExprList *pList; 692 pList = sqlite3ExprListAppend(0, Y, 0); 693 pList = sqlite3ExprListAppend(pList, X, 0); 694 if( E ){ 695 pList = sqlite3ExprListAppend(pList, E, 0); 696 } 697 A = sqlite3ExprFunction(pList, &OP.eOperator); 698 if( OP.not ) A = sqlite3Expr(TK_NOT, A, 0, 0); 699 sqlite3ExprSpan(A, &X->span, &Y->span); 700 if( A ) A->flags |= EP_InfixFunc; 701 } 702 703 expr(A) ::= expr(X) ISNULL|NOTNULL(E). { 704 A = sqlite3Expr(@E, X, 0, 0); 705 sqlite3ExprSpan(A,&X->span,&E); 706 } 707 expr(A) ::= expr(X) IS NULL(E). { 708 A = sqlite3Expr(TK_ISNULL, X, 0, 0); 709 sqlite3ExprSpan(A,&X->span,&E); 710 } 711 expr(A) ::= expr(X) NOT NULL(E). { 712 A = sqlite3Expr(TK_NOTNULL, X, 0, 0); 713 sqlite3ExprSpan(A,&X->span,&E); 714 } 715 expr(A) ::= expr(X) IS NOT NULL(E). { 716 A = sqlite3Expr(TK_NOTNULL, X, 0, 0); 717 sqlite3ExprSpan(A,&X->span,&E); 718 } 719 expr(A) ::= NOT|BITNOT(B) expr(X). { 720 A = sqlite3Expr(@B, X, 0, 0); 721 sqlite3ExprSpan(A,&B,&X->span); 722 } 723 expr(A) ::= MINUS(B) expr(X). [UMINUS] { 724 A = sqlite3Expr(TK_UMINUS, X, 0, 0); 725 sqlite3ExprSpan(A,&B,&X->span); 726 } 727 expr(A) ::= PLUS(B) expr(X). [UPLUS] { 728 A = sqlite3Expr(TK_UPLUS, X, 0, 0); 729 sqlite3ExprSpan(A,&B,&X->span); 730 } 731 %type between_op {int} 732 between_op(A) ::= BETWEEN. {A = 0;} 733 between_op(A) ::= NOT BETWEEN. {A = 1;} 734 expr(A) ::= expr(W) between_op(N) expr(X) AND expr(Y). [BETWEEN] { 735 ExprList *pList = sqlite3ExprListAppend(0, X, 0); 736 pList = sqlite3ExprListAppend(pList, Y, 0); 737 A = sqlite3Expr(TK_BETWEEN, W, 0, 0); 738 if( A ){ 739 A->pList = pList; 740 }else{ 741 sqlite3ExprListDelete(pList); 742 } 743 if( N ) A = sqlite3Expr(TK_NOT, A, 0, 0); 744 sqlite3ExprSpan(A,&W->span,&Y->span); 745 } 746 %ifndef SQLITE_OMIT_SUBQUERY 747 %type in_op {int} 748 in_op(A) ::= IN. {A = 0;} 749 in_op(A) ::= NOT IN. {A = 1;} 750 expr(A) ::= expr(X) in_op(N) LP exprlist(Y) RP(E). [IN] { 751 A = sqlite3Expr(TK_IN, X, 0, 0); 752 if( A ){ 753 A->pList = Y; 754 }else{ 755 sqlite3ExprListDelete(Y); 756 } 757 if( N ) A = sqlite3Expr(TK_NOT, A, 0, 0); 758 sqlite3ExprSpan(A,&X->span,&E); 759 } 760 expr(A) ::= LP(B) select(X) RP(E). { 761 A = sqlite3Expr(TK_SELECT, 0, 0, 0); 762 if( A ){ 763 A->pSelect = X; 764 }else{ 765 sqlite3SelectDelete(X); 766 } 767 sqlite3ExprSpan(A,&B,&E); 768 } 769 expr(A) ::= expr(X) in_op(N) LP select(Y) RP(E). [IN] { 770 A = sqlite3Expr(TK_IN, X, 0, 0); 771 if( A ){ 772 A->pSelect = Y; 773 }else{ 774 sqlite3SelectDelete(Y); 775 } 776 if( N ) A = sqlite3Expr(TK_NOT, A, 0, 0); 777 sqlite3ExprSpan(A,&X->span,&E); 778 } 779 expr(A) ::= expr(X) in_op(N) nm(Y) dbnm(Z). [IN] { 780 SrcList *pSrc = sqlite3SrcListAppend(0,&Y,&Z); 781 A = sqlite3Expr(TK_IN, X, 0, 0); 782 if( A ){ 783 A->pSelect = sqlite3SelectNew(0,pSrc,0,0,0,0,0,0,0); 784 }else{ 785 sqlite3SrcListDelete(pSrc); 786 } 787 if( N ) A = sqlite3Expr(TK_NOT, A, 0, 0); 788 sqlite3ExprSpan(A,&X->span,Z.z?&Z:&Y); 789 } 790 expr(A) ::= EXISTS(B) LP select(Y) RP(E). { 791 Expr *p = A = sqlite3Expr(TK_EXISTS, 0, 0, 0); 792 if( p ){ 793 p->pSelect = Y; 794 sqlite3ExprSpan(p,&B,&E); 795 }else{ 796 sqlite3SelectDelete(Y); 797 } 798 } 799 %endif SQLITE_OMIT_SUBQUERY 800 801 /* CASE expressions */ 802 expr(A) ::= CASE(C) case_operand(X) case_exprlist(Y) case_else(Z) END(E). { 803 A = sqlite3Expr(TK_CASE, X, Z, 0); 804 if( A ){ 805 A->pList = Y; 806 }else{ 807 sqlite3ExprListDelete(Y); 808 } 809 sqlite3ExprSpan(A, &C, &E); 810 } 811 %type case_exprlist {ExprList*} 812 %destructor case_exprlist {sqlite3ExprListDelete($$);} 813 case_exprlist(A) ::= case_exprlist(X) WHEN expr(Y) THEN expr(Z). { 814 A = sqlite3ExprListAppend(X, Y, 0); 815 A = sqlite3ExprListAppend(A, Z, 0); 816 } 817 case_exprlist(A) ::= WHEN expr(Y) THEN expr(Z). { 818 A = sqlite3ExprListAppend(0, Y, 0); 819 A = sqlite3ExprListAppend(A, Z, 0); 820 } 821 %type case_else {Expr*} 822 %destructor case_else {sqlite3ExprDelete($$);} 823 case_else(A) ::= ELSE expr(X). {A = X;} 824 case_else(A) ::= . {A = 0;} 825 %type case_operand {Expr*} 826 %destructor case_operand {sqlite3ExprDelete($$);} 827 case_operand(A) ::= expr(X). {A = X;} 828 case_operand(A) ::= . {A = 0;} 829 830 %type exprlist {ExprList*} 831 %destructor exprlist {sqlite3ExprListDelete($$);} 832 %type expritem {Expr*} 833 %destructor expritem {sqlite3ExprDelete($$);} 834 835 exprlist(A) ::= exprlist(X) COMMA expritem(Y). 836 {A = sqlite3ExprListAppend(X,Y,0);} 837 exprlist(A) ::= expritem(X). {A = sqlite3ExprListAppend(0,X,0);} 838 expritem(A) ::= expr(X). {A = X;} 839 expritem(A) ::= . {A = 0;} 840 841 ///////////////////////////// The CREATE INDEX command /////////////////////// 842 // 843 cmd ::= CREATE(S) uniqueflag(U) INDEX ifnotexists(NE) nm(X) dbnm(D) 844 ON nm(Y) LP idxlist(Z) RP(E). { 845 sqlite3CreateIndex(pParse, &X, &D, sqlite3SrcListAppend(0,&Y,0), Z, U, 846 &S, &E, SQLITE_SO_ASC, NE); 847 } 848 849 %type uniqueflag {int} 850 uniqueflag(A) ::= UNIQUE. {A = OE_Abort;} 851 uniqueflag(A) ::= . {A = OE_None;} 852 853 %type idxlist {ExprList*} 854 %destructor idxlist {sqlite3ExprListDelete($$);} 855 %type idxlist_opt {ExprList*} 856 %destructor idxlist_opt {sqlite3ExprListDelete($$);} 857 %type idxitem {Token} 858 859 idxlist_opt(A) ::= . {A = 0;} 860 idxlist_opt(A) ::= LP idxlist(X) RP. {A = X;} 861 idxlist(A) ::= idxlist(X) COMMA idxitem(Y) collate(C) sortorder(Z). { 862 Expr *p = 0; 863 if( C.n>0 ){ 864 p = sqlite3Expr(TK_COLUMN, 0, 0, 0); 865 if( p ) p->pColl = sqlite3LocateCollSeq(pParse, (char*)C.z, C.n); 866 } 867 A = sqlite3ExprListAppend(X, p, &Y); 868 if( A ) A->a[A->nExpr-1].sortOrder = Z; 869 } 870 idxlist(A) ::= idxitem(Y) collate(C) sortorder(Z). { 871 Expr *p = 0; 872 if( C.n>0 ){ 873 p = sqlite3Expr(TK_COLUMN, 0, 0, 0); 874 if( p ) p->pColl = sqlite3LocateCollSeq(pParse, (char*)C.z, C.n); 875 } 876 A = sqlite3ExprListAppend(0, p, &Y); 877 if( A ) A->a[A->nExpr-1].sortOrder = Z; 878 } 879 idxitem(A) ::= nm(X). {A = X;} 880 881 %type collate {Token} 882 collate(C) ::= . {C.z = 0; C.n = 0;} 883 collate(C) ::= COLLATE id(X). {C = X;} 884 885 886 ///////////////////////////// The DROP INDEX command ///////////////////////// 887 // 888 cmd ::= DROP INDEX ifexists(E) fullname(X). {sqlite3DropIndex(pParse, X, E);} 889 890 ///////////////////////////// The VACUUM command ///////////////////////////// 891 // 892 %ifndef SQLITE_OMIT_VACUUM 893 cmd ::= VACUUM. {sqlite3Vacuum(pParse);} 894 cmd ::= VACUUM nm. {sqlite3Vacuum(pParse);} 895 %endif SQLITE_OMIT_VACUUM 896 897 ///////////////////////////// The PRAGMA command ///////////////////////////// 898 // 899 %ifndef SQLITE_OMIT_PRAGMA 900 cmd ::= PRAGMA nm(X) dbnm(Z) EQ nmnum(Y). {sqlite3Pragma(pParse,&X,&Z,&Y,0);} 901 cmd ::= PRAGMA nm(X) dbnm(Z) EQ ON(Y). {sqlite3Pragma(pParse,&X,&Z,&Y,0);} 902 cmd ::= PRAGMA nm(X) dbnm(Z) EQ minus_num(Y). { 903 sqlite3Pragma(pParse,&X,&Z,&Y,1); 904 } 905 cmd ::= PRAGMA nm(X) dbnm(Z) LP nmnum(Y) RP. {sqlite3Pragma(pParse,&X,&Z,&Y,0);} 906 cmd ::= PRAGMA nm(X) dbnm(Z). {sqlite3Pragma(pParse,&X,&Z,0,0);} 907 nmnum(A) ::= plus_num(X). {A = X;} 908 nmnum(A) ::= nm(X). {A = X;} 909 %endif SQLITE_OMIT_PRAGMA 910 plus_num(A) ::= plus_opt number(X). {A = X;} 911 minus_num(A) ::= MINUS number(X). {A = X;} 912 number(A) ::= INTEGER|FLOAT(X). {A = X;} 913 plus_opt ::= PLUS. 914 plus_opt ::= . 915 916 //////////////////////////// The CREATE TRIGGER command ///////////////////// 917 918 %ifndef SQLITE_OMIT_TRIGGER 919 920 cmd ::= CREATE trigger_decl(A) BEGIN trigger_cmd_list(S) END(Z). { 921 Token all; 922 all.z = A.z; 923 all.n = (Z.z - A.z) + Z.n; 924 sqlite3FinishTrigger(pParse, S, &all); 925 } 926 927 trigger_decl(A) ::= temp(T) TRIGGER ifnotexists(NOERR) nm(B) dbnm(Z) 928 trigger_time(C) trigger_event(D) 929 ON fullname(E) foreach_clause(F) when_clause(G). { 930 sqlite3BeginTrigger(pParse, &B, &Z, C, D.a, D.b, E, F, G, T, NOERR); 931 A = (Z.n==0?B:Z); 932 } 933 934 %type trigger_time {int} 935 trigger_time(A) ::= BEFORE. { A = TK_BEFORE; } 936 trigger_time(A) ::= AFTER. { A = TK_AFTER; } 937 trigger_time(A) ::= INSTEAD OF. { A = TK_INSTEAD;} 938 trigger_time(A) ::= . { A = TK_BEFORE; } 939 940 %type trigger_event {struct TrigEvent} 941 %destructor trigger_event {sqlite3IdListDelete($$.b);} 942 trigger_event(A) ::= DELETE|INSERT(OP). {A.a = @OP; A.b = 0;} 943 trigger_event(A) ::= UPDATE(OP). {A.a = @OP; A.b = 0;} 944 trigger_event(A) ::= UPDATE OF inscollist(X). {A.a = TK_UPDATE; A.b = X;} 945 946 %type foreach_clause {int} 947 foreach_clause(A) ::= . { A = TK_ROW; } 948 foreach_clause(A) ::= FOR EACH ROW. { A = TK_ROW; } 949 foreach_clause(A) ::= FOR EACH STATEMENT. { A = TK_STATEMENT; } 950 951 %type when_clause {Expr*} 952 %destructor when_clause {sqlite3ExprDelete($$);} 953 when_clause(A) ::= . { A = 0; } 954 when_clause(A) ::= WHEN expr(X). { A = X; } 955 956 %type trigger_cmd_list {TriggerStep*} 957 %destructor trigger_cmd_list {sqlite3DeleteTriggerStep($$);} 958 trigger_cmd_list(A) ::= trigger_cmd_list(Y) trigger_cmd(X) SEMI. { 959 if( Y ){ 960 Y->pLast->pNext = X; 961 }else{ 962 Y = X; 963 } 964 Y->pLast = X; 965 A = Y; 966 } 967 trigger_cmd_list(A) ::= . { A = 0; } 968 969 %type trigger_cmd {TriggerStep*} 970 %destructor trigger_cmd {sqlite3DeleteTriggerStep($$);} 971 // UPDATE 972 trigger_cmd(A) ::= UPDATE orconf(R) nm(X) SET setlist(Y) where_opt(Z). 973 { A = sqlite3TriggerUpdateStep(&X, Y, Z, R); } 974 975 // INSERT 976 trigger_cmd(A) ::= insert_cmd(R) INTO nm(X) inscollist_opt(F) 977 VALUES LP itemlist(Y) RP. 978 {A = sqlite3TriggerInsertStep(&X, F, Y, 0, R);} 979 980 trigger_cmd(A) ::= insert_cmd(R) INTO nm(X) inscollist_opt(F) select(S). 981 {A = sqlite3TriggerInsertStep(&X, F, 0, S, R);} 982 983 // DELETE 984 trigger_cmd(A) ::= DELETE FROM nm(X) where_opt(Y). 985 {A = sqlite3TriggerDeleteStep(&X, Y);} 986 987 // SELECT 988 trigger_cmd(A) ::= select(X). {A = sqlite3TriggerSelectStep(X); } 989 990 // The special RAISE expression that may occur in trigger programs 991 expr(A) ::= RAISE(X) LP IGNORE RP(Y). { 992 A = sqlite3Expr(TK_RAISE, 0, 0, 0); 993 if( A ){ 994 A->iColumn = OE_Ignore; 995 sqlite3ExprSpan(A, &X, &Y); 996 } 997 } 998 expr(A) ::= RAISE(X) LP raisetype(T) COMMA nm(Z) RP(Y). { 999 A = sqlite3Expr(TK_RAISE, 0, 0, &Z); 1000 if( A ) { 1001 A->iColumn = T; 1002 sqlite3ExprSpan(A, &X, &Y); 1003 } 1004 } 1005 %endif !SQLITE_OMIT_TRIGGER 1006 1007 %type raisetype {int} 1008 raisetype(A) ::= ROLLBACK. {A = OE_Rollback;} 1009 raisetype(A) ::= ABORT. {A = OE_Abort;} 1010 raisetype(A) ::= FAIL. {A = OE_Fail;} 1011 1012 1013 //////////////////////// DROP TRIGGER statement ////////////////////////////// 1014 %ifndef SQLITE_OMIT_TRIGGER 1015 cmd ::= DROP TRIGGER ifexists(NOERR) fullname(X). { 1016 sqlite3DropTrigger(pParse,X,NOERR); 1017 } 1018 %endif !SQLITE_OMIT_TRIGGER 1019 1020 //////////////////////// ATTACH DATABASE file AS name ///////////////////////// 1021 cmd ::= ATTACH database_kw_opt expr(F) AS expr(D) key_opt(K). { 1022 sqlite3Attach(pParse, F, D, K); 1023 } 1024 %type key_opt {Expr *} 1025 %destructor key_opt {sqlite3ExprDelete($$);} 1026 key_opt(A) ::= . { A = 0; } 1027 key_opt(A) ::= KEY expr(X). { A = X; } 1028 1029 database_kw_opt ::= DATABASE. 1030 database_kw_opt ::= . 1031 1032 //////////////////////// DETACH DATABASE name ///////////////////////////////// 1033 cmd ::= DETACH database_kw_opt expr(D). { 1034 sqlite3Detach(pParse, D); 1035 } 1036 1037 ////////////////////////// REINDEX collation ////////////////////////////////// 1038 %ifndef SQLITE_OMIT_REINDEX 1039 cmd ::= REINDEX. {sqlite3Reindex(pParse, 0, 0);} 1040 cmd ::= REINDEX nm(X) dbnm(Y). {sqlite3Reindex(pParse, &X, &Y);} 1041 %endif SQLITE_OMIT_REINDEX 1042 1043 /////////////////////////////////// ANALYZE /////////////////////////////////// 1044 %ifndef SQLITE_OMIT_ANALYZE 1045 cmd ::= ANALYZE. {sqlite3Analyze(pParse, 0, 0);} 1046 cmd ::= ANALYZE nm(X) dbnm(Y). {sqlite3Analyze(pParse, &X, &Y);} 1047 %endif 1048 1049 //////////////////////// ALTER TABLE table ... //////////////////////////////// 1050 %ifndef SQLITE_OMIT_ALTERTABLE 1051 cmd ::= ALTER TABLE fullname(X) RENAME TO nm(Z). { 1052 sqlite3AlterRenameTable(pParse,X,&Z); 1053 } 1054 cmd ::= ALTER TABLE add_column_fullname ADD kwcolumn_opt column(Y). { 1055 sqlite3AlterFinishAddColumn(pParse, &Y); 1056 } 1057 add_column_fullname ::= fullname(X). { 1058 sqlite3AlterBeginAddColumn(pParse, X); 1059 } 1060 kwcolumn_opt ::= . 1061 kwcolumn_opt ::= COLUMNKW. 1062 %endif SQLITE_OMIT_ALTERTABLE 1063 1064 //////////////////////// CREATE VIRTUAL TABLE ... ///////////////////////////// 1065 %ifndef SQLITE_OMIT_VIRTUALTABLE 1066 cmd ::= create_vtab. {sqlite3VtabFinishParse(pParse,0);} 1067 cmd ::= create_vtab LP vtabarglist RP(X). {sqlite3VtabFinishParse(pParse,&X);} 1068 create_vtab ::= CREATE VIRTUAL TABLE nm(X) dbnm(Y) USING nm(Z). { 1069 sqlite3VtabBeginParse(pParse, &X, &Y, &Z); 1070 } 1071 vtabarglist ::= vtabarg. 1072 vtabarglist ::= vtabarglist COMMA vtabarg. 1073 vtabarg ::= . {sqlite3VtabArgInit(pParse);} 1074 vtabarg ::= vtabarg vtabargtoken. 1075 vtabargtoken ::= ANY(X). {sqlite3VtabArgExtend(pParse,&X);} 1076 vtabargtoken ::= lp anylist RP(X). {sqlite3VtabArgExtend(pParse,&X);} 1077 lp ::= LP(X). {sqlite3VtabArgExtend(pParse,&X);} 1078 anylist ::= . 1079 anylist ::= anylist ANY(X). {sqlite3VtabArgExtend(pParse,&X);} 1080 %endif SQLITE_OMIT_VIRTUALTABLE 1081