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 #include "sqliteInt.h" 16 17 /* 18 ** The code in this file only exists if we are not omitting the 19 ** ALTER TABLE logic from the build. 20 */ 21 #ifndef SQLITE_OMIT_ALTERTABLE 22 23 /* 24 ** Parameter zName is the name of a table that is about to be altered 25 ** (either with ALTER TABLE ... RENAME TO or ALTER TABLE ... ADD COLUMN). 26 ** If the table is a system table, this function leaves an error message 27 ** in pParse->zErr (system tables may not be altered) and returns non-zero. 28 ** 29 ** Or, if zName is not a system table, zero is returned. 30 */ 31 static int isAlterableTable(Parse *pParse, Table *pTab){ 32 if( 0==sqlite3StrNICmp(pTab->zName, "sqlite_", 7) 33 #ifndef SQLITE_OMIT_VIRTUALTABLE 34 || ( (pTab->tabFlags & TF_Shadow)!=0 35 && sqlite3ReadOnlyShadowTables(pParse->db) 36 ) 37 #endif 38 ){ 39 sqlite3ErrorMsg(pParse, "table %s may not be altered", pTab->zName); 40 return 1; 41 } 42 return 0; 43 } 44 45 /* 46 ** Generate code to verify that the schemas of database zDb and, if 47 ** bTemp is not true, database "temp", can still be parsed. This is 48 ** called at the end of the generation of an ALTER TABLE ... RENAME ... 49 ** statement to ensure that the operation has not rendered any schema 50 ** objects unusable. 51 */ 52 static void renameTestSchema(Parse *pParse, const char *zDb, int bTemp){ 53 sqlite3NestedParse(pParse, 54 "SELECT 1 " 55 "FROM \"%w\"." DFLT_SCHEMA_TABLE " " 56 "WHERE name NOT LIKE 'sqliteX_%%' ESCAPE 'X'" 57 " AND sql NOT LIKE 'create virtual%%'" 58 " AND sqlite_rename_test(%Q, sql, type, name, %d)=NULL ", 59 zDb, 60 zDb, bTemp 61 ); 62 63 if( bTemp==0 ){ 64 sqlite3NestedParse(pParse, 65 "SELECT 1 " 66 "FROM temp." DFLT_SCHEMA_TABLE " " 67 "WHERE name NOT LIKE 'sqliteX_%%' ESCAPE 'X'" 68 " AND sql NOT LIKE 'create virtual%%'" 69 " AND sqlite_rename_test(%Q, sql, type, name, 1)=NULL ", 70 zDb 71 ); 72 } 73 } 74 75 /* 76 ** Generate code to reload the schema for database iDb. And, if iDb!=1, for 77 ** the temp database as well. 78 */ 79 static void renameReloadSchema(Parse *pParse, int iDb){ 80 Vdbe *v = pParse->pVdbe; 81 if( v ){ 82 sqlite3ChangeCookie(pParse, iDb); 83 sqlite3VdbeAddParseSchemaOp(pParse->pVdbe, iDb, 0); 84 if( iDb!=1 ) sqlite3VdbeAddParseSchemaOp(pParse->pVdbe, 1, 0); 85 } 86 } 87 88 /* 89 ** Generate code to implement the "ALTER TABLE xxx RENAME TO yyy" 90 ** command. 91 */ 92 void sqlite3AlterRenameTable( 93 Parse *pParse, /* Parser context. */ 94 SrcList *pSrc, /* The table to rename. */ 95 Token *pName /* The new table name. */ 96 ){ 97 int iDb; /* Database that contains the table */ 98 char *zDb; /* Name of database iDb */ 99 Table *pTab; /* Table being renamed */ 100 char *zName = 0; /* NULL-terminated version of pName */ 101 sqlite3 *db = pParse->db; /* Database connection */ 102 int nTabName; /* Number of UTF-8 characters in zTabName */ 103 const char *zTabName; /* Original name of the table */ 104 Vdbe *v; 105 VTable *pVTab = 0; /* Non-zero if this is a v-tab with an xRename() */ 106 u32 savedDbFlags; /* Saved value of db->mDbFlags */ 107 108 savedDbFlags = db->mDbFlags; 109 if( NEVER(db->mallocFailed) ) goto exit_rename_table; 110 assert( pSrc->nSrc==1 ); 111 assert( sqlite3BtreeHoldsAllMutexes(pParse->db) ); 112 113 pTab = sqlite3LocateTableItem(pParse, 0, &pSrc->a[0]); 114 if( !pTab ) goto exit_rename_table; 115 iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema); 116 zDb = db->aDb[iDb].zDbSName; 117 db->mDbFlags |= DBFLAG_PreferBuiltin; 118 119 /* Get a NULL terminated version of the new table name. */ 120 zName = sqlite3NameFromToken(db, pName); 121 if( !zName ) goto exit_rename_table; 122 123 /* Check that a table or index named 'zName' does not already exist 124 ** in database iDb. If so, this is an error. 125 */ 126 if( sqlite3FindTable(db, zName, zDb) 127 || sqlite3FindIndex(db, zName, zDb) 128 || sqlite3IsShadowTableOf(db, pTab, zName) 129 ){ 130 sqlite3ErrorMsg(pParse, 131 "there is already another table or index with this name: %s", zName); 132 goto exit_rename_table; 133 } 134 135 /* Make sure it is not a system table being altered, or a reserved name 136 ** that the table is being renamed to. 137 */ 138 if( SQLITE_OK!=isAlterableTable(pParse, pTab) ){ 139 goto exit_rename_table; 140 } 141 if( SQLITE_OK!=sqlite3CheckObjectName(pParse,zName,"table",zName) ){ 142 goto exit_rename_table; 143 } 144 145 #ifndef SQLITE_OMIT_VIEW 146 if( pTab->pSelect ){ 147 sqlite3ErrorMsg(pParse, "view %s may not be altered", pTab->zName); 148 goto exit_rename_table; 149 } 150 #endif 151 152 #ifndef SQLITE_OMIT_AUTHORIZATION 153 /* Invoke the authorization callback. */ 154 if( sqlite3AuthCheck(pParse, SQLITE_ALTER_TABLE, zDb, pTab->zName, 0) ){ 155 goto exit_rename_table; 156 } 157 #endif 158 159 #ifndef SQLITE_OMIT_VIRTUALTABLE 160 if( sqlite3ViewGetColumnNames(pParse, pTab) ){ 161 goto exit_rename_table; 162 } 163 if( IsVirtual(pTab) ){ 164 pVTab = sqlite3GetVTable(db, pTab); 165 if( pVTab->pVtab->pModule->xRename==0 ){ 166 pVTab = 0; 167 } 168 } 169 #endif 170 171 /* Begin a transaction for database iDb. Then modify the schema cookie 172 ** (since the ALTER TABLE modifies the schema). Call sqlite3MayAbort(), 173 ** as the scalar functions (e.g. sqlite_rename_table()) invoked by the 174 ** nested SQL may raise an exception. */ 175 v = sqlite3GetVdbe(pParse); 176 if( v==0 ){ 177 goto exit_rename_table; 178 } 179 sqlite3MayAbort(pParse); 180 181 /* figure out how many UTF-8 characters are in zName */ 182 zTabName = pTab->zName; 183 nTabName = sqlite3Utf8CharLen(zTabName, -1); 184 185 /* Rewrite all CREATE TABLE, INDEX, TRIGGER or VIEW statements in 186 ** the schema to use the new table name. */ 187 sqlite3NestedParse(pParse, 188 "UPDATE \"%w\"." DFLT_SCHEMA_TABLE " SET " 189 "sql = sqlite_rename_table(%Q, type, name, sql, %Q, %Q, %d) " 190 "WHERE (type!='index' OR tbl_name=%Q COLLATE nocase)" 191 "AND name NOT LIKE 'sqliteX_%%' ESCAPE 'X'" 192 , zDb, zDb, zTabName, zName, (iDb==1), zTabName 193 ); 194 195 /* Update the tbl_name and name columns of the sqlite_schema table 196 ** as required. */ 197 sqlite3NestedParse(pParse, 198 "UPDATE %Q." DFLT_SCHEMA_TABLE " SET " 199 "tbl_name = %Q, " 200 "name = CASE " 201 "WHEN type='table' THEN %Q " 202 "WHEN name LIKE 'sqliteX_autoindex%%' ESCAPE 'X' " 203 " AND type='index' THEN " 204 "'sqlite_autoindex_' || %Q || substr(name,%d+18) " 205 "ELSE name END " 206 "WHERE tbl_name=%Q COLLATE nocase AND " 207 "(type='table' OR type='index' OR type='trigger');", 208 zDb, 209 zName, zName, zName, 210 nTabName, zTabName 211 ); 212 213 #ifndef SQLITE_OMIT_AUTOINCREMENT 214 /* If the sqlite_sequence table exists in this database, then update 215 ** it with the new table name. 216 */ 217 if( sqlite3FindTable(db, "sqlite_sequence", zDb) ){ 218 sqlite3NestedParse(pParse, 219 "UPDATE \"%w\".sqlite_sequence set name = %Q WHERE name = %Q", 220 zDb, zName, pTab->zName); 221 } 222 #endif 223 224 /* If the table being renamed is not itself part of the temp database, 225 ** edit view and trigger definitions within the temp database 226 ** as required. */ 227 if( iDb!=1 ){ 228 sqlite3NestedParse(pParse, 229 "UPDATE sqlite_temp_schema SET " 230 "sql = sqlite_rename_table(%Q, type, name, sql, %Q, %Q, 1), " 231 "tbl_name = " 232 "CASE WHEN tbl_name=%Q COLLATE nocase AND " 233 " sqlite_rename_test(%Q, sql, type, name, 1) " 234 "THEN %Q ELSE tbl_name END " 235 "WHERE type IN ('view', 'trigger')" 236 , zDb, zTabName, zName, zTabName, zDb, zName); 237 } 238 239 /* If this is a virtual table, invoke the xRename() function if 240 ** one is defined. The xRename() callback will modify the names 241 ** of any resources used by the v-table implementation (including other 242 ** SQLite tables) that are identified by the name of the virtual table. 243 */ 244 #ifndef SQLITE_OMIT_VIRTUALTABLE 245 if( pVTab ){ 246 int i = ++pParse->nMem; 247 sqlite3VdbeLoadString(v, i, zName); 248 sqlite3VdbeAddOp4(v, OP_VRename, i, 0, 0,(const char*)pVTab, P4_VTAB); 249 } 250 #endif 251 252 renameReloadSchema(pParse, iDb); 253 renameTestSchema(pParse, zDb, iDb==1); 254 255 exit_rename_table: 256 sqlite3SrcListDelete(db, pSrc); 257 sqlite3DbFree(db, zName); 258 db->mDbFlags = savedDbFlags; 259 } 260 261 /* 262 ** Write code that will raise an error if the table described by 263 ** zDb and zTab is not empty. 264 */ 265 static void sqlite3ErrorIfNotEmpty( 266 Parse *pParse, /* Parsing context */ 267 const char *zDb, /* Schema holding the table */ 268 const char *zTab, /* Table to check for empty */ 269 const char *zErr /* Error message text */ 270 ){ 271 sqlite3NestedParse(pParse, 272 "SELECT raise(ABORT,%Q) FROM \"%w\".\"%w\"", 273 zErr, zDb, zTab 274 ); 275 } 276 277 /* 278 ** This function is called after an "ALTER TABLE ... ADD" statement 279 ** has been parsed. Argument pColDef contains the text of the new 280 ** column definition. 281 ** 282 ** The Table structure pParse->pNewTable was extended to include 283 ** the new column during parsing. 284 */ 285 void sqlite3AlterFinishAddColumn(Parse *pParse, Token *pColDef){ 286 Table *pNew; /* Copy of pParse->pNewTable */ 287 Table *pTab; /* Table being altered */ 288 int iDb; /* Database number */ 289 const char *zDb; /* Database name */ 290 const char *zTab; /* Table name */ 291 char *zCol; /* Null-terminated column definition */ 292 Column *pCol; /* The new column */ 293 Expr *pDflt; /* Default value for the new column */ 294 sqlite3 *db; /* The database connection; */ 295 Vdbe *v; /* The prepared statement under construction */ 296 int r1; /* Temporary registers */ 297 298 db = pParse->db; 299 if( pParse->nErr || db->mallocFailed ) return; 300 pNew = pParse->pNewTable; 301 assert( pNew ); 302 303 assert( sqlite3BtreeHoldsAllMutexes(db) ); 304 iDb = sqlite3SchemaToIndex(db, pNew->pSchema); 305 zDb = db->aDb[iDb].zDbSName; 306 zTab = &pNew->zName[16]; /* Skip the "sqlite_altertab_" prefix on the name */ 307 pCol = &pNew->aCol[pNew->nCol-1]; 308 pDflt = pCol->pDflt; 309 pTab = sqlite3FindTable(db, zTab, zDb); 310 assert( pTab ); 311 312 #ifndef SQLITE_OMIT_AUTHORIZATION 313 /* Invoke the authorization callback. */ 314 if( sqlite3AuthCheck(pParse, SQLITE_ALTER_TABLE, zDb, pTab->zName, 0) ){ 315 return; 316 } 317 #endif 318 319 320 /* Check that the new column is not specified as PRIMARY KEY or UNIQUE. 321 ** If there is a NOT NULL constraint, then the default value for the 322 ** column must not be NULL. 323 */ 324 if( pCol->colFlags & COLFLAG_PRIMKEY ){ 325 sqlite3ErrorMsg(pParse, "Cannot add a PRIMARY KEY column"); 326 return; 327 } 328 if( pNew->pIndex ){ 329 sqlite3ErrorMsg(pParse, 330 "Cannot add a UNIQUE column"); 331 return; 332 } 333 if( (pCol->colFlags & COLFLAG_GENERATED)==0 ){ 334 /* If the default value for the new column was specified with a 335 ** literal NULL, then set pDflt to 0. This simplifies checking 336 ** for an SQL NULL default below. 337 */ 338 assert( pDflt==0 || pDflt->op==TK_SPAN ); 339 if( pDflt && pDflt->pLeft->op==TK_NULL ){ 340 pDflt = 0; 341 } 342 if( (db->flags&SQLITE_ForeignKeys) && pNew->pFKey && pDflt ){ 343 sqlite3ErrorIfNotEmpty(pParse, zDb, zTab, 344 "Cannot add a REFERENCES column with non-NULL default value"); 345 } 346 if( pCol->notNull && !pDflt ){ 347 sqlite3ErrorIfNotEmpty(pParse, zDb, zTab, 348 "Cannot add a NOT NULL column with default value NULL"); 349 } 350 351 352 /* Ensure the default expression is something that sqlite3ValueFromExpr() 353 ** can handle (i.e. not CURRENT_TIME etc.) 354 */ 355 if( pDflt ){ 356 sqlite3_value *pVal = 0; 357 int rc; 358 rc = sqlite3ValueFromExpr(db, pDflt, SQLITE_UTF8, SQLITE_AFF_BLOB, &pVal); 359 assert( rc==SQLITE_OK || rc==SQLITE_NOMEM ); 360 if( rc!=SQLITE_OK ){ 361 assert( db->mallocFailed == 1 ); 362 return; 363 } 364 if( !pVal ){ 365 sqlite3ErrorIfNotEmpty(pParse, zDb, zTab, 366 "Cannot add a column with non-constant default"); 367 } 368 sqlite3ValueFree(pVal); 369 } 370 }else if( pCol->colFlags & COLFLAG_STORED ){ 371 sqlite3ErrorIfNotEmpty(pParse, zDb, zTab, "cannot add a STORED column"); 372 } 373 374 375 /* Modify the CREATE TABLE statement. */ 376 zCol = sqlite3DbStrNDup(db, (char*)pColDef->z, pColDef->n); 377 if( zCol ){ 378 char *zEnd = &zCol[pColDef->n-1]; 379 u32 savedDbFlags = db->mDbFlags; 380 while( zEnd>zCol && (*zEnd==';' || sqlite3Isspace(*zEnd)) ){ 381 *zEnd-- = '\0'; 382 } 383 db->mDbFlags |= DBFLAG_PreferBuiltin; 384 /* substr() operations on characters, but addColOffset is in bytes. So we 385 ** have to use printf() to translate between these units: */ 386 sqlite3NestedParse(pParse, 387 "UPDATE \"%w\"." DFLT_SCHEMA_TABLE " SET " 388 "sql = printf('%%.%ds, ',sql) || %Q" 389 " || substr(sql,1+length(printf('%%.%ds',sql))) " 390 "WHERE type = 'table' AND name = %Q", 391 zDb, pNew->addColOffset, zCol, pNew->addColOffset, 392 zTab 393 ); 394 sqlite3DbFree(db, zCol); 395 db->mDbFlags = savedDbFlags; 396 } 397 398 /* Make sure the schema version is at least 3. But do not upgrade 399 ** from less than 3 to 4, as that will corrupt any preexisting DESC 400 ** index. 401 */ 402 v = sqlite3GetVdbe(pParse); 403 if( v ){ 404 r1 = sqlite3GetTempReg(pParse); 405 sqlite3VdbeAddOp3(v, OP_ReadCookie, iDb, r1, BTREE_FILE_FORMAT); 406 sqlite3VdbeUsesBtree(v, iDb); 407 sqlite3VdbeAddOp2(v, OP_AddImm, r1, -2); 408 sqlite3VdbeAddOp2(v, OP_IfPos, r1, sqlite3VdbeCurrentAddr(v)+2); 409 VdbeCoverage(v); 410 sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, BTREE_FILE_FORMAT, 3); 411 sqlite3ReleaseTempReg(pParse, r1); 412 } 413 414 /* Reload the table definition */ 415 renameReloadSchema(pParse, iDb); 416 } 417 418 /* 419 ** This function is called by the parser after the table-name in 420 ** an "ALTER TABLE <table-name> ADD" statement is parsed. Argument 421 ** pSrc is the full-name of the table being altered. 422 ** 423 ** This routine makes a (partial) copy of the Table structure 424 ** for the table being altered and sets Parse.pNewTable to point 425 ** to it. Routines called by the parser as the column definition 426 ** is parsed (i.e. sqlite3AddColumn()) add the new Column data to 427 ** the copy. The copy of the Table structure is deleted by tokenize.c 428 ** after parsing is finished. 429 ** 430 ** Routine sqlite3AlterFinishAddColumn() will be called to complete 431 ** coding the "ALTER TABLE ... ADD" statement. 432 */ 433 void sqlite3AlterBeginAddColumn(Parse *pParse, SrcList *pSrc){ 434 Table *pNew; 435 Table *pTab; 436 int iDb; 437 int i; 438 int nAlloc; 439 sqlite3 *db = pParse->db; 440 441 /* Look up the table being altered. */ 442 assert( pParse->pNewTable==0 ); 443 assert( sqlite3BtreeHoldsAllMutexes(db) ); 444 if( db->mallocFailed ) goto exit_begin_add_column; 445 pTab = sqlite3LocateTableItem(pParse, 0, &pSrc->a[0]); 446 if( !pTab ) goto exit_begin_add_column; 447 448 #ifndef SQLITE_OMIT_VIRTUALTABLE 449 if( IsVirtual(pTab) ){ 450 sqlite3ErrorMsg(pParse, "virtual tables may not be altered"); 451 goto exit_begin_add_column; 452 } 453 #endif 454 455 /* Make sure this is not an attempt to ALTER a view. */ 456 if( pTab->pSelect ){ 457 sqlite3ErrorMsg(pParse, "Cannot add a column to a view"); 458 goto exit_begin_add_column; 459 } 460 if( SQLITE_OK!=isAlterableTable(pParse, pTab) ){ 461 goto exit_begin_add_column; 462 } 463 464 sqlite3MayAbort(pParse); 465 assert( pTab->addColOffset>0 ); 466 iDb = sqlite3SchemaToIndex(db, pTab->pSchema); 467 468 /* Put a copy of the Table struct in Parse.pNewTable for the 469 ** sqlite3AddColumn() function and friends to modify. But modify 470 ** the name by adding an "sqlite_altertab_" prefix. By adding this 471 ** prefix, we insure that the name will not collide with an existing 472 ** table because user table are not allowed to have the "sqlite_" 473 ** prefix on their name. 474 */ 475 pNew = (Table*)sqlite3DbMallocZero(db, sizeof(Table)); 476 if( !pNew ) goto exit_begin_add_column; 477 pParse->pNewTable = pNew; 478 pNew->nTabRef = 1; 479 pNew->nCol = pTab->nCol; 480 assert( pNew->nCol>0 ); 481 nAlloc = (((pNew->nCol-1)/8)*8)+8; 482 assert( nAlloc>=pNew->nCol && nAlloc%8==0 && nAlloc-pNew->nCol<8 ); 483 pNew->aCol = (Column*)sqlite3DbMallocZero(db, sizeof(Column)*nAlloc); 484 pNew->zName = sqlite3MPrintf(db, "sqlite_altertab_%s", pTab->zName); 485 if( !pNew->aCol || !pNew->zName ){ 486 assert( db->mallocFailed ); 487 goto exit_begin_add_column; 488 } 489 memcpy(pNew->aCol, pTab->aCol, sizeof(Column)*pNew->nCol); 490 for(i=0; i<pNew->nCol; i++){ 491 Column *pCol = &pNew->aCol[i]; 492 pCol->zName = sqlite3DbStrDup(db, pCol->zName); 493 pCol->hName = sqlite3StrIHash(pCol->zName); 494 pCol->zColl = 0; 495 pCol->pDflt = 0; 496 } 497 pNew->pSchema = db->aDb[iDb].pSchema; 498 pNew->addColOffset = pTab->addColOffset; 499 pNew->nTabRef = 1; 500 501 exit_begin_add_column: 502 sqlite3SrcListDelete(db, pSrc); 503 return; 504 } 505 506 /* 507 ** Parameter pTab is the subject of an ALTER TABLE ... RENAME COLUMN 508 ** command. This function checks if the table is a view or virtual 509 ** table (columns of views or virtual tables may not be renamed). If so, 510 ** it loads an error message into pParse and returns non-zero. 511 ** 512 ** Or, if pTab is not a view or virtual table, zero is returned. 513 */ 514 #if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_VIRTUALTABLE) 515 static int isRealTable(Parse *pParse, Table *pTab){ 516 const char *zType = 0; 517 #ifndef SQLITE_OMIT_VIEW 518 if( pTab->pSelect ){ 519 zType = "view"; 520 } 521 #endif 522 #ifndef SQLITE_OMIT_VIRTUALTABLE 523 if( IsVirtual(pTab) ){ 524 zType = "virtual table"; 525 } 526 #endif 527 if( zType ){ 528 sqlite3ErrorMsg( 529 pParse, "cannot rename columns of %s \"%s\"", zType, pTab->zName 530 ); 531 return 1; 532 } 533 return 0; 534 } 535 #else /* !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_VIRTUALTABLE) */ 536 # define isRealTable(x,y) (0) 537 #endif 538 539 /* 540 ** Handles the following parser reduction: 541 ** 542 ** cmd ::= ALTER TABLE pSrc RENAME COLUMN pOld TO pNew 543 */ 544 void sqlite3AlterRenameColumn( 545 Parse *pParse, /* Parsing context */ 546 SrcList *pSrc, /* Table being altered. pSrc->nSrc==1 */ 547 Token *pOld, /* Name of column being changed */ 548 Token *pNew /* New column name */ 549 ){ 550 sqlite3 *db = pParse->db; /* Database connection */ 551 Table *pTab; /* Table being updated */ 552 int iCol; /* Index of column being renamed */ 553 char *zOld = 0; /* Old column name */ 554 char *zNew = 0; /* New column name */ 555 const char *zDb; /* Name of schema containing the table */ 556 int iSchema; /* Index of the schema */ 557 int bQuote; /* True to quote the new name */ 558 559 /* Locate the table to be altered */ 560 pTab = sqlite3LocateTableItem(pParse, 0, &pSrc->a[0]); 561 if( !pTab ) goto exit_rename_column; 562 563 /* Cannot alter a system table */ 564 if( SQLITE_OK!=isAlterableTable(pParse, pTab) ) goto exit_rename_column; 565 if( SQLITE_OK!=isRealTable(pParse, pTab) ) goto exit_rename_column; 566 567 /* Which schema holds the table to be altered */ 568 iSchema = sqlite3SchemaToIndex(db, pTab->pSchema); 569 assert( iSchema>=0 ); 570 zDb = db->aDb[iSchema].zDbSName; 571 572 #ifndef SQLITE_OMIT_AUTHORIZATION 573 /* Invoke the authorization callback. */ 574 if( sqlite3AuthCheck(pParse, SQLITE_ALTER_TABLE, zDb, pTab->zName, 0) ){ 575 goto exit_rename_column; 576 } 577 #endif 578 579 /* Make sure the old name really is a column name in the table to be 580 ** altered. Set iCol to be the index of the column being renamed */ 581 zOld = sqlite3NameFromToken(db, pOld); 582 if( !zOld ) goto exit_rename_column; 583 for(iCol=0; iCol<pTab->nCol; iCol++){ 584 if( 0==sqlite3StrICmp(pTab->aCol[iCol].zName, zOld) ) break; 585 } 586 if( iCol==pTab->nCol ){ 587 sqlite3ErrorMsg(pParse, "no such column: \"%s\"", zOld); 588 goto exit_rename_column; 589 } 590 591 /* Do the rename operation using a recursive UPDATE statement that 592 ** uses the sqlite_rename_column() SQL function to compute the new 593 ** CREATE statement text for the sqlite_schema table. 594 */ 595 sqlite3MayAbort(pParse); 596 zNew = sqlite3NameFromToken(db, pNew); 597 if( !zNew ) goto exit_rename_column; 598 assert( pNew->n>0 ); 599 bQuote = sqlite3Isquote(pNew->z[0]); 600 sqlite3NestedParse(pParse, 601 "UPDATE \"%w\"." DFLT_SCHEMA_TABLE " SET " 602 "sql = sqlite_rename_column(sql, type, name, %Q, %Q, %d, %Q, %d, %d) " 603 "WHERE name NOT LIKE 'sqliteX_%%' ESCAPE 'X' " 604 " AND (type != 'index' OR tbl_name = %Q)" 605 " AND sql NOT LIKE 'create virtual%%'", 606 zDb, 607 zDb, pTab->zName, iCol, zNew, bQuote, iSchema==1, 608 pTab->zName 609 ); 610 611 sqlite3NestedParse(pParse, 612 "UPDATE temp." DFLT_SCHEMA_TABLE " SET " 613 "sql = sqlite_rename_column(sql, type, name, %Q, %Q, %d, %Q, %d, 1) " 614 "WHERE type IN ('trigger', 'view')", 615 zDb, pTab->zName, iCol, zNew, bQuote 616 ); 617 618 /* Drop and reload the database schema. */ 619 renameReloadSchema(pParse, iSchema); 620 renameTestSchema(pParse, zDb, iSchema==1); 621 622 exit_rename_column: 623 sqlite3SrcListDelete(db, pSrc); 624 sqlite3DbFree(db, zOld); 625 sqlite3DbFree(db, zNew); 626 return; 627 } 628 629 /* 630 ** Each RenameToken object maps an element of the parse tree into 631 ** the token that generated that element. The parse tree element 632 ** might be one of: 633 ** 634 ** * A pointer to an Expr that represents an ID 635 ** * The name of a table column in Column.zName 636 ** 637 ** A list of RenameToken objects can be constructed during parsing. 638 ** Each new object is created by sqlite3RenameTokenMap(). 639 ** As the parse tree is transformed, the sqlite3RenameTokenRemap() 640 ** routine is used to keep the mapping current. 641 ** 642 ** After the parse finishes, renameTokenFind() routine can be used 643 ** to look up the actual token value that created some element in 644 ** the parse tree. 645 */ 646 struct RenameToken { 647 void *p; /* Parse tree element created by token t */ 648 Token t; /* The token that created parse tree element p */ 649 RenameToken *pNext; /* Next is a list of all RenameToken objects */ 650 }; 651 652 /* 653 ** The context of an ALTER TABLE RENAME COLUMN operation that gets passed 654 ** down into the Walker. 655 */ 656 typedef struct RenameCtx RenameCtx; 657 struct RenameCtx { 658 RenameToken *pList; /* List of tokens to overwrite */ 659 int nList; /* Number of tokens in pList */ 660 int iCol; /* Index of column being renamed */ 661 Table *pTab; /* Table being ALTERed */ 662 const char *zOld; /* Old column name */ 663 }; 664 665 #ifdef SQLITE_DEBUG 666 /* 667 ** This function is only for debugging. It performs two tasks: 668 ** 669 ** 1. Checks that pointer pPtr does not already appear in the 670 ** rename-token list. 671 ** 672 ** 2. Dereferences each pointer in the rename-token list. 673 ** 674 ** The second is most effective when debugging under valgrind or 675 ** address-sanitizer or similar. If any of these pointers no longer 676 ** point to valid objects, an exception is raised by the memory-checking 677 ** tool. 678 ** 679 ** The point of this is to prevent comparisons of invalid pointer values. 680 ** Even though this always seems to work, it is undefined according to the 681 ** C standard. Example of undefined comparison: 682 ** 683 ** sqlite3_free(x); 684 ** if( x==y ) ... 685 ** 686 ** Technically, as x no longer points into a valid object or to the byte 687 ** following a valid object, it may not be used in comparison operations. 688 */ 689 static void renameTokenCheckAll(Parse *pParse, void *pPtr){ 690 if( pParse->nErr==0 && pParse->db->mallocFailed==0 ){ 691 RenameToken *p; 692 u8 i = 0; 693 for(p=pParse->pRename; p; p=p->pNext){ 694 if( p->p ){ 695 assert( p->p!=pPtr ); 696 i += *(u8*)(p->p); 697 } 698 } 699 } 700 } 701 #else 702 # define renameTokenCheckAll(x,y) 703 #endif 704 705 /* 706 ** Remember that the parser tree element pPtr was created using 707 ** the token pToken. 708 ** 709 ** In other words, construct a new RenameToken object and add it 710 ** to the list of RenameToken objects currently being built up 711 ** in pParse->pRename. 712 ** 713 ** The pPtr argument is returned so that this routine can be used 714 ** with tail recursion in tokenExpr() routine, for a small performance 715 ** improvement. 716 */ 717 void *sqlite3RenameTokenMap(Parse *pParse, void *pPtr, Token *pToken){ 718 RenameToken *pNew; 719 assert( pPtr || pParse->db->mallocFailed ); 720 renameTokenCheckAll(pParse, pPtr); 721 if( ALWAYS(pParse->eParseMode!=PARSE_MODE_UNMAP) ){ 722 pNew = sqlite3DbMallocZero(pParse->db, sizeof(RenameToken)); 723 if( pNew ){ 724 pNew->p = pPtr; 725 pNew->t = *pToken; 726 pNew->pNext = pParse->pRename; 727 pParse->pRename = pNew; 728 } 729 } 730 731 return pPtr; 732 } 733 734 /* 735 ** It is assumed that there is already a RenameToken object associated 736 ** with parse tree element pFrom. This function remaps the associated token 737 ** to parse tree element pTo. 738 */ 739 void sqlite3RenameTokenRemap(Parse *pParse, void *pTo, void *pFrom){ 740 RenameToken *p; 741 renameTokenCheckAll(pParse, pTo); 742 for(p=pParse->pRename; p; p=p->pNext){ 743 if( p->p==pFrom ){ 744 p->p = pTo; 745 break; 746 } 747 } 748 } 749 750 /* 751 ** Walker callback used by sqlite3RenameExprUnmap(). 752 */ 753 static int renameUnmapExprCb(Walker *pWalker, Expr *pExpr){ 754 Parse *pParse = pWalker->pParse; 755 sqlite3RenameTokenRemap(pParse, 0, (void*)pExpr); 756 return WRC_Continue; 757 } 758 759 /* 760 ** Iterate through the Select objects that are part of WITH clauses attached 761 ** to select statement pSelect. 762 */ 763 static void renameWalkWith(Walker *pWalker, Select *pSelect){ 764 With *pWith = pSelect->pWith; 765 if( pWith ){ 766 int i; 767 for(i=0; i<pWith->nCte; i++){ 768 Select *p = pWith->a[i].pSelect; 769 NameContext sNC; 770 memset(&sNC, 0, sizeof(sNC)); 771 sNC.pParse = pWalker->pParse; 772 sqlite3SelectPrep(sNC.pParse, p, &sNC); 773 sqlite3WalkSelect(pWalker, p); 774 sqlite3RenameExprlistUnmap(pWalker->pParse, pWith->a[i].pCols); 775 } 776 } 777 } 778 779 /* 780 ** Unmap all tokens in the IdList object passed as the second argument. 781 */ 782 static void unmapColumnIdlistNames( 783 Parse *pParse, 784 IdList *pIdList 785 ){ 786 if( pIdList ){ 787 int ii; 788 for(ii=0; ii<pIdList->nId; ii++){ 789 sqlite3RenameTokenRemap(pParse, 0, (void*)pIdList->a[ii].zName); 790 } 791 } 792 } 793 794 /* 795 ** Walker callback used by sqlite3RenameExprUnmap(). 796 */ 797 static int renameUnmapSelectCb(Walker *pWalker, Select *p){ 798 Parse *pParse = pWalker->pParse; 799 int i; 800 if( pParse->nErr ) return WRC_Abort; 801 if( NEVER(p->selFlags & SF_View) ) return WRC_Prune; 802 if( ALWAYS(p->pEList) ){ 803 ExprList *pList = p->pEList; 804 for(i=0; i<pList->nExpr; i++){ 805 if( pList->a[i].zEName && pList->a[i].eEName==ENAME_NAME ){ 806 sqlite3RenameTokenRemap(pParse, 0, (void*)pList->a[i].zEName); 807 } 808 } 809 } 810 if( ALWAYS(p->pSrc) ){ /* Every Select as a SrcList, even if it is empty */ 811 SrcList *pSrc = p->pSrc; 812 for(i=0; i<pSrc->nSrc; i++){ 813 sqlite3RenameTokenRemap(pParse, 0, (void*)pSrc->a[i].zName); 814 if( sqlite3WalkExpr(pWalker, pSrc->a[i].pOn) ) return WRC_Abort; 815 unmapColumnIdlistNames(pParse, pSrc->a[i].pUsing); 816 } 817 } 818 819 renameWalkWith(pWalker, p); 820 return WRC_Continue; 821 } 822 823 /* 824 ** Remove all nodes that are part of expression pExpr from the rename list. 825 */ 826 void sqlite3RenameExprUnmap(Parse *pParse, Expr *pExpr){ 827 u8 eMode = pParse->eParseMode; 828 Walker sWalker; 829 memset(&sWalker, 0, sizeof(Walker)); 830 sWalker.pParse = pParse; 831 sWalker.xExprCallback = renameUnmapExprCb; 832 sWalker.xSelectCallback = renameUnmapSelectCb; 833 pParse->eParseMode = PARSE_MODE_UNMAP; 834 sqlite3WalkExpr(&sWalker, pExpr); 835 pParse->eParseMode = eMode; 836 } 837 838 /* 839 ** Remove all nodes that are part of expression-list pEList from the 840 ** rename list. 841 */ 842 void sqlite3RenameExprlistUnmap(Parse *pParse, ExprList *pEList){ 843 if( pEList ){ 844 int i; 845 Walker sWalker; 846 memset(&sWalker, 0, sizeof(Walker)); 847 sWalker.pParse = pParse; 848 sWalker.xExprCallback = renameUnmapExprCb; 849 sqlite3WalkExprList(&sWalker, pEList); 850 for(i=0; i<pEList->nExpr; i++){ 851 if( ALWAYS(pEList->a[i].eEName==ENAME_NAME) ){ 852 sqlite3RenameTokenRemap(pParse, 0, (void*)pEList->a[i].zEName); 853 } 854 } 855 } 856 } 857 858 /* 859 ** Free the list of RenameToken objects given in the second argument 860 */ 861 static void renameTokenFree(sqlite3 *db, RenameToken *pToken){ 862 RenameToken *pNext; 863 RenameToken *p; 864 for(p=pToken; p; p=pNext){ 865 pNext = p->pNext; 866 sqlite3DbFree(db, p); 867 } 868 } 869 870 /* 871 ** Search the Parse object passed as the first argument for a RenameToken 872 ** object associated with parse tree element pPtr. If found, remove it 873 ** from the Parse object and add it to the list maintained by the 874 ** RenameCtx object passed as the second argument. 875 */ 876 static void renameTokenFind(Parse *pParse, struct RenameCtx *pCtx, void *pPtr){ 877 RenameToken **pp; 878 assert( pPtr!=0 ); 879 for(pp=&pParse->pRename; (*pp); pp=&(*pp)->pNext){ 880 if( (*pp)->p==pPtr ){ 881 RenameToken *pToken = *pp; 882 *pp = pToken->pNext; 883 pToken->pNext = pCtx->pList; 884 pCtx->pList = pToken; 885 pCtx->nList++; 886 break; 887 } 888 } 889 } 890 891 /* 892 ** This is a Walker select callback. It does nothing. It is only required 893 ** because without a dummy callback, sqlite3WalkExpr() and similar do not 894 ** descend into sub-select statements. 895 */ 896 static int renameColumnSelectCb(Walker *pWalker, Select *p){ 897 if( p->selFlags & SF_View ) return WRC_Prune; 898 renameWalkWith(pWalker, p); 899 return WRC_Continue; 900 } 901 902 /* 903 ** This is a Walker expression callback. 904 ** 905 ** For every TK_COLUMN node in the expression tree, search to see 906 ** if the column being references is the column being renamed by an 907 ** ALTER TABLE statement. If it is, then attach its associated 908 ** RenameToken object to the list of RenameToken objects being 909 ** constructed in RenameCtx object at pWalker->u.pRename. 910 */ 911 static int renameColumnExprCb(Walker *pWalker, Expr *pExpr){ 912 RenameCtx *p = pWalker->u.pRename; 913 if( pExpr->op==TK_TRIGGER 914 && pExpr->iColumn==p->iCol 915 && pWalker->pParse->pTriggerTab==p->pTab 916 ){ 917 renameTokenFind(pWalker->pParse, p, (void*)pExpr); 918 }else if( pExpr->op==TK_COLUMN 919 && pExpr->iColumn==p->iCol 920 && p->pTab==pExpr->y.pTab 921 ){ 922 renameTokenFind(pWalker->pParse, p, (void*)pExpr); 923 } 924 return WRC_Continue; 925 } 926 927 /* 928 ** The RenameCtx contains a list of tokens that reference a column that 929 ** is being renamed by an ALTER TABLE statement. Return the "last" 930 ** RenameToken in the RenameCtx and remove that RenameToken from the 931 ** RenameContext. "Last" means the last RenameToken encountered when 932 ** the input SQL is parsed from left to right. Repeated calls to this routine 933 ** return all column name tokens in the order that they are encountered 934 ** in the SQL statement. 935 */ 936 static RenameToken *renameColumnTokenNext(RenameCtx *pCtx){ 937 RenameToken *pBest = pCtx->pList; 938 RenameToken *pToken; 939 RenameToken **pp; 940 941 for(pToken=pBest->pNext; pToken; pToken=pToken->pNext){ 942 if( pToken->t.z>pBest->t.z ) pBest = pToken; 943 } 944 for(pp=&pCtx->pList; *pp!=pBest; pp=&(*pp)->pNext); 945 *pp = pBest->pNext; 946 947 return pBest; 948 } 949 950 /* 951 ** An error occured while parsing or otherwise processing a database 952 ** object (either pParse->pNewTable, pNewIndex or pNewTrigger) as part of an 953 ** ALTER TABLE RENAME COLUMN program. The error message emitted by the 954 ** sub-routine is currently stored in pParse->zErrMsg. This function 955 ** adds context to the error message and then stores it in pCtx. 956 */ 957 static void renameColumnParseError( 958 sqlite3_context *pCtx, 959 int bPost, 960 sqlite3_value *pType, 961 sqlite3_value *pObject, 962 Parse *pParse 963 ){ 964 const char *zT = (const char*)sqlite3_value_text(pType); 965 const char *zN = (const char*)sqlite3_value_text(pObject); 966 char *zErr; 967 968 zErr = sqlite3_mprintf("error in %s %s%s: %s", 969 zT, zN, (bPost ? " after rename" : ""), 970 pParse->zErrMsg 971 ); 972 sqlite3_result_error(pCtx, zErr, -1); 973 sqlite3_free(zErr); 974 } 975 976 /* 977 ** For each name in the the expression-list pEList (i.e. each 978 ** pEList->a[i].zName) that matches the string in zOld, extract the 979 ** corresponding rename-token from Parse object pParse and add it 980 ** to the RenameCtx pCtx. 981 */ 982 static void renameColumnElistNames( 983 Parse *pParse, 984 RenameCtx *pCtx, 985 ExprList *pEList, 986 const char *zOld 987 ){ 988 if( pEList ){ 989 int i; 990 for(i=0; i<pEList->nExpr; i++){ 991 char *zName = pEList->a[i].zEName; 992 if( ALWAYS(pEList->a[i].eEName==ENAME_NAME) 993 && ALWAYS(zName!=0) 994 && 0==sqlite3_stricmp(zName, zOld) 995 ){ 996 renameTokenFind(pParse, pCtx, (void*)zName); 997 } 998 } 999 } 1000 } 1001 1002 /* 1003 ** For each name in the the id-list pIdList (i.e. each pIdList->a[i].zName) 1004 ** that matches the string in zOld, extract the corresponding rename-token 1005 ** from Parse object pParse and add it to the RenameCtx pCtx. 1006 */ 1007 static void renameColumnIdlistNames( 1008 Parse *pParse, 1009 RenameCtx *pCtx, 1010 IdList *pIdList, 1011 const char *zOld 1012 ){ 1013 if( pIdList ){ 1014 int i; 1015 for(i=0; i<pIdList->nId; i++){ 1016 char *zName = pIdList->a[i].zName; 1017 if( 0==sqlite3_stricmp(zName, zOld) ){ 1018 renameTokenFind(pParse, pCtx, (void*)zName); 1019 } 1020 } 1021 } 1022 } 1023 1024 1025 /* 1026 ** Parse the SQL statement zSql using Parse object (*p). The Parse object 1027 ** is initialized by this function before it is used. 1028 */ 1029 static int renameParseSql( 1030 Parse *p, /* Memory to use for Parse object */ 1031 const char *zDb, /* Name of schema SQL belongs to */ 1032 sqlite3 *db, /* Database handle */ 1033 const char *zSql, /* SQL to parse */ 1034 int bTemp /* True if SQL is from temp schema */ 1035 ){ 1036 int rc; 1037 char *zErr = 0; 1038 1039 db->init.iDb = bTemp ? 1 : sqlite3FindDbName(db, zDb); 1040 1041 /* Parse the SQL statement passed as the first argument. If no error 1042 ** occurs and the parse does not result in a new table, index or 1043 ** trigger object, the database must be corrupt. */ 1044 memset(p, 0, sizeof(Parse)); 1045 p->eParseMode = PARSE_MODE_RENAME; 1046 p->db = db; 1047 p->nQueryLoop = 1; 1048 rc = sqlite3RunParser(p, zSql, &zErr); 1049 assert( p->zErrMsg==0 ); 1050 assert( rc!=SQLITE_OK || zErr==0 ); 1051 p->zErrMsg = zErr; 1052 if( db->mallocFailed ) rc = SQLITE_NOMEM; 1053 if( rc==SQLITE_OK 1054 && p->pNewTable==0 && p->pNewIndex==0 && p->pNewTrigger==0 1055 ){ 1056 rc = SQLITE_CORRUPT_BKPT; 1057 } 1058 1059 #ifdef SQLITE_DEBUG 1060 /* Ensure that all mappings in the Parse.pRename list really do map to 1061 ** a part of the input string. */ 1062 if( rc==SQLITE_OK ){ 1063 int nSql = sqlite3Strlen30(zSql); 1064 RenameToken *pToken; 1065 for(pToken=p->pRename; pToken; pToken=pToken->pNext){ 1066 assert( pToken->t.z>=zSql && &pToken->t.z[pToken->t.n]<=&zSql[nSql] ); 1067 } 1068 } 1069 #endif 1070 1071 db->init.iDb = 0; 1072 return rc; 1073 } 1074 1075 /* 1076 ** This function edits SQL statement zSql, replacing each token identified 1077 ** by the linked list pRename with the text of zNew. If argument bQuote is 1078 ** true, then zNew is always quoted first. If no error occurs, the result 1079 ** is loaded into context object pCtx as the result. 1080 ** 1081 ** Or, if an error occurs (i.e. an OOM condition), an error is left in 1082 ** pCtx and an SQLite error code returned. 1083 */ 1084 static int renameEditSql( 1085 sqlite3_context *pCtx, /* Return result here */ 1086 RenameCtx *pRename, /* Rename context */ 1087 const char *zSql, /* SQL statement to edit */ 1088 const char *zNew, /* New token text */ 1089 int bQuote /* True to always quote token */ 1090 ){ 1091 int nNew = sqlite3Strlen30(zNew); 1092 int nSql = sqlite3Strlen30(zSql); 1093 sqlite3 *db = sqlite3_context_db_handle(pCtx); 1094 int rc = SQLITE_OK; 1095 char *zQuot; 1096 char *zOut; 1097 int nQuot; 1098 1099 /* Set zQuot to point to a buffer containing a quoted copy of the 1100 ** identifier zNew. If the corresponding identifier in the original 1101 ** ALTER TABLE statement was quoted (bQuote==1), then set zNew to 1102 ** point to zQuot so that all substitutions are made using the 1103 ** quoted version of the new column name. */ 1104 zQuot = sqlite3MPrintf(db, "\"%w\"", zNew); 1105 if( zQuot==0 ){ 1106 return SQLITE_NOMEM; 1107 }else{ 1108 nQuot = sqlite3Strlen30(zQuot); 1109 } 1110 if( bQuote ){ 1111 zNew = zQuot; 1112 nNew = nQuot; 1113 } 1114 1115 /* At this point pRename->pList contains a list of RenameToken objects 1116 ** corresponding to all tokens in the input SQL that must be replaced 1117 ** with the new column name. All that remains is to construct and 1118 ** return the edited SQL string. */ 1119 assert( nQuot>=nNew ); 1120 zOut = sqlite3DbMallocZero(db, nSql + pRename->nList*nQuot + 1); 1121 if( zOut ){ 1122 int nOut = nSql; 1123 memcpy(zOut, zSql, nSql); 1124 while( pRename->pList ){ 1125 int iOff; /* Offset of token to replace in zOut */ 1126 RenameToken *pBest = renameColumnTokenNext(pRename); 1127 1128 u32 nReplace; 1129 const char *zReplace; 1130 if( sqlite3IsIdChar(*pBest->t.z) ){ 1131 nReplace = nNew; 1132 zReplace = zNew; 1133 }else{ 1134 nReplace = nQuot; 1135 zReplace = zQuot; 1136 } 1137 1138 iOff = pBest->t.z - zSql; 1139 if( pBest->t.n!=nReplace ){ 1140 memmove(&zOut[iOff + nReplace], &zOut[iOff + pBest->t.n], 1141 nOut - (iOff + pBest->t.n) 1142 ); 1143 nOut += nReplace - pBest->t.n; 1144 zOut[nOut] = '\0'; 1145 } 1146 memcpy(&zOut[iOff], zReplace, nReplace); 1147 sqlite3DbFree(db, pBest); 1148 } 1149 1150 sqlite3_result_text(pCtx, zOut, -1, SQLITE_TRANSIENT); 1151 sqlite3DbFree(db, zOut); 1152 }else{ 1153 rc = SQLITE_NOMEM; 1154 } 1155 1156 sqlite3_free(zQuot); 1157 return rc; 1158 } 1159 1160 /* 1161 ** Resolve all symbols in the trigger at pParse->pNewTrigger, assuming 1162 ** it was read from the schema of database zDb. Return SQLITE_OK if 1163 ** successful. Otherwise, return an SQLite error code and leave an error 1164 ** message in the Parse object. 1165 */ 1166 static int renameResolveTrigger(Parse *pParse){ 1167 sqlite3 *db = pParse->db; 1168 Trigger *pNew = pParse->pNewTrigger; 1169 TriggerStep *pStep; 1170 NameContext sNC; 1171 int rc = SQLITE_OK; 1172 1173 memset(&sNC, 0, sizeof(sNC)); 1174 sNC.pParse = pParse; 1175 assert( pNew->pTabSchema ); 1176 pParse->pTriggerTab = sqlite3FindTable(db, pNew->table, 1177 db->aDb[sqlite3SchemaToIndex(db, pNew->pTabSchema)].zDbSName 1178 ); 1179 pParse->eTriggerOp = pNew->op; 1180 /* ALWAYS() because if the table of the trigger does not exist, the 1181 ** error would have been hit before this point */ 1182 if( ALWAYS(pParse->pTriggerTab) ){ 1183 rc = sqlite3ViewGetColumnNames(pParse, pParse->pTriggerTab); 1184 } 1185 1186 /* Resolve symbols in WHEN clause */ 1187 if( rc==SQLITE_OK && pNew->pWhen ){ 1188 rc = sqlite3ResolveExprNames(&sNC, pNew->pWhen); 1189 } 1190 1191 for(pStep=pNew->step_list; rc==SQLITE_OK && pStep; pStep=pStep->pNext){ 1192 if( pStep->pSelect ){ 1193 sqlite3SelectPrep(pParse, pStep->pSelect, &sNC); 1194 if( pParse->nErr ) rc = pParse->rc; 1195 } 1196 if( rc==SQLITE_OK && pStep->zTarget ){ 1197 SrcList *pSrc = sqlite3TriggerStepSrc(pParse, pStep); 1198 if( pSrc ){ 1199 int i; 1200 for(i=0; i<pSrc->nSrc && rc==SQLITE_OK; i++){ 1201 struct SrcList_item *p = &pSrc->a[i]; 1202 p->iCursor = pParse->nTab++; 1203 if( p->pSelect ){ 1204 sqlite3SelectPrep(pParse, p->pSelect, 0); 1205 sqlite3ExpandSubquery(pParse, p); 1206 assert( i>0 ); 1207 assert( pStep->pFrom->a[i-1].pSelect ); 1208 sqlite3SelectPrep(pParse, pStep->pFrom->a[i-1].pSelect, 0); 1209 }else{ 1210 p->pTab = sqlite3LocateTableItem(pParse, 0, p); 1211 if( p->pTab==0 ){ 1212 rc = SQLITE_ERROR; 1213 }else{ 1214 p->pTab->nTabRef++; 1215 rc = sqlite3ViewGetColumnNames(pParse, p->pTab); 1216 } 1217 } 1218 } 1219 sNC.pSrcList = pSrc; 1220 if( rc==SQLITE_OK && pStep->pWhere ){ 1221 rc = sqlite3ResolveExprNames(&sNC, pStep->pWhere); 1222 } 1223 if( rc==SQLITE_OK ){ 1224 rc = sqlite3ResolveExprListNames(&sNC, pStep->pExprList); 1225 } 1226 assert( !pStep->pUpsert || (!pStep->pWhere && !pStep->pExprList) ); 1227 if( pStep->pUpsert ){ 1228 Upsert *pUpsert = pStep->pUpsert; 1229 assert( rc==SQLITE_OK ); 1230 pUpsert->pUpsertSrc = pSrc; 1231 sNC.uNC.pUpsert = pUpsert; 1232 sNC.ncFlags = NC_UUpsert; 1233 rc = sqlite3ResolveExprListNames(&sNC, pUpsert->pUpsertTarget); 1234 if( rc==SQLITE_OK ){ 1235 ExprList *pUpsertSet = pUpsert->pUpsertSet; 1236 rc = sqlite3ResolveExprListNames(&sNC, pUpsertSet); 1237 } 1238 if( rc==SQLITE_OK ){ 1239 rc = sqlite3ResolveExprNames(&sNC, pUpsert->pUpsertWhere); 1240 } 1241 if( rc==SQLITE_OK ){ 1242 rc = sqlite3ResolveExprNames(&sNC, pUpsert->pUpsertTargetWhere); 1243 } 1244 sNC.ncFlags = 0; 1245 } 1246 sNC.pSrcList = 0; 1247 sqlite3SrcListDelete(db, pSrc); 1248 }else{ 1249 rc = SQLITE_NOMEM; 1250 } 1251 } 1252 } 1253 return rc; 1254 } 1255 1256 /* 1257 ** Invoke sqlite3WalkExpr() or sqlite3WalkSelect() on all Select or Expr 1258 ** objects that are part of the trigger passed as the second argument. 1259 */ 1260 static void renameWalkTrigger(Walker *pWalker, Trigger *pTrigger){ 1261 TriggerStep *pStep; 1262 1263 /* Find tokens to edit in WHEN clause */ 1264 sqlite3WalkExpr(pWalker, pTrigger->pWhen); 1265 1266 /* Find tokens to edit in trigger steps */ 1267 for(pStep=pTrigger->step_list; pStep; pStep=pStep->pNext){ 1268 sqlite3WalkSelect(pWalker, pStep->pSelect); 1269 sqlite3WalkExpr(pWalker, pStep->pWhere); 1270 sqlite3WalkExprList(pWalker, pStep->pExprList); 1271 if( pStep->pUpsert ){ 1272 Upsert *pUpsert = pStep->pUpsert; 1273 sqlite3WalkExprList(pWalker, pUpsert->pUpsertTarget); 1274 sqlite3WalkExprList(pWalker, pUpsert->pUpsertSet); 1275 sqlite3WalkExpr(pWalker, pUpsert->pUpsertWhere); 1276 sqlite3WalkExpr(pWalker, pUpsert->pUpsertTargetWhere); 1277 } 1278 if( pStep->pFrom ){ 1279 int i; 1280 for(i=0; i<pStep->pFrom->nSrc; i++){ 1281 sqlite3WalkSelect(pWalker, pStep->pFrom->a[i].pSelect); 1282 } 1283 } 1284 } 1285 } 1286 1287 /* 1288 ** Free the contents of Parse object (*pParse). Do not free the memory 1289 ** occupied by the Parse object itself. 1290 */ 1291 static void renameParseCleanup(Parse *pParse){ 1292 sqlite3 *db = pParse->db; 1293 Index *pIdx; 1294 if( pParse->pVdbe ){ 1295 sqlite3VdbeFinalize(pParse->pVdbe); 1296 } 1297 sqlite3DeleteTable(db, pParse->pNewTable); 1298 while( (pIdx = pParse->pNewIndex)!=0 ){ 1299 pParse->pNewIndex = pIdx->pNext; 1300 sqlite3FreeIndex(db, pIdx); 1301 } 1302 sqlite3DeleteTrigger(db, pParse->pNewTrigger); 1303 sqlite3DbFree(db, pParse->zErrMsg); 1304 renameTokenFree(db, pParse->pRename); 1305 sqlite3ParserReset(pParse); 1306 } 1307 1308 /* 1309 ** SQL function: 1310 ** 1311 ** sqlite_rename_column(zSql, iCol, bQuote, zNew, zTable, zOld) 1312 ** 1313 ** 0. zSql: SQL statement to rewrite 1314 ** 1. type: Type of object ("table", "view" etc.) 1315 ** 2. object: Name of object 1316 ** 3. Database: Database name (e.g. "main") 1317 ** 4. Table: Table name 1318 ** 5. iCol: Index of column to rename 1319 ** 6. zNew: New column name 1320 ** 7. bQuote: Non-zero if the new column name should be quoted. 1321 ** 8. bTemp: True if zSql comes from temp schema 1322 ** 1323 ** Do a column rename operation on the CREATE statement given in zSql. 1324 ** The iCol-th column (left-most is 0) of table zTable is renamed from zCol 1325 ** into zNew. The name should be quoted if bQuote is true. 1326 ** 1327 ** This function is used internally by the ALTER TABLE RENAME COLUMN command. 1328 ** It is only accessible to SQL created using sqlite3NestedParse(). It is 1329 ** not reachable from ordinary SQL passed into sqlite3_prepare(). 1330 */ 1331 static void renameColumnFunc( 1332 sqlite3_context *context, 1333 int NotUsed, 1334 sqlite3_value **argv 1335 ){ 1336 sqlite3 *db = sqlite3_context_db_handle(context); 1337 RenameCtx sCtx; 1338 const char *zSql = (const char*)sqlite3_value_text(argv[0]); 1339 const char *zDb = (const char*)sqlite3_value_text(argv[3]); 1340 const char *zTable = (const char*)sqlite3_value_text(argv[4]); 1341 int iCol = sqlite3_value_int(argv[5]); 1342 const char *zNew = (const char*)sqlite3_value_text(argv[6]); 1343 int bQuote = sqlite3_value_int(argv[7]); 1344 int bTemp = sqlite3_value_int(argv[8]); 1345 const char *zOld; 1346 int rc; 1347 Parse sParse; 1348 Walker sWalker; 1349 Index *pIdx; 1350 int i; 1351 Table *pTab; 1352 #ifndef SQLITE_OMIT_AUTHORIZATION 1353 sqlite3_xauth xAuth = db->xAuth; 1354 #endif 1355 1356 UNUSED_PARAMETER(NotUsed); 1357 if( zSql==0 ) return; 1358 if( zTable==0 ) return; 1359 if( zNew==0 ) return; 1360 if( iCol<0 ) return; 1361 sqlite3BtreeEnterAll(db); 1362 pTab = sqlite3FindTable(db, zTable, zDb); 1363 if( pTab==0 || iCol>=pTab->nCol ){ 1364 sqlite3BtreeLeaveAll(db); 1365 return; 1366 } 1367 zOld = pTab->aCol[iCol].zName; 1368 memset(&sCtx, 0, sizeof(sCtx)); 1369 sCtx.iCol = ((iCol==pTab->iPKey) ? -1 : iCol); 1370 1371 #ifndef SQLITE_OMIT_AUTHORIZATION 1372 db->xAuth = 0; 1373 #endif 1374 rc = renameParseSql(&sParse, zDb, db, zSql, bTemp); 1375 1376 /* Find tokens that need to be replaced. */ 1377 memset(&sWalker, 0, sizeof(Walker)); 1378 sWalker.pParse = &sParse; 1379 sWalker.xExprCallback = renameColumnExprCb; 1380 sWalker.xSelectCallback = renameColumnSelectCb; 1381 sWalker.u.pRename = &sCtx; 1382 1383 sCtx.pTab = pTab; 1384 if( rc!=SQLITE_OK ) goto renameColumnFunc_done; 1385 if( sParse.pNewTable ){ 1386 Select *pSelect = sParse.pNewTable->pSelect; 1387 if( pSelect ){ 1388 pSelect->selFlags &= ~SF_View; 1389 sParse.rc = SQLITE_OK; 1390 sqlite3SelectPrep(&sParse, pSelect, 0); 1391 rc = (db->mallocFailed ? SQLITE_NOMEM : sParse.rc); 1392 if( rc==SQLITE_OK ){ 1393 sqlite3WalkSelect(&sWalker, pSelect); 1394 } 1395 if( rc!=SQLITE_OK ) goto renameColumnFunc_done; 1396 }else{ 1397 /* A regular table */ 1398 int bFKOnly = sqlite3_stricmp(zTable, sParse.pNewTable->zName); 1399 FKey *pFKey; 1400 assert( sParse.pNewTable->pSelect==0 ); 1401 sCtx.pTab = sParse.pNewTable; 1402 if( bFKOnly==0 ){ 1403 renameTokenFind( 1404 &sParse, &sCtx, (void*)sParse.pNewTable->aCol[iCol].zName 1405 ); 1406 if( sCtx.iCol<0 ){ 1407 renameTokenFind(&sParse, &sCtx, (void*)&sParse.pNewTable->iPKey); 1408 } 1409 sqlite3WalkExprList(&sWalker, sParse.pNewTable->pCheck); 1410 for(pIdx=sParse.pNewTable->pIndex; pIdx; pIdx=pIdx->pNext){ 1411 sqlite3WalkExprList(&sWalker, pIdx->aColExpr); 1412 } 1413 for(pIdx=sParse.pNewIndex; pIdx; pIdx=pIdx->pNext){ 1414 sqlite3WalkExprList(&sWalker, pIdx->aColExpr); 1415 } 1416 } 1417 #ifndef SQLITE_OMIT_GENERATED_COLUMNS 1418 for(i=0; i<sParse.pNewTable->nCol; i++){ 1419 sqlite3WalkExpr(&sWalker, sParse.pNewTable->aCol[i].pDflt); 1420 } 1421 #endif 1422 1423 for(pFKey=sParse.pNewTable->pFKey; pFKey; pFKey=pFKey->pNextFrom){ 1424 for(i=0; i<pFKey->nCol; i++){ 1425 if( bFKOnly==0 && pFKey->aCol[i].iFrom==iCol ){ 1426 renameTokenFind(&sParse, &sCtx, (void*)&pFKey->aCol[i]); 1427 } 1428 if( 0==sqlite3_stricmp(pFKey->zTo, zTable) 1429 && 0==sqlite3_stricmp(pFKey->aCol[i].zCol, zOld) 1430 ){ 1431 renameTokenFind(&sParse, &sCtx, (void*)pFKey->aCol[i].zCol); 1432 } 1433 } 1434 } 1435 } 1436 }else if( sParse.pNewIndex ){ 1437 sqlite3WalkExprList(&sWalker, sParse.pNewIndex->aColExpr); 1438 sqlite3WalkExpr(&sWalker, sParse.pNewIndex->pPartIdxWhere); 1439 }else{ 1440 /* A trigger */ 1441 TriggerStep *pStep; 1442 rc = renameResolveTrigger(&sParse); 1443 if( rc!=SQLITE_OK ) goto renameColumnFunc_done; 1444 1445 for(pStep=sParse.pNewTrigger->step_list; pStep; pStep=pStep->pNext){ 1446 if( pStep->zTarget ){ 1447 Table *pTarget = sqlite3LocateTable(&sParse, 0, pStep->zTarget, zDb); 1448 if( pTarget==pTab ){ 1449 if( pStep->pUpsert ){ 1450 ExprList *pUpsertSet = pStep->pUpsert->pUpsertSet; 1451 renameColumnElistNames(&sParse, &sCtx, pUpsertSet, zOld); 1452 } 1453 renameColumnIdlistNames(&sParse, &sCtx, pStep->pIdList, zOld); 1454 renameColumnElistNames(&sParse, &sCtx, pStep->pExprList, zOld); 1455 } 1456 } 1457 } 1458 1459 1460 /* Find tokens to edit in UPDATE OF clause */ 1461 if( sParse.pTriggerTab==pTab ){ 1462 renameColumnIdlistNames(&sParse, &sCtx,sParse.pNewTrigger->pColumns,zOld); 1463 } 1464 1465 /* Find tokens to edit in various expressions and selects */ 1466 renameWalkTrigger(&sWalker, sParse.pNewTrigger); 1467 } 1468 1469 assert( rc==SQLITE_OK ); 1470 rc = renameEditSql(context, &sCtx, zSql, zNew, bQuote); 1471 1472 renameColumnFunc_done: 1473 if( rc!=SQLITE_OK ){ 1474 if( sParse.zErrMsg ){ 1475 renameColumnParseError(context, 0, argv[1], argv[2], &sParse); 1476 }else{ 1477 sqlite3_result_error_code(context, rc); 1478 } 1479 } 1480 1481 renameParseCleanup(&sParse); 1482 renameTokenFree(db, sCtx.pList); 1483 #ifndef SQLITE_OMIT_AUTHORIZATION 1484 db->xAuth = xAuth; 1485 #endif 1486 sqlite3BtreeLeaveAll(db); 1487 } 1488 1489 /* 1490 ** Walker expression callback used by "RENAME TABLE". 1491 */ 1492 static int renameTableExprCb(Walker *pWalker, Expr *pExpr){ 1493 RenameCtx *p = pWalker->u.pRename; 1494 if( pExpr->op==TK_COLUMN && p->pTab==pExpr->y.pTab ){ 1495 renameTokenFind(pWalker->pParse, p, (void*)&pExpr->y.pTab); 1496 } 1497 return WRC_Continue; 1498 } 1499 1500 /* 1501 ** Walker select callback used by "RENAME TABLE". 1502 */ 1503 static int renameTableSelectCb(Walker *pWalker, Select *pSelect){ 1504 int i; 1505 RenameCtx *p = pWalker->u.pRename; 1506 SrcList *pSrc = pSelect->pSrc; 1507 if( pSelect->selFlags & SF_View ) return WRC_Prune; 1508 if( pSrc==0 ){ 1509 assert( pWalker->pParse->db->mallocFailed ); 1510 return WRC_Abort; 1511 } 1512 for(i=0; i<pSrc->nSrc; i++){ 1513 struct SrcList_item *pItem = &pSrc->a[i]; 1514 if( pItem->pTab==p->pTab ){ 1515 renameTokenFind(pWalker->pParse, p, pItem->zName); 1516 } 1517 } 1518 renameWalkWith(pWalker, pSelect); 1519 1520 return WRC_Continue; 1521 } 1522 1523 1524 /* 1525 ** This C function implements an SQL user function that is used by SQL code 1526 ** generated by the ALTER TABLE ... RENAME command to modify the definition 1527 ** of any foreign key constraints that use the table being renamed as the 1528 ** parent table. It is passed three arguments: 1529 ** 1530 ** 0: The database containing the table being renamed. 1531 ** 1. type: Type of object ("table", "view" etc.) 1532 ** 2. object: Name of object 1533 ** 3: The complete text of the schema statement being modified, 1534 ** 4: The old name of the table being renamed, and 1535 ** 5: The new name of the table being renamed. 1536 ** 6: True if the schema statement comes from the temp db. 1537 ** 1538 ** It returns the new schema statement. For example: 1539 ** 1540 ** sqlite_rename_table('main', 'CREATE TABLE t1(a REFERENCES t2)','t2','t3',0) 1541 ** -> 'CREATE TABLE t1(a REFERENCES t3)' 1542 */ 1543 static void renameTableFunc( 1544 sqlite3_context *context, 1545 int NotUsed, 1546 sqlite3_value **argv 1547 ){ 1548 sqlite3 *db = sqlite3_context_db_handle(context); 1549 const char *zDb = (const char*)sqlite3_value_text(argv[0]); 1550 const char *zInput = (const char*)sqlite3_value_text(argv[3]); 1551 const char *zOld = (const char*)sqlite3_value_text(argv[4]); 1552 const char *zNew = (const char*)sqlite3_value_text(argv[5]); 1553 int bTemp = sqlite3_value_int(argv[6]); 1554 UNUSED_PARAMETER(NotUsed); 1555 1556 if( zInput && zOld && zNew ){ 1557 Parse sParse; 1558 int rc; 1559 int bQuote = 1; 1560 RenameCtx sCtx; 1561 Walker sWalker; 1562 1563 #ifndef SQLITE_OMIT_AUTHORIZATION 1564 sqlite3_xauth xAuth = db->xAuth; 1565 db->xAuth = 0; 1566 #endif 1567 1568 sqlite3BtreeEnterAll(db); 1569 1570 memset(&sCtx, 0, sizeof(RenameCtx)); 1571 sCtx.pTab = sqlite3FindTable(db, zOld, zDb); 1572 memset(&sWalker, 0, sizeof(Walker)); 1573 sWalker.pParse = &sParse; 1574 sWalker.xExprCallback = renameTableExprCb; 1575 sWalker.xSelectCallback = renameTableSelectCb; 1576 sWalker.u.pRename = &sCtx; 1577 1578 rc = renameParseSql(&sParse, zDb, db, zInput, bTemp); 1579 1580 if( rc==SQLITE_OK ){ 1581 int isLegacy = (db->flags & SQLITE_LegacyAlter); 1582 if( sParse.pNewTable ){ 1583 Table *pTab = sParse.pNewTable; 1584 1585 if( pTab->pSelect ){ 1586 if( isLegacy==0 ){ 1587 Select *pSelect = pTab->pSelect; 1588 NameContext sNC; 1589 memset(&sNC, 0, sizeof(sNC)); 1590 sNC.pParse = &sParse; 1591 1592 assert( pSelect->selFlags & SF_View ); 1593 pSelect->selFlags &= ~SF_View; 1594 sqlite3SelectPrep(&sParse, pTab->pSelect, &sNC); 1595 if( sParse.nErr ){ 1596 rc = sParse.rc; 1597 }else{ 1598 sqlite3WalkSelect(&sWalker, pTab->pSelect); 1599 } 1600 } 1601 }else{ 1602 /* Modify any FK definitions to point to the new table. */ 1603 #ifndef SQLITE_OMIT_FOREIGN_KEY 1604 if( isLegacy==0 || (db->flags & SQLITE_ForeignKeys) ){ 1605 FKey *pFKey; 1606 for(pFKey=pTab->pFKey; pFKey; pFKey=pFKey->pNextFrom){ 1607 if( sqlite3_stricmp(pFKey->zTo, zOld)==0 ){ 1608 renameTokenFind(&sParse, &sCtx, (void*)pFKey->zTo); 1609 } 1610 } 1611 } 1612 #endif 1613 1614 /* If this is the table being altered, fix any table refs in CHECK 1615 ** expressions. Also update the name that appears right after the 1616 ** "CREATE [VIRTUAL] TABLE" bit. */ 1617 if( sqlite3_stricmp(zOld, pTab->zName)==0 ){ 1618 sCtx.pTab = pTab; 1619 if( isLegacy==0 ){ 1620 sqlite3WalkExprList(&sWalker, pTab->pCheck); 1621 } 1622 renameTokenFind(&sParse, &sCtx, pTab->zName); 1623 } 1624 } 1625 } 1626 1627 else if( sParse.pNewIndex ){ 1628 renameTokenFind(&sParse, &sCtx, sParse.pNewIndex->zName); 1629 if( isLegacy==0 ){ 1630 sqlite3WalkExpr(&sWalker, sParse.pNewIndex->pPartIdxWhere); 1631 } 1632 } 1633 1634 #ifndef SQLITE_OMIT_TRIGGER 1635 else{ 1636 Trigger *pTrigger = sParse.pNewTrigger; 1637 TriggerStep *pStep; 1638 if( 0==sqlite3_stricmp(sParse.pNewTrigger->table, zOld) 1639 && sCtx.pTab->pSchema==pTrigger->pTabSchema 1640 ){ 1641 renameTokenFind(&sParse, &sCtx, sParse.pNewTrigger->table); 1642 } 1643 1644 if( isLegacy==0 ){ 1645 rc = renameResolveTrigger(&sParse); 1646 if( rc==SQLITE_OK ){ 1647 renameWalkTrigger(&sWalker, pTrigger); 1648 for(pStep=pTrigger->step_list; pStep; pStep=pStep->pNext){ 1649 if( pStep->zTarget && 0==sqlite3_stricmp(pStep->zTarget, zOld) ){ 1650 renameTokenFind(&sParse, &sCtx, pStep->zTarget); 1651 } 1652 } 1653 } 1654 } 1655 } 1656 #endif 1657 } 1658 1659 if( rc==SQLITE_OK ){ 1660 rc = renameEditSql(context, &sCtx, zInput, zNew, bQuote); 1661 } 1662 if( rc!=SQLITE_OK ){ 1663 if( sParse.zErrMsg ){ 1664 renameColumnParseError(context, 0, argv[1], argv[2], &sParse); 1665 }else{ 1666 sqlite3_result_error_code(context, rc); 1667 } 1668 } 1669 1670 renameParseCleanup(&sParse); 1671 renameTokenFree(db, sCtx.pList); 1672 sqlite3BtreeLeaveAll(db); 1673 #ifndef SQLITE_OMIT_AUTHORIZATION 1674 db->xAuth = xAuth; 1675 #endif 1676 } 1677 1678 return; 1679 } 1680 1681 /* 1682 ** An SQL user function that checks that there are no parse or symbol 1683 ** resolution problems in a CREATE TRIGGER|TABLE|VIEW|INDEX statement. 1684 ** After an ALTER TABLE .. RENAME operation is performed and the schema 1685 ** reloaded, this function is called on each SQL statement in the schema 1686 ** to ensure that it is still usable. 1687 ** 1688 ** 0: Database name ("main", "temp" etc.). 1689 ** 1: SQL statement. 1690 ** 2: Object type ("view", "table", "trigger" or "index"). 1691 ** 3: Object name. 1692 ** 4: True if object is from temp schema. 1693 ** 1694 ** Unless it finds an error, this function normally returns NULL. However, it 1695 ** returns integer value 1 if: 1696 ** 1697 ** * the SQL argument creates a trigger, and 1698 ** * the table that the trigger is attached to is in database zDb. 1699 */ 1700 static void renameTableTest( 1701 sqlite3_context *context, 1702 int NotUsed, 1703 sqlite3_value **argv 1704 ){ 1705 sqlite3 *db = sqlite3_context_db_handle(context); 1706 char const *zDb = (const char*)sqlite3_value_text(argv[0]); 1707 char const *zInput = (const char*)sqlite3_value_text(argv[1]); 1708 int bTemp = sqlite3_value_int(argv[4]); 1709 int isLegacy = (db->flags & SQLITE_LegacyAlter); 1710 1711 #ifndef SQLITE_OMIT_AUTHORIZATION 1712 sqlite3_xauth xAuth = db->xAuth; 1713 db->xAuth = 0; 1714 #endif 1715 1716 UNUSED_PARAMETER(NotUsed); 1717 if( zDb && zInput ){ 1718 int rc; 1719 Parse sParse; 1720 rc = renameParseSql(&sParse, zDb, db, zInput, bTemp); 1721 if( rc==SQLITE_OK ){ 1722 if( isLegacy==0 && sParse.pNewTable && sParse.pNewTable->pSelect ){ 1723 NameContext sNC; 1724 memset(&sNC, 0, sizeof(sNC)); 1725 sNC.pParse = &sParse; 1726 sqlite3SelectPrep(&sParse, sParse.pNewTable->pSelect, &sNC); 1727 if( sParse.nErr ) rc = sParse.rc; 1728 } 1729 1730 else if( sParse.pNewTrigger ){ 1731 if( isLegacy==0 ){ 1732 rc = renameResolveTrigger(&sParse); 1733 } 1734 if( rc==SQLITE_OK ){ 1735 int i1 = sqlite3SchemaToIndex(db, sParse.pNewTrigger->pTabSchema); 1736 int i2 = sqlite3FindDbName(db, zDb); 1737 if( i1==i2 ) sqlite3_result_int(context, 1); 1738 } 1739 } 1740 } 1741 1742 if( rc!=SQLITE_OK ){ 1743 renameColumnParseError(context, 1, argv[2], argv[3], &sParse); 1744 } 1745 renameParseCleanup(&sParse); 1746 } 1747 1748 #ifndef SQLITE_OMIT_AUTHORIZATION 1749 db->xAuth = xAuth; 1750 #endif 1751 } 1752 1753 /* 1754 ** Register built-in functions used to help implement ALTER TABLE 1755 */ 1756 void sqlite3AlterFunctions(void){ 1757 static FuncDef aAlterTableFuncs[] = { 1758 INTERNAL_FUNCTION(sqlite_rename_column, 9, renameColumnFunc), 1759 INTERNAL_FUNCTION(sqlite_rename_table, 7, renameTableFunc), 1760 INTERNAL_FUNCTION(sqlite_rename_test, 5, renameTableTest), 1761 }; 1762 sqlite3InsertBuiltinFuncs(aAlterTableFuncs, ArraySize(aAlterTableFuncs)); 1763 } 1764 #endif /* SQLITE_ALTER_TABLE */ 1765