blob: 85a848f6df0cfd54d8ea55b2ead9ca22175c0035 [file] [log] [blame]
drh348784e2000-05-29 20:41:49 +00001/*
drhb19a2bc2001-09-16 00:13:26 +00002** 2001 September 15
drh348784e2000-05-29 20:41:49 +00003**
drhb19a2bc2001-09-16 00:13:26 +00004** The author disclaims copyright to this source code. In place of
5** a legal notice, here is a blessing:
drh348784e2000-05-29 20:41:49 +00006**
drhb19a2bc2001-09-16 00:13:26 +00007** 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.
drh348784e2000-05-29 20:41:49 +000010**
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**
drhb52076c2006-01-23 13:22:09 +000017** @(#) $Id: parse.y,v 1.193 2006/01/23 13:22:09 drh Exp $
drh348784e2000-05-29 20:41:49 +000018*/
drh487e2622005-06-25 18:42:14 +000019
20// All token codes are small integers with #defines that begin with "TK_"
drh348784e2000-05-29 20:41:49 +000021%token_prefix TK_
drh487e2622005-06-25 18:42:14 +000022
23// The type of the data attached to each token is Token. This is also the
24// default type for non-terminals.
25//
drh348784e2000-05-29 20:41:49 +000026%token_type {Token}
drhf57b14a2001-09-14 18:54:08 +000027%default_type {Token}
drh487e2622005-06-25 18:42:14 +000028
29// The generated parser function takes a 4th argument as follows:
drh348784e2000-05-29 20:41:49 +000030%extra_argument {Parse *pParse}
drh487e2622005-06-25 18:42:14 +000031
32// This code runs whenever there is a syntax error
33//
drh348784e2000-05-29 20:41:49 +000034%syntax_error {
drhb86ccfb2003-01-28 23:13:10 +000035 if( pParse->zErrMsg==0 ){
36 if( TOKEN.z[0] ){
danielk19774adee202004-05-08 08:23:19 +000037 sqlite3ErrorMsg(pParse, "near \"%T\": syntax error", &TOKEN);
drhb86ccfb2003-01-28 23:13:10 +000038 }else{
danielk19774adee202004-05-08 08:23:19 +000039 sqlite3ErrorMsg(pParse, "incomplete SQL statement");
drhb86ccfb2003-01-28 23:13:10 +000040 }
41 }
drh348784e2000-05-29 20:41:49 +000042}
drh487e2622005-06-25 18:42:14 +000043
44// The name of the generated procedure that implements the parser
45// is as follows:
danielk19774adee202004-05-08 08:23:19 +000046%name sqlite3Parser
drh487e2622005-06-25 18:42:14 +000047
48// The following text is included near the beginning of the C source
49// code file that implements the parser.
50//
drh348784e2000-05-29 20:41:49 +000051%include {
52#include "sqliteInt.h"
53#include "parse.h"
drh9bbca4c2001-11-06 04:00:18 +000054
55/*
drhad3cab52002-05-24 02:04:32 +000056** An instance of this structure holds information about the
57** LIMIT clause of a SELECT statement.
drh9bbca4c2001-11-06 04:00:18 +000058*/
drhad3cab52002-05-24 02:04:32 +000059struct LimitVal {
danielk1977a2dc3b12005-02-05 12:48:48 +000060 Expr *pLimit; /* The LIMIT expression. NULL if there is no limit */
61 Expr *pOffset; /* The OFFSET expression. NULL if there is none */
drhad3cab52002-05-24 02:04:32 +000062};
danielk1977c3f9bad2002-05-15 08:30:12 +000063
64/*
drh2e3a1f12004-10-06 14:39:28 +000065** An instance of this structure is used to store the LIKE,
66** GLOB, NOT LIKE, and NOT GLOB operators.
67*/
68struct LikeOp {
drhb52076c2006-01-23 13:22:09 +000069 Token eOperator; /* "like" or "glob" or "regexp" */
drhb71090f2005-05-23 17:26:51 +000070 int not; /* True if the NOT keyword is present */
drh2e3a1f12004-10-06 14:39:28 +000071};
72
73/*
drhad3cab52002-05-24 02:04:32 +000074** An instance of the following structure describes the event of a
75** TRIGGER. "a" is the event type, one of TK_UPDATE, TK_INSERT,
76** TK_DELETE, or TK_INSTEAD. If the event is of the form
77**
78** UPDATE ON (a,b,c)
79**
80** Then the "b" IdList records the list "a,b,c".
danielk1977c3f9bad2002-05-15 08:30:12 +000081*/
drhad3cab52002-05-24 02:04:32 +000082struct TrigEvent { int a; IdList * b; };
drhcaec2f12003-01-07 02:47:47 +000083
drh25d65432004-07-22 15:02:25 +000084/*
85** An instance of this structure holds the ATTACH key and the key type.
86*/
87struct AttachKey { int type; Token key; };
88
drhcaec2f12003-01-07 02:47:47 +000089} // end %include
drh348784e2000-05-29 20:41:49 +000090
drh826fb5a2004-02-14 23:59:57 +000091// Input is a single SQL command
drhc4a3c772001-04-04 11:48:57 +000092input ::= cmdlist.
drh094b2bb2002-03-13 18:54:07 +000093cmdlist ::= cmdlist ecmd.
drh826fb5a2004-02-14 23:59:57 +000094cmdlist ::= ecmd.
drh80242052004-06-09 00:48:12 +000095cmdx ::= cmd. { sqlite3FinishCoding(pParse); }
drhb7f91642004-10-31 02:22:47 +000096ecmd ::= SEMI.
97ecmd ::= explain cmdx SEMI.
danielk19774adee202004-05-08 08:23:19 +000098explain ::= . { sqlite3BeginParse(pParse, 0); }
drhb7f91642004-10-31 02:22:47 +000099%ifndef SQLITE_OMIT_EXPLAIN
drhecc92422005-09-10 16:46:12 +0000100explain ::= EXPLAIN. { sqlite3BeginParse(pParse, 1); }
101explain ::= EXPLAIN QUERY PLAN. { sqlite3BeginParse(pParse, 2); }
drhb7f91642004-10-31 02:22:47 +0000102%endif
drh348784e2000-05-29 20:41:49 +0000103
drh382c0242001-10-06 16:33:02 +0000104///////////////////// Begin and end transactions. ////////////////////////////
drhc4a3c772001-04-04 11:48:57 +0000105//
drhfa86c412002-02-02 15:01:15 +0000106
drh684917c2004-10-05 02:41:42 +0000107cmd ::= BEGIN transtype(Y) trans_opt. {sqlite3BeginTransaction(pParse, Y);}
drhc4a3c772001-04-04 11:48:57 +0000108trans_opt ::= .
109trans_opt ::= TRANSACTION.
drh5ad1a6c2002-07-01 12:27:09 +0000110trans_opt ::= TRANSACTION nm.
drh684917c2004-10-05 02:41:42 +0000111%type transtype {int}
112transtype(A) ::= . {A = TK_DEFERRED;}
113transtype(A) ::= DEFERRED(X). {A = @X;}
114transtype(A) ::= IMMEDIATE(X). {A = @X;}
115transtype(A) ::= EXCLUSIVE(X). {A = @X;}
danielk19774adee202004-05-08 08:23:19 +0000116cmd ::= COMMIT trans_opt. {sqlite3CommitTransaction(pParse);}
117cmd ::= END trans_opt. {sqlite3CommitTransaction(pParse);}
118cmd ::= ROLLBACK trans_opt. {sqlite3RollbackTransaction(pParse);}
drhc4a3c772001-04-04 11:48:57 +0000119
drh382c0242001-10-06 16:33:02 +0000120///////////////////// The CREATE TABLE statement ////////////////////////////
drh348784e2000-05-29 20:41:49 +0000121//
122cmd ::= create_table create_table_args.
drhfaa59552005-12-29 23:33:54 +0000123create_table ::= CREATE(X) temp(T) TABLE ifnotexists(E) nm(Y) dbnm(Z). {
124 sqlite3StartTable(pParse,&X,&Y,&Z,T,0,E);
drh969fa7c2002-02-18 18:30:32 +0000125}
drhfaa59552005-12-29 23:33:54 +0000126%type ifnotexists {int}
127ifnotexists(A) ::= . {A = 0;}
128ifnotexists(A) ::= IF NOT EXISTS. {A = 1;}
drhf57b3392001-10-08 13:22:32 +0000129%type temp {int}
danielk197753c0f742005-03-29 03:10:59 +0000130%ifndef SQLITE_OMIT_TEMPDB
drhd24cc422003-03-27 12:51:24 +0000131temp(A) ::= TEMP. {A = 1;}
danielk197753c0f742005-03-29 03:10:59 +0000132%endif
drhd24cc422003-03-27 12:51:24 +0000133temp(A) ::= . {A = 0;}
danielk197719a8e7e2005-03-17 05:03:38 +0000134create_table_args ::= LP columnlist conslist_opt(X) RP(Y). {
135 sqlite3EndTable(pParse,&X,&Y,0);
drh969fa7c2002-02-18 18:30:32 +0000136}
137create_table_args ::= AS select(S). {
danielk197719a8e7e2005-03-17 05:03:38 +0000138 sqlite3EndTable(pParse,0,0,S);
danielk19774adee202004-05-08 08:23:19 +0000139 sqlite3SelectDelete(S);
drh969fa7c2002-02-18 18:30:32 +0000140}
drh348784e2000-05-29 20:41:49 +0000141columnlist ::= columnlist COMMA column.
142columnlist ::= column.
143
drh487e2622005-06-25 18:42:14 +0000144// A "column" is a complete description of a single column in a
145// CREATE TABLE statement. This includes the column name, its
146// datatype, and other keywords such as PRIMARY KEY, UNIQUE, REFERENCES,
147// NOT NULL and so forth.
drh348784e2000-05-29 20:41:49 +0000148//
danielk197719a8e7e2005-03-17 05:03:38 +0000149column(A) ::= columnid(X) type carglist. {
150 A.z = X.z;
151 A.n = (pParse->sLastToken.z-X.z) + pParse->sLastToken.n;
152}
153columnid(A) ::= nm(X). {
154 sqlite3AddColumn(pParse,&X);
155 A = X;
156}
157
drhc4a3c772001-04-04 11:48:57 +0000158
159// An IDENTIFIER can be a generic identifier, or one of several
160// keywords. Any non-standard keyword can also be an identifier.
drhc4a3c772001-04-04 11:48:57 +0000161//
drh982cef72000-05-30 16:27:03 +0000162%type id {Token}
drhf18543c2002-03-30 15:26:50 +0000163id(A) ::= ID(X). {A = X;}
drh0bd1f4e2002-06-06 18:54:39 +0000164
drh34e33bb2002-06-06 19:04:16 +0000165// The following directive causes tokens ABORT, AFTER, ASC, etc. to
166// fallback to ID if they will not parse as their original value.
167// This obviates the need for the "id" nonterminal.
168//
drh319e4e72003-09-30 01:54:13 +0000169%fallback ID
drh9f18e8a2005-07-08 12:13:04 +0000170 ABORT AFTER ANALYZE ASC ATTACH BEFORE BEGIN CASCADE CAST CONFLICT
drh684917c2004-10-05 02:41:42 +0000171 DATABASE DEFERRED DESC DETACH EACH END EXCLUSIVE EXPLAIN FAIL FOR
drhecc92422005-09-10 16:46:12 +0000172 IGNORE IMMEDIATE INITIALLY INSTEAD LIKE_KW MATCH PLAN QUERY KEY
drh5ad1a6c2002-07-01 12:27:09 +0000173 OF OFFSET PRAGMA RAISE REPLACE RESTRICT ROW STATEMENT
drhb7f91642004-10-31 02:22:47 +0000174 TEMP TRIGGER VACUUM VIEW
175%ifdef SQLITE_OMIT_COMPOUND_SELECT
176 EXCEPT INTERSECT UNION
177%endif
drha0733842005-12-29 01:11:36 +0000178 REINDEX RENAME CTIME_KW IF
drhb7f91642004-10-31 02:22:47 +0000179 .
drhc4a3c772001-04-04 11:48:57 +0000180
drh2d3917d2004-02-22 16:27:00 +0000181// Define operator precedence early so that this is the first occurance
182// of the operator tokens in the grammer. Keeping the operators together
183// causes them to be assigned integer values that are close together,
184// which keeps parser tables smaller.
185//
drhf2bc0132004-10-04 13:19:23 +0000186// The token values assigned to these symbols is determined by the order
187// in which lemon first sees them. It must be the case that ISNULL/NOTNULL,
188// NE/EQ, GT/LE, and GE/LT are separated by only a single value. See
189// the sqlite3ExprIfFalse() routine for additional information on this
190// constraint.
191//
drh2d3917d2004-02-22 16:27:00 +0000192%left OR.
193%left AND.
194%right NOT.
drhb71090f2005-05-23 17:26:51 +0000195%left IS LIKE_KW BETWEEN IN ISNULL NOTNULL NE EQ.
drh9a432672004-10-04 13:38:09 +0000196%left GT LE LT GE.
danielk19777c6303c2004-11-17 16:41:29 +0000197%right ESCAPE.
drh2d3917d2004-02-22 16:27:00 +0000198%left BITAND BITOR LSHIFT RSHIFT.
199%left PLUS MINUS.
200%left STAR SLASH REM.
201%left CONCAT.
202%right UMINUS UPLUS BITNOT.
203
drhc4a3c772001-04-04 11:48:57 +0000204// And "ids" is an identifer-or-string.
205//
206%type ids {Token}
drhfd405312005-11-06 04:06:59 +0000207ids(A) ::= ID|STRING(X). {A = X;}
drhc4a3c772001-04-04 11:48:57 +0000208
drh5ad1a6c2002-07-01 12:27:09 +0000209// The name of a column or table can be any of the following:
210//
211%type nm {Token}
212nm(A) ::= ID(X). {A = X;}
213nm(A) ::= STRING(X). {A = X;}
214nm(A) ::= JOIN_KW(X). {A = X;}
215
drh487e2622005-06-25 18:42:14 +0000216// A typetoken is really one or more tokens that form a type name such
217// as can be found after the column name in a CREATE TABLE statement.
218// Multiple tokens are concatenated to form the value of the typetoken.
219//
220%type typetoken {Token}
drh382c0242001-10-06 16:33:02 +0000221type ::= .
drh487e2622005-06-25 18:42:14 +0000222type ::= typetoken(X). {sqlite3AddColumnType(pParse,&X);}
223typetoken(A) ::= typename(X). {A = X;}
224typetoken(A) ::= typename(X) LP signed RP(Y). {
225 A.z = X.z;
226 A.n = &Y.z[Y.n] - X.z;
227}
228typetoken(A) ::= typename(X) LP signed COMMA signed RP(Y). {
229 A.z = X.z;
230 A.n = &Y.z[Y.n] - X.z;
231}
drh382c0242001-10-06 16:33:02 +0000232%type typename {Token}
drhe2ea40d2004-05-20 12:41:19 +0000233typename(A) ::= ids(X). {A = X;}
drh596bd232004-09-30 14:22:47 +0000234typename(A) ::= typename(X) ids(Y). {A.z=X.z; A.n=Y.n+(Y.z-X.z);}
drhef0cae52003-07-16 02:19:37 +0000235%type signed {int}
drh2646da72005-12-09 20:02:05 +0000236signed(A) ::= plus_num(X). { A = atoi((char*)X.z); }
237signed(A) ::= minus_num(X). { A = -atoi((char*)X.z); }
drh487e2622005-06-25 18:42:14 +0000238
239// "carglist" is a list of additional constraints that come after the
240// column name and column type in a CREATE TABLE statement.
241//
drh348784e2000-05-29 20:41:49 +0000242carglist ::= carglist carg.
243carglist ::= .
drh5ad1a6c2002-07-01 12:27:09 +0000244carg ::= CONSTRAINT nm ccons.
drh348784e2000-05-29 20:41:49 +0000245carg ::= ccons.
danielk19777977a172004-11-09 12:44:37 +0000246carg ::= DEFAULT term(X). {sqlite3AddDefaultValue(pParse,X);}
drheb55bd22005-06-30 17:04:21 +0000247carg ::= DEFAULT LP expr(X) RP. {sqlite3AddDefaultValue(pParse,X);}
danielk19777977a172004-11-09 12:44:37 +0000248carg ::= DEFAULT PLUS term(X). {sqlite3AddDefaultValue(pParse,X);}
249carg ::= DEFAULT MINUS term(X). {
250 Expr *p = sqlite3Expr(TK_UMINUS, X, 0, 0);
251 sqlite3AddDefaultValue(pParse,p);
252}
253carg ::= DEFAULT id(X). {
254 Expr *p = sqlite3Expr(TK_STRING, 0, 0, &X);
255 sqlite3AddDefaultValue(pParse,p);
256}
drh348784e2000-05-29 20:41:49 +0000257
drh382c0242001-10-06 16:33:02 +0000258// In addition to the type name, we also care about the primary key and
259// UNIQUE constraints.
drh348784e2000-05-29 20:41:49 +0000260//
drh0d316a42002-08-11 20:10:47 +0000261ccons ::= NULL onconf.
danielk19774adee202004-05-08 08:23:19 +0000262ccons ::= NOT NULL onconf(R). {sqlite3AddNotNull(pParse, R);}
drhfdd6e852005-12-16 01:06:16 +0000263ccons ::= PRIMARY KEY sortorder(Z) onconf(R) autoinc(I).
264 {sqlite3AddPrimaryKey(pParse,0,R,I,Z);}
drh4d91a702006-01-04 15:54:36 +0000265ccons ::= UNIQUE onconf(R). {sqlite3CreateIndex(pParse,0,0,0,0,R,0,0,0,0);}
drh06f65412005-11-03 02:03:13 +0000266ccons ::= CHECK LP expr(X) RP. {sqlite3AddCheckConstraint(pParse,X);}
drhc2eef3b2002-08-31 18:53:06 +0000267ccons ::= REFERENCES nm(T) idxlist_opt(TA) refargs(R).
danielk19774adee202004-05-08 08:23:19 +0000268 {sqlite3CreateForeignKey(pParse,0,&T,TA,R);}
269ccons ::= defer_subclause(D). {sqlite3DeferForeignKey(pParse,D);}
drh2646da72005-12-09 20:02:05 +0000270ccons ::= COLLATE id(C). {sqlite3AddCollateType(pParse, (char*)C.z, C.n);}
drh04738cb2002-06-02 18:19:00 +0000271
drh205f48e2004-11-05 00:43:11 +0000272// The optional AUTOINCREMENT keyword
273%type autoinc {int}
drh2958a4e2004-11-12 03:56:15 +0000274autoinc(X) ::= . {X = 0;}
275autoinc(X) ::= AUTOINCR. {X = 1;}
drh205f48e2004-11-05 00:43:11 +0000276
drhc2eef3b2002-08-31 18:53:06 +0000277// The next group of rules parses the arguments to a REFERENCES clause
278// that determine if the referential integrity checking is deferred or
279// or immediate and which determine what action to take if a ref-integ
280// check fails.
drh04738cb2002-06-02 18:19:00 +0000281//
drhc2eef3b2002-08-31 18:53:06 +0000282%type refargs {int}
283refargs(A) ::= . { A = OE_Restrict * 0x010101; }
284refargs(A) ::= refargs(X) refarg(Y). { A = (X & Y.mask) | Y.value; }
285%type refarg {struct {int value; int mask;}}
286refarg(A) ::= MATCH nm. { A.value = 0; A.mask = 0x000000; }
287refarg(A) ::= ON DELETE refact(X). { A.value = X; A.mask = 0x0000ff; }
288refarg(A) ::= ON UPDATE refact(X). { A.value = X<<8; A.mask = 0x00ff00; }
289refarg(A) ::= ON INSERT refact(X). { A.value = X<<16; A.mask = 0xff0000; }
290%type refact {int}
291refact(A) ::= SET NULL. { A = OE_SetNull; }
292refact(A) ::= SET DEFAULT. { A = OE_SetDflt; }
293refact(A) ::= CASCADE. { A = OE_Cascade; }
294refact(A) ::= RESTRICT. { A = OE_Restrict; }
295%type defer_subclause {int}
296defer_subclause(A) ::= NOT DEFERRABLE init_deferred_pred_opt(X). {A = X;}
297defer_subclause(A) ::= DEFERRABLE init_deferred_pred_opt(X). {A = X;}
298%type init_deferred_pred_opt {int}
299init_deferred_pred_opt(A) ::= . {A = 0;}
300init_deferred_pred_opt(A) ::= INITIALLY DEFERRED. {A = 1;}
301init_deferred_pred_opt(A) ::= INITIALLY IMMEDIATE. {A = 0;}
drh348784e2000-05-29 20:41:49 +0000302
303// For the time being, the only constraint we care about is the primary
drh382c0242001-10-06 16:33:02 +0000304// key and UNIQUE. Both create indices.
drh348784e2000-05-29 20:41:49 +0000305//
danielk197719a8e7e2005-03-17 05:03:38 +0000306conslist_opt(A) ::= . {A.n = 0; A.z = 0;}
307conslist_opt(A) ::= COMMA(X) conslist. {A = X;}
drh348784e2000-05-29 20:41:49 +0000308conslist ::= conslist COMMA tcons.
drha2e1bb52001-01-04 14:20:18 +0000309conslist ::= conslist tcons.
drh348784e2000-05-29 20:41:49 +0000310conslist ::= tcons.
drh5ad1a6c2002-07-01 12:27:09 +0000311tcons ::= CONSTRAINT nm.
drhf3388142004-11-13 03:48:06 +0000312tcons ::= PRIMARY KEY LP idxlist(X) autoinc(I) RP onconf(R).
drhfdd6e852005-12-16 01:06:16 +0000313 {sqlite3AddPrimaryKey(pParse,X,R,I,0);}
drh9cfcf5d2002-01-29 18:41:24 +0000314tcons ::= UNIQUE LP idxlist(X) RP onconf(R).
drh4d91a702006-01-04 15:54:36 +0000315 {sqlite3CreateIndex(pParse,0,0,0,X,R,0,0,0,0);}
drh06f65412005-11-03 02:03:13 +0000316tcons ::= CHECK LP expr(E) RP onconf. {sqlite3AddCheckConstraint(pParse,E);}
drhc2eef3b2002-08-31 18:53:06 +0000317tcons ::= FOREIGN KEY LP idxlist(FA) RP
318 REFERENCES nm(T) idxlist_opt(TA) refargs(R) defer_subclause_opt(D). {
danielk19774adee202004-05-08 08:23:19 +0000319 sqlite3CreateForeignKey(pParse, FA, &T, TA, R);
320 sqlite3DeferForeignKey(pParse, D);
drhc2eef3b2002-08-31 18:53:06 +0000321}
322%type defer_subclause_opt {int}
323defer_subclause_opt(A) ::= . {A = 0;}
324defer_subclause_opt(A) ::= defer_subclause(X). {A = X;}
drh9cfcf5d2002-01-29 18:41:24 +0000325
326// The following is a non-standard extension that allows us to declare the
327// default behavior when there is a constraint conflict.
328//
329%type onconf {int}
drh1c928532002-01-31 15:54:21 +0000330%type orconf {int}
331%type resolvetype {int}
drh74ad7fe2004-10-07 03:06:28 +0000332onconf(A) ::= . {A = OE_Default;}
333onconf(A) ::= ON CONFLICT resolvetype(X). {A = X;}
334orconf(A) ::= . {A = OE_Default;}
335orconf(A) ::= OR resolvetype(X). {A = X;}
336resolvetype(A) ::= raisetype(X). {A = X;}
337resolvetype(A) ::= IGNORE. {A = OE_Ignore;}
338resolvetype(A) ::= REPLACE. {A = OE_Replace;}
drh348784e2000-05-29 20:41:49 +0000339
drh382c0242001-10-06 16:33:02 +0000340////////////////////////// The DROP TABLE /////////////////////////////////////
drh348784e2000-05-29 20:41:49 +0000341//
drha0733842005-12-29 01:11:36 +0000342cmd ::= DROP TABLE ifexists(E) fullname(X). {
343 sqlite3DropTable(pParse, X, 0, E);
danielk1977a8858102004-05-28 12:11:21 +0000344}
drha0733842005-12-29 01:11:36 +0000345%type ifexists {int}
346ifexists(A) ::= IF EXISTS. {A = 1;}
347ifexists(A) ::= . {A = 0;}
drh348784e2000-05-29 20:41:49 +0000348
drha76b5df2002-02-23 02:32:10 +0000349///////////////////// The CREATE VIEW statement /////////////////////////////
350//
drhb7f91642004-10-31 02:22:47 +0000351%ifndef SQLITE_OMIT_VIEW
danielk197748dec7e2004-05-28 12:33:30 +0000352cmd ::= CREATE(X) temp(T) VIEW nm(Y) dbnm(Z) AS select(S). {
353 sqlite3CreateView(pParse, &X, &Y, &Z, S, T);
drha76b5df2002-02-23 02:32:10 +0000354}
drha0733842005-12-29 01:11:36 +0000355cmd ::= DROP VIEW ifexists(E) fullname(X). {
356 sqlite3DropTable(pParse, X, 1, E);
drha76b5df2002-02-23 02:32:10 +0000357}
drhb7f91642004-10-31 02:22:47 +0000358%endif // SQLITE_OMIT_VIEW
drha76b5df2002-02-23 02:32:10 +0000359
drh382c0242001-10-06 16:33:02 +0000360//////////////////////// The SELECT statement /////////////////////////////////
drh348784e2000-05-29 20:41:49 +0000361//
drh9bb61fe2000-06-05 16:01:39 +0000362cmd ::= select(X). {
danielk1977b3bce662005-01-29 08:32:43 +0000363 sqlite3Select(pParse, X, SRT_Callback, 0, 0, 0, 0, 0);
danielk19774adee202004-05-08 08:23:19 +0000364 sqlite3SelectDelete(X);
drh9bb61fe2000-06-05 16:01:39 +0000365}
drhefb72512000-05-31 20:00:52 +0000366
drh9bb61fe2000-06-05 16:01:39 +0000367%type select {Select*}
danielk19774adee202004-05-08 08:23:19 +0000368%destructor select {sqlite3SelectDelete($$);}
drh82c3d632000-06-06 21:56:07 +0000369%type oneselect {Select*}
danielk19774adee202004-05-08 08:23:19 +0000370%destructor oneselect {sqlite3SelectDelete($$);}
drh9bb61fe2000-06-05 16:01:39 +0000371
drh82c3d632000-06-06 21:56:07 +0000372select(A) ::= oneselect(X). {A = X;}
drhb7f91642004-10-31 02:22:47 +0000373%ifndef SQLITE_OMIT_COMPOUND_SELECT
drh0a36c572002-02-18 22:49:59 +0000374select(A) ::= select(X) multiselect_op(Y) oneselect(Z). {
drhdaffd0e2001-04-11 14:28:42 +0000375 if( Z ){
drh82c3d632000-06-06 21:56:07 +0000376 Z->op = Y;
377 Z->pPrior = X;
drhdaffd0e2001-04-11 14:28:42 +0000378 }
379 A = Z;
drh82c3d632000-06-06 21:56:07 +0000380}
drh0a36c572002-02-18 22:49:59 +0000381%type multiselect_op {int}
drhfd405312005-11-06 04:06:59 +0000382multiselect_op(A) ::= UNION(OP). {A = @OP;}
383multiselect_op(A) ::= UNION ALL. {A = TK_ALL;}
384multiselect_op(A) ::= EXCEPT|INTERSECT(OP). {A = @OP;}
drhb7f91642004-10-31 02:22:47 +0000385%endif // SQLITE_OMIT_COMPOUND_SELECT
drh82c3d632000-06-06 21:56:07 +0000386oneselect(A) ::= SELECT distinct(D) selcollist(W) from(X) where_opt(Y)
drh9bbca4c2001-11-06 04:00:18 +0000387 groupby_opt(P) having_opt(Q) orderby_opt(Z) limit_opt(L). {
danielk1977a2dc3b12005-02-05 12:48:48 +0000388 A = sqlite3SelectNew(W,X,Y,P,Q,Z,D,L.pLimit,L.pOffset);
drh9bb61fe2000-06-05 16:01:39 +0000389}
390
391// The "distinct" nonterminal is true (1) if the DISTINCT keyword is
392// present and false (0) if it is not.
393//
drhefb72512000-05-31 20:00:52 +0000394%type distinct {int}
drhefb72512000-05-31 20:00:52 +0000395distinct(A) ::= DISTINCT. {A = 1;}
drhfef52082000-06-06 01:50:43 +0000396distinct(A) ::= ALL. {A = 0;}
drhefb72512000-05-31 20:00:52 +0000397distinct(A) ::= . {A = 0;}
drh348784e2000-05-29 20:41:49 +0000398
drh9bb61fe2000-06-05 16:01:39 +0000399// selcollist is a list of expressions that are to become the return
drh7c917d12001-12-16 20:05:05 +0000400// values of the SELECT statement. The "*" in statements like
401// "SELECT * FROM ..." is encoded as a special expression with an
402// opcode of TK_ALL.
drh9bb61fe2000-06-05 16:01:39 +0000403//
drh348784e2000-05-29 20:41:49 +0000404%type selcollist {ExprList*}
danielk19774adee202004-05-08 08:23:19 +0000405%destructor selcollist {sqlite3ExprListDelete($$);}
drh348784e2000-05-29 20:41:49 +0000406%type sclp {ExprList*}
danielk19774adee202004-05-08 08:23:19 +0000407%destructor sclp {sqlite3ExprListDelete($$);}
drh348784e2000-05-29 20:41:49 +0000408sclp(A) ::= selcollist(X) COMMA. {A = X;}
409sclp(A) ::= . {A = 0;}
drh01f3f252002-05-24 16:14:15 +0000410selcollist(A) ::= sclp(P) expr(X) as(Y). {
danielk19774adee202004-05-08 08:23:19 +0000411 A = sqlite3ExprListAppend(P,X,Y.n?&Y:0);
drh01f3f252002-05-24 16:14:15 +0000412}
drh7c917d12001-12-16 20:05:05 +0000413selcollist(A) ::= sclp(P) STAR. {
danielk19774adee202004-05-08 08:23:19 +0000414 A = sqlite3ExprListAppend(P, sqlite3Expr(TK_ALL, 0, 0, 0), 0);
drh7c917d12001-12-16 20:05:05 +0000415}
drh5ad1a6c2002-07-01 12:27:09 +0000416selcollist(A) ::= sclp(P) nm(X) DOT STAR. {
danielk19774adee202004-05-08 08:23:19 +0000417 Expr *pRight = sqlite3Expr(TK_ALL, 0, 0, 0);
418 Expr *pLeft = sqlite3Expr(TK_ID, 0, 0, &X);
419 A = sqlite3ExprListAppend(P, sqlite3Expr(TK_DOT, pLeft, pRight, 0), 0);
drh54473222002-04-04 02:10:55 +0000420}
drh01f3f252002-05-24 16:14:15 +0000421
422// An option "AS <id>" phrase that can follow one of the expressions that
423// define the result set, or one of the tables in the FROM clause.
424//
425%type as {Token}
drh74ad7fe2004-10-07 03:06:28 +0000426as(X) ::= AS nm(Y). {X = Y;}
427as(X) ::= ids(Y). {X = Y;}
428as(X) ::= . {X.n = 0;}
drh9bb61fe2000-06-05 16:01:39 +0000429
drh348784e2000-05-29 20:41:49 +0000430
drhad3cab52002-05-24 02:04:32 +0000431%type seltablist {SrcList*}
danielk19774adee202004-05-08 08:23:19 +0000432%destructor seltablist {sqlite3SrcListDelete($$);}
drhad3cab52002-05-24 02:04:32 +0000433%type stl_prefix {SrcList*}
danielk19774adee202004-05-08 08:23:19 +0000434%destructor stl_prefix {sqlite3SrcListDelete($$);}
drhad3cab52002-05-24 02:04:32 +0000435%type from {SrcList*}
danielk19774adee202004-05-08 08:23:19 +0000436%destructor from {sqlite3SrcListDelete($$);}
drh348784e2000-05-29 20:41:49 +0000437
drh01f3f252002-05-24 16:14:15 +0000438// A complete FROM clause.
439//
drhbf3a4fa2002-04-06 13:57:42 +0000440from(A) ::= . {A = sqliteMalloc(sizeof(*A));}
drh348784e2000-05-29 20:41:49 +0000441from(A) ::= FROM seltablist(X). {A = X;}
drh01f3f252002-05-24 16:14:15 +0000442
443// "seltablist" is a "Select Table List" - the content of the FROM clause
444// in a SELECT statement. "stl_prefix" is a prefix of this list.
445//
446stl_prefix(A) ::= seltablist(X) joinop(Y). {
447 A = X;
448 if( A && A->nSrc>0 ) A->a[A->nSrc-1].jointype = Y;
449}
drh348784e2000-05-29 20:41:49 +0000450stl_prefix(A) ::= . {A = 0;}
drh113088e2003-03-20 01:16:58 +0000451seltablist(A) ::= stl_prefix(X) nm(Y) dbnm(D) as(Z) on_opt(N) using_opt(U). {
danielk19774adee202004-05-08 08:23:19 +0000452 A = sqlite3SrcListAppend(X,&Y,&D);
453 if( Z.n ) sqlite3SrcListAddAlias(A,&Z);
drh01f3f252002-05-24 16:14:15 +0000454 if( N ){
455 if( A && A->nSrc>1 ){ A->a[A->nSrc-2].pOn = N; }
danielk19774adee202004-05-08 08:23:19 +0000456 else { sqlite3ExprDelete(N); }
drh01f3f252002-05-24 16:14:15 +0000457 }
458 if( U ){
459 if( A && A->nSrc>1 ){ A->a[A->nSrc-2].pUsing = U; }
danielk19774adee202004-05-08 08:23:19 +0000460 else { sqlite3IdListDelete(U); }
drh01f3f252002-05-24 16:14:15 +0000461 }
drhc4a3c772001-04-04 11:48:57 +0000462}
drh51522cd2005-01-20 13:36:19 +0000463%ifndef SQLITE_OMIT_SUBQUERY
464 seltablist(A) ::= stl_prefix(X) LP seltablist_paren(S) RP
465 as(Z) on_opt(N) using_opt(U). {
466 A = sqlite3SrcListAppend(X,0,0);
467 A->a[A->nSrc-1].pSelect = S;
468 if( Z.n ) sqlite3SrcListAddAlias(A,&Z);
469 if( N ){
470 if( A && A->nSrc>1 ){ A->a[A->nSrc-2].pOn = N; }
471 else { sqlite3ExprDelete(N); }
472 }
473 if( U ){
474 if( A && A->nSrc>1 ){ A->a[A->nSrc-2].pUsing = U; }
475 else { sqlite3IdListDelete(U); }
476 }
drhd5feede2002-05-08 21:46:14 +0000477 }
drh51522cd2005-01-20 13:36:19 +0000478
479 // A seltablist_paren nonterminal represents anything in a FROM that
480 // is contained inside parentheses. This can be either a subquery or
481 // a grouping of table and subqueries.
482 //
483 %type seltablist_paren {Select*}
484 %destructor seltablist_paren {sqlite3SelectDelete($$);}
485 seltablist_paren(A) ::= select(S). {A = S;}
486 seltablist_paren(A) ::= seltablist(F). {
danielk1977a2dc3b12005-02-05 12:48:48 +0000487 A = sqlite3SelectNew(0,F,0,0,0,0,0,0,0);
drh01f3f252002-05-24 16:14:15 +0000488 }
drh51522cd2005-01-20 13:36:19 +0000489%endif // SQLITE_OMIT_SUBQUERY
drhb733d032004-01-24 20:18:12 +0000490
drh113088e2003-03-20 01:16:58 +0000491%type dbnm {Token}
492dbnm(A) ::= . {A.z=0; A.n=0;}
493dbnm(A) ::= DOT nm(X). {A = X;}
494
drh74ad7fe2004-10-07 03:06:28 +0000495%type fullname {SrcList*}
496%destructor fullname {sqlite3SrcListDelete($$);}
497fullname(A) ::= nm(X) dbnm(Y). {A = sqlite3SrcListAppend(0,&X,&Y);}
498
drh01f3f252002-05-24 16:14:15 +0000499%type joinop {int}
500%type joinop2 {int}
drhfd405312005-11-06 04:06:59 +0000501joinop(X) ::= COMMA|JOIN. { X = JT_INNER; }
danielk19774adee202004-05-08 08:23:19 +0000502joinop(X) ::= JOIN_KW(A) JOIN. { X = sqlite3JoinType(pParse,&A,0,0); }
503joinop(X) ::= JOIN_KW(A) nm(B) JOIN. { X = sqlite3JoinType(pParse,&A,&B,0); }
drh5ad1a6c2002-07-01 12:27:09 +0000504joinop(X) ::= JOIN_KW(A) nm(B) nm(C) JOIN.
danielk19774adee202004-05-08 08:23:19 +0000505 { X = sqlite3JoinType(pParse,&A,&B,&C); }
drh01f3f252002-05-24 16:14:15 +0000506
507%type on_opt {Expr*}
danielk19774adee202004-05-08 08:23:19 +0000508%destructor on_opt {sqlite3ExprDelete($$);}
drh01f3f252002-05-24 16:14:15 +0000509on_opt(N) ::= ON expr(E). {N = E;}
510on_opt(N) ::= . {N = 0;}
511
512%type using_opt {IdList*}
danielk19774adee202004-05-08 08:23:19 +0000513%destructor using_opt {sqlite3IdListDelete($$);}
danielk19770202b292004-06-09 09:55:16 +0000514using_opt(U) ::= USING LP inscollist(L) RP. {U = L;}
drh01f3f252002-05-24 16:14:15 +0000515using_opt(U) ::= . {U = 0;}
516
517
drh348784e2000-05-29 20:41:49 +0000518%type orderby_opt {ExprList*}
danielk19774adee202004-05-08 08:23:19 +0000519%destructor orderby_opt {sqlite3ExprListDelete($$);}
drh348784e2000-05-29 20:41:49 +0000520%type sortlist {ExprList*}
danielk19774adee202004-05-08 08:23:19 +0000521%destructor sortlist {sqlite3ExprListDelete($$);}
drh348784e2000-05-29 20:41:49 +0000522%type sortitem {Expr*}
danielk19774adee202004-05-08 08:23:19 +0000523%destructor sortitem {sqlite3ExprDelete($$);}
drh348784e2000-05-29 20:41:49 +0000524
525orderby_opt(A) ::= . {A = 0;}
526orderby_opt(A) ::= ORDER BY sortlist(X). {A = X;}
drh8e2ca022002-06-17 17:07:19 +0000527sortlist(A) ::= sortlist(X) COMMA sortitem(Y) collate(C) sortorder(Z). {
drh1186b0a2004-05-20 23:37:54 +0000528 A = sqlite3ExprListAppend(X,Y,C.n>0?&C:0);
drhd3d39e92004-05-20 22:16:29 +0000529 if( A ) A->a[A->nExpr-1].sortOrder = Z;
drh9bb61fe2000-06-05 16:01:39 +0000530}
drh38640e12002-07-05 21:42:36 +0000531sortlist(A) ::= sortitem(Y) collate(C) sortorder(Z). {
drh1186b0a2004-05-20 23:37:54 +0000532 A = sqlite3ExprListAppend(0,Y,C.n>0?&C:0);
danielk197796fb0dd2004-06-30 09:49:22 +0000533 if( A && A->a ) A->a[0].sortOrder = Z;
drh9bb61fe2000-06-05 16:01:39 +0000534}
drhda9d6c42000-05-31 18:20:14 +0000535sortitem(A) ::= expr(X). {A = X;}
drh348784e2000-05-29 20:41:49 +0000536
537%type sortorder {int}
drhd3d39e92004-05-20 22:16:29 +0000538%type collate {Token}
drh348784e2000-05-29 20:41:49 +0000539
drh8e2ca022002-06-17 17:07:19 +0000540sortorder(A) ::= ASC. {A = SQLITE_SO_ASC;}
541sortorder(A) ::= DESC. {A = SQLITE_SO_DESC;}
542sortorder(A) ::= . {A = SQLITE_SO_ASC;}
drhd3d39e92004-05-20 22:16:29 +0000543collate(C) ::= . {C.z = 0; C.n = 0;}
544collate(C) ::= COLLATE id(X). {C = X;}
drh348784e2000-05-29 20:41:49 +0000545
drh22827922000-06-06 17:27:05 +0000546%type groupby_opt {ExprList*}
danielk19774adee202004-05-08 08:23:19 +0000547%destructor groupby_opt {sqlite3ExprListDelete($$);}
drh6206d502000-06-19 19:09:08 +0000548groupby_opt(A) ::= . {A = 0;}
drh22827922000-06-06 17:27:05 +0000549groupby_opt(A) ::= GROUP BY exprlist(X). {A = X;}
550
551%type having_opt {Expr*}
danielk19774adee202004-05-08 08:23:19 +0000552%destructor having_opt {sqlite3ExprDelete($$);}
drh6206d502000-06-19 19:09:08 +0000553having_opt(A) ::= . {A = 0;}
drh22827922000-06-06 17:27:05 +0000554having_opt(A) ::= HAVING expr(X). {A = X;}
555
drhad3cab52002-05-24 02:04:32 +0000556%type limit_opt {struct LimitVal}
danielk1977a2dc3b12005-02-05 12:48:48 +0000557%destructor limit_opt {
558 sqlite3ExprDelete($$.pLimit);
559 sqlite3ExprDelete($$.pOffset);
560}
561limit_opt(A) ::= . {A.pLimit = 0; A.pOffset = 0;}
562limit_opt(A) ::= LIMIT expr(X). {A.pLimit = X; A.pOffset = 0;}
563limit_opt(A) ::= LIMIT expr(X) OFFSET expr(Y).
564 {A.pLimit = X; A.pOffset = Y;}
565limit_opt(A) ::= LIMIT expr(X) COMMA expr(Y).
566 {A.pOffset = X; A.pLimit = Y;}
drh9bbca4c2001-11-06 04:00:18 +0000567
drh382c0242001-10-06 16:33:02 +0000568/////////////////////////// The DELETE statement /////////////////////////////
569//
drh74ad7fe2004-10-07 03:06:28 +0000570cmd ::= DELETE FROM fullname(X) where_opt(Y). {sqlite3DeleteFrom(pParse,X,Y);}
drh348784e2000-05-29 20:41:49 +0000571
572%type where_opt {Expr*}
danielk19774adee202004-05-08 08:23:19 +0000573%destructor where_opt {sqlite3ExprDelete($$);}
drh348784e2000-05-29 20:41:49 +0000574
575where_opt(A) ::= . {A = 0;}
576where_opt(A) ::= WHERE expr(X). {A = X;}
577
drh382c0242001-10-06 16:33:02 +0000578////////////////////////// The UPDATE command ////////////////////////////////
579//
drh74ad7fe2004-10-07 03:06:28 +0000580cmd ::= UPDATE orconf(R) fullname(X) SET setlist(Y) where_opt(Z).
581 {sqlite3Update(pParse,X,Y,Z,R);}
drh348784e2000-05-29 20:41:49 +0000582
drhf8db1bc2005-04-22 02:38:37 +0000583%type setlist {ExprList*}
584%destructor setlist {sqlite3ExprListDelete($$);}
585
drh5ad1a6c2002-07-01 12:27:09 +0000586setlist(A) ::= setlist(Z) COMMA nm(X) EQ expr(Y).
danielk19774adee202004-05-08 08:23:19 +0000587 {A = sqlite3ExprListAppend(Z,Y,&X);}
588setlist(A) ::= nm(X) EQ expr(Y). {A = sqlite3ExprListAppend(0,Y,&X);}
drh348784e2000-05-29 20:41:49 +0000589
drh382c0242001-10-06 16:33:02 +0000590////////////////////////// The INSERT command /////////////////////////////////
591//
drh74ad7fe2004-10-07 03:06:28 +0000592cmd ::= insert_cmd(R) INTO fullname(X) inscollist_opt(F)
drh113088e2003-03-20 01:16:58 +0000593 VALUES LP itemlist(Y) RP.
drh74ad7fe2004-10-07 03:06:28 +0000594 {sqlite3Insert(pParse, X, Y, 0, F, R);}
595cmd ::= insert_cmd(R) INTO fullname(X) inscollist_opt(F) select(S).
596 {sqlite3Insert(pParse, X, 0, S, F, R);}
drh348784e2000-05-29 20:41:49 +0000597
drhfa86c412002-02-02 15:01:15 +0000598%type insert_cmd {int}
599insert_cmd(A) ::= INSERT orconf(R). {A = R;}
600insert_cmd(A) ::= REPLACE. {A = OE_Replace;}
601
drh348784e2000-05-29 20:41:49 +0000602
603%type itemlist {ExprList*}
danielk19774adee202004-05-08 08:23:19 +0000604%destructor itemlist {sqlite3ExprListDelete($$);}
drh348784e2000-05-29 20:41:49 +0000605
danielk19774adee202004-05-08 08:23:19 +0000606itemlist(A) ::= itemlist(X) COMMA expr(Y). {A = sqlite3ExprListAppend(X,Y,0);}
607itemlist(A) ::= expr(X). {A = sqlite3ExprListAppend(0,X,0);}
drh348784e2000-05-29 20:41:49 +0000608
drh967e8b72000-06-21 13:59:10 +0000609%type inscollist_opt {IdList*}
danielk19774adee202004-05-08 08:23:19 +0000610%destructor inscollist_opt {sqlite3IdListDelete($$);}
drh967e8b72000-06-21 13:59:10 +0000611%type inscollist {IdList*}
danielk19774adee202004-05-08 08:23:19 +0000612%destructor inscollist {sqlite3IdListDelete($$);}
drh348784e2000-05-29 20:41:49 +0000613
drhc4a3c772001-04-04 11:48:57 +0000614inscollist_opt(A) ::= . {A = 0;}
615inscollist_opt(A) ::= LP inscollist(X) RP. {A = X;}
danielk19774adee202004-05-08 08:23:19 +0000616inscollist(A) ::= inscollist(X) COMMA nm(Y). {A = sqlite3IdListAppend(X,&Y);}
617inscollist(A) ::= nm(Y). {A = sqlite3IdListAppend(0,&Y);}
drh348784e2000-05-29 20:41:49 +0000618
drh382c0242001-10-06 16:33:02 +0000619/////////////////////////// Expression Processing /////////////////////////////
620//
drh348784e2000-05-29 20:41:49 +0000621
622%type expr {Expr*}
danielk19774adee202004-05-08 08:23:19 +0000623%destructor expr {sqlite3ExprDelete($$);}
danielk19777977a172004-11-09 12:44:37 +0000624%type term {Expr*}
625%destructor term {sqlite3ExprDelete($$);}
drh348784e2000-05-29 20:41:49 +0000626
danielk19777977a172004-11-09 12:44:37 +0000627expr(A) ::= term(X). {A = X;}
danielk1977752e6792004-11-09 16:13:33 +0000628expr(A) ::= LP(B) expr(X) RP(E). {A = X; sqlite3ExprSpan(A,&B,&E); }
danielk19777977a172004-11-09 12:44:37 +0000629term(A) ::= NULL(X). {A = sqlite3Expr(@X, 0, 0, &X);}
danielk19774adee202004-05-08 08:23:19 +0000630expr(A) ::= ID(X). {A = sqlite3Expr(TK_ID, 0, 0, &X);}
631expr(A) ::= JOIN_KW(X). {A = sqlite3Expr(TK_ID, 0, 0, &X);}
drh5ad1a6c2002-07-01 12:27:09 +0000632expr(A) ::= nm(X) DOT nm(Y). {
danielk19774adee202004-05-08 08:23:19 +0000633 Expr *temp1 = sqlite3Expr(TK_ID, 0, 0, &X);
634 Expr *temp2 = sqlite3Expr(TK_ID, 0, 0, &Y);
635 A = sqlite3Expr(TK_DOT, temp1, temp2, 0);
drhe1b6a5b2000-07-29 13:06:59 +0000636}
drhd24cc422003-03-27 12:51:24 +0000637expr(A) ::= nm(X) DOT nm(Y) DOT nm(Z). {
danielk19774adee202004-05-08 08:23:19 +0000638 Expr *temp1 = sqlite3Expr(TK_ID, 0, 0, &X);
639 Expr *temp2 = sqlite3Expr(TK_ID, 0, 0, &Y);
640 Expr *temp3 = sqlite3Expr(TK_ID, 0, 0, &Z);
641 Expr *temp4 = sqlite3Expr(TK_DOT, temp2, temp3, 0);
642 A = sqlite3Expr(TK_DOT, temp1, temp4, 0);
drhd24cc422003-03-27 12:51:24 +0000643}
drhfd405312005-11-06 04:06:59 +0000644term(A) ::= INTEGER|FLOAT|BLOB(X). {A = sqlite3Expr(@X, 0, 0, &X);}
danielk19777977a172004-11-09 12:44:37 +0000645term(A) ::= STRING(X). {A = sqlite3Expr(@X, 0, 0, &X);}
drh4e0cff62004-11-05 05:10:28 +0000646expr(A) ::= REGISTER(X). {A = sqlite3RegisterExpr(pParse, &X);}
drh7c972de2003-09-06 22:18:07 +0000647expr(A) ::= VARIABLE(X). {
drh895d7472004-08-20 16:02:39 +0000648 Token *pToken = &X;
649 Expr *pExpr = A = sqlite3Expr(TK_VARIABLE, 0, 0, pToken);
drhfa6bc002004-09-07 16:19:52 +0000650 sqlite3ExprAssignVarNumber(pParse, pExpr);
drh7c972de2003-09-06 22:18:07 +0000651}
drh487e2622005-06-25 18:42:14 +0000652%ifndef SQLITE_OMIT_CAST
653expr(A) ::= CAST(X) LP expr(E) AS typetoken(T) RP(Y). {
654 A = sqlite3Expr(TK_CAST, E, 0, &T);
655 sqlite3ExprSpan(A,&X,&Y);
656}
657%endif // SQLITE_OMIT_CAST
drhfd357972005-09-09 01:33:19 +0000658expr(A) ::= ID(X) LP distinct(D) exprlist(Y) RP(E). {
danielk19774adee202004-05-08 08:23:19 +0000659 A = sqlite3ExprFunction(Y, &X);
660 sqlite3ExprSpan(A,&X,&E);
drhfd357972005-09-09 01:33:19 +0000661 if( D ){
drhfd357972005-09-09 01:33:19 +0000662 A->flags |= EP_Distinct;
663 }
drhe1b6a5b2000-07-29 13:06:59 +0000664}
665expr(A) ::= ID(X) LP STAR RP(E). {
danielk19774adee202004-05-08 08:23:19 +0000666 A = sqlite3ExprFunction(0, &X);
667 sqlite3ExprSpan(A,&X,&E);
drhe1b6a5b2000-07-29 13:06:59 +0000668}
drhb71090f2005-05-23 17:26:51 +0000669term(A) ::= CTIME_KW(OP). {
670 /* The CURRENT_TIME, CURRENT_DATE, and CURRENT_TIMESTAMP values are
671 ** treated as functions that return constants */
672 A = sqlite3ExprFunction(0,&OP);
673 if( A ) A->op = TK_CONST_FUNC;
674}
drhfd405312005-11-06 04:06:59 +0000675expr(A) ::= expr(X) AND(OP) expr(Y). {A = sqlite3Expr(@OP, X, Y, 0);}
676expr(A) ::= expr(X) OR(OP) expr(Y). {A = sqlite3Expr(@OP, X, Y, 0);}
677expr(A) ::= expr(X) LT|GT|GE|LE(OP) expr(Y). {A = sqlite3Expr(@OP, X, Y, 0);}
678expr(A) ::= expr(X) EQ|NE(OP) expr(Y). {A = sqlite3Expr(@OP, X, Y, 0);}
679expr(A) ::= expr(X) BITAND|BITOR|LSHIFT|RSHIFT(OP) expr(Y).
680 {A = sqlite3Expr(@OP, X, Y, 0);}
681expr(A) ::= expr(X) PLUS|MINUS(OP) expr(Y). {A = sqlite3Expr(@OP, X, Y, 0);}
682expr(A) ::= expr(X) STAR|SLASH|REM(OP) expr(Y). {A = sqlite3Expr(@OP, X, Y, 0);}
683expr(A) ::= expr(X) CONCAT(OP) expr(Y). {A = sqlite3Expr(@OP, X, Y, 0);}
drh74ad7fe2004-10-07 03:06:28 +0000684%type likeop {struct LikeOp}
drhb52076c2006-01-23 13:22:09 +0000685likeop(A) ::= LIKE_KW(X). {A.eOperator = X; A.not = 0;}
686likeop(A) ::= NOT LIKE_KW(X). {A.eOperator = X; A.not = 1;}
danielk19777c6303c2004-11-17 16:41:29 +0000687%type escape {Expr*}
danielk1977dba99bc2006-01-07 14:02:26 +0000688%destructor escape {sqlite3ExprDelete($$);}
danielk19777c6303c2004-11-17 16:41:29 +0000689escape(X) ::= ESCAPE expr(A). [ESCAPE] {X = A;}
690escape(X) ::= . [ESCAPE] {X = 0;}
drhb71090f2005-05-23 17:26:51 +0000691expr(A) ::= expr(X) likeop(OP) expr(Y) escape(E). [LIKE_KW] {
danielk19774adee202004-05-08 08:23:19 +0000692 ExprList *pList = sqlite3ExprListAppend(0, Y, 0);
693 pList = sqlite3ExprListAppend(pList, X, 0);
danielk19777c6303c2004-11-17 16:41:29 +0000694 if( E ){
695 pList = sqlite3ExprListAppend(pList, E, 0);
696 }
drhb52076c2006-01-23 13:22:09 +0000697 A = sqlite3ExprFunction(pList, &OP.eOperator);
drh2e3a1f12004-10-06 14:39:28 +0000698 if( OP.not ) A = sqlite3Expr(TK_NOT, A, 0, 0);
danielk19774adee202004-05-08 08:23:19 +0000699 sqlite3ExprSpan(A, &X->span, &Y->span);
drh0ac65892002-04-20 14:24:41 +0000700}
danielk19777c6303c2004-11-17 16:41:29 +0000701
drhfd405312005-11-06 04:06:59 +0000702expr(A) ::= expr(X) ISNULL|NOTNULL(E). {
703 A = sqlite3Expr(@E, X, 0, 0);
danielk19774adee202004-05-08 08:23:19 +0000704 sqlite3ExprSpan(A,&X->span,&E);
drhe1b6a5b2000-07-29 13:06:59 +0000705}
drh33048c02001-10-01 14:29:22 +0000706expr(A) ::= expr(X) IS NULL(E). {
danielk19774adee202004-05-08 08:23:19 +0000707 A = sqlite3Expr(TK_ISNULL, X, 0, 0);
708 sqlite3ExprSpan(A,&X->span,&E);
drh33048c02001-10-01 14:29:22 +0000709}
drh33048c02001-10-01 14:29:22 +0000710expr(A) ::= expr(X) NOT NULL(E). {
danielk19774adee202004-05-08 08:23:19 +0000711 A = sqlite3Expr(TK_NOTNULL, X, 0, 0);
712 sqlite3ExprSpan(A,&X->span,&E);
drh33048c02001-10-01 14:29:22 +0000713}
drh81a20f22001-10-12 17:30:04 +0000714expr(A) ::= expr(X) IS NOT NULL(E). {
danielk19774adee202004-05-08 08:23:19 +0000715 A = sqlite3Expr(TK_NOTNULL, X, 0, 0);
716 sqlite3ExprSpan(A,&X->span,&E);
drh81a20f22001-10-12 17:30:04 +0000717}
drhfd405312005-11-06 04:06:59 +0000718expr(A) ::= NOT|BITNOT(B) expr(X). {
drh7ac25c72004-08-19 15:12:26 +0000719 A = sqlite3Expr(@B, X, 0, 0);
danielk19774adee202004-05-08 08:23:19 +0000720 sqlite3ExprSpan(A,&B,&X->span);
drh81a20f22001-10-12 17:30:04 +0000721}
drhe1b6a5b2000-07-29 13:06:59 +0000722expr(A) ::= MINUS(B) expr(X). [UMINUS] {
danielk19774adee202004-05-08 08:23:19 +0000723 A = sqlite3Expr(TK_UMINUS, X, 0, 0);
724 sqlite3ExprSpan(A,&B,&X->span);
drhe1b6a5b2000-07-29 13:06:59 +0000725}
drh4b59ab52002-08-24 18:24:51 +0000726expr(A) ::= PLUS(B) expr(X). [UPLUS] {
danielk19774adee202004-05-08 08:23:19 +0000727 A = sqlite3Expr(TK_UPLUS, X, 0, 0);
728 sqlite3ExprSpan(A,&B,&X->span);
drhe1b6a5b2000-07-29 13:06:59 +0000729}
drh2e3a1f12004-10-06 14:39:28 +0000730%type between_op {int}
731between_op(A) ::= BETWEEN. {A = 0;}
732between_op(A) ::= NOT BETWEEN. {A = 1;}
733expr(A) ::= expr(W) between_op(N) expr(X) AND expr(Y). [BETWEEN] {
danielk19774adee202004-05-08 08:23:19 +0000734 ExprList *pList = sqlite3ExprListAppend(0, X, 0);
735 pList = sqlite3ExprListAppend(pList, Y, 0);
736 A = sqlite3Expr(TK_BETWEEN, W, 0, 0);
drh53f733c2005-09-16 02:38:09 +0000737 if( A ){
738 A->pList = pList;
739 }else{
740 sqlite3ExprListDelete(pList);
741 }
drh2e3a1f12004-10-06 14:39:28 +0000742 if( N ) A = sqlite3Expr(TK_NOT, A, 0, 0);
danielk19774adee202004-05-08 08:23:19 +0000743 sqlite3ExprSpan(A,&W->span,&Y->span);
drhfef52082000-06-06 01:50:43 +0000744}
drh51522cd2005-01-20 13:36:19 +0000745%ifndef SQLITE_OMIT_SUBQUERY
danielk19773e8c37e2005-01-21 03:12:14 +0000746 %type in_op {int}
747 in_op(A) ::= IN. {A = 0;}
748 in_op(A) ::= NOT IN. {A = 1;}
749 expr(A) ::= expr(X) in_op(N) LP exprlist(Y) RP(E). [IN] {
750 A = sqlite3Expr(TK_IN, X, 0, 0);
danielk1977d5d56522005-03-16 12:15:20 +0000751 if( A ){
752 A->pList = Y;
753 }else{
754 sqlite3ExprListDelete(Y);
755 }
danielk19773e8c37e2005-01-21 03:12:14 +0000756 if( N ) A = sqlite3Expr(TK_NOT, A, 0, 0);
757 sqlite3ExprSpan(A,&X->span,&E);
758 }
drh51522cd2005-01-20 13:36:19 +0000759 expr(A) ::= LP(B) select(X) RP(E). {
760 A = sqlite3Expr(TK_SELECT, 0, 0, 0);
drh53f733c2005-09-16 02:38:09 +0000761 if( A ){
762 A->pSelect = X;
763 }else{
764 sqlite3SelectDelete(X);
765 }
drh51522cd2005-01-20 13:36:19 +0000766 sqlite3ExprSpan(A,&B,&E);
767 }
768 expr(A) ::= expr(X) in_op(N) LP select(Y) RP(E). [IN] {
769 A = sqlite3Expr(TK_IN, X, 0, 0);
drh53f733c2005-09-16 02:38:09 +0000770 if( A ){
771 A->pSelect = Y;
772 }else{
773 sqlite3SelectDelete(Y);
774 }
drh51522cd2005-01-20 13:36:19 +0000775 if( N ) A = sqlite3Expr(TK_NOT, A, 0, 0);
776 sqlite3ExprSpan(A,&X->span,&E);
777 }
778 expr(A) ::= expr(X) in_op(N) nm(Y) dbnm(Z). [IN] {
779 SrcList *pSrc = sqlite3SrcListAppend(0,&Y,&Z);
780 A = sqlite3Expr(TK_IN, X, 0, 0);
drh53f733c2005-09-16 02:38:09 +0000781 if( A ){
782 A->pSelect = sqlite3SelectNew(0,pSrc,0,0,0,0,0,0,0);
783 }else{
784 sqlite3SrcListDelete(pSrc);
785 }
drh51522cd2005-01-20 13:36:19 +0000786 if( N ) A = sqlite3Expr(TK_NOT, A, 0, 0);
787 sqlite3ExprSpan(A,&X->span,Z.z?&Z:&Y);
788 }
789 expr(A) ::= EXISTS(B) LP select(Y) RP(E). {
790 Expr *p = A = sqlite3Expr(TK_EXISTS, 0, 0, 0);
791 if( p ){
792 p->pSelect = Y;
793 sqlite3ExprSpan(p,&B,&E);
drh53f733c2005-09-16 02:38:09 +0000794 }else{
795 sqlite3SelectDelete(Y);
drh51522cd2005-01-20 13:36:19 +0000796 }
797 }
798%endif // SQLITE_OMIT_SUBQUERY
drhfef52082000-06-06 01:50:43 +0000799
drh17a7f8d2002-03-24 13:13:27 +0000800/* CASE expressions */
801expr(A) ::= CASE(C) case_operand(X) case_exprlist(Y) case_else(Z) END(E). {
danielk19774adee202004-05-08 08:23:19 +0000802 A = sqlite3Expr(TK_CASE, X, Z, 0);
drh53f733c2005-09-16 02:38:09 +0000803 if( A ){
804 A->pList = Y;
805 }else{
806 sqlite3ExprListDelete(Y);
807 }
danielk19774adee202004-05-08 08:23:19 +0000808 sqlite3ExprSpan(A, &C, &E);
drh17a7f8d2002-03-24 13:13:27 +0000809}
810%type case_exprlist {ExprList*}
danielk19774adee202004-05-08 08:23:19 +0000811%destructor case_exprlist {sqlite3ExprListDelete($$);}
drh17a7f8d2002-03-24 13:13:27 +0000812case_exprlist(A) ::= case_exprlist(X) WHEN expr(Y) THEN expr(Z). {
danielk19774adee202004-05-08 08:23:19 +0000813 A = sqlite3ExprListAppend(X, Y, 0);
814 A = sqlite3ExprListAppend(A, Z, 0);
drh17a7f8d2002-03-24 13:13:27 +0000815}
816case_exprlist(A) ::= WHEN expr(Y) THEN expr(Z). {
danielk19774adee202004-05-08 08:23:19 +0000817 A = sqlite3ExprListAppend(0, Y, 0);
818 A = sqlite3ExprListAppend(A, Z, 0);
drh17a7f8d2002-03-24 13:13:27 +0000819}
820%type case_else {Expr*}
danielk1977dba99bc2006-01-07 14:02:26 +0000821%destructor case_else {sqlite3ExprDelete($$);}
drh17a7f8d2002-03-24 13:13:27 +0000822case_else(A) ::= ELSE expr(X). {A = X;}
823case_else(A) ::= . {A = 0;}
824%type case_operand {Expr*}
danielk1977dba99bc2006-01-07 14:02:26 +0000825%destructor case_operand {sqlite3ExprDelete($$);}
drh17a7f8d2002-03-24 13:13:27 +0000826case_operand(A) ::= expr(X). {A = X;}
827case_operand(A) ::= . {A = 0;}
drh348784e2000-05-29 20:41:49 +0000828
829%type exprlist {ExprList*}
danielk19774adee202004-05-08 08:23:19 +0000830%destructor exprlist {sqlite3ExprListDelete($$);}
drh348784e2000-05-29 20:41:49 +0000831%type expritem {Expr*}
danielk19774adee202004-05-08 08:23:19 +0000832%destructor expritem {sqlite3ExprDelete($$);}
drh348784e2000-05-29 20:41:49 +0000833
drh348784e2000-05-29 20:41:49 +0000834exprlist(A) ::= exprlist(X) COMMA expritem(Y).
drh74ad7fe2004-10-07 03:06:28 +0000835 {A = sqlite3ExprListAppend(X,Y,0);}
danielk19774adee202004-05-08 08:23:19 +0000836exprlist(A) ::= expritem(X). {A = sqlite3ExprListAppend(0,X,0);}
drh348784e2000-05-29 20:41:49 +0000837expritem(A) ::= expr(X). {A = X;}
838expritem(A) ::= . {A = 0;}
drhcce7d172000-05-31 15:34:51 +0000839
drh382c0242001-10-06 16:33:02 +0000840///////////////////////////// The CREATE INDEX command ///////////////////////
841//
drh4d91a702006-01-04 15:54:36 +0000842cmd ::= CREATE(S) uniqueflag(U) INDEX ifnotexists(NE) nm(X) dbnm(D)
danielk19772b6d46b2005-02-14 06:38:40 +0000843 ON nm(Y) LP idxlist(Z) RP(E) onconf(R). {
drh9cfcf5d2002-01-29 18:41:24 +0000844 if( U!=OE_None ) U = R;
845 if( U==OE_Default) U = OE_Abort;
drhfdd6e852005-12-16 01:06:16 +0000846 sqlite3CreateIndex(pParse, &X, &D, sqlite3SrcListAppend(0,&Y,0), Z, U,
drh4d91a702006-01-04 15:54:36 +0000847 &S, &E, SQLITE_SO_ASC, NE);
drh9cfcf5d2002-01-29 18:41:24 +0000848}
drh717e6402001-09-27 03:22:32 +0000849
850%type uniqueflag {int}
drh74ad7fe2004-10-07 03:06:28 +0000851uniqueflag(A) ::= UNIQUE. {A = OE_Abort;}
852uniqueflag(A) ::= . {A = OE_None;}
drh348784e2000-05-29 20:41:49 +0000853
danielk19770202b292004-06-09 09:55:16 +0000854%type idxlist {ExprList*}
855%destructor idxlist {sqlite3ExprListDelete($$);}
856%type idxlist_opt {ExprList*}
857%destructor idxlist_opt {sqlite3ExprListDelete($$);}
drh348784e2000-05-29 20:41:49 +0000858%type idxitem {Token}
859
drhc2eef3b2002-08-31 18:53:06 +0000860idxlist_opt(A) ::= . {A = 0;}
861idxlist_opt(A) ::= LP idxlist(X) RP. {A = X;}
drhfdd6e852005-12-16 01:06:16 +0000862idxlist(A) ::= idxlist(X) COMMA idxitem(Y) collate(C) sortorder(Z). {
danielk19770202b292004-06-09 09:55:16 +0000863 Expr *p = 0;
864 if( C.n>0 ){
865 p = sqlite3Expr(TK_COLUMN, 0, 0, 0);
drh2646da72005-12-09 20:02:05 +0000866 if( p ) p->pColl = sqlite3LocateCollSeq(pParse, (char*)C.z, C.n);
danielk19770202b292004-06-09 09:55:16 +0000867 }
868 A = sqlite3ExprListAppend(X, p, &Y);
drhfdd6e852005-12-16 01:06:16 +0000869 if( A ) A->a[A->nExpr-1].sortOrder = Z;
danielk19770202b292004-06-09 09:55:16 +0000870}
drhfdd6e852005-12-16 01:06:16 +0000871idxlist(A) ::= idxitem(Y) collate(C) sortorder(Z). {
danielk19770202b292004-06-09 09:55:16 +0000872 Expr *p = 0;
873 if( C.n>0 ){
874 p = sqlite3Expr(TK_COLUMN, 0, 0, 0);
drh2646da72005-12-09 20:02:05 +0000875 if( p ) p->pColl = sqlite3LocateCollSeq(pParse, (char*)C.z, C.n);
danielk19770202b292004-06-09 09:55:16 +0000876 }
877 A = sqlite3ExprListAppend(0, p, &Y);
drhfdd6e852005-12-16 01:06:16 +0000878 if( A ) A->a[A->nExpr-1].sortOrder = Z;
danielk19770202b292004-06-09 09:55:16 +0000879}
880idxitem(A) ::= nm(X). {A = X;}
881
drh348784e2000-05-29 20:41:49 +0000882
drh8aff1012001-12-22 14:49:24 +0000883///////////////////////////// The DROP INDEX command /////////////////////////
drh382c0242001-10-06 16:33:02 +0000884//
drh4d91a702006-01-04 15:54:36 +0000885cmd ::= DROP INDEX ifexists(E) fullname(X). {sqlite3DropIndex(pParse, X, E);}
drh982cef72000-05-30 16:27:03 +0000886
drh382c0242001-10-06 16:33:02 +0000887///////////////////////////// The VACUUM command /////////////////////////////
888//
danielk19774adee202004-05-08 08:23:19 +0000889cmd ::= VACUUM. {sqlite3Vacuum(pParse,0);}
drh74ad7fe2004-10-07 03:06:28 +0000890cmd ::= VACUUM nm. {sqlite3Vacuum(pParse,0);}
drhf57b14a2001-09-14 18:54:08 +0000891
drh382c0242001-10-06 16:33:02 +0000892///////////////////////////// The PRAGMA command /////////////////////////////
893//
drh13d70422004-11-13 15:59:14 +0000894%ifndef SQLITE_OMIT_PRAGMA
danielk197791cf71b2004-06-26 06:37:06 +0000895cmd ::= PRAGMA nm(X) dbnm(Z) EQ nm(Y). {sqlite3Pragma(pParse,&X,&Z,&Y,0);}
896cmd ::= PRAGMA nm(X) dbnm(Z) EQ ON(Y). {sqlite3Pragma(pParse,&X,&Z,&Y,0);}
897cmd ::= PRAGMA nm(X) dbnm(Z) EQ plus_num(Y). {sqlite3Pragma(pParse,&X,&Z,&Y,0);}
898cmd ::= PRAGMA nm(X) dbnm(Z) EQ minus_num(Y). {
899 sqlite3Pragma(pParse,&X,&Z,&Y,1);
900}
901cmd ::= PRAGMA nm(X) dbnm(Z) LP nm(Y) RP. {sqlite3Pragma(pParse,&X,&Z,&Y,0);}
drh74ad7fe2004-10-07 03:06:28 +0000902cmd ::= PRAGMA nm(X) dbnm(Z). {sqlite3Pragma(pParse,&X,&Z,0,0);}
drh13d70422004-11-13 15:59:14 +0000903%endif // SQLITE_OMIT_PRAGMA
drhf57b14a2001-09-14 18:54:08 +0000904plus_num(A) ::= plus_opt number(X). {A = X;}
905minus_num(A) ::= MINUS number(X). {A = X;}
drhfd405312005-11-06 04:06:59 +0000906number(A) ::= INTEGER|FLOAT(X). {A = X;}
drhf57b14a2001-09-14 18:54:08 +0000907plus_opt ::= PLUS.
908plus_opt ::= .
danielk1977c3f9bad2002-05-15 08:30:12 +0000909
910//////////////////////////// The CREATE TRIGGER command /////////////////////
drhf0f258b2003-04-21 18:48:45 +0000911
drhb7f91642004-10-31 02:22:47 +0000912%ifndef SQLITE_OMIT_TRIGGER
913
danielk19773df6b252004-05-29 10:23:19 +0000914cmd ::= CREATE trigger_decl(A) BEGIN trigger_cmd_list(S) END(Z). {
drh4b59ab52002-08-24 18:24:51 +0000915 Token all;
916 all.z = A.z;
917 all.n = (Z.z - A.z) + Z.n;
danielk19774adee202004-05-08 08:23:19 +0000918 sqlite3FinishTrigger(pParse, S, &all);
drhf0f258b2003-04-21 18:48:45 +0000919}
920
drh74ad7fe2004-10-07 03:06:28 +0000921trigger_decl(A) ::= temp(T) TRIGGER nm(B) dbnm(Z) trigger_time(C)
922 trigger_event(D)
923 ON fullname(E) foreach_clause(F) when_clause(G). {
924 sqlite3BeginTrigger(pParse, &B, &Z, C, D.a, D.b, E, F, G, T);
danielk19773df6b252004-05-29 10:23:19 +0000925 A = (Z.n==0?B:Z);
danielk1977c3f9bad2002-05-15 08:30:12 +0000926}
927
928%type trigger_time {int}
929trigger_time(A) ::= BEFORE. { A = TK_BEFORE; }
930trigger_time(A) ::= AFTER. { A = TK_AFTER; }
931trigger_time(A) ::= INSTEAD OF. { A = TK_INSTEAD;}
932trigger_time(A) ::= . { A = TK_BEFORE; }
933
drhad3cab52002-05-24 02:04:32 +0000934%type trigger_event {struct TrigEvent}
danielk19774adee202004-05-08 08:23:19 +0000935%destructor trigger_event {sqlite3IdListDelete($$.b);}
drhfd405312005-11-06 04:06:59 +0000936trigger_event(A) ::= DELETE|INSERT(OP). {A.a = @OP; A.b = 0;}
drh74ad7fe2004-10-07 03:06:28 +0000937trigger_event(A) ::= UPDATE(OP). {A.a = @OP; A.b = 0;}
938trigger_event(A) ::= UPDATE OF inscollist(X). {A.a = TK_UPDATE; A.b = X;}
danielk1977c3f9bad2002-05-15 08:30:12 +0000939
940%type foreach_clause {int}
941foreach_clause(A) ::= . { A = TK_ROW; }
942foreach_clause(A) ::= FOR EACH ROW. { A = TK_ROW; }
943foreach_clause(A) ::= FOR EACH STATEMENT. { A = TK_STATEMENT; }
944
drh0bb132b2004-07-20 14:06:51 +0000945%type when_clause {Expr*}
danielk1977dba99bc2006-01-07 14:02:26 +0000946%destructor when_clause {sqlite3ExprDelete($$);}
danielk1977c3f9bad2002-05-15 08:30:12 +0000947when_clause(A) ::= . { A = 0; }
948when_clause(A) ::= WHEN expr(X). { A = X; }
949
drh0bb132b2004-07-20 14:06:51 +0000950%type trigger_cmd_list {TriggerStep*}
danielk19774adee202004-05-08 08:23:19 +0000951%destructor trigger_cmd_list {sqlite3DeleteTriggerStep($$);}
danielk1977c3f9bad2002-05-15 08:30:12 +0000952trigger_cmd_list(A) ::= trigger_cmd(X) SEMI trigger_cmd_list(Y). {
drha69d9162003-04-17 22:57:53 +0000953 X->pNext = Y;
954 A = X;
955}
danielk1977c3f9bad2002-05-15 08:30:12 +0000956trigger_cmd_list(A) ::= . { A = 0; }
957
drh0bb132b2004-07-20 14:06:51 +0000958%type trigger_cmd {TriggerStep*}
danielk19774adee202004-05-08 08:23:19 +0000959%destructor trigger_cmd {sqlite3DeleteTriggerStep($$);}
danielk1977c3f9bad2002-05-15 08:30:12 +0000960// UPDATE
drh5ad1a6c2002-07-01 12:27:09 +0000961trigger_cmd(A) ::= UPDATE orconf(R) nm(X) SET setlist(Y) where_opt(Z).
danielk19774adee202004-05-08 08:23:19 +0000962 { A = sqlite3TriggerUpdateStep(&X, Y, Z, R); }
danielk1977c3f9bad2002-05-15 08:30:12 +0000963
964// INSERT
drh3054efe2004-02-12 17:28:13 +0000965trigger_cmd(A) ::= insert_cmd(R) INTO nm(X) inscollist_opt(F)
drh74ad7fe2004-10-07 03:06:28 +0000966 VALUES LP itemlist(Y) RP.
967 {A = sqlite3TriggerInsertStep(&X, F, Y, 0, R);}
danielk1977c3f9bad2002-05-15 08:30:12 +0000968
drh3054efe2004-02-12 17:28:13 +0000969trigger_cmd(A) ::= insert_cmd(R) INTO nm(X) inscollist_opt(F) select(S).
danielk19774adee202004-05-08 08:23:19 +0000970 {A = sqlite3TriggerInsertStep(&X, F, 0, S, R);}
danielk1977c3f9bad2002-05-15 08:30:12 +0000971
972// DELETE
drh5ad1a6c2002-07-01 12:27:09 +0000973trigger_cmd(A) ::= DELETE FROM nm(X) where_opt(Y).
danielk19774adee202004-05-08 08:23:19 +0000974 {A = sqlite3TriggerDeleteStep(&X, Y);}
danielk1977c3f9bad2002-05-15 08:30:12 +0000975
976// SELECT
danielk19774adee202004-05-08 08:23:19 +0000977trigger_cmd(A) ::= select(X). {A = sqlite3TriggerSelectStep(X); }
danielk1977c3f9bad2002-05-15 08:30:12 +0000978
danielk19776f349032002-06-11 02:25:40 +0000979// The special RAISE expression that may occur in trigger programs
drh4b59ab52002-08-24 18:24:51 +0000980expr(A) ::= RAISE(X) LP IGNORE RP(Y). {
danielk19774adee202004-05-08 08:23:19 +0000981 A = sqlite3Expr(TK_RAISE, 0, 0, 0);
drh4b59ab52002-08-24 18:24:51 +0000982 A->iColumn = OE_Ignore;
danielk19774adee202004-05-08 08:23:19 +0000983 sqlite3ExprSpan(A, &X, &Y);
drh4b59ab52002-08-24 18:24:51 +0000984}
drh74ad7fe2004-10-07 03:06:28 +0000985expr(A) ::= RAISE(X) LP raisetype(T) COMMA nm(Z) RP(Y). {
danielk19774adee202004-05-08 08:23:19 +0000986 A = sqlite3Expr(TK_RAISE, 0, 0, &Z);
drh74ad7fe2004-10-07 03:06:28 +0000987 A->iColumn = T;
danielk19774adee202004-05-08 08:23:19 +0000988 sqlite3ExprSpan(A, &X, &Y);
drh4b59ab52002-08-24 18:24:51 +0000989}
drhb7f91642004-10-31 02:22:47 +0000990%endif // !SQLITE_OMIT_TRIGGER
991
drh74ad7fe2004-10-07 03:06:28 +0000992%type raisetype {int}
993raisetype(A) ::= ROLLBACK. {A = OE_Rollback;}
994raisetype(A) ::= ABORT. {A = OE_Abort;}
995raisetype(A) ::= FAIL. {A = OE_Fail;}
996
danielk19776f349032002-06-11 02:25:40 +0000997
danielk1977c3f9bad2002-05-15 08:30:12 +0000998//////////////////////// DROP TRIGGER statement //////////////////////////////
drhb7f91642004-10-31 02:22:47 +0000999%ifndef SQLITE_OMIT_TRIGGER
drh74ad7fe2004-10-07 03:06:28 +00001000cmd ::= DROP TRIGGER fullname(X). {
1001 sqlite3DropTrigger(pParse,X);
danielk1977c3f9bad2002-05-15 08:30:12 +00001002}
drhb7f91642004-10-31 02:22:47 +00001003%endif // !SQLITE_OMIT_TRIGGER
drh113088e2003-03-20 01:16:58 +00001004
1005//////////////////////// ATTACH DATABASE file AS name /////////////////////////
danielk1977f744bb52005-12-06 17:19:11 +00001006cmd ::= ATTACH database_kw_opt expr(F) AS expr(D) key_opt(K). {
1007 sqlite3Attach(pParse, F, D, K);
drh1c2d8412003-03-31 00:30:47 +00001008}
danielk1977f744bb52005-12-06 17:19:11 +00001009%type key_opt {Expr *}
danielk1977dba99bc2006-01-07 14:02:26 +00001010%destructor key_opt {sqlite3ExprDelete($$);}
danielk1977f744bb52005-12-06 17:19:11 +00001011key_opt(A) ::= . { A = 0; }
1012key_opt(A) ::= KEY expr(X). { A = X; }
drh113088e2003-03-20 01:16:58 +00001013
1014database_kw_opt ::= DATABASE.
1015database_kw_opt ::= .
1016
1017//////////////////////// DETACH DATABASE name /////////////////////////////////
danielk1977f744bb52005-12-06 17:19:11 +00001018cmd ::= DETACH database_kw_opt expr(D). {
1019 sqlite3Detach(pParse, D);
drh1c2d8412003-03-31 00:30:47 +00001020}
drh4343fea2004-11-05 23:46:15 +00001021
1022////////////////////////// REINDEX collation //////////////////////////////////
1023%ifndef SQLITE_OMIT_REINDEX
1024cmd ::= REINDEX. {sqlite3Reindex(pParse, 0, 0);}
1025cmd ::= REINDEX nm(X) dbnm(Y). {sqlite3Reindex(pParse, &X, &Y);}
1026%endif
danielk19779fd2a9a2004-11-12 13:42:30 +00001027
drh9f18e8a2005-07-08 12:13:04 +00001028/////////////////////////////////// ANALYZE ///////////////////////////////////
1029%ifndef SQLITE_OMIT_ANALYZE
1030cmd ::= ANALYZE. {sqlite3Analyze(pParse, 0, 0);}
1031cmd ::= ANALYZE nm(X) dbnm(Y). {sqlite3Analyze(pParse, &X, &Y);}
1032%endif
1033
danielk19779fd2a9a2004-11-12 13:42:30 +00001034//////////////////////// ALTER TABLE table ... ////////////////////////////////
1035%ifndef SQLITE_OMIT_ALTERTABLE
1036cmd ::= ALTER TABLE fullname(X) RENAME TO nm(Z). {
1037 sqlite3AlterRenameTable(pParse,X,&Z);
1038}
danielk197719a8e7e2005-03-17 05:03:38 +00001039cmd ::= ALTER TABLE add_column_fullname ADD kwcolumn_opt column(Y). {
1040 sqlite3AlterFinishAddColumn(pParse, &Y);
1041}
1042add_column_fullname ::= fullname(X). {
1043 sqlite3AlterBeginAddColumn(pParse, X);
1044}
1045kwcolumn_opt ::= .
1046kwcolumn_opt ::= COLUMNKW.
danielk19779fd2a9a2004-11-12 13:42:30 +00001047%endif