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 ** in order to generate code for DELETE FROM statements. 14 ** 15 ** $Id: delete.c,v 1.204 2009/06/23 20:28:54 drh Exp $ 16 */ 17 #include "sqliteInt.h" 18 19 /* 20 ** Look up every table that is named in pSrc. If any table is not found, 21 ** add an error message to pParse->zErrMsg and return NULL. If all tables 22 ** are found, return a pointer to the last table. 23 */ 24 Table *sqlite3SrcListLookup(Parse *pParse, SrcList *pSrc){ 25 struct SrcList_item *pItem = pSrc->a; 26 Table *pTab; 27 assert( pItem && pSrc->nSrc==1 ); 28 pTab = sqlite3LocateTable(pParse, 0, pItem->zName, pItem->zDatabase); 29 sqlite3DeleteTable(pItem->pTab); 30 pItem->pTab = pTab; 31 if( pTab ){ 32 pTab->nRef++; 33 } 34 if( sqlite3IndexedByLookup(pParse, pItem) ){ 35 pTab = 0; 36 } 37 return pTab; 38 } 39 40 /* 41 ** Check to make sure the given table is writable. If it is not 42 ** writable, generate an error message and return 1. If it is 43 ** writable return 0; 44 */ 45 int sqlite3IsReadOnly(Parse *pParse, Table *pTab, int viewOk){ 46 if( ((pTab->tabFlags & TF_Readonly)!=0 47 && (pParse->db->flags & SQLITE_WriteSchema)==0 48 && pParse->nested==0) 49 #ifndef SQLITE_OMIT_VIRTUALTABLE 50 || (pTab->pMod && pTab->pMod->pModule->xUpdate==0) 51 #endif 52 ){ 53 sqlite3ErrorMsg(pParse, "table %s may not be modified", pTab->zName); 54 return 1; 55 } 56 #ifndef SQLITE_OMIT_VIEW 57 if( !viewOk && pTab->pSelect ){ 58 sqlite3ErrorMsg(pParse,"cannot modify %s because it is a view",pTab->zName); 59 return 1; 60 } 61 #endif 62 return 0; 63 } 64 65 66 #if !defined(SQLITE_OMIT_VIEW) && !defined(SQLITE_OMIT_TRIGGER) 67 /* 68 ** Evaluate a view and store its result in an ephemeral table. The 69 ** pWhere argument is an optional WHERE clause that restricts the 70 ** set of rows in the view that are to be added to the ephemeral table. 71 */ 72 void sqlite3MaterializeView( 73 Parse *pParse, /* Parsing context */ 74 Table *pView, /* View definition */ 75 Expr *pWhere, /* Optional WHERE clause to be added */ 76 int iCur /* Cursor number for ephemerial table */ 77 ){ 78 SelectDest dest; 79 Select *pDup; 80 sqlite3 *db = pParse->db; 81 82 pDup = sqlite3SelectDup(db, pView->pSelect, 0); 83 if( pWhere ){ 84 SrcList *pFrom; 85 86 pWhere = sqlite3ExprDup(db, pWhere, 0); 87 pFrom = sqlite3SrcListAppend(db, 0, 0, 0); 88 if( pFrom ){ 89 assert( pFrom->nSrc==1 ); 90 pFrom->a[0].zAlias = sqlite3DbStrDup(db, pView->zName); 91 pFrom->a[0].pSelect = pDup; 92 assert( pFrom->a[0].pOn==0 ); 93 assert( pFrom->a[0].pUsing==0 ); 94 }else{ 95 sqlite3SelectDelete(db, pDup); 96 } 97 pDup = sqlite3SelectNew(pParse, 0, pFrom, pWhere, 0, 0, 0, 0, 0, 0); 98 } 99 sqlite3SelectDestInit(&dest, SRT_EphemTab, iCur); 100 sqlite3Select(pParse, pDup, &dest); 101 sqlite3SelectDelete(db, pDup); 102 } 103 #endif /* !defined(SQLITE_OMIT_VIEW) && !defined(SQLITE_OMIT_TRIGGER) */ 104 105 #if defined(SQLITE_ENABLE_UPDATE_DELETE_LIMIT) && !defined(SQLITE_OMIT_SUBQUERY) 106 /* 107 ** Generate an expression tree to implement the WHERE, ORDER BY, 108 ** and LIMIT/OFFSET portion of DELETE and UPDATE statements. 109 ** 110 ** DELETE FROM table_wxyz WHERE a<5 ORDER BY a LIMIT 1; 111 ** \__________________________/ 112 ** pLimitWhere (pInClause) 113 */ 114 Expr *sqlite3LimitWhere( 115 Parse *pParse, /* The parser context */ 116 SrcList *pSrc, /* the FROM clause -- which tables to scan */ 117 Expr *pWhere, /* The WHERE clause. May be null */ 118 ExprList *pOrderBy, /* The ORDER BY clause. May be null */ 119 Expr *pLimit, /* The LIMIT clause. May be null */ 120 Expr *pOffset, /* The OFFSET clause. May be null */ 121 char *zStmtType /* Either DELETE or UPDATE. For error messages. */ 122 ){ 123 Expr *pWhereRowid = NULL; /* WHERE rowid .. */ 124 Expr *pInClause = NULL; /* WHERE rowid IN ( select ) */ 125 Expr *pSelectRowid = NULL; /* SELECT rowid ... */ 126 ExprList *pEList = NULL; /* Expression list contaning only pSelectRowid */ 127 SrcList *pSelectSrc = NULL; /* SELECT rowid FROM x ... (dup of pSrc) */ 128 Select *pSelect = NULL; /* Complete SELECT tree */ 129 130 /* Check that there isn't an ORDER BY without a LIMIT clause. 131 */ 132 if( pOrderBy && (pLimit == 0) ) { 133 sqlite3ErrorMsg(pParse, "ORDER BY without LIMIT on %s", zStmtType); 134 pParse->parseError = 1; 135 goto limit_where_cleanup_2; 136 } 137 138 /* We only need to generate a select expression if there 139 ** is a limit/offset term to enforce. 140 */ 141 if( pLimit == 0 ) { 142 /* if pLimit is null, pOffset will always be null as well. */ 143 assert( pOffset == 0 ); 144 return pWhere; 145 } 146 147 /* Generate a select expression tree to enforce the limit/offset 148 ** term for the DELETE or UPDATE statement. For example: 149 ** DELETE FROM table_a WHERE col1=1 ORDER BY col2 LIMIT 1 OFFSET 1 150 ** becomes: 151 ** DELETE FROM table_a WHERE rowid IN ( 152 ** SELECT rowid FROM table_a WHERE col1=1 ORDER BY col2 LIMIT 1 OFFSET 1 153 ** ); 154 */ 155 156 pSelectRowid = sqlite3PExpr(pParse, TK_ROW, 0, 0, 0); 157 if( pSelectRowid == 0 ) goto limit_where_cleanup_2; 158 pEList = sqlite3ExprListAppend(pParse, 0, pSelectRowid); 159 if( pEList == 0 ) goto limit_where_cleanup_2; 160 161 /* duplicate the FROM clause as it is needed by both the DELETE/UPDATE tree 162 ** and the SELECT subtree. */ 163 pSelectSrc = sqlite3SrcListDup(pParse->db, pSrc, 0); 164 if( pSelectSrc == 0 ) { 165 sqlite3ExprListDelete(pParse->db, pEList); 166 goto limit_where_cleanup_2; 167 } 168 169 /* generate the SELECT expression tree. */ 170 pSelect = sqlite3SelectNew(pParse,pEList,pSelectSrc,pWhere,0,0, 171 pOrderBy,0,pLimit,pOffset); 172 if( pSelect == 0 ) return 0; 173 174 /* now generate the new WHERE rowid IN clause for the DELETE/UDPATE */ 175 pWhereRowid = sqlite3PExpr(pParse, TK_ROW, 0, 0, 0); 176 if( pWhereRowid == 0 ) goto limit_where_cleanup_1; 177 pInClause = sqlite3PExpr(pParse, TK_IN, pWhereRowid, 0, 0); 178 if( pInClause == 0 ) goto limit_where_cleanup_1; 179 180 pInClause->x.pSelect = pSelect; 181 pInClause->flags |= EP_xIsSelect; 182 sqlite3ExprSetHeight(pParse, pInClause); 183 return pInClause; 184 185 /* something went wrong. clean up anything allocated. */ 186 limit_where_cleanup_1: 187 sqlite3SelectDelete(pParse->db, pSelect); 188 return 0; 189 190 limit_where_cleanup_2: 191 sqlite3ExprDelete(pParse->db, pWhere); 192 sqlite3ExprListDelete(pParse->db, pOrderBy); 193 sqlite3ExprDelete(pParse->db, pLimit); 194 sqlite3ExprDelete(pParse->db, pOffset); 195 return 0; 196 } 197 #endif /* defined(SQLITE_ENABLE_UPDATE_DELETE_LIMIT) && !defined(SQLITE_OMIT_SUBQUERY) */ 198 199 /* 200 ** Generate code for a DELETE FROM statement. 201 ** 202 ** DELETE FROM table_wxyz WHERE a<5 AND b NOT NULL; 203 ** \________/ \________________/ 204 ** pTabList pWhere 205 */ 206 void sqlite3DeleteFrom( 207 Parse *pParse, /* The parser context */ 208 SrcList *pTabList, /* The table from which we should delete things */ 209 Expr *pWhere /* The WHERE clause. May be null */ 210 ){ 211 Vdbe *v; /* The virtual database engine */ 212 Table *pTab; /* The table from which records will be deleted */ 213 const char *zDb; /* Name of database holding pTab */ 214 int end, addr = 0; /* A couple addresses of generated code */ 215 int i; /* Loop counter */ 216 WhereInfo *pWInfo; /* Information about the WHERE clause */ 217 Index *pIdx; /* For looping over indices of the table */ 218 int iCur; /* VDBE Cursor number for pTab */ 219 sqlite3 *db; /* Main database structure */ 220 AuthContext sContext; /* Authorization context */ 221 int oldIdx = -1; /* Cursor for the OLD table of AFTER triggers */ 222 NameContext sNC; /* Name context to resolve expressions in */ 223 int iDb; /* Database number */ 224 int memCnt = -1; /* Memory cell used for change counting */ 225 int rcauth; /* Value returned by authorization callback */ 226 227 #ifndef SQLITE_OMIT_TRIGGER 228 int isView; /* True if attempting to delete from a view */ 229 Trigger *pTrigger; /* List of table triggers, if required */ 230 #endif 231 int iBeginAfterTrigger = 0; /* Address of after trigger program */ 232 int iEndAfterTrigger = 0; /* Exit of after trigger program */ 233 int iBeginBeforeTrigger = 0; /* Address of before trigger program */ 234 int iEndBeforeTrigger = 0; /* Exit of before trigger program */ 235 u32 old_col_mask = 0; /* Mask of OLD.* columns in use */ 236 237 sContext.pParse = 0; 238 db = pParse->db; 239 if( pParse->nErr || db->mallocFailed ){ 240 goto delete_from_cleanup; 241 } 242 assert( pTabList->nSrc==1 ); 243 244 /* Locate the table which we want to delete. This table has to be 245 ** put in an SrcList structure because some of the subroutines we 246 ** will be calling are designed to work with multiple tables and expect 247 ** an SrcList* parameter instead of just a Table* parameter. 248 */ 249 pTab = sqlite3SrcListLookup(pParse, pTabList); 250 if( pTab==0 ) goto delete_from_cleanup; 251 252 /* Figure out if we have any triggers and if the table being 253 ** deleted from is a view 254 */ 255 #ifndef SQLITE_OMIT_TRIGGER 256 pTrigger = sqlite3TriggersExist(pParse, pTab, TK_DELETE, 0, 0); 257 isView = pTab->pSelect!=0; 258 #else 259 # define pTrigger 0 260 # define isView 0 261 #endif 262 #ifdef SQLITE_OMIT_VIEW 263 # undef isView 264 # define isView 0 265 #endif 266 267 if( sqlite3IsReadOnly(pParse, pTab, (pTrigger?1:0)) ){ 268 goto delete_from_cleanup; 269 } 270 iDb = sqlite3SchemaToIndex(db, pTab->pSchema); 271 assert( iDb<db->nDb ); 272 zDb = db->aDb[iDb].zName; 273 rcauth = sqlite3AuthCheck(pParse, SQLITE_DELETE, pTab->zName, 0, zDb); 274 assert( rcauth==SQLITE_OK || rcauth==SQLITE_DENY || rcauth==SQLITE_IGNORE ); 275 if( rcauth==SQLITE_DENY ){ 276 goto delete_from_cleanup; 277 } 278 assert(!isView || pTrigger); 279 280 /* If pTab is really a view, make sure it has been initialized. 281 */ 282 if( sqlite3ViewGetColumnNames(pParse, pTab) ){ 283 goto delete_from_cleanup; 284 } 285 286 /* Allocate a cursor used to store the old.* data for a trigger. 287 */ 288 if( pTrigger ){ 289 oldIdx = pParse->nTab++; 290 } 291 292 /* Assign cursor number to the table and all its indices. 293 */ 294 assert( pTabList->nSrc==1 ); 295 iCur = pTabList->a[0].iCursor = pParse->nTab++; 296 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){ 297 pParse->nTab++; 298 } 299 300 /* Start the view context 301 */ 302 if( isView ){ 303 sqlite3AuthContextPush(pParse, &sContext, pTab->zName); 304 } 305 306 /* Begin generating code. 307 */ 308 v = sqlite3GetVdbe(pParse); 309 if( v==0 ){ 310 goto delete_from_cleanup; 311 } 312 if( pParse->nested==0 ) sqlite3VdbeCountChanges(v); 313 sqlite3BeginWriteOperation(pParse, (pTrigger?1:0), iDb); 314 315 if( pTrigger ){ 316 int orconf = ((pParse->trigStack)?pParse->trigStack->orconf:OE_Default); 317 int iGoto = sqlite3VdbeAddOp0(v, OP_Goto); 318 addr = sqlite3VdbeMakeLabel(v); 319 320 iBeginBeforeTrigger = sqlite3VdbeCurrentAddr(v); 321 (void)sqlite3CodeRowTrigger(pParse, pTrigger, TK_DELETE, 0, 322 TRIGGER_BEFORE, pTab, -1, oldIdx, orconf, addr, &old_col_mask, 0); 323 iEndBeforeTrigger = sqlite3VdbeAddOp0(v, OP_Goto); 324 325 iBeginAfterTrigger = sqlite3VdbeCurrentAddr(v); 326 (void)sqlite3CodeRowTrigger(pParse, pTrigger, TK_DELETE, 0, 327 TRIGGER_AFTER, pTab, -1, oldIdx, orconf, addr, &old_col_mask, 0); 328 iEndAfterTrigger = sqlite3VdbeAddOp0(v, OP_Goto); 329 330 sqlite3VdbeJumpHere(v, iGoto); 331 } 332 333 /* If we are trying to delete from a view, realize that view into 334 ** a ephemeral table. 335 */ 336 #if !defined(SQLITE_OMIT_VIEW) && !defined(SQLITE_OMIT_TRIGGER) 337 if( isView ){ 338 sqlite3MaterializeView(pParse, pTab, pWhere, iCur); 339 } 340 #endif 341 342 /* Resolve the column names in the WHERE clause. 343 */ 344 memset(&sNC, 0, sizeof(sNC)); 345 sNC.pParse = pParse; 346 sNC.pSrcList = pTabList; 347 if( sqlite3ResolveExprNames(&sNC, pWhere) ){ 348 goto delete_from_cleanup; 349 } 350 351 /* Initialize the counter of the number of rows deleted, if 352 ** we are counting rows. 353 */ 354 if( db->flags & SQLITE_CountRows ){ 355 memCnt = ++pParse->nMem; 356 sqlite3VdbeAddOp2(v, OP_Integer, 0, memCnt); 357 } 358 359 #ifndef SQLITE_OMIT_TRUNCATE_OPTIMIZATION 360 /* Special case: A DELETE without a WHERE clause deletes everything. 361 ** It is easier just to erase the whole table. Note, however, that 362 ** this means that the row change count will be incorrect. 363 */ 364 if( rcauth==SQLITE_OK && pWhere==0 && !pTrigger && !IsVirtual(pTab) ){ 365 assert( !isView ); 366 sqlite3VdbeAddOp4(v, OP_Clear, pTab->tnum, iDb, memCnt, 367 pTab->zName, P4_STATIC); 368 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){ 369 assert( pIdx->pSchema==pTab->pSchema ); 370 sqlite3VdbeAddOp2(v, OP_Clear, pIdx->tnum, iDb); 371 } 372 }else 373 #endif /* SQLITE_OMIT_TRUNCATE_OPTIMIZATION */ 374 /* The usual case: There is a WHERE clause so we have to scan through 375 ** the table and pick which records to delete. 376 */ 377 { 378 int iRowid = ++pParse->nMem; /* Used for storing rowid values. */ 379 int iRowSet = ++pParse->nMem; /* Register for rowset of rows to delete */ 380 int regRowid; /* Actual register containing rowids */ 381 382 /* Collect rowids of every row to be deleted. 383 */ 384 sqlite3VdbeAddOp2(v, OP_Null, 0, iRowSet); 385 pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere,0,WHERE_DUPLICATES_OK); 386 if( pWInfo==0 ) goto delete_from_cleanup; 387 regRowid = sqlite3ExprCodeGetColumn(pParse, pTab, -1, iCur, iRowid, 0); 388 sqlite3VdbeAddOp2(v, OP_RowSetAdd, iRowSet, regRowid); 389 if( db->flags & SQLITE_CountRows ){ 390 sqlite3VdbeAddOp2(v, OP_AddImm, memCnt, 1); 391 } 392 sqlite3WhereEnd(pWInfo); 393 394 /* Open the pseudo-table used to store OLD if there are triggers. 395 */ 396 if( pTrigger ){ 397 sqlite3VdbeAddOp3(v, OP_OpenPseudo, oldIdx, 0, pTab->nCol); 398 } 399 400 /* Delete every item whose key was written to the list during the 401 ** database scan. We have to delete items after the scan is complete 402 ** because deleting an item can change the scan order. 403 */ 404 end = sqlite3VdbeMakeLabel(v); 405 406 if( !isView ){ 407 /* Open cursors for the table we are deleting from and 408 ** all its indices. 409 */ 410 sqlite3OpenTableAndIndices(pParse, pTab, iCur, OP_OpenWrite); 411 } 412 413 /* This is the beginning of the delete loop. If a trigger encounters 414 ** an IGNORE constraint, it jumps back to here. 415 */ 416 if( pTrigger ){ 417 sqlite3VdbeResolveLabel(v, addr); 418 } 419 addr = sqlite3VdbeAddOp3(v, OP_RowSetRead, iRowSet, end, iRowid); 420 421 if( pTrigger ){ 422 int iData = ++pParse->nMem; /* For storing row data of OLD table */ 423 424 /* If the record is no longer present in the table, jump to the 425 ** next iteration of the loop through the contents of the fifo. 426 */ 427 sqlite3VdbeAddOp3(v, OP_NotExists, iCur, addr, iRowid); 428 429 /* Populate the OLD.* pseudo-table */ 430 if( old_col_mask ){ 431 sqlite3VdbeAddOp2(v, OP_RowData, iCur, iData); 432 }else{ 433 sqlite3VdbeAddOp2(v, OP_Null, 0, iData); 434 } 435 sqlite3VdbeAddOp3(v, OP_Insert, oldIdx, iData, iRowid); 436 437 /* Jump back and run the BEFORE triggers */ 438 sqlite3VdbeAddOp2(v, OP_Goto, 0, iBeginBeforeTrigger); 439 sqlite3VdbeJumpHere(v, iEndBeforeTrigger); 440 } 441 442 if( !isView ){ 443 /* Delete the row */ 444 #ifndef SQLITE_OMIT_VIRTUALTABLE 445 if( IsVirtual(pTab) ){ 446 const char *pVtab = (const char *)pTab->pVtab; 447 sqlite3VtabMakeWritable(pParse, pTab); 448 sqlite3VdbeAddOp4(v, OP_VUpdate, 0, 1, iRowid, pVtab, P4_VTAB); 449 }else 450 #endif 451 { 452 sqlite3GenerateRowDelete(pParse, pTab, iCur, iRowid, pParse->nested==0); 453 } 454 } 455 456 /* If there are row triggers, close all cursors then invoke 457 ** the AFTER triggers 458 */ 459 if( pTrigger ){ 460 /* Jump back and run the AFTER triggers */ 461 sqlite3VdbeAddOp2(v, OP_Goto, 0, iBeginAfterTrigger); 462 sqlite3VdbeJumpHere(v, iEndAfterTrigger); 463 } 464 465 /* End of the delete loop */ 466 sqlite3VdbeAddOp2(v, OP_Goto, 0, addr); 467 sqlite3VdbeResolveLabel(v, end); 468 469 /* Close the cursors after the loop if there are no row triggers */ 470 if( !isView && !IsVirtual(pTab) ){ 471 for(i=1, pIdx=pTab->pIndex; pIdx; i++, pIdx=pIdx->pNext){ 472 sqlite3VdbeAddOp2(v, OP_Close, iCur + i, pIdx->tnum); 473 } 474 sqlite3VdbeAddOp1(v, OP_Close, iCur); 475 } 476 } 477 478 /* Update the sqlite_sequence table by storing the content of the 479 ** maximum rowid counter values recorded while inserting into 480 ** autoincrement tables. 481 */ 482 if( pParse->nested==0 && pParse->trigStack==0 ){ 483 sqlite3AutoincrementEnd(pParse); 484 } 485 486 /* 487 ** Return the number of rows that were deleted. If this routine is 488 ** generating code because of a call to sqlite3NestedParse(), do not 489 ** invoke the callback function. 490 */ 491 if( db->flags & SQLITE_CountRows && pParse->nested==0 && !pParse->trigStack ){ 492 sqlite3VdbeAddOp2(v, OP_ResultRow, memCnt, 1); 493 sqlite3VdbeSetNumCols(v, 1); 494 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "rows deleted", SQLITE_STATIC); 495 } 496 497 delete_from_cleanup: 498 sqlite3AuthContextPop(&sContext); 499 sqlite3SrcListDelete(db, pTabList); 500 sqlite3ExprDelete(db, pWhere); 501 return; 502 } 503 504 /* 505 ** This routine generates VDBE code that causes a single row of a 506 ** single table to be deleted. 507 ** 508 ** The VDBE must be in a particular state when this routine is called. 509 ** These are the requirements: 510 ** 511 ** 1. A read/write cursor pointing to pTab, the table containing the row 512 ** to be deleted, must be opened as cursor number "base". 513 ** 514 ** 2. Read/write cursors for all indices of pTab must be open as 515 ** cursor number base+i for the i-th index. 516 ** 517 ** 3. The record number of the row to be deleted must be stored in 518 ** memory cell iRowid. 519 ** 520 ** This routine pops the top of the stack to remove the record number 521 ** and then generates code to remove both the table record and all index 522 ** entries that point to that record. 523 */ 524 void sqlite3GenerateRowDelete( 525 Parse *pParse, /* Parsing context */ 526 Table *pTab, /* Table containing the row to be deleted */ 527 int iCur, /* Cursor number for the table */ 528 int iRowid, /* Memory cell that contains the rowid to delete */ 529 int count /* Increment the row change counter */ 530 ){ 531 int addr; 532 Vdbe *v; 533 534 v = pParse->pVdbe; 535 addr = sqlite3VdbeAddOp3(v, OP_NotExists, iCur, 0, iRowid); 536 sqlite3GenerateRowIndexDelete(pParse, pTab, iCur, 0); 537 sqlite3VdbeAddOp2(v, OP_Delete, iCur, (count?OPFLAG_NCHANGE:0)); 538 if( count ){ 539 sqlite3VdbeChangeP4(v, -1, pTab->zName, P4_STATIC); 540 } 541 sqlite3VdbeJumpHere(v, addr); 542 } 543 544 /* 545 ** This routine generates VDBE code that causes the deletion of all 546 ** index entries associated with a single row of a single table. 547 ** 548 ** The VDBE must be in a particular state when this routine is called. 549 ** These are the requirements: 550 ** 551 ** 1. A read/write cursor pointing to pTab, the table containing the row 552 ** to be deleted, must be opened as cursor number "iCur". 553 ** 554 ** 2. Read/write cursors for all indices of pTab must be open as 555 ** cursor number iCur+i for the i-th index. 556 ** 557 ** 3. The "iCur" cursor must be pointing to the row that is to be 558 ** deleted. 559 */ 560 void sqlite3GenerateRowIndexDelete( 561 Parse *pParse, /* Parsing and code generating context */ 562 Table *pTab, /* Table containing the row to be deleted */ 563 int iCur, /* Cursor number for the table */ 564 int *aRegIdx /* Only delete if aRegIdx!=0 && aRegIdx[i]>0 */ 565 ){ 566 int i; 567 Index *pIdx; 568 int r1; 569 570 for(i=1, pIdx=pTab->pIndex; pIdx; i++, pIdx=pIdx->pNext){ 571 if( aRegIdx!=0 && aRegIdx[i-1]==0 ) continue; 572 r1 = sqlite3GenerateIndexKey(pParse, pIdx, iCur, 0, 0); 573 sqlite3VdbeAddOp3(pParse->pVdbe, OP_IdxDelete, iCur+i, r1,pIdx->nColumn+1); 574 } 575 } 576 577 /* 578 ** Generate code that will assemble an index key and put it in register 579 ** regOut. The key with be for index pIdx which is an index on pTab. 580 ** iCur is the index of a cursor open on the pTab table and pointing to 581 ** the entry that needs indexing. 582 ** 583 ** Return a register number which is the first in a block of 584 ** registers that holds the elements of the index key. The 585 ** block of registers has already been deallocated by the time 586 ** this routine returns. 587 */ 588 int sqlite3GenerateIndexKey( 589 Parse *pParse, /* Parsing context */ 590 Index *pIdx, /* The index for which to generate a key */ 591 int iCur, /* Cursor number for the pIdx->pTable table */ 592 int regOut, /* Write the new index key to this register */ 593 int doMakeRec /* Run the OP_MakeRecord instruction if true */ 594 ){ 595 Vdbe *v = pParse->pVdbe; 596 int j; 597 Table *pTab = pIdx->pTable; 598 int regBase; 599 int nCol; 600 601 nCol = pIdx->nColumn; 602 regBase = sqlite3GetTempRange(pParse, nCol+1); 603 sqlite3VdbeAddOp2(v, OP_Rowid, iCur, regBase+nCol); 604 for(j=0; j<nCol; j++){ 605 int idx = pIdx->aiColumn[j]; 606 if( idx==pTab->iPKey ){ 607 sqlite3VdbeAddOp2(v, OP_SCopy, regBase+nCol, regBase+j); 608 }else{ 609 sqlite3VdbeAddOp3(v, OP_Column, iCur, idx, regBase+j); 610 sqlite3ColumnDefault(v, pTab, idx); 611 } 612 } 613 if( doMakeRec ){ 614 sqlite3VdbeAddOp3(v, OP_MakeRecord, regBase, nCol+1, regOut); 615 sqlite3IndexAffinityStr(v, pIdx); 616 sqlite3ExprCacheAffinityChange(pParse, regBase, nCol+1); 617 } 618 sqlite3ReleaseTempRange(pParse, regBase, nCol+1); 619 return regBase; 620 } 621 622 /* Make sure "isView" gets undefined in case this file becomes part of 623 ** the amalgamation - so that subsequent files do not see isView as a 624 ** macro. */ 625 #undef isView 626