blob: d44aace9a797deb1f3c53b847522f76478f18948 [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**
drheeb844a2009-08-08 18:01:07 +000015** $Id: update.c,v 1.207 2009/08/08 18:01:08 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.
danielk1977c7538b52009-07-27 10:05:04 +000056**
57** If parameter iReg is not negative, code an OP_RealAffinity instruction
58** on register iReg. This is used when an equivalent integer value is
59** stored in place of an 8-byte floating point value in order to save
60** space.
danielk1977aee18ef2005-03-09 12:26:50 +000061*/
danielk1977c7538b52009-07-27 10:05:04 +000062void sqlite3ColumnDefault(Vdbe *v, Table *pTab, int i, int iReg){
drhe289d602009-05-05 15:46:09 +000063 assert( pTab!=0 );
64 if( !pTab->pSelect ){
danielk1977aee18ef2005-03-09 12:26:50 +000065 sqlite3_value *pValue;
danielk197714db2662006-01-09 16:12:04 +000066 u8 enc = ENC(sqlite3VdbeDb(v));
danielk1977aee18ef2005-03-09 12:26:50 +000067 Column *pCol = &pTab->aCol[i];
drhd4e70eb2008-01-02 00:34:36 +000068 VdbeComment((v, "%s.%s", pTab->zName, pCol->zName));
danielk1977c9cf6e32007-06-25 16:29:33 +000069 assert( i<pTab->nCol );
drhd4e70eb2008-01-02 00:34:36 +000070 sqlite3ValueFromExpr(sqlite3VdbeDb(v), pCol->pDflt, enc,
71 pCol->affinity, &pValue);
drh29dda4a2005-07-21 18:23:20 +000072 if( pValue ){
drh66a51672008-01-03 00:01:23 +000073 sqlite3VdbeChangeP4(v, -1, (const char *)pValue, P4_MEM);
drh29dda4a2005-07-21 18:23:20 +000074 }
danielk1977c7538b52009-07-27 10:05:04 +000075#ifndef SQLITE_OMIT_FLOATING_POINT
76 if( iReg>=0 && pTab->aCol[i].affinity==SQLITE_AFF_REAL ){
77 sqlite3VdbeAddOp1(v, OP_RealAffinity, iReg);
78 }
79#endif
danielk1977aee18ef2005-03-09 12:26:50 +000080 }
81}
82
83/*
drhcce7d172000-05-31 15:34:51 +000084** Process an UPDATE statement.
drhb2fe7d82003-04-20 17:29:23 +000085**
86** UPDATE OR IGNORE table_wxyz SET a=b, c=d WHERE e<5 AND f NOT NULL;
87** \_______/ \________/ \______/ \________________/
88* onError pTabList pChanges pWhere
drhcce7d172000-05-31 15:34:51 +000089*/
danielk19774adee202004-05-08 08:23:19 +000090void sqlite3Update(
drhcce7d172000-05-31 15:34:51 +000091 Parse *pParse, /* The parser context */
drh113088e2003-03-20 01:16:58 +000092 SrcList *pTabList, /* The table in which we should change things */
drhcce7d172000-05-31 15:34:51 +000093 ExprList *pChanges, /* Things to be changed */
drh9cfcf5d2002-01-29 18:41:24 +000094 Expr *pWhere, /* The WHERE clause. May be null */
95 int onError /* How to handle constraint errors */
drhcce7d172000-05-31 15:34:51 +000096){
97 int i, j; /* Loop counters */
98 Table *pTab; /* The table to be updated */
danielk1977742f9472004-06-16 12:02:43 +000099 int addr = 0; /* VDBE instruction address of the start of the loop */
drhcce7d172000-05-31 15:34:51 +0000100 WhereInfo *pWInfo; /* Information about the WHERE clause */
101 Vdbe *v; /* The virtual database engine */
102 Index *pIdx; /* For looping over indices */
103 int nIdx; /* Number of indices that need updating */
drh6a3ea0e2003-05-02 14:32:12 +0000104 int iCur; /* VDBE Cursor number of pTab */
drh9bb575f2004-09-06 17:24:11 +0000105 sqlite3 *db; /* The database structure */
drhaa9b8962008-01-08 02:57:55 +0000106 int *aRegIdx = 0; /* One register assigned to each index to be updated */
drhcce7d172000-05-31 15:34:51 +0000107 int *aXRef = 0; /* aXRef[i] is the index in pChanges->a[] of the
drh967e8b72000-06-21 13:59:10 +0000108 ** an expression for the i-th column of the table.
109 ** aXRef[i]==-1 if the i-th column is not changed. */
drhf0863fe2005-06-12 21:35:51 +0000110 int chngRowid; /* True if the record number is being changed */
111 Expr *pRowidExpr = 0; /* Expression defining the new record number */
danielk1977742f9472004-06-16 12:02:43 +0000112 int openAll = 0; /* True if all indices need to be opened */
drh85e20962003-04-25 17:52:11 +0000113 AuthContext sContext; /* The authorization context */
danielk1977b3bce662005-01-29 08:32:43 +0000114 NameContext sNC; /* The name-context to resolve expressions in */
danielk1977da184232006-01-05 11:34:32 +0000115 int iDb; /* Database containing the table being updated */
drha05a7222008-01-19 03:35:58 +0000116 int j1; /* Addresses of jump instructions */
drh08c88eb2008-04-10 13:33:18 +0000117 int okOnePass; /* True for one-pass algorithm without the FIFO */
drhcce7d172000-05-31 15:34:51 +0000118
drh798da522004-11-04 04:42:28 +0000119#ifndef SQLITE_OMIT_TRIGGER
120 int isView; /* Trying to update a view */
danielk19772f886d12009-02-28 10:47:41 +0000121 Trigger *pTrigger; /* List of triggers on pTab, if required */
drh798da522004-11-04 04:42:28 +0000122#endif
danielk19778f2c54e2008-01-01 19:02:09 +0000123 u32 old_col_mask = 0; /* Mask of OLD.* columns in use */
124 u32 new_col_mask = 0; /* Mask of NEW.* columns in use */
danielk1977c3f9bad2002-05-15 08:30:12 +0000125
drh04adf412008-01-08 18:57:50 +0000126 /* Register Allocations */
127 int regRowCount = 0; /* A count of rows changed */
128 int regOldRowid; /* The old rowid */
129 int regNewRowid; /* The new rowid */
dan165921a2009-08-28 18:53:45 +0000130 int regNew;
131 int regOld;
drh4f21c4a2008-12-10 22:15:00 +0000132 int regRowSet = 0; /* Rowset of rows to be updated */
dan76d462e2009-08-30 11:42:51 +0000133 int regRec; /* Register used for new table record to insert */
drh04adf412008-01-08 18:57:50 +0000134
drheeb844a2009-08-08 18:01:07 +0000135 memset(&sContext, 0, sizeof(sContext));
drh17435752007-08-16 04:30:38 +0000136 db = pParse->db;
137 if( pParse->nErr || db->mallocFailed ){
drh6f7adc82006-01-11 21:41:20 +0000138 goto update_cleanup;
139 }
drh113088e2003-03-20 01:16:58 +0000140 assert( pTabList->nSrc==1 );
drhdaffd0e2001-04-11 14:28:42 +0000141
drhb2fe7d82003-04-20 17:29:23 +0000142 /* Locate the table which we want to update.
drhcce7d172000-05-31 15:34:51 +0000143 */
danielk19774adee202004-05-08 08:23:19 +0000144 pTab = sqlite3SrcListLookup(pParse, pTabList);
drha69d9162003-04-17 22:57:53 +0000145 if( pTab==0 ) goto update_cleanup;
danielk1977da184232006-01-05 11:34:32 +0000146 iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
drhb7f91642004-10-31 02:22:47 +0000147
148 /* Figure out if we have any triggers and if the table being
dan76d462e2009-08-30 11:42:51 +0000149 ** updated is a view.
drhb7f91642004-10-31 02:22:47 +0000150 */
151#ifndef SQLITE_OMIT_TRIGGER
danielk19772f886d12009-02-28 10:47:41 +0000152 pTrigger = sqlite3TriggersExist(pParse, pTab, TK_UPDATE, pChanges, 0);
drh5cf590c2003-04-24 01:45:04 +0000153 isView = pTab->pSelect!=0;
drhb7f91642004-10-31 02:22:47 +0000154#else
danielk19772f886d12009-02-28 10:47:41 +0000155# define pTrigger 0
drhb7f91642004-10-31 02:22:47 +0000156# define isView 0
157#endif
158#ifdef SQLITE_OMIT_VIEW
159# undef isView
160# define isView 0
161#endif
162
danielk1977595a5232009-07-24 17:58:53 +0000163 if( sqlite3ViewGetColumnNames(pParse, pTab) ){
drh5cf590c2003-04-24 01:45:04 +0000164 goto update_cleanup;
drha69d9162003-04-17 22:57:53 +0000165 }
danielk1977595a5232009-07-24 17:58:53 +0000166 if( sqlite3IsReadOnly(pParse, pTab, (pTrigger?1:0)) ){
danielk1977b3d24bf2006-06-19 03:05:10 +0000167 goto update_cleanup;
drh5cf590c2003-04-24 01:45:04 +0000168 }
drh17435752007-08-16 04:30:38 +0000169 aXRef = sqlite3DbMallocRaw(db, sizeof(int) * pTab->nCol );
drhcce7d172000-05-31 15:34:51 +0000170 if( aXRef==0 ) goto update_cleanup;
171 for(i=0; i<pTab->nCol; i++) aXRef[i] = -1;
172
drh53e3fc72002-07-16 17:22:50 +0000173 /* Allocate a cursors for the main database table and for all indices.
174 ** The index cursors might not be used, but if they are used they
175 ** need to occur right after the database cursor. So go ahead and
176 ** allocate enough space, just in case.
177 */
drh6a3ea0e2003-05-02 14:32:12 +0000178 pTabList->a[0].iCursor = iCur = pParse->nTab++;
drh53e3fc72002-07-16 17:22:50 +0000179 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
180 pParse->nTab++;
181 }
182
danielk1977b3bce662005-01-29 08:32:43 +0000183 /* Initialize the name-context */
184 memset(&sNC, 0, sizeof(sNC));
185 sNC.pParse = pParse;
186 sNC.pSrcList = pTabList;
187
drh85e20962003-04-25 17:52:11 +0000188 /* Resolve the column names in all the expressions of the
189 ** of the UPDATE statement. Also find the column index
drhed6c8672003-01-12 18:02:16 +0000190 ** for each column to be updated in the pChanges array. For each
191 ** column to be updated, make sure we have authorization to change
192 ** that column.
drhcce7d172000-05-31 15:34:51 +0000193 */
drhf0863fe2005-06-12 21:35:51 +0000194 chngRowid = 0;
drhcce7d172000-05-31 15:34:51 +0000195 for(i=0; i<pChanges->nExpr; i++){
drh7d10d5a2008-08-20 16:35:10 +0000196 if( sqlite3ResolveExprNames(&sNC, pChanges->a[i].pExpr) ){
drhcce7d172000-05-31 15:34:51 +0000197 goto update_cleanup;
198 }
199 for(j=0; j<pTab->nCol; j++){
danielk19774adee202004-05-08 08:23:19 +0000200 if( sqlite3StrICmp(pTab->aCol[j].zName, pChanges->a[i].zName)==0 ){
drh9647ff82002-01-14 02:56:24 +0000201 if( j==pTab->iPKey ){
drhf0863fe2005-06-12 21:35:51 +0000202 chngRowid = 1;
203 pRowidExpr = pChanges->a[i].pExpr;
drh4a324312001-12-21 14:30:42 +0000204 }
drhcce7d172000-05-31 15:34:51 +0000205 aXRef[j] = i;
206 break;
207 }
208 }
209 if( j>=pTab->nCol ){
danielk19774adee202004-05-08 08:23:19 +0000210 if( sqlite3IsRowid(pChanges->a[i].zName) ){
drhf0863fe2005-06-12 21:35:51 +0000211 chngRowid = 1;
212 pRowidExpr = pChanges->a[i].pExpr;
drha0217ba2003-06-01 01:10:33 +0000213 }else{
danielk19774adee202004-05-08 08:23:19 +0000214 sqlite3ErrorMsg(pParse, "no such column: %s", pChanges->a[i].zName);
drha0217ba2003-06-01 01:10:33 +0000215 goto update_cleanup;
216 }
drhcce7d172000-05-31 15:34:51 +0000217 }
drhed6c8672003-01-12 18:02:16 +0000218#ifndef SQLITE_OMIT_AUTHORIZATION
drhe5f9c642003-01-13 23:27:31 +0000219 {
220 int rc;
danielk19774adee202004-05-08 08:23:19 +0000221 rc = sqlite3AuthCheck(pParse, SQLITE_UPDATE, pTab->zName,
danielk1977da184232006-01-05 11:34:32 +0000222 pTab->aCol[j].zName, db->aDb[iDb].zName);
drhe5f9c642003-01-13 23:27:31 +0000223 if( rc==SQLITE_DENY ){
224 goto update_cleanup;
225 }else if( rc==SQLITE_IGNORE ){
226 aXRef[j] = -1;
227 }
drhed6c8672003-01-12 18:02:16 +0000228 }
229#endif
drhcce7d172000-05-31 15:34:51 +0000230 }
231
drhaa9b8962008-01-08 02:57:55 +0000232 /* Allocate memory for the array aRegIdx[]. There is one entry in the
233 ** array for each index associated with table being updated. Fill in
234 ** the value with a register number for indices that are to be used
235 ** and with zero for unused indices.
drhcce7d172000-05-31 15:34:51 +0000236 */
drhaa9b8962008-01-08 02:57:55 +0000237 for(nIdx=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, nIdx++){}
238 if( nIdx>0 ){
239 aRegIdx = sqlite3DbMallocRaw(db, sizeof(Index*) * nIdx );
240 if( aRegIdx==0 ) goto update_cleanup;
241 }
242 for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
243 int reg;
drhf0863fe2005-06-12 21:35:51 +0000244 if( chngRowid ){
drhaa9b8962008-01-08 02:57:55 +0000245 reg = ++pParse->nMem;
246 }else{
247 reg = 0;
drh4a324312001-12-21 14:30:42 +0000248 for(i=0; i<pIdx->nColumn; i++){
drhaa9b8962008-01-08 02:57:55 +0000249 if( aXRef[pIdx->aiColumn[i]]>=0 ){
250 reg = ++pParse->nMem;
251 break;
252 }
drh4a324312001-12-21 14:30:42 +0000253 }
drhcce7d172000-05-31 15:34:51 +0000254 }
drhaa9b8962008-01-08 02:57:55 +0000255 aRegIdx[j] = reg;
drhcce7d172000-05-31 15:34:51 +0000256 }
257
dan165921a2009-08-28 18:53:45 +0000258 /* Begin generating code. */
danielk19774adee202004-05-08 08:23:19 +0000259 v = sqlite3GetVdbe(pParse);
drhcce7d172000-05-31 15:34:51 +0000260 if( v==0 ) goto update_cleanup;
drh4794f732004-11-05 17:17:50 +0000261 if( pParse->nested==0 ) sqlite3VdbeCountChanges(v);
danielk1977da184232006-01-05 11:34:32 +0000262 sqlite3BeginWriteOperation(pParse, 1, iDb);
drhcce7d172000-05-31 15:34:51 +0000263
drh9c419382006-06-16 21:13:21 +0000264#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
dan165921a2009-08-28 18:53:45 +0000275 /* Allocate required registers. */
276 regOldRowid = regNewRowid = ++pParse->nMem;
277 if( pTrigger ){
278 regOld = pParse->nMem + 1;
279 pParse->nMem += pTab->nCol;
280 }
281 if( chngRowid || pTrigger ){
282 regNewRowid = ++pParse->nMem;
283 }
284 regNew = pParse->nMem + 1;
285 pParse->nMem += pTab->nCol;
dan76d462e2009-08-30 11:42:51 +0000286 regRec = ++pParse->nMem;
dan165921a2009-08-28 18:53:45 +0000287
288 /* Start the view context. */
danielk19774273dea2006-06-17 06:31:18 +0000289 if( isView ){
290 sqlite3AuthContextPush(pParse, &sContext, pTab->zName);
291 }
292
dan165921a2009-08-28 18:53:45 +0000293 /* If there are any triggers, set old_col_mask and new_col_mask. */
294 sqlite3TriggerUses(pParse,
295 pTrigger, TK_UPDATE, pChanges, pTab, onError, &old_col_mask, &new_col_mask
296 );
danielk19778f2c54e2008-01-01 19:02:09 +0000297
drh9d2985c2005-09-08 01:58:42 +0000298 /* If we are trying to update a view, realize that view into
299 ** a ephemeral table.
drh5cf590c2003-04-24 01:45:04 +0000300 */
shanefa4e62f2008-09-01 21:59:42 +0000301#if !defined(SQLITE_OMIT_VIEW) && !defined(SQLITE_OMIT_TRIGGER)
drh5cf590c2003-04-24 01:45:04 +0000302 if( isView ){
drh2a5d8252008-08-22 12:30:52 +0000303 sqlite3MaterializeView(pParse, pTab, pWhere, iCur);
drh0f35a6b2008-02-12 16:52:14 +0000304 }
shanefa4e62f2008-09-01 21:59:42 +0000305#endif
drh1013c932008-01-06 00:25:21 +0000306
drh0f35a6b2008-02-12 16:52:14 +0000307 /* Resolve the column names in all the expressions in the
308 ** WHERE clause.
309 */
drh7d10d5a2008-08-20 16:35:10 +0000310 if( sqlite3ResolveExprNames(&sNC, pWhere) ){
drh0f35a6b2008-02-12 16:52:14 +0000311 goto update_cleanup;
drh5cf590c2003-04-24 01:45:04 +0000312 }
313
drhcce7d172000-05-31 15:34:51 +0000314 /* Begin the database scan
315 */
drh08c88eb2008-04-10 13:33:18 +0000316 sqlite3VdbeAddOp2(v, OP_Null, 0, regOldRowid);
drh336a5302009-04-24 15:46:21 +0000317 pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere,0, WHERE_ONEPASS_DESIRED);
drhcce7d172000-05-31 15:34:51 +0000318 if( pWInfo==0 ) goto update_cleanup;
drh08c88eb2008-04-10 13:33:18 +0000319 okOnePass = pWInfo->okOnePass;
drhcce7d172000-05-31 15:34:51 +0000320
drh4cbdda92006-06-14 19:00:20 +0000321 /* Remember the rowid of every item to be updated.
drhcce7d172000-05-31 15:34:51 +0000322 */
drh044925b2009-04-22 17:15:02 +0000323 sqlite3VdbeAddOp2(v, OP_Rowid, iCur, regOldRowid);
drh3d4501e2008-12-04 20:40:10 +0000324 if( !okOnePass ){
325 regRowSet = ++pParse->nMem;
326 sqlite3VdbeAddOp2(v, OP_RowSetAdd, regRowSet, regOldRowid);
327 }
drhcce7d172000-05-31 15:34:51 +0000328
329 /* End the database scan loop.
330 */
danielk19774adee202004-05-08 08:23:19 +0000331 sqlite3WhereEnd(pWInfo);
drhcce7d172000-05-31 15:34:51 +0000332
drh1bee3d72001-10-15 00:44:35 +0000333 /* Initialize the count of updated rows
334 */
dan165921a2009-08-28 18:53:45 +0000335 if( (db->flags & SQLITE_CountRows) && !pParse->pTriggerTab ){
drh04adf412008-01-08 18:57:50 +0000336 regRowCount = ++pParse->nMem;
337 sqlite3VdbeAddOp2(v, OP_Integer, 0, regRowCount);
drh1bee3d72001-10-15 00:44:35 +0000338 }
339
drhe289d602009-05-05 15:46:09 +0000340 if( !isView ){
danielk1977e448dc42008-01-02 11:50:51 +0000341 /*
342 ** Open every index that needs updating. Note that if any
343 ** index could potentially invoke a REPLACE conflict resolution
344 ** action, then we need to open all indices because we might need
345 ** to be deleting some records.
drh70ce3f02003-04-15 19:22:22 +0000346 */
drh08c88eb2008-04-10 13:33:18 +0000347 if( !okOnePass ) sqlite3OpenTable(pParse, iCur, iDb, pTab, OP_OpenWrite);
danielk1977e448dc42008-01-02 11:50:51 +0000348 if( onError==OE_Replace ){
349 openAll = 1;
350 }else{
351 openAll = 0;
352 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
353 if( pIdx->onError==OE_Replace ){
354 openAll = 1;
355 break;
356 }
357 }
drh5cf590c2003-04-24 01:45:04 +0000358 }
danielk1977e448dc42008-01-02 11:50:51 +0000359 for(i=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){
drhaa9b8962008-01-08 02:57:55 +0000360 if( openAll || aRegIdx[i]>0 ){
danielk1977e448dc42008-01-02 11:50:51 +0000361 KeyInfo *pKey = sqlite3IndexKeyinfo(pParse, pIdx);
danielk1977207872a2008-01-03 07:54:23 +0000362 sqlite3VdbeAddOp4(v, OP_OpenWrite, iCur+i+1, pIdx->tnum, iDb,
drh66a51672008-01-03 00:01:23 +0000363 (char*)pKey, P4_KEYINFO_HANDOFF);
danielk1977e448dc42008-01-02 11:50:51 +0000364 assert( pParse->nTab>iCur+i+1 );
365 }
366 }
danielk1977e448dc42008-01-02 11:50:51 +0000367 }
danielk1977e448dc42008-01-02 11:50:51 +0000368
369 /* Top of the update loop */
drh08c88eb2008-04-10 13:33:18 +0000370 if( okOnePass ){
371 int a1 = sqlite3VdbeAddOp1(v, OP_NotNull, regOldRowid);
372 addr = sqlite3VdbeAddOp0(v, OP_Goto);
373 sqlite3VdbeJumpHere(v, a1);
374 }else{
drh3d4501e2008-12-04 20:40:10 +0000375 addr = sqlite3VdbeAddOp3(v, OP_RowSetRead, regRowSet, 0, regOldRowid);
drh08c88eb2008-04-10 13:33:18 +0000376 }
danielk1977e448dc42008-01-02 11:50:51 +0000377
dan165921a2009-08-28 18:53:45 +0000378 /* Make cursor iCur point to the record that is being updated. If
379 ** this record does not exist for some reason (deleted by a trigger,
380 ** for example, then jump to the next iteration of the RowSet loop. */
381 sqlite3VdbeAddOp3(v, OP_NotExists, iCur, addr, regOldRowid);
382
383 /* If there are triggers on this table, populate an array of registers
384 ** with the required old.* column data. */
danielk19772f886d12009-02-28 10:47:41 +0000385 if( pTrigger ){
drh25303782004-12-07 15:41:48 +0000386 for(i=0; i<pTab->nCol; i++){
dan165921a2009-08-28 18:53:45 +0000387 if( aXRef[i]<0 || old_col_mask==0xffffffff || (old_col_mask & (1<<i)) ){
388 sqlite3VdbeAddOp3(v, OP_Column, iCur, i, regOld+i);
389 sqlite3ColumnDefault(v, pTab, i, regOld+i);
danielk1977c3f9bad2002-05-15 08:30:12 +0000390 }else{
dan165921a2009-08-28 18:53:45 +0000391 sqlite3VdbeAddOp2(v, OP_Null, 0, regOld+i);
danielk1977c3f9bad2002-05-15 08:30:12 +0000392 }
393 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000394 }
395
dan165921a2009-08-28 18:53:45 +0000396 /* If the record number will change, set register regNewRowid to
397 ** contain the new value. If the record number is not being modified,
398 ** then regNewRowid is the same register as regOldRowid, which is
399 ** already populated. */
400 assert( chngRowid || pTrigger || regOldRowid==regNewRowid );
401 if( chngRowid ){
402 sqlite3ExprCode(pParse, pRowidExpr, regNewRowid);
403 sqlite3VdbeAddOp1(v, OP_MustBeInt, regNewRowid);
404 }else if( pTrigger ){
405 sqlite3VdbeAddOp2(v, OP_Copy, regOldRowid, regNewRowid);
406 }
drh5cf590c2003-04-24 01:45:04 +0000407
dan165921a2009-08-28 18:53:45 +0000408 /* Populate the array of registers beginning at regNew with the new
409 ** row data. This array is used to check constaints, create the new
410 ** table and index records, and as the values for any new.* references
411 ** made by triggers. */
412 for(i=0; i<pTab->nCol; i++){
413 if( i==pTab->iPKey ){
414 sqlite3VdbeAddOp2(v, OP_Null, 0, regNew+i);
415 }else{
drh5cf590c2003-04-24 01:45:04 +0000416 j = aXRef[i];
417 if( j<0 ){
dan165921a2009-08-28 18:53:45 +0000418 sqlite3VdbeAddOp3(v, OP_Column, iCur, i, regNew+i);
419 sqlite3ColumnDefault(v, pTab, i, regNew+i);
drh5cf590c2003-04-24 01:45:04 +0000420 }else{
dan165921a2009-08-28 18:53:45 +0000421 sqlite3ExprCode(pParse, pChanges->a[j].pExpr, regNew+i);
drh5cf590c2003-04-24 01:45:04 +0000422 }
423 }
dan165921a2009-08-28 18:53:45 +0000424 }
drh5cf590c2003-04-24 01:45:04 +0000425
dan76d462e2009-08-30 11:42:51 +0000426 /* If this is not a view, create the record that will be inserted into
427 ** the table (assuming no constraint checks fail). A side effect of
428 ** creating the record is applying affinity transformations to the
429 ** array of registers populated by the block above. This needs to be
430 ** done before the BEFORE triggers are fired. */
431#if 0
432 if( !isView ){
433 sqlite3VdbeAddOp3(v, OP_MakeRecord, regNew, pTab->nCol, regRec);
434 sqlite3TableAffinityStr(v, pTab);
435 sqlite3ExprCacheAffinityChange(pParse, regNew, pTab->nCol);
436 }
437#endif
438
dan165921a2009-08-28 18:53:45 +0000439 /* Fire any BEFORE UPDATE triggers. This happens before constraints are
440 ** verified. One could argue that this is wrong. */
441 sqlite3CodeRowTrigger(pParse, pTrigger, TK_UPDATE, pChanges,
dan76d462e2009-08-30 11:42:51 +0000442 TRIGGER_BEFORE, pTab, -1, regOldRowid, onError, addr);
dan165921a2009-08-28 18:53:45 +0000443
444 if( !isView ){
445
446 /* Do constraint checks. */
drh04adf412008-01-08 18:57:50 +0000447 sqlite3GenerateConstraintChecks(pParse, pTab, iCur, regNewRowid,
dan76d462e2009-08-30 11:42:51 +0000448 aRegIdx, (chngRowid?regOldRowid:0), 1, onError, addr, 0);
drh5cf590c2003-04-24 01:45:04 +0000449
dan165921a2009-08-28 18:53:45 +0000450 /* Delete the index entries associated with the current record. */
drha05a7222008-01-19 03:35:58 +0000451 j1 = sqlite3VdbeAddOp3(v, OP_NotExists, iCur, 0, regOldRowid);
drh2d401ab2008-01-10 23:50:11 +0000452 sqlite3GenerateRowIndexDelete(pParse, pTab, iCur, aRegIdx);
dan165921a2009-08-28 18:53:45 +0000453
454 /* If changing the record number, delete the old record. */
drhf0863fe2005-06-12 21:35:51 +0000455 if( chngRowid ){
drh66a51672008-01-03 00:01:23 +0000456 sqlite3VdbeAddOp2(v, OP_Delete, iCur, 0);
drh5cf590c2003-04-24 01:45:04 +0000457 }
drha05a7222008-01-19 03:35:58 +0000458 sqlite3VdbeJumpHere(v, j1);
dan165921a2009-08-28 18:53:45 +0000459
dan76d462e2009-08-30 11:42:51 +0000460 /* Insert the new index entries and the new record. */
461 sqlite3CompleteInsertion(pParse, pTab, iCur, regNewRowid, aRegIdx, 1, -1, 0, 0);
462
463#if 0
464 for(i=0; i<nIdx; i++){
465 if( aRegIdx[i] ){
466 sqlite3VdbeAddOp2(v, OP_IdxInsert, iCur+1+i, aRegIdx[i]);
467 }
468 }
469 sqlite3VdbeAddOp3(v, OP_Insert, iCur, regRec, regNewRowid);
470 if( !pParse->nested ){
471 sqlite3VdbeChangeP4(v, -1, pTab->zName, P4_STATIC);
472 sqlite3VdbeChangeP5(v, OPFLAG_NCHANGE|OPFLAG_ISUPDATE);
473 }
474#endif
drhcce7d172000-05-31 15:34:51 +0000475 }
476
drh0ca3e242002-01-29 23:07:02 +0000477 /* Increment the row counter
drh1bee3d72001-10-15 00:44:35 +0000478 */
dan165921a2009-08-28 18:53:45 +0000479 if( (db->flags & SQLITE_CountRows) && !pParse->pTriggerTab){
drh04adf412008-01-08 18:57:50 +0000480 sqlite3VdbeAddOp2(v, OP_AddImm, regRowCount, 1);
drh1bee3d72001-10-15 00:44:35 +0000481 }
482
dan165921a2009-08-28 18:53:45 +0000483 sqlite3CodeRowTrigger(pParse, pTrigger, TK_UPDATE, pChanges,
dan76d462e2009-08-30 11:42:51 +0000484 TRIGGER_AFTER, pTab, -1, regOldRowid, onError, addr);
danielk1977c3f9bad2002-05-15 08:30:12 +0000485
drhcce7d172000-05-31 15:34:51 +0000486 /* Repeat the above with the next record to be updated, until
487 ** all record selected by the WHERE clause have been updated.
488 */
drh66a51672008-01-03 00:01:23 +0000489 sqlite3VdbeAddOp2(v, OP_Goto, 0, addr);
drhd654be82005-09-20 17:42:23 +0000490 sqlite3VdbeJumpHere(v, addr);
danielk1977c3f9bad2002-05-15 08:30:12 +0000491
danielk1977e448dc42008-01-02 11:50:51 +0000492 /* Close all tables */
493 for(i=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){
drhaa9b8962008-01-08 02:57:55 +0000494 if( openAll || aRegIdx[i]>0 ){
drh66a51672008-01-03 00:01:23 +0000495 sqlite3VdbeAddOp2(v, OP_Close, iCur+i+1, 0);
danielk1977c3f9bad2002-05-15 08:30:12 +0000496 }
danielk1977e448dc42008-01-02 11:50:51 +0000497 }
drh66a51672008-01-03 00:01:23 +0000498 sqlite3VdbeAddOp2(v, OP_Close, iCur, 0);
danielk1977c3f9bad2002-05-15 08:30:12 +0000499
drh0b9f50d2009-06-23 20:28:53 +0000500 /* Update the sqlite_sequence table by storing the content of the
501 ** maximum rowid counter values recorded while inserting into
502 ** autoincrement tables.
503 */
dan165921a2009-08-28 18:53:45 +0000504 if( pParse->nested==0 && pParse->pTriggerTab==0 ){
drh0b9f50d2009-06-23 20:28:53 +0000505 sqlite3AutoincrementEnd(pParse);
506 }
507
drh1bee3d72001-10-15 00:44:35 +0000508 /*
danielk1977e7de6f22004-11-05 06:02:06 +0000509 ** Return the number of rows that were changed. If this routine is
510 ** generating code because of a call to sqlite3NestedParse(), do not
511 ** invoke the callback function.
drh1bee3d72001-10-15 00:44:35 +0000512 */
dan165921a2009-08-28 18:53:45 +0000513 if( (db->flags&SQLITE_CountRows) && !pParse->pTriggerTab && !pParse->nested ){
drh04adf412008-01-08 18:57:50 +0000514 sqlite3VdbeAddOp2(v, OP_ResultRow, regRowCount, 1);
danielk197722322fd2004-05-25 23:35:17 +0000515 sqlite3VdbeSetNumCols(v, 1);
danielk197710fb7492008-10-31 10:53:22 +0000516 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "rows updated", SQLITE_STATIC);
drh1bee3d72001-10-15 00:44:35 +0000517 }
518
drhcce7d172000-05-31 15:34:51 +0000519update_cleanup:
danielk19774adee202004-05-08 08:23:19 +0000520 sqlite3AuthContextPop(&sContext);
drh633e6d52008-07-28 19:34:53 +0000521 sqlite3DbFree(db, aRegIdx);
522 sqlite3DbFree(db, aXRef);
523 sqlite3SrcListDelete(db, pTabList);
524 sqlite3ExprListDelete(db, pChanges);
525 sqlite3ExprDelete(db, pWhere);
drhcce7d172000-05-31 15:34:51 +0000526 return;
527}
drh9c419382006-06-16 21:13:21 +0000528
529#ifndef SQLITE_OMIT_VIRTUALTABLE
530/*
531** Generate code for an UPDATE of a virtual table.
532**
533** The strategy is that we create an ephemerial table that contains
534** for each row to be changed:
535**
536** (A) The original rowid of that row.
537** (B) The revised rowid for the row. (note1)
538** (C) The content of every column in the row.
539**
540** Then we loop over this ephemeral table and for each row in
541** the ephermeral table call VUpdate.
542**
543** When finished, drop the ephemeral table.
544**
545** (note1) Actually, if we know in advance that (A) is always the same
546** as (B) we only store (A), then duplicate (A) when pulling
547** it out of the ephemeral table before calling VUpdate.
548*/
549static void updateVirtualTable(
550 Parse *pParse, /* The parsing context */
551 SrcList *pSrc, /* The virtual table to be modified */
552 Table *pTab, /* The virtual table */
553 ExprList *pChanges, /* The columns to change in the UPDATE statement */
554 Expr *pRowid, /* Expression used to recompute the rowid */
555 int *aXRef, /* Mapping from columns of pTab to entries in pChanges */
556 Expr *pWhere /* WHERE clause of the UPDATE statement */
557){
558 Vdbe *v = pParse->pVdbe; /* Virtual machine under construction */
559 ExprList *pEList = 0; /* The result set of the SELECT statement */
560 Select *pSelect = 0; /* The SELECT statement */
561 Expr *pExpr; /* Temporary expression */
562 int ephemTab; /* Table holding the result of the SELECT */
563 int i; /* Loop counter */
564 int addr; /* Address of top of loop */
danielk1977287fb612008-01-04 19:10:28 +0000565 int iReg; /* First register in set passed to OP_VUpdate */
drh17435752007-08-16 04:30:38 +0000566 sqlite3 *db = pParse->db; /* Database connection */
danielk1977595a5232009-07-24 17:58:53 +0000567 const char *pVTab = (const char*)sqlite3GetVTable(db, pTab);
drh1013c932008-01-06 00:25:21 +0000568 SelectDest dest;
drh9c419382006-06-16 21:13:21 +0000569
570 /* Construct the SELECT statement that will find the new values for
571 ** all updated rows.
572 */
drh17435752007-08-16 04:30:38 +0000573 pEList = sqlite3ExprListAppend(pParse, 0,
drhb7916a72009-05-27 10:31:29 +0000574 sqlite3CreateIdExpr(pParse, "_rowid_"));
drh9c419382006-06-16 21:13:21 +0000575 if( pRowid ){
drh17435752007-08-16 04:30:38 +0000576 pEList = sqlite3ExprListAppend(pParse, pEList,
drhb7916a72009-05-27 10:31:29 +0000577 sqlite3ExprDup(db, pRowid, 0));
drh9c419382006-06-16 21:13:21 +0000578 }
danielk197765fd59f2006-06-24 11:51:33 +0000579 assert( pTab->iPKey<0 );
drh9c419382006-06-16 21:13:21 +0000580 for(i=0; i<pTab->nCol; i++){
danielk197765fd59f2006-06-24 11:51:33 +0000581 if( aXRef[i]>=0 ){
danielk19776ab3a2e2009-02-19 14:39:25 +0000582 pExpr = sqlite3ExprDup(db, pChanges->a[aXRef[i]].pExpr, 0);
drh9c419382006-06-16 21:13:21 +0000583 }else{
drh17435752007-08-16 04:30:38 +0000584 pExpr = sqlite3CreateIdExpr(pParse, pTab->aCol[i].zName);
drh9c419382006-06-16 21:13:21 +0000585 }
drhb7916a72009-05-27 10:31:29 +0000586 pEList = sqlite3ExprListAppend(pParse, pEList, pExpr);
drh9c419382006-06-16 21:13:21 +0000587 }
drh17435752007-08-16 04:30:38 +0000588 pSelect = sqlite3SelectNew(pParse, pEList, pSrc, pWhere, 0, 0, 0, 0, 0, 0);
drh9c419382006-06-16 21:13:21 +0000589
590 /* Create the ephemeral table into which the update results will
591 ** be stored.
592 */
593 assert( v );
594 ephemTab = pParse->nTab++;
drh66a51672008-01-03 00:01:23 +0000595 sqlite3VdbeAddOp2(v, OP_OpenEphemeral, ephemTab, pTab->nCol+1+(pRowid!=0));
drh9c419382006-06-16 21:13:21 +0000596
597 /* fill the ephemeral table
598 */
drh1013c932008-01-06 00:25:21 +0000599 sqlite3SelectDestInit(&dest, SRT_Table, ephemTab);
drh7d10d5a2008-08-20 16:35:10 +0000600 sqlite3Select(pParse, pSelect, &dest);
drh9c419382006-06-16 21:13:21 +0000601
danielk1977287fb612008-01-04 19:10:28 +0000602 /* Generate code to scan the ephemeral table and call VUpdate. */
603 iReg = ++pParse->nMem;
604 pParse->nMem += pTab->nCol+1;
drhbdfb6b52009-06-27 11:17:35 +0000605 addr = sqlite3VdbeAddOp2(v, OP_Rewind, ephemTab, 0);
danielk1977287fb612008-01-04 19:10:28 +0000606 sqlite3VdbeAddOp3(v, OP_Column, ephemTab, 0, iReg);
607 sqlite3VdbeAddOp3(v, OP_Column, ephemTab, (pRowid?1:0), iReg+1);
drh9c419382006-06-16 21:13:21 +0000608 for(i=0; i<pTab->nCol; i++){
danielk1977287fb612008-01-04 19:10:28 +0000609 sqlite3VdbeAddOp3(v, OP_Column, ephemTab, i+1+(pRowid!=0), iReg+2+i);
drh9c419382006-06-16 21:13:21 +0000610 }
drh4f3dd152008-04-28 18:46:43 +0000611 sqlite3VtabMakeWritable(pParse, pTab);
danielk1977595a5232009-07-24 17:58:53 +0000612 sqlite3VdbeAddOp4(v, OP_VUpdate, 0, pTab->nCol+2, iReg, pVTab, P4_VTAB);
drhbdfb6b52009-06-27 11:17:35 +0000613 sqlite3VdbeAddOp2(v, OP_Next, ephemTab, addr+1);
614 sqlite3VdbeJumpHere(v, addr);
drh66a51672008-01-03 00:01:23 +0000615 sqlite3VdbeAddOp2(v, OP_Close, ephemTab, 0);
drh9c419382006-06-16 21:13:21 +0000616
617 /* Cleanup */
drh633e6d52008-07-28 19:34:53 +0000618 sqlite3SelectDelete(db, pSelect);
drh9c419382006-06-16 21:13:21 +0000619}
620#endif /* SQLITE_OMIT_VIRTUALTABLE */
drhf39d9582008-03-19 20:42:13 +0000621
622/* Make sure "isView" gets undefined in case this file becomes part of
623** the amalgamation - so that subsequent files do not see isView as a
624** macro. */
625#undef isView