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*417be79cSdrh ** $Id: insert.c,v 1.47 2002/03/03 18:59:41 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. 33cce7d176Sdrh */ 34cce7d176Sdrh void sqliteInsert( 35cce7d176Sdrh Parse *pParse, /* Parser context */ 36cce7d176Sdrh Token *pTableName, /* Name of table into which we are inserting */ 37cce7d176Sdrh ExprList *pList, /* List of values to be inserted */ 385974a30fSdrh Select *pSelect, /* A SELECT statement to use as the data source */ 399cfcf5d4Sdrh IdList *pColumn, /* Column names corresponding to IDLIST. */ 409cfcf5d4Sdrh int onError /* How to handle constraint errors */ 41cce7d176Sdrh ){ 425974a30fSdrh Table *pTab; /* The table to insert into */ 435974a30fSdrh char *zTab; /* Name of the table into which we are inserting */ 445974a30fSdrh int i, j, idx; /* Loop counters */ 455974a30fSdrh Vdbe *v; /* Generate code into this virtual machine */ 465974a30fSdrh Index *pIdx; /* For looping over indices of the table */ 475974a30fSdrh int srcTab; /* Date comes from this temporary cursor if >=0 */ 48967e8b73Sdrh int nColumn; /* Number of columns in the data */ 495974a30fSdrh int base; /* First available cursor */ 505974a30fSdrh int iCont, iBreak; /* Beginning and end of the loop over srcTab */ 51ecdc7530Sdrh sqlite *db; /* The main database structure */ 52f57b3399Sdrh int openOp; /* Opcode used to open cursors */ 534a32431cSdrh int keyColumn = -1; /* Column that is the INTEGER PRIMARY KEY */ 540ca3e24bSdrh int endOfLoop; /* Label for the end of the insertion loop */ 55cce7d176Sdrh 56daffd0e5Sdrh if( pParse->nErr || sqlite_malloc_failed ) goto insert_cleanup; 57ecdc7530Sdrh db = pParse->db; 58daffd0e5Sdrh 591ccde15dSdrh /* Locate the table into which we will be inserting new information. 601ccde15dSdrh */ 61cce7d176Sdrh zTab = sqliteTableNameFromToken(pTableName); 62daffd0e5Sdrh if( zTab==0 ) goto insert_cleanup; 63a76b5dfcSdrh pTab = sqliteTableNameToTable(pParse, zTab); 64cce7d176Sdrh sqliteFree(zTab); 65a76b5dfcSdrh if( pTab==0 ) goto insert_cleanup; 66*417be79cSdrh assert( pTab->pSelect==0 ); /* This table is not a VIEW */ 671ccde15dSdrh 681ccde15dSdrh /* Allocate a VDBE 691ccde15dSdrh */ 70d8bc7086Sdrh v = sqliteGetVdbe(pParse); 715974a30fSdrh if( v==0 ) goto insert_cleanup; 72663fc63aSdrh if( pSelect ){ 73663fc63aSdrh sqliteBeginMultiWriteOperation(pParse); 74663fc63aSdrh }else{ 751c92853dSdrh sqliteBeginWriteOperation(pParse); 76663fc63aSdrh } 771ccde15dSdrh 781ccde15dSdrh /* Figure out how many columns of data are supplied. If the data 79c6b52df3Sdrh ** is coming from a SELECT statement, then this step has to generate 801ccde15dSdrh ** all the code to implement the SELECT statement and leave the data 811ccde15dSdrh ** in a temporary table. If data is coming from an expression list, 821ccde15dSdrh ** then we just have to count the number of expressions. 831ccde15dSdrh */ 845974a30fSdrh if( pSelect ){ 855974a30fSdrh int rc; 865974a30fSdrh srcTab = pParse->nTab++; 8799fcd718Sdrh sqliteVdbeAddOp(v, OP_OpenTemp, srcTab, 0); 88832508b7Sdrh rc = sqliteSelect(pParse, pSelect, SRT_Table, srcTab, 0,0,0); 89daffd0e5Sdrh if( rc || pParse->nErr || sqlite_malloc_failed ) goto insert_cleanup; 905974a30fSdrh assert( pSelect->pEList ); 91967e8b73Sdrh nColumn = pSelect->pEList->nExpr; 925974a30fSdrh }else{ 93e64e7b20Sdrh IdList dummy; 94daffd0e5Sdrh assert( pList!=0 ); 955974a30fSdrh srcTab = -1; 965974a30fSdrh assert( pList ); 97967e8b73Sdrh nColumn = pList->nExpr; 98e64e7b20Sdrh dummy.nId = 0; 99e64e7b20Sdrh for(i=0; i<nColumn; i++){ 100832508b7Sdrh if( sqliteExprResolveIds(pParse, 0, &dummy, 0, pList->a[i].pExpr) ){ 101e64e7b20Sdrh goto insert_cleanup; 102e64e7b20Sdrh } 103e64e7b20Sdrh } 1045974a30fSdrh } 1051ccde15dSdrh 1061ccde15dSdrh /* Make sure the number of columns in the source data matches the number 1071ccde15dSdrh ** of columns to be inserted into the table. 1081ccde15dSdrh */ 109967e8b73Sdrh if( pColumn==0 && nColumn!=pTab->nCol ){ 110cce7d176Sdrh char zNum1[30]; 111cce7d176Sdrh char zNum2[30]; 112967e8b73Sdrh sprintf(zNum1,"%d", nColumn); 113cce7d176Sdrh sprintf(zNum2,"%d", pTab->nCol); 114cce7d176Sdrh sqliteSetString(&pParse->zErrMsg, "table ", pTab->zName, 115cce7d176Sdrh " has ", zNum2, " columns but ", 116cce7d176Sdrh zNum1, " values were supplied", 0); 117cce7d176Sdrh pParse->nErr++; 118cce7d176Sdrh goto insert_cleanup; 119cce7d176Sdrh } 120967e8b73Sdrh if( pColumn!=0 && nColumn!=pColumn->nId ){ 121cce7d176Sdrh char zNum1[30]; 122cce7d176Sdrh char zNum2[30]; 123967e8b73Sdrh sprintf(zNum1,"%d", nColumn); 124967e8b73Sdrh sprintf(zNum2,"%d", pColumn->nId); 125cce7d176Sdrh sqliteSetString(&pParse->zErrMsg, zNum1, " values for ", 126cce7d176Sdrh zNum2, " columns", 0); 127cce7d176Sdrh pParse->nErr++; 128cce7d176Sdrh goto insert_cleanup; 129cce7d176Sdrh } 1301ccde15dSdrh 1311ccde15dSdrh /* If the INSERT statement included an IDLIST term, then make sure 1321ccde15dSdrh ** all elements of the IDLIST really are columns of the table and 1331ccde15dSdrh ** remember the column indices. 134c8392586Sdrh ** 135c8392586Sdrh ** If the table has an INTEGER PRIMARY KEY column and that column 136c8392586Sdrh ** is named in the IDLIST, then record in the keyColumn variable 137c8392586Sdrh ** the index into IDLIST of the primary key column. keyColumn is 138c8392586Sdrh ** the index of the primary key as it appears in IDLIST, not as 139c8392586Sdrh ** is appears in the original table. (The index of the primary 140c8392586Sdrh ** key in the original table is pTab->iPKey.) 1411ccde15dSdrh */ 142967e8b73Sdrh if( pColumn ){ 143967e8b73Sdrh for(i=0; i<pColumn->nId; i++){ 144967e8b73Sdrh pColumn->a[i].idx = -1; 145cce7d176Sdrh } 146967e8b73Sdrh for(i=0; i<pColumn->nId; i++){ 147cce7d176Sdrh for(j=0; j<pTab->nCol; j++){ 148967e8b73Sdrh if( sqliteStrICmp(pColumn->a[i].zName, pTab->aCol[j].zName)==0 ){ 149967e8b73Sdrh pColumn->a[i].idx = j; 1504a32431cSdrh if( j==pTab->iPKey ){ 1519aa028daSdrh keyColumn = i; 1524a32431cSdrh } 153cce7d176Sdrh break; 154cce7d176Sdrh } 155cce7d176Sdrh } 156cce7d176Sdrh if( j>=pTab->nCol ){ 157cce7d176Sdrh sqliteSetString(&pParse->zErrMsg, "table ", pTab->zName, 158967e8b73Sdrh " has no column named ", pColumn->a[i].zName, 0); 159cce7d176Sdrh pParse->nErr++; 160cce7d176Sdrh goto insert_cleanup; 161cce7d176Sdrh } 162cce7d176Sdrh } 163cce7d176Sdrh } 1641ccde15dSdrh 165aacc543eSdrh /* If there is no IDLIST term but the table has an integer primary 166c8392586Sdrh ** key, the set the keyColumn variable to the primary key column index 167c8392586Sdrh ** in the original table definition. 1684a32431cSdrh */ 1694a32431cSdrh if( pColumn==0 ){ 1704a32431cSdrh keyColumn = pTab->iPKey; 1714a32431cSdrh } 1724a32431cSdrh 1731ccde15dSdrh /* Open cursors into the table that is received the new data and 1741ccde15dSdrh ** all indices of that table. 1751ccde15dSdrh */ 1765974a30fSdrh base = pParse->nTab; 177f57b3399Sdrh openOp = pTab->isTemp ? OP_OpenWrAux : OP_OpenWrite; 17899fcd718Sdrh sqliteVdbeAddOp(v, openOp, base, pTab->tnum); 17999fcd718Sdrh sqliteVdbeChangeP3(v, -1, pTab->zName, P3_STATIC); 180bed8690fSdrh for(idx=1, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, idx++){ 18199fcd718Sdrh sqliteVdbeAddOp(v, openOp, idx+base, pIdx->tnum); 18299fcd718Sdrh sqliteVdbeChangeP3(v, -1, pIdx->zName, P3_STATIC); 1835974a30fSdrh } 184832508b7Sdrh pParse->nTab += idx; 1851ccde15dSdrh 1861ccde15dSdrh /* If the data source is a SELECT statement, then we have to create 1871ccde15dSdrh ** a loop because there might be multiple rows of data. If the data 1881ccde15dSdrh ** source is an expression list, then exactly one row will be inserted 1891ccde15dSdrh ** and the loop is not used. 1901ccde15dSdrh */ 1915974a30fSdrh if( srcTab>=0 ){ 1921bee3d7bSdrh if( db->flags & SQLITE_CountRows ){ 1931bee3d7bSdrh sqliteVdbeAddOp(v, OP_Integer, 0, 0); /* Initialize the row count */ 1941bee3d7bSdrh } 1955974a30fSdrh iBreak = sqliteVdbeMakeLabel(v); 1966b56344dSdrh sqliteVdbeAddOp(v, OP_Rewind, srcTab, iBreak); 1976b56344dSdrh iCont = sqliteVdbeCurrentAddr(v); 198bed8690fSdrh } 1991ccde15dSdrh 2004a32431cSdrh /* Push the record number for the new entry onto the stack. The 2014a32431cSdrh ** record number is a randomly generate integer created by NewRecno 2024a32431cSdrh ** except when the table has an INTEGER PRIMARY KEY column, in which 203b419a926Sdrh ** case the record number is the same as that column. 2041ccde15dSdrh */ 2054a32431cSdrh if( keyColumn>=0 ){ 2064a32431cSdrh if( srcTab>=0 ){ 2074a32431cSdrh sqliteVdbeAddOp(v, OP_Column, srcTab, keyColumn); 2084a32431cSdrh }else{ 2094a32431cSdrh sqliteExprCode(pParse, pList->a[keyColumn].pExpr); 2104a32431cSdrh } 2118aff1015Sdrh sqliteVdbeAddOp(v, OP_MustBeInt, 0, 0); 2124a32431cSdrh }else{ 21399fcd718Sdrh sqliteVdbeAddOp(v, OP_NewRecno, base, 0); 2144a32431cSdrh } 2154a32431cSdrh 216aacc543eSdrh /* Push onto the stack, data for all columns of the new entry, beginning 2174a32431cSdrh ** with the first column. 2184a32431cSdrh */ 219cce7d176Sdrh for(i=0; i<pTab->nCol; i++){ 2204a32431cSdrh if( i==pTab->iPKey ){ 2214a32431cSdrh /* The value of the INTEGER PRIMARY KEY column is always a NULL. 222aacc543eSdrh ** Whenever this column is read, the record number will be substituted 223aacc543eSdrh ** in its place. So will fill this column with a NULL to avoid 224aacc543eSdrh ** taking up data space with information that will never be used. */ 2254a32431cSdrh sqliteVdbeAddOp(v, OP_String, 0, 0); 2264a32431cSdrh continue; 2274a32431cSdrh } 228967e8b73Sdrh if( pColumn==0 ){ 229cce7d176Sdrh j = i; 230cce7d176Sdrh }else{ 231967e8b73Sdrh for(j=0; j<pColumn->nId; j++){ 232967e8b73Sdrh if( pColumn->a[j].idx==i ) break; 233cce7d176Sdrh } 234cce7d176Sdrh } 235967e8b73Sdrh if( pColumn && j>=pColumn->nId ){ 23699fcd718Sdrh sqliteVdbeAddOp(v, OP_String, 0, 0); 23799fcd718Sdrh sqliteVdbeChangeP3(v, -1, pTab->aCol[i].zDflt, P3_STATIC); 2385974a30fSdrh }else if( srcTab>=0 ){ 23924e97df9Sdrh sqliteVdbeAddOp(v, OP_Column, srcTab, j); 240cce7d176Sdrh }else{ 241cce7d176Sdrh sqliteExprCode(pParse, pList->a[j].pExpr); 242cce7d176Sdrh } 243cce7d176Sdrh } 2441ccde15dSdrh 2450ca3e24bSdrh /* Generate code to check constraints and generate index keys and 2460ca3e24bSdrh ** do the insertion. 2474a32431cSdrh */ 2480ca3e24bSdrh endOfLoop = sqliteVdbeMakeLabel(v); 249b419a926Sdrh sqliteGenerateConstraintChecks(pParse, pTab, base, 0,0,0, onError, endOfLoop); 250b419a926Sdrh sqliteCompleteInsertion(pParse, pTab, base, 0,0,0); 2511bee3d7bSdrh 2521bee3d7bSdrh /* If inserting from a SELECT, keep a count of the number of 2531bee3d7bSdrh ** rows inserted. 2541bee3d7bSdrh */ 2551bee3d7bSdrh if( srcTab>=0 && (db->flags & SQLITE_CountRows)!=0 ){ 2561bee3d7bSdrh sqliteVdbeAddOp(v, OP_AddImm, 1, 0); 2571bee3d7bSdrh } 2581bee3d7bSdrh 2591ccde15dSdrh /* The bottom of the loop, if the data source is a SELECT statement 2601ccde15dSdrh */ 2610ca3e24bSdrh sqliteVdbeResolveLabel(v, endOfLoop); 2625974a30fSdrh if( srcTab>=0 ){ 2636b56344dSdrh sqliteVdbeAddOp(v, OP_Next, srcTab, iCont); 26499fcd718Sdrh sqliteVdbeResolveLabel(v, iBreak); 2656b56344dSdrh sqliteVdbeAddOp(v, OP_Close, srcTab, 0); 2666b56344dSdrh } 2676b56344dSdrh sqliteVdbeAddOp(v, OP_Close, base, 0); 2686b56344dSdrh for(idx=1, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, idx++){ 2696b56344dSdrh sqliteVdbeAddOp(v, OP_Close, idx+base, 0); 270cce7d176Sdrh } 2711c92853dSdrh sqliteEndWriteOperation(pParse); 2725e00f6c7Sdrh 2731bee3d7bSdrh /* 2741bee3d7bSdrh ** Return the number of rows inserted. 2751bee3d7bSdrh */ 2761bee3d7bSdrh if( db->flags & SQLITE_CountRows ){ 2771bee3d7bSdrh sqliteVdbeAddOp(v, OP_ColumnCount, 1, 0); 2781bee3d7bSdrh sqliteVdbeAddOp(v, OP_ColumnName, 0, 0); 2791bee3d7bSdrh sqliteVdbeChangeP3(v, -1, "rows inserted", P3_STATIC); 2801bee3d7bSdrh if( srcTab<0 ){ 2811bee3d7bSdrh sqliteVdbeAddOp(v, OP_Integer, 1, 0); 2821bee3d7bSdrh } 2831bee3d7bSdrh sqliteVdbeAddOp(v, OP_Callback, 1, 0); 2841bee3d7bSdrh } 285cce7d176Sdrh 286cce7d176Sdrh insert_cleanup: 2875974a30fSdrh if( pList ) sqliteExprListDelete(pList); 2885974a30fSdrh if( pSelect ) sqliteSelectDelete(pSelect); 289967e8b73Sdrh sqliteIdListDelete(pColumn); 290cce7d176Sdrh } 2919cfcf5d4Sdrh 2929cfcf5d4Sdrh /* 2939cfcf5d4Sdrh ** Generate code to do a constraint check prior to an INSERT or an UPDATE. 2949cfcf5d4Sdrh ** 2959cfcf5d4Sdrh ** When this routine is called, the stack contains (from bottom to top) 2960ca3e24bSdrh ** the following values: 2970ca3e24bSdrh ** 298b419a926Sdrh ** 1. The recno of the row to be updated before it is updated. This 299b419a926Sdrh ** value is omitted unless we are doing an UPDATE that involves a 300b419a926Sdrh ** change to the record number. 3010ca3e24bSdrh ** 302b419a926Sdrh ** 2. The recno of the row after the update. 3030ca3e24bSdrh ** 3040ca3e24bSdrh ** 3. The data in the first column of the entry after the update. 3050ca3e24bSdrh ** 3060ca3e24bSdrh ** i. Data from middle columns... 3070ca3e24bSdrh ** 3080ca3e24bSdrh ** N. The data in the last column of the entry after the update. 3090ca3e24bSdrh ** 310b419a926Sdrh ** The old recno shown as entry (1) above is omitted unless both isUpdate 3111c92853dSdrh ** and recnoChng are 1. isUpdate is true for UPDATEs and false for 3121c92853dSdrh ** INSERTs and recnoChng is true if the record number is being changed. 3130ca3e24bSdrh ** 3140ca3e24bSdrh ** The code generated by this routine pushes additional entries onto 3150ca3e24bSdrh ** the stack which are the keys for new index entries for the new record. 3160ca3e24bSdrh ** The order of index keys is the same as the order of the indices on 3170ca3e24bSdrh ** the pTable->pIndex list. A key is only created for index i if 3180ca3e24bSdrh ** aIdxUsed!=0 and aIdxUsed[i]!=0. 3199cfcf5d4Sdrh ** 3209cfcf5d4Sdrh ** This routine also generates code to check constraints. NOT NULL, 3219cfcf5d4Sdrh ** CHECK, and UNIQUE constraints are all checked. If a constraint fails, 3221c92853dSdrh ** then the appropriate action is performed. There are five possible 3231c92853dSdrh ** actions: ROLLBACK, ABORT, FAIL, REPLACE, and IGNORE. 3249cfcf5d4Sdrh ** 3259cfcf5d4Sdrh ** Constraint type Action What Happens 3269cfcf5d4Sdrh ** --------------- ---------- ---------------------------------------- 3271c92853dSdrh ** any ROLLBACK The current transaction is rolled back and 3289cfcf5d4Sdrh ** sqlite_exec() returns immediately with a 3299cfcf5d4Sdrh ** return code of SQLITE_CONSTRAINT. 3309cfcf5d4Sdrh ** 3311c92853dSdrh ** any ABORT Back out changes from the current command 3321c92853dSdrh ** only (do not do a complete rollback) then 3331c92853dSdrh ** cause sqlite_exec() to return immediately 3341c92853dSdrh ** with SQLITE_CONSTRAINT. 3351c92853dSdrh ** 3361c92853dSdrh ** any FAIL Sqlite_exec() returns immediately with a 3371c92853dSdrh ** return code of SQLITE_CONSTRAINT. The 3381c92853dSdrh ** transaction is not rolled back and any 3391c92853dSdrh ** prior changes are retained. 3401c92853dSdrh ** 3419cfcf5d4Sdrh ** any IGNORE The record number and data is popped from 3429cfcf5d4Sdrh ** the stack and there is an immediate jump 3439cfcf5d4Sdrh ** to label ignoreDest. 3449cfcf5d4Sdrh ** 3459cfcf5d4Sdrh ** NOT NULL REPLACE The NULL value is replace by the default 3469cfcf5d4Sdrh ** value for that column. If the default value 3479cfcf5d4Sdrh ** is NULL, the action is the same as ABORT. 3489cfcf5d4Sdrh ** 3499cfcf5d4Sdrh ** UNIQUE REPLACE The other row that conflicts with the row 3509cfcf5d4Sdrh ** being inserted is removed. 3519cfcf5d4Sdrh ** 3529cfcf5d4Sdrh ** CHECK REPLACE Illegal. The results in an exception. 3539cfcf5d4Sdrh ** 3541c92853dSdrh ** Which action to take is determined by the overrideError parameter. 3551c92853dSdrh ** Or if overrideError==OE_Default, then the pParse->onError parameter 3561c92853dSdrh ** is used. Or if pParse->onError==OE_Default then the onError value 3571c92853dSdrh ** for the constraint is used. 3589cfcf5d4Sdrh ** 359aaab5725Sdrh ** The calling routine must open a read/write cursor for pTab with 3609cfcf5d4Sdrh ** cursor number "base". All indices of pTab must also have open 3619cfcf5d4Sdrh ** read/write cursors with cursor number base+i for the i-th cursor. 3629cfcf5d4Sdrh ** Except, if there is no possibility of a REPLACE action then 3639cfcf5d4Sdrh ** cursors do not need to be open for indices where aIdxUsed[i]==0. 3649cfcf5d4Sdrh ** 3659cfcf5d4Sdrh ** If the isUpdate flag is true, it means that the "base" cursor is 3669cfcf5d4Sdrh ** initially pointing to an entry that is being updated. The isUpdate 3679cfcf5d4Sdrh ** flag causes extra code to be generated so that the "base" cursor 3689cfcf5d4Sdrh ** is still pointing at the same entry after the routine returns. 3699cfcf5d4Sdrh ** Without the isUpdate flag, the "base" cursor might be moved. 3709cfcf5d4Sdrh */ 3719cfcf5d4Sdrh void sqliteGenerateConstraintChecks( 3729cfcf5d4Sdrh Parse *pParse, /* The parser context */ 3739cfcf5d4Sdrh Table *pTab, /* the table into which we are inserting */ 3749cfcf5d4Sdrh int base, /* Index of a read/write cursor pointing at pTab */ 3759cfcf5d4Sdrh char *aIdxUsed, /* Which indices are used. NULL means all are used */ 3760ca3e24bSdrh int recnoChng, /* True if the record number will change */ 377b419a926Sdrh int isUpdate, /* True for UPDATE, False for INSERT */ 3789cfcf5d4Sdrh int overrideError, /* Override onError to this if not OE_Default */ 379b419a926Sdrh int ignoreDest /* Jump to this label on an OE_Ignore resolution */ 3809cfcf5d4Sdrh ){ 3819cfcf5d4Sdrh int i; 3829cfcf5d4Sdrh Vdbe *v; 3839cfcf5d4Sdrh int nCol; 3849cfcf5d4Sdrh int onError; 3859cfcf5d4Sdrh int addr; 3869cfcf5d4Sdrh int extra; 3870ca3e24bSdrh int iCur; 3880ca3e24bSdrh Index *pIdx; 3890ca3e24bSdrh int seenReplace = 0; 3900ca3e24bSdrh int jumpInst; 3910ca3e24bSdrh int contAddr; 392b419a926Sdrh int hasTwoRecnos = (isUpdate && recnoChng); 3939cfcf5d4Sdrh 3949cfcf5d4Sdrh v = sqliteGetVdbe(pParse); 3959cfcf5d4Sdrh assert( v!=0 ); 396*417be79cSdrh assert( pTab->pSelect==0 ); /* This table is not a VIEW */ 3979cfcf5d4Sdrh nCol = pTab->nCol; 3989cfcf5d4Sdrh 3999cfcf5d4Sdrh /* Test all NOT NULL constraints. 4009cfcf5d4Sdrh */ 4019cfcf5d4Sdrh for(i=0; i<nCol; i++){ 4020ca3e24bSdrh if( i==pTab->iPKey ){ 4030ca3e24bSdrh /* Fix me: Make sure the INTEGER PRIMARY KEY is not NULL. */ 4040ca3e24bSdrh continue; 4050ca3e24bSdrh } 4069cfcf5d4Sdrh onError = pTab->aCol[i].notNull; 4070ca3e24bSdrh if( onError==OE_None ) continue; 4089cfcf5d4Sdrh if( overrideError!=OE_Default ){ 4099cfcf5d4Sdrh onError = overrideError; 4101c92853dSdrh }else if( onError==OE_Default ){ 4110d65dc0eSdrh onError = pParse->db->onError; 4120d65dc0eSdrh if( onError==OE_Default ) onError = OE_Abort; 4139cfcf5d4Sdrh } 4149cfcf5d4Sdrh if( onError==OE_Replace && pTab->aCol[i].zDflt==0 ){ 4159cfcf5d4Sdrh onError = OE_Abort; 4169cfcf5d4Sdrh } 417ef6764a1Sdrh sqliteVdbeAddOp(v, OP_Dup, nCol-1-i, 1); 418ef6764a1Sdrh addr = sqliteVdbeAddOp(v, OP_NotNull, 0, 0); 4199cfcf5d4Sdrh switch( onError ){ 4201c92853dSdrh case OE_Rollback: 4211c92853dSdrh case OE_Abort: 4221c92853dSdrh case OE_Fail: { 4231c92853dSdrh sqliteVdbeAddOp(v, OP_Halt, SQLITE_CONSTRAINT, onError); 4249cfcf5d4Sdrh break; 4259cfcf5d4Sdrh } 4269cfcf5d4Sdrh case OE_Ignore: { 427b419a926Sdrh sqliteVdbeAddOp(v, OP_Pop, nCol+1+hasTwoRecnos, 0); 4280ca3e24bSdrh sqliteVdbeAddOp(v, OP_Goto, 0, ignoreDest); 4299cfcf5d4Sdrh break; 4309cfcf5d4Sdrh } 4319cfcf5d4Sdrh case OE_Replace: { 4329cfcf5d4Sdrh sqliteVdbeAddOp(v, OP_String, 0, 0); 4339cfcf5d4Sdrh sqliteVdbeChangeP3(v, -1, pTab->aCol[i].zDflt, P3_STATIC); 4349cfcf5d4Sdrh sqliteVdbeAddOp(v, OP_Push, nCol-i, 0); 4359cfcf5d4Sdrh break; 4369cfcf5d4Sdrh } 4370ca3e24bSdrh default: assert(0); 4389cfcf5d4Sdrh } 439ef6764a1Sdrh sqliteVdbeChangeP2(v, addr, sqliteVdbeCurrentAddr(v)); 4409cfcf5d4Sdrh } 4419cfcf5d4Sdrh 4429cfcf5d4Sdrh /* Test all CHECK constraints 4439cfcf5d4Sdrh */ 4449cfcf5d4Sdrh 4459cfcf5d4Sdrh /* Test all UNIQUE constraints. Add index records as we go. 4469cfcf5d4Sdrh */ 4470d65dc0eSdrh if( (recnoChng || !isUpdate) && pTab->iPKey>=0 ){ 4480ca3e24bSdrh onError = pTab->keyConf; 4490ca3e24bSdrh if( overrideError!=OE_Default ){ 4500ca3e24bSdrh onError = overrideError; 4511c92853dSdrh }else if( onError==OE_Default ){ 4520d65dc0eSdrh onError = pParse->db->onError; 4530d65dc0eSdrh if( onError==OE_Default ) onError = OE_Abort; 4540ca3e24bSdrh } 4550d65dc0eSdrh if( onError!=OE_Replace ){ 4560d65dc0eSdrh sqliteVdbeAddOp(v, OP_Dup, nCol, 1); 4570d65dc0eSdrh jumpInst = sqliteVdbeAddOp(v, OP_NotExists, base, 0); 4580ca3e24bSdrh switch( onError ){ 4591c92853dSdrh case OE_Rollback: 4601c92853dSdrh case OE_Abort: 4611c92853dSdrh case OE_Fail: { 4621c92853dSdrh sqliteVdbeAddOp(v, OP_Halt, SQLITE_CONSTRAINT, onError); 4630ca3e24bSdrh break; 4640ca3e24bSdrh } 4650ca3e24bSdrh case OE_Ignore: { 466b419a926Sdrh sqliteVdbeAddOp(v, OP_Pop, nCol+1+hasTwoRecnos, 0); 4670ca3e24bSdrh sqliteVdbeAddOp(v, OP_Goto, 0, ignoreDest); 4680ca3e24bSdrh break; 4690ca3e24bSdrh } 4700ca3e24bSdrh default: assert(0); 4710ca3e24bSdrh } 4720ca3e24bSdrh contAddr = sqliteVdbeCurrentAddr(v); 4730ca3e24bSdrh sqliteVdbeChangeP2(v, jumpInst, contAddr); 4740ca3e24bSdrh if( isUpdate ){ 4750ca3e24bSdrh sqliteVdbeAddOp(v, OP_Dup, nCol+1, 1); 4760ca3e24bSdrh sqliteVdbeAddOp(v, OP_MoveTo, base, 0); 4770ca3e24bSdrh } 4780ca3e24bSdrh } 4790d65dc0eSdrh } 4809cfcf5d4Sdrh extra = 0; 4819cfcf5d4Sdrh for(extra=(-1), iCur=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, iCur++){ 4829cfcf5d4Sdrh if( aIdxUsed && aIdxUsed[iCur]==0 ) continue; 4839cfcf5d4Sdrh extra++; 4849cfcf5d4Sdrh sqliteVdbeAddOp(v, OP_Dup, nCol+extra, 1); 4859cfcf5d4Sdrh for(i=0; i<pIdx->nColumn; i++){ 4869cfcf5d4Sdrh int idx = pIdx->aiColumn[i]; 4879cfcf5d4Sdrh if( idx==pTab->iPKey ){ 4880ca3e24bSdrh sqliteVdbeAddOp(v, OP_Dup, i+extra+nCol+1, 1); 4899cfcf5d4Sdrh }else{ 4900ca3e24bSdrh sqliteVdbeAddOp(v, OP_Dup, i+extra+nCol-idx, 1); 4919cfcf5d4Sdrh } 4929cfcf5d4Sdrh } 4939cfcf5d4Sdrh sqliteVdbeAddOp(v, OP_MakeIdxKey, pIdx->nColumn, 0); 4949cfcf5d4Sdrh onError = pIdx->onError; 4959cfcf5d4Sdrh if( onError==OE_None ) continue; 4969cfcf5d4Sdrh if( overrideError!=OE_Default ){ 4979cfcf5d4Sdrh onError = overrideError; 4981c92853dSdrh }else if( onError==OE_Default ){ 4990d65dc0eSdrh onError = pParse->db->onError; 5000d65dc0eSdrh if( onError==OE_Default ) onError = OE_Abort; 5019cfcf5d4Sdrh } 502b419a926Sdrh sqliteVdbeAddOp(v, OP_Dup, extra+nCol+1+hasTwoRecnos, 1); 5030ca3e24bSdrh jumpInst = sqliteVdbeAddOp(v, OP_IsUnique, base+iCur+1, 0); 5049cfcf5d4Sdrh switch( onError ){ 5051c92853dSdrh case OE_Rollback: 5061c92853dSdrh case OE_Abort: 5071c92853dSdrh case OE_Fail: { 5081c92853dSdrh sqliteVdbeAddOp(v, OP_Halt, SQLITE_CONSTRAINT, onError); 5099cfcf5d4Sdrh break; 5109cfcf5d4Sdrh } 5119cfcf5d4Sdrh case OE_Ignore: { 5120ca3e24bSdrh assert( seenReplace==0 ); 513b419a926Sdrh sqliteVdbeAddOp(v, OP_Pop, nCol+extra+2+hasTwoRecnos, 0); 5149cfcf5d4Sdrh sqliteVdbeAddOp(v, OP_Goto, 0, ignoreDest); 5159cfcf5d4Sdrh break; 5169cfcf5d4Sdrh } 5179cfcf5d4Sdrh case OE_Replace: { 5189cfcf5d4Sdrh sqliteGenerateRowDelete(v, pTab, base); 5199cfcf5d4Sdrh if( isUpdate ){ 520b419a926Sdrh sqliteVdbeAddOp(v, OP_Dup, nCol+extra+1+hasTwoRecnos, 1); 5210ca3e24bSdrh sqliteVdbeAddOp(v, OP_MoveTo, base, 0); 5229cfcf5d4Sdrh } 5230ca3e24bSdrh seenReplace = 1; 5249cfcf5d4Sdrh break; 5259cfcf5d4Sdrh } 5260ca3e24bSdrh default: assert(0); 5279cfcf5d4Sdrh } 5289cfcf5d4Sdrh contAddr = sqliteVdbeCurrentAddr(v); 5299cfcf5d4Sdrh sqliteVdbeChangeP2(v, jumpInst, contAddr); 5309cfcf5d4Sdrh } 5319cfcf5d4Sdrh } 5320ca3e24bSdrh 5330ca3e24bSdrh /* 5340ca3e24bSdrh ** This routine generates code to finish the INSERT or UPDATE operation 5350ca3e24bSdrh ** that was started by a prior call to sqliteGenerateConstraintChecks. 5360ca3e24bSdrh ** The stack must contain keys for all active indices followed by data 5370ca3e24bSdrh ** and the recno for the new entry. This routine creates the new 5380ca3e24bSdrh ** entries in all indices and in the main table. 5390ca3e24bSdrh ** 540b419a926Sdrh ** The arguments to this routine should be the same as the first six 5410ca3e24bSdrh ** arguments to sqliteGenerateConstraintChecks. 5420ca3e24bSdrh */ 5430ca3e24bSdrh void sqliteCompleteInsertion( 5440ca3e24bSdrh Parse *pParse, /* The parser context */ 5450ca3e24bSdrh Table *pTab, /* the table into which we are inserting */ 5460ca3e24bSdrh int base, /* Index of a read/write cursor pointing at pTab */ 5470ca3e24bSdrh char *aIdxUsed, /* Which indices are used. NULL means all are used */ 548b419a926Sdrh int recnoChng, /* True if the record number will change */ 549b419a926Sdrh int isUpdate /* True for UPDATE, False for INSERT */ 5500ca3e24bSdrh ){ 5510ca3e24bSdrh int i; 5520ca3e24bSdrh Vdbe *v; 5530ca3e24bSdrh int nIdx; 5540ca3e24bSdrh Index *pIdx; 5550ca3e24bSdrh 5560ca3e24bSdrh v = sqliteGetVdbe(pParse); 5570ca3e24bSdrh assert( v!=0 ); 558*417be79cSdrh assert( pTab->pSelect==0 ); /* This table is not a VIEW */ 5590ca3e24bSdrh for(nIdx=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, nIdx++){} 5600ca3e24bSdrh for(i=nIdx-1; i>=0; i--){ 5610ca3e24bSdrh if( aIdxUsed && aIdxUsed[i]==0 ) continue; 5620ca3e24bSdrh sqliteVdbeAddOp(v, OP_IdxPut, base+i+1, 0); 5630ca3e24bSdrh } 5640ca3e24bSdrh sqliteVdbeAddOp(v, OP_MakeRecord, pTab->nCol, 0); 5650ca3e24bSdrh sqliteVdbeAddOp(v, OP_PutIntKey, base, 0); 566b419a926Sdrh if( isUpdate && recnoChng ){ 5670ca3e24bSdrh sqliteVdbeAddOp(v, OP_Pop, 1, 0); 5680ca3e24bSdrh } 5690ca3e24bSdrh } 570