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