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*3d1bfeaaSdanielk1977 ** $Id: insert.c,v 1.98 2004/05/14 11:00:53 danielk1977 Exp $ 16cce7d176Sdrh */ 17cce7d176Sdrh #include "sqliteInt.h" 18cce7d176Sdrh 19cce7d176Sdrh /* 20*3d1bfeaaSdanielk1977 ** Set P3 of the most recently inserted opcode to a column affinity 21*3d1bfeaaSdanielk1977 ** string for table pTab. A column affinity string has one character 22*3d1bfeaaSdanielk1977 ** for each column in the table, according to the affinity of the column: 23*3d1bfeaaSdanielk1977 ** 24*3d1bfeaaSdanielk1977 ** Character Column affinity 25*3d1bfeaaSdanielk1977 ** ------------------------------ 26*3d1bfeaaSdanielk1977 ** 'n' NUMERIC 27*3d1bfeaaSdanielk1977 ** 'i' INTEGER 28*3d1bfeaaSdanielk1977 ** 't' TEXT 29*3d1bfeaaSdanielk1977 ** 'o' NONE 30*3d1bfeaaSdanielk1977 */ 31*3d1bfeaaSdanielk1977 int sqlite3AddRecordType(Vdbe *v, Table *pTab){ 32*3d1bfeaaSdanielk1977 assert( pTab ); 33*3d1bfeaaSdanielk1977 34*3d1bfeaaSdanielk1977 /* The first time a column affinity string for a particular table 35*3d1bfeaaSdanielk1977 ** is required, it is allocated and populated here. It is then 36*3d1bfeaaSdanielk1977 ** stored as a member of the Table structure for subsequent use. 37*3d1bfeaaSdanielk1977 ** 38*3d1bfeaaSdanielk1977 ** The column affinity string will eventually be deleted by 39*3d1bfeaaSdanielk1977 ** sqlite3DeleteTable() when the Table structure itself is cleaned up. 40*3d1bfeaaSdanielk1977 */ 41*3d1bfeaaSdanielk1977 if( !pTab->zColAff ){ 42*3d1bfeaaSdanielk1977 char *zColAff; 43*3d1bfeaaSdanielk1977 int i; 44*3d1bfeaaSdanielk1977 45*3d1bfeaaSdanielk1977 zColAff = sqliteMalloc(pTab->nCol+1); 46*3d1bfeaaSdanielk1977 if( !zColAff ){ 47*3d1bfeaaSdanielk1977 return SQLITE_NOMEM; 48*3d1bfeaaSdanielk1977 } 49*3d1bfeaaSdanielk1977 50*3d1bfeaaSdanielk1977 for(i=0; i<pTab->nCol; i++){ 51*3d1bfeaaSdanielk1977 if( pTab->aCol[i].sortOrder&SQLITE_SO_TEXT ){ 52*3d1bfeaaSdanielk1977 zColAff[i] = 't'; 53*3d1bfeaaSdanielk1977 }else{ 54*3d1bfeaaSdanielk1977 zColAff[i] = 'n'; 55*3d1bfeaaSdanielk1977 } 56*3d1bfeaaSdanielk1977 } 57*3d1bfeaaSdanielk1977 zColAff[pTab->nCol] = '\0'; 58*3d1bfeaaSdanielk1977 59*3d1bfeaaSdanielk1977 pTab->zColAff = zColAff; 60*3d1bfeaaSdanielk1977 } 61*3d1bfeaaSdanielk1977 62*3d1bfeaaSdanielk1977 /* Set the memory management at the vdbe to P3_STATIC, as the column 63*3d1bfeaaSdanielk1977 ** affinity string is managed as part of the Table structure. 64*3d1bfeaaSdanielk1977 */ 65*3d1bfeaaSdanielk1977 sqlite3VdbeChangeP3(v, -1, pTab->zColAff, P3_STATIC); 66*3d1bfeaaSdanielk1977 return SQLITE_OK; 67*3d1bfeaaSdanielk1977 } 68*3d1bfeaaSdanielk1977 69*3d1bfeaaSdanielk1977 70*3d1bfeaaSdanielk1977 /* 711ccde15dSdrh ** This routine is call to handle SQL of the following forms: 72cce7d176Sdrh ** 73cce7d176Sdrh ** insert into TABLE (IDLIST) values(EXPRLIST) 741ccde15dSdrh ** insert into TABLE (IDLIST) select 75cce7d176Sdrh ** 761ccde15dSdrh ** The IDLIST following the table name is always optional. If omitted, 771ccde15dSdrh ** then a list of all columns for the table is substituted. The IDLIST 78967e8b73Sdrh ** appears in the pColumn parameter. pColumn is NULL if IDLIST is omitted. 791ccde15dSdrh ** 801ccde15dSdrh ** The pList parameter holds EXPRLIST in the first form of the INSERT 811ccde15dSdrh ** statement above, and pSelect is NULL. For the second form, pList is 821ccde15dSdrh ** NULL and pSelect is a pointer to the select statement used to generate 831ccde15dSdrh ** data for the insert. 84142e30dfSdrh ** 85142e30dfSdrh ** The code generated follows one of three templates. For a simple 86142e30dfSdrh ** select with data coming from a VALUES clause, the code executes 87142e30dfSdrh ** once straight down through. The template looks like this: 88142e30dfSdrh ** 89142e30dfSdrh ** open write cursor to <table> and its indices 90142e30dfSdrh ** puts VALUES clause expressions onto the stack 91142e30dfSdrh ** write the resulting record into <table> 92142e30dfSdrh ** cleanup 93142e30dfSdrh ** 94142e30dfSdrh ** If the statement is of the form 95142e30dfSdrh ** 96142e30dfSdrh ** INSERT INTO <table> SELECT ... 97142e30dfSdrh ** 98142e30dfSdrh ** And the SELECT clause does not read from <table> at any time, then 99142e30dfSdrh ** the generated code follows this template: 100142e30dfSdrh ** 101142e30dfSdrh ** goto B 102142e30dfSdrh ** A: setup for the SELECT 103142e30dfSdrh ** loop over the tables in the SELECT 104142e30dfSdrh ** gosub C 105142e30dfSdrh ** end loop 106142e30dfSdrh ** cleanup after the SELECT 107142e30dfSdrh ** goto D 108142e30dfSdrh ** B: open write cursor to <table> and its indices 109142e30dfSdrh ** goto A 110142e30dfSdrh ** C: insert the select result into <table> 111142e30dfSdrh ** return 112142e30dfSdrh ** D: cleanup 113142e30dfSdrh ** 114142e30dfSdrh ** The third template is used if the insert statement takes its 115142e30dfSdrh ** values from a SELECT but the data is being inserted into a table 116142e30dfSdrh ** that is also read as part of the SELECT. In the third form, 117142e30dfSdrh ** we have to use a intermediate table to store the results of 118142e30dfSdrh ** the select. The template is like this: 119142e30dfSdrh ** 120142e30dfSdrh ** goto B 121142e30dfSdrh ** A: setup for the SELECT 122142e30dfSdrh ** loop over the tables in the SELECT 123142e30dfSdrh ** gosub C 124142e30dfSdrh ** end loop 125142e30dfSdrh ** cleanup after the SELECT 126142e30dfSdrh ** goto D 127142e30dfSdrh ** C: insert the select result into the intermediate table 128142e30dfSdrh ** return 129142e30dfSdrh ** B: open a cursor to an intermediate table 130142e30dfSdrh ** goto A 131142e30dfSdrh ** D: open write cursor to <table> and its indices 132142e30dfSdrh ** loop over the intermediate table 133142e30dfSdrh ** transfer values form intermediate table into <table> 134142e30dfSdrh ** end the loop 135142e30dfSdrh ** cleanup 136cce7d176Sdrh */ 1374adee20fSdanielk1977 void sqlite3Insert( 138cce7d176Sdrh Parse *pParse, /* Parser context */ 139113088ecSdrh SrcList *pTabList, /* Name of table into which we are inserting */ 140cce7d176Sdrh ExprList *pList, /* List of values to be inserted */ 1415974a30fSdrh Select *pSelect, /* A SELECT statement to use as the data source */ 1429cfcf5d4Sdrh IdList *pColumn, /* Column names corresponding to IDLIST. */ 1439cfcf5d4Sdrh int onError /* How to handle constraint errors */ 144cce7d176Sdrh ){ 1455974a30fSdrh Table *pTab; /* The table to insert into */ 146113088ecSdrh char *zTab; /* Name of the table into which we are inserting */ 147e22a334bSdrh const char *zDb; /* Name of the database holding this table */ 1485974a30fSdrh int i, j, idx; /* Loop counters */ 1495974a30fSdrh Vdbe *v; /* Generate code into this virtual machine */ 1505974a30fSdrh Index *pIdx; /* For looping over indices of the table */ 151967e8b73Sdrh int nColumn; /* Number of columns in the data */ 1526a3ea0e6Sdrh int base; /* VDBE Cursor number for pTab */ 1535974a30fSdrh int iCont, iBreak; /* Beginning and end of the loop over srcTab */ 154ecdc7530Sdrh sqlite *db; /* The main database structure */ 1554a32431cSdrh int keyColumn = -1; /* Column that is the INTEGER PRIMARY KEY */ 1560ca3e24bSdrh int endOfLoop; /* Label for the end of the insertion loop */ 157142e30dfSdrh int useTempTable; /* Store SELECT results in intermediate table */ 158142e30dfSdrh int srcTab; /* Data comes from this temporary cursor if >=0 */ 159142e30dfSdrh int iSelectLoop; /* Address of code that implements the SELECT */ 160142e30dfSdrh int iCleanup; /* Address of the cleanup code */ 161142e30dfSdrh int iInsertBlock; /* Address of the subroutine used to insert data */ 162142e30dfSdrh int iCntMem; /* Memory cell used for the row counter */ 1635cf590c1Sdrh int isView; /* True if attempting to insert into a view */ 164cce7d176Sdrh 165c3f9bad2Sdanielk1977 int row_triggers_exist = 0; /* True if there are FOR EACH ROW triggers */ 16670ce3f0cSdrh int before_triggers; /* True if there are BEFORE triggers */ 16770ce3f0cSdrh int after_triggers; /* True if there are AFTER triggers */ 16870ce3f0cSdrh int newIdx = -1; /* Cursor for the NEW table */ 169c3f9bad2Sdanielk1977 17024b03fd0Sdanielk1977 if( pParse->nErr || sqlite3_malloc_failed ) goto insert_cleanup; 171ecdc7530Sdrh db = pParse->db; 172daffd0e5Sdrh 1731ccde15dSdrh /* Locate the table into which we will be inserting new information. 1741ccde15dSdrh */ 175113088ecSdrh assert( pTabList->nSrc==1 ); 176113088ecSdrh zTab = pTabList->a[0].zName; 177daffd0e5Sdrh if( zTab==0 ) goto insert_cleanup; 1784adee20fSdanielk1977 pTab = sqlite3SrcListLookup(pParse, pTabList); 179c3f9bad2Sdanielk1977 if( pTab==0 ){ 180c3f9bad2Sdanielk1977 goto insert_cleanup; 181c3f9bad2Sdanielk1977 } 182e22a334bSdrh assert( pTab->iDb<db->nDb ); 183e22a334bSdrh zDb = db->aDb[pTab->iDb].zName; 1844adee20fSdanielk1977 if( sqlite3AuthCheck(pParse, SQLITE_INSERT, pTab->zName, 0, zDb) ){ 1851962bda7Sdrh goto insert_cleanup; 1861962bda7Sdrh } 187c3f9bad2Sdanielk1977 188c3f9bad2Sdanielk1977 /* Ensure that: 189c3f9bad2Sdanielk1977 * (a) the table is not read-only, 190c3f9bad2Sdanielk1977 * (b) that if it is a view then ON INSERT triggers exist 191c3f9bad2Sdanielk1977 */ 1924adee20fSdanielk1977 before_triggers = sqlite3TriggersExist(pParse, pTab->pTrigger, TK_INSERT, 19370ce3f0cSdrh TK_BEFORE, TK_ROW, 0); 1944adee20fSdanielk1977 after_triggers = sqlite3TriggersExist(pParse, pTab->pTrigger, TK_INSERT, 19570ce3f0cSdrh TK_AFTER, TK_ROW, 0); 19670ce3f0cSdrh row_triggers_exist = before_triggers || after_triggers; 1975cf590c1Sdrh isView = pTab->pSelect!=0; 1984adee20fSdanielk1977 if( sqlite3IsReadOnly(pParse, pTab, before_triggers) ){ 199c3f9bad2Sdanielk1977 goto insert_cleanup; 200c3f9bad2Sdanielk1977 } 201a76b5dfcSdrh if( pTab==0 ) goto insert_cleanup; 2021ccde15dSdrh 203f573c99bSdrh /* If pTab is really a view, make sure it has been initialized. 204f573c99bSdrh */ 2054adee20fSdanielk1977 if( isView && sqlite3ViewGetColumnNames(pParse, pTab) ){ 206f573c99bSdrh goto insert_cleanup; 207f573c99bSdrh } 208f573c99bSdrh 2091ccde15dSdrh /* Allocate a VDBE 2101ccde15dSdrh */ 2114adee20fSdanielk1977 v = sqlite3GetVdbe(pParse); 2125974a30fSdrh if( v==0 ) goto insert_cleanup; 2134adee20fSdanielk1977 sqlite3BeginWriteOperation(pParse, pSelect || row_triggers_exist, pTab->iDb); 2141ccde15dSdrh 215c3f9bad2Sdanielk1977 /* if there are row triggers, allocate a temp table for new.* references. */ 216f29ce559Sdanielk1977 if( row_triggers_exist ){ 217c3f9bad2Sdanielk1977 newIdx = pParse->nTab++; 218f29ce559Sdanielk1977 } 219c3f9bad2Sdanielk1977 2201ccde15dSdrh /* Figure out how many columns of data are supplied. If the data 221142e30dfSdrh ** is coming from a SELECT statement, then this step also generates 222142e30dfSdrh ** all the code to implement the SELECT statement and invoke a subroutine 223142e30dfSdrh ** to process each row of the result. (Template 2.) If the SELECT 224142e30dfSdrh ** statement uses the the table that is being inserted into, then the 225142e30dfSdrh ** subroutine is also coded here. That subroutine stores the SELECT 226142e30dfSdrh ** results in a temporary table. (Template 3.) 2271ccde15dSdrh */ 2285974a30fSdrh if( pSelect ){ 229142e30dfSdrh /* Data is coming from a SELECT. Generate code to implement that SELECT 230142e30dfSdrh */ 231142e30dfSdrh int rc, iInitCode; 2324adee20fSdanielk1977 iInitCode = sqlite3VdbeAddOp(v, OP_Goto, 0, 0); 2334adee20fSdanielk1977 iSelectLoop = sqlite3VdbeCurrentAddr(v); 2344adee20fSdanielk1977 iInsertBlock = sqlite3VdbeMakeLabel(v); 2354adee20fSdanielk1977 rc = sqlite3Select(pParse, pSelect, SRT_Subroutine, iInsertBlock, 0,0,0); 23624b03fd0Sdanielk1977 if( rc || pParse->nErr || sqlite3_malloc_failed ) goto insert_cleanup; 2374adee20fSdanielk1977 iCleanup = sqlite3VdbeMakeLabel(v); 2384adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Goto, 0, iCleanup); 2395974a30fSdrh assert( pSelect->pEList ); 240967e8b73Sdrh nColumn = pSelect->pEList->nExpr; 241142e30dfSdrh 242142e30dfSdrh /* Set useTempTable to TRUE if the result of the SELECT statement 243142e30dfSdrh ** should be written into a temporary table. Set to FALSE if each 244142e30dfSdrh ** row of the SELECT can be written directly into the result table. 245048c530cSdrh ** 246048c530cSdrh ** A temp table must be used if the table being updated is also one 247048c530cSdrh ** of the tables being read by the SELECT statement. Also use a 248048c530cSdrh ** temp table in the case of row triggers. 249142e30dfSdrh */ 250048c530cSdrh if( row_triggers_exist ){ 251048c530cSdrh useTempTable = 1; 252048c530cSdrh }else{ 2534adee20fSdanielk1977 int addr = sqlite3VdbeFindOp(v, OP_OpenRead, pTab->tnum); 254048c530cSdrh useTempTable = 0; 255048c530cSdrh if( addr>0 ){ 2564adee20fSdanielk1977 VdbeOp *pOp = sqlite3VdbeGetOp(v, addr-2); 257048c530cSdrh if( pOp->opcode==OP_Integer && pOp->p1==pTab->iDb ){ 258048c530cSdrh useTempTable = 1; 259048c530cSdrh } 260048c530cSdrh } 261048c530cSdrh } 262142e30dfSdrh 263142e30dfSdrh if( useTempTable ){ 264142e30dfSdrh /* Generate the subroutine that SELECT calls to process each row of 265142e30dfSdrh ** the result. Store the result in a temporary table 266142e30dfSdrh */ 267142e30dfSdrh srcTab = pParse->nTab++; 2684adee20fSdanielk1977 sqlite3VdbeResolveLabel(v, iInsertBlock); 2694adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_MakeRecord, nColumn, 0); 270*3d1bfeaaSdanielk1977 sqlite3AddRecordType(v, pTab); 2714adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_NewRecno, srcTab, 0); 2724adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Pull, 1, 0); 2734adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_PutIntKey, srcTab, 0); 2744adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Return, 0, 0); 275142e30dfSdrh 276142e30dfSdrh /* The following code runs first because the GOTO at the very top 277142e30dfSdrh ** of the program jumps to it. Create the temporary table, then jump 278142e30dfSdrh ** back up and execute the SELECT code above. 279142e30dfSdrh */ 2804adee20fSdanielk1977 sqlite3VdbeChangeP2(v, iInitCode, sqlite3VdbeCurrentAddr(v)); 2814adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_OpenTemp, srcTab, 0); 2824adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Goto, 0, iSelectLoop); 2834adee20fSdanielk1977 sqlite3VdbeResolveLabel(v, iCleanup); 2845974a30fSdrh }else{ 2854adee20fSdanielk1977 sqlite3VdbeChangeP2(v, iInitCode, sqlite3VdbeCurrentAddr(v)); 286142e30dfSdrh } 287142e30dfSdrh }else{ 288142e30dfSdrh /* This is the case if the data for the INSERT is coming from a VALUES 289142e30dfSdrh ** clause 290142e30dfSdrh */ 291ad3cab52Sdrh SrcList dummy; 292daffd0e5Sdrh assert( pList!=0 ); 2935974a30fSdrh srcTab = -1; 294142e30dfSdrh useTempTable = 0; 2955974a30fSdrh assert( pList ); 296967e8b73Sdrh nColumn = pList->nExpr; 297ad3cab52Sdrh dummy.nSrc = 0; 298e64e7b20Sdrh for(i=0; i<nColumn; i++){ 2994adee20fSdanielk1977 if( sqlite3ExprResolveIds(pParse, &dummy, 0, pList->a[i].pExpr) ){ 300e64e7b20Sdrh goto insert_cleanup; 301e64e7b20Sdrh } 3024adee20fSdanielk1977 if( sqlite3ExprCheck(pParse, pList->a[i].pExpr, 0, 0) ){ 303b04a5d87Sdrh goto insert_cleanup; 304b04a5d87Sdrh } 305e64e7b20Sdrh } 3065974a30fSdrh } 3071ccde15dSdrh 3081ccde15dSdrh /* Make sure the number of columns in the source data matches the number 3091ccde15dSdrh ** of columns to be inserted into the table. 3101ccde15dSdrh */ 311967e8b73Sdrh if( pColumn==0 && nColumn!=pTab->nCol ){ 3124adee20fSdanielk1977 sqlite3ErrorMsg(pParse, 313da93d238Sdrh "table %S has %d columns but %d values were supplied", 314da93d238Sdrh pTabList, 0, pTab->nCol, nColumn); 315cce7d176Sdrh goto insert_cleanup; 316cce7d176Sdrh } 317967e8b73Sdrh if( pColumn!=0 && nColumn!=pColumn->nId ){ 3184adee20fSdanielk1977 sqlite3ErrorMsg(pParse, "%d values for %d columns", nColumn, pColumn->nId); 319cce7d176Sdrh goto insert_cleanup; 320cce7d176Sdrh } 3211ccde15dSdrh 3221ccde15dSdrh /* If the INSERT statement included an IDLIST term, then make sure 3231ccde15dSdrh ** all elements of the IDLIST really are columns of the table and 3241ccde15dSdrh ** remember the column indices. 325c8392586Sdrh ** 326c8392586Sdrh ** If the table has an INTEGER PRIMARY KEY column and that column 327c8392586Sdrh ** is named in the IDLIST, then record in the keyColumn variable 328c8392586Sdrh ** the index into IDLIST of the primary key column. keyColumn is 329c8392586Sdrh ** the index of the primary key as it appears in IDLIST, not as 330c8392586Sdrh ** is appears in the original table. (The index of the primary 331c8392586Sdrh ** key in the original table is pTab->iPKey.) 3321ccde15dSdrh */ 333967e8b73Sdrh if( pColumn ){ 334967e8b73Sdrh for(i=0; i<pColumn->nId; i++){ 335967e8b73Sdrh pColumn->a[i].idx = -1; 336cce7d176Sdrh } 337967e8b73Sdrh for(i=0; i<pColumn->nId; i++){ 338cce7d176Sdrh for(j=0; j<pTab->nCol; j++){ 3394adee20fSdanielk1977 if( sqlite3StrICmp(pColumn->a[i].zName, pTab->aCol[j].zName)==0 ){ 340967e8b73Sdrh pColumn->a[i].idx = j; 3414a32431cSdrh if( j==pTab->iPKey ){ 3429aa028daSdrh keyColumn = i; 3434a32431cSdrh } 344cce7d176Sdrh break; 345cce7d176Sdrh } 346cce7d176Sdrh } 347cce7d176Sdrh if( j>=pTab->nCol ){ 3484adee20fSdanielk1977 if( sqlite3IsRowid(pColumn->a[i].zName) ){ 349a0217ba7Sdrh keyColumn = i; 350a0217ba7Sdrh }else{ 3514adee20fSdanielk1977 sqlite3ErrorMsg(pParse, "table %S has no column named %s", 352da93d238Sdrh pTabList, 0, pColumn->a[i].zName); 353cce7d176Sdrh pParse->nErr++; 354cce7d176Sdrh goto insert_cleanup; 355cce7d176Sdrh } 356cce7d176Sdrh } 357cce7d176Sdrh } 358a0217ba7Sdrh } 3591ccde15dSdrh 360aacc543eSdrh /* If there is no IDLIST term but the table has an integer primary 361c8392586Sdrh ** key, the set the keyColumn variable to the primary key column index 362c8392586Sdrh ** in the original table definition. 3634a32431cSdrh */ 3644a32431cSdrh if( pColumn==0 ){ 3654a32431cSdrh keyColumn = pTab->iPKey; 3664a32431cSdrh } 3674a32431cSdrh 368142e30dfSdrh /* Open the temp table for FOR EACH ROW triggers 369142e30dfSdrh */ 370f29ce559Sdanielk1977 if( row_triggers_exist ){ 3714adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_OpenPseudo, newIdx, 0); 372f29ce559Sdanielk1977 } 373c3f9bad2Sdanielk1977 374c3f9bad2Sdanielk1977 /* Initialize the count of rows to be inserted 3751ccde15dSdrh */ 376142e30dfSdrh if( db->flags & SQLITE_CountRows ){ 377142e30dfSdrh iCntMem = pParse->nMem++; 3784adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Integer, 0, 0); 3794adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_MemStore, iCntMem, 1); 380c3f9bad2Sdanielk1977 } 381c3f9bad2Sdanielk1977 382c3f9bad2Sdanielk1977 /* Open tables and indices if there are no row triggers */ 383c3f9bad2Sdanielk1977 if( !row_triggers_exist ){ 3845974a30fSdrh base = pParse->nTab; 3854adee20fSdanielk1977 idx = sqlite3OpenTableAndIndices(pParse, pTab, base); 386832508b7Sdrh pParse->nTab += idx; 387feeb1394Sdrh } 388feeb1394Sdrh 389142e30dfSdrh /* If the data source is a temporary table, then we have to create 3901ccde15dSdrh ** a loop because there might be multiple rows of data. If the data 391142e30dfSdrh ** source is a subroutine call from the SELECT statement, then we need 392142e30dfSdrh ** to launch the SELECT statement processing. 3931ccde15dSdrh */ 394142e30dfSdrh if( useTempTable ){ 3954adee20fSdanielk1977 iBreak = sqlite3VdbeMakeLabel(v); 3964adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Rewind, srcTab, iBreak); 3974adee20fSdanielk1977 iCont = sqlite3VdbeCurrentAddr(v); 398142e30dfSdrh }else if( pSelect ){ 3994adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Goto, 0, iSelectLoop); 4004adee20fSdanielk1977 sqlite3VdbeResolveLabel(v, iInsertBlock); 401bed8690fSdrh } 4021ccde15dSdrh 4035cf590c1Sdrh /* Run the BEFORE and INSTEAD OF triggers, if there are any 40470ce3f0cSdrh */ 4054adee20fSdanielk1977 endOfLoop = sqlite3VdbeMakeLabel(v); 40670ce3f0cSdrh if( before_triggers ){ 407c3f9bad2Sdanielk1977 40870ce3f0cSdrh /* build the NEW.* reference row. Note that if there is an INTEGER 40970ce3f0cSdrh ** PRIMARY KEY into which a NULL is being inserted, that NULL will be 41070ce3f0cSdrh ** translated into a unique ID for the row. But on a BEFORE trigger, 41170ce3f0cSdrh ** we do not know what the unique ID will be (because the insert has 41270ce3f0cSdrh ** not happened yet) so we substitute a rowid of -1 41370ce3f0cSdrh */ 41470ce3f0cSdrh if( keyColumn<0 ){ 4154adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Integer, -1, 0); 41670ce3f0cSdrh }else if( useTempTable ){ 4174adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Column, srcTab, keyColumn); 41870ce3f0cSdrh }else if( pSelect ){ 4194adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Dup, nColumn - keyColumn - 1, 1); 42070ce3f0cSdrh }else{ 4214adee20fSdanielk1977 sqlite3ExprCode(pParse, pList->a[keyColumn].pExpr); 4224adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_NotNull, -1, sqlite3VdbeCurrentAddr(v)+3); 4234adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Pop, 1, 0); 4244adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Integer, -1, 0); 4254adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_MustBeInt, 0, 0); 42670ce3f0cSdrh } 42770ce3f0cSdrh 42870ce3f0cSdrh /* Create the new column data 42970ce3f0cSdrh */ 430c3f9bad2Sdanielk1977 for(i=0; i<pTab->nCol; i++){ 431c3f9bad2Sdanielk1977 if( pColumn==0 ){ 432c3f9bad2Sdanielk1977 j = i; 433c3f9bad2Sdanielk1977 }else{ 434c3f9bad2Sdanielk1977 for(j=0; j<pColumn->nId; j++){ 435c3f9bad2Sdanielk1977 if( pColumn->a[j].idx==i ) break; 436c3f9bad2Sdanielk1977 } 437c3f9bad2Sdanielk1977 } 438c3f9bad2Sdanielk1977 if( pColumn && j>=pColumn->nId ){ 4394adee20fSdanielk1977 sqlite3VdbeOp3(v, OP_String, 0, 0, pTab->aCol[i].zDflt, P3_STATIC); 440142e30dfSdrh }else if( useTempTable ){ 4414adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Column, srcTab, j); 442142e30dfSdrh }else if( pSelect ){ 4434adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Dup, nColumn-j-1, 1); 444c3f9bad2Sdanielk1977 }else{ 4454adee20fSdanielk1977 sqlite3ExprCode(pParse, pList->a[j].pExpr); 446c3f9bad2Sdanielk1977 } 447c3f9bad2Sdanielk1977 } 4484adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_MakeRecord, pTab->nCol, 0); 449*3d1bfeaaSdanielk1977 sqlite3AddRecordType(v, pTab); 4504adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_PutIntKey, newIdx, 0); 451c3f9bad2Sdanielk1977 4525cf590c1Sdrh /* Fire BEFORE or INSTEAD OF triggers */ 4534adee20fSdanielk1977 if( sqlite3CodeRowTrigger(pParse, TK_INSERT, 0, TK_BEFORE, pTab, 454b2fe7d8cSdrh newIdx, -1, onError, endOfLoop) ){ 455f29ce559Sdanielk1977 goto insert_cleanup; 456f29ce559Sdanielk1977 } 45770ce3f0cSdrh } 458c3f9bad2Sdanielk1977 45970ce3f0cSdrh /* If any triggers exists, the opening of tables and indices is deferred 46070ce3f0cSdrh ** until now. 46170ce3f0cSdrh */ 4625cf590c1Sdrh if( row_triggers_exist && !isView ){ 463c3f9bad2Sdanielk1977 base = pParse->nTab; 4644adee20fSdanielk1977 idx = sqlite3OpenTableAndIndices(pParse, pTab, base); 465c3f9bad2Sdanielk1977 pParse->nTab += idx; 466c3f9bad2Sdanielk1977 } 467c3f9bad2Sdanielk1977 4684a32431cSdrh /* Push the record number for the new entry onto the stack. The 4694a32431cSdrh ** record number is a randomly generate integer created by NewRecno 4704a32431cSdrh ** except when the table has an INTEGER PRIMARY KEY column, in which 471b419a926Sdrh ** case the record number is the same as that column. 4721ccde15dSdrh */ 4735cf590c1Sdrh if( !isView ){ 4744a32431cSdrh if( keyColumn>=0 ){ 475142e30dfSdrh if( useTempTable ){ 4764adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Column, srcTab, keyColumn); 477142e30dfSdrh }else if( pSelect ){ 4784adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Dup, nColumn - keyColumn - 1, 1); 4794a32431cSdrh }else{ 4804adee20fSdanielk1977 sqlite3ExprCode(pParse, pList->a[keyColumn].pExpr); 48127a32783Sdrh } 482e1e68f49Sdrh /* If the PRIMARY KEY expression is NULL, then use OP_NewRecno 483e1e68f49Sdrh ** to generate a unique primary key value. 484e1e68f49Sdrh */ 4854adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_NotNull, -1, sqlite3VdbeCurrentAddr(v)+3); 4864adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Pop, 1, 0); 4874adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_NewRecno, base, 0); 4884adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_MustBeInt, 0, 0); 4894a32431cSdrh }else{ 4904adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_NewRecno, base, 0); 4914a32431cSdrh } 4924a32431cSdrh 493aacc543eSdrh /* Push onto the stack, data for all columns of the new entry, beginning 4944a32431cSdrh ** with the first column. 4954a32431cSdrh */ 496cce7d176Sdrh for(i=0; i<pTab->nCol; i++){ 4974a32431cSdrh if( i==pTab->iPKey ){ 4984a32431cSdrh /* The value of the INTEGER PRIMARY KEY column is always a NULL. 499aacc543eSdrh ** Whenever this column is read, the record number will be substituted 500aacc543eSdrh ** in its place. So will fill this column with a NULL to avoid 501aacc543eSdrh ** taking up data space with information that will never be used. */ 5024adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_String, 0, 0); 5034a32431cSdrh continue; 5044a32431cSdrh } 505967e8b73Sdrh if( pColumn==0 ){ 506cce7d176Sdrh j = i; 507cce7d176Sdrh }else{ 508967e8b73Sdrh for(j=0; j<pColumn->nId; j++){ 509967e8b73Sdrh if( pColumn->a[j].idx==i ) break; 510cce7d176Sdrh } 511cce7d176Sdrh } 512967e8b73Sdrh if( pColumn && j>=pColumn->nId ){ 5134adee20fSdanielk1977 sqlite3VdbeOp3(v, OP_String, 0, 0, pTab->aCol[i].zDflt, P3_STATIC); 514142e30dfSdrh }else if( useTempTable ){ 5154adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Column, srcTab, j); 516142e30dfSdrh }else if( pSelect ){ 5174adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Dup, i+nColumn-j, 1); 518cce7d176Sdrh }else{ 5194adee20fSdanielk1977 sqlite3ExprCode(pParse, pList->a[j].pExpr); 520cce7d176Sdrh } 521cce7d176Sdrh } 5221ccde15dSdrh 5230ca3e24bSdrh /* Generate code to check constraints and generate index keys and 5240ca3e24bSdrh ** do the insertion. 5254a32431cSdrh */ 5264adee20fSdanielk1977 sqlite3GenerateConstraintChecks(pParse, pTab, base, 0, keyColumn>=0, 527a0217ba7Sdrh 0, onError, endOfLoop); 5284adee20fSdanielk1977 sqlite3CompleteInsertion(pParse, pTab, base, 0,0,0, 52970ce3f0cSdrh after_triggers ? newIdx : -1); 5305cf590c1Sdrh } 5311bee3d7bSdrh 532feeb1394Sdrh /* Update the count of rows that are inserted 5331bee3d7bSdrh */ 534142e30dfSdrh if( (db->flags & SQLITE_CountRows)!=0 ){ 5354adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_MemIncr, iCntMem, 0); 5361bee3d7bSdrh } 537c3f9bad2Sdanielk1977 538c3f9bad2Sdanielk1977 if( row_triggers_exist ){ 539c3f9bad2Sdanielk1977 /* Close all tables opened */ 5405cf590c1Sdrh if( !isView ){ 5414adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Close, base, 0); 542c3f9bad2Sdanielk1977 for(idx=1, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, idx++){ 5434adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Close, idx+base, 0); 544c3f9bad2Sdanielk1977 } 545c3f9bad2Sdanielk1977 } 546c3f9bad2Sdanielk1977 547c3f9bad2Sdanielk1977 /* Code AFTER triggers */ 5484adee20fSdanielk1977 if( sqlite3CodeRowTrigger(pParse, TK_INSERT, 0, TK_AFTER, pTab, newIdx, -1, 5496f34903eSdanielk1977 onError, endOfLoop) ){ 550f29ce559Sdanielk1977 goto insert_cleanup; 551f29ce559Sdanielk1977 } 552c3f9bad2Sdanielk1977 } 5531bee3d7bSdrh 5541ccde15dSdrh /* The bottom of the loop, if the data source is a SELECT statement 5551ccde15dSdrh */ 5564adee20fSdanielk1977 sqlite3VdbeResolveLabel(v, endOfLoop); 557142e30dfSdrh if( useTempTable ){ 5584adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Next, srcTab, iCont); 5594adee20fSdanielk1977 sqlite3VdbeResolveLabel(v, iBreak); 5604adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Close, srcTab, 0); 561142e30dfSdrh }else if( pSelect ){ 5624adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Pop, nColumn, 0); 5634adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Return, 0, 0); 5644adee20fSdanielk1977 sqlite3VdbeResolveLabel(v, iCleanup); 5656b56344dSdrh } 566c3f9bad2Sdanielk1977 567c3f9bad2Sdanielk1977 if( !row_triggers_exist ){ 568c3f9bad2Sdanielk1977 /* Close all tables opened */ 5694adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Close, base, 0); 5706b56344dSdrh for(idx=1, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, idx++){ 5714adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Close, idx+base, 0); 572cce7d176Sdrh } 573c3f9bad2Sdanielk1977 } 574c3f9bad2Sdanielk1977 5754adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_SetCounts, 0, 0); 5764adee20fSdanielk1977 sqlite3EndWriteOperation(pParse); 5775e00f6c7Sdrh 5781bee3d7bSdrh /* 5791bee3d7bSdrh ** Return the number of rows inserted. 5801bee3d7bSdrh */ 581142e30dfSdrh if( db->flags & SQLITE_CountRows ){ 5824adee20fSdanielk1977 sqlite3VdbeOp3(v, OP_ColumnName, 0, 1, "rows inserted", P3_STATIC); 5834adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_MemLoad, iCntMem, 0); 5844adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Callback, 1, 0); 5851bee3d7bSdrh } 586cce7d176Sdrh 587cce7d176Sdrh insert_cleanup: 5884adee20fSdanielk1977 sqlite3SrcListDelete(pTabList); 5894adee20fSdanielk1977 if( pList ) sqlite3ExprListDelete(pList); 5904adee20fSdanielk1977 if( pSelect ) sqlite3SelectDelete(pSelect); 5914adee20fSdanielk1977 sqlite3IdListDelete(pColumn); 592cce7d176Sdrh } 5939cfcf5d4Sdrh 5949cfcf5d4Sdrh /* 5959cfcf5d4Sdrh ** Generate code to do a constraint check prior to an INSERT or an UPDATE. 5969cfcf5d4Sdrh ** 5979cfcf5d4Sdrh ** When this routine is called, the stack contains (from bottom to top) 5980ca3e24bSdrh ** the following values: 5990ca3e24bSdrh ** 600b2fe7d8cSdrh ** 1. The recno of the row to be updated before the update. This 601b419a926Sdrh ** value is omitted unless we are doing an UPDATE that involves a 602b419a926Sdrh ** change to the record number. 6030ca3e24bSdrh ** 604b419a926Sdrh ** 2. The recno of the row after the update. 6050ca3e24bSdrh ** 6060ca3e24bSdrh ** 3. The data in the first column of the entry after the update. 6070ca3e24bSdrh ** 6080ca3e24bSdrh ** i. Data from middle columns... 6090ca3e24bSdrh ** 6100ca3e24bSdrh ** N. The data in the last column of the entry after the update. 6110ca3e24bSdrh ** 612b419a926Sdrh ** The old recno shown as entry (1) above is omitted unless both isUpdate 6131c92853dSdrh ** and recnoChng are 1. isUpdate is true for UPDATEs and false for 6141c92853dSdrh ** INSERTs and recnoChng is true if the record number is being changed. 6150ca3e24bSdrh ** 6160ca3e24bSdrh ** The code generated by this routine pushes additional entries onto 6170ca3e24bSdrh ** the stack which are the keys for new index entries for the new record. 6180ca3e24bSdrh ** The order of index keys is the same as the order of the indices on 6190ca3e24bSdrh ** the pTable->pIndex list. A key is only created for index i if 6200ca3e24bSdrh ** aIdxUsed!=0 and aIdxUsed[i]!=0. 6219cfcf5d4Sdrh ** 6229cfcf5d4Sdrh ** This routine also generates code to check constraints. NOT NULL, 6239cfcf5d4Sdrh ** CHECK, and UNIQUE constraints are all checked. If a constraint fails, 6241c92853dSdrh ** then the appropriate action is performed. There are five possible 6251c92853dSdrh ** actions: ROLLBACK, ABORT, FAIL, REPLACE, and IGNORE. 6269cfcf5d4Sdrh ** 6279cfcf5d4Sdrh ** Constraint type Action What Happens 6289cfcf5d4Sdrh ** --------------- ---------- ---------------------------------------- 6291c92853dSdrh ** any ROLLBACK The current transaction is rolled back and 63024b03fd0Sdanielk1977 ** sqlite3_exec() returns immediately with a 6319cfcf5d4Sdrh ** return code of SQLITE_CONSTRAINT. 6329cfcf5d4Sdrh ** 6331c92853dSdrh ** any ABORT Back out changes from the current command 6341c92853dSdrh ** only (do not do a complete rollback) then 63524b03fd0Sdanielk1977 ** cause sqlite3_exec() to return immediately 6361c92853dSdrh ** with SQLITE_CONSTRAINT. 6371c92853dSdrh ** 6381c92853dSdrh ** any FAIL Sqlite_exec() returns immediately with a 6391c92853dSdrh ** return code of SQLITE_CONSTRAINT. The 6401c92853dSdrh ** transaction is not rolled back and any 6411c92853dSdrh ** prior changes are retained. 6421c92853dSdrh ** 6439cfcf5d4Sdrh ** any IGNORE The record number and data is popped from 6449cfcf5d4Sdrh ** the stack and there is an immediate jump 6459cfcf5d4Sdrh ** to label ignoreDest. 6469cfcf5d4Sdrh ** 6479cfcf5d4Sdrh ** NOT NULL REPLACE The NULL value is replace by the default 6489cfcf5d4Sdrh ** value for that column. If the default value 6499cfcf5d4Sdrh ** is NULL, the action is the same as ABORT. 6509cfcf5d4Sdrh ** 6519cfcf5d4Sdrh ** UNIQUE REPLACE The other row that conflicts with the row 6529cfcf5d4Sdrh ** being inserted is removed. 6539cfcf5d4Sdrh ** 6549cfcf5d4Sdrh ** CHECK REPLACE Illegal. The results in an exception. 6559cfcf5d4Sdrh ** 6561c92853dSdrh ** Which action to take is determined by the overrideError parameter. 6571c92853dSdrh ** Or if overrideError==OE_Default, then the pParse->onError parameter 6581c92853dSdrh ** is used. Or if pParse->onError==OE_Default then the onError value 6591c92853dSdrh ** for the constraint is used. 6609cfcf5d4Sdrh ** 661aaab5725Sdrh ** The calling routine must open a read/write cursor for pTab with 6629cfcf5d4Sdrh ** cursor number "base". All indices of pTab must also have open 6639cfcf5d4Sdrh ** read/write cursors with cursor number base+i for the i-th cursor. 6649cfcf5d4Sdrh ** Except, if there is no possibility of a REPLACE action then 6659cfcf5d4Sdrh ** cursors do not need to be open for indices where aIdxUsed[i]==0. 6669cfcf5d4Sdrh ** 6679cfcf5d4Sdrh ** If the isUpdate flag is true, it means that the "base" cursor is 6689cfcf5d4Sdrh ** initially pointing to an entry that is being updated. The isUpdate 6699cfcf5d4Sdrh ** flag causes extra code to be generated so that the "base" cursor 6709cfcf5d4Sdrh ** is still pointing at the same entry after the routine returns. 6719cfcf5d4Sdrh ** Without the isUpdate flag, the "base" cursor might be moved. 6729cfcf5d4Sdrh */ 6734adee20fSdanielk1977 void sqlite3GenerateConstraintChecks( 6749cfcf5d4Sdrh Parse *pParse, /* The parser context */ 6759cfcf5d4Sdrh Table *pTab, /* the table into which we are inserting */ 6769cfcf5d4Sdrh int base, /* Index of a read/write cursor pointing at pTab */ 6779cfcf5d4Sdrh char *aIdxUsed, /* Which indices are used. NULL means all are used */ 6780ca3e24bSdrh int recnoChng, /* True if the record number will change */ 679b419a926Sdrh int isUpdate, /* True for UPDATE, False for INSERT */ 6809cfcf5d4Sdrh int overrideError, /* Override onError to this if not OE_Default */ 681b419a926Sdrh int ignoreDest /* Jump to this label on an OE_Ignore resolution */ 6829cfcf5d4Sdrh ){ 6839cfcf5d4Sdrh int i; 6849cfcf5d4Sdrh Vdbe *v; 6859cfcf5d4Sdrh int nCol; 6869cfcf5d4Sdrh int onError; 6879cfcf5d4Sdrh int addr; 6889cfcf5d4Sdrh int extra; 6890ca3e24bSdrh int iCur; 6900ca3e24bSdrh Index *pIdx; 6910ca3e24bSdrh int seenReplace = 0; 692f5905aa7Sdrh int jumpInst1, jumpInst2; 6930ca3e24bSdrh int contAddr; 694b419a926Sdrh int hasTwoRecnos = (isUpdate && recnoChng); 6959cfcf5d4Sdrh 6964adee20fSdanielk1977 v = sqlite3GetVdbe(pParse); 6979cfcf5d4Sdrh assert( v!=0 ); 698417be79cSdrh assert( pTab->pSelect==0 ); /* This table is not a VIEW */ 6999cfcf5d4Sdrh nCol = pTab->nCol; 7009cfcf5d4Sdrh 7019cfcf5d4Sdrh /* Test all NOT NULL constraints. 7029cfcf5d4Sdrh */ 7039cfcf5d4Sdrh for(i=0; i<nCol; i++){ 7040ca3e24bSdrh if( i==pTab->iPKey ){ 7050ca3e24bSdrh continue; 7060ca3e24bSdrh } 7079cfcf5d4Sdrh onError = pTab->aCol[i].notNull; 7080ca3e24bSdrh if( onError==OE_None ) continue; 7099cfcf5d4Sdrh if( overrideError!=OE_Default ){ 7109cfcf5d4Sdrh onError = overrideError; 711a996e477Sdrh }else if( pParse->db->onError!=OE_Default ){ 7120d65dc0eSdrh onError = pParse->db->onError; 713a996e477Sdrh }else if( onError==OE_Default ){ 714a996e477Sdrh onError = OE_Abort; 7159cfcf5d4Sdrh } 7169cfcf5d4Sdrh if( onError==OE_Replace && pTab->aCol[i].zDflt==0 ){ 7179cfcf5d4Sdrh onError = OE_Abort; 7189cfcf5d4Sdrh } 7194adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Dup, nCol-1-i, 1); 7204adee20fSdanielk1977 addr = sqlite3VdbeAddOp(v, OP_NotNull, 1, 0); 7219cfcf5d4Sdrh switch( onError ){ 7221c92853dSdrh case OE_Rollback: 7231c92853dSdrh case OE_Abort: 7241c92853dSdrh case OE_Fail: { 725483750baSdrh char *zMsg = 0; 7264adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Halt, SQLITE_CONSTRAINT, onError); 7274adee20fSdanielk1977 sqlite3SetString(&zMsg, pTab->zName, ".", pTab->aCol[i].zName, 72841743984Sdrh " may not be NULL", (char*)0); 7294adee20fSdanielk1977 sqlite3VdbeChangeP3(v, -1, zMsg, P3_DYNAMIC); 7309cfcf5d4Sdrh break; 7319cfcf5d4Sdrh } 7329cfcf5d4Sdrh case OE_Ignore: { 7334adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Pop, nCol+1+hasTwoRecnos, 0); 7344adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Goto, 0, ignoreDest); 7359cfcf5d4Sdrh break; 7369cfcf5d4Sdrh } 7379cfcf5d4Sdrh case OE_Replace: { 7384adee20fSdanielk1977 sqlite3VdbeOp3(v, OP_String, 0, 0, pTab->aCol[i].zDflt, P3_STATIC); 7394adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Push, nCol-i, 0); 7409cfcf5d4Sdrh break; 7419cfcf5d4Sdrh } 7420ca3e24bSdrh default: assert(0); 7439cfcf5d4Sdrh } 7444adee20fSdanielk1977 sqlite3VdbeChangeP2(v, addr, sqlite3VdbeCurrentAddr(v)); 7459cfcf5d4Sdrh } 7469cfcf5d4Sdrh 7479cfcf5d4Sdrh /* Test all CHECK constraints 7489cfcf5d4Sdrh */ 7490bd1f4eaSdrh /**** TBD ****/ 7509cfcf5d4Sdrh 7510bd1f4eaSdrh /* If we have an INTEGER PRIMARY KEY, make sure the primary key 7520bd1f4eaSdrh ** of the new record does not previously exist. Except, if this 7530bd1f4eaSdrh ** is an UPDATE and the primary key is not changing, that is OK. 7549cfcf5d4Sdrh */ 7555383ae5cSdrh if( recnoChng ){ 7560ca3e24bSdrh onError = pTab->keyConf; 7570ca3e24bSdrh if( overrideError!=OE_Default ){ 7580ca3e24bSdrh onError = overrideError; 759a996e477Sdrh }else if( pParse->db->onError!=OE_Default ){ 7600d65dc0eSdrh onError = pParse->db->onError; 761a996e477Sdrh }else if( onError==OE_Default ){ 762a996e477Sdrh onError = OE_Abort; 7630ca3e24bSdrh } 764a0217ba7Sdrh 76579b0c956Sdrh if( isUpdate ){ 7664adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Dup, nCol+1, 1); 7674adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Dup, nCol+1, 1); 7684adee20fSdanielk1977 jumpInst1 = sqlite3VdbeAddOp(v, OP_Eq, 0, 0); 76979b0c956Sdrh } 7704adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Dup, nCol, 1); 7714adee20fSdanielk1977 jumpInst2 = sqlite3VdbeAddOp(v, OP_NotExists, base, 0); 7720ca3e24bSdrh switch( onError ){ 773a0217ba7Sdrh default: { 774a0217ba7Sdrh onError = OE_Abort; 775a0217ba7Sdrh /* Fall thru into the next case */ 776a0217ba7Sdrh } 7771c92853dSdrh case OE_Rollback: 7781c92853dSdrh case OE_Abort: 7791c92853dSdrh case OE_Fail: { 7804adee20fSdanielk1977 sqlite3VdbeOp3(v, OP_Halt, SQLITE_CONSTRAINT, onError, 781701a0aebSdrh "PRIMARY KEY must be unique", P3_STATIC); 7820ca3e24bSdrh break; 7830ca3e24bSdrh } 7845383ae5cSdrh case OE_Replace: { 7854adee20fSdanielk1977 sqlite3GenerateRowIndexDelete(pParse->db, v, pTab, base, 0); 7865383ae5cSdrh if( isUpdate ){ 7874adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Dup, nCol+hasTwoRecnos, 1); 7884adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_MoveTo, base, 0); 7895383ae5cSdrh } 7905383ae5cSdrh seenReplace = 1; 7915383ae5cSdrh break; 7925383ae5cSdrh } 7930ca3e24bSdrh case OE_Ignore: { 7945383ae5cSdrh assert( seenReplace==0 ); 7954adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Pop, nCol+1+hasTwoRecnos, 0); 7964adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Goto, 0, ignoreDest); 7970ca3e24bSdrh break; 7980ca3e24bSdrh } 7990ca3e24bSdrh } 8004adee20fSdanielk1977 contAddr = sqlite3VdbeCurrentAddr(v); 8014adee20fSdanielk1977 sqlite3VdbeChangeP2(v, jumpInst2, contAddr); 802f5905aa7Sdrh if( isUpdate ){ 8034adee20fSdanielk1977 sqlite3VdbeChangeP2(v, jumpInst1, contAddr); 8044adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Dup, nCol+1, 1); 8054adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_MoveTo, base, 0); 8060ca3e24bSdrh } 8070ca3e24bSdrh } 8080bd1f4eaSdrh 8090bd1f4eaSdrh /* Test all UNIQUE constraints by creating entries for each UNIQUE 8100bd1f4eaSdrh ** index and making sure that duplicate entries do not already exist. 8110bd1f4eaSdrh ** Add the new records to the indices as we go. 8120bd1f4eaSdrh */ 813b2fe7d8cSdrh extra = -1; 814b2fe7d8cSdrh for(iCur=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, iCur++){ 815b2fe7d8cSdrh if( aIdxUsed && aIdxUsed[iCur]==0 ) continue; /* Skip unused indices */ 8169cfcf5d4Sdrh extra++; 817b2fe7d8cSdrh 818b2fe7d8cSdrh /* Create a key for accessing the index entry */ 8194adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Dup, nCol+extra, 1); 8209cfcf5d4Sdrh for(i=0; i<pIdx->nColumn; i++){ 8219cfcf5d4Sdrh int idx = pIdx->aiColumn[i]; 8229cfcf5d4Sdrh if( idx==pTab->iPKey ){ 8234adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Dup, i+extra+nCol+1, 1); 8249cfcf5d4Sdrh }else{ 8254adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Dup, i+extra+nCol-idx, 1); 8269cfcf5d4Sdrh } 8279cfcf5d4Sdrh } 8284adee20fSdanielk1977 jumpInst1 = sqlite3VdbeAddOp(v, OP_MakeIdxKey, pIdx->nColumn, 0); 8293d68f03aSdanielk1977 sqlite3AddIdxKeyType(v, pIdx); 830b2fe7d8cSdrh 831b2fe7d8cSdrh /* Find out what action to take in case there is an indexing conflict */ 8329cfcf5d4Sdrh onError = pIdx->onError; 833b2fe7d8cSdrh if( onError==OE_None ) continue; /* pIdx is not a UNIQUE index */ 8349cfcf5d4Sdrh if( overrideError!=OE_Default ){ 8359cfcf5d4Sdrh onError = overrideError; 836a996e477Sdrh }else if( pParse->db->onError!=OE_Default ){ 8370d65dc0eSdrh onError = pParse->db->onError; 838a996e477Sdrh }else if( onError==OE_Default ){ 839a996e477Sdrh onError = OE_Abort; 8409cfcf5d4Sdrh } 8415383ae5cSdrh if( seenReplace ){ 8425383ae5cSdrh if( onError==OE_Ignore ) onError = OE_Replace; 8435383ae5cSdrh else if( onError==OE_Fail ) onError = OE_Abort; 8445383ae5cSdrh } 8455383ae5cSdrh 846b2fe7d8cSdrh 847b2fe7d8cSdrh /* Check to see if the new index entry will be unique */ 8484adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Dup, extra+nCol+1+hasTwoRecnos, 1); 8494adee20fSdanielk1977 jumpInst2 = sqlite3VdbeAddOp(v, OP_IsUnique, base+iCur+1, 0); 850b2fe7d8cSdrh 851b2fe7d8cSdrh /* Generate code that executes if the new index entry is not unique */ 8529cfcf5d4Sdrh switch( onError ){ 8531c92853dSdrh case OE_Rollback: 8541c92853dSdrh case OE_Abort: 8551c92853dSdrh case OE_Fail: { 85637ed48edSdrh int j, n1, n2; 85737ed48edSdrh char zErrMsg[200]; 85837ed48edSdrh strcpy(zErrMsg, pIdx->nColumn>1 ? "columns " : "column "); 85937ed48edSdrh n1 = strlen(zErrMsg); 86037ed48edSdrh for(j=0; j<pIdx->nColumn && n1<sizeof(zErrMsg)-30; j++){ 86137ed48edSdrh char *zCol = pTab->aCol[pIdx->aiColumn[j]].zName; 86237ed48edSdrh n2 = strlen(zCol); 86337ed48edSdrh if( j>0 ){ 86437ed48edSdrh strcpy(&zErrMsg[n1], ", "); 86537ed48edSdrh n1 += 2; 86637ed48edSdrh } 86737ed48edSdrh if( n1+n2>sizeof(zErrMsg)-30 ){ 86837ed48edSdrh strcpy(&zErrMsg[n1], "..."); 86937ed48edSdrh n1 += 3; 87037ed48edSdrh break; 87137ed48edSdrh }else{ 87237ed48edSdrh strcpy(&zErrMsg[n1], zCol); 87337ed48edSdrh n1 += n2; 87437ed48edSdrh } 87537ed48edSdrh } 87637ed48edSdrh strcpy(&zErrMsg[n1], 87737ed48edSdrh pIdx->nColumn>1 ? " are not unique" : " is not unique"); 8784adee20fSdanielk1977 sqlite3VdbeOp3(v, OP_Halt, SQLITE_CONSTRAINT, onError, zErrMsg, 0); 8799cfcf5d4Sdrh break; 8809cfcf5d4Sdrh } 8819cfcf5d4Sdrh case OE_Ignore: { 8820ca3e24bSdrh assert( seenReplace==0 ); 8834adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Pop, nCol+extra+3+hasTwoRecnos, 0); 8844adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Goto, 0, ignoreDest); 8859cfcf5d4Sdrh break; 8869cfcf5d4Sdrh } 8879cfcf5d4Sdrh case OE_Replace: { 8884adee20fSdanielk1977 sqlite3GenerateRowDelete(pParse->db, v, pTab, base, 0); 8899cfcf5d4Sdrh if( isUpdate ){ 8904adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Dup, nCol+extra+1+hasTwoRecnos, 1); 8914adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_MoveTo, base, 0); 8929cfcf5d4Sdrh } 8930ca3e24bSdrh seenReplace = 1; 8949cfcf5d4Sdrh break; 8959cfcf5d4Sdrh } 8960ca3e24bSdrh default: assert(0); 8979cfcf5d4Sdrh } 8984adee20fSdanielk1977 contAddr = sqlite3VdbeCurrentAddr(v); 8990bd1f4eaSdrh #if NULL_DISTINCT_FOR_UNIQUE 9004adee20fSdanielk1977 sqlite3VdbeChangeP2(v, jumpInst1, contAddr); 9010bd1f4eaSdrh #endif 9024adee20fSdanielk1977 sqlite3VdbeChangeP2(v, jumpInst2, contAddr); 9039cfcf5d4Sdrh } 9049cfcf5d4Sdrh } 9050ca3e24bSdrh 9060ca3e24bSdrh /* 9070ca3e24bSdrh ** This routine generates code to finish the INSERT or UPDATE operation 9084adee20fSdanielk1977 ** that was started by a prior call to sqlite3GenerateConstraintChecks. 9090ca3e24bSdrh ** The stack must contain keys for all active indices followed by data 9100ca3e24bSdrh ** and the recno for the new entry. This routine creates the new 9110ca3e24bSdrh ** entries in all indices and in the main table. 9120ca3e24bSdrh ** 913b419a926Sdrh ** The arguments to this routine should be the same as the first six 9144adee20fSdanielk1977 ** arguments to sqlite3GenerateConstraintChecks. 9150ca3e24bSdrh */ 9164adee20fSdanielk1977 void sqlite3CompleteInsertion( 9170ca3e24bSdrh Parse *pParse, /* The parser context */ 9180ca3e24bSdrh Table *pTab, /* the table into which we are inserting */ 9190ca3e24bSdrh int base, /* Index of a read/write cursor pointing at pTab */ 9200ca3e24bSdrh char *aIdxUsed, /* Which indices are used. NULL means all are used */ 921b419a926Sdrh int recnoChng, /* True if the record number will change */ 92270ce3f0cSdrh int isUpdate, /* True for UPDATE, False for INSERT */ 92370ce3f0cSdrh int newIdx /* Index of NEW table for triggers. -1 if none */ 9240ca3e24bSdrh ){ 9250ca3e24bSdrh int i; 9260ca3e24bSdrh Vdbe *v; 9270ca3e24bSdrh int nIdx; 9280ca3e24bSdrh Index *pIdx; 9290ca3e24bSdrh 9304adee20fSdanielk1977 v = sqlite3GetVdbe(pParse); 9310ca3e24bSdrh assert( v!=0 ); 932417be79cSdrh assert( pTab->pSelect==0 ); /* This table is not a VIEW */ 9330ca3e24bSdrh for(nIdx=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, nIdx++){} 9340ca3e24bSdrh for(i=nIdx-1; i>=0; i--){ 9350ca3e24bSdrh if( aIdxUsed && aIdxUsed[i]==0 ) continue; 9364adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_IdxPut, base+i+1, 0); 9370ca3e24bSdrh } 9384adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_MakeRecord, pTab->nCol, 0); 939*3d1bfeaaSdanielk1977 sqlite3AddRecordType(v, pTab); 94070ce3f0cSdrh if( newIdx>=0 ){ 9414adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Dup, 1, 0); 9424adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Dup, 1, 0); 9434adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_PutIntKey, newIdx, 0); 94470ce3f0cSdrh } 9454adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_PutIntKey, base, 946b0c374ffSrdc (pParse->trigStack?0:OPFLAG_NCHANGE) | 947b0c374ffSrdc (isUpdate?0:OPFLAG_LASTROWID) | OPFLAG_CSCHANGE); 948b419a926Sdrh if( isUpdate && recnoChng ){ 9494adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Pop, 1, 0); 9500ca3e24bSdrh } 9510ca3e24bSdrh } 952cd44690aSdrh 953cd44690aSdrh /* 954cd44690aSdrh ** Generate code that will open write cursors for a table and for all 955cd44690aSdrh ** indices of that table. The "base" parameter is the cursor number used 956cd44690aSdrh ** for the table. Indices are opened on subsequent cursors. 957cd44690aSdrh ** 958cd44690aSdrh ** Return the total number of cursors opened. This is always at least 959cd44690aSdrh ** 1 (for the main table) plus more for each cursor. 960cd44690aSdrh */ 9614adee20fSdanielk1977 int sqlite3OpenTableAndIndices(Parse *pParse, Table *pTab, int base){ 962cd44690aSdrh int i; 963cd44690aSdrh Index *pIdx; 9644adee20fSdanielk1977 Vdbe *v = sqlite3GetVdbe(pParse); 965cd44690aSdrh assert( v!=0 ); 9664adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Integer, pTab->iDb, 0); 9674adee20fSdanielk1977 sqlite3VdbeOp3(v, OP_OpenWrite, base, pTab->tnum, pTab->zName, P3_STATIC); 968cd44690aSdrh for(i=1, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){ 9694adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Integer, pIdx->iDb, 0); 9704adee20fSdanielk1977 sqlite3VdbeOp3(v, OP_OpenWrite, i+base, pIdx->tnum, pIdx->zName, P3_STATIC); 971cd44690aSdrh } 972cd44690aSdrh return i; 973cd44690aSdrh } 9744adee20fSdanielk1977 9754adee20fSdanielk1977 9764adee20fSdanielk1977 977