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