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*cc6bd383Sdanielk1977 ** $Id: insert.c,v 1.129 2005/01/10 02:48:49 danielk1977 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 973d1bfeaaSdanielk1977 983d1bfeaaSdanielk1977 /* 991ccde15dSdrh ** This routine is call to handle SQL of the following forms: 100cce7d176Sdrh ** 101cce7d176Sdrh ** insert into TABLE (IDLIST) values(EXPRLIST) 1021ccde15dSdrh ** insert into TABLE (IDLIST) select 103cce7d176Sdrh ** 1041ccde15dSdrh ** The IDLIST following the table name is always optional. If omitted, 1051ccde15dSdrh ** then a list of all columns for the table is substituted. The IDLIST 106967e8b73Sdrh ** appears in the pColumn parameter. pColumn is NULL if IDLIST is omitted. 1071ccde15dSdrh ** 1081ccde15dSdrh ** The pList parameter holds EXPRLIST in the first form of the INSERT 1091ccde15dSdrh ** statement above, and pSelect is NULL. For the second form, pList is 1101ccde15dSdrh ** NULL and pSelect is a pointer to the select statement used to generate 1111ccde15dSdrh ** data for the insert. 112142e30dfSdrh ** 113142e30dfSdrh ** The code generated follows one of three templates. For a simple 114142e30dfSdrh ** select with data coming from a VALUES clause, the code executes 115142e30dfSdrh ** once straight down through. The template looks like this: 116142e30dfSdrh ** 117142e30dfSdrh ** open write cursor to <table> and its indices 118142e30dfSdrh ** puts VALUES clause expressions onto the stack 119142e30dfSdrh ** write the resulting record into <table> 120142e30dfSdrh ** cleanup 121142e30dfSdrh ** 122142e30dfSdrh ** If the statement is of the form 123142e30dfSdrh ** 124142e30dfSdrh ** INSERT INTO <table> SELECT ... 125142e30dfSdrh ** 126142e30dfSdrh ** And the SELECT clause does not read from <table> at any time, then 127142e30dfSdrh ** the generated code follows this template: 128142e30dfSdrh ** 129142e30dfSdrh ** goto B 130142e30dfSdrh ** A: setup for the SELECT 131142e30dfSdrh ** loop over the tables in the SELECT 132142e30dfSdrh ** gosub C 133142e30dfSdrh ** end loop 134142e30dfSdrh ** cleanup after the SELECT 135142e30dfSdrh ** goto D 136142e30dfSdrh ** B: open write cursor to <table> and its indices 137142e30dfSdrh ** goto A 138142e30dfSdrh ** C: insert the select result into <table> 139142e30dfSdrh ** return 140142e30dfSdrh ** D: cleanup 141142e30dfSdrh ** 142142e30dfSdrh ** The third template is used if the insert statement takes its 143142e30dfSdrh ** values from a SELECT but the data is being inserted into a table 144142e30dfSdrh ** that is also read as part of the SELECT. In the third form, 145142e30dfSdrh ** we have to use a intermediate table to store the results of 146142e30dfSdrh ** the select. The template is like this: 147142e30dfSdrh ** 148142e30dfSdrh ** goto B 149142e30dfSdrh ** A: setup for the SELECT 150142e30dfSdrh ** loop over the tables in the SELECT 151142e30dfSdrh ** gosub C 152142e30dfSdrh ** end loop 153142e30dfSdrh ** cleanup after the SELECT 154142e30dfSdrh ** goto D 155142e30dfSdrh ** C: insert the select result into the intermediate table 156142e30dfSdrh ** return 157142e30dfSdrh ** B: open a cursor to an intermediate table 158142e30dfSdrh ** goto A 159142e30dfSdrh ** D: open write cursor to <table> and its indices 160142e30dfSdrh ** loop over the intermediate table 161142e30dfSdrh ** transfer values form intermediate table into <table> 162142e30dfSdrh ** end the loop 163142e30dfSdrh ** cleanup 164cce7d176Sdrh */ 1654adee20fSdanielk1977 void sqlite3Insert( 166cce7d176Sdrh Parse *pParse, /* Parser context */ 167113088ecSdrh SrcList *pTabList, /* Name of table into which we are inserting */ 168cce7d176Sdrh ExprList *pList, /* List of values to be inserted */ 1695974a30fSdrh Select *pSelect, /* A SELECT statement to use as the data source */ 1709cfcf5d4Sdrh IdList *pColumn, /* Column names corresponding to IDLIST. */ 1719cfcf5d4Sdrh int onError /* How to handle constraint errors */ 172cce7d176Sdrh ){ 1735974a30fSdrh Table *pTab; /* The table to insert into */ 174113088ecSdrh char *zTab; /* Name of the table into which we are inserting */ 175e22a334bSdrh const char *zDb; /* Name of the database holding this table */ 1765974a30fSdrh int i, j, idx; /* Loop counters */ 1775974a30fSdrh Vdbe *v; /* Generate code into this virtual machine */ 1785974a30fSdrh Index *pIdx; /* For looping over indices of the table */ 179967e8b73Sdrh int nColumn; /* Number of columns in the data */ 180cfe9a69fSdanielk1977 int base = 0; /* VDBE Cursor number for pTab */ 181cfe9a69fSdanielk1977 int iCont=0,iBreak=0; /* Beginning and end of the loop over srcTab */ 1829bb575fdSdrh sqlite3 *db; /* The main database structure */ 1834a32431cSdrh int keyColumn = -1; /* Column that is the INTEGER PRIMARY KEY */ 1840ca3e24bSdrh int endOfLoop; /* Label for the end of the insertion loop */ 185142e30dfSdrh int useTempTable; /* Store SELECT results in intermediate table */ 186cfe9a69fSdanielk1977 int srcTab = 0; /* Data comes from this temporary cursor if >=0 */ 187cfe9a69fSdanielk1977 int iSelectLoop = 0; /* Address of code that implements the SELECT */ 188cfe9a69fSdanielk1977 int iCleanup = 0; /* Address of the cleanup code */ 189cfe9a69fSdanielk1977 int iInsertBlock = 0; /* Address of the subroutine used to insert data */ 190cfe9a69fSdanielk1977 int iCntMem = 0; /* Memory cell used for the row counter */ 191798da52cSdrh int newIdx = -1; /* Cursor for the NEW table */ 1922958a4e6Sdrh Db *pDb; /* The database containing table being inserted into */ 1932958a4e6Sdrh int counterMem = 0; /* Memory cell holding AUTOINCREMENT counter */ 194cce7d176Sdrh 195798da52cSdrh #ifndef SQLITE_OMIT_TRIGGER 196798da52cSdrh int isView; /* True if attempting to insert into a view */ 197dca76841Sdrh int triggers_exist = 0; /* True if there are FOR EACH ROW triggers */ 198798da52cSdrh #endif 199c3f9bad2Sdanielk1977 200f3388144Sdrh #ifndef SQLITE_OMIT_AUTOINCREMENT 201f3388144Sdrh int counterRowid; /* Memory cell holding rowid of autoinc counter */ 202f3388144Sdrh #endif 203f3388144Sdrh 20424b03fd0Sdanielk1977 if( pParse->nErr || sqlite3_malloc_failed ) goto insert_cleanup; 205ecdc7530Sdrh db = pParse->db; 206daffd0e5Sdrh 2071ccde15dSdrh /* Locate the table into which we will be inserting new information. 2081ccde15dSdrh */ 209113088ecSdrh assert( pTabList->nSrc==1 ); 210113088ecSdrh zTab = pTabList->a[0].zName; 211daffd0e5Sdrh if( zTab==0 ) goto insert_cleanup; 2124adee20fSdanielk1977 pTab = sqlite3SrcListLookup(pParse, pTabList); 213c3f9bad2Sdanielk1977 if( pTab==0 ){ 214c3f9bad2Sdanielk1977 goto insert_cleanup; 215c3f9bad2Sdanielk1977 } 216e22a334bSdrh assert( pTab->iDb<db->nDb ); 2172958a4e6Sdrh pDb = &db->aDb[pTab->iDb]; 2182958a4e6Sdrh zDb = pDb->zName; 2194adee20fSdanielk1977 if( sqlite3AuthCheck(pParse, SQLITE_INSERT, pTab->zName, 0, zDb) ){ 2201962bda7Sdrh goto insert_cleanup; 2211962bda7Sdrh } 222c3f9bad2Sdanielk1977 223b7f9164eSdrh /* Figure out if we have any triggers and if the table being 224b7f9164eSdrh ** inserted into is a view 225b7f9164eSdrh */ 226b7f9164eSdrh #ifndef SQLITE_OMIT_TRIGGER 227dca76841Sdrh triggers_exist = sqlite3TriggersExist(pParse, pTab, TK_INSERT, 0); 228b7f9164eSdrh isView = pTab->pSelect!=0; 229b7f9164eSdrh #else 230dca76841Sdrh # define triggers_exist 0 231b7f9164eSdrh # define isView 0 232b7f9164eSdrh #endif 233b7f9164eSdrh #ifdef SQLITE_OMIT_VIEW 234b7f9164eSdrh # undef isView 235b7f9164eSdrh # define isView 0 236b7f9164eSdrh #endif 237b7f9164eSdrh 238c3f9bad2Sdanielk1977 /* Ensure that: 239c3f9bad2Sdanielk1977 * (a) the table is not read-only, 240c3f9bad2Sdanielk1977 * (b) that if it is a view then ON INSERT triggers exist 241c3f9bad2Sdanielk1977 */ 242dca76841Sdrh if( sqlite3IsReadOnly(pParse, pTab, triggers_exist) ){ 243c3f9bad2Sdanielk1977 goto insert_cleanup; 244c3f9bad2Sdanielk1977 } 245a76b5dfcSdrh if( pTab==0 ) goto insert_cleanup; 2461ccde15dSdrh 247f573c99bSdrh /* If pTab is really a view, make sure it has been initialized. 248f573c99bSdrh */ 2494adee20fSdanielk1977 if( isView && sqlite3ViewGetColumnNames(pParse, pTab) ){ 250f573c99bSdrh goto insert_cleanup; 251f573c99bSdrh } 252f573c99bSdrh 2537cedc8d4Sdanielk1977 /* Ensure all required collation sequences are available. */ 2547cedc8d4Sdanielk1977 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){ 2557cedc8d4Sdanielk1977 if( sqlite3CheckIndexCollSeq(pParse, pIdx) ){ 2567cedc8d4Sdanielk1977 goto insert_cleanup; 2577cedc8d4Sdanielk1977 } 2587cedc8d4Sdanielk1977 } 2597cedc8d4Sdanielk1977 2601ccde15dSdrh /* Allocate a VDBE 2611ccde15dSdrh */ 2624adee20fSdanielk1977 v = sqlite3GetVdbe(pParse); 2635974a30fSdrh if( v==0 ) goto insert_cleanup; 2644794f735Sdrh if( pParse->nested==0 ) sqlite3VdbeCountChanges(v); 265dca76841Sdrh sqlite3BeginWriteOperation(pParse, pSelect || triggers_exist, pTab->iDb); 2661ccde15dSdrh 267c3f9bad2Sdanielk1977 /* if there are row triggers, allocate a temp table for new.* references. */ 268dca76841Sdrh if( triggers_exist ){ 269c3f9bad2Sdanielk1977 newIdx = pParse->nTab++; 270f29ce559Sdanielk1977 } 271c3f9bad2Sdanielk1977 2722958a4e6Sdrh #ifndef SQLITE_OMIT_AUTOINCREMENT 2732958a4e6Sdrh /* If this is an AUTOINCREMENT table, look up the sequence number in the 274f3388144Sdrh ** sqlite_sequence table and store it in memory cell counterMem. Also 275f3388144Sdrh ** remember the rowid of the sqlite_sequence table entry in memory cell 276f3388144Sdrh ** counterRowid. 2772958a4e6Sdrh */ 2782958a4e6Sdrh if( pTab->autoInc ){ 279f3388144Sdrh int iCur = pParse->nTab; 280f3388144Sdrh int base = sqlite3VdbeCurrentAddr(v); 281f3388144Sdrh counterRowid = pParse->nMem++; 282f3388144Sdrh counterMem = pParse->nMem++; 283f3388144Sdrh sqlite3VdbeAddOp(v, OP_Integer, pTab->iDb, 0); 284f3388144Sdrh sqlite3VdbeAddOp(v, OP_OpenRead, iCur, pDb->pSeqTab->tnum); 285f3388144Sdrh sqlite3VdbeAddOp(v, OP_SetNumColumns, iCur, 2); 286f3388144Sdrh sqlite3VdbeAddOp(v, OP_Rewind, iCur, base+13); 287f3388144Sdrh sqlite3VdbeAddOp(v, OP_Column, iCur, 0); 288f3388144Sdrh sqlite3VdbeOp3(v, OP_String8, 0, 0, pTab->zName, 0); 289f3388144Sdrh sqlite3VdbeAddOp(v, OP_Ne, 28417, base+12); 290f3388144Sdrh sqlite3VdbeAddOp(v, OP_Recno, iCur, 0); 291f3388144Sdrh sqlite3VdbeAddOp(v, OP_MemStore, counterRowid, 1); 292f3388144Sdrh sqlite3VdbeAddOp(v, OP_Column, iCur, 1); 293f3388144Sdrh sqlite3VdbeAddOp(v, OP_MemStore, counterMem, 1); 294f3388144Sdrh sqlite3VdbeAddOp(v, OP_Goto, 0, base+13); 295f3388144Sdrh sqlite3VdbeAddOp(v, OP_Next, iCur, base+4); 296f3388144Sdrh sqlite3VdbeAddOp(v, OP_Close, iCur, 0); 2972958a4e6Sdrh } 2982958a4e6Sdrh #endif /* SQLITE_OMIT_AUTOINCREMENT */ 2992958a4e6Sdrh 3001ccde15dSdrh /* Figure out how many columns of data are supplied. If the data 301142e30dfSdrh ** is coming from a SELECT statement, then this step also generates 302142e30dfSdrh ** all the code to implement the SELECT statement and invoke a subroutine 303142e30dfSdrh ** to process each row of the result. (Template 2.) If the SELECT 304142e30dfSdrh ** statement uses the the table that is being inserted into, then the 305142e30dfSdrh ** subroutine is also coded here. That subroutine stores the SELECT 306142e30dfSdrh ** results in a temporary table. (Template 3.) 3071ccde15dSdrh */ 3085974a30fSdrh if( pSelect ){ 309142e30dfSdrh /* Data is coming from a SELECT. Generate code to implement that SELECT 310142e30dfSdrh */ 311142e30dfSdrh int rc, iInitCode; 3124adee20fSdanielk1977 iInitCode = sqlite3VdbeAddOp(v, OP_Goto, 0, 0); 3134adee20fSdanielk1977 iSelectLoop = sqlite3VdbeCurrentAddr(v); 3144adee20fSdanielk1977 iInsertBlock = sqlite3VdbeMakeLabel(v); 31584ac9d02Sdanielk1977 rc = sqlite3Select(pParse, pSelect, SRT_Subroutine, iInsertBlock, 0,0,0,0); 31624b03fd0Sdanielk1977 if( rc || pParse->nErr || sqlite3_malloc_failed ) goto insert_cleanup; 3174adee20fSdanielk1977 iCleanup = sqlite3VdbeMakeLabel(v); 3184adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Goto, 0, iCleanup); 3195974a30fSdrh assert( pSelect->pEList ); 320967e8b73Sdrh nColumn = pSelect->pEList->nExpr; 321142e30dfSdrh 322142e30dfSdrh /* Set useTempTable to TRUE if the result of the SELECT statement 323142e30dfSdrh ** should be written into a temporary table. Set to FALSE if each 324142e30dfSdrh ** row of the SELECT can be written directly into the result table. 325048c530cSdrh ** 326048c530cSdrh ** A temp table must be used if the table being updated is also one 327048c530cSdrh ** of the tables being read by the SELECT statement. Also use a 328048c530cSdrh ** temp table in the case of row triggers. 329142e30dfSdrh */ 330dca76841Sdrh if( triggers_exist ){ 331048c530cSdrh useTempTable = 1; 332048c530cSdrh }else{ 333a42707b2Sdrh int addr = 0; 334048c530cSdrh useTempTable = 0; 335a42707b2Sdrh while( useTempTable==0 ){ 336a42707b2Sdrh VdbeOp *pOp; 337a42707b2Sdrh addr = sqlite3VdbeFindOp(v, addr, OP_OpenRead, pTab->tnum); 338a42707b2Sdrh if( addr==0 ) break; 339a42707b2Sdrh pOp = sqlite3VdbeGetOp(v, addr-2); 340048c530cSdrh if( pOp->opcode==OP_Integer && pOp->p1==pTab->iDb ){ 341048c530cSdrh useTempTable = 1; 342048c530cSdrh } 343048c530cSdrh } 344048c530cSdrh } 345142e30dfSdrh 346142e30dfSdrh if( useTempTable ){ 347142e30dfSdrh /* Generate the subroutine that SELECT calls to process each row of 348142e30dfSdrh ** the result. Store the result in a temporary table 349142e30dfSdrh */ 350142e30dfSdrh srcTab = pParse->nTab++; 3514adee20fSdanielk1977 sqlite3VdbeResolveLabel(v, iInsertBlock); 3524adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_MakeRecord, nColumn, 0); 353a37cdde0Sdanielk1977 sqlite3TableAffinityStr(v, pTab); 3544adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_NewRecno, srcTab, 0); 3554adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Pull, 1, 0); 3564adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_PutIntKey, srcTab, 0); 3574adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Return, 0, 0); 358142e30dfSdrh 359142e30dfSdrh /* The following code runs first because the GOTO at the very top 360142e30dfSdrh ** of the program jumps to it. Create the temporary table, then jump 361142e30dfSdrh ** back up and execute the SELECT code above. 362142e30dfSdrh */ 3634adee20fSdanielk1977 sqlite3VdbeChangeP2(v, iInitCode, sqlite3VdbeCurrentAddr(v)); 3644adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_OpenTemp, srcTab, 0); 365b6f5452fSdrh sqlite3VdbeAddOp(v, OP_SetNumColumns, srcTab, nColumn); 3664adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Goto, 0, iSelectLoop); 3674adee20fSdanielk1977 sqlite3VdbeResolveLabel(v, iCleanup); 3685974a30fSdrh }else{ 3694adee20fSdanielk1977 sqlite3VdbeChangeP2(v, iInitCode, sqlite3VdbeCurrentAddr(v)); 370142e30dfSdrh } 371142e30dfSdrh }else{ 372142e30dfSdrh /* This is the case if the data for the INSERT is coming from a VALUES 373142e30dfSdrh ** clause 374142e30dfSdrh */ 375ad3cab52Sdrh SrcList dummy; 376daffd0e5Sdrh assert( pList!=0 ); 3775974a30fSdrh srcTab = -1; 378142e30dfSdrh useTempTable = 0; 3795974a30fSdrh assert( pList ); 380967e8b73Sdrh nColumn = pList->nExpr; 381ad3cab52Sdrh dummy.nSrc = 0; 382e64e7b20Sdrh for(i=0; i<nColumn; i++){ 383290c1948Sdrh if( sqlite3ExprResolveAndCheck(pParse,&dummy,0,pList->a[i].pExpr,0,0) ){ 384b04a5d87Sdrh goto insert_cleanup; 385b04a5d87Sdrh } 386e64e7b20Sdrh } 3875974a30fSdrh } 3881ccde15dSdrh 3891ccde15dSdrh /* Make sure the number of columns in the source data matches the number 3901ccde15dSdrh ** of columns to be inserted into the table. 3911ccde15dSdrh */ 392967e8b73Sdrh if( pColumn==0 && nColumn!=pTab->nCol ){ 3934adee20fSdanielk1977 sqlite3ErrorMsg(pParse, 394da93d238Sdrh "table %S has %d columns but %d values were supplied", 395da93d238Sdrh pTabList, 0, pTab->nCol, nColumn); 396cce7d176Sdrh goto insert_cleanup; 397cce7d176Sdrh } 398967e8b73Sdrh if( pColumn!=0 && nColumn!=pColumn->nId ){ 3994adee20fSdanielk1977 sqlite3ErrorMsg(pParse, "%d values for %d columns", nColumn, pColumn->nId); 400cce7d176Sdrh goto insert_cleanup; 401cce7d176Sdrh } 4021ccde15dSdrh 4031ccde15dSdrh /* If the INSERT statement included an IDLIST term, then make sure 4041ccde15dSdrh ** all elements of the IDLIST really are columns of the table and 4051ccde15dSdrh ** remember the column indices. 406c8392586Sdrh ** 407c8392586Sdrh ** If the table has an INTEGER PRIMARY KEY column and that column 408c8392586Sdrh ** is named in the IDLIST, then record in the keyColumn variable 409c8392586Sdrh ** the index into IDLIST of the primary key column. keyColumn is 410c8392586Sdrh ** the index of the primary key as it appears in IDLIST, not as 411c8392586Sdrh ** is appears in the original table. (The index of the primary 412c8392586Sdrh ** key in the original table is pTab->iPKey.) 4131ccde15dSdrh */ 414967e8b73Sdrh if( pColumn ){ 415967e8b73Sdrh for(i=0; i<pColumn->nId; i++){ 416967e8b73Sdrh pColumn->a[i].idx = -1; 417cce7d176Sdrh } 418967e8b73Sdrh for(i=0; i<pColumn->nId; i++){ 419cce7d176Sdrh for(j=0; j<pTab->nCol; j++){ 4204adee20fSdanielk1977 if( sqlite3StrICmp(pColumn->a[i].zName, pTab->aCol[j].zName)==0 ){ 421967e8b73Sdrh pColumn->a[i].idx = j; 4224a32431cSdrh if( j==pTab->iPKey ){ 4239aa028daSdrh keyColumn = i; 4244a32431cSdrh } 425cce7d176Sdrh break; 426cce7d176Sdrh } 427cce7d176Sdrh } 428cce7d176Sdrh if( j>=pTab->nCol ){ 4294adee20fSdanielk1977 if( sqlite3IsRowid(pColumn->a[i].zName) ){ 430a0217ba7Sdrh keyColumn = i; 431a0217ba7Sdrh }else{ 4324adee20fSdanielk1977 sqlite3ErrorMsg(pParse, "table %S has no column named %s", 433da93d238Sdrh pTabList, 0, pColumn->a[i].zName); 434cce7d176Sdrh pParse->nErr++; 435cce7d176Sdrh goto insert_cleanup; 436cce7d176Sdrh } 437cce7d176Sdrh } 438cce7d176Sdrh } 439a0217ba7Sdrh } 4401ccde15dSdrh 441aacc543eSdrh /* If there is no IDLIST term but the table has an integer primary 442c8392586Sdrh ** key, the set the keyColumn variable to the primary key column index 443c8392586Sdrh ** in the original table definition. 4444a32431cSdrh */ 4454a32431cSdrh if( pColumn==0 ){ 4464a32431cSdrh keyColumn = pTab->iPKey; 4474a32431cSdrh } 4484a32431cSdrh 449142e30dfSdrh /* Open the temp table for FOR EACH ROW triggers 450142e30dfSdrh */ 451dca76841Sdrh if( triggers_exist ){ 4524adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_OpenPseudo, newIdx, 0); 45384ac9d02Sdanielk1977 sqlite3VdbeAddOp(v, OP_SetNumColumns, newIdx, pTab->nCol); 454f29ce559Sdanielk1977 } 455c3f9bad2Sdanielk1977 456c3f9bad2Sdanielk1977 /* Initialize the count of rows to be inserted 4571ccde15dSdrh */ 458142e30dfSdrh if( db->flags & SQLITE_CountRows ){ 459142e30dfSdrh iCntMem = pParse->nMem++; 4604adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Integer, 0, 0); 4614adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_MemStore, iCntMem, 1); 462c3f9bad2Sdanielk1977 } 463c3f9bad2Sdanielk1977 464c3f9bad2Sdanielk1977 /* Open tables and indices if there are no row triggers */ 465dca76841Sdrh if( !triggers_exist ){ 4665974a30fSdrh base = pParse->nTab; 467290c1948Sdrh sqlite3OpenTableAndIndices(pParse, pTab, base, OP_OpenWrite); 468feeb1394Sdrh } 469feeb1394Sdrh 470142e30dfSdrh /* If the data source is a temporary table, then we have to create 4711ccde15dSdrh ** a loop because there might be multiple rows of data. If the data 472142e30dfSdrh ** source is a subroutine call from the SELECT statement, then we need 473142e30dfSdrh ** to launch the SELECT statement processing. 4741ccde15dSdrh */ 475142e30dfSdrh if( useTempTable ){ 4764adee20fSdanielk1977 iBreak = sqlite3VdbeMakeLabel(v); 4774adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Rewind, srcTab, iBreak); 4784adee20fSdanielk1977 iCont = sqlite3VdbeCurrentAddr(v); 479142e30dfSdrh }else if( pSelect ){ 4804adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Goto, 0, iSelectLoop); 4814adee20fSdanielk1977 sqlite3VdbeResolveLabel(v, iInsertBlock); 482bed8690fSdrh } 4831ccde15dSdrh 4845cf590c1Sdrh /* Run the BEFORE and INSTEAD OF triggers, if there are any 48570ce3f0cSdrh */ 4864adee20fSdanielk1977 endOfLoop = sqlite3VdbeMakeLabel(v); 487dca76841Sdrh if( triggers_exist & TRIGGER_BEFORE ){ 488c3f9bad2Sdanielk1977 48970ce3f0cSdrh /* build the NEW.* reference row. Note that if there is an INTEGER 49070ce3f0cSdrh ** PRIMARY KEY into which a NULL is being inserted, that NULL will be 49170ce3f0cSdrh ** translated into a unique ID for the row. But on a BEFORE trigger, 49270ce3f0cSdrh ** we do not know what the unique ID will be (because the insert has 49370ce3f0cSdrh ** not happened yet) so we substitute a rowid of -1 49470ce3f0cSdrh */ 49570ce3f0cSdrh if( keyColumn<0 ){ 4964adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Integer, -1, 0); 49770ce3f0cSdrh }else if( useTempTable ){ 4984adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Column, srcTab, keyColumn); 49970ce3f0cSdrh }else if( pSelect ){ 5004adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Dup, nColumn - keyColumn - 1, 1); 50170ce3f0cSdrh }else{ 5024adee20fSdanielk1977 sqlite3ExprCode(pParse, pList->a[keyColumn].pExpr); 5034adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_NotNull, -1, sqlite3VdbeCurrentAddr(v)+3); 5044adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Pop, 1, 0); 5054adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Integer, -1, 0); 5064adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_MustBeInt, 0, 0); 50770ce3f0cSdrh } 50870ce3f0cSdrh 50970ce3f0cSdrh /* Create the new column data 51070ce3f0cSdrh */ 511c3f9bad2Sdanielk1977 for(i=0; i<pTab->nCol; i++){ 512c3f9bad2Sdanielk1977 if( pColumn==0 ){ 513c3f9bad2Sdanielk1977 j = i; 514c3f9bad2Sdanielk1977 }else{ 515c3f9bad2Sdanielk1977 for(j=0; j<pColumn->nId; j++){ 516c3f9bad2Sdanielk1977 if( pColumn->a[j].idx==i ) break; 517c3f9bad2Sdanielk1977 } 518c3f9bad2Sdanielk1977 } 519c3f9bad2Sdanielk1977 if( pColumn && j>=pColumn->nId ){ 5207977a17fSdanielk1977 sqlite3ExprCode(pParse, pTab->aCol[i].pDflt); 521142e30dfSdrh }else if( useTempTable ){ 5224adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Column, srcTab, j); 523142e30dfSdrh }else if( pSelect ){ 5244adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Dup, nColumn-j-1, 1); 525c3f9bad2Sdanielk1977 }else{ 52625303780Sdrh sqlite3ExprCodeAndCache(pParse, pList->a[j].pExpr); 527c3f9bad2Sdanielk1977 } 528c3f9bad2Sdanielk1977 } 5294adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_MakeRecord, pTab->nCol, 0); 530a37cdde0Sdanielk1977 531a37cdde0Sdanielk1977 /* If this is an INSERT on a view with an INSTEAD OF INSERT trigger, 532a37cdde0Sdanielk1977 ** do not attempt any conversions before assembling the record. 533a37cdde0Sdanielk1977 ** If this is a real table, attempt conversions as required by the 534a37cdde0Sdanielk1977 ** table column affinities. 535a37cdde0Sdanielk1977 */ 536a37cdde0Sdanielk1977 if( !isView ){ 537a37cdde0Sdanielk1977 sqlite3TableAffinityStr(v, pTab); 538a37cdde0Sdanielk1977 } 5394adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_PutIntKey, newIdx, 0); 540c3f9bad2Sdanielk1977 5415cf590c1Sdrh /* Fire BEFORE or INSTEAD OF triggers */ 542dca76841Sdrh if( sqlite3CodeRowTrigger(pParse, TK_INSERT, 0, TRIGGER_BEFORE, pTab, 543b2fe7d8cSdrh newIdx, -1, onError, endOfLoop) ){ 544f29ce559Sdanielk1977 goto insert_cleanup; 545f29ce559Sdanielk1977 } 54670ce3f0cSdrh } 547c3f9bad2Sdanielk1977 54870ce3f0cSdrh /* If any triggers exists, the opening of tables and indices is deferred 54970ce3f0cSdrh ** until now. 55070ce3f0cSdrh */ 551dca76841Sdrh if( triggers_exist && !isView ){ 552c3f9bad2Sdanielk1977 base = pParse->nTab; 553290c1948Sdrh sqlite3OpenTableAndIndices(pParse, pTab, base, OP_OpenWrite); 554c3f9bad2Sdanielk1977 } 555c3f9bad2Sdanielk1977 5564a32431cSdrh /* Push the record number for the new entry onto the stack. The 5574a32431cSdrh ** record number is a randomly generate integer created by NewRecno 5584a32431cSdrh ** except when the table has an INTEGER PRIMARY KEY column, in which 559b419a926Sdrh ** case the record number is the same as that column. 5601ccde15dSdrh */ 5615cf590c1Sdrh if( !isView ){ 5624a32431cSdrh if( keyColumn>=0 ){ 563142e30dfSdrh if( useTempTable ){ 5644adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Column, srcTab, keyColumn); 565142e30dfSdrh }else if( pSelect ){ 5664adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Dup, nColumn - keyColumn - 1, 1); 5674a32431cSdrh }else{ 5684adee20fSdanielk1977 sqlite3ExprCode(pParse, pList->a[keyColumn].pExpr); 56927a32783Sdrh } 570e1e68f49Sdrh /* If the PRIMARY KEY expression is NULL, then use OP_NewRecno 571e1e68f49Sdrh ** to generate a unique primary key value. 572e1e68f49Sdrh */ 5734adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_NotNull, -1, sqlite3VdbeCurrentAddr(v)+3); 5744adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Pop, 1, 0); 5752958a4e6Sdrh sqlite3VdbeAddOp(v, OP_NewRecno, base, counterMem); 5764adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_MustBeInt, 0, 0); 5774a32431cSdrh }else{ 578f3388144Sdrh sqlite3VdbeAddOp(v, OP_NewRecno, base, counterMem); 5794a32431cSdrh } 5802958a4e6Sdrh #ifndef SQLITE_OMIT_AUTOINCREMENT 5812958a4e6Sdrh if( pTab->autoInc ){ 5822958a4e6Sdrh sqlite3VdbeAddOp(v, OP_MemMax, counterMem, 0); 5832958a4e6Sdrh } 5842958a4e6Sdrh #endif /* SQLITE_OMIT_AUTOINCREMENT */ 5854a32431cSdrh 586aacc543eSdrh /* Push onto the stack, data for all columns of the new entry, beginning 5874a32431cSdrh ** with the first column. 5884a32431cSdrh */ 589cce7d176Sdrh for(i=0; i<pTab->nCol; i++){ 5904a32431cSdrh if( i==pTab->iPKey ){ 5914a32431cSdrh /* The value of the INTEGER PRIMARY KEY column is always a NULL. 592aacc543eSdrh ** Whenever this column is read, the record number will be substituted 593aacc543eSdrh ** in its place. So will fill this column with a NULL to avoid 594aacc543eSdrh ** taking up data space with information that will never be used. */ 5950f69c1e3Sdanielk1977 sqlite3VdbeAddOp(v, OP_String8, 0, 0); 5964a32431cSdrh continue; 5974a32431cSdrh } 598967e8b73Sdrh if( pColumn==0 ){ 599cce7d176Sdrh j = i; 600cce7d176Sdrh }else{ 601967e8b73Sdrh for(j=0; j<pColumn->nId; j++){ 602967e8b73Sdrh if( pColumn->a[j].idx==i ) break; 603cce7d176Sdrh } 604cce7d176Sdrh } 605967e8b73Sdrh if( pColumn && j>=pColumn->nId ){ 6067977a17fSdanielk1977 sqlite3ExprCode(pParse, pTab->aCol[i].pDflt); 607142e30dfSdrh }else if( useTempTable ){ 6084adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Column, srcTab, j); 609142e30dfSdrh }else if( pSelect ){ 6104adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Dup, i+nColumn-j, 1); 611cce7d176Sdrh }else{ 6124adee20fSdanielk1977 sqlite3ExprCode(pParse, pList->a[j].pExpr); 613cce7d176Sdrh } 614cce7d176Sdrh } 6151ccde15dSdrh 6160ca3e24bSdrh /* Generate code to check constraints and generate index keys and 6170ca3e24bSdrh ** do the insertion. 6184a32431cSdrh */ 6194adee20fSdanielk1977 sqlite3GenerateConstraintChecks(pParse, pTab, base, 0, keyColumn>=0, 620a0217ba7Sdrh 0, onError, endOfLoop); 6214adee20fSdanielk1977 sqlite3CompleteInsertion(pParse, pTab, base, 0,0,0, 622dca76841Sdrh (triggers_exist & TRIGGER_AFTER)!=0 ? newIdx : -1); 6235cf590c1Sdrh } 6241bee3d7bSdrh 625feeb1394Sdrh /* Update the count of rows that are inserted 6261bee3d7bSdrh */ 627142e30dfSdrh if( (db->flags & SQLITE_CountRows)!=0 ){ 6284adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_MemIncr, iCntMem, 0); 6291bee3d7bSdrh } 630c3f9bad2Sdanielk1977 631dca76841Sdrh if( triggers_exist ){ 632c3f9bad2Sdanielk1977 /* Close all tables opened */ 6335cf590c1Sdrh if( !isView ){ 6344adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Close, base, 0); 635c3f9bad2Sdanielk1977 for(idx=1, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, idx++){ 6364adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Close, idx+base, 0); 637c3f9bad2Sdanielk1977 } 638c3f9bad2Sdanielk1977 } 639c3f9bad2Sdanielk1977 640c3f9bad2Sdanielk1977 /* Code AFTER triggers */ 641dca76841Sdrh if( sqlite3CodeRowTrigger(pParse, TK_INSERT, 0, TRIGGER_AFTER, pTab, 642dca76841Sdrh newIdx, -1, onError, endOfLoop) ){ 643f29ce559Sdanielk1977 goto insert_cleanup; 644f29ce559Sdanielk1977 } 645c3f9bad2Sdanielk1977 } 6461bee3d7bSdrh 6471ccde15dSdrh /* The bottom of the loop, if the data source is a SELECT statement 6481ccde15dSdrh */ 6494adee20fSdanielk1977 sqlite3VdbeResolveLabel(v, endOfLoop); 650142e30dfSdrh if( useTempTable ){ 6514adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Next, srcTab, iCont); 6524adee20fSdanielk1977 sqlite3VdbeResolveLabel(v, iBreak); 6534adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Close, srcTab, 0); 654142e30dfSdrh }else if( pSelect ){ 6554adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Pop, nColumn, 0); 6564adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Return, 0, 0); 6574adee20fSdanielk1977 sqlite3VdbeResolveLabel(v, iCleanup); 6586b56344dSdrh } 659c3f9bad2Sdanielk1977 660dca76841Sdrh if( !triggers_exist ){ 661c3f9bad2Sdanielk1977 /* Close all tables opened */ 6624adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Close, base, 0); 6636b56344dSdrh for(idx=1, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, idx++){ 6644adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Close, idx+base, 0); 665cce7d176Sdrh } 666c3f9bad2Sdanielk1977 } 667c3f9bad2Sdanielk1977 6682958a4e6Sdrh #ifndef SQLITE_OMIT_AUTOINCREMENT 669f3388144Sdrh /* Update the sqlite_sequence table by storing the content of the 670f3388144Sdrh ** counter value in memory counterMem back into the sqlite_sequence 671f3388144Sdrh ** table. 6722958a4e6Sdrh */ 6732958a4e6Sdrh if( pTab->autoInc ){ 674f3388144Sdrh int iCur = pParse->nTab; 675f3388144Sdrh int base = sqlite3VdbeCurrentAddr(v); 676f3388144Sdrh sqlite3VdbeAddOp(v, OP_Integer, pTab->iDb, 0); 677f3388144Sdrh sqlite3VdbeAddOp(v, OP_OpenWrite, iCur, pDb->pSeqTab->tnum); 678f3388144Sdrh sqlite3VdbeAddOp(v, OP_SetNumColumns, iCur, 2); 679f3388144Sdrh sqlite3VdbeAddOp(v, OP_MemLoad, counterRowid, 0); 680f3388144Sdrh sqlite3VdbeAddOp(v, OP_NotNull, -1, base+7); 681f3388144Sdrh sqlite3VdbeAddOp(v, OP_Pop, 1, 0); 682f3388144Sdrh sqlite3VdbeAddOp(v, OP_NewRecno, iCur, 0); 683f3388144Sdrh sqlite3VdbeOp3(v, OP_String8, 0, 0, pTab->zName, 0); 684f3388144Sdrh sqlite3VdbeAddOp(v, OP_MemLoad, counterMem, 0); 685f3388144Sdrh sqlite3VdbeAddOp(v, OP_MakeRecord, 2, 0); 686f3388144Sdrh sqlite3VdbeAddOp(v, OP_PutIntKey, iCur, 0); 687f3388144Sdrh sqlite3VdbeAddOp(v, OP_Close, iCur, 0); 6882958a4e6Sdrh } 6892958a4e6Sdrh #endif 6902958a4e6Sdrh 6911bee3d7bSdrh /* 692e7de6f25Sdanielk1977 ** Return the number of rows inserted. If this routine is 693e7de6f25Sdanielk1977 ** generating code because of a call to sqlite3NestedParse(), do not 694e7de6f25Sdanielk1977 ** invoke the callback function. 6951bee3d7bSdrh */ 696*cc6bd383Sdanielk1977 if( db->flags & SQLITE_CountRows && pParse->nested==0 && !pParse->trigStack ){ 6974adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_MemLoad, iCntMem, 0); 6984adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Callback, 1, 0); 69922322fd4Sdanielk1977 sqlite3VdbeSetNumCols(v, 1); 7003cf86063Sdanielk1977 sqlite3VdbeSetColName(v, 0, "rows inserted", P3_STATIC); 7011bee3d7bSdrh } 702cce7d176Sdrh 703cce7d176Sdrh insert_cleanup: 7044adee20fSdanielk1977 sqlite3SrcListDelete(pTabList); 7054adee20fSdanielk1977 if( pList ) sqlite3ExprListDelete(pList); 7064adee20fSdanielk1977 if( pSelect ) sqlite3SelectDelete(pSelect); 7074adee20fSdanielk1977 sqlite3IdListDelete(pColumn); 708cce7d176Sdrh } 7099cfcf5d4Sdrh 7109cfcf5d4Sdrh /* 7119cfcf5d4Sdrh ** Generate code to do a constraint check prior to an INSERT or an UPDATE. 7129cfcf5d4Sdrh ** 7139cfcf5d4Sdrh ** When this routine is called, the stack contains (from bottom to top) 7140ca3e24bSdrh ** the following values: 7150ca3e24bSdrh ** 716b2fe7d8cSdrh ** 1. The recno of the row to be updated before the update. This 717b419a926Sdrh ** value is omitted unless we are doing an UPDATE that involves a 718b419a926Sdrh ** change to the record number. 7190ca3e24bSdrh ** 720b419a926Sdrh ** 2. The recno of the row after the update. 7210ca3e24bSdrh ** 7220ca3e24bSdrh ** 3. The data in the first column of the entry after the update. 7230ca3e24bSdrh ** 7240ca3e24bSdrh ** i. Data from middle columns... 7250ca3e24bSdrh ** 7260ca3e24bSdrh ** N. The data in the last column of the entry after the update. 7270ca3e24bSdrh ** 728b419a926Sdrh ** The old recno shown as entry (1) above is omitted unless both isUpdate 7291c92853dSdrh ** and recnoChng are 1. isUpdate is true for UPDATEs and false for 7301c92853dSdrh ** INSERTs and recnoChng is true if the record number is being changed. 7310ca3e24bSdrh ** 7320ca3e24bSdrh ** The code generated by this routine pushes additional entries onto 7330ca3e24bSdrh ** the stack which are the keys for new index entries for the new record. 7340ca3e24bSdrh ** The order of index keys is the same as the order of the indices on 7350ca3e24bSdrh ** the pTable->pIndex list. A key is only created for index i if 7360ca3e24bSdrh ** aIdxUsed!=0 and aIdxUsed[i]!=0. 7379cfcf5d4Sdrh ** 7389cfcf5d4Sdrh ** This routine also generates code to check constraints. NOT NULL, 7399cfcf5d4Sdrh ** CHECK, and UNIQUE constraints are all checked. If a constraint fails, 7401c92853dSdrh ** then the appropriate action is performed. There are five possible 7411c92853dSdrh ** actions: ROLLBACK, ABORT, FAIL, REPLACE, and IGNORE. 7429cfcf5d4Sdrh ** 7439cfcf5d4Sdrh ** Constraint type Action What Happens 7449cfcf5d4Sdrh ** --------------- ---------- ---------------------------------------- 7451c92853dSdrh ** any ROLLBACK The current transaction is rolled back and 74624b03fd0Sdanielk1977 ** sqlite3_exec() returns immediately with a 7479cfcf5d4Sdrh ** return code of SQLITE_CONSTRAINT. 7489cfcf5d4Sdrh ** 7491c92853dSdrh ** any ABORT Back out changes from the current command 7501c92853dSdrh ** only (do not do a complete rollback) then 75124b03fd0Sdanielk1977 ** cause sqlite3_exec() to return immediately 7521c92853dSdrh ** with SQLITE_CONSTRAINT. 7531c92853dSdrh ** 7541c92853dSdrh ** any FAIL Sqlite_exec() returns immediately with a 7551c92853dSdrh ** return code of SQLITE_CONSTRAINT. The 7561c92853dSdrh ** transaction is not rolled back and any 7571c92853dSdrh ** prior changes are retained. 7581c92853dSdrh ** 7599cfcf5d4Sdrh ** any IGNORE The record number and data is popped from 7609cfcf5d4Sdrh ** the stack and there is an immediate jump 7619cfcf5d4Sdrh ** to label ignoreDest. 7629cfcf5d4Sdrh ** 7639cfcf5d4Sdrh ** NOT NULL REPLACE The NULL value is replace by the default 7649cfcf5d4Sdrh ** value for that column. If the default value 7659cfcf5d4Sdrh ** is NULL, the action is the same as ABORT. 7669cfcf5d4Sdrh ** 7679cfcf5d4Sdrh ** UNIQUE REPLACE The other row that conflicts with the row 7689cfcf5d4Sdrh ** being inserted is removed. 7699cfcf5d4Sdrh ** 7709cfcf5d4Sdrh ** CHECK REPLACE Illegal. The results in an exception. 7719cfcf5d4Sdrh ** 7721c92853dSdrh ** Which action to take is determined by the overrideError parameter. 7731c92853dSdrh ** Or if overrideError==OE_Default, then the pParse->onError parameter 7741c92853dSdrh ** is used. Or if pParse->onError==OE_Default then the onError value 7751c92853dSdrh ** for the constraint is used. 7769cfcf5d4Sdrh ** 777aaab5725Sdrh ** The calling routine must open a read/write cursor for pTab with 7789cfcf5d4Sdrh ** cursor number "base". All indices of pTab must also have open 7799cfcf5d4Sdrh ** read/write cursors with cursor number base+i for the i-th cursor. 7809cfcf5d4Sdrh ** Except, if there is no possibility of a REPLACE action then 7819cfcf5d4Sdrh ** cursors do not need to be open for indices where aIdxUsed[i]==0. 7829cfcf5d4Sdrh ** 7839cfcf5d4Sdrh ** If the isUpdate flag is true, it means that the "base" cursor is 7849cfcf5d4Sdrh ** initially pointing to an entry that is being updated. The isUpdate 7859cfcf5d4Sdrh ** flag causes extra code to be generated so that the "base" cursor 7869cfcf5d4Sdrh ** is still pointing at the same entry after the routine returns. 7879cfcf5d4Sdrh ** Without the isUpdate flag, the "base" cursor might be moved. 7889cfcf5d4Sdrh */ 7894adee20fSdanielk1977 void sqlite3GenerateConstraintChecks( 7909cfcf5d4Sdrh Parse *pParse, /* The parser context */ 7919cfcf5d4Sdrh Table *pTab, /* the table into which we are inserting */ 7929cfcf5d4Sdrh int base, /* Index of a read/write cursor pointing at pTab */ 7939cfcf5d4Sdrh char *aIdxUsed, /* Which indices are used. NULL means all are used */ 7940ca3e24bSdrh int recnoChng, /* True if the record number will change */ 795b419a926Sdrh int isUpdate, /* True for UPDATE, False for INSERT */ 7969cfcf5d4Sdrh int overrideError, /* Override onError to this if not OE_Default */ 797b419a926Sdrh int ignoreDest /* Jump to this label on an OE_Ignore resolution */ 7989cfcf5d4Sdrh ){ 7999cfcf5d4Sdrh int i; 8009cfcf5d4Sdrh Vdbe *v; 8019cfcf5d4Sdrh int nCol; 8029cfcf5d4Sdrh int onError; 8039cfcf5d4Sdrh int addr; 8049cfcf5d4Sdrh int extra; 8050ca3e24bSdrh int iCur; 8060ca3e24bSdrh Index *pIdx; 8070ca3e24bSdrh int seenReplace = 0; 808cfe9a69fSdanielk1977 int jumpInst1=0, jumpInst2; 8090ca3e24bSdrh int contAddr; 810b419a926Sdrh int hasTwoRecnos = (isUpdate && recnoChng); 8119cfcf5d4Sdrh 8124adee20fSdanielk1977 v = sqlite3GetVdbe(pParse); 8139cfcf5d4Sdrh assert( v!=0 ); 814417be79cSdrh assert( pTab->pSelect==0 ); /* This table is not a VIEW */ 8159cfcf5d4Sdrh nCol = pTab->nCol; 8169cfcf5d4Sdrh 8179cfcf5d4Sdrh /* Test all NOT NULL constraints. 8189cfcf5d4Sdrh */ 8199cfcf5d4Sdrh for(i=0; i<nCol; i++){ 8200ca3e24bSdrh if( i==pTab->iPKey ){ 8210ca3e24bSdrh continue; 8220ca3e24bSdrh } 8239cfcf5d4Sdrh onError = pTab->aCol[i].notNull; 8240ca3e24bSdrh if( onError==OE_None ) continue; 8259cfcf5d4Sdrh if( overrideError!=OE_Default ){ 8269cfcf5d4Sdrh onError = overrideError; 827a996e477Sdrh }else if( onError==OE_Default ){ 828a996e477Sdrh onError = OE_Abort; 8299cfcf5d4Sdrh } 8307977a17fSdanielk1977 if( onError==OE_Replace && pTab->aCol[i].pDflt==0 ){ 8319cfcf5d4Sdrh onError = OE_Abort; 8329cfcf5d4Sdrh } 8334adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Dup, nCol-1-i, 1); 8344adee20fSdanielk1977 addr = sqlite3VdbeAddOp(v, OP_NotNull, 1, 0); 8359cfcf5d4Sdrh switch( onError ){ 8361c92853dSdrh case OE_Rollback: 8371c92853dSdrh case OE_Abort: 8381c92853dSdrh case OE_Fail: { 839483750baSdrh char *zMsg = 0; 8404adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Halt, SQLITE_CONSTRAINT, onError); 8414adee20fSdanielk1977 sqlite3SetString(&zMsg, pTab->zName, ".", pTab->aCol[i].zName, 84241743984Sdrh " may not be NULL", (char*)0); 8434adee20fSdanielk1977 sqlite3VdbeChangeP3(v, -1, zMsg, P3_DYNAMIC); 8449cfcf5d4Sdrh break; 8459cfcf5d4Sdrh } 8469cfcf5d4Sdrh case OE_Ignore: { 8474adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Pop, nCol+1+hasTwoRecnos, 0); 8484adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Goto, 0, ignoreDest); 8499cfcf5d4Sdrh break; 8509cfcf5d4Sdrh } 8519cfcf5d4Sdrh case OE_Replace: { 8527977a17fSdanielk1977 sqlite3ExprCode(pParse, pTab->aCol[i].pDflt); 8534adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Push, nCol-i, 0); 8549cfcf5d4Sdrh break; 8559cfcf5d4Sdrh } 8560ca3e24bSdrh default: assert(0); 8579cfcf5d4Sdrh } 8584adee20fSdanielk1977 sqlite3VdbeChangeP2(v, addr, sqlite3VdbeCurrentAddr(v)); 8599cfcf5d4Sdrh } 8609cfcf5d4Sdrh 8619cfcf5d4Sdrh /* Test all CHECK constraints 8629cfcf5d4Sdrh */ 8630bd1f4eaSdrh /**** TBD ****/ 8649cfcf5d4Sdrh 8650bd1f4eaSdrh /* If we have an INTEGER PRIMARY KEY, make sure the primary key 8660bd1f4eaSdrh ** of the new record does not previously exist. Except, if this 8670bd1f4eaSdrh ** is an UPDATE and the primary key is not changing, that is OK. 8689cfcf5d4Sdrh */ 8695383ae5cSdrh if( recnoChng ){ 8700ca3e24bSdrh onError = pTab->keyConf; 8710ca3e24bSdrh if( overrideError!=OE_Default ){ 8720ca3e24bSdrh onError = overrideError; 873a996e477Sdrh }else if( onError==OE_Default ){ 874a996e477Sdrh onError = OE_Abort; 8750ca3e24bSdrh } 876a0217ba7Sdrh 87779b0c956Sdrh if( isUpdate ){ 8784adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Dup, nCol+1, 1); 8794adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Dup, nCol+1, 1); 8804adee20fSdanielk1977 jumpInst1 = sqlite3VdbeAddOp(v, OP_Eq, 0, 0); 88179b0c956Sdrh } 8824adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Dup, nCol, 1); 8834adee20fSdanielk1977 jumpInst2 = sqlite3VdbeAddOp(v, OP_NotExists, base, 0); 8840ca3e24bSdrh switch( onError ){ 885a0217ba7Sdrh default: { 886a0217ba7Sdrh onError = OE_Abort; 887a0217ba7Sdrh /* Fall thru into the next case */ 888a0217ba7Sdrh } 8891c92853dSdrh case OE_Rollback: 8901c92853dSdrh case OE_Abort: 8911c92853dSdrh case OE_Fail: { 8924adee20fSdanielk1977 sqlite3VdbeOp3(v, OP_Halt, SQLITE_CONSTRAINT, onError, 893701a0aebSdrh "PRIMARY KEY must be unique", P3_STATIC); 8940ca3e24bSdrh break; 8950ca3e24bSdrh } 8965383ae5cSdrh case OE_Replace: { 8974adee20fSdanielk1977 sqlite3GenerateRowIndexDelete(pParse->db, v, pTab, base, 0); 8985383ae5cSdrh if( isUpdate ){ 8994adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Dup, nCol+hasTwoRecnos, 1); 9007cf6e4deSdrh sqlite3VdbeAddOp(v, OP_MoveGe, base, 0); 9015383ae5cSdrh } 9025383ae5cSdrh seenReplace = 1; 9035383ae5cSdrh break; 9045383ae5cSdrh } 9050ca3e24bSdrh case OE_Ignore: { 9065383ae5cSdrh assert( seenReplace==0 ); 9074adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Pop, nCol+1+hasTwoRecnos, 0); 9084adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Goto, 0, ignoreDest); 9090ca3e24bSdrh break; 9100ca3e24bSdrh } 9110ca3e24bSdrh } 9124adee20fSdanielk1977 contAddr = sqlite3VdbeCurrentAddr(v); 9134adee20fSdanielk1977 sqlite3VdbeChangeP2(v, jumpInst2, contAddr); 914f5905aa7Sdrh if( isUpdate ){ 9154adee20fSdanielk1977 sqlite3VdbeChangeP2(v, jumpInst1, contAddr); 9164adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Dup, nCol+1, 1); 9177cf6e4deSdrh sqlite3VdbeAddOp(v, OP_MoveGe, base, 0); 9180ca3e24bSdrh } 9190ca3e24bSdrh } 9200bd1f4eaSdrh 9210bd1f4eaSdrh /* Test all UNIQUE constraints by creating entries for each UNIQUE 9220bd1f4eaSdrh ** index and making sure that duplicate entries do not already exist. 9230bd1f4eaSdrh ** Add the new records to the indices as we go. 9240bd1f4eaSdrh */ 925b2fe7d8cSdrh extra = -1; 926b2fe7d8cSdrh for(iCur=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, iCur++){ 927b2fe7d8cSdrh if( aIdxUsed && aIdxUsed[iCur]==0 ) continue; /* Skip unused indices */ 9289cfcf5d4Sdrh extra++; 929b2fe7d8cSdrh 930b2fe7d8cSdrh /* Create a key for accessing the index entry */ 9314adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Dup, nCol+extra, 1); 9329cfcf5d4Sdrh for(i=0; i<pIdx->nColumn; i++){ 9339cfcf5d4Sdrh int idx = pIdx->aiColumn[i]; 9349cfcf5d4Sdrh if( idx==pTab->iPKey ){ 9354adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Dup, i+extra+nCol+1, 1); 9369cfcf5d4Sdrh }else{ 9374adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Dup, i+extra+nCol-idx, 1); 9389cfcf5d4Sdrh } 9399cfcf5d4Sdrh } 940ededfd5eSdanielk1977 jumpInst1 = sqlite3VdbeAddOp(v, OP_MakeRecord, pIdx->nColumn, (1<<24)); 941a37cdde0Sdanielk1977 sqlite3IndexAffinityStr(v, pIdx); 942b2fe7d8cSdrh 943b2fe7d8cSdrh /* Find out what action to take in case there is an indexing conflict */ 9449cfcf5d4Sdrh onError = pIdx->onError; 945b2fe7d8cSdrh if( onError==OE_None ) continue; /* pIdx is not a UNIQUE index */ 9469cfcf5d4Sdrh if( overrideError!=OE_Default ){ 9479cfcf5d4Sdrh onError = overrideError; 948a996e477Sdrh }else if( onError==OE_Default ){ 949a996e477Sdrh onError = OE_Abort; 9509cfcf5d4Sdrh } 9515383ae5cSdrh if( seenReplace ){ 9525383ae5cSdrh if( onError==OE_Ignore ) onError = OE_Replace; 9535383ae5cSdrh else if( onError==OE_Fail ) onError = OE_Abort; 9545383ae5cSdrh } 9555383ae5cSdrh 956b2fe7d8cSdrh 957b2fe7d8cSdrh /* Check to see if the new index entry will be unique */ 9584adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Dup, extra+nCol+1+hasTwoRecnos, 1); 9594adee20fSdanielk1977 jumpInst2 = sqlite3VdbeAddOp(v, OP_IsUnique, base+iCur+1, 0); 960b2fe7d8cSdrh 961b2fe7d8cSdrh /* Generate code that executes if the new index entry is not unique */ 9629cfcf5d4Sdrh switch( onError ){ 9631c92853dSdrh case OE_Rollback: 9641c92853dSdrh case OE_Abort: 9651c92853dSdrh case OE_Fail: { 96637ed48edSdrh int j, n1, n2; 96737ed48edSdrh char zErrMsg[200]; 96837ed48edSdrh strcpy(zErrMsg, pIdx->nColumn>1 ? "columns " : "column "); 96937ed48edSdrh n1 = strlen(zErrMsg); 97037ed48edSdrh for(j=0; j<pIdx->nColumn && n1<sizeof(zErrMsg)-30; j++){ 97137ed48edSdrh char *zCol = pTab->aCol[pIdx->aiColumn[j]].zName; 97237ed48edSdrh n2 = strlen(zCol); 97337ed48edSdrh if( j>0 ){ 97437ed48edSdrh strcpy(&zErrMsg[n1], ", "); 97537ed48edSdrh n1 += 2; 97637ed48edSdrh } 97737ed48edSdrh if( n1+n2>sizeof(zErrMsg)-30 ){ 97837ed48edSdrh strcpy(&zErrMsg[n1], "..."); 97937ed48edSdrh n1 += 3; 98037ed48edSdrh break; 98137ed48edSdrh }else{ 98237ed48edSdrh strcpy(&zErrMsg[n1], zCol); 98337ed48edSdrh n1 += n2; 98437ed48edSdrh } 98537ed48edSdrh } 98637ed48edSdrh strcpy(&zErrMsg[n1], 98737ed48edSdrh pIdx->nColumn>1 ? " are not unique" : " is not unique"); 9884adee20fSdanielk1977 sqlite3VdbeOp3(v, OP_Halt, SQLITE_CONSTRAINT, onError, zErrMsg, 0); 9899cfcf5d4Sdrh break; 9909cfcf5d4Sdrh } 9919cfcf5d4Sdrh case OE_Ignore: { 9920ca3e24bSdrh assert( seenReplace==0 ); 9934adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Pop, nCol+extra+3+hasTwoRecnos, 0); 9944adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Goto, 0, ignoreDest); 9959cfcf5d4Sdrh break; 9969cfcf5d4Sdrh } 9979cfcf5d4Sdrh case OE_Replace: { 9984adee20fSdanielk1977 sqlite3GenerateRowDelete(pParse->db, v, pTab, base, 0); 9999cfcf5d4Sdrh if( isUpdate ){ 10004adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Dup, nCol+extra+1+hasTwoRecnos, 1); 10017cf6e4deSdrh sqlite3VdbeAddOp(v, OP_MoveGe, base, 0); 10029cfcf5d4Sdrh } 10030ca3e24bSdrh seenReplace = 1; 10049cfcf5d4Sdrh break; 10059cfcf5d4Sdrh } 10060ca3e24bSdrh default: assert(0); 10079cfcf5d4Sdrh } 10084adee20fSdanielk1977 contAddr = sqlite3VdbeCurrentAddr(v); 1009ededfd5eSdanielk1977 assert( contAddr<(1<<24) ); 10100bd1f4eaSdrh #if NULL_DISTINCT_FOR_UNIQUE 1011ededfd5eSdanielk1977 sqlite3VdbeChangeP2(v, jumpInst1, contAddr | (1<<24)); 10120bd1f4eaSdrh #endif 10134adee20fSdanielk1977 sqlite3VdbeChangeP2(v, jumpInst2, contAddr); 10149cfcf5d4Sdrh } 10159cfcf5d4Sdrh } 10160ca3e24bSdrh 10170ca3e24bSdrh /* 10180ca3e24bSdrh ** This routine generates code to finish the INSERT or UPDATE operation 10194adee20fSdanielk1977 ** that was started by a prior call to sqlite3GenerateConstraintChecks. 10200ca3e24bSdrh ** The stack must contain keys for all active indices followed by data 10210ca3e24bSdrh ** and the recno for the new entry. This routine creates the new 10220ca3e24bSdrh ** entries in all indices and in the main table. 10230ca3e24bSdrh ** 1024b419a926Sdrh ** The arguments to this routine should be the same as the first six 10254adee20fSdanielk1977 ** arguments to sqlite3GenerateConstraintChecks. 10260ca3e24bSdrh */ 10274adee20fSdanielk1977 void sqlite3CompleteInsertion( 10280ca3e24bSdrh Parse *pParse, /* The parser context */ 10290ca3e24bSdrh Table *pTab, /* the table into which we are inserting */ 10300ca3e24bSdrh int base, /* Index of a read/write cursor pointing at pTab */ 10310ca3e24bSdrh char *aIdxUsed, /* Which indices are used. NULL means all are used */ 1032b419a926Sdrh int recnoChng, /* True if the record number will change */ 103370ce3f0cSdrh int isUpdate, /* True for UPDATE, False for INSERT */ 103470ce3f0cSdrh int newIdx /* Index of NEW table for triggers. -1 if none */ 10350ca3e24bSdrh ){ 10360ca3e24bSdrh int i; 10370ca3e24bSdrh Vdbe *v; 10380ca3e24bSdrh int nIdx; 10390ca3e24bSdrh Index *pIdx; 1040b28af71aSdanielk1977 int pik_flags; 10410ca3e24bSdrh 10424adee20fSdanielk1977 v = sqlite3GetVdbe(pParse); 10430ca3e24bSdrh assert( v!=0 ); 1044417be79cSdrh assert( pTab->pSelect==0 ); /* This table is not a VIEW */ 10450ca3e24bSdrh for(nIdx=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, nIdx++){} 10460ca3e24bSdrh for(i=nIdx-1; i>=0; i--){ 10470ca3e24bSdrh if( aIdxUsed && aIdxUsed[i]==0 ) continue; 10484adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_IdxPut, base+i+1, 0); 10490ca3e24bSdrh } 10504adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_MakeRecord, pTab->nCol, 0); 1051a37cdde0Sdanielk1977 sqlite3TableAffinityStr(v, pTab); 105270ce3f0cSdrh if( newIdx>=0 ){ 10534adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Dup, 1, 0); 10544adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Dup, 1, 0); 10554adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_PutIntKey, newIdx, 0); 105670ce3f0cSdrh } 10574794f735Sdrh if( pParse->nested ){ 10584794f735Sdrh pik_flags = 0; 10594794f735Sdrh }else{ 1060b28af71aSdanielk1977 pik_flags = (OPFLAG_NCHANGE|(isUpdate?0:OPFLAG_LASTROWID)); 10614794f735Sdrh } 1062b28af71aSdanielk1977 sqlite3VdbeAddOp(v, OP_PutIntKey, base, pik_flags); 1063b28af71aSdanielk1977 1064b419a926Sdrh if( isUpdate && recnoChng ){ 10654adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Pop, 1, 0); 10660ca3e24bSdrh } 10670ca3e24bSdrh } 1068cd44690aSdrh 1069cd44690aSdrh /* 1070290c1948Sdrh ** Generate code that will open cursors for a table and for all 1071cd44690aSdrh ** indices of that table. The "base" parameter is the cursor number used 1072cd44690aSdrh ** for the table. Indices are opened on subsequent cursors. 1073cd44690aSdrh */ 1074290c1948Sdrh void sqlite3OpenTableAndIndices( 1075290c1948Sdrh Parse *pParse, /* Parsing context */ 1076290c1948Sdrh Table *pTab, /* Table to be opened */ 1077290c1948Sdrh int base, /* Cursor number assigned to the table */ 1078290c1948Sdrh int op /* OP_OpenRead or OP_OpenWrite */ 1079290c1948Sdrh ){ 1080cd44690aSdrh int i; 1081cd44690aSdrh Index *pIdx; 10824adee20fSdanielk1977 Vdbe *v = sqlite3GetVdbe(pParse); 1083cd44690aSdrh assert( v!=0 ); 10844adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Integer, pTab->iDb, 0); 1085290c1948Sdrh sqlite3VdbeAddOp(v, op, base, pTab->tnum); 1086ad6d9460Sdrh VdbeComment((v, "# %s", pTab->zName)); 1087b4964b72Sdanielk1977 sqlite3VdbeAddOp(v, OP_SetNumColumns, base, pTab->nCol); 1088cd44690aSdrh for(i=1, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){ 10894adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Integer, pIdx->iDb, 0); 1090290c1948Sdrh sqlite3VdbeOp3(v, op, i+base, pIdx->tnum, 1091d3d39e93Sdrh (char*)&pIdx->keyInfo, P3_KEYINFO); 1092cd44690aSdrh } 1093290c1948Sdrh if( pParse->nTab<=base+i ){ 1094290c1948Sdrh pParse->nTab = base+i; 1095290c1948Sdrh } 1096cd44690aSdrh } 1097