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.190 2008/12/10 21:19:57 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 /* Begin the database scan 396 */ 397 pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, 0, 0); 398 if( pWInfo==0 ) goto delete_from_cleanup; 399 400 /* Remember the rowid of every item to be deleted. 401 */ 402 sqlite3VdbeAddOp2(v, IsVirtual(pTab) ? OP_VRowid : OP_Rowid, iCur, iRowid); 403 sqlite3VdbeAddOp2(v, OP_RowSetAdd, iRowSet, iRowid); 404 if( db->flags & SQLITE_CountRows ){ 405 sqlite3VdbeAddOp2(v, OP_AddImm, memCnt, 1); 406 } 407 408 /* End the database scan loop. 409 */ 410 sqlite3WhereEnd(pWInfo); 411 412 /* Open the pseudo-table used to store OLD if there are triggers. 413 */ 414 if( triggers_exist ){ 415 sqlite3VdbeAddOp2(v, OP_SetNumColumns, 0, pTab->nCol); 416 sqlite3VdbeAddOp1(v, OP_OpenPseudo, oldIdx); 417 } 418 419 /* Delete every item whose key was written to the list during the 420 ** database scan. We have to delete items after the scan is complete 421 ** because deleting an item can change the scan order. 422 */ 423 end = sqlite3VdbeMakeLabel(v); 424 425 if( !isView ){ 426 /* Open cursors for the table we are deleting from and 427 ** all its indices. 428 */ 429 sqlite3OpenTableAndIndices(pParse, pTab, iCur, OP_OpenWrite); 430 } 431 432 /* This is the beginning of the delete loop. If a trigger encounters 433 ** an IGNORE constraint, it jumps back to here. 434 */ 435 if( triggers_exist ){ 436 sqlite3VdbeResolveLabel(v, addr); 437 } 438 addr = sqlite3VdbeAddOp3(v, OP_RowSetRead, iRowSet, end, iRowid); 439 440 if( triggers_exist ){ 441 int iData = ++pParse->nMem; /* For storing row data of OLD table */ 442 443 /* If the record is no longer present in the table, jump to the 444 ** next iteration of the loop through the contents of the fifo. 445 */ 446 sqlite3VdbeAddOp3(v, OP_NotExists, iCur, addr, iRowid); 447 448 /* Populate the OLD.* pseudo-table */ 449 if( old_col_mask ){ 450 sqlite3VdbeAddOp2(v, OP_RowData, iCur, iData); 451 }else{ 452 sqlite3VdbeAddOp2(v, OP_Null, 0, iData); 453 } 454 sqlite3VdbeAddOp3(v, OP_Insert, oldIdx, iData, iRowid); 455 456 /* Jump back and run the BEFORE triggers */ 457 sqlite3VdbeAddOp2(v, OP_Goto, 0, iBeginBeforeTrigger); 458 sqlite3VdbeJumpHere(v, iEndBeforeTrigger); 459 } 460 461 if( !isView ){ 462 /* Delete the row */ 463 #ifndef SQLITE_OMIT_VIRTUALTABLE 464 if( IsVirtual(pTab) ){ 465 const char *pVtab = (const char *)pTab->pVtab; 466 sqlite3VtabMakeWritable(pParse, pTab); 467 sqlite3VdbeAddOp4(v, OP_VUpdate, 0, 1, iRowid, pVtab, P4_VTAB); 468 }else 469 #endif 470 { 471 sqlite3GenerateRowDelete(pParse, pTab, iCur, iRowid, pParse->nested==0); 472 } 473 } 474 475 /* If there are row triggers, close all cursors then invoke 476 ** the AFTER triggers 477 */ 478 if( triggers_exist ){ 479 /* Jump back and run the AFTER triggers */ 480 sqlite3VdbeAddOp2(v, OP_Goto, 0, iBeginAfterTrigger); 481 sqlite3VdbeJumpHere(v, iEndAfterTrigger); 482 } 483 484 /* End of the delete loop */ 485 sqlite3VdbeAddOp2(v, OP_Goto, 0, addr); 486 sqlite3VdbeResolveLabel(v, end); 487 488 /* Close the cursors after the loop if there are no row triggers */ 489 if( !isView && !IsVirtual(pTab) ){ 490 for(i=1, pIdx=pTab->pIndex; pIdx; i++, pIdx=pIdx->pNext){ 491 sqlite3VdbeAddOp2(v, OP_Close, iCur + i, pIdx->tnum); 492 } 493 sqlite3VdbeAddOp1(v, OP_Close, iCur); 494 } 495 } 496 497 /* 498 ** Return the number of rows that were deleted. If this routine is 499 ** generating code because of a call to sqlite3NestedParse(), do not 500 ** invoke the callback function. 501 */ 502 if( db->flags & SQLITE_CountRows && pParse->nested==0 && !pParse->trigStack ){ 503 sqlite3VdbeAddOp2(v, OP_ResultRow, memCnt, 1); 504 sqlite3VdbeSetNumCols(v, 1); 505 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "rows deleted", SQLITE_STATIC); 506 } 507 508 delete_from_cleanup: 509 sqlite3AuthContextPop(&sContext); 510 sqlite3SrcListDelete(db, pTabList); 511 sqlite3ExprDelete(db, pWhere); 512 return; 513 } 514 515 /* 516 ** This routine generates VDBE code that causes a single row of a 517 ** single table to be deleted. 518 ** 519 ** The VDBE must be in a particular state when this routine is called. 520 ** These are the requirements: 521 ** 522 ** 1. A read/write cursor pointing to pTab, the table containing the row 523 ** to be deleted, must be opened as cursor number "base". 524 ** 525 ** 2. Read/write cursors for all indices of pTab must be open as 526 ** cursor number base+i for the i-th index. 527 ** 528 ** 3. The record number of the row to be deleted must be stored in 529 ** memory cell iRowid. 530 ** 531 ** This routine pops the top of the stack to remove the record number 532 ** and then generates code to remove both the table record and all index 533 ** entries that point to that record. 534 */ 535 void sqlite3GenerateRowDelete( 536 Parse *pParse, /* Parsing context */ 537 Table *pTab, /* Table containing the row to be deleted */ 538 int iCur, /* Cursor number for the table */ 539 int iRowid, /* Memory cell that contains the rowid to delete */ 540 int count /* Increment the row change counter */ 541 ){ 542 int addr; 543 Vdbe *v; 544 545 v = pParse->pVdbe; 546 addr = sqlite3VdbeAddOp3(v, OP_NotExists, iCur, 0, iRowid); 547 sqlite3GenerateRowIndexDelete(pParse, pTab, iCur, 0); 548 sqlite3VdbeAddOp2(v, OP_Delete, iCur, (count?OPFLAG_NCHANGE:0)); 549 if( count ){ 550 sqlite3VdbeChangeP4(v, -1, pTab->zName, P4_STATIC); 551 } 552 sqlite3VdbeJumpHere(v, addr); 553 } 554 555 /* 556 ** This routine generates VDBE code that causes the deletion of all 557 ** index entries associated with a single row of a single table. 558 ** 559 ** The VDBE must be in a particular state when this routine is called. 560 ** These are the requirements: 561 ** 562 ** 1. A read/write cursor pointing to pTab, the table containing the row 563 ** to be deleted, must be opened as cursor number "iCur". 564 ** 565 ** 2. Read/write cursors for all indices of pTab must be open as 566 ** cursor number iCur+i for the i-th index. 567 ** 568 ** 3. The "iCur" cursor must be pointing to the row that is to be 569 ** deleted. 570 */ 571 void sqlite3GenerateRowIndexDelete( 572 Parse *pParse, /* Parsing and code generating context */ 573 Table *pTab, /* Table containing the row to be deleted */ 574 int iCur, /* Cursor number for the table */ 575 int *aRegIdx /* Only delete if aRegIdx!=0 && aRegIdx[i]>0 */ 576 ){ 577 int i; 578 Index *pIdx; 579 int r1; 580 581 for(i=1, pIdx=pTab->pIndex; pIdx; i++, pIdx=pIdx->pNext){ 582 if( aRegIdx!=0 && aRegIdx[i-1]==0 ) continue; 583 r1 = sqlite3GenerateIndexKey(pParse, pIdx, iCur, 0, 0); 584 sqlite3VdbeAddOp3(pParse->pVdbe, OP_IdxDelete, iCur+i, r1,pIdx->nColumn+1); 585 } 586 } 587 588 /* 589 ** Generate code that will assemble an index key and put it in register 590 ** regOut. The key with be for index pIdx which is an index on pTab. 591 ** iCur is the index of a cursor open on the pTab table and pointing to 592 ** the entry that needs indexing. 593 ** 594 ** Return a register number which is the first in a block of 595 ** registers that holds the elements of the index key. The 596 ** block of registers has already been deallocated by the time 597 ** this routine returns. 598 */ 599 int sqlite3GenerateIndexKey( 600 Parse *pParse, /* Parsing context */ 601 Index *pIdx, /* The index for which to generate a key */ 602 int iCur, /* Cursor number for the pIdx->pTable table */ 603 int regOut, /* Write the new index key to this register */ 604 int doMakeRec /* Run the OP_MakeRecord instruction if true */ 605 ){ 606 Vdbe *v = pParse->pVdbe; 607 int j; 608 Table *pTab = pIdx->pTable; 609 int regBase; 610 int nCol; 611 612 nCol = pIdx->nColumn; 613 regBase = sqlite3GetTempRange(pParse, nCol+1); 614 sqlite3VdbeAddOp2(v, OP_Rowid, iCur, regBase+nCol); 615 for(j=0; j<nCol; j++){ 616 int idx = pIdx->aiColumn[j]; 617 if( idx==pTab->iPKey ){ 618 sqlite3VdbeAddOp2(v, OP_SCopy, regBase+nCol, regBase+j); 619 }else{ 620 sqlite3VdbeAddOp3(v, OP_Column, iCur, idx, regBase+j); 621 sqlite3ColumnDefault(v, pTab, idx); 622 } 623 } 624 if( doMakeRec ){ 625 sqlite3VdbeAddOp3(v, OP_MakeRecord, regBase, nCol+1, regOut); 626 sqlite3IndexAffinityStr(v, pIdx); 627 sqlite3ExprCacheAffinityChange(pParse, regBase, nCol+1); 628 } 629 sqlite3ReleaseTempRange(pParse, regBase, nCol+1); 630 return regBase; 631 } 632 633 /* Make sure "isView" gets undefined in case this file becomes part of 634 ** the amalgamation - so that subsequent files do not see isView as a 635 ** macro. */ 636 #undef isView 637