1 /* 2 ** 2001 September 15 3 ** 4 ** The author disclaims copyright to this source code. In place of 5 ** a legal notice, here is a blessing: 6 ** 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. 10 ** 11 ************************************************************************* 12 ** This file contains C code routines that are called by the parser 13 ** to handle UPDATE statements. 14 ** 15 ** $Id: update.c,v 1.198 2009/04/24 15:46:22 drh Exp $ 16 */ 17 #include "sqliteInt.h" 18 19 #ifndef SQLITE_OMIT_VIRTUALTABLE 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 ); 30 #endif /* SQLITE_OMIT_VIRTUALTABLE */ 31 32 /* 33 ** The most recently coded instruction was an OP_Column to retrieve the 34 ** i-th column of table pTab. This routine sets the P4 parameter of the 35 ** OP_Column to the default value, if any. 36 ** 37 ** The default value of a column is specified by a DEFAULT clause in the 38 ** column definition. This was either supplied by the user when the table 39 ** was created, or added later to the table definition by an ALTER TABLE 40 ** command. If the latter, then the row-records in the table btree on disk 41 ** may not contain a value for the column and the default value, taken 42 ** from the P4 parameter of the OP_Column instruction, is returned instead. 43 ** If the former, then all row-records are guaranteed to include a value 44 ** for the column and the P4 value is not required. 45 ** 46 ** Column definitions created by an ALTER TABLE command may only have 47 ** literal default values specified: a number, null or a string. (If a more 48 ** complicated default expression value was provided, it is evaluated 49 ** when the ALTER TABLE is executed and one of the literal values written 50 ** into the sqlite_master table.) 51 ** 52 ** Therefore, the P4 parameter is only required if the default value for 53 ** the column is a literal number, string or null. The sqlite3ValueFromExpr() 54 ** function is capable of transforming these types of expressions into 55 ** sqlite3_value objects. 56 */ 57 void sqlite3ColumnDefault(Vdbe *v, Table *pTab, int i){ 58 if( pTab && !pTab->pSelect ){ 59 sqlite3_value *pValue; 60 u8 enc = ENC(sqlite3VdbeDb(v)); 61 Column *pCol = &pTab->aCol[i]; 62 VdbeComment((v, "%s.%s", pTab->zName, pCol->zName)); 63 assert( i<pTab->nCol ); 64 sqlite3ValueFromExpr(sqlite3VdbeDb(v), pCol->pDflt, enc, 65 pCol->affinity, &pValue); 66 if( pValue ){ 67 sqlite3VdbeChangeP4(v, -1, (const char *)pValue, P4_MEM); 68 } 69 } 70 } 71 72 /* 73 ** Process an UPDATE statement. 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 78 */ 79 void sqlite3Update( 80 Parse *pParse, /* The parser context */ 81 SrcList *pTabList, /* The table in which we should change things */ 82 ExprList *pChanges, /* Things to be changed */ 83 Expr *pWhere, /* The WHERE clause. May be null */ 84 int onError /* How to handle constraint errors */ 85 ){ 86 int i, j; /* Loop counters */ 87 Table *pTab; /* The table to be updated */ 88 int addr = 0; /* VDBE instruction address of the start of the loop */ 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 */ 93 int iCur; /* VDBE Cursor number of pTab */ 94 sqlite3 *db; /* The database structure */ 95 int *aRegIdx = 0; /* One register assigned to each index to be updated */ 96 int *aXRef = 0; /* aXRef[i] is the index in pChanges->a[] of the 97 ** an expression for the i-th column of the table. 98 ** aXRef[i]==-1 if the i-th column is not changed. */ 99 int chngRowid; /* True if the record number is being changed */ 100 Expr *pRowidExpr = 0; /* Expression defining the new record number */ 101 int openAll = 0; /* True if all indices need to be opened */ 102 AuthContext sContext; /* The authorization context */ 103 NameContext sNC; /* The name-context to resolve expressions in */ 104 int iDb; /* Database containing the table being updated */ 105 int j1; /* Addresses of jump instructions */ 106 int okOnePass; /* True for one-pass algorithm without the FIFO */ 107 108 #ifndef SQLITE_OMIT_TRIGGER 109 int isView; /* Trying to update a view */ 110 Trigger *pTrigger; /* List of triggers on pTab, if required */ 111 #endif 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 */ 116 u32 old_col_mask = 0; /* Mask of OLD.* columns in use */ 117 u32 new_col_mask = 0; /* Mask of NEW.* columns in use */ 118 119 int newIdx = -1; /* index of trigger "new" temp table */ 120 int oldIdx = -1; /* index of trigger "old" temp table */ 121 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 */ 127 int regRowSet = 0; /* Rowset of rows to be updated */ 128 129 sContext.pParse = 0; 130 db = pParse->db; 131 if( pParse->nErr || db->mallocFailed ){ 132 goto update_cleanup; 133 } 134 assert( pTabList->nSrc==1 ); 135 136 /* Locate the table which we want to update. 137 */ 138 pTab = sqlite3SrcListLookup(pParse, pTabList); 139 if( pTab==0 ) goto update_cleanup; 140 iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema); 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 146 pTrigger = sqlite3TriggersExist(pParse, pTab, TK_UPDATE, pChanges, 0); 147 isView = pTab->pSelect!=0; 148 #else 149 # define pTrigger 0 150 # define isView 0 151 #endif 152 #ifdef SQLITE_OMIT_VIEW 153 # undef isView 154 # define isView 0 155 #endif 156 157 if( sqlite3IsReadOnly(pParse, pTab, (pTrigger?1:0)) ){ 158 goto update_cleanup; 159 } 160 if( sqlite3ViewGetColumnNames(pParse, pTab) ){ 161 goto update_cleanup; 162 } 163 aXRef = sqlite3DbMallocRaw(db, sizeof(int) * pTab->nCol ); 164 if( aXRef==0 ) goto update_cleanup; 165 for(i=0; i<pTab->nCol; i++) aXRef[i] = -1; 166 167 /* If there are FOR EACH ROW triggers, allocate cursors for the 168 ** special OLD and NEW tables 169 */ 170 if( pTrigger ){ 171 newIdx = pParse->nTab++; 172 oldIdx = pParse->nTab++; 173 } 174 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 */ 180 pTabList->a[0].iCursor = iCur = pParse->nTab++; 181 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){ 182 pParse->nTab++; 183 } 184 185 /* Initialize the name-context */ 186 memset(&sNC, 0, sizeof(sNC)); 187 sNC.pParse = pParse; 188 sNC.pSrcList = pTabList; 189 190 /* Resolve the column names in all the expressions of the 191 ** of the UPDATE statement. Also find the column index 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. 195 */ 196 chngRowid = 0; 197 for(i=0; i<pChanges->nExpr; i++){ 198 if( sqlite3ResolveExprNames(&sNC, pChanges->a[i].pExpr) ){ 199 goto update_cleanup; 200 } 201 for(j=0; j<pTab->nCol; j++){ 202 if( sqlite3StrICmp(pTab->aCol[j].zName, pChanges->a[i].zName)==0 ){ 203 if( j==pTab->iPKey ){ 204 chngRowid = 1; 205 pRowidExpr = pChanges->a[i].pExpr; 206 } 207 aXRef[j] = i; 208 break; 209 } 210 } 211 if( j>=pTab->nCol ){ 212 if( sqlite3IsRowid(pChanges->a[i].zName) ){ 213 chngRowid = 1; 214 pRowidExpr = pChanges->a[i].pExpr; 215 }else{ 216 sqlite3ErrorMsg(pParse, "no such column: %s", pChanges->a[i].zName); 217 goto update_cleanup; 218 } 219 } 220 #ifndef SQLITE_OMIT_AUTHORIZATION 221 { 222 int rc; 223 rc = sqlite3AuthCheck(pParse, SQLITE_UPDATE, pTab->zName, 224 pTab->aCol[j].zName, db->aDb[iDb].zName); 225 if( rc==SQLITE_DENY ){ 226 goto update_cleanup; 227 }else if( rc==SQLITE_IGNORE ){ 228 aXRef[j] = -1; 229 } 230 } 231 #endif 232 } 233 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. 238 */ 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; 246 if( chngRowid ){ 247 reg = ++pParse->nMem; 248 }else{ 249 reg = 0; 250 for(i=0; i<pIdx->nColumn; i++){ 251 if( aXRef[pIdx->aiColumn[i]]>=0 ){ 252 reg = ++pParse->nMem; 253 break; 254 } 255 } 256 } 257 aRegIdx[j] = reg; 258 } 259 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 276 /* Begin generating code. 277 */ 278 v = sqlite3GetVdbe(pParse); 279 if( v==0 ) goto update_cleanup; 280 if( pParse->nested==0 ) sqlite3VdbeCountChanges(v); 281 sqlite3BeginWriteOperation(pParse, 1, iDb); 282 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 294 /* Start the view context 295 */ 296 if( isView ){ 297 sqlite3AuthContextPush(pParse, &sContext, pTab->zName); 298 } 299 300 /* Generate the code for triggers. 301 */ 302 if( pTrigger ){ 303 int iGoto; 304 305 /* Create pseudo-tables for NEW and OLD 306 */ 307 sqlite3VdbeAddOp3(v, OP_OpenPseudo, oldIdx, 0, pTab->nCol); 308 sqlite3VdbeAddOp3(v, OP_OpenPseudo, newIdx, 0, pTab->nCol); 309 310 iGoto = sqlite3VdbeAddOp2(v, OP_Goto, 0, 0); 311 addr = sqlite3VdbeMakeLabel(v); 312 iBeginBeforeTrigger = sqlite3VdbeCurrentAddr(v); 313 if( sqlite3CodeRowTrigger(pParse, pTrigger, TK_UPDATE, pChanges, 314 TRIGGER_BEFORE, pTab, newIdx, oldIdx, onError, addr, 315 &old_col_mask, &new_col_mask) ){ 316 goto update_cleanup; 317 } 318 iEndBeforeTrigger = sqlite3VdbeAddOp2(v, OP_Goto, 0, 0); 319 iBeginAfterTrigger = sqlite3VdbeCurrentAddr(v); 320 if( sqlite3CodeRowTrigger(pParse, pTrigger, TK_UPDATE, pChanges, 321 TRIGGER_AFTER, pTab, newIdx, oldIdx, onError, addr, 322 &old_col_mask, &new_col_mask) ){ 323 goto update_cleanup; 324 } 325 iEndAfterTrigger = sqlite3VdbeAddOp2(v, OP_Goto, 0, 0); 326 sqlite3VdbeJumpHere(v, iGoto); 327 } 328 329 /* If we are trying to update a view, realize that view into 330 ** a ephemeral table. 331 */ 332 #if !defined(SQLITE_OMIT_VIEW) && !defined(SQLITE_OMIT_TRIGGER) 333 if( isView ){ 334 sqlite3MaterializeView(pParse, pTab, pWhere, iCur); 335 } 336 #endif 337 338 /* Resolve the column names in all the expressions in the 339 ** WHERE clause. 340 */ 341 if( sqlite3ResolveExprNames(&sNC, pWhere) ){ 342 goto update_cleanup; 343 } 344 345 /* Begin the database scan 346 */ 347 sqlite3VdbeAddOp2(v, OP_Null, 0, regOldRowid); 348 pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere,0, WHERE_ONEPASS_DESIRED); 349 if( pWInfo==0 ) goto update_cleanup; 350 okOnePass = pWInfo->okOnePass; 351 352 /* Remember the rowid of every item to be updated. 353 */ 354 sqlite3VdbeAddOp2(v, OP_Rowid, iCur, regOldRowid); 355 if( !okOnePass ){ 356 regRowSet = ++pParse->nMem; 357 sqlite3VdbeAddOp2(v, OP_RowSetAdd, regRowSet, regOldRowid); 358 } 359 360 /* End the database scan loop. 361 */ 362 sqlite3WhereEnd(pWInfo); 363 364 /* Initialize the count of updated rows 365 */ 366 if( db->flags & SQLITE_CountRows && !pParse->trigStack ){ 367 regRowCount = ++pParse->nMem; 368 sqlite3VdbeAddOp2(v, OP_Integer, 0, regRowCount); 369 } 370 371 if( !isView && !IsVirtual(pTab) ){ 372 /* 373 ** Open every index that needs updating. Note that if any 374 ** index could potentially invoke a REPLACE conflict resolution 375 ** action, then we need to open all indices because we might need 376 ** to be deleting some records. 377 */ 378 if( !okOnePass ) sqlite3OpenTable(pParse, iCur, iDb, pTab, OP_OpenWrite); 379 if( onError==OE_Replace ){ 380 openAll = 1; 381 }else{ 382 openAll = 0; 383 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){ 384 if( pIdx->onError==OE_Replace ){ 385 openAll = 1; 386 break; 387 } 388 } 389 } 390 for(i=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){ 391 if( openAll || aRegIdx[i]>0 ){ 392 KeyInfo *pKey = sqlite3IndexKeyinfo(pParse, pIdx); 393 sqlite3VdbeAddOp4(v, OP_OpenWrite, iCur+i+1, pIdx->tnum, iDb, 394 (char*)pKey, P4_KEYINFO_HANDOFF); 395 assert( pParse->nTab>iCur+i+1 ); 396 } 397 } 398 } 399 400 /* Jump back to this point if a trigger encounters an IGNORE constraint. */ 401 if( pTrigger ){ 402 sqlite3VdbeResolveLabel(v, addr); 403 } 404 405 /* Top of the update loop */ 406 if( okOnePass ){ 407 int a1 = sqlite3VdbeAddOp1(v, OP_NotNull, regOldRowid); 408 addr = sqlite3VdbeAddOp0(v, OP_Goto); 409 sqlite3VdbeJumpHere(v, a1); 410 }else{ 411 addr = sqlite3VdbeAddOp3(v, OP_RowSetRead, regRowSet, 0, regOldRowid); 412 } 413 414 if( pTrigger ){ 415 int regRowid; 416 int regRow; 417 int regCols; 418 419 /* Make cursor iCur point to the record that is being updated. 420 */ 421 sqlite3VdbeAddOp3(v, OP_NotExists, iCur, addr, regOldRowid); 422 423 /* Generate the OLD table 424 */ 425 regRowid = sqlite3GetTempReg(pParse); 426 regRow = sqlite3GetTempReg(pParse); 427 sqlite3VdbeAddOp2(v, OP_Rowid, iCur, regRowid); 428 if( !old_col_mask ){ 429 sqlite3VdbeAddOp2(v, OP_Null, 0, regRow); 430 }else{ 431 sqlite3VdbeAddOp2(v, OP_RowData, iCur, regRow); 432 } 433 sqlite3VdbeAddOp3(v, OP_Insert, oldIdx, regRow, regRowid); 434 435 /* Generate the NEW table 436 */ 437 if( chngRowid ){ 438 sqlite3ExprCodeAndCache(pParse, pRowidExpr, regRowid); 439 sqlite3VdbeAddOp1(v, OP_MustBeInt, regRowid); 440 }else{ 441 sqlite3VdbeAddOp2(v, OP_Rowid, iCur, regRowid); 442 } 443 regCols = sqlite3GetTempRange(pParse, pTab->nCol); 444 for(i=0; i<pTab->nCol; i++){ 445 if( i==pTab->iPKey ){ 446 sqlite3VdbeAddOp2(v, OP_Null, 0, regCols+i); 447 continue; 448 } 449 j = aXRef[i]; 450 if( new_col_mask&((u32)1<<i) || new_col_mask==0xffffffff ){ 451 if( j<0 ){ 452 sqlite3VdbeAddOp3(v, OP_Column, iCur, i, regCols+i); 453 sqlite3ColumnDefault(v, pTab, i); 454 }else{ 455 sqlite3ExprCodeAndCache(pParse, pChanges->a[j].pExpr, regCols+i); 456 } 457 }else{ 458 sqlite3VdbeAddOp2(v, OP_Null, 0, regCols+i); 459 } 460 } 461 sqlite3VdbeAddOp3(v, OP_MakeRecord, regCols, pTab->nCol, regRow); 462 if( !isView ){ 463 sqlite3TableAffinityStr(v, pTab); 464 sqlite3ExprCacheAffinityChange(pParse, regCols, pTab->nCol); 465 } 466 sqlite3ReleaseTempRange(pParse, regCols, pTab->nCol); 467 /* if( pParse->nErr ) goto update_cleanup; */ 468 sqlite3VdbeAddOp3(v, OP_Insert, newIdx, regRow, regRowid); 469 sqlite3ReleaseTempReg(pParse, regRowid); 470 sqlite3ReleaseTempReg(pParse, regRow); 471 472 sqlite3VdbeAddOp2(v, OP_Goto, 0, iBeginBeforeTrigger); 473 sqlite3VdbeJumpHere(v, iEndBeforeTrigger); 474 } 475 476 if( !isView && !IsVirtual(pTab) ){ 477 /* Loop over every record that needs updating. We have to load 478 ** the old data for each record to be updated because some columns 479 ** might not change and we will need to copy the old value. 480 ** Also, the old data is needed to delete the old index entries. 481 ** So make the cursor point at the old record. 482 */ 483 sqlite3VdbeAddOp3(v, OP_NotExists, iCur, addr, regOldRowid); 484 485 /* If the record number will change, push the record number as it 486 ** will be after the update. (The old record number is currently 487 ** on top of the stack.) 488 */ 489 if( chngRowid ){ 490 sqlite3ExprCode(pParse, pRowidExpr, regNewRowid); 491 sqlite3VdbeAddOp1(v, OP_MustBeInt, regNewRowid); 492 } 493 494 /* Compute new data for this record. 495 */ 496 for(i=0; i<pTab->nCol; i++){ 497 if( i==pTab->iPKey ){ 498 sqlite3VdbeAddOp2(v, OP_Null, 0, regData+i); 499 continue; 500 } 501 j = aXRef[i]; 502 if( j<0 ){ 503 sqlite3VdbeAddOp3(v, OP_Column, iCur, i, regData+i); 504 sqlite3ColumnDefault(v, pTab, i); 505 }else{ 506 sqlite3ExprCode(pParse, pChanges->a[j].pExpr, regData+i); 507 } 508 } 509 510 /* Do constraint checks 511 */ 512 sqlite3GenerateConstraintChecks(pParse, pTab, iCur, regNewRowid, 513 aRegIdx, chngRowid, 1, 514 onError, addr); 515 516 /* Delete the old indices for the current record. 517 */ 518 j1 = sqlite3VdbeAddOp3(v, OP_NotExists, iCur, 0, regOldRowid); 519 sqlite3GenerateRowIndexDelete(pParse, pTab, iCur, aRegIdx); 520 521 /* If changing the record number, delete the old record. 522 */ 523 if( chngRowid ){ 524 sqlite3VdbeAddOp2(v, OP_Delete, iCur, 0); 525 } 526 sqlite3VdbeJumpHere(v, j1); 527 528 /* Create the new index entries and the new record. 529 */ 530 sqlite3CompleteInsertion(pParse, pTab, iCur, regNewRowid, 531 aRegIdx, 1, -1, 0); 532 } 533 534 /* Increment the row counter 535 */ 536 if( db->flags & SQLITE_CountRows && !pParse->trigStack){ 537 sqlite3VdbeAddOp2(v, OP_AddImm, regRowCount, 1); 538 } 539 540 /* If there are triggers, close all the cursors after each iteration 541 ** through the loop. The fire the after triggers. 542 */ 543 if( pTrigger ){ 544 sqlite3VdbeAddOp2(v, OP_Goto, 0, iBeginAfterTrigger); 545 sqlite3VdbeJumpHere(v, iEndAfterTrigger); 546 } 547 548 /* Repeat the above with the next record to be updated, until 549 ** all record selected by the WHERE clause have been updated. 550 */ 551 sqlite3VdbeAddOp2(v, OP_Goto, 0, addr); 552 sqlite3VdbeJumpHere(v, addr); 553 554 /* Close all tables */ 555 for(i=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){ 556 if( openAll || aRegIdx[i]>0 ){ 557 sqlite3VdbeAddOp2(v, OP_Close, iCur+i+1, 0); 558 } 559 } 560 sqlite3VdbeAddOp2(v, OP_Close, iCur, 0); 561 if( pTrigger ){ 562 sqlite3VdbeAddOp2(v, OP_Close, newIdx, 0); 563 sqlite3VdbeAddOp2(v, OP_Close, oldIdx, 0); 564 } 565 566 /* 567 ** Return the number of rows that were changed. If this routine is 568 ** generating code because of a call to sqlite3NestedParse(), do not 569 ** invoke the callback function. 570 */ 571 if( db->flags & SQLITE_CountRows && !pParse->trigStack && pParse->nested==0 ){ 572 sqlite3VdbeAddOp2(v, OP_ResultRow, regRowCount, 1); 573 sqlite3VdbeSetNumCols(v, 1); 574 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "rows updated", SQLITE_STATIC); 575 } 576 577 update_cleanup: 578 sqlite3AuthContextPop(&sContext); 579 sqlite3DbFree(db, aRegIdx); 580 sqlite3DbFree(db, aXRef); 581 sqlite3SrcListDelete(db, pTabList); 582 sqlite3ExprListDelete(db, pChanges); 583 sqlite3ExprDelete(db, pWhere); 584 return; 585 } 586 587 #ifndef SQLITE_OMIT_VIRTUALTABLE 588 /* 589 ** Generate code for an UPDATE of a virtual table. 590 ** 591 ** The strategy is that we create an ephemerial table that contains 592 ** for each row to be changed: 593 ** 594 ** (A) The original rowid of that row. 595 ** (B) The revised rowid for the row. (note1) 596 ** (C) The content of every column in the row. 597 ** 598 ** Then we loop over this ephemeral table and for each row in 599 ** the ephermeral table call VUpdate. 600 ** 601 ** When finished, drop the ephemeral table. 602 ** 603 ** (note1) Actually, if we know in advance that (A) is always the same 604 ** as (B) we only store (A), then duplicate (A) when pulling 605 ** it out of the ephemeral table before calling VUpdate. 606 */ 607 static void updateVirtualTable( 608 Parse *pParse, /* The parsing context */ 609 SrcList *pSrc, /* The virtual table to be modified */ 610 Table *pTab, /* The virtual table */ 611 ExprList *pChanges, /* The columns to change in the UPDATE statement */ 612 Expr *pRowid, /* Expression used to recompute the rowid */ 613 int *aXRef, /* Mapping from columns of pTab to entries in pChanges */ 614 Expr *pWhere /* WHERE clause of the UPDATE statement */ 615 ){ 616 Vdbe *v = pParse->pVdbe; /* Virtual machine under construction */ 617 ExprList *pEList = 0; /* The result set of the SELECT statement */ 618 Select *pSelect = 0; /* The SELECT statement */ 619 Expr *pExpr; /* Temporary expression */ 620 int ephemTab; /* Table holding the result of the SELECT */ 621 int i; /* Loop counter */ 622 int addr; /* Address of top of loop */ 623 int iReg; /* First register in set passed to OP_VUpdate */ 624 sqlite3 *db = pParse->db; /* Database connection */ 625 const char *pVtab = (const char*)pTab->pVtab; 626 SelectDest dest; 627 628 /* Construct the SELECT statement that will find the new values for 629 ** all updated rows. 630 */ 631 pEList = sqlite3ExprListAppend(pParse, 0, 632 sqlite3CreateIdExpr(pParse, "_rowid_"), 0); 633 if( pRowid ){ 634 pEList = sqlite3ExprListAppend(pParse, pEList, 635 sqlite3ExprDup(db, pRowid, 0), 0); 636 } 637 assert( pTab->iPKey<0 ); 638 for(i=0; i<pTab->nCol; i++){ 639 if( aXRef[i]>=0 ){ 640 pExpr = sqlite3ExprDup(db, pChanges->a[aXRef[i]].pExpr, 0); 641 }else{ 642 pExpr = sqlite3CreateIdExpr(pParse, pTab->aCol[i].zName); 643 } 644 pEList = sqlite3ExprListAppend(pParse, pEList, pExpr, 0); 645 } 646 pSelect = sqlite3SelectNew(pParse, pEList, pSrc, pWhere, 0, 0, 0, 0, 0, 0); 647 648 /* Create the ephemeral table into which the update results will 649 ** be stored. 650 */ 651 assert( v ); 652 ephemTab = pParse->nTab++; 653 sqlite3VdbeAddOp2(v, OP_OpenEphemeral, ephemTab, pTab->nCol+1+(pRowid!=0)); 654 655 /* fill the ephemeral table 656 */ 657 sqlite3SelectDestInit(&dest, SRT_Table, ephemTab); 658 sqlite3Select(pParse, pSelect, &dest); 659 660 /* Generate code to scan the ephemeral table and call VUpdate. */ 661 iReg = ++pParse->nMem; 662 pParse->nMem += pTab->nCol+1; 663 sqlite3VdbeAddOp2(v, OP_Rewind, ephemTab, 0); 664 addr = sqlite3VdbeCurrentAddr(v); 665 sqlite3VdbeAddOp3(v, OP_Column, ephemTab, 0, iReg); 666 sqlite3VdbeAddOp3(v, OP_Column, ephemTab, (pRowid?1:0), iReg+1); 667 for(i=0; i<pTab->nCol; i++){ 668 sqlite3VdbeAddOp3(v, OP_Column, ephemTab, i+1+(pRowid!=0), iReg+2+i); 669 } 670 sqlite3VtabMakeWritable(pParse, pTab); 671 sqlite3VdbeAddOp4(v, OP_VUpdate, 0, pTab->nCol+2, iReg, pVtab, P4_VTAB); 672 sqlite3VdbeAddOp2(v, OP_Next, ephemTab, addr); 673 sqlite3VdbeJumpHere(v, addr-1); 674 sqlite3VdbeAddOp2(v, OP_Close, ephemTab, 0); 675 676 /* Cleanup */ 677 sqlite3SelectDelete(db, pSelect); 678 } 679 #endif /* SQLITE_OMIT_VIRTUALTABLE */ 680 681 /* Make sure "isView" gets undefined in case this file becomes part of 682 ** the amalgamation - so that subsequent files do not see isView as a 683 ** macro. */ 684 #undef isView 685