1
0
mirror of https://github.com/sqlite/sqlite.git synced 2025-07-29 08:01:23 +03:00

Remove some unreachable code in sqlite3session.c. Add test cases.

FossilOrigin-Name: 39cdfa5324ae91bfbbac733b1e3e2d33ca883340
This commit is contained in:
dan
2011-03-21 19:41:29 +00:00
parent 8cc9c1aa15
commit db04571c79
7 changed files with 150 additions and 123 deletions

View File

@ -552,11 +552,10 @@ static int sessionTableInfo(
int nThis;
int i;
u8 *pAlloc;
u8 *pFree = 0;
char **azCol;
char **azCol = 0;
u8 *abPK;
assert( pazCol || pabPK );
assert( pazCol && pabPK );
nThis = strlen(zThis);
zPragma = sqlite3_mprintf("PRAGMA '%q'.table_info('%q')", zDb, zThis);
@ -584,15 +583,10 @@ static int sessionTableInfo(
}
}
if( rc==SQLITE_OK ){
pFree = pAlloc;
if( pazCol ){
azCol = (char **)pAlloc;
pAlloc = (u8 *)&azCol[nCol];
}
if( pabPK ){
abPK = (u8 *)pAlloc;
pAlloc = &abPK[nCol];
}
azCol = (char **)pAlloc;
pAlloc = (u8 *)&azCol[nCol];
abPK = (u8 *)pAlloc;
pAlloc = &abPK[nCol];
if( pzTab ){
memcpy(pAlloc, zThis, nThis+1);
*pzTab = (char *)pAlloc;
@ -604,12 +598,10 @@ static int sessionTableInfo(
int nName = sqlite3_column_bytes(pStmt, 1);
const unsigned char *zName = sqlite3_column_text(pStmt, 1);
if( zName==0 ) break;
if( pazCol ){
memcpy(pAlloc, zName, nName+1);
azCol[i] = (char *)pAlloc;
pAlloc += nName+1;
}
if( pabPK ) abPK[i] = sqlite3_column_int(pStmt, 5);
memcpy(pAlloc, zName, nName+1);
azCol[i] = (char *)pAlloc;
pAlloc += nName+1;
abPK[i] = sqlite3_column_int(pStmt, 5);
i++;
}
rc = sqlite3_reset(pStmt);
@ -620,13 +612,13 @@ static int sessionTableInfo(
** free any allocation made. An error code will be returned in this case.
*/
if( rc==SQLITE_OK ){
if( pazCol ) *pazCol = (const char **)azCol;
if( pabPK ) *pabPK = abPK;
*pazCol = (const char **)azCol;
*pabPK = abPK;
}else{
if( pazCol ) *pazCol = 0;
if( pabPK ) *pabPK = 0;
*pazCol = 0;
*pabPK = 0;
if( pzTab ) *pzTab = 0;
sqlite3_free(pFree);
sqlite3_free(azCol);
}
sqlite3_finalize(pStmt);
return rc;
@ -1785,7 +1777,7 @@ struct SessionApplyCtx {
**
** The DELETE statement looks like this:
**
** DELETE FROM x WHERE a = :1 AND c = :3 AND :5 OR (b IS :2 AND d IS :4)
** DELETE FROM x WHERE a = :1 AND c = :3 AND (:5 OR b IS :2 AND d IS :4)
**
** Variable :5 (nCol+1) is a boolean. It should be set to 0 if we require
** matching b and d values, or 1 otherwise. The second case comes up if the
@ -1997,6 +1989,41 @@ static int sessionInsertRow(
return rc;
}
/*
** Iterator pIter must point to an SQLITE_INSERT entry. This function
** transfers new.* values from the current iterator entry to statement
** pStmt. The table being inserted into has nCol columns.
**
** New.* value $i 0 from the iterator is bound to variable ($i+1) of
** statement pStmt. If parameter abPK is NULL, all values from 0 to (nCol-1)
** are transfered to the statement. Otherwise, if abPK is not NULL, it points
** to an array nCol elements in size. In this case only those values for
** which abPK[$i] is true are read from the iterator and bound to the
** statement.
**
** An SQLite error code is returned if an error occurs. Otherwise, SQLITE_OK.
*/
static int sessionBindValues(
sqlite3_changeset_iter *pIter, /* Iterator to read values from */
int(*xIterValue)(sqlite3_changeset_iter *, int, sqlite3_value **),
int nCol, /* Number of columns */
u8 *abPK, /* If not NULL, bind only if true */
sqlite3_stmt *pStmt /* Bind values to this statement */
){
int i;
int rc = SQLITE_OK;
for(i=0; rc==SQLITE_OK && i<nCol; i++){
if( !abPK || abPK[i] ){
sqlite3_value *pVal;
rc = xIterValue(pIter, i, &pVal);
if( rc==SQLITE_OK ){
rc = sqlite3_bind_value(pStmt, i+1, pVal);
}
}
}
return rc;
}
/*
** SQL statement pSelect is as generated by the sessionSelectRow() function.
** This function binds the primary key values from the change that changeset
@ -2007,10 +2034,8 @@ static int sessionInsertRow(
** error occurs, an SQLite error code is returned.
**
** If the iterator currently points to an INSERT record, bind values from the
** new.* record to the SELECT statement. Or, if it points to a DELETE, bind
** values from the old.* record. If the changeset iterator points to an
** UPDATE, bind values from the new.* record, but use old.* values in place
** of any undefined new.* values.
** new.* record to the SELECT statement. Or, if it points to a DELETE or
** UPDATE, bind values from the old.* record.
*/
static int sessionSeekToRow(
sqlite3 *db, /* Database handle */
@ -2019,27 +2044,15 @@ static int sessionSeekToRow(
sqlite3_stmt *pSelect /* SELECT statement from sessionSelectRow() */
){
int rc = SQLITE_OK; /* Return code */
int i; /* Used to iterate through table columns */
int nCol; /* Number of columns in table */
int op; /* Changset operation (SQLITE_UPDATE etc.) */
const char *zDummy; /* Unused */
sqlite3changeset_op(pIter, &zDummy, &nCol, &op);
for(i=0; rc==SQLITE_OK && i<nCol; i++){
if( abPK[i] ){
sqlite3_value *pVal = 0;
if( op!=SQLITE_DELETE ){
rc = sqlite3changeset_new(pIter, i, &pVal);
}
if( pVal==0 ){
rc = sqlite3changeset_old(pIter, i, &pVal);
}
if( rc==SQLITE_OK ){
rc = sqlite3_bind_value(pSelect, i+1, pVal);
}
}
}
rc = sessionBindValues(pIter,
op==SQLITE_INSERT ? sqlite3changeset_new : sqlite3changeset_old,
nCol, abPK, pSelect
);
if( rc==SQLITE_OK ){
rc = sqlite3_step(pSelect);
@ -2108,7 +2121,7 @@ static int sessionConflictHandler(
if( pbReplace ){
rc = sessionSeekToRow(p->db, pIter, p->abPK, p->pSelect);
}else{
rc = SQLITE_DONE;
rc = SQLITE_OK;
}
if( rc==SQLITE_ROW ){
@ -2117,7 +2130,7 @@ static int sessionConflictHandler(
res = xConflict(pCtx, eType, pIter);
pIter->pConflict = 0;
rc = sqlite3_reset(p->pSelect);
}else{
}else if( rc==SQLITE_OK ){
/* No other row with the new.* primary key. */
rc = sqlite3_reset(p->pSelect);
if( rc==SQLITE_OK ){
@ -2192,16 +2205,9 @@ static int sessionApplyOneOp(
sqlite3changeset_op(pIter, &zDummy, &nCol, &op);
if( op==SQLITE_DELETE ){
int i;
/* Bind values to the DELETE statement. */
for(i=0; rc==SQLITE_OK && i<nCol; i++){
sqlite3_value *pVal;
rc = sqlite3changeset_old(pIter, i, &pVal);
if( rc==SQLITE_OK ){
rc = sqlite3_bind_value(p->pDelete, i+1, pVal);
}
}
rc = sessionBindValues(pIter, sqlite3changeset_old, nCol, 0, p->pDelete);
if( rc==SQLITE_OK && sqlite3_bind_parameter_count(p->pDelete)>nCol ){
rc = sqlite3_bind_int(p->pDelete, nCol+1, pbRetry==0);
}
@ -2254,44 +2260,20 @@ static int sessionApplyOneOp(
);
}else if( rc==SQLITE_CONSTRAINT ){
/* This may be a CONSTRAINT or CONFLICT error. It is a CONFLICT if
** the only problem is a duplicate PRIMARY KEY, or a CONSTRAINT
** otherwise. */
int bPKChange = 0;
/* Check if the PK has been modified. */
rc = SQLITE_OK;
for(i=0; i<nCol && rc==SQLITE_OK; i++){
if( p->abPK[i] ){
sqlite3_value *pNew;
rc = sqlite3changeset_new(pIter, i, &pNew);
if( rc==SQLITE_OK && pNew ){
bPKChange = 1;
break;
}
}
}
rc = sessionConflictHandler(SQLITE_CHANGESET_CONFLICT,
p, pIter, xConflict, pCtx, (bPKChange ? pbReplace : 0)
/* This is always a CONSTRAINT conflict. */
rc = sessionConflictHandler(
SQLITE_CHANGESET_CONFLICT, p, pIter, xConflict, pCtx, 0
);
}
}else{
int i;
assert( op==SQLITE_INSERT );
for(i=0; rc==SQLITE_OK && i<nCol; i++){
sqlite3_value *pVal;
rc = sqlite3changeset_new(pIter, i, &pVal);
if( rc==SQLITE_OK ){
rc = sqlite3_bind_value(p->pInsert, i+1, pVal);
}
}
rc = sessionBindValues(pIter, sqlite3changeset_new, nCol, 0, p->pInsert);
if( rc!=SQLITE_OK ) return rc;
sqlite3_step(p->pInsert);
rc = sqlite3_reset(p->pInsert);
if( rc==SQLITE_CONSTRAINT && xConflict ){
if( rc==SQLITE_CONSTRAINT ){
rc = sessionConflictHandler(
SQLITE_CHANGESET_CONFLICT, p, pIter, xConflict, pCtx, pbReplace
);
@ -2369,21 +2351,11 @@ int sqlite3changeset_apply(
}
if( bReplace ){
assert( pIter->op==SQLITE_INSERT );
rc = sqlite3_exec(db, "SAVEPOINT replace_op", 0, 0, 0);
if( rc==SQLITE_OK ){
int i;
for(i=0; i<sApply.nCol; i++){
if( sApply.abPK[i] ){
sqlite3_value *pVal;
rc = sqlite3changeset_new(pIter, i, &pVal);
if( rc==SQLITE_OK && pVal==0 ){
rc = sqlite3changeset_old(pIter, i, &pVal);
}
if( rc==SQLITE_OK ){
rc = sqlite3_bind_value(sApply.pDelete, i+1, pVal);
}
}
}
rc = sessionBindValues(pIter,
sqlite3changeset_new, sApply.nCol, sApply.abPK, sApply.pDelete);
sqlite3_bind_int(sApply.pDelete, sApply.nCol+1, 1);
}
if( rc==SQLITE_OK ){