Enhance UPSERT so that the UPDATE uses the same set of cursors as the INSERT.

FossilOrigin-Name: c37f39d18d41ae5ba6c4561d87cbbf71f3b6896b86cc5cff9cdf046b02dc521a
This commit is contained in:
drh
2018-04-20 13:18:51 +00:00
parent 2ac4e5cca9
commit 7fc3aba862
6 changed files with 26 additions and 18 deletions
+2
View File
@@ -812,6 +812,8 @@ void sqlite3Insert(
pTabList->a[0].iCursor = iDataCur;
pUpsert->pUpsertSrc = pTabList;
pUpsert->regData = regData;
pUpsert->iDataCur = iDataCur;
pUpsert->iIdxCur = iIdxCur;
if( pUpsert->pUpsertTarget ){
sqlite3UpsertAnalyzeTarget(pParse, pTabList, pUpsert);
}
+6
View File
@@ -2743,8 +2743,14 @@ struct Upsert {
Index *pUpsertIdx; /* Constraint that pUpsertTarget identifies */
ExprList *pUpsertSet; /* The SET clause from an ON CONFLICT UPDATE */
Expr *pUpsertWhere; /* WHERE clause for the ON CONFLICT UPDATE */
/* The fields above comprise the parse tree for the upsert clause.
** The fields below are used to transfer information from the INSERT
** processing down into the UPDATE processing while generating code.
** Upsert owns the memory allocated above, but not the memory below. */
SrcList *pUpsertSrc; /* Table to be updated */
int regData; /* First register holding array of VALUES */
int iDataCur; /* Index of the data cursor */
int iIdxCur; /* Index of the first index cursor */
};
/*
+6 -1
View File
@@ -211,6 +211,11 @@ void sqlite3Update(
}
pParse->nTab++;
}
if( pUpsert ){
iDataCur = pUpsert->iDataCur;
iIdxCur = pUpsert->iIdxCur;
pParse->nTab = iBaseCur;
}
pTabList->a[0].iCursor = iDataCur;
/* Allocate space for aXRef[], aRegIdx[], and aToOpen[].
@@ -476,7 +481,7 @@ void sqlite3Update(
}
labelBreak = sqlite3VdbeMakeLabel(v);
if( !isView ){
if( !isView && pUpsert==0 ){
int addrOnce = 0;
/* Open every index that needs updating. */
+2 -7
View File
@@ -222,7 +222,7 @@ void sqlite3UpsertDoUpdate(
pE1 = sqlite3ExprAlloc(db, TK_COLUMN, 0, 0);
if( pE1 ){
pE1->pTab = pTab;
pE1->iTable = pParse->nTab;
pE1->iTable = pUpsert->iDataCur;
pE1->iColumn = -1;
}
pE2 = sqlite3ExprAlloc(db, TK_REGISTER, 0, 0);
@@ -234,11 +234,6 @@ void sqlite3UpsertDoUpdate(
}else{
/* a WITHOUT ROWID table */
int i, j;
int iTab = pParse->nTab+1;
Index *pX;
for(pX=pTab->pIndex; ALWAYS(pX) && !IsPrimaryKeyIndex(pX); pX=pX->pNext){
iTab++;
}
for(i=0; i<pIdx->nKeyCol; i++){
regKey = ++pParse->nMem;
sqlite3VdbeAddOp3(v, OP_Column, iCur, i, regKey);
@@ -247,7 +242,7 @@ void sqlite3UpsertDoUpdate(
pE1 = sqlite3ExprAlloc(db, TK_COLUMN, 0, 0);
if( pE1 ){
pE1->pTab = pTab;
pE1->iTable = iTab;
pE1->iTable = pUpsert->iDataCur;
pE1->iColumn = j;
}
pE2 = sqlite3ExprAlloc(db, TK_REGISTER, 0, 0);