SQLite

Check-in [ad072a835f]
Login

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

Overview
Comment:Avoid incorrectly replacing tokens that refer to a column being renamed via an alias. For example, do not overwrite "xyz" when "a" is renamed in "CREATE VIEW v1 AS SELECT a AS xyz FROM tbl WHERE xyz=1"
Downloads: Tarball | ZIP archive
Timelines: family | ancestors | descendants | both | alter-table-rename-column
Files: files | file ages | folders
SHA3-256: ad072a835f97ff418f5919d94f547ce8afb4fc7c7b590deba41f1e62136a79ac
User & Date: dan 2018-08-24 16:04:26.814
Context
2018-08-24
17:55
After modifying and reparsing the schema as part of an ALTER TABLE RENAME COLUMN, check that no new schema errors have been introduced (e.g. ambiguous column names in views) before committing the operation. (check-in: a0e06d2c5e user: dan tags: alter-table-rename-column)
16:04
Avoid incorrectly replacing tokens that refer to a column being renamed via an alias. For example, do not overwrite "xyz" when "a" is renamed in "CREATE VIEW v1 AS SELECT a AS xyz FROM tbl WHERE xyz=1" (check-in: ad072a835f user: dan tags: alter-table-rename-column)
2018-08-23
20:09
Add the "atrc" test program to the Makefiles. Fix a typo in the instructions in the header comment of the atrc program. (check-in: 2130a407dd user: drh tags: alter-table-rename-column)
Changes
Unified Diff Ignore Whitespace Patch
Changes to src/alter.c.
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
  RenameToken *p;
  for(p=pParse->pRename; ALWAYS(p); p=p->pNext){
    if( p->p==pFrom ){
      p->p = pTo;
      break;
    }
  }
  assert( p );
}

/*
** Free the list of RenameToken objects given in the second argument
*/
static void renameTokenFree(sqlite3 *db, RenameToken *pToken){
  RenameToken *pNext;







|







962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
  RenameToken *p;
  for(p=pParse->pRename; ALWAYS(p); p=p->pNext){
    if( p->p==pFrom ){
      p->p = pTo;
      break;
    }
  }
  assert( pTo==0 || p );
}

/*
** Free the list of RenameToken objects given in the second argument
*/
static void renameTokenFree(sqlite3 *db, RenameToken *pToken){
  RenameToken *pNext;
985
986
987
988
989
990
991

992
993
994
995
996
997
998
** 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 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 = pCtx->pList;
      pCtx->pList = pToken;
      pCtx->nList++;







>







985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
** 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 void renameTokenFind(Parse *pParse, struct RenameCtx *pCtx, void *pPtr){
  RenameToken **pp;
  assert( pPtr!=0 );
  for(pp=&pParse->pRename; (*pp); pp=&(*pp)->pNext){
    if( (*pp)->p==pPtr ){
      RenameToken *pToken = *pp;
      *pp = pToken->pNext;
      pToken->pNext = pCtx->pList;
      pCtx->pList = pToken;
      pCtx->nList++;
Changes to src/resolve.c.
432
433
434
435
436
437
438



439
440
441
442
443
444
445
            sqlite3ErrorMsg(pParse, "row value misused");
            return WRC_Abort;
          }
          resolveAlias(pParse, pEList, j, pExpr, "", nSubquery);
          cnt = 1;
          pMatch = 0;
          assert( zTab==0 && zDb==0 );



          goto lookupname_end;
        }
      } 
    }

    /* Advance to the next name context.  The loop will exit when either
    ** we have a match (cnt>0) or when we run out of name contexts.







>
>
>







432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
            sqlite3ErrorMsg(pParse, "row value misused");
            return WRC_Abort;
          }
          resolveAlias(pParse, pEList, j, pExpr, "", nSubquery);
          cnt = 1;
          pMatch = 0;
          assert( zTab==0 && zDb==0 );
          if( IN_RENAME_COLUMN ){
            sqlite3RenameTokenRemap(pParse, 0, (void*)pExpr);
          }
          goto lookupname_end;
        }
      } 
    }

    /* Advance to the next name context.  The loop will exit when either
    ** we have a match (cnt>0) or when we run out of name contexts.
Changes to test/altercol.test.
629
630
631
632
633
634
635
















636
637

do_execsql_test 14.2 {
  SELECT 
  sqlite_rename_column(sql, type, object, db, tbl, icol, znew, bquote)
  FROM ddd;
} {{} {} {}}


















finish_test







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


629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653

do_execsql_test 14.2 {
  SELECT 
  sqlite_rename_column(sql, type, object, db, tbl, icol, znew, bquote)
  FROM ddd;
} {{} {} {}}

#-------------------------------------------------------------------------
#
reset_db
do_execsql_test 15.0 {
  CREATE TABLE xxx(a, b, c);
  SELECT a AS d FROM xxx WHERE d=0;
}

do_execsql_test 15.1 {
  CREATE VIEW vvv AS SELECT a AS d FROM xxx WHERE d=0;
  ALTER TABLE xxx RENAME a TO xyz;
}

do_execsql_test 15.2 {
  SELECT sql FROM sqlite_master WHERE type='view';
} {{CREATE VIEW vvv AS SELECT xyz AS d FROM xxx WHERE d=0}}

finish_test