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*001bbcbbSdrh ** $Id: insert.c,v 1.73 2003/03/19 03:14:01 drh Exp $ 16cce7d176Sdrh */ 17cce7d176Sdrh #include "sqliteInt.h" 18cce7d176Sdrh 19cce7d176Sdrh /* 201ccde15dSdrh ** This routine is call to handle SQL of the following forms: 21cce7d176Sdrh ** 22cce7d176Sdrh ** insert into TABLE (IDLIST) values(EXPRLIST) 231ccde15dSdrh ** insert into TABLE (IDLIST) select 24cce7d176Sdrh ** 251ccde15dSdrh ** The IDLIST following the table name is always optional. If omitted, 261ccde15dSdrh ** then a list of all columns for the table is substituted. The IDLIST 27967e8b73Sdrh ** appears in the pColumn parameter. pColumn is NULL if IDLIST is omitted. 281ccde15dSdrh ** 291ccde15dSdrh ** The pList parameter holds EXPRLIST in the first form of the INSERT 301ccde15dSdrh ** statement above, and pSelect is NULL. For the second form, pList is 311ccde15dSdrh ** NULL and pSelect is a pointer to the select statement used to generate 321ccde15dSdrh ** data for the insert. 33142e30dfSdrh ** 34142e30dfSdrh ** The code generated follows one of three templates. For a simple 35142e30dfSdrh ** select with data coming from a VALUES clause, the code executes 36142e30dfSdrh ** once straight down through. The template looks like this: 37142e30dfSdrh ** 38142e30dfSdrh ** open write cursor to <table> and its indices 39142e30dfSdrh ** puts VALUES clause expressions onto the stack 40142e30dfSdrh ** write the resulting record into <table> 41142e30dfSdrh ** cleanup 42142e30dfSdrh ** 43142e30dfSdrh ** If the statement is of the form 44142e30dfSdrh ** 45142e30dfSdrh ** INSERT INTO <table> SELECT ... 46142e30dfSdrh ** 47142e30dfSdrh ** And the SELECT clause does not read from <table> at any time, then 48142e30dfSdrh ** the generated code follows this template: 49142e30dfSdrh ** 50142e30dfSdrh ** goto B 51142e30dfSdrh ** A: setup for the SELECT 52142e30dfSdrh ** loop over the tables in the SELECT 53142e30dfSdrh ** gosub C 54142e30dfSdrh ** end loop 55142e30dfSdrh ** cleanup after the SELECT 56142e30dfSdrh ** goto D 57142e30dfSdrh ** B: open write cursor to <table> and its indices 58142e30dfSdrh ** goto A 59142e30dfSdrh ** C: insert the select result into <table> 60142e30dfSdrh ** return 61142e30dfSdrh ** D: cleanup 62142e30dfSdrh ** 63142e30dfSdrh ** The third template is used if the insert statement takes its 64142e30dfSdrh ** values from a SELECT but the data is being inserted into a table 65142e30dfSdrh ** that is also read as part of the SELECT. In the third form, 66142e30dfSdrh ** we have to use a intermediate table to store the results of 67142e30dfSdrh ** the select. The template is like this: 68142e30dfSdrh ** 69142e30dfSdrh ** goto B 70142e30dfSdrh ** A: setup for the SELECT 71142e30dfSdrh ** loop over the tables in the SELECT 72142e30dfSdrh ** gosub C 73142e30dfSdrh ** end loop 74142e30dfSdrh ** cleanup after the SELECT 75142e30dfSdrh ** goto D 76142e30dfSdrh ** C: insert the select result into the intermediate table 77142e30dfSdrh ** return 78142e30dfSdrh ** B: open a cursor to an intermediate table 79142e30dfSdrh ** goto A 80142e30dfSdrh ** D: open write cursor to <table> and its indices 81142e30dfSdrh ** loop over the intermediate table 82142e30dfSdrh ** transfer values form intermediate table into <table> 83142e30dfSdrh ** end the loop 84142e30dfSdrh ** cleanup 85cce7d176Sdrh */ 86cce7d176Sdrh void sqliteInsert( 87cce7d176Sdrh Parse *pParse, /* Parser context */ 88cce7d176Sdrh Token *pTableName, /* Name of table into which we are inserting */ 89cce7d176Sdrh ExprList *pList, /* List of values to be inserted */ 905974a30fSdrh Select *pSelect, /* A SELECT statement to use as the data source */ 919cfcf5d4Sdrh IdList *pColumn, /* Column names corresponding to IDLIST. */ 929cfcf5d4Sdrh int onError /* How to handle constraint errors */ 93cce7d176Sdrh ){ 945974a30fSdrh Table *pTab; /* The table to insert into */ 95c3f9bad2Sdanielk1977 char *zTab = 0; /* Name of the table into which we are inserting */ 965974a30fSdrh int i, j, idx; /* Loop counters */ 975974a30fSdrh Vdbe *v; /* Generate code into this virtual machine */ 985974a30fSdrh Index *pIdx; /* For looping over indices of the table */ 99967e8b73Sdrh int nColumn; /* Number of columns in the data */ 1005974a30fSdrh int base; /* First available cursor */ 1015974a30fSdrh int iCont, iBreak; /* Beginning and end of the loop over srcTab */ 102ecdc7530Sdrh sqlite *db; /* The main database structure */ 1034a32431cSdrh int keyColumn = -1; /* Column that is the INTEGER PRIMARY KEY */ 1040ca3e24bSdrh int endOfLoop; /* Label for the end of the insertion loop */ 105142e30dfSdrh int useTempTable; /* Store SELECT results in intermediate table */ 106142e30dfSdrh int srcTab; /* Data comes from this temporary cursor if >=0 */ 107142e30dfSdrh int iSelectLoop; /* Address of code that implements the SELECT */ 108142e30dfSdrh int iCleanup; /* Address of the cleanup code */ 109142e30dfSdrh int iInsertBlock; /* Address of the subroutine used to insert data */ 110142e30dfSdrh int iCntMem; /* Memory cell used for the row counter */ 111cce7d176Sdrh 112c3f9bad2Sdanielk1977 int row_triggers_exist = 0; /* True if there are FOR EACH ROW triggers */ 113c3f9bad2Sdanielk1977 int newIdx = -1; 114c3f9bad2Sdanielk1977 115daffd0e5Sdrh if( pParse->nErr || sqlite_malloc_failed ) goto insert_cleanup; 116ecdc7530Sdrh db = pParse->db; 117daffd0e5Sdrh 1181ccde15dSdrh /* Locate the table into which we will be inserting new information. 1191ccde15dSdrh */ 120cce7d176Sdrh zTab = sqliteTableNameFromToken(pTableName); 121daffd0e5Sdrh if( zTab==0 ) goto insert_cleanup; 122c3f9bad2Sdanielk1977 pTab = sqliteFindTable(pParse->db, zTab); 123c3f9bad2Sdanielk1977 if( pTab==0 ){ 124c3f9bad2Sdanielk1977 sqliteSetString(&pParse->zErrMsg, "no such table: ", zTab, 0); 125c3f9bad2Sdanielk1977 pParse->nErr++; 126c3f9bad2Sdanielk1977 goto insert_cleanup; 127c3f9bad2Sdanielk1977 } 128e5f9c644Sdrh if( sqliteAuthCheck(pParse, SQLITE_INSERT, pTab->zName, 0) ){ 1291962bda7Sdrh goto insert_cleanup; 1301962bda7Sdrh } 131c3f9bad2Sdanielk1977 132c3f9bad2Sdanielk1977 /* Ensure that: 133c3f9bad2Sdanielk1977 * (a) the table is not read-only, 134c3f9bad2Sdanielk1977 * (b) that if it is a view then ON INSERT triggers exist 135c3f9bad2Sdanielk1977 */ 136c3f9bad2Sdanielk1977 row_triggers_exist = 137c3f9bad2Sdanielk1977 sqliteTriggersExist(pParse, pTab->pTrigger, TK_INSERT, 138c3f9bad2Sdanielk1977 TK_BEFORE, TK_ROW, 0) || 139c3f9bad2Sdanielk1977 sqliteTriggersExist(pParse, pTab->pTrigger, TK_INSERT, TK_AFTER, TK_ROW, 0); 140c3f9bad2Sdanielk1977 if( pTab->readOnly || (pTab->pSelect && !row_triggers_exist) ){ 141c3f9bad2Sdanielk1977 sqliteSetString(&pParse->zErrMsg, 142c3f9bad2Sdanielk1977 pTab->pSelect ? "view " : "table ", 143c3f9bad2Sdanielk1977 zTab, 144c3f9bad2Sdanielk1977 " may not be modified", 0); 145c3f9bad2Sdanielk1977 pParse->nErr++; 146c3f9bad2Sdanielk1977 goto insert_cleanup; 147c3f9bad2Sdanielk1977 } 148cce7d176Sdrh sqliteFree(zTab); 149c3f9bad2Sdanielk1977 zTab = 0; 150c3f9bad2Sdanielk1977 151a76b5dfcSdrh if( pTab==0 ) goto insert_cleanup; 1521ccde15dSdrh 153f573c99bSdrh /* If pTab is really a view, make sure it has been initialized. 154f573c99bSdrh */ 155f573c99bSdrh if( pTab->pSelect ){ 156f573c99bSdrh if( sqliteViewGetColumnNames(pParse, pTab) ){ 157f573c99bSdrh goto insert_cleanup; 158f573c99bSdrh } 159f573c99bSdrh } 160f573c99bSdrh 1611ccde15dSdrh /* Allocate a VDBE 1621ccde15dSdrh */ 163d8bc7086Sdrh v = sqliteGetVdbe(pParse); 1645974a30fSdrh if( v==0 ) goto insert_cleanup; 165cabb0819Sdrh sqliteBeginWriteOperation(pParse, pSelect || row_triggers_exist, 166cabb0819Sdrh !row_triggers_exist && pTab->isTemp); 1671ccde15dSdrh 168c3f9bad2Sdanielk1977 /* if there are row triggers, allocate a temp table for new.* references. */ 169f29ce559Sdanielk1977 if( row_triggers_exist ){ 170c3f9bad2Sdanielk1977 newIdx = pParse->nTab++; 171f29ce559Sdanielk1977 } 172c3f9bad2Sdanielk1977 1731ccde15dSdrh /* Figure out how many columns of data are supplied. If the data 174142e30dfSdrh ** is coming from a SELECT statement, then this step also generates 175142e30dfSdrh ** all the code to implement the SELECT statement and invoke a subroutine 176142e30dfSdrh ** to process each row of the result. (Template 2.) If the SELECT 177142e30dfSdrh ** statement uses the the table that is being inserted into, then the 178142e30dfSdrh ** subroutine is also coded here. That subroutine stores the SELECT 179142e30dfSdrh ** results in a temporary table. (Template 3.) 1801ccde15dSdrh */ 1815974a30fSdrh if( pSelect ){ 182142e30dfSdrh /* Data is coming from a SELECT. Generate code to implement that SELECT 183142e30dfSdrh */ 184142e30dfSdrh int rc, iInitCode; 185142e30dfSdrh int opCode; 186142e30dfSdrh iInitCode = sqliteVdbeAddOp(v, OP_Goto, 0, 0); 187142e30dfSdrh iSelectLoop = sqliteVdbeCurrentAddr(v); 188142e30dfSdrh iInsertBlock = sqliteVdbeMakeLabel(v); 189142e30dfSdrh rc = sqliteSelect(pParse, pSelect, SRT_Subroutine, iInsertBlock, 0,0,0); 190daffd0e5Sdrh if( rc || pParse->nErr || sqlite_malloc_failed ) goto insert_cleanup; 191142e30dfSdrh iCleanup = sqliteVdbeMakeLabel(v); 192142e30dfSdrh sqliteVdbeAddOp(v, OP_Goto, 0, iCleanup); 1935974a30fSdrh assert( pSelect->pEList ); 194967e8b73Sdrh nColumn = pSelect->pEList->nExpr; 195142e30dfSdrh 196142e30dfSdrh /* Set useTempTable to TRUE if the result of the SELECT statement 197142e30dfSdrh ** should be written into a temporary table. Set to FALSE if each 198142e30dfSdrh ** row of the SELECT can be written directly into the result table. 199142e30dfSdrh */ 200*001bbcbbSdrh opCode = pTab->isTemp ? OP_OpenTemp : OP_OpenRead; 201142e30dfSdrh useTempTable = row_triggers_exist || sqliteVdbeFindOp(v,opCode,pTab->tnum); 202142e30dfSdrh 203142e30dfSdrh if( useTempTable ){ 204142e30dfSdrh /* Generate the subroutine that SELECT calls to process each row of 205142e30dfSdrh ** the result. Store the result in a temporary table 206142e30dfSdrh */ 207142e30dfSdrh srcTab = pParse->nTab++; 208142e30dfSdrh sqliteVdbeResolveLabel(v, iInsertBlock); 209142e30dfSdrh sqliteVdbeAddOp(v, OP_MakeRecord, nColumn, 0); 210142e30dfSdrh sqliteVdbeAddOp(v, OP_NewRecno, srcTab, 0); 211142e30dfSdrh sqliteVdbeAddOp(v, OP_Pull, 1, 0); 212142e30dfSdrh sqliteVdbeAddOp(v, OP_PutIntKey, srcTab, 0); 213142e30dfSdrh sqliteVdbeAddOp(v, OP_Return, 0, 0); 214142e30dfSdrh 215142e30dfSdrh /* The following code runs first because the GOTO at the very top 216142e30dfSdrh ** of the program jumps to it. Create the temporary table, then jump 217142e30dfSdrh ** back up and execute the SELECT code above. 218142e30dfSdrh */ 219142e30dfSdrh sqliteVdbeChangeP2(v, iInitCode, sqliteVdbeCurrentAddr(v)); 220142e30dfSdrh sqliteVdbeAddOp(v, OP_OpenTemp, srcTab, 0); 221142e30dfSdrh sqliteVdbeAddOp(v, OP_Goto, 0, iSelectLoop); 222142e30dfSdrh sqliteVdbeResolveLabel(v, iCleanup); 2235974a30fSdrh }else{ 224142e30dfSdrh sqliteVdbeChangeP2(v, iInitCode, sqliteVdbeCurrentAddr(v)); 225142e30dfSdrh } 226142e30dfSdrh }else{ 227142e30dfSdrh /* This is the case if the data for the INSERT is coming from a VALUES 228142e30dfSdrh ** clause 229142e30dfSdrh */ 230ad3cab52Sdrh SrcList dummy; 231daffd0e5Sdrh assert( pList!=0 ); 2325974a30fSdrh srcTab = -1; 233142e30dfSdrh useTempTable = 0; 2345974a30fSdrh assert( pList ); 235967e8b73Sdrh nColumn = pList->nExpr; 236ad3cab52Sdrh dummy.nSrc = 0; 237e64e7b20Sdrh for(i=0; i<nColumn; i++){ 238832508b7Sdrh if( sqliteExprResolveIds(pParse, 0, &dummy, 0, pList->a[i].pExpr) ){ 239e64e7b20Sdrh goto insert_cleanup; 240e64e7b20Sdrh } 241b04a5d87Sdrh if( sqliteExprCheck(pParse, pList->a[i].pExpr, 0, 0) ){ 242b04a5d87Sdrh goto insert_cleanup; 243b04a5d87Sdrh } 244e64e7b20Sdrh } 2455974a30fSdrh } 2461ccde15dSdrh 2471ccde15dSdrh /* Make sure the number of columns in the source data matches the number 2481ccde15dSdrh ** of columns to be inserted into the table. 2491ccde15dSdrh */ 250967e8b73Sdrh if( pColumn==0 && nColumn!=pTab->nCol ){ 251cce7d176Sdrh char zNum1[30]; 252cce7d176Sdrh char zNum2[30]; 253967e8b73Sdrh sprintf(zNum1,"%d", nColumn); 254cce7d176Sdrh sprintf(zNum2,"%d", pTab->nCol); 255cce7d176Sdrh sqliteSetString(&pParse->zErrMsg, "table ", pTab->zName, 256cce7d176Sdrh " has ", zNum2, " columns but ", 257cce7d176Sdrh zNum1, " values were supplied", 0); 258cce7d176Sdrh pParse->nErr++; 259cce7d176Sdrh goto insert_cleanup; 260cce7d176Sdrh } 261967e8b73Sdrh if( pColumn!=0 && nColumn!=pColumn->nId ){ 262cce7d176Sdrh char zNum1[30]; 263cce7d176Sdrh char zNum2[30]; 264967e8b73Sdrh sprintf(zNum1,"%d", nColumn); 265967e8b73Sdrh sprintf(zNum2,"%d", pColumn->nId); 266cce7d176Sdrh sqliteSetString(&pParse->zErrMsg, zNum1, " values for ", 267cce7d176Sdrh zNum2, " columns", 0); 268cce7d176Sdrh pParse->nErr++; 269cce7d176Sdrh goto insert_cleanup; 270cce7d176Sdrh } 2711ccde15dSdrh 2721ccde15dSdrh /* If the INSERT statement included an IDLIST term, then make sure 2731ccde15dSdrh ** all elements of the IDLIST really are columns of the table and 2741ccde15dSdrh ** remember the column indices. 275c8392586Sdrh ** 276c8392586Sdrh ** If the table has an INTEGER PRIMARY KEY column and that column 277c8392586Sdrh ** is named in the IDLIST, then record in the keyColumn variable 278c8392586Sdrh ** the index into IDLIST of the primary key column. keyColumn is 279c8392586Sdrh ** the index of the primary key as it appears in IDLIST, not as 280c8392586Sdrh ** is appears in the original table. (The index of the primary 281c8392586Sdrh ** key in the original table is pTab->iPKey.) 2821ccde15dSdrh */ 283967e8b73Sdrh if( pColumn ){ 284967e8b73Sdrh for(i=0; i<pColumn->nId; i++){ 285967e8b73Sdrh pColumn->a[i].idx = -1; 286cce7d176Sdrh } 287967e8b73Sdrh for(i=0; i<pColumn->nId; i++){ 288cce7d176Sdrh for(j=0; j<pTab->nCol; j++){ 289967e8b73Sdrh if( sqliteStrICmp(pColumn->a[i].zName, pTab->aCol[j].zName)==0 ){ 290967e8b73Sdrh pColumn->a[i].idx = j; 2914a32431cSdrh if( j==pTab->iPKey ){ 2929aa028daSdrh keyColumn = i; 2934a32431cSdrh } 294cce7d176Sdrh break; 295cce7d176Sdrh } 296cce7d176Sdrh } 297cce7d176Sdrh if( j>=pTab->nCol ){ 298cce7d176Sdrh sqliteSetString(&pParse->zErrMsg, "table ", pTab->zName, 299967e8b73Sdrh " has no column named ", pColumn->a[i].zName, 0); 300cce7d176Sdrh pParse->nErr++; 301cce7d176Sdrh goto insert_cleanup; 302cce7d176Sdrh } 303cce7d176Sdrh } 304cce7d176Sdrh } 3051ccde15dSdrh 306aacc543eSdrh /* If there is no IDLIST term but the table has an integer primary 307c8392586Sdrh ** key, the set the keyColumn variable to the primary key column index 308c8392586Sdrh ** in the original table definition. 3094a32431cSdrh */ 3104a32431cSdrh if( pColumn==0 ){ 3114a32431cSdrh keyColumn = pTab->iPKey; 3124a32431cSdrh } 3134a32431cSdrh 314142e30dfSdrh /* Open the temp table for FOR EACH ROW triggers 315142e30dfSdrh */ 316f29ce559Sdanielk1977 if( row_triggers_exist ){ 317c3f9bad2Sdanielk1977 sqliteVdbeAddOp(v, OP_OpenTemp, newIdx, 0); 318f29ce559Sdanielk1977 } 319c3f9bad2Sdanielk1977 320c3f9bad2Sdanielk1977 /* Initialize the count of rows to be inserted 3211ccde15dSdrh */ 322142e30dfSdrh if( db->flags & SQLITE_CountRows ){ 323142e30dfSdrh iCntMem = pParse->nMem++; 324142e30dfSdrh sqliteVdbeAddOp(v, OP_Integer, 0, 0); 325142e30dfSdrh sqliteVdbeAddOp(v, OP_MemStore, iCntMem, 1); 326c3f9bad2Sdanielk1977 } 327c3f9bad2Sdanielk1977 328c3f9bad2Sdanielk1977 /* Open tables and indices if there are no row triggers */ 329c3f9bad2Sdanielk1977 if( !row_triggers_exist ){ 3305974a30fSdrh base = pParse->nTab; 331*001bbcbbSdrh sqliteVdbeAddOp(v, OP_Integer, pTab->isTemp, 0); 332*001bbcbbSdrh sqliteVdbeAddOp(v, OP_OpenWrite, base, pTab->tnum); 33399fcd718Sdrh sqliteVdbeChangeP3(v, -1, pTab->zName, P3_STATIC); 334bed8690fSdrh for(idx=1, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, idx++){ 335*001bbcbbSdrh sqliteVdbeAddOp(v, OP_Integer, pTab->isTemp, 0); 336*001bbcbbSdrh sqliteVdbeAddOp(v, OP_OpenWrite, idx+base, pIdx->tnum); 33799fcd718Sdrh sqliteVdbeChangeP3(v, -1, pIdx->zName, P3_STATIC); 3385974a30fSdrh } 339832508b7Sdrh pParse->nTab += idx; 340feeb1394Sdrh } 341feeb1394Sdrh 342142e30dfSdrh /* If the data source is a temporary table, then we have to create 3431ccde15dSdrh ** a loop because there might be multiple rows of data. If the data 344142e30dfSdrh ** source is a subroutine call from the SELECT statement, then we need 345142e30dfSdrh ** to launch the SELECT statement processing. 3461ccde15dSdrh */ 347142e30dfSdrh if( useTempTable ){ 3485974a30fSdrh iBreak = sqliteVdbeMakeLabel(v); 3496b56344dSdrh sqliteVdbeAddOp(v, OP_Rewind, srcTab, iBreak); 3506b56344dSdrh iCont = sqliteVdbeCurrentAddr(v); 351142e30dfSdrh }else if( pSelect ){ 352142e30dfSdrh sqliteVdbeAddOp(v, OP_Goto, 0, iSelectLoop); 353142e30dfSdrh sqliteVdbeResolveLabel(v, iInsertBlock); 354bed8690fSdrh } 3551ccde15dSdrh 3566f34903eSdanielk1977 endOfLoop = sqliteVdbeMakeLabel(v); 357c3f9bad2Sdanielk1977 if( row_triggers_exist ){ 358c3f9bad2Sdanielk1977 359c3f9bad2Sdanielk1977 /* build the new.* reference row */ 360c3f9bad2Sdanielk1977 sqliteVdbeAddOp(v, OP_Integer, 13, 0); 361c3f9bad2Sdanielk1977 for(i=0; i<pTab->nCol; i++){ 362c3f9bad2Sdanielk1977 if( pColumn==0 ){ 363c3f9bad2Sdanielk1977 j = i; 364c3f9bad2Sdanielk1977 }else{ 365c3f9bad2Sdanielk1977 for(j=0; j<pColumn->nId; j++){ 366c3f9bad2Sdanielk1977 if( pColumn->a[j].idx==i ) break; 367c3f9bad2Sdanielk1977 } 368c3f9bad2Sdanielk1977 } 369c3f9bad2Sdanielk1977 if( pColumn && j>=pColumn->nId ){ 370c3f9bad2Sdanielk1977 sqliteVdbeAddOp(v, OP_String, 0, 0); 371c3f9bad2Sdanielk1977 sqliteVdbeChangeP3(v, -1, pTab->aCol[i].zDflt, P3_STATIC); 372142e30dfSdrh }else if( useTempTable ){ 373c3f9bad2Sdanielk1977 sqliteVdbeAddOp(v, OP_Column, srcTab, j); 374142e30dfSdrh }else if( pSelect ){ 375142e30dfSdrh sqliteVdbeAddOp(v, OP_Dup, nColumn-j-1, 1); 376c3f9bad2Sdanielk1977 }else{ 377c3f9bad2Sdanielk1977 sqliteExprCode(pParse, pList->a[j].pExpr); 378c3f9bad2Sdanielk1977 } 379c3f9bad2Sdanielk1977 } 380c3f9bad2Sdanielk1977 sqliteVdbeAddOp(v, OP_MakeRecord, pTab->nCol, 0); 381c3f9bad2Sdanielk1977 sqliteVdbeAddOp(v, OP_PutIntKey, newIdx, 0); 382c3f9bad2Sdanielk1977 sqliteVdbeAddOp(v, OP_Rewind, newIdx, 0); 383c3f9bad2Sdanielk1977 384c3f9bad2Sdanielk1977 /* Fire BEFORE triggers */ 385f29ce559Sdanielk1977 if( sqliteCodeRowTrigger(pParse, TK_INSERT, 0, TK_BEFORE, pTab, newIdx, -1, 3866f34903eSdanielk1977 onError, endOfLoop) ){ 387f29ce559Sdanielk1977 goto insert_cleanup; 388f29ce559Sdanielk1977 } 389c3f9bad2Sdanielk1977 390c3f9bad2Sdanielk1977 /* Open the tables and indices for the INSERT */ 391c3f9bad2Sdanielk1977 if( !pTab->pSelect ){ 392c3f9bad2Sdanielk1977 base = pParse->nTab; 393*001bbcbbSdrh sqliteVdbeAddOp(v, OP_Integer, pTab->isTemp, 0); 394*001bbcbbSdrh sqliteVdbeAddOp(v, OP_OpenWrite, base, pTab->tnum); 395c3f9bad2Sdanielk1977 sqliteVdbeChangeP3(v, -1, pTab->zName, P3_STATIC); 396c3f9bad2Sdanielk1977 for(idx=1, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, idx++){ 397*001bbcbbSdrh sqliteVdbeAddOp(v, OP_Integer, pTab->isTemp, 0); 398*001bbcbbSdrh sqliteVdbeAddOp(v, OP_OpenWrite, idx+base, pIdx->tnum); 399c3f9bad2Sdanielk1977 sqliteVdbeChangeP3(v, -1, pIdx->zName, P3_STATIC); 400c3f9bad2Sdanielk1977 } 401c3f9bad2Sdanielk1977 pParse->nTab += idx; 402c3f9bad2Sdanielk1977 } 403c3f9bad2Sdanielk1977 } 404c3f9bad2Sdanielk1977 4054a32431cSdrh /* Push the record number for the new entry onto the stack. The 4064a32431cSdrh ** record number is a randomly generate integer created by NewRecno 4074a32431cSdrh ** except when the table has an INTEGER PRIMARY KEY column, in which 408b419a926Sdrh ** case the record number is the same as that column. 4091ccde15dSdrh */ 410c3f9bad2Sdanielk1977 if( !pTab->pSelect ){ 4114a32431cSdrh if( keyColumn>=0 ){ 412142e30dfSdrh if( useTempTable ){ 4134a32431cSdrh sqliteVdbeAddOp(v, OP_Column, srcTab, keyColumn); 414142e30dfSdrh }else if( pSelect ){ 415142e30dfSdrh sqliteVdbeAddOp(v, OP_Dup, nColumn - keyColumn - 1, 1); 4164a32431cSdrh }else{ 4174a32431cSdrh sqliteExprCode(pParse, pList->a[keyColumn].pExpr); 41827a32783Sdrh } 419e1e68f49Sdrh /* If the PRIMARY KEY expression is NULL, then use OP_NewRecno 420e1e68f49Sdrh ** to generate a unique primary key value. 421e1e68f49Sdrh */ 422f5905aa7Sdrh sqliteVdbeAddOp(v, OP_NotNull, -1, sqliteVdbeCurrentAddr(v)+3); 423e1e68f49Sdrh sqliteVdbeAddOp(v, OP_Pop, 1, 0); 424e1e68f49Sdrh sqliteVdbeAddOp(v, OP_NewRecno, base, 0); 4258aff1015Sdrh sqliteVdbeAddOp(v, OP_MustBeInt, 0, 0); 4264a32431cSdrh }else{ 42799fcd718Sdrh sqliteVdbeAddOp(v, OP_NewRecno, base, 0); 4284a32431cSdrh } 4294a32431cSdrh 430aacc543eSdrh /* Push onto the stack, data for all columns of the new entry, beginning 4314a32431cSdrh ** with the first column. 4324a32431cSdrh */ 433cce7d176Sdrh for(i=0; i<pTab->nCol; i++){ 4344a32431cSdrh if( i==pTab->iPKey ){ 4354a32431cSdrh /* The value of the INTEGER PRIMARY KEY column is always a NULL. 436aacc543eSdrh ** Whenever this column is read, the record number will be substituted 437aacc543eSdrh ** in its place. So will fill this column with a NULL to avoid 438aacc543eSdrh ** taking up data space with information that will never be used. */ 4394a32431cSdrh sqliteVdbeAddOp(v, OP_String, 0, 0); 4404a32431cSdrh continue; 4414a32431cSdrh } 442967e8b73Sdrh if( pColumn==0 ){ 443cce7d176Sdrh j = i; 444cce7d176Sdrh }else{ 445967e8b73Sdrh for(j=0; j<pColumn->nId; j++){ 446967e8b73Sdrh if( pColumn->a[j].idx==i ) break; 447cce7d176Sdrh } 448cce7d176Sdrh } 449967e8b73Sdrh if( pColumn && j>=pColumn->nId ){ 45099fcd718Sdrh sqliteVdbeAddOp(v, OP_String, 0, 0); 45199fcd718Sdrh sqliteVdbeChangeP3(v, -1, pTab->aCol[i].zDflt, P3_STATIC); 452142e30dfSdrh }else if( useTempTable ){ 45324e97df9Sdrh sqliteVdbeAddOp(v, OP_Column, srcTab, j); 454142e30dfSdrh }else if( pSelect ){ 455142e30dfSdrh sqliteVdbeAddOp(v, OP_Dup, i+nColumn-j, 1); 456cce7d176Sdrh }else{ 457cce7d176Sdrh sqliteExprCode(pParse, pList->a[j].pExpr); 458cce7d176Sdrh } 459cce7d176Sdrh } 4601ccde15dSdrh 4610ca3e24bSdrh /* Generate code to check constraints and generate index keys and 4620ca3e24bSdrh ** do the insertion. 4634a32431cSdrh */ 464b419a926Sdrh sqliteGenerateConstraintChecks(pParse, pTab, base, 0,0,0,onError,endOfLoop); 465b419a926Sdrh sqliteCompleteInsertion(pParse, pTab, base, 0,0,0); 4661bee3d7bSdrh 467feeb1394Sdrh /* Update the count of rows that are inserted 4681bee3d7bSdrh */ 469142e30dfSdrh if( (db->flags & SQLITE_CountRows)!=0 ){ 470142e30dfSdrh sqliteVdbeAddOp(v, OP_MemIncr, iCntMem, 0); 4711bee3d7bSdrh } 472c3f9bad2Sdanielk1977 } 473c3f9bad2Sdanielk1977 474c3f9bad2Sdanielk1977 if( row_triggers_exist ){ 475c3f9bad2Sdanielk1977 /* Close all tables opened */ 476c3f9bad2Sdanielk1977 if( !pTab->pSelect ){ 477c3f9bad2Sdanielk1977 sqliteVdbeAddOp(v, OP_Close, base, 0); 478c3f9bad2Sdanielk1977 for(idx=1, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, idx++){ 479c3f9bad2Sdanielk1977 sqliteVdbeAddOp(v, OP_Close, idx+base, 0); 480c3f9bad2Sdanielk1977 } 481c3f9bad2Sdanielk1977 } 482c3f9bad2Sdanielk1977 483c3f9bad2Sdanielk1977 /* Code AFTER triggers */ 484f29ce559Sdanielk1977 if( sqliteCodeRowTrigger(pParse, TK_INSERT, 0, TK_AFTER, pTab, newIdx, -1, 4856f34903eSdanielk1977 onError, endOfLoop) ){ 486f29ce559Sdanielk1977 goto insert_cleanup; 487f29ce559Sdanielk1977 } 488c3f9bad2Sdanielk1977 } 4891bee3d7bSdrh 4901ccde15dSdrh /* The bottom of the loop, if the data source is a SELECT statement 4911ccde15dSdrh */ 4920ca3e24bSdrh sqliteVdbeResolveLabel(v, endOfLoop); 493142e30dfSdrh if( useTempTable ){ 4946b56344dSdrh sqliteVdbeAddOp(v, OP_Next, srcTab, iCont); 49599fcd718Sdrh sqliteVdbeResolveLabel(v, iBreak); 4966b56344dSdrh sqliteVdbeAddOp(v, OP_Close, srcTab, 0); 497142e30dfSdrh }else if( pSelect ){ 498142e30dfSdrh sqliteVdbeAddOp(v, OP_Pop, nColumn, 0); 499142e30dfSdrh sqliteVdbeAddOp(v, OP_Return, 0, 0); 500142e30dfSdrh sqliteVdbeResolveLabel(v, iCleanup); 5016b56344dSdrh } 502c3f9bad2Sdanielk1977 503c3f9bad2Sdanielk1977 if( !row_triggers_exist ){ 504c3f9bad2Sdanielk1977 /* Close all tables opened */ 5056b56344dSdrh sqliteVdbeAddOp(v, OP_Close, base, 0); 5066b56344dSdrh for(idx=1, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, idx++){ 5076b56344dSdrh sqliteVdbeAddOp(v, OP_Close, idx+base, 0); 508cce7d176Sdrh } 509c3f9bad2Sdanielk1977 } 510c3f9bad2Sdanielk1977 5111c92853dSdrh sqliteEndWriteOperation(pParse); 5125e00f6c7Sdrh 5131bee3d7bSdrh /* 5141bee3d7bSdrh ** Return the number of rows inserted. 5151bee3d7bSdrh */ 516142e30dfSdrh if( db->flags & SQLITE_CountRows ){ 5171bee3d7bSdrh sqliteVdbeAddOp(v, OP_ColumnName, 0, 0); 5181bee3d7bSdrh sqliteVdbeChangeP3(v, -1, "rows inserted", P3_STATIC); 519142e30dfSdrh sqliteVdbeAddOp(v, OP_MemLoad, iCntMem, 0); 5201bee3d7bSdrh sqliteVdbeAddOp(v, OP_Callback, 1, 0); 5211bee3d7bSdrh } 522cce7d176Sdrh 523cce7d176Sdrh insert_cleanup: 5245974a30fSdrh if( pList ) sqliteExprListDelete(pList); 5255974a30fSdrh if( pSelect ) sqliteSelectDelete(pSelect); 526c3f9bad2Sdanielk1977 if ( zTab ) sqliteFree(zTab); 527967e8b73Sdrh sqliteIdListDelete(pColumn); 528cce7d176Sdrh } 5299cfcf5d4Sdrh 5309cfcf5d4Sdrh /* 5319cfcf5d4Sdrh ** Generate code to do a constraint check prior to an INSERT or an UPDATE. 5329cfcf5d4Sdrh ** 5339cfcf5d4Sdrh ** When this routine is called, the stack contains (from bottom to top) 5340ca3e24bSdrh ** the following values: 5350ca3e24bSdrh ** 536b419a926Sdrh ** 1. The recno of the row to be updated before it is updated. This 537b419a926Sdrh ** value is omitted unless we are doing an UPDATE that involves a 538b419a926Sdrh ** change to the record number. 5390ca3e24bSdrh ** 540b419a926Sdrh ** 2. The recno of the row after the update. 5410ca3e24bSdrh ** 5420ca3e24bSdrh ** 3. The data in the first column of the entry after the update. 5430ca3e24bSdrh ** 5440ca3e24bSdrh ** i. Data from middle columns... 5450ca3e24bSdrh ** 5460ca3e24bSdrh ** N. The data in the last column of the entry after the update. 5470ca3e24bSdrh ** 548b419a926Sdrh ** The old recno shown as entry (1) above is omitted unless both isUpdate 5491c92853dSdrh ** and recnoChng are 1. isUpdate is true for UPDATEs and false for 5501c92853dSdrh ** INSERTs and recnoChng is true if the record number is being changed. 5510ca3e24bSdrh ** 5520ca3e24bSdrh ** The code generated by this routine pushes additional entries onto 5530ca3e24bSdrh ** the stack which are the keys for new index entries for the new record. 5540ca3e24bSdrh ** The order of index keys is the same as the order of the indices on 5550ca3e24bSdrh ** the pTable->pIndex list. A key is only created for index i if 5560ca3e24bSdrh ** aIdxUsed!=0 and aIdxUsed[i]!=0. 5579cfcf5d4Sdrh ** 5589cfcf5d4Sdrh ** This routine also generates code to check constraints. NOT NULL, 5599cfcf5d4Sdrh ** CHECK, and UNIQUE constraints are all checked. If a constraint fails, 5601c92853dSdrh ** then the appropriate action is performed. There are five possible 5611c92853dSdrh ** actions: ROLLBACK, ABORT, FAIL, REPLACE, and IGNORE. 5629cfcf5d4Sdrh ** 5639cfcf5d4Sdrh ** Constraint type Action What Happens 5649cfcf5d4Sdrh ** --------------- ---------- ---------------------------------------- 5651c92853dSdrh ** any ROLLBACK The current transaction is rolled back and 5669cfcf5d4Sdrh ** sqlite_exec() returns immediately with a 5679cfcf5d4Sdrh ** return code of SQLITE_CONSTRAINT. 5689cfcf5d4Sdrh ** 5691c92853dSdrh ** any ABORT Back out changes from the current command 5701c92853dSdrh ** only (do not do a complete rollback) then 5711c92853dSdrh ** cause sqlite_exec() to return immediately 5721c92853dSdrh ** with SQLITE_CONSTRAINT. 5731c92853dSdrh ** 5741c92853dSdrh ** any FAIL Sqlite_exec() returns immediately with a 5751c92853dSdrh ** return code of SQLITE_CONSTRAINT. The 5761c92853dSdrh ** transaction is not rolled back and any 5771c92853dSdrh ** prior changes are retained. 5781c92853dSdrh ** 5799cfcf5d4Sdrh ** any IGNORE The record number and data is popped from 5809cfcf5d4Sdrh ** the stack and there is an immediate jump 5819cfcf5d4Sdrh ** to label ignoreDest. 5829cfcf5d4Sdrh ** 5839cfcf5d4Sdrh ** NOT NULL REPLACE The NULL value is replace by the default 5849cfcf5d4Sdrh ** value for that column. If the default value 5859cfcf5d4Sdrh ** is NULL, the action is the same as ABORT. 5869cfcf5d4Sdrh ** 5879cfcf5d4Sdrh ** UNIQUE REPLACE The other row that conflicts with the row 5889cfcf5d4Sdrh ** being inserted is removed. 5899cfcf5d4Sdrh ** 5909cfcf5d4Sdrh ** CHECK REPLACE Illegal. The results in an exception. 5919cfcf5d4Sdrh ** 5921c92853dSdrh ** Which action to take is determined by the overrideError parameter. 5931c92853dSdrh ** Or if overrideError==OE_Default, then the pParse->onError parameter 5941c92853dSdrh ** is used. Or if pParse->onError==OE_Default then the onError value 5951c92853dSdrh ** for the constraint is used. 5969cfcf5d4Sdrh ** 597aaab5725Sdrh ** The calling routine must open a read/write cursor for pTab with 5989cfcf5d4Sdrh ** cursor number "base". All indices of pTab must also have open 5999cfcf5d4Sdrh ** read/write cursors with cursor number base+i for the i-th cursor. 6009cfcf5d4Sdrh ** Except, if there is no possibility of a REPLACE action then 6019cfcf5d4Sdrh ** cursors do not need to be open for indices where aIdxUsed[i]==0. 6029cfcf5d4Sdrh ** 6039cfcf5d4Sdrh ** If the isUpdate flag is true, it means that the "base" cursor is 6049cfcf5d4Sdrh ** initially pointing to an entry that is being updated. The isUpdate 6059cfcf5d4Sdrh ** flag causes extra code to be generated so that the "base" cursor 6069cfcf5d4Sdrh ** is still pointing at the same entry after the routine returns. 6079cfcf5d4Sdrh ** Without the isUpdate flag, the "base" cursor might be moved. 6089cfcf5d4Sdrh */ 6099cfcf5d4Sdrh void sqliteGenerateConstraintChecks( 6109cfcf5d4Sdrh Parse *pParse, /* The parser context */ 6119cfcf5d4Sdrh Table *pTab, /* the table into which we are inserting */ 6129cfcf5d4Sdrh int base, /* Index of a read/write cursor pointing at pTab */ 6139cfcf5d4Sdrh char *aIdxUsed, /* Which indices are used. NULL means all are used */ 6140ca3e24bSdrh int recnoChng, /* True if the record number will change */ 615b419a926Sdrh int isUpdate, /* True for UPDATE, False for INSERT */ 6169cfcf5d4Sdrh int overrideError, /* Override onError to this if not OE_Default */ 617b419a926Sdrh int ignoreDest /* Jump to this label on an OE_Ignore resolution */ 6189cfcf5d4Sdrh ){ 6199cfcf5d4Sdrh int i; 6209cfcf5d4Sdrh Vdbe *v; 6219cfcf5d4Sdrh int nCol; 6229cfcf5d4Sdrh int onError; 6239cfcf5d4Sdrh int addr; 6249cfcf5d4Sdrh int extra; 6250ca3e24bSdrh int iCur; 6260ca3e24bSdrh Index *pIdx; 6270ca3e24bSdrh int seenReplace = 0; 628f5905aa7Sdrh int jumpInst1, jumpInst2; 6290ca3e24bSdrh int contAddr; 630b419a926Sdrh int hasTwoRecnos = (isUpdate && recnoChng); 6319cfcf5d4Sdrh 6329cfcf5d4Sdrh v = sqliteGetVdbe(pParse); 6339cfcf5d4Sdrh assert( v!=0 ); 634417be79cSdrh assert( pTab->pSelect==0 ); /* This table is not a VIEW */ 6359cfcf5d4Sdrh nCol = pTab->nCol; 6369cfcf5d4Sdrh 6379cfcf5d4Sdrh /* Test all NOT NULL constraints. 6389cfcf5d4Sdrh */ 6399cfcf5d4Sdrh for(i=0; i<nCol; i++){ 6400ca3e24bSdrh if( i==pTab->iPKey ){ 6410ca3e24bSdrh /* Fix me: Make sure the INTEGER PRIMARY KEY is not NULL. */ 6420ca3e24bSdrh continue; 6430ca3e24bSdrh } 6449cfcf5d4Sdrh onError = pTab->aCol[i].notNull; 6450ca3e24bSdrh if( onError==OE_None ) continue; 6469cfcf5d4Sdrh if( overrideError!=OE_Default ){ 6479cfcf5d4Sdrh onError = overrideError; 6481c92853dSdrh }else if( onError==OE_Default ){ 6490d65dc0eSdrh onError = pParse->db->onError; 6500d65dc0eSdrh if( onError==OE_Default ) onError = OE_Abort; 6519cfcf5d4Sdrh } 6529cfcf5d4Sdrh if( onError==OE_Replace && pTab->aCol[i].zDflt==0 ){ 6539cfcf5d4Sdrh onError = OE_Abort; 6549cfcf5d4Sdrh } 655ef6764a1Sdrh sqliteVdbeAddOp(v, OP_Dup, nCol-1-i, 1); 656f5905aa7Sdrh addr = sqliteVdbeAddOp(v, OP_NotNull, 1, 0); 6579cfcf5d4Sdrh switch( onError ){ 6581c92853dSdrh case OE_Rollback: 6591c92853dSdrh case OE_Abort: 6601c92853dSdrh case OE_Fail: { 661483750baSdrh char *zMsg = 0; 6621c92853dSdrh sqliteVdbeAddOp(v, OP_Halt, SQLITE_CONSTRAINT, onError); 663483750baSdrh sqliteSetString(&zMsg, pTab->zName, ".", pTab->aCol[i].zName, 664483750baSdrh " may not be NULL", 0); 665483750baSdrh sqliteVdbeChangeP3(v, -1, zMsg, P3_DYNAMIC); 6669cfcf5d4Sdrh break; 6679cfcf5d4Sdrh } 6689cfcf5d4Sdrh case OE_Ignore: { 669b419a926Sdrh sqliteVdbeAddOp(v, OP_Pop, nCol+1+hasTwoRecnos, 0); 6700ca3e24bSdrh sqliteVdbeAddOp(v, OP_Goto, 0, ignoreDest); 6719cfcf5d4Sdrh break; 6729cfcf5d4Sdrh } 6739cfcf5d4Sdrh case OE_Replace: { 6749cfcf5d4Sdrh sqliteVdbeAddOp(v, OP_String, 0, 0); 6759cfcf5d4Sdrh sqliteVdbeChangeP3(v, -1, pTab->aCol[i].zDflt, P3_STATIC); 6769cfcf5d4Sdrh sqliteVdbeAddOp(v, OP_Push, nCol-i, 0); 6779cfcf5d4Sdrh break; 6789cfcf5d4Sdrh } 6790ca3e24bSdrh default: assert(0); 6809cfcf5d4Sdrh } 681ef6764a1Sdrh sqliteVdbeChangeP2(v, addr, sqliteVdbeCurrentAddr(v)); 6829cfcf5d4Sdrh } 6839cfcf5d4Sdrh 6849cfcf5d4Sdrh /* Test all CHECK constraints 6859cfcf5d4Sdrh */ 6860bd1f4eaSdrh /**** TBD ****/ 6879cfcf5d4Sdrh 6880bd1f4eaSdrh /* If we have an INTEGER PRIMARY KEY, make sure the primary key 6890bd1f4eaSdrh ** of the new record does not previously exist. Except, if this 6900bd1f4eaSdrh ** is an UPDATE and the primary key is not changing, that is OK. 6910bd1f4eaSdrh ** Also, if the conflict resolution policy is REPLACE, then we 6920bd1f4eaSdrh ** can skip this test. 6939cfcf5d4Sdrh */ 6940d65dc0eSdrh if( (recnoChng || !isUpdate) && pTab->iPKey>=0 ){ 6950ca3e24bSdrh onError = pTab->keyConf; 6960ca3e24bSdrh if( overrideError!=OE_Default ){ 6970ca3e24bSdrh onError = overrideError; 6981c92853dSdrh }else if( onError==OE_Default ){ 6990d65dc0eSdrh onError = pParse->db->onError; 7000d65dc0eSdrh if( onError==OE_Default ) onError = OE_Abort; 7010ca3e24bSdrh } 7020d65dc0eSdrh if( onError!=OE_Replace ){ 70379b0c956Sdrh if( isUpdate ){ 70479b0c956Sdrh sqliteVdbeAddOp(v, OP_Dup, nCol+1, 1); 70579b0c956Sdrh sqliteVdbeAddOp(v, OP_Dup, nCol+1, 1); 706f5905aa7Sdrh jumpInst1 = sqliteVdbeAddOp(v, OP_Eq, 0, 0); 70779b0c956Sdrh } 7080d65dc0eSdrh sqliteVdbeAddOp(v, OP_Dup, nCol, 1); 709f5905aa7Sdrh jumpInst2 = sqliteVdbeAddOp(v, OP_NotExists, base, 0); 7100ca3e24bSdrh switch( onError ){ 7111c92853dSdrh case OE_Rollback: 7121c92853dSdrh case OE_Abort: 7131c92853dSdrh case OE_Fail: { 7141c92853dSdrh sqliteVdbeAddOp(v, OP_Halt, SQLITE_CONSTRAINT, onError); 715483750baSdrh sqliteVdbeChangeP3(v, -1, "PRIMARY KEY must be unique", P3_STATIC); 7160ca3e24bSdrh break; 7170ca3e24bSdrh } 7180ca3e24bSdrh case OE_Ignore: { 719b419a926Sdrh sqliteVdbeAddOp(v, OP_Pop, nCol+1+hasTwoRecnos, 0); 7200ca3e24bSdrh sqliteVdbeAddOp(v, OP_Goto, 0, ignoreDest); 7210ca3e24bSdrh break; 7220ca3e24bSdrh } 7230ca3e24bSdrh default: assert(0); 7240ca3e24bSdrh } 7250ca3e24bSdrh contAddr = sqliteVdbeCurrentAddr(v); 72679b0c956Sdrh sqliteVdbeChangeP2(v, jumpInst2, contAddr); 727f5905aa7Sdrh if( isUpdate ){ 728f5905aa7Sdrh sqliteVdbeChangeP2(v, jumpInst1, contAddr); 7290ca3e24bSdrh sqliteVdbeAddOp(v, OP_Dup, nCol+1, 1); 7300ca3e24bSdrh sqliteVdbeAddOp(v, OP_MoveTo, base, 0); 7310ca3e24bSdrh } 7320ca3e24bSdrh } 7330d65dc0eSdrh } 7340bd1f4eaSdrh 7350bd1f4eaSdrh /* Test all UNIQUE constraints by creating entries for each UNIQUE 7360bd1f4eaSdrh ** index and making sure that duplicate entries do not already exist. 7370bd1f4eaSdrh ** Add the new records to the indices as we go. 7380bd1f4eaSdrh */ 7399cfcf5d4Sdrh extra = 0; 7409cfcf5d4Sdrh for(extra=(-1), iCur=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, iCur++){ 7419cfcf5d4Sdrh if( aIdxUsed && aIdxUsed[iCur]==0 ) continue; 7429cfcf5d4Sdrh extra++; 7439cfcf5d4Sdrh sqliteVdbeAddOp(v, OP_Dup, nCol+extra, 1); 7449cfcf5d4Sdrh for(i=0; i<pIdx->nColumn; i++){ 7459cfcf5d4Sdrh int idx = pIdx->aiColumn[i]; 7469cfcf5d4Sdrh if( idx==pTab->iPKey ){ 7470ca3e24bSdrh sqliteVdbeAddOp(v, OP_Dup, i+extra+nCol+1, 1); 7489cfcf5d4Sdrh }else{ 7490ca3e24bSdrh sqliteVdbeAddOp(v, OP_Dup, i+extra+nCol-idx, 1); 7509cfcf5d4Sdrh } 7519cfcf5d4Sdrh } 752f5905aa7Sdrh jumpInst1 = sqliteVdbeAddOp(v, OP_MakeIdxKey, pIdx->nColumn, 0); 753491791a8Sdrh if( pParse->db->file_format>=4 ) sqliteAddIdxKeyType(v, pIdx); 7549cfcf5d4Sdrh onError = pIdx->onError; 7559cfcf5d4Sdrh if( onError==OE_None ) continue; 7569cfcf5d4Sdrh if( overrideError!=OE_Default ){ 7579cfcf5d4Sdrh onError = overrideError; 7581c92853dSdrh }else if( onError==OE_Default ){ 7590d65dc0eSdrh onError = pParse->db->onError; 7600d65dc0eSdrh if( onError==OE_Default ) onError = OE_Abort; 7619cfcf5d4Sdrh } 762b419a926Sdrh sqliteVdbeAddOp(v, OP_Dup, extra+nCol+1+hasTwoRecnos, 1); 763f5905aa7Sdrh jumpInst2 = sqliteVdbeAddOp(v, OP_IsUnique, base+iCur+1, 0); 7649cfcf5d4Sdrh switch( onError ){ 7651c92853dSdrh case OE_Rollback: 7661c92853dSdrh case OE_Abort: 7671c92853dSdrh case OE_Fail: { 7681c92853dSdrh sqliteVdbeAddOp(v, OP_Halt, SQLITE_CONSTRAINT, onError); 769483750baSdrh sqliteVdbeChangeP3(v, -1, "uniqueness constraint failed", P3_STATIC); 7709cfcf5d4Sdrh break; 7719cfcf5d4Sdrh } 7729cfcf5d4Sdrh case OE_Ignore: { 7730ca3e24bSdrh assert( seenReplace==0 ); 774fe1a1773Sdrh sqliteVdbeAddOp(v, OP_Pop, nCol+extra+3+hasTwoRecnos, 0); 7759cfcf5d4Sdrh sqliteVdbeAddOp(v, OP_Goto, 0, ignoreDest); 7769cfcf5d4Sdrh break; 7779cfcf5d4Sdrh } 7789cfcf5d4Sdrh case OE_Replace: { 77938640e15Sdrh sqliteGenerateRowDelete(pParse->db, v, pTab, base, 0); 7809cfcf5d4Sdrh if( isUpdate ){ 781b419a926Sdrh sqliteVdbeAddOp(v, OP_Dup, nCol+extra+1+hasTwoRecnos, 1); 7820ca3e24bSdrh sqliteVdbeAddOp(v, OP_MoveTo, base, 0); 7839cfcf5d4Sdrh } 7840ca3e24bSdrh seenReplace = 1; 7859cfcf5d4Sdrh break; 7869cfcf5d4Sdrh } 7870ca3e24bSdrh default: assert(0); 7889cfcf5d4Sdrh } 7899cfcf5d4Sdrh contAddr = sqliteVdbeCurrentAddr(v); 7900bd1f4eaSdrh #if NULL_DISTINCT_FOR_UNIQUE 791f5905aa7Sdrh sqliteVdbeChangeP2(v, jumpInst1, contAddr); 7920bd1f4eaSdrh #endif 793f5905aa7Sdrh sqliteVdbeChangeP2(v, jumpInst2, contAddr); 7949cfcf5d4Sdrh } 7959cfcf5d4Sdrh } 7960ca3e24bSdrh 7970ca3e24bSdrh /* 7980ca3e24bSdrh ** This routine generates code to finish the INSERT or UPDATE operation 7990ca3e24bSdrh ** that was started by a prior call to sqliteGenerateConstraintChecks. 8000ca3e24bSdrh ** The stack must contain keys for all active indices followed by data 8010ca3e24bSdrh ** and the recno for the new entry. This routine creates the new 8020ca3e24bSdrh ** entries in all indices and in the main table. 8030ca3e24bSdrh ** 804b419a926Sdrh ** The arguments to this routine should be the same as the first six 8050ca3e24bSdrh ** arguments to sqliteGenerateConstraintChecks. 8060ca3e24bSdrh */ 8070ca3e24bSdrh void sqliteCompleteInsertion( 8080ca3e24bSdrh Parse *pParse, /* The parser context */ 8090ca3e24bSdrh Table *pTab, /* the table into which we are inserting */ 8100ca3e24bSdrh int base, /* Index of a read/write cursor pointing at pTab */ 8110ca3e24bSdrh char *aIdxUsed, /* Which indices are used. NULL means all are used */ 812b419a926Sdrh int recnoChng, /* True if the record number will change */ 813b419a926Sdrh int isUpdate /* True for UPDATE, False for INSERT */ 8140ca3e24bSdrh ){ 8150ca3e24bSdrh int i; 8160ca3e24bSdrh Vdbe *v; 8170ca3e24bSdrh int nIdx; 8180ca3e24bSdrh Index *pIdx; 8190ca3e24bSdrh 8200ca3e24bSdrh v = sqliteGetVdbe(pParse); 8210ca3e24bSdrh assert( v!=0 ); 822417be79cSdrh assert( pTab->pSelect==0 ); /* This table is not a VIEW */ 8230ca3e24bSdrh for(nIdx=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, nIdx++){} 8240ca3e24bSdrh for(i=nIdx-1; i>=0; i--){ 8250ca3e24bSdrh if( aIdxUsed && aIdxUsed[i]==0 ) continue; 8260ca3e24bSdrh sqliteVdbeAddOp(v, OP_IdxPut, base+i+1, 0); 8270ca3e24bSdrh } 8280ca3e24bSdrh sqliteVdbeAddOp(v, OP_MakeRecord, pTab->nCol, 0); 829c3f9bad2Sdanielk1977 sqliteVdbeAddOp(v, OP_PutIntKey, base, pParse->trigStack?0:1); 830b419a926Sdrh if( isUpdate && recnoChng ){ 8310ca3e24bSdrh sqliteVdbeAddOp(v, OP_Pop, 1, 0); 8320ca3e24bSdrh } 8330ca3e24bSdrh } 834