blob: f640a20f62e21261a4a9c5e3136e902a15bae3c6 [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**
drh826fb5a2004-02-14 23:59:57 +000017** @(#) $Id: parse.y,v 1.110 2004/02/14 23:59:57 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 {
drhb86ccfb2003-01-28 23:13:10 +000024 if( pParse->zErrMsg==0 ){
25 if( TOKEN.z[0] ){
26 sqliteSetNString(&pParse->zErrMsg,
27 "near \"", -1, TOKEN.z, TOKEN.n, "\": syntax error", -1, 0);
28 }else{
drh41743982003-12-06 21:43:55 +000029 sqliteSetString(&pParse->zErrMsg, "incomplete SQL statement", (char*)0);
drhb86ccfb2003-01-28 23:13:10 +000030 }
31 }
32 pParse->nErr++;
drh348784e2000-05-29 20:41:49 +000033}
34%name sqliteParser
35%include {
36#include "sqliteInt.h"
37#include "parse.h"
drh9bbca4c2001-11-06 04:00:18 +000038
39/*
drhad3cab52002-05-24 02:04:32 +000040** An instance of this structure holds information about the
41** LIMIT clause of a SELECT statement.
drh9bbca4c2001-11-06 04:00:18 +000042*/
drhad3cab52002-05-24 02:04:32 +000043struct LimitVal {
44 int limit; /* The LIMIT value. -1 if there is no limit */
45 int offset; /* The OFFSET. 0 if there is none */
46};
danielk1977c3f9bad2002-05-15 08:30:12 +000047
48/*
drhad3cab52002-05-24 02:04:32 +000049** An instance of the following structure describes the event of a
50** TRIGGER. "a" is the event type, one of TK_UPDATE, TK_INSERT,
51** TK_DELETE, or TK_INSTEAD. If the event is of the form
52**
53** UPDATE ON (a,b,c)
54**
55** Then the "b" IdList records the list "a,b,c".
danielk1977c3f9bad2002-05-15 08:30:12 +000056*/
drhad3cab52002-05-24 02:04:32 +000057struct TrigEvent { int a; IdList * b; };
drhcaec2f12003-01-07 02:47:47 +000058
59} // end %include
drh348784e2000-05-29 20:41:49 +000060
drh348784e2000-05-29 20:41:49 +000061// These are extra tokens used by the lexer but never seen by the
62// parser. We put them in a rule so that the parser generator will
drh6206d502000-06-19 19:09:08 +000063// add them to the parse.h output file.
drh348784e2000-05-29 20:41:49 +000064//
drhc4a3c772001-04-04 11:48:57 +000065%nonassoc END_OF_FILE ILLEGAL SPACE UNCLOSED_STRING COMMENT FUNCTION
66 COLUMN AGG_FUNCTION.
67
drh826fb5a2004-02-14 23:59:57 +000068// Input is a single SQL command
drhc4a3c772001-04-04 11:48:57 +000069input ::= cmdlist.
drh094b2bb2002-03-13 18:54:07 +000070cmdlist ::= cmdlist ecmd.
drh826fb5a2004-02-14 23:59:57 +000071cmdlist ::= ecmd.
drh483750b2003-01-29 18:46:51 +000072ecmd ::= explain cmdx SEMI.
drh094b2bb2002-03-13 18:54:07 +000073ecmd ::= SEMI.
drh483750b2003-01-29 18:46:51 +000074cmdx ::= cmd. { sqliteExec(pParse); }
drhe0bc4042002-06-25 01:09:11 +000075explain ::= EXPLAIN. { sqliteBeginParse(pParse, 1); }
76explain ::= . { sqliteBeginParse(pParse, 0); }
drh348784e2000-05-29 20:41:49 +000077
drh382c0242001-10-06 16:33:02 +000078///////////////////// Begin and end transactions. ////////////////////////////
drhc4a3c772001-04-04 11:48:57 +000079//
drhfa86c412002-02-02 15:01:15 +000080
drh0d65dc02002-02-03 00:56:09 +000081cmd ::= BEGIN trans_opt onconf(R). {sqliteBeginTransaction(pParse,R);}
drhc4a3c772001-04-04 11:48:57 +000082trans_opt ::= .
83trans_opt ::= TRANSACTION.
drh5ad1a6c2002-07-01 12:27:09 +000084trans_opt ::= TRANSACTION nm.
drhc4a3c772001-04-04 11:48:57 +000085cmd ::= COMMIT trans_opt. {sqliteCommitTransaction(pParse);}
86cmd ::= END trans_opt. {sqliteCommitTransaction(pParse);}
87cmd ::= ROLLBACK trans_opt. {sqliteRollbackTransaction(pParse);}
88
drh382c0242001-10-06 16:33:02 +000089///////////////////// The CREATE TABLE statement ////////////////////////////
drh348784e2000-05-29 20:41:49 +000090//
91cmd ::= create_table create_table_args.
drh5ad1a6c2002-07-01 12:27:09 +000092create_table ::= CREATE(X) temp(T) TABLE nm(Y). {
drhe5f9c642003-01-13 23:27:31 +000093 sqliteStartTable(pParse,&X,&Y,T,0);
drh969fa7c2002-02-18 18:30:32 +000094}
drhf57b3392001-10-08 13:22:32 +000095%type temp {int}
drhd24cc422003-03-27 12:51:24 +000096temp(A) ::= TEMP. {A = 1;}
97temp(A) ::= . {A = 0;}
drh969fa7c2002-02-18 18:30:32 +000098create_table_args ::= LP columnlist conslist_opt RP(X). {
99 sqliteEndTable(pParse,&X,0);
100}
101create_table_args ::= AS select(S). {
102 sqliteEndTable(pParse,0,S);
103 sqliteSelectDelete(S);
104}
drh348784e2000-05-29 20:41:49 +0000105columnlist ::= columnlist COMMA column.
106columnlist ::= column.
107
108// About the only information used for a column is the name of the
109// column. The type is always just "text". But the code will accept
110// an elaborate typename. Perhaps someday we'll do something with it.
111//
112column ::= columnid type carglist.
drh5ad1a6c2002-07-01 12:27:09 +0000113columnid ::= nm(X). {sqliteAddColumn(pParse,&X);}
drhc4a3c772001-04-04 11:48:57 +0000114
115// An IDENTIFIER can be a generic identifier, or one of several
116// keywords. Any non-standard keyword can also be an identifier.
drhc4a3c772001-04-04 11:48:57 +0000117//
drh982cef72000-05-30 16:27:03 +0000118%type id {Token}
drhf18543c2002-03-30 15:26:50 +0000119id(A) ::= ID(X). {A = X;}
drh0bd1f4e2002-06-06 18:54:39 +0000120
drh34e33bb2002-06-06 19:04:16 +0000121// The following directive causes tokens ABORT, AFTER, ASC, etc. to
122// fallback to ID if they will not parse as their original value.
123// This obviates the need for the "id" nonterminal.
124//
drh319e4e72003-09-30 01:54:13 +0000125%fallback ID
drh113088e2003-03-20 01:16:58 +0000126 ABORT AFTER ASC ATTACH BEFORE BEGIN CASCADE CLUSTER CONFLICT
127 COPY DATABASE DEFERRED DELIMITERS DESC DETACH EACH END EXPLAIN FAIL FOR
drh319e4e72003-09-30 01:54:13 +0000128 GLOB IGNORE IMMEDIATE INITIALLY INSTEAD LIKE MATCH KEY
drh5ad1a6c2002-07-01 12:27:09 +0000129 OF OFFSET PRAGMA RAISE REPLACE RESTRICT ROW STATEMENT
drh0bd1f4e2002-06-06 18:54:39 +0000130 TEMP TRIGGER VACUUM VIEW.
drhc4a3c772001-04-04 11:48:57 +0000131
132// And "ids" is an identifer-or-string.
133//
134%type ids {Token}
drh5ad1a6c2002-07-01 12:27:09 +0000135ids(A) ::= ID(X). {A = X;}
drhc4a3c772001-04-04 11:48:57 +0000136ids(A) ::= STRING(X). {A = X;}
137
drh5ad1a6c2002-07-01 12:27:09 +0000138// The name of a column or table can be any of the following:
139//
140%type nm {Token}
141nm(A) ::= ID(X). {A = X;}
142nm(A) ::= STRING(X). {A = X;}
143nm(A) ::= JOIN_KW(X). {A = X;}
144
drh382c0242001-10-06 16:33:02 +0000145type ::= .
146type ::= typename(X). {sqliteAddColumnType(pParse,&X,&X);}
147type ::= typename(X) LP signed RP(Y). {sqliteAddColumnType(pParse,&X,&Y);}
148type ::= typename(X) LP signed COMMA signed RP(Y).
149 {sqliteAddColumnType(pParse,&X,&Y);}
150%type typename {Token}
151typename(A) ::= ids(X). {A = X;}
152typename(A) ::= typename(X) ids. {A = X;}
drhef0cae52003-07-16 02:19:37 +0000153%type signed {int}
154signed(A) ::= INTEGER(X). { A = atoi(X.z); }
155signed(A) ::= PLUS INTEGER(X). { A = atoi(X.z); }
156signed(A) ::= MINUS INTEGER(X). { A = -atoi(X.z); }
drh348784e2000-05-29 20:41:49 +0000157carglist ::= carglist carg.
158carglist ::= .
drh5ad1a6c2002-07-01 12:27:09 +0000159carg ::= CONSTRAINT nm ccons.
drh348784e2000-05-29 20:41:49 +0000160carg ::= ccons.
drh7020f652000-06-03 18:06:52 +0000161carg ::= DEFAULT STRING(X). {sqliteAddDefaultValue(pParse,&X,0);}
162carg ::= DEFAULT ID(X). {sqliteAddDefaultValue(pParse,&X,0);}
163carg ::= DEFAULT INTEGER(X). {sqliteAddDefaultValue(pParse,&X,0);}
164carg ::= DEFAULT PLUS INTEGER(X). {sqliteAddDefaultValue(pParse,&X,0);}
165carg ::= DEFAULT MINUS INTEGER(X). {sqliteAddDefaultValue(pParse,&X,1);}
166carg ::= DEFAULT FLOAT(X). {sqliteAddDefaultValue(pParse,&X,0);}
167carg ::= DEFAULT PLUS FLOAT(X). {sqliteAddDefaultValue(pParse,&X,0);}
168carg ::= DEFAULT MINUS FLOAT(X). {sqliteAddDefaultValue(pParse,&X,1);}
169carg ::= DEFAULT NULL.
drh348784e2000-05-29 20:41:49 +0000170
drh382c0242001-10-06 16:33:02 +0000171// In addition to the type name, we also care about the primary key and
172// UNIQUE constraints.
drh348784e2000-05-29 20:41:49 +0000173//
drh0d316a42002-08-11 20:10:47 +0000174ccons ::= NULL onconf.
drh9cfcf5d2002-01-29 18:41:24 +0000175ccons ::= NOT NULL onconf(R). {sqliteAddNotNull(pParse, R);}
176ccons ::= PRIMARY KEY sortorder onconf(R). {sqliteAddPrimaryKey(pParse,0,R);}
drh4925ca02003-11-27 00:48:57 +0000177ccons ::= UNIQUE onconf(R). {sqliteCreateIndex(pParse,0,0,0,R,0,0);}
drh9cfcf5d2002-01-29 18:41:24 +0000178ccons ::= CHECK LP expr RP onconf.
drhc2eef3b2002-08-31 18:53:06 +0000179ccons ::= REFERENCES nm(T) idxlist_opt(TA) refargs(R).
180 {sqliteCreateForeignKey(pParse,0,&T,TA,R);}
181ccons ::= defer_subclause(D). {sqliteDeferForeignKey(pParse,D);}
drh8e2ca022002-06-17 17:07:19 +0000182ccons ::= COLLATE id(C). {
drhfcb78a42003-01-18 20:11:05 +0000183 sqliteAddCollateType(pParse, sqliteCollateType(C.z, C.n));
drh8e2ca022002-06-17 17:07:19 +0000184}
drh04738cb2002-06-02 18:19:00 +0000185
drhc2eef3b2002-08-31 18:53:06 +0000186// The next group of rules parses the arguments to a REFERENCES clause
187// that determine if the referential integrity checking is deferred or
188// or immediate and which determine what action to take if a ref-integ
189// check fails.
drh04738cb2002-06-02 18:19:00 +0000190//
drhc2eef3b2002-08-31 18:53:06 +0000191%type refargs {int}
192refargs(A) ::= . { A = OE_Restrict * 0x010101; }
193refargs(A) ::= refargs(X) refarg(Y). { A = (X & Y.mask) | Y.value; }
194%type refarg {struct {int value; int mask;}}
195refarg(A) ::= MATCH nm. { A.value = 0; A.mask = 0x000000; }
196refarg(A) ::= ON DELETE refact(X). { A.value = X; A.mask = 0x0000ff; }
197refarg(A) ::= ON UPDATE refact(X). { A.value = X<<8; A.mask = 0x00ff00; }
198refarg(A) ::= ON INSERT refact(X). { A.value = X<<16; A.mask = 0xff0000; }
199%type refact {int}
200refact(A) ::= SET NULL. { A = OE_SetNull; }
201refact(A) ::= SET DEFAULT. { A = OE_SetDflt; }
202refact(A) ::= CASCADE. { A = OE_Cascade; }
203refact(A) ::= RESTRICT. { A = OE_Restrict; }
204%type defer_subclause {int}
205defer_subclause(A) ::= NOT DEFERRABLE init_deferred_pred_opt(X). {A = X;}
206defer_subclause(A) ::= DEFERRABLE init_deferred_pred_opt(X). {A = X;}
207%type init_deferred_pred_opt {int}
208init_deferred_pred_opt(A) ::= . {A = 0;}
209init_deferred_pred_opt(A) ::= INITIALLY DEFERRED. {A = 1;}
210init_deferred_pred_opt(A) ::= INITIALLY IMMEDIATE. {A = 0;}
drh348784e2000-05-29 20:41:49 +0000211
212// For the time being, the only constraint we care about is the primary
drh382c0242001-10-06 16:33:02 +0000213// key and UNIQUE. Both create indices.
drh348784e2000-05-29 20:41:49 +0000214//
215conslist_opt ::= .
216conslist_opt ::= COMMA conslist.
217conslist ::= conslist COMMA tcons.
drha2e1bb52001-01-04 14:20:18 +0000218conslist ::= conslist tcons.
drh348784e2000-05-29 20:41:49 +0000219conslist ::= tcons.
drh5ad1a6c2002-07-01 12:27:09 +0000220tcons ::= CONSTRAINT nm.
drh9cfcf5d2002-01-29 18:41:24 +0000221tcons ::= PRIMARY KEY LP idxlist(X) RP onconf(R).
222 {sqliteAddPrimaryKey(pParse,X,R);}
223tcons ::= UNIQUE LP idxlist(X) RP onconf(R).
drh4925ca02003-11-27 00:48:57 +0000224 {sqliteCreateIndex(pParse,0,0,X,R,0,0);}
drh9cfcf5d2002-01-29 18:41:24 +0000225tcons ::= CHECK expr onconf.
drhc2eef3b2002-08-31 18:53:06 +0000226tcons ::= FOREIGN KEY LP idxlist(FA) RP
227 REFERENCES nm(T) idxlist_opt(TA) refargs(R) defer_subclause_opt(D). {
228 sqliteCreateForeignKey(pParse, FA, &T, TA, R);
229 sqliteDeferForeignKey(pParse, D);
230}
231%type defer_subclause_opt {int}
232defer_subclause_opt(A) ::= . {A = 0;}
233defer_subclause_opt(A) ::= defer_subclause(X). {A = X;}
drh9cfcf5d2002-01-29 18:41:24 +0000234
235// The following is a non-standard extension that allows us to declare the
236// default behavior when there is a constraint conflict.
237//
238%type onconf {int}
drh1c928532002-01-31 15:54:21 +0000239%type orconf {int}
240%type resolvetype {int}
241onconf(A) ::= . { A = OE_Default; }
242onconf(A) ::= ON CONFLICT resolvetype(X). { A = X; }
243orconf(A) ::= . { A = OE_Default; }
244orconf(A) ::= OR resolvetype(X). { A = X; }
245resolvetype(A) ::= ROLLBACK. { A = OE_Rollback; }
246resolvetype(A) ::= ABORT. { A = OE_Abort; }
247resolvetype(A) ::= FAIL. { A = OE_Fail; }
248resolvetype(A) ::= IGNORE. { A = OE_Ignore; }
249resolvetype(A) ::= REPLACE. { A = OE_Replace; }
drh348784e2000-05-29 20:41:49 +0000250
drh382c0242001-10-06 16:33:02 +0000251////////////////////////// The DROP TABLE /////////////////////////////////////
drh348784e2000-05-29 20:41:49 +0000252//
drh5ad1a6c2002-07-01 12:27:09 +0000253cmd ::= DROP TABLE nm(X). {sqliteDropTable(pParse,&X,0);}
drh348784e2000-05-29 20:41:49 +0000254
drha76b5df2002-02-23 02:32:10 +0000255///////////////////// The CREATE VIEW statement /////////////////////////////
256//
drh6276c1c2002-07-08 22:03:32 +0000257cmd ::= CREATE(X) temp(T) VIEW nm(Y) AS select(S). {
258 sqliteCreateView(pParse, &X, &Y, S, T);
drha76b5df2002-02-23 02:32:10 +0000259}
drh5ad1a6c2002-07-01 12:27:09 +0000260cmd ::= DROP VIEW nm(X). {
drh4ff6dfa2002-03-03 23:06:00 +0000261 sqliteDropTable(pParse, &X, 1);
drha76b5df2002-02-23 02:32:10 +0000262}
263
drh382c0242001-10-06 16:33:02 +0000264//////////////////////// The SELECT statement /////////////////////////////////
drh348784e2000-05-29 20:41:49 +0000265//
drh9bb61fe2000-06-05 16:01:39 +0000266cmd ::= select(X). {
drh832508b2002-03-02 17:04:07 +0000267 sqliteSelect(pParse, X, SRT_Callback, 0, 0, 0, 0);
drh9bb61fe2000-06-05 16:01:39 +0000268 sqliteSelectDelete(X);
269}
drhefb72512000-05-31 20:00:52 +0000270
drh9bb61fe2000-06-05 16:01:39 +0000271%type select {Select*}
272%destructor select {sqliteSelectDelete($$);}
drh82c3d632000-06-06 21:56:07 +0000273%type oneselect {Select*}
274%destructor oneselect {sqliteSelectDelete($$);}
drh9bb61fe2000-06-05 16:01:39 +0000275
drh82c3d632000-06-06 21:56:07 +0000276select(A) ::= oneselect(X). {A = X;}
drh0a36c572002-02-18 22:49:59 +0000277select(A) ::= select(X) multiselect_op(Y) oneselect(Z). {
drhdaffd0e2001-04-11 14:28:42 +0000278 if( Z ){
drh82c3d632000-06-06 21:56:07 +0000279 Z->op = Y;
280 Z->pPrior = X;
drhdaffd0e2001-04-11 14:28:42 +0000281 }
282 A = Z;
drh82c3d632000-06-06 21:56:07 +0000283}
drh0a36c572002-02-18 22:49:59 +0000284%type multiselect_op {int}
285multiselect_op(A) ::= UNION. {A = TK_UNION;}
286multiselect_op(A) ::= UNION ALL. {A = TK_ALL;}
287multiselect_op(A) ::= INTERSECT. {A = TK_INTERSECT;}
288multiselect_op(A) ::= EXCEPT. {A = TK_EXCEPT;}
drh82c3d632000-06-06 21:56:07 +0000289oneselect(A) ::= SELECT distinct(D) selcollist(W) from(X) where_opt(Y)
drh9bbca4c2001-11-06 04:00:18 +0000290 groupby_opt(P) having_opt(Q) orderby_opt(Z) limit_opt(L). {
drhad3cab52002-05-24 02:04:32 +0000291 A = sqliteSelectNew(W,X,Y,P,Q,Z,D,L.limit,L.offset);
drh9bb61fe2000-06-05 16:01:39 +0000292}
293
294// The "distinct" nonterminal is true (1) if the DISTINCT keyword is
295// present and false (0) if it is not.
296//
drhefb72512000-05-31 20:00:52 +0000297%type distinct {int}
drhefb72512000-05-31 20:00:52 +0000298distinct(A) ::= DISTINCT. {A = 1;}
drhfef52082000-06-06 01:50:43 +0000299distinct(A) ::= ALL. {A = 0;}
drhefb72512000-05-31 20:00:52 +0000300distinct(A) ::= . {A = 0;}
drh348784e2000-05-29 20:41:49 +0000301
drh9bb61fe2000-06-05 16:01:39 +0000302// selcollist is a list of expressions that are to become the return
drh7c917d12001-12-16 20:05:05 +0000303// values of the SELECT statement. The "*" in statements like
304// "SELECT * FROM ..." is encoded as a special expression with an
305// opcode of TK_ALL.
drh9bb61fe2000-06-05 16:01:39 +0000306//
drh348784e2000-05-29 20:41:49 +0000307%type selcollist {ExprList*}
308%destructor selcollist {sqliteExprListDelete($$);}
309%type sclp {ExprList*}
310%destructor sclp {sqliteExprListDelete($$);}
drh348784e2000-05-29 20:41:49 +0000311sclp(A) ::= selcollist(X) COMMA. {A = X;}
312sclp(A) ::= . {A = 0;}
drh01f3f252002-05-24 16:14:15 +0000313selcollist(A) ::= sclp(P) expr(X) as(Y). {
314 A = sqliteExprListAppend(P,X,Y.n?&Y:0);
315}
drh7c917d12001-12-16 20:05:05 +0000316selcollist(A) ::= sclp(P) STAR. {
317 A = sqliteExprListAppend(P, sqliteExpr(TK_ALL, 0, 0, 0), 0);
318}
drh5ad1a6c2002-07-01 12:27:09 +0000319selcollist(A) ::= sclp(P) nm(X) DOT STAR. {
drh54473222002-04-04 02:10:55 +0000320 Expr *pRight = sqliteExpr(TK_ALL, 0, 0, 0);
321 Expr *pLeft = sqliteExpr(TK_ID, 0, 0, &X);
322 A = sqliteExprListAppend(P, sqliteExpr(TK_DOT, pLeft, pRight, 0), 0);
323}
drh01f3f252002-05-24 16:14:15 +0000324
325// An option "AS <id>" phrase that can follow one of the expressions that
326// define the result set, or one of the tables in the FROM clause.
327//
328%type as {Token}
drh5ad1a6c2002-07-01 12:27:09 +0000329as(X) ::= AS nm(Y). { X = Y; }
330as(X) ::= ids(Y). { X = Y; }
331as(X) ::= . { X.n = 0; }
drh9bb61fe2000-06-05 16:01:39 +0000332
drh348784e2000-05-29 20:41:49 +0000333
drhad3cab52002-05-24 02:04:32 +0000334%type seltablist {SrcList*}
335%destructor seltablist {sqliteSrcListDelete($$);}
336%type stl_prefix {SrcList*}
337%destructor stl_prefix {sqliteSrcListDelete($$);}
338%type from {SrcList*}
339%destructor from {sqliteSrcListDelete($$);}
drh348784e2000-05-29 20:41:49 +0000340
drh01f3f252002-05-24 16:14:15 +0000341// A complete FROM clause.
342//
drhbf3a4fa2002-04-06 13:57:42 +0000343from(A) ::= . {A = sqliteMalloc(sizeof(*A));}
drh348784e2000-05-29 20:41:49 +0000344from(A) ::= FROM seltablist(X). {A = X;}
drh01f3f252002-05-24 16:14:15 +0000345
346// "seltablist" is a "Select Table List" - the content of the FROM clause
347// in a SELECT statement. "stl_prefix" is a prefix of this list.
348//
349stl_prefix(A) ::= seltablist(X) joinop(Y). {
350 A = X;
351 if( A && A->nSrc>0 ) A->a[A->nSrc-1].jointype = Y;
352}
drh348784e2000-05-29 20:41:49 +0000353stl_prefix(A) ::= . {A = 0;}
drh113088e2003-03-20 01:16:58 +0000354seltablist(A) ::= stl_prefix(X) nm(Y) dbnm(D) as(Z) on_opt(N) using_opt(U). {
355 A = sqliteSrcListAppend(X,&Y,&D);
drh01f3f252002-05-24 16:14:15 +0000356 if( Z.n ) sqliteSrcListAddAlias(A,&Z);
357 if( N ){
358 if( A && A->nSrc>1 ){ A->a[A->nSrc-2].pOn = N; }
359 else { sqliteExprDelete(N); }
360 }
361 if( U ){
362 if( A && A->nSrc>1 ){ A->a[A->nSrc-2].pUsing = U; }
363 else { sqliteIdListDelete(U); }
364 }
drhc4a3c772001-04-04 11:48:57 +0000365}
drhb733d032004-01-24 20:18:12 +0000366seltablist(A) ::= stl_prefix(X) LP seltablist_paren(S) RP
367 as(Z) on_opt(N) using_opt(U). {
drh113088e2003-03-20 01:16:58 +0000368 A = sqliteSrcListAppend(X,0,0);
drhad3cab52002-05-24 02:04:32 +0000369 A->a[A->nSrc-1].pSelect = S;
drh01f3f252002-05-24 16:14:15 +0000370 if( Z.n ) sqliteSrcListAddAlias(A,&Z);
371 if( N ){
372 if( A && A->nSrc>1 ){ A->a[A->nSrc-2].pOn = N; }
373 else { sqliteExprDelete(N); }
drhd5feede2002-05-08 21:46:14 +0000374 }
drh01f3f252002-05-24 16:14:15 +0000375 if( U ){
376 if( A && A->nSrc>1 ){ A->a[A->nSrc-2].pUsing = U; }
377 else { sqliteIdListDelete(U); }
378 }
drh22f70c32002-02-18 01:17:00 +0000379}
drh348784e2000-05-29 20:41:49 +0000380
drhb733d032004-01-24 20:18:12 +0000381// A seltablist_paren nonterminal represents anything in a FROM that
382// is contained inside parentheses. This can be either a subquery or
383// a grouping of table and subqueries.
384//
385%type seltablist_paren {Select*}
386%destructor seltablist_paren {sqliteSelectDelete($$);}
387seltablist_paren(A) ::= select(S). {A = S;}
388seltablist_paren(A) ::= seltablist(F). {
389 A = sqliteSelectNew(0,F,0,0,0,0,0,-1,0);
390}
391
drh113088e2003-03-20 01:16:58 +0000392%type dbnm {Token}
393dbnm(A) ::= . {A.z=0; A.n=0;}
394dbnm(A) ::= DOT nm(X). {A = X;}
395
drh01f3f252002-05-24 16:14:15 +0000396%type joinop {int}
397%type joinop2 {int}
398joinop(X) ::= COMMA. { X = JT_INNER; }
399joinop(X) ::= JOIN. { X = JT_INNER; }
drh5ad1a6c2002-07-01 12:27:09 +0000400joinop(X) ::= JOIN_KW(A) JOIN. { X = sqliteJoinType(pParse,&A,0,0); }
401joinop(X) ::= JOIN_KW(A) nm(B) JOIN. { X = sqliteJoinType(pParse,&A,&B,0); }
402joinop(X) ::= JOIN_KW(A) nm(B) nm(C) JOIN.
403 { X = sqliteJoinType(pParse,&A,&B,&C); }
drh01f3f252002-05-24 16:14:15 +0000404
405%type on_opt {Expr*}
406%destructor on_opt {sqliteExprDelete($$);}
407on_opt(N) ::= ON expr(E). {N = E;}
408on_opt(N) ::= . {N = 0;}
409
410%type using_opt {IdList*}
411%destructor using_opt {sqliteIdListDelete($$);}
412using_opt(U) ::= USING LP idxlist(L) RP. {U = L;}
413using_opt(U) ::= . {U = 0;}
414
415
drh348784e2000-05-29 20:41:49 +0000416%type orderby_opt {ExprList*}
417%destructor orderby_opt {sqliteExprListDelete($$);}
418%type sortlist {ExprList*}
419%destructor sortlist {sqliteExprListDelete($$);}
420%type sortitem {Expr*}
421%destructor sortitem {sqliteExprDelete($$);}
422
423orderby_opt(A) ::= . {A = 0;}
424orderby_opt(A) ::= ORDER BY sortlist(X). {A = X;}
drh8e2ca022002-06-17 17:07:19 +0000425sortlist(A) ::= sortlist(X) COMMA sortitem(Y) collate(C) sortorder(Z). {
drh9bb61fe2000-06-05 16:01:39 +0000426 A = sqliteExprListAppend(X,Y,0);
drh8e2ca022002-06-17 17:07:19 +0000427 if( A ) A->a[A->nExpr-1].sortOrder = C+Z;
drh9bb61fe2000-06-05 16:01:39 +0000428}
drh38640e12002-07-05 21:42:36 +0000429sortlist(A) ::= sortitem(Y) collate(C) sortorder(Z). {
drh9bb61fe2000-06-05 16:01:39 +0000430 A = sqliteExprListAppend(0,Y,0);
drh38640e12002-07-05 21:42:36 +0000431 if( A ) A->a[0].sortOrder = C+Z;
drh9bb61fe2000-06-05 16:01:39 +0000432}
drhda9d6c42000-05-31 18:20:14 +0000433sortitem(A) ::= expr(X). {A = X;}
drh348784e2000-05-29 20:41:49 +0000434
435%type sortorder {int}
drh8e2ca022002-06-17 17:07:19 +0000436%type collate {int}
drh348784e2000-05-29 20:41:49 +0000437
drh8e2ca022002-06-17 17:07:19 +0000438sortorder(A) ::= ASC. {A = SQLITE_SO_ASC;}
439sortorder(A) ::= DESC. {A = SQLITE_SO_DESC;}
440sortorder(A) ::= . {A = SQLITE_SO_ASC;}
441collate(C) ::= . {C = SQLITE_SO_UNK;}
drhfcb78a42003-01-18 20:11:05 +0000442collate(C) ::= COLLATE id(X). {C = sqliteCollateType(X.z, X.n);}
drh348784e2000-05-29 20:41:49 +0000443
drh22827922000-06-06 17:27:05 +0000444%type groupby_opt {ExprList*}
445%destructor groupby_opt {sqliteExprListDelete($$);}
drh6206d502000-06-19 19:09:08 +0000446groupby_opt(A) ::= . {A = 0;}
drh22827922000-06-06 17:27:05 +0000447groupby_opt(A) ::= GROUP BY exprlist(X). {A = X;}
448
449%type having_opt {Expr*}
450%destructor having_opt {sqliteExprDelete($$);}
drh6206d502000-06-19 19:09:08 +0000451having_opt(A) ::= . {A = 0;}
drh22827922000-06-06 17:27:05 +0000452having_opt(A) ::= HAVING expr(X). {A = X;}
453
drhad3cab52002-05-24 02:04:32 +0000454%type limit_opt {struct LimitVal}
drhef0cae52003-07-16 02:19:37 +0000455limit_opt(A) ::= . {A.limit = -1; A.offset = 0;}
456limit_opt(A) ::= LIMIT signed(X). {A.limit = X; A.offset = 0;}
457limit_opt(A) ::= LIMIT signed(X) OFFSET signed(Y).
458 {A.limit = X; A.offset = Y;}
459limit_opt(A) ::= LIMIT signed(X) COMMA signed(Y).
460 {A.limit = Y; A.offset = X;}
drh9bbca4c2001-11-06 04:00:18 +0000461
drh382c0242001-10-06 16:33:02 +0000462/////////////////////////// The DELETE statement /////////////////////////////
463//
drh113088e2003-03-20 01:16:58 +0000464cmd ::= DELETE FROM nm(X) dbnm(D) where_opt(Y). {
465 sqliteDeleteFrom(pParse, sqliteSrcListAppend(0,&X,&D), Y);
466}
drh348784e2000-05-29 20:41:49 +0000467
468%type where_opt {Expr*}
469%destructor where_opt {sqliteExprDelete($$);}
470
471where_opt(A) ::= . {A = 0;}
472where_opt(A) ::= WHERE expr(X). {A = X;}
473
474%type setlist {ExprList*}
475%destructor setlist {sqliteExprListDelete($$);}
476
drh382c0242001-10-06 16:33:02 +0000477////////////////////////// The UPDATE command ////////////////////////////////
478//
drh113088e2003-03-20 01:16:58 +0000479cmd ::= UPDATE orconf(R) nm(X) dbnm(D) SET setlist(Y) where_opt(Z).
480 {sqliteUpdate(pParse,sqliteSrcListAppend(0,&X,&D),Y,Z,R);}
drh348784e2000-05-29 20:41:49 +0000481
drh5ad1a6c2002-07-01 12:27:09 +0000482setlist(A) ::= setlist(Z) COMMA nm(X) EQ expr(Y).
drh348784e2000-05-29 20:41:49 +0000483 {A = sqliteExprListAppend(Z,Y,&X);}
drh5ad1a6c2002-07-01 12:27:09 +0000484setlist(A) ::= nm(X) EQ expr(Y). {A = sqliteExprListAppend(0,Y,&X);}
drh348784e2000-05-29 20:41:49 +0000485
drh382c0242001-10-06 16:33:02 +0000486////////////////////////// The INSERT command /////////////////////////////////
487//
drh113088e2003-03-20 01:16:58 +0000488cmd ::= insert_cmd(R) INTO nm(X) dbnm(D) inscollist_opt(F)
489 VALUES LP itemlist(Y) RP.
490 {sqliteInsert(pParse, sqliteSrcListAppend(0,&X,&D), Y, 0, F, R);}
491cmd ::= insert_cmd(R) INTO nm(X) dbnm(D) inscollist_opt(F) select(S).
492 {sqliteInsert(pParse, sqliteSrcListAppend(0,&X,&D), 0, S, F, R);}
drh348784e2000-05-29 20:41:49 +0000493
drhfa86c412002-02-02 15:01:15 +0000494%type insert_cmd {int}
495insert_cmd(A) ::= INSERT orconf(R). {A = R;}
496insert_cmd(A) ::= REPLACE. {A = OE_Replace;}
497
drh348784e2000-05-29 20:41:49 +0000498
499%type itemlist {ExprList*}
500%destructor itemlist {sqliteExprListDelete($$);}
drh348784e2000-05-29 20:41:49 +0000501
drhe64e7b22002-02-18 13:56:36 +0000502itemlist(A) ::= itemlist(X) COMMA expr(Y). {A = sqliteExprListAppend(X,Y,0);}
503itemlist(A) ::= expr(X). {A = sqliteExprListAppend(0,X,0);}
drh348784e2000-05-29 20:41:49 +0000504
drh967e8b72000-06-21 13:59:10 +0000505%type inscollist_opt {IdList*}
506%destructor inscollist_opt {sqliteIdListDelete($$);}
507%type inscollist {IdList*}
508%destructor inscollist {sqliteIdListDelete($$);}
drh348784e2000-05-29 20:41:49 +0000509
drhc4a3c772001-04-04 11:48:57 +0000510inscollist_opt(A) ::= . {A = 0;}
511inscollist_opt(A) ::= LP inscollist(X) RP. {A = X;}
drh5ad1a6c2002-07-01 12:27:09 +0000512inscollist(A) ::= inscollist(X) COMMA nm(Y). {A = sqliteIdListAppend(X,&Y);}
513inscollist(A) ::= nm(Y). {A = sqliteIdListAppend(0,&Y);}
drh348784e2000-05-29 20:41:49 +0000514
drh382c0242001-10-06 16:33:02 +0000515/////////////////////////// Expression Processing /////////////////////////////
516//
drh348784e2000-05-29 20:41:49 +0000517%left OR.
518%left AND.
drh8be51132000-06-03 19:19:41 +0000519%right NOT.
drhfef52082000-06-06 01:50:43 +0000520%left EQ NE ISNULL NOTNULL IS LIKE GLOB BETWEEN IN.
drh348784e2000-05-29 20:41:49 +0000521%left GT GE LT LE.
drh81a20f22001-10-12 17:30:04 +0000522%left BITAND BITOR LSHIFT RSHIFT.
drh348784e2000-05-29 20:41:49 +0000523%left PLUS MINUS.
drhbf4133c2001-10-13 02:59:08 +0000524%left STAR SLASH REM.
drh00400772000-06-16 20:51:26 +0000525%left CONCAT.
drh4b59ab52002-08-24 18:24:51 +0000526%right UMINUS UPLUS BITNOT.
drh348784e2000-05-29 20:41:49 +0000527
528%type expr {Expr*}
529%destructor expr {sqliteExprDelete($$);}
530
drh6977fea2002-10-22 23:38:04 +0000531expr(A) ::= LP(B) expr(X) RP(E). {A = X; sqliteExprSpan(A,&B,&E); }
drhe1b6a5b2000-07-29 13:06:59 +0000532expr(A) ::= NULL(X). {A = sqliteExpr(TK_NULL, 0, 0, &X);}
drh5ad1a6c2002-07-01 12:27:09 +0000533expr(A) ::= ID(X). {A = sqliteExpr(TK_ID, 0, 0, &X);}
534expr(A) ::= JOIN_KW(X). {A = sqliteExpr(TK_ID, 0, 0, &X);}
535expr(A) ::= nm(X) DOT nm(Y). {
drhe1b6a5b2000-07-29 13:06:59 +0000536 Expr *temp1 = sqliteExpr(TK_ID, 0, 0, &X);
537 Expr *temp2 = sqliteExpr(TK_ID, 0, 0, &Y);
538 A = sqliteExpr(TK_DOT, temp1, temp2, 0);
539}
drhd24cc422003-03-27 12:51:24 +0000540expr(A) ::= nm(X) DOT nm(Y) DOT nm(Z). {
541 Expr *temp1 = sqliteExpr(TK_ID, 0, 0, &X);
542 Expr *temp2 = sqliteExpr(TK_ID, 0, 0, &Y);
543 Expr *temp3 = sqliteExpr(TK_ID, 0, 0, &Z);
544 Expr *temp4 = sqliteExpr(TK_DOT, temp2, temp3, 0);
545 A = sqliteExpr(TK_DOT, temp1, temp4, 0);
546}
drh348784e2000-05-29 20:41:49 +0000547expr(A) ::= INTEGER(X). {A = sqliteExpr(TK_INTEGER, 0, 0, &X);}
548expr(A) ::= FLOAT(X). {A = sqliteExpr(TK_FLOAT, 0, 0, &X);}
549expr(A) ::= STRING(X). {A = sqliteExpr(TK_STRING, 0, 0, &X);}
drh7c972de2003-09-06 22:18:07 +0000550expr(A) ::= VARIABLE(X). {
551 A = sqliteExpr(TK_VARIABLE, 0, 0, &X);
552 if( A ) A->iTable = ++pParse->nVar;
553}
drhe1b6a5b2000-07-29 13:06:59 +0000554expr(A) ::= ID(X) LP exprlist(Y) RP(E). {
555 A = sqliteExprFunction(Y, &X);
556 sqliteExprSpan(A,&X,&E);
557}
558expr(A) ::= ID(X) LP STAR RP(E). {
559 A = sqliteExprFunction(0, &X);
560 sqliteExprSpan(A,&X,&E);
561}
drh348784e2000-05-29 20:41:49 +0000562expr(A) ::= expr(X) AND expr(Y). {A = sqliteExpr(TK_AND, X, Y, 0);}
563expr(A) ::= expr(X) OR expr(Y). {A = sqliteExpr(TK_OR, X, Y, 0);}
564expr(A) ::= expr(X) LT expr(Y). {A = sqliteExpr(TK_LT, X, Y, 0);}
565expr(A) ::= expr(X) GT expr(Y). {A = sqliteExpr(TK_GT, X, Y, 0);}
566expr(A) ::= expr(X) LE expr(Y). {A = sqliteExpr(TK_LE, X, Y, 0);}
567expr(A) ::= expr(X) GE expr(Y). {A = sqliteExpr(TK_GE, X, Y, 0);}
568expr(A) ::= expr(X) NE expr(Y). {A = sqliteExpr(TK_NE, X, Y, 0);}
569expr(A) ::= expr(X) EQ expr(Y). {A = sqliteExpr(TK_EQ, X, Y, 0);}
drh81a20f22001-10-12 17:30:04 +0000570expr(A) ::= expr(X) BITAND expr(Y). {A = sqliteExpr(TK_BITAND, X, Y, 0);}
571expr(A) ::= expr(X) BITOR expr(Y). {A = sqliteExpr(TK_BITOR, X, Y, 0);}
572expr(A) ::= expr(X) LSHIFT expr(Y). {A = sqliteExpr(TK_LSHIFT, X, Y, 0);}
573expr(A) ::= expr(X) RSHIFT expr(Y). {A = sqliteExpr(TK_RSHIFT, X, Y, 0);}
drh0ac65892002-04-20 14:24:41 +0000574expr(A) ::= expr(X) likeop(OP) expr(Y). [LIKE] {
575 ExprList *pList = sqliteExprListAppend(0, Y, 0);
576 pList = sqliteExprListAppend(pList, X, 0);
drh4b59ab52002-08-24 18:24:51 +0000577 A = sqliteExprFunction(pList, 0);
578 if( A ) A->op = OP;
drh6977fea2002-10-22 23:38:04 +0000579 sqliteExprSpan(A, &X->span, &Y->span);
drh0ac65892002-04-20 14:24:41 +0000580}
581expr(A) ::= expr(X) NOT likeop(OP) expr(Y). [LIKE] {
582 ExprList *pList = sqliteExprListAppend(0, Y, 0);
583 pList = sqliteExprListAppend(pList, X, 0);
drh4b59ab52002-08-24 18:24:51 +0000584 A = sqliteExprFunction(pList, 0);
585 if( A ) A->op = OP;
drh4794b982000-06-06 13:54:14 +0000586 A = sqliteExpr(TK_NOT, A, 0, 0);
drh6977fea2002-10-22 23:38:04 +0000587 sqliteExprSpan(A,&X->span,&Y->span);
drh4794b982000-06-06 13:54:14 +0000588}
drh4b59ab52002-08-24 18:24:51 +0000589%type likeop {int}
590likeop(A) ::= LIKE. {A = TK_LIKE;}
591likeop(A) ::= GLOB. {A = TK_GLOB;}
drh348784e2000-05-29 20:41:49 +0000592expr(A) ::= expr(X) PLUS expr(Y). {A = sqliteExpr(TK_PLUS, X, Y, 0);}
593expr(A) ::= expr(X) MINUS expr(Y). {A = sqliteExpr(TK_MINUS, X, Y, 0);}
594expr(A) ::= expr(X) STAR expr(Y). {A = sqliteExpr(TK_STAR, X, Y, 0);}
595expr(A) ::= expr(X) SLASH expr(Y). {A = sqliteExpr(TK_SLASH, X, Y, 0);}
drhbf4133c2001-10-13 02:59:08 +0000596expr(A) ::= expr(X) REM expr(Y). {A = sqliteExpr(TK_REM, X, Y, 0);}
drh00400772000-06-16 20:51:26 +0000597expr(A) ::= expr(X) CONCAT expr(Y). {A = sqliteExpr(TK_CONCAT, X, Y, 0);}
drhe1b6a5b2000-07-29 13:06:59 +0000598expr(A) ::= expr(X) ISNULL(E). {
599 A = sqliteExpr(TK_ISNULL, X, 0, 0);
drh6977fea2002-10-22 23:38:04 +0000600 sqliteExprSpan(A,&X->span,&E);
drhe1b6a5b2000-07-29 13:06:59 +0000601}
drh33048c02001-10-01 14:29:22 +0000602expr(A) ::= expr(X) IS NULL(E). {
603 A = sqliteExpr(TK_ISNULL, X, 0, 0);
drh6977fea2002-10-22 23:38:04 +0000604 sqliteExprSpan(A,&X->span,&E);
drh33048c02001-10-01 14:29:22 +0000605}
drhe1b6a5b2000-07-29 13:06:59 +0000606expr(A) ::= expr(X) NOTNULL(E). {
607 A = sqliteExpr(TK_NOTNULL, X, 0, 0);
drh6977fea2002-10-22 23:38:04 +0000608 sqliteExprSpan(A,&X->span,&E);
drhe1b6a5b2000-07-29 13:06:59 +0000609}
drh33048c02001-10-01 14:29:22 +0000610expr(A) ::= expr(X) NOT NULL(E). {
611 A = sqliteExpr(TK_NOTNULL, X, 0, 0);
drh6977fea2002-10-22 23:38:04 +0000612 sqliteExprSpan(A,&X->span,&E);
drh33048c02001-10-01 14:29:22 +0000613}
drh81a20f22001-10-12 17:30:04 +0000614expr(A) ::= expr(X) IS NOT NULL(E). {
615 A = sqliteExpr(TK_NOTNULL, X, 0, 0);
drh6977fea2002-10-22 23:38:04 +0000616 sqliteExprSpan(A,&X->span,&E);
drh81a20f22001-10-12 17:30:04 +0000617}
drhe1b6a5b2000-07-29 13:06:59 +0000618expr(A) ::= NOT(B) expr(X). {
619 A = sqliteExpr(TK_NOT, X, 0, 0);
drh6977fea2002-10-22 23:38:04 +0000620 sqliteExprSpan(A,&B,&X->span);
drhe1b6a5b2000-07-29 13:06:59 +0000621}
drh81a20f22001-10-12 17:30:04 +0000622expr(A) ::= BITNOT(B) expr(X). {
623 A = sqliteExpr(TK_BITNOT, X, 0, 0);
drh6977fea2002-10-22 23:38:04 +0000624 sqliteExprSpan(A,&B,&X->span);
drh81a20f22001-10-12 17:30:04 +0000625}
drhe1b6a5b2000-07-29 13:06:59 +0000626expr(A) ::= MINUS(B) expr(X). [UMINUS] {
627 A = sqliteExpr(TK_UMINUS, X, 0, 0);
drh6977fea2002-10-22 23:38:04 +0000628 sqliteExprSpan(A,&B,&X->span);
drhe1b6a5b2000-07-29 13:06:59 +0000629}
drh4b59ab52002-08-24 18:24:51 +0000630expr(A) ::= PLUS(B) expr(X). [UPLUS] {
631 A = sqliteExpr(TK_UPLUS, X, 0, 0);
drh6977fea2002-10-22 23:38:04 +0000632 sqliteExprSpan(A,&B,&X->span);
drhe1b6a5b2000-07-29 13:06:59 +0000633}
634expr(A) ::= LP(B) select(X) RP(E). {
drh19a775c2000-06-05 18:54:46 +0000635 A = sqliteExpr(TK_SELECT, 0, 0, 0);
drhdaffd0e2001-04-11 14:28:42 +0000636 if( A ) A->pSelect = X;
drhe1b6a5b2000-07-29 13:06:59 +0000637 sqliteExprSpan(A,&B,&E);
drh19a775c2000-06-05 18:54:46 +0000638}
drhfef52082000-06-06 01:50:43 +0000639expr(A) ::= expr(W) BETWEEN expr(X) AND expr(Y). {
640 ExprList *pList = sqliteExprListAppend(0, X, 0);
641 pList = sqliteExprListAppend(pList, Y, 0);
642 A = sqliteExpr(TK_BETWEEN, W, 0, 0);
drhdaffd0e2001-04-11 14:28:42 +0000643 if( A ) A->pList = pList;
drh6977fea2002-10-22 23:38:04 +0000644 sqliteExprSpan(A,&W->span,&Y->span);
drhfef52082000-06-06 01:50:43 +0000645}
drh4794b982000-06-06 13:54:14 +0000646expr(A) ::= expr(W) NOT BETWEEN expr(X) AND expr(Y). {
647 ExprList *pList = sqliteExprListAppend(0, X, 0);
648 pList = sqliteExprListAppend(pList, Y, 0);
649 A = sqliteExpr(TK_BETWEEN, W, 0, 0);
drhdaffd0e2001-04-11 14:28:42 +0000650 if( A ) A->pList = pList;
drh4794b982000-06-06 13:54:14 +0000651 A = sqliteExpr(TK_NOT, A, 0, 0);
drh6977fea2002-10-22 23:38:04 +0000652 sqliteExprSpan(A,&W->span,&Y->span);
drh4794b982000-06-06 13:54:14 +0000653}
drhe1b6a5b2000-07-29 13:06:59 +0000654expr(A) ::= expr(X) IN LP exprlist(Y) RP(E). {
drhfef52082000-06-06 01:50:43 +0000655 A = sqliteExpr(TK_IN, X, 0, 0);
drhdaffd0e2001-04-11 14:28:42 +0000656 if( A ) A->pList = Y;
drh6977fea2002-10-22 23:38:04 +0000657 sqliteExprSpan(A,&X->span,&E);
drhfef52082000-06-06 01:50:43 +0000658}
drhe1b6a5b2000-07-29 13:06:59 +0000659expr(A) ::= expr(X) IN LP select(Y) RP(E). {
drhfef52082000-06-06 01:50:43 +0000660 A = sqliteExpr(TK_IN, X, 0, 0);
drhdaffd0e2001-04-11 14:28:42 +0000661 if( A ) A->pSelect = Y;
drh6977fea2002-10-22 23:38:04 +0000662 sqliteExprSpan(A,&X->span,&E);
drhfef52082000-06-06 01:50:43 +0000663}
drhe1b6a5b2000-07-29 13:06:59 +0000664expr(A) ::= expr(X) NOT IN LP exprlist(Y) RP(E). {
drh4794b982000-06-06 13:54:14 +0000665 A = sqliteExpr(TK_IN, X, 0, 0);
drhdaffd0e2001-04-11 14:28:42 +0000666 if( A ) A->pList = Y;
drh4794b982000-06-06 13:54:14 +0000667 A = sqliteExpr(TK_NOT, A, 0, 0);
drh6977fea2002-10-22 23:38:04 +0000668 sqliteExprSpan(A,&X->span,&E);
drh4794b982000-06-06 13:54:14 +0000669}
drhe1b6a5b2000-07-29 13:06:59 +0000670expr(A) ::= expr(X) NOT IN LP select(Y) RP(E). {
drh4794b982000-06-06 13:54:14 +0000671 A = sqliteExpr(TK_IN, X, 0, 0);
drhdaffd0e2001-04-11 14:28:42 +0000672 if( A ) A->pSelect = Y;
drh4794b982000-06-06 13:54:14 +0000673 A = sqliteExpr(TK_NOT, A, 0, 0);
drh6977fea2002-10-22 23:38:04 +0000674 sqliteExprSpan(A,&X->span,&E);
drh4794b982000-06-06 13:54:14 +0000675}
drh23b2db22004-01-15 03:30:24 +0000676expr(A) ::= expr(X) IN nm(Y) dbnm(D). {
677 SrcList *pSrc = sqliteSrcListAppend(0, &Y, &D);
drh23b2db22004-01-15 03:30:24 +0000678 A = sqliteExpr(TK_IN, X, 0, 0);
drhb733d032004-01-24 20:18:12 +0000679 if( A ) A->pSelect = sqliteSelectNew(0,pSrc,0,0,0,0,0,-1,0);
drh23b2db22004-01-15 03:30:24 +0000680 sqliteExprSpan(A,&X->span,D.z?&D:&Y);
681}
682expr(A) ::= expr(X) NOT IN nm(Y) dbnm(D). {
683 SrcList *pSrc = sqliteSrcListAppend(0, &Y, &D);
drh23b2db22004-01-15 03:30:24 +0000684 A = sqliteExpr(TK_IN, X, 0, 0);
drhb733d032004-01-24 20:18:12 +0000685 if( A ) A->pSelect = sqliteSelectNew(0,pSrc,0,0,0,0,0,-1,0);
drh23b2db22004-01-15 03:30:24 +0000686 A = sqliteExpr(TK_NOT, A, 0, 0);
687 sqliteExprSpan(A,&X->span,D.z?&D:&Y);
688}
689
drhfef52082000-06-06 01:50:43 +0000690
drh17a7f8d2002-03-24 13:13:27 +0000691/* CASE expressions */
692expr(A) ::= CASE(C) case_operand(X) case_exprlist(Y) case_else(Z) END(E). {
693 A = sqliteExpr(TK_CASE, X, Z, 0);
694 if( A ) A->pList = Y;
695 sqliteExprSpan(A, &C, &E);
696}
697%type case_exprlist {ExprList*}
698%destructor case_exprlist {sqliteExprListDelete($$);}
699case_exprlist(A) ::= case_exprlist(X) WHEN expr(Y) THEN expr(Z). {
700 A = sqliteExprListAppend(X, Y, 0);
701 A = sqliteExprListAppend(A, Z, 0);
702}
703case_exprlist(A) ::= WHEN expr(Y) THEN expr(Z). {
704 A = sqliteExprListAppend(0, Y, 0);
705 A = sqliteExprListAppend(A, Z, 0);
706}
707%type case_else {Expr*}
708case_else(A) ::= ELSE expr(X). {A = X;}
709case_else(A) ::= . {A = 0;}
710%type case_operand {Expr*}
711case_operand(A) ::= expr(X). {A = X;}
712case_operand(A) ::= . {A = 0;}
drh348784e2000-05-29 20:41:49 +0000713
714%type exprlist {ExprList*}
715%destructor exprlist {sqliteExprListDelete($$);}
716%type expritem {Expr*}
717%destructor expritem {sqliteExprDelete($$);}
718
drh348784e2000-05-29 20:41:49 +0000719exprlist(A) ::= exprlist(X) COMMA expritem(Y).
720 {A = sqliteExprListAppend(X,Y,0);}
721exprlist(A) ::= expritem(X). {A = sqliteExprListAppend(0,X,0);}
722expritem(A) ::= expr(X). {A = X;}
723expritem(A) ::= . {A = 0;}
drhcce7d172000-05-31 15:34:51 +0000724
drh382c0242001-10-06 16:33:02 +0000725///////////////////////////// The CREATE INDEX command ///////////////////////
726//
drh4925ca02003-11-27 00:48:57 +0000727cmd ::= CREATE(S) uniqueflag(U) INDEX nm(X)
drhd24cc422003-03-27 12:51:24 +0000728 ON nm(Y) dbnm(D) LP idxlist(Z) RP(E) onconf(R). {
729 SrcList *pSrc = sqliteSrcListAppend(0, &Y, &D);
drh9cfcf5d2002-01-29 18:41:24 +0000730 if( U!=OE_None ) U = R;
731 if( U==OE_Default) U = OE_Abort;
drh4925ca02003-11-27 00:48:57 +0000732 sqliteCreateIndex(pParse, &X, pSrc, Z, U, &S, &E);
drh9cfcf5d2002-01-29 18:41:24 +0000733}
drh717e6402001-09-27 03:22:32 +0000734
735%type uniqueflag {int}
drh9cfcf5d2002-01-29 18:41:24 +0000736uniqueflag(A) ::= UNIQUE. { A = OE_Abort; }
737uniqueflag(A) ::= . { A = OE_None; }
drh348784e2000-05-29 20:41:49 +0000738
739%type idxlist {IdList*}
740%destructor idxlist {sqliteIdListDelete($$);}
drhc2eef3b2002-08-31 18:53:06 +0000741%type idxlist_opt {IdList*}
742%destructor idxlist_opt {sqliteIdListDelete($$);}
drh348784e2000-05-29 20:41:49 +0000743%type idxitem {Token}
744
drhc2eef3b2002-08-31 18:53:06 +0000745idxlist_opt(A) ::= . {A = 0;}
746idxlist_opt(A) ::= LP idxlist(X) RP. {A = X;}
747idxlist(A) ::= idxlist(X) COMMA idxitem(Y). {A = sqliteIdListAppend(X,&Y);}
748idxlist(A) ::= idxitem(Y). {A = sqliteIdListAppend(0,&Y);}
drh86e5cc02003-04-29 17:19:18 +0000749idxitem(A) ::= nm(X) sortorder. {A = X;}
drh348784e2000-05-29 20:41:49 +0000750
drh8aff1012001-12-22 14:49:24 +0000751///////////////////////////// The DROP INDEX command /////////////////////////
drh382c0242001-10-06 16:33:02 +0000752//
753
drhd24cc422003-03-27 12:51:24 +0000754cmd ::= DROP INDEX nm(X) dbnm(Y). {
755 sqliteDropIndex(pParse, sqliteSrcListAppend(0,&X,&Y));
756}
drh982cef72000-05-30 16:27:03 +0000757
drh382c0242001-10-06 16:33:02 +0000758
drh8aff1012001-12-22 14:49:24 +0000759///////////////////////////// The COPY command ///////////////////////////////
drh382c0242001-10-06 16:33:02 +0000760//
drhd24cc422003-03-27 12:51:24 +0000761cmd ::= COPY orconf(R) nm(X) dbnm(D) FROM nm(Y) USING DELIMITERS STRING(Z).
762 {sqliteCopy(pParse,sqliteSrcListAppend(0,&X,&D),&Y,&Z,R);}
763cmd ::= COPY orconf(R) nm(X) dbnm(D) FROM nm(Y).
764 {sqliteCopy(pParse,sqliteSrcListAppend(0,&X,&D),&Y,0,R);}
drhdce2cbe2000-05-31 02:27:49 +0000765
drh382c0242001-10-06 16:33:02 +0000766///////////////////////////// The VACUUM command /////////////////////////////
767//
drhdce2cbe2000-05-31 02:27:49 +0000768cmd ::= VACUUM. {sqliteVacuum(pParse,0);}
drh5ad1a6c2002-07-01 12:27:09 +0000769cmd ::= VACUUM nm(X). {sqliteVacuum(pParse,&X);}
drhf57b14a2001-09-14 18:54:08 +0000770
drh382c0242001-10-06 16:33:02 +0000771///////////////////////////// The PRAGMA command /////////////////////////////
772//
drh5ad1a6c2002-07-01 12:27:09 +0000773cmd ::= PRAGMA ids(X) EQ nm(Y). {sqlitePragma(pParse,&X,&Y,0);}
drhf57b14a2001-09-14 18:54:08 +0000774cmd ::= PRAGMA ids(X) EQ ON(Y). {sqlitePragma(pParse,&X,&Y,0);}
775cmd ::= PRAGMA ids(X) EQ plus_num(Y). {sqlitePragma(pParse,&X,&Y,0);}
776cmd ::= PRAGMA ids(X) EQ minus_num(Y). {sqlitePragma(pParse,&X,&Y,1);}
drh5ad1a6c2002-07-01 12:27:09 +0000777cmd ::= PRAGMA ids(X) LP nm(Y) RP. {sqlitePragma(pParse,&X,&Y,0);}
drh603240c2002-03-05 01:11:12 +0000778cmd ::= PRAGMA ids(X). {sqlitePragma(pParse,&X,&X,0);}
drhf57b14a2001-09-14 18:54:08 +0000779plus_num(A) ::= plus_opt number(X). {A = X;}
780minus_num(A) ::= MINUS number(X). {A = X;}
781number(A) ::= INTEGER(X). {A = X;}
782number(A) ::= FLOAT(X). {A = X;}
783plus_opt ::= PLUS.
784plus_opt ::= .
danielk1977c3f9bad2002-05-15 08:30:12 +0000785
786//////////////////////////// The CREATE TRIGGER command /////////////////////
drhf0f258b2003-04-21 18:48:45 +0000787
788cmd ::= CREATE(A) trigger_decl BEGIN trigger_cmd_list(S) END(Z). {
drh4b59ab52002-08-24 18:24:51 +0000789 Token all;
790 all.z = A.z;
791 all.n = (Z.z - A.z) + Z.n;
drhf0f258b2003-04-21 18:48:45 +0000792 sqliteFinishTrigger(pParse, S, &all);
793}
794
795trigger_decl ::= temp(T) TRIGGER nm(B) trigger_time(C) trigger_event(D)
796 ON nm(E) dbnm(DB) foreach_clause(F) when_clause(G). {
797 SrcList *pTab = sqliteSrcListAppend(0, &E, &DB);
798 sqliteBeginTrigger(pParse, &B, C, D.a, D.b, pTab, F, G, T);
danielk1977c3f9bad2002-05-15 08:30:12 +0000799}
800
801%type trigger_time {int}
802trigger_time(A) ::= BEFORE. { A = TK_BEFORE; }
803trigger_time(A) ::= AFTER. { A = TK_AFTER; }
804trigger_time(A) ::= INSTEAD OF. { A = TK_INSTEAD;}
805trigger_time(A) ::= . { A = TK_BEFORE; }
806
drhad3cab52002-05-24 02:04:32 +0000807%type trigger_event {struct TrigEvent}
808%destructor trigger_event {sqliteIdListDelete($$.b);}
danielk1977c3f9bad2002-05-15 08:30:12 +0000809trigger_event(A) ::= DELETE. { A.a = TK_DELETE; A.b = 0; }
810trigger_event(A) ::= INSERT. { A.a = TK_INSERT; A.b = 0; }
811trigger_event(A) ::= UPDATE. { A.a = TK_UPDATE; A.b = 0;}
812trigger_event(A) ::= UPDATE OF inscollist(X). {A.a = TK_UPDATE; A.b = X; }
813
814%type foreach_clause {int}
815foreach_clause(A) ::= . { A = TK_ROW; }
816foreach_clause(A) ::= FOR EACH ROW. { A = TK_ROW; }
817foreach_clause(A) ::= FOR EACH STATEMENT. { A = TK_STATEMENT; }
818
819%type when_clause {Expr *}
820when_clause(A) ::= . { A = 0; }
821when_clause(A) ::= WHEN expr(X). { A = X; }
822
823%type trigger_cmd_list {TriggerStep *}
drhf0f258b2003-04-21 18:48:45 +0000824%destructor trigger_cmd_list {sqliteDeleteTriggerStep($$);}
danielk1977c3f9bad2002-05-15 08:30:12 +0000825trigger_cmd_list(A) ::= trigger_cmd(X) SEMI trigger_cmd_list(Y). {
drha69d9162003-04-17 22:57:53 +0000826 X->pNext = Y;
827 A = X;
828}
danielk1977c3f9bad2002-05-15 08:30:12 +0000829trigger_cmd_list(A) ::= . { A = 0; }
830
831%type trigger_cmd {TriggerStep *}
drhf0f258b2003-04-21 18:48:45 +0000832%destructor trigger_cmd {sqliteDeleteTriggerStep($$);}
danielk1977c3f9bad2002-05-15 08:30:12 +0000833// UPDATE
drh5ad1a6c2002-07-01 12:27:09 +0000834trigger_cmd(A) ::= UPDATE orconf(R) nm(X) SET setlist(Y) where_opt(Z).
danielk1977c3f9bad2002-05-15 08:30:12 +0000835 { A = sqliteTriggerUpdateStep(&X, Y, Z, R); }
836
837// INSERT
drh3054efe2004-02-12 17:28:13 +0000838trigger_cmd(A) ::= insert_cmd(R) INTO nm(X) inscollist_opt(F)
danielk1977c3f9bad2002-05-15 08:30:12 +0000839 VALUES LP itemlist(Y) RP.
840{A = sqliteTriggerInsertStep(&X, F, Y, 0, R);}
841
drh3054efe2004-02-12 17:28:13 +0000842trigger_cmd(A) ::= insert_cmd(R) INTO nm(X) inscollist_opt(F) select(S).
danielk1977c3f9bad2002-05-15 08:30:12 +0000843 {A = sqliteTriggerInsertStep(&X, F, 0, S, R);}
844
845// DELETE
drh5ad1a6c2002-07-01 12:27:09 +0000846trigger_cmd(A) ::= DELETE FROM nm(X) where_opt(Y).
danielk1977c3f9bad2002-05-15 08:30:12 +0000847 {A = sqliteTriggerDeleteStep(&X, Y);}
848
849// SELECT
850trigger_cmd(A) ::= select(X). {A = sqliteTriggerSelectStep(X); }
851
danielk19776f349032002-06-11 02:25:40 +0000852// The special RAISE expression that may occur in trigger programs
drh4b59ab52002-08-24 18:24:51 +0000853expr(A) ::= RAISE(X) LP IGNORE RP(Y). {
854 A = sqliteExpr(TK_RAISE, 0, 0, 0);
855 A->iColumn = OE_Ignore;
drh6977fea2002-10-22 23:38:04 +0000856 sqliteExprSpan(A, &X, &Y);
drh4b59ab52002-08-24 18:24:51 +0000857}
858expr(A) ::= RAISE(X) LP ROLLBACK COMMA nm(Z) RP(Y). {
859 A = sqliteExpr(TK_RAISE, 0, 0, &Z);
860 A->iColumn = OE_Rollback;
drh6977fea2002-10-22 23:38:04 +0000861 sqliteExprSpan(A, &X, &Y);
drh4b59ab52002-08-24 18:24:51 +0000862}
863expr(A) ::= RAISE(X) LP ABORT COMMA nm(Z) RP(Y). {
864 A = sqliteExpr(TK_RAISE, 0, 0, &Z);
865 A->iColumn = OE_Abort;
drh6977fea2002-10-22 23:38:04 +0000866 sqliteExprSpan(A, &X, &Y);
drh4b59ab52002-08-24 18:24:51 +0000867}
868expr(A) ::= RAISE(X) LP FAIL COMMA nm(Z) RP(Y). {
869 A = sqliteExpr(TK_RAISE, 0, 0, &Z);
870 A->iColumn = OE_Fail;
drh6977fea2002-10-22 23:38:04 +0000871 sqliteExprSpan(A, &X, &Y);
drh4b59ab52002-08-24 18:24:51 +0000872}
danielk19776f349032002-06-11 02:25:40 +0000873
danielk1977c3f9bad2002-05-15 08:30:12 +0000874//////////////////////// DROP TRIGGER statement //////////////////////////////
drhd24cc422003-03-27 12:51:24 +0000875cmd ::= DROP TRIGGER nm(X) dbnm(D). {
drh79a519c2003-05-17 19:04:03 +0000876 sqliteDropTrigger(pParse,sqliteSrcListAppend(0,&X,&D));
danielk1977c3f9bad2002-05-15 08:30:12 +0000877}
drh113088e2003-03-20 01:16:58 +0000878
879//////////////////////// ATTACH DATABASE file AS name /////////////////////////
drh4d189ca2004-02-12 18:46:38 +0000880cmd ::= ATTACH database_kw_opt ids(F) AS nm(D) key_opt(K). {
881 sqliteAttach(pParse, &F, &D, &K);
drh1c2d8412003-03-31 00:30:47 +0000882}
drh4d189ca2004-02-12 18:46:38 +0000883%type key_opt {Token}
884key_opt(A) ::= USING ids(X). { A = X; }
885key_opt(A) ::= . { A.z = 0; A.n = 0; }
drh113088e2003-03-20 01:16:58 +0000886
887database_kw_opt ::= DATABASE.
888database_kw_opt ::= .
889
890//////////////////////// DETACH DATABASE name /////////////////////////////////
drh1c2d8412003-03-31 00:30:47 +0000891cmd ::= DETACH database_kw_opt nm(D). {
892 sqliteDetach(pParse, &D);
893}