source $testdir/malloc_common.tcl
set ::testprefix otafault
-do_test 1.1 {
- forcedelete ota.db
- execsql {
- PRAGMA encoding = utf16;
+proc copy_if_exists {src target} {
+ if {[file exists $src]} {
+ forcecopy $src $target
+ } else {
+ forcedelete $target
+ }
+}
+
+foreach {tn2 setup sql expect} {
+ 1 {
CREATE TABLE t1(a INTEGER PRIMARY KEY, b, c);
CREATE INDEX t1cb ON t1(c, b);
INSERT INTO t1 VALUES(1, 1, 1);
INSERT INTO t1 VALUES(2, 2, 2);
INSERT INTO t1 VALUES(3, 3, 3);
- CREATE TABLE t2(a PRIMARY KEY, b, c) WITHOUT ROWID;
- CREATE INDEX t2cb ON t1(c, b);
- INSERT INTO t2 VALUES('a', 'a', 'a');
- INSERT INTO t2 VALUES('b', 'b', 'b');
- INSERT INTO t2 VALUES('c', 'c', 'c');
- ATTACH 'ota.db' AS ota;
CREATE TABLE ota.data_t1(a, b, c, ota_control);
- CREATE TABLE ota.data_t2(a, b, c, ota_control);
-
INSERT INTO data_t1 VALUES(2, NULL, NULL, 1);
INSERT INTO data_t1 VALUES(3, 'three', NULL, '.x.');
INSERT INTO data_t1 VALUES(4, 4, 4, 0);
+ } {SELECT * FROM t1} {1 1 1 3 three 3 4 4 4}
+
+ 2 {
+ CREATE TABLE t2(a PRIMARY KEY, b, c) WITHOUT ROWID;
+ CREATE INDEX t2cb ON t2(c, b);
+ INSERT INTO t2 VALUES('a', 'a', 'a');
+ INSERT INTO t2 VALUES('b', 'b', 'b');
+ INSERT INTO t2 VALUES('c', 'c', 'c');
+ CREATE TABLE ota.data_t2(a, b, c, ota_control);
INSERT INTO data_t2 VALUES('b', NULL, NULL, 1);
INSERT INTO data_t2 VALUES('c', 'see', NULL, '.x.');
INSERT INTO data_t2 VALUES('d', 'd', 'd', 0);
+ } {SELECT * FROM t2} {a a a c see c d d d}
+
+ 3 {
+ CREATE TABLE t1(a INTEGER PRIMARY KEY, b, c);
+ CREATE TABLE t2(a PRIMARY KEY, b, c) WITHOUT ROWID;
+ CREATE INDEX t1cb ON t1(c, b);
+ CREATE INDEX t2cb ON t2(c, b);
+
+ CREATE TABLE ota.data_t1(a, b, c, ota_control);
+ CREATE TABLE ota.data_t2(a, b, c, ota_control);
+ INSERT INTO data_t1 VALUES(1, 2, 3, 0);
+ INSERT INTO data_t2 VALUES(4, 5, 6, 0);
+ } {SELECT * FROM t1 UNION ALL SELECT * FROM t2} {1 2 3 4 5 6}
+
+} {
+ catch {db close}
+ forcedelete ota.db test.db
+ sqlite3 db test.db
+ execsql {
+ PRAGMA encoding = utf16;
+ ATTACH 'ota.db' AS ota;
}
+ execsql $setup
db close
forcecopy test.db test.db.bak
forcecopy ota.db ota.db.bak
-} {}
-
-sqlite3_shutdown
-set lookaside_config [sqlite3_config_lookaside 0 0]
-sqlite3_initialize
-autoinstall_test_functions
-
-foreach {tn f reslist} {
- 1 oom-tra* {
- {0 SQLITE_DONE}
- {1 {SQLITE_NOMEM - out of memory}}
- {1 SQLITE_NOMEM}
- {1 SQLITE_IOERR_NOMEM}
- {1 {SQLITE_NOMEM - unable to open a temporary database file for storing temporary tables}}
- }
- 2 ioerr-* {
- {0 SQLITE_DONE}
- {1 {SQLITE_IOERR - disk I/O error}}
- {1 SQLITE_IOERR}
- {1 SQLITE_IOERR_WRITE}
- {1 SQLITE_IOERR_READ}
- {1 SQLITE_IOERR_FSYNC}
- {1 {SQLITE_ERROR - SQL logic error or missing database}}
- {1 {SQLITE_ERROR - unable to open database: ota.db}}
- {1 {SQLITE_IOERR - unable to open database: ota.db}}
- }
-} {
- do_faultsim_test 2 -faults $::f -prep {
- catch { db close }
- forcedelete test.db-journal test.db-wal ota.db-journal ota.db-wal
- forcecopy test.db.bak test.db
- forcecopy ota.db.bak ota.db
- } -body {
- sqlite3ota ota test.db ota.db
- while {[ota step]=="SQLITE_OK"} {}
- ota close
- } -test {
- faultsim_test_result {*}$::reslist
- if {$testrc==0} {
- sqlite3 db test.db
- faultsim_integrity_check
- set res [db eval {
- SELECT * FROM t1 UNION ALL SELECT * FROM t2;
- }]
- set expected [list {*}{
- 1 1 1 3 three 3 4 4 4
- a a a c see c d d d
- }]
+
+ foreach {tn f reslist} {
+ 1 oom-tra* {
+ {0 SQLITE_DONE}
+ {1 {SQLITE_NOMEM - out of memory}}
+ {1 SQLITE_NOMEM}
+ {1 SQLITE_IOERR_NOMEM}
+ {1 {SQLITE_NOMEM - unable to open a temporary database file for storing temporary tables}}
+ }
- if {$res != $expected} {
- puts ""
- puts "res: $res"
- puts "exp: $expected"
- error "data not as expected!"
+ 2 ioerr-* {
+ {0 SQLITE_DONE}
+ {1 {SQLITE_IOERR - disk I/O error}}
+ {1 SQLITE_IOERR}
+ {1 SQLITE_IOERR_WRITE}
+ {1 SQLITE_IOERR_READ}
+ {1 SQLITE_IOERR_FSYNC}
+ {1 {SQLITE_ERROR - SQL logic error or missing database}}
+ {1 {SQLITE_ERROR - unable to open database: ota.db}}
+ {1 {SQLITE_IOERR - unable to open database: ota.db}}
+ }
+ } {
+
+ catch {db close}
+ sqlite3_shutdown
+ set lookaside_config [sqlite3_config_lookaside 0 0]
+ sqlite3_initialize
+ autoinstall_test_functions
+
+ do_faultsim_test 2.$tn2 -faults $::f -prep {
+ catch { db close }
+ forcedelete test.db-journal test.db-wal ota.db-journal ota.db-wal
+ forcecopy test.db.bak test.db
+ forcecopy ota.db.bak ota.db
+ } -body {
+ sqlite3ota ota test.db ota.db
+ while {[ota step]=="SQLITE_OK"} {}
+ ota close
+ } -test {
+ faultsim_test_result {*}$::reslist
+ if {$testrc==0} {
+ sqlite3 db test.db
+ faultsim_integrity_check
+ set res [db eval $::sql]
+ if {$res != [list {*}$::expect]} {
+ puts ""
+ puts "res: $res"
+ puts "exp: $expect"
+ error "data not as expected!"
+ }
}
}
- }
-}
-catch {db close}
-sqlite3_shutdown
-sqlite3_config_lookaside {*}$lookaside_config
-sqlite3_initialize
-autoinstall_test_functions
+ catch {db close}
+ sqlite3_shutdown
+ sqlite3_config_lookaside {*}$lookaside_config
+ sqlite3_initialize
+ autoinstall_test_functions
-proc copy_if_exists {src target} {
- if {[file exists $src]} {
- forcecopy $src $target
- } else {
- forcedelete $target
- }
-}
-
-for {set iStep 0} {$iStep<=21} {incr iStep} {
-
- forcedelete test.db-journal test.db-wal ota.db-journal ota.db-wal
- copy_if_exists test.db.bak test.db
- copy_if_exists ota.db.bak ota.db
-
- sqlite3ota ota test.db ota.db
- for {set x 0} {$x < $::iStep} {incr x} { ota step }
- ota close
-
- copy_if_exists test.db test.db.bak.2
- copy_if_exists test.db-wal test.db.bak.2-wal
- copy_if_exists test.db-oal test.db.bak.2-oal
- copy_if_exists ota.db ota.db.bak.2
+ }
- do_faultsim_test 3.$iStep -faults oom-trans* -prep {
- catch { db close }
+ for {set iStep 0} {$iStep<=21} {incr iStep} {
+
forcedelete test.db-journal test.db-wal ota.db-journal ota.db-wal
- copy_if_exists test.db.bak.2 test.db
- copy_if_exists test.db.bak.2-wal test.db-wal
- copy_if_exists test.db.bak.2-oal test.db-oal
- copy_if_exists ota.db.bak.2 ota.db
- } -body {
+
+ copy_if_exists test.db.bak test.db
+ copy_if_exists ota.db.bak ota.db
+
sqlite3ota ota test.db ota.db
- while {[ota step] == "SQLITE_OK"} {}
+ for {set x 0} {$x < $::iStep} {incr x} { ota step }
ota close
- } -test {
- faultsim_test_result {0 SQLITE_DONE} \
- {1 {SQLITE_NOMEM - out of memory}} \
- {1 SQLITE_NOMEM} \
- {1 SQLITE_IOERR_NOMEM} \
- {1 {SQLITE_NOMEM - unable to open a temporary database file for storing temporary tables}}
- if {$testrc==0} {
- sqlite3 db test.db
- faultsim_integrity_check
- set res [db eval {
- SELECT * FROM t1 UNION ALL SELECT * FROM t2;
- }]
- set expected [list {*}{
- 1 1 1 3 three 3 4 4 4
- a a a c see c d d d
- }]
+ copy_if_exists test.db test.db.bak.2
+ copy_if_exists test.db-wal test.db.bak.2-wal
+ copy_if_exists test.db-oal test.db.bak.2-oal
+ copy_if_exists ota.db ota.db.bak.2
- if {$res != $expected} {
- puts ""
- puts "res: $res"
- puts "exp: $expected"
- error "data not as expected!"
+ do_faultsim_test 3.$tn.$iStep -faults $::f -prep {
+ catch { db close }
+ forcedelete test.db-journal test.db-wal ota.db-journal ota.db-wal
+ copy_if_exists test.db.bak.2 test.db
+ copy_if_exists test.db.bak.2-wal test.db-wal
+ copy_if_exists test.db.bak.2-oal test.db-oal
+ copy_if_exists ota.db.bak.2 ota.db
+ } -body {
+ sqlite3ota ota test.db ota.db
+ while {[ota step] == "SQLITE_OK"} {}
+ ota close
+ } -test {
+ faultsim_test_result {*}$::reslist
+
+ if {$testrc==0} {
+ sqlite3 db test.db
+ faultsim_integrity_check
+ set res [db eval $::sql]
+ if {$res != [list {*}$::expect]} {
+ puts ""
+ puts "res: $res"
+ puts "exp: $expected"
+ error "data not as expected!"
+ }
}
}
}
-
}
finish_test
** return OTA_PK_NONE
** }
*/
-static int otaTableType(
- sqlite3 *db,
+static void otaTableType(
+ sqlite3ota *p,
const char *zTab,
int *peType,
int *piPk
){
- sqlite3_stmt *pStmt = 0;
- int rc = SQLITE_OK;
- int rc2;
- char *zSql = 0;
+ /*
+ ** 0) SELECT count(*) FROM sqlite_master where name=%Q AND IsVirtual(%Q)
+ ** 1) PRAGMA index_list = ?
+ ** 2) SELECT count(*) FROM sqlite_master where name=%Q
+ ** 3) PRAGMA table_info = ?
+ */
+ sqlite3_stmt *aStmt[4] = {0, 0, 0, 0};
*peType = OTA_PK_NOTABLE;
*piPk = 0;
- zSql = sqlite3_mprintf(
+
+ assert( p->rc==SQLITE_OK );
+ p->rc = prepareFreeAndCollectError(p->db, &aStmt[0], &p->zErrmsg,
+ sqlite3_mprintf(
"SELECT (sql LIKE 'create virtual%%')"
- " FROM main.sqlite_master"
- " WHERE name=%Q", zTab);
- if( zSql==0 ) return SQLITE_NOMEM;
- rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
- sqlite3_free(zSql);
- zSql = 0;
- if( pStmt==0 ) goto otaTableType_end;
- if( sqlite3_step(pStmt)!=SQLITE_ROW ){
- goto otaTableType_end; /* no such table */
- }
- if( sqlite3_column_int(pStmt,0) ){
+ " FROM sqlite_master"
+ " WHERE name=%Q", zTab
+ ));
+ if( p->rc!=SQLITE_OK || sqlite3_step(aStmt[0])!=SQLITE_ROW ){
+ /* Either an error, or no such table. */
+ goto otaTableType_end;
+ }
+ if( sqlite3_column_int(aStmt[0], 0) ){
*peType = OTA_PK_VTAB; /* virtual table */
goto otaTableType_end;
}
- rc = sqlite3_finalize(pStmt);
- if( rc ) return rc;
- zSql = sqlite3_mprintf("PRAGMA index_list=%Q",zTab);
- if( zSql==0 ) return SQLITE_NOMEM;
- rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
- sqlite3_free(zSql);
- zSql = 0;
- if( pStmt==0 ) goto otaTableType_end;
- while( sqlite3_step(pStmt)==SQLITE_ROW ){
- const u8 *zOrig = sqlite3_column_text(pStmt,3);
- if( zOrig && zOrig[0]=='p' ){
- zSql = sqlite3_mprintf("SELECT rootpage FROM main.sqlite_master"
- " WHERE name=%Q", sqlite3_column_text(pStmt,1));
- if( zSql==0 ){ rc = SQLITE_NOMEM; goto otaTableType_end; }
- break;
+
+ p->rc = prepareFreeAndCollectError(p->db, &aStmt[1], &p->zErrmsg,
+ sqlite3_mprintf("PRAGMA index_list=%Q",zTab)
+ );
+ if( p->rc ) goto otaTableType_end;
+ while( sqlite3_step(aStmt[1])==SQLITE_ROW ){
+ const u8 *zOrig = sqlite3_column_text(aStmt[1], 3);
+ const u8 *zIdx = sqlite3_column_text(aStmt[1], 1);
+ if( zOrig && zIdx && zOrig[0]=='p' ){
+ p->rc = prepareFreeAndCollectError(p->db, &aStmt[2], &p->zErrmsg,
+ sqlite3_mprintf(
+ "SELECT rootpage FROM sqlite_master WHERE name = %Q", zIdx
+ ));
+ if( p->rc==SQLITE_OK ){
+ if( sqlite3_step(aStmt[2])==SQLITE_ROW ){
+ *piPk = sqlite3_column_int(aStmt[2], 0);
+ *peType = OTA_PK_EXTERNAL;
+ }else{
+ *peType = OTA_PK_WITHOUT_ROWID;
+ }
+ }
+ goto otaTableType_end;
}
}
- rc = sqlite3_finalize(pStmt);
- pStmt = 0;
- if( rc ) return rc;
- if( zSql ){
- rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
- sqlite3_free(zSql);
- zSql = 0;
- if( pStmt==0 ) goto otaTableType_end;
- if( sqlite3_step(pStmt)==SQLITE_ROW ){
- *piPk = sqlite3_column_int(pStmt, 0);
- *peType = OTA_PK_EXTERNAL; /* external PK index */
- }else{
- *peType = OTA_PK_WITHOUT_ROWID; /* WITHOUT ROWID table */
- }
- }else{
- zSql = sqlite3_mprintf("PRAGMA table_info=%Q", zTab);
- if( zSql==0 ) return SQLITE_NOMEM;
- rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
- sqlite3_free(zSql);
- zSql = 0;
- if( pStmt==0 ) goto otaTableType_end;
- *peType = OTA_PK_NONE; /* (default) implicit ROWID */
- while( sqlite3_step(pStmt)==SQLITE_ROW ){
- if( sqlite3_column_int(pStmt,5)>0 ){
+
+ p->rc = prepareFreeAndCollectError(p->db, &aStmt[3], &p->zErrmsg,
+ sqlite3_mprintf("PRAGMA table_info=%Q",zTab)
+ );
+ if( p->rc==SQLITE_OK ){
+ while( sqlite3_step(aStmt[3])==SQLITE_ROW ){
+ if( sqlite3_column_int(aStmt[3],5)>0 ){
*peType = OTA_PK_IPK; /* explicit IPK column */
- break;
+ goto otaTableType_end;
}
}
+ *peType = OTA_PK_NONE;
}
-otaTableType_end:
- sqlite3_free(zSql);
- rc2 = sqlite3_finalize(pStmt);
- return rc ? rc : rc2;
+otaTableType_end: {
+ int i;
+ for(i=0; i<sizeof(aStmt)/sizeof(aStmt[0]); i++){
+ int rc2 = sqlite3_finalize(aStmt[i]);
+ if( p->rc==SQLITE_OK ) p->rc = rc2;
+ }
+ }
}
/* Figure out the type of table this step will deal with. */
assert( pIter->eType==0 );
- p->rc = otaTableType(p->db, pIter->zTbl, &pIter->eType, &pIter->iPkTnum);
+ otaTableType(p, pIter->zTbl, &pIter->eType, &pIter->iPkTnum);
if( p->rc ) return p->rc;
assert( pIter->eType==OTA_PK_NONE || pIter->eType==OTA_PK_IPK
rc = sqlite3_finalize(pXList);
if( p->rc==SQLITE_OK ) p->rc = rc;
- while( p->rc==SQLITE_OK && pXInfo && SQLITE_ROW==sqlite3_step(pXInfo) ){
+ while( p->rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pXInfo) ){
if( sqlite3_column_int(pXInfo, 5) ){
/* int iCid = sqlite3_column_int(pXInfo, 0); */
const char *zCol = (const char*)sqlite3_column_text(pXInfo, 2);
return z;
}
+/*
+** This function creates the second imposter table used when writing to
+** a table b-tree where the table has an external primary key. If the
+** iterator passed as the second argument does not currently point to
+** a table (not index) with an external primary key, this function is a
+** no-op.
+**
+** Assuming the iterator does point to a table with an external PK, this
+** function creates a WITHOUT ROWID imposter table named "ota_imposter2"
+** used to access that PK index. For example, if the target table is
+** declared as follows:
+**
+** CREATE TABLE t1(a, b TEXT, c REAL, PRIMARY KEY(b, c));
+**
+** then the imposter table schema is:
+**
+** CREATE TABLE ota_imposter2(c1 TEXT, c2 REAL, id INTEGER) WITHOUT ROWID;
+**
+*/
static void otaCreateImposterTable2(sqlite3ota *p, OtaObjIter *pIter){
if( p->rc==SQLITE_OK && pIter->eType==OTA_PK_EXTERNAL ){
int tnum = pIter->iPkTnum; /* Root page of PK index */