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