blob: 7c6017a7efc7b9a196e8cb2119d011e16e69a605 [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**
danielk1977e448dc42008-01-02 11:50:51 +000015** $Id: update.c,v 1.147 2008/01/02 11:50:51 danielk1977 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
34** i-th column of table pTab. This routine sets the P3 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
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*/
57void sqlite3ColumnDefault(Vdbe *v, Table *pTab, int i){
58 if( pTab && !pTab->pSelect ){
59 sqlite3_value *pValue;
danielk197714db2662006-01-09 16:12:04 +000060 u8 enc = ENC(sqlite3VdbeDb(v));
danielk1977aee18ef2005-03-09 12:26:50 +000061 Column *pCol = &pTab->aCol[i];
drhd4e70eb2008-01-02 00:34:36 +000062 VdbeComment((v, "%s.%s", pTab->zName, pCol->zName));
danielk1977c9cf6e32007-06-25 16:29:33 +000063 assert( i<pTab->nCol );
drhd4e70eb2008-01-02 00:34:36 +000064 sqlite3ValueFromExpr(sqlite3VdbeDb(v), pCol->pDflt, enc,
65 pCol->affinity, &pValue);
drh29dda4a2005-07-21 18:23:20 +000066 if( pValue ){
drhd4e70eb2008-01-02 00:34:36 +000067 sqlite3VdbeAddOp(v, OP_DfltValue, 0, 0);
drh29dda4a2005-07-21 18:23:20 +000068 sqlite3VdbeChangeP3(v, -1, (const char *)pValue, P3_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 */
drh0ca3e242002-01-29 23:07:02 +000094 int nIdxTotal; /* Total number of indices */
drh6a3ea0e2003-05-02 14:32:12 +000095 int iCur; /* VDBE Cursor number of pTab */
drh9bb575f2004-09-06 17:24:11 +000096 sqlite3 *db; /* The database structure */
drhcce7d172000-05-31 15:34:51 +000097 Index **apIdx = 0; /* An array of indices that need updating too */
drhb2fe7d82003-04-20 17:29:23 +000098 char *aIdxUsed = 0; /* aIdxUsed[i]==1 if the i-th index is used */
drhcce7d172000-05-31 15:34:51 +000099 int *aXRef = 0; /* aXRef[i] is the index in pChanges->a[] of the
drh967e8b72000-06-21 13:59:10 +0000100 ** an expression for the i-th column of the table.
101 ** aXRef[i]==-1 if the i-th column is not changed. */
drhf0863fe2005-06-12 21:35:51 +0000102 int chngRowid; /* True if the record number is being changed */
103 Expr *pRowidExpr = 0; /* Expression defining the new record number */
danielk1977742f9472004-06-16 12:02:43 +0000104 int openAll = 0; /* True if all indices need to be opened */
drh85e20962003-04-25 17:52:11 +0000105 AuthContext sContext; /* The authorization context */
danielk1977b3bce662005-01-29 08:32:43 +0000106 NameContext sNC; /* The name-context to resolve expressions in */
danielk1977da184232006-01-05 11:34:32 +0000107 int iDb; /* Database containing the table being updated */
drh53a67772007-02-07 01:06:52 +0000108 int memCnt = 0; /* Memory cell used for counting rows changed */
drh89e0dde2007-12-12 12:25:21 +0000109 int mem1; /* Memory address storing the rowid for next row to update */
drhcce7d172000-05-31 15:34:51 +0000110
drh798da522004-11-04 04:42:28 +0000111#ifndef SQLITE_OMIT_TRIGGER
112 int isView; /* Trying to update a view */
drhdca76842004-12-07 14:06:13 +0000113 int triggers_exist = 0; /* True if any row triggers exist */
drh798da522004-11-04 04:42:28 +0000114#endif
danielk19778f2c54e2008-01-01 19:02:09 +0000115 int iBeginAfterTrigger; /* Address of after trigger program */
116 int iEndAfterTrigger; /* Exit of after trigger program */
117 int iBeginBeforeTrigger; /* Address of before trigger program */
118 int iEndBeforeTrigger; /* Exit of before trigger program */
119 u32 old_col_mask = 0; /* Mask of OLD.* columns in use */
120 u32 new_col_mask = 0; /* Mask of NEW.* columns in use */
danielk1977c3f9bad2002-05-15 08:30:12 +0000121
122 int newIdx = -1; /* index of trigger "new" temp table */
123 int oldIdx = -1; /* index of trigger "old" temp table */
124
drh85e20962003-04-25 17:52:11 +0000125 sContext.pParse = 0;
drh17435752007-08-16 04:30:38 +0000126 db = pParse->db;
127 if( pParse->nErr || db->mallocFailed ){
drh6f7adc82006-01-11 21:41:20 +0000128 goto update_cleanup;
129 }
drh113088e2003-03-20 01:16:58 +0000130 assert( pTabList->nSrc==1 );
drhdaffd0e2001-04-11 14:28:42 +0000131
drhb2fe7d82003-04-20 17:29:23 +0000132 /* Locate the table which we want to update.
drhcce7d172000-05-31 15:34:51 +0000133 */
danielk19774adee202004-05-08 08:23:19 +0000134 pTab = sqlite3SrcListLookup(pParse, pTabList);
drha69d9162003-04-17 22:57:53 +0000135 if( pTab==0 ) goto update_cleanup;
danielk1977da184232006-01-05 11:34:32 +0000136 iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
drhb7f91642004-10-31 02:22:47 +0000137
138 /* Figure out if we have any triggers and if the table being
139 ** updated is a view
140 */
141#ifndef SQLITE_OMIT_TRIGGER
drhdca76842004-12-07 14:06:13 +0000142 triggers_exist = sqlite3TriggersExist(pParse, pTab, TK_UPDATE, pChanges);
drh5cf590c2003-04-24 01:45:04 +0000143 isView = pTab->pSelect!=0;
drhb7f91642004-10-31 02:22:47 +0000144#else
drhdca76842004-12-07 14:06:13 +0000145# define triggers_exist 0
drhb7f91642004-10-31 02:22:47 +0000146# define isView 0
147#endif
148#ifdef SQLITE_OMIT_VIEW
149# undef isView
150# define isView 0
151#endif
152
drhdca76842004-12-07 14:06:13 +0000153 if( sqlite3IsReadOnly(pParse, pTab, triggers_exist) ){
drh5cf590c2003-04-24 01:45:04 +0000154 goto update_cleanup;
drha69d9162003-04-17 22:57:53 +0000155 }
danielk1977b3d24bf2006-06-19 03:05:10 +0000156 if( sqlite3ViewGetColumnNames(pParse, pTab) ){
157 goto update_cleanup;
drh5cf590c2003-04-24 01:45:04 +0000158 }
drh17435752007-08-16 04:30:38 +0000159 aXRef = sqlite3DbMallocRaw(db, sizeof(int) * pTab->nCol );
drhcce7d172000-05-31 15:34:51 +0000160 if( aXRef==0 ) goto update_cleanup;
161 for(i=0; i<pTab->nCol; i++) aXRef[i] = -1;
162
drhb2fe7d82003-04-20 17:29:23 +0000163 /* If there are FOR EACH ROW triggers, allocate cursors for the
164 ** special OLD and NEW tables
165 */
drhdca76842004-12-07 14:06:13 +0000166 if( triggers_exist ){
danielk1977c3f9bad2002-05-15 08:30:12 +0000167 newIdx = pParse->nTab++;
168 oldIdx = pParse->nTab++;
169 }
170
drh53e3fc72002-07-16 17:22:50 +0000171 /* Allocate a cursors for the main database table and for all indices.
172 ** The index cursors might not be used, but if they are used they
173 ** need to occur right after the database cursor. So go ahead and
174 ** allocate enough space, just in case.
175 */
drh6a3ea0e2003-05-02 14:32:12 +0000176 pTabList->a[0].iCursor = iCur = pParse->nTab++;
drh53e3fc72002-07-16 17:22:50 +0000177 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
178 pParse->nTab++;
179 }
180
danielk1977b3bce662005-01-29 08:32:43 +0000181 /* Initialize the name-context */
182 memset(&sNC, 0, sizeof(sNC));
183 sNC.pParse = pParse;
184 sNC.pSrcList = pTabList;
185
drh85e20962003-04-25 17:52:11 +0000186 /* Resolve the column names in all the expressions of the
187 ** of the UPDATE statement. Also find the column index
drhed6c8672003-01-12 18:02:16 +0000188 ** for each column to be updated in the pChanges array. For each
189 ** column to be updated, make sure we have authorization to change
190 ** that column.
drhcce7d172000-05-31 15:34:51 +0000191 */
drhf0863fe2005-06-12 21:35:51 +0000192 chngRowid = 0;
drhcce7d172000-05-31 15:34:51 +0000193 for(i=0; i<pChanges->nExpr; i++){
danielk1977b3bce662005-01-29 08:32:43 +0000194 if( sqlite3ExprResolveNames(&sNC, pChanges->a[i].pExpr) ){
drhcce7d172000-05-31 15:34:51 +0000195 goto update_cleanup;
196 }
197 for(j=0; j<pTab->nCol; j++){
danielk19774adee202004-05-08 08:23:19 +0000198 if( sqlite3StrICmp(pTab->aCol[j].zName, pChanges->a[i].zName)==0 ){
drh9647ff82002-01-14 02:56:24 +0000199 if( j==pTab->iPKey ){
drhf0863fe2005-06-12 21:35:51 +0000200 chngRowid = 1;
201 pRowidExpr = pChanges->a[i].pExpr;
drh4a324312001-12-21 14:30:42 +0000202 }
drhcce7d172000-05-31 15:34:51 +0000203 aXRef[j] = i;
204 break;
205 }
206 }
207 if( j>=pTab->nCol ){
danielk19774adee202004-05-08 08:23:19 +0000208 if( sqlite3IsRowid(pChanges->a[i].zName) ){
drhf0863fe2005-06-12 21:35:51 +0000209 chngRowid = 1;
210 pRowidExpr = pChanges->a[i].pExpr;
drha0217ba2003-06-01 01:10:33 +0000211 }else{
danielk19774adee202004-05-08 08:23:19 +0000212 sqlite3ErrorMsg(pParse, "no such column: %s", pChanges->a[i].zName);
drha0217ba2003-06-01 01:10:33 +0000213 goto update_cleanup;
214 }
drhcce7d172000-05-31 15:34:51 +0000215 }
drhed6c8672003-01-12 18:02:16 +0000216#ifndef SQLITE_OMIT_AUTHORIZATION
drhe5f9c642003-01-13 23:27:31 +0000217 {
218 int rc;
danielk19774adee202004-05-08 08:23:19 +0000219 rc = sqlite3AuthCheck(pParse, SQLITE_UPDATE, pTab->zName,
danielk1977da184232006-01-05 11:34:32 +0000220 pTab->aCol[j].zName, db->aDb[iDb].zName);
drhe5f9c642003-01-13 23:27:31 +0000221 if( rc==SQLITE_DENY ){
222 goto update_cleanup;
223 }else if( rc==SQLITE_IGNORE ){
224 aXRef[j] = -1;
225 }
drhed6c8672003-01-12 18:02:16 +0000226 }
227#endif
drhcce7d172000-05-31 15:34:51 +0000228 }
229
drh5a2c2c22001-11-21 02:21:11 +0000230 /* Allocate memory for the array apIdx[] and fill it with pointers to every
drhcce7d172000-05-31 15:34:51 +0000231 ** index that needs to be updated. Indices only need updating if their
drhc8392582001-12-31 02:48:51 +0000232 ** key includes one of the columns named in pChanges or if the record
233 ** number of the original table entry is changing.
drhcce7d172000-05-31 15:34:51 +0000234 */
drh0ca3e242002-01-29 23:07:02 +0000235 for(nIdx=nIdxTotal=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, nIdxTotal++){
drhf0863fe2005-06-12 21:35:51 +0000236 if( chngRowid ){
drh4a324312001-12-21 14:30:42 +0000237 i = 0;
238 }else {
239 for(i=0; i<pIdx->nColumn; i++){
240 if( aXRef[pIdx->aiColumn[i]]>=0 ) break;
241 }
drhcce7d172000-05-31 15:34:51 +0000242 }
drh967e8b72000-06-21 13:59:10 +0000243 if( i<pIdx->nColumn ) nIdx++;
drhcce7d172000-05-31 15:34:51 +0000244 }
drh0ca3e242002-01-29 23:07:02 +0000245 if( nIdxTotal>0 ){
drh17435752007-08-16 04:30:38 +0000246 apIdx = sqlite3DbMallocRaw(db, sizeof(Index*) * nIdx + nIdxTotal );
drhb0729502001-03-14 12:35:57 +0000247 if( apIdx==0 ) goto update_cleanup;
drh0ca3e242002-01-29 23:07:02 +0000248 aIdxUsed = (char*)&apIdx[nIdx];
drhb0729502001-03-14 12:35:57 +0000249 }
drh0ca3e242002-01-29 23:07:02 +0000250 for(nIdx=j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
drhf0863fe2005-06-12 21:35:51 +0000251 if( chngRowid ){
drh4a324312001-12-21 14:30:42 +0000252 i = 0;
253 }else{
254 for(i=0; i<pIdx->nColumn; i++){
255 if( aXRef[pIdx->aiColumn[i]]>=0 ) break;
256 }
drhcce7d172000-05-31 15:34:51 +0000257 }
drh0ca3e242002-01-29 23:07:02 +0000258 if( i<pIdx->nColumn ){
259 apIdx[nIdx++] = pIdx;
260 aIdxUsed[j] = 1;
261 }else{
262 aIdxUsed[j] = 0;
263 }
drhcce7d172000-05-31 15:34:51 +0000264 }
265
266 /* Begin generating code.
267 */
danielk19774adee202004-05-08 08:23:19 +0000268 v = sqlite3GetVdbe(pParse);
drhcce7d172000-05-31 15:34:51 +0000269 if( v==0 ) goto update_cleanup;
drh4794f732004-11-05 17:17:50 +0000270 if( pParse->nested==0 ) sqlite3VdbeCountChanges(v);
danielk1977da184232006-01-05 11:34:32 +0000271 sqlite3BeginWriteOperation(pParse, 1, iDb);
drh89e0dde2007-12-12 12:25:21 +0000272 mem1 = pParse->nMem++;
drhcce7d172000-05-31 15:34:51 +0000273
drh9c419382006-06-16 21:13:21 +0000274#ifndef SQLITE_OMIT_VIRTUALTABLE
275 /* Virtual tables must be handled separately */
276 if( IsVirtual(pTab) ){
277 updateVirtualTable(pParse, pTabList, pTab, pChanges, pRowidExpr, aXRef,
278 pWhere);
279 pWhere = 0;
280 pTabList = 0;
281 goto update_cleanup;
282 }
283#endif
284
285 /* Resolve the column names in all the expressions in the
286 ** WHERE clause.
287 */
288 if( sqlite3ExprResolveNames(&sNC, pWhere) ){
289 goto update_cleanup;
290 }
291
danielk19774273dea2006-06-17 06:31:18 +0000292 /* Start the view context
293 */
294 if( isView ){
295 sqlite3AuthContextPush(pParse, &sContext, pTab->zName);
296 }
297
danielk19778f2c54e2008-01-01 19:02:09 +0000298 /* Generate the code for triggers.
299 */
300 if( triggers_exist ){
danielk1977e448dc42008-01-02 11:50:51 +0000301 int iGoto;
302
303 /* Create pseudo-tables for NEW and OLD
304 */
305 sqlite3VdbeAddOp(v, OP_OpenPseudo, oldIdx, 0);
306 sqlite3VdbeAddOp(v, OP_SetNumColumns, oldIdx, pTab->nCol);
307 sqlite3VdbeAddOp(v, OP_OpenPseudo, newIdx, 0);
308 sqlite3VdbeAddOp(v, OP_SetNumColumns, newIdx, pTab->nCol);
309
310 iGoto = sqlite3VdbeAddOp(v, OP_Goto, 0, 0);
danielk19778f2c54e2008-01-01 19:02:09 +0000311 addr = sqlite3VdbeMakeLabel(v);
312 iBeginBeforeTrigger = sqlite3VdbeCurrentAddr(v);
313 if( sqlite3CodeRowTrigger(pParse, TK_UPDATE, pChanges, TRIGGER_BEFORE, pTab,
314 newIdx, oldIdx, onError, addr, &old_col_mask, &new_col_mask) ){
315 goto update_cleanup;
316 }
317 iEndBeforeTrigger = sqlite3VdbeAddOp(v, OP_Goto, 0, 0);
318 iBeginAfterTrigger = sqlite3VdbeCurrentAddr(v);
319 if( sqlite3CodeRowTrigger(pParse, TK_UPDATE, pChanges, TRIGGER_AFTER, pTab,
320 newIdx, oldIdx, onError, addr, &old_col_mask, &new_col_mask) ){
321 goto update_cleanup;
322 }
323 iEndAfterTrigger = sqlite3VdbeAddOp(v, OP_Goto, 0, 0);
324 sqlite3VdbeJumpHere(v, iGoto);
325 }
326
drh9d2985c2005-09-08 01:58:42 +0000327 /* If we are trying to update a view, realize that view into
328 ** a ephemeral table.
drh5cf590c2003-04-24 01:45:04 +0000329 */
330 if( isView ){
drh85e20962003-04-25 17:52:11 +0000331 Select *pView;
drh17435752007-08-16 04:30:38 +0000332 pView = sqlite3SelectDup(db, pTab->pSelect);
danielk19778f2c54e2008-01-01 19:02:09 +0000333 sqlite3SelectMask(pParse, pView, old_col_mask|new_col_mask);
drhb9bb7c12006-06-11 23:41:55 +0000334 sqlite3Select(pParse, pView, SRT_EphemTab, iCur, 0, 0, 0, 0);
danielk19774adee202004-05-08 08:23:19 +0000335 sqlite3SelectDelete(pView);
drh5cf590c2003-04-24 01:45:04 +0000336 }
337
drhcce7d172000-05-31 15:34:51 +0000338 /* Begin the database scan
339 */
drhf8db1bc2005-04-22 02:38:37 +0000340 pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, 0);
drhcce7d172000-05-31 15:34:51 +0000341 if( pWInfo==0 ) goto update_cleanup;
342
drh4cbdda92006-06-14 19:00:20 +0000343 /* Remember the rowid of every item to be updated.
drhcce7d172000-05-31 15:34:51 +0000344 */
drh4cbdda92006-06-14 19:00:20 +0000345 sqlite3VdbeAddOp(v, IsVirtual(pTab) ? OP_VRowid : OP_Rowid, iCur, 0);
drha01f79d2005-07-08 13:07:59 +0000346 sqlite3VdbeAddOp(v, OP_FifoWrite, 0, 0);
drhcce7d172000-05-31 15:34:51 +0000347
348 /* End the database scan loop.
349 */
danielk19774adee202004-05-08 08:23:19 +0000350 sqlite3WhereEnd(pWInfo);
drhcce7d172000-05-31 15:34:51 +0000351
drh1bee3d72001-10-15 00:44:35 +0000352 /* Initialize the count of updated rows
353 */
danielk1977c3f9bad2002-05-15 08:30:12 +0000354 if( db->flags & SQLITE_CountRows && !pParse->trigStack ){
drh53a67772007-02-07 01:06:52 +0000355 memCnt = pParse->nMem++;
356 sqlite3VdbeAddOp(v, OP_MemInt, 0, memCnt);
drh1bee3d72001-10-15 00:44:35 +0000357 }
358
danielk1977e448dc42008-01-02 11:50:51 +0000359 if( !isView && !IsVirtual(pTab) ){
360 /*
361 ** Open every index that needs updating. Note that if any
362 ** index could potentially invoke a REPLACE conflict resolution
363 ** action, then we need to open all indices because we might need
364 ** to be deleting some records.
drh70ce3f02003-04-15 19:22:22 +0000365 */
danielk1977e448dc42008-01-02 11:50:51 +0000366 sqlite3OpenTable(pParse, iCur, iDb, pTab, OP_OpenWrite);
367 if( onError==OE_Replace ){
368 openAll = 1;
369 }else{
370 openAll = 0;
371 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
372 if( pIdx->onError==OE_Replace ){
373 openAll = 1;
374 break;
375 }
376 }
drh5cf590c2003-04-24 01:45:04 +0000377 }
danielk1977e448dc42008-01-02 11:50:51 +0000378 for(i=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){
379 if( openAll || aIdxUsed[i] ){
380 KeyInfo *pKey = sqlite3IndexKeyinfo(pParse, pIdx);
381 sqlite3VdbeAddOp(v, OP_Integer, iDb, 0);
382 sqlite3VdbeOp3(v, OP_OpenWrite, iCur+i+1, pIdx->tnum,
383 (char*)pKey, P3_KEYINFO_HANDOFF);
384 assert( pParse->nTab>iCur+i+1 );
385 }
386 }
387
388 }
389
390 /* Jump back to this point if a trigger encounters an IGNORE constraint. */
391 if( triggers_exist ){
392 sqlite3VdbeResolveLabel(v, addr);
393 }
394
395 /* Top of the update loop */
396 addr = sqlite3VdbeAddOp(v, OP_FifoRead, 0, 0);
397 sqlite3VdbeAddOp(v, OP_StackDepth, -1, 0);
398 sqlite3VdbeAddOp(v, OP_MemStore, mem1, 0);
399
400 if( triggers_exist ){
401 /* Make cursor iCur point to the record that is being updated.
402 */
danielk1977967573d2007-12-12 16:06:23 +0000403 sqlite3VdbeAddOp(v, OP_NotExists, iCur, addr);
danielk1977c3f9bad2002-05-15 08:30:12 +0000404
drh70ce3f02003-04-15 19:22:22 +0000405 /* Generate the OLD table
406 */
drhf0863fe2005-06-12 21:35:51 +0000407 sqlite3VdbeAddOp(v, OP_Rowid, iCur, 0);
danielk19778f2c54e2008-01-01 19:02:09 +0000408 if( !old_col_mask ){
409 sqlite3VdbeAddOp(v, OP_Null, 0, 0);
410 }else{
411 sqlite3VdbeAddOp(v, OP_RowData, iCur, 0);
412 }
drhf0863fe2005-06-12 21:35:51 +0000413 sqlite3VdbeAddOp(v, OP_Insert, oldIdx, 0);
danielk1977c3f9bad2002-05-15 08:30:12 +0000414
drh70ce3f02003-04-15 19:22:22 +0000415 /* Generate the NEW table
416 */
drhf0863fe2005-06-12 21:35:51 +0000417 if( chngRowid ){
418 sqlite3ExprCodeAndCache(pParse, pRowidExpr);
drh70ce3f02003-04-15 19:22:22 +0000419 }else{
drhf0863fe2005-06-12 21:35:51 +0000420 sqlite3VdbeAddOp(v, OP_Rowid, iCur, 0);
drh70ce3f02003-04-15 19:22:22 +0000421 }
drh25303782004-12-07 15:41:48 +0000422 for(i=0; i<pTab->nCol; i++){
drh70ce3f02003-04-15 19:22:22 +0000423 if( i==pTab->iPKey ){
drhf0863fe2005-06-12 21:35:51 +0000424 sqlite3VdbeAddOp(v, OP_Null, 0, 0);
drh70ce3f02003-04-15 19:22:22 +0000425 continue;
426 }
427 j = aXRef[i];
danielk19778f2c54e2008-01-01 19:02:09 +0000428 if( new_col_mask&((u32)1<<i) || new_col_mask==0xffffffff ){
429 if( j<0 ){
430 sqlite3VdbeAddOp(v, OP_Column, iCur, i);
431 sqlite3ColumnDefault(v, pTab, i);
432 }else{
433 sqlite3ExprCodeAndCache(pParse, pChanges->a[j].pExpr);
434 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000435 }else{
danielk19778f2c54e2008-01-01 19:02:09 +0000436 sqlite3VdbeAddOp(v, OP_Null, 0, 0);
danielk1977c3f9bad2002-05-15 08:30:12 +0000437 }
438 }
danielk19774adee202004-05-08 08:23:19 +0000439 sqlite3VdbeAddOp(v, OP_MakeRecord, pTab->nCol, 0);
danielk1977a37cdde2004-05-16 11:15:36 +0000440 if( !isView ){
441 sqlite3TableAffinityStr(v, pTab);
442 }
443 if( pParse->nErr ) goto update_cleanup;
drhf0863fe2005-06-12 21:35:51 +0000444 sqlite3VdbeAddOp(v, OP_Insert, newIdx, 0);
danielk1977c3f9bad2002-05-15 08:30:12 +0000445
danielk19778f2c54e2008-01-01 19:02:09 +0000446 sqlite3VdbeAddOp(v, OP_Goto, 0, iBeginBeforeTrigger);
447 sqlite3VdbeJumpHere(v, iEndBeforeTrigger);
448
drh95c07752007-11-11 18:36:34 +0000449 if( !isView ){
450 sqlite3VdbeAddOp(v, OP_MemLoad, mem1, 0);
drh95c07752007-11-11 18:36:34 +0000451 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000452 }
453
drh4cbdda92006-06-14 19:00:20 +0000454 if( !isView && !IsVirtual(pTab) ){
drhcce7d172000-05-31 15:34:51 +0000455
drh5cf590c2003-04-24 01:45:04 +0000456 /* Loop over every record that needs updating. We have to load
457 ** the old data for each record to be updated because some columns
458 ** might not change and we will need to copy the old value.
drh4cbdda92006-06-14 19:00:20 +0000459 ** Also, the old data is needed to delete the old index entries.
drh5cf590c2003-04-24 01:45:04 +0000460 ** So make the cursor point at the old record.
461 */
danielk19774adee202004-05-08 08:23:19 +0000462 sqlite3VdbeAddOp(v, OP_NotExists, iCur, addr);
drh89e0dde2007-12-12 12:25:21 +0000463 sqlite3VdbeAddOp(v, OP_MemLoad, mem1, 0);
drh5cf590c2003-04-24 01:45:04 +0000464
465 /* If the record number will change, push the record number as it
466 ** will be after the update. (The old record number is currently
467 ** on top of the stack.)
468 */
drhf0863fe2005-06-12 21:35:51 +0000469 if( chngRowid ){
470 sqlite3ExprCode(pParse, pRowidExpr);
danielk19774adee202004-05-08 08:23:19 +0000471 sqlite3VdbeAddOp(v, OP_MustBeInt, 0, 0);
drhcce7d172000-05-31 15:34:51 +0000472 }
drh5cf590c2003-04-24 01:45:04 +0000473
474 /* Compute new data for this record.
475 */
476 for(i=0; i<pTab->nCol; i++){
477 if( i==pTab->iPKey ){
drhf0863fe2005-06-12 21:35:51 +0000478 sqlite3VdbeAddOp(v, OP_Null, 0, 0);
drh5cf590c2003-04-24 01:45:04 +0000479 continue;
480 }
481 j = aXRef[i];
482 if( j<0 ){
danielk19774adee202004-05-08 08:23:19 +0000483 sqlite3VdbeAddOp(v, OP_Column, iCur, i);
danielk1977aee18ef2005-03-09 12:26:50 +0000484 sqlite3ColumnDefault(v, pTab, i);
drh5cf590c2003-04-24 01:45:04 +0000485 }else{
danielk19774adee202004-05-08 08:23:19 +0000486 sqlite3ExprCode(pParse, pChanges->a[j].pExpr);
drh5cf590c2003-04-24 01:45:04 +0000487 }
488 }
489
490 /* Do constraint checks
491 */
drhf0863fe2005-06-12 21:35:51 +0000492 sqlite3GenerateConstraintChecks(pParse, pTab, iCur, aIdxUsed, chngRowid, 1,
drh5cf590c2003-04-24 01:45:04 +0000493 onError, addr);
494
495 /* Delete the old indices for the current record.
496 */
drh74161702006-02-24 02:53:49 +0000497 sqlite3GenerateRowIndexDelete(v, pTab, iCur, aIdxUsed);
drh5cf590c2003-04-24 01:45:04 +0000498
499 /* If changing the record number, delete the old record.
500 */
drhf0863fe2005-06-12 21:35:51 +0000501 if( chngRowid ){
danielk19774adee202004-05-08 08:23:19 +0000502 sqlite3VdbeAddOp(v, OP_Delete, iCur, 0);
drh5cf590c2003-04-24 01:45:04 +0000503 }
504
505 /* Create the new index entries and the new record.
506 */
drhe4d90812007-03-29 05:51:49 +0000507 sqlite3CompleteInsertion(pParse, pTab, iCur, aIdxUsed, chngRowid, 1, -1, 0);
drhcce7d172000-05-31 15:34:51 +0000508 }
509
drh0ca3e242002-01-29 23:07:02 +0000510 /* Increment the row counter
drh1bee3d72001-10-15 00:44:35 +0000511 */
danielk1977c3f9bad2002-05-15 08:30:12 +0000512 if( db->flags & SQLITE_CountRows && !pParse->trigStack){
drh53a67772007-02-07 01:06:52 +0000513 sqlite3VdbeAddOp(v, OP_MemIncr, 1, memCnt);
drh1bee3d72001-10-15 00:44:35 +0000514 }
515
drh5cf590c2003-04-24 01:45:04 +0000516 /* If there are triggers, close all the cursors after each iteration
517 ** through the loop. The fire the after triggers.
518 */
drhdca76842004-12-07 14:06:13 +0000519 if( triggers_exist ){
danielk19778f2c54e2008-01-01 19:02:09 +0000520 sqlite3VdbeAddOp(v, OP_Goto, 0, iBeginAfterTrigger);
521 sqlite3VdbeJumpHere(v, iEndAfterTrigger);
danielk1977c3f9bad2002-05-15 08:30:12 +0000522 }
523
drhcce7d172000-05-31 15:34:51 +0000524 /* Repeat the above with the next record to be updated, until
525 ** all record selected by the WHERE clause have been updated.
526 */
danielk19774adee202004-05-08 08:23:19 +0000527 sqlite3VdbeAddOp(v, OP_Goto, 0, addr);
drhd654be82005-09-20 17:42:23 +0000528 sqlite3VdbeJumpHere(v, addr);
danielk1977c3f9bad2002-05-15 08:30:12 +0000529
danielk1977e448dc42008-01-02 11:50:51 +0000530 /* Close all tables */
531 for(i=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){
532 if( openAll || aIdxUsed[i] ){
533 sqlite3VdbeAddOp(v, OP_Close, iCur+i+1, 0);
danielk1977c3f9bad2002-05-15 08:30:12 +0000534 }
danielk1977e448dc42008-01-02 11:50:51 +0000535 }
536 sqlite3VdbeAddOp(v, OP_Close, iCur, 0);
537 if( triggers_exist ){
danielk19774adee202004-05-08 08:23:19 +0000538 sqlite3VdbeAddOp(v, OP_Close, newIdx, 0);
539 sqlite3VdbeAddOp(v, OP_Close, oldIdx, 0);
danielk1977c3f9bad2002-05-15 08:30:12 +0000540 }
541
drh1bee3d72001-10-15 00:44:35 +0000542 /*
danielk1977e7de6f22004-11-05 06:02:06 +0000543 ** Return the number of rows that were changed. If this routine is
544 ** generating code because of a call to sqlite3NestedParse(), do not
545 ** invoke the callback function.
drh1bee3d72001-10-15 00:44:35 +0000546 */
danielk1977e7de6f22004-11-05 06:02:06 +0000547 if( db->flags & SQLITE_CountRows && !pParse->trigStack && pParse->nested==0 ){
drhd4e70eb2008-01-02 00:34:36 +0000548 sqlite3VdbeAddOp(v, OP_ResultRow, memCnt, 1);
danielk197722322fd2004-05-25 23:35:17 +0000549 sqlite3VdbeSetNumCols(v, 1);
danielk1977955de522006-02-10 02:27:42 +0000550 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "rows updated", P3_STATIC);
drh1bee3d72001-10-15 00:44:35 +0000551 }
552
drhcce7d172000-05-31 15:34:51 +0000553update_cleanup:
danielk19774adee202004-05-08 08:23:19 +0000554 sqlite3AuthContextPop(&sContext);
drh17435752007-08-16 04:30:38 +0000555 sqlite3_free(apIdx);
556 sqlite3_free(aXRef);
danielk19774adee202004-05-08 08:23:19 +0000557 sqlite3SrcListDelete(pTabList);
558 sqlite3ExprListDelete(pChanges);
559 sqlite3ExprDelete(pWhere);
drhcce7d172000-05-31 15:34:51 +0000560 return;
561}
drh9c419382006-06-16 21:13:21 +0000562
563#ifndef SQLITE_OMIT_VIRTUALTABLE
564/*
565** Generate code for an UPDATE of a virtual table.
566**
567** The strategy is that we create an ephemerial table that contains
568** for each row to be changed:
569**
570** (A) The original rowid of that row.
571** (B) The revised rowid for the row. (note1)
572** (C) The content of every column in the row.
573**
574** Then we loop over this ephemeral table and for each row in
575** the ephermeral table call VUpdate.
576**
577** When finished, drop the ephemeral table.
578**
579** (note1) Actually, if we know in advance that (A) is always the same
580** as (B) we only store (A), then duplicate (A) when pulling
581** it out of the ephemeral table before calling VUpdate.
582*/
583static void updateVirtualTable(
584 Parse *pParse, /* The parsing context */
585 SrcList *pSrc, /* The virtual table to be modified */
586 Table *pTab, /* The virtual table */
587 ExprList *pChanges, /* The columns to change in the UPDATE statement */
588 Expr *pRowid, /* Expression used to recompute the rowid */
589 int *aXRef, /* Mapping from columns of pTab to entries in pChanges */
590 Expr *pWhere /* WHERE clause of the UPDATE statement */
591){
592 Vdbe *v = pParse->pVdbe; /* Virtual machine under construction */
593 ExprList *pEList = 0; /* The result set of the SELECT statement */
594 Select *pSelect = 0; /* The SELECT statement */
595 Expr *pExpr; /* Temporary expression */
596 int ephemTab; /* Table holding the result of the SELECT */
597 int i; /* Loop counter */
598 int addr; /* Address of top of loop */
drh17435752007-08-16 04:30:38 +0000599 sqlite3 *db = pParse->db; /* Database connection */
drh9c419382006-06-16 21:13:21 +0000600
601 /* Construct the SELECT statement that will find the new values for
602 ** all updated rows.
603 */
drh17435752007-08-16 04:30:38 +0000604 pEList = sqlite3ExprListAppend(pParse, 0,
605 sqlite3CreateIdExpr(pParse, "_rowid_"), 0);
drh9c419382006-06-16 21:13:21 +0000606 if( pRowid ){
drh17435752007-08-16 04:30:38 +0000607 pEList = sqlite3ExprListAppend(pParse, pEList,
608 sqlite3ExprDup(db, pRowid), 0);
drh9c419382006-06-16 21:13:21 +0000609 }
danielk197765fd59f2006-06-24 11:51:33 +0000610 assert( pTab->iPKey<0 );
drh9c419382006-06-16 21:13:21 +0000611 for(i=0; i<pTab->nCol; i++){
danielk197765fd59f2006-06-24 11:51:33 +0000612 if( aXRef[i]>=0 ){
drh17435752007-08-16 04:30:38 +0000613 pExpr = sqlite3ExprDup(db, pChanges->a[aXRef[i]].pExpr);
drh9c419382006-06-16 21:13:21 +0000614 }else{
drh17435752007-08-16 04:30:38 +0000615 pExpr = sqlite3CreateIdExpr(pParse, pTab->aCol[i].zName);
drh9c419382006-06-16 21:13:21 +0000616 }
drh17435752007-08-16 04:30:38 +0000617 pEList = sqlite3ExprListAppend(pParse, pEList, pExpr, 0);
drh9c419382006-06-16 21:13:21 +0000618 }
drh17435752007-08-16 04:30:38 +0000619 pSelect = sqlite3SelectNew(pParse, pEList, pSrc, pWhere, 0, 0, 0, 0, 0, 0);
drh9c419382006-06-16 21:13:21 +0000620
621 /* Create the ephemeral table into which the update results will
622 ** be stored.
623 */
624 assert( v );
625 ephemTab = pParse->nTab++;
626 sqlite3VdbeAddOp(v, OP_OpenEphemeral, ephemTab, pTab->nCol+1+(pRowid!=0));
627
628 /* fill the ephemeral table
629 */
630 sqlite3Select(pParse, pSelect, SRT_Table, ephemTab, 0, 0, 0, 0);
631
632 /*
633 ** Generate code to scan the ephemeral table and call VDelete and
634 ** VInsert
635 */
danielk197707fd0592007-02-21 17:04:04 +0000636 sqlite3VdbeAddOp(v, OP_Rewind, ephemTab, 0);
drh9c419382006-06-16 21:13:21 +0000637 addr = sqlite3VdbeCurrentAddr(v);
638 sqlite3VdbeAddOp(v, OP_Column, ephemTab, 0);
639 if( pRowid ){
640 sqlite3VdbeAddOp(v, OP_Column, ephemTab, 1);
641 }else{
danielk19772867fef2006-06-17 03:27:21 +0000642 sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
drh9c419382006-06-16 21:13:21 +0000643 }
644 for(i=0; i<pTab->nCol; i++){
645 sqlite3VdbeAddOp(v, OP_Column, ephemTab, i+1+(pRowid!=0));
646 }
danielk1977c69cdfd2006-06-17 09:39:55 +0000647 pParse->pVirtualLock = pTab;
drh9c419382006-06-16 21:13:21 +0000648 sqlite3VdbeOp3(v, OP_VUpdate, 0, pTab->nCol+2,
649 (const char*)pTab->pVtab, P3_VTAB);
650 sqlite3VdbeAddOp(v, OP_Next, ephemTab, addr);
danielk197707fd0592007-02-21 17:04:04 +0000651 sqlite3VdbeJumpHere(v, addr-1);
drh9c419382006-06-16 21:13:21 +0000652 sqlite3VdbeAddOp(v, OP_Close, ephemTab, 0);
653
654 /* Cleanup */
655 sqlite3SelectDelete(pSelect);
656}
657#endif /* SQLITE_OMIT_VIRTUALTABLE */