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