drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 1 | /* |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 2 | ** 2001 September 15 |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 3 | ** |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 4 | ** The author disclaims copyright to this source code. In place of |
| 5 | ** a legal notice, here is a blessing: |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 6 | ** |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 7 | ** May you do good and not evil. |
| 8 | ** May you find forgiveness for yourself and forgive others. |
| 9 | ** May you share freely, never taking more than you give. |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 10 | ** |
| 11 | ************************************************************************* |
| 12 | ** This file contains C code routines that are called by the parser |
| 13 | ** to handle UPDATE statements. |
| 14 | ** |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame^] | 15 | ** $Id: update.c,v 1.115 2006/01/05 11:34:34 danielk1977 Exp $ |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 16 | */ |
| 17 | #include "sqliteInt.h" |
| 18 | |
| 19 | /* |
drh | 8a51256 | 2005-11-14 22:29:05 +0000 | [diff] [blame] | 20 | ** The most recently coded instruction was an OP_Column to retrieve the |
| 21 | ** i-th column of table pTab. This routine sets the P3 parameter of the |
danielk1977 | aee18ef | 2005-03-09 12:26:50 +0000 | [diff] [blame] | 22 | ** OP_Column to the default value, if any. |
| 23 | ** |
| 24 | ** The default value of a column is specified by a DEFAULT clause in the |
| 25 | ** column definition. This was either supplied by the user when the table |
| 26 | ** was created, or added later to the table definition by an ALTER TABLE |
| 27 | ** command. If the latter, then the row-records in the table btree on disk |
| 28 | ** may not contain a value for the column and the default value, taken |
| 29 | ** from the P3 parameter of the OP_Column instruction, is returned instead. |
| 30 | ** If the former, then all row-records are guaranteed to include a value |
| 31 | ** for the column and the P3 value is not required. |
| 32 | ** |
| 33 | ** Column definitions created by an ALTER TABLE command may only have |
| 34 | ** literal default values specified: a number, null or a string. (If a more |
| 35 | ** complicated default expression value was provided, it is evaluated |
| 36 | ** when the ALTER TABLE is executed and one of the literal values written |
| 37 | ** into the sqlite_master table.) |
| 38 | ** |
| 39 | ** Therefore, the P3 parameter is only required if the default value for |
| 40 | ** the column is a literal number, string or null. The sqlite3ValueFromExpr() |
| 41 | ** function is capable of transforming these types of expressions into |
| 42 | ** sqlite3_value objects. |
| 43 | */ |
| 44 | void sqlite3ColumnDefault(Vdbe *v, Table *pTab, int i){ |
| 45 | if( pTab && !pTab->pSelect ){ |
| 46 | sqlite3_value *pValue; |
| 47 | u8 enc = sqlite3VdbeDb(v)->enc; |
| 48 | Column *pCol = &pTab->aCol[i]; |
| 49 | sqlite3ValueFromExpr(pCol->pDflt, enc, pCol->affinity, &pValue); |
drh | 29dda4a | 2005-07-21 18:23:20 +0000 | [diff] [blame] | 50 | if( pValue ){ |
| 51 | sqlite3VdbeChangeP3(v, -1, (const char *)pValue, P3_MEM); |
| 52 | }else{ |
| 53 | VdbeComment((v, "# %s.%s", pTab->zName, pCol->zName)); |
| 54 | } |
danielk1977 | aee18ef | 2005-03-09 12:26:50 +0000 | [diff] [blame] | 55 | } |
| 56 | } |
| 57 | |
| 58 | /* |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 59 | ** Process an UPDATE statement. |
drh | b2fe7d8 | 2003-04-20 17:29:23 +0000 | [diff] [blame] | 60 | ** |
| 61 | ** UPDATE OR IGNORE table_wxyz SET a=b, c=d WHERE e<5 AND f NOT NULL; |
| 62 | ** \_______/ \________/ \______/ \________________/ |
| 63 | * onError pTabList pChanges pWhere |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 64 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 65 | void sqlite3Update( |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 66 | Parse *pParse, /* The parser context */ |
drh | 113088e | 2003-03-20 01:16:58 +0000 | [diff] [blame] | 67 | SrcList *pTabList, /* The table in which we should change things */ |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 68 | ExprList *pChanges, /* Things to be changed */ |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 69 | Expr *pWhere, /* The WHERE clause. May be null */ |
| 70 | int onError /* How to handle constraint errors */ |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 71 | ){ |
| 72 | int i, j; /* Loop counters */ |
| 73 | Table *pTab; /* The table to be updated */ |
danielk1977 | 742f947 | 2004-06-16 12:02:43 +0000 | [diff] [blame] | 74 | int addr = 0; /* VDBE instruction address of the start of the loop */ |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 75 | WhereInfo *pWInfo; /* Information about the WHERE clause */ |
| 76 | Vdbe *v; /* The virtual database engine */ |
| 77 | Index *pIdx; /* For looping over indices */ |
| 78 | int nIdx; /* Number of indices that need updating */ |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 79 | int nIdxTotal; /* Total number of indices */ |
drh | 6a3ea0e | 2003-05-02 14:32:12 +0000 | [diff] [blame] | 80 | int iCur; /* VDBE Cursor number of pTab */ |
drh | 9bb575f | 2004-09-06 17:24:11 +0000 | [diff] [blame] | 81 | sqlite3 *db; /* The database structure */ |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 82 | Index **apIdx = 0; /* An array of indices that need updating too */ |
drh | b2fe7d8 | 2003-04-20 17:29:23 +0000 | [diff] [blame] | 83 | char *aIdxUsed = 0; /* aIdxUsed[i]==1 if the i-th index is used */ |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 84 | int *aXRef = 0; /* aXRef[i] is the index in pChanges->a[] of the |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 85 | ** an expression for the i-th column of the table. |
| 86 | ** aXRef[i]==-1 if the i-th column is not changed. */ |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 87 | int chngRowid; /* True if the record number is being changed */ |
| 88 | Expr *pRowidExpr = 0; /* Expression defining the new record number */ |
danielk1977 | 742f947 | 2004-06-16 12:02:43 +0000 | [diff] [blame] | 89 | int openAll = 0; /* True if all indices need to be opened */ |
drh | 85e2096 | 2003-04-25 17:52:11 +0000 | [diff] [blame] | 90 | AuthContext sContext; /* The authorization context */ |
danielk1977 | b3bce66 | 2005-01-29 08:32:43 +0000 | [diff] [blame] | 91 | NameContext sNC; /* The name-context to resolve expressions in */ |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame^] | 92 | int iDb; /* Database containing the table being updated */ |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 93 | |
drh | 798da52 | 2004-11-04 04:42:28 +0000 | [diff] [blame] | 94 | #ifndef SQLITE_OMIT_TRIGGER |
| 95 | int isView; /* Trying to update a view */ |
drh | dca7684 | 2004-12-07 14:06:13 +0000 | [diff] [blame] | 96 | int triggers_exist = 0; /* True if any row triggers exist */ |
drh | 798da52 | 2004-11-04 04:42:28 +0000 | [diff] [blame] | 97 | #endif |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 98 | |
| 99 | int newIdx = -1; /* index of trigger "new" temp table */ |
| 100 | int oldIdx = -1; /* index of trigger "old" temp table */ |
| 101 | |
drh | 85e2096 | 2003-04-25 17:52:11 +0000 | [diff] [blame] | 102 | sContext.pParse = 0; |
danielk1977 | 261919c | 2005-12-06 12:52:59 +0000 | [diff] [blame] | 103 | if( pParse->nErr || sqlite3Tsd()->mallocFailed ) goto update_cleanup; |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 104 | db = pParse->db; |
drh | 113088e | 2003-03-20 01:16:58 +0000 | [diff] [blame] | 105 | assert( pTabList->nSrc==1 ); |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 106 | |
drh | b2fe7d8 | 2003-04-20 17:29:23 +0000 | [diff] [blame] | 107 | /* Locate the table which we want to update. |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 108 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 109 | pTab = sqlite3SrcListLookup(pParse, pTabList); |
drh | a69d916 | 2003-04-17 22:57:53 +0000 | [diff] [blame] | 110 | if( pTab==0 ) goto update_cleanup; |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame^] | 111 | iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema); |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 112 | |
| 113 | /* Figure out if we have any triggers and if the table being |
| 114 | ** updated is a view |
| 115 | */ |
| 116 | #ifndef SQLITE_OMIT_TRIGGER |
drh | dca7684 | 2004-12-07 14:06:13 +0000 | [diff] [blame] | 117 | triggers_exist = sqlite3TriggersExist(pParse, pTab, TK_UPDATE, pChanges); |
drh | 5cf590c | 2003-04-24 01:45:04 +0000 | [diff] [blame] | 118 | isView = pTab->pSelect!=0; |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 119 | #else |
drh | dca7684 | 2004-12-07 14:06:13 +0000 | [diff] [blame] | 120 | # define triggers_exist 0 |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 121 | # define isView 0 |
| 122 | #endif |
| 123 | #ifdef SQLITE_OMIT_VIEW |
| 124 | # undef isView |
| 125 | # define isView 0 |
| 126 | #endif |
| 127 | |
drh | dca7684 | 2004-12-07 14:06:13 +0000 | [diff] [blame] | 128 | if( sqlite3IsReadOnly(pParse, pTab, triggers_exist) ){ |
drh | 5cf590c | 2003-04-24 01:45:04 +0000 | [diff] [blame] | 129 | goto update_cleanup; |
drh | a69d916 | 2003-04-17 22:57:53 +0000 | [diff] [blame] | 130 | } |
drh | 85e2096 | 2003-04-25 17:52:11 +0000 | [diff] [blame] | 131 | if( isView ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 132 | if( sqlite3ViewGetColumnNames(pParse, pTab) ){ |
drh | 85e2096 | 2003-04-25 17:52:11 +0000 | [diff] [blame] | 133 | goto update_cleanup; |
| 134 | } |
drh | 5cf590c | 2003-04-24 01:45:04 +0000 | [diff] [blame] | 135 | } |
drh | eafe05b | 2004-06-13 00:54:01 +0000 | [diff] [blame] | 136 | aXRef = sqliteMallocRaw( sizeof(int) * pTab->nCol ); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 137 | if( aXRef==0 ) goto update_cleanup; |
| 138 | for(i=0; i<pTab->nCol; i++) aXRef[i] = -1; |
| 139 | |
drh | b2fe7d8 | 2003-04-20 17:29:23 +0000 | [diff] [blame] | 140 | /* If there are FOR EACH ROW triggers, allocate cursors for the |
| 141 | ** special OLD and NEW tables |
| 142 | */ |
drh | dca7684 | 2004-12-07 14:06:13 +0000 | [diff] [blame] | 143 | if( triggers_exist ){ |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 144 | newIdx = pParse->nTab++; |
| 145 | oldIdx = pParse->nTab++; |
| 146 | } |
| 147 | |
drh | 53e3fc7 | 2002-07-16 17:22:50 +0000 | [diff] [blame] | 148 | /* Allocate a cursors for the main database table and for all indices. |
| 149 | ** The index cursors might not be used, but if they are used they |
| 150 | ** need to occur right after the database cursor. So go ahead and |
| 151 | ** allocate enough space, just in case. |
| 152 | */ |
drh | 6a3ea0e | 2003-05-02 14:32:12 +0000 | [diff] [blame] | 153 | pTabList->a[0].iCursor = iCur = pParse->nTab++; |
drh | 53e3fc7 | 2002-07-16 17:22:50 +0000 | [diff] [blame] | 154 | for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){ |
| 155 | pParse->nTab++; |
| 156 | } |
| 157 | |
danielk1977 | b3bce66 | 2005-01-29 08:32:43 +0000 | [diff] [blame] | 158 | /* Initialize the name-context */ |
| 159 | memset(&sNC, 0, sizeof(sNC)); |
| 160 | sNC.pParse = pParse; |
| 161 | sNC.pSrcList = pTabList; |
| 162 | |
drh | 85e2096 | 2003-04-25 17:52:11 +0000 | [diff] [blame] | 163 | /* Resolve the column names in all the expressions of the |
| 164 | ** of the UPDATE statement. Also find the column index |
drh | ed6c867 | 2003-01-12 18:02:16 +0000 | [diff] [blame] | 165 | ** for each column to be updated in the pChanges array. For each |
| 166 | ** column to be updated, make sure we have authorization to change |
| 167 | ** that column. |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 168 | */ |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 169 | chngRowid = 0; |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 170 | for(i=0; i<pChanges->nExpr; i++){ |
danielk1977 | b3bce66 | 2005-01-29 08:32:43 +0000 | [diff] [blame] | 171 | if( sqlite3ExprResolveNames(&sNC, pChanges->a[i].pExpr) ){ |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 172 | goto update_cleanup; |
| 173 | } |
| 174 | for(j=0; j<pTab->nCol; j++){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 175 | if( sqlite3StrICmp(pTab->aCol[j].zName, pChanges->a[i].zName)==0 ){ |
drh | 9647ff8 | 2002-01-14 02:56:24 +0000 | [diff] [blame] | 176 | if( j==pTab->iPKey ){ |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 177 | chngRowid = 1; |
| 178 | pRowidExpr = pChanges->a[i].pExpr; |
drh | 4a32431 | 2001-12-21 14:30:42 +0000 | [diff] [blame] | 179 | } |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 180 | aXRef[j] = i; |
| 181 | break; |
| 182 | } |
| 183 | } |
| 184 | if( j>=pTab->nCol ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 185 | if( sqlite3IsRowid(pChanges->a[i].zName) ){ |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 186 | chngRowid = 1; |
| 187 | pRowidExpr = pChanges->a[i].pExpr; |
drh | a0217ba | 2003-06-01 01:10:33 +0000 | [diff] [blame] | 188 | }else{ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 189 | sqlite3ErrorMsg(pParse, "no such column: %s", pChanges->a[i].zName); |
drh | a0217ba | 2003-06-01 01:10:33 +0000 | [diff] [blame] | 190 | goto update_cleanup; |
| 191 | } |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 192 | } |
drh | ed6c867 | 2003-01-12 18:02:16 +0000 | [diff] [blame] | 193 | #ifndef SQLITE_OMIT_AUTHORIZATION |
drh | e5f9c64 | 2003-01-13 23:27:31 +0000 | [diff] [blame] | 194 | { |
| 195 | int rc; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 196 | rc = sqlite3AuthCheck(pParse, SQLITE_UPDATE, pTab->zName, |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame^] | 197 | pTab->aCol[j].zName, db->aDb[iDb].zName); |
drh | e5f9c64 | 2003-01-13 23:27:31 +0000 | [diff] [blame] | 198 | if( rc==SQLITE_DENY ){ |
| 199 | goto update_cleanup; |
| 200 | }else if( rc==SQLITE_IGNORE ){ |
| 201 | aXRef[j] = -1; |
| 202 | } |
drh | ed6c867 | 2003-01-12 18:02:16 +0000 | [diff] [blame] | 203 | } |
| 204 | #endif |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 205 | } |
| 206 | |
drh | 5a2c2c2 | 2001-11-21 02:21:11 +0000 | [diff] [blame] | 207 | /* Allocate memory for the array apIdx[] and fill it with pointers to every |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 208 | ** index that needs to be updated. Indices only need updating if their |
drh | c839258 | 2001-12-31 02:48:51 +0000 | [diff] [blame] | 209 | ** key includes one of the columns named in pChanges or if the record |
| 210 | ** number of the original table entry is changing. |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 211 | */ |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 212 | for(nIdx=nIdxTotal=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, nIdxTotal++){ |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 213 | if( chngRowid ){ |
drh | 4a32431 | 2001-12-21 14:30:42 +0000 | [diff] [blame] | 214 | i = 0; |
| 215 | }else { |
| 216 | for(i=0; i<pIdx->nColumn; i++){ |
| 217 | if( aXRef[pIdx->aiColumn[i]]>=0 ) break; |
| 218 | } |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 219 | } |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 220 | if( i<pIdx->nColumn ) nIdx++; |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 221 | } |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 222 | if( nIdxTotal>0 ){ |
drh | eafe05b | 2004-06-13 00:54:01 +0000 | [diff] [blame] | 223 | apIdx = sqliteMallocRaw( sizeof(Index*) * nIdx + nIdxTotal ); |
drh | b072950 | 2001-03-14 12:35:57 +0000 | [diff] [blame] | 224 | if( apIdx==0 ) goto update_cleanup; |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 225 | aIdxUsed = (char*)&apIdx[nIdx]; |
drh | b072950 | 2001-03-14 12:35:57 +0000 | [diff] [blame] | 226 | } |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 227 | for(nIdx=j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){ |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 228 | if( chngRowid ){ |
drh | 4a32431 | 2001-12-21 14:30:42 +0000 | [diff] [blame] | 229 | i = 0; |
| 230 | }else{ |
| 231 | for(i=0; i<pIdx->nColumn; i++){ |
| 232 | if( aXRef[pIdx->aiColumn[i]]>=0 ) break; |
| 233 | } |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 234 | } |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 235 | if( i<pIdx->nColumn ){ |
danielk1977 | 7cedc8d | 2004-06-10 10:50:08 +0000 | [diff] [blame] | 236 | if( sqlite3CheckIndexCollSeq(pParse, pIdx) ) goto update_cleanup; |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 237 | apIdx[nIdx++] = pIdx; |
| 238 | aIdxUsed[j] = 1; |
| 239 | }else{ |
| 240 | aIdxUsed[j] = 0; |
| 241 | } |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 242 | } |
| 243 | |
drh | 85e2096 | 2003-04-25 17:52:11 +0000 | [diff] [blame] | 244 | /* Resolve the column names in all the expressions in the |
| 245 | ** WHERE clause. |
| 246 | */ |
danielk1977 | b3bce66 | 2005-01-29 08:32:43 +0000 | [diff] [blame] | 247 | if( sqlite3ExprResolveNames(&sNC, pWhere) ){ |
drh | 290c194 | 2004-08-21 17:54:45 +0000 | [diff] [blame] | 248 | goto update_cleanup; |
drh | 85e2096 | 2003-04-25 17:52:11 +0000 | [diff] [blame] | 249 | } |
| 250 | |
| 251 | /* Start the view context |
| 252 | */ |
| 253 | if( isView ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 254 | sqlite3AuthContextPush(pParse, &sContext, pTab->zName); |
drh | 85e2096 | 2003-04-25 17:52:11 +0000 | [diff] [blame] | 255 | } |
| 256 | |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 257 | /* Begin generating code. |
| 258 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 259 | v = sqlite3GetVdbe(pParse); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 260 | if( v==0 ) goto update_cleanup; |
drh | 4794f73 | 2004-11-05 17:17:50 +0000 | [diff] [blame] | 261 | if( pParse->nested==0 ) sqlite3VdbeCountChanges(v); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame^] | 262 | sqlite3BeginWriteOperation(pParse, 1, iDb); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 263 | |
drh | 9d2985c | 2005-09-08 01:58:42 +0000 | [diff] [blame] | 264 | /* If we are trying to update a view, realize that view into |
| 265 | ** a ephemeral table. |
drh | 5cf590c | 2003-04-24 01:45:04 +0000 | [diff] [blame] | 266 | */ |
| 267 | if( isView ){ |
drh | 85e2096 | 2003-04-25 17:52:11 +0000 | [diff] [blame] | 268 | Select *pView; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 269 | pView = sqlite3SelectDup(pTab->pSelect); |
drh | 9d2985c | 2005-09-08 01:58:42 +0000 | [diff] [blame] | 270 | sqlite3Select(pParse, pView, SRT_VirtualTab, iCur, 0, 0, 0, 0); |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 271 | sqlite3SelectDelete(pView); |
drh | 5cf590c | 2003-04-24 01:45:04 +0000 | [diff] [blame] | 272 | } |
| 273 | |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 274 | /* Begin the database scan |
| 275 | */ |
drh | f8db1bc | 2005-04-22 02:38:37 +0000 | [diff] [blame] | 276 | pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, 0); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 277 | if( pWInfo==0 ) goto update_cleanup; |
| 278 | |
| 279 | /* Remember the index of every item to be updated. |
| 280 | */ |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 281 | sqlite3VdbeAddOp(v, OP_Rowid, iCur, 0); |
drh | a01f79d | 2005-07-08 13:07:59 +0000 | [diff] [blame] | 282 | sqlite3VdbeAddOp(v, OP_FifoWrite, 0, 0); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 283 | |
| 284 | /* End the database scan loop. |
| 285 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 286 | sqlite3WhereEnd(pWInfo); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 287 | |
drh | 1bee3d7 | 2001-10-15 00:44:35 +0000 | [diff] [blame] | 288 | /* Initialize the count of updated rows |
| 289 | */ |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 290 | if( db->flags & SQLITE_CountRows && !pParse->trigStack ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 291 | sqlite3VdbeAddOp(v, OP_Integer, 0, 0); |
drh | 1bee3d7 | 2001-10-15 00:44:35 +0000 | [diff] [blame] | 292 | } |
| 293 | |
drh | dca7684 | 2004-12-07 14:06:13 +0000 | [diff] [blame] | 294 | if( triggers_exist ){ |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 295 | /* Create pseudo-tables for NEW and OLD |
| 296 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 297 | sqlite3VdbeAddOp(v, OP_OpenPseudo, oldIdx, 0); |
danielk1977 | 84ac9d0 | 2004-05-18 09:58:06 +0000 | [diff] [blame] | 298 | sqlite3VdbeAddOp(v, OP_SetNumColumns, oldIdx, pTab->nCol); |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 299 | sqlite3VdbeAddOp(v, OP_OpenPseudo, newIdx, 0); |
danielk1977 | 84ac9d0 | 2004-05-18 09:58:06 +0000 | [diff] [blame] | 300 | sqlite3VdbeAddOp(v, OP_SetNumColumns, newIdx, pTab->nCol); |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 301 | |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 302 | /* The top of the update loop for when there are triggers. |
| 303 | */ |
drh | a01f79d | 2005-07-08 13:07:59 +0000 | [diff] [blame] | 304 | addr = sqlite3VdbeAddOp(v, OP_FifoRead, 0, 0); |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 305 | |
drh | 5cf590c | 2003-04-24 01:45:04 +0000 | [diff] [blame] | 306 | if( !isView ){ |
kwel | d536273 | 2005-04-08 16:08:36 +0000 | [diff] [blame] | 307 | sqlite3VdbeAddOp(v, OP_Dup, 0, 0); |
| 308 | sqlite3VdbeAddOp(v, OP_Dup, 0, 0); |
| 309 | /* Open a cursor and make it point to the record that is |
| 310 | ** being updated. |
| 311 | */ |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame^] | 312 | sqlite3OpenTableForReading(v, iCur, iDb, pTab); |
drh | 5cf590c | 2003-04-24 01:45:04 +0000 | [diff] [blame] | 313 | } |
drh | 7cf6e4d | 2004-05-19 14:56:55 +0000 | [diff] [blame] | 314 | sqlite3VdbeAddOp(v, OP_MoveGe, iCur, 0); |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 315 | |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 316 | /* Generate the OLD table |
| 317 | */ |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 318 | sqlite3VdbeAddOp(v, OP_Rowid, iCur, 0); |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 319 | sqlite3VdbeAddOp(v, OP_RowData, iCur, 0); |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 320 | sqlite3VdbeAddOp(v, OP_Insert, oldIdx, 0); |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 321 | |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 322 | /* Generate the NEW table |
| 323 | */ |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 324 | if( chngRowid ){ |
| 325 | sqlite3ExprCodeAndCache(pParse, pRowidExpr); |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 326 | }else{ |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 327 | sqlite3VdbeAddOp(v, OP_Rowid, iCur, 0); |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 328 | } |
drh | 2530378 | 2004-12-07 15:41:48 +0000 | [diff] [blame] | 329 | for(i=0; i<pTab->nCol; i++){ |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 330 | if( i==pTab->iPKey ){ |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 331 | sqlite3VdbeAddOp(v, OP_Null, 0, 0); |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 332 | continue; |
| 333 | } |
| 334 | j = aXRef[i]; |
| 335 | if( j<0 ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 336 | sqlite3VdbeAddOp(v, OP_Column, iCur, i); |
danielk1977 | aee18ef | 2005-03-09 12:26:50 +0000 | [diff] [blame] | 337 | sqlite3ColumnDefault(v, pTab, i); |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 338 | }else{ |
drh | 2530378 | 2004-12-07 15:41:48 +0000 | [diff] [blame] | 339 | sqlite3ExprCodeAndCache(pParse, pChanges->a[j].pExpr); |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 340 | } |
| 341 | } |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 342 | sqlite3VdbeAddOp(v, OP_MakeRecord, pTab->nCol, 0); |
danielk1977 | a37cdde | 2004-05-16 11:15:36 +0000 | [diff] [blame] | 343 | if( !isView ){ |
| 344 | sqlite3TableAffinityStr(v, pTab); |
| 345 | } |
| 346 | if( pParse->nErr ) goto update_cleanup; |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 347 | sqlite3VdbeAddOp(v, OP_Insert, newIdx, 0); |
drh | 5cf590c | 2003-04-24 01:45:04 +0000 | [diff] [blame] | 348 | if( !isView ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 349 | sqlite3VdbeAddOp(v, OP_Close, iCur, 0); |
drh | 5cf590c | 2003-04-24 01:45:04 +0000 | [diff] [blame] | 350 | } |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 351 | |
drh | 5cf590c | 2003-04-24 01:45:04 +0000 | [diff] [blame] | 352 | /* Fire the BEFORE and INSTEAD OF triggers |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 353 | */ |
drh | dca7684 | 2004-12-07 14:06:13 +0000 | [diff] [blame] | 354 | if( sqlite3CodeRowTrigger(pParse, TK_UPDATE, pChanges, TRIGGER_BEFORE, pTab, |
danielk1977 | 6f34903 | 2002-06-11 02:25:40 +0000 | [diff] [blame] | 355 | newIdx, oldIdx, onError, addr) ){ |
danielk1977 | f29ce55 | 2002-05-19 23:43:12 +0000 | [diff] [blame] | 356 | goto update_cleanup; |
| 357 | } |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 358 | } |
| 359 | |
drh | 5cf590c | 2003-04-24 01:45:04 +0000 | [diff] [blame] | 360 | if( !isView ){ |
| 361 | /* |
| 362 | ** Open every index that needs updating. Note that if any |
| 363 | ** index could potentially invoke a REPLACE conflict resolution |
| 364 | ** action, then we need to open all indices because we might need |
| 365 | ** to be deleting some records. |
| 366 | */ |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame^] | 367 | sqlite3VdbeAddOp(v, OP_Integer, iDb, 0); |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 368 | sqlite3VdbeAddOp(v, OP_OpenWrite, iCur, pTab->tnum); |
danielk1977 | b4964b7 | 2004-05-18 01:23:38 +0000 | [diff] [blame] | 369 | sqlite3VdbeAddOp(v, OP_SetNumColumns, iCur, pTab->nCol); |
drh | 5cf590c | 2003-04-24 01:45:04 +0000 | [diff] [blame] | 370 | if( onError==OE_Replace ){ |
| 371 | openAll = 1; |
| 372 | }else{ |
| 373 | openAll = 0; |
| 374 | for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){ |
| 375 | if( pIdx->onError==OE_Replace ){ |
| 376 | openAll = 1; |
| 377 | break; |
| 378 | } |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 379 | } |
| 380 | } |
drh | 5cf590c | 2003-04-24 01:45:04 +0000 | [diff] [blame] | 381 | for(i=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){ |
| 382 | if( openAll || aIdxUsed[i] ){ |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame^] | 383 | sqlite3VdbeAddOp(v, OP_Integer, iDb, 0); |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 384 | sqlite3VdbeOp3(v, OP_OpenWrite, iCur+i+1, pIdx->tnum, |
| 385 | (char*)&pIdx->keyInfo, P3_KEYINFO); |
drh | 6a3ea0e | 2003-05-02 14:32:12 +0000 | [diff] [blame] | 386 | assert( pParse->nTab>iCur+i+1 ); |
drh | 5cf590c | 2003-04-24 01:45:04 +0000 | [diff] [blame] | 387 | } |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 388 | } |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 389 | |
drh | 5cf590c | 2003-04-24 01:45:04 +0000 | [diff] [blame] | 390 | /* Loop over every record that needs updating. We have to load |
| 391 | ** the old data for each record to be updated because some columns |
| 392 | ** might not change and we will need to copy the old value. |
| 393 | ** Also, the old data is needed to delete the old index entires. |
| 394 | ** So make the cursor point at the old record. |
| 395 | */ |
drh | dca7684 | 2004-12-07 14:06:13 +0000 | [diff] [blame] | 396 | if( !triggers_exist ){ |
drh | a01f79d | 2005-07-08 13:07:59 +0000 | [diff] [blame] | 397 | addr = sqlite3VdbeAddOp(v, OP_FifoRead, 0, 0); |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 398 | sqlite3VdbeAddOp(v, OP_Dup, 0, 0); |
drh | 4a32431 | 2001-12-21 14:30:42 +0000 | [diff] [blame] | 399 | } |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 400 | sqlite3VdbeAddOp(v, OP_NotExists, iCur, addr); |
drh | 5cf590c | 2003-04-24 01:45:04 +0000 | [diff] [blame] | 401 | |
| 402 | /* If the record number will change, push the record number as it |
| 403 | ** will be after the update. (The old record number is currently |
| 404 | ** on top of the stack.) |
| 405 | */ |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 406 | if( chngRowid ){ |
| 407 | sqlite3ExprCode(pParse, pRowidExpr); |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 408 | sqlite3VdbeAddOp(v, OP_MustBeInt, 0, 0); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 409 | } |
drh | 5cf590c | 2003-04-24 01:45:04 +0000 | [diff] [blame] | 410 | |
| 411 | /* Compute new data for this record. |
| 412 | */ |
| 413 | for(i=0; i<pTab->nCol; i++){ |
| 414 | if( i==pTab->iPKey ){ |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 415 | sqlite3VdbeAddOp(v, OP_Null, 0, 0); |
drh | 5cf590c | 2003-04-24 01:45:04 +0000 | [diff] [blame] | 416 | continue; |
| 417 | } |
| 418 | j = aXRef[i]; |
| 419 | if( j<0 ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 420 | sqlite3VdbeAddOp(v, OP_Column, iCur, i); |
danielk1977 | aee18ef | 2005-03-09 12:26:50 +0000 | [diff] [blame] | 421 | sqlite3ColumnDefault(v, pTab, i); |
drh | 5cf590c | 2003-04-24 01:45:04 +0000 | [diff] [blame] | 422 | }else{ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 423 | sqlite3ExprCode(pParse, pChanges->a[j].pExpr); |
drh | 5cf590c | 2003-04-24 01:45:04 +0000 | [diff] [blame] | 424 | } |
| 425 | } |
| 426 | |
| 427 | /* Do constraint checks |
| 428 | */ |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 429 | sqlite3GenerateConstraintChecks(pParse, pTab, iCur, aIdxUsed, chngRowid, 1, |
drh | 5cf590c | 2003-04-24 01:45:04 +0000 | [diff] [blame] | 430 | onError, addr); |
| 431 | |
| 432 | /* Delete the old indices for the current record. |
| 433 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 434 | sqlite3GenerateRowIndexDelete(db, v, pTab, iCur, aIdxUsed); |
drh | 5cf590c | 2003-04-24 01:45:04 +0000 | [diff] [blame] | 435 | |
| 436 | /* If changing the record number, delete the old record. |
| 437 | */ |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 438 | if( chngRowid ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 439 | sqlite3VdbeAddOp(v, OP_Delete, iCur, 0); |
drh | 5cf590c | 2003-04-24 01:45:04 +0000 | [diff] [blame] | 440 | } |
| 441 | |
| 442 | /* Create the new index entries and the new record. |
| 443 | */ |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 444 | sqlite3CompleteInsertion(pParse, pTab, iCur, aIdxUsed, chngRowid, 1, -1); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 445 | } |
| 446 | |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 447 | /* Increment the row counter |
drh | 1bee3d7 | 2001-10-15 00:44:35 +0000 | [diff] [blame] | 448 | */ |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 449 | if( db->flags & SQLITE_CountRows && !pParse->trigStack){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 450 | sqlite3VdbeAddOp(v, OP_AddImm, 1, 0); |
drh | 1bee3d7 | 2001-10-15 00:44:35 +0000 | [diff] [blame] | 451 | } |
| 452 | |
drh | 5cf590c | 2003-04-24 01:45:04 +0000 | [diff] [blame] | 453 | /* If there are triggers, close all the cursors after each iteration |
| 454 | ** through the loop. The fire the after triggers. |
| 455 | */ |
drh | dca7684 | 2004-12-07 14:06:13 +0000 | [diff] [blame] | 456 | if( triggers_exist ){ |
drh | 5cf590c | 2003-04-24 01:45:04 +0000 | [diff] [blame] | 457 | if( !isView ){ |
| 458 | for(i=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){ |
| 459 | if( openAll || aIdxUsed[i] ) |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 460 | sqlite3VdbeAddOp(v, OP_Close, iCur+i+1, 0); |
drh | 5cf590c | 2003-04-24 01:45:04 +0000 | [diff] [blame] | 461 | } |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 462 | sqlite3VdbeAddOp(v, OP_Close, iCur, 0); |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 463 | } |
drh | dca7684 | 2004-12-07 14:06:13 +0000 | [diff] [blame] | 464 | if( sqlite3CodeRowTrigger(pParse, TK_UPDATE, pChanges, TRIGGER_AFTER, pTab, |
danielk1977 | 6f34903 | 2002-06-11 02:25:40 +0000 | [diff] [blame] | 465 | newIdx, oldIdx, onError, addr) ){ |
danielk1977 | f29ce55 | 2002-05-19 23:43:12 +0000 | [diff] [blame] | 466 | goto update_cleanup; |
| 467 | } |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 468 | } |
| 469 | |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 470 | /* Repeat the above with the next record to be updated, until |
| 471 | ** all record selected by the WHERE clause have been updated. |
| 472 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 473 | sqlite3VdbeAddOp(v, OP_Goto, 0, addr); |
drh | d654be8 | 2005-09-20 17:42:23 +0000 | [diff] [blame] | 474 | sqlite3VdbeJumpHere(v, addr); |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 475 | |
| 476 | /* Close all tables if there were no FOR EACH ROW triggers */ |
drh | dca7684 | 2004-12-07 14:06:13 +0000 | [diff] [blame] | 477 | if( !triggers_exist ){ |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 478 | for(i=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){ |
| 479 | if( openAll || aIdxUsed[i] ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 480 | sqlite3VdbeAddOp(v, OP_Close, iCur+i+1, 0); |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 481 | } |
| 482 | } |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 483 | sqlite3VdbeAddOp(v, OP_Close, iCur, 0); |
danielk1977 | f29ce55 | 2002-05-19 23:43:12 +0000 | [diff] [blame] | 484 | }else{ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 485 | sqlite3VdbeAddOp(v, OP_Close, newIdx, 0); |
| 486 | sqlite3VdbeAddOp(v, OP_Close, oldIdx, 0); |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 487 | } |
| 488 | |
drh | 1bee3d7 | 2001-10-15 00:44:35 +0000 | [diff] [blame] | 489 | /* |
danielk1977 | e7de6f2 | 2004-11-05 06:02:06 +0000 | [diff] [blame] | 490 | ** Return the number of rows that were changed. If this routine is |
| 491 | ** generating code because of a call to sqlite3NestedParse(), do not |
| 492 | ** invoke the callback function. |
drh | 1bee3d7 | 2001-10-15 00:44:35 +0000 | [diff] [blame] | 493 | */ |
danielk1977 | e7de6f2 | 2004-11-05 06:02:06 +0000 | [diff] [blame] | 494 | if( db->flags & SQLITE_CountRows && !pParse->trigStack && pParse->nested==0 ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 495 | sqlite3VdbeAddOp(v, OP_Callback, 1, 0); |
danielk1977 | 22322fd | 2004-05-25 23:35:17 +0000 | [diff] [blame] | 496 | sqlite3VdbeSetNumCols(v, 1); |
danielk1977 | 3cf8606 | 2004-05-26 10:11:05 +0000 | [diff] [blame] | 497 | sqlite3VdbeSetColName(v, 0, "rows updated", P3_STATIC); |
drh | 1bee3d7 | 2001-10-15 00:44:35 +0000 | [diff] [blame] | 498 | } |
| 499 | |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 500 | update_cleanup: |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 501 | sqlite3AuthContextPop(&sContext); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 502 | sqliteFree(apIdx); |
| 503 | sqliteFree(aXRef); |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 504 | sqlite3SrcListDelete(pTabList); |
| 505 | sqlite3ExprListDelete(pChanges); |
| 506 | sqlite3ExprDelete(pWhere); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 507 | return; |
| 508 | } |