1
0
mirror of https://github.com/sqlite/sqlite.git synced 2025-11-14 00:22:38 +03:00

Fix bugs in ALTER TABLE related to (a) whitespace in table defn, (b) temp triggers. (CVS 2112)

FossilOrigin-Name: 1fd8e835a3656799c23f4ef6ea1311fecf5a15cb
This commit is contained in:
danielk1977
2004-11-19 05:14:54 +00:00
parent d641d646ff
commit 343e92610e
5 changed files with 188 additions and 32 deletions

View File

@@ -22,7 +22,7 @@
** COMMIT
** ROLLBACK
**
** $Id: build.c,v 1.280 2004/11/18 15:44:29 danielk1977 Exp $
** $Id: build.c,v 1.281 2004/11/19 05:14:55 danielk1977 Exp $
*/
#include "sqliteInt.h"
#include <ctype.h>
@@ -2931,6 +2931,9 @@ void sqlite3AlterRenameTable(
char *zWhere = 0; /* Where clause of schema elements to reparse */
sqlite3 *db = pParse->db; /* Database connection */
Vdbe *v;
#ifndef SQLITE_OMIT_TRIGGER
char *zTempTrig = 0; /* Where clause to locate temp triggers */
#endif
assert( pSrc->nSrc==1 );
@@ -2994,6 +2997,39 @@ void sqlite3AlterRenameTable(
zName, strlen(pTab->zName), pTab->zName
);
#ifndef SQLITE_OMIT_TRIGGER
/* If there are TEMP triggers on this table, modify the sqlite_temp_master
** table. Don't do this if the table being ALTERed is itself located in
** the temp database.
*/
if( iDb!=1 ){
Trigger *pTrig;
char *tmp = 0;
for( pTrig=pTab->pTrigger; pTrig; pTrig=pTrig->pNext ){
if( pTrig->iDb==1 ){
if( !zTempTrig ){
zTempTrig =
sqlite3MPrintf("type = 'trigger' AND name IN(%Q", pTrig->name);
}else{
tmp = zTempTrig;
zTempTrig = sqlite3MPrintf("%s, %Q", zTempTrig, pTrig->name);
sqliteFree(tmp);
}
}
}
if( zTempTrig ){
tmp = zTempTrig;
zTempTrig = sqlite3MPrintf("%s)", zTempTrig);
sqliteFree(tmp);
sqlite3NestedParse(pParse,
"UPDATE sqlite_temp_master SET "
"sql = sqlite_alter_trigger(sql, %Q), "
"tbl_name = %Q "
"WHERE %s;", zName, zName, zTempTrig);
}
}
#endif
/* Drop the elements of the in-memory schema that refered to the table
** renamed and load the new versions from the database.
*/
@@ -3001,12 +3037,20 @@ void sqlite3AlterRenameTable(
#ifndef SQLITE_OMIT_TRIGGER
Trigger *pTrig;
for( pTrig=pTab->pTrigger; pTrig; pTrig=pTrig->pNext ){
sqlite3VdbeOp3(v, OP_DropTrigger, iDb, 0, pTrig->name, 0);
assert( pTrig->iDb==iDb || pTrig->iDb==1 );
sqlite3VdbeOp3(v, OP_DropTrigger, pTrig->iDb, 0, pTrig->name, 0);
}
#endif
sqlite3VdbeOp3(v, OP_DropTable, iDb, 0, pTab->zName, 0);
zWhere = sqlite3MPrintf("tbl_name=%Q", zName);
sqlite3VdbeOp3(v, OP_ParseSchema, iDb, 0, zWhere, P3_DYNAMIC);
#ifndef SQLITE_OMIT_TRIGGER
if( zTempTrig ){
sqlite3VdbeOp3(v, OP_ParseSchema, 1, 0, zTempTrig, P3_DYNAMIC);
}
}else{
sqliteFree(zTempTrig);
#endif
}
sqliteFree(zName);

View File

@@ -16,7 +16,7 @@
** sqliteRegisterBuildinFunctions() found at the bottom of the file.
** All other code has file scope.
**
** $Id: func.c,v 1.90 2004/11/18 15:44:29 danielk1977 Exp $
** $Id: func.c,v 1.91 2004/11/19 05:14:55 danielk1977 Exp $
*/
#include <ctype.h>
#include <math.h>
@@ -552,26 +552,38 @@ static void altertableFunc(
int argc,
sqlite3_value **argv
){
char const *zSql = sqlite3_value_text(argv[0]);
char const *zTableName = sqlite3_value_text(argv[1]);
unsigned char const *zSql = sqlite3_value_text(argv[0]);
unsigned char const *zTableName = sqlite3_value_text(argv[1]);
int token;
Token tname;
char const *zCsr = zSql;
char const *zPrev;
char *zRet = 0;
int tokenType = 0;
int len;
int len = 0;
char *zRet;
assert( argc==2 );
/* The principle used to locate the table name in the CREATE TABLE
** statement is that the table name is the first token that is immediatedly
** followed by a left parenthesis - TK_LP.
*/
if( zSql ){
while( tokenType!=TK_LP ){
zPrev = zCsr-len;
len = sqlite3GetToken(zCsr, &tokenType);
zCsr += len;
}
do {
/* Store the token that zCsr points to in tname. */
tname.z = zCsr;
tname.n = len;
zRet = sqlite3MPrintf("%.*s%Q(%s", zPrev-zSql, zSql, zTableName, zCsr);
sqlite3_result_text(context, zRet, -1, SQLITE_TRANSIENT);
sqliteFree(zRet);
/* Advance zCsr to the next token. Store that token type in 'token',
** and it's length in 'len' (to be used next iteration of this loop).
*/
do {
zCsr += len;
len = sqlite3GetToken(zCsr, &token);
} while( token==TK_SPACE );
assert( len>0 );
} while( token!=TK_LP );
zRet = sqlite3MPrintf("%.*s%Q%s", tname.z - zSql, zSql,
zTableName, tname.z+tname.n);
sqlite3_result_text(context, zRet, -1, sqlite3FreeX);
}
}
#endif
@@ -605,7 +617,6 @@ static void altertriggerFunc(
** preceded by either TK_ON or TK_DOT and immediatedly followed by one
** of TK_WHEN, TK_BEGIN or TK_FOR.
*/
assert( argc==2 );
if( zSql ){
do {
/* Store the token that zCsr points to in tname. */
@@ -641,8 +652,7 @@ static void altertriggerFunc(
*/
zRet = sqlite3MPrintf("%.*s%Q%s", tname.z - zSql, zSql,
zTableName, tname.z+tname.n);
sqlite3_result_text(context, zRet, -1, SQLITE_TRANSIENT);
sqliteFree(zRet);
sqlite3_result_text(context, zRet, -1, sqlite3FreeX);
}
}
#endif /* !SQLITE_OMIT_TRIGGER */