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