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.127 2004/06/09 09:55:18 danielk1977 Exp $ 18 */ 19 %token_prefix TK_ 20 %token_type {Token} 21 %default_type {Token} 22 %extra_argument {Parse *pParse} 23 %syntax_error { 24 if( pParse->zErrMsg==0 ){ 25 if( TOKEN.z[0] ){ 26 sqlite3ErrorMsg(pParse, "near \"%T\": syntax error", &TOKEN); 27 }else{ 28 sqlite3ErrorMsg(pParse, "incomplete SQL statement"); 29 } 30 } 31 } 32 %name sqlite3Parser 33 %include { 34 #include "sqliteInt.h" 35 #include "parse.h" 36 37 /* 38 ** An instance of this structure holds information about the 39 ** LIMIT clause of a SELECT statement. 40 */ 41 struct LimitVal { 42 int limit; /* The LIMIT value. -1 if there is no limit */ 43 int offset; /* The OFFSET. 0 if there is none */ 44 }; 45 46 /* 47 ** An instance of the following structure describes the event of a 48 ** TRIGGER. "a" is the event type, one of TK_UPDATE, TK_INSERT, 49 ** TK_DELETE, or TK_INSTEAD. If the event is of the form 50 ** 51 ** UPDATE ON (a,b,c) 52 ** 53 ** Then the "b" IdList records the list "a,b,c". 54 */ 55 struct TrigEvent { int a; IdList * b; }; 56 57 } // end %include 58 59 // These are extra tokens used by the lexer but never seen by the 60 // parser. We put them in a rule so that the parser generator will 61 // add them to the parse.h output file. 62 // 63 %nonassoc END_OF_FILE ILLEGAL SPACE UNCLOSED_STRING COMMENT FUNCTION 64 COLUMN AGG_FUNCTION. 65 66 // Input is a single SQL command 67 input ::= cmdlist. 68 cmdlist ::= cmdlist ecmd. 69 cmdlist ::= ecmd. 70 ecmd ::= explain cmdx SEMI. 71 ecmd ::= SEMI. 72 cmdx ::= cmd. { sqlite3FinishCoding(pParse); } 73 explain ::= EXPLAIN. { sqlite3BeginParse(pParse, 1); } 74 explain ::= . { sqlite3BeginParse(pParse, 0); } 75 76 ///////////////////// Begin and end transactions. //////////////////////////// 77 // 78 79 cmd ::= BEGIN trans_opt. {sqlite3BeginTransaction(pParse);} 80 trans_opt ::= . 81 trans_opt ::= TRANSACTION. 82 trans_opt ::= TRANSACTION nm. 83 cmd ::= COMMIT trans_opt. {sqlite3CommitTransaction(pParse);} 84 cmd ::= END trans_opt. {sqlite3CommitTransaction(pParse);} 85 cmd ::= ROLLBACK trans_opt. {sqlite3RollbackTransaction(pParse);} 86 87 ///////////////////// The CREATE TABLE statement //////////////////////////// 88 // 89 cmd ::= create_table create_table_args. 90 create_table ::= CREATE(X) temp(T) TABLE nm(Y) dbnm(Z). { 91 sqlite3StartTable(pParse,&X,&Y,&Z,T,0); 92 } 93 %type temp {int} 94 temp(A) ::= TEMP. {A = 1;} 95 temp(A) ::= . {A = 0;} 96 create_table_args ::= LP columnlist conslist_opt RP(X). { 97 sqlite3EndTable(pParse,&X,0); 98 } 99 create_table_args ::= AS select(S). { 100 sqlite3EndTable(pParse,0,S); 101 sqlite3SelectDelete(S); 102 } 103 columnlist ::= columnlist COMMA column. 104 columnlist ::= column. 105 106 // About the only information used for a column is the name of the 107 // column. The type is always just "text". But the code will accept 108 // an elaborate typename. Perhaps someday we'll do something with it. 109 // 110 column ::= columnid type carglist. 111 columnid ::= nm(X). {sqlite3AddColumn(pParse,&X);} 112 113 // An IDENTIFIER can be a generic identifier, or one of several 114 // keywords. Any non-standard keyword can also be an identifier. 115 // 116 %type id {Token} 117 id(A) ::= ID(X). {A = X;} 118 119 // The following directive causes tokens ABORT, AFTER, ASC, etc. to 120 // fallback to ID if they will not parse as their original value. 121 // This obviates the need for the "id" nonterminal. 122 // 123 %fallback ID 124 ABORT AFTER ASC ATTACH BEFORE BEGIN CASCADE CLUSTER CONFLICT 125 DATABASE DEFERRED DESC DETACH EACH END EXPLAIN FAIL FOR 126 GLOB IGNORE IMMEDIATE INITIALLY INSTEAD LIKE MATCH KEY 127 OF OFFSET PRAGMA RAISE REPLACE RESTRICT ROW STATEMENT 128 TEMP TRIGGER VACUUM VIEW. 129 130 // Define operator precedence early so that this is the first occurance 131 // of the operator tokens in the grammer. Keeping the operators together 132 // causes them to be assigned integer values that are close together, 133 // which keeps parser tables smaller. 134 // 135 %left OR. 136 %left AND. 137 %right NOT. 138 %left EQ NE ISNULL NOTNULL IS LIKE GLOB BETWEEN IN. 139 %left GT GE LT LE. 140 %left BITAND BITOR LSHIFT RSHIFT. 141 %left PLUS MINUS. 142 %left STAR SLASH REM. 143 %left CONCAT. 144 %right UMINUS UPLUS BITNOT. 145 146 // And "ids" is an identifer-or-string. 147 // 148 %type ids {Token} 149 ids(A) ::= ID(X). {A = X;} 150 ids(A) ::= STRING(X). {A = X;} 151 152 // The name of a column or table can be any of the following: 153 // 154 %type nm {Token} 155 nm(A) ::= ID(X). {A = X;} 156 nm(A) ::= STRING(X). {A = X;} 157 nm(A) ::= JOIN_KW(X). {A = X;} 158 159 type ::= . 160 type ::= typename(X). {sqlite3AddColumnType(pParse,&X,&X);} 161 type ::= typename(X) LP signed RP(Y). {sqlite3AddColumnType(pParse,&X,&Y);} 162 type ::= typename(X) LP signed COMMA signed RP(Y). 163 {sqlite3AddColumnType(pParse,&X,&Y);} 164 %type typename {Token} 165 typename(A) ::= ids(X). {A = X;} 166 typename(A) ::= typename(X) ids(Y). {A.z=X.z; A.n=X.n+Addr(Y.z)-Addr(X.z);} 167 %type signed {int} 168 signed(A) ::= INTEGER(X). { A = atoi(X.z); } 169 signed(A) ::= PLUS INTEGER(X). { A = atoi(X.z); } 170 signed(A) ::= MINUS INTEGER(X). { A = -atoi(X.z); } 171 carglist ::= carglist carg. 172 carglist ::= . 173 carg ::= CONSTRAINT nm ccons. 174 carg ::= ccons. 175 carg ::= DEFAULT STRING(X). {sqlite3AddDefaultValue(pParse,&X,0);} 176 carg ::= DEFAULT ID(X). {sqlite3AddDefaultValue(pParse,&X,0);} 177 carg ::= DEFAULT INTEGER(X). {sqlite3AddDefaultValue(pParse,&X,0);} 178 carg ::= DEFAULT PLUS INTEGER(X). {sqlite3AddDefaultValue(pParse,&X,0);} 179 carg ::= DEFAULT MINUS INTEGER(X). {sqlite3AddDefaultValue(pParse,&X,1);} 180 carg ::= DEFAULT FLOAT(X). {sqlite3AddDefaultValue(pParse,&X,0);} 181 carg ::= DEFAULT PLUS FLOAT(X). {sqlite3AddDefaultValue(pParse,&X,0);} 182 carg ::= DEFAULT MINUS FLOAT(X). {sqlite3AddDefaultValue(pParse,&X,1);} 183 carg ::= DEFAULT NULL. 184 185 // In addition to the type name, we also care about the primary key and 186 // UNIQUE constraints. 187 // 188 ccons ::= NULL onconf. 189 ccons ::= NOT NULL onconf(R). {sqlite3AddNotNull(pParse, R);} 190 ccons ::= PRIMARY KEY sortorder onconf(R). {sqlite3AddPrimaryKey(pParse,0,R);} 191 ccons ::= UNIQUE onconf(R). {sqlite3CreateIndex(pParse,0,0,0,0,R,0,0);} 192 ccons ::= CHECK LP expr RP onconf. 193 ccons ::= REFERENCES nm(T) idxlist_opt(TA) refargs(R). 194 {sqlite3CreateForeignKey(pParse,0,&T,TA,R);} 195 ccons ::= defer_subclause(D). {sqlite3DeferForeignKey(pParse,D);} 196 ccons ::= COLLATE id(C). {sqlite3AddCollateType(pParse, C.z, C.n);} 197 198 // The next group of rules parses the arguments to a REFERENCES clause 199 // that determine if the referential integrity checking is deferred or 200 // or immediate and which determine what action to take if a ref-integ 201 // check fails. 202 // 203 %type refargs {int} 204 refargs(A) ::= . { A = OE_Restrict * 0x010101; } 205 refargs(A) ::= refargs(X) refarg(Y). { A = (X & Y.mask) | Y.value; } 206 %type refarg {struct {int value; int mask;}} 207 refarg(A) ::= MATCH nm. { A.value = 0; A.mask = 0x000000; } 208 refarg(A) ::= ON DELETE refact(X). { A.value = X; A.mask = 0x0000ff; } 209 refarg(A) ::= ON UPDATE refact(X). { A.value = X<<8; A.mask = 0x00ff00; } 210 refarg(A) ::= ON INSERT refact(X). { A.value = X<<16; A.mask = 0xff0000; } 211 %type refact {int} 212 refact(A) ::= SET NULL. { A = OE_SetNull; } 213 refact(A) ::= SET DEFAULT. { A = OE_SetDflt; } 214 refact(A) ::= CASCADE. { A = OE_Cascade; } 215 refact(A) ::= RESTRICT. { A = OE_Restrict; } 216 %type defer_subclause {int} 217 defer_subclause(A) ::= NOT DEFERRABLE init_deferred_pred_opt(X). {A = X;} 218 defer_subclause(A) ::= DEFERRABLE init_deferred_pred_opt(X). {A = X;} 219 %type init_deferred_pred_opt {int} 220 init_deferred_pred_opt(A) ::= . {A = 0;} 221 init_deferred_pred_opt(A) ::= INITIALLY DEFERRED. {A = 1;} 222 init_deferred_pred_opt(A) ::= INITIALLY IMMEDIATE. {A = 0;} 223 224 // For the time being, the only constraint we care about is the primary 225 // key and UNIQUE. Both create indices. 226 // 227 conslist_opt ::= . 228 conslist_opt ::= COMMA conslist. 229 conslist ::= conslist COMMA tcons. 230 conslist ::= conslist tcons. 231 conslist ::= tcons. 232 tcons ::= CONSTRAINT nm. 233 tcons ::= PRIMARY KEY LP idxlist(X) RP onconf(R). 234 {sqlite3AddPrimaryKey(pParse,X,R);} 235 tcons ::= UNIQUE LP idxlist(X) RP onconf(R). 236 {sqlite3CreateIndex(pParse,0,0,0,X,R,0,0);} 237 tcons ::= CHECK expr onconf. 238 tcons ::= FOREIGN KEY LP idxlist(FA) RP 239 REFERENCES nm(T) idxlist_opt(TA) refargs(R) defer_subclause_opt(D). { 240 sqlite3CreateForeignKey(pParse, FA, &T, TA, R); 241 sqlite3DeferForeignKey(pParse, D); 242 } 243 %type defer_subclause_opt {int} 244 defer_subclause_opt(A) ::= . {A = 0;} 245 defer_subclause_opt(A) ::= defer_subclause(X). {A = X;} 246 247 // The following is a non-standard extension that allows us to declare the 248 // default behavior when there is a constraint conflict. 249 // 250 %type onconf {int} 251 %type orconf {int} 252 %type resolvetype {int} 253 onconf(A) ::= . { A = OE_Default; } 254 onconf(A) ::= ON CONFLICT resolvetype(X). { A = X; } 255 orconf(A) ::= . { A = OE_Default; } 256 orconf(A) ::= OR resolvetype(X). { A = X; } 257 resolvetype(A) ::= ROLLBACK. { A = OE_Rollback; } 258 resolvetype(A) ::= ABORT. { A = OE_Abort; } 259 resolvetype(A) ::= FAIL. { A = OE_Fail; } 260 resolvetype(A) ::= IGNORE. { A = OE_Ignore; } 261 resolvetype(A) ::= REPLACE. { A = OE_Replace; } 262 263 ////////////////////////// The DROP TABLE ///////////////////////////////////// 264 // 265 cmd ::= DROP TABLE nm(X) dbnm(Y). { 266 sqlite3DropTable(pParse, sqlite3SrcListAppend(0,&X,&Y), 0); 267 } 268 269 ///////////////////// The CREATE VIEW statement ///////////////////////////// 270 // 271 cmd ::= CREATE(X) temp(T) VIEW nm(Y) dbnm(Z) AS select(S). { 272 sqlite3CreateView(pParse, &X, &Y, &Z, S, T); 273 } 274 cmd ::= DROP VIEW nm(X) dbnm(Y). { 275 sqlite3DropTable(pParse, sqlite3SrcListAppend(0,&X,&Y), 1); 276 } 277 278 //////////////////////// The SELECT statement ///////////////////////////////// 279 // 280 cmd ::= select(X). { 281 sqlite3Select(pParse, X, SRT_Callback, 0, 0, 0, 0, 0); 282 sqlite3SelectDelete(X); 283 } 284 285 %type select {Select*} 286 %destructor select {sqlite3SelectDelete($$);} 287 %type oneselect {Select*} 288 %destructor oneselect {sqlite3SelectDelete($$);} 289 290 select(A) ::= oneselect(X). {A = X;} 291 select(A) ::= select(X) multiselect_op(Y) oneselect(Z). { 292 if( Z ){ 293 Z->op = Y; 294 Z->pPrior = X; 295 } 296 A = Z; 297 } 298 %type multiselect_op {int} 299 multiselect_op(A) ::= UNION. {A = TK_UNION;} 300 multiselect_op(A) ::= UNION ALL. {A = TK_ALL;} 301 multiselect_op(A) ::= INTERSECT. {A = TK_INTERSECT;} 302 multiselect_op(A) ::= EXCEPT. {A = TK_EXCEPT;} 303 oneselect(A) ::= SELECT distinct(D) selcollist(W) from(X) where_opt(Y) 304 groupby_opt(P) having_opt(Q) orderby_opt(Z) limit_opt(L). { 305 A = sqlite3SelectNew(W,X,Y,P,Q,Z,D,L.limit,L.offset); 306 } 307 308 // The "distinct" nonterminal is true (1) if the DISTINCT keyword is 309 // present and false (0) if it is not. 310 // 311 %type distinct {int} 312 distinct(A) ::= DISTINCT. {A = 1;} 313 distinct(A) ::= ALL. {A = 0;} 314 distinct(A) ::= . {A = 0;} 315 316 // selcollist is a list of expressions that are to become the return 317 // values of the SELECT statement. The "*" in statements like 318 // "SELECT * FROM ..." is encoded as a special expression with an 319 // opcode of TK_ALL. 320 // 321 %type selcollist {ExprList*} 322 %destructor selcollist {sqlite3ExprListDelete($$);} 323 %type sclp {ExprList*} 324 %destructor sclp {sqlite3ExprListDelete($$);} 325 sclp(A) ::= selcollist(X) COMMA. {A = X;} 326 sclp(A) ::= . {A = 0;} 327 selcollist(A) ::= sclp(P) expr(X) as(Y). { 328 A = sqlite3ExprListAppend(P,X,Y.n?&Y:0); 329 } 330 selcollist(A) ::= sclp(P) STAR. { 331 A = sqlite3ExprListAppend(P, sqlite3Expr(TK_ALL, 0, 0, 0), 0); 332 } 333 selcollist(A) ::= sclp(P) nm(X) DOT STAR. { 334 Expr *pRight = sqlite3Expr(TK_ALL, 0, 0, 0); 335 Expr *pLeft = sqlite3Expr(TK_ID, 0, 0, &X); 336 A = sqlite3ExprListAppend(P, sqlite3Expr(TK_DOT, pLeft, pRight, 0), 0); 337 } 338 339 // An option "AS <id>" phrase that can follow one of the expressions that 340 // define the result set, or one of the tables in the FROM clause. 341 // 342 %type as {Token} 343 as(X) ::= AS nm(Y). { X = Y; } 344 as(X) ::= ids(Y). { X = Y; } 345 as(X) ::= . { X.n = 0; } 346 347 348 %type seltablist {SrcList*} 349 %destructor seltablist {sqlite3SrcListDelete($$);} 350 %type stl_prefix {SrcList*} 351 %destructor stl_prefix {sqlite3SrcListDelete($$);} 352 %type from {SrcList*} 353 %destructor from {sqlite3SrcListDelete($$);} 354 355 // A complete FROM clause. 356 // 357 from(A) ::= . {A = sqliteMalloc(sizeof(*A));} 358 from(A) ::= FROM seltablist(X). {A = X;} 359 360 // "seltablist" is a "Select Table List" - the content of the FROM clause 361 // in a SELECT statement. "stl_prefix" is a prefix of this list. 362 // 363 stl_prefix(A) ::= seltablist(X) joinop(Y). { 364 A = X; 365 if( A && A->nSrc>0 ) A->a[A->nSrc-1].jointype = Y; 366 } 367 stl_prefix(A) ::= . {A = 0;} 368 seltablist(A) ::= stl_prefix(X) nm(Y) dbnm(D) as(Z) on_opt(N) using_opt(U). { 369 A = sqlite3SrcListAppend(X,&Y,&D); 370 if( Z.n ) sqlite3SrcListAddAlias(A,&Z); 371 if( N ){ 372 if( A && A->nSrc>1 ){ A->a[A->nSrc-2].pOn = N; } 373 else { sqlite3ExprDelete(N); } 374 } 375 if( U ){ 376 if( A && A->nSrc>1 ){ A->a[A->nSrc-2].pUsing = U; } 377 else { sqlite3IdListDelete(U); } 378 } 379 } 380 seltablist(A) ::= stl_prefix(X) LP seltablist_paren(S) RP 381 as(Z) on_opt(N) using_opt(U). { 382 A = sqlite3SrcListAppend(X,0,0); 383 A->a[A->nSrc-1].pSelect = S; 384 if( Z.n ) sqlite3SrcListAddAlias(A,&Z); 385 if( N ){ 386 if( A && A->nSrc>1 ){ A->a[A->nSrc-2].pOn = N; } 387 else { sqlite3ExprDelete(N); } 388 } 389 if( U ){ 390 if( A && A->nSrc>1 ){ A->a[A->nSrc-2].pUsing = U; } 391 else { sqlite3IdListDelete(U); } 392 } 393 } 394 395 // A seltablist_paren nonterminal represents anything in a FROM that 396 // is contained inside parentheses. This can be either a subquery or 397 // a grouping of table and subqueries. 398 // 399 %type seltablist_paren {Select*} 400 %destructor seltablist_paren {sqlite3SelectDelete($$);} 401 seltablist_paren(A) ::= select(S). {A = S;} 402 seltablist_paren(A) ::= seltablist(F). { 403 A = sqlite3SelectNew(0,F,0,0,0,0,0,-1,0); 404 } 405 406 %type dbnm {Token} 407 dbnm(A) ::= . {A.z=0; A.n=0;} 408 dbnm(A) ::= DOT nm(X). {A = X;} 409 410 %type joinop {int} 411 %type joinop2 {int} 412 joinop(X) ::= COMMA. { X = JT_INNER; } 413 joinop(X) ::= JOIN. { X = JT_INNER; } 414 joinop(X) ::= JOIN_KW(A) JOIN. { X = sqlite3JoinType(pParse,&A,0,0); } 415 joinop(X) ::= JOIN_KW(A) nm(B) JOIN. { X = sqlite3JoinType(pParse,&A,&B,0); } 416 joinop(X) ::= JOIN_KW(A) nm(B) nm(C) JOIN. 417 { X = sqlite3JoinType(pParse,&A,&B,&C); } 418 419 %type on_opt {Expr*} 420 %destructor on_opt {sqlite3ExprDelete($$);} 421 on_opt(N) ::= ON expr(E). {N = E;} 422 on_opt(N) ::= . {N = 0;} 423 424 %type using_opt {IdList*} 425 %destructor using_opt {sqlite3IdListDelete($$);} 426 using_opt(U) ::= USING LP inscollist(L) RP. {U = L;} 427 using_opt(U) ::= . {U = 0;} 428 429 430 %type orderby_opt {ExprList*} 431 %destructor orderby_opt {sqlite3ExprListDelete($$);} 432 %type sortlist {ExprList*} 433 %destructor sortlist {sqlite3ExprListDelete($$);} 434 %type sortitem {Expr*} 435 %destructor sortitem {sqlite3ExprDelete($$);} 436 437 orderby_opt(A) ::= . {A = 0;} 438 orderby_opt(A) ::= ORDER BY sortlist(X). {A = X;} 439 sortlist(A) ::= sortlist(X) COMMA sortitem(Y) collate(C) sortorder(Z). { 440 A = sqlite3ExprListAppend(X,Y,C.n>0?&C:0); 441 if( A ) A->a[A->nExpr-1].sortOrder = Z; 442 } 443 sortlist(A) ::= sortitem(Y) collate(C) sortorder(Z). { 444 A = sqlite3ExprListAppend(0,Y,C.n>0?&C:0); 445 if( A ) A->a[0].sortOrder = Z; 446 } 447 sortitem(A) ::= expr(X). {A = X;} 448 449 %type sortorder {int} 450 %type collate {Token} 451 452 sortorder(A) ::= ASC. {A = SQLITE_SO_ASC;} 453 sortorder(A) ::= DESC. {A = SQLITE_SO_DESC;} 454 sortorder(A) ::= . {A = SQLITE_SO_ASC;} 455 collate(C) ::= . {C.z = 0; C.n = 0;} 456 collate(C) ::= COLLATE id(X). {C = X;} 457 458 %type groupby_opt {ExprList*} 459 %destructor groupby_opt {sqlite3ExprListDelete($$);} 460 groupby_opt(A) ::= . {A = 0;} 461 groupby_opt(A) ::= GROUP BY exprlist(X). {A = X;} 462 463 %type having_opt {Expr*} 464 %destructor having_opt {sqlite3ExprDelete($$);} 465 having_opt(A) ::= . {A = 0;} 466 having_opt(A) ::= HAVING expr(X). {A = X;} 467 468 %type limit_opt {struct LimitVal} 469 limit_opt(A) ::= . {A.limit = -1; A.offset = 0;} 470 limit_opt(A) ::= LIMIT signed(X). {A.limit = X; A.offset = 0;} 471 limit_opt(A) ::= LIMIT signed(X) OFFSET signed(Y). 472 {A.limit = X; A.offset = Y;} 473 limit_opt(A) ::= LIMIT signed(X) COMMA signed(Y). 474 {A.limit = Y; A.offset = X;} 475 476 /////////////////////////// The DELETE statement ///////////////////////////// 477 // 478 cmd ::= DELETE FROM nm(X) dbnm(D) where_opt(Y). { 479 sqlite3DeleteFrom(pParse, sqlite3SrcListAppend(0,&X,&D), Y); 480 } 481 482 %type where_opt {Expr*} 483 %destructor where_opt {sqlite3ExprDelete($$);} 484 485 where_opt(A) ::= . {A = 0;} 486 where_opt(A) ::= WHERE expr(X). {A = X;} 487 488 %type setlist {ExprList*} 489 %destructor setlist {sqlite3ExprListDelete($$);} 490 491 ////////////////////////// The UPDATE command //////////////////////////////// 492 // 493 cmd ::= UPDATE orconf(R) nm(X) dbnm(D) SET setlist(Y) where_opt(Z). 494 {sqlite3Update(pParse,sqlite3SrcListAppend(0,&X,&D),Y,Z,R);} 495 496 setlist(A) ::= setlist(Z) COMMA nm(X) EQ expr(Y). 497 {A = sqlite3ExprListAppend(Z,Y,&X);} 498 setlist(A) ::= nm(X) EQ expr(Y). {A = sqlite3ExprListAppend(0,Y,&X);} 499 500 ////////////////////////// The INSERT command ///////////////////////////////// 501 // 502 cmd ::= insert_cmd(R) INTO nm(X) dbnm(D) inscollist_opt(F) 503 VALUES LP itemlist(Y) RP. 504 {sqlite3Insert(pParse, sqlite3SrcListAppend(0,&X,&D), Y, 0, F, R);} 505 cmd ::= insert_cmd(R) INTO nm(X) dbnm(D) inscollist_opt(F) select(S). 506 {sqlite3Insert(pParse, sqlite3SrcListAppend(0,&X,&D), 0, S, F, R);} 507 508 %type insert_cmd {int} 509 insert_cmd(A) ::= INSERT orconf(R). {A = R;} 510 insert_cmd(A) ::= REPLACE. {A = OE_Replace;} 511 512 513 %type itemlist {ExprList*} 514 %destructor itemlist {sqlite3ExprListDelete($$);} 515 516 itemlist(A) ::= itemlist(X) COMMA expr(Y). {A = sqlite3ExprListAppend(X,Y,0);} 517 itemlist(A) ::= expr(X). {A = sqlite3ExprListAppend(0,X,0);} 518 519 %type inscollist_opt {IdList*} 520 %destructor inscollist_opt {sqlite3IdListDelete($$);} 521 %type inscollist {IdList*} 522 %destructor inscollist {sqlite3IdListDelete($$);} 523 524 inscollist_opt(A) ::= . {A = 0;} 525 inscollist_opt(A) ::= LP inscollist(X) RP. {A = X;} 526 inscollist(A) ::= inscollist(X) COMMA nm(Y). {A = sqlite3IdListAppend(X,&Y);} 527 inscollist(A) ::= nm(Y). {A = sqlite3IdListAppend(0,&Y);} 528 529 /////////////////////////// Expression Processing ///////////////////////////// 530 // 531 532 %type expr {Expr*} 533 %destructor expr {sqlite3ExprDelete($$);} 534 535 expr(A) ::= LP(B) expr(X) RP(E). {A = X; sqlite3ExprSpan(A,&B,&E); } 536 expr(A) ::= NULL(X). {A = sqlite3Expr(TK_NULL, 0, 0, &X);} 537 expr(A) ::= ID(X). {A = sqlite3Expr(TK_ID, 0, 0, &X);} 538 expr(A) ::= JOIN_KW(X). {A = sqlite3Expr(TK_ID, 0, 0, &X);} 539 expr(A) ::= nm(X) DOT nm(Y). { 540 Expr *temp1 = sqlite3Expr(TK_ID, 0, 0, &X); 541 Expr *temp2 = sqlite3Expr(TK_ID, 0, 0, &Y); 542 A = sqlite3Expr(TK_DOT, temp1, temp2, 0); 543 } 544 expr(A) ::= nm(X) DOT nm(Y) DOT nm(Z). { 545 Expr *temp1 = sqlite3Expr(TK_ID, 0, 0, &X); 546 Expr *temp2 = sqlite3Expr(TK_ID, 0, 0, &Y); 547 Expr *temp3 = sqlite3Expr(TK_ID, 0, 0, &Z); 548 Expr *temp4 = sqlite3Expr(TK_DOT, temp2, temp3, 0); 549 A = sqlite3Expr(TK_DOT, temp1, temp4, 0); 550 } 551 expr(A) ::= INTEGER(X). {A = sqlite3Expr(TK_INTEGER, 0, 0, &X);} 552 expr(A) ::= FLOAT(X). {A = sqlite3Expr(TK_FLOAT, 0, 0, &X);} 553 expr(A) ::= STRING(X). {A = sqlite3Expr(TK_STRING, 0, 0, &X);} 554 expr(A) ::= BLOB(X). {A = sqlite3Expr(TK_BLOB, 0, 0, &X);} 555 expr(A) ::= VARIABLE(X). { 556 A = sqlite3Expr(TK_VARIABLE, 0, 0, &X); 557 if( A ) A->iTable = ++pParse->nVar; 558 } 559 expr(A) ::= ID(X) LP exprlist(Y) RP(E). { 560 A = sqlite3ExprFunction(Y, &X); 561 sqlite3ExprSpan(A,&X,&E); 562 } 563 expr(A) ::= ID(X) LP STAR RP(E). { 564 A = sqlite3ExprFunction(0, &X); 565 sqlite3ExprSpan(A,&X,&E); 566 } 567 expr(A) ::= expr(X) AND expr(Y). {A = sqlite3Expr(TK_AND, X, Y, 0);} 568 expr(A) ::= expr(X) OR expr(Y). {A = sqlite3Expr(TK_OR, X, Y, 0);} 569 expr(A) ::= expr(X) LT expr(Y). {A = sqlite3Expr(TK_LT, X, Y, 0);} 570 expr(A) ::= expr(X) GT expr(Y). {A = sqlite3Expr(TK_GT, X, Y, 0);} 571 expr(A) ::= expr(X) LE expr(Y). {A = sqlite3Expr(TK_LE, X, Y, 0);} 572 expr(A) ::= expr(X) GE expr(Y). {A = sqlite3Expr(TK_GE, X, Y, 0);} 573 expr(A) ::= expr(X) NE expr(Y). {A = sqlite3Expr(TK_NE, X, Y, 0);} 574 expr(A) ::= expr(X) EQ expr(Y). {A = sqlite3Expr(TK_EQ, X, Y, 0);} 575 expr(A) ::= expr(X) BITAND expr(Y). {A = sqlite3Expr(TK_BITAND, X, Y, 0);} 576 expr(A) ::= expr(X) BITOR expr(Y). {A = sqlite3Expr(TK_BITOR, X, Y, 0);} 577 expr(A) ::= expr(X) LSHIFT expr(Y). {A = sqlite3Expr(TK_LSHIFT, X, Y, 0);} 578 expr(A) ::= expr(X) RSHIFT expr(Y). {A = sqlite3Expr(TK_RSHIFT, X, Y, 0);} 579 expr(A) ::= expr(X) likeop(OP) expr(Y). [LIKE] { 580 ExprList *pList = sqlite3ExprListAppend(0, Y, 0); 581 pList = sqlite3ExprListAppend(pList, X, 0); 582 A = sqlite3ExprFunction(pList, 0); 583 if( A ) A->op = OP; 584 sqlite3ExprSpan(A, &X->span, &Y->span); 585 } 586 expr(A) ::= expr(X) NOT likeop(OP) expr(Y). [LIKE] { 587 ExprList *pList = sqlite3ExprListAppend(0, Y, 0); 588 pList = sqlite3ExprListAppend(pList, X, 0); 589 A = sqlite3ExprFunction(pList, 0); 590 if( A ) A->op = OP; 591 A = sqlite3Expr(TK_NOT, A, 0, 0); 592 sqlite3ExprSpan(A,&X->span,&Y->span); 593 } 594 %type likeop {int} 595 likeop(A) ::= LIKE. {A = TK_LIKE;} 596 likeop(A) ::= GLOB. {A = TK_GLOB;} 597 expr(A) ::= expr(X) PLUS expr(Y). {A = sqlite3Expr(TK_PLUS, X, Y, 0);} 598 expr(A) ::= expr(X) MINUS expr(Y). {A = sqlite3Expr(TK_MINUS, X, Y, 0);} 599 expr(A) ::= expr(X) STAR expr(Y). {A = sqlite3Expr(TK_STAR, X, Y, 0);} 600 expr(A) ::= expr(X) SLASH expr(Y). {A = sqlite3Expr(TK_SLASH, X, Y, 0);} 601 expr(A) ::= expr(X) REM expr(Y). {A = sqlite3Expr(TK_REM, X, Y, 0);} 602 expr(A) ::= expr(X) CONCAT expr(Y). {A = sqlite3Expr(TK_CONCAT, X, Y, 0);} 603 expr(A) ::= expr(X) ISNULL(E). { 604 A = sqlite3Expr(TK_ISNULL, X, 0, 0); 605 sqlite3ExprSpan(A,&X->span,&E); 606 } 607 expr(A) ::= expr(X) IS NULL(E). { 608 A = sqlite3Expr(TK_ISNULL, X, 0, 0); 609 sqlite3ExprSpan(A,&X->span,&E); 610 } 611 expr(A) ::= expr(X) NOTNULL(E). { 612 A = sqlite3Expr(TK_NOTNULL, X, 0, 0); 613 sqlite3ExprSpan(A,&X->span,&E); 614 } 615 expr(A) ::= expr(X) NOT NULL(E). { 616 A = sqlite3Expr(TK_NOTNULL, X, 0, 0); 617 sqlite3ExprSpan(A,&X->span,&E); 618 } 619 expr(A) ::= expr(X) IS NOT NULL(E). { 620 A = sqlite3Expr(TK_NOTNULL, X, 0, 0); 621 sqlite3ExprSpan(A,&X->span,&E); 622 } 623 expr(A) ::= NOT(B) expr(X). { 624 A = sqlite3Expr(TK_NOT, X, 0, 0); 625 sqlite3ExprSpan(A,&B,&X->span); 626 } 627 expr(A) ::= BITNOT(B) expr(X). { 628 A = sqlite3Expr(TK_BITNOT, X, 0, 0); 629 sqlite3ExprSpan(A,&B,&X->span); 630 } 631 expr(A) ::= MINUS(B) expr(X). [UMINUS] { 632 A = sqlite3Expr(TK_UMINUS, X, 0, 0); 633 sqlite3ExprSpan(A,&B,&X->span); 634 } 635 expr(A) ::= PLUS(B) expr(X). [UPLUS] { 636 A = sqlite3Expr(TK_UPLUS, X, 0, 0); 637 sqlite3ExprSpan(A,&B,&X->span); 638 } 639 expr(A) ::= LP(B) select(X) RP(E). { 640 A = sqlite3Expr(TK_SELECT, 0, 0, 0); 641 if( A ) A->pSelect = X; 642 sqlite3ExprSpan(A,&B,&E); 643 } 644 expr(A) ::= expr(W) BETWEEN expr(X) AND expr(Y). { 645 ExprList *pList = sqlite3ExprListAppend(0, X, 0); 646 pList = sqlite3ExprListAppend(pList, Y, 0); 647 A = sqlite3Expr(TK_BETWEEN, W, 0, 0); 648 if( A ) A->pList = pList; 649 sqlite3ExprSpan(A,&W->span,&Y->span); 650 } 651 expr(A) ::= expr(W) NOT BETWEEN expr(X) AND expr(Y). { 652 ExprList *pList = sqlite3ExprListAppend(0, X, 0); 653 pList = sqlite3ExprListAppend(pList, Y, 0); 654 A = sqlite3Expr(TK_BETWEEN, W, 0, 0); 655 if( A ) A->pList = pList; 656 A = sqlite3Expr(TK_NOT, A, 0, 0); 657 sqlite3ExprSpan(A,&W->span,&Y->span); 658 } 659 expr(A) ::= expr(X) IN LP exprlist(Y) RP(E). { 660 A = sqlite3Expr(TK_IN, X, 0, 0); 661 if( A ) A->pList = Y; 662 sqlite3ExprSpan(A,&X->span,&E); 663 } 664 expr(A) ::= expr(X) IN LP select(Y) RP(E). { 665 A = sqlite3Expr(TK_IN, X, 0, 0); 666 if( A ) A->pSelect = Y; 667 sqlite3ExprSpan(A,&X->span,&E); 668 } 669 expr(A) ::= expr(X) NOT IN LP exprlist(Y) RP(E). { 670 A = sqlite3Expr(TK_IN, X, 0, 0); 671 if( A ) A->pList = Y; 672 A = sqlite3Expr(TK_NOT, A, 0, 0); 673 sqlite3ExprSpan(A,&X->span,&E); 674 } 675 expr(A) ::= expr(X) NOT IN LP select(Y) RP(E). { 676 A = sqlite3Expr(TK_IN, X, 0, 0); 677 if( A ) A->pSelect = Y; 678 A = sqlite3Expr(TK_NOT, A, 0, 0); 679 sqlite3ExprSpan(A,&X->span,&E); 680 } 681 expr(A) ::= expr(X) IN nm(Y) dbnm(D). { 682 SrcList *pSrc = sqlite3SrcListAppend(0, &Y, &D); 683 A = sqlite3Expr(TK_IN, X, 0, 0); 684 if( A ) A->pSelect = sqlite3SelectNew(0,pSrc,0,0,0,0,0,-1,0); 685 sqlite3ExprSpan(A,&X->span,D.z?&D:&Y); 686 } 687 expr(A) ::= expr(X) NOT IN nm(Y) dbnm(D). { 688 SrcList *pSrc = sqlite3SrcListAppend(0, &Y, &D); 689 A = sqlite3Expr(TK_IN, X, 0, 0); 690 if( A ) A->pSelect = sqlite3SelectNew(0,pSrc,0,0,0,0,0,-1,0); 691 A = sqlite3Expr(TK_NOT, A, 0, 0); 692 sqlite3ExprSpan(A,&X->span,D.z?&D:&Y); 693 } 694 695 696 /* CASE expressions */ 697 expr(A) ::= CASE(C) case_operand(X) case_exprlist(Y) case_else(Z) END(E). { 698 A = sqlite3Expr(TK_CASE, X, Z, 0); 699 if( A ) A->pList = Y; 700 sqlite3ExprSpan(A, &C, &E); 701 } 702 %type case_exprlist {ExprList*} 703 %destructor case_exprlist {sqlite3ExprListDelete($$);} 704 case_exprlist(A) ::= case_exprlist(X) WHEN expr(Y) THEN expr(Z). { 705 A = sqlite3ExprListAppend(X, Y, 0); 706 A = sqlite3ExprListAppend(A, Z, 0); 707 } 708 case_exprlist(A) ::= WHEN expr(Y) THEN expr(Z). { 709 A = sqlite3ExprListAppend(0, Y, 0); 710 A = sqlite3ExprListAppend(A, Z, 0); 711 } 712 %type case_else {Expr*} 713 case_else(A) ::= ELSE expr(X). {A = X;} 714 case_else(A) ::= . {A = 0;} 715 %type case_operand {Expr*} 716 case_operand(A) ::= expr(X). {A = X;} 717 case_operand(A) ::= . {A = 0;} 718 719 %type exprlist {ExprList*} 720 %destructor exprlist {sqlite3ExprListDelete($$);} 721 %type expritem {Expr*} 722 %destructor expritem {sqlite3ExprDelete($$);} 723 724 exprlist(A) ::= exprlist(X) COMMA expritem(Y). 725 {A = sqlite3ExprListAppend(X,Y,0);} 726 exprlist(A) ::= expritem(X). {A = sqlite3ExprListAppend(0,X,0);} 727 expritem(A) ::= expr(X). {A = X;} 728 expritem(A) ::= . {A = 0;} 729 730 ///////////////////////////// The CREATE INDEX command /////////////////////// 731 // 732 cmd ::= CREATE(S) uniqueflag(U) INDEX nm(X) dbnm(D) 733 ON nm(Y) dbnm(C) LP idxlist(Z) RP(E) onconf(R). { 734 if( U!=OE_None ) U = R; 735 if( U==OE_Default) U = OE_Abort; 736 sqlite3CreateIndex(pParse, &X, &D, sqlite3SrcListAppend(0,&Y,&C), 737 Z, U, &S, &E); 738 } 739 740 %type uniqueflag {int} 741 uniqueflag(A) ::= UNIQUE. { A = OE_Abort; } 742 uniqueflag(A) ::= . { A = OE_None; } 743 744 %type idxlist {ExprList*} 745 %destructor idxlist {sqlite3ExprListDelete($$);} 746 %type idxlist_opt {ExprList*} 747 %destructor idxlist_opt {sqlite3ExprListDelete($$);} 748 %type idxitem {Token} 749 750 idxlist_opt(A) ::= . {A = 0;} 751 idxlist_opt(A) ::= LP idxlist(X) RP. {A = X;} 752 idxlist(A) ::= idxlist(X) COMMA idxitem(Y) collate(C) sortorder. { 753 Expr *p = 0; 754 if( C.n>0 ){ 755 p = sqlite3Expr(TK_COLUMN, 0, 0, 0); 756 if( p ) p->pColl = sqlite3LocateCollSeq(pParse, C.z, C.n); 757 } 758 A = sqlite3ExprListAppend(X, p, &Y); 759 } 760 idxlist(A) ::= idxitem(Y) collate(C) sortorder. { 761 Expr *p = 0; 762 if( C.n>0 ){ 763 p = sqlite3Expr(TK_COLUMN, 0, 0, 0); 764 if( p ) p->pColl = sqlite3LocateCollSeq(pParse, C.z, C.n); 765 } 766 A = sqlite3ExprListAppend(0, p, &Y); 767 } 768 idxitem(A) ::= nm(X). {A = X;} 769 770 771 ///////////////////////////// The DROP INDEX command ///////////////////////// 772 // 773 774 cmd ::= DROP INDEX nm(X) dbnm(Y). { 775 sqlite3DropIndex(pParse, sqlite3SrcListAppend(0,&X,&Y)); 776 } 777 778 ///////////////////////////// The VACUUM command ///////////////////////////// 779 // 780 cmd ::= VACUUM. {sqlite3Vacuum(pParse,0);} 781 cmd ::= VACUUM nm(X). {sqlite3Vacuum(pParse,&X);} 782 783 ///////////////////////////// The PRAGMA command ///////////////////////////// 784 // 785 cmd ::= PRAGMA ids(X) EQ nm(Y). {sqlite3Pragma(pParse,&X,&Y,0);} 786 cmd ::= PRAGMA ids(X) EQ ON(Y). {sqlite3Pragma(pParse,&X,&Y,0);} 787 cmd ::= PRAGMA ids(X) EQ plus_num(Y). {sqlite3Pragma(pParse,&X,&Y,0);} 788 cmd ::= PRAGMA ids(X) EQ minus_num(Y). {sqlite3Pragma(pParse,&X,&Y,1);} 789 cmd ::= PRAGMA ids(X) LP nm(Y) RP. {sqlite3Pragma(pParse,&X,&Y,0);} 790 cmd ::= PRAGMA ids(X). {sqlite3Pragma(pParse,&X,&X,0);} 791 plus_num(A) ::= plus_opt number(X). {A = X;} 792 minus_num(A) ::= MINUS number(X). {A = X;} 793 number(A) ::= INTEGER(X). {A = X;} 794 number(A) ::= FLOAT(X). {A = X;} 795 plus_opt ::= PLUS. 796 plus_opt ::= . 797 798 //////////////////////////// The CREATE TRIGGER command ///////////////////// 799 800 cmd ::= CREATE trigger_decl(A) BEGIN trigger_cmd_list(S) END(Z). { 801 Token all; 802 all.z = A.z; 803 all.n = (Z.z - A.z) + Z.n; 804 sqlite3FinishTrigger(pParse, S, &all); 805 } 806 807 trigger_decl(A) ::= temp(T) TRIGGER nm(B) dbnm(Z) trigger_time(C) trigger_event(D) 808 ON nm(E) dbnm(DB) foreach_clause(F) when_clause(G). { 809 SrcList *pTab = sqlite3SrcListAppend(0, &E, &DB); 810 sqlite3BeginTrigger(pParse, &B, &Z, C, D.a, D.b, pTab, F, G, T); 811 A = (Z.n==0?B:Z); 812 } 813 814 %type trigger_time {int} 815 trigger_time(A) ::= BEFORE. { A = TK_BEFORE; } 816 trigger_time(A) ::= AFTER. { A = TK_AFTER; } 817 trigger_time(A) ::= INSTEAD OF. { A = TK_INSTEAD;} 818 trigger_time(A) ::= . { A = TK_BEFORE; } 819 820 %type trigger_event {struct TrigEvent} 821 %destructor trigger_event {sqlite3IdListDelete($$.b);} 822 trigger_event(A) ::= DELETE. { A.a = TK_DELETE; A.b = 0; } 823 trigger_event(A) ::= INSERT. { A.a = TK_INSERT; A.b = 0; } 824 trigger_event(A) ::= UPDATE. { A.a = TK_UPDATE; A.b = 0;} 825 trigger_event(A) ::= UPDATE OF inscollist(X). {A.a = TK_UPDATE; A.b = X; } 826 827 %type foreach_clause {int} 828 foreach_clause(A) ::= . { A = TK_ROW; } 829 foreach_clause(A) ::= FOR EACH ROW. { A = TK_ROW; } 830 foreach_clause(A) ::= FOR EACH STATEMENT. { A = TK_STATEMENT; } 831 832 %type when_clause {Expr *} 833 when_clause(A) ::= . { A = 0; } 834 when_clause(A) ::= WHEN expr(X). { A = X; } 835 836 %type trigger_cmd_list {TriggerStep *} 837 %destructor trigger_cmd_list {sqlite3DeleteTriggerStep($$);} 838 trigger_cmd_list(A) ::= trigger_cmd(X) SEMI trigger_cmd_list(Y). { 839 X->pNext = Y; 840 A = X; 841 } 842 trigger_cmd_list(A) ::= . { A = 0; } 843 844 %type trigger_cmd {TriggerStep *} 845 %destructor trigger_cmd {sqlite3DeleteTriggerStep($$);} 846 // UPDATE 847 trigger_cmd(A) ::= UPDATE orconf(R) nm(X) SET setlist(Y) where_opt(Z). 848 { A = sqlite3TriggerUpdateStep(&X, Y, Z, R); } 849 850 // INSERT 851 trigger_cmd(A) ::= insert_cmd(R) INTO nm(X) inscollist_opt(F) 852 VALUES LP itemlist(Y) RP. 853 {A = sqlite3TriggerInsertStep(&X, F, Y, 0, R);} 854 855 trigger_cmd(A) ::= insert_cmd(R) INTO nm(X) inscollist_opt(F) select(S). 856 {A = sqlite3TriggerInsertStep(&X, F, 0, S, R);} 857 858 // DELETE 859 trigger_cmd(A) ::= DELETE FROM nm(X) where_opt(Y). 860 {A = sqlite3TriggerDeleteStep(&X, Y);} 861 862 // SELECT 863 trigger_cmd(A) ::= select(X). {A = sqlite3TriggerSelectStep(X); } 864 865 // The special RAISE expression that may occur in trigger programs 866 expr(A) ::= RAISE(X) LP IGNORE RP(Y). { 867 A = sqlite3Expr(TK_RAISE, 0, 0, 0); 868 A->iColumn = OE_Ignore; 869 sqlite3ExprSpan(A, &X, &Y); 870 } 871 expr(A) ::= RAISE(X) LP ROLLBACK COMMA nm(Z) RP(Y). { 872 A = sqlite3Expr(TK_RAISE, 0, 0, &Z); 873 A->iColumn = OE_Rollback; 874 sqlite3ExprSpan(A, &X, &Y); 875 } 876 expr(A) ::= RAISE(X) LP ABORT COMMA nm(Z) RP(Y). { 877 A = sqlite3Expr(TK_RAISE, 0, 0, &Z); 878 A->iColumn = OE_Abort; 879 sqlite3ExprSpan(A, &X, &Y); 880 } 881 expr(A) ::= RAISE(X) LP FAIL COMMA nm(Z) RP(Y). { 882 A = sqlite3Expr(TK_RAISE, 0, 0, &Z); 883 A->iColumn = OE_Fail; 884 sqlite3ExprSpan(A, &X, &Y); 885 } 886 887 //////////////////////// DROP TRIGGER statement ////////////////////////////// 888 cmd ::= DROP TRIGGER nm(X) dbnm(D). { 889 sqlite3DropTrigger(pParse,sqlite3SrcListAppend(0,&X,&D)); 890 } 891 892 //////////////////////// ATTACH DATABASE file AS name ///////////////////////// 893 cmd ::= ATTACH database_kw_opt ids(F) AS nm(D) key_opt(K). { 894 sqlite3Attach(pParse, &F, &D, &K); 895 } 896 %type key_opt {Token} 897 key_opt(A) ::= USING ids(X). { A = X; } 898 key_opt(A) ::= . { A.z = 0; A.n = 0; } 899 900 database_kw_opt ::= DATABASE. 901 database_kw_opt ::= . 902 903 //////////////////////// DETACH DATABASE name ///////////////////////////////// 904 cmd ::= DETACH database_kw_opt nm(D). { 905 sqlite3Detach(pParse, &D); 906 } 907