1
0
mirror of https://github.com/sqlite/sqlite.git synced 2025-08-07 02:42:48 +03:00

Better detection and reporting of errors when initializing from the

sqlite_master table. (CVS 688)

FossilOrigin-Name: c8a7b725b7cec792d2148455a4cecbce9dfebe80
This commit is contained in:
drh
2002-07-19 17:46:38 +00:00
parent b782325617
commit c231172f3f
5 changed files with 53 additions and 31 deletions

View File

@@ -1,5 +1,5 @@
C Version\s2.6.0\sRelease\s2\s(CVS\s687) C Better\sdetection\sand\sreporting\sof\serrors\swhen\sinitializing\sfrom\sthe\nsqlite_master\stable.\s(CVS\s688)
D 2002-07-18T11:10:30 D 2002-07-19T17:46:39
F Makefile.in 6291a33b87d2a395aafd7646ee1ed562c6f2c28c F Makefile.in 6291a33b87d2a395aafd7646ee1ed562c6f2c28c
F Makefile.template 4e11752e0b5c7a043ca50af4296ec562857ba495 F Makefile.template 4e11752e0b5c7a043ca50af4296ec562857ba495
F README a4c0ba11354ef6ba0776b400d057c59da47a4cc0 F README a4c0ba11354ef6ba0776b400d057c59da47a4cc0
@@ -28,7 +28,7 @@ F src/func.c e45cd908b9b723d9b91473d09e12c23f786b3fc2
F src/hash.c 6a6236b89c8c060c65dabd300a1c8ce7c10edb72 F src/hash.c 6a6236b89c8c060c65dabd300a1c8ce7c10edb72
F src/hash.h cd0433998bc1a3759d244e1637fe5a3c13b53bf8 F src/hash.h cd0433998bc1a3759d244e1637fe5a3c13b53bf8
F src/insert.c 9bc794863ea2988a7b8667ef010b3c46b26dba38 F src/insert.c 9bc794863ea2988a7b8667ef010b3c46b26dba38
F src/main.c e22729b9780f2f3a2c69715f53d67666072a63d5 F src/main.c ac96008489c086295c2e252f70e725eefd02131c
F src/md5.c 0ae1f3e2cac92d06fc6246d1b4b8f61a2fe66d3b F src/md5.c 0ae1f3e2cac92d06fc6246d1b4b8f61a2fe66d3b
F src/os.c edb22daad525f49681f41c76683a16c1d39755c7 F src/os.c edb22daad525f49681f41c76683a16c1d39755c7
F src/os.h 5b9a69875c880d1665ae040cbca1f7b9c82198ab F src/os.h 5b9a69875c880d1665ae040cbca1f7b9c82198ab
@@ -41,7 +41,7 @@ F src/select.c a43eabfc2e3e4d67660027f016889935f706deab
F src/shell.c 37a8405aec5740726c4ee18826c1ff5fd2c29b96 F src/shell.c 37a8405aec5740726c4ee18826c1ff5fd2c29b96
F src/shell.tcl 27ecbd63dd88396ad16d81ab44f73e6c0ea9d20e F src/shell.tcl 27ecbd63dd88396ad16d81ab44f73e6c0ea9d20e
F src/sqlite.h.in 75c5bbb066d0faf34424b7d1babf8b44d5b31af2 F src/sqlite.h.in 75c5bbb066d0faf34424b7d1babf8b44d5b31af2
F src/sqliteInt.h 0d0b7b7b2b6829eb5c2f63489b11c44ba966fc75 F src/sqliteInt.h a7a088cd9e69e9dbefe0fdfe411bb6387b50f384
F src/table.c eed2098c9b577aa17f8abe89313a9c4413f57d63 F src/table.c eed2098c9b577aa17f8abe89313a9c4413f57d63
F src/tclsqlite.c c502819c209011659e1bbb428cbac5670cce7f79 F src/tclsqlite.c c502819c209011659e1bbb428cbac5670cce7f79
F src/test1.c 456cb080db85056be723e770435d9509afc3a83a F src/test1.c 456cb080db85056be723e770435d9509afc3a83a
@@ -107,7 +107,7 @@ F test/trigger3.test 7dfe798d7e72c13720394685fe353112e3f31adf
F test/unique.test 572aa791327c1e8d797932263e9d67f176cfdb44 F test/unique.test 572aa791327c1e8d797932263e9d67f176cfdb44
F test/update.test 7ffb062d580a972e7870d0f51d5af3ab9bfeae08 F test/update.test 7ffb062d580a972e7870d0f51d5af3ab9bfeae08
F test/vacuum.test 059871b312eb910bbe49dafde1d01490cc2c6bbe F test/vacuum.test 059871b312eb910bbe49dafde1d01490cc2c6bbe
F test/version.test 7b3084bb0b225304d4f5eb8215973e6daf439bcb F test/version.test 3a4d507ad9a7d4f7e2ce829b7896e399c8372ef1
F test/view.test 3afca084dab44c7a5772d3c6a326adf93ad52050 F test/view.test 3afca084dab44c7a5772d3c6a326adf93ad52050
F test/where.test 1f87bec674bf85d74ac1cc5b2cd3d89be1e87b1d F test/where.test 1f87bec674bf85d74ac1cc5b2cd3d89be1e87b1d
F tool/diffdb.c 7524b1b5df217c20cd0431f6789851a4e0cb191b F tool/diffdb.c 7524b1b5df217c20cd0431f6789851a4e0cb191b
@@ -142,7 +142,7 @@ F www/speed.tcl da8afcc1d3ccc5696cfb388a68982bc3d9f7f00f
F www/sqlite.tcl ae3dcfb077e53833b59d4fcc94d8a12c50a44098 F www/sqlite.tcl ae3dcfb077e53833b59d4fcc94d8a12c50a44098
F www/tclsqlite.tcl 1db15abeb446aad0caf0b95b8b9579720e4ea331 F www/tclsqlite.tcl 1db15abeb446aad0caf0b95b8b9579720e4ea331
F www/vdbe.tcl 2013852c27a02a091d39a766bc87cff329f21218 F www/vdbe.tcl 2013852c27a02a091d39a766bc87cff329f21218
P ee8c2758a222251fed0197d2a364346d05728240 P cc4f824b155138326f1a62066a99c65d0f32fb8b
R 76ddeede69cbf2bb4e711ae5b4e30b2a R c16b27894d2d4cede767f11c98c478c7
U drh U drh
Z 599b54bc5f40ea342f28053ffae009fb Z c2aea7accc0c02c595db5c858e306720

View File

@@ -1 +1 @@
cc4f824b155138326f1a62066a99c65d0f32fb8b c8a7b725b7cec792d2148455a4cecbce9dfebe80

View File

@@ -14,12 +14,22 @@
** other files are for internal use by SQLite and should not be ** other files are for internal use by SQLite and should not be
** accessed by users of the library. ** accessed by users of the library.
** **
** $Id: main.c,v 1.88 2002/07/18 01:27:18 drh Exp $ ** $Id: main.c,v 1.89 2002/07/19 17:46:39 drh Exp $
*/ */
#include "sqliteInt.h" #include "sqliteInt.h"
#include "os.h" #include "os.h"
#include <ctype.h> #include <ctype.h>
/*
** A pointer to this structure is used to communicate information
** from sqliteInit into the sqliteInitCallback.
*/
typedef struct {
sqlite *db; /* The database being initialized */
char **pzErrMsg; /* Error message stored here */
} InitData;
/* /*
** This is the callback routine for the code that initializes the ** This is the callback routine for the code that initializes the
** database. See sqliteInit() below for additional information. ** database. See sqliteInit() below for additional information.
@@ -33,8 +43,9 @@
** argv[4] = "1" for temporary files, "0" for main database ** argv[4] = "1" for temporary files, "0" for main database
** **
*/ */
int sqliteInitCallback(void *pDb, int argc, char **argv, char **azColName){ static
sqlite *db = (sqlite*)pDb; int sqliteInitCallback(void *pInit, int argc, char **argv, char **azColName){
InitData *pData = (InitData*)pInit;
Parse sParse; Parse sParse;
int nErr = 0; int nErr = 0;
@@ -54,11 +65,11 @@ int sqliteInitCallback(void *pDb, int argc, char **argv, char **azColName){
** structures that describe the table, index, or view. ** structures that describe the table, index, or view.
*/ */
memset(&sParse, 0, sizeof(sParse)); memset(&sParse, 0, sizeof(sParse));
sParse.db = db; sParse.db = pData->db;
sParse.initFlag = 1; sParse.initFlag = 1;
sParse.isTemp = argv[4][0] - '0'; sParse.isTemp = argv[4][0] - '0';
sParse.newTnum = atoi(argv[2]); sParse.newTnum = atoi(argv[2]);
sqliteRunParser(&sParse, argv[3], 0); sqliteRunParser(&sParse, argv[3], pData->pzErrMsg);
}else{ }else{
/* If the SQL column is blank it means this is an index that /* If the SQL column is blank it means this is an index that
** was created to be the PRIMARY KEY or to fulfill a UNIQUE ** was created to be the PRIMARY KEY or to fulfill a UNIQUE
@@ -66,7 +77,7 @@ int sqliteInitCallback(void *pDb, int argc, char **argv, char **azColName){
** been created when we processed the CREATE TABLE. All we have ** been created when we processed the CREATE TABLE. All we have
** to do here is record the root page number for that index. ** to do here is record the root page number for that index.
*/ */
Index *pIndex = sqliteFindIndex(db, argv[1]); Index *pIndex = sqliteFindIndex(pData->db, argv[1]);
if( pIndex==0 || pIndex->tnum!=0 ){ if( pIndex==0 || pIndex->tnum!=0 ){
/* This can occur if there exists an index on a TEMP table which /* This can occur if there exists an index on a TEMP table which
** has the same name as another index on a permanent index. Since ** has the same name as another index on a permanent index. Since
@@ -98,24 +109,29 @@ int sqliteInitCallback(void *pDb, int argc, char **argv, char **azColName){
** this routine relys on the fact that no indices are used when ** this routine relys on the fact that no indices are used when
** copying a table out to a temporary file. ** copying a table out to a temporary file.
*/ */
static int static
upgrade_3_callback(void *pDb, int argc, char **argv, char **NotUsed){ int upgrade_3_callback(void *pInit, int argc, char **argv, char **NotUsed){
sqlite *db = (sqlite*)pDb; InitData *pData = (InitData*)pInit;
int rc; int rc;
Table *pTab; Table *pTab;
Trigger *pTrig; Trigger *pTrig;
char *zErr;
pTab = sqliteFindTable(db, argv[0]); pTab = sqliteFindTable(pData->db, argv[0]);
if( pTab ){ if( pTab ){
pTrig = pTab->pTrigger; pTrig = pTab->pTrigger;
pTab->pTrigger = 0; /* Disable all triggers before rebuilding the table */ pTab->pTrigger = 0; /* Disable all triggers before rebuilding the table */
} }
rc = sqlite_exec_printf(db, rc = sqlite_exec_printf(pData->db,
"CREATE TEMP TABLE sqlite_x AS SELECT * FROM '%q'; " "CREATE TEMP TABLE sqlite_x AS SELECT * FROM '%q'; "
"DELETE FROM '%q'; " "DELETE FROM '%q'; "
"INSERT INTO '%q' SELECT * FROM sqlite_x; " "INSERT INTO '%q' SELECT * FROM sqlite_x; "
"DROP TABLE sqlite_x;", "DROP TABLE sqlite_x;",
0, 0, 0, argv[0], argv[0], argv[0]); 0, 0, &zErr, argv[0], argv[0], argv[0]);
if( zErr ){
sqliteSetString(pData->pzErrMsg, zErr, 0);
sqlite_freemem(zErr);
}
if( pTab ) pTab->pTrigger = pTrig; /* Re-enable triggers */ if( pTab ) pTab->pTrigger = pTrig; /* Re-enable triggers */
return rc!=SQLITE_OK; return rc!=SQLITE_OK;
} }
@@ -142,6 +158,7 @@ int sqliteInit(sqlite *db, char **pzErrMsg){
char *azArg[6]; char *azArg[6];
int meta[SQLITE_N_BTREE_META]; int meta[SQLITE_N_BTREE_META];
Parse sParse; Parse sParse;
InitData initData;
/* /*
** The master database table has a structure like this ** The master database table has a structure like this
@@ -201,7 +218,9 @@ int sqliteInit(sqlite *db, char **pzErrMsg){
azArg[3] = master_schema; azArg[3] = master_schema;
azArg[4] = "0"; azArg[4] = "0";
azArg[5] = 0; azArg[5] = 0;
sqliteInitCallback(db, 5, azArg, 0); initData.db = db;
initData.pzErrMsg = pzErrMsg;
sqliteInitCallback(&initData, 5, azArg, 0);
pTab = sqliteFindTable(db, MASTER_NAME); pTab = sqliteFindTable(db, MASTER_NAME);
if( pTab ){ if( pTab ){
pTab->readOnly = 1; pTab->readOnly = 1;
@@ -209,7 +228,7 @@ int sqliteInit(sqlite *db, char **pzErrMsg){
azArg[1] = TEMP_MASTER_NAME; azArg[1] = TEMP_MASTER_NAME;
azArg[3] = temp_master_schema; azArg[3] = temp_master_schema;
azArg[4] = "1"; azArg[4] = "1";
sqliteInitCallback(db, 5, azArg, 0); sqliteInitCallback(&initData, 5, azArg, 0);
pTab = sqliteFindTable(db, TEMP_MASTER_NAME); pTab = sqliteFindTable(db, TEMP_MASTER_NAME);
if( pTab ){ if( pTab ){
pTab->readOnly = 1; pTab->readOnly = 1;
@@ -258,7 +277,7 @@ int sqliteInit(sqlite *db, char **pzErrMsg){
sParse.db = db; sParse.db = db;
sParse.pBe = db->pBe; sParse.pBe = db->pBe;
sParse.xCallback = sqliteInitCallback; sParse.xCallback = sqliteInitCallback;
sParse.pArg = (void*)db; sParse.pArg = (void*)&initData;
sParse.initFlag = 1; sParse.initFlag = 1;
sqliteRunParser(&sParse, sqliteRunParser(&sParse,
db->file_format>=2 ? init_script : older_init_script, db->file_format>=2 ? init_script : older_init_script,
@@ -355,14 +374,17 @@ sqlite *sqlite_open(const char *zFilename, int mode, char **pzErrMsg){
** is read only, interrupt receive, etc.) then refuse to open. ** is read only, interrupt receive, etc.) then refuse to open.
*/ */
if( db->file_format<3 ){ if( db->file_format<3 ){
char *zErr; char *zErr = 0;
InitData initData;
int meta[SQLITE_N_BTREE_META]; int meta[SQLITE_N_BTREE_META];
initData.db = db;
initData.pzErrMsg = &zErr;
db->file_format = 3; db->file_format = 3;
rc = sqlite_exec(db, rc = sqlite_exec(db,
"BEGIN; SELECT name FROM sqlite_master WHERE type='table';", "BEGIN; SELECT name FROM sqlite_master WHERE type='table';",
upgrade_3_callback, upgrade_3_callback,
db, &initData,
&zErr); &zErr);
if( rc==SQLITE_OK ){ if( rc==SQLITE_OK ){
sqliteBtreeGetMeta(db->pBe, meta); sqliteBtreeGetMeta(db->pBe, meta);
@@ -374,11 +396,12 @@ sqlite *sqlite_open(const char *zFilename, int mode, char **pzErrMsg){
sqliteSetString(pzErrMsg, sqliteSetString(pzErrMsg,
"unable to upgrade database to the version 2.6 format", "unable to upgrade database to the version 2.6 format",
zErr ? ": " : 0, zErr, 0); zErr ? ": " : 0, zErr, 0);
sqliteFree(zErr); sqlite_freemem(zErr);
sqliteStrRealloc(pzErrMsg); sqliteStrRealloc(pzErrMsg);
sqlite_close(db); sqlite_close(db);
return 0; return 0;
} }
sqlite_freemem(zErr);
} }
/* Return a pointer to the newly opened database structure */ /* Return a pointer to the newly opened database structure */

View File

@@ -11,7 +11,7 @@
************************************************************************* *************************************************************************
** Internal interface definitions for SQLite. ** Internal interface definitions for SQLite.
** **
** @(#) $Id: sqliteInt.h,v 1.139 2002/07/18 00:34:12 drh Exp $ ** @(#) $Id: sqliteInt.h,v 1.140 2002/07/19 17:46:39 drh Exp $
*/ */
#include "sqlite.h" #include "sqlite.h"
#include "hash.h" #include "hash.h"
@@ -949,4 +949,3 @@ TriggerStep *sqliteTriggerUpdateStep(Token*, ExprList*, Expr*, int);
TriggerStep *sqliteTriggerDeleteStep(Token*, Expr*); TriggerStep *sqliteTriggerDeleteStep(Token*, Expr*);
void sqliteDeleteTrigger(Trigger*); void sqliteDeleteTrigger(Trigger*);
int sqliteJoinType(Parse*, Token*, Token*, Token*); int sqliteJoinType(Parse*, Token*, Token*, Token*);
int sqliteInitCallback(void*,int,char**,char**);

View File

@@ -12,7 +12,7 @@
# focus of this file is testing the ability of the library to detect # focus of this file is testing the ability of the library to detect
# past or future file format version numbers and respond appropriately. # past or future file format version numbers and respond appropriately.
# #
# $Id: version.test,v 1.2 2002/07/18 02:07:08 drh Exp $ # $Id: version.test,v 1.3 2002/07/19 17:46:41 drh Exp $
set testdir [file dirname $argv0] set testdir [file dirname $argv0]
source $testdir/tester.tcl source $testdir/tester.tcl
@@ -177,6 +177,6 @@ do_test version-2.1 {
catch {file attributes test.db -readonly 1} catch {file attributes test.db -readonly 1}
set rc [catch {sqlite db test.db} msg] set rc [catch {sqlite db test.db} msg]
lappend rc $msg lappend rc $msg
} {1 {unable to upgrade database to the version 2.6 format}} } {1 {unable to upgrade database to the version 2.6 format: attempt to write a readonly database}}
finish_test finish_test