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 ); |
drh | 1f01ec1 | 2005-02-15 21:36:18 +0000 | [diff] [blame] | 77 | assert( len>0 ); |
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 | |
| 145 | zResult = sqlite3MPrintf(db, "%s%s", (zOutput?zOutput:""), zInput), |
| 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 ); |
| 201 | assert( len>0 ); |
| 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 | /* |
| 229 | ** Register built-in functions used to help implement ALTER TABLE |
| 230 | */ |
drh | 545f587 | 2010-04-24 14:02:59 +0000 | [diff] [blame] | 231 | void sqlite3AlterFunctions(void){ |
| 232 | static SQLITE_WSD FuncDef aAlterTableFuncs[] = { |
| 233 | FUNCTION(sqlite_rename_table, 2, 0, 0, renameTableFunc), |
drh | 1f01ec1 | 2005-02-15 21:36:18 +0000 | [diff] [blame] | 234 | #ifndef SQLITE_OMIT_TRIGGER |
drh | 545f587 | 2010-04-24 14:02:59 +0000 | [diff] [blame] | 235 | FUNCTION(sqlite_rename_trigger, 2, 0, 0, renameTriggerFunc), |
drh | 1f01ec1 | 2005-02-15 21:36:18 +0000 | [diff] [blame] | 236 | #endif |
dan | 432cc5b | 2009-09-26 17:51:48 +0000 | [diff] [blame] | 237 | #ifndef SQLITE_OMIT_FOREIGN_KEY |
drh | 545f587 | 2010-04-24 14:02:59 +0000 | [diff] [blame] | 238 | FUNCTION(sqlite_rename_parent, 3, 0, 0, renameParentFunc), |
dan | 432cc5b | 2009-09-26 17:51:48 +0000 | [diff] [blame] | 239 | #endif |
drh | 545f587 | 2010-04-24 14:02:59 +0000 | [diff] [blame] | 240 | }; |
| 241 | int i; |
| 242 | FuncDefHash *pHash = &GLOBAL(FuncDefHash, sqlite3GlobalFunctions); |
| 243 | FuncDef *aFunc = (FuncDef*)&GLOBAL(FuncDef, aAlterTableFuncs); |
| 244 | |
| 245 | for(i=0; i<ArraySize(aAlterTableFuncs); i++){ |
| 246 | sqlite3FuncDefInsert(pHash, &aFunc[i]); |
| 247 | } |
dan | 432cc5b | 2009-09-26 17:51:48 +0000 | [diff] [blame] | 248 | } |
| 249 | |
| 250 | /* |
| 251 | ** This function is used to create the text of expressions of the form: |
| 252 | ** |
| 253 | ** name=<constant1> OR name=<constant2> OR ... |
| 254 | ** |
| 255 | ** If argument zWhere is NULL, then a pointer string containing the text |
| 256 | ** "name=<constant>" is returned, where <constant> is the quoted version |
| 257 | ** of the string passed as argument zConstant. The returned buffer is |
| 258 | ** allocated using sqlite3DbMalloc(). It is the responsibility of the |
| 259 | ** caller to ensure that it is eventually freed. |
| 260 | ** |
| 261 | ** If argument zWhere is not NULL, then the string returned is |
| 262 | ** "<where> OR name=<constant>", where <where> is the contents of zWhere. |
| 263 | ** In this case zWhere is passed to sqlite3DbFree() before returning. |
| 264 | ** |
| 265 | */ |
| 266 | static char *whereOrName(sqlite3 *db, char *zWhere, char *zConstant){ |
| 267 | char *zNew; |
| 268 | if( !zWhere ){ |
| 269 | zNew = sqlite3MPrintf(db, "name=%Q", zConstant); |
| 270 | }else{ |
| 271 | zNew = sqlite3MPrintf(db, "%s OR name=%Q", zWhere, zConstant); |
| 272 | sqlite3DbFree(db, zWhere); |
| 273 | } |
| 274 | return zNew; |
| 275 | } |
| 276 | |
dan | 856ef1a | 2009-09-29 06:33:23 +0000 | [diff] [blame] | 277 | #if !defined(SQLITE_OMIT_FOREIGN_KEY) && !defined(SQLITE_OMIT_TRIGGER) |
dan | 432cc5b | 2009-09-26 17:51:48 +0000 | [diff] [blame] | 278 | /* |
| 279 | ** Generate the text of a WHERE expression which can be used to select all |
| 280 | ** tables that have foreign key constraints that refer to table pTab (i.e. |
| 281 | ** constraints for which pTab is the parent table) from the sqlite_master |
| 282 | ** table. |
| 283 | */ |
| 284 | static char *whereForeignKeys(Parse *pParse, Table *pTab){ |
| 285 | FKey *p; |
| 286 | char *zWhere = 0; |
| 287 | for(p=sqlite3FkReferences(pTab); p; p=p->pNextTo){ |
| 288 | zWhere = whereOrName(pParse->db, zWhere, p->pFrom->zName); |
| 289 | } |
| 290 | return zWhere; |
drh | 1f01ec1 | 2005-02-15 21:36:18 +0000 | [diff] [blame] | 291 | } |
dan | 856ef1a | 2009-09-29 06:33:23 +0000 | [diff] [blame] | 292 | #endif |
drh | 1f01ec1 | 2005-02-15 21:36:18 +0000 | [diff] [blame] | 293 | |
drh | d0e4a6c | 2005-02-15 20:47:57 +0000 | [diff] [blame] | 294 | /* |
danielk1977 | 19a8e7e | 2005-03-17 05:03:38 +0000 | [diff] [blame] | 295 | ** Generate the text of a WHERE expression which can be used to select all |
| 296 | ** temporary triggers on table pTab from the sqlite_temp_master table. If |
| 297 | ** table pTab has no temporary triggers, or is itself stored in the |
| 298 | ** temporary database, NULL is returned. |
| 299 | */ |
| 300 | static char *whereTempTriggers(Parse *pParse, Table *pTab){ |
| 301 | Trigger *pTrig; |
| 302 | char *zWhere = 0; |
danielk1977 | e501b89 | 2006-01-09 06:29:47 +0000 | [diff] [blame] | 303 | const Schema *pTempSchema = pParse->db->aDb[1].pSchema; /* Temp db schema */ |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 304 | |
| 305 | /* If the table is not located in the temp-db (in which case NULL is |
| 306 | ** returned, loop through the tables list of triggers. For each trigger |
| 307 | ** that is not part of the temp-db schema, add a clause to the WHERE |
| 308 | ** expression being built up in zWhere. |
| 309 | */ |
| 310 | if( pTab->pSchema!=pTempSchema ){ |
danielk1977 | 1e53695 | 2007-08-16 10:09:01 +0000 | [diff] [blame] | 311 | sqlite3 *db = pParse->db; |
danielk1977 | 2f886d1 | 2009-02-28 10:47:41 +0000 | [diff] [blame] | 312 | for(pTrig=sqlite3TriggerList(pParse, pTab); pTrig; pTrig=pTrig->pNext){ |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 313 | if( pTrig->pSchema==pTempSchema ){ |
dan | 432cc5b | 2009-09-26 17:51:48 +0000 | [diff] [blame] | 314 | zWhere = whereOrName(db, zWhere, pTrig->zName); |
danielk1977 | 19a8e7e | 2005-03-17 05:03:38 +0000 | [diff] [blame] | 315 | } |
| 316 | } |
| 317 | } |
dan | 39f1bcb | 2010-09-29 07:16:46 +0000 | [diff] [blame] | 318 | if( zWhere ){ |
| 319 | char *zNew = sqlite3MPrintf(pParse->db, "type='trigger' AND (%s)", zWhere); |
| 320 | sqlite3DbFree(pParse->db, zWhere); |
| 321 | zWhere = zNew; |
| 322 | } |
danielk1977 | 19a8e7e | 2005-03-17 05:03:38 +0000 | [diff] [blame] | 323 | return zWhere; |
| 324 | } |
| 325 | |
| 326 | /* |
| 327 | ** Generate code to drop and reload the internal representation of table |
| 328 | ** pTab from the database, including triggers and temporary triggers. |
| 329 | ** Argument zName is the name of the table in the database schema at |
| 330 | ** the time the generated code is executed. This can be different from |
| 331 | ** pTab->zName if this function is being called to code part of an |
| 332 | ** "ALTER TABLE RENAME TO" statement. |
| 333 | */ |
| 334 | static void reloadTableSchema(Parse *pParse, Table *pTab, const char *zName){ |
| 335 | Vdbe *v; |
| 336 | char *zWhere; |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 337 | int iDb; /* Index of database containing pTab */ |
danielk1977 | 19a8e7e | 2005-03-17 05:03:38 +0000 | [diff] [blame] | 338 | #ifndef SQLITE_OMIT_TRIGGER |
| 339 | Trigger *pTrig; |
| 340 | #endif |
| 341 | |
| 342 | v = sqlite3GetVdbe(pParse); |
drh | d3264c7 | 2009-04-15 13:39:47 +0000 | [diff] [blame] | 343 | if( NEVER(v==0) ) return; |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 344 | assert( sqlite3BtreeHoldsAllMutexes(pParse->db) ); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 345 | iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema); |
| 346 | assert( iDb>=0 ); |
danielk1977 | 19a8e7e | 2005-03-17 05:03:38 +0000 | [diff] [blame] | 347 | |
| 348 | #ifndef SQLITE_OMIT_TRIGGER |
| 349 | /* Drop any table triggers from the internal schema. */ |
danielk1977 | 2f886d1 | 2009-02-28 10:47:41 +0000 | [diff] [blame] | 350 | for(pTrig=sqlite3TriggerList(pParse, pTab); pTrig; pTrig=pTrig->pNext){ |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 351 | int iTrigDb = sqlite3SchemaToIndex(pParse->db, pTrig->pSchema); |
| 352 | assert( iTrigDb==iDb || iTrigDb==1 ); |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 353 | sqlite3VdbeAddOp4(v, OP_DropTrigger, iTrigDb, 0, 0, pTrig->zName, 0); |
danielk1977 | 19a8e7e | 2005-03-17 05:03:38 +0000 | [diff] [blame] | 354 | } |
| 355 | #endif |
| 356 | |
dan | 432cc5b | 2009-09-26 17:51:48 +0000 | [diff] [blame] | 357 | /* Drop the table and index from the internal schema. */ |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 358 | sqlite3VdbeAddOp4(v, OP_DropTable, iDb, 0, 0, pTab->zName, 0); |
danielk1977 | 19a8e7e | 2005-03-17 05:03:38 +0000 | [diff] [blame] | 359 | |
| 360 | /* Reload the table, index and permanent trigger schemas. */ |
danielk1977 | 1e53695 | 2007-08-16 10:09:01 +0000 | [diff] [blame] | 361 | zWhere = sqlite3MPrintf(pParse->db, "tbl_name=%Q", zName); |
danielk1977 | 19a8e7e | 2005-03-17 05:03:38 +0000 | [diff] [blame] | 362 | if( !zWhere ) return; |
drh | 5d9c9da | 2011-06-03 20:11:17 +0000 | [diff] [blame] | 363 | sqlite3VdbeAddParseSchemaOp(v, iDb, zWhere); |
danielk1977 | 19a8e7e | 2005-03-17 05:03:38 +0000 | [diff] [blame] | 364 | |
| 365 | #ifndef SQLITE_OMIT_TRIGGER |
| 366 | /* Now, if the table is not stored in the temp database, reload any temp |
| 367 | ** triggers. Don't use IN(...) in case SQLITE_OMIT_SUBQUERY is defined. |
| 368 | */ |
drh | d9cb6ac | 2005-10-20 07:28:17 +0000 | [diff] [blame] | 369 | if( (zWhere=whereTempTriggers(pParse, pTab))!=0 ){ |
drh | 5d9c9da | 2011-06-03 20:11:17 +0000 | [diff] [blame] | 370 | sqlite3VdbeAddParseSchemaOp(v, 1, zWhere); |
danielk1977 | 19a8e7e | 2005-03-17 05:03:38 +0000 | [diff] [blame] | 371 | } |
| 372 | #endif |
| 373 | } |
| 374 | |
| 375 | /* |
dan | be53500 | 2011-04-01 15:15:58 +0000 | [diff] [blame] | 376 | ** Parameter zName is the name of a table that is about to be altered |
| 377 | ** (either with ALTER TABLE ... RENAME TO or ALTER TABLE ... ADD COLUMN). |
| 378 | ** If the table is a system table, this function leaves an error message |
| 379 | ** in pParse->zErr (system tables may not be altered) and returns non-zero. |
| 380 | ** |
| 381 | ** Or, if zName is not a system table, zero is returned. |
| 382 | */ |
| 383 | static int isSystemTable(Parse *pParse, const char *zName){ |
| 384 | if( sqlite3Strlen30(zName)>6 && 0==sqlite3StrNICmp(zName, "sqlite_", 7) ){ |
| 385 | sqlite3ErrorMsg(pParse, "table %s may not be altered", zName); |
| 386 | return 1; |
| 387 | } |
| 388 | return 0; |
| 389 | } |
| 390 | |
| 391 | /* |
drh | d0e4a6c | 2005-02-15 20:47:57 +0000 | [diff] [blame] | 392 | ** Generate code to implement the "ALTER TABLE xxx RENAME TO yyy" |
| 393 | ** command. |
| 394 | */ |
| 395 | void sqlite3AlterRenameTable( |
| 396 | Parse *pParse, /* Parser context. */ |
| 397 | SrcList *pSrc, /* The table to rename. */ |
| 398 | Token *pName /* The new table name. */ |
| 399 | ){ |
| 400 | int iDb; /* Database that contains the table */ |
| 401 | char *zDb; /* Name of database iDb */ |
| 402 | Table *pTab; /* Table being renamed */ |
| 403 | char *zName = 0; /* NULL-terminated version of pName */ |
drh | d0e4a6c | 2005-02-15 20:47:57 +0000 | [diff] [blame] | 404 | sqlite3 *db = pParse->db; /* Database connection */ |
drh | 4e5dd85 | 2007-05-15 03:56:49 +0000 | [diff] [blame] | 405 | int nTabName; /* Number of UTF-8 characters in zTabName */ |
| 406 | const char *zTabName; /* Original name of the table */ |
drh | d0e4a6c | 2005-02-15 20:47:57 +0000 | [diff] [blame] | 407 | Vdbe *v; |
| 408 | #ifndef SQLITE_OMIT_TRIGGER |
danielk1977 | 19a8e7e | 2005-03-17 05:03:38 +0000 | [diff] [blame] | 409 | char *zWhere = 0; /* Where clause to locate temp triggers */ |
drh | d0e4a6c | 2005-02-15 20:47:57 +0000 | [diff] [blame] | 410 | #endif |
danielk1977 | 595a523 | 2009-07-24 17:58:53 +0000 | [diff] [blame] | 411 | VTable *pVTab = 0; /* Non-zero if this is a v-tab with an xRename() */ |
drh | 545f587 | 2010-04-24 14:02:59 +0000 | [diff] [blame] | 412 | int savedDbFlags; /* Saved value of db->flags */ |
| 413 | |
| 414 | savedDbFlags = db->flags; |
drh | 56d56f7 | 2009-04-16 16:30:17 +0000 | [diff] [blame] | 415 | if( NEVER(db->mallocFailed) ) goto exit_rename_table; |
drh | d0e4a6c | 2005-02-15 20:47:57 +0000 | [diff] [blame] | 416 | assert( pSrc->nSrc==1 ); |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 417 | assert( sqlite3BtreeHoldsAllMutexes(pParse->db) ); |
drh | d0e4a6c | 2005-02-15 20:47:57 +0000 | [diff] [blame] | 418 | |
dan | 41fb5cd | 2012-10-04 19:33:00 +0000 | [diff] [blame] | 419 | pTab = sqlite3LocateTableItem(pParse, 0, &pSrc->a[0]); |
drh | d0e4a6c | 2005-02-15 20:47:57 +0000 | [diff] [blame] | 420 | if( !pTab ) goto exit_rename_table; |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 421 | iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema); |
drh | d0e4a6c | 2005-02-15 20:47:57 +0000 | [diff] [blame] | 422 | zDb = db->aDb[iDb].zName; |
drh | 545f587 | 2010-04-24 14:02:59 +0000 | [diff] [blame] | 423 | db->flags |= SQLITE_PreferBuiltin; |
drh | d0e4a6c | 2005-02-15 20:47:57 +0000 | [diff] [blame] | 424 | |
| 425 | /* Get a NULL terminated version of the new table name. */ |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 426 | zName = sqlite3NameFromToken(db, pName); |
drh | d0e4a6c | 2005-02-15 20:47:57 +0000 | [diff] [blame] | 427 | if( !zName ) goto exit_rename_table; |
| 428 | |
| 429 | /* Check that a table or index named 'zName' does not already exist |
| 430 | ** in database iDb. If so, this is an error. |
| 431 | */ |
| 432 | if( sqlite3FindTable(db, zName, zDb) || sqlite3FindIndex(db, zName, zDb) ){ |
| 433 | sqlite3ErrorMsg(pParse, |
| 434 | "there is already another table or index with this name: %s", zName); |
| 435 | goto exit_rename_table; |
| 436 | } |
| 437 | |
| 438 | /* Make sure it is not a system table being altered, or a reserved name |
| 439 | ** that the table is being renamed to. |
| 440 | */ |
dan | be53500 | 2011-04-01 15:15:58 +0000 | [diff] [blame] | 441 | if( SQLITE_OK!=isSystemTable(pParse, pTab->zName) ){ |
drh | d0e4a6c | 2005-02-15 20:47:57 +0000 | [diff] [blame] | 442 | goto exit_rename_table; |
| 443 | } |
dan | be53500 | 2011-04-01 15:15:58 +0000 | [diff] [blame] | 444 | if( SQLITE_OK!=sqlite3CheckObjectName(pParse, zName) ){ goto |
| 445 | exit_rename_table; |
drh | d0e4a6c | 2005-02-15 20:47:57 +0000 | [diff] [blame] | 446 | } |
| 447 | |
danielk1977 | 61116ae | 2007-12-13 08:15:30 +0000 | [diff] [blame] | 448 | #ifndef SQLITE_OMIT_VIEW |
| 449 | if( pTab->pSelect ){ |
| 450 | sqlite3ErrorMsg(pParse, "view %s may not be altered", pTab->zName); |
| 451 | goto exit_rename_table; |
| 452 | } |
| 453 | #endif |
| 454 | |
drh | d0e4a6c | 2005-02-15 20:47:57 +0000 | [diff] [blame] | 455 | #ifndef SQLITE_OMIT_AUTHORIZATION |
| 456 | /* Invoke the authorization callback. */ |
| 457 | if( sqlite3AuthCheck(pParse, SQLITE_ALTER_TABLE, zDb, pTab->zName, 0) ){ |
| 458 | goto exit_rename_table; |
| 459 | } |
| 460 | #endif |
| 461 | |
danielk1977 | 182c4ba | 2007-06-27 15:53:34 +0000 | [diff] [blame] | 462 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
danielk1977 | 5c55886 | 2007-06-27 17:09:24 +0000 | [diff] [blame] | 463 | if( sqlite3ViewGetColumnNames(pParse, pTab) ){ |
| 464 | goto exit_rename_table; |
| 465 | } |
danielk1977 | 595a523 | 2009-07-24 17:58:53 +0000 | [diff] [blame] | 466 | if( IsVirtual(pTab) ){ |
| 467 | pVTab = sqlite3GetVTable(db, pTab); |
| 468 | if( pVTab->pVtab->pModule->xRename==0 ){ |
| 469 | pVTab = 0; |
| 470 | } |
danielk1977 | 182c4ba | 2007-06-27 15:53:34 +0000 | [diff] [blame] | 471 | } |
| 472 | #endif |
| 473 | |
drh | b22f7c8 | 2014-02-06 23:56:27 +0000 | [diff] [blame] | 474 | /* Begin a transaction for database iDb. |
drh | d0e4a6c | 2005-02-15 20:47:57 +0000 | [diff] [blame] | 475 | ** Then modify the schema cookie (since the ALTER TABLE modifies the |
danielk1977 | 182c4ba | 2007-06-27 15:53:34 +0000 | [diff] [blame] | 476 | ** schema). Open a statement transaction if the table is a virtual |
| 477 | ** table. |
drh | d0e4a6c | 2005-02-15 20:47:57 +0000 | [diff] [blame] | 478 | */ |
| 479 | v = sqlite3GetVdbe(pParse); |
| 480 | if( v==0 ){ |
| 481 | goto exit_rename_table; |
| 482 | } |
danielk1977 | 595a523 | 2009-07-24 17:58:53 +0000 | [diff] [blame] | 483 | sqlite3BeginWriteOperation(pParse, pVTab!=0, iDb); |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 484 | sqlite3ChangeCookie(pParse, iDb); |
drh | d0e4a6c | 2005-02-15 20:47:57 +0000 | [diff] [blame] | 485 | |
danielk1977 | 182c4ba | 2007-06-27 15:53:34 +0000 | [diff] [blame] | 486 | /* If this is a virtual table, invoke the xRename() function if |
| 487 | ** one is defined. The xRename() callback will modify the names |
| 488 | ** of any resources used by the v-table implementation (including other |
| 489 | ** SQLite tables) that are identified by the name of the virtual table. |
| 490 | */ |
| 491 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
danielk1977 | 595a523 | 2009-07-24 17:58:53 +0000 | [diff] [blame] | 492 | if( pVTab ){ |
danielk1977 | a29f18c | 2008-01-04 11:01:03 +0000 | [diff] [blame] | 493 | int i = ++pParse->nMem; |
drh | 076e85f | 2015-09-03 13:46:12 +0000 | [diff] [blame] | 494 | sqlite3VdbeLoadString(v, i, zName); |
danielk1977 | 595a523 | 2009-07-24 17:58:53 +0000 | [diff] [blame] | 495 | sqlite3VdbeAddOp4(v, OP_VRename, i, 0, 0,(const char*)pVTab, P4_VTAB); |
dan | e0af83a | 2009-09-08 19:15:01 +0000 | [diff] [blame] | 496 | sqlite3MayAbort(pParse); |
danielk1977 | 182c4ba | 2007-06-27 15:53:34 +0000 | [diff] [blame] | 497 | } |
| 498 | #endif |
| 499 | |
drh | 4e5dd85 | 2007-05-15 03:56:49 +0000 | [diff] [blame] | 500 | /* figure out how many UTF-8 characters are in zName */ |
| 501 | zTabName = pTab->zName; |
drh | 9a087a9 | 2007-05-15 14:34:32 +0000 | [diff] [blame] | 502 | nTabName = sqlite3Utf8CharLen(zTabName, -1); |
drh | 4e5dd85 | 2007-05-15 03:56:49 +0000 | [diff] [blame] | 503 | |
dan | 856ef1a | 2009-09-29 06:33:23 +0000 | [diff] [blame] | 504 | #if !defined(SQLITE_OMIT_FOREIGN_KEY) && !defined(SQLITE_OMIT_TRIGGER) |
dan | 432cc5b | 2009-09-26 17:51:48 +0000 | [diff] [blame] | 505 | if( db->flags&SQLITE_ForeignKeys ){ |
| 506 | /* If foreign-key support is enabled, rewrite the CREATE TABLE |
| 507 | ** statements corresponding to all child tables of foreign key constraints |
| 508 | ** for which the renamed table is the parent table. */ |
| 509 | if( (zWhere=whereForeignKeys(pParse, pTab))!=0 ){ |
| 510 | sqlite3NestedParse(pParse, |
drh | 9a6ffc8 | 2010-02-15 18:03:20 +0000 | [diff] [blame] | 511 | "UPDATE \"%w\".%s SET " |
dan | 432cc5b | 2009-09-26 17:51:48 +0000 | [diff] [blame] | 512 | "sql = sqlite_rename_parent(sql, %Q, %Q) " |
drh | 9a6ffc8 | 2010-02-15 18:03:20 +0000 | [diff] [blame] | 513 | "WHERE %s;", zDb, SCHEMA_TABLE(iDb), zTabName, zName, zWhere); |
dan | 432cc5b | 2009-09-26 17:51:48 +0000 | [diff] [blame] | 514 | sqlite3DbFree(db, zWhere); |
| 515 | } |
| 516 | } |
| 517 | #endif |
| 518 | |
drh | d0e4a6c | 2005-02-15 20:47:57 +0000 | [diff] [blame] | 519 | /* Modify the sqlite_master table to use the new table name. */ |
| 520 | sqlite3NestedParse(pParse, |
| 521 | "UPDATE %Q.%s SET " |
| 522 | #ifdef SQLITE_OMIT_TRIGGER |
| 523 | "sql = sqlite_rename_table(sql, %Q), " |
| 524 | #else |
| 525 | "sql = CASE " |
| 526 | "WHEN type = 'trigger' THEN sqlite_rename_trigger(sql, %Q)" |
| 527 | "ELSE sqlite_rename_table(sql, %Q) END, " |
| 528 | #endif |
| 529 | "tbl_name = %Q, " |
| 530 | "name = CASE " |
| 531 | "WHEN type='table' THEN %Q " |
| 532 | "WHEN name LIKE 'sqlite_autoindex%%' AND type='index' THEN " |
drh | a21a929 | 2007-10-20 20:58:57 +0000 | [diff] [blame] | 533 | "'sqlite_autoindex_' || %Q || substr(name,%d+18) " |
drh | d0e4a6c | 2005-02-15 20:47:57 +0000 | [diff] [blame] | 534 | "ELSE name END " |
drh | 0152268 | 2012-02-01 01:13:10 +0000 | [diff] [blame] | 535 | "WHERE tbl_name=%Q COLLATE nocase AND " |
drh | d0e4a6c | 2005-02-15 20:47:57 +0000 | [diff] [blame] | 536 | "(type='table' OR type='index' OR type='trigger');", |
| 537 | zDb, SCHEMA_TABLE(iDb), zName, zName, zName, |
| 538 | #ifndef SQLITE_OMIT_TRIGGER |
danielk1977 | 19a8e7e | 2005-03-17 05:03:38 +0000 | [diff] [blame] | 539 | zName, |
drh | d0e4a6c | 2005-02-15 20:47:57 +0000 | [diff] [blame] | 540 | #endif |
drh | 4e5dd85 | 2007-05-15 03:56:49 +0000 | [diff] [blame] | 541 | zName, nTabName, zTabName |
drh | d0e4a6c | 2005-02-15 20:47:57 +0000 | [diff] [blame] | 542 | ); |
| 543 | |
| 544 | #ifndef SQLITE_OMIT_AUTOINCREMENT |
| 545 | /* If the sqlite_sequence table exists in this database, then update |
| 546 | ** it with the new table name. |
| 547 | */ |
| 548 | if( sqlite3FindTable(db, "sqlite_sequence", zDb) ){ |
| 549 | sqlite3NestedParse(pParse, |
drh | 8e5b5f8 | 2008-02-09 14:30:29 +0000 | [diff] [blame] | 550 | "UPDATE \"%w\".sqlite_sequence set name = %Q WHERE name = %Q", |
drh | d0e4a6c | 2005-02-15 20:47:57 +0000 | [diff] [blame] | 551 | zDb, zName, pTab->zName); |
| 552 | } |
| 553 | #endif |
| 554 | |
| 555 | #ifndef SQLITE_OMIT_TRIGGER |
| 556 | /* If there are TEMP triggers on this table, modify the sqlite_temp_master |
| 557 | ** table. Don't do this if the table being ALTERed is itself located in |
| 558 | ** the temp database. |
| 559 | */ |
drh | d9cb6ac | 2005-10-20 07:28:17 +0000 | [diff] [blame] | 560 | if( (zWhere=whereTempTriggers(pParse, pTab))!=0 ){ |
danielk1977 | 19a8e7e | 2005-03-17 05:03:38 +0000 | [diff] [blame] | 561 | sqlite3NestedParse(pParse, |
| 562 | "UPDATE sqlite_temp_master SET " |
| 563 | "sql = sqlite_rename_trigger(sql, %Q), " |
| 564 | "tbl_name = %Q " |
| 565 | "WHERE %s;", zName, zName, zWhere); |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 566 | sqlite3DbFree(db, zWhere); |
drh | d0e4a6c | 2005-02-15 20:47:57 +0000 | [diff] [blame] | 567 | } |
| 568 | #endif |
| 569 | |
dan | 856ef1a | 2009-09-29 06:33:23 +0000 | [diff] [blame] | 570 | #if !defined(SQLITE_OMIT_FOREIGN_KEY) && !defined(SQLITE_OMIT_TRIGGER) |
dan | 432cc5b | 2009-09-26 17:51:48 +0000 | [diff] [blame] | 571 | if( db->flags&SQLITE_ForeignKeys ){ |
| 572 | FKey *p; |
| 573 | for(p=sqlite3FkReferences(pTab); p; p=p->pNextTo){ |
| 574 | Table *pFrom = p->pFrom; |
| 575 | if( pFrom!=pTab ){ |
| 576 | reloadTableSchema(pParse, p->pFrom, pFrom->zName); |
| 577 | } |
| 578 | } |
| 579 | } |
| 580 | #endif |
| 581 | |
danielk1977 | 19a8e7e | 2005-03-17 05:03:38 +0000 | [diff] [blame] | 582 | /* Drop and reload the internal table schema. */ |
| 583 | reloadTableSchema(pParse, pTab, zName); |
drh | d0e4a6c | 2005-02-15 20:47:57 +0000 | [diff] [blame] | 584 | |
| 585 | exit_rename_table: |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 586 | sqlite3SrcListDelete(db, pSrc); |
| 587 | sqlite3DbFree(db, zName); |
drh | 545f587 | 2010-04-24 14:02:59 +0000 | [diff] [blame] | 588 | db->flags = savedDbFlags; |
drh | d0e4a6c | 2005-02-15 20:47:57 +0000 | [diff] [blame] | 589 | } |
danielk1977 | 19a8e7e | 2005-03-17 05:03:38 +0000 | [diff] [blame] | 590 | |
| 591 | |
| 592 | /* |
drh | d300171 | 2009-05-12 17:46:53 +0000 | [diff] [blame] | 593 | ** Generate code to make sure the file format number is at least minFormat. |
| 594 | ** The generated code will increase the file format number if necessary. |
| 595 | */ |
| 596 | void sqlite3MinimumFileFormat(Parse *pParse, int iDb, int minFormat){ |
| 597 | Vdbe *v; |
| 598 | v = sqlite3GetVdbe(pParse); |
| 599 | /* The VDBE should have been allocated before this routine is called. |
| 600 | ** If that allocation failed, we would have quit before reaching this |
| 601 | ** point */ |
| 602 | if( ALWAYS(v) ){ |
| 603 | int r1 = sqlite3GetTempReg(pParse); |
| 604 | int r2 = sqlite3GetTempReg(pParse); |
drh | 728e0f9 | 2015-10-10 14:41:28 +0000 | [diff] [blame] | 605 | int addr1; |
danielk1977 | 0d19f7a | 2009-06-03 11:25:07 +0000 | [diff] [blame] | 606 | sqlite3VdbeAddOp3(v, OP_ReadCookie, iDb, r1, BTREE_FILE_FORMAT); |
drh | d300171 | 2009-05-12 17:46:53 +0000 | [diff] [blame] | 607 | sqlite3VdbeUsesBtree(v, iDb); |
| 608 | sqlite3VdbeAddOp2(v, OP_Integer, minFormat, r2); |
drh | 728e0f9 | 2015-10-10 14:41:28 +0000 | [diff] [blame] | 609 | addr1 = sqlite3VdbeAddOp3(v, OP_Ge, r2, 0, r1); |
drh | 3d77dee | 2014-02-19 14:20:49 +0000 | [diff] [blame] | 610 | sqlite3VdbeChangeP5(v, SQLITE_NOTNULL); VdbeCoverage(v); |
danielk1977 | 0d19f7a | 2009-06-03 11:25:07 +0000 | [diff] [blame] | 611 | sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, BTREE_FILE_FORMAT, r2); |
drh | 728e0f9 | 2015-10-10 14:41:28 +0000 | [diff] [blame] | 612 | sqlite3VdbeJumpHere(v, addr1); |
drh | d300171 | 2009-05-12 17:46:53 +0000 | [diff] [blame] | 613 | sqlite3ReleaseTempReg(pParse, r1); |
| 614 | sqlite3ReleaseTempReg(pParse, r2); |
| 615 | } |
| 616 | } |
| 617 | |
| 618 | /* |
danielk1977 | 19a8e7e | 2005-03-17 05:03:38 +0000 | [diff] [blame] | 619 | ** This function is called after an "ALTER TABLE ... ADD" statement |
| 620 | ** has been parsed. Argument pColDef contains the text of the new |
| 621 | ** column definition. |
| 622 | ** |
| 623 | ** The Table structure pParse->pNewTable was extended to include |
| 624 | ** the new column during parsing. |
| 625 | */ |
| 626 | void sqlite3AlterFinishAddColumn(Parse *pParse, Token *pColDef){ |
| 627 | Table *pNew; /* Copy of pParse->pNewTable */ |
| 628 | Table *pTab; /* Table being altered */ |
| 629 | int iDb; /* Database number */ |
| 630 | const char *zDb; /* Database name */ |
| 631 | const char *zTab; /* Table name */ |
| 632 | char *zCol; /* Null-terminated column definition */ |
| 633 | Column *pCol; /* The new column */ |
| 634 | Expr *pDflt; /* Default value for the new column */ |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 635 | sqlite3 *db; /* The database connection; */ |
danielk1977 | 19a8e7e | 2005-03-17 05:03:38 +0000 | [diff] [blame] | 636 | |
danielk1977 | f150c9d | 2008-10-30 17:21:12 +0000 | [diff] [blame] | 637 | db = pParse->db; |
| 638 | if( pParse->nErr || db->mallocFailed ) return; |
danielk1977 | 19a8e7e | 2005-03-17 05:03:38 +0000 | [diff] [blame] | 639 | pNew = pParse->pNewTable; |
| 640 | assert( pNew ); |
| 641 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 642 | assert( sqlite3BtreeHoldsAllMutexes(db) ); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 643 | iDb = sqlite3SchemaToIndex(db, pNew->pSchema); |
| 644 | zDb = db->aDb[iDb].zName; |
drh | 0388123 | 2009-02-13 03:43:31 +0000 | [diff] [blame] | 645 | zTab = &pNew->zName[16]; /* Skip the "sqlite_altertab_" prefix on the name */ |
danielk1977 | 19a8e7e | 2005-03-17 05:03:38 +0000 | [diff] [blame] | 646 | pCol = &pNew->aCol[pNew->nCol-1]; |
| 647 | pDflt = pCol->pDflt; |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 648 | pTab = sqlite3FindTable(db, zTab, zDb); |
danielk1977 | 19a8e7e | 2005-03-17 05:03:38 +0000 | [diff] [blame] | 649 | assert( pTab ); |
| 650 | |
drh | 81f2ccd | 2006-01-31 14:28:44 +0000 | [diff] [blame] | 651 | #ifndef SQLITE_OMIT_AUTHORIZATION |
| 652 | /* Invoke the authorization callback. */ |
| 653 | if( sqlite3AuthCheck(pParse, SQLITE_ALTER_TABLE, zDb, pTab->zName, 0) ){ |
| 654 | return; |
| 655 | } |
| 656 | #endif |
| 657 | |
danielk1977 | 19a8e7e | 2005-03-17 05:03:38 +0000 | [diff] [blame] | 658 | /* If the default value for the new column was specified with a |
| 659 | ** literal NULL, then set pDflt to 0. This simplifies checking |
| 660 | ** for an SQL NULL default below. |
| 661 | */ |
| 662 | if( pDflt && pDflt->op==TK_NULL ){ |
| 663 | pDflt = 0; |
| 664 | } |
| 665 | |
| 666 | /* Check that the new column is not specified as PRIMARY KEY or UNIQUE. |
| 667 | ** If there is a NOT NULL constraint, then the default value for the |
| 668 | ** column must not be NULL. |
| 669 | */ |
drh | a371ace | 2012-09-13 14:22:47 +0000 | [diff] [blame] | 670 | if( pCol->colFlags & COLFLAG_PRIMKEY ){ |
danielk1977 | 19a8e7e | 2005-03-17 05:03:38 +0000 | [diff] [blame] | 671 | sqlite3ErrorMsg(pParse, "Cannot add a PRIMARY KEY column"); |
| 672 | return; |
| 673 | } |
| 674 | if( pNew->pIndex ){ |
| 675 | sqlite3ErrorMsg(pParse, "Cannot add a UNIQUE column"); |
| 676 | return; |
| 677 | } |
dan | 53c3fa8 | 2009-09-25 11:26:54 +0000 | [diff] [blame] | 678 | if( (db->flags&SQLITE_ForeignKeys) && pNew->pFKey && pDflt ){ |
| 679 | sqlite3ErrorMsg(pParse, |
| 680 | "Cannot add a REFERENCES column with non-NULL default value"); |
| 681 | return; |
| 682 | } |
danielk1977 | 19a8e7e | 2005-03-17 05:03:38 +0000 | [diff] [blame] | 683 | if( pCol->notNull && !pDflt ){ |
| 684 | sqlite3ErrorMsg(pParse, |
| 685 | "Cannot add a NOT NULL column with default value NULL"); |
| 686 | return; |
| 687 | } |
| 688 | |
| 689 | /* Ensure the default expression is something that sqlite3ValueFromExpr() |
| 690 | ** can handle (i.e. not CURRENT_TIME etc.) |
| 691 | */ |
| 692 | if( pDflt ){ |
dan | 7a41923 | 2013-08-06 20:01:43 +0000 | [diff] [blame] | 693 | sqlite3_value *pVal = 0; |
drh | 96f4ad2 | 2015-03-12 21:02:36 +0000 | [diff] [blame] | 694 | int rc; |
drh | 05883a3 | 2015-06-02 15:32:08 +0000 | [diff] [blame] | 695 | rc = sqlite3ValueFromExpr(db, pDflt, SQLITE_UTF8, SQLITE_AFF_BLOB, &pVal); |
drh | 96f4ad2 | 2015-03-12 21:02:36 +0000 | [diff] [blame] | 696 | assert( rc==SQLITE_OK || rc==SQLITE_NOMEM ); |
| 697 | if( rc!=SQLITE_OK ){ |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 698 | db->mallocFailed = 1; |
danielk1977 | 19a8e7e | 2005-03-17 05:03:38 +0000 | [diff] [blame] | 699 | return; |
| 700 | } |
| 701 | if( !pVal ){ |
| 702 | sqlite3ErrorMsg(pParse, "Cannot add a column with non-constant default"); |
| 703 | return; |
| 704 | } |
| 705 | sqlite3ValueFree(pVal); |
| 706 | } |
| 707 | |
| 708 | /* Modify the CREATE TABLE statement. */ |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 709 | zCol = sqlite3DbStrNDup(db, (char*)pColDef->z, pColDef->n); |
danielk1977 | 19a8e7e | 2005-03-17 05:03:38 +0000 | [diff] [blame] | 710 | if( zCol ){ |
| 711 | char *zEnd = &zCol[pColDef->n-1]; |
drh | 545f587 | 2010-04-24 14:02:59 +0000 | [diff] [blame] | 712 | int savedDbFlags = db->flags; |
drh | 56d56f7 | 2009-04-16 16:30:17 +0000 | [diff] [blame] | 713 | while( zEnd>zCol && (*zEnd==';' || sqlite3Isspace(*zEnd)) ){ |
danielk1977 | 19a8e7e | 2005-03-17 05:03:38 +0000 | [diff] [blame] | 714 | *zEnd-- = '\0'; |
| 715 | } |
drh | 545f587 | 2010-04-24 14:02:59 +0000 | [diff] [blame] | 716 | db->flags |= SQLITE_PreferBuiltin; |
danielk1977 | 19a8e7e | 2005-03-17 05:03:38 +0000 | [diff] [blame] | 717 | sqlite3NestedParse(pParse, |
drh | 8e5b5f8 | 2008-02-09 14:30:29 +0000 | [diff] [blame] | 718 | "UPDATE \"%w\".%s SET " |
drh | a21a929 | 2007-10-20 20:58:57 +0000 | [diff] [blame] | 719 | "sql = substr(sql,1,%d) || ', ' || %Q || substr(sql,%d) " |
danielk1977 | 19a8e7e | 2005-03-17 05:03:38 +0000 | [diff] [blame] | 720 | "WHERE type = 'table' AND name = %Q", |
| 721 | zDb, SCHEMA_TABLE(iDb), pNew->addColOffset, zCol, pNew->addColOffset+1, |
| 722 | zTab |
| 723 | ); |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 724 | sqlite3DbFree(db, zCol); |
drh | 545f587 | 2010-04-24 14:02:59 +0000 | [diff] [blame] | 725 | db->flags = savedDbFlags; |
danielk1977 | 19a8e7e | 2005-03-17 05:03:38 +0000 | [diff] [blame] | 726 | } |
| 727 | |
| 728 | /* If the default value of the new column is NULL, then set the file |
| 729 | ** format to 2. If the default value of the new column is not NULL, |
| 730 | ** the file format becomes 3. |
| 731 | */ |
drh | fdd6e85 | 2005-12-16 01:06:16 +0000 | [diff] [blame] | 732 | sqlite3MinimumFileFormat(pParse, iDb, pDflt ? 3 : 2); |
danielk1977 | 19a8e7e | 2005-03-17 05:03:38 +0000 | [diff] [blame] | 733 | |
| 734 | /* Reload the schema of the modified table. */ |
| 735 | reloadTableSchema(pParse, pTab, pTab->zName); |
| 736 | } |
| 737 | |
drh | fdd6e85 | 2005-12-16 01:06:16 +0000 | [diff] [blame] | 738 | /* |
danielk1977 | 19a8e7e | 2005-03-17 05:03:38 +0000 | [diff] [blame] | 739 | ** This function is called by the parser after the table-name in |
| 740 | ** an "ALTER TABLE <table-name> ADD" statement is parsed. Argument |
| 741 | ** pSrc is the full-name of the table being altered. |
| 742 | ** |
| 743 | ** This routine makes a (partial) copy of the Table structure |
| 744 | ** for the table being altered and sets Parse.pNewTable to point |
| 745 | ** to it. Routines called by the parser as the column definition |
| 746 | ** is parsed (i.e. sqlite3AddColumn()) add the new Column data to |
| 747 | ** the copy. The copy of the Table structure is deleted by tokenize.c |
| 748 | ** after parsing is finished. |
| 749 | ** |
| 750 | ** Routine sqlite3AlterFinishAddColumn() will be called to complete |
| 751 | ** coding the "ALTER TABLE ... ADD" statement. |
| 752 | */ |
| 753 | void sqlite3AlterBeginAddColumn(Parse *pParse, SrcList *pSrc){ |
| 754 | Table *pNew; |
| 755 | Table *pTab; |
| 756 | Vdbe *v; |
| 757 | int iDb; |
| 758 | int i; |
| 759 | int nAlloc; |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 760 | sqlite3 *db = pParse->db; |
danielk1977 | 19a8e7e | 2005-03-17 05:03:38 +0000 | [diff] [blame] | 761 | |
| 762 | /* Look up the table being altered. */ |
drh | 0bbaa1b | 2005-08-19 19:14:12 +0000 | [diff] [blame] | 763 | assert( pParse->pNewTable==0 ); |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 764 | assert( sqlite3BtreeHoldsAllMutexes(db) ); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 765 | if( db->mallocFailed ) goto exit_begin_add_column; |
dan | 41fb5cd | 2012-10-04 19:33:00 +0000 | [diff] [blame] | 766 | pTab = sqlite3LocateTableItem(pParse, 0, &pSrc->a[0]); |
danielk1977 | 19a8e7e | 2005-03-17 05:03:38 +0000 | [diff] [blame] | 767 | if( !pTab ) goto exit_begin_add_column; |
| 768 | |
danielk1977 | 5ee9d69 | 2006-06-21 12:36:25 +0000 | [diff] [blame] | 769 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
| 770 | if( IsVirtual(pTab) ){ |
| 771 | sqlite3ErrorMsg(pParse, "virtual tables may not be altered"); |
| 772 | goto exit_begin_add_column; |
| 773 | } |
| 774 | #endif |
| 775 | |
danielk1977 | 19a8e7e | 2005-03-17 05:03:38 +0000 | [diff] [blame] | 776 | /* Make sure this is not an attempt to ALTER a view. */ |
| 777 | if( pTab->pSelect ){ |
| 778 | sqlite3ErrorMsg(pParse, "Cannot add a column to a view"); |
| 779 | goto exit_begin_add_column; |
| 780 | } |
dan | be53500 | 2011-04-01 15:15:58 +0000 | [diff] [blame] | 781 | if( SQLITE_OK!=isSystemTable(pParse, pTab->zName) ){ |
| 782 | goto exit_begin_add_column; |
| 783 | } |
danielk1977 | 19a8e7e | 2005-03-17 05:03:38 +0000 | [diff] [blame] | 784 | |
| 785 | assert( pTab->addColOffset>0 ); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 786 | iDb = sqlite3SchemaToIndex(db, pTab->pSchema); |
danielk1977 | 19a8e7e | 2005-03-17 05:03:38 +0000 | [diff] [blame] | 787 | |
| 788 | /* Put a copy of the Table struct in Parse.pNewTable for the |
drh | 0388123 | 2009-02-13 03:43:31 +0000 | [diff] [blame] | 789 | ** sqlite3AddColumn() function and friends to modify. But modify |
| 790 | ** the name by adding an "sqlite_altertab_" prefix. By adding this |
| 791 | ** prefix, we insure that the name will not collide with an existing |
| 792 | ** table because user table are not allowed to have the "sqlite_" |
| 793 | ** prefix on their name. |
danielk1977 | 19a8e7e | 2005-03-17 05:03:38 +0000 | [diff] [blame] | 794 | */ |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 795 | pNew = (Table*)sqlite3DbMallocZero(db, sizeof(Table)); |
danielk1977 | 19a8e7e | 2005-03-17 05:03:38 +0000 | [diff] [blame] | 796 | if( !pNew ) goto exit_begin_add_column; |
| 797 | pParse->pNewTable = pNew; |
drh | 0bbaa1b | 2005-08-19 19:14:12 +0000 | [diff] [blame] | 798 | pNew->nRef = 1; |
danielk1977 | 19a8e7e | 2005-03-17 05:03:38 +0000 | [diff] [blame] | 799 | pNew->nCol = pTab->nCol; |
danielk1977 | b3a2cce | 2005-03-27 01:56:30 +0000 | [diff] [blame] | 800 | assert( pNew->nCol>0 ); |
| 801 | nAlloc = (((pNew->nCol-1)/8)*8)+8; |
| 802 | assert( nAlloc>=pNew->nCol && nAlloc%8==0 && nAlloc-pNew->nCol<8 ); |
danielk1977 | 26783a5 | 2007-08-29 14:06:22 +0000 | [diff] [blame] | 803 | pNew->aCol = (Column*)sqlite3DbMallocZero(db, sizeof(Column)*nAlloc); |
drh | 0388123 | 2009-02-13 03:43:31 +0000 | [diff] [blame] | 804 | pNew->zName = sqlite3MPrintf(db, "sqlite_altertab_%s", pTab->zName); |
danielk1977 | 19a8e7e | 2005-03-17 05:03:38 +0000 | [diff] [blame] | 805 | if( !pNew->aCol || !pNew->zName ){ |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 806 | db->mallocFailed = 1; |
danielk1977 | 19a8e7e | 2005-03-17 05:03:38 +0000 | [diff] [blame] | 807 | goto exit_begin_add_column; |
| 808 | } |
| 809 | memcpy(pNew->aCol, pTab->aCol, sizeof(Column)*pNew->nCol); |
| 810 | for(i=0; i<pNew->nCol; i++){ |
| 811 | Column *pCol = &pNew->aCol[i]; |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 812 | pCol->zName = sqlite3DbStrDup(db, pCol->zName); |
drh | ff22e18 | 2006-02-09 02:56:02 +0000 | [diff] [blame] | 813 | pCol->zColl = 0; |
danielk1977 | 19a8e7e | 2005-03-17 05:03:38 +0000 | [diff] [blame] | 814 | pCol->zType = 0; |
| 815 | pCol->pDflt = 0; |
drh | b7916a7 | 2009-05-27 10:31:29 +0000 | [diff] [blame] | 816 | pCol->zDflt = 0; |
danielk1977 | 19a8e7e | 2005-03-17 05:03:38 +0000 | [diff] [blame] | 817 | } |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 818 | pNew->pSchema = db->aDb[iDb].pSchema; |
danielk1977 | 19a8e7e | 2005-03-17 05:03:38 +0000 | [diff] [blame] | 819 | pNew->addColOffset = pTab->addColOffset; |
drh | ed8a3bb | 2005-06-06 21:19:56 +0000 | [diff] [blame] | 820 | pNew->nRef = 1; |
danielk1977 | 19a8e7e | 2005-03-17 05:03:38 +0000 | [diff] [blame] | 821 | |
| 822 | /* Begin a transaction and increment the schema cookie. */ |
| 823 | sqlite3BeginWriteOperation(pParse, 0, iDb); |
| 824 | v = sqlite3GetVdbe(pParse); |
| 825 | if( !v ) goto exit_begin_add_column; |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 826 | sqlite3ChangeCookie(pParse, iDb); |
danielk1977 | 19a8e7e | 2005-03-17 05:03:38 +0000 | [diff] [blame] | 827 | |
| 828 | exit_begin_add_column: |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 829 | sqlite3SrcListDelete(db, pSrc); |
danielk1977 | 19a8e7e | 2005-03-17 05:03:38 +0000 | [diff] [blame] | 830 | return; |
| 831 | } |
drh | d0e4a6c | 2005-02-15 20:47:57 +0000 | [diff] [blame] | 832 | #endif /* SQLITE_ALTER_TABLE */ |