blob: 1df236b0d669162832a72fd7628a9849d8c4e730 [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**
drhcaec2f12003-01-07 02:47:47 +000017** @(#) $Id: parse.y,v 1.86 2003/01/07 02:47:48 drh Exp $
drh348784e2000-05-29 20:41:49 +000018*/
19%token_prefix TK_
20%token_type {Token}
drhf57b14a2001-09-14 18:54:08 +000021%default_type {Token}
drh348784e2000-05-29 20:41:49 +000022%extra_argument {Parse *pParse}
23%syntax_error {
drhc837e702000-06-08 16:26:24 +000024 sqliteSetString(&pParse->zErrMsg,"syntax error",0);
drh348784e2000-05-29 20:41:49 +000025 pParse->sErrToken = TOKEN;
26}
27%name sqliteParser
28%include {
29#include "sqliteInt.h"
30#include "parse.h"
drh9bbca4c2001-11-06 04:00:18 +000031
32/*
drhad3cab52002-05-24 02:04:32 +000033** An instance of this structure holds information about the
34** LIMIT clause of a SELECT statement.
drh9bbca4c2001-11-06 04:00:18 +000035*/
drhad3cab52002-05-24 02:04:32 +000036struct LimitVal {
37 int limit; /* The LIMIT value. -1 if there is no limit */
38 int offset; /* The OFFSET. 0 if there is none */
39};
danielk1977c3f9bad2002-05-15 08:30:12 +000040
41/*
drhad3cab52002-05-24 02:04:32 +000042** An instance of the following structure describes the event of a
43** TRIGGER. "a" is the event type, one of TK_UPDATE, TK_INSERT,
44** TK_DELETE, or TK_INSTEAD. If the event is of the form
45**
46** UPDATE ON (a,b,c)
47**
48** Then the "b" IdList records the list "a,b,c".
danielk1977c3f9bad2002-05-15 08:30:12 +000049*/
drhad3cab52002-05-24 02:04:32 +000050struct TrigEvent { int a; IdList * b; };
drhcaec2f12003-01-07 02:47:47 +000051
52} // end %include
drh348784e2000-05-29 20:41:49 +000053
drh348784e2000-05-29 20:41:49 +000054// These are extra tokens used by the lexer but never seen by the
55// parser. We put them in a rule so that the parser generator will
drh6206d502000-06-19 19:09:08 +000056// add them to the parse.h output file.
drh348784e2000-05-29 20:41:49 +000057//
drhc4a3c772001-04-04 11:48:57 +000058%nonassoc END_OF_FILE ILLEGAL SPACE UNCLOSED_STRING COMMENT FUNCTION
59 COLUMN AGG_FUNCTION.
60
61// Input is zero or more commands.
62input ::= cmdlist.
drh348784e2000-05-29 20:41:49 +000063
64// A list of commands is zero or more commands
65//
66cmdlist ::= ecmd.
drh094b2bb2002-03-13 18:54:07 +000067cmdlist ::= cmdlist ecmd.
68ecmd ::= explain cmd SEMI. {sqliteExec(pParse);}
drh094b2bb2002-03-13 18:54:07 +000069ecmd ::= SEMI.
drhe0bc4042002-06-25 01:09:11 +000070explain ::= EXPLAIN. { sqliteBeginParse(pParse, 1); }
71explain ::= . { sqliteBeginParse(pParse, 0); }
drh348784e2000-05-29 20:41:49 +000072
drh382c0242001-10-06 16:33:02 +000073///////////////////// Begin and end transactions. ////////////////////////////
drhc4a3c772001-04-04 11:48:57 +000074//
drhfa86c412002-02-02 15:01:15 +000075
drh0d65dc02002-02-03 00:56:09 +000076cmd ::= BEGIN trans_opt onconf(R). {sqliteBeginTransaction(pParse,R);}
drhc4a3c772001-04-04 11:48:57 +000077trans_opt ::= .
78trans_opt ::= TRANSACTION.
drh5ad1a6c2002-07-01 12:27:09 +000079trans_opt ::= TRANSACTION nm.
drhc4a3c772001-04-04 11:48:57 +000080cmd ::= COMMIT trans_opt. {sqliteCommitTransaction(pParse);}
81cmd ::= END trans_opt. {sqliteCommitTransaction(pParse);}
82cmd ::= ROLLBACK trans_opt. {sqliteRollbackTransaction(pParse);}
83
drh382c0242001-10-06 16:33:02 +000084///////////////////// The CREATE TABLE statement ////////////////////////////
drh348784e2000-05-29 20:41:49 +000085//
86cmd ::= create_table create_table_args.
drh5ad1a6c2002-07-01 12:27:09 +000087create_table ::= CREATE(X) temp(T) TABLE nm(Y). {
drh969fa7c2002-02-18 18:30:32 +000088 sqliteStartTable(pParse,&X,&Y,T);
89}
drhf57b3392001-10-08 13:22:32 +000090%type temp {int}
drhe0bc4042002-06-25 01:09:11 +000091temp(A) ::= TEMP. {A = pParse->isTemp || !pParse->initFlag;}
92temp(A) ::= . {A = pParse->isTemp;}
drh969fa7c2002-02-18 18:30:32 +000093create_table_args ::= LP columnlist conslist_opt RP(X). {
94 sqliteEndTable(pParse,&X,0);
95}
96create_table_args ::= AS select(S). {
97 sqliteEndTable(pParse,0,S);
98 sqliteSelectDelete(S);
99}
drh348784e2000-05-29 20:41:49 +0000100columnlist ::= columnlist COMMA column.
101columnlist ::= column.
102
103// About the only information used for a column is the name of the
104// column. The type is always just "text". But the code will accept
105// an elaborate typename. Perhaps someday we'll do something with it.
106//
107column ::= columnid type carglist.
drh5ad1a6c2002-07-01 12:27:09 +0000108columnid ::= nm(X). {sqliteAddColumn(pParse,&X);}
drhc4a3c772001-04-04 11:48:57 +0000109
110// An IDENTIFIER can be a generic identifier, or one of several
111// keywords. Any non-standard keyword can also be an identifier.
drhc4a3c772001-04-04 11:48:57 +0000112//
drh982cef72000-05-30 16:27:03 +0000113%type id {Token}
drhf18543c2002-03-30 15:26:50 +0000114id(A) ::= ID(X). {A = X;}
drh0bd1f4e2002-06-06 18:54:39 +0000115
drh34e33bb2002-06-06 19:04:16 +0000116// The following directive causes tokens ABORT, AFTER, ASC, etc. to
117// fallback to ID if they will not parse as their original value.
118// This obviates the need for the "id" nonterminal.
119//
drh0bd1f4e2002-06-06 18:54:39 +0000120%fallback ID
drhf04d5082002-08-18 22:41:22 +0000121 ABORT AFTER ASC BEFORE BEGIN CASCADE CLUSTER CONFLICT
drh0bd1f4e2002-06-06 18:54:39 +0000122 COPY DEFERRED DELIMITERS DESC EACH END EXPLAIN FAIL FOR
drh5ad1a6c2002-07-01 12:27:09 +0000123 IGNORE IMMEDIATE INITIALLY INSTEAD MATCH KEY
124 OF OFFSET PRAGMA RAISE REPLACE RESTRICT ROW STATEMENT
drh0bd1f4e2002-06-06 18:54:39 +0000125 TEMP TRIGGER VACUUM VIEW.
drhc4a3c772001-04-04 11:48:57 +0000126
127// And "ids" is an identifer-or-string.
128//
129%type ids {Token}
drh5ad1a6c2002-07-01 12:27:09 +0000130ids(A) ::= ID(X). {A = X;}
drhc4a3c772001-04-04 11:48:57 +0000131ids(A) ::= STRING(X). {A = X;}
132
drh5ad1a6c2002-07-01 12:27:09 +0000133// The name of a column or table can be any of the following:
134//
135%type nm {Token}
136nm(A) ::= ID(X). {A = X;}
137nm(A) ::= STRING(X). {A = X;}
138nm(A) ::= JOIN_KW(X). {A = X;}
139
drh382c0242001-10-06 16:33:02 +0000140type ::= .
141type ::= typename(X). {sqliteAddColumnType(pParse,&X,&X);}
142type ::= typename(X) LP signed RP(Y). {sqliteAddColumnType(pParse,&X,&Y);}
143type ::= typename(X) LP signed COMMA signed RP(Y).
144 {sqliteAddColumnType(pParse,&X,&Y);}
145%type typename {Token}
146typename(A) ::= ids(X). {A = X;}
147typename(A) ::= typename(X) ids. {A = X;}
drh348784e2000-05-29 20:41:49 +0000148signed ::= INTEGER.
149signed ::= PLUS INTEGER.
150signed ::= MINUS INTEGER.
151carglist ::= carglist carg.
152carglist ::= .
drh5ad1a6c2002-07-01 12:27:09 +0000153carg ::= CONSTRAINT nm ccons.
drh348784e2000-05-29 20:41:49 +0000154carg ::= ccons.
drh7020f652000-06-03 18:06:52 +0000155carg ::= DEFAULT STRING(X). {sqliteAddDefaultValue(pParse,&X,0);}
156carg ::= DEFAULT ID(X). {sqliteAddDefaultValue(pParse,&X,0);}
157carg ::= DEFAULT INTEGER(X). {sqliteAddDefaultValue(pParse,&X,0);}
158carg ::= DEFAULT PLUS INTEGER(X). {sqliteAddDefaultValue(pParse,&X,0);}
159carg ::= DEFAULT MINUS INTEGER(X). {sqliteAddDefaultValue(pParse,&X,1);}
160carg ::= DEFAULT FLOAT(X). {sqliteAddDefaultValue(pParse,&X,0);}
161carg ::= DEFAULT PLUS FLOAT(X). {sqliteAddDefaultValue(pParse,&X,0);}
162carg ::= DEFAULT MINUS FLOAT(X). {sqliteAddDefaultValue(pParse,&X,1);}
163carg ::= DEFAULT NULL.
drh348784e2000-05-29 20:41:49 +0000164
drh382c0242001-10-06 16:33:02 +0000165// In addition to the type name, we also care about the primary key and
166// UNIQUE constraints.
drh348784e2000-05-29 20:41:49 +0000167//
drh0d316a42002-08-11 20:10:47 +0000168ccons ::= NULL onconf.
drh9cfcf5d2002-01-29 18:41:24 +0000169ccons ::= NOT NULL onconf(R). {sqliteAddNotNull(pParse, R);}
170ccons ::= PRIMARY KEY sortorder onconf(R). {sqliteAddPrimaryKey(pParse,0,R);}
171ccons ::= UNIQUE onconf(R). {sqliteCreateIndex(pParse,0,0,0,R,0,0);}
172ccons ::= CHECK LP expr RP onconf.
drhc2eef3b2002-08-31 18:53:06 +0000173ccons ::= REFERENCES nm(T) idxlist_opt(TA) refargs(R).
174 {sqliteCreateForeignKey(pParse,0,&T,TA,R);}
175ccons ::= defer_subclause(D). {sqliteDeferForeignKey(pParse,D);}
drh8e2ca022002-06-17 17:07:19 +0000176ccons ::= COLLATE id(C). {
177 sqliteAddCollateType(pParse, sqliteCollateType(pParse, &C));
178}
drh04738cb2002-06-02 18:19:00 +0000179
drhc2eef3b2002-08-31 18:53:06 +0000180// The next group of rules parses the arguments to a REFERENCES clause
181// that determine if the referential integrity checking is deferred or
182// or immediate and which determine what action to take if a ref-integ
183// check fails.
drh04738cb2002-06-02 18:19:00 +0000184//
drhc2eef3b2002-08-31 18:53:06 +0000185%type refargs {int}
186refargs(A) ::= . { A = OE_Restrict * 0x010101; }
187refargs(A) ::= refargs(X) refarg(Y). { A = (X & Y.mask) | Y.value; }
188%type refarg {struct {int value; int mask;}}
189refarg(A) ::= MATCH nm. { A.value = 0; A.mask = 0x000000; }
190refarg(A) ::= ON DELETE refact(X). { A.value = X; A.mask = 0x0000ff; }
191refarg(A) ::= ON UPDATE refact(X). { A.value = X<<8; A.mask = 0x00ff00; }
192refarg(A) ::= ON INSERT refact(X). { A.value = X<<16; A.mask = 0xff0000; }
193%type refact {int}
194refact(A) ::= SET NULL. { A = OE_SetNull; }
195refact(A) ::= SET DEFAULT. { A = OE_SetDflt; }
196refact(A) ::= CASCADE. { A = OE_Cascade; }
197refact(A) ::= RESTRICT. { A = OE_Restrict; }
198%type defer_subclause {int}
199defer_subclause(A) ::= NOT DEFERRABLE init_deferred_pred_opt(X). {A = X;}
200defer_subclause(A) ::= DEFERRABLE init_deferred_pred_opt(X). {A = X;}
201%type init_deferred_pred_opt {int}
202init_deferred_pred_opt(A) ::= . {A = 0;}
203init_deferred_pred_opt(A) ::= INITIALLY DEFERRED. {A = 1;}
204init_deferred_pred_opt(A) ::= INITIALLY IMMEDIATE. {A = 0;}
drh348784e2000-05-29 20:41:49 +0000205
206// For the time being, the only constraint we care about is the primary
drh382c0242001-10-06 16:33:02 +0000207// key and UNIQUE. Both create indices.
drh348784e2000-05-29 20:41:49 +0000208//
209conslist_opt ::= .
210conslist_opt ::= COMMA conslist.
211conslist ::= conslist COMMA tcons.
drha2e1bb52001-01-04 14:20:18 +0000212conslist ::= conslist tcons.
drh348784e2000-05-29 20:41:49 +0000213conslist ::= tcons.
drh5ad1a6c2002-07-01 12:27:09 +0000214tcons ::= CONSTRAINT nm.
drh9cfcf5d2002-01-29 18:41:24 +0000215tcons ::= PRIMARY KEY LP idxlist(X) RP onconf(R).
216 {sqliteAddPrimaryKey(pParse,X,R);}
217tcons ::= UNIQUE LP idxlist(X) RP onconf(R).
218 {sqliteCreateIndex(pParse,0,0,X,R,0,0);}
219tcons ::= CHECK expr onconf.
drhc2eef3b2002-08-31 18:53:06 +0000220tcons ::= FOREIGN KEY LP idxlist(FA) RP
221 REFERENCES nm(T) idxlist_opt(TA) refargs(R) defer_subclause_opt(D). {
222 sqliteCreateForeignKey(pParse, FA, &T, TA, R);
223 sqliteDeferForeignKey(pParse, D);
224}
225%type defer_subclause_opt {int}
226defer_subclause_opt(A) ::= . {A = 0;}
227defer_subclause_opt(A) ::= defer_subclause(X). {A = X;}
drh9cfcf5d2002-01-29 18:41:24 +0000228
229// The following is a non-standard extension that allows us to declare the
230// default behavior when there is a constraint conflict.
231//
232%type onconf {int}
drh1c928532002-01-31 15:54:21 +0000233%type orconf {int}
234%type resolvetype {int}
235onconf(A) ::= . { A = OE_Default; }
236onconf(A) ::= ON CONFLICT resolvetype(X). { A = X; }
237orconf(A) ::= . { A = OE_Default; }
238orconf(A) ::= OR resolvetype(X). { A = X; }
239resolvetype(A) ::= ROLLBACK. { A = OE_Rollback; }
240resolvetype(A) ::= ABORT. { A = OE_Abort; }
241resolvetype(A) ::= FAIL. { A = OE_Fail; }
242resolvetype(A) ::= IGNORE. { A = OE_Ignore; }
243resolvetype(A) ::= REPLACE. { A = OE_Replace; }
drh348784e2000-05-29 20:41:49 +0000244
drh382c0242001-10-06 16:33:02 +0000245////////////////////////// The DROP TABLE /////////////////////////////////////
drh348784e2000-05-29 20:41:49 +0000246//
drh5ad1a6c2002-07-01 12:27:09 +0000247cmd ::= DROP TABLE nm(X). {sqliteDropTable(pParse,&X,0);}
drh348784e2000-05-29 20:41:49 +0000248
drha76b5df2002-02-23 02:32:10 +0000249///////////////////// The CREATE VIEW statement /////////////////////////////
250//
drh6276c1c2002-07-08 22:03:32 +0000251cmd ::= CREATE(X) temp(T) VIEW nm(Y) AS select(S). {
252 sqliteCreateView(pParse, &X, &Y, S, T);
drha76b5df2002-02-23 02:32:10 +0000253}
drh5ad1a6c2002-07-01 12:27:09 +0000254cmd ::= DROP VIEW nm(X). {
drh4ff6dfa2002-03-03 23:06:00 +0000255 sqliteDropTable(pParse, &X, 1);
drha76b5df2002-02-23 02:32:10 +0000256}
257
drh382c0242001-10-06 16:33:02 +0000258//////////////////////// The SELECT statement /////////////////////////////////
drh348784e2000-05-29 20:41:49 +0000259//
drh9bb61fe2000-06-05 16:01:39 +0000260cmd ::= select(X). {
drh832508b2002-03-02 17:04:07 +0000261 sqliteSelect(pParse, X, SRT_Callback, 0, 0, 0, 0);
drh9bb61fe2000-06-05 16:01:39 +0000262 sqliteSelectDelete(X);
263}
drhefb72512000-05-31 20:00:52 +0000264
drh9bb61fe2000-06-05 16:01:39 +0000265%type select {Select*}
266%destructor select {sqliteSelectDelete($$);}
drh82c3d632000-06-06 21:56:07 +0000267%type oneselect {Select*}
268%destructor oneselect {sqliteSelectDelete($$);}
drh9bb61fe2000-06-05 16:01:39 +0000269
drh82c3d632000-06-06 21:56:07 +0000270select(A) ::= oneselect(X). {A = X;}
drh0a36c572002-02-18 22:49:59 +0000271select(A) ::= select(X) multiselect_op(Y) oneselect(Z). {
drhdaffd0e2001-04-11 14:28:42 +0000272 if( Z ){
drh82c3d632000-06-06 21:56:07 +0000273 Z->op = Y;
274 Z->pPrior = X;
drhdaffd0e2001-04-11 14:28:42 +0000275 }
276 A = Z;
drh82c3d632000-06-06 21:56:07 +0000277}
drh0a36c572002-02-18 22:49:59 +0000278%type multiselect_op {int}
279multiselect_op(A) ::= UNION. {A = TK_UNION;}
280multiselect_op(A) ::= UNION ALL. {A = TK_ALL;}
281multiselect_op(A) ::= INTERSECT. {A = TK_INTERSECT;}
282multiselect_op(A) ::= EXCEPT. {A = TK_EXCEPT;}
drh82c3d632000-06-06 21:56:07 +0000283oneselect(A) ::= SELECT distinct(D) selcollist(W) from(X) where_opt(Y)
drh9bbca4c2001-11-06 04:00:18 +0000284 groupby_opt(P) having_opt(Q) orderby_opt(Z) limit_opt(L). {
drhad3cab52002-05-24 02:04:32 +0000285 A = sqliteSelectNew(W,X,Y,P,Q,Z,D,L.limit,L.offset);
drh9bb61fe2000-06-05 16:01:39 +0000286}
287
288// The "distinct" nonterminal is true (1) if the DISTINCT keyword is
289// present and false (0) if it is not.
290//
drhefb72512000-05-31 20:00:52 +0000291%type distinct {int}
drhefb72512000-05-31 20:00:52 +0000292distinct(A) ::= DISTINCT. {A = 1;}
drhfef52082000-06-06 01:50:43 +0000293distinct(A) ::= ALL. {A = 0;}
drhefb72512000-05-31 20:00:52 +0000294distinct(A) ::= . {A = 0;}
drh348784e2000-05-29 20:41:49 +0000295
drh9bb61fe2000-06-05 16:01:39 +0000296// selcollist is a list of expressions that are to become the return
drh7c917d12001-12-16 20:05:05 +0000297// values of the SELECT statement. The "*" in statements like
298// "SELECT * FROM ..." is encoded as a special expression with an
299// opcode of TK_ALL.
drh9bb61fe2000-06-05 16:01:39 +0000300//
drh348784e2000-05-29 20:41:49 +0000301%type selcollist {ExprList*}
302%destructor selcollist {sqliteExprListDelete($$);}
303%type sclp {ExprList*}
304%destructor sclp {sqliteExprListDelete($$);}
drh348784e2000-05-29 20:41:49 +0000305sclp(A) ::= selcollist(X) COMMA. {A = X;}
306sclp(A) ::= . {A = 0;}
drh01f3f252002-05-24 16:14:15 +0000307selcollist(A) ::= sclp(P) expr(X) as(Y). {
308 A = sqliteExprListAppend(P,X,Y.n?&Y:0);
309}
drh7c917d12001-12-16 20:05:05 +0000310selcollist(A) ::= sclp(P) STAR. {
311 A = sqliteExprListAppend(P, sqliteExpr(TK_ALL, 0, 0, 0), 0);
312}
drh5ad1a6c2002-07-01 12:27:09 +0000313selcollist(A) ::= sclp(P) nm(X) DOT STAR. {
drh54473222002-04-04 02:10:55 +0000314 Expr *pRight = sqliteExpr(TK_ALL, 0, 0, 0);
315 Expr *pLeft = sqliteExpr(TK_ID, 0, 0, &X);
316 A = sqliteExprListAppend(P, sqliteExpr(TK_DOT, pLeft, pRight, 0), 0);
317}
drh01f3f252002-05-24 16:14:15 +0000318
319// An option "AS <id>" phrase that can follow one of the expressions that
320// define the result set, or one of the tables in the FROM clause.
321//
322%type as {Token}
drh5ad1a6c2002-07-01 12:27:09 +0000323as(X) ::= AS nm(Y). { X = Y; }
324as(X) ::= ids(Y). { X = Y; }
325as(X) ::= . { X.n = 0; }
drh9bb61fe2000-06-05 16:01:39 +0000326
drh348784e2000-05-29 20:41:49 +0000327
drhad3cab52002-05-24 02:04:32 +0000328%type seltablist {SrcList*}
329%destructor seltablist {sqliteSrcListDelete($$);}
330%type stl_prefix {SrcList*}
331%destructor stl_prefix {sqliteSrcListDelete($$);}
332%type from {SrcList*}
333%destructor from {sqliteSrcListDelete($$);}
drh348784e2000-05-29 20:41:49 +0000334
drh01f3f252002-05-24 16:14:15 +0000335// A complete FROM clause.
336//
drhbf3a4fa2002-04-06 13:57:42 +0000337from(A) ::= . {A = sqliteMalloc(sizeof(*A));}
drh348784e2000-05-29 20:41:49 +0000338from(A) ::= FROM seltablist(X). {A = X;}
drh01f3f252002-05-24 16:14:15 +0000339
340// "seltablist" is a "Select Table List" - the content of the FROM clause
341// in a SELECT statement. "stl_prefix" is a prefix of this list.
342//
343stl_prefix(A) ::= seltablist(X) joinop(Y). {
344 A = X;
345 if( A && A->nSrc>0 ) A->a[A->nSrc-1].jointype = Y;
346}
drh348784e2000-05-29 20:41:49 +0000347stl_prefix(A) ::= . {A = 0;}
drh5ad1a6c2002-07-01 12:27:09 +0000348seltablist(A) ::= stl_prefix(X) nm(Y) as(Z) on_opt(N) using_opt(U). {
drhad3cab52002-05-24 02:04:32 +0000349 A = sqliteSrcListAppend(X,&Y);
drh01f3f252002-05-24 16:14:15 +0000350 if( Z.n ) sqliteSrcListAddAlias(A,&Z);
351 if( N ){
352 if( A && A->nSrc>1 ){ A->a[A->nSrc-2].pOn = N; }
353 else { sqliteExprDelete(N); }
354 }
355 if( U ){
356 if( A && A->nSrc>1 ){ A->a[A->nSrc-2].pUsing = U; }
357 else { sqliteIdListDelete(U); }
358 }
drhc4a3c772001-04-04 11:48:57 +0000359}
drh01f3f252002-05-24 16:14:15 +0000360seltablist(A) ::= stl_prefix(X) LP select(S) RP as(Z) on_opt(N) using_opt(U). {
drhad3cab52002-05-24 02:04:32 +0000361 A = sqliteSrcListAppend(X,0);
362 A->a[A->nSrc-1].pSelect = S;
drh01f3f252002-05-24 16:14:15 +0000363 if( Z.n ) sqliteSrcListAddAlias(A,&Z);
364 if( N ){
365 if( A && A->nSrc>1 ){ A->a[A->nSrc-2].pOn = N; }
366 else { sqliteExprDelete(N); }
drhd5feede2002-05-08 21:46:14 +0000367 }
drh01f3f252002-05-24 16:14:15 +0000368 if( U ){
369 if( A && A->nSrc>1 ){ A->a[A->nSrc-2].pUsing = U; }
370 else { sqliteIdListDelete(U); }
371 }
drh22f70c32002-02-18 01:17:00 +0000372}
drh348784e2000-05-29 20:41:49 +0000373
drh01f3f252002-05-24 16:14:15 +0000374%type joinop {int}
375%type joinop2 {int}
376joinop(X) ::= COMMA. { X = JT_INNER; }
377joinop(X) ::= JOIN. { X = JT_INNER; }
drh5ad1a6c2002-07-01 12:27:09 +0000378joinop(X) ::= JOIN_KW(A) JOIN. { X = sqliteJoinType(pParse,&A,0,0); }
379joinop(X) ::= JOIN_KW(A) nm(B) JOIN. { X = sqliteJoinType(pParse,&A,&B,0); }
380joinop(X) ::= JOIN_KW(A) nm(B) nm(C) JOIN.
381 { X = sqliteJoinType(pParse,&A,&B,&C); }
drh01f3f252002-05-24 16:14:15 +0000382
383%type on_opt {Expr*}
384%destructor on_opt {sqliteExprDelete($$);}
385on_opt(N) ::= ON expr(E). {N = E;}
386on_opt(N) ::= . {N = 0;}
387
388%type using_opt {IdList*}
389%destructor using_opt {sqliteIdListDelete($$);}
390using_opt(U) ::= USING LP idxlist(L) RP. {U = L;}
391using_opt(U) ::= . {U = 0;}
392
393
drh348784e2000-05-29 20:41:49 +0000394%type orderby_opt {ExprList*}
395%destructor orderby_opt {sqliteExprListDelete($$);}
396%type sortlist {ExprList*}
397%destructor sortlist {sqliteExprListDelete($$);}
398%type sortitem {Expr*}
399%destructor sortitem {sqliteExprDelete($$);}
400
401orderby_opt(A) ::= . {A = 0;}
402orderby_opt(A) ::= ORDER BY sortlist(X). {A = X;}
drh8e2ca022002-06-17 17:07:19 +0000403sortlist(A) ::= sortlist(X) COMMA sortitem(Y) collate(C) sortorder(Z). {
drh9bb61fe2000-06-05 16:01:39 +0000404 A = sqliteExprListAppend(X,Y,0);
drh8e2ca022002-06-17 17:07:19 +0000405 if( A ) A->a[A->nExpr-1].sortOrder = C+Z;
drh9bb61fe2000-06-05 16:01:39 +0000406}
drh38640e12002-07-05 21:42:36 +0000407sortlist(A) ::= sortitem(Y) collate(C) sortorder(Z). {
drh9bb61fe2000-06-05 16:01:39 +0000408 A = sqliteExprListAppend(0,Y,0);
drh38640e12002-07-05 21:42:36 +0000409 if( A ) A->a[0].sortOrder = C+Z;
drh9bb61fe2000-06-05 16:01:39 +0000410}
drhda9d6c42000-05-31 18:20:14 +0000411sortitem(A) ::= expr(X). {A = X;}
drh348784e2000-05-29 20:41:49 +0000412
413%type sortorder {int}
drh8e2ca022002-06-17 17:07:19 +0000414%type collate {int}
drh348784e2000-05-29 20:41:49 +0000415
drh8e2ca022002-06-17 17:07:19 +0000416sortorder(A) ::= ASC. {A = SQLITE_SO_ASC;}
417sortorder(A) ::= DESC. {A = SQLITE_SO_DESC;}
418sortorder(A) ::= . {A = SQLITE_SO_ASC;}
419collate(C) ::= . {C = SQLITE_SO_UNK;}
420collate(C) ::= COLLATE id(X). {C = sqliteCollateType(pParse, &X);}
drh348784e2000-05-29 20:41:49 +0000421
drh22827922000-06-06 17:27:05 +0000422%type groupby_opt {ExprList*}
423%destructor groupby_opt {sqliteExprListDelete($$);}
drh6206d502000-06-19 19:09:08 +0000424groupby_opt(A) ::= . {A = 0;}
drh22827922000-06-06 17:27:05 +0000425groupby_opt(A) ::= GROUP BY exprlist(X). {A = X;}
426
427%type having_opt {Expr*}
428%destructor having_opt {sqliteExprDelete($$);}
drh6206d502000-06-19 19:09:08 +0000429having_opt(A) ::= . {A = 0;}
drh22827922000-06-06 17:27:05 +0000430having_opt(A) ::= HAVING expr(X). {A = X;}
431
drhad3cab52002-05-24 02:04:32 +0000432%type limit_opt {struct LimitVal}
433limit_opt(A) ::= . {A.limit = -1; A.offset = 0;}
434limit_opt(A) ::= LIMIT INTEGER(X). {A.limit = atoi(X.z); A.offset = 0;}
drh9bbca4c2001-11-06 04:00:18 +0000435limit_opt(A) ::= LIMIT INTEGER(X) limit_sep INTEGER(Y).
drhad3cab52002-05-24 02:04:32 +0000436 {A.limit = atoi(X.z); A.offset = atoi(Y.z);}
drh9bbca4c2001-11-06 04:00:18 +0000437limit_sep ::= OFFSET.
438limit_sep ::= COMMA.
439
drh382c0242001-10-06 16:33:02 +0000440/////////////////////////// The DELETE statement /////////////////////////////
441//
drh5ad1a6c2002-07-01 12:27:09 +0000442cmd ::= DELETE FROM nm(X) where_opt(Y).
drh348784e2000-05-29 20:41:49 +0000443 {sqliteDeleteFrom(pParse, &X, Y);}
444
445%type where_opt {Expr*}
446%destructor where_opt {sqliteExprDelete($$);}
447
448where_opt(A) ::= . {A = 0;}
449where_opt(A) ::= WHERE expr(X). {A = X;}
450
451%type setlist {ExprList*}
452%destructor setlist {sqliteExprListDelete($$);}
453
drh382c0242001-10-06 16:33:02 +0000454////////////////////////// The UPDATE command ////////////////////////////////
455//
drh5ad1a6c2002-07-01 12:27:09 +0000456cmd ::= UPDATE orconf(R) nm(X) SET setlist(Y) where_opt(Z).
drh9cfcf5d2002-01-29 18:41:24 +0000457 {sqliteUpdate(pParse,&X,Y,Z,R);}
drh348784e2000-05-29 20:41:49 +0000458
drh5ad1a6c2002-07-01 12:27:09 +0000459setlist(A) ::= setlist(Z) COMMA nm(X) EQ expr(Y).
drh348784e2000-05-29 20:41:49 +0000460 {A = sqliteExprListAppend(Z,Y,&X);}
drh5ad1a6c2002-07-01 12:27:09 +0000461setlist(A) ::= nm(X) EQ expr(Y). {A = sqliteExprListAppend(0,Y,&X);}
drh348784e2000-05-29 20:41:49 +0000462
drh382c0242001-10-06 16:33:02 +0000463////////////////////////// The INSERT command /////////////////////////////////
464//
drh5ad1a6c2002-07-01 12:27:09 +0000465cmd ::= insert_cmd(R) INTO nm(X) inscollist_opt(F) VALUES LP itemlist(Y) RP.
drh9cfcf5d2002-01-29 18:41:24 +0000466 {sqliteInsert(pParse, &X, Y, 0, F, R);}
drh5ad1a6c2002-07-01 12:27:09 +0000467cmd ::= insert_cmd(R) INTO nm(X) inscollist_opt(F) select(S).
drh9cfcf5d2002-01-29 18:41:24 +0000468 {sqliteInsert(pParse, &X, 0, S, F, R);}
drh348784e2000-05-29 20:41:49 +0000469
drhfa86c412002-02-02 15:01:15 +0000470%type insert_cmd {int}
471insert_cmd(A) ::= INSERT orconf(R). {A = R;}
472insert_cmd(A) ::= REPLACE. {A = OE_Replace;}
473
drh348784e2000-05-29 20:41:49 +0000474
475%type itemlist {ExprList*}
476%destructor itemlist {sqliteExprListDelete($$);}
drh348784e2000-05-29 20:41:49 +0000477
drhe64e7b22002-02-18 13:56:36 +0000478itemlist(A) ::= itemlist(X) COMMA expr(Y). {A = sqliteExprListAppend(X,Y,0);}
479itemlist(A) ::= expr(X). {A = sqliteExprListAppend(0,X,0);}
drh348784e2000-05-29 20:41:49 +0000480
drh967e8b72000-06-21 13:59:10 +0000481%type inscollist_opt {IdList*}
482%destructor inscollist_opt {sqliteIdListDelete($$);}
483%type inscollist {IdList*}
484%destructor inscollist {sqliteIdListDelete($$);}
drh348784e2000-05-29 20:41:49 +0000485
drhc4a3c772001-04-04 11:48:57 +0000486inscollist_opt(A) ::= . {A = 0;}
487inscollist_opt(A) ::= LP inscollist(X) RP. {A = X;}
drh5ad1a6c2002-07-01 12:27:09 +0000488inscollist(A) ::= inscollist(X) COMMA nm(Y). {A = sqliteIdListAppend(X,&Y);}
489inscollist(A) ::= nm(Y). {A = sqliteIdListAppend(0,&Y);}
drh348784e2000-05-29 20:41:49 +0000490
drh382c0242001-10-06 16:33:02 +0000491/////////////////////////// Expression Processing /////////////////////////////
492//
drh348784e2000-05-29 20:41:49 +0000493%left OR.
494%left AND.
drh8be51132000-06-03 19:19:41 +0000495%right NOT.
drhfef52082000-06-06 01:50:43 +0000496%left EQ NE ISNULL NOTNULL IS LIKE GLOB BETWEEN IN.
drh348784e2000-05-29 20:41:49 +0000497%left GT GE LT LE.
drh81a20f22001-10-12 17:30:04 +0000498%left BITAND BITOR LSHIFT RSHIFT.
drh348784e2000-05-29 20:41:49 +0000499%left PLUS MINUS.
drhbf4133c2001-10-13 02:59:08 +0000500%left STAR SLASH REM.
drh00400772000-06-16 20:51:26 +0000501%left CONCAT.
drh4b59ab52002-08-24 18:24:51 +0000502%right UMINUS UPLUS BITNOT.
drh1f162302002-10-27 19:35:33 +0000503%right ORACLE_OUTER_JOIN.
drh348784e2000-05-29 20:41:49 +0000504
505%type expr {Expr*}
506%destructor expr {sqliteExprDelete($$);}
507
drh6977fea2002-10-22 23:38:04 +0000508expr(A) ::= LP(B) expr(X) RP(E). {A = X; sqliteExprSpan(A,&B,&E); }
drhe1b6a5b2000-07-29 13:06:59 +0000509expr(A) ::= NULL(X). {A = sqliteExpr(TK_NULL, 0, 0, &X);}
drh5ad1a6c2002-07-01 12:27:09 +0000510expr(A) ::= ID(X). {A = sqliteExpr(TK_ID, 0, 0, &X);}
511expr(A) ::= JOIN_KW(X). {A = sqliteExpr(TK_ID, 0, 0, &X);}
512expr(A) ::= nm(X) DOT nm(Y). {
drhe1b6a5b2000-07-29 13:06:59 +0000513 Expr *temp1 = sqliteExpr(TK_ID, 0, 0, &X);
514 Expr *temp2 = sqliteExpr(TK_ID, 0, 0, &Y);
515 A = sqliteExpr(TK_DOT, temp1, temp2, 0);
516}
drh1f162302002-10-27 19:35:33 +0000517expr(A) ::= expr(B) ORACLE_OUTER_JOIN.
518 {A = B; ExprSetProperty(A,EP_Oracle8Join);}
drh348784e2000-05-29 20:41:49 +0000519expr(A) ::= INTEGER(X). {A = sqliteExpr(TK_INTEGER, 0, 0, &X);}
520expr(A) ::= FLOAT(X). {A = sqliteExpr(TK_FLOAT, 0, 0, &X);}
521expr(A) ::= STRING(X). {A = sqliteExpr(TK_STRING, 0, 0, &X);}
drhe1b6a5b2000-07-29 13:06:59 +0000522expr(A) ::= ID(X) LP exprlist(Y) RP(E). {
523 A = sqliteExprFunction(Y, &X);
524 sqliteExprSpan(A,&X,&E);
525}
526expr(A) ::= ID(X) LP STAR RP(E). {
527 A = sqliteExprFunction(0, &X);
528 sqliteExprSpan(A,&X,&E);
529}
drh348784e2000-05-29 20:41:49 +0000530expr(A) ::= expr(X) AND expr(Y). {A = sqliteExpr(TK_AND, X, Y, 0);}
531expr(A) ::= expr(X) OR expr(Y). {A = sqliteExpr(TK_OR, X, Y, 0);}
532expr(A) ::= expr(X) LT expr(Y). {A = sqliteExpr(TK_LT, X, Y, 0);}
533expr(A) ::= expr(X) GT expr(Y). {A = sqliteExpr(TK_GT, X, Y, 0);}
534expr(A) ::= expr(X) LE expr(Y). {A = sqliteExpr(TK_LE, X, Y, 0);}
535expr(A) ::= expr(X) GE expr(Y). {A = sqliteExpr(TK_GE, X, Y, 0);}
536expr(A) ::= expr(X) NE expr(Y). {A = sqliteExpr(TK_NE, X, Y, 0);}
537expr(A) ::= expr(X) EQ expr(Y). {A = sqliteExpr(TK_EQ, X, Y, 0);}
drh81a20f22001-10-12 17:30:04 +0000538expr(A) ::= expr(X) BITAND expr(Y). {A = sqliteExpr(TK_BITAND, X, Y, 0);}
539expr(A) ::= expr(X) BITOR expr(Y). {A = sqliteExpr(TK_BITOR, X, Y, 0);}
540expr(A) ::= expr(X) LSHIFT expr(Y). {A = sqliteExpr(TK_LSHIFT, X, Y, 0);}
541expr(A) ::= expr(X) RSHIFT expr(Y). {A = sqliteExpr(TK_RSHIFT, X, Y, 0);}
drh0ac65892002-04-20 14:24:41 +0000542expr(A) ::= expr(X) likeop(OP) expr(Y). [LIKE] {
543 ExprList *pList = sqliteExprListAppend(0, Y, 0);
544 pList = sqliteExprListAppend(pList, X, 0);
drh4b59ab52002-08-24 18:24:51 +0000545 A = sqliteExprFunction(pList, 0);
546 if( A ) A->op = OP;
drh6977fea2002-10-22 23:38:04 +0000547 sqliteExprSpan(A, &X->span, &Y->span);
drh0ac65892002-04-20 14:24:41 +0000548}
549expr(A) ::= expr(X) NOT likeop(OP) expr(Y). [LIKE] {
550 ExprList *pList = sqliteExprListAppend(0, Y, 0);
551 pList = sqliteExprListAppend(pList, X, 0);
drh4b59ab52002-08-24 18:24:51 +0000552 A = sqliteExprFunction(pList, 0);
553 if( A ) A->op = OP;
drh4794b982000-06-06 13:54:14 +0000554 A = sqliteExpr(TK_NOT, A, 0, 0);
drh6977fea2002-10-22 23:38:04 +0000555 sqliteExprSpan(A,&X->span,&Y->span);
drh4794b982000-06-06 13:54:14 +0000556}
drh4b59ab52002-08-24 18:24:51 +0000557%type likeop {int}
558likeop(A) ::= LIKE. {A = TK_LIKE;}
559likeop(A) ::= GLOB. {A = TK_GLOB;}
drh348784e2000-05-29 20:41:49 +0000560expr(A) ::= expr(X) PLUS expr(Y). {A = sqliteExpr(TK_PLUS, X, Y, 0);}
561expr(A) ::= expr(X) MINUS expr(Y). {A = sqliteExpr(TK_MINUS, X, Y, 0);}
562expr(A) ::= expr(X) STAR expr(Y). {A = sqliteExpr(TK_STAR, X, Y, 0);}
563expr(A) ::= expr(X) SLASH expr(Y). {A = sqliteExpr(TK_SLASH, X, Y, 0);}
drhbf4133c2001-10-13 02:59:08 +0000564expr(A) ::= expr(X) REM expr(Y). {A = sqliteExpr(TK_REM, X, Y, 0);}
drh00400772000-06-16 20:51:26 +0000565expr(A) ::= expr(X) CONCAT expr(Y). {A = sqliteExpr(TK_CONCAT, X, Y, 0);}
drhe1b6a5b2000-07-29 13:06:59 +0000566expr(A) ::= expr(X) ISNULL(E). {
567 A = sqliteExpr(TK_ISNULL, X, 0, 0);
drh6977fea2002-10-22 23:38:04 +0000568 sqliteExprSpan(A,&X->span,&E);
drhe1b6a5b2000-07-29 13:06:59 +0000569}
drh33048c02001-10-01 14:29:22 +0000570expr(A) ::= expr(X) IS NULL(E). {
571 A = sqliteExpr(TK_ISNULL, X, 0, 0);
drh6977fea2002-10-22 23:38:04 +0000572 sqliteExprSpan(A,&X->span,&E);
drh33048c02001-10-01 14:29:22 +0000573}
drhe1b6a5b2000-07-29 13:06:59 +0000574expr(A) ::= expr(X) NOTNULL(E). {
575 A = sqliteExpr(TK_NOTNULL, X, 0, 0);
drh6977fea2002-10-22 23:38:04 +0000576 sqliteExprSpan(A,&X->span,&E);
drhe1b6a5b2000-07-29 13:06:59 +0000577}
drh33048c02001-10-01 14:29:22 +0000578expr(A) ::= expr(X) NOT NULL(E). {
579 A = sqliteExpr(TK_NOTNULL, X, 0, 0);
drh6977fea2002-10-22 23:38:04 +0000580 sqliteExprSpan(A,&X->span,&E);
drh33048c02001-10-01 14:29:22 +0000581}
drh81a20f22001-10-12 17:30:04 +0000582expr(A) ::= expr(X) IS NOT NULL(E). {
583 A = sqliteExpr(TK_NOTNULL, X, 0, 0);
drh6977fea2002-10-22 23:38:04 +0000584 sqliteExprSpan(A,&X->span,&E);
drh81a20f22001-10-12 17:30:04 +0000585}
drhe1b6a5b2000-07-29 13:06:59 +0000586expr(A) ::= NOT(B) expr(X). {
587 A = sqliteExpr(TK_NOT, X, 0, 0);
drh6977fea2002-10-22 23:38:04 +0000588 sqliteExprSpan(A,&B,&X->span);
drhe1b6a5b2000-07-29 13:06:59 +0000589}
drh81a20f22001-10-12 17:30:04 +0000590expr(A) ::= BITNOT(B) expr(X). {
591 A = sqliteExpr(TK_BITNOT, X, 0, 0);
drh6977fea2002-10-22 23:38:04 +0000592 sqliteExprSpan(A,&B,&X->span);
drh81a20f22001-10-12 17:30:04 +0000593}
drhe1b6a5b2000-07-29 13:06:59 +0000594expr(A) ::= MINUS(B) expr(X). [UMINUS] {
595 A = sqliteExpr(TK_UMINUS, X, 0, 0);
drh6977fea2002-10-22 23:38:04 +0000596 sqliteExprSpan(A,&B,&X->span);
drhe1b6a5b2000-07-29 13:06:59 +0000597}
drh4b59ab52002-08-24 18:24:51 +0000598expr(A) ::= PLUS(B) expr(X). [UPLUS] {
599 A = sqliteExpr(TK_UPLUS, X, 0, 0);
drh6977fea2002-10-22 23:38:04 +0000600 sqliteExprSpan(A,&B,&X->span);
drhe1b6a5b2000-07-29 13:06:59 +0000601}
602expr(A) ::= LP(B) select(X) RP(E). {
drh19a775c2000-06-05 18:54:46 +0000603 A = sqliteExpr(TK_SELECT, 0, 0, 0);
drhdaffd0e2001-04-11 14:28:42 +0000604 if( A ) A->pSelect = X;
drhe1b6a5b2000-07-29 13:06:59 +0000605 sqliteExprSpan(A,&B,&E);
drh19a775c2000-06-05 18:54:46 +0000606}
drhfef52082000-06-06 01:50:43 +0000607expr(A) ::= expr(W) BETWEEN expr(X) AND expr(Y). {
608 ExprList *pList = sqliteExprListAppend(0, X, 0);
609 pList = sqliteExprListAppend(pList, Y, 0);
610 A = sqliteExpr(TK_BETWEEN, W, 0, 0);
drhdaffd0e2001-04-11 14:28:42 +0000611 if( A ) A->pList = pList;
drh6977fea2002-10-22 23:38:04 +0000612 sqliteExprSpan(A,&W->span,&Y->span);
drhfef52082000-06-06 01:50:43 +0000613}
drh4794b982000-06-06 13:54:14 +0000614expr(A) ::= expr(W) NOT BETWEEN expr(X) AND expr(Y). {
615 ExprList *pList = sqliteExprListAppend(0, X, 0);
616 pList = sqliteExprListAppend(pList, Y, 0);
617 A = sqliteExpr(TK_BETWEEN, W, 0, 0);
drhdaffd0e2001-04-11 14:28:42 +0000618 if( A ) A->pList = pList;
drh4794b982000-06-06 13:54:14 +0000619 A = sqliteExpr(TK_NOT, A, 0, 0);
drh6977fea2002-10-22 23:38:04 +0000620 sqliteExprSpan(A,&W->span,&Y->span);
drh4794b982000-06-06 13:54:14 +0000621}
drhe1b6a5b2000-07-29 13:06:59 +0000622expr(A) ::= expr(X) IN LP exprlist(Y) RP(E). {
drhfef52082000-06-06 01:50:43 +0000623 A = sqliteExpr(TK_IN, X, 0, 0);
drhdaffd0e2001-04-11 14:28:42 +0000624 if( A ) A->pList = Y;
drh6977fea2002-10-22 23:38:04 +0000625 sqliteExprSpan(A,&X->span,&E);
drhfef52082000-06-06 01:50:43 +0000626}
drhe1b6a5b2000-07-29 13:06:59 +0000627expr(A) ::= expr(X) IN LP select(Y) RP(E). {
drhfef52082000-06-06 01:50:43 +0000628 A = sqliteExpr(TK_IN, X, 0, 0);
drhdaffd0e2001-04-11 14:28:42 +0000629 if( A ) A->pSelect = Y;
drh6977fea2002-10-22 23:38:04 +0000630 sqliteExprSpan(A,&X->span,&E);
drhfef52082000-06-06 01:50:43 +0000631}
drhe1b6a5b2000-07-29 13:06:59 +0000632expr(A) ::= expr(X) NOT IN LP exprlist(Y) RP(E). {
drh4794b982000-06-06 13:54:14 +0000633 A = sqliteExpr(TK_IN, X, 0, 0);
drhdaffd0e2001-04-11 14:28:42 +0000634 if( A ) A->pList = Y;
drh4794b982000-06-06 13:54:14 +0000635 A = sqliteExpr(TK_NOT, A, 0, 0);
drh6977fea2002-10-22 23:38:04 +0000636 sqliteExprSpan(A,&X->span,&E);
drh4794b982000-06-06 13:54:14 +0000637}
drhe1b6a5b2000-07-29 13:06:59 +0000638expr(A) ::= expr(X) NOT IN LP select(Y) RP(E). {
drh4794b982000-06-06 13:54:14 +0000639 A = sqliteExpr(TK_IN, X, 0, 0);
drhdaffd0e2001-04-11 14:28:42 +0000640 if( A ) A->pSelect = Y;
drh4794b982000-06-06 13:54:14 +0000641 A = sqliteExpr(TK_NOT, A, 0, 0);
drh6977fea2002-10-22 23:38:04 +0000642 sqliteExprSpan(A,&X->span,&E);
drh4794b982000-06-06 13:54:14 +0000643}
drhfef52082000-06-06 01:50:43 +0000644
drh17a7f8d2002-03-24 13:13:27 +0000645/* CASE expressions */
646expr(A) ::= CASE(C) case_operand(X) case_exprlist(Y) case_else(Z) END(E). {
647 A = sqliteExpr(TK_CASE, X, Z, 0);
648 if( A ) A->pList = Y;
649 sqliteExprSpan(A, &C, &E);
650}
651%type case_exprlist {ExprList*}
652%destructor case_exprlist {sqliteExprListDelete($$);}
653case_exprlist(A) ::= case_exprlist(X) WHEN expr(Y) THEN expr(Z). {
654 A = sqliteExprListAppend(X, Y, 0);
655 A = sqliteExprListAppend(A, Z, 0);
656}
657case_exprlist(A) ::= WHEN expr(Y) THEN expr(Z). {
658 A = sqliteExprListAppend(0, Y, 0);
659 A = sqliteExprListAppend(A, Z, 0);
660}
661%type case_else {Expr*}
662case_else(A) ::= ELSE expr(X). {A = X;}
663case_else(A) ::= . {A = 0;}
664%type case_operand {Expr*}
665case_operand(A) ::= expr(X). {A = X;}
666case_operand(A) ::= . {A = 0;}
drh348784e2000-05-29 20:41:49 +0000667
668%type exprlist {ExprList*}
669%destructor exprlist {sqliteExprListDelete($$);}
670%type expritem {Expr*}
671%destructor expritem {sqliteExprDelete($$);}
672
drh348784e2000-05-29 20:41:49 +0000673exprlist(A) ::= exprlist(X) COMMA expritem(Y).
674 {A = sqliteExprListAppend(X,Y,0);}
675exprlist(A) ::= expritem(X). {A = sqliteExprListAppend(0,X,0);}
676expritem(A) ::= expr(X). {A = X;}
677expritem(A) ::= . {A = 0;}
drhcce7d172000-05-31 15:34:51 +0000678
drh382c0242001-10-06 16:33:02 +0000679///////////////////////////// The CREATE INDEX command ///////////////////////
680//
drh5ad1a6c2002-07-01 12:27:09 +0000681cmd ::= CREATE(S) uniqueflag(U) INDEX nm(X)
682 ON nm(Y) LP idxlist(Z) RP(E) onconf(R). {
drh9cfcf5d2002-01-29 18:41:24 +0000683 if( U!=OE_None ) U = R;
684 if( U==OE_Default) U = OE_Abort;
685 sqliteCreateIndex(pParse, &X, &Y, Z, U, &S, &E);
686}
drh717e6402001-09-27 03:22:32 +0000687
688%type uniqueflag {int}
drh9cfcf5d2002-01-29 18:41:24 +0000689uniqueflag(A) ::= UNIQUE. { A = OE_Abort; }
690uniqueflag(A) ::= . { A = OE_None; }
drh348784e2000-05-29 20:41:49 +0000691
692%type idxlist {IdList*}
693%destructor idxlist {sqliteIdListDelete($$);}
drhc2eef3b2002-08-31 18:53:06 +0000694%type idxlist_opt {IdList*}
695%destructor idxlist_opt {sqliteIdListDelete($$);}
drh348784e2000-05-29 20:41:49 +0000696%type idxitem {Token}
697
drhc2eef3b2002-08-31 18:53:06 +0000698idxlist_opt(A) ::= . {A = 0;}
699idxlist_opt(A) ::= LP idxlist(X) RP. {A = X;}
700idxlist(A) ::= idxlist(X) COMMA idxitem(Y). {A = sqliteIdListAppend(X,&Y);}
701idxlist(A) ::= idxitem(Y). {A = sqliteIdListAppend(0,&Y);}
702idxitem(A) ::= nm(X). {A = X;}
drh348784e2000-05-29 20:41:49 +0000703
drh8aff1012001-12-22 14:49:24 +0000704///////////////////////////// The DROP INDEX command /////////////////////////
drh382c0242001-10-06 16:33:02 +0000705//
706
drh5ad1a6c2002-07-01 12:27:09 +0000707cmd ::= DROP INDEX nm(X). {sqliteDropIndex(pParse, &X);}
drh982cef72000-05-30 16:27:03 +0000708
drh382c0242001-10-06 16:33:02 +0000709
drh8aff1012001-12-22 14:49:24 +0000710///////////////////////////// The COPY command ///////////////////////////////
drh382c0242001-10-06 16:33:02 +0000711//
drh5ad1a6c2002-07-01 12:27:09 +0000712cmd ::= COPY orconf(R) nm(X) FROM nm(Y) USING DELIMITERS STRING(Z).
drhb419a922002-01-30 16:17:23 +0000713 {sqliteCopy(pParse,&X,&Y,&Z,R);}
drh5ad1a6c2002-07-01 12:27:09 +0000714cmd ::= COPY orconf(R) nm(X) FROM nm(Y).
drhb419a922002-01-30 16:17:23 +0000715 {sqliteCopy(pParse,&X,&Y,0,R);}
drhdce2cbe2000-05-31 02:27:49 +0000716
drh382c0242001-10-06 16:33:02 +0000717///////////////////////////// The VACUUM command /////////////////////////////
718//
drhdce2cbe2000-05-31 02:27:49 +0000719cmd ::= VACUUM. {sqliteVacuum(pParse,0);}
drh5ad1a6c2002-07-01 12:27:09 +0000720cmd ::= VACUUM nm(X). {sqliteVacuum(pParse,&X);}
drhf57b14a2001-09-14 18:54:08 +0000721
drh382c0242001-10-06 16:33:02 +0000722///////////////////////////// The PRAGMA command /////////////////////////////
723//
drh5ad1a6c2002-07-01 12:27:09 +0000724cmd ::= PRAGMA ids(X) EQ nm(Y). {sqlitePragma(pParse,&X,&Y,0);}
drhf57b14a2001-09-14 18:54:08 +0000725cmd ::= PRAGMA ids(X) EQ ON(Y). {sqlitePragma(pParse,&X,&Y,0);}
726cmd ::= PRAGMA ids(X) EQ plus_num(Y). {sqlitePragma(pParse,&X,&Y,0);}
727cmd ::= PRAGMA ids(X) EQ minus_num(Y). {sqlitePragma(pParse,&X,&Y,1);}
drh5ad1a6c2002-07-01 12:27:09 +0000728cmd ::= PRAGMA ids(X) LP nm(Y) RP. {sqlitePragma(pParse,&X,&Y,0);}
drh603240c2002-03-05 01:11:12 +0000729cmd ::= PRAGMA ids(X). {sqlitePragma(pParse,&X,&X,0);}
drhf57b14a2001-09-14 18:54:08 +0000730plus_num(A) ::= plus_opt number(X). {A = X;}
731minus_num(A) ::= MINUS number(X). {A = X;}
732number(A) ::= INTEGER(X). {A = X;}
733number(A) ::= FLOAT(X). {A = X;}
734plus_opt ::= PLUS.
735plus_opt ::= .
danielk1977c3f9bad2002-05-15 08:30:12 +0000736
737//////////////////////////// The CREATE TRIGGER command /////////////////////
drh5ad1a6c2002-07-01 12:27:09 +0000738cmd ::= CREATE(A) TRIGGER nm(B) trigger_time(C) trigger_event(D) ON nm(E)
danielk1977c3f9bad2002-05-15 08:30:12 +0000739 foreach_clause(F) when_clause(G)
740 BEGIN trigger_cmd_list(S) END(Z). {
drh4b59ab52002-08-24 18:24:51 +0000741 Token all;
742 all.z = A.z;
743 all.n = (Z.z - A.z) + Z.n;
744 sqliteCreateTrigger(pParse, &B, C, D.a, D.b, &E, F, G, S, &all);
danielk1977c3f9bad2002-05-15 08:30:12 +0000745}
746
747%type trigger_time {int}
748trigger_time(A) ::= BEFORE. { A = TK_BEFORE; }
749trigger_time(A) ::= AFTER. { A = TK_AFTER; }
750trigger_time(A) ::= INSTEAD OF. { A = TK_INSTEAD;}
751trigger_time(A) ::= . { A = TK_BEFORE; }
752
drhad3cab52002-05-24 02:04:32 +0000753%type trigger_event {struct TrigEvent}
754%destructor trigger_event {sqliteIdListDelete($$.b);}
danielk1977c3f9bad2002-05-15 08:30:12 +0000755trigger_event(A) ::= DELETE. { A.a = TK_DELETE; A.b = 0; }
756trigger_event(A) ::= INSERT. { A.a = TK_INSERT; A.b = 0; }
757trigger_event(A) ::= UPDATE. { A.a = TK_UPDATE; A.b = 0;}
758trigger_event(A) ::= UPDATE OF inscollist(X). {A.a = TK_UPDATE; A.b = X; }
759
760%type foreach_clause {int}
761foreach_clause(A) ::= . { A = TK_ROW; }
762foreach_clause(A) ::= FOR EACH ROW. { A = TK_ROW; }
763foreach_clause(A) ::= FOR EACH STATEMENT. { A = TK_STATEMENT; }
764
765%type when_clause {Expr *}
766when_clause(A) ::= . { A = 0; }
767when_clause(A) ::= WHEN expr(X). { A = X; }
768
769%type trigger_cmd_list {TriggerStep *}
770trigger_cmd_list(A) ::= trigger_cmd(X) SEMI trigger_cmd_list(Y). {
771 X->pNext = Y ; A = X; }
772trigger_cmd_list(A) ::= . { A = 0; }
773
774%type trigger_cmd {TriggerStep *}
775// UPDATE
drh5ad1a6c2002-07-01 12:27:09 +0000776trigger_cmd(A) ::= UPDATE orconf(R) nm(X) SET setlist(Y) where_opt(Z).
danielk1977c3f9bad2002-05-15 08:30:12 +0000777 { A = sqliteTriggerUpdateStep(&X, Y, Z, R); }
778
779// INSERT
drh5ad1a6c2002-07-01 12:27:09 +0000780trigger_cmd(A) ::= INSERT orconf(R) INTO nm(X) inscollist_opt(F)
danielk1977c3f9bad2002-05-15 08:30:12 +0000781 VALUES LP itemlist(Y) RP.
782{A = sqliteTriggerInsertStep(&X, F, Y, 0, R);}
783
drh5ad1a6c2002-07-01 12:27:09 +0000784trigger_cmd(A) ::= INSERT orconf(R) INTO nm(X) inscollist_opt(F) select(S).
danielk1977c3f9bad2002-05-15 08:30:12 +0000785 {A = sqliteTriggerInsertStep(&X, F, 0, S, R);}
786
787// DELETE
drh5ad1a6c2002-07-01 12:27:09 +0000788trigger_cmd(A) ::= DELETE FROM nm(X) where_opt(Y).
danielk1977c3f9bad2002-05-15 08:30:12 +0000789 {A = sqliteTriggerDeleteStep(&X, Y);}
790
791// SELECT
792trigger_cmd(A) ::= select(X). {A = sqliteTriggerSelectStep(X); }
793
danielk19776f349032002-06-11 02:25:40 +0000794// The special RAISE expression that may occur in trigger programs
drh4b59ab52002-08-24 18:24:51 +0000795expr(A) ::= RAISE(X) LP IGNORE RP(Y). {
796 A = sqliteExpr(TK_RAISE, 0, 0, 0);
797 A->iColumn = OE_Ignore;
drh6977fea2002-10-22 23:38:04 +0000798 sqliteExprSpan(A, &X, &Y);
drh4b59ab52002-08-24 18:24:51 +0000799}
800expr(A) ::= RAISE(X) LP ROLLBACK COMMA nm(Z) RP(Y). {
801 A = sqliteExpr(TK_RAISE, 0, 0, &Z);
802 A->iColumn = OE_Rollback;
drh6977fea2002-10-22 23:38:04 +0000803 sqliteExprSpan(A, &X, &Y);
drh4b59ab52002-08-24 18:24:51 +0000804}
805expr(A) ::= RAISE(X) LP ABORT COMMA nm(Z) RP(Y). {
806 A = sqliteExpr(TK_RAISE, 0, 0, &Z);
807 A->iColumn = OE_Abort;
drh6977fea2002-10-22 23:38:04 +0000808 sqliteExprSpan(A, &X, &Y);
drh4b59ab52002-08-24 18:24:51 +0000809}
810expr(A) ::= RAISE(X) LP FAIL COMMA nm(Z) RP(Y). {
811 A = sqliteExpr(TK_RAISE, 0, 0, &Z);
812 A->iColumn = OE_Fail;
drh6977fea2002-10-22 23:38:04 +0000813 sqliteExprSpan(A, &X, &Y);
drh4b59ab52002-08-24 18:24:51 +0000814}
danielk19776f349032002-06-11 02:25:40 +0000815
danielk1977c3f9bad2002-05-15 08:30:12 +0000816//////////////////////// DROP TRIGGER statement //////////////////////////////
drh5ad1a6c2002-07-01 12:27:09 +0000817cmd ::= DROP TRIGGER nm(X). {
danielk1977c3f9bad2002-05-15 08:30:12 +0000818 sqliteDropTrigger(pParse,&X,0);
819}