From 0388123f077d4d8578720f6e69b1c5bfd1b5eb3f Mon Sep 17 00:00:00 2001 From: drh Date: Fri, 13 Feb 2009 03:43:31 +0000 Subject: [PATCH] Correctly handle attempts to add a UNIQUE or PRIMARY KEY column using the ALTER TABLE statement. Ticket #3651. (CVS 6291) FossilOrigin-Name: dd179ff2986bc2a86d70bbe927fd0e123e17d398 --- manifest | 16 ++++++++-------- manifest.uuid | 2 +- src/alter.c | 12 ++++++++---- src/build.c | 9 +++++---- test/alter.test | 16 +++++++++++++++- 5 files changed, 37 insertions(+), 18 deletions(-) diff --git a/manifest b/manifest index 6564d7253c..2dc9212814 100644 --- a/manifest +++ b/manifest @@ -1,5 +1,5 @@ -C Add\sadditional\scross-references\sfrom\sAPI\sdocumentation\sto\sother\sdocuments.\nComment\schanges\sonly;\sno\schanges\sto\scode.\s(CVS\s6290) -D 2009-02-12T17:07:35 +C Correctly\shandle\sattempts\sto\sadd\sa\sUNIQUE\sor\sPRIMARY\sKEY\scolumn\susing\nthe\sALTER\sTABLE\sstatement.\s\sTicket\s#3651.\s(CVS\s6291) +D 2009-02-13T03:43:32 F Makefile.arm-wince-mingw32ce-gcc fcd5e9cd67fe88836360bb4f9ef4cb7f8e2fb5a0 F Makefile.in c7a5a30fb6852bd7839b1024e1661da8549878ee F Makefile.linux-gcc d53183f4aa6a9192d249731c90dbdffbd2c68654 @@ -97,7 +97,7 @@ F sqlite.pc.in 42b7bf0d02e08b9e77734a47798d1a55a9e0716b F sqlite3.1 6be1ad09113570e1fc8dcaff84c9b0b337db5ffc F sqlite3.def a1be7b9a4b8b51ac41c6ff6e8e44a14ef66b338b F sqlite3.pc.in ae6f59a76e862f5c561eb32a380228a02afc3cad -F src/alter.c 0ec29744c36c6e976596ce38c16289ebc5dc94db +F src/alter.c f93d13aae63ea1a5ee604d76041be354311d08a5 F src/analyze.c c86fd6a1425b22b3a46ce72ad403e4280026364f F src/attach.c 81d37d1948f409146a7b22b96998fd90649d1fd3 F src/auth.c c8b2ab5c8bad4bd90ed7c294694f48269162c627 @@ -107,7 +107,7 @@ F src/btmutex.c 63c5cc4ad5715690767ffcb741e185d7bc35ec1a F src/btree.c 71f30e74389aa7ae51421592dfaf69511152677c F src/btree.h 4eab72af6adf95f0b08b61a72ef9781bdb0bf63f F src/btreeInt.h 0a4884e6152d7cae9c741e91b830064c19fd2c05 -F src/build.c 909c43d7f283ba7fb186d144ccb2a479f3707d13 +F src/build.c a394b2511c5c768f14a9d7c1c31606b9fa569f1b F src/callback.c 5f10bca853e59a2c272bbfd5b720303f8b69e520 F src/complete.c cb14e06dbe79dee031031f0d9e686ff306afe07c F src/date.c 0d804df3bbda46329946a01ff5c75c3f4f135218 @@ -213,7 +213,7 @@ F src/where.c 63bb752784b352d8e226dc6b1ec02f0d19fed916 F test/aggerror.test a867e273ef9e3d7919f03ef4f0e8c0d2767944f2 F test/alias.test 597662c5d777a122f9a3df0047ea5c5bd383a911 F test/all.test 14165b3e32715b700b5f0cbf8f6e3833dda0be45 -F test/alter.test 6353aae6839e486c9b7d8f73b1f4a1e98e57332c +F test/alter.test 958b36b7e9752e8a4ed03ba34690b06e48b0e3e0 F test/alter2.test dd55146e812622c8fc51fd2216bcd8dca8880752 F test/alter3.test 25b95a136708f22b87184fa6a4309eea03d65153 F test/alter4.test 9386ffd1e9c7245f43eca412b2058d747509cc1f @@ -701,7 +701,7 @@ F tool/speedtest16.c c8a9c793df96db7e4933f0852abb7a03d48f2e81 F tool/speedtest2.tcl ee2149167303ba8e95af97873c575c3e0fab58ff F tool/speedtest8.c 2902c46588c40b55661e471d7a86e4dd71a18224 F tool/speedtest8inst1.c 293327bc76823f473684d589a8160bde1f52c14e -P 8c4d71a169e529964d2d0cfba82bbad66a0bcd12 -R 53fec158efaff003665a1b8df63b995c +P 97203a0ad7a7ddfae04daf99558389b0589fc170 +R c3e34896fab7ddb1e1c67f27275cda31 U drh -Z b5957726d587bc15852bcb1567bffe4d +Z d2ef14fc652ee88c4cbac35aebdcf995 diff --git a/manifest.uuid b/manifest.uuid index e41e4fc28c..524e384ddc 100644 --- a/manifest.uuid +++ b/manifest.uuid @@ -1 +1 @@ -97203a0ad7a7ddfae04daf99558389b0589fc170 \ No newline at end of file +dd179ff2986bc2a86d70bbe927fd0e123e17d398 \ No newline at end of file diff --git a/src/alter.c b/src/alter.c index f7c60266d5..4974158334 100644 --- a/src/alter.c +++ b/src/alter.c @@ -12,7 +12,7 @@ ** This file contains C code routines that used to generate VDBE code ** that implements the ALTER TABLE command. ** -** $Id: alter.c,v 1.52 2009/01/20 16:53:40 danielk1977 Exp $ +** $Id: alter.c,v 1.53 2009/02/13 03:43:32 drh Exp $ */ #include "sqliteInt.h" @@ -453,7 +453,7 @@ void sqlite3AlterFinishAddColumn(Parse *pParse, Token *pColDef){ assert( sqlite3BtreeHoldsAllMutexes(db) ); iDb = sqlite3SchemaToIndex(db, pNew->pSchema); zDb = db->aDb[iDb].zName; - zTab = pNew->zName; + zTab = &pNew->zName[16]; /* Skip the "sqlite_altertab_" prefix on the name */ pCol = &pNew->aCol[pNew->nCol-1]; pDflt = pCol->pDflt; pTab = sqlite3FindTable(db, zTab, zDb); @@ -583,7 +583,11 @@ void sqlite3AlterBeginAddColumn(Parse *pParse, SrcList *pSrc){ iDb = sqlite3SchemaToIndex(db, pTab->pSchema); /* Put a copy of the Table struct in Parse.pNewTable for the - ** sqlite3AddColumn() function and friends to modify. + ** sqlite3AddColumn() function and friends to modify. But modify + ** the name by adding an "sqlite_altertab_" prefix. By adding this + ** prefix, we insure that the name will not collide with an existing + ** table because user table are not allowed to have the "sqlite_" + ** prefix on their name. */ pNew = (Table*)sqlite3DbMallocZero(db, sizeof(Table)); if( !pNew ) goto exit_begin_add_column; @@ -595,7 +599,7 @@ void sqlite3AlterBeginAddColumn(Parse *pParse, SrcList *pSrc){ nAlloc = (((pNew->nCol-1)/8)*8)+8; assert( nAlloc>=pNew->nCol && nAlloc%8==0 && nAlloc-pNew->nCol<8 ); pNew->aCol = (Column*)sqlite3DbMallocZero(db, sizeof(Column)*nAlloc); - pNew->zName = sqlite3DbStrDup(db, pTab->zName); + pNew->zName = sqlite3MPrintf(db, "sqlite_altertab_%s", pTab->zName); if( !pNew->aCol || !pNew->zName ){ db->mallocFailed = 1; goto exit_begin_add_column; diff --git a/src/build.c b/src/build.c index c3cd213332..851071356f 100644 --- a/src/build.c +++ b/src/build.c @@ -22,7 +22,7 @@ ** COMMIT ** ROLLBACK ** -** $Id: build.c,v 1.517 2009/02/10 10:44:42 danielk1977 Exp $ +** $Id: build.c,v 1.518 2009/02/13 03:43:32 drh Exp $ */ #include "sqliteInt.h" @@ -365,7 +365,7 @@ static void freeIndex(Index *p){ ** it is not unlinked from the Table that it indexes. ** Unlinking from the Table must be done by the calling function. */ -static void sqliteDeleteIndex(Index *p){ +static void sqlite3DeleteIndex(Index *p){ Index *pOld; const char *zName = p->zName; @@ -525,7 +525,7 @@ void sqlite3DeleteTable(Table *pTable){ for(pIndex = pTable->pIndex; pIndex; pIndex=pNext){ pNext = pIndex->pNext; assert( pIndex->pSchema==pTable->pSchema ); - sqliteDeleteIndex(pIndex); + sqlite3DeleteIndex(pIndex); } #ifndef SQLITE_OMIT_FOREIGN_KEY @@ -2428,7 +2428,8 @@ void sqlite3CreateIndex( pDb = &db->aDb[iDb]; if( pTab==0 || pParse->nErr ) goto exit_create_index; - if( sqlite3StrNICmp(pTab->zName, "sqlite_", 7)==0 ){ + if( sqlite3StrNICmp(pTab->zName, "sqlite_", 7)==0 + && memcmp(&pTab->zName[7],"altertab_",9)!=0 ){ sqlite3ErrorMsg(pParse, "table %s may not be indexed", pTab->zName); goto exit_create_index; } diff --git a/test/alter.test b/test/alter.test index 002fb5a5a8..2f66108287 100644 --- a/test/alter.test +++ b/test/alter.test @@ -11,7 +11,7 @@ # This file implements regression tests for SQLite library. The # focus of this script is testing the ALTER TABLE statement. # -# $Id: alter.test,v 1.30 2008/05/09 14:17:52 drh Exp $ +# $Id: alter.test,v 1.31 2009/02/13 03:43:32 drh Exp $ # set testdir [file dirname $argv0] @@ -811,4 +811,18 @@ do_test alter-13.3 { } } {t3102a_rename t3102b_rename t3102c} +# Ticket #3651 +do_test alter-14.1 { + catchsql { + CREATE TABLE t3651(a UNIQUE); + ALTER TABLE t3651 ADD COLUMN b UNIQUE; + } +} {1 {Cannot add a UNIQUE column}} +do_test alter-14.2 { + catchsql { + ALTER TABLE t3651 ADD COLUMN b PRIMARY KEY; + } +} {1 {Cannot add a PRIMARY KEY column}} + + finish_test -- 2.47.2