parse.y 49 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403
  1. /*
  2. ** 2001 September 15
  3. **
  4. ** The author disclaims copyright to this source code. In place of
  5. ** a legal notice, here is a blessing:
  6. **
  7. ** 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.
  10. **
  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. */
  17. // All token codes are small integers with #defines that begin with "TK_"
  18. %token_prefix TK_
  19. // The type of the data attached to each token is Token. This is also the
  20. // default type for non-terminals.
  21. //
  22. %token_type {Token}
  23. %default_type {Token}
  24. // The generated parser function takes a 4th argument as follows:
  25. %extra_argument {Parse *pParse}
  26. // This code runs whenever there is a syntax error
  27. //
  28. %syntax_error {
  29. UNUSED_PARAMETER(yymajor); /* Silence some compiler warnings */
  30. assert( TOKEN.z[0] ); /* The tokenizer always gives us a token */
  31. sqlite3ErrorMsg(pParse, "near \"%T\": syntax error", &TOKEN);
  32. }
  33. %stack_overflow {
  34. UNUSED_PARAMETER(yypMinor); /* Silence some compiler warnings */
  35. sqlite3ErrorMsg(pParse, "parser stack overflow");
  36. }
  37. // The name of the generated procedure that implements the parser
  38. // is as follows:
  39. %name sqlite3Parser
  40. // The following text is included near the beginning of the C source
  41. // code file that implements the parser.
  42. //
  43. %include {
  44. #include "sqliteInt.h"
  45. /*
  46. ** Disable all error recovery processing in the parser push-down
  47. ** automaton.
  48. */
  49. #define YYNOERRORRECOVERY 1
  50. /*
  51. ** Make yytestcase() the same as testcase()
  52. */
  53. #define yytestcase(X) testcase(X)
  54. /*
  55. ** An instance of this structure holds information about the
  56. ** LIMIT clause of a SELECT statement.
  57. */
  58. struct LimitVal {
  59. Expr *pLimit; /* The LIMIT expression. NULL if there is no limit */
  60. Expr *pOffset; /* The OFFSET expression. NULL if there is none */
  61. };
  62. /*
  63. ** An instance of this structure is used to store the LIKE,
  64. ** GLOB, NOT LIKE, and NOT GLOB operators.
  65. */
  66. struct LikeOp {
  67. Token eOperator; /* "like" or "glob" or "regexp" */
  68. int bNot; /* True if the NOT keyword is present */
  69. };
  70. /*
  71. ** An instance of the following structure describes the event of a
  72. ** TRIGGER. "a" is the event type, one of TK_UPDATE, TK_INSERT,
  73. ** TK_DELETE, or TK_INSTEAD. If the event is of the form
  74. **
  75. ** UPDATE ON (a,b,c)
  76. **
  77. ** Then the "b" IdList records the list "a,b,c".
  78. */
  79. struct TrigEvent { int a; IdList * b; };
  80. /*
  81. ** An instance of this structure holds the ATTACH key and the key type.
  82. */
  83. struct AttachKey { int type; Token key; };
  84. /*
  85. ** One or more VALUES claues
  86. */
  87. struct ValueList {
  88. ExprList *pList;
  89. Select *pSelect;
  90. };
  91. } // end %include
  92. // Input is a single SQL command
  93. input ::= cmdlist.
  94. cmdlist ::= cmdlist ecmd.
  95. cmdlist ::= ecmd.
  96. ecmd ::= SEMI.
  97. ecmd ::= explain cmdx SEMI.
  98. explain ::= . { sqlite3BeginParse(pParse, 0); }
  99. %ifndef SQLITE_OMIT_EXPLAIN
  100. explain ::= EXPLAIN. { sqlite3BeginParse(pParse, 1); }
  101. explain ::= EXPLAIN QUERY PLAN. { sqlite3BeginParse(pParse, 2); }
  102. %endif SQLITE_OMIT_EXPLAIN
  103. cmdx ::= cmd. { sqlite3FinishCoding(pParse); }
  104. ///////////////////// Begin and end transactions. ////////////////////////////
  105. //
  106. cmd ::= BEGIN transtype(Y) trans_opt. {sqlite3BeginTransaction(pParse, Y);}
  107. trans_opt ::= .
  108. trans_opt ::= TRANSACTION.
  109. trans_opt ::= TRANSACTION nm.
  110. %type transtype {int}
  111. transtype(A) ::= . {A = TK_DEFERRED;}
  112. transtype(A) ::= DEFERRED(X). {A = @X;}
  113. transtype(A) ::= IMMEDIATE(X). {A = @X;}
  114. transtype(A) ::= EXCLUSIVE(X). {A = @X;}
  115. cmd ::= COMMIT trans_opt. {sqlite3CommitTransaction(pParse);}
  116. cmd ::= END trans_opt. {sqlite3CommitTransaction(pParse);}
  117. cmd ::= ROLLBACK trans_opt. {sqlite3RollbackTransaction(pParse);}
  118. savepoint_opt ::= SAVEPOINT.
  119. savepoint_opt ::= .
  120. cmd ::= SAVEPOINT nm(X). {
  121. sqlite3Savepoint(pParse, SAVEPOINT_BEGIN, &X);
  122. }
  123. cmd ::= RELEASE savepoint_opt nm(X). {
  124. sqlite3Savepoint(pParse, SAVEPOINT_RELEASE, &X);
  125. }
  126. cmd ::= ROLLBACK trans_opt TO savepoint_opt nm(X). {
  127. sqlite3Savepoint(pParse, SAVEPOINT_ROLLBACK, &X);
  128. }
  129. ///////////////////// The CREATE TABLE statement ////////////////////////////
  130. //
  131. cmd ::= create_table create_table_args.
  132. create_table ::= createkw temp(T) TABLE ifnotexists(E) nm(Y) dbnm(Z). {
  133. sqlite3StartTable(pParse,&Y,&Z,T,0,0,E);
  134. }
  135. createkw(A) ::= CREATE(X). {
  136. pParse->db->lookaside.bEnabled = 0;
  137. A = X;
  138. }
  139. %type ifnotexists {int}
  140. ifnotexists(A) ::= . {A = 0;}
  141. ifnotexists(A) ::= IF NOT EXISTS. {A = 1;}
  142. %type temp {int}
  143. %ifndef SQLITE_OMIT_TEMPDB
  144. temp(A) ::= TEMP. {A = 1;}
  145. %endif SQLITE_OMIT_TEMPDB
  146. temp(A) ::= . {A = 0;}
  147. create_table_args ::= LP columnlist conslist_opt(X) RP(Y). {
  148. sqlite3EndTable(pParse,&X,&Y,0);
  149. }
  150. create_table_args ::= AS select(S). {
  151. sqlite3EndTable(pParse,0,0,S);
  152. sqlite3SelectDelete(pParse->db, S);
  153. }
  154. columnlist ::= columnlist COMMA column.
  155. columnlist ::= column.
  156. // A "column" is a complete description of a single column in a
  157. // CREATE TABLE statement. This includes the column name, its
  158. // datatype, and other keywords such as PRIMARY KEY, UNIQUE, REFERENCES,
  159. // NOT NULL and so forth.
  160. //
  161. column(A) ::= columnid(X) type carglist. {
  162. A.z = X.z;
  163. A.n = (int)(pParse->sLastToken.z-X.z) + pParse->sLastToken.n;
  164. }
  165. columnid(A) ::= nm(X). {
  166. sqlite3AddColumn(pParse,&X);
  167. A = X;
  168. pParse->constraintName.n = 0;
  169. }
  170. // An IDENTIFIER can be a generic identifier, or one of several
  171. // keywords. Any non-standard keyword can also be an identifier.
  172. //
  173. %type id {Token}
  174. id(A) ::= ID(X). {A = X;}
  175. id(A) ::= INDEXED(X). {A = X;}
  176. // The following directive causes tokens ABORT, AFTER, ASC, etc. to
  177. // fallback to ID if they will not parse as their original value.
  178. // This obviates the need for the "id" nonterminal.
  179. //
  180. %fallback ID
  181. ABORT ACTION AFTER ANALYZE ASC ATTACH BEFORE BEGIN BY CASCADE CAST COLUMNKW
  182. CONFLICT DATABASE DEFERRED DESC DETACH EACH END EXCLUSIVE EXPLAIN FAIL FOR
  183. IGNORE IMMEDIATE INITIALLY INSTEAD LIKE_KW MATCH NO PLAN
  184. QUERY KEY OF OFFSET PRAGMA RAISE RELEASE REPLACE RESTRICT ROW ROLLBACK
  185. SAVEPOINT TEMP TRIGGER VACUUM VIEW VIRTUAL
  186. %ifdef SQLITE_OMIT_COMPOUND_SELECT
  187. EXCEPT INTERSECT UNION
  188. %endif SQLITE_OMIT_COMPOUND_SELECT
  189. REINDEX RENAME CTIME_KW IF
  190. .
  191. %wildcard ANY.
  192. // Define operator precedence early so that this is the first occurrence
  193. // of the operator tokens in the grammer. Keeping the operators together
  194. // causes them to be assigned integer values that are close together,
  195. // which keeps parser tables smaller.
  196. //
  197. // The token values assigned to these symbols is determined by the order
  198. // in which lemon first sees them. It must be the case that ISNULL/NOTNULL,
  199. // NE/EQ, GT/LE, and GE/LT are separated by only a single value. See
  200. // the sqlite3ExprIfFalse() routine for additional information on this
  201. // constraint.
  202. //
  203. %left OR.
  204. %left AND.
  205. %right NOT.
  206. %left IS MATCH LIKE_KW BETWEEN IN ISNULL NOTNULL NE EQ.
  207. %left GT LE LT GE.
  208. %right ESCAPE.
  209. %left BITAND BITOR LSHIFT RSHIFT.
  210. %left PLUS MINUS.
  211. %left STAR SLASH REM.
  212. %left CONCAT.
  213. %left COLLATE.
  214. %right BITNOT.
  215. // And "ids" is an identifer-or-string.
  216. //
  217. %type ids {Token}
  218. ids(A) ::= ID|STRING(X). {A = X;}
  219. // The name of a column or table can be any of the following:
  220. //
  221. %type nm {Token}
  222. nm(A) ::= id(X). {A = X;}
  223. nm(A) ::= STRING(X). {A = X;}
  224. nm(A) ::= JOIN_KW(X). {A = X;}
  225. // A typetoken is really one or more tokens that form a type name such
  226. // as can be found after the column name in a CREATE TABLE statement.
  227. // Multiple tokens are concatenated to form the value of the typetoken.
  228. //
  229. %type typetoken {Token}
  230. type ::= .
  231. type ::= typetoken(X). {sqlite3AddColumnType(pParse,&X);}
  232. typetoken(A) ::= typename(X). {A = X;}
  233. typetoken(A) ::= typename(X) LP signed RP(Y). {
  234. A.z = X.z;
  235. A.n = (int)(&Y.z[Y.n] - X.z);
  236. }
  237. typetoken(A) ::= typename(X) LP signed COMMA signed RP(Y). {
  238. A.z = X.z;
  239. A.n = (int)(&Y.z[Y.n] - X.z);
  240. }
  241. %type typename {Token}
  242. typename(A) ::= ids(X). {A = X;}
  243. typename(A) ::= typename(X) ids(Y). {A.z=X.z; A.n=Y.n+(int)(Y.z-X.z);}
  244. signed ::= plus_num.
  245. signed ::= minus_num.
  246. // "carglist" is a list of additional constraints that come after the
  247. // column name and column type in a CREATE TABLE statement.
  248. //
  249. carglist ::= carglist ccons.
  250. carglist ::= .
  251. ccons ::= CONSTRAINT nm(X). {pParse->constraintName = X;}
  252. ccons ::= DEFAULT term(X). {sqlite3AddDefaultValue(pParse,&X);}
  253. ccons ::= DEFAULT LP expr(X) RP. {sqlite3AddDefaultValue(pParse,&X);}
  254. ccons ::= DEFAULT PLUS term(X). {sqlite3AddDefaultValue(pParse,&X);}
  255. ccons ::= DEFAULT MINUS(A) term(X). {
  256. ExprSpan v;
  257. v.pExpr = sqlite3PExpr(pParse, TK_UMINUS, X.pExpr, 0, 0);
  258. v.zStart = A.z;
  259. v.zEnd = X.zEnd;
  260. sqlite3AddDefaultValue(pParse,&v);
  261. }
  262. ccons ::= DEFAULT id(X). {
  263. ExprSpan v;
  264. spanExpr(&v, pParse, TK_STRING, &X);
  265. sqlite3AddDefaultValue(pParse,&v);
  266. }
  267. // In addition to the type name, we also care about the primary key and
  268. // UNIQUE constraints.
  269. //
  270. ccons ::= NULL onconf.
  271. ccons ::= NOT NULL onconf(R). {sqlite3AddNotNull(pParse, R);}
  272. ccons ::= PRIMARY KEY sortorder(Z) onconf(R) autoinc(I).
  273. {sqlite3AddPrimaryKey(pParse,0,R,I,Z);}
  274. ccons ::= UNIQUE onconf(R). {sqlite3CreateIndex(pParse,0,0,0,0,R,0,0,0,0);}
  275. ccons ::= CHECK LP expr(X) RP. {sqlite3AddCheckConstraint(pParse,X.pExpr);}
  276. ccons ::= REFERENCES nm(T) idxlist_opt(TA) refargs(R).
  277. {sqlite3CreateForeignKey(pParse,0,&T,TA,R);}
  278. ccons ::= defer_subclause(D). {sqlite3DeferForeignKey(pParse,D);}
  279. ccons ::= COLLATE ids(C). {sqlite3AddCollateType(pParse, &C);}
  280. // The optional AUTOINCREMENT keyword
  281. %type autoinc {int}
  282. autoinc(X) ::= . {X = 0;}
  283. autoinc(X) ::= AUTOINCR. {X = 1;}
  284. // The next group of rules parses the arguments to a REFERENCES clause
  285. // that determine if the referential integrity checking is deferred or
  286. // or immediate and which determine what action to take if a ref-integ
  287. // check fails.
  288. //
  289. %type refargs {int}
  290. refargs(A) ::= . { A = OE_None*0x0101; /* EV: R-19803-45884 */}
  291. refargs(A) ::= refargs(X) refarg(Y). { A = (X & ~Y.mask) | Y.value; }
  292. %type refarg {struct {int value; int mask;}}
  293. refarg(A) ::= MATCH nm. { A.value = 0; A.mask = 0x000000; }
  294. refarg(A) ::= ON INSERT refact. { A.value = 0; A.mask = 0x000000; }
  295. refarg(A) ::= ON DELETE refact(X). { A.value = X; A.mask = 0x0000ff; }
  296. refarg(A) ::= ON UPDATE refact(X). { A.value = X<<8; A.mask = 0x00ff00; }
  297. %type refact {int}
  298. refact(A) ::= SET NULL. { A = OE_SetNull; /* EV: R-33326-45252 */}
  299. refact(A) ::= SET DEFAULT. { A = OE_SetDflt; /* EV: R-33326-45252 */}
  300. refact(A) ::= CASCADE. { A = OE_Cascade; /* EV: R-33326-45252 */}
  301. refact(A) ::= RESTRICT. { A = OE_Restrict; /* EV: R-33326-45252 */}
  302. refact(A) ::= NO ACTION. { A = OE_None; /* EV: R-33326-45252 */}
  303. %type defer_subclause {int}
  304. defer_subclause(A) ::= NOT DEFERRABLE init_deferred_pred_opt. {A = 0;}
  305. defer_subclause(A) ::= DEFERRABLE init_deferred_pred_opt(X). {A = X;}
  306. %type init_deferred_pred_opt {int}
  307. init_deferred_pred_opt(A) ::= . {A = 0;}
  308. init_deferred_pred_opt(A) ::= INITIALLY DEFERRED. {A = 1;}
  309. init_deferred_pred_opt(A) ::= INITIALLY IMMEDIATE. {A = 0;}
  310. conslist_opt(A) ::= . {A.n = 0; A.z = 0;}
  311. conslist_opt(A) ::= COMMA(X) conslist. {A = X;}
  312. conslist ::= conslist tconscomma tcons.
  313. conslist ::= tcons.
  314. tconscomma ::= COMMA. {pParse->constraintName.n = 0;}
  315. tconscomma ::= .
  316. tcons ::= CONSTRAINT nm(X). {pParse->constraintName = X;}
  317. tcons ::= PRIMARY KEY LP idxlist(X) autoinc(I) RP onconf(R).
  318. {sqlite3AddPrimaryKey(pParse,X,R,I,0);}
  319. tcons ::= UNIQUE LP idxlist(X) RP onconf(R).
  320. {sqlite3CreateIndex(pParse,0,0,0,X,R,0,0,0,0);}
  321. tcons ::= CHECK LP expr(E) RP onconf.
  322. {sqlite3AddCheckConstraint(pParse,E.pExpr);}
  323. tcons ::= FOREIGN KEY LP idxlist(FA) RP
  324. REFERENCES nm(T) idxlist_opt(TA) refargs(R) defer_subclause_opt(D). {
  325. sqlite3CreateForeignKey(pParse, FA, &T, TA, R);
  326. sqlite3DeferForeignKey(pParse, D);
  327. }
  328. %type defer_subclause_opt {int}
  329. defer_subclause_opt(A) ::= . {A = 0;}
  330. defer_subclause_opt(A) ::= defer_subclause(X). {A = X;}
  331. // The following is a non-standard extension that allows us to declare the
  332. // default behavior when there is a constraint conflict.
  333. //
  334. %type onconf {int}
  335. %type orconf {u8}
  336. %type resolvetype {int}
  337. onconf(A) ::= . {A = OE_Default;}
  338. onconf(A) ::= ON CONFLICT resolvetype(X). {A = X;}
  339. orconf(A) ::= . {A = OE_Default;}
  340. orconf(A) ::= OR resolvetype(X). {A = (u8)X;}
  341. resolvetype(A) ::= raisetype(X). {A = X;}
  342. resolvetype(A) ::= IGNORE. {A = OE_Ignore;}
  343. resolvetype(A) ::= REPLACE. {A = OE_Replace;}
  344. ////////////////////////// The DROP TABLE /////////////////////////////////////
  345. //
  346. cmd ::= DROP TABLE ifexists(E) fullname(X). {
  347. sqlite3DropTable(pParse, X, 0, E);
  348. }
  349. %type ifexists {int}
  350. ifexists(A) ::= IF EXISTS. {A = 1;}
  351. ifexists(A) ::= . {A = 0;}
  352. ///////////////////// The CREATE VIEW statement /////////////////////////////
  353. //
  354. %ifndef SQLITE_OMIT_VIEW
  355. cmd ::= createkw(X) temp(T) VIEW ifnotexists(E) nm(Y) dbnm(Z) AS select(S). {
  356. sqlite3CreateView(pParse, &X, &Y, &Z, S, T, E);
  357. }
  358. cmd ::= DROP VIEW ifexists(E) fullname(X). {
  359. sqlite3DropTable(pParse, X, 1, E);
  360. }
  361. %endif SQLITE_OMIT_VIEW
  362. //////////////////////// The SELECT statement /////////////////////////////////
  363. //
  364. cmd ::= select(X). {
  365. SelectDest dest = {SRT_Output, 0, 0, 0, 0};
  366. sqlite3Select(pParse, X, &dest);
  367. sqlite3ExplainBegin(pParse->pVdbe);
  368. sqlite3ExplainSelect(pParse->pVdbe, X);
  369. sqlite3ExplainFinish(pParse->pVdbe);
  370. sqlite3SelectDelete(pParse->db, X);
  371. }
  372. %type select {Select*}
  373. %destructor select {sqlite3SelectDelete(pParse->db, $$);}
  374. %type oneselect {Select*}
  375. %destructor oneselect {sqlite3SelectDelete(pParse->db, $$);}
  376. select(A) ::= oneselect(X). {A = X;}
  377. %ifndef SQLITE_OMIT_COMPOUND_SELECT
  378. select(A) ::= select(X) multiselect_op(Y) oneselect(Z). {
  379. if( Z ){
  380. Z->op = (u8)Y;
  381. Z->pPrior = X;
  382. if( Y!=TK_ALL ) pParse->hasCompound = 1;
  383. }else{
  384. sqlite3SelectDelete(pParse->db, X);
  385. }
  386. A = Z;
  387. }
  388. %type multiselect_op {int}
  389. multiselect_op(A) ::= UNION(OP). {A = @OP;}
  390. multiselect_op(A) ::= UNION ALL. {A = TK_ALL;}
  391. multiselect_op(A) ::= EXCEPT|INTERSECT(OP). {A = @OP;}
  392. %endif SQLITE_OMIT_COMPOUND_SELECT
  393. oneselect(A) ::= SELECT distinct(D) selcollist(W) from(X) where_opt(Y)
  394. groupby_opt(P) having_opt(Q) orderby_opt(Z) limit_opt(L). {
  395. A = sqlite3SelectNew(pParse,W,X,Y,P,Q,Z,D,L.pLimit,L.pOffset);
  396. }
  397. // The "distinct" nonterminal is true (1) if the DISTINCT keyword is
  398. // present and false (0) if it is not.
  399. //
  400. %type distinct {u16}
  401. distinct(A) ::= DISTINCT. {A = SF_Distinct;}
  402. distinct(A) ::= ALL. {A = 0;}
  403. distinct(A) ::= . {A = 0;}
  404. // selcollist is a list of expressions that are to become the return
  405. // values of the SELECT statement. The "*" in statements like
  406. // "SELECT * FROM ..." is encoded as a special expression with an
  407. // opcode of TK_ALL.
  408. //
  409. %type selcollist {ExprList*}
  410. %destructor selcollist {sqlite3ExprListDelete(pParse->db, $$);}
  411. %type sclp {ExprList*}
  412. %destructor sclp {sqlite3ExprListDelete(pParse->db, $$);}
  413. sclp(A) ::= selcollist(X) COMMA. {A = X;}
  414. sclp(A) ::= . {A = 0;}
  415. selcollist(A) ::= sclp(P) expr(X) as(Y). {
  416. A = sqlite3ExprListAppend(pParse, P, X.pExpr);
  417. if( Y.n>0 ) sqlite3ExprListSetName(pParse, A, &Y, 1);
  418. sqlite3ExprListSetSpan(pParse,A,&X);
  419. }
  420. selcollist(A) ::= sclp(P) STAR. {
  421. Expr *p = sqlite3Expr(pParse->db, TK_ALL, 0);
  422. A = sqlite3ExprListAppend(pParse, P, p);
  423. }
  424. selcollist(A) ::= sclp(P) nm(X) DOT STAR(Y). {
  425. Expr *pRight = sqlite3PExpr(pParse, TK_ALL, 0, 0, &Y);
  426. Expr *pLeft = sqlite3PExpr(pParse, TK_ID, 0, 0, &X);
  427. Expr *pDot = sqlite3PExpr(pParse, TK_DOT, pLeft, pRight, 0);
  428. A = sqlite3ExprListAppend(pParse,P, pDot);
  429. }
  430. // An option "AS <id>" phrase that can follow one of the expressions that
  431. // define the result set, or one of the tables in the FROM clause.
  432. //
  433. %type as {Token}
  434. as(X) ::= AS nm(Y). {X = Y;}
  435. as(X) ::= ids(Y). {X = Y;}
  436. as(X) ::= . {X.n = 0;}
  437. %type seltablist {SrcList*}
  438. %destructor seltablist {sqlite3SrcListDelete(pParse->db, $$);}
  439. %type stl_prefix {SrcList*}
  440. %destructor stl_prefix {sqlite3SrcListDelete(pParse->db, $$);}
  441. %type from {SrcList*}
  442. %destructor from {sqlite3SrcListDelete(pParse->db, $$);}
  443. // A complete FROM clause.
  444. //
  445. from(A) ::= . {A = sqlite3DbMallocZero(pParse->db, sizeof(*A));}
  446. from(A) ::= FROM seltablist(X). {
  447. A = X;
  448. sqlite3SrcListShiftJoinType(A);
  449. }
  450. // "seltablist" is a "Select Table List" - the content of the FROM clause
  451. // in a SELECT statement. "stl_prefix" is a prefix of this list.
  452. //
  453. stl_prefix(A) ::= seltablist(X) joinop(Y). {
  454. A = X;
  455. if( ALWAYS(A && A->nSrc>0) ) A->a[A->nSrc-1].jointype = (u8)Y;
  456. }
  457. stl_prefix(A) ::= . {A = 0;}
  458. seltablist(A) ::= stl_prefix(X) nm(Y) dbnm(D) as(Z) indexed_opt(I)
  459. on_opt(N) using_opt(U). {
  460. A = sqlite3SrcListAppendFromTerm(pParse,X,&Y,&D,&Z,0,N,U);
  461. sqlite3SrcListIndexedBy(pParse, A, &I);
  462. }
  463. %ifndef SQLITE_OMIT_SUBQUERY
  464. seltablist(A) ::= stl_prefix(X) LP select(S) RP
  465. as(Z) on_opt(N) using_opt(U). {
  466. A = sqlite3SrcListAppendFromTerm(pParse,X,0,0,&Z,S,N,U);
  467. }
  468. seltablist(A) ::= stl_prefix(X) LP seltablist(F) RP
  469. as(Z) on_opt(N) using_opt(U). {
  470. if( X==0 && Z.n==0 && N==0 && U==0 ){
  471. A = F;
  472. }else if( F->nSrc==1 ){
  473. A = sqlite3SrcListAppendFromTerm(pParse,X,0,0,&Z,0,N,U);
  474. if( A ){
  475. struct SrcList_item *pNew = &A->a[A->nSrc-1];
  476. struct SrcList_item *pOld = F->a;
  477. pNew->zName = pOld->zName;
  478. pNew->zDatabase = pOld->zDatabase;
  479. pNew->pSelect = pOld->pSelect;
  480. pOld->zName = pOld->zDatabase = 0;
  481. pOld->pSelect = 0;
  482. }
  483. sqlite3SrcListDelete(pParse->db, F);
  484. }else{
  485. Select *pSubquery;
  486. sqlite3SrcListShiftJoinType(F);
  487. pSubquery = sqlite3SelectNew(pParse,0,F,0,0,0,0,SF_NestedFrom,0,0);
  488. A = sqlite3SrcListAppendFromTerm(pParse,X,0,0,&Z,pSubquery,N,U);
  489. }
  490. }
  491. %endif SQLITE_OMIT_SUBQUERY
  492. %type dbnm {Token}
  493. dbnm(A) ::= . {A.z=0; A.n=0;}
  494. dbnm(A) ::= DOT nm(X). {A = X;}
  495. %type fullname {SrcList*}
  496. %destructor fullname {sqlite3SrcListDelete(pParse->db, $$);}
  497. fullname(A) ::= nm(X) dbnm(Y). {A = sqlite3SrcListAppend(pParse->db,0,&X,&Y);}
  498. %type joinop {int}
  499. %type joinop2 {int}
  500. joinop(X) ::= COMMA|JOIN. { X = JT_INNER; }
  501. joinop(X) ::= JOIN_KW(A) JOIN. { X = sqlite3JoinType(pParse,&A,0,0); }
  502. joinop(X) ::= JOIN_KW(A) nm(B) JOIN. { X = sqlite3JoinType(pParse,&A,&B,0); }
  503. joinop(X) ::= JOIN_KW(A) nm(B) nm(C) JOIN.
  504. { X = sqlite3JoinType(pParse,&A,&B,&C); }
  505. %type on_opt {Expr*}
  506. %destructor on_opt {sqlite3ExprDelete(pParse->db, $$);}
  507. on_opt(N) ::= ON expr(E). {N = E.pExpr;}
  508. on_opt(N) ::= . {N = 0;}
  509. // Note that this block abuses the Token type just a little. If there is
  510. // no "INDEXED BY" clause, the returned token is empty (z==0 && n==0). If
  511. // there is an INDEXED BY clause, then the token is populated as per normal,
  512. // with z pointing to the token data and n containing the number of bytes
  513. // in the token.
  514. //
  515. // If there is a "NOT INDEXED" clause, then (z==0 && n==1), which is
  516. // normally illegal. The sqlite3SrcListIndexedBy() function
  517. // recognizes and interprets this as a special case.
  518. //
  519. %type indexed_opt {Token}
  520. indexed_opt(A) ::= . {A.z=0; A.n=0;}
  521. indexed_opt(A) ::= INDEXED BY nm(X). {A = X;}
  522. indexed_opt(A) ::= NOT INDEXED. {A.z=0; A.n=1;}
  523. %type using_opt {IdList*}
  524. %destructor using_opt {sqlite3IdListDelete(pParse->db, $$);}
  525. using_opt(U) ::= USING LP inscollist(L) RP. {U = L;}
  526. using_opt(U) ::= . {U = 0;}
  527. %type orderby_opt {ExprList*}
  528. %destructor orderby_opt {sqlite3ExprListDelete(pParse->db, $$);}
  529. %type sortlist {ExprList*}
  530. %destructor sortlist {sqlite3ExprListDelete(pParse->db, $$);}
  531. orderby_opt(A) ::= . {A = 0;}
  532. orderby_opt(A) ::= ORDER BY sortlist(X). {A = X;}
  533. sortlist(A) ::= sortlist(X) COMMA expr(Y) sortorder(Z). {
  534. A = sqlite3ExprListAppend(pParse,X,Y.pExpr);
  535. if( A ) A->a[A->nExpr-1].sortOrder = (u8)Z;
  536. }
  537. sortlist(A) ::= expr(Y) sortorder(Z). {
  538. A = sqlite3ExprListAppend(pParse,0,Y.pExpr);
  539. if( A && ALWAYS(A->a) ) A->a[0].sortOrder = (u8)Z;
  540. }
  541. %type sortorder {int}
  542. sortorder(A) ::= ASC. {A = SQLITE_SO_ASC;}
  543. sortorder(A) ::= DESC. {A = SQLITE_SO_DESC;}
  544. sortorder(A) ::= . {A = SQLITE_SO_ASC;}
  545. %type groupby_opt {ExprList*}
  546. %destructor groupby_opt {sqlite3ExprListDelete(pParse->db, $$);}
  547. groupby_opt(A) ::= . {A = 0;}
  548. groupby_opt(A) ::= GROUP BY nexprlist(X). {A = X;}
  549. %type having_opt {Expr*}
  550. %destructor having_opt {sqlite3ExprDelete(pParse->db, $$);}
  551. having_opt(A) ::= . {A = 0;}
  552. having_opt(A) ::= HAVING expr(X). {A = X.pExpr;}
  553. %type limit_opt {struct LimitVal}
  554. // The destructor for limit_opt will never fire in the current grammar.
  555. // The limit_opt non-terminal only occurs at the end of a single production
  556. // rule for SELECT statements. As soon as the rule that create the
  557. // limit_opt non-terminal reduces, the SELECT statement rule will also
  558. // reduce. So there is never a limit_opt non-terminal on the stack
  559. // except as a transient. So there is never anything to destroy.
  560. //
  561. //%destructor limit_opt {
  562. // sqlite3ExprDelete(pParse->db, $$.pLimit);
  563. // sqlite3ExprDelete(pParse->db, $$.pOffset);
  564. //}
  565. limit_opt(A) ::= . {A.pLimit = 0; A.pOffset = 0;}
  566. limit_opt(A) ::= LIMIT expr(X). {A.pLimit = X.pExpr; A.pOffset = 0;}
  567. limit_opt(A) ::= LIMIT expr(X) OFFSET expr(Y).
  568. {A.pLimit = X.pExpr; A.pOffset = Y.pExpr;}
  569. limit_opt(A) ::= LIMIT expr(X) COMMA expr(Y).
  570. {A.pOffset = X.pExpr; A.pLimit = Y.pExpr;}
  571. /////////////////////////// The DELETE statement /////////////////////////////
  572. //
  573. %ifdef SQLITE_ENABLE_UPDATE_DELETE_LIMIT
  574. cmd ::= DELETE FROM fullname(X) indexed_opt(I) where_opt(W)
  575. orderby_opt(O) limit_opt(L). {
  576. sqlite3SrcListIndexedBy(pParse, X, &I);
  577. W = sqlite3LimitWhere(pParse, X, W, O, L.pLimit, L.pOffset, "DELETE");
  578. sqlite3DeleteFrom(pParse,X,W);
  579. }
  580. %endif
  581. %ifndef SQLITE_ENABLE_UPDATE_DELETE_LIMIT
  582. cmd ::= DELETE FROM fullname(X) indexed_opt(I) where_opt(W). {
  583. sqlite3SrcListIndexedBy(pParse, X, &I);
  584. sqlite3DeleteFrom(pParse,X,W);
  585. }
  586. %endif
  587. %type where_opt {Expr*}
  588. %destructor where_opt {sqlite3ExprDelete(pParse->db, $$);}
  589. where_opt(A) ::= . {A = 0;}
  590. where_opt(A) ::= WHERE expr(X). {A = X.pExpr;}
  591. ////////////////////////// The UPDATE command ////////////////////////////////
  592. //
  593. %ifdef SQLITE_ENABLE_UPDATE_DELETE_LIMIT
  594. cmd ::= UPDATE orconf(R) fullname(X) indexed_opt(I) SET setlist(Y) where_opt(W)
  595. orderby_opt(O) limit_opt(L). {
  596. sqlite3SrcListIndexedBy(pParse, X, &I);
  597. sqlite3ExprListCheckLength(pParse,Y,"set list");
  598. W = sqlite3LimitWhere(pParse, X, W, O, L.pLimit, L.pOffset, "UPDATE");
  599. sqlite3Update(pParse,X,Y,W,R);
  600. }
  601. %endif
  602. %ifndef SQLITE_ENABLE_UPDATE_DELETE_LIMIT
  603. cmd ::= UPDATE orconf(R) fullname(X) indexed_opt(I) SET setlist(Y)
  604. where_opt(W). {
  605. sqlite3SrcListIndexedBy(pParse, X, &I);
  606. sqlite3ExprListCheckLength(pParse,Y,"set list");
  607. sqlite3Update(pParse,X,Y,W,R);
  608. }
  609. %endif
  610. %type setlist {ExprList*}
  611. %destructor setlist {sqlite3ExprListDelete(pParse->db, $$);}
  612. setlist(A) ::= setlist(Z) COMMA nm(X) EQ expr(Y). {
  613. A = sqlite3ExprListAppend(pParse, Z, Y.pExpr);
  614. sqlite3ExprListSetName(pParse, A, &X, 1);
  615. }
  616. setlist(A) ::= nm(X) EQ expr(Y). {
  617. A = sqlite3ExprListAppend(pParse, 0, Y.pExpr);
  618. sqlite3ExprListSetName(pParse, A, &X, 1);
  619. }
  620. ////////////////////////// The INSERT command /////////////////////////////////
  621. //
  622. cmd ::= insert_cmd(R) INTO fullname(X) inscollist_opt(F) valuelist(Y).
  623. {sqlite3Insert(pParse, X, Y.pList, Y.pSelect, F, R);}
  624. cmd ::= insert_cmd(R) INTO fullname(X) inscollist_opt(F) select(S).
  625. {sqlite3Insert(pParse, X, 0, S, F, R);}
  626. cmd ::= insert_cmd(R) INTO fullname(X) inscollist_opt(F) DEFAULT VALUES.
  627. {sqlite3Insert(pParse, X, 0, 0, F, R);}
  628. %type insert_cmd {u8}
  629. insert_cmd(A) ::= INSERT orconf(R). {A = R;}
  630. insert_cmd(A) ::= REPLACE. {A = OE_Replace;}
  631. // A ValueList is either a single VALUES clause or a comma-separated list
  632. // of VALUES clauses. If it is a single VALUES clause then the
  633. // ValueList.pList field points to the expression list of that clause.
  634. // If it is a list of VALUES clauses, then those clauses are transformed
  635. // into a set of SELECT statements without FROM clauses and connected by
  636. // UNION ALL and the ValueList.pSelect points to the right-most SELECT in
  637. // that compound.
  638. %type valuelist {struct ValueList}
  639. %destructor valuelist {
  640. sqlite3ExprListDelete(pParse->db, $$.pList);
  641. sqlite3SelectDelete(pParse->db, $$.pSelect);
  642. }
  643. valuelist(A) ::= VALUES LP nexprlist(X) RP. {
  644. A.pList = X;
  645. A.pSelect = 0;
  646. }
  647. // Since a list of VALUEs is inplemented as a compound SELECT, we have
  648. // to disable the value list option if compound SELECTs are disabled.
  649. %ifndef SQLITE_OMIT_COMPOUND_SELECT
  650. valuelist(A) ::= valuelist(X) COMMA LP exprlist(Y) RP. {
  651. Select *pRight = sqlite3SelectNew(pParse, Y, 0, 0, 0, 0, 0, 0, 0, 0);
  652. if( X.pList ){
  653. X.pSelect = sqlite3SelectNew(pParse, X.pList, 0, 0, 0, 0, 0, 0, 0, 0);
  654. X.pList = 0;
  655. }
  656. A.pList = 0;
  657. if( X.pSelect==0 || pRight==0 ){
  658. sqlite3SelectDelete(pParse->db, pRight);
  659. sqlite3SelectDelete(pParse->db, X.pSelect);
  660. A.pSelect = 0;
  661. }else{
  662. pRight->op = TK_ALL;
  663. pRight->pPrior = X.pSelect;
  664. pRight->selFlags |= SF_Values;
  665. pRight->pPrior->selFlags |= SF_Values;
  666. A.pSelect = pRight;
  667. }
  668. }
  669. %endif SQLITE_OMIT_COMPOUND_SELECT
  670. %type inscollist_opt {IdList*}
  671. %destructor inscollist_opt {sqlite3IdListDelete(pParse->db, $$);}
  672. %type inscollist {IdList*}
  673. %destructor inscollist {sqlite3IdListDelete(pParse->db, $$);}
  674. inscollist_opt(A) ::= . {A = 0;}
  675. inscollist_opt(A) ::= LP inscollist(X) RP. {A = X;}
  676. inscollist(A) ::= inscollist(X) COMMA nm(Y).
  677. {A = sqlite3IdListAppend(pParse->db,X,&Y);}
  678. inscollist(A) ::= nm(Y).
  679. {A = sqlite3IdListAppend(pParse->db,0,&Y);}
  680. /////////////////////////// Expression Processing /////////////////////////////
  681. //
  682. %type expr {ExprSpan}
  683. %destructor expr {sqlite3ExprDelete(pParse->db, $$.pExpr);}
  684. %type term {ExprSpan}
  685. %destructor term {sqlite3ExprDelete(pParse->db, $$.pExpr);}
  686. %include {
  687. /* This is a utility routine used to set the ExprSpan.zStart and
  688. ** ExprSpan.zEnd values of pOut so that the span covers the complete
  689. ** range of text beginning with pStart and going to the end of pEnd.
  690. */
  691. static void spanSet(ExprSpan *pOut, Token *pStart, Token *pEnd){
  692. pOut->zStart = pStart->z;
  693. pOut->zEnd = &pEnd->z[pEnd->n];
  694. }
  695. /* Construct a new Expr object from a single identifier. Use the
  696. ** new Expr to populate pOut. Set the span of pOut to be the identifier
  697. ** that created the expression.
  698. */
  699. static void spanExpr(ExprSpan *pOut, Parse *pParse, int op, Token *pValue){
  700. pOut->pExpr = sqlite3PExpr(pParse, op, 0, 0, pValue);
  701. pOut->zStart = pValue->z;
  702. pOut->zEnd = &pValue->z[pValue->n];
  703. }
  704. }
  705. expr(A) ::= term(X). {A = X;}
  706. expr(A) ::= LP(B) expr(X) RP(E). {A.pExpr = X.pExpr; spanSet(&A,&B,&E);}
  707. term(A) ::= NULL(X). {spanExpr(&A, pParse, @X, &X);}
  708. expr(A) ::= id(X). {spanExpr(&A, pParse, TK_ID, &X);}
  709. expr(A) ::= JOIN_KW(X). {spanExpr(&A, pParse, TK_ID, &X);}
  710. expr(A) ::= nm(X) DOT nm(Y). {
  711. Expr *temp1 = sqlite3PExpr(pParse, TK_ID, 0, 0, &X);
  712. Expr *temp2 = sqlite3PExpr(pParse, TK_ID, 0, 0, &Y);
  713. A.pExpr = sqlite3PExpr(pParse, TK_DOT, temp1, temp2, 0);
  714. spanSet(&A,&X,&Y);
  715. }
  716. expr(A) ::= nm(X) DOT nm(Y) DOT nm(Z). {
  717. Expr *temp1 = sqlite3PExpr(pParse, TK_ID, 0, 0, &X);
  718. Expr *temp2 = sqlite3PExpr(pParse, TK_ID, 0, 0, &Y);
  719. Expr *temp3 = sqlite3PExpr(pParse, TK_ID, 0, 0, &Z);
  720. Expr *temp4 = sqlite3PExpr(pParse, TK_DOT, temp2, temp3, 0);
  721. A.pExpr = sqlite3PExpr(pParse, TK_DOT, temp1, temp4, 0);
  722. spanSet(&A,&X,&Z);
  723. }
  724. term(A) ::= INTEGER|FLOAT|BLOB(X). {spanExpr(&A, pParse, @X, &X);}
  725. term(A) ::= STRING(X). {spanExpr(&A, pParse, @X, &X);}
  726. expr(A) ::= REGISTER(X). {
  727. /* When doing a nested parse, one can include terms in an expression
  728. ** that look like this: #1 #2 ... These terms refer to registers
  729. ** in the virtual machine. #N is the N-th register. */
  730. if( pParse->nested==0 ){
  731. sqlite3ErrorMsg(pParse, "near \"%T\": syntax error", &X);
  732. A.pExpr = 0;
  733. }else{
  734. A.pExpr = sqlite3PExpr(pParse, TK_REGISTER, 0, 0, &X);
  735. if( A.pExpr ) sqlite3GetInt32(&X.z[1], &A.pExpr->iTable);
  736. }
  737. spanSet(&A, &X, &X);
  738. }
  739. expr(A) ::= VARIABLE(X). {
  740. spanExpr(&A, pParse, TK_VARIABLE, &X);
  741. sqlite3ExprAssignVarNumber(pParse, A.pExpr);
  742. spanSet(&A, &X, &X);
  743. }
  744. expr(A) ::= expr(E) COLLATE ids(C). {
  745. A.pExpr = sqlite3ExprAddCollateToken(pParse, E.pExpr, &C);
  746. A.zStart = E.zStart;
  747. A.zEnd = &C.z[C.n];
  748. }
  749. %ifndef SQLITE_OMIT_CAST
  750. expr(A) ::= CAST(X) LP expr(E) AS typetoken(T) RP(Y). {
  751. A.pExpr = sqlite3PExpr(pParse, TK_CAST, E.pExpr, 0, &T);
  752. spanSet(&A,&X,&Y);
  753. }
  754. %endif SQLITE_OMIT_CAST
  755. expr(A) ::= ID(X) LP distinct(D) exprlist(Y) RP(E). {
  756. if( Y && Y->nExpr>pParse->db->aLimit[SQLITE_LIMIT_FUNCTION_ARG] ){
  757. sqlite3ErrorMsg(pParse, "too many arguments on function %T", &X);
  758. }
  759. A.pExpr = sqlite3ExprFunction(pParse, Y, &X);
  760. spanSet(&A,&X,&E);
  761. if( D && A.pExpr ){
  762. A.pExpr->flags |= EP_Distinct;
  763. }
  764. }
  765. expr(A) ::= ID(X) LP STAR RP(E). {
  766. A.pExpr = sqlite3ExprFunction(pParse, 0, &X);
  767. spanSet(&A,&X,&E);
  768. }
  769. term(A) ::= CTIME_KW(OP). {
  770. /* The CURRENT_TIME, CURRENT_DATE, and CURRENT_TIMESTAMP values are
  771. ** treated as functions that return constants */
  772. A.pExpr = sqlite3ExprFunction(pParse, 0,&OP);
  773. if( A.pExpr ){
  774. A.pExpr->op = TK_CONST_FUNC;
  775. }
  776. spanSet(&A, &OP, &OP);
  777. }
  778. %include {
  779. /* This routine constructs a binary expression node out of two ExprSpan
  780. ** objects and uses the result to populate a new ExprSpan object.
  781. */
  782. static void spanBinaryExpr(
  783. ExprSpan *pOut, /* Write the result here */
  784. Parse *pParse, /* The parsing context. Errors accumulate here */
  785. int op, /* The binary operation */
  786. ExprSpan *pLeft, /* The left operand */
  787. ExprSpan *pRight /* The right operand */
  788. ){
  789. pOut->pExpr = sqlite3PExpr(pParse, op, pLeft->pExpr, pRight->pExpr, 0);
  790. pOut->zStart = pLeft->zStart;
  791. pOut->zEnd = pRight->zEnd;
  792. }
  793. }
  794. expr(A) ::= expr(X) AND(OP) expr(Y). {spanBinaryExpr(&A,pParse,@OP,&X,&Y);}
  795. expr(A) ::= expr(X) OR(OP) expr(Y). {spanBinaryExpr(&A,pParse,@OP,&X,&Y);}
  796. expr(A) ::= expr(X) LT|GT|GE|LE(OP) expr(Y).
  797. {spanBinaryExpr(&A,pParse,@OP,&X,&Y);}
  798. expr(A) ::= expr(X) EQ|NE(OP) expr(Y). {spanBinaryExpr(&A,pParse,@OP,&X,&Y);}
  799. expr(A) ::= expr(X) BITAND|BITOR|LSHIFT|RSHIFT(OP) expr(Y).
  800. {spanBinaryExpr(&A,pParse,@OP,&X,&Y);}
  801. expr(A) ::= expr(X) PLUS|MINUS(OP) expr(Y).
  802. {spanBinaryExpr(&A,pParse,@OP,&X,&Y);}
  803. expr(A) ::= expr(X) STAR|SLASH|REM(OP) expr(Y).
  804. {spanBinaryExpr(&A,pParse,@OP,&X,&Y);}
  805. expr(A) ::= expr(X) CONCAT(OP) expr(Y). {spanBinaryExpr(&A,pParse,@OP,&X,&Y);}
  806. %type likeop {struct LikeOp}
  807. likeop(A) ::= LIKE_KW(X). {A.eOperator = X; A.bNot = 0;}
  808. likeop(A) ::= NOT LIKE_KW(X). {A.eOperator = X; A.bNot = 1;}
  809. likeop(A) ::= MATCH(X). {A.eOperator = X; A.bNot = 0;}
  810. likeop(A) ::= NOT MATCH(X). {A.eOperator = X; A.bNot = 1;}
  811. expr(A) ::= expr(X) likeop(OP) expr(Y). [LIKE_KW] {
  812. ExprList *pList;
  813. pList = sqlite3ExprListAppend(pParse,0, Y.pExpr);
  814. pList = sqlite3ExprListAppend(pParse,pList, X.pExpr);
  815. A.pExpr = sqlite3ExprFunction(pParse, pList, &OP.eOperator);
  816. if( OP.bNot ) A.pExpr = sqlite3PExpr(pParse, TK_NOT, A.pExpr, 0, 0);
  817. A.zStart = X.zStart;
  818. A.zEnd = Y.zEnd;
  819. if( A.pExpr ) A.pExpr->flags |= EP_InfixFunc;
  820. }
  821. expr(A) ::= expr(X) likeop(OP) expr(Y) ESCAPE expr(E). [LIKE_KW] {
  822. ExprList *pList;
  823. pList = sqlite3ExprListAppend(pParse,0, Y.pExpr);
  824. pList = sqlite3ExprListAppend(pParse,pList, X.pExpr);
  825. pList = sqlite3ExprListAppend(pParse,pList, E.pExpr);
  826. A.pExpr = sqlite3ExprFunction(pParse, pList, &OP.eOperator);
  827. if( OP.bNot ) A.pExpr = sqlite3PExpr(pParse, TK_NOT, A.pExpr, 0, 0);
  828. A.zStart = X.zStart;
  829. A.zEnd = E.zEnd;
  830. if( A.pExpr ) A.pExpr->flags |= EP_InfixFunc;
  831. }
  832. %include {
  833. /* Construct an expression node for a unary postfix operator
  834. */
  835. static void spanUnaryPostfix(
  836. ExprSpan *pOut, /* Write the new expression node here */
  837. Parse *pParse, /* Parsing context to record errors */
  838. int op, /* The operator */
  839. ExprSpan *pOperand, /* The operand */
  840. Token *pPostOp /* The operand token for setting the span */
  841. ){
  842. pOut->pExpr = sqlite3PExpr(pParse, op, pOperand->pExpr, 0, 0);
  843. pOut->zStart = pOperand->zStart;
  844. pOut->zEnd = &pPostOp->z[pPostOp->n];
  845. }
  846. }
  847. expr(A) ::= expr(X) ISNULL|NOTNULL(E). {spanUnaryPostfix(&A,pParse,@E,&X,&E);}
  848. expr(A) ::= expr(X) NOT NULL(E). {spanUnaryPostfix(&A,pParse,TK_NOTNULL,&X,&E);}
  849. %include {
  850. /* A routine to convert a binary TK_IS or TK_ISNOT expression into a
  851. ** unary TK_ISNULL or TK_NOTNULL expression. */
  852. static void binaryToUnaryIfNull(Parse *pParse, Expr *pY, Expr *pA, int op){
  853. sqlite3 *db = pParse->db;
  854. if( db->mallocFailed==0 && pY->op==TK_NULL ){
  855. pA->op = (u8)op;
  856. sqlite3ExprDelete(db, pA->pRight);
  857. pA->pRight = 0;
  858. }
  859. }
  860. }
  861. // expr1 IS expr2
  862. // expr1 IS NOT expr2
  863. //
  864. // If expr2 is NULL then code as TK_ISNULL or TK_NOTNULL. If expr2
  865. // is any other expression, code as TK_IS or TK_ISNOT.
  866. //
  867. expr(A) ::= expr(X) IS expr(Y). {
  868. spanBinaryExpr(&A,pParse,TK_IS,&X,&Y);
  869. binaryToUnaryIfNull(pParse, Y.pExpr, A.pExpr, TK_ISNULL);
  870. }
  871. expr(A) ::= expr(X) IS NOT expr(Y). {
  872. spanBinaryExpr(&A,pParse,TK_ISNOT,&X,&Y);
  873. binaryToUnaryIfNull(pParse, Y.pExpr, A.pExpr, TK_NOTNULL);
  874. }
  875. %include {
  876. /* Construct an expression node for a unary prefix operator
  877. */
  878. static void spanUnaryPrefix(
  879. ExprSpan *pOut, /* Write the new expression node here */
  880. Parse *pParse, /* Parsing context to record errors */
  881. int op, /* The operator */
  882. ExprSpan *pOperand, /* The operand */
  883. Token *pPreOp /* The operand token for setting the span */
  884. ){
  885. pOut->pExpr = sqlite3PExpr(pParse, op, pOperand->pExpr, 0, 0);
  886. pOut->zStart = pPreOp->z;
  887. pOut->zEnd = pOperand->zEnd;
  888. }
  889. }
  890. expr(A) ::= NOT(B) expr(X). {spanUnaryPrefix(&A,pParse,@B,&X,&B);}
  891. expr(A) ::= BITNOT(B) expr(X). {spanUnaryPrefix(&A,pParse,@B,&X,&B);}
  892. expr(A) ::= MINUS(B) expr(X). [BITNOT]
  893. {spanUnaryPrefix(&A,pParse,TK_UMINUS,&X,&B);}
  894. expr(A) ::= PLUS(B) expr(X). [BITNOT]
  895. {spanUnaryPrefix(&A,pParse,TK_UPLUS,&X,&B);}
  896. %type between_op {int}
  897. between_op(A) ::= BETWEEN. {A = 0;}
  898. between_op(A) ::= NOT BETWEEN. {A = 1;}
  899. expr(A) ::= expr(W) between_op(N) expr(X) AND expr(Y). [BETWEEN] {
  900. ExprList *pList = sqlite3ExprListAppend(pParse,0, X.pExpr);
  901. pList = sqlite3ExprListAppend(pParse,pList, Y.pExpr);
  902. A.pExpr = sqlite3PExpr(pParse, TK_BETWEEN, W.pExpr, 0, 0);
  903. if( A.pExpr ){
  904. A.pExpr->x.pList = pList;
  905. }else{
  906. sqlite3ExprListDelete(pParse->db, pList);
  907. }
  908. if( N ) A.pExpr = sqlite3PExpr(pParse, TK_NOT, A.pExpr, 0, 0);
  909. A.zStart = W.zStart;
  910. A.zEnd = Y.zEnd;
  911. }
  912. %ifndef SQLITE_OMIT_SUBQUERY
  913. %type in_op {int}
  914. in_op(A) ::= IN. {A = 0;}
  915. in_op(A) ::= NOT IN. {A = 1;}
  916. expr(A) ::= expr(X) in_op(N) LP exprlist(Y) RP(E). [IN] {
  917. if( Y==0 ){
  918. /* Expressions of the form
  919. **
  920. ** expr1 IN ()
  921. ** expr1 NOT IN ()
  922. **
  923. ** simplify to constants 0 (false) and 1 (true), respectively,
  924. ** regardless of the value of expr1.
  925. */
  926. A.pExpr = sqlite3PExpr(pParse, TK_INTEGER, 0, 0, &sqlite3IntTokens[N]);
  927. sqlite3ExprDelete(pParse->db, X.pExpr);
  928. }else{
  929. A.pExpr = sqlite3PExpr(pParse, TK_IN, X.pExpr, 0, 0);
  930. if( A.pExpr ){
  931. A.pExpr->x.pList = Y;
  932. sqlite3ExprSetHeight(pParse, A.pExpr);
  933. }else{
  934. sqlite3ExprListDelete(pParse->db, Y);
  935. }
  936. if( N ) A.pExpr = sqlite3PExpr(pParse, TK_NOT, A.pExpr, 0, 0);
  937. }
  938. A.zStart = X.zStart;
  939. A.zEnd = &E.z[E.n];
  940. }
  941. expr(A) ::= LP(B) select(X) RP(E). {
  942. A.pExpr = sqlite3PExpr(pParse, TK_SELECT, 0, 0, 0);
  943. if( A.pExpr ){
  944. A.pExpr->x.pSelect = X;
  945. ExprSetProperty(A.pExpr, EP_xIsSelect);
  946. sqlite3ExprSetHeight(pParse, A.pExpr);
  947. }else{
  948. sqlite3SelectDelete(pParse->db, X);
  949. }
  950. A.zStart = B.z;
  951. A.zEnd = &E.z[E.n];
  952. }
  953. expr(A) ::= expr(X) in_op(N) LP select(Y) RP(E). [IN] {
  954. A.pExpr = sqlite3PExpr(pParse, TK_IN, X.pExpr, 0, 0);
  955. if( A.pExpr ){
  956. A.pExpr->x.pSelect = Y;
  957. ExprSetProperty(A.pExpr, EP_xIsSelect);
  958. sqlite3ExprSetHeight(pParse, A.pExpr);
  959. }else{
  960. sqlite3SelectDelete(pParse->db, Y);
  961. }
  962. if( N ) A.pExpr = sqlite3PExpr(pParse, TK_NOT, A.pExpr, 0, 0);
  963. A.zStart = X.zStart;
  964. A.zEnd = &E.z[E.n];
  965. }
  966. expr(A) ::= expr(X) in_op(N) nm(Y) dbnm(Z). [IN] {
  967. SrcList *pSrc = sqlite3SrcListAppend(pParse->db, 0,&Y,&Z);
  968. A.pExpr = sqlite3PExpr(pParse, TK_IN, X.pExpr, 0, 0);
  969. if( A.pExpr ){
  970. A.pExpr->x.pSelect = sqlite3SelectNew(pParse, 0,pSrc,0,0,0,0,0,0,0);
  971. ExprSetProperty(A.pExpr, EP_xIsSelect);
  972. sqlite3ExprSetHeight(pParse, A.pExpr);
  973. }else{
  974. sqlite3SrcListDelete(pParse->db, pSrc);
  975. }
  976. if( N ) A.pExpr = sqlite3PExpr(pParse, TK_NOT, A.pExpr, 0, 0);
  977. A.zStart = X.zStart;
  978. A.zEnd = Z.z ? &Z.z[Z.n] : &Y.z[Y.n];
  979. }
  980. expr(A) ::= EXISTS(B) LP select(Y) RP(E). {
  981. Expr *p = A.pExpr = sqlite3PExpr(pParse, TK_EXISTS, 0, 0, 0);
  982. if( p ){
  983. p->x.pSelect = Y;
  984. ExprSetProperty(p, EP_xIsSelect);
  985. sqlite3ExprSetHeight(pParse, p);
  986. }else{
  987. sqlite3SelectDelete(pParse->db, Y);
  988. }
  989. A.zStart = B.z;
  990. A.zEnd = &E.z[E.n];
  991. }
  992. %endif SQLITE_OMIT_SUBQUERY
  993. /* CASE expressions */
  994. expr(A) ::= CASE(C) case_operand(X) case_exprlist(Y) case_else(Z) END(E). {
  995. A.pExpr = sqlite3PExpr(pParse, TK_CASE, X, 0, 0);
  996. if( A.pExpr ){
  997. A.pExpr->x.pList = Z ? sqlite3ExprListAppend(pParse,Y,Z) : Y;
  998. sqlite3ExprSetHeight(pParse, A.pExpr);
  999. }else{
  1000. sqlite3ExprListDelete(pParse->db, Y);
  1001. sqlite3ExprDelete(pParse->db, Z);
  1002. }
  1003. A.zStart = C.z;
  1004. A.zEnd = &E.z[E.n];
  1005. }
  1006. %type case_exprlist {ExprList*}
  1007. %destructor case_exprlist {sqlite3ExprListDelete(pParse->db, $$);}
  1008. case_exprlist(A) ::= case_exprlist(X) WHEN expr(Y) THEN expr(Z). {
  1009. A = sqlite3ExprListAppend(pParse,X, Y.pExpr);
  1010. A = sqlite3ExprListAppend(pParse,A, Z.pExpr);
  1011. }
  1012. case_exprlist(A) ::= WHEN expr(Y) THEN expr(Z). {
  1013. A = sqlite3ExprListAppend(pParse,0, Y.pExpr);
  1014. A = sqlite3ExprListAppend(pParse,A, Z.pExpr);
  1015. }
  1016. %type case_else {Expr*}
  1017. %destructor case_else {sqlite3ExprDelete(pParse->db, $$);}
  1018. case_else(A) ::= ELSE expr(X). {A = X.pExpr;}
  1019. case_else(A) ::= . {A = 0;}
  1020. %type case_operand {Expr*}
  1021. %destructor case_operand {sqlite3ExprDelete(pParse->db, $$);}
  1022. case_operand(A) ::= expr(X). {A = X.pExpr;}
  1023. case_operand(A) ::= . {A = 0;}
  1024. %type exprlist {ExprList*}
  1025. %destructor exprlist {sqlite3ExprListDelete(pParse->db, $$);}
  1026. %type nexprlist {ExprList*}
  1027. %destructor nexprlist {sqlite3ExprListDelete(pParse->db, $$);}
  1028. exprlist(A) ::= nexprlist(X). {A = X;}
  1029. exprlist(A) ::= . {A = 0;}
  1030. nexprlist(A) ::= nexprlist(X) COMMA expr(Y).
  1031. {A = sqlite3ExprListAppend(pParse,X,Y.pExpr);}
  1032. nexprlist(A) ::= expr(Y).
  1033. {A = sqlite3ExprListAppend(pParse,0,Y.pExpr);}
  1034. ///////////////////////////// The CREATE INDEX command ///////////////////////
  1035. //
  1036. cmd ::= createkw(S) uniqueflag(U) INDEX ifnotexists(NE) nm(X) dbnm(D)
  1037. ON nm(Y) LP idxlist(Z) RP where_opt(W). {
  1038. sqlite3CreateIndex(pParse, &X, &D,
  1039. sqlite3SrcListAppend(pParse->db,0,&Y,0), Z, U,
  1040. &S, W, SQLITE_SO_ASC, NE);
  1041. }
  1042. %type uniqueflag {int}
  1043. uniqueflag(A) ::= UNIQUE. {A = OE_Abort;}
  1044. uniqueflag(A) ::= . {A = OE_None;}
  1045. %type idxlist {ExprList*}
  1046. %destructor idxlist {sqlite3ExprListDelete(pParse->db, $$);}
  1047. %type idxlist_opt {ExprList*}
  1048. %destructor idxlist_opt {sqlite3ExprListDelete(pParse->db, $$);}
  1049. idxlist_opt(A) ::= . {A = 0;}
  1050. idxlist_opt(A) ::= LP idxlist(X) RP. {A = X;}
  1051. idxlist(A) ::= idxlist(X) COMMA nm(Y) collate(C) sortorder(Z). {
  1052. Expr *p = sqlite3ExprAddCollateToken(pParse, 0, &C);
  1053. A = sqlite3ExprListAppend(pParse,X, p);
  1054. sqlite3ExprListSetName(pParse,A,&Y,1);
  1055. sqlite3ExprListCheckLength(pParse, A, "index");
  1056. if( A ) A->a[A->nExpr-1].sortOrder = (u8)Z;
  1057. }
  1058. idxlist(A) ::= nm(Y) collate(C) sortorder(Z). {
  1059. Expr *p = sqlite3ExprAddCollateToken(pParse, 0, &C);
  1060. A = sqlite3ExprListAppend(pParse,0, p);
  1061. sqlite3ExprListSetName(pParse, A, &Y, 1);
  1062. sqlite3ExprListCheckLength(pParse, A, "index");
  1063. if( A ) A->a[A->nExpr-1].sortOrder = (u8)Z;
  1064. }
  1065. %type collate {Token}
  1066. collate(C) ::= . {C.z = 0; C.n = 0;}
  1067. collate(C) ::= COLLATE ids(X). {C = X;}
  1068. ///////////////////////////// The DROP INDEX command /////////////////////////
  1069. //
  1070. cmd ::= DROP INDEX ifexists(E) fullname(X). {sqlite3DropIndex(pParse, X, E);}
  1071. ///////////////////////////// The VACUUM command /////////////////////////////
  1072. //
  1073. %ifndef SQLITE_OMIT_VACUUM
  1074. %ifndef SQLITE_OMIT_ATTACH
  1075. cmd ::= VACUUM. {sqlite3Vacuum(pParse);}
  1076. cmd ::= VACUUM nm. {sqlite3Vacuum(pParse);}
  1077. %endif SQLITE_OMIT_ATTACH
  1078. %endif SQLITE_OMIT_VACUUM
  1079. ///////////////////////////// The PRAGMA command /////////////////////////////
  1080. //
  1081. %ifndef SQLITE_OMIT_PRAGMA
  1082. cmd ::= PRAGMA nm(X) dbnm(Z). {sqlite3Pragma(pParse,&X,&Z,0,0);}
  1083. cmd ::= PRAGMA nm(X) dbnm(Z) EQ nmnum(Y). {sqlite3Pragma(pParse,&X,&Z,&Y,0);}
  1084. cmd ::= PRAGMA nm(X) dbnm(Z) LP nmnum(Y) RP. {sqlite3Pragma(pParse,&X,&Z,&Y,0);}
  1085. cmd ::= PRAGMA nm(X) dbnm(Z) EQ minus_num(Y).
  1086. {sqlite3Pragma(pParse,&X,&Z,&Y,1);}
  1087. cmd ::= PRAGMA nm(X) dbnm(Z) LP minus_num(Y) RP.
  1088. {sqlite3Pragma(pParse,&X,&Z,&Y,1);}
  1089. nmnum(A) ::= plus_num(X). {A = X;}
  1090. nmnum(A) ::= nm(X). {A = X;}
  1091. nmnum(A) ::= ON(X). {A = X;}
  1092. nmnum(A) ::= DELETE(X). {A = X;}
  1093. nmnum(A) ::= DEFAULT(X). {A = X;}
  1094. %endif SQLITE_OMIT_PRAGMA
  1095. plus_num(A) ::= PLUS number(X). {A = X;}
  1096. plus_num(A) ::= number(X). {A = X;}
  1097. minus_num(A) ::= MINUS number(X). {A = X;}
  1098. number(A) ::= INTEGER|FLOAT(X). {A = X;}
  1099. //////////////////////////// The CREATE TRIGGER command /////////////////////
  1100. %ifndef SQLITE_OMIT_TRIGGER
  1101. cmd ::= createkw trigger_decl(A) BEGIN trigger_cmd_list(S) END(Z). {
  1102. Token all;
  1103. all.z = A.z;
  1104. all.n = (int)(Z.z - A.z) + Z.n;
  1105. sqlite3FinishTrigger(pParse, S, &all);
  1106. }
  1107. trigger_decl(A) ::= temp(T) TRIGGER ifnotexists(NOERR) nm(B) dbnm(Z)
  1108. trigger_time(C) trigger_event(D)
  1109. ON fullname(E) foreach_clause when_clause(G). {
  1110. sqlite3BeginTrigger(pParse, &B, &Z, C, D.a, D.b, E, G, T, NOERR);
  1111. A = (Z.n==0?B:Z);
  1112. }
  1113. %type trigger_time {int}
  1114. trigger_time(A) ::= BEFORE. { A = TK_BEFORE; }
  1115. trigger_time(A) ::= AFTER. { A = TK_AFTER; }
  1116. trigger_time(A) ::= INSTEAD OF. { A = TK_INSTEAD;}
  1117. trigger_time(A) ::= . { A = TK_BEFORE; }
  1118. %type trigger_event {struct TrigEvent}
  1119. %destructor trigger_event {sqlite3IdListDelete(pParse->db, $$.b);}
  1120. trigger_event(A) ::= DELETE|INSERT(OP). {A.a = @OP; A.b = 0;}
  1121. trigger_event(A) ::= UPDATE(OP). {A.a = @OP; A.b = 0;}
  1122. trigger_event(A) ::= UPDATE OF inscollist(X). {A.a = TK_UPDATE; A.b = X;}
  1123. foreach_clause ::= .
  1124. foreach_clause ::= FOR EACH ROW.
  1125. %type when_clause {Expr*}
  1126. %destructor when_clause {sqlite3ExprDelete(pParse->db, $$);}
  1127. when_clause(A) ::= . { A = 0; }
  1128. when_clause(A) ::= WHEN expr(X). { A = X.pExpr; }
  1129. %type trigger_cmd_list {TriggerStep*}
  1130. %destructor trigger_cmd_list {sqlite3DeleteTriggerStep(pParse->db, $$);}
  1131. trigger_cmd_list(A) ::= trigger_cmd_list(Y) trigger_cmd(X) SEMI. {
  1132. assert( Y!=0 );
  1133. Y->pLast->pNext = X;
  1134. Y->pLast = X;
  1135. A = Y;
  1136. }
  1137. trigger_cmd_list(A) ::= trigger_cmd(X) SEMI. {
  1138. assert( X!=0 );
  1139. X->pLast = X;
  1140. A = X;
  1141. }
  1142. // Disallow qualified table names on INSERT, UPDATE, and DELETE statements
  1143. // within a trigger. The table to INSERT, UPDATE, or DELETE is always in
  1144. // the same database as the table that the trigger fires on.
  1145. //
  1146. %type trnm {Token}
  1147. trnm(A) ::= nm(X). {A = X;}
  1148. trnm(A) ::= nm DOT nm(X). {
  1149. A = X;
  1150. sqlite3ErrorMsg(pParse,
  1151. "qualified table names are not allowed on INSERT, UPDATE, and DELETE "
  1152. "statements within triggers");
  1153. }
  1154. // Disallow the INDEX BY and NOT INDEXED clauses on UPDATE and DELETE
  1155. // statements within triggers. We make a specific error message for this
  1156. // since it is an exception to the default grammar rules.
  1157. //
  1158. tridxby ::= .
  1159. tridxby ::= INDEXED BY nm. {
  1160. sqlite3ErrorMsg(pParse,
  1161. "the INDEXED BY clause is not allowed on UPDATE or DELETE statements "
  1162. "within triggers");
  1163. }
  1164. tridxby ::= NOT INDEXED. {
  1165. sqlite3ErrorMsg(pParse,
  1166. "the NOT INDEXED clause is not allowed on UPDATE or DELETE statements "
  1167. "within triggers");
  1168. }
  1169. %type trigger_cmd {TriggerStep*}
  1170. %destructor trigger_cmd {sqlite3DeleteTriggerStep(pParse->db, $$);}
  1171. // UPDATE
  1172. trigger_cmd(A) ::=
  1173. UPDATE orconf(R) trnm(X) tridxby SET setlist(Y) where_opt(Z).
  1174. { A = sqlite3TriggerUpdateStep(pParse->db, &X, Y, Z, R); }
  1175. // INSERT
  1176. trigger_cmd(A) ::=
  1177. insert_cmd(R) INTO trnm(X) inscollist_opt(F) valuelist(Y).
  1178. {A = sqlite3TriggerInsertStep(pParse->db, &X, F, Y.pList, Y.pSelect, R);}
  1179. trigger_cmd(A) ::= insert_cmd(R) INTO trnm(X) inscollist_opt(F) select(S).
  1180. {A = sqlite3TriggerInsertStep(pParse->db, &X, F, 0, S, R);}
  1181. // DELETE
  1182. trigger_cmd(A) ::= DELETE FROM trnm(X) tridxby where_opt(Y).
  1183. {A = sqlite3TriggerDeleteStep(pParse->db, &X, Y);}
  1184. // SELECT
  1185. trigger_cmd(A) ::= select(X). {A = sqlite3TriggerSelectStep(pParse->db, X); }
  1186. // The special RAISE expression that may occur in trigger programs
  1187. expr(A) ::= RAISE(X) LP IGNORE RP(Y). {
  1188. A.pExpr = sqlite3PExpr(pParse, TK_RAISE, 0, 0, 0);
  1189. if( A.pExpr ){
  1190. A.pExpr->affinity = OE_Ignore;
  1191. }
  1192. A.zStart = X.z;
  1193. A.zEnd = &Y.z[Y.n];
  1194. }
  1195. expr(A) ::= RAISE(X) LP raisetype(T) COMMA nm(Z) RP(Y). {
  1196. A.pExpr = sqlite3PExpr(pParse, TK_RAISE, 0, 0, &Z);
  1197. if( A.pExpr ) {
  1198. A.pExpr->affinity = (char)T;
  1199. }
  1200. A.zStart = X.z;
  1201. A.zEnd = &Y.z[Y.n];
  1202. }
  1203. %endif !SQLITE_OMIT_TRIGGER
  1204. %type raisetype {int}
  1205. raisetype(A) ::= ROLLBACK. {A = OE_Rollback;}
  1206. raisetype(A) ::= ABORT. {A = OE_Abort;}
  1207. raisetype(A) ::= FAIL. {A = OE_Fail;}
  1208. //////////////////////// DROP TRIGGER statement //////////////////////////////
  1209. %ifndef SQLITE_OMIT_TRIGGER
  1210. cmd ::= DROP TRIGGER ifexists(NOERR) fullname(X). {
  1211. sqlite3DropTrigger(pParse,X,NOERR);
  1212. }
  1213. %endif !SQLITE_OMIT_TRIGGER
  1214. //////////////////////// ATTACH DATABASE file AS name /////////////////////////
  1215. %ifndef SQLITE_OMIT_ATTACH
  1216. cmd ::= ATTACH database_kw_opt expr(F) AS expr(D) key_opt(K). {
  1217. sqlite3Attach(pParse, F.pExpr, D.pExpr, K);
  1218. }
  1219. cmd ::= DETACH database_kw_opt expr(D). {
  1220. sqlite3Detach(pParse, D.pExpr);
  1221. }
  1222. %type key_opt {Expr*}
  1223. %destructor key_opt {sqlite3ExprDelete(pParse->db, $$);}
  1224. key_opt(A) ::= . { A = 0; }
  1225. key_opt(A) ::= KEY expr(X). { A = X.pExpr; }
  1226. database_kw_opt ::= DATABASE.
  1227. database_kw_opt ::= .
  1228. %endif SQLITE_OMIT_ATTACH
  1229. ////////////////////////// REINDEX collation //////////////////////////////////
  1230. %ifndef SQLITE_OMIT_REINDEX
  1231. cmd ::= REINDEX. {sqlite3Reindex(pParse, 0, 0);}
  1232. cmd ::= REINDEX nm(X) dbnm(Y). {sqlite3Reindex(pParse, &X, &Y);}
  1233. %endif SQLITE_OMIT_REINDEX
  1234. /////////////////////////////////// ANALYZE ///////////////////////////////////
  1235. %ifndef SQLITE_OMIT_ANALYZE
  1236. cmd ::= ANALYZE. {sqlite3Analyze(pParse, 0, 0);}
  1237. cmd ::= ANALYZE nm(X) dbnm(Y). {sqlite3Analyze(pParse, &X, &Y);}
  1238. %endif
  1239. //////////////////////// ALTER TABLE table ... ////////////////////////////////
  1240. %ifndef SQLITE_OMIT_ALTERTABLE
  1241. cmd ::= ALTER TABLE fullname(X) RENAME TO nm(Z). {
  1242. sqlite3AlterRenameTable(pParse,X,&Z);
  1243. }
  1244. cmd ::= ALTER TABLE add_column_fullname ADD kwcolumn_opt column(Y). {
  1245. sqlite3AlterFinishAddColumn(pParse, &Y);
  1246. }
  1247. add_column_fullname ::= fullname(X). {
  1248. pParse->db->lookaside.bEnabled = 0;
  1249. sqlite3AlterBeginAddColumn(pParse, X);
  1250. }
  1251. kwcolumn_opt ::= .
  1252. kwcolumn_opt ::= COLUMNKW.
  1253. %endif SQLITE_OMIT_ALTERTABLE
  1254. //////////////////////// CREATE VIRTUAL TABLE ... /////////////////////////////
  1255. %ifndef SQLITE_OMIT_VIRTUALTABLE
  1256. cmd ::= create_vtab. {sqlite3VtabFinishParse(pParse,0);}
  1257. cmd ::= create_vtab LP vtabarglist RP(X). {sqlite3VtabFinishParse(pParse,&X);}
  1258. create_vtab ::= createkw VIRTUAL TABLE ifnotexists(E)
  1259. nm(X) dbnm(Y) USING nm(Z). {
  1260. sqlite3VtabBeginParse(pParse, &X, &Y, &Z, E);
  1261. }
  1262. vtabarglist ::= vtabarg.
  1263. vtabarglist ::= vtabarglist COMMA vtabarg.
  1264. vtabarg ::= . {sqlite3VtabArgInit(pParse);}
  1265. vtabarg ::= vtabarg vtabargtoken.
  1266. vtabargtoken ::= ANY(X). {sqlite3VtabArgExtend(pParse,&X);}
  1267. vtabargtoken ::= lp anylist RP(X). {sqlite3VtabArgExtend(pParse,&X);}
  1268. lp ::= LP(X). {sqlite3VtabArgExtend(pParse,&X);}
  1269. anylist ::= .
  1270. anylist ::= anylist LP anylist RP.
  1271. anylist ::= anylist ANY.
  1272. %endif SQLITE_OMIT_VIRTUALTABLE