xref: /sqlite-3.40.0/src/parse.y (revision 108aa00a)
1348784efSdrh /*
2b19a2bc6Sdrh ** 2001 September 15
3348784efSdrh **
4b19a2bc6Sdrh ** The author disclaims copyright to this source code.  In place of
5b19a2bc6Sdrh ** a legal notice, here is a blessing:
6348784efSdrh **
7b19a2bc6Sdrh **    May you do good and not evil.
8b19a2bc6Sdrh **    May you find forgiveness for yourself and forgive others.
9b19a2bc6Sdrh **    May you share freely, never taking more than you give.
10348784efSdrh **
11348784efSdrh *************************************************************************
12348784efSdrh ** This file contains SQLite's grammar for SQL.  Process this file
13348784efSdrh ** using the lemon parser generator to generate C code that runs
14348784efSdrh ** the parser.  Lemon will also generate a header file containing
15348784efSdrh ** numeric codes for all of the tokens.
16348784efSdrh */
17487e262fSdrh 
18487e262fSdrh // All token codes are small integers with #defines that begin with "TK_"
19348784efSdrh %token_prefix TK_
20487e262fSdrh 
21487e262fSdrh // The type of the data attached to each token is Token.  This is also the
22487e262fSdrh // default type for non-terminals.
23487e262fSdrh //
24348784efSdrh %token_type {Token}
25f57b14a6Sdrh %default_type {Token}
26487e262fSdrh 
27487e262fSdrh // The generated parser function takes a 4th argument as follows:
28348784efSdrh %extra_argument {Parse *pParse}
29487e262fSdrh 
30487e262fSdrh // This code runs whenever there is a syntax error
31487e262fSdrh //
32348784efSdrh %syntax_error {
33128255fcSdrh   UNUSED_PARAMETER(yymajor);  /* Silence some compiler warnings */
3455176259Sdrh   assert( TOKEN.z[0] );  /* The tokenizer always gives us a token */
354adee20fSdanielk1977   sqlite3ErrorMsg(pParse, "near \"%T\": syntax error", &TOKEN);
36b86ccfb2Sdrh }
378fc3345fSdrh %stack_overflow {
38128255fcSdrh   UNUSED_PARAMETER(yypMinor); /* Silence some compiler warnings */
398fc3345fSdrh   sqlite3ErrorMsg(pParse, "parser stack overflow");
408fc3345fSdrh }
41487e262fSdrh 
42487e262fSdrh // The name of the generated procedure that implements the parser
43487e262fSdrh // is as follows:
444adee20fSdanielk1977 %name sqlite3Parser
45487e262fSdrh 
46487e262fSdrh // The following text is included near the beginning of the C source
47487e262fSdrh // code file that implements the parser.
48487e262fSdrh //
49348784efSdrh %include {
50348784efSdrh #include "sqliteInt.h"
519bbca4c1Sdrh 
529bbca4c1Sdrh /*
53d3ec02d3Sdrh ** Disable all error recovery processing in the parser push-down
54d3ec02d3Sdrh ** automaton.
55d3ec02d3Sdrh */
56d3ec02d3Sdrh #define YYNOERRORRECOVERY 1
57d3ec02d3Sdrh 
58d3ec02d3Sdrh /*
598a415d37Sdrh ** Make yytestcase() the same as testcase()
608a415d37Sdrh */
618a415d37Sdrh #define yytestcase(X) testcase(X)
628a415d37Sdrh 
638a415d37Sdrh /*
64ad3cab52Sdrh ** An instance of this structure holds information about the
65ad3cab52Sdrh ** LIMIT clause of a SELECT statement.
669bbca4c1Sdrh */
67ad3cab52Sdrh struct LimitVal {
68a2dc3b1aSdanielk1977   Expr *pLimit;    /* The LIMIT expression.  NULL if there is no limit */
69a2dc3b1aSdanielk1977   Expr *pOffset;   /* The OFFSET expression.  NULL if there is none */
70ad3cab52Sdrh };
71c3f9bad2Sdanielk1977 
72c3f9bad2Sdanielk1977 /*
732e3a1f16Sdrh ** An instance of this structure is used to store the LIKE,
742e3a1f16Sdrh ** GLOB, NOT LIKE, and NOT GLOB operators.
752e3a1f16Sdrh */
762e3a1f16Sdrh struct LikeOp {
77b52076cdSdrh   Token eOperator;  /* "like" or "glob" or "regexp" */
78f9df4498Sdrh   int bNot;         /* True if the NOT keyword is present */
792e3a1f16Sdrh };
802e3a1f16Sdrh 
812e3a1f16Sdrh /*
82ad3cab52Sdrh ** An instance of the following structure describes the event of a
83ad3cab52Sdrh ** TRIGGER.  "a" is the event type, one of TK_UPDATE, TK_INSERT,
84ad3cab52Sdrh ** TK_DELETE, or TK_INSTEAD.  If the event is of the form
85ad3cab52Sdrh **
86ad3cab52Sdrh **      UPDATE ON (a,b,c)
87ad3cab52Sdrh **
88ad3cab52Sdrh ** Then the "b" IdList records the list "a,b,c".
89c3f9bad2Sdanielk1977 */
90ad3cab52Sdrh struct TrigEvent { int a; IdList * b; };
91caec2f12Sdrh 
9225d6543dSdrh /*
9325d6543dSdrh ** An instance of this structure holds the ATTACH key and the key type.
9425d6543dSdrh */
9525d6543dSdrh struct AttachKey { int type;  Token key; };
9625d6543dSdrh 
97caec2f12Sdrh } // end %include
98348784efSdrh 
99826fb5a3Sdrh // Input is a single SQL command
100c4a3c779Sdrh input ::= cmdlist.
101094b2bbfSdrh cmdlist ::= cmdlist ecmd.
102826fb5a3Sdrh cmdlist ::= ecmd.
103b7f9164eSdrh ecmd ::= SEMI.
104b7f9164eSdrh ecmd ::= explain cmdx SEMI.
1054adee20fSdanielk1977 explain ::= .           { sqlite3BeginParse(pParse, 0); }
106b7f9164eSdrh %ifndef SQLITE_OMIT_EXPLAIN
107b7f9164eSdrh explain ::= EXPLAIN.              { sqlite3BeginParse(pParse, 1); }
108ecc9242fSdrh explain ::= EXPLAIN QUERY PLAN.   { sqlite3BeginParse(pParse, 2); }
109154d4b24Sdrh %endif  SQLITE_OMIT_EXPLAIN
110200a81dcSdrh cmdx ::= cmd.           { sqlite3FinishCoding(pParse); }
111348784efSdrh 
112382c0247Sdrh ///////////////////// Begin and end transactions. ////////////////////////////
113c4a3c779Sdrh //
114fa86c412Sdrh 
115684917c2Sdrh cmd ::= BEGIN transtype(Y) trans_opt.  {sqlite3BeginTransaction(pParse, Y);}
116c4a3c779Sdrh trans_opt ::= .
117c4a3c779Sdrh trans_opt ::= TRANSACTION.
1185ad1a6c8Sdrh trans_opt ::= TRANSACTION nm.
119684917c2Sdrh %type transtype {int}
120684917c2Sdrh transtype(A) ::= .             {A = TK_DEFERRED;}
121684917c2Sdrh transtype(A) ::= DEFERRED(X).  {A = @X;}
122684917c2Sdrh transtype(A) ::= IMMEDIATE(X). {A = @X;}
123684917c2Sdrh transtype(A) ::= EXCLUSIVE(X). {A = @X;}
1244adee20fSdanielk1977 cmd ::= COMMIT trans_opt.      {sqlite3CommitTransaction(pParse);}
1254adee20fSdanielk1977 cmd ::= END trans_opt.         {sqlite3CommitTransaction(pParse);}
1264adee20fSdanielk1977 cmd ::= ROLLBACK trans_opt.    {sqlite3RollbackTransaction(pParse);}
127c4a3c779Sdrh 
128fd7f0452Sdanielk1977 savepoint_opt ::= SAVEPOINT.
129fd7f0452Sdanielk1977 savepoint_opt ::= .
130fd7f0452Sdanielk1977 cmd ::= SAVEPOINT nm(X). {
131fd7f0452Sdanielk1977   sqlite3Savepoint(pParse, SAVEPOINT_BEGIN, &X);
132fd7f0452Sdanielk1977 }
133fd7f0452Sdanielk1977 cmd ::= RELEASE savepoint_opt nm(X). {
134fd7f0452Sdanielk1977   sqlite3Savepoint(pParse, SAVEPOINT_RELEASE, &X);
135fd7f0452Sdanielk1977 }
136fd7f0452Sdanielk1977 cmd ::= ROLLBACK trans_opt TO savepoint_opt nm(X). {
137fd7f0452Sdanielk1977   sqlite3Savepoint(pParse, SAVEPOINT_ROLLBACK, &X);
138fd7f0452Sdanielk1977 }
139fd7f0452Sdanielk1977 
140382c0247Sdrh ///////////////////// The CREATE TABLE statement ////////////////////////////
141348784efSdrh //
142348784efSdrh cmd ::= create_table create_table_args.
143d9da78a2Sdrh create_table ::= createkw temp(T) TABLE ifnotexists(E) nm(Y) dbnm(Z). {
144f1a381e7Sdanielk1977    sqlite3StartTable(pParse,&Y,&Z,T,0,0,E);
145969fa7c1Sdrh }
146d9da78a2Sdrh createkw(A) ::= CREATE(X).  {
147d9da78a2Sdrh   pParse->db->lookaside.bEnabled = 0;
148d9da78a2Sdrh   A = X;
149d9da78a2Sdrh }
150faa59554Sdrh %type ifnotexists {int}
151faa59554Sdrh ifnotexists(A) ::= .              {A = 0;}
152faa59554Sdrh ifnotexists(A) ::= IF NOT EXISTS. {A = 1;}
153f57b3399Sdrh %type temp {int}
15453c0f748Sdanielk1977 %ifndef SQLITE_OMIT_TEMPDB
155d24cc427Sdrh temp(A) ::= TEMP.  {A = 1;}
156154d4b24Sdrh %endif  SQLITE_OMIT_TEMPDB
157d24cc427Sdrh temp(A) ::= .      {A = 0;}
1585969da4aSdrh create_table_args ::= LP columnlist conslist_opt(X) RP(E) table_options(F). {
1595969da4aSdrh   sqlite3EndTable(pParse,&X,&E,F,0);
160969fa7c1Sdrh }
161969fa7c1Sdrh create_table_args ::= AS select(S). {
1625969da4aSdrh   sqlite3EndTable(pParse,0,0,0,S);
163633e6d57Sdrh   sqlite3SelectDelete(pParse->db, S);
164969fa7c1Sdrh }
1655969da4aSdrh %type table_options {u8}
1665969da4aSdrh table_options(A) ::= .    {A = 0;}
1675969da4aSdrh table_options(A) ::= WITHOUT nm(X). {
1685969da4aSdrh   if( X.n==5 && sqlite3_strnicmp(X.z,"rowid",5)==0 ){
169fccda8a1Sdrh     A = TF_WithoutRowid | TF_NoVisibleRowid;
1705969da4aSdrh   }else{
1715969da4aSdrh     A = 0;
1725969da4aSdrh     sqlite3ErrorMsg(pParse, "unknown table option: %.*s", X.n, X.z);
1735969da4aSdrh   }
1745969da4aSdrh }
175348784efSdrh columnlist ::= columnlist COMMA column.
176348784efSdrh columnlist ::= column.
177348784efSdrh 
178487e262fSdrh // A "column" is a complete description of a single column in a
179487e262fSdrh // CREATE TABLE statement.  This includes the column name, its
180487e262fSdrh // datatype, and other keywords such as PRIMARY KEY, UNIQUE, REFERENCES,
181487e262fSdrh // NOT NULL and so forth.
182348784efSdrh //
18319a8e7e8Sdanielk1977 column(A) ::= columnid(X) type carglist. {
18419a8e7e8Sdanielk1977   A.z = X.z;
185b27b7f5dSdrh   A.n = (int)(pParse->sLastToken.z-X.z) + pParse->sLastToken.n;
18619a8e7e8Sdanielk1977 }
18719a8e7e8Sdanielk1977 columnid(A) ::= nm(X). {
18819a8e7e8Sdanielk1977   sqlite3AddColumn(pParse,&X);
18919a8e7e8Sdanielk1977   A = X;
1904dc330ddSdrh   pParse->constraintName.n = 0;
19119a8e7e8Sdanielk1977 }
19219a8e7e8Sdanielk1977 
193c4a3c779Sdrh 
194c4a3c779Sdrh // An IDENTIFIER can be a generic identifier, or one of several
195c4a3c779Sdrh // keywords.  Any non-standard keyword can also be an identifier.
196c4a3c779Sdrh //
197f59b12fbSdrh %token_class id  ID|INDEXED.
1980bd1f4eaSdrh 
19934e33bb8Sdrh // The following directive causes tokens ABORT, AFTER, ASC, etc. to
20034e33bb8Sdrh // fallback to ID if they will not parse as their original value.
20134e33bb8Sdrh // This obviates the need for the "id" nonterminal.
20234e33bb8Sdrh //
2030bd1f4eaSdrh %fallback ID
2041da40a38Sdan   ABORT ACTION AFTER ANALYZE ASC ATTACH BEFORE BEGIN BY CASCADE CAST COLUMNKW
2051da40a38Sdan   CONFLICT DATABASE DEFERRED DESC DETACH EACH END EXCLUSIVE EXPLAIN FAIL FOR
2061da40a38Sdan   IGNORE IMMEDIATE INITIALLY INSTEAD LIKE_KW MATCH NO PLAN
2078b471863Sdrh   QUERY KEY OF OFFSET PRAGMA RAISE RECURSIVE RELEASE REPLACE RESTRICT ROW
2088b471863Sdrh   ROLLBACK SAVEPOINT TEMP TRIGGER VACUUM VIEW VIRTUAL WITH WITHOUT
209b7f9164eSdrh %ifdef SQLITE_OMIT_COMPOUND_SELECT
210b7f9164eSdrh   EXCEPT INTERSECT UNION
211154d4b24Sdrh %endif SQLITE_OMIT_COMPOUND_SELECT
212a073384fSdrh   REINDEX RENAME CTIME_KW IF
213b7f9164eSdrh   .
214e09daa90Sdrh %wildcard ANY.
215c4a3c779Sdrh 
216f7b5496eSdrh // Define operator precedence early so that this is the first occurrence
2172d3917daSdrh // of the operator tokens in the grammer.  Keeping the operators together
2182d3917daSdrh // causes them to be assigned integer values that are close together,
2192d3917daSdrh // which keeps parser tables smaller.
2202d3917daSdrh //
221f2bc013cSdrh // The token values assigned to these symbols is determined by the order
222f2bc013cSdrh // in which lemon first sees them.  It must be the case that ISNULL/NOTNULL,
223f2bc013cSdrh // NE/EQ, GT/LE, and GE/LT are separated by only a single value.  See
224f2bc013cSdrh // the sqlite3ExprIfFalse() routine for additional information on this
225f2bc013cSdrh // constraint.
226f2bc013cSdrh //
2272d3917daSdrh %left OR.
2282d3917daSdrh %left AND.
2292d3917daSdrh %right NOT.
23003bea70cSdrh %left IS MATCH LIKE_KW BETWEEN IN ISNULL NOTNULL NE EQ.
2319a43267bSdrh %left GT LE LT GE.
2327c6303c0Sdanielk1977 %right ESCAPE.
2332d3917daSdrh %left BITAND BITOR LSHIFT RSHIFT.
2342d3917daSdrh %left PLUS MINUS.
2352d3917daSdrh %left STAR SLASH REM.
236a34001c9Sdrh %left CONCAT.
237a34001c9Sdrh %left COLLATE.
2387ba5bc5bSdrh %right BITNOT.
2392d3917daSdrh 
240c4a3c779Sdrh // And "ids" is an identifer-or-string.
241c4a3c779Sdrh //
242f59b12fbSdrh %token_class ids  ID|STRING.
243c4a3c779Sdrh 
2445ad1a6c8Sdrh // The name of a column or table can be any of the following:
2455ad1a6c8Sdrh //
2465ad1a6c8Sdrh %type nm {Token}
247296a483cSdrh nm(A) ::= id(X).         {A = X;}
2485ad1a6c8Sdrh nm(A) ::= STRING(X).     {A = X;}
2495ad1a6c8Sdrh nm(A) ::= JOIN_KW(X).    {A = X;}
2505ad1a6c8Sdrh 
251487e262fSdrh // A typetoken is really one or more tokens that form a type name such
252487e262fSdrh // as can be found after the column name in a CREATE TABLE statement.
253487e262fSdrh // Multiple tokens are concatenated to form the value of the typetoken.
254487e262fSdrh //
255487e262fSdrh %type typetoken {Token}
256382c0247Sdrh type ::= .
257487e262fSdrh type ::= typetoken(X).                   {sqlite3AddColumnType(pParse,&X);}
258487e262fSdrh typetoken(A) ::= typename(X).   {A = X;}
259487e262fSdrh typetoken(A) ::= typename(X) LP signed RP(Y). {
260487e262fSdrh   A.z = X.z;
261b27b7f5dSdrh   A.n = (int)(&Y.z[Y.n] - X.z);
262487e262fSdrh }
263487e262fSdrh typetoken(A) ::= typename(X) LP signed COMMA signed RP(Y). {
264487e262fSdrh   A.z = X.z;
265b27b7f5dSdrh   A.n = (int)(&Y.z[Y.n] - X.z);
266487e262fSdrh }
267382c0247Sdrh %type typename {Token}
268382c0247Sdrh typename(A) ::= ids(X).             {A = X;}
269b27b7f5dSdrh typename(A) ::= typename(X) ids(Y). {A.z=X.z; A.n=Y.n+(int)(Y.z-X.z);}
27060218d2aSdrh signed ::= plus_num.
27160218d2aSdrh signed ::= minus_num.
272487e262fSdrh 
273487e262fSdrh // "carglist" is a list of additional constraints that come after the
274487e262fSdrh // column name and column type in a CREATE TABLE statement.
275487e262fSdrh //
2764dc330ddSdrh carglist ::= carglist ccons.
277348784efSdrh carglist ::= .
2784dc330ddSdrh ccons ::= CONSTRAINT nm(X).           {pParse->constraintName = X;}
279b7916a78Sdrh ccons ::= DEFAULT term(X).            {sqlite3AddDefaultValue(pParse,&X);}
280b7916a78Sdrh ccons ::= DEFAULT LP expr(X) RP.      {sqlite3AddDefaultValue(pParse,&X);}
281b7916a78Sdrh ccons ::= DEFAULT PLUS term(X).       {sqlite3AddDefaultValue(pParse,&X);}
282f96a3778Sdanielk1977 ccons ::= DEFAULT MINUS(A) term(X).      {
283b7916a78Sdrh   ExprSpan v;
284b7916a78Sdrh   v.pExpr = sqlite3PExpr(pParse, TK_UMINUS, X.pExpr, 0, 0);
285b7916a78Sdrh   v.zStart = A.z;
286b7916a78Sdrh   v.zEnd = X.zEnd;
287b7916a78Sdrh   sqlite3AddDefaultValue(pParse,&v);
2887977a17fSdanielk1977 }
2892b7acc35Sdrh ccons ::= DEFAULT id(X).              {
290b7916a78Sdrh   ExprSpan v;
291b7916a78Sdrh   spanExpr(&v, pParse, TK_STRING, &X);
292b7916a78Sdrh   sqlite3AddDefaultValue(pParse,&v);
2937977a17fSdanielk1977 }
294348784efSdrh 
295382c0247Sdrh // In addition to the type name, we also care about the primary key and
296382c0247Sdrh // UNIQUE constraints.
297348784efSdrh //
2980d316a40Sdrh ccons ::= NULL onconf.
2994adee20fSdanielk1977 ccons ::= NOT NULL onconf(R).    {sqlite3AddNotNull(pParse, R);}
300fdd6e85aSdrh ccons ::= PRIMARY KEY sortorder(Z) onconf(R) autoinc(I).
301fdd6e85aSdrh                                  {sqlite3AddPrimaryKey(pParse,0,R,I,Z);}
3028a9789b6Sdrh ccons ::= UNIQUE onconf(R).      {sqlite3CreateIndex(pParse,0,0,0,0,R,0,0,0,0);}
303b7916a78Sdrh ccons ::= CHECK LP expr(X) RP.   {sqlite3AddCheckConstraint(pParse,X.pExpr);}
304*108aa00aSdrh ccons ::= REFERENCES nm(T) eidlist_opt(TA) refargs(R).
3054adee20fSdanielk1977                                  {sqlite3CreateForeignKey(pParse,0,&T,TA,R);}
3064adee20fSdanielk1977 ccons ::= defer_subclause(D).    {sqlite3DeferForeignKey(pParse,D);}
30739002505Sdanielk1977 ccons ::= COLLATE ids(C).        {sqlite3AddCollateType(pParse, &C);}
30804738cb9Sdrh 
309205f48e6Sdrh // The optional AUTOINCREMENT keyword
310205f48e6Sdrh %type autoinc {int}
311205f48e6Sdrh autoinc(X) ::= .          {X = 0;}
3122958a4e6Sdrh autoinc(X) ::= AUTOINCR.  {X = 1;}
313205f48e6Sdrh 
314c2eef3b3Sdrh // The next group of rules parses the arguments to a REFERENCES clause
315c2eef3b3Sdrh // that determine if the referential integrity checking is deferred or
316c2eef3b3Sdrh // or immediate and which determine what action to take if a ref-integ
317c2eef3b3Sdrh // check fails.
31804738cb9Sdrh //
319c2eef3b3Sdrh %type refargs {int}
320fcf486c3Sdrh refargs(A) ::= .                  { A = OE_None*0x0101; /* EV: R-19803-45884 */}
32150af3e1dSdanielk1977 refargs(A) ::= refargs(X) refarg(Y). { A = (X & ~Y.mask) | Y.value; }
322c2eef3b3Sdrh %type refarg {struct {int value; int mask;}}
323c2eef3b3Sdrh refarg(A) ::= MATCH nm.              { A.value = 0;     A.mask = 0x000000; }
324c29c5aa1Sdrh refarg(A) ::= ON INSERT refact.      { A.value = 0;     A.mask = 0x000000; }
325c2eef3b3Sdrh refarg(A) ::= ON DELETE refact(X).   { A.value = X;     A.mask = 0x0000ff; }
326c2eef3b3Sdrh refarg(A) ::= ON UPDATE refact(X).   { A.value = X<<8;  A.mask = 0x00ff00; }
327c2eef3b3Sdrh %type refact {int}
328fcf486c3Sdrh refact(A) ::= SET NULL.              { A = OE_SetNull;  /* EV: R-33326-45252 */}
329fcf486c3Sdrh refact(A) ::= SET DEFAULT.           { A = OE_SetDflt;  /* EV: R-33326-45252 */}
330fcf486c3Sdrh refact(A) ::= CASCADE.               { A = OE_Cascade;  /* EV: R-33326-45252 */}
331fcf486c3Sdrh refact(A) ::= RESTRICT.              { A = OE_Restrict; /* EV: R-33326-45252 */}
332fcf486c3Sdrh refact(A) ::= NO ACTION.             { A = OE_None;     /* EV: R-33326-45252 */}
333c2eef3b3Sdrh %type defer_subclause {int}
3341da40a38Sdan defer_subclause(A) ::= NOT DEFERRABLE init_deferred_pred_opt.     {A = 0;}
335c2eef3b3Sdrh defer_subclause(A) ::= DEFERRABLE init_deferred_pred_opt(X).      {A = X;}
336c2eef3b3Sdrh %type init_deferred_pred_opt {int}
337c2eef3b3Sdrh init_deferred_pred_opt(A) ::= .                       {A = 0;}
338c2eef3b3Sdrh init_deferred_pred_opt(A) ::= INITIALLY DEFERRED.     {A = 1;}
339c2eef3b3Sdrh init_deferred_pred_opt(A) ::= INITIALLY IMMEDIATE.    {A = 0;}
340348784efSdrh 
34119a8e7e8Sdanielk1977 conslist_opt(A) ::= .                         {A.n = 0; A.z = 0;}
342ab35eaedSdrh conslist_opt(A) ::= COMMA(X) conslist.        {A = X;}
343ab35eaedSdrh conslist ::= conslist tconscomma tcons.
344ab35eaedSdrh conslist ::= tcons.
345ab35eaedSdrh tconscomma ::= COMMA.            {pParse->constraintName.n = 0;}
346ab35eaedSdrh tconscomma ::= .
347ab35eaedSdrh tcons ::= CONSTRAINT nm(X).      {pParse->constraintName = X;}
348*108aa00aSdrh tcons ::= PRIMARY KEY LP sortlist(X) autoinc(I) RP onconf(R).
349fdd6e85aSdrh                                  {sqlite3AddPrimaryKey(pParse,X,R,I,0);}
350*108aa00aSdrh tcons ::= UNIQUE LP sortlist(X) RP onconf(R).
3518a9789b6Sdrh                                  {sqlite3CreateIndex(pParse,0,0,0,X,R,0,0,0,0);}
352b7916a78Sdrh tcons ::= CHECK LP expr(E) RP onconf.
353b7916a78Sdrh                                  {sqlite3AddCheckConstraint(pParse,E.pExpr);}
354*108aa00aSdrh tcons ::= FOREIGN KEY LP eidlist(FA) RP
355*108aa00aSdrh           REFERENCES nm(T) eidlist_opt(TA) refargs(R) defer_subclause_opt(D). {
3564adee20fSdanielk1977     sqlite3CreateForeignKey(pParse, FA, &T, TA, R);
3574adee20fSdanielk1977     sqlite3DeferForeignKey(pParse, D);
358c2eef3b3Sdrh }
359c2eef3b3Sdrh %type defer_subclause_opt {int}
360c2eef3b3Sdrh defer_subclause_opt(A) ::= .                    {A = 0;}
361c2eef3b3Sdrh defer_subclause_opt(A) ::= defer_subclause(X).  {A = X;}
3629cfcf5d4Sdrh 
3639cfcf5d4Sdrh // The following is a non-standard extension that allows us to declare the
3649cfcf5d4Sdrh // default behavior when there is a constraint conflict.
3659cfcf5d4Sdrh //
3669cfcf5d4Sdrh %type onconf {int}
3675eff7cf0Sshane %type orconf {u8}
3681c92853dSdrh %type resolvetype {int}
3691c92853dSdrh onconf(A) ::= .                              {A = OE_Default;}
3701c92853dSdrh onconf(A) ::= ON CONFLICT resolvetype(X).    {A = X;}
3711c92853dSdrh orconf(A) ::= .                              {A = OE_Default;}
3725eff7cf0Sshane orconf(A) ::= OR resolvetype(X).             {A = (u8)X;}
37374ad7fe9Sdrh resolvetype(A) ::= raisetype(X).             {A = X;}
3741c92853dSdrh resolvetype(A) ::= IGNORE.                   {A = OE_Ignore;}
3751c92853dSdrh resolvetype(A) ::= REPLACE.                  {A = OE_Replace;}
376348784efSdrh 
377382c0247Sdrh ////////////////////////// The DROP TABLE /////////////////////////////////////
378348784efSdrh //
379a073384fSdrh cmd ::= DROP TABLE ifexists(E) fullname(X). {
380a073384fSdrh   sqlite3DropTable(pParse, X, 0, E);
381a8858103Sdanielk1977 }
382a073384fSdrh %type ifexists {int}
383a073384fSdrh ifexists(A) ::= IF EXISTS.   {A = 1;}
384a073384fSdrh ifexists(A) ::= .            {A = 0;}
385348784efSdrh 
386a76b5dfcSdrh ///////////////////// The CREATE VIEW statement /////////////////////////////
387a76b5dfcSdrh //
388b7f9164eSdrh %ifndef SQLITE_OMIT_VIEW
389*108aa00aSdrh cmd ::= createkw(X) temp(T) VIEW ifnotexists(E) nm(Y) dbnm(Z) eidlist_opt(C)
3908981b904Sdrh           AS select(S). {
3918981b904Sdrh   sqlite3CreateView(pParse, &X, &Y, &Z, C, S, T, E);
392a76b5dfcSdrh }
393a073384fSdrh cmd ::= DROP VIEW ifexists(E) fullname(X). {
394a073384fSdrh   sqlite3DropTable(pParse, X, 1, E);
395a76b5dfcSdrh }
396154d4b24Sdrh %endif  SQLITE_OMIT_VIEW
397a76b5dfcSdrh 
398382c0247Sdrh //////////////////////// The SELECT statement /////////////////////////////////
399348784efSdrh //
4007d562dbeSdan cmd ::= select(X).  {
401edf83d1eSdrh   SelectDest dest = {SRT_Output, 0, 0, 0, 0, 0};
4027d10d5a6Sdrh   sqlite3Select(pParse, X, &dest);
403633e6d57Sdrh   sqlite3SelectDelete(pParse->db, X);
4049bb61fe7Sdrh }
405efb7251dSdrh 
4069bb61fe7Sdrh %type select {Select*}
407633e6d57Sdrh %destructor select {sqlite3SelectDelete(pParse->db, $$);}
4087d562dbeSdan %type selectnowith {Select*}
4097d562dbeSdan %destructor selectnowith {sqlite3SelectDelete(pParse->db, $$);}
41082c3d636Sdrh %type oneselect {Select*}
411633e6d57Sdrh %destructor oneselect {sqlite3SelectDelete(pParse->db, $$);}
4129bb61fe7Sdrh 
413772460fdSdrh %include {
414772460fdSdrh   /*
415772460fdSdrh   ** For a compound SELECT statement, make sure p->pPrior->pNext==p for
416772460fdSdrh   ** all elements in the list.  And make sure list length does not exceed
417772460fdSdrh   ** SQLITE_LIMIT_COMPOUND_SELECT.
418772460fdSdrh   */
419e318a7f8Sdrh   static void parserDoubleLinkSelect(Parse *pParse, Select *p){
420d227a291Sdrh     if( p->pPrior ){
421772460fdSdrh       Select *pNext = 0, *pLoop;
422772460fdSdrh       int mxSelect, cnt = 0;
423d227a291Sdrh       for(pLoop=p; pLoop; pNext=pLoop, pLoop=pLoop->pPrior, cnt++){
424d227a291Sdrh         pLoop->pNext = pNext;
425d227a291Sdrh         pLoop->selFlags |= SF_Compound;
426d227a291Sdrh       }
427772460fdSdrh       if( (p->selFlags & SF_MultiValue)==0 &&
428772460fdSdrh         (mxSelect = pParse->db->aLimit[SQLITE_LIMIT_COMPOUND_SELECT])>0 &&
429772460fdSdrh         cnt>mxSelect
430a0c01768Sdrh       ){
431d227a291Sdrh         sqlite3ErrorMsg(pParse, "too many terms in compound SELECT");
432d227a291Sdrh       }
433d227a291Sdrh     }
434772460fdSdrh   }
435772460fdSdrh }
436772460fdSdrh 
437772460fdSdrh select(A) ::= with(W) selectnowith(X). {
438772460fdSdrh   Select *p = X;
439772460fdSdrh   if( p ){
440772460fdSdrh     p->pWith = W;
441772460fdSdrh     parserDoubleLinkSelect(pParse, p);
442a9f5c13dSdan   }else{
443a9f5c13dSdan     sqlite3WithDelete(pParse->db, W);
444a9f5c13dSdan   }
445d227a291Sdrh   A = p;
446a9f5c13dSdan }
4477d562dbeSdan 
4487d562dbeSdan selectnowith(A) ::= oneselect(X).                      {A = X;}
449b7f9164eSdrh %ifndef SQLITE_OMIT_COMPOUND_SELECT
4507d562dbeSdan selectnowith(A) ::= selectnowith(X) multiselect_op(Y) oneselect(Z).  {
451c0bf493eSdrh   Select *pRhs = Z;
45200d5ab74Sdrh   Select *pLhs = X;
453c0bf493eSdrh   if( pRhs && pRhs->pPrior ){
454c0bf493eSdrh     SrcList *pFrom;
455c0bf493eSdrh     Token x;
456c0bf493eSdrh     x.n = 0;
457772460fdSdrh     parserDoubleLinkSelect(pParse, pRhs);
458c0bf493eSdrh     pFrom = sqlite3SrcListAppendFromTerm(pParse,0,0,0,&x,pRhs,0,0);
459c0bf493eSdrh     pRhs = sqlite3SelectNew(pParse,0,pFrom,0,0,0,0,0,0,0);
460c0bf493eSdrh   }
461c0bf493eSdrh   if( pRhs ){
462c0bf493eSdrh     pRhs->op = (u8)Y;
46300d5ab74Sdrh     pRhs->pPrior = pLhs;
46400d5ab74Sdrh     if( ALWAYS(pLhs) ) pLhs->selFlags &= ~SF_MultiValue;
465772460fdSdrh     pRhs->selFlags &= ~SF_MultiValue;
466d58d3278Sdrh     if( Y!=TK_ALL ) pParse->hasCompound = 1;
46743b78826Sdrh   }else{
46800d5ab74Sdrh     sqlite3SelectDelete(pParse->db, pLhs);
469daffd0e5Sdrh   }
470c0bf493eSdrh   A = pRhs;
47182c3d636Sdrh }
4720a36c57eSdrh %type multiselect_op {int}
47374ad7fe9Sdrh multiselect_op(A) ::= UNION(OP).             {A = @OP;}
4740a36c57eSdrh multiselect_op(A) ::= UNION ALL.             {A = TK_ALL;}
475fd405314Sdrh multiselect_op(A) ::= EXCEPT|INTERSECT(OP).  {A = @OP;}
476154d4b24Sdrh %endif SQLITE_OMIT_COMPOUND_SELECT
477abd4c723Sdrh oneselect(A) ::= SELECT(S) distinct(D) selcollist(W) from(X) where_opt(Y)
4789bbca4c1Sdrh                  groupby_opt(P) having_opt(Q) orderby_opt(Z) limit_opt(L). {
47917435752Sdrh   A = sqlite3SelectNew(pParse,W,X,Y,P,Q,Z,D,L.pLimit,L.pOffset);
480abd4c723Sdrh #if SELECTTRACE_ENABLED
481eb9b884cSdrh   /* Populate the Select.zSelName[] string that is used to help with
482abd4c723Sdrh   ** query planner debugging, to differentiate between multiple Select
483abd4c723Sdrh   ** objects in a complex query.
484abd4c723Sdrh   **
485abd4c723Sdrh   ** If the SELECT keyword is immediately followed by a C-style comment
486abd4c723Sdrh   ** then extract the first few alphanumeric characters from within that
487eb9b884cSdrh   ** comment to be the zSelName value.  Otherwise, the label is #N where
488abd4c723Sdrh   ** is an integer that is incremented with each SELECT statement seen.
489abd4c723Sdrh   */
490abd4c723Sdrh   if( A!=0 ){
491abd4c723Sdrh     const char *z = S.z+6;
492abd4c723Sdrh     int i;
493eb9b884cSdrh     sqlite3_snprintf(sizeof(A->zSelName), A->zSelName, "#%d",
494abd4c723Sdrh                      ++pParse->nSelect);
495abd4c723Sdrh     while( z[0]==' ' ) z++;
496abd4c723Sdrh     if( z[0]=='/' && z[1]=='*' ){
497abd4c723Sdrh       z += 2;
498abd4c723Sdrh       while( z[0]==' ' ) z++;
499abd4c723Sdrh       for(i=0; sqlite3Isalnum(z[i]); i++){}
500eb9b884cSdrh       sqlite3_snprintf(sizeof(A->zSelName), A->zSelName, "%.*s", i, z);
501abd4c723Sdrh     }
502abd4c723Sdrh   }
503abd4c723Sdrh #endif /* SELECTRACE_ENABLED */
5049bb61fe7Sdrh }
50575593d96Sdrh oneselect(A) ::= values(X).    {A = X;}
50675593d96Sdrh 
50775593d96Sdrh %type values {Select*}
50875593d96Sdrh %destructor values {sqlite3SelectDelete(pParse->db, $$);}
50975593d96Sdrh values(A) ::= VALUES LP nexprlist(X) RP. {
51075593d96Sdrh   A = sqlite3SelectNew(pParse,X,0,0,0,0,0,SF_Values,0,0);
51175593d96Sdrh }
51275593d96Sdrh values(A) ::= values(X) COMMA LP exprlist(Y) RP. {
513772460fdSdrh   Select *pRight, *pLeft = X;
514772460fdSdrh   pRight = sqlite3SelectNew(pParse,Y,0,0,0,0,0,SF_Values|SF_MultiValue,0,0);
515f3151f0aSdrh   if( ALWAYS(pLeft) ) pLeft->selFlags &= ~SF_MultiValue;
51675593d96Sdrh   if( pRight ){
51775593d96Sdrh     pRight->op = TK_ALL;
518772460fdSdrh     pLeft = X;
519772460fdSdrh     pRight->pPrior = pLeft;
52075593d96Sdrh     A = pRight;
52175593d96Sdrh   }else{
522772460fdSdrh     A = pLeft;
52375593d96Sdrh   }
52475593d96Sdrh }
5259bb61fe7Sdrh 
5269bb61fe7Sdrh // The "distinct" nonterminal is true (1) if the DISTINCT keyword is
5279bb61fe7Sdrh // present and false (0) if it is not.
5289bb61fe7Sdrh //
529832ee3d4Sdrh %type distinct {u16}
530832ee3d4Sdrh distinct(A) ::= DISTINCT.   {A = SF_Distinct;}
5317cea7f95Sdrh distinct(A) ::= ALL.        {A = SF_All;}
532efb7251dSdrh distinct(A) ::= .           {A = 0;}
533348784efSdrh 
5349bb61fe7Sdrh // selcollist is a list of expressions that are to become the return
5357c917d19Sdrh // values of the SELECT statement.  The "*" in statements like
5367c917d19Sdrh // "SELECT * FROM ..." is encoded as a special expression with an
5377c917d19Sdrh // opcode of TK_ALL.
5389bb61fe7Sdrh //
539348784efSdrh %type selcollist {ExprList*}
540633e6d57Sdrh %destructor selcollist {sqlite3ExprListDelete(pParse->db, $$);}
541348784efSdrh %type sclp {ExprList*}
542633e6d57Sdrh %destructor sclp {sqlite3ExprListDelete(pParse->db, $$);}
543348784efSdrh sclp(A) ::= selcollist(X) COMMA.             {A = X;}
544348784efSdrh sclp(A) ::= .                                {A = 0;}
54501f3f253Sdrh selcollist(A) ::= sclp(P) expr(X) as(Y).     {
546b7916a78Sdrh    A = sqlite3ExprListAppend(pParse, P, X.pExpr);
547b7916a78Sdrh    if( Y.n>0 ) sqlite3ExprListSetName(pParse, A, &Y, 1);
548b7916a78Sdrh    sqlite3ExprListSetSpan(pParse,A,&X);
54901f3f253Sdrh }
5507c917d19Sdrh selcollist(A) ::= sclp(P) STAR. {
551b7916a78Sdrh   Expr *p = sqlite3Expr(pParse->db, TK_ALL, 0);
552b7916a78Sdrh   A = sqlite3ExprListAppend(pParse, P, p);
5537c917d19Sdrh }
554e54a62adSdrh selcollist(A) ::= sclp(P) nm(X) DOT STAR(Y). {
555e54a62adSdrh   Expr *pRight = sqlite3PExpr(pParse, TK_ALL, 0, 0, &Y);
55617435752Sdrh   Expr *pLeft = sqlite3PExpr(pParse, TK_ID, 0, 0, &X);
55717435752Sdrh   Expr *pDot = sqlite3PExpr(pParse, TK_DOT, pLeft, pRight, 0);
558b7916a78Sdrh   A = sqlite3ExprListAppend(pParse,P, pDot);
55954473229Sdrh }
56001f3f253Sdrh 
56101f3f253Sdrh // An option "AS <id>" phrase that can follow one of the expressions that
56201f3f253Sdrh // define the result set, or one of the tables in the FROM clause.
56301f3f253Sdrh //
56401f3f253Sdrh %type as {Token}
5655ad1a6c8Sdrh as(X) ::= AS nm(Y).    {X = Y;}
5665ad1a6c8Sdrh as(X) ::= ids(Y).      {X = Y;}
56701f3f253Sdrh as(X) ::= .            {X.n = 0;}
5689bb61fe7Sdrh 
569348784efSdrh 
570ad3cab52Sdrh %type seltablist {SrcList*}
571633e6d57Sdrh %destructor seltablist {sqlite3SrcListDelete(pParse->db, $$);}
572ad3cab52Sdrh %type stl_prefix {SrcList*}
573633e6d57Sdrh %destructor stl_prefix {sqlite3SrcListDelete(pParse->db, $$);}
574ad3cab52Sdrh %type from {SrcList*}
575633e6d57Sdrh %destructor from {sqlite3SrcListDelete(pParse->db, $$);}
576348784efSdrh 
57701f3f253Sdrh // A complete FROM clause.
57801f3f253Sdrh //
57917435752Sdrh from(A) ::= .                {A = sqlite3DbMallocZero(pParse->db, sizeof(*A));}
58061dfc31dSdrh from(A) ::= FROM seltablist(X). {
58161dfc31dSdrh   A = X;
58261dfc31dSdrh   sqlite3SrcListShiftJoinType(A);
58361dfc31dSdrh }
58401f3f253Sdrh 
58501f3f253Sdrh // "seltablist" is a "Select Table List" - the content of the FROM clause
58601f3f253Sdrh // in a SELECT statement.  "stl_prefix" is a prefix of this list.
58701f3f253Sdrh //
58801f3f253Sdrh stl_prefix(A) ::= seltablist(X) joinop(Y).    {
58901f3f253Sdrh    A = X;
5908a48b9c0Sdrh    if( ALWAYS(A && A->nSrc>0) ) A->a[A->nSrc-1].fg.jointype = (u8)Y;
59101f3f253Sdrh }
592348784efSdrh stl_prefix(A) ::= .                           {A = 0;}
593e9240418Sdrh seltablist(A) ::= stl_prefix(X) nm(Y) dbnm(D) as(Z) indexed_opt(I)
594e9240418Sdrh                   on_opt(N) using_opt(U). {
595b1c685b0Sdanielk1977   A = sqlite3SrcListAppendFromTerm(pParse,X,&Y,&D,&Z,0,N,U);
596b1c685b0Sdanielk1977   sqlite3SrcListIndexedBy(pParse, A, &I);
59701f3f253Sdrh }
59801d230ceSdrh seltablist(A) ::= stl_prefix(X) nm(Y) dbnm(D) LP exprlist(E) RP as(Z)
59901d230ceSdrh                   on_opt(N) using_opt(U). {
60001d230ceSdrh   A = sqlite3SrcListAppendFromTerm(pParse,X,&Y,&D,&Z,0,N,U);
60101d230ceSdrh   sqlite3SrcListFuncArgs(pParse, A, E);
60201d230ceSdrh }
60351522cd3Sdrh %ifndef SQLITE_OMIT_SUBQUERY
604fbdc7f69Sdrh   seltablist(A) ::= stl_prefix(X) LP select(S) RP
605b733d037Sdrh                     as(Z) on_opt(N) using_opt(U). {
606b1c685b0Sdanielk1977     A = sqlite3SrcListAppendFromTerm(pParse,X,0,0,&Z,S,N,U);
60722f70c32Sdrh   }
608fbdc7f69Sdrh   seltablist(A) ::= stl_prefix(X) LP seltablist(F) RP
609fbdc7f69Sdrh                     as(Z) on_opt(N) using_opt(U). {
6109b87d7b9Sdanielk1977     if( X==0 && Z.n==0 && N==0 && U==0 ){
611fbdc7f69Sdrh       A = F;
612832ee3d4Sdrh     }else if( F->nSrc==1 ){
613832ee3d4Sdrh       A = sqlite3SrcListAppendFromTerm(pParse,X,0,0,&Z,0,N,U);
614832ee3d4Sdrh       if( A ){
615832ee3d4Sdrh         struct SrcList_item *pNew = &A->a[A->nSrc-1];
616832ee3d4Sdrh         struct SrcList_item *pOld = F->a;
617832ee3d4Sdrh         pNew->zName = pOld->zName;
618832ee3d4Sdrh         pNew->zDatabase = pOld->zDatabase;
6193c449c6bSdrh         pNew->pSelect = pOld->pSelect;
620832ee3d4Sdrh         pOld->zName = pOld->zDatabase = 0;
6213c449c6bSdrh         pOld->pSelect = 0;
622832ee3d4Sdrh       }
623832ee3d4Sdrh       sqlite3SrcListDelete(pParse->db, F);
624fbdc7f69Sdrh     }else{
625fbdc7f69Sdrh       Select *pSubquery;
626fbdc7f69Sdrh       sqlite3SrcListShiftJoinType(F);
627832ee3d4Sdrh       pSubquery = sqlite3SelectNew(pParse,0,F,0,0,0,0,SF_NestedFrom,0,0);
628fbdc7f69Sdrh       A = sqlite3SrcListAppendFromTerm(pParse,X,0,0,&Z,pSubquery,N,U);
629fbdc7f69Sdrh     }
630fbdc7f69Sdrh   }
631154d4b24Sdrh %endif  SQLITE_OMIT_SUBQUERY
632b733d037Sdrh 
633113088ecSdrh %type dbnm {Token}
634113088ecSdrh dbnm(A) ::= .          {A.z=0; A.n=0;}
635113088ecSdrh dbnm(A) ::= DOT nm(X). {A = X;}
636113088ecSdrh 
63774ad7fe9Sdrh %type fullname {SrcList*}
638633e6d57Sdrh %destructor fullname {sqlite3SrcListDelete(pParse->db, $$);}
63917435752Sdrh fullname(A) ::= nm(X) dbnm(Y).  {A = sqlite3SrcListAppend(pParse->db,0,&X,&Y);}
64074ad7fe9Sdrh 
64101f3f253Sdrh %type joinop {int}
64201f3f253Sdrh %type joinop2 {int}
643fd405314Sdrh joinop(X) ::= COMMA|JOIN.              { X = JT_INNER; }
6444adee20fSdanielk1977 joinop(X) ::= JOIN_KW(A) JOIN.         { X = sqlite3JoinType(pParse,&A,0,0); }
6454adee20fSdanielk1977 joinop(X) ::= JOIN_KW(A) nm(B) JOIN.   { X = sqlite3JoinType(pParse,&A,&B,0); }
6465ad1a6c8Sdrh joinop(X) ::= JOIN_KW(A) nm(B) nm(C) JOIN.
6474adee20fSdanielk1977                                        { X = sqlite3JoinType(pParse,&A,&B,&C); }
64801f3f253Sdrh 
64901f3f253Sdrh %type on_opt {Expr*}
650633e6d57Sdrh %destructor on_opt {sqlite3ExprDelete(pParse->db, $$);}
651b7916a78Sdrh on_opt(N) ::= ON expr(E).   {N = E.pExpr;}
65201f3f253Sdrh on_opt(N) ::= .             {N = 0;}
65301f3f253Sdrh 
65485574e31Sdanielk1977 // Note that this block abuses the Token type just a little. If there is
65585574e31Sdanielk1977 // no "INDEXED BY" clause, the returned token is empty (z==0 && n==0). If
65685574e31Sdanielk1977 // there is an INDEXED BY clause, then the token is populated as per normal,
65785574e31Sdanielk1977 // with z pointing to the token data and n containing the number of bytes
65885574e31Sdanielk1977 // in the token.
65985574e31Sdanielk1977 //
66085574e31Sdanielk1977 // If there is a "NOT INDEXED" clause, then (z==0 && n==1), which is
661b1c685b0Sdanielk1977 // normally illegal. The sqlite3SrcListIndexedBy() function
66285574e31Sdanielk1977 // recognizes and interprets this as a special case.
66385574e31Sdanielk1977 //
66485574e31Sdanielk1977 %type indexed_opt {Token}
66585574e31Sdanielk1977 indexed_opt(A) ::= .                 {A.z=0; A.n=0;}
66685574e31Sdanielk1977 indexed_opt(A) ::= INDEXED BY nm(X). {A = X;}
66785574e31Sdanielk1977 indexed_opt(A) ::= NOT INDEXED.      {A.z=0; A.n=1;}
66885574e31Sdanielk1977 
66901f3f253Sdrh %type using_opt {IdList*}
670633e6d57Sdrh %destructor using_opt {sqlite3IdListDelete(pParse->db, $$);}
67181eba73eSdrh using_opt(U) ::= USING LP idlist(L) RP.  {U = L;}
67201f3f253Sdrh using_opt(U) ::= .                        {U = 0;}
67301f3f253Sdrh 
67401f3f253Sdrh 
675348784efSdrh %type orderby_opt {ExprList*}
676633e6d57Sdrh %destructor orderby_opt {sqlite3ExprListDelete(pParse->db, $$);}
677*108aa00aSdrh 
678*108aa00aSdrh // the sortlist non-terminal stores a list of expression where each
679*108aa00aSdrh // expression is optionally followed by ASC or DESC to indicate the
680*108aa00aSdrh // sort order.
681*108aa00aSdrh //
682348784efSdrh %type sortlist {ExprList*}
683633e6d57Sdrh %destructor sortlist {sqlite3ExprListDelete(pParse->db, $$);}
684348784efSdrh 
685348784efSdrh orderby_opt(A) ::= .                          {A = 0;}
686348784efSdrh orderby_opt(A) ::= ORDER BY sortlist(X).      {A = X;}
6878395b7b6Sdrh sortlist(A) ::= sortlist(X) COMMA expr(Y) sortorder(Z). {
6888395b7b6Sdrh   A = sqlite3ExprListAppend(pParse,X,Y.pExpr);
689bc622bc0Sdrh   sqlite3ExprListSetSortOrder(A,Z);
690348784efSdrh }
6918395b7b6Sdrh sortlist(A) ::= expr(Y) sortorder(Z). {
6928395b7b6Sdrh   A = sqlite3ExprListAppend(pParse,0,Y.pExpr);
693bc622bc0Sdrh   sqlite3ExprListSetSortOrder(A,Z);
694348784efSdrh }
695348784efSdrh 
696348784efSdrh %type sortorder {int}
697348784efSdrh 
6988e2ca029Sdrh sortorder(A) ::= ASC.           {A = SQLITE_SO_ASC;}
6998e2ca029Sdrh sortorder(A) ::= DESC.          {A = SQLITE_SO_DESC;}
700bc622bc0Sdrh sortorder(A) ::= .              {A = SQLITE_SO_UNDEFINED;}
701348784efSdrh 
7022282792aSdrh %type groupby_opt {ExprList*}
703633e6d57Sdrh %destructor groupby_opt {sqlite3ExprListDelete(pParse->db, $$);}
7042282792aSdrh groupby_opt(A) ::= .                      {A = 0;}
7059245c243Sdrh groupby_opt(A) ::= GROUP BY nexprlist(X). {A = X;}
7062282792aSdrh 
7072282792aSdrh %type having_opt {Expr*}
708633e6d57Sdrh %destructor having_opt {sqlite3ExprDelete(pParse->db, $$);}
7092282792aSdrh having_opt(A) ::= .                {A = 0;}
710b7916a78Sdrh having_opt(A) ::= HAVING expr(X).  {A = X.pExpr;}
7112282792aSdrh 
712ad3cab52Sdrh %type limit_opt {struct LimitVal}
71315926590Sdrh 
71415926590Sdrh // The destructor for limit_opt will never fire in the current grammar.
71515926590Sdrh // The limit_opt non-terminal only occurs at the end of a single production
71615926590Sdrh // rule for SELECT statements.  As soon as the rule that create the
71715926590Sdrh // limit_opt non-terminal reduces, the SELECT statement rule will also
71815926590Sdrh // reduce.  So there is never a limit_opt non-terminal on the stack
71915926590Sdrh // except as a transient.  So there is never anything to destroy.
72015926590Sdrh //
72115926590Sdrh //%destructor limit_opt {
722633e6d57Sdrh //  sqlite3ExprDelete(pParse->db, $$.pLimit);
723633e6d57Sdrh //  sqlite3ExprDelete(pParse->db, $$.pOffset);
72415926590Sdrh //}
725a2dc3b1aSdanielk1977 limit_opt(A) ::= .                    {A.pLimit = 0; A.pOffset = 0;}
726b7916a78Sdrh limit_opt(A) ::= LIMIT expr(X).       {A.pLimit = X.pExpr; A.pOffset = 0;}
727a2dc3b1aSdanielk1977 limit_opt(A) ::= LIMIT expr(X) OFFSET expr(Y).
728b7916a78Sdrh                                       {A.pLimit = X.pExpr; A.pOffset = Y.pExpr;}
729a2dc3b1aSdanielk1977 limit_opt(A) ::= LIMIT expr(X) COMMA expr(Y).
730b7916a78Sdrh                                       {A.pOffset = X.pExpr; A.pLimit = Y.pExpr;}
7319bbca4c1Sdrh 
732382c0247Sdrh /////////////////////////// The DELETE statement /////////////////////////////
733382c0247Sdrh //
734273f619bSshane %ifdef SQLITE_ENABLE_UPDATE_DELETE_LIMIT
7354e9119d9Sdan cmd ::= with(C) DELETE FROM fullname(X) indexed_opt(I) where_opt(W)
736931577f1Sdrh         orderby_opt(O) limit_opt(L). {
737b290f117Sdan   sqlite3WithPush(pParse, C, 1);
738b1c685b0Sdanielk1977   sqlite3SrcListIndexedBy(pParse, X, &I);
73949ffdbf4Sshane   W = sqlite3LimitWhere(pParse, X, W, O, L.pLimit, L.pOffset, "DELETE");
7404281bd42Sshane   sqlite3DeleteFrom(pParse,X,W);
7414281bd42Sshane }
7424281bd42Sshane %endif
743273f619bSshane %ifndef SQLITE_ENABLE_UPDATE_DELETE_LIMIT
7444e9119d9Sdan cmd ::= with(C) DELETE FROM fullname(X) indexed_opt(I) where_opt(W). {
745b290f117Sdan   sqlite3WithPush(pParse, C, 1);
7464281bd42Sshane   sqlite3SrcListIndexedBy(pParse, X, &I);
7474281bd42Sshane   sqlite3DeleteFrom(pParse,X,W);
7484281bd42Sshane }
7494281bd42Sshane %endif
750348784efSdrh 
751348784efSdrh %type where_opt {Expr*}
752633e6d57Sdrh %destructor where_opt {sqlite3ExprDelete(pParse->db, $$);}
753348784efSdrh 
754348784efSdrh where_opt(A) ::= .                    {A = 0;}
755b7916a78Sdrh where_opt(A) ::= WHERE expr(X).       {A = X.pExpr;}
756348784efSdrh 
757382c0247Sdrh ////////////////////////// The UPDATE command ////////////////////////////////
758382c0247Sdrh //
759273f619bSshane %ifdef SQLITE_ENABLE_UPDATE_DELETE_LIMIT
760bfe31e7fSdan cmd ::= with(C) UPDATE orconf(R) fullname(X) indexed_opt(I) SET setlist(Y)
7618b471863Sdrh         where_opt(W) orderby_opt(O) limit_opt(L).  {
762b290f117Sdan   sqlite3WithPush(pParse, C, 1);
763b1c685b0Sdanielk1977   sqlite3SrcListIndexedBy(pParse, X, &I);
764b1a6c3c1Sdrh   sqlite3ExprListCheckLength(pParse,Y,"set list");
76549ffdbf4Sshane   W = sqlite3LimitWhere(pParse, X, W, O, L.pLimit, L.pOffset, "UPDATE");
7664281bd42Sshane   sqlite3Update(pParse,X,Y,W,R);
7674281bd42Sshane }
7684281bd42Sshane %endif
769273f619bSshane %ifndef SQLITE_ENABLE_UPDATE_DELETE_LIMIT
7704e9119d9Sdan cmd ::= with(C) UPDATE orconf(R) fullname(X) indexed_opt(I) SET setlist(Y)
771e9240418Sdrh         where_opt(W).  {
772b290f117Sdan   sqlite3WithPush(pParse, C, 1);
7734281bd42Sshane   sqlite3SrcListIndexedBy(pParse, X, &I);
7744281bd42Sshane   sqlite3ExprListCheckLength(pParse,Y,"set list");
7754281bd42Sshane   sqlite3Update(pParse,X,Y,W,R);
7764281bd42Sshane }
7774281bd42Sshane %endif
778348784efSdrh 
779f8db1bc0Sdrh %type setlist {ExprList*}
780633e6d57Sdrh %destructor setlist {sqlite3ExprListDelete(pParse->db, $$);}
781f8db1bc0Sdrh 
782b7916a78Sdrh setlist(A) ::= setlist(Z) COMMA nm(X) EQ expr(Y). {
783b7916a78Sdrh   A = sqlite3ExprListAppend(pParse, Z, Y.pExpr);
784b7916a78Sdrh   sqlite3ExprListSetName(pParse, A, &X, 1);
785b7916a78Sdrh }
786b7916a78Sdrh setlist(A) ::= nm(X) EQ expr(Y). {
787b7916a78Sdrh   A = sqlite3ExprListAppend(pParse, 0, Y.pExpr);
788b7916a78Sdrh   sqlite3ExprListSetName(pParse, A, &X, 1);
789b7916a78Sdrh }
790348784efSdrh 
791382c0247Sdrh ////////////////////////// The INSERT command /////////////////////////////////
792382c0247Sdrh //
7938981b904Sdrh cmd ::= with(W) insert_cmd(R) INTO fullname(X) idlist_opt(F) select(S). {
794b290f117Sdan   sqlite3WithPush(pParse, W, 1);
7954e9119d9Sdan   sqlite3Insert(pParse, X, S, F, R);
7964e9119d9Sdan }
7978981b904Sdrh cmd ::= with(W) insert_cmd(R) INTO fullname(X) idlist_opt(F) DEFAULT VALUES.
7984e9119d9Sdan {
799b290f117Sdan   sqlite3WithPush(pParse, W, 1);
8004e9119d9Sdan   sqlite3Insert(pParse, X, 0, F, R);
8014e9119d9Sdan }
802348784efSdrh 
8035eff7cf0Sshane %type insert_cmd {u8}
804fa86c412Sdrh insert_cmd(A) ::= INSERT orconf(R).   {A = R;}
805fa86c412Sdrh insert_cmd(A) ::= REPLACE.            {A = OE_Replace;}
806fa86c412Sdrh 
8078981b904Sdrh %type idlist_opt {IdList*}
8088981b904Sdrh %destructor idlist_opt {sqlite3IdListDelete(pParse->db, $$);}
80981eba73eSdrh %type idlist {IdList*}
81081eba73eSdrh %destructor idlist {sqlite3IdListDelete(pParse->db, $$);}
811348784efSdrh 
8128981b904Sdrh idlist_opt(A) ::= .                       {A = 0;}
8138981b904Sdrh idlist_opt(A) ::= LP idlist(X) RP.    {A = X;}
81481eba73eSdrh idlist(A) ::= idlist(X) COMMA nm(Y).
81517435752Sdrh     {A = sqlite3IdListAppend(pParse->db,X,&Y);}
81681eba73eSdrh idlist(A) ::= nm(Y).
81717435752Sdrh     {A = sqlite3IdListAppend(pParse->db,0,&Y);}
818348784efSdrh 
819382c0247Sdrh /////////////////////////// Expression Processing /////////////////////////////
820382c0247Sdrh //
821348784efSdrh 
822b7916a78Sdrh %type expr {ExprSpan}
823b7916a78Sdrh %destructor expr {sqlite3ExprDelete(pParse->db, $$.pExpr);}
824b7916a78Sdrh %type term {ExprSpan}
825b7916a78Sdrh %destructor term {sqlite3ExprDelete(pParse->db, $$.pExpr);}
826b7916a78Sdrh 
827b7916a78Sdrh %include {
828b7916a78Sdrh   /* This is a utility routine used to set the ExprSpan.zStart and
829b7916a78Sdrh   ** ExprSpan.zEnd values of pOut so that the span covers the complete
830b7916a78Sdrh   ** range of text beginning with pStart and going to the end of pEnd.
831b7916a78Sdrh   */
832b7916a78Sdrh   static void spanSet(ExprSpan *pOut, Token *pStart, Token *pEnd){
833b7916a78Sdrh     pOut->zStart = pStart->z;
834b7916a78Sdrh     pOut->zEnd = &pEnd->z[pEnd->n];
835b7916a78Sdrh   }
836b7916a78Sdrh 
837b7916a78Sdrh   /* Construct a new Expr object from a single identifier.  Use the
838b7916a78Sdrh   ** new Expr to populate pOut.  Set the span of pOut to be the identifier
839b7916a78Sdrh   ** that created the expression.
840b7916a78Sdrh   */
841b7916a78Sdrh   static void spanExpr(ExprSpan *pOut, Parse *pParse, int op, Token *pValue){
842b7916a78Sdrh     pOut->pExpr = sqlite3PExpr(pParse, op, 0, 0, pValue);
843b7916a78Sdrh     pOut->zStart = pValue->z;
844b7916a78Sdrh     pOut->zEnd = &pValue->z[pValue->n];
845b7916a78Sdrh   }
846b7916a78Sdrh }
847348784efSdrh 
8487977a17fSdanielk1977 expr(A) ::= term(X).             {A = X;}
849b7916a78Sdrh expr(A) ::= LP(B) expr(X) RP(E). {A.pExpr = X.pExpr; spanSet(&A,&B,&E);}
850b7916a78Sdrh term(A) ::= NULL(X).             {spanExpr(&A, pParse, @X, &X);}
851b7916a78Sdrh expr(A) ::= id(X).               {spanExpr(&A, pParse, TK_ID, &X);}
852b7916a78Sdrh expr(A) ::= JOIN_KW(X).          {spanExpr(&A, pParse, TK_ID, &X);}
8535ad1a6c8Sdrh expr(A) ::= nm(X) DOT nm(Y). {
85417435752Sdrh   Expr *temp1 = sqlite3PExpr(pParse, TK_ID, 0, 0, &X);
85517435752Sdrh   Expr *temp2 = sqlite3PExpr(pParse, TK_ID, 0, 0, &Y);
856b7916a78Sdrh   A.pExpr = sqlite3PExpr(pParse, TK_DOT, temp1, temp2, 0);
857b7916a78Sdrh   spanSet(&A,&X,&Y);
858e1b6a5b8Sdrh }
859d24cc427Sdrh expr(A) ::= nm(X) DOT nm(Y) DOT nm(Z). {
86017435752Sdrh   Expr *temp1 = sqlite3PExpr(pParse, TK_ID, 0, 0, &X);
86117435752Sdrh   Expr *temp2 = sqlite3PExpr(pParse, TK_ID, 0, 0, &Y);
86217435752Sdrh   Expr *temp3 = sqlite3PExpr(pParse, TK_ID, 0, 0, &Z);
86317435752Sdrh   Expr *temp4 = sqlite3PExpr(pParse, TK_DOT, temp2, temp3, 0);
864b7916a78Sdrh   A.pExpr = sqlite3PExpr(pParse, TK_DOT, temp1, temp4, 0);
865b7916a78Sdrh   spanSet(&A,&X,&Z);
866d24cc427Sdrh }
867b7916a78Sdrh term(A) ::= INTEGER|FLOAT|BLOB(X).  {spanExpr(&A, pParse, @X, &X);}
868b7916a78Sdrh term(A) ::= STRING(X).              {spanExpr(&A, pParse, @X, &X);}
869f59b12fbSdrh expr(A) ::= VARIABLE(X).     {
870f59b12fbSdrh   if( X.n>=2 && X.z[0]=='#' && sqlite3Isdigit(X.z[1]) ){
871b7916a78Sdrh     /* When doing a nested parse, one can include terms in an expression
872b7916a78Sdrh     ** that look like this:   #1 #2 ...  These terms refer to registers
873b7916a78Sdrh     ** in the virtual machine.  #N is the N-th register. */
874b7916a78Sdrh     if( pParse->nested==0 ){
875b7916a78Sdrh       sqlite3ErrorMsg(pParse, "near \"%T\": syntax error", &X);
876b7916a78Sdrh       A.pExpr = 0;
877b7916a78Sdrh     }else{
878b7916a78Sdrh       A.pExpr = sqlite3PExpr(pParse, TK_REGISTER, 0, 0, &X);
879b7916a78Sdrh       if( A.pExpr ) sqlite3GetInt32(&X.z[1], &A.pExpr->iTable);
880b7916a78Sdrh     }
881f59b12fbSdrh   }else{
882b7916a78Sdrh     spanExpr(&A, pParse, TK_VARIABLE, &X);
883b7916a78Sdrh     sqlite3ExprAssignVarNumber(pParse, A.pExpr);
884f59b12fbSdrh   }
885b7916a78Sdrh   spanSet(&A, &X, &X);
886895d7472Sdrh }
88739002505Sdanielk1977 expr(A) ::= expr(E) COLLATE ids(C). {
88880103fc6Sdan   A.pExpr = sqlite3ExprAddCollateToken(pParse, E.pExpr, &C, 1);
889b7916a78Sdrh   A.zStart = E.zStart;
890b7916a78Sdrh   A.zEnd = &C.z[C.n];
8918b4c40d8Sdrh }
892487e262fSdrh %ifndef SQLITE_OMIT_CAST
893487e262fSdrh expr(A) ::= CAST(X) LP expr(E) AS typetoken(T) RP(Y). {
894b7916a78Sdrh   A.pExpr = sqlite3PExpr(pParse, TK_CAST, E.pExpr, 0, &T);
895b7916a78Sdrh   spanSet(&A,&X,&Y);
896487e262fSdrh }
897154d4b24Sdrh %endif  SQLITE_OMIT_CAST
898f59b12fbSdrh expr(A) ::= id(X) LP distinct(D) exprlist(Y) RP(E). {
899994704d1Sdrh   if( Y && Y->nExpr>pParse->db->aLimit[SQLITE_LIMIT_FUNCTION_ARG] ){
900e5c941b8Sdrh     sqlite3ErrorMsg(pParse, "too many arguments on function %T", &X);
9014e05c83bSdrh   }
902b7916a78Sdrh   A.pExpr = sqlite3ExprFunction(pParse, Y, &X);
903b7916a78Sdrh   spanSet(&A,&X,&E);
9047cea7f95Sdrh   if( D==SF_Distinct && A.pExpr ){
905b7916a78Sdrh     A.pExpr->flags |= EP_Distinct;
906fd357974Sdrh   }
907e1b6a5b8Sdrh }
908f59b12fbSdrh expr(A) ::= id(X) LP STAR RP(E). {
909b7916a78Sdrh   A.pExpr = sqlite3ExprFunction(pParse, 0, &X);
910b7916a78Sdrh   spanSet(&A,&X,&E);
911e1b6a5b8Sdrh }
912b71090fdSdrh term(A) ::= CTIME_KW(OP). {
913b7916a78Sdrh   A.pExpr = sqlite3ExprFunction(pParse, 0, &OP);
914b7916a78Sdrh   spanSet(&A, &OP, &OP);
915b7916a78Sdrh }
916b7916a78Sdrh 
917b7916a78Sdrh %include {
918b7916a78Sdrh   /* This routine constructs a binary expression node out of two ExprSpan
919b7916a78Sdrh   ** objects and uses the result to populate a new ExprSpan object.
920b7916a78Sdrh   */
921b7916a78Sdrh   static void spanBinaryExpr(
922b7916a78Sdrh     ExprSpan *pOut,     /* Write the result here */
923b7916a78Sdrh     Parse *pParse,      /* The parsing context.  Errors accumulate here */
924b7916a78Sdrh     int op,             /* The binary operation */
925b7916a78Sdrh     ExprSpan *pLeft,    /* The left operand */
926b7916a78Sdrh     ExprSpan *pRight    /* The right operand */
927b7916a78Sdrh   ){
928b7916a78Sdrh     pOut->pExpr = sqlite3PExpr(pParse, op, pLeft->pExpr, pRight->pExpr, 0);
929b7916a78Sdrh     pOut->zStart = pLeft->zStart;
930b7916a78Sdrh     pOut->zEnd = pRight->zEnd;
931417ec638Sdrh   }
932b71090fdSdrh }
933b7916a78Sdrh 
934b7916a78Sdrh expr(A) ::= expr(X) AND(OP) expr(Y).    {spanBinaryExpr(&A,pParse,@OP,&X,&Y);}
935b7916a78Sdrh expr(A) ::= expr(X) OR(OP) expr(Y).     {spanBinaryExpr(&A,pParse,@OP,&X,&Y);}
93617435752Sdrh expr(A) ::= expr(X) LT|GT|GE|LE(OP) expr(Y).
937b7916a78Sdrh                                         {spanBinaryExpr(&A,pParse,@OP,&X,&Y);}
938b7916a78Sdrh expr(A) ::= expr(X) EQ|NE(OP) expr(Y).  {spanBinaryExpr(&A,pParse,@OP,&X,&Y);}
939fd405314Sdrh expr(A) ::= expr(X) BITAND|BITOR|LSHIFT|RSHIFT(OP) expr(Y).
940b7916a78Sdrh                                         {spanBinaryExpr(&A,pParse,@OP,&X,&Y);}
941b7916a78Sdrh expr(A) ::= expr(X) PLUS|MINUS(OP) expr(Y).
942b7916a78Sdrh                                         {spanBinaryExpr(&A,pParse,@OP,&X,&Y);}
94317435752Sdrh expr(A) ::= expr(X) STAR|SLASH|REM(OP) expr(Y).
944b7916a78Sdrh                                         {spanBinaryExpr(&A,pParse,@OP,&X,&Y);}
945b7916a78Sdrh expr(A) ::= expr(X) CONCAT(OP) expr(Y). {spanBinaryExpr(&A,pParse,@OP,&X,&Y);}
94674ad7fe9Sdrh %type likeop {struct LikeOp}
9478b471863Sdrh likeop(A) ::= LIKE_KW|MATCH(X).     {A.eOperator = X; A.bNot = 0;}
9488b471863Sdrh likeop(A) ::= NOT LIKE_KW|MATCH(X). {A.eOperator = X; A.bNot = 1;}
9491dca1458Sdrh expr(A) ::= expr(X) likeop(OP) expr(Y).  [LIKE_KW]  {
9508aa34ae0Sdrh   ExprList *pList;
951b7916a78Sdrh   pList = sqlite3ExprListAppend(pParse,0, Y.pExpr);
952b7916a78Sdrh   pList = sqlite3ExprListAppend(pParse,pList, X.pExpr);
953b7916a78Sdrh   A.pExpr = sqlite3ExprFunction(pParse, pList, &OP.eOperator);
954f9df4498Sdrh   if( OP.bNot ) A.pExpr = sqlite3PExpr(pParse, TK_NOT, A.pExpr, 0, 0);
955b7916a78Sdrh   A.zStart = X.zStart;
956b7916a78Sdrh   A.zEnd = Y.zEnd;
957b7916a78Sdrh   if( A.pExpr ) A.pExpr->flags |= EP_InfixFunc;
9580ac65892Sdrh }
9591dca1458Sdrh expr(A) ::= expr(X) likeop(OP) expr(Y) ESCAPE expr(E).  [LIKE_KW]  {
9601dca1458Sdrh   ExprList *pList;
9611dca1458Sdrh   pList = sqlite3ExprListAppend(pParse,0, Y.pExpr);
9621dca1458Sdrh   pList = sqlite3ExprListAppend(pParse,pList, X.pExpr);
9631dca1458Sdrh   pList = sqlite3ExprListAppend(pParse,pList, E.pExpr);
9641dca1458Sdrh   A.pExpr = sqlite3ExprFunction(pParse, pList, &OP.eOperator);
965f9df4498Sdrh   if( OP.bNot ) A.pExpr = sqlite3PExpr(pParse, TK_NOT, A.pExpr, 0, 0);
9661dca1458Sdrh   A.zStart = X.zStart;
9671dca1458Sdrh   A.zEnd = E.zEnd;
9681dca1458Sdrh   if( A.pExpr ) A.pExpr->flags |= EP_InfixFunc;
9691dca1458Sdrh }
9707c6303c0Sdanielk1977 
971b7916a78Sdrh %include {
972b7916a78Sdrh   /* Construct an expression node for a unary postfix operator
973b7916a78Sdrh   */
974b7916a78Sdrh   static void spanUnaryPostfix(
975b7916a78Sdrh     ExprSpan *pOut,        /* Write the new expression node here */
976b7916a78Sdrh     Parse *pParse,         /* Parsing context to record errors */
977b7916a78Sdrh     int op,                /* The operator */
978b7916a78Sdrh     ExprSpan *pOperand,    /* The operand */
979b7916a78Sdrh     Token *pPostOp         /* The operand token for setting the span */
980b7916a78Sdrh   ){
981b7916a78Sdrh     pOut->pExpr = sqlite3PExpr(pParse, op, pOperand->pExpr, 0, 0);
982b7916a78Sdrh     pOut->zStart = pOperand->zStart;
983b7916a78Sdrh     pOut->zEnd = &pPostOp->z[pPostOp->n];
984e1b6a5b8Sdrh   }
98533048c0bSdrh }
986b7916a78Sdrh 
987b7916a78Sdrh expr(A) ::= expr(X) ISNULL|NOTNULL(E).   {spanUnaryPostfix(&A,pParse,@E,&X,&E);}
988b7916a78Sdrh expr(A) ::= expr(X) NOT NULL(E). {spanUnaryPostfix(&A,pParse,TK_NOTNULL,&X,&E);}
9896a2fe093Sdrh 
9906a51741dSdrh %include {
9916a51741dSdrh   /* A routine to convert a binary TK_IS or TK_ISNOT expression into a
9926a51741dSdrh   ** unary TK_ISNULL or TK_NOTNULL expression. */
9936a51741dSdrh   static void binaryToUnaryIfNull(Parse *pParse, Expr *pY, Expr *pA, int op){
9946a51741dSdrh     sqlite3 *db = pParse->db;
9952a3d1d17Sdrh     if( pY && pA && pY->op==TK_NULL ){
9965e17e8b7Sshaneh       pA->op = (u8)op;
9976a51741dSdrh       sqlite3ExprDelete(db, pA->pRight);
9986a51741dSdrh       pA->pRight = 0;
9996a51741dSdrh     }
10006a51741dSdrh   }
10016a51741dSdrh }
10026a51741dSdrh 
10036a2fe093Sdrh //    expr1 IS expr2
10046a2fe093Sdrh //    expr1 IS NOT expr2
10056a2fe093Sdrh //
10066a2fe093Sdrh // If expr2 is NULL then code as TK_ISNULL or TK_NOTNULL.  If expr2
10076a2fe093Sdrh // is any other expression, code as TK_IS or TK_ISNOT.
10086a2fe093Sdrh //
10096a2fe093Sdrh expr(A) ::= expr(X) IS expr(Y).     {
10106a2fe093Sdrh   spanBinaryExpr(&A,pParse,TK_IS,&X,&Y);
10116a51741dSdrh   binaryToUnaryIfNull(pParse, Y.pExpr, A.pExpr, TK_ISNULL);
10126a2fe093Sdrh }
10136a2fe093Sdrh expr(A) ::= expr(X) IS NOT expr(Y). {
10146a2fe093Sdrh   spanBinaryExpr(&A,pParse,TK_ISNOT,&X,&Y);
10156a51741dSdrh   binaryToUnaryIfNull(pParse, Y.pExpr, A.pExpr, TK_NOTNULL);
10166a2fe093Sdrh }
1017b7916a78Sdrh 
1018b7916a78Sdrh %include {
1019b7916a78Sdrh   /* Construct an expression node for a unary prefix operator
1020b7916a78Sdrh   */
1021b7916a78Sdrh   static void spanUnaryPrefix(
1022b7916a78Sdrh     ExprSpan *pOut,        /* Write the new expression node here */
1023b7916a78Sdrh     Parse *pParse,         /* Parsing context to record errors */
1024b7916a78Sdrh     int op,                /* The operator */
1025b7916a78Sdrh     ExprSpan *pOperand,    /* The operand */
1026b7916a78Sdrh     Token *pPreOp         /* The operand token for setting the span */
1027b7916a78Sdrh   ){
1028b7916a78Sdrh     pOut->pExpr = sqlite3PExpr(pParse, op, pOperand->pExpr, 0, 0);
1029b7916a78Sdrh     pOut->zStart = pPreOp->z;
1030b7916a78Sdrh     pOut->zEnd = pOperand->zEnd;
103133048c0bSdrh   }
103281a20f21Sdrh }
1033b7916a78Sdrh 
1034b7916a78Sdrh 
1035b7916a78Sdrh 
1036b7916a78Sdrh expr(A) ::= NOT(B) expr(X).    {spanUnaryPrefix(&A,pParse,@B,&X,&B);}
1037b7916a78Sdrh expr(A) ::= BITNOT(B) expr(X). {spanUnaryPrefix(&A,pParse,@B,&X,&B);}
10387ba5bc5bSdrh expr(A) ::= MINUS(B) expr(X). [BITNOT]
1039b7916a78Sdrh                                {spanUnaryPrefix(&A,pParse,TK_UMINUS,&X,&B);}
10407ba5bc5bSdrh expr(A) ::= PLUS(B) expr(X). [BITNOT]
1041b7916a78Sdrh                                {spanUnaryPrefix(&A,pParse,TK_UPLUS,&X,&B);}
1042b7916a78Sdrh 
10432e3a1f16Sdrh %type between_op {int}
10442e3a1f16Sdrh between_op(A) ::= BETWEEN.     {A = 0;}
10452e3a1f16Sdrh between_op(A) ::= NOT BETWEEN. {A = 1;}
10462e3a1f16Sdrh expr(A) ::= expr(W) between_op(N) expr(X) AND expr(Y). [BETWEEN] {
1047b7916a78Sdrh   ExprList *pList = sqlite3ExprListAppend(pParse,0, X.pExpr);
1048b7916a78Sdrh   pList = sqlite3ExprListAppend(pParse,pList, Y.pExpr);
1049b7916a78Sdrh   A.pExpr = sqlite3PExpr(pParse, TK_BETWEEN, W.pExpr, 0, 0);
1050b7916a78Sdrh   if( A.pExpr ){
1051b7916a78Sdrh     A.pExpr->x.pList = pList;
105253f733c7Sdrh   }else{
1053633e6d57Sdrh     sqlite3ExprListDelete(pParse->db, pList);
105453f733c7Sdrh   }
1055b7916a78Sdrh   if( N ) A.pExpr = sqlite3PExpr(pParse, TK_NOT, A.pExpr, 0, 0);
1056b7916a78Sdrh   A.zStart = W.zStart;
1057b7916a78Sdrh   A.zEnd = Y.zEnd;
1058fef5208cSdrh }
10593e8c37e7Sdanielk1977 %ifndef SQLITE_OMIT_SUBQUERY
10602e3a1f16Sdrh   %type in_op {int}
10612e3a1f16Sdrh   in_op(A) ::= IN.      {A = 0;}
10622e3a1f16Sdrh   in_op(A) ::= NOT IN.  {A = 1;}
10632e3a1f16Sdrh   expr(A) ::= expr(X) in_op(N) LP exprlist(Y) RP(E). [IN] {
1064094430ebSdrh     if( Y==0 ){
1065473c1bf2Sdan       /* Expressions of the form
1066473c1bf2Sdan       **
1067473c1bf2Sdan       **      expr1 IN ()
1068473c1bf2Sdan       **      expr1 NOT IN ()
1069473c1bf2Sdan       **
1070473c1bf2Sdan       ** simplify to constants 0 (false) and 1 (true), respectively,
1071473c1bf2Sdan       ** regardless of the value of expr1.
1072473c1bf2Sdan       */
1073094430ebSdrh       A.pExpr = sqlite3PExpr(pParse, TK_INTEGER, 0, 0, &sqlite3IntTokens[N]);
1074094430ebSdrh       sqlite3ExprDelete(pParse->db, X.pExpr);
10752b59b3a4Sdrh     }else if( Y->nExpr==1 ){
10762b59b3a4Sdrh       /* Expressions of the form:
10772b59b3a4Sdrh       **
10782b59b3a4Sdrh       **      expr1 IN (?1)
10792b59b3a4Sdrh       **      expr1 NOT IN (?2)
10802b59b3a4Sdrh       **
1081fbb24d10Sdrh       ** with exactly one value on the RHS can be simplified to something
1082fbb24d10Sdrh       ** like this:
10832b59b3a4Sdrh       **
1084fbb24d10Sdrh       **      expr1 == ?1
1085fbb24d10Sdrh       **      expr1 <> ?2
1086fbb24d10Sdrh       **
1087fbb24d10Sdrh       ** But, the RHS of the == or <> is marked with the EP_Generic flag
1088fbb24d10Sdrh       ** so that it may not contribute to the computation of comparison
1089fbb24d10Sdrh       ** affinity or the collating sequence to use for comparison.  Otherwise,
1090fbb24d10Sdrh       ** the semantics would be subtly different from IN or NOT IN.
10912b59b3a4Sdrh       */
1092fbb24d10Sdrh       Expr *pRHS = Y->a[0].pExpr;
10932b59b3a4Sdrh       Y->a[0].pExpr = 0;
10942b59b3a4Sdrh       sqlite3ExprListDelete(pParse->db, Y);
10955b1420e0Sdrh       /* pRHS cannot be NULL because a malloc error would have been detected
10965b1420e0Sdrh       ** before now and control would have never reached this point */
10975b1420e0Sdrh       if( ALWAYS(pRHS) ){
1098fbb24d10Sdrh         pRHS->flags &= ~EP_Collate;
1099fbb24d10Sdrh         pRHS->flags |= EP_Generic;
1100fbb24d10Sdrh       }
11012b59b3a4Sdrh       A.pExpr = sqlite3PExpr(pParse, N ? TK_NE : TK_EQ, X.pExpr, pRHS, 0);
1102094430ebSdrh     }else{
1103b7916a78Sdrh       A.pExpr = sqlite3PExpr(pParse, TK_IN, X.pExpr, 0, 0);
1104b7916a78Sdrh       if( A.pExpr ){
1105b7916a78Sdrh         A.pExpr->x.pList = Y;
11062308ed38Sdrh         sqlite3ExprSetHeightAndFlags(pParse, A.pExpr);
1107d5d56523Sdanielk1977       }else{
1108633e6d57Sdrh         sqlite3ExprListDelete(pParse->db, Y);
1109d5d56523Sdanielk1977       }
1110b7916a78Sdrh       if( N ) A.pExpr = sqlite3PExpr(pParse, TK_NOT, A.pExpr, 0, 0);
1111094430ebSdrh     }
1112b7916a78Sdrh     A.zStart = X.zStart;
1113b7916a78Sdrh     A.zEnd = &E.z[E.n];
1114fef5208cSdrh   }
111551522cd3Sdrh   expr(A) ::= LP(B) select(X) RP(E). {
1116b7916a78Sdrh     A.pExpr = sqlite3PExpr(pParse, TK_SELECT, 0, 0, 0);
1117b7916a78Sdrh     if( A.pExpr ){
1118b7916a78Sdrh       A.pExpr->x.pSelect = X;
1119885a5b03Sdrh       ExprSetProperty(A.pExpr, EP_xIsSelect|EP_Subquery);
11202308ed38Sdrh       sqlite3ExprSetHeightAndFlags(pParse, A.pExpr);
112153f733c7Sdrh     }else{
1122633e6d57Sdrh       sqlite3SelectDelete(pParse->db, X);
112353f733c7Sdrh     }
1124b7916a78Sdrh     A.zStart = B.z;
1125b7916a78Sdrh     A.zEnd = &E.z[E.n];
112651522cd3Sdrh   }
11272e3a1f16Sdrh   expr(A) ::= expr(X) in_op(N) LP select(Y) RP(E).  [IN] {
1128b7916a78Sdrh     A.pExpr = sqlite3PExpr(pParse, TK_IN, X.pExpr, 0, 0);
1129b7916a78Sdrh     if( A.pExpr ){
1130b7916a78Sdrh       A.pExpr->x.pSelect = Y;
1131885a5b03Sdrh       ExprSetProperty(A.pExpr, EP_xIsSelect|EP_Subquery);
11322308ed38Sdrh       sqlite3ExprSetHeightAndFlags(pParse, A.pExpr);
113353f733c7Sdrh     }else{
1134633e6d57Sdrh       sqlite3SelectDelete(pParse->db, Y);
113553f733c7Sdrh     }
1136b7916a78Sdrh     if( N ) A.pExpr = sqlite3PExpr(pParse, TK_NOT, A.pExpr, 0, 0);
1137b7916a78Sdrh     A.zStart = X.zStart;
1138b7916a78Sdrh     A.zEnd = &E.z[E.n];
1139fef5208cSdrh   }
114074ad7fe9Sdrh   expr(A) ::= expr(X) in_op(N) nm(Y) dbnm(Z). [IN] {
114117435752Sdrh     SrcList *pSrc = sqlite3SrcListAppend(pParse->db, 0,&Y,&Z);
1142b7916a78Sdrh     A.pExpr = sqlite3PExpr(pParse, TK_IN, X.pExpr, 0, 0);
1143b7916a78Sdrh     if( A.pExpr ){
1144b7916a78Sdrh       A.pExpr->x.pSelect = sqlite3SelectNew(pParse, 0,pSrc,0,0,0,0,0,0,0);
1145885a5b03Sdrh       ExprSetProperty(A.pExpr, EP_xIsSelect|EP_Subquery);
11462308ed38Sdrh       sqlite3ExprSetHeightAndFlags(pParse, A.pExpr);
114753f733c7Sdrh     }else{
1148633e6d57Sdrh       sqlite3SrcListDelete(pParse->db, pSrc);
114953f733c7Sdrh     }
1150b7916a78Sdrh     if( N ) A.pExpr = sqlite3PExpr(pParse, TK_NOT, A.pExpr, 0, 0);
1151b7916a78Sdrh     A.zStart = X.zStart;
1152b7916a78Sdrh     A.zEnd = Z.z ? &Z.z[Z.n] : &Y.z[Y.n];
115323b2db23Sdrh   }
115451522cd3Sdrh   expr(A) ::= EXISTS(B) LP select(Y) RP(E). {
1155b7916a78Sdrh     Expr *p = A.pExpr = sqlite3PExpr(pParse, TK_EXISTS, 0, 0, 0);
115651522cd3Sdrh     if( p ){
11576ab3a2ecSdanielk1977       p->x.pSelect = Y;
1158885a5b03Sdrh       ExprSetProperty(p, EP_xIsSelect|EP_Subquery);
11592308ed38Sdrh       sqlite3ExprSetHeightAndFlags(pParse, p);
116053f733c7Sdrh     }else{
1161633e6d57Sdrh       sqlite3SelectDelete(pParse->db, Y);
116251522cd3Sdrh     }
1163b7916a78Sdrh     A.zStart = B.z;
1164b7916a78Sdrh     A.zEnd = &E.z[E.n];
116551522cd3Sdrh   }
1166154d4b24Sdrh %endif SQLITE_OMIT_SUBQUERY
1167fef5208cSdrh 
116817a7f8ddSdrh /* CASE expressions */
116917a7f8ddSdrh expr(A) ::= CASE(C) case_operand(X) case_exprlist(Y) case_else(Z) END(E). {
1170c5cd1249Sdrh   A.pExpr = sqlite3PExpr(pParse, TK_CASE, X, 0, 0);
1171b7916a78Sdrh   if( A.pExpr ){
1172c5cd1249Sdrh     A.pExpr->x.pList = Z ? sqlite3ExprListAppend(pParse,Y,Z) : Y;
11732308ed38Sdrh     sqlite3ExprSetHeightAndFlags(pParse, A.pExpr);
117453f733c7Sdrh   }else{
1175633e6d57Sdrh     sqlite3ExprListDelete(pParse->db, Y);
1176c5cd1249Sdrh     sqlite3ExprDelete(pParse->db, Z);
117753f733c7Sdrh   }
1178b7916a78Sdrh   A.zStart = C.z;
1179b7916a78Sdrh   A.zEnd = &E.z[E.n];
118017a7f8ddSdrh }
118117a7f8ddSdrh %type case_exprlist {ExprList*}
1182633e6d57Sdrh %destructor case_exprlist {sqlite3ExprListDelete(pParse->db, $$);}
118317a7f8ddSdrh case_exprlist(A) ::= case_exprlist(X) WHEN expr(Y) THEN expr(Z). {
1184b7916a78Sdrh   A = sqlite3ExprListAppend(pParse,X, Y.pExpr);
1185b7916a78Sdrh   A = sqlite3ExprListAppend(pParse,A, Z.pExpr);
118617a7f8ddSdrh }
118717a7f8ddSdrh case_exprlist(A) ::= WHEN expr(Y) THEN expr(Z). {
1188b7916a78Sdrh   A = sqlite3ExprListAppend(pParse,0, Y.pExpr);
1189b7916a78Sdrh   A = sqlite3ExprListAppend(pParse,A, Z.pExpr);
119017a7f8ddSdrh }
119117a7f8ddSdrh %type case_else {Expr*}
1192633e6d57Sdrh %destructor case_else {sqlite3ExprDelete(pParse->db, $$);}
1193b7916a78Sdrh case_else(A) ::=  ELSE expr(X).         {A = X.pExpr;}
119417a7f8ddSdrh case_else(A) ::=  .                     {A = 0;}
119517a7f8ddSdrh %type case_operand {Expr*}
1196633e6d57Sdrh %destructor case_operand {sqlite3ExprDelete(pParse->db, $$);}
1197b7916a78Sdrh case_operand(A) ::= expr(X).            {A = X.pExpr;}
119817a7f8ddSdrh case_operand(A) ::= .                   {A = 0;}
1199348784efSdrh 
1200348784efSdrh %type exprlist {ExprList*}
1201633e6d57Sdrh %destructor exprlist {sqlite3ExprListDelete(pParse->db, $$);}
12029245c243Sdrh %type nexprlist {ExprList*}
1203633e6d57Sdrh %destructor nexprlist {sqlite3ExprListDelete(pParse->db, $$);}
1204348784efSdrh 
12059245c243Sdrh exprlist(A) ::= nexprlist(X).                {A = X;}
12069245c243Sdrh exprlist(A) ::= .                            {A = 0;}
120717435752Sdrh nexprlist(A) ::= nexprlist(X) COMMA expr(Y).
1208b7916a78Sdrh     {A = sqlite3ExprListAppend(pParse,X,Y.pExpr);}
120917435752Sdrh nexprlist(A) ::= expr(Y).
1210b7916a78Sdrh     {A = sqlite3ExprListAppend(pParse,0,Y.pExpr);}
12119245c243Sdrh 
1212cce7d176Sdrh 
1213382c0247Sdrh ///////////////////////////// The CREATE INDEX command ///////////////////////
1214382c0247Sdrh //
1215d9da78a2Sdrh cmd ::= createkw(S) uniqueflag(U) INDEX ifnotexists(NE) nm(X) dbnm(D)
1216*108aa00aSdrh         ON nm(Y) LP sortlist(Z) RP where_opt(W). {
121717435752Sdrh   sqlite3CreateIndex(pParse, &X, &D,
121817435752Sdrh                      sqlite3SrcListAppend(pParse->db,0,&Y,0), Z, U,
12198a9789b6Sdrh                       &S, W, SQLITE_SO_ASC, NE);
12209cfcf5d4Sdrh }
1221717e6402Sdrh 
1222717e6402Sdrh %type uniqueflag {int}
12239cfcf5d4Sdrh uniqueflag(A) ::= UNIQUE.  {A = OE_Abort;}
12249cfcf5d4Sdrh uniqueflag(A) ::= .        {A = OE_None;}
1225348784efSdrh 
1226348784efSdrh 
1227*108aa00aSdrh // The eidlist non-terminal (Expression Id List) generates an ExprList
1228*108aa00aSdrh // from a list of identifiers.  The identifier names are in ExprList.a[].zName.
1229*108aa00aSdrh // This list is stored in an ExprList rather than an IdList so that it
1230*108aa00aSdrh // can be easily sent to sqlite3ColumnsExprList().
1231*108aa00aSdrh //
1232*108aa00aSdrh // eidlist is grouped with CREATE INDEX because it used to be the non-terminal
1233*108aa00aSdrh // used for the arguments to an index.  That is just an historical accident.
1234*108aa00aSdrh //
1235*108aa00aSdrh // IMPORTANT COMPATIBILITY NOTE:  Some prior versions of SQLite accepted
1236*108aa00aSdrh // COLLATE clauses and ASC or DESC keywords on ID lists in inappropriate
1237*108aa00aSdrh // places - places that might have been stored in the sqlite_master schema.
1238*108aa00aSdrh // Those extra features were ignored.  But because they might be in some
1239*108aa00aSdrh // (busted) old databases, we need to continue parsing them when loading
1240*108aa00aSdrh // historical schemas.
1241*108aa00aSdrh //
1242*108aa00aSdrh %type eidlist {ExprList*}
1243*108aa00aSdrh %destructor eidlist {sqlite3ExprListDelete(pParse->db, $$);}
1244*108aa00aSdrh %type eidlist_opt {ExprList*}
1245*108aa00aSdrh %destructor eidlist_opt {sqlite3ExprListDelete(pParse->db, $$);}
1246*108aa00aSdrh 
1247*108aa00aSdrh %include {
1248*108aa00aSdrh   /* Add a single new term to an ExprList that is used to store a
1249*108aa00aSdrh   ** list of identifiers.  Report an error if the ID list contains
1250*108aa00aSdrh   ** a COLLATE clause or an ASC or DESC keyword, except ignore the
1251*108aa00aSdrh   ** error while parsing a legacy schema.
1252*108aa00aSdrh   */
1253*108aa00aSdrh   static ExprList *parserAddExprIdListTerm(
1254*108aa00aSdrh     Parse *pParse,
1255*108aa00aSdrh     ExprList *pPrior,
1256*108aa00aSdrh     Token *pIdToken,
1257*108aa00aSdrh     int hasCollate,
1258*108aa00aSdrh     int sortOrder
1259*108aa00aSdrh   ){
1260*108aa00aSdrh     ExprList *p = sqlite3ExprListAppend(pParse, pPrior, 0);
1261*108aa00aSdrh     if( (hasCollate || sortOrder!=SQLITE_SO_UNDEFINED)
1262*108aa00aSdrh         && pParse->db->init.busy==0
1263*108aa00aSdrh     ){
1264*108aa00aSdrh       sqlite3ErrorMsg(pParse, "syntax error after column name \"%.*s\"",
1265*108aa00aSdrh                          pIdToken->n, pIdToken->z);
12660202b29eSdanielk1977     }
1267*108aa00aSdrh     sqlite3ExprListSetName(pParse, p, pIdToken, 1);
1268*108aa00aSdrh     return p;
1269*108aa00aSdrh   }
1270*108aa00aSdrh } // end %include
1271*108aa00aSdrh 
1272*108aa00aSdrh eidlist_opt(A) ::= .                         {A = 0;}
1273*108aa00aSdrh eidlist_opt(A) ::= LP eidlist(X) RP.         {A = X;}
1274*108aa00aSdrh eidlist(A) ::= eidlist(X) COMMA nm(Y) collate(C) sortorder(Z).  {
1275*108aa00aSdrh   A = parserAddExprIdListTerm(pParse, X, &Y, C, Z);
1276*108aa00aSdrh }
1277*108aa00aSdrh eidlist(A) ::= nm(Y) collate(C) sortorder(Z). {
1278*108aa00aSdrh   A = parserAddExprIdListTerm(pParse, 0, &Y, C, Z);
12790202b29eSdanielk1977 }
12800202b29eSdanielk1977 
1281*108aa00aSdrh %type collate {int}
1282*108aa00aSdrh collate(C) ::= .              {C = 0;}
1283*108aa00aSdrh collate(C) ::= COLLATE ids.   {C = 1;}
1284a34001c9Sdrh 
1285348784efSdrh 
12868aff1015Sdrh ///////////////////////////// The DROP INDEX command /////////////////////////
1287382c0247Sdrh //
12884d91a701Sdrh cmd ::= DROP INDEX ifexists(E) fullname(X).   {sqlite3DropIndex(pParse, X, E);}
1289982cef7eSdrh 
1290382c0247Sdrh ///////////////////////////// The VACUUM command /////////////////////////////
1291382c0247Sdrh //
1292154d4b24Sdrh %ifndef SQLITE_OMIT_VACUUM
1293fdbcdee5Sdrh %ifndef SQLITE_OMIT_ATTACH
129474161705Sdrh cmd ::= VACUUM.                {sqlite3Vacuum(pParse);}
129574161705Sdrh cmd ::= VACUUM nm.             {sqlite3Vacuum(pParse);}
1296fdbcdee5Sdrh %endif  SQLITE_OMIT_ATTACH
1297154d4b24Sdrh %endif  SQLITE_OMIT_VACUUM
1298f57b14a6Sdrh 
1299382c0247Sdrh ///////////////////////////// The PRAGMA command /////////////////////////////
1300382c0247Sdrh //
130113d7042aSdrh %ifndef SQLITE_OMIT_PRAGMA
130291cf71b0Sdanielk1977 cmd ::= PRAGMA nm(X) dbnm(Z).                {sqlite3Pragma(pParse,&X,&Z,0,0);}
1303ada2ee0dSdrh cmd ::= PRAGMA nm(X) dbnm(Z) EQ nmnum(Y).    {sqlite3Pragma(pParse,&X,&Z,&Y,0);}
1304ada2ee0dSdrh cmd ::= PRAGMA nm(X) dbnm(Z) LP nmnum(Y) RP. {sqlite3Pragma(pParse,&X,&Z,&Y,0);}
1305ada2ee0dSdrh cmd ::= PRAGMA nm(X) dbnm(Z) EQ minus_num(Y).
1306ada2ee0dSdrh                                              {sqlite3Pragma(pParse,&X,&Z,&Y,1);}
1307ada2ee0dSdrh cmd ::= PRAGMA nm(X) dbnm(Z) LP minus_num(Y) RP.
1308ada2ee0dSdrh                                              {sqlite3Pragma(pParse,&X,&Z,&Y,1);}
1309ada2ee0dSdrh 
1310a3eb4b44Sdrh nmnum(A) ::= plus_num(X).             {A = X;}
1311a3eb4b44Sdrh nmnum(A) ::= nm(X).                   {A = X;}
1312ada2ee0dSdrh nmnum(A) ::= ON(X).                   {A = X;}
1313ada2ee0dSdrh nmnum(A) ::= DELETE(X).               {A = X;}
13146da861beSdrh nmnum(A) ::= DEFAULT(X).              {A = X;}
1315154d4b24Sdrh %endif SQLITE_OMIT_PRAGMA
1316f59b12fbSdrh %token_class number INTEGER|FLOAT.
13178395b7b6Sdrh plus_num(A) ::= PLUS number(X).       {A = X;}
13188395b7b6Sdrh plus_num(A) ::= number(X).            {A = X;}
1319f57b14a6Sdrh minus_num(A) ::= MINUS number(X).     {A = X;}
1320c3f9bad2Sdanielk1977 //////////////////////////// The CREATE TRIGGER command /////////////////////
1321f0f258b1Sdrh 
1322b7f9164eSdrh %ifndef SQLITE_OMIT_TRIGGER
1323b7f9164eSdrh 
1324d9da78a2Sdrh cmd ::= createkw trigger_decl(A) BEGIN trigger_cmd_list(S) END(Z). {
13254b59ab5eSdrh   Token all;
13264b59ab5eSdrh   all.z = A.z;
1327b27b7f5dSdrh   all.n = (int)(Z.z - A.z) + Z.n;
13284adee20fSdanielk1977   sqlite3FinishTrigger(pParse, S, &all);
1329f0f258b1Sdrh }
1330f0f258b1Sdrh 
1331fdd48a76Sdrh trigger_decl(A) ::= temp(T) TRIGGER ifnotexists(NOERR) nm(B) dbnm(Z)
1332fdd48a76Sdrh                     trigger_time(C) trigger_event(D)
133360218d2aSdrh                     ON fullname(E) foreach_clause when_clause(G). {
133460218d2aSdrh   sqlite3BeginTrigger(pParse, &B, &Z, C, D.a, D.b, E, G, T, NOERR);
13353df6b257Sdanielk1977   A = (Z.n==0?B:Z);
1336c3f9bad2Sdanielk1977 }
1337c3f9bad2Sdanielk1977 
1338c3f9bad2Sdanielk1977 %type trigger_time {int}
1339c3f9bad2Sdanielk1977 trigger_time(A) ::= BEFORE.      { A = TK_BEFORE; }
1340c3f9bad2Sdanielk1977 trigger_time(A) ::= AFTER.       { A = TK_AFTER;  }
1341c3f9bad2Sdanielk1977 trigger_time(A) ::= INSTEAD OF.  { A = TK_INSTEAD;}
1342c3f9bad2Sdanielk1977 trigger_time(A) ::= .            { A = TK_BEFORE; }
1343c3f9bad2Sdanielk1977 
1344ad3cab52Sdrh %type trigger_event {struct TrigEvent}
1345633e6d57Sdrh %destructor trigger_event {sqlite3IdListDelete(pParse->db, $$.b);}
1346fd405314Sdrh trigger_event(A) ::= DELETE|INSERT(OP).       {A.a = @OP; A.b = 0;}
134774ad7fe9Sdrh trigger_event(A) ::= UPDATE(OP).              {A.a = @OP; A.b = 0;}
134881eba73eSdrh trigger_event(A) ::= UPDATE OF idlist(X). {A.a = TK_UPDATE; A.b = X;}
1349c3f9bad2Sdanielk1977 
135060218d2aSdrh foreach_clause ::= .
135160218d2aSdrh foreach_clause ::= FOR EACH ROW.
1352c3f9bad2Sdanielk1977 
1353c3f9bad2Sdanielk1977 %type when_clause {Expr*}
1354633e6d57Sdrh %destructor when_clause {sqlite3ExprDelete(pParse->db, $$);}
1355c3f9bad2Sdanielk1977 when_clause(A) ::= .             { A = 0; }
1356b7916a78Sdrh when_clause(A) ::= WHEN expr(X). { A = X.pExpr; }
1357c3f9bad2Sdanielk1977 
1358c3f9bad2Sdanielk1977 %type trigger_cmd_list {TriggerStep*}
1359633e6d57Sdrh %destructor trigger_cmd_list {sqlite3DeleteTriggerStep(pParse->db, $$);}
1360187e4c6aSdrh trigger_cmd_list(A) ::= trigger_cmd_list(Y) trigger_cmd(X) SEMI. {
136181238966Sdrh   assert( Y!=0 );
136281238966Sdrh   Y->pLast->pNext = X;
1363187e4c6aSdrh   Y->pLast = X;
1364187e4c6aSdrh   A = Y;
1365a69d9168Sdrh }
136681238966Sdrh trigger_cmd_list(A) ::= trigger_cmd(X) SEMI. {
136781238966Sdrh   assert( X!=0 );
136881238966Sdrh   X->pLast = X;
136981238966Sdrh   A = X;
137081238966Sdrh }
1371c3f9bad2Sdanielk1977 
1372b1819a0bSdrh // Disallow qualified table names on INSERT, UPDATE, and DELETE statements
1373b1819a0bSdrh // within a trigger.  The table to INSERT, UPDATE, or DELETE is always in
1374b1819a0bSdrh // the same database as the table that the trigger fires on.
1375b1819a0bSdrh //
1376b1819a0bSdrh %type trnm {Token}
1377b1819a0bSdrh trnm(A) ::= nm(X).   {A = X;}
1378b1819a0bSdrh trnm(A) ::= nm DOT nm(X). {
1379b1819a0bSdrh   A = X;
1380b1819a0bSdrh   sqlite3ErrorMsg(pParse,
1381b1819a0bSdrh         "qualified table names are not allowed on INSERT, UPDATE, and DELETE "
1382b1819a0bSdrh         "statements within triggers");
1383b1819a0bSdrh }
1384b1819a0bSdrh 
1385b1819a0bSdrh // Disallow the INDEX BY and NOT INDEXED clauses on UPDATE and DELETE
1386b1819a0bSdrh // statements within triggers.  We make a specific error message for this
1387b1819a0bSdrh // since it is an exception to the default grammar rules.
1388b1819a0bSdrh //
1389b1819a0bSdrh tridxby ::= .
1390b1819a0bSdrh tridxby ::= INDEXED BY nm. {
1391b1819a0bSdrh   sqlite3ErrorMsg(pParse,
1392b1819a0bSdrh         "the INDEXED BY clause is not allowed on UPDATE or DELETE statements "
1393b1819a0bSdrh         "within triggers");
1394b1819a0bSdrh }
1395b1819a0bSdrh tridxby ::= NOT INDEXED. {
1396b1819a0bSdrh   sqlite3ErrorMsg(pParse,
1397b1819a0bSdrh         "the NOT INDEXED clause is not allowed on UPDATE or DELETE statements "
1398b1819a0bSdrh         "within triggers");
1399b1819a0bSdrh }
1400b1819a0bSdrh 
1401b1819a0bSdrh 
1402b1819a0bSdrh 
1403c3f9bad2Sdanielk1977 %type trigger_cmd {TriggerStep*}
1404633e6d57Sdrh %destructor trigger_cmd {sqlite3DeleteTriggerStep(pParse->db, $$);}
1405c3f9bad2Sdanielk1977 // UPDATE
1406b1819a0bSdrh trigger_cmd(A) ::=
1407b1819a0bSdrh    UPDATE orconf(R) trnm(X) tridxby SET setlist(Y) where_opt(Z).
140817435752Sdrh    { A = sqlite3TriggerUpdateStep(pParse->db, &X, Y, Z, R); }
1409c3f9bad2Sdanielk1977 
1410c3f9bad2Sdanielk1977 // INSERT
14118981b904Sdrh trigger_cmd(A) ::= insert_cmd(R) INTO trnm(X) idlist_opt(F) select(S).
141275593d96Sdrh                {A = sqlite3TriggerInsertStep(pParse->db, &X, F, S, R);}
1413c3f9bad2Sdanielk1977 
1414c3f9bad2Sdanielk1977 // DELETE
1415b1819a0bSdrh trigger_cmd(A) ::= DELETE FROM trnm(X) tridxby where_opt(Y).
141617435752Sdrh                {A = sqlite3TriggerDeleteStep(pParse->db, &X, Y);}
1417c3f9bad2Sdanielk1977 
1418c3f9bad2Sdanielk1977 // SELECT
141917435752Sdrh trigger_cmd(A) ::= select(X).  {A = sqlite3TriggerSelectStep(pParse->db, X); }
1420c3f9bad2Sdanielk1977 
14216f34903eSdanielk1977 // The special RAISE expression that may occur in trigger programs
14224b59ab5eSdrh expr(A) ::= RAISE(X) LP IGNORE RP(Y).  {
1423b7916a78Sdrh   A.pExpr = sqlite3PExpr(pParse, TK_RAISE, 0, 0, 0);
1424b7916a78Sdrh   if( A.pExpr ){
1425b7916a78Sdrh     A.pExpr->affinity = OE_Ignore;
14264b59ab5eSdrh   }
1427b7916a78Sdrh   A.zStart = X.z;
1428b7916a78Sdrh   A.zEnd = &Y.z[Y.n];
14298aa34ae0Sdrh }
143074ad7fe9Sdrh expr(A) ::= RAISE(X) LP raisetype(T) COMMA nm(Z) RP(Y).  {
1431b7916a78Sdrh   A.pExpr = sqlite3PExpr(pParse, TK_RAISE, 0, 0, &Z);
1432b7916a78Sdrh   if( A.pExpr ) {
1433b7916a78Sdrh     A.pExpr->affinity = (char)T;
14344b59ab5eSdrh   }
1435b7916a78Sdrh   A.zStart = X.z;
1436b7916a78Sdrh   A.zEnd = &Y.z[Y.n];
14378aa34ae0Sdrh }
1438154d4b24Sdrh %endif  !SQLITE_OMIT_TRIGGER
1439b7f9164eSdrh 
144074ad7fe9Sdrh %type raisetype {int}
144174ad7fe9Sdrh raisetype(A) ::= ROLLBACK.  {A = OE_Rollback;}
144274ad7fe9Sdrh raisetype(A) ::= ABORT.     {A = OE_Abort;}
144374ad7fe9Sdrh raisetype(A) ::= FAIL.      {A = OE_Fail;}
144474ad7fe9Sdrh 
14456f34903eSdanielk1977 
1446c3f9bad2Sdanielk1977 ////////////////////////  DROP TRIGGER statement //////////////////////////////
1447b7f9164eSdrh %ifndef SQLITE_OMIT_TRIGGER
1448fdd48a76Sdrh cmd ::= DROP TRIGGER ifexists(NOERR) fullname(X). {
1449fdd48a76Sdrh   sqlite3DropTrigger(pParse,X,NOERR);
1450c3f9bad2Sdanielk1977 }
1451154d4b24Sdrh %endif  !SQLITE_OMIT_TRIGGER
1452113088ecSdrh 
1453113088ecSdrh //////////////////////// ATTACH DATABASE file AS name /////////////////////////
1454fdbcdee5Sdrh %ifndef SQLITE_OMIT_ATTACH
1455f744bb56Sdanielk1977 cmd ::= ATTACH database_kw_opt expr(F) AS expr(D) key_opt(K). {
1456b7916a78Sdrh   sqlite3Attach(pParse, F.pExpr, D.pExpr, K);
14571c2d8414Sdrh }
1458fdbcdee5Sdrh cmd ::= DETACH database_kw_opt expr(D). {
1459b7916a78Sdrh   sqlite3Detach(pParse, D.pExpr);
1460fdbcdee5Sdrh }
1461fdbcdee5Sdrh 
1462f744bb56Sdanielk1977 %type key_opt {Expr*}
1463633e6d57Sdrh %destructor key_opt {sqlite3ExprDelete(pParse->db, $$);}
1464f744bb56Sdanielk1977 key_opt(A) ::= .                     { A = 0; }
1465b7916a78Sdrh key_opt(A) ::= KEY expr(X).          { A = X.pExpr; }
1466113088ecSdrh 
1467113088ecSdrh database_kw_opt ::= DATABASE.
1468113088ecSdrh database_kw_opt ::= .
1469fdbcdee5Sdrh %endif SQLITE_OMIT_ATTACH
14704343fea2Sdrh 
14714343fea2Sdrh ////////////////////////// REINDEX collation //////////////////////////////////
14724343fea2Sdrh %ifndef SQLITE_OMIT_REINDEX
14734343fea2Sdrh cmd ::= REINDEX.                {sqlite3Reindex(pParse, 0, 0);}
14744343fea2Sdrh cmd ::= REINDEX nm(X) dbnm(Y).  {sqlite3Reindex(pParse, &X, &Y);}
1475154d4b24Sdrh %endif  SQLITE_OMIT_REINDEX
14769fd2a9a0Sdanielk1977 
14779f18e8a0Sdrh /////////////////////////////////// ANALYZE ///////////////////////////////////
14789f18e8a0Sdrh %ifndef SQLITE_OMIT_ANALYZE
14799f18e8a0Sdrh cmd ::= ANALYZE.                {sqlite3Analyze(pParse, 0, 0);}
14809f18e8a0Sdrh cmd ::= ANALYZE nm(X) dbnm(Y).  {sqlite3Analyze(pParse, &X, &Y);}
14819f18e8a0Sdrh %endif
14829f18e8a0Sdrh 
14839fd2a9a0Sdanielk1977 //////////////////////// ALTER TABLE table ... ////////////////////////////////
14849fd2a9a0Sdanielk1977 %ifndef SQLITE_OMIT_ALTERTABLE
14859fd2a9a0Sdanielk1977 cmd ::= ALTER TABLE fullname(X) RENAME TO nm(Z). {
14869fd2a9a0Sdanielk1977   sqlite3AlterRenameTable(pParse,X,&Z);
14879fd2a9a0Sdanielk1977 }
148819a8e7e8Sdanielk1977 cmd ::= ALTER TABLE add_column_fullname ADD kwcolumn_opt column(Y). {
148919a8e7e8Sdanielk1977   sqlite3AlterFinishAddColumn(pParse, &Y);
149019a8e7e8Sdanielk1977 }
149119a8e7e8Sdanielk1977 add_column_fullname ::= fullname(X). {
1492d9da78a2Sdrh   pParse->db->lookaside.bEnabled = 0;
149319a8e7e8Sdanielk1977   sqlite3AlterBeginAddColumn(pParse, X);
149419a8e7e8Sdanielk1977 }
149519a8e7e8Sdanielk1977 kwcolumn_opt ::= .
149619a8e7e8Sdanielk1977 kwcolumn_opt ::= COLUMNKW.
1497154d4b24Sdrh %endif  SQLITE_OMIT_ALTERTABLE
1498e09daa90Sdrh 
1499e09daa90Sdrh //////////////////////// CREATE VIRTUAL TABLE ... /////////////////////////////
1500e09daa90Sdrh %ifndef SQLITE_OMIT_VIRTUALTABLE
1501b9bb7c18Sdrh cmd ::= create_vtab.                       {sqlite3VtabFinishParse(pParse,0);}
1502b9bb7c18Sdrh cmd ::= create_vtab LP vtabarglist RP(X).  {sqlite3VtabFinishParse(pParse,&X);}
1503b421b894Sdrh create_vtab ::= createkw VIRTUAL TABLE ifnotexists(E)
1504b421b894Sdrh                 nm(X) dbnm(Y) USING nm(Z). {
1505b421b894Sdrh     sqlite3VtabBeginParse(pParse, &X, &Y, &Z, E);
1506b9bb7c18Sdrh }
1507e09daa90Sdrh vtabarglist ::= vtabarg.
1508e09daa90Sdrh vtabarglist ::= vtabarglist COMMA vtabarg.
1509b9bb7c18Sdrh vtabarg ::= .                       {sqlite3VtabArgInit(pParse);}
1510b9bb7c18Sdrh vtabarg ::= vtabarg vtabargtoken.
1511b9bb7c18Sdrh vtabargtoken ::= ANY(X).            {sqlite3VtabArgExtend(pParse,&X);}
1512b9bb7c18Sdrh vtabargtoken ::= lp anylist RP(X).  {sqlite3VtabArgExtend(pParse,&X);}
1513b9bb7c18Sdrh lp ::= LP(X).                       {sqlite3VtabArgExtend(pParse,&X);}
1514b9bb7c18Sdrh anylist ::= .
1515aaac8b4eSdrh anylist ::= anylist LP anylist RP.
1516aaac8b4eSdrh anylist ::= anylist ANY.
1517154d4b24Sdrh %endif  SQLITE_OMIT_VIRTUALTABLE
15188b471863Sdrh 
15198b471863Sdrh 
15208b471863Sdrh //////////////////////// COMMON TABLE EXPRESSIONS ////////////////////////////
15217d562dbeSdan %type with {With*}
15227d562dbeSdan %type wqlist {With*}
15237d562dbeSdan %destructor with {sqlite3WithDelete(pParse->db, $$);}
15244e9119d9Sdan %destructor wqlist {sqlite3WithDelete(pParse->db, $$);}
15257d562dbeSdan 
15267d562dbeSdan with(A) ::= . {A = 0;}
15278b471863Sdrh %ifndef SQLITE_OMIT_CTE
15287d562dbeSdan with(A) ::= WITH wqlist(W).              { A = W; }
15297d562dbeSdan with(A) ::= WITH RECURSIVE wqlist(W).    { A = W; }
15307d562dbeSdan 
1531*108aa00aSdrh wqlist(A) ::= nm(X) eidlist_opt(Y) AS LP select(Z) RP. {
15327d562dbeSdan   A = sqlite3WithAdd(pParse, 0, &X, Y, Z);
15337d562dbeSdan }
1534*108aa00aSdrh wqlist(A) ::= wqlist(W) COMMA nm(X) eidlist_opt(Y) AS LP select(Z) RP. {
15357d562dbeSdan   A = sqlite3WithAdd(pParse, W, &X, Y, Z);
15368b471863Sdrh }
15378b471863Sdrh %endif  SQLITE_OMIT_CTE
1538