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