blob: e72a6354a363696eedd670ea89739cbb59984829 [file] [log] [blame]
drhcce7d172000-05-31 15:34:51 +00001/*
drhb19a2bc2001-09-16 00:13:26 +00002** 2001 September 15
drhcce7d172000-05-31 15:34:51 +00003**
drhb19a2bc2001-09-16 00:13:26 +00004** The author disclaims copyright to this source code. In place of
5** a legal notice, here is a blessing:
drhcce7d172000-05-31 15:34:51 +00006**
drhb19a2bc2001-09-16 00:13:26 +00007** 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.
drhcce7d172000-05-31 15:34:51 +000010**
11*************************************************************************
12** This file contains C code routines that are called by the parser
13** to handle UPDATE statements.
14**
drhb7916a72009-05-27 10:31:29 +000015** $Id: update.c,v 1.201 2009/05/27 10:31:29 drh Exp $
drhcce7d172000-05-31 15:34:51 +000016*/
17#include "sqliteInt.h"
18
drhedb193b2006-06-27 13:20:21 +000019#ifndef SQLITE_OMIT_VIRTUALTABLE
drh9c419382006-06-16 21:13:21 +000020/* Forward declaration */
21static 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);
drhedb193b2006-06-27 13:20:21 +000030#endif /* SQLITE_OMIT_VIRTUALTABLE */
drh9c419382006-06-16 21:13:21 +000031
drhcce7d172000-05-31 15:34:51 +000032/*
drh8a512562005-11-14 22:29:05 +000033** The most recently coded instruction was an OP_Column to retrieve the
drh66a51672008-01-03 00:01:23 +000034** i-th column of table pTab. This routine sets the P4 parameter of the
danielk1977aee18ef2005-03-09 12:26:50 +000035** 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
drh66a51672008-01-03 00:01:23 +000042** from the P4 parameter of the OP_Column instruction, is returned instead.
danielk1977aee18ef2005-03-09 12:26:50 +000043** If the former, then all row-records are guaranteed to include a value
drh66a51672008-01-03 00:01:23 +000044** for the column and the P4 value is not required.
danielk1977aee18ef2005-03-09 12:26:50 +000045**
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**
drh66a51672008-01-03 00:01:23 +000052** Therefore, the P4 parameter is only required if the default value for
danielk1977aee18ef2005-03-09 12:26:50 +000053** 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*/
57void sqlite3ColumnDefault(Vdbe *v, Table *pTab, int i){
drhe289d602009-05-05 15:46:09 +000058 assert( pTab!=0 );
59 if( !pTab->pSelect ){
danielk1977aee18ef2005-03-09 12:26:50 +000060 sqlite3_value *pValue;
danielk197714db2662006-01-09 16:12:04 +000061 u8 enc = ENC(sqlite3VdbeDb(v));
danielk1977aee18ef2005-03-09 12:26:50 +000062 Column *pCol = &pTab->aCol[i];
drhd4e70eb2008-01-02 00:34:36 +000063 VdbeComment((v, "%s.%s", pTab->zName, pCol->zName));
danielk1977c9cf6e32007-06-25 16:29:33 +000064 assert( i<pTab->nCol );
drhd4e70eb2008-01-02 00:34:36 +000065 sqlite3ValueFromExpr(sqlite3VdbeDb(v), pCol->pDflt, enc,
66 pCol->affinity, &pValue);
drh29dda4a2005-07-21 18:23:20 +000067 if( pValue ){
drh66a51672008-01-03 00:01:23 +000068 sqlite3VdbeChangeP4(v, -1, (const char *)pValue, P4_MEM);
drh29dda4a2005-07-21 18:23:20 +000069 }
danielk1977aee18ef2005-03-09 12:26:50 +000070 }
71}
72
73/*
drhcce7d172000-05-31 15:34:51 +000074** Process an UPDATE statement.
drhb2fe7d82003-04-20 17:29:23 +000075**
76** UPDATE OR IGNORE table_wxyz SET a=b, c=d WHERE e<5 AND f NOT NULL;
77** \_______/ \________/ \______/ \________________/
78* onError pTabList pChanges pWhere
drhcce7d172000-05-31 15:34:51 +000079*/
danielk19774adee202004-05-08 08:23:19 +000080void sqlite3Update(
drhcce7d172000-05-31 15:34:51 +000081 Parse *pParse, /* The parser context */
drh113088e2003-03-20 01:16:58 +000082 SrcList *pTabList, /* The table in which we should change things */
drhcce7d172000-05-31 15:34:51 +000083 ExprList *pChanges, /* Things to be changed */
drh9cfcf5d2002-01-29 18:41:24 +000084 Expr *pWhere, /* The WHERE clause. May be null */
85 int onError /* How to handle constraint errors */
drhcce7d172000-05-31 15:34:51 +000086){
87 int i, j; /* Loop counters */
88 Table *pTab; /* The table to be updated */
danielk1977742f9472004-06-16 12:02:43 +000089 int addr = 0; /* VDBE instruction address of the start of the loop */
drhcce7d172000-05-31 15:34:51 +000090 WhereInfo *pWInfo; /* Information about the WHERE clause */
91 Vdbe *v; /* The virtual database engine */
92 Index *pIdx; /* For looping over indices */
93 int nIdx; /* Number of indices that need updating */
drh6a3ea0e2003-05-02 14:32:12 +000094 int iCur; /* VDBE Cursor number of pTab */
drh9bb575f2004-09-06 17:24:11 +000095 sqlite3 *db; /* The database structure */
drhaa9b8962008-01-08 02:57:55 +000096 int *aRegIdx = 0; /* One register assigned to each index to be updated */
drhcce7d172000-05-31 15:34:51 +000097 int *aXRef = 0; /* aXRef[i] is the index in pChanges->a[] of the
drh967e8b72000-06-21 13:59:10 +000098 ** an expression for the i-th column of the table.
99 ** aXRef[i]==-1 if the i-th column is not changed. */
drhf0863fe2005-06-12 21:35:51 +0000100 int chngRowid; /* True if the record number is being changed */
101 Expr *pRowidExpr = 0; /* Expression defining the new record number */
danielk1977742f9472004-06-16 12:02:43 +0000102 int openAll = 0; /* True if all indices need to be opened */
drh85e20962003-04-25 17:52:11 +0000103 AuthContext sContext; /* The authorization context */
danielk1977b3bce662005-01-29 08:32:43 +0000104 NameContext sNC; /* The name-context to resolve expressions in */
danielk1977da184232006-01-05 11:34:32 +0000105 int iDb; /* Database containing the table being updated */
drha05a7222008-01-19 03:35:58 +0000106 int j1; /* Addresses of jump instructions */
drh08c88eb2008-04-10 13:33:18 +0000107 int okOnePass; /* True for one-pass algorithm without the FIFO */
drhcce7d172000-05-31 15:34:51 +0000108
drh798da522004-11-04 04:42:28 +0000109#ifndef SQLITE_OMIT_TRIGGER
110 int isView; /* Trying to update a view */
danielk19772f886d12009-02-28 10:47:41 +0000111 Trigger *pTrigger; /* List of triggers on pTab, if required */
drh798da522004-11-04 04:42:28 +0000112#endif
drhb27b7f52008-12-10 18:03:45 +0000113 int iBeginAfterTrigger = 0; /* Address of after trigger program */
114 int iEndAfterTrigger = 0; /* Exit of after trigger program */
115 int iBeginBeforeTrigger = 0; /* Address of before trigger program */
116 int iEndBeforeTrigger = 0; /* Exit of before trigger program */
danielk19778f2c54e2008-01-01 19:02:09 +0000117 u32 old_col_mask = 0; /* Mask of OLD.* columns in use */
118 u32 new_col_mask = 0; /* Mask of NEW.* columns in use */
danielk1977c3f9bad2002-05-15 08:30:12 +0000119
120 int newIdx = -1; /* index of trigger "new" temp table */
121 int oldIdx = -1; /* index of trigger "old" temp table */
122
drh04adf412008-01-08 18:57:50 +0000123 /* Register Allocations */
124 int regRowCount = 0; /* A count of rows changed */
125 int regOldRowid; /* The old rowid */
126 int regNewRowid; /* The new rowid */
127 int regData; /* New data for the row */
drh4f21c4a2008-12-10 22:15:00 +0000128 int regRowSet = 0; /* Rowset of rows to be updated */
drh04adf412008-01-08 18:57:50 +0000129
drh85e20962003-04-25 17:52:11 +0000130 sContext.pParse = 0;
drh17435752007-08-16 04:30:38 +0000131 db = pParse->db;
132 if( pParse->nErr || db->mallocFailed ){
drh6f7adc82006-01-11 21:41:20 +0000133 goto update_cleanup;
134 }
drh113088e2003-03-20 01:16:58 +0000135 assert( pTabList->nSrc==1 );
drhdaffd0e2001-04-11 14:28:42 +0000136
drhb2fe7d82003-04-20 17:29:23 +0000137 /* Locate the table which we want to update.
drhcce7d172000-05-31 15:34:51 +0000138 */
danielk19774adee202004-05-08 08:23:19 +0000139 pTab = sqlite3SrcListLookup(pParse, pTabList);
drha69d9162003-04-17 22:57:53 +0000140 if( pTab==0 ) goto update_cleanup;
danielk1977da184232006-01-05 11:34:32 +0000141 iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
drhb7f91642004-10-31 02:22:47 +0000142
143 /* Figure out if we have any triggers and if the table being
144 ** updated is a view
145 */
146#ifndef SQLITE_OMIT_TRIGGER
danielk19772f886d12009-02-28 10:47:41 +0000147 pTrigger = sqlite3TriggersExist(pParse, pTab, TK_UPDATE, pChanges, 0);
drh5cf590c2003-04-24 01:45:04 +0000148 isView = pTab->pSelect!=0;
drhb7f91642004-10-31 02:22:47 +0000149#else
danielk19772f886d12009-02-28 10:47:41 +0000150# define pTrigger 0
drhb7f91642004-10-31 02:22:47 +0000151# define isView 0
152#endif
153#ifdef SQLITE_OMIT_VIEW
154# undef isView
155# define isView 0
156#endif
157
danielk19772f886d12009-02-28 10:47:41 +0000158 if( sqlite3IsReadOnly(pParse, pTab, (pTrigger?1:0)) ){
drh5cf590c2003-04-24 01:45:04 +0000159 goto update_cleanup;
drha69d9162003-04-17 22:57:53 +0000160 }
danielk1977b3d24bf2006-06-19 03:05:10 +0000161 if( sqlite3ViewGetColumnNames(pParse, pTab) ){
162 goto update_cleanup;
drh5cf590c2003-04-24 01:45:04 +0000163 }
drh17435752007-08-16 04:30:38 +0000164 aXRef = sqlite3DbMallocRaw(db, sizeof(int) * pTab->nCol );
drhcce7d172000-05-31 15:34:51 +0000165 if( aXRef==0 ) goto update_cleanup;
166 for(i=0; i<pTab->nCol; i++) aXRef[i] = -1;
167
drhb2fe7d82003-04-20 17:29:23 +0000168 /* If there are FOR EACH ROW triggers, allocate cursors for the
169 ** special OLD and NEW tables
170 */
danielk19772f886d12009-02-28 10:47:41 +0000171 if( pTrigger ){
danielk1977c3f9bad2002-05-15 08:30:12 +0000172 newIdx = pParse->nTab++;
173 oldIdx = pParse->nTab++;
174 }
175
drh53e3fc72002-07-16 17:22:50 +0000176 /* Allocate a cursors for the main database table and for all indices.
177 ** The index cursors might not be used, but if they are used they
178 ** need to occur right after the database cursor. So go ahead and
179 ** allocate enough space, just in case.
180 */
drh6a3ea0e2003-05-02 14:32:12 +0000181 pTabList->a[0].iCursor = iCur = pParse->nTab++;
drh53e3fc72002-07-16 17:22:50 +0000182 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
183 pParse->nTab++;
184 }
185
danielk1977b3bce662005-01-29 08:32:43 +0000186 /* Initialize the name-context */
187 memset(&sNC, 0, sizeof(sNC));
188 sNC.pParse = pParse;
189 sNC.pSrcList = pTabList;
190
drh85e20962003-04-25 17:52:11 +0000191 /* Resolve the column names in all the expressions of the
192 ** of the UPDATE statement. Also find the column index
drhed6c8672003-01-12 18:02:16 +0000193 ** for each column to be updated in the pChanges array. For each
194 ** column to be updated, make sure we have authorization to change
195 ** that column.
drhcce7d172000-05-31 15:34:51 +0000196 */
drhf0863fe2005-06-12 21:35:51 +0000197 chngRowid = 0;
drhcce7d172000-05-31 15:34:51 +0000198 for(i=0; i<pChanges->nExpr; i++){
drh7d10d5a2008-08-20 16:35:10 +0000199 if( sqlite3ResolveExprNames(&sNC, pChanges->a[i].pExpr) ){
drhcce7d172000-05-31 15:34:51 +0000200 goto update_cleanup;
201 }
202 for(j=0; j<pTab->nCol; j++){
danielk19774adee202004-05-08 08:23:19 +0000203 if( sqlite3StrICmp(pTab->aCol[j].zName, pChanges->a[i].zName)==0 ){
drh9647ff82002-01-14 02:56:24 +0000204 if( j==pTab->iPKey ){
drhf0863fe2005-06-12 21:35:51 +0000205 chngRowid = 1;
206 pRowidExpr = pChanges->a[i].pExpr;
drh4a324312001-12-21 14:30:42 +0000207 }
drhcce7d172000-05-31 15:34:51 +0000208 aXRef[j] = i;
209 break;
210 }
211 }
212 if( j>=pTab->nCol ){
danielk19774adee202004-05-08 08:23:19 +0000213 if( sqlite3IsRowid(pChanges->a[i].zName) ){
drhf0863fe2005-06-12 21:35:51 +0000214 chngRowid = 1;
215 pRowidExpr = pChanges->a[i].pExpr;
drha0217ba2003-06-01 01:10:33 +0000216 }else{
danielk19774adee202004-05-08 08:23:19 +0000217 sqlite3ErrorMsg(pParse, "no such column: %s", pChanges->a[i].zName);
drha0217ba2003-06-01 01:10:33 +0000218 goto update_cleanup;
219 }
drhcce7d172000-05-31 15:34:51 +0000220 }
drhed6c8672003-01-12 18:02:16 +0000221#ifndef SQLITE_OMIT_AUTHORIZATION
drhe5f9c642003-01-13 23:27:31 +0000222 {
223 int rc;
danielk19774adee202004-05-08 08:23:19 +0000224 rc = sqlite3AuthCheck(pParse, SQLITE_UPDATE, pTab->zName,
danielk1977da184232006-01-05 11:34:32 +0000225 pTab->aCol[j].zName, db->aDb[iDb].zName);
drhe5f9c642003-01-13 23:27:31 +0000226 if( rc==SQLITE_DENY ){
227 goto update_cleanup;
228 }else if( rc==SQLITE_IGNORE ){
229 aXRef[j] = -1;
230 }
drhed6c8672003-01-12 18:02:16 +0000231 }
232#endif
drhcce7d172000-05-31 15:34:51 +0000233 }
234
drhaa9b8962008-01-08 02:57:55 +0000235 /* Allocate memory for the array aRegIdx[]. There is one entry in the
236 ** array for each index associated with table being updated. Fill in
237 ** the value with a register number for indices that are to be used
238 ** and with zero for unused indices.
drhcce7d172000-05-31 15:34:51 +0000239 */
drhaa9b8962008-01-08 02:57:55 +0000240 for(nIdx=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, nIdx++){}
241 if( nIdx>0 ){
242 aRegIdx = sqlite3DbMallocRaw(db, sizeof(Index*) * nIdx );
243 if( aRegIdx==0 ) goto update_cleanup;
244 }
245 for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
246 int reg;
drhf0863fe2005-06-12 21:35:51 +0000247 if( chngRowid ){
drhaa9b8962008-01-08 02:57:55 +0000248 reg = ++pParse->nMem;
249 }else{
250 reg = 0;
drh4a324312001-12-21 14:30:42 +0000251 for(i=0; i<pIdx->nColumn; i++){
drhaa9b8962008-01-08 02:57:55 +0000252 if( aXRef[pIdx->aiColumn[i]]>=0 ){
253 reg = ++pParse->nMem;
254 break;
255 }
drh4a324312001-12-21 14:30:42 +0000256 }
drhcce7d172000-05-31 15:34:51 +0000257 }
drhaa9b8962008-01-08 02:57:55 +0000258 aRegIdx[j] = reg;
drhcce7d172000-05-31 15:34:51 +0000259 }
260
drh04adf412008-01-08 18:57:50 +0000261 /* Allocate a block of register used to store the change record
262 ** sent to sqlite3GenerateConstraintChecks(). There are either
263 ** one or two registers for holding the rowid. One rowid register
264 ** is used if chngRowid is false and two are used if chngRowid is
265 ** true. Following these are pTab->nCol register holding column
266 ** data.
267 */
268 regOldRowid = regNewRowid = pParse->nMem + 1;
269 pParse->nMem += pTab->nCol + 1;
270 if( chngRowid ){
271 regNewRowid++;
272 pParse->nMem++;
273 }
274 regData = regNewRowid+1;
275
276
drhcce7d172000-05-31 15:34:51 +0000277 /* Begin generating code.
278 */
danielk19774adee202004-05-08 08:23:19 +0000279 v = sqlite3GetVdbe(pParse);
drhcce7d172000-05-31 15:34:51 +0000280 if( v==0 ) goto update_cleanup;
drh4794f732004-11-05 17:17:50 +0000281 if( pParse->nested==0 ) sqlite3VdbeCountChanges(v);
danielk1977da184232006-01-05 11:34:32 +0000282 sqlite3BeginWriteOperation(pParse, 1, iDb);
drhcce7d172000-05-31 15:34:51 +0000283
drh9c419382006-06-16 21:13:21 +0000284#ifndef SQLITE_OMIT_VIRTUALTABLE
285 /* Virtual tables must be handled separately */
286 if( IsVirtual(pTab) ){
287 updateVirtualTable(pParse, pTabList, pTab, pChanges, pRowidExpr, aXRef,
288 pWhere);
289 pWhere = 0;
290 pTabList = 0;
291 goto update_cleanup;
292 }
293#endif
294
danielk19774273dea2006-06-17 06:31:18 +0000295 /* Start the view context
296 */
297 if( isView ){
298 sqlite3AuthContextPush(pParse, &sContext, pTab->zName);
299 }
300
danielk19778f2c54e2008-01-01 19:02:09 +0000301 /* Generate the code for triggers.
302 */
danielk19772f886d12009-02-28 10:47:41 +0000303 if( pTrigger ){
danielk1977e448dc42008-01-02 11:50:51 +0000304 int iGoto;
305
306 /* Create pseudo-tables for NEW and OLD
307 */
danielk1977d336e222009-02-20 10:58:41 +0000308 sqlite3VdbeAddOp3(v, OP_OpenPseudo, oldIdx, 0, pTab->nCol);
309 sqlite3VdbeAddOp3(v, OP_OpenPseudo, newIdx, 0, pTab->nCol);
danielk1977e448dc42008-01-02 11:50:51 +0000310
drh66a51672008-01-03 00:01:23 +0000311 iGoto = sqlite3VdbeAddOp2(v, OP_Goto, 0, 0);
danielk19778f2c54e2008-01-01 19:02:09 +0000312 addr = sqlite3VdbeMakeLabel(v);
313 iBeginBeforeTrigger = sqlite3VdbeCurrentAddr(v);
danielk19772f886d12009-02-28 10:47:41 +0000314 if( sqlite3CodeRowTrigger(pParse, pTrigger, TK_UPDATE, pChanges,
315 TRIGGER_BEFORE, pTab, newIdx, oldIdx, onError, addr,
316 &old_col_mask, &new_col_mask) ){
danielk19778f2c54e2008-01-01 19:02:09 +0000317 goto update_cleanup;
318 }
drh66a51672008-01-03 00:01:23 +0000319 iEndBeforeTrigger = sqlite3VdbeAddOp2(v, OP_Goto, 0, 0);
danielk19778f2c54e2008-01-01 19:02:09 +0000320 iBeginAfterTrigger = sqlite3VdbeCurrentAddr(v);
danielk19772f886d12009-02-28 10:47:41 +0000321 if( sqlite3CodeRowTrigger(pParse, pTrigger, TK_UPDATE, pChanges,
322 TRIGGER_AFTER, pTab, newIdx, oldIdx, onError, addr,
323 &old_col_mask, &new_col_mask) ){
danielk19778f2c54e2008-01-01 19:02:09 +0000324 goto update_cleanup;
325 }
drh66a51672008-01-03 00:01:23 +0000326 iEndAfterTrigger = sqlite3VdbeAddOp2(v, OP_Goto, 0, 0);
danielk19778f2c54e2008-01-01 19:02:09 +0000327 sqlite3VdbeJumpHere(v, iGoto);
328 }
329
drh9d2985c2005-09-08 01:58:42 +0000330 /* If we are trying to update a view, realize that view into
331 ** a ephemeral table.
drh5cf590c2003-04-24 01:45:04 +0000332 */
shanefa4e62f2008-09-01 21:59:42 +0000333#if !defined(SQLITE_OMIT_VIEW) && !defined(SQLITE_OMIT_TRIGGER)
drh5cf590c2003-04-24 01:45:04 +0000334 if( isView ){
drh2a5d8252008-08-22 12:30:52 +0000335 sqlite3MaterializeView(pParse, pTab, pWhere, iCur);
drh0f35a6b2008-02-12 16:52:14 +0000336 }
shanefa4e62f2008-09-01 21:59:42 +0000337#endif
drh1013c932008-01-06 00:25:21 +0000338
drh0f35a6b2008-02-12 16:52:14 +0000339 /* Resolve the column names in all the expressions in the
340 ** WHERE clause.
341 */
drh7d10d5a2008-08-20 16:35:10 +0000342 if( sqlite3ResolveExprNames(&sNC, pWhere) ){
drh0f35a6b2008-02-12 16:52:14 +0000343 goto update_cleanup;
drh5cf590c2003-04-24 01:45:04 +0000344 }
345
drhcce7d172000-05-31 15:34:51 +0000346 /* Begin the database scan
347 */
drh08c88eb2008-04-10 13:33:18 +0000348 sqlite3VdbeAddOp2(v, OP_Null, 0, regOldRowid);
drh336a5302009-04-24 15:46:21 +0000349 pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere,0, WHERE_ONEPASS_DESIRED);
drhcce7d172000-05-31 15:34:51 +0000350 if( pWInfo==0 ) goto update_cleanup;
drh08c88eb2008-04-10 13:33:18 +0000351 okOnePass = pWInfo->okOnePass;
drhcce7d172000-05-31 15:34:51 +0000352
drh4cbdda92006-06-14 19:00:20 +0000353 /* Remember the rowid of every item to be updated.
drhcce7d172000-05-31 15:34:51 +0000354 */
drh044925b2009-04-22 17:15:02 +0000355 sqlite3VdbeAddOp2(v, OP_Rowid, iCur, regOldRowid);
drh3d4501e2008-12-04 20:40:10 +0000356 if( !okOnePass ){
357 regRowSet = ++pParse->nMem;
358 sqlite3VdbeAddOp2(v, OP_RowSetAdd, regRowSet, regOldRowid);
359 }
drhcce7d172000-05-31 15:34:51 +0000360
361 /* End the database scan loop.
362 */
danielk19774adee202004-05-08 08:23:19 +0000363 sqlite3WhereEnd(pWInfo);
drhcce7d172000-05-31 15:34:51 +0000364
drh1bee3d72001-10-15 00:44:35 +0000365 /* Initialize the count of updated rows
366 */
danielk1977c3f9bad2002-05-15 08:30:12 +0000367 if( db->flags & SQLITE_CountRows && !pParse->trigStack ){
drh04adf412008-01-08 18:57:50 +0000368 regRowCount = ++pParse->nMem;
369 sqlite3VdbeAddOp2(v, OP_Integer, 0, regRowCount);
drh1bee3d72001-10-15 00:44:35 +0000370 }
371
drhe289d602009-05-05 15:46:09 +0000372 if( !isView ){
danielk1977e448dc42008-01-02 11:50:51 +0000373 /*
374 ** Open every index that needs updating. Note that if any
375 ** index could potentially invoke a REPLACE conflict resolution
376 ** action, then we need to open all indices because we might need
377 ** to be deleting some records.
drh70ce3f02003-04-15 19:22:22 +0000378 */
drh08c88eb2008-04-10 13:33:18 +0000379 if( !okOnePass ) sqlite3OpenTable(pParse, iCur, iDb, pTab, OP_OpenWrite);
danielk1977e448dc42008-01-02 11:50:51 +0000380 if( onError==OE_Replace ){
381 openAll = 1;
382 }else{
383 openAll = 0;
384 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
385 if( pIdx->onError==OE_Replace ){
386 openAll = 1;
387 break;
388 }
389 }
drh5cf590c2003-04-24 01:45:04 +0000390 }
danielk1977e448dc42008-01-02 11:50:51 +0000391 for(i=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){
drhaa9b8962008-01-08 02:57:55 +0000392 if( openAll || aRegIdx[i]>0 ){
danielk1977e448dc42008-01-02 11:50:51 +0000393 KeyInfo *pKey = sqlite3IndexKeyinfo(pParse, pIdx);
danielk1977207872a2008-01-03 07:54:23 +0000394 sqlite3VdbeAddOp4(v, OP_OpenWrite, iCur+i+1, pIdx->tnum, iDb,
drh66a51672008-01-03 00:01:23 +0000395 (char*)pKey, P4_KEYINFO_HANDOFF);
danielk1977e448dc42008-01-02 11:50:51 +0000396 assert( pParse->nTab>iCur+i+1 );
397 }
398 }
danielk1977e448dc42008-01-02 11:50:51 +0000399 }
400
401 /* Jump back to this point if a trigger encounters an IGNORE constraint. */
danielk19772f886d12009-02-28 10:47:41 +0000402 if( pTrigger ){
danielk1977e448dc42008-01-02 11:50:51 +0000403 sqlite3VdbeResolveLabel(v, addr);
404 }
405
406 /* Top of the update loop */
drh08c88eb2008-04-10 13:33:18 +0000407 if( okOnePass ){
408 int a1 = sqlite3VdbeAddOp1(v, OP_NotNull, regOldRowid);
409 addr = sqlite3VdbeAddOp0(v, OP_Goto);
410 sqlite3VdbeJumpHere(v, a1);
411 }else{
drh3d4501e2008-12-04 20:40:10 +0000412 addr = sqlite3VdbeAddOp3(v, OP_RowSetRead, regRowSet, 0, regOldRowid);
drh08c88eb2008-04-10 13:33:18 +0000413 }
danielk1977e448dc42008-01-02 11:50:51 +0000414
danielk19772f886d12009-02-28 10:47:41 +0000415 if( pTrigger ){
drh2d401ab2008-01-10 23:50:11 +0000416 int regRowid;
417 int regRow;
418 int regCols;
419
danielk1977e448dc42008-01-02 11:50:51 +0000420 /* Make cursor iCur point to the record that is being updated.
421 */
drh04adf412008-01-08 18:57:50 +0000422 sqlite3VdbeAddOp3(v, OP_NotExists, iCur, addr, regOldRowid);
danielk1977c3f9bad2002-05-15 08:30:12 +0000423
drh70ce3f02003-04-15 19:22:22 +0000424 /* Generate the OLD table
425 */
drh2d401ab2008-01-10 23:50:11 +0000426 regRowid = sqlite3GetTempReg(pParse);
427 regRow = sqlite3GetTempReg(pParse);
428 sqlite3VdbeAddOp2(v, OP_Rowid, iCur, regRowid);
danielk19778f2c54e2008-01-01 19:02:09 +0000429 if( !old_col_mask ){
drh2d401ab2008-01-10 23:50:11 +0000430 sqlite3VdbeAddOp2(v, OP_Null, 0, regRow);
danielk19778f2c54e2008-01-01 19:02:09 +0000431 }else{
drh2d401ab2008-01-10 23:50:11 +0000432 sqlite3VdbeAddOp2(v, OP_RowData, iCur, regRow);
danielk19778f2c54e2008-01-01 19:02:09 +0000433 }
drh2d401ab2008-01-10 23:50:11 +0000434 sqlite3VdbeAddOp3(v, OP_Insert, oldIdx, regRow, regRowid);
danielk1977c3f9bad2002-05-15 08:30:12 +0000435
drh70ce3f02003-04-15 19:22:22 +0000436 /* Generate the NEW table
437 */
drhf0863fe2005-06-12 21:35:51 +0000438 if( chngRowid ){
drh2d401ab2008-01-10 23:50:11 +0000439 sqlite3ExprCodeAndCache(pParse, pRowidExpr, regRowid);
danielk1977510f9652008-10-09 18:48:30 +0000440 sqlite3VdbeAddOp1(v, OP_MustBeInt, regRowid);
drh70ce3f02003-04-15 19:22:22 +0000441 }else{
drh2d401ab2008-01-10 23:50:11 +0000442 sqlite3VdbeAddOp2(v, OP_Rowid, iCur, regRowid);
drh70ce3f02003-04-15 19:22:22 +0000443 }
drh2d401ab2008-01-10 23:50:11 +0000444 regCols = sqlite3GetTempRange(pParse, pTab->nCol);
drh25303782004-12-07 15:41:48 +0000445 for(i=0; i<pTab->nCol; i++){
drh70ce3f02003-04-15 19:22:22 +0000446 if( i==pTab->iPKey ){
drh2d401ab2008-01-10 23:50:11 +0000447 sqlite3VdbeAddOp2(v, OP_Null, 0, regCols+i);
drh70ce3f02003-04-15 19:22:22 +0000448 continue;
449 }
450 j = aXRef[i];
drhe289d602009-05-05 15:46:09 +0000451 if( (i<32 && (new_col_mask&((u32)1<<i))!=0) || new_col_mask==0xffffffff ){
danielk19778f2c54e2008-01-01 19:02:09 +0000452 if( j<0 ){
drh2d401ab2008-01-10 23:50:11 +0000453 sqlite3VdbeAddOp3(v, OP_Column, iCur, i, regCols+i);
danielk19778f2c54e2008-01-01 19:02:09 +0000454 sqlite3ColumnDefault(v, pTab, i);
455 }else{
drh2d401ab2008-01-10 23:50:11 +0000456 sqlite3ExprCodeAndCache(pParse, pChanges->a[j].pExpr, regCols+i);
danielk19778f2c54e2008-01-01 19:02:09 +0000457 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000458 }else{
drh2d401ab2008-01-10 23:50:11 +0000459 sqlite3VdbeAddOp2(v, OP_Null, 0, regCols+i);
danielk1977c3f9bad2002-05-15 08:30:12 +0000460 }
461 }
drh1db639c2008-01-17 02:36:28 +0000462 sqlite3VdbeAddOp3(v, OP_MakeRecord, regCols, pTab->nCol, regRow);
danielk1977a37cdde2004-05-16 11:15:36 +0000463 if( !isView ){
464 sqlite3TableAffinityStr(v, pTab);
drhda250ea2008-04-01 05:07:14 +0000465 sqlite3ExprCacheAffinityChange(pParse, regCols, pTab->nCol);
danielk1977a37cdde2004-05-16 11:15:36 +0000466 }
drh2d401ab2008-01-10 23:50:11 +0000467 sqlite3ReleaseTempRange(pParse, regCols, pTab->nCol);
drhaf005fb2008-07-09 16:51:51 +0000468 /* if( pParse->nErr ) goto update_cleanup; */
drh2d401ab2008-01-10 23:50:11 +0000469 sqlite3VdbeAddOp3(v, OP_Insert, newIdx, regRow, regRowid);
470 sqlite3ReleaseTempReg(pParse, regRowid);
471 sqlite3ReleaseTempReg(pParse, regRow);
danielk1977c3f9bad2002-05-15 08:30:12 +0000472
drh66a51672008-01-03 00:01:23 +0000473 sqlite3VdbeAddOp2(v, OP_Goto, 0, iBeginBeforeTrigger);
danielk19778f2c54e2008-01-01 19:02:09 +0000474 sqlite3VdbeJumpHere(v, iEndBeforeTrigger);
danielk1977c3f9bad2002-05-15 08:30:12 +0000475 }
476
drhe289d602009-05-05 15:46:09 +0000477 if( !isView ){
drh5cf590c2003-04-24 01:45:04 +0000478 /* Loop over every record that needs updating. We have to load
479 ** the old data for each record to be updated because some columns
480 ** might not change and we will need to copy the old value.
drh4cbdda92006-06-14 19:00:20 +0000481 ** Also, the old data is needed to delete the old index entries.
drh5cf590c2003-04-24 01:45:04 +0000482 ** So make the cursor point at the old record.
483 */
drh04adf412008-01-08 18:57:50 +0000484 sqlite3VdbeAddOp3(v, OP_NotExists, iCur, addr, regOldRowid);
drh5cf590c2003-04-24 01:45:04 +0000485
486 /* If the record number will change, push the record number as it
487 ** will be after the update. (The old record number is currently
488 ** on top of the stack.)
489 */
drhf0863fe2005-06-12 21:35:51 +0000490 if( chngRowid ){
drh04adf412008-01-08 18:57:50 +0000491 sqlite3ExprCode(pParse, pRowidExpr, regNewRowid);
drh3c84ddf2008-01-09 02:15:38 +0000492 sqlite3VdbeAddOp1(v, OP_MustBeInt, regNewRowid);
drhcce7d172000-05-31 15:34:51 +0000493 }
drh5cf590c2003-04-24 01:45:04 +0000494
495 /* Compute new data for this record.
496 */
497 for(i=0; i<pTab->nCol; i++){
498 if( i==pTab->iPKey ){
drh04adf412008-01-08 18:57:50 +0000499 sqlite3VdbeAddOp2(v, OP_Null, 0, regData+i);
drh5cf590c2003-04-24 01:45:04 +0000500 continue;
501 }
502 j = aXRef[i];
503 if( j<0 ){
drh04adf412008-01-08 18:57:50 +0000504 sqlite3VdbeAddOp3(v, OP_Column, iCur, i, regData+i);
danielk1977aee18ef2005-03-09 12:26:50 +0000505 sqlite3ColumnDefault(v, pTab, i);
drh5cf590c2003-04-24 01:45:04 +0000506 }else{
drh04adf412008-01-08 18:57:50 +0000507 sqlite3ExprCode(pParse, pChanges->a[j].pExpr, regData+i);
drh5cf590c2003-04-24 01:45:04 +0000508 }
509 }
510
511 /* Do constraint checks
512 */
drh04adf412008-01-08 18:57:50 +0000513 sqlite3GenerateConstraintChecks(pParse, pTab, iCur, regNewRowid,
514 aRegIdx, chngRowid, 1,
danielk1977de630352009-05-04 11:42:29 +0000515 onError, addr, 0);
drh5cf590c2003-04-24 01:45:04 +0000516
517 /* Delete the old indices for the current record.
518 */
drha05a7222008-01-19 03:35:58 +0000519 j1 = sqlite3VdbeAddOp3(v, OP_NotExists, iCur, 0, regOldRowid);
drh2d401ab2008-01-10 23:50:11 +0000520 sqlite3GenerateRowIndexDelete(pParse, pTab, iCur, aRegIdx);
drh5cf590c2003-04-24 01:45:04 +0000521
522 /* If changing the record number, delete the old record.
523 */
drhf0863fe2005-06-12 21:35:51 +0000524 if( chngRowid ){
drh66a51672008-01-03 00:01:23 +0000525 sqlite3VdbeAddOp2(v, OP_Delete, iCur, 0);
drh5cf590c2003-04-24 01:45:04 +0000526 }
drha05a7222008-01-19 03:35:58 +0000527 sqlite3VdbeJumpHere(v, j1);
drh5cf590c2003-04-24 01:45:04 +0000528
529 /* Create the new index entries and the new record.
530 */
drh04adf412008-01-08 18:57:50 +0000531 sqlite3CompleteInsertion(pParse, pTab, iCur, regNewRowid,
danielk1977de630352009-05-04 11:42:29 +0000532 aRegIdx, 1, -1, 0, 0);
drhcce7d172000-05-31 15:34:51 +0000533 }
534
drh0ca3e242002-01-29 23:07:02 +0000535 /* Increment the row counter
drh1bee3d72001-10-15 00:44:35 +0000536 */
danielk1977c3f9bad2002-05-15 08:30:12 +0000537 if( db->flags & SQLITE_CountRows && !pParse->trigStack){
drh04adf412008-01-08 18:57:50 +0000538 sqlite3VdbeAddOp2(v, OP_AddImm, regRowCount, 1);
drh1bee3d72001-10-15 00:44:35 +0000539 }
540
drh5cf590c2003-04-24 01:45:04 +0000541 /* If there are triggers, close all the cursors after each iteration
542 ** through the loop. The fire the after triggers.
543 */
danielk19772f886d12009-02-28 10:47:41 +0000544 if( pTrigger ){
drh66a51672008-01-03 00:01:23 +0000545 sqlite3VdbeAddOp2(v, OP_Goto, 0, iBeginAfterTrigger);
danielk19778f2c54e2008-01-01 19:02:09 +0000546 sqlite3VdbeJumpHere(v, iEndAfterTrigger);
danielk1977c3f9bad2002-05-15 08:30:12 +0000547 }
548
drhcce7d172000-05-31 15:34:51 +0000549 /* Repeat the above with the next record to be updated, until
550 ** all record selected by the WHERE clause have been updated.
551 */
drh66a51672008-01-03 00:01:23 +0000552 sqlite3VdbeAddOp2(v, OP_Goto, 0, addr);
drhd654be82005-09-20 17:42:23 +0000553 sqlite3VdbeJumpHere(v, addr);
danielk1977c3f9bad2002-05-15 08:30:12 +0000554
danielk1977e448dc42008-01-02 11:50:51 +0000555 /* Close all tables */
556 for(i=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){
drhaa9b8962008-01-08 02:57:55 +0000557 if( openAll || aRegIdx[i]>0 ){
drh66a51672008-01-03 00:01:23 +0000558 sqlite3VdbeAddOp2(v, OP_Close, iCur+i+1, 0);
danielk1977c3f9bad2002-05-15 08:30:12 +0000559 }
danielk1977e448dc42008-01-02 11:50:51 +0000560 }
drh66a51672008-01-03 00:01:23 +0000561 sqlite3VdbeAddOp2(v, OP_Close, iCur, 0);
danielk19772f886d12009-02-28 10:47:41 +0000562 if( pTrigger ){
drh66a51672008-01-03 00:01:23 +0000563 sqlite3VdbeAddOp2(v, OP_Close, newIdx, 0);
564 sqlite3VdbeAddOp2(v, OP_Close, oldIdx, 0);
danielk1977c3f9bad2002-05-15 08:30:12 +0000565 }
566
drh1bee3d72001-10-15 00:44:35 +0000567 /*
danielk1977e7de6f22004-11-05 06:02:06 +0000568 ** Return the number of rows that were changed. If this routine is
569 ** generating code because of a call to sqlite3NestedParse(), do not
570 ** invoke the callback function.
drh1bee3d72001-10-15 00:44:35 +0000571 */
danielk1977e7de6f22004-11-05 06:02:06 +0000572 if( db->flags & SQLITE_CountRows && !pParse->trigStack && pParse->nested==0 ){
drh04adf412008-01-08 18:57:50 +0000573 sqlite3VdbeAddOp2(v, OP_ResultRow, regRowCount, 1);
danielk197722322fd2004-05-25 23:35:17 +0000574 sqlite3VdbeSetNumCols(v, 1);
danielk197710fb7492008-10-31 10:53:22 +0000575 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "rows updated", SQLITE_STATIC);
drh1bee3d72001-10-15 00:44:35 +0000576 }
577
drhcce7d172000-05-31 15:34:51 +0000578update_cleanup:
danielk19774adee202004-05-08 08:23:19 +0000579 sqlite3AuthContextPop(&sContext);
drh633e6d52008-07-28 19:34:53 +0000580 sqlite3DbFree(db, aRegIdx);
581 sqlite3DbFree(db, aXRef);
582 sqlite3SrcListDelete(db, pTabList);
583 sqlite3ExprListDelete(db, pChanges);
584 sqlite3ExprDelete(db, pWhere);
drhcce7d172000-05-31 15:34:51 +0000585 return;
586}
drh9c419382006-06-16 21:13:21 +0000587
588#ifndef SQLITE_OMIT_VIRTUALTABLE
589/*
590** Generate code for an UPDATE of a virtual table.
591**
592** The strategy is that we create an ephemerial table that contains
593** for each row to be changed:
594**
595** (A) The original rowid of that row.
596** (B) The revised rowid for the row. (note1)
597** (C) The content of every column in the row.
598**
599** Then we loop over this ephemeral table and for each row in
600** the ephermeral table call VUpdate.
601**
602** When finished, drop the ephemeral table.
603**
604** (note1) Actually, if we know in advance that (A) is always the same
605** as (B) we only store (A), then duplicate (A) when pulling
606** it out of the ephemeral table before calling VUpdate.
607*/
608static void updateVirtualTable(
609 Parse *pParse, /* The parsing context */
610 SrcList *pSrc, /* The virtual table to be modified */
611 Table *pTab, /* The virtual table */
612 ExprList *pChanges, /* The columns to change in the UPDATE statement */
613 Expr *pRowid, /* Expression used to recompute the rowid */
614 int *aXRef, /* Mapping from columns of pTab to entries in pChanges */
615 Expr *pWhere /* WHERE clause of the UPDATE statement */
616){
617 Vdbe *v = pParse->pVdbe; /* Virtual machine under construction */
618 ExprList *pEList = 0; /* The result set of the SELECT statement */
619 Select *pSelect = 0; /* The SELECT statement */
620 Expr *pExpr; /* Temporary expression */
621 int ephemTab; /* Table holding the result of the SELECT */
622 int i; /* Loop counter */
623 int addr; /* Address of top of loop */
danielk1977287fb612008-01-04 19:10:28 +0000624 int iReg; /* First register in set passed to OP_VUpdate */
drh17435752007-08-16 04:30:38 +0000625 sqlite3 *db = pParse->db; /* Database connection */
danielk1977287fb612008-01-04 19:10:28 +0000626 const char *pVtab = (const char*)pTab->pVtab;
drh1013c932008-01-06 00:25:21 +0000627 SelectDest dest;
drh9c419382006-06-16 21:13:21 +0000628
629 /* Construct the SELECT statement that will find the new values for
630 ** all updated rows.
631 */
drh17435752007-08-16 04:30:38 +0000632 pEList = sqlite3ExprListAppend(pParse, 0,
drhb7916a72009-05-27 10:31:29 +0000633 sqlite3CreateIdExpr(pParse, "_rowid_"));
drh9c419382006-06-16 21:13:21 +0000634 if( pRowid ){
drh17435752007-08-16 04:30:38 +0000635 pEList = sqlite3ExprListAppend(pParse, pEList,
drhb7916a72009-05-27 10:31:29 +0000636 sqlite3ExprDup(db, pRowid, 0));
drh9c419382006-06-16 21:13:21 +0000637 }
danielk197765fd59f2006-06-24 11:51:33 +0000638 assert( pTab->iPKey<0 );
drh9c419382006-06-16 21:13:21 +0000639 for(i=0; i<pTab->nCol; i++){
danielk197765fd59f2006-06-24 11:51:33 +0000640 if( aXRef[i]>=0 ){
danielk19776ab3a2e2009-02-19 14:39:25 +0000641 pExpr = sqlite3ExprDup(db, pChanges->a[aXRef[i]].pExpr, 0);
drh9c419382006-06-16 21:13:21 +0000642 }else{
drh17435752007-08-16 04:30:38 +0000643 pExpr = sqlite3CreateIdExpr(pParse, pTab->aCol[i].zName);
drh9c419382006-06-16 21:13:21 +0000644 }
drhb7916a72009-05-27 10:31:29 +0000645 pEList = sqlite3ExprListAppend(pParse, pEList, pExpr);
drh9c419382006-06-16 21:13:21 +0000646 }
drh17435752007-08-16 04:30:38 +0000647 pSelect = sqlite3SelectNew(pParse, pEList, pSrc, pWhere, 0, 0, 0, 0, 0, 0);
drh9c419382006-06-16 21:13:21 +0000648
649 /* Create the ephemeral table into which the update results will
650 ** be stored.
651 */
652 assert( v );
653 ephemTab = pParse->nTab++;
drh66a51672008-01-03 00:01:23 +0000654 sqlite3VdbeAddOp2(v, OP_OpenEphemeral, ephemTab, pTab->nCol+1+(pRowid!=0));
drh9c419382006-06-16 21:13:21 +0000655
656 /* fill the ephemeral table
657 */
drh1013c932008-01-06 00:25:21 +0000658 sqlite3SelectDestInit(&dest, SRT_Table, ephemTab);
drh7d10d5a2008-08-20 16:35:10 +0000659 sqlite3Select(pParse, pSelect, &dest);
drh9c419382006-06-16 21:13:21 +0000660
danielk1977287fb612008-01-04 19:10:28 +0000661 /* Generate code to scan the ephemeral table and call VUpdate. */
662 iReg = ++pParse->nMem;
663 pParse->nMem += pTab->nCol+1;
drh66a51672008-01-03 00:01:23 +0000664 sqlite3VdbeAddOp2(v, OP_Rewind, ephemTab, 0);
drh9c419382006-06-16 21:13:21 +0000665 addr = sqlite3VdbeCurrentAddr(v);
danielk1977287fb612008-01-04 19:10:28 +0000666 sqlite3VdbeAddOp3(v, OP_Column, ephemTab, 0, iReg);
667 sqlite3VdbeAddOp3(v, OP_Column, ephemTab, (pRowid?1:0), iReg+1);
drh9c419382006-06-16 21:13:21 +0000668 for(i=0; i<pTab->nCol; i++){
danielk1977287fb612008-01-04 19:10:28 +0000669 sqlite3VdbeAddOp3(v, OP_Column, ephemTab, i+1+(pRowid!=0), iReg+2+i);
drh9c419382006-06-16 21:13:21 +0000670 }
drh4f3dd152008-04-28 18:46:43 +0000671 sqlite3VtabMakeWritable(pParse, pTab);
danielk1977287fb612008-01-04 19:10:28 +0000672 sqlite3VdbeAddOp4(v, OP_VUpdate, 0, pTab->nCol+2, iReg, pVtab, P4_VTAB);
drh66a51672008-01-03 00:01:23 +0000673 sqlite3VdbeAddOp2(v, OP_Next, ephemTab, addr);
danielk197707fd0592007-02-21 17:04:04 +0000674 sqlite3VdbeJumpHere(v, addr-1);
drh66a51672008-01-03 00:01:23 +0000675 sqlite3VdbeAddOp2(v, OP_Close, ephemTab, 0);
drh9c419382006-06-16 21:13:21 +0000676
677 /* Cleanup */
drh633e6d52008-07-28 19:34:53 +0000678 sqlite3SelectDelete(db, pSelect);
drh9c419382006-06-16 21:13:21 +0000679}
680#endif /* SQLITE_OMIT_VIRTUALTABLE */
drhf39d9582008-03-19 20:42:13 +0000681
682/* Make sure "isView" gets undefined in case this file becomes part of
683** the amalgamation - so that subsequent files do not see isView as a
684** macro. */
685#undef isView