blob: 6d1a21f41481591c811e3a0ae3d80f14ca3acd8e [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**
drh2d401ab2008-01-10 23:50:11 +000015** $Id: update.c,v 1.167 2008/01/10 23:50:11 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){
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 ){
drh66a51672008-01-03 00:01:23 +000067 sqlite3VdbeChangeP4(v, -1, (const char *)pValue, P4_MEM);
drh29dda4a2005-07-21 18:23:20 +000068 }
danielk1977aee18ef2005-03-09 12:26:50 +000069 }
70}
71
72/*
drhcce7d172000-05-31 15:34:51 +000073** Process an UPDATE statement.
drhb2fe7d82003-04-20 17:29:23 +000074**
75** UPDATE OR IGNORE table_wxyz SET a=b, c=d WHERE e<5 AND f NOT NULL;
76** \_______/ \________/ \______/ \________________/
77* onError pTabList pChanges pWhere
drhcce7d172000-05-31 15:34:51 +000078*/
danielk19774adee202004-05-08 08:23:19 +000079void sqlite3Update(
drhcce7d172000-05-31 15:34:51 +000080 Parse *pParse, /* The parser context */
drh113088e2003-03-20 01:16:58 +000081 SrcList *pTabList, /* The table in which we should change things */
drhcce7d172000-05-31 15:34:51 +000082 ExprList *pChanges, /* Things to be changed */
drh9cfcf5d2002-01-29 18:41:24 +000083 Expr *pWhere, /* The WHERE clause. May be null */
84 int onError /* How to handle constraint errors */
drhcce7d172000-05-31 15:34:51 +000085){
86 int i, j; /* Loop counters */
87 Table *pTab; /* The table to be updated */
danielk1977742f9472004-06-16 12:02:43 +000088 int addr = 0; /* VDBE instruction address of the start of the loop */
drhcce7d172000-05-31 15:34:51 +000089 WhereInfo *pWInfo; /* Information about the WHERE clause */
90 Vdbe *v; /* The virtual database engine */
91 Index *pIdx; /* For looping over indices */
92 int nIdx; /* Number of indices that need updating */
drh6a3ea0e2003-05-02 14:32:12 +000093 int iCur; /* VDBE Cursor number of pTab */
drh9bb575f2004-09-06 17:24:11 +000094 sqlite3 *db; /* The database structure */
drhaa9b8962008-01-08 02:57:55 +000095 int *aRegIdx = 0; /* One register assigned to each index to be updated */
drhcce7d172000-05-31 15:34:51 +000096 int *aXRef = 0; /* aXRef[i] is the index in pChanges->a[] of the
drh967e8b72000-06-21 13:59:10 +000097 ** an expression for the i-th column of the table.
98 ** aXRef[i]==-1 if the i-th column is not changed. */
drhf0863fe2005-06-12 21:35:51 +000099 int chngRowid; /* True if the record number is being changed */
100 Expr *pRowidExpr = 0; /* Expression defining the new record number */
danielk1977742f9472004-06-16 12:02:43 +0000101 int openAll = 0; /* True if all indices need to be opened */
drh85e20962003-04-25 17:52:11 +0000102 AuthContext sContext; /* The authorization context */
danielk1977b3bce662005-01-29 08:32:43 +0000103 NameContext sNC; /* The name-context to resolve expressions in */
danielk1977da184232006-01-05 11:34:32 +0000104 int iDb; /* Database containing the table being updated */
drhcce7d172000-05-31 15:34:51 +0000105
drh798da522004-11-04 04:42:28 +0000106#ifndef SQLITE_OMIT_TRIGGER
107 int isView; /* Trying to update a view */
drhdca76842004-12-07 14:06:13 +0000108 int triggers_exist = 0; /* True if any row triggers exist */
drh798da522004-11-04 04:42:28 +0000109#endif
danielk19778f2c54e2008-01-01 19:02:09 +0000110 int iBeginAfterTrigger; /* Address of after trigger program */
111 int iEndAfterTrigger; /* Exit of after trigger program */
112 int iBeginBeforeTrigger; /* Address of before trigger program */
113 int iEndBeforeTrigger; /* Exit of before trigger program */
114 u32 old_col_mask = 0; /* Mask of OLD.* columns in use */
115 u32 new_col_mask = 0; /* Mask of NEW.* columns in use */
danielk1977c3f9bad2002-05-15 08:30:12 +0000116
117 int newIdx = -1; /* index of trigger "new" temp table */
118 int oldIdx = -1; /* index of trigger "old" temp table */
119
drh04adf412008-01-08 18:57:50 +0000120 /* Register Allocations */
121 int regRowCount = 0; /* A count of rows changed */
122 int regOldRowid; /* The old rowid */
123 int regNewRowid; /* The new rowid */
124 int regData; /* New data for the row */
125
drh85e20962003-04-25 17:52:11 +0000126 sContext.pParse = 0;
drh17435752007-08-16 04:30:38 +0000127 db = pParse->db;
128 if( pParse->nErr || db->mallocFailed ){
drh6f7adc82006-01-11 21:41:20 +0000129 goto update_cleanup;
130 }
drh113088e2003-03-20 01:16:58 +0000131 assert( pTabList->nSrc==1 );
drhdaffd0e2001-04-11 14:28:42 +0000132
drhb2fe7d82003-04-20 17:29:23 +0000133 /* Locate the table which we want to update.
drhcce7d172000-05-31 15:34:51 +0000134 */
danielk19774adee202004-05-08 08:23:19 +0000135 pTab = sqlite3SrcListLookup(pParse, pTabList);
drha69d9162003-04-17 22:57:53 +0000136 if( pTab==0 ) goto update_cleanup;
danielk1977da184232006-01-05 11:34:32 +0000137 iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
drhb7f91642004-10-31 02:22:47 +0000138
139 /* Figure out if we have any triggers and if the table being
140 ** updated is a view
141 */
142#ifndef SQLITE_OMIT_TRIGGER
drhdca76842004-12-07 14:06:13 +0000143 triggers_exist = sqlite3TriggersExist(pParse, pTab, TK_UPDATE, pChanges);
drh5cf590c2003-04-24 01:45:04 +0000144 isView = pTab->pSelect!=0;
drhb7f91642004-10-31 02:22:47 +0000145#else
drhdca76842004-12-07 14:06:13 +0000146# define triggers_exist 0
drhb7f91642004-10-31 02:22:47 +0000147# define isView 0
148#endif
149#ifdef SQLITE_OMIT_VIEW
150# undef isView
151# define isView 0
152#endif
153
drhdca76842004-12-07 14:06:13 +0000154 if( sqlite3IsReadOnly(pParse, pTab, triggers_exist) ){
drh5cf590c2003-04-24 01:45:04 +0000155 goto update_cleanup;
drha69d9162003-04-17 22:57:53 +0000156 }
danielk1977b3d24bf2006-06-19 03:05:10 +0000157 if( sqlite3ViewGetColumnNames(pParse, pTab) ){
158 goto update_cleanup;
drh5cf590c2003-04-24 01:45:04 +0000159 }
drh17435752007-08-16 04:30:38 +0000160 aXRef = sqlite3DbMallocRaw(db, sizeof(int) * pTab->nCol );
drhcce7d172000-05-31 15:34:51 +0000161 if( aXRef==0 ) goto update_cleanup;
162 for(i=0; i<pTab->nCol; i++) aXRef[i] = -1;
163
drhb2fe7d82003-04-20 17:29:23 +0000164 /* If there are FOR EACH ROW triggers, allocate cursors for the
165 ** special OLD and NEW tables
166 */
drhdca76842004-12-07 14:06:13 +0000167 if( triggers_exist ){
danielk1977c3f9bad2002-05-15 08:30:12 +0000168 newIdx = pParse->nTab++;
169 oldIdx = pParse->nTab++;
170 }
171
drh53e3fc72002-07-16 17:22:50 +0000172 /* Allocate a cursors for the main database table and for all indices.
173 ** The index cursors might not be used, but if they are used they
174 ** need to occur right after the database cursor. So go ahead and
175 ** allocate enough space, just in case.
176 */
drh6a3ea0e2003-05-02 14:32:12 +0000177 pTabList->a[0].iCursor = iCur = pParse->nTab++;
drh53e3fc72002-07-16 17:22:50 +0000178 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
179 pParse->nTab++;
180 }
181
danielk1977b3bce662005-01-29 08:32:43 +0000182 /* Initialize the name-context */
183 memset(&sNC, 0, sizeof(sNC));
184 sNC.pParse = pParse;
185 sNC.pSrcList = pTabList;
186
drh85e20962003-04-25 17:52:11 +0000187 /* Resolve the column names in all the expressions of the
188 ** of the UPDATE statement. Also find the column index
drhed6c8672003-01-12 18:02:16 +0000189 ** for each column to be updated in the pChanges array. For each
190 ** column to be updated, make sure we have authorization to change
191 ** that column.
drhcce7d172000-05-31 15:34:51 +0000192 */
drhf0863fe2005-06-12 21:35:51 +0000193 chngRowid = 0;
drhcce7d172000-05-31 15:34:51 +0000194 for(i=0; i<pChanges->nExpr; i++){
danielk1977b3bce662005-01-29 08:32:43 +0000195 if( sqlite3ExprResolveNames(&sNC, pChanges->a[i].pExpr) ){
drhcce7d172000-05-31 15:34:51 +0000196 goto update_cleanup;
197 }
198 for(j=0; j<pTab->nCol; j++){
danielk19774adee202004-05-08 08:23:19 +0000199 if( sqlite3StrICmp(pTab->aCol[j].zName, pChanges->a[i].zName)==0 ){
drh9647ff82002-01-14 02:56:24 +0000200 if( j==pTab->iPKey ){
drhf0863fe2005-06-12 21:35:51 +0000201 chngRowid = 1;
202 pRowidExpr = pChanges->a[i].pExpr;
drh4a324312001-12-21 14:30:42 +0000203 }
drhcce7d172000-05-31 15:34:51 +0000204 aXRef[j] = i;
205 break;
206 }
207 }
208 if( j>=pTab->nCol ){
danielk19774adee202004-05-08 08:23:19 +0000209 if( sqlite3IsRowid(pChanges->a[i].zName) ){
drhf0863fe2005-06-12 21:35:51 +0000210 chngRowid = 1;
211 pRowidExpr = pChanges->a[i].pExpr;
drha0217ba2003-06-01 01:10:33 +0000212 }else{
danielk19774adee202004-05-08 08:23:19 +0000213 sqlite3ErrorMsg(pParse, "no such column: %s", pChanges->a[i].zName);
drha0217ba2003-06-01 01:10:33 +0000214 goto update_cleanup;
215 }
drhcce7d172000-05-31 15:34:51 +0000216 }
drhed6c8672003-01-12 18:02:16 +0000217#ifndef SQLITE_OMIT_AUTHORIZATION
drhe5f9c642003-01-13 23:27:31 +0000218 {
219 int rc;
danielk19774adee202004-05-08 08:23:19 +0000220 rc = sqlite3AuthCheck(pParse, SQLITE_UPDATE, pTab->zName,
danielk1977da184232006-01-05 11:34:32 +0000221 pTab->aCol[j].zName, db->aDb[iDb].zName);
drhe5f9c642003-01-13 23:27:31 +0000222 if( rc==SQLITE_DENY ){
223 goto update_cleanup;
224 }else if( rc==SQLITE_IGNORE ){
225 aXRef[j] = -1;
226 }
drhed6c8672003-01-12 18:02:16 +0000227 }
228#endif
drhcce7d172000-05-31 15:34:51 +0000229 }
230
drhaa9b8962008-01-08 02:57:55 +0000231 /* Allocate memory for the array aRegIdx[]. There is one entry in the
232 ** array for each index associated with table being updated. Fill in
233 ** the value with a register number for indices that are to be used
234 ** and with zero for unused indices.
drhcce7d172000-05-31 15:34:51 +0000235 */
drhaa9b8962008-01-08 02:57:55 +0000236 for(nIdx=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, nIdx++){}
237 if( nIdx>0 ){
238 aRegIdx = sqlite3DbMallocRaw(db, sizeof(Index*) * nIdx );
239 if( aRegIdx==0 ) goto update_cleanup;
240 }
241 for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
242 int reg;
drhf0863fe2005-06-12 21:35:51 +0000243 if( chngRowid ){
drhaa9b8962008-01-08 02:57:55 +0000244 reg = ++pParse->nMem;
245 }else{
246 reg = 0;
drh4a324312001-12-21 14:30:42 +0000247 for(i=0; i<pIdx->nColumn; i++){
drhaa9b8962008-01-08 02:57:55 +0000248 if( aXRef[pIdx->aiColumn[i]]>=0 ){
249 reg = ++pParse->nMem;
250 break;
251 }
drh4a324312001-12-21 14:30:42 +0000252 }
drhcce7d172000-05-31 15:34:51 +0000253 }
drhaa9b8962008-01-08 02:57:55 +0000254 aRegIdx[j] = reg;
drhcce7d172000-05-31 15:34:51 +0000255 }
256
drh04adf412008-01-08 18:57:50 +0000257 /* Allocate a block of register used to store the change record
258 ** sent to sqlite3GenerateConstraintChecks(). There are either
259 ** one or two registers for holding the rowid. One rowid register
260 ** is used if chngRowid is false and two are used if chngRowid is
261 ** true. Following these are pTab->nCol register holding column
262 ** data.
263 */
264 regOldRowid = regNewRowid = pParse->nMem + 1;
265 pParse->nMem += pTab->nCol + 1;
266 if( chngRowid ){
267 regNewRowid++;
268 pParse->nMem++;
269 }
270 regData = regNewRowid+1;
271
272
drhcce7d172000-05-31 15:34:51 +0000273 /* Begin generating code.
274 */
danielk19774adee202004-05-08 08:23:19 +0000275 v = sqlite3GetVdbe(pParse);
drhcce7d172000-05-31 15:34:51 +0000276 if( v==0 ) goto update_cleanup;
drh4794f732004-11-05 17:17:50 +0000277 if( pParse->nested==0 ) sqlite3VdbeCountChanges(v);
danielk1977da184232006-01-05 11:34:32 +0000278 sqlite3BeginWriteOperation(pParse, 1, iDb);
drhcce7d172000-05-31 15:34:51 +0000279
drh9c419382006-06-16 21:13:21 +0000280#ifndef SQLITE_OMIT_VIRTUALTABLE
281 /* Virtual tables must be handled separately */
282 if( IsVirtual(pTab) ){
283 updateVirtualTable(pParse, pTabList, pTab, pChanges, pRowidExpr, aXRef,
284 pWhere);
285 pWhere = 0;
286 pTabList = 0;
287 goto update_cleanup;
288 }
289#endif
290
291 /* Resolve the column names in all the expressions in the
292 ** WHERE clause.
293 */
294 if( sqlite3ExprResolveNames(&sNC, pWhere) ){
295 goto update_cleanup;
296 }
297
danielk19774273dea2006-06-17 06:31:18 +0000298 /* Start the view context
299 */
300 if( isView ){
301 sqlite3AuthContextPush(pParse, &sContext, pTab->zName);
302 }
303
danielk19778f2c54e2008-01-01 19:02:09 +0000304 /* Generate the code for triggers.
305 */
306 if( triggers_exist ){
danielk1977e448dc42008-01-02 11:50:51 +0000307 int iGoto;
308
309 /* Create pseudo-tables for NEW and OLD
310 */
drh66a51672008-01-03 00:01:23 +0000311 sqlite3VdbeAddOp2(v, OP_OpenPseudo, oldIdx, 0);
312 sqlite3VdbeAddOp2(v, OP_SetNumColumns, oldIdx, pTab->nCol);
313 sqlite3VdbeAddOp2(v, OP_OpenPseudo, newIdx, 0);
314 sqlite3VdbeAddOp2(v, OP_SetNumColumns, newIdx, pTab->nCol);
danielk1977e448dc42008-01-02 11:50:51 +0000315
drh66a51672008-01-03 00:01:23 +0000316 iGoto = sqlite3VdbeAddOp2(v, OP_Goto, 0, 0);
danielk19778f2c54e2008-01-01 19:02:09 +0000317 addr = sqlite3VdbeMakeLabel(v);
318 iBeginBeforeTrigger = sqlite3VdbeCurrentAddr(v);
319 if( sqlite3CodeRowTrigger(pParse, TK_UPDATE, pChanges, TRIGGER_BEFORE, pTab,
320 newIdx, oldIdx, onError, addr, &old_col_mask, &new_col_mask) ){
321 goto update_cleanup;
322 }
drh66a51672008-01-03 00:01:23 +0000323 iEndBeforeTrigger = sqlite3VdbeAddOp2(v, OP_Goto, 0, 0);
danielk19778f2c54e2008-01-01 19:02:09 +0000324 iBeginAfterTrigger = sqlite3VdbeCurrentAddr(v);
325 if( sqlite3CodeRowTrigger(pParse, TK_UPDATE, pChanges, TRIGGER_AFTER, pTab,
326 newIdx, oldIdx, onError, addr, &old_col_mask, &new_col_mask) ){
327 goto update_cleanup;
328 }
drh66a51672008-01-03 00:01:23 +0000329 iEndAfterTrigger = sqlite3VdbeAddOp2(v, OP_Goto, 0, 0);
danielk19778f2c54e2008-01-01 19:02:09 +0000330 sqlite3VdbeJumpHere(v, iGoto);
331 }
332
drh9d2985c2005-09-08 01:58:42 +0000333 /* If we are trying to update a view, realize that view into
334 ** a ephemeral table.
drh5cf590c2003-04-24 01:45:04 +0000335 */
336 if( isView ){
drh85e20962003-04-25 17:52:11 +0000337 Select *pView;
drh1013c932008-01-06 00:25:21 +0000338 SelectDest dest;
339
drh17435752007-08-16 04:30:38 +0000340 pView = sqlite3SelectDup(db, pTab->pSelect);
danielk19778f2c54e2008-01-01 19:02:09 +0000341 sqlite3SelectMask(pParse, pView, old_col_mask|new_col_mask);
drh1013c932008-01-06 00:25:21 +0000342 sqlite3SelectDestInit(&dest, SRT_EphemTab, iCur);
danielk19776c8c8ce2008-01-02 16:27:09 +0000343 sqlite3Select(pParse, pView, &dest, 0, 0, 0, 0);
danielk19774adee202004-05-08 08:23:19 +0000344 sqlite3SelectDelete(pView);
drh5cf590c2003-04-24 01:45:04 +0000345 }
346
drhcce7d172000-05-31 15:34:51 +0000347 /* Begin the database scan
348 */
danielk1977a9d1ccb2008-01-05 17:39:29 +0000349 pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, 0, 0);
drhcce7d172000-05-31 15:34:51 +0000350 if( pWInfo==0 ) goto update_cleanup;
351
drh4cbdda92006-06-14 19:00:20 +0000352 /* Remember the rowid of every item to be updated.
drhcce7d172000-05-31 15:34:51 +0000353 */
drh04adf412008-01-08 18:57:50 +0000354 sqlite3VdbeAddOp2(v, IsVirtual(pTab) ? OP_VRowid : OP_Rowid,iCur,regOldRowid);
355 sqlite3VdbeAddOp2(v, OP_FifoWrite, regOldRowid, 0);
drhcce7d172000-05-31 15:34:51 +0000356
357 /* End the database scan loop.
358 */
danielk19774adee202004-05-08 08:23:19 +0000359 sqlite3WhereEnd(pWInfo);
drhcce7d172000-05-31 15:34:51 +0000360
drh1bee3d72001-10-15 00:44:35 +0000361 /* Initialize the count of updated rows
362 */
danielk1977c3f9bad2002-05-15 08:30:12 +0000363 if( db->flags & SQLITE_CountRows && !pParse->trigStack ){
drh04adf412008-01-08 18:57:50 +0000364 regRowCount = ++pParse->nMem;
365 sqlite3VdbeAddOp2(v, OP_Integer, 0, regRowCount);
drh1bee3d72001-10-15 00:44:35 +0000366 }
367
danielk1977e448dc42008-01-02 11:50:51 +0000368 if( !isView && !IsVirtual(pTab) ){
369 /*
370 ** Open every index that needs updating. Note that if any
371 ** index could potentially invoke a REPLACE conflict resolution
372 ** action, then we need to open all indices because we might need
373 ** to be deleting some records.
drh70ce3f02003-04-15 19:22:22 +0000374 */
danielk1977e448dc42008-01-02 11:50:51 +0000375 sqlite3OpenTable(pParse, iCur, iDb, pTab, OP_OpenWrite);
376 if( onError==OE_Replace ){
377 openAll = 1;
378 }else{
379 openAll = 0;
380 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
381 if( pIdx->onError==OE_Replace ){
382 openAll = 1;
383 break;
384 }
385 }
drh5cf590c2003-04-24 01:45:04 +0000386 }
danielk1977e448dc42008-01-02 11:50:51 +0000387 for(i=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){
drhaa9b8962008-01-08 02:57:55 +0000388 if( openAll || aRegIdx[i]>0 ){
danielk1977e448dc42008-01-02 11:50:51 +0000389 KeyInfo *pKey = sqlite3IndexKeyinfo(pParse, pIdx);
danielk1977207872a2008-01-03 07:54:23 +0000390 sqlite3VdbeAddOp4(v, OP_OpenWrite, iCur+i+1, pIdx->tnum, iDb,
drh66a51672008-01-03 00:01:23 +0000391 (char*)pKey, P4_KEYINFO_HANDOFF);
danielk1977e448dc42008-01-02 11:50:51 +0000392 assert( pParse->nTab>iCur+i+1 );
393 }
394 }
danielk1977e448dc42008-01-02 11:50:51 +0000395 }
396
397 /* Jump back to this point if a trigger encounters an IGNORE constraint. */
398 if( triggers_exist ){
399 sqlite3VdbeResolveLabel(v, addr);
400 }
401
402 /* Top of the update loop */
drh04adf412008-01-08 18:57:50 +0000403 addr = sqlite3VdbeAddOp2(v, OP_FifoRead, regOldRowid, 0);
drh66a51672008-01-03 00:01:23 +0000404 sqlite3VdbeAddOp2(v, OP_StackDepth, -1, 0);
danielk1977e448dc42008-01-02 11:50:51 +0000405
406 if( triggers_exist ){
drh2d401ab2008-01-10 23:50:11 +0000407 int regRowid;
408 int regRow;
409 int regCols;
410
danielk1977e448dc42008-01-02 11:50:51 +0000411 /* Make cursor iCur point to the record that is being updated.
412 */
drh04adf412008-01-08 18:57:50 +0000413 sqlite3VdbeAddOp3(v, OP_NotExists, iCur, addr, regOldRowid);
danielk1977c3f9bad2002-05-15 08:30:12 +0000414
drh70ce3f02003-04-15 19:22:22 +0000415 /* Generate the OLD table
416 */
drh2d401ab2008-01-10 23:50:11 +0000417 regRowid = sqlite3GetTempReg(pParse);
418 regRow = sqlite3GetTempReg(pParse);
419 sqlite3VdbeAddOp2(v, OP_Rowid, iCur, regRowid);
danielk19778f2c54e2008-01-01 19:02:09 +0000420 if( !old_col_mask ){
drh2d401ab2008-01-10 23:50:11 +0000421 sqlite3VdbeAddOp2(v, OP_Null, 0, regRow);
danielk19778f2c54e2008-01-01 19:02:09 +0000422 }else{
drh2d401ab2008-01-10 23:50:11 +0000423 sqlite3VdbeAddOp2(v, OP_RowData, iCur, regRow);
danielk19778f2c54e2008-01-01 19:02:09 +0000424 }
drh2d401ab2008-01-10 23:50:11 +0000425 sqlite3VdbeAddOp3(v, OP_Insert, oldIdx, regRow, regRowid);
danielk1977c3f9bad2002-05-15 08:30:12 +0000426
drh70ce3f02003-04-15 19:22:22 +0000427 /* Generate the NEW table
428 */
drhf0863fe2005-06-12 21:35:51 +0000429 if( chngRowid ){
drh2d401ab2008-01-10 23:50:11 +0000430 sqlite3ExprCodeAndCache(pParse, pRowidExpr, regRowid);
drh70ce3f02003-04-15 19:22:22 +0000431 }else{
drh2d401ab2008-01-10 23:50:11 +0000432 sqlite3VdbeAddOp2(v, OP_Rowid, iCur, regRowid);
drh70ce3f02003-04-15 19:22:22 +0000433 }
drh2d401ab2008-01-10 23:50:11 +0000434 regCols = sqlite3GetTempRange(pParse, pTab->nCol);
drh25303782004-12-07 15:41:48 +0000435 for(i=0; i<pTab->nCol; i++){
drh70ce3f02003-04-15 19:22:22 +0000436 if( i==pTab->iPKey ){
drh2d401ab2008-01-10 23:50:11 +0000437 sqlite3VdbeAddOp2(v, OP_Null, 0, regCols+i);
drh70ce3f02003-04-15 19:22:22 +0000438 continue;
439 }
440 j = aXRef[i];
danielk19778f2c54e2008-01-01 19:02:09 +0000441 if( new_col_mask&((u32)1<<i) || new_col_mask==0xffffffff ){
442 if( j<0 ){
drh2d401ab2008-01-10 23:50:11 +0000443 sqlite3VdbeAddOp3(v, OP_Column, iCur, i, regCols+i);
danielk19778f2c54e2008-01-01 19:02:09 +0000444 sqlite3ColumnDefault(v, pTab, i);
445 }else{
drh2d401ab2008-01-10 23:50:11 +0000446 sqlite3ExprCodeAndCache(pParse, pChanges->a[j].pExpr, regCols+i);
danielk19778f2c54e2008-01-01 19:02:09 +0000447 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000448 }else{
drh2d401ab2008-01-10 23:50:11 +0000449 sqlite3VdbeAddOp2(v, OP_Null, 0, regCols+i);
danielk1977c3f9bad2002-05-15 08:30:12 +0000450 }
451 }
drh2d401ab2008-01-10 23:50:11 +0000452 sqlite3VdbeAddOp3(v, OP_RegMakeRec, regCols, pTab->nCol, regRow);
danielk1977a37cdde2004-05-16 11:15:36 +0000453 if( !isView ){
454 sqlite3TableAffinityStr(v, pTab);
455 }
drh2d401ab2008-01-10 23:50:11 +0000456 sqlite3ReleaseTempRange(pParse, regCols, pTab->nCol);
danielk1977a37cdde2004-05-16 11:15:36 +0000457 if( pParse->nErr ) goto update_cleanup;
drh2d401ab2008-01-10 23:50:11 +0000458 sqlite3VdbeAddOp3(v, OP_Insert, newIdx, regRow, regRowid);
459 sqlite3ReleaseTempReg(pParse, regRowid);
460 sqlite3ReleaseTempReg(pParse, regRow);
danielk1977c3f9bad2002-05-15 08:30:12 +0000461
drh66a51672008-01-03 00:01:23 +0000462 sqlite3VdbeAddOp2(v, OP_Goto, 0, iBeginBeforeTrigger);
danielk19778f2c54e2008-01-01 19:02:09 +0000463 sqlite3VdbeJumpHere(v, iEndBeforeTrigger);
danielk1977c3f9bad2002-05-15 08:30:12 +0000464 }
465
drh4cbdda92006-06-14 19:00:20 +0000466 if( !isView && !IsVirtual(pTab) ){
drh5cf590c2003-04-24 01:45:04 +0000467 /* Loop over every record that needs updating. We have to load
468 ** the old data for each record to be updated because some columns
469 ** might not change and we will need to copy the old value.
drh4cbdda92006-06-14 19:00:20 +0000470 ** Also, the old data is needed to delete the old index entries.
drh5cf590c2003-04-24 01:45:04 +0000471 ** So make the cursor point at the old record.
472 */
drh04adf412008-01-08 18:57:50 +0000473 sqlite3VdbeAddOp3(v, OP_NotExists, iCur, addr, regOldRowid);
drh5cf590c2003-04-24 01:45:04 +0000474
475 /* If the record number will change, push the record number as it
476 ** will be after the update. (The old record number is currently
477 ** on top of the stack.)
478 */
drhf0863fe2005-06-12 21:35:51 +0000479 if( chngRowid ){
drh04adf412008-01-08 18:57:50 +0000480 sqlite3ExprCode(pParse, pRowidExpr, regNewRowid);
drh3c84ddf2008-01-09 02:15:38 +0000481 sqlite3VdbeAddOp1(v, OP_MustBeInt, regNewRowid);
drhcce7d172000-05-31 15:34:51 +0000482 }
drh5cf590c2003-04-24 01:45:04 +0000483
484 /* Compute new data for this record.
485 */
486 for(i=0; i<pTab->nCol; i++){
487 if( i==pTab->iPKey ){
drh04adf412008-01-08 18:57:50 +0000488 sqlite3VdbeAddOp2(v, OP_Null, 0, regData+i);
drh5cf590c2003-04-24 01:45:04 +0000489 continue;
490 }
491 j = aXRef[i];
492 if( j<0 ){
drh04adf412008-01-08 18:57:50 +0000493 sqlite3VdbeAddOp3(v, OP_Column, iCur, i, regData+i);
danielk1977aee18ef2005-03-09 12:26:50 +0000494 sqlite3ColumnDefault(v, pTab, i);
drh5cf590c2003-04-24 01:45:04 +0000495 }else{
drh04adf412008-01-08 18:57:50 +0000496 sqlite3ExprCode(pParse, pChanges->a[j].pExpr, regData+i);
drh5cf590c2003-04-24 01:45:04 +0000497 }
498 }
499
500 /* Do constraint checks
501 */
drh04adf412008-01-08 18:57:50 +0000502 sqlite3GenerateConstraintChecks(pParse, pTab, iCur, regNewRowid,
503 aRegIdx, chngRowid, 1,
504 onError, addr);
drh5cf590c2003-04-24 01:45:04 +0000505
506 /* Delete the old indices for the current record.
507 */
drh2d401ab2008-01-10 23:50:11 +0000508 sqlite3GenerateRowIndexDelete(pParse, pTab, iCur, aRegIdx);
drh5cf590c2003-04-24 01:45:04 +0000509
510 /* If changing the record number, delete the old record.
511 */
drhf0863fe2005-06-12 21:35:51 +0000512 if( chngRowid ){
drh66a51672008-01-03 00:01:23 +0000513 sqlite3VdbeAddOp2(v, OP_Delete, iCur, 0);
drh5cf590c2003-04-24 01:45:04 +0000514 }
515
516 /* Create the new index entries and the new record.
517 */
drh04adf412008-01-08 18:57:50 +0000518 sqlite3CompleteInsertion(pParse, pTab, iCur, regNewRowid,
519 aRegIdx, chngRowid, 1, -1, 0);
drhcce7d172000-05-31 15:34:51 +0000520 }
521
drh0ca3e242002-01-29 23:07:02 +0000522 /* Increment the row counter
drh1bee3d72001-10-15 00:44:35 +0000523 */
danielk1977c3f9bad2002-05-15 08:30:12 +0000524 if( db->flags & SQLITE_CountRows && !pParse->trigStack){
drh04adf412008-01-08 18:57:50 +0000525 sqlite3VdbeAddOp2(v, OP_AddImm, regRowCount, 1);
drh1bee3d72001-10-15 00:44:35 +0000526 }
527
drh5cf590c2003-04-24 01:45:04 +0000528 /* If there are triggers, close all the cursors after each iteration
529 ** through the loop. The fire the after triggers.
530 */
drhdca76842004-12-07 14:06:13 +0000531 if( triggers_exist ){
drh66a51672008-01-03 00:01:23 +0000532 sqlite3VdbeAddOp2(v, OP_Goto, 0, iBeginAfterTrigger);
danielk19778f2c54e2008-01-01 19:02:09 +0000533 sqlite3VdbeJumpHere(v, iEndAfterTrigger);
danielk1977c3f9bad2002-05-15 08:30:12 +0000534 }
535
drhcce7d172000-05-31 15:34:51 +0000536 /* Repeat the above with the next record to be updated, until
537 ** all record selected by the WHERE clause have been updated.
538 */
drh66a51672008-01-03 00:01:23 +0000539 sqlite3VdbeAddOp2(v, OP_Goto, 0, addr);
drhd654be82005-09-20 17:42:23 +0000540 sqlite3VdbeJumpHere(v, addr);
danielk1977c3f9bad2002-05-15 08:30:12 +0000541
danielk1977e448dc42008-01-02 11:50:51 +0000542 /* Close all tables */
543 for(i=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){
drhaa9b8962008-01-08 02:57:55 +0000544 if( openAll || aRegIdx[i]>0 ){
drh66a51672008-01-03 00:01:23 +0000545 sqlite3VdbeAddOp2(v, OP_Close, iCur+i+1, 0);
danielk1977c3f9bad2002-05-15 08:30:12 +0000546 }
danielk1977e448dc42008-01-02 11:50:51 +0000547 }
drh66a51672008-01-03 00:01:23 +0000548 sqlite3VdbeAddOp2(v, OP_Close, iCur, 0);
danielk1977e448dc42008-01-02 11:50:51 +0000549 if( triggers_exist ){
drh66a51672008-01-03 00:01:23 +0000550 sqlite3VdbeAddOp2(v, OP_Close, newIdx, 0);
551 sqlite3VdbeAddOp2(v, OP_Close, oldIdx, 0);
danielk1977c3f9bad2002-05-15 08:30:12 +0000552 }
553
drh1bee3d72001-10-15 00:44:35 +0000554 /*
danielk1977e7de6f22004-11-05 06:02:06 +0000555 ** Return the number of rows that were changed. If this routine is
556 ** generating code because of a call to sqlite3NestedParse(), do not
557 ** invoke the callback function.
drh1bee3d72001-10-15 00:44:35 +0000558 */
danielk1977e7de6f22004-11-05 06:02:06 +0000559 if( db->flags & SQLITE_CountRows && !pParse->trigStack && pParse->nested==0 ){
drh04adf412008-01-08 18:57:50 +0000560 sqlite3VdbeAddOp2(v, OP_ResultRow, regRowCount, 1);
danielk197722322fd2004-05-25 23:35:17 +0000561 sqlite3VdbeSetNumCols(v, 1);
drh66a51672008-01-03 00:01:23 +0000562 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "rows updated", P4_STATIC);
drh1bee3d72001-10-15 00:44:35 +0000563 }
564
drhcce7d172000-05-31 15:34:51 +0000565update_cleanup:
danielk19774adee202004-05-08 08:23:19 +0000566 sqlite3AuthContextPop(&sContext);
drhaa9b8962008-01-08 02:57:55 +0000567 sqlite3_free(aRegIdx);
drh17435752007-08-16 04:30:38 +0000568 sqlite3_free(aXRef);
danielk19774adee202004-05-08 08:23:19 +0000569 sqlite3SrcListDelete(pTabList);
570 sqlite3ExprListDelete(pChanges);
571 sqlite3ExprDelete(pWhere);
drhcce7d172000-05-31 15:34:51 +0000572 return;
573}
drh9c419382006-06-16 21:13:21 +0000574
575#ifndef SQLITE_OMIT_VIRTUALTABLE
576/*
577** Generate code for an UPDATE of a virtual table.
578**
579** The strategy is that we create an ephemerial table that contains
580** for each row to be changed:
581**
582** (A) The original rowid of that row.
583** (B) The revised rowid for the row. (note1)
584** (C) The content of every column in the row.
585**
586** Then we loop over this ephemeral table and for each row in
587** the ephermeral table call VUpdate.
588**
589** When finished, drop the ephemeral table.
590**
591** (note1) Actually, if we know in advance that (A) is always the same
592** as (B) we only store (A), then duplicate (A) when pulling
593** it out of the ephemeral table before calling VUpdate.
594*/
595static void updateVirtualTable(
596 Parse *pParse, /* The parsing context */
597 SrcList *pSrc, /* The virtual table to be modified */
598 Table *pTab, /* The virtual table */
599 ExprList *pChanges, /* The columns to change in the UPDATE statement */
600 Expr *pRowid, /* Expression used to recompute the rowid */
601 int *aXRef, /* Mapping from columns of pTab to entries in pChanges */
602 Expr *pWhere /* WHERE clause of the UPDATE statement */
603){
604 Vdbe *v = pParse->pVdbe; /* Virtual machine under construction */
605 ExprList *pEList = 0; /* The result set of the SELECT statement */
606 Select *pSelect = 0; /* The SELECT statement */
607 Expr *pExpr; /* Temporary expression */
608 int ephemTab; /* Table holding the result of the SELECT */
609 int i; /* Loop counter */
610 int addr; /* Address of top of loop */
danielk1977287fb612008-01-04 19:10:28 +0000611 int iReg; /* First register in set passed to OP_VUpdate */
drh17435752007-08-16 04:30:38 +0000612 sqlite3 *db = pParse->db; /* Database connection */
danielk1977287fb612008-01-04 19:10:28 +0000613 const char *pVtab = (const char*)pTab->pVtab;
drh1013c932008-01-06 00:25:21 +0000614 SelectDest dest;
drh9c419382006-06-16 21:13:21 +0000615
616 /* Construct the SELECT statement that will find the new values for
617 ** all updated rows.
618 */
drh17435752007-08-16 04:30:38 +0000619 pEList = sqlite3ExprListAppend(pParse, 0,
620 sqlite3CreateIdExpr(pParse, "_rowid_"), 0);
drh9c419382006-06-16 21:13:21 +0000621 if( pRowid ){
drh17435752007-08-16 04:30:38 +0000622 pEList = sqlite3ExprListAppend(pParse, pEList,
623 sqlite3ExprDup(db, pRowid), 0);
drh9c419382006-06-16 21:13:21 +0000624 }
danielk197765fd59f2006-06-24 11:51:33 +0000625 assert( pTab->iPKey<0 );
drh9c419382006-06-16 21:13:21 +0000626 for(i=0; i<pTab->nCol; i++){
danielk197765fd59f2006-06-24 11:51:33 +0000627 if( aXRef[i]>=0 ){
drh17435752007-08-16 04:30:38 +0000628 pExpr = sqlite3ExprDup(db, pChanges->a[aXRef[i]].pExpr);
drh9c419382006-06-16 21:13:21 +0000629 }else{
drh17435752007-08-16 04:30:38 +0000630 pExpr = sqlite3CreateIdExpr(pParse, pTab->aCol[i].zName);
drh9c419382006-06-16 21:13:21 +0000631 }
drh17435752007-08-16 04:30:38 +0000632 pEList = sqlite3ExprListAppend(pParse, pEList, pExpr, 0);
drh9c419382006-06-16 21:13:21 +0000633 }
drh17435752007-08-16 04:30:38 +0000634 pSelect = sqlite3SelectNew(pParse, pEList, pSrc, pWhere, 0, 0, 0, 0, 0, 0);
drh9c419382006-06-16 21:13:21 +0000635
636 /* Create the ephemeral table into which the update results will
637 ** be stored.
638 */
639 assert( v );
640 ephemTab = pParse->nTab++;
drh66a51672008-01-03 00:01:23 +0000641 sqlite3VdbeAddOp2(v, OP_OpenEphemeral, ephemTab, pTab->nCol+1+(pRowid!=0));
drh9c419382006-06-16 21:13:21 +0000642
643 /* fill the ephemeral table
644 */
drh1013c932008-01-06 00:25:21 +0000645 sqlite3SelectDestInit(&dest, SRT_Table, ephemTab);
danielk19776c8c8ce2008-01-02 16:27:09 +0000646 sqlite3Select(pParse, pSelect, &dest, 0, 0, 0, 0);
drh9c419382006-06-16 21:13:21 +0000647
danielk1977287fb612008-01-04 19:10:28 +0000648 /* Generate code to scan the ephemeral table and call VUpdate. */
649 iReg = ++pParse->nMem;
650 pParse->nMem += pTab->nCol+1;
drh66a51672008-01-03 00:01:23 +0000651 sqlite3VdbeAddOp2(v, OP_Rewind, ephemTab, 0);
drh9c419382006-06-16 21:13:21 +0000652 addr = sqlite3VdbeCurrentAddr(v);
danielk1977287fb612008-01-04 19:10:28 +0000653 sqlite3VdbeAddOp3(v, OP_Column, ephemTab, 0, iReg);
654 sqlite3VdbeAddOp3(v, OP_Column, ephemTab, (pRowid?1:0), iReg+1);
drh9c419382006-06-16 21:13:21 +0000655 for(i=0; i<pTab->nCol; i++){
danielk1977287fb612008-01-04 19:10:28 +0000656 sqlite3VdbeAddOp3(v, OP_Column, ephemTab, i+1+(pRowid!=0), iReg+2+i);
drh9c419382006-06-16 21:13:21 +0000657 }
danielk1977c69cdfd2006-06-17 09:39:55 +0000658 pParse->pVirtualLock = pTab;
danielk1977287fb612008-01-04 19:10:28 +0000659 sqlite3VdbeAddOp4(v, OP_VUpdate, 0, pTab->nCol+2, iReg, pVtab, P4_VTAB);
drh66a51672008-01-03 00:01:23 +0000660 sqlite3VdbeAddOp2(v, OP_Next, ephemTab, addr);
danielk197707fd0592007-02-21 17:04:04 +0000661 sqlite3VdbeJumpHere(v, addr-1);
drh66a51672008-01-03 00:01:23 +0000662 sqlite3VdbeAddOp2(v, OP_Close, ephemTab, 0);
drh9c419382006-06-16 21:13:21 +0000663
664 /* Cleanup */
665 sqlite3SelectDelete(pSelect);
666}
667#endif /* SQLITE_OMIT_VIRTUALTABLE */