Add some more simple test cases for UPSERT. And a minor fix.
FossilOrigin-Name: 27cd3b2fb2ad0cf2b36741bd1057cb7973954d40456e9db158261a38b049d2b5
diff --git a/src/insert.c b/src/insert.c
index 4f9e43b..c5c92d6 100644
--- a/src/insert.c
+++ b/src/insert.c
@@ -1580,7 +1580,7 @@
}
#ifndef SQLITE_OMIT_UPSERT
case OE_Update: {
- sqlite3UpsertDoUpdate(pParse, pUpsert, pTab, 0, iDataCur, 0);
+ sqlite3UpsertDoUpdate(pParse, pUpsert, pTab, 0, iDataCur);
/* Fall through */
}
#endif
@@ -1787,7 +1787,7 @@
}
#ifndef SQLITE_OMIT_UPSERT
case OE_Update: {
- sqlite3UpsertDoUpdate(pParse, pUpsert, pTab, pIdx, iDataCur, iIdxCur);
+ sqlite3UpsertDoUpdate(pParse, pUpsert, pTab, pIdx, iIdxCur+ix);
/* Fall through */
}
#endif
diff --git a/src/sqliteInt.h b/src/sqliteInt.h
index 1a5fc5e..c0d0027 100644
--- a/src/sqliteInt.h
+++ b/src/sqliteInt.h
@@ -4293,7 +4293,7 @@
void sqlite3UpsertDelete(sqlite3*,Upsert*);
Upsert *sqlite3UpsertDup(sqlite3*,Upsert*);
int sqlite3UpsertAnalyzeTarget(Parse*,SrcList*,Upsert*);
- void sqlite3UpsertDoUpdate(Parse*,Upsert*,Table*,Index*,int,int);
+ void sqlite3UpsertDoUpdate(Parse*,Upsert*,Table*,Index*,int);
#else
#define sqlite3UpsertNew(x,y,z,w) ((Upsert*)0)
#define sqlite3UpsertDelete(x,y)
diff --git a/src/upsert.c b/src/upsert.c
index 053bc92..e1af0a0 100644
--- a/src/upsert.c
+++ b/src/upsert.c
@@ -182,14 +182,19 @@
/*
** Generate bytecode that does an UPDATE as part of an upsert.
+**
+** If pIdx is NULL, then the UNIQUE constraint that failed was the IPK.
+** In this case parameter iCur is a cursor open on the table b-tree that
+** currently points to the conflicting table row. Otherwise, if pIdx
+** is not NULL, then pIdx is the constraint that failed and iCur is a
+** cursor points to the conflicting row.
*/
void sqlite3UpsertDoUpdate(
Parse *pParse, /* The parsing and code-generating context */
Upsert *pUpsert, /* The ON CONFLICT clause for the upsert */
Table *pTab, /* The table being updated */
Index *pIdx, /* The UNIQUE constraint that failed */
- int iDataCur, /* Cursor for the pTab, table being updated */
- int iIdxCur /* Cursor for pIdx */
+ int iCur /* Cursor for pIdx (or pTab if pIdx==NULL) */
){
Vdbe *v = pParse->pVdbe;
sqlite3 *db = pParse->db;
@@ -205,9 +210,9 @@
/* We are dealing with an IPK */
regKey = ++pParse->nMem;
if( pIdx ){
- sqlite3VdbeAddOp2(v, OP_IdxRowid, iIdxCur, regKey);
+ sqlite3VdbeAddOp2(v, OP_IdxRowid, iCur, regKey);
}else{
- sqlite3VdbeAddOp2(v, OP_Rowid, iDataCur, regKey);
+ sqlite3VdbeAddOp2(v, OP_Rowid, iCur, regKey);
}
pE1 = sqlite3ExprAlloc(db, TK_COLUMN, 0, 0);
if( pE1 ){
@@ -231,7 +236,7 @@
}
for(i=0; i<pIdx->nKeyCol; i++){
regKey = ++pParse->nMem;
- sqlite3VdbeAddOp3(v, OP_Column, iDataCur, i, regKey);
+ sqlite3VdbeAddOp3(v, OP_Column, iCur, i, regKey);
j = pIdx->aiColumn[i];
VdbeComment((v, "%s", pTab->aCol[j].zName));
pE1 = sqlite3ExprAlloc(db, TK_COLUMN, 0, 0);