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*bb37fdc7Sdrh ** $Id: insert.c,v 1.36 2002/01/30 00:54:56 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; 82ecdc7530Sdrh if( (db->flags & SQLITE_InTrans)==0 ){ 8399fcd718Sdrh sqliteVdbeAddOp(v, OP_Transaction, 0, 0); 8499fcd718Sdrh sqliteVdbeAddOp(v, OP_VerifyCookie, db->schema_cookie, 0); 85ecdc7530Sdrh pParse->schemaVerified = 1; 865e00f6c7Sdrh } 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 2050ca3e24bSdrh ** case the record number is the same as that column. May a copy 2060ca3e24bSdrh ** because sqliteGenerateConstraintChecks() requires two copies of 2070ca3e24bSdrh ** the record number. 2081ccde15dSdrh */ 2094a32431cSdrh if( keyColumn>=0 ){ 2104a32431cSdrh if( srcTab>=0 ){ 2114a32431cSdrh sqliteVdbeAddOp(v, OP_Column, srcTab, keyColumn); 2124a32431cSdrh }else{ 2134a32431cSdrh sqliteExprCode(pParse, pList->a[keyColumn].pExpr); 2144a32431cSdrh } 2158aff1015Sdrh sqliteVdbeAddOp(v, OP_MustBeInt, 0, 0); 2164a32431cSdrh }else{ 21799fcd718Sdrh sqliteVdbeAddOp(v, OP_NewRecno, base, 0); 2184a32431cSdrh } 21999fcd718Sdrh sqliteVdbeAddOp(v, OP_Dup, 0, 0); 2204a32431cSdrh 221aacc543eSdrh /* Push onto the stack, data for all columns of the new entry, beginning 2224a32431cSdrh ** with the first column. 2234a32431cSdrh */ 224cce7d176Sdrh for(i=0; i<pTab->nCol; i++){ 2254a32431cSdrh if( i==pTab->iPKey ){ 2264a32431cSdrh /* The value of the INTEGER PRIMARY KEY column is always a NULL. 227aacc543eSdrh ** Whenever this column is read, the record number will be substituted 228aacc543eSdrh ** in its place. So will fill this column with a NULL to avoid 229aacc543eSdrh ** taking up data space with information that will never be used. */ 2304a32431cSdrh sqliteVdbeAddOp(v, OP_String, 0, 0); 2314a32431cSdrh continue; 2324a32431cSdrh } 233967e8b73Sdrh if( pColumn==0 ){ 234cce7d176Sdrh j = i; 235cce7d176Sdrh }else{ 236967e8b73Sdrh for(j=0; j<pColumn->nId; j++){ 237967e8b73Sdrh if( pColumn->a[j].idx==i ) break; 238cce7d176Sdrh } 239cce7d176Sdrh } 240967e8b73Sdrh if( pColumn && j>=pColumn->nId ){ 24199fcd718Sdrh sqliteVdbeAddOp(v, OP_String, 0, 0); 24299fcd718Sdrh sqliteVdbeChangeP3(v, -1, pTab->aCol[i].zDflt, P3_STATIC); 2435974a30fSdrh }else if( srcTab>=0 ){ 24499fcd718Sdrh sqliteVdbeAddOp(v, OP_Column, srcTab, i); 245cce7d176Sdrh }else{ 246cce7d176Sdrh sqliteExprCode(pParse, pList->a[j].pExpr); 247cce7d176Sdrh } 248cce7d176Sdrh } 2491ccde15dSdrh 2500ca3e24bSdrh /* Generate code to check constraints and generate index keys and 2510ca3e24bSdrh ** do the insertion. 2524a32431cSdrh */ 2530ca3e24bSdrh endOfLoop = sqliteVdbeMakeLabel(v); 2540ca3e24bSdrh sqliteGenerateConstraintChecks(pParse, pTab, base, 0,1,onError,endOfLoop,0); 2550ca3e24bSdrh sqliteCompleteInsertion(pParse, pTab, base, 0, 1); 2561bee3d7bSdrh 2571bee3d7bSdrh /* If inserting from a SELECT, keep a count of the number of 2581bee3d7bSdrh ** rows inserted. 2591bee3d7bSdrh */ 2601bee3d7bSdrh if( srcTab>=0 && (db->flags & SQLITE_CountRows)!=0 ){ 2611bee3d7bSdrh sqliteVdbeAddOp(v, OP_AddImm, 1, 0); 2621bee3d7bSdrh } 2631bee3d7bSdrh 2641ccde15dSdrh /* The bottom of the loop, if the data source is a SELECT statement 2651ccde15dSdrh */ 2660ca3e24bSdrh sqliteVdbeResolveLabel(v, endOfLoop); 2675974a30fSdrh if( srcTab>=0 ){ 2686b56344dSdrh sqliteVdbeAddOp(v, OP_Next, srcTab, iCont); 26999fcd718Sdrh sqliteVdbeResolveLabel(v, iBreak); 2706b56344dSdrh sqliteVdbeAddOp(v, OP_Close, srcTab, 0); 2716b56344dSdrh } 2726b56344dSdrh sqliteVdbeAddOp(v, OP_Close, base, 0); 2736b56344dSdrh for(idx=1, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, idx++){ 2746b56344dSdrh sqliteVdbeAddOp(v, OP_Close, idx+base, 0); 275cce7d176Sdrh } 276ecdc7530Sdrh if( (db->flags & SQLITE_InTrans)==0 ){ 27799fcd718Sdrh sqliteVdbeAddOp(v, OP_Commit, 0, 0); 2785e00f6c7Sdrh } 2795e00f6c7Sdrh 2801bee3d7bSdrh /* 2811bee3d7bSdrh ** Return the number of rows inserted. 2821bee3d7bSdrh */ 2831bee3d7bSdrh if( db->flags & SQLITE_CountRows ){ 2841bee3d7bSdrh sqliteVdbeAddOp(v, OP_ColumnCount, 1, 0); 2851bee3d7bSdrh sqliteVdbeAddOp(v, OP_ColumnName, 0, 0); 2861bee3d7bSdrh sqliteVdbeChangeP3(v, -1, "rows inserted", P3_STATIC); 2871bee3d7bSdrh if( srcTab<0 ){ 2881bee3d7bSdrh sqliteVdbeAddOp(v, OP_Integer, 1, 0); 2891bee3d7bSdrh } 2901bee3d7bSdrh sqliteVdbeAddOp(v, OP_Callback, 1, 0); 2911bee3d7bSdrh } 292cce7d176Sdrh 293cce7d176Sdrh insert_cleanup: 2945974a30fSdrh if( pList ) sqliteExprListDelete(pList); 2955974a30fSdrh if( pSelect ) sqliteSelectDelete(pSelect); 296967e8b73Sdrh sqliteIdListDelete(pColumn); 297cce7d176Sdrh } 2989cfcf5d4Sdrh 2999cfcf5d4Sdrh /* 3009cfcf5d4Sdrh ** Generate code to do a constraint check prior to an INSERT or an UPDATE. 3019cfcf5d4Sdrh ** 3029cfcf5d4Sdrh ** When this routine is called, the stack contains (from bottom to top) 3030ca3e24bSdrh ** the following values: 3040ca3e24bSdrh ** 3050ca3e24bSdrh ** 1. The recno of the row to be updated before it is updated. 3060ca3e24bSdrh ** 3070ca3e24bSdrh ** 2. The recno of the row after the update. (This is usually the 3080ca3e24bSdrh ** same as (1) but can be different if an UPDATE changes an 3090ca3e24bSdrh ** INTEGER PRIMARY KEY column.) 3100ca3e24bSdrh ** 3110ca3e24bSdrh ** 3. The data in the first column of the entry after the update. 3120ca3e24bSdrh ** 3130ca3e24bSdrh ** i. Data from middle columns... 3140ca3e24bSdrh ** 3150ca3e24bSdrh ** N. The data in the last column of the entry after the update. 3160ca3e24bSdrh ** 3170ca3e24bSdrh ** The old recno shown as entry (1) above is omitted if the recnoChng 3180ca3e24bSdrh ** parameter is 0. recnoChange is true if the record number is changing 3190ca3e24bSdrh ** and false if not. 3200ca3e24bSdrh ** 3210ca3e24bSdrh ** The code generated by this routine pushes additional entries onto 3220ca3e24bSdrh ** the stack which are the keys for new index entries for the new record. 3230ca3e24bSdrh ** The order of index keys is the same as the order of the indices on 3240ca3e24bSdrh ** the pTable->pIndex list. A key is only created for index i if 3250ca3e24bSdrh ** aIdxUsed!=0 and aIdxUsed[i]!=0. 3269cfcf5d4Sdrh ** 3279cfcf5d4Sdrh ** This routine also generates code to check constraints. NOT NULL, 3289cfcf5d4Sdrh ** CHECK, and UNIQUE constraints are all checked. If a constraint fails, 3299cfcf5d4Sdrh ** then the appropriate action is performed. The default action is to 3309cfcf5d4Sdrh ** execute OP_Halt to abort the transaction and cause sqlite_exec() to 3319cfcf5d4Sdrh ** return SQLITE_CONSTRAINT. This is the so-called "ABORT" action. 3329cfcf5d4Sdrh ** Other actions are REPLACE and IGNORE. The following table summarizes 3339cfcf5d4Sdrh ** what happens. 3349cfcf5d4Sdrh ** 3359cfcf5d4Sdrh ** Constraint type Action What Happens 3369cfcf5d4Sdrh ** --------------- ---------- ---------------------------------------- 3379cfcf5d4Sdrh ** any ABORT The current transaction is rolled back and 3389cfcf5d4Sdrh ** sqlite_exec() returns immediately with a 3399cfcf5d4Sdrh ** return code of SQLITE_CONSTRAINT. 3409cfcf5d4Sdrh ** 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 ** 3549cfcf5d4Sdrh ** The action to take is determined by the constraint itself if 3559cfcf5d4Sdrh ** overrideError is OE_Default. Otherwise, overrideError determines 3569cfcf5d4Sdrh ** which action to use. 3579cfcf5d4Sdrh ** 3589cfcf5d4Sdrh ** The calling routine must an open read/write cursor for pTab with 3599cfcf5d4Sdrh ** cursor number "base". All indices of pTab must also have open 3609cfcf5d4Sdrh ** read/write cursors with cursor number base+i for the i-th cursor. 3619cfcf5d4Sdrh ** Except, if there is no possibility of a REPLACE action then 3629cfcf5d4Sdrh ** cursors do not need to be open for indices where aIdxUsed[i]==0. 3639cfcf5d4Sdrh ** 3649cfcf5d4Sdrh ** If the isUpdate flag is true, it means that the "base" cursor is 3659cfcf5d4Sdrh ** initially pointing to an entry that is being updated. The isUpdate 3669cfcf5d4Sdrh ** flag causes extra code to be generated so that the "base" cursor 3679cfcf5d4Sdrh ** is still pointing at the same entry after the routine returns. 3689cfcf5d4Sdrh ** Without the isUpdate flag, the "base" cursor might be moved. 3699cfcf5d4Sdrh */ 3709cfcf5d4Sdrh void sqliteGenerateConstraintChecks( 3719cfcf5d4Sdrh Parse *pParse, /* The parser context */ 3729cfcf5d4Sdrh Table *pTab, /* the table into which we are inserting */ 3739cfcf5d4Sdrh int base, /* Index of a read/write cursor pointing at pTab */ 3749cfcf5d4Sdrh char *aIdxUsed, /* Which indices are used. NULL means all are used */ 3750ca3e24bSdrh int recnoChng, /* True if the record number will change */ 3769cfcf5d4Sdrh int overrideError, /* Override onError to this if not OE_Default */ 3779cfcf5d4Sdrh int ignoreDest, /* Jump to this label on an OE_Ignore resolution */ 3789cfcf5d4Sdrh int isUpdate /* True for UPDATE, False for INSERT */ 3799cfcf5d4Sdrh ){ 3809cfcf5d4Sdrh int i; 3819cfcf5d4Sdrh Vdbe *v; 3829cfcf5d4Sdrh int nCol; 3839cfcf5d4Sdrh int onError; 3849cfcf5d4Sdrh int addr; 3859cfcf5d4Sdrh int extra; 3860ca3e24bSdrh int iCur; 3870ca3e24bSdrh Index *pIdx; 3880ca3e24bSdrh int seenReplace = 0; 3890ca3e24bSdrh int jumpInst; 3900ca3e24bSdrh int contAddr; 3919cfcf5d4Sdrh 3929cfcf5d4Sdrh v = sqliteGetVdbe(pParse); 3939cfcf5d4Sdrh assert( v!=0 ); 3949cfcf5d4Sdrh nCol = pTab->nCol; 3950ca3e24bSdrh recnoChng = (recnoChng!=0); /* Must be either 1 or 0 */ 3969cfcf5d4Sdrh 3979cfcf5d4Sdrh /* Test all NOT NULL constraints. 3989cfcf5d4Sdrh */ 3999cfcf5d4Sdrh for(i=0; i<nCol; i++){ 4000ca3e24bSdrh if( i==pTab->iPKey ){ 4010ca3e24bSdrh /* Fix me: Make sure the INTEGER PRIMARY KEY is not NULL. */ 4020ca3e24bSdrh continue; 4030ca3e24bSdrh } 4049cfcf5d4Sdrh onError = pTab->aCol[i].notNull; 4050ca3e24bSdrh if( onError==OE_None ) continue; 4069cfcf5d4Sdrh if( overrideError!=OE_Default ){ 4079cfcf5d4Sdrh onError = overrideError; 4089cfcf5d4Sdrh } 4099cfcf5d4Sdrh if( onError==OE_Replace && pTab->aCol[i].zDflt==0 ){ 4109cfcf5d4Sdrh onError = OE_Abort; 4119cfcf5d4Sdrh } 4129cfcf5d4Sdrh addr = sqliteVdbeAddOp(v, OP_Dup, nCol-i, 1); 4139cfcf5d4Sdrh sqliteVdbeAddOp(v, OP_NotNull, 0, addr+1+(onError!=OE_Abort)); 4149cfcf5d4Sdrh switch( onError ){ 4159cfcf5d4Sdrh case OE_Abort: { 4169cfcf5d4Sdrh sqliteVdbeAddOp(v, OP_Halt, SQLITE_CONSTRAINT, 0); 4179cfcf5d4Sdrh break; 4189cfcf5d4Sdrh } 4199cfcf5d4Sdrh case OE_Ignore: { 4200ca3e24bSdrh sqliteVdbeAddOp(v, OP_Pop, nCol+1+recnoChng, 0); 4210ca3e24bSdrh sqliteVdbeAddOp(v, OP_Goto, 0, ignoreDest); 4229cfcf5d4Sdrh break; 4239cfcf5d4Sdrh } 4249cfcf5d4Sdrh case OE_Replace: { 4259cfcf5d4Sdrh sqliteVdbeAddOp(v, OP_String, 0, 0); 4269cfcf5d4Sdrh sqliteVdbeChangeP3(v, -1, pTab->aCol[i].zDflt, P3_STATIC); 4279cfcf5d4Sdrh sqliteVdbeAddOp(v, OP_Push, nCol-i, 0); 4289cfcf5d4Sdrh break; 4299cfcf5d4Sdrh } 4300ca3e24bSdrh default: assert(0); 4319cfcf5d4Sdrh } 4329cfcf5d4Sdrh } 4339cfcf5d4Sdrh 4349cfcf5d4Sdrh /* Test all CHECK constraints 4359cfcf5d4Sdrh */ 4369cfcf5d4Sdrh 4379cfcf5d4Sdrh /* Test all UNIQUE constraints. Add index records as we go. 4389cfcf5d4Sdrh */ 4390ca3e24bSdrh if( recnoChng && pTab->iPKey>=0 && pTab->keyConf!=OE_Replace 4400ca3e24bSdrh && overrideError!=OE_Replace ){ 4410ca3e24bSdrh sqliteVdbeAddOp(v, OP_Dup, nCol, 1); 4420ca3e24bSdrh jumpInst = sqliteVdbeAddOp(v, OP_NotExists, base, 0); 4430ca3e24bSdrh onError = pTab->keyConf; 4440ca3e24bSdrh if( overrideError!=OE_Default ){ 4450ca3e24bSdrh onError = overrideError; 4460ca3e24bSdrh } 4470ca3e24bSdrh switch( onError ){ 4480ca3e24bSdrh case OE_Abort: { 4490ca3e24bSdrh sqliteVdbeAddOp(v, OP_Halt, SQLITE_CONSTRAINT, 0); 4500ca3e24bSdrh break; 4510ca3e24bSdrh } 4520ca3e24bSdrh case OE_Ignore: { 4530ca3e24bSdrh sqliteVdbeAddOp(v, OP_Pop, nCol+2, 0); 4540ca3e24bSdrh sqliteVdbeAddOp(v, OP_Goto, 0, ignoreDest); 4550ca3e24bSdrh break; 4560ca3e24bSdrh } 4570ca3e24bSdrh default: assert(0); 4580ca3e24bSdrh } 4590ca3e24bSdrh contAddr = sqliteVdbeCurrentAddr(v); 4600ca3e24bSdrh sqliteVdbeChangeP2(v, jumpInst, contAddr); 4610ca3e24bSdrh if( isUpdate ){ 4620ca3e24bSdrh sqliteVdbeAddOp(v, OP_Dup, nCol+1, 1); 4630ca3e24bSdrh sqliteVdbeAddOp(v, OP_MoveTo, base, 0); 4640ca3e24bSdrh } 4650ca3e24bSdrh } 4669cfcf5d4Sdrh extra = 0; 4679cfcf5d4Sdrh for(extra=(-1), iCur=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, iCur++){ 4689cfcf5d4Sdrh if( aIdxUsed && aIdxUsed[iCur]==0 ) continue; 4699cfcf5d4Sdrh extra++; 4709cfcf5d4Sdrh sqliteVdbeAddOp(v, OP_Dup, nCol+extra, 1); 4719cfcf5d4Sdrh for(i=0; i<pIdx->nColumn; i++){ 4729cfcf5d4Sdrh int idx = pIdx->aiColumn[i]; 4739cfcf5d4Sdrh if( idx==pTab->iPKey ){ 4740ca3e24bSdrh sqliteVdbeAddOp(v, OP_Dup, i+extra+nCol+1, 1); 4759cfcf5d4Sdrh }else{ 4760ca3e24bSdrh sqliteVdbeAddOp(v, OP_Dup, i+extra+nCol-idx, 1); 4779cfcf5d4Sdrh } 4789cfcf5d4Sdrh } 4799cfcf5d4Sdrh sqliteVdbeAddOp(v, OP_MakeIdxKey, pIdx->nColumn, 0); 4809cfcf5d4Sdrh onError = pIdx->onError; 4819cfcf5d4Sdrh if( onError==OE_None ) continue; 4829cfcf5d4Sdrh if( overrideError!=OE_Default ){ 4839cfcf5d4Sdrh onError = overrideError; 4849cfcf5d4Sdrh } 485*bb37fdc7Sdrh sqliteVdbeAddOp(v, OP_Dup, extra+nCol+1+recnoChng, 1); 4860ca3e24bSdrh jumpInst = sqliteVdbeAddOp(v, OP_IsUnique, base+iCur+1, 0); 4879cfcf5d4Sdrh switch( onError ){ 4889cfcf5d4Sdrh case OE_Abort: { 4899cfcf5d4Sdrh sqliteVdbeAddOp(v, OP_Halt, SQLITE_CONSTRAINT, 0); 4909cfcf5d4Sdrh break; 4919cfcf5d4Sdrh } 4929cfcf5d4Sdrh case OE_Ignore: { 4930ca3e24bSdrh assert( seenReplace==0 ); 4940ca3e24bSdrh sqliteVdbeAddOp(v, OP_Pop, nCol+extra+2+recnoChng, 0); 4959cfcf5d4Sdrh sqliteVdbeAddOp(v, OP_Goto, 0, ignoreDest); 4969cfcf5d4Sdrh break; 4979cfcf5d4Sdrh } 4989cfcf5d4Sdrh case OE_Replace: { 4999cfcf5d4Sdrh sqliteGenerateRowDelete(v, pTab, base); 5009cfcf5d4Sdrh if( isUpdate ){ 5010ca3e24bSdrh sqliteVdbeAddOp(v, OP_Dup, nCol+extra+recnoChng, 1); 5020ca3e24bSdrh sqliteVdbeAddOp(v, OP_MoveTo, base, 0); 5039cfcf5d4Sdrh } 5040ca3e24bSdrh seenReplace = 1; 5059cfcf5d4Sdrh break; 5069cfcf5d4Sdrh } 5070ca3e24bSdrh default: assert(0); 5089cfcf5d4Sdrh } 5099cfcf5d4Sdrh contAddr = sqliteVdbeCurrentAddr(v); 5109cfcf5d4Sdrh sqliteVdbeChangeP2(v, jumpInst, contAddr); 5119cfcf5d4Sdrh } 5129cfcf5d4Sdrh } 5130ca3e24bSdrh 5140ca3e24bSdrh /* 5150ca3e24bSdrh ** This routine generates code to finish the INSERT or UPDATE operation 5160ca3e24bSdrh ** that was started by a prior call to sqliteGenerateConstraintChecks. 5170ca3e24bSdrh ** The stack must contain keys for all active indices followed by data 5180ca3e24bSdrh ** and the recno for the new entry. This routine creates the new 5190ca3e24bSdrh ** entries in all indices and in the main table. 5200ca3e24bSdrh ** 5210ca3e24bSdrh ** The arguments to this routine should be the same as the first five 5220ca3e24bSdrh ** arguments to sqliteGenerateConstraintChecks. 5230ca3e24bSdrh */ 5240ca3e24bSdrh void sqliteCompleteInsertion( 5250ca3e24bSdrh Parse *pParse, /* The parser context */ 5260ca3e24bSdrh Table *pTab, /* the table into which we are inserting */ 5270ca3e24bSdrh int base, /* Index of a read/write cursor pointing at pTab */ 5280ca3e24bSdrh char *aIdxUsed, /* Which indices are used. NULL means all are used */ 5290ca3e24bSdrh int recnoChng /* True if the record number changed */ 5300ca3e24bSdrh ){ 5310ca3e24bSdrh int i; 5320ca3e24bSdrh Vdbe *v; 5330ca3e24bSdrh int nIdx; 5340ca3e24bSdrh Index *pIdx; 5350ca3e24bSdrh 5360ca3e24bSdrh v = sqliteGetVdbe(pParse); 5370ca3e24bSdrh assert( v!=0 ); 5380ca3e24bSdrh for(nIdx=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, nIdx++){} 5390ca3e24bSdrh for(i=nIdx-1; i>=0; i--){ 5400ca3e24bSdrh if( aIdxUsed && aIdxUsed[i]==0 ) continue; 5410ca3e24bSdrh sqliteVdbeAddOp(v, OP_IdxPut, base+i+1, 0); 5420ca3e24bSdrh } 5430ca3e24bSdrh sqliteVdbeAddOp(v, OP_MakeRecord, pTab->nCol, 0); 5440ca3e24bSdrh sqliteVdbeAddOp(v, OP_PutIntKey, base, 0); 5450ca3e24bSdrh if( recnoChng ){ 5460ca3e24bSdrh sqliteVdbeAddOp(v, OP_Pop, 1, 0); 5470ca3e24bSdrh } 5480ca3e24bSdrh } 549