blob: fb6c7c0c0702f76ca540f76a71d3d2e19f30e45c [file] [log] [blame]
drhfcfd7562018-04-12 21:42:51 +00001/*
2** 2018-04-12
3**
4** The author disclaims copyright to this source code. In place of
5** a legal notice, here is a blessing:
6**
7** May you do good and not evil.
8** May you find forgiveness for yourself and forgive others.
9** May you share freely, never taking more than you give.
10**
11*************************************************************************
12** This file contains code to implement various aspects of UPSERT
13** processing and handling of the Upsert object.
14*/
15#include "sqliteInt.h"
16
17#ifndef SQLITE_OMIT_UPSERT
18/*
19** Free a list of Upsert objects
20*/
drh2549e4c2020-12-08 14:29:03 +000021static void SQLITE_NOINLINE upsertDelete(sqlite3 *db, Upsert *p){
22 do{
23 Upsert *pNext = p->pNextUpsert;
drhfcfd7562018-04-12 21:42:51 +000024 sqlite3ExprListDelete(db, p->pUpsertTarget);
drhe9c2e772018-04-13 13:06:45 +000025 sqlite3ExprDelete(db, p->pUpsertTargetWhere);
drhfcfd7562018-04-12 21:42:51 +000026 sqlite3ExprListDelete(db, p->pUpsertSet);
27 sqlite3ExprDelete(db, p->pUpsertWhere);
drhdaf27612020-12-10 20:31:25 +000028 sqlite3DbFree(db, p->pToFree);
drhfcfd7562018-04-12 21:42:51 +000029 sqlite3DbFree(db, p);
drh2549e4c2020-12-08 14:29:03 +000030 p = pNext;
31 }while( p );
drhfcfd7562018-04-12 21:42:51 +000032}
drh2549e4c2020-12-08 14:29:03 +000033void sqlite3UpsertDelete(sqlite3 *db, Upsert *p){
34 if( p ) upsertDelete(db, p);
35}
36
drhfcfd7562018-04-12 21:42:51 +000037
38/*
39** Duplicate an Upsert object.
40*/
41Upsert *sqlite3UpsertDup(sqlite3 *db, Upsert *p){
42 if( p==0 ) return 0;
43 return sqlite3UpsertNew(db,
drhfcfd7562018-04-12 21:42:51 +000044 sqlite3ExprListDup(db, p->pUpsertTarget, 0),
drhe9c2e772018-04-13 13:06:45 +000045 sqlite3ExprDup(db, p->pUpsertTargetWhere, 0),
drhfcfd7562018-04-12 21:42:51 +000046 sqlite3ExprListDup(db, p->pUpsertSet, 0),
drh2549e4c2020-12-08 14:29:03 +000047 sqlite3ExprDup(db, p->pUpsertWhere, 0),
48 sqlite3UpsertDup(db, p->pNextUpsert)
drhfcfd7562018-04-12 21:42:51 +000049 );
50}
51
52/*
53** Create a new Upsert object.
54*/
55Upsert *sqlite3UpsertNew(
56 sqlite3 *db, /* Determines which memory allocator to use */
drhfcfd7562018-04-12 21:42:51 +000057 ExprList *pTarget, /* Target argument to ON CONFLICT, or NULL */
drhe9c2e772018-04-13 13:06:45 +000058 Expr *pTargetWhere, /* Optional WHERE clause on the target */
drhfcfd7562018-04-12 21:42:51 +000059 ExprList *pSet, /* UPDATE columns, or NULL for a DO NOTHING */
drh2549e4c2020-12-08 14:29:03 +000060 Expr *pWhere, /* WHERE clause for the ON CONFLICT UPDATE */
61 Upsert *pNext /* Next ON CONFLICT clause in the list */
drhfcfd7562018-04-12 21:42:51 +000062){
63 Upsert *pNew;
drhe84ad922020-12-09 20:30:47 +000064 pNew = sqlite3DbMallocZero(db, sizeof(Upsert));
drhfcfd7562018-04-12 21:42:51 +000065 if( pNew==0 ){
drhfcfd7562018-04-12 21:42:51 +000066 sqlite3ExprListDelete(db, pTarget);
drhe9c2e772018-04-13 13:06:45 +000067 sqlite3ExprDelete(db, pTargetWhere);
drhfcfd7562018-04-12 21:42:51 +000068 sqlite3ExprListDelete(db, pSet);
69 sqlite3ExprDelete(db, pWhere);
drh2549e4c2020-12-08 14:29:03 +000070 sqlite3UpsertDelete(db, pNext);
drhfcfd7562018-04-12 21:42:51 +000071 return 0;
72 }else{
73 pNew->pUpsertTarget = pTarget;
drhe9c2e772018-04-13 13:06:45 +000074 pNew->pUpsertTargetWhere = pTargetWhere;
drhfcfd7562018-04-12 21:42:51 +000075 pNew->pUpsertSet = pSet;
drhfcfd7562018-04-12 21:42:51 +000076 pNew->pUpsertWhere = pWhere;
drh255c1c12020-12-12 00:28:15 +000077 pNew->isDoUpdate = pSet!=0;
drh2549e4c2020-12-08 14:29:03 +000078 pNew->pNextUpsert = pNext;
drhfcfd7562018-04-12 21:42:51 +000079 }
80 return pNew;
81}
82
drh788d55a2018-04-13 01:15:09 +000083/*
drhe9c2e772018-04-13 13:06:45 +000084** Analyze the ON CONFLICT clause described by pUpsert. Resolve all
85** symbols in the conflict-target.
drh788d55a2018-04-13 01:15:09 +000086**
drhe9c2e772018-04-13 13:06:45 +000087** Return SQLITE_OK if everything works, or an error code is something
88** is wrong.
drh788d55a2018-04-13 01:15:09 +000089*/
drhe9c2e772018-04-13 13:06:45 +000090int sqlite3UpsertAnalyzeTarget(
drh788d55a2018-04-13 01:15:09 +000091 Parse *pParse, /* The parsing context */
92 SrcList *pTabList, /* Table into which we are inserting */
drhe9c2e772018-04-13 13:06:45 +000093 Upsert *pUpsert /* The ON CONFLICT clauses */
drh788d55a2018-04-13 01:15:09 +000094){
drhd5af5422018-04-13 14:27:01 +000095 Table *pTab; /* That table into which we are inserting */
96 int rc; /* Result code */
97 int iCursor; /* Cursor used by pTab */
98 Index *pIdx; /* One of the indexes of pTab */
99 ExprList *pTarget; /* The conflict-target clause */
100 Expr *pTerm; /* One term of the conflict-target clause */
101 NameContext sNC; /* Context for resolving symbolic names */
102 Expr sCol[2]; /* Index column converted into an Expr */
drh2549e4c2020-12-08 14:29:03 +0000103 int nClause = 0; /* Counter of ON CONFLICT clauses */
drh788d55a2018-04-13 01:15:09 +0000104
105 assert( pTabList->nSrc==1 );
106 assert( pTabList->a[0].pTab!=0 );
drhe9c2e772018-04-13 13:06:45 +0000107 assert( pUpsert!=0 );
108 assert( pUpsert->pUpsertTarget!=0 );
109
110 /* Resolve all symbolic names in the conflict-target clause, which
111 ** includes both the list of columns and the optional partial-index
112 ** WHERE clause.
113 */
drh788d55a2018-04-13 01:15:09 +0000114 memset(&sNC, 0, sizeof(sNC));
115 sNC.pParse = pParse;
116 sNC.pSrcList = pTabList;
drh2549e4c2020-12-08 14:29:03 +0000117 for(; pUpsert && pUpsert->pUpsertTarget;
118 pUpsert=pUpsert->pNextUpsert, nClause++){
119 rc = sqlite3ResolveExprListNames(&sNC, pUpsert->pUpsertTarget);
120 if( rc ) return rc;
121 rc = sqlite3ResolveExprNames(&sNC, pUpsert->pUpsertTargetWhere);
122 if( rc ) return rc;
123
124 /* Check to see if the conflict target matches the rowid. */
125 pTab = pTabList->a[0].pTab;
126 pTarget = pUpsert->pUpsertTarget;
127 iCursor = pTabList->a[0].iCursor;
128 if( HasRowid(pTab)
129 && pTarget->nExpr==1
130 && (pTerm = pTarget->a[0].pExpr)->op==TK_COLUMN
131 && pTerm->iColumn==XN_ROWID
132 ){
133 /* The conflict-target is the rowid of the primary table */
134 assert( pUpsert->pUpsertIdx==0 );
drh3b45d8b2018-04-13 13:44:48 +0000135 continue;
136 }
drh2549e4c2020-12-08 14:29:03 +0000137
138 /* Initialize sCol[0..1] to be an expression parse tree for a
139 ** single column of an index. The sCol[0] node will be the TK_COLLATE
140 ** operator and sCol[1] will be the TK_COLUMN operator. Code below
141 ** will populate the specific collation and column number values
142 ** prior to comparing against the conflict-target expression.
143 */
144 memset(sCol, 0, sizeof(sCol));
145 sCol[0].op = TK_COLLATE;
146 sCol[0].pLeft = &sCol[1];
147 sCol[1].op = TK_COLUMN;
148 sCol[1].iTable = pTabList->a[0].iCursor;
149
150 /* Check for matches against other indexes */
151 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
152 int ii, jj, nn;
153 if( !IsUniqueIndex(pIdx) ) continue;
154 if( pTarget->nExpr!=pIdx->nKeyCol ) continue;
155 if( pIdx->pPartIdxWhere ){
156 if( pUpsert->pUpsertTargetWhere==0 ) continue;
157 if( sqlite3ExprCompare(pParse, pUpsert->pUpsertTargetWhere,
158 pIdx->pPartIdxWhere, iCursor)!=0 ){
159 continue;
160 }
161 }
162 nn = pIdx->nKeyCol;
163 for(ii=0; ii<nn; ii++){
164 Expr *pExpr;
165 sCol[0].u.zToken = (char*)pIdx->azColl[ii];
166 if( pIdx->aiColumn[ii]==XN_EXPR ){
167 assert( pIdx->aColExpr!=0 );
168 assert( pIdx->aColExpr->nExpr>ii );
169 pExpr = pIdx->aColExpr->a[ii].pExpr;
170 if( pExpr->op!=TK_COLLATE ){
171 sCol[0].pLeft = pExpr;
172 pExpr = &sCol[0];
173 }
174 }else{
175 sCol[0].pLeft = &sCol[1];
176 sCol[1].iColumn = pIdx->aiColumn[ii];
177 pExpr = &sCol[0];
178 }
179 for(jj=0; jj<nn; jj++){
180 if( sqlite3ExprCompare(pParse,pTarget->a[jj].pExpr,pExpr,iCursor)<2 ){
181 break; /* Column ii of the index matches column jj of target */
182 }
183 }
184 if( jj>=nn ){
185 /* The target contains no match for column jj of the index */
186 break;
187 }
188 }
189 if( ii<nn ){
190 /* Column ii of the index did not match any term of the conflict target.
191 ** Continue the search with the next index. */
192 continue;
193 }
194 pUpsert->pUpsertIdx = pIdx;
195 break;
196 }
197 if( pUpsert->pUpsertIdx==0 ){
198 char zWhich[16];
199 if( nClause==0 && pUpsert->pNextUpsert==0 ){
200 zWhich[0] = 0;
201 }else{
202 sqlite3_snprintf(sizeof(zWhich),zWhich,"%r ", nClause+1);
203 }
204 sqlite3ErrorMsg(pParse, "%sON CONFLICT clause does not match any "
205 "PRIMARY KEY or UNIQUE constraint", zWhich);
206 return SQLITE_ERROR;
207 }
drh788d55a2018-04-13 01:15:09 +0000208 }
drh2549e4c2020-12-08 14:29:03 +0000209 return SQLITE_OK;
drh788d55a2018-04-13 01:15:09 +0000210}
211
drh9eddaca2018-04-13 18:59:17 +0000212/*
drh61e280a2020-12-11 01:17:06 +0000213** Return true if pUpsert is the last ON CONFLICT clause with a
214** conflict target, or if pUpsert is followed by another ON CONFLICT
215** clause that targets the INTEGER PRIMARY KEY.
216*/
217int sqlite3UpsertNextIsIPK(Upsert *pUpsert){
218 Upsert *pNext;
drh1c198482020-12-14 13:52:03 +0000219 if( NEVER(pUpsert==0) ) return 0;
drh61e280a2020-12-11 01:17:06 +0000220 pNext = pUpsert->pNextUpsert;
221 if( pNext==0 ) return 1;
222 if( pNext->pUpsertTarget==0 ) return 1;
223 if( pNext->pUpsertIdx==0 ) return 1;
224 return 0;
225}
226
227/*
228** Given the list of ON CONFLICT clauses described by pUpsert, and
229** a particular index pIdx, return a pointer to the particular ON CONFLICT
230** clause that applies to the index. Or, if the index is not subject to
231** any ON CONFLICT clause, return NULL.
232*/
233Upsert *sqlite3UpsertOfIndex(Upsert *pUpsert, Index *pIdx){
234 while(
235 pUpsert
236 && pUpsert->pUpsertTarget!=0
237 && pUpsert->pUpsertIdx!=pIdx
238 ){
239 pUpsert = pUpsert->pNextUpsert;
240 }
241 return pUpsert;
242}
243
244/*
drh9eddaca2018-04-13 18:59:17 +0000245** Generate bytecode that does an UPDATE as part of an upsert.
dan2cc00422018-04-17 18:16:10 +0000246**
247** If pIdx is NULL, then the UNIQUE constraint that failed was the IPK.
248** In this case parameter iCur is a cursor open on the table b-tree that
249** currently points to the conflicting table row. Otherwise, if pIdx
250** is not NULL, then pIdx is the constraint that failed and iCur is a
251** cursor points to the conflicting row.
drh9eddaca2018-04-13 18:59:17 +0000252*/
253void sqlite3UpsertDoUpdate(
254 Parse *pParse, /* The parsing and code-generating context */
255 Upsert *pUpsert, /* The ON CONFLICT clause for the upsert */
256 Table *pTab, /* The table being updated */
257 Index *pIdx, /* The UNIQUE constraint that failed */
dan2cc00422018-04-17 18:16:10 +0000258 int iCur /* Cursor for pIdx (or pTab if pIdx==NULL) */
drh9eddaca2018-04-13 18:59:17 +0000259){
260 Vdbe *v = pParse->pVdbe;
drh0b30a112018-04-13 21:55:22 +0000261 sqlite3 *db = pParse->db;
drh0b30a112018-04-13 21:55:22 +0000262 SrcList *pSrc; /* FROM clause for the UPDATE */
drhc4ceea72018-08-21 12:16:33 +0000263 int iDataCur;
drha7ce1672019-08-30 23:15:00 +0000264 int i;
drh91f27172020-12-10 12:49:26 +0000265 Upsert *pTop = pUpsert;
drh0b30a112018-04-13 21:55:22 +0000266
drh9eddaca2018-04-13 18:59:17 +0000267 assert( v!=0 );
drhc4ceea72018-08-21 12:16:33 +0000268 assert( pUpsert!=0 );
drhc4ceea72018-08-21 12:16:33 +0000269 iDataCur = pUpsert->iDataCur;
drh61e280a2020-12-11 01:17:06 +0000270 pUpsert = sqlite3UpsertOfIndex(pTop, pIdx);
drh91f27172020-12-10 12:49:26 +0000271 VdbeNoopComment((v, "Begin DO UPDATE of UPSERT"));
drhfb2213e2018-04-20 15:56:24 +0000272 if( pIdx && iCur!=iDataCur ){
273 if( HasRowid(pTab) ){
274 int regRowid = sqlite3GetTempReg(pParse);
275 sqlite3VdbeAddOp2(v, OP_IdxRowid, iCur, regRowid);
276 sqlite3VdbeAddOp3(v, OP_SeekRowid, iDataCur, 0, regRowid);
277 VdbeCoverage(v);
278 sqlite3ReleaseTempReg(pParse, regRowid);
drh0b30a112018-04-13 21:55:22 +0000279 }else{
drhfb2213e2018-04-20 15:56:24 +0000280 Index *pPk = sqlite3PrimaryKeyIndex(pTab);
281 int nPk = pPk->nKeyCol;
282 int iPk = pParse->nMem+1;
drhfb2213e2018-04-20 15:56:24 +0000283 pParse->nMem += nPk;
284 for(i=0; i<nPk; i++){
285 int k;
286 assert( pPk->aiColumn[i]>=0 );
drhb9bcf7c2019-10-19 13:29:10 +0000287 k = sqlite3TableColumnToIndex(pIdx, pPk->aiColumn[i]);
drhfb2213e2018-04-20 15:56:24 +0000288 sqlite3VdbeAddOp3(v, OP_Column, iCur, k, iPk+i);
drh9cadb232018-04-20 18:01:31 +0000289 VdbeComment((v, "%s.%s", pIdx->zName,
drhcf9d36d2021-08-02 18:03:43 +0000290 pTab->aCol[pPk->aiColumn[i]].zCnName));
drhe966a362018-04-14 22:35:34 +0000291 }
drh4031baf2018-05-28 17:31:20 +0000292 sqlite3VdbeVerifyAbortable(v, OE_Abort);
drhfb2213e2018-04-20 15:56:24 +0000293 i = sqlite3VdbeAddOp4Int(v, OP_Found, iDataCur, 0, iPk, nPk);
294 VdbeCoverage(v);
drh9cadb232018-04-20 18:01:31 +0000295 sqlite3VdbeAddOp4(v, OP_Halt, SQLITE_CORRUPT, OE_Abort, 0,
296 "corrupt database", P4_STATIC);
drhfe2a3f12019-12-26 23:40:33 +0000297 sqlite3MayAbort(pParse);
drhfb2213e2018-04-20 15:56:24 +0000298 sqlite3VdbeJumpHere(v, i);
drhe966a362018-04-14 22:35:34 +0000299 }
drh0b30a112018-04-13 21:55:22 +0000300 }
drh91f27172020-12-10 12:49:26 +0000301 /* pUpsert does not own pTop->pUpsertSrc - the outer INSERT statement does.
302 ** So we have to make a copy before passing it down into sqlite3Update() */
303 pSrc = sqlite3SrcListDup(db, pTop->pUpsertSrc, 0);
drha7ce1672019-08-30 23:15:00 +0000304 /* excluded.* columns of type REAL need to be converted to a hard real */
305 for(i=0; i<pTab->nCol; i++){
306 if( pTab->aCol[i].affinity==SQLITE_AFF_REAL ){
drh91f27172020-12-10 12:49:26 +0000307 sqlite3VdbeAddOp1(v, OP_RealAffinity, pTop->regData+i);
drha7ce1672019-08-30 23:15:00 +0000308 }
309 }
drh255c1c12020-12-12 00:28:15 +0000310 sqlite3Update(pParse, pSrc, sqlite3ExprListDup(db,pUpsert->pUpsertSet,0),
311 sqlite3ExprDup(db,pUpsert->pUpsertWhere,0), OE_Abort, 0, 0, pUpsert);
drh9eddaca2018-04-13 18:59:17 +0000312 VdbeNoopComment((v, "End DO UPDATE of UPSERT"));
313}
314
drhfcfd7562018-04-12 21:42:51 +0000315#endif /* SQLITE_OMIT_UPSERT */