OLD | NEW |
1 /* | 1 /* |
2 ** 2005 February 15 | 2 ** 2005 February 15 |
3 ** | 3 ** |
4 ** The author disclaims copyright to this source code. In place of | 4 ** The author disclaims copyright to this source code. In place of |
5 ** a legal notice, here is a blessing: | 5 ** a legal notice, here is a blessing: |
6 ** | 6 ** |
7 ** May you do good and not evil. | 7 ** May you do good and not evil. |
8 ** May you find forgiveness for yourself and forgive others. | 8 ** May you find forgiveness for yourself and forgive others. |
9 ** May you share freely, never taking more than you give. | 9 ** May you share freely, never taking more than you give. |
10 ** | 10 ** |
11 ************************************************************************* | 11 ************************************************************************* |
12 ** This file contains C code routines that used to generate VDBE code | 12 ** This file contains C code routines that used to generate VDBE code |
13 ** that implements the ALTER TABLE command. | 13 ** that implements the ALTER TABLE command. |
14 ** | |
15 ** $Id: alter.c,v 1.62 2009/07/24 17:58:53 danielk1977 Exp $ | |
16 */ | 14 */ |
17 #include "sqliteInt.h" | 15 #include "sqliteInt.h" |
18 | 16 |
19 /* | 17 /* |
20 ** The code in this file only exists if we are not omitting the | 18 ** The code in this file only exists if we are not omitting the |
21 ** ALTER TABLE logic from the build. | 19 ** ALTER TABLE logic from the build. |
22 */ | 20 */ |
23 #ifndef SQLITE_OMIT_ALTERTABLE | 21 #ifndef SQLITE_OMIT_ALTERTABLE |
24 | 22 |
25 | 23 |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
78 } while( token==TK_SPACE ); | 76 } while( token==TK_SPACE ); |
79 assert( len>0 ); | 77 assert( len>0 ); |
80 } while( token!=TK_LP && token!=TK_USING ); | 78 } while( token!=TK_LP && token!=TK_USING ); |
81 | 79 |
82 zRet = sqlite3MPrintf(db, "%.*s\"%w\"%s", ((u8*)tname.z) - zSql, zSql, | 80 zRet = sqlite3MPrintf(db, "%.*s\"%w\"%s", ((u8*)tname.z) - zSql, zSql, |
83 zTableName, tname.z+tname.n); | 81 zTableName, tname.z+tname.n); |
84 sqlite3_result_text(context, zRet, -1, SQLITE_DYNAMIC); | 82 sqlite3_result_text(context, zRet, -1, SQLITE_DYNAMIC); |
85 } | 83 } |
86 } | 84 } |
87 | 85 |
| 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 |
| 118 UNUSED_PARAMETER(NotUsed); |
| 119 for(z=zInput; *z; z=z+n){ |
| 120 n = sqlite3GetToken(z, &token); |
| 121 if( token==TK_REFERENCES ){ |
| 122 char *zParent; |
| 123 do { |
| 124 z += n; |
| 125 n = sqlite3GetToken(z, &token); |
| 126 }while( token==TK_SPACE ); |
| 127 |
| 128 zParent = sqlite3DbStrNDup(db, (const char *)z, n); |
| 129 if( zParent==0 ) break; |
| 130 sqlite3Dequote(zParent); |
| 131 if( 0==sqlite3StrICmp((const char *)zOld, zParent) ){ |
| 132 char *zOut = sqlite3MPrintf(db, "%s%.*s\"%w\"", |
| 133 (zOutput?zOutput:""), z-zInput, zInput, (const char *)zNew |
| 134 ); |
| 135 sqlite3DbFree(db, zOutput); |
| 136 zOutput = zOut; |
| 137 zInput = &z[n]; |
| 138 } |
| 139 sqlite3DbFree(db, zParent); |
| 140 } |
| 141 } |
| 142 |
| 143 zResult = sqlite3MPrintf(db, "%s%s", (zOutput?zOutput:""), zInput), |
| 144 sqlite3_result_text(context, zResult, -1, SQLITE_DYNAMIC); |
| 145 sqlite3DbFree(db, zOutput); |
| 146 } |
| 147 #endif |
| 148 |
88 #ifndef SQLITE_OMIT_TRIGGER | 149 #ifndef SQLITE_OMIT_TRIGGER |
89 /* This function is used by SQL generated to implement the | 150 /* This function is used by SQL generated to implement the |
90 ** ALTER TABLE command. The first argument is the text of a CREATE TRIGGER | 151 ** ALTER TABLE command. The first argument is the text of a CREATE TRIGGER |
91 ** statement. The second is a table name. The table name in the CREATE | 152 ** statement. The second is a table name. The table name in the CREATE |
92 ** TRIGGER statement is replaced with the third argument and the result | 153 ** TRIGGER statement is replaced with the third argument and the result |
93 ** returned. This is analagous to renameTableFunc() above, except for CREATE | 154 ** returned. This is analagous to renameTableFunc() above, except for CREATE |
94 ** TRIGGER, not CREATE INDEX and CREATE TABLE. | 155 ** TRIGGER, not CREATE INDEX and CREATE TABLE. |
95 */ | 156 */ |
96 static void renameTriggerFunc( | 157 static void renameTriggerFunc( |
97 sqlite3_context *context, | 158 sqlite3_context *context, |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
158 zRet = sqlite3MPrintf(db, "%.*s\"%w\"%s", ((u8*)tname.z) - zSql, zSql, | 219 zRet = sqlite3MPrintf(db, "%.*s\"%w\"%s", ((u8*)tname.z) - zSql, zSql, |
159 zTableName, tname.z+tname.n); | 220 zTableName, tname.z+tname.n); |
160 sqlite3_result_text(context, zRet, -1, SQLITE_DYNAMIC); | 221 sqlite3_result_text(context, zRet, -1, SQLITE_DYNAMIC); |
161 } | 222 } |
162 } | 223 } |
163 #endif /* !SQLITE_OMIT_TRIGGER */ | 224 #endif /* !SQLITE_OMIT_TRIGGER */ |
164 | 225 |
165 /* | 226 /* |
166 ** Register built-in functions used to help implement ALTER TABLE | 227 ** Register built-in functions used to help implement ALTER TABLE |
167 */ | 228 */ |
168 void sqlite3AlterFunctions(sqlite3 *db){ | 229 void sqlite3AlterFunctions(void){ |
169 sqlite3CreateFunc(db, "sqlite_rename_table", 2, SQLITE_UTF8, 0, | 230 static SQLITE_WSD FuncDef aAlterTableFuncs[] = { |
170 renameTableFunc, 0, 0); | 231 FUNCTION(sqlite_rename_table, 2, 0, 0, renameTableFunc), |
171 #ifndef SQLITE_OMIT_TRIGGER | 232 #ifndef SQLITE_OMIT_TRIGGER |
172 sqlite3CreateFunc(db, "sqlite_rename_trigger", 2, SQLITE_UTF8, 0, | 233 FUNCTION(sqlite_rename_trigger, 2, 0, 0, renameTriggerFunc), |
173 renameTriggerFunc, 0, 0); | |
174 #endif | 234 #endif |
| 235 #ifndef SQLITE_OMIT_FOREIGN_KEY |
| 236 FUNCTION(sqlite_rename_parent, 3, 0, 0, renameParentFunc), |
| 237 #endif |
| 238 }; |
| 239 int i; |
| 240 FuncDefHash *pHash = &GLOBAL(FuncDefHash, sqlite3GlobalFunctions); |
| 241 FuncDef *aFunc = (FuncDef*)&GLOBAL(FuncDef, aAlterTableFuncs); |
| 242 |
| 243 for(i=0; i<ArraySize(aAlterTableFuncs); i++){ |
| 244 sqlite3FuncDefInsert(pHash, &aFunc[i]); |
| 245 } |
175 } | 246 } |
176 | 247 |
177 /* | 248 /* |
| 249 ** This function is used to create the text of expressions of the form: |
| 250 ** |
| 251 ** name=<constant1> OR name=<constant2> OR ... |
| 252 ** |
| 253 ** If argument zWhere is NULL, then a pointer string containing the text |
| 254 ** "name=<constant>" is returned, where <constant> is the quoted version |
| 255 ** of the string passed as argument zConstant. The returned buffer is |
| 256 ** allocated using sqlite3DbMalloc(). It is the responsibility of the |
| 257 ** caller to ensure that it is eventually freed. |
| 258 ** |
| 259 ** If argument zWhere is not NULL, then the string returned is |
| 260 ** "<where> OR name=<constant>", where <where> is the contents of zWhere. |
| 261 ** In this case zWhere is passed to sqlite3DbFree() before returning. |
| 262 ** |
| 263 */ |
| 264 static char *whereOrName(sqlite3 *db, char *zWhere, char *zConstant){ |
| 265 char *zNew; |
| 266 if( !zWhere ){ |
| 267 zNew = sqlite3MPrintf(db, "name=%Q", zConstant); |
| 268 }else{ |
| 269 zNew = sqlite3MPrintf(db, "%s OR name=%Q", zWhere, zConstant); |
| 270 sqlite3DbFree(db, zWhere); |
| 271 } |
| 272 return zNew; |
| 273 } |
| 274 |
| 275 #if !defined(SQLITE_OMIT_FOREIGN_KEY) && !defined(SQLITE_OMIT_TRIGGER) |
| 276 /* |
| 277 ** Generate the text of a WHERE expression which can be used to select all |
| 278 ** tables that have foreign key constraints that refer to table pTab (i.e. |
| 279 ** constraints for which pTab is the parent table) from the sqlite_master |
| 280 ** table. |
| 281 */ |
| 282 static char *whereForeignKeys(Parse *pParse, Table *pTab){ |
| 283 FKey *p; |
| 284 char *zWhere = 0; |
| 285 for(p=sqlite3FkReferences(pTab); p; p=p->pNextTo){ |
| 286 zWhere = whereOrName(pParse->db, zWhere, p->pFrom->zName); |
| 287 } |
| 288 return zWhere; |
| 289 } |
| 290 #endif |
| 291 |
| 292 /* |
178 ** Generate the text of a WHERE expression which can be used to select all | 293 ** Generate the text of a WHERE expression which can be used to select all |
179 ** temporary triggers on table pTab from the sqlite_temp_master table. If | 294 ** temporary triggers on table pTab from the sqlite_temp_master table. If |
180 ** table pTab has no temporary triggers, or is itself stored in the | 295 ** table pTab has no temporary triggers, or is itself stored in the |
181 ** temporary database, NULL is returned. | 296 ** temporary database, NULL is returned. |
182 */ | 297 */ |
183 static char *whereTempTriggers(Parse *pParse, Table *pTab){ | 298 static char *whereTempTriggers(Parse *pParse, Table *pTab){ |
184 Trigger *pTrig; | 299 Trigger *pTrig; |
185 char *zWhere = 0; | 300 char *zWhere = 0; |
186 char *tmp = 0; | |
187 const Schema *pTempSchema = pParse->db->aDb[1].pSchema; /* Temp db schema */ | 301 const Schema *pTempSchema = pParse->db->aDb[1].pSchema; /* Temp db schema */ |
188 | 302 |
189 /* If the table is not located in the temp-db (in which case NULL is | 303 /* If the table is not located in the temp-db (in which case NULL is |
190 ** returned, loop through the tables list of triggers. For each trigger | 304 ** returned, loop through the tables list of triggers. For each trigger |
191 ** that is not part of the temp-db schema, add a clause to the WHERE | 305 ** that is not part of the temp-db schema, add a clause to the WHERE |
192 ** expression being built up in zWhere. | 306 ** expression being built up in zWhere. |
193 */ | 307 */ |
194 if( pTab->pSchema!=pTempSchema ){ | 308 if( pTab->pSchema!=pTempSchema ){ |
195 sqlite3 *db = pParse->db; | 309 sqlite3 *db = pParse->db; |
196 for(pTrig=sqlite3TriggerList(pParse, pTab); pTrig; pTrig=pTrig->pNext){ | 310 for(pTrig=sqlite3TriggerList(pParse, pTab); pTrig; pTrig=pTrig->pNext){ |
197 if( pTrig->pSchema==pTempSchema ){ | 311 if( pTrig->pSchema==pTempSchema ){ |
198 if( !zWhere ){ | 312 zWhere = whereOrName(db, zWhere, pTrig->zName); |
199 zWhere = sqlite3MPrintf(db, "name=%Q", pTrig->zName); | |
200 }else{ | |
201 tmp = zWhere; | |
202 zWhere = sqlite3MPrintf(db, "%s OR name=%Q", zWhere, pTrig->zName); | |
203 sqlite3DbFree(db, tmp); | |
204 } | |
205 } | 313 } |
206 } | 314 } |
207 } | 315 } |
| 316 if( zWhere ){ |
| 317 char *zNew = sqlite3MPrintf(pParse->db, "type='trigger' AND (%s)", zWhere); |
| 318 sqlite3DbFree(pParse->db, zWhere); |
| 319 zWhere = zNew; |
| 320 } |
208 return zWhere; | 321 return zWhere; |
209 } | 322 } |
210 | 323 |
211 /* | 324 /* |
212 ** Generate code to drop and reload the internal representation of table | 325 ** Generate code to drop and reload the internal representation of table |
213 ** pTab from the database, including triggers and temporary triggers. | 326 ** pTab from the database, including triggers and temporary triggers. |
214 ** Argument zName is the name of the table in the database schema at | 327 ** Argument zName is the name of the table in the database schema at |
215 ** the time the generated code is executed. This can be different from | 328 ** the time the generated code is executed. This can be different from |
216 ** pTab->zName if this function is being called to code part of an | 329 ** pTab->zName if this function is being called to code part of an |
217 ** "ALTER TABLE RENAME TO" statement. | 330 ** "ALTER TABLE RENAME TO" statement. |
(...skipping 14 matching lines...) Expand all Loading... |
232 | 345 |
233 #ifndef SQLITE_OMIT_TRIGGER | 346 #ifndef SQLITE_OMIT_TRIGGER |
234 /* Drop any table triggers from the internal schema. */ | 347 /* Drop any table triggers from the internal schema. */ |
235 for(pTrig=sqlite3TriggerList(pParse, pTab); pTrig; pTrig=pTrig->pNext){ | 348 for(pTrig=sqlite3TriggerList(pParse, pTab); pTrig; pTrig=pTrig->pNext){ |
236 int iTrigDb = sqlite3SchemaToIndex(pParse->db, pTrig->pSchema); | 349 int iTrigDb = sqlite3SchemaToIndex(pParse->db, pTrig->pSchema); |
237 assert( iTrigDb==iDb || iTrigDb==1 ); | 350 assert( iTrigDb==iDb || iTrigDb==1 ); |
238 sqlite3VdbeAddOp4(v, OP_DropTrigger, iTrigDb, 0, 0, pTrig->zName, 0); | 351 sqlite3VdbeAddOp4(v, OP_DropTrigger, iTrigDb, 0, 0, pTrig->zName, 0); |
239 } | 352 } |
240 #endif | 353 #endif |
241 | 354 |
242 /* Drop the table and index from the internal schema */ | 355 /* Drop the table and index from the internal schema. */ |
243 sqlite3VdbeAddOp4(v, OP_DropTable, iDb, 0, 0, pTab->zName, 0); | 356 sqlite3VdbeAddOp4(v, OP_DropTable, iDb, 0, 0, pTab->zName, 0); |
244 | 357 |
245 /* Reload the table, index and permanent trigger schemas. */ | 358 /* Reload the table, index and permanent trigger schemas. */ |
246 zWhere = sqlite3MPrintf(pParse->db, "tbl_name=%Q", zName); | 359 zWhere = sqlite3MPrintf(pParse->db, "tbl_name=%Q", zName); |
247 if( !zWhere ) return; | 360 if( !zWhere ) return; |
248 sqlite3VdbeAddOp4(v, OP_ParseSchema, iDb, 0, 0, zWhere, P4_DYNAMIC); | 361 sqlite3VdbeAddOp4(v, OP_ParseSchema, iDb, 0, 0, zWhere, P4_DYNAMIC); |
249 | 362 |
250 #ifndef SQLITE_OMIT_TRIGGER | 363 #ifndef SQLITE_OMIT_TRIGGER |
251 /* Now, if the table is not stored in the temp database, reload any temp | 364 /* Now, if the table is not stored in the temp database, reload any temp |
252 ** triggers. Don't use IN(...) in case SQLITE_OMIT_SUBQUERY is defined. | 365 ** triggers. Don't use IN(...) in case SQLITE_OMIT_SUBQUERY is defined. |
253 */ | 366 */ |
254 if( (zWhere=whereTempTriggers(pParse, pTab))!=0 ){ | 367 if( (zWhere=whereTempTriggers(pParse, pTab))!=0 ){ |
255 sqlite3VdbeAddOp4(v, OP_ParseSchema, 1, 0, 0, zWhere, P4_DYNAMIC); | 368 sqlite3VdbeAddOp4(v, OP_ParseSchema, 1, 0, 0, zWhere, P4_DYNAMIC); |
256 } | 369 } |
257 #endif | 370 #endif |
258 } | 371 } |
259 | 372 |
260 /* | 373 /* |
| 374 ** Parameter zName is the name of a table that is about to be altered |
| 375 ** (either with ALTER TABLE ... RENAME TO or ALTER TABLE ... ADD COLUMN). |
| 376 ** If the table is a system table, this function leaves an error message |
| 377 ** in pParse->zErr (system tables may not be altered) and returns non-zero. |
| 378 ** |
| 379 ** Or, if zName is not a system table, zero is returned. |
| 380 */ |
| 381 static int isSystemTable(Parse *pParse, const char *zName){ |
| 382 if( sqlite3Strlen30(zName)>6 && 0==sqlite3StrNICmp(zName, "sqlite_", 7) ){ |
| 383 sqlite3ErrorMsg(pParse, "table %s may not be altered", zName); |
| 384 return 1; |
| 385 } |
| 386 return 0; |
| 387 } |
| 388 |
| 389 /* |
261 ** Generate code to implement the "ALTER TABLE xxx RENAME TO yyy" | 390 ** Generate code to implement the "ALTER TABLE xxx RENAME TO yyy" |
262 ** command. | 391 ** command. |
263 */ | 392 */ |
264 void sqlite3AlterRenameTable( | 393 void sqlite3AlterRenameTable( |
265 Parse *pParse, /* Parser context. */ | 394 Parse *pParse, /* Parser context. */ |
266 SrcList *pSrc, /* The table to rename. */ | 395 SrcList *pSrc, /* The table to rename. */ |
267 Token *pName /* The new table name. */ | 396 Token *pName /* The new table name. */ |
268 ){ | 397 ){ |
269 int iDb; /* Database that contains the table */ | 398 int iDb; /* Database that contains the table */ |
270 char *zDb; /* Name of database iDb */ | 399 char *zDb; /* Name of database iDb */ |
271 Table *pTab; /* Table being renamed */ | 400 Table *pTab; /* Table being renamed */ |
272 char *zName = 0; /* NULL-terminated version of pName */ | 401 char *zName = 0; /* NULL-terminated version of pName */ |
273 sqlite3 *db = pParse->db; /* Database connection */ | 402 sqlite3 *db = pParse->db; /* Database connection */ |
274 int nTabName; /* Number of UTF-8 characters in zTabName */ | 403 int nTabName; /* Number of UTF-8 characters in zTabName */ |
275 const char *zTabName; /* Original name of the table */ | 404 const char *zTabName; /* Original name of the table */ |
276 Vdbe *v; | 405 Vdbe *v; |
277 #ifndef SQLITE_OMIT_TRIGGER | 406 #ifndef SQLITE_OMIT_TRIGGER |
278 char *zWhere = 0; /* Where clause to locate temp triggers */ | 407 char *zWhere = 0; /* Where clause to locate temp triggers */ |
279 #endif | 408 #endif |
280 VTable *pVTab = 0; /* Non-zero if this is a v-tab with an xRename() */ | 409 VTable *pVTab = 0; /* Non-zero if this is a v-tab with an xRename() */ |
281 | 410 int savedDbFlags; /* Saved value of db->flags */ |
| 411 |
| 412 savedDbFlags = db->flags; |
282 if( NEVER(db->mallocFailed) ) goto exit_rename_table; | 413 if( NEVER(db->mallocFailed) ) goto exit_rename_table; |
283 assert( pSrc->nSrc==1 ); | 414 assert( pSrc->nSrc==1 ); |
284 assert( sqlite3BtreeHoldsAllMutexes(pParse->db) ); | 415 assert( sqlite3BtreeHoldsAllMutexes(pParse->db) ); |
285 | 416 |
286 pTab = sqlite3LocateTable(pParse, 0, pSrc->a[0].zName, pSrc->a[0].zDatabase); | 417 pTab = sqlite3LocateTable(pParse, 0, pSrc->a[0].zName, pSrc->a[0].zDatabase); |
287 if( !pTab ) goto exit_rename_table; | 418 if( !pTab ) goto exit_rename_table; |
288 iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema); | 419 iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema); |
289 zDb = db->aDb[iDb].zName; | 420 zDb = db->aDb[iDb].zName; |
| 421 db->flags |= SQLITE_PreferBuiltin; |
290 | 422 |
291 /* Get a NULL terminated version of the new table name. */ | 423 /* Get a NULL terminated version of the new table name. */ |
292 zName = sqlite3NameFromToken(db, pName); | 424 zName = sqlite3NameFromToken(db, pName); |
293 if( !zName ) goto exit_rename_table; | 425 if( !zName ) goto exit_rename_table; |
294 | 426 |
295 /* Check that a table or index named 'zName' does not already exist | 427 /* Check that a table or index named 'zName' does not already exist |
296 ** in database iDb. If so, this is an error. | 428 ** in database iDb. If so, this is an error. |
297 */ | 429 */ |
298 if( sqlite3FindTable(db, zName, zDb) || sqlite3FindIndex(db, zName, zDb) ){ | 430 if( sqlite3FindTable(db, zName, zDb) || sqlite3FindIndex(db, zName, zDb) ){ |
299 sqlite3ErrorMsg(pParse, | 431 sqlite3ErrorMsg(pParse, |
300 "there is already another table or index with this name: %s", zName); | 432 "there is already another table or index with this name: %s", zName); |
301 goto exit_rename_table; | 433 goto exit_rename_table; |
302 } | 434 } |
303 | 435 |
304 /* Make sure it is not a system table being altered, or a reserved name | 436 /* Make sure it is not a system table being altered, or a reserved name |
305 ** that the table is being renamed to. | 437 ** that the table is being renamed to. |
306 */ | 438 */ |
307 if( sqlite3Strlen30(pTab->zName)>6 | 439 if( SQLITE_OK!=isSystemTable(pParse, pTab->zName) ){ |
308 && 0==sqlite3StrNICmp(pTab->zName, "sqlite_", 7) | |
309 ){ | |
310 sqlite3ErrorMsg(pParse, "table %s may not be altered", pTab->zName); | |
311 goto exit_rename_table; | 440 goto exit_rename_table; |
312 } | 441 } |
313 if( SQLITE_OK!=sqlite3CheckObjectName(pParse, zName) ){ | 442 if( SQLITE_OK!=sqlite3CheckObjectName(pParse, zName) ){ goto |
314 goto exit_rename_table; | 443 exit_rename_table; |
315 } | 444 } |
316 | 445 |
317 #ifndef SQLITE_OMIT_VIEW | 446 #ifndef SQLITE_OMIT_VIEW |
318 if( pTab->pSelect ){ | 447 if( pTab->pSelect ){ |
319 sqlite3ErrorMsg(pParse, "view %s may not be altered", pTab->zName); | 448 sqlite3ErrorMsg(pParse, "view %s may not be altered", pTab->zName); |
320 goto exit_rename_table; | 449 goto exit_rename_table; |
321 } | 450 } |
322 #endif | 451 #endif |
323 | 452 |
324 #ifndef SQLITE_OMIT_AUTHORIZATION | 453 #ifndef SQLITE_OMIT_AUTHORIZATION |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
363 sqlite3VdbeAddOp4(v, OP_String8, 0, i, 0, zName, 0); | 492 sqlite3VdbeAddOp4(v, OP_String8, 0, i, 0, zName, 0); |
364 sqlite3VdbeAddOp4(v, OP_VRename, i, 0, 0,(const char*)pVTab, P4_VTAB); | 493 sqlite3VdbeAddOp4(v, OP_VRename, i, 0, 0,(const char*)pVTab, P4_VTAB); |
365 sqlite3MayAbort(pParse); | 494 sqlite3MayAbort(pParse); |
366 } | 495 } |
367 #endif | 496 #endif |
368 | 497 |
369 /* figure out how many UTF-8 characters are in zName */ | 498 /* figure out how many UTF-8 characters are in zName */ |
370 zTabName = pTab->zName; | 499 zTabName = pTab->zName; |
371 nTabName = sqlite3Utf8CharLen(zTabName, -1); | 500 nTabName = sqlite3Utf8CharLen(zTabName, -1); |
372 | 501 |
| 502 #if !defined(SQLITE_OMIT_FOREIGN_KEY) && !defined(SQLITE_OMIT_TRIGGER) |
| 503 if( db->flags&SQLITE_ForeignKeys ){ |
| 504 /* If foreign-key support is enabled, rewrite the CREATE TABLE |
| 505 ** statements corresponding to all child tables of foreign key constraints |
| 506 ** for which the renamed table is the parent table. */ |
| 507 if( (zWhere=whereForeignKeys(pParse, pTab))!=0 ){ |
| 508 sqlite3NestedParse(pParse, |
| 509 "UPDATE \"%w\".%s SET " |
| 510 "sql = sqlite_rename_parent(sql, %Q, %Q) " |
| 511 "WHERE %s;", zDb, SCHEMA_TABLE(iDb), zTabName, zName, zWhere); |
| 512 sqlite3DbFree(db, zWhere); |
| 513 } |
| 514 } |
| 515 #endif |
| 516 |
373 /* Modify the sqlite_master table to use the new table name. */ | 517 /* Modify the sqlite_master table to use the new table name. */ |
374 sqlite3NestedParse(pParse, | 518 sqlite3NestedParse(pParse, |
375 "UPDATE %Q.%s SET " | 519 "UPDATE %Q.%s SET " |
376 #ifdef SQLITE_OMIT_TRIGGER | 520 #ifdef SQLITE_OMIT_TRIGGER |
377 "sql = sqlite_rename_table(sql, %Q), " | 521 "sql = sqlite_rename_table(sql, %Q), " |
378 #else | 522 #else |
379 "sql = CASE " | 523 "sql = CASE " |
380 "WHEN type = 'trigger' THEN sqlite_rename_trigger(sql, %Q)" | 524 "WHEN type = 'trigger' THEN sqlite_rename_trigger(sql, %Q)" |
381 "ELSE sqlite_rename_table(sql, %Q) END, " | 525 "ELSE sqlite_rename_table(sql, %Q) END, " |
382 #endif | 526 #endif |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
414 if( (zWhere=whereTempTriggers(pParse, pTab))!=0 ){ | 558 if( (zWhere=whereTempTriggers(pParse, pTab))!=0 ){ |
415 sqlite3NestedParse(pParse, | 559 sqlite3NestedParse(pParse, |
416 "UPDATE sqlite_temp_master SET " | 560 "UPDATE sqlite_temp_master SET " |
417 "sql = sqlite_rename_trigger(sql, %Q), " | 561 "sql = sqlite_rename_trigger(sql, %Q), " |
418 "tbl_name = %Q " | 562 "tbl_name = %Q " |
419 "WHERE %s;", zName, zName, zWhere); | 563 "WHERE %s;", zName, zName, zWhere); |
420 sqlite3DbFree(db, zWhere); | 564 sqlite3DbFree(db, zWhere); |
421 } | 565 } |
422 #endif | 566 #endif |
423 | 567 |
| 568 #if !defined(SQLITE_OMIT_FOREIGN_KEY) && !defined(SQLITE_OMIT_TRIGGER) |
| 569 if( db->flags&SQLITE_ForeignKeys ){ |
| 570 FKey *p; |
| 571 for(p=sqlite3FkReferences(pTab); p; p=p->pNextTo){ |
| 572 Table *pFrom = p->pFrom; |
| 573 if( pFrom!=pTab ){ |
| 574 reloadTableSchema(pParse, p->pFrom, pFrom->zName); |
| 575 } |
| 576 } |
| 577 } |
| 578 #endif |
| 579 |
424 /* Drop and reload the internal table schema. */ | 580 /* Drop and reload the internal table schema. */ |
425 reloadTableSchema(pParse, pTab, zName); | 581 reloadTableSchema(pParse, pTab, zName); |
426 | 582 |
427 exit_rename_table: | 583 exit_rename_table: |
428 sqlite3SrcListDelete(db, pSrc); | 584 sqlite3SrcListDelete(db, pSrc); |
429 sqlite3DbFree(db, zName); | 585 sqlite3DbFree(db, zName); |
| 586 db->flags = savedDbFlags; |
430 } | 587 } |
431 | 588 |
432 | 589 |
433 /* | 590 /* |
434 ** Generate code to make sure the file format number is at least minFormat. | 591 ** Generate code to make sure the file format number is at least minFormat. |
435 ** The generated code will increase the file format number if necessary. | 592 ** The generated code will increase the file format number if necessary. |
436 */ | 593 */ |
437 void sqlite3MinimumFileFormat(Parse *pParse, int iDb, int minFormat){ | 594 void sqlite3MinimumFileFormat(Parse *pParse, int iDb, int minFormat){ |
438 Vdbe *v; | 595 Vdbe *v; |
439 v = sqlite3GetVdbe(pParse); | 596 v = sqlite3GetVdbe(pParse); |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
508 ** column must not be NULL. | 665 ** column must not be NULL. |
509 */ | 666 */ |
510 if( pCol->isPrimKey ){ | 667 if( pCol->isPrimKey ){ |
511 sqlite3ErrorMsg(pParse, "Cannot add a PRIMARY KEY column"); | 668 sqlite3ErrorMsg(pParse, "Cannot add a PRIMARY KEY column"); |
512 return; | 669 return; |
513 } | 670 } |
514 if( pNew->pIndex ){ | 671 if( pNew->pIndex ){ |
515 sqlite3ErrorMsg(pParse, "Cannot add a UNIQUE column"); | 672 sqlite3ErrorMsg(pParse, "Cannot add a UNIQUE column"); |
516 return; | 673 return; |
517 } | 674 } |
| 675 if( (db->flags&SQLITE_ForeignKeys) && pNew->pFKey && pDflt ){ |
| 676 sqlite3ErrorMsg(pParse, |
| 677 "Cannot add a REFERENCES column with non-NULL default value"); |
| 678 return; |
| 679 } |
518 if( pCol->notNull && !pDflt ){ | 680 if( pCol->notNull && !pDflt ){ |
519 sqlite3ErrorMsg(pParse, | 681 sqlite3ErrorMsg(pParse, |
520 "Cannot add a NOT NULL column with default value NULL"); | 682 "Cannot add a NOT NULL column with default value NULL"); |
521 return; | 683 return; |
522 } | 684 } |
523 | 685 |
524 /* Ensure the default expression is something that sqlite3ValueFromExpr() | 686 /* Ensure the default expression is something that sqlite3ValueFromExpr() |
525 ** can handle (i.e. not CURRENT_TIME etc.) | 687 ** can handle (i.e. not CURRENT_TIME etc.) |
526 */ | 688 */ |
527 if( pDflt ){ | 689 if( pDflt ){ |
528 sqlite3_value *pVal; | 690 sqlite3_value *pVal; |
529 if( sqlite3ValueFromExpr(db, pDflt, SQLITE_UTF8, SQLITE_AFF_NONE, &pVal) ){ | 691 if( sqlite3ValueFromExpr(db, pDflt, SQLITE_UTF8, SQLITE_AFF_NONE, &pVal) ){ |
530 db->mallocFailed = 1; | 692 db->mallocFailed = 1; |
531 return; | 693 return; |
532 } | 694 } |
533 if( !pVal ){ | 695 if( !pVal ){ |
534 sqlite3ErrorMsg(pParse, "Cannot add a column with non-constant default"); | 696 sqlite3ErrorMsg(pParse, "Cannot add a column with non-constant default"); |
535 return; | 697 return; |
536 } | 698 } |
537 sqlite3ValueFree(pVal); | 699 sqlite3ValueFree(pVal); |
538 } | 700 } |
539 | 701 |
540 /* Modify the CREATE TABLE statement. */ | 702 /* Modify the CREATE TABLE statement. */ |
541 zCol = sqlite3DbStrNDup(db, (char*)pColDef->z, pColDef->n); | 703 zCol = sqlite3DbStrNDup(db, (char*)pColDef->z, pColDef->n); |
542 if( zCol ){ | 704 if( zCol ){ |
543 char *zEnd = &zCol[pColDef->n-1]; | 705 char *zEnd = &zCol[pColDef->n-1]; |
| 706 int savedDbFlags = db->flags; |
544 while( zEnd>zCol && (*zEnd==';' || sqlite3Isspace(*zEnd)) ){ | 707 while( zEnd>zCol && (*zEnd==';' || sqlite3Isspace(*zEnd)) ){ |
545 *zEnd-- = '\0'; | 708 *zEnd-- = '\0'; |
546 } | 709 } |
| 710 db->flags |= SQLITE_PreferBuiltin; |
547 sqlite3NestedParse(pParse, | 711 sqlite3NestedParse(pParse, |
548 "UPDATE \"%w\".%s SET " | 712 "UPDATE \"%w\".%s SET " |
549 "sql = substr(sql,1,%d) || ', ' || %Q || substr(sql,%d) " | 713 "sql = substr(sql,1,%d) || ', ' || %Q || substr(sql,%d) " |
550 "WHERE type = 'table' AND name = %Q", | 714 "WHERE type = 'table' AND name = %Q", |
551 zDb, SCHEMA_TABLE(iDb), pNew->addColOffset, zCol, pNew->addColOffset+1, | 715 zDb, SCHEMA_TABLE(iDb), pNew->addColOffset, zCol, pNew->addColOffset+1, |
552 zTab | 716 zTab |
553 ); | 717 ); |
554 sqlite3DbFree(db, zCol); | 718 sqlite3DbFree(db, zCol); |
| 719 db->flags = savedDbFlags; |
555 } | 720 } |
556 | 721 |
557 /* If the default value of the new column is NULL, then set the file | 722 /* If the default value of the new column is NULL, then set the file |
558 ** format to 2. If the default value of the new column is not NULL, | 723 ** format to 2. If the default value of the new column is not NULL, |
559 ** the file format becomes 3. | 724 ** the file format becomes 3. |
560 */ | 725 */ |
561 sqlite3MinimumFileFormat(pParse, iDb, pDflt ? 3 : 2); | 726 sqlite3MinimumFileFormat(pParse, iDb, pDflt ? 3 : 2); |
562 | 727 |
563 /* Reload the schema of the modified table. */ | 728 /* Reload the schema of the modified table. */ |
564 reloadTableSchema(pParse, pTab, pTab->zName); | 729 reloadTableSchema(pParse, pTab, pTab->zName); |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
600 sqlite3ErrorMsg(pParse, "virtual tables may not be altered"); | 765 sqlite3ErrorMsg(pParse, "virtual tables may not be altered"); |
601 goto exit_begin_add_column; | 766 goto exit_begin_add_column; |
602 } | 767 } |
603 #endif | 768 #endif |
604 | 769 |
605 /* Make sure this is not an attempt to ALTER a view. */ | 770 /* Make sure this is not an attempt to ALTER a view. */ |
606 if( pTab->pSelect ){ | 771 if( pTab->pSelect ){ |
607 sqlite3ErrorMsg(pParse, "Cannot add a column to a view"); | 772 sqlite3ErrorMsg(pParse, "Cannot add a column to a view"); |
608 goto exit_begin_add_column; | 773 goto exit_begin_add_column; |
609 } | 774 } |
| 775 if( SQLITE_OK!=isSystemTable(pParse, pTab->zName) ){ |
| 776 goto exit_begin_add_column; |
| 777 } |
610 | 778 |
611 assert( pTab->addColOffset>0 ); | 779 assert( pTab->addColOffset>0 ); |
612 iDb = sqlite3SchemaToIndex(db, pTab->pSchema); | 780 iDb = sqlite3SchemaToIndex(db, pTab->pSchema); |
613 | 781 |
614 /* Put a copy of the Table struct in Parse.pNewTable for the | 782 /* Put a copy of the Table struct in Parse.pNewTable for the |
615 ** sqlite3AddColumn() function and friends to modify. But modify | 783 ** sqlite3AddColumn() function and friends to modify. But modify |
616 ** the name by adding an "sqlite_altertab_" prefix. By adding this | 784 ** the name by adding an "sqlite_altertab_" prefix. By adding this |
617 ** prefix, we insure that the name will not collide with an existing | 785 ** prefix, we insure that the name will not collide with an existing |
618 ** table because user table are not allowed to have the "sqlite_" | 786 ** table because user table are not allowed to have the "sqlite_" |
619 ** prefix on their name. | 787 ** prefix on their name. |
620 */ | 788 */ |
621 pNew = (Table*)sqlite3DbMallocZero(db, sizeof(Table)); | 789 pNew = (Table*)sqlite3DbMallocZero(db, sizeof(Table)); |
622 if( !pNew ) goto exit_begin_add_column; | 790 if( !pNew ) goto exit_begin_add_column; |
623 pParse->pNewTable = pNew; | 791 pParse->pNewTable = pNew; |
624 pNew->nRef = 1; | 792 pNew->nRef = 1; |
625 pNew->dbMem = pTab->dbMem; | |
626 pNew->nCol = pTab->nCol; | 793 pNew->nCol = pTab->nCol; |
627 assert( pNew->nCol>0 ); | 794 assert( pNew->nCol>0 ); |
628 nAlloc = (((pNew->nCol-1)/8)*8)+8; | 795 nAlloc = (((pNew->nCol-1)/8)*8)+8; |
629 assert( nAlloc>=pNew->nCol && nAlloc%8==0 && nAlloc-pNew->nCol<8 ); | 796 assert( nAlloc>=pNew->nCol && nAlloc%8==0 && nAlloc-pNew->nCol<8 ); |
630 pNew->aCol = (Column*)sqlite3DbMallocZero(db, sizeof(Column)*nAlloc); | 797 pNew->aCol = (Column*)sqlite3DbMallocZero(db, sizeof(Column)*nAlloc); |
631 pNew->zName = sqlite3MPrintf(db, "sqlite_altertab_%s", pTab->zName); | 798 pNew->zName = sqlite3MPrintf(db, "sqlite_altertab_%s", pTab->zName); |
632 if( !pNew->aCol || !pNew->zName ){ | 799 if( !pNew->aCol || !pNew->zName ){ |
633 db->mallocFailed = 1; | 800 db->mallocFailed = 1; |
634 goto exit_begin_add_column; | 801 goto exit_begin_add_column; |
635 } | 802 } |
(...skipping 14 matching lines...) Expand all Loading... |
650 sqlite3BeginWriteOperation(pParse, 0, iDb); | 817 sqlite3BeginWriteOperation(pParse, 0, iDb); |
651 v = sqlite3GetVdbe(pParse); | 818 v = sqlite3GetVdbe(pParse); |
652 if( !v ) goto exit_begin_add_column; | 819 if( !v ) goto exit_begin_add_column; |
653 sqlite3ChangeCookie(pParse, iDb); | 820 sqlite3ChangeCookie(pParse, iDb); |
654 | 821 |
655 exit_begin_add_column: | 822 exit_begin_add_column: |
656 sqlite3SrcListDelete(db, pSrc); | 823 sqlite3SrcListDelete(db, pSrc); |
657 return; | 824 return; |
658 } | 825 } |
659 #endif /* SQLITE_ALTER_TABLE */ | 826 #endif /* SQLITE_ALTER_TABLE */ |
OLD | NEW |