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*663fc63aSdrh ** $Id: insert.c,v 1.40 2002/02/02 18:49:20 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; 63ecdc7530Sdrh pTab = sqliteFindTable(db, zTab); 64cce7d176Sdrh sqliteFree(zTab); 65cce7d176Sdrh if( pTab==0 ){ 66cce7d176Sdrh sqliteSetNString(&pParse->zErrMsg, "no such table: ", 0, 67cce7d176Sdrh pTableName->z, pTableName->n, 0); 68cce7d176Sdrh pParse->nErr++; 69cce7d176Sdrh goto insert_cleanup; 70cce7d176Sdrh } 71cce7d176Sdrh if( pTab->readOnly ){ 72cce7d176Sdrh sqliteSetString(&pParse->zErrMsg, "table ", pTab->zName, 73cce7d176Sdrh " may not be modified", 0); 74cce7d176Sdrh pParse->nErr++; 75cce7d176Sdrh goto insert_cleanup; 76cce7d176Sdrh } 771ccde15dSdrh 781ccde15dSdrh /* Allocate a VDBE 791ccde15dSdrh */ 80d8bc7086Sdrh v = sqliteGetVdbe(pParse); 815974a30fSdrh if( v==0 ) goto insert_cleanup; 82*663fc63aSdrh if( pSelect ){ 83*663fc63aSdrh sqliteBeginMultiWriteOperation(pParse); 84*663fc63aSdrh }else{ 851c92853dSdrh sqliteBeginWriteOperation(pParse); 86*663fc63aSdrh } 871ccde15dSdrh 881ccde15dSdrh /* Figure out how many columns of data are supplied. If the data 89c6b52df3Sdrh ** is coming from a SELECT statement, then this step has to generate 901ccde15dSdrh ** all the code to implement the SELECT statement and leave the data 911ccde15dSdrh ** in a temporary table. If data is coming from an expression list, 921ccde15dSdrh ** then we just have to count the number of expressions. 931ccde15dSdrh */ 945974a30fSdrh if( pSelect ){ 955974a30fSdrh int rc; 965974a30fSdrh srcTab = pParse->nTab++; 9799fcd718Sdrh sqliteVdbeAddOp(v, OP_OpenTemp, srcTab, 0); 985974a30fSdrh rc = sqliteSelect(pParse, pSelect, SRT_Table, srcTab); 99daffd0e5Sdrh if( rc || pParse->nErr || sqlite_malloc_failed ) goto insert_cleanup; 1005974a30fSdrh assert( pSelect->pEList ); 101967e8b73Sdrh nColumn = pSelect->pEList->nExpr; 1025974a30fSdrh }else{ 103daffd0e5Sdrh assert( pList!=0 ); 1045974a30fSdrh srcTab = -1; 1055974a30fSdrh assert( pList ); 106967e8b73Sdrh nColumn = pList->nExpr; 1075974a30fSdrh } 1081ccde15dSdrh 1091ccde15dSdrh /* Make sure the number of columns in the source data matches the number 1101ccde15dSdrh ** of columns to be inserted into the table. 1111ccde15dSdrh */ 112967e8b73Sdrh if( pColumn==0 && nColumn!=pTab->nCol ){ 113cce7d176Sdrh char zNum1[30]; 114cce7d176Sdrh char zNum2[30]; 115967e8b73Sdrh sprintf(zNum1,"%d", nColumn); 116cce7d176Sdrh sprintf(zNum2,"%d", pTab->nCol); 117cce7d176Sdrh sqliteSetString(&pParse->zErrMsg, "table ", pTab->zName, 118cce7d176Sdrh " has ", zNum2, " columns but ", 119cce7d176Sdrh zNum1, " values were supplied", 0); 120cce7d176Sdrh pParse->nErr++; 121cce7d176Sdrh goto insert_cleanup; 122cce7d176Sdrh } 123967e8b73Sdrh if( pColumn!=0 && nColumn!=pColumn->nId ){ 124cce7d176Sdrh char zNum1[30]; 125cce7d176Sdrh char zNum2[30]; 126967e8b73Sdrh sprintf(zNum1,"%d", nColumn); 127967e8b73Sdrh sprintf(zNum2,"%d", pColumn->nId); 128cce7d176Sdrh sqliteSetString(&pParse->zErrMsg, zNum1, " values for ", 129cce7d176Sdrh zNum2, " columns", 0); 130cce7d176Sdrh pParse->nErr++; 131cce7d176Sdrh goto insert_cleanup; 132cce7d176Sdrh } 1331ccde15dSdrh 1341ccde15dSdrh /* If the INSERT statement included an IDLIST term, then make sure 1351ccde15dSdrh ** all elements of the IDLIST really are columns of the table and 1361ccde15dSdrh ** remember the column indices. 137c8392586Sdrh ** 138c8392586Sdrh ** If the table has an INTEGER PRIMARY KEY column and that column 139c8392586Sdrh ** is named in the IDLIST, then record in the keyColumn variable 140c8392586Sdrh ** the index into IDLIST of the primary key column. keyColumn is 141c8392586Sdrh ** the index of the primary key as it appears in IDLIST, not as 142c8392586Sdrh ** is appears in the original table. (The index of the primary 143c8392586Sdrh ** key in the original table is pTab->iPKey.) 1441ccde15dSdrh */ 145967e8b73Sdrh if( pColumn ){ 146967e8b73Sdrh for(i=0; i<pColumn->nId; i++){ 147967e8b73Sdrh pColumn->a[i].idx = -1; 148cce7d176Sdrh } 149967e8b73Sdrh for(i=0; i<pColumn->nId; i++){ 150cce7d176Sdrh for(j=0; j<pTab->nCol; j++){ 151967e8b73Sdrh if( sqliteStrICmp(pColumn->a[i].zName, pTab->aCol[j].zName)==0 ){ 152967e8b73Sdrh pColumn->a[i].idx = j; 1534a32431cSdrh if( j==pTab->iPKey ){ 1549aa028daSdrh keyColumn = i; 1554a32431cSdrh } 156cce7d176Sdrh break; 157cce7d176Sdrh } 158cce7d176Sdrh } 159cce7d176Sdrh if( j>=pTab->nCol ){ 160cce7d176Sdrh sqliteSetString(&pParse->zErrMsg, "table ", pTab->zName, 161967e8b73Sdrh " has no column named ", pColumn->a[i].zName, 0); 162cce7d176Sdrh pParse->nErr++; 163cce7d176Sdrh goto insert_cleanup; 164cce7d176Sdrh } 165cce7d176Sdrh } 166cce7d176Sdrh } 1671ccde15dSdrh 168aacc543eSdrh /* If there is no IDLIST term but the table has an integer primary 169c8392586Sdrh ** key, the set the keyColumn variable to the primary key column index 170c8392586Sdrh ** in the original table definition. 1714a32431cSdrh */ 1724a32431cSdrh if( pColumn==0 ){ 1734a32431cSdrh keyColumn = pTab->iPKey; 1744a32431cSdrh } 1754a32431cSdrh 1761ccde15dSdrh /* Open cursors into the table that is received the new data and 1771ccde15dSdrh ** all indices of that table. 1781ccde15dSdrh */ 1795974a30fSdrh base = pParse->nTab; 180f57b3399Sdrh openOp = pTab->isTemp ? OP_OpenWrAux : OP_OpenWrite; 18199fcd718Sdrh sqliteVdbeAddOp(v, openOp, base, pTab->tnum); 18299fcd718Sdrh sqliteVdbeChangeP3(v, -1, pTab->zName, P3_STATIC); 183bed8690fSdrh for(idx=1, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, idx++){ 18499fcd718Sdrh sqliteVdbeAddOp(v, openOp, idx+base, pIdx->tnum); 18599fcd718Sdrh sqliteVdbeChangeP3(v, -1, pIdx->zName, P3_STATIC); 1865974a30fSdrh } 1871ccde15dSdrh 1881ccde15dSdrh /* If the data source is a SELECT statement, then we have to create 1891ccde15dSdrh ** a loop because there might be multiple rows of data. If the data 1901ccde15dSdrh ** source is an expression list, then exactly one row will be inserted 1911ccde15dSdrh ** and the loop is not used. 1921ccde15dSdrh */ 1935974a30fSdrh if( srcTab>=0 ){ 1941bee3d7bSdrh if( db->flags & SQLITE_CountRows ){ 1951bee3d7bSdrh sqliteVdbeAddOp(v, OP_Integer, 0, 0); /* Initialize the row count */ 1961bee3d7bSdrh } 1975974a30fSdrh iBreak = sqliteVdbeMakeLabel(v); 1986b56344dSdrh sqliteVdbeAddOp(v, OP_Rewind, srcTab, iBreak); 1996b56344dSdrh iCont = sqliteVdbeCurrentAddr(v); 200bed8690fSdrh } 2011ccde15dSdrh 2024a32431cSdrh /* Push the record number for the new entry onto the stack. The 2034a32431cSdrh ** record number is a randomly generate integer created by NewRecno 2044a32431cSdrh ** except when the table has an INTEGER PRIMARY KEY column, in which 205b419a926Sdrh ** case the record number is the same as that column. 2061ccde15dSdrh */ 2074a32431cSdrh if( keyColumn>=0 ){ 2084a32431cSdrh if( srcTab>=0 ){ 2094a32431cSdrh sqliteVdbeAddOp(v, OP_Column, srcTab, keyColumn); 2104a32431cSdrh }else{ 2114a32431cSdrh sqliteExprCode(pParse, pList->a[keyColumn].pExpr); 2124a32431cSdrh } 2138aff1015Sdrh sqliteVdbeAddOp(v, OP_MustBeInt, 0, 0); 2144a32431cSdrh }else{ 21599fcd718Sdrh sqliteVdbeAddOp(v, OP_NewRecno, base, 0); 2164a32431cSdrh } 2174a32431cSdrh 218aacc543eSdrh /* Push onto the stack, data for all columns of the new entry, beginning 2194a32431cSdrh ** with the first column. 2204a32431cSdrh */ 221cce7d176Sdrh for(i=0; i<pTab->nCol; i++){ 2224a32431cSdrh if( i==pTab->iPKey ){ 2234a32431cSdrh /* The value of the INTEGER PRIMARY KEY column is always a NULL. 224aacc543eSdrh ** Whenever this column is read, the record number will be substituted 225aacc543eSdrh ** in its place. So will fill this column with a NULL to avoid 226aacc543eSdrh ** taking up data space with information that will never be used. */ 2274a32431cSdrh sqliteVdbeAddOp(v, OP_String, 0, 0); 2284a32431cSdrh continue; 2294a32431cSdrh } 230967e8b73Sdrh if( pColumn==0 ){ 231cce7d176Sdrh j = i; 232cce7d176Sdrh }else{ 233967e8b73Sdrh for(j=0; j<pColumn->nId; j++){ 234967e8b73Sdrh if( pColumn->a[j].idx==i ) break; 235cce7d176Sdrh } 236cce7d176Sdrh } 237967e8b73Sdrh if( pColumn && j>=pColumn->nId ){ 23899fcd718Sdrh sqliteVdbeAddOp(v, OP_String, 0, 0); 23999fcd718Sdrh sqliteVdbeChangeP3(v, -1, pTab->aCol[i].zDflt, P3_STATIC); 2405974a30fSdrh }else if( srcTab>=0 ){ 24199fcd718Sdrh sqliteVdbeAddOp(v, OP_Column, srcTab, i); 242cce7d176Sdrh }else{ 243cce7d176Sdrh sqliteExprCode(pParse, pList->a[j].pExpr); 244cce7d176Sdrh } 245cce7d176Sdrh } 2461ccde15dSdrh 2470ca3e24bSdrh /* Generate code to check constraints and generate index keys and 2480ca3e24bSdrh ** do the insertion. 2494a32431cSdrh */ 2500ca3e24bSdrh endOfLoop = sqliteVdbeMakeLabel(v); 251b419a926Sdrh sqliteGenerateConstraintChecks(pParse, pTab, base, 0,0,0, onError, endOfLoop); 252b419a926Sdrh sqliteCompleteInsertion(pParse, pTab, base, 0,0,0); 2531bee3d7bSdrh 2541bee3d7bSdrh /* If inserting from a SELECT, keep a count of the number of 2551bee3d7bSdrh ** rows inserted. 2561bee3d7bSdrh */ 2571bee3d7bSdrh if( srcTab>=0 && (db->flags & SQLITE_CountRows)!=0 ){ 2581bee3d7bSdrh sqliteVdbeAddOp(v, OP_AddImm, 1, 0); 2591bee3d7bSdrh } 2601bee3d7bSdrh 2611ccde15dSdrh /* The bottom of the loop, if the data source is a SELECT statement 2621ccde15dSdrh */ 2630ca3e24bSdrh sqliteVdbeResolveLabel(v, endOfLoop); 2645974a30fSdrh if( srcTab>=0 ){ 2656b56344dSdrh sqliteVdbeAddOp(v, OP_Next, srcTab, iCont); 26699fcd718Sdrh sqliteVdbeResolveLabel(v, iBreak); 2676b56344dSdrh sqliteVdbeAddOp(v, OP_Close, srcTab, 0); 2686b56344dSdrh } 2696b56344dSdrh sqliteVdbeAddOp(v, OP_Close, base, 0); 2706b56344dSdrh for(idx=1, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, idx++){ 2716b56344dSdrh sqliteVdbeAddOp(v, OP_Close, idx+base, 0); 272cce7d176Sdrh } 2731c92853dSdrh sqliteEndWriteOperation(pParse); 2745e00f6c7Sdrh 2751bee3d7bSdrh /* 2761bee3d7bSdrh ** Return the number of rows inserted. 2771bee3d7bSdrh */ 2781bee3d7bSdrh if( db->flags & SQLITE_CountRows ){ 2791bee3d7bSdrh sqliteVdbeAddOp(v, OP_ColumnCount, 1, 0); 2801bee3d7bSdrh sqliteVdbeAddOp(v, OP_ColumnName, 0, 0); 2811bee3d7bSdrh sqliteVdbeChangeP3(v, -1, "rows inserted", P3_STATIC); 2821bee3d7bSdrh if( srcTab<0 ){ 2831bee3d7bSdrh sqliteVdbeAddOp(v, OP_Integer, 1, 0); 2841bee3d7bSdrh } 2851bee3d7bSdrh sqliteVdbeAddOp(v, OP_Callback, 1, 0); 2861bee3d7bSdrh } 287cce7d176Sdrh 288cce7d176Sdrh insert_cleanup: 2895974a30fSdrh if( pList ) sqliteExprListDelete(pList); 2905974a30fSdrh if( pSelect ) sqliteSelectDelete(pSelect); 291967e8b73Sdrh sqliteIdListDelete(pColumn); 292cce7d176Sdrh } 2939cfcf5d4Sdrh 2949cfcf5d4Sdrh /* 2959cfcf5d4Sdrh ** Generate code to do a constraint check prior to an INSERT or an UPDATE. 2969cfcf5d4Sdrh ** 2979cfcf5d4Sdrh ** When this routine is called, the stack contains (from bottom to top) 2980ca3e24bSdrh ** the following values: 2990ca3e24bSdrh ** 300b419a926Sdrh ** 1. The recno of the row to be updated before it is updated. This 301b419a926Sdrh ** value is omitted unless we are doing an UPDATE that involves a 302b419a926Sdrh ** change to the record number. 3030ca3e24bSdrh ** 304b419a926Sdrh ** 2. The recno of the row after the update. 3050ca3e24bSdrh ** 3060ca3e24bSdrh ** 3. The data in the first column of the entry after the update. 3070ca3e24bSdrh ** 3080ca3e24bSdrh ** i. Data from middle columns... 3090ca3e24bSdrh ** 3100ca3e24bSdrh ** N. The data in the last column of the entry after the update. 3110ca3e24bSdrh ** 312b419a926Sdrh ** The old recno shown as entry (1) above is omitted unless both isUpdate 3131c92853dSdrh ** and recnoChng are 1. isUpdate is true for UPDATEs and false for 3141c92853dSdrh ** INSERTs and recnoChng is true if the record number is being changed. 3150ca3e24bSdrh ** 3160ca3e24bSdrh ** The code generated by this routine pushes additional entries onto 3170ca3e24bSdrh ** the stack which are the keys for new index entries for the new record. 3180ca3e24bSdrh ** The order of index keys is the same as the order of the indices on 3190ca3e24bSdrh ** the pTable->pIndex list. A key is only created for index i if 3200ca3e24bSdrh ** aIdxUsed!=0 and aIdxUsed[i]!=0. 3219cfcf5d4Sdrh ** 3229cfcf5d4Sdrh ** This routine also generates code to check constraints. NOT NULL, 3239cfcf5d4Sdrh ** CHECK, and UNIQUE constraints are all checked. If a constraint fails, 3241c92853dSdrh ** then the appropriate action is performed. There are five possible 3251c92853dSdrh ** actions: ROLLBACK, ABORT, FAIL, REPLACE, and IGNORE. 3269cfcf5d4Sdrh ** 3279cfcf5d4Sdrh ** Constraint type Action What Happens 3289cfcf5d4Sdrh ** --------------- ---------- ---------------------------------------- 3291c92853dSdrh ** any ROLLBACK The current transaction is rolled back and 3309cfcf5d4Sdrh ** sqlite_exec() returns immediately with a 3319cfcf5d4Sdrh ** return code of SQLITE_CONSTRAINT. 3329cfcf5d4Sdrh ** 3331c92853dSdrh ** any ABORT Back out changes from the current command 3341c92853dSdrh ** only (do not do a complete rollback) then 3351c92853dSdrh ** cause sqlite_exec() to return immediately 3361c92853dSdrh ** with SQLITE_CONSTRAINT. 3371c92853dSdrh ** 3381c92853dSdrh ** any FAIL Sqlite_exec() returns immediately with a 3391c92853dSdrh ** return code of SQLITE_CONSTRAINT. The 3401c92853dSdrh ** transaction is not rolled back and any 3411c92853dSdrh ** prior changes are retained. 3421c92853dSdrh ** 3439cfcf5d4Sdrh ** any IGNORE The record number and data is popped from 3449cfcf5d4Sdrh ** the stack and there is an immediate jump 3459cfcf5d4Sdrh ** to label ignoreDest. 3469cfcf5d4Sdrh ** 3479cfcf5d4Sdrh ** NOT NULL REPLACE The NULL value is replace by the default 3489cfcf5d4Sdrh ** value for that column. If the default value 3499cfcf5d4Sdrh ** is NULL, the action is the same as ABORT. 3509cfcf5d4Sdrh ** 3519cfcf5d4Sdrh ** UNIQUE REPLACE The other row that conflicts with the row 3529cfcf5d4Sdrh ** being inserted is removed. 3539cfcf5d4Sdrh ** 3549cfcf5d4Sdrh ** CHECK REPLACE Illegal. The results in an exception. 3559cfcf5d4Sdrh ** 3561c92853dSdrh ** Which action to take is determined by the overrideError parameter. 3571c92853dSdrh ** Or if overrideError==OE_Default, then the pParse->onError parameter 3581c92853dSdrh ** is used. Or if pParse->onError==OE_Default then the onError value 3591c92853dSdrh ** for the constraint is used. 3609cfcf5d4Sdrh ** 3619cfcf5d4Sdrh ** The calling routine must an open read/write cursor for pTab with 3629cfcf5d4Sdrh ** cursor number "base". All indices of pTab must also have open 3639cfcf5d4Sdrh ** read/write cursors with cursor number base+i for the i-th cursor. 3649cfcf5d4Sdrh ** Except, if there is no possibility of a REPLACE action then 3659cfcf5d4Sdrh ** cursors do not need to be open for indices where aIdxUsed[i]==0. 3669cfcf5d4Sdrh ** 3679cfcf5d4Sdrh ** If the isUpdate flag is true, it means that the "base" cursor is 3689cfcf5d4Sdrh ** initially pointing to an entry that is being updated. The isUpdate 3699cfcf5d4Sdrh ** flag causes extra code to be generated so that the "base" cursor 3709cfcf5d4Sdrh ** is still pointing at the same entry after the routine returns. 3719cfcf5d4Sdrh ** Without the isUpdate flag, the "base" cursor might be moved. 3729cfcf5d4Sdrh */ 3739cfcf5d4Sdrh void sqliteGenerateConstraintChecks( 3749cfcf5d4Sdrh Parse *pParse, /* The parser context */ 3759cfcf5d4Sdrh Table *pTab, /* the table into which we are inserting */ 3769cfcf5d4Sdrh int base, /* Index of a read/write cursor pointing at pTab */ 3779cfcf5d4Sdrh char *aIdxUsed, /* Which indices are used. NULL means all are used */ 3780ca3e24bSdrh int recnoChng, /* True if the record number will change */ 379b419a926Sdrh int isUpdate, /* True for UPDATE, False for INSERT */ 3809cfcf5d4Sdrh int overrideError, /* Override onError to this if not OE_Default */ 381b419a926Sdrh int ignoreDest /* Jump to this label on an OE_Ignore resolution */ 3829cfcf5d4Sdrh ){ 3839cfcf5d4Sdrh int i; 3849cfcf5d4Sdrh Vdbe *v; 3859cfcf5d4Sdrh int nCol; 3869cfcf5d4Sdrh int onError; 3879cfcf5d4Sdrh int addr; 3889cfcf5d4Sdrh int extra; 3890ca3e24bSdrh int iCur; 3900ca3e24bSdrh Index *pIdx; 3910ca3e24bSdrh int seenReplace = 0; 3920ca3e24bSdrh int jumpInst; 3930ca3e24bSdrh int contAddr; 394b419a926Sdrh int hasTwoRecnos = (isUpdate && recnoChng); 3959cfcf5d4Sdrh 3969cfcf5d4Sdrh v = sqliteGetVdbe(pParse); 3979cfcf5d4Sdrh assert( v!=0 ); 3989cfcf5d4Sdrh nCol = pTab->nCol; 3991c92853dSdrh if( overrideError==OE_Default ){ 4001c92853dSdrh overrideError = pParse->db->onError; 4011c92853dSdrh } 4029cfcf5d4Sdrh 4039cfcf5d4Sdrh /* Test all NOT NULL constraints. 4049cfcf5d4Sdrh */ 4059cfcf5d4Sdrh for(i=0; i<nCol; i++){ 4060ca3e24bSdrh if( i==pTab->iPKey ){ 4070ca3e24bSdrh /* Fix me: Make sure the INTEGER PRIMARY KEY is not NULL. */ 4080ca3e24bSdrh continue; 4090ca3e24bSdrh } 4109cfcf5d4Sdrh onError = pTab->aCol[i].notNull; 4110ca3e24bSdrh if( onError==OE_None ) continue; 4129cfcf5d4Sdrh if( overrideError!=OE_Default ){ 4139cfcf5d4Sdrh onError = overrideError; 4141c92853dSdrh }else if( onError==OE_Default ){ 4151c92853dSdrh onError = OE_Abort; 4169cfcf5d4Sdrh } 4179cfcf5d4Sdrh if( onError==OE_Replace && pTab->aCol[i].zDflt==0 ){ 4189cfcf5d4Sdrh onError = OE_Abort; 4199cfcf5d4Sdrh } 420ef6764a1Sdrh sqliteVdbeAddOp(v, OP_Dup, nCol-1-i, 1); 421ef6764a1Sdrh addr = sqliteVdbeAddOp(v, OP_NotNull, 0, 0); 4229cfcf5d4Sdrh switch( onError ){ 4231c92853dSdrh case OE_Rollback: 4241c92853dSdrh case OE_Abort: 4251c92853dSdrh case OE_Fail: { 4261c92853dSdrh sqliteVdbeAddOp(v, OP_Halt, SQLITE_CONSTRAINT, onError); 4279cfcf5d4Sdrh break; 4289cfcf5d4Sdrh } 4299cfcf5d4Sdrh case OE_Ignore: { 430b419a926Sdrh sqliteVdbeAddOp(v, OP_Pop, nCol+1+hasTwoRecnos, 0); 4310ca3e24bSdrh sqliteVdbeAddOp(v, OP_Goto, 0, ignoreDest); 4329cfcf5d4Sdrh break; 4339cfcf5d4Sdrh } 4349cfcf5d4Sdrh case OE_Replace: { 4359cfcf5d4Sdrh sqliteVdbeAddOp(v, OP_String, 0, 0); 4369cfcf5d4Sdrh sqliteVdbeChangeP3(v, -1, pTab->aCol[i].zDflt, P3_STATIC); 4379cfcf5d4Sdrh sqliteVdbeAddOp(v, OP_Push, nCol-i, 0); 4389cfcf5d4Sdrh break; 4399cfcf5d4Sdrh } 4400ca3e24bSdrh default: assert(0); 4419cfcf5d4Sdrh } 442ef6764a1Sdrh sqliteVdbeChangeP2(v, addr, sqliteVdbeCurrentAddr(v)); 4439cfcf5d4Sdrh } 4449cfcf5d4Sdrh 4459cfcf5d4Sdrh /* Test all CHECK constraints 4469cfcf5d4Sdrh */ 4479cfcf5d4Sdrh 4489cfcf5d4Sdrh /* Test all UNIQUE constraints. Add index records as we go. 4499cfcf5d4Sdrh */ 450b419a926Sdrh if( (recnoChng || !isUpdate) && pTab->iPKey>=0 && pTab->keyConf!=OE_Replace 4510ca3e24bSdrh && overrideError!=OE_Replace ){ 4520ca3e24bSdrh sqliteVdbeAddOp(v, OP_Dup, nCol, 1); 4530ca3e24bSdrh jumpInst = sqliteVdbeAddOp(v, OP_NotExists, base, 0); 4540ca3e24bSdrh onError = pTab->keyConf; 4550ca3e24bSdrh if( overrideError!=OE_Default ){ 4560ca3e24bSdrh onError = overrideError; 4571c92853dSdrh }else if( onError==OE_Default ){ 4581c92853dSdrh onError = OE_Abort; 4590ca3e24bSdrh } 4600ca3e24bSdrh switch( onError ){ 4611c92853dSdrh case OE_Rollback: 4621c92853dSdrh case OE_Abort: 4631c92853dSdrh case OE_Fail: { 4641c92853dSdrh sqliteVdbeAddOp(v, OP_Halt, SQLITE_CONSTRAINT, onError); 4650ca3e24bSdrh break; 4660ca3e24bSdrh } 4670ca3e24bSdrh case OE_Ignore: { 468b419a926Sdrh sqliteVdbeAddOp(v, OP_Pop, nCol+1+hasTwoRecnos, 0); 4690ca3e24bSdrh sqliteVdbeAddOp(v, OP_Goto, 0, ignoreDest); 4700ca3e24bSdrh break; 4710ca3e24bSdrh } 4720ca3e24bSdrh default: assert(0); 4730ca3e24bSdrh } 4740ca3e24bSdrh contAddr = sqliteVdbeCurrentAddr(v); 4750ca3e24bSdrh sqliteVdbeChangeP2(v, jumpInst, contAddr); 4760ca3e24bSdrh if( isUpdate ){ 4770ca3e24bSdrh sqliteVdbeAddOp(v, OP_Dup, nCol+1, 1); 4780ca3e24bSdrh sqliteVdbeAddOp(v, OP_MoveTo, base, 0); 4790ca3e24bSdrh } 4800ca3e24bSdrh } 4819cfcf5d4Sdrh extra = 0; 4829cfcf5d4Sdrh for(extra=(-1), iCur=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, iCur++){ 4839cfcf5d4Sdrh if( aIdxUsed && aIdxUsed[iCur]==0 ) continue; 4849cfcf5d4Sdrh extra++; 4859cfcf5d4Sdrh sqliteVdbeAddOp(v, OP_Dup, nCol+extra, 1); 4869cfcf5d4Sdrh for(i=0; i<pIdx->nColumn; i++){ 4879cfcf5d4Sdrh int idx = pIdx->aiColumn[i]; 4889cfcf5d4Sdrh if( idx==pTab->iPKey ){ 4890ca3e24bSdrh sqliteVdbeAddOp(v, OP_Dup, i+extra+nCol+1, 1); 4909cfcf5d4Sdrh }else{ 4910ca3e24bSdrh sqliteVdbeAddOp(v, OP_Dup, i+extra+nCol-idx, 1); 4929cfcf5d4Sdrh } 4939cfcf5d4Sdrh } 4949cfcf5d4Sdrh sqliteVdbeAddOp(v, OP_MakeIdxKey, pIdx->nColumn, 0); 4959cfcf5d4Sdrh onError = pIdx->onError; 4969cfcf5d4Sdrh if( onError==OE_None ) continue; 4979cfcf5d4Sdrh if( overrideError!=OE_Default ){ 4989cfcf5d4Sdrh onError = overrideError; 4991c92853dSdrh }else if( onError==OE_Default ){ 5001c92853dSdrh 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