xref: /sqlite-3.40.0/src/update.c (revision 74e4352a)
1 /*
2 ** 2001 September 15
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 C code routines that are called by the parser
13 ** to handle UPDATE statements.
14 **
15 ** $Id: update.c,v 1.136 2007/02/21 17:04:04 danielk1977 Exp $
16 */
17 #include "sqliteInt.h"
18 
19 #ifndef SQLITE_OMIT_VIRTUALTABLE
20 /* Forward declaration */
21 static void updateVirtualTable(
22   Parse *pParse,       /* The parsing context */
23   SrcList *pSrc,       /* The virtual table to be modified */
24   Table *pTab,         /* The virtual table */
25   ExprList *pChanges,  /* The columns to change in the UPDATE statement */
26   Expr *pRowidExpr,    /* Expression used to recompute the rowid */
27   int *aXRef,          /* Mapping from columns of pTab to entries in pChanges */
28   Expr *pWhere         /* WHERE clause of the UPDATE statement */
29 );
30 #endif /* SQLITE_OMIT_VIRTUALTABLE */
31 
32 /*
33 ** The most recently coded instruction was an OP_Column to retrieve the
34 ** i-th column of table pTab. This routine sets the P3 parameter of the
35 ** OP_Column to the default value, if any.
36 **
37 ** The default value of a column is specified by a DEFAULT clause in the
38 ** column definition. This was either supplied by the user when the table
39 ** was created, or added later to the table definition by an ALTER TABLE
40 ** command. If the latter, then the row-records in the table btree on disk
41 ** may not contain a value for the column and the default value, taken
42 ** from the P3 parameter of the OP_Column instruction, is returned instead.
43 ** If the former, then all row-records are guaranteed to include a value
44 ** for the column and the P3 value is not required.
45 **
46 ** Column definitions created by an ALTER TABLE command may only have
47 ** literal default values specified: a number, null or a string. (If a more
48 ** complicated default expression value was provided, it is evaluated
49 ** when the ALTER TABLE is executed and one of the literal values written
50 ** into the sqlite_master table.)
51 **
52 ** Therefore, the P3 parameter is only required if the default value for
53 ** the column is a literal number, string or null. The sqlite3ValueFromExpr()
54 ** function is capable of transforming these types of expressions into
55 ** sqlite3_value objects.
56 */
57 void sqlite3ColumnDefault(Vdbe *v, Table *pTab, int i){
58   if( pTab && !pTab->pSelect ){
59     sqlite3_value *pValue;
60     u8 enc = ENC(sqlite3VdbeDb(v));
61     Column *pCol = &pTab->aCol[i];
62     sqlite3ValueFromExpr(pCol->pDflt, enc, pCol->affinity, &pValue);
63     if( pValue ){
64       sqlite3VdbeChangeP3(v, -1, (const char *)pValue, P3_MEM);
65     }else{
66       VdbeComment((v, "# %s.%s", pTab->zName, pCol->zName));
67     }
68   }
69 }
70 
71 /*
72 ** Process an UPDATE statement.
73 **
74 **   UPDATE OR IGNORE table_wxyz SET a=b, c=d WHERE e<5 AND f NOT NULL;
75 **          \_______/ \________/     \______/       \________________/
76 *            onError   pTabList      pChanges             pWhere
77 */
78 void sqlite3Update(
79   Parse *pParse,         /* The parser context */
80   SrcList *pTabList,     /* The table in which we should change things */
81   ExprList *pChanges,    /* Things to be changed */
82   Expr *pWhere,          /* The WHERE clause.  May be null */
83   int onError            /* How to handle constraint errors */
84 ){
85   int i, j;              /* Loop counters */
86   Table *pTab;           /* The table to be updated */
87   int addr = 0;          /* VDBE instruction address of the start of the loop */
88   WhereInfo *pWInfo;     /* Information about the WHERE clause */
89   Vdbe *v;               /* The virtual database engine */
90   Index *pIdx;           /* For looping over indices */
91   int nIdx;              /* Number of indices that need updating */
92   int nIdxTotal;         /* Total number of indices */
93   int iCur;              /* VDBE Cursor number of pTab */
94   sqlite3 *db;           /* The database structure */
95   Index **apIdx = 0;     /* An array of indices that need updating too */
96   char *aIdxUsed = 0;    /* aIdxUsed[i]==1 if the i-th index is used */
97   int *aXRef = 0;        /* aXRef[i] is the index in pChanges->a[] of the
98                          ** an expression for the i-th column of the table.
99                          ** aXRef[i]==-1 if the i-th column is not changed. */
100   int chngRowid;         /* True if the record number is being changed */
101   Expr *pRowidExpr = 0;  /* Expression defining the new record number */
102   int openAll = 0;       /* True if all indices need to be opened */
103   AuthContext sContext;  /* The authorization context */
104   NameContext sNC;       /* The name-context to resolve expressions in */
105   int iDb;               /* Database containing the table being updated */
106   int memCnt = 0;        /* Memory cell used for counting rows changed */
107 
108 #ifndef SQLITE_OMIT_TRIGGER
109   int isView;                  /* Trying to update a view */
110   int triggers_exist = 0;      /* True if any row triggers exist */
111 #endif
112 
113   int newIdx      = -1;  /* index of trigger "new" temp table       */
114   int oldIdx      = -1;  /* index of trigger "old" temp table       */
115 
116   sContext.pParse = 0;
117   if( pParse->nErr || sqlite3MallocFailed() ){
118     goto update_cleanup;
119   }
120   db = pParse->db;
121   assert( pTabList->nSrc==1 );
122 
123   /* Locate the table which we want to update.
124   */
125   pTab = sqlite3SrcListLookup(pParse, pTabList);
126   if( pTab==0 ) goto update_cleanup;
127   iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
128 
129   /* Figure out if we have any triggers and if the table being
130   ** updated is a view
131   */
132 #ifndef SQLITE_OMIT_TRIGGER
133   triggers_exist = sqlite3TriggersExist(pParse, pTab, TK_UPDATE, pChanges);
134   isView = pTab->pSelect!=0;
135 #else
136 # define triggers_exist 0
137 # define isView 0
138 #endif
139 #ifdef SQLITE_OMIT_VIEW
140 # undef isView
141 # define isView 0
142 #endif
143 
144   if( sqlite3IsReadOnly(pParse, pTab, triggers_exist) ){
145     goto update_cleanup;
146   }
147   if( sqlite3ViewGetColumnNames(pParse, pTab) ){
148     goto update_cleanup;
149   }
150   aXRef = sqliteMallocRaw( sizeof(int) * pTab->nCol );
151   if( aXRef==0 ) goto update_cleanup;
152   for(i=0; i<pTab->nCol; i++) aXRef[i] = -1;
153 
154   /* If there are FOR EACH ROW triggers, allocate cursors for the
155   ** special OLD and NEW tables
156   */
157   if( triggers_exist ){
158     newIdx = pParse->nTab++;
159     oldIdx = pParse->nTab++;
160   }
161 
162   /* Allocate a cursors for the main database table and for all indices.
163   ** The index cursors might not be used, but if they are used they
164   ** need to occur right after the database cursor.  So go ahead and
165   ** allocate enough space, just in case.
166   */
167   pTabList->a[0].iCursor = iCur = pParse->nTab++;
168   for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
169     pParse->nTab++;
170   }
171 
172   /* Initialize the name-context */
173   memset(&sNC, 0, sizeof(sNC));
174   sNC.pParse = pParse;
175   sNC.pSrcList = pTabList;
176 
177   /* Resolve the column names in all the expressions of the
178   ** of the UPDATE statement.  Also find the column index
179   ** for each column to be updated in the pChanges array.  For each
180   ** column to be updated, make sure we have authorization to change
181   ** that column.
182   */
183   chngRowid = 0;
184   for(i=0; i<pChanges->nExpr; i++){
185     if( sqlite3ExprResolveNames(&sNC, pChanges->a[i].pExpr) ){
186       goto update_cleanup;
187     }
188     for(j=0; j<pTab->nCol; j++){
189       if( sqlite3StrICmp(pTab->aCol[j].zName, pChanges->a[i].zName)==0 ){
190         if( j==pTab->iPKey ){
191           chngRowid = 1;
192           pRowidExpr = pChanges->a[i].pExpr;
193         }
194         aXRef[j] = i;
195         break;
196       }
197     }
198     if( j>=pTab->nCol ){
199       if( sqlite3IsRowid(pChanges->a[i].zName) ){
200         chngRowid = 1;
201         pRowidExpr = pChanges->a[i].pExpr;
202       }else{
203         sqlite3ErrorMsg(pParse, "no such column: %s", pChanges->a[i].zName);
204         goto update_cleanup;
205       }
206     }
207 #ifndef SQLITE_OMIT_AUTHORIZATION
208     {
209       int rc;
210       rc = sqlite3AuthCheck(pParse, SQLITE_UPDATE, pTab->zName,
211                            pTab->aCol[j].zName, db->aDb[iDb].zName);
212       if( rc==SQLITE_DENY ){
213         goto update_cleanup;
214       }else if( rc==SQLITE_IGNORE ){
215         aXRef[j] = -1;
216       }
217     }
218 #endif
219   }
220 
221   /* Allocate memory for the array apIdx[] and fill it with pointers to every
222   ** index that needs to be updated.  Indices only need updating if their
223   ** key includes one of the columns named in pChanges or if the record
224   ** number of the original table entry is changing.
225   */
226   for(nIdx=nIdxTotal=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, nIdxTotal++){
227     if( chngRowid ){
228       i = 0;
229     }else {
230       for(i=0; i<pIdx->nColumn; i++){
231         if( aXRef[pIdx->aiColumn[i]]>=0 ) break;
232       }
233     }
234     if( i<pIdx->nColumn ) nIdx++;
235   }
236   if( nIdxTotal>0 ){
237     apIdx = sqliteMallocRaw( sizeof(Index*) * nIdx + nIdxTotal );
238     if( apIdx==0 ) goto update_cleanup;
239     aIdxUsed = (char*)&apIdx[nIdx];
240   }
241   for(nIdx=j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
242     if( chngRowid ){
243       i = 0;
244     }else{
245       for(i=0; i<pIdx->nColumn; i++){
246         if( aXRef[pIdx->aiColumn[i]]>=0 ) break;
247       }
248     }
249     if( i<pIdx->nColumn ){
250       apIdx[nIdx++] = pIdx;
251       aIdxUsed[j] = 1;
252     }else{
253       aIdxUsed[j] = 0;
254     }
255   }
256 
257   /* Begin generating code.
258   */
259   v = sqlite3GetVdbe(pParse);
260   if( v==0 ) goto update_cleanup;
261   if( pParse->nested==0 ) sqlite3VdbeCountChanges(v);
262   sqlite3BeginWriteOperation(pParse, 1, iDb);
263 
264 #ifndef SQLITE_OMIT_VIRTUALTABLE
265   /* Virtual tables must be handled separately */
266   if( IsVirtual(pTab) ){
267     updateVirtualTable(pParse, pTabList, pTab, pChanges, pRowidExpr, aXRef,
268                        pWhere);
269     pWhere = 0;
270     pTabList = 0;
271     goto update_cleanup;
272   }
273 #endif
274 
275   /* Resolve the column names in all the expressions in the
276   ** WHERE clause.
277   */
278   if( sqlite3ExprResolveNames(&sNC, pWhere) ){
279     goto update_cleanup;
280   }
281 
282   /* Start the view context
283   */
284   if( isView ){
285     sqlite3AuthContextPush(pParse, &sContext, pTab->zName);
286   }
287 
288   /* If we are trying to update a view, realize that view into
289   ** a ephemeral table.
290   */
291   if( isView ){
292     Select *pView;
293     pView = sqlite3SelectDup(pTab->pSelect);
294     sqlite3Select(pParse, pView, SRT_EphemTab, iCur, 0, 0, 0, 0);
295     sqlite3SelectDelete(pView);
296   }
297 
298   /* Begin the database scan
299   */
300   pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, 0);
301   if( pWInfo==0 ) goto update_cleanup;
302 
303   /* Remember the rowid of every item to be updated.
304   */
305   sqlite3VdbeAddOp(v, IsVirtual(pTab) ? OP_VRowid : OP_Rowid, iCur, 0);
306   sqlite3VdbeAddOp(v, OP_FifoWrite, 0, 0);
307 
308   /* End the database scan loop.
309   */
310   sqlite3WhereEnd(pWInfo);
311 
312   /* Initialize the count of updated rows
313   */
314   if( db->flags & SQLITE_CountRows && !pParse->trigStack ){
315     memCnt = pParse->nMem++;
316     sqlite3VdbeAddOp(v, OP_MemInt, 0, memCnt);
317   }
318 
319   if( triggers_exist ){
320     /* Create pseudo-tables for NEW and OLD
321     */
322     sqlite3VdbeAddOp(v, OP_OpenPseudo, oldIdx, 0);
323     sqlite3VdbeAddOp(v, OP_SetNumColumns, oldIdx, pTab->nCol);
324     sqlite3VdbeAddOp(v, OP_OpenPseudo, newIdx, 0);
325     sqlite3VdbeAddOp(v, OP_SetNumColumns, newIdx, pTab->nCol);
326 
327     /* The top of the update loop for when there are triggers.
328     */
329     addr = sqlite3VdbeAddOp(v, OP_FifoRead, 0, 0);
330 
331     if( !isView ){
332       sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
333       sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
334       /* Open a cursor and make it point to the record that is
335       ** being updated.
336       */
337       sqlite3OpenTable(pParse, iCur, iDb, pTab, OP_OpenRead);
338     }
339     sqlite3VdbeAddOp(v, OP_MoveGe, iCur, 0);
340 
341     /* Generate the OLD table
342     */
343     sqlite3VdbeAddOp(v, OP_Rowid, iCur, 0);
344     sqlite3VdbeAddOp(v, OP_RowData, iCur, 0);
345     sqlite3VdbeAddOp(v, OP_Insert, oldIdx, 0);
346 
347     /* Generate the NEW table
348     */
349     if( chngRowid ){
350       sqlite3ExprCodeAndCache(pParse, pRowidExpr);
351     }else{
352       sqlite3VdbeAddOp(v, OP_Rowid, iCur, 0);
353     }
354     for(i=0; i<pTab->nCol; i++){
355       if( i==pTab->iPKey ){
356         sqlite3VdbeAddOp(v, OP_Null, 0, 0);
357         continue;
358       }
359       j = aXRef[i];
360       if( j<0 ){
361         sqlite3VdbeAddOp(v, OP_Column, iCur, i);
362         sqlite3ColumnDefault(v, pTab, i);
363       }else{
364         sqlite3ExprCodeAndCache(pParse, pChanges->a[j].pExpr);
365       }
366     }
367     sqlite3VdbeAddOp(v, OP_MakeRecord, pTab->nCol, 0);
368     if( !isView ){
369       sqlite3TableAffinityStr(v, pTab);
370     }
371     if( pParse->nErr ) goto update_cleanup;
372     sqlite3VdbeAddOp(v, OP_Insert, newIdx, 0);
373     if( !isView ){
374       sqlite3VdbeAddOp(v, OP_Close, iCur, 0);
375     }
376 
377     /* Fire the BEFORE and INSTEAD OF triggers
378     */
379     if( sqlite3CodeRowTrigger(pParse, TK_UPDATE, pChanges, TRIGGER_BEFORE, pTab,
380           newIdx, oldIdx, onError, addr) ){
381       goto update_cleanup;
382     }
383   }
384 
385   if( !isView && !IsVirtual(pTab) ){
386     /*
387     ** Open every index that needs updating.  Note that if any
388     ** index could potentially invoke a REPLACE conflict resolution
389     ** action, then we need to open all indices because we might need
390     ** to be deleting some records.
391     */
392     sqlite3OpenTable(pParse, iCur, iDb, pTab, OP_OpenWrite);
393     if( onError==OE_Replace ){
394       openAll = 1;
395     }else{
396       openAll = 0;
397       for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
398         if( pIdx->onError==OE_Replace ){
399           openAll = 1;
400           break;
401         }
402       }
403     }
404     for(i=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){
405       if( openAll || aIdxUsed[i] ){
406         KeyInfo *pKey = sqlite3IndexKeyinfo(pParse, pIdx);
407         sqlite3VdbeAddOp(v, OP_Integer, iDb, 0);
408         sqlite3VdbeOp3(v, OP_OpenWrite, iCur+i+1, pIdx->tnum,
409                        (char*)pKey, P3_KEYINFO_HANDOFF);
410         assert( pParse->nTab>iCur+i+1 );
411       }
412     }
413 
414     /* Loop over every record that needs updating.  We have to load
415     ** the old data for each record to be updated because some columns
416     ** might not change and we will need to copy the old value.
417     ** Also, the old data is needed to delete the old index entries.
418     ** So make the cursor point at the old record.
419     */
420     if( !triggers_exist ){
421       addr = sqlite3VdbeAddOp(v, OP_FifoRead, 0, 0);
422       sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
423     }
424     sqlite3VdbeAddOp(v, OP_NotExists, iCur, addr);
425 
426     /* If the record number will change, push the record number as it
427     ** will be after the update. (The old record number is currently
428     ** on top of the stack.)
429     */
430     if( chngRowid ){
431       sqlite3ExprCode(pParse, pRowidExpr);
432       sqlite3VdbeAddOp(v, OP_MustBeInt, 0, 0);
433     }
434 
435     /* Compute new data for this record.
436     */
437     for(i=0; i<pTab->nCol; i++){
438       if( i==pTab->iPKey ){
439         sqlite3VdbeAddOp(v, OP_Null, 0, 0);
440         continue;
441       }
442       j = aXRef[i];
443       if( j<0 ){
444         sqlite3VdbeAddOp(v, OP_Column, iCur, i);
445         sqlite3ColumnDefault(v, pTab, i);
446       }else{
447         sqlite3ExprCode(pParse, pChanges->a[j].pExpr);
448       }
449     }
450 
451     /* Do constraint checks
452     */
453     sqlite3GenerateConstraintChecks(pParse, pTab, iCur, aIdxUsed, chngRowid, 1,
454                                    onError, addr);
455 
456     /* Delete the old indices for the current record.
457     */
458     sqlite3GenerateRowIndexDelete(v, pTab, iCur, aIdxUsed);
459 
460     /* If changing the record number, delete the old record.
461     */
462     if( chngRowid ){
463       sqlite3VdbeAddOp(v, OP_Delete, iCur, 0);
464     }
465 
466     /* Create the new index entries and the new record.
467     */
468     sqlite3CompleteInsertion(pParse, pTab, iCur, aIdxUsed, chngRowid, 1, -1);
469   }
470 
471   /* Increment the row counter
472   */
473   if( db->flags & SQLITE_CountRows && !pParse->trigStack){
474     sqlite3VdbeAddOp(v, OP_MemIncr, 1, memCnt);
475   }
476 
477   /* If there are triggers, close all the cursors after each iteration
478   ** through the loop.  The fire the after triggers.
479   */
480   if( triggers_exist ){
481     if( !isView ){
482       for(i=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){
483         if( openAll || aIdxUsed[i] )
484           sqlite3VdbeAddOp(v, OP_Close, iCur+i+1, 0);
485       }
486       sqlite3VdbeAddOp(v, OP_Close, iCur, 0);
487     }
488     if( sqlite3CodeRowTrigger(pParse, TK_UPDATE, pChanges, TRIGGER_AFTER, pTab,
489           newIdx, oldIdx, onError, addr) ){
490       goto update_cleanup;
491     }
492   }
493 
494   /* Repeat the above with the next record to be updated, until
495   ** all record selected by the WHERE clause have been updated.
496   */
497   sqlite3VdbeAddOp(v, OP_Goto, 0, addr);
498   sqlite3VdbeJumpHere(v, addr);
499 
500   /* Close all tables if there were no FOR EACH ROW triggers */
501   if( !triggers_exist ){
502     for(i=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){
503       if( openAll || aIdxUsed[i] ){
504         sqlite3VdbeAddOp(v, OP_Close, iCur+i+1, 0);
505       }
506     }
507     sqlite3VdbeAddOp(v, OP_Close, iCur, 0);
508   }else{
509     sqlite3VdbeAddOp(v, OP_Close, newIdx, 0);
510     sqlite3VdbeAddOp(v, OP_Close, oldIdx, 0);
511   }
512 
513   /*
514   ** Return the number of rows that were changed. If this routine is
515   ** generating code because of a call to sqlite3NestedParse(), do not
516   ** invoke the callback function.
517   */
518   if( db->flags & SQLITE_CountRows && !pParse->trigStack && pParse->nested==0 ){
519     sqlite3VdbeAddOp(v, OP_MemLoad, memCnt, 0);
520     sqlite3VdbeAddOp(v, OP_Callback, 1, 0);
521     sqlite3VdbeSetNumCols(v, 1);
522     sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "rows updated", P3_STATIC);
523   }
524 
525 update_cleanup:
526   sqlite3AuthContextPop(&sContext);
527   sqliteFree(apIdx);
528   sqliteFree(aXRef);
529   sqlite3SrcListDelete(pTabList);
530   sqlite3ExprListDelete(pChanges);
531   sqlite3ExprDelete(pWhere);
532   return;
533 }
534 
535 #ifndef SQLITE_OMIT_VIRTUALTABLE
536 /*
537 ** Generate code for an UPDATE of a virtual table.
538 **
539 ** The strategy is that we create an ephemerial table that contains
540 ** for each row to be changed:
541 **
542 **   (A)  The original rowid of that row.
543 **   (B)  The revised rowid for the row. (note1)
544 **   (C)  The content of every column in the row.
545 **
546 ** Then we loop over this ephemeral table and for each row in
547 ** the ephermeral table call VUpdate.
548 **
549 ** When finished, drop the ephemeral table.
550 **
551 ** (note1) Actually, if we know in advance that (A) is always the same
552 ** as (B) we only store (A), then duplicate (A) when pulling
553 ** it out of the ephemeral table before calling VUpdate.
554 */
555 static void updateVirtualTable(
556   Parse *pParse,       /* The parsing context */
557   SrcList *pSrc,       /* The virtual table to be modified */
558   Table *pTab,         /* The virtual table */
559   ExprList *pChanges,  /* The columns to change in the UPDATE statement */
560   Expr *pRowid,        /* Expression used to recompute the rowid */
561   int *aXRef,          /* Mapping from columns of pTab to entries in pChanges */
562   Expr *pWhere         /* WHERE clause of the UPDATE statement */
563 ){
564   Vdbe *v = pParse->pVdbe;  /* Virtual machine under construction */
565   ExprList *pEList = 0;     /* The result set of the SELECT statement */
566   Select *pSelect = 0;      /* The SELECT statement */
567   Expr *pExpr;              /* Temporary expression */
568   int ephemTab;             /* Table holding the result of the SELECT */
569   int i;                    /* Loop counter */
570   int addr;                 /* Address of top of loop */
571 
572   /* Construct the SELECT statement that will find the new values for
573   ** all updated rows.
574   */
575   pEList = sqlite3ExprListAppend(0, sqlite3CreateIdExpr("_rowid_"), 0);
576   if( pRowid ){
577     pEList = sqlite3ExprListAppend(pEList, sqlite3ExprDup(pRowid), 0);
578   }
579   assert( pTab->iPKey<0 );
580   for(i=0; i<pTab->nCol; i++){
581     if( aXRef[i]>=0 ){
582       pExpr = sqlite3ExprDup(pChanges->a[aXRef[i]].pExpr);
583     }else{
584       pExpr = sqlite3CreateIdExpr(pTab->aCol[i].zName);
585     }
586     pEList = sqlite3ExprListAppend(pEList, pExpr, 0);
587   }
588   pSelect = sqlite3SelectNew(pEList, pSrc, pWhere, 0, 0, 0, 0, 0, 0);
589 
590   /* Create the ephemeral table into which the update results will
591   ** be stored.
592   */
593   assert( v );
594   ephemTab = pParse->nTab++;
595   sqlite3VdbeAddOp(v, OP_OpenEphemeral, ephemTab, pTab->nCol+1+(pRowid!=0));
596 
597   /* fill the ephemeral table
598   */
599   sqlite3Select(pParse, pSelect, SRT_Table, ephemTab, 0, 0, 0, 0);
600 
601   /*
602   ** Generate code to scan the ephemeral table and call VDelete and
603   ** VInsert
604   */
605   sqlite3VdbeAddOp(v, OP_Rewind, ephemTab, 0);
606   addr = sqlite3VdbeCurrentAddr(v);
607   sqlite3VdbeAddOp(v, OP_Column,  ephemTab, 0);
608   if( pRowid ){
609     sqlite3VdbeAddOp(v, OP_Column, ephemTab, 1);
610   }else{
611     sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
612   }
613   for(i=0; i<pTab->nCol; i++){
614     sqlite3VdbeAddOp(v, OP_Column, ephemTab, i+1+(pRowid!=0));
615   }
616   pParse->pVirtualLock = pTab;
617   sqlite3VdbeOp3(v, OP_VUpdate, 0, pTab->nCol+2,
618                      (const char*)pTab->pVtab, P3_VTAB);
619   sqlite3VdbeAddOp(v, OP_Next, ephemTab, addr);
620   sqlite3VdbeJumpHere(v, addr-1);
621   sqlite3VdbeAddOp(v, OP_Close, ephemTab, 0);
622 
623   /* Cleanup */
624   sqlite3SelectDelete(pSelect);
625 }
626 #endif /* SQLITE_OMIT_VIRTUALTABLE */
627