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*2958a4e6Sdrh ** $Id: insert.c,v 1.125 2004/11/12 03:56:15 drh Exp $ 16cce7d176Sdrh */ 17cce7d176Sdrh #include "sqliteInt.h" 18cce7d176Sdrh 19cce7d176Sdrh /* 203d1bfeaaSdanielk1977 ** Set P3 of the most recently inserted opcode to a column affinity 21a37cdde0Sdanielk1977 ** string for index pIdx. A column affinity string has one character 223d1bfeaaSdanielk1977 ** for each column in the table, according to the affinity of the column: 233d1bfeaaSdanielk1977 ** 243d1bfeaaSdanielk1977 ** Character Column affinity 253d1bfeaaSdanielk1977 ** ------------------------------ 263d1bfeaaSdanielk1977 ** 'n' NUMERIC 273d1bfeaaSdanielk1977 ** 'i' INTEGER 283d1bfeaaSdanielk1977 ** 't' TEXT 293d1bfeaaSdanielk1977 ** 'o' NONE 303d1bfeaaSdanielk1977 */ 31a37cdde0Sdanielk1977 void sqlite3IndexAffinityStr(Vdbe *v, Index *pIdx){ 32a37cdde0Sdanielk1977 if( !pIdx->zColAff ){ 33e014a838Sdanielk1977 /* The first time a column affinity string for a particular index is 34a37cdde0Sdanielk1977 ** required, it is allocated and populated here. It is then stored as 35e014a838Sdanielk1977 ** a member of the Index structure for subsequent use. 36a37cdde0Sdanielk1977 ** 37a37cdde0Sdanielk1977 ** The column affinity string will eventually be deleted by 38e014a838Sdanielk1977 ** sqliteDeleteIndex() when the Index structure itself is cleaned 39a37cdde0Sdanielk1977 ** up. 40a37cdde0Sdanielk1977 */ 41a37cdde0Sdanielk1977 int n; 42a37cdde0Sdanielk1977 Table *pTab = pIdx->pTable; 43a37cdde0Sdanielk1977 pIdx->zColAff = (char *)sqliteMalloc(pIdx->nColumn+1); 44a37cdde0Sdanielk1977 if( !pIdx->zColAff ){ 45a37cdde0Sdanielk1977 return; 46a37cdde0Sdanielk1977 } 47a37cdde0Sdanielk1977 for(n=0; n<pIdx->nColumn; n++){ 48a37cdde0Sdanielk1977 pIdx->zColAff[n] = pTab->aCol[pIdx->aiColumn[n]].affinity; 49a37cdde0Sdanielk1977 } 50a37cdde0Sdanielk1977 pIdx->zColAff[pIdx->nColumn] = '\0'; 51a37cdde0Sdanielk1977 } 523d1bfeaaSdanielk1977 53956bc92cSdrh sqlite3VdbeChangeP3(v, -1, pIdx->zColAff, 0); 54a37cdde0Sdanielk1977 } 55a37cdde0Sdanielk1977 56a37cdde0Sdanielk1977 /* 57a37cdde0Sdanielk1977 ** Set P3 of the most recently inserted opcode to a column affinity 58a37cdde0Sdanielk1977 ** string for table pTab. A column affinity string has one character 59a37cdde0Sdanielk1977 ** for each column indexed by the index, according to the affinity of the 60a37cdde0Sdanielk1977 ** column: 61a37cdde0Sdanielk1977 ** 62a37cdde0Sdanielk1977 ** Character Column affinity 63a37cdde0Sdanielk1977 ** ------------------------------ 64a37cdde0Sdanielk1977 ** 'n' NUMERIC 65a37cdde0Sdanielk1977 ** 'i' INTEGER 66a37cdde0Sdanielk1977 ** 't' TEXT 67a37cdde0Sdanielk1977 ** 'o' NONE 68a37cdde0Sdanielk1977 */ 69a37cdde0Sdanielk1977 void sqlite3TableAffinityStr(Vdbe *v, Table *pTab){ 703d1bfeaaSdanielk1977 /* The first time a column affinity string for a particular table 713d1bfeaaSdanielk1977 ** is required, it is allocated and populated here. It is then 723d1bfeaaSdanielk1977 ** stored as a member of the Table structure for subsequent use. 733d1bfeaaSdanielk1977 ** 743d1bfeaaSdanielk1977 ** The column affinity string will eventually be deleted by 753d1bfeaaSdanielk1977 ** sqlite3DeleteTable() when the Table structure itself is cleaned up. 763d1bfeaaSdanielk1977 */ 773d1bfeaaSdanielk1977 if( !pTab->zColAff ){ 783d1bfeaaSdanielk1977 char *zColAff; 793d1bfeaaSdanielk1977 int i; 803d1bfeaaSdanielk1977 81a37cdde0Sdanielk1977 zColAff = (char *)sqliteMalloc(pTab->nCol+1); 823d1bfeaaSdanielk1977 if( !zColAff ){ 83a37cdde0Sdanielk1977 return; 843d1bfeaaSdanielk1977 } 853d1bfeaaSdanielk1977 863d1bfeaaSdanielk1977 for(i=0; i<pTab->nCol; i++){ 87a37cdde0Sdanielk1977 zColAff[i] = pTab->aCol[i].affinity; 883d1bfeaaSdanielk1977 } 893d1bfeaaSdanielk1977 zColAff[pTab->nCol] = '\0'; 903d1bfeaaSdanielk1977 913d1bfeaaSdanielk1977 pTab->zColAff = zColAff; 923d1bfeaaSdanielk1977 } 933d1bfeaaSdanielk1977 94956bc92cSdrh sqlite3VdbeChangeP3(v, -1, pTab->zColAff, 0); 953d1bfeaaSdanielk1977 } 963d1bfeaaSdanielk1977 973d1bfeaaSdanielk1977 983d1bfeaaSdanielk1977 /* 991ccde15dSdrh ** This routine is call to handle SQL of the following forms: 100cce7d176Sdrh ** 101cce7d176Sdrh ** insert into TABLE (IDLIST) values(EXPRLIST) 1021ccde15dSdrh ** insert into TABLE (IDLIST) select 103cce7d176Sdrh ** 1041ccde15dSdrh ** The IDLIST following the table name is always optional. If omitted, 1051ccde15dSdrh ** then a list of all columns for the table is substituted. The IDLIST 106967e8b73Sdrh ** appears in the pColumn parameter. pColumn is NULL if IDLIST is omitted. 1071ccde15dSdrh ** 1081ccde15dSdrh ** The pList parameter holds EXPRLIST in the first form of the INSERT 1091ccde15dSdrh ** statement above, and pSelect is NULL. For the second form, pList is 1101ccde15dSdrh ** NULL and pSelect is a pointer to the select statement used to generate 1111ccde15dSdrh ** data for the insert. 112142e30dfSdrh ** 113142e30dfSdrh ** The code generated follows one of three templates. For a simple 114142e30dfSdrh ** select with data coming from a VALUES clause, the code executes 115142e30dfSdrh ** once straight down through. The template looks like this: 116142e30dfSdrh ** 117142e30dfSdrh ** open write cursor to <table> and its indices 118142e30dfSdrh ** puts VALUES clause expressions onto the stack 119142e30dfSdrh ** write the resulting record into <table> 120142e30dfSdrh ** cleanup 121142e30dfSdrh ** 122142e30dfSdrh ** If the statement is of the form 123142e30dfSdrh ** 124142e30dfSdrh ** INSERT INTO <table> SELECT ... 125142e30dfSdrh ** 126142e30dfSdrh ** And the SELECT clause does not read from <table> at any time, then 127142e30dfSdrh ** the generated code follows this template: 128142e30dfSdrh ** 129142e30dfSdrh ** goto B 130142e30dfSdrh ** A: setup for the SELECT 131142e30dfSdrh ** loop over the tables in the SELECT 132142e30dfSdrh ** gosub C 133142e30dfSdrh ** end loop 134142e30dfSdrh ** cleanup after the SELECT 135142e30dfSdrh ** goto D 136142e30dfSdrh ** B: open write cursor to <table> and its indices 137142e30dfSdrh ** goto A 138142e30dfSdrh ** C: insert the select result into <table> 139142e30dfSdrh ** return 140142e30dfSdrh ** D: cleanup 141142e30dfSdrh ** 142142e30dfSdrh ** The third template is used if the insert statement takes its 143142e30dfSdrh ** values from a SELECT but the data is being inserted into a table 144142e30dfSdrh ** that is also read as part of the SELECT. In the third form, 145142e30dfSdrh ** we have to use a intermediate table to store the results of 146142e30dfSdrh ** the select. The template is like this: 147142e30dfSdrh ** 148142e30dfSdrh ** goto B 149142e30dfSdrh ** A: setup for the SELECT 150142e30dfSdrh ** loop over the tables in the SELECT 151142e30dfSdrh ** gosub C 152142e30dfSdrh ** end loop 153142e30dfSdrh ** cleanup after the SELECT 154142e30dfSdrh ** goto D 155142e30dfSdrh ** C: insert the select result into the intermediate table 156142e30dfSdrh ** return 157142e30dfSdrh ** B: open a cursor to an intermediate table 158142e30dfSdrh ** goto A 159142e30dfSdrh ** D: open write cursor to <table> and its indices 160142e30dfSdrh ** loop over the intermediate table 161142e30dfSdrh ** transfer values form intermediate table into <table> 162142e30dfSdrh ** end the loop 163142e30dfSdrh ** cleanup 164cce7d176Sdrh */ 1654adee20fSdanielk1977 void sqlite3Insert( 166cce7d176Sdrh Parse *pParse, /* Parser context */ 167113088ecSdrh SrcList *pTabList, /* Name of table into which we are inserting */ 168cce7d176Sdrh ExprList *pList, /* List of values to be inserted */ 1695974a30fSdrh Select *pSelect, /* A SELECT statement to use as the data source */ 1709cfcf5d4Sdrh IdList *pColumn, /* Column names corresponding to IDLIST. */ 1719cfcf5d4Sdrh int onError /* How to handle constraint errors */ 172cce7d176Sdrh ){ 1735974a30fSdrh Table *pTab; /* The table to insert into */ 174113088ecSdrh char *zTab; /* Name of the table into which we are inserting */ 175e22a334bSdrh const char *zDb; /* Name of the database holding this table */ 1765974a30fSdrh int i, j, idx; /* Loop counters */ 1775974a30fSdrh Vdbe *v; /* Generate code into this virtual machine */ 1785974a30fSdrh Index *pIdx; /* For looping over indices of the table */ 179967e8b73Sdrh int nColumn; /* Number of columns in the data */ 180cfe9a69fSdanielk1977 int base = 0; /* VDBE Cursor number for pTab */ 181cfe9a69fSdanielk1977 int iCont=0,iBreak=0; /* Beginning and end of the loop over srcTab */ 1829bb575fdSdrh sqlite3 *db; /* The main database structure */ 1834a32431cSdrh int keyColumn = -1; /* Column that is the INTEGER PRIMARY KEY */ 1840ca3e24bSdrh int endOfLoop; /* Label for the end of the insertion loop */ 185142e30dfSdrh int useTempTable; /* Store SELECT results in intermediate table */ 186cfe9a69fSdanielk1977 int srcTab = 0; /* Data comes from this temporary cursor if >=0 */ 187cfe9a69fSdanielk1977 int iSelectLoop = 0; /* Address of code that implements the SELECT */ 188cfe9a69fSdanielk1977 int iCleanup = 0; /* Address of the cleanup code */ 189cfe9a69fSdanielk1977 int iInsertBlock = 0; /* Address of the subroutine used to insert data */ 190cfe9a69fSdanielk1977 int iCntMem = 0; /* Memory cell used for the row counter */ 191798da52cSdrh int newIdx = -1; /* Cursor for the NEW table */ 192*2958a4e6Sdrh Db *pDb; /* The database containing table being inserted into */ 193*2958a4e6Sdrh int counterMem = 0; /* Memory cell holding AUTOINCREMENT counter */ 194cce7d176Sdrh 195798da52cSdrh #ifndef SQLITE_OMIT_TRIGGER 196798da52cSdrh int isView; /* True if attempting to insert into a view */ 197c3f9bad2Sdanielk1977 int row_triggers_exist = 0; /* True if there are FOR EACH ROW triggers */ 19870ce3f0cSdrh int before_triggers; /* True if there are BEFORE triggers */ 19970ce3f0cSdrh int after_triggers; /* True if there are AFTER triggers */ 200798da52cSdrh #endif 201c3f9bad2Sdanielk1977 20224b03fd0Sdanielk1977 if( pParse->nErr || sqlite3_malloc_failed ) goto insert_cleanup; 203ecdc7530Sdrh db = pParse->db; 204daffd0e5Sdrh 2051ccde15dSdrh /* Locate the table into which we will be inserting new information. 2061ccde15dSdrh */ 207113088ecSdrh assert( pTabList->nSrc==1 ); 208113088ecSdrh zTab = pTabList->a[0].zName; 209daffd0e5Sdrh if( zTab==0 ) goto insert_cleanup; 2104adee20fSdanielk1977 pTab = sqlite3SrcListLookup(pParse, pTabList); 211c3f9bad2Sdanielk1977 if( pTab==0 ){ 212c3f9bad2Sdanielk1977 goto insert_cleanup; 213c3f9bad2Sdanielk1977 } 214e22a334bSdrh assert( pTab->iDb<db->nDb ); 215*2958a4e6Sdrh pDb = &db->aDb[pTab->iDb]; 216*2958a4e6Sdrh zDb = pDb->zName; 2174adee20fSdanielk1977 if( sqlite3AuthCheck(pParse, SQLITE_INSERT, pTab->zName, 0, zDb) ){ 2181962bda7Sdrh goto insert_cleanup; 2191962bda7Sdrh } 220c3f9bad2Sdanielk1977 221b7f9164eSdrh /* Figure out if we have any triggers and if the table being 222b7f9164eSdrh ** inserted into is a view 223b7f9164eSdrh */ 224b7f9164eSdrh #ifndef SQLITE_OMIT_TRIGGER 225b7f9164eSdrh before_triggers = sqlite3TriggersExist(pParse, pTab->pTrigger, 226b7f9164eSdrh TK_INSERT, TK_BEFORE, TK_ROW, 0); 227b7f9164eSdrh after_triggers = sqlite3TriggersExist(pParse, pTab->pTrigger, 228b7f9164eSdrh TK_INSERT, TK_AFTER, TK_ROW, 0); 229b7f9164eSdrh row_triggers_exist = before_triggers || after_triggers; 230b7f9164eSdrh isView = pTab->pSelect!=0; 231b7f9164eSdrh #else 232b7f9164eSdrh # define before_triggers 0 233b7f9164eSdrh # define after_triggers 0 234b7f9164eSdrh # define row_triggers_exist 0 235b7f9164eSdrh # define isView 0 236b7f9164eSdrh #endif 237b7f9164eSdrh #ifdef SQLITE_OMIT_VIEW 238b7f9164eSdrh # undef isView 239b7f9164eSdrh # define isView 0 240b7f9164eSdrh #endif 241b7f9164eSdrh 242c3f9bad2Sdanielk1977 /* Ensure that: 243c3f9bad2Sdanielk1977 * (a) the table is not read-only, 244c3f9bad2Sdanielk1977 * (b) that if it is a view then ON INSERT triggers exist 245c3f9bad2Sdanielk1977 */ 2464adee20fSdanielk1977 if( sqlite3IsReadOnly(pParse, pTab, before_triggers) ){ 247c3f9bad2Sdanielk1977 goto insert_cleanup; 248c3f9bad2Sdanielk1977 } 249a76b5dfcSdrh if( pTab==0 ) goto insert_cleanup; 2501ccde15dSdrh 251f573c99bSdrh /* If pTab is really a view, make sure it has been initialized. 252f573c99bSdrh */ 2534adee20fSdanielk1977 if( isView && sqlite3ViewGetColumnNames(pParse, pTab) ){ 254f573c99bSdrh goto insert_cleanup; 255f573c99bSdrh } 256f573c99bSdrh 2577cedc8d4Sdanielk1977 /* Ensure all required collation sequences are available. */ 2587cedc8d4Sdanielk1977 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){ 2597cedc8d4Sdanielk1977 if( sqlite3CheckIndexCollSeq(pParse, pIdx) ){ 2607cedc8d4Sdanielk1977 goto insert_cleanup; 2617cedc8d4Sdanielk1977 } 2627cedc8d4Sdanielk1977 } 2637cedc8d4Sdanielk1977 2641ccde15dSdrh /* Allocate a VDBE 2651ccde15dSdrh */ 2664adee20fSdanielk1977 v = sqlite3GetVdbe(pParse); 2675974a30fSdrh if( v==0 ) goto insert_cleanup; 2684794f735Sdrh if( pParse->nested==0 ) sqlite3VdbeCountChanges(v); 2694adee20fSdanielk1977 sqlite3BeginWriteOperation(pParse, pSelect || row_triggers_exist, pTab->iDb); 2701ccde15dSdrh 271c3f9bad2Sdanielk1977 /* if there are row triggers, allocate a temp table for new.* references. */ 272f29ce559Sdanielk1977 if( row_triggers_exist ){ 273c3f9bad2Sdanielk1977 newIdx = pParse->nTab++; 274f29ce559Sdanielk1977 } 275c3f9bad2Sdanielk1977 276*2958a4e6Sdrh #ifndef SQLITE_OMIT_AUTOINCREMENT 277*2958a4e6Sdrh /* If this is an AUTOINCREMENT table, look up the sequence number in the 278*2958a4e6Sdrh ** sqlite_sequence table and store it in a memory cell. Create a new 279*2958a4e6Sdrh ** sqlite_sequence table entry if one does not already exist. 280*2958a4e6Sdrh */ 281*2958a4e6Sdrh if( pTab->autoInc ){ 282*2958a4e6Sdrh counterMem = ++pParse->nMem; 283*2958a4e6Sdrh assert( counterMem>0 ); /* Must be so for OP_NewRecno to work right */ 284*2958a4e6Sdrh sqlite3NestedParse(pParse, 285*2958a4e6Sdrh "SELECT seq FROM %Q.sqlite_sequence WHERE name=%Q INTO %d", 286*2958a4e6Sdrh pDb->zName, pTab->zName, counterMem 287*2958a4e6Sdrh ); 288*2958a4e6Sdrh } 289*2958a4e6Sdrh #endif /* SQLITE_OMIT_AUTOINCREMENT */ 290*2958a4e6Sdrh 2911ccde15dSdrh /* Figure out how many columns of data are supplied. If the data 292142e30dfSdrh ** is coming from a SELECT statement, then this step also generates 293142e30dfSdrh ** all the code to implement the SELECT statement and invoke a subroutine 294142e30dfSdrh ** to process each row of the result. (Template 2.) If the SELECT 295142e30dfSdrh ** statement uses the the table that is being inserted into, then the 296142e30dfSdrh ** subroutine is also coded here. That subroutine stores the SELECT 297142e30dfSdrh ** results in a temporary table. (Template 3.) 2981ccde15dSdrh */ 2995974a30fSdrh if( pSelect ){ 300142e30dfSdrh /* Data is coming from a SELECT. Generate code to implement that SELECT 301142e30dfSdrh */ 302142e30dfSdrh int rc, iInitCode; 3034adee20fSdanielk1977 iInitCode = sqlite3VdbeAddOp(v, OP_Goto, 0, 0); 3044adee20fSdanielk1977 iSelectLoop = sqlite3VdbeCurrentAddr(v); 3054adee20fSdanielk1977 iInsertBlock = sqlite3VdbeMakeLabel(v); 30684ac9d02Sdanielk1977 rc = sqlite3Select(pParse, pSelect, SRT_Subroutine, iInsertBlock, 0,0,0,0); 30724b03fd0Sdanielk1977 if( rc || pParse->nErr || sqlite3_malloc_failed ) goto insert_cleanup; 3084adee20fSdanielk1977 iCleanup = sqlite3VdbeMakeLabel(v); 3094adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Goto, 0, iCleanup); 3105974a30fSdrh assert( pSelect->pEList ); 311967e8b73Sdrh nColumn = pSelect->pEList->nExpr; 312142e30dfSdrh 313142e30dfSdrh /* Set useTempTable to TRUE if the result of the SELECT statement 314142e30dfSdrh ** should be written into a temporary table. Set to FALSE if each 315142e30dfSdrh ** row of the SELECT can be written directly into the result table. 316048c530cSdrh ** 317048c530cSdrh ** A temp table must be used if the table being updated is also one 318048c530cSdrh ** of the tables being read by the SELECT statement. Also use a 319048c530cSdrh ** temp table in the case of row triggers. 320142e30dfSdrh */ 321048c530cSdrh if( row_triggers_exist ){ 322048c530cSdrh useTempTable = 1; 323048c530cSdrh }else{ 324a42707b2Sdrh int addr = 0; 325048c530cSdrh useTempTable = 0; 326a42707b2Sdrh while( useTempTable==0 ){ 327a42707b2Sdrh VdbeOp *pOp; 328a42707b2Sdrh addr = sqlite3VdbeFindOp(v, addr, OP_OpenRead, pTab->tnum); 329a42707b2Sdrh if( addr==0 ) break; 330a42707b2Sdrh pOp = sqlite3VdbeGetOp(v, addr-2); 331048c530cSdrh if( pOp->opcode==OP_Integer && pOp->p1==pTab->iDb ){ 332048c530cSdrh useTempTable = 1; 333048c530cSdrh } 334048c530cSdrh } 335048c530cSdrh } 336142e30dfSdrh 337142e30dfSdrh if( useTempTable ){ 338142e30dfSdrh /* Generate the subroutine that SELECT calls to process each row of 339142e30dfSdrh ** the result. Store the result in a temporary table 340142e30dfSdrh */ 341142e30dfSdrh srcTab = pParse->nTab++; 3424adee20fSdanielk1977 sqlite3VdbeResolveLabel(v, iInsertBlock); 3434adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_MakeRecord, nColumn, 0); 344a37cdde0Sdanielk1977 sqlite3TableAffinityStr(v, pTab); 3454adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_NewRecno, srcTab, 0); 3464adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Pull, 1, 0); 3474adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_PutIntKey, srcTab, 0); 3484adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Return, 0, 0); 349142e30dfSdrh 350142e30dfSdrh /* The following code runs first because the GOTO at the very top 351142e30dfSdrh ** of the program jumps to it. Create the temporary table, then jump 352142e30dfSdrh ** back up and execute the SELECT code above. 353142e30dfSdrh */ 3544adee20fSdanielk1977 sqlite3VdbeChangeP2(v, iInitCode, sqlite3VdbeCurrentAddr(v)); 3554adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_OpenTemp, srcTab, 0); 356b6f5452fSdrh sqlite3VdbeAddOp(v, OP_SetNumColumns, srcTab, nColumn); 3574adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Goto, 0, iSelectLoop); 3584adee20fSdanielk1977 sqlite3VdbeResolveLabel(v, iCleanup); 3595974a30fSdrh }else{ 3604adee20fSdanielk1977 sqlite3VdbeChangeP2(v, iInitCode, sqlite3VdbeCurrentAddr(v)); 361142e30dfSdrh } 362142e30dfSdrh }else{ 363142e30dfSdrh /* This is the case if the data for the INSERT is coming from a VALUES 364142e30dfSdrh ** clause 365142e30dfSdrh */ 366ad3cab52Sdrh SrcList dummy; 367daffd0e5Sdrh assert( pList!=0 ); 3685974a30fSdrh srcTab = -1; 369142e30dfSdrh useTempTable = 0; 3705974a30fSdrh assert( pList ); 371967e8b73Sdrh nColumn = pList->nExpr; 372ad3cab52Sdrh dummy.nSrc = 0; 373e64e7b20Sdrh for(i=0; i<nColumn; i++){ 374290c1948Sdrh if( sqlite3ExprResolveAndCheck(pParse,&dummy,0,pList->a[i].pExpr,0,0) ){ 375b04a5d87Sdrh goto insert_cleanup; 376b04a5d87Sdrh } 377e64e7b20Sdrh } 3785974a30fSdrh } 3791ccde15dSdrh 3801ccde15dSdrh /* Make sure the number of columns in the source data matches the number 3811ccde15dSdrh ** of columns to be inserted into the table. 3821ccde15dSdrh */ 383967e8b73Sdrh if( pColumn==0 && nColumn!=pTab->nCol ){ 3844adee20fSdanielk1977 sqlite3ErrorMsg(pParse, 385da93d238Sdrh "table %S has %d columns but %d values were supplied", 386da93d238Sdrh pTabList, 0, pTab->nCol, nColumn); 387cce7d176Sdrh goto insert_cleanup; 388cce7d176Sdrh } 389967e8b73Sdrh if( pColumn!=0 && nColumn!=pColumn->nId ){ 3904adee20fSdanielk1977 sqlite3ErrorMsg(pParse, "%d values for %d columns", nColumn, pColumn->nId); 391cce7d176Sdrh goto insert_cleanup; 392cce7d176Sdrh } 3931ccde15dSdrh 3941ccde15dSdrh /* If the INSERT statement included an IDLIST term, then make sure 3951ccde15dSdrh ** all elements of the IDLIST really are columns of the table and 3961ccde15dSdrh ** remember the column indices. 397c8392586Sdrh ** 398c8392586Sdrh ** If the table has an INTEGER PRIMARY KEY column and that column 399c8392586Sdrh ** is named in the IDLIST, then record in the keyColumn variable 400c8392586Sdrh ** the index into IDLIST of the primary key column. keyColumn is 401c8392586Sdrh ** the index of the primary key as it appears in IDLIST, not as 402c8392586Sdrh ** is appears in the original table. (The index of the primary 403c8392586Sdrh ** key in the original table is pTab->iPKey.) 4041ccde15dSdrh */ 405967e8b73Sdrh if( pColumn ){ 406967e8b73Sdrh for(i=0; i<pColumn->nId; i++){ 407967e8b73Sdrh pColumn->a[i].idx = -1; 408cce7d176Sdrh } 409967e8b73Sdrh for(i=0; i<pColumn->nId; i++){ 410cce7d176Sdrh for(j=0; j<pTab->nCol; j++){ 4114adee20fSdanielk1977 if( sqlite3StrICmp(pColumn->a[i].zName, pTab->aCol[j].zName)==0 ){ 412967e8b73Sdrh pColumn->a[i].idx = j; 4134a32431cSdrh if( j==pTab->iPKey ){ 4149aa028daSdrh keyColumn = i; 4154a32431cSdrh } 416cce7d176Sdrh break; 417cce7d176Sdrh } 418cce7d176Sdrh } 419cce7d176Sdrh if( j>=pTab->nCol ){ 4204adee20fSdanielk1977 if( sqlite3IsRowid(pColumn->a[i].zName) ){ 421a0217ba7Sdrh keyColumn = i; 422a0217ba7Sdrh }else{ 4234adee20fSdanielk1977 sqlite3ErrorMsg(pParse, "table %S has no column named %s", 424da93d238Sdrh pTabList, 0, pColumn->a[i].zName); 425cce7d176Sdrh pParse->nErr++; 426cce7d176Sdrh goto insert_cleanup; 427cce7d176Sdrh } 428cce7d176Sdrh } 429cce7d176Sdrh } 430a0217ba7Sdrh } 4311ccde15dSdrh 432aacc543eSdrh /* If there is no IDLIST term but the table has an integer primary 433c8392586Sdrh ** key, the set the keyColumn variable to the primary key column index 434c8392586Sdrh ** in the original table definition. 4354a32431cSdrh */ 4364a32431cSdrh if( pColumn==0 ){ 4374a32431cSdrh keyColumn = pTab->iPKey; 4384a32431cSdrh } 4394a32431cSdrh 440142e30dfSdrh /* Open the temp table for FOR EACH ROW triggers 441142e30dfSdrh */ 442f29ce559Sdanielk1977 if( row_triggers_exist ){ 4434adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_OpenPseudo, newIdx, 0); 44484ac9d02Sdanielk1977 sqlite3VdbeAddOp(v, OP_SetNumColumns, newIdx, pTab->nCol); 445f29ce559Sdanielk1977 } 446c3f9bad2Sdanielk1977 447c3f9bad2Sdanielk1977 /* Initialize the count of rows to be inserted 4481ccde15dSdrh */ 449142e30dfSdrh if( db->flags & SQLITE_CountRows ){ 450142e30dfSdrh iCntMem = pParse->nMem++; 4514adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Integer, 0, 0); 4524adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_MemStore, iCntMem, 1); 453c3f9bad2Sdanielk1977 } 454c3f9bad2Sdanielk1977 455c3f9bad2Sdanielk1977 /* Open tables and indices if there are no row triggers */ 456c3f9bad2Sdanielk1977 if( !row_triggers_exist ){ 4575974a30fSdrh base = pParse->nTab; 458290c1948Sdrh sqlite3OpenTableAndIndices(pParse, pTab, base, OP_OpenWrite); 459feeb1394Sdrh } 460feeb1394Sdrh 461142e30dfSdrh /* If the data source is a temporary table, then we have to create 4621ccde15dSdrh ** a loop because there might be multiple rows of data. If the data 463142e30dfSdrh ** source is a subroutine call from the SELECT statement, then we need 464142e30dfSdrh ** to launch the SELECT statement processing. 4651ccde15dSdrh */ 466142e30dfSdrh if( useTempTable ){ 4674adee20fSdanielk1977 iBreak = sqlite3VdbeMakeLabel(v); 4684adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Rewind, srcTab, iBreak); 4694adee20fSdanielk1977 iCont = sqlite3VdbeCurrentAddr(v); 470142e30dfSdrh }else if( pSelect ){ 4714adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Goto, 0, iSelectLoop); 4724adee20fSdanielk1977 sqlite3VdbeResolveLabel(v, iInsertBlock); 473bed8690fSdrh } 4741ccde15dSdrh 4755cf590c1Sdrh /* Run the BEFORE and INSTEAD OF triggers, if there are any 47670ce3f0cSdrh */ 4774adee20fSdanielk1977 endOfLoop = sqlite3VdbeMakeLabel(v); 47870ce3f0cSdrh if( before_triggers ){ 479c3f9bad2Sdanielk1977 48070ce3f0cSdrh /* build the NEW.* reference row. Note that if there is an INTEGER 48170ce3f0cSdrh ** PRIMARY KEY into which a NULL is being inserted, that NULL will be 48270ce3f0cSdrh ** translated into a unique ID for the row. But on a BEFORE trigger, 48370ce3f0cSdrh ** we do not know what the unique ID will be (because the insert has 48470ce3f0cSdrh ** not happened yet) so we substitute a rowid of -1 48570ce3f0cSdrh */ 48670ce3f0cSdrh if( keyColumn<0 ){ 4874adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Integer, -1, 0); 48870ce3f0cSdrh }else if( useTempTable ){ 4894adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Column, srcTab, keyColumn); 49070ce3f0cSdrh }else if( pSelect ){ 4914adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Dup, nColumn - keyColumn - 1, 1); 49270ce3f0cSdrh }else{ 4934adee20fSdanielk1977 sqlite3ExprCode(pParse, pList->a[keyColumn].pExpr); 4944adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_NotNull, -1, sqlite3VdbeCurrentAddr(v)+3); 4954adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Pop, 1, 0); 4964adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Integer, -1, 0); 4974adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_MustBeInt, 0, 0); 49870ce3f0cSdrh } 49970ce3f0cSdrh 50070ce3f0cSdrh /* Create the new column data 50170ce3f0cSdrh */ 502c3f9bad2Sdanielk1977 for(i=0; i<pTab->nCol; i++){ 503c3f9bad2Sdanielk1977 if( pColumn==0 ){ 504c3f9bad2Sdanielk1977 j = i; 505c3f9bad2Sdanielk1977 }else{ 506c3f9bad2Sdanielk1977 for(j=0; j<pColumn->nId; j++){ 507c3f9bad2Sdanielk1977 if( pColumn->a[j].idx==i ) break; 508c3f9bad2Sdanielk1977 } 509c3f9bad2Sdanielk1977 } 510c3f9bad2Sdanielk1977 if( pColumn && j>=pColumn->nId ){ 5117977a17fSdanielk1977 sqlite3ExprCode(pParse, pTab->aCol[i].pDflt); 512142e30dfSdrh }else if( useTempTable ){ 5134adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Column, srcTab, j); 514142e30dfSdrh }else if( pSelect ){ 5154adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Dup, nColumn-j-1, 1); 516c3f9bad2Sdanielk1977 }else{ 5174adee20fSdanielk1977 sqlite3ExprCode(pParse, pList->a[j].pExpr); 518c3f9bad2Sdanielk1977 } 519c3f9bad2Sdanielk1977 } 5204adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_MakeRecord, pTab->nCol, 0); 521a37cdde0Sdanielk1977 522a37cdde0Sdanielk1977 /* If this is an INSERT on a view with an INSTEAD OF INSERT trigger, 523a37cdde0Sdanielk1977 ** do not attempt any conversions before assembling the record. 524a37cdde0Sdanielk1977 ** If this is a real table, attempt conversions as required by the 525a37cdde0Sdanielk1977 ** table column affinities. 526a37cdde0Sdanielk1977 */ 527a37cdde0Sdanielk1977 if( !isView ){ 528a37cdde0Sdanielk1977 sqlite3TableAffinityStr(v, pTab); 529a37cdde0Sdanielk1977 } 5304adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_PutIntKey, newIdx, 0); 531c3f9bad2Sdanielk1977 5325cf590c1Sdrh /* Fire BEFORE or INSTEAD OF triggers */ 5334adee20fSdanielk1977 if( sqlite3CodeRowTrigger(pParse, TK_INSERT, 0, TK_BEFORE, pTab, 534b2fe7d8cSdrh newIdx, -1, onError, endOfLoop) ){ 535f29ce559Sdanielk1977 goto insert_cleanup; 536f29ce559Sdanielk1977 } 53770ce3f0cSdrh } 538c3f9bad2Sdanielk1977 53970ce3f0cSdrh /* If any triggers exists, the opening of tables and indices is deferred 54070ce3f0cSdrh ** until now. 54170ce3f0cSdrh */ 5425cf590c1Sdrh if( row_triggers_exist && !isView ){ 543c3f9bad2Sdanielk1977 base = pParse->nTab; 544290c1948Sdrh sqlite3OpenTableAndIndices(pParse, pTab, base, OP_OpenWrite); 545c3f9bad2Sdanielk1977 } 546c3f9bad2Sdanielk1977 5474a32431cSdrh /* Push the record number for the new entry onto the stack. The 5484a32431cSdrh ** record number is a randomly generate integer created by NewRecno 5494a32431cSdrh ** except when the table has an INTEGER PRIMARY KEY column, in which 550b419a926Sdrh ** case the record number is the same as that column. 5511ccde15dSdrh */ 5525cf590c1Sdrh if( !isView ){ 5534a32431cSdrh if( keyColumn>=0 ){ 554142e30dfSdrh if( useTempTable ){ 5554adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Column, srcTab, keyColumn); 556142e30dfSdrh }else if( pSelect ){ 5574adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Dup, nColumn - keyColumn - 1, 1); 5584a32431cSdrh }else{ 5594adee20fSdanielk1977 sqlite3ExprCode(pParse, pList->a[keyColumn].pExpr); 56027a32783Sdrh } 561e1e68f49Sdrh /* If the PRIMARY KEY expression is NULL, then use OP_NewRecno 562e1e68f49Sdrh ** to generate a unique primary key value. 563e1e68f49Sdrh */ 5644adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_NotNull, -1, sqlite3VdbeCurrentAddr(v)+3); 5654adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Pop, 1, 0); 566*2958a4e6Sdrh sqlite3VdbeAddOp(v, OP_NewRecno, base, counterMem); 5674adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_MustBeInt, 0, 0); 5684a32431cSdrh }else{ 5694adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_NewRecno, base, 0); 5704a32431cSdrh } 571*2958a4e6Sdrh #ifndef SQLITE_OMIT_AUTOINCREMENT 572*2958a4e6Sdrh if( pTab->autoInc ){ 573*2958a4e6Sdrh sqlite3VdbeAddOp(v, OP_MemMax, counterMem, 0); 574*2958a4e6Sdrh } 575*2958a4e6Sdrh #endif /* SQLITE_OMIT_AUTOINCREMENT */ 5764a32431cSdrh 577aacc543eSdrh /* Push onto the stack, data for all columns of the new entry, beginning 5784a32431cSdrh ** with the first column. 5794a32431cSdrh */ 580cce7d176Sdrh for(i=0; i<pTab->nCol; i++){ 5814a32431cSdrh if( i==pTab->iPKey ){ 5824a32431cSdrh /* The value of the INTEGER PRIMARY KEY column is always a NULL. 583aacc543eSdrh ** Whenever this column is read, the record number will be substituted 584aacc543eSdrh ** in its place. So will fill this column with a NULL to avoid 585aacc543eSdrh ** taking up data space with information that will never be used. */ 5860f69c1e3Sdanielk1977 sqlite3VdbeAddOp(v, OP_String8, 0, 0); 5874a32431cSdrh continue; 5884a32431cSdrh } 589967e8b73Sdrh if( pColumn==0 ){ 590cce7d176Sdrh j = i; 591cce7d176Sdrh }else{ 592967e8b73Sdrh for(j=0; j<pColumn->nId; j++){ 593967e8b73Sdrh if( pColumn->a[j].idx==i ) break; 594cce7d176Sdrh } 595cce7d176Sdrh } 596967e8b73Sdrh if( pColumn && j>=pColumn->nId ){ 5977977a17fSdanielk1977 sqlite3ExprCode(pParse, pTab->aCol[i].pDflt); 598142e30dfSdrh }else if( useTempTable ){ 5994adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Column, srcTab, j); 600142e30dfSdrh }else if( pSelect ){ 6014adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Dup, i+nColumn-j, 1); 602cce7d176Sdrh }else{ 6034adee20fSdanielk1977 sqlite3ExprCode(pParse, pList->a[j].pExpr); 604cce7d176Sdrh } 605cce7d176Sdrh } 6061ccde15dSdrh 6070ca3e24bSdrh /* Generate code to check constraints and generate index keys and 6080ca3e24bSdrh ** do the insertion. 6094a32431cSdrh */ 6104adee20fSdanielk1977 sqlite3GenerateConstraintChecks(pParse, pTab, base, 0, keyColumn>=0, 611a0217ba7Sdrh 0, onError, endOfLoop); 6124adee20fSdanielk1977 sqlite3CompleteInsertion(pParse, pTab, base, 0,0,0, 61370ce3f0cSdrh after_triggers ? newIdx : -1); 6145cf590c1Sdrh } 6151bee3d7bSdrh 616feeb1394Sdrh /* Update the count of rows that are inserted 6171bee3d7bSdrh */ 618142e30dfSdrh if( (db->flags & SQLITE_CountRows)!=0 ){ 6194adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_MemIncr, iCntMem, 0); 6201bee3d7bSdrh } 621c3f9bad2Sdanielk1977 622c3f9bad2Sdanielk1977 if( row_triggers_exist ){ 623c3f9bad2Sdanielk1977 /* Close all tables opened */ 6245cf590c1Sdrh if( !isView ){ 6254adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Close, base, 0); 626c3f9bad2Sdanielk1977 for(idx=1, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, idx++){ 6274adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Close, idx+base, 0); 628c3f9bad2Sdanielk1977 } 629c3f9bad2Sdanielk1977 } 630c3f9bad2Sdanielk1977 631c3f9bad2Sdanielk1977 /* Code AFTER triggers */ 6324adee20fSdanielk1977 if( sqlite3CodeRowTrigger(pParse, TK_INSERT, 0, TK_AFTER, pTab, newIdx, -1, 6336f34903eSdanielk1977 onError, endOfLoop) ){ 634f29ce559Sdanielk1977 goto insert_cleanup; 635f29ce559Sdanielk1977 } 636c3f9bad2Sdanielk1977 } 6371bee3d7bSdrh 6381ccde15dSdrh /* The bottom of the loop, if the data source is a SELECT statement 6391ccde15dSdrh */ 6404adee20fSdanielk1977 sqlite3VdbeResolveLabel(v, endOfLoop); 641142e30dfSdrh if( useTempTable ){ 6424adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Next, srcTab, iCont); 6434adee20fSdanielk1977 sqlite3VdbeResolveLabel(v, iBreak); 6444adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Close, srcTab, 0); 645142e30dfSdrh }else if( pSelect ){ 6464adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Pop, nColumn, 0); 6474adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Return, 0, 0); 6484adee20fSdanielk1977 sqlite3VdbeResolveLabel(v, iCleanup); 6496b56344dSdrh } 650c3f9bad2Sdanielk1977 651c3f9bad2Sdanielk1977 if( !row_triggers_exist ){ 652c3f9bad2Sdanielk1977 /* Close all tables opened */ 6534adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Close, base, 0); 6546b56344dSdrh for(idx=1, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, idx++){ 6554adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Close, idx+base, 0); 656cce7d176Sdrh } 657c3f9bad2Sdanielk1977 } 658c3f9bad2Sdanielk1977 659*2958a4e6Sdrh #ifndef SQLITE_OMIT_AUTOINCREMENT 660*2958a4e6Sdrh /* Update the sqlite_sequence table 661*2958a4e6Sdrh */ 662*2958a4e6Sdrh if( pTab->autoInc ){ 663*2958a4e6Sdrh sqlite3NestedParse(pParse, 664*2958a4e6Sdrh "UPDATE %Q.sqlite_sequence SET seq=#-%d WHERE name=%Q", 665*2958a4e6Sdrh pDb->zName, counterMem+1, pTab->zName 666*2958a4e6Sdrh ); 667*2958a4e6Sdrh } 668*2958a4e6Sdrh #endif 669*2958a4e6Sdrh 6701bee3d7bSdrh /* 671e7de6f25Sdanielk1977 ** Return the number of rows inserted. If this routine is 672e7de6f25Sdanielk1977 ** generating code because of a call to sqlite3NestedParse(), do not 673e7de6f25Sdanielk1977 ** invoke the callback function. 6741bee3d7bSdrh */ 675e7de6f25Sdanielk1977 if( db->flags & SQLITE_CountRows && pParse->nested==0 ){ 6764adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_MemLoad, iCntMem, 0); 6774adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Callback, 1, 0); 67822322fd4Sdanielk1977 sqlite3VdbeSetNumCols(v, 1); 6793cf86063Sdanielk1977 sqlite3VdbeSetColName(v, 0, "rows inserted", P3_STATIC); 6801bee3d7bSdrh } 681cce7d176Sdrh 682cce7d176Sdrh insert_cleanup: 6834adee20fSdanielk1977 sqlite3SrcListDelete(pTabList); 6844adee20fSdanielk1977 if( pList ) sqlite3ExprListDelete(pList); 6854adee20fSdanielk1977 if( pSelect ) sqlite3SelectDelete(pSelect); 6864adee20fSdanielk1977 sqlite3IdListDelete(pColumn); 687cce7d176Sdrh } 6889cfcf5d4Sdrh 6899cfcf5d4Sdrh /* 6909cfcf5d4Sdrh ** Generate code to do a constraint check prior to an INSERT or an UPDATE. 6919cfcf5d4Sdrh ** 6929cfcf5d4Sdrh ** When this routine is called, the stack contains (from bottom to top) 6930ca3e24bSdrh ** the following values: 6940ca3e24bSdrh ** 695b2fe7d8cSdrh ** 1. The recno of the row to be updated before the update. This 696b419a926Sdrh ** value is omitted unless we are doing an UPDATE that involves a 697b419a926Sdrh ** change to the record number. 6980ca3e24bSdrh ** 699b419a926Sdrh ** 2. The recno of the row after the update. 7000ca3e24bSdrh ** 7010ca3e24bSdrh ** 3. The data in the first column of the entry after the update. 7020ca3e24bSdrh ** 7030ca3e24bSdrh ** i. Data from middle columns... 7040ca3e24bSdrh ** 7050ca3e24bSdrh ** N. The data in the last column of the entry after the update. 7060ca3e24bSdrh ** 707b419a926Sdrh ** The old recno shown as entry (1) above is omitted unless both isUpdate 7081c92853dSdrh ** and recnoChng are 1. isUpdate is true for UPDATEs and false for 7091c92853dSdrh ** INSERTs and recnoChng is true if the record number is being changed. 7100ca3e24bSdrh ** 7110ca3e24bSdrh ** The code generated by this routine pushes additional entries onto 7120ca3e24bSdrh ** the stack which are the keys for new index entries for the new record. 7130ca3e24bSdrh ** The order of index keys is the same as the order of the indices on 7140ca3e24bSdrh ** the pTable->pIndex list. A key is only created for index i if 7150ca3e24bSdrh ** aIdxUsed!=0 and aIdxUsed[i]!=0. 7169cfcf5d4Sdrh ** 7179cfcf5d4Sdrh ** This routine also generates code to check constraints. NOT NULL, 7189cfcf5d4Sdrh ** CHECK, and UNIQUE constraints are all checked. If a constraint fails, 7191c92853dSdrh ** then the appropriate action is performed. There are five possible 7201c92853dSdrh ** actions: ROLLBACK, ABORT, FAIL, REPLACE, and IGNORE. 7219cfcf5d4Sdrh ** 7229cfcf5d4Sdrh ** Constraint type Action What Happens 7239cfcf5d4Sdrh ** --------------- ---------- ---------------------------------------- 7241c92853dSdrh ** any ROLLBACK The current transaction is rolled back and 72524b03fd0Sdanielk1977 ** sqlite3_exec() returns immediately with a 7269cfcf5d4Sdrh ** return code of SQLITE_CONSTRAINT. 7279cfcf5d4Sdrh ** 7281c92853dSdrh ** any ABORT Back out changes from the current command 7291c92853dSdrh ** only (do not do a complete rollback) then 73024b03fd0Sdanielk1977 ** cause sqlite3_exec() to return immediately 7311c92853dSdrh ** with SQLITE_CONSTRAINT. 7321c92853dSdrh ** 7331c92853dSdrh ** any FAIL Sqlite_exec() returns immediately with a 7341c92853dSdrh ** return code of SQLITE_CONSTRAINT. The 7351c92853dSdrh ** transaction is not rolled back and any 7361c92853dSdrh ** prior changes are retained. 7371c92853dSdrh ** 7389cfcf5d4Sdrh ** any IGNORE The record number and data is popped from 7399cfcf5d4Sdrh ** the stack and there is an immediate jump 7409cfcf5d4Sdrh ** to label ignoreDest. 7419cfcf5d4Sdrh ** 7429cfcf5d4Sdrh ** NOT NULL REPLACE The NULL value is replace by the default 7439cfcf5d4Sdrh ** value for that column. If the default value 7449cfcf5d4Sdrh ** is NULL, the action is the same as ABORT. 7459cfcf5d4Sdrh ** 7469cfcf5d4Sdrh ** UNIQUE REPLACE The other row that conflicts with the row 7479cfcf5d4Sdrh ** being inserted is removed. 7489cfcf5d4Sdrh ** 7499cfcf5d4Sdrh ** CHECK REPLACE Illegal. The results in an exception. 7509cfcf5d4Sdrh ** 7511c92853dSdrh ** Which action to take is determined by the overrideError parameter. 7521c92853dSdrh ** Or if overrideError==OE_Default, then the pParse->onError parameter 7531c92853dSdrh ** is used. Or if pParse->onError==OE_Default then the onError value 7541c92853dSdrh ** for the constraint is used. 7559cfcf5d4Sdrh ** 756aaab5725Sdrh ** The calling routine must open a read/write cursor for pTab with 7579cfcf5d4Sdrh ** cursor number "base". All indices of pTab must also have open 7589cfcf5d4Sdrh ** read/write cursors with cursor number base+i for the i-th cursor. 7599cfcf5d4Sdrh ** Except, if there is no possibility of a REPLACE action then 7609cfcf5d4Sdrh ** cursors do not need to be open for indices where aIdxUsed[i]==0. 7619cfcf5d4Sdrh ** 7629cfcf5d4Sdrh ** If the isUpdate flag is true, it means that the "base" cursor is 7639cfcf5d4Sdrh ** initially pointing to an entry that is being updated. The isUpdate 7649cfcf5d4Sdrh ** flag causes extra code to be generated so that the "base" cursor 7659cfcf5d4Sdrh ** is still pointing at the same entry after the routine returns. 7669cfcf5d4Sdrh ** Without the isUpdate flag, the "base" cursor might be moved. 7679cfcf5d4Sdrh */ 7684adee20fSdanielk1977 void sqlite3GenerateConstraintChecks( 7699cfcf5d4Sdrh Parse *pParse, /* The parser context */ 7709cfcf5d4Sdrh Table *pTab, /* the table into which we are inserting */ 7719cfcf5d4Sdrh int base, /* Index of a read/write cursor pointing at pTab */ 7729cfcf5d4Sdrh char *aIdxUsed, /* Which indices are used. NULL means all are used */ 7730ca3e24bSdrh int recnoChng, /* True if the record number will change */ 774b419a926Sdrh int isUpdate, /* True for UPDATE, False for INSERT */ 7759cfcf5d4Sdrh int overrideError, /* Override onError to this if not OE_Default */ 776b419a926Sdrh int ignoreDest /* Jump to this label on an OE_Ignore resolution */ 7779cfcf5d4Sdrh ){ 7789cfcf5d4Sdrh int i; 7799cfcf5d4Sdrh Vdbe *v; 7809cfcf5d4Sdrh int nCol; 7819cfcf5d4Sdrh int onError; 7829cfcf5d4Sdrh int addr; 7839cfcf5d4Sdrh int extra; 7840ca3e24bSdrh int iCur; 7850ca3e24bSdrh Index *pIdx; 7860ca3e24bSdrh int seenReplace = 0; 787cfe9a69fSdanielk1977 int jumpInst1=0, jumpInst2; 7880ca3e24bSdrh int contAddr; 789b419a926Sdrh int hasTwoRecnos = (isUpdate && recnoChng); 7909cfcf5d4Sdrh 7914adee20fSdanielk1977 v = sqlite3GetVdbe(pParse); 7929cfcf5d4Sdrh assert( v!=0 ); 793417be79cSdrh assert( pTab->pSelect==0 ); /* This table is not a VIEW */ 7949cfcf5d4Sdrh nCol = pTab->nCol; 7959cfcf5d4Sdrh 7969cfcf5d4Sdrh /* Test all NOT NULL constraints. 7979cfcf5d4Sdrh */ 7989cfcf5d4Sdrh for(i=0; i<nCol; i++){ 7990ca3e24bSdrh if( i==pTab->iPKey ){ 8000ca3e24bSdrh continue; 8010ca3e24bSdrh } 8029cfcf5d4Sdrh onError = pTab->aCol[i].notNull; 8030ca3e24bSdrh if( onError==OE_None ) continue; 8049cfcf5d4Sdrh if( overrideError!=OE_Default ){ 8059cfcf5d4Sdrh onError = overrideError; 806a996e477Sdrh }else if( onError==OE_Default ){ 807a996e477Sdrh onError = OE_Abort; 8089cfcf5d4Sdrh } 8097977a17fSdanielk1977 if( onError==OE_Replace && pTab->aCol[i].pDflt==0 ){ 8109cfcf5d4Sdrh onError = OE_Abort; 8119cfcf5d4Sdrh } 8124adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Dup, nCol-1-i, 1); 8134adee20fSdanielk1977 addr = sqlite3VdbeAddOp(v, OP_NotNull, 1, 0); 8149cfcf5d4Sdrh switch( onError ){ 8151c92853dSdrh case OE_Rollback: 8161c92853dSdrh case OE_Abort: 8171c92853dSdrh case OE_Fail: { 818483750baSdrh char *zMsg = 0; 8194adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Halt, SQLITE_CONSTRAINT, onError); 8204adee20fSdanielk1977 sqlite3SetString(&zMsg, pTab->zName, ".", pTab->aCol[i].zName, 82141743984Sdrh " may not be NULL", (char*)0); 8224adee20fSdanielk1977 sqlite3VdbeChangeP3(v, -1, zMsg, P3_DYNAMIC); 8239cfcf5d4Sdrh break; 8249cfcf5d4Sdrh } 8259cfcf5d4Sdrh case OE_Ignore: { 8264adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Pop, nCol+1+hasTwoRecnos, 0); 8274adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Goto, 0, ignoreDest); 8289cfcf5d4Sdrh break; 8299cfcf5d4Sdrh } 8309cfcf5d4Sdrh case OE_Replace: { 8317977a17fSdanielk1977 sqlite3ExprCode(pParse, pTab->aCol[i].pDflt); 8324adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Push, nCol-i, 0); 8339cfcf5d4Sdrh break; 8349cfcf5d4Sdrh } 8350ca3e24bSdrh default: assert(0); 8369cfcf5d4Sdrh } 8374adee20fSdanielk1977 sqlite3VdbeChangeP2(v, addr, sqlite3VdbeCurrentAddr(v)); 8389cfcf5d4Sdrh } 8399cfcf5d4Sdrh 8409cfcf5d4Sdrh /* Test all CHECK constraints 8419cfcf5d4Sdrh */ 8420bd1f4eaSdrh /**** TBD ****/ 8439cfcf5d4Sdrh 8440bd1f4eaSdrh /* If we have an INTEGER PRIMARY KEY, make sure the primary key 8450bd1f4eaSdrh ** of the new record does not previously exist. Except, if this 8460bd1f4eaSdrh ** is an UPDATE and the primary key is not changing, that is OK. 8479cfcf5d4Sdrh */ 8485383ae5cSdrh if( recnoChng ){ 8490ca3e24bSdrh onError = pTab->keyConf; 8500ca3e24bSdrh if( overrideError!=OE_Default ){ 8510ca3e24bSdrh onError = overrideError; 852a996e477Sdrh }else if( onError==OE_Default ){ 853a996e477Sdrh onError = OE_Abort; 8540ca3e24bSdrh } 855a0217ba7Sdrh 85679b0c956Sdrh if( isUpdate ){ 8574adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Dup, nCol+1, 1); 8584adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Dup, nCol+1, 1); 8594adee20fSdanielk1977 jumpInst1 = sqlite3VdbeAddOp(v, OP_Eq, 0, 0); 86079b0c956Sdrh } 8614adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Dup, nCol, 1); 8624adee20fSdanielk1977 jumpInst2 = sqlite3VdbeAddOp(v, OP_NotExists, base, 0); 8630ca3e24bSdrh switch( onError ){ 864a0217ba7Sdrh default: { 865a0217ba7Sdrh onError = OE_Abort; 866a0217ba7Sdrh /* Fall thru into the next case */ 867a0217ba7Sdrh } 8681c92853dSdrh case OE_Rollback: 8691c92853dSdrh case OE_Abort: 8701c92853dSdrh case OE_Fail: { 8714adee20fSdanielk1977 sqlite3VdbeOp3(v, OP_Halt, SQLITE_CONSTRAINT, onError, 872701a0aebSdrh "PRIMARY KEY must be unique", P3_STATIC); 8730ca3e24bSdrh break; 8740ca3e24bSdrh } 8755383ae5cSdrh case OE_Replace: { 8764adee20fSdanielk1977 sqlite3GenerateRowIndexDelete(pParse->db, v, pTab, base, 0); 8775383ae5cSdrh if( isUpdate ){ 8784adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Dup, nCol+hasTwoRecnos, 1); 8797cf6e4deSdrh sqlite3VdbeAddOp(v, OP_MoveGe, base, 0); 8805383ae5cSdrh } 8815383ae5cSdrh seenReplace = 1; 8825383ae5cSdrh break; 8835383ae5cSdrh } 8840ca3e24bSdrh case OE_Ignore: { 8855383ae5cSdrh assert( seenReplace==0 ); 8864adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Pop, nCol+1+hasTwoRecnos, 0); 8874adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Goto, 0, ignoreDest); 8880ca3e24bSdrh break; 8890ca3e24bSdrh } 8900ca3e24bSdrh } 8914adee20fSdanielk1977 contAddr = sqlite3VdbeCurrentAddr(v); 8924adee20fSdanielk1977 sqlite3VdbeChangeP2(v, jumpInst2, contAddr); 893f5905aa7Sdrh if( isUpdate ){ 8944adee20fSdanielk1977 sqlite3VdbeChangeP2(v, jumpInst1, contAddr); 8954adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Dup, nCol+1, 1); 8967cf6e4deSdrh sqlite3VdbeAddOp(v, OP_MoveGe, base, 0); 8970ca3e24bSdrh } 8980ca3e24bSdrh } 8990bd1f4eaSdrh 9000bd1f4eaSdrh /* Test all UNIQUE constraints by creating entries for each UNIQUE 9010bd1f4eaSdrh ** index and making sure that duplicate entries do not already exist. 9020bd1f4eaSdrh ** Add the new records to the indices as we go. 9030bd1f4eaSdrh */ 904b2fe7d8cSdrh extra = -1; 905b2fe7d8cSdrh for(iCur=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, iCur++){ 906b2fe7d8cSdrh if( aIdxUsed && aIdxUsed[iCur]==0 ) continue; /* Skip unused indices */ 9079cfcf5d4Sdrh extra++; 908b2fe7d8cSdrh 909b2fe7d8cSdrh /* Create a key for accessing the index entry */ 9104adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Dup, nCol+extra, 1); 9119cfcf5d4Sdrh for(i=0; i<pIdx->nColumn; i++){ 9129cfcf5d4Sdrh int idx = pIdx->aiColumn[i]; 9139cfcf5d4Sdrh if( idx==pTab->iPKey ){ 9144adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Dup, i+extra+nCol+1, 1); 9159cfcf5d4Sdrh }else{ 9164adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Dup, i+extra+nCol-idx, 1); 9179cfcf5d4Sdrh } 9189cfcf5d4Sdrh } 919ededfd5eSdanielk1977 jumpInst1 = sqlite3VdbeAddOp(v, OP_MakeRecord, pIdx->nColumn, (1<<24)); 920a37cdde0Sdanielk1977 sqlite3IndexAffinityStr(v, pIdx); 921b2fe7d8cSdrh 922b2fe7d8cSdrh /* Find out what action to take in case there is an indexing conflict */ 9239cfcf5d4Sdrh onError = pIdx->onError; 924b2fe7d8cSdrh if( onError==OE_None ) continue; /* pIdx is not a UNIQUE index */ 9259cfcf5d4Sdrh if( overrideError!=OE_Default ){ 9269cfcf5d4Sdrh onError = overrideError; 927a996e477Sdrh }else if( onError==OE_Default ){ 928a996e477Sdrh onError = OE_Abort; 9299cfcf5d4Sdrh } 9305383ae5cSdrh if( seenReplace ){ 9315383ae5cSdrh if( onError==OE_Ignore ) onError = OE_Replace; 9325383ae5cSdrh else if( onError==OE_Fail ) onError = OE_Abort; 9335383ae5cSdrh } 9345383ae5cSdrh 935b2fe7d8cSdrh 936b2fe7d8cSdrh /* Check to see if the new index entry will be unique */ 9374adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Dup, extra+nCol+1+hasTwoRecnos, 1); 9384adee20fSdanielk1977 jumpInst2 = sqlite3VdbeAddOp(v, OP_IsUnique, base+iCur+1, 0); 939b2fe7d8cSdrh 940b2fe7d8cSdrh /* Generate code that executes if the new index entry is not unique */ 9419cfcf5d4Sdrh switch( onError ){ 9421c92853dSdrh case OE_Rollback: 9431c92853dSdrh case OE_Abort: 9441c92853dSdrh case OE_Fail: { 94537ed48edSdrh int j, n1, n2; 94637ed48edSdrh char zErrMsg[200]; 94737ed48edSdrh strcpy(zErrMsg, pIdx->nColumn>1 ? "columns " : "column "); 94837ed48edSdrh n1 = strlen(zErrMsg); 94937ed48edSdrh for(j=0; j<pIdx->nColumn && n1<sizeof(zErrMsg)-30; j++){ 95037ed48edSdrh char *zCol = pTab->aCol[pIdx->aiColumn[j]].zName; 95137ed48edSdrh n2 = strlen(zCol); 95237ed48edSdrh if( j>0 ){ 95337ed48edSdrh strcpy(&zErrMsg[n1], ", "); 95437ed48edSdrh n1 += 2; 95537ed48edSdrh } 95637ed48edSdrh if( n1+n2>sizeof(zErrMsg)-30 ){ 95737ed48edSdrh strcpy(&zErrMsg[n1], "..."); 95837ed48edSdrh n1 += 3; 95937ed48edSdrh break; 96037ed48edSdrh }else{ 96137ed48edSdrh strcpy(&zErrMsg[n1], zCol); 96237ed48edSdrh n1 += n2; 96337ed48edSdrh } 96437ed48edSdrh } 96537ed48edSdrh strcpy(&zErrMsg[n1], 96637ed48edSdrh pIdx->nColumn>1 ? " are not unique" : " is not unique"); 9674adee20fSdanielk1977 sqlite3VdbeOp3(v, OP_Halt, SQLITE_CONSTRAINT, onError, zErrMsg, 0); 9689cfcf5d4Sdrh break; 9699cfcf5d4Sdrh } 9709cfcf5d4Sdrh case OE_Ignore: { 9710ca3e24bSdrh assert( seenReplace==0 ); 9724adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Pop, nCol+extra+3+hasTwoRecnos, 0); 9734adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Goto, 0, ignoreDest); 9749cfcf5d4Sdrh break; 9759cfcf5d4Sdrh } 9769cfcf5d4Sdrh case OE_Replace: { 9774adee20fSdanielk1977 sqlite3GenerateRowDelete(pParse->db, v, pTab, base, 0); 9789cfcf5d4Sdrh if( isUpdate ){ 9794adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Dup, nCol+extra+1+hasTwoRecnos, 1); 9807cf6e4deSdrh sqlite3VdbeAddOp(v, OP_MoveGe, base, 0); 9819cfcf5d4Sdrh } 9820ca3e24bSdrh seenReplace = 1; 9839cfcf5d4Sdrh break; 9849cfcf5d4Sdrh } 9850ca3e24bSdrh default: assert(0); 9869cfcf5d4Sdrh } 9874adee20fSdanielk1977 contAddr = sqlite3VdbeCurrentAddr(v); 988ededfd5eSdanielk1977 assert( contAddr<(1<<24) ); 9890bd1f4eaSdrh #if NULL_DISTINCT_FOR_UNIQUE 990ededfd5eSdanielk1977 sqlite3VdbeChangeP2(v, jumpInst1, contAddr | (1<<24)); 9910bd1f4eaSdrh #endif 9924adee20fSdanielk1977 sqlite3VdbeChangeP2(v, jumpInst2, contAddr); 9939cfcf5d4Sdrh } 9949cfcf5d4Sdrh } 9950ca3e24bSdrh 9960ca3e24bSdrh /* 9970ca3e24bSdrh ** This routine generates code to finish the INSERT or UPDATE operation 9984adee20fSdanielk1977 ** that was started by a prior call to sqlite3GenerateConstraintChecks. 9990ca3e24bSdrh ** The stack must contain keys for all active indices followed by data 10000ca3e24bSdrh ** and the recno for the new entry. This routine creates the new 10010ca3e24bSdrh ** entries in all indices and in the main table. 10020ca3e24bSdrh ** 1003b419a926Sdrh ** The arguments to this routine should be the same as the first six 10044adee20fSdanielk1977 ** arguments to sqlite3GenerateConstraintChecks. 10050ca3e24bSdrh */ 10064adee20fSdanielk1977 void sqlite3CompleteInsertion( 10070ca3e24bSdrh Parse *pParse, /* The parser context */ 10080ca3e24bSdrh Table *pTab, /* the table into which we are inserting */ 10090ca3e24bSdrh int base, /* Index of a read/write cursor pointing at pTab */ 10100ca3e24bSdrh char *aIdxUsed, /* Which indices are used. NULL means all are used */ 1011b419a926Sdrh int recnoChng, /* True if the record number will change */ 101270ce3f0cSdrh int isUpdate, /* True for UPDATE, False for INSERT */ 101370ce3f0cSdrh int newIdx /* Index of NEW table for triggers. -1 if none */ 10140ca3e24bSdrh ){ 10150ca3e24bSdrh int i; 10160ca3e24bSdrh Vdbe *v; 10170ca3e24bSdrh int nIdx; 10180ca3e24bSdrh Index *pIdx; 1019b28af71aSdanielk1977 int pik_flags; 10200ca3e24bSdrh 10214adee20fSdanielk1977 v = sqlite3GetVdbe(pParse); 10220ca3e24bSdrh assert( v!=0 ); 1023417be79cSdrh assert( pTab->pSelect==0 ); /* This table is not a VIEW */ 10240ca3e24bSdrh for(nIdx=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, nIdx++){} 10250ca3e24bSdrh for(i=nIdx-1; i>=0; i--){ 10260ca3e24bSdrh if( aIdxUsed && aIdxUsed[i]==0 ) continue; 10274adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_IdxPut, base+i+1, 0); 10280ca3e24bSdrh } 10294adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_MakeRecord, pTab->nCol, 0); 1030a37cdde0Sdanielk1977 sqlite3TableAffinityStr(v, pTab); 103170ce3f0cSdrh if( newIdx>=0 ){ 10324adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Dup, 1, 0); 10334adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Dup, 1, 0); 10344adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_PutIntKey, newIdx, 0); 103570ce3f0cSdrh } 10364794f735Sdrh if( pParse->nested ){ 10374794f735Sdrh pik_flags = 0; 10384794f735Sdrh }else{ 1039b28af71aSdanielk1977 pik_flags = (OPFLAG_NCHANGE|(isUpdate?0:OPFLAG_LASTROWID)); 10404794f735Sdrh } 1041b28af71aSdanielk1977 sqlite3VdbeAddOp(v, OP_PutIntKey, base, pik_flags); 1042b28af71aSdanielk1977 1043b419a926Sdrh if( isUpdate && recnoChng ){ 10444adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Pop, 1, 0); 10450ca3e24bSdrh } 10460ca3e24bSdrh } 1047cd44690aSdrh 1048cd44690aSdrh /* 1049290c1948Sdrh ** Generate code that will open cursors for a table and for all 1050cd44690aSdrh ** indices of that table. The "base" parameter is the cursor number used 1051cd44690aSdrh ** for the table. Indices are opened on subsequent cursors. 1052cd44690aSdrh */ 1053290c1948Sdrh void sqlite3OpenTableAndIndices( 1054290c1948Sdrh Parse *pParse, /* Parsing context */ 1055290c1948Sdrh Table *pTab, /* Table to be opened */ 1056290c1948Sdrh int base, /* Cursor number assigned to the table */ 1057290c1948Sdrh int op /* OP_OpenRead or OP_OpenWrite */ 1058290c1948Sdrh ){ 1059cd44690aSdrh int i; 1060cd44690aSdrh Index *pIdx; 10614adee20fSdanielk1977 Vdbe *v = sqlite3GetVdbe(pParse); 1062cd44690aSdrh assert( v!=0 ); 10634adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Integer, pTab->iDb, 0); 1064290c1948Sdrh sqlite3VdbeAddOp(v, op, base, pTab->tnum); 1065ad6d9460Sdrh VdbeComment((v, "# %s", pTab->zName)); 1066b4964b72Sdanielk1977 sqlite3VdbeAddOp(v, OP_SetNumColumns, base, pTab->nCol); 1067cd44690aSdrh for(i=1, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){ 10684adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Integer, pIdx->iDb, 0); 1069290c1948Sdrh sqlite3VdbeOp3(v, op, i+base, pIdx->tnum, 1070d3d39e93Sdrh (char*)&pIdx->keyInfo, P3_KEYINFO); 1071cd44690aSdrh } 1072290c1948Sdrh if( pParse->nTab<=base+i ){ 1073290c1948Sdrh pParse->nTab = base+i; 1074290c1948Sdrh } 1075cd44690aSdrh } 1076