Index: src/alter.c ================================================================== --- src/alter.c +++ src/alter.c @@ -939,29 +939,29 @@ sqlite3DbFree(db, p); } } /* -** Return the RenameToken object associated with parse tree element pPtr, -** or a NULL pointer if not found. The RenameToken object returned is -** removed from the list of RenameToken objects attached to the Parse -** object and the caller becomes the new owner of the RenameToken object -** Hence, the caller assumes responsibility for freeing the returned -** RenameToken object. +** Search the Parse object passed as the first argument for a RenameToken +** object associated with parse tree element pPtr. If found, remove it +** from the Parse object and add it to the list maintained by the +** RenameCtx object passed as the second argument. */ -static RenameToken *renameTokenFind(Parse *pParse, void *pPtr){ +static void renameTokenFind(Parse *pParse, struct RenameCtx *pCtx, void *pPtr){ RenameToken **pp; for(pp=&pParse->pRename; (*pp); pp=&(*pp)->pNext){ if( (*pp)->p==pPtr ){ RenameToken *pToken = *pp; *pp = pToken->pNext; - pToken->pNext = 0; - return pToken; + pToken->pNext = pCtx->pList; + pCtx->pList = pToken; + pCtx->nList++; + break; } } - return 0; } + /* ** This is a Walker expression callback. ** ** For every TK_COLUMN node in the expression tree, search to see @@ -971,16 +971,11 @@ ** constructed in RenameCtx object at pWalker->u.pRename. */ static int renameColumnExprCb(Walker *pWalker, Expr *pExpr){ struct RenameCtx *p = pWalker->u.pRename; if( pExpr->op==TK_COLUMN && pExpr->iColumn==p->iCol ){ - RenameToken *pTok = renameTokenFind(pWalker->pParse, (void*)pExpr); - if( pTok ){ - pTok->pNext = p->pList; - p->pList = pTok; - p->nList++; - } + renameTokenFind(pWalker->pParse, p, (void*)pExpr); } return WRC_Continue; } /* @@ -1113,16 +1108,16 @@ if( sParse.pNewTable ){ int bFKOnly = sqlite3_stricmp(zTable, sParse.pNewTable->zName); FKey *pFKey; if( bFKOnly==0 ){ - sCtx.pList = renameTokenFind( - &sParse, (void*)sParse.pNewTable->aCol[sCtx.iCol].zName + renameTokenFind( + &sParse, &sCtx, (void*)sParse.pNewTable->aCol[sCtx.iCol].zName ); - sCtx.nList = 1; 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); @@ -1131,24 +1126,16 @@ for(pFKey=sParse.pNewTable->pFKey; pFKey; pFKey=pFKey->pNextFrom){ for(i=0; inCol; i++){ RenameToken *pTok = 0; if( bFKOnly==0 && pFKey->aCol[i].iFrom==sCtx.iCol ){ - pTok = renameTokenFind(&sParse, (void*)&pFKey->aCol[i]); - if( pTok ){ - pTok->pNext = sCtx.pList; - sCtx.pList = pTok; - sCtx.nList++; - } + renameTokenFind(&sParse, &sCtx, (void*)&pFKey->aCol[i]); } if( 0==sqlite3_stricmp(pFKey->zTo, zTable) && 0==sqlite3_stricmp(pFKey->aCol[i].zCol, zOld) ){ - pTok = renameTokenFind(&sParse, (void*)pFKey->aCol[i].zCol); - pTok->pNext = sCtx.pList; - sCtx.pList = pTok; - sCtx.nList++; + renameTokenFind(&sParse, &sCtx, (void*)pFKey->aCol[i].zCol); } } } }else{ sqlite3WalkExprList(&sWalker, sParse.pNewIndex->aColExpr); Index: src/build.c ================================================================== --- src/build.c +++ src/build.c @@ -1368,10 +1368,13 @@ if( nTerm==1 && pCol && sqlite3StrICmp(sqlite3ColumnType(pCol,""), "INTEGER")==0 && sortOrder!=SQLITE_SO_DESC ){ + if( IN_RENAME_COLUMN && pList ){ + sqlite3MoveRenameToken(pParse, &pTab->iPKey, pList->a[0].pExpr); + } pTab->iPKey = iCol; pTab->keyConf = (u8)onError; assert( autoInc==0 || autoInc==1 ); pTab->tabFlags |= autoInc*TF_Autoincrement; if( pList ) pParse->iPkSortOrder = pList->a[0].sortOrder; Index: test/altercol.test ================================================================== --- test/altercol.test +++ test/altercol.test @@ -63,10 +63,16 @@ 12 {CREATE TABLE t1(a, b, c); CREATE INDEX t1i ON t1(b+b+b+b, c) WHERE b>0} {{CREATE TABLE t1(a, d, c)} {CREATE INDEX t1i ON t1(d+d+d+d, c) WHERE d>0}} 13 {CREATE TABLE t1(a, b, c, FOREIGN KEY (b) REFERENCES t2)} {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