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.144 2007/12/12 16:06:23 danielk1977 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 P3 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 P3 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 P3 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 P3 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 assert( i<pTab->nCol ); 63 sqlite3ValueFromExpr(sqlite3VdbeDb(v), pCol->pDflt, enc, pCol->affinity, &pValue); 64 if( pValue ){ 65 sqlite3VdbeChangeP3(v, -1, (const char *)pValue, P3_MEM); 66 }else{ 67 VdbeComment((v, "# %s.%s", pTab->zName, pCol->zName)); 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 nIdxTotal; /* Total number of indices */ 94 int iCur; /* VDBE Cursor number of pTab */ 95 sqlite3 *db; /* The database structure */ 96 Index **apIdx = 0; /* An array of indices that need updating too */ 97 char *aIdxUsed = 0; /* aIdxUsed[i]==1 if the i-th index is used */ 98 int *aXRef = 0; /* aXRef[i] is the index in pChanges->a[] of the 99 ** an expression for the i-th column of the table. 100 ** aXRef[i]==-1 if the i-th column is not changed. */ 101 int chngRowid; /* True if the record number is being changed */ 102 Expr *pRowidExpr = 0; /* Expression defining the new record number */ 103 int openAll = 0; /* True if all indices need to be opened */ 104 AuthContext sContext; /* The authorization context */ 105 NameContext sNC; /* The name-context to resolve expressions in */ 106 int iDb; /* Database containing the table being updated */ 107 int memCnt = 0; /* Memory cell used for counting rows changed */ 108 int mem1; /* Memory address storing the rowid for next row to update */ 109 110 #ifndef SQLITE_OMIT_TRIGGER 111 int isView; /* Trying to update a view */ 112 int triggers_exist = 0; /* True if any row triggers exist */ 113 #endif 114 115 int newIdx = -1; /* index of trigger "new" temp table */ 116 int oldIdx = -1; /* index of trigger "old" temp table */ 117 118 sContext.pParse = 0; 119 db = pParse->db; 120 if( pParse->nErr || db->mallocFailed ){ 121 goto update_cleanup; 122 } 123 assert( pTabList->nSrc==1 ); 124 125 /* Locate the table which we want to update. 126 */ 127 pTab = sqlite3SrcListLookup(pParse, pTabList); 128 if( pTab==0 ) goto update_cleanup; 129 iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema); 130 131 /* Figure out if we have any triggers and if the table being 132 ** updated is a view 133 */ 134 #ifndef SQLITE_OMIT_TRIGGER 135 triggers_exist = sqlite3TriggersExist(pParse, pTab, TK_UPDATE, pChanges); 136 isView = pTab->pSelect!=0; 137 #else 138 # define triggers_exist 0 139 # define isView 0 140 #endif 141 #ifdef SQLITE_OMIT_VIEW 142 # undef isView 143 # define isView 0 144 #endif 145 146 if( sqlite3IsReadOnly(pParse, pTab, triggers_exist) ){ 147 goto update_cleanup; 148 } 149 if( sqlite3ViewGetColumnNames(pParse, pTab) ){ 150 goto update_cleanup; 151 } 152 aXRef = sqlite3DbMallocRaw(db, sizeof(int) * pTab->nCol ); 153 if( aXRef==0 ) goto update_cleanup; 154 for(i=0; i<pTab->nCol; i++) aXRef[i] = -1; 155 156 /* If there are FOR EACH ROW triggers, allocate cursors for the 157 ** special OLD and NEW tables 158 */ 159 if( triggers_exist ){ 160 newIdx = pParse->nTab++; 161 oldIdx = pParse->nTab++; 162 } 163 164 /* Allocate a cursors for the main database table and for all indices. 165 ** The index cursors might not be used, but if they are used they 166 ** need to occur right after the database cursor. So go ahead and 167 ** allocate enough space, just in case. 168 */ 169 pTabList->a[0].iCursor = iCur = pParse->nTab++; 170 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){ 171 pParse->nTab++; 172 } 173 174 /* Initialize the name-context */ 175 memset(&sNC, 0, sizeof(sNC)); 176 sNC.pParse = pParse; 177 sNC.pSrcList = pTabList; 178 179 /* Resolve the column names in all the expressions of the 180 ** of the UPDATE statement. Also find the column index 181 ** for each column to be updated in the pChanges array. For each 182 ** column to be updated, make sure we have authorization to change 183 ** that column. 184 */ 185 chngRowid = 0; 186 for(i=0; i<pChanges->nExpr; i++){ 187 if( sqlite3ExprResolveNames(&sNC, pChanges->a[i].pExpr) ){ 188 goto update_cleanup; 189 } 190 for(j=0; j<pTab->nCol; j++){ 191 if( sqlite3StrICmp(pTab->aCol[j].zName, pChanges->a[i].zName)==0 ){ 192 if( j==pTab->iPKey ){ 193 chngRowid = 1; 194 pRowidExpr = pChanges->a[i].pExpr; 195 } 196 aXRef[j] = i; 197 break; 198 } 199 } 200 if( j>=pTab->nCol ){ 201 if( sqlite3IsRowid(pChanges->a[i].zName) ){ 202 chngRowid = 1; 203 pRowidExpr = pChanges->a[i].pExpr; 204 }else{ 205 sqlite3ErrorMsg(pParse, "no such column: %s", pChanges->a[i].zName); 206 goto update_cleanup; 207 } 208 } 209 #ifndef SQLITE_OMIT_AUTHORIZATION 210 { 211 int rc; 212 rc = sqlite3AuthCheck(pParse, SQLITE_UPDATE, pTab->zName, 213 pTab->aCol[j].zName, db->aDb[iDb].zName); 214 if( rc==SQLITE_DENY ){ 215 goto update_cleanup; 216 }else if( rc==SQLITE_IGNORE ){ 217 aXRef[j] = -1; 218 } 219 } 220 #endif 221 } 222 223 /* Allocate memory for the array apIdx[] and fill it with pointers to every 224 ** index that needs to be updated. Indices only need updating if their 225 ** key includes one of the columns named in pChanges or if the record 226 ** number of the original table entry is changing. 227 */ 228 for(nIdx=nIdxTotal=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, nIdxTotal++){ 229 if( chngRowid ){ 230 i = 0; 231 }else { 232 for(i=0; i<pIdx->nColumn; i++){ 233 if( aXRef[pIdx->aiColumn[i]]>=0 ) break; 234 } 235 } 236 if( i<pIdx->nColumn ) nIdx++; 237 } 238 if( nIdxTotal>0 ){ 239 apIdx = sqlite3DbMallocRaw(db, sizeof(Index*) * nIdx + nIdxTotal ); 240 if( apIdx==0 ) goto update_cleanup; 241 aIdxUsed = (char*)&apIdx[nIdx]; 242 } 243 for(nIdx=j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){ 244 if( chngRowid ){ 245 i = 0; 246 }else{ 247 for(i=0; i<pIdx->nColumn; i++){ 248 if( aXRef[pIdx->aiColumn[i]]>=0 ) break; 249 } 250 } 251 if( i<pIdx->nColumn ){ 252 apIdx[nIdx++] = pIdx; 253 aIdxUsed[j] = 1; 254 }else{ 255 aIdxUsed[j] = 0; 256 } 257 } 258 259 /* Begin generating code. 260 */ 261 v = sqlite3GetVdbe(pParse); 262 if( v==0 ) goto update_cleanup; 263 if( pParse->nested==0 ) sqlite3VdbeCountChanges(v); 264 sqlite3BeginWriteOperation(pParse, 1, iDb); 265 mem1 = pParse->nMem++; 266 267 #ifndef SQLITE_OMIT_VIRTUALTABLE 268 /* Virtual tables must be handled separately */ 269 if( IsVirtual(pTab) ){ 270 updateVirtualTable(pParse, pTabList, pTab, pChanges, pRowidExpr, aXRef, 271 pWhere); 272 pWhere = 0; 273 pTabList = 0; 274 goto update_cleanup; 275 } 276 #endif 277 278 /* Resolve the column names in all the expressions in the 279 ** WHERE clause. 280 */ 281 if( sqlite3ExprResolveNames(&sNC, pWhere) ){ 282 goto update_cleanup; 283 } 284 285 /* Start the view context 286 */ 287 if( isView ){ 288 sqlite3AuthContextPush(pParse, &sContext, pTab->zName); 289 } 290 291 /* If we are trying to update a view, realize that view into 292 ** a ephemeral table. 293 */ 294 if( isView ){ 295 Select *pView; 296 pView = sqlite3SelectDup(db, pTab->pSelect); 297 sqlite3Select(pParse, pView, SRT_EphemTab, iCur, 0, 0, 0, 0); 298 sqlite3SelectDelete(pView); 299 } 300 301 /* Begin the database scan 302 */ 303 pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, 0); 304 if( pWInfo==0 ) goto update_cleanup; 305 306 /* Remember the rowid of every item to be updated. 307 */ 308 sqlite3VdbeAddOp(v, IsVirtual(pTab) ? OP_VRowid : OP_Rowid, iCur, 0); 309 sqlite3VdbeAddOp(v, OP_FifoWrite, 0, 0); 310 311 /* End the database scan loop. 312 */ 313 sqlite3WhereEnd(pWInfo); 314 315 /* Initialize the count of updated rows 316 */ 317 if( db->flags & SQLITE_CountRows && !pParse->trigStack ){ 318 memCnt = pParse->nMem++; 319 sqlite3VdbeAddOp(v, OP_MemInt, 0, memCnt); 320 } 321 322 if( triggers_exist ){ 323 324 /* Create pseudo-tables for NEW and OLD 325 */ 326 sqlite3VdbeAddOp(v, OP_OpenPseudo, oldIdx, 0); 327 sqlite3VdbeAddOp(v, OP_SetNumColumns, oldIdx, pTab->nCol); 328 sqlite3VdbeAddOp(v, OP_OpenPseudo, newIdx, 0); 329 sqlite3VdbeAddOp(v, OP_SetNumColumns, newIdx, pTab->nCol); 330 331 /* The top of the update loop for when there are triggers. 332 */ 333 addr = sqlite3VdbeAddOp(v, OP_FifoRead, 0, 0); 334 sqlite3VdbeAddOp(v, OP_StackDepth, -1, 0); 335 sqlite3VdbeAddOp(v, OP_MemStore, mem1, 0); 336 337 if( !isView ){ 338 /* Open a cursor and make it point to the record that is 339 ** being updated. 340 */ 341 sqlite3OpenTable(pParse, iCur, iDb, pTab, OP_OpenRead); 342 } 343 sqlite3VdbeAddOp(v, OP_NotExists, iCur, addr); 344 345 /* Generate the OLD table 346 */ 347 sqlite3VdbeAddOp(v, OP_Rowid, iCur, 0); 348 sqlite3VdbeAddOp(v, OP_RowData, iCur, 0); 349 sqlite3VdbeAddOp(v, OP_Insert, oldIdx, 0); 350 351 /* Generate the NEW table 352 */ 353 if( chngRowid ){ 354 sqlite3ExprCodeAndCache(pParse, pRowidExpr); 355 }else{ 356 sqlite3VdbeAddOp(v, OP_Rowid, iCur, 0); 357 } 358 for(i=0; i<pTab->nCol; i++){ 359 if( i==pTab->iPKey ){ 360 sqlite3VdbeAddOp(v, OP_Null, 0, 0); 361 continue; 362 } 363 j = aXRef[i]; 364 if( j<0 ){ 365 sqlite3VdbeAddOp(v, OP_Column, iCur, i); 366 sqlite3ColumnDefault(v, pTab, i); 367 }else{ 368 sqlite3ExprCodeAndCache(pParse, pChanges->a[j].pExpr); 369 } 370 } 371 sqlite3VdbeAddOp(v, OP_MakeRecord, pTab->nCol, 0); 372 if( !isView ){ 373 sqlite3TableAffinityStr(v, pTab); 374 } 375 if( pParse->nErr ) goto update_cleanup; 376 sqlite3VdbeAddOp(v, OP_Insert, newIdx, 0); 377 if( !isView ){ 378 sqlite3VdbeAddOp(v, OP_Close, iCur, 0); 379 } 380 381 /* Fire the BEFORE and INSTEAD OF triggers 382 */ 383 if( sqlite3CodeRowTrigger(pParse, TK_UPDATE, pChanges, TRIGGER_BEFORE, pTab, 384 newIdx, oldIdx, onError, addr) ){ 385 goto update_cleanup; 386 } 387 388 if( !isView ){ 389 sqlite3VdbeAddOp(v, OP_MemLoad, mem1, 0); 390 } 391 } 392 393 if( !isView && !IsVirtual(pTab) ){ 394 /* 395 ** Open every index that needs updating. Note that if any 396 ** index could potentially invoke a REPLACE conflict resolution 397 ** action, then we need to open all indices because we might need 398 ** to be deleting some records. 399 */ 400 sqlite3OpenTable(pParse, iCur, iDb, pTab, OP_OpenWrite); 401 if( onError==OE_Replace ){ 402 openAll = 1; 403 }else{ 404 openAll = 0; 405 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){ 406 if( pIdx->onError==OE_Replace ){ 407 openAll = 1; 408 break; 409 } 410 } 411 } 412 for(i=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){ 413 if( openAll || aIdxUsed[i] ){ 414 KeyInfo *pKey = sqlite3IndexKeyinfo(pParse, pIdx); 415 sqlite3VdbeAddOp(v, OP_Integer, iDb, 0); 416 sqlite3VdbeOp3(v, OP_OpenWrite, iCur+i+1, pIdx->tnum, 417 (char*)pKey, P3_KEYINFO_HANDOFF); 418 assert( pParse->nTab>iCur+i+1 ); 419 } 420 } 421 422 /* Loop over every record that needs updating. We have to load 423 ** the old data for each record to be updated because some columns 424 ** might not change and we will need to copy the old value. 425 ** Also, the old data is needed to delete the old index entries. 426 ** So make the cursor point at the old record. 427 */ 428 if( !triggers_exist ){ 429 addr = sqlite3VdbeAddOp(v, OP_FifoRead, 0, 0); 430 sqlite3VdbeAddOp(v, OP_StackDepth, -1, 0); 431 sqlite3VdbeAddOp(v, OP_MemStore, mem1, 0); 432 } 433 sqlite3VdbeAddOp(v, OP_NotExists, iCur, addr); 434 sqlite3VdbeAddOp(v, OP_MemLoad, mem1, 0); 435 436 /* If the record number will change, push the record number as it 437 ** will be after the update. (The old record number is currently 438 ** on top of the stack.) 439 */ 440 if( chngRowid ){ 441 sqlite3ExprCode(pParse, pRowidExpr); 442 sqlite3VdbeAddOp(v, OP_MustBeInt, 0, 0); 443 } 444 445 /* Compute new data for this record. 446 */ 447 for(i=0; i<pTab->nCol; i++){ 448 if( i==pTab->iPKey ){ 449 sqlite3VdbeAddOp(v, OP_Null, 0, 0); 450 continue; 451 } 452 j = aXRef[i]; 453 if( j<0 ){ 454 sqlite3VdbeAddOp(v, OP_Column, iCur, i); 455 sqlite3ColumnDefault(v, pTab, i); 456 }else{ 457 sqlite3ExprCode(pParse, pChanges->a[j].pExpr); 458 } 459 } 460 461 /* Do constraint checks 462 */ 463 sqlite3GenerateConstraintChecks(pParse, pTab, iCur, aIdxUsed, chngRowid, 1, 464 onError, addr); 465 466 /* Delete the old indices for the current record. 467 */ 468 sqlite3GenerateRowIndexDelete(v, pTab, iCur, aIdxUsed); 469 470 /* If changing the record number, delete the old record. 471 */ 472 if( chngRowid ){ 473 sqlite3VdbeAddOp(v, OP_Delete, iCur, 0); 474 } 475 476 /* Create the new index entries and the new record. 477 */ 478 sqlite3CompleteInsertion(pParse, pTab, iCur, aIdxUsed, chngRowid, 1, -1, 0); 479 } 480 481 /* Increment the row counter 482 */ 483 if( db->flags & SQLITE_CountRows && !pParse->trigStack){ 484 sqlite3VdbeAddOp(v, OP_MemIncr, 1, memCnt); 485 } 486 487 /* If there are triggers, close all the cursors after each iteration 488 ** through the loop. The fire the after triggers. 489 */ 490 if( triggers_exist ){ 491 if( !isView ){ 492 for(i=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){ 493 if( openAll || aIdxUsed[i] ) 494 sqlite3VdbeAddOp(v, OP_Close, iCur+i+1, 0); 495 } 496 sqlite3VdbeAddOp(v, OP_Close, iCur, 0); 497 } 498 if( sqlite3CodeRowTrigger(pParse, TK_UPDATE, pChanges, TRIGGER_AFTER, pTab, 499 newIdx, oldIdx, onError, addr) ){ 500 goto update_cleanup; 501 } 502 } 503 504 /* Repeat the above with the next record to be updated, until 505 ** all record selected by the WHERE clause have been updated. 506 */ 507 sqlite3VdbeAddOp(v, OP_Goto, 0, addr); 508 sqlite3VdbeJumpHere(v, addr); 509 510 /* Close all tables if there were no FOR EACH ROW triggers */ 511 if( !triggers_exist ){ 512 for(i=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){ 513 if( openAll || aIdxUsed[i] ){ 514 sqlite3VdbeAddOp(v, OP_Close, iCur+i+1, 0); 515 } 516 } 517 sqlite3VdbeAddOp(v, OP_Close, iCur, 0); 518 }else{ 519 sqlite3VdbeAddOp(v, OP_Close, newIdx, 0); 520 sqlite3VdbeAddOp(v, OP_Close, oldIdx, 0); 521 } 522 523 /* 524 ** Return the number of rows that were changed. If this routine is 525 ** generating code because of a call to sqlite3NestedParse(), do not 526 ** invoke the callback function. 527 */ 528 if( db->flags & SQLITE_CountRows && !pParse->trigStack && pParse->nested==0 ){ 529 sqlite3VdbeAddOp(v, OP_MemLoad, memCnt, 0); 530 sqlite3VdbeAddOp(v, OP_Callback, 1, 0); 531 sqlite3VdbeSetNumCols(v, 1); 532 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "rows updated", P3_STATIC); 533 } 534 535 update_cleanup: 536 sqlite3AuthContextPop(&sContext); 537 sqlite3_free(apIdx); 538 sqlite3_free(aXRef); 539 sqlite3SrcListDelete(pTabList); 540 sqlite3ExprListDelete(pChanges); 541 sqlite3ExprDelete(pWhere); 542 return; 543 } 544 545 #ifndef SQLITE_OMIT_VIRTUALTABLE 546 /* 547 ** Generate code for an UPDATE of a virtual table. 548 ** 549 ** The strategy is that we create an ephemerial table that contains 550 ** for each row to be changed: 551 ** 552 ** (A) The original rowid of that row. 553 ** (B) The revised rowid for the row. (note1) 554 ** (C) The content of every column in the row. 555 ** 556 ** Then we loop over this ephemeral table and for each row in 557 ** the ephermeral table call VUpdate. 558 ** 559 ** When finished, drop the ephemeral table. 560 ** 561 ** (note1) Actually, if we know in advance that (A) is always the same 562 ** as (B) we only store (A), then duplicate (A) when pulling 563 ** it out of the ephemeral table before calling VUpdate. 564 */ 565 static void updateVirtualTable( 566 Parse *pParse, /* The parsing context */ 567 SrcList *pSrc, /* The virtual table to be modified */ 568 Table *pTab, /* The virtual table */ 569 ExprList *pChanges, /* The columns to change in the UPDATE statement */ 570 Expr *pRowid, /* Expression used to recompute the rowid */ 571 int *aXRef, /* Mapping from columns of pTab to entries in pChanges */ 572 Expr *pWhere /* WHERE clause of the UPDATE statement */ 573 ){ 574 Vdbe *v = pParse->pVdbe; /* Virtual machine under construction */ 575 ExprList *pEList = 0; /* The result set of the SELECT statement */ 576 Select *pSelect = 0; /* The SELECT statement */ 577 Expr *pExpr; /* Temporary expression */ 578 int ephemTab; /* Table holding the result of the SELECT */ 579 int i; /* Loop counter */ 580 int addr; /* Address of top of loop */ 581 sqlite3 *db = pParse->db; /* Database connection */ 582 583 /* Construct the SELECT statement that will find the new values for 584 ** all updated rows. 585 */ 586 pEList = sqlite3ExprListAppend(pParse, 0, 587 sqlite3CreateIdExpr(pParse, "_rowid_"), 0); 588 if( pRowid ){ 589 pEList = sqlite3ExprListAppend(pParse, pEList, 590 sqlite3ExprDup(db, pRowid), 0); 591 } 592 assert( pTab->iPKey<0 ); 593 for(i=0; i<pTab->nCol; i++){ 594 if( aXRef[i]>=0 ){ 595 pExpr = sqlite3ExprDup(db, pChanges->a[aXRef[i]].pExpr); 596 }else{ 597 pExpr = sqlite3CreateIdExpr(pParse, pTab->aCol[i].zName); 598 } 599 pEList = sqlite3ExprListAppend(pParse, pEList, pExpr, 0); 600 } 601 pSelect = sqlite3SelectNew(pParse, pEList, pSrc, pWhere, 0, 0, 0, 0, 0, 0); 602 603 /* Create the ephemeral table into which the update results will 604 ** be stored. 605 */ 606 assert( v ); 607 ephemTab = pParse->nTab++; 608 sqlite3VdbeAddOp(v, OP_OpenEphemeral, ephemTab, pTab->nCol+1+(pRowid!=0)); 609 610 /* fill the ephemeral table 611 */ 612 sqlite3Select(pParse, pSelect, SRT_Table, ephemTab, 0, 0, 0, 0); 613 614 /* 615 ** Generate code to scan the ephemeral table and call VDelete and 616 ** VInsert 617 */ 618 sqlite3VdbeAddOp(v, OP_Rewind, ephemTab, 0); 619 addr = sqlite3VdbeCurrentAddr(v); 620 sqlite3VdbeAddOp(v, OP_Column, ephemTab, 0); 621 if( pRowid ){ 622 sqlite3VdbeAddOp(v, OP_Column, ephemTab, 1); 623 }else{ 624 sqlite3VdbeAddOp(v, OP_Dup, 0, 0); 625 } 626 for(i=0; i<pTab->nCol; i++){ 627 sqlite3VdbeAddOp(v, OP_Column, ephemTab, i+1+(pRowid!=0)); 628 } 629 pParse->pVirtualLock = pTab; 630 sqlite3VdbeOp3(v, OP_VUpdate, 0, pTab->nCol+2, 631 (const char*)pTab->pVtab, P3_VTAB); 632 sqlite3VdbeAddOp(v, OP_Next, ephemTab, addr); 633 sqlite3VdbeJumpHere(v, addr-1); 634 sqlite3VdbeAddOp(v, OP_Close, ephemTab, 0); 635 636 /* Cleanup */ 637 sqlite3SelectDelete(pSelect); 638 } 639 #endif /* SQLITE_OMIT_VIRTUALTABLE */ 640