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. |
| 16 | ** |
drh | 06f6541 | 2005-11-03 02:03:13 +0000 | [diff] [blame^] | 17 | ** @(#) $Id: parse.y,v 1.182 2005/11/03 02:03:13 drh Exp $ |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 18 | */ |
drh | 487e262 | 2005-06-25 18:42:14 +0000 | [diff] [blame] | 19 | |
| 20 | // All token codes are small integers with #defines that begin with "TK_" |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 21 | %token_prefix TK_ |
drh | 487e262 | 2005-06-25 18:42:14 +0000 | [diff] [blame] | 22 | |
| 23 | // The type of the data attached to each token is Token. This is also the |
| 24 | // default type for non-terminals. |
| 25 | // |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 26 | %token_type {Token} |
drh | f57b14a | 2001-09-14 18:54:08 +0000 | [diff] [blame] | 27 | %default_type {Token} |
drh | 487e262 | 2005-06-25 18:42:14 +0000 | [diff] [blame] | 28 | |
| 29 | // The generated parser function takes a 4th argument as follows: |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 30 | %extra_argument {Parse *pParse} |
drh | 487e262 | 2005-06-25 18:42:14 +0000 | [diff] [blame] | 31 | |
| 32 | // This code runs whenever there is a syntax error |
| 33 | // |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 34 | %syntax_error { |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 35 | if( pParse->zErrMsg==0 ){ |
| 36 | if( TOKEN.z[0] ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 37 | sqlite3ErrorMsg(pParse, "near \"%T\": syntax error", &TOKEN); |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 38 | }else{ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 39 | sqlite3ErrorMsg(pParse, "incomplete SQL statement"); |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 40 | } |
| 41 | } |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 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" |
| 53 | #include "parse.h" |
drh | 9bbca4c | 2001-11-06 04:00:18 +0000 | [diff] [blame] | 54 | |
| 55 | /* |
drh | ad3cab5 | 2002-05-24 02:04:32 +0000 | [diff] [blame] | 56 | ** An instance of this structure holds information about the |
| 57 | ** LIMIT clause of a SELECT statement. |
drh | 9bbca4c | 2001-11-06 04:00:18 +0000 | [diff] [blame] | 58 | */ |
drh | ad3cab5 | 2002-05-24 02:04:32 +0000 | [diff] [blame] | 59 | struct LimitVal { |
danielk1977 | a2dc3b1 | 2005-02-05 12:48:48 +0000 | [diff] [blame] | 60 | Expr *pLimit; /* The LIMIT expression. NULL if there is no limit */ |
| 61 | Expr *pOffset; /* The OFFSET expression. NULL if there is none */ |
drh | ad3cab5 | 2002-05-24 02:04:32 +0000 | [diff] [blame] | 62 | }; |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 63 | |
| 64 | /* |
drh | 2e3a1f1 | 2004-10-06 14:39:28 +0000 | [diff] [blame] | 65 | ** An instance of this structure is used to store the LIKE, |
| 66 | ** GLOB, NOT LIKE, and NOT GLOB operators. |
| 67 | */ |
| 68 | struct LikeOp { |
drh | b71090f | 2005-05-23 17:26:51 +0000 | [diff] [blame] | 69 | Token operator; /* "like" or "glob" or "regexp" */ |
| 70 | int not; /* True if the NOT keyword is present */ |
drh | 2e3a1f1 | 2004-10-06 14:39:28 +0000 | [diff] [blame] | 71 | }; |
| 72 | |
| 73 | /* |
drh | ad3cab5 | 2002-05-24 02:04:32 +0000 | [diff] [blame] | 74 | ** An instance of the following structure describes the event of a |
| 75 | ** TRIGGER. "a" is the event type, one of TK_UPDATE, TK_INSERT, |
| 76 | ** TK_DELETE, or TK_INSTEAD. If the event is of the form |
| 77 | ** |
| 78 | ** UPDATE ON (a,b,c) |
| 79 | ** |
| 80 | ** Then the "b" IdList records the list "a,b,c". |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 81 | */ |
drh | ad3cab5 | 2002-05-24 02:04:32 +0000 | [diff] [blame] | 82 | struct TrigEvent { int a; IdList * b; }; |
drh | caec2f1 | 2003-01-07 02:47:47 +0000 | [diff] [blame] | 83 | |
drh | 25d6543 | 2004-07-22 15:02:25 +0000 | [diff] [blame] | 84 | /* |
| 85 | ** An instance of this structure holds the ATTACH key and the key type. |
| 86 | */ |
| 87 | struct AttachKey { int type; Token key; }; |
| 88 | |
drh | caec2f1 | 2003-01-07 02:47:47 +0000 | [diff] [blame] | 89 | } // end %include |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 90 | |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 91 | // These are extra tokens used by the lexer but never seen by the |
| 92 | // parser. We put them in a rule so that the parser generator will |
drh | 6206d50 | 2000-06-19 19:09:08 +0000 | [diff] [blame] | 93 | // add them to the parse.h output file. |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 94 | // |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 95 | %nonassoc END_OF_FILE ILLEGAL SPACE UNCLOSED_STRING COMMENT FUNCTION |
drh | 1344989 | 2005-09-07 21:22:45 +0000 | [diff] [blame] | 96 | COLUMN AGG_FUNCTION AGG_COLUMN CONST_FUNC. |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 97 | |
drh | 826fb5a | 2004-02-14 23:59:57 +0000 | [diff] [blame] | 98 | // Input is a single SQL command |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 99 | input ::= cmdlist. |
drh | 094b2bb | 2002-03-13 18:54:07 +0000 | [diff] [blame] | 100 | cmdlist ::= cmdlist ecmd. |
drh | 826fb5a | 2004-02-14 23:59:57 +0000 | [diff] [blame] | 101 | cmdlist ::= ecmd. |
drh | 8024205 | 2004-06-09 00:48:12 +0000 | [diff] [blame] | 102 | cmdx ::= cmd. { sqlite3FinishCoding(pParse); } |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 103 | ecmd ::= SEMI. |
| 104 | ecmd ::= explain cmdx SEMI. |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 105 | explain ::= . { sqlite3BeginParse(pParse, 0); } |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 106 | %ifndef SQLITE_OMIT_EXPLAIN |
drh | ecc9242 | 2005-09-10 16:46:12 +0000 | [diff] [blame] | 107 | explain ::= EXPLAIN. { sqlite3BeginParse(pParse, 1); } |
| 108 | explain ::= EXPLAIN QUERY PLAN. { sqlite3BeginParse(pParse, 2); } |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 109 | %endif |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 110 | |
drh | 382c024 | 2001-10-06 16:33:02 +0000 | [diff] [blame] | 111 | ///////////////////// Begin and end transactions. //////////////////////////// |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 112 | // |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 113 | |
drh | 684917c | 2004-10-05 02:41:42 +0000 | [diff] [blame] | 114 | cmd ::= BEGIN transtype(Y) trans_opt. {sqlite3BeginTransaction(pParse, Y);} |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 115 | trans_opt ::= . |
| 116 | trans_opt ::= TRANSACTION. |
drh | 5ad1a6c | 2002-07-01 12:27:09 +0000 | [diff] [blame] | 117 | trans_opt ::= TRANSACTION nm. |
drh | 684917c | 2004-10-05 02:41:42 +0000 | [diff] [blame] | 118 | %type transtype {int} |
| 119 | transtype(A) ::= . {A = TK_DEFERRED;} |
| 120 | transtype(A) ::= DEFERRED(X). {A = @X;} |
| 121 | transtype(A) ::= IMMEDIATE(X). {A = @X;} |
| 122 | transtype(A) ::= EXCLUSIVE(X). {A = @X;} |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 123 | cmd ::= COMMIT trans_opt. {sqlite3CommitTransaction(pParse);} |
| 124 | cmd ::= END trans_opt. {sqlite3CommitTransaction(pParse);} |
| 125 | cmd ::= ROLLBACK trans_opt. {sqlite3RollbackTransaction(pParse);} |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 126 | |
drh | 382c024 | 2001-10-06 16:33:02 +0000 | [diff] [blame] | 127 | ///////////////////// The CREATE TABLE statement //////////////////////////// |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 128 | // |
| 129 | cmd ::= create_table create_table_args. |
danielk1977 | cbb18d2 | 2004-05-28 11:37:27 +0000 | [diff] [blame] | 130 | create_table ::= CREATE(X) temp(T) TABLE nm(Y) dbnm(Z). { |
| 131 | sqlite3StartTable(pParse,&X,&Y,&Z,T,0); |
drh | 969fa7c | 2002-02-18 18:30:32 +0000 | [diff] [blame] | 132 | } |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 133 | %type temp {int} |
danielk1977 | 53c0f74 | 2005-03-29 03:10:59 +0000 | [diff] [blame] | 134 | %ifndef SQLITE_OMIT_TEMPDB |
drh | d24cc42 | 2003-03-27 12:51:24 +0000 | [diff] [blame] | 135 | temp(A) ::= TEMP. {A = 1;} |
danielk1977 | 53c0f74 | 2005-03-29 03:10:59 +0000 | [diff] [blame] | 136 | %endif |
drh | d24cc42 | 2003-03-27 12:51:24 +0000 | [diff] [blame] | 137 | temp(A) ::= . {A = 0;} |
danielk1977 | 19a8e7e | 2005-03-17 05:03:38 +0000 | [diff] [blame] | 138 | create_table_args ::= LP columnlist conslist_opt(X) RP(Y). { |
| 139 | sqlite3EndTable(pParse,&X,&Y,0); |
drh | 969fa7c | 2002-02-18 18:30:32 +0000 | [diff] [blame] | 140 | } |
| 141 | create_table_args ::= AS select(S). { |
danielk1977 | 19a8e7e | 2005-03-17 05:03:38 +0000 | [diff] [blame] | 142 | sqlite3EndTable(pParse,0,0,S); |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 143 | sqlite3SelectDelete(S); |
drh | 969fa7c | 2002-02-18 18:30:32 +0000 | [diff] [blame] | 144 | } |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 145 | columnlist ::= columnlist COMMA column. |
| 146 | columnlist ::= column. |
| 147 | |
drh | 487e262 | 2005-06-25 18:42:14 +0000 | [diff] [blame] | 148 | // A "column" is a complete description of a single column in a |
| 149 | // CREATE TABLE statement. This includes the column name, its |
| 150 | // datatype, and other keywords such as PRIMARY KEY, UNIQUE, REFERENCES, |
| 151 | // NOT NULL and so forth. |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 152 | // |
danielk1977 | 19a8e7e | 2005-03-17 05:03:38 +0000 | [diff] [blame] | 153 | column(A) ::= columnid(X) type carglist. { |
| 154 | A.z = X.z; |
| 155 | A.n = (pParse->sLastToken.z-X.z) + pParse->sLastToken.n; |
| 156 | } |
| 157 | columnid(A) ::= nm(X). { |
| 158 | sqlite3AddColumn(pParse,&X); |
| 159 | A = X; |
| 160 | } |
| 161 | |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 162 | |
| 163 | // An IDENTIFIER can be a generic identifier, or one of several |
| 164 | // keywords. Any non-standard keyword can also be an identifier. |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 165 | // |
drh | 982cef7 | 2000-05-30 16:27:03 +0000 | [diff] [blame] | 166 | %type id {Token} |
drh | f18543c | 2002-03-30 15:26:50 +0000 | [diff] [blame] | 167 | id(A) ::= ID(X). {A = X;} |
drh | 0bd1f4e | 2002-06-06 18:54:39 +0000 | [diff] [blame] | 168 | |
drh | 34e33bb | 2002-06-06 19:04:16 +0000 | [diff] [blame] | 169 | // The following directive causes tokens ABORT, AFTER, ASC, etc. to |
| 170 | // fallback to ID if they will not parse as their original value. |
| 171 | // This obviates the need for the "id" nonterminal. |
| 172 | // |
drh | 319e4e7 | 2003-09-30 01:54:13 +0000 | [diff] [blame] | 173 | %fallback ID |
drh | 9f18e8a | 2005-07-08 12:13:04 +0000 | [diff] [blame] | 174 | ABORT AFTER ANALYZE ASC ATTACH BEFORE BEGIN CASCADE CAST CONFLICT |
drh | 684917c | 2004-10-05 02:41:42 +0000 | [diff] [blame] | 175 | DATABASE DEFERRED DESC DETACH EACH END EXCLUSIVE EXPLAIN FAIL FOR |
drh | ecc9242 | 2005-09-10 16:46:12 +0000 | [diff] [blame] | 176 | IGNORE IMMEDIATE INITIALLY INSTEAD LIKE_KW MATCH PLAN QUERY KEY |
drh | 5ad1a6c | 2002-07-01 12:27:09 +0000 | [diff] [blame] | 177 | OF OFFSET PRAGMA RAISE REPLACE RESTRICT ROW STATEMENT |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 178 | TEMP TRIGGER VACUUM VIEW |
| 179 | %ifdef SQLITE_OMIT_COMPOUND_SELECT |
| 180 | EXCEPT INTERSECT UNION |
| 181 | %endif |
drh | b71090f | 2005-05-23 17:26:51 +0000 | [diff] [blame] | 182 | REINDEX RENAME CTIME_KW ALTER |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 183 | . |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 184 | |
drh | 2d3917d | 2004-02-22 16:27:00 +0000 | [diff] [blame] | 185 | // Define operator precedence early so that this is the first occurance |
| 186 | // of the operator tokens in the grammer. Keeping the operators together |
| 187 | // causes them to be assigned integer values that are close together, |
| 188 | // which keeps parser tables smaller. |
| 189 | // |
drh | f2bc013 | 2004-10-04 13:19:23 +0000 | [diff] [blame] | 190 | // The token values assigned to these symbols is determined by the order |
| 191 | // in which lemon first sees them. It must be the case that ISNULL/NOTNULL, |
| 192 | // NE/EQ, GT/LE, and GE/LT are separated by only a single value. See |
| 193 | // the sqlite3ExprIfFalse() routine for additional information on this |
| 194 | // constraint. |
| 195 | // |
drh | 2d3917d | 2004-02-22 16:27:00 +0000 | [diff] [blame] | 196 | %left OR. |
| 197 | %left AND. |
| 198 | %right NOT. |
drh | b71090f | 2005-05-23 17:26:51 +0000 | [diff] [blame] | 199 | %left IS LIKE_KW BETWEEN IN ISNULL NOTNULL NE EQ. |
drh | 9a43267 | 2004-10-04 13:38:09 +0000 | [diff] [blame] | 200 | %left GT LE LT GE. |
danielk1977 | 7c6303c | 2004-11-17 16:41:29 +0000 | [diff] [blame] | 201 | %right ESCAPE. |
drh | 2d3917d | 2004-02-22 16:27:00 +0000 | [diff] [blame] | 202 | %left BITAND BITOR LSHIFT RSHIFT. |
| 203 | %left PLUS MINUS. |
| 204 | %left STAR SLASH REM. |
| 205 | %left CONCAT. |
| 206 | %right UMINUS UPLUS BITNOT. |
| 207 | |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 208 | // And "ids" is an identifer-or-string. |
| 209 | // |
| 210 | %type ids {Token} |
drh | 5ad1a6c | 2002-07-01 12:27:09 +0000 | [diff] [blame] | 211 | ids(A) ::= ID(X). {A = X;} |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 212 | ids(A) ::= STRING(X). {A = X;} |
| 213 | |
drh | 5ad1a6c | 2002-07-01 12:27:09 +0000 | [diff] [blame] | 214 | // The name of a column or table can be any of the following: |
| 215 | // |
| 216 | %type nm {Token} |
| 217 | nm(A) ::= ID(X). {A = X;} |
| 218 | nm(A) ::= STRING(X). {A = X;} |
| 219 | nm(A) ::= JOIN_KW(X). {A = X;} |
| 220 | |
drh | 487e262 | 2005-06-25 18:42:14 +0000 | [diff] [blame] | 221 | // A typetoken is really one or more tokens that form a type name such |
| 222 | // as can be found after the column name in a CREATE TABLE statement. |
| 223 | // Multiple tokens are concatenated to form the value of the typetoken. |
| 224 | // |
| 225 | %type typetoken {Token} |
drh | 382c024 | 2001-10-06 16:33:02 +0000 | [diff] [blame] | 226 | type ::= . |
drh | 487e262 | 2005-06-25 18:42:14 +0000 | [diff] [blame] | 227 | type ::= typetoken(X). {sqlite3AddColumnType(pParse,&X);} |
| 228 | typetoken(A) ::= typename(X). {A = X;} |
| 229 | typetoken(A) ::= typename(X) LP signed RP(Y). { |
| 230 | A.z = X.z; |
| 231 | A.n = &Y.z[Y.n] - X.z; |
| 232 | } |
| 233 | typetoken(A) ::= typename(X) LP signed COMMA signed RP(Y). { |
| 234 | A.z = X.z; |
| 235 | A.n = &Y.z[Y.n] - X.z; |
| 236 | } |
drh | 382c024 | 2001-10-06 16:33:02 +0000 | [diff] [blame] | 237 | %type typename {Token} |
drh | e2ea40d | 2004-05-20 12:41:19 +0000 | [diff] [blame] | 238 | typename(A) ::= ids(X). {A = X;} |
drh | 596bd23 | 2004-09-30 14:22:47 +0000 | [diff] [blame] | 239 | typename(A) ::= typename(X) ids(Y). {A.z=X.z; A.n=Y.n+(Y.z-X.z);} |
drh | ef0cae5 | 2003-07-16 02:19:37 +0000 | [diff] [blame] | 240 | %type signed {int} |
drh | 74ad7fe | 2004-10-07 03:06:28 +0000 | [diff] [blame] | 241 | signed(A) ::= plus_num(X). { A = atoi(X.z); } |
| 242 | signed(A) ::= minus_num(X). { A = -atoi(X.z); } |
drh | 487e262 | 2005-06-25 18:42:14 +0000 | [diff] [blame] | 243 | |
| 244 | // "carglist" is a list of additional constraints that come after the |
| 245 | // column name and column type in a CREATE TABLE statement. |
| 246 | // |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 247 | carglist ::= carglist carg. |
| 248 | carglist ::= . |
drh | 5ad1a6c | 2002-07-01 12:27:09 +0000 | [diff] [blame] | 249 | carg ::= CONSTRAINT nm ccons. |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 250 | carg ::= ccons. |
danielk1977 | 7977a17 | 2004-11-09 12:44:37 +0000 | [diff] [blame] | 251 | carg ::= DEFAULT term(X). {sqlite3AddDefaultValue(pParse,X);} |
drh | eb55bd2 | 2005-06-30 17:04:21 +0000 | [diff] [blame] | 252 | carg ::= DEFAULT LP expr(X) RP. {sqlite3AddDefaultValue(pParse,X);} |
danielk1977 | 7977a17 | 2004-11-09 12:44:37 +0000 | [diff] [blame] | 253 | carg ::= DEFAULT PLUS term(X). {sqlite3AddDefaultValue(pParse,X);} |
| 254 | carg ::= DEFAULT MINUS term(X). { |
| 255 | Expr *p = sqlite3Expr(TK_UMINUS, X, 0, 0); |
| 256 | sqlite3AddDefaultValue(pParse,p); |
| 257 | } |
| 258 | carg ::= DEFAULT id(X). { |
| 259 | Expr *p = sqlite3Expr(TK_STRING, 0, 0, &X); |
| 260 | sqlite3AddDefaultValue(pParse,p); |
| 261 | } |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 262 | |
drh | 382c024 | 2001-10-06 16:33:02 +0000 | [diff] [blame] | 263 | // In addition to the type name, we also care about the primary key and |
| 264 | // UNIQUE constraints. |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 265 | // |
drh | 0d316a4 | 2002-08-11 20:10:47 +0000 | [diff] [blame] | 266 | ccons ::= NULL onconf. |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 267 | ccons ::= NOT NULL onconf(R). {sqlite3AddNotNull(pParse, R);} |
drh | 205f48e | 2004-11-05 00:43:11 +0000 | [diff] [blame] | 268 | ccons ::= PRIMARY KEY sortorder onconf(R) autoinc(I). |
| 269 | {sqlite3AddPrimaryKey(pParse,0,R,I);} |
drh | 74ad7fe | 2004-10-07 03:06:28 +0000 | [diff] [blame] | 270 | ccons ::= UNIQUE onconf(R). {sqlite3CreateIndex(pParse,0,0,0,0,R,0,0);} |
drh | 06f6541 | 2005-11-03 02:03:13 +0000 | [diff] [blame^] | 271 | ccons ::= CHECK LP expr(X) RP. {sqlite3AddCheckConstraint(pParse,X);} |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 272 | ccons ::= REFERENCES nm(T) idxlist_opt(TA) refargs(R). |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 273 | {sqlite3CreateForeignKey(pParse,0,&T,TA,R);} |
| 274 | ccons ::= defer_subclause(D). {sqlite3DeferForeignKey(pParse,D);} |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 275 | ccons ::= COLLATE id(C). {sqlite3AddCollateType(pParse, C.z, C.n);} |
drh | 04738cb | 2002-06-02 18:19:00 +0000 | [diff] [blame] | 276 | |
drh | 205f48e | 2004-11-05 00:43:11 +0000 | [diff] [blame] | 277 | // The optional AUTOINCREMENT keyword |
| 278 | %type autoinc {int} |
drh | 2958a4e | 2004-11-12 03:56:15 +0000 | [diff] [blame] | 279 | autoinc(X) ::= . {X = 0;} |
| 280 | autoinc(X) ::= AUTOINCR. {X = 1;} |
drh | 205f48e | 2004-11-05 00:43:11 +0000 | [diff] [blame] | 281 | |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 282 | // The next group of rules parses the arguments to a REFERENCES clause |
| 283 | // that determine if the referential integrity checking is deferred or |
| 284 | // or immediate and which determine what action to take if a ref-integ |
| 285 | // check fails. |
drh | 04738cb | 2002-06-02 18:19:00 +0000 | [diff] [blame] | 286 | // |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 287 | %type refargs {int} |
| 288 | refargs(A) ::= . { A = OE_Restrict * 0x010101; } |
| 289 | refargs(A) ::= refargs(X) refarg(Y). { A = (X & Y.mask) | Y.value; } |
| 290 | %type refarg {struct {int value; int mask;}} |
| 291 | refarg(A) ::= MATCH nm. { A.value = 0; A.mask = 0x000000; } |
| 292 | refarg(A) ::= ON DELETE refact(X). { A.value = X; A.mask = 0x0000ff; } |
| 293 | refarg(A) ::= ON UPDATE refact(X). { A.value = X<<8; A.mask = 0x00ff00; } |
| 294 | refarg(A) ::= ON INSERT refact(X). { A.value = X<<16; A.mask = 0xff0000; } |
| 295 | %type refact {int} |
| 296 | refact(A) ::= SET NULL. { A = OE_SetNull; } |
| 297 | refact(A) ::= SET DEFAULT. { A = OE_SetDflt; } |
| 298 | refact(A) ::= CASCADE. { A = OE_Cascade; } |
| 299 | refact(A) ::= RESTRICT. { A = OE_Restrict; } |
| 300 | %type defer_subclause {int} |
| 301 | defer_subclause(A) ::= NOT DEFERRABLE init_deferred_pred_opt(X). {A = X;} |
| 302 | defer_subclause(A) ::= DEFERRABLE init_deferred_pred_opt(X). {A = X;} |
| 303 | %type init_deferred_pred_opt {int} |
| 304 | init_deferred_pred_opt(A) ::= . {A = 0;} |
| 305 | init_deferred_pred_opt(A) ::= INITIALLY DEFERRED. {A = 1;} |
| 306 | init_deferred_pred_opt(A) ::= INITIALLY IMMEDIATE. {A = 0;} |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 307 | |
| 308 | // For the time being, the only constraint we care about is the primary |
drh | 382c024 | 2001-10-06 16:33:02 +0000 | [diff] [blame] | 309 | // key and UNIQUE. Both create indices. |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 310 | // |
danielk1977 | 19a8e7e | 2005-03-17 05:03:38 +0000 | [diff] [blame] | 311 | conslist_opt(A) ::= . {A.n = 0; A.z = 0;} |
| 312 | conslist_opt(A) ::= COMMA(X) conslist. {A = X;} |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 313 | conslist ::= conslist COMMA tcons. |
drh | a2e1bb5 | 2001-01-04 14:20:18 +0000 | [diff] [blame] | 314 | conslist ::= conslist tcons. |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 315 | conslist ::= tcons. |
drh | 5ad1a6c | 2002-07-01 12:27:09 +0000 | [diff] [blame] | 316 | tcons ::= CONSTRAINT nm. |
drh | f338814 | 2004-11-13 03:48:06 +0000 | [diff] [blame] | 317 | tcons ::= PRIMARY KEY LP idxlist(X) autoinc(I) RP onconf(R). |
drh | 205f48e | 2004-11-05 00:43:11 +0000 | [diff] [blame] | 318 | {sqlite3AddPrimaryKey(pParse,X,R,I);} |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 319 | tcons ::= UNIQUE LP idxlist(X) RP onconf(R). |
danielk1977 | cbb18d2 | 2004-05-28 11:37:27 +0000 | [diff] [blame] | 320 | {sqlite3CreateIndex(pParse,0,0,0,X,R,0,0);} |
drh | 06f6541 | 2005-11-03 02:03:13 +0000 | [diff] [blame^] | 321 | tcons ::= CHECK LP expr(E) RP onconf. {sqlite3AddCheckConstraint(pParse,E);} |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 322 | tcons ::= FOREIGN KEY LP idxlist(FA) RP |
| 323 | REFERENCES nm(T) idxlist_opt(TA) refargs(R) defer_subclause_opt(D). { |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 324 | sqlite3CreateForeignKey(pParse, FA, &T, TA, R); |
| 325 | sqlite3DeferForeignKey(pParse, D); |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 326 | } |
| 327 | %type defer_subclause_opt {int} |
| 328 | defer_subclause_opt(A) ::= . {A = 0;} |
| 329 | defer_subclause_opt(A) ::= defer_subclause(X). {A = X;} |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 330 | |
| 331 | // The following is a non-standard extension that allows us to declare the |
| 332 | // default behavior when there is a constraint conflict. |
| 333 | // |
| 334 | %type onconf {int} |
drh | 1c92853 | 2002-01-31 15:54:21 +0000 | [diff] [blame] | 335 | %type orconf {int} |
| 336 | %type resolvetype {int} |
drh | 74ad7fe | 2004-10-07 03:06:28 +0000 | [diff] [blame] | 337 | onconf(A) ::= . {A = OE_Default;} |
| 338 | onconf(A) ::= ON CONFLICT resolvetype(X). {A = X;} |
| 339 | orconf(A) ::= . {A = OE_Default;} |
| 340 | orconf(A) ::= OR resolvetype(X). {A = X;} |
| 341 | resolvetype(A) ::= raisetype(X). {A = X;} |
| 342 | resolvetype(A) ::= IGNORE. {A = OE_Ignore;} |
| 343 | resolvetype(A) ::= REPLACE. {A = OE_Replace;} |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 344 | |
drh | 382c024 | 2001-10-06 16:33:02 +0000 | [diff] [blame] | 345 | ////////////////////////// The DROP TABLE ///////////////////////////////////// |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 346 | // |
drh | 74ad7fe | 2004-10-07 03:06:28 +0000 | [diff] [blame] | 347 | cmd ::= DROP TABLE fullname(X). { |
| 348 | sqlite3DropTable(pParse, X, 0); |
danielk1977 | a885810 | 2004-05-28 12:11:21 +0000 | [diff] [blame] | 349 | } |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 350 | |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 351 | ///////////////////// The CREATE VIEW statement ///////////////////////////// |
| 352 | // |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 353 | %ifndef SQLITE_OMIT_VIEW |
danielk1977 | 48dec7e | 2004-05-28 12:33:30 +0000 | [diff] [blame] | 354 | cmd ::= CREATE(X) temp(T) VIEW nm(Y) dbnm(Z) AS select(S). { |
| 355 | sqlite3CreateView(pParse, &X, &Y, &Z, S, T); |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 356 | } |
drh | 74ad7fe | 2004-10-07 03:06:28 +0000 | [diff] [blame] | 357 | cmd ::= DROP VIEW fullname(X). { |
| 358 | sqlite3DropTable(pParse, X, 1); |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 359 | } |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 360 | %endif // SQLITE_OMIT_VIEW |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 361 | |
drh | 382c024 | 2001-10-06 16:33:02 +0000 | [diff] [blame] | 362 | //////////////////////// The SELECT statement ///////////////////////////////// |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 363 | // |
drh | 9bb61fe | 2000-06-05 16:01:39 +0000 | [diff] [blame] | 364 | cmd ::= select(X). { |
danielk1977 | b3bce66 | 2005-01-29 08:32:43 +0000 | [diff] [blame] | 365 | sqlite3Select(pParse, X, SRT_Callback, 0, 0, 0, 0, 0); |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 366 | sqlite3SelectDelete(X); |
drh | 9bb61fe | 2000-06-05 16:01:39 +0000 | [diff] [blame] | 367 | } |
drh | efb7251 | 2000-05-31 20:00:52 +0000 | [diff] [blame] | 368 | |
drh | 9bb61fe | 2000-06-05 16:01:39 +0000 | [diff] [blame] | 369 | %type select {Select*} |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 370 | %destructor select {sqlite3SelectDelete($$);} |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 371 | %type oneselect {Select*} |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 372 | %destructor oneselect {sqlite3SelectDelete($$);} |
drh | 9bb61fe | 2000-06-05 16:01:39 +0000 | [diff] [blame] | 373 | |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 374 | select(A) ::= oneselect(X). {A = X;} |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 375 | %ifndef SQLITE_OMIT_COMPOUND_SELECT |
drh | 0a36c57 | 2002-02-18 22:49:59 +0000 | [diff] [blame] | 376 | select(A) ::= select(X) multiselect_op(Y) oneselect(Z). { |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 377 | if( Z ){ |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 378 | Z->op = Y; |
| 379 | Z->pPrior = X; |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 380 | } |
| 381 | A = Z; |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 382 | } |
drh | 0a36c57 | 2002-02-18 22:49:59 +0000 | [diff] [blame] | 383 | %type multiselect_op {int} |
drh | 74ad7fe | 2004-10-07 03:06:28 +0000 | [diff] [blame] | 384 | multiselect_op(A) ::= UNION(OP). {A = @OP;} |
| 385 | multiselect_op(A) ::= UNION ALL. {A = TK_ALL;} |
| 386 | multiselect_op(A) ::= INTERSECT(OP). {A = @OP;} |
| 387 | multiselect_op(A) ::= EXCEPT(OP). {A = @OP;} |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 388 | %endif // SQLITE_OMIT_COMPOUND_SELECT |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 389 | oneselect(A) ::= SELECT distinct(D) selcollist(W) from(X) where_opt(Y) |
drh | 9bbca4c | 2001-11-06 04:00:18 +0000 | [diff] [blame] | 390 | groupby_opt(P) having_opt(Q) orderby_opt(Z) limit_opt(L). { |
danielk1977 | a2dc3b1 | 2005-02-05 12:48:48 +0000 | [diff] [blame] | 391 | A = sqlite3SelectNew(W,X,Y,P,Q,Z,D,L.pLimit,L.pOffset); |
drh | 9bb61fe | 2000-06-05 16:01:39 +0000 | [diff] [blame] | 392 | } |
| 393 | |
| 394 | // The "distinct" nonterminal is true (1) if the DISTINCT keyword is |
| 395 | // present and false (0) if it is not. |
| 396 | // |
drh | efb7251 | 2000-05-31 20:00:52 +0000 | [diff] [blame] | 397 | %type distinct {int} |
drh | efb7251 | 2000-05-31 20:00:52 +0000 | [diff] [blame] | 398 | distinct(A) ::= DISTINCT. {A = 1;} |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 399 | distinct(A) ::= ALL. {A = 0;} |
drh | efb7251 | 2000-05-31 20:00:52 +0000 | [diff] [blame] | 400 | distinct(A) ::= . {A = 0;} |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 401 | |
drh | 9bb61fe | 2000-06-05 16:01:39 +0000 | [diff] [blame] | 402 | // selcollist is a list of expressions that are to become the return |
drh | 7c917d1 | 2001-12-16 20:05:05 +0000 | [diff] [blame] | 403 | // values of the SELECT statement. The "*" in statements like |
| 404 | // "SELECT * FROM ..." is encoded as a special expression with an |
| 405 | // opcode of TK_ALL. |
drh | 9bb61fe | 2000-06-05 16:01:39 +0000 | [diff] [blame] | 406 | // |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 407 | %type selcollist {ExprList*} |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 408 | %destructor selcollist {sqlite3ExprListDelete($$);} |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 409 | %type sclp {ExprList*} |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 410 | %destructor sclp {sqlite3ExprListDelete($$);} |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 411 | sclp(A) ::= selcollist(X) COMMA. {A = X;} |
| 412 | sclp(A) ::= . {A = 0;} |
drh | 01f3f25 | 2002-05-24 16:14:15 +0000 | [diff] [blame] | 413 | selcollist(A) ::= sclp(P) expr(X) as(Y). { |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 414 | A = sqlite3ExprListAppend(P,X,Y.n?&Y:0); |
drh | 01f3f25 | 2002-05-24 16:14:15 +0000 | [diff] [blame] | 415 | } |
drh | 7c917d1 | 2001-12-16 20:05:05 +0000 | [diff] [blame] | 416 | selcollist(A) ::= sclp(P) STAR. { |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 417 | A = sqlite3ExprListAppend(P, sqlite3Expr(TK_ALL, 0, 0, 0), 0); |
drh | 7c917d1 | 2001-12-16 20:05:05 +0000 | [diff] [blame] | 418 | } |
drh | 5ad1a6c | 2002-07-01 12:27:09 +0000 | [diff] [blame] | 419 | selcollist(A) ::= sclp(P) nm(X) DOT STAR. { |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 420 | Expr *pRight = sqlite3Expr(TK_ALL, 0, 0, 0); |
| 421 | Expr *pLeft = sqlite3Expr(TK_ID, 0, 0, &X); |
| 422 | A = sqlite3ExprListAppend(P, sqlite3Expr(TK_DOT, pLeft, pRight, 0), 0); |
drh | 5447322 | 2002-04-04 02:10:55 +0000 | [diff] [blame] | 423 | } |
drh | 01f3f25 | 2002-05-24 16:14:15 +0000 | [diff] [blame] | 424 | |
| 425 | // An option "AS <id>" phrase that can follow one of the expressions that |
| 426 | // define the result set, or one of the tables in the FROM clause. |
| 427 | // |
| 428 | %type as {Token} |
drh | 74ad7fe | 2004-10-07 03:06:28 +0000 | [diff] [blame] | 429 | as(X) ::= AS nm(Y). {X = Y;} |
| 430 | as(X) ::= ids(Y). {X = Y;} |
| 431 | as(X) ::= . {X.n = 0;} |
drh | 9bb61fe | 2000-06-05 16:01:39 +0000 | [diff] [blame] | 432 | |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 433 | |
drh | ad3cab5 | 2002-05-24 02:04:32 +0000 | [diff] [blame] | 434 | %type seltablist {SrcList*} |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 435 | %destructor seltablist {sqlite3SrcListDelete($$);} |
drh | ad3cab5 | 2002-05-24 02:04:32 +0000 | [diff] [blame] | 436 | %type stl_prefix {SrcList*} |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 437 | %destructor stl_prefix {sqlite3SrcListDelete($$);} |
drh | ad3cab5 | 2002-05-24 02:04:32 +0000 | [diff] [blame] | 438 | %type from {SrcList*} |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 439 | %destructor from {sqlite3SrcListDelete($$);} |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 440 | |
drh | 01f3f25 | 2002-05-24 16:14:15 +0000 | [diff] [blame] | 441 | // A complete FROM clause. |
| 442 | // |
drh | bf3a4fa | 2002-04-06 13:57:42 +0000 | [diff] [blame] | 443 | from(A) ::= . {A = sqliteMalloc(sizeof(*A));} |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 444 | from(A) ::= FROM seltablist(X). {A = X;} |
drh | 01f3f25 | 2002-05-24 16:14:15 +0000 | [diff] [blame] | 445 | |
| 446 | // "seltablist" is a "Select Table List" - the content of the FROM clause |
| 447 | // in a SELECT statement. "stl_prefix" is a prefix of this list. |
| 448 | // |
| 449 | stl_prefix(A) ::= seltablist(X) joinop(Y). { |
| 450 | A = X; |
| 451 | if( A && A->nSrc>0 ) A->a[A->nSrc-1].jointype = Y; |
| 452 | } |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 453 | stl_prefix(A) ::= . {A = 0;} |
drh | 113088e | 2003-03-20 01:16:58 +0000 | [diff] [blame] | 454 | seltablist(A) ::= stl_prefix(X) nm(Y) dbnm(D) as(Z) on_opt(N) using_opt(U). { |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 455 | A = sqlite3SrcListAppend(X,&Y,&D); |
| 456 | if( Z.n ) sqlite3SrcListAddAlias(A,&Z); |
drh | 01f3f25 | 2002-05-24 16:14:15 +0000 | [diff] [blame] | 457 | if( N ){ |
| 458 | if( A && A->nSrc>1 ){ A->a[A->nSrc-2].pOn = N; } |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 459 | else { sqlite3ExprDelete(N); } |
drh | 01f3f25 | 2002-05-24 16:14:15 +0000 | [diff] [blame] | 460 | } |
| 461 | if( U ){ |
| 462 | if( A && A->nSrc>1 ){ A->a[A->nSrc-2].pUsing = U; } |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 463 | else { sqlite3IdListDelete(U); } |
drh | 01f3f25 | 2002-05-24 16:14:15 +0000 | [diff] [blame] | 464 | } |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 465 | } |
drh | 51522cd | 2005-01-20 13:36:19 +0000 | [diff] [blame] | 466 | %ifndef SQLITE_OMIT_SUBQUERY |
| 467 | seltablist(A) ::= stl_prefix(X) LP seltablist_paren(S) RP |
| 468 | as(Z) on_opt(N) using_opt(U). { |
| 469 | A = sqlite3SrcListAppend(X,0,0); |
| 470 | A->a[A->nSrc-1].pSelect = S; |
| 471 | if( Z.n ) sqlite3SrcListAddAlias(A,&Z); |
| 472 | if( N ){ |
| 473 | if( A && A->nSrc>1 ){ A->a[A->nSrc-2].pOn = N; } |
| 474 | else { sqlite3ExprDelete(N); } |
| 475 | } |
| 476 | if( U ){ |
| 477 | if( A && A->nSrc>1 ){ A->a[A->nSrc-2].pUsing = U; } |
| 478 | else { sqlite3IdListDelete(U); } |
| 479 | } |
drh | d5feede | 2002-05-08 21:46:14 +0000 | [diff] [blame] | 480 | } |
drh | 51522cd | 2005-01-20 13:36:19 +0000 | [diff] [blame] | 481 | |
| 482 | // A seltablist_paren nonterminal represents anything in a FROM that |
| 483 | // is contained inside parentheses. This can be either a subquery or |
| 484 | // a grouping of table and subqueries. |
| 485 | // |
| 486 | %type seltablist_paren {Select*} |
| 487 | %destructor seltablist_paren {sqlite3SelectDelete($$);} |
| 488 | seltablist_paren(A) ::= select(S). {A = S;} |
| 489 | seltablist_paren(A) ::= seltablist(F). { |
danielk1977 | a2dc3b1 | 2005-02-05 12:48:48 +0000 | [diff] [blame] | 490 | A = sqlite3SelectNew(0,F,0,0,0,0,0,0,0); |
drh | 01f3f25 | 2002-05-24 16:14:15 +0000 | [diff] [blame] | 491 | } |
drh | 51522cd | 2005-01-20 13:36:19 +0000 | [diff] [blame] | 492 | %endif // SQLITE_OMIT_SUBQUERY |
drh | b733d03 | 2004-01-24 20:18:12 +0000 | [diff] [blame] | 493 | |
drh | 113088e | 2003-03-20 01:16:58 +0000 | [diff] [blame] | 494 | %type dbnm {Token} |
| 495 | dbnm(A) ::= . {A.z=0; A.n=0;} |
| 496 | dbnm(A) ::= DOT nm(X). {A = X;} |
| 497 | |
drh | 74ad7fe | 2004-10-07 03:06:28 +0000 | [diff] [blame] | 498 | %type fullname {SrcList*} |
| 499 | %destructor fullname {sqlite3SrcListDelete($$);} |
| 500 | fullname(A) ::= nm(X) dbnm(Y). {A = sqlite3SrcListAppend(0,&X,&Y);} |
| 501 | |
drh | 01f3f25 | 2002-05-24 16:14:15 +0000 | [diff] [blame] | 502 | %type joinop {int} |
| 503 | %type joinop2 {int} |
| 504 | joinop(X) ::= COMMA. { X = JT_INNER; } |
| 505 | joinop(X) ::= JOIN. { X = JT_INNER; } |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 506 | joinop(X) ::= JOIN_KW(A) JOIN. { X = sqlite3JoinType(pParse,&A,0,0); } |
| 507 | joinop(X) ::= JOIN_KW(A) nm(B) JOIN. { X = sqlite3JoinType(pParse,&A,&B,0); } |
drh | 5ad1a6c | 2002-07-01 12:27:09 +0000 | [diff] [blame] | 508 | joinop(X) ::= JOIN_KW(A) nm(B) nm(C) JOIN. |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 509 | { X = sqlite3JoinType(pParse,&A,&B,&C); } |
drh | 01f3f25 | 2002-05-24 16:14:15 +0000 | [diff] [blame] | 510 | |
| 511 | %type on_opt {Expr*} |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 512 | %destructor on_opt {sqlite3ExprDelete($$);} |
drh | 01f3f25 | 2002-05-24 16:14:15 +0000 | [diff] [blame] | 513 | on_opt(N) ::= ON expr(E). {N = E;} |
| 514 | on_opt(N) ::= . {N = 0;} |
| 515 | |
| 516 | %type using_opt {IdList*} |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 517 | %destructor using_opt {sqlite3IdListDelete($$);} |
danielk1977 | 0202b29 | 2004-06-09 09:55:16 +0000 | [diff] [blame] | 518 | using_opt(U) ::= USING LP inscollist(L) RP. {U = L;} |
drh | 01f3f25 | 2002-05-24 16:14:15 +0000 | [diff] [blame] | 519 | using_opt(U) ::= . {U = 0;} |
| 520 | |
| 521 | |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 522 | %type orderby_opt {ExprList*} |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 523 | %destructor orderby_opt {sqlite3ExprListDelete($$);} |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 524 | %type sortlist {ExprList*} |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 525 | %destructor sortlist {sqlite3ExprListDelete($$);} |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 526 | %type sortitem {Expr*} |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 527 | %destructor sortitem {sqlite3ExprDelete($$);} |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 528 | |
| 529 | orderby_opt(A) ::= . {A = 0;} |
| 530 | orderby_opt(A) ::= ORDER BY sortlist(X). {A = X;} |
drh | 8e2ca02 | 2002-06-17 17:07:19 +0000 | [diff] [blame] | 531 | sortlist(A) ::= sortlist(X) COMMA sortitem(Y) collate(C) sortorder(Z). { |
drh | 1186b0a | 2004-05-20 23:37:54 +0000 | [diff] [blame] | 532 | A = sqlite3ExprListAppend(X,Y,C.n>0?&C:0); |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 533 | if( A ) A->a[A->nExpr-1].sortOrder = Z; |
drh | 9bb61fe | 2000-06-05 16:01:39 +0000 | [diff] [blame] | 534 | } |
drh | 38640e1 | 2002-07-05 21:42:36 +0000 | [diff] [blame] | 535 | sortlist(A) ::= sortitem(Y) collate(C) sortorder(Z). { |
drh | 1186b0a | 2004-05-20 23:37:54 +0000 | [diff] [blame] | 536 | A = sqlite3ExprListAppend(0,Y,C.n>0?&C:0); |
danielk1977 | 96fb0dd | 2004-06-30 09:49:22 +0000 | [diff] [blame] | 537 | if( A && A->a ) A->a[0].sortOrder = Z; |
drh | 9bb61fe | 2000-06-05 16:01:39 +0000 | [diff] [blame] | 538 | } |
drh | da9d6c4 | 2000-05-31 18:20:14 +0000 | [diff] [blame] | 539 | sortitem(A) ::= expr(X). {A = X;} |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 540 | |
| 541 | %type sortorder {int} |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 542 | %type collate {Token} |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 543 | |
drh | 8e2ca02 | 2002-06-17 17:07:19 +0000 | [diff] [blame] | 544 | sortorder(A) ::= ASC. {A = SQLITE_SO_ASC;} |
| 545 | sortorder(A) ::= DESC. {A = SQLITE_SO_DESC;} |
| 546 | sortorder(A) ::= . {A = SQLITE_SO_ASC;} |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 547 | collate(C) ::= . {C.z = 0; C.n = 0;} |
| 548 | collate(C) ::= COLLATE id(X). {C = X;} |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 549 | |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 550 | %type groupby_opt {ExprList*} |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 551 | %destructor groupby_opt {sqlite3ExprListDelete($$);} |
drh | 6206d50 | 2000-06-19 19:09:08 +0000 | [diff] [blame] | 552 | groupby_opt(A) ::= . {A = 0;} |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 553 | groupby_opt(A) ::= GROUP BY exprlist(X). {A = X;} |
| 554 | |
| 555 | %type having_opt {Expr*} |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 556 | %destructor having_opt {sqlite3ExprDelete($$);} |
drh | 6206d50 | 2000-06-19 19:09:08 +0000 | [diff] [blame] | 557 | having_opt(A) ::= . {A = 0;} |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 558 | having_opt(A) ::= HAVING expr(X). {A = X;} |
| 559 | |
drh | ad3cab5 | 2002-05-24 02:04:32 +0000 | [diff] [blame] | 560 | %type limit_opt {struct LimitVal} |
danielk1977 | a2dc3b1 | 2005-02-05 12:48:48 +0000 | [diff] [blame] | 561 | %destructor limit_opt { |
| 562 | sqlite3ExprDelete($$.pLimit); |
| 563 | sqlite3ExprDelete($$.pOffset); |
| 564 | } |
| 565 | limit_opt(A) ::= . {A.pLimit = 0; A.pOffset = 0;} |
| 566 | limit_opt(A) ::= LIMIT expr(X). {A.pLimit = X; A.pOffset = 0;} |
| 567 | limit_opt(A) ::= LIMIT expr(X) OFFSET expr(Y). |
| 568 | {A.pLimit = X; A.pOffset = Y;} |
| 569 | limit_opt(A) ::= LIMIT expr(X) COMMA expr(Y). |
| 570 | {A.pOffset = X; A.pLimit = Y;} |
drh | 9bbca4c | 2001-11-06 04:00:18 +0000 | [diff] [blame] | 571 | |
drh | 382c024 | 2001-10-06 16:33:02 +0000 | [diff] [blame] | 572 | /////////////////////////// The DELETE statement ///////////////////////////// |
| 573 | // |
drh | 74ad7fe | 2004-10-07 03:06:28 +0000 | [diff] [blame] | 574 | cmd ::= DELETE FROM fullname(X) where_opt(Y). {sqlite3DeleteFrom(pParse,X,Y);} |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 575 | |
| 576 | %type where_opt {Expr*} |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 577 | %destructor where_opt {sqlite3ExprDelete($$);} |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 578 | |
| 579 | where_opt(A) ::= . {A = 0;} |
| 580 | where_opt(A) ::= WHERE expr(X). {A = X;} |
| 581 | |
drh | 382c024 | 2001-10-06 16:33:02 +0000 | [diff] [blame] | 582 | ////////////////////////// The UPDATE command //////////////////////////////// |
| 583 | // |
drh | 74ad7fe | 2004-10-07 03:06:28 +0000 | [diff] [blame] | 584 | cmd ::= UPDATE orconf(R) fullname(X) SET setlist(Y) where_opt(Z). |
| 585 | {sqlite3Update(pParse,X,Y,Z,R);} |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 586 | |
drh | f8db1bc | 2005-04-22 02:38:37 +0000 | [diff] [blame] | 587 | %type setlist {ExprList*} |
| 588 | %destructor setlist {sqlite3ExprListDelete($$);} |
| 589 | |
drh | 5ad1a6c | 2002-07-01 12:27:09 +0000 | [diff] [blame] | 590 | setlist(A) ::= setlist(Z) COMMA nm(X) EQ expr(Y). |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 591 | {A = sqlite3ExprListAppend(Z,Y,&X);} |
| 592 | setlist(A) ::= nm(X) EQ expr(Y). {A = sqlite3ExprListAppend(0,Y,&X);} |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 593 | |
drh | 382c024 | 2001-10-06 16:33:02 +0000 | [diff] [blame] | 594 | ////////////////////////// The INSERT command ///////////////////////////////// |
| 595 | // |
drh | 74ad7fe | 2004-10-07 03:06:28 +0000 | [diff] [blame] | 596 | cmd ::= insert_cmd(R) INTO fullname(X) inscollist_opt(F) |
drh | 113088e | 2003-03-20 01:16:58 +0000 | [diff] [blame] | 597 | VALUES LP itemlist(Y) RP. |
drh | 74ad7fe | 2004-10-07 03:06:28 +0000 | [diff] [blame] | 598 | {sqlite3Insert(pParse, X, Y, 0, F, R);} |
| 599 | cmd ::= insert_cmd(R) INTO fullname(X) inscollist_opt(F) select(S). |
| 600 | {sqlite3Insert(pParse, X, 0, S, F, R);} |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 601 | |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 602 | %type insert_cmd {int} |
| 603 | insert_cmd(A) ::= INSERT orconf(R). {A = R;} |
| 604 | insert_cmd(A) ::= REPLACE. {A = OE_Replace;} |
| 605 | |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 606 | |
| 607 | %type itemlist {ExprList*} |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 608 | %destructor itemlist {sqlite3ExprListDelete($$);} |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 609 | |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 610 | itemlist(A) ::= itemlist(X) COMMA expr(Y). {A = sqlite3ExprListAppend(X,Y,0);} |
| 611 | itemlist(A) ::= expr(X). {A = sqlite3ExprListAppend(0,X,0);} |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 612 | |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 613 | %type inscollist_opt {IdList*} |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 614 | %destructor inscollist_opt {sqlite3IdListDelete($$);} |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 615 | %type inscollist {IdList*} |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 616 | %destructor inscollist {sqlite3IdListDelete($$);} |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 617 | |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 618 | inscollist_opt(A) ::= . {A = 0;} |
| 619 | inscollist_opt(A) ::= LP inscollist(X) RP. {A = X;} |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 620 | inscollist(A) ::= inscollist(X) COMMA nm(Y). {A = sqlite3IdListAppend(X,&Y);} |
| 621 | inscollist(A) ::= nm(Y). {A = sqlite3IdListAppend(0,&Y);} |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 622 | |
drh | 382c024 | 2001-10-06 16:33:02 +0000 | [diff] [blame] | 623 | /////////////////////////// Expression Processing ///////////////////////////// |
| 624 | // |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 625 | |
| 626 | %type expr {Expr*} |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 627 | %destructor expr {sqlite3ExprDelete($$);} |
danielk1977 | 7977a17 | 2004-11-09 12:44:37 +0000 | [diff] [blame] | 628 | %type term {Expr*} |
| 629 | %destructor term {sqlite3ExprDelete($$);} |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 630 | |
danielk1977 | 7977a17 | 2004-11-09 12:44:37 +0000 | [diff] [blame] | 631 | expr(A) ::= term(X). {A = X;} |
danielk1977 | 752e679 | 2004-11-09 16:13:33 +0000 | [diff] [blame] | 632 | expr(A) ::= LP(B) expr(X) RP(E). {A = X; sqlite3ExprSpan(A,&B,&E); } |
danielk1977 | 7977a17 | 2004-11-09 12:44:37 +0000 | [diff] [blame] | 633 | term(A) ::= NULL(X). {A = sqlite3Expr(@X, 0, 0, &X);} |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 634 | expr(A) ::= ID(X). {A = sqlite3Expr(TK_ID, 0, 0, &X);} |
| 635 | expr(A) ::= JOIN_KW(X). {A = sqlite3Expr(TK_ID, 0, 0, &X);} |
drh | 5ad1a6c | 2002-07-01 12:27:09 +0000 | [diff] [blame] | 636 | expr(A) ::= nm(X) DOT nm(Y). { |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 637 | Expr *temp1 = sqlite3Expr(TK_ID, 0, 0, &X); |
| 638 | Expr *temp2 = sqlite3Expr(TK_ID, 0, 0, &Y); |
| 639 | A = sqlite3Expr(TK_DOT, temp1, temp2, 0); |
drh | e1b6a5b | 2000-07-29 13:06:59 +0000 | [diff] [blame] | 640 | } |
drh | d24cc42 | 2003-03-27 12:51:24 +0000 | [diff] [blame] | 641 | expr(A) ::= nm(X) DOT nm(Y) DOT nm(Z). { |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 642 | Expr *temp1 = sqlite3Expr(TK_ID, 0, 0, &X); |
| 643 | Expr *temp2 = sqlite3Expr(TK_ID, 0, 0, &Y); |
| 644 | Expr *temp3 = sqlite3Expr(TK_ID, 0, 0, &Z); |
| 645 | Expr *temp4 = sqlite3Expr(TK_DOT, temp2, temp3, 0); |
| 646 | A = sqlite3Expr(TK_DOT, temp1, temp4, 0); |
drh | d24cc42 | 2003-03-27 12:51:24 +0000 | [diff] [blame] | 647 | } |
danielk1977 | 7977a17 | 2004-11-09 12:44:37 +0000 | [diff] [blame] | 648 | term(A) ::= INTEGER(X). {A = sqlite3Expr(@X, 0, 0, &X);} |
| 649 | term(A) ::= FLOAT(X). {A = sqlite3Expr(@X, 0, 0, &X);} |
| 650 | term(A) ::= STRING(X). {A = sqlite3Expr(@X, 0, 0, &X);} |
danielk1977 | aee18ef | 2005-03-09 12:26:50 +0000 | [diff] [blame] | 651 | term(A) ::= BLOB(X). {A = sqlite3Expr(@X, 0, 0, &X);} |
drh | 4e0cff6 | 2004-11-05 05:10:28 +0000 | [diff] [blame] | 652 | expr(A) ::= REGISTER(X). {A = sqlite3RegisterExpr(pParse, &X);} |
drh | 7c972de | 2003-09-06 22:18:07 +0000 | [diff] [blame] | 653 | expr(A) ::= VARIABLE(X). { |
drh | 895d747 | 2004-08-20 16:02:39 +0000 | [diff] [blame] | 654 | Token *pToken = &X; |
| 655 | Expr *pExpr = A = sqlite3Expr(TK_VARIABLE, 0, 0, pToken); |
drh | fa6bc00 | 2004-09-07 16:19:52 +0000 | [diff] [blame] | 656 | sqlite3ExprAssignVarNumber(pParse, pExpr); |
drh | 7c972de | 2003-09-06 22:18:07 +0000 | [diff] [blame] | 657 | } |
drh | 487e262 | 2005-06-25 18:42:14 +0000 | [diff] [blame] | 658 | %ifndef SQLITE_OMIT_CAST |
| 659 | expr(A) ::= CAST(X) LP expr(E) AS typetoken(T) RP(Y). { |
| 660 | A = sqlite3Expr(TK_CAST, E, 0, &T); |
| 661 | sqlite3ExprSpan(A,&X,&Y); |
| 662 | } |
| 663 | %endif // SQLITE_OMIT_CAST |
drh | fd35797 | 2005-09-09 01:33:19 +0000 | [diff] [blame] | 664 | expr(A) ::= ID(X) LP distinct(D) exprlist(Y) RP(E). { |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 665 | A = sqlite3ExprFunction(Y, &X); |
| 666 | sqlite3ExprSpan(A,&X,&E); |
drh | fd35797 | 2005-09-09 01:33:19 +0000 | [diff] [blame] | 667 | if( D ){ |
drh | fd35797 | 2005-09-09 01:33:19 +0000 | [diff] [blame] | 668 | A->flags |= EP_Distinct; |
| 669 | } |
drh | e1b6a5b | 2000-07-29 13:06:59 +0000 | [diff] [blame] | 670 | } |
| 671 | expr(A) ::= ID(X) LP STAR RP(E). { |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 672 | A = sqlite3ExprFunction(0, &X); |
| 673 | sqlite3ExprSpan(A,&X,&E); |
drh | e1b6a5b | 2000-07-29 13:06:59 +0000 | [diff] [blame] | 674 | } |
drh | b71090f | 2005-05-23 17:26:51 +0000 | [diff] [blame] | 675 | term(A) ::= CTIME_KW(OP). { |
| 676 | /* The CURRENT_TIME, CURRENT_DATE, and CURRENT_TIMESTAMP values are |
| 677 | ** treated as functions that return constants */ |
| 678 | A = sqlite3ExprFunction(0,&OP); |
| 679 | if( A ) A->op = TK_CONST_FUNC; |
| 680 | } |
drh | 7ac25c7 | 2004-08-19 15:12:26 +0000 | [diff] [blame] | 681 | expr(A) ::= expr(X) AND(OP) expr(Y). {A = sqlite3Expr(@OP, X, Y, 0);} |
| 682 | expr(A) ::= expr(X) OR(OP) expr(Y). {A = sqlite3Expr(@OP, X, Y, 0);} |
| 683 | expr(A) ::= expr(X) LT(OP) expr(Y). {A = sqlite3Expr(@OP, X, Y, 0);} |
| 684 | expr(A) ::= expr(X) GT(OP) expr(Y). {A = sqlite3Expr(@OP, X, Y, 0);} |
| 685 | expr(A) ::= expr(X) LE(OP) expr(Y). {A = sqlite3Expr(@OP, X, Y, 0);} |
| 686 | expr(A) ::= expr(X) GE(OP) expr(Y). {A = sqlite3Expr(@OP, X, Y, 0);} |
| 687 | expr(A) ::= expr(X) NE(OP) expr(Y). {A = sqlite3Expr(@OP, X, Y, 0);} |
| 688 | expr(A) ::= expr(X) EQ(OP) expr(Y). {A = sqlite3Expr(@OP, X, Y, 0);} |
| 689 | expr(A) ::= expr(X) BITAND(OP) expr(Y). {A = sqlite3Expr(@OP, X, Y, 0);} |
| 690 | expr(A) ::= expr(X) BITOR(OP) expr(Y). {A = sqlite3Expr(@OP, X, Y, 0);} |
| 691 | expr(A) ::= expr(X) LSHIFT(OP) expr(Y). {A = sqlite3Expr(@OP, X, Y, 0);} |
| 692 | expr(A) ::= expr(X) RSHIFT(OP) expr(Y). {A = sqlite3Expr(@OP, X, Y, 0);} |
drh | 74ad7fe | 2004-10-07 03:06:28 +0000 | [diff] [blame] | 693 | expr(A) ::= expr(X) PLUS(OP) expr(Y). {A = sqlite3Expr(@OP, X, Y, 0);} |
| 694 | expr(A) ::= expr(X) MINUS(OP) expr(Y). {A = sqlite3Expr(@OP, X, Y, 0);} |
| 695 | expr(A) ::= expr(X) STAR(OP) expr(Y). {A = sqlite3Expr(@OP, X, Y, 0);} |
| 696 | expr(A) ::= expr(X) SLASH(OP) expr(Y). {A = sqlite3Expr(@OP, X, Y, 0);} |
| 697 | expr(A) ::= expr(X) REM(OP) expr(Y). {A = sqlite3Expr(@OP, X, Y, 0);} |
| 698 | expr(A) ::= expr(X) CONCAT(OP) expr(Y). {A = sqlite3Expr(@OP, X, Y, 0);} |
| 699 | %type likeop {struct LikeOp} |
drh | b71090f | 2005-05-23 17:26:51 +0000 | [diff] [blame] | 700 | likeop(A) ::= LIKE_KW(X). {A.operator = X; A.not = 0;} |
| 701 | likeop(A) ::= NOT LIKE_KW(X). {A.operator = X; A.not = 1;} |
danielk1977 | 7c6303c | 2004-11-17 16:41:29 +0000 | [diff] [blame] | 702 | %type escape {Expr*} |
| 703 | escape(X) ::= ESCAPE expr(A). [ESCAPE] {X = A;} |
| 704 | escape(X) ::= . [ESCAPE] {X = 0;} |
drh | b71090f | 2005-05-23 17:26:51 +0000 | [diff] [blame] | 705 | expr(A) ::= expr(X) likeop(OP) expr(Y) escape(E). [LIKE_KW] { |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 706 | ExprList *pList = sqlite3ExprListAppend(0, Y, 0); |
| 707 | pList = sqlite3ExprListAppend(pList, X, 0); |
danielk1977 | 7c6303c | 2004-11-17 16:41:29 +0000 | [diff] [blame] | 708 | if( E ){ |
| 709 | pList = sqlite3ExprListAppend(pList, E, 0); |
| 710 | } |
drh | b71090f | 2005-05-23 17:26:51 +0000 | [diff] [blame] | 711 | A = sqlite3ExprFunction(pList, &OP.operator); |
drh | 2e3a1f1 | 2004-10-06 14:39:28 +0000 | [diff] [blame] | 712 | if( OP.not ) A = sqlite3Expr(TK_NOT, A, 0, 0); |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 713 | sqlite3ExprSpan(A, &X->span, &Y->span); |
drh | 0ac6589 | 2002-04-20 14:24:41 +0000 | [diff] [blame] | 714 | } |
danielk1977 | 7c6303c | 2004-11-17 16:41:29 +0000 | [diff] [blame] | 715 | |
drh | e1b6a5b | 2000-07-29 13:06:59 +0000 | [diff] [blame] | 716 | expr(A) ::= expr(X) ISNULL(E). { |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 717 | A = sqlite3Expr(TK_ISNULL, X, 0, 0); |
| 718 | sqlite3ExprSpan(A,&X->span,&E); |
drh | e1b6a5b | 2000-07-29 13:06:59 +0000 | [diff] [blame] | 719 | } |
drh | 33048c0 | 2001-10-01 14:29:22 +0000 | [diff] [blame] | 720 | expr(A) ::= expr(X) IS NULL(E). { |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 721 | A = sqlite3Expr(TK_ISNULL, X, 0, 0); |
| 722 | sqlite3ExprSpan(A,&X->span,&E); |
drh | 33048c0 | 2001-10-01 14:29:22 +0000 | [diff] [blame] | 723 | } |
drh | e1b6a5b | 2000-07-29 13:06:59 +0000 | [diff] [blame] | 724 | expr(A) ::= expr(X) NOTNULL(E). { |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 725 | A = sqlite3Expr(TK_NOTNULL, X, 0, 0); |
| 726 | sqlite3ExprSpan(A,&X->span,&E); |
drh | e1b6a5b | 2000-07-29 13:06:59 +0000 | [diff] [blame] | 727 | } |
drh | 33048c0 | 2001-10-01 14:29:22 +0000 | [diff] [blame] | 728 | expr(A) ::= expr(X) NOT NULL(E). { |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 729 | A = sqlite3Expr(TK_NOTNULL, X, 0, 0); |
| 730 | sqlite3ExprSpan(A,&X->span,&E); |
drh | 33048c0 | 2001-10-01 14:29:22 +0000 | [diff] [blame] | 731 | } |
drh | 81a20f2 | 2001-10-12 17:30:04 +0000 | [diff] [blame] | 732 | expr(A) ::= expr(X) IS NOT NULL(E). { |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 733 | A = sqlite3Expr(TK_NOTNULL, X, 0, 0); |
| 734 | sqlite3ExprSpan(A,&X->span,&E); |
drh | 81a20f2 | 2001-10-12 17:30:04 +0000 | [diff] [blame] | 735 | } |
drh | e1b6a5b | 2000-07-29 13:06:59 +0000 | [diff] [blame] | 736 | expr(A) ::= NOT(B) expr(X). { |
drh | 7ac25c7 | 2004-08-19 15:12:26 +0000 | [diff] [blame] | 737 | A = sqlite3Expr(@B, X, 0, 0); |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 738 | sqlite3ExprSpan(A,&B,&X->span); |
drh | e1b6a5b | 2000-07-29 13:06:59 +0000 | [diff] [blame] | 739 | } |
drh | 81a20f2 | 2001-10-12 17:30:04 +0000 | [diff] [blame] | 740 | expr(A) ::= BITNOT(B) expr(X). { |
drh | 7ac25c7 | 2004-08-19 15:12:26 +0000 | [diff] [blame] | 741 | A = sqlite3Expr(@B, X, 0, 0); |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 742 | sqlite3ExprSpan(A,&B,&X->span); |
drh | 81a20f2 | 2001-10-12 17:30:04 +0000 | [diff] [blame] | 743 | } |
drh | e1b6a5b | 2000-07-29 13:06:59 +0000 | [diff] [blame] | 744 | expr(A) ::= MINUS(B) expr(X). [UMINUS] { |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 745 | A = sqlite3Expr(TK_UMINUS, X, 0, 0); |
| 746 | sqlite3ExprSpan(A,&B,&X->span); |
drh | e1b6a5b | 2000-07-29 13:06:59 +0000 | [diff] [blame] | 747 | } |
drh | 4b59ab5 | 2002-08-24 18:24:51 +0000 | [diff] [blame] | 748 | expr(A) ::= PLUS(B) expr(X). [UPLUS] { |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 749 | A = sqlite3Expr(TK_UPLUS, X, 0, 0); |
| 750 | sqlite3ExprSpan(A,&B,&X->span); |
drh | e1b6a5b | 2000-07-29 13:06:59 +0000 | [diff] [blame] | 751 | } |
drh | 2e3a1f1 | 2004-10-06 14:39:28 +0000 | [diff] [blame] | 752 | %type between_op {int} |
| 753 | between_op(A) ::= BETWEEN. {A = 0;} |
| 754 | between_op(A) ::= NOT BETWEEN. {A = 1;} |
| 755 | expr(A) ::= expr(W) between_op(N) expr(X) AND expr(Y). [BETWEEN] { |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 756 | ExprList *pList = sqlite3ExprListAppend(0, X, 0); |
| 757 | pList = sqlite3ExprListAppend(pList, Y, 0); |
| 758 | A = sqlite3Expr(TK_BETWEEN, W, 0, 0); |
drh | 53f733c | 2005-09-16 02:38:09 +0000 | [diff] [blame] | 759 | if( A ){ |
| 760 | A->pList = pList; |
| 761 | }else{ |
| 762 | sqlite3ExprListDelete(pList); |
| 763 | } |
drh | 2e3a1f1 | 2004-10-06 14:39:28 +0000 | [diff] [blame] | 764 | if( N ) A = sqlite3Expr(TK_NOT, A, 0, 0); |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 765 | sqlite3ExprSpan(A,&W->span,&Y->span); |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 766 | } |
drh | 51522cd | 2005-01-20 13:36:19 +0000 | [diff] [blame] | 767 | %ifndef SQLITE_OMIT_SUBQUERY |
danielk1977 | 3e8c37e | 2005-01-21 03:12:14 +0000 | [diff] [blame] | 768 | %type in_op {int} |
| 769 | in_op(A) ::= IN. {A = 0;} |
| 770 | in_op(A) ::= NOT IN. {A = 1;} |
| 771 | expr(A) ::= expr(X) in_op(N) LP exprlist(Y) RP(E). [IN] { |
| 772 | A = sqlite3Expr(TK_IN, X, 0, 0); |
danielk1977 | d5d5652 | 2005-03-16 12:15:20 +0000 | [diff] [blame] | 773 | if( A ){ |
| 774 | A->pList = Y; |
| 775 | }else{ |
| 776 | sqlite3ExprListDelete(Y); |
| 777 | } |
danielk1977 | 3e8c37e | 2005-01-21 03:12:14 +0000 | [diff] [blame] | 778 | if( N ) A = sqlite3Expr(TK_NOT, A, 0, 0); |
| 779 | sqlite3ExprSpan(A,&X->span,&E); |
| 780 | } |
drh | 51522cd | 2005-01-20 13:36:19 +0000 | [diff] [blame] | 781 | expr(A) ::= LP(B) select(X) RP(E). { |
| 782 | A = sqlite3Expr(TK_SELECT, 0, 0, 0); |
drh | 53f733c | 2005-09-16 02:38:09 +0000 | [diff] [blame] | 783 | if( A ){ |
| 784 | A->pSelect = X; |
| 785 | }else{ |
| 786 | sqlite3SelectDelete(X); |
| 787 | } |
drh | 51522cd | 2005-01-20 13:36:19 +0000 | [diff] [blame] | 788 | sqlite3ExprSpan(A,&B,&E); |
| 789 | } |
| 790 | expr(A) ::= expr(X) in_op(N) LP select(Y) RP(E). [IN] { |
| 791 | A = sqlite3Expr(TK_IN, X, 0, 0); |
drh | 53f733c | 2005-09-16 02:38:09 +0000 | [diff] [blame] | 792 | if( A ){ |
| 793 | A->pSelect = Y; |
| 794 | }else{ |
| 795 | sqlite3SelectDelete(Y); |
| 796 | } |
drh | 51522cd | 2005-01-20 13:36:19 +0000 | [diff] [blame] | 797 | if( N ) A = sqlite3Expr(TK_NOT, A, 0, 0); |
| 798 | sqlite3ExprSpan(A,&X->span,&E); |
| 799 | } |
| 800 | expr(A) ::= expr(X) in_op(N) nm(Y) dbnm(Z). [IN] { |
| 801 | SrcList *pSrc = sqlite3SrcListAppend(0,&Y,&Z); |
| 802 | A = sqlite3Expr(TK_IN, X, 0, 0); |
drh | 53f733c | 2005-09-16 02:38:09 +0000 | [diff] [blame] | 803 | if( A ){ |
| 804 | A->pSelect = sqlite3SelectNew(0,pSrc,0,0,0,0,0,0,0); |
| 805 | }else{ |
| 806 | sqlite3SrcListDelete(pSrc); |
| 807 | } |
drh | 51522cd | 2005-01-20 13:36:19 +0000 | [diff] [blame] | 808 | if( N ) A = sqlite3Expr(TK_NOT, A, 0, 0); |
| 809 | sqlite3ExprSpan(A,&X->span,Z.z?&Z:&Y); |
| 810 | } |
| 811 | expr(A) ::= EXISTS(B) LP select(Y) RP(E). { |
| 812 | Expr *p = A = sqlite3Expr(TK_EXISTS, 0, 0, 0); |
| 813 | if( p ){ |
| 814 | p->pSelect = Y; |
| 815 | sqlite3ExprSpan(p,&B,&E); |
drh | 53f733c | 2005-09-16 02:38:09 +0000 | [diff] [blame] | 816 | }else{ |
| 817 | sqlite3SelectDelete(Y); |
drh | 51522cd | 2005-01-20 13:36:19 +0000 | [diff] [blame] | 818 | } |
| 819 | } |
| 820 | %endif // SQLITE_OMIT_SUBQUERY |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 821 | |
drh | 17a7f8d | 2002-03-24 13:13:27 +0000 | [diff] [blame] | 822 | /* CASE expressions */ |
| 823 | expr(A) ::= CASE(C) case_operand(X) case_exprlist(Y) case_else(Z) END(E). { |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 824 | A = sqlite3Expr(TK_CASE, X, Z, 0); |
drh | 53f733c | 2005-09-16 02:38:09 +0000 | [diff] [blame] | 825 | if( A ){ |
| 826 | A->pList = Y; |
| 827 | }else{ |
| 828 | sqlite3ExprListDelete(Y); |
| 829 | } |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 830 | sqlite3ExprSpan(A, &C, &E); |
drh | 17a7f8d | 2002-03-24 13:13:27 +0000 | [diff] [blame] | 831 | } |
| 832 | %type case_exprlist {ExprList*} |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 833 | %destructor case_exprlist {sqlite3ExprListDelete($$);} |
drh | 17a7f8d | 2002-03-24 13:13:27 +0000 | [diff] [blame] | 834 | case_exprlist(A) ::= case_exprlist(X) WHEN expr(Y) THEN expr(Z). { |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 835 | A = sqlite3ExprListAppend(X, Y, 0); |
| 836 | A = sqlite3ExprListAppend(A, Z, 0); |
drh | 17a7f8d | 2002-03-24 13:13:27 +0000 | [diff] [blame] | 837 | } |
| 838 | case_exprlist(A) ::= WHEN expr(Y) THEN expr(Z). { |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 839 | A = sqlite3ExprListAppend(0, Y, 0); |
| 840 | A = sqlite3ExprListAppend(A, Z, 0); |
drh | 17a7f8d | 2002-03-24 13:13:27 +0000 | [diff] [blame] | 841 | } |
| 842 | %type case_else {Expr*} |
| 843 | case_else(A) ::= ELSE expr(X). {A = X;} |
| 844 | case_else(A) ::= . {A = 0;} |
| 845 | %type case_operand {Expr*} |
| 846 | case_operand(A) ::= expr(X). {A = X;} |
| 847 | case_operand(A) ::= . {A = 0;} |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 848 | |
| 849 | %type exprlist {ExprList*} |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 850 | %destructor exprlist {sqlite3ExprListDelete($$);} |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 851 | %type expritem {Expr*} |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 852 | %destructor expritem {sqlite3ExprDelete($$);} |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 853 | |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 854 | exprlist(A) ::= exprlist(X) COMMA expritem(Y). |
drh | 74ad7fe | 2004-10-07 03:06:28 +0000 | [diff] [blame] | 855 | {A = sqlite3ExprListAppend(X,Y,0);} |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 856 | exprlist(A) ::= expritem(X). {A = sqlite3ExprListAppend(0,X,0);} |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 857 | expritem(A) ::= expr(X). {A = X;} |
| 858 | expritem(A) ::= . {A = 0;} |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 859 | |
drh | 382c024 | 2001-10-06 16:33:02 +0000 | [diff] [blame] | 860 | ///////////////////////////// The CREATE INDEX command /////////////////////// |
| 861 | // |
danielk1977 | cbb18d2 | 2004-05-28 11:37:27 +0000 | [diff] [blame] | 862 | cmd ::= CREATE(S) uniqueflag(U) INDEX nm(X) dbnm(D) |
danielk1977 | 2b6d46b | 2005-02-14 06:38:40 +0000 | [diff] [blame] | 863 | ON nm(Y) LP idxlist(Z) RP(E) onconf(R). { |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 864 | if( U!=OE_None ) U = R; |
| 865 | if( U==OE_Default) U = OE_Abort; |
danielk1977 | 2b6d46b | 2005-02-14 06:38:40 +0000 | [diff] [blame] | 866 | sqlite3CreateIndex(pParse, &X, &D, sqlite3SrcListAppend(0,&Y,0),Z,U, &S, &E); |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 867 | } |
drh | 717e640 | 2001-09-27 03:22:32 +0000 | [diff] [blame] | 868 | |
| 869 | %type uniqueflag {int} |
drh | 74ad7fe | 2004-10-07 03:06:28 +0000 | [diff] [blame] | 870 | uniqueflag(A) ::= UNIQUE. {A = OE_Abort;} |
| 871 | uniqueflag(A) ::= . {A = OE_None;} |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 872 | |
danielk1977 | 0202b29 | 2004-06-09 09:55:16 +0000 | [diff] [blame] | 873 | %type idxlist {ExprList*} |
| 874 | %destructor idxlist {sqlite3ExprListDelete($$);} |
| 875 | %type idxlist_opt {ExprList*} |
| 876 | %destructor idxlist_opt {sqlite3ExprListDelete($$);} |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 877 | %type idxitem {Token} |
| 878 | |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 879 | idxlist_opt(A) ::= . {A = 0;} |
| 880 | idxlist_opt(A) ::= LP idxlist(X) RP. {A = X;} |
danielk1977 | 0202b29 | 2004-06-09 09:55:16 +0000 | [diff] [blame] | 881 | idxlist(A) ::= idxlist(X) COMMA idxitem(Y) collate(C) sortorder. { |
| 882 | Expr *p = 0; |
| 883 | if( C.n>0 ){ |
| 884 | p = sqlite3Expr(TK_COLUMN, 0, 0, 0); |
| 885 | if( p ) p->pColl = sqlite3LocateCollSeq(pParse, C.z, C.n); |
| 886 | } |
| 887 | A = sqlite3ExprListAppend(X, p, &Y); |
| 888 | } |
| 889 | idxlist(A) ::= idxitem(Y) collate(C) sortorder. { |
| 890 | Expr *p = 0; |
| 891 | if( C.n>0 ){ |
| 892 | p = sqlite3Expr(TK_COLUMN, 0, 0, 0); |
| 893 | if( p ) p->pColl = sqlite3LocateCollSeq(pParse, C.z, C.n); |
| 894 | } |
| 895 | A = sqlite3ExprListAppend(0, p, &Y); |
| 896 | } |
| 897 | idxitem(A) ::= nm(X). {A = X;} |
| 898 | |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 899 | |
drh | 8aff101 | 2001-12-22 14:49:24 +0000 | [diff] [blame] | 900 | ///////////////////////////// The DROP INDEX command ///////////////////////// |
drh | 382c024 | 2001-10-06 16:33:02 +0000 | [diff] [blame] | 901 | // |
drh | 74ad7fe | 2004-10-07 03:06:28 +0000 | [diff] [blame] | 902 | cmd ::= DROP INDEX fullname(X). {sqlite3DropIndex(pParse, X);} |
drh | 982cef7 | 2000-05-30 16:27:03 +0000 | [diff] [blame] | 903 | |
drh | 382c024 | 2001-10-06 16:33:02 +0000 | [diff] [blame] | 904 | ///////////////////////////// The VACUUM command ///////////////////////////// |
| 905 | // |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 906 | cmd ::= VACUUM. {sqlite3Vacuum(pParse,0);} |
drh | 74ad7fe | 2004-10-07 03:06:28 +0000 | [diff] [blame] | 907 | cmd ::= VACUUM nm. {sqlite3Vacuum(pParse,0);} |
drh | f57b14a | 2001-09-14 18:54:08 +0000 | [diff] [blame] | 908 | |
drh | 382c024 | 2001-10-06 16:33:02 +0000 | [diff] [blame] | 909 | ///////////////////////////// The PRAGMA command ///////////////////////////// |
| 910 | // |
drh | 13d7042 | 2004-11-13 15:59:14 +0000 | [diff] [blame] | 911 | %ifndef SQLITE_OMIT_PRAGMA |
danielk1977 | 91cf71b | 2004-06-26 06:37:06 +0000 | [diff] [blame] | 912 | cmd ::= PRAGMA nm(X) dbnm(Z) EQ nm(Y). {sqlite3Pragma(pParse,&X,&Z,&Y,0);} |
| 913 | cmd ::= PRAGMA nm(X) dbnm(Z) EQ ON(Y). {sqlite3Pragma(pParse,&X,&Z,&Y,0);} |
| 914 | cmd ::= PRAGMA nm(X) dbnm(Z) EQ plus_num(Y). {sqlite3Pragma(pParse,&X,&Z,&Y,0);} |
| 915 | cmd ::= PRAGMA nm(X) dbnm(Z) EQ minus_num(Y). { |
| 916 | sqlite3Pragma(pParse,&X,&Z,&Y,1); |
| 917 | } |
| 918 | cmd ::= PRAGMA nm(X) dbnm(Z) LP nm(Y) RP. {sqlite3Pragma(pParse,&X,&Z,&Y,0);} |
drh | 74ad7fe | 2004-10-07 03:06:28 +0000 | [diff] [blame] | 919 | cmd ::= PRAGMA nm(X) dbnm(Z). {sqlite3Pragma(pParse,&X,&Z,0,0);} |
drh | 13d7042 | 2004-11-13 15:59:14 +0000 | [diff] [blame] | 920 | %endif // SQLITE_OMIT_PRAGMA |
drh | f57b14a | 2001-09-14 18:54:08 +0000 | [diff] [blame] | 921 | plus_num(A) ::= plus_opt number(X). {A = X;} |
| 922 | minus_num(A) ::= MINUS number(X). {A = X;} |
drh | 74ad7fe | 2004-10-07 03:06:28 +0000 | [diff] [blame] | 923 | number(A) ::= INTEGER(X). {A = X;} |
| 924 | number(A) ::= FLOAT(X). {A = X;} |
drh | f57b14a | 2001-09-14 18:54:08 +0000 | [diff] [blame] | 925 | plus_opt ::= PLUS. |
| 926 | plus_opt ::= . |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 927 | |
| 928 | //////////////////////////// The CREATE TRIGGER command ///////////////////// |
drh | f0f258b | 2003-04-21 18:48:45 +0000 | [diff] [blame] | 929 | |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 930 | %ifndef SQLITE_OMIT_TRIGGER |
| 931 | |
danielk1977 | 3df6b25 | 2004-05-29 10:23:19 +0000 | [diff] [blame] | 932 | cmd ::= CREATE trigger_decl(A) BEGIN trigger_cmd_list(S) END(Z). { |
drh | 4b59ab5 | 2002-08-24 18:24:51 +0000 | [diff] [blame] | 933 | Token all; |
| 934 | all.z = A.z; |
| 935 | all.n = (Z.z - A.z) + Z.n; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 936 | sqlite3FinishTrigger(pParse, S, &all); |
drh | f0f258b | 2003-04-21 18:48:45 +0000 | [diff] [blame] | 937 | } |
| 938 | |
drh | 74ad7fe | 2004-10-07 03:06:28 +0000 | [diff] [blame] | 939 | trigger_decl(A) ::= temp(T) TRIGGER nm(B) dbnm(Z) trigger_time(C) |
| 940 | trigger_event(D) |
| 941 | ON fullname(E) foreach_clause(F) when_clause(G). { |
| 942 | sqlite3BeginTrigger(pParse, &B, &Z, C, D.a, D.b, E, F, G, T); |
danielk1977 | 3df6b25 | 2004-05-29 10:23:19 +0000 | [diff] [blame] | 943 | A = (Z.n==0?B:Z); |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 944 | } |
| 945 | |
| 946 | %type trigger_time {int} |
| 947 | trigger_time(A) ::= BEFORE. { A = TK_BEFORE; } |
| 948 | trigger_time(A) ::= AFTER. { A = TK_AFTER; } |
| 949 | trigger_time(A) ::= INSTEAD OF. { A = TK_INSTEAD;} |
| 950 | trigger_time(A) ::= . { A = TK_BEFORE; } |
| 951 | |
drh | ad3cab5 | 2002-05-24 02:04:32 +0000 | [diff] [blame] | 952 | %type trigger_event {struct TrigEvent} |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 953 | %destructor trigger_event {sqlite3IdListDelete($$.b);} |
drh | 74ad7fe | 2004-10-07 03:06:28 +0000 | [diff] [blame] | 954 | trigger_event(A) ::= DELETE(OP). {A.a = @OP; A.b = 0;} |
| 955 | trigger_event(A) ::= INSERT(OP). {A.a = @OP; A.b = 0;} |
| 956 | trigger_event(A) ::= UPDATE(OP). {A.a = @OP; A.b = 0;} |
| 957 | trigger_event(A) ::= UPDATE OF inscollist(X). {A.a = TK_UPDATE; A.b = X;} |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 958 | |
| 959 | %type foreach_clause {int} |
| 960 | foreach_clause(A) ::= . { A = TK_ROW; } |
| 961 | foreach_clause(A) ::= FOR EACH ROW. { A = TK_ROW; } |
| 962 | foreach_clause(A) ::= FOR EACH STATEMENT. { A = TK_STATEMENT; } |
| 963 | |
drh | 0bb132b | 2004-07-20 14:06:51 +0000 | [diff] [blame] | 964 | %type when_clause {Expr*} |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 965 | when_clause(A) ::= . { A = 0; } |
| 966 | when_clause(A) ::= WHEN expr(X). { A = X; } |
| 967 | |
drh | 0bb132b | 2004-07-20 14:06:51 +0000 | [diff] [blame] | 968 | %type trigger_cmd_list {TriggerStep*} |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 969 | %destructor trigger_cmd_list {sqlite3DeleteTriggerStep($$);} |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 970 | trigger_cmd_list(A) ::= trigger_cmd(X) SEMI trigger_cmd_list(Y). { |
drh | a69d916 | 2003-04-17 22:57:53 +0000 | [diff] [blame] | 971 | X->pNext = Y; |
| 972 | A = X; |
| 973 | } |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 974 | trigger_cmd_list(A) ::= . { A = 0; } |
| 975 | |
drh | 0bb132b | 2004-07-20 14:06:51 +0000 | [diff] [blame] | 976 | %type trigger_cmd {TriggerStep*} |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 977 | %destructor trigger_cmd {sqlite3DeleteTriggerStep($$);} |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 978 | // UPDATE |
drh | 5ad1a6c | 2002-07-01 12:27:09 +0000 | [diff] [blame] | 979 | trigger_cmd(A) ::= UPDATE orconf(R) nm(X) SET setlist(Y) where_opt(Z). |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 980 | { A = sqlite3TriggerUpdateStep(&X, Y, Z, R); } |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 981 | |
| 982 | // INSERT |
drh | 3054efe | 2004-02-12 17:28:13 +0000 | [diff] [blame] | 983 | trigger_cmd(A) ::= insert_cmd(R) INTO nm(X) inscollist_opt(F) |
drh | 74ad7fe | 2004-10-07 03:06:28 +0000 | [diff] [blame] | 984 | VALUES LP itemlist(Y) RP. |
| 985 | {A = sqlite3TriggerInsertStep(&X, F, Y, 0, R);} |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 986 | |
drh | 3054efe | 2004-02-12 17:28:13 +0000 | [diff] [blame] | 987 | trigger_cmd(A) ::= insert_cmd(R) INTO nm(X) inscollist_opt(F) select(S). |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 988 | {A = sqlite3TriggerInsertStep(&X, F, 0, S, R);} |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 989 | |
| 990 | // DELETE |
drh | 5ad1a6c | 2002-07-01 12:27:09 +0000 | [diff] [blame] | 991 | trigger_cmd(A) ::= DELETE FROM nm(X) where_opt(Y). |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 992 | {A = sqlite3TriggerDeleteStep(&X, Y);} |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 993 | |
| 994 | // SELECT |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 995 | trigger_cmd(A) ::= select(X). {A = sqlite3TriggerSelectStep(X); } |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 996 | |
danielk1977 | 6f34903 | 2002-06-11 02:25:40 +0000 | [diff] [blame] | 997 | // The special RAISE expression that may occur in trigger programs |
drh | 4b59ab5 | 2002-08-24 18:24:51 +0000 | [diff] [blame] | 998 | expr(A) ::= RAISE(X) LP IGNORE RP(Y). { |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 999 | A = sqlite3Expr(TK_RAISE, 0, 0, 0); |
drh | 4b59ab5 | 2002-08-24 18:24:51 +0000 | [diff] [blame] | 1000 | A->iColumn = OE_Ignore; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1001 | sqlite3ExprSpan(A, &X, &Y); |
drh | 4b59ab5 | 2002-08-24 18:24:51 +0000 | [diff] [blame] | 1002 | } |
drh | 74ad7fe | 2004-10-07 03:06:28 +0000 | [diff] [blame] | 1003 | expr(A) ::= RAISE(X) LP raisetype(T) COMMA nm(Z) RP(Y). { |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1004 | A = sqlite3Expr(TK_RAISE, 0, 0, &Z); |
drh | 74ad7fe | 2004-10-07 03:06:28 +0000 | [diff] [blame] | 1005 | A->iColumn = T; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1006 | sqlite3ExprSpan(A, &X, &Y); |
drh | 4b59ab5 | 2002-08-24 18:24:51 +0000 | [diff] [blame] | 1007 | } |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 1008 | %endif // !SQLITE_OMIT_TRIGGER |
| 1009 | |
drh | 74ad7fe | 2004-10-07 03:06:28 +0000 | [diff] [blame] | 1010 | %type raisetype {int} |
| 1011 | raisetype(A) ::= ROLLBACK. {A = OE_Rollback;} |
| 1012 | raisetype(A) ::= ABORT. {A = OE_Abort;} |
| 1013 | raisetype(A) ::= FAIL. {A = OE_Fail;} |
| 1014 | |
danielk1977 | 6f34903 | 2002-06-11 02:25:40 +0000 | [diff] [blame] | 1015 | |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 1016 | //////////////////////// DROP TRIGGER statement ////////////////////////////// |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 1017 | %ifndef SQLITE_OMIT_TRIGGER |
drh | 74ad7fe | 2004-10-07 03:06:28 +0000 | [diff] [blame] | 1018 | cmd ::= DROP TRIGGER fullname(X). { |
| 1019 | sqlite3DropTrigger(pParse,X); |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 1020 | } |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 1021 | %endif // !SQLITE_OMIT_TRIGGER |
drh | 113088e | 2003-03-20 01:16:58 +0000 | [diff] [blame] | 1022 | |
| 1023 | //////////////////////// ATTACH DATABASE file AS name ///////////////////////// |
drh | 4d189ca | 2004-02-12 18:46:38 +0000 | [diff] [blame] | 1024 | cmd ::= ATTACH database_kw_opt ids(F) AS nm(D) key_opt(K). { |
drh | 25d6543 | 2004-07-22 15:02:25 +0000 | [diff] [blame] | 1025 | sqlite3Attach(pParse, &F, &D, K.type, &K.key); |
drh | 1c2d841 | 2003-03-31 00:30:47 +0000 | [diff] [blame] | 1026 | } |
drh | 25d6543 | 2004-07-22 15:02:25 +0000 | [diff] [blame] | 1027 | %type key_opt {struct AttachKey} |
| 1028 | key_opt(A) ::= . { A.type = 0; } |
drh | 25d6543 | 2004-07-22 15:02:25 +0000 | [diff] [blame] | 1029 | key_opt(A) ::= KEY ids(X). { A.type=1; A.key = X; } |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 1030 | key_opt(A) ::= KEY BLOB(X). { A.type=2; A.key = X; } |
drh | 113088e | 2003-03-20 01:16:58 +0000 | [diff] [blame] | 1031 | |
| 1032 | database_kw_opt ::= DATABASE. |
| 1033 | database_kw_opt ::= . |
| 1034 | |
| 1035 | //////////////////////// DETACH DATABASE name ///////////////////////////////// |
drh | 1c2d841 | 2003-03-31 00:30:47 +0000 | [diff] [blame] | 1036 | cmd ::= DETACH database_kw_opt nm(D). { |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1037 | sqlite3Detach(pParse, &D); |
drh | 1c2d841 | 2003-03-31 00:30:47 +0000 | [diff] [blame] | 1038 | } |
drh | 4343fea | 2004-11-05 23:46:15 +0000 | [diff] [blame] | 1039 | |
| 1040 | ////////////////////////// REINDEX collation ////////////////////////////////// |
| 1041 | %ifndef SQLITE_OMIT_REINDEX |
| 1042 | cmd ::= REINDEX. {sqlite3Reindex(pParse, 0, 0);} |
| 1043 | cmd ::= REINDEX nm(X) dbnm(Y). {sqlite3Reindex(pParse, &X, &Y);} |
| 1044 | %endif |
danielk1977 | 9fd2a9a | 2004-11-12 13:42:30 +0000 | [diff] [blame] | 1045 | |
drh | 9f18e8a | 2005-07-08 12:13:04 +0000 | [diff] [blame] | 1046 | /////////////////////////////////// ANALYZE /////////////////////////////////// |
| 1047 | %ifndef SQLITE_OMIT_ANALYZE |
| 1048 | cmd ::= ANALYZE. {sqlite3Analyze(pParse, 0, 0);} |
| 1049 | cmd ::= ANALYZE nm(X) dbnm(Y). {sqlite3Analyze(pParse, &X, &Y);} |
| 1050 | %endif |
| 1051 | |
danielk1977 | 9fd2a9a | 2004-11-12 13:42:30 +0000 | [diff] [blame] | 1052 | //////////////////////// ALTER TABLE table ... //////////////////////////////// |
| 1053 | %ifndef SQLITE_OMIT_ALTERTABLE |
| 1054 | cmd ::= ALTER TABLE fullname(X) RENAME TO nm(Z). { |
| 1055 | sqlite3AlterRenameTable(pParse,X,&Z); |
| 1056 | } |
danielk1977 | 19a8e7e | 2005-03-17 05:03:38 +0000 | [diff] [blame] | 1057 | cmd ::= ALTER TABLE add_column_fullname ADD kwcolumn_opt column(Y). { |
| 1058 | sqlite3AlterFinishAddColumn(pParse, &Y); |
| 1059 | } |
| 1060 | add_column_fullname ::= fullname(X). { |
| 1061 | sqlite3AlterBeginAddColumn(pParse, X); |
| 1062 | } |
| 1063 | kwcolumn_opt ::= . |
| 1064 | kwcolumn_opt ::= COLUMNKW. |
danielk1977 | 9fd2a9a | 2004-11-12 13:42:30 +0000 | [diff] [blame] | 1065 | %endif |