blob: 22d6b7a029b4cc12aad9f08ea3a98398958dbdec [file] [log] [blame]
drhcce7d172000-05-31 15:34:51 +00001/*
drhb19a2bc2001-09-16 00:13:26 +00002** 2001 September 15
drhcce7d172000-05-31 15:34:51 +00003**
drhb19a2bc2001-09-16 00:13:26 +00004** The author disclaims copyright to this source code. In place of
5** a legal notice, here is a blessing:
drhcce7d172000-05-31 15:34:51 +00006**
drhb19a2bc2001-09-16 00:13:26 +00007** 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.
drhcce7d172000-05-31 15:34:51 +000010**
11*************************************************************************
12** This file contains C code routines that are called by the parser
13** to handle UPDATE statements.
14**
danielk1977b3d24bf2006-06-19 03:05:10 +000015** $Id: update.c,v 1.131 2006/06/19 03:05:10 danielk1977 Exp $
drhcce7d172000-05-31 15:34:51 +000016*/
17#include "sqliteInt.h"
18
drh9c419382006-06-16 21:13:21 +000019/* Forward declaration */
20static void updateVirtualTable(
21 Parse *pParse, /* The parsing context */
22 SrcList *pSrc, /* The virtual table to be modified */
23 Table *pTab, /* The virtual table */
24 ExprList *pChanges, /* The columns to change in the UPDATE statement */
25 Expr *pRowidExpr, /* Expression used to recompute the rowid */
26 int *aXRef, /* Mapping from columns of pTab to entries in pChanges */
27 Expr *pWhere /* WHERE clause of the UPDATE statement */
28);
29
drhcce7d172000-05-31 15:34:51 +000030/*
drh8a512562005-11-14 22:29:05 +000031** The most recently coded instruction was an OP_Column to retrieve the
32** i-th column of table pTab. This routine sets the P3 parameter of the
danielk1977aee18ef2005-03-09 12:26:50 +000033** OP_Column to the default value, if any.
34**
35** The default value of a column is specified by a DEFAULT clause in the
36** column definition. This was either supplied by the user when the table
37** was created, or added later to the table definition by an ALTER TABLE
38** command. If the latter, then the row-records in the table btree on disk
39** may not contain a value for the column and the default value, taken
40** from the P3 parameter of the OP_Column instruction, is returned instead.
41** If the former, then all row-records are guaranteed to include a value
42** for the column and the P3 value is not required.
43**
44** Column definitions created by an ALTER TABLE command may only have
45** literal default values specified: a number, null or a string. (If a more
46** complicated default expression value was provided, it is evaluated
47** when the ALTER TABLE is executed and one of the literal values written
48** into the sqlite_master table.)
49**
50** Therefore, the P3 parameter is only required if the default value for
51** the column is a literal number, string or null. The sqlite3ValueFromExpr()
52** function is capable of transforming these types of expressions into
53** sqlite3_value objects.
54*/
55void sqlite3ColumnDefault(Vdbe *v, Table *pTab, int i){
56 if( pTab && !pTab->pSelect ){
57 sqlite3_value *pValue;
danielk197714db2662006-01-09 16:12:04 +000058 u8 enc = ENC(sqlite3VdbeDb(v));
danielk1977aee18ef2005-03-09 12:26:50 +000059 Column *pCol = &pTab->aCol[i];
60 sqlite3ValueFromExpr(pCol->pDflt, enc, pCol->affinity, &pValue);
drh29dda4a2005-07-21 18:23:20 +000061 if( pValue ){
62 sqlite3VdbeChangeP3(v, -1, (const char *)pValue, P3_MEM);
63 }else{
64 VdbeComment((v, "# %s.%s", pTab->zName, pCol->zName));
65 }
danielk1977aee18ef2005-03-09 12:26:50 +000066 }
67}
68
69/*
drhcce7d172000-05-31 15:34:51 +000070** Process an UPDATE statement.
drhb2fe7d82003-04-20 17:29:23 +000071**
72** UPDATE OR IGNORE table_wxyz SET a=b, c=d WHERE e<5 AND f NOT NULL;
73** \_______/ \________/ \______/ \________________/
74* onError pTabList pChanges pWhere
drhcce7d172000-05-31 15:34:51 +000075*/
danielk19774adee202004-05-08 08:23:19 +000076void sqlite3Update(
drhcce7d172000-05-31 15:34:51 +000077 Parse *pParse, /* The parser context */
drh113088e2003-03-20 01:16:58 +000078 SrcList *pTabList, /* The table in which we should change things */
drhcce7d172000-05-31 15:34:51 +000079 ExprList *pChanges, /* Things to be changed */
drh9cfcf5d2002-01-29 18:41:24 +000080 Expr *pWhere, /* The WHERE clause. May be null */
81 int onError /* How to handle constraint errors */
drhcce7d172000-05-31 15:34:51 +000082){
83 int i, j; /* Loop counters */
84 Table *pTab; /* The table to be updated */
danielk1977742f9472004-06-16 12:02:43 +000085 int addr = 0; /* VDBE instruction address of the start of the loop */
drhcce7d172000-05-31 15:34:51 +000086 WhereInfo *pWInfo; /* Information about the WHERE clause */
87 Vdbe *v; /* The virtual database engine */
88 Index *pIdx; /* For looping over indices */
89 int nIdx; /* Number of indices that need updating */
drh0ca3e242002-01-29 23:07:02 +000090 int nIdxTotal; /* Total number of indices */
drh6a3ea0e2003-05-02 14:32:12 +000091 int iCur; /* VDBE Cursor number of pTab */
drh9bb575f2004-09-06 17:24:11 +000092 sqlite3 *db; /* The database structure */
drhcce7d172000-05-31 15:34:51 +000093 Index **apIdx = 0; /* An array of indices that need updating too */
drhb2fe7d82003-04-20 17:29:23 +000094 char *aIdxUsed = 0; /* aIdxUsed[i]==1 if the i-th index is used */
drhcce7d172000-05-31 15:34:51 +000095 int *aXRef = 0; /* aXRef[i] is the index in pChanges->a[] of the
drh967e8b72000-06-21 13:59:10 +000096 ** an expression for the i-th column of the table.
97 ** aXRef[i]==-1 if the i-th column is not changed. */
drhf0863fe2005-06-12 21:35:51 +000098 int chngRowid; /* True if the record number is being changed */
99 Expr *pRowidExpr = 0; /* Expression defining the new record number */
danielk1977742f9472004-06-16 12:02:43 +0000100 int openAll = 0; /* True if all indices need to be opened */
drh85e20962003-04-25 17:52:11 +0000101 AuthContext sContext; /* The authorization context */
danielk1977b3bce662005-01-29 08:32:43 +0000102 NameContext sNC; /* The name-context to resolve expressions in */
danielk1977da184232006-01-05 11:34:32 +0000103 int iDb; /* Database containing the table being updated */
drhcce7d172000-05-31 15:34:51 +0000104
drh798da522004-11-04 04:42:28 +0000105#ifndef SQLITE_OMIT_TRIGGER
106 int isView; /* Trying to update a view */
drhdca76842004-12-07 14:06:13 +0000107 int triggers_exist = 0; /* True if any row triggers exist */
drh798da522004-11-04 04:42:28 +0000108#endif
danielk1977c3f9bad2002-05-15 08:30:12 +0000109
110 int newIdx = -1; /* index of trigger "new" temp table */
111 int oldIdx = -1; /* index of trigger "old" temp table */
112
drh85e20962003-04-25 17:52:11 +0000113 sContext.pParse = 0;
danielk19779e128002006-01-18 16:51:35 +0000114 if( pParse->nErr || sqlite3MallocFailed() ){
drh6f7adc82006-01-11 21:41:20 +0000115 goto update_cleanup;
116 }
drhecdc7532001-09-23 02:35:53 +0000117 db = pParse->db;
drh113088e2003-03-20 01:16:58 +0000118 assert( pTabList->nSrc==1 );
drhdaffd0e2001-04-11 14:28:42 +0000119
drhb2fe7d82003-04-20 17:29:23 +0000120 /* Locate the table which we want to update.
drhcce7d172000-05-31 15:34:51 +0000121 */
danielk19774adee202004-05-08 08:23:19 +0000122 pTab = sqlite3SrcListLookup(pParse, pTabList);
drha69d9162003-04-17 22:57:53 +0000123 if( pTab==0 ) goto update_cleanup;
danielk1977da184232006-01-05 11:34:32 +0000124 iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
drhb7f91642004-10-31 02:22:47 +0000125
126 /* Figure out if we have any triggers and if the table being
127 ** updated is a view
128 */
129#ifndef SQLITE_OMIT_TRIGGER
drhdca76842004-12-07 14:06:13 +0000130 triggers_exist = sqlite3TriggersExist(pParse, pTab, TK_UPDATE, pChanges);
drh5cf590c2003-04-24 01:45:04 +0000131 isView = pTab->pSelect!=0;
drhb7f91642004-10-31 02:22:47 +0000132#else
drhdca76842004-12-07 14:06:13 +0000133# define triggers_exist 0
drhb7f91642004-10-31 02:22:47 +0000134# define isView 0
135#endif
136#ifdef SQLITE_OMIT_VIEW
137# undef isView
138# define isView 0
139#endif
140
drhdca76842004-12-07 14:06:13 +0000141 if( sqlite3IsReadOnly(pParse, pTab, triggers_exist) ){
drh5cf590c2003-04-24 01:45:04 +0000142 goto update_cleanup;
drha69d9162003-04-17 22:57:53 +0000143 }
danielk1977b3d24bf2006-06-19 03:05:10 +0000144 if( sqlite3ViewGetColumnNames(pParse, pTab) ){
145 goto update_cleanup;
drh5cf590c2003-04-24 01:45:04 +0000146 }
drheafe05b2004-06-13 00:54:01 +0000147 aXRef = sqliteMallocRaw( sizeof(int) * pTab->nCol );
drhcce7d172000-05-31 15:34:51 +0000148 if( aXRef==0 ) goto update_cleanup;
149 for(i=0; i<pTab->nCol; i++) aXRef[i] = -1;
150
drhb2fe7d82003-04-20 17:29:23 +0000151 /* If there are FOR EACH ROW triggers, allocate cursors for the
152 ** special OLD and NEW tables
153 */
drhdca76842004-12-07 14:06:13 +0000154 if( triggers_exist ){
danielk1977c3f9bad2002-05-15 08:30:12 +0000155 newIdx = pParse->nTab++;
156 oldIdx = pParse->nTab++;
157 }
158
drh53e3fc72002-07-16 17:22:50 +0000159 /* Allocate a cursors for the main database table and for all indices.
160 ** The index cursors might not be used, but if they are used they
161 ** need to occur right after the database cursor. So go ahead and
162 ** allocate enough space, just in case.
163 */
drh6a3ea0e2003-05-02 14:32:12 +0000164 pTabList->a[0].iCursor = iCur = pParse->nTab++;
drh53e3fc72002-07-16 17:22:50 +0000165 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
166 pParse->nTab++;
167 }
168
danielk1977b3bce662005-01-29 08:32:43 +0000169 /* Initialize the name-context */
170 memset(&sNC, 0, sizeof(sNC));
171 sNC.pParse = pParse;
172 sNC.pSrcList = pTabList;
173
drh85e20962003-04-25 17:52:11 +0000174 /* Resolve the column names in all the expressions of the
175 ** of the UPDATE statement. Also find the column index
drhed6c8672003-01-12 18:02:16 +0000176 ** for each column to be updated in the pChanges array. For each
177 ** column to be updated, make sure we have authorization to change
178 ** that column.
drhcce7d172000-05-31 15:34:51 +0000179 */
drhf0863fe2005-06-12 21:35:51 +0000180 chngRowid = 0;
drhcce7d172000-05-31 15:34:51 +0000181 for(i=0; i<pChanges->nExpr; i++){
danielk1977b3bce662005-01-29 08:32:43 +0000182 if( sqlite3ExprResolveNames(&sNC, pChanges->a[i].pExpr) ){
drhcce7d172000-05-31 15:34:51 +0000183 goto update_cleanup;
184 }
185 for(j=0; j<pTab->nCol; j++){
danielk19774adee202004-05-08 08:23:19 +0000186 if( sqlite3StrICmp(pTab->aCol[j].zName, pChanges->a[i].zName)==0 ){
drh9647ff82002-01-14 02:56:24 +0000187 if( j==pTab->iPKey ){
drhf0863fe2005-06-12 21:35:51 +0000188 chngRowid = 1;
189 pRowidExpr = pChanges->a[i].pExpr;
drh4a324312001-12-21 14:30:42 +0000190 }
drhcce7d172000-05-31 15:34:51 +0000191 aXRef[j] = i;
192 break;
193 }
194 }
195 if( j>=pTab->nCol ){
danielk19774adee202004-05-08 08:23:19 +0000196 if( sqlite3IsRowid(pChanges->a[i].zName) ){
drhf0863fe2005-06-12 21:35:51 +0000197 chngRowid = 1;
198 pRowidExpr = pChanges->a[i].pExpr;
drha0217ba2003-06-01 01:10:33 +0000199 }else{
danielk19774adee202004-05-08 08:23:19 +0000200 sqlite3ErrorMsg(pParse, "no such column: %s", pChanges->a[i].zName);
drha0217ba2003-06-01 01:10:33 +0000201 goto update_cleanup;
202 }
drhcce7d172000-05-31 15:34:51 +0000203 }
drhed6c8672003-01-12 18:02:16 +0000204#ifndef SQLITE_OMIT_AUTHORIZATION
drhe5f9c642003-01-13 23:27:31 +0000205 {
206 int rc;
danielk19774adee202004-05-08 08:23:19 +0000207 rc = sqlite3AuthCheck(pParse, SQLITE_UPDATE, pTab->zName,
danielk1977da184232006-01-05 11:34:32 +0000208 pTab->aCol[j].zName, db->aDb[iDb].zName);
drhe5f9c642003-01-13 23:27:31 +0000209 if( rc==SQLITE_DENY ){
210 goto update_cleanup;
211 }else if( rc==SQLITE_IGNORE ){
212 aXRef[j] = -1;
213 }
drhed6c8672003-01-12 18:02:16 +0000214 }
215#endif
drhcce7d172000-05-31 15:34:51 +0000216 }
217
drh5a2c2c22001-11-21 02:21:11 +0000218 /* Allocate memory for the array apIdx[] and fill it with pointers to every
drhcce7d172000-05-31 15:34:51 +0000219 ** index that needs to be updated. Indices only need updating if their
drhc8392582001-12-31 02:48:51 +0000220 ** key includes one of the columns named in pChanges or if the record
221 ** number of the original table entry is changing.
drhcce7d172000-05-31 15:34:51 +0000222 */
drh0ca3e242002-01-29 23:07:02 +0000223 for(nIdx=nIdxTotal=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, nIdxTotal++){
drhf0863fe2005-06-12 21:35:51 +0000224 if( chngRowid ){
drh4a324312001-12-21 14:30:42 +0000225 i = 0;
226 }else {
227 for(i=0; i<pIdx->nColumn; i++){
228 if( aXRef[pIdx->aiColumn[i]]>=0 ) break;
229 }
drhcce7d172000-05-31 15:34:51 +0000230 }
drh967e8b72000-06-21 13:59:10 +0000231 if( i<pIdx->nColumn ) nIdx++;
drhcce7d172000-05-31 15:34:51 +0000232 }
drh0ca3e242002-01-29 23:07:02 +0000233 if( nIdxTotal>0 ){
drheafe05b2004-06-13 00:54:01 +0000234 apIdx = sqliteMallocRaw( sizeof(Index*) * nIdx + nIdxTotal );
drhb0729502001-03-14 12:35:57 +0000235 if( apIdx==0 ) goto update_cleanup;
drh0ca3e242002-01-29 23:07:02 +0000236 aIdxUsed = (char*)&apIdx[nIdx];
drhb0729502001-03-14 12:35:57 +0000237 }
drh0ca3e242002-01-29 23:07:02 +0000238 for(nIdx=j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
drhf0863fe2005-06-12 21:35:51 +0000239 if( chngRowid ){
drh4a324312001-12-21 14:30:42 +0000240 i = 0;
241 }else{
242 for(i=0; i<pIdx->nColumn; i++){
243 if( aXRef[pIdx->aiColumn[i]]>=0 ) break;
244 }
drhcce7d172000-05-31 15:34:51 +0000245 }
drh0ca3e242002-01-29 23:07:02 +0000246 if( i<pIdx->nColumn ){
247 apIdx[nIdx++] = pIdx;
248 aIdxUsed[j] = 1;
249 }else{
250 aIdxUsed[j] = 0;
251 }
drhcce7d172000-05-31 15:34:51 +0000252 }
253
254 /* Begin generating code.
255 */
danielk19774adee202004-05-08 08:23:19 +0000256 v = sqlite3GetVdbe(pParse);
drhcce7d172000-05-31 15:34:51 +0000257 if( v==0 ) goto update_cleanup;
drh4794f732004-11-05 17:17:50 +0000258 if( pParse->nested==0 ) sqlite3VdbeCountChanges(v);
danielk1977da184232006-01-05 11:34:32 +0000259 sqlite3BeginWriteOperation(pParse, 1, iDb);
drhcce7d172000-05-31 15:34:51 +0000260
drh9c419382006-06-16 21:13:21 +0000261#ifndef SQLITE_OMIT_VIRTUALTABLE
262 /* Virtual tables must be handled separately */
263 if( IsVirtual(pTab) ){
264 updateVirtualTable(pParse, pTabList, pTab, pChanges, pRowidExpr, aXRef,
265 pWhere);
266 pWhere = 0;
267 pTabList = 0;
268 goto update_cleanup;
269 }
270#endif
271
272 /* Resolve the column names in all the expressions in the
273 ** WHERE clause.
274 */
275 if( sqlite3ExprResolveNames(&sNC, pWhere) ){
276 goto update_cleanup;
277 }
278
danielk19774273dea2006-06-17 06:31:18 +0000279 /* Start the view context
280 */
281 if( isView ){
282 sqlite3AuthContextPush(pParse, &sContext, pTab->zName);
283 }
284
drh9d2985c2005-09-08 01:58:42 +0000285 /* If we are trying to update a view, realize that view into
286 ** a ephemeral table.
drh5cf590c2003-04-24 01:45:04 +0000287 */
288 if( isView ){
drh85e20962003-04-25 17:52:11 +0000289 Select *pView;
danielk19774adee202004-05-08 08:23:19 +0000290 pView = sqlite3SelectDup(pTab->pSelect);
drhb9bb7c12006-06-11 23:41:55 +0000291 sqlite3Select(pParse, pView, SRT_EphemTab, iCur, 0, 0, 0, 0);
danielk19774adee202004-05-08 08:23:19 +0000292 sqlite3SelectDelete(pView);
drh5cf590c2003-04-24 01:45:04 +0000293 }
294
drhcce7d172000-05-31 15:34:51 +0000295 /* Begin the database scan
296 */
drhf8db1bc2005-04-22 02:38:37 +0000297 pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, 0);
drhcce7d172000-05-31 15:34:51 +0000298 if( pWInfo==0 ) goto update_cleanup;
299
drh4cbdda92006-06-14 19:00:20 +0000300 /* Remember the rowid of every item to be updated.
drhcce7d172000-05-31 15:34:51 +0000301 */
drh4cbdda92006-06-14 19:00:20 +0000302 sqlite3VdbeAddOp(v, IsVirtual(pTab) ? OP_VRowid : OP_Rowid, iCur, 0);
drha01f79d2005-07-08 13:07:59 +0000303 sqlite3VdbeAddOp(v, OP_FifoWrite, 0, 0);
drhcce7d172000-05-31 15:34:51 +0000304
305 /* End the database scan loop.
306 */
danielk19774adee202004-05-08 08:23:19 +0000307 sqlite3WhereEnd(pWInfo);
drhcce7d172000-05-31 15:34:51 +0000308
drh1bee3d72001-10-15 00:44:35 +0000309 /* Initialize the count of updated rows
310 */
danielk1977c3f9bad2002-05-15 08:30:12 +0000311 if( db->flags & SQLITE_CountRows && !pParse->trigStack ){
danielk19774adee202004-05-08 08:23:19 +0000312 sqlite3VdbeAddOp(v, OP_Integer, 0, 0);
drh1bee3d72001-10-15 00:44:35 +0000313 }
314
drhdca76842004-12-07 14:06:13 +0000315 if( triggers_exist ){
drh70ce3f02003-04-15 19:22:22 +0000316 /* Create pseudo-tables for NEW and OLD
317 */
danielk19774adee202004-05-08 08:23:19 +0000318 sqlite3VdbeAddOp(v, OP_OpenPseudo, oldIdx, 0);
danielk197784ac9d02004-05-18 09:58:06 +0000319 sqlite3VdbeAddOp(v, OP_SetNumColumns, oldIdx, pTab->nCol);
danielk19774adee202004-05-08 08:23:19 +0000320 sqlite3VdbeAddOp(v, OP_OpenPseudo, newIdx, 0);
danielk197784ac9d02004-05-18 09:58:06 +0000321 sqlite3VdbeAddOp(v, OP_SetNumColumns, newIdx, pTab->nCol);
danielk1977c3f9bad2002-05-15 08:30:12 +0000322
drh70ce3f02003-04-15 19:22:22 +0000323 /* The top of the update loop for when there are triggers.
324 */
drha01f79d2005-07-08 13:07:59 +0000325 addr = sqlite3VdbeAddOp(v, OP_FifoRead, 0, 0);
danielk1977c3f9bad2002-05-15 08:30:12 +0000326
drh5cf590c2003-04-24 01:45:04 +0000327 if( !isView ){
kweld5362732005-04-08 16:08:36 +0000328 sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
329 sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
330 /* Open a cursor and make it point to the record that is
331 ** being updated.
332 */
danielk1977c00da102006-01-07 13:21:04 +0000333 sqlite3OpenTable(pParse, iCur, iDb, pTab, OP_OpenRead);
drh5cf590c2003-04-24 01:45:04 +0000334 }
drh7cf6e4d2004-05-19 14:56:55 +0000335 sqlite3VdbeAddOp(v, OP_MoveGe, iCur, 0);
danielk1977c3f9bad2002-05-15 08:30:12 +0000336
drh70ce3f02003-04-15 19:22:22 +0000337 /* Generate the OLD table
338 */
drhf0863fe2005-06-12 21:35:51 +0000339 sqlite3VdbeAddOp(v, OP_Rowid, iCur, 0);
danielk19774adee202004-05-08 08:23:19 +0000340 sqlite3VdbeAddOp(v, OP_RowData, iCur, 0);
drhf0863fe2005-06-12 21:35:51 +0000341 sqlite3VdbeAddOp(v, OP_Insert, oldIdx, 0);
danielk1977c3f9bad2002-05-15 08:30:12 +0000342
drh70ce3f02003-04-15 19:22:22 +0000343 /* Generate the NEW table
344 */
drhf0863fe2005-06-12 21:35:51 +0000345 if( chngRowid ){
346 sqlite3ExprCodeAndCache(pParse, pRowidExpr);
drh70ce3f02003-04-15 19:22:22 +0000347 }else{
drhf0863fe2005-06-12 21:35:51 +0000348 sqlite3VdbeAddOp(v, OP_Rowid, iCur, 0);
drh70ce3f02003-04-15 19:22:22 +0000349 }
drh25303782004-12-07 15:41:48 +0000350 for(i=0; i<pTab->nCol; i++){
drh70ce3f02003-04-15 19:22:22 +0000351 if( i==pTab->iPKey ){
drhf0863fe2005-06-12 21:35:51 +0000352 sqlite3VdbeAddOp(v, OP_Null, 0, 0);
drh70ce3f02003-04-15 19:22:22 +0000353 continue;
354 }
355 j = aXRef[i];
356 if( j<0 ){
danielk19774adee202004-05-08 08:23:19 +0000357 sqlite3VdbeAddOp(v, OP_Column, iCur, i);
danielk1977aee18ef2005-03-09 12:26:50 +0000358 sqlite3ColumnDefault(v, pTab, i);
danielk1977c3f9bad2002-05-15 08:30:12 +0000359 }else{
drh25303782004-12-07 15:41:48 +0000360 sqlite3ExprCodeAndCache(pParse, pChanges->a[j].pExpr);
danielk1977c3f9bad2002-05-15 08:30:12 +0000361 }
362 }
danielk19774adee202004-05-08 08:23:19 +0000363 sqlite3VdbeAddOp(v, OP_MakeRecord, pTab->nCol, 0);
danielk1977a37cdde2004-05-16 11:15:36 +0000364 if( !isView ){
365 sqlite3TableAffinityStr(v, pTab);
366 }
367 if( pParse->nErr ) goto update_cleanup;
drhf0863fe2005-06-12 21:35:51 +0000368 sqlite3VdbeAddOp(v, OP_Insert, newIdx, 0);
drh5cf590c2003-04-24 01:45:04 +0000369 if( !isView ){
danielk19774adee202004-05-08 08:23:19 +0000370 sqlite3VdbeAddOp(v, OP_Close, iCur, 0);
drh5cf590c2003-04-24 01:45:04 +0000371 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000372
drh5cf590c2003-04-24 01:45:04 +0000373 /* Fire the BEFORE and INSTEAD OF triggers
drh70ce3f02003-04-15 19:22:22 +0000374 */
drhdca76842004-12-07 14:06:13 +0000375 if( sqlite3CodeRowTrigger(pParse, TK_UPDATE, pChanges, TRIGGER_BEFORE, pTab,
danielk19776f349032002-06-11 02:25:40 +0000376 newIdx, oldIdx, onError, addr) ){
danielk1977f29ce552002-05-19 23:43:12 +0000377 goto update_cleanup;
378 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000379 }
380
drh4cbdda92006-06-14 19:00:20 +0000381 if( !isView && !IsVirtual(pTab) ){
drh5cf590c2003-04-24 01:45:04 +0000382 /*
383 ** Open every index that needs updating. Note that if any
384 ** index could potentially invoke a REPLACE conflict resolution
385 ** action, then we need to open all indices because we might need
386 ** to be deleting some records.
387 */
danielk1977c00da102006-01-07 13:21:04 +0000388 sqlite3OpenTable(pParse, iCur, iDb, pTab, OP_OpenWrite);
drh5cf590c2003-04-24 01:45:04 +0000389 if( onError==OE_Replace ){
390 openAll = 1;
391 }else{
392 openAll = 0;
393 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
394 if( pIdx->onError==OE_Replace ){
395 openAll = 1;
396 break;
397 }
drh0ca3e242002-01-29 23:07:02 +0000398 }
399 }
drh5cf590c2003-04-24 01:45:04 +0000400 for(i=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){
401 if( openAll || aIdxUsed[i] ){
danielk1977b3bf5562006-01-10 17:58:23 +0000402 KeyInfo *pKey = sqlite3IndexKeyinfo(pParse, pIdx);
danielk1977da184232006-01-05 11:34:32 +0000403 sqlite3VdbeAddOp(v, OP_Integer, iDb, 0);
drhd3d39e92004-05-20 22:16:29 +0000404 sqlite3VdbeOp3(v, OP_OpenWrite, iCur+i+1, pIdx->tnum,
danielk1977b3bf5562006-01-10 17:58:23 +0000405 (char*)pKey, P3_KEYINFO_HANDOFF);
drh6a3ea0e2003-05-02 14:32:12 +0000406 assert( pParse->nTab>iCur+i+1 );
drh5cf590c2003-04-24 01:45:04 +0000407 }
drh0ca3e242002-01-29 23:07:02 +0000408 }
drhcce7d172000-05-31 15:34:51 +0000409
drh5cf590c2003-04-24 01:45:04 +0000410 /* Loop over every record that needs updating. We have to load
411 ** the old data for each record to be updated because some columns
412 ** might not change and we will need to copy the old value.
drh4cbdda92006-06-14 19:00:20 +0000413 ** Also, the old data is needed to delete the old index entries.
drh5cf590c2003-04-24 01:45:04 +0000414 ** So make the cursor point at the old record.
415 */
drhdca76842004-12-07 14:06:13 +0000416 if( !triggers_exist ){
drha01f79d2005-07-08 13:07:59 +0000417 addr = sqlite3VdbeAddOp(v, OP_FifoRead, 0, 0);
danielk19774adee202004-05-08 08:23:19 +0000418 sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
drh4a324312001-12-21 14:30:42 +0000419 }
danielk19774adee202004-05-08 08:23:19 +0000420 sqlite3VdbeAddOp(v, OP_NotExists, iCur, addr);
drh5cf590c2003-04-24 01:45:04 +0000421
422 /* If the record number will change, push the record number as it
423 ** will be after the update. (The old record number is currently
424 ** on top of the stack.)
425 */
drhf0863fe2005-06-12 21:35:51 +0000426 if( chngRowid ){
427 sqlite3ExprCode(pParse, pRowidExpr);
danielk19774adee202004-05-08 08:23:19 +0000428 sqlite3VdbeAddOp(v, OP_MustBeInt, 0, 0);
drhcce7d172000-05-31 15:34:51 +0000429 }
drh5cf590c2003-04-24 01:45:04 +0000430
431 /* Compute new data for this record.
432 */
433 for(i=0; i<pTab->nCol; i++){
434 if( i==pTab->iPKey ){
drhf0863fe2005-06-12 21:35:51 +0000435 sqlite3VdbeAddOp(v, OP_Null, 0, 0);
drh5cf590c2003-04-24 01:45:04 +0000436 continue;
437 }
438 j = aXRef[i];
439 if( j<0 ){
danielk19774adee202004-05-08 08:23:19 +0000440 sqlite3VdbeAddOp(v, OP_Column, iCur, i);
danielk1977aee18ef2005-03-09 12:26:50 +0000441 sqlite3ColumnDefault(v, pTab, i);
drh5cf590c2003-04-24 01:45:04 +0000442 }else{
danielk19774adee202004-05-08 08:23:19 +0000443 sqlite3ExprCode(pParse, pChanges->a[j].pExpr);
drh5cf590c2003-04-24 01:45:04 +0000444 }
445 }
446
447 /* Do constraint checks
448 */
drhf0863fe2005-06-12 21:35:51 +0000449 sqlite3GenerateConstraintChecks(pParse, pTab, iCur, aIdxUsed, chngRowid, 1,
drh5cf590c2003-04-24 01:45:04 +0000450 onError, addr);
451
452 /* Delete the old indices for the current record.
453 */
drh74161702006-02-24 02:53:49 +0000454 sqlite3GenerateRowIndexDelete(v, pTab, iCur, aIdxUsed);
drh5cf590c2003-04-24 01:45:04 +0000455
456 /* If changing the record number, delete the old record.
457 */
drhf0863fe2005-06-12 21:35:51 +0000458 if( chngRowid ){
danielk19774adee202004-05-08 08:23:19 +0000459 sqlite3VdbeAddOp(v, OP_Delete, iCur, 0);
drh5cf590c2003-04-24 01:45:04 +0000460 }
461
462 /* Create the new index entries and the new record.
463 */
drhf0863fe2005-06-12 21:35:51 +0000464 sqlite3CompleteInsertion(pParse, pTab, iCur, aIdxUsed, chngRowid, 1, -1);
drhcce7d172000-05-31 15:34:51 +0000465 }
466
drh0ca3e242002-01-29 23:07:02 +0000467 /* Increment the row counter
drh1bee3d72001-10-15 00:44:35 +0000468 */
danielk1977c3f9bad2002-05-15 08:30:12 +0000469 if( db->flags & SQLITE_CountRows && !pParse->trigStack){
danielk19774adee202004-05-08 08:23:19 +0000470 sqlite3VdbeAddOp(v, OP_AddImm, 1, 0);
drh1bee3d72001-10-15 00:44:35 +0000471 }
472
drh5cf590c2003-04-24 01:45:04 +0000473 /* If there are triggers, close all the cursors after each iteration
474 ** through the loop. The fire the after triggers.
475 */
drhdca76842004-12-07 14:06:13 +0000476 if( triggers_exist ){
drh5cf590c2003-04-24 01:45:04 +0000477 if( !isView ){
478 for(i=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){
479 if( openAll || aIdxUsed[i] )
danielk19774adee202004-05-08 08:23:19 +0000480 sqlite3VdbeAddOp(v, OP_Close, iCur+i+1, 0);
drh5cf590c2003-04-24 01:45:04 +0000481 }
danielk19774adee202004-05-08 08:23:19 +0000482 sqlite3VdbeAddOp(v, OP_Close, iCur, 0);
danielk1977c3f9bad2002-05-15 08:30:12 +0000483 }
drhdca76842004-12-07 14:06:13 +0000484 if( sqlite3CodeRowTrigger(pParse, TK_UPDATE, pChanges, TRIGGER_AFTER, pTab,
danielk19776f349032002-06-11 02:25:40 +0000485 newIdx, oldIdx, onError, addr) ){
danielk1977f29ce552002-05-19 23:43:12 +0000486 goto update_cleanup;
487 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000488 }
489
drhcce7d172000-05-31 15:34:51 +0000490 /* Repeat the above with the next record to be updated, until
491 ** all record selected by the WHERE clause have been updated.
492 */
danielk19774adee202004-05-08 08:23:19 +0000493 sqlite3VdbeAddOp(v, OP_Goto, 0, addr);
drhd654be82005-09-20 17:42:23 +0000494 sqlite3VdbeJumpHere(v, addr);
danielk1977c3f9bad2002-05-15 08:30:12 +0000495
496 /* Close all tables if there were no FOR EACH ROW triggers */
drh9c419382006-06-16 21:13:21 +0000497 if( !triggers_exist ){
danielk1977c3f9bad2002-05-15 08:30:12 +0000498 for(i=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){
499 if( openAll || aIdxUsed[i] ){
danielk19774adee202004-05-08 08:23:19 +0000500 sqlite3VdbeAddOp(v, OP_Close, iCur+i+1, 0);
danielk1977c3f9bad2002-05-15 08:30:12 +0000501 }
502 }
danielk19774adee202004-05-08 08:23:19 +0000503 sqlite3VdbeAddOp(v, OP_Close, iCur, 0);
danielk1977f29ce552002-05-19 23:43:12 +0000504 }else{
danielk19774adee202004-05-08 08:23:19 +0000505 sqlite3VdbeAddOp(v, OP_Close, newIdx, 0);
506 sqlite3VdbeAddOp(v, OP_Close, oldIdx, 0);
danielk1977c3f9bad2002-05-15 08:30:12 +0000507 }
508
drh1bee3d72001-10-15 00:44:35 +0000509 /*
danielk1977e7de6f22004-11-05 06:02:06 +0000510 ** Return the number of rows that were changed. If this routine is
511 ** generating code because of a call to sqlite3NestedParse(), do not
512 ** invoke the callback function.
drh1bee3d72001-10-15 00:44:35 +0000513 */
danielk1977e7de6f22004-11-05 06:02:06 +0000514 if( db->flags & SQLITE_CountRows && !pParse->trigStack && pParse->nested==0 ){
danielk19774adee202004-05-08 08:23:19 +0000515 sqlite3VdbeAddOp(v, OP_Callback, 1, 0);
danielk197722322fd2004-05-25 23:35:17 +0000516 sqlite3VdbeSetNumCols(v, 1);
danielk1977955de522006-02-10 02:27:42 +0000517 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "rows updated", P3_STATIC);
drh1bee3d72001-10-15 00:44:35 +0000518 }
519
drhcce7d172000-05-31 15:34:51 +0000520update_cleanup:
danielk19774adee202004-05-08 08:23:19 +0000521 sqlite3AuthContextPop(&sContext);
drhcce7d172000-05-31 15:34:51 +0000522 sqliteFree(apIdx);
523 sqliteFree(aXRef);
danielk19774adee202004-05-08 08:23:19 +0000524 sqlite3SrcListDelete(pTabList);
525 sqlite3ExprListDelete(pChanges);
526 sqlite3ExprDelete(pWhere);
drhcce7d172000-05-31 15:34:51 +0000527 return;
528}
drh9c419382006-06-16 21:13:21 +0000529
530#ifndef SQLITE_OMIT_VIRTUALTABLE
531/*
532** Generate code for an UPDATE of a virtual table.
533**
534** The strategy is that we create an ephemerial table that contains
535** for each row to be changed:
536**
537** (A) The original rowid of that row.
538** (B) The revised rowid for the row. (note1)
539** (C) The content of every column in the row.
540**
541** Then we loop over this ephemeral table and for each row in
542** the ephermeral table call VUpdate.
543**
544** When finished, drop the ephemeral table.
545**
546** (note1) Actually, if we know in advance that (A) is always the same
547** as (B) we only store (A), then duplicate (A) when pulling
548** it out of the ephemeral table before calling VUpdate.
549*/
550static void updateVirtualTable(
551 Parse *pParse, /* The parsing context */
552 SrcList *pSrc, /* The virtual table to be modified */
553 Table *pTab, /* The virtual table */
554 ExprList *pChanges, /* The columns to change in the UPDATE statement */
555 Expr *pRowid, /* Expression used to recompute the rowid */
556 int *aXRef, /* Mapping from columns of pTab to entries in pChanges */
557 Expr *pWhere /* WHERE clause of the UPDATE statement */
558){
559 Vdbe *v = pParse->pVdbe; /* Virtual machine under construction */
560 ExprList *pEList = 0; /* The result set of the SELECT statement */
561 Select *pSelect = 0; /* The SELECT statement */
562 Expr *pExpr; /* Temporary expression */
563 int ephemTab; /* Table holding the result of the SELECT */
564 int i; /* Loop counter */
565 int addr; /* Address of top of loop */
566
567 /* Construct the SELECT statement that will find the new values for
568 ** all updated rows.
569 */
570 pEList = sqlite3ExprListAppend(0, sqlite3CreateIdExpr("_rowid_"), 0);
571 if( pRowid ){
danielk19772867fef2006-06-17 03:27:21 +0000572 pEList = sqlite3ExprListAppend(pEList, sqlite3ExprDup(pRowid), 0);
drh9c419382006-06-16 21:13:21 +0000573 }
574 for(i=0; i<pTab->nCol; i++){
575 if( i==pTab->iPKey ){
576 pExpr = sqlite3Expr(TK_NULL, 0, 0, 0);
577 }else if( aXRef[i]>=0 ){
578 pExpr = sqlite3ExprDup(pChanges->a[aXRef[i]].pExpr);
579 }else{
580 pExpr = sqlite3CreateIdExpr(pTab->aCol[i].zName);
581 }
582 pEList = sqlite3ExprListAppend(pEList, pExpr, 0);
583 }
584 pSelect = sqlite3SelectNew(pEList, pSrc, pWhere, 0, 0, 0, 0, 0, 0);
585
586 /* Create the ephemeral table into which the update results will
587 ** be stored.
588 */
589 assert( v );
590 ephemTab = pParse->nTab++;
591 sqlite3VdbeAddOp(v, OP_OpenEphemeral, ephemTab, pTab->nCol+1+(pRowid!=0));
592
593 /* fill the ephemeral table
594 */
595 sqlite3Select(pParse, pSelect, SRT_Table, ephemTab, 0, 0, 0, 0);
596
597 /*
598 ** Generate code to scan the ephemeral table and call VDelete and
599 ** VInsert
600 */
601 sqlite3VdbeAddOp(v, OP_Rewind, ephemTab, 0);
602 addr = sqlite3VdbeCurrentAddr(v);
603 sqlite3VdbeAddOp(v, OP_Column, ephemTab, 0);
604 if( pRowid ){
605 sqlite3VdbeAddOp(v, OP_Column, ephemTab, 1);
606 }else{
danielk19772867fef2006-06-17 03:27:21 +0000607 sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
drh9c419382006-06-16 21:13:21 +0000608 }
609 for(i=0; i<pTab->nCol; i++){
610 sqlite3VdbeAddOp(v, OP_Column, ephemTab, i+1+(pRowid!=0));
611 }
danielk1977c69cdfd2006-06-17 09:39:55 +0000612 pParse->pVirtualLock = pTab;
drh9c419382006-06-16 21:13:21 +0000613 sqlite3VdbeOp3(v, OP_VUpdate, 0, pTab->nCol+2,
614 (const char*)pTab->pVtab, P3_VTAB);
615 sqlite3VdbeAddOp(v, OP_Next, ephemTab, addr);
616 sqlite3VdbeAddOp(v, OP_Close, ephemTab, 0);
617
618 /* Cleanup */
619 sqlite3SelectDelete(pSelect);
620}
621#endif /* SQLITE_OMIT_VIRTUALTABLE */