blob: b45d1986cef56b58bf6c82251dc6bb979a486bef [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**
drh8aa34ae2006-03-13 12:54:09 +000017** @(#) $Id: parse.y,v 1.199 2006/03/13 12:54:10 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}
drh8fc33452006-02-27 21:58:07 +000043%stack_overflow {
44 sqlite3ErrorMsg(pParse, "parser stack overflow");
45}
drh487e2622005-06-25 18:42:14 +000046
47// The name of the generated procedure that implements the parser
48// is as follows:
danielk19774adee202004-05-08 08:23:19 +000049%name sqlite3Parser
drh487e2622005-06-25 18:42:14 +000050
51// The following text is included near the beginning of the C source
52// code file that implements the parser.
53//
drh348784e2000-05-29 20:41:49 +000054%include {
55#include "sqliteInt.h"
56#include "parse.h"
drh9bbca4c2001-11-06 04:00:18 +000057
58/*
drhad3cab52002-05-24 02:04:32 +000059** An instance of this structure holds information about the
60** LIMIT clause of a SELECT statement.
drh9bbca4c2001-11-06 04:00:18 +000061*/
drhad3cab52002-05-24 02:04:32 +000062struct LimitVal {
danielk1977a2dc3b12005-02-05 12:48:48 +000063 Expr *pLimit; /* The LIMIT expression. NULL if there is no limit */
64 Expr *pOffset; /* The OFFSET expression. NULL if there is none */
drhad3cab52002-05-24 02:04:32 +000065};
danielk1977c3f9bad2002-05-15 08:30:12 +000066
67/*
drh2e3a1f12004-10-06 14:39:28 +000068** An instance of this structure is used to store the LIKE,
69** GLOB, NOT LIKE, and NOT GLOB operators.
70*/
71struct LikeOp {
drhb52076c2006-01-23 13:22:09 +000072 Token eOperator; /* "like" or "glob" or "regexp" */
drhb71090f2005-05-23 17:26:51 +000073 int not; /* True if the NOT keyword is present */
drh2e3a1f12004-10-06 14:39:28 +000074};
75
76/*
drhad3cab52002-05-24 02:04:32 +000077** An instance of the following structure describes the event of a
78** TRIGGER. "a" is the event type, one of TK_UPDATE, TK_INSERT,
79** TK_DELETE, or TK_INSTEAD. If the event is of the form
80**
81** UPDATE ON (a,b,c)
82**
83** Then the "b" IdList records the list "a,b,c".
danielk1977c3f9bad2002-05-15 08:30:12 +000084*/
drhad3cab52002-05-24 02:04:32 +000085struct TrigEvent { int a; IdList * b; };
drhcaec2f12003-01-07 02:47:47 +000086
drh25d65432004-07-22 15:02:25 +000087/*
88** An instance of this structure holds the ATTACH key and the key type.
89*/
90struct AttachKey { int type; Token key; };
91
drhcaec2f12003-01-07 02:47:47 +000092} // end %include
drh348784e2000-05-29 20:41:49 +000093
drh826fb5a2004-02-14 23:59:57 +000094// Input is a single SQL command
drhc4a3c772001-04-04 11:48:57 +000095input ::= cmdlist.
drh094b2bb2002-03-13 18:54:07 +000096cmdlist ::= cmdlist ecmd.
drh826fb5a2004-02-14 23:59:57 +000097cmdlist ::= ecmd.
drh80242052004-06-09 00:48:12 +000098cmdx ::= cmd. { sqlite3FinishCoding(pParse); }
drhb7f91642004-10-31 02:22:47 +000099ecmd ::= SEMI.
100ecmd ::= explain cmdx SEMI.
danielk19774adee202004-05-08 08:23:19 +0000101explain ::= . { sqlite3BeginParse(pParse, 0); }
drhb7f91642004-10-31 02:22:47 +0000102%ifndef SQLITE_OMIT_EXPLAIN
drhecc92422005-09-10 16:46:12 +0000103explain ::= EXPLAIN. { sqlite3BeginParse(pParse, 1); }
104explain ::= EXPLAIN QUERY PLAN. { sqlite3BeginParse(pParse, 2); }
drhb7f91642004-10-31 02:22:47 +0000105%endif
drh348784e2000-05-29 20:41:49 +0000106
drh382c0242001-10-06 16:33:02 +0000107///////////////////// Begin and end transactions. ////////////////////////////
drhc4a3c772001-04-04 11:48:57 +0000108//
drhfa86c412002-02-02 15:01:15 +0000109
drh684917c2004-10-05 02:41:42 +0000110cmd ::= BEGIN transtype(Y) trans_opt. {sqlite3BeginTransaction(pParse, Y);}
drhc4a3c772001-04-04 11:48:57 +0000111trans_opt ::= .
112trans_opt ::= TRANSACTION.
drh5ad1a6c2002-07-01 12:27:09 +0000113trans_opt ::= TRANSACTION nm.
drh684917c2004-10-05 02:41:42 +0000114%type transtype {int}
115transtype(A) ::= . {A = TK_DEFERRED;}
116transtype(A) ::= DEFERRED(X). {A = @X;}
117transtype(A) ::= IMMEDIATE(X). {A = @X;}
118transtype(A) ::= EXCLUSIVE(X). {A = @X;}
danielk19774adee202004-05-08 08:23:19 +0000119cmd ::= COMMIT trans_opt. {sqlite3CommitTransaction(pParse);}
120cmd ::= END trans_opt. {sqlite3CommitTransaction(pParse);}
121cmd ::= ROLLBACK trans_opt. {sqlite3RollbackTransaction(pParse);}
drhc4a3c772001-04-04 11:48:57 +0000122
drh382c0242001-10-06 16:33:02 +0000123///////////////////// The CREATE TABLE statement ////////////////////////////
drh348784e2000-05-29 20:41:49 +0000124//
125cmd ::= create_table create_table_args.
drh74161702006-02-24 02:53:49 +0000126create_table ::= CREATE temp(T) TABLE ifnotexists(E) nm(Y) dbnm(Z). {
127 sqlite3StartTable(pParse,&Y,&Z,T,0,E);
drh969fa7c2002-02-18 18:30:32 +0000128}
drhfaa59552005-12-29 23:33:54 +0000129%type ifnotexists {int}
130ifnotexists(A) ::= . {A = 0;}
131ifnotexists(A) ::= IF NOT EXISTS. {A = 1;}
drhf57b3392001-10-08 13:22:32 +0000132%type temp {int}
danielk197753c0f742005-03-29 03:10:59 +0000133%ifndef SQLITE_OMIT_TEMPDB
drhd24cc422003-03-27 12:51:24 +0000134temp(A) ::= TEMP. {A = 1;}
danielk197753c0f742005-03-29 03:10:59 +0000135%endif
drhd24cc422003-03-27 12:51:24 +0000136temp(A) ::= . {A = 0;}
danielk197719a8e7e2005-03-17 05:03:38 +0000137create_table_args ::= LP columnlist conslist_opt(X) RP(Y). {
138 sqlite3EndTable(pParse,&X,&Y,0);
drh969fa7c2002-02-18 18:30:32 +0000139}
140create_table_args ::= AS select(S). {
danielk197719a8e7e2005-03-17 05:03:38 +0000141 sqlite3EndTable(pParse,0,0,S);
danielk19774adee202004-05-08 08:23:19 +0000142 sqlite3SelectDelete(S);
drh969fa7c2002-02-18 18:30:32 +0000143}
drh348784e2000-05-29 20:41:49 +0000144columnlist ::= columnlist COMMA column.
145columnlist ::= column.
146
drh487e2622005-06-25 18:42:14 +0000147// A "column" is a complete description of a single column in a
148// CREATE TABLE statement. This includes the column name, its
149// datatype, and other keywords such as PRIMARY KEY, UNIQUE, REFERENCES,
150// NOT NULL and so forth.
drh348784e2000-05-29 20:41:49 +0000151//
danielk197719a8e7e2005-03-17 05:03:38 +0000152column(A) ::= columnid(X) type carglist. {
153 A.z = X.z;
154 A.n = (pParse->sLastToken.z-X.z) + pParse->sLastToken.n;
155}
156columnid(A) ::= nm(X). {
157 sqlite3AddColumn(pParse,&X);
158 A = X;
159}
160
drhc4a3c772001-04-04 11:48:57 +0000161
162// An IDENTIFIER can be a generic identifier, or one of several
163// keywords. Any non-standard keyword can also be an identifier.
drhc4a3c772001-04-04 11:48:57 +0000164//
drh982cef72000-05-30 16:27:03 +0000165%type id {Token}
drhf18543c2002-03-30 15:26:50 +0000166id(A) ::= ID(X). {A = X;}
drh0bd1f4e2002-06-06 18:54:39 +0000167
drh34e33bb2002-06-06 19:04:16 +0000168// The following directive causes tokens ABORT, AFTER, ASC, etc. to
169// fallback to ID if they will not parse as their original value.
170// This obviates the need for the "id" nonterminal.
171//
drh319e4e72003-09-30 01:54:13 +0000172%fallback ID
drh9f18e8a2005-07-08 12:13:04 +0000173 ABORT AFTER ANALYZE ASC ATTACH BEFORE BEGIN CASCADE CAST CONFLICT
drh684917c2004-10-05 02:41:42 +0000174 DATABASE DEFERRED DESC DETACH EACH END EXCLUSIVE EXPLAIN FAIL FOR
drhecc92422005-09-10 16:46:12 +0000175 IGNORE IMMEDIATE INITIALLY INSTEAD LIKE_KW MATCH PLAN QUERY KEY
drh5ad1a6c2002-07-01 12:27:09 +0000176 OF OFFSET PRAGMA RAISE REPLACE RESTRICT ROW STATEMENT
drhb7f91642004-10-31 02:22:47 +0000177 TEMP TRIGGER VACUUM VIEW
178%ifdef SQLITE_OMIT_COMPOUND_SELECT
179 EXCEPT INTERSECT UNION
180%endif
drha0733842005-12-29 01:11:36 +0000181 REINDEX RENAME CTIME_KW IF
drhb7f91642004-10-31 02:22:47 +0000182 .
drhc4a3c772001-04-04 11:48:57 +0000183
drh2d3917d2004-02-22 16:27:00 +0000184// Define operator precedence early so that this is the first occurance
185// of the operator tokens in the grammer. Keeping the operators together
186// causes them to be assigned integer values that are close together,
187// which keeps parser tables smaller.
188//
drhf2bc0132004-10-04 13:19:23 +0000189// The token values assigned to these symbols is determined by the order
190// in which lemon first sees them. It must be the case that ISNULL/NOTNULL,
191// NE/EQ, GT/LE, and GE/LT are separated by only a single value. See
192// the sqlite3ExprIfFalse() routine for additional information on this
193// constraint.
194//
drh2d3917d2004-02-22 16:27:00 +0000195%left OR.
196%left AND.
197%right NOT.
drhb71090f2005-05-23 17:26:51 +0000198%left IS LIKE_KW BETWEEN IN ISNULL NOTNULL NE EQ.
drh9a432672004-10-04 13:38:09 +0000199%left GT LE LT GE.
danielk19777c6303c2004-11-17 16:41:29 +0000200%right ESCAPE.
drh2d3917d2004-02-22 16:27:00 +0000201%left BITAND BITOR LSHIFT RSHIFT.
202%left PLUS MINUS.
203%left STAR SLASH REM.
204%left CONCAT.
205%right UMINUS UPLUS BITNOT.
206
drhc4a3c772001-04-04 11:48:57 +0000207// And "ids" is an identifer-or-string.
208//
209%type ids {Token}
drhfd405312005-11-06 04:06:59 +0000210ids(A) ::= ID|STRING(X). {A = X;}
drhc4a3c772001-04-04 11:48:57 +0000211
drh5ad1a6c2002-07-01 12:27:09 +0000212// The name of a column or table can be any of the following:
213//
214%type nm {Token}
215nm(A) ::= ID(X). {A = X;}
216nm(A) ::= STRING(X). {A = X;}
217nm(A) ::= JOIN_KW(X). {A = X;}
218
drh487e2622005-06-25 18:42:14 +0000219// A typetoken is really one or more tokens that form a type name such
220// as can be found after the column name in a CREATE TABLE statement.
221// Multiple tokens are concatenated to form the value of the typetoken.
222//
223%type typetoken {Token}
drh382c0242001-10-06 16:33:02 +0000224type ::= .
drh487e2622005-06-25 18:42:14 +0000225type ::= typetoken(X). {sqlite3AddColumnType(pParse,&X);}
226typetoken(A) ::= typename(X). {A = X;}
227typetoken(A) ::= typename(X) LP signed RP(Y). {
228 A.z = X.z;
229 A.n = &Y.z[Y.n] - X.z;
230}
231typetoken(A) ::= typename(X) LP signed COMMA signed RP(Y). {
232 A.z = X.z;
233 A.n = &Y.z[Y.n] - X.z;
234}
drh382c0242001-10-06 16:33:02 +0000235%type typename {Token}
drhe2ea40d2004-05-20 12:41:19 +0000236typename(A) ::= ids(X). {A = X;}
drh596bd232004-09-30 14:22:47 +0000237typename(A) ::= typename(X) ids(Y). {A.z=X.z; A.n=Y.n+(Y.z-X.z);}
drhef0cae52003-07-16 02:19:37 +0000238%type signed {int}
drh2646da72005-12-09 20:02:05 +0000239signed(A) ::= plus_num(X). { A = atoi((char*)X.z); }
240signed(A) ::= minus_num(X). { A = -atoi((char*)X.z); }
drh487e2622005-06-25 18:42:14 +0000241
242// "carglist" is a list of additional constraints that come after the
243// column name and column type in a CREATE TABLE statement.
244//
drh348784e2000-05-29 20:41:49 +0000245carglist ::= carglist carg.
246carglist ::= .
drh5ad1a6c2002-07-01 12:27:09 +0000247carg ::= CONSTRAINT nm ccons.
drh348784e2000-05-29 20:41:49 +0000248carg ::= ccons.
danielk19777977a172004-11-09 12:44:37 +0000249carg ::= DEFAULT term(X). {sqlite3AddDefaultValue(pParse,X);}
drheb55bd22005-06-30 17:04:21 +0000250carg ::= DEFAULT LP expr(X) RP. {sqlite3AddDefaultValue(pParse,X);}
danielk19777977a172004-11-09 12:44:37 +0000251carg ::= DEFAULT PLUS term(X). {sqlite3AddDefaultValue(pParse,X);}
252carg ::= DEFAULT MINUS term(X). {
253 Expr *p = sqlite3Expr(TK_UMINUS, X, 0, 0);
254 sqlite3AddDefaultValue(pParse,p);
255}
256carg ::= DEFAULT id(X). {
257 Expr *p = sqlite3Expr(TK_STRING, 0, 0, &X);
258 sqlite3AddDefaultValue(pParse,p);
259}
drh348784e2000-05-29 20:41:49 +0000260
drh382c0242001-10-06 16:33:02 +0000261// In addition to the type name, we also care about the primary key and
262// UNIQUE constraints.
drh348784e2000-05-29 20:41:49 +0000263//
drh0d316a42002-08-11 20:10:47 +0000264ccons ::= NULL onconf.
danielk19774adee202004-05-08 08:23:19 +0000265ccons ::= NOT NULL onconf(R). {sqlite3AddNotNull(pParse, R);}
drhfdd6e852005-12-16 01:06:16 +0000266ccons ::= PRIMARY KEY sortorder(Z) onconf(R) autoinc(I).
267 {sqlite3AddPrimaryKey(pParse,0,R,I,Z);}
drh4d91a702006-01-04 15:54:36 +0000268ccons ::= UNIQUE onconf(R). {sqlite3CreateIndex(pParse,0,0,0,0,R,0,0,0,0);}
drh06f65412005-11-03 02:03:13 +0000269ccons ::= CHECK LP expr(X) RP. {sqlite3AddCheckConstraint(pParse,X);}
drhc2eef3b2002-08-31 18:53:06 +0000270ccons ::= REFERENCES nm(T) idxlist_opt(TA) refargs(R).
danielk19774adee202004-05-08 08:23:19 +0000271 {sqlite3CreateForeignKey(pParse,0,&T,TA,R);}
272ccons ::= defer_subclause(D). {sqlite3DeferForeignKey(pParse,D);}
drh2646da72005-12-09 20:02:05 +0000273ccons ::= COLLATE id(C). {sqlite3AddCollateType(pParse, (char*)C.z, C.n);}
drh04738cb2002-06-02 18:19:00 +0000274
drh205f48e2004-11-05 00:43:11 +0000275// The optional AUTOINCREMENT keyword
276%type autoinc {int}
drh2958a4e2004-11-12 03:56:15 +0000277autoinc(X) ::= . {X = 0;}
278autoinc(X) ::= AUTOINCR. {X = 1;}
drh205f48e2004-11-05 00:43:11 +0000279
drhc2eef3b2002-08-31 18:53:06 +0000280// The next group of rules parses the arguments to a REFERENCES clause
281// that determine if the referential integrity checking is deferred or
282// or immediate and which determine what action to take if a ref-integ
283// check fails.
drh04738cb2002-06-02 18:19:00 +0000284//
drhc2eef3b2002-08-31 18:53:06 +0000285%type refargs {int}
286refargs(A) ::= . { A = OE_Restrict * 0x010101; }
287refargs(A) ::= refargs(X) refarg(Y). { A = (X & Y.mask) | Y.value; }
288%type refarg {struct {int value; int mask;}}
289refarg(A) ::= MATCH nm. { A.value = 0; A.mask = 0x000000; }
290refarg(A) ::= ON DELETE refact(X). { A.value = X; A.mask = 0x0000ff; }
291refarg(A) ::= ON UPDATE refact(X). { A.value = X<<8; A.mask = 0x00ff00; }
292refarg(A) ::= ON INSERT refact(X). { A.value = X<<16; A.mask = 0xff0000; }
293%type refact {int}
294refact(A) ::= SET NULL. { A = OE_SetNull; }
295refact(A) ::= SET DEFAULT. { A = OE_SetDflt; }
296refact(A) ::= CASCADE. { A = OE_Cascade; }
297refact(A) ::= RESTRICT. { A = OE_Restrict; }
298%type defer_subclause {int}
299defer_subclause(A) ::= NOT DEFERRABLE init_deferred_pred_opt(X). {A = X;}
300defer_subclause(A) ::= DEFERRABLE init_deferred_pred_opt(X). {A = X;}
301%type init_deferred_pred_opt {int}
302init_deferred_pred_opt(A) ::= . {A = 0;}
303init_deferred_pred_opt(A) ::= INITIALLY DEFERRED. {A = 1;}
304init_deferred_pred_opt(A) ::= INITIALLY IMMEDIATE. {A = 0;}
drh348784e2000-05-29 20:41:49 +0000305
306// For the time being, the only constraint we care about is the primary
drh382c0242001-10-06 16:33:02 +0000307// key and UNIQUE. Both create indices.
drh348784e2000-05-29 20:41:49 +0000308//
danielk197719a8e7e2005-03-17 05:03:38 +0000309conslist_opt(A) ::= . {A.n = 0; A.z = 0;}
310conslist_opt(A) ::= COMMA(X) conslist. {A = X;}
drh348784e2000-05-29 20:41:49 +0000311conslist ::= conslist COMMA tcons.
drha2e1bb52001-01-04 14:20:18 +0000312conslist ::= conslist tcons.
drh348784e2000-05-29 20:41:49 +0000313conslist ::= tcons.
drh5ad1a6c2002-07-01 12:27:09 +0000314tcons ::= CONSTRAINT nm.
drhf3388142004-11-13 03:48:06 +0000315tcons ::= PRIMARY KEY LP idxlist(X) autoinc(I) RP onconf(R).
drhfdd6e852005-12-16 01:06:16 +0000316 {sqlite3AddPrimaryKey(pParse,X,R,I,0);}
drh9cfcf5d2002-01-29 18:41:24 +0000317tcons ::= UNIQUE LP idxlist(X) RP onconf(R).
drh4d91a702006-01-04 15:54:36 +0000318 {sqlite3CreateIndex(pParse,0,0,0,X,R,0,0,0,0);}
drh06f65412005-11-03 02:03:13 +0000319tcons ::= CHECK LP expr(E) RP onconf. {sqlite3AddCheckConstraint(pParse,E);}
drhc2eef3b2002-08-31 18:53:06 +0000320tcons ::= FOREIGN KEY LP idxlist(FA) RP
321 REFERENCES nm(T) idxlist_opt(TA) refargs(R) defer_subclause_opt(D). {
danielk19774adee202004-05-08 08:23:19 +0000322 sqlite3CreateForeignKey(pParse, FA, &T, TA, R);
323 sqlite3DeferForeignKey(pParse, D);
drhc2eef3b2002-08-31 18:53:06 +0000324}
325%type defer_subclause_opt {int}
326defer_subclause_opt(A) ::= . {A = 0;}
327defer_subclause_opt(A) ::= defer_subclause(X). {A = X;}
drh9cfcf5d2002-01-29 18:41:24 +0000328
329// The following is a non-standard extension that allows us to declare the
330// default behavior when there is a constraint conflict.
331//
332%type onconf {int}
drh1c928532002-01-31 15:54:21 +0000333%type orconf {int}
334%type resolvetype {int}
drh74ad7fe2004-10-07 03:06:28 +0000335onconf(A) ::= . {A = OE_Default;}
336onconf(A) ::= ON CONFLICT resolvetype(X). {A = X;}
337orconf(A) ::= . {A = OE_Default;}
338orconf(A) ::= OR resolvetype(X). {A = X;}
339resolvetype(A) ::= raisetype(X). {A = X;}
340resolvetype(A) ::= IGNORE. {A = OE_Ignore;}
341resolvetype(A) ::= REPLACE. {A = OE_Replace;}
drh348784e2000-05-29 20:41:49 +0000342
drh382c0242001-10-06 16:33:02 +0000343////////////////////////// The DROP TABLE /////////////////////////////////////
drh348784e2000-05-29 20:41:49 +0000344//
drha0733842005-12-29 01:11:36 +0000345cmd ::= DROP TABLE ifexists(E) fullname(X). {
346 sqlite3DropTable(pParse, X, 0, E);
danielk1977a8858102004-05-28 12:11:21 +0000347}
drha0733842005-12-29 01:11:36 +0000348%type ifexists {int}
349ifexists(A) ::= IF EXISTS. {A = 1;}
350ifexists(A) ::= . {A = 0;}
drh348784e2000-05-29 20:41:49 +0000351
drha76b5df2002-02-23 02:32:10 +0000352///////////////////// The CREATE VIEW statement /////////////////////////////
353//
drhb7f91642004-10-31 02:22:47 +0000354%ifndef SQLITE_OMIT_VIEW
danielk197748dec7e2004-05-28 12:33:30 +0000355cmd ::= CREATE(X) temp(T) VIEW nm(Y) dbnm(Z) AS select(S). {
356 sqlite3CreateView(pParse, &X, &Y, &Z, S, T);
drha76b5df2002-02-23 02:32:10 +0000357}
drha0733842005-12-29 01:11:36 +0000358cmd ::= DROP VIEW ifexists(E) fullname(X). {
359 sqlite3DropTable(pParse, X, 1, E);
drha76b5df2002-02-23 02:32:10 +0000360}
drhb7f91642004-10-31 02:22:47 +0000361%endif // SQLITE_OMIT_VIEW
drha76b5df2002-02-23 02:32:10 +0000362
drh382c0242001-10-06 16:33:02 +0000363//////////////////////// The SELECT statement /////////////////////////////////
drh348784e2000-05-29 20:41:49 +0000364//
drh9bb61fe2000-06-05 16:01:39 +0000365cmd ::= select(X). {
danielk1977b3bce662005-01-29 08:32:43 +0000366 sqlite3Select(pParse, X, SRT_Callback, 0, 0, 0, 0, 0);
danielk19774adee202004-05-08 08:23:19 +0000367 sqlite3SelectDelete(X);
drh9bb61fe2000-06-05 16:01:39 +0000368}
drhefb72512000-05-31 20:00:52 +0000369
drh9bb61fe2000-06-05 16:01:39 +0000370%type select {Select*}
danielk19774adee202004-05-08 08:23:19 +0000371%destructor select {sqlite3SelectDelete($$);}
drh82c3d632000-06-06 21:56:07 +0000372%type oneselect {Select*}
danielk19774adee202004-05-08 08:23:19 +0000373%destructor oneselect {sqlite3SelectDelete($$);}
drh9bb61fe2000-06-05 16:01:39 +0000374
drh82c3d632000-06-06 21:56:07 +0000375select(A) ::= oneselect(X). {A = X;}
drhb7f91642004-10-31 02:22:47 +0000376%ifndef SQLITE_OMIT_COMPOUND_SELECT
drh0a36c572002-02-18 22:49:59 +0000377select(A) ::= select(X) multiselect_op(Y) oneselect(Z). {
drhdaffd0e2001-04-11 14:28:42 +0000378 if( Z ){
drh82c3d632000-06-06 21:56:07 +0000379 Z->op = Y;
380 Z->pPrior = X;
drhdaffd0e2001-04-11 14:28:42 +0000381 }
382 A = Z;
drh82c3d632000-06-06 21:56:07 +0000383}
drh0a36c572002-02-18 22:49:59 +0000384%type multiselect_op {int}
drhfd405312005-11-06 04:06:59 +0000385multiselect_op(A) ::= UNION(OP). {A = @OP;}
386multiselect_op(A) ::= UNION ALL. {A = TK_ALL;}
387multiselect_op(A) ::= EXCEPT|INTERSECT(OP). {A = @OP;}
drhb7f91642004-10-31 02:22:47 +0000388%endif // SQLITE_OMIT_COMPOUND_SELECT
drh82c3d632000-06-06 21:56:07 +0000389oneselect(A) ::= SELECT distinct(D) selcollist(W) from(X) where_opt(Y)
drh9bbca4c2001-11-06 04:00:18 +0000390 groupby_opt(P) having_opt(Q) orderby_opt(Z) limit_opt(L). {
danielk1977a2dc3b12005-02-05 12:48:48 +0000391 A = sqlite3SelectNew(W,X,Y,P,Q,Z,D,L.pLimit,L.pOffset);
drh9bb61fe2000-06-05 16:01:39 +0000392}
393
394// The "distinct" nonterminal is true (1) if the DISTINCT keyword is
395// present and false (0) if it is not.
396//
drhefb72512000-05-31 20:00:52 +0000397%type distinct {int}
drhefb72512000-05-31 20:00:52 +0000398distinct(A) ::= DISTINCT. {A = 1;}
drhfef52082000-06-06 01:50:43 +0000399distinct(A) ::= ALL. {A = 0;}
drhefb72512000-05-31 20:00:52 +0000400distinct(A) ::= . {A = 0;}
drh348784e2000-05-29 20:41:49 +0000401
drh9bb61fe2000-06-05 16:01:39 +0000402// selcollist is a list of expressions that are to become the return
drh7c917d12001-12-16 20:05:05 +0000403// values of the SELECT statement. The "*" in statements like
404// "SELECT * FROM ..." is encoded as a special expression with an
405// opcode of TK_ALL.
drh9bb61fe2000-06-05 16:01:39 +0000406//
drh348784e2000-05-29 20:41:49 +0000407%type selcollist {ExprList*}
danielk19774adee202004-05-08 08:23:19 +0000408%destructor selcollist {sqlite3ExprListDelete($$);}
drh348784e2000-05-29 20:41:49 +0000409%type sclp {ExprList*}
danielk19774adee202004-05-08 08:23:19 +0000410%destructor sclp {sqlite3ExprListDelete($$);}
drh348784e2000-05-29 20:41:49 +0000411sclp(A) ::= selcollist(X) COMMA. {A = X;}
412sclp(A) ::= . {A = 0;}
drh01f3f252002-05-24 16:14:15 +0000413selcollist(A) ::= sclp(P) expr(X) as(Y). {
danielk19774adee202004-05-08 08:23:19 +0000414 A = sqlite3ExprListAppend(P,X,Y.n?&Y:0);
drh01f3f252002-05-24 16:14:15 +0000415}
drh7c917d12001-12-16 20:05:05 +0000416selcollist(A) ::= sclp(P) STAR. {
danielk19774adee202004-05-08 08:23:19 +0000417 A = sqlite3ExprListAppend(P, sqlite3Expr(TK_ALL, 0, 0, 0), 0);
drh7c917d12001-12-16 20:05:05 +0000418}
drh5ad1a6c2002-07-01 12:27:09 +0000419selcollist(A) ::= sclp(P) nm(X) DOT STAR. {
danielk19774adee202004-05-08 08:23:19 +0000420 Expr *pRight = sqlite3Expr(TK_ALL, 0, 0, 0);
421 Expr *pLeft = sqlite3Expr(TK_ID, 0, 0, &X);
422 A = sqlite3ExprListAppend(P, sqlite3Expr(TK_DOT, pLeft, pRight, 0), 0);
drh54473222002-04-04 02:10:55 +0000423}
drh01f3f252002-05-24 16:14:15 +0000424
425// An option "AS <id>" phrase that can follow one of the expressions that
426// define the result set, or one of the tables in the FROM clause.
427//
428%type as {Token}
drh74ad7fe2004-10-07 03:06:28 +0000429as(X) ::= AS nm(Y). {X = Y;}
430as(X) ::= ids(Y). {X = Y;}
431as(X) ::= . {X.n = 0;}
drh9bb61fe2000-06-05 16:01:39 +0000432
drh348784e2000-05-29 20:41:49 +0000433
drhad3cab52002-05-24 02:04:32 +0000434%type seltablist {SrcList*}
danielk19774adee202004-05-08 08:23:19 +0000435%destructor seltablist {sqlite3SrcListDelete($$);}
drhad3cab52002-05-24 02:04:32 +0000436%type stl_prefix {SrcList*}
danielk19774adee202004-05-08 08:23:19 +0000437%destructor stl_prefix {sqlite3SrcListDelete($$);}
drhad3cab52002-05-24 02:04:32 +0000438%type from {SrcList*}
danielk19774adee202004-05-08 08:23:19 +0000439%destructor from {sqlite3SrcListDelete($$);}
drh348784e2000-05-29 20:41:49 +0000440
drh01f3f252002-05-24 16:14:15 +0000441// A complete FROM clause.
442//
drhbf3a4fa2002-04-06 13:57:42 +0000443from(A) ::= . {A = sqliteMalloc(sizeof(*A));}
drh348784e2000-05-29 20:41:49 +0000444from(A) ::= FROM seltablist(X). {A = X;}
drh01f3f252002-05-24 16:14:15 +0000445
446// "seltablist" is a "Select Table List" - the content of the FROM clause
447// in a SELECT statement. "stl_prefix" is a prefix of this list.
448//
449stl_prefix(A) ::= seltablist(X) joinop(Y). {
450 A = X;
451 if( A && A->nSrc>0 ) A->a[A->nSrc-1].jointype = Y;
452}
drh348784e2000-05-29 20:41:49 +0000453stl_prefix(A) ::= . {A = 0;}
drh113088e2003-03-20 01:16:58 +0000454seltablist(A) ::= stl_prefix(X) nm(Y) dbnm(D) as(Z) on_opt(N) using_opt(U). {
danielk19774adee202004-05-08 08:23:19 +0000455 A = sqlite3SrcListAppend(X,&Y,&D);
456 if( Z.n ) sqlite3SrcListAddAlias(A,&Z);
drh01f3f252002-05-24 16:14:15 +0000457 if( N ){
458 if( A && A->nSrc>1 ){ A->a[A->nSrc-2].pOn = N; }
danielk19774adee202004-05-08 08:23:19 +0000459 else { sqlite3ExprDelete(N); }
drh01f3f252002-05-24 16:14:15 +0000460 }
461 if( U ){
462 if( A && A->nSrc>1 ){ A->a[A->nSrc-2].pUsing = U; }
danielk19774adee202004-05-08 08:23:19 +0000463 else { sqlite3IdListDelete(U); }
drh01f3f252002-05-24 16:14:15 +0000464 }
drhc4a3c772001-04-04 11:48:57 +0000465}
drh51522cd2005-01-20 13:36:19 +0000466%ifndef SQLITE_OMIT_SUBQUERY
467 seltablist(A) ::= stl_prefix(X) LP seltablist_paren(S) RP
468 as(Z) on_opt(N) using_opt(U). {
469 A = sqlite3SrcListAppend(X,0,0);
470 A->a[A->nSrc-1].pSelect = S;
471 if( Z.n ) sqlite3SrcListAddAlias(A,&Z);
472 if( N ){
473 if( A && A->nSrc>1 ){ A->a[A->nSrc-2].pOn = N; }
474 else { sqlite3ExprDelete(N); }
475 }
476 if( U ){
477 if( A && A->nSrc>1 ){ A->a[A->nSrc-2].pUsing = U; }
478 else { sqlite3IdListDelete(U); }
479 }
drhd5feede2002-05-08 21:46:14 +0000480 }
drh51522cd2005-01-20 13:36:19 +0000481
danielk1977955de522006-02-10 02:27:42 +0000482 // A seltablist_paren nonterminal represents anything in a FROM that
drh51522cd2005-01-20 13:36:19 +0000483 // is contained inside parentheses. This can be either a subquery or
484 // a grouping of table and subqueries.
485 //
486 %type seltablist_paren {Select*}
487 %destructor seltablist_paren {sqlite3SelectDelete($$);}
488 seltablist_paren(A) ::= select(S). {A = S;}
489 seltablist_paren(A) ::= seltablist(F). {
danielk1977a2dc3b12005-02-05 12:48:48 +0000490 A = sqlite3SelectNew(0,F,0,0,0,0,0,0,0);
drh01f3f252002-05-24 16:14:15 +0000491 }
drh51522cd2005-01-20 13:36:19 +0000492%endif // SQLITE_OMIT_SUBQUERY
drhb733d032004-01-24 20:18:12 +0000493
drh113088e2003-03-20 01:16:58 +0000494%type dbnm {Token}
495dbnm(A) ::= . {A.z=0; A.n=0;}
496dbnm(A) ::= DOT nm(X). {A = X;}
497
drh74ad7fe2004-10-07 03:06:28 +0000498%type fullname {SrcList*}
499%destructor fullname {sqlite3SrcListDelete($$);}
500fullname(A) ::= nm(X) dbnm(Y). {A = sqlite3SrcListAppend(0,&X,&Y);}
501
drh01f3f252002-05-24 16:14:15 +0000502%type joinop {int}
503%type joinop2 {int}
drhfd405312005-11-06 04:06:59 +0000504joinop(X) ::= COMMA|JOIN. { X = JT_INNER; }
danielk19774adee202004-05-08 08:23:19 +0000505joinop(X) ::= JOIN_KW(A) JOIN. { X = sqlite3JoinType(pParse,&A,0,0); }
506joinop(X) ::= JOIN_KW(A) nm(B) JOIN. { X = sqlite3JoinType(pParse,&A,&B,0); }
drh5ad1a6c2002-07-01 12:27:09 +0000507joinop(X) ::= JOIN_KW(A) nm(B) nm(C) JOIN.
danielk19774adee202004-05-08 08:23:19 +0000508 { X = sqlite3JoinType(pParse,&A,&B,&C); }
drh01f3f252002-05-24 16:14:15 +0000509
510%type on_opt {Expr*}
danielk19774adee202004-05-08 08:23:19 +0000511%destructor on_opt {sqlite3ExprDelete($$);}
drh01f3f252002-05-24 16:14:15 +0000512on_opt(N) ::= ON expr(E). {N = E;}
513on_opt(N) ::= . {N = 0;}
514
515%type using_opt {IdList*}
danielk19774adee202004-05-08 08:23:19 +0000516%destructor using_opt {sqlite3IdListDelete($$);}
danielk19770202b292004-06-09 09:55:16 +0000517using_opt(U) ::= USING LP inscollist(L) RP. {U = L;}
drh01f3f252002-05-24 16:14:15 +0000518using_opt(U) ::= . {U = 0;}
519
520
drh348784e2000-05-29 20:41:49 +0000521%type orderby_opt {ExprList*}
danielk19774adee202004-05-08 08:23:19 +0000522%destructor orderby_opt {sqlite3ExprListDelete($$);}
drh348784e2000-05-29 20:41:49 +0000523%type sortlist {ExprList*}
danielk19774adee202004-05-08 08:23:19 +0000524%destructor sortlist {sqlite3ExprListDelete($$);}
drh348784e2000-05-29 20:41:49 +0000525%type sortitem {Expr*}
danielk19774adee202004-05-08 08:23:19 +0000526%destructor sortitem {sqlite3ExprDelete($$);}
drh348784e2000-05-29 20:41:49 +0000527
528orderby_opt(A) ::= . {A = 0;}
529orderby_opt(A) ::= ORDER BY sortlist(X). {A = X;}
drh8e2ca022002-06-17 17:07:19 +0000530sortlist(A) ::= sortlist(X) COMMA sortitem(Y) collate(C) sortorder(Z). {
drh1186b0a2004-05-20 23:37:54 +0000531 A = sqlite3ExprListAppend(X,Y,C.n>0?&C:0);
drhd3d39e92004-05-20 22:16:29 +0000532 if( A ) A->a[A->nExpr-1].sortOrder = Z;
drh9bb61fe2000-06-05 16:01:39 +0000533}
drh38640e12002-07-05 21:42:36 +0000534sortlist(A) ::= sortitem(Y) collate(C) sortorder(Z). {
drh1186b0a2004-05-20 23:37:54 +0000535 A = sqlite3ExprListAppend(0,Y,C.n>0?&C:0);
danielk197796fb0dd2004-06-30 09:49:22 +0000536 if( A && A->a ) A->a[0].sortOrder = Z;
drh9bb61fe2000-06-05 16:01:39 +0000537}
drhda9d6c42000-05-31 18:20:14 +0000538sortitem(A) ::= expr(X). {A = X;}
drh348784e2000-05-29 20:41:49 +0000539
540%type sortorder {int}
drhd3d39e92004-05-20 22:16:29 +0000541%type collate {Token}
drh348784e2000-05-29 20:41:49 +0000542
drh8e2ca022002-06-17 17:07:19 +0000543sortorder(A) ::= ASC. {A = SQLITE_SO_ASC;}
544sortorder(A) ::= DESC. {A = SQLITE_SO_DESC;}
545sortorder(A) ::= . {A = SQLITE_SO_ASC;}
drhd3d39e92004-05-20 22:16:29 +0000546collate(C) ::= . {C.z = 0; C.n = 0;}
547collate(C) ::= COLLATE id(X). {C = X;}
drh348784e2000-05-29 20:41:49 +0000548
drh22827922000-06-06 17:27:05 +0000549%type groupby_opt {ExprList*}
danielk19774adee202004-05-08 08:23:19 +0000550%destructor groupby_opt {sqlite3ExprListDelete($$);}
drh6206d502000-06-19 19:09:08 +0000551groupby_opt(A) ::= . {A = 0;}
drh22827922000-06-06 17:27:05 +0000552groupby_opt(A) ::= GROUP BY exprlist(X). {A = X;}
553
554%type having_opt {Expr*}
danielk19774adee202004-05-08 08:23:19 +0000555%destructor having_opt {sqlite3ExprDelete($$);}
drh6206d502000-06-19 19:09:08 +0000556having_opt(A) ::= . {A = 0;}
drh22827922000-06-06 17:27:05 +0000557having_opt(A) ::= HAVING expr(X). {A = X;}
558
drhad3cab52002-05-24 02:04:32 +0000559%type limit_opt {struct LimitVal}
danielk1977a2dc3b12005-02-05 12:48:48 +0000560%destructor limit_opt {
561 sqlite3ExprDelete($$.pLimit);
562 sqlite3ExprDelete($$.pOffset);
563}
564limit_opt(A) ::= . {A.pLimit = 0; A.pOffset = 0;}
565limit_opt(A) ::= LIMIT expr(X). {A.pLimit = X; A.pOffset = 0;}
566limit_opt(A) ::= LIMIT expr(X) OFFSET expr(Y).
567 {A.pLimit = X; A.pOffset = Y;}
568limit_opt(A) ::= LIMIT expr(X) COMMA expr(Y).
569 {A.pOffset = X; A.pLimit = Y;}
drh9bbca4c2001-11-06 04:00:18 +0000570
drh382c0242001-10-06 16:33:02 +0000571/////////////////////////// The DELETE statement /////////////////////////////
572//
drh74ad7fe2004-10-07 03:06:28 +0000573cmd ::= DELETE FROM fullname(X) where_opt(Y). {sqlite3DeleteFrom(pParse,X,Y);}
drh348784e2000-05-29 20:41:49 +0000574
575%type where_opt {Expr*}
danielk19774adee202004-05-08 08:23:19 +0000576%destructor where_opt {sqlite3ExprDelete($$);}
drh348784e2000-05-29 20:41:49 +0000577
578where_opt(A) ::= . {A = 0;}
579where_opt(A) ::= WHERE expr(X). {A = X;}
580
drh382c0242001-10-06 16:33:02 +0000581////////////////////////// The UPDATE command ////////////////////////////////
582//
drh74ad7fe2004-10-07 03:06:28 +0000583cmd ::= UPDATE orconf(R) fullname(X) SET setlist(Y) where_opt(Z).
584 {sqlite3Update(pParse,X,Y,Z,R);}
drh348784e2000-05-29 20:41:49 +0000585
drhf8db1bc2005-04-22 02:38:37 +0000586%type setlist {ExprList*}
587%destructor setlist {sqlite3ExprListDelete($$);}
588
drh5ad1a6c2002-07-01 12:27:09 +0000589setlist(A) ::= setlist(Z) COMMA nm(X) EQ expr(Y).
danielk19774adee202004-05-08 08:23:19 +0000590 {A = sqlite3ExprListAppend(Z,Y,&X);}
591setlist(A) ::= nm(X) EQ expr(Y). {A = sqlite3ExprListAppend(0,Y,&X);}
drh348784e2000-05-29 20:41:49 +0000592
drh382c0242001-10-06 16:33:02 +0000593////////////////////////// The INSERT command /////////////////////////////////
594//
drh74ad7fe2004-10-07 03:06:28 +0000595cmd ::= insert_cmd(R) INTO fullname(X) inscollist_opt(F)
drh113088e2003-03-20 01:16:58 +0000596 VALUES LP itemlist(Y) RP.
drh74ad7fe2004-10-07 03:06:28 +0000597 {sqlite3Insert(pParse, X, Y, 0, F, R);}
598cmd ::= insert_cmd(R) INTO fullname(X) inscollist_opt(F) select(S).
599 {sqlite3Insert(pParse, X, 0, S, F, R);}
drh348784e2000-05-29 20:41:49 +0000600
drhfa86c412002-02-02 15:01:15 +0000601%type insert_cmd {int}
602insert_cmd(A) ::= INSERT orconf(R). {A = R;}
603insert_cmd(A) ::= REPLACE. {A = OE_Replace;}
604
drh348784e2000-05-29 20:41:49 +0000605
606%type itemlist {ExprList*}
danielk19774adee202004-05-08 08:23:19 +0000607%destructor itemlist {sqlite3ExprListDelete($$);}
drh348784e2000-05-29 20:41:49 +0000608
danielk19774adee202004-05-08 08:23:19 +0000609itemlist(A) ::= itemlist(X) COMMA expr(Y). {A = sqlite3ExprListAppend(X,Y,0);}
610itemlist(A) ::= expr(X). {A = sqlite3ExprListAppend(0,X,0);}
drh348784e2000-05-29 20:41:49 +0000611
drh967e8b72000-06-21 13:59:10 +0000612%type inscollist_opt {IdList*}
danielk19774adee202004-05-08 08:23:19 +0000613%destructor inscollist_opt {sqlite3IdListDelete($$);}
drh967e8b72000-06-21 13:59:10 +0000614%type inscollist {IdList*}
danielk19774adee202004-05-08 08:23:19 +0000615%destructor inscollist {sqlite3IdListDelete($$);}
drh348784e2000-05-29 20:41:49 +0000616
drhc4a3c772001-04-04 11:48:57 +0000617inscollist_opt(A) ::= . {A = 0;}
618inscollist_opt(A) ::= LP inscollist(X) RP. {A = X;}
danielk19774adee202004-05-08 08:23:19 +0000619inscollist(A) ::= inscollist(X) COMMA nm(Y). {A = sqlite3IdListAppend(X,&Y);}
620inscollist(A) ::= nm(Y). {A = sqlite3IdListAppend(0,&Y);}
drh348784e2000-05-29 20:41:49 +0000621
drh382c0242001-10-06 16:33:02 +0000622/////////////////////////// Expression Processing /////////////////////////////
623//
drh348784e2000-05-29 20:41:49 +0000624
625%type expr {Expr*}
danielk19774adee202004-05-08 08:23:19 +0000626%destructor expr {sqlite3ExprDelete($$);}
danielk19777977a172004-11-09 12:44:37 +0000627%type term {Expr*}
628%destructor term {sqlite3ExprDelete($$);}
drh348784e2000-05-29 20:41:49 +0000629
danielk19777977a172004-11-09 12:44:37 +0000630expr(A) ::= term(X). {A = X;}
danielk1977752e6792004-11-09 16:13:33 +0000631expr(A) ::= LP(B) expr(X) RP(E). {A = X; sqlite3ExprSpan(A,&B,&E); }
danielk19777977a172004-11-09 12:44:37 +0000632term(A) ::= NULL(X). {A = sqlite3Expr(@X, 0, 0, &X);}
danielk19774adee202004-05-08 08:23:19 +0000633expr(A) ::= ID(X). {A = sqlite3Expr(TK_ID, 0, 0, &X);}
634expr(A) ::= JOIN_KW(X). {A = sqlite3Expr(TK_ID, 0, 0, &X);}
drh5ad1a6c2002-07-01 12:27:09 +0000635expr(A) ::= nm(X) DOT nm(Y). {
danielk19774adee202004-05-08 08:23:19 +0000636 Expr *temp1 = sqlite3Expr(TK_ID, 0, 0, &X);
637 Expr *temp2 = sqlite3Expr(TK_ID, 0, 0, &Y);
638 A = sqlite3Expr(TK_DOT, temp1, temp2, 0);
drhe1b6a5b2000-07-29 13:06:59 +0000639}
drhd24cc422003-03-27 12:51:24 +0000640expr(A) ::= nm(X) DOT nm(Y) DOT nm(Z). {
danielk19774adee202004-05-08 08:23:19 +0000641 Expr *temp1 = sqlite3Expr(TK_ID, 0, 0, &X);
642 Expr *temp2 = sqlite3Expr(TK_ID, 0, 0, &Y);
643 Expr *temp3 = sqlite3Expr(TK_ID, 0, 0, &Z);
644 Expr *temp4 = sqlite3Expr(TK_DOT, temp2, temp3, 0);
645 A = sqlite3Expr(TK_DOT, temp1, temp4, 0);
drhd24cc422003-03-27 12:51:24 +0000646}
drhfd405312005-11-06 04:06:59 +0000647term(A) ::= INTEGER|FLOAT|BLOB(X). {A = sqlite3Expr(@X, 0, 0, &X);}
danielk19777977a172004-11-09 12:44:37 +0000648term(A) ::= STRING(X). {A = sqlite3Expr(@X, 0, 0, &X);}
drh4e0cff62004-11-05 05:10:28 +0000649expr(A) ::= REGISTER(X). {A = sqlite3RegisterExpr(pParse, &X);}
drh7c972de2003-09-06 22:18:07 +0000650expr(A) ::= VARIABLE(X). {
drh895d7472004-08-20 16:02:39 +0000651 Token *pToken = &X;
652 Expr *pExpr = A = sqlite3Expr(TK_VARIABLE, 0, 0, pToken);
drhfa6bc002004-09-07 16:19:52 +0000653 sqlite3ExprAssignVarNumber(pParse, pExpr);
drh7c972de2003-09-06 22:18:07 +0000654}
drh487e2622005-06-25 18:42:14 +0000655%ifndef SQLITE_OMIT_CAST
656expr(A) ::= CAST(X) LP expr(E) AS typetoken(T) RP(Y). {
657 A = sqlite3Expr(TK_CAST, E, 0, &T);
658 sqlite3ExprSpan(A,&X,&Y);
659}
660%endif // SQLITE_OMIT_CAST
drhfd357972005-09-09 01:33:19 +0000661expr(A) ::= ID(X) LP distinct(D) exprlist(Y) RP(E). {
danielk19774adee202004-05-08 08:23:19 +0000662 A = sqlite3ExprFunction(Y, &X);
663 sqlite3ExprSpan(A,&X,&E);
drh8aa34ae2006-03-13 12:54:09 +0000664 if( D && A ){
drhfd357972005-09-09 01:33:19 +0000665 A->flags |= EP_Distinct;
666 }
drhe1b6a5b2000-07-29 13:06:59 +0000667}
668expr(A) ::= ID(X) LP STAR RP(E). {
danielk19774adee202004-05-08 08:23:19 +0000669 A = sqlite3ExprFunction(0, &X);
670 sqlite3ExprSpan(A,&X,&E);
drhe1b6a5b2000-07-29 13:06:59 +0000671}
drhb71090f2005-05-23 17:26:51 +0000672term(A) ::= CTIME_KW(OP). {
673 /* The CURRENT_TIME, CURRENT_DATE, and CURRENT_TIMESTAMP values are
674 ** treated as functions that return constants */
675 A = sqlite3ExprFunction(0,&OP);
676 if( A ) A->op = TK_CONST_FUNC;
677}
drhfd405312005-11-06 04:06:59 +0000678expr(A) ::= expr(X) AND(OP) expr(Y). {A = sqlite3Expr(@OP, X, Y, 0);}
679expr(A) ::= expr(X) OR(OP) expr(Y). {A = sqlite3Expr(@OP, X, Y, 0);}
680expr(A) ::= expr(X) LT|GT|GE|LE(OP) expr(Y). {A = sqlite3Expr(@OP, X, Y, 0);}
681expr(A) ::= expr(X) EQ|NE(OP) expr(Y). {A = sqlite3Expr(@OP, X, Y, 0);}
682expr(A) ::= expr(X) BITAND|BITOR|LSHIFT|RSHIFT(OP) expr(Y).
683 {A = sqlite3Expr(@OP, X, Y, 0);}
684expr(A) ::= expr(X) PLUS|MINUS(OP) expr(Y). {A = sqlite3Expr(@OP, X, Y, 0);}
685expr(A) ::= expr(X) STAR|SLASH|REM(OP) expr(Y). {A = sqlite3Expr(@OP, X, Y, 0);}
686expr(A) ::= expr(X) CONCAT(OP) expr(Y). {A = sqlite3Expr(@OP, X, Y, 0);}
drh74ad7fe2004-10-07 03:06:28 +0000687%type likeop {struct LikeOp}
drhb52076c2006-01-23 13:22:09 +0000688likeop(A) ::= LIKE_KW(X). {A.eOperator = X; A.not = 0;}
689likeop(A) ::= NOT LIKE_KW(X). {A.eOperator = X; A.not = 1;}
danielk19777c6303c2004-11-17 16:41:29 +0000690%type escape {Expr*}
danielk1977dba99bc2006-01-07 14:02:26 +0000691%destructor escape {sqlite3ExprDelete($$);}
danielk19777c6303c2004-11-17 16:41:29 +0000692escape(X) ::= ESCAPE expr(A). [ESCAPE] {X = A;}
693escape(X) ::= . [ESCAPE] {X = 0;}
drhb71090f2005-05-23 17:26:51 +0000694expr(A) ::= expr(X) likeop(OP) expr(Y) escape(E). [LIKE_KW] {
drh8aa34ae2006-03-13 12:54:09 +0000695 ExprList *pList;
696 pList = sqlite3ExprListAppend(0, Y, 0);
danielk19774adee202004-05-08 08:23:19 +0000697 pList = sqlite3ExprListAppend(pList, X, 0);
danielk19777c6303c2004-11-17 16:41:29 +0000698 if( E ){
699 pList = sqlite3ExprListAppend(pList, E, 0);
700 }
drhb52076c2006-01-23 13:22:09 +0000701 A = sqlite3ExprFunction(pList, &OP.eOperator);
drh2e3a1f12004-10-06 14:39:28 +0000702 if( OP.not ) A = sqlite3Expr(TK_NOT, A, 0, 0);
danielk19774adee202004-05-08 08:23:19 +0000703 sqlite3ExprSpan(A, &X->span, &Y->span);
drh0ac65892002-04-20 14:24:41 +0000704}
danielk19777c6303c2004-11-17 16:41:29 +0000705
drhfd405312005-11-06 04:06:59 +0000706expr(A) ::= expr(X) ISNULL|NOTNULL(E). {
707 A = sqlite3Expr(@E, X, 0, 0);
danielk19774adee202004-05-08 08:23:19 +0000708 sqlite3ExprSpan(A,&X->span,&E);
drhe1b6a5b2000-07-29 13:06:59 +0000709}
drh33048c02001-10-01 14:29:22 +0000710expr(A) ::= expr(X) IS NULL(E). {
danielk19774adee202004-05-08 08:23:19 +0000711 A = sqlite3Expr(TK_ISNULL, X, 0, 0);
712 sqlite3ExprSpan(A,&X->span,&E);
drh33048c02001-10-01 14:29:22 +0000713}
drh33048c02001-10-01 14:29:22 +0000714expr(A) ::= expr(X) NOT NULL(E). {
danielk19774adee202004-05-08 08:23:19 +0000715 A = sqlite3Expr(TK_NOTNULL, X, 0, 0);
716 sqlite3ExprSpan(A,&X->span,&E);
drh33048c02001-10-01 14:29:22 +0000717}
drh81a20f22001-10-12 17:30:04 +0000718expr(A) ::= expr(X) IS NOT NULL(E). {
danielk19774adee202004-05-08 08:23:19 +0000719 A = sqlite3Expr(TK_NOTNULL, X, 0, 0);
720 sqlite3ExprSpan(A,&X->span,&E);
drh81a20f22001-10-12 17:30:04 +0000721}
drhfd405312005-11-06 04:06:59 +0000722expr(A) ::= NOT|BITNOT(B) expr(X). {
drh7ac25c72004-08-19 15:12:26 +0000723 A = sqlite3Expr(@B, X, 0, 0);
danielk19774adee202004-05-08 08:23:19 +0000724 sqlite3ExprSpan(A,&B,&X->span);
drh81a20f22001-10-12 17:30:04 +0000725}
drhe1b6a5b2000-07-29 13:06:59 +0000726expr(A) ::= MINUS(B) expr(X). [UMINUS] {
danielk19774adee202004-05-08 08:23:19 +0000727 A = sqlite3Expr(TK_UMINUS, X, 0, 0);
728 sqlite3ExprSpan(A,&B,&X->span);
drhe1b6a5b2000-07-29 13:06:59 +0000729}
drh4b59ab52002-08-24 18:24:51 +0000730expr(A) ::= PLUS(B) expr(X). [UPLUS] {
danielk19774adee202004-05-08 08:23:19 +0000731 A = sqlite3Expr(TK_UPLUS, X, 0, 0);
732 sqlite3ExprSpan(A,&B,&X->span);
drhe1b6a5b2000-07-29 13:06:59 +0000733}
drh2e3a1f12004-10-06 14:39:28 +0000734%type between_op {int}
735between_op(A) ::= BETWEEN. {A = 0;}
736between_op(A) ::= NOT BETWEEN. {A = 1;}
737expr(A) ::= expr(W) between_op(N) expr(X) AND expr(Y). [BETWEEN] {
danielk19774adee202004-05-08 08:23:19 +0000738 ExprList *pList = sqlite3ExprListAppend(0, X, 0);
739 pList = sqlite3ExprListAppend(pList, Y, 0);
740 A = sqlite3Expr(TK_BETWEEN, W, 0, 0);
drh53f733c2005-09-16 02:38:09 +0000741 if( A ){
742 A->pList = pList;
743 }else{
744 sqlite3ExprListDelete(pList);
745 }
drh2e3a1f12004-10-06 14:39:28 +0000746 if( N ) A = sqlite3Expr(TK_NOT, A, 0, 0);
danielk19774adee202004-05-08 08:23:19 +0000747 sqlite3ExprSpan(A,&W->span,&Y->span);
drhfef52082000-06-06 01:50:43 +0000748}
drh51522cd2005-01-20 13:36:19 +0000749%ifndef SQLITE_OMIT_SUBQUERY
danielk19773e8c37e2005-01-21 03:12:14 +0000750 %type in_op {int}
751 in_op(A) ::= IN. {A = 0;}
752 in_op(A) ::= NOT IN. {A = 1;}
753 expr(A) ::= expr(X) in_op(N) LP exprlist(Y) RP(E). [IN] {
754 A = sqlite3Expr(TK_IN, X, 0, 0);
danielk1977d5d56522005-03-16 12:15:20 +0000755 if( A ){
756 A->pList = Y;
757 }else{
758 sqlite3ExprListDelete(Y);
759 }
danielk19773e8c37e2005-01-21 03:12:14 +0000760 if( N ) A = sqlite3Expr(TK_NOT, A, 0, 0);
761 sqlite3ExprSpan(A,&X->span,&E);
762 }
drh51522cd2005-01-20 13:36:19 +0000763 expr(A) ::= LP(B) select(X) RP(E). {
764 A = sqlite3Expr(TK_SELECT, 0, 0, 0);
drh53f733c2005-09-16 02:38:09 +0000765 if( A ){
766 A->pSelect = X;
767 }else{
768 sqlite3SelectDelete(X);
769 }
drh51522cd2005-01-20 13:36:19 +0000770 sqlite3ExprSpan(A,&B,&E);
771 }
772 expr(A) ::= expr(X) in_op(N) LP select(Y) RP(E). [IN] {
773 A = sqlite3Expr(TK_IN, X, 0, 0);
drh53f733c2005-09-16 02:38:09 +0000774 if( A ){
775 A->pSelect = Y;
776 }else{
777 sqlite3SelectDelete(Y);
778 }
drh51522cd2005-01-20 13:36:19 +0000779 if( N ) A = sqlite3Expr(TK_NOT, A, 0, 0);
780 sqlite3ExprSpan(A,&X->span,&E);
781 }
782 expr(A) ::= expr(X) in_op(N) nm(Y) dbnm(Z). [IN] {
783 SrcList *pSrc = sqlite3SrcListAppend(0,&Y,&Z);
784 A = sqlite3Expr(TK_IN, X, 0, 0);
drh53f733c2005-09-16 02:38:09 +0000785 if( A ){
786 A->pSelect = sqlite3SelectNew(0,pSrc,0,0,0,0,0,0,0);
787 }else{
788 sqlite3SrcListDelete(pSrc);
789 }
drh51522cd2005-01-20 13:36:19 +0000790 if( N ) A = sqlite3Expr(TK_NOT, A, 0, 0);
791 sqlite3ExprSpan(A,&X->span,Z.z?&Z:&Y);
792 }
793 expr(A) ::= EXISTS(B) LP select(Y) RP(E). {
794 Expr *p = A = sqlite3Expr(TK_EXISTS, 0, 0, 0);
795 if( p ){
796 p->pSelect = Y;
797 sqlite3ExprSpan(p,&B,&E);
drh53f733c2005-09-16 02:38:09 +0000798 }else{
799 sqlite3SelectDelete(Y);
drh51522cd2005-01-20 13:36:19 +0000800 }
801 }
802%endif // SQLITE_OMIT_SUBQUERY
drhfef52082000-06-06 01:50:43 +0000803
drh17a7f8d2002-03-24 13:13:27 +0000804/* CASE expressions */
805expr(A) ::= CASE(C) case_operand(X) case_exprlist(Y) case_else(Z) END(E). {
danielk19774adee202004-05-08 08:23:19 +0000806 A = sqlite3Expr(TK_CASE, X, Z, 0);
drh53f733c2005-09-16 02:38:09 +0000807 if( A ){
808 A->pList = Y;
809 }else{
810 sqlite3ExprListDelete(Y);
811 }
danielk19774adee202004-05-08 08:23:19 +0000812 sqlite3ExprSpan(A, &C, &E);
drh17a7f8d2002-03-24 13:13:27 +0000813}
814%type case_exprlist {ExprList*}
danielk19774adee202004-05-08 08:23:19 +0000815%destructor case_exprlist {sqlite3ExprListDelete($$);}
drh17a7f8d2002-03-24 13:13:27 +0000816case_exprlist(A) ::= case_exprlist(X) WHEN expr(Y) THEN expr(Z). {
danielk19774adee202004-05-08 08:23:19 +0000817 A = sqlite3ExprListAppend(X, Y, 0);
818 A = sqlite3ExprListAppend(A, Z, 0);
drh17a7f8d2002-03-24 13:13:27 +0000819}
820case_exprlist(A) ::= WHEN expr(Y) THEN expr(Z). {
danielk19774adee202004-05-08 08:23:19 +0000821 A = sqlite3ExprListAppend(0, Y, 0);
822 A = sqlite3ExprListAppend(A, Z, 0);
drh17a7f8d2002-03-24 13:13:27 +0000823}
824%type case_else {Expr*}
danielk1977dba99bc2006-01-07 14:02:26 +0000825%destructor case_else {sqlite3ExprDelete($$);}
drh17a7f8d2002-03-24 13:13:27 +0000826case_else(A) ::= ELSE expr(X). {A = X;}
827case_else(A) ::= . {A = 0;}
828%type case_operand {Expr*}
danielk1977dba99bc2006-01-07 14:02:26 +0000829%destructor case_operand {sqlite3ExprDelete($$);}
drh17a7f8d2002-03-24 13:13:27 +0000830case_operand(A) ::= expr(X). {A = X;}
831case_operand(A) ::= . {A = 0;}
drh348784e2000-05-29 20:41:49 +0000832
833%type exprlist {ExprList*}
danielk19774adee202004-05-08 08:23:19 +0000834%destructor exprlist {sqlite3ExprListDelete($$);}
drh348784e2000-05-29 20:41:49 +0000835%type expritem {Expr*}
danielk19774adee202004-05-08 08:23:19 +0000836%destructor expritem {sqlite3ExprDelete($$);}
drh348784e2000-05-29 20:41:49 +0000837
drh348784e2000-05-29 20:41:49 +0000838exprlist(A) ::= exprlist(X) COMMA expritem(Y).
drh74ad7fe2004-10-07 03:06:28 +0000839 {A = sqlite3ExprListAppend(X,Y,0);}
danielk19774adee202004-05-08 08:23:19 +0000840exprlist(A) ::= expritem(X). {A = sqlite3ExprListAppend(0,X,0);}
drh348784e2000-05-29 20:41:49 +0000841expritem(A) ::= expr(X). {A = X;}
842expritem(A) ::= . {A = 0;}
drhcce7d172000-05-31 15:34:51 +0000843
drh382c0242001-10-06 16:33:02 +0000844///////////////////////////// The CREATE INDEX command ///////////////////////
845//
drh4d91a702006-01-04 15:54:36 +0000846cmd ::= CREATE(S) uniqueflag(U) INDEX ifnotexists(NE) nm(X) dbnm(D)
drh77e96d12006-01-30 23:04:51 +0000847 ON nm(Y) LP idxlist(Z) RP(E). {
drhfdd6e852005-12-16 01:06:16 +0000848 sqlite3CreateIndex(pParse, &X, &D, sqlite3SrcListAppend(0,&Y,0), Z, U,
drh4d91a702006-01-04 15:54:36 +0000849 &S, &E, SQLITE_SO_ASC, NE);
drh9cfcf5d2002-01-29 18:41:24 +0000850}
drh717e6402001-09-27 03:22:32 +0000851
852%type uniqueflag {int}
drh74ad7fe2004-10-07 03:06:28 +0000853uniqueflag(A) ::= UNIQUE. {A = OE_Abort;}
854uniqueflag(A) ::= . {A = OE_None;}
drh348784e2000-05-29 20:41:49 +0000855
danielk19770202b292004-06-09 09:55:16 +0000856%type idxlist {ExprList*}
857%destructor idxlist {sqlite3ExprListDelete($$);}
858%type idxlist_opt {ExprList*}
859%destructor idxlist_opt {sqlite3ExprListDelete($$);}
drh348784e2000-05-29 20:41:49 +0000860%type idxitem {Token}
861
drhc2eef3b2002-08-31 18:53:06 +0000862idxlist_opt(A) ::= . {A = 0;}
863idxlist_opt(A) ::= LP idxlist(X) RP. {A = X;}
drhfdd6e852005-12-16 01:06:16 +0000864idxlist(A) ::= idxlist(X) COMMA idxitem(Y) collate(C) sortorder(Z). {
danielk19770202b292004-06-09 09:55:16 +0000865 Expr *p = 0;
866 if( C.n>0 ){
867 p = sqlite3Expr(TK_COLUMN, 0, 0, 0);
drh2646da72005-12-09 20:02:05 +0000868 if( p ) p->pColl = sqlite3LocateCollSeq(pParse, (char*)C.z, C.n);
danielk19770202b292004-06-09 09:55:16 +0000869 }
870 A = sqlite3ExprListAppend(X, p, &Y);
drhfdd6e852005-12-16 01:06:16 +0000871 if( A ) A->a[A->nExpr-1].sortOrder = Z;
danielk19770202b292004-06-09 09:55:16 +0000872}
drhfdd6e852005-12-16 01:06:16 +0000873idxlist(A) ::= idxitem(Y) collate(C) sortorder(Z). {
danielk19770202b292004-06-09 09:55:16 +0000874 Expr *p = 0;
875 if( C.n>0 ){
876 p = sqlite3Expr(TK_COLUMN, 0, 0, 0);
drh2646da72005-12-09 20:02:05 +0000877 if( p ) p->pColl = sqlite3LocateCollSeq(pParse, (char*)C.z, C.n);
danielk19770202b292004-06-09 09:55:16 +0000878 }
879 A = sqlite3ExprListAppend(0, p, &Y);
drhfdd6e852005-12-16 01:06:16 +0000880 if( A ) A->a[A->nExpr-1].sortOrder = Z;
danielk19770202b292004-06-09 09:55:16 +0000881}
882idxitem(A) ::= nm(X). {A = X;}
883
drh348784e2000-05-29 20:41:49 +0000884
drh8aff1012001-12-22 14:49:24 +0000885///////////////////////////// The DROP INDEX command /////////////////////////
drh382c0242001-10-06 16:33:02 +0000886//
drh4d91a702006-01-04 15:54:36 +0000887cmd ::= DROP INDEX ifexists(E) fullname(X). {sqlite3DropIndex(pParse, X, E);}
drh982cef72000-05-30 16:27:03 +0000888
drh382c0242001-10-06 16:33:02 +0000889///////////////////////////// The VACUUM command /////////////////////////////
890//
drh74161702006-02-24 02:53:49 +0000891cmd ::= VACUUM. {sqlite3Vacuum(pParse);}
892cmd ::= VACUUM nm. {sqlite3Vacuum(pParse);}
drhf57b14a2001-09-14 18:54:08 +0000893
drh382c0242001-10-06 16:33:02 +0000894///////////////////////////// The PRAGMA command /////////////////////////////
895//
drh13d70422004-11-13 15:59:14 +0000896%ifndef SQLITE_OMIT_PRAGMA
danielk197791cf71b2004-06-26 06:37:06 +0000897cmd ::= PRAGMA nm(X) dbnm(Z) EQ nm(Y). {sqlite3Pragma(pParse,&X,&Z,&Y,0);}
898cmd ::= PRAGMA nm(X) dbnm(Z) EQ ON(Y). {sqlite3Pragma(pParse,&X,&Z,&Y,0);}
899cmd ::= PRAGMA nm(X) dbnm(Z) EQ plus_num(Y). {sqlite3Pragma(pParse,&X,&Z,&Y,0);}
900cmd ::= PRAGMA nm(X) dbnm(Z) EQ minus_num(Y). {
901 sqlite3Pragma(pParse,&X,&Z,&Y,1);
902}
903cmd ::= PRAGMA nm(X) dbnm(Z) LP nm(Y) RP. {sqlite3Pragma(pParse,&X,&Z,&Y,0);}
drh74ad7fe2004-10-07 03:06:28 +0000904cmd ::= PRAGMA nm(X) dbnm(Z). {sqlite3Pragma(pParse,&X,&Z,0,0);}
drh13d70422004-11-13 15:59:14 +0000905%endif // SQLITE_OMIT_PRAGMA
drhf57b14a2001-09-14 18:54:08 +0000906plus_num(A) ::= plus_opt number(X). {A = X;}
907minus_num(A) ::= MINUS number(X). {A = X;}
drhfd405312005-11-06 04:06:59 +0000908number(A) ::= INTEGER|FLOAT(X). {A = X;}
drhf57b14a2001-09-14 18:54:08 +0000909plus_opt ::= PLUS.
910plus_opt ::= .
danielk1977c3f9bad2002-05-15 08:30:12 +0000911
912//////////////////////////// The CREATE TRIGGER command /////////////////////
drhf0f258b2003-04-21 18:48:45 +0000913
drhb7f91642004-10-31 02:22:47 +0000914%ifndef SQLITE_OMIT_TRIGGER
915
danielk19773df6b252004-05-29 10:23:19 +0000916cmd ::= CREATE trigger_decl(A) BEGIN trigger_cmd_list(S) END(Z). {
drh4b59ab52002-08-24 18:24:51 +0000917 Token all;
918 all.z = A.z;
919 all.n = (Z.z - A.z) + Z.n;
danielk19774adee202004-05-08 08:23:19 +0000920 sqlite3FinishTrigger(pParse, S, &all);
drhf0f258b2003-04-21 18:48:45 +0000921}
922
drh74ad7fe2004-10-07 03:06:28 +0000923trigger_decl(A) ::= temp(T) TRIGGER nm(B) dbnm(Z) trigger_time(C)
924 trigger_event(D)
925 ON fullname(E) foreach_clause(F) when_clause(G). {
926 sqlite3BeginTrigger(pParse, &B, &Z, C, D.a, D.b, E, F, G, T);
danielk19773df6b252004-05-29 10:23:19 +0000927 A = (Z.n==0?B:Z);
danielk1977c3f9bad2002-05-15 08:30:12 +0000928}
929
930%type trigger_time {int}
931trigger_time(A) ::= BEFORE. { A = TK_BEFORE; }
932trigger_time(A) ::= AFTER. { A = TK_AFTER; }
933trigger_time(A) ::= INSTEAD OF. { A = TK_INSTEAD;}
934trigger_time(A) ::= . { A = TK_BEFORE; }
935
drhad3cab52002-05-24 02:04:32 +0000936%type trigger_event {struct TrigEvent}
danielk19774adee202004-05-08 08:23:19 +0000937%destructor trigger_event {sqlite3IdListDelete($$.b);}
drhfd405312005-11-06 04:06:59 +0000938trigger_event(A) ::= DELETE|INSERT(OP). {A.a = @OP; A.b = 0;}
drh74ad7fe2004-10-07 03:06:28 +0000939trigger_event(A) ::= UPDATE(OP). {A.a = @OP; A.b = 0;}
940trigger_event(A) ::= UPDATE OF inscollist(X). {A.a = TK_UPDATE; A.b = X;}
danielk1977c3f9bad2002-05-15 08:30:12 +0000941
942%type foreach_clause {int}
943foreach_clause(A) ::= . { A = TK_ROW; }
944foreach_clause(A) ::= FOR EACH ROW. { A = TK_ROW; }
945foreach_clause(A) ::= FOR EACH STATEMENT. { A = TK_STATEMENT; }
946
drh0bb132b2004-07-20 14:06:51 +0000947%type when_clause {Expr*}
danielk1977dba99bc2006-01-07 14:02:26 +0000948%destructor when_clause {sqlite3ExprDelete($$);}
danielk1977c3f9bad2002-05-15 08:30:12 +0000949when_clause(A) ::= . { A = 0; }
950when_clause(A) ::= WHEN expr(X). { A = X; }
951
drh0bb132b2004-07-20 14:06:51 +0000952%type trigger_cmd_list {TriggerStep*}
danielk19774adee202004-05-08 08:23:19 +0000953%destructor trigger_cmd_list {sqlite3DeleteTriggerStep($$);}
drh187e4c62006-02-27 22:22:27 +0000954trigger_cmd_list(A) ::= trigger_cmd_list(Y) trigger_cmd(X) SEMI. {
955 if( Y ){
956 Y->pLast->pNext = X;
957 }else{
958 Y = X;
959 }
960 Y->pLast = X;
961 A = Y;
drha69d9162003-04-17 22:57:53 +0000962}
danielk1977c3f9bad2002-05-15 08:30:12 +0000963trigger_cmd_list(A) ::= . { A = 0; }
964
drh0bb132b2004-07-20 14:06:51 +0000965%type trigger_cmd {TriggerStep*}
danielk19774adee202004-05-08 08:23:19 +0000966%destructor trigger_cmd {sqlite3DeleteTriggerStep($$);}
danielk1977c3f9bad2002-05-15 08:30:12 +0000967// UPDATE
drh5ad1a6c2002-07-01 12:27:09 +0000968trigger_cmd(A) ::= UPDATE orconf(R) nm(X) SET setlist(Y) where_opt(Z).
danielk19774adee202004-05-08 08:23:19 +0000969 { A = sqlite3TriggerUpdateStep(&X, Y, Z, R); }
danielk1977c3f9bad2002-05-15 08:30:12 +0000970
971// INSERT
drh3054efe2004-02-12 17:28:13 +0000972trigger_cmd(A) ::= insert_cmd(R) INTO nm(X) inscollist_opt(F)
drh74ad7fe2004-10-07 03:06:28 +0000973 VALUES LP itemlist(Y) RP.
974 {A = sqlite3TriggerInsertStep(&X, F, Y, 0, R);}
danielk1977c3f9bad2002-05-15 08:30:12 +0000975
drh3054efe2004-02-12 17:28:13 +0000976trigger_cmd(A) ::= insert_cmd(R) INTO nm(X) inscollist_opt(F) select(S).
danielk19774adee202004-05-08 08:23:19 +0000977 {A = sqlite3TriggerInsertStep(&X, F, 0, S, R);}
danielk1977c3f9bad2002-05-15 08:30:12 +0000978
979// DELETE
drh5ad1a6c2002-07-01 12:27:09 +0000980trigger_cmd(A) ::= DELETE FROM nm(X) where_opt(Y).
danielk19774adee202004-05-08 08:23:19 +0000981 {A = sqlite3TriggerDeleteStep(&X, Y);}
danielk1977c3f9bad2002-05-15 08:30:12 +0000982
983// SELECT
danielk19774adee202004-05-08 08:23:19 +0000984trigger_cmd(A) ::= select(X). {A = sqlite3TriggerSelectStep(X); }
danielk1977c3f9bad2002-05-15 08:30:12 +0000985
danielk19776f349032002-06-11 02:25:40 +0000986// The special RAISE expression that may occur in trigger programs
drh4b59ab52002-08-24 18:24:51 +0000987expr(A) ::= RAISE(X) LP IGNORE RP(Y). {
danielk19774adee202004-05-08 08:23:19 +0000988 A = sqlite3Expr(TK_RAISE, 0, 0, 0);
drh8aa34ae2006-03-13 12:54:09 +0000989 if( A ){
990 A->iColumn = OE_Ignore;
991 sqlite3ExprSpan(A, &X, &Y);
992 }
drh4b59ab52002-08-24 18:24:51 +0000993}
drh74ad7fe2004-10-07 03:06:28 +0000994expr(A) ::= RAISE(X) LP raisetype(T) COMMA nm(Z) RP(Y). {
danielk19774adee202004-05-08 08:23:19 +0000995 A = sqlite3Expr(TK_RAISE, 0, 0, &Z);
drh8aa34ae2006-03-13 12:54:09 +0000996 if( A ) {
997 A->iColumn = T;
998 sqlite3ExprSpan(A, &X, &Y);
999 }
drh4b59ab52002-08-24 18:24:51 +00001000}
drhb7f91642004-10-31 02:22:47 +00001001%endif // !SQLITE_OMIT_TRIGGER
1002
drh74ad7fe2004-10-07 03:06:28 +00001003%type raisetype {int}
1004raisetype(A) ::= ROLLBACK. {A = OE_Rollback;}
1005raisetype(A) ::= ABORT. {A = OE_Abort;}
1006raisetype(A) ::= FAIL. {A = OE_Fail;}
1007
danielk19776f349032002-06-11 02:25:40 +00001008
danielk1977c3f9bad2002-05-15 08:30:12 +00001009//////////////////////// DROP TRIGGER statement //////////////////////////////
drhb7f91642004-10-31 02:22:47 +00001010%ifndef SQLITE_OMIT_TRIGGER
drh74ad7fe2004-10-07 03:06:28 +00001011cmd ::= DROP TRIGGER fullname(X). {
1012 sqlite3DropTrigger(pParse,X);
danielk1977c3f9bad2002-05-15 08:30:12 +00001013}
drhb7f91642004-10-31 02:22:47 +00001014%endif // !SQLITE_OMIT_TRIGGER
drh113088e2003-03-20 01:16:58 +00001015
1016//////////////////////// ATTACH DATABASE file AS name /////////////////////////
danielk1977f744bb52005-12-06 17:19:11 +00001017cmd ::= ATTACH database_kw_opt expr(F) AS expr(D) key_opt(K). {
1018 sqlite3Attach(pParse, F, D, K);
drh1c2d8412003-03-31 00:30:47 +00001019}
danielk1977f744bb52005-12-06 17:19:11 +00001020%type key_opt {Expr *}
danielk1977dba99bc2006-01-07 14:02:26 +00001021%destructor key_opt {sqlite3ExprDelete($$);}
danielk1977f744bb52005-12-06 17:19:11 +00001022key_opt(A) ::= . { A = 0; }
1023key_opt(A) ::= KEY expr(X). { A = X; }
drh113088e2003-03-20 01:16:58 +00001024
1025database_kw_opt ::= DATABASE.
1026database_kw_opt ::= .
1027
1028//////////////////////// DETACH DATABASE name /////////////////////////////////
danielk1977f744bb52005-12-06 17:19:11 +00001029cmd ::= DETACH database_kw_opt expr(D). {
1030 sqlite3Detach(pParse, D);
drh1c2d8412003-03-31 00:30:47 +00001031}
drh4343fea2004-11-05 23:46:15 +00001032
1033////////////////////////// REINDEX collation //////////////////////////////////
1034%ifndef SQLITE_OMIT_REINDEX
1035cmd ::= REINDEX. {sqlite3Reindex(pParse, 0, 0);}
1036cmd ::= REINDEX nm(X) dbnm(Y). {sqlite3Reindex(pParse, &X, &Y);}
1037%endif
danielk19779fd2a9a2004-11-12 13:42:30 +00001038
drh9f18e8a2005-07-08 12:13:04 +00001039/////////////////////////////////// ANALYZE ///////////////////////////////////
1040%ifndef SQLITE_OMIT_ANALYZE
1041cmd ::= ANALYZE. {sqlite3Analyze(pParse, 0, 0);}
1042cmd ::= ANALYZE nm(X) dbnm(Y). {sqlite3Analyze(pParse, &X, &Y);}
1043%endif
1044
danielk19779fd2a9a2004-11-12 13:42:30 +00001045//////////////////////// ALTER TABLE table ... ////////////////////////////////
1046%ifndef SQLITE_OMIT_ALTERTABLE
1047cmd ::= ALTER TABLE fullname(X) RENAME TO nm(Z). {
1048 sqlite3AlterRenameTable(pParse,X,&Z);
1049}
danielk197719a8e7e2005-03-17 05:03:38 +00001050cmd ::= ALTER TABLE add_column_fullname ADD kwcolumn_opt column(Y). {
1051 sqlite3AlterFinishAddColumn(pParse, &Y);
1052}
1053add_column_fullname ::= fullname(X). {
1054 sqlite3AlterBeginAddColumn(pParse, X);
1055}
1056kwcolumn_opt ::= .
1057kwcolumn_opt ::= COLUMNKW.
danielk19779fd2a9a2004-11-12 13:42:30 +00001058%endif