4 ** The author disclaims copyright to this source code. In place of
5 ** a legal notice, here is a blessing:
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.
11 *************************************************************************
12 ** This file contains C code routines that are called by the parser
13 ** to handle UPDATE statements.
15 #include "sqliteInt.h"
17 #ifndef SQLITE_OMIT_VIRTUALTABLE
18 /* Forward declaration */
19 static void updateVirtualTable(
20 Parse
*pParse
, /* The parsing context */
21 SrcList
*pSrc
, /* The virtual table to be modified */
22 Table
*pTab
, /* The virtual table */
23 ExprList
*pChanges
, /* The columns to change in the UPDATE statement */
24 Expr
*pRowidExpr
, /* Expression used to recompute the rowid */
25 int *aXRef
, /* Mapping from columns of pTab to entries in pChanges */
26 Expr
*pWhere
, /* WHERE clause of the UPDATE statement */
27 int onError
/* ON CONFLICT strategy */
29 #endif /* SQLITE_OMIT_VIRTUALTABLE */
32 ** The most recently coded instruction was an OP_Column to retrieve the
33 ** i-th column of table pTab. This routine sets the P4 parameter of the
34 ** OP_Column to the default value, if any.
36 ** The default value of a column is specified by a DEFAULT clause in the
37 ** column definition. This was either supplied by the user when the table
38 ** was created, or added later to the table definition by an ALTER TABLE
39 ** command. If the latter, then the row-records in the table btree on disk
40 ** may not contain a value for the column and the default value, taken
41 ** from the P4 parameter of the OP_Column instruction, is returned instead.
42 ** If the former, then all row-records are guaranteed to include a value
43 ** for the column and the P4 value is not required.
45 ** Column definitions created by an ALTER TABLE command may only have
46 ** literal default values specified: a number, null or a string. (If a more
47 ** complicated default expression value was provided, it is evaluated
48 ** when the ALTER TABLE is executed and one of the literal values written
49 ** into the sqlite_schema table.)
51 ** Therefore, the P4 parameter is only required if the default value for
52 ** the column is a literal number, string or null. The sqlite3ValueFromExpr()
53 ** function is capable of transforming these types of expressions into
54 ** sqlite3_value objects.
56 ** If column as REAL affinity and the table is an ordinary b-tree table
57 ** (not a virtual table) then the value might have been stored as an
58 ** integer. In that case, add an OP_RealAffinity opcode to make sure
59 ** it has been converted into REAL.
61 void sqlite3ColumnDefault(Vdbe
*v
, Table
*pTab
, int i
, int iReg
){
64 assert( pTab
->nCol
>i
);
65 pCol
= &pTab
->aCol
[i
];
67 sqlite3_value
*pValue
= 0;
68 u8 enc
= ENC(sqlite3VdbeDb(v
));
69 assert( !IsView(pTab
) );
70 VdbeComment((v
, "%s.%s", pTab
->zName
, pCol
->zCnName
));
71 assert( i
<pTab
->nCol
);
72 sqlite3ValueFromExpr(sqlite3VdbeDb(v
),
73 sqlite3ColumnExpr(pTab
,pCol
), enc
,
74 pCol
->affinity
, &pValue
);
76 sqlite3VdbeAppendP4(v
, pValue
, P4_MEM
);
79 #ifndef SQLITE_OMIT_FLOATING_POINT
80 if( pCol
->affinity
==SQLITE_AFF_REAL
&& !IsVirtual(pTab
) ){
81 sqlite3VdbeAddOp1(v
, OP_RealAffinity
, iReg
);
87 ** Check to see if column iCol of index pIdx references any of the
88 ** columns defined by aXRef and chngRowid. Return true if it does
89 ** and false if not. This is an optimization. False-positives are a
90 ** performance degradation, but false-negatives can result in a corrupt
91 ** index and incorrect answers.
93 ** aXRef[j] will be non-negative if column j of the original table is
94 ** being updated. chngRowid will be true if the rowid of the table is
97 static int indexColumnIsBeingUpdated(
98 Index
*pIdx
, /* The index to check */
99 int iCol
, /* Which column of the index to check */
100 int *aXRef
, /* aXRef[j]>=0 if column j is being updated */
101 int chngRowid
/* true if the rowid is being updated */
103 i16 iIdxCol
= pIdx
->aiColumn
[iCol
];
104 assert( iIdxCol
!=XN_ROWID
); /* Cannot index rowid */
106 return aXRef
[iIdxCol
]>=0;
108 assert( iIdxCol
==XN_EXPR
);
109 assert( pIdx
->aColExpr
!=0 );
110 assert( pIdx
->aColExpr
->a
[iCol
].pExpr
!=0 );
111 return sqlite3ExprReferencesUpdatedColumn(pIdx
->aColExpr
->a
[iCol
].pExpr
,
116 ** Check to see if index pIdx is a partial index whose conditional
117 ** expression might change values due to an UPDATE. Return true if
118 ** the index is subject to change and false if the index is guaranteed
119 ** to be unchanged. This is an optimization. False-positives are a
120 ** performance degradation, but false-negatives can result in a corrupt
121 ** index and incorrect answers.
123 ** aXRef[j] will be non-negative if column j of the original table is
124 ** being updated. chngRowid will be true if the rowid of the table is
127 static int indexWhereClauseMightChange(
128 Index
*pIdx
, /* The index to check */
129 int *aXRef
, /* aXRef[j]>=0 if column j is being updated */
130 int chngRowid
/* true if the rowid is being updated */
132 if( pIdx
->pPartIdxWhere
==0 ) return 0;
133 return sqlite3ExprReferencesUpdatedColumn(pIdx
->pPartIdxWhere
,
138 ** Allocate and return a pointer to an expression of type TK_ROW with
139 ** Expr.iColumn set to value (iCol+1). The resolver will modify the
140 ** expression to be a TK_COLUMN reading column iCol of the first
141 ** table in the source-list (pSrc->a[0]).
143 static Expr
*exprRowColumn(Parse
*pParse
, int iCol
){
144 Expr
*pRet
= sqlite3PExpr(pParse
, TK_ROW
, 0, 0);
145 if( pRet
) pRet
->iColumn
= iCol
+1;
150 ** Assuming both the pLimit and pOrderBy parameters are NULL, this function
151 ** generates VM code to run the query:
153 ** SELECT <other-columns>, pChanges FROM pTabList WHERE pWhere
155 ** and write the results to the ephemeral table already opened as cursor
156 ** iEph. None of pChanges, pTabList or pWhere are modified or consumed by
157 ** this function, they must be deleted by the caller.
159 ** Or, if pLimit and pOrderBy are not NULL, and pTab is not a view:
161 ** SELECT <other-columns>, pChanges FROM pTabList
163 ** GROUP BY <other-columns>
164 ** ORDER BY pOrderBy LIMIT pLimit
166 ** If pTab is a view, the GROUP BY clause is omitted.
168 ** Exactly how results are written to table iEph, and exactly what
169 ** the <other-columns> in the query above are is determined by the type
170 ** of table pTabList->a[0].pTab.
172 ** If the table is a WITHOUT ROWID table, then argument pPk must be its
173 ** PRIMARY KEY. In this case <other-columns> are the primary key columns
174 ** of the table, in order. The results of the query are written to ephemeral
175 ** table iEph as index keys, using OP_IdxInsert.
177 ** If the table is actually a view, then <other-columns> are all columns of
178 ** the view. The results are written to the ephemeral table iEph as records
179 ** with automatically assigned integer keys.
181 ** If the table is a virtual or ordinary intkey table, then <other-columns>
182 ** is its rowid. For a virtual table, the results are written to iEph as
183 ** records with automatically assigned integer keys For intkey tables, the
184 ** rowid value in <other-columns> is used as the integer key, and the
185 ** remaining fields make up the table record.
187 static void updateFromSelect(
188 Parse
*pParse
, /* Parse context */
189 int iEph
, /* Cursor for open eph. table */
190 Index
*pPk
, /* PK if table 0 is WITHOUT ROWID */
191 ExprList
*pChanges
, /* List of expressions to return */
192 SrcList
*pTabList
, /* List of tables to select from */
193 Expr
*pWhere
, /* WHERE clause for query */
194 ExprList
*pOrderBy
, /* ORDER BY clause */
195 Expr
*pLimit
/* LIMIT clause */
203 ExprList
*pOrderBy2
= 0;
204 sqlite3
*db
= pParse
->db
;
205 Table
*pTab
= pTabList
->a
[0].pTab
;
210 #ifdef SQLITE_ENABLE_UPDATE_DELETE_LIMIT
211 if( pOrderBy
&& pLimit
==0 ) {
212 sqlite3ErrorMsg(pParse
, "ORDER BY without LIMIT on UPDATE");
215 pOrderBy2
= sqlite3ExprListDup(db
, pOrderBy
, 0);
216 pLimit2
= sqlite3ExprDup(db
, pLimit
, 0);
218 UNUSED_PARAMETER(pOrderBy
);
219 UNUSED_PARAMETER(pLimit
);
222 pSrc
= sqlite3SrcListDup(db
, pTabList
, 0);
223 pWhere2
= sqlite3ExprDup(db
, pWhere
, 0);
225 assert( pTabList
->nSrc
>1 );
227 assert( pSrc
->a
[0].fg
.notCte
);
228 pSrc
->a
[0].iCursor
= -1;
229 pSrc
->a
[0].pTab
->nTabRef
--;
233 for(i
=0; i
<pPk
->nKeyCol
; i
++){
234 Expr
*pNew
= exprRowColumn(pParse
, pPk
->aiColumn
[i
]);
235 #ifdef SQLITE_ENABLE_UPDATE_DELETE_LIMIT
237 pGrp
= sqlite3ExprListAppend(pParse
, pGrp
, sqlite3ExprDup(db
, pNew
, 0));
240 pList
= sqlite3ExprListAppend(pParse
, pList
, pNew
);
242 eDest
= IsVirtual(pTab
) ? SRT_Table
: SRT_Upfrom
;
243 }else if( IsView(pTab
) ){
244 for(i
=0; i
<pTab
->nCol
; i
++){
245 pList
= sqlite3ExprListAppend(pParse
, pList
, exprRowColumn(pParse
, i
));
249 eDest
= IsVirtual(pTab
) ? SRT_Table
: SRT_Upfrom
;
250 pList
= sqlite3ExprListAppend(pParse
, 0, sqlite3PExpr(pParse
,TK_ROW
,0,0));
251 #ifdef SQLITE_ENABLE_UPDATE_DELETE_LIMIT
253 pGrp
= sqlite3ExprListAppend(pParse
, 0, sqlite3PExpr(pParse
,TK_ROW
,0,0));
257 assert( pChanges
!=0 || pParse
->db
->mallocFailed
);
259 for(i
=0; i
<pChanges
->nExpr
; i
++){
260 pList
= sqlite3ExprListAppend(pParse
, pList
,
261 sqlite3ExprDup(db
, pChanges
->a
[i
].pExpr
, 0)
265 pSelect
= sqlite3SelectNew(pParse
, pList
,
266 pSrc
, pWhere2
, pGrp
, 0, pOrderBy2
,
267 SF_UFSrcCheck
|SF_IncludeHidden
|SF_UpdateFrom
, pLimit2
269 if( pSelect
) pSelect
->selFlags
|= SF_OrderByReqd
;
270 sqlite3SelectDestInit(&dest
, eDest
, iEph
);
271 dest
.iSDParm2
= (pPk
? pPk
->nKeyCol
: -1);
272 sqlite3Select(pParse
, pSelect
, &dest
);
273 sqlite3SelectDelete(db
, pSelect
);
277 ** Process an UPDATE statement.
279 ** UPDATE OR IGNORE tbl SET a=b, c=d FROM tbl2... WHERE e<5 AND f NOT NULL;
280 ** \_______/ \_/ \______/ \_____/ \________________/
281 ** onError | pChanges | pWhere
282 ** \_______________________/
286 Parse
*pParse
, /* The parser context */
287 SrcList
*pTabList
, /* The table in which we should change things */
288 ExprList
*pChanges
, /* Things to be changed */
289 Expr
*pWhere
, /* The WHERE clause. May be null */
290 int onError
, /* How to handle constraint errors */
291 ExprList
*pOrderBy
, /* ORDER BY clause. May be null */
292 Expr
*pLimit
, /* LIMIT clause. May be null */
293 Upsert
*pUpsert
/* ON CONFLICT clause, or null */
295 int i
, j
, k
; /* Loop counters */
296 Table
*pTab
; /* The table to be updated */
297 int addrTop
= 0; /* VDBE instruction address of the start of the loop */
298 WhereInfo
*pWInfo
= 0; /* Information about the WHERE clause */
299 Vdbe
*v
; /* The virtual database engine */
300 Index
*pIdx
; /* For looping over indices */
301 Index
*pPk
; /* The PRIMARY KEY index for WITHOUT ROWID tables */
302 int nIdx
; /* Number of indices that need updating */
303 int nAllIdx
; /* Total number of indexes */
304 int iBaseCur
; /* Base cursor number */
305 int iDataCur
; /* Cursor for the canonical data btree */
306 int iIdxCur
; /* Cursor for the first index */
307 sqlite3
*db
; /* The database structure */
308 int *aRegIdx
= 0; /* Registers for to each index and the main table */
309 int *aXRef
= 0; /* aXRef[i] is the index in pChanges->a[] of the
310 ** an expression for the i-th column of the table.
311 ** aXRef[i]==-1 if the i-th column is not changed. */
312 u8
*aToOpen
; /* 1 for tables and indices to be opened */
313 u8 chngPk
; /* PRIMARY KEY changed in a WITHOUT ROWID table */
314 u8 chngRowid
; /* Rowid changed in a normal table */
315 u8 chngKey
; /* Either chngPk or chngRowid */
316 Expr
*pRowidExpr
= 0; /* Expression defining the new record number */
317 int iRowidExpr
= -1; /* Index of "rowid=" (or IPK) assignment in pChanges */
318 AuthContext sContext
; /* The authorization context */
319 NameContext sNC
; /* The name-context to resolve expressions in */
320 int iDb
; /* Database containing the table being updated */
321 int eOnePass
; /* ONEPASS_XXX value from where.c */
322 int hasFK
; /* True if foreign key processing is required */
323 int labelBreak
; /* Jump here to break out of UPDATE loop */
324 int labelContinue
; /* Jump here to continue next step of UPDATE loop */
325 int flags
; /* Flags for sqlite3WhereBegin() */
327 #ifndef SQLITE_OMIT_TRIGGER
328 int isView
; /* True when updating a view (INSTEAD OF trigger) */
329 Trigger
*pTrigger
; /* List of triggers on pTab, if required */
330 int tmask
; /* Mask of TRIGGER_BEFORE|TRIGGER_AFTER */
332 int newmask
; /* Mask of NEW.* columns accessed by BEFORE triggers */
333 int iEph
= 0; /* Ephemeral table holding all primary key values */
334 int nKey
= 0; /* Number of elements in regKey for WITHOUT ROWID */
335 int aiCurOnePass
[2]; /* The write cursors opened by WHERE_ONEPASS */
336 int addrOpen
= 0; /* Address of OP_OpenEphemeral */
337 int iPk
= 0; /* First of nPk cells holding PRIMARY KEY value */
338 i16 nPk
= 0; /* Number of components of the PRIMARY KEY */
339 int bReplace
= 0; /* True if REPLACE conflict resolution might happen */
340 int bFinishSeek
= 1; /* The OP_FinishSeek opcode is needed */
341 int nChangeFrom
= 0; /* If there is a FROM, pChanges->nExpr, else 0 */
343 /* Register Allocations */
344 int regRowCount
= 0; /* A count of rows changed */
345 int regOldRowid
= 0; /* The old rowid */
346 int regNewRowid
= 0; /* The new rowid */
347 int regNew
= 0; /* Content of the NEW.* table in triggers */
348 int regOld
= 0; /* Content of OLD.* table in triggers */
349 int regRowSet
= 0; /* Rowset of rows to be updated */
350 int regKey
= 0; /* composite PRIMARY KEY value */
352 memset(&sContext
, 0, sizeof(sContext
));
354 assert( db
->pParse
==pParse
);
358 assert( db
->mallocFailed
==0 );
360 /* Locate the table which we want to update.
362 pTab
= sqlite3SrcListLookup(pParse
, pTabList
);
363 if( pTab
==0 ) goto update_cleanup
;
364 iDb
= sqlite3SchemaToIndex(pParse
->db
, pTab
->pSchema
);
366 /* Figure out if we have any triggers and if the table being
367 ** updated is a view.
369 #ifndef SQLITE_OMIT_TRIGGER
370 pTrigger
= sqlite3TriggersExist(pParse
, pTab
, TK_UPDATE
, pChanges
, &tmask
);
371 isView
= IsView(pTab
);
372 assert( pTrigger
|| tmask
==0 );
378 #ifdef SQLITE_OMIT_VIEW
383 #if TREETRACE_ENABLED
384 if( sqlite3TreeTrace
& 0x10000 ){
385 sqlite3TreeViewLine(0, "In sqlite3Update() at %s:%d", __FILE__
, __LINE__
);
386 sqlite3TreeViewUpdate(pParse
->pWith
, pTabList
, pChanges
, pWhere
,
387 onError
, pOrderBy
, pLimit
, pUpsert
, pTrigger
);
391 /* If there was a FROM clause, set nChangeFrom to the number of expressions
392 ** in the change-list. Otherwise, set it to 0. There cannot be a FROM
393 ** clause if this function is being called to generate code for part of
394 ** an UPSERT statement. */
395 nChangeFrom
= (pTabList
->nSrc
>1) ? pChanges
->nExpr
: 0;
396 assert( nChangeFrom
==0 || pUpsert
==0 );
398 #ifdef SQLITE_ENABLE_UPDATE_DELETE_LIMIT
399 if( !isView
&& nChangeFrom
==0 ){
400 pWhere
= sqlite3LimitWhere(
401 pParse
, pTabList
, pWhere
, pOrderBy
, pLimit
, "UPDATE"
408 if( sqlite3ViewGetColumnNames(pParse
, pTab
) ){
411 if( sqlite3IsReadOnly(pParse
, pTab
, pTrigger
) ){
415 /* Allocate a cursors for the main database table and for all indices.
416 ** The index cursors might not be used, but if they are used they
417 ** need to occur right after the database cursor. So go ahead and
418 ** allocate enough space, just in case.
420 iBaseCur
= iDataCur
= pParse
->nTab
++;
421 iIdxCur
= iDataCur
+1;
422 pPk
= HasRowid(pTab
) ? 0 : sqlite3PrimaryKeyIndex(pTab
);
423 testcase( pPk
!=0 && pPk
!=pTab
->pIndex
);
424 for(nIdx
=0, pIdx
=pTab
->pIndex
; pIdx
; pIdx
=pIdx
->pNext
, nIdx
++){
426 iDataCur
= pParse
->nTab
;
431 /* On an UPSERT, reuse the same cursors already opened by INSERT */
432 iDataCur
= pUpsert
->iDataCur
;
433 iIdxCur
= pUpsert
->iIdxCur
;
434 pParse
->nTab
= iBaseCur
;
436 pTabList
->a
[0].iCursor
= iDataCur
;
438 /* Allocate space for aXRef[], aRegIdx[], and aToOpen[].
439 ** Initialize aXRef[] and aToOpen[] to their default values.
441 aXRef
= sqlite3DbMallocRawNN(db
, sizeof(int) * (pTab
->nCol
+nIdx
+1) + nIdx
+2 );
442 if( aXRef
==0 ) goto update_cleanup
;
443 aRegIdx
= aXRef
+pTab
->nCol
;
444 aToOpen
= (u8
*)(aRegIdx
+nIdx
+1);
445 memset(aToOpen
, 1, nIdx
+1);
447 for(i
=0; i
<pTab
->nCol
; i
++) aXRef
[i
] = -1;
449 /* Initialize the name-context */
450 memset(&sNC
, 0, sizeof(sNC
));
452 sNC
.pSrcList
= pTabList
;
453 sNC
.uNC
.pUpsert
= pUpsert
;
454 sNC
.ncFlags
= NC_UUpsert
;
456 /* Begin generating code. */
457 v
= sqlite3GetVdbe(pParse
);
458 if( v
==0 ) goto update_cleanup
;
460 /* Resolve the column names in all the expressions of the
461 ** of the UPDATE statement. Also find the column index
462 ** for each column to be updated in the pChanges array. For each
463 ** column to be updated, make sure we have authorization to change
466 chngRowid
= chngPk
= 0;
467 for(i
=0; i
<pChanges
->nExpr
; i
++){
468 u8 hCol
= sqlite3StrIHash(pChanges
->a
[i
].zEName
);
469 /* If this is an UPDATE with a FROM clause, do not resolve expressions
470 ** here. The call to sqlite3Select() below will do that. */
471 if( nChangeFrom
==0 && sqlite3ResolveExprNames(&sNC
, pChanges
->a
[i
].pExpr
) ){
474 for(j
=0; j
<pTab
->nCol
; j
++){
475 if( pTab
->aCol
[j
].hName
==hCol
476 && sqlite3StrICmp(pTab
->aCol
[j
].zCnName
, pChanges
->a
[i
].zEName
)==0
478 if( j
==pTab
->iPKey
){
480 pRowidExpr
= pChanges
->a
[i
].pExpr
;
482 }else if( pPk
&& (pTab
->aCol
[j
].colFlags
& COLFLAG_PRIMKEY
)!=0 ){
485 #ifndef SQLITE_OMIT_GENERATED_COLUMNS
486 else if( pTab
->aCol
[j
].colFlags
& COLFLAG_GENERATED
){
487 testcase( pTab
->aCol
[j
].colFlags
& COLFLAG_VIRTUAL
);
488 testcase( pTab
->aCol
[j
].colFlags
& COLFLAG_STORED
);
489 sqlite3ErrorMsg(pParse
,
490 "cannot UPDATE generated column \"%s\"",
491 pTab
->aCol
[j
].zCnName
);
500 if( pPk
==0 && sqlite3IsRowid(pChanges
->a
[i
].zEName
) ){
503 pRowidExpr
= pChanges
->a
[i
].pExpr
;
506 sqlite3ErrorMsg(pParse
, "no such column: %s", pChanges
->a
[i
].zEName
);
507 pParse
->checkSchema
= 1;
511 #ifndef SQLITE_OMIT_AUTHORIZATION
514 rc
= sqlite3AuthCheck(pParse
, SQLITE_UPDATE
, pTab
->zName
,
515 j
<0 ? "ROWID" : pTab
->aCol
[j
].zCnName
,
516 db
->aDb
[iDb
].zDbSName
);
517 if( rc
==SQLITE_DENY
){
519 }else if( rc
==SQLITE_IGNORE
){
525 assert( (chngRowid
& chngPk
)==0 );
526 assert( chngRowid
==0 || chngRowid
==1 );
527 assert( chngPk
==0 || chngPk
==1 );
528 chngKey
= chngRowid
+ chngPk
;
530 #ifndef SQLITE_OMIT_GENERATED_COLUMNS
531 /* Mark generated columns as changing if their generator expressions
532 ** reference any changing column. The actual aXRef[] value for
533 ** generated expressions is not used, other than to check to see that it
534 ** is non-negative, so the value of aXRef[] for generated columns can be
535 ** set to any non-negative number. We use 99999 so that the value is
536 ** obvious when looking at aXRef[] in a symbolic debugger.
538 if( pTab
->tabFlags
& TF_HasGenerated
){
540 testcase( pTab
->tabFlags
& TF_HasVirtual
);
541 testcase( pTab
->tabFlags
& TF_HasStored
);
544 for(i
=0; i
<pTab
->nCol
; i
++){
545 if( aXRef
[i
]>=0 ) continue;
546 if( (pTab
->aCol
[i
].colFlags
& COLFLAG_GENERATED
)==0 ) continue;
547 if( sqlite3ExprReferencesUpdatedColumn(
548 sqlite3ColumnExpr(pTab
, &pTab
->aCol
[i
]),
559 /* The SET expressions are not actually used inside the WHERE loop.
560 ** So reset the colUsed mask. Unless this is a virtual table. In that
561 ** case, set all bits of the colUsed mask (to ensure that the virtual
562 ** table implementation makes all columns available).
564 pTabList
->a
[0].colUsed
= IsVirtual(pTab
) ? ALLBITS
: 0;
566 hasFK
= sqlite3FkRequired(pParse
, pTab
, aXRef
, chngKey
);
568 /* There is one entry in the aRegIdx[] array for each index on the table
569 ** being updated. Fill in aRegIdx[] with a register number that will hold
570 ** the key for accessing each index.
572 if( onError
==OE_Replace
) bReplace
= 1;
573 for(nAllIdx
=0, pIdx
=pTab
->pIndex
; pIdx
; pIdx
=pIdx
->pNext
, nAllIdx
++){
575 if( chngKey
|| hasFK
>1 || pIdx
==pPk
576 || indexWhereClauseMightChange(pIdx
,aXRef
,chngRowid
)
578 reg
= ++pParse
->nMem
;
579 pParse
->nMem
+= pIdx
->nColumn
;
582 for(i
=0; i
<pIdx
->nKeyCol
; i
++){
583 if( indexColumnIsBeingUpdated(pIdx
, i
, aXRef
, chngRowid
) ){
584 reg
= ++pParse
->nMem
;
585 pParse
->nMem
+= pIdx
->nColumn
;
586 if( onError
==OE_Default
&& pIdx
->onError
==OE_Replace
){
593 if( reg
==0 ) aToOpen
[nAllIdx
+1] = 0;
594 aRegIdx
[nAllIdx
] = reg
;
596 aRegIdx
[nAllIdx
] = ++pParse
->nMem
; /* Register storing the table record */
598 /* If REPLACE conflict resolution might be invoked, open cursors on all
599 ** indexes in case they are needed to delete records. */
600 memset(aToOpen
, 1, nIdx
+1);
603 if( pParse
->nested
==0 ) sqlite3VdbeCountChanges(v
);
604 sqlite3BeginWriteOperation(pParse
, pTrigger
|| hasFK
, iDb
);
606 /* Allocate required registers. */
607 if( !IsVirtual(pTab
) ){
608 /* For now, regRowSet and aRegIdx[nAllIdx] share the same register.
609 ** If regRowSet turns out to be needed, then aRegIdx[nAllIdx] will be
610 ** reallocated. aRegIdx[nAllIdx] is the register in which the main
611 ** table record is written. regRowSet holds the RowSet for the
612 ** two-pass update algorithm. */
613 assert( aRegIdx
[nAllIdx
]==pParse
->nMem
);
614 regRowSet
= aRegIdx
[nAllIdx
];
615 regOldRowid
= regNewRowid
= ++pParse
->nMem
;
616 if( chngPk
|| pTrigger
|| hasFK
){
617 regOld
= pParse
->nMem
+ 1;
618 pParse
->nMem
+= pTab
->nCol
;
620 if( chngKey
|| pTrigger
|| hasFK
){
621 regNewRowid
= ++pParse
->nMem
;
623 regNew
= pParse
->nMem
+ 1;
624 pParse
->nMem
+= pTab
->nCol
;
627 /* Start the view context. */
629 sqlite3AuthContextPush(pParse
, &sContext
, pTab
->zName
);
632 /* If we are trying to update a view, realize that view into
633 ** an ephemeral table.
635 #if !defined(SQLITE_OMIT_VIEW) && !defined(SQLITE_OMIT_TRIGGER)
636 if( nChangeFrom
==0 && isView
){
637 sqlite3MaterializeView(pParse
, pTab
,
638 pWhere
, pOrderBy
, pLimit
, iDataCur
645 /* Resolve the column names in all the expressions in the
648 if( nChangeFrom
==0 && sqlite3ResolveExprNames(&sNC
, pWhere
) ){
652 #ifndef SQLITE_OMIT_VIRTUALTABLE
653 /* Virtual tables must be handled separately */
654 if( IsVirtual(pTab
) ){
655 updateVirtualTable(pParse
, pTabList
, pTab
, pChanges
, pRowidExpr
, aXRef
,
661 /* Jump to labelBreak to abandon further processing of this UPDATE */
662 labelContinue
= labelBreak
= sqlite3VdbeMakeLabel(pParse
);
664 /* Not an UPSERT. Normal processing. Begin by
665 ** initialize the count of updated rows */
666 if( (db
->flags
&SQLITE_CountRows
)!=0
667 && !pParse
->pTriggerTab
669 && !pParse
->bReturning
672 regRowCount
= ++pParse
->nMem
;
673 sqlite3VdbeAddOp2(v
, OP_Integer
, 0, regRowCount
);
676 if( nChangeFrom
==0 && HasRowid(pTab
) ){
677 sqlite3VdbeAddOp3(v
, OP_Null
, 0, regRowSet
, regOldRowid
);
678 iEph
= pParse
->nTab
++;
679 addrOpen
= sqlite3VdbeAddOp3(v
, OP_OpenEphemeral
, iEph
, 0, regRowSet
);
681 assert( pPk
!=0 || HasRowid(pTab
) );
682 nPk
= pPk
? pPk
->nKeyCol
: 0;
683 iPk
= pParse
->nMem
+1;
685 pParse
->nMem
+= nChangeFrom
;
686 regKey
= ++pParse
->nMem
;
688 int nEphCol
= nPk
+ nChangeFrom
+ (isView
? pTab
->nCol
: 0);
689 iEph
= pParse
->nTab
++;
690 if( pPk
) sqlite3VdbeAddOp3(v
, OP_Null
, 0, iPk
, iPk
+nPk
-1);
691 addrOpen
= sqlite3VdbeAddOp2(v
, OP_OpenEphemeral
, iEph
, nEphCol
);
693 KeyInfo
*pKeyInfo
= sqlite3KeyInfoOfIndex(pParse
, pPk
);
695 pKeyInfo
->nAllField
= nEphCol
;
696 sqlite3VdbeAppendP4(v
, pKeyInfo
, P4_KEYINFO
);
701 pParse
, iEph
, pPk
, pChanges
, pTabList
, pWhere
, pOrderBy
, pLimit
703 #ifndef SQLITE_OMIT_SUBQUERY
704 if( isView
) iDataCur
= iEph
;
711 sqlite3MultiWrite(pParse
);
712 eOnePass
= ONEPASS_OFF
;
717 /* If this is an UPSERT, then all cursors have already been opened by
718 ** the outer INSERT and the data cursor should be pointing at the row
719 ** that is to be updated. So bypass the code that searches for the
720 ** row(s) to be updated.
723 eOnePass
= ONEPASS_SINGLE
;
724 sqlite3ExprIfFalse(pParse
, pWhere
, labelBreak
, SQLITE_JUMPIFNULL
);
727 /* Begin the database scan.
729 ** Do not consider a single-pass strategy for a multi-row update if
730 ** there is anything that might disrupt the cursor being used to do
732 ** (1) This is a nested UPDATE
733 ** (2) There are triggers
734 ** (3) There are FOREIGN KEY constraints
735 ** (4) There are REPLACE conflict handlers
736 ** (5) There are subqueries in the WHERE clause
738 flags
= WHERE_ONEPASS_DESIRED
;
744 && (pWhere
==0 || !ExprHasProperty(pWhere
, EP_Subquery
))
746 flags
|= WHERE_ONEPASS_MULTIROW
;
748 pWInfo
= sqlite3WhereBegin(pParse
, pTabList
, pWhere
,0,0,0,flags
,iIdxCur
);
749 if( pWInfo
==0 ) goto update_cleanup
;
751 /* A one-pass strategy that might update more than one row may not
752 ** be used if any column of the index used for the scan is being
753 ** updated. Otherwise, if there is an index on "b", statements like
754 ** the following could create an infinite loop:
756 ** UPDATE t1 SET b=b+1 WHERE b>?
758 ** Fall back to ONEPASS_OFF if where.c has selected a ONEPASS_MULTI
759 ** strategy that uses an index for which one or more columns are being
761 eOnePass
= sqlite3WhereOkOnePass(pWInfo
, aiCurOnePass
);
762 bFinishSeek
= sqlite3WhereUsesDeferredSeek(pWInfo
);
763 if( eOnePass
!=ONEPASS_SINGLE
){
764 sqlite3MultiWrite(pParse
);
765 if( eOnePass
==ONEPASS_MULTI
){
766 int iCur
= aiCurOnePass
[1];
767 if( iCur
>=0 && iCur
!=iDataCur
&& aToOpen
[iCur
-iBaseCur
] ){
768 eOnePass
= ONEPASS_OFF
;
770 assert( iCur
!=iDataCur
|| !HasRowid(pTab
) );
775 if( HasRowid(pTab
) ){
776 /* Read the rowid of the current row of the WHERE scan. In ONEPASS_OFF
777 ** mode, write the rowid into the FIFO. In either of the one-pass modes,
778 ** leave it in register regOldRowid. */
779 sqlite3VdbeAddOp2(v
, OP_Rowid
, iDataCur
, regOldRowid
);
780 if( eOnePass
==ONEPASS_OFF
){
781 aRegIdx
[nAllIdx
] = ++pParse
->nMem
;
782 sqlite3VdbeAddOp3(v
, OP_Insert
, iEph
, regRowSet
, regOldRowid
);
784 if( ALWAYS(addrOpen
) ) sqlite3VdbeChangeToNoop(v
, addrOpen
);
787 /* Read the PK of the current row into an array of registers. In
788 ** ONEPASS_OFF mode, serialize the array into a record and store it in
789 ** the ephemeral table. Or, in ONEPASS_SINGLE or MULTI mode, change
790 ** the OP_OpenEphemeral instruction to a Noop (the ephemeral table
791 ** is not required) and leave the PK fields in the array of registers. */
792 for(i
=0; i
<nPk
; i
++){
793 assert( pPk
->aiColumn
[i
]>=0 );
794 sqlite3ExprCodeGetColumnOfTable(v
, pTab
, iDataCur
,
795 pPk
->aiColumn
[i
], iPk
+i
);
798 if( addrOpen
) sqlite3VdbeChangeToNoop(v
, addrOpen
);
802 sqlite3VdbeAddOp4(v
, OP_MakeRecord
, iPk
, nPk
, regKey
,
803 sqlite3IndexAffinityStr(db
, pPk
), nPk
);
804 sqlite3VdbeAddOp4Int(v
, OP_IdxInsert
, iEph
, regKey
, iPk
, nPk
);
810 if( nChangeFrom
==0 && eOnePass
!=ONEPASS_MULTI
){
811 sqlite3WhereEnd(pWInfo
);
819 /* Open every index that needs updating. */
820 if( eOnePass
!=ONEPASS_OFF
){
821 if( aiCurOnePass
[0]>=0 ) aToOpen
[aiCurOnePass
[0]-iBaseCur
] = 0;
822 if( aiCurOnePass
[1]>=0 ) aToOpen
[aiCurOnePass
[1]-iBaseCur
] = 0;
825 if( eOnePass
==ONEPASS_MULTI
&& (nIdx
-(aiCurOnePass
[1]>=0))>0 ){
826 addrOnce
= sqlite3VdbeAddOp0(v
, OP_Once
); VdbeCoverage(v
);
828 sqlite3OpenTableAndIndices(pParse
, pTab
, OP_OpenWrite
, 0, iBaseCur
,
829 aToOpen
, &iNotUsed1
, &iNotUsed2
);
831 sqlite3VdbeJumpHereOrPopInst(v
, addrOnce
);
835 /* Top of the update loop */
836 if( eOnePass
!=ONEPASS_OFF
){
837 if( aiCurOnePass
[0]!=iDataCur
838 && aiCurOnePass
[1]!=iDataCur
839 #ifdef SQLITE_ALLOW_ROWID_IN_VIEW
844 sqlite3VdbeAddOp4Int(v
, OP_NotFound
, iDataCur
, labelBreak
, regKey
,nKey
);
847 if( eOnePass
!=ONEPASS_SINGLE
){
848 labelContinue
= sqlite3VdbeMakeLabel(pParse
);
850 sqlite3VdbeAddOp2(v
, OP_IsNull
, pPk
? regKey
: regOldRowid
, labelBreak
);
851 VdbeCoverageIf(v
, pPk
==0);
852 VdbeCoverageIf(v
, pPk
!=0);
853 }else if( pPk
|| nChangeFrom
){
854 labelContinue
= sqlite3VdbeMakeLabel(pParse
);
855 sqlite3VdbeAddOp2(v
, OP_Rewind
, iEph
, labelBreak
); VdbeCoverage(v
);
856 addrTop
= sqlite3VdbeCurrentAddr(v
);
860 for(i
=0; i
<nPk
; i
++){
861 sqlite3VdbeAddOp3(v
, OP_Column
, iEph
, i
, iPk
+i
);
863 sqlite3VdbeAddOp4Int(
864 v
, OP_NotFound
, iDataCur
, labelContinue
, iPk
, nPk
867 sqlite3VdbeAddOp2(v
, OP_Rowid
, iEph
, regOldRowid
);
869 v
, OP_NotExists
, iDataCur
, labelContinue
, regOldRowid
874 sqlite3VdbeAddOp2(v
, OP_RowData
, iEph
, regKey
);
875 sqlite3VdbeAddOp4Int(v
, OP_NotFound
, iDataCur
, labelContinue
, regKey
,0);
879 sqlite3VdbeAddOp2(v
, OP_Rewind
, iEph
, labelBreak
); VdbeCoverage(v
);
880 labelContinue
= sqlite3VdbeMakeLabel(pParse
);
881 addrTop
= sqlite3VdbeAddOp2(v
, OP_Rowid
, iEph
, regOldRowid
);
883 sqlite3VdbeAddOp3(v
, OP_NotExists
, iDataCur
, labelContinue
, regOldRowid
);
888 /* If the rowid value will change, set register regNewRowid to
889 ** contain the new value. If the rowid is not being modified,
890 ** then regNewRowid is the same register as regOldRowid, which is
891 ** already populated. */
892 assert( chngKey
|| pTrigger
|| hasFK
|| regOldRowid
==regNewRowid
);
894 assert( iRowidExpr
>=0 );
895 if( nChangeFrom
==0 ){
896 sqlite3ExprCode(pParse
, pRowidExpr
, regNewRowid
);
898 sqlite3VdbeAddOp3(v
, OP_Column
, iEph
, iRowidExpr
, regNewRowid
);
900 sqlite3VdbeAddOp1(v
, OP_MustBeInt
, regNewRowid
); VdbeCoverage(v
);
903 /* Compute the old pre-UPDATE content of the row being changed, if that
904 ** information is needed */
905 if( chngPk
|| hasFK
|| pTrigger
){
906 u32 oldmask
= (hasFK
? sqlite3FkOldmask(pParse
, pTab
) : 0);
907 oldmask
|= sqlite3TriggerColmask(pParse
,
908 pTrigger
, pChanges
, 0, TRIGGER_BEFORE
|TRIGGER_AFTER
, pTab
, onError
910 for(i
=0; i
<pTab
->nCol
; i
++){
911 u32 colFlags
= pTab
->aCol
[i
].colFlags
;
912 k
= sqlite3TableColumnToStorage(pTab
, i
) + regOld
;
913 if( oldmask
==0xffffffff
914 || (i
<32 && (oldmask
& MASKBIT32(i
))!=0)
915 || (colFlags
& COLFLAG_PRIMKEY
)!=0
917 testcase( oldmask
!=0xffffffff && i
==31 );
918 sqlite3ExprCodeGetColumnOfTable(v
, pTab
, iDataCur
, i
, k
);
920 sqlite3VdbeAddOp2(v
, OP_Null
, 0, k
);
923 if( chngRowid
==0 && pPk
==0 ){
924 #ifdef SQLITE_ALLOW_ROWID_IN_VIEW
925 if( isView
) sqlite3VdbeAddOp2(v
, OP_Null
, 0, regOldRowid
);
927 sqlite3VdbeAddOp2(v
, OP_Copy
, regOldRowid
, regNewRowid
);
931 /* Populate the array of registers beginning at regNew with the new
932 ** row data. This array is used to check constants, create the new
933 ** table and index records, and as the values for any new.* references
936 ** If there are one or more BEFORE triggers, then do not populate the
937 ** registers associated with columns that are (a) not modified by
938 ** this UPDATE statement and (b) not accessed by new.* references. The
939 ** values for registers not modified by the UPDATE must be reloaded from
940 ** the database after the BEFORE triggers are fired anyway (as the trigger
941 ** may have modified them). So not loading those that are not going to
942 ** be used eliminates some redundant opcodes.
944 newmask
= sqlite3TriggerColmask(
945 pParse
, pTrigger
, pChanges
, 1, TRIGGER_BEFORE
, pTab
, onError
947 for(i
=0, k
=regNew
; i
<pTab
->nCol
; i
++, k
++){
948 if( i
==pTab
->iPKey
){
949 sqlite3VdbeAddOp2(v
, OP_Null
, 0, k
);
950 }else if( (pTab
->aCol
[i
].colFlags
& COLFLAG_GENERATED
)!=0 ){
951 if( pTab
->aCol
[i
].colFlags
& COLFLAG_VIRTUAL
) k
--;
956 int nOff
= (isView
? pTab
->nCol
: nPk
);
957 assert( eOnePass
==ONEPASS_OFF
);
958 sqlite3VdbeAddOp3(v
, OP_Column
, iEph
, nOff
+j
, k
);
960 sqlite3ExprCode(pParse
, pChanges
->a
[j
].pExpr
, k
);
962 }else if( 0==(tmask
&TRIGGER_BEFORE
) || i
>31 || (newmask
& MASKBIT32(i
)) ){
963 /* This branch loads the value of a column that will not be changed
964 ** into a register. This is done if there are no BEFORE triggers, or
965 ** if there are one or more BEFORE triggers that use this value via
966 ** a new.* reference in a trigger program.
970 sqlite3ExprCodeGetColumnOfTable(v
, pTab
, iDataCur
, i
, k
);
973 sqlite3VdbeAddOp2(v
, OP_Null
, 0, k
);
977 #ifndef SQLITE_OMIT_GENERATED_COLUMNS
978 if( pTab
->tabFlags
& TF_HasGenerated
){
979 testcase( pTab
->tabFlags
& TF_HasVirtual
);
980 testcase( pTab
->tabFlags
& TF_HasStored
);
981 sqlite3ComputeGeneratedColumns(pParse
, regNew
, pTab
);
985 /* Fire any BEFORE UPDATE triggers. This happens before constraints are
986 ** verified. One could argue that this is wrong.
988 if( tmask
&TRIGGER_BEFORE
){
989 sqlite3TableAffinity(v
, pTab
, regNew
);
990 sqlite3CodeRowTrigger(pParse
, pTrigger
, TK_UPDATE
, pChanges
,
991 TRIGGER_BEFORE
, pTab
, regOldRowid
, onError
, labelContinue
);
994 /* The row-trigger may have deleted the row being updated. In this
995 ** case, jump to the next row. No updates or AFTER triggers are
996 ** required. This behavior - what happens when the row being updated
997 ** is deleted or renamed by a BEFORE trigger - is left undefined in the
1001 sqlite3VdbeAddOp4Int(v
, OP_NotFound
,iDataCur
,labelContinue
,regKey
,nKey
);
1004 sqlite3VdbeAddOp3(v
, OP_NotExists
, iDataCur
, labelContinue
,regOldRowid
);
1008 /* After-BEFORE-trigger-reload-loop:
1009 ** If it did not delete it, the BEFORE trigger may still have modified
1010 ** some of the columns of the row being updated. Load the values for
1011 ** all columns not modified by the update statement into their registers
1012 ** in case this has happened. Only unmodified columns are reloaded.
1013 ** The values computed for modified columns use the values before the
1014 ** BEFORE trigger runs. See test case trigger1-18.0 (added 2018-04-26)
1017 for(i
=0, k
=regNew
; i
<pTab
->nCol
; i
++, k
++){
1018 if( pTab
->aCol
[i
].colFlags
& COLFLAG_GENERATED
){
1019 if( pTab
->aCol
[i
].colFlags
& COLFLAG_VIRTUAL
) k
--;
1020 }else if( aXRef
[i
]<0 && i
!=pTab
->iPKey
){
1021 sqlite3ExprCodeGetColumnOfTable(v
, pTab
, iDataCur
, i
, k
);
1024 #ifndef SQLITE_OMIT_GENERATED_COLUMNS
1025 if( pTab
->tabFlags
& TF_HasGenerated
){
1026 testcase( pTab
->tabFlags
& TF_HasVirtual
);
1027 testcase( pTab
->tabFlags
& TF_HasStored
);
1028 sqlite3ComputeGeneratedColumns(pParse
, regNew
, pTab
);
1035 /* Do constraint checks. */
1036 assert( regOldRowid
>0 );
1037 sqlite3GenerateConstraintChecks(pParse
, pTab
, aRegIdx
, iDataCur
, iIdxCur
,
1038 regNewRowid
, regOldRowid
, chngKey
, onError
, labelContinue
, &bReplace
,
1041 /* If REPLACE conflict handling may have been used, or if the PK of the
1042 ** row is changing, then the GenerateConstraintChecks() above may have
1043 ** moved cursor iDataCur. Reseek it. */
1044 if( bReplace
|| chngKey
){
1046 sqlite3VdbeAddOp4Int(v
, OP_NotFound
,iDataCur
,labelContinue
,regKey
,nKey
);
1048 sqlite3VdbeAddOp3(v
, OP_NotExists
, iDataCur
, labelContinue
,regOldRowid
);
1053 /* Do FK constraint checks. */
1055 sqlite3FkCheck(pParse
, pTab
, regOldRowid
, 0, aXRef
, chngKey
);
1058 /* Delete the index entries associated with the current record. */
1059 sqlite3GenerateRowIndexDelete(pParse
, pTab
, iDataCur
, iIdxCur
, aRegIdx
, -1);
1061 /* We must run the OP_FinishSeek opcode to resolve a prior
1062 ** OP_DeferredSeek if there is any possibility that there have been
1063 ** no OP_Column opcodes since the OP_DeferredSeek was issued. But
1064 ** we want to avoid the OP_FinishSeek if possible, as running it
1065 ** costs CPU cycles. */
1067 sqlite3VdbeAddOp1(v
, OP_FinishSeek
, iDataCur
);
1070 /* If changing the rowid value, or if there are foreign key constraints
1071 ** to process, delete the old record. Otherwise, add a noop OP_Delete
1072 ** to invoke the pre-update hook.
1074 ** That (regNew==regnewRowid+1) is true is also important for the
1075 ** pre-update hook. If the caller invokes preupdate_new(), the returned
1076 ** value is copied from memory cell (regNewRowid+1+iCol), where iCol
1077 ** is the column index supplied by the user.
1079 assert( regNew
==regNewRowid
+1 );
1080 #ifdef SQLITE_ENABLE_PREUPDATE_HOOK
1081 sqlite3VdbeAddOp3(v
, OP_Delete
, iDataCur
,
1082 OPFLAG_ISUPDATE
| ((hasFK
>1 || chngKey
) ? 0 : OPFLAG_ISNOOP
),
1085 if( eOnePass
==ONEPASS_MULTI
){
1086 assert( hasFK
==0 && chngKey
==0 );
1087 sqlite3VdbeChangeP5(v
, OPFLAG_SAVEPOSITION
);
1089 if( !pParse
->nested
){
1090 sqlite3VdbeAppendP4(v
, pTab
, P4_TABLE
);
1093 if( hasFK
>1 || chngKey
){
1094 sqlite3VdbeAddOp2(v
, OP_Delete
, iDataCur
, 0);
1099 sqlite3FkCheck(pParse
, pTab
, 0, regNewRowid
, aXRef
, chngKey
);
1102 /* Insert the new index entries and the new record. */
1103 sqlite3CompleteInsertion(
1104 pParse
, pTab
, iDataCur
, iIdxCur
, regNewRowid
, aRegIdx
,
1105 OPFLAG_ISUPDATE
| (eOnePass
==ONEPASS_MULTI
? OPFLAG_SAVEPOSITION
: 0),
1109 /* Do any ON CASCADE, SET NULL or SET DEFAULT operations required to
1110 ** handle rows (possibly in other tables) that refer via a foreign key
1111 ** to the row just updated. */
1113 sqlite3FkActions(pParse
, pTab
, pChanges
, regOldRowid
, aXRef
, chngKey
);
1117 /* Increment the row counter
1120 sqlite3VdbeAddOp2(v
, OP_AddImm
, regRowCount
, 1);
1124 sqlite3CodeRowTrigger(pParse
, pTrigger
, TK_UPDATE
, pChanges
,
1125 TRIGGER_AFTER
, pTab
, regOldRowid
, onError
, labelContinue
);
1128 /* Repeat the above with the next record to be updated, until
1129 ** all record selected by the WHERE clause have been updated.
1131 if( eOnePass
==ONEPASS_SINGLE
){
1132 /* Nothing to do at end-of-loop for a single-pass */
1133 }else if( eOnePass
==ONEPASS_MULTI
){
1134 sqlite3VdbeResolveLabel(v
, labelContinue
);
1135 sqlite3WhereEnd(pWInfo
);
1137 sqlite3VdbeResolveLabel(v
, labelContinue
);
1138 sqlite3VdbeAddOp2(v
, OP_Next
, iEph
, addrTop
); VdbeCoverage(v
);
1140 sqlite3VdbeResolveLabel(v
, labelBreak
);
1142 /* Update the sqlite_sequence table by storing the content of the
1143 ** maximum rowid counter values recorded while inserting into
1144 ** autoincrement tables.
1146 if( pParse
->nested
==0 && pParse
->pTriggerTab
==0 && pUpsert
==0 ){
1147 sqlite3AutoincrementEnd(pParse
);
1151 ** Return the number of rows that were changed, if we are tracking
1152 ** that information.
1155 sqlite3CodeChangeCount(v
, regRowCount
, "rows updated");
1159 sqlite3AuthContextPop(&sContext
);
1160 sqlite3DbFree(db
, aXRef
); /* Also frees aRegIdx[] and aToOpen[] */
1161 sqlite3SrcListDelete(db
, pTabList
);
1162 sqlite3ExprListDelete(db
, pChanges
);
1163 sqlite3ExprDelete(db
, pWhere
);
1164 #if defined(SQLITE_ENABLE_UPDATE_DELETE_LIMIT)
1165 sqlite3ExprListDelete(db
, pOrderBy
);
1166 sqlite3ExprDelete(db
, pLimit
);
1170 /* Make sure "isView" and other macros defined above are undefined. Otherwise
1171 ** they may interfere with compilation of other functions in this file
1172 ** (or in another file, if this file becomes part of the amalgamation). */
1180 #ifndef SQLITE_OMIT_VIRTUALTABLE
1182 ** Generate code for an UPDATE of a virtual table.
1184 ** There are two possible strategies - the default and the special
1185 ** "onepass" strategy. Onepass is only used if the virtual table
1186 ** implementation indicates that pWhere may match at most one row.
1188 ** The default strategy is to create an ephemeral table that contains
1189 ** for each row to be changed:
1191 ** (A) The original rowid of that row.
1192 ** (B) The revised rowid for the row.
1193 ** (C) The content of every column in the row.
1195 ** Then loop through the contents of this ephemeral table executing a
1196 ** VUpdate for each row. When finished, drop the ephemeral table.
1198 ** The "onepass" strategy does not use an ephemeral table. Instead, it
1199 ** stores the same values (A, B and C above) in a register array and
1200 ** makes a single invocation of VUpdate.
1202 static void updateVirtualTable(
1203 Parse
*pParse
, /* The parsing context */
1204 SrcList
*pSrc
, /* The virtual table to be modified */
1205 Table
*pTab
, /* The virtual table */
1206 ExprList
*pChanges
, /* The columns to change in the UPDATE statement */
1207 Expr
*pRowid
, /* Expression used to recompute the rowid */
1208 int *aXRef
, /* Mapping from columns of pTab to entries in pChanges */
1209 Expr
*pWhere
, /* WHERE clause of the UPDATE statement */
1210 int onError
/* ON CONFLICT strategy */
1212 Vdbe
*v
= pParse
->pVdbe
; /* Virtual machine under construction */
1213 int ephemTab
; /* Table holding the result of the SELECT */
1214 int i
; /* Loop counter */
1215 sqlite3
*db
= pParse
->db
; /* Database connection */
1216 const char *pVTab
= (const char*)sqlite3GetVTable(db
, pTab
);
1217 WhereInfo
*pWInfo
= 0;
1218 int nArg
= 2 + pTab
->nCol
; /* Number of arguments to VUpdate */
1219 int regArg
; /* First register in VUpdate arg array */
1220 int regRec
; /* Register in which to assemble record */
1221 int regRowid
; /* Register for ephemeral table rowid */
1222 int iCsr
= pSrc
->a
[0].iCursor
; /* Cursor used for virtual table scan */
1223 int aDummy
[2]; /* Unused arg for sqlite3WhereOkOnePass() */
1224 int eOnePass
; /* True to use onepass strategy */
1225 int addr
; /* Address of OP_OpenEphemeral */
1227 /* Allocate nArg registers in which to gather the arguments for VUpdate. Then
1228 ** create and open the ephemeral table in which the records created from
1229 ** these arguments will be temporarily stored. */
1231 ephemTab
= pParse
->nTab
++;
1232 addr
= sqlite3VdbeAddOp2(v
, OP_OpenEphemeral
, ephemTab
, nArg
);
1233 regArg
= pParse
->nMem
+ 1;
1234 pParse
->nMem
+= nArg
;
1239 if( HasRowid(pTab
) ){
1241 pRow
= sqlite3ExprDup(db
, pRowid
, 0);
1243 pRow
= sqlite3PExpr(pParse
, TK_ROW
, 0, 0);
1246 i16 iPk
; /* PRIMARY KEY column */
1247 pPk
= sqlite3PrimaryKeyIndex(pTab
);
1249 assert( pPk
->nKeyCol
==1 );
1250 iPk
= pPk
->aiColumn
[0];
1251 if( aXRef
[iPk
]>=0 ){
1252 pRow
= sqlite3ExprDup(db
, pChanges
->a
[aXRef
[iPk
]].pExpr
, 0);
1254 pRow
= exprRowColumn(pParse
, iPk
);
1257 pList
= sqlite3ExprListAppend(pParse
, 0, pRow
);
1259 for(i
=0; i
<pTab
->nCol
; i
++){
1261 pList
= sqlite3ExprListAppend(pParse
, pList
,
1262 sqlite3ExprDup(db
, pChanges
->a
[aXRef
[i
]].pExpr
, 0)
1265 Expr
*pRowExpr
= exprRowColumn(pParse
, i
);
1266 if( pRowExpr
) pRowExpr
->op2
= OPFLAG_NOCHNG
;
1267 pList
= sqlite3ExprListAppend(pParse
, pList
, pRowExpr
);
1271 updateFromSelect(pParse
, ephemTab
, pPk
, pList
, pSrc
, pWhere
, 0, 0);
1272 sqlite3ExprListDelete(db
, pList
);
1273 eOnePass
= ONEPASS_OFF
;
1275 regRec
= ++pParse
->nMem
;
1276 regRowid
= ++pParse
->nMem
;
1278 /* Start scanning the virtual table */
1279 pWInfo
= sqlite3WhereBegin(
1280 pParse
, pSrc
, pWhere
, 0, 0, 0, WHERE_ONEPASS_DESIRED
, 0
1282 if( pWInfo
==0 ) return;
1284 /* Populate the argument registers. */
1285 for(i
=0; i
<pTab
->nCol
; i
++){
1286 assert( (pTab
->aCol
[i
].colFlags
& COLFLAG_GENERATED
)==0 );
1288 sqlite3ExprCode(pParse
, pChanges
->a
[aXRef
[i
]].pExpr
, regArg
+2+i
);
1290 sqlite3VdbeAddOp3(v
, OP_VColumn
, iCsr
, i
, regArg
+2+i
);
1291 sqlite3VdbeChangeP5(v
, OPFLAG_NOCHNG
);/* For sqlite3_vtab_nochange() */
1294 if( HasRowid(pTab
) ){
1295 sqlite3VdbeAddOp2(v
, OP_Rowid
, iCsr
, regArg
);
1297 sqlite3ExprCode(pParse
, pRowid
, regArg
+1);
1299 sqlite3VdbeAddOp2(v
, OP_Rowid
, iCsr
, regArg
+1);
1302 Index
*pPk
; /* PRIMARY KEY index */
1303 i16 iPk
; /* PRIMARY KEY column */
1304 pPk
= sqlite3PrimaryKeyIndex(pTab
);
1306 assert( pPk
->nKeyCol
==1 );
1307 iPk
= pPk
->aiColumn
[0];
1308 sqlite3VdbeAddOp3(v
, OP_VColumn
, iCsr
, iPk
, regArg
);
1309 sqlite3VdbeAddOp2(v
, OP_SCopy
, regArg
+2+iPk
, regArg
+1);
1312 eOnePass
= sqlite3WhereOkOnePass(pWInfo
, aDummy
);
1314 /* There is no ONEPASS_MULTI on virtual tables */
1315 assert( eOnePass
==ONEPASS_OFF
|| eOnePass
==ONEPASS_SINGLE
);
1318 /* If using the onepass strategy, no-op out the OP_OpenEphemeral coded
1320 sqlite3VdbeChangeToNoop(v
, addr
);
1321 sqlite3VdbeAddOp1(v
, OP_Close
, iCsr
);
1323 /* Create a record from the argument register contents and insert it into
1324 ** the ephemeral table. */
1325 sqlite3MultiWrite(pParse
);
1326 sqlite3VdbeAddOp3(v
, OP_MakeRecord
, regArg
, nArg
, regRec
);
1327 #if defined(SQLITE_DEBUG) && !defined(SQLITE_ENABLE_NULL_TRIM)
1328 /* Signal an assert() within OP_MakeRecord that it is allowed to
1329 ** accept no-change records with serial_type 10 */
1330 sqlite3VdbeChangeP5(v
, OPFLAG_NOCHNG_MAGIC
);
1332 sqlite3VdbeAddOp2(v
, OP_NewRowid
, ephemTab
, regRowid
);
1333 sqlite3VdbeAddOp3(v
, OP_Insert
, ephemTab
, regRec
, regRowid
);
1338 if( eOnePass
==ONEPASS_OFF
){
1339 /* End the virtual table scan */
1340 if( pSrc
->nSrc
==1 ){
1341 sqlite3WhereEnd(pWInfo
);
1344 /* Begin scanning through the ephemeral table. */
1345 addr
= sqlite3VdbeAddOp1(v
, OP_Rewind
, ephemTab
); VdbeCoverage(v
);
1347 /* Extract arguments from the current row of the ephemeral table and
1348 ** invoke the VUpdate method. */
1349 for(i
=0; i
<nArg
; i
++){
1350 sqlite3VdbeAddOp3(v
, OP_Column
, ephemTab
, i
, regArg
+i
);
1353 sqlite3VtabMakeWritable(pParse
, pTab
);
1354 sqlite3VdbeAddOp4(v
, OP_VUpdate
, 0, nArg
, regArg
, pVTab
, P4_VTAB
);
1355 sqlite3VdbeChangeP5(v
, onError
==OE_Default
? OE_Abort
: onError
);
1356 sqlite3MayAbort(pParse
);
1358 /* End of the ephemeral table scan. Or, if using the onepass strategy,
1359 ** jump to here if the scan visited zero rows. */
1360 if( eOnePass
==ONEPASS_OFF
){
1361 sqlite3VdbeAddOp2(v
, OP_Next
, ephemTab
, addr
+1); VdbeCoverage(v
);
1362 sqlite3VdbeJumpHere(v
, addr
);
1363 sqlite3VdbeAddOp2(v
, OP_Close
, ephemTab
, 0);
1365 sqlite3WhereEnd(pWInfo
);
1368 #endif /* SQLITE_OMIT_VIRTUALTABLE */