/ Check-in [eac2aa7d]
Login

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

Overview
Comment:Ensure b-tree mutexes are always held when sqlite3FindTable() is called. Do not invoke the authorizer callback when parsing schema items as part of ALTER TABLE commands. Fix test script issues.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | alter-table-rename-table
Files: files | file ages | folders
SHA3-256:eac2aa7dce577f1998cc2078dd8ac384070f8acf1ceb565999a5cf719092024e
User & Date: dan 2018-08-31 18:23:53
Context
2018-09-01
15:49
Merge fixes and enhancements from trunk. check-in: 589186c0 user: drh tags: alter-table-rename-table
2018-08-31
18:23
Ensure b-tree mutexes are always held when sqlite3FindTable() is called. Do not invoke the authorizer callback when parsing schema items as part of ALTER TABLE commands. Fix test script issues. check-in: eac2aa7d user: dan tags: alter-table-rename-table
2018-08-30
20:03
Test the schema after renaming a table. Ensure that temp database triggers and views are updated when renaming a column. check-in: f3c27d91 user: dan tags: alter-table-rename-table
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to src/alter.c.

1157
1158
1159
1160
1161
1162
1163













1164
1165
1166
1167
1168
1169
1170
....
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220



1221
1222
1223
1224
1225
1226
1227
....
1230
1231
1232
1233
1234
1235
1236



1237
1238
1239
1240
1241
1242
1243
....
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344



1345
1346
1347
1348
1349
1350
1351
....
1373
1374
1375
1376
1377
1378
1379

1380
1381
1382

1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
....
1396
1397
1398
1399
1400
1401
1402

1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413




1414


1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481

1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502

1503
1504
1505
1506



1507

1508
1509
1510
1511
1512
1513
1514
....
1515
1516
1517
1518
1519
1520
1521





1522
1523
1524
1525
1526
1527
1528
....
1534
1535
1536
1537
1538
1539
1540

1541
1542
1543
1544

1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
      sqlite3WalkExprList(pWalker, pUpsert->pUpsertTarget);
      sqlite3WalkExprList(pWalker, pUpsert->pUpsertSet);
      sqlite3WalkExpr(pWalker, pUpsert->pUpsertWhere);
      sqlite3WalkExpr(pWalker, pUpsert->pUpsertTargetWhere);
    }
  }
}














/*
** SQL function:
**
**     sqlite_rename_column(zSql, iCol, bQuote, zNew, zTable, zOld)
**
**   0. zSql:     SQL statement to rewrite
................................................................................
  const char *zTable = (const char*)sqlite3_value_text(argv[4]);
  int iCol = sqlite3_value_int(argv[5]);
  const char *zNew = (const char*)sqlite3_value_text(argv[6]);
  int nNew = sqlite3_value_bytes(argv[6]);
  int bQuote = sqlite3_value_int(argv[7]);
  const char *zOld;
  int bTemp = 0;

  int rc;
  char *zErr = 0;
  Parse sParse;
  Walker sWalker;
  Index *pIdx;
  char *zOut = 0;

  int i;
  Table *pTab;




  UNUSED_PARAMETER(NotUsed);
  if( zSql==0 ) return;
  if( zTable==0 ) return;
  if( zNew==0 ) return;
  if( iCol<0 ) return;
  sqlite3BtreeEnterAll(db);
................................................................................
    sqlite3BtreeLeaveAll(db);
    return;
  }
  zOld = pTab->aCol[iCol].zName;
  memset(&sCtx, 0, sizeof(sCtx));
  sCtx.iCol = ((iCol==pTab->iPKey) ? -1 : iCol);




  rc = renameParseSql(&sParse, zDb, 0, db, zSql, bTemp);

  /* Find tokens that need to be replaced. */
  memset(&sWalker, 0, sizeof(Walker));
  sWalker.pParse = &sParse;
  sWalker.xExprCallback = renameColumnExprCb;
  sWalker.xSelectCallback = renameColumnSelectCb;
................................................................................
    if( sParse.zErrMsg ){
      renameColumnParseError(context, 0, argv[1], argv[2], &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);



  sqlite3BtreeLeaveAll(db);
}

static int renameTableExprCb(Walker *pWalker, Expr *pExpr){
  RenameCtx *p = pWalker->u.pRename;
  if( pExpr->op==TK_COLUMN && p->pTab==pExpr->pTab ){
    renameTokenFind(pWalker->pParse, p, (void*)&pExpr->pTab);
................................................................................

/*
** This C function implements an SQL user function that is used by SQL code
** generated by the ALTER TABLE ... RENAME command to modify the definition
** of any foreign key constraints that use the table being renamed as the 
** parent table. It is passed three arguments:
**

**   1) The complete text of the CREATE TABLE statement being modified,
**   2) The old name of the table being renamed, and
**   3) The new name of the table being renamed.

**
** It returns the new CREATE TABLE statement. For example:
**
**   sqlite_rename_table('CREATE TABLE t1(a REFERENCES t2)', 't2', 't3')
**       -> 'CREATE TABLE t1(a REFERENCES t3)'
*/
static void renameTableFunc(
  sqlite3_context *context,
  int NotUsed,
  sqlite3_value **argv
){
................................................................................
  char *zResult;
  unsigned char const *zDb = sqlite3_value_text(argv[0]);
  unsigned char const *zInput = sqlite3_value_text(argv[1]);
  unsigned char const *zOld = sqlite3_value_text(argv[2]);
  unsigned char const *zNew = sqlite3_value_text(argv[3]);
  int bTemp = sqlite3_value_int(argv[4]);


  unsigned const char *z;         /* Pointer to token */
  int n;                          /* Length of token z */
  int token;                      /* Type of token */

  Parse sParse;
  int rc;
  int bQuote = 1;
  RenameCtx sCtx;
  Walker sWalker;

  if( zInput==0 || zOld==0 || zNew==0 ) return;







  memset(&sCtx, 0, sizeof(RenameCtx));
  sCtx.pTab = sqlite3FindTable(db, zOld, zDb);
  memset(&sWalker, 0, sizeof(Walker));
  sWalker.pParse = &sParse;
  sWalker.xExprCallback = renameTableExprCb;
  sWalker.xSelectCallback = renameTableSelectCb;
  sWalker.u.pRename = &sCtx;

  sqlite3BtreeEnterAll(db);

  rc = renameParseSql(&sParse, zDb, 1, db, zInput, bTemp);

  if( rc==SQLITE_OK ){
    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);
      }else{
        /* Modify any FK definitions to point to the new table. */
#ifndef SQLITE_OMIT_FOREIGN_KEY
        FKey *pFKey;
        for(pFKey=pTab->pFKey; pFKey; pFKey=pFKey->pNextFrom){
          if( sqlite3_stricmp(pFKey->zTo, zOld)==0 ){
            renameTokenFind(&sParse, &sCtx, (void*)pFKey->zTo);
          }
        }
#endif

        /* If this is the table being altered, fix any table refs in CHECK
        ** expressions. Also update the name that appears right after the
        ** "CREATE [VIRTUAL] TABLE" bit. */
        if( sqlite3_stricmp(zOld, pTab->zName)==0 ){
          sCtx.pTab = pTab;
          sqlite3WalkExprList(&sWalker, pTab->pCheck);
          renameTokenFind(&sParse, &sCtx, pTab->zName);
        }
      }
    }

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

#ifndef SQLITE_OMIT_TRIGGER
    else if( sParse.pNewTrigger ){
      Trigger *pTrigger = sParse.pNewTrigger;
      TriggerStep *pStep;
      if( 0==sqlite3_stricmp(sParse.pNewTrigger->table, zOld) 
       && sCtx.pTab->pSchema==pTrigger->pTabSchema
      ){
        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);

          }
        }
      }
    }
#endif
  }

  if( rc==SQLITE_OK ){
    rc = renameEditSql(context, &sCtx, zInput, zNew, bQuote);
  }

  if( rc!=SQLITE_OK ){
    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);
  sqlite3BtreeLeaveAll(db);





  return;
}

static void renameTableTest(
  sqlite3_context *context,
  int NotUsed,
  sqlite3_value **argv
................................................................................
){
  sqlite3 *db = sqlite3_context_db_handle(context);
  unsigned char const *zDb = sqlite3_value_text(argv[0]);
  unsigned char const *zInput = sqlite3_value_text(argv[1]);
  int bTemp = sqlite3_value_int(argv[4]);
  int rc;
  Parse sParse;






  rc = renameParseSql(&sParse, zDb, 1, db, zInput, bTemp);
  if( rc==SQLITE_OK ){
    if( sParse.pNewTable && sParse.pNewTable->pSelect ){
      NameContext sNC;
      memset(&sNC, 0, sizeof(sNC));
      sNC.pParse = &sParse;
................................................................................
      rc = renameResolveTrigger(&sParse, bTemp ? 0 : zDb);
    }
  }

  if( rc!=SQLITE_OK ){
    renameColumnParseError(context, 1, argv[2], argv[3], &sParse);
  }


  if( sParse.pVdbe ){
    sqlite3VdbeFinalize(sParse.pVdbe);
  }

  sqlite3DeleteTable(db, sParse.pNewTable);
  if( sParse.pNewIndex ) sqlite3FreeIndex(db, sParse.pNewIndex);
  sqlite3DeleteTrigger(db, sParse.pNewTrigger);
  sqlite3DbFree(db, sParse.zErrMsg);
  renameTokenFree(db, sParse.pRename);
  sqlite3ParserReset(&sParse);
}

/*
** Register built-in functions used to help implement ALTER TABLE
*/
void sqlite3AlterFunctions(void){
  static FuncDef aAlterTableFuncs[] = {







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







 







<






<


>
>
>







 







>
>
>







 







|
<
<
<
<
<
<

<
<
>
>
>







 







>
|
|
|
>

|

|







 







>
|
|
|

|
|
|
|
|

<
>
>
>
>

>
>
|
|
|
|
|
|
|

<
<
|

|
|
|

|
|
|
|

|
|
|
|
|

|
|
|
|
|
|


|
|
|
|
|
|
|
|
|
|

|
|
|
|


|
|
|
|
|
|
|
|

|
|
|
|
|
|
>



<

|

|
|
|
<
|
|
|
<
<
|
<
<
<
<
>
|
<
<
|
>
>
>
|
>







 







>
>
>
>
>







 







>

<
|
<
>
|
<
<
<
<
<







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
....
1217
1218
1219
1220
1221
1222
1223

1224
1225
1226
1227
1228
1229

1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
....
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
....
1345
1346
1347
1348
1349
1350
1351
1352






1353


1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
....
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
....
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427

1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442


1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503

1504
1505
1506
1507
1508
1509

1510
1511
1512


1513




1514
1515


1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
....
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
....
1553
1554
1555
1556
1557
1558
1559
1560
1561

1562

1563
1564





1565
1566
1567
1568
1569
1570
1571
      sqlite3WalkExprList(pWalker, pUpsert->pUpsertTarget);
      sqlite3WalkExprList(pWalker, pUpsert->pUpsertSet);
      sqlite3WalkExpr(pWalker, pUpsert->pUpsertWhere);
      sqlite3WalkExpr(pWalker, pUpsert->pUpsertTargetWhere);
    }
  }
}

static void renameParseCleanup(Parse *pParse){
  sqlite3 *db = pParse->db;
  if( pParse->pVdbe ){
    sqlite3VdbeFinalize(pParse->pVdbe);
  }
  sqlite3DeleteTable(db, pParse->pNewTable);
  if( pParse->pNewIndex ) sqlite3FreeIndex(db, pParse->pNewIndex);
  sqlite3DeleteTrigger(db, pParse->pNewTrigger);
  sqlite3DbFree(db, pParse->zErrMsg);
  renameTokenFree(db, pParse->pRename);
  sqlite3ParserReset(pParse);
}

/*
** SQL function:
**
**     sqlite_rename_column(zSql, iCol, bQuote, zNew, zTable, zOld)
**
**   0. zSql:     SQL statement to rewrite
................................................................................
  const char *zTable = (const char*)sqlite3_value_text(argv[4]);
  int iCol = sqlite3_value_int(argv[5]);
  const char *zNew = (const char*)sqlite3_value_text(argv[6]);
  int nNew = sqlite3_value_bytes(argv[6]);
  int bQuote = sqlite3_value_int(argv[7]);
  const char *zOld;
  int bTemp = 0;

  int rc;
  char *zErr = 0;
  Parse sParse;
  Walker sWalker;
  Index *pIdx;
  char *zOut = 0;

  int i;
  Table *pTab;
#ifndef SQLITE_OMIT_AUTHORIZATION
  sqlite3_xauth xAuth = db->xAuth;
#endif

  UNUSED_PARAMETER(NotUsed);
  if( zSql==0 ) return;
  if( zTable==0 ) return;
  if( zNew==0 ) return;
  if( iCol<0 ) return;
  sqlite3BtreeEnterAll(db);
................................................................................
    sqlite3BtreeLeaveAll(db);
    return;
  }
  zOld = pTab->aCol[iCol].zName;
  memset(&sCtx, 0, sizeof(sCtx));
  sCtx.iCol = ((iCol==pTab->iPKey) ? -1 : iCol);

#ifndef SQLITE_OMIT_AUTHORIZATION
  db->xAuth = 0;
#endif
  rc = renameParseSql(&sParse, zDb, 0, db, zSql, bTemp);

  /* Find tokens that need to be replaced. */
  memset(&sWalker, 0, sizeof(Walker));
  sWalker.pParse = &sParse;
  sWalker.xExprCallback = renameColumnExprCb;
  sWalker.xSelectCallback = renameColumnSelectCb;
................................................................................
    if( sParse.zErrMsg ){
      renameColumnParseError(context, 0, argv[1], argv[2], &sParse);
    }else{
      sqlite3_result_error_code(context, rc);
    }
  }

  renameParseCleanup(&sParse);






  renameTokenFree(db, sCtx.pList);


#ifndef SQLITE_OMIT_AUTHORIZATION
  db->xAuth = xAuth;
#endif
  sqlite3BtreeLeaveAll(db);
}

static int renameTableExprCb(Walker *pWalker, Expr *pExpr){
  RenameCtx *p = pWalker->u.pRename;
  if( pExpr->op==TK_COLUMN && p->pTab==pExpr->pTab ){
    renameTokenFind(pWalker->pParse, p, (void*)&pExpr->pTab);
................................................................................

/*
** This C function implements an SQL user function that is used by SQL code
** generated by the ALTER TABLE ... RENAME command to modify the definition
** of any foreign key constraints that use the table being renamed as the 
** parent table. It is passed three arguments:
**
**   0: The database containing the table being renamed.
**   1: The complete text of the schema statement being modified,
**   2: The old name of the table being renamed, and
**   3: The new name of the table being renamed.
**   4: True if the schema statement comes from the temp db.
**
** It returns the new schema statement. For example:
**
** sqlite_rename_table('main', 'CREATE TABLE t1(a REFERENCES t2)','t2','t3',0)
**       -> 'CREATE TABLE t1(a REFERENCES t3)'
*/
static void renameTableFunc(
  sqlite3_context *context,
  int NotUsed,
  sqlite3_value **argv
){
................................................................................
  char *zResult;
  unsigned char const *zDb = sqlite3_value_text(argv[0]);
  unsigned char const *zInput = sqlite3_value_text(argv[1]);
  unsigned char const *zOld = sqlite3_value_text(argv[2]);
  unsigned char const *zNew = sqlite3_value_text(argv[3]);
  int bTemp = sqlite3_value_int(argv[4]);

  if( zInput && zOld && zNew ){
    unsigned const char *z;         /* Pointer to token */
    int n;                          /* Length of token z */
    int token;                      /* Type of token */

    Parse sParse;
    int rc;
    int bQuote = 1;
    RenameCtx sCtx;
    Walker sWalker;


#ifndef SQLITE_OMIT_AUTHORIZATION
    sqlite3_xauth xAuth = db->xAuth;
    db->xAuth = 0;
#endif

    sqlite3BtreeEnterAll(db);

    memset(&sCtx, 0, sizeof(RenameCtx));
    sCtx.pTab = sqlite3FindTable(db, zOld, zDb);
    memset(&sWalker, 0, sizeof(Walker));
    sWalker.pParse = &sParse;
    sWalker.xExprCallback = renameTableExprCb;
    sWalker.xSelectCallback = renameTableSelectCb;
    sWalker.u.pRename = &sCtx;



    rc = renameParseSql(&sParse, zDb, 1, db, zInput, bTemp);

    if( rc==SQLITE_OK ){
      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);
        }else{
          /* Modify any FK definitions to point to the new table. */
#ifndef SQLITE_OMIT_FOREIGN_KEY
          FKey *pFKey;
          for(pFKey=pTab->pFKey; pFKey; pFKey=pFKey->pNextFrom){
            if( sqlite3_stricmp(pFKey->zTo, zOld)==0 ){
              renameTokenFind(&sParse, &sCtx, (void*)pFKey->zTo);
            }
          }
#endif

          /* If this is the table being altered, fix any table refs in CHECK
          ** expressions. Also update the name that appears right after the
          ** "CREATE [VIRTUAL] TABLE" bit. */
          if( sqlite3_stricmp(zOld, pTab->zName)==0 ){
            sCtx.pTab = pTab;
            sqlite3WalkExprList(&sWalker, pTab->pCheck);
            renameTokenFind(&sParse, &sCtx, pTab->zName);
          }
        }
      }

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

#ifndef SQLITE_OMIT_TRIGGER
      else if( sParse.pNewTrigger ){
        Trigger *pTrigger = sParse.pNewTrigger;
        TriggerStep *pStep;
        if( 0==sqlite3_stricmp(sParse.pNewTrigger->table, zOld) 
            && sCtx.pTab->pSchema==pTrigger->pTabSchema
          ){
          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);
            }
          }
        }
      }

#endif
    }

    if( rc==SQLITE_OK ){
      rc = renameEditSql(context, &sCtx, zInput, zNew, bQuote);
    }

    if( rc!=SQLITE_OK ){
      sqlite3_result_error_code(context, rc);
    }







    renameParseCleanup(&sParse);
    renameTokenFree(db, sCtx.pList);


    sqlite3BtreeLeaveAll(db);
#ifndef SQLITE_OMIT_AUTHORIZATION
    db->xAuth = xAuth;
#endif
  }

  return;
}

static void renameTableTest(
  sqlite3_context *context,
  int NotUsed,
  sqlite3_value **argv
................................................................................
){
  sqlite3 *db = sqlite3_context_db_handle(context);
  unsigned char const *zDb = sqlite3_value_text(argv[0]);
  unsigned char const *zInput = sqlite3_value_text(argv[1]);
  int bTemp = sqlite3_value_int(argv[4]);
  int rc;
  Parse sParse;

#ifndef SQLITE_OMIT_AUTHORIZATION
  sqlite3_xauth xAuth = db->xAuth;
  db->xAuth = 0;
#endif

  rc = renameParseSql(&sParse, zDb, 1, db, zInput, bTemp);
  if( rc==SQLITE_OK ){
    if( sParse.pNewTable && sParse.pNewTable->pSelect ){
      NameContext sNC;
      memset(&sNC, 0, sizeof(sNC));
      sNC.pParse = &sParse;
................................................................................
      rc = renameResolveTrigger(&sParse, bTemp ? 0 : zDb);
    }
  }

  if( rc!=SQLITE_OK ){
    renameColumnParseError(context, 1, argv[2], argv[3], &sParse);
  }
  renameParseCleanup(&sParse);


#ifndef SQLITE_OMIT_AUTHORIZATION

  db->xAuth = xAuth;
#endif





}

/*
** Register built-in functions used to help implement ALTER TABLE
*/
void sqlite3AlterFunctions(void){
  static FuncDef aAlterTableFuncs[] = {

Changes to test/alter.test.

677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693

694
695
696


697
698
699
700
701
702
703
704
705
...
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925

do_test alter-8.2 {
  execsql {
    SELECT a, sum(b) FROM t2 GROUP BY a;
  }
} {1 18 2 9}

#--------------------------------------------------------------------------
# alter-9.X - Special test: Make sure the sqlite_rename_trigger() and
# rename_table() functions do not crash when handed bad input.
#
ifcapable trigger {
  do_test alter-9.1 {
    execsql {SELECT SQLITE_RENAME_TRIGGER(0,0)}
  } {{}}
}
do_test alter-9.2 {
  execsql {

    SELECT SQLITE_RENAME_TABLE(0,0);
    SELECT SQLITE_RENAME_TABLE(10,20);
    SELECT SQLITE_RENAME_TABLE('foo', 'foo');


  }
} {{} {} {}}

#------------------------------------------------------------------------
# alter-10.X - Make sure ALTER TABLE works with multi-byte UTF-8 characters 
# in the names.
#
do_test alter-10.1 {
  execsql "CREATE TABLE xyz(x UNIQUE)"
................................................................................
  SELECT * FROM t16a ORDER BY a;
} {abc 1.25 99 xyzzy cba 5.5 98 fizzle}
do_execsql_test alter-16.2 {
  ALTER TABLE t16a RENAME TO t16a_rn;
  SELECT * FROM t16a_rn ORDER BY a;
} {abc 1.25 99 xyzzy cba 5.5 98 fizzle}

#-------------------------------------------------------------------------
# Verify that NULL values into the internal-use-only sqlite_rename_*()
# functions do not cause problems.
#
do_execsql_test alter-17.1 {
  SELECT sqlite_rename_table('CREATE TABLE xyz(a,b,c)','abc');
} {{CREATE TABLE "abc"(a,b,c)}}
do_execsql_test alter-17.2 {
  SELECT sqlite_rename_table('CREATE TABLE xyz(a,b,c)',NULL);
} {{CREATE TABLE "(NULL)"(a,b,c)}}
do_execsql_test alter-17.3 {
  SELECT sqlite_rename_table(NULL,'abc');
} {{}}
do_execsql_test alter-17.4 {
  SELECT sqlite_rename_trigger('CREATE TRIGGER r1 ON xyz WHEN','abc');
} {{CREATE TRIGGER r1 ON "abc" WHEN}}
do_execsql_test alter-17.5 {
  SELECT sqlite_rename_trigger('CREATE TRIGGER r1 ON xyz WHEN',NULL);
} {{CREATE TRIGGER r1 ON "(NULL)" WHEN}}
do_execsql_test alter-17.6 {
  SELECT sqlite_rename_trigger(NULL,'abc');
} {{}}
do_execsql_test alter-17.7 {
  SELECT sqlite_rename_parent('main', 'CREATE TABLE t1(a REFERENCES "xyzzy")',
         'xyzzy','lmnop');
} {{CREATE TABLE t1(a REFERENCES "lmnop")}}
do_execsql_test alter-17.8 {
  SELECT sqlite_rename_parent('main', 'CREATE TABLE t1(a REFERENCES "xyzzy")',
         'xyzzy',NULL);
} {{}}
do_execsql_test alter-17.9 {
  SELECT sqlite_rename_parent('main', 'CREATE TABLE t1(a REFERENCES "xyzzy")',
         NULL, 'lmnop');
} {{}}
do_execsql_test alter-17.10 {
  SELECT sqlite_rename_parent('main', NULL,'abc','xyz');
} {{}}
do_catchsql_test alter-17.11 {
  SELECT sqlite_rename_parent('main', 'create references ''','abc','xyz');
} {1 {SQL logic error}}
do_catchsql_test alter-17.12 {
  SELECT sqlite_rename_parent('main', 'create references "abc"123" ','abc','xyz');
} {1 {SQL logic error}}
do_catchsql_test alter-17.13 {
  SELECT sqlite_rename_parent('main', "references '''",'abc','xyz');
} {1 {SQL logic error}}

finish_test








|


<
|
|
|
<
<
<
>
|
|
|
>
>
|
<







 







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

>
677
678
679
680
681
682
683
684
685
686

687
688
689



690
691
692
693
694
695
696

697
698
699
700
701
702
703
...
869
870
871
872
873
874
875















































876
877
do_test alter-8.2 {
  execsql {
    SELECT a, sum(b) FROM t2 GROUP BY a;
  }
} {1 18 2 9}

#--------------------------------------------------------------------------
# alter-9.X - Special test: Make sure the sqlite_rename_column() and
# rename_table() functions do not crash when handed bad input.
#

do_test alter-9.1 {
  execsql {SELECT SQLITE_RENAME_COLUMN(0,0,0,0,0,0,0,0)}
} {{}}



foreach {tn sql} {
    1 { SELECT SQLITE_RENAME_TABLE(0,0,0,0,0) }
    2 { SELECT SQLITE_RENAME_TABLE(10,20,30,40,50) }
    3 { SELECT SQLITE_RENAME_TABLE('foo', 'foo', 'foo', 'foo', 'foo') }
} {
  do_catchsql_test alter-9.2.$tn $sql {1 {SQL logic error}}
}


#------------------------------------------------------------------------
# alter-10.X - Make sure ALTER TABLE works with multi-byte UTF-8 characters 
# in the names.
#
do_test alter-10.1 {
  execsql "CREATE TABLE xyz(x UNIQUE)"
................................................................................
  SELECT * FROM t16a ORDER BY a;
} {abc 1.25 99 xyzzy cba 5.5 98 fizzle}
do_execsql_test alter-16.2 {
  ALTER TABLE t16a RENAME TO t16a_rn;
  SELECT * FROM t16a_rn ORDER BY a;
} {abc 1.25 99 xyzzy cba 5.5 98 fizzle}
















































finish_test

Changes to test/fkey2.test.

979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
    }
  } {{CREATE TABLE t2(a, b, c REFERENCES t1, d DEFAULT NULL REFERENCES t1, e REFERENCES t1 DEFAULT NULL, h DEFAULT 'text' REFERENCES t1)}}
  
  
  # Test the sqlite_rename_parent() function directly.
  #
  proc test_rename_parent {zCreate zOld zNew} {
    db eval {SELECT sqlite_rename_parent($zCreate, $zOld, $zNew)}
  }
  do_test fkey2-14.2.1.1 {
    test_rename_parent {CREATE TABLE t1(a REFERENCES t2)} t2 t3
  } {{CREATE TABLE t1(a REFERENCES "t3")}}
  do_test fkey2-14.2.1.2 {
    test_rename_parent {CREATE TABLE t1(a REFERENCES t2)} t4 t3
  } {{CREATE TABLE t1(a REFERENCES t2)}}







|







979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
    }
  } {{CREATE TABLE t2(a, b, c REFERENCES t1, d DEFAULT NULL REFERENCES t1, e REFERENCES t1 DEFAULT NULL, h DEFAULT 'text' REFERENCES t1)}}
  
  
  # Test the sqlite_rename_parent() function directly.
  #
  proc test_rename_parent {zCreate zOld zNew} {
    db eval {SELECT sqlite_rename_table('main', $zCreate, $zOld, $zNew, 0)}
  }
  do_test fkey2-14.2.1.1 {
    test_rename_parent {CREATE TABLE t1(a REFERENCES t2)} t2 t3
  } {{CREATE TABLE t1(a REFERENCES "t3")}}
  do_test fkey2-14.2.1.2 {
    test_rename_parent {CREATE TABLE t1(a REFERENCES t2)} t4 t3
  } {{CREATE TABLE t1(a REFERENCES t2)}}

Changes to test/without_rowid3.test.

945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
    }
  } {{CREATE TABLE t2(a, b, c REFERENCES t1, d DEFAULT NULL REFERENCES t1, e REFERENCES t1 DEFAULT NULL, h DEFAULT 'text' REFERENCES t1)}}
  
  
  # Test the sqlite_rename_parent() function directly.
  #
  proc test_rename_parent {zCreate zOld zNew} {
    db eval {SELECT sqlite_rename_parent($zCreate, $zOld, $zNew)}
  }
  do_test without_rowid3-14.2.1.1 {
    test_rename_parent {CREATE TABLE t1(a REFERENCES t2)} t2 t3
  } {{CREATE TABLE t1(a REFERENCES "t3")}}
  do_test without_rowid3-14.2.1.2 {
    test_rename_parent {CREATE TABLE t1(a REFERENCES t2)} t4 t3
  } {{CREATE TABLE t1(a REFERENCES t2)}}







|







945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
    }
  } {{CREATE TABLE t2(a, b, c REFERENCES t1, d DEFAULT NULL REFERENCES t1, e REFERENCES t1 DEFAULT NULL, h DEFAULT 'text' REFERENCES t1)}}
  
  
  # Test the sqlite_rename_parent() function directly.
  #
  proc test_rename_parent {zCreate zOld zNew} {
    db eval {SELECT sqlite_rename_table('main', $zCreate, $zOld, $zNew, 0)}
  }
  do_test without_rowid3-14.2.1.1 {
    test_rename_parent {CREATE TABLE t1(a REFERENCES t2)} t2 t3
  } {{CREATE TABLE t1(a REFERENCES "t3")}}
  do_test without_rowid3-14.2.1.2 {
    test_rename_parent {CREATE TABLE t1(a REFERENCES t2)} t4 t3
  } {{CREATE TABLE t1(a REFERENCES t2)}}