Grammar rules for a hypothetical FOR EACH ROW statement. No new keywords
required. However, the sqlite3_complete() machine had to be reworked because
of the fact that FOR EACH ROW can contain internal semicolon tokens.
FossilOrigin-Name: 32ae9ee7129a9f0779accef8cad499ebf044eebf08a0ecfe4cec41e5dcdd15a9
diff --git a/manifest b/manifest
index 9e29dea..50f6430 100644
--- a/manifest
+++ b/manifest
@@ -1,5 +1,5 @@
-C Fix\stwo\sunreachable\sbranches\sintroduced\sby\sthe\srecent\nsqlite3TriggersExist()\soptimization.
-D 2022-04-07T20:45:38.697
+C Grammar\srules\sfor\sa\shypothetical\sFOR\sEACH\sROW\sstatement.\s\sNo\snew\skeywords\nrequired.\s\sHowever,\sthe\ssqlite3_complete()\smachine\shad\sto\sbe\sreworked\sbecause\nof\sthe\sfact\sthat\sFOR\sEACH\sROW\scan\scontain\sinternal\ssemicolon\stokens.
+D 2022-04-08T12:50:31.318
F .fossil-settings/empty-dirs dbb81e8fc0401ac46a1491ab34a7f2c7c0452f2f06b54ebb845d024ca8283ef1
F .fossil-settings/ignore-glob 35175cdfcf539b2318cb04a9901442804be81cd677d8b889fcc9149c21f239ea
F LICENSE.md df5091916dbb40e6e9686186587125e1b2ff51f022cc334e886c19a0e9982724
@@ -497,7 +497,7 @@
F src/btreeInt.h 8ce1332edd89dfd2461d561ac10a0ab5601c8e06200cb5230596c3caaf54482e
F src/build.c ff119be98394a65bc8be7afc39d4a791a66f03a778d396de3ec456f5dfaf39e8
F src/callback.c 4c19af69835787bfe790ac560f3071a824eb629f34e41f97b52ce5235c77de1c
-F src/complete.c a3634ab1e687055cd002e11b8f43eb75c17da23e
+F src/complete.c 5d1146ae4ac8ced756be3e1e93e63eec3e6e3fa3ac92f6c52ffb2b694e153347
F src/ctime.c 026dbdcdbd8c3cde98a88483ee88310ff43150ab164ad768f12cc700a11495ad
F src/date.c 15082566229d4b1e5f24fdb490bf9bcc68824b911d70e3573ef075a1b9e2d26f
F src/dbpage.c a70be9a4879ac5392673a1050d526a72b8b2f9938df7049f65348566a2637db3
@@ -541,7 +541,7 @@
F src/os_win.h 7b073010f1451abe501be30d12f6bc599824944a
F src/pager.c 42120492784fc9bcd9082b5c9b5e329b7318c357f9f3574a1bbfcf7418910356
F src/pager.h f82e9844166e1585f5786837ddc7709966138ced17f568c16af7ccf946c2baa3
-F src/parse.y 9130a936927658e7c574e97e5d4f0a747a3f6b9a42bc5c63e455c8e40cc74216
+F src/parse.y bbb1709a4c5245c224f8451cab2a27604833f9c75eac237991d5da1e76b24ecc
F src/pcache.c 084e638432c610f95aea72b8509f0845d2791293f39d1b82f0c0a7e089c3bb6b
F src/pcache.h 4f87acd914cef5016fae3030343540d75f5b85a1877eed1a2a19b9f284248586
F src/pcache1.c 54881292a9a5db202b2c0ac541c5e3ef9a5e8c4f1c1383adb2601d5499a60e65
@@ -1945,8 +1945,11 @@
F vsixtest/vsixtest.vcxproj.data 2ed517e100c66dc455b492e1a33350c1b20fbcdc
F vsixtest/vsixtest.vcxproj.filters 37e51ffedcdb064aad6ff33b6148725226cd608e
F vsixtest/vsixtest_TemporaryKey.pfx e5b1b036facdb453873e7084e1cae9102ccc67a0
-P c2965fea9df7076b235d3eadaf84f0a36242476d0329030b0e57557b66e2540c
-R 07d0b0c26e9cd2798bbb00ddbafc6a3b
+P 1b5475d212cf9de0bff69eee8c607b4fcd8e04bf4df72171429e7609c4153951
+R f561994920f732a9d1a57dcea0b115a2
+T *branch * foreachrow-statement
+T *sym-foreachrow-statement *
+T -sym-trunk *
U drh
-Z 93237070ff101cf9292ceb1ee884c28a
+Z 4231091c94d4a7d97bb7ff93181450a2
# Remove this line to create a well-formed Fossil manifest.
diff --git a/manifest.uuid b/manifest.uuid
index 55edbcf..4c28fa2 100644
--- a/manifest.uuid
+++ b/manifest.uuid
@@ -1 +1 @@
-1b5475d212cf9de0bff69eee8c607b4fcd8e04bf4df72171429e7609c4153951
\ No newline at end of file
+32ae9ee7129a9f0779accef8cad499ebf044eebf08a0ecfe4cec41e5dcdd15a9
\ No newline at end of file
diff --git a/src/complete.c b/src/complete.c
index bb2c030..2474921 100644
--- a/src/complete.c
+++ b/src/complete.c
@@ -37,16 +37,17 @@
** Token types used by the sqlite3_complete() routine. See the header
** comments on that procedure for additional information.
*/
-#define tkSEMI 0
-#define tkWS 1
-#define tkOTHER 2
-#ifndef SQLITE_OMIT_TRIGGER
-#define tkEXPLAIN 3
-#define tkCREATE 4
-#define tkTEMP 5
-#define tkTRIGGER 6
-#define tkEND 7
-#endif
+#define tkSEMI 0 /* ; */
+#define tkWS 1 /* whitespace */
+#define tkOTHER 2 /* other puntuation, literal-value, or quoted ID */
+#define tkEXPLAIN 3 /* EXPLAIN keyword */
+#define tkCREATE 4 /* CREATE keyword */
+#define tkTRIGGER 5 /* TRIGGER keyword */
+#define tkEND 6 /* END keyword */
+#define tkFOR 7 /* FOR keyword */
+#define tkEACH 8 /* EACH keyword */
+#define tkROW 9 /* ROW keyword */
+#define tkKYWD 10 /* Any other keyword */
/*
** Return TRUE if the given SQL string ends in a semicolon.
@@ -57,8 +58,6 @@
**
** This implementation uses a state machine with 8 states:
**
-** (0) INVALID We have not yet seen a non-whitespace character.
-**
** (1) START At the beginning or end of an SQL statement. This routine
** returns 1 if it ends in the START state and 0 if it ends
** in any other state.
@@ -66,33 +65,45 @@
** (2) NORMAL We are in the middle of statement which ends with a single
** semicolon.
**
-** (3) EXPLAIN The keyword EXPLAIN has been seen at the beginning of
+** (2) EXPLAIN The keyword EXPLAIN has been seen at the beginning of
** a statement.
**
-** (4) CREATE The keyword CREATE has been seen at the beginning of a
+** (3) CREATE The keyword CREATE has been seen at the beginning of a
** statement, possibly preceded by EXPLAIN and/or followed by
** TEMP or TEMPORARY
**
-** (5) TRIGGER We are in the middle of a trigger definition that must be
-** ended by a semicolon, the keyword END, and another semicolon.
+** (4) TRIGGER We are in the middle of a trigger definition or FOR EACH ROW
+** statement that must be ended by ";END:"
**
-** (6) SEMI We've seen the first semicolon in the ";END;" that occurs at
+** (5) SEMI We've seen the first semicolon in the ";END;" that occurs at
** the end of a trigger definition.
**
-** (7) END We've seen the ";END" of the ";END;" that occurs at the end
+** (6) END We've seen the ";END" of the ";END;" that occurs at the end
** of a trigger definition.
**
+** (7) FOR We've seen the "FOR" keyword near the beginning of a
+** statement. Awaiting a following "EACH".
+**
+** (8) EACH We've seen keywrods "FOR" and "EACH" near the beginning of
+** of a statement and are awaiting a "ROW" token.
+**
+** (9) INIT No non-whitespace input seen so far.
+**
+**
** Transitions between states above are determined by tokens extracted
** from the input. The following tokens are significant:
**
** (0) tkSEMI A semicolon.
** (1) tkWS Whitespace.
-** (2) tkOTHER Any other SQL token.
+** (2) tkOTHER Any other token.
** (3) tkEXPLAIN The "explain" keyword.
** (4) tkCREATE The "create" keyword.
-** (5) tkTEMP The "temp" or "temporary" keyword.
-** (6) tkTRIGGER The "trigger" keyword.
-** (7) tkEND The "end" keyword.
+** (5) tkTRIGGER The "trigger" keyword.
+** (6) tkEND The "end" keyword.
+** (7) tkFOR The "for" keyword.
+** (8) tkEACH The "each" keyword.
+** (9) tkROW The "row" keyword.
+** (10) tkKYWD Any other keyword-like token.
**
** Whitespace never causes a state transition and is always ignored.
** This means that a SQL string of all whitespace is invalid.
@@ -102,37 +113,28 @@
** is look for a semicolon that is not part of an string or comment.
*/
int sqlite3_complete(const char *zSql){
- u8 state = 0; /* Current state, using numbers defined in header comment */
+ u8 state = 9; /* Current state, using numbers defined in header comment */
u8 token; /* Value of the next token */
-#ifndef SQLITE_OMIT_TRIGGER
/* A complex statement machine used to detect the end of a CREATE TRIGGER
** statement. This is the normal case.
*/
- static const u8 trans[8][8] = {
- /* Token: */
- /* State: ** SEMI WS OTHER EXPLAIN CREATE TEMP TRIGGER END */
- /* 0 INVALID: */ { 1, 0, 2, 3, 4, 2, 2, 2, },
- /* 1 START: */ { 1, 1, 2, 3, 4, 2, 2, 2, },
- /* 2 NORMAL: */ { 1, 2, 2, 2, 2, 2, 2, 2, },
- /* 3 EXPLAIN: */ { 1, 3, 3, 2, 4, 2, 2, 2, },
- /* 4 CREATE: */ { 1, 4, 2, 2, 2, 4, 5, 2, },
- /* 5 TRIGGER: */ { 6, 5, 5, 5, 5, 5, 5, 5, },
- /* 6 SEMI: */ { 6, 6, 5, 5, 5, 5, 5, 7, },
- /* 7 END: */ { 1, 7, 5, 5, 5, 5, 5, 5, },
+ static const u8 trans[10][11] = {
+ /* Token: */
+ /* 0 1 2 3 4 5 6 7 8 9 10 */
+/* State: ** SEMI WS OTHER EXPLN CREATE TRIGR END FOR EACH ROW KYWD */
+/* 0 START: */{ 0, 0, 1, 2, 3, 1, 1, 7, 1, 1, 1 },
+/* 1 NORMAL: */{ 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
+/* 2 EXPLAIN: */{ 0, 2, 1, 1, 3, 1, 1, 7, 1, 1, 2 },
+/* 3 CREATE: */{ 0, 3, 1, 1, 1, 4, 1, 1, 1, 1, 3 },
+/* 4 TRIGGER: */{ 5, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4 },
+/* 5 SEMI: */{ 5, 5, 4, 4, 4, 4, 6, 4, 4, 4, 4 },
+/* 6 END: */{ 0, 6, 4, 4, 4, 4, 4, 4, 4, 4, 4 },
+/* 7 FOR: */{ 0, 7, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
+/* 8 EACH: */{ 0, 8, 1, 1, 1, 1, 1, 1, 1, 4, 1 },
+/* 9 INIT: */{ 0, 9, 1, 2, 3, 1, 1, 7, 1, 1, 1 }
};
-#else
- /* If triggers are not supported by this compile then the statement machine
- ** used to detect the end of a statement is much simpler
- */
- static const u8 trans[3][3] = {
- /* Token: */
- /* State: ** SEMI WS OTHER */
- /* 0 INVALID: */ { 1, 0, 2, },
- /* 1 START: */ { 1, 1, 2, },
- /* 2 NORMAL: */ { 1, 2, 2, },
- };
-#endif /* SQLITE_OMIT_TRIGGER */
+
#ifdef SQLITE_ENABLE_API_ARMOR
if( zSql==0 ){
@@ -202,27 +204,12 @@
/* Keywords and unquoted identifiers */
int nId;
for(nId=1; IdChar(zSql[nId]); nId++){}
-#ifdef SQLITE_OMIT_TRIGGER
- token = tkOTHER;
-#else
switch( *zSql ){
case 'c': case 'C': {
if( nId==6 && sqlite3StrNICmp(zSql, "create", 6)==0 ){
token = tkCREATE;
}else{
- token = tkOTHER;
- }
- break;
- }
- case 't': case 'T': {
- if( nId==7 && sqlite3StrNICmp(zSql, "trigger", 7)==0 ){
- token = tkTRIGGER;
- }else if( nId==4 && sqlite3StrNICmp(zSql, "temp", 4)==0 ){
- token = tkTEMP;
- }else if( nId==9 && sqlite3StrNICmp(zSql, "temporary", 9)==0 ){
- token = tkTEMP;
- }else{
- token = tkOTHER;
+ token = tkKYWD;
}
break;
}
@@ -230,22 +217,45 @@
if( nId==3 && sqlite3StrNICmp(zSql, "end", 3)==0 ){
token = tkEND;
}else
-#ifndef SQLITE_OMIT_EXPLAIN
if( nId==7 && sqlite3StrNICmp(zSql, "explain", 7)==0 ){
token = tkEXPLAIN;
}else
-#endif
- {
- token = tkOTHER;
+ if( nId==4 && sqlite3StrNICmp(zSql, "each", 4)==0 ){
+ token = tkEACH;
+ }else{
+ token = tkKYWD;
+ }
+ break;
+ }
+ case 'f': case 'F': {
+ if( nId==3 && sqlite3StrNICmp(zSql, "for", 3)==0 ){
+ token = tkFOR;
+ }else{
+ token = tkKYWD;
+ }
+ break;
+ }
+ case 'r': case 'R': {
+ if( nId==3 && sqlite3StrNICmp(zSql, "row", 3)==0 ){
+ token = tkROW;
+ }else{
+ token = tkKYWD;
+ }
+ break;
+ }
+ case 't': case 'T': {
+ if( nId==7 && sqlite3StrNICmp(zSql, "trigger", 7)==0 ){
+ token = tkTRIGGER;
+ }else{
+ token = tkKYWD;
}
break;
}
default: {
- token = tkOTHER;
+ token = tkKYWD;
break;
}
}
-#endif /* SQLITE_OMIT_TRIGGER */
zSql += nId-1;
}else{
/* Operators and special symbols */
@@ -254,10 +264,13 @@
break;
}
}
+#if 0
+ printf("COMPLETER: %d.%d -> %d\n", state, token, trans[state][token]);
+#endif
state = trans[state][token];
zSql++;
}
- return state==1;
+ return state==0;
}
#ifndef SQLITE_OMIT_UTF16
diff --git a/src/parse.y b/src/parse.y
index 4deae14..42d1e12 100644
--- a/src/parse.y
+++ b/src/parse.y
@@ -1852,6 +1852,28 @@
filter_clause(A) ::= FILTER LP WHERE expr(X) RP. { A = X; }
%endif /* SQLITE_OMIT_WINDOWFUNC */
+//////////////////////// The FOR EACH ROW Command ///////////////////////////////////
+//
+%ifndef SQLITE_OMIT_FOREACHROW
+cmd ::= foreachcmd.
+foreachcmd ::= FOR EACH ROW IN select fewhenlist END.
+foreachcmd ::= FOR EACH ROW IN select DO feactionlist END.
+fewhenlist ::= fewhen.
+fewhenlist ::= fewhenlist fewhen.
+fewhen ::= WHEN expr THEN feactionlist.
+fewhen ::= WHEN expr THEN DO NOTHING.
+fewhen ::= ELSE feactionlist.
+feactionlist ::= feaction SEMI.
+feactionlist ::= feactionlist feaction SEMI.
+feaction ::= insert_cmd INTO xfullname idlist_opt select upsert.
+feaction ::= insert_cmd INTO xfullname idlist_opt DEFAULT VALUES.
+feaction ::= UPDATE orconf xfullname indexed_opt SET setlist from where_opt upsert.
+feaction ::= DELETE FROM xfullname indexed_opt where_opt.
+feaction ::= select.
+feaction ::= .
+%endif
+
+
/*
** The code generator needs some extra TK_ token values for tokens that
** are synthesized and do not actually appear in the grammar: