drh | 60c71b0 | 2020-09-01 11:20:03 +0000 | [diff] [blame] | 1 | %include { |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 2 | /* |
drh | 60c71b0 | 2020-09-01 11:20:03 +0000 | [diff] [blame] | 3 | ** 2001-09-15 |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 4 | ** |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 5 | ** The author disclaims copyright to this source code. In place of |
| 6 | ** a legal notice, here is a blessing: |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 7 | ** |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 8 | ** May you do good and not evil. |
| 9 | ** May you find forgiveness for yourself and forgive others. |
| 10 | ** May you share freely, never taking more than you give. |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 11 | ** |
| 12 | ************************************************************************* |
drh | 60c71b0 | 2020-09-01 11:20:03 +0000 | [diff] [blame] | 13 | ** This file contains SQLite's SQL parser. |
| 14 | ** |
| 15 | ** The canonical source code to this file ("parse.y") is a Lemon grammar |
| 16 | ** file that specifies the input grammar and actions to take while parsing. |
| 17 | ** That input file is processed by Lemon to generate a C-language |
| 18 | ** implementation of a parser for the given grammer. You might be reading |
| 19 | ** this comment as part of the translated C-code. Edits should be made |
| 20 | ** to the original parse.y sources. |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 21 | */ |
drh | 60c71b0 | 2020-09-01 11:20:03 +0000 | [diff] [blame] | 22 | } |
drh | 487e262 | 2005-06-25 18:42:14 +0000 | [diff] [blame] | 23 | |
| 24 | // All token codes are small integers with #defines that begin with "TK_" |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 25 | %token_prefix TK_ |
drh | 487e262 | 2005-06-25 18:42:14 +0000 | [diff] [blame] | 26 | |
| 27 | // The type of the data attached to each token is Token. This is also the |
| 28 | // default type for non-terminals. |
| 29 | // |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 30 | %token_type {Token} |
drh | f57b14a | 2001-09-14 18:54:08 +0000 | [diff] [blame] | 31 | %default_type {Token} |
drh | 487e262 | 2005-06-25 18:42:14 +0000 | [diff] [blame] | 32 | |
drh | fb32c44 | 2018-04-21 13:51:42 +0000 | [diff] [blame] | 33 | // An extra argument to the constructor for the parser, which is available |
| 34 | // to all actions. |
| 35 | %extra_context {Parse *pParse} |
drh | 487e262 | 2005-06-25 18:42:14 +0000 | [diff] [blame] | 36 | |
| 37 | // This code runs whenever there is a syntax error |
| 38 | // |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 39 | %syntax_error { |
drh | 128255f | 2008-12-08 16:01:12 +0000 | [diff] [blame] | 40 | UNUSED_PARAMETER(yymajor); /* Silence some compiler warnings */ |
drh | 6116ee4 | 2018-01-10 00:40:06 +0000 | [diff] [blame] | 41 | if( TOKEN.z[0] ){ |
| 42 | sqlite3ErrorMsg(pParse, "near \"%T\": syntax error", &TOKEN); |
| 43 | }else{ |
| 44 | sqlite3ErrorMsg(pParse, "incomplete input"); |
| 45 | } |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 46 | } |
drh | 8fc3345 | 2006-02-27 21:58:07 +0000 | [diff] [blame] | 47 | %stack_overflow { |
| 48 | sqlite3ErrorMsg(pParse, "parser stack overflow"); |
| 49 | } |
drh | 487e262 | 2005-06-25 18:42:14 +0000 | [diff] [blame] | 50 | |
| 51 | // The name of the generated procedure that implements the parser |
| 52 | // is as follows: |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 53 | %name sqlite3Parser |
drh | 487e262 | 2005-06-25 18:42:14 +0000 | [diff] [blame] | 54 | |
| 55 | // The following text is included near the beginning of the C source |
| 56 | // code file that implements the parser. |
| 57 | // |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 58 | %include { |
| 59 | #include "sqliteInt.h" |
drh | 9bbca4c | 2001-11-06 04:00:18 +0000 | [diff] [blame] | 60 | |
| 61 | /* |
drh | d3ec02d | 2009-06-12 02:27:14 +0000 | [diff] [blame] | 62 | ** Disable all error recovery processing in the parser push-down |
| 63 | ** automaton. |
| 64 | */ |
| 65 | #define YYNOERRORRECOVERY 1 |
| 66 | |
| 67 | /* |
drh | 8a415d3 | 2009-06-12 13:53:51 +0000 | [diff] [blame] | 68 | ** Make yytestcase() the same as testcase() |
| 69 | */ |
| 70 | #define yytestcase(X) testcase(X) |
| 71 | |
| 72 | /* |
drh | 82415f2 | 2015-11-09 19:33:42 +0000 | [diff] [blame] | 73 | ** Indicate that sqlite3ParserFree() will never be called with a null |
| 74 | ** pointer. |
| 75 | */ |
drh | 644f4c1 | 2015-11-12 15:04:05 +0000 | [diff] [blame] | 76 | #define YYPARSEFREENEVERNULL 1 |
drh | 82415f2 | 2015-11-09 19:33:42 +0000 | [diff] [blame] | 77 | |
| 78 | /* |
drh | d26cc54 | 2017-01-28 20:46:37 +0000 | [diff] [blame] | 79 | ** In the amalgamation, the parse.c file generated by lemon and the |
| 80 | ** tokenize.c file are concatenated. In that case, sqlite3RunParser() |
| 81 | ** has access to the the size of the yyParser object and so the parser |
| 82 | ** engine can be allocated from stack. In that case, only the |
| 83 | ** sqlite3ParserInit() and sqlite3ParserFinalize() routines are invoked |
| 84 | ** and the sqlite3ParserAlloc() and sqlite3ParserFree() routines can be |
| 85 | ** omitted. |
| 86 | */ |
| 87 | #ifdef SQLITE_AMALGAMATION |
| 88 | # define sqlite3Parser_ENGINEALWAYSONSTACK 1 |
| 89 | #endif |
| 90 | |
| 91 | /* |
drh | 82415f2 | 2015-11-09 19:33:42 +0000 | [diff] [blame] | 92 | ** Alternative datatype for the argument to the malloc() routine passed |
| 93 | ** into sqlite3ParserAlloc(). The default is size_t. |
| 94 | */ |
| 95 | #define YYMALLOCARGTYPE u64 |
| 96 | |
| 97 | /* |
drh | ad3cab5 | 2002-05-24 02:04:32 +0000 | [diff] [blame] | 98 | ** An instance of the following structure describes the event of a |
| 99 | ** TRIGGER. "a" is the event type, one of TK_UPDATE, TK_INSERT, |
| 100 | ** TK_DELETE, or TK_INSTEAD. If the event is of the form |
| 101 | ** |
| 102 | ** UPDATE ON (a,b,c) |
| 103 | ** |
| 104 | ** Then the "b" IdList records the list "a,b,c". |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 105 | */ |
drh | ad3cab5 | 2002-05-24 02:04:32 +0000 | [diff] [blame] | 106 | struct TrigEvent { int a; IdList * b; }; |
drh | caec2f1 | 2003-01-07 02:47:47 +0000 | [diff] [blame] | 107 | |
dan | 86fb6e1 | 2018-05-16 20:58:07 +0000 | [diff] [blame] | 108 | struct FrameBound { int eType; Expr *pExpr; }; |
| 109 | |
drh | 25d6543 | 2004-07-22 15:02:25 +0000 | [diff] [blame] | 110 | /* |
drh | 4a642b6 | 2016-02-05 01:55:27 +0000 | [diff] [blame] | 111 | ** Disable lookaside memory allocation for objects that might be |
| 112 | ** shared across database connections. |
| 113 | */ |
| 114 | static void disableLookaside(Parse *pParse){ |
drh | 31f6962 | 2019-10-05 14:39:36 +0000 | [diff] [blame] | 115 | sqlite3 *db = pParse->db; |
drh | 4a642b6 | 2016-02-05 01:55:27 +0000 | [diff] [blame] | 116 | pParse->disableLookaside++; |
drh | 31f6962 | 2019-10-05 14:39:36 +0000 | [diff] [blame] | 117 | DisableLookaside; |
drh | 4a642b6 | 2016-02-05 01:55:27 +0000 | [diff] [blame] | 118 | } |
| 119 | |
drh | 27ee29f | 2020-07-03 21:18:07 +0000 | [diff] [blame] | 120 | #if !defined(SQLITE_ENABLE_UPDATE_DELETE_LIMIT) \ |
| 121 | && defined(SQLITE_UDL_CAPABLE_PARSER) |
drh | c601f10 | 2020-07-03 17:24:35 +0000 | [diff] [blame] | 122 | /* |
| 123 | ** Issue an error message if an ORDER BY or LIMIT clause occurs on an |
| 124 | ** UPDATE or DELETE statement. |
| 125 | */ |
| 126 | static void updateDeleteLimitError( |
| 127 | Parse *pParse, |
| 128 | ExprList *pOrderBy, |
| 129 | Expr *pLimit |
| 130 | ){ |
| 131 | if( pOrderBy ){ |
| 132 | sqlite3ErrorMsg(pParse, "syntax error near \"ORDER BY\""); |
| 133 | }else{ |
| 134 | sqlite3ErrorMsg(pParse, "syntax error near \"LIMIT\""); |
| 135 | } |
| 136 | sqlite3ExprListDelete(pParse->db, pOrderBy); |
| 137 | sqlite3ExprDelete(pParse->db, pLimit); |
| 138 | } |
| 139 | #endif /* SQLITE_ENABLE_UPDATE_DELETE_LIMIT */ |
| 140 | |
drh | caec2f1 | 2003-01-07 02:47:47 +0000 | [diff] [blame] | 141 | } // end %include |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 142 | |
drh | 826fb5a | 2004-02-14 23:59:57 +0000 | [diff] [blame] | 143 | // Input is a single SQL command |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 144 | input ::= cmdlist. |
drh | 094b2bb | 2002-03-13 18:54:07 +0000 | [diff] [blame] | 145 | cmdlist ::= cmdlist ecmd. |
drh | 826fb5a | 2004-02-14 23:59:57 +0000 | [diff] [blame] | 146 | cmdlist ::= ecmd. |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 147 | ecmd ::= SEMI. |
drh | 2424aa7 | 2018-04-11 17:10:54 +0000 | [diff] [blame] | 148 | ecmd ::= cmdx SEMI. |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 149 | %ifndef SQLITE_OMIT_EXPLAIN |
drh | e94006e | 2019-12-10 20:41:48 +0000 | [diff] [blame] | 150 | ecmd ::= explain cmdx SEMI. {NEVER-REDUCE} |
drh | 8549d55 | 2016-01-07 17:09:43 +0000 | [diff] [blame] | 151 | explain ::= EXPLAIN. { pParse->explain = 1; } |
| 152 | explain ::= EXPLAIN QUERY PLAN. { pParse->explain = 2; } |
drh | 154d4b2 | 2006-09-21 11:02:16 +0000 | [diff] [blame] | 153 | %endif SQLITE_OMIT_EXPLAIN |
drh | 200a81d | 2008-08-08 14:19:41 +0000 | [diff] [blame] | 154 | cmdx ::= cmd. { sqlite3FinishCoding(pParse); } |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 155 | |
drh | 382c024 | 2001-10-06 16:33:02 +0000 | [diff] [blame] | 156 | ///////////////////// Begin and end transactions. //////////////////////////// |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 157 | // |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 158 | |
drh | 684917c | 2004-10-05 02:41:42 +0000 | [diff] [blame] | 159 | cmd ::= BEGIN transtype(Y) trans_opt. {sqlite3BeginTransaction(pParse, Y);} |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 160 | trans_opt ::= . |
| 161 | trans_opt ::= TRANSACTION. |
drh | 5ad1a6c | 2002-07-01 12:27:09 +0000 | [diff] [blame] | 162 | trans_opt ::= TRANSACTION nm. |
drh | 684917c | 2004-10-05 02:41:42 +0000 | [diff] [blame] | 163 | %type transtype {int} |
| 164 | transtype(A) ::= . {A = TK_DEFERRED;} |
drh | cf82f0d | 2016-02-17 04:33:10 +0000 | [diff] [blame] | 165 | transtype(A) ::= DEFERRED(X). {A = @X; /*A-overwrites-X*/} |
| 166 | transtype(A) ::= IMMEDIATE(X). {A = @X; /*A-overwrites-X*/} |
| 167 | transtype(A) ::= EXCLUSIVE(X). {A = @X; /*A-overwrites-X*/} |
drh | 07a3b11 | 2017-07-06 01:28:02 +0000 | [diff] [blame] | 168 | cmd ::= COMMIT|END(X) trans_opt. {sqlite3EndTransaction(pParse,@X);} |
| 169 | cmd ::= ROLLBACK(X) trans_opt. {sqlite3EndTransaction(pParse,@X);} |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 170 | |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 171 | savepoint_opt ::= SAVEPOINT. |
| 172 | savepoint_opt ::= . |
| 173 | cmd ::= SAVEPOINT nm(X). { |
| 174 | sqlite3Savepoint(pParse, SAVEPOINT_BEGIN, &X); |
| 175 | } |
| 176 | cmd ::= RELEASE savepoint_opt nm(X). { |
| 177 | sqlite3Savepoint(pParse, SAVEPOINT_RELEASE, &X); |
| 178 | } |
| 179 | cmd ::= ROLLBACK trans_opt TO savepoint_opt nm(X). { |
| 180 | sqlite3Savepoint(pParse, SAVEPOINT_ROLLBACK, &X); |
| 181 | } |
| 182 | |
drh | 382c024 | 2001-10-06 16:33:02 +0000 | [diff] [blame] | 183 | ///////////////////// The CREATE TABLE statement //////////////////////////// |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 184 | // |
| 185 | cmd ::= create_table create_table_args. |
drh | d9da78a | 2009-03-24 15:08:09 +0000 | [diff] [blame] | 186 | create_table ::= createkw temp(T) TABLE ifnotexists(E) nm(Y) dbnm(Z). { |
danielk1977 | f1a381e | 2006-06-16 08:01:02 +0000 | [diff] [blame] | 187 | sqlite3StartTable(pParse,&Y,&Z,T,0,0,E); |
drh | 969fa7c | 2002-02-18 18:30:32 +0000 | [diff] [blame] | 188 | } |
drh | dabd04c | 2016-02-17 01:46:19 +0000 | [diff] [blame] | 189 | createkw(A) ::= CREATE(A). {disableLookaside(pParse);} |
| 190 | |
drh | faa5955 | 2005-12-29 23:33:54 +0000 | [diff] [blame] | 191 | %type ifnotexists {int} |
| 192 | ifnotexists(A) ::= . {A = 0;} |
| 193 | ifnotexists(A) ::= IF NOT EXISTS. {A = 1;} |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 194 | %type temp {int} |
danielk1977 | 53c0f74 | 2005-03-29 03:10:59 +0000 | [diff] [blame] | 195 | %ifndef SQLITE_OMIT_TEMPDB |
drh | c645871 | 2021-04-28 17:37:26 +0000 | [diff] [blame] | 196 | temp(A) ::= TEMP. {A = pParse->db->init.busy==0;} |
drh | 154d4b2 | 2006-09-21 11:02:16 +0000 | [diff] [blame] | 197 | %endif SQLITE_OMIT_TEMPDB |
drh | d24cc42 | 2003-03-27 12:51:24 +0000 | [diff] [blame] | 198 | temp(A) ::= . {A = 0;} |
drh | 44183f8 | 2021-08-18 13:13:58 +0000 | [diff] [blame] | 199 | create_table_args ::= LP columnlist conslist_opt(X) RP(E) table_option_set(F). { |
drh | 5969da4 | 2013-10-21 02:14:45 +0000 | [diff] [blame] | 200 | sqlite3EndTable(pParse,&X,&E,F,0); |
drh | 969fa7c | 2002-02-18 18:30:32 +0000 | [diff] [blame] | 201 | } |
| 202 | create_table_args ::= AS select(S). { |
drh | 5969da4 | 2013-10-21 02:14:45 +0000 | [diff] [blame] | 203 | sqlite3EndTable(pParse,0,0,0,S); |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 204 | sqlite3SelectDelete(pParse->db, S); |
drh | 969fa7c | 2002-02-18 18:30:32 +0000 | [diff] [blame] | 205 | } |
drh | 44183f8 | 2021-08-18 13:13:58 +0000 | [diff] [blame] | 206 | %type table_option_set {u32} |
| 207 | %type table_option {u32} |
| 208 | table_option_set(A) ::= . {A = 0;} |
| 209 | table_option_set(A) ::= table_option(A). |
| 210 | table_option_set(A) ::= table_option_set(X) COMMA table_option(Y). {A = X|Y;} |
| 211 | table_option(A) ::= WITHOUT nm(X). { |
drh | 5969da4 | 2013-10-21 02:14:45 +0000 | [diff] [blame] | 212 | if( X.n==5 && sqlite3_strnicmp(X.z,"rowid",5)==0 ){ |
drh | fccda8a | 2015-05-27 13:06:55 +0000 | [diff] [blame] | 213 | A = TF_WithoutRowid | TF_NoVisibleRowid; |
drh | 5969da4 | 2013-10-21 02:14:45 +0000 | [diff] [blame] | 214 | }else{ |
| 215 | A = 0; |
| 216 | sqlite3ErrorMsg(pParse, "unknown table option: %.*s", X.n, X.z); |
| 217 | } |
| 218 | } |
drh | 44183f8 | 2021-08-18 13:13:58 +0000 | [diff] [blame] | 219 | table_option(A) ::= nm(X). { |
| 220 | if( X.n==6 && sqlite3_strnicmp(X.z,"strict",6)==0 ){ |
| 221 | A = TF_Strict; |
| 222 | }else{ |
| 223 | A = 0; |
| 224 | sqlite3ErrorMsg(pParse, "unknown table option: %.*s", X.n, X.z); |
| 225 | } |
| 226 | } |
drh | 986dde7 | 2016-02-29 13:37:21 +0000 | [diff] [blame] | 227 | columnlist ::= columnlist COMMA columnname carglist. |
| 228 | columnlist ::= columnname carglist. |
drh | 77441fa | 2021-07-30 18:39:59 +0000 | [diff] [blame] | 229 | columnname(A) ::= nm(A) typetoken(Y). {sqlite3AddColumn(pParse,A,Y);} |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 230 | |
drh | 6a8700b | 2017-08-02 11:04:00 +0000 | [diff] [blame] | 231 | // Declare some tokens early in order to influence their values, to |
| 232 | // improve performance and reduce the executable size. The goal here is |
| 233 | // to get the "jump" operations in ISNULL through ESCAPE to have numeric |
| 234 | // values that are early enough so that all jump operations are clustered |
drh | 7bbdc3c | 2019-04-05 21:17:11 +0000 | [diff] [blame] | 235 | // at the beginning. |
drh | 6a8700b | 2017-08-02 11:04:00 +0000 | [diff] [blame] | 236 | // |
| 237 | %token ABORT ACTION AFTER ANALYZE ASC ATTACH BEFORE BEGIN BY CASCADE CAST. |
| 238 | %token CONFLICT DATABASE DEFERRED DESC DETACH EACH END EXCLUSIVE EXPLAIN FAIL. |
drh | d5326c3 | 2022-01-07 14:58:47 +0000 | [diff] [blame] | 239 | %token OR AND NOT IS MATCH LIKE_KW BETWEEN IN ISNULL NOTNULL NE EQ. |
drh | 6a8700b | 2017-08-02 11:04:00 +0000 | [diff] [blame] | 240 | %token GT LE LT GE ESCAPE. |
| 241 | |
drh | 31d6fd5 | 2017-04-14 19:03:10 +0000 | [diff] [blame] | 242 | // The following directive causes tokens ABORT, AFTER, ASC, etc. to |
| 243 | // fallback to ID if they will not parse as their original value. |
| 244 | // This obviates the need for the "id" nonterminal. |
| 245 | // |
| 246 | %fallback ID |
| 247 | ABORT ACTION AFTER ANALYZE ASC ATTACH BEFORE BEGIN BY CASCADE CAST COLUMNKW |
drh | 0a6259f | 2018-04-16 13:26:53 +0000 | [diff] [blame] | 248 | CONFLICT DATABASE DEFERRED DESC DETACH DO |
drh | 6cd7d48 | 2018-04-12 15:43:05 +0000 | [diff] [blame] | 249 | EACH END EXCLUSIVE EXPLAIN FAIL FOR |
drh | 31d6fd5 | 2017-04-14 19:03:10 +0000 | [diff] [blame] | 250 | IGNORE IMMEDIATE INITIALLY INSTEAD LIKE_KW MATCH NO PLAN |
dan | 86fb6e1 | 2018-05-16 20:58:07 +0000 | [diff] [blame] | 251 | QUERY KEY OF OFFSET PRAGMA RAISE RECURSIVE RELEASE REPLACE RESTRICT ROW ROWS |
drh | 31d6fd5 | 2017-04-14 19:03:10 +0000 | [diff] [blame] | 252 | ROLLBACK SAVEPOINT TEMP TRIGGER VACUUM VIEW VIRTUAL WITH WITHOUT |
dan | 6e11892 | 2019-08-12 16:36:38 +0000 | [diff] [blame] | 253 | NULLS FIRST LAST |
drh | 31d6fd5 | 2017-04-14 19:03:10 +0000 | [diff] [blame] | 254 | %ifdef SQLITE_OMIT_COMPOUND_SELECT |
| 255 | EXCEPT INTERSECT UNION |
| 256 | %endif SQLITE_OMIT_COMPOUND_SELECT |
drh | 3773c25 | 2018-06-28 03:38:49 +0000 | [diff] [blame] | 257 | %ifndef SQLITE_OMIT_WINDOWFUNC |
dan | 6e2210e | 2018-06-30 18:54:56 +0000 | [diff] [blame] | 258 | CURRENT FOLLOWING PARTITION PRECEDING RANGE UNBOUNDED |
dan | ced8912 | 2019-03-19 06:40:29 +0000 | [diff] [blame] | 259 | EXCLUDE GROUPS OTHERS TIES |
drh | 3773c25 | 2018-06-28 03:38:49 +0000 | [diff] [blame] | 260 | %endif SQLITE_OMIT_WINDOWFUNC |
drh | 9ee9414 | 2019-10-30 13:00:23 +0000 | [diff] [blame] | 261 | %ifndef SQLITE_OMIT_GENERATED_COLUMNS |
drh | 089c4bc | 2019-11-02 13:45:03 +0000 | [diff] [blame] | 262 | GENERATED ALWAYS |
drh | 9ee9414 | 2019-10-30 13:00:23 +0000 | [diff] [blame] | 263 | %endif |
drh | 745912e | 2021-02-22 03:04:25 +0000 | [diff] [blame] | 264 | MATERIALIZED |
drh | 31d6fd5 | 2017-04-14 19:03:10 +0000 | [diff] [blame] | 265 | REINDEX RENAME CTIME_KW IF |
| 266 | . |
| 267 | %wildcard ANY. |
| 268 | |
drh | f7b5496 | 2013-05-28 12:11:54 +0000 | [diff] [blame] | 269 | // Define operator precedence early so that this is the first occurrence |
drh | 2d3917d | 2004-02-22 16:27:00 +0000 | [diff] [blame] | 270 | // of the operator tokens in the grammer. Keeping the operators together |
| 271 | // causes them to be assigned integer values that are close together, |
| 272 | // which keeps parser tables smaller. |
| 273 | // |
drh | f2bc013 | 2004-10-04 13:19:23 +0000 | [diff] [blame] | 274 | // The token values assigned to these symbols is determined by the order |
| 275 | // in which lemon first sees them. It must be the case that ISNULL/NOTNULL, |
| 276 | // NE/EQ, GT/LE, and GE/LT are separated by only a single value. See |
| 277 | // the sqlite3ExprIfFalse() routine for additional information on this |
| 278 | // constraint. |
| 279 | // |
drh | 2d3917d | 2004-02-22 16:27:00 +0000 | [diff] [blame] | 280 | %left OR. |
| 281 | %left AND. |
| 282 | %right NOT. |
drh | 03bea70 | 2006-06-13 15:37:26 +0000 | [diff] [blame] | 283 | %left IS MATCH LIKE_KW BETWEEN IN ISNULL NOTNULL NE EQ. |
drh | 9a43267 | 2004-10-04 13:38:09 +0000 | [diff] [blame] | 284 | %left GT LE LT GE. |
danielk1977 | 7c6303c | 2004-11-17 16:41:29 +0000 | [diff] [blame] | 285 | %right ESCAPE. |
drh | 2d3917d | 2004-02-22 16:27:00 +0000 | [diff] [blame] | 286 | %left BITAND BITOR LSHIFT RSHIFT. |
| 287 | %left PLUS MINUS. |
| 288 | %left STAR SLASH REM. |
drh | d5326c3 | 2022-01-07 14:58:47 +0000 | [diff] [blame] | 289 | %left CONCAT PTR. |
drh | a34001c | 2007-02-02 12:44:37 +0000 | [diff] [blame] | 290 | %left COLLATE. |
drh | 7ba5bc5 | 2009-09-22 20:08:34 +0000 | [diff] [blame] | 291 | %right BITNOT. |
drh | 26cf56f | 2018-04-06 19:36:49 +0000 | [diff] [blame] | 292 | %nonassoc ON. |
drh | 2d3917d | 2004-02-22 16:27:00 +0000 | [diff] [blame] | 293 | |
drh | 7cc84c2 | 2016-04-11 13:36:42 +0000 | [diff] [blame] | 294 | // An IDENTIFIER can be a generic identifier, or one of several |
| 295 | // keywords. Any non-standard keyword can also be an identifier. |
| 296 | // |
| 297 | %token_class id ID|INDEXED. |
| 298 | |
drh | 7cc84c2 | 2016-04-11 13:36:42 +0000 | [diff] [blame] | 299 | |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 300 | // And "ids" is an identifer-or-string. |
| 301 | // |
dan | 59ff425 | 2018-06-29 17:44:52 +0000 | [diff] [blame] | 302 | %token_class ids ID|STRING. |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 303 | |
drh | 5ad1a6c | 2002-07-01 12:27:09 +0000 | [diff] [blame] | 304 | // The name of a column or table can be any of the following: |
| 305 | // |
| 306 | %type nm {Token} |
drh | 4dd0d3f | 2016-02-17 01:18:33 +0000 | [diff] [blame] | 307 | nm(A) ::= id(A). |
| 308 | nm(A) ::= STRING(A). |
| 309 | nm(A) ::= JOIN_KW(A). |
drh | 5ad1a6c | 2002-07-01 12:27:09 +0000 | [diff] [blame] | 310 | |
drh | 986dde7 | 2016-02-29 13:37:21 +0000 | [diff] [blame] | 311 | // A typetoken is really zero or more tokens that form a type name such |
drh | 487e262 | 2005-06-25 18:42:14 +0000 | [diff] [blame] | 312 | // as can be found after the column name in a CREATE TABLE statement. |
| 313 | // Multiple tokens are concatenated to form the value of the typetoken. |
| 314 | // |
| 315 | %type typetoken {Token} |
drh | 986dde7 | 2016-02-29 13:37:21 +0000 | [diff] [blame] | 316 | typetoken(A) ::= . {A.n = 0; A.z = 0;} |
drh | 4dd0d3f | 2016-02-17 01:18:33 +0000 | [diff] [blame] | 317 | typetoken(A) ::= typename(A). |
| 318 | typetoken(A) ::= typename(A) LP signed RP(Y). { |
| 319 | A.n = (int)(&Y.z[Y.n] - A.z); |
drh | 487e262 | 2005-06-25 18:42:14 +0000 | [diff] [blame] | 320 | } |
drh | 4dd0d3f | 2016-02-17 01:18:33 +0000 | [diff] [blame] | 321 | typetoken(A) ::= typename(A) LP signed COMMA signed RP(Y). { |
| 322 | A.n = (int)(&Y.z[Y.n] - A.z); |
drh | 487e262 | 2005-06-25 18:42:14 +0000 | [diff] [blame] | 323 | } |
drh | 382c024 | 2001-10-06 16:33:02 +0000 | [diff] [blame] | 324 | %type typename {Token} |
dan | 59ff425 | 2018-06-29 17:44:52 +0000 | [diff] [blame] | 325 | typename(A) ::= ids(A). |
| 326 | typename(A) ::= typename(A) ids(Y). {A.n=Y.n+(int)(Y.z-A.z);} |
drh | 60218d2 | 2007-04-06 11:26:00 +0000 | [diff] [blame] | 327 | signed ::= plus_num. |
| 328 | signed ::= minus_num. |
drh | 487e262 | 2005-06-25 18:42:14 +0000 | [diff] [blame] | 329 | |
drh | 1be266b | 2017-12-24 00:18:47 +0000 | [diff] [blame] | 330 | // The scanpt non-terminal takes a value which is a pointer to the |
| 331 | // input text just past the last token that has been shifted into |
| 332 | // the parser. By surrounding some phrase in the grammar with two |
| 333 | // scanpt non-terminals, we can capture the input text for that phrase. |
| 334 | // For example: |
| 335 | // |
| 336 | // something ::= .... scanpt(A) phrase scanpt(Z). |
| 337 | // |
| 338 | // The text that is parsed as "phrase" is a string starting at A |
| 339 | // and containing (int)(Z-A) characters. There might be some extra |
| 340 | // whitespace on either end of the text, but that can be removed in |
| 341 | // post-processing, if needed. |
| 342 | // |
| 343 | %type scanpt {const char*} |
| 344 | scanpt(A) ::= . { |
drh | f259df5 | 2017-12-27 20:38:35 +0000 | [diff] [blame] | 345 | assert( yyLookahead!=YYNOCODE ); |
| 346 | A = yyLookaheadToken.z; |
drh | 1be266b | 2017-12-24 00:18:47 +0000 | [diff] [blame] | 347 | } |
dan | 4db4b5b | 2019-05-18 19:49:08 +0000 | [diff] [blame] | 348 | scantok(A) ::= . { |
| 349 | assert( yyLookahead!=YYNOCODE ); |
| 350 | A = yyLookaheadToken; |
| 351 | } |
drh | 1be266b | 2017-12-24 00:18:47 +0000 | [diff] [blame] | 352 | |
drh | 487e262 | 2005-06-25 18:42:14 +0000 | [diff] [blame] | 353 | // "carglist" is a list of additional constraints that come after the |
| 354 | // column name and column type in a CREATE TABLE statement. |
| 355 | // |
drh | 4dc330d | 2012-05-07 19:21:36 +0000 | [diff] [blame] | 356 | carglist ::= carglist ccons. |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 357 | carglist ::= . |
drh | 4dc330d | 2012-05-07 19:21:36 +0000 | [diff] [blame] | 358 | ccons ::= CONSTRAINT nm(X). {pParse->constraintName = X;} |
dan | 4db4b5b | 2019-05-18 19:49:08 +0000 | [diff] [blame] | 359 | ccons ::= DEFAULT scantok(A) term(X). |
| 360 | {sqlite3AddDefaultValue(pParse,X,A.z,&A.z[A.n]);} |
drh | 1be266b | 2017-12-24 00:18:47 +0000 | [diff] [blame] | 361 | ccons ::= DEFAULT LP(A) expr(X) RP(Z). |
| 362 | {sqlite3AddDefaultValue(pParse,X,A.z+1,Z.z);} |
dan | 4db4b5b | 2019-05-18 19:49:08 +0000 | [diff] [blame] | 363 | ccons ::= DEFAULT PLUS(A) scantok(Z) term(X). |
| 364 | {sqlite3AddDefaultValue(pParse,X,A.z,&Z.z[Z.n]);} |
| 365 | ccons ::= DEFAULT MINUS(A) scantok(Z) term(X). { |
drh | 1be266b | 2017-12-24 00:18:47 +0000 | [diff] [blame] | 366 | Expr *p = sqlite3PExpr(pParse, TK_UMINUS, X, 0); |
dan | 4db4b5b | 2019-05-18 19:49:08 +0000 | [diff] [blame] | 367 | sqlite3AddDefaultValue(pParse,p,A.z,&Z.z[Z.n]); |
danielk1977 | 7977a17 | 2004-11-09 12:44:37 +0000 | [diff] [blame] | 368 | } |
dan | 4db4b5b | 2019-05-18 19:49:08 +0000 | [diff] [blame] | 369 | ccons ::= DEFAULT scantok id(X). { |
drh | 1be266b | 2017-12-24 00:18:47 +0000 | [diff] [blame] | 370 | Expr *p = tokenExpr(pParse, TK_STRING, X); |
drh | d7fd899 | 2018-02-28 04:30:55 +0000 | [diff] [blame] | 371 | if( p ){ |
| 372 | sqlite3ExprIdToTrueFalse(p); |
| 373 | testcase( p->op==TK_TRUEFALSE && sqlite3ExprTruthValue(p) ); |
| 374 | } |
dan | c9461ec | 2018-08-29 21:00:16 +0000 | [diff] [blame] | 375 | sqlite3AddDefaultValue(pParse,p,X.z,X.z+X.n); |
danielk1977 | 7977a17 | 2004-11-09 12:44:37 +0000 | [diff] [blame] | 376 | } |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 377 | |
drh | 382c024 | 2001-10-06 16:33:02 +0000 | [diff] [blame] | 378 | // In addition to the type name, we also care about the primary key and |
| 379 | // UNIQUE constraints. |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 380 | // |
drh | 0d316a4 | 2002-08-11 20:10:47 +0000 | [diff] [blame] | 381 | ccons ::= NULL onconf. |
drh | b7916a7 | 2009-05-27 10:31:29 +0000 | [diff] [blame] | 382 | ccons ::= NOT NULL onconf(R). {sqlite3AddNotNull(pParse, R);} |
drh | fdd6e85 | 2005-12-16 01:06:16 +0000 | [diff] [blame] | 383 | ccons ::= PRIMARY KEY sortorder(Z) onconf(R) autoinc(I). |
drh | b7916a7 | 2009-05-27 10:31:29 +0000 | [diff] [blame] | 384 | {sqlite3AddPrimaryKey(pParse,0,R,I,Z);} |
drh | 62340f8 | 2016-05-31 21:18:15 +0000 | [diff] [blame] | 385 | ccons ::= UNIQUE onconf(R). {sqlite3CreateIndex(pParse,0,0,0,0,R,0,0,0,0, |
| 386 | SQLITE_IDXTYPE_UNIQUE);} |
drh | 92e21ef | 2020-08-27 18:36:30 +0000 | [diff] [blame] | 387 | ccons ::= CHECK LP(A) expr(X) RP(B). {sqlite3AddCheckConstraint(pParse,X,A.z,B.z);} |
drh | 108aa00 | 2015-08-24 20:21:20 +0000 | [diff] [blame] | 388 | ccons ::= REFERENCES nm(T) eidlist_opt(TA) refargs(R). |
drh | b7916a7 | 2009-05-27 10:31:29 +0000 | [diff] [blame] | 389 | {sqlite3CreateForeignKey(pParse,0,&T,TA,R);} |
| 390 | ccons ::= defer_subclause(D). {sqlite3DeferForeignKey(pParse,D);} |
dan | 59ff425 | 2018-06-29 17:44:52 +0000 | [diff] [blame] | 391 | ccons ::= COLLATE ids(C). {sqlite3AddCollateType(pParse, &C);} |
drh | 81f7b37 | 2019-10-16 12:18:59 +0000 | [diff] [blame] | 392 | ccons ::= GENERATED ALWAYS AS generated. |
| 393 | ccons ::= AS generated. |
drh | 7e508f1 | 2019-10-16 19:31:46 +0000 | [diff] [blame] | 394 | generated ::= LP expr(E) RP. {sqlite3AddGenerated(pParse,E,0);} |
| 395 | generated ::= LP expr(E) RP ID(TYPE). {sqlite3AddGenerated(pParse,E,&TYPE);} |
drh | 04738cb | 2002-06-02 18:19:00 +0000 | [diff] [blame] | 396 | |
drh | 205f48e | 2004-11-05 00:43:11 +0000 | [diff] [blame] | 397 | // The optional AUTOINCREMENT keyword |
| 398 | %type autoinc {int} |
drh | 2958a4e | 2004-11-12 03:56:15 +0000 | [diff] [blame] | 399 | autoinc(X) ::= . {X = 0;} |
| 400 | autoinc(X) ::= AUTOINCR. {X = 1;} |
drh | 205f48e | 2004-11-05 00:43:11 +0000 | [diff] [blame] | 401 | |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 402 | // The next group of rules parses the arguments to a REFERENCES clause |
| 403 | // that determine if the referential integrity checking is deferred or |
| 404 | // or immediate and which determine what action to take if a ref-integ |
| 405 | // check fails. |
drh | 04738cb | 2002-06-02 18:19:00 +0000 | [diff] [blame] | 406 | // |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 407 | %type refargs {int} |
drh | fcf486c | 2009-10-21 13:48:24 +0000 | [diff] [blame] | 408 | refargs(A) ::= . { A = OE_None*0x0101; /* EV: R-19803-45884 */} |
drh | 4dd0d3f | 2016-02-17 01:18:33 +0000 | [diff] [blame] | 409 | refargs(A) ::= refargs(A) refarg(Y). { A = (A & ~Y.mask) | Y.value; } |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 410 | %type refarg {struct {int value; int mask;}} |
| 411 | refarg(A) ::= MATCH nm. { A.value = 0; A.mask = 0x000000; } |
drh | c29c5aa1 | 2009-12-09 21:43:36 +0000 | [diff] [blame] | 412 | refarg(A) ::= ON INSERT refact. { A.value = 0; A.mask = 0x000000; } |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 413 | refarg(A) ::= ON DELETE refact(X). { A.value = X; A.mask = 0x0000ff; } |
| 414 | refarg(A) ::= ON UPDATE refact(X). { A.value = X<<8; A.mask = 0x00ff00; } |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 415 | %type refact {int} |
drh | fcf486c | 2009-10-21 13:48:24 +0000 | [diff] [blame] | 416 | refact(A) ::= SET NULL. { A = OE_SetNull; /* EV: R-33326-45252 */} |
| 417 | refact(A) ::= SET DEFAULT. { A = OE_SetDflt; /* EV: R-33326-45252 */} |
| 418 | refact(A) ::= CASCADE. { A = OE_Cascade; /* EV: R-33326-45252 */} |
| 419 | refact(A) ::= RESTRICT. { A = OE_Restrict; /* EV: R-33326-45252 */} |
| 420 | refact(A) ::= NO ACTION. { A = OE_None; /* EV: R-33326-45252 */} |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 421 | %type defer_subclause {int} |
dan | 1da40a3 | 2009-09-19 17:00:31 +0000 | [diff] [blame] | 422 | defer_subclause(A) ::= NOT DEFERRABLE init_deferred_pred_opt. {A = 0;} |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 423 | defer_subclause(A) ::= DEFERRABLE init_deferred_pred_opt(X). {A = X;} |
| 424 | %type init_deferred_pred_opt {int} |
| 425 | init_deferred_pred_opt(A) ::= . {A = 0;} |
| 426 | init_deferred_pred_opt(A) ::= INITIALLY DEFERRED. {A = 1;} |
| 427 | init_deferred_pred_opt(A) ::= INITIALLY IMMEDIATE. {A = 0;} |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 428 | |
drh | aeb281c | 2012-05-08 11:17:33 +0000 | [diff] [blame] | 429 | conslist_opt(A) ::= . {A.n = 0; A.z = 0;} |
drh | 4dd0d3f | 2016-02-17 01:18:33 +0000 | [diff] [blame] | 430 | conslist_opt(A) ::= COMMA(A) conslist. |
drh | ab35eae | 2012-05-12 18:29:53 +0000 | [diff] [blame] | 431 | conslist ::= conslist tconscomma tcons. |
| 432 | conslist ::= tcons. |
| 433 | tconscomma ::= COMMA. {pParse->constraintName.n = 0;} |
| 434 | tconscomma ::= . |
| 435 | tcons ::= CONSTRAINT nm(X). {pParse->constraintName = X;} |
drh | 108aa00 | 2015-08-24 20:21:20 +0000 | [diff] [blame] | 436 | tcons ::= PRIMARY KEY LP sortlist(X) autoinc(I) RP onconf(R). |
drh | b7916a7 | 2009-05-27 10:31:29 +0000 | [diff] [blame] | 437 | {sqlite3AddPrimaryKey(pParse,X,R,I,0);} |
drh | 108aa00 | 2015-08-24 20:21:20 +0000 | [diff] [blame] | 438 | tcons ::= UNIQUE LP sortlist(X) RP onconf(R). |
drh | 62340f8 | 2016-05-31 21:18:15 +0000 | [diff] [blame] | 439 | {sqlite3CreateIndex(pParse,0,0,0,X,R,0,0,0,0, |
| 440 | SQLITE_IDXTYPE_UNIQUE);} |
drh | 92e21ef | 2020-08-27 18:36:30 +0000 | [diff] [blame] | 441 | tcons ::= CHECK LP(A) expr(E) RP(B) onconf. |
| 442 | {sqlite3AddCheckConstraint(pParse,E,A.z,B.z);} |
drh | 108aa00 | 2015-08-24 20:21:20 +0000 | [diff] [blame] | 443 | tcons ::= FOREIGN KEY LP eidlist(FA) RP |
| 444 | REFERENCES nm(T) eidlist_opt(TA) refargs(R) defer_subclause_opt(D). { |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 445 | sqlite3CreateForeignKey(pParse, FA, &T, TA, R); |
| 446 | sqlite3DeferForeignKey(pParse, D); |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 447 | } |
| 448 | %type defer_subclause_opt {int} |
| 449 | defer_subclause_opt(A) ::= . {A = 0;} |
drh | 4dd0d3f | 2016-02-17 01:18:33 +0000 | [diff] [blame] | 450 | defer_subclause_opt(A) ::= defer_subclause(A). |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 451 | |
| 452 | // The following is a non-standard extension that allows us to declare the |
| 453 | // default behavior when there is a constraint conflict. |
| 454 | // |
| 455 | %type onconf {int} |
drh | 3334d08 | 2015-11-10 13:45:21 +0000 | [diff] [blame] | 456 | %type orconf {int} |
drh | 1c92853 | 2002-01-31 15:54:21 +0000 | [diff] [blame] | 457 | %type resolvetype {int} |
drh | 74ad7fe | 2004-10-07 03:06:28 +0000 | [diff] [blame] | 458 | onconf(A) ::= . {A = OE_Default;} |
| 459 | onconf(A) ::= ON CONFLICT resolvetype(X). {A = X;} |
| 460 | orconf(A) ::= . {A = OE_Default;} |
drh | 3334d08 | 2015-11-10 13:45:21 +0000 | [diff] [blame] | 461 | orconf(A) ::= OR resolvetype(X). {A = X;} |
drh | 4dd0d3f | 2016-02-17 01:18:33 +0000 | [diff] [blame] | 462 | resolvetype(A) ::= raisetype(A). |
drh | 74ad7fe | 2004-10-07 03:06:28 +0000 | [diff] [blame] | 463 | resolvetype(A) ::= IGNORE. {A = OE_Ignore;} |
| 464 | resolvetype(A) ::= REPLACE. {A = OE_Replace;} |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 465 | |
drh | 382c024 | 2001-10-06 16:33:02 +0000 | [diff] [blame] | 466 | ////////////////////////// The DROP TABLE ///////////////////////////////////// |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 467 | // |
drh | a073384 | 2005-12-29 01:11:36 +0000 | [diff] [blame] | 468 | cmd ::= DROP TABLE ifexists(E) fullname(X). { |
| 469 | sqlite3DropTable(pParse, X, 0, E); |
danielk1977 | a885810 | 2004-05-28 12:11:21 +0000 | [diff] [blame] | 470 | } |
drh | a073384 | 2005-12-29 01:11:36 +0000 | [diff] [blame] | 471 | %type ifexists {int} |
| 472 | ifexists(A) ::= IF EXISTS. {A = 1;} |
| 473 | ifexists(A) ::= . {A = 0;} |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 474 | |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 475 | ///////////////////// The CREATE VIEW statement ///////////////////////////// |
| 476 | // |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 477 | %ifndef SQLITE_OMIT_VIEW |
drh | 108aa00 | 2015-08-24 20:21:20 +0000 | [diff] [blame] | 478 | cmd ::= createkw(X) temp(T) VIEW ifnotexists(E) nm(Y) dbnm(Z) eidlist_opt(C) |
drh | 8981b90 | 2015-08-24 17:42:49 +0000 | [diff] [blame] | 479 | AS select(S). { |
| 480 | sqlite3CreateView(pParse, &X, &Y, &Z, C, S, T, E); |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 481 | } |
drh | a073384 | 2005-12-29 01:11:36 +0000 | [diff] [blame] | 482 | cmd ::= DROP VIEW ifexists(E) fullname(X). { |
| 483 | sqlite3DropTable(pParse, X, 1, E); |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 484 | } |
drh | 154d4b2 | 2006-09-21 11:02:16 +0000 | [diff] [blame] | 485 | %endif SQLITE_OMIT_VIEW |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 486 | |
drh | 382c024 | 2001-10-06 16:33:02 +0000 | [diff] [blame] | 487 | //////////////////////// The SELECT statement ///////////////////////////////// |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 488 | // |
dan | 7d562db | 2014-01-11 19:19:36 +0000 | [diff] [blame] | 489 | cmd ::= select(X). { |
drh | a7c7400 | 2020-07-18 18:44:59 +0000 | [diff] [blame] | 490 | SelectDest dest = {SRT_Output, 0, 0, 0, 0, 0, 0}; |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 491 | sqlite3Select(pParse, X, &dest); |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 492 | sqlite3SelectDelete(pParse->db, X); |
drh | 9bb61fe | 2000-06-05 16:01:39 +0000 | [diff] [blame] | 493 | } |
drh | efb7251 | 2000-05-31 20:00:52 +0000 | [diff] [blame] | 494 | |
drh | 9bb61fe | 2000-06-05 16:01:39 +0000 | [diff] [blame] | 495 | %type select {Select*} |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 496 | %destructor select {sqlite3SelectDelete(pParse->db, $$);} |
dan | 7d562db | 2014-01-11 19:19:36 +0000 | [diff] [blame] | 497 | %type selectnowith {Select*} |
| 498 | %destructor selectnowith {sqlite3SelectDelete(pParse->db, $$);} |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 499 | %type oneselect {Select*} |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 500 | %destructor oneselect {sqlite3SelectDelete(pParse->db, $$);} |
drh | 9bb61fe | 2000-06-05 16:01:39 +0000 | [diff] [blame] | 501 | |
drh | 772460f | 2015-04-16 14:13:12 +0000 | [diff] [blame] | 502 | %include { |
| 503 | /* |
| 504 | ** For a compound SELECT statement, make sure p->pPrior->pNext==p for |
| 505 | ** all elements in the list. And make sure list length does not exceed |
| 506 | ** SQLITE_LIMIT_COMPOUND_SELECT. |
| 507 | */ |
drh | e318a7f | 2015-04-16 23:04:17 +0000 | [diff] [blame] | 508 | static void parserDoubleLinkSelect(Parse *pParse, Select *p){ |
drh | 55f66b3 | 2019-07-16 19:44:32 +0000 | [diff] [blame] | 509 | assert( p!=0 ); |
drh | d227a29 | 2014-02-09 18:02:09 +0000 | [diff] [blame] | 510 | if( p->pPrior ){ |
drh | aae0f74 | 2021-03-04 16:03:32 +0000 | [diff] [blame] | 511 | Select *pNext = 0, *pLoop = p; |
| 512 | int mxSelect, cnt = 1; |
| 513 | while(1){ |
drh | d227a29 | 2014-02-09 18:02:09 +0000 | [diff] [blame] | 514 | pLoop->pNext = pNext; |
| 515 | pLoop->selFlags |= SF_Compound; |
drh | aae0f74 | 2021-03-04 16:03:32 +0000 | [diff] [blame] | 516 | pNext = pLoop; |
| 517 | pLoop = pLoop->pPrior; |
| 518 | if( pLoop==0 ) break; |
| 519 | cnt++; |
| 520 | if( pLoop->pOrderBy || pLoop->pLimit ){ |
| 521 | sqlite3ErrorMsg(pParse,"%s clause should come after %s not before", |
| 522 | pLoop->pOrderBy!=0 ? "ORDER BY" : "LIMIT", |
| 523 | sqlite3SelectOpName(pNext->op)); |
| 524 | break; |
| 525 | } |
drh | d227a29 | 2014-02-09 18:02:09 +0000 | [diff] [blame] | 526 | } |
drh | 772460f | 2015-04-16 14:13:12 +0000 | [diff] [blame] | 527 | if( (p->selFlags & SF_MultiValue)==0 && |
| 528 | (mxSelect = pParse->db->aLimit[SQLITE_LIMIT_COMPOUND_SELECT])>0 && |
| 529 | cnt>mxSelect |
drh | a0c0176 | 2015-01-05 16:27:43 +0000 | [diff] [blame] | 530 | ){ |
drh | d227a29 | 2014-02-09 18:02:09 +0000 | [diff] [blame] | 531 | sqlite3ErrorMsg(pParse, "too many terms in compound SELECT"); |
| 532 | } |
| 533 | } |
drh | 772460f | 2015-04-16 14:13:12 +0000 | [diff] [blame] | 534 | } |
drh | f824b41 | 2021-02-20 14:57:16 +0000 | [diff] [blame] | 535 | |
| 536 | /* Attach a With object describing the WITH clause to a Select |
| 537 | ** object describing the query for which the WITH clause is a prefix. |
| 538 | */ |
| 539 | static Select *attachWithToSelect(Parse *pParse, Select *pSelect, With *pWith){ |
| 540 | if( pSelect ){ |
| 541 | pSelect->pWith = pWith; |
| 542 | parserDoubleLinkSelect(pParse, pSelect); |
| 543 | }else{ |
| 544 | sqlite3WithDelete(pParse->db, pWith); |
| 545 | } |
| 546 | return pSelect; |
| 547 | } |
drh | 772460f | 2015-04-16 14:13:12 +0000 | [diff] [blame] | 548 | } |
| 549 | |
drh | 6a8b94e | 2018-05-30 01:14:20 +0000 | [diff] [blame] | 550 | %ifndef SQLITE_OMIT_CTE |
drh | f824b41 | 2021-02-20 14:57:16 +0000 | [diff] [blame] | 551 | select(A) ::= WITH wqlist(W) selectnowith(X). {A = attachWithToSelect(pParse,X,W);} |
| 552 | select(A) ::= WITH RECURSIVE wqlist(W) selectnowith(X). |
| 553 | {A = attachWithToSelect(pParse,X,W);} |
drh | 6a8b94e | 2018-05-30 01:14:20 +0000 | [diff] [blame] | 554 | %endif /* SQLITE_OMIT_CTE */ |
drh | a5746e0 | 2018-04-09 20:36:09 +0000 | [diff] [blame] | 555 | select(A) ::= selectnowith(X). { |
| 556 | Select *p = X; |
| 557 | if( p ){ |
| 558 | parserDoubleLinkSelect(pParse, p); |
| 559 | } |
| 560 | A = p; /*A-overwrites-X*/ |
dan | a9f5c13 | 2014-01-13 16:36:40 +0000 | [diff] [blame] | 561 | } |
dan | 7d562db | 2014-01-11 19:19:36 +0000 | [diff] [blame] | 562 | |
drh | 4dd0d3f | 2016-02-17 01:18:33 +0000 | [diff] [blame] | 563 | selectnowith(A) ::= oneselect(A). |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 564 | %ifndef SQLITE_OMIT_COMPOUND_SELECT |
drh | 4dd0d3f | 2016-02-17 01:18:33 +0000 | [diff] [blame] | 565 | selectnowith(A) ::= selectnowith(A) multiselect_op(Y) oneselect(Z). { |
drh | c0bf493 | 2014-02-19 01:31:02 +0000 | [diff] [blame] | 566 | Select *pRhs = Z; |
drh | 4dd0d3f | 2016-02-17 01:18:33 +0000 | [diff] [blame] | 567 | Select *pLhs = A; |
drh | c0bf493 | 2014-02-19 01:31:02 +0000 | [diff] [blame] | 568 | if( pRhs && pRhs->pPrior ){ |
| 569 | SrcList *pFrom; |
| 570 | Token x; |
| 571 | x.n = 0; |
drh | 772460f | 2015-04-16 14:13:12 +0000 | [diff] [blame] | 572 | parserDoubleLinkSelect(pParse, pRhs); |
drh | d44f8b2 | 2022-04-07 01:11:13 +0000 | [diff] [blame] | 573 | pFrom = sqlite3SrcListAppendFromTerm(pParse,0,0,0,&x,pRhs,0); |
drh | 8c0833f | 2017-11-14 23:48:23 +0000 | [diff] [blame] | 574 | pRhs = sqlite3SelectNew(pParse,0,pFrom,0,0,0,0,0,0); |
drh | c0bf493 | 2014-02-19 01:31:02 +0000 | [diff] [blame] | 575 | } |
| 576 | if( pRhs ){ |
| 577 | pRhs->op = (u8)Y; |
drh | 00d5ab7 | 2015-05-20 00:15:27 +0000 | [diff] [blame] | 578 | pRhs->pPrior = pLhs; |
| 579 | if( ALWAYS(pLhs) ) pLhs->selFlags &= ~SF_MultiValue; |
drh | 772460f | 2015-04-16 14:13:12 +0000 | [diff] [blame] | 580 | pRhs->selFlags &= ~SF_MultiValue; |
drh | d58d327 | 2013-08-05 22:05:02 +0000 | [diff] [blame] | 581 | if( Y!=TK_ALL ) pParse->hasCompound = 1; |
drh | 43b7882 | 2007-06-15 17:03:14 +0000 | [diff] [blame] | 582 | }else{ |
drh | 00d5ab7 | 2015-05-20 00:15:27 +0000 | [diff] [blame] | 583 | sqlite3SelectDelete(pParse->db, pLhs); |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 584 | } |
drh | c0bf493 | 2014-02-19 01:31:02 +0000 | [diff] [blame] | 585 | A = pRhs; |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 586 | } |
drh | 0a36c57 | 2002-02-18 22:49:59 +0000 | [diff] [blame] | 587 | %type multiselect_op {int} |
drh | cf82f0d | 2016-02-17 04:33:10 +0000 | [diff] [blame] | 588 | multiselect_op(A) ::= UNION(OP). {A = @OP; /*A-overwrites-OP*/} |
drh | fd40531 | 2005-11-06 04:06:59 +0000 | [diff] [blame] | 589 | multiselect_op(A) ::= UNION ALL. {A = TK_ALL;} |
drh | cf82f0d | 2016-02-17 04:33:10 +0000 | [diff] [blame] | 590 | multiselect_op(A) ::= EXCEPT|INTERSECT(OP). {A = @OP; /*A-overwrites-OP*/} |
drh | 154d4b2 | 2006-09-21 11:02:16 +0000 | [diff] [blame] | 591 | %endif SQLITE_OMIT_COMPOUND_SELECT |
drh | 550a330 | 2018-07-27 22:14:50 +0000 | [diff] [blame] | 592 | |
drh | fef3776 | 2018-07-10 19:48:35 +0000 | [diff] [blame] | 593 | oneselect(A) ::= SELECT distinct(D) selcollist(W) from(X) where_opt(Y) |
dan | 67a9b8e | 2018-06-22 20:51:35 +0000 | [diff] [blame] | 594 | groupby_opt(P) having_opt(Q) |
dan | e3bf632 | 2018-06-08 20:58:27 +0000 | [diff] [blame] | 595 | orderby_opt(Z) limit_opt(L). { |
drh | 8c0833f | 2017-11-14 23:48:23 +0000 | [diff] [blame] | 596 | A = sqlite3SelectNew(pParse,W,X,Y,P,Q,Z,D,L); |
drh | 550a330 | 2018-07-27 22:14:50 +0000 | [diff] [blame] | 597 | } |
| 598 | %ifndef SQLITE_OMIT_WINDOWFUNC |
| 599 | oneselect(A) ::= SELECT distinct(D) selcollist(W) from(X) where_opt(Y) |
| 600 | groupby_opt(P) having_opt(Q) window_clause(R) |
| 601 | orderby_opt(Z) limit_opt(L). { |
| 602 | A = sqlite3SelectNew(pParse,W,X,Y,P,Q,Z,D,L); |
dan | 6fde179 | 2018-06-15 19:01:35 +0000 | [diff] [blame] | 603 | if( A ){ |
| 604 | A->pWinDefn = R; |
| 605 | }else{ |
| 606 | sqlite3WindowListDelete(pParse->db, R); |
| 607 | } |
drh | 9bb61fe | 2000-06-05 16:01:39 +0000 | [diff] [blame] | 608 | } |
drh | 550a330 | 2018-07-27 22:14:50 +0000 | [diff] [blame] | 609 | %endif |
| 610 | |
| 611 | |
drh | 4dd0d3f | 2016-02-17 01:18:33 +0000 | [diff] [blame] | 612 | oneselect(A) ::= values(A). |
drh | 75593d9 | 2014-01-10 20:46:55 +0000 | [diff] [blame] | 613 | |
| 614 | %type values {Select*} |
| 615 | %destructor values {sqlite3SelectDelete(pParse->db, $$);} |
| 616 | values(A) ::= VALUES LP nexprlist(X) RP. { |
drh | 8c0833f | 2017-11-14 23:48:23 +0000 | [diff] [blame] | 617 | A = sqlite3SelectNew(pParse,X,0,0,0,0,0,SF_Values,0); |
drh | 75593d9 | 2014-01-10 20:46:55 +0000 | [diff] [blame] | 618 | } |
drh | 954733b | 2018-07-27 23:33:16 +0000 | [diff] [blame] | 619 | values(A) ::= values(A) COMMA LP nexprlist(Y) RP. { |
drh | 4dd0d3f | 2016-02-17 01:18:33 +0000 | [diff] [blame] | 620 | Select *pRight, *pLeft = A; |
drh | 8c0833f | 2017-11-14 23:48:23 +0000 | [diff] [blame] | 621 | pRight = sqlite3SelectNew(pParse,Y,0,0,0,0,0,SF_Values|SF_MultiValue,0); |
drh | f3151f0 | 2015-04-16 20:27:09 +0000 | [diff] [blame] | 622 | if( ALWAYS(pLeft) ) pLeft->selFlags &= ~SF_MultiValue; |
drh | 75593d9 | 2014-01-10 20:46:55 +0000 | [diff] [blame] | 623 | if( pRight ){ |
| 624 | pRight->op = TK_ALL; |
drh | 772460f | 2015-04-16 14:13:12 +0000 | [diff] [blame] | 625 | pRight->pPrior = pLeft; |
drh | 75593d9 | 2014-01-10 20:46:55 +0000 | [diff] [blame] | 626 | A = pRight; |
| 627 | }else{ |
drh | 772460f | 2015-04-16 14:13:12 +0000 | [diff] [blame] | 628 | A = pLeft; |
drh | 75593d9 | 2014-01-10 20:46:55 +0000 | [diff] [blame] | 629 | } |
| 630 | } |
drh | 9bb61fe | 2000-06-05 16:01:39 +0000 | [diff] [blame] | 631 | |
| 632 | // The "distinct" nonterminal is true (1) if the DISTINCT keyword is |
| 633 | // present and false (0) if it is not. |
| 634 | // |
drh | 3334d08 | 2015-11-10 13:45:21 +0000 | [diff] [blame] | 635 | %type distinct {int} |
drh | 832ee3d | 2012-12-18 19:36:11 +0000 | [diff] [blame] | 636 | distinct(A) ::= DISTINCT. {A = SF_Distinct;} |
drh | 7cea7f9 | 2015-05-29 01:35:19 +0000 | [diff] [blame] | 637 | distinct(A) ::= ALL. {A = SF_All;} |
drh | efb7251 | 2000-05-31 20:00:52 +0000 | [diff] [blame] | 638 | distinct(A) ::= . {A = 0;} |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 639 | |
drh | 9bb61fe | 2000-06-05 16:01:39 +0000 | [diff] [blame] | 640 | // selcollist is a list of expressions that are to become the return |
drh | 7c917d1 | 2001-12-16 20:05:05 +0000 | [diff] [blame] | 641 | // values of the SELECT statement. The "*" in statements like |
| 642 | // "SELECT * FROM ..." is encoded as a special expression with an |
drh | 1a1d3cd | 2015-11-19 16:33:31 +0000 | [diff] [blame] | 643 | // opcode of TK_ASTERISK. |
drh | 9bb61fe | 2000-06-05 16:01:39 +0000 | [diff] [blame] | 644 | // |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 645 | %type selcollist {ExprList*} |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 646 | %destructor selcollist {sqlite3ExprListDelete(pParse->db, $$);} |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 647 | %type sclp {ExprList*} |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 648 | %destructor sclp {sqlite3ExprListDelete(pParse->db, $$);} |
drh | 4dd0d3f | 2016-02-17 01:18:33 +0000 | [diff] [blame] | 649 | sclp(A) ::= selcollist(A) COMMA. |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 650 | sclp(A) ::= . {A = 0;} |
drh | 1be266b | 2017-12-24 00:18:47 +0000 | [diff] [blame] | 651 | selcollist(A) ::= sclp(A) scanpt(B) expr(X) scanpt(Z) as(Y). { |
| 652 | A = sqlite3ExprListAppend(pParse, A, X); |
drh | b7916a7 | 2009-05-27 10:31:29 +0000 | [diff] [blame] | 653 | if( Y.n>0 ) sqlite3ExprListSetName(pParse, A, &Y, 1); |
drh | 1be266b | 2017-12-24 00:18:47 +0000 | [diff] [blame] | 654 | sqlite3ExprListSetSpan(pParse,A,B,Z); |
drh | 01f3f25 | 2002-05-24 16:14:15 +0000 | [diff] [blame] | 655 | } |
drh | d3f5d61 | 2017-12-24 17:01:54 +0000 | [diff] [blame] | 656 | selcollist(A) ::= sclp(A) scanpt STAR. { |
drh | 1a1d3cd | 2015-11-19 16:33:31 +0000 | [diff] [blame] | 657 | Expr *p = sqlite3Expr(pParse->db, TK_ASTERISK, 0); |
drh | 4dd0d3f | 2016-02-17 01:18:33 +0000 | [diff] [blame] | 658 | A = sqlite3ExprListAppend(pParse, A, p); |
drh | 7c917d1 | 2001-12-16 20:05:05 +0000 | [diff] [blame] | 659 | } |
drh | 1be266b | 2017-12-24 00:18:47 +0000 | [diff] [blame] | 660 | selcollist(A) ::= sclp(A) scanpt nm(X) DOT STAR. { |
drh | abfd35e | 2016-12-06 22:47:23 +0000 | [diff] [blame] | 661 | Expr *pRight = sqlite3PExpr(pParse, TK_ASTERISK, 0, 0); |
drh | 796588a | 2022-02-05 21:49:47 +0000 | [diff] [blame] | 662 | Expr *pLeft = tokenExpr(pParse, TK_ID, X); |
drh | abfd35e | 2016-12-06 22:47:23 +0000 | [diff] [blame] | 663 | Expr *pDot = sqlite3PExpr(pParse, TK_DOT, pLeft, pRight); |
drh | 4dd0d3f | 2016-02-17 01:18:33 +0000 | [diff] [blame] | 664 | A = sqlite3ExprListAppend(pParse,A, pDot); |
drh | 5447322 | 2002-04-04 02:10:55 +0000 | [diff] [blame] | 665 | } |
drh | 01f3f25 | 2002-05-24 16:14:15 +0000 | [diff] [blame] | 666 | |
| 667 | // An option "AS <id>" phrase that can follow one of the expressions that |
| 668 | // define the result set, or one of the tables in the FROM clause. |
| 669 | // |
| 670 | %type as {Token} |
drh | 74ad7fe | 2004-10-07 03:06:28 +0000 | [diff] [blame] | 671 | as(X) ::= AS nm(Y). {X = Y;} |
drh | 4dd0d3f | 2016-02-17 01:18:33 +0000 | [diff] [blame] | 672 | as(X) ::= ids(X). |
drh | 986dde7 | 2016-02-29 13:37:21 +0000 | [diff] [blame] | 673 | as(X) ::= . {X.n = 0; X.z = 0;} |
drh | 9bb61fe | 2000-06-05 16:01:39 +0000 | [diff] [blame] | 674 | |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 675 | |
drh | ad3cab5 | 2002-05-24 02:04:32 +0000 | [diff] [blame] | 676 | %type seltablist {SrcList*} |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 677 | %destructor seltablist {sqlite3SrcListDelete(pParse->db, $$);} |
drh | ad3cab5 | 2002-05-24 02:04:32 +0000 | [diff] [blame] | 678 | %type stl_prefix {SrcList*} |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 679 | %destructor stl_prefix {sqlite3SrcListDelete(pParse->db, $$);} |
drh | ad3cab5 | 2002-05-24 02:04:32 +0000 | [diff] [blame] | 680 | %type from {SrcList*} |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 681 | %destructor from {sqlite3SrcListDelete(pParse->db, $$);} |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 682 | |
drh | 01f3f25 | 2002-05-24 16:14:15 +0000 | [diff] [blame] | 683 | // A complete FROM clause. |
| 684 | // |
dan | 69887c9 | 2020-04-27 20:55:33 +0000 | [diff] [blame] | 685 | from(A) ::= . {A = 0;} |
drh | fbdc7f6 | 2008-12-03 23:23:40 +0000 | [diff] [blame] | 686 | from(A) ::= FROM seltablist(X). { |
drh | 61dfc31 | 2006-12-16 16:25:15 +0000 | [diff] [blame] | 687 | A = X; |
drh | fdc621a | 2022-04-16 19:13:16 +0000 | [diff] [blame] | 688 | sqlite3SrcListShiftJoinType(pParse,A); |
drh | 61dfc31 | 2006-12-16 16:25:15 +0000 | [diff] [blame] | 689 | } |
drh | 01f3f25 | 2002-05-24 16:14:15 +0000 | [diff] [blame] | 690 | |
| 691 | // "seltablist" is a "Select Table List" - the content of the FROM clause |
| 692 | // in a SELECT statement. "stl_prefix" is a prefix of this list. |
| 693 | // |
drh | 4dd0d3f | 2016-02-17 01:18:33 +0000 | [diff] [blame] | 694 | stl_prefix(A) ::= seltablist(A) joinop(Y). { |
drh | 8a48b9c | 2015-08-19 15:20:00 +0000 | [diff] [blame] | 695 | if( ALWAYS(A && A->nSrc>0) ) A->a[A->nSrc-1].fg.jointype = (u8)Y; |
drh | 01f3f25 | 2002-05-24 16:14:15 +0000 | [diff] [blame] | 696 | } |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 697 | stl_prefix(A) ::= . {A = 0;} |
drh | d44f8b2 | 2022-04-07 01:11:13 +0000 | [diff] [blame] | 698 | seltablist(A) ::= stl_prefix(A) nm(Y) dbnm(D) as(Z) on_using(N). { |
| 699 | A = sqlite3SrcListAppendFromTerm(pParse,A,&Y,&D,&Z,0,&N); |
drh | 200adc9 | 2022-04-06 19:46:20 +0000 | [diff] [blame] | 700 | } |
drh | d44f8b2 | 2022-04-07 01:11:13 +0000 | [diff] [blame] | 701 | seltablist(A) ::= stl_prefix(A) nm(Y) dbnm(D) as(Z) indexed_by(I) on_using(N). { |
| 702 | A = sqlite3SrcListAppendFromTerm(pParse,A,&Y,&D,&Z,0,&N); |
danielk1977 | b1c685b | 2008-10-06 16:18:39 +0000 | [diff] [blame] | 703 | sqlite3SrcListIndexedBy(pParse, A, &I); |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 704 | } |
drh | d44f8b2 | 2022-04-07 01:11:13 +0000 | [diff] [blame] | 705 | seltablist(A) ::= stl_prefix(A) nm(Y) dbnm(D) LP exprlist(E) RP as(Z) on_using(N). { |
| 706 | A = sqlite3SrcListAppendFromTerm(pParse,A,&Y,&D,&Z,0,&N); |
drh | 01d230c | 2015-08-19 17:11:37 +0000 | [diff] [blame] | 707 | sqlite3SrcListFuncArgs(pParse, A, E); |
| 708 | } |
drh | 51522cd | 2005-01-20 13:36:19 +0000 | [diff] [blame] | 709 | %ifndef SQLITE_OMIT_SUBQUERY |
drh | d44f8b2 | 2022-04-07 01:11:13 +0000 | [diff] [blame] | 710 | seltablist(A) ::= stl_prefix(A) LP select(S) RP as(Z) on_using(N). { |
| 711 | A = sqlite3SrcListAppendFromTerm(pParse,A,0,0,&Z,S,&N); |
drh | d5feede | 2002-05-08 21:46:14 +0000 | [diff] [blame] | 712 | } |
drh | d44f8b2 | 2022-04-07 01:11:13 +0000 | [diff] [blame] | 713 | seltablist(A) ::= stl_prefix(A) LP seltablist(F) RP as(Z) on_using(N). { |
| 714 | if( A==0 && Z.n==0 && N.pOn==0 && N.pUsing==0 ){ |
drh | fbdc7f6 | 2008-12-03 23:23:40 +0000 | [diff] [blame] | 715 | A = F; |
drh | 832ee3d | 2012-12-18 19:36:11 +0000 | [diff] [blame] | 716 | }else if( F->nSrc==1 ){ |
drh | d44f8b2 | 2022-04-07 01:11:13 +0000 | [diff] [blame] | 717 | A = sqlite3SrcListAppendFromTerm(pParse,A,0,0,&Z,0,&N); |
drh | 832ee3d | 2012-12-18 19:36:11 +0000 | [diff] [blame] | 718 | if( A ){ |
drh | 7601294 | 2021-02-21 21:04:54 +0000 | [diff] [blame] | 719 | SrcItem *pNew = &A->a[A->nSrc-1]; |
| 720 | SrcItem *pOld = F->a; |
drh | 832ee3d | 2012-12-18 19:36:11 +0000 | [diff] [blame] | 721 | pNew->zName = pOld->zName; |
| 722 | pNew->zDatabase = pOld->zDatabase; |
drh | 3c449c6 | 2013-04-30 14:06:57 +0000 | [diff] [blame] | 723 | pNew->pSelect = pOld->pSelect; |
drh | 825a6bf | 2022-04-21 14:08:29 +0000 | [diff] [blame] | 724 | if( pNew->pSelect && (pNew->pSelect->selFlags & SF_NestedFrom)!=0 ){ |
| 725 | pNew->fg.isNestedFrom = 1; |
| 726 | } |
drh | 4a5cff7 | 2018-12-03 01:47:41 +0000 | [diff] [blame] | 727 | if( pOld->fg.isTabFunc ){ |
| 728 | pNew->u1.pFuncArg = pOld->u1.pFuncArg; |
| 729 | pOld->u1.pFuncArg = 0; |
| 730 | pOld->fg.isTabFunc = 0; |
| 731 | pNew->fg.isTabFunc = 1; |
| 732 | } |
drh | 832ee3d | 2012-12-18 19:36:11 +0000 | [diff] [blame] | 733 | pOld->zName = pOld->zDatabase = 0; |
drh | 3c449c6 | 2013-04-30 14:06:57 +0000 | [diff] [blame] | 734 | pOld->pSelect = 0; |
drh | 832ee3d | 2012-12-18 19:36:11 +0000 | [diff] [blame] | 735 | } |
| 736 | sqlite3SrcListDelete(pParse->db, F); |
drh | fbdc7f6 | 2008-12-03 23:23:40 +0000 | [diff] [blame] | 737 | }else{ |
| 738 | Select *pSubquery; |
drh | fdc621a | 2022-04-16 19:13:16 +0000 | [diff] [blame] | 739 | sqlite3SrcListShiftJoinType(pParse,F); |
drh | 8c0833f | 2017-11-14 23:48:23 +0000 | [diff] [blame] | 740 | pSubquery = sqlite3SelectNew(pParse,0,F,0,0,0,0,SF_NestedFrom,0); |
drh | d44f8b2 | 2022-04-07 01:11:13 +0000 | [diff] [blame] | 741 | A = sqlite3SrcListAppendFromTerm(pParse,A,0,0,&Z,pSubquery,&N); |
drh | fbdc7f6 | 2008-12-03 23:23:40 +0000 | [diff] [blame] | 742 | } |
| 743 | } |
drh | 154d4b2 | 2006-09-21 11:02:16 +0000 | [diff] [blame] | 744 | %endif SQLITE_OMIT_SUBQUERY |
drh | b733d03 | 2004-01-24 20:18:12 +0000 | [diff] [blame] | 745 | |
drh | 113088e | 2003-03-20 01:16:58 +0000 | [diff] [blame] | 746 | %type dbnm {Token} |
| 747 | dbnm(A) ::= . {A.z=0; A.n=0;} |
| 748 | dbnm(A) ::= DOT nm(X). {A = X;} |
| 749 | |
drh | 74ad7fe | 2004-10-07 03:06:28 +0000 | [diff] [blame] | 750 | %type fullname {SrcList*} |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 751 | %destructor fullname {sqlite3SrcListDelete(pParse->db, $$);} |
dan | c9461ec | 2018-08-29 21:00:16 +0000 | [diff] [blame] | 752 | fullname(A) ::= nm(X). { |
drh | 29c992c | 2019-01-17 15:40:41 +0000 | [diff] [blame] | 753 | A = sqlite3SrcListAppend(pParse,0,&X,0); |
dan | c9461ec | 2018-08-29 21:00:16 +0000 | [diff] [blame] | 754 | if( IN_RENAME_OBJECT && A ) sqlite3RenameTokenMap(pParse, A->a[0].zName, &X); |
| 755 | } |
| 756 | fullname(A) ::= nm(X) DOT nm(Y). { |
drh | 29c992c | 2019-01-17 15:40:41 +0000 | [diff] [blame] | 757 | A = sqlite3SrcListAppend(pParse,0,&X,&Y); |
dan | c9461ec | 2018-08-29 21:00:16 +0000 | [diff] [blame] | 758 | if( IN_RENAME_OBJECT && A ) sqlite3RenameTokenMap(pParse, A->a[0].zName, &Y); |
| 759 | } |
drh | 74ad7fe | 2004-10-07 03:06:28 +0000 | [diff] [blame] | 760 | |
drh | 5e3a6eb | 2018-04-19 11:45:16 +0000 | [diff] [blame] | 761 | %type xfullname {SrcList*} |
| 762 | %destructor xfullname {sqlite3SrcListDelete(pParse->db, $$);} |
| 763 | xfullname(A) ::= nm(X). |
drh | 29c992c | 2019-01-17 15:40:41 +0000 | [diff] [blame] | 764 | {A = sqlite3SrcListAppend(pParse,0,&X,0); /*A-overwrites-X*/} |
drh | 5e3a6eb | 2018-04-19 11:45:16 +0000 | [diff] [blame] | 765 | xfullname(A) ::= nm(X) DOT nm(Y). |
drh | 29c992c | 2019-01-17 15:40:41 +0000 | [diff] [blame] | 766 | {A = sqlite3SrcListAppend(pParse,0,&X,&Y); /*A-overwrites-X*/} |
drh | 5e3a6eb | 2018-04-19 11:45:16 +0000 | [diff] [blame] | 767 | xfullname(A) ::= nm(X) DOT nm(Y) AS nm(Z). { |
drh | 29c992c | 2019-01-17 15:40:41 +0000 | [diff] [blame] | 768 | A = sqlite3SrcListAppend(pParse,0,&X,&Y); /*A-overwrites-X*/ |
drh | 5e3a6eb | 2018-04-19 11:45:16 +0000 | [diff] [blame] | 769 | if( A ) A->a[0].zAlias = sqlite3NameFromToken(pParse->db, &Z); |
| 770 | } |
| 771 | xfullname(A) ::= nm(X) AS nm(Z). { |
drh | 29c992c | 2019-01-17 15:40:41 +0000 | [diff] [blame] | 772 | A = sqlite3SrcListAppend(pParse,0,&X,0); /*A-overwrites-X*/ |
drh | 5e3a6eb | 2018-04-19 11:45:16 +0000 | [diff] [blame] | 773 | if( A ) A->a[0].zAlias = sqlite3NameFromToken(pParse->db, &Z); |
| 774 | } |
| 775 | |
drh | 01f3f25 | 2002-05-24 16:14:15 +0000 | [diff] [blame] | 776 | %type joinop {int} |
drh | fd40531 | 2005-11-06 04:06:59 +0000 | [diff] [blame] | 777 | joinop(X) ::= COMMA|JOIN. { X = JT_INNER; } |
drh | cf82f0d | 2016-02-17 04:33:10 +0000 | [diff] [blame] | 778 | joinop(X) ::= JOIN_KW(A) JOIN. |
| 779 | {X = sqlite3JoinType(pParse,&A,0,0); /*X-overwrites-A*/} |
| 780 | joinop(X) ::= JOIN_KW(A) nm(B) JOIN. |
| 781 | {X = sqlite3JoinType(pParse,&A,&B,0); /*X-overwrites-A*/} |
drh | 5ad1a6c | 2002-07-01 12:27:09 +0000 | [diff] [blame] | 782 | joinop(X) ::= JOIN_KW(A) nm(B) nm(C) JOIN. |
drh | cf82f0d | 2016-02-17 04:33:10 +0000 | [diff] [blame] | 783 | {X = sqlite3JoinType(pParse,&A,&B,&C);/*X-overwrites-A*/} |
drh | 01f3f25 | 2002-05-24 16:14:15 +0000 | [diff] [blame] | 784 | |
drh | 26cf56f | 2018-04-06 19:36:49 +0000 | [diff] [blame] | 785 | // There is a parsing abiguity in an upsert statement that uses a |
| 786 | // SELECT on the RHS of a the INSERT: |
| 787 | // |
| 788 | // INSERT INTO tab SELECT * FROM aaa JOIN bbb ON CONFLICT ... |
| 789 | // here ----^^ |
| 790 | // |
| 791 | // When the ON token is encountered, the parser does not know if it is |
| 792 | // the beginning of an ON CONFLICT clause, or the beginning of an ON |
| 793 | // clause associated with the JOIN. The conflict is resolved in favor |
| 794 | // of the JOIN. If an ON CONFLICT clause is intended, insert a dummy |
| 795 | // WHERE clause in between, like this: |
| 796 | // |
| 797 | // INSERT INTO tab SELECT * FROM aaa JOIN bbb WHERE true ON CONFLICT ... |
| 798 | // |
drh | d44f8b2 | 2022-04-07 01:11:13 +0000 | [diff] [blame] | 799 | // The [AND] and [OR] precedence marks in the rules for on_using cause the |
drh | 26cf56f | 2018-04-06 19:36:49 +0000 | [diff] [blame] | 800 | // ON in this context to always be interpreted as belonging to the JOIN. |
| 801 | // |
drh | d44f8b2 | 2022-04-07 01:11:13 +0000 | [diff] [blame] | 802 | %type on_using {OnOrUsing} |
| 803 | //%destructor on_using {sqlite3ClearOnOrUsing(pParse->db, &$$);} |
| 804 | on_using(N) ::= ON expr(E). {N.pOn = E; N.pUsing = 0;} |
| 805 | on_using(N) ::= USING LP idlist(L) RP. {N.pOn = 0; N.pUsing = L;} |
| 806 | on_using(N) ::= . [OR] {N.pOn = 0; N.pUsing = 0;} |
drh | 01f3f25 | 2002-05-24 16:14:15 +0000 | [diff] [blame] | 807 | |
danielk1977 | 85574e3 | 2008-10-06 05:32:18 +0000 | [diff] [blame] | 808 | // Note that this block abuses the Token type just a little. If there is |
| 809 | // no "INDEXED BY" clause, the returned token is empty (z==0 && n==0). If |
| 810 | // there is an INDEXED BY clause, then the token is populated as per normal, |
| 811 | // with z pointing to the token data and n containing the number of bytes |
| 812 | // in the token. |
| 813 | // |
| 814 | // If there is a "NOT INDEXED" clause, then (z==0 && n==1), which is |
danielk1977 | b1c685b | 2008-10-06 16:18:39 +0000 | [diff] [blame] | 815 | // normally illegal. The sqlite3SrcListIndexedBy() function |
danielk1977 | 85574e3 | 2008-10-06 05:32:18 +0000 | [diff] [blame] | 816 | // recognizes and interprets this as a special case. |
| 817 | // |
| 818 | %type indexed_opt {Token} |
drh | 200adc9 | 2022-04-06 19:46:20 +0000 | [diff] [blame] | 819 | %type indexed_by {Token} |
danielk1977 | 85574e3 | 2008-10-06 05:32:18 +0000 | [diff] [blame] | 820 | indexed_opt(A) ::= . {A.z=0; A.n=0;} |
drh | 200adc9 | 2022-04-06 19:46:20 +0000 | [diff] [blame] | 821 | indexed_opt(A) ::= indexed_by(A). |
| 822 | indexed_by(A) ::= INDEXED BY nm(X). {A = X;} |
| 823 | indexed_by(A) ::= NOT INDEXED. {A.z=0; A.n=1;} |
danielk1977 | 85574e3 | 2008-10-06 05:32:18 +0000 | [diff] [blame] | 824 | |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 825 | %type orderby_opt {ExprList*} |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 826 | %destructor orderby_opt {sqlite3ExprListDelete(pParse->db, $$);} |
drh | 108aa00 | 2015-08-24 20:21:20 +0000 | [diff] [blame] | 827 | |
| 828 | // the sortlist non-terminal stores a list of expression where each |
| 829 | // expression is optionally followed by ASC or DESC to indicate the |
| 830 | // sort order. |
| 831 | // |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 832 | %type sortlist {ExprList*} |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 833 | %destructor sortlist {sqlite3ExprListDelete(pParse->db, $$);} |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 834 | |
| 835 | orderby_opt(A) ::= . {A = 0;} |
| 836 | orderby_opt(A) ::= ORDER BY sortlist(X). {A = X;} |
dan | 6e11892 | 2019-08-12 16:36:38 +0000 | [diff] [blame] | 837 | sortlist(A) ::= sortlist(A) COMMA expr(Y) sortorder(Z) nulls(X). { |
drh | 1be266b | 2017-12-24 00:18:47 +0000 | [diff] [blame] | 838 | A = sqlite3ExprListAppend(pParse,A,Y); |
dan | 6e11892 | 2019-08-12 16:36:38 +0000 | [diff] [blame] | 839 | sqlite3ExprListSetSortOrder(A,Z,X); |
drh | 9bb61fe | 2000-06-05 16:01:39 +0000 | [diff] [blame] | 840 | } |
dan | 6e11892 | 2019-08-12 16:36:38 +0000 | [diff] [blame] | 841 | sortlist(A) ::= expr(Y) sortorder(Z) nulls(X). { |
drh | 1be266b | 2017-12-24 00:18:47 +0000 | [diff] [blame] | 842 | A = sqlite3ExprListAppend(pParse,0,Y); /*A-overwrites-Y*/ |
dan | 6e11892 | 2019-08-12 16:36:38 +0000 | [diff] [blame] | 843 | sqlite3ExprListSetSortOrder(A,Z,X); |
drh | 9bb61fe | 2000-06-05 16:01:39 +0000 | [diff] [blame] | 844 | } |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 845 | |
| 846 | %type sortorder {int} |
| 847 | |
drh | 8e2ca02 | 2002-06-17 17:07:19 +0000 | [diff] [blame] | 848 | sortorder(A) ::= ASC. {A = SQLITE_SO_ASC;} |
| 849 | sortorder(A) ::= DESC. {A = SQLITE_SO_DESC;} |
drh | bc622bc | 2015-08-24 15:39:42 +0000 | [diff] [blame] | 850 | sortorder(A) ::= . {A = SQLITE_SO_UNDEFINED;} |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 851 | |
dan | 6e11892 | 2019-08-12 16:36:38 +0000 | [diff] [blame] | 852 | %type nulls {int} |
| 853 | nulls(A) ::= NULLS FIRST. {A = SQLITE_SO_ASC;} |
| 854 | nulls(A) ::= NULLS LAST. {A = SQLITE_SO_DESC;} |
| 855 | nulls(A) ::= . {A = SQLITE_SO_UNDEFINED;} |
| 856 | |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 857 | %type groupby_opt {ExprList*} |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 858 | %destructor groupby_opt {sqlite3ExprListDelete(pParse->db, $$);} |
drh | 6206d50 | 2000-06-19 19:09:08 +0000 | [diff] [blame] | 859 | groupby_opt(A) ::= . {A = 0;} |
drh | 9245c24 | 2007-06-20 12:18:31 +0000 | [diff] [blame] | 860 | groupby_opt(A) ::= GROUP BY nexprlist(X). {A = X;} |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 861 | |
| 862 | %type having_opt {Expr*} |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 863 | %destructor having_opt {sqlite3ExprDelete(pParse->db, $$);} |
drh | 6206d50 | 2000-06-19 19:09:08 +0000 | [diff] [blame] | 864 | having_opt(A) ::= . {A = 0;} |
drh | 1be266b | 2017-12-24 00:18:47 +0000 | [diff] [blame] | 865 | having_opt(A) ::= HAVING expr(X). {A = X;} |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 866 | |
drh | 8c0833f | 2017-11-14 23:48:23 +0000 | [diff] [blame] | 867 | %type limit_opt {Expr*} |
drh | 1592659 | 2007-04-06 15:02:13 +0000 | [diff] [blame] | 868 | |
| 869 | // The destructor for limit_opt will never fire in the current grammar. |
| 870 | // The limit_opt non-terminal only occurs at the end of a single production |
| 871 | // rule for SELECT statements. As soon as the rule that create the |
| 872 | // limit_opt non-terminal reduces, the SELECT statement rule will also |
| 873 | // reduce. So there is never a limit_opt non-terminal on the stack |
| 874 | // except as a transient. So there is never anything to destroy. |
| 875 | // |
drh | 8c0833f | 2017-11-14 23:48:23 +0000 | [diff] [blame] | 876 | //%destructor limit_opt {sqlite3ExprDelete(pParse->db, $$);} |
| 877 | limit_opt(A) ::= . {A = 0;} |
| 878 | limit_opt(A) ::= LIMIT expr(X). |
drh | 1be266b | 2017-12-24 00:18:47 +0000 | [diff] [blame] | 879 | {A = sqlite3PExpr(pParse,TK_LIMIT,X,0);} |
danielk1977 | a2dc3b1 | 2005-02-05 12:48:48 +0000 | [diff] [blame] | 880 | limit_opt(A) ::= LIMIT expr(X) OFFSET expr(Y). |
drh | 1be266b | 2017-12-24 00:18:47 +0000 | [diff] [blame] | 881 | {A = sqlite3PExpr(pParse,TK_LIMIT,X,Y);} |
danielk1977 | a2dc3b1 | 2005-02-05 12:48:48 +0000 | [diff] [blame] | 882 | limit_opt(A) ::= LIMIT expr(X) COMMA expr(Y). |
drh | 1be266b | 2017-12-24 00:18:47 +0000 | [diff] [blame] | 883 | {A = sqlite3PExpr(pParse,TK_LIMIT,Y,X);} |
drh | 9bbca4c | 2001-11-06 04:00:18 +0000 | [diff] [blame] | 884 | |
drh | 382c024 | 2001-10-06 16:33:02 +0000 | [diff] [blame] | 885 | /////////////////////////// The DELETE statement ///////////////////////////// |
| 886 | // |
drh | c601f10 | 2020-07-03 17:24:35 +0000 | [diff] [blame] | 887 | %if SQLITE_ENABLE_UPDATE_DELETE_LIMIT || SQLITE_UDL_CAPABLE_PARSER |
drh | 2053f31 | 2021-01-12 20:16:31 +0000 | [diff] [blame] | 888 | cmd ::= with DELETE FROM xfullname(X) indexed_opt(I) where_opt_ret(W) |
drh | 931577f | 2008-10-10 14:27:16 +0000 | [diff] [blame] | 889 | orderby_opt(O) limit_opt(L). { |
danielk1977 | b1c685b | 2008-10-06 16:18:39 +0000 | [diff] [blame] | 890 | sqlite3SrcListIndexedBy(pParse, X, &I); |
drh | 6a0db87 | 2019-01-31 02:42:47 +0000 | [diff] [blame] | 891 | #ifndef SQLITE_ENABLE_UPDATE_DELETE_LIMIT |
drh | c601f10 | 2020-07-03 17:24:35 +0000 | [diff] [blame] | 892 | if( O || L ){ |
| 893 | updateDeleteLimitError(pParse,O,L); |
| 894 | O = 0; |
| 895 | L = 0; |
| 896 | } |
drh | 6a0db87 | 2019-01-31 02:42:47 +0000 | [diff] [blame] | 897 | #endif |
drh | 8c0833f | 2017-11-14 23:48:23 +0000 | [diff] [blame] | 898 | sqlite3DeleteFrom(pParse,X,W,O,L); |
danielk1977 | b1c685b | 2008-10-06 16:18:39 +0000 | [diff] [blame] | 899 | } |
drh | c601f10 | 2020-07-03 17:24:35 +0000 | [diff] [blame] | 900 | %else |
drh | 2053f31 | 2021-01-12 20:16:31 +0000 | [diff] [blame] | 901 | cmd ::= with DELETE FROM xfullname(X) indexed_opt(I) where_opt_ret(W). { |
shane | 4281bd4 | 2008-10-07 05:27:11 +0000 | [diff] [blame] | 902 | sqlite3SrcListIndexedBy(pParse, X, &I); |
drh | 8c0833f | 2017-11-14 23:48:23 +0000 | [diff] [blame] | 903 | sqlite3DeleteFrom(pParse,X,W,0,0); |
shane | 4281bd4 | 2008-10-07 05:27:11 +0000 | [diff] [blame] | 904 | } |
| 905 | %endif |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 906 | |
| 907 | %type where_opt {Expr*} |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 908 | %destructor where_opt {sqlite3ExprDelete(pParse->db, $$);} |
drh | 2053f31 | 2021-01-12 20:16:31 +0000 | [diff] [blame] | 909 | %type where_opt_ret {Expr*} |
| 910 | %destructor where_opt_ret {sqlite3ExprDelete(pParse->db, $$);} |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 911 | |
| 912 | where_opt(A) ::= . {A = 0;} |
drh | 1be266b | 2017-12-24 00:18:47 +0000 | [diff] [blame] | 913 | where_opt(A) ::= WHERE expr(X). {A = X;} |
drh | 2053f31 | 2021-01-12 20:16:31 +0000 | [diff] [blame] | 914 | where_opt_ret(A) ::= . {A = 0;} |
| 915 | where_opt_ret(A) ::= WHERE expr(X). {A = X;} |
| 916 | where_opt_ret(A) ::= RETURNING selcollist(X). |
| 917 | {sqlite3AddReturning(pParse,X); A = 0;} |
| 918 | where_opt_ret(A) ::= WHERE expr(X) RETURNING selcollist(Y). |
| 919 | {sqlite3AddReturning(pParse,Y); A = X;} |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 920 | |
drh | 382c024 | 2001-10-06 16:33:02 +0000 | [diff] [blame] | 921 | ////////////////////////// The UPDATE command //////////////////////////////// |
| 922 | // |
drh | c601f10 | 2020-07-03 17:24:35 +0000 | [diff] [blame] | 923 | %if SQLITE_ENABLE_UPDATE_DELETE_LIMIT || SQLITE_UDL_CAPABLE_PARSER |
dan | 69887c9 | 2020-04-27 20:55:33 +0000 | [diff] [blame] | 924 | cmd ::= with UPDATE orconf(R) xfullname(X) indexed_opt(I) SET setlist(Y) from(F) |
drh | b835247 | 2021-01-29 19:32:17 +0000 | [diff] [blame] | 925 | where_opt_ret(W) orderby_opt(O) limit_opt(L). { |
danielk1977 | b1c685b | 2008-10-06 16:18:39 +0000 | [diff] [blame] | 926 | sqlite3SrcListIndexedBy(pParse, X, &I); |
drh | 1943005 | 2022-05-28 14:03:23 +0000 | [diff] [blame] | 927 | if( F ){ |
| 928 | SrcList *pFromClause = F; |
| 929 | if( pFromClause->nSrc>1 ){ |
| 930 | Select *pSubquery; |
| 931 | Token as; |
| 932 | pSubquery = sqlite3SelectNew(pParse,0,pFromClause,0,0,0,0,SF_NestedFrom,0); |
| 933 | as.n = 0; |
| 934 | as.z = 0; |
| 935 | pFromClause = sqlite3SrcListAppendFromTerm(pParse,0,0,0,&as,pSubquery,0); |
| 936 | } |
| 937 | X = sqlite3SrcListAppendList(pParse, X, pFromClause); |
| 938 | } |
drh | b1a6c3c | 2008-03-20 16:30:17 +0000 | [diff] [blame] | 939 | sqlite3ExprListCheckLength(pParse,Y,"set list"); |
drh | c601f10 | 2020-07-03 17:24:35 +0000 | [diff] [blame] | 940 | #ifndef SQLITE_ENABLE_UPDATE_DELETE_LIMIT |
| 941 | if( O || L ){ |
| 942 | updateDeleteLimitError(pParse,O,L); |
| 943 | O = 0; |
| 944 | L = 0; |
| 945 | } |
| 946 | #endif |
drh | eac9fab | 2018-04-16 13:00:50 +0000 | [diff] [blame] | 947 | sqlite3Update(pParse,X,Y,W,R,O,L,0); |
danielk1977 | 7a15a4b | 2007-05-08 17:54:43 +0000 | [diff] [blame] | 948 | } |
drh | c601f10 | 2020-07-03 17:24:35 +0000 | [diff] [blame] | 949 | %else |
dan | 69887c9 | 2020-04-27 20:55:33 +0000 | [diff] [blame] | 950 | cmd ::= with UPDATE orconf(R) xfullname(X) indexed_opt(I) SET setlist(Y) from(F) |
drh | b835247 | 2021-01-29 19:32:17 +0000 | [diff] [blame] | 951 | where_opt_ret(W). { |
shane | 4281bd4 | 2008-10-07 05:27:11 +0000 | [diff] [blame] | 952 | sqlite3SrcListIndexedBy(pParse, X, &I); |
| 953 | sqlite3ExprListCheckLength(pParse,Y,"set list"); |
drh | fb98dac | 2022-05-25 02:32:11 +0000 | [diff] [blame] | 954 | if( F ){ |
| 955 | SrcList *pFromClause = F; |
| 956 | if( pFromClause->nSrc>1 ){ |
| 957 | Select *pSubquery; |
| 958 | Token as; |
| 959 | pSubquery = sqlite3SelectNew(pParse,0,pFromClause,0,0,0,0,SF_NestedFrom,0); |
| 960 | as.n = 0; |
| 961 | as.z = 0; |
| 962 | pFromClause = sqlite3SrcListAppendFromTerm(pParse,0,0,0,&as,pSubquery,0); |
| 963 | } |
| 964 | X = sqlite3SrcListAppendList(pParse, X, pFromClause); |
| 965 | } |
drh | eac9fab | 2018-04-16 13:00:50 +0000 | [diff] [blame] | 966 | sqlite3Update(pParse,X,Y,W,R,0,0,0); |
shane | 4281bd4 | 2008-10-07 05:27:11 +0000 | [diff] [blame] | 967 | } |
| 968 | %endif |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 969 | |
drh | c601f10 | 2020-07-03 17:24:35 +0000 | [diff] [blame] | 970 | |
| 971 | |
drh | f8db1bc | 2005-04-22 02:38:37 +0000 | [diff] [blame] | 972 | %type setlist {ExprList*} |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 973 | %destructor setlist {sqlite3ExprListDelete(pParse->db, $$);} |
drh | f8db1bc | 2005-04-22 02:38:37 +0000 | [diff] [blame] | 974 | |
drh | 4dd0d3f | 2016-02-17 01:18:33 +0000 | [diff] [blame] | 975 | setlist(A) ::= setlist(A) COMMA nm(X) EQ expr(Y). { |
drh | 1be266b | 2017-12-24 00:18:47 +0000 | [diff] [blame] | 976 | A = sqlite3ExprListAppend(pParse, A, Y); |
drh | b7916a7 | 2009-05-27 10:31:29 +0000 | [diff] [blame] | 977 | sqlite3ExprListSetName(pParse, A, &X, 1); |
| 978 | } |
drh | a1251bc | 2016-08-20 00:51:37 +0000 | [diff] [blame] | 979 | setlist(A) ::= setlist(A) COMMA LP idlist(X) RP EQ expr(Y). { |
drh | 1be266b | 2017-12-24 00:18:47 +0000 | [diff] [blame] | 980 | A = sqlite3ExprListAppendVector(pParse, A, X, Y); |
drh | a1251bc | 2016-08-20 00:51:37 +0000 | [diff] [blame] | 981 | } |
drh | b7916a7 | 2009-05-27 10:31:29 +0000 | [diff] [blame] | 982 | setlist(A) ::= nm(X) EQ expr(Y). { |
drh | 1be266b | 2017-12-24 00:18:47 +0000 | [diff] [blame] | 983 | A = sqlite3ExprListAppend(pParse, 0, Y); |
drh | b7916a7 | 2009-05-27 10:31:29 +0000 | [diff] [blame] | 984 | sqlite3ExprListSetName(pParse, A, &X, 1); |
| 985 | } |
drh | a1251bc | 2016-08-20 00:51:37 +0000 | [diff] [blame] | 986 | setlist(A) ::= LP idlist(X) RP EQ expr(Y). { |
drh | 1be266b | 2017-12-24 00:18:47 +0000 | [diff] [blame] | 987 | A = sqlite3ExprListAppendVector(pParse, 0, X, Y); |
drh | a1251bc | 2016-08-20 00:51:37 +0000 | [diff] [blame] | 988 | } |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 989 | |
drh | 382c024 | 2001-10-06 16:33:02 +0000 | [diff] [blame] | 990 | ////////////////////////// The INSERT command ///////////////////////////////// |
| 991 | // |
drh | 5e3a6eb | 2018-04-19 11:45:16 +0000 | [diff] [blame] | 992 | cmd ::= with insert_cmd(R) INTO xfullname(X) idlist_opt(F) select(S) |
drh | 2c2e844 | 2018-04-07 15:04:05 +0000 | [diff] [blame] | 993 | upsert(U). { |
drh | 46d2e5c | 2018-04-12 13:15:43 +0000 | [diff] [blame] | 994 | sqlite3Insert(pParse, X, S, F, R, U); |
dan | 4e9119d | 2014-01-13 15:12:23 +0000 | [diff] [blame] | 995 | } |
drh | 2053f31 | 2021-01-12 20:16:31 +0000 | [diff] [blame] | 996 | cmd ::= with insert_cmd(R) INTO xfullname(X) idlist_opt(F) DEFAULT VALUES returning. |
dan | 4e9119d | 2014-01-13 15:12:23 +0000 | [diff] [blame] | 997 | { |
drh | 2c2e844 | 2018-04-07 15:04:05 +0000 | [diff] [blame] | 998 | sqlite3Insert(pParse, X, 0, F, R, 0); |
dan | 4e9119d | 2014-01-13 15:12:23 +0000 | [diff] [blame] | 999 | } |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 1000 | |
drh | 46d2e5c | 2018-04-12 13:15:43 +0000 | [diff] [blame] | 1001 | %type upsert {Upsert*} |
drh | 1c19848 | 2020-12-14 13:52:03 +0000 | [diff] [blame] | 1002 | |
| 1003 | // Because upsert only occurs at the tip end of the INSERT rule for cmd, |
| 1004 | // there is never a case where the value of the upsert pointer will not |
| 1005 | // be destroyed by the cmd action. So comment-out the destructor to |
| 1006 | // avoid unreachable code. |
| 1007 | //%destructor upsert {sqlite3UpsertDelete(pParse->db,$$);} |
drh | 46d2e5c | 2018-04-12 13:15:43 +0000 | [diff] [blame] | 1008 | upsert(A) ::= . { A = 0; } |
drh | 2053f31 | 2021-01-12 20:16:31 +0000 | [diff] [blame] | 1009 | upsert(A) ::= RETURNING selcollist(X). { A = 0; sqlite3AddReturning(pParse,X); } |
drh | e9c2e77 | 2018-04-13 13:06:45 +0000 | [diff] [blame] | 1010 | upsert(A) ::= ON CONFLICT LP sortlist(T) RP where_opt(TW) |
drh | 2549e4c | 2020-12-08 14:29:03 +0000 | [diff] [blame] | 1011 | DO UPDATE SET setlist(Z) where_opt(W) upsert(N). |
| 1012 | { A = sqlite3UpsertNew(pParse->db,T,TW,Z,W,N);} |
| 1013 | upsert(A) ::= ON CONFLICT LP sortlist(T) RP where_opt(TW) DO NOTHING upsert(N). |
| 1014 | { A = sqlite3UpsertNew(pParse->db,T,TW,0,0,N); } |
drh | 2053f31 | 2021-01-12 20:16:31 +0000 | [diff] [blame] | 1015 | upsert(A) ::= ON CONFLICT DO NOTHING returning. |
drh | 2549e4c | 2020-12-08 14:29:03 +0000 | [diff] [blame] | 1016 | { A = sqlite3UpsertNew(pParse->db,0,0,0,0,0); } |
drh | 2053f31 | 2021-01-12 20:16:31 +0000 | [diff] [blame] | 1017 | upsert(A) ::= ON CONFLICT DO UPDATE SET setlist(Z) where_opt(W) returning. |
drh | 2549e4c | 2020-12-08 14:29:03 +0000 | [diff] [blame] | 1018 | { A = sqlite3UpsertNew(pParse->db,0,0,Z,W,0);} |
drh | 26cf56f | 2018-04-06 19:36:49 +0000 | [diff] [blame] | 1019 | |
drh | 2053f31 | 2021-01-12 20:16:31 +0000 | [diff] [blame] | 1020 | returning ::= RETURNING selcollist(X). {sqlite3AddReturning(pParse,X);} |
| 1021 | returning ::= . |
| 1022 | |
drh | 3334d08 | 2015-11-10 13:45:21 +0000 | [diff] [blame] | 1023 | %type insert_cmd {int} |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 1024 | insert_cmd(A) ::= INSERT orconf(R). {A = R;} |
| 1025 | insert_cmd(A) ::= REPLACE. {A = OE_Replace;} |
| 1026 | |
drh | 8981b90 | 2015-08-24 17:42:49 +0000 | [diff] [blame] | 1027 | %type idlist_opt {IdList*} |
| 1028 | %destructor idlist_opt {sqlite3IdListDelete(pParse->db, $$);} |
drh | 81eba73 | 2013-10-19 23:31:56 +0000 | [diff] [blame] | 1029 | %type idlist {IdList*} |
| 1030 | %destructor idlist {sqlite3IdListDelete(pParse->db, $$);} |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 1031 | |
drh | 8981b90 | 2015-08-24 17:42:49 +0000 | [diff] [blame] | 1032 | idlist_opt(A) ::= . {A = 0;} |
| 1033 | idlist_opt(A) ::= LP idlist(X) RP. {A = X;} |
drh | 4dd0d3f | 2016-02-17 01:18:33 +0000 | [diff] [blame] | 1034 | idlist(A) ::= idlist(A) COMMA nm(Y). |
dan | 5496d6a | 2018-08-13 17:14:26 +0000 | [diff] [blame] | 1035 | {A = sqlite3IdListAppend(pParse,A,&Y);} |
drh | 81eba73 | 2013-10-19 23:31:56 +0000 | [diff] [blame] | 1036 | idlist(A) ::= nm(Y). |
dan | 5496d6a | 2018-08-13 17:14:26 +0000 | [diff] [blame] | 1037 | {A = sqlite3IdListAppend(pParse,0,&Y); /*A-overwrites-Y*/} |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 1038 | |
drh | 382c024 | 2001-10-06 16:33:02 +0000 | [diff] [blame] | 1039 | /////////////////////////// Expression Processing ///////////////////////////// |
| 1040 | // |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 1041 | |
drh | 1be266b | 2017-12-24 00:18:47 +0000 | [diff] [blame] | 1042 | %type expr {Expr*} |
| 1043 | %destructor expr {sqlite3ExprDelete(pParse->db, $$);} |
| 1044 | %type term {Expr*} |
| 1045 | %destructor term {sqlite3ExprDelete(pParse->db, $$);} |
drh | b7916a7 | 2009-05-27 10:31:29 +0000 | [diff] [blame] | 1046 | |
| 1047 | %include { |
drh | b7916a7 | 2009-05-27 10:31:29 +0000 | [diff] [blame] | 1048 | |
drh | 796588a | 2022-02-05 21:49:47 +0000 | [diff] [blame] | 1049 | /* Construct a new Expr object from a single token */ |
drh | 1be266b | 2017-12-24 00:18:47 +0000 | [diff] [blame] | 1050 | static Expr *tokenExpr(Parse *pParse, int op, Token t){ |
drh | 0cd874b | 2016-09-26 12:38:22 +0000 | [diff] [blame] | 1051 | Expr *p = sqlite3DbMallocRawNN(pParse->db, sizeof(Expr)+t.n+1); |
| 1052 | if( p ){ |
dan | d145e5f | 2018-08-21 08:29:48 +0000 | [diff] [blame] | 1053 | /* memset(p, 0, sizeof(Expr)); */ |
drh | 0cd874b | 2016-09-26 12:38:22 +0000 | [diff] [blame] | 1054 | p->op = (u8)op; |
drh | 1194904 | 2019-08-05 18:01:42 +0000 | [diff] [blame] | 1055 | p->affExpr = 0; |
drh | 0cd874b | 2016-09-26 12:38:22 +0000 | [diff] [blame] | 1056 | p->flags = EP_Leaf; |
drh | e7375bf | 2020-03-10 19:24:38 +0000 | [diff] [blame] | 1057 | ExprClearVVAProperties(p); |
drh | 9c949b1 | 2022-04-07 12:10:00 +0000 | [diff] [blame] | 1058 | /* p->iAgg = -1; // Not required */ |
dan | d145e5f | 2018-08-21 08:29:48 +0000 | [diff] [blame] | 1059 | p->pLeft = p->pRight = 0; |
dan | d145e5f | 2018-08-21 08:29:48 +0000 | [diff] [blame] | 1060 | p->pAggInfo = 0; |
drh | 477572b | 2021-10-07 20:46:29 +0000 | [diff] [blame] | 1061 | memset(&p->x, 0, sizeof(p->x)); |
| 1062 | memset(&p->y, 0, sizeof(p->y)); |
dan | d145e5f | 2018-08-21 08:29:48 +0000 | [diff] [blame] | 1063 | p->op2 = 0; |
drh | 4fe759b | 2018-08-23 18:22:00 +0000 | [diff] [blame] | 1064 | p->iTable = 0; |
drh | d3130da | 2018-08-23 18:50:19 +0000 | [diff] [blame] | 1065 | p->iColumn = 0; |
drh | 0cd874b | 2016-09-26 12:38:22 +0000 | [diff] [blame] | 1066 | p->u.zToken = (char*)&p[1]; |
| 1067 | memcpy(p->u.zToken, t.z, t.n); |
| 1068 | p->u.zToken[t.n] = 0; |
drh | 796588a | 2022-02-05 21:49:47 +0000 | [diff] [blame] | 1069 | p->w.iOfst = (int)(t.z - pParse->zTail); |
drh | 0cd874b | 2016-09-26 12:38:22 +0000 | [diff] [blame] | 1070 | if( sqlite3Isquote(p->u.zToken[0]) ){ |
drh | 51d35b0 | 2019-01-11 13:32:23 +0000 | [diff] [blame] | 1071 | sqlite3DequoteExpr(p); |
drh | 0cd874b | 2016-09-26 12:38:22 +0000 | [diff] [blame] | 1072 | } |
| 1073 | #if SQLITE_MAX_EXPR_DEPTH>0 |
| 1074 | p->nHeight = 1; |
| 1075 | #endif |
dan | c9461ec | 2018-08-29 21:00:16 +0000 | [diff] [blame] | 1076 | if( IN_RENAME_OBJECT ){ |
dan | 07e9523 | 2018-08-21 16:32:53 +0000 | [diff] [blame] | 1077 | return (Expr*)sqlite3RenameTokenMap(pParse, (void*)p, &t); |
dan | d145e5f | 2018-08-21 08:29:48 +0000 | [diff] [blame] | 1078 | } |
drh | 0cd874b | 2016-09-26 12:38:22 +0000 | [diff] [blame] | 1079 | } |
drh | 1be266b | 2017-12-24 00:18:47 +0000 | [diff] [blame] | 1080 | return p; |
drh | b7916a7 | 2009-05-27 10:31:29 +0000 | [diff] [blame] | 1081 | } |
dan | d145e5f | 2018-08-21 08:29:48 +0000 | [diff] [blame] | 1082 | |
drh | b7916a7 | 2009-05-27 10:31:29 +0000 | [diff] [blame] | 1083 | } |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 1084 | |
drh | 4dd0d3f | 2016-02-17 01:18:33 +0000 | [diff] [blame] | 1085 | expr(A) ::= term(A). |
drh | 1be266b | 2017-12-24 00:18:47 +0000 | [diff] [blame] | 1086 | expr(A) ::= LP expr(X) RP. {A = X;} |
| 1087 | expr(A) ::= id(X). {A=tokenExpr(pParse,TK_ID,X); /*A-overwrites-X*/} |
| 1088 | expr(A) ::= JOIN_KW(X). {A=tokenExpr(pParse,TK_ID,X); /*A-overwrites-X*/} |
drh | 5ad1a6c | 2002-07-01 12:27:09 +0000 | [diff] [blame] | 1089 | expr(A) ::= nm(X) DOT nm(Y). { |
drh | 796588a | 2022-02-05 21:49:47 +0000 | [diff] [blame] | 1090 | Expr *temp1 = tokenExpr(pParse,TK_ID,X); |
| 1091 | Expr *temp2 = tokenExpr(pParse,TK_ID,Y); |
drh | 1be266b | 2017-12-24 00:18:47 +0000 | [diff] [blame] | 1092 | A = sqlite3PExpr(pParse, TK_DOT, temp1, temp2); |
drh | e1b6a5b | 2000-07-29 13:06:59 +0000 | [diff] [blame] | 1093 | } |
drh | d24cc42 | 2003-03-27 12:51:24 +0000 | [diff] [blame] | 1094 | expr(A) ::= nm(X) DOT nm(Y) DOT nm(Z). { |
drh | 796588a | 2022-02-05 21:49:47 +0000 | [diff] [blame] | 1095 | Expr *temp1 = tokenExpr(pParse,TK_ID,X); |
| 1096 | Expr *temp2 = tokenExpr(pParse,TK_ID,Y); |
| 1097 | Expr *temp3 = tokenExpr(pParse,TK_ID,Z); |
drh | abfd35e | 2016-12-06 22:47:23 +0000 | [diff] [blame] | 1098 | Expr *temp4 = sqlite3PExpr(pParse, TK_DOT, temp2, temp3); |
dan | 02ede43 | 2022-02-12 16:02:37 +0000 | [diff] [blame] | 1099 | if( IN_RENAME_OBJECT ){ |
| 1100 | sqlite3RenameTokenRemap(pParse, 0, temp1); |
| 1101 | } |
drh | 1be266b | 2017-12-24 00:18:47 +0000 | [diff] [blame] | 1102 | A = sqlite3PExpr(pParse, TK_DOT, temp1, temp4); |
drh | d24cc42 | 2003-03-27 12:51:24 +0000 | [diff] [blame] | 1103 | } |
drh | 1be266b | 2017-12-24 00:18:47 +0000 | [diff] [blame] | 1104 | term(A) ::= NULL|FLOAT|BLOB(X). {A=tokenExpr(pParse,@X,X); /*A-overwrites-X*/} |
| 1105 | term(A) ::= STRING(X). {A=tokenExpr(pParse,@X,X); /*A-overwrites-X*/} |
drh | 0cd874b | 2016-09-26 12:38:22 +0000 | [diff] [blame] | 1106 | term(A) ::= INTEGER(X). { |
drh | 1be266b | 2017-12-24 00:18:47 +0000 | [diff] [blame] | 1107 | A = sqlite3ExprAlloc(pParse->db, TK_INTEGER, &X, 1); |
drh | 17a93ae | 2022-02-06 11:26:51 +0000 | [diff] [blame] | 1108 | if( A ) A->w.iOfst = (int)(X.z - pParse->zTail); |
drh | 0cd874b | 2016-09-26 12:38:22 +0000 | [diff] [blame] | 1109 | } |
drh | 7c972de | 2003-09-06 22:18:07 +0000 | [diff] [blame] | 1110 | expr(A) ::= VARIABLE(X). { |
drh | 8679fba | 2016-04-11 01:43:33 +0000 | [diff] [blame] | 1111 | if( !(X.z[0]=='#' && sqlite3Isdigit(X.z[1])) ){ |
drh | de25a88 | 2016-10-03 15:28:24 +0000 | [diff] [blame] | 1112 | u32 n = X.n; |
drh | 1be266b | 2017-12-24 00:18:47 +0000 | [diff] [blame] | 1113 | A = tokenExpr(pParse, TK_VARIABLE, X); |
| 1114 | sqlite3ExprAssignVarNumber(pParse, A, n); |
drh | 8f3b137 | 2016-04-11 01:26:31 +0000 | [diff] [blame] | 1115 | }else{ |
drh | f59b12f | 2014-01-11 03:54:05 +0000 | [diff] [blame] | 1116 | /* When doing a nested parse, one can include terms in an expression |
| 1117 | ** that look like this: #1 #2 ... These terms refer to registers |
| 1118 | ** in the virtual machine. #N is the N-th register. */ |
drh | 8f3b137 | 2016-04-11 01:26:31 +0000 | [diff] [blame] | 1119 | Token t = X; /*A-overwrites-X*/ |
| 1120 | assert( t.n>=2 ); |
drh | f59b12f | 2014-01-11 03:54:05 +0000 | [diff] [blame] | 1121 | if( pParse->nested==0 ){ |
drh | 43303de | 2016-02-17 12:34:03 +0000 | [diff] [blame] | 1122 | sqlite3ErrorMsg(pParse, "near \"%T\": syntax error", &t); |
drh | 1be266b | 2017-12-24 00:18:47 +0000 | [diff] [blame] | 1123 | A = 0; |
drh | f59b12f | 2014-01-11 03:54:05 +0000 | [diff] [blame] | 1124 | }else{ |
drh | 1be266b | 2017-12-24 00:18:47 +0000 | [diff] [blame] | 1125 | A = sqlite3PExpr(pParse, TK_REGISTER, 0, 0); |
| 1126 | if( A ) sqlite3GetInt32(&t.z[1], &A->iTable); |
drh | f59b12f | 2014-01-11 03:54:05 +0000 | [diff] [blame] | 1127 | } |
drh | f59b12f | 2014-01-11 03:54:05 +0000 | [diff] [blame] | 1128 | } |
drh | 7c972de | 2003-09-06 22:18:07 +0000 | [diff] [blame] | 1129 | } |
dan | 59ff425 | 2018-06-29 17:44:52 +0000 | [diff] [blame] | 1130 | expr(A) ::= expr(A) COLLATE ids(C). { |
drh | 1be266b | 2017-12-24 00:18:47 +0000 | [diff] [blame] | 1131 | A = sqlite3ExprAddCollateToken(pParse, A, &C, 1); |
drh | 8b4c40d | 2007-02-01 23:02:45 +0000 | [diff] [blame] | 1132 | } |
drh | 487e262 | 2005-06-25 18:42:14 +0000 | [diff] [blame] | 1133 | %ifndef SQLITE_OMIT_CAST |
drh | 1be266b | 2017-12-24 00:18:47 +0000 | [diff] [blame] | 1134 | expr(A) ::= CAST LP expr(E) AS typetoken(T) RP. { |
| 1135 | A = sqlite3ExprAlloc(pParse->db, TK_CAST, &T, 1); |
| 1136 | sqlite3ExprAttachSubtrees(pParse->db, A, E, 0); |
drh | 487e262 | 2005-06-25 18:42:14 +0000 | [diff] [blame] | 1137 | } |
drh | 154d4b2 | 2006-09-21 11:02:16 +0000 | [diff] [blame] | 1138 | %endif SQLITE_OMIT_CAST |
drh | 550a330 | 2018-07-27 22:14:50 +0000 | [diff] [blame] | 1139 | |
| 1140 | |
| 1141 | expr(A) ::= id(X) LP distinct(D) exprlist(Y) RP. { |
drh | 954733b | 2018-07-27 23:33:16 +0000 | [diff] [blame] | 1142 | A = sqlite3ExprFunction(pParse, Y, &X, D); |
drh | 550a330 | 2018-07-27 22:14:50 +0000 | [diff] [blame] | 1143 | } |
| 1144 | expr(A) ::= id(X) LP STAR RP. { |
drh | 954733b | 2018-07-27 23:33:16 +0000 | [diff] [blame] | 1145 | A = sqlite3ExprFunction(pParse, 0, &X, 0); |
drh | 550a330 | 2018-07-27 22:14:50 +0000 | [diff] [blame] | 1146 | } |
| 1147 | |
dan | 67a9b8e | 2018-06-22 20:51:35 +0000 | [diff] [blame] | 1148 | %ifndef SQLITE_OMIT_WINDOWFUNC |
dan | 4f9adee | 2019-07-13 16:22:50 +0000 | [diff] [blame] | 1149 | expr(A) ::= id(X) LP distinct(D) exprlist(Y) RP filter_over(Z). { |
dan | 6488271 | 2019-07-11 18:43:33 +0000 | [diff] [blame] | 1150 | A = sqlite3ExprFunction(pParse, Y, &X, D); |
dan | 4f9adee | 2019-07-13 16:22:50 +0000 | [diff] [blame] | 1151 | sqlite3WindowAttach(pParse, A, Z); |
dan | 6488271 | 2019-07-11 18:43:33 +0000 | [diff] [blame] | 1152 | } |
dan | 4f9adee | 2019-07-13 16:22:50 +0000 | [diff] [blame] | 1153 | expr(A) ::= id(X) LP STAR RP filter_over(Z). { |
drh | 954733b | 2018-07-27 23:33:16 +0000 | [diff] [blame] | 1154 | A = sqlite3ExprFunction(pParse, 0, &X, 0); |
dan | 4f9adee | 2019-07-13 16:22:50 +0000 | [diff] [blame] | 1155 | sqlite3WindowAttach(pParse, A, Z); |
drh | e1b6a5b | 2000-07-29 13:06:59 +0000 | [diff] [blame] | 1156 | } |
drh | 550a330 | 2018-07-27 22:14:50 +0000 | [diff] [blame] | 1157 | %endif |
| 1158 | |
drh | b71090f | 2005-05-23 17:26:51 +0000 | [diff] [blame] | 1159 | term(A) ::= CTIME_KW(OP). { |
drh | 954733b | 2018-07-27 23:33:16 +0000 | [diff] [blame] | 1160 | A = sqlite3ExprFunction(pParse, 0, &OP, 0); |
drh | b7916a7 | 2009-05-27 10:31:29 +0000 | [diff] [blame] | 1161 | } |
| 1162 | |
drh | 1be266b | 2017-12-24 00:18:47 +0000 | [diff] [blame] | 1163 | expr(A) ::= LP nexprlist(X) COMMA expr(Y) RP. { |
| 1164 | ExprList *pList = sqlite3ExprListAppend(pParse, X, Y); |
| 1165 | A = sqlite3PExpr(pParse, TK_VECTOR, 0, 0); |
| 1166 | if( A ){ |
| 1167 | A->x.pList = pList; |
drh | 76baf79 | 2019-10-28 04:20:28 +0000 | [diff] [blame] | 1168 | if( ALWAYS(pList->nExpr) ){ |
drh | 269d322 | 2019-10-23 18:09:39 +0000 | [diff] [blame] | 1169 | A->flags |= pList->a[0].pExpr->flags & EP_Propagate; |
| 1170 | } |
drh | 8bd0d58 | 2016-08-20 18:06:14 +0000 | [diff] [blame] | 1171 | }else{ |
| 1172 | sqlite3ExprListDelete(pParse->db, pList); |
dan | 71c57db | 2016-07-09 20:23:55 +0000 | [diff] [blame] | 1173 | } |
| 1174 | } |
| 1175 | |
drh | d5c851c | 2019-04-19 13:38:34 +0000 | [diff] [blame] | 1176 | expr(A) ::= expr(A) AND expr(Y). {A=sqlite3ExprAnd(pParse,A,Y);} |
drh | 1be266b | 2017-12-24 00:18:47 +0000 | [diff] [blame] | 1177 | expr(A) ::= expr(A) OR(OP) expr(Y). {A=sqlite3PExpr(pParse,@OP,A,Y);} |
drh | 4dd0d3f | 2016-02-17 01:18:33 +0000 | [diff] [blame] | 1178 | expr(A) ::= expr(A) LT|GT|GE|LE(OP) expr(Y). |
drh | 1be266b | 2017-12-24 00:18:47 +0000 | [diff] [blame] | 1179 | {A=sqlite3PExpr(pParse,@OP,A,Y);} |
| 1180 | expr(A) ::= expr(A) EQ|NE(OP) expr(Y). {A=sqlite3PExpr(pParse,@OP,A,Y);} |
drh | 4dd0d3f | 2016-02-17 01:18:33 +0000 | [diff] [blame] | 1181 | expr(A) ::= expr(A) BITAND|BITOR|LSHIFT|RSHIFT(OP) expr(Y). |
drh | 1be266b | 2017-12-24 00:18:47 +0000 | [diff] [blame] | 1182 | {A=sqlite3PExpr(pParse,@OP,A,Y);} |
drh | 4dd0d3f | 2016-02-17 01:18:33 +0000 | [diff] [blame] | 1183 | expr(A) ::= expr(A) PLUS|MINUS(OP) expr(Y). |
drh | 1be266b | 2017-12-24 00:18:47 +0000 | [diff] [blame] | 1184 | {A=sqlite3PExpr(pParse,@OP,A,Y);} |
drh | 4dd0d3f | 2016-02-17 01:18:33 +0000 | [diff] [blame] | 1185 | expr(A) ::= expr(A) STAR|SLASH|REM(OP) expr(Y). |
drh | 1be266b | 2017-12-24 00:18:47 +0000 | [diff] [blame] | 1186 | {A=sqlite3PExpr(pParse,@OP,A,Y);} |
| 1187 | expr(A) ::= expr(A) CONCAT(OP) expr(Y). {A=sqlite3PExpr(pParse,@OP,A,Y);} |
drh | 410c301 | 2016-09-24 17:42:43 +0000 | [diff] [blame] | 1188 | %type likeop {Token} |
drh | 7e84b37 | 2017-02-20 14:30:17 +0000 | [diff] [blame] | 1189 | likeop(A) ::= LIKE_KW|MATCH(A). |
drh | 410c301 | 2016-09-24 17:42:43 +0000 | [diff] [blame] | 1190 | likeop(A) ::= NOT LIKE_KW|MATCH(X). {A=X; A.n|=0x80000000; /*A-overwrite-X*/} |
drh | 4dd0d3f | 2016-02-17 01:18:33 +0000 | [diff] [blame] | 1191 | expr(A) ::= expr(A) likeop(OP) expr(Y). [LIKE_KW] { |
drh | 8aa34ae | 2006-03-13 12:54:09 +0000 | [diff] [blame] | 1192 | ExprList *pList; |
drh | 410c301 | 2016-09-24 17:42:43 +0000 | [diff] [blame] | 1193 | int bNot = OP.n & 0x80000000; |
| 1194 | OP.n &= 0x7fffffff; |
drh | 1be266b | 2017-12-24 00:18:47 +0000 | [diff] [blame] | 1195 | pList = sqlite3ExprListAppend(pParse,0, Y); |
| 1196 | pList = sqlite3ExprListAppend(pParse,pList, A); |
drh | 954733b | 2018-07-27 23:33:16 +0000 | [diff] [blame] | 1197 | A = sqlite3ExprFunction(pParse, pList, &OP, 0); |
drh | 1be266b | 2017-12-24 00:18:47 +0000 | [diff] [blame] | 1198 | if( bNot ) A = sqlite3PExpr(pParse, TK_NOT, A, 0); |
| 1199 | if( A ) A->flags |= EP_InfixFunc; |
drh | 0ac6589 | 2002-04-20 14:24:41 +0000 | [diff] [blame] | 1200 | } |
drh | 4dd0d3f | 2016-02-17 01:18:33 +0000 | [diff] [blame] | 1201 | expr(A) ::= expr(A) likeop(OP) expr(Y) ESCAPE expr(E). [LIKE_KW] { |
drh | 1dca145 | 2010-07-19 02:30:33 +0000 | [diff] [blame] | 1202 | ExprList *pList; |
drh | 410c301 | 2016-09-24 17:42:43 +0000 | [diff] [blame] | 1203 | int bNot = OP.n & 0x80000000; |
| 1204 | OP.n &= 0x7fffffff; |
drh | 1be266b | 2017-12-24 00:18:47 +0000 | [diff] [blame] | 1205 | pList = sqlite3ExprListAppend(pParse,0, Y); |
| 1206 | pList = sqlite3ExprListAppend(pParse,pList, A); |
| 1207 | pList = sqlite3ExprListAppend(pParse,pList, E); |
drh | 954733b | 2018-07-27 23:33:16 +0000 | [diff] [blame] | 1208 | A = sqlite3ExprFunction(pParse, pList, &OP, 0); |
drh | 1be266b | 2017-12-24 00:18:47 +0000 | [diff] [blame] | 1209 | if( bNot ) A = sqlite3PExpr(pParse, TK_NOT, A, 0); |
| 1210 | if( A ) A->flags |= EP_InfixFunc; |
drh | 1dca145 | 2010-07-19 02:30:33 +0000 | [diff] [blame] | 1211 | } |
danielk1977 | 7c6303c | 2004-11-17 16:41:29 +0000 | [diff] [blame] | 1212 | |
drh | 1be266b | 2017-12-24 00:18:47 +0000 | [diff] [blame] | 1213 | expr(A) ::= expr(A) ISNULL|NOTNULL(E). {A = sqlite3PExpr(pParse,@E,A,0);} |
| 1214 | expr(A) ::= expr(A) NOT NULL. {A = sqlite3PExpr(pParse,TK_NOTNULL,A,0);} |
drh | 6a2fe09 | 2009-09-23 02:29:36 +0000 | [diff] [blame] | 1215 | |
drh | 6a51741 | 2009-11-12 03:46:34 +0000 | [diff] [blame] | 1216 | %include { |
| 1217 | /* A routine to convert a binary TK_IS or TK_ISNOT expression into a |
| 1218 | ** unary TK_ISNULL or TK_NOTNULL expression. */ |
| 1219 | static void binaryToUnaryIfNull(Parse *pParse, Expr *pY, Expr *pA, int op){ |
| 1220 | sqlite3 *db = pParse->db; |
dan | c50f75d | 2018-09-06 18:56:36 +0000 | [diff] [blame] | 1221 | if( pA && pY && pY->op==TK_NULL && !IN_RENAME_OBJECT ){ |
shaneh | 5e17e8b | 2009-12-03 04:40:47 +0000 | [diff] [blame] | 1222 | pA->op = (u8)op; |
drh | 6a51741 | 2009-11-12 03:46:34 +0000 | [diff] [blame] | 1223 | sqlite3ExprDelete(db, pA->pRight); |
| 1224 | pA->pRight = 0; |
| 1225 | } |
| 1226 | } |
| 1227 | } |
| 1228 | |
drh | 6a2fe09 | 2009-09-23 02:29:36 +0000 | [diff] [blame] | 1229 | // expr1 IS expr2 |
| 1230 | // expr1 IS NOT expr2 |
| 1231 | // |
| 1232 | // If expr2 is NULL then code as TK_ISNULL or TK_NOTNULL. If expr2 |
| 1233 | // is any other expression, code as TK_IS or TK_ISNOT. |
| 1234 | // |
drh | 4dd0d3f | 2016-02-17 01:18:33 +0000 | [diff] [blame] | 1235 | expr(A) ::= expr(A) IS expr(Y). { |
drh | 1be266b | 2017-12-24 00:18:47 +0000 | [diff] [blame] | 1236 | A = sqlite3PExpr(pParse,TK_IS,A,Y); |
| 1237 | binaryToUnaryIfNull(pParse, Y, A, TK_ISNULL); |
drh | 6a2fe09 | 2009-09-23 02:29:36 +0000 | [diff] [blame] | 1238 | } |
drh | 4dd0d3f | 2016-02-17 01:18:33 +0000 | [diff] [blame] | 1239 | expr(A) ::= expr(A) IS NOT expr(Y). { |
drh | 1be266b | 2017-12-24 00:18:47 +0000 | [diff] [blame] | 1240 | A = sqlite3PExpr(pParse,TK_ISNOT,A,Y); |
| 1241 | binaryToUnaryIfNull(pParse, Y, A, TK_NOTNULL); |
drh | 6a2fe09 | 2009-09-23 02:29:36 +0000 | [diff] [blame] | 1242 | } |
drh | 902e260 | 2022-05-12 11:45:20 +0000 | [diff] [blame] | 1243 | expr(A) ::= expr(A) IS NOT DISTINCT FROM expr(Y). { |
| 1244 | A = sqlite3PExpr(pParse,TK_IS,A,Y); |
| 1245 | binaryToUnaryIfNull(pParse, Y, A, TK_ISNULL); |
| 1246 | } |
| 1247 | expr(A) ::= expr(A) IS DISTINCT FROM expr(Y). { |
| 1248 | A = sqlite3PExpr(pParse,TK_ISNOT,A,Y); |
| 1249 | binaryToUnaryIfNull(pParse, Y, A, TK_NOTNULL); |
| 1250 | } |
drh | b7916a7 | 2009-05-27 10:31:29 +0000 | [diff] [blame] | 1251 | |
drh | 43303de | 2016-02-17 12:34:03 +0000 | [diff] [blame] | 1252 | expr(A) ::= NOT(B) expr(X). |
drh | 1be266b | 2017-12-24 00:18:47 +0000 | [diff] [blame] | 1253 | {A = sqlite3PExpr(pParse, @B, X, 0);/*A-overwrites-B*/} |
drh | 43303de | 2016-02-17 12:34:03 +0000 | [diff] [blame] | 1254 | expr(A) ::= BITNOT(B) expr(X). |
drh | 1be266b | 2017-12-24 00:18:47 +0000 | [diff] [blame] | 1255 | {A = sqlite3PExpr(pParse, @B, X, 0);/*A-overwrites-B*/} |
drh | ca5aa59 | 2018-06-19 11:15:19 +0000 | [diff] [blame] | 1256 | expr(A) ::= PLUS|MINUS(B) expr(X). [BITNOT] { |
| 1257 | A = sqlite3PExpr(pParse, @B==TK_PLUS ? TK_UPLUS : TK_UMINUS, X, 0); |
| 1258 | /*A-overwrites-B*/ |
| 1259 | } |
drh | b7916a7 | 2009-05-27 10:31:29 +0000 | [diff] [blame] | 1260 | |
drh | d5326c3 | 2022-01-07 14:58:47 +0000 | [diff] [blame] | 1261 | expr(A) ::= expr(B) PTR(C) expr(D). { |
| 1262 | ExprList *pList = sqlite3ExprListAppend(pParse, 0, B); |
| 1263 | pList = sqlite3ExprListAppend(pParse, pList, D); |
| 1264 | A = sqlite3ExprFunction(pParse, pList, &C, 0); |
| 1265 | } |
| 1266 | |
drh | 2e3a1f1 | 2004-10-06 14:39:28 +0000 | [diff] [blame] | 1267 | %type between_op {int} |
| 1268 | between_op(A) ::= BETWEEN. {A = 0;} |
| 1269 | between_op(A) ::= NOT BETWEEN. {A = 1;} |
drh | 4dd0d3f | 2016-02-17 01:18:33 +0000 | [diff] [blame] | 1270 | expr(A) ::= expr(A) between_op(N) expr(X) AND expr(Y). [BETWEEN] { |
drh | 1be266b | 2017-12-24 00:18:47 +0000 | [diff] [blame] | 1271 | ExprList *pList = sqlite3ExprListAppend(pParse,0, X); |
| 1272 | pList = sqlite3ExprListAppend(pParse,pList, Y); |
| 1273 | A = sqlite3PExpr(pParse, TK_BETWEEN, A, 0); |
| 1274 | if( A ){ |
| 1275 | A->x.pList = pList; |
drh | 53f733c | 2005-09-16 02:38:09 +0000 | [diff] [blame] | 1276 | }else{ |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 1277 | sqlite3ExprListDelete(pParse->db, pList); |
drh | 53f733c | 2005-09-16 02:38:09 +0000 | [diff] [blame] | 1278 | } |
drh | 1be266b | 2017-12-24 00:18:47 +0000 | [diff] [blame] | 1279 | if( N ) A = sqlite3PExpr(pParse, TK_NOT, A, 0); |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 1280 | } |
drh | 51522cd | 2005-01-20 13:36:19 +0000 | [diff] [blame] | 1281 | %ifndef SQLITE_OMIT_SUBQUERY |
danielk1977 | 3e8c37e | 2005-01-21 03:12:14 +0000 | [diff] [blame] | 1282 | %type in_op {int} |
| 1283 | in_op(A) ::= IN. {A = 0;} |
| 1284 | in_op(A) ::= NOT IN. {A = 1;} |
drh | 1be266b | 2017-12-24 00:18:47 +0000 | [diff] [blame] | 1285 | expr(A) ::= expr(A) in_op(N) LP exprlist(Y) RP. [IN] { |
drh | 094430e | 2010-07-14 18:24:06 +0000 | [diff] [blame] | 1286 | if( Y==0 ){ |
dan | 473c1bf | 2010-07-15 11:14:21 +0000 | [diff] [blame] | 1287 | /* Expressions of the form |
| 1288 | ** |
| 1289 | ** expr1 IN () |
| 1290 | ** expr1 NOT IN () |
| 1291 | ** |
| 1292 | ** simplify to constants 0 (false) and 1 (true), respectively, |
| 1293 | ** regardless of the value of expr1. |
| 1294 | */ |
drh | 8e34e40 | 2019-06-11 10:43:56 +0000 | [diff] [blame] | 1295 | sqlite3ExprUnmapAndDelete(pParse, A); |
dan | 5348fbe | 2022-05-17 15:01:01 +0000 | [diff] [blame] | 1296 | A = sqlite3Expr(pParse->db, TK_STRING, N ? "true" : "false"); |
| 1297 | if( A ) sqlite3ExprIdToTrueFalse(A); |
danielk1977 | d5d5652 | 2005-03-16 12:15:20 +0000 | [diff] [blame] | 1298 | }else{ |
dan | 9289f51 | 2021-07-06 20:44:32 +0000 | [diff] [blame] | 1299 | Expr *pRHS = Y->a[0].pExpr; |
dan | 74777f9 | 2021-07-07 13:53:55 +0000 | [diff] [blame] | 1300 | if( Y->nExpr==1 && sqlite3ExprIsConstant(pRHS) && A->op!=TK_VECTOR ){ |
dan | 9289f51 | 2021-07-06 20:44:32 +0000 | [diff] [blame] | 1301 | Y->a[0].pExpr = 0; |
drh | 094430e | 2010-07-14 18:24:06 +0000 | [diff] [blame] | 1302 | sqlite3ExprListDelete(pParse->db, Y); |
dan | 9289f51 | 2021-07-06 20:44:32 +0000 | [diff] [blame] | 1303 | pRHS = sqlite3PExpr(pParse, TK_UPLUS, pRHS, 0); |
| 1304 | A = sqlite3PExpr(pParse, TK_EQ, A, pRHS); |
| 1305 | }else{ |
| 1306 | A = sqlite3PExpr(pParse, TK_IN, A, 0); |
dan | 74777f9 | 2021-07-07 13:53:55 +0000 | [diff] [blame] | 1307 | if( A==0 ){ |
| 1308 | sqlite3ExprListDelete(pParse->db, Y); |
| 1309 | }else if( A->pLeft->op==TK_VECTOR ){ |
| 1310 | int nExpr = A->pLeft->x.pList->nExpr; |
drh | 7098859 | 2021-07-07 19:40:18 +0000 | [diff] [blame] | 1311 | Select *pSelectRHS = sqlite3ExprListToValues(pParse, nExpr, Y); |
| 1312 | if( pSelectRHS ){ |
| 1313 | parserDoubleLinkSelect(pParse, pSelectRHS); |
| 1314 | sqlite3PExprAddSelect(pParse, A, pSelectRHS); |
dan | 9289f51 | 2021-07-06 20:44:32 +0000 | [diff] [blame] | 1315 | } |
| 1316 | }else{ |
| 1317 | A->x.pList = Y; |
| 1318 | sqlite3ExprSetHeightAndFlags(pParse, A); |
| 1319 | } |
drh | 094430e | 2010-07-14 18:24:06 +0000 | [diff] [blame] | 1320 | } |
drh | 1be266b | 2017-12-24 00:18:47 +0000 | [diff] [blame] | 1321 | if( N ) A = sqlite3PExpr(pParse, TK_NOT, A, 0); |
danielk1977 | d5d5652 | 2005-03-16 12:15:20 +0000 | [diff] [blame] | 1322 | } |
danielk1977 | 3e8c37e | 2005-01-21 03:12:14 +0000 | [diff] [blame] | 1323 | } |
drh | 1be266b | 2017-12-24 00:18:47 +0000 | [diff] [blame] | 1324 | expr(A) ::= LP select(X) RP. { |
| 1325 | A = sqlite3PExpr(pParse, TK_SELECT, 0, 0); |
| 1326 | sqlite3PExprAddSelect(pParse, A, X); |
drh | 51522cd | 2005-01-20 13:36:19 +0000 | [diff] [blame] | 1327 | } |
drh | 1be266b | 2017-12-24 00:18:47 +0000 | [diff] [blame] | 1328 | expr(A) ::= expr(A) in_op(N) LP select(Y) RP. [IN] { |
| 1329 | A = sqlite3PExpr(pParse, TK_IN, A, 0); |
| 1330 | sqlite3PExprAddSelect(pParse, A, Y); |
| 1331 | if( N ) A = sqlite3PExpr(pParse, TK_NOT, A, 0); |
drh | 51522cd | 2005-01-20 13:36:19 +0000 | [diff] [blame] | 1332 | } |
drh | 5fbab88 | 2016-07-02 12:08:14 +0000 | [diff] [blame] | 1333 | expr(A) ::= expr(A) in_op(N) nm(Y) dbnm(Z) paren_exprlist(E). [IN] { |
drh | 29c992c | 2019-01-17 15:40:41 +0000 | [diff] [blame] | 1334 | SrcList *pSrc = sqlite3SrcListAppend(pParse, 0,&Y,&Z); |
drh | 8c0833f | 2017-11-14 23:48:23 +0000 | [diff] [blame] | 1335 | Select *pSelect = sqlite3SelectNew(pParse, 0,pSrc,0,0,0,0,0,0); |
drh | 9de4757 | 2016-07-02 12:33:21 +0000 | [diff] [blame] | 1336 | if( E ) sqlite3SrcListFuncArgs(pParse, pSelect ? pSrc : 0, E); |
drh | 1be266b | 2017-12-24 00:18:47 +0000 | [diff] [blame] | 1337 | A = sqlite3PExpr(pParse, TK_IN, A, 0); |
| 1338 | sqlite3PExprAddSelect(pParse, A, pSelect); |
| 1339 | if( N ) A = sqlite3PExpr(pParse, TK_NOT, A, 0); |
drh | 51522cd | 2005-01-20 13:36:19 +0000 | [diff] [blame] | 1340 | } |
drh | 1be266b | 2017-12-24 00:18:47 +0000 | [diff] [blame] | 1341 | expr(A) ::= EXISTS LP select(Y) RP. { |
drh | 43303de | 2016-02-17 12:34:03 +0000 | [diff] [blame] | 1342 | Expr *p; |
drh | 1be266b | 2017-12-24 00:18:47 +0000 | [diff] [blame] | 1343 | p = A = sqlite3PExpr(pParse, TK_EXISTS, 0, 0); |
drh | 08de4f7 | 2016-04-11 01:06:47 +0000 | [diff] [blame] | 1344 | sqlite3PExprAddSelect(pParse, p, Y); |
drh | 51522cd | 2005-01-20 13:36:19 +0000 | [diff] [blame] | 1345 | } |
drh | 154d4b2 | 2006-09-21 11:02:16 +0000 | [diff] [blame] | 1346 | %endif SQLITE_OMIT_SUBQUERY |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 1347 | |
drh | 17a7f8d | 2002-03-24 13:13:27 +0000 | [diff] [blame] | 1348 | /* CASE expressions */ |
drh | 1be266b | 2017-12-24 00:18:47 +0000 | [diff] [blame] | 1349 | expr(A) ::= CASE case_operand(X) case_exprlist(Y) case_else(Z) END. { |
| 1350 | A = sqlite3PExpr(pParse, TK_CASE, X, 0); |
| 1351 | if( A ){ |
| 1352 | A->x.pList = Z ? sqlite3ExprListAppend(pParse,Y,Z) : Y; |
| 1353 | sqlite3ExprSetHeightAndFlags(pParse, A); |
drh | 53f733c | 2005-09-16 02:38:09 +0000 | [diff] [blame] | 1354 | }else{ |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 1355 | sqlite3ExprListDelete(pParse->db, Y); |
drh | c5cd124 | 2013-09-12 16:50:49 +0000 | [diff] [blame] | 1356 | sqlite3ExprDelete(pParse->db, Z); |
drh | 53f733c | 2005-09-16 02:38:09 +0000 | [diff] [blame] | 1357 | } |
drh | 17a7f8d | 2002-03-24 13:13:27 +0000 | [diff] [blame] | 1358 | } |
| 1359 | %type case_exprlist {ExprList*} |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 1360 | %destructor case_exprlist {sqlite3ExprListDelete(pParse->db, $$);} |
drh | 4dd0d3f | 2016-02-17 01:18:33 +0000 | [diff] [blame] | 1361 | case_exprlist(A) ::= case_exprlist(A) WHEN expr(Y) THEN expr(Z). { |
drh | 1be266b | 2017-12-24 00:18:47 +0000 | [diff] [blame] | 1362 | A = sqlite3ExprListAppend(pParse,A, Y); |
| 1363 | A = sqlite3ExprListAppend(pParse,A, Z); |
drh | 17a7f8d | 2002-03-24 13:13:27 +0000 | [diff] [blame] | 1364 | } |
| 1365 | case_exprlist(A) ::= WHEN expr(Y) THEN expr(Z). { |
drh | 1be266b | 2017-12-24 00:18:47 +0000 | [diff] [blame] | 1366 | A = sqlite3ExprListAppend(pParse,0, Y); |
| 1367 | A = sqlite3ExprListAppend(pParse,A, Z); |
drh | 17a7f8d | 2002-03-24 13:13:27 +0000 | [diff] [blame] | 1368 | } |
| 1369 | %type case_else {Expr*} |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 1370 | %destructor case_else {sqlite3ExprDelete(pParse->db, $$);} |
drh | 1be266b | 2017-12-24 00:18:47 +0000 | [diff] [blame] | 1371 | case_else(A) ::= ELSE expr(X). {A = X;} |
drh | 17a7f8d | 2002-03-24 13:13:27 +0000 | [diff] [blame] | 1372 | case_else(A) ::= . {A = 0;} |
| 1373 | %type case_operand {Expr*} |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 1374 | %destructor case_operand {sqlite3ExprDelete(pParse->db, $$);} |
drh | 1be266b | 2017-12-24 00:18:47 +0000 | [diff] [blame] | 1375 | case_operand(A) ::= expr(X). {A = X; /*A-overwrites-X*/} |
drh | 17a7f8d | 2002-03-24 13:13:27 +0000 | [diff] [blame] | 1376 | case_operand(A) ::= . {A = 0;} |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 1377 | |
| 1378 | %type exprlist {ExprList*} |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 1379 | %destructor exprlist {sqlite3ExprListDelete(pParse->db, $$);} |
drh | 9245c24 | 2007-06-20 12:18:31 +0000 | [diff] [blame] | 1380 | %type nexprlist {ExprList*} |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 1381 | %destructor nexprlist {sqlite3ExprListDelete(pParse->db, $$);} |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 1382 | |
drh | 4dd0d3f | 2016-02-17 01:18:33 +0000 | [diff] [blame] | 1383 | exprlist(A) ::= nexprlist(A). |
drh | 9245c24 | 2007-06-20 12:18:31 +0000 | [diff] [blame] | 1384 | exprlist(A) ::= . {A = 0;} |
drh | 4dd0d3f | 2016-02-17 01:18:33 +0000 | [diff] [blame] | 1385 | nexprlist(A) ::= nexprlist(A) COMMA expr(Y). |
drh | 1be266b | 2017-12-24 00:18:47 +0000 | [diff] [blame] | 1386 | {A = sqlite3ExprListAppend(pParse,A,Y);} |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 1387 | nexprlist(A) ::= expr(Y). |
drh | 1be266b | 2017-12-24 00:18:47 +0000 | [diff] [blame] | 1388 | {A = sqlite3ExprListAppend(pParse,0,Y); /*A-overwrites-Y*/} |
drh | 9245c24 | 2007-06-20 12:18:31 +0000 | [diff] [blame] | 1389 | |
drh | a522473 | 2016-07-25 14:40:43 +0000 | [diff] [blame] | 1390 | %ifndef SQLITE_OMIT_SUBQUERY |
drh | 5fbab88 | 2016-07-02 12:08:14 +0000 | [diff] [blame] | 1391 | /* A paren_exprlist is an optional expression list contained inside |
| 1392 | ** of parenthesis */ |
| 1393 | %type paren_exprlist {ExprList*} |
| 1394 | %destructor paren_exprlist {sqlite3ExprListDelete(pParse->db, $$);} |
| 1395 | paren_exprlist(A) ::= . {A = 0;} |
| 1396 | paren_exprlist(A) ::= LP exprlist(X) RP. {A = X;} |
drh | a522473 | 2016-07-25 14:40:43 +0000 | [diff] [blame] | 1397 | %endif SQLITE_OMIT_SUBQUERY |
drh | 5fbab88 | 2016-07-02 12:08:14 +0000 | [diff] [blame] | 1398 | |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 1399 | |
drh | 382c024 | 2001-10-06 16:33:02 +0000 | [diff] [blame] | 1400 | ///////////////////////////// The CREATE INDEX command /////////////////////// |
| 1401 | // |
drh | d9da78a | 2009-03-24 15:08:09 +0000 | [diff] [blame] | 1402 | cmd ::= createkw(S) uniqueflag(U) INDEX ifnotexists(NE) nm(X) dbnm(D) |
drh | 108aa00 | 2015-08-24 20:21:20 +0000 | [diff] [blame] | 1403 | ON nm(Y) LP sortlist(Z) RP where_opt(W). { |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 1404 | sqlite3CreateIndex(pParse, &X, &D, |
drh | 29c992c | 2019-01-17 15:40:41 +0000 | [diff] [blame] | 1405 | sqlite3SrcListAppend(pParse,0,&Y,0), Z, U, |
drh | 62340f8 | 2016-05-31 21:18:15 +0000 | [diff] [blame] | 1406 | &S, W, SQLITE_SO_ASC, NE, SQLITE_IDXTYPE_APPDEF); |
dan | c9461ec | 2018-08-29 21:00:16 +0000 | [diff] [blame] | 1407 | if( IN_RENAME_OBJECT && pParse->pNewIndex ){ |
| 1408 | sqlite3RenameTokenMap(pParse, pParse->pNewIndex->zName, &Y); |
| 1409 | } |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 1410 | } |
drh | 717e640 | 2001-09-27 03:22:32 +0000 | [diff] [blame] | 1411 | |
| 1412 | %type uniqueflag {int} |
drh | 74ad7fe | 2004-10-07 03:06:28 +0000 | [diff] [blame] | 1413 | uniqueflag(A) ::= UNIQUE. {A = OE_Abort;} |
| 1414 | uniqueflag(A) ::= . {A = OE_None;} |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 1415 | |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 1416 | |
drh | 108aa00 | 2015-08-24 20:21:20 +0000 | [diff] [blame] | 1417 | // The eidlist non-terminal (Expression Id List) generates an ExprList |
| 1418 | // from a list of identifiers. The identifier names are in ExprList.a[].zName. |
| 1419 | // This list is stored in an ExprList rather than an IdList so that it |
| 1420 | // can be easily sent to sqlite3ColumnsExprList(). |
| 1421 | // |
| 1422 | // eidlist is grouped with CREATE INDEX because it used to be the non-terminal |
| 1423 | // used for the arguments to an index. That is just an historical accident. |
| 1424 | // |
| 1425 | // IMPORTANT COMPATIBILITY NOTE: Some prior versions of SQLite accepted |
| 1426 | // COLLATE clauses and ASC or DESC keywords on ID lists in inappropriate |
drh | 067b92b | 2020-06-19 15:24:12 +0000 | [diff] [blame] | 1427 | // places - places that might have been stored in the sqlite_schema table. |
drh | 108aa00 | 2015-08-24 20:21:20 +0000 | [diff] [blame] | 1428 | // Those extra features were ignored. But because they might be in some |
| 1429 | // (busted) old databases, we need to continue parsing them when loading |
| 1430 | // historical schemas. |
| 1431 | // |
| 1432 | %type eidlist {ExprList*} |
| 1433 | %destructor eidlist {sqlite3ExprListDelete(pParse->db, $$);} |
| 1434 | %type eidlist_opt {ExprList*} |
| 1435 | %destructor eidlist_opt {sqlite3ExprListDelete(pParse->db, $$);} |
| 1436 | |
| 1437 | %include { |
| 1438 | /* Add a single new term to an ExprList that is used to store a |
| 1439 | ** list of identifiers. Report an error if the ID list contains |
| 1440 | ** a COLLATE clause or an ASC or DESC keyword, except ignore the |
| 1441 | ** error while parsing a legacy schema. |
| 1442 | */ |
| 1443 | static ExprList *parserAddExprIdListTerm( |
| 1444 | Parse *pParse, |
| 1445 | ExprList *pPrior, |
| 1446 | Token *pIdToken, |
| 1447 | int hasCollate, |
| 1448 | int sortOrder |
| 1449 | ){ |
| 1450 | ExprList *p = sqlite3ExprListAppend(pParse, pPrior, 0); |
| 1451 | if( (hasCollate || sortOrder!=SQLITE_SO_UNDEFINED) |
| 1452 | && pParse->db->init.busy==0 |
| 1453 | ){ |
| 1454 | sqlite3ErrorMsg(pParse, "syntax error after column name \"%.*s\"", |
| 1455 | pIdToken->n, pIdToken->z); |
| 1456 | } |
| 1457 | sqlite3ExprListSetName(pParse, p, pIdToken, 1); |
| 1458 | return p; |
| 1459 | } |
| 1460 | } // end %include |
| 1461 | |
| 1462 | eidlist_opt(A) ::= . {A = 0;} |
| 1463 | eidlist_opt(A) ::= LP eidlist(X) RP. {A = X;} |
drh | 4dd0d3f | 2016-02-17 01:18:33 +0000 | [diff] [blame] | 1464 | eidlist(A) ::= eidlist(A) COMMA nm(Y) collate(C) sortorder(Z). { |
| 1465 | A = parserAddExprIdListTerm(pParse, A, &Y, C, Z); |
danielk1977 | 0202b29 | 2004-06-09 09:55:16 +0000 | [diff] [blame] | 1466 | } |
drh | 108aa00 | 2015-08-24 20:21:20 +0000 | [diff] [blame] | 1467 | eidlist(A) ::= nm(Y) collate(C) sortorder(Z). { |
drh | cf82f0d | 2016-02-17 04:33:10 +0000 | [diff] [blame] | 1468 | A = parserAddExprIdListTerm(pParse, 0, &Y, C, Z); /*A-overwrites-Y*/ |
danielk1977 | 0202b29 | 2004-06-09 09:55:16 +0000 | [diff] [blame] | 1469 | } |
danielk1977 | 0202b29 | 2004-06-09 09:55:16 +0000 | [diff] [blame] | 1470 | |
drh | 108aa00 | 2015-08-24 20:21:20 +0000 | [diff] [blame] | 1471 | %type collate {int} |
| 1472 | collate(C) ::= . {C = 0;} |
dan | 59ff425 | 2018-06-29 17:44:52 +0000 | [diff] [blame] | 1473 | collate(C) ::= COLLATE ids. {C = 1;} |
drh | a34001c | 2007-02-02 12:44:37 +0000 | [diff] [blame] | 1474 | |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 1475 | |
drh | 8aff101 | 2001-12-22 14:49:24 +0000 | [diff] [blame] | 1476 | ///////////////////////////// The DROP INDEX command ///////////////////////// |
drh | 382c024 | 2001-10-06 16:33:02 +0000 | [diff] [blame] | 1477 | // |
drh | 4d91a70 | 2006-01-04 15:54:36 +0000 | [diff] [blame] | 1478 | cmd ::= DROP INDEX ifexists(E) fullname(X). {sqlite3DropIndex(pParse, X, E);} |
drh | 982cef7 | 2000-05-30 16:27:03 +0000 | [diff] [blame] | 1479 | |
drh | 382c024 | 2001-10-06 16:33:02 +0000 | [diff] [blame] | 1480 | ///////////////////////////// The VACUUM command ///////////////////////////// |
| 1481 | // |
drh | c601f10 | 2020-07-03 17:24:35 +0000 | [diff] [blame] | 1482 | %if !SQLITE_OMIT_VACUUM && !SQLITE_OMIT_ATTACH |
drh | 2f6239e | 2018-12-08 00:43:08 +0000 | [diff] [blame] | 1483 | %type vinto {Expr*} |
| 1484 | %destructor vinto {sqlite3ExprDelete(pParse->db, $$);} |
| 1485 | cmd ::= VACUUM vinto(Y). {sqlite3Vacuum(pParse,0,Y);} |
| 1486 | cmd ::= VACUUM nm(X) vinto(Y). {sqlite3Vacuum(pParse,&X,Y);} |
| 1487 | vinto(A) ::= INTO expr(X). {A = X;} |
| 1488 | vinto(A) ::= . {A = 0;} |
drh | c601f10 | 2020-07-03 17:24:35 +0000 | [diff] [blame] | 1489 | %endif |
drh | f57b14a | 2001-09-14 18:54:08 +0000 | [diff] [blame] | 1490 | |
drh | 382c024 | 2001-10-06 16:33:02 +0000 | [diff] [blame] | 1491 | ///////////////////////////// The PRAGMA command ///////////////////////////// |
| 1492 | // |
drh | 13d7042 | 2004-11-13 15:59:14 +0000 | [diff] [blame] | 1493 | %ifndef SQLITE_OMIT_PRAGMA |
drh | ada2ee0 | 2009-04-03 01:43:57 +0000 | [diff] [blame] | 1494 | cmd ::= PRAGMA nm(X) dbnm(Z). {sqlite3Pragma(pParse,&X,&Z,0,0);} |
| 1495 | cmd ::= PRAGMA nm(X) dbnm(Z) EQ nmnum(Y). {sqlite3Pragma(pParse,&X,&Z,&Y,0);} |
drh | a3eb4b4 | 2007-01-27 02:38:29 +0000 | [diff] [blame] | 1496 | cmd ::= PRAGMA nm(X) dbnm(Z) LP nmnum(Y) RP. {sqlite3Pragma(pParse,&X,&Z,&Y,0);} |
drh | ada2ee0 | 2009-04-03 01:43:57 +0000 | [diff] [blame] | 1497 | cmd ::= PRAGMA nm(X) dbnm(Z) EQ minus_num(Y). |
| 1498 | {sqlite3Pragma(pParse,&X,&Z,&Y,1);} |
| 1499 | cmd ::= PRAGMA nm(X) dbnm(Z) LP minus_num(Y) RP. |
| 1500 | {sqlite3Pragma(pParse,&X,&Z,&Y,1);} |
| 1501 | |
drh | cf82f0d | 2016-02-17 04:33:10 +0000 | [diff] [blame] | 1502 | nmnum(A) ::= plus_num(A). |
| 1503 | nmnum(A) ::= nm(A). |
| 1504 | nmnum(A) ::= ON(A). |
| 1505 | nmnum(A) ::= DELETE(A). |
| 1506 | nmnum(A) ::= DEFAULT(A). |
drh | 154d4b2 | 2006-09-21 11:02:16 +0000 | [diff] [blame] | 1507 | %endif SQLITE_OMIT_PRAGMA |
drh | f59b12f | 2014-01-11 03:54:05 +0000 | [diff] [blame] | 1508 | %token_class number INTEGER|FLOAT. |
drh | 8395b7b | 2012-01-28 19:44:22 +0000 | [diff] [blame] | 1509 | plus_num(A) ::= PLUS number(X). {A = X;} |
drh | cf82f0d | 2016-02-17 04:33:10 +0000 | [diff] [blame] | 1510 | plus_num(A) ::= number(A). |
drh | f57b14a | 2001-09-14 18:54:08 +0000 | [diff] [blame] | 1511 | minus_num(A) ::= MINUS number(X). {A = X;} |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 1512 | //////////////////////////// The CREATE TRIGGER command ///////////////////// |
drh | f0f258b | 2003-04-21 18:48:45 +0000 | [diff] [blame] | 1513 | |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 1514 | %ifndef SQLITE_OMIT_TRIGGER |
| 1515 | |
drh | d9da78a | 2009-03-24 15:08:09 +0000 | [diff] [blame] | 1516 | cmd ::= createkw trigger_decl(A) BEGIN trigger_cmd_list(S) END(Z). { |
drh | 4b59ab5 | 2002-08-24 18:24:51 +0000 | [diff] [blame] | 1517 | Token all; |
| 1518 | all.z = A.z; |
drh | b27b7f5 | 2008-12-10 18:03:45 +0000 | [diff] [blame] | 1519 | all.n = (int)(Z.z - A.z) + Z.n; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1520 | sqlite3FinishTrigger(pParse, S, &all); |
drh | f0f258b | 2003-04-21 18:48:45 +0000 | [diff] [blame] | 1521 | } |
| 1522 | |
drh | fdd48a7 | 2006-09-11 23:45:48 +0000 | [diff] [blame] | 1523 | trigger_decl(A) ::= temp(T) TRIGGER ifnotexists(NOERR) nm(B) dbnm(Z) |
| 1524 | trigger_time(C) trigger_event(D) |
drh | 60218d2 | 2007-04-06 11:26:00 +0000 | [diff] [blame] | 1525 | ON fullname(E) foreach_clause when_clause(G). { |
| 1526 | sqlite3BeginTrigger(pParse, &B, &Z, C, D.a, D.b, E, G, T, NOERR); |
drh | cf82f0d | 2016-02-17 04:33:10 +0000 | [diff] [blame] | 1527 | A = (Z.n==0?B:Z); /*A-overwrites-T*/ |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 1528 | } |
| 1529 | |
drh | c4dd3fd | 2008-01-22 01:48:05 +0000 | [diff] [blame] | 1530 | %type trigger_time {int} |
drh | 6559e2c | 2017-06-28 14:26:37 +0000 | [diff] [blame] | 1531 | trigger_time(A) ::= BEFORE|AFTER(X). { A = @X; /*A-overwrites-X*/ } |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 1532 | trigger_time(A) ::= INSTEAD OF. { A = TK_INSTEAD;} |
| 1533 | trigger_time(A) ::= . { A = TK_BEFORE; } |
| 1534 | |
drh | ad3cab5 | 2002-05-24 02:04:32 +0000 | [diff] [blame] | 1535 | %type trigger_event {struct TrigEvent} |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 1536 | %destructor trigger_event {sqlite3IdListDelete(pParse->db, $$.b);} |
drh | cf82f0d | 2016-02-17 04:33:10 +0000 | [diff] [blame] | 1537 | trigger_event(A) ::= DELETE|INSERT(X). {A.a = @X; /*A-overwrites-X*/ A.b = 0;} |
| 1538 | trigger_event(A) ::= UPDATE(X). {A.a = @X; /*A-overwrites-X*/ A.b = 0;} |
| 1539 | trigger_event(A) ::= UPDATE OF idlist(X).{A.a = TK_UPDATE; A.b = X;} |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 1540 | |
drh | 60218d2 | 2007-04-06 11:26:00 +0000 | [diff] [blame] | 1541 | foreach_clause ::= . |
| 1542 | foreach_clause ::= FOR EACH ROW. |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 1543 | |
drh | 0bb132b | 2004-07-20 14:06:51 +0000 | [diff] [blame] | 1544 | %type when_clause {Expr*} |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 1545 | %destructor when_clause {sqlite3ExprDelete(pParse->db, $$);} |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 1546 | when_clause(A) ::= . { A = 0; } |
drh | 1be266b | 2017-12-24 00:18:47 +0000 | [diff] [blame] | 1547 | when_clause(A) ::= WHEN expr(X). { A = X; } |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 1548 | |
drh | 0bb132b | 2004-07-20 14:06:51 +0000 | [diff] [blame] | 1549 | %type trigger_cmd_list {TriggerStep*} |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 1550 | %destructor trigger_cmd_list {sqlite3DeleteTriggerStep(pParse->db, $$);} |
drh | 4dd0d3f | 2016-02-17 01:18:33 +0000 | [diff] [blame] | 1551 | trigger_cmd_list(A) ::= trigger_cmd_list(A) trigger_cmd(X) SEMI. { |
| 1552 | assert( A!=0 ); |
| 1553 | A->pLast->pNext = X; |
| 1554 | A->pLast = X; |
drh | a69d916 | 2003-04-17 22:57:53 +0000 | [diff] [blame] | 1555 | } |
drh | 4dd0d3f | 2016-02-17 01:18:33 +0000 | [diff] [blame] | 1556 | trigger_cmd_list(A) ::= trigger_cmd(A) SEMI. { |
| 1557 | assert( A!=0 ); |
| 1558 | A->pLast = A; |
drh | 8123896 | 2008-08-11 14:26:35 +0000 | [diff] [blame] | 1559 | } |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 1560 | |
drh | b1819a0 | 2009-07-03 15:37:27 +0000 | [diff] [blame] | 1561 | // Disallow qualified table names on INSERT, UPDATE, and DELETE statements |
| 1562 | // within a trigger. The table to INSERT, UPDATE, or DELETE is always in |
| 1563 | // the same database as the table that the trigger fires on. |
| 1564 | // |
| 1565 | %type trnm {Token} |
drh | 4dd0d3f | 2016-02-17 01:18:33 +0000 | [diff] [blame] | 1566 | trnm(A) ::= nm(A). |
drh | b1819a0 | 2009-07-03 15:37:27 +0000 | [diff] [blame] | 1567 | trnm(A) ::= nm DOT nm(X). { |
| 1568 | A = X; |
| 1569 | sqlite3ErrorMsg(pParse, |
| 1570 | "qualified table names are not allowed on INSERT, UPDATE, and DELETE " |
| 1571 | "statements within triggers"); |
| 1572 | } |
| 1573 | |
| 1574 | // Disallow the INDEX BY and NOT INDEXED clauses on UPDATE and DELETE |
| 1575 | // statements within triggers. We make a specific error message for this |
| 1576 | // since it is an exception to the default grammar rules. |
| 1577 | // |
| 1578 | tridxby ::= . |
| 1579 | tridxby ::= INDEXED BY nm. { |
| 1580 | sqlite3ErrorMsg(pParse, |
| 1581 | "the INDEXED BY clause is not allowed on UPDATE or DELETE statements " |
| 1582 | "within triggers"); |
| 1583 | } |
| 1584 | tridxby ::= NOT INDEXED. { |
| 1585 | sqlite3ErrorMsg(pParse, |
| 1586 | "the NOT INDEXED clause is not allowed on UPDATE or DELETE statements " |
| 1587 | "within triggers"); |
| 1588 | } |
| 1589 | |
| 1590 | |
| 1591 | |
drh | 0bb132b | 2004-07-20 14:06:51 +0000 | [diff] [blame] | 1592 | %type trigger_cmd {TriggerStep*} |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 1593 | %destructor trigger_cmd {sqlite3DeleteTriggerStep(pParse->db, $$);} |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 1594 | // UPDATE |
drh | b1819a0 | 2009-07-03 15:37:27 +0000 | [diff] [blame] | 1595 | trigger_cmd(A) ::= |
dan | e7877b2 | 2020-07-14 19:51:01 +0000 | [diff] [blame] | 1596 | UPDATE(B) orconf(R) trnm(X) tridxby SET setlist(Y) from(F) where_opt(Z) scanpt(E). |
| 1597 | {A = sqlite3TriggerUpdateStep(pParse, &X, F, Y, Z, R, B.z, E);} |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 1598 | |
| 1599 | // INSERT |
drh | f259df5 | 2017-12-27 20:38:35 +0000 | [diff] [blame] | 1600 | trigger_cmd(A) ::= scanpt(B) insert_cmd(R) INTO |
drh | 2c2e844 | 2018-04-07 15:04:05 +0000 | [diff] [blame] | 1601 | trnm(X) idlist_opt(F) select(S) upsert(U) scanpt(Z). { |
dan | 5be60c5 | 2018-08-15 20:28:39 +0000 | [diff] [blame] | 1602 | A = sqlite3TriggerInsertStep(pParse,&X,F,S,R,U,B,Z);/*A-overwrites-R*/ |
drh | 2c2e844 | 2018-04-07 15:04:05 +0000 | [diff] [blame] | 1603 | } |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 1604 | // DELETE |
drh | f259df5 | 2017-12-27 20:38:35 +0000 | [diff] [blame] | 1605 | trigger_cmd(A) ::= DELETE(B) FROM trnm(X) tridxby where_opt(Y) scanpt(E). |
dan | 5be60c5 | 2018-08-15 20:28:39 +0000 | [diff] [blame] | 1606 | {A = sqlite3TriggerDeleteStep(pParse, &X, Y, B.z, E);} |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 1607 | |
| 1608 | // SELECT |
drh | f259df5 | 2017-12-27 20:38:35 +0000 | [diff] [blame] | 1609 | trigger_cmd(A) ::= scanpt(B) select(X) scanpt(E). |
| 1610 | {A = sqlite3TriggerSelectStep(pParse->db, X, B, E); /*A-overwrites-X*/} |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 1611 | |
danielk1977 | 6f34903 | 2002-06-11 02:25:40 +0000 | [diff] [blame] | 1612 | // The special RAISE expression that may occur in trigger programs |
drh | 1be266b | 2017-12-24 00:18:47 +0000 | [diff] [blame] | 1613 | expr(A) ::= RAISE LP IGNORE RP. { |
| 1614 | A = sqlite3PExpr(pParse, TK_RAISE, 0, 0); |
| 1615 | if( A ){ |
drh | 1194904 | 2019-08-05 18:01:42 +0000 | [diff] [blame] | 1616 | A->affExpr = OE_Ignore; |
drh | 8aa34ae | 2006-03-13 12:54:09 +0000 | [diff] [blame] | 1617 | } |
drh | 4b59ab5 | 2002-08-24 18:24:51 +0000 | [diff] [blame] | 1618 | } |
drh | 1be266b | 2017-12-24 00:18:47 +0000 | [diff] [blame] | 1619 | expr(A) ::= RAISE LP raisetype(T) COMMA nm(Z) RP. { |
drh | 796588a | 2022-02-05 21:49:47 +0000 | [diff] [blame] | 1620 | A = sqlite3ExprAlloc(pParse->db, TK_RAISE, &Z, 1); |
drh | 1be266b | 2017-12-24 00:18:47 +0000 | [diff] [blame] | 1621 | if( A ) { |
drh | 1194904 | 2019-08-05 18:01:42 +0000 | [diff] [blame] | 1622 | A->affExpr = (char)T; |
drh | 8aa34ae | 2006-03-13 12:54:09 +0000 | [diff] [blame] | 1623 | } |
drh | 4b59ab5 | 2002-08-24 18:24:51 +0000 | [diff] [blame] | 1624 | } |
drh | 154d4b2 | 2006-09-21 11:02:16 +0000 | [diff] [blame] | 1625 | %endif !SQLITE_OMIT_TRIGGER |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 1626 | |
drh | 74ad7fe | 2004-10-07 03:06:28 +0000 | [diff] [blame] | 1627 | %type raisetype {int} |
| 1628 | raisetype(A) ::= ROLLBACK. {A = OE_Rollback;} |
| 1629 | raisetype(A) ::= ABORT. {A = OE_Abort;} |
| 1630 | raisetype(A) ::= FAIL. {A = OE_Fail;} |
| 1631 | |
danielk1977 | 6f34903 | 2002-06-11 02:25:40 +0000 | [diff] [blame] | 1632 | |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 1633 | //////////////////////// DROP TRIGGER statement ////////////////////////////// |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 1634 | %ifndef SQLITE_OMIT_TRIGGER |
drh | fdd48a7 | 2006-09-11 23:45:48 +0000 | [diff] [blame] | 1635 | cmd ::= DROP TRIGGER ifexists(NOERR) fullname(X). { |
| 1636 | sqlite3DropTrigger(pParse,X,NOERR); |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 1637 | } |
drh | 154d4b2 | 2006-09-21 11:02:16 +0000 | [diff] [blame] | 1638 | %endif !SQLITE_OMIT_TRIGGER |
drh | 113088e | 2003-03-20 01:16:58 +0000 | [diff] [blame] | 1639 | |
| 1640 | //////////////////////// ATTACH DATABASE file AS name ///////////////////////// |
drh | fdbcdee | 2007-03-27 14:44:50 +0000 | [diff] [blame] | 1641 | %ifndef SQLITE_OMIT_ATTACH |
danielk1977 | f744bb5 | 2005-12-06 17:19:11 +0000 | [diff] [blame] | 1642 | cmd ::= ATTACH database_kw_opt expr(F) AS expr(D) key_opt(K). { |
drh | 1be266b | 2017-12-24 00:18:47 +0000 | [diff] [blame] | 1643 | sqlite3Attach(pParse, F, D, K); |
drh | 1c2d841 | 2003-03-31 00:30:47 +0000 | [diff] [blame] | 1644 | } |
drh | fdbcdee | 2007-03-27 14:44:50 +0000 | [diff] [blame] | 1645 | cmd ::= DETACH database_kw_opt expr(D). { |
drh | 1be266b | 2017-12-24 00:18:47 +0000 | [diff] [blame] | 1646 | sqlite3Detach(pParse, D); |
drh | fdbcdee | 2007-03-27 14:44:50 +0000 | [diff] [blame] | 1647 | } |
| 1648 | |
drh | c4dd3fd | 2008-01-22 01:48:05 +0000 | [diff] [blame] | 1649 | %type key_opt {Expr*} |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 1650 | %destructor key_opt {sqlite3ExprDelete(pParse->db, $$);} |
danielk1977 | f744bb5 | 2005-12-06 17:19:11 +0000 | [diff] [blame] | 1651 | key_opt(A) ::= . { A = 0; } |
drh | 1be266b | 2017-12-24 00:18:47 +0000 | [diff] [blame] | 1652 | key_opt(A) ::= KEY expr(X). { A = X; } |
drh | 113088e | 2003-03-20 01:16:58 +0000 | [diff] [blame] | 1653 | |
| 1654 | database_kw_opt ::= DATABASE. |
| 1655 | database_kw_opt ::= . |
drh | fdbcdee | 2007-03-27 14:44:50 +0000 | [diff] [blame] | 1656 | %endif SQLITE_OMIT_ATTACH |
drh | 4343fea | 2004-11-05 23:46:15 +0000 | [diff] [blame] | 1657 | |
| 1658 | ////////////////////////// REINDEX collation ////////////////////////////////// |
| 1659 | %ifndef SQLITE_OMIT_REINDEX |
| 1660 | cmd ::= REINDEX. {sqlite3Reindex(pParse, 0, 0);} |
| 1661 | cmd ::= REINDEX nm(X) dbnm(Y). {sqlite3Reindex(pParse, &X, &Y);} |
drh | 154d4b2 | 2006-09-21 11:02:16 +0000 | [diff] [blame] | 1662 | %endif SQLITE_OMIT_REINDEX |
danielk1977 | 9fd2a9a | 2004-11-12 13:42:30 +0000 | [diff] [blame] | 1663 | |
drh | 9f18e8a | 2005-07-08 12:13:04 +0000 | [diff] [blame] | 1664 | /////////////////////////////////// ANALYZE /////////////////////////////////// |
| 1665 | %ifndef SQLITE_OMIT_ANALYZE |
| 1666 | cmd ::= ANALYZE. {sqlite3Analyze(pParse, 0, 0);} |
| 1667 | cmd ::= ANALYZE nm(X) dbnm(Y). {sqlite3Analyze(pParse, &X, &Y);} |
| 1668 | %endif |
| 1669 | |
danielk1977 | 9fd2a9a | 2004-11-12 13:42:30 +0000 | [diff] [blame] | 1670 | //////////////////////// ALTER TABLE table ... //////////////////////////////// |
dan | 37f3ac8 | 2021-10-01 20:39:50 +0000 | [diff] [blame] | 1671 | %ifndef SQLITE_OMIT_ALTERTABLE |
| 1672 | %ifndef SQLITE_OMIT_VIRTUALTABLE |
danielk1977 | 9fd2a9a | 2004-11-12 13:42:30 +0000 | [diff] [blame] | 1673 | cmd ::= ALTER TABLE fullname(X) RENAME TO nm(Z). { |
| 1674 | sqlite3AlterRenameTable(pParse,X,&Z); |
| 1675 | } |
drh | 986dde7 | 2016-02-29 13:37:21 +0000 | [diff] [blame] | 1676 | cmd ::= ALTER TABLE add_column_fullname |
| 1677 | ADD kwcolumn_opt columnname(Y) carglist. { |
| 1678 | Y.n = (int)(pParse->sLastToken.z-Y.z) + pParse->sLastToken.n; |
danielk1977 | 19a8e7e | 2005-03-17 05:03:38 +0000 | [diff] [blame] | 1679 | sqlite3AlterFinishAddColumn(pParse, &Y); |
| 1680 | } |
drh | 329cb9e | 2021-02-19 09:36:50 +0000 | [diff] [blame] | 1681 | cmd ::= ALTER TABLE fullname(X) DROP kwcolumn_opt nm(Y). { |
dan | 6e6d983 | 2021-02-16 20:43:36 +0000 | [diff] [blame] | 1682 | sqlite3AlterDropColumn(pParse, X, &Y); |
| 1683 | } |
| 1684 | |
danielk1977 | 19a8e7e | 2005-03-17 05:03:38 +0000 | [diff] [blame] | 1685 | add_column_fullname ::= fullname(X). { |
drh | 4a642b6 | 2016-02-05 01:55:27 +0000 | [diff] [blame] | 1686 | disableLookaside(pParse); |
danielk1977 | 19a8e7e | 2005-03-17 05:03:38 +0000 | [diff] [blame] | 1687 | sqlite3AlterBeginAddColumn(pParse, X); |
| 1688 | } |
dan | cf8f289 | 2018-08-09 20:47:01 +0000 | [diff] [blame] | 1689 | cmd ::= ALTER TABLE fullname(X) RENAME kwcolumn_opt nm(Y) TO nm(Z). { |
| 1690 | sqlite3AlterRenameColumn(pParse, X, &Y, &Z); |
| 1691 | } |
| 1692 | |
danielk1977 | 19a8e7e | 2005-03-17 05:03:38 +0000 | [diff] [blame] | 1693 | kwcolumn_opt ::= . |
| 1694 | kwcolumn_opt ::= COLUMNKW. |
dan | cf8f289 | 2018-08-09 20:47:01 +0000 | [diff] [blame] | 1695 | |
dan | 37f3ac8 | 2021-10-01 20:39:50 +0000 | [diff] [blame] | 1696 | %endif SQLITE_OMIT_VIRTUALTABLE |
| 1697 | %endif SQLITE_OMIT_ALTERTABLE |
drh | e09daa9 | 2006-06-10 13:29:31 +0000 | [diff] [blame] | 1698 | |
| 1699 | //////////////////////// CREATE VIRTUAL TABLE ... ///////////////////////////// |
| 1700 | %ifndef SQLITE_OMIT_VIRTUALTABLE |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 1701 | cmd ::= create_vtab. {sqlite3VtabFinishParse(pParse,0);} |
| 1702 | cmd ::= create_vtab LP vtabarglist RP(X). {sqlite3VtabFinishParse(pParse,&X);} |
drh | b421b89 | 2012-01-28 19:41:53 +0000 | [diff] [blame] | 1703 | create_vtab ::= createkw VIRTUAL TABLE ifnotexists(E) |
| 1704 | nm(X) dbnm(Y) USING nm(Z). { |
| 1705 | sqlite3VtabBeginParse(pParse, &X, &Y, &Z, E); |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 1706 | } |
drh | e09daa9 | 2006-06-10 13:29:31 +0000 | [diff] [blame] | 1707 | vtabarglist ::= vtabarg. |
| 1708 | vtabarglist ::= vtabarglist COMMA vtabarg. |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 1709 | vtabarg ::= . {sqlite3VtabArgInit(pParse);} |
| 1710 | vtabarg ::= vtabarg vtabargtoken. |
| 1711 | vtabargtoken ::= ANY(X). {sqlite3VtabArgExtend(pParse,&X);} |
| 1712 | vtabargtoken ::= lp anylist RP(X). {sqlite3VtabArgExtend(pParse,&X);} |
| 1713 | lp ::= LP(X). {sqlite3VtabArgExtend(pParse,&X);} |
| 1714 | anylist ::= . |
drh | aaac8b4 | 2009-05-11 18:22:30 +0000 | [diff] [blame] | 1715 | anylist ::= anylist LP anylist RP. |
| 1716 | anylist ::= anylist ANY. |
drh | 154d4b2 | 2006-09-21 11:02:16 +0000 | [diff] [blame] | 1717 | %endif SQLITE_OMIT_VIRTUALTABLE |
drh | 8b47186 | 2014-01-11 13:22:17 +0000 | [diff] [blame] | 1718 | |
| 1719 | |
| 1720 | //////////////////////// COMMON TABLE EXPRESSIONS //////////////////////////// |
dan | 7d562db | 2014-01-11 19:19:36 +0000 | [diff] [blame] | 1721 | %type wqlist {With*} |
dan | 4e9119d | 2014-01-13 15:12:23 +0000 | [diff] [blame] | 1722 | %destructor wqlist {sqlite3WithDelete(pParse->db, $$);} |
drh | f824b41 | 2021-02-20 14:57:16 +0000 | [diff] [blame] | 1723 | %type wqitem {Cte*} |
| 1724 | // %destructor wqitem {sqlite3CteDelete(pParse->db, $$);} // not reachable |
dan | 7d562db | 2014-01-11 19:19:36 +0000 | [diff] [blame] | 1725 | |
drh | a5746e0 | 2018-04-09 20:36:09 +0000 | [diff] [blame] | 1726 | with ::= . |
drh | 8b47186 | 2014-01-11 13:22:17 +0000 | [diff] [blame] | 1727 | %ifndef SQLITE_OMIT_CTE |
drh | a5746e0 | 2018-04-09 20:36:09 +0000 | [diff] [blame] | 1728 | with ::= WITH wqlist(W). { sqlite3WithPush(pParse, W, 1); } |
| 1729 | with ::= WITH RECURSIVE wqlist(W). { sqlite3WithPush(pParse, W, 1); } |
dan | 7d562db | 2014-01-11 19:19:36 +0000 | [diff] [blame] | 1730 | |
drh | 745912e | 2021-02-22 03:04:25 +0000 | [diff] [blame] | 1731 | %type wqas {u8} |
| 1732 | wqas(A) ::= AS. {A = M10d_Any;} |
| 1733 | wqas(A) ::= AS MATERIALIZED. {A = M10d_Yes;} |
| 1734 | wqas(A) ::= AS NOT MATERIALIZED. {A = M10d_No;} |
| 1735 | wqitem(A) ::= nm(X) eidlist_opt(Y) wqas(M) LP select(Z) RP. { |
| 1736 | A = sqlite3CteNew(pParse, &X, Y, Z, M); /*A-overwrites-X*/ |
dan | 7d562db | 2014-01-11 19:19:36 +0000 | [diff] [blame] | 1737 | } |
drh | f824b41 | 2021-02-20 14:57:16 +0000 | [diff] [blame] | 1738 | wqlist(A) ::= wqitem(X). { |
| 1739 | A = sqlite3WithAdd(pParse, 0, X); /*A-overwrites-X*/ |
| 1740 | } |
| 1741 | wqlist(A) ::= wqlist(A) COMMA wqitem(X). { |
| 1742 | A = sqlite3WithAdd(pParse, A, X); |
drh | 8b47186 | 2014-01-11 13:22:17 +0000 | [diff] [blame] | 1743 | } |
| 1744 | %endif SQLITE_OMIT_CTE |
dan | 34a7d79 | 2018-06-29 20:43:33 +0000 | [diff] [blame] | 1745 | |
| 1746 | //////////////////////// WINDOW FUNCTION EXPRESSIONS ///////////////////////// |
dan | 6e2210e | 2018-06-30 18:54:56 +0000 | [diff] [blame] | 1747 | // These must be at the end of this file. Specifically, the rules that |
| 1748 | // introduce tokens WINDOW, OVER and FILTER must appear last. This causes |
| 1749 | // the integer values assigned to these tokens to be larger than all other |
| 1750 | // tokens that may be output by the tokenizer except TK_SPACE and TK_ILLEGAL. |
dan | 34a7d79 | 2018-06-29 20:43:33 +0000 | [diff] [blame] | 1751 | // |
| 1752 | %ifndef SQLITE_OMIT_WINDOWFUNC |
| 1753 | %type windowdefn_list {Window*} |
drh | a57aac2 | 2018-07-09 16:24:00 +0000 | [diff] [blame] | 1754 | %destructor windowdefn_list {sqlite3WindowListDelete(pParse->db, $$);} |
dan | 34a7d79 | 2018-06-29 20:43:33 +0000 | [diff] [blame] | 1755 | windowdefn_list(A) ::= windowdefn(Z). { A = Z; } |
| 1756 | windowdefn_list(A) ::= windowdefn_list(Y) COMMA windowdefn(Z). { |
drh | a57aac2 | 2018-07-09 16:24:00 +0000 | [diff] [blame] | 1757 | assert( Z!=0 ); |
dan | e7c9ca4 | 2019-02-16 17:27:51 +0000 | [diff] [blame] | 1758 | sqlite3WindowChain(pParse, Z, Y); |
drh | a57aac2 | 2018-07-09 16:24:00 +0000 | [diff] [blame] | 1759 | Z->pNextWin = Y; |
dan | 34a7d79 | 2018-06-29 20:43:33 +0000 | [diff] [blame] | 1760 | A = Z; |
| 1761 | } |
| 1762 | |
| 1763 | %type windowdefn {Window*} |
| 1764 | %destructor windowdefn {sqlite3WindowDelete(pParse->db, $$);} |
dan | e7c9ca4 | 2019-02-16 17:27:51 +0000 | [diff] [blame] | 1765 | windowdefn(A) ::= nm(X) AS LP window(Y) RP. { |
drh | a57aac2 | 2018-07-09 16:24:00 +0000 | [diff] [blame] | 1766 | if( ALWAYS(Y) ){ |
dan | 34a7d79 | 2018-06-29 20:43:33 +0000 | [diff] [blame] | 1767 | Y->zName = sqlite3DbStrNDup(pParse->db, X.z, X.n); |
| 1768 | } |
| 1769 | A = Y; |
| 1770 | } |
| 1771 | |
dan | 34a7d79 | 2018-06-29 20:43:33 +0000 | [diff] [blame] | 1772 | %type window {Window*} |
| 1773 | %destructor window {sqlite3WindowDelete(pParse->db, $$);} |
| 1774 | |
| 1775 | %type frame_opt {Window*} |
| 1776 | %destructor frame_opt {sqlite3WindowDelete(pParse->db, $$);} |
| 1777 | |
dan | 34a7d79 | 2018-06-29 20:43:33 +0000 | [diff] [blame] | 1778 | %type part_opt {ExprList*} |
| 1779 | %destructor part_opt {sqlite3ExprListDelete(pParse->db, $$);} |
| 1780 | |
dan | 6488271 | 2019-07-11 18:43:33 +0000 | [diff] [blame] | 1781 | %type filter_clause {Expr*} |
| 1782 | %destructor filter_clause {sqlite3ExprDelete(pParse->db, $$);} |
| 1783 | |
| 1784 | %type over_clause {Window*} |
| 1785 | %destructor over_clause {sqlite3WindowDelete(pParse->db, $$);} |
dan | 34a7d79 | 2018-06-29 20:43:33 +0000 | [diff] [blame] | 1786 | |
dan | 4f9adee | 2019-07-13 16:22:50 +0000 | [diff] [blame] | 1787 | %type filter_over {Window*} |
| 1788 | %destructor filter_over {sqlite3WindowDelete(pParse->db, $$);} |
| 1789 | |
dan | 34a7d79 | 2018-06-29 20:43:33 +0000 | [diff] [blame] | 1790 | %type range_or_rows {int} |
| 1791 | |
| 1792 | %type frame_bound {struct FrameBound} |
| 1793 | %destructor frame_bound {sqlite3ExprDelete(pParse->db, $$.pExpr);} |
dan | 287fa17 | 2018-07-06 13:48:09 +0000 | [diff] [blame] | 1794 | %type frame_bound_s {struct FrameBound} |
| 1795 | %destructor frame_bound_s {sqlite3ExprDelete(pParse->db, $$.pExpr);} |
| 1796 | %type frame_bound_e {struct FrameBound} |
| 1797 | %destructor frame_bound_e {sqlite3ExprDelete(pParse->db, $$.pExpr);} |
dan | 34a7d79 | 2018-06-29 20:43:33 +0000 | [diff] [blame] | 1798 | |
dan | e7c9ca4 | 2019-02-16 17:27:51 +0000 | [diff] [blame] | 1799 | window(A) ::= PARTITION BY nexprlist(X) orderby_opt(Y) frame_opt(Z). { |
| 1800 | A = sqlite3WindowAssemble(pParse, Z, X, Y, 0); |
| 1801 | } |
| 1802 | window(A) ::= nm(W) PARTITION BY nexprlist(X) orderby_opt(Y) frame_opt(Z). { |
| 1803 | A = sqlite3WindowAssemble(pParse, Z, X, Y, &W); |
| 1804 | } |
| 1805 | window(A) ::= ORDER BY sortlist(Y) frame_opt(Z). { |
| 1806 | A = sqlite3WindowAssemble(pParse, Z, 0, Y, 0); |
| 1807 | } |
| 1808 | window(A) ::= nm(W) ORDER BY sortlist(Y) frame_opt(Z). { |
| 1809 | A = sqlite3WindowAssemble(pParse, Z, 0, Y, &W); |
| 1810 | } |
| 1811 | window(A) ::= frame_opt(Z). { |
dan | 34a7d79 | 2018-06-29 20:43:33 +0000 | [diff] [blame] | 1812 | A = Z; |
dan | e7c9ca4 | 2019-02-16 17:27:51 +0000 | [diff] [blame] | 1813 | } |
| 1814 | window(A) ::= nm(W) frame_opt(Z). { |
| 1815 | A = sqlite3WindowAssemble(pParse, Z, 0, 0, &W); |
dan | 34a7d79 | 2018-06-29 20:43:33 +0000 | [diff] [blame] | 1816 | } |
| 1817 | |
dan | 34a7d79 | 2018-06-29 20:43:33 +0000 | [diff] [blame] | 1818 | frame_opt(A) ::= . { |
dan | d35300f | 2019-03-14 20:53:21 +0000 | [diff] [blame] | 1819 | A = sqlite3WindowAlloc(pParse, 0, TK_UNBOUNDED, 0, TK_CURRENT, 0, 0); |
dan | 34a7d79 | 2018-06-29 20:43:33 +0000 | [diff] [blame] | 1820 | } |
dan | d35300f | 2019-03-14 20:53:21 +0000 | [diff] [blame] | 1821 | frame_opt(A) ::= range_or_rows(X) frame_bound_s(Y) frame_exclude_opt(Z). { |
| 1822 | A = sqlite3WindowAlloc(pParse, X, Y.eType, Y.pExpr, TK_CURRENT, 0, Z); |
dan | 34a7d79 | 2018-06-29 20:43:33 +0000 | [diff] [blame] | 1823 | } |
drh | 0f134f0 | 2019-04-02 18:12:20 +0000 | [diff] [blame] | 1824 | frame_opt(A) ::= range_or_rows(X) BETWEEN frame_bound_s(Y) AND |
| 1825 | frame_bound_e(Z) frame_exclude_opt(W). { |
dan | d35300f | 2019-03-14 20:53:21 +0000 | [diff] [blame] | 1826 | A = sqlite3WindowAlloc(pParse, X, Y.eType, Y.pExpr, Z.eType, Z.pExpr, W); |
dan | 34a7d79 | 2018-06-29 20:43:33 +0000 | [diff] [blame] | 1827 | } |
| 1828 | |
drh | 0f134f0 | 2019-04-02 18:12:20 +0000 | [diff] [blame] | 1829 | range_or_rows(A) ::= RANGE|ROWS|GROUPS(X). {A = @X; /*A-overwrites-X*/} |
dan | 34a7d79 | 2018-06-29 20:43:33 +0000 | [diff] [blame] | 1830 | |
drh | 0f134f0 | 2019-04-02 18:12:20 +0000 | [diff] [blame] | 1831 | frame_bound_s(A) ::= frame_bound(X). {A = X;} |
| 1832 | frame_bound_s(A) ::= UNBOUNDED(X) PRECEDING. {A.eType = @X; A.pExpr = 0;} |
| 1833 | frame_bound_e(A) ::= frame_bound(X). {A = X;} |
| 1834 | frame_bound_e(A) ::= UNBOUNDED(X) FOLLOWING. {A.eType = @X; A.pExpr = 0;} |
dan | 287fa17 | 2018-07-06 13:48:09 +0000 | [diff] [blame] | 1835 | |
drh | 0f134f0 | 2019-04-02 18:12:20 +0000 | [diff] [blame] | 1836 | frame_bound(A) ::= expr(X) PRECEDING|FOLLOWING(Y). |
| 1837 | {A.eType = @Y; A.pExpr = X;} |
| 1838 | frame_bound(A) ::= CURRENT(X) ROW. {A.eType = @X; A.pExpr = 0;} |
dan | 34a7d79 | 2018-06-29 20:43:33 +0000 | [diff] [blame] | 1839 | |
dan | d35300f | 2019-03-14 20:53:21 +0000 | [diff] [blame] | 1840 | %type frame_exclude_opt {u8} |
drh | 0f134f0 | 2019-04-02 18:12:20 +0000 | [diff] [blame] | 1841 | frame_exclude_opt(A) ::= . {A = 0;} |
| 1842 | frame_exclude_opt(A) ::= EXCLUDE frame_exclude(X). {A = X;} |
dan | d35300f | 2019-03-14 20:53:21 +0000 | [diff] [blame] | 1843 | |
| 1844 | %type frame_exclude {u8} |
drh | 0f134f0 | 2019-04-02 18:12:20 +0000 | [diff] [blame] | 1845 | frame_exclude(A) ::= NO(X) OTHERS. {A = @X; /*A-overwrites-X*/} |
| 1846 | frame_exclude(A) ::= CURRENT(X) ROW. {A = @X; /*A-overwrites-X*/} |
| 1847 | frame_exclude(A) ::= GROUP|TIES(X). {A = @X; /*A-overwrites-X*/} |
dan | d35300f | 2019-03-14 20:53:21 +0000 | [diff] [blame] | 1848 | |
| 1849 | |
drh | 550a330 | 2018-07-27 22:14:50 +0000 | [diff] [blame] | 1850 | %type window_clause {Window*} |
| 1851 | %destructor window_clause {sqlite3WindowListDelete(pParse->db, $$);} |
| 1852 | window_clause(A) ::= WINDOW windowdefn_list(B). { A = B; } |
dan | 6e2210e | 2018-06-30 18:54:56 +0000 | [diff] [blame] | 1853 | |
dan | 4f9adee | 2019-07-13 16:22:50 +0000 | [diff] [blame] | 1854 | filter_over(A) ::= filter_clause(F) over_clause(O). { |
drh | d4f7ec7 | 2021-04-08 14:15:26 +0000 | [diff] [blame] | 1855 | if( O ){ |
| 1856 | O->pFilter = F; |
| 1857 | }else{ |
| 1858 | sqlite3ExprDelete(pParse->db, F); |
| 1859 | } |
dan | 4f9adee | 2019-07-13 16:22:50 +0000 | [diff] [blame] | 1860 | A = O; |
dan | 6488271 | 2019-07-11 18:43:33 +0000 | [diff] [blame] | 1861 | } |
dan | 4f9adee | 2019-07-13 16:22:50 +0000 | [diff] [blame] | 1862 | filter_over(A) ::= over_clause(O). { |
| 1863 | A = O; |
dan | 6488271 | 2019-07-11 18:43:33 +0000 | [diff] [blame] | 1864 | } |
dan | 4f9adee | 2019-07-13 16:22:50 +0000 | [diff] [blame] | 1865 | filter_over(A) ::= filter_clause(F). { |
| 1866 | A = (Window*)sqlite3DbMallocZero(pParse->db, sizeof(Window)); |
| 1867 | if( A ){ |
| 1868 | A->eFrmType = TK_FILTER; |
| 1869 | A->pFilter = F; |
dan | 0088574 | 2019-07-13 18:27:54 +0000 | [diff] [blame] | 1870 | }else{ |
| 1871 | sqlite3ExprDelete(pParse->db, F); |
dan | 4f9adee | 2019-07-13 16:22:50 +0000 | [diff] [blame] | 1872 | } |
dan | 6488271 | 2019-07-11 18:43:33 +0000 | [diff] [blame] | 1873 | } |
| 1874 | |
| 1875 | over_clause(A) ::= OVER LP window(Z) RP. { |
dan | 6e2210e | 2018-06-30 18:54:56 +0000 | [diff] [blame] | 1876 | A = Z; |
drh | a57aac2 | 2018-07-09 16:24:00 +0000 | [diff] [blame] | 1877 | assert( A!=0 ); |
drh | a57aac2 | 2018-07-09 16:24:00 +0000 | [diff] [blame] | 1878 | } |
dan | 6488271 | 2019-07-11 18:43:33 +0000 | [diff] [blame] | 1879 | over_clause(A) ::= OVER nm(Z). { |
drh | a57aac2 | 2018-07-09 16:24:00 +0000 | [diff] [blame] | 1880 | A = (Window*)sqlite3DbMallocZero(pParse->db, sizeof(Window)); |
| 1881 | if( A ){ |
| 1882 | A->zName = sqlite3DbStrNDup(pParse->db, Z.z, Z.n); |
drh | a57aac2 | 2018-07-09 16:24:00 +0000 | [diff] [blame] | 1883 | } |
dan | 6e2210e | 2018-06-30 18:54:56 +0000 | [diff] [blame] | 1884 | } |
| 1885 | |
dan | 6488271 | 2019-07-11 18:43:33 +0000 | [diff] [blame] | 1886 | filter_clause(A) ::= FILTER LP WHERE expr(X) RP. { A = X; } |
drh | c7bf571 | 2018-07-09 22:49:01 +0000 | [diff] [blame] | 1887 | %endif /* SQLITE_OMIT_WINDOWFUNC */ |
drh | f1722ba | 2019-04-05 20:56:46 +0000 | [diff] [blame] | 1888 | |
| 1889 | /* |
| 1890 | ** The code generator needs some extra TK_ token values for tokens that |
| 1891 | ** are synthesized and do not actually appear in the grammar: |
| 1892 | */ |
| 1893 | %token |
drh | f1722ba | 2019-04-05 20:56:46 +0000 | [diff] [blame] | 1894 | COLUMN /* Reference to a table column */ |
| 1895 | AGG_FUNCTION /* An aggregate function */ |
| 1896 | AGG_COLUMN /* An aggregated column */ |
dan | ee6c5e5 | 2019-08-23 20:33:01 +0000 | [diff] [blame] | 1897 | TRUEFALSE /* True or false keyword */ |
| 1898 | ISNOT /* Combination of IS and NOT */ |
| 1899 | FUNCTION /* A function invocation */ |
drh | f1722ba | 2019-04-05 20:56:46 +0000 | [diff] [blame] | 1900 | UMINUS /* Unary minus */ |
| 1901 | UPLUS /* Unary plus */ |
| 1902 | TRUTH /* IS TRUE or IS FALSE or IS NOT TRUE or IS NOT FALSE */ |
| 1903 | REGISTER /* Reference to a VDBE register */ |
| 1904 | VECTOR /* Vector */ |
| 1905 | SELECT_COLUMN /* Choose a single column from a multi-column SELECT */ |
| 1906 | IF_NULL_ROW /* the if-null-row operator */ |
| 1907 | ASTERISK /* The "*" in count(*) and similar */ |
| 1908 | SPAN /* The span operator */ |
drh | 0542812 | 2021-05-24 00:17:04 +0000 | [diff] [blame] | 1909 | ERROR /* An expression containing an error */ |
drh | f1722ba | 2019-04-05 20:56:46 +0000 | [diff] [blame] | 1910 | . |
| 1911 | /* There must be no more than 255 tokens defined above. If this grammar |
| 1912 | ** is extended with new rules and tokens, they must either be so few in |
| 1913 | ** number that TK_SPAN is no more than 255, or else the new tokens must |
| 1914 | ** appear after this line. |
| 1915 | */ |
| 1916 | %include { |
| 1917 | #if TK_SPAN>255 |
| 1918 | # error too many tokens in the grammar |
| 1919 | #endif |
| 1920 | } |
| 1921 | |
| 1922 | /* |
| 1923 | ** The TK_SPACE and TK_ILLEGAL tokens must be the last two tokens. The |
| 1924 | ** parser depends on this. Those tokens are not used in any grammar rule. |
| 1925 | ** They are only used by the tokenizer. Declare them last so that they |
| 1926 | ** are guaranteed to be the last two tokens |
| 1927 | */ |
| 1928 | %token SPACE ILLEGAL. |