blob: e9f9fc336c99f15f2de6b136b734c67b7cad59d7 [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**
drh74ad7fe2004-10-07 03:06:28 +000017** @(#) $Id: parse.y,v 1.144 2004/10/07 03:06:29 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] ){
danielk19774adee202004-05-08 08:23:19 +000026 sqlite3ErrorMsg(pParse, "near \"%T\": syntax error", &TOKEN);
drhb86ccfb2003-01-28 23:13:10 +000027 }else{
danielk19774adee202004-05-08 08:23:19 +000028 sqlite3ErrorMsg(pParse, "incomplete SQL statement");
drhb86ccfb2003-01-28 23:13:10 +000029 }
30 }
drh348784e2000-05-29 20:41:49 +000031}
danielk19774adee202004-05-08 08:23:19 +000032%name sqlite3Parser
drh348784e2000-05-29 20:41:49 +000033%include {
34#include "sqliteInt.h"
35#include "parse.h"
drh9bbca4c2001-11-06 04:00:18 +000036
37/*
drhad3cab52002-05-24 02:04:32 +000038** An instance of this structure holds information about the
39** LIMIT clause of a SELECT statement.
drh9bbca4c2001-11-06 04:00:18 +000040*/
drhad3cab52002-05-24 02:04:32 +000041struct LimitVal {
42 int limit; /* The LIMIT value. -1 if there is no limit */
43 int offset; /* The OFFSET. 0 if there is none */
44};
danielk1977c3f9bad2002-05-15 08:30:12 +000045
46/*
drh2e3a1f12004-10-06 14:39:28 +000047** An instance of this structure is used to store the LIKE,
48** GLOB, NOT LIKE, and NOT GLOB operators.
49*/
50struct LikeOp {
51 int opcode; /* Either TK_GLOB or TK_LIKE */
52 int not; /* True if the NOT keyword is present */
53};
54
55/*
drhad3cab52002-05-24 02:04:32 +000056** An instance of the following structure describes the event of a
57** TRIGGER. "a" is the event type, one of TK_UPDATE, TK_INSERT,
58** TK_DELETE, or TK_INSTEAD. If the event is of the form
59**
60** UPDATE ON (a,b,c)
61**
62** Then the "b" IdList records the list "a,b,c".
danielk1977c3f9bad2002-05-15 08:30:12 +000063*/
drhad3cab52002-05-24 02:04:32 +000064struct TrigEvent { int a; IdList * b; };
drhcaec2f12003-01-07 02:47:47 +000065
drh25d65432004-07-22 15:02:25 +000066/*
67** An instance of this structure holds the ATTACH key and the key type.
68*/
69struct AttachKey { int type; Token key; };
70
drhcaec2f12003-01-07 02:47:47 +000071} // end %include
drh348784e2000-05-29 20:41:49 +000072
drh348784e2000-05-29 20:41:49 +000073// These are extra tokens used by the lexer but never seen by the
74// parser. We put them in a rule so that the parser generator will
drh6206d502000-06-19 19:09:08 +000075// add them to the parse.h output file.
drh348784e2000-05-29 20:41:49 +000076//
drhc4a3c772001-04-04 11:48:57 +000077%nonassoc END_OF_FILE ILLEGAL SPACE UNCLOSED_STRING COMMENT FUNCTION
78 COLUMN AGG_FUNCTION.
79
drh826fb5a2004-02-14 23:59:57 +000080// Input is a single SQL command
drhc4a3c772001-04-04 11:48:57 +000081input ::= cmdlist.
drh094b2bb2002-03-13 18:54:07 +000082cmdlist ::= cmdlist ecmd.
drh826fb5a2004-02-14 23:59:57 +000083cmdlist ::= ecmd.
drh483750b2003-01-29 18:46:51 +000084ecmd ::= explain cmdx SEMI.
drh094b2bb2002-03-13 18:54:07 +000085ecmd ::= SEMI.
drh80242052004-06-09 00:48:12 +000086cmdx ::= cmd. { sqlite3FinishCoding(pParse); }
danielk19774adee202004-05-08 08:23:19 +000087explain ::= EXPLAIN. { sqlite3BeginParse(pParse, 1); }
88explain ::= . { sqlite3BeginParse(pParse, 0); }
drh348784e2000-05-29 20:41:49 +000089
drh382c0242001-10-06 16:33:02 +000090///////////////////// Begin and end transactions. ////////////////////////////
drhc4a3c772001-04-04 11:48:57 +000091//
drhfa86c412002-02-02 15:01:15 +000092
drh684917c2004-10-05 02:41:42 +000093cmd ::= BEGIN transtype(Y) trans_opt. {sqlite3BeginTransaction(pParse, Y);}
drhc4a3c772001-04-04 11:48:57 +000094trans_opt ::= .
95trans_opt ::= TRANSACTION.
drh5ad1a6c2002-07-01 12:27:09 +000096trans_opt ::= TRANSACTION nm.
drh684917c2004-10-05 02:41:42 +000097%type transtype {int}
98transtype(A) ::= . {A = TK_DEFERRED;}
99transtype(A) ::= DEFERRED(X). {A = @X;}
100transtype(A) ::= IMMEDIATE(X). {A = @X;}
101transtype(A) ::= EXCLUSIVE(X). {A = @X;}
danielk19774adee202004-05-08 08:23:19 +0000102cmd ::= COMMIT trans_opt. {sqlite3CommitTransaction(pParse);}
103cmd ::= END trans_opt. {sqlite3CommitTransaction(pParse);}
104cmd ::= ROLLBACK trans_opt. {sqlite3RollbackTransaction(pParse);}
drhc4a3c772001-04-04 11:48:57 +0000105
drh382c0242001-10-06 16:33:02 +0000106///////////////////// The CREATE TABLE statement ////////////////////////////
drh348784e2000-05-29 20:41:49 +0000107//
108cmd ::= create_table create_table_args.
danielk1977cbb18d22004-05-28 11:37:27 +0000109create_table ::= CREATE(X) temp(T) TABLE nm(Y) dbnm(Z). {
110 sqlite3StartTable(pParse,&X,&Y,&Z,T,0);
drh969fa7c2002-02-18 18:30:32 +0000111}
drhf57b3392001-10-08 13:22:32 +0000112%type temp {int}
drhd24cc422003-03-27 12:51:24 +0000113temp(A) ::= TEMP. {A = 1;}
114temp(A) ::= . {A = 0;}
drh969fa7c2002-02-18 18:30:32 +0000115create_table_args ::= LP columnlist conslist_opt RP(X). {
danielk19774adee202004-05-08 08:23:19 +0000116 sqlite3EndTable(pParse,&X,0);
drh969fa7c2002-02-18 18:30:32 +0000117}
118create_table_args ::= AS select(S). {
danielk19774adee202004-05-08 08:23:19 +0000119 sqlite3EndTable(pParse,0,S);
120 sqlite3SelectDelete(S);
drh969fa7c2002-02-18 18:30:32 +0000121}
drh348784e2000-05-29 20:41:49 +0000122columnlist ::= columnlist COMMA column.
123columnlist ::= column.
124
125// About the only information used for a column is the name of the
126// column. The type is always just "text". But the code will accept
127// an elaborate typename. Perhaps someday we'll do something with it.
128//
129column ::= columnid type carglist.
danielk19774adee202004-05-08 08:23:19 +0000130columnid ::= nm(X). {sqlite3AddColumn(pParse,&X);}
drhc4a3c772001-04-04 11:48:57 +0000131
132// An IDENTIFIER can be a generic identifier, or one of several
133// keywords. Any non-standard keyword can also be an identifier.
drhc4a3c772001-04-04 11:48:57 +0000134//
drh982cef72000-05-30 16:27:03 +0000135%type id {Token}
drhf18543c2002-03-30 15:26:50 +0000136id(A) ::= ID(X). {A = X;}
drh0bd1f4e2002-06-06 18:54:39 +0000137
drh34e33bb2002-06-06 19:04:16 +0000138// The following directive causes tokens ABORT, AFTER, ASC, etc. to
139// fallback to ID if they will not parse as their original value.
140// This obviates the need for the "id" nonterminal.
141//
drh319e4e72003-09-30 01:54:13 +0000142%fallback ID
drh10e82662004-09-25 15:29:09 +0000143 ABORT AFTER ASC ATTACH BEFORE BEGIN CASCADE CONFLICT
drh684917c2004-10-05 02:41:42 +0000144 DATABASE DEFERRED DESC DETACH EACH END EXCLUSIVE EXPLAIN FAIL FOR
drh319e4e72003-09-30 01:54:13 +0000145 GLOB IGNORE IMMEDIATE INITIALLY INSTEAD LIKE MATCH KEY
drh5ad1a6c2002-07-01 12:27:09 +0000146 OF OFFSET PRAGMA RAISE REPLACE RESTRICT ROW STATEMENT
drh0bd1f4e2002-06-06 18:54:39 +0000147 TEMP TRIGGER VACUUM VIEW.
drhc4a3c772001-04-04 11:48:57 +0000148
drh2d3917d2004-02-22 16:27:00 +0000149// Define operator precedence early so that this is the first occurance
150// of the operator tokens in the grammer. Keeping the operators together
151// causes them to be assigned integer values that are close together,
152// which keeps parser tables smaller.
153//
drhf2bc0132004-10-04 13:19:23 +0000154// The token values assigned to these symbols is determined by the order
155// in which lemon first sees them. It must be the case that ISNULL/NOTNULL,
156// NE/EQ, GT/LE, and GE/LT are separated by only a single value. See
157// the sqlite3ExprIfFalse() routine for additional information on this
158// constraint.
159//
drh2d3917d2004-02-22 16:27:00 +0000160%left OR.
161%left AND.
162%right NOT.
drhf2bc0132004-10-04 13:19:23 +0000163%left IS LIKE GLOB BETWEEN IN ISNULL NOTNULL NE EQ.
drh9a432672004-10-04 13:38:09 +0000164%left GT LE LT GE.
drh2d3917d2004-02-22 16:27:00 +0000165%left BITAND BITOR LSHIFT RSHIFT.
166%left PLUS MINUS.
167%left STAR SLASH REM.
168%left CONCAT.
169%right UMINUS UPLUS BITNOT.
170
drhc4a3c772001-04-04 11:48:57 +0000171// And "ids" is an identifer-or-string.
172//
173%type ids {Token}
drh5ad1a6c2002-07-01 12:27:09 +0000174ids(A) ::= ID(X). {A = X;}
drhc4a3c772001-04-04 11:48:57 +0000175ids(A) ::= STRING(X). {A = X;}
176
drh5ad1a6c2002-07-01 12:27:09 +0000177// The name of a column or table can be any of the following:
178//
179%type nm {Token}
180nm(A) ::= ID(X). {A = X;}
181nm(A) ::= STRING(X). {A = X;}
182nm(A) ::= JOIN_KW(X). {A = X;}
183
drh382c0242001-10-06 16:33:02 +0000184type ::= .
danielk19774adee202004-05-08 08:23:19 +0000185type ::= typename(X). {sqlite3AddColumnType(pParse,&X,&X);}
186type ::= typename(X) LP signed RP(Y). {sqlite3AddColumnType(pParse,&X,&Y);}
drh382c0242001-10-06 16:33:02 +0000187type ::= typename(X) LP signed COMMA signed RP(Y).
danielk19774adee202004-05-08 08:23:19 +0000188 {sqlite3AddColumnType(pParse,&X,&Y);}
drh382c0242001-10-06 16:33:02 +0000189%type typename {Token}
drhe2ea40d2004-05-20 12:41:19 +0000190typename(A) ::= ids(X). {A = X;}
drh596bd232004-09-30 14:22:47 +0000191typename(A) ::= typename(X) ids(Y). {A.z=X.z; A.n=Y.n+(Y.z-X.z);}
drhef0cae52003-07-16 02:19:37 +0000192%type signed {int}
drh74ad7fe2004-10-07 03:06:28 +0000193signed(A) ::= plus_num(X). { A = atoi(X.z); }
194signed(A) ::= minus_num(X). { A = -atoi(X.z); }
drh348784e2000-05-29 20:41:49 +0000195carglist ::= carglist carg.
196carglist ::= .
drh5ad1a6c2002-07-01 12:27:09 +0000197carg ::= CONSTRAINT nm ccons.
drh348784e2000-05-29 20:41:49 +0000198carg ::= ccons.
drh74ad7fe2004-10-07 03:06:28 +0000199carg ::= DEFAULT ids(X). {sqlite3AddDefaultValue(pParse,&X,0);}
200carg ::= DEFAULT plus_num(X). {sqlite3AddDefaultValue(pParse,&X,0);}
201carg ::= DEFAULT minus_num(X). {sqlite3AddDefaultValue(pParse,&X,1);}
drh7020f652000-06-03 18:06:52 +0000202carg ::= DEFAULT NULL.
drh348784e2000-05-29 20:41:49 +0000203
drh382c0242001-10-06 16:33:02 +0000204// In addition to the type name, we also care about the primary key and
205// UNIQUE constraints.
drh348784e2000-05-29 20:41:49 +0000206//
drh0d316a42002-08-11 20:10:47 +0000207ccons ::= NULL onconf.
danielk19774adee202004-05-08 08:23:19 +0000208ccons ::= NOT NULL onconf(R). {sqlite3AddNotNull(pParse, R);}
209ccons ::= PRIMARY KEY sortorder onconf(R). {sqlite3AddPrimaryKey(pParse,0,R);}
drh74ad7fe2004-10-07 03:06:28 +0000210ccons ::= UNIQUE onconf(R). {sqlite3CreateIndex(pParse,0,0,0,0,R,0,0);}
drh9cfcf5d2002-01-29 18:41:24 +0000211ccons ::= CHECK LP expr RP onconf.
drhc2eef3b2002-08-31 18:53:06 +0000212ccons ::= REFERENCES nm(T) idxlist_opt(TA) refargs(R).
danielk19774adee202004-05-08 08:23:19 +0000213 {sqlite3CreateForeignKey(pParse,0,&T,TA,R);}
214ccons ::= defer_subclause(D). {sqlite3DeferForeignKey(pParse,D);}
drhd3d39e92004-05-20 22:16:29 +0000215ccons ::= COLLATE id(C). {sqlite3AddCollateType(pParse, C.z, C.n);}
drh04738cb2002-06-02 18:19:00 +0000216
drhc2eef3b2002-08-31 18:53:06 +0000217// The next group of rules parses the arguments to a REFERENCES clause
218// that determine if the referential integrity checking is deferred or
219// or immediate and which determine what action to take if a ref-integ
220// check fails.
drh04738cb2002-06-02 18:19:00 +0000221//
drhc2eef3b2002-08-31 18:53:06 +0000222%type refargs {int}
223refargs(A) ::= . { A = OE_Restrict * 0x010101; }
224refargs(A) ::= refargs(X) refarg(Y). { A = (X & Y.mask) | Y.value; }
225%type refarg {struct {int value; int mask;}}
226refarg(A) ::= MATCH nm. { A.value = 0; A.mask = 0x000000; }
227refarg(A) ::= ON DELETE refact(X). { A.value = X; A.mask = 0x0000ff; }
228refarg(A) ::= ON UPDATE refact(X). { A.value = X<<8; A.mask = 0x00ff00; }
229refarg(A) ::= ON INSERT refact(X). { A.value = X<<16; A.mask = 0xff0000; }
230%type refact {int}
231refact(A) ::= SET NULL. { A = OE_SetNull; }
232refact(A) ::= SET DEFAULT. { A = OE_SetDflt; }
233refact(A) ::= CASCADE. { A = OE_Cascade; }
234refact(A) ::= RESTRICT. { A = OE_Restrict; }
235%type defer_subclause {int}
236defer_subclause(A) ::= NOT DEFERRABLE init_deferred_pred_opt(X). {A = X;}
237defer_subclause(A) ::= DEFERRABLE init_deferred_pred_opt(X). {A = X;}
238%type init_deferred_pred_opt {int}
239init_deferred_pred_opt(A) ::= . {A = 0;}
240init_deferred_pred_opt(A) ::= INITIALLY DEFERRED. {A = 1;}
241init_deferred_pred_opt(A) ::= INITIALLY IMMEDIATE. {A = 0;}
drh348784e2000-05-29 20:41:49 +0000242
243// For the time being, the only constraint we care about is the primary
drh382c0242001-10-06 16:33:02 +0000244// key and UNIQUE. Both create indices.
drh348784e2000-05-29 20:41:49 +0000245//
246conslist_opt ::= .
247conslist_opt ::= COMMA conslist.
248conslist ::= conslist COMMA tcons.
drha2e1bb52001-01-04 14:20:18 +0000249conslist ::= conslist tcons.
drh348784e2000-05-29 20:41:49 +0000250conslist ::= tcons.
drh5ad1a6c2002-07-01 12:27:09 +0000251tcons ::= CONSTRAINT nm.
drh9cfcf5d2002-01-29 18:41:24 +0000252tcons ::= PRIMARY KEY LP idxlist(X) RP onconf(R).
danielk19774adee202004-05-08 08:23:19 +0000253 {sqlite3AddPrimaryKey(pParse,X,R);}
drh9cfcf5d2002-01-29 18:41:24 +0000254tcons ::= UNIQUE LP idxlist(X) RP onconf(R).
danielk1977cbb18d22004-05-28 11:37:27 +0000255 {sqlite3CreateIndex(pParse,0,0,0,X,R,0,0);}
drh9cfcf5d2002-01-29 18:41:24 +0000256tcons ::= CHECK expr onconf.
drhc2eef3b2002-08-31 18:53:06 +0000257tcons ::= FOREIGN KEY LP idxlist(FA) RP
258 REFERENCES nm(T) idxlist_opt(TA) refargs(R) defer_subclause_opt(D). {
danielk19774adee202004-05-08 08:23:19 +0000259 sqlite3CreateForeignKey(pParse, FA, &T, TA, R);
260 sqlite3DeferForeignKey(pParse, D);
drhc2eef3b2002-08-31 18:53:06 +0000261}
262%type defer_subclause_opt {int}
263defer_subclause_opt(A) ::= . {A = 0;}
264defer_subclause_opt(A) ::= defer_subclause(X). {A = X;}
drh9cfcf5d2002-01-29 18:41:24 +0000265
266// The following is a non-standard extension that allows us to declare the
267// default behavior when there is a constraint conflict.
268//
269%type onconf {int}
drh1c928532002-01-31 15:54:21 +0000270%type orconf {int}
271%type resolvetype {int}
drh74ad7fe2004-10-07 03:06:28 +0000272onconf(A) ::= . {A = OE_Default;}
273onconf(A) ::= ON CONFLICT resolvetype(X). {A = X;}
274orconf(A) ::= . {A = OE_Default;}
275orconf(A) ::= OR resolvetype(X). {A = X;}
276resolvetype(A) ::= raisetype(X). {A = X;}
277resolvetype(A) ::= IGNORE. {A = OE_Ignore;}
278resolvetype(A) ::= REPLACE. {A = OE_Replace;}
drh348784e2000-05-29 20:41:49 +0000279
drh382c0242001-10-06 16:33:02 +0000280////////////////////////// The DROP TABLE /////////////////////////////////////
drh348784e2000-05-29 20:41:49 +0000281//
drh74ad7fe2004-10-07 03:06:28 +0000282cmd ::= DROP TABLE fullname(X). {
283 sqlite3DropTable(pParse, X, 0);
danielk1977a8858102004-05-28 12:11:21 +0000284}
drh348784e2000-05-29 20:41:49 +0000285
drha76b5df2002-02-23 02:32:10 +0000286///////////////////// The CREATE VIEW statement /////////////////////////////
287//
danielk197748dec7e2004-05-28 12:33:30 +0000288cmd ::= CREATE(X) temp(T) VIEW nm(Y) dbnm(Z) AS select(S). {
289 sqlite3CreateView(pParse, &X, &Y, &Z, S, T);
drha76b5df2002-02-23 02:32:10 +0000290}
drh74ad7fe2004-10-07 03:06:28 +0000291cmd ::= DROP VIEW fullname(X). {
292 sqlite3DropTable(pParse, X, 1);
drha76b5df2002-02-23 02:32:10 +0000293}
294
drh382c0242001-10-06 16:33:02 +0000295//////////////////////// The SELECT statement /////////////////////////////////
drh348784e2000-05-29 20:41:49 +0000296//
drh9bb61fe2000-06-05 16:01:39 +0000297cmd ::= select(X). {
danielk1977bf3b7212004-05-18 10:06:24 +0000298 sqlite3Select(pParse, X, SRT_Callback, 0, 0, 0, 0, 0);
danielk19774adee202004-05-08 08:23:19 +0000299 sqlite3SelectDelete(X);
drh9bb61fe2000-06-05 16:01:39 +0000300}
drhefb72512000-05-31 20:00:52 +0000301
drh9bb61fe2000-06-05 16:01:39 +0000302%type select {Select*}
danielk19774adee202004-05-08 08:23:19 +0000303%destructor select {sqlite3SelectDelete($$);}
drh82c3d632000-06-06 21:56:07 +0000304%type oneselect {Select*}
danielk19774adee202004-05-08 08:23:19 +0000305%destructor oneselect {sqlite3SelectDelete($$);}
drh9bb61fe2000-06-05 16:01:39 +0000306
drh82c3d632000-06-06 21:56:07 +0000307select(A) ::= oneselect(X). {A = X;}
drh0a36c572002-02-18 22:49:59 +0000308select(A) ::= select(X) multiselect_op(Y) oneselect(Z). {
drhdaffd0e2001-04-11 14:28:42 +0000309 if( Z ){
drh82c3d632000-06-06 21:56:07 +0000310 Z->op = Y;
311 Z->pPrior = X;
drhdaffd0e2001-04-11 14:28:42 +0000312 }
313 A = Z;
drh82c3d632000-06-06 21:56:07 +0000314}
drh0a36c572002-02-18 22:49:59 +0000315%type multiselect_op {int}
drh74ad7fe2004-10-07 03:06:28 +0000316multiselect_op(A) ::= UNION(OP). {A = @OP;}
317multiselect_op(A) ::= UNION ALL. {A = TK_ALL;}
318multiselect_op(A) ::= INTERSECT(OP). {A = @OP;}
319multiselect_op(A) ::= EXCEPT(OP). {A = @OP;}
drh82c3d632000-06-06 21:56:07 +0000320oneselect(A) ::= SELECT distinct(D) selcollist(W) from(X) where_opt(Y)
drh9bbca4c2001-11-06 04:00:18 +0000321 groupby_opt(P) having_opt(Q) orderby_opt(Z) limit_opt(L). {
danielk19774adee202004-05-08 08:23:19 +0000322 A = sqlite3SelectNew(W,X,Y,P,Q,Z,D,L.limit,L.offset);
drh9bb61fe2000-06-05 16:01:39 +0000323}
324
325// The "distinct" nonterminal is true (1) if the DISTINCT keyword is
326// present and false (0) if it is not.
327//
drhefb72512000-05-31 20:00:52 +0000328%type distinct {int}
drhefb72512000-05-31 20:00:52 +0000329distinct(A) ::= DISTINCT. {A = 1;}
drhfef52082000-06-06 01:50:43 +0000330distinct(A) ::= ALL. {A = 0;}
drhefb72512000-05-31 20:00:52 +0000331distinct(A) ::= . {A = 0;}
drh348784e2000-05-29 20:41:49 +0000332
drh9bb61fe2000-06-05 16:01:39 +0000333// selcollist is a list of expressions that are to become the return
drh7c917d12001-12-16 20:05:05 +0000334// values of the SELECT statement. The "*" in statements like
335// "SELECT * FROM ..." is encoded as a special expression with an
336// opcode of TK_ALL.
drh9bb61fe2000-06-05 16:01:39 +0000337//
drh348784e2000-05-29 20:41:49 +0000338%type selcollist {ExprList*}
danielk19774adee202004-05-08 08:23:19 +0000339%destructor selcollist {sqlite3ExprListDelete($$);}
drh348784e2000-05-29 20:41:49 +0000340%type sclp {ExprList*}
danielk19774adee202004-05-08 08:23:19 +0000341%destructor sclp {sqlite3ExprListDelete($$);}
drh348784e2000-05-29 20:41:49 +0000342sclp(A) ::= selcollist(X) COMMA. {A = X;}
343sclp(A) ::= . {A = 0;}
drh01f3f252002-05-24 16:14:15 +0000344selcollist(A) ::= sclp(P) expr(X) as(Y). {
danielk19774adee202004-05-08 08:23:19 +0000345 A = sqlite3ExprListAppend(P,X,Y.n?&Y:0);
drh01f3f252002-05-24 16:14:15 +0000346}
drh7c917d12001-12-16 20:05:05 +0000347selcollist(A) ::= sclp(P) STAR. {
danielk19774adee202004-05-08 08:23:19 +0000348 A = sqlite3ExprListAppend(P, sqlite3Expr(TK_ALL, 0, 0, 0), 0);
drh7c917d12001-12-16 20:05:05 +0000349}
drh5ad1a6c2002-07-01 12:27:09 +0000350selcollist(A) ::= sclp(P) nm(X) DOT STAR. {
danielk19774adee202004-05-08 08:23:19 +0000351 Expr *pRight = sqlite3Expr(TK_ALL, 0, 0, 0);
352 Expr *pLeft = sqlite3Expr(TK_ID, 0, 0, &X);
353 A = sqlite3ExprListAppend(P, sqlite3Expr(TK_DOT, pLeft, pRight, 0), 0);
drh54473222002-04-04 02:10:55 +0000354}
drh01f3f252002-05-24 16:14:15 +0000355
356// An option "AS <id>" phrase that can follow one of the expressions that
357// define the result set, or one of the tables in the FROM clause.
358//
359%type as {Token}
drh74ad7fe2004-10-07 03:06:28 +0000360as(X) ::= AS nm(Y). {X = Y;}
361as(X) ::= ids(Y). {X = Y;}
362as(X) ::= . {X.n = 0;}
drh9bb61fe2000-06-05 16:01:39 +0000363
drh348784e2000-05-29 20:41:49 +0000364
drhad3cab52002-05-24 02:04:32 +0000365%type seltablist {SrcList*}
danielk19774adee202004-05-08 08:23:19 +0000366%destructor seltablist {sqlite3SrcListDelete($$);}
drhad3cab52002-05-24 02:04:32 +0000367%type stl_prefix {SrcList*}
danielk19774adee202004-05-08 08:23:19 +0000368%destructor stl_prefix {sqlite3SrcListDelete($$);}
drhad3cab52002-05-24 02:04:32 +0000369%type from {SrcList*}
danielk19774adee202004-05-08 08:23:19 +0000370%destructor from {sqlite3SrcListDelete($$);}
drh348784e2000-05-29 20:41:49 +0000371
drh01f3f252002-05-24 16:14:15 +0000372// A complete FROM clause.
373//
drhbf3a4fa2002-04-06 13:57:42 +0000374from(A) ::= . {A = sqliteMalloc(sizeof(*A));}
drh348784e2000-05-29 20:41:49 +0000375from(A) ::= FROM seltablist(X). {A = X;}
drh01f3f252002-05-24 16:14:15 +0000376
377// "seltablist" is a "Select Table List" - the content of the FROM clause
378// in a SELECT statement. "stl_prefix" is a prefix of this list.
379//
380stl_prefix(A) ::= seltablist(X) joinop(Y). {
381 A = X;
382 if( A && A->nSrc>0 ) A->a[A->nSrc-1].jointype = Y;
383}
drh348784e2000-05-29 20:41:49 +0000384stl_prefix(A) ::= . {A = 0;}
drh113088e2003-03-20 01:16:58 +0000385seltablist(A) ::= stl_prefix(X) nm(Y) dbnm(D) as(Z) on_opt(N) using_opt(U). {
danielk19774adee202004-05-08 08:23:19 +0000386 A = sqlite3SrcListAppend(X,&Y,&D);
387 if( Z.n ) sqlite3SrcListAddAlias(A,&Z);
drh01f3f252002-05-24 16:14:15 +0000388 if( N ){
389 if( A && A->nSrc>1 ){ A->a[A->nSrc-2].pOn = N; }
danielk19774adee202004-05-08 08:23:19 +0000390 else { sqlite3ExprDelete(N); }
drh01f3f252002-05-24 16:14:15 +0000391 }
392 if( U ){
393 if( A && A->nSrc>1 ){ A->a[A->nSrc-2].pUsing = U; }
danielk19774adee202004-05-08 08:23:19 +0000394 else { sqlite3IdListDelete(U); }
drh01f3f252002-05-24 16:14:15 +0000395 }
drhc4a3c772001-04-04 11:48:57 +0000396}
drhb733d032004-01-24 20:18:12 +0000397seltablist(A) ::= stl_prefix(X) LP seltablist_paren(S) RP
398 as(Z) on_opt(N) using_opt(U). {
danielk19774adee202004-05-08 08:23:19 +0000399 A = sqlite3SrcListAppend(X,0,0);
drhad3cab52002-05-24 02:04:32 +0000400 A->a[A->nSrc-1].pSelect = S;
danielk19774adee202004-05-08 08:23:19 +0000401 if( Z.n ) sqlite3SrcListAddAlias(A,&Z);
drh01f3f252002-05-24 16:14:15 +0000402 if( N ){
403 if( A && A->nSrc>1 ){ A->a[A->nSrc-2].pOn = N; }
danielk19774adee202004-05-08 08:23:19 +0000404 else { sqlite3ExprDelete(N); }
drhd5feede2002-05-08 21:46:14 +0000405 }
drh01f3f252002-05-24 16:14:15 +0000406 if( U ){
407 if( A && A->nSrc>1 ){ A->a[A->nSrc-2].pUsing = U; }
danielk19774adee202004-05-08 08:23:19 +0000408 else { sqlite3IdListDelete(U); }
drh01f3f252002-05-24 16:14:15 +0000409 }
drh22f70c32002-02-18 01:17:00 +0000410}
drh348784e2000-05-29 20:41:49 +0000411
drhb733d032004-01-24 20:18:12 +0000412// A seltablist_paren nonterminal represents anything in a FROM that
413// is contained inside parentheses. This can be either a subquery or
414// a grouping of table and subqueries.
415//
416%type seltablist_paren {Select*}
danielk19774adee202004-05-08 08:23:19 +0000417%destructor seltablist_paren {sqlite3SelectDelete($$);}
drhb733d032004-01-24 20:18:12 +0000418seltablist_paren(A) ::= select(S). {A = S;}
419seltablist_paren(A) ::= seltablist(F). {
danielk19774adee202004-05-08 08:23:19 +0000420 A = sqlite3SelectNew(0,F,0,0,0,0,0,-1,0);
drhb733d032004-01-24 20:18:12 +0000421}
422
drh113088e2003-03-20 01:16:58 +0000423%type dbnm {Token}
424dbnm(A) ::= . {A.z=0; A.n=0;}
425dbnm(A) ::= DOT nm(X). {A = X;}
426
drh74ad7fe2004-10-07 03:06:28 +0000427%type fullname {SrcList*}
428%destructor fullname {sqlite3SrcListDelete($$);}
429fullname(A) ::= nm(X) dbnm(Y). {A = sqlite3SrcListAppend(0,&X,&Y);}
430
drh01f3f252002-05-24 16:14:15 +0000431%type joinop {int}
432%type joinop2 {int}
433joinop(X) ::= COMMA. { X = JT_INNER; }
434joinop(X) ::= JOIN. { X = JT_INNER; }
danielk19774adee202004-05-08 08:23:19 +0000435joinop(X) ::= JOIN_KW(A) JOIN. { X = sqlite3JoinType(pParse,&A,0,0); }
436joinop(X) ::= JOIN_KW(A) nm(B) JOIN. { X = sqlite3JoinType(pParse,&A,&B,0); }
drh5ad1a6c2002-07-01 12:27:09 +0000437joinop(X) ::= JOIN_KW(A) nm(B) nm(C) JOIN.
danielk19774adee202004-05-08 08:23:19 +0000438 { X = sqlite3JoinType(pParse,&A,&B,&C); }
drh01f3f252002-05-24 16:14:15 +0000439
440%type on_opt {Expr*}
danielk19774adee202004-05-08 08:23:19 +0000441%destructor on_opt {sqlite3ExprDelete($$);}
drh01f3f252002-05-24 16:14:15 +0000442on_opt(N) ::= ON expr(E). {N = E;}
443on_opt(N) ::= . {N = 0;}
444
445%type using_opt {IdList*}
danielk19774adee202004-05-08 08:23:19 +0000446%destructor using_opt {sqlite3IdListDelete($$);}
danielk19770202b292004-06-09 09:55:16 +0000447using_opt(U) ::= USING LP inscollist(L) RP. {U = L;}
drh01f3f252002-05-24 16:14:15 +0000448using_opt(U) ::= . {U = 0;}
449
450
drh348784e2000-05-29 20:41:49 +0000451%type orderby_opt {ExprList*}
danielk19774adee202004-05-08 08:23:19 +0000452%destructor orderby_opt {sqlite3ExprListDelete($$);}
drh348784e2000-05-29 20:41:49 +0000453%type sortlist {ExprList*}
danielk19774adee202004-05-08 08:23:19 +0000454%destructor sortlist {sqlite3ExprListDelete($$);}
drh348784e2000-05-29 20:41:49 +0000455%type sortitem {Expr*}
danielk19774adee202004-05-08 08:23:19 +0000456%destructor sortitem {sqlite3ExprDelete($$);}
drh348784e2000-05-29 20:41:49 +0000457
458orderby_opt(A) ::= . {A = 0;}
459orderby_opt(A) ::= ORDER BY sortlist(X). {A = X;}
drh8e2ca022002-06-17 17:07:19 +0000460sortlist(A) ::= sortlist(X) COMMA sortitem(Y) collate(C) sortorder(Z). {
drh1186b0a2004-05-20 23:37:54 +0000461 A = sqlite3ExprListAppend(X,Y,C.n>0?&C:0);
drhd3d39e92004-05-20 22:16:29 +0000462 if( A ) A->a[A->nExpr-1].sortOrder = Z;
drh9bb61fe2000-06-05 16:01:39 +0000463}
drh38640e12002-07-05 21:42:36 +0000464sortlist(A) ::= sortitem(Y) collate(C) sortorder(Z). {
drh1186b0a2004-05-20 23:37:54 +0000465 A = sqlite3ExprListAppend(0,Y,C.n>0?&C:0);
danielk197796fb0dd2004-06-30 09:49:22 +0000466 if( A && A->a ) A->a[0].sortOrder = Z;
drh9bb61fe2000-06-05 16:01:39 +0000467}
drhda9d6c42000-05-31 18:20:14 +0000468sortitem(A) ::= expr(X). {A = X;}
drh348784e2000-05-29 20:41:49 +0000469
470%type sortorder {int}
drhd3d39e92004-05-20 22:16:29 +0000471%type collate {Token}
drh348784e2000-05-29 20:41:49 +0000472
drh8e2ca022002-06-17 17:07:19 +0000473sortorder(A) ::= ASC. {A = SQLITE_SO_ASC;}
474sortorder(A) ::= DESC. {A = SQLITE_SO_DESC;}
475sortorder(A) ::= . {A = SQLITE_SO_ASC;}
drhd3d39e92004-05-20 22:16:29 +0000476collate(C) ::= . {C.z = 0; C.n = 0;}
477collate(C) ::= COLLATE id(X). {C = X;}
drh348784e2000-05-29 20:41:49 +0000478
drh22827922000-06-06 17:27:05 +0000479%type groupby_opt {ExprList*}
danielk19774adee202004-05-08 08:23:19 +0000480%destructor groupby_opt {sqlite3ExprListDelete($$);}
drh6206d502000-06-19 19:09:08 +0000481groupby_opt(A) ::= . {A = 0;}
drh22827922000-06-06 17:27:05 +0000482groupby_opt(A) ::= GROUP BY exprlist(X). {A = X;}
483
484%type having_opt {Expr*}
danielk19774adee202004-05-08 08:23:19 +0000485%destructor having_opt {sqlite3ExprDelete($$);}
drh6206d502000-06-19 19:09:08 +0000486having_opt(A) ::= . {A = 0;}
drh22827922000-06-06 17:27:05 +0000487having_opt(A) ::= HAVING expr(X). {A = X;}
488
drhad3cab52002-05-24 02:04:32 +0000489%type limit_opt {struct LimitVal}
drhef0cae52003-07-16 02:19:37 +0000490limit_opt(A) ::= . {A.limit = -1; A.offset = 0;}
491limit_opt(A) ::= LIMIT signed(X). {A.limit = X; A.offset = 0;}
492limit_opt(A) ::= LIMIT signed(X) OFFSET signed(Y).
493 {A.limit = X; A.offset = Y;}
494limit_opt(A) ::= LIMIT signed(X) COMMA signed(Y).
495 {A.limit = Y; A.offset = X;}
drh9bbca4c2001-11-06 04:00:18 +0000496
drh382c0242001-10-06 16:33:02 +0000497/////////////////////////// The DELETE statement /////////////////////////////
498//
drh74ad7fe2004-10-07 03:06:28 +0000499cmd ::= DELETE FROM fullname(X) where_opt(Y). {sqlite3DeleteFrom(pParse,X,Y);}
drh348784e2000-05-29 20:41:49 +0000500
501%type where_opt {Expr*}
danielk19774adee202004-05-08 08:23:19 +0000502%destructor where_opt {sqlite3ExprDelete($$);}
drh348784e2000-05-29 20:41:49 +0000503
504where_opt(A) ::= . {A = 0;}
505where_opt(A) ::= WHERE expr(X). {A = X;}
506
507%type setlist {ExprList*}
danielk19774adee202004-05-08 08:23:19 +0000508%destructor setlist {sqlite3ExprListDelete($$);}
drh348784e2000-05-29 20:41:49 +0000509
drh382c0242001-10-06 16:33:02 +0000510////////////////////////// The UPDATE command ////////////////////////////////
511//
drh74ad7fe2004-10-07 03:06:28 +0000512cmd ::= UPDATE orconf(R) fullname(X) SET setlist(Y) where_opt(Z).
513 {sqlite3Update(pParse,X,Y,Z,R);}
drh348784e2000-05-29 20:41:49 +0000514
drh5ad1a6c2002-07-01 12:27:09 +0000515setlist(A) ::= setlist(Z) COMMA nm(X) EQ expr(Y).
danielk19774adee202004-05-08 08:23:19 +0000516 {A = sqlite3ExprListAppend(Z,Y,&X);}
517setlist(A) ::= nm(X) EQ expr(Y). {A = sqlite3ExprListAppend(0,Y,&X);}
drh348784e2000-05-29 20:41:49 +0000518
drh382c0242001-10-06 16:33:02 +0000519////////////////////////// The INSERT command /////////////////////////////////
520//
drh74ad7fe2004-10-07 03:06:28 +0000521cmd ::= insert_cmd(R) INTO fullname(X) inscollist_opt(F)
drh113088e2003-03-20 01:16:58 +0000522 VALUES LP itemlist(Y) RP.
drh74ad7fe2004-10-07 03:06:28 +0000523 {sqlite3Insert(pParse, X, Y, 0, F, R);}
524cmd ::= insert_cmd(R) INTO fullname(X) inscollist_opt(F) select(S).
525 {sqlite3Insert(pParse, X, 0, S, F, R);}
drh348784e2000-05-29 20:41:49 +0000526
drhfa86c412002-02-02 15:01:15 +0000527%type insert_cmd {int}
528insert_cmd(A) ::= INSERT orconf(R). {A = R;}
529insert_cmd(A) ::= REPLACE. {A = OE_Replace;}
530
drh348784e2000-05-29 20:41:49 +0000531
532%type itemlist {ExprList*}
danielk19774adee202004-05-08 08:23:19 +0000533%destructor itemlist {sqlite3ExprListDelete($$);}
drh348784e2000-05-29 20:41:49 +0000534
danielk19774adee202004-05-08 08:23:19 +0000535itemlist(A) ::= itemlist(X) COMMA expr(Y). {A = sqlite3ExprListAppend(X,Y,0);}
536itemlist(A) ::= expr(X). {A = sqlite3ExprListAppend(0,X,0);}
drh348784e2000-05-29 20:41:49 +0000537
drh967e8b72000-06-21 13:59:10 +0000538%type inscollist_opt {IdList*}
danielk19774adee202004-05-08 08:23:19 +0000539%destructor inscollist_opt {sqlite3IdListDelete($$);}
drh967e8b72000-06-21 13:59:10 +0000540%type inscollist {IdList*}
danielk19774adee202004-05-08 08:23:19 +0000541%destructor inscollist {sqlite3IdListDelete($$);}
drh348784e2000-05-29 20:41:49 +0000542
drhc4a3c772001-04-04 11:48:57 +0000543inscollist_opt(A) ::= . {A = 0;}
544inscollist_opt(A) ::= LP inscollist(X) RP. {A = X;}
danielk19774adee202004-05-08 08:23:19 +0000545inscollist(A) ::= inscollist(X) COMMA nm(Y). {A = sqlite3IdListAppend(X,&Y);}
546inscollist(A) ::= nm(Y). {A = sqlite3IdListAppend(0,&Y);}
drh348784e2000-05-29 20:41:49 +0000547
drh382c0242001-10-06 16:33:02 +0000548/////////////////////////// Expression Processing /////////////////////////////
549//
drh348784e2000-05-29 20:41:49 +0000550
551%type expr {Expr*}
danielk19774adee202004-05-08 08:23:19 +0000552%destructor expr {sqlite3ExprDelete($$);}
drh348784e2000-05-29 20:41:49 +0000553
danielk19774adee202004-05-08 08:23:19 +0000554expr(A) ::= LP(B) expr(X) RP(E). {A = X; sqlite3ExprSpan(A,&B,&E); }
drh7ac25c72004-08-19 15:12:26 +0000555expr(A) ::= NULL(X). {A = sqlite3Expr(@X, 0, 0, &X);}
danielk19774adee202004-05-08 08:23:19 +0000556expr(A) ::= ID(X). {A = sqlite3Expr(TK_ID, 0, 0, &X);}
557expr(A) ::= JOIN_KW(X). {A = sqlite3Expr(TK_ID, 0, 0, &X);}
drh5ad1a6c2002-07-01 12:27:09 +0000558expr(A) ::= nm(X) DOT nm(Y). {
danielk19774adee202004-05-08 08:23:19 +0000559 Expr *temp1 = sqlite3Expr(TK_ID, 0, 0, &X);
560 Expr *temp2 = sqlite3Expr(TK_ID, 0, 0, &Y);
561 A = sqlite3Expr(TK_DOT, temp1, temp2, 0);
drhe1b6a5b2000-07-29 13:06:59 +0000562}
drhd24cc422003-03-27 12:51:24 +0000563expr(A) ::= nm(X) DOT nm(Y) DOT nm(Z). {
danielk19774adee202004-05-08 08:23:19 +0000564 Expr *temp1 = sqlite3Expr(TK_ID, 0, 0, &X);
565 Expr *temp2 = sqlite3Expr(TK_ID, 0, 0, &Y);
566 Expr *temp3 = sqlite3Expr(TK_ID, 0, 0, &Z);
567 Expr *temp4 = sqlite3Expr(TK_DOT, temp2, temp3, 0);
568 A = sqlite3Expr(TK_DOT, temp1, temp4, 0);
drhd24cc422003-03-27 12:51:24 +0000569}
drh7ac25c72004-08-19 15:12:26 +0000570expr(A) ::= INTEGER(X). {A = sqlite3Expr(@X, 0, 0, &X);}
571expr(A) ::= FLOAT(X). {A = sqlite3Expr(@X, 0, 0, &X);}
572expr(A) ::= STRING(X). {A = sqlite3Expr(@X, 0, 0, &X);}
573expr(A) ::= BLOB(X). {A = sqlite3Expr(@X, 0, 0, &X);}
drh7c972de2003-09-06 22:18:07 +0000574expr(A) ::= VARIABLE(X). {
drh895d7472004-08-20 16:02:39 +0000575 Token *pToken = &X;
576 Expr *pExpr = A = sqlite3Expr(TK_VARIABLE, 0, 0, pToken);
drhfa6bc002004-09-07 16:19:52 +0000577 sqlite3ExprAssignVarNumber(pParse, pExpr);
drh7c972de2003-09-06 22:18:07 +0000578}
drhe1b6a5b2000-07-29 13:06:59 +0000579expr(A) ::= ID(X) LP exprlist(Y) RP(E). {
danielk19774adee202004-05-08 08:23:19 +0000580 A = sqlite3ExprFunction(Y, &X);
581 sqlite3ExprSpan(A,&X,&E);
drhe1b6a5b2000-07-29 13:06:59 +0000582}
583expr(A) ::= ID(X) LP STAR RP(E). {
danielk19774adee202004-05-08 08:23:19 +0000584 A = sqlite3ExprFunction(0, &X);
585 sqlite3ExprSpan(A,&X,&E);
drhe1b6a5b2000-07-29 13:06:59 +0000586}
drh7ac25c72004-08-19 15:12:26 +0000587expr(A) ::= expr(X) AND(OP) expr(Y). {A = sqlite3Expr(@OP, X, Y, 0);}
588expr(A) ::= expr(X) OR(OP) expr(Y). {A = sqlite3Expr(@OP, X, Y, 0);}
589expr(A) ::= expr(X) LT(OP) expr(Y). {A = sqlite3Expr(@OP, X, Y, 0);}
590expr(A) ::= expr(X) GT(OP) expr(Y). {A = sqlite3Expr(@OP, X, Y, 0);}
591expr(A) ::= expr(X) LE(OP) expr(Y). {A = sqlite3Expr(@OP, X, Y, 0);}
592expr(A) ::= expr(X) GE(OP) expr(Y). {A = sqlite3Expr(@OP, X, Y, 0);}
593expr(A) ::= expr(X) NE(OP) expr(Y). {A = sqlite3Expr(@OP, X, Y, 0);}
594expr(A) ::= expr(X) EQ(OP) expr(Y). {A = sqlite3Expr(@OP, X, Y, 0);}
595expr(A) ::= expr(X) BITAND(OP) expr(Y). {A = sqlite3Expr(@OP, X, Y, 0);}
596expr(A) ::= expr(X) BITOR(OP) expr(Y). {A = sqlite3Expr(@OP, X, Y, 0);}
597expr(A) ::= expr(X) LSHIFT(OP) expr(Y). {A = sqlite3Expr(@OP, X, Y, 0);}
598expr(A) ::= expr(X) RSHIFT(OP) expr(Y). {A = sqlite3Expr(@OP, X, Y, 0);}
drh74ad7fe2004-10-07 03:06:28 +0000599expr(A) ::= expr(X) PLUS(OP) expr(Y). {A = sqlite3Expr(@OP, X, Y, 0);}
600expr(A) ::= expr(X) MINUS(OP) expr(Y). {A = sqlite3Expr(@OP, X, Y, 0);}
601expr(A) ::= expr(X) STAR(OP) expr(Y). {A = sqlite3Expr(@OP, X, Y, 0);}
602expr(A) ::= expr(X) SLASH(OP) expr(Y). {A = sqlite3Expr(@OP, X, Y, 0);}
603expr(A) ::= expr(X) REM(OP) expr(Y). {A = sqlite3Expr(@OP, X, Y, 0);}
604expr(A) ::= expr(X) CONCAT(OP) expr(Y). {A = sqlite3Expr(@OP, X, Y, 0);}
605%type likeop {struct LikeOp}
606likeop(A) ::= LIKE. {A.opcode = TK_LIKE; A.not = 0;}
607likeop(A) ::= GLOB. {A.opcode = TK_GLOB; A.not = 0;}
608likeop(A) ::= NOT LIKE. {A.opcode = TK_LIKE; A.not = 1;}
609likeop(A) ::= NOT GLOB. {A.opcode = TK_GLOB; A.not = 1;}
drh0ac65892002-04-20 14:24:41 +0000610expr(A) ::= expr(X) likeop(OP) expr(Y). [LIKE] {
danielk19774adee202004-05-08 08:23:19 +0000611 ExprList *pList = sqlite3ExprListAppend(0, Y, 0);
612 pList = sqlite3ExprListAppend(pList, X, 0);
613 A = sqlite3ExprFunction(pList, 0);
drh2e3a1f12004-10-06 14:39:28 +0000614 if( A ) A->op = OP.opcode;
615 if( OP.not ) A = sqlite3Expr(TK_NOT, A, 0, 0);
danielk19774adee202004-05-08 08:23:19 +0000616 sqlite3ExprSpan(A, &X->span, &Y->span);
drh0ac65892002-04-20 14:24:41 +0000617}
drhe1b6a5b2000-07-29 13:06:59 +0000618expr(A) ::= expr(X) ISNULL(E). {
danielk19774adee202004-05-08 08:23:19 +0000619 A = sqlite3Expr(TK_ISNULL, X, 0, 0);
620 sqlite3ExprSpan(A,&X->span,&E);
drhe1b6a5b2000-07-29 13:06:59 +0000621}
drh33048c02001-10-01 14:29:22 +0000622expr(A) ::= expr(X) IS NULL(E). {
danielk19774adee202004-05-08 08:23:19 +0000623 A = sqlite3Expr(TK_ISNULL, X, 0, 0);
624 sqlite3ExprSpan(A,&X->span,&E);
drh33048c02001-10-01 14:29:22 +0000625}
drhe1b6a5b2000-07-29 13:06:59 +0000626expr(A) ::= expr(X) NOTNULL(E). {
danielk19774adee202004-05-08 08:23:19 +0000627 A = sqlite3Expr(TK_NOTNULL, X, 0, 0);
628 sqlite3ExprSpan(A,&X->span,&E);
drhe1b6a5b2000-07-29 13:06:59 +0000629}
drh33048c02001-10-01 14:29:22 +0000630expr(A) ::= expr(X) NOT NULL(E). {
danielk19774adee202004-05-08 08:23:19 +0000631 A = sqlite3Expr(TK_NOTNULL, X, 0, 0);
632 sqlite3ExprSpan(A,&X->span,&E);
drh33048c02001-10-01 14:29:22 +0000633}
drh81a20f22001-10-12 17:30:04 +0000634expr(A) ::= expr(X) IS NOT NULL(E). {
danielk19774adee202004-05-08 08:23:19 +0000635 A = sqlite3Expr(TK_NOTNULL, X, 0, 0);
636 sqlite3ExprSpan(A,&X->span,&E);
drh81a20f22001-10-12 17:30:04 +0000637}
drhe1b6a5b2000-07-29 13:06:59 +0000638expr(A) ::= NOT(B) expr(X). {
drh7ac25c72004-08-19 15:12:26 +0000639 A = sqlite3Expr(@B, X, 0, 0);
danielk19774adee202004-05-08 08:23:19 +0000640 sqlite3ExprSpan(A,&B,&X->span);
drhe1b6a5b2000-07-29 13:06:59 +0000641}
drh81a20f22001-10-12 17:30:04 +0000642expr(A) ::= BITNOT(B) expr(X). {
drh7ac25c72004-08-19 15:12:26 +0000643 A = sqlite3Expr(@B, X, 0, 0);
danielk19774adee202004-05-08 08:23:19 +0000644 sqlite3ExprSpan(A,&B,&X->span);
drh81a20f22001-10-12 17:30:04 +0000645}
drhe1b6a5b2000-07-29 13:06:59 +0000646expr(A) ::= MINUS(B) expr(X). [UMINUS] {
danielk19774adee202004-05-08 08:23:19 +0000647 A = sqlite3Expr(TK_UMINUS, X, 0, 0);
648 sqlite3ExprSpan(A,&B,&X->span);
drhe1b6a5b2000-07-29 13:06:59 +0000649}
drh4b59ab52002-08-24 18:24:51 +0000650expr(A) ::= PLUS(B) expr(X). [UPLUS] {
danielk19774adee202004-05-08 08:23:19 +0000651 A = sqlite3Expr(TK_UPLUS, X, 0, 0);
652 sqlite3ExprSpan(A,&B,&X->span);
drhe1b6a5b2000-07-29 13:06:59 +0000653}
654expr(A) ::= LP(B) select(X) RP(E). {
danielk19774adee202004-05-08 08:23:19 +0000655 A = sqlite3Expr(TK_SELECT, 0, 0, 0);
drhdaffd0e2001-04-11 14:28:42 +0000656 if( A ) A->pSelect = X;
danielk19774adee202004-05-08 08:23:19 +0000657 sqlite3ExprSpan(A,&B,&E);
drh19a775c2000-06-05 18:54:46 +0000658}
drh2e3a1f12004-10-06 14:39:28 +0000659%type between_op {int}
660between_op(A) ::= BETWEEN. {A = 0;}
661between_op(A) ::= NOT BETWEEN. {A = 1;}
662expr(A) ::= expr(W) between_op(N) expr(X) AND expr(Y). [BETWEEN] {
danielk19774adee202004-05-08 08:23:19 +0000663 ExprList *pList = sqlite3ExprListAppend(0, X, 0);
664 pList = sqlite3ExprListAppend(pList, Y, 0);
665 A = sqlite3Expr(TK_BETWEEN, W, 0, 0);
drhdaffd0e2001-04-11 14:28:42 +0000666 if( A ) A->pList = pList;
drh2e3a1f12004-10-06 14:39:28 +0000667 if( N ) A = sqlite3Expr(TK_NOT, A, 0, 0);
danielk19774adee202004-05-08 08:23:19 +0000668 sqlite3ExprSpan(A,&W->span,&Y->span);
drhfef52082000-06-06 01:50:43 +0000669}
drh2e3a1f12004-10-06 14:39:28 +0000670%type in_op {int}
671in_op(A) ::= IN. {A = 0;}
672in_op(A) ::= NOT IN. {A = 1;}
673expr(A) ::= expr(X) in_op(N) LP exprlist(Y) RP(E). [IN] {
danielk19774adee202004-05-08 08:23:19 +0000674 A = sqlite3Expr(TK_IN, X, 0, 0);
drhdaffd0e2001-04-11 14:28:42 +0000675 if( A ) A->pList = Y;
drh2e3a1f12004-10-06 14:39:28 +0000676 if( N ) A = sqlite3Expr(TK_NOT, A, 0, 0);
danielk19774adee202004-05-08 08:23:19 +0000677 sqlite3ExprSpan(A,&X->span,&E);
drhfef52082000-06-06 01:50:43 +0000678}
drh2e3a1f12004-10-06 14:39:28 +0000679expr(A) ::= expr(X) in_op(N) LP select(Y) RP(E). [IN] {
danielk19774adee202004-05-08 08:23:19 +0000680 A = sqlite3Expr(TK_IN, X, 0, 0);
drhdaffd0e2001-04-11 14:28:42 +0000681 if( A ) A->pSelect = Y;
drh2e3a1f12004-10-06 14:39:28 +0000682 if( N ) A = sqlite3Expr(TK_NOT, A, 0, 0);
danielk19774adee202004-05-08 08:23:19 +0000683 sqlite3ExprSpan(A,&X->span,&E);
drhfef52082000-06-06 01:50:43 +0000684}
drh74ad7fe2004-10-07 03:06:28 +0000685expr(A) ::= expr(X) in_op(N) nm(Y) dbnm(Z). [IN] {
686 SrcList *pSrc = sqlite3SrcListAppend(0,&Y,&Z);
danielk19774adee202004-05-08 08:23:19 +0000687 A = sqlite3Expr(TK_IN, X, 0, 0);
688 if( A ) A->pSelect = sqlite3SelectNew(0,pSrc,0,0,0,0,0,-1,0);
drh2e3a1f12004-10-06 14:39:28 +0000689 if( N ) A = sqlite3Expr(TK_NOT, A, 0, 0);
drh74ad7fe2004-10-07 03:06:28 +0000690 sqlite3ExprSpan(A,&X->span,Z.z?&Z:&Y);
drh23b2db22004-01-15 03:30:24 +0000691}
692
drhfef52082000-06-06 01:50:43 +0000693
drh17a7f8d2002-03-24 13:13:27 +0000694/* CASE expressions */
695expr(A) ::= CASE(C) case_operand(X) case_exprlist(Y) case_else(Z) END(E). {
danielk19774adee202004-05-08 08:23:19 +0000696 A = sqlite3Expr(TK_CASE, X, Z, 0);
drh17a7f8d2002-03-24 13:13:27 +0000697 if( A ) A->pList = Y;
danielk19774adee202004-05-08 08:23:19 +0000698 sqlite3ExprSpan(A, &C, &E);
drh17a7f8d2002-03-24 13:13:27 +0000699}
700%type case_exprlist {ExprList*}
danielk19774adee202004-05-08 08:23:19 +0000701%destructor case_exprlist {sqlite3ExprListDelete($$);}
drh17a7f8d2002-03-24 13:13:27 +0000702case_exprlist(A) ::= case_exprlist(X) WHEN expr(Y) THEN expr(Z). {
danielk19774adee202004-05-08 08:23:19 +0000703 A = sqlite3ExprListAppend(X, Y, 0);
704 A = sqlite3ExprListAppend(A, Z, 0);
drh17a7f8d2002-03-24 13:13:27 +0000705}
706case_exprlist(A) ::= WHEN expr(Y) THEN expr(Z). {
danielk19774adee202004-05-08 08:23:19 +0000707 A = sqlite3ExprListAppend(0, Y, 0);
708 A = sqlite3ExprListAppend(A, Z, 0);
drh17a7f8d2002-03-24 13:13:27 +0000709}
710%type case_else {Expr*}
711case_else(A) ::= ELSE expr(X). {A = X;}
712case_else(A) ::= . {A = 0;}
713%type case_operand {Expr*}
714case_operand(A) ::= expr(X). {A = X;}
715case_operand(A) ::= . {A = 0;}
drh348784e2000-05-29 20:41:49 +0000716
717%type exprlist {ExprList*}
danielk19774adee202004-05-08 08:23:19 +0000718%destructor exprlist {sqlite3ExprListDelete($$);}
drh348784e2000-05-29 20:41:49 +0000719%type expritem {Expr*}
danielk19774adee202004-05-08 08:23:19 +0000720%destructor expritem {sqlite3ExprDelete($$);}
drh348784e2000-05-29 20:41:49 +0000721
drh348784e2000-05-29 20:41:49 +0000722exprlist(A) ::= exprlist(X) COMMA expritem(Y).
drh74ad7fe2004-10-07 03:06:28 +0000723 {A = sqlite3ExprListAppend(X,Y,0);}
danielk19774adee202004-05-08 08:23:19 +0000724exprlist(A) ::= expritem(X). {A = sqlite3ExprListAppend(0,X,0);}
drh348784e2000-05-29 20:41:49 +0000725expritem(A) ::= expr(X). {A = X;}
726expritem(A) ::= . {A = 0;}
drhcce7d172000-05-31 15:34:51 +0000727
drh382c0242001-10-06 16:33:02 +0000728///////////////////////////// The CREATE INDEX command ///////////////////////
729//
danielk1977cbb18d22004-05-28 11:37:27 +0000730cmd ::= CREATE(S) uniqueflag(U) INDEX nm(X) dbnm(D)
drh74ad7fe2004-10-07 03:06:28 +0000731 ON fullname(Y) LP idxlist(Z) RP(E) onconf(R). {
drh9cfcf5d2002-01-29 18:41:24 +0000732 if( U!=OE_None ) U = R;
733 if( U==OE_Default) U = OE_Abort;
drh74ad7fe2004-10-07 03:06:28 +0000734 sqlite3CreateIndex(pParse, &X, &D, Y, Z, U, &S, &E);
drh9cfcf5d2002-01-29 18:41:24 +0000735}
drh717e6402001-09-27 03:22:32 +0000736
737%type uniqueflag {int}
drh74ad7fe2004-10-07 03:06:28 +0000738uniqueflag(A) ::= UNIQUE. {A = OE_Abort;}
739uniqueflag(A) ::= . {A = OE_None;}
drh348784e2000-05-29 20:41:49 +0000740
danielk19770202b292004-06-09 09:55:16 +0000741%type idxlist {ExprList*}
742%destructor idxlist {sqlite3ExprListDelete($$);}
743%type idxlist_opt {ExprList*}
744%destructor idxlist_opt {sqlite3ExprListDelete($$);}
drh348784e2000-05-29 20:41:49 +0000745%type idxitem {Token}
746
drhc2eef3b2002-08-31 18:53:06 +0000747idxlist_opt(A) ::= . {A = 0;}
748idxlist_opt(A) ::= LP idxlist(X) RP. {A = X;}
danielk19770202b292004-06-09 09:55:16 +0000749idxlist(A) ::= idxlist(X) COMMA idxitem(Y) collate(C) sortorder. {
750 Expr *p = 0;
751 if( C.n>0 ){
752 p = sqlite3Expr(TK_COLUMN, 0, 0, 0);
753 if( p ) p->pColl = sqlite3LocateCollSeq(pParse, C.z, C.n);
754 }
755 A = sqlite3ExprListAppend(X, p, &Y);
756}
757idxlist(A) ::= idxitem(Y) collate(C) sortorder. {
758 Expr *p = 0;
759 if( C.n>0 ){
760 p = sqlite3Expr(TK_COLUMN, 0, 0, 0);
761 if( p ) p->pColl = sqlite3LocateCollSeq(pParse, C.z, C.n);
762 }
763 A = sqlite3ExprListAppend(0, p, &Y);
764}
765idxitem(A) ::= nm(X). {A = X;}
766
drh348784e2000-05-29 20:41:49 +0000767
drh8aff1012001-12-22 14:49:24 +0000768///////////////////////////// The DROP INDEX command /////////////////////////
drh382c0242001-10-06 16:33:02 +0000769//
drh74ad7fe2004-10-07 03:06:28 +0000770cmd ::= DROP INDEX fullname(X). {sqlite3DropIndex(pParse, X);}
drh982cef72000-05-30 16:27:03 +0000771
drh382c0242001-10-06 16:33:02 +0000772///////////////////////////// The VACUUM command /////////////////////////////
773//
danielk19774adee202004-05-08 08:23:19 +0000774cmd ::= VACUUM. {sqlite3Vacuum(pParse,0);}
drh74ad7fe2004-10-07 03:06:28 +0000775cmd ::= VACUUM nm. {sqlite3Vacuum(pParse,0);}
drhf57b14a2001-09-14 18:54:08 +0000776
drh382c0242001-10-06 16:33:02 +0000777///////////////////////////// The PRAGMA command /////////////////////////////
778//
danielk197791cf71b2004-06-26 06:37:06 +0000779cmd ::= PRAGMA nm(X) dbnm(Z) EQ nm(Y). {sqlite3Pragma(pParse,&X,&Z,&Y,0);}
780cmd ::= PRAGMA nm(X) dbnm(Z) EQ ON(Y). {sqlite3Pragma(pParse,&X,&Z,&Y,0);}
781cmd ::= PRAGMA nm(X) dbnm(Z) EQ plus_num(Y). {sqlite3Pragma(pParse,&X,&Z,&Y,0);}
782cmd ::= PRAGMA nm(X) dbnm(Z) EQ minus_num(Y). {
783 sqlite3Pragma(pParse,&X,&Z,&Y,1);
784}
785cmd ::= PRAGMA nm(X) dbnm(Z) LP nm(Y) RP. {sqlite3Pragma(pParse,&X,&Z,&Y,0);}
drh74ad7fe2004-10-07 03:06:28 +0000786cmd ::= PRAGMA nm(X) dbnm(Z). {sqlite3Pragma(pParse,&X,&Z,0,0);}
drhf57b14a2001-09-14 18:54:08 +0000787plus_num(A) ::= plus_opt number(X). {A = X;}
788minus_num(A) ::= MINUS number(X). {A = X;}
drh74ad7fe2004-10-07 03:06:28 +0000789number(A) ::= INTEGER(X). {A = X;}
790number(A) ::= FLOAT(X). {A = X;}
drhf57b14a2001-09-14 18:54:08 +0000791plus_opt ::= PLUS.
792plus_opt ::= .
danielk1977c3f9bad2002-05-15 08:30:12 +0000793
794//////////////////////////// The CREATE TRIGGER command /////////////////////
drhf0f258b2003-04-21 18:48:45 +0000795
danielk19773df6b252004-05-29 10:23:19 +0000796cmd ::= CREATE trigger_decl(A) BEGIN trigger_cmd_list(S) END(Z). {
drh4b59ab52002-08-24 18:24:51 +0000797 Token all;
798 all.z = A.z;
799 all.n = (Z.z - A.z) + Z.n;
danielk19774adee202004-05-08 08:23:19 +0000800 sqlite3FinishTrigger(pParse, S, &all);
drhf0f258b2003-04-21 18:48:45 +0000801}
802
drh74ad7fe2004-10-07 03:06:28 +0000803trigger_decl(A) ::= temp(T) TRIGGER nm(B) dbnm(Z) trigger_time(C)
804 trigger_event(D)
805 ON fullname(E) foreach_clause(F) when_clause(G). {
806 sqlite3BeginTrigger(pParse, &B, &Z, C, D.a, D.b, E, F, G, T);
danielk19773df6b252004-05-29 10:23:19 +0000807 A = (Z.n==0?B:Z);
danielk1977c3f9bad2002-05-15 08:30:12 +0000808}
809
810%type trigger_time {int}
811trigger_time(A) ::= BEFORE. { A = TK_BEFORE; }
812trigger_time(A) ::= AFTER. { A = TK_AFTER; }
813trigger_time(A) ::= INSTEAD OF. { A = TK_INSTEAD;}
814trigger_time(A) ::= . { A = TK_BEFORE; }
815
drhad3cab52002-05-24 02:04:32 +0000816%type trigger_event {struct TrigEvent}
danielk19774adee202004-05-08 08:23:19 +0000817%destructor trigger_event {sqlite3IdListDelete($$.b);}
drh74ad7fe2004-10-07 03:06:28 +0000818trigger_event(A) ::= DELETE(OP). {A.a = @OP; A.b = 0;}
819trigger_event(A) ::= INSERT(OP). {A.a = @OP; A.b = 0;}
820trigger_event(A) ::= UPDATE(OP). {A.a = @OP; A.b = 0;}
821trigger_event(A) ::= UPDATE OF inscollist(X). {A.a = TK_UPDATE; A.b = X;}
danielk1977c3f9bad2002-05-15 08:30:12 +0000822
823%type foreach_clause {int}
824foreach_clause(A) ::= . { A = TK_ROW; }
825foreach_clause(A) ::= FOR EACH ROW. { A = TK_ROW; }
826foreach_clause(A) ::= FOR EACH STATEMENT. { A = TK_STATEMENT; }
827
drh0bb132b2004-07-20 14:06:51 +0000828%type when_clause {Expr*}
danielk1977c3f9bad2002-05-15 08:30:12 +0000829when_clause(A) ::= . { A = 0; }
830when_clause(A) ::= WHEN expr(X). { A = X; }
831
drh0bb132b2004-07-20 14:06:51 +0000832%type trigger_cmd_list {TriggerStep*}
danielk19774adee202004-05-08 08:23:19 +0000833%destructor trigger_cmd_list {sqlite3DeleteTriggerStep($$);}
danielk1977c3f9bad2002-05-15 08:30:12 +0000834trigger_cmd_list(A) ::= trigger_cmd(X) SEMI trigger_cmd_list(Y). {
drha69d9162003-04-17 22:57:53 +0000835 X->pNext = Y;
836 A = X;
837}
danielk1977c3f9bad2002-05-15 08:30:12 +0000838trigger_cmd_list(A) ::= . { A = 0; }
839
drh0bb132b2004-07-20 14:06:51 +0000840%type trigger_cmd {TriggerStep*}
danielk19774adee202004-05-08 08:23:19 +0000841%destructor trigger_cmd {sqlite3DeleteTriggerStep($$);}
danielk1977c3f9bad2002-05-15 08:30:12 +0000842// UPDATE
drh5ad1a6c2002-07-01 12:27:09 +0000843trigger_cmd(A) ::= UPDATE orconf(R) nm(X) SET setlist(Y) where_opt(Z).
danielk19774adee202004-05-08 08:23:19 +0000844 { A = sqlite3TriggerUpdateStep(&X, Y, Z, R); }
danielk1977c3f9bad2002-05-15 08:30:12 +0000845
846// INSERT
drh3054efe2004-02-12 17:28:13 +0000847trigger_cmd(A) ::= insert_cmd(R) INTO nm(X) inscollist_opt(F)
drh74ad7fe2004-10-07 03:06:28 +0000848 VALUES LP itemlist(Y) RP.
849 {A = sqlite3TriggerInsertStep(&X, F, Y, 0, R);}
danielk1977c3f9bad2002-05-15 08:30:12 +0000850
drh3054efe2004-02-12 17:28:13 +0000851trigger_cmd(A) ::= insert_cmd(R) INTO nm(X) inscollist_opt(F) select(S).
danielk19774adee202004-05-08 08:23:19 +0000852 {A = sqlite3TriggerInsertStep(&X, F, 0, S, R);}
danielk1977c3f9bad2002-05-15 08:30:12 +0000853
854// DELETE
drh5ad1a6c2002-07-01 12:27:09 +0000855trigger_cmd(A) ::= DELETE FROM nm(X) where_opt(Y).
danielk19774adee202004-05-08 08:23:19 +0000856 {A = sqlite3TriggerDeleteStep(&X, Y);}
danielk1977c3f9bad2002-05-15 08:30:12 +0000857
858// SELECT
danielk19774adee202004-05-08 08:23:19 +0000859trigger_cmd(A) ::= select(X). {A = sqlite3TriggerSelectStep(X); }
danielk1977c3f9bad2002-05-15 08:30:12 +0000860
danielk19776f349032002-06-11 02:25:40 +0000861// The special RAISE expression that may occur in trigger programs
drh4b59ab52002-08-24 18:24:51 +0000862expr(A) ::= RAISE(X) LP IGNORE RP(Y). {
danielk19774adee202004-05-08 08:23:19 +0000863 A = sqlite3Expr(TK_RAISE, 0, 0, 0);
drh4b59ab52002-08-24 18:24:51 +0000864 A->iColumn = OE_Ignore;
danielk19774adee202004-05-08 08:23:19 +0000865 sqlite3ExprSpan(A, &X, &Y);
drh4b59ab52002-08-24 18:24:51 +0000866}
drh74ad7fe2004-10-07 03:06:28 +0000867expr(A) ::= RAISE(X) LP raisetype(T) COMMA nm(Z) RP(Y). {
danielk19774adee202004-05-08 08:23:19 +0000868 A = sqlite3Expr(TK_RAISE, 0, 0, &Z);
drh74ad7fe2004-10-07 03:06:28 +0000869 A->iColumn = T;
danielk19774adee202004-05-08 08:23:19 +0000870 sqlite3ExprSpan(A, &X, &Y);
drh4b59ab52002-08-24 18:24:51 +0000871}
drh74ad7fe2004-10-07 03:06:28 +0000872%type raisetype {int}
873raisetype(A) ::= ROLLBACK. {A = OE_Rollback;}
874raisetype(A) ::= ABORT. {A = OE_Abort;}
875raisetype(A) ::= FAIL. {A = OE_Fail;}
876
danielk19776f349032002-06-11 02:25:40 +0000877
danielk1977c3f9bad2002-05-15 08:30:12 +0000878//////////////////////// DROP TRIGGER statement //////////////////////////////
drh74ad7fe2004-10-07 03:06:28 +0000879cmd ::= DROP TRIGGER fullname(X). {
880 sqlite3DropTrigger(pParse,X);
danielk1977c3f9bad2002-05-15 08:30:12 +0000881}
drh113088e2003-03-20 01:16:58 +0000882
883//////////////////////// ATTACH DATABASE file AS name /////////////////////////
drh4d189ca2004-02-12 18:46:38 +0000884cmd ::= ATTACH database_kw_opt ids(F) AS nm(D) key_opt(K). {
drh25d65432004-07-22 15:02:25 +0000885 sqlite3Attach(pParse, &F, &D, K.type, &K.key);
drh1c2d8412003-03-31 00:30:47 +0000886}
drh25d65432004-07-22 15:02:25 +0000887%type key_opt {struct AttachKey}
888key_opt(A) ::= . { A.type = 0; }
drh25d65432004-07-22 15:02:25 +0000889key_opt(A) ::= KEY ids(X). { A.type=1; A.key = X; }
drhfeac5f82004-08-01 00:10:45 +0000890key_opt(A) ::= KEY BLOB(X). { A.type=2; A.key = X; }
drh113088e2003-03-20 01:16:58 +0000891
892database_kw_opt ::= DATABASE.
893database_kw_opt ::= .
894
895//////////////////////// DETACH DATABASE name /////////////////////////////////
drh1c2d8412003-03-31 00:30:47 +0000896cmd ::= DETACH database_kw_opt nm(D). {
danielk19774adee202004-05-08 08:23:19 +0000897 sqlite3Detach(pParse, &D);
drh1c2d8412003-03-31 00:30:47 +0000898}