xref: /sqlite-3.40.0/src/delete.c (revision 41ce47c4)
1cce7d176Sdrh /*
2b19a2bc6Sdrh ** 2001 September 15
3cce7d176Sdrh **
4b19a2bc6Sdrh ** The author disclaims copyright to this source code.  In place of
5b19a2bc6Sdrh ** a legal notice, here is a blessing:
6cce7d176Sdrh **
7b19a2bc6Sdrh **    May you do good and not evil.
8b19a2bc6Sdrh **    May you find forgiveness for yourself and forgive others.
9b19a2bc6Sdrh **    May you share freely, never taking more than you give.
10cce7d176Sdrh **
11cce7d176Sdrh *************************************************************************
12cce7d176Sdrh ** This file contains C code routines that are called by the parser
1323bf66d6Sdrh ** in order to generate code for DELETE FROM statements.
14cce7d176Sdrh */
15cce7d176Sdrh #include "sqliteInt.h"
16cce7d176Sdrh 
17a76b5dfcSdrh /*
180bfc2d1cSdrh ** While a SrcList can in general represent multiple tables and subqueries
190bfc2d1cSdrh ** (as in the FROM clause of a SELECT statement) in this case it contains
200bfc2d1cSdrh ** the name of a single table, as one might find in an INSERT, DELETE,
210bfc2d1cSdrh ** or UPDATE statement.  Look up that table in the symbol table and
220bfc2d1cSdrh ** return a pointer.  Set an error message and return NULL if the table
230bfc2d1cSdrh ** name is not found or if any other error occurs.
240bfc2d1cSdrh **
250bfc2d1cSdrh ** The following fields are initialized appropriate in pSrc:
260bfc2d1cSdrh **
270bfc2d1cSdrh **    pSrc->a[0].pTab       Pointer to the Table object
280bfc2d1cSdrh **    pSrc->a[0].pIndex     Pointer to the INDEXED BY index, if there is one
290bfc2d1cSdrh **
30a76b5dfcSdrh */
sqlite3SrcListLookup(Parse * pParse,SrcList * pSrc)314adee20fSdanielk1977 Table *sqlite3SrcListLookup(Parse *pParse, SrcList *pSrc){
327601294aSdrh   SrcItem *pItem = pSrc->a;
33b1c685b0Sdanielk1977   Table *pTab;
3469887c99Sdan   assert( pItem && pSrc->nSrc>=1 );
3541fb5cd1Sdan   pTab = sqlite3LocateTableItem(pParse, 0, pItem);
361feeaed2Sdan   sqlite3DeleteTable(pParse->db, pItem->pTab);
37855eb1cfSdrh   pItem->pTab = pTab;
38ed8a3bb1Sdrh   if( pTab ){
3979df7782Sdrh     pTab->nTabRef++;
401862271fSdrh     if( pItem->fg.isIndexedBy && sqlite3IndexedByLookup(pParse, pItem) ){
41b1c685b0Sdanielk1977       pTab = 0;
42a76b5dfcSdrh     }
431862271fSdrh   }
44a76b5dfcSdrh   return pTab;
45a76b5dfcSdrh }
46a76b5dfcSdrh 
473b26b2b5Sdrh /* Generate byte-code that will report the number of rows modified
483b26b2b5Sdrh ** by a DELETE, INSERT, or UPDATE statement.
493b26b2b5Sdrh */
sqlite3CodeChangeCount(Vdbe * v,int regCounter,const char * zColName)503b26b2b5Sdrh void sqlite3CodeChangeCount(Vdbe *v, int regCounter, const char *zColName){
513b26b2b5Sdrh   sqlite3VdbeAddOp0(v, OP_FkCheck);
523b26b2b5Sdrh   sqlite3VdbeAddOp2(v, OP_ResultRow, regCounter, 1);
533b26b2b5Sdrh   sqlite3VdbeSetNumCols(v, 1);
543b26b2b5Sdrh   sqlite3VdbeSetColName(v, 0, COLNAME_NAME, zColName, SQLITE_STATIC);
553b26b2b5Sdrh }
563b26b2b5Sdrh 
57e1857cc0Sdrh /* Return true if table pTab is read-only.
58e1857cc0Sdrh **
59e1857cc0Sdrh ** A table is read-only if any of the following are true:
60e1857cc0Sdrh **
61e1857cc0Sdrh **   1) It is a virtual table and no implementation of the xUpdate method
62e1857cc0Sdrh **      has been provided
63e1857cc0Sdrh **
6400378fdeSdrh **   2) A trigger is currently being coded and the table is a virtual table
6500378fdeSdrh **      that is SQLITE_VTAB_DIRECTONLY or if PRAGMA trusted_schema=OFF and
6600378fdeSdrh **      the table is not SQLITE_VTAB_INNOCUOUS.
6700378fdeSdrh **
6800378fdeSdrh **   3) It is a system table (i.e. sqlite_schema), this call is not
69e1857cc0Sdrh **      part of a nested parse and writable_schema pragma has not
70e1857cc0Sdrh **      been specified
71e1857cc0Sdrh **
7200378fdeSdrh **   4) The table is a shadow table, the database connection is in
73e1857cc0Sdrh **      defensive mode, and the current sqlite3_prepare()
74e1857cc0Sdrh **      is for a top-level SQL statement.
75e1857cc0Sdrh */
vtabIsReadOnly(Parse * pParse,Table * pTab)7600378fdeSdrh static int vtabIsReadOnly(Parse *pParse, Table *pTab){
7700378fdeSdrh   if( sqlite3GetVTable(pParse->db, pTab)->pMod->pModule->xUpdate==0 ){
7800378fdeSdrh     return 1;
7900378fdeSdrh   }
8000378fdeSdrh 
8100378fdeSdrh   /* Within triggers:
8200378fdeSdrh   **   *  Do not allow DELETE, INSERT, or UPDATE of SQLITE_VTAB_DIRECTONLY
8300378fdeSdrh   **      virtual tables
8400378fdeSdrh   **   *  Only allow DELETE, INSERT, or UPDATE of non-SQLITE_VTAB_INNOCUOUS
8500378fdeSdrh   **      virtual tables if PRAGMA trusted_schema=ON.
8600378fdeSdrh   */
8700378fdeSdrh   if( pParse->pToplevel!=0
8800378fdeSdrh    && pTab->u.vtab.p->eVtabRisk >
8900378fdeSdrh            ((pParse->db->flags & SQLITE_TrustedSchema)!=0)
9000378fdeSdrh   ){
9100378fdeSdrh     sqlite3ErrorMsg(pParse, "unsafe use of virtual table \"%s\"",
9200378fdeSdrh       pTab->zName);
9300378fdeSdrh   }
9400378fdeSdrh   return 0;
9500378fdeSdrh }
tabIsReadOnly(Parse * pParse,Table * pTab)96e1857cc0Sdrh static int tabIsReadOnly(Parse *pParse, Table *pTab){
97e1857cc0Sdrh   sqlite3 *db;
98e1857cc0Sdrh   if( IsVirtual(pTab) ){
9900378fdeSdrh     return vtabIsReadOnly(pParse, pTab);
100e1857cc0Sdrh   }
101e1857cc0Sdrh   if( (pTab->tabFlags & (TF_Readonly|TF_Shadow))==0 ) return 0;
102e1857cc0Sdrh   db = pParse->db;
103e1857cc0Sdrh   if( (pTab->tabFlags & TF_Readonly)!=0 ){
104e1857cc0Sdrh     return sqlite3WritableSchema(db)==0 && pParse->nested==0;
105e1857cc0Sdrh   }
106e1857cc0Sdrh   assert( pTab->tabFlags & TF_Shadow );
107070ae3beSdrh   return sqlite3ReadOnlyShadowTables(db);
108e1857cc0Sdrh }
109e1857cc0Sdrh 
110a76b5dfcSdrh /*
11100378fdeSdrh ** Check to make sure the given table is writable.
11200378fdeSdrh **
11300378fdeSdrh ** If pTab is not writable  ->  generate an error message and return 1.
11400378fdeSdrh ** If pTab is writable but other errors have occurred -> return 1.
11500378fdeSdrh ** If pTab is writable and no prior errors -> return 0;
116812d7a21Sdrh */
sqlite3IsReadOnly(Parse * pParse,Table * pTab,int viewOk)1174adee20fSdanielk1977 int sqlite3IsReadOnly(Parse *pParse, Table *pTab, int viewOk){
118e1857cc0Sdrh   if( tabIsReadOnly(pParse, pTab) ){
1194adee20fSdanielk1977     sqlite3ErrorMsg(pParse, "table %s may not be modified", pTab->zName);
1205cf590c1Sdrh     return 1;
1215cf590c1Sdrh   }
122b84f96f8Sdanielk1977 #ifndef SQLITE_OMIT_VIEW
123f38524d2Sdrh   if( !viewOk && IsView(pTab) ){
1244adee20fSdanielk1977     sqlite3ErrorMsg(pParse,"cannot modify %s because it is a view",pTab->zName);
125812d7a21Sdrh     return 1;
126812d7a21Sdrh   }
127b84f96f8Sdanielk1977 #endif
128812d7a21Sdrh   return 0;
129812d7a21Sdrh }
130812d7a21Sdrh 
131299b187dSdanielk1977 
1320f35a6b5Sdrh #if !defined(SQLITE_OMIT_VIEW) && !defined(SQLITE_OMIT_TRIGGER)
1330f35a6b5Sdrh /*
1340f35a6b5Sdrh ** Evaluate a view and store its result in an ephemeral table.  The
1350f35a6b5Sdrh ** pWhere argument is an optional WHERE clause that restricts the
1360f35a6b5Sdrh ** set of rows in the view that are to be added to the ephemeral table.
1370f35a6b5Sdrh */
sqlite3MaterializeView(Parse * pParse,Table * pView,Expr * pWhere,ExprList * pOrderBy,Expr * pLimit,int iCur)1380f35a6b5Sdrh void sqlite3MaterializeView(
1390f35a6b5Sdrh   Parse *pParse,       /* Parsing context */
1402a5d825eSdrh   Table *pView,        /* View definition */
1410f35a6b5Sdrh   Expr *pWhere,        /* Optional WHERE clause to be added */
142b3c16b89Sdan   ExprList *pOrderBy,  /* Optional ORDER BY clause */
143b3c16b89Sdan   Expr *pLimit,        /* Optional LIMIT clause */
14460ec914cSpeter.d.reid   int iCur             /* Cursor number for ephemeral table */
1450f35a6b5Sdrh ){
1460f35a6b5Sdrh   SelectDest dest;
1476d235cb8Sdan   Select *pSel;
1480f35a6b5Sdrh   SrcList *pFrom;
1496d235cb8Sdan   sqlite3 *db = pParse->db;
1506d235cb8Sdan   int iDb = sqlite3SchemaToIndex(db, pView->pSchema);
1516ab3a2ecSdanielk1977   pWhere = sqlite3ExprDup(db, pWhere, 0);
15229c992cbSdrh   pFrom = sqlite3SrcListAppend(pParse, 0, 0, 0);
153b7916a78Sdrh   if( pFrom ){
154b7916a78Sdrh     assert( pFrom->nSrc==1 );
1556d235cb8Sdan     pFrom->a[0].zName = sqlite3DbStrDup(db, pView->zName);
15669c33826Sdrh     pFrom->a[0].zDatabase = sqlite3DbStrDup(db, db->aDb[iDb].zDbSName);
157d44f8b23Sdrh     assert( pFrom->a[0].fg.isUsing==0 );
158d44f8b23Sdrh     assert( pFrom->a[0].u3.pOn==0 );
159b7916a78Sdrh   }
160b3c16b89Sdan   pSel = sqlite3SelectNew(pParse, 0, pFrom, pWhere, 0, 0, pOrderBy,
1618c0833fbSdrh                           SF_IncludeHidden, pLimit);
1620f35a6b5Sdrh   sqlite3SelectDestInit(&dest, SRT_EphemTab, iCur);
1636d235cb8Sdan   sqlite3Select(pParse, pSel, &dest);
1646d235cb8Sdan   sqlite3SelectDelete(db, pSel);
1650f35a6b5Sdrh }
1660f35a6b5Sdrh #endif /* !defined(SQLITE_OMIT_VIEW) && !defined(SQLITE_OMIT_TRIGGER) */
1670f35a6b5Sdrh 
168273f619bSshane #if defined(SQLITE_ENABLE_UPDATE_DELETE_LIMIT) && !defined(SQLITE_OMIT_SUBQUERY)
1694281bd42Sshane /*
1704281bd42Sshane ** Generate an expression tree to implement the WHERE, ORDER BY,
1714281bd42Sshane ** and LIMIT/OFFSET portion of DELETE and UPDATE statements.
1724281bd42Sshane **
1734281bd42Sshane **     DELETE FROM table_wxyz WHERE a<5 ORDER BY a LIMIT 1;
1744281bd42Sshane **                            \__________________________/
1754281bd42Sshane **                               pLimitWhere (pInClause)
1764281bd42Sshane */
sqlite3LimitWhere(Parse * pParse,SrcList * pSrc,Expr * pWhere,ExprList * pOrderBy,Expr * pLimit,char * zStmtType)1774281bd42Sshane Expr *sqlite3LimitWhere(
1784281bd42Sshane   Parse *pParse,               /* The parser context */
1794281bd42Sshane   SrcList *pSrc,               /* the FROM clause -- which tables to scan */
1804281bd42Sshane   Expr *pWhere,                /* The WHERE clause.  May be null */
1814281bd42Sshane   ExprList *pOrderBy,          /* The ORDER BY clause.  May be null */
1824281bd42Sshane   Expr *pLimit,                /* The LIMIT clause.  May be null */
1839eade087Sdrh   char *zStmtType              /* Either DELETE or UPDATE.  For err msgs. */
1844281bd42Sshane ){
185b3c16b89Sdan   sqlite3 *db = pParse->db;
186b3c16b89Sdan   Expr *pLhs = NULL;           /* LHS of IN(SELECT...) operator */
18749ffdbf4Sshane   Expr *pInClause = NULL;      /* WHERE rowid IN ( select ) */
18849ffdbf4Sshane   ExprList *pEList = NULL;     /* Expression list contaning only pSelectRowid */
18949ffdbf4Sshane   SrcList *pSelectSrc = NULL;  /* SELECT rowid FROM x ... (dup of pSrc) */
19049ffdbf4Sshane   Select *pSelect = NULL;      /* Complete SELECT tree */
191b3c16b89Sdan   Table *pTab;
19249ffdbf4Sshane 
19349ffdbf4Sshane   /* Check that there isn't an ORDER BY without a LIMIT clause.
19449ffdbf4Sshane   */
195b3c16b89Sdan   if( pOrderBy && pLimit==0 ) {
19649ffdbf4Sshane     sqlite3ErrorMsg(pParse, "ORDER BY without LIMIT on %s", zStmtType);
197b3c16b89Sdan     sqlite3ExprDelete(pParse->db, pWhere);
198b3c16b89Sdan     sqlite3ExprListDelete(pParse->db, pOrderBy);
199b3c16b89Sdan     return 0;
20049ffdbf4Sshane   }
201273f619bSshane 
202273f619bSshane   /* We only need to generate a select expression if there
203273f619bSshane   ** is a limit/offset term to enforce.
204273f619bSshane   */
20549ffdbf4Sshane   if( pLimit == 0 ) {
20649ffdbf4Sshane     return pWhere;
20749ffdbf4Sshane   }
208273f619bSshane 
209273f619bSshane   /* Generate a select expression tree to enforce the limit/offset
210273f619bSshane   ** term for the DELETE or UPDATE statement.  For example:
211273f619bSshane   **   DELETE FROM table_a WHERE col1=1 ORDER BY col2 LIMIT 1 OFFSET 1
212273f619bSshane   ** becomes:
213273f619bSshane   **   DELETE FROM table_a WHERE rowid IN (
214b235db9cSshane   **     SELECT rowid FROM table_a WHERE col1=1 ORDER BY col2 LIMIT 1 OFFSET 1
215273f619bSshane   **   );
216273f619bSshane   */
217b235db9cSshane 
218b3c16b89Sdan   pTab = pSrc->a[0].pTab;
219b3c16b89Sdan   if( HasRowid(pTab) ){
220b3c16b89Sdan     pLhs = sqlite3PExpr(pParse, TK_ROW, 0, 0);
221b3c16b89Sdan     pEList = sqlite3ExprListAppend(
222b3c16b89Sdan         pParse, 0, sqlite3PExpr(pParse, TK_ROW, 0, 0)
223b3c16b89Sdan     );
224b3c16b89Sdan   }else{
225b3c16b89Sdan     Index *pPk = sqlite3PrimaryKeyIndex(pTab);
226b3c16b89Sdan     if( pPk->nKeyCol==1 ){
2277061aa89Sdrh       const char *zName = pTab->aCol[pPk->aiColumn[0]].zCnName;
22826caf5beSdan       pLhs = sqlite3Expr(db, TK_ID, zName);
22926caf5beSdan       pEList = sqlite3ExprListAppend(pParse, 0, sqlite3Expr(db, TK_ID, zName));
230b3c16b89Sdan     }else{
231b3c16b89Sdan       int i;
232b3c16b89Sdan       for(i=0; i<pPk->nKeyCol; i++){
2337061aa89Sdrh         Expr *p = sqlite3Expr(db, TK_ID, pTab->aCol[pPk->aiColumn[i]].zCnName);
234b3c16b89Sdan         pEList = sqlite3ExprListAppend(pParse, pEList, p);
235b3c16b89Sdan       }
236b3c16b89Sdan       pLhs = sqlite3PExpr(pParse, TK_VECTOR, 0, 0);
237b3c16b89Sdan       if( pLhs ){
238b3c16b89Sdan         pLhs->x.pList = sqlite3ExprListDup(db, pEList, 0);
239b3c16b89Sdan       }
240b3c16b89Sdan     }
241b3c16b89Sdan   }
242b235db9cSshane 
243273f619bSshane   /* duplicate the FROM clause as it is needed by both the DELETE/UPDATE tree
244b235db9cSshane   ** and the SELECT subtree. */
245b3c16b89Sdan   pSrc->a[0].pTab = 0;
246a4656e39Sdan   pSelectSrc = sqlite3SrcListDup(db, pSrc, 0);
247b3c16b89Sdan   pSrc->a[0].pTab = pTab;
248a79e2a2dSdrh   if( pSrc->a[0].fg.isIndexedBy ){
249dbfbb5a0Sdrh     assert( pSrc->a[0].fg.isCte==0 );
250a79e2a2dSdrh     pSrc->a[0].u2.pIBIndex = 0;
251a4656e39Sdan     pSrc->a[0].fg.isIndexedBy = 0;
252a4656e39Sdan     sqlite3DbFree(db, pSrc->a[0].u1.zIndexedBy);
253a79e2a2dSdrh   }else if( pSrc->a[0].fg.isCte ){
254a79e2a2dSdrh     pSrc->a[0].u2.pCteUse->nUse++;
255a79e2a2dSdrh   }
256b235db9cSshane 
257b235db9cSshane   /* generate the SELECT expression tree. */
25818209cd6Sdrh   pSelect = sqlite3SelectNew(pParse, pEList, pSelectSrc, pWhere, 0 ,0,
2598c0833fbSdrh       pOrderBy,0,pLimit
260b3c16b89Sdan   );
261b235db9cSshane 
26249ffdbf4Sshane   /* now generate the new WHERE rowid IN clause for the DELETE/UDPATE */
263b3c16b89Sdan   pInClause = sqlite3PExpr(pParse, TK_IN, pLhs, 0);
26408de4f79Sdrh   sqlite3PExprAddSelect(pParse, pInClause, pSelect);
2654281bd42Sshane   return pInClause;
2664281bd42Sshane }
2679eade087Sdrh #endif /* defined(SQLITE_ENABLE_UPDATE_DELETE_LIMIT) */
2689eade087Sdrh        /*      && !defined(SQLITE_OMIT_SUBQUERY) */
2690f35a6b5Sdrh 
270ad6d9460Sdrh /*
27123bf66d6Sdrh ** Generate code for a DELETE FROM statement.
27223bf66d6Sdrh **
27323bf66d6Sdrh **     DELETE FROM table_wxyz WHERE a<5 AND b NOT NULL;
27423bf66d6Sdrh **                 \________/       \________________/
27523bf66d6Sdrh **                  pTabList              pWhere
276cce7d176Sdrh */
sqlite3DeleteFrom(Parse * pParse,SrcList * pTabList,Expr * pWhere,ExprList * pOrderBy,Expr * pLimit)2773b61ebb8Sdan void sqlite3DeleteFrom(
278cce7d176Sdrh   Parse *pParse,         /* The parser context */
279113088ecSdrh   SrcList *pTabList,     /* The table from which we should delete things */
280b3c16b89Sdan   Expr *pWhere,          /* The WHERE clause.  May be null */
2813b61ebb8Sdan   ExprList *pOrderBy,    /* ORDER BY clause. May be null */
2828c0833fbSdrh   Expr *pLimit           /* LIMIT clause. May be null */
283cce7d176Sdrh ){
284cce7d176Sdrh   Vdbe *v;               /* The virtual database engine */
285cce7d176Sdrh   Table *pTab;           /* The table from which records will be deleted */
286cce7d176Sdrh   int i;                 /* Loop counter */
287cce7d176Sdrh   WhereInfo *pWInfo;     /* Information about the WHERE clause */
288cce7d176Sdrh   Index *pIdx;           /* For looping over indices of the table */
28926198bb4Sdrh   int iTabCur;           /* Cursor number for the table */
2901a51ce78Smistachkin   int iDataCur = 0;      /* VDBE cursor for the canonical data source */
2911a51ce78Smistachkin   int iIdxCur = 0;       /* Cursor number of the first index */
2926a53499aSdrh   int nIdx;              /* Number of indices */
2939bb575fdSdrh   sqlite3 *db;           /* Main database structure */
29485e2096fSdrh   AuthContext sContext;  /* Authorization context */
295b3bce662Sdanielk1977   NameContext sNC;       /* Name context to resolve expressions in */
29653a67774Sdrh   int iDb;               /* Database number */
29779636913Sdrh   int memCnt = 0;        /* Memory cell used for change counting */
29852bd7912Sdanielk1977   int rcauth;            /* Value returned by authorization callback */
299b0264eecSdrh   int eOnePass;          /* ONEPASS_OFF or _SINGLE or _MULTI */
3006a53499aSdrh   int aiCurOnePass[2];   /* The write cursors opened by WHERE_ONEPASS */
3016a53499aSdrh   u8 *aToOpen = 0;       /* Open cursor iTabCur+j if aToOpen[j] is true */
30296129472Sdrh   Index *pPk;            /* The PRIMARY KEY index on the table */
30320b85953Smistachkin   int iPk = 0;           /* First of nPk registers holding PRIMARY KEY value */
304e73e0671Sdrh   i16 nPk = 1;           /* Number of columns in the PRIMARY KEY */
305e73e0671Sdrh   int iKey;              /* Memory cell holding key of row to be deleted */
306e73e0671Sdrh   i16 nKey;              /* Number of memory cells in the row key */
30796129472Sdrh   int iEphCur = 0;       /* Ephemeral table holding all primary key values */
30896129472Sdrh   int iRowSet = 0;       /* Register for rowset of rows to delete */
30996129472Sdrh   int addrBypass = 0;    /* Address of jump over the delete logic */
31096129472Sdrh   int addrLoop = 0;      /* Top of the delete loop */
31160ec914cSpeter.d.reid   int addrEphOpen = 0;   /* Instruction to open the Ephemeral table */
3129fe3fbbcSmistachkin   int bComplex;          /* True if there are triggers or FKs or
31357a07ba9Sdrh                          ** subqueries in the WHERE clause */
3141bee3d7bSdrh 
315798da52cSdrh #ifndef SQLITE_OMIT_TRIGGER
316798da52cSdrh   int isView;                  /* True if attempting to delete from a view */
3172f886d1dSdanielk1977   Trigger *pTrigger;           /* List of table triggers, if required */
318798da52cSdrh #endif
319cce7d176Sdrh 
320eeb844a7Sdrh   memset(&sContext, 0, sizeof(sContext));
32117435752Sdrh   db = pParse->db;
3220c7d3d39Sdrh   assert( db->pParse==pParse );
3230c7d3d39Sdrh   if( pParse->nErr ){
324daffd0e5Sdrh     goto delete_from_cleanup;
325daffd0e5Sdrh   }
3260c7d3d39Sdrh   assert( db->mallocFailed==0 );
327113088ecSdrh   assert( pTabList->nSrc==1 );
328daffd0e5Sdrh 
3291ccde15dSdrh   /* Locate the table which we want to delete.  This table has to be
330ad3cab52Sdrh   ** put in an SrcList structure because some of the subroutines we
331cce7d176Sdrh   ** will be calling are designed to work with multiple tables and expect
332ad3cab52Sdrh   ** an SrcList* parameter instead of just a Table* parameter.
333cce7d176Sdrh   */
3344adee20fSdanielk1977   pTab = sqlite3SrcListLookup(pParse, pTabList);
335a69d9168Sdrh   if( pTab==0 )  goto delete_from_cleanup;
336b7f9164eSdrh 
337b7f9164eSdrh   /* Figure out if we have any triggers and if the table being
338b7f9164eSdrh   ** deleted from is a view
339b7f9164eSdrh   */
340b7f9164eSdrh #ifndef SQLITE_OMIT_TRIGGER
3412f886d1dSdanielk1977   pTrigger = sqlite3TriggersExist(pParse, pTab, TK_DELETE, 0, 0);
342f38524d2Sdrh   isView = IsView(pTab);
343b7f9164eSdrh #else
3442f886d1dSdanielk1977 # define pTrigger 0
345b7f9164eSdrh # define isView 0
346b7f9164eSdrh #endif
347ee15962dSdrh   bComplex = pTrigger || sqlite3FkRequired(pParse, pTab, 0, 0);
348b7f9164eSdrh #ifdef SQLITE_OMIT_VIEW
349b7f9164eSdrh # undef isView
350b7f9164eSdrh # define isView 0
351b7f9164eSdrh #endif
352b7f9164eSdrh 
3532a7dcbfbSdrh #if TREETRACE_ENABLED
3542a7dcbfbSdrh   if( sqlite3TreeTrace & 0x10000 ){
3552a7dcbfbSdrh     sqlite3TreeViewLine(0, "In sqlite3Delete() at %s:%d", __FILE__, __LINE__);
3562a7dcbfbSdrh     sqlite3TreeViewDelete(pParse->pWith, pTabList, pWhere,
3572a7dcbfbSdrh                           pOrderBy, pLimit, pTrigger);
3582a7dcbfbSdrh   }
3592a7dcbfbSdrh #endif
3602a7dcbfbSdrh 
361b3c16b89Sdan #ifdef SQLITE_ENABLE_UPDATE_DELETE_LIMIT
362b3c16b89Sdan   if( !isView ){
363b3c16b89Sdan     pWhere = sqlite3LimitWhere(
3648c0833fbSdrh         pParse, pTabList, pWhere, pOrderBy, pLimit, "DELETE"
365b3c16b89Sdan     );
366b3c16b89Sdan     pOrderBy = 0;
3678c0833fbSdrh     pLimit = 0;
368b3c16b89Sdan   }
369b3c16b89Sdan #endif
370b3c16b89Sdan 
371595a523aSdanielk1977   /* If pTab is really a view, make sure it has been initialized.
372595a523aSdanielk1977   */
373595a523aSdanielk1977   if( sqlite3ViewGetColumnNames(pParse, pTab) ){
374595a523aSdanielk1977     goto delete_from_cleanup;
375595a523aSdanielk1977   }
376595a523aSdanielk1977 
3772f886d1dSdanielk1977   if( sqlite3IsReadOnly(pParse, pTab, (pTrigger?1:0)) ){
3785cf590c1Sdrh     goto delete_from_cleanup;
379113088ecSdrh   }
380da184236Sdanielk1977   iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
381da184236Sdanielk1977   assert( iDb<db->nDb );
382a0daa751Sdrh   rcauth = sqlite3AuthCheck(pParse, SQLITE_DELETE, pTab->zName, 0,
383a0daa751Sdrh                             db->aDb[iDb].zDbSName);
38452bd7912Sdanielk1977   assert( rcauth==SQLITE_OK || rcauth==SQLITE_DENY || rcauth==SQLITE_IGNORE );
38552bd7912Sdanielk1977   if( rcauth==SQLITE_DENY ){
386e5f9c644Sdrh     goto delete_from_cleanup;
387e5f9c644Sdrh   }
3882f886d1dSdanielk1977   assert(!isView || pTrigger);
389cce7d176Sdrh 
3906a53499aSdrh   /* Assign cursor numbers to the table and all its indices.
391cce7d176Sdrh   */
3926a3ea0e6Sdrh   assert( pTabList->nSrc==1 );
39326198bb4Sdrh   iTabCur = pTabList->a[0].iCursor = pParse->nTab++;
3946a53499aSdrh   for(nIdx=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, nIdx++){
395e448dc4aSdanielk1977     pParse->nTab++;
396e448dc4aSdanielk1977   }
397cce7d176Sdrh 
39885e2096fSdrh   /* Start the view context
39985e2096fSdrh   */
40085e2096fSdrh   if( isView ){
4014adee20fSdanielk1977     sqlite3AuthContextPush(pParse, &sContext, pTab->zName);
40285e2096fSdrh   }
40385e2096fSdrh 
404cce7d176Sdrh   /* Begin generating code.
405cce7d176Sdrh   */
4064adee20fSdanielk1977   v = sqlite3GetVdbe(pParse);
407f29ce559Sdanielk1977   if( v==0 ){
408f29ce559Sdanielk1977     goto delete_from_cleanup;
409f29ce559Sdanielk1977   }
4104794f735Sdrh   if( pParse->nested==0 ) sqlite3VdbeCountChanges(v);
4117c48360dSdrh   sqlite3BeginWriteOperation(pParse, bComplex, iDb);
4125e00f6c7Sdrh 
4139d2985c7Sdrh   /* If we are trying to delete from a view, realize that view into
41460ec914cSpeter.d.reid   ** an ephemeral table.
4155cf590c1Sdrh   */
416fa4e62f3Sshane #if !defined(SQLITE_OMIT_VIEW) && !defined(SQLITE_OMIT_TRIGGER)
4175cf590c1Sdrh   if( isView ){
418b3c16b89Sdan     sqlite3MaterializeView(pParse, pTab,
4198c0833fbSdrh         pWhere, pOrderBy, pLimit, iTabCur
420b3c16b89Sdan     );
421f8ffb278Sdrh     iDataCur = iIdxCur = iTabCur;
422b3c16b89Sdan     pOrderBy = 0;
4238c0833fbSdrh     pLimit = 0;
4240f35a6b5Sdrh   }
425fa4e62f3Sshane #endif
4261013c932Sdrh 
4270f35a6b5Sdrh   /* Resolve the column names in the WHERE clause.
4280f35a6b5Sdrh   */
4290f35a6b5Sdrh   memset(&sNC, 0, sizeof(sNC));
4300f35a6b5Sdrh   sNC.pParse = pParse;
4310f35a6b5Sdrh   sNC.pSrcList = pTabList;
4327d10d5a6Sdrh   if( sqlite3ResolveExprNames(&sNC, pWhere) ){
4330f35a6b5Sdrh     goto delete_from_cleanup;
4345cf590c1Sdrh   }
4355cf590c1Sdrh 
4361bee3d7bSdrh   /* Initialize the counter of the number of rows deleted, if
4371bee3d7bSdrh   ** we are counting rows.
4381bee3d7bSdrh   */
43979636913Sdrh   if( (db->flags & SQLITE_CountRows)!=0
44079636913Sdrh    && !pParse->nested
44179636913Sdrh    && !pParse->pTriggerTab
442d086aa0aSdrh    && !pParse->bReturning
44379636913Sdrh   ){
4440a07c107Sdrh     memCnt = ++pParse->nMem;
4454c583128Sdrh     sqlite3VdbeAddOp2(v, OP_Integer, 0, memCnt);
4461bee3d7bSdrh   }
447cce7d176Sdrh 
448f8cecdabSdrh #ifndef SQLITE_OMIT_TRUNCATE_OPTIMIZATION
4490353ceddSdrh   /* Special case: A DELETE without a WHERE clause deletes everything.
4502283d46cSdan   ** It is easier just to erase the whole table. Prior to version 3.6.5,
4512283d46cSdan   ** this optimization caused the row change count (the value returned by
4529418921cSdrh   ** API function sqlite3_count_changes) to be set incorrectly.
4539418921cSdrh   **
4549418921cSdrh   ** The "rcauth==SQLITE_OK" terms is the
4551e1c4226Sdrh   ** IMPLEMENTATION-OF: R-17228-37124 If the action code is SQLITE_DELETE and
4569418921cSdrh   ** the callback returns SQLITE_IGNORE then the DELETE operation proceeds but
4579418921cSdrh   ** the truncate optimization is disabled and all rows are deleted
4589418921cSdrh   ** individually.
4599418921cSdrh   */
46067c495acSdrh   if( rcauth==SQLITE_OK
46167c495acSdrh    && pWhere==0
462b77ebd82Sdrh    && !bComplex
46367c495acSdrh    && !IsVirtual(pTab)
4649b1c62d4Sdrh #ifdef SQLITE_ENABLE_PREUPDATE_HOOK
46567c495acSdrh    && db->xPreUpdateCallback==0
4669b1c62d4Sdrh #endif
4671da40a38Sdan   ){
4689a02fb44Sdanielk1977     assert( !isView );
4695d1dcff0Sdan     sqlite3TableLock(pParse, iDb, pTab->tnum, 1, pTab->zName);
470261c02d9Sdrh     if( HasRowid(pTab) ){
47179636913Sdrh       sqlite3VdbeAddOp4(v, OP_Clear, pTab->tnum, iDb, memCnt ? memCnt : -1,
472bbb5e4e0Sdrh                         pTab->zName, P4_STATIC);
473261c02d9Sdrh     }
4740353ceddSdrh     for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
475da184236Sdanielk1977       assert( pIdx->pSchema==pTab->pSchema );
476a6df0e69Sdrh       if( IsPrimaryKeyIndex(pIdx) && !HasRowid(pTab) ){
477058e9950Sdrh         sqlite3VdbeAddOp3(v, OP_Clear, pIdx->tnum, iDb, memCnt ? memCnt : -1);
478058e9950Sdrh       }else{
479058e9950Sdrh         sqlite3VdbeAddOp2(v, OP_Clear, pIdx->tnum, iDb);
480a6df0e69Sdrh       }
4810353ceddSdrh     }
482f8cecdabSdrh   }else
483f8cecdabSdrh #endif /* SQLITE_OMIT_TRUNCATE_OPTIMIZATION */
48496129472Sdrh   {
48568c0c710Sdrh     u16 wcf = WHERE_ONEPASS_DESIRED|WHERE_DUPLICATES_OK;
48691da7072Sdrh     if( sNC.ncFlags & NC_VarSelect ) bComplex = 1;
487f0ee1d3cSdan     wcf |= (bComplex ? 0 : WHERE_ONEPASS_MULTIROW);
48896129472Sdrh     if( HasRowid(pTab) ){
48996129472Sdrh       /* For a rowid table, initialize the RowSet to an empty set */
49096129472Sdrh       pPk = 0;
49196129472Sdrh       nPk = 1;
49296129472Sdrh       iRowSet = ++pParse->nMem;
49396129472Sdrh       sqlite3VdbeAddOp2(v, OP_Null, 0, iRowSet);
49496129472Sdrh     }else{
49560ec914cSpeter.d.reid       /* For a WITHOUT ROWID table, create an ephemeral table used to
49696129472Sdrh       ** hold all primary keys for rows to be deleted. */
497261c02d9Sdrh       pPk = sqlite3PrimaryKeyIndex(pTab);
498261c02d9Sdrh       assert( pPk!=0 );
499261c02d9Sdrh       nPk = pPk->nKeyCol;
500261c02d9Sdrh       iPk = pParse->nMem+1;
501261c02d9Sdrh       pParse->nMem += nPk;
50296129472Sdrh       iEphCur = pParse->nTab++;
50396129472Sdrh       addrEphOpen = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, iEphCur, nPk);
5042ec2fb22Sdrh       sqlite3VdbeSetP4KeyInfo(pParse, pPk);
505261c02d9Sdrh     }
50696129472Sdrh 
50796129472Sdrh     /* Construct a query to find the rowid or primary key for every row
508f0ee1d3cSdan     ** to be deleted, based on the WHERE clause. Set variable eOnePass
509f0ee1d3cSdan     ** to indicate the strategy used to implement this delete:
510f0ee1d3cSdan     **
511b0264eecSdrh     **  ONEPASS_OFF:    Two-pass approach - use a FIFO for rowids/PK values.
512b0264eecSdrh     **  ONEPASS_SINGLE: One-pass approach - at most one row deleted.
513b0264eecSdrh     **  ONEPASS_MULTI:  One-pass approach - any number of rows may be deleted.
51496129472Sdrh     */
515895bab33Sdrh     pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, 0, 0,0,wcf,iTabCur+1);
516261c02d9Sdrh     if( pWInfo==0 ) goto delete_from_cleanup;
517f0ee1d3cSdan     eOnePass = sqlite3WhereOkOnePass(pWInfo, aiCurOnePass);
518076e0f96Sdan     assert( IsVirtual(pTab)==0 || eOnePass!=ONEPASS_MULTI );
51980b7198fSdan     assert( IsVirtual(pTab) || bComplex || eOnePass!=ONEPASS_OFF );
5207c48360dSdrh     if( eOnePass!=ONEPASS_SINGLE ) sqlite3MultiWrite(pParse);
52168c0c710Sdrh     if( sqlite3WhereUsesDeferredSeek(pWInfo) ){
52268c0c710Sdrh       sqlite3VdbeAddOp1(v, OP_FinishSeek, iTabCur);
52368c0c710Sdrh     }
52496129472Sdrh 
52596129472Sdrh     /* Keep track of the number of rows to be deleted */
52679636913Sdrh     if( memCnt ){
5276546af14Sdrh       sqlite3VdbeAddOp2(v, OP_AddImm, memCnt, 1);
5286546af14Sdrh     }
52996129472Sdrh 
53096129472Sdrh     /* Extract the rowid or primary key for the current row */
53196129472Sdrh     if( pPk ){
53296129472Sdrh       for(i=0; i<nPk; i++){
5334b92f98cSdrh         assert( pPk->aiColumn[i]>=0 );
53496129472Sdrh         sqlite3ExprCodeGetColumnOfTable(v, pTab, iTabCur,
53596129472Sdrh                                         pPk->aiColumn[i], iPk+i);
53696129472Sdrh       }
53796129472Sdrh       iKey = iPk;
53896129472Sdrh     }else{
5398c607191Sdrh       iKey = ++pParse->nMem;
5408c607191Sdrh       sqlite3ExprCodeGetColumnOfTable(v, pTab, iTabCur, -1, iKey);
54196129472Sdrh     }
54296129472Sdrh 
543b0264eecSdrh     if( eOnePass!=ONEPASS_OFF ){
54496129472Sdrh       /* For ONEPASS, no need to store the rowid/primary-key. There is only
54596129472Sdrh       ** one, so just keep it in its register(s) and fall through to the
546f0ee1d3cSdan       ** delete code.  */
54796129472Sdrh       nKey = nPk; /* OP_Found will use an unpacked key */
548575fad65Sdrh       aToOpen = sqlite3DbMallocRawNN(db, nIdx+2);
54952a82e69Sdrh       if( aToOpen==0 ){
55052a82e69Sdrh         sqlite3WhereEnd(pWInfo);
55152a82e69Sdrh         goto delete_from_cleanup;
55252a82e69Sdrh       }
553156c7919Sdrh       memset(aToOpen, 1, nIdx+1);
554156c7919Sdrh       aToOpen[nIdx+1] = 0;
555156c7919Sdrh       if( aiCurOnePass[0]>=0 ) aToOpen[aiCurOnePass[0]-iTabCur] = 0;
556156c7919Sdrh       if( aiCurOnePass[1]>=0 ) aToOpen[aiCurOnePass[1]-iTabCur] = 0;
55796129472Sdrh       if( addrEphOpen ) sqlite3VdbeChangeToNoop(v, addrEphOpen);
55868c0c710Sdrh       addrBypass = sqlite3VdbeMakeLabel(pParse);
559f0ee1d3cSdan     }else{
560f0ee1d3cSdan       if( pPk ){
561f0ee1d3cSdan         /* Add the PK key for this row to the temporary table */
56296129472Sdrh         iKey = ++pParse->nMem;
56396129472Sdrh         nKey = 0;   /* Zero tells OP_Found to use a composite key */
564261c02d9Sdrh         sqlite3VdbeAddOp4(v, OP_MakeRecord, iPk, nPk, iKey,
565e9107698Sdrh             sqlite3IndexAffinityStr(pParse->db, pPk), nPk);
5669b4eaebcSdrh         sqlite3VdbeAddOp4Int(v, OP_IdxInsert, iEphCur, iKey, iPk, nPk);
567261c02d9Sdrh       }else{
568f0ee1d3cSdan         /* Add the rowid of the row to be deleted to the RowSet */
569170ad68aSdrh         nKey = 1;  /* OP_DeferredSeek always uses a single rowid */
57096129472Sdrh         sqlite3VdbeAddOp2(v, OP_RowSetAdd, iRowSet, iKey);
571299b187dSdanielk1977       }
572f0ee1d3cSdan       sqlite3WhereEnd(pWInfo);
573156c7919Sdrh     }
574ed326d70Sdanielk1977 
575165921a7Sdan     /* Unless this is a view, open cursors for the table we are
576165921a7Sdan     ** deleting from and all its indices. If this is a view, then the
577165921a7Sdan     ** only effect this statement has is to fire the INSTEAD OF
57896129472Sdrh     ** triggers.
57996129472Sdrh     */
580e448dc4aSdanielk1977     if( !isView ){
5817210b3d1Sdan       int iAddrOnce = 0;
582b0264eecSdrh       if( eOnePass==ONEPASS_MULTI ){
583511f9e8dSdrh         iAddrOnce = sqlite3VdbeAddOp0(v, OP_Once); VdbeCoverage(v);
58466336f37Sdrh       }
585b6b4b79fSdrh       testcase( IsVirtual(pTab) );
586b89aeb6aSdrh       sqlite3OpenTableAndIndices(pParse, pTab, OP_OpenWrite, OPFLAG_FORDELETE,
587b89aeb6aSdrh                                  iTabCur, aToOpen, &iDataCur, &iIdxCur);
588b6b4b79fSdrh       assert( pPk || IsVirtual(pTab) || iDataCur==iTabCur );
589b6b4b79fSdrh       assert( pPk || IsVirtual(pTab) || iIdxCur==iDataCur+1 );
590dc4f6fc0Sdrh       if( eOnePass==ONEPASS_MULTI ){
591dc4f6fc0Sdrh         sqlite3VdbeJumpHereOrPopInst(v, iAddrOnce);
592dc4f6fc0Sdrh       }
593e448dc4aSdanielk1977     }
594e448dc4aSdanielk1977 
59596129472Sdrh     /* Set up a loop over the rowids/primary-keys that were found in the
59696129472Sdrh     ** where-clause loop above.
59796129472Sdrh     */
598b0264eecSdrh     if( eOnePass!=ONEPASS_OFF ){
59996129472Sdrh       assert( nKey==nPk );  /* OP_Found will use an unpacked key */
600076e0f96Sdan       if( !IsVirtual(pTab) && aToOpen[iDataCur-iTabCur] ){
601f38524d2Sdrh         assert( pPk!=0 || IsView(pTab) );
60283d47afeSdrh         sqlite3VdbeAddOp4Int(v, OP_NotFound, iDataCur, addrBypass, iKey, nKey);
603688852abSdrh         VdbeCoverage(v);
60483d47afeSdrh       }
60596129472Sdrh     }else if( pPk ){
606688852abSdrh       addrLoop = sqlite3VdbeAddOp1(v, OP_Rewind, iEphCur); VdbeCoverage(v);
607e3740f27Sdrh       if( IsVirtual(pTab) ){
608e3740f27Sdrh         sqlite3VdbeAddOp3(v, OP_Column, iEphCur, 0, iKey);
609e3740f27Sdrh       }else{
6109057fc7cSdrh         sqlite3VdbeAddOp2(v, OP_RowData, iEphCur, iKey);
611e3740f27Sdrh       }
61296129472Sdrh       assert( nKey==0 );  /* OP_Found will use a composite key */
61396129472Sdrh     }else{
61496129472Sdrh       addrLoop = sqlite3VdbeAddOp3(v, OP_RowSetRead, iRowSet, 0, iKey);
615688852abSdrh       VdbeCoverage(v);
61696129472Sdrh       assert( nKey==1 );
6176a53499aSdrh     }
618e448dc4aSdanielk1977 
619299b187dSdanielk1977     /* Delete the row */
6204cbdda9eSdrh #ifndef SQLITE_OMIT_VIRTUALTABLE
6214cbdda9eSdrh     if( IsVirtual(pTab) ){
622595a523aSdanielk1977       const char *pVTab = (const char *)sqlite3GetVTable(db, pTab);
6234f3dd150Sdrh       sqlite3VtabMakeWritable(pParse, pTab);
624076e0f96Sdan       assert( eOnePass==ONEPASS_OFF || eOnePass==ONEPASS_SINGLE );
625e0af83acSdan       sqlite3MayAbort(pParse);
626338e311aSdrh       if( eOnePass==ONEPASS_SINGLE ){
627338e311aSdrh         sqlite3VdbeAddOp1(v, OP_Close, iTabCur);
628338e311aSdrh         if( sqlite3IsToplevel(pParse) ){
629076e0f96Sdan           pParse->isMultiWrite = 0;
630076e0f96Sdan         }
631338e311aSdrh       }
632338e311aSdrh       sqlite3VdbeAddOp4(v, OP_VUpdate, 0, 1, iKey, pVTab, P4_VTAB);
633338e311aSdrh       sqlite3VdbeChangeP5(v, OE_Abort);
6344cbdda9eSdrh     }else
6354cbdda9eSdrh #endif
6364cbdda9eSdrh     {
6372283d46cSdan       int count = (pParse->nested==0);    /* True to count changes */
63826198bb4Sdrh       sqlite3GenerateRowDelete(pParse, pTab, pTrigger, iDataCur, iIdxCur,
63968116939Sdrh           iKey, nKey, count, OE_Default, eOnePass, aiCurOnePass[1]);
640299b187dSdanielk1977     }
641c3f9bad2Sdanielk1977 
64296129472Sdrh     /* End of the loop over all rowids/primary-keys. */
643b0264eecSdrh     if( eOnePass!=ONEPASS_OFF ){
64483d47afeSdrh       sqlite3VdbeResolveLabel(v, addrBypass);
645f0ee1d3cSdan       sqlite3WhereEnd(pWInfo);
64696129472Sdrh     }else if( pPk ){
647688852abSdrh       sqlite3VdbeAddOp2(v, OP_Next, iEphCur, addrLoop+1); VdbeCoverage(v);
64896129472Sdrh       sqlite3VdbeJumpHere(v, addrLoop);
64996129472Sdrh     }else{
650076e85f5Sdrh       sqlite3VdbeGoto(v, addrLoop);
65196129472Sdrh       sqlite3VdbeJumpHere(v, addrLoop);
6526a53499aSdrh     }
65396129472Sdrh   } /* End non-truncate path */
6545e00f6c7Sdrh 
6550b9f50d8Sdrh   /* Update the sqlite_sequence table by storing the content of the
6560b9f50d8Sdrh   ** maximum rowid counter values recorded while inserting into
6570b9f50d8Sdrh   ** autoincrement tables.
6580b9f50d8Sdrh   */
659165921a7Sdan   if( pParse->nested==0 && pParse->pTriggerTab==0 ){
6600b9f50d8Sdrh     sqlite3AutoincrementEnd(pParse);
6610b9f50d8Sdrh   }
6620b9f50d8Sdrh 
6632283d46cSdan   /* Return the number of rows that were deleted. If this routine is
664e7de6f25Sdanielk1977   ** generating code because of a call to sqlite3NestedParse(), do not
665e7de6f25Sdanielk1977   ** invoke the callback function.
6661bee3d7bSdrh   */
66779636913Sdrh   if( memCnt ){
6683b26b2b5Sdrh     sqlite3CodeChangeCount(v, memCnt, "rows deleted");
6691bee3d7bSdrh   }
670cce7d176Sdrh 
671cce7d176Sdrh delete_from_cleanup:
6724adee20fSdanielk1977   sqlite3AuthContextPop(&sContext);
673633e6d57Sdrh   sqlite3SrcListDelete(db, pTabList);
674633e6d57Sdrh   sqlite3ExprDelete(db, pWhere);
6753b61ebb8Sdan #if defined(SQLITE_ENABLE_UPDATE_DELETE_LIMIT)
676b3c16b89Sdan   sqlite3ExprListDelete(db, pOrderBy);
677b3c16b89Sdan   sqlite3ExprDelete(db, pLimit);
6783b61ebb8Sdan #endif
679*41ce47c4Sdrh   if( aToOpen ) sqlite3DbNNFreeNN(db, aToOpen);
680cce7d176Sdrh   return;
681cce7d176Sdrh }
68275cbd984Sdan /* Make sure "isView" and other macros defined above are undefined. Otherwise
68360ec914cSpeter.d.reid ** they may interfere with compilation of other functions in this file
68475cbd984Sdan ** (or in another file, if this file becomes part of the amalgamation).  */
68575cbd984Sdan #ifdef isView
68675cbd984Sdan  #undef isView
68775cbd984Sdan #endif
68875cbd984Sdan #ifdef pTrigger
68975cbd984Sdan  #undef pTrigger
69075cbd984Sdan #endif
6919cfcf5d4Sdrh 
6929cfcf5d4Sdrh /*
6939cfcf5d4Sdrh ** This routine generates VDBE code that causes a single row of a
6946934fc7bSdrh ** single table to be deleted.  Both the original table entry and
6956934fc7bSdrh ** all indices are removed.
6969cfcf5d4Sdrh **
6976934fc7bSdrh ** Preconditions:
6989cfcf5d4Sdrh **
6996934fc7bSdrh **   1.  iDataCur is an open cursor on the btree that is the canonical data
7006934fc7bSdrh **       store for the table.  (This will be either the table itself,
70126198bb4Sdrh **       in the case of a rowid table, or the PRIMARY KEY index in the case
7026934fc7bSdrh **       of a WITHOUT ROWID table.)
7039cfcf5d4Sdrh **
7049cfcf5d4Sdrh **   2.  Read/write cursors for all indices of pTab must be open as
70526198bb4Sdrh **       cursor number iIdxCur+i for the i-th index.
7069cfcf5d4Sdrh **
707ecb31b63Sdrh **   3.  The primary key for the row to be deleted must be stored in a
708313619f5Sdrh **       sequence of nPk memory cells starting at iPk.  If nPk==0 that means
709313619f5Sdrh **       that a search record formed from OP_MakeRecord is contained in the
710313619f5Sdrh **       single memory location iPk.
711f0ee1d3cSdan **
712f0ee1d3cSdan ** eMode:
713b0264eecSdrh **   Parameter eMode may be passed either ONEPASS_OFF (0), ONEPASS_SINGLE, or
714b0264eecSdrh **   ONEPASS_MULTI.  If eMode is not ONEPASS_OFF, then the cursor
715b0264eecSdrh **   iDataCur already points to the row to delete. If eMode is ONEPASS_OFF
716b0264eecSdrh **   then this function must seek iDataCur to the entry identified by iPk
717b0264eecSdrh **   and nPk before reading from it.
718f0ee1d3cSdan **
719b0264eecSdrh **   If eMode is ONEPASS_MULTI, then this call is being made as part
720f0ee1d3cSdan **   of a ONEPASS delete that affects multiple rows. In this case, if
7214cef5b1cSdrh **   iIdxNoSeek is a valid cursor number (>=0) and is not the same as
7224cef5b1cSdrh **   iDataCur, then its position should be preserved following the delete
7234cef5b1cSdrh **   operation. Or, if iIdxNoSeek is not a valid cursor number, the
7244cef5b1cSdrh **   position of iDataCur should be preserved instead.
725f0ee1d3cSdan **
726f0ee1d3cSdan ** iIdxNoSeek:
7274cef5b1cSdrh **   If iIdxNoSeek is a valid cursor number (>=0) not equal to iDataCur,
7284cef5b1cSdrh **   then it identifies an index cursor (from within array of cursors
7294cef5b1cSdrh **   starting at iIdxCur) that already points to the index entry to be deleted.
73068116939Sdrh **   Except, this optimization is disabled if there are BEFORE triggers since
73168116939Sdrh **   the trigger body might have moved the cursor.
7329cfcf5d4Sdrh */
sqlite3GenerateRowDelete(Parse * pParse,Table * pTab,Trigger * pTrigger,int iDataCur,int iIdxCur,int iPk,i16 nPk,u8 count,u8 onconf,u8 eMode,int iIdxNoSeek)7334adee20fSdanielk1977 void sqlite3GenerateRowDelete(
7342d401ab8Sdrh   Parse *pParse,     /* Parsing context */
7359cfcf5d4Sdrh   Table *pTab,       /* Table containing the row to be deleted */
7362283d46cSdan   Trigger *pTrigger, /* List of triggers to (potentially) fire */
73726198bb4Sdrh   int iDataCur,      /* Cursor from which column data is extracted */
73826198bb4Sdrh   int iIdxCur,       /* First index cursor */
739ecb31b63Sdrh   int iPk,           /* First memory cell containing the PRIMARY KEY */
740ecb31b63Sdrh   i16 nPk,           /* Number of PRIMARY KEY memory cells */
741ecb31b63Sdrh   u8 count,          /* If non-zero, increment the row change counter */
742392ee21dSdrh   u8 onconf,         /* Default ON CONFLICT policy for triggers */
743b0264eecSdrh   u8 eMode,          /* ONEPASS_OFF, _SINGLE, or _MULTI.  See above */
744f0ee1d3cSdan   int iIdxNoSeek     /* Cursor number of cursor that does not need seeking */
7459cfcf5d4Sdrh ){
7462283d46cSdan   Vdbe *v = pParse->pVdbe;        /* Vdbe */
7473f022189Sdrh   int iOld = 0;                   /* First register in OLD.* array */
7482283d46cSdan   int iLabel;                     /* Label resolved to end of generated code */
749261c02d9Sdrh   u8 opSeek;                      /* Seek opcode */
7502d401ab8Sdrh 
7512283d46cSdan   /* Vdbe is guaranteed to have been allocated by this stage. */
7522283d46cSdan   assert( v );
7536934fc7bSdrh   VdbeModuleComment((v, "BEGIN: GenRowDel(%d,%d,%d,%d)",
75426198bb4Sdrh                          iDataCur, iIdxCur, iPk, (int)nPk));
7552283d46cSdan 
7562283d46cSdan   /* Seek cursor iCur to the row to delete. If this row no longer exists
7572283d46cSdan   ** (this can happen if a trigger program has already deleted it), do
7582283d46cSdan   ** not attempt to delete it or fire any DELETE triggers.  */
759ec4ccdbcSdrh   iLabel = sqlite3VdbeMakeLabel(pParse);
760261c02d9Sdrh   opSeek = HasRowid(pTab) ? OP_NotExists : OP_NotFound;
761b0264eecSdrh   if( eMode==ONEPASS_OFF ){
762688852abSdrh     sqlite3VdbeAddOp4Int(v, opSeek, iDataCur, iLabel, iPk, nPk);
76349d9ba63Sdrh     VdbeCoverageIf(v, opSeek==OP_NotExists);
76449d9ba63Sdrh     VdbeCoverageIf(v, opSeek==OP_NotFound);
765688852abSdrh   }
7662283d46cSdan 
7672283d46cSdan   /* If there are any triggers to fire, allocate a range of registers to
7682283d46cSdan   ** use for the old.* references in the triggers.  */
769e7a94d81Sdan   if( sqlite3FkRequired(pParse, pTab, 0, 0) || pTrigger ){
7702283d46cSdan     u32 mask;                     /* Mask of OLD.* columns in use */
7712283d46cSdan     int iCol;                     /* Iterator used while populating OLD.* */
77283d47afeSdrh     int addrStart;                /* Start of BEFORE trigger programs */
7732283d46cSdan 
7742283d46cSdan     /* TODO: Could use temporary registers here. Also could attempt to
7752283d46cSdan     ** avoid copying the contents of the rowid register.  */
776bb5f168fSdan     mask = sqlite3TriggerColmask(
777bb5f168fSdan         pParse, pTrigger, 0, 0, TRIGGER_BEFORE|TRIGGER_AFTER, pTab, onconf
778bb5f168fSdan     );
779e7a94d81Sdan     mask |= sqlite3FkOldmask(pParse, pTab);
7802283d46cSdan     iOld = pParse->nMem+1;
7812283d46cSdan     pParse->nMem += (1 + pTab->nCol);
7822283d46cSdan 
7832283d46cSdan     /* Populate the OLD.* pseudo-table register array. These values will be
7842283d46cSdan     ** used by any BEFORE and AFTER triggers that exist.  */
785ecb31b63Sdrh     sqlite3VdbeAddOp2(v, OP_Copy, iPk, iOld);
7862283d46cSdan     for(iCol=0; iCol<pTab->nCol; iCol++){
787693e6719Sdrh       testcase( mask!=0xffffffff && iCol==31 );
7884d06798eSdrh       testcase( mask!=0xffffffff && iCol==32 );
7894d06798eSdrh       if( mask==0xffffffff || (iCol<=31 && (mask & MASKBIT32(iCol))!=0) ){
790f09a14fbSdrh         int kk = sqlite3TableColumnToStorage(pTab, iCol);
791f09a14fbSdrh         sqlite3ExprCodeGetColumnOfTable(v, pTab, iDataCur, iCol, iOld+kk+1);
7922283d46cSdan       }
7932283d46cSdan     }
7942283d46cSdan 
7951da40a38Sdan     /* Invoke BEFORE DELETE trigger programs. */
79683d47afeSdrh     addrStart = sqlite3VdbeCurrentAddr(v);
7972283d46cSdan     sqlite3CodeRowTrigger(pParse, pTrigger,
79894d7f50aSdan         TK_DELETE, 0, TRIGGER_BEFORE, pTab, iOld, onconf, iLabel
7992283d46cSdan     );
8002283d46cSdan 
80183d47afeSdrh     /* If any BEFORE triggers were coded, then seek the cursor to the
80283d47afeSdrh     ** row to be deleted again. It may be that the BEFORE triggers moved
8034cef5b1cSdrh     ** the cursor or already deleted the row that the cursor was
80483d47afeSdrh     ** pointing to.
80568116939Sdrh     **
80668116939Sdrh     ** Also disable the iIdxNoSeek optimization since the BEFORE trigger
80768116939Sdrh     ** may have moved that cursor.
80883d47afeSdrh     */
80983d47afeSdrh     if( addrStart<sqlite3VdbeCurrentAddr(v) ){
81026198bb4Sdrh       sqlite3VdbeAddOp4Int(v, opSeek, iDataCur, iLabel, iPk, nPk);
81149d9ba63Sdrh       VdbeCoverageIf(v, opSeek==OP_NotExists);
81249d9ba63Sdrh       VdbeCoverageIf(v, opSeek==OP_NotFound);
81368116939Sdrh       testcase( iIdxNoSeek>=0 );
81468116939Sdrh       iIdxNoSeek = -1;
81583d47afeSdrh     }
8161da40a38Sdan 
8171da40a38Sdan     /* Do FK processing. This call checks that any FK constraints that
8181da40a38Sdan     ** refer to this table (i.e. constraints attached to other tables)
8191da40a38Sdan     ** are not violated by deleting this row.  */
8208ff2d956Sdan     sqlite3FkCheck(pParse, pTab, iOld, 0, 0, 0);
8212283d46cSdan   }
8222283d46cSdan 
8232283d46cSdan   /* Delete the index and table entries. Skip this step if pTab is really
8242283d46cSdan   ** a view (in which case the only effect of the DELETE statement is to
82546c47d46Sdan   ** fire the INSTEAD OF triggers).
82646c47d46Sdan   **
82746c47d46Sdan   ** If variable 'count' is non-zero, then this OP_Delete instruction should
82846c47d46Sdan   ** invoke the update-hook. The pre-update-hook, on the other hand should
82946c47d46Sdan   ** be invoked unless table pTab is a system table. The difference is that
83046c47d46Sdan   ** the update-hook is not invoked for rows removed by REPLACE, but the
83146c47d46Sdan   ** pre-update-hook is.
83246c47d46Sdan   */
833f38524d2Sdrh   if( !IsView(pTab) ){
834e807bdbaSdrh     u8 p5 = 0;
835f0ee1d3cSdan     sqlite3GenerateRowIndexDelete(pParse, pTab, iDataCur, iIdxCur,0,iIdxNoSeek);
83626198bb4Sdrh     sqlite3VdbeAddOp2(v, OP_Delete, iDataCur, (count?OPFLAG_NCHANGE:0));
837614efe2bSdan     if( pParse->nested==0 || 0==sqlite3_stricmp(pTab->zName, "sqlite_stat1") ){
838f14b7fb7Sdrh       sqlite3VdbeAppendP4(v, (char*)pTab, P4_TABLE);
83950133deaSdan     }
840b89aeb6aSdrh     if( eMode!=ONEPASS_OFF ){
841def19e3bSdrh       sqlite3VdbeChangeP5(v, OPFLAG_AUXDELETE);
842b89aeb6aSdrh     }
843ad1d9a87Sdrh     if( iIdxNoSeek>=0 && iIdxNoSeek!=iDataCur ){
844f0ee1d3cSdan       sqlite3VdbeAddOp1(v, OP_Delete, iIdxNoSeek);
845f0ee1d3cSdan     }
846e807bdbaSdrh     if( eMode==ONEPASS_MULTI ) p5 |= OPFLAG_SAVEPOSITION;
847e807bdbaSdrh     sqlite3VdbeChangeP5(v, p5);
84894eb6a14Sdanielk1977   }
8492283d46cSdan 
8501da40a38Sdan   /* Do any ON CASCADE, SET NULL or SET DEFAULT operations required to
8511da40a38Sdan   ** handle rows (possibly in other tables) that refer via a foreign key
8521da40a38Sdan   ** to the row just deleted. */
8538ff2d956Sdan   sqlite3FkActions(pParse, pTab, 0, iOld, 0, 0);
8541da40a38Sdan 
8551da40a38Sdan   /* Invoke AFTER DELETE trigger programs. */
8562283d46cSdan   sqlite3CodeRowTrigger(pParse, pTrigger,
85794d7f50aSdan       TK_DELETE, 0, TRIGGER_AFTER, pTab, iOld, onconf, iLabel
8582283d46cSdan   );
8592283d46cSdan 
8602283d46cSdan   /* Jump here if the row had already been deleted before any BEFORE
8612283d46cSdan   ** trigger programs were invoked. Or if a trigger program throws a
8622283d46cSdan   ** RAISE(IGNORE) exception.  */
8632283d46cSdan   sqlite3VdbeResolveLabel(v, iLabel);
8646934fc7bSdrh   VdbeModuleComment((v, "END: GenRowDel()"));
8650ca3e24bSdrh }
8660ca3e24bSdrh 
8670ca3e24bSdrh /*
8680ca3e24bSdrh ** This routine generates VDBE code that causes the deletion of all
8696934fc7bSdrh ** index entries associated with a single row of a single table, pTab
8700ca3e24bSdrh **
8716934fc7bSdrh ** Preconditions:
8720ca3e24bSdrh **
8736934fc7bSdrh **   1.  A read/write cursor "iDataCur" must be open on the canonical storage
8746934fc7bSdrh **       btree for the table pTab.  (This will be either the table itself
87526198bb4Sdrh **       for rowid tables or to the primary key index for WITHOUT ROWID
8766934fc7bSdrh **       tables.)
8770ca3e24bSdrh **
8780ca3e24bSdrh **   2.  Read/write cursors for all indices of pTab must be open as
8796934fc7bSdrh **       cursor number iIdxCur+i for the i-th index.  (The pTab->pIndex
8806934fc7bSdrh **       index is the 0-th index.)
8810ca3e24bSdrh **
8826934fc7bSdrh **   3.  The "iDataCur" cursor must be already be positioned on the row
8836934fc7bSdrh **       that is to be deleted.
8840ca3e24bSdrh */
sqlite3GenerateRowIndexDelete(Parse * pParse,Table * pTab,int iDataCur,int iIdxCur,int * aRegIdx,int iIdxNoSeek)8854adee20fSdanielk1977 void sqlite3GenerateRowIndexDelete(
8862d401ab8Sdrh   Parse *pParse,     /* Parsing and code generating context */
8870ca3e24bSdrh   Table *pTab,       /* Table containing the row to be deleted */
88826198bb4Sdrh   int iDataCur,      /* Cursor of table holding data. */
88926198bb4Sdrh   int iIdxCur,       /* First index cursor */
890f0ee1d3cSdan   int *aRegIdx,      /* Only delete if aRegIdx!=0 && aRegIdx[i]>0 */
891f0ee1d3cSdan   int iIdxNoSeek     /* Do not delete from this cursor */
8920ca3e24bSdrh ){
8936934fc7bSdrh   int i;             /* Index loop counter */
8941c2c0b77Sdrh   int r1 = -1;       /* Register holding an index key */
8956934fc7bSdrh   int iPartIdxLabel; /* Jump destination for skipping partial index entries */
8966934fc7bSdrh   Index *pIdx;       /* Current index */
8971c2c0b77Sdrh   Index *pPrior = 0; /* Prior index */
8986934fc7bSdrh   Vdbe *v;           /* The prepared statement under construction */
8996934fc7bSdrh   Index *pPk;        /* PRIMARY KEY index, or NULL for rowid tables */
9009cfcf5d4Sdrh 
9016934fc7bSdrh   v = pParse->pVdbe;
902261c02d9Sdrh   pPk = HasRowid(pTab) ? 0 : sqlite3PrimaryKeyIndex(pTab);
90326198bb4Sdrh   for(i=0, pIdx=pTab->pIndex; pIdx; i++, pIdx=pIdx->pNext){
9046934fc7bSdrh     assert( iIdxCur+i!=iDataCur || pPk==pIdx );
90526198bb4Sdrh     if( aRegIdx!=0 && aRegIdx[i]==0 ) continue;
906261c02d9Sdrh     if( pIdx==pPk ) continue;
907f0ee1d3cSdan     if( iIdxCur+i==iIdxNoSeek ) continue;
908392ee21dSdrh     VdbeModuleComment((v, "GenRowIdxDel for %s", pIdx->zName));
9091c2c0b77Sdrh     r1 = sqlite3GenerateIndexKey(pParse, pIdx, iDataCur, 0, 1,
9101c2c0b77Sdrh         &iPartIdxLabel, pPrior, r1);
91126198bb4Sdrh     sqlite3VdbeAddOp3(v, OP_IdxDelete, iIdxCur+i, r1,
9129eade087Sdrh         pIdx->uniqNotNull ? pIdx->nKeyCol : pIdx->nColumn);
91385bd353aSdrh     sqlite3VdbeChangeP5(v, 1);  /* Cause IdxDelete to error if no entry found */
91487744513Sdrh     sqlite3ResolvePartIdxLabel(pParse, iPartIdxLabel);
9151c2c0b77Sdrh     pPrior = pIdx;
91651846b56Sdrh   }
91751846b56Sdrh }
91851846b56Sdrh 
91951846b56Sdrh /*
9209eade087Sdrh ** Generate code that will assemble an index key and stores it in register
921e14006d0Sdrh ** regOut.  The key with be for index pIdx which is an index on pTab.
92251846b56Sdrh ** iCur is the index of a cursor open on the pTab table and pointing to
92326198bb4Sdrh ** the entry that needs indexing.  If pTab is a WITHOUT ROWID table, then
92426198bb4Sdrh ** iCur must be the cursor of the PRIMARY KEY index.
9252d401ab8Sdrh **
9262d401ab8Sdrh ** Return a register number which is the first in a block of
9272d401ab8Sdrh ** registers that holds the elements of the index key.  The
9282d401ab8Sdrh ** block of registers has already been deallocated by the time
9292d401ab8Sdrh ** this routine returns.
930b2b9d3d7Sdrh **
931b2b9d3d7Sdrh ** If *piPartIdxLabel is not NULL, fill it in with a label and jump
932b2b9d3d7Sdrh ** to that label if pIdx is a partial index that should be skipped.
93387744513Sdrh ** The label should be resolved using sqlite3ResolvePartIdxLabel().
934b2b9d3d7Sdrh ** A partial index should be skipped if its WHERE clause evaluates
935b2b9d3d7Sdrh ** to false or null.  If pIdx is not a partial index, *piPartIdxLabel
936b2b9d3d7Sdrh ** will be set to zero which is an empty label that is ignored by
93787744513Sdrh ** sqlite3ResolvePartIdxLabel().
9381c2c0b77Sdrh **
9391c2c0b77Sdrh ** The pPrior and regPrior parameters are used to implement a cache to
9401c2c0b77Sdrh ** avoid unnecessary register loads.  If pPrior is not NULL, then it is
9411c2c0b77Sdrh ** a pointer to a different index for which an index key has just been
9421c2c0b77Sdrh ** computed into register regPrior.  If the current pIdx index is generating
9431c2c0b77Sdrh ** its key into the same sequence of registers and if pPrior and pIdx share
9441c2c0b77Sdrh ** a column in common, then the register corresponding to that column already
9451c2c0b77Sdrh ** holds the correct value and the loading of that register is skipped.
9461c2c0b77Sdrh ** This optimization is helpful when doing a DELETE or an INTEGRITY_CHECK
9471c2c0b77Sdrh ** on a table with multiple indices, and especially with the ROWID or
9481c2c0b77Sdrh ** PRIMARY KEY columns of the index.
94951846b56Sdrh */
sqlite3GenerateIndexKey(Parse * pParse,Index * pIdx,int iDataCur,int regOut,int prefixOnly,int * piPartIdxLabel,Index * pPrior,int regPrior)9502d401ab8Sdrh int sqlite3GenerateIndexKey(
9512d401ab8Sdrh   Parse *pParse,       /* Parsing context */
95251846b56Sdrh   Index *pIdx,         /* The index for which to generate a key */
95326198bb4Sdrh   int iDataCur,        /* Cursor number from which to take column data */
9549eade087Sdrh   int regOut,          /* Put the new key into this register if not 0 */
9559eade087Sdrh   int prefixOnly,      /* Compute only a unique prefix of the key */
9561c2c0b77Sdrh   int *piPartIdxLabel, /* OUT: Jump to this label to skip partial index */
9571c2c0b77Sdrh   Index *pPrior,       /* Previously generated index key */
9581c2c0b77Sdrh   int regPrior         /* Register holding previous generated key */
95951846b56Sdrh ){
9602d401ab8Sdrh   Vdbe *v = pParse->pVdbe;
96151846b56Sdrh   int j;
9622d401ab8Sdrh   int regBase;
9632d401ab8Sdrh   int nCol;
96451846b56Sdrh 
965b2b9d3d7Sdrh   if( piPartIdxLabel ){
966b2b9d3d7Sdrh     if( pIdx->pPartIdxWhere ){
967ec4ccdbcSdrh       *piPartIdxLabel = sqlite3VdbeMakeLabel(pParse);
9683e34eabcSdrh       pParse->iSelfTab = iDataCur + 1;
96972bc8208Sdrh       sqlite3ExprIfFalseDup(pParse, pIdx->pPartIdxWhere, *piPartIdxLabel,
970b2b9d3d7Sdrh                             SQLITE_JUMPIFNULL);
9713e34eabcSdrh       pParse->iSelfTab = 0;
97239b30a7dSdrh       pPrior = 0; /* Ticket a9efb42811fa41ee 2019-11-02;
97339b30a7dSdrh                   ** pPartIdxWhere may have corrupted regPrior registers */
974b2b9d3d7Sdrh     }else{
975b2b9d3d7Sdrh       *piPartIdxLabel = 0;
976b2b9d3d7Sdrh     }
977b2b9d3d7Sdrh   }
9789eade087Sdrh   nCol = (prefixOnly && pIdx->uniqNotNull) ? pIdx->nKeyCol : pIdx->nColumn;
9794415628aSdrh   regBase = sqlite3GetTempRange(pParse, nCol);
9801c2c0b77Sdrh   if( pPrior && (regBase!=regPrior || pPrior->pPartIdxWhere) ) pPrior = 0;
9812d401ab8Sdrh   for(j=0; j<nCol; j++){
9821f9ca2c8Sdrh     if( pPrior
9831f9ca2c8Sdrh      && pPrior->aiColumn[j]==pIdx->aiColumn[j]
9844b92f98cSdrh      && pPrior->aiColumn[j]!=XN_EXPR
9851f9ca2c8Sdrh     ){
9861f9ca2c8Sdrh       /* This column was already computed by the previous index */
9871f9ca2c8Sdrh       continue;
9881f9ca2c8Sdrh     }
9891f9ca2c8Sdrh     sqlite3ExprCodeLoadIndexColumn(pParse, pIdx, iDataCur, j, regBase+j);
99021571a94Sdrh     if( pIdx->aiColumn[j]>=0 ){
991762c1c40Sdrh       /* If the column affinity is REAL but the number is an integer, then it
992762c1c40Sdrh       ** might be stored in the table as an integer (using a compact
993762c1c40Sdrh       ** representation) then converted to REAL by an OP_RealAffinity opcode.
994762c1c40Sdrh       ** But we are getting ready to store this value back into an index, where
99521571a94Sdrh       ** it should be converted by to INTEGER again.  So omit the
99621571a94Sdrh       ** OP_RealAffinity opcode if it is present */
99761019c78Sdrh       sqlite3VdbeDeletePriorOpcode(v, OP_RealAffinity);
9989cfcf5d4Sdrh     }
99921571a94Sdrh   }
10009eade087Sdrh   if( regOut ){
10014415628aSdrh     sqlite3VdbeAddOp3(v, OP_MakeRecord, regBase, nCol, regOut);
1002e14006d0Sdrh   }
10034415628aSdrh   sqlite3ReleaseTempRange(pParse, regBase, nCol);
10042d401ab8Sdrh   return regBase;
10059cfcf5d4Sdrh }
100687744513Sdrh 
100787744513Sdrh /*
100887744513Sdrh ** If a prior call to sqlite3GenerateIndexKey() generated a jump-over label
100987744513Sdrh ** because it was a partial index, then this routine should be called to
101087744513Sdrh ** resolve that label.
101187744513Sdrh */
sqlite3ResolvePartIdxLabel(Parse * pParse,int iLabel)101287744513Sdrh void sqlite3ResolvePartIdxLabel(Parse *pParse, int iLabel){
101387744513Sdrh   if( iLabel ){
101487744513Sdrh     sqlite3VdbeResolveLabel(pParse->pVdbe, iLabel);
101587744513Sdrh   }
101687744513Sdrh }
1017