xref: /sqlite-3.40.0/src/insert.c (revision d230f648)
1 /*
2 ** 2001 September 15
3 **
4 ** The author disclaims copyright to this source code.  In place of
5 ** a legal notice, here is a blessing:
6 **
7 **    May you do good and not evil.
8 **    May you find forgiveness for yourself and forgive others.
9 **    May you share freely, never taking more than you give.
10 **
11 *************************************************************************
12 ** This file contains C code routines that are called by the parser
13 ** to handle INSERT statements in SQLite.
14 **
15 ** $Id: insert.c,v 1.186 2007/05/04 13:15:56 drh Exp $
16 */
17 #include "sqliteInt.h"
18 
19 /*
20 ** Set P3 of the most recently inserted opcode to a column affinity
21 ** string for index pIdx. A column affinity string has one character
22 ** for each column in the table, according to the affinity of the column:
23 **
24 **  Character      Column affinity
25 **  ------------------------------
26 **  'a'            TEXT
27 **  'b'            NONE
28 **  'c'            NUMERIC
29 **  'd'            INTEGER
30 **  'e'            REAL
31 */
32 void sqlite3IndexAffinityStr(Vdbe *v, Index *pIdx){
33   if( !pIdx->zColAff ){
34     /* The first time a column affinity string for a particular index is
35     ** required, it is allocated and populated here. It is then stored as
36     ** a member of the Index structure for subsequent use.
37     **
38     ** The column affinity string will eventually be deleted by
39     ** sqliteDeleteIndex() when the Index structure itself is cleaned
40     ** up.
41     */
42     int n;
43     Table *pTab = pIdx->pTable;
44     pIdx->zColAff = (char *)sqliteMalloc(pIdx->nColumn+1);
45     if( !pIdx->zColAff ){
46       return;
47     }
48     for(n=0; n<pIdx->nColumn; n++){
49       pIdx->zColAff[n] = pTab->aCol[pIdx->aiColumn[n]].affinity;
50     }
51     pIdx->zColAff[pIdx->nColumn] = '\0';
52   }
53 
54   sqlite3VdbeChangeP3(v, -1, pIdx->zColAff, 0);
55 }
56 
57 /*
58 ** Set P3 of the most recently inserted opcode to a column affinity
59 ** string for table pTab. A column affinity string has one character
60 ** for each column indexed by the index, according to the affinity of the
61 ** column:
62 **
63 **  Character      Column affinity
64 **  ------------------------------
65 **  'a'            TEXT
66 **  'b'            NONE
67 **  'c'            NUMERIC
68 **  'd'            INTEGER
69 **  'e'            REAL
70 */
71 void sqlite3TableAffinityStr(Vdbe *v, Table *pTab){
72   /* The first time a column affinity string for a particular table
73   ** is required, it is allocated and populated here. It is then
74   ** stored as a member of the Table structure for subsequent use.
75   **
76   ** The column affinity string will eventually be deleted by
77   ** sqlite3DeleteTable() when the Table structure itself is cleaned up.
78   */
79   if( !pTab->zColAff ){
80     char *zColAff;
81     int i;
82 
83     zColAff = (char *)sqliteMalloc(pTab->nCol+1);
84     if( !zColAff ){
85       return;
86     }
87 
88     for(i=0; i<pTab->nCol; i++){
89       zColAff[i] = pTab->aCol[i].affinity;
90     }
91     zColAff[pTab->nCol] = '\0';
92 
93     pTab->zColAff = zColAff;
94   }
95 
96   sqlite3VdbeChangeP3(v, -1, pTab->zColAff, 0);
97 }
98 
99 /*
100 ** Return non-zero if SELECT statement p opens the table with rootpage
101 ** iTab in database iDb.  This is used to see if a statement of the form
102 ** "INSERT INTO <iDb, iTab> SELECT ..." can run without using temporary
103 ** table for the results of the SELECT.
104 **
105 ** No checking is done for sub-selects that are part of expressions.
106 */
107 static int selectReadsTable(Select *p, Schema *pSchema, int iTab){
108   int i;
109   struct SrcList_item *pItem;
110   if( p->pSrc==0 ) return 0;
111   for(i=0, pItem=p->pSrc->a; i<p->pSrc->nSrc; i++, pItem++){
112     if( pItem->pSelect ){
113       if( selectReadsTable(pItem->pSelect, pSchema, iTab) ) return 1;
114     }else{
115       if( pItem->pTab->pSchema==pSchema && pItem->pTab->tnum==iTab ) return 1;
116     }
117   }
118   return 0;
119 }
120 
121 #ifndef SQLITE_OMIT_AUTOINCREMENT
122 /*
123 ** Write out code to initialize the autoincrement logic.  This code
124 ** looks up the current autoincrement value in the sqlite_sequence
125 ** table and stores that value in a memory cell.  Code generated by
126 ** autoIncStep() will keep that memory cell holding the largest
127 ** rowid value.  Code generated by autoIncEnd() will write the new
128 ** largest value of the counter back into the sqlite_sequence table.
129 **
130 ** This routine returns the index of the mem[] cell that contains
131 ** the maximum rowid counter.
132 **
133 ** Two memory cells are allocated.  The next memory cell after the
134 ** one returned holds the rowid in sqlite_sequence where we will
135 ** write back the revised maximum rowid.
136 */
137 static int autoIncBegin(
138   Parse *pParse,      /* Parsing context */
139   int iDb,            /* Index of the database holding pTab */
140   Table *pTab         /* The table we are writing to */
141 ){
142   int memId = 0;
143   if( pTab->autoInc ){
144     Vdbe *v = pParse->pVdbe;
145     Db *pDb = &pParse->db->aDb[iDb];
146     int iCur = pParse->nTab;
147     int addr;
148     assert( v );
149     addr = sqlite3VdbeCurrentAddr(v);
150     memId = pParse->nMem+1;
151     pParse->nMem += 2;
152     sqlite3OpenTable(pParse, iCur, iDb, pDb->pSchema->pSeqTab, OP_OpenRead);
153     sqlite3VdbeAddOp(v, OP_Rewind, iCur, addr+13);
154     sqlite3VdbeAddOp(v, OP_Column, iCur, 0);
155     sqlite3VdbeOp3(v, OP_String8, 0, 0, pTab->zName, 0);
156     sqlite3VdbeAddOp(v, OP_Ne, 0x100, addr+12);
157     sqlite3VdbeAddOp(v, OP_Rowid, iCur, 0);
158     sqlite3VdbeAddOp(v, OP_MemStore, memId-1, 1);
159     sqlite3VdbeAddOp(v, OP_Column, iCur, 1);
160     sqlite3VdbeAddOp(v, OP_MemStore, memId, 1);
161     sqlite3VdbeAddOp(v, OP_Goto, 0, addr+13);
162     sqlite3VdbeAddOp(v, OP_Next, iCur, addr+4);
163     sqlite3VdbeAddOp(v, OP_Close, iCur, 0);
164   }
165   return memId;
166 }
167 
168 /*
169 ** Update the maximum rowid for an autoincrement calculation.
170 **
171 ** This routine should be called when the top of the stack holds a
172 ** new rowid that is about to be inserted.  If that new rowid is
173 ** larger than the maximum rowid in the memId memory cell, then the
174 ** memory cell is updated.  The stack is unchanged.
175 */
176 static void autoIncStep(Parse *pParse, int memId){
177   if( memId>0 ){
178     sqlite3VdbeAddOp(pParse->pVdbe, OP_MemMax, memId, 0);
179   }
180 }
181 
182 /*
183 ** After doing one or more inserts, the maximum rowid is stored
184 ** in mem[memId].  Generate code to write this value back into the
185 ** the sqlite_sequence table.
186 */
187 static void autoIncEnd(
188   Parse *pParse,     /* The parsing context */
189   int iDb,           /* Index of the database holding pTab */
190   Table *pTab,       /* Table we are inserting into */
191   int memId          /* Memory cell holding the maximum rowid */
192 ){
193   if( pTab->autoInc ){
194     int iCur = pParse->nTab;
195     Vdbe *v = pParse->pVdbe;
196     Db *pDb = &pParse->db->aDb[iDb];
197     int addr;
198     assert( v );
199     addr = sqlite3VdbeCurrentAddr(v);
200     sqlite3OpenTable(pParse, iCur, iDb, pDb->pSchema->pSeqTab, OP_OpenWrite);
201     sqlite3VdbeAddOp(v, OP_MemLoad, memId-1, 0);
202     sqlite3VdbeAddOp(v, OP_NotNull, -1, addr+7);
203     sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
204     sqlite3VdbeAddOp(v, OP_NewRowid, iCur, 0);
205     sqlite3VdbeOp3(v, OP_String8, 0, 0, pTab->zName, 0);
206     sqlite3VdbeAddOp(v, OP_MemLoad, memId, 0);
207     sqlite3VdbeAddOp(v, OP_MakeRecord, 2, 0);
208     sqlite3VdbeAddOp(v, OP_Insert, iCur, OPFLAG_APPEND);
209     sqlite3VdbeAddOp(v, OP_Close, iCur, 0);
210   }
211 }
212 #else
213 /*
214 ** If SQLITE_OMIT_AUTOINCREMENT is defined, then the three routines
215 ** above are all no-ops
216 */
217 # define autoIncBegin(A,B,C) (0)
218 # define autoIncStep(A,B)
219 # define autoIncEnd(A,B,C,D)
220 #endif /* SQLITE_OMIT_AUTOINCREMENT */
221 
222 
223 /* Forward declaration */
224 static int xferOptimization(
225   Parse *pParse,        /* Parser context */
226   Table *pDest,         /* The table we are inserting into */
227   Select *pSelect,      /* A SELECT statement to use as the data source */
228   int onError,          /* How to handle constraint errors */
229   int iDbDest           /* The database of pDest */
230 );
231 
232 /*
233 ** This routine is call to handle SQL of the following forms:
234 **
235 **    insert into TABLE (IDLIST) values(EXPRLIST)
236 **    insert into TABLE (IDLIST) select
237 **
238 ** The IDLIST following the table name is always optional.  If omitted,
239 ** then a list of all columns for the table is substituted.  The IDLIST
240 ** appears in the pColumn parameter.  pColumn is NULL if IDLIST is omitted.
241 **
242 ** The pList parameter holds EXPRLIST in the first form of the INSERT
243 ** statement above, and pSelect is NULL.  For the second form, pList is
244 ** NULL and pSelect is a pointer to the select statement used to generate
245 ** data for the insert.
246 **
247 ** The code generated follows one of four templates.  For a simple
248 ** select with data coming from a VALUES clause, the code executes
249 ** once straight down through.  The template looks like this:
250 **
251 **         open write cursor to <table> and its indices
252 **         puts VALUES clause expressions onto the stack
253 **         write the resulting record into <table>
254 **         cleanup
255 **
256 ** The three remaining templates assume the statement is of the form
257 **
258 **   INSERT INTO <table> SELECT ...
259 **
260 ** If the SELECT clause is of the restricted form "SELECT * FROM <table2>" -
261 ** in other words if the SELECT pulls all columns from a single table
262 ** and there is no WHERE or LIMIT or GROUP BY or ORDER BY clauses, and
263 ** if <table2> and <table1> are distinct tables but have identical
264 ** schemas, including all the same indices, then a special optimization
265 ** is invoked that copies raw records from <table2> over to <table1>.
266 ** See the xferOptimization() function for the implementation of this
267 ** template.  This is the second template.
268 **
269 **         open a write cursor to <table>
270 **         open read cursor on <table2>
271 **         transfer all records in <table2> over to <table>
272 **         close cursors
273 **         foreach index on <table>
274 **           open a write cursor on the <table> index
275 **           open a read cursor on the corresponding <table2> index
276 **           transfer all records from the read to the write cursors
277 **           close cursors
278 **         end foreach
279 **
280 ** The third template is for when the second template does not apply
281 ** and the SELECT clause does not read from <table> at any time.
282 ** The generated code follows this template:
283 **
284 **         goto B
285 **      A: setup for the SELECT
286 **         loop over the rows in the SELECT
287 **           gosub C
288 **         end loop
289 **         cleanup after the SELECT
290 **         goto D
291 **      B: open write cursor to <table> and its indices
292 **         goto A
293 **      C: insert the select result into <table>
294 **         return
295 **      D: cleanup
296 **
297 ** The fourth template is used if the insert statement takes its
298 ** values from a SELECT but the data is being inserted into a table
299 ** that is also read as part of the SELECT.  In the third form,
300 ** we have to use a intermediate table to store the results of
301 ** the select.  The template is like this:
302 **
303 **         goto B
304 **      A: setup for the SELECT
305 **         loop over the tables in the SELECT
306 **           gosub C
307 **         end loop
308 **         cleanup after the SELECT
309 **         goto D
310 **      C: insert the select result into the intermediate table
311 **         return
312 **      B: open a cursor to an intermediate table
313 **         goto A
314 **      D: open write cursor to <table> and its indices
315 **         loop over the intermediate table
316 **           transfer values form intermediate table into <table>
317 **         end the loop
318 **         cleanup
319 */
320 void sqlite3Insert(
321   Parse *pParse,        /* Parser context */
322   SrcList *pTabList,    /* Name of table into which we are inserting */
323   ExprList *pList,      /* List of values to be inserted */
324   Select *pSelect,      /* A SELECT statement to use as the data source */
325   IdList *pColumn,      /* Column names corresponding to IDLIST. */
326   int onError           /* How to handle constraint errors */
327 ){
328   Table *pTab;          /* The table to insert into */
329   char *zTab;           /* Name of the table into which we are inserting */
330   const char *zDb;      /* Name of the database holding this table */
331   int i, j, idx;        /* Loop counters */
332   Vdbe *v;              /* Generate code into this virtual machine */
333   Index *pIdx;          /* For looping over indices of the table */
334   int nColumn;          /* Number of columns in the data */
335   int base = 0;         /* VDBE Cursor number for pTab */
336   int iCont=0,iBreak=0; /* Beginning and end of the loop over srcTab */
337   sqlite3 *db;          /* The main database structure */
338   int keyColumn = -1;   /* Column that is the INTEGER PRIMARY KEY */
339   int endOfLoop;        /* Label for the end of the insertion loop */
340   int useTempTable = 0; /* Store SELECT results in intermediate table */
341   int srcTab = 0;       /* Data comes from this temporary cursor if >=0 */
342   int iSelectLoop = 0;  /* Address of code that implements the SELECT */
343   int iCleanup = 0;     /* Address of the cleanup code */
344   int iInsertBlock = 0; /* Address of the subroutine used to insert data */
345   int iCntMem = 0;      /* Memory cell used for the row counter */
346   int newIdx = -1;      /* Cursor for the NEW table */
347   Db *pDb;              /* The database containing table being inserted into */
348   int counterMem = 0;   /* Memory cell holding AUTOINCREMENT counter */
349   int appendFlag = 0;   /* True if the insert is likely to be an append */
350   int iDb;
351 
352 #ifndef SQLITE_OMIT_TRIGGER
353   int isView;                 /* True if attempting to insert into a view */
354   int triggers_exist = 0;     /* True if there are FOR EACH ROW triggers */
355 #endif
356 
357   if( pParse->nErr || sqlite3MallocFailed() ){
358     goto insert_cleanup;
359   }
360   db = pParse->db;
361 
362   /* Locate the table into which we will be inserting new information.
363   */
364   assert( pTabList->nSrc==1 );
365   zTab = pTabList->a[0].zName;
366   if( zTab==0 ) goto insert_cleanup;
367   pTab = sqlite3SrcListLookup(pParse, pTabList);
368   if( pTab==0 ){
369     goto insert_cleanup;
370   }
371   iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
372   assert( iDb<db->nDb );
373   pDb = &db->aDb[iDb];
374   zDb = pDb->zName;
375   if( sqlite3AuthCheck(pParse, SQLITE_INSERT, pTab->zName, 0, zDb) ){
376     goto insert_cleanup;
377   }
378 
379   /* Figure out if we have any triggers and if the table being
380   ** inserted into is a view
381   */
382 #ifndef SQLITE_OMIT_TRIGGER
383   triggers_exist = sqlite3TriggersExist(pParse, pTab, TK_INSERT, 0);
384   isView = pTab->pSelect!=0;
385 #else
386 # define triggers_exist 0
387 # define isView 0
388 #endif
389 #ifdef SQLITE_OMIT_VIEW
390 # undef isView
391 # define isView 0
392 #endif
393 
394   /* Ensure that:
395   *  (a) the table is not read-only,
396   *  (b) that if it is a view then ON INSERT triggers exist
397   */
398   if( sqlite3IsReadOnly(pParse, pTab, triggers_exist) ){
399     goto insert_cleanup;
400   }
401   assert( pTab!=0 );
402 
403   /* If pTab is really a view, make sure it has been initialized.
404   ** ViewGetColumnNames() is a no-op if pTab is not a view (or virtual
405   ** module table).
406   */
407   if( sqlite3ViewGetColumnNames(pParse, pTab) ){
408     goto insert_cleanup;
409   }
410 
411   /* Allocate a VDBE
412   */
413   v = sqlite3GetVdbe(pParse);
414   if( v==0 ) goto insert_cleanup;
415   if( pParse->nested==0 ) sqlite3VdbeCountChanges(v);
416   sqlite3BeginWriteOperation(pParse, pSelect || triggers_exist, iDb);
417 
418   /* if there are row triggers, allocate a temp table for new.* references. */
419   if( triggers_exist ){
420     newIdx = pParse->nTab++;
421   }
422 
423 #ifndef SQLITE_OMIT_XFER_OPT
424   /* If the statement is of the form
425   **
426   **       INSERT INTO <table1> SELECT * FROM <table2>;
427   **
428   ** Then special optimizations can be applied that make the transfer
429   ** very fast and which reduce fragmentation of indices.
430   */
431   if( pColumn==0 && xferOptimization(pParse, pTab, pSelect, onError, iDb) ){
432     assert( !triggers_exist );
433     assert( pList==0 );
434     goto insert_cleanup;
435   }
436 #endif /* SQLITE_OMIT_XFER_OPT */
437 
438   /* If this is an AUTOINCREMENT table, look up the sequence number in the
439   ** sqlite_sequence table and store it in memory cell counterMem.  Also
440   ** remember the rowid of the sqlite_sequence table entry in memory cell
441   ** counterRowid.
442   */
443   counterMem = autoIncBegin(pParse, iDb, pTab);
444 
445   /* Figure out how many columns of data are supplied.  If the data
446   ** is coming from a SELECT statement, then this step also generates
447   ** all the code to implement the SELECT statement and invoke a subroutine
448   ** to process each row of the result. (Template 2.) If the SELECT
449   ** statement uses the the table that is being inserted into, then the
450   ** subroutine is also coded here.  That subroutine stores the SELECT
451   ** results in a temporary table. (Template 3.)
452   */
453   if( pSelect ){
454     /* Data is coming from a SELECT.  Generate code to implement that SELECT
455     */
456     int rc, iInitCode;
457     iInitCode = sqlite3VdbeAddOp(v, OP_Goto, 0, 0);
458     iSelectLoop = sqlite3VdbeCurrentAddr(v);
459     iInsertBlock = sqlite3VdbeMakeLabel(v);
460 
461     /* Resolve the expressions in the SELECT statement and execute it. */
462     rc = sqlite3Select(pParse, pSelect, SRT_Subroutine, iInsertBlock,0,0,0,0);
463     if( rc || pParse->nErr || sqlite3MallocFailed() ){
464       goto insert_cleanup;
465     }
466 
467     iCleanup = sqlite3VdbeMakeLabel(v);
468     sqlite3VdbeAddOp(v, OP_Goto, 0, iCleanup);
469     assert( pSelect->pEList );
470     nColumn = pSelect->pEList->nExpr;
471 
472     /* Set useTempTable to TRUE if the result of the SELECT statement
473     ** should be written into a temporary table.  Set to FALSE if each
474     ** row of the SELECT can be written directly into the result table.
475     **
476     ** A temp table must be used if the table being updated is also one
477     ** of the tables being read by the SELECT statement.  Also use a
478     ** temp table in the case of row triggers.
479     */
480     if( triggers_exist || selectReadsTable(pSelect,pTab->pSchema,pTab->tnum) ){
481       useTempTable = 1;
482     }
483 
484     if( useTempTable ){
485       /* Generate the subroutine that SELECT calls to process each row of
486       ** the result.  Store the result in a temporary table
487       */
488       srcTab = pParse->nTab++;
489       sqlite3VdbeResolveLabel(v, iInsertBlock);
490       sqlite3VdbeAddOp(v, OP_MakeRecord, nColumn, 0);
491       sqlite3VdbeAddOp(v, OP_NewRowid, srcTab, 0);
492       sqlite3VdbeAddOp(v, OP_Pull, 1, 0);
493       sqlite3VdbeAddOp(v, OP_Insert, srcTab, OPFLAG_APPEND);
494       sqlite3VdbeAddOp(v, OP_Return, 0, 0);
495 
496       /* The following code runs first because the GOTO at the very top
497       ** of the program jumps to it.  Create the temporary table, then jump
498       ** back up and execute the SELECT code above.
499       */
500       sqlite3VdbeJumpHere(v, iInitCode);
501       sqlite3VdbeAddOp(v, OP_OpenEphemeral, srcTab, 0);
502       sqlite3VdbeAddOp(v, OP_SetNumColumns, srcTab, nColumn);
503       sqlite3VdbeAddOp(v, OP_Goto, 0, iSelectLoop);
504       sqlite3VdbeResolveLabel(v, iCleanup);
505     }else{
506       sqlite3VdbeJumpHere(v, iInitCode);
507     }
508   }else{
509     /* This is the case if the data for the INSERT is coming from a VALUES
510     ** clause
511     */
512     NameContext sNC;
513     memset(&sNC, 0, sizeof(sNC));
514     sNC.pParse = pParse;
515     srcTab = -1;
516     useTempTable = 0;
517     nColumn = pList ? pList->nExpr : 0;
518     for(i=0; i<nColumn; i++){
519       if( sqlite3ExprResolveNames(&sNC, pList->a[i].pExpr) ){
520         goto insert_cleanup;
521       }
522     }
523   }
524 
525   /* Make sure the number of columns in the source data matches the number
526   ** of columns to be inserted into the table.
527   */
528   if( pColumn==0 && nColumn && nColumn!=pTab->nCol ){
529     sqlite3ErrorMsg(pParse,
530        "table %S has %d columns but %d values were supplied",
531        pTabList, 0, pTab->nCol, nColumn);
532     goto insert_cleanup;
533   }
534   if( pColumn!=0 && nColumn!=pColumn->nId ){
535     sqlite3ErrorMsg(pParse, "%d values for %d columns", nColumn, pColumn->nId);
536     goto insert_cleanup;
537   }
538 
539   /* If the INSERT statement included an IDLIST term, then make sure
540   ** all elements of the IDLIST really are columns of the table and
541   ** remember the column indices.
542   **
543   ** If the table has an INTEGER PRIMARY KEY column and that column
544   ** is named in the IDLIST, then record in the keyColumn variable
545   ** the index into IDLIST of the primary key column.  keyColumn is
546   ** the index of the primary key as it appears in IDLIST, not as
547   ** is appears in the original table.  (The index of the primary
548   ** key in the original table is pTab->iPKey.)
549   */
550   if( pColumn ){
551     for(i=0; i<pColumn->nId; i++){
552       pColumn->a[i].idx = -1;
553     }
554     for(i=0; i<pColumn->nId; i++){
555       for(j=0; j<pTab->nCol; j++){
556         if( sqlite3StrICmp(pColumn->a[i].zName, pTab->aCol[j].zName)==0 ){
557           pColumn->a[i].idx = j;
558           if( j==pTab->iPKey ){
559             keyColumn = i;
560           }
561           break;
562         }
563       }
564       if( j>=pTab->nCol ){
565         if( sqlite3IsRowid(pColumn->a[i].zName) ){
566           keyColumn = i;
567         }else{
568           sqlite3ErrorMsg(pParse, "table %S has no column named %s",
569               pTabList, 0, pColumn->a[i].zName);
570           pParse->nErr++;
571           goto insert_cleanup;
572         }
573       }
574     }
575   }
576 
577   /* If there is no IDLIST term but the table has an integer primary
578   ** key, the set the keyColumn variable to the primary key column index
579   ** in the original table definition.
580   */
581   if( pColumn==0 && nColumn>0 ){
582     keyColumn = pTab->iPKey;
583   }
584 
585   /* Open the temp table for FOR EACH ROW triggers
586   */
587   if( triggers_exist ){
588     sqlite3VdbeAddOp(v, OP_OpenPseudo, newIdx, 0);
589     sqlite3VdbeAddOp(v, OP_SetNumColumns, newIdx, pTab->nCol);
590   }
591 
592   /* Initialize the count of rows to be inserted
593   */
594   if( db->flags & SQLITE_CountRows ){
595     iCntMem = pParse->nMem++;
596     sqlite3VdbeAddOp(v, OP_MemInt, 0, iCntMem);
597   }
598 
599   /* Open tables and indices if there are no row triggers */
600   if( !triggers_exist ){
601     base = pParse->nTab;
602     sqlite3OpenTableAndIndices(pParse, pTab, base, OP_OpenWrite);
603   }
604 
605   /* If the data source is a temporary table, then we have to create
606   ** a loop because there might be multiple rows of data.  If the data
607   ** source is a subroutine call from the SELECT statement, then we need
608   ** to launch the SELECT statement processing.
609   */
610   if( useTempTable ){
611     iBreak = sqlite3VdbeMakeLabel(v);
612     sqlite3VdbeAddOp(v, OP_Rewind, srcTab, iBreak);
613     iCont = sqlite3VdbeCurrentAddr(v);
614   }else if( pSelect ){
615     sqlite3VdbeAddOp(v, OP_Goto, 0, iSelectLoop);
616     sqlite3VdbeResolveLabel(v, iInsertBlock);
617   }
618 
619   /* Run the BEFORE and INSTEAD OF triggers, if there are any
620   */
621   endOfLoop = sqlite3VdbeMakeLabel(v);
622   if( triggers_exist & TRIGGER_BEFORE ){
623 
624     /* build the NEW.* reference row.  Note that if there is an INTEGER
625     ** PRIMARY KEY into which a NULL is being inserted, that NULL will be
626     ** translated into a unique ID for the row.  But on a BEFORE trigger,
627     ** we do not know what the unique ID will be (because the insert has
628     ** not happened yet) so we substitute a rowid of -1
629     */
630     if( keyColumn<0 ){
631       sqlite3VdbeAddOp(v, OP_Integer, -1, 0);
632     }else if( useTempTable ){
633       sqlite3VdbeAddOp(v, OP_Column, srcTab, keyColumn);
634     }else{
635       assert( pSelect==0 );  /* Otherwise useTempTable is true */
636       sqlite3ExprCode(pParse, pList->a[keyColumn].pExpr);
637       sqlite3VdbeAddOp(v, OP_NotNull, -1, sqlite3VdbeCurrentAddr(v)+3);
638       sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
639       sqlite3VdbeAddOp(v, OP_Integer, -1, 0);
640       sqlite3VdbeAddOp(v, OP_MustBeInt, 0, 0);
641     }
642 
643     /* Create the new column data
644     */
645     for(i=0; i<pTab->nCol; i++){
646       if( pColumn==0 ){
647         j = i;
648       }else{
649         for(j=0; j<pColumn->nId; j++){
650           if( pColumn->a[j].idx==i ) break;
651         }
652       }
653       if( pColumn && j>=pColumn->nId ){
654         sqlite3ExprCode(pParse, pTab->aCol[i].pDflt);
655       }else if( useTempTable ){
656         sqlite3VdbeAddOp(v, OP_Column, srcTab, j);
657       }else{
658         assert( pSelect==0 ); /* Otherwise useTempTable is true */
659         sqlite3ExprCodeAndCache(pParse, pList->a[j].pExpr);
660       }
661     }
662     sqlite3VdbeAddOp(v, OP_MakeRecord, pTab->nCol, 0);
663 
664     /* If this is an INSERT on a view with an INSTEAD OF INSERT trigger,
665     ** do not attempt any conversions before assembling the record.
666     ** If this is a real table, attempt conversions as required by the
667     ** table column affinities.
668     */
669     if( !isView ){
670       sqlite3TableAffinityStr(v, pTab);
671     }
672     sqlite3VdbeAddOp(v, OP_Insert, newIdx, 0);
673 
674     /* Fire BEFORE or INSTEAD OF triggers */
675     if( sqlite3CodeRowTrigger(pParse, TK_INSERT, 0, TRIGGER_BEFORE, pTab,
676         newIdx, -1, onError, endOfLoop) ){
677       goto insert_cleanup;
678     }
679   }
680 
681   /* If any triggers exists, the opening of tables and indices is deferred
682   ** until now.
683   */
684   if( triggers_exist && !isView ){
685     base = pParse->nTab;
686     sqlite3OpenTableAndIndices(pParse, pTab, base, OP_OpenWrite);
687   }
688 
689   /* Push the record number for the new entry onto the stack.  The
690   ** record number is a randomly generate integer created by NewRowid
691   ** except when the table has an INTEGER PRIMARY KEY column, in which
692   ** case the record number is the same as that column.
693   */
694   if( !isView ){
695     if( IsVirtual(pTab) ){
696       /* The row that the VUpdate opcode will delete:  none */
697       sqlite3VdbeAddOp(v, OP_Null, 0, 0);
698     }
699     if( keyColumn>=0 ){
700       if( useTempTable ){
701         sqlite3VdbeAddOp(v, OP_Column, srcTab, keyColumn);
702       }else if( pSelect ){
703         sqlite3VdbeAddOp(v, OP_Dup, nColumn - keyColumn - 1, 1);
704       }else{
705         VdbeOp *pOp;
706         sqlite3ExprCode(pParse, pList->a[keyColumn].pExpr);
707         pOp = sqlite3VdbeGetOp(v, sqlite3VdbeCurrentAddr(v) - 1);
708         if( pOp && pOp->opcode==OP_Null ){
709           appendFlag = 1;
710           pOp->opcode = OP_NewRowid;
711           pOp->p1 = base;
712           pOp->p2 = counterMem;
713         }
714       }
715       /* If the PRIMARY KEY expression is NULL, then use OP_NewRowid
716       ** to generate a unique primary key value.
717       */
718       if( !appendFlag ){
719         sqlite3VdbeAddOp(v, OP_NotNull, -1, sqlite3VdbeCurrentAddr(v)+3);
720         sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
721         sqlite3VdbeAddOp(v, OP_NewRowid, base, counterMem);
722         sqlite3VdbeAddOp(v, OP_MustBeInt, 0, 0);
723       }
724     }else if( IsVirtual(pTab) ){
725       sqlite3VdbeAddOp(v, OP_Null, 0, 0);
726     }else{
727       sqlite3VdbeAddOp(v, OP_NewRowid, base, counterMem);
728       appendFlag = 1;
729     }
730     autoIncStep(pParse, counterMem);
731 
732     /* Push onto the stack, data for all columns of the new entry, beginning
733     ** with the first column.
734     */
735     for(i=0; i<pTab->nCol; i++){
736       if( i==pTab->iPKey ){
737         /* The value of the INTEGER PRIMARY KEY column is always a NULL.
738         ** Whenever this column is read, the record number will be substituted
739         ** in its place.  So will fill this column with a NULL to avoid
740         ** taking up data space with information that will never be used. */
741         sqlite3VdbeAddOp(v, OP_Null, 0, 0);
742         continue;
743       }
744       if( pColumn==0 ){
745         j = i;
746       }else{
747         for(j=0; j<pColumn->nId; j++){
748           if( pColumn->a[j].idx==i ) break;
749         }
750       }
751       if( nColumn==0 || (pColumn && j>=pColumn->nId) ){
752         sqlite3ExprCode(pParse, pTab->aCol[i].pDflt);
753       }else if( useTempTable ){
754         sqlite3VdbeAddOp(v, OP_Column, srcTab, j);
755       }else if( pSelect ){
756         sqlite3VdbeAddOp(v, OP_Dup, i+nColumn-j+IsVirtual(pTab), 1);
757       }else{
758         sqlite3ExprCode(pParse, pList->a[j].pExpr);
759       }
760     }
761 
762     /* Generate code to check constraints and generate index keys and
763     ** do the insertion.
764     */
765 #ifndef SQLITE_OMIT_VIRTUALTABLE
766     if( IsVirtual(pTab) ){
767       pParse->pVirtualLock = pTab;
768       sqlite3VdbeOp3(v, OP_VUpdate, 1, pTab->nCol+2,
769                      (const char*)pTab->pVtab, P3_VTAB);
770     }else
771 #endif
772     {
773       sqlite3GenerateConstraintChecks(pParse, pTab, base, 0, keyColumn>=0,
774                                      0, onError, endOfLoop);
775       sqlite3CompleteInsertion(pParse, pTab, base, 0,0,0,
776                             (triggers_exist & TRIGGER_AFTER)!=0 ? newIdx : -1,
777                             appendFlag);
778     }
779   }
780 
781   /* Update the count of rows that are inserted
782   */
783   if( (db->flags & SQLITE_CountRows)!=0 ){
784     sqlite3VdbeAddOp(v, OP_MemIncr, 1, iCntMem);
785   }
786 
787   if( triggers_exist ){
788     /* Close all tables opened */
789     if( !isView ){
790       sqlite3VdbeAddOp(v, OP_Close, base, 0);
791       for(idx=1, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, idx++){
792         sqlite3VdbeAddOp(v, OP_Close, idx+base, 0);
793       }
794     }
795 
796     /* Code AFTER triggers */
797     if( sqlite3CodeRowTrigger(pParse, TK_INSERT, 0, TRIGGER_AFTER, pTab,
798           newIdx, -1, onError, endOfLoop) ){
799       goto insert_cleanup;
800     }
801   }
802 
803   /* The bottom of the loop, if the data source is a SELECT statement
804   */
805   sqlite3VdbeResolveLabel(v, endOfLoop);
806   if( useTempTable ){
807     sqlite3VdbeAddOp(v, OP_Next, srcTab, iCont);
808     sqlite3VdbeResolveLabel(v, iBreak);
809     sqlite3VdbeAddOp(v, OP_Close, srcTab, 0);
810   }else if( pSelect ){
811     sqlite3VdbeAddOp(v, OP_Pop, nColumn, 0);
812     sqlite3VdbeAddOp(v, OP_Return, 0, 0);
813     sqlite3VdbeResolveLabel(v, iCleanup);
814   }
815 
816   if( !triggers_exist && !IsVirtual(pTab) ){
817     /* Close all tables opened */
818     sqlite3VdbeAddOp(v, OP_Close, base, 0);
819     for(idx=1, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, idx++){
820       sqlite3VdbeAddOp(v, OP_Close, idx+base, 0);
821     }
822   }
823 
824   /* Update the sqlite_sequence table by storing the content of the
825   ** counter value in memory counterMem back into the sqlite_sequence
826   ** table.
827   */
828   autoIncEnd(pParse, iDb, pTab, counterMem);
829 
830   /*
831   ** Return the number of rows inserted. If this routine is
832   ** generating code because of a call to sqlite3NestedParse(), do not
833   ** invoke the callback function.
834   */
835   if( db->flags & SQLITE_CountRows && pParse->nested==0 && !pParse->trigStack ){
836     sqlite3VdbeAddOp(v, OP_MemLoad, iCntMem, 0);
837     sqlite3VdbeAddOp(v, OP_Callback, 1, 0);
838     sqlite3VdbeSetNumCols(v, 1);
839     sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "rows inserted", P3_STATIC);
840   }
841 
842 insert_cleanup:
843   sqlite3SrcListDelete(pTabList);
844   sqlite3ExprListDelete(pList);
845   sqlite3SelectDelete(pSelect);
846   sqlite3IdListDelete(pColumn);
847 }
848 
849 /*
850 ** Generate code to do a constraint check prior to an INSERT or an UPDATE.
851 **
852 ** When this routine is called, the stack contains (from bottom to top)
853 ** the following values:
854 **
855 **    1.  The rowid of the row to be updated before the update.  This
856 **        value is omitted unless we are doing an UPDATE that involves a
857 **        change to the record number.
858 **
859 **    2.  The rowid of the row after the update.
860 **
861 **    3.  The data in the first column of the entry after the update.
862 **
863 **    i.  Data from middle columns...
864 **
865 **    N.  The data in the last column of the entry after the update.
866 **
867 ** The old rowid shown as entry (1) above is omitted unless both isUpdate
868 ** and rowidChng are 1.  isUpdate is true for UPDATEs and false for
869 ** INSERTs and rowidChng is true if the record number is being changed.
870 **
871 ** The code generated by this routine pushes additional entries onto
872 ** the stack which are the keys for new index entries for the new record.
873 ** The order of index keys is the same as the order of the indices on
874 ** the pTable->pIndex list.  A key is only created for index i if
875 ** aIdxUsed!=0 and aIdxUsed[i]!=0.
876 **
877 ** This routine also generates code to check constraints.  NOT NULL,
878 ** CHECK, and UNIQUE constraints are all checked.  If a constraint fails,
879 ** then the appropriate action is performed.  There are five possible
880 ** actions: ROLLBACK, ABORT, FAIL, REPLACE, and IGNORE.
881 **
882 **  Constraint type  Action       What Happens
883 **  ---------------  ----------   ----------------------------------------
884 **  any              ROLLBACK     The current transaction is rolled back and
885 **                                sqlite3_exec() returns immediately with a
886 **                                return code of SQLITE_CONSTRAINT.
887 **
888 **  any              ABORT        Back out changes from the current command
889 **                                only (do not do a complete rollback) then
890 **                                cause sqlite3_exec() to return immediately
891 **                                with SQLITE_CONSTRAINT.
892 **
893 **  any              FAIL         Sqlite_exec() returns immediately with a
894 **                                return code of SQLITE_CONSTRAINT.  The
895 **                                transaction is not rolled back and any
896 **                                prior changes are retained.
897 **
898 **  any              IGNORE       The record number and data is popped from
899 **                                the stack and there is an immediate jump
900 **                                to label ignoreDest.
901 **
902 **  NOT NULL         REPLACE      The NULL value is replace by the default
903 **                                value for that column.  If the default value
904 **                                is NULL, the action is the same as ABORT.
905 **
906 **  UNIQUE           REPLACE      The other row that conflicts with the row
907 **                                being inserted is removed.
908 **
909 **  CHECK            REPLACE      Illegal.  The results in an exception.
910 **
911 ** Which action to take is determined by the overrideError parameter.
912 ** Or if overrideError==OE_Default, then the pParse->onError parameter
913 ** is used.  Or if pParse->onError==OE_Default then the onError value
914 ** for the constraint is used.
915 **
916 ** The calling routine must open a read/write cursor for pTab with
917 ** cursor number "base".  All indices of pTab must also have open
918 ** read/write cursors with cursor number base+i for the i-th cursor.
919 ** Except, if there is no possibility of a REPLACE action then
920 ** cursors do not need to be open for indices where aIdxUsed[i]==0.
921 **
922 ** If the isUpdate flag is true, it means that the "base" cursor is
923 ** initially pointing to an entry that is being updated.  The isUpdate
924 ** flag causes extra code to be generated so that the "base" cursor
925 ** is still pointing at the same entry after the routine returns.
926 ** Without the isUpdate flag, the "base" cursor might be moved.
927 */
928 void sqlite3GenerateConstraintChecks(
929   Parse *pParse,      /* The parser context */
930   Table *pTab,        /* the table into which we are inserting */
931   int base,           /* Index of a read/write cursor pointing at pTab */
932   char *aIdxUsed,     /* Which indices are used.  NULL means all are used */
933   int rowidChng,      /* True if the record number will change */
934   int isUpdate,       /* True for UPDATE, False for INSERT */
935   int overrideError,  /* Override onError to this if not OE_Default */
936   int ignoreDest      /* Jump to this label on an OE_Ignore resolution */
937 ){
938   int i;
939   Vdbe *v;
940   int nCol;
941   int onError;
942   int addr;
943   int extra;
944   int iCur;
945   Index *pIdx;
946   int seenReplace = 0;
947   int jumpInst1=0, jumpInst2;
948   int hasTwoRowids = (isUpdate && rowidChng);
949 
950   v = sqlite3GetVdbe(pParse);
951   assert( v!=0 );
952   assert( pTab->pSelect==0 );  /* This table is not a VIEW */
953   nCol = pTab->nCol;
954 
955   /* Test all NOT NULL constraints.
956   */
957   for(i=0; i<nCol; i++){
958     if( i==pTab->iPKey ){
959       continue;
960     }
961     onError = pTab->aCol[i].notNull;
962     if( onError==OE_None ) continue;
963     if( overrideError!=OE_Default ){
964       onError = overrideError;
965     }else if( onError==OE_Default ){
966       onError = OE_Abort;
967     }
968     if( onError==OE_Replace && pTab->aCol[i].pDflt==0 ){
969       onError = OE_Abort;
970     }
971     sqlite3VdbeAddOp(v, OP_Dup, nCol-1-i, 1);
972     addr = sqlite3VdbeAddOp(v, OP_NotNull, 1, 0);
973     assert( onError==OE_Rollback || onError==OE_Abort || onError==OE_Fail
974         || onError==OE_Ignore || onError==OE_Replace );
975     switch( onError ){
976       case OE_Rollback:
977       case OE_Abort:
978       case OE_Fail: {
979         char *zMsg = 0;
980         sqlite3VdbeAddOp(v, OP_Halt, SQLITE_CONSTRAINT, onError);
981         sqlite3SetString(&zMsg, pTab->zName, ".", pTab->aCol[i].zName,
982                         " may not be NULL", (char*)0);
983         sqlite3VdbeChangeP3(v, -1, zMsg, P3_DYNAMIC);
984         break;
985       }
986       case OE_Ignore: {
987         sqlite3VdbeAddOp(v, OP_Pop, nCol+1+hasTwoRowids, 0);
988         sqlite3VdbeAddOp(v, OP_Goto, 0, ignoreDest);
989         break;
990       }
991       case OE_Replace: {
992         sqlite3ExprCode(pParse, pTab->aCol[i].pDflt);
993         sqlite3VdbeAddOp(v, OP_Push, nCol-i, 0);
994         break;
995       }
996     }
997     sqlite3VdbeJumpHere(v, addr);
998   }
999 
1000   /* Test all CHECK constraints
1001   */
1002 #ifndef SQLITE_OMIT_CHECK
1003   if( pTab->pCheck && (pParse->db->flags & SQLITE_IgnoreChecks)==0 ){
1004     int allOk = sqlite3VdbeMakeLabel(v);
1005     assert( pParse->ckOffset==0 );
1006     pParse->ckOffset = nCol;
1007     sqlite3ExprIfTrue(pParse, pTab->pCheck, allOk, 1);
1008     assert( pParse->ckOffset==nCol );
1009     pParse->ckOffset = 0;
1010     onError = overrideError!=OE_Default ? overrideError : OE_Abort;
1011     if( onError==OE_Ignore || onError==OE_Replace ){
1012       sqlite3VdbeAddOp(v, OP_Pop, nCol+1+hasTwoRowids, 0);
1013       sqlite3VdbeAddOp(v, OP_Goto, 0, ignoreDest);
1014     }else{
1015       sqlite3VdbeAddOp(v, OP_Halt, SQLITE_CONSTRAINT, onError);
1016     }
1017     sqlite3VdbeResolveLabel(v, allOk);
1018   }
1019 #endif /* !defined(SQLITE_OMIT_CHECK) */
1020 
1021   /* If we have an INTEGER PRIMARY KEY, make sure the primary key
1022   ** of the new record does not previously exist.  Except, if this
1023   ** is an UPDATE and the primary key is not changing, that is OK.
1024   */
1025   if( rowidChng ){
1026     onError = pTab->keyConf;
1027     if( overrideError!=OE_Default ){
1028       onError = overrideError;
1029     }else if( onError==OE_Default ){
1030       onError = OE_Abort;
1031     }
1032 
1033     if( isUpdate ){
1034       sqlite3VdbeAddOp(v, OP_Dup, nCol+1, 1);
1035       sqlite3VdbeAddOp(v, OP_Dup, nCol+1, 1);
1036       jumpInst1 = sqlite3VdbeAddOp(v, OP_Eq, 0, 0);
1037     }
1038     sqlite3VdbeAddOp(v, OP_Dup, nCol, 1);
1039     jumpInst2 = sqlite3VdbeAddOp(v, OP_NotExists, base, 0);
1040     switch( onError ){
1041       default: {
1042         onError = OE_Abort;
1043         /* Fall thru into the next case */
1044       }
1045       case OE_Rollback:
1046       case OE_Abort:
1047       case OE_Fail: {
1048         sqlite3VdbeOp3(v, OP_Halt, SQLITE_CONSTRAINT, onError,
1049                          "PRIMARY KEY must be unique", P3_STATIC);
1050         break;
1051       }
1052       case OE_Replace: {
1053         sqlite3GenerateRowIndexDelete(v, pTab, base, 0);
1054         if( isUpdate ){
1055           sqlite3VdbeAddOp(v, OP_Dup, nCol+hasTwoRowids, 1);
1056           sqlite3VdbeAddOp(v, OP_MoveGe, base, 0);
1057         }
1058         seenReplace = 1;
1059         break;
1060       }
1061       case OE_Ignore: {
1062         assert( seenReplace==0 );
1063         sqlite3VdbeAddOp(v, OP_Pop, nCol+1+hasTwoRowids, 0);
1064         sqlite3VdbeAddOp(v, OP_Goto, 0, ignoreDest);
1065         break;
1066       }
1067     }
1068     sqlite3VdbeJumpHere(v, jumpInst2);
1069     if( isUpdate ){
1070       sqlite3VdbeJumpHere(v, jumpInst1);
1071       sqlite3VdbeAddOp(v, OP_Dup, nCol+1, 1);
1072       sqlite3VdbeAddOp(v, OP_MoveGe, base, 0);
1073     }
1074   }
1075 
1076   /* Test all UNIQUE constraints by creating entries for each UNIQUE
1077   ** index and making sure that duplicate entries do not already exist.
1078   ** Add the new records to the indices as we go.
1079   */
1080   extra = -1;
1081   for(iCur=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, iCur++){
1082     if( aIdxUsed && aIdxUsed[iCur]==0 ) continue;  /* Skip unused indices */
1083     extra++;
1084 
1085     /* Create a key for accessing the index entry */
1086     sqlite3VdbeAddOp(v, OP_Dup, nCol+extra, 1);
1087     for(i=0; i<pIdx->nColumn; i++){
1088       int idx = pIdx->aiColumn[i];
1089       if( idx==pTab->iPKey ){
1090         sqlite3VdbeAddOp(v, OP_Dup, i+extra+nCol+1, 1);
1091       }else{
1092         sqlite3VdbeAddOp(v, OP_Dup, i+extra+nCol-idx, 1);
1093       }
1094     }
1095     jumpInst1 = sqlite3VdbeAddOp(v, OP_MakeIdxRec, pIdx->nColumn, 0);
1096     sqlite3IndexAffinityStr(v, pIdx);
1097 
1098     /* Find out what action to take in case there is an indexing conflict */
1099     onError = pIdx->onError;
1100     if( onError==OE_None ) continue;  /* pIdx is not a UNIQUE index */
1101     if( overrideError!=OE_Default ){
1102       onError = overrideError;
1103     }else if( onError==OE_Default ){
1104       onError = OE_Abort;
1105     }
1106     if( seenReplace ){
1107       if( onError==OE_Ignore ) onError = OE_Replace;
1108       else if( onError==OE_Fail ) onError = OE_Abort;
1109     }
1110 
1111 
1112     /* Check to see if the new index entry will be unique */
1113     sqlite3VdbeAddOp(v, OP_Dup, extra+nCol+1+hasTwoRowids, 1);
1114     jumpInst2 = sqlite3VdbeAddOp(v, OP_IsUnique, base+iCur+1, 0);
1115 
1116     /* Generate code that executes if the new index entry is not unique */
1117     assert( onError==OE_Rollback || onError==OE_Abort || onError==OE_Fail
1118         || onError==OE_Ignore || onError==OE_Replace );
1119     switch( onError ){
1120       case OE_Rollback:
1121       case OE_Abort:
1122       case OE_Fail: {
1123         int j, n1, n2;
1124         char zErrMsg[200];
1125         sqlite3_snprintf(sizeof(zErrMsg), zErrMsg,
1126                          pIdx->nColumn>1 ? "columns " : "column ");
1127         n1 = strlen(zErrMsg);
1128         for(j=0; j<pIdx->nColumn && n1<sizeof(zErrMsg)-30; j++){
1129           char *zCol = pTab->aCol[pIdx->aiColumn[j]].zName;
1130           n2 = strlen(zCol);
1131           if( j>0 ){
1132             sqlite3_snprintf(sizeof(zErrMsg)-n1, &zErrMsg[n1], ", ");
1133             n1 += 2;
1134           }
1135           if( n1+n2>sizeof(zErrMsg)-30 ){
1136             sqlite3_snprintf(sizeof(zErrMsg)-n1, &zErrMsg[n1], "...");
1137             n1 += 3;
1138             break;
1139           }else{
1140             sqlite3_snprintf(sizeof(zErrMsg)-n1, &zErrMsg[n1], "%s", zCol);
1141             n1 += n2;
1142           }
1143         }
1144         sqlite3_snprintf(sizeof(zErrMsg)-n1, &zErrMsg[n1],
1145             pIdx->nColumn>1 ? " are not unique" : " is not unique");
1146         sqlite3VdbeOp3(v, OP_Halt, SQLITE_CONSTRAINT, onError, zErrMsg, 0);
1147         break;
1148       }
1149       case OE_Ignore: {
1150         assert( seenReplace==0 );
1151         sqlite3VdbeAddOp(v, OP_Pop, nCol+extra+3+hasTwoRowids, 0);
1152         sqlite3VdbeAddOp(v, OP_Goto, 0, ignoreDest);
1153         break;
1154       }
1155       case OE_Replace: {
1156         sqlite3GenerateRowDelete(pParse->db, v, pTab, base, 0);
1157         if( isUpdate ){
1158           sqlite3VdbeAddOp(v, OP_Dup, nCol+extra+1+hasTwoRowids, 1);
1159           sqlite3VdbeAddOp(v, OP_MoveGe, base, 0);
1160         }
1161         seenReplace = 1;
1162         break;
1163       }
1164     }
1165 #if NULL_DISTINCT_FOR_UNIQUE
1166     sqlite3VdbeJumpHere(v, jumpInst1);
1167 #endif
1168     sqlite3VdbeJumpHere(v, jumpInst2);
1169   }
1170 }
1171 
1172 /*
1173 ** This routine generates code to finish the INSERT or UPDATE operation
1174 ** that was started by a prior call to sqlite3GenerateConstraintChecks.
1175 ** The stack must contain keys for all active indices followed by data
1176 ** and the rowid for the new entry.  This routine creates the new
1177 ** entries in all indices and in the main table.
1178 **
1179 ** The arguments to this routine should be the same as the first six
1180 ** arguments to sqlite3GenerateConstraintChecks.
1181 */
1182 void sqlite3CompleteInsertion(
1183   Parse *pParse,      /* The parser context */
1184   Table *pTab,        /* the table into which we are inserting */
1185   int base,           /* Index of a read/write cursor pointing at pTab */
1186   char *aIdxUsed,     /* Which indices are used.  NULL means all are used */
1187   int rowidChng,      /* True if the record number will change */
1188   int isUpdate,       /* True for UPDATE, False for INSERT */
1189   int newIdx,         /* Index of NEW table for triggers.  -1 if none */
1190   int appendBias      /* True if this is likely to be an append */
1191 ){
1192   int i;
1193   Vdbe *v;
1194   int nIdx;
1195   Index *pIdx;
1196   int pik_flags;
1197 
1198   v = sqlite3GetVdbe(pParse);
1199   assert( v!=0 );
1200   assert( pTab->pSelect==0 );  /* This table is not a VIEW */
1201   for(nIdx=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, nIdx++){}
1202   for(i=nIdx-1; i>=0; i--){
1203     if( aIdxUsed && aIdxUsed[i]==0 ) continue;
1204     sqlite3VdbeAddOp(v, OP_IdxInsert, base+i+1, 0);
1205   }
1206   sqlite3VdbeAddOp(v, OP_MakeRecord, pTab->nCol, 0);
1207   sqlite3TableAffinityStr(v, pTab);
1208 #ifndef SQLITE_OMIT_TRIGGER
1209   if( newIdx>=0 ){
1210     sqlite3VdbeAddOp(v, OP_Dup, 1, 0);
1211     sqlite3VdbeAddOp(v, OP_Dup, 1, 0);
1212     sqlite3VdbeAddOp(v, OP_Insert, newIdx, 0);
1213   }
1214 #endif
1215   if( pParse->nested ){
1216     pik_flags = 0;
1217   }else{
1218     pik_flags = OPFLAG_NCHANGE;
1219     pik_flags |= (isUpdate?OPFLAG_ISUPDATE:OPFLAG_LASTROWID);
1220   }
1221   if( appendBias ){
1222     pik_flags |= OPFLAG_APPEND;
1223   }
1224   sqlite3VdbeAddOp(v, OP_Insert, base, pik_flags);
1225   if( !pParse->nested ){
1226     sqlite3VdbeChangeP3(v, -1, pTab->zName, P3_STATIC);
1227   }
1228 
1229   if( isUpdate && rowidChng ){
1230     sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
1231   }
1232 }
1233 
1234 /*
1235 ** Generate code that will open cursors for a table and for all
1236 ** indices of that table.  The "base" parameter is the cursor number used
1237 ** for the table.  Indices are opened on subsequent cursors.
1238 */
1239 void sqlite3OpenTableAndIndices(
1240   Parse *pParse,   /* Parsing context */
1241   Table *pTab,     /* Table to be opened */
1242   int base,        /* Cursor number assigned to the table */
1243   int op           /* OP_OpenRead or OP_OpenWrite */
1244 ){
1245   int i;
1246   int iDb;
1247   Index *pIdx;
1248   Vdbe *v;
1249 
1250   if( IsVirtual(pTab) ) return;
1251   iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
1252   v = sqlite3GetVdbe(pParse);
1253   assert( v!=0 );
1254   sqlite3OpenTable(pParse, base, iDb, pTab, op);
1255   for(i=1, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){
1256     KeyInfo *pKey = sqlite3IndexKeyinfo(pParse, pIdx);
1257     assert( pIdx->pSchema==pTab->pSchema );
1258     sqlite3VdbeAddOp(v, OP_Integer, iDb, 0);
1259     VdbeComment((v, "# %s", pIdx->zName));
1260     sqlite3VdbeOp3(v, op, i+base, pIdx->tnum, (char*)pKey, P3_KEYINFO_HANDOFF);
1261   }
1262   if( pParse->nTab<=base+i ){
1263     pParse->nTab = base+i;
1264   }
1265 }
1266 
1267 
1268 #ifdef SQLITE_TEST
1269 /*
1270 ** The following global variable is incremented whenever the
1271 ** transfer optimization is used.  This is used for testing
1272 ** purposes only - to make sure the transfer optimization really
1273 ** is happening when it is suppose to.
1274 */
1275 int sqlite3_xferopt_count;
1276 #endif /* SQLITE_TEST */
1277 
1278 
1279 #ifndef SQLITE_OMIT_XFER_OPT
1280 /*
1281 ** Check to collation names to see if they are compatible.
1282 */
1283 static int xferCompatibleCollation(const char *z1, const char *z2){
1284   if( z1==0 ){
1285     return z2==0;
1286   }
1287   if( z2==0 ){
1288     return 0;
1289   }
1290   return sqlite3StrICmp(z1, z2)==0;
1291 }
1292 
1293 
1294 /*
1295 ** Check to see if index pSrc is compatible as a source of data
1296 ** for index pDest in an insert transfer optimization.  The rules
1297 ** for a compatible index:
1298 **
1299 **    *   The index is over the same set of columns
1300 **    *   The same DESC and ASC markings occurs on all columns
1301 **    *   The same onError processing (OE_Abort, OE_Ignore, etc)
1302 **    *   The same collating sequence on each column
1303 */
1304 static int xferCompatibleIndex(Index *pDest, Index *pSrc){
1305   int i;
1306   assert( pDest && pSrc );
1307   assert( pDest->pTable!=pSrc->pTable );
1308   if( pDest->nColumn!=pSrc->nColumn ){
1309     return 0;   /* Different number of columns */
1310   }
1311   if( pDest->onError!=pSrc->onError ){
1312     return 0;   /* Different conflict resolution strategies */
1313   }
1314   for(i=0; i<pSrc->nColumn; i++){
1315     if( pSrc->aiColumn[i]!=pDest->aiColumn[i] ){
1316       return 0;   /* Different columns indexed */
1317     }
1318     if( pSrc->aSortOrder[i]!=pDest->aSortOrder[i] ){
1319       return 0;   /* Different sort orders */
1320     }
1321     if( pSrc->azColl[i]!=pDest->azColl[i] ){
1322       return 0;   /* Different sort orders */
1323     }
1324   }
1325 
1326   /* If no test above fails then the indices must be compatible */
1327   return 1;
1328 }
1329 
1330 /*
1331 ** Attempt the transfer optimization on INSERTs of the form
1332 **
1333 **     INSERT INTO tab1 SELECT * FROM tab2;
1334 **
1335 ** This optimization is only attempted if
1336 **
1337 **    (1)  tab1 and tab2 have identical schemas including all the
1338 **         same indices and constraints
1339 **
1340 **    (2)  tab1 and tab2 are different tables
1341 **
1342 **    (3)  There must be no triggers on tab1
1343 **
1344 **    (4)  The result set of the SELECT statement is "*"
1345 **
1346 **    (5)  The SELECT statement has no WHERE, HAVING, ORDER BY, GROUP BY,
1347 **         or LIMIT clause.
1348 **
1349 **    (6)  The SELECT statement is a simple (not a compound) select that
1350 **         contains only tab2 in its FROM clause
1351 **
1352 ** This method for implementing the INSERT transfers raw records from
1353 ** tab2 over to tab1.  The columns are not decoded.  Raw records from
1354 ** the indices of tab2 are transfered to tab1 as well.  In so doing,
1355 ** the resulting tab1 has much less fragmentation.
1356 **
1357 ** This routine returns TRUE if the optimization is attempted.  If any
1358 ** of the conditions above fail so that the optimization should not
1359 ** be attempted, then this routine returns FALSE.
1360 */
1361 static int xferOptimization(
1362   Parse *pParse,        /* Parser context */
1363   Table *pDest,         /* The table we are inserting into */
1364   Select *pSelect,      /* A SELECT statement to use as the data source */
1365   int onError,          /* How to handle constraint errors */
1366   int iDbDest           /* The database of pDest */
1367 ){
1368   ExprList *pEList;                /* The result set of the SELECT */
1369   Table *pSrc;                     /* The table in the FROM clause of SELECT */
1370   Index *pSrcIdx, *pDestIdx;       /* Source and destination indices */
1371   struct SrcList_item *pItem;      /* An element of pSelect->pSrc */
1372   int i;                           /* Loop counter */
1373   int iDbSrc;                      /* The database of pSrc */
1374   int iSrc, iDest;                 /* Cursors from source and destination */
1375   int addr1, addr2;                /* Loop addresses */
1376   int emptyDestTest;               /* Address of test for empty pDest */
1377   int emptySrcTest;                /* Address of test for empty pSrc */
1378   Vdbe *v;                         /* The VDBE we are building */
1379   KeyInfo *pKey;                   /* Key information for an index */
1380   int counterMem;                  /* Memory register used by AUTOINC */
1381   int destHasUniqueIdx = 0;        /* True if pDest has a UNIQUE index */
1382 
1383   if( pSelect==0 ){
1384     return 0;   /* Must be of the form  INSERT INTO ... SELECT ... */
1385   }
1386   if( pDest->pTrigger ){
1387     return 0;   /* tab1 must not have triggers */
1388   }
1389 #ifndef SQLITE_OMIT_VIRTUALTABLE
1390   if( pDest->isVirtual ){
1391     return 0;   /* tab1 must not be a virtual table */
1392   }
1393 #endif
1394   if( onError==OE_Default ){
1395     onError = OE_Abort;
1396   }
1397   if( onError!=OE_Abort && onError!=OE_Rollback ){
1398     return 0;   /* Cannot do OR REPLACE or OR IGNORE or OR FAIL */
1399   }
1400   if( pSelect->pSrc==0 ){
1401     return 0;   /* SELECT must have a FROM clause */
1402   }
1403   if( pSelect->pSrc->nSrc!=1 ){
1404     return 0;   /* FROM clause must have exactly one term */
1405   }
1406   if( pSelect->pSrc->a[0].pSelect ){
1407     return 0;   /* FROM clause cannot contain a subquery */
1408   }
1409   if( pSelect->pWhere ){
1410     return 0;   /* SELECT may not have a WHERE clause */
1411   }
1412   if( pSelect->pOrderBy ){
1413     return 0;   /* SELECT may not have an ORDER BY clause */
1414   }
1415   /* Do not need to test for a HAVING clause.  If HAVING is present but
1416   ** there is no ORDER BY, we will get an error. */
1417   if( pSelect->pGroupBy ){
1418     return 0;   /* SELECT may not have a GROUP BY clause */
1419   }
1420   if( pSelect->pLimit ){
1421     return 0;   /* SELECT may not have a LIMIT clause */
1422   }
1423   assert( pSelect->pOffset==0 );  /* Must be so if pLimit==0 */
1424   if( pSelect->pPrior ){
1425     return 0;   /* SELECT may not be a compound query */
1426   }
1427   if( pSelect->isDistinct ){
1428     return 0;   /* SELECT may not be DISTINCT */
1429   }
1430   pEList = pSelect->pEList;
1431   assert( pEList!=0 );
1432   if( pEList->nExpr!=1 ){
1433     return 0;   /* The result set must have exactly one column */
1434   }
1435   assert( pEList->a[0].pExpr );
1436   if( pEList->a[0].pExpr->op!=TK_ALL ){
1437     return 0;   /* The result set must be the special operator "*" */
1438   }
1439 
1440   /* At this point we have established that the statement is of the
1441   ** correct syntactic form to participate in this optimization.  Now
1442   ** we have to check the semantics.
1443   */
1444   pItem = pSelect->pSrc->a;
1445   pSrc = sqlite3LocateTable(pParse, pItem->zName, pItem->zDatabase);
1446   if( pSrc==0 ){
1447     return 0;   /* FROM clause does not contain a real table */
1448   }
1449   if( pSrc==pDest ){
1450     return 0;   /* tab1 and tab2 may not be the same table */
1451   }
1452 #ifndef SQLITE_OMIT_VIRTUALTABLE
1453   if( pSrc->isVirtual ){
1454     return 0;   /* tab2 must not be a virtual table */
1455   }
1456 #endif
1457   if( pSrc->pSelect ){
1458     return 0;   /* tab2 may not be a view */
1459   }
1460   if( pDest->nCol!=pSrc->nCol ){
1461     return 0;   /* Number of columns must be the same in tab1 and tab2 */
1462   }
1463   if( pDest->iPKey!=pSrc->iPKey ){
1464     return 0;   /* Both tables must have the same INTEGER PRIMARY KEY */
1465   }
1466   for(i=0; i<pDest->nCol; i++){
1467     if( pDest->aCol[i].affinity!=pSrc->aCol[i].affinity ){
1468       return 0;    /* Affinity must be the same on all columns */
1469     }
1470     if( !xferCompatibleCollation(pDest->aCol[i].zColl, pSrc->aCol[i].zColl) ){
1471       return 0;    /* Collating sequence must be the same on all columns */
1472     }
1473     if( pDest->aCol[i].notNull && !pSrc->aCol[i].notNull ){
1474       return 0;    /* tab2 must be NOT NULL if tab1 is */
1475     }
1476   }
1477   for(pDestIdx=pDest->pIndex; pDestIdx; pDestIdx=pDestIdx->pNext){
1478     if( pDestIdx->onError!=OE_None ){
1479       destHasUniqueIdx = 1;
1480     }
1481     for(pSrcIdx=pSrc->pIndex; pSrcIdx; pSrcIdx=pSrcIdx->pNext){
1482       if( xferCompatibleIndex(pDestIdx, pSrcIdx) ) break;
1483     }
1484     if( pSrcIdx==0 ){
1485       return 0;    /* pDestIdx has no corresponding index in pSrc */
1486     }
1487   }
1488 #ifndef SQLITE_OMIT_CHECK
1489   if( pDest->pCheck && !sqlite3ExprCompare(pSrc->pCheck, pDest->pCheck) ){
1490     return 0;   /* Tables have different CHECK constraints.  Ticket #2252 */
1491   }
1492 #endif
1493 
1494   /* If we get this far, it means either:
1495   **
1496   **    *   We can always do the transfer if the table contains an
1497   **        an integer primary key
1498   **
1499   **    *   We can conditionally do the transfer if the destination
1500   **        table is empty.
1501   */
1502 #ifdef SQLITE_TEST
1503   sqlite3_xferopt_count++;
1504 #endif
1505   iDbSrc = sqlite3SchemaToIndex(pParse->db, pSrc->pSchema);
1506   v = sqlite3GetVdbe(pParse);
1507   iSrc = pParse->nTab++;
1508   iDest = pParse->nTab++;
1509   counterMem = autoIncBegin(pParse, iDbDest, pDest);
1510   sqlite3OpenTable(pParse, iDest, iDbDest, pDest, OP_OpenWrite);
1511   if( (pDest->iPKey<0 && pDest->pIndex!=0) || destHasUniqueIdx ){
1512     /* If tables do not have an INTEGER PRIMARY KEY and there
1513     ** are indices to be copied and the destination is not empty,
1514     ** we have to disallow the transfer optimization because the
1515     ** the rowids might change which will mess up indexing.
1516     **
1517     ** Or if the destination has a UNIQUE index and is not empty,
1518     ** we also disallow the transfer optimization because we cannot
1519     ** insure that all entries in the union of DEST and SRC will be
1520     ** unique.
1521     */
1522     addr1 = sqlite3VdbeAddOp(v, OP_Rewind, iDest, 0);
1523     emptyDestTest = sqlite3VdbeAddOp(v, OP_Goto, 0, 0);
1524     sqlite3VdbeJumpHere(v, addr1);
1525   }else{
1526     emptyDestTest = 0;
1527   }
1528   sqlite3OpenTable(pParse, iSrc, iDbSrc, pSrc, OP_OpenRead);
1529   emptySrcTest = sqlite3VdbeAddOp(v, OP_Rewind, iSrc, 0);
1530   if( pDest->iPKey>=0 ){
1531     addr1 = sqlite3VdbeAddOp(v, OP_Rowid, iSrc, 0);
1532     sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
1533     addr2 = sqlite3VdbeAddOp(v, OP_NotExists, iDest, 0);
1534     sqlite3VdbeOp3(v, OP_Halt, SQLITE_CONSTRAINT, onError,
1535                       "PRIMARY KEY must be unique", P3_STATIC);
1536     sqlite3VdbeJumpHere(v, addr2);
1537     autoIncStep(pParse, counterMem);
1538   }else if( pDest->pIndex==0 ){
1539     addr1 = sqlite3VdbeAddOp(v, OP_NewRowid, iDest, 0);
1540   }else{
1541     addr1 = sqlite3VdbeAddOp(v, OP_Rowid, iSrc, 0);
1542     assert( pDest->autoInc==0 );
1543   }
1544   sqlite3VdbeAddOp(v, OP_RowData, iSrc, 0);
1545   sqlite3VdbeOp3(v, OP_Insert, iDest,
1546                     OPFLAG_NCHANGE|OPFLAG_LASTROWID|OPFLAG_APPEND,
1547                     pDest->zName, 0);
1548   sqlite3VdbeAddOp(v, OP_Next, iSrc, addr1);
1549   autoIncEnd(pParse, iDbDest, pDest, counterMem);
1550   for(pDestIdx=pDest->pIndex; pDestIdx; pDestIdx=pDestIdx->pNext){
1551     for(pSrcIdx=pSrc->pIndex; pSrcIdx; pSrcIdx=pSrcIdx->pNext){
1552       if( xferCompatibleIndex(pDestIdx, pSrcIdx) ) break;
1553     }
1554     assert( pSrcIdx );
1555     sqlite3VdbeAddOp(v, OP_Close, iSrc, 0);
1556     sqlite3VdbeAddOp(v, OP_Close, iDest, 0);
1557     sqlite3VdbeAddOp(v, OP_Integer, iDbSrc, 0);
1558     pKey = sqlite3IndexKeyinfo(pParse, pSrcIdx);
1559     VdbeComment((v, "# %s", pSrcIdx->zName));
1560     sqlite3VdbeOp3(v, OP_OpenRead, iSrc, pSrcIdx->tnum,
1561                    (char*)pKey, P3_KEYINFO_HANDOFF);
1562     sqlite3VdbeAddOp(v, OP_Integer, iDbDest, 0);
1563     pKey = sqlite3IndexKeyinfo(pParse, pDestIdx);
1564     VdbeComment((v, "# %s", pDestIdx->zName));
1565     sqlite3VdbeOp3(v, OP_OpenWrite, iDest, pDestIdx->tnum,
1566                    (char*)pKey, P3_KEYINFO_HANDOFF);
1567     addr1 = sqlite3VdbeAddOp(v, OP_Rewind, iSrc, 0);
1568     sqlite3VdbeAddOp(v, OP_RowKey, iSrc, 0);
1569     sqlite3VdbeAddOp(v, OP_IdxInsert, iDest, 1);
1570     sqlite3VdbeAddOp(v, OP_Next, iSrc, addr1+1);
1571     sqlite3VdbeJumpHere(v, addr1);
1572   }
1573   sqlite3VdbeJumpHere(v, emptySrcTest);
1574   sqlite3VdbeAddOp(v, OP_Close, iSrc, 0);
1575   sqlite3VdbeAddOp(v, OP_Close, iDest, 0);
1576   if( emptyDestTest ){
1577     sqlite3VdbeAddOp(v, OP_Halt, SQLITE_OK, 0);
1578     sqlite3VdbeJumpHere(v, emptyDestTest);
1579     sqlite3VdbeAddOp(v, OP_Close, iDest, 0);
1580     return 0;
1581   }else{
1582     return 1;
1583   }
1584 }
1585 #endif /* SQLITE_OMIT_XFER_OPT */
1586