000001  /*
000002  ** 2001 September 15
000003  **
000004  ** The author disclaims copyright to this source code.  In place of
000005  ** a legal notice, here is a blessing:
000006  **
000007  **    May you do good and not evil.
000008  **    May you find forgiveness for yourself and forgive others.
000009  **    May you share freely, never taking more than you give.
000010  **
000011  *************************************************************************
000012  ** This file contains C code routines that are called by the parser
000013  ** in order to generate code for DELETE FROM statements.
000014  */
000015  #include "sqliteInt.h"
000016  
000017  /*
000018  ** While a SrcList can in general represent multiple tables and subqueries
000019  ** (as in the FROM clause of a SELECT statement) in this case it contains
000020  ** the name of a single table, as one might find in an INSERT, DELETE,
000021  ** or UPDATE statement.  Look up that table in the symbol table and
000022  ** return a pointer.  Set an error message and return NULL if the table 
000023  ** name is not found or if any other error occurs.
000024  **
000025  ** The following fields are initialized appropriate in pSrc:
000026  **
000027  **    pSrc->a[0].pTab       Pointer to the Table object
000028  **    pSrc->a[0].pIndex     Pointer to the INDEXED BY index, if there is one
000029  **
000030  */
000031  Table *sqlite3SrcListLookup(Parse *pParse, SrcList *pSrc){
000032    struct SrcList_item *pItem = pSrc->a;
000033    Table *pTab;
000034    assert( pItem && pSrc->nSrc==1 );
000035    pTab = sqlite3LocateTableItem(pParse, 0, pItem);
000036    sqlite3DeleteTable(pParse->db, pItem->pTab);
000037    pItem->pTab = pTab;
000038    if( pTab ){
000039      pTab->nTabRef++;
000040    }
000041    if( sqlite3IndexedByLookup(pParse, pItem) ){
000042      pTab = 0;
000043    }
000044    return pTab;
000045  }
000046  
000047  /* Return true if table pTab is read-only.
000048  **
000049  ** A table is read-only if any of the following are true:
000050  **
000051  **   1) It is a virtual table and no implementation of the xUpdate method
000052  **      has been provided
000053  **
000054  **   2) It is a system table (i.e. sqlite_master), this call is not
000055  **      part of a nested parse and writable_schema pragma has not 
000056  **      been specified
000057  **
000058  **   3) The table is a shadow table, the database connection is in
000059  **      defensive mode, and the current sqlite3_prepare()
000060  **      is for a top-level SQL statement.
000061  */
000062  static int tabIsReadOnly(Parse *pParse, Table *pTab){
000063    sqlite3 *db;
000064    if( IsVirtual(pTab) ){
000065      return sqlite3GetVTable(pParse->db, pTab)->pMod->pModule->xUpdate==0;
000066    }
000067    if( (pTab->tabFlags & (TF_Readonly|TF_Shadow))==0 ) return 0;
000068    db = pParse->db;
000069    if( (pTab->tabFlags & TF_Readonly)!=0 ){
000070      return sqlite3WritableSchema(db)==0 && pParse->nested==0;
000071    }
000072    assert( pTab->tabFlags & TF_Shadow );
000073    return sqlite3ReadOnlyShadowTables(db);
000074  }
000075  
000076  /*
000077  ** Check to make sure the given table is writable.  If it is not
000078  ** writable, generate an error message and return 1.  If it is
000079  ** writable return 0;
000080  */
000081  int sqlite3IsReadOnly(Parse *pParse, Table *pTab, int viewOk){
000082    if( tabIsReadOnly(pParse, pTab) ){
000083      sqlite3ErrorMsg(pParse, "table %s may not be modified", pTab->zName);
000084      return 1;
000085    }
000086  #ifndef SQLITE_OMIT_VIEW
000087    if( !viewOk && pTab->pSelect ){
000088      sqlite3ErrorMsg(pParse,"cannot modify %s because it is a view",pTab->zName);
000089      return 1;
000090    }
000091  #endif
000092    return 0;
000093  }
000094  
000095  
000096  #if !defined(SQLITE_OMIT_VIEW) && !defined(SQLITE_OMIT_TRIGGER)
000097  /*
000098  ** Evaluate a view and store its result in an ephemeral table.  The
000099  ** pWhere argument is an optional WHERE clause that restricts the
000100  ** set of rows in the view that are to be added to the ephemeral table.
000101  */
000102  void sqlite3MaterializeView(
000103    Parse *pParse,       /* Parsing context */
000104    Table *pView,        /* View definition */
000105    Expr *pWhere,        /* Optional WHERE clause to be added */
000106    ExprList *pOrderBy,  /* Optional ORDER BY clause */
000107    Expr *pLimit,        /* Optional LIMIT clause */
000108    int iCur             /* Cursor number for ephemeral table */
000109  ){
000110    SelectDest dest;
000111    Select *pSel;
000112    SrcList *pFrom;
000113    sqlite3 *db = pParse->db;
000114    int iDb = sqlite3SchemaToIndex(db, pView->pSchema);
000115    pWhere = sqlite3ExprDup(db, pWhere, 0);
000116    pFrom = sqlite3SrcListAppend(pParse, 0, 0, 0);
000117    if( pFrom ){
000118      assert( pFrom->nSrc==1 );
000119      pFrom->a[0].zName = sqlite3DbStrDup(db, pView->zName);
000120      pFrom->a[0].zDatabase = sqlite3DbStrDup(db, db->aDb[iDb].zDbSName);
000121      assert( pFrom->a[0].pOn==0 );
000122      assert( pFrom->a[0].pUsing==0 );
000123    }
000124    pSel = sqlite3SelectNew(pParse, 0, pFrom, pWhere, 0, 0, pOrderBy, 
000125                            SF_IncludeHidden, pLimit);
000126    sqlite3SelectDestInit(&dest, SRT_EphemTab, iCur);
000127    sqlite3Select(pParse, pSel, &dest);
000128    sqlite3SelectDelete(db, pSel);
000129  }
000130  #endif /* !defined(SQLITE_OMIT_VIEW) && !defined(SQLITE_OMIT_TRIGGER) */
000131  
000132  #if defined(SQLITE_ENABLE_UPDATE_DELETE_LIMIT) && !defined(SQLITE_OMIT_SUBQUERY)
000133  /*
000134  ** Generate an expression tree to implement the WHERE, ORDER BY,
000135  ** and LIMIT/OFFSET portion of DELETE and UPDATE statements.
000136  **
000137  **     DELETE FROM table_wxyz WHERE a<5 ORDER BY a LIMIT 1;
000138  **                            \__________________________/
000139  **                               pLimitWhere (pInClause)
000140  */
000141  Expr *sqlite3LimitWhere(
000142    Parse *pParse,               /* The parser context */
000143    SrcList *pSrc,               /* the FROM clause -- which tables to scan */
000144    Expr *pWhere,                /* The WHERE clause.  May be null */
000145    ExprList *pOrderBy,          /* The ORDER BY clause.  May be null */
000146    Expr *pLimit,                /* The LIMIT clause.  May be null */
000147    char *zStmtType              /* Either DELETE or UPDATE.  For err msgs. */
000148  ){
000149    sqlite3 *db = pParse->db;
000150    Expr *pLhs = NULL;           /* LHS of IN(SELECT...) operator */
000151    Expr *pInClause = NULL;      /* WHERE rowid IN ( select ) */
000152    ExprList *pEList = NULL;     /* Expression list contaning only pSelectRowid */
000153    SrcList *pSelectSrc = NULL;  /* SELECT rowid FROM x ... (dup of pSrc) */
000154    Select *pSelect = NULL;      /* Complete SELECT tree */
000155    Table *pTab;
000156  
000157    /* Check that there isn't an ORDER BY without a LIMIT clause.
000158    */
000159    if( pOrderBy && pLimit==0 ) {
000160      sqlite3ErrorMsg(pParse, "ORDER BY without LIMIT on %s", zStmtType);
000161      sqlite3ExprDelete(pParse->db, pWhere);
000162      sqlite3ExprListDelete(pParse->db, pOrderBy);
000163      return 0;
000164    }
000165  
000166    /* We only need to generate a select expression if there
000167    ** is a limit/offset term to enforce.
000168    */
000169    if( pLimit == 0 ) {
000170      return pWhere;
000171    }
000172  
000173    /* Generate a select expression tree to enforce the limit/offset 
000174    ** term for the DELETE or UPDATE statement.  For example:
000175    **   DELETE FROM table_a WHERE col1=1 ORDER BY col2 LIMIT 1 OFFSET 1
000176    ** becomes:
000177    **   DELETE FROM table_a WHERE rowid IN ( 
000178    **     SELECT rowid FROM table_a WHERE col1=1 ORDER BY col2 LIMIT 1 OFFSET 1
000179    **   );
000180    */
000181  
000182    pTab = pSrc->a[0].pTab;
000183    if( HasRowid(pTab) ){
000184      pLhs = sqlite3PExpr(pParse, TK_ROW, 0, 0);
000185      pEList = sqlite3ExprListAppend(
000186          pParse, 0, sqlite3PExpr(pParse, TK_ROW, 0, 0)
000187      );
000188    }else{
000189      Index *pPk = sqlite3PrimaryKeyIndex(pTab);
000190      if( pPk->nKeyCol==1 ){
000191        const char *zName = pTab->aCol[pPk->aiColumn[0]].zName;
000192        pLhs = sqlite3Expr(db, TK_ID, zName);
000193        pEList = sqlite3ExprListAppend(pParse, 0, sqlite3Expr(db, TK_ID, zName));
000194      }else{
000195        int i;
000196        for(i=0; i<pPk->nKeyCol; i++){
000197          Expr *p = sqlite3Expr(db, TK_ID, pTab->aCol[pPk->aiColumn[i]].zName);
000198          pEList = sqlite3ExprListAppend(pParse, pEList, p);
000199        }
000200        pLhs = sqlite3PExpr(pParse, TK_VECTOR, 0, 0);
000201        if( pLhs ){
000202          pLhs->x.pList = sqlite3ExprListDup(db, pEList, 0);
000203        }
000204      }
000205    }
000206  
000207    /* duplicate the FROM clause as it is needed by both the DELETE/UPDATE tree
000208    ** and the SELECT subtree. */
000209    pSrc->a[0].pTab = 0;
000210    pSelectSrc = sqlite3SrcListDup(pParse->db, pSrc, 0);
000211    pSrc->a[0].pTab = pTab;
000212    pSrc->a[0].pIBIndex = 0;
000213  
000214    /* generate the SELECT expression tree. */
000215    pSelect = sqlite3SelectNew(pParse, pEList, pSelectSrc, pWhere, 0 ,0, 
000216        pOrderBy,0,pLimit
000217    );
000218  
000219    /* now generate the new WHERE rowid IN clause for the DELETE/UDPATE */
000220    pInClause = sqlite3PExpr(pParse, TK_IN, pLhs, 0);
000221    sqlite3PExprAddSelect(pParse, pInClause, pSelect);
000222    return pInClause;
000223  }
000224  #endif /* defined(SQLITE_ENABLE_UPDATE_DELETE_LIMIT) */
000225         /*      && !defined(SQLITE_OMIT_SUBQUERY) */
000226  
000227  /*
000228  ** Generate code for a DELETE FROM statement.
000229  **
000230  **     DELETE FROM table_wxyz WHERE a<5 AND b NOT NULL;
000231  **                 \________/       \________________/
000232  **                  pTabList              pWhere
000233  */
000234  void sqlite3DeleteFrom(
000235    Parse *pParse,         /* The parser context */
000236    SrcList *pTabList,     /* The table from which we should delete things */
000237    Expr *pWhere,          /* The WHERE clause.  May be null */
000238    ExprList *pOrderBy,    /* ORDER BY clause. May be null */
000239    Expr *pLimit           /* LIMIT clause. May be null */
000240  ){
000241    Vdbe *v;               /* The virtual database engine */
000242    Table *pTab;           /* The table from which records will be deleted */
000243    int i;                 /* Loop counter */
000244    WhereInfo *pWInfo;     /* Information about the WHERE clause */
000245    Index *pIdx;           /* For looping over indices of the table */
000246    int iTabCur;           /* Cursor number for the table */
000247    int iDataCur = 0;      /* VDBE cursor for the canonical data source */
000248    int iIdxCur = 0;       /* Cursor number of the first index */
000249    int nIdx;              /* Number of indices */
000250    sqlite3 *db;           /* Main database structure */
000251    AuthContext sContext;  /* Authorization context */
000252    NameContext sNC;       /* Name context to resolve expressions in */
000253    int iDb;               /* Database number */
000254    int memCnt = 0;        /* Memory cell used for change counting */
000255    int rcauth;            /* Value returned by authorization callback */
000256    int eOnePass;          /* ONEPASS_OFF or _SINGLE or _MULTI */
000257    int aiCurOnePass[2];   /* The write cursors opened by WHERE_ONEPASS */
000258    u8 *aToOpen = 0;       /* Open cursor iTabCur+j if aToOpen[j] is true */
000259    Index *pPk;            /* The PRIMARY KEY index on the table */
000260    int iPk = 0;           /* First of nPk registers holding PRIMARY KEY value */
000261    i16 nPk = 1;           /* Number of columns in the PRIMARY KEY */
000262    int iKey;              /* Memory cell holding key of row to be deleted */
000263    i16 nKey;              /* Number of memory cells in the row key */
000264    int iEphCur = 0;       /* Ephemeral table holding all primary key values */
000265    int iRowSet = 0;       /* Register for rowset of rows to delete */
000266    int addrBypass = 0;    /* Address of jump over the delete logic */
000267    int addrLoop = 0;      /* Top of the delete loop */
000268    int addrEphOpen = 0;   /* Instruction to open the Ephemeral table */
000269    int bComplex;          /* True if there are triggers or FKs or
000270                           ** subqueries in the WHERE clause */
000271   
000272  #ifndef SQLITE_OMIT_TRIGGER
000273    int isView;                  /* True if attempting to delete from a view */
000274    Trigger *pTrigger;           /* List of table triggers, if required */
000275  #endif
000276  
000277    memset(&sContext, 0, sizeof(sContext));
000278    db = pParse->db;
000279    if( pParse->nErr || db->mallocFailed ){
000280      goto delete_from_cleanup;
000281    }
000282    assert( pTabList->nSrc==1 );
000283  
000284  
000285    /* Locate the table which we want to delete.  This table has to be
000286    ** put in an SrcList structure because some of the subroutines we
000287    ** will be calling are designed to work with multiple tables and expect
000288    ** an SrcList* parameter instead of just a Table* parameter.
000289    */
000290    pTab = sqlite3SrcListLookup(pParse, pTabList);
000291    if( pTab==0 )  goto delete_from_cleanup;
000292  
000293    /* Figure out if we have any triggers and if the table being
000294    ** deleted from is a view
000295    */
000296  #ifndef SQLITE_OMIT_TRIGGER
000297    pTrigger = sqlite3TriggersExist(pParse, pTab, TK_DELETE, 0, 0);
000298    isView = pTab->pSelect!=0;
000299  #else
000300  # define pTrigger 0
000301  # define isView 0
000302  #endif
000303    bComplex = pTrigger || sqlite3FkRequired(pParse, pTab, 0, 0);
000304  #ifdef SQLITE_OMIT_VIEW
000305  # undef isView
000306  # define isView 0
000307  #endif
000308  
000309  #ifdef SQLITE_ENABLE_UPDATE_DELETE_LIMIT
000310    if( !isView ){
000311      pWhere = sqlite3LimitWhere(
000312          pParse, pTabList, pWhere, pOrderBy, pLimit, "DELETE"
000313      );
000314      pOrderBy = 0;
000315      pLimit = 0;
000316    }
000317  #endif
000318  
000319    /* If pTab is really a view, make sure it has been initialized.
000320    */
000321    if( sqlite3ViewGetColumnNames(pParse, pTab) ){
000322      goto delete_from_cleanup;
000323    }
000324  
000325    if( sqlite3IsReadOnly(pParse, pTab, (pTrigger?1:0)) ){
000326      goto delete_from_cleanup;
000327    }
000328    iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
000329    assert( iDb<db->nDb );
000330    rcauth = sqlite3AuthCheck(pParse, SQLITE_DELETE, pTab->zName, 0, 
000331                              db->aDb[iDb].zDbSName);
000332    assert( rcauth==SQLITE_OK || rcauth==SQLITE_DENY || rcauth==SQLITE_IGNORE );
000333    if( rcauth==SQLITE_DENY ){
000334      goto delete_from_cleanup;
000335    }
000336    assert(!isView || pTrigger);
000337  
000338    /* Assign cursor numbers to the table and all its indices.
000339    */
000340    assert( pTabList->nSrc==1 );
000341    iTabCur = pTabList->a[0].iCursor = pParse->nTab++;
000342    for(nIdx=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, nIdx++){
000343      pParse->nTab++;
000344    }
000345  
000346    /* Start the view context
000347    */
000348    if( isView ){
000349      sqlite3AuthContextPush(pParse, &sContext, pTab->zName);
000350    }
000351  
000352    /* Begin generating code.
000353    */
000354    v = sqlite3GetVdbe(pParse);
000355    if( v==0 ){
000356      goto delete_from_cleanup;
000357    }
000358    if( pParse->nested==0 ) sqlite3VdbeCountChanges(v);
000359    sqlite3BeginWriteOperation(pParse, bComplex, iDb);
000360  
000361    /* If we are trying to delete from a view, realize that view into
000362    ** an ephemeral table.
000363    */
000364  #if !defined(SQLITE_OMIT_VIEW) && !defined(SQLITE_OMIT_TRIGGER)
000365    if( isView ){
000366      sqlite3MaterializeView(pParse, pTab, 
000367          pWhere, pOrderBy, pLimit, iTabCur
000368      );
000369      iDataCur = iIdxCur = iTabCur;
000370      pOrderBy = 0;
000371      pLimit = 0;
000372    }
000373  #endif
000374  
000375    /* Resolve the column names in the WHERE clause.
000376    */
000377    memset(&sNC, 0, sizeof(sNC));
000378    sNC.pParse = pParse;
000379    sNC.pSrcList = pTabList;
000380    if( sqlite3ResolveExprNames(&sNC, pWhere) ){
000381      goto delete_from_cleanup;
000382    }
000383  
000384    /* Initialize the counter of the number of rows deleted, if
000385    ** we are counting rows.
000386    */
000387    if( (db->flags & SQLITE_CountRows)!=0
000388     && !pParse->nested
000389     && !pParse->pTriggerTab
000390    ){
000391      memCnt = ++pParse->nMem;
000392      sqlite3VdbeAddOp2(v, OP_Integer, 0, memCnt);
000393    }
000394  
000395  #ifndef SQLITE_OMIT_TRUNCATE_OPTIMIZATION
000396    /* Special case: A DELETE without a WHERE clause deletes everything.
000397    ** It is easier just to erase the whole table. Prior to version 3.6.5,
000398    ** this optimization caused the row change count (the value returned by 
000399    ** API function sqlite3_count_changes) to be set incorrectly.
000400    **
000401    ** The "rcauth==SQLITE_OK" terms is the
000402    ** IMPLEMENTATION-OF: R-17228-37124 If the action code is SQLITE_DELETE and
000403    ** the callback returns SQLITE_IGNORE then the DELETE operation proceeds but
000404    ** the truncate optimization is disabled and all rows are deleted
000405    ** individually.
000406    */
000407    if( rcauth==SQLITE_OK
000408     && pWhere==0
000409     && !bComplex
000410     && !IsVirtual(pTab)
000411  #ifdef SQLITE_ENABLE_PREUPDATE_HOOK
000412     && db->xPreUpdateCallback==0
000413  #endif
000414    ){
000415      assert( !isView );
000416      sqlite3TableLock(pParse, iDb, pTab->tnum, 1, pTab->zName);
000417      if( HasRowid(pTab) ){
000418        sqlite3VdbeAddOp4(v, OP_Clear, pTab->tnum, iDb, memCnt ? memCnt : -1,
000419                          pTab->zName, P4_STATIC);
000420      }
000421      for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
000422        assert( pIdx->pSchema==pTab->pSchema );
000423        sqlite3VdbeAddOp2(v, OP_Clear, pIdx->tnum, iDb);
000424      }
000425    }else
000426  #endif /* SQLITE_OMIT_TRUNCATE_OPTIMIZATION */
000427    {
000428      u16 wcf = WHERE_ONEPASS_DESIRED|WHERE_DUPLICATES_OK|WHERE_SEEK_TABLE;
000429      if( sNC.ncFlags & NC_VarSelect ) bComplex = 1;
000430      wcf |= (bComplex ? 0 : WHERE_ONEPASS_MULTIROW);
000431      if( HasRowid(pTab) ){
000432        /* For a rowid table, initialize the RowSet to an empty set */
000433        pPk = 0;
000434        nPk = 1;
000435        iRowSet = ++pParse->nMem;
000436        sqlite3VdbeAddOp2(v, OP_Null, 0, iRowSet);
000437      }else{
000438        /* For a WITHOUT ROWID table, create an ephemeral table used to
000439        ** hold all primary keys for rows to be deleted. */
000440        pPk = sqlite3PrimaryKeyIndex(pTab);
000441        assert( pPk!=0 );
000442        nPk = pPk->nKeyCol;
000443        iPk = pParse->nMem+1;
000444        pParse->nMem += nPk;
000445        iEphCur = pParse->nTab++;
000446        addrEphOpen = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, iEphCur, nPk);
000447        sqlite3VdbeSetP4KeyInfo(pParse, pPk);
000448      }
000449    
000450      /* Construct a query to find the rowid or primary key for every row
000451      ** to be deleted, based on the WHERE clause. Set variable eOnePass
000452      ** to indicate the strategy used to implement this delete:
000453      **
000454      **  ONEPASS_OFF:    Two-pass approach - use a FIFO for rowids/PK values.
000455      **  ONEPASS_SINGLE: One-pass approach - at most one row deleted.
000456      **  ONEPASS_MULTI:  One-pass approach - any number of rows may be deleted.
000457      */
000458      pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, 0, 0, wcf, iTabCur+1);
000459      if( pWInfo==0 ) goto delete_from_cleanup;
000460      eOnePass = sqlite3WhereOkOnePass(pWInfo, aiCurOnePass);
000461      assert( IsVirtual(pTab)==0 || eOnePass!=ONEPASS_MULTI );
000462      assert( IsVirtual(pTab) || bComplex || eOnePass!=ONEPASS_OFF );
000463      if( eOnePass!=ONEPASS_SINGLE ) sqlite3MultiWrite(pParse);
000464    
000465      /* Keep track of the number of rows to be deleted */
000466      if( memCnt ){
000467        sqlite3VdbeAddOp2(v, OP_AddImm, memCnt, 1);
000468      }
000469    
000470      /* Extract the rowid or primary key for the current row */
000471      if( pPk ){
000472        for(i=0; i<nPk; i++){
000473          assert( pPk->aiColumn[i]>=0 );
000474          sqlite3ExprCodeGetColumnOfTable(v, pTab, iTabCur,
000475                                          pPk->aiColumn[i], iPk+i);
000476        }
000477        iKey = iPk;
000478      }else{
000479        iKey = ++pParse->nMem;
000480        sqlite3ExprCodeGetColumnOfTable(v, pTab, iTabCur, -1, iKey);
000481      }
000482    
000483      if( eOnePass!=ONEPASS_OFF ){
000484        /* For ONEPASS, no need to store the rowid/primary-key. There is only
000485        ** one, so just keep it in its register(s) and fall through to the
000486        ** delete code.  */
000487        nKey = nPk; /* OP_Found will use an unpacked key */
000488        aToOpen = sqlite3DbMallocRawNN(db, nIdx+2);
000489        if( aToOpen==0 ){
000490          sqlite3WhereEnd(pWInfo);
000491          goto delete_from_cleanup;
000492        }
000493        memset(aToOpen, 1, nIdx+1);
000494        aToOpen[nIdx+1] = 0;
000495        if( aiCurOnePass[0]>=0 ) aToOpen[aiCurOnePass[0]-iTabCur] = 0;
000496        if( aiCurOnePass[1]>=0 ) aToOpen[aiCurOnePass[1]-iTabCur] = 0;
000497        if( addrEphOpen ) sqlite3VdbeChangeToNoop(v, addrEphOpen);
000498      }else{
000499        if( pPk ){
000500          /* Add the PK key for this row to the temporary table */
000501          iKey = ++pParse->nMem;
000502          nKey = 0;   /* Zero tells OP_Found to use a composite key */
000503          sqlite3VdbeAddOp4(v, OP_MakeRecord, iPk, nPk, iKey,
000504              sqlite3IndexAffinityStr(pParse->db, pPk), nPk);
000505          sqlite3VdbeAddOp4Int(v, OP_IdxInsert, iEphCur, iKey, iPk, nPk);
000506        }else{
000507          /* Add the rowid of the row to be deleted to the RowSet */
000508          nKey = 1;  /* OP_DeferredSeek always uses a single rowid */
000509          sqlite3VdbeAddOp2(v, OP_RowSetAdd, iRowSet, iKey);
000510        }
000511      }
000512    
000513      /* If this DELETE cannot use the ONEPASS strategy, this is the 
000514      ** end of the WHERE loop */
000515      if( eOnePass!=ONEPASS_OFF ){
000516        addrBypass = sqlite3VdbeMakeLabel(pParse);
000517      }else{
000518        sqlite3WhereEnd(pWInfo);
000519      }
000520    
000521      /* Unless this is a view, open cursors for the table we are 
000522      ** deleting from and all its indices. If this is a view, then the
000523      ** only effect this statement has is to fire the INSTEAD OF 
000524      ** triggers.
000525      */
000526      if( !isView ){
000527        int iAddrOnce = 0;
000528        if( eOnePass==ONEPASS_MULTI ){
000529          iAddrOnce = sqlite3VdbeAddOp0(v, OP_Once); VdbeCoverage(v);
000530        }
000531        testcase( IsVirtual(pTab) );
000532        sqlite3OpenTableAndIndices(pParse, pTab, OP_OpenWrite, OPFLAG_FORDELETE,
000533                                   iTabCur, aToOpen, &iDataCur, &iIdxCur);
000534        assert( pPk || IsVirtual(pTab) || iDataCur==iTabCur );
000535        assert( pPk || IsVirtual(pTab) || iIdxCur==iDataCur+1 );
000536        if( eOnePass==ONEPASS_MULTI ) sqlite3VdbeJumpHere(v, iAddrOnce);
000537      }
000538    
000539      /* Set up a loop over the rowids/primary-keys that were found in the
000540      ** where-clause loop above.
000541      */
000542      if( eOnePass!=ONEPASS_OFF ){
000543        assert( nKey==nPk );  /* OP_Found will use an unpacked key */
000544        if( !IsVirtual(pTab) && aToOpen[iDataCur-iTabCur] ){
000545          assert( pPk!=0 || pTab->pSelect!=0 );
000546          sqlite3VdbeAddOp4Int(v, OP_NotFound, iDataCur, addrBypass, iKey, nKey);
000547          VdbeCoverage(v);
000548        }
000549      }else if( pPk ){
000550        addrLoop = sqlite3VdbeAddOp1(v, OP_Rewind, iEphCur); VdbeCoverage(v);
000551        if( IsVirtual(pTab) ){
000552          sqlite3VdbeAddOp3(v, OP_Column, iEphCur, 0, iKey);
000553        }else{
000554          sqlite3VdbeAddOp2(v, OP_RowData, iEphCur, iKey);
000555        }
000556        assert( nKey==0 );  /* OP_Found will use a composite key */
000557      }else{
000558        addrLoop = sqlite3VdbeAddOp3(v, OP_RowSetRead, iRowSet, 0, iKey);
000559        VdbeCoverage(v);
000560        assert( nKey==1 );
000561      }  
000562    
000563      /* Delete the row */
000564  #ifndef SQLITE_OMIT_VIRTUALTABLE
000565      if( IsVirtual(pTab) ){
000566        const char *pVTab = (const char *)sqlite3GetVTable(db, pTab);
000567        sqlite3VtabMakeWritable(pParse, pTab);
000568        assert( eOnePass==ONEPASS_OFF || eOnePass==ONEPASS_SINGLE );
000569        sqlite3MayAbort(pParse);
000570        if( eOnePass==ONEPASS_SINGLE ){
000571          sqlite3VdbeAddOp1(v, OP_Close, iTabCur);
000572          if( sqlite3IsToplevel(pParse) ){
000573            pParse->isMultiWrite = 0;
000574          }
000575        }
000576        sqlite3VdbeAddOp4(v, OP_VUpdate, 0, 1, iKey, pVTab, P4_VTAB);
000577        sqlite3VdbeChangeP5(v, OE_Abort);
000578      }else
000579  #endif
000580      {
000581        int count = (pParse->nested==0);    /* True to count changes */
000582        sqlite3GenerateRowDelete(pParse, pTab, pTrigger, iDataCur, iIdxCur,
000583            iKey, nKey, count, OE_Default, eOnePass, aiCurOnePass[1]);
000584      }
000585    
000586      /* End of the loop over all rowids/primary-keys. */
000587      if( eOnePass!=ONEPASS_OFF ){
000588        sqlite3VdbeResolveLabel(v, addrBypass);
000589        sqlite3WhereEnd(pWInfo);
000590      }else if( pPk ){
000591        sqlite3VdbeAddOp2(v, OP_Next, iEphCur, addrLoop+1); VdbeCoverage(v);
000592        sqlite3VdbeJumpHere(v, addrLoop);
000593      }else{
000594        sqlite3VdbeGoto(v, addrLoop);
000595        sqlite3VdbeJumpHere(v, addrLoop);
000596      }     
000597    } /* End non-truncate path */
000598  
000599    /* Update the sqlite_sequence table by storing the content of the
000600    ** maximum rowid counter values recorded while inserting into
000601    ** autoincrement tables.
000602    */
000603    if( pParse->nested==0 && pParse->pTriggerTab==0 ){
000604      sqlite3AutoincrementEnd(pParse);
000605    }
000606  
000607    /* Return the number of rows that were deleted. If this routine is 
000608    ** generating code because of a call to sqlite3NestedParse(), do not
000609    ** invoke the callback function.
000610    */
000611    if( memCnt ){
000612      sqlite3VdbeAddOp2(v, OP_ResultRow, memCnt, 1);
000613      sqlite3VdbeSetNumCols(v, 1);
000614      sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "rows deleted", SQLITE_STATIC);
000615    }
000616  
000617  delete_from_cleanup:
000618    sqlite3AuthContextPop(&sContext);
000619    sqlite3SrcListDelete(db, pTabList);
000620    sqlite3ExprDelete(db, pWhere);
000621  #if defined(SQLITE_ENABLE_UPDATE_DELETE_LIMIT) 
000622    sqlite3ExprListDelete(db, pOrderBy);
000623    sqlite3ExprDelete(db, pLimit);
000624  #endif
000625    sqlite3DbFree(db, aToOpen);
000626    return;
000627  }
000628  /* Make sure "isView" and other macros defined above are undefined. Otherwise
000629  ** they may interfere with compilation of other functions in this file
000630  ** (or in another file, if this file becomes part of the amalgamation).  */
000631  #ifdef isView
000632   #undef isView
000633  #endif
000634  #ifdef pTrigger
000635   #undef pTrigger
000636  #endif
000637  
000638  /*
000639  ** This routine generates VDBE code that causes a single row of a
000640  ** single table to be deleted.  Both the original table entry and
000641  ** all indices are removed.
000642  **
000643  ** Preconditions:
000644  **
000645  **   1.  iDataCur is an open cursor on the btree that is the canonical data
000646  **       store for the table.  (This will be either the table itself,
000647  **       in the case of a rowid table, or the PRIMARY KEY index in the case
000648  **       of a WITHOUT ROWID table.)
000649  **
000650  **   2.  Read/write cursors for all indices of pTab must be open as
000651  **       cursor number iIdxCur+i for the i-th index.
000652  **
000653  **   3.  The primary key for the row to be deleted must be stored in a
000654  **       sequence of nPk memory cells starting at iPk.  If nPk==0 that means
000655  **       that a search record formed from OP_MakeRecord is contained in the
000656  **       single memory location iPk.
000657  **
000658  ** eMode:
000659  **   Parameter eMode may be passed either ONEPASS_OFF (0), ONEPASS_SINGLE, or
000660  **   ONEPASS_MULTI.  If eMode is not ONEPASS_OFF, then the cursor
000661  **   iDataCur already points to the row to delete. If eMode is ONEPASS_OFF
000662  **   then this function must seek iDataCur to the entry identified by iPk
000663  **   and nPk before reading from it.
000664  **
000665  **   If eMode is ONEPASS_MULTI, then this call is being made as part
000666  **   of a ONEPASS delete that affects multiple rows. In this case, if 
000667  **   iIdxNoSeek is a valid cursor number (>=0) and is not the same as
000668  **   iDataCur, then its position should be preserved following the delete
000669  **   operation. Or, if iIdxNoSeek is not a valid cursor number, the
000670  **   position of iDataCur should be preserved instead.
000671  **
000672  ** iIdxNoSeek:
000673  **   If iIdxNoSeek is a valid cursor number (>=0) not equal to iDataCur,
000674  **   then it identifies an index cursor (from within array of cursors
000675  **   starting at iIdxCur) that already points to the index entry to be deleted.
000676  **   Except, this optimization is disabled if there are BEFORE triggers since
000677  **   the trigger body might have moved the cursor.
000678  */
000679  void sqlite3GenerateRowDelete(
000680    Parse *pParse,     /* Parsing context */
000681    Table *pTab,       /* Table containing the row to be deleted */
000682    Trigger *pTrigger, /* List of triggers to (potentially) fire */
000683    int iDataCur,      /* Cursor from which column data is extracted */
000684    int iIdxCur,       /* First index cursor */
000685    int iPk,           /* First memory cell containing the PRIMARY KEY */
000686    i16 nPk,           /* Number of PRIMARY KEY memory cells */
000687    u8 count,          /* If non-zero, increment the row change counter */
000688    u8 onconf,         /* Default ON CONFLICT policy for triggers */
000689    u8 eMode,          /* ONEPASS_OFF, _SINGLE, or _MULTI.  See above */
000690    int iIdxNoSeek     /* Cursor number of cursor that does not need seeking */
000691  ){
000692    Vdbe *v = pParse->pVdbe;        /* Vdbe */
000693    int iOld = 0;                   /* First register in OLD.* array */
000694    int iLabel;                     /* Label resolved to end of generated code */
000695    u8 opSeek;                      /* Seek opcode */
000696  
000697    /* Vdbe is guaranteed to have been allocated by this stage. */
000698    assert( v );
000699    VdbeModuleComment((v, "BEGIN: GenRowDel(%d,%d,%d,%d)",
000700                           iDataCur, iIdxCur, iPk, (int)nPk));
000701  
000702    /* Seek cursor iCur to the row to delete. If this row no longer exists 
000703    ** (this can happen if a trigger program has already deleted it), do
000704    ** not attempt to delete it or fire any DELETE triggers.  */
000705    iLabel = sqlite3VdbeMakeLabel(pParse);
000706    opSeek = HasRowid(pTab) ? OP_NotExists : OP_NotFound;
000707    if( eMode==ONEPASS_OFF ){
000708      sqlite3VdbeAddOp4Int(v, opSeek, iDataCur, iLabel, iPk, nPk);
000709      VdbeCoverageIf(v, opSeek==OP_NotExists);
000710      VdbeCoverageIf(v, opSeek==OP_NotFound);
000711    }
000712   
000713    /* If there are any triggers to fire, allocate a range of registers to
000714    ** use for the old.* references in the triggers.  */
000715    if( sqlite3FkRequired(pParse, pTab, 0, 0) || pTrigger ){
000716      u32 mask;                     /* Mask of OLD.* columns in use */
000717      int iCol;                     /* Iterator used while populating OLD.* */
000718      int addrStart;                /* Start of BEFORE trigger programs */
000719  
000720      /* TODO: Could use temporary registers here. Also could attempt to
000721      ** avoid copying the contents of the rowid register.  */
000722      mask = sqlite3TriggerColmask(
000723          pParse, pTrigger, 0, 0, TRIGGER_BEFORE|TRIGGER_AFTER, pTab, onconf
000724      );
000725      mask |= sqlite3FkOldmask(pParse, pTab);
000726      iOld = pParse->nMem+1;
000727      pParse->nMem += (1 + pTab->nCol);
000728  
000729      /* Populate the OLD.* pseudo-table register array. These values will be 
000730      ** used by any BEFORE and AFTER triggers that exist.  */
000731      sqlite3VdbeAddOp2(v, OP_Copy, iPk, iOld);
000732      for(iCol=0; iCol<pTab->nCol; iCol++){
000733        testcase( mask!=0xffffffff && iCol==31 );
000734        testcase( mask!=0xffffffff && iCol==32 );
000735        if( mask==0xffffffff || (iCol<=31 && (mask & MASKBIT32(iCol))!=0) ){
000736          int kk = sqlite3TableColumnToStorage(pTab, iCol);
000737          sqlite3ExprCodeGetColumnOfTable(v, pTab, iDataCur, iCol, iOld+kk+1);
000738        }
000739      }
000740  
000741      /* Invoke BEFORE DELETE trigger programs. */
000742      addrStart = sqlite3VdbeCurrentAddr(v);
000743      sqlite3CodeRowTrigger(pParse, pTrigger, 
000744          TK_DELETE, 0, TRIGGER_BEFORE, pTab, iOld, onconf, iLabel
000745      );
000746  
000747      /* If any BEFORE triggers were coded, then seek the cursor to the 
000748      ** row to be deleted again. It may be that the BEFORE triggers moved
000749      ** the cursor or already deleted the row that the cursor was
000750      ** pointing to.
000751      **
000752      ** Also disable the iIdxNoSeek optimization since the BEFORE trigger
000753      ** may have moved that cursor.
000754      */
000755      if( addrStart<sqlite3VdbeCurrentAddr(v) ){
000756        sqlite3VdbeAddOp4Int(v, opSeek, iDataCur, iLabel, iPk, nPk);
000757        VdbeCoverageIf(v, opSeek==OP_NotExists);
000758        VdbeCoverageIf(v, opSeek==OP_NotFound);
000759        testcase( iIdxNoSeek>=0 );
000760        iIdxNoSeek = -1;
000761      }
000762  
000763      /* Do FK processing. This call checks that any FK constraints that
000764      ** refer to this table (i.e. constraints attached to other tables) 
000765      ** are not violated by deleting this row.  */
000766      sqlite3FkCheck(pParse, pTab, iOld, 0, 0, 0);
000767    }
000768  
000769    /* Delete the index and table entries. Skip this step if pTab is really
000770    ** a view (in which case the only effect of the DELETE statement is to
000771    ** fire the INSTEAD OF triggers).  
000772    **
000773    ** If variable 'count' is non-zero, then this OP_Delete instruction should
000774    ** invoke the update-hook. The pre-update-hook, on the other hand should
000775    ** be invoked unless table pTab is a system table. The difference is that
000776    ** the update-hook is not invoked for rows removed by REPLACE, but the 
000777    ** pre-update-hook is.
000778    */ 
000779    if( pTab->pSelect==0 ){
000780      u8 p5 = 0;
000781      sqlite3GenerateRowIndexDelete(pParse, pTab, iDataCur, iIdxCur,0,iIdxNoSeek);
000782      sqlite3VdbeAddOp2(v, OP_Delete, iDataCur, (count?OPFLAG_NCHANGE:0));
000783      if( pParse->nested==0 || 0==sqlite3_stricmp(pTab->zName, "sqlite_stat1") ){
000784        sqlite3VdbeAppendP4(v, (char*)pTab, P4_TABLE);
000785      }
000786      if( eMode!=ONEPASS_OFF ){
000787        sqlite3VdbeChangeP5(v, OPFLAG_AUXDELETE);
000788      }
000789      if( iIdxNoSeek>=0 && iIdxNoSeek!=iDataCur ){
000790        sqlite3VdbeAddOp1(v, OP_Delete, iIdxNoSeek);
000791      }
000792      if( eMode==ONEPASS_MULTI ) p5 |= OPFLAG_SAVEPOSITION;
000793      sqlite3VdbeChangeP5(v, p5);
000794    }
000795  
000796    /* Do any ON CASCADE, SET NULL or SET DEFAULT operations required to
000797    ** handle rows (possibly in other tables) that refer via a foreign key
000798    ** to the row just deleted. */ 
000799    sqlite3FkActions(pParse, pTab, 0, iOld, 0, 0);
000800  
000801    /* Invoke AFTER DELETE trigger programs. */
000802    sqlite3CodeRowTrigger(pParse, pTrigger, 
000803        TK_DELETE, 0, TRIGGER_AFTER, pTab, iOld, onconf, iLabel
000804    );
000805  
000806    /* Jump here if the row had already been deleted before any BEFORE
000807    ** trigger programs were invoked. Or if a trigger program throws a 
000808    ** RAISE(IGNORE) exception.  */
000809    sqlite3VdbeResolveLabel(v, iLabel);
000810    VdbeModuleComment((v, "END: GenRowDel()"));
000811  }
000812  
000813  /*
000814  ** This routine generates VDBE code that causes the deletion of all
000815  ** index entries associated with a single row of a single table, pTab
000816  **
000817  ** Preconditions:
000818  **
000819  **   1.  A read/write cursor "iDataCur" must be open on the canonical storage
000820  **       btree for the table pTab.  (This will be either the table itself
000821  **       for rowid tables or to the primary key index for WITHOUT ROWID
000822  **       tables.)
000823  **
000824  **   2.  Read/write cursors for all indices of pTab must be open as
000825  **       cursor number iIdxCur+i for the i-th index.  (The pTab->pIndex
000826  **       index is the 0-th index.)
000827  **
000828  **   3.  The "iDataCur" cursor must be already be positioned on the row
000829  **       that is to be deleted.
000830  */
000831  void sqlite3GenerateRowIndexDelete(
000832    Parse *pParse,     /* Parsing and code generating context */
000833    Table *pTab,       /* Table containing the row to be deleted */
000834    int iDataCur,      /* Cursor of table holding data. */
000835    int iIdxCur,       /* First index cursor */
000836    int *aRegIdx,      /* Only delete if aRegIdx!=0 && aRegIdx[i]>0 */
000837    int iIdxNoSeek     /* Do not delete from this cursor */
000838  ){
000839    int i;             /* Index loop counter */
000840    int r1 = -1;       /* Register holding an index key */
000841    int iPartIdxLabel; /* Jump destination for skipping partial index entries */
000842    Index *pIdx;       /* Current index */
000843    Index *pPrior = 0; /* Prior index */
000844    Vdbe *v;           /* The prepared statement under construction */
000845    Index *pPk;        /* PRIMARY KEY index, or NULL for rowid tables */
000846  
000847    v = pParse->pVdbe;
000848    pPk = HasRowid(pTab) ? 0 : sqlite3PrimaryKeyIndex(pTab);
000849    for(i=0, pIdx=pTab->pIndex; pIdx; i++, pIdx=pIdx->pNext){
000850      assert( iIdxCur+i!=iDataCur || pPk==pIdx );
000851      if( aRegIdx!=0 && aRegIdx[i]==0 ) continue;
000852      if( pIdx==pPk ) continue;
000853      if( iIdxCur+i==iIdxNoSeek ) continue;
000854      VdbeModuleComment((v, "GenRowIdxDel for %s", pIdx->zName));
000855      r1 = sqlite3GenerateIndexKey(pParse, pIdx, iDataCur, 0, 1,
000856          &iPartIdxLabel, pPrior, r1);
000857      sqlite3VdbeAddOp3(v, OP_IdxDelete, iIdxCur+i, r1,
000858          pIdx->uniqNotNull ? pIdx->nKeyCol : pIdx->nColumn);
000859      sqlite3ResolvePartIdxLabel(pParse, iPartIdxLabel);
000860      pPrior = pIdx;
000861    }
000862  }
000863  
000864  /*
000865  ** Generate code that will assemble an index key and stores it in register
000866  ** regOut.  The key with be for index pIdx which is an index on pTab.
000867  ** iCur is the index of a cursor open on the pTab table and pointing to
000868  ** the entry that needs indexing.  If pTab is a WITHOUT ROWID table, then
000869  ** iCur must be the cursor of the PRIMARY KEY index.
000870  **
000871  ** Return a register number which is the first in a block of
000872  ** registers that holds the elements of the index key.  The
000873  ** block of registers has already been deallocated by the time
000874  ** this routine returns.
000875  **
000876  ** If *piPartIdxLabel is not NULL, fill it in with a label and jump
000877  ** to that label if pIdx is a partial index that should be skipped.
000878  ** The label should be resolved using sqlite3ResolvePartIdxLabel().
000879  ** A partial index should be skipped if its WHERE clause evaluates
000880  ** to false or null.  If pIdx is not a partial index, *piPartIdxLabel
000881  ** will be set to zero which is an empty label that is ignored by
000882  ** sqlite3ResolvePartIdxLabel().
000883  **
000884  ** The pPrior and regPrior parameters are used to implement a cache to
000885  ** avoid unnecessary register loads.  If pPrior is not NULL, then it is
000886  ** a pointer to a different index for which an index key has just been
000887  ** computed into register regPrior.  If the current pIdx index is generating
000888  ** its key into the same sequence of registers and if pPrior and pIdx share
000889  ** a column in common, then the register corresponding to that column already
000890  ** holds the correct value and the loading of that register is skipped.
000891  ** This optimization is helpful when doing a DELETE or an INTEGRITY_CHECK 
000892  ** on a table with multiple indices, and especially with the ROWID or
000893  ** PRIMARY KEY columns of the index.
000894  */
000895  int sqlite3GenerateIndexKey(
000896    Parse *pParse,       /* Parsing context */
000897    Index *pIdx,         /* The index for which to generate a key */
000898    int iDataCur,        /* Cursor number from which to take column data */
000899    int regOut,          /* Put the new key into this register if not 0 */
000900    int prefixOnly,      /* Compute only a unique prefix of the key */
000901    int *piPartIdxLabel, /* OUT: Jump to this label to skip partial index */
000902    Index *pPrior,       /* Previously generated index key */
000903    int regPrior         /* Register holding previous generated key */
000904  ){
000905    Vdbe *v = pParse->pVdbe;
000906    int j;
000907    int regBase;
000908    int nCol;
000909  
000910    if( piPartIdxLabel ){
000911      if( pIdx->pPartIdxWhere ){
000912        *piPartIdxLabel = sqlite3VdbeMakeLabel(pParse);
000913        pParse->iSelfTab = iDataCur + 1;
000914        sqlite3ExprIfFalseDup(pParse, pIdx->pPartIdxWhere, *piPartIdxLabel, 
000915                              SQLITE_JUMPIFNULL);
000916        pParse->iSelfTab = 0;
000917        pPrior = 0; /* Ticket a9efb42811fa41ee 2019-11-02;
000918                    ** pPartIdxWhere may have corrupted regPrior registers */
000919      }else{
000920        *piPartIdxLabel = 0;
000921      }
000922    }
000923    nCol = (prefixOnly && pIdx->uniqNotNull) ? pIdx->nKeyCol : pIdx->nColumn;
000924    regBase = sqlite3GetTempRange(pParse, nCol);
000925    if( pPrior && (regBase!=regPrior || pPrior->pPartIdxWhere) ) pPrior = 0;
000926    for(j=0; j<nCol; j++){
000927      if( pPrior
000928       && pPrior->aiColumn[j]==pIdx->aiColumn[j]
000929       && pPrior->aiColumn[j]!=XN_EXPR
000930      ){
000931        /* This column was already computed by the previous index */
000932        continue;
000933      }
000934      sqlite3ExprCodeLoadIndexColumn(pParse, pIdx, iDataCur, j, regBase+j);
000935      /* If the column affinity is REAL but the number is an integer, then it
000936      ** might be stored in the table as an integer (using a compact
000937      ** representation) then converted to REAL by an OP_RealAffinity opcode.
000938      ** But we are getting ready to store this value back into an index, where
000939      ** it should be converted by to INTEGER again.  So omit the OP_RealAffinity
000940      ** opcode if it is present */
000941      sqlite3VdbeDeletePriorOpcode(v, OP_RealAffinity);
000942    }
000943    if( regOut ){
000944      sqlite3VdbeAddOp3(v, OP_MakeRecord, regBase, nCol, regOut);
000945      if( pIdx->pTable->pSelect ){
000946        const char *zAff = sqlite3IndexAffinityStr(pParse->db, pIdx);
000947        sqlite3VdbeChangeP4(v, -1, zAff, P4_TRANSIENT);
000948      }
000949    }
000950    sqlite3ReleaseTempRange(pParse, regBase, nCol);
000951    return regBase;
000952  }
000953  
000954  /*
000955  ** If a prior call to sqlite3GenerateIndexKey() generated a jump-over label
000956  ** because it was a partial index, then this routine should be called to
000957  ** resolve that label.
000958  */
000959  void sqlite3ResolvePartIdxLabel(Parse *pParse, int iLabel){
000960    if( iLabel ){
000961      sqlite3VdbeResolveLabel(pParse->pVdbe, iLabel);
000962    }
000963  }