1cce7d176Sdrh /* 2b19a2bc6Sdrh ** 2001 September 15 3cce7d176Sdrh ** 4b19a2bc6Sdrh ** The author disclaims copyright to this source code. In place of 5b19a2bc6Sdrh ** a legal notice, here is a blessing: 6cce7d176Sdrh ** 7b19a2bc6Sdrh ** May you do good and not evil. 8b19a2bc6Sdrh ** May you find forgiveness for yourself and forgive others. 9b19a2bc6Sdrh ** May you share freely, never taking more than you give. 10cce7d176Sdrh ** 11cce7d176Sdrh ************************************************************************* 12cce7d176Sdrh ** This file contains C code routines that are called by the parser 13b19a2bc6Sdrh ** to handle INSERT statements in SQLite. 14cce7d176Sdrh */ 15cce7d176Sdrh #include "sqliteInt.h" 16cce7d176Sdrh 17cce7d176Sdrh /* 18bbb5e4e0Sdrh ** Generate code that will open a table for reading. 19bbb5e4e0Sdrh */ 20bbb5e4e0Sdrh void sqlite3OpenTable( 21bbb5e4e0Sdrh Parse *p, /* Generate code into this VDBE */ 22bbb5e4e0Sdrh int iCur, /* The cursor number of the table */ 23bbb5e4e0Sdrh int iDb, /* The database index in sqlite3.aDb[] */ 24bbb5e4e0Sdrh Table *pTab, /* The table to be opened */ 25bbb5e4e0Sdrh int opcode /* OP_OpenRead or OP_OpenWrite */ 26bbb5e4e0Sdrh ){ 27bbb5e4e0Sdrh Vdbe *v; 28bbb5e4e0Sdrh if( IsVirtual(pTab) ) return; 29bbb5e4e0Sdrh v = sqlite3GetVdbe(p); 30bbb5e4e0Sdrh assert( opcode==OP_OpenWrite || opcode==OP_OpenRead ); 31bbb5e4e0Sdrh sqlite3TableLock(p, iDb, pTab->tnum, (opcode==OP_OpenWrite)?1:0, pTab->zName); 32bbb5e4e0Sdrh sqlite3VdbeAddOp3(v, opcode, iCur, pTab->tnum, iDb); 33bbb5e4e0Sdrh sqlite3VdbeChangeP4(v, -1, SQLITE_INT_TO_PTR(pTab->nCol), P4_INT32); 34bbb5e4e0Sdrh VdbeComment((v, "%s", pTab->zName)); 35bbb5e4e0Sdrh } 36bbb5e4e0Sdrh 37bbb5e4e0Sdrh /* 3869f8bb9cSdan ** Return a pointer to the column affinity string associated with index 3969f8bb9cSdan ** pIdx. A column affinity string has one character for each column in 4069f8bb9cSdan ** the table, according to the affinity of the column: 413d1bfeaaSdanielk1977 ** 423d1bfeaaSdanielk1977 ** Character Column affinity 433d1bfeaaSdanielk1977 ** ------------------------------ 443eda040bSdrh ** 'a' TEXT 453eda040bSdrh ** 'b' NONE 463eda040bSdrh ** 'c' NUMERIC 473eda040bSdrh ** 'd' INTEGER 483eda040bSdrh ** 'e' REAL 492d401ab8Sdrh ** 502d401ab8Sdrh ** An extra 'b' is appended to the end of the string to cover the 512d401ab8Sdrh ** rowid that appears as the last column in every index. 5269f8bb9cSdan ** 5369f8bb9cSdan ** Memory for the buffer containing the column index affinity string 5469f8bb9cSdan ** is managed along with the rest of the Index structure. It will be 5569f8bb9cSdan ** released when sqlite3DeleteIndex() is called. 563d1bfeaaSdanielk1977 */ 5769f8bb9cSdan const char *sqlite3IndexAffinityStr(Vdbe *v, Index *pIdx){ 58a37cdde0Sdanielk1977 if( !pIdx->zColAff ){ 59e014a838Sdanielk1977 /* The first time a column affinity string for a particular index is 60a37cdde0Sdanielk1977 ** required, it is allocated and populated here. It is then stored as 61e014a838Sdanielk1977 ** a member of the Index structure for subsequent use. 62a37cdde0Sdanielk1977 ** 63a37cdde0Sdanielk1977 ** The column affinity string will eventually be deleted by 64e014a838Sdanielk1977 ** sqliteDeleteIndex() when the Index structure itself is cleaned 65a37cdde0Sdanielk1977 ** up. 66a37cdde0Sdanielk1977 */ 67a37cdde0Sdanielk1977 int n; 68a37cdde0Sdanielk1977 Table *pTab = pIdx->pTable; 69abb6fcabSdrh sqlite3 *db = sqlite3VdbeDb(v); 70*b975598eSdrh pIdx->zColAff = (char *)sqlite3DbMallocRaw(0, pIdx->nColumn+2); 71a37cdde0Sdanielk1977 if( !pIdx->zColAff ){ 72633e6d57Sdrh db->mallocFailed = 1; 7369f8bb9cSdan return 0; 74a37cdde0Sdanielk1977 } 75a37cdde0Sdanielk1977 for(n=0; n<pIdx->nColumn; n++){ 76a37cdde0Sdanielk1977 pIdx->zColAff[n] = pTab->aCol[pIdx->aiColumn[n]].affinity; 77a37cdde0Sdanielk1977 } 782d401ab8Sdrh pIdx->zColAff[n++] = SQLITE_AFF_NONE; 792d401ab8Sdrh pIdx->zColAff[n] = 0; 80a37cdde0Sdanielk1977 } 813d1bfeaaSdanielk1977 8269f8bb9cSdan return pIdx->zColAff; 83a37cdde0Sdanielk1977 } 84a37cdde0Sdanielk1977 85a37cdde0Sdanielk1977 /* 8666a5167bSdrh ** Set P4 of the most recently inserted opcode to a column affinity 87a37cdde0Sdanielk1977 ** string for table pTab. A column affinity string has one character 88a37cdde0Sdanielk1977 ** for each column indexed by the index, according to the affinity of the 89a37cdde0Sdanielk1977 ** column: 90a37cdde0Sdanielk1977 ** 91a37cdde0Sdanielk1977 ** Character Column affinity 92a37cdde0Sdanielk1977 ** ------------------------------ 933eda040bSdrh ** 'a' TEXT 943eda040bSdrh ** 'b' NONE 953eda040bSdrh ** 'c' NUMERIC 963eda040bSdrh ** 'd' INTEGER 973eda040bSdrh ** 'e' REAL 98a37cdde0Sdanielk1977 */ 99a37cdde0Sdanielk1977 void sqlite3TableAffinityStr(Vdbe *v, Table *pTab){ 1003d1bfeaaSdanielk1977 /* The first time a column affinity string for a particular table 1013d1bfeaaSdanielk1977 ** is required, it is allocated and populated here. It is then 1023d1bfeaaSdanielk1977 ** stored as a member of the Table structure for subsequent use. 1033d1bfeaaSdanielk1977 ** 1043d1bfeaaSdanielk1977 ** The column affinity string will eventually be deleted by 1053d1bfeaaSdanielk1977 ** sqlite3DeleteTable() when the Table structure itself is cleaned up. 1063d1bfeaaSdanielk1977 */ 1073d1bfeaaSdanielk1977 if( !pTab->zColAff ){ 1083d1bfeaaSdanielk1977 char *zColAff; 1093d1bfeaaSdanielk1977 int i; 110abb6fcabSdrh sqlite3 *db = sqlite3VdbeDb(v); 1113d1bfeaaSdanielk1977 112*b975598eSdrh zColAff = (char *)sqlite3DbMallocRaw(0, pTab->nCol+1); 1133d1bfeaaSdanielk1977 if( !zColAff ){ 114633e6d57Sdrh db->mallocFailed = 1; 115a37cdde0Sdanielk1977 return; 1163d1bfeaaSdanielk1977 } 1173d1bfeaaSdanielk1977 1183d1bfeaaSdanielk1977 for(i=0; i<pTab->nCol; i++){ 119a37cdde0Sdanielk1977 zColAff[i] = pTab->aCol[i].affinity; 1203d1bfeaaSdanielk1977 } 1213d1bfeaaSdanielk1977 zColAff[pTab->nCol] = '\0'; 1223d1bfeaaSdanielk1977 1233d1bfeaaSdanielk1977 pTab->zColAff = zColAff; 1243d1bfeaaSdanielk1977 } 1253d1bfeaaSdanielk1977 12666a5167bSdrh sqlite3VdbeChangeP4(v, -1, pTab->zColAff, 0); 1273d1bfeaaSdanielk1977 } 1283d1bfeaaSdanielk1977 1294d88778bSdanielk1977 /* 13048d1178aSdrh ** Return non-zero if the table pTab in database iDb or any of its indices 13148d1178aSdrh ** have been opened at any point in the VDBE program beginning at location 13248d1178aSdrh ** iStartAddr throught the end of the program. This is used to see if 13348d1178aSdrh ** a statement of the form "INSERT INTO <iDb, pTab> SELECT ..." can 13448d1178aSdrh ** run without using temporary table for the results of the SELECT. 1354d88778bSdanielk1977 */ 136595a523aSdanielk1977 static int readsTable(Parse *p, int iStartAddr, int iDb, Table *pTab){ 137595a523aSdanielk1977 Vdbe *v = sqlite3GetVdbe(p); 1384d88778bSdanielk1977 int i; 13948d1178aSdrh int iEnd = sqlite3VdbeCurrentAddr(v); 140595a523aSdanielk1977 #ifndef SQLITE_OMIT_VIRTUALTABLE 141595a523aSdanielk1977 VTable *pVTab = IsVirtual(pTab) ? sqlite3GetVTable(p->db, pTab) : 0; 142595a523aSdanielk1977 #endif 143595a523aSdanielk1977 14448d1178aSdrh for(i=iStartAddr; i<iEnd; i++){ 14548d1178aSdrh VdbeOp *pOp = sqlite3VdbeGetOp(v, i); 146ef0bea92Sdrh assert( pOp!=0 ); 147207872a4Sdanielk1977 if( pOp->opcode==OP_OpenRead && pOp->p3==iDb ){ 14848d1178aSdrh Index *pIndex; 149207872a4Sdanielk1977 int tnum = pOp->p2; 15048d1178aSdrh if( tnum==pTab->tnum ){ 15148d1178aSdrh return 1; 15248d1178aSdrh } 15348d1178aSdrh for(pIndex=pTab->pIndex; pIndex; pIndex=pIndex->pNext){ 15448d1178aSdrh if( tnum==pIndex->tnum ){ 15548d1178aSdrh return 1; 15648d1178aSdrh } 15748d1178aSdrh } 15848d1178aSdrh } 159543165efSdrh #ifndef SQLITE_OMIT_VIRTUALTABLE 160595a523aSdanielk1977 if( pOp->opcode==OP_VOpen && pOp->p4.pVtab==pVTab ){ 1612dca4ac1Sdanielk1977 assert( pOp->p4.pVtab!=0 ); 16266a5167bSdrh assert( pOp->p4type==P4_VTAB ); 16348d1178aSdrh return 1; 1644d88778bSdanielk1977 } 165543165efSdrh #endif 1664d88778bSdanielk1977 } 1674d88778bSdanielk1977 return 0; 1684d88778bSdanielk1977 } 1693d1bfeaaSdanielk1977 1709d9cf229Sdrh #ifndef SQLITE_OMIT_AUTOINCREMENT 1719d9cf229Sdrh /* 1720b9f50d8Sdrh ** Locate or create an AutoincInfo structure associated with table pTab 1730b9f50d8Sdrh ** which is in database iDb. Return the register number for the register 1740b9f50d8Sdrh ** that holds the maximum rowid. 1759d9cf229Sdrh ** 1760b9f50d8Sdrh ** There is at most one AutoincInfo structure per table even if the 1770b9f50d8Sdrh ** same table is autoincremented multiple times due to inserts within 1780b9f50d8Sdrh ** triggers. A new AutoincInfo structure is created if this is the 1790b9f50d8Sdrh ** first use of table pTab. On 2nd and subsequent uses, the original 1800b9f50d8Sdrh ** AutoincInfo structure is used. 1819d9cf229Sdrh ** 1820b9f50d8Sdrh ** Three memory locations are allocated: 1830b9f50d8Sdrh ** 1840b9f50d8Sdrh ** (1) Register to hold the name of the pTab table. 1850b9f50d8Sdrh ** (2) Register to hold the maximum ROWID of pTab. 1860b9f50d8Sdrh ** (3) Register to hold the rowid in sqlite_sequence of pTab 1870b9f50d8Sdrh ** 1880b9f50d8Sdrh ** The 2nd register is the one that is returned. That is all the 1890b9f50d8Sdrh ** insert routine needs to know about. 1909d9cf229Sdrh */ 1919d9cf229Sdrh static int autoIncBegin( 1929d9cf229Sdrh Parse *pParse, /* Parsing context */ 1939d9cf229Sdrh int iDb, /* Index of the database holding pTab */ 1949d9cf229Sdrh Table *pTab /* The table we are writing to */ 1959d9cf229Sdrh ){ 1966a288a33Sdrh int memId = 0; /* Register holding maximum rowid */ 1977d10d5a6Sdrh if( pTab->tabFlags & TF_Autoincrement ){ 19865a7cd16Sdan Parse *pToplevel = sqlite3ParseToplevel(pParse); 1990b9f50d8Sdrh AutoincInfo *pInfo; 2000b9f50d8Sdrh 20165a7cd16Sdan pInfo = pToplevel->pAinc; 2020b9f50d8Sdrh while( pInfo && pInfo->pTab!=pTab ){ pInfo = pInfo->pNext; } 2030b9f50d8Sdrh if( pInfo==0 ){ 2040b9f50d8Sdrh pInfo = sqlite3DbMallocRaw(pParse->db, sizeof(*pInfo)); 2050b9f50d8Sdrh if( pInfo==0 ) return 0; 20665a7cd16Sdan pInfo->pNext = pToplevel->pAinc; 20765a7cd16Sdan pToplevel->pAinc = pInfo; 2080b9f50d8Sdrh pInfo->pTab = pTab; 2090b9f50d8Sdrh pInfo->iDb = iDb; 21065a7cd16Sdan pToplevel->nMem++; /* Register to hold name of table */ 21165a7cd16Sdan pInfo->regCtr = ++pToplevel->nMem; /* Max rowid register */ 21265a7cd16Sdan pToplevel->nMem++; /* Rowid in sqlite_sequence */ 2130b9f50d8Sdrh } 2140b9f50d8Sdrh memId = pInfo->regCtr; 2159d9cf229Sdrh } 2169d9cf229Sdrh return memId; 2179d9cf229Sdrh } 2189d9cf229Sdrh 2199d9cf229Sdrh /* 2200b9f50d8Sdrh ** This routine generates code that will initialize all of the 2210b9f50d8Sdrh ** register used by the autoincrement tracker. 2220b9f50d8Sdrh */ 2230b9f50d8Sdrh void sqlite3AutoincrementBegin(Parse *pParse){ 2240b9f50d8Sdrh AutoincInfo *p; /* Information about an AUTOINCREMENT */ 2250b9f50d8Sdrh sqlite3 *db = pParse->db; /* The database connection */ 2260b9f50d8Sdrh Db *pDb; /* Database only autoinc table */ 2270b9f50d8Sdrh int memId; /* Register holding max rowid */ 2280b9f50d8Sdrh int addr; /* A VDBE address */ 2290b9f50d8Sdrh Vdbe *v = pParse->pVdbe; /* VDBE under construction */ 2300b9f50d8Sdrh 231345ba7dbSdrh /* This routine is never called during trigger-generation. It is 232345ba7dbSdrh ** only called from the top-level */ 233345ba7dbSdrh assert( pParse->pTriggerTab==0 ); 234345ba7dbSdrh assert( pParse==sqlite3ParseToplevel(pParse) ); 23576d462eeSdan 2360b9f50d8Sdrh assert( v ); /* We failed long ago if this is not so */ 2370b9f50d8Sdrh for(p = pParse->pAinc; p; p = p->pNext){ 2380b9f50d8Sdrh pDb = &db->aDb[p->iDb]; 2390b9f50d8Sdrh memId = p->regCtr; 2400b9f50d8Sdrh sqlite3OpenTable(pParse, 0, p->iDb, pDb->pSchema->pSeqTab, OP_OpenRead); 2410b9f50d8Sdrh addr = sqlite3VdbeCurrentAddr(v); 2420b9f50d8Sdrh sqlite3VdbeAddOp4(v, OP_String8, 0, memId-1, 0, p->pTab->zName, 0); 2430b9f50d8Sdrh sqlite3VdbeAddOp2(v, OP_Rewind, 0, addr+9); 2440b9f50d8Sdrh sqlite3VdbeAddOp3(v, OP_Column, 0, 0, memId); 2450b9f50d8Sdrh sqlite3VdbeAddOp3(v, OP_Ne, memId-1, addr+7, memId); 2460b9f50d8Sdrh sqlite3VdbeChangeP5(v, SQLITE_JUMPIFNULL); 2470b9f50d8Sdrh sqlite3VdbeAddOp2(v, OP_Rowid, 0, memId+1); 2480b9f50d8Sdrh sqlite3VdbeAddOp3(v, OP_Column, 0, 1, memId); 2490b9f50d8Sdrh sqlite3VdbeAddOp2(v, OP_Goto, 0, addr+9); 2500b9f50d8Sdrh sqlite3VdbeAddOp2(v, OP_Next, 0, addr+2); 2510b9f50d8Sdrh sqlite3VdbeAddOp2(v, OP_Integer, 0, memId); 2520b9f50d8Sdrh sqlite3VdbeAddOp0(v, OP_Close); 2530b9f50d8Sdrh } 2540b9f50d8Sdrh } 2550b9f50d8Sdrh 2560b9f50d8Sdrh /* 2579d9cf229Sdrh ** Update the maximum rowid for an autoincrement calculation. 2589d9cf229Sdrh ** 2599d9cf229Sdrh ** This routine should be called when the top of the stack holds a 2609d9cf229Sdrh ** new rowid that is about to be inserted. If that new rowid is 2619d9cf229Sdrh ** larger than the maximum rowid in the memId memory cell, then the 2629d9cf229Sdrh ** memory cell is updated. The stack is unchanged. 2639d9cf229Sdrh */ 2646a288a33Sdrh static void autoIncStep(Parse *pParse, int memId, int regRowid){ 2659d9cf229Sdrh if( memId>0 ){ 2666a288a33Sdrh sqlite3VdbeAddOp2(pParse->pVdbe, OP_MemMax, memId, regRowid); 2679d9cf229Sdrh } 2689d9cf229Sdrh } 2699d9cf229Sdrh 2709d9cf229Sdrh /* 2710b9f50d8Sdrh ** This routine generates the code needed to write autoincrement 2720b9f50d8Sdrh ** maximum rowid values back into the sqlite_sequence register. 2730b9f50d8Sdrh ** Every statement that might do an INSERT into an autoincrement 2740b9f50d8Sdrh ** table (either directly or through triggers) needs to call this 2750b9f50d8Sdrh ** routine just before the "exit" code. 2769d9cf229Sdrh */ 2770b9f50d8Sdrh void sqlite3AutoincrementEnd(Parse *pParse){ 2780b9f50d8Sdrh AutoincInfo *p; 2799d9cf229Sdrh Vdbe *v = pParse->pVdbe; 2800b9f50d8Sdrh sqlite3 *db = pParse->db; 2816a288a33Sdrh 2829d9cf229Sdrh assert( v ); 2830b9f50d8Sdrh for(p = pParse->pAinc; p; p = p->pNext){ 2840b9f50d8Sdrh Db *pDb = &db->aDb[p->iDb]; 2850b9f50d8Sdrh int j1, j2, j3, j4, j5; 2860b9f50d8Sdrh int iRec; 2870b9f50d8Sdrh int memId = p->regCtr; 2880b9f50d8Sdrh 2890b9f50d8Sdrh iRec = sqlite3GetTempReg(pParse); 2900b9f50d8Sdrh sqlite3OpenTable(pParse, 0, p->iDb, pDb->pSchema->pSeqTab, OP_OpenWrite); 2916a288a33Sdrh j1 = sqlite3VdbeAddOp1(v, OP_NotNull, memId+1); 2920b9f50d8Sdrh j2 = sqlite3VdbeAddOp0(v, OP_Rewind); 2930b9f50d8Sdrh j3 = sqlite3VdbeAddOp3(v, OP_Column, 0, 0, iRec); 2940b9f50d8Sdrh j4 = sqlite3VdbeAddOp3(v, OP_Eq, memId-1, 0, iRec); 2950b9f50d8Sdrh sqlite3VdbeAddOp2(v, OP_Next, 0, j3); 2960b9f50d8Sdrh sqlite3VdbeJumpHere(v, j2); 2970b9f50d8Sdrh sqlite3VdbeAddOp2(v, OP_NewRowid, 0, memId+1); 2980b9f50d8Sdrh j5 = sqlite3VdbeAddOp0(v, OP_Goto); 2990b9f50d8Sdrh sqlite3VdbeJumpHere(v, j4); 3000b9f50d8Sdrh sqlite3VdbeAddOp2(v, OP_Rowid, 0, memId+1); 3016a288a33Sdrh sqlite3VdbeJumpHere(v, j1); 3020b9f50d8Sdrh sqlite3VdbeJumpHere(v, j5); 303a7a8e14bSdanielk1977 sqlite3VdbeAddOp3(v, OP_MakeRecord, memId-1, 2, iRec); 3040b9f50d8Sdrh sqlite3VdbeAddOp3(v, OP_Insert, 0, iRec, memId+1); 30535573356Sdrh sqlite3VdbeChangeP5(v, OPFLAG_APPEND); 3060b9f50d8Sdrh sqlite3VdbeAddOp0(v, OP_Close); 3070b9f50d8Sdrh sqlite3ReleaseTempReg(pParse, iRec); 3089d9cf229Sdrh } 3099d9cf229Sdrh } 3109d9cf229Sdrh #else 3119d9cf229Sdrh /* 3129d9cf229Sdrh ** If SQLITE_OMIT_AUTOINCREMENT is defined, then the three routines 3139d9cf229Sdrh ** above are all no-ops 3149d9cf229Sdrh */ 3159d9cf229Sdrh # define autoIncBegin(A,B,C) (0) 316287fb61cSdanielk1977 # define autoIncStep(A,B,C) 3179d9cf229Sdrh #endif /* SQLITE_OMIT_AUTOINCREMENT */ 3189d9cf229Sdrh 3199d9cf229Sdrh 3209d9cf229Sdrh /* Forward declaration */ 3219d9cf229Sdrh static int xferOptimization( 3229d9cf229Sdrh Parse *pParse, /* Parser context */ 3239d9cf229Sdrh Table *pDest, /* The table we are inserting into */ 3249d9cf229Sdrh Select *pSelect, /* A SELECT statement to use as the data source */ 3259d9cf229Sdrh int onError, /* How to handle constraint errors */ 3269d9cf229Sdrh int iDbDest /* The database of pDest */ 3279d9cf229Sdrh ); 3289d9cf229Sdrh 3293d1bfeaaSdanielk1977 /* 3301ccde15dSdrh ** This routine is call to handle SQL of the following forms: 331cce7d176Sdrh ** 332cce7d176Sdrh ** insert into TABLE (IDLIST) values(EXPRLIST) 3331ccde15dSdrh ** insert into TABLE (IDLIST) select 334cce7d176Sdrh ** 3351ccde15dSdrh ** The IDLIST following the table name is always optional. If omitted, 3361ccde15dSdrh ** then a list of all columns for the table is substituted. The IDLIST 337967e8b73Sdrh ** appears in the pColumn parameter. pColumn is NULL if IDLIST is omitted. 3381ccde15dSdrh ** 3391ccde15dSdrh ** The pList parameter holds EXPRLIST in the first form of the INSERT 3401ccde15dSdrh ** statement above, and pSelect is NULL. For the second form, pList is 3411ccde15dSdrh ** NULL and pSelect is a pointer to the select statement used to generate 3421ccde15dSdrh ** data for the insert. 343142e30dfSdrh ** 3449d9cf229Sdrh ** The code generated follows one of four templates. For a simple 345142e30dfSdrh ** select with data coming from a VALUES clause, the code executes 346e00ee6ebSdrh ** once straight down through. Pseudo-code follows (we call this 347e00ee6ebSdrh ** the "1st template"): 348142e30dfSdrh ** 349142e30dfSdrh ** open write cursor to <table> and its indices 350142e30dfSdrh ** puts VALUES clause expressions onto the stack 351142e30dfSdrh ** write the resulting record into <table> 352142e30dfSdrh ** cleanup 353142e30dfSdrh ** 3549d9cf229Sdrh ** The three remaining templates assume the statement is of the form 355142e30dfSdrh ** 356142e30dfSdrh ** INSERT INTO <table> SELECT ... 357142e30dfSdrh ** 3589d9cf229Sdrh ** If the SELECT clause is of the restricted form "SELECT * FROM <table2>" - 3599d9cf229Sdrh ** in other words if the SELECT pulls all columns from a single table 3609d9cf229Sdrh ** and there is no WHERE or LIMIT or GROUP BY or ORDER BY clauses, and 3619d9cf229Sdrh ** if <table2> and <table1> are distinct tables but have identical 3629d9cf229Sdrh ** schemas, including all the same indices, then a special optimization 3639d9cf229Sdrh ** is invoked that copies raw records from <table2> over to <table1>. 3649d9cf229Sdrh ** See the xferOptimization() function for the implementation of this 365e00ee6ebSdrh ** template. This is the 2nd template. 3669d9cf229Sdrh ** 3679d9cf229Sdrh ** open a write cursor to <table> 3689d9cf229Sdrh ** open read cursor on <table2> 3699d9cf229Sdrh ** transfer all records in <table2> over to <table> 3709d9cf229Sdrh ** close cursors 3719d9cf229Sdrh ** foreach index on <table> 3729d9cf229Sdrh ** open a write cursor on the <table> index 3739d9cf229Sdrh ** open a read cursor on the corresponding <table2> index 3749d9cf229Sdrh ** transfer all records from the read to the write cursors 3759d9cf229Sdrh ** close cursors 3769d9cf229Sdrh ** end foreach 3779d9cf229Sdrh ** 378e00ee6ebSdrh ** The 3rd template is for when the second template does not apply 3799d9cf229Sdrh ** and the SELECT clause does not read from <table> at any time. 3809d9cf229Sdrh ** The generated code follows this template: 381142e30dfSdrh ** 382e00ee6ebSdrh ** EOF <- 0 383e00ee6ebSdrh ** X <- A 384142e30dfSdrh ** goto B 385142e30dfSdrh ** A: setup for the SELECT 3869d9cf229Sdrh ** loop over the rows in the SELECT 387e00ee6ebSdrh ** load values into registers R..R+n 388e00ee6ebSdrh ** yield X 389142e30dfSdrh ** end loop 390142e30dfSdrh ** cleanup after the SELECT 391e00ee6ebSdrh ** EOF <- 1 392e00ee6ebSdrh ** yield X 393142e30dfSdrh ** goto A 394e00ee6ebSdrh ** B: open write cursor to <table> and its indices 395e00ee6ebSdrh ** C: yield X 396e00ee6ebSdrh ** if EOF goto D 397e00ee6ebSdrh ** insert the select result into <table> from R..R+n 398e00ee6ebSdrh ** goto C 399142e30dfSdrh ** D: cleanup 400142e30dfSdrh ** 401e00ee6ebSdrh ** The 4th template is used if the insert statement takes its 402142e30dfSdrh ** values from a SELECT but the data is being inserted into a table 403142e30dfSdrh ** that is also read as part of the SELECT. In the third form, 404142e30dfSdrh ** we have to use a intermediate table to store the results of 405142e30dfSdrh ** the select. The template is like this: 406142e30dfSdrh ** 407e00ee6ebSdrh ** EOF <- 0 408e00ee6ebSdrh ** X <- A 409142e30dfSdrh ** goto B 410142e30dfSdrh ** A: setup for the SELECT 411142e30dfSdrh ** loop over the tables in the SELECT 412e00ee6ebSdrh ** load value into register R..R+n 413e00ee6ebSdrh ** yield X 414142e30dfSdrh ** end loop 415142e30dfSdrh ** cleanup after the SELECT 416e00ee6ebSdrh ** EOF <- 1 417e00ee6ebSdrh ** yield X 418e00ee6ebSdrh ** halt-error 419e00ee6ebSdrh ** B: open temp table 420e00ee6ebSdrh ** L: yield X 421e00ee6ebSdrh ** if EOF goto M 422e00ee6ebSdrh ** insert row from R..R+n into temp table 423e00ee6ebSdrh ** goto L 424e00ee6ebSdrh ** M: open write cursor to <table> and its indices 425e00ee6ebSdrh ** rewind temp table 426e00ee6ebSdrh ** C: loop over rows of intermediate table 427142e30dfSdrh ** transfer values form intermediate table into <table> 428e00ee6ebSdrh ** end loop 429e00ee6ebSdrh ** D: cleanup 430cce7d176Sdrh */ 4314adee20fSdanielk1977 void sqlite3Insert( 432cce7d176Sdrh Parse *pParse, /* Parser context */ 433113088ecSdrh SrcList *pTabList, /* Name of table into which we are inserting */ 434cce7d176Sdrh ExprList *pList, /* List of values to be inserted */ 4355974a30fSdrh Select *pSelect, /* A SELECT statement to use as the data source */ 4369cfcf5d4Sdrh IdList *pColumn, /* Column names corresponding to IDLIST. */ 4379cfcf5d4Sdrh int onError /* How to handle constraint errors */ 438cce7d176Sdrh ){ 4396a288a33Sdrh sqlite3 *db; /* The main database structure */ 4406a288a33Sdrh Table *pTab; /* The table to insert into. aka TABLE */ 441113088ecSdrh char *zTab; /* Name of the table into which we are inserting */ 442e22a334bSdrh const char *zDb; /* Name of the database holding this table */ 4435974a30fSdrh int i, j, idx; /* Loop counters */ 4445974a30fSdrh Vdbe *v; /* Generate code into this virtual machine */ 4455974a30fSdrh Index *pIdx; /* For looping over indices of the table */ 446967e8b73Sdrh int nColumn; /* Number of columns in the data */ 4476a288a33Sdrh int nHidden = 0; /* Number of hidden columns if TABLE is virtual */ 44804adf416Sdrh int baseCur = 0; /* VDBE Cursor number for pTab */ 4494a32431cSdrh int keyColumn = -1; /* Column that is the INTEGER PRIMARY KEY */ 4500ca3e24bSdrh int endOfLoop; /* Label for the end of the insertion loop */ 4514d88778bSdanielk1977 int useTempTable = 0; /* Store SELECT results in intermediate table */ 452cfe9a69fSdanielk1977 int srcTab = 0; /* Data comes from this temporary cursor if >=0 */ 453e00ee6ebSdrh int addrInsTop = 0; /* Jump to label "D" */ 454e00ee6ebSdrh int addrCont = 0; /* Top of insert loop. Label "C" in templates 3 and 4 */ 455e00ee6ebSdrh int addrSelect = 0; /* Address of coroutine that implements the SELECT */ 4562eb95377Sdrh SelectDest dest; /* Destination for SELECT on rhs of INSERT */ 4576a288a33Sdrh int iDb; /* Index of database holding TABLE */ 4582958a4e6Sdrh Db *pDb; /* The database containing table being inserted into */ 459e4d90813Sdrh int appendFlag = 0; /* True if the insert is likely to be an append */ 460cce7d176Sdrh 4616a288a33Sdrh /* Register allocations */ 4621bd10f8aSdrh int regFromSelect = 0;/* Base register for data coming from SELECT */ 4636a288a33Sdrh int regAutoinc = 0; /* Register holding the AUTOINCREMENT counter */ 4646a288a33Sdrh int regRowCount = 0; /* Memory cell used for the row counter */ 4656a288a33Sdrh int regIns; /* Block of regs holding rowid+data being inserted */ 4666a288a33Sdrh int regRowid; /* registers holding insert rowid */ 4676a288a33Sdrh int regData; /* register holding first column to insert */ 4686a288a33Sdrh int regRecord; /* Holds the assemblied row record */ 4691bd10f8aSdrh int regEof = 0; /* Register recording end of SELECT data */ 470aa9b8963Sdrh int *aRegIdx = 0; /* One register allocated to each index */ 4716a288a33Sdrh 472798da52cSdrh #ifndef SQLITE_OMIT_TRIGGER 473798da52cSdrh int isView; /* True if attempting to insert into a view */ 4742f886d1dSdanielk1977 Trigger *pTrigger; /* List of triggers on pTab, if required */ 4752f886d1dSdanielk1977 int tmask; /* Mask of trigger times */ 476798da52cSdrh #endif 477c3f9bad2Sdanielk1977 47817435752Sdrh db = pParse->db; 4791bd10f8aSdrh memset(&dest, 0, sizeof(dest)); 48017435752Sdrh if( pParse->nErr || db->mallocFailed ){ 4816f7adc8aSdrh goto insert_cleanup; 4826f7adc8aSdrh } 483daffd0e5Sdrh 4841ccde15dSdrh /* Locate the table into which we will be inserting new information. 4851ccde15dSdrh */ 486113088ecSdrh assert( pTabList->nSrc==1 ); 487113088ecSdrh zTab = pTabList->a[0].zName; 488098d1684Sdrh if( NEVER(zTab==0) ) goto insert_cleanup; 4894adee20fSdanielk1977 pTab = sqlite3SrcListLookup(pParse, pTabList); 490c3f9bad2Sdanielk1977 if( pTab==0 ){ 491c3f9bad2Sdanielk1977 goto insert_cleanup; 492c3f9bad2Sdanielk1977 } 493da184236Sdanielk1977 iDb = sqlite3SchemaToIndex(db, pTab->pSchema); 494da184236Sdanielk1977 assert( iDb<db->nDb ); 495da184236Sdanielk1977 pDb = &db->aDb[iDb]; 4962958a4e6Sdrh zDb = pDb->zName; 4974adee20fSdanielk1977 if( sqlite3AuthCheck(pParse, SQLITE_INSERT, pTab->zName, 0, zDb) ){ 4981962bda7Sdrh goto insert_cleanup; 4991962bda7Sdrh } 500c3f9bad2Sdanielk1977 501b7f9164eSdrh /* Figure out if we have any triggers and if the table being 502b7f9164eSdrh ** inserted into is a view 503b7f9164eSdrh */ 504b7f9164eSdrh #ifndef SQLITE_OMIT_TRIGGER 5052f886d1dSdanielk1977 pTrigger = sqlite3TriggersExist(pParse, pTab, TK_INSERT, 0, &tmask); 506b7f9164eSdrh isView = pTab->pSelect!=0; 507b7f9164eSdrh #else 5082f886d1dSdanielk1977 # define pTrigger 0 5092f886d1dSdanielk1977 # define tmask 0 510b7f9164eSdrh # define isView 0 511b7f9164eSdrh #endif 512b7f9164eSdrh #ifdef SQLITE_OMIT_VIEW 513b7f9164eSdrh # undef isView 514b7f9164eSdrh # define isView 0 515b7f9164eSdrh #endif 5162f886d1dSdanielk1977 assert( (pTrigger && tmask) || (pTrigger==0 && tmask==0) ); 517b7f9164eSdrh 518f573c99bSdrh /* If pTab is really a view, make sure it has been initialized. 519b3d24bf8Sdanielk1977 ** ViewGetColumnNames() is a no-op if pTab is not a view (or virtual 520b3d24bf8Sdanielk1977 ** module table). 521f573c99bSdrh */ 522b3d24bf8Sdanielk1977 if( sqlite3ViewGetColumnNames(pParse, pTab) ){ 523f573c99bSdrh goto insert_cleanup; 524f573c99bSdrh } 525f573c99bSdrh 526595a523aSdanielk1977 /* Ensure that: 527595a523aSdanielk1977 * (a) the table is not read-only, 528595a523aSdanielk1977 * (b) that if it is a view then ON INSERT triggers exist 529595a523aSdanielk1977 */ 530595a523aSdanielk1977 if( sqlite3IsReadOnly(pParse, pTab, tmask) ){ 531595a523aSdanielk1977 goto insert_cleanup; 532595a523aSdanielk1977 } 533595a523aSdanielk1977 5341ccde15dSdrh /* Allocate a VDBE 5351ccde15dSdrh */ 5364adee20fSdanielk1977 v = sqlite3GetVdbe(pParse); 5375974a30fSdrh if( v==0 ) goto insert_cleanup; 5384794f735Sdrh if( pParse->nested==0 ) sqlite3VdbeCountChanges(v); 5392f886d1dSdanielk1977 sqlite3BeginWriteOperation(pParse, pSelect || pTrigger, iDb); 5401ccde15dSdrh 5419d9cf229Sdrh #ifndef SQLITE_OMIT_XFER_OPT 5429d9cf229Sdrh /* If the statement is of the form 5439d9cf229Sdrh ** 5449d9cf229Sdrh ** INSERT INTO <table1> SELECT * FROM <table2>; 5459d9cf229Sdrh ** 5469d9cf229Sdrh ** Then special optimizations can be applied that make the transfer 5479d9cf229Sdrh ** very fast and which reduce fragmentation of indices. 548e00ee6ebSdrh ** 549e00ee6ebSdrh ** This is the 2nd template. 5509d9cf229Sdrh */ 5519d9cf229Sdrh if( pColumn==0 && xferOptimization(pParse, pTab, pSelect, onError, iDb) ){ 5522f886d1dSdanielk1977 assert( !pTrigger ); 5539d9cf229Sdrh assert( pList==0 ); 5540b9f50d8Sdrh goto insert_end; 5559d9cf229Sdrh } 5569d9cf229Sdrh #endif /* SQLITE_OMIT_XFER_OPT */ 5579d9cf229Sdrh 5582958a4e6Sdrh /* If this is an AUTOINCREMENT table, look up the sequence number in the 5596a288a33Sdrh ** sqlite_sequence table and store it in memory cell regAutoinc. 5602958a4e6Sdrh */ 5616a288a33Sdrh regAutoinc = autoIncBegin(pParse, iDb, pTab); 5622958a4e6Sdrh 5631ccde15dSdrh /* Figure out how many columns of data are supplied. If the data 564e00ee6ebSdrh ** is coming from a SELECT statement, then generate a co-routine that 565e00ee6ebSdrh ** produces a single row of the SELECT on each invocation. The 566e00ee6ebSdrh ** co-routine is the common header to the 3rd and 4th templates. 5671ccde15dSdrh */ 5685974a30fSdrh if( pSelect ){ 569142e30dfSdrh /* Data is coming from a SELECT. Generate code to implement that SELECT 570e00ee6ebSdrh ** as a co-routine. The code is common to both the 3rd and 4th 571e00ee6ebSdrh ** templates: 572e00ee6ebSdrh ** 573e00ee6ebSdrh ** EOF <- 0 574e00ee6ebSdrh ** X <- A 575e00ee6ebSdrh ** goto B 576e00ee6ebSdrh ** A: setup for the SELECT 577e00ee6ebSdrh ** loop over the tables in the SELECT 578e00ee6ebSdrh ** load value into register R..R+n 579e00ee6ebSdrh ** yield X 580e00ee6ebSdrh ** end loop 581e00ee6ebSdrh ** cleanup after the SELECT 582e00ee6ebSdrh ** EOF <- 1 583e00ee6ebSdrh ** yield X 584e00ee6ebSdrh ** halt-error 585e00ee6ebSdrh ** 586e00ee6ebSdrh ** On each invocation of the co-routine, it puts a single row of the 587e00ee6ebSdrh ** SELECT result into registers dest.iMem...dest.iMem+dest.nMem-1. 588e00ee6ebSdrh ** (These output registers are allocated by sqlite3Select().) When 589e00ee6ebSdrh ** the SELECT completes, it sets the EOF flag stored in regEof. 590142e30dfSdrh */ 591e00ee6ebSdrh int rc, j1; 5921013c932Sdrh 593e00ee6ebSdrh regEof = ++pParse->nMem; 594e00ee6ebSdrh sqlite3VdbeAddOp2(v, OP_Integer, 0, regEof); /* EOF <- 0 */ 595e00ee6ebSdrh VdbeComment((v, "SELECT eof flag")); 59692b01d53Sdrh sqlite3SelectDestInit(&dest, SRT_Coroutine, ++pParse->nMem); 597e00ee6ebSdrh addrSelect = sqlite3VdbeCurrentAddr(v)+2; 59892b01d53Sdrh sqlite3VdbeAddOp2(v, OP_Integer, addrSelect-1, dest.iParm); 599e00ee6ebSdrh j1 = sqlite3VdbeAddOp2(v, OP_Goto, 0, 0); 600e00ee6ebSdrh VdbeComment((v, "Jump over SELECT coroutine")); 601b3bce662Sdanielk1977 602b3bce662Sdanielk1977 /* Resolve the expressions in the SELECT statement and execute it. */ 6037d10d5a6Sdrh rc = sqlite3Select(pParse, pSelect, &dest); 604098d1684Sdrh assert( pParse->nErr==0 || rc ); 605098d1684Sdrh if( rc || NEVER(pParse->nErr) || db->mallocFailed ){ 6066f7adc8aSdrh goto insert_cleanup; 6076f7adc8aSdrh } 608e00ee6ebSdrh sqlite3VdbeAddOp2(v, OP_Integer, 1, regEof); /* EOF <- 1 */ 60992b01d53Sdrh sqlite3VdbeAddOp1(v, OP_Yield, dest.iParm); /* yield X */ 610e00ee6ebSdrh sqlite3VdbeAddOp2(v, OP_Halt, SQLITE_INTERNAL, OE_Abort); 611e00ee6ebSdrh VdbeComment((v, "End of SELECT coroutine")); 612e00ee6ebSdrh sqlite3VdbeJumpHere(v, j1); /* label B: */ 613b3bce662Sdanielk1977 6146a288a33Sdrh regFromSelect = dest.iMem; 6155974a30fSdrh assert( pSelect->pEList ); 616967e8b73Sdrh nColumn = pSelect->pEList->nExpr; 617e00ee6ebSdrh assert( dest.nMem==nColumn ); 618142e30dfSdrh 619142e30dfSdrh /* Set useTempTable to TRUE if the result of the SELECT statement 620e00ee6ebSdrh ** should be written into a temporary table (template 4). Set to 621e00ee6ebSdrh ** FALSE if each* row of the SELECT can be written directly into 622e00ee6ebSdrh ** the destination table (template 3). 623048c530cSdrh ** 624048c530cSdrh ** A temp table must be used if the table being updated is also one 625048c530cSdrh ** of the tables being read by the SELECT statement. Also use a 626048c530cSdrh ** temp table in the case of row triggers. 627142e30dfSdrh */ 628595a523aSdanielk1977 if( pTrigger || readsTable(pParse, addrSelect, iDb, pTab) ){ 629048c530cSdrh useTempTable = 1; 630048c530cSdrh } 631142e30dfSdrh 632142e30dfSdrh if( useTempTable ){ 633e00ee6ebSdrh /* Invoke the coroutine to extract information from the SELECT 634e00ee6ebSdrh ** and add it to a transient table srcTab. The code generated 635e00ee6ebSdrh ** here is from the 4th template: 636e00ee6ebSdrh ** 637e00ee6ebSdrh ** B: open temp table 638e00ee6ebSdrh ** L: yield X 639e00ee6ebSdrh ** if EOF goto M 640e00ee6ebSdrh ** insert row from R..R+n into temp table 641e00ee6ebSdrh ** goto L 642e00ee6ebSdrh ** M: ... 643142e30dfSdrh */ 644e00ee6ebSdrh int regRec; /* Register to hold packed record */ 645dc5ea5c7Sdrh int regTempRowid; /* Register to hold temp table ROWID */ 646e00ee6ebSdrh int addrTop; /* Label "L" */ 647e00ee6ebSdrh int addrIf; /* Address of jump to M */ 648b7654111Sdrh 649142e30dfSdrh srcTab = pParse->nTab++; 650b7654111Sdrh regRec = sqlite3GetTempReg(pParse); 651dc5ea5c7Sdrh regTempRowid = sqlite3GetTempReg(pParse); 652e00ee6ebSdrh sqlite3VdbeAddOp2(v, OP_OpenEphemeral, srcTab, nColumn); 65392b01d53Sdrh addrTop = sqlite3VdbeAddOp1(v, OP_Yield, dest.iParm); 654e00ee6ebSdrh addrIf = sqlite3VdbeAddOp1(v, OP_If, regEof); 6551db639ceSdrh sqlite3VdbeAddOp3(v, OP_MakeRecord, regFromSelect, nColumn, regRec); 656dc5ea5c7Sdrh sqlite3VdbeAddOp2(v, OP_NewRowid, srcTab, regTempRowid); 657dc5ea5c7Sdrh sqlite3VdbeAddOp3(v, OP_Insert, srcTab, regRec, regTempRowid); 658e00ee6ebSdrh sqlite3VdbeAddOp2(v, OP_Goto, 0, addrTop); 659e00ee6ebSdrh sqlite3VdbeJumpHere(v, addrIf); 660b7654111Sdrh sqlite3ReleaseTempReg(pParse, regRec); 661dc5ea5c7Sdrh sqlite3ReleaseTempReg(pParse, regTempRowid); 662142e30dfSdrh } 663142e30dfSdrh }else{ 664142e30dfSdrh /* This is the case if the data for the INSERT is coming from a VALUES 665142e30dfSdrh ** clause 666142e30dfSdrh */ 667b3bce662Sdanielk1977 NameContext sNC; 668b3bce662Sdanielk1977 memset(&sNC, 0, sizeof(sNC)); 669b3bce662Sdanielk1977 sNC.pParse = pParse; 6705974a30fSdrh srcTab = -1; 67148d1178aSdrh assert( useTempTable==0 ); 672147d0cccSdrh nColumn = pList ? pList->nExpr : 0; 673e64e7b20Sdrh for(i=0; i<nColumn; i++){ 6747d10d5a6Sdrh if( sqlite3ResolveExprNames(&sNC, pList->a[i].pExpr) ){ 675b04a5d87Sdrh goto insert_cleanup; 676b04a5d87Sdrh } 677e64e7b20Sdrh } 6785974a30fSdrh } 6791ccde15dSdrh 6801ccde15dSdrh /* Make sure the number of columns in the source data matches the number 6811ccde15dSdrh ** of columns to be inserted into the table. 6821ccde15dSdrh */ 683034ca14fSdanielk1977 if( IsVirtual(pTab) ){ 684034ca14fSdanielk1977 for(i=0; i<pTab->nCol; i++){ 685034ca14fSdanielk1977 nHidden += (IsHiddenColumn(&pTab->aCol[i]) ? 1 : 0); 686034ca14fSdanielk1977 } 687034ca14fSdanielk1977 } 688034ca14fSdanielk1977 if( pColumn==0 && nColumn && nColumn!=(pTab->nCol-nHidden) ){ 6894adee20fSdanielk1977 sqlite3ErrorMsg(pParse, 690da93d238Sdrh "table %S has %d columns but %d values were supplied", 691d51397a6Sdrh pTabList, 0, pTab->nCol-nHidden, nColumn); 692cce7d176Sdrh goto insert_cleanup; 693cce7d176Sdrh } 694967e8b73Sdrh if( pColumn!=0 && nColumn!=pColumn->nId ){ 6954adee20fSdanielk1977 sqlite3ErrorMsg(pParse, "%d values for %d columns", nColumn, pColumn->nId); 696cce7d176Sdrh goto insert_cleanup; 697cce7d176Sdrh } 6981ccde15dSdrh 6991ccde15dSdrh /* If the INSERT statement included an IDLIST term, then make sure 7001ccde15dSdrh ** all elements of the IDLIST really are columns of the table and 7011ccde15dSdrh ** remember the column indices. 702c8392586Sdrh ** 703c8392586Sdrh ** If the table has an INTEGER PRIMARY KEY column and that column 704c8392586Sdrh ** is named in the IDLIST, then record in the keyColumn variable 705c8392586Sdrh ** the index into IDLIST of the primary key column. keyColumn is 706c8392586Sdrh ** the index of the primary key as it appears in IDLIST, not as 707c8392586Sdrh ** is appears in the original table. (The index of the primary 708c8392586Sdrh ** key in the original table is pTab->iPKey.) 7091ccde15dSdrh */ 710967e8b73Sdrh if( pColumn ){ 711967e8b73Sdrh for(i=0; i<pColumn->nId; i++){ 712967e8b73Sdrh pColumn->a[i].idx = -1; 713cce7d176Sdrh } 714967e8b73Sdrh for(i=0; i<pColumn->nId; i++){ 715cce7d176Sdrh for(j=0; j<pTab->nCol; j++){ 7164adee20fSdanielk1977 if( sqlite3StrICmp(pColumn->a[i].zName, pTab->aCol[j].zName)==0 ){ 717967e8b73Sdrh pColumn->a[i].idx = j; 7184a32431cSdrh if( j==pTab->iPKey ){ 7199aa028daSdrh keyColumn = i; 7204a32431cSdrh } 721cce7d176Sdrh break; 722cce7d176Sdrh } 723cce7d176Sdrh } 724cce7d176Sdrh if( j>=pTab->nCol ){ 7254adee20fSdanielk1977 if( sqlite3IsRowid(pColumn->a[i].zName) ){ 726a0217ba7Sdrh keyColumn = i; 727a0217ba7Sdrh }else{ 7284adee20fSdanielk1977 sqlite3ErrorMsg(pParse, "table %S has no column named %s", 729da93d238Sdrh pTabList, 0, pColumn->a[i].zName); 7301db95106Sdan pParse->checkSchema = 1; 731cce7d176Sdrh goto insert_cleanup; 732cce7d176Sdrh } 733cce7d176Sdrh } 734cce7d176Sdrh } 735a0217ba7Sdrh } 7361ccde15dSdrh 737aacc543eSdrh /* If there is no IDLIST term but the table has an integer primary 738c8392586Sdrh ** key, the set the keyColumn variable to the primary key column index 739c8392586Sdrh ** in the original table definition. 7404a32431cSdrh */ 741147d0cccSdrh if( pColumn==0 && nColumn>0 ){ 7424a32431cSdrh keyColumn = pTab->iPKey; 7434a32431cSdrh } 7444a32431cSdrh 745c3f9bad2Sdanielk1977 /* Initialize the count of rows to be inserted 7461ccde15dSdrh */ 747142e30dfSdrh if( db->flags & SQLITE_CountRows ){ 7486a288a33Sdrh regRowCount = ++pParse->nMem; 7496a288a33Sdrh sqlite3VdbeAddOp2(v, OP_Integer, 0, regRowCount); 750c3f9bad2Sdanielk1977 } 751c3f9bad2Sdanielk1977 752e448dc4aSdanielk1977 /* If this is not a view, open the table and and all indices */ 753e448dc4aSdanielk1977 if( !isView ){ 754aa9b8963Sdrh int nIdx; 755aa9b8963Sdrh 75604adf416Sdrh baseCur = pParse->nTab; 75704adf416Sdrh nIdx = sqlite3OpenTableAndIndices(pParse, pTab, baseCur, OP_OpenWrite); 7585c070538Sdrh aRegIdx = sqlite3DbMallocRaw(db, sizeof(int)*(nIdx+1)); 759aa9b8963Sdrh if( aRegIdx==0 ){ 760aa9b8963Sdrh goto insert_cleanup; 761aa9b8963Sdrh } 762aa9b8963Sdrh for(i=0; i<nIdx; i++){ 763aa9b8963Sdrh aRegIdx[i] = ++pParse->nMem; 764aa9b8963Sdrh } 765feeb1394Sdrh } 766feeb1394Sdrh 767e00ee6ebSdrh /* This is the top of the main insertion loop */ 768142e30dfSdrh if( useTempTable ){ 769e00ee6ebSdrh /* This block codes the top of loop only. The complete loop is the 770e00ee6ebSdrh ** following pseudocode (template 4): 771e00ee6ebSdrh ** 772e00ee6ebSdrh ** rewind temp table 773e00ee6ebSdrh ** C: loop over rows of intermediate table 774e00ee6ebSdrh ** transfer values form intermediate table into <table> 775e00ee6ebSdrh ** end loop 776e00ee6ebSdrh ** D: ... 777e00ee6ebSdrh */ 778e00ee6ebSdrh addrInsTop = sqlite3VdbeAddOp1(v, OP_Rewind, srcTab); 779e00ee6ebSdrh addrCont = sqlite3VdbeCurrentAddr(v); 780142e30dfSdrh }else if( pSelect ){ 781e00ee6ebSdrh /* This block codes the top of loop only. The complete loop is the 782e00ee6ebSdrh ** following pseudocode (template 3): 783e00ee6ebSdrh ** 784e00ee6ebSdrh ** C: yield X 785e00ee6ebSdrh ** if EOF goto D 786e00ee6ebSdrh ** insert the select result into <table> from R..R+n 787e00ee6ebSdrh ** goto C 788e00ee6ebSdrh ** D: ... 789e00ee6ebSdrh */ 79092b01d53Sdrh addrCont = sqlite3VdbeAddOp1(v, OP_Yield, dest.iParm); 791e00ee6ebSdrh addrInsTop = sqlite3VdbeAddOp1(v, OP_If, regEof); 792bed8690fSdrh } 7931ccde15dSdrh 7946a288a33Sdrh /* Allocate registers for holding the rowid of the new row, 7956a288a33Sdrh ** the content of the new row, and the assemblied row record. 7966a288a33Sdrh */ 7976a288a33Sdrh regRecord = ++pParse->nMem; 7986a288a33Sdrh regRowid = regIns = pParse->nMem+1; 7996a288a33Sdrh pParse->nMem += pTab->nCol + 1; 8006a288a33Sdrh if( IsVirtual(pTab) ){ 8016a288a33Sdrh regRowid++; 8026a288a33Sdrh pParse->nMem++; 8036a288a33Sdrh } 8046a288a33Sdrh regData = regRowid+1; 8056a288a33Sdrh 8065cf590c1Sdrh /* Run the BEFORE and INSTEAD OF triggers, if there are any 80770ce3f0cSdrh */ 8084adee20fSdanielk1977 endOfLoop = sqlite3VdbeMakeLabel(v); 8092f886d1dSdanielk1977 if( tmask & TRIGGER_BEFORE ){ 81076d462eeSdan int regCols = sqlite3GetTempRange(pParse, pTab->nCol+1); 811c3f9bad2Sdanielk1977 81270ce3f0cSdrh /* build the NEW.* reference row. Note that if there is an INTEGER 81370ce3f0cSdrh ** PRIMARY KEY into which a NULL is being inserted, that NULL will be 81470ce3f0cSdrh ** translated into a unique ID for the row. But on a BEFORE trigger, 81570ce3f0cSdrh ** we do not know what the unique ID will be (because the insert has 81670ce3f0cSdrh ** not happened yet) so we substitute a rowid of -1 81770ce3f0cSdrh */ 81870ce3f0cSdrh if( keyColumn<0 ){ 81976d462eeSdan sqlite3VdbeAddOp2(v, OP_Integer, -1, regCols); 82070ce3f0cSdrh }else{ 8216a288a33Sdrh int j1; 8227fe45908Sdrh if( useTempTable ){ 82376d462eeSdan sqlite3VdbeAddOp3(v, OP_Column, srcTab, keyColumn, regCols); 8247fe45908Sdrh }else{ 825d6fe961eSdrh assert( pSelect==0 ); /* Otherwise useTempTable is true */ 82676d462eeSdan sqlite3ExprCode(pParse, pList->a[keyColumn].pExpr, regCols); 8277fe45908Sdrh } 82876d462eeSdan j1 = sqlite3VdbeAddOp1(v, OP_NotNull, regCols); 82976d462eeSdan sqlite3VdbeAddOp2(v, OP_Integer, -1, regCols); 8306a288a33Sdrh sqlite3VdbeJumpHere(v, j1); 83176d462eeSdan sqlite3VdbeAddOp1(v, OP_MustBeInt, regCols); 83270ce3f0cSdrh } 83370ce3f0cSdrh 834034ca14fSdanielk1977 /* Cannot have triggers on a virtual table. If it were possible, 835034ca14fSdanielk1977 ** this block would have to account for hidden column. 836034ca14fSdanielk1977 */ 837034ca14fSdanielk1977 assert( !IsVirtual(pTab) ); 838034ca14fSdanielk1977 83970ce3f0cSdrh /* Create the new column data 84070ce3f0cSdrh */ 841c3f9bad2Sdanielk1977 for(i=0; i<pTab->nCol; i++){ 842c3f9bad2Sdanielk1977 if( pColumn==0 ){ 843c3f9bad2Sdanielk1977 j = i; 844c3f9bad2Sdanielk1977 }else{ 845c3f9bad2Sdanielk1977 for(j=0; j<pColumn->nId; j++){ 846c3f9bad2Sdanielk1977 if( pColumn->a[j].idx==i ) break; 847c3f9bad2Sdanielk1977 } 848c3f9bad2Sdanielk1977 } 8497ba45971Sdan if( (!useTempTable && !pList) || (pColumn && j>=pColumn->nId) ){ 85076d462eeSdan sqlite3ExprCode(pParse, pTab->aCol[i].pDflt, regCols+i+1); 851142e30dfSdrh }else if( useTempTable ){ 85276d462eeSdan sqlite3VdbeAddOp3(v, OP_Column, srcTab, j, regCols+i+1); 853c3f9bad2Sdanielk1977 }else{ 854d6fe961eSdrh assert( pSelect==0 ); /* Otherwise useTempTable is true */ 85576d462eeSdan sqlite3ExprCodeAndCache(pParse, pList->a[j].pExpr, regCols+i+1); 856c3f9bad2Sdanielk1977 } 857c3f9bad2Sdanielk1977 } 858a37cdde0Sdanielk1977 859a37cdde0Sdanielk1977 /* If this is an INSERT on a view with an INSTEAD OF INSERT trigger, 860a37cdde0Sdanielk1977 ** do not attempt any conversions before assembling the record. 861a37cdde0Sdanielk1977 ** If this is a real table, attempt conversions as required by the 862a37cdde0Sdanielk1977 ** table column affinities. 863a37cdde0Sdanielk1977 */ 864a37cdde0Sdanielk1977 if( !isView ){ 86576d462eeSdan sqlite3VdbeAddOp2(v, OP_Affinity, regCols+1, pTab->nCol); 866a37cdde0Sdanielk1977 sqlite3TableAffinityStr(v, pTab); 867a37cdde0Sdanielk1977 } 868c3f9bad2Sdanielk1977 8695cf590c1Sdrh /* Fire BEFORE or INSTEAD OF triggers */ 870165921a7Sdan sqlite3CodeRowTrigger(pParse, pTrigger, TK_INSERT, 0, TRIGGER_BEFORE, 87194d7f50aSdan pTab, regCols-pTab->nCol-1, onError, endOfLoop); 872165921a7Sdan 87376d462eeSdan sqlite3ReleaseTempRange(pParse, regCols, pTab->nCol+1); 87470ce3f0cSdrh } 875c3f9bad2Sdanielk1977 8764a32431cSdrh /* Push the record number for the new entry onto the stack. The 877f0863fe5Sdrh ** record number is a randomly generate integer created by NewRowid 8784a32431cSdrh ** except when the table has an INTEGER PRIMARY KEY column, in which 879b419a926Sdrh ** case the record number is the same as that column. 8801ccde15dSdrh */ 8815cf590c1Sdrh if( !isView ){ 8824cbdda9eSdrh if( IsVirtual(pTab) ){ 8834cbdda9eSdrh /* The row that the VUpdate opcode will delete: none */ 8846a288a33Sdrh sqlite3VdbeAddOp2(v, OP_Null, 0, regIns); 8854cbdda9eSdrh } 8864a32431cSdrh if( keyColumn>=0 ){ 887142e30dfSdrh if( useTempTable ){ 8886a288a33Sdrh sqlite3VdbeAddOp3(v, OP_Column, srcTab, keyColumn, regRowid); 889142e30dfSdrh }else if( pSelect ){ 890b7654111Sdrh sqlite3VdbeAddOp2(v, OP_SCopy, regFromSelect+keyColumn, regRowid); 8914a32431cSdrh }else{ 892e4d90813Sdrh VdbeOp *pOp; 8931db639ceSdrh sqlite3ExprCode(pParse, pList->a[keyColumn].pExpr, regRowid); 89420411ea7Sdrh pOp = sqlite3VdbeGetOp(v, -1); 8951b7ecbb4Sdrh if( ALWAYS(pOp) && pOp->opcode==OP_Null && !IsVirtual(pTab) ){ 896e4d90813Sdrh appendFlag = 1; 897e4d90813Sdrh pOp->opcode = OP_NewRowid; 89804adf416Sdrh pOp->p1 = baseCur; 8996a288a33Sdrh pOp->p2 = regRowid; 9006a288a33Sdrh pOp->p3 = regAutoinc; 901e4d90813Sdrh } 90227a32783Sdrh } 903f0863fe5Sdrh /* If the PRIMARY KEY expression is NULL, then use OP_NewRowid 904e1e68f49Sdrh ** to generate a unique primary key value. 905e1e68f49Sdrh */ 906e4d90813Sdrh if( !appendFlag ){ 9071db639ceSdrh int j1; 908bb50e7adSdanielk1977 if( !IsVirtual(pTab) ){ 9091db639ceSdrh j1 = sqlite3VdbeAddOp1(v, OP_NotNull, regRowid); 91004adf416Sdrh sqlite3VdbeAddOp3(v, OP_NewRowid, baseCur, regRowid, regAutoinc); 9111db639ceSdrh sqlite3VdbeJumpHere(v, j1); 912bb50e7adSdanielk1977 }else{ 913bb50e7adSdanielk1977 j1 = sqlite3VdbeCurrentAddr(v); 914bb50e7adSdanielk1977 sqlite3VdbeAddOp2(v, OP_IsNull, regRowid, j1+2); 915bb50e7adSdanielk1977 } 9163c84ddffSdrh sqlite3VdbeAddOp1(v, OP_MustBeInt, regRowid); 917e4d90813Sdrh } 9184cbdda9eSdrh }else if( IsVirtual(pTab) ){ 9196a288a33Sdrh sqlite3VdbeAddOp2(v, OP_Null, 0, regRowid); 9204a32431cSdrh }else{ 92104adf416Sdrh sqlite3VdbeAddOp3(v, OP_NewRowid, baseCur, regRowid, regAutoinc); 922e4d90813Sdrh appendFlag = 1; 9234a32431cSdrh } 9246a288a33Sdrh autoIncStep(pParse, regAutoinc, regRowid); 9254a32431cSdrh 926aacc543eSdrh /* Push onto the stack, data for all columns of the new entry, beginning 9274a32431cSdrh ** with the first column. 9284a32431cSdrh */ 929034ca14fSdanielk1977 nHidden = 0; 930cce7d176Sdrh for(i=0; i<pTab->nCol; i++){ 9316a288a33Sdrh int iRegStore = regRowid+1+i; 9324a32431cSdrh if( i==pTab->iPKey ){ 9334a32431cSdrh /* The value of the INTEGER PRIMARY KEY column is always a NULL. 934aacc543eSdrh ** Whenever this column is read, the record number will be substituted 935aacc543eSdrh ** in its place. So will fill this column with a NULL to avoid 936aacc543eSdrh ** taking up data space with information that will never be used. */ 9374c583128Sdrh sqlite3VdbeAddOp2(v, OP_Null, 0, iRegStore); 9384a32431cSdrh continue; 9394a32431cSdrh } 940967e8b73Sdrh if( pColumn==0 ){ 941034ca14fSdanielk1977 if( IsHiddenColumn(&pTab->aCol[i]) ){ 942034ca14fSdanielk1977 assert( IsVirtual(pTab) ); 943034ca14fSdanielk1977 j = -1; 944034ca14fSdanielk1977 nHidden++; 945034ca14fSdanielk1977 }else{ 946034ca14fSdanielk1977 j = i - nHidden; 947034ca14fSdanielk1977 } 948cce7d176Sdrh }else{ 949967e8b73Sdrh for(j=0; j<pColumn->nId; j++){ 950967e8b73Sdrh if( pColumn->a[j].idx==i ) break; 951cce7d176Sdrh } 952cce7d176Sdrh } 953034ca14fSdanielk1977 if( j<0 || nColumn==0 || (pColumn && j>=pColumn->nId) ){ 954287fb61cSdanielk1977 sqlite3ExprCode(pParse, pTab->aCol[i].pDflt, iRegStore); 955142e30dfSdrh }else if( useTempTable ){ 956287fb61cSdanielk1977 sqlite3VdbeAddOp3(v, OP_Column, srcTab, j, iRegStore); 957142e30dfSdrh }else if( pSelect ){ 958b7654111Sdrh sqlite3VdbeAddOp2(v, OP_SCopy, regFromSelect+j, iRegStore); 959cce7d176Sdrh }else{ 960287fb61cSdanielk1977 sqlite3ExprCode(pParse, pList->a[j].pExpr, iRegStore); 961cce7d176Sdrh } 962cce7d176Sdrh } 9631ccde15dSdrh 9640ca3e24bSdrh /* Generate code to check constraints and generate index keys and 9650ca3e24bSdrh ** do the insertion. 9664a32431cSdrh */ 9674cbdda9eSdrh #ifndef SQLITE_OMIT_VIRTUALTABLE 9684cbdda9eSdrh if( IsVirtual(pTab) ){ 969595a523aSdanielk1977 const char *pVTab = (const char *)sqlite3GetVTable(db, pTab); 9704f3dd150Sdrh sqlite3VtabMakeWritable(pParse, pTab); 971595a523aSdanielk1977 sqlite3VdbeAddOp4(v, OP_VUpdate, 1, pTab->nCol+2, regIns, pVTab, P4_VTAB); 972e0af83acSdan sqlite3MayAbort(pParse); 9734cbdda9eSdrh }else 9744cbdda9eSdrh #endif 9754cbdda9eSdrh { 976de630353Sdanielk1977 int isReplace; /* Set to true if constraints may cause a replace */ 977de630353Sdanielk1977 sqlite3GenerateConstraintChecks(pParse, pTab, baseCur, regIns, aRegIdx, 978de630353Sdanielk1977 keyColumn>=0, 0, onError, endOfLoop, &isReplace 97904adf416Sdrh ); 980e7a94d81Sdan sqlite3FkCheck(pParse, pTab, 0, regIns); 98104adf416Sdrh sqlite3CompleteInsertion( 9822832ad42Sdan pParse, pTab, baseCur, regIns, aRegIdx, 0, appendFlag, isReplace==0 98304adf416Sdrh ); 9845cf590c1Sdrh } 9854cbdda9eSdrh } 9861bee3d7bSdrh 987feeb1394Sdrh /* Update the count of rows that are inserted 9881bee3d7bSdrh */ 989142e30dfSdrh if( (db->flags & SQLITE_CountRows)!=0 ){ 9906a288a33Sdrh sqlite3VdbeAddOp2(v, OP_AddImm, regRowCount, 1); 9911bee3d7bSdrh } 992c3f9bad2Sdanielk1977 9932f886d1dSdanielk1977 if( pTrigger ){ 994c3f9bad2Sdanielk1977 /* Code AFTER triggers */ 995165921a7Sdan sqlite3CodeRowTrigger(pParse, pTrigger, TK_INSERT, 0, TRIGGER_AFTER, 99694d7f50aSdan pTab, regData-2-pTab->nCol, onError, endOfLoop); 997c3f9bad2Sdanielk1977 } 9981bee3d7bSdrh 999e00ee6ebSdrh /* The bottom of the main insertion loop, if the data source 1000e00ee6ebSdrh ** is a SELECT statement. 10011ccde15dSdrh */ 10024adee20fSdanielk1977 sqlite3VdbeResolveLabel(v, endOfLoop); 1003142e30dfSdrh if( useTempTable ){ 1004e00ee6ebSdrh sqlite3VdbeAddOp2(v, OP_Next, srcTab, addrCont); 1005e00ee6ebSdrh sqlite3VdbeJumpHere(v, addrInsTop); 10062eb95377Sdrh sqlite3VdbeAddOp1(v, OP_Close, srcTab); 1007142e30dfSdrh }else if( pSelect ){ 1008e00ee6ebSdrh sqlite3VdbeAddOp2(v, OP_Goto, 0, addrCont); 1009e00ee6ebSdrh sqlite3VdbeJumpHere(v, addrInsTop); 10106b56344dSdrh } 1011c3f9bad2Sdanielk1977 1012e448dc4aSdanielk1977 if( !IsVirtual(pTab) && !isView ){ 1013c3f9bad2Sdanielk1977 /* Close all tables opened */ 10142eb95377Sdrh sqlite3VdbeAddOp1(v, OP_Close, baseCur); 10156b56344dSdrh for(idx=1, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, idx++){ 10162eb95377Sdrh sqlite3VdbeAddOp1(v, OP_Close, idx+baseCur); 1017cce7d176Sdrh } 1018c3f9bad2Sdanielk1977 } 1019c3f9bad2Sdanielk1977 10200b9f50d8Sdrh insert_end: 1021f3388144Sdrh /* Update the sqlite_sequence table by storing the content of the 10220b9f50d8Sdrh ** maximum rowid counter values recorded while inserting into 10230b9f50d8Sdrh ** autoincrement tables. 10242958a4e6Sdrh */ 1025165921a7Sdan if( pParse->nested==0 && pParse->pTriggerTab==0 ){ 10260b9f50d8Sdrh sqlite3AutoincrementEnd(pParse); 10270b9f50d8Sdrh } 10282958a4e6Sdrh 10291bee3d7bSdrh /* 1030e7de6f25Sdanielk1977 ** Return the number of rows inserted. If this routine is 1031e7de6f25Sdanielk1977 ** generating code because of a call to sqlite3NestedParse(), do not 1032e7de6f25Sdanielk1977 ** invoke the callback function. 10331bee3d7bSdrh */ 1034165921a7Sdan if( (db->flags&SQLITE_CountRows) && !pParse->nested && !pParse->pTriggerTab ){ 10356a288a33Sdrh sqlite3VdbeAddOp2(v, OP_ResultRow, regRowCount, 1); 103622322fd4Sdanielk1977 sqlite3VdbeSetNumCols(v, 1); 103710fb749bSdanielk1977 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "rows inserted", SQLITE_STATIC); 10381bee3d7bSdrh } 1039cce7d176Sdrh 1040cce7d176Sdrh insert_cleanup: 1041633e6d57Sdrh sqlite3SrcListDelete(db, pTabList); 1042633e6d57Sdrh sqlite3ExprListDelete(db, pList); 1043633e6d57Sdrh sqlite3SelectDelete(db, pSelect); 1044633e6d57Sdrh sqlite3IdListDelete(db, pColumn); 1045633e6d57Sdrh sqlite3DbFree(db, aRegIdx); 1046cce7d176Sdrh } 10479cfcf5d4Sdrh 104875cbd984Sdan /* Make sure "isView" and other macros defined above are undefined. Otherwise 104975cbd984Sdan ** thely may interfere with compilation of other functions in this file 105075cbd984Sdan ** (or in another file, if this file becomes part of the amalgamation). */ 105175cbd984Sdan #ifdef isView 105275cbd984Sdan #undef isView 105375cbd984Sdan #endif 105475cbd984Sdan #ifdef pTrigger 105575cbd984Sdan #undef pTrigger 105675cbd984Sdan #endif 105775cbd984Sdan #ifdef tmask 105875cbd984Sdan #undef tmask 105975cbd984Sdan #endif 106075cbd984Sdan 106175cbd984Sdan 10629cfcf5d4Sdrh /* 10636a288a33Sdrh ** Generate code to do constraint checks prior to an INSERT or an UPDATE. 10649cfcf5d4Sdrh ** 106504adf416Sdrh ** The input is a range of consecutive registers as follows: 10660ca3e24bSdrh ** 106765a7cd16Sdan ** 1. The rowid of the row after the update. 10680ca3e24bSdrh ** 106965a7cd16Sdan ** 2. The data in the first column of the entry after the update. 10700ca3e24bSdrh ** 10710ca3e24bSdrh ** i. Data from middle columns... 10720ca3e24bSdrh ** 10730ca3e24bSdrh ** N. The data in the last column of the entry after the update. 10740ca3e24bSdrh ** 107565a7cd16Sdan ** The regRowid parameter is the index of the register containing (1). 107604adf416Sdrh ** 107765a7cd16Sdan ** If isUpdate is true and rowidChng is non-zero, then rowidChng contains 107865a7cd16Sdan ** the address of a register containing the rowid before the update takes 107965a7cd16Sdan ** place. isUpdate is true for UPDATEs and false for INSERTs. If isUpdate 108065a7cd16Sdan ** is false, indicating an INSERT statement, then a non-zero rowidChng 108165a7cd16Sdan ** indicates that the rowid was explicitly specified as part of the 108265a7cd16Sdan ** INSERT statement. If rowidChng is false, it means that the rowid is 108365a7cd16Sdan ** computed automatically in an insert or that the rowid value is not 108465a7cd16Sdan ** modified by an update. 10850ca3e24bSdrh ** 1086aa9b8963Sdrh ** The code generated by this routine store new index entries into 1087aa9b8963Sdrh ** registers identified by aRegIdx[]. No index entry is created for 1088aa9b8963Sdrh ** indices where aRegIdx[i]==0. The order of indices in aRegIdx[] is 1089aa9b8963Sdrh ** the same as the order of indices on the linked list of indices 1090aa9b8963Sdrh ** attached to the table. 10919cfcf5d4Sdrh ** 10929cfcf5d4Sdrh ** This routine also generates code to check constraints. NOT NULL, 10939cfcf5d4Sdrh ** CHECK, and UNIQUE constraints are all checked. If a constraint fails, 10941c92853dSdrh ** then the appropriate action is performed. There are five possible 10951c92853dSdrh ** actions: ROLLBACK, ABORT, FAIL, REPLACE, and IGNORE. 10969cfcf5d4Sdrh ** 10979cfcf5d4Sdrh ** Constraint type Action What Happens 10989cfcf5d4Sdrh ** --------------- ---------- ---------------------------------------- 10991c92853dSdrh ** any ROLLBACK The current transaction is rolled back and 110024b03fd0Sdanielk1977 ** sqlite3_exec() returns immediately with a 11019cfcf5d4Sdrh ** return code of SQLITE_CONSTRAINT. 11029cfcf5d4Sdrh ** 11031c92853dSdrh ** any ABORT Back out changes from the current command 11041c92853dSdrh ** only (do not do a complete rollback) then 110524b03fd0Sdanielk1977 ** cause sqlite3_exec() to return immediately 11061c92853dSdrh ** with SQLITE_CONSTRAINT. 11071c92853dSdrh ** 11081c92853dSdrh ** any FAIL Sqlite_exec() returns immediately with a 11091c92853dSdrh ** return code of SQLITE_CONSTRAINT. The 11101c92853dSdrh ** transaction is not rolled back and any 11111c92853dSdrh ** prior changes are retained. 11121c92853dSdrh ** 11139cfcf5d4Sdrh ** any IGNORE The record number and data is popped from 11149cfcf5d4Sdrh ** the stack and there is an immediate jump 11159cfcf5d4Sdrh ** to label ignoreDest. 11169cfcf5d4Sdrh ** 11179cfcf5d4Sdrh ** NOT NULL REPLACE The NULL value is replace by the default 11189cfcf5d4Sdrh ** value for that column. If the default value 11199cfcf5d4Sdrh ** is NULL, the action is the same as ABORT. 11209cfcf5d4Sdrh ** 11219cfcf5d4Sdrh ** UNIQUE REPLACE The other row that conflicts with the row 11229cfcf5d4Sdrh ** being inserted is removed. 11239cfcf5d4Sdrh ** 11249cfcf5d4Sdrh ** CHECK REPLACE Illegal. The results in an exception. 11259cfcf5d4Sdrh ** 11261c92853dSdrh ** Which action to take is determined by the overrideError parameter. 11271c92853dSdrh ** Or if overrideError==OE_Default, then the pParse->onError parameter 11281c92853dSdrh ** is used. Or if pParse->onError==OE_Default then the onError value 11291c92853dSdrh ** for the constraint is used. 11309cfcf5d4Sdrh ** 1131aaab5725Sdrh ** The calling routine must open a read/write cursor for pTab with 113204adf416Sdrh ** cursor number "baseCur". All indices of pTab must also have open 113304adf416Sdrh ** read/write cursors with cursor number baseCur+i for the i-th cursor. 11349cfcf5d4Sdrh ** Except, if there is no possibility of a REPLACE action then 1135aa9b8963Sdrh ** cursors do not need to be open for indices where aRegIdx[i]==0. 11369cfcf5d4Sdrh */ 11374adee20fSdanielk1977 void sqlite3GenerateConstraintChecks( 11389cfcf5d4Sdrh Parse *pParse, /* The parser context */ 11399cfcf5d4Sdrh Table *pTab, /* the table into which we are inserting */ 114004adf416Sdrh int baseCur, /* Index of a read/write cursor pointing at pTab */ 114104adf416Sdrh int regRowid, /* Index of the range of input registers */ 1142aa9b8963Sdrh int *aRegIdx, /* Register used by each index. 0 for unused indices */ 1143a05a722fSdrh int rowidChng, /* True if the rowid might collide with existing entry */ 1144b419a926Sdrh int isUpdate, /* True for UPDATE, False for INSERT */ 11459cfcf5d4Sdrh int overrideError, /* Override onError to this if not OE_Default */ 1146de630353Sdanielk1977 int ignoreDest, /* Jump to this label on an OE_Ignore resolution */ 1147de630353Sdanielk1977 int *pbMayReplace /* OUT: Set to true if constraint may cause a replace */ 11489cfcf5d4Sdrh ){ 11491b7ecbb4Sdrh int i; /* loop counter */ 11501b7ecbb4Sdrh Vdbe *v; /* VDBE under constrution */ 11511b7ecbb4Sdrh int nCol; /* Number of columns */ 11521b7ecbb4Sdrh int onError; /* Conflict resolution strategy */ 11531bd10f8aSdrh int j1; /* Addresss of jump instruction */ 11541bd10f8aSdrh int j2 = 0, j3; /* Addresses of jump instructions */ 115504adf416Sdrh int regData; /* Register containing first data column */ 11561b7ecbb4Sdrh int iCur; /* Table cursor number */ 11571b7ecbb4Sdrh Index *pIdx; /* Pointer to one of the indices */ 11581b7ecbb4Sdrh int seenReplace = 0; /* True if REPLACE is used to resolve INT PK conflict */ 115965a7cd16Sdan int regOldRowid = (rowidChng && isUpdate) ? rowidChng : regRowid; 11609cfcf5d4Sdrh 11614adee20fSdanielk1977 v = sqlite3GetVdbe(pParse); 11629cfcf5d4Sdrh assert( v!=0 ); 1163417be79cSdrh assert( pTab->pSelect==0 ); /* This table is not a VIEW */ 11649cfcf5d4Sdrh nCol = pTab->nCol; 1165aa9b8963Sdrh regData = regRowid + 1; 1166aa9b8963Sdrh 11679cfcf5d4Sdrh /* Test all NOT NULL constraints. 11689cfcf5d4Sdrh */ 11699cfcf5d4Sdrh for(i=0; i<nCol; i++){ 11700ca3e24bSdrh if( i==pTab->iPKey ){ 11710ca3e24bSdrh continue; 11720ca3e24bSdrh } 11739cfcf5d4Sdrh onError = pTab->aCol[i].notNull; 11740ca3e24bSdrh if( onError==OE_None ) continue; 11759cfcf5d4Sdrh if( overrideError!=OE_Default ){ 11769cfcf5d4Sdrh onError = overrideError; 1177a996e477Sdrh }else if( onError==OE_Default ){ 1178a996e477Sdrh onError = OE_Abort; 11799cfcf5d4Sdrh } 11807977a17fSdanielk1977 if( onError==OE_Replace && pTab->aCol[i].pDflt==0 ){ 11819cfcf5d4Sdrh onError = OE_Abort; 11829cfcf5d4Sdrh } 1183b84f96f8Sdanielk1977 assert( onError==OE_Rollback || onError==OE_Abort || onError==OE_Fail 1184b84f96f8Sdanielk1977 || onError==OE_Ignore || onError==OE_Replace ); 11859cfcf5d4Sdrh switch( onError ){ 11861c92853dSdrh case OE_Abort: 1187e0af83acSdan sqlite3MayAbort(pParse); 1188e0af83acSdan case OE_Rollback: 11891c92853dSdrh case OE_Fail: { 1190f089aa45Sdrh char *zMsg; 11915053a79bSdrh j1 = sqlite3VdbeAddOp3(v, OP_HaltIfNull, 11925053a79bSdrh SQLITE_CONSTRAINT, onError, regData+i); 1193f089aa45Sdrh zMsg = sqlite3MPrintf(pParse->db, "%s.%s may not be NULL", 1194f089aa45Sdrh pTab->zName, pTab->aCol[i].zName); 119566a5167bSdrh sqlite3VdbeChangeP4(v, -1, zMsg, P4_DYNAMIC); 11969cfcf5d4Sdrh break; 11979cfcf5d4Sdrh } 11989cfcf5d4Sdrh case OE_Ignore: { 11995053a79bSdrh sqlite3VdbeAddOp2(v, OP_IsNull, regData+i, ignoreDest); 12009cfcf5d4Sdrh break; 12019cfcf5d4Sdrh } 1202098d1684Sdrh default: { 1203098d1684Sdrh assert( onError==OE_Replace ); 12045053a79bSdrh j1 = sqlite3VdbeAddOp1(v, OP_NotNull, regData+i); 120504adf416Sdrh sqlite3ExprCode(pParse, pTab->aCol[i].pDflt, regData+i); 12065053a79bSdrh sqlite3VdbeJumpHere(v, j1); 12079cfcf5d4Sdrh break; 12089cfcf5d4Sdrh } 12099cfcf5d4Sdrh } 12109cfcf5d4Sdrh } 12119cfcf5d4Sdrh 12129cfcf5d4Sdrh /* Test all CHECK constraints 12139cfcf5d4Sdrh */ 1214ffe07b2dSdrh #ifndef SQLITE_OMIT_CHECK 12150cd2d4c9Sdrh if( pTab->pCheck && (pParse->db->flags & SQLITE_IgnoreChecks)==0 ){ 1216ffe07b2dSdrh int allOk = sqlite3VdbeMakeLabel(v); 1217aa9b8963Sdrh pParse->ckBase = regData; 121835573356Sdrh sqlite3ExprIfTrue(pParse, pTab->pCheck, allOk, SQLITE_JUMPIFNULL); 1219aa01c7e2Sdrh onError = overrideError!=OE_Default ? overrideError : OE_Abort; 12202e06c67cSdrh if( onError==OE_Ignore ){ 122166a5167bSdrh sqlite3VdbeAddOp2(v, OP_Goto, 0, ignoreDest); 1222aa01c7e2Sdrh }else{ 1223e0af83acSdan sqlite3HaltConstraint(pParse, onError, 0, 0); 1224aa01c7e2Sdrh } 1225ffe07b2dSdrh sqlite3VdbeResolveLabel(v, allOk); 1226ffe07b2dSdrh } 1227ffe07b2dSdrh #endif /* !defined(SQLITE_OMIT_CHECK) */ 12289cfcf5d4Sdrh 12290bd1f4eaSdrh /* If we have an INTEGER PRIMARY KEY, make sure the primary key 12300bd1f4eaSdrh ** of the new record does not previously exist. Except, if this 12310bd1f4eaSdrh ** is an UPDATE and the primary key is not changing, that is OK. 12329cfcf5d4Sdrh */ 1233f0863fe5Sdrh if( rowidChng ){ 12340ca3e24bSdrh onError = pTab->keyConf; 12350ca3e24bSdrh if( overrideError!=OE_Default ){ 12360ca3e24bSdrh onError = overrideError; 1237a996e477Sdrh }else if( onError==OE_Default ){ 1238a996e477Sdrh onError = OE_Abort; 12390ca3e24bSdrh } 1240a0217ba7Sdrh 124179b0c956Sdrh if( isUpdate ){ 124276d462eeSdan j2 = sqlite3VdbeAddOp3(v, OP_Eq, regRowid, 0, rowidChng); 124379b0c956Sdrh } 124404adf416Sdrh j3 = sqlite3VdbeAddOp3(v, OP_NotExists, baseCur, 0, regRowid); 12450ca3e24bSdrh switch( onError ){ 1246a0217ba7Sdrh default: { 1247a0217ba7Sdrh onError = OE_Abort; 1248a0217ba7Sdrh /* Fall thru into the next case */ 1249a0217ba7Sdrh } 12501c92853dSdrh case OE_Rollback: 12511c92853dSdrh case OE_Abort: 12521c92853dSdrh case OE_Fail: { 1253e0af83acSdan sqlite3HaltConstraint( 1254e0af83acSdan pParse, onError, "PRIMARY KEY must be unique", P4_STATIC); 12550ca3e24bSdrh break; 12560ca3e24bSdrh } 12575383ae5cSdrh case OE_Replace: { 12582283d46cSdan /* If there are DELETE triggers on this table and the 12592283d46cSdan ** recursive-triggers flag is set, call GenerateRowDelete() to 12602283d46cSdan ** remove the conflicting row from the the table. This will fire 12612283d46cSdan ** the triggers and remove both the table and index b-tree entries. 12622283d46cSdan ** 12632283d46cSdan ** Otherwise, if there are no triggers or the recursive-triggers 1264da730f6eSdan ** flag is not set, but the table has one or more indexes, call 1265da730f6eSdan ** GenerateRowIndexDelete(). This removes the index b-tree entries 1266da730f6eSdan ** only. The table b-tree entry will be replaced by the new entry 1267da730f6eSdan ** when it is inserted. 1268da730f6eSdan ** 1269da730f6eSdan ** If either GenerateRowDelete() or GenerateRowIndexDelete() is called, 1270da730f6eSdan ** also invoke MultiWrite() to indicate that this VDBE may require 1271da730f6eSdan ** statement rollback (if the statement is aborted after the delete 1272da730f6eSdan ** takes place). Earlier versions called sqlite3MultiWrite() regardless, 1273da730f6eSdan ** but being more selective here allows statements like: 1274da730f6eSdan ** 1275da730f6eSdan ** REPLACE INTO t(rowid) VALUES($newrowid) 1276da730f6eSdan ** 1277da730f6eSdan ** to run without a statement journal if there are no indexes on the 1278da730f6eSdan ** table. 1279da730f6eSdan */ 12802283d46cSdan Trigger *pTrigger = 0; 12812283d46cSdan if( pParse->db->flags&SQLITE_RecTriggers ){ 12822283d46cSdan pTrigger = sqlite3TriggersExist(pParse, pTab, TK_DELETE, 0, 0); 12832283d46cSdan } 1284e7a94d81Sdan if( pTrigger || sqlite3FkRequired(pParse, pTab, 0, 0) ){ 1285da730f6eSdan sqlite3MultiWrite(pParse); 12862283d46cSdan sqlite3GenerateRowDelete( 12872283d46cSdan pParse, pTab, baseCur, regRowid, 0, pTrigger, OE_Replace 12882283d46cSdan ); 1289da730f6eSdan }else if( pTab->pIndex ){ 1290da730f6eSdan sqlite3MultiWrite(pParse); 12912d401ab8Sdrh sqlite3GenerateRowIndexDelete(pParse, pTab, baseCur, 0); 12922283d46cSdan } 12935383ae5cSdrh seenReplace = 1; 12945383ae5cSdrh break; 12955383ae5cSdrh } 12960ca3e24bSdrh case OE_Ignore: { 12975383ae5cSdrh assert( seenReplace==0 ); 129866a5167bSdrh sqlite3VdbeAddOp2(v, OP_Goto, 0, ignoreDest); 12990ca3e24bSdrh break; 13000ca3e24bSdrh } 13010ca3e24bSdrh } 1302aa9b8963Sdrh sqlite3VdbeJumpHere(v, j3); 1303f5905aa7Sdrh if( isUpdate ){ 1304aa9b8963Sdrh sqlite3VdbeJumpHere(v, j2); 1305a05a722fSdrh } 13060ca3e24bSdrh } 13070bd1f4eaSdrh 13080bd1f4eaSdrh /* Test all UNIQUE constraints by creating entries for each UNIQUE 13090bd1f4eaSdrh ** index and making sure that duplicate entries do not already exist. 13100bd1f4eaSdrh ** Add the new records to the indices as we go. 13110bd1f4eaSdrh */ 1312b2fe7d8cSdrh for(iCur=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, iCur++){ 13132d401ab8Sdrh int regIdx; 13142d401ab8Sdrh int regR; 13152d401ab8Sdrh 1316aa9b8963Sdrh if( aRegIdx[iCur]==0 ) continue; /* Skip unused indices */ 1317b2fe7d8cSdrh 1318b2fe7d8cSdrh /* Create a key for accessing the index entry */ 13192d401ab8Sdrh regIdx = sqlite3GetTempRange(pParse, pIdx->nColumn+1); 13209cfcf5d4Sdrh for(i=0; i<pIdx->nColumn; i++){ 13219cfcf5d4Sdrh int idx = pIdx->aiColumn[i]; 13229cfcf5d4Sdrh if( idx==pTab->iPKey ){ 13232d401ab8Sdrh sqlite3VdbeAddOp2(v, OP_SCopy, regRowid, regIdx+i); 13249cfcf5d4Sdrh }else{ 13252d401ab8Sdrh sqlite3VdbeAddOp2(v, OP_SCopy, regData+idx, regIdx+i); 13269cfcf5d4Sdrh } 13279cfcf5d4Sdrh } 13282d401ab8Sdrh sqlite3VdbeAddOp2(v, OP_SCopy, regRowid, regIdx+i); 13291db639ceSdrh sqlite3VdbeAddOp3(v, OP_MakeRecord, regIdx, pIdx->nColumn+1, aRegIdx[iCur]); 133069f8bb9cSdan sqlite3VdbeChangeP4(v, -1, sqlite3IndexAffinityStr(v, pIdx), 0); 1331da250ea5Sdrh sqlite3ExprCacheAffinityChange(pParse, regIdx, pIdx->nColumn+1); 1332b2fe7d8cSdrh 1333b2fe7d8cSdrh /* Find out what action to take in case there is an indexing conflict */ 13349cfcf5d4Sdrh onError = pIdx->onError; 1335de630353Sdanielk1977 if( onError==OE_None ){ 1336de630353Sdanielk1977 sqlite3ReleaseTempRange(pParse, regIdx, pIdx->nColumn+1); 1337de630353Sdanielk1977 continue; /* pIdx is not a UNIQUE index */ 1338de630353Sdanielk1977 } 13399cfcf5d4Sdrh if( overrideError!=OE_Default ){ 13409cfcf5d4Sdrh onError = overrideError; 1341a996e477Sdrh }else if( onError==OE_Default ){ 1342a996e477Sdrh onError = OE_Abort; 13439cfcf5d4Sdrh } 13445383ae5cSdrh if( seenReplace ){ 13455383ae5cSdrh if( onError==OE_Ignore ) onError = OE_Replace; 13465383ae5cSdrh else if( onError==OE_Fail ) onError = OE_Abort; 13475383ae5cSdrh } 13485383ae5cSdrh 1349b2fe7d8cSdrh /* Check to see if the new index entry will be unique */ 13502d401ab8Sdrh regR = sqlite3GetTempReg(pParse); 135165a7cd16Sdan sqlite3VdbeAddOp2(v, OP_SCopy, regOldRowid, regR); 13522d401ab8Sdrh j3 = sqlite3VdbeAddOp4(v, OP_IsUnique, baseCur+iCur+1, 0, 1353de630353Sdanielk1977 regR, SQLITE_INT_TO_PTR(regIdx), 1354a9e852b6Smlcreech P4_INT32); 1355de630353Sdanielk1977 sqlite3ReleaseTempRange(pParse, regIdx, pIdx->nColumn+1); 1356b2fe7d8cSdrh 1357b2fe7d8cSdrh /* Generate code that executes if the new index entry is not unique */ 1358b84f96f8Sdanielk1977 assert( onError==OE_Rollback || onError==OE_Abort || onError==OE_Fail 1359b84f96f8Sdanielk1977 || onError==OE_Ignore || onError==OE_Replace ); 13609cfcf5d4Sdrh switch( onError ){ 13611c92853dSdrh case OE_Rollback: 13621c92853dSdrh case OE_Abort: 13631c92853dSdrh case OE_Fail: { 1364098d1684Sdrh int j; 1365098d1684Sdrh StrAccum errMsg; 1366098d1684Sdrh const char *zSep; 1367098d1684Sdrh char *zErr; 1368098d1684Sdrh 1369098d1684Sdrh sqlite3StrAccumInit(&errMsg, 0, 0, 200); 1370098d1684Sdrh errMsg.db = pParse->db; 1371098d1684Sdrh zSep = pIdx->nColumn>1 ? "columns " : "column "; 1372098d1684Sdrh for(j=0; j<pIdx->nColumn; j++){ 137337ed48edSdrh char *zCol = pTab->aCol[pIdx->aiColumn[j]].zName; 1374098d1684Sdrh sqlite3StrAccumAppend(&errMsg, zSep, -1); 1375098d1684Sdrh zSep = ", "; 1376098d1684Sdrh sqlite3StrAccumAppend(&errMsg, zCol, -1); 137737ed48edSdrh } 1378098d1684Sdrh sqlite3StrAccumAppend(&errMsg, 1379098d1684Sdrh pIdx->nColumn>1 ? " are not unique" : " is not unique", -1); 1380098d1684Sdrh zErr = sqlite3StrAccumFinish(&errMsg); 1381e0af83acSdan sqlite3HaltConstraint(pParse, onError, zErr, 0); 1382098d1684Sdrh sqlite3DbFree(errMsg.db, zErr); 13839cfcf5d4Sdrh break; 13849cfcf5d4Sdrh } 13859cfcf5d4Sdrh case OE_Ignore: { 13860ca3e24bSdrh assert( seenReplace==0 ); 138766a5167bSdrh sqlite3VdbeAddOp2(v, OP_Goto, 0, ignoreDest); 13889cfcf5d4Sdrh break; 13899cfcf5d4Sdrh } 1390098d1684Sdrh default: { 13912283d46cSdan Trigger *pTrigger = 0; 1392098d1684Sdrh assert( onError==OE_Replace ); 13931bea559aSdan sqlite3MultiWrite(pParse); 13942283d46cSdan if( pParse->db->flags&SQLITE_RecTriggers ){ 13952283d46cSdan pTrigger = sqlite3TriggersExist(pParse, pTab, TK_DELETE, 0, 0); 13962283d46cSdan } 13972283d46cSdan sqlite3GenerateRowDelete( 13982283d46cSdan pParse, pTab, baseCur, regR, 0, pTrigger, OE_Replace 13992283d46cSdan ); 14000ca3e24bSdrh seenReplace = 1; 14019cfcf5d4Sdrh break; 14029cfcf5d4Sdrh } 14039cfcf5d4Sdrh } 14042d401ab8Sdrh sqlite3VdbeJumpHere(v, j3); 14052d401ab8Sdrh sqlite3ReleaseTempReg(pParse, regR); 14069cfcf5d4Sdrh } 1407de630353Sdanielk1977 1408de630353Sdanielk1977 if( pbMayReplace ){ 1409de630353Sdanielk1977 *pbMayReplace = seenReplace; 1410de630353Sdanielk1977 } 14119cfcf5d4Sdrh } 14120ca3e24bSdrh 14130ca3e24bSdrh /* 14140ca3e24bSdrh ** This routine generates code to finish the INSERT or UPDATE operation 14154adee20fSdanielk1977 ** that was started by a prior call to sqlite3GenerateConstraintChecks. 141604adf416Sdrh ** A consecutive range of registers starting at regRowid contains the 141704adf416Sdrh ** rowid and the content to be inserted. 14180ca3e24bSdrh ** 1419b419a926Sdrh ** The arguments to this routine should be the same as the first six 14204adee20fSdanielk1977 ** arguments to sqlite3GenerateConstraintChecks. 14210ca3e24bSdrh */ 14224adee20fSdanielk1977 void sqlite3CompleteInsertion( 14230ca3e24bSdrh Parse *pParse, /* The parser context */ 14240ca3e24bSdrh Table *pTab, /* the table into which we are inserting */ 142504adf416Sdrh int baseCur, /* Index of a read/write cursor pointing at pTab */ 142604adf416Sdrh int regRowid, /* Range of content */ 1427aa9b8963Sdrh int *aRegIdx, /* Register used by each index. 0 for unused indices */ 142870ce3f0cSdrh int isUpdate, /* True for UPDATE, False for INSERT */ 1429de630353Sdanielk1977 int appendBias, /* True if this is likely to be an append */ 1430de630353Sdanielk1977 int useSeekResult /* True to set the USESEEKRESULT flag on OP_[Idx]Insert */ 14310ca3e24bSdrh ){ 14320ca3e24bSdrh int i; 14330ca3e24bSdrh Vdbe *v; 14340ca3e24bSdrh int nIdx; 14350ca3e24bSdrh Index *pIdx; 14361bd10f8aSdrh u8 pik_flags; 143704adf416Sdrh int regData; 1438b7654111Sdrh int regRec; 14390ca3e24bSdrh 14404adee20fSdanielk1977 v = sqlite3GetVdbe(pParse); 14410ca3e24bSdrh assert( v!=0 ); 1442417be79cSdrh assert( pTab->pSelect==0 ); /* This table is not a VIEW */ 14430ca3e24bSdrh for(nIdx=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, nIdx++){} 14440ca3e24bSdrh for(i=nIdx-1; i>=0; i--){ 1445aa9b8963Sdrh if( aRegIdx[i]==0 ) continue; 144604adf416Sdrh sqlite3VdbeAddOp2(v, OP_IdxInsert, baseCur+i+1, aRegIdx[i]); 1447de630353Sdanielk1977 if( useSeekResult ){ 1448de630353Sdanielk1977 sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT); 1449de630353Sdanielk1977 } 14500ca3e24bSdrh } 145104adf416Sdrh regData = regRowid + 1; 1452b7654111Sdrh regRec = sqlite3GetTempReg(pParse); 14531db639ceSdrh sqlite3VdbeAddOp3(v, OP_MakeRecord, regData, pTab->nCol, regRec); 1454a37cdde0Sdanielk1977 sqlite3TableAffinityStr(v, pTab); 1455da250ea5Sdrh sqlite3ExprCacheAffinityChange(pParse, regData, pTab->nCol); 14564794f735Sdrh if( pParse->nested ){ 14574794f735Sdrh pik_flags = 0; 14584794f735Sdrh }else{ 145994eb6a14Sdanielk1977 pik_flags = OPFLAG_NCHANGE; 146094eb6a14Sdanielk1977 pik_flags |= (isUpdate?OPFLAG_ISUPDATE:OPFLAG_LASTROWID); 14614794f735Sdrh } 1462e4d90813Sdrh if( appendBias ){ 1463e4d90813Sdrh pik_flags |= OPFLAG_APPEND; 1464e4d90813Sdrh } 1465de630353Sdanielk1977 if( useSeekResult ){ 1466de630353Sdanielk1977 pik_flags |= OPFLAG_USESEEKRESULT; 1467de630353Sdanielk1977 } 1468b7654111Sdrh sqlite3VdbeAddOp3(v, OP_Insert, baseCur, regRec, regRowid); 146994eb6a14Sdanielk1977 if( !pParse->nested ){ 147066a5167bSdrh sqlite3VdbeChangeP4(v, -1, pTab->zName, P4_STATIC); 147194eb6a14Sdanielk1977 } 1472b7654111Sdrh sqlite3VdbeChangeP5(v, pik_flags); 14730ca3e24bSdrh } 1474cd44690aSdrh 1475cd44690aSdrh /* 1476290c1948Sdrh ** Generate code that will open cursors for a table and for all 147704adf416Sdrh ** indices of that table. The "baseCur" parameter is the cursor number used 1478cd44690aSdrh ** for the table. Indices are opened on subsequent cursors. 1479aa9b8963Sdrh ** 1480aa9b8963Sdrh ** Return the number of indices on the table. 1481cd44690aSdrh */ 1482aa9b8963Sdrh int sqlite3OpenTableAndIndices( 1483290c1948Sdrh Parse *pParse, /* Parsing context */ 1484290c1948Sdrh Table *pTab, /* Table to be opened */ 148504adf416Sdrh int baseCur, /* Cursor number assigned to the table */ 1486290c1948Sdrh int op /* OP_OpenRead or OP_OpenWrite */ 1487290c1948Sdrh ){ 1488cd44690aSdrh int i; 14894cbdda9eSdrh int iDb; 1490cd44690aSdrh Index *pIdx; 14914cbdda9eSdrh Vdbe *v; 14924cbdda9eSdrh 1493aa9b8963Sdrh if( IsVirtual(pTab) ) return 0; 14944cbdda9eSdrh iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema); 14954cbdda9eSdrh v = sqlite3GetVdbe(pParse); 1496cd44690aSdrh assert( v!=0 ); 149704adf416Sdrh sqlite3OpenTable(pParse, baseCur, iDb, pTab, op); 1498cd44690aSdrh for(i=1, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){ 1499b3bf556eSdanielk1977 KeyInfo *pKey = sqlite3IndexKeyinfo(pParse, pIdx); 1500da184236Sdanielk1977 assert( pIdx->pSchema==pTab->pSchema ); 150104adf416Sdrh sqlite3VdbeAddOp4(v, op, i+baseCur, pIdx->tnum, iDb, 150266a5167bSdrh (char*)pKey, P4_KEYINFO_HANDOFF); 1503207872a4Sdanielk1977 VdbeComment((v, "%s", pIdx->zName)); 1504cd44690aSdrh } 15051b7ecbb4Sdrh if( pParse->nTab<baseCur+i ){ 150604adf416Sdrh pParse->nTab = baseCur+i; 1507290c1948Sdrh } 1508aa9b8963Sdrh return i-1; 1509cd44690aSdrh } 15109d9cf229Sdrh 151191c58e23Sdrh 151291c58e23Sdrh #ifdef SQLITE_TEST 151391c58e23Sdrh /* 151491c58e23Sdrh ** The following global variable is incremented whenever the 151591c58e23Sdrh ** transfer optimization is used. This is used for testing 151691c58e23Sdrh ** purposes only - to make sure the transfer optimization really 151791c58e23Sdrh ** is happening when it is suppose to. 151891c58e23Sdrh */ 151991c58e23Sdrh int sqlite3_xferopt_count; 152091c58e23Sdrh #endif /* SQLITE_TEST */ 152191c58e23Sdrh 152291c58e23Sdrh 15239d9cf229Sdrh #ifndef SQLITE_OMIT_XFER_OPT 15249d9cf229Sdrh /* 15259d9cf229Sdrh ** Check to collation names to see if they are compatible. 15269d9cf229Sdrh */ 15279d9cf229Sdrh static int xferCompatibleCollation(const char *z1, const char *z2){ 15289d9cf229Sdrh if( z1==0 ){ 15299d9cf229Sdrh return z2==0; 15309d9cf229Sdrh } 15319d9cf229Sdrh if( z2==0 ){ 15329d9cf229Sdrh return 0; 15339d9cf229Sdrh } 15349d9cf229Sdrh return sqlite3StrICmp(z1, z2)==0; 15359d9cf229Sdrh } 15369d9cf229Sdrh 15379d9cf229Sdrh 15389d9cf229Sdrh /* 15399d9cf229Sdrh ** Check to see if index pSrc is compatible as a source of data 15409d9cf229Sdrh ** for index pDest in an insert transfer optimization. The rules 15419d9cf229Sdrh ** for a compatible index: 15429d9cf229Sdrh ** 15439d9cf229Sdrh ** * The index is over the same set of columns 15449d9cf229Sdrh ** * The same DESC and ASC markings occurs on all columns 15459d9cf229Sdrh ** * The same onError processing (OE_Abort, OE_Ignore, etc) 15469d9cf229Sdrh ** * The same collating sequence on each column 15479d9cf229Sdrh */ 15489d9cf229Sdrh static int xferCompatibleIndex(Index *pDest, Index *pSrc){ 15499d9cf229Sdrh int i; 15509d9cf229Sdrh assert( pDest && pSrc ); 15519d9cf229Sdrh assert( pDest->pTable!=pSrc->pTable ); 15529d9cf229Sdrh if( pDest->nColumn!=pSrc->nColumn ){ 15539d9cf229Sdrh return 0; /* Different number of columns */ 15549d9cf229Sdrh } 15559d9cf229Sdrh if( pDest->onError!=pSrc->onError ){ 15569d9cf229Sdrh return 0; /* Different conflict resolution strategies */ 15579d9cf229Sdrh } 15589d9cf229Sdrh for(i=0; i<pSrc->nColumn; i++){ 15599d9cf229Sdrh if( pSrc->aiColumn[i]!=pDest->aiColumn[i] ){ 15609d9cf229Sdrh return 0; /* Different columns indexed */ 15619d9cf229Sdrh } 15629d9cf229Sdrh if( pSrc->aSortOrder[i]!=pDest->aSortOrder[i] ){ 15639d9cf229Sdrh return 0; /* Different sort orders */ 15649d9cf229Sdrh } 15653f6e781dSdrh if( !xferCompatibleCollation(pSrc->azColl[i],pDest->azColl[i]) ){ 156660a713c6Sdrh return 0; /* Different collating sequences */ 15679d9cf229Sdrh } 15689d9cf229Sdrh } 15699d9cf229Sdrh 15709d9cf229Sdrh /* If no test above fails then the indices must be compatible */ 15719d9cf229Sdrh return 1; 15729d9cf229Sdrh } 15739d9cf229Sdrh 15749d9cf229Sdrh /* 15759d9cf229Sdrh ** Attempt the transfer optimization on INSERTs of the form 15769d9cf229Sdrh ** 15779d9cf229Sdrh ** INSERT INTO tab1 SELECT * FROM tab2; 15789d9cf229Sdrh ** 15799d9cf229Sdrh ** This optimization is only attempted if 15809d9cf229Sdrh ** 15819d9cf229Sdrh ** (1) tab1 and tab2 have identical schemas including all the 15828103b7d2Sdrh ** same indices and constraints 15839d9cf229Sdrh ** 15849d9cf229Sdrh ** (2) tab1 and tab2 are different tables 15859d9cf229Sdrh ** 15869d9cf229Sdrh ** (3) There must be no triggers on tab1 15879d9cf229Sdrh ** 15889d9cf229Sdrh ** (4) The result set of the SELECT statement is "*" 15899d9cf229Sdrh ** 15909d9cf229Sdrh ** (5) The SELECT statement has no WHERE, HAVING, ORDER BY, GROUP BY, 15919d9cf229Sdrh ** or LIMIT clause. 15929d9cf229Sdrh ** 15939d9cf229Sdrh ** (6) The SELECT statement is a simple (not a compound) select that 15949d9cf229Sdrh ** contains only tab2 in its FROM clause 15959d9cf229Sdrh ** 15969d9cf229Sdrh ** This method for implementing the INSERT transfers raw records from 15979d9cf229Sdrh ** tab2 over to tab1. The columns are not decoded. Raw records from 15989d9cf229Sdrh ** the indices of tab2 are transfered to tab1 as well. In so doing, 15999d9cf229Sdrh ** the resulting tab1 has much less fragmentation. 16009d9cf229Sdrh ** 16019d9cf229Sdrh ** This routine returns TRUE if the optimization is attempted. If any 16029d9cf229Sdrh ** of the conditions above fail so that the optimization should not 16039d9cf229Sdrh ** be attempted, then this routine returns FALSE. 16049d9cf229Sdrh */ 16059d9cf229Sdrh static int xferOptimization( 16069d9cf229Sdrh Parse *pParse, /* Parser context */ 16079d9cf229Sdrh Table *pDest, /* The table we are inserting into */ 16089d9cf229Sdrh Select *pSelect, /* A SELECT statement to use as the data source */ 16099d9cf229Sdrh int onError, /* How to handle constraint errors */ 16109d9cf229Sdrh int iDbDest /* The database of pDest */ 16119d9cf229Sdrh ){ 16129d9cf229Sdrh ExprList *pEList; /* The result set of the SELECT */ 16139d9cf229Sdrh Table *pSrc; /* The table in the FROM clause of SELECT */ 16149d9cf229Sdrh Index *pSrcIdx, *pDestIdx; /* Source and destination indices */ 16159d9cf229Sdrh struct SrcList_item *pItem; /* An element of pSelect->pSrc */ 16169d9cf229Sdrh int i; /* Loop counter */ 16179d9cf229Sdrh int iDbSrc; /* The database of pSrc */ 16189d9cf229Sdrh int iSrc, iDest; /* Cursors from source and destination */ 16199d9cf229Sdrh int addr1, addr2; /* Loop addresses */ 16209d9cf229Sdrh int emptyDestTest; /* Address of test for empty pDest */ 16219d9cf229Sdrh int emptySrcTest; /* Address of test for empty pSrc */ 16229d9cf229Sdrh Vdbe *v; /* The VDBE we are building */ 16239d9cf229Sdrh KeyInfo *pKey; /* Key information for an index */ 16246a288a33Sdrh int regAutoinc; /* Memory register used by AUTOINC */ 1625f33c9fadSdrh int destHasUniqueIdx = 0; /* True if pDest has a UNIQUE index */ 1626b7654111Sdrh int regData, regRowid; /* Registers holding data and rowid */ 16279d9cf229Sdrh 16289d9cf229Sdrh if( pSelect==0 ){ 16299d9cf229Sdrh return 0; /* Must be of the form INSERT INTO ... SELECT ... */ 16309d9cf229Sdrh } 16312f886d1dSdanielk1977 if( sqlite3TriggerList(pParse, pDest) ){ 16329d9cf229Sdrh return 0; /* tab1 must not have triggers */ 16339d9cf229Sdrh } 16349d9cf229Sdrh #ifndef SQLITE_OMIT_VIRTUALTABLE 16357d10d5a6Sdrh if( pDest->tabFlags & TF_Virtual ){ 16369d9cf229Sdrh return 0; /* tab1 must not be a virtual table */ 16379d9cf229Sdrh } 16389d9cf229Sdrh #endif 16399d9cf229Sdrh if( onError==OE_Default ){ 16409d9cf229Sdrh onError = OE_Abort; 16419d9cf229Sdrh } 16429d9cf229Sdrh if( onError!=OE_Abort && onError!=OE_Rollback ){ 16439d9cf229Sdrh return 0; /* Cannot do OR REPLACE or OR IGNORE or OR FAIL */ 16449d9cf229Sdrh } 16455ce240a6Sdanielk1977 assert(pSelect->pSrc); /* allocated even if there is no FROM clause */ 16469d9cf229Sdrh if( pSelect->pSrc->nSrc!=1 ){ 16479d9cf229Sdrh return 0; /* FROM clause must have exactly one term */ 16489d9cf229Sdrh } 16499d9cf229Sdrh if( pSelect->pSrc->a[0].pSelect ){ 16509d9cf229Sdrh return 0; /* FROM clause cannot contain a subquery */ 16519d9cf229Sdrh } 16529d9cf229Sdrh if( pSelect->pWhere ){ 16539d9cf229Sdrh return 0; /* SELECT may not have a WHERE clause */ 16549d9cf229Sdrh } 16559d9cf229Sdrh if( pSelect->pOrderBy ){ 16569d9cf229Sdrh return 0; /* SELECT may not have an ORDER BY clause */ 16579d9cf229Sdrh } 16588103b7d2Sdrh /* Do not need to test for a HAVING clause. If HAVING is present but 16598103b7d2Sdrh ** there is no ORDER BY, we will get an error. */ 16609d9cf229Sdrh if( pSelect->pGroupBy ){ 16619d9cf229Sdrh return 0; /* SELECT may not have a GROUP BY clause */ 16629d9cf229Sdrh } 16639d9cf229Sdrh if( pSelect->pLimit ){ 16649d9cf229Sdrh return 0; /* SELECT may not have a LIMIT clause */ 16659d9cf229Sdrh } 16668103b7d2Sdrh assert( pSelect->pOffset==0 ); /* Must be so if pLimit==0 */ 16679d9cf229Sdrh if( pSelect->pPrior ){ 16689d9cf229Sdrh return 0; /* SELECT may not be a compound query */ 16699d9cf229Sdrh } 16707d10d5a6Sdrh if( pSelect->selFlags & SF_Distinct ){ 16719d9cf229Sdrh return 0; /* SELECT may not be DISTINCT */ 16729d9cf229Sdrh } 16739d9cf229Sdrh pEList = pSelect->pEList; 16749d9cf229Sdrh assert( pEList!=0 ); 16759d9cf229Sdrh if( pEList->nExpr!=1 ){ 16769d9cf229Sdrh return 0; /* The result set must have exactly one column */ 16779d9cf229Sdrh } 16789d9cf229Sdrh assert( pEList->a[0].pExpr ); 16799d9cf229Sdrh if( pEList->a[0].pExpr->op!=TK_ALL ){ 16809d9cf229Sdrh return 0; /* The result set must be the special operator "*" */ 16819d9cf229Sdrh } 16829d9cf229Sdrh 16839d9cf229Sdrh /* At this point we have established that the statement is of the 16849d9cf229Sdrh ** correct syntactic form to participate in this optimization. Now 16859d9cf229Sdrh ** we have to check the semantics. 16869d9cf229Sdrh */ 16879d9cf229Sdrh pItem = pSelect->pSrc->a; 1688ca424114Sdrh pSrc = sqlite3LocateTable(pParse, 0, pItem->zName, pItem->zDatabase); 16899d9cf229Sdrh if( pSrc==0 ){ 16909d9cf229Sdrh return 0; /* FROM clause does not contain a real table */ 16919d9cf229Sdrh } 16929d9cf229Sdrh if( pSrc==pDest ){ 16939d9cf229Sdrh return 0; /* tab1 and tab2 may not be the same table */ 16949d9cf229Sdrh } 16959d9cf229Sdrh #ifndef SQLITE_OMIT_VIRTUALTABLE 16967d10d5a6Sdrh if( pSrc->tabFlags & TF_Virtual ){ 16979d9cf229Sdrh return 0; /* tab2 must not be a virtual table */ 16989d9cf229Sdrh } 16999d9cf229Sdrh #endif 17009d9cf229Sdrh if( pSrc->pSelect ){ 17019d9cf229Sdrh return 0; /* tab2 may not be a view */ 17029d9cf229Sdrh } 17039d9cf229Sdrh if( pDest->nCol!=pSrc->nCol ){ 17049d9cf229Sdrh return 0; /* Number of columns must be the same in tab1 and tab2 */ 17059d9cf229Sdrh } 17069d9cf229Sdrh if( pDest->iPKey!=pSrc->iPKey ){ 17079d9cf229Sdrh return 0; /* Both tables must have the same INTEGER PRIMARY KEY */ 17089d9cf229Sdrh } 17099d9cf229Sdrh for(i=0; i<pDest->nCol; i++){ 17109d9cf229Sdrh if( pDest->aCol[i].affinity!=pSrc->aCol[i].affinity ){ 17119d9cf229Sdrh return 0; /* Affinity must be the same on all columns */ 17129d9cf229Sdrh } 17139d9cf229Sdrh if( !xferCompatibleCollation(pDest->aCol[i].zColl, pSrc->aCol[i].zColl) ){ 17149d9cf229Sdrh return 0; /* Collating sequence must be the same on all columns */ 17159d9cf229Sdrh } 17169d9cf229Sdrh if( pDest->aCol[i].notNull && !pSrc->aCol[i].notNull ){ 17179d9cf229Sdrh return 0; /* tab2 must be NOT NULL if tab1 is */ 17189d9cf229Sdrh } 17199d9cf229Sdrh } 17209d9cf229Sdrh for(pDestIdx=pDest->pIndex; pDestIdx; pDestIdx=pDestIdx->pNext){ 1721f33c9fadSdrh if( pDestIdx->onError!=OE_None ){ 1722f33c9fadSdrh destHasUniqueIdx = 1; 1723f33c9fadSdrh } 17249d9cf229Sdrh for(pSrcIdx=pSrc->pIndex; pSrcIdx; pSrcIdx=pSrcIdx->pNext){ 17259d9cf229Sdrh if( xferCompatibleIndex(pDestIdx, pSrcIdx) ) break; 17269d9cf229Sdrh } 17279d9cf229Sdrh if( pSrcIdx==0 ){ 17289d9cf229Sdrh return 0; /* pDestIdx has no corresponding index in pSrc */ 17299d9cf229Sdrh } 17309d9cf229Sdrh } 17317fc2f41bSdrh #ifndef SQLITE_OMIT_CHECK 17321d9da70aSdrh if( pDest->pCheck && sqlite3ExprCompare(pSrc->pCheck, pDest->pCheck) ){ 17338103b7d2Sdrh return 0; /* Tables have different CHECK constraints. Ticket #2252 */ 17348103b7d2Sdrh } 17357fc2f41bSdrh #endif 17369d9cf229Sdrh 17379d9cf229Sdrh /* If we get this far, it means either: 17389d9cf229Sdrh ** 17399d9cf229Sdrh ** * We can always do the transfer if the table contains an 17409d9cf229Sdrh ** an integer primary key 17419d9cf229Sdrh ** 17429d9cf229Sdrh ** * We can conditionally do the transfer if the destination 17439d9cf229Sdrh ** table is empty. 17449d9cf229Sdrh */ 1745dd73521bSdrh #ifdef SQLITE_TEST 1746dd73521bSdrh sqlite3_xferopt_count++; 1747dd73521bSdrh #endif 17489d9cf229Sdrh iDbSrc = sqlite3SchemaToIndex(pParse->db, pSrc->pSchema); 17499d9cf229Sdrh v = sqlite3GetVdbe(pParse); 1750f53e9b5aSdrh sqlite3CodeVerifySchema(pParse, iDbSrc); 17519d9cf229Sdrh iSrc = pParse->nTab++; 17529d9cf229Sdrh iDest = pParse->nTab++; 17536a288a33Sdrh regAutoinc = autoIncBegin(pParse, iDbDest, pDest); 17549d9cf229Sdrh sqlite3OpenTable(pParse, iDest, iDbDest, pDest, OP_OpenWrite); 1755f33c9fadSdrh if( (pDest->iPKey<0 && pDest->pIndex!=0) || destHasUniqueIdx ){ 1756bd36ba69Sdrh /* If tables do not have an INTEGER PRIMARY KEY and there 1757bd36ba69Sdrh ** are indices to be copied and the destination is not empty, 1758bd36ba69Sdrh ** we have to disallow the transfer optimization because the 1759bd36ba69Sdrh ** the rowids might change which will mess up indexing. 1760f33c9fadSdrh ** 1761f33c9fadSdrh ** Or if the destination has a UNIQUE index and is not empty, 1762f33c9fadSdrh ** we also disallow the transfer optimization because we cannot 1763f33c9fadSdrh ** insure that all entries in the union of DEST and SRC will be 1764f33c9fadSdrh ** unique. 17659d9cf229Sdrh */ 176666a5167bSdrh addr1 = sqlite3VdbeAddOp2(v, OP_Rewind, iDest, 0); 176766a5167bSdrh emptyDestTest = sqlite3VdbeAddOp2(v, OP_Goto, 0, 0); 17689d9cf229Sdrh sqlite3VdbeJumpHere(v, addr1); 17699d9cf229Sdrh }else{ 17709d9cf229Sdrh emptyDestTest = 0; 17719d9cf229Sdrh } 17729d9cf229Sdrh sqlite3OpenTable(pParse, iSrc, iDbSrc, pSrc, OP_OpenRead); 177366a5167bSdrh emptySrcTest = sqlite3VdbeAddOp2(v, OP_Rewind, iSrc, 0); 1774b7654111Sdrh regData = sqlite3GetTempReg(pParse); 1775b7654111Sdrh regRowid = sqlite3GetTempReg(pParse); 177642242dedSdrh if( pDest->iPKey>=0 ){ 1777b7654111Sdrh addr1 = sqlite3VdbeAddOp2(v, OP_Rowid, iSrc, regRowid); 1778b7654111Sdrh addr2 = sqlite3VdbeAddOp3(v, OP_NotExists, iDest, 0, regRowid); 1779e0af83acSdan sqlite3HaltConstraint( 1780e0af83acSdan pParse, onError, "PRIMARY KEY must be unique", P4_STATIC); 17819d9cf229Sdrh sqlite3VdbeJumpHere(v, addr2); 1782b7654111Sdrh autoIncStep(pParse, regAutoinc, regRowid); 1783bd36ba69Sdrh }else if( pDest->pIndex==0 ){ 1784b7654111Sdrh addr1 = sqlite3VdbeAddOp2(v, OP_NewRowid, iDest, regRowid); 178595bad4c7Sdrh }else{ 1786b7654111Sdrh addr1 = sqlite3VdbeAddOp2(v, OP_Rowid, iSrc, regRowid); 17877d10d5a6Sdrh assert( (pDest->tabFlags & TF_Autoincrement)==0 ); 178895bad4c7Sdrh } 1789b7654111Sdrh sqlite3VdbeAddOp2(v, OP_RowData, iSrc, regData); 1790b7654111Sdrh sqlite3VdbeAddOp3(v, OP_Insert, iDest, regData, regRowid); 1791b7654111Sdrh sqlite3VdbeChangeP5(v, OPFLAG_NCHANGE|OPFLAG_LASTROWID|OPFLAG_APPEND); 17921f4aa337Sdanielk1977 sqlite3VdbeChangeP4(v, -1, pDest->zName, 0); 179366a5167bSdrh sqlite3VdbeAddOp2(v, OP_Next, iSrc, addr1); 17949d9cf229Sdrh for(pDestIdx=pDest->pIndex; pDestIdx; pDestIdx=pDestIdx->pNext){ 17951b7ecbb4Sdrh for(pSrcIdx=pSrc->pIndex; ALWAYS(pSrcIdx); pSrcIdx=pSrcIdx->pNext){ 17969d9cf229Sdrh if( xferCompatibleIndex(pDestIdx, pSrcIdx) ) break; 17979d9cf229Sdrh } 17989d9cf229Sdrh assert( pSrcIdx ); 179966a5167bSdrh sqlite3VdbeAddOp2(v, OP_Close, iSrc, 0); 180066a5167bSdrh sqlite3VdbeAddOp2(v, OP_Close, iDest, 0); 18019d9cf229Sdrh pKey = sqlite3IndexKeyinfo(pParse, pSrcIdx); 1802207872a4Sdanielk1977 sqlite3VdbeAddOp4(v, OP_OpenRead, iSrc, pSrcIdx->tnum, iDbSrc, 1803207872a4Sdanielk1977 (char*)pKey, P4_KEYINFO_HANDOFF); 1804d4e70ebdSdrh VdbeComment((v, "%s", pSrcIdx->zName)); 18059d9cf229Sdrh pKey = sqlite3IndexKeyinfo(pParse, pDestIdx); 1806207872a4Sdanielk1977 sqlite3VdbeAddOp4(v, OP_OpenWrite, iDest, pDestIdx->tnum, iDbDest, 180766a5167bSdrh (char*)pKey, P4_KEYINFO_HANDOFF); 1808207872a4Sdanielk1977 VdbeComment((v, "%s", pDestIdx->zName)); 180966a5167bSdrh addr1 = sqlite3VdbeAddOp2(v, OP_Rewind, iSrc, 0); 1810b7654111Sdrh sqlite3VdbeAddOp2(v, OP_RowKey, iSrc, regData); 1811b7654111Sdrh sqlite3VdbeAddOp3(v, OP_IdxInsert, iDest, regData, 1); 181266a5167bSdrh sqlite3VdbeAddOp2(v, OP_Next, iSrc, addr1+1); 18139d9cf229Sdrh sqlite3VdbeJumpHere(v, addr1); 18149d9cf229Sdrh } 18159d9cf229Sdrh sqlite3VdbeJumpHere(v, emptySrcTest); 1816b7654111Sdrh sqlite3ReleaseTempReg(pParse, regRowid); 1817b7654111Sdrh sqlite3ReleaseTempReg(pParse, regData); 181866a5167bSdrh sqlite3VdbeAddOp2(v, OP_Close, iSrc, 0); 181966a5167bSdrh sqlite3VdbeAddOp2(v, OP_Close, iDest, 0); 18209d9cf229Sdrh if( emptyDestTest ){ 182166a5167bSdrh sqlite3VdbeAddOp2(v, OP_Halt, SQLITE_OK, 0); 18229d9cf229Sdrh sqlite3VdbeJumpHere(v, emptyDestTest); 182366a5167bSdrh sqlite3VdbeAddOp2(v, OP_Close, iDest, 0); 18249d9cf229Sdrh return 0; 18259d9cf229Sdrh }else{ 18269d9cf229Sdrh return 1; 18279d9cf229Sdrh } 18289d9cf229Sdrh } 18299d9cf229Sdrh #endif /* SQLITE_OMIT_XFER_OPT */ 1830