drh | d0e4a6c | 2005-02-15 20:47:57 +0000 | [diff] [blame] | 1 | /* |
| 2 | ** 2005 February 15 |
| 3 | ** |
| 4 | ** The author disclaims copyright to this source code. In place of |
| 5 | ** a legal notice, here is a blessing: |
| 6 | ** |
| 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. |
| 10 | ** |
| 11 | ************************************************************************* |
| 12 | ** This file contains C code routines that used to generate VDBE code |
| 13 | ** that implements the ALTER TABLE command. |
drh | d0e4a6c | 2005-02-15 20:47:57 +0000 | [diff] [blame] | 14 | */ |
| 15 | #include "sqliteInt.h" |
| 16 | |
drh | 1f01ec1 | 2005-02-15 21:36:18 +0000 | [diff] [blame] | 17 | /* |
| 18 | ** The code in this file only exists if we are not omitting the |
| 19 | ** ALTER TABLE logic from the build. |
| 20 | */ |
drh | d0e4a6c | 2005-02-15 20:47:57 +0000 | [diff] [blame] | 21 | #ifndef SQLITE_OMIT_ALTERTABLE |
drh | 1f01ec1 | 2005-02-15 21:36:18 +0000 | [diff] [blame] | 22 | |
| 23 | |
| 24 | /* |
| 25 | ** This function is used by SQL generated to implement the |
| 26 | ** ALTER TABLE command. The first argument is the text of a CREATE TABLE or |
| 27 | ** CREATE INDEX command. The second is a table name. The table name in |
drh | f11c34d | 2006-09-08 12:27:36 +0000 | [diff] [blame] | 28 | ** the CREATE TABLE or CREATE INDEX statement is replaced with the third |
drh | 1f01ec1 | 2005-02-15 21:36:18 +0000 | [diff] [blame] | 29 | ** argument and the result returned. Examples: |
| 30 | ** |
| 31 | ** sqlite_rename_table('CREATE TABLE abc(a, b, c)', 'def') |
| 32 | ** -> 'CREATE TABLE def(a, b, c)' |
| 33 | ** |
| 34 | ** sqlite_rename_table('CREATE INDEX i ON abc(a)', 'def') |
| 35 | ** -> 'CREATE INDEX i ON def(a, b, c)' |
| 36 | */ |
| 37 | static void renameTableFunc( |
| 38 | sqlite3_context *context, |
danielk1977 | 62c14b3 | 2008-11-19 09:05:26 +0000 | [diff] [blame] | 39 | int NotUsed, |
drh | 1f01ec1 | 2005-02-15 21:36:18 +0000 | [diff] [blame] | 40 | sqlite3_value **argv |
| 41 | ){ |
| 42 | unsigned char const *zSql = sqlite3_value_text(argv[0]); |
| 43 | unsigned char const *zTableName = sqlite3_value_text(argv[1]); |
| 44 | |
| 45 | int token; |
| 46 | Token tname; |
drh | 2646da7 | 2005-12-09 20:02:05 +0000 | [diff] [blame] | 47 | unsigned char const *zCsr = zSql; |
drh | 1f01ec1 | 2005-02-15 21:36:18 +0000 | [diff] [blame] | 48 | int len = 0; |
| 49 | char *zRet; |
| 50 | |
drh | fa4a4b9 | 2008-03-19 21:45:51 +0000 | [diff] [blame] | 51 | sqlite3 *db = sqlite3_context_db_handle(context); |
danielk1977 | 1e53695 | 2007-08-16 10:09:01 +0000 | [diff] [blame] | 52 | |
danielk1977 | 62c14b3 | 2008-11-19 09:05:26 +0000 | [diff] [blame] | 53 | UNUSED_PARAMETER(NotUsed); |
| 54 | |
drh | 1f01ec1 | 2005-02-15 21:36:18 +0000 | [diff] [blame] | 55 | /* The principle used to locate the table name in the CREATE TABLE |
drh | 7382945 | 2008-05-09 14:17:51 +0000 | [diff] [blame] | 56 | ** statement is that the table name is the first non-space token that |
drh | 6aa1edc | 2008-07-07 12:44:58 +0000 | [diff] [blame] | 57 | ** is immediately followed by a TK_LP or TK_USING token. |
drh | 1f01ec1 | 2005-02-15 21:36:18 +0000 | [diff] [blame] | 58 | */ |
| 59 | if( zSql ){ |
| 60 | do { |
danielk1977 | dce872b | 2007-05-08 12:37:45 +0000 | [diff] [blame] | 61 | if( !*zCsr ){ |
| 62 | /* Ran out of input before finding an opening bracket. Return NULL. */ |
| 63 | return; |
| 64 | } |
| 65 | |
drh | 1f01ec1 | 2005-02-15 21:36:18 +0000 | [diff] [blame] | 66 | /* Store the token that zCsr points to in tname. */ |
drh | b7916a7 | 2009-05-27 10:31:29 +0000 | [diff] [blame] | 67 | tname.z = (char*)zCsr; |
drh | 1f01ec1 | 2005-02-15 21:36:18 +0000 | [diff] [blame] | 68 | tname.n = len; |
| 69 | |
| 70 | /* Advance zCsr to the next token. Store that token type in 'token', |
drh | 85b623f | 2007-12-13 21:54:09 +0000 | [diff] [blame] | 71 | ** and its length in 'len' (to be used next iteration of this loop). |
drh | 1f01ec1 | 2005-02-15 21:36:18 +0000 | [diff] [blame] | 72 | */ |
| 73 | do { |
| 74 | zCsr += len; |
| 75 | len = sqlite3GetToken(zCsr, &token); |
drh | 200a81d | 2008-08-08 14:19:41 +0000 | [diff] [blame] | 76 | } while( token==TK_SPACE ); |
dan | 2b5f152 | 2018-06-30 20:00:35 +0000 | [diff] [blame] | 77 | assert( len>0 || !*zCsr ); |
danielk1977 | 182c4ba | 2007-06-27 15:53:34 +0000 | [diff] [blame] | 78 | } while( token!=TK_LP && token!=TK_USING ); |
drh | 1f01ec1 | 2005-02-15 21:36:18 +0000 | [diff] [blame] | 79 | |
drh | 4300c1a | 2014-02-20 19:23:15 +0000 | [diff] [blame] | 80 | zRet = sqlite3MPrintf(db, "%.*s\"%w\"%s", (int)(((u8*)tname.z) - zSql), |
| 81 | zSql, zTableName, tname.z+tname.n); |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 82 | sqlite3_result_text(context, zRet, -1, SQLITE_DYNAMIC); |
drh | 1f01ec1 | 2005-02-15 21:36:18 +0000 | [diff] [blame] | 83 | } |
| 84 | } |
| 85 | |
dan | 432cc5b | 2009-09-26 17:51:48 +0000 | [diff] [blame] | 86 | /* |
| 87 | ** This C function implements an SQL user function that is used by SQL code |
| 88 | ** generated by the ALTER TABLE ... RENAME command to modify the definition |
| 89 | ** of any foreign key constraints that use the table being renamed as the |
| 90 | ** parent table. It is passed three arguments: |
| 91 | ** |
| 92 | ** 1) The complete text of the CREATE TABLE statement being modified, |
| 93 | ** 2) The old name of the table being renamed, and |
| 94 | ** 3) The new name of the table being renamed. |
| 95 | ** |
| 96 | ** It returns the new CREATE TABLE statement. For example: |
| 97 | ** |
| 98 | ** sqlite_rename_parent('CREATE TABLE t1(a REFERENCES t2)', 't2', 't3') |
| 99 | ** -> 'CREATE TABLE t1(a REFERENCES t3)' |
| 100 | */ |
| 101 | #ifndef SQLITE_OMIT_FOREIGN_KEY |
| 102 | static void renameParentFunc( |
| 103 | sqlite3_context *context, |
| 104 | int NotUsed, |
| 105 | sqlite3_value **argv |
| 106 | ){ |
| 107 | sqlite3 *db = sqlite3_context_db_handle(context); |
| 108 | char *zOutput = 0; |
| 109 | char *zResult; |
| 110 | unsigned char const *zInput = sqlite3_value_text(argv[0]); |
| 111 | unsigned char const *zOld = sqlite3_value_text(argv[1]); |
| 112 | unsigned char const *zNew = sqlite3_value_text(argv[2]); |
| 113 | |
| 114 | unsigned const char *z; /* Pointer to token */ |
| 115 | int n; /* Length of token z */ |
| 116 | int token; /* Type of token */ |
| 117 | |
drh | d3ceeb5 | 2009-10-13 13:08:19 +0000 | [diff] [blame] | 118 | UNUSED_PARAMETER(NotUsed); |
drh | 65b9ac5 | 2014-04-14 19:48:25 +0000 | [diff] [blame] | 119 | if( zInput==0 || zOld==0 ) return; |
dan | 432cc5b | 2009-09-26 17:51:48 +0000 | [diff] [blame] | 120 | for(z=zInput; *z; z=z+n){ |
| 121 | n = sqlite3GetToken(z, &token); |
| 122 | if( token==TK_REFERENCES ){ |
| 123 | char *zParent; |
| 124 | do { |
| 125 | z += n; |
| 126 | n = sqlite3GetToken(z, &token); |
| 127 | }while( token==TK_SPACE ); |
| 128 | |
dan | d68d1f4 | 2015-04-28 14:07:02 +0000 | [diff] [blame] | 129 | if( token==TK_ILLEGAL ) break; |
dan | 432cc5b | 2009-09-26 17:51:48 +0000 | [diff] [blame] | 130 | zParent = sqlite3DbStrNDup(db, (const char *)z, n); |
drh | a172c34 | 2009-10-08 01:43:55 +0000 | [diff] [blame] | 131 | if( zParent==0 ) break; |
dan | 432cc5b | 2009-09-26 17:51:48 +0000 | [diff] [blame] | 132 | sqlite3Dequote(zParent); |
| 133 | if( 0==sqlite3StrICmp((const char *)zOld, zParent) ){ |
| 134 | char *zOut = sqlite3MPrintf(db, "%s%.*s\"%w\"", |
drh | 4300c1a | 2014-02-20 19:23:15 +0000 | [diff] [blame] | 135 | (zOutput?zOutput:""), (int)(z-zInput), zInput, (const char *)zNew |
dan | 432cc5b | 2009-09-26 17:51:48 +0000 | [diff] [blame] | 136 | ); |
| 137 | sqlite3DbFree(db, zOutput); |
| 138 | zOutput = zOut; |
| 139 | zInput = &z[n]; |
| 140 | } |
| 141 | sqlite3DbFree(db, zParent); |
| 142 | } |
| 143 | } |
| 144 | |
drh | df12595 | 2018-06-06 23:31:26 +0000 | [diff] [blame] | 145 | zResult = sqlite3MPrintf(db, "%s%s", (zOutput?zOutput:""), zInput); |
dan | 432cc5b | 2009-09-26 17:51:48 +0000 | [diff] [blame] | 146 | sqlite3_result_text(context, zResult, -1, SQLITE_DYNAMIC); |
| 147 | sqlite3DbFree(db, zOutput); |
| 148 | } |
| 149 | #endif |
| 150 | |
drh | 1f01ec1 | 2005-02-15 21:36:18 +0000 | [diff] [blame] | 151 | #ifndef SQLITE_OMIT_TRIGGER |
drh | f11c34d | 2006-09-08 12:27:36 +0000 | [diff] [blame] | 152 | /* This function is used by SQL generated to implement the |
drh | 1f01ec1 | 2005-02-15 21:36:18 +0000 | [diff] [blame] | 153 | ** ALTER TABLE command. The first argument is the text of a CREATE TRIGGER |
| 154 | ** statement. The second is a table name. The table name in the CREATE |
drh | f11c34d | 2006-09-08 12:27:36 +0000 | [diff] [blame] | 155 | ** TRIGGER statement is replaced with the third argument and the result |
drh | 1f01ec1 | 2005-02-15 21:36:18 +0000 | [diff] [blame] | 156 | ** returned. This is analagous to renameTableFunc() above, except for CREATE |
| 157 | ** TRIGGER, not CREATE INDEX and CREATE TABLE. |
| 158 | */ |
| 159 | static void renameTriggerFunc( |
| 160 | sqlite3_context *context, |
danielk1977 | 62c14b3 | 2008-11-19 09:05:26 +0000 | [diff] [blame] | 161 | int NotUsed, |
drh | 1f01ec1 | 2005-02-15 21:36:18 +0000 | [diff] [blame] | 162 | sqlite3_value **argv |
| 163 | ){ |
| 164 | unsigned char const *zSql = sqlite3_value_text(argv[0]); |
| 165 | unsigned char const *zTableName = sqlite3_value_text(argv[1]); |
| 166 | |
| 167 | int token; |
| 168 | Token tname; |
| 169 | int dist = 3; |
drh | 2646da7 | 2005-12-09 20:02:05 +0000 | [diff] [blame] | 170 | unsigned char const *zCsr = zSql; |
drh | 1f01ec1 | 2005-02-15 21:36:18 +0000 | [diff] [blame] | 171 | int len = 0; |
| 172 | char *zRet; |
drh | fa4a4b9 | 2008-03-19 21:45:51 +0000 | [diff] [blame] | 173 | sqlite3 *db = sqlite3_context_db_handle(context); |
danielk1977 | 1e53695 | 2007-08-16 10:09:01 +0000 | [diff] [blame] | 174 | |
danielk1977 | 62c14b3 | 2008-11-19 09:05:26 +0000 | [diff] [blame] | 175 | UNUSED_PARAMETER(NotUsed); |
| 176 | |
drh | 1f01ec1 | 2005-02-15 21:36:18 +0000 | [diff] [blame] | 177 | /* The principle used to locate the table name in the CREATE TRIGGER |
peter.d.reid | 60ec914 | 2014-09-06 16:39:46 +0000 | [diff] [blame] | 178 | ** statement is that the table name is the first token that is immediately |
| 179 | ** preceded by either TK_ON or TK_DOT and immediately followed by one |
drh | 1f01ec1 | 2005-02-15 21:36:18 +0000 | [diff] [blame] | 180 | ** of TK_WHEN, TK_BEGIN or TK_FOR. |
| 181 | */ |
| 182 | if( zSql ){ |
| 183 | do { |
danielk1977 | dce872b | 2007-05-08 12:37:45 +0000 | [diff] [blame] | 184 | |
| 185 | if( !*zCsr ){ |
| 186 | /* Ran out of input before finding the table name. Return NULL. */ |
| 187 | return; |
| 188 | } |
| 189 | |
drh | 1f01ec1 | 2005-02-15 21:36:18 +0000 | [diff] [blame] | 190 | /* Store the token that zCsr points to in tname. */ |
drh | b7916a7 | 2009-05-27 10:31:29 +0000 | [diff] [blame] | 191 | tname.z = (char*)zCsr; |
drh | 1f01ec1 | 2005-02-15 21:36:18 +0000 | [diff] [blame] | 192 | tname.n = len; |
| 193 | |
| 194 | /* Advance zCsr to the next token. Store that token type in 'token', |
drh | 85b623f | 2007-12-13 21:54:09 +0000 | [diff] [blame] | 195 | ** and its length in 'len' (to be used next iteration of this loop). |
drh | 1f01ec1 | 2005-02-15 21:36:18 +0000 | [diff] [blame] | 196 | */ |
| 197 | do { |
| 198 | zCsr += len; |
| 199 | len = sqlite3GetToken(zCsr, &token); |
| 200 | }while( token==TK_SPACE ); |
dan | 2b5f152 | 2018-06-30 20:00:35 +0000 | [diff] [blame] | 201 | assert( len>0 || !*zCsr ); |
drh | 1f01ec1 | 2005-02-15 21:36:18 +0000 | [diff] [blame] | 202 | |
| 203 | /* Variable 'dist' stores the number of tokens read since the most |
| 204 | ** recent TK_DOT or TK_ON. This means that when a WHEN, FOR or BEGIN |
| 205 | ** token is read and 'dist' equals 2, the condition stated above |
| 206 | ** to be met. |
| 207 | ** |
| 208 | ** Note that ON cannot be a database, table or column name, so |
| 209 | ** there is no need to worry about syntax like |
| 210 | ** "CREATE TRIGGER ... ON ON.ON BEGIN ..." etc. |
| 211 | */ |
| 212 | dist++; |
| 213 | if( token==TK_DOT || token==TK_ON ){ |
| 214 | dist = 0; |
| 215 | } |
| 216 | } while( dist!=2 || (token!=TK_WHEN && token!=TK_FOR && token!=TK_BEGIN) ); |
| 217 | |
| 218 | /* Variable tname now contains the token that is the old table-name |
| 219 | ** in the CREATE TRIGGER statement. |
| 220 | */ |
drh | 4300c1a | 2014-02-20 19:23:15 +0000 | [diff] [blame] | 221 | zRet = sqlite3MPrintf(db, "%.*s\"%w\"%s", (int)(((u8*)tname.z) - zSql), |
| 222 | zSql, zTableName, tname.z+tname.n); |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 223 | sqlite3_result_text(context, zRet, -1, SQLITE_DYNAMIC); |
drh | 1f01ec1 | 2005-02-15 21:36:18 +0000 | [diff] [blame] | 224 | } |
| 225 | } |
| 226 | #endif /* !SQLITE_OMIT_TRIGGER */ |
| 227 | |
| 228 | /* |
dan | 432cc5b | 2009-09-26 17:51:48 +0000 | [diff] [blame] | 229 | ** This function is used to create the text of expressions of the form: |
| 230 | ** |
| 231 | ** name=<constant1> OR name=<constant2> OR ... |
| 232 | ** |
| 233 | ** If argument zWhere is NULL, then a pointer string containing the text |
| 234 | ** "name=<constant>" is returned, where <constant> is the quoted version |
| 235 | ** of the string passed as argument zConstant. The returned buffer is |
| 236 | ** allocated using sqlite3DbMalloc(). It is the responsibility of the |
| 237 | ** caller to ensure that it is eventually freed. |
| 238 | ** |
| 239 | ** If argument zWhere is not NULL, then the string returned is |
| 240 | ** "<where> OR name=<constant>", where <where> is the contents of zWhere. |
| 241 | ** In this case zWhere is passed to sqlite3DbFree() before returning. |
| 242 | ** |
| 243 | */ |
| 244 | static char *whereOrName(sqlite3 *db, char *zWhere, char *zConstant){ |
| 245 | char *zNew; |
| 246 | if( !zWhere ){ |
| 247 | zNew = sqlite3MPrintf(db, "name=%Q", zConstant); |
| 248 | }else{ |
| 249 | zNew = sqlite3MPrintf(db, "%s OR name=%Q", zWhere, zConstant); |
| 250 | sqlite3DbFree(db, zWhere); |
| 251 | } |
| 252 | return zNew; |
| 253 | } |
| 254 | |
dan | 856ef1a | 2009-09-29 06:33:23 +0000 | [diff] [blame] | 255 | #if !defined(SQLITE_OMIT_FOREIGN_KEY) && !defined(SQLITE_OMIT_TRIGGER) |
dan | 432cc5b | 2009-09-26 17:51:48 +0000 | [diff] [blame] | 256 | /* |
| 257 | ** Generate the text of a WHERE expression which can be used to select all |
| 258 | ** tables that have foreign key constraints that refer to table pTab (i.e. |
| 259 | ** constraints for which pTab is the parent table) from the sqlite_master |
| 260 | ** table. |
| 261 | */ |
| 262 | static char *whereForeignKeys(Parse *pParse, Table *pTab){ |
| 263 | FKey *p; |
| 264 | char *zWhere = 0; |
| 265 | for(p=sqlite3FkReferences(pTab); p; p=p->pNextTo){ |
| 266 | zWhere = whereOrName(pParse->db, zWhere, p->pFrom->zName); |
| 267 | } |
| 268 | return zWhere; |
drh | 1f01ec1 | 2005-02-15 21:36:18 +0000 | [diff] [blame] | 269 | } |
dan | 856ef1a | 2009-09-29 06:33:23 +0000 | [diff] [blame] | 270 | #endif |
drh | 1f01ec1 | 2005-02-15 21:36:18 +0000 | [diff] [blame] | 271 | |
drh | d0e4a6c | 2005-02-15 20:47:57 +0000 | [diff] [blame] | 272 | /* |
danielk1977 | 19a8e7e | 2005-03-17 05:03:38 +0000 | [diff] [blame] | 273 | ** Generate the text of a WHERE expression which can be used to select all |
| 274 | ** temporary triggers on table pTab from the sqlite_temp_master table. If |
| 275 | ** table pTab has no temporary triggers, or is itself stored in the |
| 276 | ** temporary database, NULL is returned. |
| 277 | */ |
| 278 | static char *whereTempTriggers(Parse *pParse, Table *pTab){ |
| 279 | Trigger *pTrig; |
| 280 | char *zWhere = 0; |
danielk1977 | e501b89 | 2006-01-09 06:29:47 +0000 | [diff] [blame] | 281 | const Schema *pTempSchema = pParse->db->aDb[1].pSchema; /* Temp db schema */ |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 282 | |
| 283 | /* If the table is not located in the temp-db (in which case NULL is |
| 284 | ** returned, loop through the tables list of triggers. For each trigger |
| 285 | ** that is not part of the temp-db schema, add a clause to the WHERE |
| 286 | ** expression being built up in zWhere. |
| 287 | */ |
| 288 | if( pTab->pSchema!=pTempSchema ){ |
danielk1977 | 1e53695 | 2007-08-16 10:09:01 +0000 | [diff] [blame] | 289 | sqlite3 *db = pParse->db; |
danielk1977 | 2f886d1 | 2009-02-28 10:47:41 +0000 | [diff] [blame] | 290 | for(pTrig=sqlite3TriggerList(pParse, pTab); pTrig; pTrig=pTrig->pNext){ |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 291 | if( pTrig->pSchema==pTempSchema ){ |
dan | 432cc5b | 2009-09-26 17:51:48 +0000 | [diff] [blame] | 292 | zWhere = whereOrName(db, zWhere, pTrig->zName); |
danielk1977 | 19a8e7e | 2005-03-17 05:03:38 +0000 | [diff] [blame] | 293 | } |
| 294 | } |
| 295 | } |
dan | 39f1bcb | 2010-09-29 07:16:46 +0000 | [diff] [blame] | 296 | if( zWhere ){ |
| 297 | char *zNew = sqlite3MPrintf(pParse->db, "type='trigger' AND (%s)", zWhere); |
| 298 | sqlite3DbFree(pParse->db, zWhere); |
| 299 | zWhere = zNew; |
| 300 | } |
danielk1977 | 19a8e7e | 2005-03-17 05:03:38 +0000 | [diff] [blame] | 301 | return zWhere; |
| 302 | } |
| 303 | |
| 304 | /* |
| 305 | ** Generate code to drop and reload the internal representation of table |
| 306 | ** pTab from the database, including triggers and temporary triggers. |
| 307 | ** Argument zName is the name of the table in the database schema at |
| 308 | ** the time the generated code is executed. This can be different from |
| 309 | ** pTab->zName if this function is being called to code part of an |
| 310 | ** "ALTER TABLE RENAME TO" statement. |
| 311 | */ |
| 312 | static void reloadTableSchema(Parse *pParse, Table *pTab, const char *zName){ |
| 313 | Vdbe *v; |
| 314 | char *zWhere; |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 315 | int iDb; /* Index of database containing pTab */ |
danielk1977 | 19a8e7e | 2005-03-17 05:03:38 +0000 | [diff] [blame] | 316 | #ifndef SQLITE_OMIT_TRIGGER |
| 317 | Trigger *pTrig; |
| 318 | #endif |
| 319 | |
| 320 | v = sqlite3GetVdbe(pParse); |
drh | d3264c7 | 2009-04-15 13:39:47 +0000 | [diff] [blame] | 321 | if( NEVER(v==0) ) return; |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 322 | assert( sqlite3BtreeHoldsAllMutexes(pParse->db) ); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 323 | iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema); |
| 324 | assert( iDb>=0 ); |
danielk1977 | 19a8e7e | 2005-03-17 05:03:38 +0000 | [diff] [blame] | 325 | |
| 326 | #ifndef SQLITE_OMIT_TRIGGER |
| 327 | /* Drop any table triggers from the internal schema. */ |
danielk1977 | 2f886d1 | 2009-02-28 10:47:41 +0000 | [diff] [blame] | 328 | for(pTrig=sqlite3TriggerList(pParse, pTab); pTrig; pTrig=pTrig->pNext){ |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 329 | int iTrigDb = sqlite3SchemaToIndex(pParse->db, pTrig->pSchema); |
| 330 | assert( iTrigDb==iDb || iTrigDb==1 ); |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 331 | sqlite3VdbeAddOp4(v, OP_DropTrigger, iTrigDb, 0, 0, pTrig->zName, 0); |
danielk1977 | 19a8e7e | 2005-03-17 05:03:38 +0000 | [diff] [blame] | 332 | } |
| 333 | #endif |
| 334 | |
dan | 432cc5b | 2009-09-26 17:51:48 +0000 | [diff] [blame] | 335 | /* Drop the table and index from the internal schema. */ |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 336 | sqlite3VdbeAddOp4(v, OP_DropTable, iDb, 0, 0, pTab->zName, 0); |
danielk1977 | 19a8e7e | 2005-03-17 05:03:38 +0000 | [diff] [blame] | 337 | |
| 338 | /* Reload the table, index and permanent trigger schemas. */ |
danielk1977 | 1e53695 | 2007-08-16 10:09:01 +0000 | [diff] [blame] | 339 | zWhere = sqlite3MPrintf(pParse->db, "tbl_name=%Q", zName); |
danielk1977 | 19a8e7e | 2005-03-17 05:03:38 +0000 | [diff] [blame] | 340 | if( !zWhere ) return; |
drh | 5d9c9da | 2011-06-03 20:11:17 +0000 | [diff] [blame] | 341 | sqlite3VdbeAddParseSchemaOp(v, iDb, zWhere); |
danielk1977 | 19a8e7e | 2005-03-17 05:03:38 +0000 | [diff] [blame] | 342 | |
| 343 | #ifndef SQLITE_OMIT_TRIGGER |
| 344 | /* Now, if the table is not stored in the temp database, reload any temp |
| 345 | ** triggers. Don't use IN(...) in case SQLITE_OMIT_SUBQUERY is defined. |
| 346 | */ |
drh | d9cb6ac | 2005-10-20 07:28:17 +0000 | [diff] [blame] | 347 | if( (zWhere=whereTempTriggers(pParse, pTab))!=0 ){ |
drh | 5d9c9da | 2011-06-03 20:11:17 +0000 | [diff] [blame] | 348 | sqlite3VdbeAddParseSchemaOp(v, 1, zWhere); |
danielk1977 | 19a8e7e | 2005-03-17 05:03:38 +0000 | [diff] [blame] | 349 | } |
| 350 | #endif |
| 351 | } |
| 352 | |
| 353 | /* |
dan | be53500 | 2011-04-01 15:15:58 +0000 | [diff] [blame] | 354 | ** Parameter zName is the name of a table that is about to be altered |
| 355 | ** (either with ALTER TABLE ... RENAME TO or ALTER TABLE ... ADD COLUMN). |
| 356 | ** If the table is a system table, this function leaves an error message |
| 357 | ** in pParse->zErr (system tables may not be altered) and returns non-zero. |
| 358 | ** |
| 359 | ** Or, if zName is not a system table, zero is returned. |
| 360 | */ |
| 361 | static int isSystemTable(Parse *pParse, const char *zName){ |
drh | 59a386e | 2017-06-28 01:12:53 +0000 | [diff] [blame] | 362 | if( 0==sqlite3StrNICmp(zName, "sqlite_", 7) ){ |
dan | be53500 | 2011-04-01 15:15:58 +0000 | [diff] [blame] | 363 | sqlite3ErrorMsg(pParse, "table %s may not be altered", zName); |
| 364 | return 1; |
| 365 | } |
| 366 | return 0; |
| 367 | } |
| 368 | |
| 369 | /* |
drh | d0e4a6c | 2005-02-15 20:47:57 +0000 | [diff] [blame] | 370 | ** Generate code to implement the "ALTER TABLE xxx RENAME TO yyy" |
| 371 | ** command. |
| 372 | */ |
| 373 | void sqlite3AlterRenameTable( |
| 374 | Parse *pParse, /* Parser context. */ |
| 375 | SrcList *pSrc, /* The table to rename. */ |
| 376 | Token *pName /* The new table name. */ |
| 377 | ){ |
| 378 | int iDb; /* Database that contains the table */ |
| 379 | char *zDb; /* Name of database iDb */ |
| 380 | Table *pTab; /* Table being renamed */ |
| 381 | char *zName = 0; /* NULL-terminated version of pName */ |
drh | d0e4a6c | 2005-02-15 20:47:57 +0000 | [diff] [blame] | 382 | sqlite3 *db = pParse->db; /* Database connection */ |
drh | 4e5dd85 | 2007-05-15 03:56:49 +0000 | [diff] [blame] | 383 | int nTabName; /* Number of UTF-8 characters in zTabName */ |
| 384 | const char *zTabName; /* Original name of the table */ |
drh | d0e4a6c | 2005-02-15 20:47:57 +0000 | [diff] [blame] | 385 | Vdbe *v; |
| 386 | #ifndef SQLITE_OMIT_TRIGGER |
danielk1977 | 19a8e7e | 2005-03-17 05:03:38 +0000 | [diff] [blame] | 387 | char *zWhere = 0; /* Where clause to locate temp triggers */ |
drh | d0e4a6c | 2005-02-15 20:47:57 +0000 | [diff] [blame] | 388 | #endif |
danielk1977 | 595a523 | 2009-07-24 17:58:53 +0000 | [diff] [blame] | 389 | VTable *pVTab = 0; /* Non-zero if this is a v-tab with an xRename() */ |
drh | 8257aa8 | 2017-07-26 19:59:13 +0000 | [diff] [blame] | 390 | u32 savedDbFlags; /* Saved value of db->mDbFlags */ |
drh | 545f587 | 2010-04-24 14:02:59 +0000 | [diff] [blame] | 391 | |
drh | 8257aa8 | 2017-07-26 19:59:13 +0000 | [diff] [blame] | 392 | savedDbFlags = db->mDbFlags; |
drh | 56d56f7 | 2009-04-16 16:30:17 +0000 | [diff] [blame] | 393 | if( NEVER(db->mallocFailed) ) goto exit_rename_table; |
drh | d0e4a6c | 2005-02-15 20:47:57 +0000 | [diff] [blame] | 394 | assert( pSrc->nSrc==1 ); |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 395 | assert( sqlite3BtreeHoldsAllMutexes(pParse->db) ); |
drh | d0e4a6c | 2005-02-15 20:47:57 +0000 | [diff] [blame] | 396 | |
dan | 41fb5cd | 2012-10-04 19:33:00 +0000 | [diff] [blame] | 397 | pTab = sqlite3LocateTableItem(pParse, 0, &pSrc->a[0]); |
drh | d0e4a6c | 2005-02-15 20:47:57 +0000 | [diff] [blame] | 398 | if( !pTab ) goto exit_rename_table; |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 399 | iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema); |
drh | 69c3382 | 2016-08-18 14:33:11 +0000 | [diff] [blame] | 400 | zDb = db->aDb[iDb].zDbSName; |
drh | 8257aa8 | 2017-07-26 19:59:13 +0000 | [diff] [blame] | 401 | db->mDbFlags |= DBFLAG_PreferBuiltin; |
drh | d0e4a6c | 2005-02-15 20:47:57 +0000 | [diff] [blame] | 402 | |
| 403 | /* Get a NULL terminated version of the new table name. */ |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 404 | zName = sqlite3NameFromToken(db, pName); |
drh | d0e4a6c | 2005-02-15 20:47:57 +0000 | [diff] [blame] | 405 | if( !zName ) goto exit_rename_table; |
| 406 | |
| 407 | /* Check that a table or index named 'zName' does not already exist |
| 408 | ** in database iDb. If so, this is an error. |
| 409 | */ |
| 410 | if( sqlite3FindTable(db, zName, zDb) || sqlite3FindIndex(db, zName, zDb) ){ |
| 411 | sqlite3ErrorMsg(pParse, |
| 412 | "there is already another table or index with this name: %s", zName); |
| 413 | goto exit_rename_table; |
| 414 | } |
| 415 | |
| 416 | /* Make sure it is not a system table being altered, or a reserved name |
| 417 | ** that the table is being renamed to. |
| 418 | */ |
dan | be53500 | 2011-04-01 15:15:58 +0000 | [diff] [blame] | 419 | if( SQLITE_OK!=isSystemTable(pParse, pTab->zName) ){ |
drh | d0e4a6c | 2005-02-15 20:47:57 +0000 | [diff] [blame] | 420 | goto exit_rename_table; |
| 421 | } |
dan | be53500 | 2011-04-01 15:15:58 +0000 | [diff] [blame] | 422 | if( SQLITE_OK!=sqlite3CheckObjectName(pParse, zName) ){ goto |
| 423 | exit_rename_table; |
drh | d0e4a6c | 2005-02-15 20:47:57 +0000 | [diff] [blame] | 424 | } |
| 425 | |
danielk1977 | 61116ae | 2007-12-13 08:15:30 +0000 | [diff] [blame] | 426 | #ifndef SQLITE_OMIT_VIEW |
| 427 | if( pTab->pSelect ){ |
| 428 | sqlite3ErrorMsg(pParse, "view %s may not be altered", pTab->zName); |
| 429 | goto exit_rename_table; |
| 430 | } |
| 431 | #endif |
| 432 | |
drh | d0e4a6c | 2005-02-15 20:47:57 +0000 | [diff] [blame] | 433 | #ifndef SQLITE_OMIT_AUTHORIZATION |
| 434 | /* Invoke the authorization callback. */ |
| 435 | if( sqlite3AuthCheck(pParse, SQLITE_ALTER_TABLE, zDb, pTab->zName, 0) ){ |
| 436 | goto exit_rename_table; |
| 437 | } |
| 438 | #endif |
| 439 | |
danielk1977 | 182c4ba | 2007-06-27 15:53:34 +0000 | [diff] [blame] | 440 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
danielk1977 | 5c55886 | 2007-06-27 17:09:24 +0000 | [diff] [blame] | 441 | if( sqlite3ViewGetColumnNames(pParse, pTab) ){ |
| 442 | goto exit_rename_table; |
| 443 | } |
danielk1977 | 595a523 | 2009-07-24 17:58:53 +0000 | [diff] [blame] | 444 | if( IsVirtual(pTab) ){ |
| 445 | pVTab = sqlite3GetVTable(db, pTab); |
| 446 | if( pVTab->pVtab->pModule->xRename==0 ){ |
| 447 | pVTab = 0; |
| 448 | } |
danielk1977 | 182c4ba | 2007-06-27 15:53:34 +0000 | [diff] [blame] | 449 | } |
| 450 | #endif |
| 451 | |
drh | b22f7c8 | 2014-02-06 23:56:27 +0000 | [diff] [blame] | 452 | /* Begin a transaction for database iDb. |
drh | d0e4a6c | 2005-02-15 20:47:57 +0000 | [diff] [blame] | 453 | ** Then modify the schema cookie (since the ALTER TABLE modifies the |
danielk1977 | 182c4ba | 2007-06-27 15:53:34 +0000 | [diff] [blame] | 454 | ** schema). Open a statement transaction if the table is a virtual |
| 455 | ** table. |
drh | d0e4a6c | 2005-02-15 20:47:57 +0000 | [diff] [blame] | 456 | */ |
| 457 | v = sqlite3GetVdbe(pParse); |
| 458 | if( v==0 ){ |
| 459 | goto exit_rename_table; |
| 460 | } |
danielk1977 | 595a523 | 2009-07-24 17:58:53 +0000 | [diff] [blame] | 461 | sqlite3BeginWriteOperation(pParse, pVTab!=0, iDb); |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 462 | sqlite3ChangeCookie(pParse, iDb); |
drh | d0e4a6c | 2005-02-15 20:47:57 +0000 | [diff] [blame] | 463 | |
danielk1977 | 182c4ba | 2007-06-27 15:53:34 +0000 | [diff] [blame] | 464 | /* If this is a virtual table, invoke the xRename() function if |
| 465 | ** one is defined. The xRename() callback will modify the names |
| 466 | ** of any resources used by the v-table implementation (including other |
| 467 | ** SQLite tables) that are identified by the name of the virtual table. |
| 468 | */ |
| 469 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
danielk1977 | 595a523 | 2009-07-24 17:58:53 +0000 | [diff] [blame] | 470 | if( pVTab ){ |
danielk1977 | a29f18c | 2008-01-04 11:01:03 +0000 | [diff] [blame] | 471 | int i = ++pParse->nMem; |
drh | 076e85f | 2015-09-03 13:46:12 +0000 | [diff] [blame] | 472 | sqlite3VdbeLoadString(v, i, zName); |
danielk1977 | 595a523 | 2009-07-24 17:58:53 +0000 | [diff] [blame] | 473 | sqlite3VdbeAddOp4(v, OP_VRename, i, 0, 0,(const char*)pVTab, P4_VTAB); |
dan | e0af83a | 2009-09-08 19:15:01 +0000 | [diff] [blame] | 474 | sqlite3MayAbort(pParse); |
danielk1977 | 182c4ba | 2007-06-27 15:53:34 +0000 | [diff] [blame] | 475 | } |
| 476 | #endif |
| 477 | |
drh | 4e5dd85 | 2007-05-15 03:56:49 +0000 | [diff] [blame] | 478 | /* figure out how many UTF-8 characters are in zName */ |
| 479 | zTabName = pTab->zName; |
drh | 9a087a9 | 2007-05-15 14:34:32 +0000 | [diff] [blame] | 480 | nTabName = sqlite3Utf8CharLen(zTabName, -1); |
drh | 4e5dd85 | 2007-05-15 03:56:49 +0000 | [diff] [blame] | 481 | |
dan | 856ef1a | 2009-09-29 06:33:23 +0000 | [diff] [blame] | 482 | #if !defined(SQLITE_OMIT_FOREIGN_KEY) && !defined(SQLITE_OMIT_TRIGGER) |
dan | 432cc5b | 2009-09-26 17:51:48 +0000 | [diff] [blame] | 483 | if( db->flags&SQLITE_ForeignKeys ){ |
| 484 | /* If foreign-key support is enabled, rewrite the CREATE TABLE |
| 485 | ** statements corresponding to all child tables of foreign key constraints |
| 486 | ** for which the renamed table is the parent table. */ |
| 487 | if( (zWhere=whereForeignKeys(pParse, pTab))!=0 ){ |
| 488 | sqlite3NestedParse(pParse, |
drh | 9a6ffc8 | 2010-02-15 18:03:20 +0000 | [diff] [blame] | 489 | "UPDATE \"%w\".%s SET " |
dan | 432cc5b | 2009-09-26 17:51:48 +0000 | [diff] [blame] | 490 | "sql = sqlite_rename_parent(sql, %Q, %Q) " |
drh | e0a04a3 | 2016-12-16 01:00:21 +0000 | [diff] [blame] | 491 | "WHERE %s;", zDb, MASTER_NAME, zTabName, zName, zWhere); |
dan | 432cc5b | 2009-09-26 17:51:48 +0000 | [diff] [blame] | 492 | sqlite3DbFree(db, zWhere); |
| 493 | } |
| 494 | } |
| 495 | #endif |
| 496 | |
drh | d0e4a6c | 2005-02-15 20:47:57 +0000 | [diff] [blame] | 497 | /* Modify the sqlite_master table to use the new table name. */ |
| 498 | sqlite3NestedParse(pParse, |
| 499 | "UPDATE %Q.%s SET " |
| 500 | #ifdef SQLITE_OMIT_TRIGGER |
| 501 | "sql = sqlite_rename_table(sql, %Q), " |
| 502 | #else |
| 503 | "sql = CASE " |
| 504 | "WHEN type = 'trigger' THEN sqlite_rename_trigger(sql, %Q)" |
| 505 | "ELSE sqlite_rename_table(sql, %Q) END, " |
| 506 | #endif |
| 507 | "tbl_name = %Q, " |
| 508 | "name = CASE " |
| 509 | "WHEN type='table' THEN %Q " |
| 510 | "WHEN name LIKE 'sqlite_autoindex%%' AND type='index' THEN " |
drh | a21a929 | 2007-10-20 20:58:57 +0000 | [diff] [blame] | 511 | "'sqlite_autoindex_' || %Q || substr(name,%d+18) " |
drh | d0e4a6c | 2005-02-15 20:47:57 +0000 | [diff] [blame] | 512 | "ELSE name END " |
drh | 0152268 | 2012-02-01 01:13:10 +0000 | [diff] [blame] | 513 | "WHERE tbl_name=%Q COLLATE nocase AND " |
drh | d0e4a6c | 2005-02-15 20:47:57 +0000 | [diff] [blame] | 514 | "(type='table' OR type='index' OR type='trigger');", |
drh | e0a04a3 | 2016-12-16 01:00:21 +0000 | [diff] [blame] | 515 | zDb, MASTER_NAME, zName, zName, zName, |
drh | d0e4a6c | 2005-02-15 20:47:57 +0000 | [diff] [blame] | 516 | #ifndef SQLITE_OMIT_TRIGGER |
danielk1977 | 19a8e7e | 2005-03-17 05:03:38 +0000 | [diff] [blame] | 517 | zName, |
drh | d0e4a6c | 2005-02-15 20:47:57 +0000 | [diff] [blame] | 518 | #endif |
drh | 4e5dd85 | 2007-05-15 03:56:49 +0000 | [diff] [blame] | 519 | zName, nTabName, zTabName |
drh | d0e4a6c | 2005-02-15 20:47:57 +0000 | [diff] [blame] | 520 | ); |
| 521 | |
| 522 | #ifndef SQLITE_OMIT_AUTOINCREMENT |
| 523 | /* If the sqlite_sequence table exists in this database, then update |
| 524 | ** it with the new table name. |
| 525 | */ |
| 526 | if( sqlite3FindTable(db, "sqlite_sequence", zDb) ){ |
| 527 | sqlite3NestedParse(pParse, |
drh | 8e5b5f8 | 2008-02-09 14:30:29 +0000 | [diff] [blame] | 528 | "UPDATE \"%w\".sqlite_sequence set name = %Q WHERE name = %Q", |
drh | d0e4a6c | 2005-02-15 20:47:57 +0000 | [diff] [blame] | 529 | zDb, zName, pTab->zName); |
| 530 | } |
| 531 | #endif |
| 532 | |
| 533 | #ifndef SQLITE_OMIT_TRIGGER |
| 534 | /* If there are TEMP triggers on this table, modify the sqlite_temp_master |
| 535 | ** table. Don't do this if the table being ALTERed is itself located in |
| 536 | ** the temp database. |
| 537 | */ |
drh | d9cb6ac | 2005-10-20 07:28:17 +0000 | [diff] [blame] | 538 | if( (zWhere=whereTempTriggers(pParse, pTab))!=0 ){ |
danielk1977 | 19a8e7e | 2005-03-17 05:03:38 +0000 | [diff] [blame] | 539 | sqlite3NestedParse(pParse, |
| 540 | "UPDATE sqlite_temp_master SET " |
| 541 | "sql = sqlite_rename_trigger(sql, %Q), " |
| 542 | "tbl_name = %Q " |
| 543 | "WHERE %s;", zName, zName, zWhere); |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 544 | sqlite3DbFree(db, zWhere); |
drh | d0e4a6c | 2005-02-15 20:47:57 +0000 | [diff] [blame] | 545 | } |
| 546 | #endif |
| 547 | |
dan | 856ef1a | 2009-09-29 06:33:23 +0000 | [diff] [blame] | 548 | #if !defined(SQLITE_OMIT_FOREIGN_KEY) && !defined(SQLITE_OMIT_TRIGGER) |
dan | 432cc5b | 2009-09-26 17:51:48 +0000 | [diff] [blame] | 549 | if( db->flags&SQLITE_ForeignKeys ){ |
| 550 | FKey *p; |
| 551 | for(p=sqlite3FkReferences(pTab); p; p=p->pNextTo){ |
| 552 | Table *pFrom = p->pFrom; |
| 553 | if( pFrom!=pTab ){ |
| 554 | reloadTableSchema(pParse, p->pFrom, pFrom->zName); |
| 555 | } |
| 556 | } |
| 557 | } |
| 558 | #endif |
| 559 | |
danielk1977 | 19a8e7e | 2005-03-17 05:03:38 +0000 | [diff] [blame] | 560 | /* Drop and reload the internal table schema. */ |
| 561 | reloadTableSchema(pParse, pTab, zName); |
drh | d0e4a6c | 2005-02-15 20:47:57 +0000 | [diff] [blame] | 562 | |
| 563 | exit_rename_table: |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 564 | sqlite3SrcListDelete(db, pSrc); |
| 565 | sqlite3DbFree(db, zName); |
drh | 8257aa8 | 2017-07-26 19:59:13 +0000 | [diff] [blame] | 566 | db->mDbFlags = savedDbFlags; |
drh | d0e4a6c | 2005-02-15 20:47:57 +0000 | [diff] [blame] | 567 | } |
danielk1977 | 19a8e7e | 2005-03-17 05:03:38 +0000 | [diff] [blame] | 568 | |
drh | d300171 | 2009-05-12 17:46:53 +0000 | [diff] [blame] | 569 | /* |
danielk1977 | 19a8e7e | 2005-03-17 05:03:38 +0000 | [diff] [blame] | 570 | ** This function is called after an "ALTER TABLE ... ADD" statement |
| 571 | ** has been parsed. Argument pColDef contains the text of the new |
| 572 | ** column definition. |
| 573 | ** |
| 574 | ** The Table structure pParse->pNewTable was extended to include |
| 575 | ** the new column during parsing. |
| 576 | */ |
| 577 | void sqlite3AlterFinishAddColumn(Parse *pParse, Token *pColDef){ |
| 578 | Table *pNew; /* Copy of pParse->pNewTable */ |
| 579 | Table *pTab; /* Table being altered */ |
| 580 | int iDb; /* Database number */ |
| 581 | const char *zDb; /* Database name */ |
| 582 | const char *zTab; /* Table name */ |
| 583 | char *zCol; /* Null-terminated column definition */ |
| 584 | Column *pCol; /* The new column */ |
| 585 | Expr *pDflt; /* Default value for the new column */ |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 586 | sqlite3 *db; /* The database connection; */ |
drh | bbde018 | 2016-02-09 16:09:22 +0000 | [diff] [blame] | 587 | Vdbe *v = pParse->pVdbe; /* The prepared statement under construction */ |
drh | 8639621 | 2016-07-14 19:13:11 +0000 | [diff] [blame] | 588 | int r1; /* Temporary registers */ |
danielk1977 | 19a8e7e | 2005-03-17 05:03:38 +0000 | [diff] [blame] | 589 | |
danielk1977 | f150c9d | 2008-10-30 17:21:12 +0000 | [diff] [blame] | 590 | db = pParse->db; |
| 591 | if( pParse->nErr || db->mallocFailed ) return; |
drh | bbde018 | 2016-02-09 16:09:22 +0000 | [diff] [blame] | 592 | assert( v!=0 ); |
danielk1977 | 19a8e7e | 2005-03-17 05:03:38 +0000 | [diff] [blame] | 593 | pNew = pParse->pNewTable; |
| 594 | assert( pNew ); |
| 595 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 596 | assert( sqlite3BtreeHoldsAllMutexes(db) ); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 597 | iDb = sqlite3SchemaToIndex(db, pNew->pSchema); |
drh | 69c3382 | 2016-08-18 14:33:11 +0000 | [diff] [blame] | 598 | zDb = db->aDb[iDb].zDbSName; |
drh | 0388123 | 2009-02-13 03:43:31 +0000 | [diff] [blame] | 599 | zTab = &pNew->zName[16]; /* Skip the "sqlite_altertab_" prefix on the name */ |
danielk1977 | 19a8e7e | 2005-03-17 05:03:38 +0000 | [diff] [blame] | 600 | pCol = &pNew->aCol[pNew->nCol-1]; |
| 601 | pDflt = pCol->pDflt; |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 602 | pTab = sqlite3FindTable(db, zTab, zDb); |
danielk1977 | 19a8e7e | 2005-03-17 05:03:38 +0000 | [diff] [blame] | 603 | assert( pTab ); |
| 604 | |
drh | 81f2ccd | 2006-01-31 14:28:44 +0000 | [diff] [blame] | 605 | #ifndef SQLITE_OMIT_AUTHORIZATION |
| 606 | /* Invoke the authorization callback. */ |
| 607 | if( sqlite3AuthCheck(pParse, SQLITE_ALTER_TABLE, zDb, pTab->zName, 0) ){ |
| 608 | return; |
| 609 | } |
| 610 | #endif |
| 611 | |
danielk1977 | 19a8e7e | 2005-03-17 05:03:38 +0000 | [diff] [blame] | 612 | /* If the default value for the new column was specified with a |
| 613 | ** literal NULL, then set pDflt to 0. This simplifies checking |
| 614 | ** for an SQL NULL default below. |
| 615 | */ |
drh | 94fa9c4 | 2016-02-27 21:16:04 +0000 | [diff] [blame] | 616 | assert( pDflt==0 || pDflt->op==TK_SPAN ); |
| 617 | if( pDflt && pDflt->pLeft->op==TK_NULL ){ |
danielk1977 | 19a8e7e | 2005-03-17 05:03:38 +0000 | [diff] [blame] | 618 | pDflt = 0; |
| 619 | } |
| 620 | |
| 621 | /* Check that the new column is not specified as PRIMARY KEY or UNIQUE. |
| 622 | ** If there is a NOT NULL constraint, then the default value for the |
| 623 | ** column must not be NULL. |
| 624 | */ |
drh | a371ace | 2012-09-13 14:22:47 +0000 | [diff] [blame] | 625 | if( pCol->colFlags & COLFLAG_PRIMKEY ){ |
danielk1977 | 19a8e7e | 2005-03-17 05:03:38 +0000 | [diff] [blame] | 626 | sqlite3ErrorMsg(pParse, "Cannot add a PRIMARY KEY column"); |
| 627 | return; |
| 628 | } |
| 629 | if( pNew->pIndex ){ |
| 630 | sqlite3ErrorMsg(pParse, "Cannot add a UNIQUE column"); |
| 631 | return; |
| 632 | } |
dan | 53c3fa8 | 2009-09-25 11:26:54 +0000 | [diff] [blame] | 633 | if( (db->flags&SQLITE_ForeignKeys) && pNew->pFKey && pDflt ){ |
| 634 | sqlite3ErrorMsg(pParse, |
| 635 | "Cannot add a REFERENCES column with non-NULL default value"); |
| 636 | return; |
| 637 | } |
danielk1977 | 19a8e7e | 2005-03-17 05:03:38 +0000 | [diff] [blame] | 638 | if( pCol->notNull && !pDflt ){ |
| 639 | sqlite3ErrorMsg(pParse, |
| 640 | "Cannot add a NOT NULL column with default value NULL"); |
| 641 | return; |
| 642 | } |
| 643 | |
| 644 | /* Ensure the default expression is something that sqlite3ValueFromExpr() |
| 645 | ** can handle (i.e. not CURRENT_TIME etc.) |
| 646 | */ |
| 647 | if( pDflt ){ |
dan | 7a41923 | 2013-08-06 20:01:43 +0000 | [diff] [blame] | 648 | sqlite3_value *pVal = 0; |
drh | 96f4ad2 | 2015-03-12 21:02:36 +0000 | [diff] [blame] | 649 | int rc; |
drh | 05883a3 | 2015-06-02 15:32:08 +0000 | [diff] [blame] | 650 | rc = sqlite3ValueFromExpr(db, pDflt, SQLITE_UTF8, SQLITE_AFF_BLOB, &pVal); |
drh | 96f4ad2 | 2015-03-12 21:02:36 +0000 | [diff] [blame] | 651 | assert( rc==SQLITE_OK || rc==SQLITE_NOMEM ); |
| 652 | if( rc!=SQLITE_OK ){ |
pdr | bb3da06 | 2016-02-06 14:14:43 +0000 | [diff] [blame] | 653 | assert( db->mallocFailed == 1 ); |
danielk1977 | 19a8e7e | 2005-03-17 05:03:38 +0000 | [diff] [blame] | 654 | return; |
| 655 | } |
| 656 | if( !pVal ){ |
| 657 | sqlite3ErrorMsg(pParse, "Cannot add a column with non-constant default"); |
| 658 | return; |
| 659 | } |
| 660 | sqlite3ValueFree(pVal); |
| 661 | } |
| 662 | |
| 663 | /* Modify the CREATE TABLE statement. */ |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 664 | zCol = sqlite3DbStrNDup(db, (char*)pColDef->z, pColDef->n); |
danielk1977 | 19a8e7e | 2005-03-17 05:03:38 +0000 | [diff] [blame] | 665 | if( zCol ){ |
| 666 | char *zEnd = &zCol[pColDef->n-1]; |
drh | 8257aa8 | 2017-07-26 19:59:13 +0000 | [diff] [blame] | 667 | u32 savedDbFlags = db->mDbFlags; |
drh | 56d56f7 | 2009-04-16 16:30:17 +0000 | [diff] [blame] | 668 | while( zEnd>zCol && (*zEnd==';' || sqlite3Isspace(*zEnd)) ){ |
danielk1977 | 19a8e7e | 2005-03-17 05:03:38 +0000 | [diff] [blame] | 669 | *zEnd-- = '\0'; |
| 670 | } |
drh | 8257aa8 | 2017-07-26 19:59:13 +0000 | [diff] [blame] | 671 | db->mDbFlags |= DBFLAG_PreferBuiltin; |
danielk1977 | 19a8e7e | 2005-03-17 05:03:38 +0000 | [diff] [blame] | 672 | sqlite3NestedParse(pParse, |
drh | 8e5b5f8 | 2008-02-09 14:30:29 +0000 | [diff] [blame] | 673 | "UPDATE \"%w\".%s SET " |
drh | a21a929 | 2007-10-20 20:58:57 +0000 | [diff] [blame] | 674 | "sql = substr(sql,1,%d) || ', ' || %Q || substr(sql,%d) " |
danielk1977 | 19a8e7e | 2005-03-17 05:03:38 +0000 | [diff] [blame] | 675 | "WHERE type = 'table' AND name = %Q", |
drh | e0a04a3 | 2016-12-16 01:00:21 +0000 | [diff] [blame] | 676 | zDb, MASTER_NAME, pNew->addColOffset, zCol, pNew->addColOffset+1, |
danielk1977 | 19a8e7e | 2005-03-17 05:03:38 +0000 | [diff] [blame] | 677 | zTab |
| 678 | ); |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 679 | sqlite3DbFree(db, zCol); |
drh | 8257aa8 | 2017-07-26 19:59:13 +0000 | [diff] [blame] | 680 | db->mDbFlags = savedDbFlags; |
danielk1977 | 19a8e7e | 2005-03-17 05:03:38 +0000 | [diff] [blame] | 681 | } |
| 682 | |
drh | 8639621 | 2016-07-14 19:13:11 +0000 | [diff] [blame] | 683 | /* Make sure the schema version is at least 3. But do not upgrade |
| 684 | ** from less than 3 to 4, as that will corrupt any preexisting DESC |
| 685 | ** index. |
danielk1977 | 19a8e7e | 2005-03-17 05:03:38 +0000 | [diff] [blame] | 686 | */ |
drh | 8639621 | 2016-07-14 19:13:11 +0000 | [diff] [blame] | 687 | r1 = sqlite3GetTempReg(pParse); |
| 688 | sqlite3VdbeAddOp3(v, OP_ReadCookie, iDb, r1, BTREE_FILE_FORMAT); |
| 689 | sqlite3VdbeUsesBtree(v, iDb); |
| 690 | sqlite3VdbeAddOp2(v, OP_AddImm, r1, -2); |
| 691 | sqlite3VdbeAddOp2(v, OP_IfPos, r1, sqlite3VdbeCurrentAddr(v)+2); |
| 692 | VdbeCoverage(v); |
| 693 | sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, BTREE_FILE_FORMAT, 3); |
| 694 | sqlite3ReleaseTempReg(pParse, r1); |
danielk1977 | 19a8e7e | 2005-03-17 05:03:38 +0000 | [diff] [blame] | 695 | |
| 696 | /* Reload the schema of the modified table. */ |
| 697 | reloadTableSchema(pParse, pTab, pTab->zName); |
| 698 | } |
| 699 | |
drh | fdd6e85 | 2005-12-16 01:06:16 +0000 | [diff] [blame] | 700 | /* |
danielk1977 | 19a8e7e | 2005-03-17 05:03:38 +0000 | [diff] [blame] | 701 | ** This function is called by the parser after the table-name in |
| 702 | ** an "ALTER TABLE <table-name> ADD" statement is parsed. Argument |
| 703 | ** pSrc is the full-name of the table being altered. |
| 704 | ** |
| 705 | ** This routine makes a (partial) copy of the Table structure |
| 706 | ** for the table being altered and sets Parse.pNewTable to point |
| 707 | ** to it. Routines called by the parser as the column definition |
| 708 | ** is parsed (i.e. sqlite3AddColumn()) add the new Column data to |
| 709 | ** the copy. The copy of the Table structure is deleted by tokenize.c |
| 710 | ** after parsing is finished. |
| 711 | ** |
| 712 | ** Routine sqlite3AlterFinishAddColumn() will be called to complete |
| 713 | ** coding the "ALTER TABLE ... ADD" statement. |
| 714 | */ |
| 715 | void sqlite3AlterBeginAddColumn(Parse *pParse, SrcList *pSrc){ |
| 716 | Table *pNew; |
| 717 | Table *pTab; |
| 718 | Vdbe *v; |
| 719 | int iDb; |
| 720 | int i; |
| 721 | int nAlloc; |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 722 | sqlite3 *db = pParse->db; |
danielk1977 | 19a8e7e | 2005-03-17 05:03:38 +0000 | [diff] [blame] | 723 | |
| 724 | /* Look up the table being altered. */ |
drh | 0bbaa1b | 2005-08-19 19:14:12 +0000 | [diff] [blame] | 725 | assert( pParse->pNewTable==0 ); |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 726 | assert( sqlite3BtreeHoldsAllMutexes(db) ); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 727 | if( db->mallocFailed ) goto exit_begin_add_column; |
dan | 41fb5cd | 2012-10-04 19:33:00 +0000 | [diff] [blame] | 728 | pTab = sqlite3LocateTableItem(pParse, 0, &pSrc->a[0]); |
danielk1977 | 19a8e7e | 2005-03-17 05:03:38 +0000 | [diff] [blame] | 729 | if( !pTab ) goto exit_begin_add_column; |
| 730 | |
danielk1977 | 5ee9d69 | 2006-06-21 12:36:25 +0000 | [diff] [blame] | 731 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
| 732 | if( IsVirtual(pTab) ){ |
| 733 | sqlite3ErrorMsg(pParse, "virtual tables may not be altered"); |
| 734 | goto exit_begin_add_column; |
| 735 | } |
| 736 | #endif |
| 737 | |
danielk1977 | 19a8e7e | 2005-03-17 05:03:38 +0000 | [diff] [blame] | 738 | /* Make sure this is not an attempt to ALTER a view. */ |
| 739 | if( pTab->pSelect ){ |
| 740 | sqlite3ErrorMsg(pParse, "Cannot add a column to a view"); |
| 741 | goto exit_begin_add_column; |
| 742 | } |
dan | be53500 | 2011-04-01 15:15:58 +0000 | [diff] [blame] | 743 | if( SQLITE_OK!=isSystemTable(pParse, pTab->zName) ){ |
| 744 | goto exit_begin_add_column; |
| 745 | } |
danielk1977 | 19a8e7e | 2005-03-17 05:03:38 +0000 | [diff] [blame] | 746 | |
| 747 | assert( pTab->addColOffset>0 ); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 748 | iDb = sqlite3SchemaToIndex(db, pTab->pSchema); |
danielk1977 | 19a8e7e | 2005-03-17 05:03:38 +0000 | [diff] [blame] | 749 | |
| 750 | /* Put a copy of the Table struct in Parse.pNewTable for the |
drh | 0388123 | 2009-02-13 03:43:31 +0000 | [diff] [blame] | 751 | ** sqlite3AddColumn() function and friends to modify. But modify |
| 752 | ** the name by adding an "sqlite_altertab_" prefix. By adding this |
| 753 | ** prefix, we insure that the name will not collide with an existing |
| 754 | ** table because user table are not allowed to have the "sqlite_" |
| 755 | ** prefix on their name. |
danielk1977 | 19a8e7e | 2005-03-17 05:03:38 +0000 | [diff] [blame] | 756 | */ |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 757 | pNew = (Table*)sqlite3DbMallocZero(db, sizeof(Table)); |
danielk1977 | 19a8e7e | 2005-03-17 05:03:38 +0000 | [diff] [blame] | 758 | if( !pNew ) goto exit_begin_add_column; |
| 759 | pParse->pNewTable = pNew; |
drh | 79df778 | 2016-12-14 14:07:35 +0000 | [diff] [blame] | 760 | pNew->nTabRef = 1; |
danielk1977 | 19a8e7e | 2005-03-17 05:03:38 +0000 | [diff] [blame] | 761 | pNew->nCol = pTab->nCol; |
danielk1977 | b3a2cce | 2005-03-27 01:56:30 +0000 | [diff] [blame] | 762 | assert( pNew->nCol>0 ); |
| 763 | nAlloc = (((pNew->nCol-1)/8)*8)+8; |
| 764 | assert( nAlloc>=pNew->nCol && nAlloc%8==0 && nAlloc-pNew->nCol<8 ); |
danielk1977 | 26783a5 | 2007-08-29 14:06:22 +0000 | [diff] [blame] | 765 | pNew->aCol = (Column*)sqlite3DbMallocZero(db, sizeof(Column)*nAlloc); |
drh | 0388123 | 2009-02-13 03:43:31 +0000 | [diff] [blame] | 766 | pNew->zName = sqlite3MPrintf(db, "sqlite_altertab_%s", pTab->zName); |
danielk1977 | 19a8e7e | 2005-03-17 05:03:38 +0000 | [diff] [blame] | 767 | if( !pNew->aCol || !pNew->zName ){ |
drh | 4df86af | 2016-02-04 11:48:00 +0000 | [diff] [blame] | 768 | assert( db->mallocFailed ); |
danielk1977 | 19a8e7e | 2005-03-17 05:03:38 +0000 | [diff] [blame] | 769 | goto exit_begin_add_column; |
| 770 | } |
| 771 | memcpy(pNew->aCol, pTab->aCol, sizeof(Column)*pNew->nCol); |
| 772 | for(i=0; i<pNew->nCol; i++){ |
| 773 | Column *pCol = &pNew->aCol[i]; |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 774 | pCol->zName = sqlite3DbStrDup(db, pCol->zName); |
drh | ff22e18 | 2006-02-09 02:56:02 +0000 | [diff] [blame] | 775 | pCol->zColl = 0; |
danielk1977 | 19a8e7e | 2005-03-17 05:03:38 +0000 | [diff] [blame] | 776 | pCol->pDflt = 0; |
| 777 | } |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 778 | pNew->pSchema = db->aDb[iDb].pSchema; |
danielk1977 | 19a8e7e | 2005-03-17 05:03:38 +0000 | [diff] [blame] | 779 | pNew->addColOffset = pTab->addColOffset; |
drh | 79df778 | 2016-12-14 14:07:35 +0000 | [diff] [blame] | 780 | pNew->nTabRef = 1; |
danielk1977 | 19a8e7e | 2005-03-17 05:03:38 +0000 | [diff] [blame] | 781 | |
| 782 | /* Begin a transaction and increment the schema cookie. */ |
| 783 | sqlite3BeginWriteOperation(pParse, 0, iDb); |
| 784 | v = sqlite3GetVdbe(pParse); |
| 785 | if( !v ) goto exit_begin_add_column; |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 786 | sqlite3ChangeCookie(pParse, iDb); |
danielk1977 | 19a8e7e | 2005-03-17 05:03:38 +0000 | [diff] [blame] | 787 | |
| 788 | exit_begin_add_column: |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 789 | sqlite3SrcListDelete(db, pSrc); |
danielk1977 | 19a8e7e | 2005-03-17 05:03:38 +0000 | [diff] [blame] | 790 | return; |
| 791 | } |
dan | cf8f289 | 2018-08-09 20:47:01 +0000 | [diff] [blame] | 792 | |
drh | 4a2c747 | 2018-08-13 15:09:48 +0000 | [diff] [blame] | 793 | /* |
| 794 | ** Handles the following parser reduction: |
| 795 | ** |
| 796 | ** cmd ::= ALTER TABLE pSrc RENAME COLUMN pOld TO pNew |
| 797 | */ |
dan | cf8f289 | 2018-08-09 20:47:01 +0000 | [diff] [blame] | 798 | void sqlite3AlterRenameColumn( |
drh | 4a2c747 | 2018-08-13 15:09:48 +0000 | [diff] [blame] | 799 | Parse *pParse, /* Parsing context */ |
| 800 | SrcList *pSrc, /* Table being altered. pSrc->nSrc==1 */ |
| 801 | Token *pOld, /* Name of column being changed */ |
| 802 | Token *pNew /* New column name */ |
dan | cf8f289 | 2018-08-09 20:47:01 +0000 | [diff] [blame] | 803 | ){ |
drh | 4a2c747 | 2018-08-13 15:09:48 +0000 | [diff] [blame] | 804 | sqlite3 *db = pParse->db; /* Database connection */ |
dan | cf8f289 | 2018-08-09 20:47:01 +0000 | [diff] [blame] | 805 | Table *pTab; /* Table being updated */ |
| 806 | int iCol; /* Index of column being renamed */ |
drh | 4a2c747 | 2018-08-13 15:09:48 +0000 | [diff] [blame] | 807 | char *zOld = 0; /* Old column name */ |
| 808 | char *zNew = 0; /* New column name */ |
| 809 | const char *zDb; /* Name of schema containing the table */ |
| 810 | int iSchema; /* Index of the schema */ |
| 811 | int bQuote; /* True to quote the new name */ |
dan | cf8f289 | 2018-08-09 20:47:01 +0000 | [diff] [blame] | 812 | |
drh | 4a2c747 | 2018-08-13 15:09:48 +0000 | [diff] [blame] | 813 | /* Locate the table to be altered */ |
dan | cf8f289 | 2018-08-09 20:47:01 +0000 | [diff] [blame] | 814 | pTab = sqlite3LocateTableItem(pParse, 0, &pSrc->a[0]); |
| 815 | if( !pTab ) goto exit_rename_column; |
drh | 4a2c747 | 2018-08-13 15:09:48 +0000 | [diff] [blame] | 816 | |
| 817 | /* Cannot alter a system table */ |
dan | 872165f | 2018-08-11 17:34:38 +0000 | [diff] [blame] | 818 | if( SQLITE_OK!=isSystemTable(pParse, pTab->zName) ) goto exit_rename_column; |
drh | 4a2c747 | 2018-08-13 15:09:48 +0000 | [diff] [blame] | 819 | |
| 820 | /* Which schema holds the table to be altered */ |
dan | cf8f289 | 2018-08-09 20:47:01 +0000 | [diff] [blame] | 821 | iSchema = sqlite3SchemaToIndex(db, pTab->pSchema); |
| 822 | assert( iSchema>=0 ); |
| 823 | zDb = db->aDb[iSchema].zDbSName; |
| 824 | |
drh | 4a2c747 | 2018-08-13 15:09:48 +0000 | [diff] [blame] | 825 | /* Make sure the old name really is a column name in the table to be |
| 826 | ** altered. Set iCol to be the index of the column being renamed */ |
dan | cf8f289 | 2018-08-09 20:47:01 +0000 | [diff] [blame] | 827 | zOld = sqlite3NameFromToken(db, pOld); |
| 828 | if( !zOld ) goto exit_rename_column; |
| 829 | for(iCol=0; iCol<pTab->nCol; iCol++){ |
| 830 | if( 0==sqlite3StrICmp(pTab->aCol[iCol].zName, zOld) ) break; |
| 831 | } |
| 832 | if( iCol==pTab->nCol ){ |
| 833 | sqlite3ErrorMsg(pParse, "no such column: \"%s\"", zOld); |
| 834 | goto exit_rename_column; |
| 835 | } |
| 836 | |
drh | 4a2c747 | 2018-08-13 15:09:48 +0000 | [diff] [blame] | 837 | /* Do the rename operation using a recursive UPDATE statement that |
| 838 | ** uses the sqlite_rename_column() SQL function to compute the new |
| 839 | ** CREATE statement text for the sqlite_master table. |
| 840 | */ |
dan | cf8f289 | 2018-08-09 20:47:01 +0000 | [diff] [blame] | 841 | zNew = sqlite3NameFromToken(db, pNew); |
| 842 | if( !zNew ) goto exit_rename_column; |
dan | 404c3ba | 2018-08-11 20:38:33 +0000 | [diff] [blame] | 843 | assert( pNew->n>0 ); |
| 844 | bQuote = sqlite3Isquote(pNew->z[0]); |
dan | cf8f289 | 2018-08-09 20:47:01 +0000 | [diff] [blame] | 845 | sqlite3NestedParse(pParse, |
| 846 | "UPDATE \"%w\".%s SET " |
dan | 987db76 | 2018-08-14 20:18:50 +0000 | [diff] [blame] | 847 | "sql = sqlite_rename_column(sql, %Q, %Q, %d, %Q, %d) " |
dan | 499b825 | 2018-08-17 18:08:28 +0000 | [diff] [blame] | 848 | "WHERE name NOT LIKE 'sqlite_%%' AND (type != 'index' OR tbl_name = %Q)" |
| 849 | " AND sql NOT LIKE 'create virtual%%'", |
dan | 987db76 | 2018-08-14 20:18:50 +0000 | [diff] [blame] | 850 | zDb, MASTER_NAME, |
| 851 | zDb, pTab->zName, iCol, zNew, bQuote, |
| 852 | pTab->zName |
dan | cf8f289 | 2018-08-09 20:47:01 +0000 | [diff] [blame] | 853 | ); |
| 854 | |
dan | e325ffe | 2018-08-11 13:40:20 +0000 | [diff] [blame] | 855 | /* Drop and reload the database schema. */ |
dan | 872165f | 2018-08-11 17:34:38 +0000 | [diff] [blame] | 856 | if( pParse->pVdbe ){ |
dan | e325ffe | 2018-08-11 13:40:20 +0000 | [diff] [blame] | 857 | sqlite3ChangeCookie(pParse, iSchema); |
| 858 | sqlite3VdbeAddParseSchemaOp(pParse->pVdbe, iSchema, 0); |
| 859 | } |
dan | cf8f289 | 2018-08-09 20:47:01 +0000 | [diff] [blame] | 860 | |
| 861 | exit_rename_column: |
| 862 | sqlite3SrcListDelete(db, pSrc); |
| 863 | sqlite3DbFree(db, zOld); |
| 864 | sqlite3DbFree(db, zNew); |
| 865 | return; |
| 866 | } |
| 867 | |
drh | 4a2c747 | 2018-08-13 15:09:48 +0000 | [diff] [blame] | 868 | /* |
| 869 | ** Each RenameToken object maps an element of the parse tree into |
| 870 | ** the token that generated that element. The parse tree element |
| 871 | ** might be one of: |
| 872 | ** |
| 873 | ** * A pointer to an Expr that represents an ID |
| 874 | ** * The name of a table column in Column.zName |
| 875 | ** |
| 876 | ** A list of RenameToken objects can be constructed during parsing. |
| 877 | ** Each new object is created by sqlite3RenameToken(). |
| 878 | ** As the parse tree is transformed, the sqlite3MoveRenameToken() |
| 879 | ** routine is used to keep the mapping current. |
| 880 | ** |
| 881 | ** After the parse finishes, renameTokenFind() routine can be used |
| 882 | ** to look up the actual token value that created some element in |
| 883 | ** the parse tree. |
| 884 | */ |
dan | cf8f289 | 2018-08-09 20:47:01 +0000 | [diff] [blame] | 885 | struct RenameToken { |
drh | 4a2c747 | 2018-08-13 15:09:48 +0000 | [diff] [blame] | 886 | void *p; /* Parse tree element created by token t */ |
| 887 | Token t; /* The token that created parse tree element p */ |
| 888 | RenameToken *pNext; /* Next is a list of all RenameToken objects */ |
dan | cf8f289 | 2018-08-09 20:47:01 +0000 | [diff] [blame] | 889 | }; |
| 890 | |
drh | 4a2c747 | 2018-08-13 15:09:48 +0000 | [diff] [blame] | 891 | /* |
| 892 | ** The context of an ALTER TABLE RENAME COLUMN operation that gets passed |
| 893 | ** down into the Walker. |
| 894 | */ |
dan | 5496d6a | 2018-08-13 17:14:26 +0000 | [diff] [blame] | 895 | typedef struct RenameCtx RenameCtx; |
dan | cf8f289 | 2018-08-09 20:47:01 +0000 | [diff] [blame] | 896 | struct RenameCtx { |
| 897 | RenameToken *pList; /* List of tokens to overwrite */ |
| 898 | int nList; /* Number of tokens in pList */ |
| 899 | int iCol; /* Index of column being renamed */ |
dan | 987db76 | 2018-08-14 20:18:50 +0000 | [diff] [blame] | 900 | Table *pTab; /* Table being ALTERed */ |
dan | 5496d6a | 2018-08-13 17:14:26 +0000 | [diff] [blame] | 901 | const char *zOld; /* Old column name */ |
dan | cf8f289 | 2018-08-09 20:47:01 +0000 | [diff] [blame] | 902 | }; |
| 903 | |
drh | 4a2c747 | 2018-08-13 15:09:48 +0000 | [diff] [blame] | 904 | /* |
| 905 | ** Add a new RenameToken object mapping parse tree element pPtr into |
| 906 | ** token *pToken to the Parse object currently under construction. |
| 907 | */ |
dan | cf8f289 | 2018-08-09 20:47:01 +0000 | [diff] [blame] | 908 | void sqlite3RenameToken(Parse *pParse, void *pPtr, Token *pToken){ |
| 909 | RenameToken *pNew; |
| 910 | pNew = sqlite3DbMallocZero(pParse->db, sizeof(RenameToken)); |
| 911 | if( pNew ){ |
| 912 | pNew->p = pPtr; |
| 913 | pNew->t = *pToken; |
| 914 | pNew->pNext = pParse->pRename; |
| 915 | pParse->pRename = pNew; |
| 916 | } |
| 917 | } |
| 918 | |
drh | 4a2c747 | 2018-08-13 15:09:48 +0000 | [diff] [blame] | 919 | /* |
| 920 | ** If there is a RenameToken object associated with parse tree element |
| 921 | ** pFrom, then remap that object over to pTo due to a transformation |
| 922 | ** in the parse tree. |
| 923 | */ |
dan | cf8f289 | 2018-08-09 20:47:01 +0000 | [diff] [blame] | 924 | void sqlite3MoveRenameToken(Parse *pParse, void *pTo, void *pFrom){ |
| 925 | RenameToken *p; |
| 926 | for(p=pParse->pRename; p; p=p->pNext){ |
| 927 | if( p->p==pFrom ){ |
| 928 | p->p = pTo; |
| 929 | break; |
| 930 | } |
| 931 | } |
| 932 | assert( p ); |
| 933 | } |
| 934 | |
drh | 4a2c747 | 2018-08-13 15:09:48 +0000 | [diff] [blame] | 935 | /* |
| 936 | ** Free the list of RenameToken objects given in the second argument |
| 937 | */ |
dan | cf8f289 | 2018-08-09 20:47:01 +0000 | [diff] [blame] | 938 | static void renameTokenFree(sqlite3 *db, RenameToken *pToken){ |
| 939 | RenameToken *pNext; |
| 940 | RenameToken *p; |
| 941 | for(p=pToken; p; p=pNext){ |
| 942 | pNext = p->pNext; |
| 943 | sqlite3DbFree(db, p); |
| 944 | } |
| 945 | } |
| 946 | |
dan | 987db76 | 2018-08-14 20:18:50 +0000 | [diff] [blame] | 947 | /* |
| 948 | ** Search the Parse object passed as the first argument for a RenameToken |
| 949 | ** object associated with parse tree element pPtr. If found, remove it |
| 950 | ** from the Parse object and add it to the list maintained by the |
| 951 | ** RenameCtx object passed as the second argument. |
| 952 | */ |
| 953 | static void renameTokenFind(Parse *pParse, struct RenameCtx *pCtx, void *pPtr){ |
dan | cf8f289 | 2018-08-09 20:47:01 +0000 | [diff] [blame] | 954 | RenameToken **pp; |
| 955 | for(pp=&pParse->pRename; (*pp); pp=&(*pp)->pNext){ |
| 956 | if( (*pp)->p==pPtr ){ |
| 957 | RenameToken *pToken = *pp; |
| 958 | *pp = pToken->pNext; |
dan | 5496d6a | 2018-08-13 17:14:26 +0000 | [diff] [blame] | 959 | pToken->pNext = pCtx->pList; |
| 960 | pCtx->pList = pToken; |
| 961 | pCtx->nList++; |
| 962 | break; |
dan | cf8f289 | 2018-08-09 20:47:01 +0000 | [diff] [blame] | 963 | } |
| 964 | } |
dan | cf8f289 | 2018-08-09 20:47:01 +0000 | [diff] [blame] | 965 | } |
| 966 | |
dan | 24fedb9 | 2018-08-18 17:35:38 +0000 | [diff] [blame] | 967 | /* |
| 968 | ** This is a Walker select callback. It does nothing. It is only required |
| 969 | ** because without a dummy callback, sqlite3WalkExpr() and similar do not |
| 970 | ** descend into sub-select statements. |
| 971 | */ |
dan | 987db76 | 2018-08-14 20:18:50 +0000 | [diff] [blame] | 972 | static int renameColumnSelectCb(Walker *pWalker, Select *p){ |
| 973 | return WRC_Continue; |
| 974 | } |
| 975 | |
dan | 987db76 | 2018-08-14 20:18:50 +0000 | [diff] [blame] | 976 | /* |
| 977 | ** This is a Walker expression callback. |
| 978 | ** |
| 979 | ** For every TK_COLUMN node in the expression tree, search to see |
| 980 | ** if the column being references is the column being renamed by an |
| 981 | ** ALTER TABLE statement. If it is, then attach its associated |
| 982 | ** RenameToken object to the list of RenameToken objects being |
| 983 | ** constructed in RenameCtx object at pWalker->u.pRename. |
| 984 | */ |
dan | cf8f289 | 2018-08-09 20:47:01 +0000 | [diff] [blame] | 985 | static int renameColumnExprCb(Walker *pWalker, Expr *pExpr){ |
dan | 5496d6a | 2018-08-13 17:14:26 +0000 | [diff] [blame] | 986 | RenameCtx *p = pWalker->u.pRename; |
dan | 0cbb0b1 | 2018-08-16 19:49:16 +0000 | [diff] [blame] | 987 | if( pExpr->op==TK_TRIGGER |
| 988 | && pExpr->iColumn==p->iCol |
| 989 | && pWalker->pParse->pTriggerTab==p->pTab |
| 990 | ){ |
dan | 5be60c5 | 2018-08-15 20:28:39 +0000 | [diff] [blame] | 991 | renameTokenFind(pWalker->pParse, p, (void*)pExpr); |
dan | 0cbb0b1 | 2018-08-16 19:49:16 +0000 | [diff] [blame] | 992 | }else if( pExpr->op==TK_COLUMN |
| 993 | && pExpr->iColumn==p->iCol |
| 994 | && p->pTab==pExpr->pTab |
dan | 987db76 | 2018-08-14 20:18:50 +0000 | [diff] [blame] | 995 | ){ |
dan | 5496d6a | 2018-08-13 17:14:26 +0000 | [diff] [blame] | 996 | renameTokenFind(pWalker->pParse, p, (void*)pExpr); |
dan | cf8f289 | 2018-08-09 20:47:01 +0000 | [diff] [blame] | 997 | } |
| 998 | return WRC_Continue; |
| 999 | } |
| 1000 | |
dan | 987db76 | 2018-08-14 20:18:50 +0000 | [diff] [blame] | 1001 | /* |
| 1002 | ** The RenameCtx contains a list of tokens that reference a column that |
dan | 24fedb9 | 2018-08-18 17:35:38 +0000 | [diff] [blame] | 1003 | ** is being renamed by an ALTER TABLE statement. Return the "last" |
dan | 987db76 | 2018-08-14 20:18:50 +0000 | [diff] [blame] | 1004 | ** RenameToken in the RenameCtx and remove that RenameToken from the |
dan | 24fedb9 | 2018-08-18 17:35:38 +0000 | [diff] [blame] | 1005 | ** RenameContext. "Last" means the last RenameToken encountered when |
| 1006 | ** the input SQL is parsed from left to right. Repeated calls to this routine |
dan | 987db76 | 2018-08-14 20:18:50 +0000 | [diff] [blame] | 1007 | ** return all column name tokens in the order that they are encountered |
| 1008 | ** in the SQL statement. |
| 1009 | */ |
dan | 5496d6a | 2018-08-13 17:14:26 +0000 | [diff] [blame] | 1010 | static RenameToken *renameColumnTokenNext(RenameCtx *pCtx){ |
dan | cf8f289 | 2018-08-09 20:47:01 +0000 | [diff] [blame] | 1011 | RenameToken *pBest = pCtx->pList; |
| 1012 | RenameToken *pToken; |
| 1013 | RenameToken **pp; |
| 1014 | |
| 1015 | for(pToken=pBest->pNext; pToken; pToken=pToken->pNext){ |
| 1016 | if( pToken->t.z>pBest->t.z ) pBest = pToken; |
| 1017 | } |
| 1018 | for(pp=&pCtx->pList; *pp!=pBest; pp=&(*pp)->pNext); |
| 1019 | *pp = pBest->pNext; |
| 1020 | |
| 1021 | return pBest; |
| 1022 | } |
| 1023 | |
dan | 6fe7f23 | 2018-08-10 19:19:33 +0000 | [diff] [blame] | 1024 | /* |
dan | 24fedb9 | 2018-08-18 17:35:38 +0000 | [diff] [blame] | 1025 | ** An error occured while parsing or otherwise processing a database |
| 1026 | ** object (either pParse->pNewTable, pNewIndex or pNewTrigger) as part of an |
| 1027 | ** ALTER TABLE RENAME COLUMN program. The error message emitted by the |
| 1028 | ** sub-routine is currently stored in pParse->zErrMsg. This function |
| 1029 | ** adds context to the error message and then stores it in pCtx. |
| 1030 | */ |
| 1031 | static void renameColumnParseError(sqlite3_context *pCtx, Parse *pParse){ |
| 1032 | const char *zT; |
| 1033 | const char *zN; |
| 1034 | char *zErr; |
| 1035 | if( pParse->pNewTable ){ |
| 1036 | zT = pParse->pNewTable->pSelect ? "view" : "table"; |
| 1037 | zN = pParse->pNewTable->zName; |
| 1038 | }else if( pParse->pNewIndex ){ |
| 1039 | zT = "index"; |
| 1040 | zN = pParse->pNewIndex->zName; |
| 1041 | }else{ |
| 1042 | assert( pParse->pNewTrigger ); |
| 1043 | zT = "trigger"; |
| 1044 | zN = pParse->pNewTrigger->zName; |
| 1045 | } |
| 1046 | zErr = sqlite3_mprintf("error processing %s %s: %s", zT, zN, pParse->zErrMsg); |
| 1047 | sqlite3_result_error(pCtx, zErr, -1); |
| 1048 | sqlite3_free(zErr); |
| 1049 | } |
| 1050 | |
| 1051 | /* |
dan | 987db76 | 2018-08-14 20:18:50 +0000 | [diff] [blame] | 1052 | ** SQL function: |
| 1053 | ** |
| 1054 | ** sqlite_rename_column(zSql, iCol, bQuote, zNew, zTable, zOld) |
| 1055 | ** |
| 1056 | ** 0. zSql: SQL statement to rewrite |
| 1057 | ** 1. Database: Database name (e.g. "main") |
| 1058 | ** 2. Table: Table name |
| 1059 | ** 3. iCol: Index of column to rename |
| 1060 | ** 4. zNew: New column name |
| 1061 | ** 5. bQuote: True if the new column name should be quoted |
| 1062 | ** |
| 1063 | ** Do a column rename operation on the CREATE statement given in zSql. |
| 1064 | ** The iCol-th column (left-most is 0) of table zTable is renamed from zCol |
| 1065 | ** into zNew. The name should be quoted if bQuote is true. |
| 1066 | ** |
| 1067 | ** This function is used internally by the ALTER TABLE RENAME COLUMN command. |
| 1068 | ** Though accessible to application code, it is not intended for use by |
| 1069 | ** applications. The existance of this function, and the way it works, |
| 1070 | ** is subject to change without notice. |
| 1071 | ** |
| 1072 | ** If any of the parameters are out-of-bounds, then simply return NULL. |
| 1073 | ** An out-of-bounds parameter can only occur when the application calls |
| 1074 | ** this function directly. The parameters will always be well-formed when |
| 1075 | ** this routine is invoked by the bytecode for a legitimate ALTER TABLE |
| 1076 | ** statement. |
dan | 6fe7f23 | 2018-08-10 19:19:33 +0000 | [diff] [blame] | 1077 | */ |
dan | cf8f289 | 2018-08-09 20:47:01 +0000 | [diff] [blame] | 1078 | static void renameColumnFunc( |
| 1079 | sqlite3_context *context, |
| 1080 | int NotUsed, |
| 1081 | sqlite3_value **argv |
| 1082 | ){ |
| 1083 | sqlite3 *db = sqlite3_context_db_handle(context); |
dan | 5496d6a | 2018-08-13 17:14:26 +0000 | [diff] [blame] | 1084 | RenameCtx sCtx; |
drh | ad866a1 | 2018-08-10 19:33:09 +0000 | [diff] [blame] | 1085 | const char *zSql = (const char*)sqlite3_value_text(argv[0]); |
dan | cf8f289 | 2018-08-09 20:47:01 +0000 | [diff] [blame] | 1086 | int nSql = sqlite3_value_bytes(argv[0]); |
dan | 987db76 | 2018-08-14 20:18:50 +0000 | [diff] [blame] | 1087 | const char *zDb = (const char*)sqlite3_value_text(argv[1]); |
| 1088 | const char *zTable = (const char*)sqlite3_value_text(argv[2]); |
| 1089 | int iCol = sqlite3_value_int(argv[3]); |
| 1090 | const char *zNew = (const char*)sqlite3_value_text(argv[4]); |
| 1091 | int nNew = sqlite3_value_bytes(argv[4]); |
| 1092 | int bQuote = sqlite3_value_int(argv[5]); |
| 1093 | const char *zOld; |
dan | 6fe7f23 | 2018-08-10 19:19:33 +0000 | [diff] [blame] | 1094 | |
dan | cf8f289 | 2018-08-09 20:47:01 +0000 | [diff] [blame] | 1095 | int rc; |
| 1096 | char *zErr = 0; |
| 1097 | Parse sParse; |
| 1098 | Walker sWalker; |
dan | cf8f289 | 2018-08-09 20:47:01 +0000 | [diff] [blame] | 1099 | Index *pIdx; |
| 1100 | char *zOut = 0; |
| 1101 | |
| 1102 | char *zQuot = 0; /* Quoted version of zNew */ |
| 1103 | int nQuot = 0; /* Length of zQuot in bytes */ |
| 1104 | int i; |
dan | 987db76 | 2018-08-14 20:18:50 +0000 | [diff] [blame] | 1105 | Table *pTab; |
dan | cf8f289 | 2018-08-09 20:47:01 +0000 | [diff] [blame] | 1106 | |
dan | 987db76 | 2018-08-14 20:18:50 +0000 | [diff] [blame] | 1107 | if( zSql==0 ) return; |
| 1108 | if( zNew==0 ) return; |
| 1109 | if( zTable==0 ) return; |
| 1110 | if( iCol<0 ) return; |
| 1111 | pTab = sqlite3FindTable(db, zTable, zDb); |
| 1112 | if( pTab==0 || iCol>=pTab->nCol ) return; |
| 1113 | zOld = pTab->aCol[iCol].zName; |
dan | cf8f289 | 2018-08-09 20:47:01 +0000 | [diff] [blame] | 1114 | memset(&sCtx, 0, sizeof(sCtx)); |
dan | 987db76 | 2018-08-14 20:18:50 +0000 | [diff] [blame] | 1115 | sCtx.iCol = ((iCol==pTab->iPKey) ? -1 : iCol); |
dan | cf8f289 | 2018-08-09 20:47:01 +0000 | [diff] [blame] | 1116 | |
dan | 24fedb9 | 2018-08-18 17:35:38 +0000 | [diff] [blame] | 1117 | /* Parse the SQL statement passed as the first argument. If no error |
| 1118 | ** occurs and the parse does not result in a new table, index or |
| 1119 | ** trigger object, the database must be corrupt. */ |
dan | cf8f289 | 2018-08-09 20:47:01 +0000 | [diff] [blame] | 1120 | memset(&sParse, 0, sizeof(sParse)); |
| 1121 | sParse.eParseMode = PARSE_MODE_RENAME_COLUMN; |
| 1122 | sParse.db = db; |
| 1123 | sParse.nQueryLoop = 1; |
| 1124 | rc = sqlite3RunParser(&sParse, zSql, &zErr); |
dan | 24fedb9 | 2018-08-18 17:35:38 +0000 | [diff] [blame] | 1125 | assert( sParse.zErrMsg==0 ); |
| 1126 | assert( rc!=SQLITE_OK || zErr==0 ); |
| 1127 | assert( (!!sParse.pNewTable)+(!!sParse.pNewIndex)+(!!sParse.pNewTrigger)<2 ); |
| 1128 | sParse.zErrMsg = zErr; |
drh | 5fc22cd | 2018-08-13 13:43:11 +0000 | [diff] [blame] | 1129 | if( db->mallocFailed ) rc = SQLITE_NOMEM; |
dan | 5496d6a | 2018-08-13 17:14:26 +0000 | [diff] [blame] | 1130 | if( rc==SQLITE_OK |
| 1131 | && sParse.pNewTable==0 && sParse.pNewIndex==0 && sParse.pNewTrigger==0 |
| 1132 | ){ |
dan | cf8f289 | 2018-08-09 20:47:01 +0000 | [diff] [blame] | 1133 | rc = SQLITE_CORRUPT_BKPT; |
| 1134 | } |
| 1135 | |
dan | cf8f289 | 2018-08-09 20:47:01 +0000 | [diff] [blame] | 1136 | #ifdef SQLITE_DEBUG |
dan | 24fedb9 | 2018-08-18 17:35:38 +0000 | [diff] [blame] | 1137 | /* Ensure that all mappings in the Parse.pRename list really do map to |
| 1138 | ** a part of the input string. */ |
dan | cf8f289 | 2018-08-09 20:47:01 +0000 | [diff] [blame] | 1139 | assert( sqlite3Strlen30(zSql)==nSql ); |
dan | 987db76 | 2018-08-14 20:18:50 +0000 | [diff] [blame] | 1140 | if( rc==SQLITE_OK ){ |
dan | cf8f289 | 2018-08-09 20:47:01 +0000 | [diff] [blame] | 1141 | RenameToken *pToken; |
| 1142 | for(pToken=sParse.pRename; pToken; pToken=pToken->pNext){ |
| 1143 | assert( pToken->t.z>=zSql && &pToken->t.z[pToken->t.n]<=&zSql[nSql] ); |
| 1144 | } |
| 1145 | } |
| 1146 | #endif |
| 1147 | |
dan | 24fedb9 | 2018-08-18 17:35:38 +0000 | [diff] [blame] | 1148 | /* Set zQuot to point to a buffer containing a quoted copy of the |
| 1149 | ** identifier zNew. If the corresponding identifier in the original |
| 1150 | ** ALTER TABLE statement was quoted (bQuote==1), then set zNew to |
| 1151 | ** point to zQuot so that all substitutions are made using the |
| 1152 | ** quoted version of the new column name. */ |
| 1153 | if( rc==SQLITE_OK ){ |
| 1154 | zQuot = sqlite3_mprintf("\"%w\"", zNew); |
| 1155 | if( zQuot==0 ){ |
| 1156 | rc = SQLITE_NOMEM; |
| 1157 | }else{ |
| 1158 | nQuot = sqlite3Strlen30(zQuot); |
| 1159 | } |
| 1160 | } |
| 1161 | if( bQuote ){ |
| 1162 | zNew = zQuot; |
| 1163 | nNew = nQuot; |
| 1164 | } |
| 1165 | |
dan | cf8f289 | 2018-08-09 20:47:01 +0000 | [diff] [blame] | 1166 | /* Find tokens that need to be replaced. */ |
| 1167 | memset(&sWalker, 0, sizeof(Walker)); |
| 1168 | sWalker.pParse = &sParse; |
| 1169 | sWalker.xExprCallback = renameColumnExprCb; |
dan | 987db76 | 2018-08-14 20:18:50 +0000 | [diff] [blame] | 1170 | sWalker.xSelectCallback = renameColumnSelectCb; |
dan | cf8f289 | 2018-08-09 20:47:01 +0000 | [diff] [blame] | 1171 | sWalker.u.pRename = &sCtx; |
| 1172 | |
dan | 0cbb0b1 | 2018-08-16 19:49:16 +0000 | [diff] [blame] | 1173 | sCtx.pTab = pTab; |
dan | 987db76 | 2018-08-14 20:18:50 +0000 | [diff] [blame] | 1174 | if( rc!=SQLITE_OK ) goto renameColumnFunc_done; |
dan | cf8f289 | 2018-08-09 20:47:01 +0000 | [diff] [blame] | 1175 | if( sParse.pNewTable ){ |
dan | 987db76 | 2018-08-14 20:18:50 +0000 | [diff] [blame] | 1176 | Select *pSelect = sParse.pNewTable->pSelect; |
| 1177 | if( pSelect ){ |
dan | 987db76 | 2018-08-14 20:18:50 +0000 | [diff] [blame] | 1178 | sParse.rc = SQLITE_OK; |
| 1179 | sqlite3SelectPrep(&sParse, sParse.pNewTable->pSelect, 0); |
| 1180 | rc = (db->mallocFailed ? SQLITE_NOMEM : sParse.rc); |
| 1181 | if( rc==SQLITE_OK ){ |
| 1182 | sqlite3WalkSelect(&sWalker, pSelect); |
dan | a8762ae | 2018-08-11 17:49:23 +0000 | [diff] [blame] | 1183 | } |
dan | 987db76 | 2018-08-14 20:18:50 +0000 | [diff] [blame] | 1184 | if( rc!=SQLITE_OK ) goto renameColumnFunc_done; |
| 1185 | }else{ |
| 1186 | /* A regular table */ |
| 1187 | int bFKOnly = sqlite3_stricmp(zTable, sParse.pNewTable->zName); |
| 1188 | FKey *pFKey; |
| 1189 | assert( sParse.pNewTable->pSelect==0 ); |
dan | 0cbb0b1 | 2018-08-16 19:49:16 +0000 | [diff] [blame] | 1190 | sCtx.pTab = sParse.pNewTable; |
dan | 987db76 | 2018-08-14 20:18:50 +0000 | [diff] [blame] | 1191 | if( bFKOnly==0 ){ |
| 1192 | renameTokenFind( |
| 1193 | &sParse, &sCtx, (void*)sParse.pNewTable->aCol[iCol].zName |
| 1194 | ); |
| 1195 | if( sCtx.iCol<0 ){ |
| 1196 | renameTokenFind(&sParse, &sCtx, (void*)&sParse.pNewTable->iPKey); |
dan | cf8f289 | 2018-08-09 20:47:01 +0000 | [diff] [blame] | 1197 | } |
dan | 987db76 | 2018-08-14 20:18:50 +0000 | [diff] [blame] | 1198 | sqlite3WalkExprList(&sWalker, sParse.pNewTable->pCheck); |
| 1199 | for(pIdx=sParse.pNewTable->pIndex; pIdx; pIdx=pIdx->pNext){ |
| 1200 | sqlite3WalkExprList(&sWalker, pIdx->aColExpr); |
| 1201 | } |
| 1202 | } |
| 1203 | |
| 1204 | for(pFKey=sParse.pNewTable->pFKey; pFKey; pFKey=pFKey->pNextFrom){ |
| 1205 | for(i=0; i<pFKey->nCol; i++){ |
dan | 356afab | 2018-08-14 21:05:35 +0000 | [diff] [blame] | 1206 | if( bFKOnly==0 && pFKey->aCol[i].iFrom==iCol ){ |
dan | 987db76 | 2018-08-14 20:18:50 +0000 | [diff] [blame] | 1207 | renameTokenFind(&sParse, &sCtx, (void*)&pFKey->aCol[i]); |
| 1208 | } |
| 1209 | if( 0==sqlite3_stricmp(pFKey->zTo, zTable) |
| 1210 | && 0==sqlite3_stricmp(pFKey->aCol[i].zCol, zOld) |
| 1211 | ){ |
| 1212 | renameTokenFind(&sParse, &sCtx, (void*)pFKey->aCol[i].zCol); |
| 1213 | } |
dan | 6fe7f23 | 2018-08-10 19:19:33 +0000 | [diff] [blame] | 1214 | } |
dan | cf8f289 | 2018-08-09 20:47:01 +0000 | [diff] [blame] | 1215 | } |
| 1216 | } |
dan | 5496d6a | 2018-08-13 17:14:26 +0000 | [diff] [blame] | 1217 | }else if( sParse.pNewIndex ){ |
dan | cf8f289 | 2018-08-09 20:47:01 +0000 | [diff] [blame] | 1218 | sqlite3WalkExprList(&sWalker, sParse.pNewIndex->aColExpr); |
| 1219 | sqlite3WalkExpr(&sWalker, sParse.pNewIndex->pPartIdxWhere); |
dan | 5496d6a | 2018-08-13 17:14:26 +0000 | [diff] [blame] | 1220 | }else{ |
dan | 5be60c5 | 2018-08-15 20:28:39 +0000 | [diff] [blame] | 1221 | /* A trigger */ |
| 1222 | TriggerStep *pStep; |
| 1223 | NameContext sNC; |
| 1224 | memset(&sNC, 0, sizeof(sNC)); |
| 1225 | sNC.pParse = &sParse; |
dan | 0cbb0b1 | 2018-08-16 19:49:16 +0000 | [diff] [blame] | 1226 | sParse.pTriggerTab = sqlite3FindTable(db, sParse.pNewTrigger->table, zDb); |
dan | 5be60c5 | 2018-08-15 20:28:39 +0000 | [diff] [blame] | 1227 | sParse.eTriggerOp = sParse.pNewTrigger->op; |
| 1228 | |
dan | 0cbb0b1 | 2018-08-16 19:49:16 +0000 | [diff] [blame] | 1229 | /* Resolve symbols in WHEN clause */ |
| 1230 | if( sParse.pNewTrigger->pWhen ){ |
dan | 5be60c5 | 2018-08-15 20:28:39 +0000 | [diff] [blame] | 1231 | rc = sqlite3ResolveExprNames(&sNC, sParse.pNewTrigger->pWhen); |
| 1232 | } |
| 1233 | |
| 1234 | for(pStep=sParse.pNewTrigger->step_list; |
| 1235 | rc==SQLITE_OK && pStep; |
| 1236 | pStep=pStep->pNext |
| 1237 | ){ |
| 1238 | if( pStep->pSelect ) sqlite3SelectPrep(&sParse, pStep->pSelect, &sNC); |
| 1239 | if( pStep->zTarget ){ |
| 1240 | Table *pTarget = sqlite3FindTable(db, pStep->zTarget, zDb); |
| 1241 | if( pTarget==0 ){ |
| 1242 | rc = SQLITE_ERROR; |
| 1243 | }else{ |
| 1244 | SrcList sSrc; |
| 1245 | memset(&sSrc, 0, sizeof(sSrc)); |
| 1246 | sSrc.nSrc = 1; |
| 1247 | sSrc.a[0].zName = pStep->zTarget; |
| 1248 | sSrc.a[0].pTab = pTarget; |
| 1249 | sNC.pSrcList = &sSrc; |
| 1250 | if( pStep->pWhere ){ |
| 1251 | rc = sqlite3ResolveExprNames(&sNC, pStep->pWhere); |
| 1252 | } |
| 1253 | if( rc==SQLITE_OK ){ |
| 1254 | rc = sqlite3ResolveExprListNames(&sNC, pStep->pExprList); |
| 1255 | } |
dan | 0cbb0b1 | 2018-08-16 19:49:16 +0000 | [diff] [blame] | 1256 | if( pStep->pUpsert ){ |
| 1257 | Upsert *pUpsert = pStep->pUpsert; |
| 1258 | if( rc==SQLITE_OK ){ |
| 1259 | rc = sqlite3ResolveExprListNames(&sNC, pUpsert->pUpsertTarget); |
| 1260 | } |
| 1261 | if( rc==SQLITE_OK && pUpsert->pUpsertSet){ |
| 1262 | ExprList *pUpsertSet = pUpsert->pUpsertSet; |
| 1263 | rc = sqlite3ResolveExprListNames(&sNC, pUpsertSet); |
| 1264 | if( rc==SQLITE_OK && pTarget==pTab ){ |
| 1265 | for(i=0; i<pUpsertSet->nExpr; i++){ |
| 1266 | char *zName = pUpsertSet->a[i].zName; |
| 1267 | if( 0==sqlite3_stricmp(zName, zOld) ){ |
| 1268 | renameTokenFind(&sParse, &sCtx, (void*)zName); |
| 1269 | } |
| 1270 | } |
| 1271 | } |
| 1272 | } |
| 1273 | if( rc==SQLITE_OK ){ |
| 1274 | rc = sqlite3ResolveExprNames(&sNC, pUpsert->pUpsertWhere); |
| 1275 | } |
| 1276 | if( rc==SQLITE_OK ){ |
| 1277 | rc = sqlite3ResolveExprNames(&sNC, pUpsert->pUpsertTargetWhere); |
| 1278 | } |
| 1279 | } |
dan | 5be60c5 | 2018-08-15 20:28:39 +0000 | [diff] [blame] | 1280 | |
| 1281 | if( rc==SQLITE_OK && pTarget==pTab ){ |
| 1282 | if( pStep->pIdList ){ |
| 1283 | for(i=0; i<pStep->pIdList->nId; i++){ |
| 1284 | char *zName = pStep->pIdList->a[i].zName; |
| 1285 | if( 0==sqlite3_stricmp(zName, zOld) ){ |
| 1286 | renameTokenFind(&sParse, &sCtx, (void*)zName); |
| 1287 | } |
| 1288 | } |
| 1289 | } |
| 1290 | if( pStep->op==TK_UPDATE ){ |
| 1291 | assert( pStep->pExprList ); |
| 1292 | for(i=0; i<pStep->pExprList->nExpr; i++){ |
| 1293 | char *zName = pStep->pExprList->a[i].zName; |
| 1294 | if( 0==sqlite3_stricmp(zName, zOld) ){ |
| 1295 | renameTokenFind(&sParse, &sCtx, (void*)zName); |
| 1296 | } |
| 1297 | } |
| 1298 | } |
| 1299 | } |
| 1300 | } |
| 1301 | } |
| 1302 | } |
| 1303 | |
| 1304 | if( rc!=SQLITE_OK ) goto renameColumnFunc_done; |
| 1305 | |
| 1306 | /* Find tokens to edit in UPDATE OF clause */ |
| 1307 | if( sParse.pTriggerTab==pTab && sParse.pNewTrigger->pColumns ){ |
dan | 5496d6a | 2018-08-13 17:14:26 +0000 | [diff] [blame] | 1308 | for(i=0; i<sParse.pNewTrigger->pColumns->nId; i++){ |
| 1309 | char *zName = sParse.pNewTrigger->pColumns->a[i].zName; |
| 1310 | if( 0==sqlite3_stricmp(zName, zOld) ){ |
| 1311 | renameTokenFind(&sParse, &sCtx, (void*)zName); |
| 1312 | } |
| 1313 | } |
| 1314 | } |
dan | 5be60c5 | 2018-08-15 20:28:39 +0000 | [diff] [blame] | 1315 | |
| 1316 | /* Find tokens to edit in WHEN clause */ |
| 1317 | sqlite3WalkExpr(&sWalker, sParse.pNewTrigger->pWhen); |
| 1318 | |
| 1319 | /* Find tokens to edit in trigger steps */ |
| 1320 | for(pStep=sParse.pNewTrigger->step_list; pStep; pStep=pStep->pNext){ |
| 1321 | sqlite3WalkSelect(&sWalker, pStep->pSelect); |
| 1322 | sqlite3WalkExpr(&sWalker, pStep->pWhere); |
| 1323 | sqlite3WalkExprList(&sWalker, pStep->pExprList); |
dan | 0cbb0b1 | 2018-08-16 19:49:16 +0000 | [diff] [blame] | 1324 | if( pStep->pUpsert ){ |
| 1325 | Upsert *pUpsert = pStep->pUpsert; |
| 1326 | sqlite3WalkExprList(&sWalker, pUpsert->pUpsertTarget); |
| 1327 | sqlite3WalkExprList(&sWalker, pUpsert->pUpsertSet); |
| 1328 | sqlite3WalkExpr(&sWalker, pUpsert->pUpsertWhere); |
| 1329 | sqlite3WalkExpr(&sWalker, pUpsert->pUpsertTargetWhere); |
| 1330 | } |
dan | 5be60c5 | 2018-08-15 20:28:39 +0000 | [diff] [blame] | 1331 | } |
dan | cf8f289 | 2018-08-09 20:47:01 +0000 | [diff] [blame] | 1332 | } |
| 1333 | |
dan | 24fedb9 | 2018-08-18 17:35:38 +0000 | [diff] [blame] | 1334 | /* At this point sCtx.pList contains a list of RenameToken objects |
| 1335 | ** corresponding to all tokens in the input SQL that must be replaced |
| 1336 | ** with the new column name. All that remains is to construct and |
| 1337 | ** return the edited SQL string. */ |
dan | 987db76 | 2018-08-14 20:18:50 +0000 | [diff] [blame] | 1338 | assert( rc==SQLITE_OK ); |
dan | 404c3ba | 2018-08-11 20:38:33 +0000 | [diff] [blame] | 1339 | assert( nQuot>=nNew ); |
| 1340 | zOut = sqlite3DbMallocZero(db, nSql + sCtx.nList*nQuot + 1); |
dan | cf8f289 | 2018-08-09 20:47:01 +0000 | [diff] [blame] | 1341 | if( zOut ){ |
| 1342 | int nOut = nSql; |
| 1343 | memcpy(zOut, zSql, nSql); |
| 1344 | while( sCtx.pList ){ |
| 1345 | int iOff; /* Offset of token to replace in zOut */ |
| 1346 | RenameToken *pBest = renameColumnTokenNext(&sCtx); |
| 1347 | |
| 1348 | int nReplace; |
| 1349 | const char *zReplace; |
| 1350 | if( sqlite3IsIdChar(*pBest->t.z) ){ |
| 1351 | nReplace = nNew; |
| 1352 | zReplace = zNew; |
| 1353 | }else{ |
| 1354 | nReplace = nQuot; |
| 1355 | zReplace = zQuot; |
| 1356 | } |
| 1357 | |
| 1358 | iOff = pBest->t.z - zSql; |
| 1359 | if( pBest->t.n!=nReplace ){ |
| 1360 | memmove(&zOut[iOff + nReplace], &zOut[iOff + pBest->t.n], |
| 1361 | nOut - (iOff + pBest->t.n) |
| 1362 | ); |
| 1363 | nOut += nReplace - pBest->t.n; |
| 1364 | zOut[nOut] = '\0'; |
| 1365 | } |
| 1366 | memcpy(&zOut[iOff], zReplace, nReplace); |
| 1367 | sqlite3DbFree(db, pBest); |
| 1368 | } |
| 1369 | |
| 1370 | sqlite3_result_text(context, zOut, -1, SQLITE_TRANSIENT); |
| 1371 | sqlite3DbFree(db, zOut); |
dan | 987db76 | 2018-08-14 20:18:50 +0000 | [diff] [blame] | 1372 | }else{ |
| 1373 | rc = SQLITE_NOMEM; |
dan | cf8f289 | 2018-08-09 20:47:01 +0000 | [diff] [blame] | 1374 | } |
| 1375 | |
drh | 5fc22cd | 2018-08-13 13:43:11 +0000 | [diff] [blame] | 1376 | renameColumnFunc_done: |
dan | 987db76 | 2018-08-14 20:18:50 +0000 | [diff] [blame] | 1377 | if( rc!=SQLITE_OK ){ |
dan | 24fedb9 | 2018-08-18 17:35:38 +0000 | [diff] [blame] | 1378 | if( sParse.zErrMsg ){ |
| 1379 | renameColumnParseError(context, &sParse); |
dan | 987db76 | 2018-08-14 20:18:50 +0000 | [diff] [blame] | 1380 | }else{ |
| 1381 | sqlite3_result_error_code(context, rc); |
| 1382 | } |
| 1383 | } |
| 1384 | |
dan | cf8f289 | 2018-08-09 20:47:01 +0000 | [diff] [blame] | 1385 | if( sParse.pVdbe ){ |
| 1386 | sqlite3VdbeFinalize(sParse.pVdbe); |
| 1387 | } |
| 1388 | sqlite3DeleteTable(db, sParse.pNewTable); |
| 1389 | if( sParse.pNewIndex ) sqlite3FreeIndex(db, sParse.pNewIndex); |
dan | 5496d6a | 2018-08-13 17:14:26 +0000 | [diff] [blame] | 1390 | sqlite3DeleteTrigger(db, sParse.pNewTrigger); |
dan | cf8f289 | 2018-08-09 20:47:01 +0000 | [diff] [blame] | 1391 | renameTokenFree(db, sParse.pRename); |
drh | 4a2c747 | 2018-08-13 15:09:48 +0000 | [diff] [blame] | 1392 | renameTokenFree(db, sCtx.pList); |
dan | 24fedb9 | 2018-08-18 17:35:38 +0000 | [diff] [blame] | 1393 | sqlite3DbFree(db, sParse.zErrMsg); |
dan | cf8f289 | 2018-08-09 20:47:01 +0000 | [diff] [blame] | 1394 | sqlite3ParserReset(&sParse); |
| 1395 | sqlite3_free(zQuot); |
| 1396 | } |
| 1397 | |
| 1398 | /* |
| 1399 | ** Register built-in functions used to help implement ALTER TABLE |
| 1400 | */ |
| 1401 | void sqlite3AlterFunctions(void){ |
| 1402 | static FuncDef aAlterTableFuncs[] = { |
| 1403 | FUNCTION(sqlite_rename_table, 2, 0, 0, renameTableFunc), |
dan | 404c3ba | 2018-08-11 20:38:33 +0000 | [diff] [blame] | 1404 | FUNCTION(sqlite_rename_column, 6, 0, 0, renameColumnFunc), |
dan | cf8f289 | 2018-08-09 20:47:01 +0000 | [diff] [blame] | 1405 | #ifndef SQLITE_OMIT_TRIGGER |
| 1406 | FUNCTION(sqlite_rename_trigger, 2, 0, 0, renameTriggerFunc), |
| 1407 | #endif |
| 1408 | #ifndef SQLITE_OMIT_FOREIGN_KEY |
| 1409 | FUNCTION(sqlite_rename_parent, 3, 0, 0, renameParentFunc), |
| 1410 | #endif |
| 1411 | }; |
| 1412 | sqlite3InsertBuiltinFuncs(aAlterTableFuncs, ArraySize(aAlterTableFuncs)); |
| 1413 | } |
drh | d0e4a6c | 2005-02-15 20:47:57 +0000 | [diff] [blame] | 1414 | #endif /* SQLITE_ALTER_TABLE */ |