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  ** to handle INSERT statements in SQLite.
000014  */
000015  #include "sqliteInt.h"
000016  
000017  /*
000018  ** Generate code that will
000019  **
000020  **   (1) acquire a lock for table pTab then
000021  **   (2) open pTab as cursor iCur.
000022  **
000023  ** If pTab is a WITHOUT ROWID table, then it is the PRIMARY KEY index
000024  ** for that table that is actually opened.
000025  */
000026  void sqlite3OpenTable(
000027    Parse *pParse,  /* Generate code into this VDBE */
000028    int iCur,       /* The cursor number of the table */
000029    int iDb,        /* The database index in sqlite3.aDb[] */
000030    Table *pTab,    /* The table to be opened */
000031    int opcode      /* OP_OpenRead or OP_OpenWrite */
000032  ){
000033    Vdbe *v;
000034    assert( !IsVirtual(pTab) );
000035    assert( pParse->pVdbe!=0 );
000036    v = pParse->pVdbe;
000037    assert( opcode==OP_OpenWrite || opcode==OP_OpenRead );
000038    if( !pParse->db->noSharedCache ){
000039      sqlite3TableLock(pParse, iDb, pTab->tnum,
000040                       (opcode==OP_OpenWrite)?1:0, pTab->zName);
000041    }
000042    if( HasRowid(pTab) ){
000043      sqlite3VdbeAddOp4Int(v, opcode, iCur, pTab->tnum, iDb, pTab->nNVCol);
000044      VdbeComment((v, "%s", pTab->zName));
000045    }else{
000046      Index *pPk = sqlite3PrimaryKeyIndex(pTab);
000047      assert( pPk!=0 );
000048      assert( pPk->tnum==pTab->tnum || CORRUPT_DB );
000049      sqlite3VdbeAddOp3(v, opcode, iCur, pPk->tnum, iDb);
000050      sqlite3VdbeSetP4KeyInfo(pParse, pPk);
000051      VdbeComment((v, "%s", pTab->zName));
000052    }
000053  }
000054  
000055  /*
000056  ** Return a pointer to the column affinity string associated with index
000057  ** pIdx. A column affinity string has one character for each column in
000058  ** the table, according to the affinity of the column:
000059  **
000060  **  Character      Column affinity
000061  **  ------------------------------
000062  **  'A'            BLOB
000063  **  'B'            TEXT
000064  **  'C'            NUMERIC
000065  **  'D'            INTEGER
000066  **  'F'            REAL
000067  **
000068  ** An extra 'D' is appended to the end of the string to cover the
000069  ** rowid that appears as the last column in every index.
000070  **
000071  ** Memory for the buffer containing the column index affinity string
000072  ** is managed along with the rest of the Index structure. It will be
000073  ** released when sqlite3DeleteIndex() is called.
000074  */
000075  static SQLITE_NOINLINE const char *computeIndexAffStr(sqlite3 *db, Index *pIdx){
000076    /* The first time a column affinity string for a particular index is
000077    ** required, it is allocated and populated here. It is then stored as
000078    ** a member of the Index structure for subsequent use.
000079    **
000080    ** The column affinity string will eventually be deleted by
000081    ** sqliteDeleteIndex() when the Index structure itself is cleaned
000082    ** up.
000083    */
000084    int n;
000085    Table *pTab = pIdx->pTable;
000086    pIdx->zColAff = (char *)sqlite3DbMallocRaw(0, pIdx->nColumn+1);
000087    if( !pIdx->zColAff ){
000088      sqlite3OomFault(db);
000089      return 0;
000090    }
000091    for(n=0; n<pIdx->nColumn; n++){
000092      i16 x = pIdx->aiColumn[n];
000093      char aff;
000094      if( x>=0 ){
000095        aff = pTab->aCol[x].affinity;
000096      }else if( x==XN_ROWID ){
000097        aff = SQLITE_AFF_INTEGER;
000098      }else{
000099        assert( x==XN_EXPR );
000100        assert( pIdx->bHasExpr );
000101        assert( pIdx->aColExpr!=0 );
000102        aff = sqlite3ExprAffinity(pIdx->aColExpr->a[n].pExpr);
000103      }
000104      if( aff<SQLITE_AFF_BLOB ) aff = SQLITE_AFF_BLOB;
000105      if( aff>SQLITE_AFF_NUMERIC) aff = SQLITE_AFF_NUMERIC;
000106      pIdx->zColAff[n] = aff;
000107    }
000108    pIdx->zColAff[n] = 0;
000109    return pIdx->zColAff;
000110  }
000111  const char *sqlite3IndexAffinityStr(sqlite3 *db, Index *pIdx){
000112    if( !pIdx->zColAff ) return computeIndexAffStr(db, pIdx);
000113    return pIdx->zColAff;
000114  }
000115  
000116  
000117  /*
000118  ** Compute an affinity string for a table.   Space is obtained
000119  ** from sqlite3DbMalloc().  The caller is responsible for freeing
000120  ** the space when done.
000121  */
000122  char *sqlite3TableAffinityStr(sqlite3 *db, const Table *pTab){
000123    char *zColAff;
000124    zColAff = (char *)sqlite3DbMallocRaw(db, pTab->nCol+1);
000125    if( zColAff ){
000126      int i, j;
000127      for(i=j=0; i<pTab->nCol; i++){
000128        if( (pTab->aCol[i].colFlags & COLFLAG_VIRTUAL)==0 ){
000129          zColAff[j++] = pTab->aCol[i].affinity;
000130        }
000131      }
000132      do{
000133        zColAff[j--] = 0;
000134      }while( j>=0 && zColAff[j]<=SQLITE_AFF_BLOB );
000135    }
000136    return zColAff; 
000137  }
000138  
000139  /*
000140  ** Make changes to the evolving bytecode to do affinity transformations
000141  ** of values that are about to be gathered into a row for table pTab.
000142  **
000143  ** For ordinary (legacy, non-strict) tables:
000144  ** -----------------------------------------
000145  **
000146  ** Compute the affinity string for table pTab, if it has not already been
000147  ** computed.  As an optimization, omit trailing SQLITE_AFF_BLOB affinities.
000148  **
000149  ** If the affinity string is empty (because it was all SQLITE_AFF_BLOB entries
000150  ** which were then optimized out) then this routine becomes a no-op.
000151  **
000152  ** Otherwise if iReg>0 then code an OP_Affinity opcode that will set the
000153  ** affinities for register iReg and following.  Or if iReg==0,
000154  ** then just set the P4 operand of the previous opcode (which should  be
000155  ** an OP_MakeRecord) to the affinity string.
000156  **
000157  ** A column affinity string has one character per column:
000158  **
000159  **    Character      Column affinity
000160  **    ---------      ---------------
000161  **    'A'            BLOB
000162  **    'B'            TEXT
000163  **    'C'            NUMERIC
000164  **    'D'            INTEGER
000165  **    'E'            REAL
000166  **
000167  ** For STRICT tables:
000168  ** ------------------
000169  **
000170  ** Generate an appropriate OP_TypeCheck opcode that will verify the
000171  ** datatypes against the column definitions in pTab.  If iReg==0, that
000172  ** means an OP_MakeRecord opcode has already been generated and should be
000173  ** the last opcode generated.  The new OP_TypeCheck needs to be inserted
000174  ** before the OP_MakeRecord.  The new OP_TypeCheck should use the same
000175  ** register set as the OP_MakeRecord.  If iReg>0 then register iReg is
000176  ** the first of a series of registers that will form the new record.
000177  ** Apply the type checking to that array of registers.
000178  */
000179  void sqlite3TableAffinity(Vdbe *v, Table *pTab, int iReg){
000180    int i;
000181    char *zColAff;
000182    if( pTab->tabFlags & TF_Strict ){
000183      if( iReg==0 ){
000184        /* Move the previous opcode (which should be OP_MakeRecord) forward
000185        ** by one slot and insert a new OP_TypeCheck where the current
000186        ** OP_MakeRecord is found */
000187        VdbeOp *pPrev;
000188        sqlite3VdbeAppendP4(v, pTab, P4_TABLE);
000189        pPrev = sqlite3VdbeGetLastOp(v);
000190        assert( pPrev!=0 );
000191        assert( pPrev->opcode==OP_MakeRecord || sqlite3VdbeDb(v)->mallocFailed );
000192        pPrev->opcode = OP_TypeCheck;
000193        sqlite3VdbeAddOp3(v, OP_MakeRecord, pPrev->p1, pPrev->p2, pPrev->p3);
000194      }else{
000195        /* Insert an isolated OP_Typecheck */
000196        sqlite3VdbeAddOp2(v, OP_TypeCheck, iReg, pTab->nNVCol);
000197        sqlite3VdbeAppendP4(v, pTab, P4_TABLE);
000198      }
000199      return;
000200    }
000201    zColAff = pTab->zColAff;
000202    if( zColAff==0 ){
000203      zColAff = sqlite3TableAffinityStr(0, pTab);
000204      if( !zColAff ){
000205        sqlite3OomFault(sqlite3VdbeDb(v));
000206        return;
000207      }
000208      pTab->zColAff = zColAff;
000209    }
000210    assert( zColAff!=0 );
000211    i = sqlite3Strlen30NN(zColAff);
000212    if( i ){
000213      if( iReg ){
000214        sqlite3VdbeAddOp4(v, OP_Affinity, iReg, i, 0, zColAff, i);
000215      }else{
000216        assert( sqlite3VdbeGetLastOp(v)->opcode==OP_MakeRecord
000217                || sqlite3VdbeDb(v)->mallocFailed );
000218        sqlite3VdbeChangeP4(v, -1, zColAff, i);
000219      }
000220    }
000221  }
000222  
000223  /*
000224  ** Return non-zero if the table pTab in database iDb or any of its indices
000225  ** have been opened at any point in the VDBE program. This is used to see if
000226  ** a statement of the form  "INSERT INTO <iDb, pTab> SELECT ..." can
000227  ** run without using a temporary table for the results of the SELECT.
000228  */
000229  static int readsTable(Parse *p, int iDb, Table *pTab){
000230    Vdbe *v = sqlite3GetVdbe(p);
000231    int i;
000232    int iEnd = sqlite3VdbeCurrentAddr(v);
000233  #ifndef SQLITE_OMIT_VIRTUALTABLE
000234    VTable *pVTab = IsVirtual(pTab) ? sqlite3GetVTable(p->db, pTab) : 0;
000235  #endif
000236  
000237    for(i=1; i<iEnd; i++){
000238      VdbeOp *pOp = sqlite3VdbeGetOp(v, i);
000239      assert( pOp!=0 );
000240      if( pOp->opcode==OP_OpenRead && pOp->p3==iDb ){
000241        Index *pIndex;
000242        Pgno tnum = pOp->p2;
000243        if( tnum==pTab->tnum ){
000244          return 1;
000245        }
000246        for(pIndex=pTab->pIndex; pIndex; pIndex=pIndex->pNext){
000247          if( tnum==pIndex->tnum ){
000248            return 1;
000249          }
000250        }
000251      }
000252  #ifndef SQLITE_OMIT_VIRTUALTABLE
000253      if( pOp->opcode==OP_VOpen && pOp->p4.pVtab==pVTab ){
000254        assert( pOp->p4.pVtab!=0 );
000255        assert( pOp->p4type==P4_VTAB );
000256        return 1;
000257      }
000258  #endif
000259    }
000260    return 0;
000261  }
000262  
000263  /* This walker callback will compute the union of colFlags flags for all
000264  ** referenced columns in a CHECK constraint or generated column expression.
000265  */
000266  static int exprColumnFlagUnion(Walker *pWalker, Expr *pExpr){
000267    if( pExpr->op==TK_COLUMN && pExpr->iColumn>=0 ){
000268      assert( pExpr->iColumn < pWalker->u.pTab->nCol );
000269      pWalker->eCode |= pWalker->u.pTab->aCol[pExpr->iColumn].colFlags;
000270    }
000271    return WRC_Continue;
000272  }
000273  
000274  #ifndef SQLITE_OMIT_GENERATED_COLUMNS
000275  /*
000276  ** All regular columns for table pTab have been puts into registers
000277  ** starting with iRegStore.  The registers that correspond to STORED
000278  ** or VIRTUAL columns have not yet been initialized.  This routine goes
000279  ** back and computes the values for those columns based on the previously
000280  ** computed normal columns.
000281  */
000282  void sqlite3ComputeGeneratedColumns(
000283    Parse *pParse,    /* Parsing context */
000284    int iRegStore,    /* Register holding the first column */
000285    Table *pTab       /* The table */
000286  ){
000287    int i;
000288    Walker w;
000289    Column *pRedo;
000290    int eProgress;
000291    VdbeOp *pOp;
000292  
000293    assert( pTab->tabFlags & TF_HasGenerated );
000294    testcase( pTab->tabFlags & TF_HasVirtual );
000295    testcase( pTab->tabFlags & TF_HasStored );
000296  
000297    /* Before computing generated columns, first go through and make sure
000298    ** that appropriate affinity has been applied to the regular columns
000299    */
000300    sqlite3TableAffinity(pParse->pVdbe, pTab, iRegStore);
000301    if( (pTab->tabFlags & TF_HasStored)!=0 ){
000302      pOp = sqlite3VdbeGetLastOp(pParse->pVdbe);
000303      if( pOp->opcode==OP_Affinity ){
000304        /* Change the OP_Affinity argument to '@' (NONE) for all stored
000305        ** columns.  '@' is the no-op affinity and those columns have not
000306        ** yet been computed. */
000307        int ii, jj;
000308        char *zP4 = pOp->p4.z;
000309        assert( zP4!=0 );
000310        assert( pOp->p4type==P4_DYNAMIC );
000311        for(ii=jj=0; zP4[jj]; ii++){
000312          if( pTab->aCol[ii].colFlags & COLFLAG_VIRTUAL ){
000313            continue;
000314          }
000315          if( pTab->aCol[ii].colFlags & COLFLAG_STORED ){
000316            zP4[jj] = SQLITE_AFF_NONE;
000317          }
000318          jj++;
000319        }
000320      }else if( pOp->opcode==OP_TypeCheck ){
000321        /* If an OP_TypeCheck was generated because the table is STRICT,
000322        ** then set the P3 operand to indicate that generated columns should
000323        ** not be checked */
000324        pOp->p3 = 1;
000325      }
000326    }
000327  
000328    /* Because there can be multiple generated columns that refer to one another,
000329    ** this is a two-pass algorithm.  On the first pass, mark all generated
000330    ** columns as "not available".
000331    */
000332    for(i=0; i<pTab->nCol; i++){
000333      if( pTab->aCol[i].colFlags & COLFLAG_GENERATED ){
000334        testcase( pTab->aCol[i].colFlags & COLFLAG_VIRTUAL );
000335        testcase( pTab->aCol[i].colFlags & COLFLAG_STORED );
000336        pTab->aCol[i].colFlags |= COLFLAG_NOTAVAIL;
000337      }
000338    }
000339  
000340    w.u.pTab = pTab;
000341    w.xExprCallback = exprColumnFlagUnion;
000342    w.xSelectCallback = 0;
000343    w.xSelectCallback2 = 0;
000344  
000345    /* On the second pass, compute the value of each NOT-AVAILABLE column.
000346    ** Companion code in the TK_COLUMN case of sqlite3ExprCodeTarget() will
000347    ** compute dependencies and mark remove the COLSPAN_NOTAVAIL mark, as
000348    ** they are needed.
000349    */
000350    pParse->iSelfTab = -iRegStore;
000351    do{
000352      eProgress = 0;
000353      pRedo = 0;
000354      for(i=0; i<pTab->nCol; i++){
000355        Column *pCol = pTab->aCol + i;
000356        if( (pCol->colFlags & COLFLAG_NOTAVAIL)!=0 ){
000357          int x;
000358          pCol->colFlags |= COLFLAG_BUSY;
000359          w.eCode = 0;
000360          sqlite3WalkExpr(&w, sqlite3ColumnExpr(pTab, pCol));
000361          pCol->colFlags &= ~COLFLAG_BUSY;
000362          if( w.eCode & COLFLAG_NOTAVAIL ){
000363            pRedo = pCol;
000364            continue;
000365          }
000366          eProgress = 1;
000367          assert( pCol->colFlags & COLFLAG_GENERATED );
000368          x = sqlite3TableColumnToStorage(pTab, i) + iRegStore;
000369          sqlite3ExprCodeGeneratedColumn(pParse, pTab, pCol, x);
000370          pCol->colFlags &= ~COLFLAG_NOTAVAIL;
000371        }
000372      }
000373    }while( pRedo && eProgress );
000374    if( pRedo ){
000375      sqlite3ErrorMsg(pParse, "generated column loop on \"%s\"", pRedo->zCnName);
000376    }
000377    pParse->iSelfTab = 0;
000378  }
000379  #endif /* SQLITE_OMIT_GENERATED_COLUMNS */
000380  
000381  
000382  #ifndef SQLITE_OMIT_AUTOINCREMENT
000383  /*
000384  ** Locate or create an AutoincInfo structure associated with table pTab
000385  ** which is in database iDb.  Return the register number for the register
000386  ** that holds the maximum rowid.  Return zero if pTab is not an AUTOINCREMENT
000387  ** table.  (Also return zero when doing a VACUUM since we do not want to
000388  ** update the AUTOINCREMENT counters during a VACUUM.)
000389  **
000390  ** There is at most one AutoincInfo structure per table even if the
000391  ** same table is autoincremented multiple times due to inserts within
000392  ** triggers.  A new AutoincInfo structure is created if this is the
000393  ** first use of table pTab.  On 2nd and subsequent uses, the original
000394  ** AutoincInfo structure is used.
000395  **
000396  ** Four consecutive registers are allocated:
000397  **
000398  **   (1)  The name of the pTab table.
000399  **   (2)  The maximum ROWID of pTab.
000400  **   (3)  The rowid in sqlite_sequence of pTab
000401  **   (4)  The original value of the max ROWID in pTab, or NULL if none
000402  **
000403  ** The 2nd register is the one that is returned.  That is all the
000404  ** insert routine needs to know about.
000405  */
000406  static int autoIncBegin(
000407    Parse *pParse,      /* Parsing context */
000408    int iDb,            /* Index of the database holding pTab */
000409    Table *pTab         /* The table we are writing to */
000410  ){
000411    int memId = 0;      /* Register holding maximum rowid */
000412    assert( pParse->db->aDb[iDb].pSchema!=0 );
000413    if( (pTab->tabFlags & TF_Autoincrement)!=0
000414     && (pParse->db->mDbFlags & DBFLAG_Vacuum)==0
000415    ){
000416      Parse *pToplevel = sqlite3ParseToplevel(pParse);
000417      AutoincInfo *pInfo;
000418      Table *pSeqTab = pParse->db->aDb[iDb].pSchema->pSeqTab;
000419  
000420      /* Verify that the sqlite_sequence table exists and is an ordinary
000421      ** rowid table with exactly two columns.
000422      ** Ticket d8dc2b3a58cd5dc2918a1d4acb 2018-05-23 */
000423      if( pSeqTab==0
000424       || !HasRowid(pSeqTab)
000425       || NEVER(IsVirtual(pSeqTab))
000426       || pSeqTab->nCol!=2
000427      ){
000428        pParse->nErr++;
000429        pParse->rc = SQLITE_CORRUPT_SEQUENCE;
000430        return 0;
000431      }
000432  
000433      pInfo = pToplevel->pAinc;
000434      while( pInfo && pInfo->pTab!=pTab ){ pInfo = pInfo->pNext; }
000435      if( pInfo==0 ){
000436        pInfo = sqlite3DbMallocRawNN(pParse->db, sizeof(*pInfo));
000437        sqlite3ParserAddCleanup(pToplevel, sqlite3DbFree, pInfo);
000438        testcase( pParse->earlyCleanup );
000439        if( pParse->db->mallocFailed ) return 0;
000440        pInfo->pNext = pToplevel->pAinc;
000441        pToplevel->pAinc = pInfo;
000442        pInfo->pTab = pTab;
000443        pInfo->iDb = iDb;
000444        pToplevel->nMem++;                  /* Register to hold name of table */
000445        pInfo->regCtr = ++pToplevel->nMem;  /* Max rowid register */
000446        pToplevel->nMem +=2;       /* Rowid in sqlite_sequence + orig max val */
000447      }
000448      memId = pInfo->regCtr;
000449    }
000450    return memId;
000451  }
000452  
000453  /*
000454  ** This routine generates code that will initialize all of the
000455  ** register used by the autoincrement tracker. 
000456  */
000457  void sqlite3AutoincrementBegin(Parse *pParse){
000458    AutoincInfo *p;            /* Information about an AUTOINCREMENT */
000459    sqlite3 *db = pParse->db;  /* The database connection */
000460    Db *pDb;                   /* Database only autoinc table */
000461    int memId;                 /* Register holding max rowid */
000462    Vdbe *v = pParse->pVdbe;   /* VDBE under construction */
000463  
000464    /* This routine is never called during trigger-generation.  It is
000465    ** only called from the top-level */
000466    assert( pParse->pTriggerTab==0 );
000467    assert( sqlite3IsToplevel(pParse) );
000468  
000469    assert( v );   /* We failed long ago if this is not so */
000470    for(p = pParse->pAinc; p; p = p->pNext){
000471      static const int iLn = VDBE_OFFSET_LINENO(2);
000472      static const VdbeOpList autoInc[] = {
000473        /* 0  */ {OP_Null,    0,  0, 0},
000474        /* 1  */ {OP_Rewind,  0, 10, 0},
000475        /* 2  */ {OP_Column,  0,  0, 0},
000476        /* 3  */ {OP_Ne,      0,  9, 0},
000477        /* 4  */ {OP_Rowid,   0,  0, 0},
000478        /* 5  */ {OP_Column,  0,  1, 0},
000479        /* 6  */ {OP_AddImm,  0,  0, 0},
000480        /* 7  */ {OP_Copy,    0,  0, 0},
000481        /* 8  */ {OP_Goto,    0, 11, 0},
000482        /* 9  */ {OP_Next,    0,  2, 0},
000483        /* 10 */ {OP_Integer, 0,  0, 0},
000484        /* 11 */ {OP_Close,   0,  0, 0}
000485      };
000486      VdbeOp *aOp;
000487      pDb = &db->aDb[p->iDb];
000488      memId = p->regCtr;
000489      assert( sqlite3SchemaMutexHeld(db, 0, pDb->pSchema) );
000490      sqlite3OpenTable(pParse, 0, p->iDb, pDb->pSchema->pSeqTab, OP_OpenRead);
000491      sqlite3VdbeLoadString(v, memId-1, p->pTab->zName);
000492      aOp = sqlite3VdbeAddOpList(v, ArraySize(autoInc), autoInc, iLn);
000493      if( aOp==0 ) break;
000494      aOp[0].p2 = memId;
000495      aOp[0].p3 = memId+2;
000496      aOp[2].p3 = memId;
000497      aOp[3].p1 = memId-1;
000498      aOp[3].p3 = memId;
000499      aOp[3].p5 = SQLITE_JUMPIFNULL;
000500      aOp[4].p2 = memId+1;
000501      aOp[5].p3 = memId;
000502      aOp[6].p1 = memId;
000503      aOp[7].p2 = memId+2;
000504      aOp[7].p1 = memId;
000505      aOp[10].p2 = memId;
000506      if( pParse->nTab==0 ) pParse->nTab = 1;
000507    }
000508  }
000509  
000510  /*
000511  ** Update the maximum rowid for an autoincrement calculation.
000512  **
000513  ** This routine should be called when the regRowid register holds a
000514  ** new rowid that is about to be inserted.  If that new rowid is
000515  ** larger than the maximum rowid in the memId memory cell, then the
000516  ** memory cell is updated.
000517  */
000518  static void autoIncStep(Parse *pParse, int memId, int regRowid){
000519    if( memId>0 ){
000520      sqlite3VdbeAddOp2(pParse->pVdbe, OP_MemMax, memId, regRowid);
000521    }
000522  }
000523  
000524  /*
000525  ** This routine generates the code needed to write autoincrement
000526  ** maximum rowid values back into the sqlite_sequence register.
000527  ** Every statement that might do an INSERT into an autoincrement
000528  ** table (either directly or through triggers) needs to call this
000529  ** routine just before the "exit" code.
000530  */
000531  static SQLITE_NOINLINE void autoIncrementEnd(Parse *pParse){
000532    AutoincInfo *p;
000533    Vdbe *v = pParse->pVdbe;
000534    sqlite3 *db = pParse->db;
000535  
000536    assert( v );
000537    for(p = pParse->pAinc; p; p = p->pNext){
000538      static const int iLn = VDBE_OFFSET_LINENO(2);
000539      static const VdbeOpList autoIncEnd[] = {
000540        /* 0 */ {OP_NotNull,     0, 2, 0},
000541        /* 1 */ {OP_NewRowid,    0, 0, 0},
000542        /* 2 */ {OP_MakeRecord,  0, 2, 0},
000543        /* 3 */ {OP_Insert,      0, 0, 0},
000544        /* 4 */ {OP_Close,       0, 0, 0}
000545      };
000546      VdbeOp *aOp;
000547      Db *pDb = &db->aDb[p->iDb];
000548      int iRec;
000549      int memId = p->regCtr;
000550  
000551      iRec = sqlite3GetTempReg(pParse);
000552      assert( sqlite3SchemaMutexHeld(db, 0, pDb->pSchema) );
000553      sqlite3VdbeAddOp3(v, OP_Le, memId+2, sqlite3VdbeCurrentAddr(v)+7, memId);
000554      VdbeCoverage(v);
000555      sqlite3OpenTable(pParse, 0, p->iDb, pDb->pSchema->pSeqTab, OP_OpenWrite);
000556      aOp = sqlite3VdbeAddOpList(v, ArraySize(autoIncEnd), autoIncEnd, iLn);
000557      if( aOp==0 ) break;
000558      aOp[0].p1 = memId+1;
000559      aOp[1].p2 = memId+1;
000560      aOp[2].p1 = memId-1;
000561      aOp[2].p3 = iRec;
000562      aOp[3].p2 = iRec;
000563      aOp[3].p3 = memId+1;
000564      aOp[3].p5 = OPFLAG_APPEND;
000565      sqlite3ReleaseTempReg(pParse, iRec);
000566    }
000567  }
000568  void sqlite3AutoincrementEnd(Parse *pParse){
000569    if( pParse->pAinc ) autoIncrementEnd(pParse);
000570  }
000571  #else
000572  /*
000573  ** If SQLITE_OMIT_AUTOINCREMENT is defined, then the three routines
000574  ** above are all no-ops
000575  */
000576  # define autoIncBegin(A,B,C) (0)
000577  # define autoIncStep(A,B,C)
000578  #endif /* SQLITE_OMIT_AUTOINCREMENT */
000579  
000580  
000581  /* Forward declaration */
000582  static int xferOptimization(
000583    Parse *pParse,        /* Parser context */
000584    Table *pDest,         /* The table we are inserting into */
000585    Select *pSelect,      /* A SELECT statement to use as the data source */
000586    int onError,          /* How to handle constraint errors */
000587    int iDbDest           /* The database of pDest */
000588  );
000589  
000590  /*
000591  ** This routine is called to handle SQL of the following forms:
000592  **
000593  **    insert into TABLE (IDLIST) values(EXPRLIST),(EXPRLIST),...
000594  **    insert into TABLE (IDLIST) select
000595  **    insert into TABLE (IDLIST) default values
000596  **
000597  ** The IDLIST following the table name is always optional.  If omitted,
000598  ** then a list of all (non-hidden) columns for the table is substituted.
000599  ** The IDLIST appears in the pColumn parameter.  pColumn is NULL if IDLIST
000600  ** is omitted.
000601  **
000602  ** For the pSelect parameter holds the values to be inserted for the
000603  ** first two forms shown above.  A VALUES clause is really just short-hand
000604  ** for a SELECT statement that omits the FROM clause and everything else
000605  ** that follows.  If the pSelect parameter is NULL, that means that the
000606  ** DEFAULT VALUES form of the INSERT statement is intended.
000607  **
000608  ** The code generated follows one of four templates.  For a simple
000609  ** insert with data coming from a single-row VALUES clause, the code executes
000610  ** once straight down through.  Pseudo-code follows (we call this
000611  ** the "1st template"):
000612  **
000613  **         open write cursor to <table> and its indices
000614  **         put VALUES clause expressions into registers
000615  **         write the resulting record into <table>
000616  **         cleanup
000617  **
000618  ** The three remaining templates assume the statement is of the form
000619  **
000620  **   INSERT INTO <table> SELECT ...
000621  **
000622  ** If the SELECT clause is of the restricted form "SELECT * FROM <table2>" -
000623  ** in other words if the SELECT pulls all columns from a single table
000624  ** and there is no WHERE or LIMIT or GROUP BY or ORDER BY clauses, and
000625  ** if <table2> and <table1> are distinct tables but have identical
000626  ** schemas, including all the same indices, then a special optimization
000627  ** is invoked that copies raw records from <table2> over to <table1>.
000628  ** See the xferOptimization() function for the implementation of this
000629  ** template.  This is the 2nd template.
000630  **
000631  **         open a write cursor to <table>
000632  **         open read cursor on <table2>
000633  **         transfer all records in <table2> over to <table>
000634  **         close cursors
000635  **         foreach index on <table>
000636  **           open a write cursor on the <table> index
000637  **           open a read cursor on the corresponding <table2> index
000638  **           transfer all records from the read to the write cursors
000639  **           close cursors
000640  **         end foreach
000641  **
000642  ** The 3rd template is for when the second template does not apply
000643  ** and the SELECT clause does not read from <table> at any time.
000644  ** The generated code follows this template:
000645  **
000646  **         X <- A
000647  **         goto B
000648  **      A: setup for the SELECT
000649  **         loop over the rows in the SELECT
000650  **           load values into registers R..R+n
000651  **           yield X
000652  **         end loop
000653  **         cleanup after the SELECT
000654  **         end-coroutine X
000655  **      B: open write cursor to <table> and its indices
000656  **      C: yield X, at EOF goto D
000657  **         insert the select result into <table> from R..R+n
000658  **         goto C
000659  **      D: cleanup
000660  **
000661  ** The 4th template is used if the insert statement takes its
000662  ** values from a SELECT but the data is being inserted into a table
000663  ** that is also read as part of the SELECT.  In the third form,
000664  ** we have to use an intermediate table to store the results of
000665  ** the select.  The template is like this:
000666  **
000667  **         X <- A
000668  **         goto B
000669  **      A: setup for the SELECT
000670  **         loop over the tables in the SELECT
000671  **           load value into register R..R+n
000672  **           yield X
000673  **         end loop
000674  **         cleanup after the SELECT
000675  **         end co-routine R
000676  **      B: open temp table
000677  **      L: yield X, at EOF goto M
000678  **         insert row from R..R+n into temp table
000679  **         goto L
000680  **      M: open write cursor to <table> and its indices
000681  **         rewind temp table
000682  **      C: loop over rows of intermediate table
000683  **           transfer values form intermediate table into <table>
000684  **         end loop
000685  **      D: cleanup
000686  */
000687  void sqlite3Insert(
000688    Parse *pParse,        /* Parser context */
000689    SrcList *pTabList,    /* Name of table into which we are inserting */
000690    Select *pSelect,      /* A SELECT statement to use as the data source */
000691    IdList *pColumn,      /* Column names corresponding to IDLIST, or NULL. */
000692    int onError,          /* How to handle constraint errors */
000693    Upsert *pUpsert       /* ON CONFLICT clauses for upsert, or NULL */
000694  ){
000695    sqlite3 *db;          /* The main database structure */
000696    Table *pTab;          /* The table to insert into.  aka TABLE */
000697    int i, j;             /* Loop counters */
000698    Vdbe *v;              /* Generate code into this virtual machine */
000699    Index *pIdx;          /* For looping over indices of the table */
000700    int nColumn;          /* Number of columns in the data */
000701    int nHidden = 0;      /* Number of hidden columns if TABLE is virtual */
000702    int iDataCur = 0;     /* VDBE cursor that is the main data repository */
000703    int iIdxCur = 0;      /* First index cursor */
000704    int ipkColumn = -1;   /* Column that is the INTEGER PRIMARY KEY */
000705    int endOfLoop;        /* Label for the end of the insertion loop */
000706    int srcTab = 0;       /* Data comes from this temporary cursor if >=0 */
000707    int addrInsTop = 0;   /* Jump to label "D" */
000708    int addrCont = 0;     /* Top of insert loop. Label "C" in templates 3 and 4 */
000709    SelectDest dest;      /* Destination for SELECT on rhs of INSERT */
000710    int iDb;              /* Index of database holding TABLE */
000711    u8 useTempTable = 0;  /* Store SELECT results in intermediate table */
000712    u8 appendFlag = 0;    /* True if the insert is likely to be an append */
000713    u8 withoutRowid;      /* 0 for normal table.  1 for WITHOUT ROWID table */
000714    u8 bIdListInOrder;    /* True if IDLIST is in table order */
000715    ExprList *pList = 0;  /* List of VALUES() to be inserted  */
000716    int iRegStore;        /* Register in which to store next column */
000717  
000718    /* Register allocations */
000719    int regFromSelect = 0;/* Base register for data coming from SELECT */
000720    int regAutoinc = 0;   /* Register holding the AUTOINCREMENT counter */
000721    int regRowCount = 0;  /* Memory cell used for the row counter */
000722    int regIns;           /* Block of regs holding rowid+data being inserted */
000723    int regRowid;         /* registers holding insert rowid */
000724    int regData;          /* register holding first column to insert */
000725    int *aRegIdx = 0;     /* One register allocated to each index */
000726  
000727  #ifndef SQLITE_OMIT_TRIGGER
000728    int isView;                 /* True if attempting to insert into a view */
000729    Trigger *pTrigger;          /* List of triggers on pTab, if required */
000730    int tmask;                  /* Mask of trigger times */
000731  #endif
000732  
000733    db = pParse->db;
000734    assert( db->pParse==pParse );
000735    if( pParse->nErr ){
000736      goto insert_cleanup;
000737    }
000738    assert( db->mallocFailed==0 );
000739    dest.iSDParm = 0;  /* Suppress a harmless compiler warning */
000740  
000741    /* If the Select object is really just a simple VALUES() list with a
000742    ** single row (the common case) then keep that one row of values
000743    ** and discard the other (unused) parts of the pSelect object
000744    */
000745    if( pSelect && (pSelect->selFlags & SF_Values)!=0 && pSelect->pPrior==0 ){
000746      pList = pSelect->pEList;
000747      pSelect->pEList = 0;
000748      sqlite3SelectDelete(db, pSelect);
000749      pSelect = 0;
000750    }
000751  
000752    /* Locate the table into which we will be inserting new information.
000753    */
000754    assert( pTabList->nSrc==1 );
000755    pTab = sqlite3SrcListLookup(pParse, pTabList);
000756    if( pTab==0 ){
000757      goto insert_cleanup;
000758    }
000759    iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
000760    assert( iDb<db->nDb );
000761    if( sqlite3AuthCheck(pParse, SQLITE_INSERT, pTab->zName, 0,
000762                         db->aDb[iDb].zDbSName) ){
000763      goto insert_cleanup;
000764    }
000765    withoutRowid = !HasRowid(pTab);
000766  
000767    /* Figure out if we have any triggers and if the table being
000768    ** inserted into is a view
000769    */
000770  #ifndef SQLITE_OMIT_TRIGGER
000771    pTrigger = sqlite3TriggersExist(pParse, pTab, TK_INSERT, 0, &tmask);
000772    isView = IsView(pTab);
000773  #else
000774  # define pTrigger 0
000775  # define tmask 0
000776  # define isView 0
000777  #endif
000778  #ifdef SQLITE_OMIT_VIEW
000779  # undef isView
000780  # define isView 0
000781  #endif
000782    assert( (pTrigger && tmask) || (pTrigger==0 && tmask==0) );
000783  
000784  #if TREETRACE_ENABLED
000785    if( sqlite3TreeTrace & 0x10000 ){
000786      sqlite3TreeViewLine(0, "In sqlite3Insert() at %s:%d", __FILE__, __LINE__);
000787      sqlite3TreeViewInsert(pParse->pWith, pTabList, pColumn, pSelect, pList,
000788                            onError, pUpsert, pTrigger);
000789    }
000790  #endif
000791  
000792    /* If pTab is really a view, make sure it has been initialized.
000793    ** ViewGetColumnNames() is a no-op if pTab is not a view.
000794    */
000795    if( sqlite3ViewGetColumnNames(pParse, pTab) ){
000796      goto insert_cleanup;
000797    }
000798  
000799    /* Cannot insert into a read-only table.
000800    */
000801    if( sqlite3IsReadOnly(pParse, pTab, pTrigger) ){
000802      goto insert_cleanup;
000803    }
000804  
000805    /* Allocate a VDBE
000806    */
000807    v = sqlite3GetVdbe(pParse);
000808    if( v==0 ) goto insert_cleanup;
000809    if( pParse->nested==0 ) sqlite3VdbeCountChanges(v);
000810    sqlite3BeginWriteOperation(pParse, pSelect || pTrigger, iDb);
000811  
000812  #ifndef SQLITE_OMIT_XFER_OPT
000813    /* If the statement is of the form
000814    **
000815    **       INSERT INTO <table1> SELECT * FROM <table2>;
000816    **
000817    ** Then special optimizations can be applied that make the transfer
000818    ** very fast and which reduce fragmentation of indices.
000819    **
000820    ** This is the 2nd template.
000821    */
000822    if( pColumn==0
000823     && pSelect!=0
000824     && pTrigger==0
000825     && xferOptimization(pParse, pTab, pSelect, onError, iDb)
000826    ){
000827      assert( !pTrigger );
000828      assert( pList==0 );
000829      goto insert_end;
000830    }
000831  #endif /* SQLITE_OMIT_XFER_OPT */
000832  
000833    /* If this is an AUTOINCREMENT table, look up the sequence number in the
000834    ** sqlite_sequence table and store it in memory cell regAutoinc.
000835    */
000836    regAutoinc = autoIncBegin(pParse, iDb, pTab);
000837  
000838    /* Allocate a block registers to hold the rowid and the values
000839    ** for all columns of the new row.
000840    */
000841    regRowid = regIns = pParse->nMem+1;
000842    pParse->nMem += pTab->nCol + 1;
000843    if( IsVirtual(pTab) ){
000844      regRowid++;
000845      pParse->nMem++;
000846    }
000847    regData = regRowid+1;
000848  
000849    /* If the INSERT statement included an IDLIST term, then make sure
000850    ** all elements of the IDLIST really are columns of the table and
000851    ** remember the column indices.
000852    **
000853    ** If the table has an INTEGER PRIMARY KEY column and that column
000854    ** is named in the IDLIST, then record in the ipkColumn variable
000855    ** the index into IDLIST of the primary key column.  ipkColumn is
000856    ** the index of the primary key as it appears in IDLIST, not as
000857    ** is appears in the original table.  (The index of the INTEGER
000858    ** PRIMARY KEY in the original table is pTab->iPKey.)  After this
000859    ** loop, if ipkColumn==(-1), that means that integer primary key
000860    ** is unspecified, and hence the table is either WITHOUT ROWID or
000861    ** it will automatically generated an integer primary key.
000862    **
000863    ** bIdListInOrder is true if the columns in IDLIST are in storage
000864    ** order.  This enables an optimization that avoids shuffling the
000865    ** columns into storage order.  False negatives are harmless,
000866    ** but false positives will cause database corruption.
000867    */
000868    bIdListInOrder = (pTab->tabFlags & (TF_OOOHidden|TF_HasStored))==0;
000869    if( pColumn ){
000870      assert( pColumn->eU4!=EU4_EXPR );
000871      pColumn->eU4 = EU4_IDX;
000872      for(i=0; i<pColumn->nId; i++){
000873        pColumn->a[i].u4.idx = -1;
000874      }
000875      for(i=0; i<pColumn->nId; i++){
000876        for(j=0; j<pTab->nCol; j++){
000877          if( sqlite3StrICmp(pColumn->a[i].zName, pTab->aCol[j].zCnName)==0 ){
000878            pColumn->a[i].u4.idx = j;
000879            if( i!=j ) bIdListInOrder = 0;
000880            if( j==pTab->iPKey ){
000881              ipkColumn = i;  assert( !withoutRowid );
000882            }
000883  #ifndef SQLITE_OMIT_GENERATED_COLUMNS
000884            if( pTab->aCol[j].colFlags & (COLFLAG_STORED|COLFLAG_VIRTUAL) ){
000885              sqlite3ErrorMsg(pParse,
000886                 "cannot INSERT into generated column \"%s\"",
000887                 pTab->aCol[j].zCnName);
000888              goto insert_cleanup;
000889            }
000890  #endif
000891            break;
000892          }
000893        }
000894        if( j>=pTab->nCol ){
000895          if( sqlite3IsRowid(pColumn->a[i].zName) && !withoutRowid ){
000896            ipkColumn = i;
000897            bIdListInOrder = 0;
000898          }else{
000899            sqlite3ErrorMsg(pParse, "table %S has no column named %s",
000900                pTabList->a, pColumn->a[i].zName);
000901            pParse->checkSchema = 1;
000902            goto insert_cleanup;
000903          }
000904        }
000905      }
000906    }
000907  
000908    /* Figure out how many columns of data are supplied.  If the data
000909    ** is coming from a SELECT statement, then generate a co-routine that
000910    ** produces a single row of the SELECT on each invocation.  The
000911    ** co-routine is the common header to the 3rd and 4th templates.
000912    */
000913    if( pSelect ){
000914      /* Data is coming from a SELECT or from a multi-row VALUES clause.
000915      ** Generate a co-routine to run the SELECT. */
000916      int regYield;       /* Register holding co-routine entry-point */
000917      int addrTop;        /* Top of the co-routine */
000918      int rc;             /* Result code */
000919  
000920      regYield = ++pParse->nMem;
000921      addrTop = sqlite3VdbeCurrentAddr(v) + 1;
000922      sqlite3VdbeAddOp3(v, OP_InitCoroutine, regYield, 0, addrTop);
000923      sqlite3SelectDestInit(&dest, SRT_Coroutine, regYield);
000924      dest.iSdst = bIdListInOrder ? regData : 0;
000925      dest.nSdst = pTab->nCol;
000926      rc = sqlite3Select(pParse, pSelect, &dest);
000927      regFromSelect = dest.iSdst;
000928      assert( db->pParse==pParse );
000929      if( rc || pParse->nErr ) goto insert_cleanup;
000930      assert( db->mallocFailed==0 );
000931      sqlite3VdbeEndCoroutine(v, regYield);
000932      sqlite3VdbeJumpHere(v, addrTop - 1);                       /* label B: */
000933      assert( pSelect->pEList );
000934      nColumn = pSelect->pEList->nExpr;
000935  
000936      /* Set useTempTable to TRUE if the result of the SELECT statement
000937      ** should be written into a temporary table (template 4).  Set to
000938      ** FALSE if each output row of the SELECT can be written directly into
000939      ** the destination table (template 3).
000940      **
000941      ** A temp table must be used if the table being updated is also one
000942      ** of the tables being read by the SELECT statement.  Also use a
000943      ** temp table in the case of row triggers.
000944      */
000945      if( pTrigger || readsTable(pParse, iDb, pTab) ){
000946        useTempTable = 1;
000947      }
000948  
000949      if( useTempTable ){
000950        /* Invoke the coroutine to extract information from the SELECT
000951        ** and add it to a transient table srcTab.  The code generated
000952        ** here is from the 4th template:
000953        **
000954        **      B: open temp table
000955        **      L: yield X, goto M at EOF
000956        **         insert row from R..R+n into temp table
000957        **         goto L
000958        **      M: ...
000959        */
000960        int regRec;          /* Register to hold packed record */
000961        int regTempRowid;    /* Register to hold temp table ROWID */
000962        int addrL;           /* Label "L" */
000963  
000964        srcTab = pParse->nTab++;
000965        regRec = sqlite3GetTempReg(pParse);
000966        regTempRowid = sqlite3GetTempReg(pParse);
000967        sqlite3VdbeAddOp2(v, OP_OpenEphemeral, srcTab, nColumn);
000968        addrL = sqlite3VdbeAddOp1(v, OP_Yield, dest.iSDParm); VdbeCoverage(v);
000969        sqlite3VdbeAddOp3(v, OP_MakeRecord, regFromSelect, nColumn, regRec);
000970        sqlite3VdbeAddOp2(v, OP_NewRowid, srcTab, regTempRowid);
000971        sqlite3VdbeAddOp3(v, OP_Insert, srcTab, regRec, regTempRowid);
000972        sqlite3VdbeGoto(v, addrL);
000973        sqlite3VdbeJumpHere(v, addrL);
000974        sqlite3ReleaseTempReg(pParse, regRec);
000975        sqlite3ReleaseTempReg(pParse, regTempRowid);
000976      }
000977    }else{
000978      /* This is the case if the data for the INSERT is coming from a
000979      ** single-row VALUES clause
000980      */
000981      NameContext sNC;
000982      memset(&sNC, 0, sizeof(sNC));
000983      sNC.pParse = pParse;
000984      srcTab = -1;
000985      assert( useTempTable==0 );
000986      if( pList ){
000987        nColumn = pList->nExpr;
000988        if( sqlite3ResolveExprListNames(&sNC, pList) ){
000989          goto insert_cleanup;
000990        }
000991      }else{
000992        nColumn = 0;
000993      }
000994    }
000995  
000996    /* If there is no IDLIST term but the table has an integer primary
000997    ** key, the set the ipkColumn variable to the integer primary key
000998    ** column index in the original table definition.
000999    */
001000    if( pColumn==0 && nColumn>0 ){
001001      ipkColumn = pTab->iPKey;
001002  #ifndef SQLITE_OMIT_GENERATED_COLUMNS
001003      if( ipkColumn>=0 && (pTab->tabFlags & TF_HasGenerated)!=0 ){
001004        testcase( pTab->tabFlags & TF_HasVirtual );
001005        testcase( pTab->tabFlags & TF_HasStored );
001006        for(i=ipkColumn-1; i>=0; i--){
001007          if( pTab->aCol[i].colFlags & COLFLAG_GENERATED ){
001008            testcase( pTab->aCol[i].colFlags & COLFLAG_VIRTUAL );
001009            testcase( pTab->aCol[i].colFlags & COLFLAG_STORED );
001010            ipkColumn--;
001011          }
001012        }
001013      }
001014  #endif
001015  
001016      /* Make sure the number of columns in the source data matches the number
001017      ** of columns to be inserted into the table.
001018      */
001019      assert( TF_HasHidden==COLFLAG_HIDDEN );
001020      assert( TF_HasGenerated==COLFLAG_GENERATED );
001021      assert( COLFLAG_NOINSERT==(COLFLAG_GENERATED|COLFLAG_HIDDEN) );
001022      if( (pTab->tabFlags & (TF_HasGenerated|TF_HasHidden))!=0 ){
001023        for(i=0; i<pTab->nCol; i++){
001024          if( pTab->aCol[i].colFlags & COLFLAG_NOINSERT ) nHidden++;
001025        }
001026      }
001027      if( nColumn!=(pTab->nCol-nHidden) ){
001028        sqlite3ErrorMsg(pParse,
001029           "table %S has %d columns but %d values were supplied",
001030           pTabList->a, pTab->nCol-nHidden, nColumn);
001031       goto insert_cleanup;
001032      }
001033    }
001034    if( pColumn!=0 && nColumn!=pColumn->nId ){
001035      sqlite3ErrorMsg(pParse, "%d values for %d columns", nColumn, pColumn->nId);
001036      goto insert_cleanup;
001037    }
001038     
001039    /* Initialize the count of rows to be inserted
001040    */
001041    if( (db->flags & SQLITE_CountRows)!=0
001042     && !pParse->nested
001043     && !pParse->pTriggerTab
001044     && !pParse->bReturning
001045    ){
001046      regRowCount = ++pParse->nMem;
001047      sqlite3VdbeAddOp2(v, OP_Integer, 0, regRowCount);
001048    }
001049  
001050    /* If this is not a view, open the table and and all indices */
001051    if( !isView ){
001052      int nIdx;
001053      nIdx = sqlite3OpenTableAndIndices(pParse, pTab, OP_OpenWrite, 0, -1, 0,
001054                                        &iDataCur, &iIdxCur);
001055      aRegIdx = sqlite3DbMallocRawNN(db, sizeof(int)*(nIdx+2));
001056      if( aRegIdx==0 ){
001057        goto insert_cleanup;
001058      }
001059      for(i=0, pIdx=pTab->pIndex; i<nIdx; pIdx=pIdx->pNext, i++){
001060        assert( pIdx );
001061        aRegIdx[i] = ++pParse->nMem;
001062        pParse->nMem += pIdx->nColumn;
001063      }
001064      aRegIdx[i] = ++pParse->nMem;  /* Register to store the table record */
001065    }
001066  #ifndef SQLITE_OMIT_UPSERT
001067    if( pUpsert ){
001068      Upsert *pNx;
001069      if( IsVirtual(pTab) ){
001070        sqlite3ErrorMsg(pParse, "UPSERT not implemented for virtual table \"%s\"",
001071                pTab->zName);
001072        goto insert_cleanup;
001073      }
001074      if( IsView(pTab) ){
001075        sqlite3ErrorMsg(pParse, "cannot UPSERT a view");
001076        goto insert_cleanup;
001077      }
001078      if( sqlite3HasExplicitNulls(pParse, pUpsert->pUpsertTarget) ){
001079        goto insert_cleanup;
001080      }
001081      pTabList->a[0].iCursor = iDataCur;
001082      pNx = pUpsert;
001083      do{
001084        pNx->pUpsertSrc = pTabList;
001085        pNx->regData = regData;
001086        pNx->iDataCur = iDataCur;
001087        pNx->iIdxCur = iIdxCur;
001088        if( pNx->pUpsertTarget ){
001089          if( sqlite3UpsertAnalyzeTarget(pParse, pTabList, pNx, pUpsert) ){
001090            goto insert_cleanup;
001091          }
001092        }
001093        pNx = pNx->pNextUpsert;
001094      }while( pNx!=0 );
001095    }
001096  #endif
001097  
001098  
001099    /* This is the top of the main insertion loop */
001100    if( useTempTable ){
001101      /* This block codes the top of loop only.  The complete loop is the
001102      ** following pseudocode (template 4):
001103      **
001104      **         rewind temp table, if empty goto D
001105      **      C: loop over rows of intermediate table
001106      **           transfer values form intermediate table into <table>
001107      **         end loop
001108      **      D: ...
001109      */
001110      addrInsTop = sqlite3VdbeAddOp1(v, OP_Rewind, srcTab); VdbeCoverage(v);
001111      addrCont = sqlite3VdbeCurrentAddr(v);
001112    }else if( pSelect ){
001113      /* This block codes the top of loop only.  The complete loop is the
001114      ** following pseudocode (template 3):
001115      **
001116      **      C: yield X, at EOF goto D
001117      **         insert the select result into <table> from R..R+n
001118      **         goto C
001119      **      D: ...
001120      */
001121      sqlite3VdbeReleaseRegisters(pParse, regData, pTab->nCol, 0, 0);
001122      addrInsTop = addrCont = sqlite3VdbeAddOp1(v, OP_Yield, dest.iSDParm);
001123      VdbeCoverage(v);
001124      if( ipkColumn>=0 ){
001125        /* tag-20191021-001: If the INTEGER PRIMARY KEY is being generated by the
001126        ** SELECT, go ahead and copy the value into the rowid slot now, so that
001127        ** the value does not get overwritten by a NULL at tag-20191021-002. */
001128        sqlite3VdbeAddOp2(v, OP_Copy, regFromSelect+ipkColumn, regRowid);
001129      }
001130    }
001131  
001132    /* Compute data for ordinary columns of the new entry.  Values
001133    ** are written in storage order into registers starting with regData.
001134    ** Only ordinary columns are computed in this loop. The rowid
001135    ** (if there is one) is computed later and generated columns are
001136    ** computed after the rowid since they might depend on the value
001137    ** of the rowid.
001138    */
001139    nHidden = 0;
001140    iRegStore = regData;  assert( regData==regRowid+1 );
001141    for(i=0; i<pTab->nCol; i++, iRegStore++){
001142      int k;
001143      u32 colFlags;
001144      assert( i>=nHidden );
001145      if( i==pTab->iPKey ){
001146        /* tag-20191021-002: References to the INTEGER PRIMARY KEY are filled
001147        ** using the rowid. So put a NULL in the IPK slot of the record to avoid
001148        ** using excess space.  The file format definition requires this extra
001149        ** NULL - we cannot optimize further by skipping the column completely */
001150        sqlite3VdbeAddOp1(v, OP_SoftNull, iRegStore);
001151        continue;
001152      }
001153      if( ((colFlags = pTab->aCol[i].colFlags) & COLFLAG_NOINSERT)!=0 ){
001154        nHidden++;
001155        if( (colFlags & COLFLAG_VIRTUAL)!=0 ){
001156          /* Virtual columns do not participate in OP_MakeRecord.  So back up
001157          ** iRegStore by one slot to compensate for the iRegStore++ in the
001158          ** outer for() loop */
001159          iRegStore--;
001160          continue;
001161        }else if( (colFlags & COLFLAG_STORED)!=0 ){
001162          /* Stored columns are computed later.  But if there are BEFORE
001163          ** triggers, the slots used for stored columns will be OP_Copy-ed
001164          ** to a second block of registers, so the register needs to be
001165          ** initialized to NULL to avoid an uninitialized register read */
001166          if( tmask & TRIGGER_BEFORE ){
001167            sqlite3VdbeAddOp1(v, OP_SoftNull, iRegStore);
001168          }
001169          continue;
001170        }else if( pColumn==0 ){
001171          /* Hidden columns that are not explicitly named in the INSERT
001172          ** get there default value */
001173          sqlite3ExprCodeFactorable(pParse,
001174              sqlite3ColumnExpr(pTab, &pTab->aCol[i]),
001175              iRegStore);
001176          continue;
001177        }
001178      }
001179      if( pColumn ){
001180        assert( pColumn->eU4==EU4_IDX );
001181        for(j=0; j<pColumn->nId && pColumn->a[j].u4.idx!=i; j++){}
001182        if( j>=pColumn->nId ){
001183          /* A column not named in the insert column list gets its
001184          ** default value */
001185          sqlite3ExprCodeFactorable(pParse,
001186              sqlite3ColumnExpr(pTab, &pTab->aCol[i]),
001187              iRegStore);
001188          continue;
001189        }
001190        k = j;
001191      }else if( nColumn==0 ){
001192        /* This is INSERT INTO ... DEFAULT VALUES.  Load the default value. */
001193        sqlite3ExprCodeFactorable(pParse,
001194            sqlite3ColumnExpr(pTab, &pTab->aCol[i]),
001195            iRegStore);
001196        continue;
001197      }else{
001198        k = i - nHidden;
001199      }
001200  
001201      if( useTempTable ){
001202        sqlite3VdbeAddOp3(v, OP_Column, srcTab, k, iRegStore);
001203      }else if( pSelect ){
001204        if( regFromSelect!=regData ){
001205          sqlite3VdbeAddOp2(v, OP_SCopy, regFromSelect+k, iRegStore);
001206        }
001207      }else{
001208        Expr *pX = pList->a[k].pExpr;
001209        int y = sqlite3ExprCodeTarget(pParse, pX, iRegStore);
001210        if( y!=iRegStore ){
001211          sqlite3VdbeAddOp2(v,
001212            ExprHasProperty(pX, EP_Subquery) ? OP_Copy : OP_SCopy, y, iRegStore);
001213        }
001214      }
001215    }
001216  
001217  
001218    /* Run the BEFORE and INSTEAD OF triggers, if there are any
001219    */
001220    endOfLoop = sqlite3VdbeMakeLabel(pParse);
001221    if( tmask & TRIGGER_BEFORE ){
001222      int regCols = sqlite3GetTempRange(pParse, pTab->nCol+1);
001223  
001224      /* build the NEW.* reference row.  Note that if there is an INTEGER
001225      ** PRIMARY KEY into which a NULL is being inserted, that NULL will be
001226      ** translated into a unique ID for the row.  But on a BEFORE trigger,
001227      ** we do not know what the unique ID will be (because the insert has
001228      ** not happened yet) so we substitute a rowid of -1
001229      */
001230      if( ipkColumn<0 ){
001231        sqlite3VdbeAddOp2(v, OP_Integer, -1, regCols);
001232      }else{
001233        int addr1;
001234        assert( !withoutRowid );
001235        if( useTempTable ){
001236          sqlite3VdbeAddOp3(v, OP_Column, srcTab, ipkColumn, regCols);
001237        }else{
001238          assert( pSelect==0 );  /* Otherwise useTempTable is true */
001239          sqlite3ExprCode(pParse, pList->a[ipkColumn].pExpr, regCols);
001240        }
001241        addr1 = sqlite3VdbeAddOp1(v, OP_NotNull, regCols); VdbeCoverage(v);
001242        sqlite3VdbeAddOp2(v, OP_Integer, -1, regCols);
001243        sqlite3VdbeJumpHere(v, addr1);
001244        sqlite3VdbeAddOp1(v, OP_MustBeInt, regCols); VdbeCoverage(v);
001245      }
001246  
001247      /* Copy the new data already generated. */
001248      assert( pTab->nNVCol>0 || pParse->nErr>0 );
001249      sqlite3VdbeAddOp3(v, OP_Copy, regRowid+1, regCols+1, pTab->nNVCol-1);
001250  
001251  #ifndef SQLITE_OMIT_GENERATED_COLUMNS
001252      /* Compute the new value for generated columns after all other
001253      ** columns have already been computed.  This must be done after
001254      ** computing the ROWID in case one of the generated columns
001255      ** refers to the ROWID. */
001256      if( pTab->tabFlags & TF_HasGenerated ){
001257        testcase( pTab->tabFlags & TF_HasVirtual );
001258        testcase( pTab->tabFlags & TF_HasStored );
001259        sqlite3ComputeGeneratedColumns(pParse, regCols+1, pTab);
001260      }
001261  #endif
001262  
001263      /* If this is an INSERT on a view with an INSTEAD OF INSERT trigger,
001264      ** do not attempt any conversions before assembling the record.
001265      ** If this is a real table, attempt conversions as required by the
001266      ** table column affinities.
001267      */
001268      if( !isView ){
001269        sqlite3TableAffinity(v, pTab, regCols+1);
001270      }
001271  
001272      /* Fire BEFORE or INSTEAD OF triggers */
001273      sqlite3CodeRowTrigger(pParse, pTrigger, TK_INSERT, 0, TRIGGER_BEFORE,
001274          pTab, regCols-pTab->nCol-1, onError, endOfLoop);
001275  
001276      sqlite3ReleaseTempRange(pParse, regCols, pTab->nCol+1);
001277    }
001278  
001279    if( !isView ){
001280      if( IsVirtual(pTab) ){
001281        /* The row that the VUpdate opcode will delete: none */
001282        sqlite3VdbeAddOp2(v, OP_Null, 0, regIns);
001283      }
001284      if( ipkColumn>=0 ){
001285        /* Compute the new rowid */
001286        if( useTempTable ){
001287          sqlite3VdbeAddOp3(v, OP_Column, srcTab, ipkColumn, regRowid);
001288        }else if( pSelect ){
001289          /* Rowid already initialized at tag-20191021-001 */
001290        }else{
001291          Expr *pIpk = pList->a[ipkColumn].pExpr;
001292          if( pIpk->op==TK_NULL && !IsVirtual(pTab) ){
001293            sqlite3VdbeAddOp3(v, OP_NewRowid, iDataCur, regRowid, regAutoinc);
001294            appendFlag = 1;
001295          }else{
001296            sqlite3ExprCode(pParse, pList->a[ipkColumn].pExpr, regRowid);
001297          }
001298        }
001299        /* If the PRIMARY KEY expression is NULL, then use OP_NewRowid
001300        ** to generate a unique primary key value.
001301        */
001302        if( !appendFlag ){
001303          int addr1;
001304          if( !IsVirtual(pTab) ){
001305            addr1 = sqlite3VdbeAddOp1(v, OP_NotNull, regRowid); VdbeCoverage(v);
001306            sqlite3VdbeAddOp3(v, OP_NewRowid, iDataCur, regRowid, regAutoinc);
001307            sqlite3VdbeJumpHere(v, addr1);
001308          }else{
001309            addr1 = sqlite3VdbeCurrentAddr(v);
001310            sqlite3VdbeAddOp2(v, OP_IsNull, regRowid, addr1+2); VdbeCoverage(v);
001311          }
001312          sqlite3VdbeAddOp1(v, OP_MustBeInt, regRowid); VdbeCoverage(v);
001313        }
001314      }else if( IsVirtual(pTab) || withoutRowid ){
001315        sqlite3VdbeAddOp2(v, OP_Null, 0, regRowid);
001316      }else{
001317        sqlite3VdbeAddOp3(v, OP_NewRowid, iDataCur, regRowid, regAutoinc);
001318        appendFlag = 1;
001319      }
001320      autoIncStep(pParse, regAutoinc, regRowid);
001321  
001322  #ifndef SQLITE_OMIT_GENERATED_COLUMNS
001323      /* Compute the new value for generated columns after all other
001324      ** columns have already been computed.  This must be done after
001325      ** computing the ROWID in case one of the generated columns
001326      ** is derived from the INTEGER PRIMARY KEY. */
001327      if( pTab->tabFlags & TF_HasGenerated ){
001328        sqlite3ComputeGeneratedColumns(pParse, regRowid+1, pTab);
001329      }
001330  #endif
001331  
001332      /* Generate code to check constraints and generate index keys and
001333      ** do the insertion.
001334      */
001335  #ifndef SQLITE_OMIT_VIRTUALTABLE
001336      if( IsVirtual(pTab) ){
001337        const char *pVTab = (const char *)sqlite3GetVTable(db, pTab);
001338        sqlite3VtabMakeWritable(pParse, pTab);
001339        sqlite3VdbeAddOp4(v, OP_VUpdate, 1, pTab->nCol+2, regIns, pVTab, P4_VTAB);
001340        sqlite3VdbeChangeP5(v, onError==OE_Default ? OE_Abort : onError);
001341        sqlite3MayAbort(pParse);
001342      }else
001343  #endif
001344      {
001345        int isReplace = 0;/* Set to true if constraints may cause a replace */
001346        int bUseSeek;     /* True to use OPFLAG_SEEKRESULT */
001347        sqlite3GenerateConstraintChecks(pParse, pTab, aRegIdx, iDataCur, iIdxCur,
001348            regIns, 0, ipkColumn>=0, onError, endOfLoop, &isReplace, 0, pUpsert
001349        );
001350        if( db->flags & SQLITE_ForeignKeys ){
001351          sqlite3FkCheck(pParse, pTab, 0, regIns, 0, 0);
001352        }
001353  
001354        /* Set the OPFLAG_USESEEKRESULT flag if either (a) there are no REPLACE
001355        ** constraints or (b) there are no triggers and this table is not a
001356        ** parent table in a foreign key constraint. It is safe to set the
001357        ** flag in the second case as if any REPLACE constraint is hit, an
001358        ** OP_Delete or OP_IdxDelete instruction will be executed on each
001359        ** cursor that is disturbed. And these instructions both clear the
001360        ** VdbeCursor.seekResult variable, disabling the OPFLAG_USESEEKRESULT
001361        ** functionality.  */
001362        bUseSeek = (isReplace==0 || !sqlite3VdbeHasSubProgram(v));
001363        sqlite3CompleteInsertion(pParse, pTab, iDataCur, iIdxCur,
001364            regIns, aRegIdx, 0, appendFlag, bUseSeek
001365        );
001366      }
001367  #ifdef SQLITE_ALLOW_ROWID_IN_VIEW
001368    }else if( pParse->bReturning ){
001369      /* If there is a RETURNING clause, populate the rowid register with
001370      ** constant value -1, in case one or more of the returned expressions
001371      ** refer to the "rowid" of the view.  */
001372      sqlite3VdbeAddOp2(v, OP_Integer, -1, regRowid);
001373  #endif
001374    }
001375  
001376    /* Update the count of rows that are inserted
001377    */
001378    if( regRowCount ){
001379      sqlite3VdbeAddOp2(v, OP_AddImm, regRowCount, 1);
001380    }
001381  
001382    if( pTrigger ){
001383      /* Code AFTER triggers */
001384      sqlite3CodeRowTrigger(pParse, pTrigger, TK_INSERT, 0, TRIGGER_AFTER,
001385          pTab, regData-2-pTab->nCol, onError, endOfLoop);
001386    }
001387  
001388    /* The bottom of the main insertion loop, if the data source
001389    ** is a SELECT statement.
001390    */
001391    sqlite3VdbeResolveLabel(v, endOfLoop);
001392    if( useTempTable ){
001393      sqlite3VdbeAddOp2(v, OP_Next, srcTab, addrCont); VdbeCoverage(v);
001394      sqlite3VdbeJumpHere(v, addrInsTop);
001395      sqlite3VdbeAddOp1(v, OP_Close, srcTab);
001396    }else if( pSelect ){
001397      sqlite3VdbeGoto(v, addrCont);
001398  #ifdef SQLITE_DEBUG
001399      /* If we are jumping back to an OP_Yield that is preceded by an
001400      ** OP_ReleaseReg, set the p5 flag on the OP_Goto so that the
001401      ** OP_ReleaseReg will be included in the loop. */
001402      if( sqlite3VdbeGetOp(v, addrCont-1)->opcode==OP_ReleaseReg ){
001403        assert( sqlite3VdbeGetOp(v, addrCont)->opcode==OP_Yield );
001404        sqlite3VdbeChangeP5(v, 1);
001405      }
001406  #endif
001407      sqlite3VdbeJumpHere(v, addrInsTop);
001408    }
001409  
001410  #ifndef SQLITE_OMIT_XFER_OPT
001411  insert_end:
001412  #endif /* SQLITE_OMIT_XFER_OPT */
001413    /* Update the sqlite_sequence table by storing the content of the
001414    ** maximum rowid counter values recorded while inserting into
001415    ** autoincrement tables.
001416    */
001417    if( pParse->nested==0 && pParse->pTriggerTab==0 ){
001418      sqlite3AutoincrementEnd(pParse);
001419    }
001420  
001421    /*
001422    ** Return the number of rows inserted. If this routine is
001423    ** generating code because of a call to sqlite3NestedParse(), do not
001424    ** invoke the callback function.
001425    */
001426    if( regRowCount ){
001427      sqlite3CodeChangeCount(v, regRowCount, "rows inserted");
001428    }
001429  
001430  insert_cleanup:
001431    sqlite3SrcListDelete(db, pTabList);
001432    sqlite3ExprListDelete(db, pList);
001433    sqlite3UpsertDelete(db, pUpsert);
001434    sqlite3SelectDelete(db, pSelect);
001435    sqlite3IdListDelete(db, pColumn);
001436    if( aRegIdx ) sqlite3DbNNFreeNN(db, aRegIdx);
001437  }
001438  
001439  /* Make sure "isView" and other macros defined above are undefined. Otherwise
001440  ** they may interfere with compilation of other functions in this file
001441  ** (or in another file, if this file becomes part of the amalgamation).  */
001442  #ifdef isView
001443   #undef isView
001444  #endif
001445  #ifdef pTrigger
001446   #undef pTrigger
001447  #endif
001448  #ifdef tmask
001449   #undef tmask
001450  #endif
001451  
001452  /*
001453  ** Meanings of bits in of pWalker->eCode for
001454  ** sqlite3ExprReferencesUpdatedColumn()
001455  */
001456  #define CKCNSTRNT_COLUMN   0x01    /* CHECK constraint uses a changing column */
001457  #define CKCNSTRNT_ROWID    0x02    /* CHECK constraint references the ROWID */
001458  
001459  /* This is the Walker callback from sqlite3ExprReferencesUpdatedColumn().
001460  *  Set bit 0x01 of pWalker->eCode if pWalker->eCode to 0 and if this
001461  ** expression node references any of the
001462  ** columns that are being modified by an UPDATE statement.
001463  */
001464  static int checkConstraintExprNode(Walker *pWalker, Expr *pExpr){
001465    if( pExpr->op==TK_COLUMN ){
001466      assert( pExpr->iColumn>=0 || pExpr->iColumn==-1 );
001467      if( pExpr->iColumn>=0 ){
001468        if( pWalker->u.aiCol[pExpr->iColumn]>=0 ){
001469          pWalker->eCode |= CKCNSTRNT_COLUMN;
001470        }
001471      }else{
001472        pWalker->eCode |= CKCNSTRNT_ROWID;
001473      }
001474    }
001475    return WRC_Continue;
001476  }
001477  
001478  /*
001479  ** pExpr is a CHECK constraint on a row that is being UPDATE-ed.  The
001480  ** only columns that are modified by the UPDATE are those for which
001481  ** aiChng[i]>=0, and also the ROWID is modified if chngRowid is true.
001482  **
001483  ** Return true if CHECK constraint pExpr uses any of the
001484  ** changing columns (or the rowid if it is changing).  In other words,
001485  ** return true if this CHECK constraint must be validated for
001486  ** the new row in the UPDATE statement.
001487  **
001488  ** 2018-09-15: pExpr might also be an expression for an index-on-expressions.
001489  ** The operation of this routine is the same - return true if an only if
001490  ** the expression uses one or more of columns identified by the second and
001491  ** third arguments.
001492  */
001493  int sqlite3ExprReferencesUpdatedColumn(
001494    Expr *pExpr,    /* The expression to be checked */
001495    int *aiChng,    /* aiChng[x]>=0 if column x changed by the UPDATE */
001496    int chngRowid   /* True if UPDATE changes the rowid */
001497  ){
001498    Walker w;
001499    memset(&w, 0, sizeof(w));
001500    w.eCode = 0;
001501    w.xExprCallback = checkConstraintExprNode;
001502    w.u.aiCol = aiChng;
001503    sqlite3WalkExpr(&w, pExpr);
001504    if( !chngRowid ){
001505      testcase( (w.eCode & CKCNSTRNT_ROWID)!=0 );
001506      w.eCode &= ~CKCNSTRNT_ROWID;
001507    }
001508    testcase( w.eCode==0 );
001509    testcase( w.eCode==CKCNSTRNT_COLUMN );
001510    testcase( w.eCode==CKCNSTRNT_ROWID );
001511    testcase( w.eCode==(CKCNSTRNT_ROWID|CKCNSTRNT_COLUMN) );
001512    return w.eCode!=0;
001513  }
001514  
001515  /*
001516  ** The sqlite3GenerateConstraintChecks() routine usually wants to visit
001517  ** the indexes of a table in the order provided in the Table->pIndex list.
001518  ** However, sometimes (rarely - when there is an upsert) it wants to visit
001519  ** the indexes in a different order.  The following data structures accomplish
001520  ** this.
001521  **
001522  ** The IndexIterator object is used to walk through all of the indexes
001523  ** of a table in either Index.pNext order, or in some other order established
001524  ** by an array of IndexListTerm objects.
001525  */
001526  typedef struct IndexListTerm IndexListTerm;
001527  typedef struct IndexIterator IndexIterator;
001528  struct IndexIterator {
001529    int eType;    /* 0 for Index.pNext list.  1 for an array of IndexListTerm */
001530    int i;        /* Index of the current item from the list */
001531    union {
001532      struct {    /* Use this object for eType==0: A Index.pNext list */
001533        Index *pIdx;   /* The current Index */
001534      } lx;     
001535      struct {    /* Use this object for eType==1; Array of IndexListTerm */
001536        int nIdx;               /* Size of the array */
001537        IndexListTerm *aIdx;    /* Array of IndexListTerms */
001538      } ax;
001539    } u;
001540  };
001541  
001542  /* When IndexIterator.eType==1, then each index is an array of instances
001543  ** of the following object
001544  */
001545  struct IndexListTerm {
001546    Index *p;  /* The index */
001547    int ix;    /* Which entry in the original Table.pIndex list is this index*/
001548  };
001549  
001550  /* Return the first index on the list */
001551  static Index *indexIteratorFirst(IndexIterator *pIter, int *pIx){
001552    assert( pIter->i==0 );
001553    if( pIter->eType ){
001554      *pIx = pIter->u.ax.aIdx[0].ix;
001555      return pIter->u.ax.aIdx[0].p;
001556    }else{
001557      *pIx = 0;
001558      return pIter->u.lx.pIdx;
001559    }
001560  }
001561  
001562  /* Return the next index from the list.  Return NULL when out of indexes */
001563  static Index *indexIteratorNext(IndexIterator *pIter, int *pIx){
001564    if( pIter->eType ){
001565      int i = ++pIter->i;
001566      if( i>=pIter->u.ax.nIdx ){
001567        *pIx = i;
001568        return 0;
001569      }
001570      *pIx = pIter->u.ax.aIdx[i].ix;
001571      return pIter->u.ax.aIdx[i].p;
001572    }else{
001573      ++(*pIx);
001574      pIter->u.lx.pIdx = pIter->u.lx.pIdx->pNext;
001575      return pIter->u.lx.pIdx;
001576    }
001577  }
001578   
001579  /*
001580  ** Generate code to do constraint checks prior to an INSERT or an UPDATE
001581  ** on table pTab.
001582  **
001583  ** The regNewData parameter is the first register in a range that contains
001584  ** the data to be inserted or the data after the update.  There will be
001585  ** pTab->nCol+1 registers in this range.  The first register (the one
001586  ** that regNewData points to) will contain the new rowid, or NULL in the
001587  ** case of a WITHOUT ROWID table.  The second register in the range will
001588  ** contain the content of the first table column.  The third register will
001589  ** contain the content of the second table column.  And so forth.
001590  **
001591  ** The regOldData parameter is similar to regNewData except that it contains
001592  ** the data prior to an UPDATE rather than afterwards.  regOldData is zero
001593  ** for an INSERT.  This routine can distinguish between UPDATE and INSERT by
001594  ** checking regOldData for zero.
001595  **
001596  ** For an UPDATE, the pkChng boolean is true if the true primary key (the
001597  ** rowid for a normal table or the PRIMARY KEY for a WITHOUT ROWID table)
001598  ** might be modified by the UPDATE.  If pkChng is false, then the key of
001599  ** the iDataCur content table is guaranteed to be unchanged by the UPDATE.
001600  **
001601  ** For an INSERT, the pkChng boolean indicates whether or not the rowid
001602  ** was explicitly specified as part of the INSERT statement.  If pkChng
001603  ** is zero, it means that the either rowid is computed automatically or
001604  ** that the table is a WITHOUT ROWID table and has no rowid.  On an INSERT,
001605  ** pkChng will only be true if the INSERT statement provides an integer
001606  ** value for either the rowid column or its INTEGER PRIMARY KEY alias.
001607  **
001608  ** The code generated by this routine will store new index entries into
001609  ** registers identified by aRegIdx[].  No index entry is created for
001610  ** indices where aRegIdx[i]==0.  The order of indices in aRegIdx[] is
001611  ** the same as the order of indices on the linked list of indices
001612  ** at pTab->pIndex.
001613  **
001614  ** (2019-05-07) The generated code also creates a new record for the
001615  ** main table, if pTab is a rowid table, and stores that record in the
001616  ** register identified by aRegIdx[nIdx] - in other words in the first
001617  ** entry of aRegIdx[] past the last index.  It is important that the
001618  ** record be generated during constraint checks to avoid affinity changes
001619  ** to the register content that occur after constraint checks but before
001620  ** the new record is inserted.
001621  **
001622  ** The caller must have already opened writeable cursors on the main
001623  ** table and all applicable indices (that is to say, all indices for which
001624  ** aRegIdx[] is not zero).  iDataCur is the cursor for the main table when
001625  ** inserting or updating a rowid table, or the cursor for the PRIMARY KEY
001626  ** index when operating on a WITHOUT ROWID table.  iIdxCur is the cursor
001627  ** for the first index in the pTab->pIndex list.  Cursors for other indices
001628  ** are at iIdxCur+N for the N-th element of the pTab->pIndex list.
001629  **
001630  ** This routine also generates code to check constraints.  NOT NULL,
001631  ** CHECK, and UNIQUE constraints are all checked.  If a constraint fails,
001632  ** then the appropriate action is performed.  There are five possible
001633  ** actions: ROLLBACK, ABORT, FAIL, REPLACE, and IGNORE.
001634  **
001635  **  Constraint type  Action       What Happens
001636  **  ---------------  ----------   ----------------------------------------
001637  **  any              ROLLBACK     The current transaction is rolled back and
001638  **                                sqlite3_step() returns immediately with a
001639  **                                return code of SQLITE_CONSTRAINT.
001640  **
001641  **  any              ABORT        Back out changes from the current command
001642  **                                only (do not do a complete rollback) then
001643  **                                cause sqlite3_step() to return immediately
001644  **                                with SQLITE_CONSTRAINT.
001645  **
001646  **  any              FAIL         Sqlite3_step() returns immediately with a
001647  **                                return code of SQLITE_CONSTRAINT.  The
001648  **                                transaction is not rolled back and any
001649  **                                changes to prior rows are retained.
001650  **
001651  **  any              IGNORE       The attempt in insert or update the current
001652  **                                row is skipped, without throwing an error.
001653  **                                Processing continues with the next row.
001654  **                                (There is an immediate jump to ignoreDest.)
001655  **
001656  **  NOT NULL         REPLACE      The NULL value is replace by the default
001657  **                                value for that column.  If the default value
001658  **                                is NULL, the action is the same as ABORT.
001659  **
001660  **  UNIQUE           REPLACE      The other row that conflicts with the row
001661  **                                being inserted is removed.
001662  **
001663  **  CHECK            REPLACE      Illegal.  The results in an exception.
001664  **
001665  ** Which action to take is determined by the overrideError parameter.
001666  ** Or if overrideError==OE_Default, then the pParse->onError parameter
001667  ** is used.  Or if pParse->onError==OE_Default then the onError value
001668  ** for the constraint is used.
001669  */
001670  void sqlite3GenerateConstraintChecks(
001671    Parse *pParse,       /* The parser context */
001672    Table *pTab,         /* The table being inserted or updated */
001673    int *aRegIdx,        /* Use register aRegIdx[i] for index i.  0 for unused */
001674    int iDataCur,        /* Canonical data cursor (main table or PK index) */
001675    int iIdxCur,         /* First index cursor */
001676    int regNewData,      /* First register in a range holding values to insert */
001677    int regOldData,      /* Previous content.  0 for INSERTs */
001678    u8 pkChng,           /* Non-zero if the rowid or PRIMARY KEY changed */
001679    u8 overrideError,    /* Override onError to this if not OE_Default */
001680    int ignoreDest,      /* Jump to this label on an OE_Ignore resolution */
001681    int *pbMayReplace,   /* OUT: Set to true if constraint may cause a replace */
001682    int *aiChng,         /* column i is unchanged if aiChng[i]<0 */
001683    Upsert *pUpsert      /* ON CONFLICT clauses, if any.  NULL otherwise */
001684  ){
001685    Vdbe *v;             /* VDBE under construction */
001686    Index *pIdx;         /* Pointer to one of the indices */
001687    Index *pPk = 0;      /* The PRIMARY KEY index for WITHOUT ROWID tables */
001688    sqlite3 *db;         /* Database connection */
001689    int i;               /* loop counter */
001690    int ix;              /* Index loop counter */
001691    int nCol;            /* Number of columns */
001692    int onError;         /* Conflict resolution strategy */
001693    int seenReplace = 0; /* True if REPLACE is used to resolve INT PK conflict */
001694    int nPkField;        /* Number of fields in PRIMARY KEY. 1 for ROWID tables */
001695    Upsert *pUpsertClause = 0;  /* The specific ON CONFLICT clause for pIdx */
001696    u8 isUpdate;           /* True if this is an UPDATE operation */
001697    u8 bAffinityDone = 0;  /* True if the OP_Affinity operation has been run */
001698    int upsertIpkReturn = 0; /* Address of Goto at end of IPK uniqueness check */
001699    int upsertIpkDelay = 0;  /* Address of Goto to bypass initial IPK check */
001700    int ipkTop = 0;        /* Top of the IPK uniqueness check */
001701    int ipkBottom = 0;     /* OP_Goto at the end of the IPK uniqueness check */
001702    /* Variables associated with retesting uniqueness constraints after
001703    ** replace triggers fire have run */
001704    int regTrigCnt;       /* Register used to count replace trigger invocations */
001705    int addrRecheck = 0;  /* Jump here to recheck all uniqueness constraints */
001706    int lblRecheckOk = 0; /* Each recheck jumps to this label if it passes */
001707    Trigger *pTrigger;    /* List of DELETE triggers on the table pTab */
001708    int nReplaceTrig = 0; /* Number of replace triggers coded */
001709    IndexIterator sIdxIter;  /* Index iterator */
001710  
001711    isUpdate = regOldData!=0;
001712    db = pParse->db;
001713    v = pParse->pVdbe;
001714    assert( v!=0 );
001715    assert( !IsView(pTab) );  /* This table is not a VIEW */
001716    nCol = pTab->nCol;
001717   
001718    /* pPk is the PRIMARY KEY index for WITHOUT ROWID tables and NULL for
001719    ** normal rowid tables.  nPkField is the number of key fields in the
001720    ** pPk index or 1 for a rowid table.  In other words, nPkField is the
001721    ** number of fields in the true primary key of the table. */
001722    if( HasRowid(pTab) ){
001723      pPk = 0;
001724      nPkField = 1;
001725    }else{
001726      pPk = sqlite3PrimaryKeyIndex(pTab);
001727      nPkField = pPk->nKeyCol;
001728    }
001729  
001730    /* Record that this module has started */
001731    VdbeModuleComment((v, "BEGIN: GenCnstCks(%d,%d,%d,%d,%d)",
001732                       iDataCur, iIdxCur, regNewData, regOldData, pkChng));
001733  
001734    /* Test all NOT NULL constraints.
001735    */
001736    if( pTab->tabFlags & TF_HasNotNull ){
001737      int b2ndPass = 0;         /* True if currently running 2nd pass */
001738      int nSeenReplace = 0;     /* Number of ON CONFLICT REPLACE operations */
001739      int nGenerated = 0;       /* Number of generated columns with NOT NULL */
001740      while(1){  /* Make 2 passes over columns. Exit loop via "break" */
001741        for(i=0; i<nCol; i++){
001742          int iReg;                        /* Register holding column value */
001743          Column *pCol = &pTab->aCol[i];   /* The column to check for NOT NULL */
001744          int isGenerated;                 /* non-zero if column is generated */
001745          onError = pCol->notNull;
001746          if( onError==OE_None ) continue; /* No NOT NULL on this column */
001747          if( i==pTab->iPKey ){
001748            continue;        /* ROWID is never NULL */
001749          }
001750          isGenerated = pCol->colFlags & COLFLAG_GENERATED;
001751          if( isGenerated && !b2ndPass ){
001752            nGenerated++;
001753            continue;        /* Generated columns processed on 2nd pass */
001754          }
001755          if( aiChng && aiChng[i]<0 && !isGenerated ){
001756            /* Do not check NOT NULL on columns that do not change */
001757            continue;
001758          }
001759          if( overrideError!=OE_Default ){
001760            onError = overrideError;
001761          }else if( onError==OE_Default ){
001762            onError = OE_Abort;
001763          }
001764          if( onError==OE_Replace ){
001765            if( b2ndPass        /* REPLACE becomes ABORT on the 2nd pass */
001766             || pCol->iDflt==0  /* REPLACE is ABORT if no DEFAULT value */
001767            ){
001768              testcase( pCol->colFlags & COLFLAG_VIRTUAL );
001769              testcase( pCol->colFlags & COLFLAG_STORED );
001770              testcase( pCol->colFlags & COLFLAG_GENERATED );
001771              onError = OE_Abort;
001772            }else{
001773              assert( !isGenerated );
001774            }
001775          }else if( b2ndPass && !isGenerated ){
001776            continue;
001777          }
001778          assert( onError==OE_Rollback || onError==OE_Abort || onError==OE_Fail
001779              || onError==OE_Ignore || onError==OE_Replace );
001780          testcase( i!=sqlite3TableColumnToStorage(pTab, i) );
001781          iReg = sqlite3TableColumnToStorage(pTab, i) + regNewData + 1;
001782          switch( onError ){
001783            case OE_Replace: {
001784              int addr1 = sqlite3VdbeAddOp1(v, OP_NotNull, iReg);
001785              VdbeCoverage(v);
001786              assert( (pCol->colFlags & COLFLAG_GENERATED)==0 );
001787              nSeenReplace++;
001788              sqlite3ExprCodeCopy(pParse,
001789                 sqlite3ColumnExpr(pTab, pCol), iReg);
001790              sqlite3VdbeJumpHere(v, addr1);
001791              break;
001792            }
001793            case OE_Abort:
001794              sqlite3MayAbort(pParse);
001795              /* no break */ deliberate_fall_through
001796            case OE_Rollback:
001797            case OE_Fail: {
001798              char *zMsg = sqlite3MPrintf(db, "%s.%s", pTab->zName,
001799                                          pCol->zCnName);
001800              testcase( zMsg==0 && db->mallocFailed==0 );
001801              sqlite3VdbeAddOp3(v, OP_HaltIfNull, SQLITE_CONSTRAINT_NOTNULL,
001802                                onError, iReg);
001803              sqlite3VdbeAppendP4(v, zMsg, P4_DYNAMIC);
001804              sqlite3VdbeChangeP5(v, P5_ConstraintNotNull);
001805              VdbeCoverage(v);
001806              break;
001807            }
001808            default: {
001809              assert( onError==OE_Ignore );
001810              sqlite3VdbeAddOp2(v, OP_IsNull, iReg, ignoreDest);
001811              VdbeCoverage(v);
001812              break;
001813            }
001814          } /* end switch(onError) */
001815        } /* end loop i over columns */
001816        if( nGenerated==0 && nSeenReplace==0 ){
001817          /* If there are no generated columns with NOT NULL constraints
001818          ** and no NOT NULL ON CONFLICT REPLACE constraints, then a single
001819          ** pass is sufficient */
001820          break;
001821        }
001822        if( b2ndPass ) break;  /* Never need more than 2 passes */
001823        b2ndPass = 1;
001824  #ifndef SQLITE_OMIT_GENERATED_COLUMNS
001825        if( nSeenReplace>0 && (pTab->tabFlags & TF_HasGenerated)!=0 ){
001826          /* If any NOT NULL ON CONFLICT REPLACE constraints fired on the
001827          ** first pass, recomputed values for all generated columns, as
001828          ** those values might depend on columns affected by the REPLACE.
001829          */
001830          sqlite3ComputeGeneratedColumns(pParse, regNewData+1, pTab);
001831        }
001832  #endif
001833      } /* end of 2-pass loop */
001834    } /* end if( has-not-null-constraints ) */
001835  
001836    /* Test all CHECK constraints
001837    */
001838  #ifndef SQLITE_OMIT_CHECK
001839    if( pTab->pCheck && (db->flags & SQLITE_IgnoreChecks)==0 ){
001840      ExprList *pCheck = pTab->pCheck;
001841      pParse->iSelfTab = -(regNewData+1);
001842      onError = overrideError!=OE_Default ? overrideError : OE_Abort;
001843      for(i=0; i<pCheck->nExpr; i++){
001844        int allOk;
001845        Expr *pCopy;
001846        Expr *pExpr = pCheck->a[i].pExpr;
001847        if( aiChng
001848         && !sqlite3ExprReferencesUpdatedColumn(pExpr, aiChng, pkChng)
001849        ){
001850          /* The check constraints do not reference any of the columns being
001851          ** updated so there is no point it verifying the check constraint */
001852          continue;
001853        }
001854        if( bAffinityDone==0 ){
001855          sqlite3TableAffinity(v, pTab, regNewData+1);
001856          bAffinityDone = 1;
001857        }
001858        allOk = sqlite3VdbeMakeLabel(pParse);
001859        sqlite3VdbeVerifyAbortable(v, onError);
001860        pCopy = sqlite3ExprDup(db, pExpr, 0);
001861        if( !db->mallocFailed ){
001862          sqlite3ExprIfTrue(pParse, pCopy, allOk, SQLITE_JUMPIFNULL);
001863        }
001864        sqlite3ExprDelete(db, pCopy);
001865        if( onError==OE_Ignore ){
001866          sqlite3VdbeGoto(v, ignoreDest);
001867        }else{
001868          char *zName = pCheck->a[i].zEName;
001869          assert( zName!=0 || pParse->db->mallocFailed );
001870          if( onError==OE_Replace ) onError = OE_Abort; /* IMP: R-26383-51744 */
001871          sqlite3HaltConstraint(pParse, SQLITE_CONSTRAINT_CHECK,
001872                                onError, zName, P4_TRANSIENT,
001873                                P5_ConstraintCheck);
001874        }
001875        sqlite3VdbeResolveLabel(v, allOk);
001876      }
001877      pParse->iSelfTab = 0;
001878    }
001879  #endif /* !defined(SQLITE_OMIT_CHECK) */
001880  
001881    /* UNIQUE and PRIMARY KEY constraints should be handled in the following
001882    ** order:
001883    **
001884    **   (1)  OE_Update
001885    **   (2)  OE_Abort, OE_Fail, OE_Rollback, OE_Ignore
001886    **   (3)  OE_Replace
001887    **
001888    ** OE_Fail and OE_Ignore must happen before any changes are made.
001889    ** OE_Update guarantees that only a single row will change, so it
001890    ** must happen before OE_Replace.  Technically, OE_Abort and OE_Rollback
001891    ** could happen in any order, but they are grouped up front for
001892    ** convenience.
001893    **
001894    ** 2018-08-14: Ticket https://www.sqlite.org/src/info/908f001483982c43
001895    ** The order of constraints used to have OE_Update as (2) and OE_Abort
001896    ** and so forth as (1). But apparently PostgreSQL checks the OE_Update
001897    ** constraint before any others, so it had to be moved.
001898    **
001899    ** Constraint checking code is generated in this order:
001900    **   (A)  The rowid constraint
001901    **   (B)  Unique index constraints that do not have OE_Replace as their
001902    **        default conflict resolution strategy
001903    **   (C)  Unique index that do use OE_Replace by default.
001904    **
001905    ** The ordering of (2) and (3) is accomplished by making sure the linked
001906    ** list of indexes attached to a table puts all OE_Replace indexes last
001907    ** in the list.  See sqlite3CreateIndex() for where that happens.
001908    */
001909    sIdxIter.eType = 0;
001910    sIdxIter.i = 0;
001911    sIdxIter.u.ax.aIdx = 0;  /* Silence harmless compiler warning */
001912    sIdxIter.u.lx.pIdx = pTab->pIndex;
001913    if( pUpsert ){
001914      if( pUpsert->pUpsertTarget==0 ){
001915        /* There is just on ON CONFLICT clause and it has no constraint-target */
001916        assert( pUpsert->pNextUpsert==0 );
001917        if( pUpsert->isDoUpdate==0 ){
001918          /* A single ON CONFLICT DO NOTHING clause, without a constraint-target.
001919          ** Make all unique constraint resolution be OE_Ignore */
001920          overrideError = OE_Ignore;
001921          pUpsert = 0;
001922        }else{
001923          /* A single ON CONFLICT DO UPDATE.  Make all resolutions OE_Update */
001924          overrideError = OE_Update;
001925        }
001926      }else if( pTab->pIndex!=0 ){
001927        /* Otherwise, we'll need to run the IndexListTerm array version of the
001928        ** iterator to ensure that all of the ON CONFLICT conditions are
001929        ** checked first and in order. */
001930        int nIdx, jj;
001931        u64 nByte;
001932        Upsert *pTerm;
001933        u8 *bUsed;
001934        for(nIdx=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, nIdx++){
001935           assert( aRegIdx[nIdx]>0 );
001936        }
001937        sIdxIter.eType = 1;
001938        sIdxIter.u.ax.nIdx = nIdx;
001939        nByte = (sizeof(IndexListTerm)+1)*nIdx + nIdx;
001940        sIdxIter.u.ax.aIdx = sqlite3DbMallocZero(db, nByte);
001941        if( sIdxIter.u.ax.aIdx==0 ) return; /* OOM */
001942        bUsed = (u8*)&sIdxIter.u.ax.aIdx[nIdx];
001943        pUpsert->pToFree = sIdxIter.u.ax.aIdx;
001944        for(i=0, pTerm=pUpsert; pTerm; pTerm=pTerm->pNextUpsert){
001945          if( pTerm->pUpsertTarget==0 ) break;
001946          if( pTerm->pUpsertIdx==0 ) continue;  /* Skip ON CONFLICT for the IPK */
001947          jj = 0;
001948          pIdx = pTab->pIndex;
001949          while( ALWAYS(pIdx!=0) && pIdx!=pTerm->pUpsertIdx ){
001950             pIdx = pIdx->pNext;
001951             jj++;
001952          }
001953          if( bUsed[jj] ) continue; /* Duplicate ON CONFLICT clause ignored */
001954          bUsed[jj] = 1;
001955          sIdxIter.u.ax.aIdx[i].p = pIdx;
001956          sIdxIter.u.ax.aIdx[i].ix = jj;
001957          i++;
001958        }
001959        for(jj=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, jj++){
001960          if( bUsed[jj] ) continue;
001961          sIdxIter.u.ax.aIdx[i].p = pIdx;
001962          sIdxIter.u.ax.aIdx[i].ix = jj;
001963          i++;
001964        }
001965        assert( i==nIdx );
001966      }
001967    }
001968  
001969    /* Determine if it is possible that triggers (either explicitly coded
001970    ** triggers or FK resolution actions) might run as a result of deletes
001971    ** that happen when OE_Replace conflict resolution occurs. (Call these
001972    ** "replace triggers".)  If any replace triggers run, we will need to
001973    ** recheck all of the uniqueness constraints after they have all run.
001974    ** But on the recheck, the resolution is OE_Abort instead of OE_Replace.
001975    **
001976    ** If replace triggers are a possibility, then
001977    **
001978    **   (1) Allocate register regTrigCnt and initialize it to zero.
001979    **       That register will count the number of replace triggers that
001980    **       fire.  Constraint recheck only occurs if the number is positive.
001981    **   (2) Initialize pTrigger to the list of all DELETE triggers on pTab.
001982    **   (3) Initialize addrRecheck and lblRecheckOk
001983    **
001984    ** The uniqueness rechecking code will create a series of tests to run
001985    ** in a second pass.  The addrRecheck and lblRecheckOk variables are
001986    ** used to link together these tests which are separated from each other
001987    ** in the generate bytecode.
001988    */
001989    if( (db->flags & (SQLITE_RecTriggers|SQLITE_ForeignKeys))==0 ){
001990      /* There are not DELETE triggers nor FK constraints.  No constraint
001991      ** rechecks are needed. */
001992      pTrigger = 0;
001993      regTrigCnt = 0;
001994    }else{
001995      if( db->flags&SQLITE_RecTriggers ){
001996        pTrigger = sqlite3TriggersExist(pParse, pTab, TK_DELETE, 0, 0);
001997        regTrigCnt = pTrigger!=0 || sqlite3FkRequired(pParse, pTab, 0, 0);
001998      }else{
001999        pTrigger = 0;
002000        regTrigCnt = sqlite3FkRequired(pParse, pTab, 0, 0);
002001      }
002002      if( regTrigCnt ){
002003        /* Replace triggers might exist.  Allocate the counter and
002004        ** initialize it to zero. */
002005        regTrigCnt = ++pParse->nMem;
002006        sqlite3VdbeAddOp2(v, OP_Integer, 0, regTrigCnt);
002007        VdbeComment((v, "trigger count"));
002008        lblRecheckOk = sqlite3VdbeMakeLabel(pParse);
002009        addrRecheck = lblRecheckOk;
002010      }
002011    }
002012  
002013    /* If rowid is changing, make sure the new rowid does not previously
002014    ** exist in the table.
002015    */
002016    if( pkChng && pPk==0 ){
002017      int addrRowidOk = sqlite3VdbeMakeLabel(pParse);
002018  
002019      /* Figure out what action to take in case of a rowid collision */
002020      onError = pTab->keyConf;
002021      if( overrideError!=OE_Default ){
002022        onError = overrideError;
002023      }else if( onError==OE_Default ){
002024        onError = OE_Abort;
002025      }
002026  
002027      /* figure out whether or not upsert applies in this case */
002028      if( pUpsert ){
002029        pUpsertClause = sqlite3UpsertOfIndex(pUpsert,0);
002030        if( pUpsertClause!=0 ){
002031          if( pUpsertClause->isDoUpdate==0 ){
002032            onError = OE_Ignore;  /* DO NOTHING is the same as INSERT OR IGNORE */
002033          }else{
002034            onError = OE_Update;  /* DO UPDATE */
002035          }
002036        }
002037        if( pUpsertClause!=pUpsert ){
002038          /* The first ON CONFLICT clause has a conflict target other than
002039          ** the IPK.  We have to jump ahead to that first ON CONFLICT clause
002040          ** and then come back here and deal with the IPK afterwards */
002041          upsertIpkDelay = sqlite3VdbeAddOp0(v, OP_Goto);
002042        }
002043      }
002044  
002045      /* If the response to a rowid conflict is REPLACE but the response
002046      ** to some other UNIQUE constraint is FAIL or IGNORE, then we need
002047      ** to defer the running of the rowid conflict checking until after
002048      ** the UNIQUE constraints have run.
002049      */
002050      if( onError==OE_Replace      /* IPK rule is REPLACE */
002051       && onError!=overrideError   /* Rules for other constraints are different */
002052       && pTab->pIndex             /* There exist other constraints */
002053       && !upsertIpkDelay          /* IPK check already deferred by UPSERT */
002054      ){
002055        ipkTop = sqlite3VdbeAddOp0(v, OP_Goto)+1;
002056        VdbeComment((v, "defer IPK REPLACE until last"));
002057      }
002058  
002059      if( isUpdate ){
002060        /* pkChng!=0 does not mean that the rowid has changed, only that
002061        ** it might have changed.  Skip the conflict logic below if the rowid
002062        ** is unchanged. */
002063        sqlite3VdbeAddOp3(v, OP_Eq, regNewData, addrRowidOk, regOldData);
002064        sqlite3VdbeChangeP5(v, SQLITE_NOTNULL);
002065        VdbeCoverage(v);
002066      }
002067  
002068      /* Check to see if the new rowid already exists in the table.  Skip
002069      ** the following conflict logic if it does not. */
002070      VdbeNoopComment((v, "uniqueness check for ROWID"));
002071      sqlite3VdbeVerifyAbortable(v, onError);
002072      sqlite3VdbeAddOp3(v, OP_NotExists, iDataCur, addrRowidOk, regNewData);
002073      VdbeCoverage(v);
002074  
002075      switch( onError ){
002076        default: {
002077          onError = OE_Abort;
002078          /* no break */ deliberate_fall_through
002079        }
002080        case OE_Rollback:
002081        case OE_Abort:
002082        case OE_Fail: {
002083          testcase( onError==OE_Rollback );
002084          testcase( onError==OE_Abort );
002085          testcase( onError==OE_Fail );
002086          sqlite3RowidConstraint(pParse, onError, pTab);
002087          break;
002088        }
002089        case OE_Replace: {
002090          /* If there are DELETE triggers on this table and the
002091          ** recursive-triggers flag is set, call GenerateRowDelete() to
002092          ** remove the conflicting row from the table. This will fire
002093          ** the triggers and remove both the table and index b-tree entries.
002094          **
002095          ** Otherwise, if there are no triggers or the recursive-triggers
002096          ** flag is not set, but the table has one or more indexes, call
002097          ** GenerateRowIndexDelete(). This removes the index b-tree entries
002098          ** only. The table b-tree entry will be replaced by the new entry
002099          ** when it is inserted. 
002100          **
002101          ** If either GenerateRowDelete() or GenerateRowIndexDelete() is called,
002102          ** also invoke MultiWrite() to indicate that this VDBE may require
002103          ** statement rollback (if the statement is aborted after the delete
002104          ** takes place). Earlier versions called sqlite3MultiWrite() regardless,
002105          ** but being more selective here allows statements like:
002106          **
002107          **   REPLACE INTO t(rowid) VALUES($newrowid)
002108          **
002109          ** to run without a statement journal if there are no indexes on the
002110          ** table.
002111          */
002112          if( regTrigCnt ){
002113            sqlite3MultiWrite(pParse);
002114            sqlite3GenerateRowDelete(pParse, pTab, pTrigger, iDataCur, iIdxCur,
002115                                     regNewData, 1, 0, OE_Replace, 1, -1);
002116            sqlite3VdbeAddOp2(v, OP_AddImm, regTrigCnt, 1); /* incr trigger cnt */
002117            nReplaceTrig++;
002118          }else{
002119  #ifdef SQLITE_ENABLE_PREUPDATE_HOOK
002120            assert( HasRowid(pTab) );
002121            /* This OP_Delete opcode fires the pre-update-hook only. It does
002122            ** not modify the b-tree. It is more efficient to let the coming
002123            ** OP_Insert replace the existing entry than it is to delete the
002124            ** existing entry and then insert a new one. */
002125            sqlite3VdbeAddOp2(v, OP_Delete, iDataCur, OPFLAG_ISNOOP);
002126            sqlite3VdbeAppendP4(v, pTab, P4_TABLE);
002127  #endif /* SQLITE_ENABLE_PREUPDATE_HOOK */
002128            if( pTab->pIndex ){
002129              sqlite3MultiWrite(pParse);
002130              sqlite3GenerateRowIndexDelete(pParse, pTab, iDataCur, iIdxCur,0,-1);
002131            }
002132          }
002133          seenReplace = 1;
002134          break;
002135        }
002136  #ifndef SQLITE_OMIT_UPSERT
002137        case OE_Update: {
002138          sqlite3UpsertDoUpdate(pParse, pUpsert, pTab, 0, iDataCur);
002139          /* no break */ deliberate_fall_through
002140        }
002141  #endif
002142        case OE_Ignore: {
002143          testcase( onError==OE_Ignore );
002144          sqlite3VdbeGoto(v, ignoreDest);
002145          break;
002146        }
002147      }
002148      sqlite3VdbeResolveLabel(v, addrRowidOk);
002149      if( pUpsert && pUpsertClause!=pUpsert ){
002150        upsertIpkReturn = sqlite3VdbeAddOp0(v, OP_Goto);
002151      }else if( ipkTop ){
002152        ipkBottom = sqlite3VdbeAddOp0(v, OP_Goto);
002153        sqlite3VdbeJumpHere(v, ipkTop-1);
002154      }
002155    }
002156  
002157    /* Test all UNIQUE constraints by creating entries for each UNIQUE
002158    ** index and making sure that duplicate entries do not already exist.
002159    ** Compute the revised record entries for indices as we go.
002160    **
002161    ** This loop also handles the case of the PRIMARY KEY index for a
002162    ** WITHOUT ROWID table.
002163    */
002164    for(pIdx = indexIteratorFirst(&sIdxIter, &ix);
002165        pIdx;
002166        pIdx = indexIteratorNext(&sIdxIter, &ix)
002167    ){
002168      int regIdx;          /* Range of registers holding content for pIdx */
002169      int regR;            /* Range of registers holding conflicting PK */
002170      int iThisCur;        /* Cursor for this UNIQUE index */
002171      int addrUniqueOk;    /* Jump here if the UNIQUE constraint is satisfied */
002172      int addrConflictCk;  /* First opcode in the conflict check logic */
002173  
002174      if( aRegIdx[ix]==0 ) continue;  /* Skip indices that do not change */
002175      if( pUpsert ){
002176        pUpsertClause = sqlite3UpsertOfIndex(pUpsert, pIdx);
002177        if( upsertIpkDelay && pUpsertClause==pUpsert ){
002178          sqlite3VdbeJumpHere(v, upsertIpkDelay);
002179        }
002180      }
002181      addrUniqueOk = sqlite3VdbeMakeLabel(pParse);
002182      if( bAffinityDone==0 ){
002183        sqlite3TableAffinity(v, pTab, regNewData+1);
002184        bAffinityDone = 1;
002185      }
002186      VdbeNoopComment((v, "prep index %s", pIdx->zName));
002187      iThisCur = iIdxCur+ix;
002188  
002189  
002190      /* Skip partial indices for which the WHERE clause is not true */
002191      if( pIdx->pPartIdxWhere ){
002192        sqlite3VdbeAddOp2(v, OP_Null, 0, aRegIdx[ix]);
002193        pParse->iSelfTab = -(regNewData+1);
002194        sqlite3ExprIfFalseDup(pParse, pIdx->pPartIdxWhere, addrUniqueOk,
002195                              SQLITE_JUMPIFNULL);
002196        pParse->iSelfTab = 0;
002197      }
002198  
002199      /* Create a record for this index entry as it should appear after
002200      ** the insert or update.  Store that record in the aRegIdx[ix] register
002201      */
002202      regIdx = aRegIdx[ix]+1;
002203      for(i=0; i<pIdx->nColumn; i++){
002204        int iField = pIdx->aiColumn[i];
002205        int x;
002206        if( iField==XN_EXPR ){
002207          pParse->iSelfTab = -(regNewData+1);
002208          sqlite3ExprCodeCopy(pParse, pIdx->aColExpr->a[i].pExpr, regIdx+i);
002209          pParse->iSelfTab = 0;
002210          VdbeComment((v, "%s column %d", pIdx->zName, i));
002211        }else if( iField==XN_ROWID || iField==pTab->iPKey ){
002212          x = regNewData;
002213          sqlite3VdbeAddOp2(v, OP_IntCopy, x, regIdx+i);
002214          VdbeComment((v, "rowid"));
002215        }else{
002216          testcase( sqlite3TableColumnToStorage(pTab, iField)!=iField );
002217          x = sqlite3TableColumnToStorage(pTab, iField) + regNewData + 1;
002218          sqlite3VdbeAddOp2(v, OP_SCopy, x, regIdx+i);
002219          VdbeComment((v, "%s", pTab->aCol[iField].zCnName));
002220        }
002221      }
002222      sqlite3VdbeAddOp3(v, OP_MakeRecord, regIdx, pIdx->nColumn, aRegIdx[ix]);
002223      VdbeComment((v, "for %s", pIdx->zName));
002224  #ifdef SQLITE_ENABLE_NULL_TRIM
002225      if( pIdx->idxType==SQLITE_IDXTYPE_PRIMARYKEY ){
002226        sqlite3SetMakeRecordP5(v, pIdx->pTable);
002227      }
002228  #endif
002229      sqlite3VdbeReleaseRegisters(pParse, regIdx, pIdx->nColumn, 0, 0);
002230  
002231      /* In an UPDATE operation, if this index is the PRIMARY KEY index
002232      ** of a WITHOUT ROWID table and there has been no change the
002233      ** primary key, then no collision is possible.  The collision detection
002234      ** logic below can all be skipped. */
002235      if( isUpdate && pPk==pIdx && pkChng==0 ){
002236        sqlite3VdbeResolveLabel(v, addrUniqueOk);
002237        continue;
002238      }
002239  
002240      /* Find out what action to take in case there is a uniqueness conflict */
002241      onError = pIdx->onError;
002242      if( onError==OE_None ){
002243        sqlite3VdbeResolveLabel(v, addrUniqueOk);
002244        continue;  /* pIdx is not a UNIQUE index */
002245      }
002246      if( overrideError!=OE_Default ){
002247        onError = overrideError;
002248      }else if( onError==OE_Default ){
002249        onError = OE_Abort;
002250      }
002251  
002252      /* Figure out if the upsert clause applies to this index */
002253      if( pUpsertClause ){
002254        if( pUpsertClause->isDoUpdate==0 ){
002255          onError = OE_Ignore;  /* DO NOTHING is the same as INSERT OR IGNORE */
002256        }else{
002257          onError = OE_Update;  /* DO UPDATE */
002258        }
002259      }
002260  
002261      /* Collision detection may be omitted if all of the following are true:
002262      **   (1) The conflict resolution algorithm is REPLACE
002263      **   (2) The table is a WITHOUT ROWID table
002264      **   (3) There are no secondary indexes on the table
002265      **   (4) No delete triggers need to be fired if there is a conflict
002266      **   (5) No FK constraint counters need to be updated if a conflict occurs.
002267      **
002268      ** This is not possible for ENABLE_PREUPDATE_HOOK builds, as the row
002269      ** must be explicitly deleted in order to ensure any pre-update hook
002270      ** is invoked.  */
002271      assert( IsOrdinaryTable(pTab) );
002272  #ifndef SQLITE_ENABLE_PREUPDATE_HOOK
002273      if( (ix==0 && pIdx->pNext==0)                   /* Condition 3 */
002274       && pPk==pIdx                                   /* Condition 2 */
002275       && onError==OE_Replace                         /* Condition 1 */
002276       && ( 0==(db->flags&SQLITE_RecTriggers) ||      /* Condition 4 */
002277            0==sqlite3TriggersExist(pParse, pTab, TK_DELETE, 0, 0))
002278       && ( 0==(db->flags&SQLITE_ForeignKeys) ||      /* Condition 5 */
002279           (0==pTab->u.tab.pFKey && 0==sqlite3FkReferences(pTab)))
002280      ){
002281        sqlite3VdbeResolveLabel(v, addrUniqueOk);
002282        continue;
002283      }
002284  #endif /* ifndef SQLITE_ENABLE_PREUPDATE_HOOK */
002285  
002286      /* Check to see if the new index entry will be unique */
002287      sqlite3VdbeVerifyAbortable(v, onError);
002288      addrConflictCk =
002289        sqlite3VdbeAddOp4Int(v, OP_NoConflict, iThisCur, addrUniqueOk,
002290                             regIdx, pIdx->nKeyCol); VdbeCoverage(v);
002291  
002292      /* Generate code to handle collisions */
002293      regR = pIdx==pPk ? regIdx : sqlite3GetTempRange(pParse, nPkField);
002294      if( isUpdate || onError==OE_Replace ){
002295        if( HasRowid(pTab) ){
002296          sqlite3VdbeAddOp2(v, OP_IdxRowid, iThisCur, regR);
002297          /* Conflict only if the rowid of the existing index entry
002298          ** is different from old-rowid */
002299          if( isUpdate ){
002300            sqlite3VdbeAddOp3(v, OP_Eq, regR, addrUniqueOk, regOldData);
002301            sqlite3VdbeChangeP5(v, SQLITE_NOTNULL);
002302            VdbeCoverage(v);
002303          }
002304        }else{
002305          int x;
002306          /* Extract the PRIMARY KEY from the end of the index entry and
002307          ** store it in registers regR..regR+nPk-1 */
002308          if( pIdx!=pPk ){
002309            for(i=0; i<pPk->nKeyCol; i++){
002310              assert( pPk->aiColumn[i]>=0 );
002311              x = sqlite3TableColumnToIndex(pIdx, pPk->aiColumn[i]);
002312              sqlite3VdbeAddOp3(v, OP_Column, iThisCur, x, regR+i);
002313              VdbeComment((v, "%s.%s", pTab->zName,
002314                           pTab->aCol[pPk->aiColumn[i]].zCnName));
002315            }
002316          }
002317          if( isUpdate ){
002318            /* If currently processing the PRIMARY KEY of a WITHOUT ROWID
002319            ** table, only conflict if the new PRIMARY KEY values are actually
002320            ** different from the old.  See TH3 withoutrowid04.test.
002321            **
002322            ** For a UNIQUE index, only conflict if the PRIMARY KEY values
002323            ** of the matched index row are different from the original PRIMARY
002324            ** KEY values of this row before the update.  */
002325            int addrJump = sqlite3VdbeCurrentAddr(v)+pPk->nKeyCol;
002326            int op = OP_Ne;
002327            int regCmp = (IsPrimaryKeyIndex(pIdx) ? regIdx : regR);
002328   
002329            for(i=0; i<pPk->nKeyCol; i++){
002330              char *p4 = (char*)sqlite3LocateCollSeq(pParse, pPk->azColl[i]);
002331              x = pPk->aiColumn[i];
002332              assert( x>=0 );
002333              if( i==(pPk->nKeyCol-1) ){
002334                addrJump = addrUniqueOk;
002335                op = OP_Eq;
002336              }
002337              x = sqlite3TableColumnToStorage(pTab, x);
002338              sqlite3VdbeAddOp4(v, op,
002339                  regOldData+1+x, addrJump, regCmp+i, p4, P4_COLLSEQ
002340              );
002341              sqlite3VdbeChangeP5(v, SQLITE_NOTNULL);
002342              VdbeCoverageIf(v, op==OP_Eq);
002343              VdbeCoverageIf(v, op==OP_Ne);
002344            }
002345          }
002346        }
002347      }
002348  
002349      /* Generate code that executes if the new index entry is not unique */
002350      assert( onError==OE_Rollback || onError==OE_Abort || onError==OE_Fail
002351          || onError==OE_Ignore || onError==OE_Replace || onError==OE_Update );
002352      switch( onError ){
002353        case OE_Rollback:
002354        case OE_Abort:
002355        case OE_Fail: {
002356          testcase( onError==OE_Rollback );
002357          testcase( onError==OE_Abort );
002358          testcase( onError==OE_Fail );
002359          sqlite3UniqueConstraint(pParse, onError, pIdx);
002360          break;
002361        }
002362  #ifndef SQLITE_OMIT_UPSERT
002363        case OE_Update: {
002364          sqlite3UpsertDoUpdate(pParse, pUpsert, pTab, pIdx, iIdxCur+ix);
002365          /* no break */ deliberate_fall_through
002366        }
002367  #endif
002368        case OE_Ignore: {
002369          testcase( onError==OE_Ignore );
002370          sqlite3VdbeGoto(v, ignoreDest);
002371          break;
002372        }
002373        default: {
002374          int nConflictCk;   /* Number of opcodes in conflict check logic */
002375  
002376          assert( onError==OE_Replace );
002377          nConflictCk = sqlite3VdbeCurrentAddr(v) - addrConflictCk;
002378          assert( nConflictCk>0 || db->mallocFailed );
002379          testcase( nConflictCk<=0 );
002380          testcase( nConflictCk>1 );
002381          if( regTrigCnt ){
002382            sqlite3MultiWrite(pParse);
002383            nReplaceTrig++;
002384          }
002385          if( pTrigger && isUpdate ){
002386            sqlite3VdbeAddOp1(v, OP_CursorLock, iDataCur);
002387          }
002388          sqlite3GenerateRowDelete(pParse, pTab, pTrigger, iDataCur, iIdxCur,
002389              regR, nPkField, 0, OE_Replace,
002390              (pIdx==pPk ? ONEPASS_SINGLE : ONEPASS_OFF), iThisCur);
002391          if( pTrigger && isUpdate ){
002392            sqlite3VdbeAddOp1(v, OP_CursorUnlock, iDataCur);
002393          }
002394          if( regTrigCnt ){
002395            int addrBypass;  /* Jump destination to bypass recheck logic */
002396  
002397            sqlite3VdbeAddOp2(v, OP_AddImm, regTrigCnt, 1); /* incr trigger cnt */
002398            addrBypass = sqlite3VdbeAddOp0(v, OP_Goto);  /* Bypass recheck */
002399            VdbeComment((v, "bypass recheck"));
002400  
002401            /* Here we insert code that will be invoked after all constraint
002402            ** checks have run, if and only if one or more replace triggers
002403            ** fired. */
002404            sqlite3VdbeResolveLabel(v, lblRecheckOk);
002405            lblRecheckOk = sqlite3VdbeMakeLabel(pParse);
002406            if( pIdx->pPartIdxWhere ){
002407              /* Bypass the recheck if this partial index is not defined
002408              ** for the current row */
002409              sqlite3VdbeAddOp2(v, OP_IsNull, regIdx-1, lblRecheckOk);
002410              VdbeCoverage(v);
002411            }
002412            /* Copy the constraint check code from above, except change
002413            ** the constraint-ok jump destination to be the address of
002414            ** the next retest block */
002415            while( nConflictCk>0 ){
002416              VdbeOp x;    /* Conflict check opcode to copy */
002417              /* The sqlite3VdbeAddOp4() call might reallocate the opcode array.
002418              ** Hence, make a complete copy of the opcode, rather than using
002419              ** a pointer to the opcode. */
002420              x = *sqlite3VdbeGetOp(v, addrConflictCk);
002421              if( x.opcode!=OP_IdxRowid ){
002422                int p2;      /* New P2 value for copied conflict check opcode */
002423                const char *zP4;
002424                if( sqlite3OpcodeProperty[x.opcode]&OPFLG_JUMP ){
002425                  p2 = lblRecheckOk;
002426                }else{
002427                  p2 = x.p2;
002428                }
002429                zP4 = x.p4type==P4_INT32 ? SQLITE_INT_TO_PTR(x.p4.i) : x.p4.z;
002430                sqlite3VdbeAddOp4(v, x.opcode, x.p1, p2, x.p3, zP4, x.p4type);
002431                sqlite3VdbeChangeP5(v, x.p5);
002432                VdbeCoverageIf(v, p2!=x.p2);
002433              }
002434              nConflictCk--;
002435              addrConflictCk++;
002436            }
002437            /* If the retest fails, issue an abort */
002438            sqlite3UniqueConstraint(pParse, OE_Abort, pIdx);
002439  
002440            sqlite3VdbeJumpHere(v, addrBypass); /* Terminate the recheck bypass */
002441          }
002442          seenReplace = 1;
002443          break;
002444        }
002445      }
002446      sqlite3VdbeResolveLabel(v, addrUniqueOk);
002447      if( regR!=regIdx ) sqlite3ReleaseTempRange(pParse, regR, nPkField);
002448      if( pUpsertClause
002449       && upsertIpkReturn
002450       && sqlite3UpsertNextIsIPK(pUpsertClause)
002451      ){
002452        sqlite3VdbeGoto(v, upsertIpkDelay+1);
002453        sqlite3VdbeJumpHere(v, upsertIpkReturn);
002454        upsertIpkReturn = 0;
002455      }
002456    }
002457  
002458    /* If the IPK constraint is a REPLACE, run it last */
002459    if( ipkTop ){
002460      sqlite3VdbeGoto(v, ipkTop);
002461      VdbeComment((v, "Do IPK REPLACE"));
002462      assert( ipkBottom>0 );
002463      sqlite3VdbeJumpHere(v, ipkBottom);
002464    }
002465  
002466    /* Recheck all uniqueness constraints after replace triggers have run */
002467    testcase( regTrigCnt!=0 && nReplaceTrig==0 );
002468    assert( regTrigCnt!=0 || nReplaceTrig==0 );
002469    if( nReplaceTrig ){
002470      sqlite3VdbeAddOp2(v, OP_IfNot, regTrigCnt, lblRecheckOk);VdbeCoverage(v);
002471      if( !pPk ){
002472        if( isUpdate ){
002473          sqlite3VdbeAddOp3(v, OP_Eq, regNewData, addrRecheck, regOldData);
002474          sqlite3VdbeChangeP5(v, SQLITE_NOTNULL);
002475          VdbeCoverage(v);
002476        }
002477        sqlite3VdbeAddOp3(v, OP_NotExists, iDataCur, addrRecheck, regNewData);
002478        VdbeCoverage(v);
002479        sqlite3RowidConstraint(pParse, OE_Abort, pTab);
002480      }else{
002481        sqlite3VdbeGoto(v, addrRecheck);
002482      }
002483      sqlite3VdbeResolveLabel(v, lblRecheckOk);
002484    }
002485  
002486    /* Generate the table record */
002487    if( HasRowid(pTab) ){
002488      int regRec = aRegIdx[ix];
002489      sqlite3VdbeAddOp3(v, OP_MakeRecord, regNewData+1, pTab->nNVCol, regRec);
002490      sqlite3SetMakeRecordP5(v, pTab);
002491      if( !bAffinityDone ){
002492        sqlite3TableAffinity(v, pTab, 0);
002493      }
002494    }
002495  
002496    *pbMayReplace = seenReplace;
002497    VdbeModuleComment((v, "END: GenCnstCks(%d)", seenReplace));
002498  }
002499  
002500  #ifdef SQLITE_ENABLE_NULL_TRIM
002501  /*
002502  ** Change the P5 operand on the last opcode (which should be an OP_MakeRecord)
002503  ** to be the number of columns in table pTab that must not be NULL-trimmed.
002504  **
002505  ** Or if no columns of pTab may be NULL-trimmed, leave P5 at zero.
002506  */
002507  void sqlite3SetMakeRecordP5(Vdbe *v, Table *pTab){
002508    u16 i;
002509  
002510    /* Records with omitted columns are only allowed for schema format
002511    ** version 2 and later (SQLite version 3.1.4, 2005-02-20). */
002512    if( pTab->pSchema->file_format<2 ) return;
002513  
002514    for(i=pTab->nCol-1; i>0; i--){
002515      if( pTab->aCol[i].iDflt!=0 ) break;
002516      if( pTab->aCol[i].colFlags & COLFLAG_PRIMKEY ) break;
002517    }
002518    sqlite3VdbeChangeP5(v, i+1);
002519  }
002520  #endif
002521  
002522  /*
002523  ** Table pTab is a WITHOUT ROWID table that is being written to. The cursor
002524  ** number is iCur, and register regData contains the new record for the
002525  ** PK index. This function adds code to invoke the pre-update hook,
002526  ** if one is registered.
002527  */
002528  #ifdef SQLITE_ENABLE_PREUPDATE_HOOK
002529  static void codeWithoutRowidPreupdate(
002530    Parse *pParse,                  /* Parse context */
002531    Table *pTab,                    /* Table being updated */
002532    int iCur,                       /* Cursor number for table */
002533    int regData                     /* Data containing new record */
002534  ){
002535    Vdbe *v = pParse->pVdbe;
002536    int r = sqlite3GetTempReg(pParse);
002537    assert( !HasRowid(pTab) );
002538    assert( 0==(pParse->db->mDbFlags & DBFLAG_Vacuum) || CORRUPT_DB );
002539    sqlite3VdbeAddOp2(v, OP_Integer, 0, r);
002540    sqlite3VdbeAddOp4(v, OP_Insert, iCur, regData, r, (char*)pTab, P4_TABLE);
002541    sqlite3VdbeChangeP5(v, OPFLAG_ISNOOP);
002542    sqlite3ReleaseTempReg(pParse, r);
002543  }
002544  #else
002545  # define codeWithoutRowidPreupdate(a,b,c,d)
002546  #endif
002547  
002548  /*
002549  ** This routine generates code to finish the INSERT or UPDATE operation
002550  ** that was started by a prior call to sqlite3GenerateConstraintChecks.
002551  ** A consecutive range of registers starting at regNewData contains the
002552  ** rowid and the content to be inserted.
002553  **
002554  ** The arguments to this routine should be the same as the first six
002555  ** arguments to sqlite3GenerateConstraintChecks.
002556  */
002557  void sqlite3CompleteInsertion(
002558    Parse *pParse,      /* The parser context */
002559    Table *pTab,        /* the table into which we are inserting */
002560    int iDataCur,       /* Cursor of the canonical data source */
002561    int iIdxCur,        /* First index cursor */
002562    int regNewData,     /* Range of content */
002563    int *aRegIdx,       /* Register used by each index.  0 for unused indices */
002564    int update_flags,   /* True for UPDATE, False for INSERT */
002565    int appendBias,     /* True if this is likely to be an append */
002566    int useSeekResult   /* True to set the USESEEKRESULT flag on OP_[Idx]Insert */
002567  ){
002568    Vdbe *v;            /* Prepared statements under construction */
002569    Index *pIdx;        /* An index being inserted or updated */
002570    u8 pik_flags;       /* flag values passed to the btree insert */
002571    int i;              /* Loop counter */
002572  
002573    assert( update_flags==0
002574         || update_flags==OPFLAG_ISUPDATE
002575         || update_flags==(OPFLAG_ISUPDATE|OPFLAG_SAVEPOSITION)
002576    );
002577  
002578    v = pParse->pVdbe;
002579    assert( v!=0 );
002580    assert( !IsView(pTab) );  /* This table is not a VIEW */
002581    for(i=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){
002582      /* All REPLACE indexes are at the end of the list */
002583      assert( pIdx->onError!=OE_Replace
002584           || pIdx->pNext==0
002585           || pIdx->pNext->onError==OE_Replace );
002586      if( aRegIdx[i]==0 ) continue;
002587      if( pIdx->pPartIdxWhere ){
002588        sqlite3VdbeAddOp2(v, OP_IsNull, aRegIdx[i], sqlite3VdbeCurrentAddr(v)+2);
002589        VdbeCoverage(v);
002590      }
002591      pik_flags = (useSeekResult ? OPFLAG_USESEEKRESULT : 0);
002592      if( IsPrimaryKeyIndex(pIdx) && !HasRowid(pTab) ){
002593        pik_flags |= OPFLAG_NCHANGE;
002594        pik_flags |= (update_flags & OPFLAG_SAVEPOSITION);
002595        if( update_flags==0 ){
002596          codeWithoutRowidPreupdate(pParse, pTab, iIdxCur+i, aRegIdx[i]);
002597        }
002598      }
002599      sqlite3VdbeAddOp4Int(v, OP_IdxInsert, iIdxCur+i, aRegIdx[i],
002600                           aRegIdx[i]+1,
002601                           pIdx->uniqNotNull ? pIdx->nKeyCol: pIdx->nColumn);
002602      sqlite3VdbeChangeP5(v, pik_flags);
002603    }
002604    if( !HasRowid(pTab) ) return;
002605    if( pParse->nested ){
002606      pik_flags = 0;
002607    }else{
002608      pik_flags = OPFLAG_NCHANGE;
002609      pik_flags |= (update_flags?update_flags:OPFLAG_LASTROWID);
002610    }
002611    if( appendBias ){
002612      pik_flags |= OPFLAG_APPEND;
002613    }
002614    if( useSeekResult ){
002615      pik_flags |= OPFLAG_USESEEKRESULT;
002616    }
002617    sqlite3VdbeAddOp3(v, OP_Insert, iDataCur, aRegIdx[i], regNewData);
002618    if( !pParse->nested ){
002619      sqlite3VdbeAppendP4(v, pTab, P4_TABLE);
002620    }
002621    sqlite3VdbeChangeP5(v, pik_flags);
002622  }
002623  
002624  /*
002625  ** Allocate cursors for the pTab table and all its indices and generate
002626  ** code to open and initialized those cursors.
002627  **
002628  ** The cursor for the object that contains the complete data (normally
002629  ** the table itself, but the PRIMARY KEY index in the case of a WITHOUT
002630  ** ROWID table) is returned in *piDataCur.  The first index cursor is
002631  ** returned in *piIdxCur.  The number of indices is returned.
002632  **
002633  ** Use iBase as the first cursor (either the *piDataCur for rowid tables
002634  ** or the first index for WITHOUT ROWID tables) if it is non-negative.
002635  ** If iBase is negative, then allocate the next available cursor.
002636  **
002637  ** For a rowid table, *piDataCur will be exactly one less than *piIdxCur.
002638  ** For a WITHOUT ROWID table, *piDataCur will be somewhere in the range
002639  ** of *piIdxCurs, depending on where the PRIMARY KEY index appears on the
002640  ** pTab->pIndex list.
002641  **
002642  ** If pTab is a virtual table, then this routine is a no-op and the
002643  ** *piDataCur and *piIdxCur values are left uninitialized.
002644  */
002645  int sqlite3OpenTableAndIndices(
002646    Parse *pParse,   /* Parsing context */
002647    Table *pTab,     /* Table to be opened */
002648    int op,          /* OP_OpenRead or OP_OpenWrite */
002649    u8 p5,           /* P5 value for OP_Open* opcodes (except on WITHOUT ROWID) */
002650    int iBase,       /* Use this for the table cursor, if there is one */
002651    u8 *aToOpen,     /* If not NULL: boolean for each table and index */
002652    int *piDataCur,  /* Write the database source cursor number here */
002653    int *piIdxCur    /* Write the first index cursor number here */
002654  ){
002655    int i;
002656    int iDb;
002657    int iDataCur;
002658    Index *pIdx;
002659    Vdbe *v;
002660  
002661    assert( op==OP_OpenRead || op==OP_OpenWrite );
002662    assert( op==OP_OpenWrite || p5==0 );
002663    assert( piDataCur!=0 );
002664    assert( piIdxCur!=0 );
002665    if( IsVirtual(pTab) ){
002666      /* This routine is a no-op for virtual tables. Leave the output
002667      ** variables *piDataCur and *piIdxCur set to illegal cursor numbers
002668      ** for improved error detection. */
002669      *piDataCur = *piIdxCur = -999;
002670      return 0;
002671    }
002672    iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
002673    v = pParse->pVdbe;
002674    assert( v!=0 );
002675    if( iBase<0 ) iBase = pParse->nTab;
002676    iDataCur = iBase++;
002677    *piDataCur = iDataCur;
002678    if( HasRowid(pTab) && (aToOpen==0 || aToOpen[0]) ){
002679      sqlite3OpenTable(pParse, iDataCur, iDb, pTab, op);
002680    }else if( pParse->db->noSharedCache==0 ){
002681      sqlite3TableLock(pParse, iDb, pTab->tnum, op==OP_OpenWrite, pTab->zName);
002682    }
002683    *piIdxCur = iBase;
002684    for(i=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){
002685      int iIdxCur = iBase++;
002686      assert( pIdx->pSchema==pTab->pSchema );
002687      if( IsPrimaryKeyIndex(pIdx) && !HasRowid(pTab) ){
002688        *piDataCur = iIdxCur;
002689        p5 = 0;
002690      }
002691      if( aToOpen==0 || aToOpen[i+1] ){
002692        sqlite3VdbeAddOp3(v, op, iIdxCur, pIdx->tnum, iDb);
002693        sqlite3VdbeSetP4KeyInfo(pParse, pIdx);
002694        sqlite3VdbeChangeP5(v, p5);
002695        VdbeComment((v, "%s", pIdx->zName));
002696      }
002697    }
002698    if( iBase>pParse->nTab ) pParse->nTab = iBase;
002699    return i;
002700  }
002701  
002702  
002703  #ifdef SQLITE_TEST
002704  /*
002705  ** The following global variable is incremented whenever the
002706  ** transfer optimization is used.  This is used for testing
002707  ** purposes only - to make sure the transfer optimization really
002708  ** is happening when it is supposed to.
002709  */
002710  int sqlite3_xferopt_count;
002711  #endif /* SQLITE_TEST */
002712  
002713  
002714  #ifndef SQLITE_OMIT_XFER_OPT
002715  /*
002716  ** Check to see if index pSrc is compatible as a source of data
002717  ** for index pDest in an insert transfer optimization.  The rules
002718  ** for a compatible index:
002719  **
002720  **    *   The index is over the same set of columns
002721  **    *   The same DESC and ASC markings occurs on all columns
002722  **    *   The same onError processing (OE_Abort, OE_Ignore, etc)
002723  **    *   The same collating sequence on each column
002724  **    *   The index has the exact same WHERE clause
002725  */
002726  static int xferCompatibleIndex(Index *pDest, Index *pSrc){
002727    int i;
002728    assert( pDest && pSrc );
002729    assert( pDest->pTable!=pSrc->pTable );
002730    if( pDest->nKeyCol!=pSrc->nKeyCol || pDest->nColumn!=pSrc->nColumn ){
002731      return 0;   /* Different number of columns */
002732    }
002733    if( pDest->onError!=pSrc->onError ){
002734      return 0;   /* Different conflict resolution strategies */
002735    }
002736    for(i=0; i<pSrc->nKeyCol; i++){
002737      if( pSrc->aiColumn[i]!=pDest->aiColumn[i] ){
002738        return 0;   /* Different columns indexed */
002739      }
002740      if( pSrc->aiColumn[i]==XN_EXPR ){
002741        assert( pSrc->aColExpr!=0 && pDest->aColExpr!=0 );
002742        if( sqlite3ExprCompare(0, pSrc->aColExpr->a[i].pExpr,
002743                               pDest->aColExpr->a[i].pExpr, -1)!=0 ){
002744          return 0;   /* Different expressions in the index */
002745        }
002746      }
002747      if( pSrc->aSortOrder[i]!=pDest->aSortOrder[i] ){
002748        return 0;   /* Different sort orders */
002749      }
002750      if( sqlite3_stricmp(pSrc->azColl[i],pDest->azColl[i])!=0 ){
002751        return 0;   /* Different collating sequences */
002752      }
002753    }
002754    if( sqlite3ExprCompare(0, pSrc->pPartIdxWhere, pDest->pPartIdxWhere, -1) ){
002755      return 0;     /* Different WHERE clauses */
002756    }
002757  
002758    /* If no test above fails then the indices must be compatible */
002759    return 1;
002760  }
002761  
002762  /*
002763  ** Attempt the transfer optimization on INSERTs of the form
002764  **
002765  **     INSERT INTO tab1 SELECT * FROM tab2;
002766  **
002767  ** The xfer optimization transfers raw records from tab2 over to tab1. 
002768  ** Columns are not decoded and reassembled, which greatly improves
002769  ** performance.  Raw index records are transferred in the same way.
002770  **
002771  ** The xfer optimization is only attempted if tab1 and tab2 are compatible.
002772  ** There are lots of rules for determining compatibility - see comments
002773  ** embedded in the code for details.
002774  **
002775  ** This routine returns TRUE if the optimization is guaranteed to be used.
002776  ** Sometimes the xfer optimization will only work if the destination table
002777  ** is empty - a factor that can only be determined at run-time.  In that
002778  ** case, this routine generates code for the xfer optimization but also
002779  ** does a test to see if the destination table is empty and jumps over the
002780  ** xfer optimization code if the test fails.  In that case, this routine
002781  ** returns FALSE so that the caller will know to go ahead and generate
002782  ** an unoptimized transfer.  This routine also returns FALSE if there
002783  ** is no chance that the xfer optimization can be applied.
002784  **
002785  ** This optimization is particularly useful at making VACUUM run faster.
002786  */
002787  static int xferOptimization(
002788    Parse *pParse,        /* Parser context */
002789    Table *pDest,         /* The table we are inserting into */
002790    Select *pSelect,      /* A SELECT statement to use as the data source */
002791    int onError,          /* How to handle constraint errors */
002792    int iDbDest           /* The database of pDest */
002793  ){
002794    sqlite3 *db = pParse->db;
002795    ExprList *pEList;                /* The result set of the SELECT */
002796    Table *pSrc;                     /* The table in the FROM clause of SELECT */
002797    Index *pSrcIdx, *pDestIdx;       /* Source and destination indices */
002798    SrcItem *pItem;                  /* An element of pSelect->pSrc */
002799    int i;                           /* Loop counter */
002800    int iDbSrc;                      /* The database of pSrc */
002801    int iSrc, iDest;                 /* Cursors from source and destination */
002802    int addr1, addr2;                /* Loop addresses */
002803    int emptyDestTest = 0;           /* Address of test for empty pDest */
002804    int emptySrcTest = 0;            /* Address of test for empty pSrc */
002805    Vdbe *v;                         /* The VDBE we are building */
002806    int regAutoinc;                  /* Memory register used by AUTOINC */
002807    int destHasUniqueIdx = 0;        /* True if pDest has a UNIQUE index */
002808    int regData, regRowid;           /* Registers holding data and rowid */
002809  
002810    assert( pSelect!=0 );
002811    if( pParse->pWith || pSelect->pWith ){
002812      /* Do not attempt to process this query if there are an WITH clauses
002813      ** attached to it. Proceeding may generate a false "no such table: xxx"
002814      ** error if pSelect reads from a CTE named "xxx".  */
002815      return 0;
002816    }
002817  #ifndef SQLITE_OMIT_VIRTUALTABLE
002818    if( IsVirtual(pDest) ){
002819      return 0;   /* tab1 must not be a virtual table */
002820    }
002821  #endif
002822    if( onError==OE_Default ){
002823      if( pDest->iPKey>=0 ) onError = pDest->keyConf;
002824      if( onError==OE_Default ) onError = OE_Abort;
002825    }
002826    assert(pSelect->pSrc);   /* allocated even if there is no FROM clause */
002827    if( pSelect->pSrc->nSrc!=1 ){
002828      return 0;   /* FROM clause must have exactly one term */
002829    }
002830    if( pSelect->pSrc->a[0].pSelect ){
002831      return 0;   /* FROM clause cannot contain a subquery */
002832    }
002833    if( pSelect->pWhere ){
002834      return 0;   /* SELECT may not have a WHERE clause */
002835    }
002836    if( pSelect->pOrderBy ){
002837      return 0;   /* SELECT may not have an ORDER BY clause */
002838    }
002839    /* Do not need to test for a HAVING clause.  If HAVING is present but
002840    ** there is no ORDER BY, we will get an error. */
002841    if( pSelect->pGroupBy ){
002842      return 0;   /* SELECT may not have a GROUP BY clause */
002843    }
002844    if( pSelect->pLimit ){
002845      return 0;   /* SELECT may not have a LIMIT clause */
002846    }
002847    if( pSelect->pPrior ){
002848      return 0;   /* SELECT may not be a compound query */
002849    }
002850    if( pSelect->selFlags & SF_Distinct ){
002851      return 0;   /* SELECT may not be DISTINCT */
002852    }
002853    pEList = pSelect->pEList;
002854    assert( pEList!=0 );
002855    if( pEList->nExpr!=1 ){
002856      return 0;   /* The result set must have exactly one column */
002857    }
002858    assert( pEList->a[0].pExpr );
002859    if( pEList->a[0].pExpr->op!=TK_ASTERISK ){
002860      return 0;   /* The result set must be the special operator "*" */
002861    }
002862  
002863    /* At this point we have established that the statement is of the
002864    ** correct syntactic form to participate in this optimization.  Now
002865    ** we have to check the semantics.
002866    */
002867    pItem = pSelect->pSrc->a;
002868    pSrc = sqlite3LocateTableItem(pParse, 0, pItem);
002869    if( pSrc==0 ){
002870      return 0;   /* FROM clause does not contain a real table */
002871    }
002872    if( pSrc->tnum==pDest->tnum && pSrc->pSchema==pDest->pSchema ){
002873      testcase( pSrc!=pDest ); /* Possible due to bad sqlite_schema.rootpage */
002874      return 0;   /* tab1 and tab2 may not be the same table */
002875    }
002876    if( HasRowid(pDest)!=HasRowid(pSrc) ){
002877      return 0;   /* source and destination must both be WITHOUT ROWID or not */
002878    }
002879    if( !IsOrdinaryTable(pSrc) ){
002880      return 0;   /* tab2 may not be a view or virtual table */
002881    }
002882    if( pDest->nCol!=pSrc->nCol ){
002883      return 0;   /* Number of columns must be the same in tab1 and tab2 */
002884    }
002885    if( pDest->iPKey!=pSrc->iPKey ){
002886      return 0;   /* Both tables must have the same INTEGER PRIMARY KEY */
002887    }
002888    if( (pDest->tabFlags & TF_Strict)!=0 && (pSrc->tabFlags & TF_Strict)==0 ){
002889      return 0;   /* Cannot feed from a non-strict into a strict table */
002890    }
002891    for(i=0; i<pDest->nCol; i++){
002892      Column *pDestCol = &pDest->aCol[i];
002893      Column *pSrcCol = &pSrc->aCol[i];
002894  #ifdef SQLITE_ENABLE_HIDDEN_COLUMNS
002895      if( (db->mDbFlags & DBFLAG_Vacuum)==0
002896       && (pDestCol->colFlags | pSrcCol->colFlags) & COLFLAG_HIDDEN
002897      ){
002898        return 0;    /* Neither table may have __hidden__ columns */
002899      }
002900  #endif
002901  #ifndef SQLITE_OMIT_GENERATED_COLUMNS
002902      /* Even if tables t1 and t2 have identical schemas, if they contain
002903      ** generated columns, then this statement is semantically incorrect:
002904      **
002905      **     INSERT INTO t2 SELECT * FROM t1;
002906      **
002907      ** The reason is that generated column values are returned by the
002908      ** the SELECT statement on the right but the INSERT statement on the
002909      ** left wants them to be omitted.
002910      **
002911      ** Nevertheless, this is a useful notational shorthand to tell SQLite
002912      ** to do a bulk transfer all of the content from t1 over to t2.
002913      **
002914      ** We could, in theory, disable this (except for internal use by the
002915      ** VACUUM command where it is actually needed).  But why do that?  It
002916      ** seems harmless enough, and provides a useful service.
002917      */
002918      if( (pDestCol->colFlags & COLFLAG_GENERATED) !=
002919          (pSrcCol->colFlags & COLFLAG_GENERATED) ){
002920        return 0;    /* Both columns have the same generated-column type */
002921      }
002922      /* But the transfer is only allowed if both the source and destination
002923      ** tables have the exact same expressions for generated columns.
002924      ** This requirement could be relaxed for VIRTUAL columns, I suppose.
002925      */
002926      if( (pDestCol->colFlags & COLFLAG_GENERATED)!=0 ){
002927        if( sqlite3ExprCompare(0,
002928               sqlite3ColumnExpr(pSrc, pSrcCol),
002929               sqlite3ColumnExpr(pDest, pDestCol), -1)!=0 ){
002930          testcase( pDestCol->colFlags & COLFLAG_VIRTUAL );
002931          testcase( pDestCol->colFlags & COLFLAG_STORED );
002932          return 0;  /* Different generator expressions */
002933        }
002934      }
002935  #endif
002936      if( pDestCol->affinity!=pSrcCol->affinity ){
002937        return 0;    /* Affinity must be the same on all columns */
002938      }
002939      if( sqlite3_stricmp(sqlite3ColumnColl(pDestCol),
002940                          sqlite3ColumnColl(pSrcCol))!=0 ){
002941        return 0;    /* Collating sequence must be the same on all columns */
002942      }
002943      if( pDestCol->notNull && !pSrcCol->notNull ){
002944        return 0;    /* tab2 must be NOT NULL if tab1 is */
002945      }
002946      /* Default values for second and subsequent columns need to match. */
002947      if( (pDestCol->colFlags & COLFLAG_GENERATED)==0 && i>0 ){
002948        Expr *pDestExpr = sqlite3ColumnExpr(pDest, pDestCol);
002949        Expr *pSrcExpr = sqlite3ColumnExpr(pSrc, pSrcCol);
002950        assert( pDestExpr==0 || pDestExpr->op==TK_SPAN );
002951        assert( pDestExpr==0 || !ExprHasProperty(pDestExpr, EP_IntValue) );
002952        assert( pSrcExpr==0 || pSrcExpr->op==TK_SPAN );
002953        assert( pSrcExpr==0 || !ExprHasProperty(pSrcExpr, EP_IntValue) );
002954        if( (pDestExpr==0)!=(pSrcExpr==0)
002955         || (pDestExpr!=0 && strcmp(pDestExpr->u.zToken,
002956                                         pSrcExpr->u.zToken)!=0)
002957        ){
002958          return 0;    /* Default values must be the same for all columns */
002959        }
002960      }
002961    }
002962    for(pDestIdx=pDest->pIndex; pDestIdx; pDestIdx=pDestIdx->pNext){
002963      if( IsUniqueIndex(pDestIdx) ){
002964        destHasUniqueIdx = 1;
002965      }
002966      for(pSrcIdx=pSrc->pIndex; pSrcIdx; pSrcIdx=pSrcIdx->pNext){
002967        if( xferCompatibleIndex(pDestIdx, pSrcIdx) ) break;
002968      }
002969      if( pSrcIdx==0 ){
002970        return 0;    /* pDestIdx has no corresponding index in pSrc */
002971      }
002972      if( pSrcIdx->tnum==pDestIdx->tnum && pSrc->pSchema==pDest->pSchema
002973           && sqlite3FaultSim(411)==SQLITE_OK ){
002974        /* The sqlite3FaultSim() call allows this corruption test to be
002975        ** bypassed during testing, in order to exercise other corruption tests
002976        ** further downstream. */
002977        return 0;   /* Corrupt schema - two indexes on the same btree */
002978      }
002979    }
002980  #ifndef SQLITE_OMIT_CHECK
002981    if( pDest->pCheck && sqlite3ExprListCompare(pSrc->pCheck,pDest->pCheck,-1) ){
002982      return 0;   /* Tables have different CHECK constraints.  Ticket #2252 */
002983    }
002984  #endif
002985  #ifndef SQLITE_OMIT_FOREIGN_KEY
002986    /* Disallow the transfer optimization if the destination table contains
002987    ** any foreign key constraints.  This is more restrictive than necessary.
002988    ** But the main beneficiary of the transfer optimization is the VACUUM
002989    ** command, and the VACUUM command disables foreign key constraints.  So
002990    ** the extra complication to make this rule less restrictive is probably
002991    ** not worth the effort.  Ticket [6284df89debdfa61db8073e062908af0c9b6118e]
002992    */
002993    assert( IsOrdinaryTable(pDest) );
002994    if( (db->flags & SQLITE_ForeignKeys)!=0 && pDest->u.tab.pFKey!=0 ){
002995      return 0;
002996    }
002997  #endif
002998    if( (db->flags & SQLITE_CountRows)!=0 ){
002999      return 0;  /* xfer opt does not play well with PRAGMA count_changes */
003000    }
003001  
003002    /* If we get this far, it means that the xfer optimization is at
003003    ** least a possibility, though it might only work if the destination
003004    ** table (tab1) is initially empty.
003005    */
003006  #ifdef SQLITE_TEST
003007    sqlite3_xferopt_count++;
003008  #endif
003009    iDbSrc = sqlite3SchemaToIndex(db, pSrc->pSchema);
003010    v = sqlite3GetVdbe(pParse);
003011    sqlite3CodeVerifySchema(pParse, iDbSrc);
003012    iSrc = pParse->nTab++;
003013    iDest = pParse->nTab++;
003014    regAutoinc = autoIncBegin(pParse, iDbDest, pDest);
003015    regData = sqlite3GetTempReg(pParse);
003016    sqlite3VdbeAddOp2(v, OP_Null, 0, regData);
003017    regRowid = sqlite3GetTempReg(pParse);
003018    sqlite3OpenTable(pParse, iDest, iDbDest, pDest, OP_OpenWrite);
003019    assert( HasRowid(pDest) || destHasUniqueIdx );
003020    if( (db->mDbFlags & DBFLAG_Vacuum)==0 && (
003021        (pDest->iPKey<0 && pDest->pIndex!=0)          /* (1) */
003022     || destHasUniqueIdx                              /* (2) */
003023     || (onError!=OE_Abort && onError!=OE_Rollback)   /* (3) */
003024    )){
003025      /* In some circumstances, we are able to run the xfer optimization
003026      ** only if the destination table is initially empty. Unless the
003027      ** DBFLAG_Vacuum flag is set, this block generates code to make
003028      ** that determination. If DBFLAG_Vacuum is set, then the destination
003029      ** table is always empty.
003030      **
003031      ** Conditions under which the destination must be empty:
003032      **
003033      ** (1) There is no INTEGER PRIMARY KEY but there are indices.
003034      **     (If the destination is not initially empty, the rowid fields
003035      **     of index entries might need to change.)
003036      **
003037      ** (2) The destination has a unique index.  (The xfer optimization
003038      **     is unable to test uniqueness.)
003039      **
003040      ** (3) onError is something other than OE_Abort and OE_Rollback.
003041      */
003042      addr1 = sqlite3VdbeAddOp2(v, OP_Rewind, iDest, 0); VdbeCoverage(v);
003043      emptyDestTest = sqlite3VdbeAddOp0(v, OP_Goto);
003044      sqlite3VdbeJumpHere(v, addr1);
003045    }
003046    if( HasRowid(pSrc) ){
003047      u8 insFlags;
003048      sqlite3OpenTable(pParse, iSrc, iDbSrc, pSrc, OP_OpenRead);
003049      emptySrcTest = sqlite3VdbeAddOp2(v, OP_Rewind, iSrc, 0); VdbeCoverage(v);
003050      if( pDest->iPKey>=0 ){
003051        addr1 = sqlite3VdbeAddOp2(v, OP_Rowid, iSrc, regRowid);
003052        if( (db->mDbFlags & DBFLAG_Vacuum)==0 ){
003053          sqlite3VdbeVerifyAbortable(v, onError);
003054          addr2 = sqlite3VdbeAddOp3(v, OP_NotExists, iDest, 0, regRowid);
003055          VdbeCoverage(v);
003056          sqlite3RowidConstraint(pParse, onError, pDest);
003057          sqlite3VdbeJumpHere(v, addr2);
003058        }
003059        autoIncStep(pParse, regAutoinc, regRowid);
003060      }else if( pDest->pIndex==0 && !(db->mDbFlags & DBFLAG_VacuumInto) ){
003061        addr1 = sqlite3VdbeAddOp2(v, OP_NewRowid, iDest, regRowid);
003062      }else{
003063        addr1 = sqlite3VdbeAddOp2(v, OP_Rowid, iSrc, regRowid);
003064        assert( (pDest->tabFlags & TF_Autoincrement)==0 );
003065      }
003066  
003067      if( db->mDbFlags & DBFLAG_Vacuum ){
003068        sqlite3VdbeAddOp1(v, OP_SeekEnd, iDest);
003069        insFlags = OPFLAG_APPEND|OPFLAG_USESEEKRESULT|OPFLAG_PREFORMAT;
003070      }else{
003071        insFlags = OPFLAG_NCHANGE|OPFLAG_LASTROWID|OPFLAG_APPEND|OPFLAG_PREFORMAT;
003072      }
003073  #ifdef SQLITE_ENABLE_PREUPDATE_HOOK
003074      if( (db->mDbFlags & DBFLAG_Vacuum)==0 ){
003075        sqlite3VdbeAddOp3(v, OP_RowData, iSrc, regData, 1);
003076        insFlags &= ~OPFLAG_PREFORMAT;
003077      }else
003078  #endif
003079      {
003080        sqlite3VdbeAddOp3(v, OP_RowCell, iDest, iSrc, regRowid);
003081      }
003082      sqlite3VdbeAddOp3(v, OP_Insert, iDest, regData, regRowid);
003083      if( (db->mDbFlags & DBFLAG_Vacuum)==0 ){
003084        sqlite3VdbeChangeP4(v, -1, (char*)pDest, P4_TABLE);
003085      }
003086      sqlite3VdbeChangeP5(v, insFlags);
003087  
003088      sqlite3VdbeAddOp2(v, OP_Next, iSrc, addr1); VdbeCoverage(v);
003089      sqlite3VdbeAddOp2(v, OP_Close, iSrc, 0);
003090      sqlite3VdbeAddOp2(v, OP_Close, iDest, 0);
003091    }else{
003092      sqlite3TableLock(pParse, iDbDest, pDest->tnum, 1, pDest->zName);
003093      sqlite3TableLock(pParse, iDbSrc, pSrc->tnum, 0, pSrc->zName);
003094    }
003095    for(pDestIdx=pDest->pIndex; pDestIdx; pDestIdx=pDestIdx->pNext){
003096      u8 idxInsFlags = 0;
003097      for(pSrcIdx=pSrc->pIndex; ALWAYS(pSrcIdx); pSrcIdx=pSrcIdx->pNext){
003098        if( xferCompatibleIndex(pDestIdx, pSrcIdx) ) break;
003099      }
003100      assert( pSrcIdx );
003101      sqlite3VdbeAddOp3(v, OP_OpenRead, iSrc, pSrcIdx->tnum, iDbSrc);
003102      sqlite3VdbeSetP4KeyInfo(pParse, pSrcIdx);
003103      VdbeComment((v, "%s", pSrcIdx->zName));
003104      sqlite3VdbeAddOp3(v, OP_OpenWrite, iDest, pDestIdx->tnum, iDbDest);
003105      sqlite3VdbeSetP4KeyInfo(pParse, pDestIdx);
003106      sqlite3VdbeChangeP5(v, OPFLAG_BULKCSR);
003107      VdbeComment((v, "%s", pDestIdx->zName));
003108      addr1 = sqlite3VdbeAddOp2(v, OP_Rewind, iSrc, 0); VdbeCoverage(v);
003109      if( db->mDbFlags & DBFLAG_Vacuum ){
003110        /* This INSERT command is part of a VACUUM operation, which guarantees
003111        ** that the destination table is empty. If all indexed columns use
003112        ** collation sequence BINARY, then it can also be assumed that the
003113        ** index will be populated by inserting keys in strictly sorted
003114        ** order. In this case, instead of seeking within the b-tree as part
003115        ** of every OP_IdxInsert opcode, an OP_SeekEnd is added before the
003116        ** OP_IdxInsert to seek to the point within the b-tree where each key
003117        ** should be inserted. This is faster.
003118        **
003119        ** If any of the indexed columns use a collation sequence other than
003120        ** BINARY, this optimization is disabled. This is because the user
003121        ** might change the definition of a collation sequence and then run
003122        ** a VACUUM command. In that case keys may not be written in strictly
003123        ** sorted order.  */
003124        for(i=0; i<pSrcIdx->nColumn; i++){
003125          const char *zColl = pSrcIdx->azColl[i];
003126          if( sqlite3_stricmp(sqlite3StrBINARY, zColl) ) break;
003127        }
003128        if( i==pSrcIdx->nColumn ){
003129          idxInsFlags = OPFLAG_USESEEKRESULT|OPFLAG_PREFORMAT;
003130          sqlite3VdbeAddOp1(v, OP_SeekEnd, iDest);
003131          sqlite3VdbeAddOp2(v, OP_RowCell, iDest, iSrc);
003132        }
003133      }else if( !HasRowid(pSrc) && pDestIdx->idxType==SQLITE_IDXTYPE_PRIMARYKEY ){
003134        idxInsFlags |= OPFLAG_NCHANGE;
003135      }
003136      if( idxInsFlags!=(OPFLAG_USESEEKRESULT|OPFLAG_PREFORMAT) ){
003137        sqlite3VdbeAddOp3(v, OP_RowData, iSrc, regData, 1);
003138        if( (db->mDbFlags & DBFLAG_Vacuum)==0
003139         && !HasRowid(pDest)
003140         && IsPrimaryKeyIndex(pDestIdx)
003141        ){
003142          codeWithoutRowidPreupdate(pParse, pDest, iDest, regData);
003143        }
003144      }
003145      sqlite3VdbeAddOp2(v, OP_IdxInsert, iDest, regData);
003146      sqlite3VdbeChangeP5(v, idxInsFlags|OPFLAG_APPEND);
003147      sqlite3VdbeAddOp2(v, OP_Next, iSrc, addr1+1); VdbeCoverage(v);
003148      sqlite3VdbeJumpHere(v, addr1);
003149      sqlite3VdbeAddOp2(v, OP_Close, iSrc, 0);
003150      sqlite3VdbeAddOp2(v, OP_Close, iDest, 0);
003151    }
003152    if( emptySrcTest ) sqlite3VdbeJumpHere(v, emptySrcTest);
003153    sqlite3ReleaseTempReg(pParse, regRowid);
003154    sqlite3ReleaseTempReg(pParse, regData);
003155    if( emptyDestTest ){
003156      sqlite3AutoincrementEnd(pParse);
003157      sqlite3VdbeAddOp2(v, OP_Halt, SQLITE_OK, 0);
003158      sqlite3VdbeJumpHere(v, emptyDestTest);
003159      sqlite3VdbeAddOp2(v, OP_Close, iDest, 0);
003160      return 0;
003161    }else{
003162      return 1;
003163    }
003164  }
003165  #endif /* SQLITE_OMIT_XFER_OPT */