| OLD | NEW |
| (Empty) |
| 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. | |
| 14 ** | |
| 15 ** $Id: alter.c,v 1.62 2009/07/24 17:58:53 danielk1977 Exp $ | |
| 16 */ | |
| 17 #include "sqliteInt.h" | |
| 18 | |
| 19 /* | |
| 20 ** The code in this file only exists if we are not omitting the | |
| 21 ** ALTER TABLE logic from the build. | |
| 22 */ | |
| 23 #ifndef SQLITE_OMIT_ALTERTABLE | |
| 24 | |
| 25 | |
| 26 /* | |
| 27 ** This function is used by SQL generated to implement the | |
| 28 ** ALTER TABLE command. The first argument is the text of a CREATE TABLE or | |
| 29 ** CREATE INDEX command. The second is a table name. The table name in | |
| 30 ** the CREATE TABLE or CREATE INDEX statement is replaced with the third | |
| 31 ** argument and the result returned. Examples: | |
| 32 ** | |
| 33 ** sqlite_rename_table('CREATE TABLE abc(a, b, c)', 'def') | |
| 34 ** -> 'CREATE TABLE def(a, b, c)' | |
| 35 ** | |
| 36 ** sqlite_rename_table('CREATE INDEX i ON abc(a)', 'def') | |
| 37 ** -> 'CREATE INDEX i ON def(a, b, c)' | |
| 38 */ | |
| 39 static void renameTableFunc( | |
| 40 sqlite3_context *context, | |
| 41 int NotUsed, | |
| 42 sqlite3_value **argv | |
| 43 ){ | |
| 44 unsigned char const *zSql = sqlite3_value_text(argv[0]); | |
| 45 unsigned char const *zTableName = sqlite3_value_text(argv[1]); | |
| 46 | |
| 47 int token; | |
| 48 Token tname; | |
| 49 unsigned char const *zCsr = zSql; | |
| 50 int len = 0; | |
| 51 char *zRet; | |
| 52 | |
| 53 sqlite3 *db = sqlite3_context_db_handle(context); | |
| 54 | |
| 55 UNUSED_PARAMETER(NotUsed); | |
| 56 | |
| 57 /* The principle used to locate the table name in the CREATE TABLE | |
| 58 ** statement is that the table name is the first non-space token that | |
| 59 ** is immediately followed by a TK_LP or TK_USING token. | |
| 60 */ | |
| 61 if( zSql ){ | |
| 62 do { | |
| 63 if( !*zCsr ){ | |
| 64 /* Ran out of input before finding an opening bracket. Return NULL. */ | |
| 65 return; | |
| 66 } | |
| 67 | |
| 68 /* Store the token that zCsr points to in tname. */ | |
| 69 tname.z = (char*)zCsr; | |
| 70 tname.n = len; | |
| 71 | |
| 72 /* Advance zCsr to the next token. Store that token type in 'token', | |
| 73 ** and its length in 'len' (to be used next iteration of this loop). | |
| 74 */ | |
| 75 do { | |
| 76 zCsr += len; | |
| 77 len = sqlite3GetToken(zCsr, &token); | |
| 78 } while( token==TK_SPACE ); | |
| 79 assert( len>0 ); | |
| 80 } while( token!=TK_LP && token!=TK_USING ); | |
| 81 | |
| 82 zRet = sqlite3MPrintf(db, "%.*s\"%w\"%s", ((u8*)tname.z) - zSql, zSql, | |
| 83 zTableName, tname.z+tname.n); | |
| 84 sqlite3_result_text(context, zRet, -1, SQLITE_DYNAMIC); | |
| 85 } | |
| 86 } | |
| 87 | |
| 88 #ifndef SQLITE_OMIT_TRIGGER | |
| 89 /* This function is used by SQL generated to implement the | |
| 90 ** 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 | |
| 92 ** TRIGGER statement is replaced with the third argument and the result | |
| 93 ** returned. This is analagous to renameTableFunc() above, except for CREATE | |
| 94 ** TRIGGER, not CREATE INDEX and CREATE TABLE. | |
| 95 */ | |
| 96 static void renameTriggerFunc( | |
| 97 sqlite3_context *context, | |
| 98 int NotUsed, | |
| 99 sqlite3_value **argv | |
| 100 ){ | |
| 101 unsigned char const *zSql = sqlite3_value_text(argv[0]); | |
| 102 unsigned char const *zTableName = sqlite3_value_text(argv[1]); | |
| 103 | |
| 104 int token; | |
| 105 Token tname; | |
| 106 int dist = 3; | |
| 107 unsigned char const *zCsr = zSql; | |
| 108 int len = 0; | |
| 109 char *zRet; | |
| 110 sqlite3 *db = sqlite3_context_db_handle(context); | |
| 111 | |
| 112 UNUSED_PARAMETER(NotUsed); | |
| 113 | |
| 114 /* The principle used to locate the table name in the CREATE TRIGGER | |
| 115 ** statement is that the table name is the first token that is immediatedly | |
| 116 ** preceded by either TK_ON or TK_DOT and immediatedly followed by one | |
| 117 ** of TK_WHEN, TK_BEGIN or TK_FOR. | |
| 118 */ | |
| 119 if( zSql ){ | |
| 120 do { | |
| 121 | |
| 122 if( !*zCsr ){ | |
| 123 /* Ran out of input before finding the table name. Return NULL. */ | |
| 124 return; | |
| 125 } | |
| 126 | |
| 127 /* Store the token that zCsr points to in tname. */ | |
| 128 tname.z = (char*)zCsr; | |
| 129 tname.n = len; | |
| 130 | |
| 131 /* Advance zCsr to the next token. Store that token type in 'token', | |
| 132 ** and its length in 'len' (to be used next iteration of this loop). | |
| 133 */ | |
| 134 do { | |
| 135 zCsr += len; | |
| 136 len = sqlite3GetToken(zCsr, &token); | |
| 137 }while( token==TK_SPACE ); | |
| 138 assert( len>0 ); | |
| 139 | |
| 140 /* Variable 'dist' stores the number of tokens read since the most | |
| 141 ** recent TK_DOT or TK_ON. This means that when a WHEN, FOR or BEGIN | |
| 142 ** token is read and 'dist' equals 2, the condition stated above | |
| 143 ** to be met. | |
| 144 ** | |
| 145 ** Note that ON cannot be a database, table or column name, so | |
| 146 ** there is no need to worry about syntax like | |
| 147 ** "CREATE TRIGGER ... ON ON.ON BEGIN ..." etc. | |
| 148 */ | |
| 149 dist++; | |
| 150 if( token==TK_DOT || token==TK_ON ){ | |
| 151 dist = 0; | |
| 152 } | |
| 153 } while( dist!=2 || (token!=TK_WHEN && token!=TK_FOR && token!=TK_BEGIN) ); | |
| 154 | |
| 155 /* Variable tname now contains the token that is the old table-name | |
| 156 ** in the CREATE TRIGGER statement. | |
| 157 */ | |
| 158 zRet = sqlite3MPrintf(db, "%.*s\"%w\"%s", ((u8*)tname.z) - zSql, zSql, | |
| 159 zTableName, tname.z+tname.n); | |
| 160 sqlite3_result_text(context, zRet, -1, SQLITE_DYNAMIC); | |
| 161 } | |
| 162 } | |
| 163 #endif /* !SQLITE_OMIT_TRIGGER */ | |
| 164 | |
| 165 /* | |
| 166 ** Register built-in functions used to help implement ALTER TABLE | |
| 167 */ | |
| 168 void sqlite3AlterFunctions(sqlite3 *db){ | |
| 169 sqlite3CreateFunc(db, "sqlite_rename_table", 2, SQLITE_UTF8, 0, | |
| 170 renameTableFunc, 0, 0); | |
| 171 #ifndef SQLITE_OMIT_TRIGGER | |
| 172 sqlite3CreateFunc(db, "sqlite_rename_trigger", 2, SQLITE_UTF8, 0, | |
| 173 renameTriggerFunc, 0, 0); | |
| 174 #endif | |
| 175 } | |
| 176 | |
| 177 /* | |
| 178 ** 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 | |
| 180 ** table pTab has no temporary triggers, or is itself stored in the | |
| 181 ** temporary database, NULL is returned. | |
| 182 */ | |
| 183 static char *whereTempTriggers(Parse *pParse, Table *pTab){ | |
| 184 Trigger *pTrig; | |
| 185 char *zWhere = 0; | |
| 186 char *tmp = 0; | |
| 187 const Schema *pTempSchema = pParse->db->aDb[1].pSchema; /* Temp db schema */ | |
| 188 | |
| 189 /* 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 | |
| 191 ** that is not part of the temp-db schema, add a clause to the WHERE | |
| 192 ** expression being built up in zWhere. | |
| 193 */ | |
| 194 if( pTab->pSchema!=pTempSchema ){ | |
| 195 sqlite3 *db = pParse->db; | |
| 196 for(pTrig=sqlite3TriggerList(pParse, pTab); pTrig; pTrig=pTrig->pNext){ | |
| 197 if( pTrig->pSchema==pTempSchema ){ | |
| 198 if( !zWhere ){ | |
| 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 } | |
| 206 } | |
| 207 } | |
| 208 return zWhere; | |
| 209 } | |
| 210 | |
| 211 /* | |
| 212 ** Generate code to drop and reload the internal representation of table | |
| 213 ** pTab from the database, including triggers and temporary triggers. | |
| 214 ** 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 | |
| 216 ** pTab->zName if this function is being called to code part of an | |
| 217 ** "ALTER TABLE RENAME TO" statement. | |
| 218 */ | |
| 219 static void reloadTableSchema(Parse *pParse, Table *pTab, const char *zName){ | |
| 220 Vdbe *v; | |
| 221 char *zWhere; | |
| 222 int iDb; /* Index of database containing pTab */ | |
| 223 #ifndef SQLITE_OMIT_TRIGGER | |
| 224 Trigger *pTrig; | |
| 225 #endif | |
| 226 | |
| 227 v = sqlite3GetVdbe(pParse); | |
| 228 if( NEVER(v==0) ) return; | |
| 229 assert( sqlite3BtreeHoldsAllMutexes(pParse->db) ); | |
| 230 iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema); | |
| 231 assert( iDb>=0 ); | |
| 232 | |
| 233 #ifndef SQLITE_OMIT_TRIGGER | |
| 234 /* Drop any table triggers from the internal schema. */ | |
| 235 for(pTrig=sqlite3TriggerList(pParse, pTab); pTrig; pTrig=pTrig->pNext){ | |
| 236 int iTrigDb = sqlite3SchemaToIndex(pParse->db, pTrig->pSchema); | |
| 237 assert( iTrigDb==iDb || iTrigDb==1 ); | |
| 238 sqlite3VdbeAddOp4(v, OP_DropTrigger, iTrigDb, 0, 0, pTrig->zName, 0); | |
| 239 } | |
| 240 #endif | |
| 241 | |
| 242 /* Drop the table and index from the internal schema */ | |
| 243 sqlite3VdbeAddOp4(v, OP_DropTable, iDb, 0, 0, pTab->zName, 0); | |
| 244 | |
| 245 /* Reload the table, index and permanent trigger schemas. */ | |
| 246 zWhere = sqlite3MPrintf(pParse->db, "tbl_name=%Q", zName); | |
| 247 if( !zWhere ) return; | |
| 248 sqlite3VdbeAddOp4(v, OP_ParseSchema, iDb, 0, 0, zWhere, P4_DYNAMIC); | |
| 249 | |
| 250 #ifndef SQLITE_OMIT_TRIGGER | |
| 251 /* 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. | |
| 253 */ | |
| 254 if( (zWhere=whereTempTriggers(pParse, pTab))!=0 ){ | |
| 255 sqlite3VdbeAddOp4(v, OP_ParseSchema, 1, 0, 0, zWhere, P4_DYNAMIC); | |
| 256 } | |
| 257 #endif | |
| 258 } | |
| 259 | |
| 260 /* | |
| 261 ** Generate code to implement the "ALTER TABLE xxx RENAME TO yyy" | |
| 262 ** command. | |
| 263 */ | |
| 264 void sqlite3AlterRenameTable( | |
| 265 Parse *pParse, /* Parser context. */ | |
| 266 SrcList *pSrc, /* The table to rename. */ | |
| 267 Token *pName /* The new table name. */ | |
| 268 ){ | |
| 269 int iDb; /* Database that contains the table */ | |
| 270 char *zDb; /* Name of database iDb */ | |
| 271 Table *pTab; /* Table being renamed */ | |
| 272 char *zName = 0; /* NULL-terminated version of pName */ | |
| 273 sqlite3 *db = pParse->db; /* Database connection */ | |
| 274 int nTabName; /* Number of UTF-8 characters in zTabName */ | |
| 275 const char *zTabName; /* Original name of the table */ | |
| 276 Vdbe *v; | |
| 277 #ifndef SQLITE_OMIT_TRIGGER | |
| 278 char *zWhere = 0; /* Where clause to locate temp triggers */ | |
| 279 #endif | |
| 280 VTable *pVTab = 0; /* Non-zero if this is a v-tab with an xRename() */ | |
| 281 | |
| 282 if( NEVER(db->mallocFailed) ) goto exit_rename_table; | |
| 283 assert( pSrc->nSrc==1 ); | |
| 284 assert( sqlite3BtreeHoldsAllMutexes(pParse->db) ); | |
| 285 | |
| 286 pTab = sqlite3LocateTable(pParse, 0, pSrc->a[0].zName, pSrc->a[0].zDatabase); | |
| 287 if( !pTab ) goto exit_rename_table; | |
| 288 iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema); | |
| 289 zDb = db->aDb[iDb].zName; | |
| 290 | |
| 291 /* Get a NULL terminated version of the new table name. */ | |
| 292 zName = sqlite3NameFromToken(db, pName); | |
| 293 if( !zName ) goto exit_rename_table; | |
| 294 | |
| 295 /* Check that a table or index named 'zName' does not already exist | |
| 296 ** in database iDb. If so, this is an error. | |
| 297 */ | |
| 298 if( sqlite3FindTable(db, zName, zDb) || sqlite3FindIndex(db, zName, zDb) ){ | |
| 299 sqlite3ErrorMsg(pParse, | |
| 300 "there is already another table or index with this name: %s", zName); | |
| 301 goto exit_rename_table; | |
| 302 } | |
| 303 | |
| 304 /* Make sure it is not a system table being altered, or a reserved name | |
| 305 ** that the table is being renamed to. | |
| 306 */ | |
| 307 if( sqlite3Strlen30(pTab->zName)>6 | |
| 308 && 0==sqlite3StrNICmp(pTab->zName, "sqlite_", 7) | |
| 309 ){ | |
| 310 sqlite3ErrorMsg(pParse, "table %s may not be altered", pTab->zName); | |
| 311 goto exit_rename_table; | |
| 312 } | |
| 313 if( SQLITE_OK!=sqlite3CheckObjectName(pParse, zName) ){ | |
| 314 goto exit_rename_table; | |
| 315 } | |
| 316 | |
| 317 #ifndef SQLITE_OMIT_VIEW | |
| 318 if( pTab->pSelect ){ | |
| 319 sqlite3ErrorMsg(pParse, "view %s may not be altered", pTab->zName); | |
| 320 goto exit_rename_table; | |
| 321 } | |
| 322 #endif | |
| 323 | |
| 324 #ifndef SQLITE_OMIT_AUTHORIZATION | |
| 325 /* Invoke the authorization callback. */ | |
| 326 if( sqlite3AuthCheck(pParse, SQLITE_ALTER_TABLE, zDb, pTab->zName, 0) ){ | |
| 327 goto exit_rename_table; | |
| 328 } | |
| 329 #endif | |
| 330 | |
| 331 #ifndef SQLITE_OMIT_VIRTUALTABLE | |
| 332 if( sqlite3ViewGetColumnNames(pParse, pTab) ){ | |
| 333 goto exit_rename_table; | |
| 334 } | |
| 335 if( IsVirtual(pTab) ){ | |
| 336 pVTab = sqlite3GetVTable(db, pTab); | |
| 337 if( pVTab->pVtab->pModule->xRename==0 ){ | |
| 338 pVTab = 0; | |
| 339 } | |
| 340 } | |
| 341 #endif | |
| 342 | |
| 343 /* Begin a transaction and code the VerifyCookie for database iDb. | |
| 344 ** Then modify the schema cookie (since the ALTER TABLE modifies the | |
| 345 ** schema). Open a statement transaction if the table is a virtual | |
| 346 ** table. | |
| 347 */ | |
| 348 v = sqlite3GetVdbe(pParse); | |
| 349 if( v==0 ){ | |
| 350 goto exit_rename_table; | |
| 351 } | |
| 352 sqlite3BeginWriteOperation(pParse, pVTab!=0, iDb); | |
| 353 sqlite3ChangeCookie(pParse, iDb); | |
| 354 | |
| 355 /* If this is a virtual table, invoke the xRename() function if | |
| 356 ** one is defined. The xRename() callback will modify the names | |
| 357 ** of any resources used by the v-table implementation (including other | |
| 358 ** SQLite tables) that are identified by the name of the virtual table. | |
| 359 */ | |
| 360 #ifndef SQLITE_OMIT_VIRTUALTABLE | |
| 361 if( pVTab ){ | |
| 362 int i = ++pParse->nMem; | |
| 363 sqlite3VdbeAddOp4(v, OP_String8, 0, i, 0, zName, 0); | |
| 364 sqlite3VdbeAddOp4(v, OP_VRename, i, 0, 0,(const char*)pVTab, P4_VTAB); | |
| 365 sqlite3MayAbort(pParse); | |
| 366 } | |
| 367 #endif | |
| 368 | |
| 369 /* figure out how many UTF-8 characters are in zName */ | |
| 370 zTabName = pTab->zName; | |
| 371 nTabName = sqlite3Utf8CharLen(zTabName, -1); | |
| 372 | |
| 373 /* Modify the sqlite_master table to use the new table name. */ | |
| 374 sqlite3NestedParse(pParse, | |
| 375 "UPDATE %Q.%s SET " | |
| 376 #ifdef SQLITE_OMIT_TRIGGER | |
| 377 "sql = sqlite_rename_table(sql, %Q), " | |
| 378 #else | |
| 379 "sql = CASE " | |
| 380 "WHEN type = 'trigger' THEN sqlite_rename_trigger(sql, %Q)" | |
| 381 "ELSE sqlite_rename_table(sql, %Q) END, " | |
| 382 #endif | |
| 383 "tbl_name = %Q, " | |
| 384 "name = CASE " | |
| 385 "WHEN type='table' THEN %Q " | |
| 386 "WHEN name LIKE 'sqlite_autoindex%%' AND type='index' THEN " | |
| 387 "'sqlite_autoindex_' || %Q || substr(name,%d+18) " | |
| 388 "ELSE name END " | |
| 389 "WHERE tbl_name=%Q AND " | |
| 390 "(type='table' OR type='index' OR type='trigger');", | |
| 391 zDb, SCHEMA_TABLE(iDb), zName, zName, zName, | |
| 392 #ifndef SQLITE_OMIT_TRIGGER | |
| 393 zName, | |
| 394 #endif | |
| 395 zName, nTabName, zTabName | |
| 396 ); | |
| 397 | |
| 398 #ifndef SQLITE_OMIT_AUTOINCREMENT | |
| 399 /* If the sqlite_sequence table exists in this database, then update | |
| 400 ** it with the new table name. | |
| 401 */ | |
| 402 if( sqlite3FindTable(db, "sqlite_sequence", zDb) ){ | |
| 403 sqlite3NestedParse(pParse, | |
| 404 "UPDATE \"%w\".sqlite_sequence set name = %Q WHERE name = %Q", | |
| 405 zDb, zName, pTab->zName); | |
| 406 } | |
| 407 #endif | |
| 408 | |
| 409 #ifndef SQLITE_OMIT_TRIGGER | |
| 410 /* If there are TEMP triggers on this table, modify the sqlite_temp_master | |
| 411 ** table. Don't do this if the table being ALTERed is itself located in | |
| 412 ** the temp database. | |
| 413 */ | |
| 414 if( (zWhere=whereTempTriggers(pParse, pTab))!=0 ){ | |
| 415 sqlite3NestedParse(pParse, | |
| 416 "UPDATE sqlite_temp_master SET " | |
| 417 "sql = sqlite_rename_trigger(sql, %Q), " | |
| 418 "tbl_name = %Q " | |
| 419 "WHERE %s;", zName, zName, zWhere); | |
| 420 sqlite3DbFree(db, zWhere); | |
| 421 } | |
| 422 #endif | |
| 423 | |
| 424 /* Drop and reload the internal table schema. */ | |
| 425 reloadTableSchema(pParse, pTab, zName); | |
| 426 | |
| 427 exit_rename_table: | |
| 428 sqlite3SrcListDelete(db, pSrc); | |
| 429 sqlite3DbFree(db, zName); | |
| 430 } | |
| 431 | |
| 432 | |
| 433 /* | |
| 434 ** 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. | |
| 436 */ | |
| 437 void sqlite3MinimumFileFormat(Parse *pParse, int iDb, int minFormat){ | |
| 438 Vdbe *v; | |
| 439 v = sqlite3GetVdbe(pParse); | |
| 440 /* The VDBE should have been allocated before this routine is called. | |
| 441 ** If that allocation failed, we would have quit before reaching this | |
| 442 ** point */ | |
| 443 if( ALWAYS(v) ){ | |
| 444 int r1 = sqlite3GetTempReg(pParse); | |
| 445 int r2 = sqlite3GetTempReg(pParse); | |
| 446 int j1; | |
| 447 sqlite3VdbeAddOp3(v, OP_ReadCookie, iDb, r1, BTREE_FILE_FORMAT); | |
| 448 sqlite3VdbeUsesBtree(v, iDb); | |
| 449 sqlite3VdbeAddOp2(v, OP_Integer, minFormat, r2); | |
| 450 j1 = sqlite3VdbeAddOp3(v, OP_Ge, r2, 0, r1); | |
| 451 sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, BTREE_FILE_FORMAT, r2); | |
| 452 sqlite3VdbeJumpHere(v, j1); | |
| 453 sqlite3ReleaseTempReg(pParse, r1); | |
| 454 sqlite3ReleaseTempReg(pParse, r2); | |
| 455 } | |
| 456 } | |
| 457 | |
| 458 /* | |
| 459 ** This function is called after an "ALTER TABLE ... ADD" statement | |
| 460 ** has been parsed. Argument pColDef contains the text of the new | |
| 461 ** column definition. | |
| 462 ** | |
| 463 ** The Table structure pParse->pNewTable was extended to include | |
| 464 ** the new column during parsing. | |
| 465 */ | |
| 466 void sqlite3AlterFinishAddColumn(Parse *pParse, Token *pColDef){ | |
| 467 Table *pNew; /* Copy of pParse->pNewTable */ | |
| 468 Table *pTab; /* Table being altered */ | |
| 469 int iDb; /* Database number */ | |
| 470 const char *zDb; /* Database name */ | |
| 471 const char *zTab; /* Table name */ | |
| 472 char *zCol; /* Null-terminated column definition */ | |
| 473 Column *pCol; /* The new column */ | |
| 474 Expr *pDflt; /* Default value for the new column */ | |
| 475 sqlite3 *db; /* The database connection; */ | |
| 476 | |
| 477 db = pParse->db; | |
| 478 if( pParse->nErr || db->mallocFailed ) return; | |
| 479 pNew = pParse->pNewTable; | |
| 480 assert( pNew ); | |
| 481 | |
| 482 assert( sqlite3BtreeHoldsAllMutexes(db) ); | |
| 483 iDb = sqlite3SchemaToIndex(db, pNew->pSchema); | |
| 484 zDb = db->aDb[iDb].zName; | |
| 485 zTab = &pNew->zName[16]; /* Skip the "sqlite_altertab_" prefix on the name */ | |
| 486 pCol = &pNew->aCol[pNew->nCol-1]; | |
| 487 pDflt = pCol->pDflt; | |
| 488 pTab = sqlite3FindTable(db, zTab, zDb); | |
| 489 assert( pTab ); | |
| 490 | |
| 491 #ifndef SQLITE_OMIT_AUTHORIZATION | |
| 492 /* Invoke the authorization callback. */ | |
| 493 if( sqlite3AuthCheck(pParse, SQLITE_ALTER_TABLE, zDb, pTab->zName, 0) ){ | |
| 494 return; | |
| 495 } | |
| 496 #endif | |
| 497 | |
| 498 /* If the default value for the new column was specified with a | |
| 499 ** literal NULL, then set pDflt to 0. This simplifies checking | |
| 500 ** for an SQL NULL default below. | |
| 501 */ | |
| 502 if( pDflt && pDflt->op==TK_NULL ){ | |
| 503 pDflt = 0; | |
| 504 } | |
| 505 | |
| 506 /* Check that the new column is not specified as PRIMARY KEY or UNIQUE. | |
| 507 ** If there is a NOT NULL constraint, then the default value for the | |
| 508 ** column must not be NULL. | |
| 509 */ | |
| 510 if( pCol->isPrimKey ){ | |
| 511 sqlite3ErrorMsg(pParse, "Cannot add a PRIMARY KEY column"); | |
| 512 return; | |
| 513 } | |
| 514 if( pNew->pIndex ){ | |
| 515 sqlite3ErrorMsg(pParse, "Cannot add a UNIQUE column"); | |
| 516 return; | |
| 517 } | |
| 518 if( pCol->notNull && !pDflt ){ | |
| 519 sqlite3ErrorMsg(pParse, | |
| 520 "Cannot add a NOT NULL column with default value NULL"); | |
| 521 return; | |
| 522 } | |
| 523 | |
| 524 /* Ensure the default expression is something that sqlite3ValueFromExpr() | |
| 525 ** can handle (i.e. not CURRENT_TIME etc.) | |
| 526 */ | |
| 527 if( pDflt ){ | |
| 528 sqlite3_value *pVal; | |
| 529 if( sqlite3ValueFromExpr(db, pDflt, SQLITE_UTF8, SQLITE_AFF_NONE, &pVal) ){ | |
| 530 db->mallocFailed = 1; | |
| 531 return; | |
| 532 } | |
| 533 if( !pVal ){ | |
| 534 sqlite3ErrorMsg(pParse, "Cannot add a column with non-constant default"); | |
| 535 return; | |
| 536 } | |
| 537 sqlite3ValueFree(pVal); | |
| 538 } | |
| 539 | |
| 540 /* Modify the CREATE TABLE statement. */ | |
| 541 zCol = sqlite3DbStrNDup(db, (char*)pColDef->z, pColDef->n); | |
| 542 if( zCol ){ | |
| 543 char *zEnd = &zCol[pColDef->n-1]; | |
| 544 while( zEnd>zCol && (*zEnd==';' || sqlite3Isspace(*zEnd)) ){ | |
| 545 *zEnd-- = '\0'; | |
| 546 } | |
| 547 sqlite3NestedParse(pParse, | |
| 548 "UPDATE \"%w\".%s SET " | |
| 549 "sql = substr(sql,1,%d) || ', ' || %Q || substr(sql,%d) " | |
| 550 "WHERE type = 'table' AND name = %Q", | |
| 551 zDb, SCHEMA_TABLE(iDb), pNew->addColOffset, zCol, pNew->addColOffset+1, | |
| 552 zTab | |
| 553 ); | |
| 554 sqlite3DbFree(db, zCol); | |
| 555 } | |
| 556 | |
| 557 /* 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, | |
| 559 ** the file format becomes 3. | |
| 560 */ | |
| 561 sqlite3MinimumFileFormat(pParse, iDb, pDflt ? 3 : 2); | |
| 562 | |
| 563 /* Reload the schema of the modified table. */ | |
| 564 reloadTableSchema(pParse, pTab, pTab->zName); | |
| 565 } | |
| 566 | |
| 567 /* | |
| 568 ** This function is called by the parser after the table-name in | |
| 569 ** an "ALTER TABLE <table-name> ADD" statement is parsed. Argument | |
| 570 ** pSrc is the full-name of the table being altered. | |
| 571 ** | |
| 572 ** This routine makes a (partial) copy of the Table structure | |
| 573 ** for the table being altered and sets Parse.pNewTable to point | |
| 574 ** to it. Routines called by the parser as the column definition | |
| 575 ** is parsed (i.e. sqlite3AddColumn()) add the new Column data to | |
| 576 ** the copy. The copy of the Table structure is deleted by tokenize.c | |
| 577 ** after parsing is finished. | |
| 578 ** | |
| 579 ** Routine sqlite3AlterFinishAddColumn() will be called to complete | |
| 580 ** coding the "ALTER TABLE ... ADD" statement. | |
| 581 */ | |
| 582 void sqlite3AlterBeginAddColumn(Parse *pParse, SrcList *pSrc){ | |
| 583 Table *pNew; | |
| 584 Table *pTab; | |
| 585 Vdbe *v; | |
| 586 int iDb; | |
| 587 int i; | |
| 588 int nAlloc; | |
| 589 sqlite3 *db = pParse->db; | |
| 590 | |
| 591 /* Look up the table being altered. */ | |
| 592 assert( pParse->pNewTable==0 ); | |
| 593 assert( sqlite3BtreeHoldsAllMutexes(db) ); | |
| 594 if( db->mallocFailed ) goto exit_begin_add_column; | |
| 595 pTab = sqlite3LocateTable(pParse, 0, pSrc->a[0].zName, pSrc->a[0].zDatabase); | |
| 596 if( !pTab ) goto exit_begin_add_column; | |
| 597 | |
| 598 #ifndef SQLITE_OMIT_VIRTUALTABLE | |
| 599 if( IsVirtual(pTab) ){ | |
| 600 sqlite3ErrorMsg(pParse, "virtual tables may not be altered"); | |
| 601 goto exit_begin_add_column; | |
| 602 } | |
| 603 #endif | |
| 604 | |
| 605 /* Make sure this is not an attempt to ALTER a view. */ | |
| 606 if( pTab->pSelect ){ | |
| 607 sqlite3ErrorMsg(pParse, "Cannot add a column to a view"); | |
| 608 goto exit_begin_add_column; | |
| 609 } | |
| 610 | |
| 611 assert( pTab->addColOffset>0 ); | |
| 612 iDb = sqlite3SchemaToIndex(db, pTab->pSchema); | |
| 613 | |
| 614 /* Put a copy of the Table struct in Parse.pNewTable for the | |
| 615 ** sqlite3AddColumn() function and friends to modify. But modify | |
| 616 ** the name by adding an "sqlite_altertab_" prefix. By adding this | |
| 617 ** prefix, we insure that the name will not collide with an existing | |
| 618 ** table because user table are not allowed to have the "sqlite_" | |
| 619 ** prefix on their name. | |
| 620 */ | |
| 621 pNew = (Table*)sqlite3DbMallocZero(db, sizeof(Table)); | |
| 622 if( !pNew ) goto exit_begin_add_column; | |
| 623 pParse->pNewTable = pNew; | |
| 624 pNew->nRef = 1; | |
| 625 pNew->dbMem = pTab->dbMem; | |
| 626 pNew->nCol = pTab->nCol; | |
| 627 assert( pNew->nCol>0 ); | |
| 628 nAlloc = (((pNew->nCol-1)/8)*8)+8; | |
| 629 assert( nAlloc>=pNew->nCol && nAlloc%8==0 && nAlloc-pNew->nCol<8 ); | |
| 630 pNew->aCol = (Column*)sqlite3DbMallocZero(db, sizeof(Column)*nAlloc); | |
| 631 pNew->zName = sqlite3MPrintf(db, "sqlite_altertab_%s", pTab->zName); | |
| 632 if( !pNew->aCol || !pNew->zName ){ | |
| 633 db->mallocFailed = 1; | |
| 634 goto exit_begin_add_column; | |
| 635 } | |
| 636 memcpy(pNew->aCol, pTab->aCol, sizeof(Column)*pNew->nCol); | |
| 637 for(i=0; i<pNew->nCol; i++){ | |
| 638 Column *pCol = &pNew->aCol[i]; | |
| 639 pCol->zName = sqlite3DbStrDup(db, pCol->zName); | |
| 640 pCol->zColl = 0; | |
| 641 pCol->zType = 0; | |
| 642 pCol->pDflt = 0; | |
| 643 pCol->zDflt = 0; | |
| 644 } | |
| 645 pNew->pSchema = db->aDb[iDb].pSchema; | |
| 646 pNew->addColOffset = pTab->addColOffset; | |
| 647 pNew->nRef = 1; | |
| 648 | |
| 649 /* Begin a transaction and increment the schema cookie. */ | |
| 650 sqlite3BeginWriteOperation(pParse, 0, iDb); | |
| 651 v = sqlite3GetVdbe(pParse); | |
| 652 if( !v ) goto exit_begin_add_column; | |
| 653 sqlite3ChangeCookie(pParse, iDb); | |
| 654 | |
| 655 exit_begin_add_column: | |
| 656 sqlite3SrcListDelete(db, pSrc); | |
| 657 return; | |
| 658 } | |
| 659 #endif /* SQLITE_ALTER_TABLE */ | |
| OLD | NEW |