]> git.ipfire.org Git - thirdparty/sqlite.git/commitdiff
Correctly handle attempts to add a UNIQUE or PRIMARY KEY column using
authordrh <drh@noemail.net>
Fri, 13 Feb 2009 03:43:31 +0000 (03:43 +0000)
committerdrh <drh@noemail.net>
Fri, 13 Feb 2009 03:43:31 +0000 (03:43 +0000)
the ALTER TABLE statement.  Ticket #3651. (CVS 6291)

FossilOrigin-Name: dd179ff2986bc2a86d70bbe927fd0e123e17d398

manifest
manifest.uuid
src/alter.c
src/build.c
test/alter.test

index 6564d7253ca4bc1b7e8c2f4951a5c08f51eace49..2dc92128146ec51346b073342710af90bcd0e621 100644 (file)
--- 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
index e41e4fc28cc278da5f484c86f83362d263dcf17e..524e384ddcf9d594832bf818af4b14b800e78c9e 100644 (file)
@@ -1 +1 @@
-97203a0ad7a7ddfae04daf99558389b0589fc170
\ No newline at end of file
+dd179ff2986bc2a86d70bbe927fd0e123e17d398
\ No newline at end of file
index f7c60266d5bdd41cb3bdc41521aa4a2280123a5c..49741583341d40e3a584a262bf92e5f7c7be2cee 100644 (file)
@@ -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;
index c3cd213332331095d087682ac557b4bd4e543f7a..851071356fc75d3071fa160884cd7cccc3c2f889 100644 (file)
@@ -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;
   }
index 002fb5a5a8480c6e33818d9f270e87496465f8dd..2f661082871a133d1494eb0e57c8803eab06602b 100644 (file)
@@ -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