/ Check-in [18ba35b86f]
Login

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

Overview
Comment:Fix minor code issues in alter.c.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | alter-table-rename-column
Files: files | file ages | folders
SHA3-256: 18ba35b86f3b9813179b5f8d74e59e4860bfb800f45aabab8d6c0a6d7c97fe74
User & Date: dan 2018-09-05 08:28:30
Context
2018-09-05
14:36
Avoid comparing pointer values after the object that they point to has been deleted. check-in: 2ec7e50cbc user: dan tags: alter-table-rename-column
08:28
Fix minor code issues in alter.c. check-in: 18ba35b86f user: dan tags: alter-table-rename-column
2018-09-04
18:23
Merge latest trunk changes into this branch. check-in: ef9e088290 user: dan tags: alter-table-rename-column
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Changes to src/alter.c.

   831    831         if( 0==sqlite3_stricmp(zName, zOld) ){
   832    832           renameTokenFind(pParse, pCtx, (void*)zName);
   833    833         }
   834    834       }
   835    835     }
   836    836   }
   837    837   
          838  +/*
          839  +** Parse the SQL statement zSql using Parse object (*p). The Parse object
          840  +** is initialized by this function before it is used.
          841  +*/
   838    842   static int renameParseSql(
   839         -  Parse *p, 
   840         -  const char *zDb, 
   841         -  int bTable, 
   842         -  sqlite3 *db, 
   843         -  const char *zSql,
   844         -  int bTemp
          843  +  Parse *p,                       /* Memory to use for Parse object */
          844  +  const char *zDb,                /* Name of schema SQL belongs to */
          845  +  int bTable,                     /* 1 -> RENAME TABLE, 0 -> RENAME COLUMN */
          846  +  sqlite3 *db,                    /* Database handle */
          847  +  const char *zSql,               /* SQL to parse */
          848  +  int bTemp                       /* True if SQL is from temp schema */
   845    849   ){
   846    850     int rc;
   847    851     char *zErr = 0;
   848    852   
   849    853     db->init.iDb = bTemp ? 1 : sqlite3FindDbName(db, zDb);
   850    854   
   851    855     /* Parse the SQL statement passed as the first argument. If no error
................................................................................
   879    883     }
   880    884   #endif
   881    885   
   882    886     db->init.iDb = 0;
   883    887     return rc;
   884    888   }
   885    889   
          890  +/*
          891  +** This function edits SQL statement zSql, replacing each token identified
          892  +** by the linked list pRename with the text of zNew. If argument bQuote is
          893  +** true, then zNew is always quoted first. If no error occurs, the result
          894  +** is loaded into context object pCtx as the result.
          895  +**
          896  +** Or, if an error occurs (i.e. an OOM condition), an error is left in
          897  +** pCtx and an SQLite error code returned.
          898  +*/
   886    899   static int renameEditSql(
   887    900     sqlite3_context *pCtx,          /* Return result here */
   888    901     RenameCtx *pRename,             /* Rename context */
   889    902     const char *zSql,               /* SQL statement to edit */
   890    903     const char *zNew,               /* New token text */
   891    904     int bQuote                      /* True to always quote token */
   892    905   ){
................................................................................
   955    968       rc = SQLITE_NOMEM;
   956    969     }
   957    970   
   958    971     sqlite3_free(zQuot);
   959    972     return rc;
   960    973   }
   961    974   
   962         -static int renameResolveTrigger(
   963         -  Parse *pParse,
   964         -  const char *zDb
   965         -){
          975  +/*
          976  +** Resolve all symbols in the trigger at pParse->pNewTrigger, assuming
          977  +** it was read from the schema of database zDb. Return SQLITE_OK if 
          978  +** successful. Otherwise, return an SQLite error code and leave an error
          979  +** message in the Parse object.
          980  +*/
          981  +static int renameResolveTrigger(Parse *pParse, const char *zDb){
   966    982     sqlite3 *db = pParse->db;
   967    983     TriggerStep *pStep;
   968    984     NameContext sNC;
   969    985     int rc = SQLITE_OK;
   970    986   
   971    987     memset(&sNC, 0, sizeof(sNC));
   972    988     sNC.pParse = pParse;
................................................................................
  1025   1041           }
  1026   1042         }
  1027   1043       }
  1028   1044     }
  1029   1045     return rc;
  1030   1046   }
  1031   1047   
         1048  +/*
         1049  +** Invoke sqlite3WalkExpr() or sqlite3WalkSelect() on all Select or Expr
         1050  +** objects that are part of the trigger passed as the second argument.
         1051  +*/
  1032   1052   static void renameWalkTrigger(Walker *pWalker, Trigger *pTrigger){
  1033   1053     TriggerStep *pStep;
  1034   1054   
  1035   1055     /* Find tokens to edit in WHEN clause */
  1036   1056     sqlite3WalkExpr(pWalker, pTrigger->pWhen);
  1037   1057   
  1038   1058     /* Find tokens to edit in trigger steps */
................................................................................
  1046   1066         sqlite3WalkExprList(pWalker, pUpsert->pUpsertSet);
  1047   1067         sqlite3WalkExpr(pWalker, pUpsert->pUpsertWhere);
  1048   1068         sqlite3WalkExpr(pWalker, pUpsert->pUpsertTargetWhere);
  1049   1069       }
  1050   1070     }
  1051   1071   }
  1052   1072   
         1073  +/*
         1074  +** Free the contents of Parse object (*pParse). Do not free the memory
         1075  +** occupied by the Parse object itself.
         1076  +*/
  1053   1077   static void renameParseCleanup(Parse *pParse){
  1054   1078     sqlite3 *db = pParse->db;
  1055   1079     if( pParse->pVdbe ){
  1056   1080       sqlite3VdbeFinalize(pParse->pVdbe);
  1057   1081     }
  1058   1082     sqlite3DeleteTable(db, pParse->pNewTable);
  1059   1083     if( pParse->pNewIndex ) sqlite3FreeIndex(db, pParse->pNewIndex);
................................................................................
  1238   1262     renameTokenFree(db, sCtx.pList);
  1239   1263   #ifndef SQLITE_OMIT_AUTHORIZATION
  1240   1264     db->xAuth = xAuth;
  1241   1265   #endif
  1242   1266     sqlite3BtreeLeaveAll(db);
  1243   1267   }
  1244   1268   
         1269  +/*
         1270  +** Walker expression callback used by "RENAME TABLE". 
         1271  +*/
  1245   1272   static int renameTableExprCb(Walker *pWalker, Expr *pExpr){
  1246   1273     RenameCtx *p = pWalker->u.pRename;
  1247   1274     if( pExpr->op==TK_COLUMN && p->pTab==pExpr->pTab ){
  1248   1275       renameTokenFind(pWalker->pParse, p, (void*)&pExpr->pTab);
  1249   1276     }
  1250   1277     return WRC_Continue;
  1251   1278   }
  1252   1279   
  1253   1280   /*
  1254         -** This is a Walker select callback. 
         1281  +** Walker select callback used by "RENAME TABLE". 
  1255   1282   */
  1256   1283   static int renameTableSelectCb(Walker *pWalker, Select *pSelect){
  1257   1284     int i;
  1258   1285     RenameCtx *p = pWalker->u.pRename;
  1259   1286     SrcList *pSrc = pSelect->pSrc;
  1260   1287     for(i=0; i<pSrc->nSrc; i++){
  1261   1288       struct SrcList_item *pItem = &pSrc->a[i];
................................................................................
  1404   1431       db->xAuth = xAuth;
  1405   1432   #endif
  1406   1433     }
  1407   1434   
  1408   1435     return;
  1409   1436   }
  1410   1437   
         1438  +/*
         1439  +** An SQL user function that checks that there are no parse or symbol
         1440  +** resolution problems in a CREATE TRIGGER|TABLE|VIEW|INDEX statement.
         1441  +** After an ALTER TABLE .. RENAME operation is performed and the schema
         1442  +** reloaded, this function is called on each SQL statement in the schema
         1443  +** to ensure that it are still usable.
         1444  +**
         1445  +**   0: Database name ("main", "temp" etc.).
         1446  +**   1: SQL statement.
         1447  +**   2: Object type ("view", "table", "trigger" or "index").
         1448  +**   3: Object name.
         1449  +**   4: True if object is from temp schema.
         1450  +*/
  1411   1451   static void renameTableTest(
  1412   1452     sqlite3_context *context,
  1413   1453     int NotUsed,
  1414   1454     sqlite3_value **argv
  1415   1455   ){
  1416   1456     sqlite3 *db = sqlite3_context_db_handle(context);
  1417   1457     char const *zDb = (const char*)sqlite3_value_text(argv[0]);