blob: 9c41484ab21900711c5b235290c4c8310da99108 [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.
drh348784e2000-05-29 20:41:49 +000016*/
drh487e2622005-06-25 18:42:14 +000017
18// All token codes are small integers with #defines that begin with "TK_"
drh348784e2000-05-29 20:41:49 +000019%token_prefix TK_
drh487e2622005-06-25 18:42:14 +000020
21// The type of the data attached to each token is Token. This is also the
22// default type for non-terminals.
23//
drh348784e2000-05-29 20:41:49 +000024%token_type {Token}
drhf57b14a2001-09-14 18:54:08 +000025%default_type {Token}
drh487e2622005-06-25 18:42:14 +000026
27// The generated parser function takes a 4th argument as follows:
drh348784e2000-05-29 20:41:49 +000028%extra_argument {Parse *pParse}
drh487e2622005-06-25 18:42:14 +000029
30// This code runs whenever there is a syntax error
31//
drh348784e2000-05-29 20:41:49 +000032%syntax_error {
drh128255f2008-12-08 16:01:12 +000033 UNUSED_PARAMETER(yymajor); /* Silence some compiler warnings */
drh6116ee42018-01-10 00:40:06 +000034 if( TOKEN.z[0] ){
35 sqlite3ErrorMsg(pParse, "near \"%T\": syntax error", &TOKEN);
36 }else{
37 sqlite3ErrorMsg(pParse, "incomplete input");
38 }
drh348784e2000-05-29 20:41:49 +000039}
drh8fc33452006-02-27 21:58:07 +000040%stack_overflow {
41 sqlite3ErrorMsg(pParse, "parser stack overflow");
42}
drh487e2622005-06-25 18:42:14 +000043
44// The name of the generated procedure that implements the parser
45// is as follows:
danielk19774adee202004-05-08 08:23:19 +000046%name sqlite3Parser
drh487e2622005-06-25 18:42:14 +000047
48// The following text is included near the beginning of the C source
49// code file that implements the parser.
50//
drh348784e2000-05-29 20:41:49 +000051%include {
52#include "sqliteInt.h"
drh9bbca4c2001-11-06 04:00:18 +000053
54/*
drhd3ec02d2009-06-12 02:27:14 +000055** Disable all error recovery processing in the parser push-down
56** automaton.
57*/
58#define YYNOERRORRECOVERY 1
59
60/*
drh8a415d32009-06-12 13:53:51 +000061** Make yytestcase() the same as testcase()
62*/
63#define yytestcase(X) testcase(X)
64
65/*
drh82415f22015-11-09 19:33:42 +000066** Indicate that sqlite3ParserFree() will never be called with a null
67** pointer.
68*/
drh644f4c12015-11-12 15:04:05 +000069#define YYPARSEFREENEVERNULL 1
drh82415f22015-11-09 19:33:42 +000070
71/*
drhd26cc542017-01-28 20:46:37 +000072** In the amalgamation, the parse.c file generated by lemon and the
73** tokenize.c file are concatenated. In that case, sqlite3RunParser()
74** has access to the the size of the yyParser object and so the parser
75** engine can be allocated from stack. In that case, only the
76** sqlite3ParserInit() and sqlite3ParserFinalize() routines are invoked
77** and the sqlite3ParserAlloc() and sqlite3ParserFree() routines can be
78** omitted.
79*/
80#ifdef SQLITE_AMALGAMATION
81# define sqlite3Parser_ENGINEALWAYSONSTACK 1
82#endif
83
84/*
drh82415f22015-11-09 19:33:42 +000085** Alternative datatype for the argument to the malloc() routine passed
86** into sqlite3ParserAlloc(). The default is size_t.
87*/
88#define YYMALLOCARGTYPE u64
89
90/*
drhad3cab52002-05-24 02:04:32 +000091** An instance of the following structure describes the event of a
92** TRIGGER. "a" is the event type, one of TK_UPDATE, TK_INSERT,
93** TK_DELETE, or TK_INSTEAD. If the event is of the form
94**
95** UPDATE ON (a,b,c)
96**
97** Then the "b" IdList records the list "a,b,c".
danielk1977c3f9bad2002-05-15 08:30:12 +000098*/
drhad3cab52002-05-24 02:04:32 +000099struct TrigEvent { int a; IdList * b; };
drhcaec2f12003-01-07 02:47:47 +0000100
drh25d65432004-07-22 15:02:25 +0000101/*
drh4a642b62016-02-05 01:55:27 +0000102** Disable lookaside memory allocation for objects that might be
103** shared across database connections.
104*/
105static void disableLookaside(Parse *pParse){
106 pParse->disableLookaside++;
107 pParse->db->lookaside.bDisable++;
108}
109
drhcaec2f12003-01-07 02:47:47 +0000110} // end %include
drh348784e2000-05-29 20:41:49 +0000111
drh826fb5a2004-02-14 23:59:57 +0000112// Input is a single SQL command
drhc4a3c772001-04-04 11:48:57 +0000113input ::= cmdlist.
drh094b2bb2002-03-13 18:54:07 +0000114cmdlist ::= cmdlist ecmd.
drh826fb5a2004-02-14 23:59:57 +0000115cmdlist ::= ecmd.
drhb7f91642004-10-31 02:22:47 +0000116ecmd ::= SEMI.
117ecmd ::= explain cmdx SEMI.
drh8549d552016-01-07 17:09:43 +0000118explain ::= .
drhb7f91642004-10-31 02:22:47 +0000119%ifndef SQLITE_OMIT_EXPLAIN
drh8549d552016-01-07 17:09:43 +0000120explain ::= EXPLAIN. { pParse->explain = 1; }
121explain ::= EXPLAIN QUERY PLAN. { pParse->explain = 2; }
drh154d4b22006-09-21 11:02:16 +0000122%endif SQLITE_OMIT_EXPLAIN
drh200a81d2008-08-08 14:19:41 +0000123cmdx ::= cmd. { sqlite3FinishCoding(pParse); }
drh348784e2000-05-29 20:41:49 +0000124
drh382c0242001-10-06 16:33:02 +0000125///////////////////// Begin and end transactions. ////////////////////////////
drhc4a3c772001-04-04 11:48:57 +0000126//
drhfa86c412002-02-02 15:01:15 +0000127
drh684917c2004-10-05 02:41:42 +0000128cmd ::= BEGIN transtype(Y) trans_opt. {sqlite3BeginTransaction(pParse, Y);}
drhc4a3c772001-04-04 11:48:57 +0000129trans_opt ::= .
130trans_opt ::= TRANSACTION.
drh5ad1a6c2002-07-01 12:27:09 +0000131trans_opt ::= TRANSACTION nm.
drh684917c2004-10-05 02:41:42 +0000132%type transtype {int}
133transtype(A) ::= . {A = TK_DEFERRED;}
drhcf82f0d2016-02-17 04:33:10 +0000134transtype(A) ::= DEFERRED(X). {A = @X; /*A-overwrites-X*/}
135transtype(A) ::= IMMEDIATE(X). {A = @X; /*A-overwrites-X*/}
136transtype(A) ::= EXCLUSIVE(X). {A = @X; /*A-overwrites-X*/}
drh07a3b112017-07-06 01:28:02 +0000137cmd ::= COMMIT|END(X) trans_opt. {sqlite3EndTransaction(pParse,@X);}
138cmd ::= ROLLBACK(X) trans_opt. {sqlite3EndTransaction(pParse,@X);}
drhc4a3c772001-04-04 11:48:57 +0000139
danielk1977fd7f0452008-12-17 17:30:26 +0000140savepoint_opt ::= SAVEPOINT.
141savepoint_opt ::= .
142cmd ::= SAVEPOINT nm(X). {
143 sqlite3Savepoint(pParse, SAVEPOINT_BEGIN, &X);
144}
145cmd ::= RELEASE savepoint_opt nm(X). {
146 sqlite3Savepoint(pParse, SAVEPOINT_RELEASE, &X);
147}
148cmd ::= ROLLBACK trans_opt TO savepoint_opt nm(X). {
149 sqlite3Savepoint(pParse, SAVEPOINT_ROLLBACK, &X);
150}
151
drh382c0242001-10-06 16:33:02 +0000152///////////////////// The CREATE TABLE statement ////////////////////////////
drh348784e2000-05-29 20:41:49 +0000153//
154cmd ::= create_table create_table_args.
drhd9da78a2009-03-24 15:08:09 +0000155create_table ::= createkw temp(T) TABLE ifnotexists(E) nm(Y) dbnm(Z). {
danielk1977f1a381e2006-06-16 08:01:02 +0000156 sqlite3StartTable(pParse,&Y,&Z,T,0,0,E);
drh969fa7c2002-02-18 18:30:32 +0000157}
drhdabd04c2016-02-17 01:46:19 +0000158createkw(A) ::= CREATE(A). {disableLookaside(pParse);}
159
drhfaa59552005-12-29 23:33:54 +0000160%type ifnotexists {int}
161ifnotexists(A) ::= . {A = 0;}
162ifnotexists(A) ::= IF NOT EXISTS. {A = 1;}
drhf57b3392001-10-08 13:22:32 +0000163%type temp {int}
danielk197753c0f742005-03-29 03:10:59 +0000164%ifndef SQLITE_OMIT_TEMPDB
drhd24cc422003-03-27 12:51:24 +0000165temp(A) ::= TEMP. {A = 1;}
drh154d4b22006-09-21 11:02:16 +0000166%endif SQLITE_OMIT_TEMPDB
drhd24cc422003-03-27 12:51:24 +0000167temp(A) ::= . {A = 0;}
drh5969da42013-10-21 02:14:45 +0000168create_table_args ::= LP columnlist conslist_opt(X) RP(E) table_options(F). {
169 sqlite3EndTable(pParse,&X,&E,F,0);
drh969fa7c2002-02-18 18:30:32 +0000170}
171create_table_args ::= AS select(S). {
drh5969da42013-10-21 02:14:45 +0000172 sqlite3EndTable(pParse,0,0,0,S);
drh633e6d52008-07-28 19:34:53 +0000173 sqlite3SelectDelete(pParse->db, S);
drh969fa7c2002-02-18 18:30:32 +0000174}
drh3334d082015-11-10 13:45:21 +0000175%type table_options {int}
drh5969da42013-10-21 02:14:45 +0000176table_options(A) ::= . {A = 0;}
177table_options(A) ::= WITHOUT nm(X). {
178 if( X.n==5 && sqlite3_strnicmp(X.z,"rowid",5)==0 ){
drhfccda8a2015-05-27 13:06:55 +0000179 A = TF_WithoutRowid | TF_NoVisibleRowid;
drh5969da42013-10-21 02:14:45 +0000180 }else{
181 A = 0;
182 sqlite3ErrorMsg(pParse, "unknown table option: %.*s", X.n, X.z);
183 }
184}
drh986dde72016-02-29 13:37:21 +0000185columnlist ::= columnlist COMMA columnname carglist.
186columnlist ::= columnname carglist.
drh2881ab62016-02-27 23:25:36 +0000187columnname(A) ::= nm(A) typetoken(Y). {sqlite3AddColumn(pParse,&A,&Y);}
drhc4a3c772001-04-04 11:48:57 +0000188
drh6a8700b2017-08-02 11:04:00 +0000189// Declare some tokens early in order to influence their values, to
190// improve performance and reduce the executable size. The goal here is
191// to get the "jump" operations in ISNULL through ESCAPE to have numeric
192// values that are early enough so that all jump operations are clustered
193// at the beginning, but also so that the comparison tokens NE through GE
194// are as large as possible so that they are near to FUNCTION, which is a
195// token synthesized by addopcodes.tcl.
196//
197%token ABORT ACTION AFTER ANALYZE ASC ATTACH BEFORE BEGIN BY CASCADE CAST.
198%token CONFLICT DATABASE DEFERRED DESC DETACH EACH END EXCLUSIVE EXPLAIN FAIL.
199%token OR AND NOT IS MATCH LIKE_KW BETWEEN IN ISNULL NOTNULL NE EQ.
200%token GT LE LT GE ESCAPE.
201
drh31d6fd52017-04-14 19:03:10 +0000202// The following directive causes tokens ABORT, AFTER, ASC, etc. to
203// fallback to ID if they will not parse as their original value.
204// This obviates the need for the "id" nonterminal.
205//
206%fallback ID
207 ABORT ACTION AFTER ANALYZE ASC ATTACH BEFORE BEGIN BY CASCADE CAST COLUMNKW
208 CONFLICT DATABASE DEFERRED DESC DETACH EACH END EXCLUSIVE EXPLAIN FAIL FOR
209 IGNORE IMMEDIATE INITIALLY INSTEAD LIKE_KW MATCH NO PLAN
210 QUERY KEY OF OFFSET PRAGMA RAISE RECURSIVE RELEASE REPLACE RESTRICT ROW
211 ROLLBACK SAVEPOINT TEMP TRIGGER VACUUM VIEW VIRTUAL WITH WITHOUT
212%ifdef SQLITE_OMIT_COMPOUND_SELECT
213 EXCEPT INTERSECT UNION
214%endif SQLITE_OMIT_COMPOUND_SELECT
215 REINDEX RENAME CTIME_KW IF
216 .
217%wildcard ANY.
218
drhf7b54962013-05-28 12:11:54 +0000219// Define operator precedence early so that this is the first occurrence
drh2d3917d2004-02-22 16:27:00 +0000220// of the operator tokens in the grammer. Keeping the operators together
221// causes them to be assigned integer values that are close together,
222// which keeps parser tables smaller.
223//
drhf2bc0132004-10-04 13:19:23 +0000224// The token values assigned to these symbols is determined by the order
225// in which lemon first sees them. It must be the case that ISNULL/NOTNULL,
226// NE/EQ, GT/LE, and GE/LT are separated by only a single value. See
227// the sqlite3ExprIfFalse() routine for additional information on this
228// constraint.
229//
drh2d3917d2004-02-22 16:27:00 +0000230%left OR.
231%left AND.
232%right NOT.
drh03bea702006-06-13 15:37:26 +0000233%left IS MATCH LIKE_KW BETWEEN IN ISNULL NOTNULL NE EQ.
drh9a432672004-10-04 13:38:09 +0000234%left GT LE LT GE.
danielk19777c6303c2004-11-17 16:41:29 +0000235%right ESCAPE.
drh2d3917d2004-02-22 16:27:00 +0000236%left BITAND BITOR LSHIFT RSHIFT.
237%left PLUS MINUS.
238%left STAR SLASH REM.
drha34001c2007-02-02 12:44:37 +0000239%left CONCAT.
240%left COLLATE.
drh7ba5bc52009-09-22 20:08:34 +0000241%right BITNOT.
drh2d3917d2004-02-22 16:27:00 +0000242
drh7cc84c22016-04-11 13:36:42 +0000243// An IDENTIFIER can be a generic identifier, or one of several
244// keywords. Any non-standard keyword can also be an identifier.
245//
246%token_class id ID|INDEXED.
247
drh7cc84c22016-04-11 13:36:42 +0000248
drhc4a3c772001-04-04 11:48:57 +0000249// And "ids" is an identifer-or-string.
250//
drhf59b12f2014-01-11 03:54:05 +0000251%token_class ids ID|STRING.
drhc4a3c772001-04-04 11:48:57 +0000252
drh5ad1a6c2002-07-01 12:27:09 +0000253// The name of a column or table can be any of the following:
254//
255%type nm {Token}
drh4dd0d3f2016-02-17 01:18:33 +0000256nm(A) ::= id(A).
257nm(A) ::= STRING(A).
258nm(A) ::= JOIN_KW(A).
drh5ad1a6c2002-07-01 12:27:09 +0000259
drh986dde72016-02-29 13:37:21 +0000260// A typetoken is really zero or more tokens that form a type name such
drh487e2622005-06-25 18:42:14 +0000261// as can be found after the column name in a CREATE TABLE statement.
262// Multiple tokens are concatenated to form the value of the typetoken.
263//
264%type typetoken {Token}
drh986dde72016-02-29 13:37:21 +0000265typetoken(A) ::= . {A.n = 0; A.z = 0;}
drh4dd0d3f2016-02-17 01:18:33 +0000266typetoken(A) ::= typename(A).
267typetoken(A) ::= typename(A) LP signed RP(Y). {
268 A.n = (int)(&Y.z[Y.n] - A.z);
drh487e2622005-06-25 18:42:14 +0000269}
drh4dd0d3f2016-02-17 01:18:33 +0000270typetoken(A) ::= typename(A) LP signed COMMA signed RP(Y). {
271 A.n = (int)(&Y.z[Y.n] - A.z);
drh487e2622005-06-25 18:42:14 +0000272}
drh382c0242001-10-06 16:33:02 +0000273%type typename {Token}
drh4dd0d3f2016-02-17 01:18:33 +0000274typename(A) ::= ids(A).
275typename(A) ::= typename(A) ids(Y). {A.n=Y.n+(int)(Y.z-A.z);}
drh60218d22007-04-06 11:26:00 +0000276signed ::= plus_num.
277signed ::= minus_num.
drh487e2622005-06-25 18:42:14 +0000278
drh1be266b2017-12-24 00:18:47 +0000279// The scanpt non-terminal takes a value which is a pointer to the
280// input text just past the last token that has been shifted into
281// the parser. By surrounding some phrase in the grammar with two
282// scanpt non-terminals, we can capture the input text for that phrase.
283// For example:
284//
285// something ::= .... scanpt(A) phrase scanpt(Z).
286//
287// The text that is parsed as "phrase" is a string starting at A
288// and containing (int)(Z-A) characters. There might be some extra
289// whitespace on either end of the text, but that can be removed in
290// post-processing, if needed.
291//
292%type scanpt {const char*}
293scanpt(A) ::= . {
drhf259df52017-12-27 20:38:35 +0000294 assert( yyLookahead!=YYNOCODE );
295 A = yyLookaheadToken.z;
drh1be266b2017-12-24 00:18:47 +0000296}
297
drh487e2622005-06-25 18:42:14 +0000298// "carglist" is a list of additional constraints that come after the
299// column name and column type in a CREATE TABLE statement.
300//
drh4dc330d2012-05-07 19:21:36 +0000301carglist ::= carglist ccons.
drh348784e2000-05-29 20:41:49 +0000302carglist ::= .
drh4dc330d2012-05-07 19:21:36 +0000303ccons ::= CONSTRAINT nm(X). {pParse->constraintName = X;}
drh1be266b2017-12-24 00:18:47 +0000304ccons ::= DEFAULT scanpt(A) term(X) scanpt(Z).
305 {sqlite3AddDefaultValue(pParse,X,A,Z);}
306ccons ::= DEFAULT LP(A) expr(X) RP(Z).
307 {sqlite3AddDefaultValue(pParse,X,A.z+1,Z.z);}
308ccons ::= DEFAULT PLUS(A) term(X) scanpt(Z).
309 {sqlite3AddDefaultValue(pParse,X,A.z,Z);}
310ccons ::= DEFAULT MINUS(A) term(X) scanpt(Z). {
311 Expr *p = sqlite3PExpr(pParse, TK_UMINUS, X, 0);
312 sqlite3AddDefaultValue(pParse,p,A.z,Z);
danielk19777977a172004-11-09 12:44:37 +0000313}
drh1be266b2017-12-24 00:18:47 +0000314ccons ::= DEFAULT scanpt id(X). {
315 Expr *p = tokenExpr(pParse, TK_STRING, X);
drhd7fd8992018-02-28 04:30:55 +0000316 if( p ){
317 sqlite3ExprIdToTrueFalse(p);
318 testcase( p->op==TK_TRUEFALSE && sqlite3ExprTruthValue(p) );
319 }
drh1be266b2017-12-24 00:18:47 +0000320 sqlite3AddDefaultValue(pParse,p,X.z,X.z+X.n);
danielk19777977a172004-11-09 12:44:37 +0000321}
drh348784e2000-05-29 20:41:49 +0000322
drh382c0242001-10-06 16:33:02 +0000323// In addition to the type name, we also care about the primary key and
324// UNIQUE constraints.
drh348784e2000-05-29 20:41:49 +0000325//
drh0d316a42002-08-11 20:10:47 +0000326ccons ::= NULL onconf.
drhb7916a72009-05-27 10:31:29 +0000327ccons ::= NOT NULL onconf(R). {sqlite3AddNotNull(pParse, R);}
drhfdd6e852005-12-16 01:06:16 +0000328ccons ::= PRIMARY KEY sortorder(Z) onconf(R) autoinc(I).
drhb7916a72009-05-27 10:31:29 +0000329 {sqlite3AddPrimaryKey(pParse,0,R,I,Z);}
drh62340f82016-05-31 21:18:15 +0000330ccons ::= UNIQUE onconf(R). {sqlite3CreateIndex(pParse,0,0,0,0,R,0,0,0,0,
331 SQLITE_IDXTYPE_UNIQUE);}
drh1be266b2017-12-24 00:18:47 +0000332ccons ::= CHECK LP expr(X) RP. {sqlite3AddCheckConstraint(pParse,X);}
drh108aa002015-08-24 20:21:20 +0000333ccons ::= REFERENCES nm(T) eidlist_opt(TA) refargs(R).
drhb7916a72009-05-27 10:31:29 +0000334 {sqlite3CreateForeignKey(pParse,0,&T,TA,R);}
335ccons ::= defer_subclause(D). {sqlite3DeferForeignKey(pParse,D);}
336ccons ::= COLLATE ids(C). {sqlite3AddCollateType(pParse, &C);}
drh04738cb2002-06-02 18:19:00 +0000337
drh205f48e2004-11-05 00:43:11 +0000338// The optional AUTOINCREMENT keyword
339%type autoinc {int}
drh2958a4e2004-11-12 03:56:15 +0000340autoinc(X) ::= . {X = 0;}
341autoinc(X) ::= AUTOINCR. {X = 1;}
drh205f48e2004-11-05 00:43:11 +0000342
drhc2eef3b2002-08-31 18:53:06 +0000343// The next group of rules parses the arguments to a REFERENCES clause
344// that determine if the referential integrity checking is deferred or
345// or immediate and which determine what action to take if a ref-integ
346// check fails.
drh04738cb2002-06-02 18:19:00 +0000347//
drhc2eef3b2002-08-31 18:53:06 +0000348%type refargs {int}
drhfcf486c2009-10-21 13:48:24 +0000349refargs(A) ::= . { A = OE_None*0x0101; /* EV: R-19803-45884 */}
drh4dd0d3f2016-02-17 01:18:33 +0000350refargs(A) ::= refargs(A) refarg(Y). { A = (A & ~Y.mask) | Y.value; }
drhc2eef3b2002-08-31 18:53:06 +0000351%type refarg {struct {int value; int mask;}}
352refarg(A) ::= MATCH nm. { A.value = 0; A.mask = 0x000000; }
drhc29c5aa12009-12-09 21:43:36 +0000353refarg(A) ::= ON INSERT refact. { A.value = 0; A.mask = 0x000000; }
drhc2eef3b2002-08-31 18:53:06 +0000354refarg(A) ::= ON DELETE refact(X). { A.value = X; A.mask = 0x0000ff; }
355refarg(A) ::= ON UPDATE refact(X). { A.value = X<<8; A.mask = 0x00ff00; }
drhc2eef3b2002-08-31 18:53:06 +0000356%type refact {int}
drhfcf486c2009-10-21 13:48:24 +0000357refact(A) ::= SET NULL. { A = OE_SetNull; /* EV: R-33326-45252 */}
358refact(A) ::= SET DEFAULT. { A = OE_SetDflt; /* EV: R-33326-45252 */}
359refact(A) ::= CASCADE. { A = OE_Cascade; /* EV: R-33326-45252 */}
360refact(A) ::= RESTRICT. { A = OE_Restrict; /* EV: R-33326-45252 */}
361refact(A) ::= NO ACTION. { A = OE_None; /* EV: R-33326-45252 */}
drhc2eef3b2002-08-31 18:53:06 +0000362%type defer_subclause {int}
dan1da40a32009-09-19 17:00:31 +0000363defer_subclause(A) ::= NOT DEFERRABLE init_deferred_pred_opt. {A = 0;}
drhc2eef3b2002-08-31 18:53:06 +0000364defer_subclause(A) ::= DEFERRABLE init_deferred_pred_opt(X). {A = X;}
365%type init_deferred_pred_opt {int}
366init_deferred_pred_opt(A) ::= . {A = 0;}
367init_deferred_pred_opt(A) ::= INITIALLY DEFERRED. {A = 1;}
368init_deferred_pred_opt(A) ::= INITIALLY IMMEDIATE. {A = 0;}
drh348784e2000-05-29 20:41:49 +0000369
drhaeb281c2012-05-08 11:17:33 +0000370conslist_opt(A) ::= . {A.n = 0; A.z = 0;}
drh4dd0d3f2016-02-17 01:18:33 +0000371conslist_opt(A) ::= COMMA(A) conslist.
drhab35eae2012-05-12 18:29:53 +0000372conslist ::= conslist tconscomma tcons.
373conslist ::= tcons.
374tconscomma ::= COMMA. {pParse->constraintName.n = 0;}
375tconscomma ::= .
376tcons ::= CONSTRAINT nm(X). {pParse->constraintName = X;}
drh108aa002015-08-24 20:21:20 +0000377tcons ::= PRIMARY KEY LP sortlist(X) autoinc(I) RP onconf(R).
drhb7916a72009-05-27 10:31:29 +0000378 {sqlite3AddPrimaryKey(pParse,X,R,I,0);}
drh108aa002015-08-24 20:21:20 +0000379tcons ::= UNIQUE LP sortlist(X) RP onconf(R).
drh62340f82016-05-31 21:18:15 +0000380 {sqlite3CreateIndex(pParse,0,0,0,X,R,0,0,0,0,
381 SQLITE_IDXTYPE_UNIQUE);}
drhb7916a72009-05-27 10:31:29 +0000382tcons ::= CHECK LP expr(E) RP onconf.
drh1be266b2017-12-24 00:18:47 +0000383 {sqlite3AddCheckConstraint(pParse,E);}
drh108aa002015-08-24 20:21:20 +0000384tcons ::= FOREIGN KEY LP eidlist(FA) RP
385 REFERENCES nm(T) eidlist_opt(TA) refargs(R) defer_subclause_opt(D). {
danielk19774adee202004-05-08 08:23:19 +0000386 sqlite3CreateForeignKey(pParse, FA, &T, TA, R);
387 sqlite3DeferForeignKey(pParse, D);
drhc2eef3b2002-08-31 18:53:06 +0000388}
389%type defer_subclause_opt {int}
390defer_subclause_opt(A) ::= . {A = 0;}
drh4dd0d3f2016-02-17 01:18:33 +0000391defer_subclause_opt(A) ::= defer_subclause(A).
drh9cfcf5d2002-01-29 18:41:24 +0000392
393// The following is a non-standard extension that allows us to declare the
394// default behavior when there is a constraint conflict.
395//
396%type onconf {int}
drh3334d082015-11-10 13:45:21 +0000397%type orconf {int}
drh1c928532002-01-31 15:54:21 +0000398%type resolvetype {int}
drh74ad7fe2004-10-07 03:06:28 +0000399onconf(A) ::= . {A = OE_Default;}
400onconf(A) ::= ON CONFLICT resolvetype(X). {A = X;}
401orconf(A) ::= . {A = OE_Default;}
drh3334d082015-11-10 13:45:21 +0000402orconf(A) ::= OR resolvetype(X). {A = X;}
drh4dd0d3f2016-02-17 01:18:33 +0000403resolvetype(A) ::= raisetype(A).
drh74ad7fe2004-10-07 03:06:28 +0000404resolvetype(A) ::= IGNORE. {A = OE_Ignore;}
405resolvetype(A) ::= REPLACE. {A = OE_Replace;}
drh348784e2000-05-29 20:41:49 +0000406
drh382c0242001-10-06 16:33:02 +0000407////////////////////////// The DROP TABLE /////////////////////////////////////
drh348784e2000-05-29 20:41:49 +0000408//
drha0733842005-12-29 01:11:36 +0000409cmd ::= DROP TABLE ifexists(E) fullname(X). {
410 sqlite3DropTable(pParse, X, 0, E);
danielk1977a8858102004-05-28 12:11:21 +0000411}
drha0733842005-12-29 01:11:36 +0000412%type ifexists {int}
413ifexists(A) ::= IF EXISTS. {A = 1;}
414ifexists(A) ::= . {A = 0;}
drh348784e2000-05-29 20:41:49 +0000415
drha76b5df2002-02-23 02:32:10 +0000416///////////////////// The CREATE VIEW statement /////////////////////////////
417//
drhb7f91642004-10-31 02:22:47 +0000418%ifndef SQLITE_OMIT_VIEW
drh108aa002015-08-24 20:21:20 +0000419cmd ::= createkw(X) temp(T) VIEW ifnotexists(E) nm(Y) dbnm(Z) eidlist_opt(C)
drh8981b902015-08-24 17:42:49 +0000420 AS select(S). {
421 sqlite3CreateView(pParse, &X, &Y, &Z, C, S, T, E);
drha76b5df2002-02-23 02:32:10 +0000422}
drha0733842005-12-29 01:11:36 +0000423cmd ::= DROP VIEW ifexists(E) fullname(X). {
424 sqlite3DropTable(pParse, X, 1, E);
drha76b5df2002-02-23 02:32:10 +0000425}
drh154d4b22006-09-21 11:02:16 +0000426%endif SQLITE_OMIT_VIEW
drha76b5df2002-02-23 02:32:10 +0000427
drh382c0242001-10-06 16:33:02 +0000428//////////////////////// The SELECT statement /////////////////////////////////
drh348784e2000-05-29 20:41:49 +0000429//
dan7d562db2014-01-11 19:19:36 +0000430cmd ::= select(X). {
drhedf83d12014-01-22 18:31:27 +0000431 SelectDest dest = {SRT_Output, 0, 0, 0, 0, 0};
drh7d10d5a2008-08-20 16:35:10 +0000432 sqlite3Select(pParse, X, &dest);
drh633e6d52008-07-28 19:34:53 +0000433 sqlite3SelectDelete(pParse->db, X);
drh9bb61fe2000-06-05 16:01:39 +0000434}
drhefb72512000-05-31 20:00:52 +0000435
drh9bb61fe2000-06-05 16:01:39 +0000436%type select {Select*}
drh633e6d52008-07-28 19:34:53 +0000437%destructor select {sqlite3SelectDelete(pParse->db, $$);}
dan7d562db2014-01-11 19:19:36 +0000438%type selectnowith {Select*}
439%destructor selectnowith {sqlite3SelectDelete(pParse->db, $$);}
drh82c3d632000-06-06 21:56:07 +0000440%type oneselect {Select*}
drh633e6d52008-07-28 19:34:53 +0000441%destructor oneselect {sqlite3SelectDelete(pParse->db, $$);}
drh9bb61fe2000-06-05 16:01:39 +0000442
drh772460f2015-04-16 14:13:12 +0000443%include {
444 /*
445 ** For a compound SELECT statement, make sure p->pPrior->pNext==p for
446 ** all elements in the list. And make sure list length does not exceed
447 ** SQLITE_LIMIT_COMPOUND_SELECT.
448 */
drhe318a7f2015-04-16 23:04:17 +0000449 static void parserDoubleLinkSelect(Parse *pParse, Select *p){
drhd227a292014-02-09 18:02:09 +0000450 if( p->pPrior ){
drh772460f2015-04-16 14:13:12 +0000451 Select *pNext = 0, *pLoop;
452 int mxSelect, cnt = 0;
drhd227a292014-02-09 18:02:09 +0000453 for(pLoop=p; pLoop; pNext=pLoop, pLoop=pLoop->pPrior, cnt++){
454 pLoop->pNext = pNext;
455 pLoop->selFlags |= SF_Compound;
456 }
drh772460f2015-04-16 14:13:12 +0000457 if( (p->selFlags & SF_MultiValue)==0 &&
458 (mxSelect = pParse->db->aLimit[SQLITE_LIMIT_COMPOUND_SELECT])>0 &&
459 cnt>mxSelect
drha0c01762015-01-05 16:27:43 +0000460 ){
drhd227a292014-02-09 18:02:09 +0000461 sqlite3ErrorMsg(pParse, "too many terms in compound SELECT");
462 }
463 }
drh772460f2015-04-16 14:13:12 +0000464 }
465}
466
467select(A) ::= with(W) selectnowith(X). {
468 Select *p = X;
469 if( p ){
470 p->pWith = W;
471 parserDoubleLinkSelect(pParse, p);
dana9f5c132014-01-13 16:36:40 +0000472 }else{
473 sqlite3WithDelete(pParse->db, W);
474 }
drhcf82f0d2016-02-17 04:33:10 +0000475 A = p; /*A-overwrites-W*/
dana9f5c132014-01-13 16:36:40 +0000476}
dan7d562db2014-01-11 19:19:36 +0000477
drh4dd0d3f2016-02-17 01:18:33 +0000478selectnowith(A) ::= oneselect(A).
drhb7f91642004-10-31 02:22:47 +0000479%ifndef SQLITE_OMIT_COMPOUND_SELECT
drh4dd0d3f2016-02-17 01:18:33 +0000480selectnowith(A) ::= selectnowith(A) multiselect_op(Y) oneselect(Z). {
drhc0bf4932014-02-19 01:31:02 +0000481 Select *pRhs = Z;
drh4dd0d3f2016-02-17 01:18:33 +0000482 Select *pLhs = A;
drhc0bf4932014-02-19 01:31:02 +0000483 if( pRhs && pRhs->pPrior ){
484 SrcList *pFrom;
485 Token x;
486 x.n = 0;
drh772460f2015-04-16 14:13:12 +0000487 parserDoubleLinkSelect(pParse, pRhs);
drhc0bf4932014-02-19 01:31:02 +0000488 pFrom = sqlite3SrcListAppendFromTerm(pParse,0,0,0,&x,pRhs,0,0);
drh8c0833f2017-11-14 23:48:23 +0000489 pRhs = sqlite3SelectNew(pParse,0,pFrom,0,0,0,0,0,0);
drhc0bf4932014-02-19 01:31:02 +0000490 }
491 if( pRhs ){
492 pRhs->op = (u8)Y;
drh00d5ab72015-05-20 00:15:27 +0000493 pRhs->pPrior = pLhs;
494 if( ALWAYS(pLhs) ) pLhs->selFlags &= ~SF_MultiValue;
drh772460f2015-04-16 14:13:12 +0000495 pRhs->selFlags &= ~SF_MultiValue;
drhd58d3272013-08-05 22:05:02 +0000496 if( Y!=TK_ALL ) pParse->hasCompound = 1;
drh43b78822007-06-15 17:03:14 +0000497 }else{
drh00d5ab72015-05-20 00:15:27 +0000498 sqlite3SelectDelete(pParse->db, pLhs);
drhdaffd0e2001-04-11 14:28:42 +0000499 }
drhc0bf4932014-02-19 01:31:02 +0000500 A = pRhs;
drh82c3d632000-06-06 21:56:07 +0000501}
drh0a36c572002-02-18 22:49:59 +0000502%type multiselect_op {int}
drhcf82f0d2016-02-17 04:33:10 +0000503multiselect_op(A) ::= UNION(OP). {A = @OP; /*A-overwrites-OP*/}
drhfd405312005-11-06 04:06:59 +0000504multiselect_op(A) ::= UNION ALL. {A = TK_ALL;}
drhcf82f0d2016-02-17 04:33:10 +0000505multiselect_op(A) ::= EXCEPT|INTERSECT(OP). {A = @OP; /*A-overwrites-OP*/}
drh154d4b22006-09-21 11:02:16 +0000506%endif SQLITE_OMIT_COMPOUND_SELECT
drhabd4c722014-09-20 18:18:33 +0000507oneselect(A) ::= SELECT(S) distinct(D) selcollist(W) from(X) where_opt(Y)
drh9bbca4c2001-11-06 04:00:18 +0000508 groupby_opt(P) having_opt(Q) orderby_opt(Z) limit_opt(L). {
drh43303de2016-02-17 12:34:03 +0000509#if SELECTTRACE_ENABLED
510 Token s = S; /*A-overwrites-S*/
511#endif
drh8c0833f2017-11-14 23:48:23 +0000512 A = sqlite3SelectNew(pParse,W,X,Y,P,Q,Z,D,L);
drhabd4c722014-09-20 18:18:33 +0000513#if SELECTTRACE_ENABLED
drheb9b8842014-09-21 00:27:26 +0000514 /* Populate the Select.zSelName[] string that is used to help with
drhabd4c722014-09-20 18:18:33 +0000515 ** query planner debugging, to differentiate between multiple Select
516 ** objects in a complex query.
517 **
518 ** If the SELECT keyword is immediately followed by a C-style comment
519 ** then extract the first few alphanumeric characters from within that
drheb9b8842014-09-21 00:27:26 +0000520 ** comment to be the zSelName value. Otherwise, the label is #N where
drhabd4c722014-09-20 18:18:33 +0000521 ** is an integer that is incremented with each SELECT statement seen.
522 */
523 if( A!=0 ){
drh43303de2016-02-17 12:34:03 +0000524 const char *z = s.z+6;
drhabd4c722014-09-20 18:18:33 +0000525 int i;
drheb9b8842014-09-21 00:27:26 +0000526 sqlite3_snprintf(sizeof(A->zSelName), A->zSelName, "#%d",
drhabd4c722014-09-20 18:18:33 +0000527 ++pParse->nSelect);
528 while( z[0]==' ' ) z++;
529 if( z[0]=='/' && z[1]=='*' ){
530 z += 2;
531 while( z[0]==' ' ) z++;
532 for(i=0; sqlite3Isalnum(z[i]); i++){}
drheb9b8842014-09-21 00:27:26 +0000533 sqlite3_snprintf(sizeof(A->zSelName), A->zSelName, "%.*s", i, z);
drhabd4c722014-09-20 18:18:33 +0000534 }
535 }
536#endif /* SELECTRACE_ENABLED */
drh9bb61fe2000-06-05 16:01:39 +0000537}
drh4dd0d3f2016-02-17 01:18:33 +0000538oneselect(A) ::= values(A).
drh75593d92014-01-10 20:46:55 +0000539
540%type values {Select*}
541%destructor values {sqlite3SelectDelete(pParse->db, $$);}
542values(A) ::= VALUES LP nexprlist(X) RP. {
drh8c0833f2017-11-14 23:48:23 +0000543 A = sqlite3SelectNew(pParse,X,0,0,0,0,0,SF_Values,0);
drh75593d92014-01-10 20:46:55 +0000544}
drh4dd0d3f2016-02-17 01:18:33 +0000545values(A) ::= values(A) COMMA LP exprlist(Y) RP. {
546 Select *pRight, *pLeft = A;
drh8c0833f2017-11-14 23:48:23 +0000547 pRight = sqlite3SelectNew(pParse,Y,0,0,0,0,0,SF_Values|SF_MultiValue,0);
drhf3151f02015-04-16 20:27:09 +0000548 if( ALWAYS(pLeft) ) pLeft->selFlags &= ~SF_MultiValue;
drh75593d92014-01-10 20:46:55 +0000549 if( pRight ){
550 pRight->op = TK_ALL;
drh772460f2015-04-16 14:13:12 +0000551 pRight->pPrior = pLeft;
drh75593d92014-01-10 20:46:55 +0000552 A = pRight;
553 }else{
drh772460f2015-04-16 14:13:12 +0000554 A = pLeft;
drh75593d92014-01-10 20:46:55 +0000555 }
556}
drh9bb61fe2000-06-05 16:01:39 +0000557
558// The "distinct" nonterminal is true (1) if the DISTINCT keyword is
559// present and false (0) if it is not.
560//
drh3334d082015-11-10 13:45:21 +0000561%type distinct {int}
drh832ee3d2012-12-18 19:36:11 +0000562distinct(A) ::= DISTINCT. {A = SF_Distinct;}
drh7cea7f92015-05-29 01:35:19 +0000563distinct(A) ::= ALL. {A = SF_All;}
drhefb72512000-05-31 20:00:52 +0000564distinct(A) ::= . {A = 0;}
drh348784e2000-05-29 20:41:49 +0000565
drh9bb61fe2000-06-05 16:01:39 +0000566// selcollist is a list of expressions that are to become the return
drh7c917d12001-12-16 20:05:05 +0000567// values of the SELECT statement. The "*" in statements like
568// "SELECT * FROM ..." is encoded as a special expression with an
drh1a1d3cd2015-11-19 16:33:31 +0000569// opcode of TK_ASTERISK.
drh9bb61fe2000-06-05 16:01:39 +0000570//
drh348784e2000-05-29 20:41:49 +0000571%type selcollist {ExprList*}
drh633e6d52008-07-28 19:34:53 +0000572%destructor selcollist {sqlite3ExprListDelete(pParse->db, $$);}
drh348784e2000-05-29 20:41:49 +0000573%type sclp {ExprList*}
drh633e6d52008-07-28 19:34:53 +0000574%destructor sclp {sqlite3ExprListDelete(pParse->db, $$);}
drh4dd0d3f2016-02-17 01:18:33 +0000575sclp(A) ::= selcollist(A) COMMA.
drh348784e2000-05-29 20:41:49 +0000576sclp(A) ::= . {A = 0;}
drh1be266b2017-12-24 00:18:47 +0000577selcollist(A) ::= sclp(A) scanpt(B) expr(X) scanpt(Z) as(Y). {
578 A = sqlite3ExprListAppend(pParse, A, X);
drhb7916a72009-05-27 10:31:29 +0000579 if( Y.n>0 ) sqlite3ExprListSetName(pParse, A, &Y, 1);
drh1be266b2017-12-24 00:18:47 +0000580 sqlite3ExprListSetSpan(pParse,A,B,Z);
drh01f3f252002-05-24 16:14:15 +0000581}
drhd3f5d612017-12-24 17:01:54 +0000582selcollist(A) ::= sclp(A) scanpt STAR. {
drh1a1d3cd2015-11-19 16:33:31 +0000583 Expr *p = sqlite3Expr(pParse->db, TK_ASTERISK, 0);
drh4dd0d3f2016-02-17 01:18:33 +0000584 A = sqlite3ExprListAppend(pParse, A, p);
drh7c917d12001-12-16 20:05:05 +0000585}
drh1be266b2017-12-24 00:18:47 +0000586selcollist(A) ::= sclp(A) scanpt nm(X) DOT STAR. {
drhabfd35e2016-12-06 22:47:23 +0000587 Expr *pRight = sqlite3PExpr(pParse, TK_ASTERISK, 0, 0);
588 Expr *pLeft = sqlite3ExprAlloc(pParse->db, TK_ID, &X, 1);
589 Expr *pDot = sqlite3PExpr(pParse, TK_DOT, pLeft, pRight);
drh4dd0d3f2016-02-17 01:18:33 +0000590 A = sqlite3ExprListAppend(pParse,A, pDot);
drh54473222002-04-04 02:10:55 +0000591}
drh01f3f252002-05-24 16:14:15 +0000592
593// An option "AS <id>" phrase that can follow one of the expressions that
594// define the result set, or one of the tables in the FROM clause.
595//
596%type as {Token}
drh74ad7fe2004-10-07 03:06:28 +0000597as(X) ::= AS nm(Y). {X = Y;}
drh4dd0d3f2016-02-17 01:18:33 +0000598as(X) ::= ids(X).
drh986dde72016-02-29 13:37:21 +0000599as(X) ::= . {X.n = 0; X.z = 0;}
drh9bb61fe2000-06-05 16:01:39 +0000600
drh348784e2000-05-29 20:41:49 +0000601
drhad3cab52002-05-24 02:04:32 +0000602%type seltablist {SrcList*}
drh633e6d52008-07-28 19:34:53 +0000603%destructor seltablist {sqlite3SrcListDelete(pParse->db, $$);}
drhad3cab52002-05-24 02:04:32 +0000604%type stl_prefix {SrcList*}
drh633e6d52008-07-28 19:34:53 +0000605%destructor stl_prefix {sqlite3SrcListDelete(pParse->db, $$);}
drhad3cab52002-05-24 02:04:32 +0000606%type from {SrcList*}
drh633e6d52008-07-28 19:34:53 +0000607%destructor from {sqlite3SrcListDelete(pParse->db, $$);}
drh348784e2000-05-29 20:41:49 +0000608
drh01f3f252002-05-24 16:14:15 +0000609// A complete FROM clause.
610//
drh17435752007-08-16 04:30:38 +0000611from(A) ::= . {A = sqlite3DbMallocZero(pParse->db, sizeof(*A));}
drhfbdc7f62008-12-03 23:23:40 +0000612from(A) ::= FROM seltablist(X). {
drh61dfc312006-12-16 16:25:15 +0000613 A = X;
614 sqlite3SrcListShiftJoinType(A);
615}
drh01f3f252002-05-24 16:14:15 +0000616
617// "seltablist" is a "Select Table List" - the content of the FROM clause
618// in a SELECT statement. "stl_prefix" is a prefix of this list.
619//
drh4dd0d3f2016-02-17 01:18:33 +0000620stl_prefix(A) ::= seltablist(A) joinop(Y). {
drh8a48b9c2015-08-19 15:20:00 +0000621 if( ALWAYS(A && A->nSrc>0) ) A->a[A->nSrc-1].fg.jointype = (u8)Y;
drh01f3f252002-05-24 16:14:15 +0000622}
drh348784e2000-05-29 20:41:49 +0000623stl_prefix(A) ::= . {A = 0;}
drh4dd0d3f2016-02-17 01:18:33 +0000624seltablist(A) ::= stl_prefix(A) nm(Y) dbnm(D) as(Z) indexed_opt(I)
drhe9240412012-12-18 13:12:03 +0000625 on_opt(N) using_opt(U). {
drh4dd0d3f2016-02-17 01:18:33 +0000626 A = sqlite3SrcListAppendFromTerm(pParse,A,&Y,&D,&Z,0,N,U);
danielk1977b1c685b2008-10-06 16:18:39 +0000627 sqlite3SrcListIndexedBy(pParse, A, &I);
drhc4a3c772001-04-04 11:48:57 +0000628}
drh4dd0d3f2016-02-17 01:18:33 +0000629seltablist(A) ::= stl_prefix(A) nm(Y) dbnm(D) LP exprlist(E) RP as(Z)
drh01d230c2015-08-19 17:11:37 +0000630 on_opt(N) using_opt(U). {
drh4dd0d3f2016-02-17 01:18:33 +0000631 A = sqlite3SrcListAppendFromTerm(pParse,A,&Y,&D,&Z,0,N,U);
drh01d230c2015-08-19 17:11:37 +0000632 sqlite3SrcListFuncArgs(pParse, A, E);
633}
drh51522cd2005-01-20 13:36:19 +0000634%ifndef SQLITE_OMIT_SUBQUERY
drh4dd0d3f2016-02-17 01:18:33 +0000635 seltablist(A) ::= stl_prefix(A) LP select(S) RP
drh51522cd2005-01-20 13:36:19 +0000636 as(Z) on_opt(N) using_opt(U). {
drh4dd0d3f2016-02-17 01:18:33 +0000637 A = sqlite3SrcListAppendFromTerm(pParse,A,0,0,&Z,S,N,U);
drhd5feede2002-05-08 21:46:14 +0000638 }
drh4dd0d3f2016-02-17 01:18:33 +0000639 seltablist(A) ::= stl_prefix(A) LP seltablist(F) RP
drhfbdc7f62008-12-03 23:23:40 +0000640 as(Z) on_opt(N) using_opt(U). {
drh4dd0d3f2016-02-17 01:18:33 +0000641 if( A==0 && Z.n==0 && N==0 && U==0 ){
drhfbdc7f62008-12-03 23:23:40 +0000642 A = F;
drh832ee3d2012-12-18 19:36:11 +0000643 }else if( F->nSrc==1 ){
drh4dd0d3f2016-02-17 01:18:33 +0000644 A = sqlite3SrcListAppendFromTerm(pParse,A,0,0,&Z,0,N,U);
drh832ee3d2012-12-18 19:36:11 +0000645 if( A ){
646 struct SrcList_item *pNew = &A->a[A->nSrc-1];
647 struct SrcList_item *pOld = F->a;
648 pNew->zName = pOld->zName;
649 pNew->zDatabase = pOld->zDatabase;
drh3c449c62013-04-30 14:06:57 +0000650 pNew->pSelect = pOld->pSelect;
drh832ee3d2012-12-18 19:36:11 +0000651 pOld->zName = pOld->zDatabase = 0;
drh3c449c62013-04-30 14:06:57 +0000652 pOld->pSelect = 0;
drh832ee3d2012-12-18 19:36:11 +0000653 }
654 sqlite3SrcListDelete(pParse->db, F);
drhfbdc7f62008-12-03 23:23:40 +0000655 }else{
656 Select *pSubquery;
657 sqlite3SrcListShiftJoinType(F);
drh8c0833f2017-11-14 23:48:23 +0000658 pSubquery = sqlite3SelectNew(pParse,0,F,0,0,0,0,SF_NestedFrom,0);
drh4dd0d3f2016-02-17 01:18:33 +0000659 A = sqlite3SrcListAppendFromTerm(pParse,A,0,0,&Z,pSubquery,N,U);
drhfbdc7f62008-12-03 23:23:40 +0000660 }
661 }
drh154d4b22006-09-21 11:02:16 +0000662%endif SQLITE_OMIT_SUBQUERY
drhb733d032004-01-24 20:18:12 +0000663
drh113088e2003-03-20 01:16:58 +0000664%type dbnm {Token}
665dbnm(A) ::= . {A.z=0; A.n=0;}
666dbnm(A) ::= DOT nm(X). {A = X;}
667
drh74ad7fe2004-10-07 03:06:28 +0000668%type fullname {SrcList*}
drh633e6d52008-07-28 19:34:53 +0000669%destructor fullname {sqlite3SrcListDelete(pParse->db, $$);}
drhcf82f0d2016-02-17 04:33:10 +0000670fullname(A) ::= nm(X) dbnm(Y).
671 {A = sqlite3SrcListAppend(pParse->db,0,&X,&Y); /*A-overwrites-X*/}
drh74ad7fe2004-10-07 03:06:28 +0000672
drh01f3f252002-05-24 16:14:15 +0000673%type joinop {int}
drhfd405312005-11-06 04:06:59 +0000674joinop(X) ::= COMMA|JOIN. { X = JT_INNER; }
drhcf82f0d2016-02-17 04:33:10 +0000675joinop(X) ::= JOIN_KW(A) JOIN.
676 {X = sqlite3JoinType(pParse,&A,0,0); /*X-overwrites-A*/}
677joinop(X) ::= JOIN_KW(A) nm(B) JOIN.
678 {X = sqlite3JoinType(pParse,&A,&B,0); /*X-overwrites-A*/}
drh5ad1a6c2002-07-01 12:27:09 +0000679joinop(X) ::= JOIN_KW(A) nm(B) nm(C) JOIN.
drhcf82f0d2016-02-17 04:33:10 +0000680 {X = sqlite3JoinType(pParse,&A,&B,&C);/*X-overwrites-A*/}
drh01f3f252002-05-24 16:14:15 +0000681
682%type on_opt {Expr*}
drh633e6d52008-07-28 19:34:53 +0000683%destructor on_opt {sqlite3ExprDelete(pParse->db, $$);}
drh1be266b2017-12-24 00:18:47 +0000684on_opt(N) ::= ON expr(E). {N = E;}
drh01f3f252002-05-24 16:14:15 +0000685on_opt(N) ::= . {N = 0;}
686
danielk197785574e32008-10-06 05:32:18 +0000687// Note that this block abuses the Token type just a little. If there is
688// no "INDEXED BY" clause, the returned token is empty (z==0 && n==0). If
689// there is an INDEXED BY clause, then the token is populated as per normal,
690// with z pointing to the token data and n containing the number of bytes
691// in the token.
692//
693// If there is a "NOT INDEXED" clause, then (z==0 && n==1), which is
danielk1977b1c685b2008-10-06 16:18:39 +0000694// normally illegal. The sqlite3SrcListIndexedBy() function
danielk197785574e32008-10-06 05:32:18 +0000695// recognizes and interprets this as a special case.
696//
697%type indexed_opt {Token}
698indexed_opt(A) ::= . {A.z=0; A.n=0;}
699indexed_opt(A) ::= INDEXED BY nm(X). {A = X;}
700indexed_opt(A) ::= NOT INDEXED. {A.z=0; A.n=1;}
701
drh01f3f252002-05-24 16:14:15 +0000702%type using_opt {IdList*}
drh633e6d52008-07-28 19:34:53 +0000703%destructor using_opt {sqlite3IdListDelete(pParse->db, $$);}
drh81eba732013-10-19 23:31:56 +0000704using_opt(U) ::= USING LP idlist(L) RP. {U = L;}
drh01f3f252002-05-24 16:14:15 +0000705using_opt(U) ::= . {U = 0;}
706
707
drh348784e2000-05-29 20:41:49 +0000708%type orderby_opt {ExprList*}
drh633e6d52008-07-28 19:34:53 +0000709%destructor orderby_opt {sqlite3ExprListDelete(pParse->db, $$);}
drh108aa002015-08-24 20:21:20 +0000710
711// the sortlist non-terminal stores a list of expression where each
712// expression is optionally followed by ASC or DESC to indicate the
713// sort order.
714//
drh348784e2000-05-29 20:41:49 +0000715%type sortlist {ExprList*}
drh633e6d52008-07-28 19:34:53 +0000716%destructor sortlist {sqlite3ExprListDelete(pParse->db, $$);}
drh348784e2000-05-29 20:41:49 +0000717
718orderby_opt(A) ::= . {A = 0;}
719orderby_opt(A) ::= ORDER BY sortlist(X). {A = X;}
drh4dd0d3f2016-02-17 01:18:33 +0000720sortlist(A) ::= sortlist(A) COMMA expr(Y) sortorder(Z). {
drh1be266b2017-12-24 00:18:47 +0000721 A = sqlite3ExprListAppend(pParse,A,Y);
drhbc622bc2015-08-24 15:39:42 +0000722 sqlite3ExprListSetSortOrder(A,Z);
drh9bb61fe2000-06-05 16:01:39 +0000723}
drh8395b7b2012-01-28 19:44:22 +0000724sortlist(A) ::= expr(Y) sortorder(Z). {
drh1be266b2017-12-24 00:18:47 +0000725 A = sqlite3ExprListAppend(pParse,0,Y); /*A-overwrites-Y*/
drhbc622bc2015-08-24 15:39:42 +0000726 sqlite3ExprListSetSortOrder(A,Z);
drh9bb61fe2000-06-05 16:01:39 +0000727}
drh348784e2000-05-29 20:41:49 +0000728
729%type sortorder {int}
730
drh8e2ca022002-06-17 17:07:19 +0000731sortorder(A) ::= ASC. {A = SQLITE_SO_ASC;}
732sortorder(A) ::= DESC. {A = SQLITE_SO_DESC;}
drhbc622bc2015-08-24 15:39:42 +0000733sortorder(A) ::= . {A = SQLITE_SO_UNDEFINED;}
drh348784e2000-05-29 20:41:49 +0000734
drh22827922000-06-06 17:27:05 +0000735%type groupby_opt {ExprList*}
drh633e6d52008-07-28 19:34:53 +0000736%destructor groupby_opt {sqlite3ExprListDelete(pParse->db, $$);}
drh6206d502000-06-19 19:09:08 +0000737groupby_opt(A) ::= . {A = 0;}
drh9245c242007-06-20 12:18:31 +0000738groupby_opt(A) ::= GROUP BY nexprlist(X). {A = X;}
drh22827922000-06-06 17:27:05 +0000739
740%type having_opt {Expr*}
drh633e6d52008-07-28 19:34:53 +0000741%destructor having_opt {sqlite3ExprDelete(pParse->db, $$);}
drh6206d502000-06-19 19:09:08 +0000742having_opt(A) ::= . {A = 0;}
drh1be266b2017-12-24 00:18:47 +0000743having_opt(A) ::= HAVING expr(X). {A = X;}
drh22827922000-06-06 17:27:05 +0000744
drh8c0833f2017-11-14 23:48:23 +0000745%type limit_opt {Expr*}
drh15926592007-04-06 15:02:13 +0000746
747// The destructor for limit_opt will never fire in the current grammar.
748// The limit_opt non-terminal only occurs at the end of a single production
749// rule for SELECT statements. As soon as the rule that create the
750// limit_opt non-terminal reduces, the SELECT statement rule will also
751// reduce. So there is never a limit_opt non-terminal on the stack
752// except as a transient. So there is never anything to destroy.
753//
drh8c0833f2017-11-14 23:48:23 +0000754//%destructor limit_opt {sqlite3ExprDelete(pParse->db, $$);}
755limit_opt(A) ::= . {A = 0;}
756limit_opt(A) ::= LIMIT expr(X).
drh1be266b2017-12-24 00:18:47 +0000757 {A = sqlite3PExpr(pParse,TK_LIMIT,X,0);}
danielk1977a2dc3b12005-02-05 12:48:48 +0000758limit_opt(A) ::= LIMIT expr(X) OFFSET expr(Y).
drh1be266b2017-12-24 00:18:47 +0000759 {A = sqlite3PExpr(pParse,TK_LIMIT,X,Y);}
danielk1977a2dc3b12005-02-05 12:48:48 +0000760limit_opt(A) ::= LIMIT expr(X) COMMA expr(Y).
drh1be266b2017-12-24 00:18:47 +0000761 {A = sqlite3PExpr(pParse,TK_LIMIT,Y,X);}
drh9bbca4c2001-11-06 04:00:18 +0000762
drh382c0242001-10-06 16:33:02 +0000763/////////////////////////// The DELETE statement /////////////////////////////
764//
shane273f6192008-10-10 04:34:16 +0000765%ifdef SQLITE_ENABLE_UPDATE_DELETE_LIMIT
dan4e9119d2014-01-13 15:12:23 +0000766cmd ::= with(C) DELETE FROM fullname(X) indexed_opt(I) where_opt(W)
drh931577f2008-10-10 14:27:16 +0000767 orderby_opt(O) limit_opt(L). {
danb290f112014-01-17 14:59:27 +0000768 sqlite3WithPush(pParse, C, 1);
danielk1977b1c685b2008-10-06 16:18:39 +0000769 sqlite3SrcListIndexedBy(pParse, X, &I);
drh8c0833f2017-11-14 23:48:23 +0000770 sqlite3DeleteFrom(pParse,X,W,O,L);
danielk1977b1c685b2008-10-06 16:18:39 +0000771}
shane4281bd42008-10-07 05:27:11 +0000772%endif
shane273f6192008-10-10 04:34:16 +0000773%ifndef SQLITE_ENABLE_UPDATE_DELETE_LIMIT
dan4e9119d2014-01-13 15:12:23 +0000774cmd ::= with(C) DELETE FROM fullname(X) indexed_opt(I) where_opt(W). {
danb290f112014-01-17 14:59:27 +0000775 sqlite3WithPush(pParse, C, 1);
shane4281bd42008-10-07 05:27:11 +0000776 sqlite3SrcListIndexedBy(pParse, X, &I);
drh8c0833f2017-11-14 23:48:23 +0000777 sqlite3DeleteFrom(pParse,X,W,0,0);
shane4281bd42008-10-07 05:27:11 +0000778}
779%endif
drh348784e2000-05-29 20:41:49 +0000780
781%type where_opt {Expr*}
drh633e6d52008-07-28 19:34:53 +0000782%destructor where_opt {sqlite3ExprDelete(pParse->db, $$);}
drh348784e2000-05-29 20:41:49 +0000783
784where_opt(A) ::= . {A = 0;}
drh1be266b2017-12-24 00:18:47 +0000785where_opt(A) ::= WHERE expr(X). {A = X;}
drh348784e2000-05-29 20:41:49 +0000786
drh382c0242001-10-06 16:33:02 +0000787////////////////////////// The UPDATE command ////////////////////////////////
788//
shane273f6192008-10-10 04:34:16 +0000789%ifdef SQLITE_ENABLE_UPDATE_DELETE_LIMIT
danbfe31e72014-01-15 14:17:31 +0000790cmd ::= with(C) UPDATE orconf(R) fullname(X) indexed_opt(I) SET setlist(Y)
drh8b471862014-01-11 13:22:17 +0000791 where_opt(W) orderby_opt(O) limit_opt(L). {
danb290f112014-01-17 14:59:27 +0000792 sqlite3WithPush(pParse, C, 1);
danielk1977b1c685b2008-10-06 16:18:39 +0000793 sqlite3SrcListIndexedBy(pParse, X, &I);
drhb1a6c3c2008-03-20 16:30:17 +0000794 sqlite3ExprListCheckLength(pParse,Y,"set list");
drh8c0833f2017-11-14 23:48:23 +0000795 sqlite3Update(pParse,X,Y,W,R,O,L);
danielk19777a15a4b2007-05-08 17:54:43 +0000796}
shane4281bd42008-10-07 05:27:11 +0000797%endif
shane273f6192008-10-10 04:34:16 +0000798%ifndef SQLITE_ENABLE_UPDATE_DELETE_LIMIT
dan4e9119d2014-01-13 15:12:23 +0000799cmd ::= with(C) UPDATE orconf(R) fullname(X) indexed_opt(I) SET setlist(Y)
drhe9240412012-12-18 13:12:03 +0000800 where_opt(W). {
danb290f112014-01-17 14:59:27 +0000801 sqlite3WithPush(pParse, C, 1);
shane4281bd42008-10-07 05:27:11 +0000802 sqlite3SrcListIndexedBy(pParse, X, &I);
803 sqlite3ExprListCheckLength(pParse,Y,"set list");
drh8c0833f2017-11-14 23:48:23 +0000804 sqlite3Update(pParse,X,Y,W,R,0,0);
shane4281bd42008-10-07 05:27:11 +0000805}
806%endif
drh348784e2000-05-29 20:41:49 +0000807
drhf8db1bc2005-04-22 02:38:37 +0000808%type setlist {ExprList*}
drh633e6d52008-07-28 19:34:53 +0000809%destructor setlist {sqlite3ExprListDelete(pParse->db, $$);}
drhf8db1bc2005-04-22 02:38:37 +0000810
drh4dd0d3f2016-02-17 01:18:33 +0000811setlist(A) ::= setlist(A) COMMA nm(X) EQ expr(Y). {
drh1be266b2017-12-24 00:18:47 +0000812 A = sqlite3ExprListAppend(pParse, A, Y);
drhb7916a72009-05-27 10:31:29 +0000813 sqlite3ExprListSetName(pParse, A, &X, 1);
814}
drha1251bc2016-08-20 00:51:37 +0000815setlist(A) ::= setlist(A) COMMA LP idlist(X) RP EQ expr(Y). {
drh1be266b2017-12-24 00:18:47 +0000816 A = sqlite3ExprListAppendVector(pParse, A, X, Y);
drha1251bc2016-08-20 00:51:37 +0000817}
drhb7916a72009-05-27 10:31:29 +0000818setlist(A) ::= nm(X) EQ expr(Y). {
drh1be266b2017-12-24 00:18:47 +0000819 A = sqlite3ExprListAppend(pParse, 0, Y);
drhb7916a72009-05-27 10:31:29 +0000820 sqlite3ExprListSetName(pParse, A, &X, 1);
821}
drha1251bc2016-08-20 00:51:37 +0000822setlist(A) ::= LP idlist(X) RP EQ expr(Y). {
drh1be266b2017-12-24 00:18:47 +0000823 A = sqlite3ExprListAppendVector(pParse, 0, X, Y);
drha1251bc2016-08-20 00:51:37 +0000824}
drh348784e2000-05-29 20:41:49 +0000825
drh382c0242001-10-06 16:33:02 +0000826////////////////////////// The INSERT command /////////////////////////////////
827//
drh8981b902015-08-24 17:42:49 +0000828cmd ::= with(W) insert_cmd(R) INTO fullname(X) idlist_opt(F) select(S). {
danb290f112014-01-17 14:59:27 +0000829 sqlite3WithPush(pParse, W, 1);
dan4e9119d2014-01-13 15:12:23 +0000830 sqlite3Insert(pParse, X, S, F, R);
831}
drh8981b902015-08-24 17:42:49 +0000832cmd ::= with(W) insert_cmd(R) INTO fullname(X) idlist_opt(F) DEFAULT VALUES.
dan4e9119d2014-01-13 15:12:23 +0000833{
danb290f112014-01-17 14:59:27 +0000834 sqlite3WithPush(pParse, W, 1);
dan4e9119d2014-01-13 15:12:23 +0000835 sqlite3Insert(pParse, X, 0, F, R);
836}
drh348784e2000-05-29 20:41:49 +0000837
drh3334d082015-11-10 13:45:21 +0000838%type insert_cmd {int}
drhfa86c412002-02-02 15:01:15 +0000839insert_cmd(A) ::= INSERT orconf(R). {A = R;}
840insert_cmd(A) ::= REPLACE. {A = OE_Replace;}
841
drh8981b902015-08-24 17:42:49 +0000842%type idlist_opt {IdList*}
843%destructor idlist_opt {sqlite3IdListDelete(pParse->db, $$);}
drh81eba732013-10-19 23:31:56 +0000844%type idlist {IdList*}
845%destructor idlist {sqlite3IdListDelete(pParse->db, $$);}
drh348784e2000-05-29 20:41:49 +0000846
drh8981b902015-08-24 17:42:49 +0000847idlist_opt(A) ::= . {A = 0;}
848idlist_opt(A) ::= LP idlist(X) RP. {A = X;}
drh4dd0d3f2016-02-17 01:18:33 +0000849idlist(A) ::= idlist(A) COMMA nm(Y).
850 {A = sqlite3IdListAppend(pParse->db,A,&Y);}
drh81eba732013-10-19 23:31:56 +0000851idlist(A) ::= nm(Y).
drhcf82f0d2016-02-17 04:33:10 +0000852 {A = sqlite3IdListAppend(pParse->db,0,&Y); /*A-overwrites-Y*/}
drh348784e2000-05-29 20:41:49 +0000853
drh382c0242001-10-06 16:33:02 +0000854/////////////////////////// Expression Processing /////////////////////////////
855//
drh348784e2000-05-29 20:41:49 +0000856
drh1be266b2017-12-24 00:18:47 +0000857%type expr {Expr*}
858%destructor expr {sqlite3ExprDelete(pParse->db, $$);}
859%type term {Expr*}
860%destructor term {sqlite3ExprDelete(pParse->db, $$);}
drhb7916a72009-05-27 10:31:29 +0000861
862%include {
drhb7916a72009-05-27 10:31:29 +0000863
864 /* Construct a new Expr object from a single identifier. Use the
865 ** new Expr to populate pOut. Set the span of pOut to be the identifier
866 ** that created the expression.
867 */
drh1be266b2017-12-24 00:18:47 +0000868 static Expr *tokenExpr(Parse *pParse, int op, Token t){
drh0cd874b2016-09-26 12:38:22 +0000869 Expr *p = sqlite3DbMallocRawNN(pParse->db, sizeof(Expr)+t.n+1);
870 if( p ){
871 memset(p, 0, sizeof(Expr));
872 p->op = (u8)op;
873 p->flags = EP_Leaf;
874 p->iAgg = -1;
875 p->u.zToken = (char*)&p[1];
876 memcpy(p->u.zToken, t.z, t.n);
877 p->u.zToken[t.n] = 0;
878 if( sqlite3Isquote(p->u.zToken[0]) ){
879 if( p->u.zToken[0]=='"' ) p->flags |= EP_DblQuoted;
880 sqlite3Dequote(p->u.zToken);
881 }
882#if SQLITE_MAX_EXPR_DEPTH>0
883 p->nHeight = 1;
884#endif
885 }
drh1be266b2017-12-24 00:18:47 +0000886 return p;
drhb7916a72009-05-27 10:31:29 +0000887 }
888}
drh348784e2000-05-29 20:41:49 +0000889
drh4dd0d3f2016-02-17 01:18:33 +0000890expr(A) ::= term(A).
drh1be266b2017-12-24 00:18:47 +0000891expr(A) ::= LP expr(X) RP. {A = X;}
892expr(A) ::= id(X). {A=tokenExpr(pParse,TK_ID,X); /*A-overwrites-X*/}
893expr(A) ::= JOIN_KW(X). {A=tokenExpr(pParse,TK_ID,X); /*A-overwrites-X*/}
drh5ad1a6c2002-07-01 12:27:09 +0000894expr(A) ::= nm(X) DOT nm(Y). {
drh410c3012016-09-24 17:42:43 +0000895 Expr *temp1 = sqlite3ExprAlloc(pParse->db, TK_ID, &X, 1);
896 Expr *temp2 = sqlite3ExprAlloc(pParse->db, TK_ID, &Y, 1);
drh1be266b2017-12-24 00:18:47 +0000897 A = sqlite3PExpr(pParse, TK_DOT, temp1, temp2);
drhe1b6a5b2000-07-29 13:06:59 +0000898}
drhd24cc422003-03-27 12:51:24 +0000899expr(A) ::= nm(X) DOT nm(Y) DOT nm(Z). {
drh410c3012016-09-24 17:42:43 +0000900 Expr *temp1 = sqlite3ExprAlloc(pParse->db, TK_ID, &X, 1);
901 Expr *temp2 = sqlite3ExprAlloc(pParse->db, TK_ID, &Y, 1);
902 Expr *temp3 = sqlite3ExprAlloc(pParse->db, TK_ID, &Z, 1);
drhabfd35e2016-12-06 22:47:23 +0000903 Expr *temp4 = sqlite3PExpr(pParse, TK_DOT, temp2, temp3);
drh1be266b2017-12-24 00:18:47 +0000904 A = sqlite3PExpr(pParse, TK_DOT, temp1, temp4);
drhd24cc422003-03-27 12:51:24 +0000905}
drh1be266b2017-12-24 00:18:47 +0000906term(A) ::= NULL|FLOAT|BLOB(X). {A=tokenExpr(pParse,@X,X); /*A-overwrites-X*/}
907term(A) ::= STRING(X). {A=tokenExpr(pParse,@X,X); /*A-overwrites-X*/}
drh0cd874b2016-09-26 12:38:22 +0000908term(A) ::= INTEGER(X). {
drh1be266b2017-12-24 00:18:47 +0000909 A = sqlite3ExprAlloc(pParse->db, TK_INTEGER, &X, 1);
drh0cd874b2016-09-26 12:38:22 +0000910}
drh7c972de2003-09-06 22:18:07 +0000911expr(A) ::= VARIABLE(X). {
drh8679fba2016-04-11 01:43:33 +0000912 if( !(X.z[0]=='#' && sqlite3Isdigit(X.z[1])) ){
drhde25a882016-10-03 15:28:24 +0000913 u32 n = X.n;
drh1be266b2017-12-24 00:18:47 +0000914 A = tokenExpr(pParse, TK_VARIABLE, X);
915 sqlite3ExprAssignVarNumber(pParse, A, n);
drh8f3b1372016-04-11 01:26:31 +0000916 }else{
drhf59b12f2014-01-11 03:54:05 +0000917 /* When doing a nested parse, one can include terms in an expression
918 ** that look like this: #1 #2 ... These terms refer to registers
919 ** in the virtual machine. #N is the N-th register. */
drh8f3b1372016-04-11 01:26:31 +0000920 Token t = X; /*A-overwrites-X*/
921 assert( t.n>=2 );
drhf59b12f2014-01-11 03:54:05 +0000922 if( pParse->nested==0 ){
drh43303de2016-02-17 12:34:03 +0000923 sqlite3ErrorMsg(pParse, "near \"%T\": syntax error", &t);
drh1be266b2017-12-24 00:18:47 +0000924 A = 0;
drhf59b12f2014-01-11 03:54:05 +0000925 }else{
drh1be266b2017-12-24 00:18:47 +0000926 A = sqlite3PExpr(pParse, TK_REGISTER, 0, 0);
927 if( A ) sqlite3GetInt32(&t.z[1], &A->iTable);
drhf59b12f2014-01-11 03:54:05 +0000928 }
drhf59b12f2014-01-11 03:54:05 +0000929 }
drh7c972de2003-09-06 22:18:07 +0000930}
drh4dd0d3f2016-02-17 01:18:33 +0000931expr(A) ::= expr(A) COLLATE ids(C). {
drh1be266b2017-12-24 00:18:47 +0000932 A = sqlite3ExprAddCollateToken(pParse, A, &C, 1);
drh8b4c40d2007-02-01 23:02:45 +0000933}
drh487e2622005-06-25 18:42:14 +0000934%ifndef SQLITE_OMIT_CAST
drh1be266b2017-12-24 00:18:47 +0000935expr(A) ::= CAST LP expr(E) AS typetoken(T) RP. {
936 A = sqlite3ExprAlloc(pParse->db, TK_CAST, &T, 1);
937 sqlite3ExprAttachSubtrees(pParse->db, A, E, 0);
drh487e2622005-06-25 18:42:14 +0000938}
drh154d4b22006-09-21 11:02:16 +0000939%endif SQLITE_OMIT_CAST
drh1be266b2017-12-24 00:18:47 +0000940expr(A) ::= id(X) LP distinct(D) exprlist(Y) RP. {
drh994704d2009-06-12 12:04:16 +0000941 if( Y && Y->nExpr>pParse->db->aLimit[SQLITE_LIMIT_FUNCTION_ARG] ){
drhe5c941b2007-05-08 13:58:26 +0000942 sqlite3ErrorMsg(pParse, "too many arguments on function %T", &X);
drh4e05c832007-05-11 01:44:50 +0000943 }
drh1be266b2017-12-24 00:18:47 +0000944 A = sqlite3ExprFunction(pParse, Y, &X);
945 if( D==SF_Distinct && A ){
946 A->flags |= EP_Distinct;
drhfd357972005-09-09 01:33:19 +0000947 }
drhe1b6a5b2000-07-29 13:06:59 +0000948}
drh1be266b2017-12-24 00:18:47 +0000949expr(A) ::= id(X) LP STAR RP. {
950 A = sqlite3ExprFunction(pParse, 0, &X);
drhe1b6a5b2000-07-29 13:06:59 +0000951}
drhb71090f2005-05-23 17:26:51 +0000952term(A) ::= CTIME_KW(OP). {
drh1be266b2017-12-24 00:18:47 +0000953 A = sqlite3ExprFunction(pParse, 0, &OP);
drhb7916a72009-05-27 10:31:29 +0000954}
955
drh1be266b2017-12-24 00:18:47 +0000956expr(A) ::= LP nexprlist(X) COMMA expr(Y) RP. {
957 ExprList *pList = sqlite3ExprListAppend(pParse, X, Y);
958 A = sqlite3PExpr(pParse, TK_VECTOR, 0, 0);
959 if( A ){
960 A->x.pList = pList;
drh8bd0d582016-08-20 18:06:14 +0000961 }else{
962 sqlite3ExprListDelete(pParse->db, pList);
dan71c57db2016-07-09 20:23:55 +0000963 }
964}
965
drh1be266b2017-12-24 00:18:47 +0000966expr(A) ::= expr(A) AND(OP) expr(Y). {A=sqlite3PExpr(pParse,@OP,A,Y);}
967expr(A) ::= expr(A) OR(OP) expr(Y). {A=sqlite3PExpr(pParse,@OP,A,Y);}
drh4dd0d3f2016-02-17 01:18:33 +0000968expr(A) ::= expr(A) LT|GT|GE|LE(OP) expr(Y).
drh1be266b2017-12-24 00:18:47 +0000969 {A=sqlite3PExpr(pParse,@OP,A,Y);}
970expr(A) ::= expr(A) EQ|NE(OP) expr(Y). {A=sqlite3PExpr(pParse,@OP,A,Y);}
drh4dd0d3f2016-02-17 01:18:33 +0000971expr(A) ::= expr(A) BITAND|BITOR|LSHIFT|RSHIFT(OP) expr(Y).
drh1be266b2017-12-24 00:18:47 +0000972 {A=sqlite3PExpr(pParse,@OP,A,Y);}
drh4dd0d3f2016-02-17 01:18:33 +0000973expr(A) ::= expr(A) PLUS|MINUS(OP) expr(Y).
drh1be266b2017-12-24 00:18:47 +0000974 {A=sqlite3PExpr(pParse,@OP,A,Y);}
drh4dd0d3f2016-02-17 01:18:33 +0000975expr(A) ::= expr(A) STAR|SLASH|REM(OP) expr(Y).
drh1be266b2017-12-24 00:18:47 +0000976 {A=sqlite3PExpr(pParse,@OP,A,Y);}
977expr(A) ::= expr(A) CONCAT(OP) expr(Y). {A=sqlite3PExpr(pParse,@OP,A,Y);}
drh410c3012016-09-24 17:42:43 +0000978%type likeop {Token}
drh7e84b372017-02-20 14:30:17 +0000979likeop(A) ::= LIKE_KW|MATCH(A).
drh410c3012016-09-24 17:42:43 +0000980likeop(A) ::= NOT LIKE_KW|MATCH(X). {A=X; A.n|=0x80000000; /*A-overwrite-X*/}
drh4dd0d3f2016-02-17 01:18:33 +0000981expr(A) ::= expr(A) likeop(OP) expr(Y). [LIKE_KW] {
drh8aa34ae2006-03-13 12:54:09 +0000982 ExprList *pList;
drh410c3012016-09-24 17:42:43 +0000983 int bNot = OP.n & 0x80000000;
984 OP.n &= 0x7fffffff;
drh1be266b2017-12-24 00:18:47 +0000985 pList = sqlite3ExprListAppend(pParse,0, Y);
986 pList = sqlite3ExprListAppend(pParse,pList, A);
987 A = sqlite3ExprFunction(pParse, pList, &OP);
988 if( bNot ) A = sqlite3PExpr(pParse, TK_NOT, A, 0);
989 if( A ) A->flags |= EP_InfixFunc;
drh0ac65892002-04-20 14:24:41 +0000990}
drh4dd0d3f2016-02-17 01:18:33 +0000991expr(A) ::= expr(A) likeop(OP) expr(Y) ESCAPE expr(E). [LIKE_KW] {
drh1dca1452010-07-19 02:30:33 +0000992 ExprList *pList;
drh410c3012016-09-24 17:42:43 +0000993 int bNot = OP.n & 0x80000000;
994 OP.n &= 0x7fffffff;
drh1be266b2017-12-24 00:18:47 +0000995 pList = sqlite3ExprListAppend(pParse,0, Y);
996 pList = sqlite3ExprListAppend(pParse,pList, A);
997 pList = sqlite3ExprListAppend(pParse,pList, E);
998 A = sqlite3ExprFunction(pParse, pList, &OP);
999 if( bNot ) A = sqlite3PExpr(pParse, TK_NOT, A, 0);
1000 if( A ) A->flags |= EP_InfixFunc;
drh1dca1452010-07-19 02:30:33 +00001001}
danielk19777c6303c2004-11-17 16:41:29 +00001002
drh1be266b2017-12-24 00:18:47 +00001003expr(A) ::= expr(A) ISNULL|NOTNULL(E). {A = sqlite3PExpr(pParse,@E,A,0);}
1004expr(A) ::= expr(A) NOT NULL. {A = sqlite3PExpr(pParse,TK_NOTNULL,A,0);}
drh6a2fe092009-09-23 02:29:36 +00001005
drh6a517412009-11-12 03:46:34 +00001006%include {
1007 /* A routine to convert a binary TK_IS or TK_ISNOT expression into a
1008 ** unary TK_ISNULL or TK_NOTNULL expression. */
1009 static void binaryToUnaryIfNull(Parse *pParse, Expr *pY, Expr *pA, int op){
1010 sqlite3 *db = pParse->db;
dan895c00e2016-01-28 18:22:46 +00001011 if( pA && pY && pY->op==TK_NULL ){
shaneh5e17e8b2009-12-03 04:40:47 +00001012 pA->op = (u8)op;
drh6a517412009-11-12 03:46:34 +00001013 sqlite3ExprDelete(db, pA->pRight);
1014 pA->pRight = 0;
1015 }
1016 }
1017}
1018
drh6a2fe092009-09-23 02:29:36 +00001019// expr1 IS expr2
1020// expr1 IS NOT expr2
1021//
1022// If expr2 is NULL then code as TK_ISNULL or TK_NOTNULL. If expr2
1023// is any other expression, code as TK_IS or TK_ISNOT.
1024//
drh4dd0d3f2016-02-17 01:18:33 +00001025expr(A) ::= expr(A) IS expr(Y). {
drh1be266b2017-12-24 00:18:47 +00001026 A = sqlite3PExpr(pParse,TK_IS,A,Y);
1027 binaryToUnaryIfNull(pParse, Y, A, TK_ISNULL);
drh6a2fe092009-09-23 02:29:36 +00001028}
drh4dd0d3f2016-02-17 01:18:33 +00001029expr(A) ::= expr(A) IS NOT expr(Y). {
drh1be266b2017-12-24 00:18:47 +00001030 A = sqlite3PExpr(pParse,TK_ISNOT,A,Y);
1031 binaryToUnaryIfNull(pParse, Y, A, TK_NOTNULL);
drh6a2fe092009-09-23 02:29:36 +00001032}
drhb7916a72009-05-27 10:31:29 +00001033
drh43303de2016-02-17 12:34:03 +00001034expr(A) ::= NOT(B) expr(X).
drh1be266b2017-12-24 00:18:47 +00001035 {A = sqlite3PExpr(pParse, @B, X, 0);/*A-overwrites-B*/}
drh43303de2016-02-17 12:34:03 +00001036expr(A) ::= BITNOT(B) expr(X).
drh1be266b2017-12-24 00:18:47 +00001037 {A = sqlite3PExpr(pParse, @B, X, 0);/*A-overwrites-B*/}
1038expr(A) ::= MINUS expr(X). [BITNOT]
1039 {A = sqlite3PExpr(pParse, TK_UMINUS, X, 0);}
1040expr(A) ::= PLUS expr(X). [BITNOT]
1041 {A = sqlite3PExpr(pParse, TK_UPLUS, X, 0);}
drhb7916a72009-05-27 10:31:29 +00001042
drh2e3a1f12004-10-06 14:39:28 +00001043%type between_op {int}
1044between_op(A) ::= BETWEEN. {A = 0;}
1045between_op(A) ::= NOT BETWEEN. {A = 1;}
drh4dd0d3f2016-02-17 01:18:33 +00001046expr(A) ::= expr(A) between_op(N) expr(X) AND expr(Y). [BETWEEN] {
drh1be266b2017-12-24 00:18:47 +00001047 ExprList *pList = sqlite3ExprListAppend(pParse,0, X);
1048 pList = sqlite3ExprListAppend(pParse,pList, Y);
1049 A = sqlite3PExpr(pParse, TK_BETWEEN, A, 0);
1050 if( A ){
1051 A->x.pList = pList;
drh53f733c2005-09-16 02:38:09 +00001052 }else{
drh633e6d52008-07-28 19:34:53 +00001053 sqlite3ExprListDelete(pParse->db, pList);
drh53f733c2005-09-16 02:38:09 +00001054 }
drh1be266b2017-12-24 00:18:47 +00001055 if( N ) A = sqlite3PExpr(pParse, TK_NOT, A, 0);
drhfef52082000-06-06 01:50:43 +00001056}
drh51522cd2005-01-20 13:36:19 +00001057%ifndef SQLITE_OMIT_SUBQUERY
danielk19773e8c37e2005-01-21 03:12:14 +00001058 %type in_op {int}
1059 in_op(A) ::= IN. {A = 0;}
1060 in_op(A) ::= NOT IN. {A = 1;}
drh1be266b2017-12-24 00:18:47 +00001061 expr(A) ::= expr(A) in_op(N) LP exprlist(Y) RP. [IN] {
drh094430e2010-07-14 18:24:06 +00001062 if( Y==0 ){
dan473c1bf2010-07-15 11:14:21 +00001063 /* Expressions of the form
1064 **
1065 ** expr1 IN ()
1066 ** expr1 NOT IN ()
1067 **
1068 ** simplify to constants 0 (false) and 1 (true), respectively,
1069 ** regardless of the value of expr1.
1070 */
drh1be266b2017-12-24 00:18:47 +00001071 sqlite3ExprDelete(pParse->db, A);
1072 A = sqlite3ExprAlloc(pParse->db, TK_INTEGER,&sqlite3IntTokens[N],1);
drh2b59b3a2014-03-20 13:26:47 +00001073 }else if( Y->nExpr==1 ){
1074 /* Expressions of the form:
1075 **
1076 ** expr1 IN (?1)
1077 ** expr1 NOT IN (?2)
1078 **
drhfbb24d12014-03-20 17:03:30 +00001079 ** with exactly one value on the RHS can be simplified to something
1080 ** like this:
drh2b59b3a2014-03-20 13:26:47 +00001081 **
drhfbb24d12014-03-20 17:03:30 +00001082 ** expr1 == ?1
1083 ** expr1 <> ?2
1084 **
1085 ** But, the RHS of the == or <> is marked with the EP_Generic flag
1086 ** so that it may not contribute to the computation of comparison
1087 ** affinity or the collating sequence to use for comparison. Otherwise,
1088 ** the semantics would be subtly different from IN or NOT IN.
drh2b59b3a2014-03-20 13:26:47 +00001089 */
drhfbb24d12014-03-20 17:03:30 +00001090 Expr *pRHS = Y->a[0].pExpr;
drh2b59b3a2014-03-20 13:26:47 +00001091 Y->a[0].pExpr = 0;
1092 sqlite3ExprListDelete(pParse->db, Y);
drh5b1420e2014-03-20 19:04:56 +00001093 /* pRHS cannot be NULL because a malloc error would have been detected
1094 ** before now and control would have never reached this point */
1095 if( ALWAYS(pRHS) ){
drhfbb24d12014-03-20 17:03:30 +00001096 pRHS->flags &= ~EP_Collate;
1097 pRHS->flags |= EP_Generic;
1098 }
drh1be266b2017-12-24 00:18:47 +00001099 A = sqlite3PExpr(pParse, N ? TK_NE : TK_EQ, A, pRHS);
danielk1977d5d56522005-03-16 12:15:20 +00001100 }else{
drh1be266b2017-12-24 00:18:47 +00001101 A = sqlite3PExpr(pParse, TK_IN, A, 0);
1102 if( A ){
1103 A->x.pList = Y;
1104 sqlite3ExprSetHeightAndFlags(pParse, A);
drh094430e2010-07-14 18:24:06 +00001105 }else{
1106 sqlite3ExprListDelete(pParse->db, Y);
1107 }
drh1be266b2017-12-24 00:18:47 +00001108 if( N ) A = sqlite3PExpr(pParse, TK_NOT, A, 0);
danielk1977d5d56522005-03-16 12:15:20 +00001109 }
danielk19773e8c37e2005-01-21 03:12:14 +00001110 }
drh1be266b2017-12-24 00:18:47 +00001111 expr(A) ::= LP select(X) RP. {
1112 A = sqlite3PExpr(pParse, TK_SELECT, 0, 0);
1113 sqlite3PExprAddSelect(pParse, A, X);
drh51522cd2005-01-20 13:36:19 +00001114 }
drh1be266b2017-12-24 00:18:47 +00001115 expr(A) ::= expr(A) in_op(N) LP select(Y) RP. [IN] {
1116 A = sqlite3PExpr(pParse, TK_IN, A, 0);
1117 sqlite3PExprAddSelect(pParse, A, Y);
1118 if( N ) A = sqlite3PExpr(pParse, TK_NOT, A, 0);
drh51522cd2005-01-20 13:36:19 +00001119 }
drh5fbab882016-07-02 12:08:14 +00001120 expr(A) ::= expr(A) in_op(N) nm(Y) dbnm(Z) paren_exprlist(E). [IN] {
drh17435752007-08-16 04:30:38 +00001121 SrcList *pSrc = sqlite3SrcListAppend(pParse->db, 0,&Y,&Z);
drh8c0833f2017-11-14 23:48:23 +00001122 Select *pSelect = sqlite3SelectNew(pParse, 0,pSrc,0,0,0,0,0,0);
drh9de47572016-07-02 12:33:21 +00001123 if( E ) sqlite3SrcListFuncArgs(pParse, pSelect ? pSrc : 0, E);
drh1be266b2017-12-24 00:18:47 +00001124 A = sqlite3PExpr(pParse, TK_IN, A, 0);
1125 sqlite3PExprAddSelect(pParse, A, pSelect);
1126 if( N ) A = sqlite3PExpr(pParse, TK_NOT, A, 0);
drh51522cd2005-01-20 13:36:19 +00001127 }
drh1be266b2017-12-24 00:18:47 +00001128 expr(A) ::= EXISTS LP select(Y) RP. {
drh43303de2016-02-17 12:34:03 +00001129 Expr *p;
drh1be266b2017-12-24 00:18:47 +00001130 p = A = sqlite3PExpr(pParse, TK_EXISTS, 0, 0);
drh08de4f72016-04-11 01:06:47 +00001131 sqlite3PExprAddSelect(pParse, p, Y);
drh51522cd2005-01-20 13:36:19 +00001132 }
drh154d4b22006-09-21 11:02:16 +00001133%endif SQLITE_OMIT_SUBQUERY
drhfef52082000-06-06 01:50:43 +00001134
drh17a7f8d2002-03-24 13:13:27 +00001135/* CASE expressions */
drh1be266b2017-12-24 00:18:47 +00001136expr(A) ::= CASE case_operand(X) case_exprlist(Y) case_else(Z) END. {
1137 A = sqlite3PExpr(pParse, TK_CASE, X, 0);
1138 if( A ){
1139 A->x.pList = Z ? sqlite3ExprListAppend(pParse,Y,Z) : Y;
1140 sqlite3ExprSetHeightAndFlags(pParse, A);
drh53f733c2005-09-16 02:38:09 +00001141 }else{
drh633e6d52008-07-28 19:34:53 +00001142 sqlite3ExprListDelete(pParse->db, Y);
drhc5cd1242013-09-12 16:50:49 +00001143 sqlite3ExprDelete(pParse->db, Z);
drh53f733c2005-09-16 02:38:09 +00001144 }
drh17a7f8d2002-03-24 13:13:27 +00001145}
1146%type case_exprlist {ExprList*}
drh633e6d52008-07-28 19:34:53 +00001147%destructor case_exprlist {sqlite3ExprListDelete(pParse->db, $$);}
drh4dd0d3f2016-02-17 01:18:33 +00001148case_exprlist(A) ::= case_exprlist(A) WHEN expr(Y) THEN expr(Z). {
drh1be266b2017-12-24 00:18:47 +00001149 A = sqlite3ExprListAppend(pParse,A, Y);
1150 A = sqlite3ExprListAppend(pParse,A, Z);
drh17a7f8d2002-03-24 13:13:27 +00001151}
1152case_exprlist(A) ::= WHEN expr(Y) THEN expr(Z). {
drh1be266b2017-12-24 00:18:47 +00001153 A = sqlite3ExprListAppend(pParse,0, Y);
1154 A = sqlite3ExprListAppend(pParse,A, Z);
drh17a7f8d2002-03-24 13:13:27 +00001155}
1156%type case_else {Expr*}
drh633e6d52008-07-28 19:34:53 +00001157%destructor case_else {sqlite3ExprDelete(pParse->db, $$);}
drh1be266b2017-12-24 00:18:47 +00001158case_else(A) ::= ELSE expr(X). {A = X;}
drh17a7f8d2002-03-24 13:13:27 +00001159case_else(A) ::= . {A = 0;}
1160%type case_operand {Expr*}
drh633e6d52008-07-28 19:34:53 +00001161%destructor case_operand {sqlite3ExprDelete(pParse->db, $$);}
drh1be266b2017-12-24 00:18:47 +00001162case_operand(A) ::= expr(X). {A = X; /*A-overwrites-X*/}
drh17a7f8d2002-03-24 13:13:27 +00001163case_operand(A) ::= . {A = 0;}
drh348784e2000-05-29 20:41:49 +00001164
1165%type exprlist {ExprList*}
drh633e6d52008-07-28 19:34:53 +00001166%destructor exprlist {sqlite3ExprListDelete(pParse->db, $$);}
drh9245c242007-06-20 12:18:31 +00001167%type nexprlist {ExprList*}
drh633e6d52008-07-28 19:34:53 +00001168%destructor nexprlist {sqlite3ExprListDelete(pParse->db, $$);}
drh348784e2000-05-29 20:41:49 +00001169
drh4dd0d3f2016-02-17 01:18:33 +00001170exprlist(A) ::= nexprlist(A).
drh9245c242007-06-20 12:18:31 +00001171exprlist(A) ::= . {A = 0;}
drh4dd0d3f2016-02-17 01:18:33 +00001172nexprlist(A) ::= nexprlist(A) COMMA expr(Y).
drh1be266b2017-12-24 00:18:47 +00001173 {A = sqlite3ExprListAppend(pParse,A,Y);}
drh17435752007-08-16 04:30:38 +00001174nexprlist(A) ::= expr(Y).
drh1be266b2017-12-24 00:18:47 +00001175 {A = sqlite3ExprListAppend(pParse,0,Y); /*A-overwrites-Y*/}
drh9245c242007-06-20 12:18:31 +00001176
drha5224732016-07-25 14:40:43 +00001177%ifndef SQLITE_OMIT_SUBQUERY
drh5fbab882016-07-02 12:08:14 +00001178/* A paren_exprlist is an optional expression list contained inside
1179** of parenthesis */
1180%type paren_exprlist {ExprList*}
1181%destructor paren_exprlist {sqlite3ExprListDelete(pParse->db, $$);}
1182paren_exprlist(A) ::= . {A = 0;}
1183paren_exprlist(A) ::= LP exprlist(X) RP. {A = X;}
drha5224732016-07-25 14:40:43 +00001184%endif SQLITE_OMIT_SUBQUERY
drh5fbab882016-07-02 12:08:14 +00001185
drhcce7d172000-05-31 15:34:51 +00001186
drh382c0242001-10-06 16:33:02 +00001187///////////////////////////// The CREATE INDEX command ///////////////////////
1188//
drhd9da78a2009-03-24 15:08:09 +00001189cmd ::= createkw(S) uniqueflag(U) INDEX ifnotexists(NE) nm(X) dbnm(D)
drh108aa002015-08-24 20:21:20 +00001190 ON nm(Y) LP sortlist(Z) RP where_opt(W). {
drh17435752007-08-16 04:30:38 +00001191 sqlite3CreateIndex(pParse, &X, &D,
1192 sqlite3SrcListAppend(pParse->db,0,&Y,0), Z, U,
drh62340f82016-05-31 21:18:15 +00001193 &S, W, SQLITE_SO_ASC, NE, SQLITE_IDXTYPE_APPDEF);
drh9cfcf5d2002-01-29 18:41:24 +00001194}
drh717e6402001-09-27 03:22:32 +00001195
1196%type uniqueflag {int}
drh74ad7fe2004-10-07 03:06:28 +00001197uniqueflag(A) ::= UNIQUE. {A = OE_Abort;}
1198uniqueflag(A) ::= . {A = OE_None;}
drh348784e2000-05-29 20:41:49 +00001199
drh348784e2000-05-29 20:41:49 +00001200
drh108aa002015-08-24 20:21:20 +00001201// The eidlist non-terminal (Expression Id List) generates an ExprList
1202// from a list of identifiers. The identifier names are in ExprList.a[].zName.
1203// This list is stored in an ExprList rather than an IdList so that it
1204// can be easily sent to sqlite3ColumnsExprList().
1205//
1206// eidlist is grouped with CREATE INDEX because it used to be the non-terminal
1207// used for the arguments to an index. That is just an historical accident.
1208//
1209// IMPORTANT COMPATIBILITY NOTE: Some prior versions of SQLite accepted
1210// COLLATE clauses and ASC or DESC keywords on ID lists in inappropriate
1211// places - places that might have been stored in the sqlite_master schema.
1212// Those extra features were ignored. But because they might be in some
1213// (busted) old databases, we need to continue parsing them when loading
1214// historical schemas.
1215//
1216%type eidlist {ExprList*}
1217%destructor eidlist {sqlite3ExprListDelete(pParse->db, $$);}
1218%type eidlist_opt {ExprList*}
1219%destructor eidlist_opt {sqlite3ExprListDelete(pParse->db, $$);}
1220
1221%include {
1222 /* Add a single new term to an ExprList that is used to store a
1223 ** list of identifiers. Report an error if the ID list contains
1224 ** a COLLATE clause or an ASC or DESC keyword, except ignore the
1225 ** error while parsing a legacy schema.
1226 */
1227 static ExprList *parserAddExprIdListTerm(
1228 Parse *pParse,
1229 ExprList *pPrior,
1230 Token *pIdToken,
1231 int hasCollate,
1232 int sortOrder
1233 ){
1234 ExprList *p = sqlite3ExprListAppend(pParse, pPrior, 0);
1235 if( (hasCollate || sortOrder!=SQLITE_SO_UNDEFINED)
1236 && pParse->db->init.busy==0
1237 ){
1238 sqlite3ErrorMsg(pParse, "syntax error after column name \"%.*s\"",
1239 pIdToken->n, pIdToken->z);
1240 }
1241 sqlite3ExprListSetName(pParse, p, pIdToken, 1);
1242 return p;
1243 }
1244} // end %include
1245
1246eidlist_opt(A) ::= . {A = 0;}
1247eidlist_opt(A) ::= LP eidlist(X) RP. {A = X;}
drh4dd0d3f2016-02-17 01:18:33 +00001248eidlist(A) ::= eidlist(A) COMMA nm(Y) collate(C) sortorder(Z). {
1249 A = parserAddExprIdListTerm(pParse, A, &Y, C, Z);
danielk19770202b292004-06-09 09:55:16 +00001250}
drh108aa002015-08-24 20:21:20 +00001251eidlist(A) ::= nm(Y) collate(C) sortorder(Z). {
drhcf82f0d2016-02-17 04:33:10 +00001252 A = parserAddExprIdListTerm(pParse, 0, &Y, C, Z); /*A-overwrites-Y*/
danielk19770202b292004-06-09 09:55:16 +00001253}
danielk19770202b292004-06-09 09:55:16 +00001254
drh108aa002015-08-24 20:21:20 +00001255%type collate {int}
1256collate(C) ::= . {C = 0;}
1257collate(C) ::= COLLATE ids. {C = 1;}
drha34001c2007-02-02 12:44:37 +00001258
drh348784e2000-05-29 20:41:49 +00001259
drh8aff1012001-12-22 14:49:24 +00001260///////////////////////////// The DROP INDEX command /////////////////////////
drh382c0242001-10-06 16:33:02 +00001261//
drh4d91a702006-01-04 15:54:36 +00001262cmd ::= DROP INDEX ifexists(E) fullname(X). {sqlite3DropIndex(pParse, X, E);}
drh982cef72000-05-30 16:27:03 +00001263
drh382c0242001-10-06 16:33:02 +00001264///////////////////////////// The VACUUM command /////////////////////////////
1265//
drh154d4b22006-09-21 11:02:16 +00001266%ifndef SQLITE_OMIT_VACUUM
drhfdbcdee2007-03-27 14:44:50 +00001267%ifndef SQLITE_OMIT_ATTACH
drh9ef5e772016-08-19 14:20:56 +00001268cmd ::= VACUUM. {sqlite3Vacuum(pParse,0);}
1269cmd ::= VACUUM nm(X). {sqlite3Vacuum(pParse,&X);}
drhfdbcdee2007-03-27 14:44:50 +00001270%endif SQLITE_OMIT_ATTACH
drh154d4b22006-09-21 11:02:16 +00001271%endif SQLITE_OMIT_VACUUM
drhf57b14a2001-09-14 18:54:08 +00001272
drh382c0242001-10-06 16:33:02 +00001273///////////////////////////// The PRAGMA command /////////////////////////////
1274//
drh13d70422004-11-13 15:59:14 +00001275%ifndef SQLITE_OMIT_PRAGMA
drhada2ee02009-04-03 01:43:57 +00001276cmd ::= PRAGMA nm(X) dbnm(Z). {sqlite3Pragma(pParse,&X,&Z,0,0);}
1277cmd ::= PRAGMA nm(X) dbnm(Z) EQ nmnum(Y). {sqlite3Pragma(pParse,&X,&Z,&Y,0);}
drha3eb4b42007-01-27 02:38:29 +00001278cmd ::= PRAGMA nm(X) dbnm(Z) LP nmnum(Y) RP. {sqlite3Pragma(pParse,&X,&Z,&Y,0);}
drhada2ee02009-04-03 01:43:57 +00001279cmd ::= PRAGMA nm(X) dbnm(Z) EQ minus_num(Y).
1280 {sqlite3Pragma(pParse,&X,&Z,&Y,1);}
1281cmd ::= PRAGMA nm(X) dbnm(Z) LP minus_num(Y) RP.
1282 {sqlite3Pragma(pParse,&X,&Z,&Y,1);}
1283
drhcf82f0d2016-02-17 04:33:10 +00001284nmnum(A) ::= plus_num(A).
1285nmnum(A) ::= nm(A).
1286nmnum(A) ::= ON(A).
1287nmnum(A) ::= DELETE(A).
1288nmnum(A) ::= DEFAULT(A).
drh154d4b22006-09-21 11:02:16 +00001289%endif SQLITE_OMIT_PRAGMA
drhf59b12f2014-01-11 03:54:05 +00001290%token_class number INTEGER|FLOAT.
drh8395b7b2012-01-28 19:44:22 +00001291plus_num(A) ::= PLUS number(X). {A = X;}
drhcf82f0d2016-02-17 04:33:10 +00001292plus_num(A) ::= number(A).
drhf57b14a2001-09-14 18:54:08 +00001293minus_num(A) ::= MINUS number(X). {A = X;}
danielk1977c3f9bad2002-05-15 08:30:12 +00001294//////////////////////////// The CREATE TRIGGER command /////////////////////
drhf0f258b2003-04-21 18:48:45 +00001295
drhb7f91642004-10-31 02:22:47 +00001296%ifndef SQLITE_OMIT_TRIGGER
1297
drhd9da78a2009-03-24 15:08:09 +00001298cmd ::= createkw trigger_decl(A) BEGIN trigger_cmd_list(S) END(Z). {
drh4b59ab52002-08-24 18:24:51 +00001299 Token all;
1300 all.z = A.z;
drhb27b7f52008-12-10 18:03:45 +00001301 all.n = (int)(Z.z - A.z) + Z.n;
danielk19774adee202004-05-08 08:23:19 +00001302 sqlite3FinishTrigger(pParse, S, &all);
drhf0f258b2003-04-21 18:48:45 +00001303}
1304
drhfdd48a72006-09-11 23:45:48 +00001305trigger_decl(A) ::= temp(T) TRIGGER ifnotexists(NOERR) nm(B) dbnm(Z)
1306 trigger_time(C) trigger_event(D)
drh60218d22007-04-06 11:26:00 +00001307 ON fullname(E) foreach_clause when_clause(G). {
1308 sqlite3BeginTrigger(pParse, &B, &Z, C, D.a, D.b, E, G, T, NOERR);
drhcf82f0d2016-02-17 04:33:10 +00001309 A = (Z.n==0?B:Z); /*A-overwrites-T*/
danielk1977c3f9bad2002-05-15 08:30:12 +00001310}
1311
drhc4dd3fd2008-01-22 01:48:05 +00001312%type trigger_time {int}
drh6559e2c2017-06-28 14:26:37 +00001313trigger_time(A) ::= BEFORE|AFTER(X). { A = @X; /*A-overwrites-X*/ }
danielk1977c3f9bad2002-05-15 08:30:12 +00001314trigger_time(A) ::= INSTEAD OF. { A = TK_INSTEAD;}
1315trigger_time(A) ::= . { A = TK_BEFORE; }
1316
drhad3cab52002-05-24 02:04:32 +00001317%type trigger_event {struct TrigEvent}
drh633e6d52008-07-28 19:34:53 +00001318%destructor trigger_event {sqlite3IdListDelete(pParse->db, $$.b);}
drhcf82f0d2016-02-17 04:33:10 +00001319trigger_event(A) ::= DELETE|INSERT(X). {A.a = @X; /*A-overwrites-X*/ A.b = 0;}
1320trigger_event(A) ::= UPDATE(X). {A.a = @X; /*A-overwrites-X*/ A.b = 0;}
1321trigger_event(A) ::= UPDATE OF idlist(X).{A.a = TK_UPDATE; A.b = X;}
danielk1977c3f9bad2002-05-15 08:30:12 +00001322
drh60218d22007-04-06 11:26:00 +00001323foreach_clause ::= .
1324foreach_clause ::= FOR EACH ROW.
danielk1977c3f9bad2002-05-15 08:30:12 +00001325
drh0bb132b2004-07-20 14:06:51 +00001326%type when_clause {Expr*}
drh633e6d52008-07-28 19:34:53 +00001327%destructor when_clause {sqlite3ExprDelete(pParse->db, $$);}
danielk1977c3f9bad2002-05-15 08:30:12 +00001328when_clause(A) ::= . { A = 0; }
drh1be266b2017-12-24 00:18:47 +00001329when_clause(A) ::= WHEN expr(X). { A = X; }
danielk1977c3f9bad2002-05-15 08:30:12 +00001330
drh0bb132b2004-07-20 14:06:51 +00001331%type trigger_cmd_list {TriggerStep*}
drh633e6d52008-07-28 19:34:53 +00001332%destructor trigger_cmd_list {sqlite3DeleteTriggerStep(pParse->db, $$);}
drh4dd0d3f2016-02-17 01:18:33 +00001333trigger_cmd_list(A) ::= trigger_cmd_list(A) trigger_cmd(X) SEMI. {
1334 assert( A!=0 );
1335 A->pLast->pNext = X;
1336 A->pLast = X;
drha69d9162003-04-17 22:57:53 +00001337}
drh4dd0d3f2016-02-17 01:18:33 +00001338trigger_cmd_list(A) ::= trigger_cmd(A) SEMI. {
1339 assert( A!=0 );
1340 A->pLast = A;
drh81238962008-08-11 14:26:35 +00001341}
danielk1977c3f9bad2002-05-15 08:30:12 +00001342
drhb1819a02009-07-03 15:37:27 +00001343// Disallow qualified table names on INSERT, UPDATE, and DELETE statements
1344// within a trigger. The table to INSERT, UPDATE, or DELETE is always in
1345// the same database as the table that the trigger fires on.
1346//
1347%type trnm {Token}
drh4dd0d3f2016-02-17 01:18:33 +00001348trnm(A) ::= nm(A).
drhb1819a02009-07-03 15:37:27 +00001349trnm(A) ::= nm DOT nm(X). {
1350 A = X;
1351 sqlite3ErrorMsg(pParse,
1352 "qualified table names are not allowed on INSERT, UPDATE, and DELETE "
1353 "statements within triggers");
1354}
1355
1356// Disallow the INDEX BY and NOT INDEXED clauses on UPDATE and DELETE
1357// statements within triggers. We make a specific error message for this
1358// since it is an exception to the default grammar rules.
1359//
1360tridxby ::= .
1361tridxby ::= INDEXED BY nm. {
1362 sqlite3ErrorMsg(pParse,
1363 "the INDEXED BY clause is not allowed on UPDATE or DELETE statements "
1364 "within triggers");
1365}
1366tridxby ::= NOT INDEXED. {
1367 sqlite3ErrorMsg(pParse,
1368 "the NOT INDEXED clause is not allowed on UPDATE or DELETE statements "
1369 "within triggers");
1370}
1371
1372
1373
drh0bb132b2004-07-20 14:06:51 +00001374%type trigger_cmd {TriggerStep*}
drh633e6d52008-07-28 19:34:53 +00001375%destructor trigger_cmd {sqlite3DeleteTriggerStep(pParse->db, $$);}
danielk1977c3f9bad2002-05-15 08:30:12 +00001376// UPDATE
drhb1819a02009-07-03 15:37:27 +00001377trigger_cmd(A) ::=
drhf259df52017-12-27 20:38:35 +00001378 UPDATE(B) orconf(R) trnm(X) tridxby SET setlist(Y) where_opt(Z) scanpt(E).
1379 {A = sqlite3TriggerUpdateStep(pParse->db, &X, Y, Z, R, B.z, E);}
danielk1977c3f9bad2002-05-15 08:30:12 +00001380
1381// INSERT
drhf259df52017-12-27 20:38:35 +00001382trigger_cmd(A) ::= scanpt(B) insert_cmd(R) INTO
1383 trnm(X) idlist_opt(F) select(S) scanpt(Z).
1384 {A = sqlite3TriggerInsertStep(pParse->db,&X,F,S,R,B,Z);/*A-overwrites-R*/}
danielk1977c3f9bad2002-05-15 08:30:12 +00001385
1386// DELETE
drhf259df52017-12-27 20:38:35 +00001387trigger_cmd(A) ::= DELETE(B) FROM trnm(X) tridxby where_opt(Y) scanpt(E).
1388 {A = sqlite3TriggerDeleteStep(pParse->db, &X, Y, B.z, E);}
danielk1977c3f9bad2002-05-15 08:30:12 +00001389
1390// SELECT
drhf259df52017-12-27 20:38:35 +00001391trigger_cmd(A) ::= scanpt(B) select(X) scanpt(E).
1392 {A = sqlite3TriggerSelectStep(pParse->db, X, B, E); /*A-overwrites-X*/}
danielk1977c3f9bad2002-05-15 08:30:12 +00001393
danielk19776f349032002-06-11 02:25:40 +00001394// The special RAISE expression that may occur in trigger programs
drh1be266b2017-12-24 00:18:47 +00001395expr(A) ::= RAISE LP IGNORE RP. {
1396 A = sqlite3PExpr(pParse, TK_RAISE, 0, 0);
1397 if( A ){
1398 A->affinity = OE_Ignore;
drh8aa34ae2006-03-13 12:54:09 +00001399 }
drh4b59ab52002-08-24 18:24:51 +00001400}
drh1be266b2017-12-24 00:18:47 +00001401expr(A) ::= RAISE LP raisetype(T) COMMA nm(Z) RP. {
1402 A = sqlite3ExprAlloc(pParse->db, TK_RAISE, &Z, 1);
1403 if( A ) {
1404 A->affinity = (char)T;
drh8aa34ae2006-03-13 12:54:09 +00001405 }
drh4b59ab52002-08-24 18:24:51 +00001406}
drh154d4b22006-09-21 11:02:16 +00001407%endif !SQLITE_OMIT_TRIGGER
drhb7f91642004-10-31 02:22:47 +00001408
drh74ad7fe2004-10-07 03:06:28 +00001409%type raisetype {int}
1410raisetype(A) ::= ROLLBACK. {A = OE_Rollback;}
1411raisetype(A) ::= ABORT. {A = OE_Abort;}
1412raisetype(A) ::= FAIL. {A = OE_Fail;}
1413
danielk19776f349032002-06-11 02:25:40 +00001414
danielk1977c3f9bad2002-05-15 08:30:12 +00001415//////////////////////// DROP TRIGGER statement //////////////////////////////
drhb7f91642004-10-31 02:22:47 +00001416%ifndef SQLITE_OMIT_TRIGGER
drhfdd48a72006-09-11 23:45:48 +00001417cmd ::= DROP TRIGGER ifexists(NOERR) fullname(X). {
1418 sqlite3DropTrigger(pParse,X,NOERR);
danielk1977c3f9bad2002-05-15 08:30:12 +00001419}
drh154d4b22006-09-21 11:02:16 +00001420%endif !SQLITE_OMIT_TRIGGER
drh113088e2003-03-20 01:16:58 +00001421
1422//////////////////////// ATTACH DATABASE file AS name /////////////////////////
drhfdbcdee2007-03-27 14:44:50 +00001423%ifndef SQLITE_OMIT_ATTACH
danielk1977f744bb52005-12-06 17:19:11 +00001424cmd ::= ATTACH database_kw_opt expr(F) AS expr(D) key_opt(K). {
drh1be266b2017-12-24 00:18:47 +00001425 sqlite3Attach(pParse, F, D, K);
drh1c2d8412003-03-31 00:30:47 +00001426}
drhfdbcdee2007-03-27 14:44:50 +00001427cmd ::= DETACH database_kw_opt expr(D). {
drh1be266b2017-12-24 00:18:47 +00001428 sqlite3Detach(pParse, D);
drhfdbcdee2007-03-27 14:44:50 +00001429}
1430
drhc4dd3fd2008-01-22 01:48:05 +00001431%type key_opt {Expr*}
drh633e6d52008-07-28 19:34:53 +00001432%destructor key_opt {sqlite3ExprDelete(pParse->db, $$);}
danielk1977f744bb52005-12-06 17:19:11 +00001433key_opt(A) ::= . { A = 0; }
drh1be266b2017-12-24 00:18:47 +00001434key_opt(A) ::= KEY expr(X). { A = X; }
drh113088e2003-03-20 01:16:58 +00001435
1436database_kw_opt ::= DATABASE.
1437database_kw_opt ::= .
drhfdbcdee2007-03-27 14:44:50 +00001438%endif SQLITE_OMIT_ATTACH
drh4343fea2004-11-05 23:46:15 +00001439
1440////////////////////////// REINDEX collation //////////////////////////////////
1441%ifndef SQLITE_OMIT_REINDEX
1442cmd ::= REINDEX. {sqlite3Reindex(pParse, 0, 0);}
1443cmd ::= REINDEX nm(X) dbnm(Y). {sqlite3Reindex(pParse, &X, &Y);}
drh154d4b22006-09-21 11:02:16 +00001444%endif SQLITE_OMIT_REINDEX
danielk19779fd2a9a2004-11-12 13:42:30 +00001445
drh9f18e8a2005-07-08 12:13:04 +00001446/////////////////////////////////// ANALYZE ///////////////////////////////////
1447%ifndef SQLITE_OMIT_ANALYZE
1448cmd ::= ANALYZE. {sqlite3Analyze(pParse, 0, 0);}
1449cmd ::= ANALYZE nm(X) dbnm(Y). {sqlite3Analyze(pParse, &X, &Y);}
1450%endif
1451
danielk19779fd2a9a2004-11-12 13:42:30 +00001452//////////////////////// ALTER TABLE table ... ////////////////////////////////
1453%ifndef SQLITE_OMIT_ALTERTABLE
1454cmd ::= ALTER TABLE fullname(X) RENAME TO nm(Z). {
1455 sqlite3AlterRenameTable(pParse,X,&Z);
1456}
drh986dde72016-02-29 13:37:21 +00001457cmd ::= ALTER TABLE add_column_fullname
1458 ADD kwcolumn_opt columnname(Y) carglist. {
1459 Y.n = (int)(pParse->sLastToken.z-Y.z) + pParse->sLastToken.n;
danielk197719a8e7e2005-03-17 05:03:38 +00001460 sqlite3AlterFinishAddColumn(pParse, &Y);
1461}
1462add_column_fullname ::= fullname(X). {
drh4a642b62016-02-05 01:55:27 +00001463 disableLookaside(pParse);
danielk197719a8e7e2005-03-17 05:03:38 +00001464 sqlite3AlterBeginAddColumn(pParse, X);
1465}
1466kwcolumn_opt ::= .
1467kwcolumn_opt ::= COLUMNKW.
drh154d4b22006-09-21 11:02:16 +00001468%endif SQLITE_OMIT_ALTERTABLE
drhe09daa92006-06-10 13:29:31 +00001469
1470//////////////////////// CREATE VIRTUAL TABLE ... /////////////////////////////
1471%ifndef SQLITE_OMIT_VIRTUALTABLE
drhb9bb7c12006-06-11 23:41:55 +00001472cmd ::= create_vtab. {sqlite3VtabFinishParse(pParse,0);}
1473cmd ::= create_vtab LP vtabarglist RP(X). {sqlite3VtabFinishParse(pParse,&X);}
drhb421b892012-01-28 19:41:53 +00001474create_vtab ::= createkw VIRTUAL TABLE ifnotexists(E)
1475 nm(X) dbnm(Y) USING nm(Z). {
1476 sqlite3VtabBeginParse(pParse, &X, &Y, &Z, E);
drhb9bb7c12006-06-11 23:41:55 +00001477}
drhe09daa92006-06-10 13:29:31 +00001478vtabarglist ::= vtabarg.
1479vtabarglist ::= vtabarglist COMMA vtabarg.
drhb9bb7c12006-06-11 23:41:55 +00001480vtabarg ::= . {sqlite3VtabArgInit(pParse);}
1481vtabarg ::= vtabarg vtabargtoken.
1482vtabargtoken ::= ANY(X). {sqlite3VtabArgExtend(pParse,&X);}
1483vtabargtoken ::= lp anylist RP(X). {sqlite3VtabArgExtend(pParse,&X);}
1484lp ::= LP(X). {sqlite3VtabArgExtend(pParse,&X);}
1485anylist ::= .
drhaaac8b42009-05-11 18:22:30 +00001486anylist ::= anylist LP anylist RP.
1487anylist ::= anylist ANY.
drh154d4b22006-09-21 11:02:16 +00001488%endif SQLITE_OMIT_VIRTUALTABLE
drh8b471862014-01-11 13:22:17 +00001489
1490
1491//////////////////////// COMMON TABLE EXPRESSIONS ////////////////////////////
dan7d562db2014-01-11 19:19:36 +00001492%type with {With*}
1493%type wqlist {With*}
1494%destructor with {sqlite3WithDelete(pParse->db, $$);}
dan4e9119d2014-01-13 15:12:23 +00001495%destructor wqlist {sqlite3WithDelete(pParse->db, $$);}
dan7d562db2014-01-11 19:19:36 +00001496
1497with(A) ::= . {A = 0;}
drh8b471862014-01-11 13:22:17 +00001498%ifndef SQLITE_OMIT_CTE
dan7d562db2014-01-11 19:19:36 +00001499with(A) ::= WITH wqlist(W). { A = W; }
1500with(A) ::= WITH RECURSIVE wqlist(W). { A = W; }
1501
drh108aa002015-08-24 20:21:20 +00001502wqlist(A) ::= nm(X) eidlist_opt(Y) AS LP select(Z) RP. {
drhcf82f0d2016-02-17 04:33:10 +00001503 A = sqlite3WithAdd(pParse, 0, &X, Y, Z); /*A-overwrites-X*/
dan7d562db2014-01-11 19:19:36 +00001504}
drh4dd0d3f2016-02-17 01:18:33 +00001505wqlist(A) ::= wqlist(A) COMMA nm(X) eidlist_opt(Y) AS LP select(Z) RP. {
1506 A = sqlite3WithAdd(pParse, A, &X, Y, Z);
drh8b471862014-01-11 13:22:17 +00001507}
1508%endif SQLITE_OMIT_CTE