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 ** 15595a523aSdanielk1977 ** $Id: insert.c,v 1.270 2009/07/24 17:58:53 danielk1977 Exp $ 16cce7d176Sdrh */ 17cce7d176Sdrh #include "sqliteInt.h" 18cce7d176Sdrh 19cce7d176Sdrh /* 20bbb5e4e0Sdrh ** Generate code that will open a table for reading. 21bbb5e4e0Sdrh */ 22bbb5e4e0Sdrh void sqlite3OpenTable( 23bbb5e4e0Sdrh Parse *p, /* Generate code into this VDBE */ 24bbb5e4e0Sdrh int iCur, /* The cursor number of the table */ 25bbb5e4e0Sdrh int iDb, /* The database index in sqlite3.aDb[] */ 26bbb5e4e0Sdrh Table *pTab, /* The table to be opened */ 27bbb5e4e0Sdrh int opcode /* OP_OpenRead or OP_OpenWrite */ 28bbb5e4e0Sdrh ){ 29bbb5e4e0Sdrh Vdbe *v; 30bbb5e4e0Sdrh if( IsVirtual(pTab) ) return; 31bbb5e4e0Sdrh v = sqlite3GetVdbe(p); 32bbb5e4e0Sdrh assert( opcode==OP_OpenWrite || opcode==OP_OpenRead ); 33bbb5e4e0Sdrh sqlite3TableLock(p, iDb, pTab->tnum, (opcode==OP_OpenWrite)?1:0, pTab->zName); 34bbb5e4e0Sdrh sqlite3VdbeAddOp3(v, opcode, iCur, pTab->tnum, iDb); 35bbb5e4e0Sdrh sqlite3VdbeChangeP4(v, -1, SQLITE_INT_TO_PTR(pTab->nCol), P4_INT32); 36bbb5e4e0Sdrh VdbeComment((v, "%s", pTab->zName)); 37bbb5e4e0Sdrh } 38bbb5e4e0Sdrh 39bbb5e4e0Sdrh /* 4069f8bb9cSdan ** Return a pointer to the column affinity string associated with index 4169f8bb9cSdan ** pIdx. A column affinity string has one character for each column in 4269f8bb9cSdan ** the table, according to the affinity of the column: 433d1bfeaaSdanielk1977 ** 443d1bfeaaSdanielk1977 ** Character Column affinity 453d1bfeaaSdanielk1977 ** ------------------------------ 463eda040bSdrh ** 'a' TEXT 473eda040bSdrh ** 'b' NONE 483eda040bSdrh ** 'c' NUMERIC 493eda040bSdrh ** 'd' INTEGER 503eda040bSdrh ** 'e' REAL 512d401ab8Sdrh ** 522d401ab8Sdrh ** An extra 'b' is appended to the end of the string to cover the 532d401ab8Sdrh ** rowid that appears as the last column in every index. 5469f8bb9cSdan ** 5569f8bb9cSdan ** Memory for the buffer containing the column index affinity string 5669f8bb9cSdan ** is managed along with the rest of the Index structure. It will be 5769f8bb9cSdan ** released when sqlite3DeleteIndex() is called. 583d1bfeaaSdanielk1977 */ 5969f8bb9cSdan const char *sqlite3IndexAffinityStr(Vdbe *v, Index *pIdx){ 60a37cdde0Sdanielk1977 if( !pIdx->zColAff ){ 61e014a838Sdanielk1977 /* The first time a column affinity string for a particular index is 62a37cdde0Sdanielk1977 ** required, it is allocated and populated here. It is then stored as 63e014a838Sdanielk1977 ** a member of the Index structure for subsequent use. 64a37cdde0Sdanielk1977 ** 65a37cdde0Sdanielk1977 ** The column affinity string will eventually be deleted by 66e014a838Sdanielk1977 ** sqliteDeleteIndex() when the Index structure itself is cleaned 67a37cdde0Sdanielk1977 ** up. 68a37cdde0Sdanielk1977 */ 69a37cdde0Sdanielk1977 int n; 70a37cdde0Sdanielk1977 Table *pTab = pIdx->pTable; 71abb6fcabSdrh sqlite3 *db = sqlite3VdbeDb(v); 72633e6d57Sdrh pIdx->zColAff = (char *)sqlite3Malloc(pIdx->nColumn+2); 73a37cdde0Sdanielk1977 if( !pIdx->zColAff ){ 74633e6d57Sdrh db->mallocFailed = 1; 7569f8bb9cSdan return 0; 76a37cdde0Sdanielk1977 } 77a37cdde0Sdanielk1977 for(n=0; n<pIdx->nColumn; n++){ 78a37cdde0Sdanielk1977 pIdx->zColAff[n] = pTab->aCol[pIdx->aiColumn[n]].affinity; 79a37cdde0Sdanielk1977 } 802d401ab8Sdrh pIdx->zColAff[n++] = SQLITE_AFF_NONE; 812d401ab8Sdrh pIdx->zColAff[n] = 0; 82a37cdde0Sdanielk1977 } 833d1bfeaaSdanielk1977 8469f8bb9cSdan return pIdx->zColAff; 85a37cdde0Sdanielk1977 } 86a37cdde0Sdanielk1977 87a37cdde0Sdanielk1977 /* 8866a5167bSdrh ** Set P4 of the most recently inserted opcode to a column affinity 89a37cdde0Sdanielk1977 ** string for table pTab. A column affinity string has one character 90a37cdde0Sdanielk1977 ** for each column indexed by the index, according to the affinity of the 91a37cdde0Sdanielk1977 ** column: 92a37cdde0Sdanielk1977 ** 93a37cdde0Sdanielk1977 ** Character Column affinity 94a37cdde0Sdanielk1977 ** ------------------------------ 953eda040bSdrh ** 'a' TEXT 963eda040bSdrh ** 'b' NONE 973eda040bSdrh ** 'c' NUMERIC 983eda040bSdrh ** 'd' INTEGER 993eda040bSdrh ** 'e' REAL 100a37cdde0Sdanielk1977 */ 101a37cdde0Sdanielk1977 void sqlite3TableAffinityStr(Vdbe *v, Table *pTab){ 1023d1bfeaaSdanielk1977 /* The first time a column affinity string for a particular table 1033d1bfeaaSdanielk1977 ** is required, it is allocated and populated here. It is then 1043d1bfeaaSdanielk1977 ** stored as a member of the Table structure for subsequent use. 1053d1bfeaaSdanielk1977 ** 1063d1bfeaaSdanielk1977 ** The column affinity string will eventually be deleted by 1073d1bfeaaSdanielk1977 ** sqlite3DeleteTable() when the Table structure itself is cleaned up. 1083d1bfeaaSdanielk1977 */ 1093d1bfeaaSdanielk1977 if( !pTab->zColAff ){ 1103d1bfeaaSdanielk1977 char *zColAff; 1113d1bfeaaSdanielk1977 int i; 112abb6fcabSdrh sqlite3 *db = sqlite3VdbeDb(v); 1133d1bfeaaSdanielk1977 114633e6d57Sdrh zColAff = (char *)sqlite3Malloc(pTab->nCol+1); 1153d1bfeaaSdanielk1977 if( !zColAff ){ 116633e6d57Sdrh db->mallocFailed = 1; 117a37cdde0Sdanielk1977 return; 1183d1bfeaaSdanielk1977 } 1193d1bfeaaSdanielk1977 1203d1bfeaaSdanielk1977 for(i=0; i<pTab->nCol; i++){ 121a37cdde0Sdanielk1977 zColAff[i] = pTab->aCol[i].affinity; 1223d1bfeaaSdanielk1977 } 1233d1bfeaaSdanielk1977 zColAff[pTab->nCol] = '\0'; 1243d1bfeaaSdanielk1977 1253d1bfeaaSdanielk1977 pTab->zColAff = zColAff; 1263d1bfeaaSdanielk1977 } 1273d1bfeaaSdanielk1977 12866a5167bSdrh sqlite3VdbeChangeP4(v, -1, pTab->zColAff, 0); 1293d1bfeaaSdanielk1977 } 1303d1bfeaaSdanielk1977 1314d88778bSdanielk1977 /* 13248d1178aSdrh ** Return non-zero if the table pTab in database iDb or any of its indices 13348d1178aSdrh ** have been opened at any point in the VDBE program beginning at location 13448d1178aSdrh ** iStartAddr throught the end of the program. This is used to see if 13548d1178aSdrh ** a statement of the form "INSERT INTO <iDb, pTab> SELECT ..." can 13648d1178aSdrh ** run without using temporary table for the results of the SELECT. 1374d88778bSdanielk1977 */ 138595a523aSdanielk1977 static int readsTable(Parse *p, int iStartAddr, int iDb, Table *pTab){ 139595a523aSdanielk1977 Vdbe *v = sqlite3GetVdbe(p); 1404d88778bSdanielk1977 int i; 14148d1178aSdrh int iEnd = sqlite3VdbeCurrentAddr(v); 142595a523aSdanielk1977 #ifndef SQLITE_OMIT_VIRTUALTABLE 143595a523aSdanielk1977 VTable *pVTab = IsVirtual(pTab) ? sqlite3GetVTable(p->db, pTab) : 0; 144595a523aSdanielk1977 #endif 145595a523aSdanielk1977 14648d1178aSdrh for(i=iStartAddr; i<iEnd; i++){ 14748d1178aSdrh VdbeOp *pOp = sqlite3VdbeGetOp(v, i); 148ef0bea92Sdrh assert( pOp!=0 ); 149207872a4Sdanielk1977 if( pOp->opcode==OP_OpenRead && pOp->p3==iDb ){ 15048d1178aSdrh Index *pIndex; 151207872a4Sdanielk1977 int tnum = pOp->p2; 15248d1178aSdrh if( tnum==pTab->tnum ){ 15348d1178aSdrh return 1; 15448d1178aSdrh } 15548d1178aSdrh for(pIndex=pTab->pIndex; pIndex; pIndex=pIndex->pNext){ 15648d1178aSdrh if( tnum==pIndex->tnum ){ 15748d1178aSdrh return 1; 15848d1178aSdrh } 15948d1178aSdrh } 16048d1178aSdrh } 161543165efSdrh #ifndef SQLITE_OMIT_VIRTUALTABLE 162595a523aSdanielk1977 if( pOp->opcode==OP_VOpen && pOp->p4.pVtab==pVTab ){ 1632dca4ac1Sdanielk1977 assert( pOp->p4.pVtab!=0 ); 16466a5167bSdrh assert( pOp->p4type==P4_VTAB ); 16548d1178aSdrh return 1; 1664d88778bSdanielk1977 } 167543165efSdrh #endif 1684d88778bSdanielk1977 } 1694d88778bSdanielk1977 return 0; 1704d88778bSdanielk1977 } 1713d1bfeaaSdanielk1977 1729d9cf229Sdrh #ifndef SQLITE_OMIT_AUTOINCREMENT 1739d9cf229Sdrh /* 1740b9f50d8Sdrh ** Locate or create an AutoincInfo structure associated with table pTab 1750b9f50d8Sdrh ** which is in database iDb. Return the register number for the register 1760b9f50d8Sdrh ** that holds the maximum rowid. 1779d9cf229Sdrh ** 1780b9f50d8Sdrh ** There is at most one AutoincInfo structure per table even if the 1790b9f50d8Sdrh ** same table is autoincremented multiple times due to inserts within 1800b9f50d8Sdrh ** triggers. A new AutoincInfo structure is created if this is the 1810b9f50d8Sdrh ** first use of table pTab. On 2nd and subsequent uses, the original 1820b9f50d8Sdrh ** AutoincInfo structure is used. 1839d9cf229Sdrh ** 1840b9f50d8Sdrh ** Three memory locations are allocated: 1850b9f50d8Sdrh ** 1860b9f50d8Sdrh ** (1) Register to hold the name of the pTab table. 1870b9f50d8Sdrh ** (2) Register to hold the maximum ROWID of pTab. 1880b9f50d8Sdrh ** (3) Register to hold the rowid in sqlite_sequence of pTab 1890b9f50d8Sdrh ** 1900b9f50d8Sdrh ** The 2nd register is the one that is returned. That is all the 1910b9f50d8Sdrh ** insert routine needs to know about. 1929d9cf229Sdrh */ 1939d9cf229Sdrh static int autoIncBegin( 1949d9cf229Sdrh Parse *pParse, /* Parsing context */ 1959d9cf229Sdrh int iDb, /* Index of the database holding pTab */ 1969d9cf229Sdrh Table *pTab /* The table we are writing to */ 1979d9cf229Sdrh ){ 1986a288a33Sdrh int memId = 0; /* Register holding maximum rowid */ 1997d10d5a6Sdrh if( pTab->tabFlags & TF_Autoincrement ){ 200*76d462eeSdan Parse *pRoot = (pParse->pRoot ? pParse->pRoot : pParse); 2010b9f50d8Sdrh AutoincInfo *pInfo; 2020b9f50d8Sdrh 203*76d462eeSdan pInfo = pRoot->pAinc; 2040b9f50d8Sdrh while( pInfo && pInfo->pTab!=pTab ){ pInfo = pInfo->pNext; } 2050b9f50d8Sdrh if( pInfo==0 ){ 2060b9f50d8Sdrh pInfo = sqlite3DbMallocRaw(pParse->db, sizeof(*pInfo)); 2070b9f50d8Sdrh if( pInfo==0 ) return 0; 208*76d462eeSdan pInfo->pNext = pRoot->pAinc; 209*76d462eeSdan pRoot->pAinc = pInfo; 2100b9f50d8Sdrh pInfo->pTab = pTab; 2110b9f50d8Sdrh pInfo->iDb = iDb; 212*76d462eeSdan pRoot->nMem++; /* Register to hold name of table */ 213*76d462eeSdan pInfo->regCtr = ++pRoot->nMem; /* Max rowid register */ 214*76d462eeSdan pRoot->nMem++; /* Rowid in sqlite_sequence */ 2150b9f50d8Sdrh } 2160b9f50d8Sdrh memId = pInfo->regCtr; 2179d9cf229Sdrh } 2189d9cf229Sdrh return memId; 2199d9cf229Sdrh } 2209d9cf229Sdrh 2219d9cf229Sdrh /* 2220b9f50d8Sdrh ** This routine generates code that will initialize all of the 2230b9f50d8Sdrh ** register used by the autoincrement tracker. 2240b9f50d8Sdrh */ 2250b9f50d8Sdrh void sqlite3AutoincrementBegin(Parse *pParse){ 2260b9f50d8Sdrh AutoincInfo *p; /* Information about an AUTOINCREMENT */ 2270b9f50d8Sdrh sqlite3 *db = pParse->db; /* The database connection */ 2280b9f50d8Sdrh Db *pDb; /* Database only autoinc table */ 2290b9f50d8Sdrh int memId; /* Register holding max rowid */ 2300b9f50d8Sdrh int addr; /* A VDBE address */ 2310b9f50d8Sdrh Vdbe *v = pParse->pVdbe; /* VDBE under construction */ 2320b9f50d8Sdrh 233*76d462eeSdan /* If currently generating a trigger program, this call is a no-op */ 234*76d462eeSdan if( pParse->pTriggerTab ) return; 235*76d462eeSdan 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 newIdx = -1; /* Cursor for the NEW pseudo-table */ 4586a288a33Sdrh int iDb; /* Index of database holding TABLE */ 4592958a4e6Sdrh Db *pDb; /* The database containing table being inserted into */ 460e4d90813Sdrh int appendFlag = 0; /* True if the insert is likely to be an append */ 461cce7d176Sdrh 4626a288a33Sdrh /* Register allocations */ 4631bd10f8aSdrh int regFromSelect = 0;/* Base register for data coming from SELECT */ 4646a288a33Sdrh int regAutoinc = 0; /* Register holding the AUTOINCREMENT counter */ 4656a288a33Sdrh int regRowCount = 0; /* Memory cell used for the row counter */ 4666a288a33Sdrh int regIns; /* Block of regs holding rowid+data being inserted */ 4676a288a33Sdrh int regRowid; /* registers holding insert rowid */ 4686a288a33Sdrh int regData; /* register holding first column to insert */ 4696a288a33Sdrh int regRecord; /* Holds the assemblied row record */ 4701bd10f8aSdrh int regEof = 0; /* Register recording end of SELECT data */ 471aa9b8963Sdrh int *aRegIdx = 0; /* One register allocated to each index */ 4726a288a33Sdrh 473034ca14fSdanielk1977 474798da52cSdrh #ifndef SQLITE_OMIT_TRIGGER 475798da52cSdrh int isView; /* True if attempting to insert into a view */ 4762f886d1dSdanielk1977 Trigger *pTrigger; /* List of triggers on pTab, if required */ 4772f886d1dSdanielk1977 int tmask; /* Mask of trigger times */ 478798da52cSdrh #endif 479c3f9bad2Sdanielk1977 48017435752Sdrh db = pParse->db; 4811bd10f8aSdrh memset(&dest, 0, sizeof(dest)); 48217435752Sdrh if( pParse->nErr || db->mallocFailed ){ 4836f7adc8aSdrh goto insert_cleanup; 4846f7adc8aSdrh } 485daffd0e5Sdrh 4861ccde15dSdrh /* Locate the table into which we will be inserting new information. 4871ccde15dSdrh */ 488113088ecSdrh assert( pTabList->nSrc==1 ); 489113088ecSdrh zTab = pTabList->a[0].zName; 490098d1684Sdrh if( NEVER(zTab==0) ) goto insert_cleanup; 4914adee20fSdanielk1977 pTab = sqlite3SrcListLookup(pParse, pTabList); 492c3f9bad2Sdanielk1977 if( pTab==0 ){ 493c3f9bad2Sdanielk1977 goto insert_cleanup; 494c3f9bad2Sdanielk1977 } 495da184236Sdanielk1977 iDb = sqlite3SchemaToIndex(db, pTab->pSchema); 496da184236Sdanielk1977 assert( iDb<db->nDb ); 497da184236Sdanielk1977 pDb = &db->aDb[iDb]; 4982958a4e6Sdrh zDb = pDb->zName; 4994adee20fSdanielk1977 if( sqlite3AuthCheck(pParse, SQLITE_INSERT, pTab->zName, 0, zDb) ){ 5001962bda7Sdrh goto insert_cleanup; 5011962bda7Sdrh } 502c3f9bad2Sdanielk1977 503b7f9164eSdrh /* Figure out if we have any triggers and if the table being 504b7f9164eSdrh ** inserted into is a view 505b7f9164eSdrh */ 506b7f9164eSdrh #ifndef SQLITE_OMIT_TRIGGER 5072f886d1dSdanielk1977 pTrigger = sqlite3TriggersExist(pParse, pTab, TK_INSERT, 0, &tmask); 508b7f9164eSdrh isView = pTab->pSelect!=0; 509b7f9164eSdrh #else 5102f886d1dSdanielk1977 # define pTrigger 0 5112f886d1dSdanielk1977 # define tmask 0 512b7f9164eSdrh # define isView 0 513b7f9164eSdrh #endif 514b7f9164eSdrh #ifdef SQLITE_OMIT_VIEW 515b7f9164eSdrh # undef isView 516b7f9164eSdrh # define isView 0 517b7f9164eSdrh #endif 5182f886d1dSdanielk1977 assert( (pTrigger && tmask) || (pTrigger==0 && tmask==0) ); 519b7f9164eSdrh 520f573c99bSdrh /* If pTab is really a view, make sure it has been initialized. 521b3d24bf8Sdanielk1977 ** ViewGetColumnNames() is a no-op if pTab is not a view (or virtual 522b3d24bf8Sdanielk1977 ** module table). 523f573c99bSdrh */ 524b3d24bf8Sdanielk1977 if( sqlite3ViewGetColumnNames(pParse, pTab) ){ 525f573c99bSdrh goto insert_cleanup; 526f573c99bSdrh } 527f573c99bSdrh 528595a523aSdanielk1977 /* Ensure that: 529595a523aSdanielk1977 * (a) the table is not read-only, 530595a523aSdanielk1977 * (b) that if it is a view then ON INSERT triggers exist 531595a523aSdanielk1977 */ 532595a523aSdanielk1977 if( sqlite3IsReadOnly(pParse, pTab, tmask) ){ 533595a523aSdanielk1977 goto insert_cleanup; 534595a523aSdanielk1977 } 535595a523aSdanielk1977 5361ccde15dSdrh /* Allocate a VDBE 5371ccde15dSdrh */ 5384adee20fSdanielk1977 v = sqlite3GetVdbe(pParse); 5395974a30fSdrh if( v==0 ) goto insert_cleanup; 5404794f735Sdrh if( pParse->nested==0 ) sqlite3VdbeCountChanges(v); 5412f886d1dSdanielk1977 sqlite3BeginWriteOperation(pParse, pSelect || pTrigger, iDb); 5421ccde15dSdrh 5439d9cf229Sdrh #ifndef SQLITE_OMIT_XFER_OPT 5449d9cf229Sdrh /* If the statement is of the form 5459d9cf229Sdrh ** 5469d9cf229Sdrh ** INSERT INTO <table1> SELECT * FROM <table2>; 5479d9cf229Sdrh ** 5489d9cf229Sdrh ** Then special optimizations can be applied that make the transfer 5499d9cf229Sdrh ** very fast and which reduce fragmentation of indices. 550e00ee6ebSdrh ** 551e00ee6ebSdrh ** This is the 2nd template. 5529d9cf229Sdrh */ 5539d9cf229Sdrh if( pColumn==0 && xferOptimization(pParse, pTab, pSelect, onError, iDb) ){ 5542f886d1dSdanielk1977 assert( !pTrigger ); 5559d9cf229Sdrh assert( pList==0 ); 5560b9f50d8Sdrh goto insert_end; 5579d9cf229Sdrh } 5589d9cf229Sdrh #endif /* SQLITE_OMIT_XFER_OPT */ 5599d9cf229Sdrh 5602958a4e6Sdrh /* If this is an AUTOINCREMENT table, look up the sequence number in the 5616a288a33Sdrh ** sqlite_sequence table and store it in memory cell regAutoinc. 5622958a4e6Sdrh */ 5636a288a33Sdrh regAutoinc = autoIncBegin(pParse, iDb, pTab); 5642958a4e6Sdrh 5651ccde15dSdrh /* Figure out how many columns of data are supplied. If the data 566e00ee6ebSdrh ** is coming from a SELECT statement, then generate a co-routine that 567e00ee6ebSdrh ** produces a single row of the SELECT on each invocation. The 568e00ee6ebSdrh ** co-routine is the common header to the 3rd and 4th templates. 5691ccde15dSdrh */ 5705974a30fSdrh if( pSelect ){ 571142e30dfSdrh /* Data is coming from a SELECT. Generate code to implement that SELECT 572e00ee6ebSdrh ** as a co-routine. The code is common to both the 3rd and 4th 573e00ee6ebSdrh ** templates: 574e00ee6ebSdrh ** 575e00ee6ebSdrh ** EOF <- 0 576e00ee6ebSdrh ** X <- A 577e00ee6ebSdrh ** goto B 578e00ee6ebSdrh ** A: setup for the SELECT 579e00ee6ebSdrh ** loop over the tables in the SELECT 580e00ee6ebSdrh ** load value into register R..R+n 581e00ee6ebSdrh ** yield X 582e00ee6ebSdrh ** end loop 583e00ee6ebSdrh ** cleanup after the SELECT 584e00ee6ebSdrh ** EOF <- 1 585e00ee6ebSdrh ** yield X 586e00ee6ebSdrh ** halt-error 587e00ee6ebSdrh ** 588e00ee6ebSdrh ** On each invocation of the co-routine, it puts a single row of the 589e00ee6ebSdrh ** SELECT result into registers dest.iMem...dest.iMem+dest.nMem-1. 590e00ee6ebSdrh ** (These output registers are allocated by sqlite3Select().) When 591e00ee6ebSdrh ** the SELECT completes, it sets the EOF flag stored in regEof. 592142e30dfSdrh */ 593e00ee6ebSdrh int rc, j1; 5941013c932Sdrh 595e00ee6ebSdrh regEof = ++pParse->nMem; 596e00ee6ebSdrh sqlite3VdbeAddOp2(v, OP_Integer, 0, regEof); /* EOF <- 0 */ 597e00ee6ebSdrh VdbeComment((v, "SELECT eof flag")); 59892b01d53Sdrh sqlite3SelectDestInit(&dest, SRT_Coroutine, ++pParse->nMem); 599e00ee6ebSdrh addrSelect = sqlite3VdbeCurrentAddr(v)+2; 60092b01d53Sdrh sqlite3VdbeAddOp2(v, OP_Integer, addrSelect-1, dest.iParm); 601e00ee6ebSdrh j1 = sqlite3VdbeAddOp2(v, OP_Goto, 0, 0); 602e00ee6ebSdrh VdbeComment((v, "Jump over SELECT coroutine")); 603b3bce662Sdanielk1977 604b3bce662Sdanielk1977 /* Resolve the expressions in the SELECT statement and execute it. */ 6057d10d5a6Sdrh rc = sqlite3Select(pParse, pSelect, &dest); 606098d1684Sdrh assert( pParse->nErr==0 || rc ); 607098d1684Sdrh if( rc || NEVER(pParse->nErr) || db->mallocFailed ){ 6086f7adc8aSdrh goto insert_cleanup; 6096f7adc8aSdrh } 610e00ee6ebSdrh sqlite3VdbeAddOp2(v, OP_Integer, 1, regEof); /* EOF <- 1 */ 61192b01d53Sdrh sqlite3VdbeAddOp1(v, OP_Yield, dest.iParm); /* yield X */ 612e00ee6ebSdrh sqlite3VdbeAddOp2(v, OP_Halt, SQLITE_INTERNAL, OE_Abort); 613e00ee6ebSdrh VdbeComment((v, "End of SELECT coroutine")); 614e00ee6ebSdrh sqlite3VdbeJumpHere(v, j1); /* label B: */ 615b3bce662Sdanielk1977 6166a288a33Sdrh regFromSelect = dest.iMem; 6175974a30fSdrh assert( pSelect->pEList ); 618967e8b73Sdrh nColumn = pSelect->pEList->nExpr; 619e00ee6ebSdrh assert( dest.nMem==nColumn ); 620142e30dfSdrh 621142e30dfSdrh /* Set useTempTable to TRUE if the result of the SELECT statement 622e00ee6ebSdrh ** should be written into a temporary table (template 4). Set to 623e00ee6ebSdrh ** FALSE if each* row of the SELECT can be written directly into 624e00ee6ebSdrh ** the destination table (template 3). 625048c530cSdrh ** 626048c530cSdrh ** A temp table must be used if the table being updated is also one 627048c530cSdrh ** of the tables being read by the SELECT statement. Also use a 628048c530cSdrh ** temp table in the case of row triggers. 629142e30dfSdrh */ 630595a523aSdanielk1977 if( pTrigger || readsTable(pParse, addrSelect, iDb, pTab) ){ 631048c530cSdrh useTempTable = 1; 632048c530cSdrh } 633142e30dfSdrh 634142e30dfSdrh if( useTempTable ){ 635e00ee6ebSdrh /* Invoke the coroutine to extract information from the SELECT 636e00ee6ebSdrh ** and add it to a transient table srcTab. The code generated 637e00ee6ebSdrh ** here is from the 4th template: 638e00ee6ebSdrh ** 639e00ee6ebSdrh ** B: open temp table 640e00ee6ebSdrh ** L: yield X 641e00ee6ebSdrh ** if EOF goto M 642e00ee6ebSdrh ** insert row from R..R+n into temp table 643e00ee6ebSdrh ** goto L 644e00ee6ebSdrh ** M: ... 645142e30dfSdrh */ 646e00ee6ebSdrh int regRec; /* Register to hold packed record */ 647dc5ea5c7Sdrh int regTempRowid; /* Register to hold temp table ROWID */ 648e00ee6ebSdrh int addrTop; /* Label "L" */ 649e00ee6ebSdrh int addrIf; /* Address of jump to M */ 650b7654111Sdrh 651142e30dfSdrh srcTab = pParse->nTab++; 652b7654111Sdrh regRec = sqlite3GetTempReg(pParse); 653dc5ea5c7Sdrh regTempRowid = sqlite3GetTempReg(pParse); 654e00ee6ebSdrh sqlite3VdbeAddOp2(v, OP_OpenEphemeral, srcTab, nColumn); 65592b01d53Sdrh addrTop = sqlite3VdbeAddOp1(v, OP_Yield, dest.iParm); 656e00ee6ebSdrh addrIf = sqlite3VdbeAddOp1(v, OP_If, regEof); 6571db639ceSdrh sqlite3VdbeAddOp3(v, OP_MakeRecord, regFromSelect, nColumn, regRec); 658dc5ea5c7Sdrh sqlite3VdbeAddOp2(v, OP_NewRowid, srcTab, regTempRowid); 659dc5ea5c7Sdrh sqlite3VdbeAddOp3(v, OP_Insert, srcTab, regRec, regTempRowid); 660e00ee6ebSdrh sqlite3VdbeAddOp2(v, OP_Goto, 0, addrTop); 661e00ee6ebSdrh sqlite3VdbeJumpHere(v, addrIf); 662b7654111Sdrh sqlite3ReleaseTempReg(pParse, regRec); 663dc5ea5c7Sdrh sqlite3ReleaseTempReg(pParse, regTempRowid); 664142e30dfSdrh } 665142e30dfSdrh }else{ 666142e30dfSdrh /* This is the case if the data for the INSERT is coming from a VALUES 667142e30dfSdrh ** clause 668142e30dfSdrh */ 669b3bce662Sdanielk1977 NameContext sNC; 670b3bce662Sdanielk1977 memset(&sNC, 0, sizeof(sNC)); 671b3bce662Sdanielk1977 sNC.pParse = pParse; 6725974a30fSdrh srcTab = -1; 67348d1178aSdrh assert( useTempTable==0 ); 674147d0cccSdrh nColumn = pList ? pList->nExpr : 0; 675e64e7b20Sdrh for(i=0; i<nColumn; i++){ 6767d10d5a6Sdrh if( sqlite3ResolveExprNames(&sNC, pList->a[i].pExpr) ){ 677b04a5d87Sdrh goto insert_cleanup; 678b04a5d87Sdrh } 679e64e7b20Sdrh } 6805974a30fSdrh } 6811ccde15dSdrh 6821ccde15dSdrh /* Make sure the number of columns in the source data matches the number 6831ccde15dSdrh ** of columns to be inserted into the table. 6841ccde15dSdrh */ 685034ca14fSdanielk1977 if( IsVirtual(pTab) ){ 686034ca14fSdanielk1977 for(i=0; i<pTab->nCol; i++){ 687034ca14fSdanielk1977 nHidden += (IsHiddenColumn(&pTab->aCol[i]) ? 1 : 0); 688034ca14fSdanielk1977 } 689034ca14fSdanielk1977 } 690034ca14fSdanielk1977 if( pColumn==0 && nColumn && nColumn!=(pTab->nCol-nHidden) ){ 6914adee20fSdanielk1977 sqlite3ErrorMsg(pParse, 692da93d238Sdrh "table %S has %d columns but %d values were supplied", 693d51397a6Sdrh pTabList, 0, pTab->nCol-nHidden, nColumn); 694cce7d176Sdrh goto insert_cleanup; 695cce7d176Sdrh } 696967e8b73Sdrh if( pColumn!=0 && nColumn!=pColumn->nId ){ 6974adee20fSdanielk1977 sqlite3ErrorMsg(pParse, "%d values for %d columns", nColumn, pColumn->nId); 698cce7d176Sdrh goto insert_cleanup; 699cce7d176Sdrh } 7001ccde15dSdrh 7011ccde15dSdrh /* If the INSERT statement included an IDLIST term, then make sure 7021ccde15dSdrh ** all elements of the IDLIST really are columns of the table and 7031ccde15dSdrh ** remember the column indices. 704c8392586Sdrh ** 705c8392586Sdrh ** If the table has an INTEGER PRIMARY KEY column and that column 706c8392586Sdrh ** is named in the IDLIST, then record in the keyColumn variable 707c8392586Sdrh ** the index into IDLIST of the primary key column. keyColumn is 708c8392586Sdrh ** the index of the primary key as it appears in IDLIST, not as 709c8392586Sdrh ** is appears in the original table. (The index of the primary 710c8392586Sdrh ** key in the original table is pTab->iPKey.) 7111ccde15dSdrh */ 712967e8b73Sdrh if( pColumn ){ 713967e8b73Sdrh for(i=0; i<pColumn->nId; i++){ 714967e8b73Sdrh pColumn->a[i].idx = -1; 715cce7d176Sdrh } 716967e8b73Sdrh for(i=0; i<pColumn->nId; i++){ 717cce7d176Sdrh for(j=0; j<pTab->nCol; j++){ 7184adee20fSdanielk1977 if( sqlite3StrICmp(pColumn->a[i].zName, pTab->aCol[j].zName)==0 ){ 719967e8b73Sdrh pColumn->a[i].idx = j; 7204a32431cSdrh if( j==pTab->iPKey ){ 7219aa028daSdrh keyColumn = i; 7224a32431cSdrh } 723cce7d176Sdrh break; 724cce7d176Sdrh } 725cce7d176Sdrh } 726cce7d176Sdrh if( j>=pTab->nCol ){ 7274adee20fSdanielk1977 if( sqlite3IsRowid(pColumn->a[i].zName) ){ 728a0217ba7Sdrh keyColumn = i; 729a0217ba7Sdrh }else{ 7304adee20fSdanielk1977 sqlite3ErrorMsg(pParse, "table %S has no column named %s", 731da93d238Sdrh pTabList, 0, pColumn->a[i].zName); 732cce7d176Sdrh pParse->nErr++; 733cce7d176Sdrh goto insert_cleanup; 734cce7d176Sdrh } 735cce7d176Sdrh } 736cce7d176Sdrh } 737a0217ba7Sdrh } 7381ccde15dSdrh 739aacc543eSdrh /* If there is no IDLIST term but the table has an integer primary 740c8392586Sdrh ** key, the set the keyColumn variable to the primary key column index 741c8392586Sdrh ** in the original table definition. 7424a32431cSdrh */ 743147d0cccSdrh if( pColumn==0 && nColumn>0 ){ 7444a32431cSdrh keyColumn = pTab->iPKey; 7454a32431cSdrh } 7464a32431cSdrh 747c3f9bad2Sdanielk1977 /* Initialize the count of rows to be inserted 7481ccde15dSdrh */ 749142e30dfSdrh if( db->flags & SQLITE_CountRows ){ 7506a288a33Sdrh regRowCount = ++pParse->nMem; 7516a288a33Sdrh sqlite3VdbeAddOp2(v, OP_Integer, 0, regRowCount); 752c3f9bad2Sdanielk1977 } 753c3f9bad2Sdanielk1977 754e448dc4aSdanielk1977 /* If this is not a view, open the table and and all indices */ 755e448dc4aSdanielk1977 if( !isView ){ 756aa9b8963Sdrh int nIdx; 757aa9b8963Sdrh 75804adf416Sdrh baseCur = pParse->nTab; 75904adf416Sdrh nIdx = sqlite3OpenTableAndIndices(pParse, pTab, baseCur, OP_OpenWrite); 7605c070538Sdrh aRegIdx = sqlite3DbMallocRaw(db, sizeof(int)*(nIdx+1)); 761aa9b8963Sdrh if( aRegIdx==0 ){ 762aa9b8963Sdrh goto insert_cleanup; 763aa9b8963Sdrh } 764aa9b8963Sdrh for(i=0; i<nIdx; i++){ 765aa9b8963Sdrh aRegIdx[i] = ++pParse->nMem; 766aa9b8963Sdrh } 767feeb1394Sdrh } 768feeb1394Sdrh 769e00ee6ebSdrh /* This is the top of the main insertion loop */ 770142e30dfSdrh if( useTempTable ){ 771e00ee6ebSdrh /* This block codes the top of loop only. The complete loop is the 772e00ee6ebSdrh ** following pseudocode (template 4): 773e00ee6ebSdrh ** 774e00ee6ebSdrh ** rewind temp table 775e00ee6ebSdrh ** C: loop over rows of intermediate table 776e00ee6ebSdrh ** transfer values form intermediate table into <table> 777e00ee6ebSdrh ** end loop 778e00ee6ebSdrh ** D: ... 779e00ee6ebSdrh */ 780e00ee6ebSdrh addrInsTop = sqlite3VdbeAddOp1(v, OP_Rewind, srcTab); 781e00ee6ebSdrh addrCont = sqlite3VdbeCurrentAddr(v); 782142e30dfSdrh }else if( pSelect ){ 783e00ee6ebSdrh /* This block codes the top of loop only. The complete loop is the 784e00ee6ebSdrh ** following pseudocode (template 3): 785e00ee6ebSdrh ** 786e00ee6ebSdrh ** C: yield X 787e00ee6ebSdrh ** if EOF goto D 788e00ee6ebSdrh ** insert the select result into <table> from R..R+n 789e00ee6ebSdrh ** goto C 790e00ee6ebSdrh ** D: ... 791e00ee6ebSdrh */ 79292b01d53Sdrh addrCont = sqlite3VdbeAddOp1(v, OP_Yield, dest.iParm); 793e00ee6ebSdrh addrInsTop = sqlite3VdbeAddOp1(v, OP_If, regEof); 794bed8690fSdrh } 7951ccde15dSdrh 7966a288a33Sdrh /* Allocate registers for holding the rowid of the new row, 7976a288a33Sdrh ** the content of the new row, and the assemblied row record. 7986a288a33Sdrh */ 7996a288a33Sdrh regRecord = ++pParse->nMem; 8006a288a33Sdrh regRowid = regIns = pParse->nMem+1; 8016a288a33Sdrh pParse->nMem += pTab->nCol + 1; 8026a288a33Sdrh if( IsVirtual(pTab) ){ 8036a288a33Sdrh regRowid++; 8046a288a33Sdrh pParse->nMem++; 8056a288a33Sdrh } 8066a288a33Sdrh regData = regRowid+1; 8076a288a33Sdrh 8085cf590c1Sdrh /* Run the BEFORE and INSTEAD OF triggers, if there are any 80970ce3f0cSdrh */ 8104adee20fSdanielk1977 endOfLoop = sqlite3VdbeMakeLabel(v); 8112f886d1dSdanielk1977 if( tmask & TRIGGER_BEFORE ){ 812*76d462eeSdan int regCols = sqlite3GetTempRange(pParse, pTab->nCol+1); 813c3f9bad2Sdanielk1977 81470ce3f0cSdrh /* build the NEW.* reference row. Note that if there is an INTEGER 81570ce3f0cSdrh ** PRIMARY KEY into which a NULL is being inserted, that NULL will be 81670ce3f0cSdrh ** translated into a unique ID for the row. But on a BEFORE trigger, 81770ce3f0cSdrh ** we do not know what the unique ID will be (because the insert has 81870ce3f0cSdrh ** not happened yet) so we substitute a rowid of -1 81970ce3f0cSdrh */ 82070ce3f0cSdrh if( keyColumn<0 ){ 821*76d462eeSdan sqlite3VdbeAddOp2(v, OP_Integer, -1, regCols); 82270ce3f0cSdrh }else{ 8236a288a33Sdrh int j1; 8247fe45908Sdrh if( useTempTable ){ 825*76d462eeSdan sqlite3VdbeAddOp3(v, OP_Column, srcTab, keyColumn, regCols); 8267fe45908Sdrh }else{ 827d6fe961eSdrh assert( pSelect==0 ); /* Otherwise useTempTable is true */ 828*76d462eeSdan sqlite3ExprCode(pParse, pList->a[keyColumn].pExpr, regCols); 8297fe45908Sdrh } 830*76d462eeSdan j1 = sqlite3VdbeAddOp1(v, OP_NotNull, regCols); 831*76d462eeSdan sqlite3VdbeAddOp2(v, OP_Integer, -1, regCols); 8326a288a33Sdrh sqlite3VdbeJumpHere(v, j1); 833*76d462eeSdan sqlite3VdbeAddOp1(v, OP_MustBeInt, regCols); 83470ce3f0cSdrh } 83570ce3f0cSdrh 836034ca14fSdanielk1977 /* Cannot have triggers on a virtual table. If it were possible, 837034ca14fSdanielk1977 ** this block would have to account for hidden column. 838034ca14fSdanielk1977 */ 839034ca14fSdanielk1977 assert( !IsVirtual(pTab) ); 840034ca14fSdanielk1977 84170ce3f0cSdrh /* Create the new column data 84270ce3f0cSdrh */ 843c3f9bad2Sdanielk1977 for(i=0; i<pTab->nCol; i++){ 844c3f9bad2Sdanielk1977 if( pColumn==0 ){ 845c3f9bad2Sdanielk1977 j = i; 846c3f9bad2Sdanielk1977 }else{ 847c3f9bad2Sdanielk1977 for(j=0; j<pColumn->nId; j++){ 848c3f9bad2Sdanielk1977 if( pColumn->a[j].idx==i ) break; 849c3f9bad2Sdanielk1977 } 850c3f9bad2Sdanielk1977 } 851c3f9bad2Sdanielk1977 if( pColumn && j>=pColumn->nId ){ 852*76d462eeSdan sqlite3ExprCode(pParse, pTab->aCol[i].pDflt, regCols+i+1); 853142e30dfSdrh }else if( useTempTable ){ 854*76d462eeSdan sqlite3VdbeAddOp3(v, OP_Column, srcTab, j, regCols+i+1); 855c3f9bad2Sdanielk1977 }else{ 856d6fe961eSdrh assert( pSelect==0 ); /* Otherwise useTempTable is true */ 857*76d462eeSdan sqlite3ExprCodeAndCache(pParse, pList->a[j].pExpr, regCols+i+1); 858c3f9bad2Sdanielk1977 } 859c3f9bad2Sdanielk1977 } 860a37cdde0Sdanielk1977 861a37cdde0Sdanielk1977 /* If this is an INSERT on a view with an INSTEAD OF INSERT trigger, 862a37cdde0Sdanielk1977 ** do not attempt any conversions before assembling the record. 863a37cdde0Sdanielk1977 ** If this is a real table, attempt conversions as required by the 864a37cdde0Sdanielk1977 ** table column affinities. 865a37cdde0Sdanielk1977 */ 866a37cdde0Sdanielk1977 if( !isView ){ 867*76d462eeSdan sqlite3VdbeAddOp2(v, OP_Affinity, regCols+1, pTab->nCol); 868a37cdde0Sdanielk1977 sqlite3TableAffinityStr(v, pTab); 869a37cdde0Sdanielk1977 } 870c3f9bad2Sdanielk1977 8715cf590c1Sdrh /* Fire BEFORE or INSTEAD OF triggers */ 872165921a7Sdan sqlite3CodeRowTrigger(pParse, pTrigger, TK_INSERT, 0, TRIGGER_BEFORE, 873*76d462eeSdan pTab, -1, regCols-pTab->nCol-1, onError, endOfLoop); 874165921a7Sdan 875*76d462eeSdan sqlite3ReleaseTempRange(pParse, regCols, pTab->nCol+1); 87670ce3f0cSdrh } 877c3f9bad2Sdanielk1977 8784a32431cSdrh /* Push the record number for the new entry onto the stack. The 879f0863fe5Sdrh ** record number is a randomly generate integer created by NewRowid 8804a32431cSdrh ** except when the table has an INTEGER PRIMARY KEY column, in which 881b419a926Sdrh ** case the record number is the same as that column. 8821ccde15dSdrh */ 8835cf590c1Sdrh if( !isView ){ 8844cbdda9eSdrh if( IsVirtual(pTab) ){ 8854cbdda9eSdrh /* The row that the VUpdate opcode will delete: none */ 8866a288a33Sdrh sqlite3VdbeAddOp2(v, OP_Null, 0, regIns); 8874cbdda9eSdrh } 8884a32431cSdrh if( keyColumn>=0 ){ 889142e30dfSdrh if( useTempTable ){ 8906a288a33Sdrh sqlite3VdbeAddOp3(v, OP_Column, srcTab, keyColumn, regRowid); 891142e30dfSdrh }else if( pSelect ){ 892b7654111Sdrh sqlite3VdbeAddOp2(v, OP_SCopy, regFromSelect+keyColumn, regRowid); 8934a32431cSdrh }else{ 894e4d90813Sdrh VdbeOp *pOp; 8951db639ceSdrh sqlite3ExprCode(pParse, pList->a[keyColumn].pExpr, regRowid); 89620411ea7Sdrh pOp = sqlite3VdbeGetOp(v, -1); 8971b7ecbb4Sdrh if( ALWAYS(pOp) && pOp->opcode==OP_Null && !IsVirtual(pTab) ){ 898e4d90813Sdrh appendFlag = 1; 899e4d90813Sdrh pOp->opcode = OP_NewRowid; 90004adf416Sdrh pOp->p1 = baseCur; 9016a288a33Sdrh pOp->p2 = regRowid; 9026a288a33Sdrh pOp->p3 = regAutoinc; 903e4d90813Sdrh } 90427a32783Sdrh } 905f0863fe5Sdrh /* If the PRIMARY KEY expression is NULL, then use OP_NewRowid 906e1e68f49Sdrh ** to generate a unique primary key value. 907e1e68f49Sdrh */ 908e4d90813Sdrh if( !appendFlag ){ 9091db639ceSdrh int j1; 910bb50e7adSdanielk1977 if( !IsVirtual(pTab) ){ 9111db639ceSdrh j1 = sqlite3VdbeAddOp1(v, OP_NotNull, regRowid); 91204adf416Sdrh sqlite3VdbeAddOp3(v, OP_NewRowid, baseCur, regRowid, regAutoinc); 9131db639ceSdrh sqlite3VdbeJumpHere(v, j1); 914bb50e7adSdanielk1977 }else{ 915bb50e7adSdanielk1977 j1 = sqlite3VdbeCurrentAddr(v); 916bb50e7adSdanielk1977 sqlite3VdbeAddOp2(v, OP_IsNull, regRowid, j1+2); 917bb50e7adSdanielk1977 } 9183c84ddffSdrh sqlite3VdbeAddOp1(v, OP_MustBeInt, regRowid); 919e4d90813Sdrh } 9204cbdda9eSdrh }else if( IsVirtual(pTab) ){ 9216a288a33Sdrh sqlite3VdbeAddOp2(v, OP_Null, 0, regRowid); 9224a32431cSdrh }else{ 92304adf416Sdrh sqlite3VdbeAddOp3(v, OP_NewRowid, baseCur, regRowid, regAutoinc); 924e4d90813Sdrh appendFlag = 1; 9254a32431cSdrh } 9266a288a33Sdrh autoIncStep(pParse, regAutoinc, regRowid); 9274a32431cSdrh 928aacc543eSdrh /* Push onto the stack, data for all columns of the new entry, beginning 9294a32431cSdrh ** with the first column. 9304a32431cSdrh */ 931034ca14fSdanielk1977 nHidden = 0; 932cce7d176Sdrh for(i=0; i<pTab->nCol; i++){ 9336a288a33Sdrh int iRegStore = regRowid+1+i; 9344a32431cSdrh if( i==pTab->iPKey ){ 9354a32431cSdrh /* The value of the INTEGER PRIMARY KEY column is always a NULL. 936aacc543eSdrh ** Whenever this column is read, the record number will be substituted 937aacc543eSdrh ** in its place. So will fill this column with a NULL to avoid 938aacc543eSdrh ** taking up data space with information that will never be used. */ 9394c583128Sdrh sqlite3VdbeAddOp2(v, OP_Null, 0, iRegStore); 9404a32431cSdrh continue; 9414a32431cSdrh } 942967e8b73Sdrh if( pColumn==0 ){ 943034ca14fSdanielk1977 if( IsHiddenColumn(&pTab->aCol[i]) ){ 944034ca14fSdanielk1977 assert( IsVirtual(pTab) ); 945034ca14fSdanielk1977 j = -1; 946034ca14fSdanielk1977 nHidden++; 947034ca14fSdanielk1977 }else{ 948034ca14fSdanielk1977 j = i - nHidden; 949034ca14fSdanielk1977 } 950cce7d176Sdrh }else{ 951967e8b73Sdrh for(j=0; j<pColumn->nId; j++){ 952967e8b73Sdrh if( pColumn->a[j].idx==i ) break; 953cce7d176Sdrh } 954cce7d176Sdrh } 955034ca14fSdanielk1977 if( j<0 || nColumn==0 || (pColumn && j>=pColumn->nId) ){ 956287fb61cSdanielk1977 sqlite3ExprCode(pParse, pTab->aCol[i].pDflt, iRegStore); 957142e30dfSdrh }else if( useTempTable ){ 958287fb61cSdanielk1977 sqlite3VdbeAddOp3(v, OP_Column, srcTab, j, iRegStore); 959142e30dfSdrh }else if( pSelect ){ 960b7654111Sdrh sqlite3VdbeAddOp2(v, OP_SCopy, regFromSelect+j, iRegStore); 961cce7d176Sdrh }else{ 962287fb61cSdanielk1977 sqlite3ExprCode(pParse, pList->a[j].pExpr, iRegStore); 963cce7d176Sdrh } 964cce7d176Sdrh } 9651ccde15dSdrh 9660ca3e24bSdrh /* Generate code to check constraints and generate index keys and 9670ca3e24bSdrh ** do the insertion. 9684a32431cSdrh */ 9694cbdda9eSdrh #ifndef SQLITE_OMIT_VIRTUALTABLE 9704cbdda9eSdrh if( IsVirtual(pTab) ){ 971595a523aSdanielk1977 const char *pVTab = (const char *)sqlite3GetVTable(db, pTab); 9724f3dd150Sdrh sqlite3VtabMakeWritable(pParse, pTab); 973595a523aSdanielk1977 sqlite3VdbeAddOp4(v, OP_VUpdate, 1, pTab->nCol+2, regIns, pVTab, P4_VTAB); 9744cbdda9eSdrh }else 9754cbdda9eSdrh #endif 9764cbdda9eSdrh { 977de630353Sdanielk1977 int isReplace; /* Set to true if constraints may cause a replace */ 978de630353Sdanielk1977 sqlite3GenerateConstraintChecks(pParse, pTab, baseCur, regIns, aRegIdx, 979de630353Sdanielk1977 keyColumn>=0, 0, onError, endOfLoop, &isReplace 98004adf416Sdrh ); 98104adf416Sdrh sqlite3CompleteInsertion( 982de630353Sdanielk1977 pParse, pTab, baseCur, regIns, aRegIdx, 0, 983de630353Sdanielk1977 (tmask&TRIGGER_AFTER) ? newIdx : -1, appendFlag, isReplace==0 98404adf416Sdrh ); 9855cf590c1Sdrh } 9864cbdda9eSdrh } 9871bee3d7bSdrh 988feeb1394Sdrh /* Update the count of rows that are inserted 9891bee3d7bSdrh */ 990142e30dfSdrh if( (db->flags & SQLITE_CountRows)!=0 ){ 9916a288a33Sdrh sqlite3VdbeAddOp2(v, OP_AddImm, regRowCount, 1); 9921bee3d7bSdrh } 993c3f9bad2Sdanielk1977 9942f886d1dSdanielk1977 if( pTrigger ){ 995c3f9bad2Sdanielk1977 /* Code AFTER triggers */ 996165921a7Sdan sqlite3CodeRowTrigger(pParse, pTrigger, TK_INSERT, 0, TRIGGER_AFTER, 997*76d462eeSdan pTab, -1, regData-2-pTab->nCol, onError, endOfLoop); 998c3f9bad2Sdanielk1977 } 9991bee3d7bSdrh 1000e00ee6ebSdrh /* The bottom of the main insertion loop, if the data source 1001e00ee6ebSdrh ** is a SELECT statement. 10021ccde15dSdrh */ 10034adee20fSdanielk1977 sqlite3VdbeResolveLabel(v, endOfLoop); 1004142e30dfSdrh if( useTempTable ){ 1005e00ee6ebSdrh sqlite3VdbeAddOp2(v, OP_Next, srcTab, addrCont); 1006e00ee6ebSdrh sqlite3VdbeJumpHere(v, addrInsTop); 10072eb95377Sdrh sqlite3VdbeAddOp1(v, OP_Close, srcTab); 1008142e30dfSdrh }else if( pSelect ){ 1009e00ee6ebSdrh sqlite3VdbeAddOp2(v, OP_Goto, 0, addrCont); 1010e00ee6ebSdrh sqlite3VdbeJumpHere(v, addrInsTop); 10116b56344dSdrh } 1012c3f9bad2Sdanielk1977 1013e448dc4aSdanielk1977 if( !IsVirtual(pTab) && !isView ){ 1014c3f9bad2Sdanielk1977 /* Close all tables opened */ 10152eb95377Sdrh sqlite3VdbeAddOp1(v, OP_Close, baseCur); 10166b56344dSdrh for(idx=1, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, idx++){ 10172eb95377Sdrh sqlite3VdbeAddOp1(v, OP_Close, idx+baseCur); 1018cce7d176Sdrh } 1019c3f9bad2Sdanielk1977 } 1020c3f9bad2Sdanielk1977 10210b9f50d8Sdrh insert_end: 1022f3388144Sdrh /* Update the sqlite_sequence table by storing the content of the 10230b9f50d8Sdrh ** maximum rowid counter values recorded while inserting into 10240b9f50d8Sdrh ** autoincrement tables. 10252958a4e6Sdrh */ 1026165921a7Sdan if( pParse->nested==0 && pParse->pTriggerTab==0 ){ 10270b9f50d8Sdrh sqlite3AutoincrementEnd(pParse); 10280b9f50d8Sdrh } 10292958a4e6Sdrh 10301bee3d7bSdrh /* 1031e7de6f25Sdanielk1977 ** Return the number of rows inserted. If this routine is 1032e7de6f25Sdanielk1977 ** generating code because of a call to sqlite3NestedParse(), do not 1033e7de6f25Sdanielk1977 ** invoke the callback function. 10341bee3d7bSdrh */ 1035165921a7Sdan if( (db->flags&SQLITE_CountRows) && !pParse->nested && !pParse->pTriggerTab ){ 10366a288a33Sdrh sqlite3VdbeAddOp2(v, OP_ResultRow, regRowCount, 1); 103722322fd4Sdanielk1977 sqlite3VdbeSetNumCols(v, 1); 103810fb749bSdanielk1977 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "rows inserted", SQLITE_STATIC); 10391bee3d7bSdrh } 1040cce7d176Sdrh 1041cce7d176Sdrh insert_cleanup: 1042633e6d57Sdrh sqlite3SrcListDelete(db, pTabList); 1043633e6d57Sdrh sqlite3ExprListDelete(db, pList); 1044633e6d57Sdrh sqlite3SelectDelete(db, pSelect); 1045633e6d57Sdrh sqlite3IdListDelete(db, pColumn); 1046633e6d57Sdrh sqlite3DbFree(db, aRegIdx); 1047cce7d176Sdrh } 10489cfcf5d4Sdrh 10499cfcf5d4Sdrh /* 10506a288a33Sdrh ** Generate code to do constraint checks prior to an INSERT or an UPDATE. 10519cfcf5d4Sdrh ** 105204adf416Sdrh ** The input is a range of consecutive registers as follows: 10530ca3e24bSdrh ** 1054f0863fe5Sdrh ** 1. The rowid of the row to be updated before the update. This 1055b419a926Sdrh ** value is omitted unless we are doing an UPDATE that involves a 1056a05a722fSdrh ** change to the record number or writing to a virtual table. 10570ca3e24bSdrh ** 1058f0863fe5Sdrh ** 2. The rowid of the row after the update. 10590ca3e24bSdrh ** 10600ca3e24bSdrh ** 3. The data in the first column of the entry after the update. 10610ca3e24bSdrh ** 10620ca3e24bSdrh ** i. Data from middle columns... 10630ca3e24bSdrh ** 10640ca3e24bSdrh ** N. The data in the last column of the entry after the update. 10650ca3e24bSdrh ** 106604adf416Sdrh ** The regRowid parameter is the index of the register containing (2). 106704adf416Sdrh ** 1068f0863fe5Sdrh ** The old rowid shown as entry (1) above is omitted unless both isUpdate 1069f0863fe5Sdrh ** and rowidChng are 1. isUpdate is true for UPDATEs and false for 1070a05a722fSdrh ** INSERTs. RowidChng means that the new rowid is explicitly specified by 1071a05a722fSdrh ** the update or insert statement. If rowidChng is false, it means that 1072a05a722fSdrh ** the rowid is computed automatically in an insert or that the rowid value 1073a05a722fSdrh ** is not modified by the update. 10740ca3e24bSdrh ** 1075aa9b8963Sdrh ** The code generated by this routine store new index entries into 1076aa9b8963Sdrh ** registers identified by aRegIdx[]. No index entry is created for 1077aa9b8963Sdrh ** indices where aRegIdx[i]==0. The order of indices in aRegIdx[] is 1078aa9b8963Sdrh ** the same as the order of indices on the linked list of indices 1079aa9b8963Sdrh ** attached to the table. 10809cfcf5d4Sdrh ** 10819cfcf5d4Sdrh ** This routine also generates code to check constraints. NOT NULL, 10829cfcf5d4Sdrh ** CHECK, and UNIQUE constraints are all checked. If a constraint fails, 10831c92853dSdrh ** then the appropriate action is performed. There are five possible 10841c92853dSdrh ** actions: ROLLBACK, ABORT, FAIL, REPLACE, and IGNORE. 10859cfcf5d4Sdrh ** 10869cfcf5d4Sdrh ** Constraint type Action What Happens 10879cfcf5d4Sdrh ** --------------- ---------- ---------------------------------------- 10881c92853dSdrh ** any ROLLBACK The current transaction is rolled back and 108924b03fd0Sdanielk1977 ** sqlite3_exec() returns immediately with a 10909cfcf5d4Sdrh ** return code of SQLITE_CONSTRAINT. 10919cfcf5d4Sdrh ** 10921c92853dSdrh ** any ABORT Back out changes from the current command 10931c92853dSdrh ** only (do not do a complete rollback) then 109424b03fd0Sdanielk1977 ** cause sqlite3_exec() to return immediately 10951c92853dSdrh ** with SQLITE_CONSTRAINT. 10961c92853dSdrh ** 10971c92853dSdrh ** any FAIL Sqlite_exec() returns immediately with a 10981c92853dSdrh ** return code of SQLITE_CONSTRAINT. The 10991c92853dSdrh ** transaction is not rolled back and any 11001c92853dSdrh ** prior changes are retained. 11011c92853dSdrh ** 11029cfcf5d4Sdrh ** any IGNORE The record number and data is popped from 11039cfcf5d4Sdrh ** the stack and there is an immediate jump 11049cfcf5d4Sdrh ** to label ignoreDest. 11059cfcf5d4Sdrh ** 11069cfcf5d4Sdrh ** NOT NULL REPLACE The NULL value is replace by the default 11079cfcf5d4Sdrh ** value for that column. If the default value 11089cfcf5d4Sdrh ** is NULL, the action is the same as ABORT. 11099cfcf5d4Sdrh ** 11109cfcf5d4Sdrh ** UNIQUE REPLACE The other row that conflicts with the row 11119cfcf5d4Sdrh ** being inserted is removed. 11129cfcf5d4Sdrh ** 11139cfcf5d4Sdrh ** CHECK REPLACE Illegal. The results in an exception. 11149cfcf5d4Sdrh ** 11151c92853dSdrh ** Which action to take is determined by the overrideError parameter. 11161c92853dSdrh ** Or if overrideError==OE_Default, then the pParse->onError parameter 11171c92853dSdrh ** is used. Or if pParse->onError==OE_Default then the onError value 11181c92853dSdrh ** for the constraint is used. 11199cfcf5d4Sdrh ** 1120aaab5725Sdrh ** The calling routine must open a read/write cursor for pTab with 112104adf416Sdrh ** cursor number "baseCur". All indices of pTab must also have open 112204adf416Sdrh ** read/write cursors with cursor number baseCur+i for the i-th cursor. 11239cfcf5d4Sdrh ** Except, if there is no possibility of a REPLACE action then 1124aa9b8963Sdrh ** cursors do not need to be open for indices where aRegIdx[i]==0. 11259cfcf5d4Sdrh */ 11264adee20fSdanielk1977 void sqlite3GenerateConstraintChecks( 11279cfcf5d4Sdrh Parse *pParse, /* The parser context */ 11289cfcf5d4Sdrh Table *pTab, /* the table into which we are inserting */ 112904adf416Sdrh int baseCur, /* Index of a read/write cursor pointing at pTab */ 113004adf416Sdrh int regRowid, /* Index of the range of input registers */ 1131aa9b8963Sdrh int *aRegIdx, /* Register used by each index. 0 for unused indices */ 1132a05a722fSdrh int rowidChng, /* True if the rowid might collide with existing entry */ 1133b419a926Sdrh int isUpdate, /* True for UPDATE, False for INSERT */ 11349cfcf5d4Sdrh int overrideError, /* Override onError to this if not OE_Default */ 1135de630353Sdanielk1977 int ignoreDest, /* Jump to this label on an OE_Ignore resolution */ 1136de630353Sdanielk1977 int *pbMayReplace /* OUT: Set to true if constraint may cause a replace */ 11379cfcf5d4Sdrh ){ 11381b7ecbb4Sdrh int i; /* loop counter */ 11391b7ecbb4Sdrh Vdbe *v; /* VDBE under constrution */ 11401b7ecbb4Sdrh int nCol; /* Number of columns */ 11411b7ecbb4Sdrh int onError; /* Conflict resolution strategy */ 11421bd10f8aSdrh int j1; /* Addresss of jump instruction */ 11431bd10f8aSdrh int j2 = 0, j3; /* Addresses of jump instructions */ 114404adf416Sdrh int regData; /* Register containing first data column */ 11451b7ecbb4Sdrh int iCur; /* Table cursor number */ 11461b7ecbb4Sdrh Index *pIdx; /* Pointer to one of the indices */ 11471b7ecbb4Sdrh int seenReplace = 0; /* True if REPLACE is used to resolve INT PK conflict */ 1148f0863fe5Sdrh int hasTwoRowids = (isUpdate && rowidChng); 11499cfcf5d4Sdrh 11504adee20fSdanielk1977 v = sqlite3GetVdbe(pParse); 11519cfcf5d4Sdrh assert( v!=0 ); 1152417be79cSdrh assert( pTab->pSelect==0 ); /* This table is not a VIEW */ 11539cfcf5d4Sdrh nCol = pTab->nCol; 1154aa9b8963Sdrh regData = regRowid + 1; 1155aa9b8963Sdrh 11569cfcf5d4Sdrh /* Test all NOT NULL constraints. 11579cfcf5d4Sdrh */ 11589cfcf5d4Sdrh for(i=0; i<nCol; i++){ 11590ca3e24bSdrh if( i==pTab->iPKey ){ 11600ca3e24bSdrh continue; 11610ca3e24bSdrh } 11629cfcf5d4Sdrh onError = pTab->aCol[i].notNull; 11630ca3e24bSdrh if( onError==OE_None ) continue; 11649cfcf5d4Sdrh if( overrideError!=OE_Default ){ 11659cfcf5d4Sdrh onError = overrideError; 1166a996e477Sdrh }else if( onError==OE_Default ){ 1167a996e477Sdrh onError = OE_Abort; 11689cfcf5d4Sdrh } 11697977a17fSdanielk1977 if( onError==OE_Replace && pTab->aCol[i].pDflt==0 ){ 11709cfcf5d4Sdrh onError = OE_Abort; 11719cfcf5d4Sdrh } 1172b84f96f8Sdanielk1977 assert( onError==OE_Rollback || onError==OE_Abort || onError==OE_Fail 1173b84f96f8Sdanielk1977 || onError==OE_Ignore || onError==OE_Replace ); 11749cfcf5d4Sdrh switch( onError ){ 11751c92853dSdrh case OE_Rollback: 11761c92853dSdrh case OE_Abort: 11771c92853dSdrh case OE_Fail: { 1178f089aa45Sdrh char *zMsg; 11795053a79bSdrh j1 = sqlite3VdbeAddOp3(v, OP_HaltIfNull, 11805053a79bSdrh SQLITE_CONSTRAINT, onError, regData+i); 1181f089aa45Sdrh zMsg = sqlite3MPrintf(pParse->db, "%s.%s may not be NULL", 1182f089aa45Sdrh pTab->zName, pTab->aCol[i].zName); 118366a5167bSdrh sqlite3VdbeChangeP4(v, -1, zMsg, P4_DYNAMIC); 11849cfcf5d4Sdrh break; 11859cfcf5d4Sdrh } 11869cfcf5d4Sdrh case OE_Ignore: { 11875053a79bSdrh sqlite3VdbeAddOp2(v, OP_IsNull, regData+i, ignoreDest); 11889cfcf5d4Sdrh break; 11899cfcf5d4Sdrh } 1190098d1684Sdrh default: { 1191098d1684Sdrh assert( onError==OE_Replace ); 11925053a79bSdrh j1 = sqlite3VdbeAddOp1(v, OP_NotNull, regData+i); 119304adf416Sdrh sqlite3ExprCode(pParse, pTab->aCol[i].pDflt, regData+i); 11945053a79bSdrh sqlite3VdbeJumpHere(v, j1); 11959cfcf5d4Sdrh break; 11969cfcf5d4Sdrh } 11979cfcf5d4Sdrh } 11989cfcf5d4Sdrh } 11999cfcf5d4Sdrh 12009cfcf5d4Sdrh /* Test all CHECK constraints 12019cfcf5d4Sdrh */ 1202ffe07b2dSdrh #ifndef SQLITE_OMIT_CHECK 12030cd2d4c9Sdrh if( pTab->pCheck && (pParse->db->flags & SQLITE_IgnoreChecks)==0 ){ 1204ffe07b2dSdrh int allOk = sqlite3VdbeMakeLabel(v); 1205aa9b8963Sdrh pParse->ckBase = regData; 120635573356Sdrh sqlite3ExprIfTrue(pParse, pTab->pCheck, allOk, SQLITE_JUMPIFNULL); 1207aa01c7e2Sdrh onError = overrideError!=OE_Default ? overrideError : OE_Abort; 12082e06c67cSdrh if( onError==OE_Ignore ){ 120966a5167bSdrh sqlite3VdbeAddOp2(v, OP_Goto, 0, ignoreDest); 1210aa01c7e2Sdrh }else{ 121166a5167bSdrh sqlite3VdbeAddOp2(v, OP_Halt, SQLITE_CONSTRAINT, onError); 1212aa01c7e2Sdrh } 1213ffe07b2dSdrh sqlite3VdbeResolveLabel(v, allOk); 1214ffe07b2dSdrh } 1215ffe07b2dSdrh #endif /* !defined(SQLITE_OMIT_CHECK) */ 12169cfcf5d4Sdrh 12170bd1f4eaSdrh /* If we have an INTEGER PRIMARY KEY, make sure the primary key 12180bd1f4eaSdrh ** of the new record does not previously exist. Except, if this 12190bd1f4eaSdrh ** is an UPDATE and the primary key is not changing, that is OK. 12209cfcf5d4Sdrh */ 1221f0863fe5Sdrh if( rowidChng ){ 12220ca3e24bSdrh onError = pTab->keyConf; 12230ca3e24bSdrh if( overrideError!=OE_Default ){ 12240ca3e24bSdrh onError = overrideError; 1225a996e477Sdrh }else if( onError==OE_Default ){ 1226a996e477Sdrh onError = OE_Abort; 12270ca3e24bSdrh } 1228a0217ba7Sdrh 122960a713c6Sdrh if( onError!=OE_Replace || pTab->pIndex ){ 123079b0c956Sdrh if( isUpdate ){ 1231*76d462eeSdan j2 = sqlite3VdbeAddOp3(v, OP_Eq, regRowid, 0, rowidChng); 123279b0c956Sdrh } 123304adf416Sdrh j3 = sqlite3VdbeAddOp3(v, OP_NotExists, baseCur, 0, regRowid); 12340ca3e24bSdrh switch( onError ){ 1235a0217ba7Sdrh default: { 1236a0217ba7Sdrh onError = OE_Abort; 1237a0217ba7Sdrh /* Fall thru into the next case */ 1238a0217ba7Sdrh } 12391c92853dSdrh case OE_Rollback: 12401c92853dSdrh case OE_Abort: 12411c92853dSdrh case OE_Fail: { 124266a5167bSdrh sqlite3VdbeAddOp4(v, OP_Halt, SQLITE_CONSTRAINT, onError, 0, 124366a5167bSdrh "PRIMARY KEY must be unique", P4_STATIC); 12440ca3e24bSdrh break; 12450ca3e24bSdrh } 12465383ae5cSdrh case OE_Replace: { 12472d401ab8Sdrh sqlite3GenerateRowIndexDelete(pParse, pTab, baseCur, 0); 12485383ae5cSdrh seenReplace = 1; 12495383ae5cSdrh break; 12505383ae5cSdrh } 12510ca3e24bSdrh case OE_Ignore: { 12525383ae5cSdrh assert( seenReplace==0 ); 125366a5167bSdrh sqlite3VdbeAddOp2(v, OP_Goto, 0, ignoreDest); 12540ca3e24bSdrh break; 12550ca3e24bSdrh } 12560ca3e24bSdrh } 1257aa9b8963Sdrh sqlite3VdbeJumpHere(v, j3); 1258f5905aa7Sdrh if( isUpdate ){ 1259aa9b8963Sdrh sqlite3VdbeJumpHere(v, j2); 1260a05a722fSdrh } 12610ca3e24bSdrh } 12620ca3e24bSdrh } 12630bd1f4eaSdrh 12640bd1f4eaSdrh /* Test all UNIQUE constraints by creating entries for each UNIQUE 12650bd1f4eaSdrh ** index and making sure that duplicate entries do not already exist. 12660bd1f4eaSdrh ** Add the new records to the indices as we go. 12670bd1f4eaSdrh */ 1268b2fe7d8cSdrh for(iCur=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, iCur++){ 12692d401ab8Sdrh int regIdx; 12702d401ab8Sdrh int regR; 12712d401ab8Sdrh 1272aa9b8963Sdrh if( aRegIdx[iCur]==0 ) continue; /* Skip unused indices */ 1273b2fe7d8cSdrh 1274b2fe7d8cSdrh /* Create a key for accessing the index entry */ 12752d401ab8Sdrh regIdx = sqlite3GetTempRange(pParse, pIdx->nColumn+1); 12769cfcf5d4Sdrh for(i=0; i<pIdx->nColumn; i++){ 12779cfcf5d4Sdrh int idx = pIdx->aiColumn[i]; 12789cfcf5d4Sdrh if( idx==pTab->iPKey ){ 12792d401ab8Sdrh sqlite3VdbeAddOp2(v, OP_SCopy, regRowid, regIdx+i); 12809cfcf5d4Sdrh }else{ 12812d401ab8Sdrh sqlite3VdbeAddOp2(v, OP_SCopy, regData+idx, regIdx+i); 12829cfcf5d4Sdrh } 12839cfcf5d4Sdrh } 12842d401ab8Sdrh sqlite3VdbeAddOp2(v, OP_SCopy, regRowid, regIdx+i); 12851db639ceSdrh sqlite3VdbeAddOp3(v, OP_MakeRecord, regIdx, pIdx->nColumn+1, aRegIdx[iCur]); 128669f8bb9cSdan sqlite3VdbeChangeP4(v, -1, sqlite3IndexAffinityStr(v, pIdx), 0); 1287da250ea5Sdrh sqlite3ExprCacheAffinityChange(pParse, regIdx, pIdx->nColumn+1); 1288b2fe7d8cSdrh 1289b2fe7d8cSdrh /* Find out what action to take in case there is an indexing conflict */ 12909cfcf5d4Sdrh onError = pIdx->onError; 1291de630353Sdanielk1977 if( onError==OE_None ){ 1292de630353Sdanielk1977 sqlite3ReleaseTempRange(pParse, regIdx, pIdx->nColumn+1); 1293de630353Sdanielk1977 continue; /* pIdx is not a UNIQUE index */ 1294de630353Sdanielk1977 } 12959cfcf5d4Sdrh if( overrideError!=OE_Default ){ 12969cfcf5d4Sdrh onError = overrideError; 1297a996e477Sdrh }else if( onError==OE_Default ){ 1298a996e477Sdrh onError = OE_Abort; 12999cfcf5d4Sdrh } 13005383ae5cSdrh if( seenReplace ){ 13015383ae5cSdrh if( onError==OE_Ignore ) onError = OE_Replace; 13025383ae5cSdrh else if( onError==OE_Fail ) onError = OE_Abort; 13035383ae5cSdrh } 13045383ae5cSdrh 1305b2fe7d8cSdrh 1306b2fe7d8cSdrh /* Check to see if the new index entry will be unique */ 13072d401ab8Sdrh regR = sqlite3GetTempReg(pParse); 13082d401ab8Sdrh sqlite3VdbeAddOp2(v, OP_SCopy, regRowid-hasTwoRowids, regR); 13092d401ab8Sdrh j3 = sqlite3VdbeAddOp4(v, OP_IsUnique, baseCur+iCur+1, 0, 1310de630353Sdanielk1977 regR, SQLITE_INT_TO_PTR(regIdx), 1311a9e852b6Smlcreech P4_INT32); 1312de630353Sdanielk1977 sqlite3ReleaseTempRange(pParse, regIdx, pIdx->nColumn+1); 1313b2fe7d8cSdrh 1314b2fe7d8cSdrh /* Generate code that executes if the new index entry is not unique */ 1315b84f96f8Sdanielk1977 assert( onError==OE_Rollback || onError==OE_Abort || onError==OE_Fail 1316b84f96f8Sdanielk1977 || onError==OE_Ignore || onError==OE_Replace ); 13179cfcf5d4Sdrh switch( onError ){ 13181c92853dSdrh case OE_Rollback: 13191c92853dSdrh case OE_Abort: 13201c92853dSdrh case OE_Fail: { 1321098d1684Sdrh int j; 1322098d1684Sdrh StrAccum errMsg; 1323098d1684Sdrh const char *zSep; 1324098d1684Sdrh char *zErr; 1325098d1684Sdrh 1326098d1684Sdrh sqlite3StrAccumInit(&errMsg, 0, 0, 200); 1327098d1684Sdrh errMsg.db = pParse->db; 1328098d1684Sdrh zSep = pIdx->nColumn>1 ? "columns " : "column "; 1329098d1684Sdrh for(j=0; j<pIdx->nColumn; j++){ 133037ed48edSdrh char *zCol = pTab->aCol[pIdx->aiColumn[j]].zName; 1331098d1684Sdrh sqlite3StrAccumAppend(&errMsg, zSep, -1); 1332098d1684Sdrh zSep = ", "; 1333098d1684Sdrh sqlite3StrAccumAppend(&errMsg, zCol, -1); 133437ed48edSdrh } 1335098d1684Sdrh sqlite3StrAccumAppend(&errMsg, 1336098d1684Sdrh pIdx->nColumn>1 ? " are not unique" : " is not unique", -1); 1337098d1684Sdrh zErr = sqlite3StrAccumFinish(&errMsg); 1338098d1684Sdrh sqlite3VdbeAddOp4(v, OP_Halt, SQLITE_CONSTRAINT, onError, 0, zErr, 0); 1339098d1684Sdrh sqlite3DbFree(errMsg.db, zErr); 13409cfcf5d4Sdrh break; 13419cfcf5d4Sdrh } 13429cfcf5d4Sdrh case OE_Ignore: { 13430ca3e24bSdrh assert( seenReplace==0 ); 134466a5167bSdrh sqlite3VdbeAddOp2(v, OP_Goto, 0, ignoreDest); 13459cfcf5d4Sdrh break; 13469cfcf5d4Sdrh } 1347098d1684Sdrh default: { 1348098d1684Sdrh assert( onError==OE_Replace ); 13492d401ab8Sdrh sqlite3GenerateRowDelete(pParse, pTab, baseCur, regR, 0); 13500ca3e24bSdrh seenReplace = 1; 13519cfcf5d4Sdrh break; 13529cfcf5d4Sdrh } 13539cfcf5d4Sdrh } 13542d401ab8Sdrh sqlite3VdbeJumpHere(v, j3); 13552d401ab8Sdrh sqlite3ReleaseTempReg(pParse, regR); 13569cfcf5d4Sdrh } 1357de630353Sdanielk1977 1358de630353Sdanielk1977 if( pbMayReplace ){ 1359de630353Sdanielk1977 *pbMayReplace = seenReplace; 1360de630353Sdanielk1977 } 13619cfcf5d4Sdrh } 13620ca3e24bSdrh 13630ca3e24bSdrh /* 13640ca3e24bSdrh ** This routine generates code to finish the INSERT or UPDATE operation 13654adee20fSdanielk1977 ** that was started by a prior call to sqlite3GenerateConstraintChecks. 136604adf416Sdrh ** A consecutive range of registers starting at regRowid contains the 136704adf416Sdrh ** rowid and the content to be inserted. 13680ca3e24bSdrh ** 1369b419a926Sdrh ** The arguments to this routine should be the same as the first six 13704adee20fSdanielk1977 ** arguments to sqlite3GenerateConstraintChecks. 13710ca3e24bSdrh */ 13724adee20fSdanielk1977 void sqlite3CompleteInsertion( 13730ca3e24bSdrh Parse *pParse, /* The parser context */ 13740ca3e24bSdrh Table *pTab, /* the table into which we are inserting */ 137504adf416Sdrh int baseCur, /* Index of a read/write cursor pointing at pTab */ 137604adf416Sdrh int regRowid, /* Range of content */ 1377aa9b8963Sdrh int *aRegIdx, /* Register used by each index. 0 for unused indices */ 137870ce3f0cSdrh int isUpdate, /* True for UPDATE, False for INSERT */ 1379e4d90813Sdrh int newIdx, /* Index of NEW table for triggers. -1 if none */ 1380de630353Sdanielk1977 int appendBias, /* True if this is likely to be an append */ 1381de630353Sdanielk1977 int useSeekResult /* True to set the USESEEKRESULT flag on OP_[Idx]Insert */ 13820ca3e24bSdrh ){ 13830ca3e24bSdrh int i; 13840ca3e24bSdrh Vdbe *v; 13850ca3e24bSdrh int nIdx; 13860ca3e24bSdrh Index *pIdx; 13871bd10f8aSdrh u8 pik_flags; 138804adf416Sdrh int regData; 1389b7654111Sdrh int regRec; 13900ca3e24bSdrh 13914adee20fSdanielk1977 v = sqlite3GetVdbe(pParse); 13920ca3e24bSdrh assert( v!=0 ); 1393417be79cSdrh assert( pTab->pSelect==0 ); /* This table is not a VIEW */ 13940ca3e24bSdrh for(nIdx=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, nIdx++){} 13950ca3e24bSdrh for(i=nIdx-1; i>=0; i--){ 1396aa9b8963Sdrh if( aRegIdx[i]==0 ) continue; 139704adf416Sdrh sqlite3VdbeAddOp2(v, OP_IdxInsert, baseCur+i+1, aRegIdx[i]); 1398de630353Sdanielk1977 if( useSeekResult ){ 1399de630353Sdanielk1977 sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT); 1400de630353Sdanielk1977 } 14010ca3e24bSdrh } 140204adf416Sdrh regData = regRowid + 1; 1403b7654111Sdrh regRec = sqlite3GetTempReg(pParse); 14041db639ceSdrh sqlite3VdbeAddOp3(v, OP_MakeRecord, regData, pTab->nCol, regRec); 1405a37cdde0Sdanielk1977 sqlite3TableAffinityStr(v, pTab); 1406da250ea5Sdrh sqlite3ExprCacheAffinityChange(pParse, regData, pTab->nCol); 14074794f735Sdrh if( pParse->nested ){ 14084794f735Sdrh pik_flags = 0; 14094794f735Sdrh }else{ 141094eb6a14Sdanielk1977 pik_flags = OPFLAG_NCHANGE; 141194eb6a14Sdanielk1977 pik_flags |= (isUpdate?OPFLAG_ISUPDATE:OPFLAG_LASTROWID); 14124794f735Sdrh } 1413e4d90813Sdrh if( appendBias ){ 1414e4d90813Sdrh pik_flags |= OPFLAG_APPEND; 1415e4d90813Sdrh } 1416de630353Sdanielk1977 if( useSeekResult ){ 1417de630353Sdanielk1977 pik_flags |= OPFLAG_USESEEKRESULT; 1418de630353Sdanielk1977 } 1419b7654111Sdrh sqlite3VdbeAddOp3(v, OP_Insert, baseCur, regRec, regRowid); 142094eb6a14Sdanielk1977 if( !pParse->nested ){ 142166a5167bSdrh sqlite3VdbeChangeP4(v, -1, pTab->zName, P4_STATIC); 142294eb6a14Sdanielk1977 } 1423b7654111Sdrh sqlite3VdbeChangeP5(v, pik_flags); 14240ca3e24bSdrh } 1425cd44690aSdrh 1426cd44690aSdrh /* 1427290c1948Sdrh ** Generate code that will open cursors for a table and for all 142804adf416Sdrh ** indices of that table. The "baseCur" parameter is the cursor number used 1429cd44690aSdrh ** for the table. Indices are opened on subsequent cursors. 1430aa9b8963Sdrh ** 1431aa9b8963Sdrh ** Return the number of indices on the table. 1432cd44690aSdrh */ 1433aa9b8963Sdrh int sqlite3OpenTableAndIndices( 1434290c1948Sdrh Parse *pParse, /* Parsing context */ 1435290c1948Sdrh Table *pTab, /* Table to be opened */ 143604adf416Sdrh int baseCur, /* Cursor number assigned to the table */ 1437290c1948Sdrh int op /* OP_OpenRead or OP_OpenWrite */ 1438290c1948Sdrh ){ 1439cd44690aSdrh int i; 14404cbdda9eSdrh int iDb; 1441cd44690aSdrh Index *pIdx; 14424cbdda9eSdrh Vdbe *v; 14434cbdda9eSdrh 1444aa9b8963Sdrh if( IsVirtual(pTab) ) return 0; 14454cbdda9eSdrh iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema); 14464cbdda9eSdrh v = sqlite3GetVdbe(pParse); 1447cd44690aSdrh assert( v!=0 ); 144804adf416Sdrh sqlite3OpenTable(pParse, baseCur, iDb, pTab, op); 1449cd44690aSdrh for(i=1, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){ 1450b3bf556eSdanielk1977 KeyInfo *pKey = sqlite3IndexKeyinfo(pParse, pIdx); 1451da184236Sdanielk1977 assert( pIdx->pSchema==pTab->pSchema ); 145204adf416Sdrh sqlite3VdbeAddOp4(v, op, i+baseCur, pIdx->tnum, iDb, 145366a5167bSdrh (char*)pKey, P4_KEYINFO_HANDOFF); 1454207872a4Sdanielk1977 VdbeComment((v, "%s", pIdx->zName)); 1455cd44690aSdrh } 14561b7ecbb4Sdrh if( pParse->nTab<baseCur+i ){ 145704adf416Sdrh pParse->nTab = baseCur+i; 1458290c1948Sdrh } 1459aa9b8963Sdrh return i-1; 1460cd44690aSdrh } 14619d9cf229Sdrh 146291c58e23Sdrh 146391c58e23Sdrh #ifdef SQLITE_TEST 146491c58e23Sdrh /* 146591c58e23Sdrh ** The following global variable is incremented whenever the 146691c58e23Sdrh ** transfer optimization is used. This is used for testing 146791c58e23Sdrh ** purposes only - to make sure the transfer optimization really 146891c58e23Sdrh ** is happening when it is suppose to. 146991c58e23Sdrh */ 147091c58e23Sdrh int sqlite3_xferopt_count; 147191c58e23Sdrh #endif /* SQLITE_TEST */ 147291c58e23Sdrh 147391c58e23Sdrh 14749d9cf229Sdrh #ifndef SQLITE_OMIT_XFER_OPT 14759d9cf229Sdrh /* 14769d9cf229Sdrh ** Check to collation names to see if they are compatible. 14779d9cf229Sdrh */ 14789d9cf229Sdrh static int xferCompatibleCollation(const char *z1, const char *z2){ 14799d9cf229Sdrh if( z1==0 ){ 14809d9cf229Sdrh return z2==0; 14819d9cf229Sdrh } 14829d9cf229Sdrh if( z2==0 ){ 14839d9cf229Sdrh return 0; 14849d9cf229Sdrh } 14859d9cf229Sdrh return sqlite3StrICmp(z1, z2)==0; 14869d9cf229Sdrh } 14879d9cf229Sdrh 14889d9cf229Sdrh 14899d9cf229Sdrh /* 14909d9cf229Sdrh ** Check to see if index pSrc is compatible as a source of data 14919d9cf229Sdrh ** for index pDest in an insert transfer optimization. The rules 14929d9cf229Sdrh ** for a compatible index: 14939d9cf229Sdrh ** 14949d9cf229Sdrh ** * The index is over the same set of columns 14959d9cf229Sdrh ** * The same DESC and ASC markings occurs on all columns 14969d9cf229Sdrh ** * The same onError processing (OE_Abort, OE_Ignore, etc) 14979d9cf229Sdrh ** * The same collating sequence on each column 14989d9cf229Sdrh */ 14999d9cf229Sdrh static int xferCompatibleIndex(Index *pDest, Index *pSrc){ 15009d9cf229Sdrh int i; 15019d9cf229Sdrh assert( pDest && pSrc ); 15029d9cf229Sdrh assert( pDest->pTable!=pSrc->pTable ); 15039d9cf229Sdrh if( pDest->nColumn!=pSrc->nColumn ){ 15049d9cf229Sdrh return 0; /* Different number of columns */ 15059d9cf229Sdrh } 15069d9cf229Sdrh if( pDest->onError!=pSrc->onError ){ 15079d9cf229Sdrh return 0; /* Different conflict resolution strategies */ 15089d9cf229Sdrh } 15099d9cf229Sdrh for(i=0; i<pSrc->nColumn; i++){ 15109d9cf229Sdrh if( pSrc->aiColumn[i]!=pDest->aiColumn[i] ){ 15119d9cf229Sdrh return 0; /* Different columns indexed */ 15129d9cf229Sdrh } 15139d9cf229Sdrh if( pSrc->aSortOrder[i]!=pDest->aSortOrder[i] ){ 15149d9cf229Sdrh return 0; /* Different sort orders */ 15159d9cf229Sdrh } 15163f6e781dSdrh if( !xferCompatibleCollation(pSrc->azColl[i],pDest->azColl[i]) ){ 151760a713c6Sdrh return 0; /* Different collating sequences */ 15189d9cf229Sdrh } 15199d9cf229Sdrh } 15209d9cf229Sdrh 15219d9cf229Sdrh /* If no test above fails then the indices must be compatible */ 15229d9cf229Sdrh return 1; 15239d9cf229Sdrh } 15249d9cf229Sdrh 15259d9cf229Sdrh /* 15269d9cf229Sdrh ** Attempt the transfer optimization on INSERTs of the form 15279d9cf229Sdrh ** 15289d9cf229Sdrh ** INSERT INTO tab1 SELECT * FROM tab2; 15299d9cf229Sdrh ** 15309d9cf229Sdrh ** This optimization is only attempted if 15319d9cf229Sdrh ** 15329d9cf229Sdrh ** (1) tab1 and tab2 have identical schemas including all the 15338103b7d2Sdrh ** same indices and constraints 15349d9cf229Sdrh ** 15359d9cf229Sdrh ** (2) tab1 and tab2 are different tables 15369d9cf229Sdrh ** 15379d9cf229Sdrh ** (3) There must be no triggers on tab1 15389d9cf229Sdrh ** 15399d9cf229Sdrh ** (4) The result set of the SELECT statement is "*" 15409d9cf229Sdrh ** 15419d9cf229Sdrh ** (5) The SELECT statement has no WHERE, HAVING, ORDER BY, GROUP BY, 15429d9cf229Sdrh ** or LIMIT clause. 15439d9cf229Sdrh ** 15449d9cf229Sdrh ** (6) The SELECT statement is a simple (not a compound) select that 15459d9cf229Sdrh ** contains only tab2 in its FROM clause 15469d9cf229Sdrh ** 15479d9cf229Sdrh ** This method for implementing the INSERT transfers raw records from 15489d9cf229Sdrh ** tab2 over to tab1. The columns are not decoded. Raw records from 15499d9cf229Sdrh ** the indices of tab2 are transfered to tab1 as well. In so doing, 15509d9cf229Sdrh ** the resulting tab1 has much less fragmentation. 15519d9cf229Sdrh ** 15529d9cf229Sdrh ** This routine returns TRUE if the optimization is attempted. If any 15539d9cf229Sdrh ** of the conditions above fail so that the optimization should not 15549d9cf229Sdrh ** be attempted, then this routine returns FALSE. 15559d9cf229Sdrh */ 15569d9cf229Sdrh static int xferOptimization( 15579d9cf229Sdrh Parse *pParse, /* Parser context */ 15589d9cf229Sdrh Table *pDest, /* The table we are inserting into */ 15599d9cf229Sdrh Select *pSelect, /* A SELECT statement to use as the data source */ 15609d9cf229Sdrh int onError, /* How to handle constraint errors */ 15619d9cf229Sdrh int iDbDest /* The database of pDest */ 15629d9cf229Sdrh ){ 15639d9cf229Sdrh ExprList *pEList; /* The result set of the SELECT */ 15649d9cf229Sdrh Table *pSrc; /* The table in the FROM clause of SELECT */ 15659d9cf229Sdrh Index *pSrcIdx, *pDestIdx; /* Source and destination indices */ 15669d9cf229Sdrh struct SrcList_item *pItem; /* An element of pSelect->pSrc */ 15679d9cf229Sdrh int i; /* Loop counter */ 15689d9cf229Sdrh int iDbSrc; /* The database of pSrc */ 15699d9cf229Sdrh int iSrc, iDest; /* Cursors from source and destination */ 15709d9cf229Sdrh int addr1, addr2; /* Loop addresses */ 15719d9cf229Sdrh int emptyDestTest; /* Address of test for empty pDest */ 15729d9cf229Sdrh int emptySrcTest; /* Address of test for empty pSrc */ 15739d9cf229Sdrh Vdbe *v; /* The VDBE we are building */ 15749d9cf229Sdrh KeyInfo *pKey; /* Key information for an index */ 15756a288a33Sdrh int regAutoinc; /* Memory register used by AUTOINC */ 1576f33c9fadSdrh int destHasUniqueIdx = 0; /* True if pDest has a UNIQUE index */ 1577b7654111Sdrh int regData, regRowid; /* Registers holding data and rowid */ 15789d9cf229Sdrh 15799d9cf229Sdrh if( pSelect==0 ){ 15809d9cf229Sdrh return 0; /* Must be of the form INSERT INTO ... SELECT ... */ 15819d9cf229Sdrh } 15822f886d1dSdanielk1977 if( sqlite3TriggerList(pParse, pDest) ){ 15839d9cf229Sdrh return 0; /* tab1 must not have triggers */ 15849d9cf229Sdrh } 15859d9cf229Sdrh #ifndef SQLITE_OMIT_VIRTUALTABLE 15867d10d5a6Sdrh if( pDest->tabFlags & TF_Virtual ){ 15879d9cf229Sdrh return 0; /* tab1 must not be a virtual table */ 15889d9cf229Sdrh } 15899d9cf229Sdrh #endif 15909d9cf229Sdrh if( onError==OE_Default ){ 15919d9cf229Sdrh onError = OE_Abort; 15929d9cf229Sdrh } 15939d9cf229Sdrh if( onError!=OE_Abort && onError!=OE_Rollback ){ 15949d9cf229Sdrh return 0; /* Cannot do OR REPLACE or OR IGNORE or OR FAIL */ 15959d9cf229Sdrh } 15965ce240a6Sdanielk1977 assert(pSelect->pSrc); /* allocated even if there is no FROM clause */ 15979d9cf229Sdrh if( pSelect->pSrc->nSrc!=1 ){ 15989d9cf229Sdrh return 0; /* FROM clause must have exactly one term */ 15999d9cf229Sdrh } 16009d9cf229Sdrh if( pSelect->pSrc->a[0].pSelect ){ 16019d9cf229Sdrh return 0; /* FROM clause cannot contain a subquery */ 16029d9cf229Sdrh } 16039d9cf229Sdrh if( pSelect->pWhere ){ 16049d9cf229Sdrh return 0; /* SELECT may not have a WHERE clause */ 16059d9cf229Sdrh } 16069d9cf229Sdrh if( pSelect->pOrderBy ){ 16079d9cf229Sdrh return 0; /* SELECT may not have an ORDER BY clause */ 16089d9cf229Sdrh } 16098103b7d2Sdrh /* Do not need to test for a HAVING clause. If HAVING is present but 16108103b7d2Sdrh ** there is no ORDER BY, we will get an error. */ 16119d9cf229Sdrh if( pSelect->pGroupBy ){ 16129d9cf229Sdrh return 0; /* SELECT may not have a GROUP BY clause */ 16139d9cf229Sdrh } 16149d9cf229Sdrh if( pSelect->pLimit ){ 16159d9cf229Sdrh return 0; /* SELECT may not have a LIMIT clause */ 16169d9cf229Sdrh } 16178103b7d2Sdrh assert( pSelect->pOffset==0 ); /* Must be so if pLimit==0 */ 16189d9cf229Sdrh if( pSelect->pPrior ){ 16199d9cf229Sdrh return 0; /* SELECT may not be a compound query */ 16209d9cf229Sdrh } 16217d10d5a6Sdrh if( pSelect->selFlags & SF_Distinct ){ 16229d9cf229Sdrh return 0; /* SELECT may not be DISTINCT */ 16239d9cf229Sdrh } 16249d9cf229Sdrh pEList = pSelect->pEList; 16259d9cf229Sdrh assert( pEList!=0 ); 16269d9cf229Sdrh if( pEList->nExpr!=1 ){ 16279d9cf229Sdrh return 0; /* The result set must have exactly one column */ 16289d9cf229Sdrh } 16299d9cf229Sdrh assert( pEList->a[0].pExpr ); 16309d9cf229Sdrh if( pEList->a[0].pExpr->op!=TK_ALL ){ 16319d9cf229Sdrh return 0; /* The result set must be the special operator "*" */ 16329d9cf229Sdrh } 16339d9cf229Sdrh 16349d9cf229Sdrh /* At this point we have established that the statement is of the 16359d9cf229Sdrh ** correct syntactic form to participate in this optimization. Now 16369d9cf229Sdrh ** we have to check the semantics. 16379d9cf229Sdrh */ 16389d9cf229Sdrh pItem = pSelect->pSrc->a; 1639ca424114Sdrh pSrc = sqlite3LocateTable(pParse, 0, pItem->zName, pItem->zDatabase); 16409d9cf229Sdrh if( pSrc==0 ){ 16419d9cf229Sdrh return 0; /* FROM clause does not contain a real table */ 16429d9cf229Sdrh } 16439d9cf229Sdrh if( pSrc==pDest ){ 16449d9cf229Sdrh return 0; /* tab1 and tab2 may not be the same table */ 16459d9cf229Sdrh } 16469d9cf229Sdrh #ifndef SQLITE_OMIT_VIRTUALTABLE 16477d10d5a6Sdrh if( pSrc->tabFlags & TF_Virtual ){ 16489d9cf229Sdrh return 0; /* tab2 must not be a virtual table */ 16499d9cf229Sdrh } 16509d9cf229Sdrh #endif 16519d9cf229Sdrh if( pSrc->pSelect ){ 16529d9cf229Sdrh return 0; /* tab2 may not be a view */ 16539d9cf229Sdrh } 16549d9cf229Sdrh if( pDest->nCol!=pSrc->nCol ){ 16559d9cf229Sdrh return 0; /* Number of columns must be the same in tab1 and tab2 */ 16569d9cf229Sdrh } 16579d9cf229Sdrh if( pDest->iPKey!=pSrc->iPKey ){ 16589d9cf229Sdrh return 0; /* Both tables must have the same INTEGER PRIMARY KEY */ 16599d9cf229Sdrh } 16609d9cf229Sdrh for(i=0; i<pDest->nCol; i++){ 16619d9cf229Sdrh if( pDest->aCol[i].affinity!=pSrc->aCol[i].affinity ){ 16629d9cf229Sdrh return 0; /* Affinity must be the same on all columns */ 16639d9cf229Sdrh } 16649d9cf229Sdrh if( !xferCompatibleCollation(pDest->aCol[i].zColl, pSrc->aCol[i].zColl) ){ 16659d9cf229Sdrh return 0; /* Collating sequence must be the same on all columns */ 16669d9cf229Sdrh } 16679d9cf229Sdrh if( pDest->aCol[i].notNull && !pSrc->aCol[i].notNull ){ 16689d9cf229Sdrh return 0; /* tab2 must be NOT NULL if tab1 is */ 16699d9cf229Sdrh } 16709d9cf229Sdrh } 16719d9cf229Sdrh for(pDestIdx=pDest->pIndex; pDestIdx; pDestIdx=pDestIdx->pNext){ 1672f33c9fadSdrh if( pDestIdx->onError!=OE_None ){ 1673f33c9fadSdrh destHasUniqueIdx = 1; 1674f33c9fadSdrh } 16759d9cf229Sdrh for(pSrcIdx=pSrc->pIndex; pSrcIdx; pSrcIdx=pSrcIdx->pNext){ 16769d9cf229Sdrh if( xferCompatibleIndex(pDestIdx, pSrcIdx) ) break; 16779d9cf229Sdrh } 16789d9cf229Sdrh if( pSrcIdx==0 ){ 16799d9cf229Sdrh return 0; /* pDestIdx has no corresponding index in pSrc */ 16809d9cf229Sdrh } 16819d9cf229Sdrh } 16827fc2f41bSdrh #ifndef SQLITE_OMIT_CHECK 1683fb658dedSdrh if( pDest->pCheck && !sqlite3ExprCompare(pSrc->pCheck, pDest->pCheck) ){ 16848103b7d2Sdrh return 0; /* Tables have different CHECK constraints. Ticket #2252 */ 16858103b7d2Sdrh } 16867fc2f41bSdrh #endif 16879d9cf229Sdrh 16889d9cf229Sdrh /* If we get this far, it means either: 16899d9cf229Sdrh ** 16909d9cf229Sdrh ** * We can always do the transfer if the table contains an 16919d9cf229Sdrh ** an integer primary key 16929d9cf229Sdrh ** 16939d9cf229Sdrh ** * We can conditionally do the transfer if the destination 16949d9cf229Sdrh ** table is empty. 16959d9cf229Sdrh */ 1696dd73521bSdrh #ifdef SQLITE_TEST 1697dd73521bSdrh sqlite3_xferopt_count++; 1698dd73521bSdrh #endif 16999d9cf229Sdrh iDbSrc = sqlite3SchemaToIndex(pParse->db, pSrc->pSchema); 17009d9cf229Sdrh v = sqlite3GetVdbe(pParse); 1701f53e9b5aSdrh sqlite3CodeVerifySchema(pParse, iDbSrc); 17029d9cf229Sdrh iSrc = pParse->nTab++; 17039d9cf229Sdrh iDest = pParse->nTab++; 17046a288a33Sdrh regAutoinc = autoIncBegin(pParse, iDbDest, pDest); 17059d9cf229Sdrh sqlite3OpenTable(pParse, iDest, iDbDest, pDest, OP_OpenWrite); 1706f33c9fadSdrh if( (pDest->iPKey<0 && pDest->pIndex!=0) || destHasUniqueIdx ){ 1707bd36ba69Sdrh /* If tables do not have an INTEGER PRIMARY KEY and there 1708bd36ba69Sdrh ** are indices to be copied and the destination is not empty, 1709bd36ba69Sdrh ** we have to disallow the transfer optimization because the 1710bd36ba69Sdrh ** the rowids might change which will mess up indexing. 1711f33c9fadSdrh ** 1712f33c9fadSdrh ** Or if the destination has a UNIQUE index and is not empty, 1713f33c9fadSdrh ** we also disallow the transfer optimization because we cannot 1714f33c9fadSdrh ** insure that all entries in the union of DEST and SRC will be 1715f33c9fadSdrh ** unique. 17169d9cf229Sdrh */ 171766a5167bSdrh addr1 = sqlite3VdbeAddOp2(v, OP_Rewind, iDest, 0); 171866a5167bSdrh emptyDestTest = sqlite3VdbeAddOp2(v, OP_Goto, 0, 0); 17199d9cf229Sdrh sqlite3VdbeJumpHere(v, addr1); 17209d9cf229Sdrh }else{ 17219d9cf229Sdrh emptyDestTest = 0; 17229d9cf229Sdrh } 17239d9cf229Sdrh sqlite3OpenTable(pParse, iSrc, iDbSrc, pSrc, OP_OpenRead); 172466a5167bSdrh emptySrcTest = sqlite3VdbeAddOp2(v, OP_Rewind, iSrc, 0); 1725b7654111Sdrh regData = sqlite3GetTempReg(pParse); 1726b7654111Sdrh regRowid = sqlite3GetTempReg(pParse); 172742242dedSdrh if( pDest->iPKey>=0 ){ 1728b7654111Sdrh addr1 = sqlite3VdbeAddOp2(v, OP_Rowid, iSrc, regRowid); 1729b7654111Sdrh addr2 = sqlite3VdbeAddOp3(v, OP_NotExists, iDest, 0, regRowid); 173066a5167bSdrh sqlite3VdbeAddOp4(v, OP_Halt, SQLITE_CONSTRAINT, onError, 0, 173166a5167bSdrh "PRIMARY KEY must be unique", P4_STATIC); 17329d9cf229Sdrh sqlite3VdbeJumpHere(v, addr2); 1733b7654111Sdrh autoIncStep(pParse, regAutoinc, regRowid); 1734bd36ba69Sdrh }else if( pDest->pIndex==0 ){ 1735b7654111Sdrh addr1 = sqlite3VdbeAddOp2(v, OP_NewRowid, iDest, regRowid); 173695bad4c7Sdrh }else{ 1737b7654111Sdrh addr1 = sqlite3VdbeAddOp2(v, OP_Rowid, iSrc, regRowid); 17387d10d5a6Sdrh assert( (pDest->tabFlags & TF_Autoincrement)==0 ); 173995bad4c7Sdrh } 1740b7654111Sdrh sqlite3VdbeAddOp2(v, OP_RowData, iSrc, regData); 1741b7654111Sdrh sqlite3VdbeAddOp3(v, OP_Insert, iDest, regData, regRowid); 1742b7654111Sdrh sqlite3VdbeChangeP5(v, OPFLAG_NCHANGE|OPFLAG_LASTROWID|OPFLAG_APPEND); 17431f4aa337Sdanielk1977 sqlite3VdbeChangeP4(v, -1, pDest->zName, 0); 174466a5167bSdrh sqlite3VdbeAddOp2(v, OP_Next, iSrc, addr1); 17459d9cf229Sdrh for(pDestIdx=pDest->pIndex; pDestIdx; pDestIdx=pDestIdx->pNext){ 17461b7ecbb4Sdrh for(pSrcIdx=pSrc->pIndex; ALWAYS(pSrcIdx); pSrcIdx=pSrcIdx->pNext){ 17479d9cf229Sdrh if( xferCompatibleIndex(pDestIdx, pSrcIdx) ) break; 17489d9cf229Sdrh } 17499d9cf229Sdrh assert( pSrcIdx ); 175066a5167bSdrh sqlite3VdbeAddOp2(v, OP_Close, iSrc, 0); 175166a5167bSdrh sqlite3VdbeAddOp2(v, OP_Close, iDest, 0); 17529d9cf229Sdrh pKey = sqlite3IndexKeyinfo(pParse, pSrcIdx); 1753207872a4Sdanielk1977 sqlite3VdbeAddOp4(v, OP_OpenRead, iSrc, pSrcIdx->tnum, iDbSrc, 1754207872a4Sdanielk1977 (char*)pKey, P4_KEYINFO_HANDOFF); 1755d4e70ebdSdrh VdbeComment((v, "%s", pSrcIdx->zName)); 17569d9cf229Sdrh pKey = sqlite3IndexKeyinfo(pParse, pDestIdx); 1757207872a4Sdanielk1977 sqlite3VdbeAddOp4(v, OP_OpenWrite, iDest, pDestIdx->tnum, iDbDest, 175866a5167bSdrh (char*)pKey, P4_KEYINFO_HANDOFF); 1759207872a4Sdanielk1977 VdbeComment((v, "%s", pDestIdx->zName)); 176066a5167bSdrh addr1 = sqlite3VdbeAddOp2(v, OP_Rewind, iSrc, 0); 1761b7654111Sdrh sqlite3VdbeAddOp2(v, OP_RowKey, iSrc, regData); 1762b7654111Sdrh sqlite3VdbeAddOp3(v, OP_IdxInsert, iDest, regData, 1); 176366a5167bSdrh sqlite3VdbeAddOp2(v, OP_Next, iSrc, addr1+1); 17649d9cf229Sdrh sqlite3VdbeJumpHere(v, addr1); 17659d9cf229Sdrh } 17669d9cf229Sdrh sqlite3VdbeJumpHere(v, emptySrcTest); 1767b7654111Sdrh sqlite3ReleaseTempReg(pParse, regRowid); 1768b7654111Sdrh sqlite3ReleaseTempReg(pParse, regData); 176966a5167bSdrh sqlite3VdbeAddOp2(v, OP_Close, iSrc, 0); 177066a5167bSdrh sqlite3VdbeAddOp2(v, OP_Close, iDest, 0); 17719d9cf229Sdrh if( emptyDestTest ){ 177266a5167bSdrh sqlite3VdbeAddOp2(v, OP_Halt, SQLITE_OK, 0); 17739d9cf229Sdrh sqlite3VdbeJumpHere(v, emptyDestTest); 177466a5167bSdrh sqlite3VdbeAddOp2(v, OP_Close, iDest, 0); 17759d9cf229Sdrh return 0; 17769d9cf229Sdrh }else{ 17779d9cf229Sdrh return 1; 17789d9cf229Sdrh } 17799d9cf229Sdrh } 17809d9cf229Sdrh #endif /* SQLITE_OMIT_XFER_OPT */ 1781f39d9588Sdrh 1782f39d9588Sdrh /* Make sure "isView" gets undefined in case this file becomes part of 1783f39d9588Sdrh ** the amalgamation - so that subsequent files do not see isView as a 1784f39d9588Sdrh ** macro. */ 1785f39d9588Sdrh #undef isView 1786