OLD | NEW |
1 /* | 1 /* |
2 ** 2001 September 15 | 2 ** 2001 September 15 |
3 ** | 3 ** |
4 ** The author disclaims copyright to this source code. In place of | 4 ** The author disclaims copyright to this source code. In place of |
5 ** a legal notice, here is a blessing: | 5 ** a legal notice, here is a blessing: |
6 ** | 6 ** |
7 ** May you do good and not evil. | 7 ** May you do good and not evil. |
8 ** May you find forgiveness for yourself and forgive others. | 8 ** May you find forgiveness for yourself and forgive others. |
9 ** May you share freely, never taking more than you give. | 9 ** May you share freely, never taking more than you give. |
10 ** | 10 ** |
(...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
127 Trigger *pTrigger; /* List of triggers on pTab, if required */ | 127 Trigger *pTrigger; /* List of triggers on pTab, if required */ |
128 int tmask; /* Mask of TRIGGER_BEFORE|TRIGGER_AFTER */ | 128 int tmask; /* Mask of TRIGGER_BEFORE|TRIGGER_AFTER */ |
129 #endif | 129 #endif |
130 int newmask; /* Mask of NEW.* columns accessed by BEFORE triggers */ | 130 int newmask; /* Mask of NEW.* columns accessed by BEFORE triggers */ |
131 int iEph = 0; /* Ephemeral table holding all primary key values */ | 131 int iEph = 0; /* Ephemeral table holding all primary key values */ |
132 int nKey = 0; /* Number of elements in regKey for WITHOUT ROWID */ | 132 int nKey = 0; /* Number of elements in regKey for WITHOUT ROWID */ |
133 int aiCurOnePass[2]; /* The write cursors opened by WHERE_ONEPASS */ | 133 int aiCurOnePass[2]; /* The write cursors opened by WHERE_ONEPASS */ |
134 | 134 |
135 /* Register Allocations */ | 135 /* Register Allocations */ |
136 int regRowCount = 0; /* A count of rows changed */ | 136 int regRowCount = 0; /* A count of rows changed */ |
137 int regOldRowid; /* The old rowid */ | 137 int regOldRowid = 0; /* The old rowid */ |
138 int regNewRowid; /* The new rowid */ | 138 int regNewRowid = 0; /* The new rowid */ |
139 int regNew; /* Content of the NEW.* table in triggers */ | 139 int regNew = 0; /* Content of the NEW.* table in triggers */ |
140 int regOld = 0; /* Content of OLD.* table in triggers */ | 140 int regOld = 0; /* Content of OLD.* table in triggers */ |
141 int regRowSet = 0; /* Rowset of rows to be updated */ | 141 int regRowSet = 0; /* Rowset of rows to be updated */ |
142 int regKey = 0; /* composite PRIMARY KEY value */ | 142 int regKey = 0; /* composite PRIMARY KEY value */ |
143 | 143 |
144 memset(&sContext, 0, sizeof(sContext)); | 144 memset(&sContext, 0, sizeof(sContext)); |
145 db = pParse->db; | 145 db = pParse->db; |
146 if( pParse->nErr || db->mallocFailed ){ | 146 if( pParse->nErr || db->mallocFailed ){ |
147 goto update_cleanup; | 147 goto update_cleanup; |
148 } | 148 } |
149 assert( pTabList->nSrc==1 ); | 149 assert( pTabList->nSrc==1 ); |
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
256 aXRef[j] = -1; | 256 aXRef[j] = -1; |
257 } | 257 } |
258 } | 258 } |
259 #endif | 259 #endif |
260 } | 260 } |
261 assert( (chngRowid & chngPk)==0 ); | 261 assert( (chngRowid & chngPk)==0 ); |
262 assert( chngRowid==0 || chngRowid==1 ); | 262 assert( chngRowid==0 || chngRowid==1 ); |
263 assert( chngPk==0 || chngPk==1 ); | 263 assert( chngPk==0 || chngPk==1 ); |
264 chngKey = chngRowid + chngPk; | 264 chngKey = chngRowid + chngPk; |
265 | 265 |
266 /* The SET expressions are not actually used inside the WHERE loop. | 266 /* The SET expressions are not actually used inside the WHERE loop. |
267 ** So reset the colUsed mask | 267 ** So reset the colUsed mask. Unless this is a virtual table. In that |
| 268 ** case, set all bits of the colUsed mask (to ensure that the virtual |
| 269 ** table implementation makes all columns available). |
268 */ | 270 */ |
269 pTabList->a[0].colUsed = 0; | 271 pTabList->a[0].colUsed = IsVirtual(pTab) ? (Bitmask)-1 : 0; |
270 | 272 |
271 hasFK = sqlite3FkRequired(pParse, pTab, aXRef, chngKey); | 273 hasFK = sqlite3FkRequired(pParse, pTab, aXRef, chngKey); |
272 | 274 |
273 /* There is one entry in the aRegIdx[] array for each index on the table | 275 /* There is one entry in the aRegIdx[] array for each index on the table |
274 ** being updated. Fill in aRegIdx[] with a register number that will hold | 276 ** being updated. Fill in aRegIdx[] with a register number that will hold |
275 ** the key for accessing each index. | 277 ** the key for accessing each index. |
| 278 ** |
| 279 ** FIXME: Be smarter about omitting indexes that use expressions. |
276 */ | 280 */ |
277 for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){ | 281 for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){ |
278 int reg; | 282 int reg; |
279 if( chngKey || hasFK || pIdx->pPartIdxWhere || pIdx==pPk ){ | 283 if( chngKey || hasFK || pIdx->pPartIdxWhere || pIdx==pPk ){ |
280 reg = ++pParse->nMem; | 284 reg = ++pParse->nMem; |
281 }else{ | 285 }else{ |
282 reg = 0; | 286 reg = 0; |
283 for(i=0; i<pIdx->nKeyCol; i++){ | 287 for(i=0; i<pIdx->nKeyCol; i++){ |
284 if( aXRef[pIdx->aiColumn[i]]>=0 ){ | 288 i16 iIdxCol = pIdx->aiColumn[i]; |
| 289 if( iIdxCol<0 || aXRef[iIdxCol]>=0 ){ |
285 reg = ++pParse->nMem; | 290 reg = ++pParse->nMem; |
286 break; | 291 break; |
287 } | 292 } |
288 } | 293 } |
289 } | 294 } |
290 if( reg==0 ) aToOpen[j+1] = 0; | 295 if( reg==0 ) aToOpen[j+1] = 0; |
291 aRegIdx[j] = reg; | 296 aRegIdx[j] = reg; |
292 } | 297 } |
293 | 298 |
294 /* Begin generating code. */ | 299 /* Begin generating code. */ |
295 v = sqlite3GetVdbe(pParse); | 300 v = sqlite3GetVdbe(pParse); |
296 if( v==0 ) goto update_cleanup; | 301 if( v==0 ) goto update_cleanup; |
297 if( pParse->nested==0 ) sqlite3VdbeCountChanges(v); | 302 if( pParse->nested==0 ) sqlite3VdbeCountChanges(v); |
298 sqlite3BeginWriteOperation(pParse, 1, iDb); | 303 sqlite3BeginWriteOperation(pParse, 1, iDb); |
299 | 304 |
300 #ifndef SQLITE_OMIT_VIRTUALTABLE | |
301 /* Virtual tables must be handled separately */ | |
302 if( IsVirtual(pTab) ){ | |
303 updateVirtualTable(pParse, pTabList, pTab, pChanges, pRowidExpr, aXRef, | |
304 pWhere, onError); | |
305 pWhere = 0; | |
306 pTabList = 0; | |
307 goto update_cleanup; | |
308 } | |
309 #endif | |
310 | |
311 /* Allocate required registers. */ | 305 /* Allocate required registers. */ |
312 regRowSet = ++pParse->nMem; | 306 if( !IsVirtual(pTab) ){ |
313 regOldRowid = regNewRowid = ++pParse->nMem; | 307 regRowSet = ++pParse->nMem; |
314 if( chngPk || pTrigger || hasFK ){ | 308 regOldRowid = regNewRowid = ++pParse->nMem; |
315 regOld = pParse->nMem + 1; | 309 if( chngPk || pTrigger || hasFK ){ |
| 310 regOld = pParse->nMem + 1; |
| 311 pParse->nMem += pTab->nCol; |
| 312 } |
| 313 if( chngKey || pTrigger || hasFK ){ |
| 314 regNewRowid = ++pParse->nMem; |
| 315 } |
| 316 regNew = pParse->nMem + 1; |
316 pParse->nMem += pTab->nCol; | 317 pParse->nMem += pTab->nCol; |
317 } | 318 } |
318 if( chngKey || pTrigger || hasFK ){ | |
319 regNewRowid = ++pParse->nMem; | |
320 } | |
321 regNew = pParse->nMem + 1; | |
322 pParse->nMem += pTab->nCol; | |
323 | 319 |
324 /* Start the view context. */ | 320 /* Start the view context. */ |
325 if( isView ){ | 321 if( isView ){ |
326 sqlite3AuthContextPush(pParse, &sContext, pTab->zName); | 322 sqlite3AuthContextPush(pParse, &sContext, pTab->zName); |
327 } | 323 } |
328 | 324 |
329 /* If we are trying to update a view, realize that view into | 325 /* If we are trying to update a view, realize that view into |
330 ** an ephemeral table. | 326 ** an ephemeral table. |
331 */ | 327 */ |
332 #if !defined(SQLITE_OMIT_VIEW) && !defined(SQLITE_OMIT_TRIGGER) | 328 #if !defined(SQLITE_OMIT_VIEW) && !defined(SQLITE_OMIT_TRIGGER) |
333 if( isView ){ | 329 if( isView ){ |
334 sqlite3MaterializeView(pParse, pTab, pWhere, iDataCur); | 330 sqlite3MaterializeView(pParse, pTab, pWhere, iDataCur); |
335 } | 331 } |
336 #endif | 332 #endif |
337 | 333 |
338 /* Resolve the column names in all the expressions in the | 334 /* Resolve the column names in all the expressions in the |
339 ** WHERE clause. | 335 ** WHERE clause. |
340 */ | 336 */ |
341 if( sqlite3ResolveExprNames(&sNC, pWhere) ){ | 337 if( sqlite3ResolveExprNames(&sNC, pWhere) ){ |
342 goto update_cleanup; | 338 goto update_cleanup; |
343 } | 339 } |
344 | 340 |
| 341 #ifndef SQLITE_OMIT_VIRTUALTABLE |
| 342 /* Virtual tables must be handled separately */ |
| 343 if( IsVirtual(pTab) ){ |
| 344 updateVirtualTable(pParse, pTabList, pTab, pChanges, pRowidExpr, aXRef, |
| 345 pWhere, onError); |
| 346 goto update_cleanup; |
| 347 } |
| 348 #endif |
| 349 |
345 /* Begin the database scan | 350 /* Begin the database scan |
346 */ | 351 */ |
347 if( HasRowid(pTab) ){ | 352 if( HasRowid(pTab) ){ |
348 sqlite3VdbeAddOp3(v, OP_Null, 0, regRowSet, regOldRowid); | 353 sqlite3VdbeAddOp3(v, OP_Null, 0, regRowSet, regOldRowid); |
349 pWInfo = sqlite3WhereBegin( | 354 pWInfo = sqlite3WhereBegin( |
350 pParse, pTabList, pWhere, 0, 0, WHERE_ONEPASS_DESIRED, iIdxCur | 355 pParse, pTabList, pWhere, 0, 0, WHERE_ONEPASS_DESIRED, iIdxCur |
351 ); | 356 ); |
352 if( pWInfo==0 ) goto update_cleanup; | 357 if( pWInfo==0 ) goto update_cleanup; |
353 okOnePass = sqlite3WhereOkOnePass(pWInfo, aiCurOnePass); | 358 okOnePass = sqlite3WhereOkOnePass(pWInfo, aiCurOnePass); |
354 | 359 |
(...skipping 19 matching lines...) Expand all Loading... |
374 regKey = ++pParse->nMem; | 379 regKey = ++pParse->nMem; |
375 iEph = pParse->nTab++; | 380 iEph = pParse->nTab++; |
376 sqlite3VdbeAddOp2(v, OP_Null, 0, iPk); | 381 sqlite3VdbeAddOp2(v, OP_Null, 0, iPk); |
377 addrOpen = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, iEph, nPk); | 382 addrOpen = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, iEph, nPk); |
378 sqlite3VdbeSetP4KeyInfo(pParse, pPk); | 383 sqlite3VdbeSetP4KeyInfo(pParse, pPk); |
379 pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, 0, 0, | 384 pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, 0, 0, |
380 WHERE_ONEPASS_DESIRED, iIdxCur); | 385 WHERE_ONEPASS_DESIRED, iIdxCur); |
381 if( pWInfo==0 ) goto update_cleanup; | 386 if( pWInfo==0 ) goto update_cleanup; |
382 okOnePass = sqlite3WhereOkOnePass(pWInfo, aiCurOnePass); | 387 okOnePass = sqlite3WhereOkOnePass(pWInfo, aiCurOnePass); |
383 for(i=0; i<nPk; i++){ | 388 for(i=0; i<nPk; i++){ |
| 389 assert( pPk->aiColumn[i]>=0 ); |
384 sqlite3ExprCodeGetColumnOfTable(v, pTab, iDataCur, pPk->aiColumn[i], | 390 sqlite3ExprCodeGetColumnOfTable(v, pTab, iDataCur, pPk->aiColumn[i], |
385 iPk+i); | 391 iPk+i); |
386 } | 392 } |
387 if( okOnePass ){ | 393 if( okOnePass ){ |
388 sqlite3VdbeChangeToNoop(v, addrOpen); | 394 sqlite3VdbeChangeToNoop(v, addrOpen); |
389 nKey = nPk; | 395 nKey = nPk; |
390 regKey = iPk; | 396 regKey = iPk; |
391 }else{ | 397 }else{ |
392 sqlite3VdbeAddOp4(v, OP_MakeRecord, iPk, nPk, regKey, | 398 sqlite3VdbeAddOp4(v, OP_MakeRecord, iPk, nPk, regKey, |
393 sqlite3IndexAffinityStr(v, pPk), nPk); | 399 sqlite3IndexAffinityStr(db, pPk), nPk); |
394 sqlite3VdbeAddOp2(v, OP_IdxInsert, iEph, regKey); | 400 sqlite3VdbeAddOp2(v, OP_IdxInsert, iEph, regKey); |
395 } | 401 } |
396 sqlite3WhereEnd(pWInfo); | 402 sqlite3WhereEnd(pWInfo); |
397 } | 403 } |
398 | 404 |
399 /* Initialize the count of updated rows | 405 /* Initialize the count of updated rows |
400 */ | 406 */ |
401 if( (db->flags & SQLITE_CountRows) && !pParse->pTriggerTab ){ | 407 if( (db->flags & SQLITE_CountRows) && !pParse->pTriggerTab ){ |
402 regRowCount = ++pParse->nMem; | 408 regRowCount = ++pParse->nMem; |
403 sqlite3VdbeAddOp2(v, OP_Integer, 0, regRowCount); | 409 sqlite3VdbeAddOp2(v, OP_Integer, 0, regRowCount); |
(...skipping 14 matching lines...) Expand all Loading... |
418 if( pIdx->onError==OE_Replace ){ | 424 if( pIdx->onError==OE_Replace ){ |
419 memset(aToOpen, 1, nIdx+1); | 425 memset(aToOpen, 1, nIdx+1); |
420 break; | 426 break; |
421 } | 427 } |
422 } | 428 } |
423 } | 429 } |
424 if( okOnePass ){ | 430 if( okOnePass ){ |
425 if( aiCurOnePass[0]>=0 ) aToOpen[aiCurOnePass[0]-iBaseCur] = 0; | 431 if( aiCurOnePass[0]>=0 ) aToOpen[aiCurOnePass[0]-iBaseCur] = 0; |
426 if( aiCurOnePass[1]>=0 ) aToOpen[aiCurOnePass[1]-iBaseCur] = 0; | 432 if( aiCurOnePass[1]>=0 ) aToOpen[aiCurOnePass[1]-iBaseCur] = 0; |
427 } | 433 } |
428 sqlite3OpenTableAndIndices(pParse, pTab, OP_OpenWrite, iBaseCur, aToOpen, | 434 sqlite3OpenTableAndIndices(pParse, pTab, OP_OpenWrite, 0, iBaseCur, aToOpen, |
429 0, 0); | 435 0, 0); |
430 } | 436 } |
431 | 437 |
432 /* Top of the update loop */ | 438 /* Top of the update loop */ |
433 if( okOnePass ){ | 439 if( okOnePass ){ |
434 if( aToOpen[iDataCur-iBaseCur] && !isView ){ | 440 if( aToOpen[iDataCur-iBaseCur] && !isView ){ |
435 assert( pPk ); | 441 assert( pPk ); |
436 sqlite3VdbeAddOp4Int(v, OP_NotFound, iDataCur, labelBreak, regKey, nKey); | 442 sqlite3VdbeAddOp4Int(v, OP_NotFound, iDataCur, labelBreak, regKey, nKey); |
437 VdbeCoverageNeverTaken(v); | 443 VdbeCoverageNeverTaken(v); |
438 } | 444 } |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
496 ** registers associated with columns that are (a) not modified by | 502 ** registers associated with columns that are (a) not modified by |
497 ** this UPDATE statement and (b) not accessed by new.* references. The | 503 ** this UPDATE statement and (b) not accessed by new.* references. The |
498 ** values for registers not modified by the UPDATE must be reloaded from | 504 ** values for registers not modified by the UPDATE must be reloaded from |
499 ** the database after the BEFORE triggers are fired anyway (as the trigger | 505 ** the database after the BEFORE triggers are fired anyway (as the trigger |
500 ** may have modified them). So not loading those that are not going to | 506 ** may have modified them). So not loading those that are not going to |
501 ** be used eliminates some redundant opcodes. | 507 ** be used eliminates some redundant opcodes. |
502 */ | 508 */ |
503 newmask = sqlite3TriggerColmask( | 509 newmask = sqlite3TriggerColmask( |
504 pParse, pTrigger, pChanges, 1, TRIGGER_BEFORE, pTab, onError | 510 pParse, pTrigger, pChanges, 1, TRIGGER_BEFORE, pTab, onError |
505 ); | 511 ); |
506 /*sqlite3VdbeAddOp3(v, OP_Null, 0, regNew, regNew+pTab->nCol-1);*/ | |
507 for(i=0; i<pTab->nCol; i++){ | 512 for(i=0; i<pTab->nCol; i++){ |
508 if( i==pTab->iPKey ){ | 513 if( i==pTab->iPKey ){ |
509 sqlite3VdbeAddOp2(v, OP_Null, 0, regNew+i); | 514 sqlite3VdbeAddOp2(v, OP_Null, 0, regNew+i); |
510 }else{ | 515 }else{ |
511 j = aXRef[i]; | 516 j = aXRef[i]; |
512 if( j>=0 ){ | 517 if( j>=0 ){ |
513 sqlite3ExprCode(pParse, pChanges->a[j].pExpr, regNew+i); | 518 sqlite3ExprCode(pParse, pChanges->a[j].pExpr, regNew+i); |
514 }else if( 0==(tmask&TRIGGER_BEFORE) || i>31 || (newmask & MASKBIT32(i)) ){ | 519 }else if( 0==(tmask&TRIGGER_BEFORE) || i>31 || (newmask & MASKBIT32(i)) ){ |
515 /* This branch loads the value of a column that will not be changed | 520 /* This branch loads the value of a column that will not be changed |
516 ** into a register. This is done if there are no BEFORE triggers, or | 521 ** into a register. This is done if there are no BEFORE triggers, or |
517 ** if there are one or more BEFORE triggers that use this value via | 522 ** if there are one or more BEFORE triggers that use this value via |
518 ** a new.* reference in a trigger program. | 523 ** a new.* reference in a trigger program. |
519 */ | 524 */ |
520 testcase( i==31 ); | 525 testcase( i==31 ); |
521 testcase( i==32 ); | 526 testcase( i==32 ); |
522 sqlite3ExprCodeGetColumnOfTable(v, pTab, iDataCur, i, regNew+i); | 527 sqlite3ExprCodeGetColumnToReg(pParse, pTab, i, iDataCur, regNew+i); |
523 }else{ | 528 }else{ |
524 sqlite3VdbeAddOp2(v, OP_Null, 0, regNew+i); | 529 sqlite3VdbeAddOp2(v, OP_Null, 0, regNew+i); |
525 } | 530 } |
526 } | 531 } |
527 } | 532 } |
528 | 533 |
529 /* Fire any BEFORE UPDATE triggers. This happens before constraints are | 534 /* Fire any BEFORE UPDATE triggers. This happens before constraints are |
530 ** verified. One could argue that this is wrong. | 535 ** verified. One could argue that this is wrong. |
531 */ | 536 */ |
532 if( tmask&TRIGGER_BEFORE ){ | 537 if( tmask&TRIGGER_BEFORE ){ |
(...skipping 21 matching lines...) Expand all Loading... |
554 ** registers in case this has happened. | 559 ** registers in case this has happened. |
555 */ | 560 */ |
556 for(i=0; i<pTab->nCol; i++){ | 561 for(i=0; i<pTab->nCol; i++){ |
557 if( aXRef[i]<0 && i!=pTab->iPKey ){ | 562 if( aXRef[i]<0 && i!=pTab->iPKey ){ |
558 sqlite3ExprCodeGetColumnOfTable(v, pTab, iDataCur, i, regNew+i); | 563 sqlite3ExprCodeGetColumnOfTable(v, pTab, iDataCur, i, regNew+i); |
559 } | 564 } |
560 } | 565 } |
561 } | 566 } |
562 | 567 |
563 if( !isView ){ | 568 if( !isView ){ |
564 int j1 = 0; /* Address of jump instruction */ | 569 int addr1 = 0; /* Address of jump instruction */ |
565 int bReplace = 0; /* True if REPLACE conflict resolution might happen */ | 570 int bReplace = 0; /* True if REPLACE conflict resolution might happen */ |
566 | 571 |
567 /* Do constraint checks. */ | 572 /* Do constraint checks. */ |
568 assert( regOldRowid>0 ); | 573 assert( regOldRowid>0 ); |
569 sqlite3GenerateConstraintChecks(pParse, pTab, aRegIdx, iDataCur, iIdxCur, | 574 sqlite3GenerateConstraintChecks(pParse, pTab, aRegIdx, iDataCur, iIdxCur, |
570 regNewRowid, regOldRowid, chngKey, onError, labelContinue, &bReplace); | 575 regNewRowid, regOldRowid, chngKey, onError, labelContinue, &bReplace); |
571 | 576 |
572 /* Do FK constraint checks. */ | 577 /* Do FK constraint checks. */ |
573 if( hasFK ){ | 578 if( hasFK ){ |
574 sqlite3FkCheck(pParse, pTab, regOldRowid, 0, aXRef, chngKey); | 579 sqlite3FkCheck(pParse, pTab, regOldRowid, 0, aXRef, chngKey); |
575 } | 580 } |
576 | 581 |
577 /* Delete the index entries associated with the current record. */ | 582 /* Delete the index entries associated with the current record. */ |
578 if( bReplace || chngKey ){ | 583 if( bReplace || chngKey ){ |
579 if( pPk ){ | 584 if( pPk ){ |
580 j1 = sqlite3VdbeAddOp4Int(v, OP_NotFound, iDataCur, 0, regKey, nKey); | 585 addr1 = sqlite3VdbeAddOp4Int(v, OP_NotFound, iDataCur, 0, regKey, nKey); |
581 }else{ | 586 }else{ |
582 j1 = sqlite3VdbeAddOp3(v, OP_NotExists, iDataCur, 0, regOldRowid); | 587 addr1 = sqlite3VdbeAddOp3(v, OP_NotExists, iDataCur, 0, regOldRowid); |
583 } | 588 } |
584 VdbeCoverageNeverTaken(v); | 589 VdbeCoverageNeverTaken(v); |
585 } | 590 } |
586 sqlite3GenerateRowIndexDelete(pParse, pTab, iDataCur, iIdxCur, aRegIdx); | 591 sqlite3GenerateRowIndexDelete(pParse, pTab, iDataCur, iIdxCur, aRegIdx, -1); |
587 | 592 |
588 /* If changing the record number, delete the old record. */ | 593 /* If changing the record number, delete the old record. */ |
589 if( hasFK || chngKey || pPk!=0 ){ | 594 if( hasFK || chngKey || pPk!=0 ){ |
590 sqlite3VdbeAddOp2(v, OP_Delete, iDataCur, 0); | 595 sqlite3VdbeAddOp2(v, OP_Delete, iDataCur, 0); |
591 } | 596 } |
592 if( bReplace || chngKey ){ | 597 if( bReplace || chngKey ){ |
593 sqlite3VdbeJumpHere(v, j1); | 598 sqlite3VdbeJumpHere(v, addr1); |
594 } | 599 } |
595 | 600 |
596 if( hasFK ){ | 601 if( hasFK ){ |
597 sqlite3FkCheck(pParse, pTab, 0, regNewRowid, aXRef, chngKey); | 602 sqlite3FkCheck(pParse, pTab, 0, regNewRowid, aXRef, chngKey); |
598 } | 603 } |
599 | 604 |
600 /* Insert the new index entries and the new record. */ | 605 /* Insert the new index entries and the new record. */ |
601 sqlite3CompleteInsertion(pParse, pTab, iDataCur, iIdxCur, | 606 sqlite3CompleteInsertion(pParse, pTab, iDataCur, iIdxCur, |
602 regNewRowid, aRegIdx, 1, 0, 0); | 607 regNewRowid, aRegIdx, 1, 0, 0); |
603 | 608 |
(...skipping 16 matching lines...) Expand all Loading... |
620 | 625 |
621 /* Repeat the above with the next record to be updated, until | 626 /* Repeat the above with the next record to be updated, until |
622 ** all record selected by the WHERE clause have been updated. | 627 ** all record selected by the WHERE clause have been updated. |
623 */ | 628 */ |
624 if( okOnePass ){ | 629 if( okOnePass ){ |
625 /* Nothing to do at end-of-loop for a single-pass */ | 630 /* Nothing to do at end-of-loop for a single-pass */ |
626 }else if( pPk ){ | 631 }else if( pPk ){ |
627 sqlite3VdbeResolveLabel(v, labelContinue); | 632 sqlite3VdbeResolveLabel(v, labelContinue); |
628 sqlite3VdbeAddOp2(v, OP_Next, iEph, addrTop); VdbeCoverage(v); | 633 sqlite3VdbeAddOp2(v, OP_Next, iEph, addrTop); VdbeCoverage(v); |
629 }else{ | 634 }else{ |
630 sqlite3VdbeAddOp2(v, OP_Goto, 0, labelContinue); | 635 sqlite3VdbeGoto(v, labelContinue); |
631 } | 636 } |
632 sqlite3VdbeResolveLabel(v, labelBreak); | 637 sqlite3VdbeResolveLabel(v, labelBreak); |
633 | 638 |
634 /* Close all tables */ | 639 /* Close all tables */ |
635 for(i=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){ | 640 for(i=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){ |
636 assert( aRegIdx ); | 641 assert( aRegIdx ); |
637 if( aToOpen[i+1] ){ | 642 if( aToOpen[i+1] ){ |
638 sqlite3VdbeAddOp2(v, OP_Close, iIdxCur+i, 0); | 643 sqlite3VdbeAddOp2(v, OP_Close, iIdxCur+i, 0); |
639 } | 644 } |
640 } | 645 } |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
674 #undef isView | 679 #undef isView |
675 #endif | 680 #endif |
676 #ifdef pTrigger | 681 #ifdef pTrigger |
677 #undef pTrigger | 682 #undef pTrigger |
678 #endif | 683 #endif |
679 | 684 |
680 #ifndef SQLITE_OMIT_VIRTUALTABLE | 685 #ifndef SQLITE_OMIT_VIRTUALTABLE |
681 /* | 686 /* |
682 ** Generate code for an UPDATE of a virtual table. | 687 ** Generate code for an UPDATE of a virtual table. |
683 ** | 688 ** |
684 ** The strategy is that we create an ephemeral table that contains | 689 ** There are two possible strategies - the default and the special |
| 690 ** "onepass" strategy. Onepass is only used if the virtual table |
| 691 ** implementation indicates that pWhere may match at most one row. |
| 692 ** |
| 693 ** The default strategy is to create an ephemeral table that contains |
685 ** for each row to be changed: | 694 ** for each row to be changed: |
686 ** | 695 ** |
687 ** (A) The original rowid of that row. | 696 ** (A) The original rowid of that row. |
688 ** (B) The revised rowid for the row. (note1) | 697 ** (B) The revised rowid for the row. |
689 ** (C) The content of every column in the row. | 698 ** (C) The content of every column in the row. |
690 ** | 699 ** |
691 ** Then we loop over this ephemeral table and for each row in | 700 ** Then loop through the contents of this ephemeral table executing a |
692 ** the ephemeral table call VUpdate. | 701 ** VUpdate for each row. When finished, drop the ephemeral table. |
693 ** | 702 ** |
694 ** When finished, drop the ephemeral table. | 703 ** The "onepass" strategy does not use an ephemeral table. Instead, it |
695 ** | 704 ** stores the same values (A, B and C above) in a register array and |
696 ** (note1) Actually, if we know in advance that (A) is always the same | 705 ** makes a single invocation of VUpdate. |
697 ** as (B) we only store (A), then duplicate (A) when pulling | |
698 ** it out of the ephemeral table before calling VUpdate. | |
699 */ | 706 */ |
700 static void updateVirtualTable( | 707 static void updateVirtualTable( |
701 Parse *pParse, /* The parsing context */ | 708 Parse *pParse, /* The parsing context */ |
702 SrcList *pSrc, /* The virtual table to be modified */ | 709 SrcList *pSrc, /* The virtual table to be modified */ |
703 Table *pTab, /* The virtual table */ | 710 Table *pTab, /* The virtual table */ |
704 ExprList *pChanges, /* The columns to change in the UPDATE statement */ | 711 ExprList *pChanges, /* The columns to change in the UPDATE statement */ |
705 Expr *pRowid, /* Expression used to recompute the rowid */ | 712 Expr *pRowid, /* Expression used to recompute the rowid */ |
706 int *aXRef, /* Mapping from columns of pTab to entries in pChanges */ | 713 int *aXRef, /* Mapping from columns of pTab to entries in pChanges */ |
707 Expr *pWhere, /* WHERE clause of the UPDATE statement */ | 714 Expr *pWhere, /* WHERE clause of the UPDATE statement */ |
708 int onError /* ON CONFLICT strategy */ | 715 int onError /* ON CONFLICT strategy */ |
709 ){ | 716 ){ |
710 Vdbe *v = pParse->pVdbe; /* Virtual machine under construction */ | 717 Vdbe *v = pParse->pVdbe; /* Virtual machine under construction */ |
711 ExprList *pEList = 0; /* The result set of the SELECT statement */ | |
712 Select *pSelect = 0; /* The SELECT statement */ | |
713 Expr *pExpr; /* Temporary expression */ | |
714 int ephemTab; /* Table holding the result of the SELECT */ | 718 int ephemTab; /* Table holding the result of the SELECT */ |
715 int i; /* Loop counter */ | 719 int i; /* Loop counter */ |
716 int addr; /* Address of top of loop */ | |
717 int iReg; /* First register in set passed to OP_VUpdate */ | |
718 sqlite3 *db = pParse->db; /* Database connection */ | 720 sqlite3 *db = pParse->db; /* Database connection */ |
719 const char *pVTab = (const char*)sqlite3GetVTable(db, pTab); | 721 const char *pVTab = (const char*)sqlite3GetVTable(db, pTab); |
720 SelectDest dest; | 722 WhereInfo *pWInfo; |
| 723 int nArg = 2 + pTab->nCol; /* Number of arguments to VUpdate */ |
| 724 int regArg; /* First register in VUpdate arg array */ |
| 725 int regRec; /* Register in which to assemble record */ |
| 726 int regRowid; /* Register for ephem table rowid */ |
| 727 int iCsr = pSrc->a[0].iCursor; /* Cursor used for virtual table scan */ |
| 728 int aDummy[2]; /* Unused arg for sqlite3WhereOkOnePass() */ |
| 729 int bOnePass; /* True to use onepass strategy */ |
| 730 int addr; /* Address of OP_OpenEphemeral */ |
721 | 731 |
722 /* Construct the SELECT statement that will find the new values for | 732 /* Allocate nArg registers to martial the arguments to VUpdate. Then |
723 ** all updated rows. | 733 ** create and open the ephemeral table in which the records created from |
724 */ | 734 ** these arguments will be temporarily stored. */ |
725 pEList = sqlite3ExprListAppend(pParse, 0, sqlite3Expr(db, TK_ID, "_rowid_")); | 735 assert( v ); |
| 736 ephemTab = pParse->nTab++; |
| 737 addr= sqlite3VdbeAddOp2(v, OP_OpenEphemeral, ephemTab, nArg); |
| 738 regArg = pParse->nMem + 1; |
| 739 pParse->nMem += nArg; |
| 740 regRec = ++pParse->nMem; |
| 741 regRowid = ++pParse->nMem; |
| 742 |
| 743 /* Start scanning the virtual table */ |
| 744 pWInfo = sqlite3WhereBegin(pParse, pSrc, pWhere, 0,0,WHERE_ONEPASS_DESIRED,0); |
| 745 if( pWInfo==0 ) return; |
| 746 |
| 747 /* Populate the argument registers. */ |
| 748 sqlite3VdbeAddOp2(v, OP_Rowid, iCsr, regArg); |
726 if( pRowid ){ | 749 if( pRowid ){ |
727 pEList = sqlite3ExprListAppend(pParse, pEList, | 750 sqlite3ExprCode(pParse, pRowid, regArg+1); |
728 sqlite3ExprDup(db, pRowid, 0)); | 751 }else{ |
| 752 sqlite3VdbeAddOp2(v, OP_Rowid, iCsr, regArg+1); |
729 } | 753 } |
730 assert( pTab->iPKey<0 ); | |
731 for(i=0; i<pTab->nCol; i++){ | 754 for(i=0; i<pTab->nCol; i++){ |
732 if( aXRef[i]>=0 ){ | 755 if( aXRef[i]>=0 ){ |
733 pExpr = sqlite3ExprDup(db, pChanges->a[aXRef[i]].pExpr, 0); | 756 sqlite3ExprCode(pParse, pChanges->a[aXRef[i]].pExpr, regArg+2+i); |
734 }else{ | 757 }else{ |
735 pExpr = sqlite3Expr(db, TK_ID, pTab->aCol[i].zName); | 758 sqlite3VdbeAddOp3(v, OP_VColumn, iCsr, i, regArg+2+i); |
736 } | 759 } |
737 pEList = sqlite3ExprListAppend(pParse, pEList, pExpr); | |
738 } | 760 } |
739 pSelect = sqlite3SelectNew(pParse, pEList, pSrc, pWhere, 0, 0, 0, 0, 0, 0); | |
740 | |
741 /* Create the ephemeral table into which the update results will | |
742 ** be stored. | |
743 */ | |
744 assert( v ); | |
745 ephemTab = pParse->nTab++; | |
746 sqlite3VdbeAddOp2(v, OP_OpenEphemeral, ephemTab, pTab->nCol+1+(pRowid!=0)); | |
747 sqlite3VdbeChangeP5(v, BTREE_UNORDERED); | |
748 | 761 |
749 /* fill the ephemeral table | 762 bOnePass = sqlite3WhereOkOnePass(pWInfo, aDummy); |
750 */ | |
751 sqlite3SelectDestInit(&dest, SRT_Table, ephemTab); | |
752 sqlite3Select(pParse, pSelect, &dest); | |
753 | 763 |
754 /* Generate code to scan the ephemeral table and call VUpdate. */ | 764 if( bOnePass ){ |
755 iReg = ++pParse->nMem; | 765 /* If using the onepass strategy, no-op out the OP_OpenEphemeral coded |
756 pParse->nMem += pTab->nCol+1; | 766 ** above. Also, if this is a top-level parse (not a trigger), clear the |
757 addr = sqlite3VdbeAddOp2(v, OP_Rewind, ephemTab, 0); VdbeCoverage(v); | 767 ** multi-write flag so that the VM does not open a statement journal */ |
758 sqlite3VdbeAddOp3(v, OP_Column, ephemTab, 0, iReg); | 768 sqlite3VdbeChangeToNoop(v, addr); |
759 sqlite3VdbeAddOp3(v, OP_Column, ephemTab, (pRowid?1:0), iReg+1); | 769 if( sqlite3IsToplevel(pParse) ){ |
760 for(i=0; i<pTab->nCol; i++){ | 770 pParse->isMultiWrite = 0; |
761 sqlite3VdbeAddOp3(v, OP_Column, ephemTab, i+1+(pRowid!=0), iReg+2+i); | 771 } |
| 772 }else{ |
| 773 /* Create a record from the argument register contents and insert it into |
| 774 ** the ephemeral table. */ |
| 775 sqlite3VdbeAddOp3(v, OP_MakeRecord, regArg, nArg, regRec); |
| 776 sqlite3VdbeAddOp2(v, OP_NewRowid, ephemTab, regRowid); |
| 777 sqlite3VdbeAddOp3(v, OP_Insert, ephemTab, regRec, regRowid); |
| 778 } |
| 779 |
| 780 |
| 781 if( bOnePass==0 ){ |
| 782 /* End the virtual table scan */ |
| 783 sqlite3WhereEnd(pWInfo); |
| 784 |
| 785 /* Begin scannning through the ephemeral table. */ |
| 786 addr = sqlite3VdbeAddOp1(v, OP_Rewind, ephemTab); VdbeCoverage(v); |
| 787 |
| 788 /* Extract arguments from the current row of the ephemeral table and |
| 789 ** invoke the VUpdate method. */ |
| 790 for(i=0; i<nArg; i++){ |
| 791 sqlite3VdbeAddOp3(v, OP_Column, ephemTab, i, regArg+i); |
| 792 } |
762 } | 793 } |
763 sqlite3VtabMakeWritable(pParse, pTab); | 794 sqlite3VtabMakeWritable(pParse, pTab); |
764 sqlite3VdbeAddOp4(v, OP_VUpdate, 0, pTab->nCol+2, iReg, pVTab, P4_VTAB); | 795 sqlite3VdbeAddOp4(v, OP_VUpdate, 0, nArg, regArg, pVTab, P4_VTAB); |
765 sqlite3VdbeChangeP5(v, onError==OE_Default ? OE_Abort : onError); | 796 sqlite3VdbeChangeP5(v, onError==OE_Default ? OE_Abort : onError); |
766 sqlite3MayAbort(pParse); | 797 sqlite3MayAbort(pParse); |
767 sqlite3VdbeAddOp2(v, OP_Next, ephemTab, addr+1); VdbeCoverage(v); | |
768 sqlite3VdbeJumpHere(v, addr); | |
769 sqlite3VdbeAddOp2(v, OP_Close, ephemTab, 0); | |
770 | 798 |
771 /* Cleanup */ | 799 /* End of the ephemeral table scan. Or, if using the onepass strategy, |
772 sqlite3SelectDelete(db, pSelect); | 800 ** jump to here if the scan visited zero rows. */ |
| 801 if( bOnePass==0 ){ |
| 802 sqlite3VdbeAddOp2(v, OP_Next, ephemTab, addr+1); VdbeCoverage(v); |
| 803 sqlite3VdbeJumpHere(v, addr); |
| 804 sqlite3VdbeAddOp2(v, OP_Close, ephemTab, 0); |
| 805 }else{ |
| 806 sqlite3WhereEnd(pWInfo); |
| 807 } |
773 } | 808 } |
774 #endif /* SQLITE_OMIT_VIRTUALTABLE */ | 809 #endif /* SQLITE_OMIT_VIRTUALTABLE */ |
OLD | NEW |