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