aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authordrh <drh@noemail.net>2009-02-13 03:43:31 +0000
committerdrh <drh@noemail.net>2009-02-13 03:43:31 +0000
commit0388123f077d4d8578720f6e69b1c5bfd1b5eb3f (patch)
tree1e96ff2672511fc65055dbe1b469d4d08059795f /src
parentaff46970a759ce0a809b2bcd3b571a3c7b09ca9f (diff)
downloadsqlite-0388123f077d4d8578720f6e69b1c5bfd1b5eb3f.tar.gz
sqlite-0388123f077d4d8578720f6e69b1c5bfd1b5eb3f.zip
Correctly handle attempts to add a UNIQUE or PRIMARY KEY column using
the ALTER TABLE statement. Ticket #3651. (CVS 6291) FossilOrigin-Name: dd179ff2986bc2a86d70bbe927fd0e123e17d398
Diffstat (limited to 'src')
-rw-r--r--src/alter.c12
-rw-r--r--src/build.c9
2 files changed, 13 insertions, 8 deletions
diff --git a/src/alter.c b/src/alter.c
index f7c60266d..497415833 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 c3cd21333..851071356 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;
}