SQLite

Check-in [ad15486022]
Login

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Fix a problem when renaming an IPK column that is also part of a child key.
Downloads: Tarball | ZIP archive
Timelines: family | ancestors | descendants | both | alter-table-rename-column
Files: files | file ages | folders
SHA3-256: ad15486022209205c65fb5ffdbe30a7b99379170451e6aff4bab6e90b549d6c7
User & Date: dan 2018-08-14 21:03:38.735
Context
2018-08-18
18:01
Have ALTER TABLE RENAME COLUMN also edit trigger and view definitions. (check-in: 7908e8a4a3 user: dan tags: alter-table-rename-column)
2018-08-14
21:05
Fix a problem when renaming an IPK column that is also part of a child key. (check-in: 6e6a2bfdbb user: dan tags: edit-trigger-wrapper)
21:03
Fix a problem when renaming an IPK column that is also part of a child key. (check-in: ad15486022 user: dan tags: alter-table-rename-column)
20:38
Do not allow ALTER TABLE RENAME COLUMN on a virtual table. (check-in: f6d6b47271 user: drh tags: alter-table-rename-column)
Changes
Unified Diff Show Whitespace Changes Patch
Changes to src/alter.c.
1112
1113
1114
1115
1116
1117
1118












1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
  sWalker.pParse = &sParse;
  sWalker.xExprCallback = renameColumnExprCb;
  sWalker.u.pRename = &sCtx;

  if( sParse.pNewTable ){
    int bFKOnly = sqlite3_stricmp(zTable, sParse.pNewTable->zName);
    FKey *pFKey;












    if( bFKOnly==0 ){
      renameTokenFind(
          &sParse, &sCtx, (void*)sParse.pNewTable->aCol[sCtx.iCol].zName
      );
      assert( sCtx.iCol>=0 );
      if( sParse.pNewTable->iPKey==sCtx.iCol ){
        renameTokenFind(&sParse, &sCtx, (void*)&sParse.pNewTable->iPKey);
        sCtx.iCol = -1;
      }
      sqlite3WalkExprList(&sWalker, sParse.pNewTable->pCheck);
      for(pIdx=sParse.pNewTable->pIndex; pIdx; pIdx=pIdx->pNext){
        sqlite3WalkExprList(&sWalker, pIdx->aColExpr);
      }
    }

    for(pFKey=sParse.pNewTable->pFKey; pFKey; pFKey=pFKey->pNextFrom){
      for(i=0; i<pFKey->nCol; i++){
        if( bFKOnly==0 && pFKey->aCol[i].iFrom==sCtx.iCol ){
          renameTokenFind(&sParse, &sCtx, (void*)&pFKey->aCol[i]);
        }
        if( 0==sqlite3_stricmp(pFKey->zTo, zTable)
         && 0==sqlite3_stricmp(pFKey->aCol[i].zCol, zOld)
        ){
          renameTokenFind(&sParse, &sCtx, (void*)pFKey->aCol[i].zCol);
        }
      }
    }
  }else{
    sqlite3WalkExprList(&sWalker, sParse.pNewIndex->aColExpr);
    sqlite3WalkExpr(&sWalker, sParse.pNewIndex->pPartIdxWhere);
  }

  assert( nQuot>=nNew );
  zOut = sqlite3DbMallocZero(db, nSql + sCtx.nList*nQuot + 1);







>
>
>
>
>
>
>
>
>
>
>
>














<
<
<
<
<
<
<
<
<
<
<
<
<







1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144













1145
1146
1147
1148
1149
1150
1151
  sWalker.pParse = &sParse;
  sWalker.xExprCallback = renameColumnExprCb;
  sWalker.u.pRename = &sCtx;

  if( sParse.pNewTable ){
    int bFKOnly = sqlite3_stricmp(zTable, sParse.pNewTable->zName);
    FKey *pFKey;
    for(pFKey=sParse.pNewTable->pFKey; pFKey; pFKey=pFKey->pNextFrom){
      for(i=0; i<pFKey->nCol; i++){
        if( bFKOnly==0 && pFKey->aCol[i].iFrom==sCtx.iCol ){
          renameTokenFind(&sParse, &sCtx, (void*)&pFKey->aCol[i]);
        }
        if( 0==sqlite3_stricmp(pFKey->zTo, zTable)
         && 0==sqlite3_stricmp(pFKey->aCol[i].zCol, zOld)
        ){
          renameTokenFind(&sParse, &sCtx, (void*)pFKey->aCol[i].zCol);
        }
      }
    }
    if( bFKOnly==0 ){
      renameTokenFind(
          &sParse, &sCtx, (void*)sParse.pNewTable->aCol[sCtx.iCol].zName
      );
      assert( sCtx.iCol>=0 );
      if( sParse.pNewTable->iPKey==sCtx.iCol ){
        renameTokenFind(&sParse, &sCtx, (void*)&sParse.pNewTable->iPKey);
        sCtx.iCol = -1;
      }
      sqlite3WalkExprList(&sWalker, sParse.pNewTable->pCheck);
      for(pIdx=sParse.pNewTable->pIndex; pIdx; pIdx=pIdx->pNext){
        sqlite3WalkExprList(&sWalker, pIdx->aColExpr);
      }
    }













  }else{
    sqlite3WalkExprList(&sWalker, sParse.pNewIndex->aColExpr);
    sqlite3WalkExpr(&sWalker, sParse.pNewIndex->pPartIdxWhere);
  }

  assert( nQuot>=nNew );
  zOut = sqlite3DbMallocZero(db, nSql + sCtx.nList*nQuot + 1);
Changes to test/altercol.test.
67
68
69
70
71
72
73



74
75
76
77
78
79
80
    {CREATE TABLE t1(a, d, c, FOREIGN KEY (d) REFERENCES t2)}

 15 {CREATE TABLE t1(a INTEGER, b INTEGER, c BLOB, PRIMARY KEY(b))}
    {CREATE TABLE t1(a INTEGER, d INTEGER, c BLOB, PRIMARY KEY(d))}

 16 {CREATE TABLE t1(a INTEGER, b INTEGER PRIMARY KEY, c BLOB)}
    {CREATE TABLE t1(a INTEGER, d INTEGER PRIMARY KEY, c BLOB)}




} {
  reset_db
  do_execsql_test 1.$tn.0 $before

  do_execsql_test 1.$tn.1 {
    INSERT INTO t1 VALUES(1, 2, 3);







>
>
>







67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
    {CREATE TABLE t1(a, d, c, FOREIGN KEY (d) REFERENCES t2)}

 15 {CREATE TABLE t1(a INTEGER, b INTEGER, c BLOB, PRIMARY KEY(b))}
    {CREATE TABLE t1(a INTEGER, d INTEGER, c BLOB, PRIMARY KEY(d))}

 16 {CREATE TABLE t1(a INTEGER, b INTEGER PRIMARY KEY, c BLOB)}
    {CREATE TABLE t1(a INTEGER, d INTEGER PRIMARY KEY, c BLOB)}

 17  {CREATE TABLE t1(a INTEGER, b INTEGER PRIMARY KEY, c BLOB, FOREIGN KEY (b) REFERENCES t2)}
     {CREATE TABLE t1(a INTEGER, d INTEGER PRIMARY KEY, c BLOB, FOREIGN KEY (d) REFERENCES t2)}

} {
  reset_db
  do_execsql_test 1.$tn.0 $before

  do_execsql_test 1.$tn.1 {
    INSERT INTO t1 VALUES(1, 2, 3);