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