mirror of
https://github.com/sqlite/sqlite.git
synced 2025-07-30 19:03:16 +03:00
Fix RBU so that it does not write rows that should be excluded into partial indexes (corrupting the database).
FossilOrigin-Name: 31eb27f438ad727b095a518bfe0f7ed37cb806fc1e6929b821eddcc6cc9de260
This commit is contained in:
@ -240,6 +240,11 @@ struct RbuUpdateStmt {
|
||||
** it points to an array of flags nTblCol elements in size. The flag is
|
||||
** set for each column that is either a part of the PK or a part of an
|
||||
** index. Or clear otherwise.
|
||||
**
|
||||
** If there are one or more partial indexes on the table, all fields of
|
||||
** this array set set to 1. This is because in that case, the module has
|
||||
** no way to tell which fields will be required to add and remove entries
|
||||
** from the partial indexes.
|
||||
**
|
||||
*/
|
||||
struct RbuObjIter {
|
||||
@ -1250,8 +1255,12 @@ static void rbuObjIterCacheIndexedCols(sqlite3rbu *p, RbuObjIter *pIter){
|
||||
pIter->nIndex = 0;
|
||||
while( p->rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pList) ){
|
||||
const char *zIdx = (const char*)sqlite3_column_text(pList, 1);
|
||||
int bPartial = sqlite3_column_int(pList, 4);
|
||||
sqlite3_stmt *pXInfo = 0;
|
||||
if( zIdx==0 ) break;
|
||||
if( bPartial ){
|
||||
memset(pIter->abIndexed, 0x01, sizeof(u8)*pIter->nTblCol);
|
||||
}
|
||||
p->rc = prepareFreeAndCollectError(p->dbMain, &pXInfo, &p->zErrmsg,
|
||||
sqlite3_mprintf("PRAGMA main.index_xinfo = %Q", zIdx)
|
||||
);
|
||||
@ -1958,6 +1967,62 @@ static void rbuTmpInsertFunc(
|
||||
}
|
||||
}
|
||||
|
||||
static char *rbuObjIterGetIndexWhere(sqlite3rbu *p, RbuObjIter *pIter){
|
||||
sqlite3_stmt *pStmt = 0;
|
||||
int rc = p->rc;
|
||||
char *zRet = 0;
|
||||
|
||||
if( rc==SQLITE_OK ){
|
||||
rc = prepareAndCollectError(p->dbMain, &pStmt, &p->zErrmsg,
|
||||
"SELECT trim(sql) FROM sqlite_master WHERE type='index' AND name=?"
|
||||
);
|
||||
}
|
||||
if( rc==SQLITE_OK ){
|
||||
int rc2;
|
||||
rc = sqlite3_bind_text(pStmt, 1, pIter->zIdx, -1, SQLITE_STATIC);
|
||||
if( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
|
||||
const char *zSql = (const char*)sqlite3_column_text(pStmt, 0);
|
||||
if( zSql ){
|
||||
int nParen = 0; /* Number of open parenthesis */
|
||||
int i;
|
||||
for(i=0; zSql[i]; i++){
|
||||
char c = zSql[i];
|
||||
if( c=='(' ){
|
||||
nParen++;
|
||||
}
|
||||
else if( c==')' ){
|
||||
nParen--;
|
||||
if( nParen==0 ){
|
||||
i++;
|
||||
break;
|
||||
}
|
||||
}else if( c=='"' || c=='\'' || c=='`' ){
|
||||
for(i++; 1; i++){
|
||||
if( zSql[i]==c ){
|
||||
if( zSql[i+1]!=c ) break;
|
||||
i++;
|
||||
}
|
||||
}
|
||||
}else if( c=='[' ){
|
||||
for(i++; 1; i++){
|
||||
if( zSql[i]==']' ) break;
|
||||
}
|
||||
}
|
||||
}
|
||||
if( zSql[i] ){
|
||||
zRet = rbuStrndup(&zSql[i], &rc);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
rc2 = sqlite3_finalize(pStmt);
|
||||
if( rc==SQLITE_OK ) rc = rc2;
|
||||
}
|
||||
|
||||
p->rc = rc;
|
||||
return zRet;
|
||||
}
|
||||
|
||||
/*
|
||||
** Ensure that the SQLite statement handles required to update the
|
||||
** target database object currently indicated by the iterator passed
|
||||
@ -1987,6 +2052,7 @@ static int rbuObjIterPrepareAll(
|
||||
char *zImposterPK = 0; /* Primary key declaration for imposter */
|
||||
char *zWhere = 0; /* WHERE clause on PK columns */
|
||||
char *zBind = 0;
|
||||
char *zPart = 0;
|
||||
int nBind = 0;
|
||||
|
||||
assert( pIter->eType!=RBU_PK_VTAB );
|
||||
@ -1994,6 +2060,7 @@ static int rbuObjIterPrepareAll(
|
||||
p, pIter, &zImposterCols, &zImposterPK, &zWhere, &nBind
|
||||
);
|
||||
zBind = rbuObjIterGetBindlist(p, nBind);
|
||||
zPart = rbuObjIterGetIndexWhere(p, pIter);
|
||||
|
||||
/* Create the imposter table used to write to this index. */
|
||||
sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->dbMain, "main", 0, 1);
|
||||
@ -2026,28 +2093,30 @@ static int rbuObjIterPrepareAll(
|
||||
char *zSql;
|
||||
if( rbuIsVacuum(p) ){
|
||||
zSql = sqlite3_mprintf(
|
||||
"SELECT %s, 0 AS rbu_control FROM '%q' ORDER BY %s%s",
|
||||
"SELECT %s, 0 AS rbu_control FROM '%q' %s ORDER BY %s%s",
|
||||
zCollist,
|
||||
pIter->zDataTbl,
|
||||
zCollist, zLimit
|
||||
zPart, zCollist, zLimit
|
||||
);
|
||||
}else
|
||||
|
||||
if( pIter->eType==RBU_PK_EXTERNAL || pIter->eType==RBU_PK_NONE ){
|
||||
zSql = sqlite3_mprintf(
|
||||
"SELECT %s, rbu_control FROM %s.'rbu_tmp_%q' ORDER BY %s%s",
|
||||
"SELECT %s, rbu_control FROM %s.'rbu_tmp_%q' %s ORDER BY %s%s",
|
||||
zCollist, p->zStateDb, pIter->zDataTbl,
|
||||
zCollist, zLimit
|
||||
zPart, zCollist, zLimit
|
||||
);
|
||||
}else{
|
||||
zSql = sqlite3_mprintf(
|
||||
"SELECT %s, rbu_control FROM %s.'rbu_tmp_%q' "
|
||||
"SELECT %s, rbu_control FROM %s.'rbu_tmp_%q' %s "
|
||||
"UNION ALL "
|
||||
"SELECT %s, rbu_control FROM '%q' "
|
||||
"WHERE typeof(rbu_control)='integer' AND rbu_control!=1 "
|
||||
"%s %s typeof(rbu_control)='integer' AND rbu_control!=1 "
|
||||
"ORDER BY %s%s",
|
||||
zCollist, p->zStateDb, pIter->zDataTbl,
|
||||
zCollist, p->zStateDb, pIter->zDataTbl, zPart,
|
||||
zCollist, pIter->zDataTbl,
|
||||
zPart,
|
||||
(zPart ? "AND" : "WHERE"),
|
||||
zCollist, zLimit
|
||||
);
|
||||
}
|
||||
@ -2058,6 +2127,7 @@ static int rbuObjIterPrepareAll(
|
||||
sqlite3_free(zImposterPK);
|
||||
sqlite3_free(zWhere);
|
||||
sqlite3_free(zBind);
|
||||
sqlite3_free(zPart);
|
||||
}else{
|
||||
int bRbuRowid = (pIter->eType==RBU_PK_VTAB)
|
||||
||(pIter->eType==RBU_PK_NONE)
|
||||
|
Reference in New Issue
Block a user