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*a76b5dfcSdrh ** $Id: insert.c,v 1.45 2002/02/23 02:32:10 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; 63*a76b5dfcSdrh pTab = sqliteTableNameToTable(pParse, zTab); 64cce7d176Sdrh sqliteFree(zTab); 65*a76b5dfcSdrh if( pTab==0 ) goto insert_cleanup; 661ccde15dSdrh 671ccde15dSdrh /* Allocate a VDBE 681ccde15dSdrh */ 69d8bc7086Sdrh v = sqliteGetVdbe(pParse); 705974a30fSdrh if( v==0 ) goto insert_cleanup; 71663fc63aSdrh if( pSelect ){ 72663fc63aSdrh sqliteBeginMultiWriteOperation(pParse); 73663fc63aSdrh }else{ 741c92853dSdrh sqliteBeginWriteOperation(pParse); 75663fc63aSdrh } 761ccde15dSdrh 771ccde15dSdrh /* Figure out how many columns of data are supplied. If the data 78c6b52df3Sdrh ** is coming from a SELECT statement, then this step has to generate 791ccde15dSdrh ** all the code to implement the SELECT statement and leave the data 801ccde15dSdrh ** in a temporary table. If data is coming from an expression list, 811ccde15dSdrh ** then we just have to count the number of expressions. 821ccde15dSdrh */ 835974a30fSdrh if( pSelect ){ 845974a30fSdrh int rc; 855974a30fSdrh srcTab = pParse->nTab++; 8699fcd718Sdrh sqliteVdbeAddOp(v, OP_OpenTemp, srcTab, 0); 875974a30fSdrh rc = sqliteSelect(pParse, pSelect, SRT_Table, srcTab); 88daffd0e5Sdrh if( rc || pParse->nErr || sqlite_malloc_failed ) goto insert_cleanup; 895974a30fSdrh assert( pSelect->pEList ); 90967e8b73Sdrh nColumn = pSelect->pEList->nExpr; 915974a30fSdrh }else{ 92e64e7b20Sdrh IdList dummy; 93daffd0e5Sdrh assert( pList!=0 ); 945974a30fSdrh srcTab = -1; 955974a30fSdrh assert( pList ); 96967e8b73Sdrh nColumn = pList->nExpr; 97e64e7b20Sdrh for(i=0; i<nColumn; i++){ 98e64e7b20Sdrh sqliteExprResolveInSelect(pParse, pList->a[i].pExpr); 99e64e7b20Sdrh } 100e64e7b20Sdrh dummy.nId = 0; 101e64e7b20Sdrh for(i=0; i<nColumn; i++){ 102e64e7b20Sdrh if( sqliteExprResolveIds(pParse, &dummy, 0, pList->a[i].pExpr) ){ 103e64e7b20Sdrh goto insert_cleanup; 104e64e7b20Sdrh } 105e64e7b20Sdrh } 1065974a30fSdrh } 1071ccde15dSdrh 1081ccde15dSdrh /* Make sure the number of columns in the source data matches the number 1091ccde15dSdrh ** of columns to be inserted into the table. 1101ccde15dSdrh */ 111967e8b73Sdrh if( pColumn==0 && nColumn!=pTab->nCol ){ 112cce7d176Sdrh char zNum1[30]; 113cce7d176Sdrh char zNum2[30]; 114967e8b73Sdrh sprintf(zNum1,"%d", nColumn); 115cce7d176Sdrh sprintf(zNum2,"%d", pTab->nCol); 116cce7d176Sdrh sqliteSetString(&pParse->zErrMsg, "table ", pTab->zName, 117cce7d176Sdrh " has ", zNum2, " columns but ", 118cce7d176Sdrh zNum1, " values were supplied", 0); 119cce7d176Sdrh pParse->nErr++; 120cce7d176Sdrh goto insert_cleanup; 121cce7d176Sdrh } 122967e8b73Sdrh if( pColumn!=0 && nColumn!=pColumn->nId ){ 123cce7d176Sdrh char zNum1[30]; 124cce7d176Sdrh char zNum2[30]; 125967e8b73Sdrh sprintf(zNum1,"%d", nColumn); 126967e8b73Sdrh sprintf(zNum2,"%d", pColumn->nId); 127cce7d176Sdrh sqliteSetString(&pParse->zErrMsg, zNum1, " values for ", 128cce7d176Sdrh zNum2, " columns", 0); 129cce7d176Sdrh pParse->nErr++; 130cce7d176Sdrh goto insert_cleanup; 131cce7d176Sdrh } 1321ccde15dSdrh 1331ccde15dSdrh /* If the INSERT statement included an IDLIST term, then make sure 1341ccde15dSdrh ** all elements of the IDLIST really are columns of the table and 1351ccde15dSdrh ** remember the column indices. 136c8392586Sdrh ** 137c8392586Sdrh ** If the table has an INTEGER PRIMARY KEY column and that column 138c8392586Sdrh ** is named in the IDLIST, then record in the keyColumn variable 139c8392586Sdrh ** the index into IDLIST of the primary key column. keyColumn is 140c8392586Sdrh ** the index of the primary key as it appears in IDLIST, not as 141c8392586Sdrh ** is appears in the original table. (The index of the primary 142c8392586Sdrh ** key in the original table is pTab->iPKey.) 1431ccde15dSdrh */ 144967e8b73Sdrh if( pColumn ){ 145967e8b73Sdrh for(i=0; i<pColumn->nId; i++){ 146967e8b73Sdrh pColumn->a[i].idx = -1; 147cce7d176Sdrh } 148967e8b73Sdrh for(i=0; i<pColumn->nId; i++){ 149cce7d176Sdrh for(j=0; j<pTab->nCol; j++){ 150967e8b73Sdrh if( sqliteStrICmp(pColumn->a[i].zName, pTab->aCol[j].zName)==0 ){ 151967e8b73Sdrh pColumn->a[i].idx = j; 1524a32431cSdrh if( j==pTab->iPKey ){ 1539aa028daSdrh keyColumn = i; 1544a32431cSdrh } 155cce7d176Sdrh break; 156cce7d176Sdrh } 157cce7d176Sdrh } 158cce7d176Sdrh if( j>=pTab->nCol ){ 159cce7d176Sdrh sqliteSetString(&pParse->zErrMsg, "table ", pTab->zName, 160967e8b73Sdrh " has no column named ", pColumn->a[i].zName, 0); 161cce7d176Sdrh pParse->nErr++; 162cce7d176Sdrh goto insert_cleanup; 163cce7d176Sdrh } 164cce7d176Sdrh } 165cce7d176Sdrh } 1661ccde15dSdrh 167aacc543eSdrh /* If there is no IDLIST term but the table has an integer primary 168c8392586Sdrh ** key, the set the keyColumn variable to the primary key column index 169c8392586Sdrh ** in the original table definition. 1704a32431cSdrh */ 1714a32431cSdrh if( pColumn==0 ){ 1724a32431cSdrh keyColumn = pTab->iPKey; 1734a32431cSdrh } 1744a32431cSdrh 1751ccde15dSdrh /* Open cursors into the table that is received the new data and 1761ccde15dSdrh ** all indices of that table. 1771ccde15dSdrh */ 1785974a30fSdrh base = pParse->nTab; 179f57b3399Sdrh openOp = pTab->isTemp ? OP_OpenWrAux : OP_OpenWrite; 18099fcd718Sdrh sqliteVdbeAddOp(v, openOp, base, pTab->tnum); 18199fcd718Sdrh sqliteVdbeChangeP3(v, -1, pTab->zName, P3_STATIC); 182bed8690fSdrh for(idx=1, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, idx++){ 18399fcd718Sdrh sqliteVdbeAddOp(v, openOp, idx+base, pIdx->tnum); 18499fcd718Sdrh sqliteVdbeChangeP3(v, -1, pIdx->zName, P3_STATIC); 1855974a30fSdrh } 1861ccde15dSdrh 1871ccde15dSdrh /* If the data source is a SELECT statement, then we have to create 1881ccde15dSdrh ** a loop because there might be multiple rows of data. If the data 1891ccde15dSdrh ** source is an expression list, then exactly one row will be inserted 1901ccde15dSdrh ** and the loop is not used. 1911ccde15dSdrh */ 1925974a30fSdrh if( srcTab>=0 ){ 1931bee3d7bSdrh if( db->flags & SQLITE_CountRows ){ 1941bee3d7bSdrh sqliteVdbeAddOp(v, OP_Integer, 0, 0); /* Initialize the row count */ 1951bee3d7bSdrh } 1965974a30fSdrh iBreak = sqliteVdbeMakeLabel(v); 1976b56344dSdrh sqliteVdbeAddOp(v, OP_Rewind, srcTab, iBreak); 1986b56344dSdrh iCont = sqliteVdbeCurrentAddr(v); 199bed8690fSdrh } 2001ccde15dSdrh 2014a32431cSdrh /* Push the record number for the new entry onto the stack. The 2024a32431cSdrh ** record number is a randomly generate integer created by NewRecno 2034a32431cSdrh ** except when the table has an INTEGER PRIMARY KEY column, in which 204b419a926Sdrh ** case the record number is the same as that column. 2051ccde15dSdrh */ 2064a32431cSdrh if( keyColumn>=0 ){ 2074a32431cSdrh if( srcTab>=0 ){ 2084a32431cSdrh sqliteVdbeAddOp(v, OP_Column, srcTab, keyColumn); 2094a32431cSdrh }else{ 2104a32431cSdrh sqliteExprCode(pParse, pList->a[keyColumn].pExpr); 2114a32431cSdrh } 2128aff1015Sdrh sqliteVdbeAddOp(v, OP_MustBeInt, 0, 0); 2134a32431cSdrh }else{ 21499fcd718Sdrh sqliteVdbeAddOp(v, OP_NewRecno, base, 0); 2154a32431cSdrh } 2164a32431cSdrh 217aacc543eSdrh /* Push onto the stack, data for all columns of the new entry, beginning 2184a32431cSdrh ** with the first column. 2194a32431cSdrh */ 220cce7d176Sdrh for(i=0; i<pTab->nCol; i++){ 2214a32431cSdrh if( i==pTab->iPKey ){ 2224a32431cSdrh /* The value of the INTEGER PRIMARY KEY column is always a NULL. 223aacc543eSdrh ** Whenever this column is read, the record number will be substituted 224aacc543eSdrh ** in its place. So will fill this column with a NULL to avoid 225aacc543eSdrh ** taking up data space with information that will never be used. */ 2264a32431cSdrh sqliteVdbeAddOp(v, OP_String, 0, 0); 2274a32431cSdrh continue; 2284a32431cSdrh } 229967e8b73Sdrh if( pColumn==0 ){ 230cce7d176Sdrh j = i; 231cce7d176Sdrh }else{ 232967e8b73Sdrh for(j=0; j<pColumn->nId; j++){ 233967e8b73Sdrh if( pColumn->a[j].idx==i ) break; 234cce7d176Sdrh } 235cce7d176Sdrh } 236967e8b73Sdrh if( pColumn && j>=pColumn->nId ){ 23799fcd718Sdrh sqliteVdbeAddOp(v, OP_String, 0, 0); 23899fcd718Sdrh sqliteVdbeChangeP3(v, -1, pTab->aCol[i].zDflt, P3_STATIC); 2395974a30fSdrh }else if( srcTab>=0 ){ 24024e97df9Sdrh sqliteVdbeAddOp(v, OP_Column, srcTab, j); 241cce7d176Sdrh }else{ 242cce7d176Sdrh sqliteExprCode(pParse, pList->a[j].pExpr); 243cce7d176Sdrh } 244cce7d176Sdrh } 2451ccde15dSdrh 2460ca3e24bSdrh /* Generate code to check constraints and generate index keys and 2470ca3e24bSdrh ** do the insertion. 2484a32431cSdrh */ 2490ca3e24bSdrh endOfLoop = sqliteVdbeMakeLabel(v); 250b419a926Sdrh sqliteGenerateConstraintChecks(pParse, pTab, base, 0,0,0, onError, endOfLoop); 251b419a926Sdrh sqliteCompleteInsertion(pParse, pTab, base, 0,0,0); 2521bee3d7bSdrh 2531bee3d7bSdrh /* If inserting from a SELECT, keep a count of the number of 2541bee3d7bSdrh ** rows inserted. 2551bee3d7bSdrh */ 2561bee3d7bSdrh if( srcTab>=0 && (db->flags & SQLITE_CountRows)!=0 ){ 2571bee3d7bSdrh sqliteVdbeAddOp(v, OP_AddImm, 1, 0); 2581bee3d7bSdrh } 2591bee3d7bSdrh 2601ccde15dSdrh /* The bottom of the loop, if the data source is a SELECT statement 2611ccde15dSdrh */ 2620ca3e24bSdrh sqliteVdbeResolveLabel(v, endOfLoop); 2635974a30fSdrh if( srcTab>=0 ){ 2646b56344dSdrh sqliteVdbeAddOp(v, OP_Next, srcTab, iCont); 26599fcd718Sdrh sqliteVdbeResolveLabel(v, iBreak); 2666b56344dSdrh sqliteVdbeAddOp(v, OP_Close, srcTab, 0); 2676b56344dSdrh } 2686b56344dSdrh sqliteVdbeAddOp(v, OP_Close, base, 0); 2696b56344dSdrh for(idx=1, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, idx++){ 2706b56344dSdrh sqliteVdbeAddOp(v, OP_Close, idx+base, 0); 271cce7d176Sdrh } 2721c92853dSdrh sqliteEndWriteOperation(pParse); 2735e00f6c7Sdrh 2741bee3d7bSdrh /* 2751bee3d7bSdrh ** Return the number of rows inserted. 2761bee3d7bSdrh */ 2771bee3d7bSdrh if( db->flags & SQLITE_CountRows ){ 2781bee3d7bSdrh sqliteVdbeAddOp(v, OP_ColumnCount, 1, 0); 2791bee3d7bSdrh sqliteVdbeAddOp(v, OP_ColumnName, 0, 0); 2801bee3d7bSdrh sqliteVdbeChangeP3(v, -1, "rows inserted", P3_STATIC); 2811bee3d7bSdrh if( srcTab<0 ){ 2821bee3d7bSdrh sqliteVdbeAddOp(v, OP_Integer, 1, 0); 2831bee3d7bSdrh } 2841bee3d7bSdrh sqliteVdbeAddOp(v, OP_Callback, 1, 0); 2851bee3d7bSdrh } 286cce7d176Sdrh 287cce7d176Sdrh insert_cleanup: 2885974a30fSdrh if( pList ) sqliteExprListDelete(pList); 2895974a30fSdrh if( pSelect ) sqliteSelectDelete(pSelect); 290967e8b73Sdrh sqliteIdListDelete(pColumn); 291cce7d176Sdrh } 2929cfcf5d4Sdrh 2939cfcf5d4Sdrh /* 2949cfcf5d4Sdrh ** Generate code to do a constraint check prior to an INSERT or an UPDATE. 2959cfcf5d4Sdrh ** 2969cfcf5d4Sdrh ** When this routine is called, the stack contains (from bottom to top) 2970ca3e24bSdrh ** the following values: 2980ca3e24bSdrh ** 299b419a926Sdrh ** 1. The recno of the row to be updated before it is updated. This 300b419a926Sdrh ** value is omitted unless we are doing an UPDATE that involves a 301b419a926Sdrh ** change to the record number. 3020ca3e24bSdrh ** 303b419a926Sdrh ** 2. The recno of the row after the update. 3040ca3e24bSdrh ** 3050ca3e24bSdrh ** 3. The data in the first column of the entry after the update. 3060ca3e24bSdrh ** 3070ca3e24bSdrh ** i. Data from middle columns... 3080ca3e24bSdrh ** 3090ca3e24bSdrh ** N. The data in the last column of the entry after the update. 3100ca3e24bSdrh ** 311b419a926Sdrh ** The old recno shown as entry (1) above is omitted unless both isUpdate 3121c92853dSdrh ** and recnoChng are 1. isUpdate is true for UPDATEs and false for 3131c92853dSdrh ** INSERTs and recnoChng is true if the record number is being changed. 3140ca3e24bSdrh ** 3150ca3e24bSdrh ** The code generated by this routine pushes additional entries onto 3160ca3e24bSdrh ** the stack which are the keys for new index entries for the new record. 3170ca3e24bSdrh ** The order of index keys is the same as the order of the indices on 3180ca3e24bSdrh ** the pTable->pIndex list. A key is only created for index i if 3190ca3e24bSdrh ** aIdxUsed!=0 and aIdxUsed[i]!=0. 3209cfcf5d4Sdrh ** 3219cfcf5d4Sdrh ** This routine also generates code to check constraints. NOT NULL, 3229cfcf5d4Sdrh ** CHECK, and UNIQUE constraints are all checked. If a constraint fails, 3231c92853dSdrh ** then the appropriate action is performed. There are five possible 3241c92853dSdrh ** actions: ROLLBACK, ABORT, FAIL, REPLACE, and IGNORE. 3259cfcf5d4Sdrh ** 3269cfcf5d4Sdrh ** Constraint type Action What Happens 3279cfcf5d4Sdrh ** --------------- ---------- ---------------------------------------- 3281c92853dSdrh ** any ROLLBACK The current transaction is rolled back and 3299cfcf5d4Sdrh ** sqlite_exec() returns immediately with a 3309cfcf5d4Sdrh ** return code of SQLITE_CONSTRAINT. 3319cfcf5d4Sdrh ** 3321c92853dSdrh ** any ABORT Back out changes from the current command 3331c92853dSdrh ** only (do not do a complete rollback) then 3341c92853dSdrh ** cause sqlite_exec() to return immediately 3351c92853dSdrh ** with SQLITE_CONSTRAINT. 3361c92853dSdrh ** 3371c92853dSdrh ** any FAIL Sqlite_exec() returns immediately with a 3381c92853dSdrh ** return code of SQLITE_CONSTRAINT. The 3391c92853dSdrh ** transaction is not rolled back and any 3401c92853dSdrh ** prior changes are retained. 3411c92853dSdrh ** 3429cfcf5d4Sdrh ** any IGNORE The record number and data is popped from 3439cfcf5d4Sdrh ** the stack and there is an immediate jump 3449cfcf5d4Sdrh ** to label ignoreDest. 3459cfcf5d4Sdrh ** 3469cfcf5d4Sdrh ** NOT NULL REPLACE The NULL value is replace by the default 3479cfcf5d4Sdrh ** value for that column. If the default value 3489cfcf5d4Sdrh ** is NULL, the action is the same as ABORT. 3499cfcf5d4Sdrh ** 3509cfcf5d4Sdrh ** UNIQUE REPLACE The other row that conflicts with the row 3519cfcf5d4Sdrh ** being inserted is removed. 3529cfcf5d4Sdrh ** 3539cfcf5d4Sdrh ** CHECK REPLACE Illegal. The results in an exception. 3549cfcf5d4Sdrh ** 3551c92853dSdrh ** Which action to take is determined by the overrideError parameter. 3561c92853dSdrh ** Or if overrideError==OE_Default, then the pParse->onError parameter 3571c92853dSdrh ** is used. Or if pParse->onError==OE_Default then the onError value 3581c92853dSdrh ** for the constraint is used. 3599cfcf5d4Sdrh ** 360aaab5725Sdrh ** The calling routine must open a read/write cursor for pTab with 3619cfcf5d4Sdrh ** cursor number "base". All indices of pTab must also have open 3629cfcf5d4Sdrh ** read/write cursors with cursor number base+i for the i-th cursor. 3639cfcf5d4Sdrh ** Except, if there is no possibility of a REPLACE action then 3649cfcf5d4Sdrh ** cursors do not need to be open for indices where aIdxUsed[i]==0. 3659cfcf5d4Sdrh ** 3669cfcf5d4Sdrh ** If the isUpdate flag is true, it means that the "base" cursor is 3679cfcf5d4Sdrh ** initially pointing to an entry that is being updated. The isUpdate 3689cfcf5d4Sdrh ** flag causes extra code to be generated so that the "base" cursor 3699cfcf5d4Sdrh ** is still pointing at the same entry after the routine returns. 3709cfcf5d4Sdrh ** Without the isUpdate flag, the "base" cursor might be moved. 3719cfcf5d4Sdrh */ 3729cfcf5d4Sdrh void sqliteGenerateConstraintChecks( 3739cfcf5d4Sdrh Parse *pParse, /* The parser context */ 3749cfcf5d4Sdrh Table *pTab, /* the table into which we are inserting */ 3759cfcf5d4Sdrh int base, /* Index of a read/write cursor pointing at pTab */ 3769cfcf5d4Sdrh char *aIdxUsed, /* Which indices are used. NULL means all are used */ 3770ca3e24bSdrh int recnoChng, /* True if the record number will change */ 378b419a926Sdrh int isUpdate, /* True for UPDATE, False for INSERT */ 3799cfcf5d4Sdrh int overrideError, /* Override onError to this if not OE_Default */ 380b419a926Sdrh int ignoreDest /* Jump to this label on an OE_Ignore resolution */ 3819cfcf5d4Sdrh ){ 3829cfcf5d4Sdrh int i; 3839cfcf5d4Sdrh Vdbe *v; 3849cfcf5d4Sdrh int nCol; 3859cfcf5d4Sdrh int onError; 3869cfcf5d4Sdrh int addr; 3879cfcf5d4Sdrh int extra; 3880ca3e24bSdrh int iCur; 3890ca3e24bSdrh Index *pIdx; 3900ca3e24bSdrh int seenReplace = 0; 3910ca3e24bSdrh int jumpInst; 3920ca3e24bSdrh int contAddr; 393b419a926Sdrh int hasTwoRecnos = (isUpdate && recnoChng); 3949cfcf5d4Sdrh 3959cfcf5d4Sdrh v = sqliteGetVdbe(pParse); 3969cfcf5d4Sdrh assert( v!=0 ); 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 ); 5580ca3e24bSdrh for(nIdx=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, nIdx++){} 5590ca3e24bSdrh for(i=nIdx-1; i>=0; i--){ 5600ca3e24bSdrh if( aIdxUsed && aIdxUsed[i]==0 ) continue; 5610ca3e24bSdrh sqliteVdbeAddOp(v, OP_IdxPut, base+i+1, 0); 5620ca3e24bSdrh } 5630ca3e24bSdrh sqliteVdbeAddOp(v, OP_MakeRecord, pTab->nCol, 0); 5640ca3e24bSdrh sqliteVdbeAddOp(v, OP_PutIntKey, base, 0); 565b419a926Sdrh if( isUpdate && recnoChng ){ 5660ca3e24bSdrh sqliteVdbeAddOp(v, OP_Pop, 1, 0); 5670ca3e24bSdrh } 5680ca3e24bSdrh } 569