mirror of
https://github.com/sqlite/sqlite.git
synced 2025-11-14 00:22:38 +03:00
Enhance the "PRAGMA integrity_check" command to verify that all indices are
correctly constructed. New calls to integrity_check are made in the test suite. These changes are intended to prevent any future problems such as seen in ticket #334. (CVS 1024) FossilOrigin-Name: c9734c27074d2039a1896a8c6965c08d03711b13
This commit is contained in:
158
src/pragma.c
158
src/pragma.c
@@ -11,7 +11,7 @@
|
||||
*************************************************************************
|
||||
** This file contains code used to implement the PRAGMA command.
|
||||
**
|
||||
** $Id: pragma.c,v 1.9 2003/06/04 15:48:33 drh Exp $
|
||||
** $Id: pragma.c,v 1.10 2003/06/15 23:42:24 drh Exp $
|
||||
*/
|
||||
#include "sqliteInt.h"
|
||||
#include <ctype.h>
|
||||
@@ -509,28 +509,156 @@ void sqlitePragma(Parse *pParse, Token *pLeft, Token *pRight, int minusFlag){
|
||||
#endif
|
||||
|
||||
if( sqliteStrICmp(zLeft, "integrity_check")==0 ){
|
||||
int i, j, addr;
|
||||
|
||||
/* Code that initializes the integrity check program. Set the
|
||||
** error message to an empty string and register the callback
|
||||
** column name.
|
||||
*/
|
||||
static VdbeOp initCode[] = {
|
||||
{ OP_String, 0, 0, ""},
|
||||
{ OP_MemStore, 0, 1, 0},
|
||||
{ OP_ColumnName, 0, 0, "integrity_check"},
|
||||
};
|
||||
|
||||
/* Code to do an BTree integrity check on a single database file.
|
||||
*/
|
||||
static VdbeOp checkDb[] = {
|
||||
{ OP_SetInsert, 0, 0, "2"},
|
||||
{ OP_Integer, 0, 0, 0},
|
||||
{ OP_Integer, 0, 0, 0}, /* 1 */
|
||||
{ OP_OpenRead, 0, 2, 0},
|
||||
{ OP_Rewind, 0, 7, 0},
|
||||
{ OP_Rewind, 0, 7, 0}, /* 3 */
|
||||
{ OP_Column, 0, 3, 0}, /* 4 */
|
||||
{ OP_SetInsert, 0, 0, 0},
|
||||
{ OP_Next, 0, 4, 0},
|
||||
{ OP_Next, 0, 4, 0}, /* 6 */
|
||||
{ OP_IntegrityCk, 0, 0, 0}, /* 7 */
|
||||
{ OP_ColumnName, 0, 0, "integrity_check"},
|
||||
{ OP_Callback, 1, 0, 0},
|
||||
{ OP_SetInsert, 1, 0, "2"},
|
||||
{ OP_Integer, 1, 0, 0},
|
||||
{ OP_OpenRead, 1, 2, 0},
|
||||
{ OP_Rewind, 1, 17, 0},
|
||||
{ OP_Column, 1, 3, 0}, /* 14 */
|
||||
{ OP_SetInsert, 1, 0, 0},
|
||||
{ OP_Next, 1, 14, 0},
|
||||
{ OP_IntegrityCk, 1, 1, 0}, /* 17 */
|
||||
{ OP_Dup, 0, 1, 0},
|
||||
{ OP_String, 0, 0, "ok"},
|
||||
{ OP_StrEq, 0, 12, 0}, /* 10 */
|
||||
{ OP_MemLoad, 0, 0, 0},
|
||||
{ OP_String, 0, 0, "*** in database "},
|
||||
{ OP_String, 0, 0, 0}, /* 13 */
|
||||
{ OP_String, 0, 0, " ***\n"},
|
||||
{ OP_Pull, 4, 0, 0},
|
||||
{ OP_Concat, 5, 1, 0},
|
||||
{ OP_MemStore, 0, 1, 0},
|
||||
{ OP_Integer, 0, 0, 0},
|
||||
{ OP_Pop, 1, 0, 0},
|
||||
};
|
||||
|
||||
/* Code that appears at the end of the integrity check. If no error
|
||||
** messages have been generated, output OK. Otherwise output the
|
||||
** error message
|
||||
*/
|
||||
static VdbeOp endCode[] = {
|
||||
{ OP_MemLoad, 0, 0, 0},
|
||||
{ OP_Dup, 0, 1, 0},
|
||||
{ OP_String, 0, 0, ""},
|
||||
{ OP_StrNe, 0, 0, 0}, /* 3 */
|
||||
{ OP_Pop, 1, 0, 0},
|
||||
{ OP_String, 0, 0, "ok"},
|
||||
{ OP_Callback, 1, 0, 0},
|
||||
};
|
||||
sqliteVdbeAddOpList(v, ArraySize(checkDb), checkDb);
|
||||
|
||||
/* Initialize the VDBE program */
|
||||
sqliteVdbeAddOpList(v, ArraySize(initCode), initCode);
|
||||
|
||||
/* Do an integrity check on each database file */
|
||||
for(i=0; i<db->nDb; i++){
|
||||
HashElem *x;
|
||||
|
||||
/* Do an integrity check of the B-Tree
|
||||
*/
|
||||
addr = sqliteVdbeAddOpList(v, ArraySize(checkDb), checkDb);
|
||||
sqliteVdbeChangeP1(v, addr+1, i);
|
||||
sqliteVdbeChangeP2(v, addr+3, addr+7);
|
||||
sqliteVdbeChangeP2(v, addr+6, addr+4);
|
||||
sqliteVdbeChangeP2(v, addr+7, i);
|
||||
sqliteVdbeChangeP2(v, addr+10, addr+ArraySize(checkDb)-1);
|
||||
sqliteVdbeChangeP3(v, addr+13, db->aDb[i].zName, P3_STATIC);
|
||||
|
||||
/* Make sure all the indices are constructed correctly.
|
||||
*/
|
||||
sqliteCodeVerifySchema(pParse, i);
|
||||
for(x=sqliteHashFirst(&db->aDb[i].tblHash); x; x=sqliteHashNext(x)){
|
||||
Table *pTab = sqliteHashData(x);
|
||||
Index *pIdx;
|
||||
int loopTop;
|
||||
|
||||
if( pTab->pIndex==0 ) continue;
|
||||
sqliteVdbeAddOp(v, OP_Integer, i, 0);
|
||||
sqliteVdbeAddOp(v, OP_OpenRead, 1, pTab->tnum);
|
||||
sqliteVdbeChangeP3(v, -1, pTab->zName, P3_STATIC);
|
||||
for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
|
||||
if( pIdx->tnum==0 ) continue;
|
||||
sqliteVdbeAddOp(v, OP_Integer, pIdx->iDb, 0);
|
||||
sqliteVdbeAddOp(v, OP_OpenRead, j+2, pIdx->tnum);
|
||||
sqliteVdbeChangeP3(v, -1, pIdx->zName, P3_STATIC);
|
||||
}
|
||||
sqliteVdbeAddOp(v, OP_Integer, 0, 0);
|
||||
sqliteVdbeAddOp(v, OP_MemStore, 1, 1);
|
||||
loopTop = sqliteVdbeAddOp(v, OP_Rewind, 1, 0);
|
||||
sqliteVdbeAddOp(v, OP_MemIncr, 1, 0);
|
||||
for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
|
||||
int k, jmp2;
|
||||
static VdbeOp idxErr[] = {
|
||||
{ OP_MemLoad, 0, 0, 0},
|
||||
{ OP_String, 0, 0, "rowid "},
|
||||
{ OP_Recno, 1, 0, 0},
|
||||
{ OP_String, 0, 0, " missing from index "},
|
||||
{ OP_String, 0, 0, 0}, /* 4 */
|
||||
{ OP_String, 0, 0, "\n"},
|
||||
{ OP_Concat, 6, 0, 0},
|
||||
{ OP_MemStore, 0, 1, 0},
|
||||
};
|
||||
sqliteVdbeAddOp(v, OP_Recno, 1, 0);
|
||||
for(k=0; k<pIdx->nColumn; k++){
|
||||
int idx = pIdx->aiColumn[k];
|
||||
if( idx==pTab->iPKey ){
|
||||
sqliteVdbeAddOp(v, OP_Recno, 1, 0);
|
||||
}else{
|
||||
sqliteVdbeAddOp(v, OP_Column, 1, idx);
|
||||
}
|
||||
}
|
||||
sqliteVdbeAddOp(v, OP_MakeIdxKey, pIdx->nColumn, 0);
|
||||
if( db->file_format>=4 ) sqliteAddIdxKeyType(v, pIdx);
|
||||
jmp2 = sqliteVdbeAddOp(v, OP_Found, j+2, 0);
|
||||
addr = sqliteVdbeAddOpList(v, ArraySize(idxErr), idxErr);
|
||||
sqliteVdbeChangeP3(v, addr+4, pIdx->zName, P3_STATIC);
|
||||
sqliteVdbeChangeP2(v, jmp2, sqliteVdbeCurrentAddr(v));
|
||||
}
|
||||
sqliteVdbeAddOp(v, OP_Next, 1, loopTop+1);
|
||||
sqliteVdbeChangeP2(v, loopTop, sqliteVdbeCurrentAddr(v));
|
||||
for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
|
||||
static VdbeOp cntIdx[] = {
|
||||
{ OP_Integer, 0, 0, 0},
|
||||
{ OP_MemStore, 2, 1, 0},
|
||||
{ OP_Rewind, 0, 0, 0}, /* 2 */
|
||||
{ OP_MemIncr, 2, 0, 0},
|
||||
{ OP_Next, 0, 0, 0}, /* 4 */
|
||||
{ OP_MemLoad, 1, 0, 0},
|
||||
{ OP_MemLoad, 2, 0, 0},
|
||||
{ OP_Eq, 0, 0, 0}, /* 7 */
|
||||
{ OP_MemLoad, 0, 0, 0},
|
||||
{ OP_String, 0, 0, "wrong # of entries in index "},
|
||||
{ OP_String, 0, 0, 0}, /* 10 */
|
||||
{ OP_String, 0, 0, "\n"},
|
||||
{ OP_Concat, 4, 0, 0},
|
||||
{ OP_MemStore, 0, 1, 0},
|
||||
};
|
||||
if( pIdx->tnum==0 ) continue;
|
||||
addr = sqliteVdbeAddOpList(v, ArraySize(cntIdx), cntIdx);
|
||||
sqliteVdbeChangeP1(v, addr+2, j+2);
|
||||
sqliteVdbeChangeP2(v, addr+2, addr+5);
|
||||
sqliteVdbeChangeP1(v, addr+4, j+2);
|
||||
sqliteVdbeChangeP2(v, addr+4, addr+3);
|
||||
sqliteVdbeChangeP2(v, addr+7, addr+ArraySize(cntIdx));
|
||||
sqliteVdbeChangeP3(v, addr+10, pIdx->zName, P3_STATIC);
|
||||
}
|
||||
}
|
||||
}
|
||||
addr = sqliteVdbeAddOpList(v, ArraySize(endCode), endCode);
|
||||
sqliteVdbeChangeP2(v, addr+3, addr+ArraySize(endCode)-1);
|
||||
}else
|
||||
|
||||
{}
|
||||
|
||||
Reference in New Issue
Block a user