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