Many hyperlinks are disabled.
Use anonymous login
to enable hyperlinks.
Overview
Comment: | Improvements to error handling in ALTER TABLE RENAME COLUMN. |
---|---|
Downloads: | Tarball | ZIP archive |
Timelines: | family | ancestors | descendants | both | edit-trigger-wrapper |
Files: | files | file ages | folders |
SHA3-256: |
7fa1faeaff30b74b68ee6f4b363d837f |
User & Date: | dan 2018-08-18 17:35:38.195 |
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) | |
17:35 | Improvements to error handling in ALTER TABLE RENAME COLUMN. (Closed-Leaf check-in: 7fa1faeaff user: dan tags: edit-trigger-wrapper) | |
2018-08-17
| ||
18:08 | Allow an ALTER TABLE RENAME COLUMN to proceed even if the schema contains a virtual table for which the module is unavailable. (check-in: 7b72b2360a user: dan tags: edit-trigger-wrapper) | |
Changes
Changes to src/alter.c.
︙ | ︙ | |||
960 961 962 963 964 965 966 967 968 969 | pCtx->pList = pToken; pCtx->nList++; break; } } } static int renameColumnSelectCb(Walker *pWalker, Select *p){ return WRC_Continue; } | > > > > > < | 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 | pCtx->pList = pToken; pCtx->nList++; break; } } } /* ** This is a Walker select callback. It does nothing. It is only required ** because without a dummy callback, sqlite3WalkExpr() and similar do not ** descend into sub-select statements. */ static int renameColumnSelectCb(Walker *pWalker, Select *p){ return WRC_Continue; } /* ** This is a Walker expression callback. ** ** For every TK_COLUMN node in the expression tree, search to see ** if the column being references is the column being renamed by an ** ALTER TABLE statement. If it is, then attach its associated |
︙ | ︙ | |||
992 993 994 995 996 997 998 | renameTokenFind(pWalker->pParse, p, (void*)pExpr); } return WRC_Continue; } /* ** The RenameCtx contains a list of tokens that reference a column that | | | | > > > > > > > > > > > > > > > > > > > > > > > > > > > | 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 | renameTokenFind(pWalker->pParse, p, (void*)pExpr); } return WRC_Continue; } /* ** The RenameCtx contains a list of tokens that reference a column that ** is being renamed by an ALTER TABLE statement. Return the "last" ** RenameToken in the RenameCtx and remove that RenameToken from the ** RenameContext. "Last" means the last RenameToken encountered when ** the input SQL is parsed from left to right. Repeated calls to this routine ** return all column name tokens in the order that they are encountered ** in the SQL statement. */ static RenameToken *renameColumnTokenNext(RenameCtx *pCtx){ RenameToken *pBest = pCtx->pList; RenameToken *pToken; RenameToken **pp; for(pToken=pBest->pNext; pToken; pToken=pToken->pNext){ if( pToken->t.z>pBest->t.z ) pBest = pToken; } for(pp=&pCtx->pList; *pp!=pBest; pp=&(*pp)->pNext); *pp = pBest->pNext; return pBest; } /* ** An error occured while parsing or otherwise processing a database ** object (either pParse->pNewTable, pNewIndex or pNewTrigger) as part of an ** ALTER TABLE RENAME COLUMN program. The error message emitted by the ** sub-routine is currently stored in pParse->zErrMsg. This function ** adds context to the error message and then stores it in pCtx. */ static void renameColumnParseError(sqlite3_context *pCtx, Parse *pParse){ const char *zT; const char *zN; char *zErr; if( pParse->pNewTable ){ zT = pParse->pNewTable->pSelect ? "view" : "table"; zN = pParse->pNewTable->zName; }else if( pParse->pNewIndex ){ zT = "index"; zN = pParse->pNewIndex->zName; }else{ assert( pParse->pNewTrigger ); zT = "trigger"; zN = pParse->pNewTrigger->zName; } zErr = sqlite3_mprintf("error processing %s %s: %s", zT, zN, pParse->zErrMsg); sqlite3_result_error(pCtx, zErr, -1); sqlite3_free(zErr); } /* ** SQL function: ** ** sqlite_rename_column(zSql, iCol, bQuote, zNew, zTable, zOld) ** ** 0. zSql: SQL statement to rewrite |
︙ | ︙ | |||
1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 | if( iCol<0 ) return; pTab = sqlite3FindTable(db, zTable, zDb); if( pTab==0 || iCol>=pTab->nCol ) return; zOld = pTab->aCol[iCol].zName; memset(&sCtx, 0, sizeof(sCtx)); sCtx.iCol = ((iCol==pTab->iPKey) ? -1 : iCol); memset(&sParse, 0, sizeof(sParse)); sParse.eParseMode = PARSE_MODE_RENAME_COLUMN; sParse.db = db; sParse.nQueryLoop = 1; rc = sqlite3RunParser(&sParse, zSql, &zErr); | > > > | > > > < < < | < < < < | < < < < | < > > > > > > > > > > > > > > > > > > < < < < < < | 1110 1111 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 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 | if( iCol<0 ) return; pTab = sqlite3FindTable(db, zTable, zDb); if( pTab==0 || iCol>=pTab->nCol ) return; zOld = pTab->aCol[iCol].zName; memset(&sCtx, 0, sizeof(sCtx)); sCtx.iCol = ((iCol==pTab->iPKey) ? -1 : iCol); /* Parse the SQL statement passed as the first argument. If no error ** occurs and the parse does not result in a new table, index or ** trigger object, the database must be corrupt. */ memset(&sParse, 0, sizeof(sParse)); sParse.eParseMode = PARSE_MODE_RENAME_COLUMN; sParse.db = db; sParse.nQueryLoop = 1; rc = sqlite3RunParser(&sParse, zSql, &zErr); assert( sParse.zErrMsg==0 ); assert( rc!=SQLITE_OK || zErr==0 ); assert( (!!sParse.pNewTable)+(!!sParse.pNewIndex)+(!!sParse.pNewTrigger)<2 ); sParse.zErrMsg = zErr; if( db->mallocFailed ) rc = SQLITE_NOMEM; if( rc==SQLITE_OK && sParse.pNewTable==0 && sParse.pNewIndex==0 && sParse.pNewTrigger==0 ){ rc = SQLITE_CORRUPT_BKPT; } #ifdef SQLITE_DEBUG /* Ensure that all mappings in the Parse.pRename list really do map to ** a part of the input string. */ assert( sqlite3Strlen30(zSql)==nSql ); if( rc==SQLITE_OK ){ RenameToken *pToken; for(pToken=sParse.pRename; pToken; pToken=pToken->pNext){ assert( pToken->t.z>=zSql && &pToken->t.z[pToken->t.n]<=&zSql[nSql] ); } } #endif /* Set zQuot to point to a buffer containing a quoted copy of the ** identifier zNew. If the corresponding identifier in the original ** ALTER TABLE statement was quoted (bQuote==1), then set zNew to ** point to zQuot so that all substitutions are made using the ** quoted version of the new column name. */ if( rc==SQLITE_OK ){ zQuot = sqlite3_mprintf("\"%w\"", zNew); if( zQuot==0 ){ rc = SQLITE_NOMEM; }else{ nQuot = sqlite3Strlen30(zQuot); } } if( bQuote ){ zNew = zQuot; nNew = nQuot; } /* Find tokens that need to be replaced. */ memset(&sWalker, 0, sizeof(Walker)); sWalker.pParse = &sParse; sWalker.xExprCallback = renameColumnExprCb; sWalker.xSelectCallback = renameColumnSelectCb; sWalker.u.pRename = &sCtx; sCtx.pTab = pTab; if( rc!=SQLITE_OK ) goto renameColumnFunc_done; if( sParse.pNewTable ){ Select *pSelect = sParse.pNewTable->pSelect; if( pSelect ){ sParse.rc = SQLITE_OK; sqlite3SelectPrep(&sParse, sParse.pNewTable->pSelect, 0); rc = (db->mallocFailed ? SQLITE_NOMEM : sParse.rc); if( rc==SQLITE_OK ){ sqlite3WalkSelect(&sWalker, pSelect); } if( rc!=SQLITE_OK ) goto renameColumnFunc_done; }else{ /* A regular table */ int bFKOnly = sqlite3_stricmp(zTable, sParse.pNewTable->zName); FKey *pFKey; assert( sParse.pNewTable->pSelect==0 ); |
︙ | ︙ | |||
1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 | sqlite3WalkExprList(&sWalker, pUpsert->pUpsertSet); sqlite3WalkExpr(&sWalker, pUpsert->pUpsertWhere); sqlite3WalkExpr(&sWalker, pUpsert->pUpsertTargetWhere); } } } assert( rc==SQLITE_OK ); assert( nQuot>=nNew ); zOut = sqlite3DbMallocZero(db, nSql + sCtx.nList*nQuot + 1); if( zOut ){ int nOut = nSql; memcpy(zOut, zSql, nSql); while( sCtx.pList ){ | > > > > | 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 | sqlite3WalkExprList(&sWalker, pUpsert->pUpsertSet); sqlite3WalkExpr(&sWalker, pUpsert->pUpsertWhere); sqlite3WalkExpr(&sWalker, pUpsert->pUpsertTargetWhere); } } } /* At this point sCtx.pList contains a list of RenameToken objects ** corresponding to all tokens in the input SQL that must be replaced ** with the new column name. All that remains is to construct and ** return the edited SQL string. */ assert( rc==SQLITE_OK ); assert( nQuot>=nNew ); zOut = sqlite3DbMallocZero(db, nSql + sCtx.nList*nQuot + 1); if( zOut ){ int nOut = nSql; memcpy(zOut, zSql, nSql); while( sCtx.pList ){ |
︙ | ︙ | |||
1330 1331 1332 1333 1334 1335 1336 | sqlite3DbFree(db, zOut); }else{ rc = SQLITE_NOMEM; } renameColumnFunc_done: if( rc!=SQLITE_OK ){ | | | > | 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 | sqlite3DbFree(db, zOut); }else{ rc = SQLITE_NOMEM; } renameColumnFunc_done: if( rc!=SQLITE_OK ){ if( sParse.zErrMsg ){ renameColumnParseError(context, &sParse); }else{ sqlite3_result_error_code(context, rc); } } if( sParse.pVdbe ){ sqlite3VdbeFinalize(sParse.pVdbe); } sqlite3DeleteTable(db, sParse.pNewTable); if( sParse.pNewIndex ) sqlite3FreeIndex(db, sParse.pNewIndex); sqlite3DeleteTrigger(db, sParse.pNewTrigger); renameTokenFree(db, sParse.pRename); renameTokenFree(db, sCtx.pList); sqlite3DbFree(db, sParse.zErrMsg); sqlite3ParserReset(&sParse); sqlite3_free(zQuot); } /* ** Register built-in functions used to help implement ALTER TABLE */ |
︙ | ︙ |
Changes to test/altercol.test.
︙ | ︙ | |||
21 22 23 24 25 26 27 28 29 30 31 32 33 34 | set testprefix altercol # If SQLITE_OMIT_ALTERTABLE is defined, omit this file. ifcapable !altertable { finish_test return } foreach {tn before after} { 1 {CREATE TABLE t1(a INTEGER, b TEXT, c BLOB)} {CREATE TABLE t1(a INTEGER, d TEXT, c BLOB)} 2 {CREATE TABLE t1(a INTEGER, x TEXT, "b" BLOB)} {CREATE TABLE t1(a INTEGER, x TEXT, "d" BLOB)} | > > > > > > > > > > > > > > | 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 | set testprefix altercol # If SQLITE_OMIT_ALTERTABLE is defined, omit this file. ifcapable !altertable { finish_test return } # Drop all the tables and views in the 'main' database of database connect # [db]. Sort the objects by name before dropping them. # proc drop_all_tables_and_views {db} { set SQL { SELECT name, type FROM sqlite_master WHERE type IN ('table', 'view') AND name NOT LIKE 'sqlite_%' ORDER BY 1 } foreach {z t} [db eval $SQL] { db eval "DROP $t $z" } } foreach {tn before after} { 1 {CREATE TABLE t1(a INTEGER, b TEXT, c BLOB)} {CREATE TABLE t1(a INTEGER, d TEXT, c BLOB)} 2 {CREATE TABLE t1(a INTEGER, x TEXT, "b" BLOB)} {CREATE TABLE t1(a INTEGER, x TEXT, "d" BLOB)} |
︙ | ︙ | |||
320 321 322 323 324 325 326 | } do_execsql_test 8.4.4 { ALTER TABLE b2 RENAME x TO hello; SELECT sql FROM sqlite_master WHERE name='xxx'; } {{CREATE VIEW xxx AS SELECT a FROM b1 UNION SELECT hello FROM b2 ORDER BY 1 COLLATE nocase}} | | | < < | < < < < | | 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 | } do_execsql_test 8.4.4 { ALTER TABLE b2 RENAME x TO hello; SELECT sql FROM sqlite_master WHERE name='xxx'; } {{CREATE VIEW xxx AS SELECT a FROM b1 UNION SELECT hello FROM b2 ORDER BY 1 COLLATE nocase}} do_catchsql_test 8.4.5 { CREATE VIEW zzz AS SELECT george, ringo FROM b1; ALTER TABLE b1 RENAME a TO aaa; } {1 {error processing view zzz: no such column: george}} #------------------------------------------------------------------------- # More triggers. # proc do_rename_column_test {tn old new lSchema} { for {set i 0} {$i < 2} {incr i} { drop_all_tables_and_views db set lSorted [list] foreach sql $lSchema { execsql $sql lappend lSorted [string trim $sql] } set lSorted [lsort $lSorted] do_execsql_test $tn.$i.1 { SELECT sql FROM sqlite_master WHERE sql!='' ORDER BY 1 } $lSorted if {$i==1} { db close sqlite3 db test.db } do_execsql_test $tn.$i.2 "ALTER TABLE t1 RENAME $old TO $new" do_execsql_test $tn.$i.3 { |
︙ | ︙ | |||
428 429 430 431 432 433 434 | { CREATE VIRTUAL TABLE e1 USING echo(t1) } } } { register_echo_module db do_rename_column_test 10.$tn $old $new $lSchema } | > > > | > > > > > > > > > > > > > > > > > > > > > > | 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 | { CREATE VIRTUAL TABLE e1 USING echo(t1) } } } { register_echo_module db do_rename_column_test 10.$tn $old $new $lSchema } #-------------------------------------------------------------------------- # Test that if a view or trigger refers to a virtual table for which the # module is not available, RENAME COLUMN cannot proceed. # reset_db register_echo_module db do_execsql_test 11.0 { CREATE TABLE x1(a, b, c); CREATE VIRTUAL TABLE e1 USING echo(x1); } db close sqlite3 db test.db do_execsql_test 11.1 { ALTER TABLE x1 RENAME b TO bbb; SELECT sql FROM sqlite_master; } { {CREATE TABLE x1(a, bbb, c)} {CREATE VIRTUAL TABLE e1 USING echo(x1)} } do_execsql_test 11.2 { CREATE VIEW v1 AS SELECT e1.*, x1.c FROM e1, x1; } do_catchsql_test 11.3 { ALTER TABLE x1 RENAME c TO ccc; } {1 {error processing view v1: no such module: echo}} finish_test |