Skip to content

Commit

Permalink
Add a PRAGMA that restores the legacy ALTER TABLE RENAME TO behaviour.
Browse files Browse the repository at this point in the history
  • Loading branch information
danielk-1977 committed Sep 20, 2018
1 parent 4c98ab8 commit b072396
Show file tree
Hide file tree
Showing 5 changed files with 511 additions and 18 deletions.
46 changes: 29 additions & 17 deletions src/alter.c
Original file line number Diff line number Diff line change
Expand Up @@ -1422,17 +1422,20 @@ static void renameTableFunc(
rc = renameParseSql(&sParse, zDb, 1, db, zInput, bTemp);

if( rc==SQLITE_OK ){
int isLegacy = (db->flags & SQLITE_LegacyAlter);
if( sParse.pNewTable ){
Table *pTab = sParse.pNewTable;

if( pTab->pSelect ){
NameContext sNC;
memset(&sNC, 0, sizeof(sNC));
sNC.pParse = &sParse;

sqlite3SelectPrep(&sParse, pTab->pSelect, &sNC);
if( sParse.nErr ) rc = sParse.rc;
sqlite3WalkSelect(&sWalker, pTab->pSelect);
if( isLegacy==0 ){
NameContext sNC;
memset(&sNC, 0, sizeof(sNC));
sNC.pParse = &sParse;

sqlite3SelectPrep(&sParse, pTab->pSelect, &sNC);
if( sParse.nErr ) rc = sParse.rc;
sqlite3WalkSelect(&sWalker, pTab->pSelect);
}
}else{
/* Modify any FK definitions to point to the new table. */
#ifndef SQLITE_OMIT_FOREIGN_KEY
Expand All @@ -1451,15 +1454,19 @@ static void renameTableFunc(
** "CREATE [VIRTUAL] TABLE" bit. */
if( sqlite3_stricmp(zOld, pTab->zName)==0 ){
sCtx.pTab = pTab;
sqlite3WalkExprList(&sWalker, pTab->pCheck);
if( isLegacy==0 ){
sqlite3WalkExprList(&sWalker, pTab->pCheck);
}
renameTokenFind(&sParse, &sCtx, pTab->zName);
}
}
}

else if( sParse.pNewIndex ){
renameTokenFind(&sParse, &sCtx, sParse.pNewIndex->zName);
sqlite3WalkExpr(&sWalker, sParse.pNewIndex->pPartIdxWhere);
if( isLegacy==0 ){
sqlite3WalkExpr(&sWalker, sParse.pNewIndex->pPartIdxWhere);
}
}

#ifndef SQLITE_OMIT_TRIGGER
Expand All @@ -1472,12 +1479,14 @@ static void renameTableFunc(
renameTokenFind(&sParse, &sCtx, sParse.pNewTrigger->table);
}

rc = renameResolveTrigger(&sParse, bTemp ? 0 : zDb);
if( rc==SQLITE_OK ){
renameWalkTrigger(&sWalker, pTrigger);
for(pStep=pTrigger->step_list; pStep; pStep=pStep->pNext){
if( pStep->zTarget && 0==sqlite3_stricmp(pStep->zTarget, zOld) ){
renameTokenFind(&sParse, &sCtx, pStep->zTarget);
if( isLegacy==0 ){
rc = renameResolveTrigger(&sParse, bTemp ? 0 : zDb);
if( rc==SQLITE_OK ){
renameWalkTrigger(&sWalker, pTrigger);
for(pStep=pTrigger->step_list; pStep; pStep=pStep->pNext){
if( pStep->zTarget && 0==sqlite3_stricmp(pStep->zTarget, zOld) ){
renameTokenFind(&sParse, &sCtx, pStep->zTarget);
}
}
}
}
Expand Down Expand Up @@ -1535,6 +1544,7 @@ static void renameTableTest(
char const *zDb = (const char*)sqlite3_value_text(argv[0]);
char const *zInput = (const char*)sqlite3_value_text(argv[1]);
int bTemp = sqlite3_value_int(argv[4]);
int isLegacy = (db->flags & SQLITE_LegacyAlter);

#ifndef SQLITE_OMIT_AUTHORIZATION
sqlite3_xauth xAuth = db->xAuth;
Expand All @@ -1547,7 +1557,7 @@ static void renameTableTest(
Parse sParse;
rc = renameParseSql(&sParse, zDb, 1, db, zInput, bTemp);
if( rc==SQLITE_OK ){
if( sParse.pNewTable && sParse.pNewTable->pSelect ){
if( isLegacy==0 && sParse.pNewTable && sParse.pNewTable->pSelect ){
NameContext sNC;
memset(&sNC, 0, sizeof(sNC));
sNC.pParse = &sParse;
Expand All @@ -1556,7 +1566,9 @@ static void renameTableTest(
}

else if( sParse.pNewTrigger ){
rc = renameResolveTrigger(&sParse, bTemp ? 0 : zDb);
if( isLegacy==0 ){
rc = renameResolveTrigger(&sParse, bTemp ? 0 : zDb);
}
if( rc==SQLITE_OK ){
int i1 = sqlite3SchemaToIndex(db, sParse.pNewTrigger->pTabSchema);
int i2 = sqlite3FindDbName(db, zDb);
Expand Down
7 changes: 6 additions & 1 deletion src/pragma.h
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,11 @@ static const PragmaName aPragmaName[] = {
/* iArg: */ 0 },
#endif
#if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
{/* zName: */ "legacy_alter_table",
/* ePragTyp: */ PragTyp_FLAG,
/* ePragFlg: */ PragFlg_Result0|PragFlg_NoColumns1,
/* ColNames: */ 0, 0,
/* iArg: */ SQLITE_LegacyAlter },
{/* zName: */ "legacy_file_format",
/* ePragTyp: */ PragTyp_FLAG,
/* ePragFlg: */ PragFlg_Result0|PragFlg_NoColumns1,
Expand Down Expand Up @@ -646,4 +651,4 @@ static const PragmaName aPragmaName[] = {
/* iArg: */ SQLITE_WriteSchema },
#endif
};
/* Number of pragmas: 60 on by default, 77 total. */
/* Number of pragmas: 61 on by default, 78 total. */
1 change: 1 addition & 0 deletions src/sqliteInt.h
Original file line number Diff line number Diff line change
Expand Up @@ -1536,6 +1536,7 @@ struct sqlite3 {
#define SQLITE_EnableQPSG 0x00800000 /* Query Planner Stability Guarantee*/
#define SQLITE_TriggerEQP 0x01000000 /* Show trigger EXPLAIN QUERY PLAN */
#define SQLITE_ResetDatabase 0x02000000 /* Reset the database */
#define SQLITE_LegacyAlter 0x04000000 /* Legacy ALTER TABLE behaviour */

/* Flags used only if debugging */
#ifdef SQLITE_DEBUG
Expand Down
Loading

0 comments on commit b072396

Please sign in to comment.