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