xref: /illumos-gate/usr/src/lib/libsqlite/src/insert.c (revision 55fea89d)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate ** 2001 September 15
37c478bd9Sstevel@tonic-gate **
47c478bd9Sstevel@tonic-gate ** The author disclaims copyright to this source code.  In place of
57c478bd9Sstevel@tonic-gate ** a legal notice, here is a blessing:
67c478bd9Sstevel@tonic-gate **
77c478bd9Sstevel@tonic-gate **    May you do good and not evil.
87c478bd9Sstevel@tonic-gate **    May you find forgiveness for yourself and forgive others.
97c478bd9Sstevel@tonic-gate **    May you share freely, never taking more than you give.
107c478bd9Sstevel@tonic-gate **
117c478bd9Sstevel@tonic-gate *************************************************************************
127c478bd9Sstevel@tonic-gate ** This file contains C code routines that are called by the parser
137c478bd9Sstevel@tonic-gate ** to handle INSERT statements in SQLite.
147c478bd9Sstevel@tonic-gate **
157c478bd9Sstevel@tonic-gate ** $Id: insert.c,v 1.94 2004/02/24 01:05:33 drh Exp $
167c478bd9Sstevel@tonic-gate */
177c478bd9Sstevel@tonic-gate #include "sqliteInt.h"
187c478bd9Sstevel@tonic-gate 
197c478bd9Sstevel@tonic-gate /*
207c478bd9Sstevel@tonic-gate ** This routine is call to handle SQL of the following forms:
217c478bd9Sstevel@tonic-gate **
227c478bd9Sstevel@tonic-gate **    insert into TABLE (IDLIST) values(EXPRLIST)
237c478bd9Sstevel@tonic-gate **    insert into TABLE (IDLIST) select
247c478bd9Sstevel@tonic-gate **
257c478bd9Sstevel@tonic-gate ** The IDLIST following the table name is always optional.  If omitted,
267c478bd9Sstevel@tonic-gate ** then a list of all columns for the table is substituted.  The IDLIST
277c478bd9Sstevel@tonic-gate ** appears in the pColumn parameter.  pColumn is NULL if IDLIST is omitted.
287c478bd9Sstevel@tonic-gate **
297c478bd9Sstevel@tonic-gate ** The pList parameter holds EXPRLIST in the first form of the INSERT
307c478bd9Sstevel@tonic-gate ** statement above, and pSelect is NULL.  For the second form, pList is
317c478bd9Sstevel@tonic-gate ** NULL and pSelect is a pointer to the select statement used to generate
327c478bd9Sstevel@tonic-gate ** data for the insert.
337c478bd9Sstevel@tonic-gate **
347c478bd9Sstevel@tonic-gate ** The code generated follows one of three templates.  For a simple
357c478bd9Sstevel@tonic-gate ** select with data coming from a VALUES clause, the code executes
367c478bd9Sstevel@tonic-gate ** once straight down through.  The template looks like this:
377c478bd9Sstevel@tonic-gate **
387c478bd9Sstevel@tonic-gate **         open write cursor to <table> and its indices
397c478bd9Sstevel@tonic-gate **         puts VALUES clause expressions onto the stack
407c478bd9Sstevel@tonic-gate **         write the resulting record into <table>
417c478bd9Sstevel@tonic-gate **         cleanup
427c478bd9Sstevel@tonic-gate **
437c478bd9Sstevel@tonic-gate ** If the statement is of the form
447c478bd9Sstevel@tonic-gate **
457c478bd9Sstevel@tonic-gate **   INSERT INTO <table> SELECT ...
467c478bd9Sstevel@tonic-gate **
477c478bd9Sstevel@tonic-gate ** And the SELECT clause does not read from <table> at any time, then
487c478bd9Sstevel@tonic-gate ** the generated code follows this template:
497c478bd9Sstevel@tonic-gate **
507c478bd9Sstevel@tonic-gate **         goto B
517c478bd9Sstevel@tonic-gate **      A: setup for the SELECT
527c478bd9Sstevel@tonic-gate **         loop over the tables in the SELECT
537c478bd9Sstevel@tonic-gate **           gosub C
547c478bd9Sstevel@tonic-gate **         end loop
557c478bd9Sstevel@tonic-gate **         cleanup after the SELECT
567c478bd9Sstevel@tonic-gate **         goto D
577c478bd9Sstevel@tonic-gate **      B: open write cursor to <table> and its indices
587c478bd9Sstevel@tonic-gate **         goto A
597c478bd9Sstevel@tonic-gate **      C: insert the select result into <table>
607c478bd9Sstevel@tonic-gate **         return
617c478bd9Sstevel@tonic-gate **      D: cleanup
627c478bd9Sstevel@tonic-gate **
637c478bd9Sstevel@tonic-gate ** The third template is used if the insert statement takes its
647c478bd9Sstevel@tonic-gate ** values from a SELECT but the data is being inserted into a table
657c478bd9Sstevel@tonic-gate ** that is also read as part of the SELECT.  In the third form,
667c478bd9Sstevel@tonic-gate ** we have to use a intermediate table to store the results of
677c478bd9Sstevel@tonic-gate ** the select.  The template is like this:
687c478bd9Sstevel@tonic-gate **
697c478bd9Sstevel@tonic-gate **         goto B
707c478bd9Sstevel@tonic-gate **      A: setup for the SELECT
717c478bd9Sstevel@tonic-gate **         loop over the tables in the SELECT
727c478bd9Sstevel@tonic-gate **           gosub C
737c478bd9Sstevel@tonic-gate **         end loop
747c478bd9Sstevel@tonic-gate **         cleanup after the SELECT
757c478bd9Sstevel@tonic-gate **         goto D
767c478bd9Sstevel@tonic-gate **      C: insert the select result into the intermediate table
777c478bd9Sstevel@tonic-gate **         return
787c478bd9Sstevel@tonic-gate **      B: open a cursor to an intermediate table
797c478bd9Sstevel@tonic-gate **         goto A
807c478bd9Sstevel@tonic-gate **      D: open write cursor to <table> and its indices
817c478bd9Sstevel@tonic-gate **         loop over the intermediate table
827c478bd9Sstevel@tonic-gate **           transfer values form intermediate table into <table>
837c478bd9Sstevel@tonic-gate **         end the loop
847c478bd9Sstevel@tonic-gate **         cleanup
857c478bd9Sstevel@tonic-gate */
sqliteInsert(Parse * pParse,SrcList * pTabList,ExprList * pList,Select * pSelect,IdList * pColumn,int onError)867c478bd9Sstevel@tonic-gate void sqliteInsert(
877c478bd9Sstevel@tonic-gate   Parse *pParse,        /* Parser context */
887c478bd9Sstevel@tonic-gate   SrcList *pTabList,    /* Name of table into which we are inserting */
897c478bd9Sstevel@tonic-gate   ExprList *pList,      /* List of values to be inserted */
907c478bd9Sstevel@tonic-gate   Select *pSelect,      /* A SELECT statement to use as the data source */
917c478bd9Sstevel@tonic-gate   IdList *pColumn,      /* Column names corresponding to IDLIST. */
927c478bd9Sstevel@tonic-gate   int onError           /* How to handle constraint errors */
937c478bd9Sstevel@tonic-gate ){
947c478bd9Sstevel@tonic-gate   Table *pTab;          /* The table to insert into */
957c478bd9Sstevel@tonic-gate   char *zTab;           /* Name of the table into which we are inserting */
967c478bd9Sstevel@tonic-gate   const char *zDb;      /* Name of the database holding this table */
977c478bd9Sstevel@tonic-gate   int i, j, idx;        /* Loop counters */
987c478bd9Sstevel@tonic-gate   Vdbe *v;              /* Generate code into this virtual machine */
997c478bd9Sstevel@tonic-gate   Index *pIdx;          /* For looping over indices of the table */
1007c478bd9Sstevel@tonic-gate   int nColumn;          /* Number of columns in the data */
1017c478bd9Sstevel@tonic-gate   int base;             /* VDBE Cursor number for pTab */
1027c478bd9Sstevel@tonic-gate   int iCont, iBreak;    /* Beginning and end of the loop over srcTab */
1037c478bd9Sstevel@tonic-gate   sqlite *db;           /* The main database structure */
1047c478bd9Sstevel@tonic-gate   int keyColumn = -1;   /* Column that is the INTEGER PRIMARY KEY */
1057c478bd9Sstevel@tonic-gate   int endOfLoop;        /* Label for the end of the insertion loop */
1067c478bd9Sstevel@tonic-gate   int useTempTable;     /* Store SELECT results in intermediate table */
1077c478bd9Sstevel@tonic-gate   int srcTab;           /* Data comes from this temporary cursor if >=0 */
1087c478bd9Sstevel@tonic-gate   int iSelectLoop;      /* Address of code that implements the SELECT */
1097c478bd9Sstevel@tonic-gate   int iCleanup;         /* Address of the cleanup code */
1107c478bd9Sstevel@tonic-gate   int iInsertBlock;     /* Address of the subroutine used to insert data */
1117c478bd9Sstevel@tonic-gate   int iCntMem;          /* Memory cell used for the row counter */
1127c478bd9Sstevel@tonic-gate   int isView;           /* True if attempting to insert into a view */
1137c478bd9Sstevel@tonic-gate 
1147c478bd9Sstevel@tonic-gate   int row_triggers_exist = 0; /* True if there are FOR EACH ROW triggers */
1157c478bd9Sstevel@tonic-gate   int before_triggers;        /* True if there are BEFORE triggers */
1167c478bd9Sstevel@tonic-gate   int after_triggers;         /* True if there are AFTER triggers */
1177c478bd9Sstevel@tonic-gate   int newIdx = -1;            /* Cursor for the NEW table */
1187c478bd9Sstevel@tonic-gate 
1197c478bd9Sstevel@tonic-gate   if( pParse->nErr || sqlite_malloc_failed ) goto insert_cleanup;
1207c478bd9Sstevel@tonic-gate   db = pParse->db;
1217c478bd9Sstevel@tonic-gate 
1227c478bd9Sstevel@tonic-gate   /* Locate the table into which we will be inserting new information.
1237c478bd9Sstevel@tonic-gate   */
1247c478bd9Sstevel@tonic-gate   assert( pTabList->nSrc==1 );
1257c478bd9Sstevel@tonic-gate   zTab = pTabList->a[0].zName;
1267c478bd9Sstevel@tonic-gate   if( zTab==0 ) goto insert_cleanup;
1277c478bd9Sstevel@tonic-gate   pTab = sqliteSrcListLookup(pParse, pTabList);
1287c478bd9Sstevel@tonic-gate   if( pTab==0 ){
1297c478bd9Sstevel@tonic-gate     goto insert_cleanup;
1307c478bd9Sstevel@tonic-gate   }
1317c478bd9Sstevel@tonic-gate   assert( pTab->iDb<db->nDb );
1327c478bd9Sstevel@tonic-gate   zDb = db->aDb[pTab->iDb].zName;
1337c478bd9Sstevel@tonic-gate   if( sqliteAuthCheck(pParse, SQLITE_INSERT, pTab->zName, 0, zDb) ){
1347c478bd9Sstevel@tonic-gate     goto insert_cleanup;
1357c478bd9Sstevel@tonic-gate   }
1367c478bd9Sstevel@tonic-gate 
1377c478bd9Sstevel@tonic-gate   /* Ensure that:
138*55fea89dSDan Cross   *  (a) the table is not read-only,
1397c478bd9Sstevel@tonic-gate   *  (b) that if it is a view then ON INSERT triggers exist
1407c478bd9Sstevel@tonic-gate   */
141*55fea89dSDan Cross   before_triggers = sqliteTriggersExist(pParse, pTab->pTrigger, TK_INSERT,
1427c478bd9Sstevel@tonic-gate                                        TK_BEFORE, TK_ROW, 0);
1437c478bd9Sstevel@tonic-gate   after_triggers = sqliteTriggersExist(pParse, pTab->pTrigger, TK_INSERT,
1447c478bd9Sstevel@tonic-gate                                        TK_AFTER, TK_ROW, 0);
1457c478bd9Sstevel@tonic-gate   row_triggers_exist = before_triggers || after_triggers;
1467c478bd9Sstevel@tonic-gate   isView = pTab->pSelect!=0;
1477c478bd9Sstevel@tonic-gate   if( sqliteIsReadOnly(pParse, pTab, before_triggers) ){
1487c478bd9Sstevel@tonic-gate     goto insert_cleanup;
1497c478bd9Sstevel@tonic-gate   }
1507c478bd9Sstevel@tonic-gate   if( pTab==0 ) goto insert_cleanup;
1517c478bd9Sstevel@tonic-gate 
1527c478bd9Sstevel@tonic-gate   /* If pTab is really a view, make sure it has been initialized.
1537c478bd9Sstevel@tonic-gate   */
1547c478bd9Sstevel@tonic-gate   if( isView && sqliteViewGetColumnNames(pParse, pTab) ){
1557c478bd9Sstevel@tonic-gate     goto insert_cleanup;
1567c478bd9Sstevel@tonic-gate   }
1577c478bd9Sstevel@tonic-gate 
1587c478bd9Sstevel@tonic-gate   /* Allocate a VDBE
1597c478bd9Sstevel@tonic-gate   */
1607c478bd9Sstevel@tonic-gate   v = sqliteGetVdbe(pParse);
1617c478bd9Sstevel@tonic-gate   if( v==0 ) goto insert_cleanup;
1627c478bd9Sstevel@tonic-gate   sqliteBeginWriteOperation(pParse, pSelect || row_triggers_exist, pTab->iDb);
1637c478bd9Sstevel@tonic-gate 
1647c478bd9Sstevel@tonic-gate   /* if there are row triggers, allocate a temp table for new.* references. */
1657c478bd9Sstevel@tonic-gate   if( row_triggers_exist ){
1667c478bd9Sstevel@tonic-gate     newIdx = pParse->nTab++;
1677c478bd9Sstevel@tonic-gate   }
1687c478bd9Sstevel@tonic-gate 
1697c478bd9Sstevel@tonic-gate   /* Figure out how many columns of data are supplied.  If the data
1707c478bd9Sstevel@tonic-gate   ** is coming from a SELECT statement, then this step also generates
1717c478bd9Sstevel@tonic-gate   ** all the code to implement the SELECT statement and invoke a subroutine
1727c478bd9Sstevel@tonic-gate   ** to process each row of the result. (Template 2.) If the SELECT
1737c478bd9Sstevel@tonic-gate   ** statement uses the the table that is being inserted into, then the
1747c478bd9Sstevel@tonic-gate   ** subroutine is also coded here.  That subroutine stores the SELECT
1757c478bd9Sstevel@tonic-gate   ** results in a temporary table. (Template 3.)
1767c478bd9Sstevel@tonic-gate   */
1777c478bd9Sstevel@tonic-gate   if( pSelect ){
1787c478bd9Sstevel@tonic-gate     /* Data is coming from a SELECT.  Generate code to implement that SELECT
1797c478bd9Sstevel@tonic-gate     */
1807c478bd9Sstevel@tonic-gate     int rc, iInitCode;
1817c478bd9Sstevel@tonic-gate     iInitCode = sqliteVdbeAddOp(v, OP_Goto, 0, 0);
1827c478bd9Sstevel@tonic-gate     iSelectLoop = sqliteVdbeCurrentAddr(v);
1837c478bd9Sstevel@tonic-gate     iInsertBlock = sqliteVdbeMakeLabel(v);
1847c478bd9Sstevel@tonic-gate     rc = sqliteSelect(pParse, pSelect, SRT_Subroutine, iInsertBlock, 0,0,0);
1857c478bd9Sstevel@tonic-gate     if( rc || pParse->nErr || sqlite_malloc_failed ) goto insert_cleanup;
1867c478bd9Sstevel@tonic-gate     iCleanup = sqliteVdbeMakeLabel(v);
1877c478bd9Sstevel@tonic-gate     sqliteVdbeAddOp(v, OP_Goto, 0, iCleanup);
1887c478bd9Sstevel@tonic-gate     assert( pSelect->pEList );
1897c478bd9Sstevel@tonic-gate     nColumn = pSelect->pEList->nExpr;
1907c478bd9Sstevel@tonic-gate 
1917c478bd9Sstevel@tonic-gate     /* Set useTempTable to TRUE if the result of the SELECT statement
1927c478bd9Sstevel@tonic-gate     ** should be written into a temporary table.  Set to FALSE if each
1937c478bd9Sstevel@tonic-gate     ** row of the SELECT can be written directly into the result table.
1947c478bd9Sstevel@tonic-gate     **
1957c478bd9Sstevel@tonic-gate     ** A temp table must be used if the table being updated is also one
196*55fea89dSDan Cross     ** of the tables being read by the SELECT statement.  Also use a
1977c478bd9Sstevel@tonic-gate     ** temp table in the case of row triggers.
1987c478bd9Sstevel@tonic-gate     */
1997c478bd9Sstevel@tonic-gate     if( row_triggers_exist ){
2007c478bd9Sstevel@tonic-gate       useTempTable = 1;
2017c478bd9Sstevel@tonic-gate     }else{
2027c478bd9Sstevel@tonic-gate       int addr = sqliteVdbeFindOp(v, OP_OpenRead, pTab->tnum);
2037c478bd9Sstevel@tonic-gate       useTempTable = 0;
2047c478bd9Sstevel@tonic-gate       if( addr>0 ){
2057c478bd9Sstevel@tonic-gate         VdbeOp *pOp = sqliteVdbeGetOp(v, addr-2);
2067c478bd9Sstevel@tonic-gate         if( pOp->opcode==OP_Integer && pOp->p1==pTab->iDb ){
2077c478bd9Sstevel@tonic-gate           useTempTable = 1;
2087c478bd9Sstevel@tonic-gate         }
2097c478bd9Sstevel@tonic-gate       }
2107c478bd9Sstevel@tonic-gate     }
2117c478bd9Sstevel@tonic-gate 
2127c478bd9Sstevel@tonic-gate     if( useTempTable ){
2137c478bd9Sstevel@tonic-gate       /* Generate the subroutine that SELECT calls to process each row of
2147c478bd9Sstevel@tonic-gate       ** the result.  Store the result in a temporary table
2157c478bd9Sstevel@tonic-gate       */
2167c478bd9Sstevel@tonic-gate       srcTab = pParse->nTab++;
2177c478bd9Sstevel@tonic-gate       sqliteVdbeResolveLabel(v, iInsertBlock);
2187c478bd9Sstevel@tonic-gate       sqliteVdbeAddOp(v, OP_MakeRecord, nColumn, 0);
2197c478bd9Sstevel@tonic-gate       sqliteVdbeAddOp(v, OP_NewRecno, srcTab, 0);
2207c478bd9Sstevel@tonic-gate       sqliteVdbeAddOp(v, OP_Pull, 1, 0);
2217c478bd9Sstevel@tonic-gate       sqliteVdbeAddOp(v, OP_PutIntKey, srcTab, 0);
2227c478bd9Sstevel@tonic-gate       sqliteVdbeAddOp(v, OP_Return, 0, 0);
2237c478bd9Sstevel@tonic-gate 
2247c478bd9Sstevel@tonic-gate       /* The following code runs first because the GOTO at the very top
2257c478bd9Sstevel@tonic-gate       ** of the program jumps to it.  Create the temporary table, then jump
2267c478bd9Sstevel@tonic-gate       ** back up and execute the SELECT code above.
2277c478bd9Sstevel@tonic-gate       */
2287c478bd9Sstevel@tonic-gate       sqliteVdbeChangeP2(v, iInitCode, sqliteVdbeCurrentAddr(v));
2297c478bd9Sstevel@tonic-gate       sqliteVdbeAddOp(v, OP_OpenTemp, srcTab, 0);
2307c478bd9Sstevel@tonic-gate       sqliteVdbeAddOp(v, OP_Goto, 0, iSelectLoop);
2317c478bd9Sstevel@tonic-gate       sqliteVdbeResolveLabel(v, iCleanup);
2327c478bd9Sstevel@tonic-gate     }else{
2337c478bd9Sstevel@tonic-gate       sqliteVdbeChangeP2(v, iInitCode, sqliteVdbeCurrentAddr(v));
2347c478bd9Sstevel@tonic-gate     }
2357c478bd9Sstevel@tonic-gate   }else{
2367c478bd9Sstevel@tonic-gate     /* This is the case if the data for the INSERT is coming from a VALUES
2377c478bd9Sstevel@tonic-gate     ** clause
2387c478bd9Sstevel@tonic-gate     */
2397c478bd9Sstevel@tonic-gate     SrcList dummy;
2407c478bd9Sstevel@tonic-gate     assert( pList!=0 );
2417c478bd9Sstevel@tonic-gate     srcTab = -1;
2427c478bd9Sstevel@tonic-gate     useTempTable = 0;
2437c478bd9Sstevel@tonic-gate     assert( pList );
2447c478bd9Sstevel@tonic-gate     nColumn = pList->nExpr;
2457c478bd9Sstevel@tonic-gate     dummy.nSrc = 0;
2467c478bd9Sstevel@tonic-gate     for(i=0; i<nColumn; i++){
2477c478bd9Sstevel@tonic-gate       if( sqliteExprResolveIds(pParse, &dummy, 0, pList->a[i].pExpr) ){
2487c478bd9Sstevel@tonic-gate         goto insert_cleanup;
2497c478bd9Sstevel@tonic-gate       }
2507c478bd9Sstevel@tonic-gate       if( sqliteExprCheck(pParse, pList->a[i].pExpr, 0, 0) ){
2517c478bd9Sstevel@tonic-gate         goto insert_cleanup;
2527c478bd9Sstevel@tonic-gate       }
2537c478bd9Sstevel@tonic-gate     }
2547c478bd9Sstevel@tonic-gate   }
2557c478bd9Sstevel@tonic-gate 
2567c478bd9Sstevel@tonic-gate   /* Make sure the number of columns in the source data matches the number
2577c478bd9Sstevel@tonic-gate   ** of columns to be inserted into the table.
2587c478bd9Sstevel@tonic-gate   */
2597c478bd9Sstevel@tonic-gate   if( pColumn==0 && nColumn!=pTab->nCol ){
260*55fea89dSDan Cross     sqliteErrorMsg(pParse,
2617c478bd9Sstevel@tonic-gate        "table %S has %d columns but %d values were supplied",
2627c478bd9Sstevel@tonic-gate        pTabList, 0, pTab->nCol, nColumn);
2637c478bd9Sstevel@tonic-gate     goto insert_cleanup;
2647c478bd9Sstevel@tonic-gate   }
2657c478bd9Sstevel@tonic-gate   if( pColumn!=0 && nColumn!=pColumn->nId ){
2667c478bd9Sstevel@tonic-gate     sqliteErrorMsg(pParse, "%d values for %d columns", nColumn, pColumn->nId);
2677c478bd9Sstevel@tonic-gate     goto insert_cleanup;
2687c478bd9Sstevel@tonic-gate   }
2697c478bd9Sstevel@tonic-gate 
2707c478bd9Sstevel@tonic-gate   /* If the INSERT statement included an IDLIST term, then make sure
271*55fea89dSDan Cross   ** all elements of the IDLIST really are columns of the table and
2727c478bd9Sstevel@tonic-gate   ** remember the column indices.
2737c478bd9Sstevel@tonic-gate   **
2747c478bd9Sstevel@tonic-gate   ** If the table has an INTEGER PRIMARY KEY column and that column
2757c478bd9Sstevel@tonic-gate   ** is named in the IDLIST, then record in the keyColumn variable
2767c478bd9Sstevel@tonic-gate   ** the index into IDLIST of the primary key column.  keyColumn is
2777c478bd9Sstevel@tonic-gate   ** the index of the primary key as it appears in IDLIST, not as
2787c478bd9Sstevel@tonic-gate   ** is appears in the original table.  (The index of the primary
2797c478bd9Sstevel@tonic-gate   ** key in the original table is pTab->iPKey.)
2807c478bd9Sstevel@tonic-gate   */
2817c478bd9Sstevel@tonic-gate   if( pColumn ){
2827c478bd9Sstevel@tonic-gate     for(i=0; i<pColumn->nId; i++){
2837c478bd9Sstevel@tonic-gate       pColumn->a[i].idx = -1;
2847c478bd9Sstevel@tonic-gate     }
2857c478bd9Sstevel@tonic-gate     for(i=0; i<pColumn->nId; i++){
2867c478bd9Sstevel@tonic-gate       for(j=0; j<pTab->nCol; j++){
2877c478bd9Sstevel@tonic-gate         if( sqliteStrICmp(pColumn->a[i].zName, pTab->aCol[j].zName)==0 ){
2887c478bd9Sstevel@tonic-gate           pColumn->a[i].idx = j;
2897c478bd9Sstevel@tonic-gate           if( j==pTab->iPKey ){
2907c478bd9Sstevel@tonic-gate             keyColumn = i;
2917c478bd9Sstevel@tonic-gate           }
2927c478bd9Sstevel@tonic-gate           break;
2937c478bd9Sstevel@tonic-gate         }
2947c478bd9Sstevel@tonic-gate       }
2957c478bd9Sstevel@tonic-gate       if( j>=pTab->nCol ){
2967c478bd9Sstevel@tonic-gate         if( sqliteIsRowid(pColumn->a[i].zName) ){
2977c478bd9Sstevel@tonic-gate           keyColumn = i;
2987c478bd9Sstevel@tonic-gate         }else{
2997c478bd9Sstevel@tonic-gate           sqliteErrorMsg(pParse, "table %S has no column named %s",
3007c478bd9Sstevel@tonic-gate               pTabList, 0, pColumn->a[i].zName);
3017c478bd9Sstevel@tonic-gate           pParse->nErr++;
3027c478bd9Sstevel@tonic-gate           goto insert_cleanup;
3037c478bd9Sstevel@tonic-gate         }
3047c478bd9Sstevel@tonic-gate       }
3057c478bd9Sstevel@tonic-gate     }
3067c478bd9Sstevel@tonic-gate   }
3077c478bd9Sstevel@tonic-gate 
3087c478bd9Sstevel@tonic-gate   /* If there is no IDLIST term but the table has an integer primary
3097c478bd9Sstevel@tonic-gate   ** key, the set the keyColumn variable to the primary key column index
3107c478bd9Sstevel@tonic-gate   ** in the original table definition.
3117c478bd9Sstevel@tonic-gate   */
3127c478bd9Sstevel@tonic-gate   if( pColumn==0 ){
3137c478bd9Sstevel@tonic-gate     keyColumn = pTab->iPKey;
3147c478bd9Sstevel@tonic-gate   }
3157c478bd9Sstevel@tonic-gate 
3167c478bd9Sstevel@tonic-gate   /* Open the temp table for FOR EACH ROW triggers
3177c478bd9Sstevel@tonic-gate   */
3187c478bd9Sstevel@tonic-gate   if( row_triggers_exist ){
3197c478bd9Sstevel@tonic-gate     sqliteVdbeAddOp(v, OP_OpenPseudo, newIdx, 0);
3207c478bd9Sstevel@tonic-gate   }
321*55fea89dSDan Cross 
3227c478bd9Sstevel@tonic-gate   /* Initialize the count of rows to be inserted
3237c478bd9Sstevel@tonic-gate   */
3247c478bd9Sstevel@tonic-gate   if( db->flags & SQLITE_CountRows ){
3257c478bd9Sstevel@tonic-gate     iCntMem = pParse->nMem++;
3267c478bd9Sstevel@tonic-gate     sqliteVdbeAddOp(v, OP_Integer, 0, 0);
3277c478bd9Sstevel@tonic-gate     sqliteVdbeAddOp(v, OP_MemStore, iCntMem, 1);
3287c478bd9Sstevel@tonic-gate   }
3297c478bd9Sstevel@tonic-gate 
3307c478bd9Sstevel@tonic-gate   /* Open tables and indices if there are no row triggers */
3317c478bd9Sstevel@tonic-gate   if( !row_triggers_exist ){
3327c478bd9Sstevel@tonic-gate     base = pParse->nTab;
3337c478bd9Sstevel@tonic-gate     idx = sqliteOpenTableAndIndices(pParse, pTab, base);
3347c478bd9Sstevel@tonic-gate     pParse->nTab += idx;
3357c478bd9Sstevel@tonic-gate   }
3367c478bd9Sstevel@tonic-gate 
3377c478bd9Sstevel@tonic-gate   /* If the data source is a temporary table, then we have to create
3387c478bd9Sstevel@tonic-gate   ** a loop because there might be multiple rows of data.  If the data
3397c478bd9Sstevel@tonic-gate   ** source is a subroutine call from the SELECT statement, then we need
3407c478bd9Sstevel@tonic-gate   ** to launch the SELECT statement processing.
3417c478bd9Sstevel@tonic-gate   */
3427c478bd9Sstevel@tonic-gate   if( useTempTable ){
3437c478bd9Sstevel@tonic-gate     iBreak = sqliteVdbeMakeLabel(v);
3447c478bd9Sstevel@tonic-gate     sqliteVdbeAddOp(v, OP_Rewind, srcTab, iBreak);
3457c478bd9Sstevel@tonic-gate     iCont = sqliteVdbeCurrentAddr(v);
3467c478bd9Sstevel@tonic-gate   }else if( pSelect ){
3477c478bd9Sstevel@tonic-gate     sqliteVdbeAddOp(v, OP_Goto, 0, iSelectLoop);
3487c478bd9Sstevel@tonic-gate     sqliteVdbeResolveLabel(v, iInsertBlock);
3497c478bd9Sstevel@tonic-gate   }
3507c478bd9Sstevel@tonic-gate 
3517c478bd9Sstevel@tonic-gate   /* Run the BEFORE and INSTEAD OF triggers, if there are any
3527c478bd9Sstevel@tonic-gate   */
3537c478bd9Sstevel@tonic-gate   endOfLoop = sqliteVdbeMakeLabel(v);
3547c478bd9Sstevel@tonic-gate   if( before_triggers ){
3557c478bd9Sstevel@tonic-gate 
3567c478bd9Sstevel@tonic-gate     /* build the NEW.* reference row.  Note that if there is an INTEGER
3577c478bd9Sstevel@tonic-gate     ** PRIMARY KEY into which a NULL is being inserted, that NULL will be
3587c478bd9Sstevel@tonic-gate     ** translated into a unique ID for the row.  But on a BEFORE trigger,
3597c478bd9Sstevel@tonic-gate     ** we do not know what the unique ID will be (because the insert has
3607c478bd9Sstevel@tonic-gate     ** not happened yet) so we substitute a rowid of -1
3617c478bd9Sstevel@tonic-gate     */
3627c478bd9Sstevel@tonic-gate     if( keyColumn<0 ){
3637c478bd9Sstevel@tonic-gate       sqliteVdbeAddOp(v, OP_Integer, -1, 0);
3647c478bd9Sstevel@tonic-gate     }else if( useTempTable ){
3657c478bd9Sstevel@tonic-gate       sqliteVdbeAddOp(v, OP_Column, srcTab, keyColumn);
3667c478bd9Sstevel@tonic-gate     }else if( pSelect ){
3677c478bd9Sstevel@tonic-gate       sqliteVdbeAddOp(v, OP_Dup, nColumn - keyColumn - 1, 1);
3687c478bd9Sstevel@tonic-gate     }else{
3697c478bd9Sstevel@tonic-gate       sqliteExprCode(pParse, pList->a[keyColumn].pExpr);
3707c478bd9Sstevel@tonic-gate       sqliteVdbeAddOp(v, OP_NotNull, -1, sqliteVdbeCurrentAddr(v)+3);
3717c478bd9Sstevel@tonic-gate       sqliteVdbeAddOp(v, OP_Pop, 1, 0);
3727c478bd9Sstevel@tonic-gate       sqliteVdbeAddOp(v, OP_Integer, -1, 0);
3737c478bd9Sstevel@tonic-gate       sqliteVdbeAddOp(v, OP_MustBeInt, 0, 0);
3747c478bd9Sstevel@tonic-gate     }
3757c478bd9Sstevel@tonic-gate 
3767c478bd9Sstevel@tonic-gate     /* Create the new column data
3777c478bd9Sstevel@tonic-gate     */
3787c478bd9Sstevel@tonic-gate     for(i=0; i<pTab->nCol; i++){
3797c478bd9Sstevel@tonic-gate       if( pColumn==0 ){
3807c478bd9Sstevel@tonic-gate         j = i;
3817c478bd9Sstevel@tonic-gate       }else{
3827c478bd9Sstevel@tonic-gate         for(j=0; j<pColumn->nId; j++){
3837c478bd9Sstevel@tonic-gate           if( pColumn->a[j].idx==i ) break;
3847c478bd9Sstevel@tonic-gate         }
3857c478bd9Sstevel@tonic-gate       }
3867c478bd9Sstevel@tonic-gate       if( pColumn && j>=pColumn->nId ){
3877c478bd9Sstevel@tonic-gate         sqliteVdbeOp3(v, OP_String, 0, 0, pTab->aCol[i].zDflt, P3_STATIC);
3887c478bd9Sstevel@tonic-gate       }else if( useTempTable ){
389*55fea89dSDan Cross         sqliteVdbeAddOp(v, OP_Column, srcTab, j);
3907c478bd9Sstevel@tonic-gate       }else if( pSelect ){
3917c478bd9Sstevel@tonic-gate         sqliteVdbeAddOp(v, OP_Dup, nColumn-j-1, 1);
3927c478bd9Sstevel@tonic-gate       }else{
3937c478bd9Sstevel@tonic-gate         sqliteExprCode(pParse, pList->a[j].pExpr);
3947c478bd9Sstevel@tonic-gate       }
3957c478bd9Sstevel@tonic-gate     }
3967c478bd9Sstevel@tonic-gate     sqliteVdbeAddOp(v, OP_MakeRecord, pTab->nCol, 0);
3977c478bd9Sstevel@tonic-gate     sqliteVdbeAddOp(v, OP_PutIntKey, newIdx, 0);
3987c478bd9Sstevel@tonic-gate 
3997c478bd9Sstevel@tonic-gate     /* Fire BEFORE or INSTEAD OF triggers */
400*55fea89dSDan Cross     if( sqliteCodeRowTrigger(pParse, TK_INSERT, 0, TK_BEFORE, pTab,
4017c478bd9Sstevel@tonic-gate         newIdx, -1, onError, endOfLoop) ){
4027c478bd9Sstevel@tonic-gate       goto insert_cleanup;
4037c478bd9Sstevel@tonic-gate     }
4047c478bd9Sstevel@tonic-gate   }
4057c478bd9Sstevel@tonic-gate 
4067c478bd9Sstevel@tonic-gate   /* If any triggers exists, the opening of tables and indices is deferred
4077c478bd9Sstevel@tonic-gate   ** until now.
4087c478bd9Sstevel@tonic-gate   */
4097c478bd9Sstevel@tonic-gate   if( row_triggers_exist && !isView ){
4107c478bd9Sstevel@tonic-gate     base = pParse->nTab;
4117c478bd9Sstevel@tonic-gate     idx = sqliteOpenTableAndIndices(pParse, pTab, base);
4127c478bd9Sstevel@tonic-gate     pParse->nTab += idx;
4137c478bd9Sstevel@tonic-gate   }
4147c478bd9Sstevel@tonic-gate 
4157c478bd9Sstevel@tonic-gate   /* Push the record number for the new entry onto the stack.  The
4167c478bd9Sstevel@tonic-gate   ** record number is a randomly generate integer created by NewRecno
4177c478bd9Sstevel@tonic-gate   ** except when the table has an INTEGER PRIMARY KEY column, in which
418*55fea89dSDan Cross   ** case the record number is the same as that column.
4197c478bd9Sstevel@tonic-gate   */
4207c478bd9Sstevel@tonic-gate   if( !isView ){
4217c478bd9Sstevel@tonic-gate     if( keyColumn>=0 ){
4227c478bd9Sstevel@tonic-gate       if( useTempTable ){
4237c478bd9Sstevel@tonic-gate         sqliteVdbeAddOp(v, OP_Column, srcTab, keyColumn);
4247c478bd9Sstevel@tonic-gate       }else if( pSelect ){
4257c478bd9Sstevel@tonic-gate         sqliteVdbeAddOp(v, OP_Dup, nColumn - keyColumn - 1, 1);
4267c478bd9Sstevel@tonic-gate       }else{
4277c478bd9Sstevel@tonic-gate         sqliteExprCode(pParse, pList->a[keyColumn].pExpr);
4287c478bd9Sstevel@tonic-gate       }
4297c478bd9Sstevel@tonic-gate       /* If the PRIMARY KEY expression is NULL, then use OP_NewRecno
4307c478bd9Sstevel@tonic-gate       ** to generate a unique primary key value.
4317c478bd9Sstevel@tonic-gate       */
4327c478bd9Sstevel@tonic-gate       sqliteVdbeAddOp(v, OP_NotNull, -1, sqliteVdbeCurrentAddr(v)+3);
4337c478bd9Sstevel@tonic-gate       sqliteVdbeAddOp(v, OP_Pop, 1, 0);
4347c478bd9Sstevel@tonic-gate       sqliteVdbeAddOp(v, OP_NewRecno, base, 0);
4357c478bd9Sstevel@tonic-gate       sqliteVdbeAddOp(v, OP_MustBeInt, 0, 0);
4367c478bd9Sstevel@tonic-gate     }else{
4377c478bd9Sstevel@tonic-gate       sqliteVdbeAddOp(v, OP_NewRecno, base, 0);
4387c478bd9Sstevel@tonic-gate     }
4397c478bd9Sstevel@tonic-gate 
4407c478bd9Sstevel@tonic-gate     /* Push onto the stack, data for all columns of the new entry, beginning
4417c478bd9Sstevel@tonic-gate     ** with the first column.
4427c478bd9Sstevel@tonic-gate     */
4437c478bd9Sstevel@tonic-gate     for(i=0; i<pTab->nCol; i++){
4447c478bd9Sstevel@tonic-gate       if( i==pTab->iPKey ){
4457c478bd9Sstevel@tonic-gate         /* The value of the INTEGER PRIMARY KEY column is always a NULL.
4467c478bd9Sstevel@tonic-gate         ** Whenever this column is read, the record number will be substituted
4477c478bd9Sstevel@tonic-gate         ** in its place.  So will fill this column with a NULL to avoid
4487c478bd9Sstevel@tonic-gate         ** taking up data space with information that will never be used. */
4497c478bd9Sstevel@tonic-gate         sqliteVdbeAddOp(v, OP_String, 0, 0);
4507c478bd9Sstevel@tonic-gate         continue;
4517c478bd9Sstevel@tonic-gate       }
4527c478bd9Sstevel@tonic-gate       if( pColumn==0 ){
4537c478bd9Sstevel@tonic-gate         j = i;
4547c478bd9Sstevel@tonic-gate       }else{
4557c478bd9Sstevel@tonic-gate         for(j=0; j<pColumn->nId; j++){
4567c478bd9Sstevel@tonic-gate           if( pColumn->a[j].idx==i ) break;
4577c478bd9Sstevel@tonic-gate         }
4587c478bd9Sstevel@tonic-gate       }
4597c478bd9Sstevel@tonic-gate       if( pColumn && j>=pColumn->nId ){
4607c478bd9Sstevel@tonic-gate         sqliteVdbeOp3(v, OP_String, 0, 0, pTab->aCol[i].zDflt, P3_STATIC);
4617c478bd9Sstevel@tonic-gate       }else if( useTempTable ){
462*55fea89dSDan Cross         sqliteVdbeAddOp(v, OP_Column, srcTab, j);
4637c478bd9Sstevel@tonic-gate       }else if( pSelect ){
4647c478bd9Sstevel@tonic-gate         sqliteVdbeAddOp(v, OP_Dup, i+nColumn-j, 1);
4657c478bd9Sstevel@tonic-gate       }else{
4667c478bd9Sstevel@tonic-gate         sqliteExprCode(pParse, pList->a[j].pExpr);
4677c478bd9Sstevel@tonic-gate       }
4687c478bd9Sstevel@tonic-gate     }
4697c478bd9Sstevel@tonic-gate 
4707c478bd9Sstevel@tonic-gate     /* Generate code to check constraints and generate index keys and
4717c478bd9Sstevel@tonic-gate     ** do the insertion.
4727c478bd9Sstevel@tonic-gate     */
4737c478bd9Sstevel@tonic-gate     sqliteGenerateConstraintChecks(pParse, pTab, base, 0, keyColumn>=0,
4747c478bd9Sstevel@tonic-gate                                    0, onError, endOfLoop);
4757c478bd9Sstevel@tonic-gate     sqliteCompleteInsertion(pParse, pTab, base, 0,0,0,
4767c478bd9Sstevel@tonic-gate                             after_triggers ? newIdx : -1);
4777c478bd9Sstevel@tonic-gate   }
4787c478bd9Sstevel@tonic-gate 
4797c478bd9Sstevel@tonic-gate   /* Update the count of rows that are inserted
4807c478bd9Sstevel@tonic-gate   */
4817c478bd9Sstevel@tonic-gate   if( (db->flags & SQLITE_CountRows)!=0 ){
4827c478bd9Sstevel@tonic-gate     sqliteVdbeAddOp(v, OP_MemIncr, iCntMem, 0);
4837c478bd9Sstevel@tonic-gate   }
4847c478bd9Sstevel@tonic-gate 
4857c478bd9Sstevel@tonic-gate   if( row_triggers_exist ){
4867c478bd9Sstevel@tonic-gate     /* Close all tables opened */
4877c478bd9Sstevel@tonic-gate     if( !isView ){
4887c478bd9Sstevel@tonic-gate       sqliteVdbeAddOp(v, OP_Close, base, 0);
4897c478bd9Sstevel@tonic-gate       for(idx=1, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, idx++){
4907c478bd9Sstevel@tonic-gate         sqliteVdbeAddOp(v, OP_Close, idx+base, 0);
4917c478bd9Sstevel@tonic-gate       }
4927c478bd9Sstevel@tonic-gate     }
4937c478bd9Sstevel@tonic-gate 
4947c478bd9Sstevel@tonic-gate     /* Code AFTER triggers */
495*55fea89dSDan Cross     if( sqliteCodeRowTrigger(pParse, TK_INSERT, 0, TK_AFTER, pTab, newIdx, -1,
4967c478bd9Sstevel@tonic-gate           onError, endOfLoop) ){
4977c478bd9Sstevel@tonic-gate       goto insert_cleanup;
4987c478bd9Sstevel@tonic-gate     }
4997c478bd9Sstevel@tonic-gate   }
5007c478bd9Sstevel@tonic-gate 
5017c478bd9Sstevel@tonic-gate   /* The bottom of the loop, if the data source is a SELECT statement
5027c478bd9Sstevel@tonic-gate   */
5037c478bd9Sstevel@tonic-gate   sqliteVdbeResolveLabel(v, endOfLoop);
5047c478bd9Sstevel@tonic-gate   if( useTempTable ){
5057c478bd9Sstevel@tonic-gate     sqliteVdbeAddOp(v, OP_Next, srcTab, iCont);
5067c478bd9Sstevel@tonic-gate     sqliteVdbeResolveLabel(v, iBreak);
5077c478bd9Sstevel@tonic-gate     sqliteVdbeAddOp(v, OP_Close, srcTab, 0);
5087c478bd9Sstevel@tonic-gate   }else if( pSelect ){
5097c478bd9Sstevel@tonic-gate     sqliteVdbeAddOp(v, OP_Pop, nColumn, 0);
5107c478bd9Sstevel@tonic-gate     sqliteVdbeAddOp(v, OP_Return, 0, 0);
5117c478bd9Sstevel@tonic-gate     sqliteVdbeResolveLabel(v, iCleanup);
5127c478bd9Sstevel@tonic-gate   }
5137c478bd9Sstevel@tonic-gate 
5147c478bd9Sstevel@tonic-gate   if( !row_triggers_exist ){
5157c478bd9Sstevel@tonic-gate     /* Close all tables opened */
5167c478bd9Sstevel@tonic-gate     sqliteVdbeAddOp(v, OP_Close, base, 0);
5177c478bd9Sstevel@tonic-gate     for(idx=1, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, idx++){
5187c478bd9Sstevel@tonic-gate       sqliteVdbeAddOp(v, OP_Close, idx+base, 0);
5197c478bd9Sstevel@tonic-gate     }
5207c478bd9Sstevel@tonic-gate   }
5217c478bd9Sstevel@tonic-gate 
5227c478bd9Sstevel@tonic-gate   sqliteVdbeAddOp(v, OP_SetCounts, 0, 0);
5237c478bd9Sstevel@tonic-gate   sqliteEndWriteOperation(pParse);
5247c478bd9Sstevel@tonic-gate 
5257c478bd9Sstevel@tonic-gate   /*
5267c478bd9Sstevel@tonic-gate   ** Return the number of rows inserted.
5277c478bd9Sstevel@tonic-gate   */
5287c478bd9Sstevel@tonic-gate   if( db->flags & SQLITE_CountRows ){
5297c478bd9Sstevel@tonic-gate     sqliteVdbeOp3(v, OP_ColumnName, 0, 1, "rows inserted", P3_STATIC);
5307c478bd9Sstevel@tonic-gate     sqliteVdbeAddOp(v, OP_MemLoad, iCntMem, 0);
5317c478bd9Sstevel@tonic-gate     sqliteVdbeAddOp(v, OP_Callback, 1, 0);
5327c478bd9Sstevel@tonic-gate   }
5337c478bd9Sstevel@tonic-gate 
5347c478bd9Sstevel@tonic-gate insert_cleanup:
5357c478bd9Sstevel@tonic-gate   sqliteSrcListDelete(pTabList);
5367c478bd9Sstevel@tonic-gate   if( pList ) sqliteExprListDelete(pList);
5377c478bd9Sstevel@tonic-gate   if( pSelect ) sqliteSelectDelete(pSelect);
5387c478bd9Sstevel@tonic-gate   sqliteIdListDelete(pColumn);
5397c478bd9Sstevel@tonic-gate }
5407c478bd9Sstevel@tonic-gate 
5417c478bd9Sstevel@tonic-gate /*
5427c478bd9Sstevel@tonic-gate ** Generate code to do a constraint check prior to an INSERT or an UPDATE.
5437c478bd9Sstevel@tonic-gate **
5447c478bd9Sstevel@tonic-gate ** When this routine is called, the stack contains (from bottom to top)
5457c478bd9Sstevel@tonic-gate ** the following values:
5467c478bd9Sstevel@tonic-gate **
5477c478bd9Sstevel@tonic-gate **    1.  The recno of the row to be updated before the update.  This
5487c478bd9Sstevel@tonic-gate **        value is omitted unless we are doing an UPDATE that involves a
5497c478bd9Sstevel@tonic-gate **        change to the record number.
5507c478bd9Sstevel@tonic-gate **
5517c478bd9Sstevel@tonic-gate **    2.  The recno of the row after the update.
5527c478bd9Sstevel@tonic-gate **
5537c478bd9Sstevel@tonic-gate **    3.  The data in the first column of the entry after the update.
5547c478bd9Sstevel@tonic-gate **
5557c478bd9Sstevel@tonic-gate **    i.  Data from middle columns...
5567c478bd9Sstevel@tonic-gate **
5577c478bd9Sstevel@tonic-gate **    N.  The data in the last column of the entry after the update.
5587c478bd9Sstevel@tonic-gate **
5597c478bd9Sstevel@tonic-gate ** The old recno shown as entry (1) above is omitted unless both isUpdate
5607c478bd9Sstevel@tonic-gate ** and recnoChng are 1.  isUpdate is true for UPDATEs and false for
5617c478bd9Sstevel@tonic-gate ** INSERTs and recnoChng is true if the record number is being changed.
5627c478bd9Sstevel@tonic-gate **
5637c478bd9Sstevel@tonic-gate ** The code generated by this routine pushes additional entries onto
5647c478bd9Sstevel@tonic-gate ** the stack which are the keys for new index entries for the new record.
5657c478bd9Sstevel@tonic-gate ** The order of index keys is the same as the order of the indices on
566*55fea89dSDan Cross ** the pTable->pIndex list.  A key is only created for index i if
5677c478bd9Sstevel@tonic-gate ** aIdxUsed!=0 and aIdxUsed[i]!=0.
5687c478bd9Sstevel@tonic-gate **
5697c478bd9Sstevel@tonic-gate ** This routine also generates code to check constraints.  NOT NULL,
5707c478bd9Sstevel@tonic-gate ** CHECK, and UNIQUE constraints are all checked.  If a constraint fails,
5717c478bd9Sstevel@tonic-gate ** then the appropriate action is performed.  There are five possible
5727c478bd9Sstevel@tonic-gate ** actions: ROLLBACK, ABORT, FAIL, REPLACE, and IGNORE.
5737c478bd9Sstevel@tonic-gate **
5747c478bd9Sstevel@tonic-gate **  Constraint type  Action       What Happens
5757c478bd9Sstevel@tonic-gate **  ---------------  ----------   ----------------------------------------
5767c478bd9Sstevel@tonic-gate **  any              ROLLBACK     The current transaction is rolled back and
5777c478bd9Sstevel@tonic-gate **                                sqlite_exec() returns immediately with a
5787c478bd9Sstevel@tonic-gate **                                return code of SQLITE_CONSTRAINT.
5797c478bd9Sstevel@tonic-gate **
5807c478bd9Sstevel@tonic-gate **  any              ABORT        Back out changes from the current command
5817c478bd9Sstevel@tonic-gate **                                only (do not do a complete rollback) then
5827c478bd9Sstevel@tonic-gate **                                cause sqlite_exec() to return immediately
5837c478bd9Sstevel@tonic-gate **                                with SQLITE_CONSTRAINT.
5847c478bd9Sstevel@tonic-gate **
5857c478bd9Sstevel@tonic-gate **  any              FAIL         Sqlite_exec() returns immediately with a
5867c478bd9Sstevel@tonic-gate **                                return code of SQLITE_CONSTRAINT.  The
5877c478bd9Sstevel@tonic-gate **                                transaction is not rolled back and any
5887c478bd9Sstevel@tonic-gate **                                prior changes are retained.
5897c478bd9Sstevel@tonic-gate **
5907c478bd9Sstevel@tonic-gate **  any              IGNORE       The record number and data is popped from
5917c478bd9Sstevel@tonic-gate **                                the stack and there is an immediate jump
5927c478bd9Sstevel@tonic-gate **                                to label ignoreDest.
5937c478bd9Sstevel@tonic-gate **
5947c478bd9Sstevel@tonic-gate **  NOT NULL         REPLACE      The NULL value is replace by the default
5957c478bd9Sstevel@tonic-gate **                                value for that column.  If the default value
5967c478bd9Sstevel@tonic-gate **                                is NULL, the action is the same as ABORT.
5977c478bd9Sstevel@tonic-gate **
5987c478bd9Sstevel@tonic-gate **  UNIQUE           REPLACE      The other row that conflicts with the row
5997c478bd9Sstevel@tonic-gate **                                being inserted is removed.
6007c478bd9Sstevel@tonic-gate **
6017c478bd9Sstevel@tonic-gate **  CHECK            REPLACE      Illegal.  The results in an exception.
6027c478bd9Sstevel@tonic-gate **
6037c478bd9Sstevel@tonic-gate ** Which action to take is determined by the overrideError parameter.
6047c478bd9Sstevel@tonic-gate ** Or if overrideError==OE_Default, then the pParse->onError parameter
6057c478bd9Sstevel@tonic-gate ** is used.  Or if pParse->onError==OE_Default then the onError value
6067c478bd9Sstevel@tonic-gate ** for the constraint is used.
6077c478bd9Sstevel@tonic-gate **
6087c478bd9Sstevel@tonic-gate ** The calling routine must open a read/write cursor for pTab with
6097c478bd9Sstevel@tonic-gate ** cursor number "base".  All indices of pTab must also have open
6107c478bd9Sstevel@tonic-gate ** read/write cursors with cursor number base+i for the i-th cursor.
6117c478bd9Sstevel@tonic-gate ** Except, if there is no possibility of a REPLACE action then
6127c478bd9Sstevel@tonic-gate ** cursors do not need to be open for indices where aIdxUsed[i]==0.
6137c478bd9Sstevel@tonic-gate **
6147c478bd9Sstevel@tonic-gate ** If the isUpdate flag is true, it means that the "base" cursor is
6157c478bd9Sstevel@tonic-gate ** initially pointing to an entry that is being updated.  The isUpdate
6167c478bd9Sstevel@tonic-gate ** flag causes extra code to be generated so that the "base" cursor
6177c478bd9Sstevel@tonic-gate ** is still pointing at the same entry after the routine returns.
6187c478bd9Sstevel@tonic-gate ** Without the isUpdate flag, the "base" cursor might be moved.
6197c478bd9Sstevel@tonic-gate */
sqliteGenerateConstraintChecks(Parse * pParse,Table * pTab,int base,char * aIdxUsed,int recnoChng,int isUpdate,int overrideError,int ignoreDest)6207c478bd9Sstevel@tonic-gate void sqliteGenerateConstraintChecks(
6217c478bd9Sstevel@tonic-gate   Parse *pParse,      /* The parser context */
6227c478bd9Sstevel@tonic-gate   Table *pTab,        /* the table into which we are inserting */
6237c478bd9Sstevel@tonic-gate   int base,           /* Index of a read/write cursor pointing at pTab */
6247c478bd9Sstevel@tonic-gate   char *aIdxUsed,     /* Which indices are used.  NULL means all are used */
6257c478bd9Sstevel@tonic-gate   int recnoChng,      /* True if the record number will change */
6267c478bd9Sstevel@tonic-gate   int isUpdate,       /* True for UPDATE, False for INSERT */
6277c478bd9Sstevel@tonic-gate   int overrideError,  /* Override onError to this if not OE_Default */
6287c478bd9Sstevel@tonic-gate   int ignoreDest      /* Jump to this label on an OE_Ignore resolution */
6297c478bd9Sstevel@tonic-gate ){
6307c478bd9Sstevel@tonic-gate   int i;
6317c478bd9Sstevel@tonic-gate   Vdbe *v;
6327c478bd9Sstevel@tonic-gate   int nCol;
6337c478bd9Sstevel@tonic-gate   int onError;
6347c478bd9Sstevel@tonic-gate   int addr;
6357c478bd9Sstevel@tonic-gate   int extra;
6367c478bd9Sstevel@tonic-gate   int iCur;
6377c478bd9Sstevel@tonic-gate   Index *pIdx;
6387c478bd9Sstevel@tonic-gate   int seenReplace = 0;
6397c478bd9Sstevel@tonic-gate   int jumpInst1, jumpInst2;
6407c478bd9Sstevel@tonic-gate   int contAddr;
6417c478bd9Sstevel@tonic-gate   int hasTwoRecnos = (isUpdate && recnoChng);
6427c478bd9Sstevel@tonic-gate 
6437c478bd9Sstevel@tonic-gate   v = sqliteGetVdbe(pParse);
6447c478bd9Sstevel@tonic-gate   assert( v!=0 );
6457c478bd9Sstevel@tonic-gate   assert( pTab->pSelect==0 );  /* This table is not a VIEW */
6467c478bd9Sstevel@tonic-gate   nCol = pTab->nCol;
6477c478bd9Sstevel@tonic-gate 
6487c478bd9Sstevel@tonic-gate   /* Test all NOT NULL constraints.
6497c478bd9Sstevel@tonic-gate   */
6507c478bd9Sstevel@tonic-gate   for(i=0; i<nCol; i++){
6517c478bd9Sstevel@tonic-gate     if( i==pTab->iPKey ){
6527c478bd9Sstevel@tonic-gate       continue;
6537c478bd9Sstevel@tonic-gate     }
6547c478bd9Sstevel@tonic-gate     onError = pTab->aCol[i].notNull;
6557c478bd9Sstevel@tonic-gate     if( onError==OE_None ) continue;
6567c478bd9Sstevel@tonic-gate     if( overrideError!=OE_Default ){
6577c478bd9Sstevel@tonic-gate       onError = overrideError;
6587c478bd9Sstevel@tonic-gate     }else if( pParse->db->onError!=OE_Default ){
6597c478bd9Sstevel@tonic-gate       onError = pParse->db->onError;
6607c478bd9Sstevel@tonic-gate     }else if( onError==OE_Default ){
6617c478bd9Sstevel@tonic-gate       onError = OE_Abort;
6627c478bd9Sstevel@tonic-gate     }
6637c478bd9Sstevel@tonic-gate     if( onError==OE_Replace && pTab->aCol[i].zDflt==0 ){
6647c478bd9Sstevel@tonic-gate       onError = OE_Abort;
6657c478bd9Sstevel@tonic-gate     }
6667c478bd9Sstevel@tonic-gate     sqliteVdbeAddOp(v, OP_Dup, nCol-1-i, 1);
6677c478bd9Sstevel@tonic-gate     addr = sqliteVdbeAddOp(v, OP_NotNull, 1, 0);
6687c478bd9Sstevel@tonic-gate     switch( onError ){
6697c478bd9Sstevel@tonic-gate       case OE_Rollback:
6707c478bd9Sstevel@tonic-gate       case OE_Abort:
6717c478bd9Sstevel@tonic-gate       case OE_Fail: {
6727c478bd9Sstevel@tonic-gate         char *zMsg = 0;
6737c478bd9Sstevel@tonic-gate         sqliteVdbeAddOp(v, OP_Halt, SQLITE_CONSTRAINT, onError);
6747c478bd9Sstevel@tonic-gate         sqliteSetString(&zMsg, pTab->zName, ".", pTab->aCol[i].zName,
6757c478bd9Sstevel@tonic-gate                         " may not be NULL", (char*)0);
6767c478bd9Sstevel@tonic-gate         sqliteVdbeChangeP3(v, -1, zMsg, P3_DYNAMIC);
6777c478bd9Sstevel@tonic-gate         break;
6787c478bd9Sstevel@tonic-gate       }
6797c478bd9Sstevel@tonic-gate       case OE_Ignore: {
6807c478bd9Sstevel@tonic-gate         sqliteVdbeAddOp(v, OP_Pop, nCol+1+hasTwoRecnos, 0);
6817c478bd9Sstevel@tonic-gate         sqliteVdbeAddOp(v, OP_Goto, 0, ignoreDest);
6827c478bd9Sstevel@tonic-gate         break;
6837c478bd9Sstevel@tonic-gate       }
6847c478bd9Sstevel@tonic-gate       case OE_Replace: {
6857c478bd9Sstevel@tonic-gate         sqliteVdbeOp3(v, OP_String, 0, 0, pTab->aCol[i].zDflt, P3_STATIC);
6867c478bd9Sstevel@tonic-gate         sqliteVdbeAddOp(v, OP_Push, nCol-i, 0);
6877c478bd9Sstevel@tonic-gate         break;
6887c478bd9Sstevel@tonic-gate       }
6897c478bd9Sstevel@tonic-gate       default: assert(0);
6907c478bd9Sstevel@tonic-gate     }
6917c478bd9Sstevel@tonic-gate     sqliteVdbeChangeP2(v, addr, sqliteVdbeCurrentAddr(v));
6927c478bd9Sstevel@tonic-gate   }
6937c478bd9Sstevel@tonic-gate 
6947c478bd9Sstevel@tonic-gate   /* Test all CHECK constraints
6957c478bd9Sstevel@tonic-gate   */
6967c478bd9Sstevel@tonic-gate   /**** TBD ****/
6977c478bd9Sstevel@tonic-gate 
6987c478bd9Sstevel@tonic-gate   /* If we have an INTEGER PRIMARY KEY, make sure the primary key
6997c478bd9Sstevel@tonic-gate   ** of the new record does not previously exist.  Except, if this
7007c478bd9Sstevel@tonic-gate   ** is an UPDATE and the primary key is not changing, that is OK.
7017c478bd9Sstevel@tonic-gate   */
7027c478bd9Sstevel@tonic-gate   if( recnoChng ){
7037c478bd9Sstevel@tonic-gate     onError = pTab->keyConf;
7047c478bd9Sstevel@tonic-gate     if( overrideError!=OE_Default ){
7057c478bd9Sstevel@tonic-gate       onError = overrideError;
7067c478bd9Sstevel@tonic-gate     }else if( pParse->db->onError!=OE_Default ){
7077c478bd9Sstevel@tonic-gate       onError = pParse->db->onError;
7087c478bd9Sstevel@tonic-gate     }else if( onError==OE_Default ){
7097c478bd9Sstevel@tonic-gate       onError = OE_Abort;
7107c478bd9Sstevel@tonic-gate     }
711*55fea89dSDan Cross 
7127c478bd9Sstevel@tonic-gate     if( isUpdate ){
7137c478bd9Sstevel@tonic-gate       sqliteVdbeAddOp(v, OP_Dup, nCol+1, 1);
7147c478bd9Sstevel@tonic-gate       sqliteVdbeAddOp(v, OP_Dup, nCol+1, 1);
7157c478bd9Sstevel@tonic-gate       jumpInst1 = sqliteVdbeAddOp(v, OP_Eq, 0, 0);
7167c478bd9Sstevel@tonic-gate     }
7177c478bd9Sstevel@tonic-gate     sqliteVdbeAddOp(v, OP_Dup, nCol, 1);
7187c478bd9Sstevel@tonic-gate     jumpInst2 = sqliteVdbeAddOp(v, OP_NotExists, base, 0);
7197c478bd9Sstevel@tonic-gate     switch( onError ){
7207c478bd9Sstevel@tonic-gate       default: {
7217c478bd9Sstevel@tonic-gate         onError = OE_Abort;
7227c478bd9Sstevel@tonic-gate       }
72388e1588bSToomas Soome       /* FALLTHROUGH */
7247c478bd9Sstevel@tonic-gate       case OE_Rollback:
7257c478bd9Sstevel@tonic-gate       case OE_Abort:
7267c478bd9Sstevel@tonic-gate       case OE_Fail: {
7277c478bd9Sstevel@tonic-gate         sqliteVdbeOp3(v, OP_Halt, SQLITE_CONSTRAINT, onError,
7287c478bd9Sstevel@tonic-gate                          "PRIMARY KEY must be unique", P3_STATIC);
7297c478bd9Sstevel@tonic-gate         break;
7307c478bd9Sstevel@tonic-gate       }
7317c478bd9Sstevel@tonic-gate       case OE_Replace: {
7327c478bd9Sstevel@tonic-gate         sqliteGenerateRowIndexDelete(pParse->db, v, pTab, base, 0);
7337c478bd9Sstevel@tonic-gate         if( isUpdate ){
7347c478bd9Sstevel@tonic-gate           sqliteVdbeAddOp(v, OP_Dup, nCol+hasTwoRecnos, 1);
7357c478bd9Sstevel@tonic-gate           sqliteVdbeAddOp(v, OP_MoveTo, base, 0);
7367c478bd9Sstevel@tonic-gate         }
7377c478bd9Sstevel@tonic-gate         seenReplace = 1;
7387c478bd9Sstevel@tonic-gate         break;
7397c478bd9Sstevel@tonic-gate       }
7407c478bd9Sstevel@tonic-gate       case OE_Ignore: {
7417c478bd9Sstevel@tonic-gate         assert( seenReplace==0 );
7427c478bd9Sstevel@tonic-gate         sqliteVdbeAddOp(v, OP_Pop, nCol+1+hasTwoRecnos, 0);
7437c478bd9Sstevel@tonic-gate         sqliteVdbeAddOp(v, OP_Goto, 0, ignoreDest);
7447c478bd9Sstevel@tonic-gate         break;
7457c478bd9Sstevel@tonic-gate       }
7467c478bd9Sstevel@tonic-gate     }
7477c478bd9Sstevel@tonic-gate     contAddr = sqliteVdbeCurrentAddr(v);
7487c478bd9Sstevel@tonic-gate     sqliteVdbeChangeP2(v, jumpInst2, contAddr);
7497c478bd9Sstevel@tonic-gate     if( isUpdate ){
7507c478bd9Sstevel@tonic-gate       sqliteVdbeChangeP2(v, jumpInst1, contAddr);
7517c478bd9Sstevel@tonic-gate       sqliteVdbeAddOp(v, OP_Dup, nCol+1, 1);
7527c478bd9Sstevel@tonic-gate       sqliteVdbeAddOp(v, OP_MoveTo, base, 0);
7537c478bd9Sstevel@tonic-gate     }
7547c478bd9Sstevel@tonic-gate   }
7557c478bd9Sstevel@tonic-gate 
7567c478bd9Sstevel@tonic-gate   /* Test all UNIQUE constraints by creating entries for each UNIQUE
7577c478bd9Sstevel@tonic-gate   ** index and making sure that duplicate entries do not already exist.
7587c478bd9Sstevel@tonic-gate   ** Add the new records to the indices as we go.
7597c478bd9Sstevel@tonic-gate   */
7607c478bd9Sstevel@tonic-gate   extra = -1;
7617c478bd9Sstevel@tonic-gate   for(iCur=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, iCur++){
7627c478bd9Sstevel@tonic-gate     if( aIdxUsed && aIdxUsed[iCur]==0 ) continue;  /* Skip unused indices */
7637c478bd9Sstevel@tonic-gate     extra++;
7647c478bd9Sstevel@tonic-gate 
7657c478bd9Sstevel@tonic-gate     /* Create a key for accessing the index entry */
7667c478bd9Sstevel@tonic-gate     sqliteVdbeAddOp(v, OP_Dup, nCol+extra, 1);
7677c478bd9Sstevel@tonic-gate     for(i=0; i<pIdx->nColumn; i++){
7687c478bd9Sstevel@tonic-gate       int idx = pIdx->aiColumn[i];
7697c478bd9Sstevel@tonic-gate       if( idx==pTab->iPKey ){
7707c478bd9Sstevel@tonic-gate         sqliteVdbeAddOp(v, OP_Dup, i+extra+nCol+1, 1);
7717c478bd9Sstevel@tonic-gate       }else{
7727c478bd9Sstevel@tonic-gate         sqliteVdbeAddOp(v, OP_Dup, i+extra+nCol-idx, 1);
7737c478bd9Sstevel@tonic-gate       }
7747c478bd9Sstevel@tonic-gate     }
7757c478bd9Sstevel@tonic-gate     jumpInst1 = sqliteVdbeAddOp(v, OP_MakeIdxKey, pIdx->nColumn, 0);
7767c478bd9Sstevel@tonic-gate     if( pParse->db->file_format>=4 ) sqliteAddIdxKeyType(v, pIdx);
7777c478bd9Sstevel@tonic-gate 
7787c478bd9Sstevel@tonic-gate     /* Find out what action to take in case there is an indexing conflict */
7797c478bd9Sstevel@tonic-gate     onError = pIdx->onError;
7807c478bd9Sstevel@tonic-gate     if( onError==OE_None ) continue;  /* pIdx is not a UNIQUE index */
7817c478bd9Sstevel@tonic-gate     if( overrideError!=OE_Default ){
7827c478bd9Sstevel@tonic-gate       onError = overrideError;
7837c478bd9Sstevel@tonic-gate     }else if( pParse->db->onError!=OE_Default ){
7847c478bd9Sstevel@tonic-gate       onError = pParse->db->onError;
7857c478bd9Sstevel@tonic-gate     }else if( onError==OE_Default ){
7867c478bd9Sstevel@tonic-gate       onError = OE_Abort;
7877c478bd9Sstevel@tonic-gate     }
7887c478bd9Sstevel@tonic-gate     if( seenReplace ){
7897c478bd9Sstevel@tonic-gate       if( onError==OE_Ignore ) onError = OE_Replace;
7907c478bd9Sstevel@tonic-gate       else if( onError==OE_Fail ) onError = OE_Abort;
7917c478bd9Sstevel@tonic-gate     }
792*55fea89dSDan Cross 
7937c478bd9Sstevel@tonic-gate 
7947c478bd9Sstevel@tonic-gate     /* Check to see if the new index entry will be unique */
7957c478bd9Sstevel@tonic-gate     sqliteVdbeAddOp(v, OP_Dup, extra+nCol+1+hasTwoRecnos, 1);
7967c478bd9Sstevel@tonic-gate     jumpInst2 = sqliteVdbeAddOp(v, OP_IsUnique, base+iCur+1, 0);
7977c478bd9Sstevel@tonic-gate 
7987c478bd9Sstevel@tonic-gate     /* Generate code that executes if the new index entry is not unique */
7997c478bd9Sstevel@tonic-gate     switch( onError ){
8007c478bd9Sstevel@tonic-gate       case OE_Rollback:
8017c478bd9Sstevel@tonic-gate       case OE_Abort:
8027c478bd9Sstevel@tonic-gate       case OE_Fail: {
8037c478bd9Sstevel@tonic-gate         int j, n1, n2;
8047c478bd9Sstevel@tonic-gate         char zErrMsg[200];
8057c478bd9Sstevel@tonic-gate         strcpy(zErrMsg, pIdx->nColumn>1 ? "columns " : "column ");
8067c478bd9Sstevel@tonic-gate         n1 = strlen(zErrMsg);
8077c478bd9Sstevel@tonic-gate         for(j=0; j<pIdx->nColumn && n1<sizeof(zErrMsg)-30; j++){
8087c478bd9Sstevel@tonic-gate           char *zCol = pTab->aCol[pIdx->aiColumn[j]].zName;
8097c478bd9Sstevel@tonic-gate           n2 = strlen(zCol);
8107c478bd9Sstevel@tonic-gate           if( j>0 ){
8117c478bd9Sstevel@tonic-gate             strcpy(&zErrMsg[n1], ", ");
8127c478bd9Sstevel@tonic-gate             n1 += 2;
8137c478bd9Sstevel@tonic-gate           }
8147c478bd9Sstevel@tonic-gate           if( n1+n2>sizeof(zErrMsg)-30 ){
8157c478bd9Sstevel@tonic-gate             strcpy(&zErrMsg[n1], "...");
8167c478bd9Sstevel@tonic-gate             n1 += 3;
8177c478bd9Sstevel@tonic-gate             break;
8187c478bd9Sstevel@tonic-gate           }else{
8197c478bd9Sstevel@tonic-gate             strcpy(&zErrMsg[n1], zCol);
8207c478bd9Sstevel@tonic-gate             n1 += n2;
8217c478bd9Sstevel@tonic-gate           }
8227c478bd9Sstevel@tonic-gate         }
823*55fea89dSDan Cross         strcpy(&zErrMsg[n1],
8247c478bd9Sstevel@tonic-gate             pIdx->nColumn>1 ? " are not unique" : " is not unique");
8257c478bd9Sstevel@tonic-gate         sqliteVdbeOp3(v, OP_Halt, SQLITE_CONSTRAINT, onError, zErrMsg, 0);
8267c478bd9Sstevel@tonic-gate         break;
8277c478bd9Sstevel@tonic-gate       }
8287c478bd9Sstevel@tonic-gate       case OE_Ignore: {
8297c478bd9Sstevel@tonic-gate         assert( seenReplace==0 );
8307c478bd9Sstevel@tonic-gate         sqliteVdbeAddOp(v, OP_Pop, nCol+extra+3+hasTwoRecnos, 0);
8317c478bd9Sstevel@tonic-gate         sqliteVdbeAddOp(v, OP_Goto, 0, ignoreDest);
8327c478bd9Sstevel@tonic-gate         break;
8337c478bd9Sstevel@tonic-gate       }
8347c478bd9Sstevel@tonic-gate       case OE_Replace: {
8357c478bd9Sstevel@tonic-gate         sqliteGenerateRowDelete(pParse->db, v, pTab, base, 0);
8367c478bd9Sstevel@tonic-gate         if( isUpdate ){
8377c478bd9Sstevel@tonic-gate           sqliteVdbeAddOp(v, OP_Dup, nCol+extra+1+hasTwoRecnos, 1);
8387c478bd9Sstevel@tonic-gate           sqliteVdbeAddOp(v, OP_MoveTo, base, 0);
8397c478bd9Sstevel@tonic-gate         }
8407c478bd9Sstevel@tonic-gate         seenReplace = 1;
8417c478bd9Sstevel@tonic-gate         break;
8427c478bd9Sstevel@tonic-gate       }
8437c478bd9Sstevel@tonic-gate       default: assert(0);
8447c478bd9Sstevel@tonic-gate     }
8457c478bd9Sstevel@tonic-gate     contAddr = sqliteVdbeCurrentAddr(v);
8467c478bd9Sstevel@tonic-gate #if NULL_DISTINCT_FOR_UNIQUE
8477c478bd9Sstevel@tonic-gate     sqliteVdbeChangeP2(v, jumpInst1, contAddr);
8487c478bd9Sstevel@tonic-gate #endif
8497c478bd9Sstevel@tonic-gate     sqliteVdbeChangeP2(v, jumpInst2, contAddr);
8507c478bd9Sstevel@tonic-gate   }
8517c478bd9Sstevel@tonic-gate }
8527c478bd9Sstevel@tonic-gate 
8537c478bd9Sstevel@tonic-gate /*
8547c478bd9Sstevel@tonic-gate ** This routine generates code to finish the INSERT or UPDATE operation
8557c478bd9Sstevel@tonic-gate ** that was started by a prior call to sqliteGenerateConstraintChecks.
8567c478bd9Sstevel@tonic-gate ** The stack must contain keys for all active indices followed by data
8577c478bd9Sstevel@tonic-gate ** and the recno for the new entry.  This routine creates the new
8587c478bd9Sstevel@tonic-gate ** entries in all indices and in the main table.
8597c478bd9Sstevel@tonic-gate **
8607c478bd9Sstevel@tonic-gate ** The arguments to this routine should be the same as the first six
8617c478bd9Sstevel@tonic-gate ** arguments to sqliteGenerateConstraintChecks.
8627c478bd9Sstevel@tonic-gate */
sqliteCompleteInsertion(Parse * pParse,Table * pTab,int base,char * aIdxUsed,int recnoChng,int isUpdate,int newIdx)8637c478bd9Sstevel@tonic-gate void sqliteCompleteInsertion(
8647c478bd9Sstevel@tonic-gate   Parse *pParse,      /* The parser context */
8657c478bd9Sstevel@tonic-gate   Table *pTab,        /* the table into which we are inserting */
8667c478bd9Sstevel@tonic-gate   int base,           /* Index of a read/write cursor pointing at pTab */
8677c478bd9Sstevel@tonic-gate   char *aIdxUsed,     /* Which indices are used.  NULL means all are used */
8687c478bd9Sstevel@tonic-gate   int recnoChng,      /* True if the record number will change */
8697c478bd9Sstevel@tonic-gate   int isUpdate,       /* True for UPDATE, False for INSERT */
8707c478bd9Sstevel@tonic-gate   int newIdx          /* Index of NEW table for triggers.  -1 if none */
8717c478bd9Sstevel@tonic-gate ){
8727c478bd9Sstevel@tonic-gate   int i;
8737c478bd9Sstevel@tonic-gate   Vdbe *v;
8747c478bd9Sstevel@tonic-gate   int nIdx;
8757c478bd9Sstevel@tonic-gate   Index *pIdx;
8767c478bd9Sstevel@tonic-gate 
8777c478bd9Sstevel@tonic-gate   v = sqliteGetVdbe(pParse);
8787c478bd9Sstevel@tonic-gate   assert( v!=0 );
8797c478bd9Sstevel@tonic-gate   assert( pTab->pSelect==0 );  /* This table is not a VIEW */
8807c478bd9Sstevel@tonic-gate   for(nIdx=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, nIdx++){}
8817c478bd9Sstevel@tonic-gate   for(i=nIdx-1; i>=0; i--){
8827c478bd9Sstevel@tonic-gate     if( aIdxUsed && aIdxUsed[i]==0 ) continue;
8837c478bd9Sstevel@tonic-gate     sqliteVdbeAddOp(v, OP_IdxPut, base+i+1, 0);
8847c478bd9Sstevel@tonic-gate   }
8857c478bd9Sstevel@tonic-gate   sqliteVdbeAddOp(v, OP_MakeRecord, pTab->nCol, 0);
8867c478bd9Sstevel@tonic-gate   if( newIdx>=0 ){
8877c478bd9Sstevel@tonic-gate     sqliteVdbeAddOp(v, OP_Dup, 1, 0);
8887c478bd9Sstevel@tonic-gate     sqliteVdbeAddOp(v, OP_Dup, 1, 0);
8897c478bd9Sstevel@tonic-gate     sqliteVdbeAddOp(v, OP_PutIntKey, newIdx, 0);
8907c478bd9Sstevel@tonic-gate   }
8917c478bd9Sstevel@tonic-gate   sqliteVdbeAddOp(v, OP_PutIntKey, base,
8927c478bd9Sstevel@tonic-gate     (pParse->trigStack?0:OPFLAG_NCHANGE) |
8937c478bd9Sstevel@tonic-gate     (isUpdate?0:OPFLAG_LASTROWID) | OPFLAG_CSCHANGE);
8947c478bd9Sstevel@tonic-gate   if( isUpdate && recnoChng ){
8957c478bd9Sstevel@tonic-gate     sqliteVdbeAddOp(v, OP_Pop, 1, 0);
8967c478bd9Sstevel@tonic-gate   }
8977c478bd9Sstevel@tonic-gate }
8987c478bd9Sstevel@tonic-gate 
8997c478bd9Sstevel@tonic-gate /*
9007c478bd9Sstevel@tonic-gate ** Generate code that will open write cursors for a table and for all
9017c478bd9Sstevel@tonic-gate ** indices of that table.  The "base" parameter is the cursor number used
9027c478bd9Sstevel@tonic-gate ** for the table.  Indices are opened on subsequent cursors.
9037c478bd9Sstevel@tonic-gate **
9047c478bd9Sstevel@tonic-gate ** Return the total number of cursors opened.  This is always at least
9057c478bd9Sstevel@tonic-gate ** 1 (for the main table) plus more for each cursor.
9067c478bd9Sstevel@tonic-gate */
sqliteOpenTableAndIndices(Parse * pParse,Table * pTab,int base)9077c478bd9Sstevel@tonic-gate int sqliteOpenTableAndIndices(Parse *pParse, Table *pTab, int base){
9087c478bd9Sstevel@tonic-gate   int i;
9097c478bd9Sstevel@tonic-gate   Index *pIdx;
9107c478bd9Sstevel@tonic-gate   Vdbe *v = sqliteGetVdbe(pParse);
9117c478bd9Sstevel@tonic-gate   assert( v!=0 );
9127c478bd9Sstevel@tonic-gate   sqliteVdbeAddOp(v, OP_Integer, pTab->iDb, 0);
9137c478bd9Sstevel@tonic-gate   sqliteVdbeOp3(v, OP_OpenWrite, base, pTab->tnum, pTab->zName, P3_STATIC);
9147c478bd9Sstevel@tonic-gate   for(i=1, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){
9157c478bd9Sstevel@tonic-gate     sqliteVdbeAddOp(v, OP_Integer, pIdx->iDb, 0);
9167c478bd9Sstevel@tonic-gate     sqliteVdbeOp3(v, OP_OpenWrite, i+base, pIdx->tnum, pIdx->zName, P3_STATIC);
9177c478bd9Sstevel@tonic-gate   }
9187c478bd9Sstevel@tonic-gate   return i;
9197c478bd9Sstevel@tonic-gate }
920