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*9cfcf5d4Sdrh ** $Id: insert.c,v 1.34 2002/01/29 18:41:25 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 */ 39*9cfcf5d4Sdrh IdList *pColumn, /* Column names corresponding to IDLIST. */ 40*9cfcf5d4Sdrh 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 */ 54cce7d176Sdrh 55daffd0e5Sdrh if( pParse->nErr || sqlite_malloc_failed ) goto insert_cleanup; 56ecdc7530Sdrh db = pParse->db; 57daffd0e5Sdrh 581ccde15dSdrh /* Locate the table into which we will be inserting new information. 591ccde15dSdrh */ 60cce7d176Sdrh zTab = sqliteTableNameFromToken(pTableName); 61daffd0e5Sdrh if( zTab==0 ) goto insert_cleanup; 62ecdc7530Sdrh pTab = sqliteFindTable(db, zTab); 63cce7d176Sdrh sqliteFree(zTab); 64cce7d176Sdrh if( pTab==0 ){ 65cce7d176Sdrh sqliteSetNString(&pParse->zErrMsg, "no such table: ", 0, 66cce7d176Sdrh pTableName->z, pTableName->n, 0); 67cce7d176Sdrh pParse->nErr++; 68cce7d176Sdrh goto insert_cleanup; 69cce7d176Sdrh } 70cce7d176Sdrh if( pTab->readOnly ){ 71cce7d176Sdrh sqliteSetString(&pParse->zErrMsg, "table ", pTab->zName, 72cce7d176Sdrh " may not be modified", 0); 73cce7d176Sdrh pParse->nErr++; 74cce7d176Sdrh goto insert_cleanup; 75cce7d176Sdrh } 761ccde15dSdrh 771ccde15dSdrh /* Allocate a VDBE 781ccde15dSdrh */ 79d8bc7086Sdrh v = sqliteGetVdbe(pParse); 805974a30fSdrh if( v==0 ) goto insert_cleanup; 81ecdc7530Sdrh if( (db->flags & SQLITE_InTrans)==0 ){ 8299fcd718Sdrh sqliteVdbeAddOp(v, OP_Transaction, 0, 0); 8399fcd718Sdrh sqliteVdbeAddOp(v, OP_VerifyCookie, db->schema_cookie, 0); 84ecdc7530Sdrh pParse->schemaVerified = 1; 855e00f6c7Sdrh } 861ccde15dSdrh 871ccde15dSdrh /* Figure out how many columns of data are supplied. If the data 88c6b52df3Sdrh ** is coming from a SELECT statement, then this step has to generate 891ccde15dSdrh ** all the code to implement the SELECT statement and leave the data 901ccde15dSdrh ** in a temporary table. If data is coming from an expression list, 911ccde15dSdrh ** then we just have to count the number of expressions. 921ccde15dSdrh */ 935974a30fSdrh if( pSelect ){ 945974a30fSdrh int rc; 955974a30fSdrh srcTab = pParse->nTab++; 9699fcd718Sdrh sqliteVdbeAddOp(v, OP_OpenTemp, srcTab, 0); 975974a30fSdrh rc = sqliteSelect(pParse, pSelect, SRT_Table, srcTab); 98daffd0e5Sdrh if( rc || pParse->nErr || sqlite_malloc_failed ) goto insert_cleanup; 995974a30fSdrh assert( pSelect->pEList ); 100967e8b73Sdrh nColumn = pSelect->pEList->nExpr; 1015974a30fSdrh }else{ 102daffd0e5Sdrh assert( pList!=0 ); 1035974a30fSdrh srcTab = -1; 1045974a30fSdrh assert( pList ); 105967e8b73Sdrh nColumn = pList->nExpr; 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 2044a32431cSdrh ** 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 217c8392586Sdrh /* If there are indices, we'll need the new record number again, so make 2184a32431cSdrh ** a copy. 2194a32431cSdrh */ 220cce7d176Sdrh if( pTab->pIndex ){ 22199fcd718Sdrh sqliteVdbeAddOp(v, OP_Dup, 0, 0); 222cce7d176Sdrh } 2234a32431cSdrh 224aacc543eSdrh /* Push onto the stack, data for all columns of the new entry, beginning 2254a32431cSdrh ** with the first column. 2264a32431cSdrh */ 227cce7d176Sdrh for(i=0; i<pTab->nCol; i++){ 2284a32431cSdrh if( i==pTab->iPKey ){ 2294a32431cSdrh /* The value of the INTEGER PRIMARY KEY column is always a NULL. 230aacc543eSdrh ** Whenever this column is read, the record number will be substituted 231aacc543eSdrh ** in its place. So will fill this column with a NULL to avoid 232aacc543eSdrh ** taking up data space with information that will never be used. */ 2334a32431cSdrh sqliteVdbeAddOp(v, OP_String, 0, 0); 2344a32431cSdrh continue; 2354a32431cSdrh } 236967e8b73Sdrh if( pColumn==0 ){ 237cce7d176Sdrh j = i; 238cce7d176Sdrh }else{ 239967e8b73Sdrh for(j=0; j<pColumn->nId; j++){ 240967e8b73Sdrh if( pColumn->a[j].idx==i ) break; 241cce7d176Sdrh } 242cce7d176Sdrh } 243967e8b73Sdrh if( pColumn && j>=pColumn->nId ){ 24499fcd718Sdrh sqliteVdbeAddOp(v, OP_String, 0, 0); 24599fcd718Sdrh sqliteVdbeChangeP3(v, -1, pTab->aCol[i].zDflt, P3_STATIC); 2465974a30fSdrh }else if( srcTab>=0 ){ 24799fcd718Sdrh sqliteVdbeAddOp(v, OP_Column, srcTab, i); 248cce7d176Sdrh }else{ 249cce7d176Sdrh sqliteExprCode(pParse, pList->a[j].pExpr); 250cce7d176Sdrh } 251cce7d176Sdrh } 2521ccde15dSdrh 2534a32431cSdrh /* Create the new record and put it into the database. 2544a32431cSdrh */ 2554a32431cSdrh sqliteVdbeAddOp(v, OP_MakeRecord, pTab->nCol, 0); 2566b12545fSdrh sqliteVdbeAddOp(v, OP_PutIntKey, base, keyColumn>=0); 2571bee3d7bSdrh 2581ccde15dSdrh /* Create appropriate entries for the new data row in all indices 2591ccde15dSdrh ** of the table. 2601ccde15dSdrh */ 261bed8690fSdrh for(idx=1, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, idx++){ 262cce7d176Sdrh if( pIdx->pNext ){ 26399fcd718Sdrh sqliteVdbeAddOp(v, OP_Dup, 0, 0); 264cce7d176Sdrh } 265967e8b73Sdrh for(i=0; i<pIdx->nColumn; i++){ 266967e8b73Sdrh int idx = pIdx->aiColumn[i]; 2674a32431cSdrh if( idx==pTab->iPKey ){ 2684a32431cSdrh /* Copy the record number in place of the INTEGER PRIMARY KEY column */ 2694a32431cSdrh sqliteVdbeAddOp(v, OP_Dup, i, 0); 2704a32431cSdrh continue; 2714a32431cSdrh } 272967e8b73Sdrh if( pColumn==0 ){ 273cce7d176Sdrh j = idx; 274cce7d176Sdrh }else{ 275967e8b73Sdrh for(j=0; j<pColumn->nId; j++){ 276967e8b73Sdrh if( pColumn->a[j].idx==idx ) break; 277cce7d176Sdrh } 278cce7d176Sdrh } 279967e8b73Sdrh if( pColumn && j>=pColumn->nId ){ 28099fcd718Sdrh sqliteVdbeAddOp(v, OP_String, 0, 0); 28199fcd718Sdrh sqliteVdbeChangeP3(v, -1, pTab->aCol[idx].zDflt, P3_STATIC); 2825974a30fSdrh }else if( srcTab>=0 ){ 28399fcd718Sdrh sqliteVdbeAddOp(v, OP_Column, srcTab, idx); 284cce7d176Sdrh }else{ 285cce7d176Sdrh sqliteExprCode(pParse, pList->a[j].pExpr); 286cce7d176Sdrh } 287cce7d176Sdrh } 28899fcd718Sdrh sqliteVdbeAddOp(v, OP_MakeIdxKey, pIdx->nColumn, 0); 2898721ce4aSdrh sqliteVdbeAddOp(v, OP_IdxPut, idx+base, pIdx->isUnique); 290cce7d176Sdrh } 2911ccde15dSdrh 2921bee3d7bSdrh 2931bee3d7bSdrh /* If inserting from a SELECT, keep a count of the number of 2941bee3d7bSdrh ** rows inserted. 2951bee3d7bSdrh */ 2961bee3d7bSdrh if( srcTab>=0 && (db->flags & SQLITE_CountRows)!=0 ){ 2971bee3d7bSdrh sqliteVdbeAddOp(v, OP_AddImm, 1, 0); 2981bee3d7bSdrh } 2991bee3d7bSdrh 3001ccde15dSdrh /* The bottom of the loop, if the data source is a SELECT statement 3011ccde15dSdrh */ 3025974a30fSdrh if( srcTab>=0 ){ 3036b56344dSdrh sqliteVdbeAddOp(v, OP_Next, srcTab, iCont); 30499fcd718Sdrh sqliteVdbeResolveLabel(v, iBreak); 3056b56344dSdrh sqliteVdbeAddOp(v, OP_Close, srcTab, 0); 3066b56344dSdrh } 3076b56344dSdrh sqliteVdbeAddOp(v, OP_Close, base, 0); 3086b56344dSdrh for(idx=1, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, idx++){ 3096b56344dSdrh sqliteVdbeAddOp(v, OP_Close, idx+base, 0); 310cce7d176Sdrh } 311ecdc7530Sdrh if( (db->flags & SQLITE_InTrans)==0 ){ 31299fcd718Sdrh sqliteVdbeAddOp(v, OP_Commit, 0, 0); 3135e00f6c7Sdrh } 3145e00f6c7Sdrh 3151bee3d7bSdrh /* 3161bee3d7bSdrh ** Return the number of rows inserted. 3171bee3d7bSdrh */ 3181bee3d7bSdrh if( db->flags & SQLITE_CountRows ){ 3191bee3d7bSdrh sqliteVdbeAddOp(v, OP_ColumnCount, 1, 0); 3201bee3d7bSdrh sqliteVdbeAddOp(v, OP_ColumnName, 0, 0); 3211bee3d7bSdrh sqliteVdbeChangeP3(v, -1, "rows inserted", P3_STATIC); 3221bee3d7bSdrh if( srcTab<0 ){ 3231bee3d7bSdrh sqliteVdbeAddOp(v, OP_Integer, 1, 0); 3241bee3d7bSdrh } 3251bee3d7bSdrh sqliteVdbeAddOp(v, OP_Callback, 1, 0); 3261bee3d7bSdrh } 327cce7d176Sdrh 328cce7d176Sdrh insert_cleanup: 3295974a30fSdrh if( pList ) sqliteExprListDelete(pList); 3305974a30fSdrh if( pSelect ) sqliteSelectDelete(pSelect); 331967e8b73Sdrh sqliteIdListDelete(pColumn); 332cce7d176Sdrh } 333*9cfcf5d4Sdrh 334*9cfcf5d4Sdrh #if 0 335*9cfcf5d4Sdrh /* 336*9cfcf5d4Sdrh ** Generate code to do a constraint check prior to an INSERT or an UPDATE. 337*9cfcf5d4Sdrh ** 338*9cfcf5d4Sdrh ** When this routine is called, the stack contains (from bottom to top) 339*9cfcf5d4Sdrh ** the recno of the row to be updated and each column of new data beginning 340*9cfcf5d4Sdrh ** with the first column. The code generated by this routine pushes addition 341*9cfcf5d4Sdrh ** entries onto the stack which are the keys for new index entries for 342*9cfcf5d4Sdrh ** the new record. The order of index keys is the same as the order of 343*9cfcf5d4Sdrh ** the indices on the pTable->pIndex list. A key is only created for 344*9cfcf5d4Sdrh ** index i if aIdxUsed!=0 and aIdxUsed[i]!=0. 345*9cfcf5d4Sdrh ** 346*9cfcf5d4Sdrh ** This routine also generates code to check constraints. NOT NULL, 347*9cfcf5d4Sdrh ** CHECK, and UNIQUE constraints are all checked. If a constraint fails, 348*9cfcf5d4Sdrh ** then the appropriate action is performed. The default action is to 349*9cfcf5d4Sdrh ** execute OP_Halt to abort the transaction and cause sqlite_exec() to 350*9cfcf5d4Sdrh ** return SQLITE_CONSTRAINT. This is the so-called "ABORT" action. 351*9cfcf5d4Sdrh ** Other actions are REPLACE and IGNORE. The following table summarizes 352*9cfcf5d4Sdrh ** what happens. 353*9cfcf5d4Sdrh ** 354*9cfcf5d4Sdrh ** Constraint type Action What Happens 355*9cfcf5d4Sdrh ** --------------- ---------- ---------------------------------------- 356*9cfcf5d4Sdrh ** any ABORT The current transaction is rolled back and 357*9cfcf5d4Sdrh ** sqlite_exec() returns immediately with a 358*9cfcf5d4Sdrh ** return code of SQLITE_CONSTRAINT. 359*9cfcf5d4Sdrh ** 360*9cfcf5d4Sdrh ** any IGNORE The record number and data is popped from 361*9cfcf5d4Sdrh ** the stack and there is an immediate jump 362*9cfcf5d4Sdrh ** to label ignoreDest. 363*9cfcf5d4Sdrh ** 364*9cfcf5d4Sdrh ** NOT NULL REPLACE The NULL value is replace by the default 365*9cfcf5d4Sdrh ** value for that column. If the default value 366*9cfcf5d4Sdrh ** is NULL, the action is the same as ABORT. 367*9cfcf5d4Sdrh ** 368*9cfcf5d4Sdrh ** UNIQUE REPLACE The other row that conflicts with the row 369*9cfcf5d4Sdrh ** being inserted is removed. 370*9cfcf5d4Sdrh ** 371*9cfcf5d4Sdrh ** CHECK REPLACE Illegal. The results in an exception. 372*9cfcf5d4Sdrh ** 373*9cfcf5d4Sdrh ** The action to take is determined by the constraint itself if 374*9cfcf5d4Sdrh ** overrideError is OE_Default. Otherwise, overrideError determines 375*9cfcf5d4Sdrh ** which action to use. 376*9cfcf5d4Sdrh ** 377*9cfcf5d4Sdrh ** The calling routine must an open read/write cursor for pTab with 378*9cfcf5d4Sdrh ** cursor number "base". All indices of pTab must also have open 379*9cfcf5d4Sdrh ** read/write cursors with cursor number base+i for the i-th cursor. 380*9cfcf5d4Sdrh ** Except, if there is no possibility of a REPLACE action then 381*9cfcf5d4Sdrh ** cursors do not need to be open for indices where aIdxUsed[i]==0. 382*9cfcf5d4Sdrh ** 383*9cfcf5d4Sdrh ** If the isUpdate flag is true, it means that the "base" cursor is 384*9cfcf5d4Sdrh ** initially pointing to an entry that is being updated. The isUpdate 385*9cfcf5d4Sdrh ** flag causes extra code to be generated so that the "base" cursor 386*9cfcf5d4Sdrh ** is still pointing at the same entry after the routine returns. 387*9cfcf5d4Sdrh ** Without the isUpdate flag, the "base" cursor might be moved. 388*9cfcf5d4Sdrh */ 389*9cfcf5d4Sdrh void sqliteGenerateConstraintChecks( 390*9cfcf5d4Sdrh Parse *pParse, /* The parser context */ 391*9cfcf5d4Sdrh Table *pTab, /* the table into which we are inserting */ 392*9cfcf5d4Sdrh int base, /* Index of a read/write cursor pointing at pTab */ 393*9cfcf5d4Sdrh char *aIdxUsed, /* Which indices are used. NULL means all are used */ 394*9cfcf5d4Sdrh int overrideError, /* Override onError to this if not OE_Default */ 395*9cfcf5d4Sdrh int ignoreDest, /* Jump to this label on an OE_Ignore resolution */ 396*9cfcf5d4Sdrh int isUpdate /* True for UPDATE, False for INSERT */ 397*9cfcf5d4Sdrh ){ 398*9cfcf5d4Sdrh int i; 399*9cfcf5d4Sdrh Vdbe *v; 400*9cfcf5d4Sdrh int nCol; 401*9cfcf5d4Sdrh int onError; 402*9cfcf5d4Sdrh int addr; 403*9cfcf5d4Sdrh int extra; 404*9cfcf5d4Sdrh char *pToFree = 0; 405*9cfcf5d4Sdrh int seenIgnore = 0; 406*9cfcf5d4Sdrh 407*9cfcf5d4Sdrh v = sqliteGetVdbe(pParse); 408*9cfcf5d4Sdrh assert( v!=0 ); 409*9cfcf5d4Sdrh nCol = pTab->nCol; 410*9cfcf5d4Sdrh 411*9cfcf5d4Sdrh /* Test all NOT NULL constraints. 412*9cfcf5d4Sdrh */ 413*9cfcf5d4Sdrh for(i=0; i<nCol; i++){ 414*9cfcf5d4Sdrh onError = pTab->aCol[i].notNull; 415*9cfcf5d4Sdrh if( i==iPKey || onError==OE_None ) continue; 416*9cfcf5d4Sdrh if( overrideError!=OE_Default ){ 417*9cfcf5d4Sdrh onError = overrideError; 418*9cfcf5d4Sdrh } 419*9cfcf5d4Sdrh if( onError==OE_Replace && pTab->aCol[i].zDflt==0 ){ 420*9cfcf5d4Sdrh onError = OE_Abort; 421*9cfcf5d4Sdrh } 422*9cfcf5d4Sdrh addr = sqliteVdbeAddOp(v, OP_Dup, nCol-i, 1); 423*9cfcf5d4Sdrh sqliteVdbeAddOp(v, OP_NotNull, 0, addr+1+(onError!=OE_Abort)); 424*9cfcf5d4Sdrh switch( onError ){ 425*9cfcf5d4Sdrh case OE_Abort: { 426*9cfcf5d4Sdrh sqliteVdbeAddOp(v, OP_Halt, SQLITE_CONSTRAINT, 0); 427*9cfcf5d4Sdrh break; 428*9cfcf5d4Sdrh } 429*9cfcf5d4Sdrh case OE_Ignore: { 430*9cfcf5d4Sdrh sqliteVdbeAddOp(v, OP_Pop, nCol+1, 0); 431*9cfcf5d4Sdrh sqliteVdbeAddOp(v, OP_GoTo, 0, ignoreDest); 432*9cfcf5d4Sdrh break; 433*9cfcf5d4Sdrh } 434*9cfcf5d4Sdrh case OE_Replace: { 435*9cfcf5d4Sdrh sqliteVdbeAddOp(v, OP_String, 0, 0); 436*9cfcf5d4Sdrh sqliteVdbeChangeP3(v, -1, pTab->aCol[i].zDflt, P3_STATIC); 437*9cfcf5d4Sdrh sqliteVdbeAddOp(v, OP_Push, nCol-i, 0); 438*9cfcf5d4Sdrh break; 439*9cfcf5d4Sdrh } 440*9cfcf5d4Sdrh default: { 441*9cfcf5d4Sdrh CANT_HAPPEN; 442*9cfcf5d4Sdrh } 443*9cfcf5d4Sdrh } 444*9cfcf5d4Sdrh } 445*9cfcf5d4Sdrh 446*9cfcf5d4Sdrh /* Test all CHECK constraints 447*9cfcf5d4Sdrh */ 448*9cfcf5d4Sdrh 449*9cfcf5d4Sdrh /* Test all UNIQUE constraints. Add index records as we go. 450*9cfcf5d4Sdrh */ 451*9cfcf5d4Sdrh extra = 0; 452*9cfcf5d4Sdrh for(extra=(-1), iCur=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, iCur++){ 453*9cfcf5d4Sdrh int jumpInst; 454*9cfcf5d4Sdrh int contAddr; 455*9cfcf5d4Sdrh 456*9cfcf5d4Sdrh if( aIdxUsed && aIdxUsed[iCur]==0 ) continue; 457*9cfcf5d4Sdrh extra++; 458*9cfcf5d4Sdrh sqliteVdbeAddOp(v, OP_Dup, nCol+extra, 1); 459*9cfcf5d4Sdrh for(i=0; i<pIdx->nColumn; i++){ 460*9cfcf5d4Sdrh int idx = pIdx->aiColumn[i]; 461*9cfcf5d4Sdrh if( idx==pTab->iPKey ){ 462*9cfcf5d4Sdrh sqliteVdbeAddOp(v, OP_Dup, i+extra+nCol+1, 0); 463*9cfcf5d4Sdrh }else{ 464*9cfcf5d4Sdrh sqliteVdbeAddOp(v, OP_Dup, i+extra+nCol-idx, 0); 465*9cfcf5d4Sdrh } 466*9cfcf5d4Sdrh } 467*9cfcf5d4Sdrh sqliteVdbeAddOp(v, OP_MakeIdxKey, pIdx->nColumn, 0); 468*9cfcf5d4Sdrh onError = pIdx->onError; 469*9cfcf5d4Sdrh if( onError==OE_None ) continue; 470*9cfcf5d4Sdrh if( overrideError!=OE_Default ){ 471*9cfcf5d4Sdrh onError = overrideError; 472*9cfcf5d4Sdrh } 473*9cfcf5d4Sdrh jumpInst = sqliteVdbeAddOp(v, OP_IsUnique, iCur, 0); 474*9cfcf5d4Sdrh switch( onError ){ 475*9cfcf5d4Sdrh case OE_Abort: { 476*9cfcf5d4Sdrh sqliteVdbeAddOp(v, OP_Halt, SQLITE_CONSTRAINT, 0); 477*9cfcf5d4Sdrh break; 478*9cfcf5d4Sdrh } 479*9cfcf5d4Sdrh case OE_Ignore: { 480*9cfcf5d4Sdrh sqliteVdbeAddOp(v, OP_Pop, nCol+extra+2, 0); 481*9cfcf5d4Sdrh sqliteVdbeAddOp(v, OP_Goto, 0, ignoreDest); 482*9cfcf5d4Sdrh seenIgnore = 1; 483*9cfcf5d4Sdrh break; 484*9cfcf5d4Sdrh } 485*9cfcf5d4Sdrh case OE_Replace: { 486*9cfcf5d4Sdrh assert( seenIgnore==0 ); 487*9cfcf5d4Sdrh sqliteGenerateRowDelete(v, pTab, base); 488*9cfcf5d4Sdrh if( isUpdate ){ 489*9cfcf5d4Sdrh sqliteVdbeAddOp(v, OP_Dup, nCol+extra+2, 1); 490*9cfcf5d4Sdrh sqliteVdbeAddOp(v, OP_Moveto, base, 0); 491*9cfcf5d4Sdrh } 492*9cfcf5d4Sdrh break; 493*9cfcf5d4Sdrh } 494*9cfcf5d4Sdrh default: CANT_HAPPEN; 495*9cfcf5d4Sdrh } 496*9cfcf5d4Sdrh contAddr = sqliteVdbeCurrentAddr(v); 497*9cfcf5d4Sdrh sqliteVdbeChangeP2(v, jumpInst, contAddr); 498*9cfcf5d4Sdrh } 499*9cfcf5d4Sdrh } 500*9cfcf5d4Sdrh #endif 501