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 ** 15*9170dd7eSdrh ** $Id: insert.c,v 1.141 2005/07/08 17:13:47 drh Exp $ 16cce7d176Sdrh */ 17cce7d176Sdrh #include "sqliteInt.h" 18cce7d176Sdrh 19cce7d176Sdrh /* 203d1bfeaaSdanielk1977 ** Set P3 of the most recently inserted opcode to a column affinity 21a37cdde0Sdanielk1977 ** string for index pIdx. A column affinity string has one character 223d1bfeaaSdanielk1977 ** for each column in the table, according to the affinity of the column: 233d1bfeaaSdanielk1977 ** 243d1bfeaaSdanielk1977 ** Character Column affinity 253d1bfeaaSdanielk1977 ** ------------------------------ 263d1bfeaaSdanielk1977 ** 'n' NUMERIC 273d1bfeaaSdanielk1977 ** 'i' INTEGER 283d1bfeaaSdanielk1977 ** 't' TEXT 293d1bfeaaSdanielk1977 ** 'o' NONE 303d1bfeaaSdanielk1977 */ 31a37cdde0Sdanielk1977 void sqlite3IndexAffinityStr(Vdbe *v, Index *pIdx){ 32a37cdde0Sdanielk1977 if( !pIdx->zColAff ){ 33e014a838Sdanielk1977 /* The first time a column affinity string for a particular index is 34a37cdde0Sdanielk1977 ** required, it is allocated and populated here. It is then stored as 35e014a838Sdanielk1977 ** a member of the Index structure for subsequent use. 36a37cdde0Sdanielk1977 ** 37a37cdde0Sdanielk1977 ** The column affinity string will eventually be deleted by 38e014a838Sdanielk1977 ** sqliteDeleteIndex() when the Index structure itself is cleaned 39a37cdde0Sdanielk1977 ** up. 40a37cdde0Sdanielk1977 */ 41a37cdde0Sdanielk1977 int n; 42a37cdde0Sdanielk1977 Table *pTab = pIdx->pTable; 43a37cdde0Sdanielk1977 pIdx->zColAff = (char *)sqliteMalloc(pIdx->nColumn+1); 44a37cdde0Sdanielk1977 if( !pIdx->zColAff ){ 45a37cdde0Sdanielk1977 return; 46a37cdde0Sdanielk1977 } 47a37cdde0Sdanielk1977 for(n=0; n<pIdx->nColumn; n++){ 48a37cdde0Sdanielk1977 pIdx->zColAff[n] = pTab->aCol[pIdx->aiColumn[n]].affinity; 49a37cdde0Sdanielk1977 } 50a37cdde0Sdanielk1977 pIdx->zColAff[pIdx->nColumn] = '\0'; 51a37cdde0Sdanielk1977 } 523d1bfeaaSdanielk1977 53956bc92cSdrh sqlite3VdbeChangeP3(v, -1, pIdx->zColAff, 0); 54a37cdde0Sdanielk1977 } 55a37cdde0Sdanielk1977 56a37cdde0Sdanielk1977 /* 57a37cdde0Sdanielk1977 ** Set P3 of the most recently inserted opcode to a column affinity 58a37cdde0Sdanielk1977 ** string for table pTab. A column affinity string has one character 59a37cdde0Sdanielk1977 ** for each column indexed by the index, according to the affinity of the 60a37cdde0Sdanielk1977 ** column: 61a37cdde0Sdanielk1977 ** 62a37cdde0Sdanielk1977 ** Character Column affinity 63a37cdde0Sdanielk1977 ** ------------------------------ 64a37cdde0Sdanielk1977 ** 'n' NUMERIC 65a37cdde0Sdanielk1977 ** 'i' INTEGER 66a37cdde0Sdanielk1977 ** 't' TEXT 67a37cdde0Sdanielk1977 ** 'o' NONE 68a37cdde0Sdanielk1977 */ 69a37cdde0Sdanielk1977 void sqlite3TableAffinityStr(Vdbe *v, Table *pTab){ 703d1bfeaaSdanielk1977 /* The first time a column affinity string for a particular table 713d1bfeaaSdanielk1977 ** is required, it is allocated and populated here. It is then 723d1bfeaaSdanielk1977 ** stored as a member of the Table structure for subsequent use. 733d1bfeaaSdanielk1977 ** 743d1bfeaaSdanielk1977 ** The column affinity string will eventually be deleted by 753d1bfeaaSdanielk1977 ** sqlite3DeleteTable() when the Table structure itself is cleaned up. 763d1bfeaaSdanielk1977 */ 773d1bfeaaSdanielk1977 if( !pTab->zColAff ){ 783d1bfeaaSdanielk1977 char *zColAff; 793d1bfeaaSdanielk1977 int i; 803d1bfeaaSdanielk1977 81a37cdde0Sdanielk1977 zColAff = (char *)sqliteMalloc(pTab->nCol+1); 823d1bfeaaSdanielk1977 if( !zColAff ){ 83a37cdde0Sdanielk1977 return; 843d1bfeaaSdanielk1977 } 853d1bfeaaSdanielk1977 863d1bfeaaSdanielk1977 for(i=0; i<pTab->nCol; i++){ 87a37cdde0Sdanielk1977 zColAff[i] = pTab->aCol[i].affinity; 883d1bfeaaSdanielk1977 } 893d1bfeaaSdanielk1977 zColAff[pTab->nCol] = '\0'; 903d1bfeaaSdanielk1977 913d1bfeaaSdanielk1977 pTab->zColAff = zColAff; 923d1bfeaaSdanielk1977 } 933d1bfeaaSdanielk1977 94956bc92cSdrh sqlite3VdbeChangeP3(v, -1, pTab->zColAff, 0); 953d1bfeaaSdanielk1977 } 963d1bfeaaSdanielk1977 974d88778bSdanielk1977 /* 984d88778bSdanielk1977 ** Return non-zero if SELECT statement p opens the table with rootpage 994d88778bSdanielk1977 ** iTab in database iDb. This is used to see if a statement of the form 1004d88778bSdanielk1977 ** "INSERT INTO <iDb, iTab> SELECT ..." can run without using temporary 1014d88778bSdanielk1977 ** table for the results of the SELECT. 1024d88778bSdanielk1977 ** 1034d88778bSdanielk1977 ** No checking is done for sub-selects that are part of expressions. 1044d88778bSdanielk1977 */ 1054d88778bSdanielk1977 static int selectReadsTable(Select *p, int iDb, int iTab){ 1064d88778bSdanielk1977 int i; 1074d88778bSdanielk1977 struct SrcList_item *pItem; 1084d88778bSdanielk1977 if( p->pSrc==0 ) return 0; 1094d88778bSdanielk1977 for(i=0, pItem=p->pSrc->a; i<p->pSrc->nSrc; i++, pItem++){ 1104d88778bSdanielk1977 if( pItem->pSelect ){ 11138fba691Sdrh if( selectReadsTable(pItem->pSelect, iDb, iTab) ) return 1; 1124d88778bSdanielk1977 }else{ 1134d88778bSdanielk1977 if( pItem->pTab->iDb==iDb && pItem->pTab->tnum==iTab ) return 1; 1144d88778bSdanielk1977 } 1154d88778bSdanielk1977 } 1164d88778bSdanielk1977 return 0; 1174d88778bSdanielk1977 } 1183d1bfeaaSdanielk1977 1193d1bfeaaSdanielk1977 /* 1201ccde15dSdrh ** This routine is call to handle SQL of the following forms: 121cce7d176Sdrh ** 122cce7d176Sdrh ** insert into TABLE (IDLIST) values(EXPRLIST) 1231ccde15dSdrh ** insert into TABLE (IDLIST) select 124cce7d176Sdrh ** 1251ccde15dSdrh ** The IDLIST following the table name is always optional. If omitted, 1261ccde15dSdrh ** then a list of all columns for the table is substituted. The IDLIST 127967e8b73Sdrh ** appears in the pColumn parameter. pColumn is NULL if IDLIST is omitted. 1281ccde15dSdrh ** 1291ccde15dSdrh ** The pList parameter holds EXPRLIST in the first form of the INSERT 1301ccde15dSdrh ** statement above, and pSelect is NULL. For the second form, pList is 1311ccde15dSdrh ** NULL and pSelect is a pointer to the select statement used to generate 1321ccde15dSdrh ** data for the insert. 133142e30dfSdrh ** 134142e30dfSdrh ** The code generated follows one of three templates. For a simple 135142e30dfSdrh ** select with data coming from a VALUES clause, the code executes 136142e30dfSdrh ** once straight down through. The template looks like this: 137142e30dfSdrh ** 138142e30dfSdrh ** open write cursor to <table> and its indices 139142e30dfSdrh ** puts VALUES clause expressions onto the stack 140142e30dfSdrh ** write the resulting record into <table> 141142e30dfSdrh ** cleanup 142142e30dfSdrh ** 143142e30dfSdrh ** If the statement is of the form 144142e30dfSdrh ** 145142e30dfSdrh ** INSERT INTO <table> SELECT ... 146142e30dfSdrh ** 147142e30dfSdrh ** And the SELECT clause does not read from <table> at any time, then 148142e30dfSdrh ** the generated code follows this template: 149142e30dfSdrh ** 150142e30dfSdrh ** goto B 151142e30dfSdrh ** A: setup for the SELECT 152142e30dfSdrh ** loop over the tables in the SELECT 153142e30dfSdrh ** gosub C 154142e30dfSdrh ** end loop 155142e30dfSdrh ** cleanup after the SELECT 156142e30dfSdrh ** goto D 157142e30dfSdrh ** B: open write cursor to <table> and its indices 158142e30dfSdrh ** goto A 159142e30dfSdrh ** C: insert the select result into <table> 160142e30dfSdrh ** return 161142e30dfSdrh ** D: cleanup 162142e30dfSdrh ** 163142e30dfSdrh ** The third template is used if the insert statement takes its 164142e30dfSdrh ** values from a SELECT but the data is being inserted into a table 165142e30dfSdrh ** that is also read as part of the SELECT. In the third form, 166142e30dfSdrh ** we have to use a intermediate table to store the results of 167142e30dfSdrh ** the select. The template is like this: 168142e30dfSdrh ** 169142e30dfSdrh ** goto B 170142e30dfSdrh ** A: setup for the SELECT 171142e30dfSdrh ** loop over the tables in the SELECT 172142e30dfSdrh ** gosub C 173142e30dfSdrh ** end loop 174142e30dfSdrh ** cleanup after the SELECT 175142e30dfSdrh ** goto D 176142e30dfSdrh ** C: insert the select result into the intermediate table 177142e30dfSdrh ** return 178142e30dfSdrh ** B: open a cursor to an intermediate table 179142e30dfSdrh ** goto A 180142e30dfSdrh ** D: open write cursor to <table> and its indices 181142e30dfSdrh ** loop over the intermediate table 182142e30dfSdrh ** transfer values form intermediate table into <table> 183142e30dfSdrh ** end the loop 184142e30dfSdrh ** cleanup 185cce7d176Sdrh */ 1864adee20fSdanielk1977 void sqlite3Insert( 187cce7d176Sdrh Parse *pParse, /* Parser context */ 188113088ecSdrh SrcList *pTabList, /* Name of table into which we are inserting */ 189cce7d176Sdrh ExprList *pList, /* List of values to be inserted */ 1905974a30fSdrh Select *pSelect, /* A SELECT statement to use as the data source */ 1919cfcf5d4Sdrh IdList *pColumn, /* Column names corresponding to IDLIST. */ 1929cfcf5d4Sdrh int onError /* How to handle constraint errors */ 193cce7d176Sdrh ){ 1945974a30fSdrh Table *pTab; /* The table to insert into */ 195113088ecSdrh char *zTab; /* Name of the table into which we are inserting */ 196e22a334bSdrh const char *zDb; /* Name of the database holding this table */ 1975974a30fSdrh int i, j, idx; /* Loop counters */ 1985974a30fSdrh Vdbe *v; /* Generate code into this virtual machine */ 1995974a30fSdrh Index *pIdx; /* For looping over indices of the table */ 200967e8b73Sdrh int nColumn; /* Number of columns in the data */ 201cfe9a69fSdanielk1977 int base = 0; /* VDBE Cursor number for pTab */ 202cfe9a69fSdanielk1977 int iCont=0,iBreak=0; /* Beginning and end of the loop over srcTab */ 2039bb575fdSdrh sqlite3 *db; /* The main database structure */ 2044a32431cSdrh int keyColumn = -1; /* Column that is the INTEGER PRIMARY KEY */ 2050ca3e24bSdrh int endOfLoop; /* Label for the end of the insertion loop */ 2064d88778bSdanielk1977 int useTempTable = 0; /* Store SELECT results in intermediate table */ 207cfe9a69fSdanielk1977 int srcTab = 0; /* Data comes from this temporary cursor if >=0 */ 208cfe9a69fSdanielk1977 int iSelectLoop = 0; /* Address of code that implements the SELECT */ 209cfe9a69fSdanielk1977 int iCleanup = 0; /* Address of the cleanup code */ 210cfe9a69fSdanielk1977 int iInsertBlock = 0; /* Address of the subroutine used to insert data */ 211cfe9a69fSdanielk1977 int iCntMem = 0; /* Memory cell used for the row counter */ 212798da52cSdrh int newIdx = -1; /* Cursor for the NEW table */ 2132958a4e6Sdrh Db *pDb; /* The database containing table being inserted into */ 2142958a4e6Sdrh int counterMem = 0; /* Memory cell holding AUTOINCREMENT counter */ 215cce7d176Sdrh 216798da52cSdrh #ifndef SQLITE_OMIT_TRIGGER 217798da52cSdrh int isView; /* True if attempting to insert into a view */ 218dca76841Sdrh int triggers_exist = 0; /* True if there are FOR EACH ROW triggers */ 219798da52cSdrh #endif 220c3f9bad2Sdanielk1977 221f3388144Sdrh #ifndef SQLITE_OMIT_AUTOINCREMENT 222f3388144Sdrh int counterRowid; /* Memory cell holding rowid of autoinc counter */ 223f3388144Sdrh #endif 224f3388144Sdrh 22524b03fd0Sdanielk1977 if( pParse->nErr || sqlite3_malloc_failed ) goto insert_cleanup; 226ecdc7530Sdrh db = pParse->db; 227daffd0e5Sdrh 2281ccde15dSdrh /* Locate the table into which we will be inserting new information. 2291ccde15dSdrh */ 230113088ecSdrh assert( pTabList->nSrc==1 ); 231113088ecSdrh zTab = pTabList->a[0].zName; 232daffd0e5Sdrh if( zTab==0 ) goto insert_cleanup; 2334adee20fSdanielk1977 pTab = sqlite3SrcListLookup(pParse, pTabList); 234c3f9bad2Sdanielk1977 if( pTab==0 ){ 235c3f9bad2Sdanielk1977 goto insert_cleanup; 236c3f9bad2Sdanielk1977 } 237e22a334bSdrh assert( pTab->iDb<db->nDb ); 2382958a4e6Sdrh pDb = &db->aDb[pTab->iDb]; 2392958a4e6Sdrh zDb = pDb->zName; 2404adee20fSdanielk1977 if( sqlite3AuthCheck(pParse, SQLITE_INSERT, pTab->zName, 0, zDb) ){ 2411962bda7Sdrh goto insert_cleanup; 2421962bda7Sdrh } 243c3f9bad2Sdanielk1977 244b7f9164eSdrh /* Figure out if we have any triggers and if the table being 245b7f9164eSdrh ** inserted into is a view 246b7f9164eSdrh */ 247b7f9164eSdrh #ifndef SQLITE_OMIT_TRIGGER 248dca76841Sdrh triggers_exist = sqlite3TriggersExist(pParse, pTab, TK_INSERT, 0); 249b7f9164eSdrh isView = pTab->pSelect!=0; 250b7f9164eSdrh #else 251dca76841Sdrh # define triggers_exist 0 252b7f9164eSdrh # define isView 0 253b7f9164eSdrh #endif 254b7f9164eSdrh #ifdef SQLITE_OMIT_VIEW 255b7f9164eSdrh # undef isView 256b7f9164eSdrh # define isView 0 257b7f9164eSdrh #endif 258b7f9164eSdrh 259c3f9bad2Sdanielk1977 /* Ensure that: 260c3f9bad2Sdanielk1977 * (a) the table is not read-only, 261c3f9bad2Sdanielk1977 * (b) that if it is a view then ON INSERT triggers exist 262c3f9bad2Sdanielk1977 */ 263dca76841Sdrh if( sqlite3IsReadOnly(pParse, pTab, triggers_exist) ){ 264c3f9bad2Sdanielk1977 goto insert_cleanup; 265c3f9bad2Sdanielk1977 } 266a76b5dfcSdrh if( pTab==0 ) goto insert_cleanup; 2671ccde15dSdrh 268f573c99bSdrh /* If pTab is really a view, make sure it has been initialized. 269f573c99bSdrh */ 2704adee20fSdanielk1977 if( isView && sqlite3ViewGetColumnNames(pParse, pTab) ){ 271f573c99bSdrh goto insert_cleanup; 272f573c99bSdrh } 273f573c99bSdrh 2747cedc8d4Sdanielk1977 /* Ensure all required collation sequences are available. */ 2757cedc8d4Sdanielk1977 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){ 2767cedc8d4Sdanielk1977 if( sqlite3CheckIndexCollSeq(pParse, pIdx) ){ 2777cedc8d4Sdanielk1977 goto insert_cleanup; 2787cedc8d4Sdanielk1977 } 2797cedc8d4Sdanielk1977 } 2807cedc8d4Sdanielk1977 2811ccde15dSdrh /* Allocate a VDBE 2821ccde15dSdrh */ 2834adee20fSdanielk1977 v = sqlite3GetVdbe(pParse); 2845974a30fSdrh if( v==0 ) goto insert_cleanup; 2854794f735Sdrh if( pParse->nested==0 ) sqlite3VdbeCountChanges(v); 286dca76841Sdrh sqlite3BeginWriteOperation(pParse, pSelect || triggers_exist, pTab->iDb); 2871ccde15dSdrh 288c3f9bad2Sdanielk1977 /* if there are row triggers, allocate a temp table for new.* references. */ 289dca76841Sdrh if( triggers_exist ){ 290c3f9bad2Sdanielk1977 newIdx = pParse->nTab++; 291f29ce559Sdanielk1977 } 292c3f9bad2Sdanielk1977 2932958a4e6Sdrh #ifndef SQLITE_OMIT_AUTOINCREMENT 2942958a4e6Sdrh /* If this is an AUTOINCREMENT table, look up the sequence number in the 295f3388144Sdrh ** sqlite_sequence table and store it in memory cell counterMem. Also 296f3388144Sdrh ** remember the rowid of the sqlite_sequence table entry in memory cell 297f3388144Sdrh ** counterRowid. 2982958a4e6Sdrh */ 2992958a4e6Sdrh if( pTab->autoInc ){ 300f3388144Sdrh int iCur = pParse->nTab; 301f3388144Sdrh int base = sqlite3VdbeCurrentAddr(v); 302f3388144Sdrh counterRowid = pParse->nMem++; 303f3388144Sdrh counterMem = pParse->nMem++; 304f3388144Sdrh sqlite3VdbeAddOp(v, OP_Integer, pTab->iDb, 0); 305f3388144Sdrh sqlite3VdbeAddOp(v, OP_OpenRead, iCur, pDb->pSeqTab->tnum); 306f3388144Sdrh sqlite3VdbeAddOp(v, OP_SetNumColumns, iCur, 2); 307f3388144Sdrh sqlite3VdbeAddOp(v, OP_Rewind, iCur, base+13); 308f3388144Sdrh sqlite3VdbeAddOp(v, OP_Column, iCur, 0); 309f3388144Sdrh sqlite3VdbeOp3(v, OP_String8, 0, 0, pTab->zName, 0); 310f3388144Sdrh sqlite3VdbeAddOp(v, OP_Ne, 28417, base+12); 311f0863fe5Sdrh sqlite3VdbeAddOp(v, OP_Rowid, iCur, 0); 312f3388144Sdrh sqlite3VdbeAddOp(v, OP_MemStore, counterRowid, 1); 313f3388144Sdrh sqlite3VdbeAddOp(v, OP_Column, iCur, 1); 314f3388144Sdrh sqlite3VdbeAddOp(v, OP_MemStore, counterMem, 1); 315f3388144Sdrh sqlite3VdbeAddOp(v, OP_Goto, 0, base+13); 316f3388144Sdrh sqlite3VdbeAddOp(v, OP_Next, iCur, base+4); 317f3388144Sdrh sqlite3VdbeAddOp(v, OP_Close, iCur, 0); 3182958a4e6Sdrh } 3192958a4e6Sdrh #endif /* SQLITE_OMIT_AUTOINCREMENT */ 3202958a4e6Sdrh 3211ccde15dSdrh /* Figure out how many columns of data are supplied. If the data 322142e30dfSdrh ** is coming from a SELECT statement, then this step also generates 323142e30dfSdrh ** all the code to implement the SELECT statement and invoke a subroutine 324142e30dfSdrh ** to process each row of the result. (Template 2.) If the SELECT 325142e30dfSdrh ** statement uses the the table that is being inserted into, then the 326142e30dfSdrh ** subroutine is also coded here. That subroutine stores the SELECT 327142e30dfSdrh ** results in a temporary table. (Template 3.) 3281ccde15dSdrh */ 3295974a30fSdrh if( pSelect ){ 330142e30dfSdrh /* Data is coming from a SELECT. Generate code to implement that SELECT 331142e30dfSdrh */ 332142e30dfSdrh int rc, iInitCode; 3334adee20fSdanielk1977 iInitCode = sqlite3VdbeAddOp(v, OP_Goto, 0, 0); 3344adee20fSdanielk1977 iSelectLoop = sqlite3VdbeCurrentAddr(v); 3354adee20fSdanielk1977 iInsertBlock = sqlite3VdbeMakeLabel(v); 336b3bce662Sdanielk1977 337b3bce662Sdanielk1977 /* Resolve the expressions in the SELECT statement and execute it. */ 338b3bce662Sdanielk1977 rc = sqlite3Select(pParse, pSelect, SRT_Subroutine, iInsertBlock,0,0,0,0); 33924b03fd0Sdanielk1977 if( rc || pParse->nErr || sqlite3_malloc_failed ) goto insert_cleanup; 340b3bce662Sdanielk1977 3414adee20fSdanielk1977 iCleanup = sqlite3VdbeMakeLabel(v); 3424adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Goto, 0, iCleanup); 3435974a30fSdrh assert( pSelect->pEList ); 344967e8b73Sdrh nColumn = pSelect->pEList->nExpr; 345142e30dfSdrh 346142e30dfSdrh /* Set useTempTable to TRUE if the result of the SELECT statement 347142e30dfSdrh ** should be written into a temporary table. Set to FALSE if each 348142e30dfSdrh ** row of the SELECT can be written directly into the result table. 349048c530cSdrh ** 350048c530cSdrh ** A temp table must be used if the table being updated is also one 351048c530cSdrh ** of the tables being read by the SELECT statement. Also use a 352048c530cSdrh ** temp table in the case of row triggers. 353142e30dfSdrh */ 3544d88778bSdanielk1977 if( triggers_exist || selectReadsTable(pSelect, pTab->iDb, pTab->tnum) ){ 355048c530cSdrh useTempTable = 1; 356048c530cSdrh } 357142e30dfSdrh 358142e30dfSdrh if( useTempTable ){ 359142e30dfSdrh /* Generate the subroutine that SELECT calls to process each row of 360142e30dfSdrh ** the result. Store the result in a temporary table 361142e30dfSdrh */ 362142e30dfSdrh srcTab = pParse->nTab++; 3634adee20fSdanielk1977 sqlite3VdbeResolveLabel(v, iInsertBlock); 3644adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_MakeRecord, nColumn, 0); 365a37cdde0Sdanielk1977 sqlite3TableAffinityStr(v, pTab); 366f0863fe5Sdrh sqlite3VdbeAddOp(v, OP_NewRowid, srcTab, 0); 3674adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Pull, 1, 0); 368f0863fe5Sdrh sqlite3VdbeAddOp(v, OP_Insert, srcTab, 0); 3694adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Return, 0, 0); 370142e30dfSdrh 371142e30dfSdrh /* The following code runs first because the GOTO at the very top 372142e30dfSdrh ** of the program jumps to it. Create the temporary table, then jump 373142e30dfSdrh ** back up and execute the SELECT code above. 374142e30dfSdrh */ 3754adee20fSdanielk1977 sqlite3VdbeChangeP2(v, iInitCode, sqlite3VdbeCurrentAddr(v)); 376*9170dd7eSdrh sqlite3VdbeAddOp(v, OP_OpenVirtual, srcTab, 0); 377b6f5452fSdrh sqlite3VdbeAddOp(v, OP_SetNumColumns, srcTab, nColumn); 3784adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Goto, 0, iSelectLoop); 3794adee20fSdanielk1977 sqlite3VdbeResolveLabel(v, iCleanup); 3805974a30fSdrh }else{ 3814adee20fSdanielk1977 sqlite3VdbeChangeP2(v, iInitCode, sqlite3VdbeCurrentAddr(v)); 382142e30dfSdrh } 383142e30dfSdrh }else{ 384142e30dfSdrh /* This is the case if the data for the INSERT is coming from a VALUES 385142e30dfSdrh ** clause 386142e30dfSdrh */ 387b3bce662Sdanielk1977 NameContext sNC; 388b3bce662Sdanielk1977 memset(&sNC, 0, sizeof(sNC)); 389b3bce662Sdanielk1977 sNC.pParse = pParse; 390daffd0e5Sdrh assert( pList!=0 ); 3915974a30fSdrh srcTab = -1; 392142e30dfSdrh useTempTable = 0; 3935974a30fSdrh assert( pList ); 394967e8b73Sdrh nColumn = pList->nExpr; 395e64e7b20Sdrh for(i=0; i<nColumn; i++){ 396b3bce662Sdanielk1977 if( sqlite3ExprResolveNames(&sNC, pList->a[i].pExpr) ){ 397b04a5d87Sdrh goto insert_cleanup; 398b04a5d87Sdrh } 399e64e7b20Sdrh } 4005974a30fSdrh } 4011ccde15dSdrh 4021ccde15dSdrh /* Make sure the number of columns in the source data matches the number 4031ccde15dSdrh ** of columns to be inserted into the table. 4041ccde15dSdrh */ 405967e8b73Sdrh if( pColumn==0 && nColumn!=pTab->nCol ){ 4064adee20fSdanielk1977 sqlite3ErrorMsg(pParse, 407da93d238Sdrh "table %S has %d columns but %d values were supplied", 408da93d238Sdrh pTabList, 0, pTab->nCol, nColumn); 409cce7d176Sdrh goto insert_cleanup; 410cce7d176Sdrh } 411967e8b73Sdrh if( pColumn!=0 && nColumn!=pColumn->nId ){ 4124adee20fSdanielk1977 sqlite3ErrorMsg(pParse, "%d values for %d columns", nColumn, pColumn->nId); 413cce7d176Sdrh goto insert_cleanup; 414cce7d176Sdrh } 4151ccde15dSdrh 4161ccde15dSdrh /* If the INSERT statement included an IDLIST term, then make sure 4171ccde15dSdrh ** all elements of the IDLIST really are columns of the table and 4181ccde15dSdrh ** remember the column indices. 419c8392586Sdrh ** 420c8392586Sdrh ** If the table has an INTEGER PRIMARY KEY column and that column 421c8392586Sdrh ** is named in the IDLIST, then record in the keyColumn variable 422c8392586Sdrh ** the index into IDLIST of the primary key column. keyColumn is 423c8392586Sdrh ** the index of the primary key as it appears in IDLIST, not as 424c8392586Sdrh ** is appears in the original table. (The index of the primary 425c8392586Sdrh ** key in the original table is pTab->iPKey.) 4261ccde15dSdrh */ 427967e8b73Sdrh if( pColumn ){ 428967e8b73Sdrh for(i=0; i<pColumn->nId; i++){ 429967e8b73Sdrh pColumn->a[i].idx = -1; 430cce7d176Sdrh } 431967e8b73Sdrh for(i=0; i<pColumn->nId; i++){ 432cce7d176Sdrh for(j=0; j<pTab->nCol; j++){ 4334adee20fSdanielk1977 if( sqlite3StrICmp(pColumn->a[i].zName, pTab->aCol[j].zName)==0 ){ 434967e8b73Sdrh pColumn->a[i].idx = j; 4354a32431cSdrh if( j==pTab->iPKey ){ 4369aa028daSdrh keyColumn = i; 4374a32431cSdrh } 438cce7d176Sdrh break; 439cce7d176Sdrh } 440cce7d176Sdrh } 441cce7d176Sdrh if( j>=pTab->nCol ){ 4424adee20fSdanielk1977 if( sqlite3IsRowid(pColumn->a[i].zName) ){ 443a0217ba7Sdrh keyColumn = i; 444a0217ba7Sdrh }else{ 4454adee20fSdanielk1977 sqlite3ErrorMsg(pParse, "table %S has no column named %s", 446da93d238Sdrh pTabList, 0, pColumn->a[i].zName); 447cce7d176Sdrh pParse->nErr++; 448cce7d176Sdrh goto insert_cleanup; 449cce7d176Sdrh } 450cce7d176Sdrh } 451cce7d176Sdrh } 452a0217ba7Sdrh } 4531ccde15dSdrh 454aacc543eSdrh /* If there is no IDLIST term but the table has an integer primary 455c8392586Sdrh ** key, the set the keyColumn variable to the primary key column index 456c8392586Sdrh ** in the original table definition. 4574a32431cSdrh */ 4584a32431cSdrh if( pColumn==0 ){ 4594a32431cSdrh keyColumn = pTab->iPKey; 4604a32431cSdrh } 4614a32431cSdrh 462142e30dfSdrh /* Open the temp table for FOR EACH ROW triggers 463142e30dfSdrh */ 464dca76841Sdrh if( triggers_exist ){ 4654adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_OpenPseudo, newIdx, 0); 46684ac9d02Sdanielk1977 sqlite3VdbeAddOp(v, OP_SetNumColumns, newIdx, pTab->nCol); 467f29ce559Sdanielk1977 } 468c3f9bad2Sdanielk1977 469c3f9bad2Sdanielk1977 /* Initialize the count of rows to be inserted 4701ccde15dSdrh */ 471142e30dfSdrh if( db->flags & SQLITE_CountRows ){ 472142e30dfSdrh iCntMem = pParse->nMem++; 4734adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Integer, 0, 0); 4744adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_MemStore, iCntMem, 1); 475c3f9bad2Sdanielk1977 } 476c3f9bad2Sdanielk1977 477c3f9bad2Sdanielk1977 /* Open tables and indices if there are no row triggers */ 478dca76841Sdrh if( !triggers_exist ){ 4795974a30fSdrh base = pParse->nTab; 480290c1948Sdrh sqlite3OpenTableAndIndices(pParse, pTab, base, OP_OpenWrite); 481feeb1394Sdrh } 482feeb1394Sdrh 483142e30dfSdrh /* If the data source is a temporary table, then we have to create 4841ccde15dSdrh ** a loop because there might be multiple rows of data. If the data 485142e30dfSdrh ** source is a subroutine call from the SELECT statement, then we need 486142e30dfSdrh ** to launch the SELECT statement processing. 4871ccde15dSdrh */ 488142e30dfSdrh if( useTempTable ){ 4894adee20fSdanielk1977 iBreak = sqlite3VdbeMakeLabel(v); 4904adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Rewind, srcTab, iBreak); 4914adee20fSdanielk1977 iCont = sqlite3VdbeCurrentAddr(v); 492142e30dfSdrh }else if( pSelect ){ 4934adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Goto, 0, iSelectLoop); 4944adee20fSdanielk1977 sqlite3VdbeResolveLabel(v, iInsertBlock); 495bed8690fSdrh } 4961ccde15dSdrh 4975cf590c1Sdrh /* Run the BEFORE and INSTEAD OF triggers, if there are any 49870ce3f0cSdrh */ 4994adee20fSdanielk1977 endOfLoop = sqlite3VdbeMakeLabel(v); 500dca76841Sdrh if( triggers_exist & TRIGGER_BEFORE ){ 501c3f9bad2Sdanielk1977 50270ce3f0cSdrh /* build the NEW.* reference row. Note that if there is an INTEGER 50370ce3f0cSdrh ** PRIMARY KEY into which a NULL is being inserted, that NULL will be 50470ce3f0cSdrh ** translated into a unique ID for the row. But on a BEFORE trigger, 50570ce3f0cSdrh ** we do not know what the unique ID will be (because the insert has 50670ce3f0cSdrh ** not happened yet) so we substitute a rowid of -1 50770ce3f0cSdrh */ 50870ce3f0cSdrh if( keyColumn<0 ){ 5094adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Integer, -1, 0); 51070ce3f0cSdrh }else if( useTempTable ){ 5114adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Column, srcTab, keyColumn); 51270ce3f0cSdrh }else{ 513d6fe961eSdrh assert( pSelect==0 ); /* Otherwise useTempTable is true */ 5144adee20fSdanielk1977 sqlite3ExprCode(pParse, pList->a[keyColumn].pExpr); 5154adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_NotNull, -1, sqlite3VdbeCurrentAddr(v)+3); 5164adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Pop, 1, 0); 5174adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Integer, -1, 0); 5184adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_MustBeInt, 0, 0); 51970ce3f0cSdrh } 52070ce3f0cSdrh 52170ce3f0cSdrh /* Create the new column data 52270ce3f0cSdrh */ 523c3f9bad2Sdanielk1977 for(i=0; i<pTab->nCol; i++){ 524c3f9bad2Sdanielk1977 if( pColumn==0 ){ 525c3f9bad2Sdanielk1977 j = i; 526c3f9bad2Sdanielk1977 }else{ 527c3f9bad2Sdanielk1977 for(j=0; j<pColumn->nId; j++){ 528c3f9bad2Sdanielk1977 if( pColumn->a[j].idx==i ) break; 529c3f9bad2Sdanielk1977 } 530c3f9bad2Sdanielk1977 } 531c3f9bad2Sdanielk1977 if( pColumn && j>=pColumn->nId ){ 5327977a17fSdanielk1977 sqlite3ExprCode(pParse, pTab->aCol[i].pDflt); 533142e30dfSdrh }else if( useTempTable ){ 5344adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Column, srcTab, j); 535c3f9bad2Sdanielk1977 }else{ 536d6fe961eSdrh assert( pSelect==0 ); /* Otherwise useTempTable is true */ 53725303780Sdrh sqlite3ExprCodeAndCache(pParse, pList->a[j].pExpr); 538c3f9bad2Sdanielk1977 } 539c3f9bad2Sdanielk1977 } 5404adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_MakeRecord, pTab->nCol, 0); 541a37cdde0Sdanielk1977 542a37cdde0Sdanielk1977 /* If this is an INSERT on a view with an INSTEAD OF INSERT trigger, 543a37cdde0Sdanielk1977 ** do not attempt any conversions before assembling the record. 544a37cdde0Sdanielk1977 ** If this is a real table, attempt conversions as required by the 545a37cdde0Sdanielk1977 ** table column affinities. 546a37cdde0Sdanielk1977 */ 547a37cdde0Sdanielk1977 if( !isView ){ 548a37cdde0Sdanielk1977 sqlite3TableAffinityStr(v, pTab); 549a37cdde0Sdanielk1977 } 550f0863fe5Sdrh sqlite3VdbeAddOp(v, OP_Insert, newIdx, 0); 551c3f9bad2Sdanielk1977 5525cf590c1Sdrh /* Fire BEFORE or INSTEAD OF triggers */ 553dca76841Sdrh if( sqlite3CodeRowTrigger(pParse, TK_INSERT, 0, TRIGGER_BEFORE, pTab, 554b2fe7d8cSdrh newIdx, -1, onError, endOfLoop) ){ 555f29ce559Sdanielk1977 goto insert_cleanup; 556f29ce559Sdanielk1977 } 55770ce3f0cSdrh } 558c3f9bad2Sdanielk1977 55970ce3f0cSdrh /* If any triggers exists, the opening of tables and indices is deferred 56070ce3f0cSdrh ** until now. 56170ce3f0cSdrh */ 562dca76841Sdrh if( triggers_exist && !isView ){ 563c3f9bad2Sdanielk1977 base = pParse->nTab; 564290c1948Sdrh sqlite3OpenTableAndIndices(pParse, pTab, base, OP_OpenWrite); 565c3f9bad2Sdanielk1977 } 566c3f9bad2Sdanielk1977 5674a32431cSdrh /* Push the record number for the new entry onto the stack. The 568f0863fe5Sdrh ** record number is a randomly generate integer created by NewRowid 5694a32431cSdrh ** except when the table has an INTEGER PRIMARY KEY column, in which 570b419a926Sdrh ** case the record number is the same as that column. 5711ccde15dSdrh */ 5725cf590c1Sdrh if( !isView ){ 5734a32431cSdrh if( keyColumn>=0 ){ 574142e30dfSdrh if( useTempTable ){ 5754adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Column, srcTab, keyColumn); 576142e30dfSdrh }else if( pSelect ){ 5774adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Dup, nColumn - keyColumn - 1, 1); 5784a32431cSdrh }else{ 5794adee20fSdanielk1977 sqlite3ExprCode(pParse, pList->a[keyColumn].pExpr); 58027a32783Sdrh } 581f0863fe5Sdrh /* If the PRIMARY KEY expression is NULL, then use OP_NewRowid 582e1e68f49Sdrh ** to generate a unique primary key value. 583e1e68f49Sdrh */ 5844adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_NotNull, -1, sqlite3VdbeCurrentAddr(v)+3); 5854adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Pop, 1, 0); 586f0863fe5Sdrh sqlite3VdbeAddOp(v, OP_NewRowid, base, counterMem); 5874adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_MustBeInt, 0, 0); 5884a32431cSdrh }else{ 589f0863fe5Sdrh sqlite3VdbeAddOp(v, OP_NewRowid, base, counterMem); 5904a32431cSdrh } 5912958a4e6Sdrh #ifndef SQLITE_OMIT_AUTOINCREMENT 5922958a4e6Sdrh if( pTab->autoInc ){ 5932958a4e6Sdrh sqlite3VdbeAddOp(v, OP_MemMax, counterMem, 0); 5942958a4e6Sdrh } 5952958a4e6Sdrh #endif /* SQLITE_OMIT_AUTOINCREMENT */ 5964a32431cSdrh 597aacc543eSdrh /* Push onto the stack, data for all columns of the new entry, beginning 5984a32431cSdrh ** with the first column. 5994a32431cSdrh */ 600cce7d176Sdrh for(i=0; i<pTab->nCol; i++){ 6014a32431cSdrh if( i==pTab->iPKey ){ 6024a32431cSdrh /* The value of the INTEGER PRIMARY KEY column is always a NULL. 603aacc543eSdrh ** Whenever this column is read, the record number will be substituted 604aacc543eSdrh ** in its place. So will fill this column with a NULL to avoid 605aacc543eSdrh ** taking up data space with information that will never be used. */ 606f0863fe5Sdrh sqlite3VdbeAddOp(v, OP_Null, 0, 0); 6074a32431cSdrh continue; 6084a32431cSdrh } 609967e8b73Sdrh if( pColumn==0 ){ 610cce7d176Sdrh j = i; 611cce7d176Sdrh }else{ 612967e8b73Sdrh for(j=0; j<pColumn->nId; j++){ 613967e8b73Sdrh if( pColumn->a[j].idx==i ) break; 614cce7d176Sdrh } 615cce7d176Sdrh } 616967e8b73Sdrh if( pColumn && j>=pColumn->nId ){ 6177977a17fSdanielk1977 sqlite3ExprCode(pParse, pTab->aCol[i].pDflt); 618142e30dfSdrh }else if( useTempTable ){ 6194adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Column, srcTab, j); 620142e30dfSdrh }else if( pSelect ){ 6214adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Dup, i+nColumn-j, 1); 622cce7d176Sdrh }else{ 6234adee20fSdanielk1977 sqlite3ExprCode(pParse, pList->a[j].pExpr); 624cce7d176Sdrh } 625cce7d176Sdrh } 6261ccde15dSdrh 6270ca3e24bSdrh /* Generate code to check constraints and generate index keys and 6280ca3e24bSdrh ** do the insertion. 6294a32431cSdrh */ 6304adee20fSdanielk1977 sqlite3GenerateConstraintChecks(pParse, pTab, base, 0, keyColumn>=0, 631a0217ba7Sdrh 0, onError, endOfLoop); 6324adee20fSdanielk1977 sqlite3CompleteInsertion(pParse, pTab, base, 0,0,0, 633dca76841Sdrh (triggers_exist & TRIGGER_AFTER)!=0 ? newIdx : -1); 6345cf590c1Sdrh } 6351bee3d7bSdrh 636feeb1394Sdrh /* Update the count of rows that are inserted 6371bee3d7bSdrh */ 638142e30dfSdrh if( (db->flags & SQLITE_CountRows)!=0 ){ 6394adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_MemIncr, iCntMem, 0); 6401bee3d7bSdrh } 641c3f9bad2Sdanielk1977 642dca76841Sdrh if( triggers_exist ){ 643c3f9bad2Sdanielk1977 /* Close all tables opened */ 6445cf590c1Sdrh if( !isView ){ 6454adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Close, base, 0); 646c3f9bad2Sdanielk1977 for(idx=1, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, idx++){ 6474adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Close, idx+base, 0); 648c3f9bad2Sdanielk1977 } 649c3f9bad2Sdanielk1977 } 650c3f9bad2Sdanielk1977 651c3f9bad2Sdanielk1977 /* Code AFTER triggers */ 652dca76841Sdrh if( sqlite3CodeRowTrigger(pParse, TK_INSERT, 0, TRIGGER_AFTER, pTab, 653dca76841Sdrh newIdx, -1, onError, endOfLoop) ){ 654f29ce559Sdanielk1977 goto insert_cleanup; 655f29ce559Sdanielk1977 } 656c3f9bad2Sdanielk1977 } 6571bee3d7bSdrh 6581ccde15dSdrh /* The bottom of the loop, if the data source is a SELECT statement 6591ccde15dSdrh */ 6604adee20fSdanielk1977 sqlite3VdbeResolveLabel(v, endOfLoop); 661142e30dfSdrh if( useTempTable ){ 6624adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Next, srcTab, iCont); 6634adee20fSdanielk1977 sqlite3VdbeResolveLabel(v, iBreak); 6644adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Close, srcTab, 0); 665142e30dfSdrh }else if( pSelect ){ 6664adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Pop, nColumn, 0); 6674adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Return, 0, 0); 6684adee20fSdanielk1977 sqlite3VdbeResolveLabel(v, iCleanup); 6696b56344dSdrh } 670c3f9bad2Sdanielk1977 671dca76841Sdrh if( !triggers_exist ){ 672c3f9bad2Sdanielk1977 /* Close all tables opened */ 6734adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Close, base, 0); 6746b56344dSdrh for(idx=1, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, idx++){ 6754adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Close, idx+base, 0); 676cce7d176Sdrh } 677c3f9bad2Sdanielk1977 } 678c3f9bad2Sdanielk1977 6792958a4e6Sdrh #ifndef SQLITE_OMIT_AUTOINCREMENT 680f3388144Sdrh /* Update the sqlite_sequence table by storing the content of the 681f3388144Sdrh ** counter value in memory counterMem back into the sqlite_sequence 682f3388144Sdrh ** table. 6832958a4e6Sdrh */ 6842958a4e6Sdrh if( pTab->autoInc ){ 685f3388144Sdrh int iCur = pParse->nTab; 686f3388144Sdrh int base = sqlite3VdbeCurrentAddr(v); 687f3388144Sdrh sqlite3VdbeAddOp(v, OP_Integer, pTab->iDb, 0); 688f3388144Sdrh sqlite3VdbeAddOp(v, OP_OpenWrite, iCur, pDb->pSeqTab->tnum); 689f3388144Sdrh sqlite3VdbeAddOp(v, OP_SetNumColumns, iCur, 2); 690f3388144Sdrh sqlite3VdbeAddOp(v, OP_MemLoad, counterRowid, 0); 691f3388144Sdrh sqlite3VdbeAddOp(v, OP_NotNull, -1, base+7); 692f3388144Sdrh sqlite3VdbeAddOp(v, OP_Pop, 1, 0); 693f0863fe5Sdrh sqlite3VdbeAddOp(v, OP_NewRowid, iCur, 0); 694f3388144Sdrh sqlite3VdbeOp3(v, OP_String8, 0, 0, pTab->zName, 0); 695f3388144Sdrh sqlite3VdbeAddOp(v, OP_MemLoad, counterMem, 0); 696f3388144Sdrh sqlite3VdbeAddOp(v, OP_MakeRecord, 2, 0); 697f0863fe5Sdrh sqlite3VdbeAddOp(v, OP_Insert, iCur, 0); 698f3388144Sdrh sqlite3VdbeAddOp(v, OP_Close, iCur, 0); 6992958a4e6Sdrh } 7002958a4e6Sdrh #endif 7012958a4e6Sdrh 7021bee3d7bSdrh /* 703e7de6f25Sdanielk1977 ** Return the number of rows inserted. If this routine is 704e7de6f25Sdanielk1977 ** generating code because of a call to sqlite3NestedParse(), do not 705e7de6f25Sdanielk1977 ** invoke the callback function. 7061bee3d7bSdrh */ 707cc6bd383Sdanielk1977 if( db->flags & SQLITE_CountRows && pParse->nested==0 && !pParse->trigStack ){ 7084adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_MemLoad, iCntMem, 0); 7094adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Callback, 1, 0); 71022322fd4Sdanielk1977 sqlite3VdbeSetNumCols(v, 1); 7113cf86063Sdanielk1977 sqlite3VdbeSetColName(v, 0, "rows inserted", P3_STATIC); 7121bee3d7bSdrh } 713cce7d176Sdrh 714cce7d176Sdrh insert_cleanup: 7154adee20fSdanielk1977 sqlite3SrcListDelete(pTabList); 716d5d56523Sdanielk1977 sqlite3ExprListDelete(pList); 717d5d56523Sdanielk1977 sqlite3SelectDelete(pSelect); 7184adee20fSdanielk1977 sqlite3IdListDelete(pColumn); 719cce7d176Sdrh } 7209cfcf5d4Sdrh 7219cfcf5d4Sdrh /* 7229cfcf5d4Sdrh ** Generate code to do a constraint check prior to an INSERT or an UPDATE. 7239cfcf5d4Sdrh ** 7249cfcf5d4Sdrh ** When this routine is called, the stack contains (from bottom to top) 7250ca3e24bSdrh ** the following values: 7260ca3e24bSdrh ** 727f0863fe5Sdrh ** 1. The rowid of the row to be updated before the update. This 728b419a926Sdrh ** value is omitted unless we are doing an UPDATE that involves a 729b419a926Sdrh ** change to the record number. 7300ca3e24bSdrh ** 731f0863fe5Sdrh ** 2. The rowid of the row after the update. 7320ca3e24bSdrh ** 7330ca3e24bSdrh ** 3. The data in the first column of the entry after the update. 7340ca3e24bSdrh ** 7350ca3e24bSdrh ** i. Data from middle columns... 7360ca3e24bSdrh ** 7370ca3e24bSdrh ** N. The data in the last column of the entry after the update. 7380ca3e24bSdrh ** 739f0863fe5Sdrh ** The old rowid shown as entry (1) above is omitted unless both isUpdate 740f0863fe5Sdrh ** and rowidChng are 1. isUpdate is true for UPDATEs and false for 741f0863fe5Sdrh ** INSERTs and rowidChng is true if the record number is being changed. 7420ca3e24bSdrh ** 7430ca3e24bSdrh ** The code generated by this routine pushes additional entries onto 7440ca3e24bSdrh ** the stack which are the keys for new index entries for the new record. 7450ca3e24bSdrh ** The order of index keys is the same as the order of the indices on 7460ca3e24bSdrh ** the pTable->pIndex list. A key is only created for index i if 7470ca3e24bSdrh ** aIdxUsed!=0 and aIdxUsed[i]!=0. 7489cfcf5d4Sdrh ** 7499cfcf5d4Sdrh ** This routine also generates code to check constraints. NOT NULL, 7509cfcf5d4Sdrh ** CHECK, and UNIQUE constraints are all checked. If a constraint fails, 7511c92853dSdrh ** then the appropriate action is performed. There are five possible 7521c92853dSdrh ** actions: ROLLBACK, ABORT, FAIL, REPLACE, and IGNORE. 7539cfcf5d4Sdrh ** 7549cfcf5d4Sdrh ** Constraint type Action What Happens 7559cfcf5d4Sdrh ** --------------- ---------- ---------------------------------------- 7561c92853dSdrh ** any ROLLBACK The current transaction is rolled back and 75724b03fd0Sdanielk1977 ** sqlite3_exec() returns immediately with a 7589cfcf5d4Sdrh ** return code of SQLITE_CONSTRAINT. 7599cfcf5d4Sdrh ** 7601c92853dSdrh ** any ABORT Back out changes from the current command 7611c92853dSdrh ** only (do not do a complete rollback) then 76224b03fd0Sdanielk1977 ** cause sqlite3_exec() to return immediately 7631c92853dSdrh ** with SQLITE_CONSTRAINT. 7641c92853dSdrh ** 7651c92853dSdrh ** any FAIL Sqlite_exec() returns immediately with a 7661c92853dSdrh ** return code of SQLITE_CONSTRAINT. The 7671c92853dSdrh ** transaction is not rolled back and any 7681c92853dSdrh ** prior changes are retained. 7691c92853dSdrh ** 7709cfcf5d4Sdrh ** any IGNORE The record number and data is popped from 7719cfcf5d4Sdrh ** the stack and there is an immediate jump 7729cfcf5d4Sdrh ** to label ignoreDest. 7739cfcf5d4Sdrh ** 7749cfcf5d4Sdrh ** NOT NULL REPLACE The NULL value is replace by the default 7759cfcf5d4Sdrh ** value for that column. If the default value 7769cfcf5d4Sdrh ** is NULL, the action is the same as ABORT. 7779cfcf5d4Sdrh ** 7789cfcf5d4Sdrh ** UNIQUE REPLACE The other row that conflicts with the row 7799cfcf5d4Sdrh ** being inserted is removed. 7809cfcf5d4Sdrh ** 7819cfcf5d4Sdrh ** CHECK REPLACE Illegal. The results in an exception. 7829cfcf5d4Sdrh ** 7831c92853dSdrh ** Which action to take is determined by the overrideError parameter. 7841c92853dSdrh ** Or if overrideError==OE_Default, then the pParse->onError parameter 7851c92853dSdrh ** is used. Or if pParse->onError==OE_Default then the onError value 7861c92853dSdrh ** for the constraint is used. 7879cfcf5d4Sdrh ** 788aaab5725Sdrh ** The calling routine must open a read/write cursor for pTab with 7899cfcf5d4Sdrh ** cursor number "base". All indices of pTab must also have open 7909cfcf5d4Sdrh ** read/write cursors with cursor number base+i for the i-th cursor. 7919cfcf5d4Sdrh ** Except, if there is no possibility of a REPLACE action then 7929cfcf5d4Sdrh ** cursors do not need to be open for indices where aIdxUsed[i]==0. 7939cfcf5d4Sdrh ** 7949cfcf5d4Sdrh ** If the isUpdate flag is true, it means that the "base" cursor is 7959cfcf5d4Sdrh ** initially pointing to an entry that is being updated. The isUpdate 7969cfcf5d4Sdrh ** flag causes extra code to be generated so that the "base" cursor 7979cfcf5d4Sdrh ** is still pointing at the same entry after the routine returns. 7989cfcf5d4Sdrh ** Without the isUpdate flag, the "base" cursor might be moved. 7999cfcf5d4Sdrh */ 8004adee20fSdanielk1977 void sqlite3GenerateConstraintChecks( 8019cfcf5d4Sdrh Parse *pParse, /* The parser context */ 8029cfcf5d4Sdrh Table *pTab, /* the table into which we are inserting */ 8039cfcf5d4Sdrh int base, /* Index of a read/write cursor pointing at pTab */ 8049cfcf5d4Sdrh char *aIdxUsed, /* Which indices are used. NULL means all are used */ 805f0863fe5Sdrh int rowidChng, /* True if the record number will change */ 806b419a926Sdrh int isUpdate, /* True for UPDATE, False for INSERT */ 8079cfcf5d4Sdrh int overrideError, /* Override onError to this if not OE_Default */ 808b419a926Sdrh int ignoreDest /* Jump to this label on an OE_Ignore resolution */ 8099cfcf5d4Sdrh ){ 8109cfcf5d4Sdrh int i; 8119cfcf5d4Sdrh Vdbe *v; 8129cfcf5d4Sdrh int nCol; 8139cfcf5d4Sdrh int onError; 8149cfcf5d4Sdrh int addr; 8159cfcf5d4Sdrh int extra; 8160ca3e24bSdrh int iCur; 8170ca3e24bSdrh Index *pIdx; 8180ca3e24bSdrh int seenReplace = 0; 819cfe9a69fSdanielk1977 int jumpInst1=0, jumpInst2; 8200ca3e24bSdrh int contAddr; 821f0863fe5Sdrh int hasTwoRowids = (isUpdate && rowidChng); 8229cfcf5d4Sdrh 8234adee20fSdanielk1977 v = sqlite3GetVdbe(pParse); 8249cfcf5d4Sdrh assert( v!=0 ); 825417be79cSdrh assert( pTab->pSelect==0 ); /* This table is not a VIEW */ 8269cfcf5d4Sdrh nCol = pTab->nCol; 8279cfcf5d4Sdrh 8289cfcf5d4Sdrh /* Test all NOT NULL constraints. 8299cfcf5d4Sdrh */ 8309cfcf5d4Sdrh for(i=0; i<nCol; i++){ 8310ca3e24bSdrh if( i==pTab->iPKey ){ 8320ca3e24bSdrh continue; 8330ca3e24bSdrh } 8349cfcf5d4Sdrh onError = pTab->aCol[i].notNull; 8350ca3e24bSdrh if( onError==OE_None ) continue; 8369cfcf5d4Sdrh if( overrideError!=OE_Default ){ 8379cfcf5d4Sdrh onError = overrideError; 838a996e477Sdrh }else if( onError==OE_Default ){ 839a996e477Sdrh onError = OE_Abort; 8409cfcf5d4Sdrh } 8417977a17fSdanielk1977 if( onError==OE_Replace && pTab->aCol[i].pDflt==0 ){ 8429cfcf5d4Sdrh onError = OE_Abort; 8439cfcf5d4Sdrh } 8444adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Dup, nCol-1-i, 1); 8454adee20fSdanielk1977 addr = sqlite3VdbeAddOp(v, OP_NotNull, 1, 0); 846b84f96f8Sdanielk1977 assert( onError==OE_Rollback || onError==OE_Abort || onError==OE_Fail 847b84f96f8Sdanielk1977 || onError==OE_Ignore || onError==OE_Replace ); 8489cfcf5d4Sdrh switch( onError ){ 8491c92853dSdrh case OE_Rollback: 8501c92853dSdrh case OE_Abort: 8511c92853dSdrh case OE_Fail: { 852483750baSdrh char *zMsg = 0; 8534adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Halt, SQLITE_CONSTRAINT, onError); 8544adee20fSdanielk1977 sqlite3SetString(&zMsg, pTab->zName, ".", pTab->aCol[i].zName, 85541743984Sdrh " may not be NULL", (char*)0); 8564adee20fSdanielk1977 sqlite3VdbeChangeP3(v, -1, zMsg, P3_DYNAMIC); 8579cfcf5d4Sdrh break; 8589cfcf5d4Sdrh } 8599cfcf5d4Sdrh case OE_Ignore: { 860f0863fe5Sdrh sqlite3VdbeAddOp(v, OP_Pop, nCol+1+hasTwoRowids, 0); 8614adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Goto, 0, ignoreDest); 8629cfcf5d4Sdrh break; 8639cfcf5d4Sdrh } 8649cfcf5d4Sdrh case OE_Replace: { 8657977a17fSdanielk1977 sqlite3ExprCode(pParse, pTab->aCol[i].pDflt); 8664adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Push, nCol-i, 0); 8679cfcf5d4Sdrh break; 8689cfcf5d4Sdrh } 8699cfcf5d4Sdrh } 8704adee20fSdanielk1977 sqlite3VdbeChangeP2(v, addr, sqlite3VdbeCurrentAddr(v)); 8719cfcf5d4Sdrh } 8729cfcf5d4Sdrh 8739cfcf5d4Sdrh /* Test all CHECK constraints 8749cfcf5d4Sdrh */ 8750bd1f4eaSdrh /**** TBD ****/ 8769cfcf5d4Sdrh 8770bd1f4eaSdrh /* If we have an INTEGER PRIMARY KEY, make sure the primary key 8780bd1f4eaSdrh ** of the new record does not previously exist. Except, if this 8790bd1f4eaSdrh ** is an UPDATE and the primary key is not changing, that is OK. 8809cfcf5d4Sdrh */ 881f0863fe5Sdrh if( rowidChng ){ 8820ca3e24bSdrh onError = pTab->keyConf; 8830ca3e24bSdrh if( overrideError!=OE_Default ){ 8840ca3e24bSdrh onError = overrideError; 885a996e477Sdrh }else if( onError==OE_Default ){ 886a996e477Sdrh onError = OE_Abort; 8870ca3e24bSdrh } 888a0217ba7Sdrh 88979b0c956Sdrh if( isUpdate ){ 8904adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Dup, nCol+1, 1); 8914adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Dup, nCol+1, 1); 8924adee20fSdanielk1977 jumpInst1 = sqlite3VdbeAddOp(v, OP_Eq, 0, 0); 89379b0c956Sdrh } 8944adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Dup, nCol, 1); 8954adee20fSdanielk1977 jumpInst2 = sqlite3VdbeAddOp(v, OP_NotExists, base, 0); 8960ca3e24bSdrh switch( onError ){ 897a0217ba7Sdrh default: { 898a0217ba7Sdrh onError = OE_Abort; 899a0217ba7Sdrh /* Fall thru into the next case */ 900a0217ba7Sdrh } 9011c92853dSdrh case OE_Rollback: 9021c92853dSdrh case OE_Abort: 9031c92853dSdrh case OE_Fail: { 9044adee20fSdanielk1977 sqlite3VdbeOp3(v, OP_Halt, SQLITE_CONSTRAINT, onError, 905701a0aebSdrh "PRIMARY KEY must be unique", P3_STATIC); 9060ca3e24bSdrh break; 9070ca3e24bSdrh } 9085383ae5cSdrh case OE_Replace: { 9094adee20fSdanielk1977 sqlite3GenerateRowIndexDelete(pParse->db, v, pTab, base, 0); 9105383ae5cSdrh if( isUpdate ){ 911f0863fe5Sdrh sqlite3VdbeAddOp(v, OP_Dup, nCol+hasTwoRowids, 1); 9127cf6e4deSdrh sqlite3VdbeAddOp(v, OP_MoveGe, base, 0); 9135383ae5cSdrh } 9145383ae5cSdrh seenReplace = 1; 9155383ae5cSdrh break; 9165383ae5cSdrh } 9170ca3e24bSdrh case OE_Ignore: { 9185383ae5cSdrh assert( seenReplace==0 ); 919f0863fe5Sdrh sqlite3VdbeAddOp(v, OP_Pop, nCol+1+hasTwoRowids, 0); 9204adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Goto, 0, ignoreDest); 9210ca3e24bSdrh break; 9220ca3e24bSdrh } 9230ca3e24bSdrh } 9244adee20fSdanielk1977 contAddr = sqlite3VdbeCurrentAddr(v); 9254adee20fSdanielk1977 sqlite3VdbeChangeP2(v, jumpInst2, contAddr); 926f5905aa7Sdrh if( isUpdate ){ 9274adee20fSdanielk1977 sqlite3VdbeChangeP2(v, jumpInst1, contAddr); 9284adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Dup, nCol+1, 1); 9297cf6e4deSdrh sqlite3VdbeAddOp(v, OP_MoveGe, base, 0); 9300ca3e24bSdrh } 9310ca3e24bSdrh } 9320bd1f4eaSdrh 9330bd1f4eaSdrh /* Test all UNIQUE constraints by creating entries for each UNIQUE 9340bd1f4eaSdrh ** index and making sure that duplicate entries do not already exist. 9350bd1f4eaSdrh ** Add the new records to the indices as we go. 9360bd1f4eaSdrh */ 937b2fe7d8cSdrh extra = -1; 938b2fe7d8cSdrh for(iCur=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, iCur++){ 939b2fe7d8cSdrh if( aIdxUsed && aIdxUsed[iCur]==0 ) continue; /* Skip unused indices */ 9409cfcf5d4Sdrh extra++; 941b2fe7d8cSdrh 942b2fe7d8cSdrh /* Create a key for accessing the index entry */ 9434adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Dup, nCol+extra, 1); 9449cfcf5d4Sdrh for(i=0; i<pIdx->nColumn; i++){ 9459cfcf5d4Sdrh int idx = pIdx->aiColumn[i]; 9469cfcf5d4Sdrh if( idx==pTab->iPKey ){ 9474adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Dup, i+extra+nCol+1, 1); 9489cfcf5d4Sdrh }else{ 9494adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Dup, i+extra+nCol-idx, 1); 9509cfcf5d4Sdrh } 9519cfcf5d4Sdrh } 9527f057c91Sdrh jumpInst1 = sqlite3VdbeAddOp(v, OP_MakeIdxRec, pIdx->nColumn, 0); 953a37cdde0Sdanielk1977 sqlite3IndexAffinityStr(v, pIdx); 954b2fe7d8cSdrh 955b2fe7d8cSdrh /* Find out what action to take in case there is an indexing conflict */ 9569cfcf5d4Sdrh onError = pIdx->onError; 957b2fe7d8cSdrh if( onError==OE_None ) continue; /* pIdx is not a UNIQUE index */ 9589cfcf5d4Sdrh if( overrideError!=OE_Default ){ 9599cfcf5d4Sdrh onError = overrideError; 960a996e477Sdrh }else if( onError==OE_Default ){ 961a996e477Sdrh onError = OE_Abort; 9629cfcf5d4Sdrh } 9635383ae5cSdrh if( seenReplace ){ 9645383ae5cSdrh if( onError==OE_Ignore ) onError = OE_Replace; 9655383ae5cSdrh else if( onError==OE_Fail ) onError = OE_Abort; 9665383ae5cSdrh } 9675383ae5cSdrh 968b2fe7d8cSdrh 969b2fe7d8cSdrh /* Check to see if the new index entry will be unique */ 970f0863fe5Sdrh sqlite3VdbeAddOp(v, OP_Dup, extra+nCol+1+hasTwoRowids, 1); 9714adee20fSdanielk1977 jumpInst2 = sqlite3VdbeAddOp(v, OP_IsUnique, base+iCur+1, 0); 972b2fe7d8cSdrh 973b2fe7d8cSdrh /* Generate code that executes if the new index entry is not unique */ 974b84f96f8Sdanielk1977 assert( onError==OE_Rollback || onError==OE_Abort || onError==OE_Fail 975b84f96f8Sdanielk1977 || onError==OE_Ignore || onError==OE_Replace ); 9769cfcf5d4Sdrh switch( onError ){ 9771c92853dSdrh case OE_Rollback: 9781c92853dSdrh case OE_Abort: 9791c92853dSdrh case OE_Fail: { 98037ed48edSdrh int j, n1, n2; 98137ed48edSdrh char zErrMsg[200]; 98237ed48edSdrh strcpy(zErrMsg, pIdx->nColumn>1 ? "columns " : "column "); 98337ed48edSdrh n1 = strlen(zErrMsg); 98437ed48edSdrh for(j=0; j<pIdx->nColumn && n1<sizeof(zErrMsg)-30; j++){ 98537ed48edSdrh char *zCol = pTab->aCol[pIdx->aiColumn[j]].zName; 98637ed48edSdrh n2 = strlen(zCol); 98737ed48edSdrh if( j>0 ){ 98837ed48edSdrh strcpy(&zErrMsg[n1], ", "); 98937ed48edSdrh n1 += 2; 99037ed48edSdrh } 99137ed48edSdrh if( n1+n2>sizeof(zErrMsg)-30 ){ 99237ed48edSdrh strcpy(&zErrMsg[n1], "..."); 99337ed48edSdrh n1 += 3; 99437ed48edSdrh break; 99537ed48edSdrh }else{ 99637ed48edSdrh strcpy(&zErrMsg[n1], zCol); 99737ed48edSdrh n1 += n2; 99837ed48edSdrh } 99937ed48edSdrh } 100037ed48edSdrh strcpy(&zErrMsg[n1], 100137ed48edSdrh pIdx->nColumn>1 ? " are not unique" : " is not unique"); 10024adee20fSdanielk1977 sqlite3VdbeOp3(v, OP_Halt, SQLITE_CONSTRAINT, onError, zErrMsg, 0); 10039cfcf5d4Sdrh break; 10049cfcf5d4Sdrh } 10059cfcf5d4Sdrh case OE_Ignore: { 10060ca3e24bSdrh assert( seenReplace==0 ); 1007f0863fe5Sdrh sqlite3VdbeAddOp(v, OP_Pop, nCol+extra+3+hasTwoRowids, 0); 10084adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Goto, 0, ignoreDest); 10099cfcf5d4Sdrh break; 10109cfcf5d4Sdrh } 10119cfcf5d4Sdrh case OE_Replace: { 10124adee20fSdanielk1977 sqlite3GenerateRowDelete(pParse->db, v, pTab, base, 0); 10139cfcf5d4Sdrh if( isUpdate ){ 1014f0863fe5Sdrh sqlite3VdbeAddOp(v, OP_Dup, nCol+extra+1+hasTwoRowids, 1); 10157cf6e4deSdrh sqlite3VdbeAddOp(v, OP_MoveGe, base, 0); 10169cfcf5d4Sdrh } 10170ca3e24bSdrh seenReplace = 1; 10189cfcf5d4Sdrh break; 10199cfcf5d4Sdrh } 10209cfcf5d4Sdrh } 10214adee20fSdanielk1977 contAddr = sqlite3VdbeCurrentAddr(v); 10220bd1f4eaSdrh #if NULL_DISTINCT_FOR_UNIQUE 10237f057c91Sdrh sqlite3VdbeChangeP2(v, jumpInst1, contAddr); 10240bd1f4eaSdrh #endif 10254adee20fSdanielk1977 sqlite3VdbeChangeP2(v, jumpInst2, contAddr); 10269cfcf5d4Sdrh } 10279cfcf5d4Sdrh } 10280ca3e24bSdrh 10290ca3e24bSdrh /* 10300ca3e24bSdrh ** This routine generates code to finish the INSERT or UPDATE operation 10314adee20fSdanielk1977 ** that was started by a prior call to sqlite3GenerateConstraintChecks. 10320ca3e24bSdrh ** The stack must contain keys for all active indices followed by data 1033f0863fe5Sdrh ** and the rowid for the new entry. This routine creates the new 10340ca3e24bSdrh ** entries in all indices and in the main table. 10350ca3e24bSdrh ** 1036b419a926Sdrh ** The arguments to this routine should be the same as the first six 10374adee20fSdanielk1977 ** arguments to sqlite3GenerateConstraintChecks. 10380ca3e24bSdrh */ 10394adee20fSdanielk1977 void sqlite3CompleteInsertion( 10400ca3e24bSdrh Parse *pParse, /* The parser context */ 10410ca3e24bSdrh Table *pTab, /* the table into which we are inserting */ 10420ca3e24bSdrh int base, /* Index of a read/write cursor pointing at pTab */ 10430ca3e24bSdrh char *aIdxUsed, /* Which indices are used. NULL means all are used */ 1044f0863fe5Sdrh int rowidChng, /* True if the record number will change */ 104570ce3f0cSdrh int isUpdate, /* True for UPDATE, False for INSERT */ 104670ce3f0cSdrh int newIdx /* Index of NEW table for triggers. -1 if none */ 10470ca3e24bSdrh ){ 10480ca3e24bSdrh int i; 10490ca3e24bSdrh Vdbe *v; 10500ca3e24bSdrh int nIdx; 10510ca3e24bSdrh Index *pIdx; 1052b28af71aSdanielk1977 int pik_flags; 10530ca3e24bSdrh 10544adee20fSdanielk1977 v = sqlite3GetVdbe(pParse); 10550ca3e24bSdrh assert( v!=0 ); 1056417be79cSdrh assert( pTab->pSelect==0 ); /* This table is not a VIEW */ 10570ca3e24bSdrh for(nIdx=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, nIdx++){} 10580ca3e24bSdrh for(i=nIdx-1; i>=0; i--){ 10590ca3e24bSdrh if( aIdxUsed && aIdxUsed[i]==0 ) continue; 1060f0863fe5Sdrh sqlite3VdbeAddOp(v, OP_IdxInsert, base+i+1, 0); 10610ca3e24bSdrh } 10624adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_MakeRecord, pTab->nCol, 0); 1063a37cdde0Sdanielk1977 sqlite3TableAffinityStr(v, pTab); 1064b84f96f8Sdanielk1977 #ifndef SQLITE_OMIT_TRIGGER 106570ce3f0cSdrh if( newIdx>=0 ){ 10664adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Dup, 1, 0); 10674adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Dup, 1, 0); 1068f0863fe5Sdrh sqlite3VdbeAddOp(v, OP_Insert, newIdx, 0); 106970ce3f0cSdrh } 1070b84f96f8Sdanielk1977 #endif 10714794f735Sdrh if( pParse->nested ){ 10724794f735Sdrh pik_flags = 0; 10734794f735Sdrh }else{ 1074b28af71aSdanielk1977 pik_flags = (OPFLAG_NCHANGE|(isUpdate?0:OPFLAG_LASTROWID)); 10754794f735Sdrh } 1076f0863fe5Sdrh sqlite3VdbeAddOp(v, OP_Insert, base, pik_flags); 1077b28af71aSdanielk1977 1078f0863fe5Sdrh if( isUpdate && rowidChng ){ 10794adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Pop, 1, 0); 10800ca3e24bSdrh } 10810ca3e24bSdrh } 1082cd44690aSdrh 1083cd44690aSdrh /* 1084290c1948Sdrh ** Generate code that will open cursors for a table and for all 1085cd44690aSdrh ** indices of that table. The "base" parameter is the cursor number used 1086cd44690aSdrh ** for the table. Indices are opened on subsequent cursors. 1087cd44690aSdrh */ 1088290c1948Sdrh void sqlite3OpenTableAndIndices( 1089290c1948Sdrh Parse *pParse, /* Parsing context */ 1090290c1948Sdrh Table *pTab, /* Table to be opened */ 1091290c1948Sdrh int base, /* Cursor number assigned to the table */ 1092290c1948Sdrh int op /* OP_OpenRead or OP_OpenWrite */ 1093290c1948Sdrh ){ 1094cd44690aSdrh int i; 1095cd44690aSdrh Index *pIdx; 10964adee20fSdanielk1977 Vdbe *v = sqlite3GetVdbe(pParse); 1097cd44690aSdrh assert( v!=0 ); 10984adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Integer, pTab->iDb, 0); 1099290c1948Sdrh sqlite3VdbeAddOp(v, op, base, pTab->tnum); 1100ad6d9460Sdrh VdbeComment((v, "# %s", pTab->zName)); 1101b4964b72Sdanielk1977 sqlite3VdbeAddOp(v, OP_SetNumColumns, base, pTab->nCol); 1102cd44690aSdrh for(i=1, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){ 11034adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Integer, pIdx->iDb, 0); 1104290c1948Sdrh sqlite3VdbeOp3(v, op, i+base, pIdx->tnum, 1105d3d39e93Sdrh (char*)&pIdx->keyInfo, P3_KEYINFO); 1106cd44690aSdrh } 1107290c1948Sdrh if( pParse->nTab<=base+i ){ 1108290c1948Sdrh pParse->nTab = base+i; 1109290c1948Sdrh } 1110cd44690aSdrh } 1111