blob: 6823a72cb913f68c76af5253724d9accf31bd398 [file] [log] [blame]
drhcce7d172000-05-31 15:34:51 +00001/*
drhb19a2bc2001-09-16 00:13:26 +00002** 2001 September 15
drhcce7d172000-05-31 15:34:51 +00003**
drhb19a2bc2001-09-16 00:13:26 +00004** The author disclaims copyright to this source code. In place of
5** a legal notice, here is a blessing:
drhcce7d172000-05-31 15:34:51 +00006**
drhb19a2bc2001-09-16 00:13:26 +00007** 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.
drhcce7d172000-05-31 15:34:51 +000010**
11*************************************************************************
12** This file contains C code routines that are called by the parser
drhb19a2bc2001-09-16 00:13:26 +000013** to handle INSERT statements in SQLite.
drhcce7d172000-05-31 15:34:51 +000014**
drh8103b7d2007-02-24 13:23:51 +000015** $Id: insert.c,v 1.174 2007/02/24 13:23:52 drh Exp $
drhcce7d172000-05-31 15:34:51 +000016*/
17#include "sqliteInt.h"
18
19/*
danielk19773d1bfea2004-05-14 11:00:53 +000020** Set P3 of the most recently inserted opcode to a column affinity
danielk1977a37cdde2004-05-16 11:15:36 +000021** string for index pIdx. A column affinity string has one character
danielk19773d1bfea2004-05-14 11:00:53 +000022** for each column in the table, according to the affinity of the column:
23**
24** Character Column affinity
25** ------------------------------
drh3eda0402005-11-24 13:15:32 +000026** 'a' TEXT
27** 'b' NONE
28** 'c' NUMERIC
29** 'd' INTEGER
30** 'e' REAL
danielk19773d1bfea2004-05-14 11:00:53 +000031*/
danielk1977a37cdde2004-05-16 11:15:36 +000032void sqlite3IndexAffinityStr(Vdbe *v, Index *pIdx){
33 if( !pIdx->zColAff ){
danielk1977e014a832004-05-17 10:48:57 +000034 /* The first time a column affinity string for a particular index is
danielk1977a37cdde2004-05-16 11:15:36 +000035 ** required, it is allocated and populated here. It is then stored as
danielk1977e014a832004-05-17 10:48:57 +000036 ** a member of the Index structure for subsequent use.
danielk1977a37cdde2004-05-16 11:15:36 +000037 **
38 ** The column affinity string will eventually be deleted by
danielk1977e014a832004-05-17 10:48:57 +000039 ** sqliteDeleteIndex() when the Index structure itself is cleaned
danielk1977a37cdde2004-05-16 11:15:36 +000040 ** 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 }
danielk19773d1bfea2004-05-14 11:00:53 +000053
drh956bc922004-07-24 17:38:29 +000054 sqlite3VdbeChangeP3(v, -1, pIdx->zColAff, 0);
danielk1977a37cdde2004-05-16 11:15:36 +000055}
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** ------------------------------
drh3eda0402005-11-24 13:15:32 +000065** 'a' TEXT
66** 'b' NONE
67** 'c' NUMERIC
68** 'd' INTEGER
69** 'e' REAL
danielk1977a37cdde2004-05-16 11:15:36 +000070*/
71void sqlite3TableAffinityStr(Vdbe *v, Table *pTab){
danielk19773d1bfea2004-05-14 11:00:53 +000072 /* 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
danielk1977a37cdde2004-05-16 11:15:36 +000083 zColAff = (char *)sqliteMalloc(pTab->nCol+1);
danielk19773d1bfea2004-05-14 11:00:53 +000084 if( !zColAff ){
danielk1977a37cdde2004-05-16 11:15:36 +000085 return;
danielk19773d1bfea2004-05-14 11:00:53 +000086 }
87
88 for(i=0; i<pTab->nCol; i++){
danielk1977a37cdde2004-05-16 11:15:36 +000089 zColAff[i] = pTab->aCol[i].affinity;
danielk19773d1bfea2004-05-14 11:00:53 +000090 }
91 zColAff[pTab->nCol] = '\0';
92
93 pTab->zColAff = zColAff;
94 }
95
drh956bc922004-07-24 17:38:29 +000096 sqlite3VdbeChangeP3(v, -1, pTab->zColAff, 0);
danielk19773d1bfea2004-05-14 11:00:53 +000097}
98
danielk19774d887782005-02-08 08:42:27 +000099/*
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*/
danielk1977e501b892006-01-09 06:29:47 +0000107static int selectReadsTable(Select *p, Schema *pSchema, int iTab){
danielk19774d887782005-02-08 08:42:27 +0000108 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 ){
danielk1977da184232006-01-05 11:34:32 +0000113 if( selectReadsTable(pItem->pSelect, pSchema, iTab) ) return 1;
danielk19774d887782005-02-08 08:42:27 +0000114 }else{
danielk1977da184232006-01-05 11:34:32 +0000115 if( pItem->pTab->pSchema==pSchema && pItem->pTab->tnum==iTab ) return 1;
danielk19774d887782005-02-08 08:42:27 +0000116 }
117 }
118 return 0;
119}
danielk19773d1bfea2004-05-14 11:00:53 +0000120
drh9d9cf222007-02-13 15:01:11 +0000121#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*/
137static 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*/
176static 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*/
187static 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, 0);
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 */
224static 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
danielk19773d1bfea2004-05-14 11:00:53 +0000232/*
drh1ccde152000-06-17 13:12:39 +0000233** This routine is call to handle SQL of the following forms:
drhcce7d172000-05-31 15:34:51 +0000234**
235** insert into TABLE (IDLIST) values(EXPRLIST)
drh1ccde152000-06-17 13:12:39 +0000236** insert into TABLE (IDLIST) select
drhcce7d172000-05-31 15:34:51 +0000237**
drh1ccde152000-06-17 13:12:39 +0000238** 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
drh967e8b72000-06-21 13:59:10 +0000240** appears in the pColumn parameter. pColumn is NULL if IDLIST is omitted.
drh1ccde152000-06-17 13:12:39 +0000241**
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.
drh142e30d2002-08-28 03:00:58 +0000246**
drh9d9cf222007-02-13 15:01:11 +0000247** The code generated follows one of four templates. For a simple
drh142e30d2002-08-28 03:00:58 +0000248** 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**
drh9d9cf222007-02-13 15:01:11 +0000256** The three remaining templates assume the statement is of the form
drh142e30d2002-08-28 03:00:58 +0000257**
258** INSERT INTO <table> SELECT ...
259**
drh9d9cf222007-02-13 15:01:11 +0000260** 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:
drh142e30d2002-08-28 03:00:58 +0000283**
284** goto B
285** A: setup for the SELECT
drh9d9cf222007-02-13 15:01:11 +0000286** loop over the rows in the SELECT
drh142e30d2002-08-28 03:00:58 +0000287** 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**
drh9d9cf222007-02-13 15:01:11 +0000297** The fourth template is used if the insert statement takes its
drh142e30d2002-08-28 03:00:58 +0000298** 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
drhcce7d172000-05-31 15:34:51 +0000319*/
danielk19774adee202004-05-08 08:23:19 +0000320void sqlite3Insert(
drhcce7d172000-05-31 15:34:51 +0000321 Parse *pParse, /* Parser context */
drh113088e2003-03-20 01:16:58 +0000322 SrcList *pTabList, /* Name of table into which we are inserting */
drhcce7d172000-05-31 15:34:51 +0000323 ExprList *pList, /* List of values to be inserted */
drh5974a302000-06-07 14:42:26 +0000324 Select *pSelect, /* A SELECT statement to use as the data source */
drh9cfcf5d2002-01-29 18:41:24 +0000325 IdList *pColumn, /* Column names corresponding to IDLIST. */
326 int onError /* How to handle constraint errors */
drhcce7d172000-05-31 15:34:51 +0000327){
drh5974a302000-06-07 14:42:26 +0000328 Table *pTab; /* The table to insert into */
drh113088e2003-03-20 01:16:58 +0000329 char *zTab; /* Name of the table into which we are inserting */
drhe22a3342003-04-22 20:30:37 +0000330 const char *zDb; /* Name of the database holding this table */
drh5974a302000-06-07 14:42:26 +0000331 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 */
drh967e8b72000-06-21 13:59:10 +0000334 int nColumn; /* Number of columns in the data */
danielk1977cfe9a692004-06-16 12:00:29 +0000335 int base = 0; /* VDBE Cursor number for pTab */
336 int iCont=0,iBreak=0; /* Beginning and end of the loop over srcTab */
drh9bb575f2004-09-06 17:24:11 +0000337 sqlite3 *db; /* The main database structure */
drh4a324312001-12-21 14:30:42 +0000338 int keyColumn = -1; /* Column that is the INTEGER PRIMARY KEY */
drh0ca3e242002-01-29 23:07:02 +0000339 int endOfLoop; /* Label for the end of the insertion loop */
danielk19774d887782005-02-08 08:42:27 +0000340 int useTempTable = 0; /* Store SELECT results in intermediate table */
danielk1977cfe9a692004-06-16 12:00:29 +0000341 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 */
drh798da522004-11-04 04:42:28 +0000346 int newIdx = -1; /* Cursor for the NEW table */
drh2958a4e2004-11-12 03:56:15 +0000347 Db *pDb; /* The database containing table being inserted into */
348 int counterMem = 0; /* Memory cell holding AUTOINCREMENT counter */
danielk1977da184232006-01-05 11:34:32 +0000349 int iDb;
drhcce7d172000-05-31 15:34:51 +0000350
drh798da522004-11-04 04:42:28 +0000351#ifndef SQLITE_OMIT_TRIGGER
352 int isView; /* True if attempting to insert into a view */
drhdca76842004-12-07 14:06:13 +0000353 int triggers_exist = 0; /* True if there are FOR EACH ROW triggers */
drh798da522004-11-04 04:42:28 +0000354#endif
danielk1977c3f9bad2002-05-15 08:30:12 +0000355
danielk19779e128002006-01-18 16:51:35 +0000356 if( pParse->nErr || sqlite3MallocFailed() ){
drh6f7adc82006-01-11 21:41:20 +0000357 goto insert_cleanup;
358 }
drhecdc7532001-09-23 02:35:53 +0000359 db = pParse->db;
drhdaffd0e2001-04-11 14:28:42 +0000360
drh1ccde152000-06-17 13:12:39 +0000361 /* Locate the table into which we will be inserting new information.
362 */
drh113088e2003-03-20 01:16:58 +0000363 assert( pTabList->nSrc==1 );
364 zTab = pTabList->a[0].zName;
drhdaffd0e2001-04-11 14:28:42 +0000365 if( zTab==0 ) goto insert_cleanup;
danielk19774adee202004-05-08 08:23:19 +0000366 pTab = sqlite3SrcListLookup(pParse, pTabList);
danielk1977c3f9bad2002-05-15 08:30:12 +0000367 if( pTab==0 ){
danielk1977c3f9bad2002-05-15 08:30:12 +0000368 goto insert_cleanup;
369 }
danielk1977da184232006-01-05 11:34:32 +0000370 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
371 assert( iDb<db->nDb );
372 pDb = &db->aDb[iDb];
drh2958a4e2004-11-12 03:56:15 +0000373 zDb = pDb->zName;
danielk19774adee202004-05-08 08:23:19 +0000374 if( sqlite3AuthCheck(pParse, SQLITE_INSERT, pTab->zName, 0, zDb) ){
drh1962bda2003-01-12 19:33:52 +0000375 goto insert_cleanup;
376 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000377
drhb7f91642004-10-31 02:22:47 +0000378 /* Figure out if we have any triggers and if the table being
379 ** inserted into is a view
380 */
381#ifndef SQLITE_OMIT_TRIGGER
drhdca76842004-12-07 14:06:13 +0000382 triggers_exist = sqlite3TriggersExist(pParse, pTab, TK_INSERT, 0);
drhb7f91642004-10-31 02:22:47 +0000383 isView = pTab->pSelect!=0;
384#else
drhdca76842004-12-07 14:06:13 +0000385# define triggers_exist 0
drhb7f91642004-10-31 02:22:47 +0000386# define isView 0
387#endif
388#ifdef SQLITE_OMIT_VIEW
389# undef isView
390# define isView 0
391#endif
392
danielk1977c3f9bad2002-05-15 08:30:12 +0000393 /* Ensure that:
394 * (a) the table is not read-only,
395 * (b) that if it is a view then ON INSERT triggers exist
396 */
drhdca76842004-12-07 14:06:13 +0000397 if( sqlite3IsReadOnly(pParse, pTab, triggers_exist) ){
danielk1977c3f9bad2002-05-15 08:30:12 +0000398 goto insert_cleanup;
399 }
drh43617e92006-03-06 20:55:46 +0000400 assert( pTab!=0 );
drh1ccde152000-06-17 13:12:39 +0000401
drhf573c992002-07-31 00:32:50 +0000402 /* If pTab is really a view, make sure it has been initialized.
danielk1977b3d24bf2006-06-19 03:05:10 +0000403 ** ViewGetColumnNames() is a no-op if pTab is not a view (or virtual
404 ** module table).
drhf573c992002-07-31 00:32:50 +0000405 */
danielk1977b3d24bf2006-06-19 03:05:10 +0000406 if( sqlite3ViewGetColumnNames(pParse, pTab) ){
drh5cf590c2003-04-24 01:45:04 +0000407 goto insert_cleanup;
drhf573c992002-07-31 00:32:50 +0000408 }
409
drh1ccde152000-06-17 13:12:39 +0000410 /* Allocate a VDBE
411 */
danielk19774adee202004-05-08 08:23:19 +0000412 v = sqlite3GetVdbe(pParse);
drh5974a302000-06-07 14:42:26 +0000413 if( v==0 ) goto insert_cleanup;
drh4794f732004-11-05 17:17:50 +0000414 if( pParse->nested==0 ) sqlite3VdbeCountChanges(v);
danielk1977da184232006-01-05 11:34:32 +0000415 sqlite3BeginWriteOperation(pParse, pSelect || triggers_exist, iDb);
drh1ccde152000-06-17 13:12:39 +0000416
danielk1977c3f9bad2002-05-15 08:30:12 +0000417 /* if there are row triggers, allocate a temp table for new.* references. */
drhdca76842004-12-07 14:06:13 +0000418 if( triggers_exist ){
danielk1977c3f9bad2002-05-15 08:30:12 +0000419 newIdx = pParse->nTab++;
danielk1977f29ce552002-05-19 23:43:12 +0000420 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000421
drh9d9cf222007-02-13 15:01:11 +0000422#ifndef SQLITE_OMIT_XFER_OPT
423 /* If the statement is of the form
424 **
425 ** INSERT INTO <table1> SELECT * FROM <table2>;
426 **
427 ** Then special optimizations can be applied that make the transfer
428 ** very fast and which reduce fragmentation of indices.
429 */
430 if( pColumn==0 && xferOptimization(pParse, pTab, pSelect, onError, iDb) ){
431 assert( !triggers_exist );
432 assert( pList==0 );
433 goto insert_cleanup;
434 }
435#endif /* SQLITE_OMIT_XFER_OPT */
436
drh2958a4e2004-11-12 03:56:15 +0000437 /* If this is an AUTOINCREMENT table, look up the sequence number in the
drhf3388142004-11-13 03:48:06 +0000438 ** sqlite_sequence table and store it in memory cell counterMem. Also
439 ** remember the rowid of the sqlite_sequence table entry in memory cell
440 ** counterRowid.
drh2958a4e2004-11-12 03:56:15 +0000441 */
drh9d9cf222007-02-13 15:01:11 +0000442 counterMem = autoIncBegin(pParse, iDb, pTab);
drh2958a4e2004-11-12 03:56:15 +0000443
drh1ccde152000-06-17 13:12:39 +0000444 /* Figure out how many columns of data are supplied. If the data
drh142e30d2002-08-28 03:00:58 +0000445 ** is coming from a SELECT statement, then this step also generates
446 ** all the code to implement the SELECT statement and invoke a subroutine
447 ** to process each row of the result. (Template 2.) If the SELECT
448 ** statement uses the the table that is being inserted into, then the
449 ** subroutine is also coded here. That subroutine stores the SELECT
450 ** results in a temporary table. (Template 3.)
drh1ccde152000-06-17 13:12:39 +0000451 */
drh5974a302000-06-07 14:42:26 +0000452 if( pSelect ){
drh142e30d2002-08-28 03:00:58 +0000453 /* Data is coming from a SELECT. Generate code to implement that SELECT
454 */
455 int rc, iInitCode;
danielk19774adee202004-05-08 08:23:19 +0000456 iInitCode = sqlite3VdbeAddOp(v, OP_Goto, 0, 0);
457 iSelectLoop = sqlite3VdbeCurrentAddr(v);
458 iInsertBlock = sqlite3VdbeMakeLabel(v);
danielk1977b3bce662005-01-29 08:32:43 +0000459
460 /* Resolve the expressions in the SELECT statement and execute it. */
461 rc = sqlite3Select(pParse, pSelect, SRT_Subroutine, iInsertBlock,0,0,0,0);
danielk19779e128002006-01-18 16:51:35 +0000462 if( rc || pParse->nErr || sqlite3MallocFailed() ){
drh6f7adc82006-01-11 21:41:20 +0000463 goto insert_cleanup;
464 }
danielk1977b3bce662005-01-29 08:32:43 +0000465
danielk19774adee202004-05-08 08:23:19 +0000466 iCleanup = sqlite3VdbeMakeLabel(v);
467 sqlite3VdbeAddOp(v, OP_Goto, 0, iCleanup);
drh5974a302000-06-07 14:42:26 +0000468 assert( pSelect->pEList );
drh967e8b72000-06-21 13:59:10 +0000469 nColumn = pSelect->pEList->nExpr;
drh142e30d2002-08-28 03:00:58 +0000470
471 /* Set useTempTable to TRUE if the result of the SELECT statement
472 ** should be written into a temporary table. Set to FALSE if each
473 ** row of the SELECT can be written directly into the result table.
drh048c5302003-04-03 01:50:44 +0000474 **
475 ** A temp table must be used if the table being updated is also one
476 ** of the tables being read by the SELECT statement. Also use a
477 ** temp table in the case of row triggers.
drh142e30d2002-08-28 03:00:58 +0000478 */
danielk1977da184232006-01-05 11:34:32 +0000479 if( triggers_exist || selectReadsTable(pSelect,pTab->pSchema,pTab->tnum) ){
drh048c5302003-04-03 01:50:44 +0000480 useTempTable = 1;
drh048c5302003-04-03 01:50:44 +0000481 }
drh142e30d2002-08-28 03:00:58 +0000482
483 if( useTempTable ){
484 /* Generate the subroutine that SELECT calls to process each row of
485 ** the result. Store the result in a temporary table
486 */
487 srcTab = pParse->nTab++;
danielk19774adee202004-05-08 08:23:19 +0000488 sqlite3VdbeResolveLabel(v, iInsertBlock);
489 sqlite3VdbeAddOp(v, OP_MakeRecord, nColumn, 0);
drhf0863fe2005-06-12 21:35:51 +0000490 sqlite3VdbeAddOp(v, OP_NewRowid, srcTab, 0);
danielk19774adee202004-05-08 08:23:19 +0000491 sqlite3VdbeAddOp(v, OP_Pull, 1, 0);
drhf0863fe2005-06-12 21:35:51 +0000492 sqlite3VdbeAddOp(v, OP_Insert, srcTab, 0);
danielk19774adee202004-05-08 08:23:19 +0000493 sqlite3VdbeAddOp(v, OP_Return, 0, 0);
drh142e30d2002-08-28 03:00:58 +0000494
495 /* The following code runs first because the GOTO at the very top
496 ** of the program jumps to it. Create the temporary table, then jump
497 ** back up and execute the SELECT code above.
498 */
drhd654be82005-09-20 17:42:23 +0000499 sqlite3VdbeJumpHere(v, iInitCode);
drhb9bb7c12006-06-11 23:41:55 +0000500 sqlite3VdbeAddOp(v, OP_OpenEphemeral, srcTab, 0);
drhb6f54522004-05-20 02:42:16 +0000501 sqlite3VdbeAddOp(v, OP_SetNumColumns, srcTab, nColumn);
danielk19774adee202004-05-08 08:23:19 +0000502 sqlite3VdbeAddOp(v, OP_Goto, 0, iSelectLoop);
503 sqlite3VdbeResolveLabel(v, iCleanup);
drh142e30d2002-08-28 03:00:58 +0000504 }else{
drhd654be82005-09-20 17:42:23 +0000505 sqlite3VdbeJumpHere(v, iInitCode);
drh142e30d2002-08-28 03:00:58 +0000506 }
drh5974a302000-06-07 14:42:26 +0000507 }else{
drh142e30d2002-08-28 03:00:58 +0000508 /* This is the case if the data for the INSERT is coming from a VALUES
509 ** clause
510 */
danielk1977b3bce662005-01-29 08:32:43 +0000511 NameContext sNC;
512 memset(&sNC, 0, sizeof(sNC));
513 sNC.pParse = pParse;
drh5974a302000-06-07 14:42:26 +0000514 srcTab = -1;
drh142e30d2002-08-28 03:00:58 +0000515 useTempTable = 0;
drh147d0cc2006-08-25 23:42:53 +0000516 nColumn = pList ? pList->nExpr : 0;
drhe64e7b22002-02-18 13:56:36 +0000517 for(i=0; i<nColumn; i++){
danielk1977b3bce662005-01-29 08:32:43 +0000518 if( sqlite3ExprResolveNames(&sNC, pList->a[i].pExpr) ){
drhb04a5d82002-04-12 03:55:15 +0000519 goto insert_cleanup;
520 }
drhe64e7b22002-02-18 13:56:36 +0000521 }
drh5974a302000-06-07 14:42:26 +0000522 }
drh1ccde152000-06-17 13:12:39 +0000523
524 /* Make sure the number of columns in the source data matches the number
525 ** of columns to be inserted into the table.
526 */
drh147d0cc2006-08-25 23:42:53 +0000527 if( pColumn==0 && nColumn && nColumn!=pTab->nCol ){
danielk19774adee202004-05-08 08:23:19 +0000528 sqlite3ErrorMsg(pParse,
drhda93d232003-03-31 02:12:46 +0000529 "table %S has %d columns but %d values were supplied",
530 pTabList, 0, pTab->nCol, nColumn);
drhcce7d172000-05-31 15:34:51 +0000531 goto insert_cleanup;
532 }
drh967e8b72000-06-21 13:59:10 +0000533 if( pColumn!=0 && nColumn!=pColumn->nId ){
danielk19774adee202004-05-08 08:23:19 +0000534 sqlite3ErrorMsg(pParse, "%d values for %d columns", nColumn, pColumn->nId);
drhcce7d172000-05-31 15:34:51 +0000535 goto insert_cleanup;
536 }
drh1ccde152000-06-17 13:12:39 +0000537
538 /* If the INSERT statement included an IDLIST term, then make sure
539 ** all elements of the IDLIST really are columns of the table and
540 ** remember the column indices.
drhc8392582001-12-31 02:48:51 +0000541 **
542 ** If the table has an INTEGER PRIMARY KEY column and that column
543 ** is named in the IDLIST, then record in the keyColumn variable
544 ** the index into IDLIST of the primary key column. keyColumn is
545 ** the index of the primary key as it appears in IDLIST, not as
546 ** is appears in the original table. (The index of the primary
547 ** key in the original table is pTab->iPKey.)
drh1ccde152000-06-17 13:12:39 +0000548 */
drh967e8b72000-06-21 13:59:10 +0000549 if( pColumn ){
550 for(i=0; i<pColumn->nId; i++){
551 pColumn->a[i].idx = -1;
drhcce7d172000-05-31 15:34:51 +0000552 }
drh967e8b72000-06-21 13:59:10 +0000553 for(i=0; i<pColumn->nId; i++){
drhcce7d172000-05-31 15:34:51 +0000554 for(j=0; j<pTab->nCol; j++){
danielk19774adee202004-05-08 08:23:19 +0000555 if( sqlite3StrICmp(pColumn->a[i].zName, pTab->aCol[j].zName)==0 ){
drh967e8b72000-06-21 13:59:10 +0000556 pColumn->a[i].idx = j;
drh4a324312001-12-21 14:30:42 +0000557 if( j==pTab->iPKey ){
drh9aa028d2001-12-22 21:48:29 +0000558 keyColumn = i;
drh4a324312001-12-21 14:30:42 +0000559 }
drhcce7d172000-05-31 15:34:51 +0000560 break;
561 }
562 }
563 if( j>=pTab->nCol ){
danielk19774adee202004-05-08 08:23:19 +0000564 if( sqlite3IsRowid(pColumn->a[i].zName) ){
drha0217ba2003-06-01 01:10:33 +0000565 keyColumn = i;
566 }else{
danielk19774adee202004-05-08 08:23:19 +0000567 sqlite3ErrorMsg(pParse, "table %S has no column named %s",
drha0217ba2003-06-01 01:10:33 +0000568 pTabList, 0, pColumn->a[i].zName);
569 pParse->nErr++;
570 goto insert_cleanup;
571 }
drhcce7d172000-05-31 15:34:51 +0000572 }
573 }
574 }
drh1ccde152000-06-17 13:12:39 +0000575
drhaacc5432002-01-06 17:07:40 +0000576 /* If there is no IDLIST term but the table has an integer primary
drhc8392582001-12-31 02:48:51 +0000577 ** key, the set the keyColumn variable to the primary key column index
578 ** in the original table definition.
drh4a324312001-12-21 14:30:42 +0000579 */
drh147d0cc2006-08-25 23:42:53 +0000580 if( pColumn==0 && nColumn>0 ){
drh4a324312001-12-21 14:30:42 +0000581 keyColumn = pTab->iPKey;
582 }
583
drh142e30d2002-08-28 03:00:58 +0000584 /* Open the temp table for FOR EACH ROW triggers
585 */
drhdca76842004-12-07 14:06:13 +0000586 if( triggers_exist ){
danielk19774adee202004-05-08 08:23:19 +0000587 sqlite3VdbeAddOp(v, OP_OpenPseudo, newIdx, 0);
danielk197784ac9d02004-05-18 09:58:06 +0000588 sqlite3VdbeAddOp(v, OP_SetNumColumns, newIdx, pTab->nCol);
danielk1977f29ce552002-05-19 23:43:12 +0000589 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000590
drhfeeb1392002-04-09 03:28:01 +0000591 /* Initialize the count of rows to be inserted
592 */
drh142e30d2002-08-28 03:00:58 +0000593 if( db->flags & SQLITE_CountRows ){
594 iCntMem = pParse->nMem++;
drhd654be82005-09-20 17:42:23 +0000595 sqlite3VdbeAddOp(v, OP_MemInt, 0, iCntMem);
drhfeeb1392002-04-09 03:28:01 +0000596 }
597
danielk1977c3f9bad2002-05-15 08:30:12 +0000598 /* Open tables and indices if there are no row triggers */
drhdca76842004-12-07 14:06:13 +0000599 if( !triggers_exist ){
danielk1977c3f9bad2002-05-15 08:30:12 +0000600 base = pParse->nTab;
drh290c1942004-08-21 17:54:45 +0000601 sqlite3OpenTableAndIndices(pParse, pTab, base, OP_OpenWrite);
danielk1977c3f9bad2002-05-15 08:30:12 +0000602 }
603
drh142e30d2002-08-28 03:00:58 +0000604 /* If the data source is a temporary table, then we have to create
drh1ccde152000-06-17 13:12:39 +0000605 ** a loop because there might be multiple rows of data. If the data
drh142e30d2002-08-28 03:00:58 +0000606 ** source is a subroutine call from the SELECT statement, then we need
607 ** to launch the SELECT statement processing.
drh1ccde152000-06-17 13:12:39 +0000608 */
drh142e30d2002-08-28 03:00:58 +0000609 if( useTempTable ){
danielk19774adee202004-05-08 08:23:19 +0000610 iBreak = sqlite3VdbeMakeLabel(v);
611 sqlite3VdbeAddOp(v, OP_Rewind, srcTab, iBreak);
612 iCont = sqlite3VdbeCurrentAddr(v);
drh142e30d2002-08-28 03:00:58 +0000613 }else if( pSelect ){
danielk19774adee202004-05-08 08:23:19 +0000614 sqlite3VdbeAddOp(v, OP_Goto, 0, iSelectLoop);
615 sqlite3VdbeResolveLabel(v, iInsertBlock);
drh5974a302000-06-07 14:42:26 +0000616 }
drh1ccde152000-06-17 13:12:39 +0000617
drh5cf590c2003-04-24 01:45:04 +0000618 /* Run the BEFORE and INSTEAD OF triggers, if there are any
drh70ce3f02003-04-15 19:22:22 +0000619 */
danielk19774adee202004-05-08 08:23:19 +0000620 endOfLoop = sqlite3VdbeMakeLabel(v);
drhdca76842004-12-07 14:06:13 +0000621 if( triggers_exist & TRIGGER_BEFORE ){
danielk1977c3f9bad2002-05-15 08:30:12 +0000622
drh70ce3f02003-04-15 19:22:22 +0000623 /* build the NEW.* reference row. Note that if there is an INTEGER
624 ** PRIMARY KEY into which a NULL is being inserted, that NULL will be
625 ** translated into a unique ID for the row. But on a BEFORE trigger,
626 ** we do not know what the unique ID will be (because the insert has
627 ** not happened yet) so we substitute a rowid of -1
628 */
629 if( keyColumn<0 ){
danielk19774adee202004-05-08 08:23:19 +0000630 sqlite3VdbeAddOp(v, OP_Integer, -1, 0);
drh70ce3f02003-04-15 19:22:22 +0000631 }else if( useTempTable ){
danielk19774adee202004-05-08 08:23:19 +0000632 sqlite3VdbeAddOp(v, OP_Column, srcTab, keyColumn);
drh70ce3f02003-04-15 19:22:22 +0000633 }else{
drhd6fe9612005-01-14 01:22:00 +0000634 assert( pSelect==0 ); /* Otherwise useTempTable is true */
danielk19774adee202004-05-08 08:23:19 +0000635 sqlite3ExprCode(pParse, pList->a[keyColumn].pExpr);
636 sqlite3VdbeAddOp(v, OP_NotNull, -1, sqlite3VdbeCurrentAddr(v)+3);
637 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
638 sqlite3VdbeAddOp(v, OP_Integer, -1, 0);
639 sqlite3VdbeAddOp(v, OP_MustBeInt, 0, 0);
drh70ce3f02003-04-15 19:22:22 +0000640 }
641
642 /* Create the new column data
643 */
danielk1977c3f9bad2002-05-15 08:30:12 +0000644 for(i=0; i<pTab->nCol; i++){
645 if( pColumn==0 ){
drh9adf9ac2002-05-15 11:44:13 +0000646 j = i;
danielk1977c3f9bad2002-05-15 08:30:12 +0000647 }else{
drh9adf9ac2002-05-15 11:44:13 +0000648 for(j=0; j<pColumn->nId; j++){
649 if( pColumn->a[j].idx==i ) break;
650 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000651 }
652 if( pColumn && j>=pColumn->nId ){
danielk19777977a172004-11-09 12:44:37 +0000653 sqlite3ExprCode(pParse, pTab->aCol[i].pDflt);
drh142e30d2002-08-28 03:00:58 +0000654 }else if( useTempTable ){
danielk19774adee202004-05-08 08:23:19 +0000655 sqlite3VdbeAddOp(v, OP_Column, srcTab, j);
danielk1977c3f9bad2002-05-15 08:30:12 +0000656 }else{
drhd6fe9612005-01-14 01:22:00 +0000657 assert( pSelect==0 ); /* Otherwise useTempTable is true */
drh25303782004-12-07 15:41:48 +0000658 sqlite3ExprCodeAndCache(pParse, pList->a[j].pExpr);
danielk1977c3f9bad2002-05-15 08:30:12 +0000659 }
660 }
danielk19774adee202004-05-08 08:23:19 +0000661 sqlite3VdbeAddOp(v, OP_MakeRecord, pTab->nCol, 0);
danielk1977a37cdde2004-05-16 11:15:36 +0000662
663 /* If this is an INSERT on a view with an INSTEAD OF INSERT trigger,
664 ** do not attempt any conversions before assembling the record.
665 ** If this is a real table, attempt conversions as required by the
666 ** table column affinities.
667 */
668 if( !isView ){
669 sqlite3TableAffinityStr(v, pTab);
670 }
drhf0863fe2005-06-12 21:35:51 +0000671 sqlite3VdbeAddOp(v, OP_Insert, newIdx, 0);
danielk1977c3f9bad2002-05-15 08:30:12 +0000672
drh5cf590c2003-04-24 01:45:04 +0000673 /* Fire BEFORE or INSTEAD OF triggers */
drhdca76842004-12-07 14:06:13 +0000674 if( sqlite3CodeRowTrigger(pParse, TK_INSERT, 0, TRIGGER_BEFORE, pTab,
drhb2fe7d82003-04-20 17:29:23 +0000675 newIdx, -1, onError, endOfLoop) ){
danielk1977f29ce552002-05-19 23:43:12 +0000676 goto insert_cleanup;
677 }
drh70ce3f02003-04-15 19:22:22 +0000678 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000679
drh70ce3f02003-04-15 19:22:22 +0000680 /* If any triggers exists, the opening of tables and indices is deferred
681 ** until now.
682 */
drhdca76842004-12-07 14:06:13 +0000683 if( triggers_exist && !isView ){
drh5cf590c2003-04-24 01:45:04 +0000684 base = pParse->nTab;
drh290c1942004-08-21 17:54:45 +0000685 sqlite3OpenTableAndIndices(pParse, pTab, base, OP_OpenWrite);
danielk1977c3f9bad2002-05-15 08:30:12 +0000686 }
687
drh4a324312001-12-21 14:30:42 +0000688 /* Push the record number for the new entry onto the stack. The
drhf0863fe2005-06-12 21:35:51 +0000689 ** record number is a randomly generate integer created by NewRowid
drh4a324312001-12-21 14:30:42 +0000690 ** except when the table has an INTEGER PRIMARY KEY column, in which
drhb419a922002-01-30 16:17:23 +0000691 ** case the record number is the same as that column.
drh1ccde152000-06-17 13:12:39 +0000692 */
drh5cf590c2003-04-24 01:45:04 +0000693 if( !isView ){
drh4cbdda92006-06-14 19:00:20 +0000694 if( IsVirtual(pTab) ){
695 /* The row that the VUpdate opcode will delete: none */
696 sqlite3VdbeAddOp(v, OP_Null, 0, 0);
697 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000698 if( keyColumn>=0 ){
drh142e30d2002-08-28 03:00:58 +0000699 if( useTempTable ){
danielk19774adee202004-05-08 08:23:19 +0000700 sqlite3VdbeAddOp(v, OP_Column, srcTab, keyColumn);
drh142e30d2002-08-28 03:00:58 +0000701 }else if( pSelect ){
danielk19774adee202004-05-08 08:23:19 +0000702 sqlite3VdbeAddOp(v, OP_Dup, nColumn - keyColumn - 1, 1);
danielk1977c3f9bad2002-05-15 08:30:12 +0000703 }else{
danielk19774adee202004-05-08 08:23:19 +0000704 sqlite3ExprCode(pParse, pList->a[keyColumn].pExpr);
danielk1977c3f9bad2002-05-15 08:30:12 +0000705 }
drhf0863fe2005-06-12 21:35:51 +0000706 /* If the PRIMARY KEY expression is NULL, then use OP_NewRowid
drh27a32782002-06-19 20:32:43 +0000707 ** to generate a unique primary key value.
708 */
danielk19774adee202004-05-08 08:23:19 +0000709 sqlite3VdbeAddOp(v, OP_NotNull, -1, sqlite3VdbeCurrentAddr(v)+3);
710 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
drhf0863fe2005-06-12 21:35:51 +0000711 sqlite3VdbeAddOp(v, OP_NewRowid, base, counterMem);
danielk19774adee202004-05-08 08:23:19 +0000712 sqlite3VdbeAddOp(v, OP_MustBeInt, 0, 0);
drh4cbdda92006-06-14 19:00:20 +0000713 }else if( IsVirtual(pTab) ){
714 sqlite3VdbeAddOp(v, OP_Null, 0, 0);
danielk1977c3f9bad2002-05-15 08:30:12 +0000715 }else{
drhf0863fe2005-06-12 21:35:51 +0000716 sqlite3VdbeAddOp(v, OP_NewRowid, base, counterMem);
drh4a324312001-12-21 14:30:42 +0000717 }
drh9d9cf222007-02-13 15:01:11 +0000718 autoIncStep(pParse, counterMem);
drh4a324312001-12-21 14:30:42 +0000719
danielk1977c3f9bad2002-05-15 08:30:12 +0000720 /* Push onto the stack, data for all columns of the new entry, beginning
danielk1977f29ce552002-05-19 23:43:12 +0000721 ** with the first column.
722 */
danielk1977c3f9bad2002-05-15 08:30:12 +0000723 for(i=0; i<pTab->nCol; i++){
724 if( i==pTab->iPKey ){
drh9adf9ac2002-05-15 11:44:13 +0000725 /* The value of the INTEGER PRIMARY KEY column is always a NULL.
danielk1977f29ce552002-05-19 23:43:12 +0000726 ** Whenever this column is read, the record number will be substituted
727 ** in its place. So will fill this column with a NULL to avoid
728 ** taking up data space with information that will never be used. */
drhf0863fe2005-06-12 21:35:51 +0000729 sqlite3VdbeAddOp(v, OP_Null, 0, 0);
drh9adf9ac2002-05-15 11:44:13 +0000730 continue;
danielk1977c3f9bad2002-05-15 08:30:12 +0000731 }
732 if( pColumn==0 ){
drh9adf9ac2002-05-15 11:44:13 +0000733 j = i;
danielk1977c3f9bad2002-05-15 08:30:12 +0000734 }else{
drh9adf9ac2002-05-15 11:44:13 +0000735 for(j=0; j<pColumn->nId; j++){
736 if( pColumn->a[j].idx==i ) break;
737 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000738 }
drh147d0cc2006-08-25 23:42:53 +0000739 if( nColumn==0 || (pColumn && j>=pColumn->nId) ){
danielk19777977a172004-11-09 12:44:37 +0000740 sqlite3ExprCode(pParse, pTab->aCol[i].pDflt);
drh142e30d2002-08-28 03:00:58 +0000741 }else if( useTempTable ){
danielk19774adee202004-05-08 08:23:19 +0000742 sqlite3VdbeAddOp(v, OP_Column, srcTab, j);
drh142e30d2002-08-28 03:00:58 +0000743 }else if( pSelect ){
drh16ed8a62006-08-29 18:46:14 +0000744 sqlite3VdbeAddOp(v, OP_Dup, i+nColumn-j+IsVirtual(pTab), 1);
danielk1977c3f9bad2002-05-15 08:30:12 +0000745 }else{
danielk19774adee202004-05-08 08:23:19 +0000746 sqlite3ExprCode(pParse, pList->a[j].pExpr);
drh5974a302000-06-07 14:42:26 +0000747 }
drhbed86902000-06-02 13:27:59 +0000748 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000749
750 /* Generate code to check constraints and generate index keys and
danielk1977f29ce552002-05-19 23:43:12 +0000751 ** do the insertion.
752 */
drh4cbdda92006-06-14 19:00:20 +0000753#ifndef SQLITE_OMIT_VIRTUALTABLE
754 if( IsVirtual(pTab) ){
danielk1977f9e7dda2006-06-16 16:08:53 +0000755 pParse->pVirtualLock = pTab;
danielk19771f6eec52006-06-16 06:17:47 +0000756 sqlite3VdbeOp3(v, OP_VUpdate, 1, pTab->nCol+2,
drh4cbdda92006-06-14 19:00:20 +0000757 (const char*)pTab->pVtab, P3_VTAB);
758 }else
759#endif
760 {
761 sqlite3GenerateConstraintChecks(pParse, pTab, base, 0, keyColumn>=0,
762 0, onError, endOfLoop);
763 sqlite3CompleteInsertion(pParse, pTab, base, 0,0,0,
drhdca76842004-12-07 14:06:13 +0000764 (triggers_exist & TRIGGER_AFTER)!=0 ? newIdx : -1);
drh4cbdda92006-06-14 19:00:20 +0000765 }
drh5cf590c2003-04-24 01:45:04 +0000766 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000767
drh5cf590c2003-04-24 01:45:04 +0000768 /* Update the count of rows that are inserted
769 */
770 if( (db->flags & SQLITE_CountRows)!=0 ){
drh15007a92006-01-08 18:10:17 +0000771 sqlite3VdbeAddOp(v, OP_MemIncr, 1, iCntMem);
drh5974a302000-06-07 14:42:26 +0000772 }
drh1ccde152000-06-17 13:12:39 +0000773
drhdca76842004-12-07 14:06:13 +0000774 if( triggers_exist ){
danielk1977c3f9bad2002-05-15 08:30:12 +0000775 /* Close all tables opened */
drh5cf590c2003-04-24 01:45:04 +0000776 if( !isView ){
danielk19774adee202004-05-08 08:23:19 +0000777 sqlite3VdbeAddOp(v, OP_Close, base, 0);
danielk1977c3f9bad2002-05-15 08:30:12 +0000778 for(idx=1, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, idx++){
danielk19774adee202004-05-08 08:23:19 +0000779 sqlite3VdbeAddOp(v, OP_Close, idx+base, 0);
danielk1977c3f9bad2002-05-15 08:30:12 +0000780 }
781 }
drh1bee3d72001-10-15 00:44:35 +0000782
danielk1977c3f9bad2002-05-15 08:30:12 +0000783 /* Code AFTER triggers */
drhdca76842004-12-07 14:06:13 +0000784 if( sqlite3CodeRowTrigger(pParse, TK_INSERT, 0, TRIGGER_AFTER, pTab,
785 newIdx, -1, onError, endOfLoop) ){
danielk1977f29ce552002-05-19 23:43:12 +0000786 goto insert_cleanup;
787 }
drh1bee3d72001-10-15 00:44:35 +0000788 }
789
drh1ccde152000-06-17 13:12:39 +0000790 /* The bottom of the loop, if the data source is a SELECT statement
danielk1977f29ce552002-05-19 23:43:12 +0000791 */
danielk19774adee202004-05-08 08:23:19 +0000792 sqlite3VdbeResolveLabel(v, endOfLoop);
drh142e30d2002-08-28 03:00:58 +0000793 if( useTempTable ){
danielk19774adee202004-05-08 08:23:19 +0000794 sqlite3VdbeAddOp(v, OP_Next, srcTab, iCont);
795 sqlite3VdbeResolveLabel(v, iBreak);
796 sqlite3VdbeAddOp(v, OP_Close, srcTab, 0);
drh142e30d2002-08-28 03:00:58 +0000797 }else if( pSelect ){
danielk19774adee202004-05-08 08:23:19 +0000798 sqlite3VdbeAddOp(v, OP_Pop, nColumn, 0);
799 sqlite3VdbeAddOp(v, OP_Return, 0, 0);
800 sqlite3VdbeResolveLabel(v, iCleanup);
drh6b563442001-11-07 16:48:26 +0000801 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000802
drh4cbdda92006-06-14 19:00:20 +0000803 if( !triggers_exist && !IsVirtual(pTab) ){
danielk1977c3f9bad2002-05-15 08:30:12 +0000804 /* Close all tables opened */
danielk19774adee202004-05-08 08:23:19 +0000805 sqlite3VdbeAddOp(v, OP_Close, base, 0);
danielk1977c3f9bad2002-05-15 08:30:12 +0000806 for(idx=1, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, idx++){
danielk19774adee202004-05-08 08:23:19 +0000807 sqlite3VdbeAddOp(v, OP_Close, idx+base, 0);
danielk1977c3f9bad2002-05-15 08:30:12 +0000808 }
drhcce7d172000-05-31 15:34:51 +0000809 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000810
drhf3388142004-11-13 03:48:06 +0000811 /* Update the sqlite_sequence table by storing the content of the
812 ** counter value in memory counterMem back into the sqlite_sequence
813 ** table.
drh2958a4e2004-11-12 03:56:15 +0000814 */
drh9d9cf222007-02-13 15:01:11 +0000815 autoIncEnd(pParse, iDb, pTab, counterMem);
drh2958a4e2004-11-12 03:56:15 +0000816
drh1bee3d72001-10-15 00:44:35 +0000817 /*
danielk1977e7de6f22004-11-05 06:02:06 +0000818 ** Return the number of rows inserted. If this routine is
819 ** generating code because of a call to sqlite3NestedParse(), do not
820 ** invoke the callback function.
drh1bee3d72001-10-15 00:44:35 +0000821 */
danielk1977cc6bd382005-01-10 02:48:49 +0000822 if( db->flags & SQLITE_CountRows && pParse->nested==0 && !pParse->trigStack ){
danielk19774adee202004-05-08 08:23:19 +0000823 sqlite3VdbeAddOp(v, OP_MemLoad, iCntMem, 0);
824 sqlite3VdbeAddOp(v, OP_Callback, 1, 0);
danielk197722322fd2004-05-25 23:35:17 +0000825 sqlite3VdbeSetNumCols(v, 1);
danielk1977955de522006-02-10 02:27:42 +0000826 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "rows inserted", P3_STATIC);
drh1bee3d72001-10-15 00:44:35 +0000827 }
drhcce7d172000-05-31 15:34:51 +0000828
829insert_cleanup:
danielk19774adee202004-05-08 08:23:19 +0000830 sqlite3SrcListDelete(pTabList);
danielk1977d5d56522005-03-16 12:15:20 +0000831 sqlite3ExprListDelete(pList);
832 sqlite3SelectDelete(pSelect);
danielk19774adee202004-05-08 08:23:19 +0000833 sqlite3IdListDelete(pColumn);
drhcce7d172000-05-31 15:34:51 +0000834}
drh9cfcf5d2002-01-29 18:41:24 +0000835
drh9cfcf5d2002-01-29 18:41:24 +0000836/*
837** Generate code to do a constraint check prior to an INSERT or an UPDATE.
838**
839** When this routine is called, the stack contains (from bottom to top)
drh0ca3e242002-01-29 23:07:02 +0000840** the following values:
841**
drhf0863fe2005-06-12 21:35:51 +0000842** 1. The rowid of the row to be updated before the update. This
drhb419a922002-01-30 16:17:23 +0000843** value is omitted unless we are doing an UPDATE that involves a
844** change to the record number.
drh0ca3e242002-01-29 23:07:02 +0000845**
drhf0863fe2005-06-12 21:35:51 +0000846** 2. The rowid of the row after the update.
drh0ca3e242002-01-29 23:07:02 +0000847**
848** 3. The data in the first column of the entry after the update.
849**
850** i. Data from middle columns...
851**
852** N. The data in the last column of the entry after the update.
853**
drhf0863fe2005-06-12 21:35:51 +0000854** The old rowid shown as entry (1) above is omitted unless both isUpdate
855** and rowidChng are 1. isUpdate is true for UPDATEs and false for
856** INSERTs and rowidChng is true if the record number is being changed.
drh0ca3e242002-01-29 23:07:02 +0000857**
858** The code generated by this routine pushes additional entries onto
859** the stack which are the keys for new index entries for the new record.
860** The order of index keys is the same as the order of the indices on
861** the pTable->pIndex list. A key is only created for index i if
862** aIdxUsed!=0 and aIdxUsed[i]!=0.
drh9cfcf5d2002-01-29 18:41:24 +0000863**
864** This routine also generates code to check constraints. NOT NULL,
865** CHECK, and UNIQUE constraints are all checked. If a constraint fails,
drh1c928532002-01-31 15:54:21 +0000866** then the appropriate action is performed. There are five possible
867** actions: ROLLBACK, ABORT, FAIL, REPLACE, and IGNORE.
drh9cfcf5d2002-01-29 18:41:24 +0000868**
869** Constraint type Action What Happens
870** --------------- ---------- ----------------------------------------
drh1c928532002-01-31 15:54:21 +0000871** any ROLLBACK The current transaction is rolled back and
danielk197724b03fd2004-05-10 10:34:34 +0000872** sqlite3_exec() returns immediately with a
drh9cfcf5d2002-01-29 18:41:24 +0000873** return code of SQLITE_CONSTRAINT.
874**
drh1c928532002-01-31 15:54:21 +0000875** any ABORT Back out changes from the current command
876** only (do not do a complete rollback) then
danielk197724b03fd2004-05-10 10:34:34 +0000877** cause sqlite3_exec() to return immediately
drh1c928532002-01-31 15:54:21 +0000878** with SQLITE_CONSTRAINT.
879**
880** any FAIL Sqlite_exec() returns immediately with a
881** return code of SQLITE_CONSTRAINT. The
882** transaction is not rolled back and any
883** prior changes are retained.
884**
drh9cfcf5d2002-01-29 18:41:24 +0000885** any IGNORE The record number and data is popped from
886** the stack and there is an immediate jump
887** to label ignoreDest.
888**
889** NOT NULL REPLACE The NULL value is replace by the default
890** value for that column. If the default value
891** is NULL, the action is the same as ABORT.
892**
893** UNIQUE REPLACE The other row that conflicts with the row
894** being inserted is removed.
895**
896** CHECK REPLACE Illegal. The results in an exception.
897**
drh1c928532002-01-31 15:54:21 +0000898** Which action to take is determined by the overrideError parameter.
899** Or if overrideError==OE_Default, then the pParse->onError parameter
900** is used. Or if pParse->onError==OE_Default then the onError value
901** for the constraint is used.
drh9cfcf5d2002-01-29 18:41:24 +0000902**
drhaaab5722002-02-19 13:39:21 +0000903** The calling routine must open a read/write cursor for pTab with
drh9cfcf5d2002-01-29 18:41:24 +0000904** cursor number "base". All indices of pTab must also have open
905** read/write cursors with cursor number base+i for the i-th cursor.
906** Except, if there is no possibility of a REPLACE action then
907** cursors do not need to be open for indices where aIdxUsed[i]==0.
908**
909** If the isUpdate flag is true, it means that the "base" cursor is
910** initially pointing to an entry that is being updated. The isUpdate
911** flag causes extra code to be generated so that the "base" cursor
912** is still pointing at the same entry after the routine returns.
913** Without the isUpdate flag, the "base" cursor might be moved.
914*/
danielk19774adee202004-05-08 08:23:19 +0000915void sqlite3GenerateConstraintChecks(
drh9cfcf5d2002-01-29 18:41:24 +0000916 Parse *pParse, /* The parser context */
917 Table *pTab, /* the table into which we are inserting */
918 int base, /* Index of a read/write cursor pointing at pTab */
919 char *aIdxUsed, /* Which indices are used. NULL means all are used */
drhf0863fe2005-06-12 21:35:51 +0000920 int rowidChng, /* True if the record number will change */
drhb419a922002-01-30 16:17:23 +0000921 int isUpdate, /* True for UPDATE, False for INSERT */
drh9cfcf5d2002-01-29 18:41:24 +0000922 int overrideError, /* Override onError to this if not OE_Default */
drhb419a922002-01-30 16:17:23 +0000923 int ignoreDest /* Jump to this label on an OE_Ignore resolution */
drh9cfcf5d2002-01-29 18:41:24 +0000924){
925 int i;
926 Vdbe *v;
927 int nCol;
928 int onError;
929 int addr;
930 int extra;
drh0ca3e242002-01-29 23:07:02 +0000931 int iCur;
932 Index *pIdx;
933 int seenReplace = 0;
danielk1977cfe9a692004-06-16 12:00:29 +0000934 int jumpInst1=0, jumpInst2;
drhf0863fe2005-06-12 21:35:51 +0000935 int hasTwoRowids = (isUpdate && rowidChng);
drh9cfcf5d2002-01-29 18:41:24 +0000936
danielk19774adee202004-05-08 08:23:19 +0000937 v = sqlite3GetVdbe(pParse);
drh9cfcf5d2002-01-29 18:41:24 +0000938 assert( v!=0 );
drh417be792002-03-03 18:59:40 +0000939 assert( pTab->pSelect==0 ); /* This table is not a VIEW */
drh9cfcf5d2002-01-29 18:41:24 +0000940 nCol = pTab->nCol;
941
942 /* Test all NOT NULL constraints.
943 */
944 for(i=0; i<nCol; i++){
drh0ca3e242002-01-29 23:07:02 +0000945 if( i==pTab->iPKey ){
drh0ca3e242002-01-29 23:07:02 +0000946 continue;
947 }
drh9cfcf5d2002-01-29 18:41:24 +0000948 onError = pTab->aCol[i].notNull;
drh0ca3e242002-01-29 23:07:02 +0000949 if( onError==OE_None ) continue;
drh9cfcf5d2002-01-29 18:41:24 +0000950 if( overrideError!=OE_Default ){
951 onError = overrideError;
drha996e472003-05-16 02:30:27 +0000952 }else if( onError==OE_Default ){
953 onError = OE_Abort;
drh9cfcf5d2002-01-29 18:41:24 +0000954 }
danielk19777977a172004-11-09 12:44:37 +0000955 if( onError==OE_Replace && pTab->aCol[i].pDflt==0 ){
drh9cfcf5d2002-01-29 18:41:24 +0000956 onError = OE_Abort;
957 }
danielk19774adee202004-05-08 08:23:19 +0000958 sqlite3VdbeAddOp(v, OP_Dup, nCol-1-i, 1);
959 addr = sqlite3VdbeAddOp(v, OP_NotNull, 1, 0);
danielk1977b84f96f2005-01-20 11:32:23 +0000960 assert( onError==OE_Rollback || onError==OE_Abort || onError==OE_Fail
961 || onError==OE_Ignore || onError==OE_Replace );
drh9cfcf5d2002-01-29 18:41:24 +0000962 switch( onError ){
drh1c928532002-01-31 15:54:21 +0000963 case OE_Rollback:
964 case OE_Abort:
965 case OE_Fail: {
drh483750b2003-01-29 18:46:51 +0000966 char *zMsg = 0;
danielk19774adee202004-05-08 08:23:19 +0000967 sqlite3VdbeAddOp(v, OP_Halt, SQLITE_CONSTRAINT, onError);
968 sqlite3SetString(&zMsg, pTab->zName, ".", pTab->aCol[i].zName,
drh41743982003-12-06 21:43:55 +0000969 " may not be NULL", (char*)0);
danielk19774adee202004-05-08 08:23:19 +0000970 sqlite3VdbeChangeP3(v, -1, zMsg, P3_DYNAMIC);
drh9cfcf5d2002-01-29 18:41:24 +0000971 break;
972 }
973 case OE_Ignore: {
drhf0863fe2005-06-12 21:35:51 +0000974 sqlite3VdbeAddOp(v, OP_Pop, nCol+1+hasTwoRowids, 0);
danielk19774adee202004-05-08 08:23:19 +0000975 sqlite3VdbeAddOp(v, OP_Goto, 0, ignoreDest);
drh9cfcf5d2002-01-29 18:41:24 +0000976 break;
977 }
978 case OE_Replace: {
danielk19777977a172004-11-09 12:44:37 +0000979 sqlite3ExprCode(pParse, pTab->aCol[i].pDflt);
danielk19774adee202004-05-08 08:23:19 +0000980 sqlite3VdbeAddOp(v, OP_Push, nCol-i, 0);
drh9cfcf5d2002-01-29 18:41:24 +0000981 break;
982 }
drh9cfcf5d2002-01-29 18:41:24 +0000983 }
drhd654be82005-09-20 17:42:23 +0000984 sqlite3VdbeJumpHere(v, addr);
drh9cfcf5d2002-01-29 18:41:24 +0000985 }
986
987 /* Test all CHECK constraints
988 */
drhffe07b22005-11-03 00:41:17 +0000989#ifndef SQLITE_OMIT_CHECK
drh0cd2d4c2005-11-03 02:15:02 +0000990 if( pTab->pCheck && (pParse->db->flags & SQLITE_IgnoreChecks)==0 ){
drhffe07b22005-11-03 00:41:17 +0000991 int allOk = sqlite3VdbeMakeLabel(v);
992 assert( pParse->ckOffset==0 );
993 pParse->ckOffset = nCol;
drh6275b882005-11-03 01:22:30 +0000994 sqlite3ExprIfTrue(pParse, pTab->pCheck, allOk, 1);
drhffe07b22005-11-03 00:41:17 +0000995 assert( pParse->ckOffset==nCol );
996 pParse->ckOffset = 0;
drhaa01c7e2006-03-15 16:26:10 +0000997 onError = overrideError!=OE_Default ? overrideError : OE_Abort;
998 if( onError==OE_Ignore || onError==OE_Replace ){
999 sqlite3VdbeAddOp(v, OP_Pop, nCol+1+hasTwoRowids, 0);
1000 sqlite3VdbeAddOp(v, OP_Goto, 0, ignoreDest);
1001 }else{
1002 sqlite3VdbeAddOp(v, OP_Halt, SQLITE_CONSTRAINT, onError);
1003 }
drhffe07b22005-11-03 00:41:17 +00001004 sqlite3VdbeResolveLabel(v, allOk);
1005 }
1006#endif /* !defined(SQLITE_OMIT_CHECK) */
drh9cfcf5d2002-01-29 18:41:24 +00001007
drh0bd1f4e2002-06-06 18:54:39 +00001008 /* If we have an INTEGER PRIMARY KEY, make sure the primary key
1009 ** of the new record does not previously exist. Except, if this
1010 ** is an UPDATE and the primary key is not changing, that is OK.
drh9cfcf5d2002-01-29 18:41:24 +00001011 */
drhf0863fe2005-06-12 21:35:51 +00001012 if( rowidChng ){
drh0ca3e242002-01-29 23:07:02 +00001013 onError = pTab->keyConf;
1014 if( overrideError!=OE_Default ){
1015 onError = overrideError;
drha996e472003-05-16 02:30:27 +00001016 }else if( onError==OE_Default ){
1017 onError = OE_Abort;
drh0ca3e242002-01-29 23:07:02 +00001018 }
drha0217ba2003-06-01 01:10:33 +00001019
drh5383ae52003-06-04 12:23:30 +00001020 if( isUpdate ){
danielk19774adee202004-05-08 08:23:19 +00001021 sqlite3VdbeAddOp(v, OP_Dup, nCol+1, 1);
1022 sqlite3VdbeAddOp(v, OP_Dup, nCol+1, 1);
1023 jumpInst1 = sqlite3VdbeAddOp(v, OP_Eq, 0, 0);
drh5383ae52003-06-04 12:23:30 +00001024 }
danielk19774adee202004-05-08 08:23:19 +00001025 sqlite3VdbeAddOp(v, OP_Dup, nCol, 1);
1026 jumpInst2 = sqlite3VdbeAddOp(v, OP_NotExists, base, 0);
drh5383ae52003-06-04 12:23:30 +00001027 switch( onError ){
1028 default: {
1029 onError = OE_Abort;
1030 /* Fall thru into the next case */
drh79b0c952002-05-21 12:56:43 +00001031 }
drh5383ae52003-06-04 12:23:30 +00001032 case OE_Rollback:
1033 case OE_Abort:
1034 case OE_Fail: {
danielk19774adee202004-05-08 08:23:19 +00001035 sqlite3VdbeOp3(v, OP_Halt, SQLITE_CONSTRAINT, onError,
drh701a0ae2004-02-22 20:05:00 +00001036 "PRIMARY KEY must be unique", P3_STATIC);
drh5383ae52003-06-04 12:23:30 +00001037 break;
drh0ca3e242002-01-29 23:07:02 +00001038 }
drh5383ae52003-06-04 12:23:30 +00001039 case OE_Replace: {
drh74161702006-02-24 02:53:49 +00001040 sqlite3GenerateRowIndexDelete(v, pTab, base, 0);
drh5383ae52003-06-04 12:23:30 +00001041 if( isUpdate ){
drhf0863fe2005-06-12 21:35:51 +00001042 sqlite3VdbeAddOp(v, OP_Dup, nCol+hasTwoRowids, 1);
drh7cf6e4d2004-05-19 14:56:55 +00001043 sqlite3VdbeAddOp(v, OP_MoveGe, base, 0);
drh5383ae52003-06-04 12:23:30 +00001044 }
1045 seenReplace = 1;
1046 break;
drh0ca3e242002-01-29 23:07:02 +00001047 }
drh5383ae52003-06-04 12:23:30 +00001048 case OE_Ignore: {
1049 assert( seenReplace==0 );
drhf0863fe2005-06-12 21:35:51 +00001050 sqlite3VdbeAddOp(v, OP_Pop, nCol+1+hasTwoRowids, 0);
danielk19774adee202004-05-08 08:23:19 +00001051 sqlite3VdbeAddOp(v, OP_Goto, 0, ignoreDest);
drh5383ae52003-06-04 12:23:30 +00001052 break;
1053 }
1054 }
drhd654be82005-09-20 17:42:23 +00001055 sqlite3VdbeJumpHere(v, jumpInst2);
drh5383ae52003-06-04 12:23:30 +00001056 if( isUpdate ){
drhd654be82005-09-20 17:42:23 +00001057 sqlite3VdbeJumpHere(v, jumpInst1);
danielk19774adee202004-05-08 08:23:19 +00001058 sqlite3VdbeAddOp(v, OP_Dup, nCol+1, 1);
drh7cf6e4d2004-05-19 14:56:55 +00001059 sqlite3VdbeAddOp(v, OP_MoveGe, base, 0);
drh0ca3e242002-01-29 23:07:02 +00001060 }
1061 }
drh0bd1f4e2002-06-06 18:54:39 +00001062
1063 /* Test all UNIQUE constraints by creating entries for each UNIQUE
1064 ** index and making sure that duplicate entries do not already exist.
1065 ** Add the new records to the indices as we go.
1066 */
drhb2fe7d82003-04-20 17:29:23 +00001067 extra = -1;
1068 for(iCur=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, iCur++){
1069 if( aIdxUsed && aIdxUsed[iCur]==0 ) continue; /* Skip unused indices */
1070 extra++;
1071
1072 /* Create a key for accessing the index entry */
danielk19774adee202004-05-08 08:23:19 +00001073 sqlite3VdbeAddOp(v, OP_Dup, nCol+extra, 1);
drh9cfcf5d2002-01-29 18:41:24 +00001074 for(i=0; i<pIdx->nColumn; i++){
1075 int idx = pIdx->aiColumn[i];
1076 if( idx==pTab->iPKey ){
danielk19774adee202004-05-08 08:23:19 +00001077 sqlite3VdbeAddOp(v, OP_Dup, i+extra+nCol+1, 1);
drh9cfcf5d2002-01-29 18:41:24 +00001078 }else{
danielk19774adee202004-05-08 08:23:19 +00001079 sqlite3VdbeAddOp(v, OP_Dup, i+extra+nCol-idx, 1);
drh9cfcf5d2002-01-29 18:41:24 +00001080 }
1081 }
drh7f057c92005-06-24 03:53:06 +00001082 jumpInst1 = sqlite3VdbeAddOp(v, OP_MakeIdxRec, pIdx->nColumn, 0);
danielk1977a37cdde2004-05-16 11:15:36 +00001083 sqlite3IndexAffinityStr(v, pIdx);
drhb2fe7d82003-04-20 17:29:23 +00001084
1085 /* Find out what action to take in case there is an indexing conflict */
drh9cfcf5d2002-01-29 18:41:24 +00001086 onError = pIdx->onError;
drhb2fe7d82003-04-20 17:29:23 +00001087 if( onError==OE_None ) continue; /* pIdx is not a UNIQUE index */
drh9cfcf5d2002-01-29 18:41:24 +00001088 if( overrideError!=OE_Default ){
1089 onError = overrideError;
drha996e472003-05-16 02:30:27 +00001090 }else if( onError==OE_Default ){
1091 onError = OE_Abort;
drh9cfcf5d2002-01-29 18:41:24 +00001092 }
drh5383ae52003-06-04 12:23:30 +00001093 if( seenReplace ){
1094 if( onError==OE_Ignore ) onError = OE_Replace;
1095 else if( onError==OE_Fail ) onError = OE_Abort;
1096 }
1097
drhb2fe7d82003-04-20 17:29:23 +00001098
1099 /* Check to see if the new index entry will be unique */
drhf0863fe2005-06-12 21:35:51 +00001100 sqlite3VdbeAddOp(v, OP_Dup, extra+nCol+1+hasTwoRowids, 1);
danielk19774adee202004-05-08 08:23:19 +00001101 jumpInst2 = sqlite3VdbeAddOp(v, OP_IsUnique, base+iCur+1, 0);
drhb2fe7d82003-04-20 17:29:23 +00001102
1103 /* Generate code that executes if the new index entry is not unique */
danielk1977b84f96f2005-01-20 11:32:23 +00001104 assert( onError==OE_Rollback || onError==OE_Abort || onError==OE_Fail
1105 || onError==OE_Ignore || onError==OE_Replace );
drh9cfcf5d2002-01-29 18:41:24 +00001106 switch( onError ){
drh1c928532002-01-31 15:54:21 +00001107 case OE_Rollback:
1108 case OE_Abort:
1109 case OE_Fail: {
drh37ed48e2003-08-05 13:13:38 +00001110 int j, n1, n2;
1111 char zErrMsg[200];
1112 strcpy(zErrMsg, pIdx->nColumn>1 ? "columns " : "column ");
1113 n1 = strlen(zErrMsg);
1114 for(j=0; j<pIdx->nColumn && n1<sizeof(zErrMsg)-30; j++){
1115 char *zCol = pTab->aCol[pIdx->aiColumn[j]].zName;
1116 n2 = strlen(zCol);
1117 if( j>0 ){
1118 strcpy(&zErrMsg[n1], ", ");
1119 n1 += 2;
1120 }
1121 if( n1+n2>sizeof(zErrMsg)-30 ){
1122 strcpy(&zErrMsg[n1], "...");
1123 n1 += 3;
1124 break;
1125 }else{
1126 strcpy(&zErrMsg[n1], zCol);
1127 n1 += n2;
1128 }
1129 }
1130 strcpy(&zErrMsg[n1],
1131 pIdx->nColumn>1 ? " are not unique" : " is not unique");
danielk19774adee202004-05-08 08:23:19 +00001132 sqlite3VdbeOp3(v, OP_Halt, SQLITE_CONSTRAINT, onError, zErrMsg, 0);
drh9cfcf5d2002-01-29 18:41:24 +00001133 break;
1134 }
1135 case OE_Ignore: {
drh0ca3e242002-01-29 23:07:02 +00001136 assert( seenReplace==0 );
drhf0863fe2005-06-12 21:35:51 +00001137 sqlite3VdbeAddOp(v, OP_Pop, nCol+extra+3+hasTwoRowids, 0);
danielk19774adee202004-05-08 08:23:19 +00001138 sqlite3VdbeAddOp(v, OP_Goto, 0, ignoreDest);
drh9cfcf5d2002-01-29 18:41:24 +00001139 break;
1140 }
1141 case OE_Replace: {
danielk19774adee202004-05-08 08:23:19 +00001142 sqlite3GenerateRowDelete(pParse->db, v, pTab, base, 0);
drh9cfcf5d2002-01-29 18:41:24 +00001143 if( isUpdate ){
drhf0863fe2005-06-12 21:35:51 +00001144 sqlite3VdbeAddOp(v, OP_Dup, nCol+extra+1+hasTwoRowids, 1);
drh7cf6e4d2004-05-19 14:56:55 +00001145 sqlite3VdbeAddOp(v, OP_MoveGe, base, 0);
drh9cfcf5d2002-01-29 18:41:24 +00001146 }
drh0ca3e242002-01-29 23:07:02 +00001147 seenReplace = 1;
drh9cfcf5d2002-01-29 18:41:24 +00001148 break;
1149 }
drh9cfcf5d2002-01-29 18:41:24 +00001150 }
drh0bd1f4e2002-06-06 18:54:39 +00001151#if NULL_DISTINCT_FOR_UNIQUE
drhd654be82005-09-20 17:42:23 +00001152 sqlite3VdbeJumpHere(v, jumpInst1);
drh0bd1f4e2002-06-06 18:54:39 +00001153#endif
drhd654be82005-09-20 17:42:23 +00001154 sqlite3VdbeJumpHere(v, jumpInst2);
drh9cfcf5d2002-01-29 18:41:24 +00001155 }
1156}
drh0ca3e242002-01-29 23:07:02 +00001157
1158/*
1159** This routine generates code to finish the INSERT or UPDATE operation
danielk19774adee202004-05-08 08:23:19 +00001160** that was started by a prior call to sqlite3GenerateConstraintChecks.
drh0ca3e242002-01-29 23:07:02 +00001161** The stack must contain keys for all active indices followed by data
drhf0863fe2005-06-12 21:35:51 +00001162** and the rowid for the new entry. This routine creates the new
drh0ca3e242002-01-29 23:07:02 +00001163** entries in all indices and in the main table.
1164**
drhb419a922002-01-30 16:17:23 +00001165** The arguments to this routine should be the same as the first six
danielk19774adee202004-05-08 08:23:19 +00001166** arguments to sqlite3GenerateConstraintChecks.
drh0ca3e242002-01-29 23:07:02 +00001167*/
danielk19774adee202004-05-08 08:23:19 +00001168void sqlite3CompleteInsertion(
drh0ca3e242002-01-29 23:07:02 +00001169 Parse *pParse, /* The parser context */
1170 Table *pTab, /* the table into which we are inserting */
1171 int base, /* Index of a read/write cursor pointing at pTab */
1172 char *aIdxUsed, /* Which indices are used. NULL means all are used */
drhf0863fe2005-06-12 21:35:51 +00001173 int rowidChng, /* True if the record number will change */
drh70ce3f02003-04-15 19:22:22 +00001174 int isUpdate, /* True for UPDATE, False for INSERT */
1175 int newIdx /* Index of NEW table for triggers. -1 if none */
drh0ca3e242002-01-29 23:07:02 +00001176){
1177 int i;
1178 Vdbe *v;
1179 int nIdx;
1180 Index *pIdx;
danielk1977b28af712004-06-21 06:50:26 +00001181 int pik_flags;
drh0ca3e242002-01-29 23:07:02 +00001182
danielk19774adee202004-05-08 08:23:19 +00001183 v = sqlite3GetVdbe(pParse);
drh0ca3e242002-01-29 23:07:02 +00001184 assert( v!=0 );
drh417be792002-03-03 18:59:40 +00001185 assert( pTab->pSelect==0 ); /* This table is not a VIEW */
drh0ca3e242002-01-29 23:07:02 +00001186 for(nIdx=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, nIdx++){}
1187 for(i=nIdx-1; i>=0; i--){
1188 if( aIdxUsed && aIdxUsed[i]==0 ) continue;
drhf0863fe2005-06-12 21:35:51 +00001189 sqlite3VdbeAddOp(v, OP_IdxInsert, base+i+1, 0);
drh0ca3e242002-01-29 23:07:02 +00001190 }
danielk19774adee202004-05-08 08:23:19 +00001191 sqlite3VdbeAddOp(v, OP_MakeRecord, pTab->nCol, 0);
danielk1977a37cdde2004-05-16 11:15:36 +00001192 sqlite3TableAffinityStr(v, pTab);
danielk1977b84f96f2005-01-20 11:32:23 +00001193#ifndef SQLITE_OMIT_TRIGGER
drh70ce3f02003-04-15 19:22:22 +00001194 if( newIdx>=0 ){
danielk19774adee202004-05-08 08:23:19 +00001195 sqlite3VdbeAddOp(v, OP_Dup, 1, 0);
1196 sqlite3VdbeAddOp(v, OP_Dup, 1, 0);
drhf0863fe2005-06-12 21:35:51 +00001197 sqlite3VdbeAddOp(v, OP_Insert, newIdx, 0);
drh70ce3f02003-04-15 19:22:22 +00001198 }
danielk1977b84f96f2005-01-20 11:32:23 +00001199#endif
drh4794f732004-11-05 17:17:50 +00001200 if( pParse->nested ){
1201 pik_flags = 0;
1202 }else{
danielk197794eb6a12005-12-15 15:22:08 +00001203 pik_flags = OPFLAG_NCHANGE;
1204 pik_flags |= (isUpdate?OPFLAG_ISUPDATE:OPFLAG_LASTROWID);
drh4794f732004-11-05 17:17:50 +00001205 }
drhf0863fe2005-06-12 21:35:51 +00001206 sqlite3VdbeAddOp(v, OP_Insert, base, pik_flags);
danielk197794eb6a12005-12-15 15:22:08 +00001207 if( !pParse->nested ){
1208 sqlite3VdbeChangeP3(v, -1, pTab->zName, P3_STATIC);
1209 }
danielk1977b28af712004-06-21 06:50:26 +00001210
drhf0863fe2005-06-12 21:35:51 +00001211 if( isUpdate && rowidChng ){
danielk19774adee202004-05-08 08:23:19 +00001212 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
drh0ca3e242002-01-29 23:07:02 +00001213 }
1214}
drhcd446902004-02-24 01:05:31 +00001215
1216/*
drh290c1942004-08-21 17:54:45 +00001217** Generate code that will open cursors for a table and for all
drhcd446902004-02-24 01:05:31 +00001218** indices of that table. The "base" parameter is the cursor number used
1219** for the table. Indices are opened on subsequent cursors.
drhcd446902004-02-24 01:05:31 +00001220*/
drh290c1942004-08-21 17:54:45 +00001221void sqlite3OpenTableAndIndices(
1222 Parse *pParse, /* Parsing context */
1223 Table *pTab, /* Table to be opened */
1224 int base, /* Cursor number assigned to the table */
1225 int op /* OP_OpenRead or OP_OpenWrite */
1226){
drhcd446902004-02-24 01:05:31 +00001227 int i;
drh4cbdda92006-06-14 19:00:20 +00001228 int iDb;
drhcd446902004-02-24 01:05:31 +00001229 Index *pIdx;
drh4cbdda92006-06-14 19:00:20 +00001230 Vdbe *v;
1231
1232 if( IsVirtual(pTab) ) return;
1233 iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
1234 v = sqlite3GetVdbe(pParse);
drhcd446902004-02-24 01:05:31 +00001235 assert( v!=0 );
danielk1977c00da102006-01-07 13:21:04 +00001236 sqlite3OpenTable(pParse, base, iDb, pTab, op);
drhcd446902004-02-24 01:05:31 +00001237 for(i=1, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){
danielk1977b3bf5562006-01-10 17:58:23 +00001238 KeyInfo *pKey = sqlite3IndexKeyinfo(pParse, pIdx);
danielk1977da184232006-01-05 11:34:32 +00001239 assert( pIdx->pSchema==pTab->pSchema );
1240 sqlite3VdbeAddOp(v, OP_Integer, iDb, 0);
drh29dda4a2005-07-21 18:23:20 +00001241 VdbeComment((v, "# %s", pIdx->zName));
danielk1977b3bf5562006-01-10 17:58:23 +00001242 sqlite3VdbeOp3(v, op, i+base, pIdx->tnum, (char*)pKey, P3_KEYINFO_HANDOFF);
drhcd446902004-02-24 01:05:31 +00001243 }
drh290c1942004-08-21 17:54:45 +00001244 if( pParse->nTab<=base+i ){
1245 pParse->nTab = base+i;
1246 }
drhcd446902004-02-24 01:05:31 +00001247}
drh9d9cf222007-02-13 15:01:11 +00001248
1249#ifndef SQLITE_OMIT_XFER_OPT
1250/*
1251** Check to collation names to see if they are compatible.
1252*/
1253static int xferCompatibleCollation(const char *z1, const char *z2){
1254 if( z1==0 ){
1255 return z2==0;
1256 }
1257 if( z2==0 ){
1258 return 0;
1259 }
1260 return sqlite3StrICmp(z1, z2)==0;
1261}
1262
1263
1264/*
1265** Check to see if index pSrc is compatible as a source of data
1266** for index pDest in an insert transfer optimization. The rules
1267** for a compatible index:
1268**
1269** * The index is over the same set of columns
1270** * The same DESC and ASC markings occurs on all columns
1271** * The same onError processing (OE_Abort, OE_Ignore, etc)
1272** * The same collating sequence on each column
1273*/
1274static int xferCompatibleIndex(Index *pDest, Index *pSrc){
1275 int i;
1276 assert( pDest && pSrc );
1277 assert( pDest->pTable!=pSrc->pTable );
1278 if( pDest->nColumn!=pSrc->nColumn ){
1279 return 0; /* Different number of columns */
1280 }
1281 if( pDest->onError!=pSrc->onError ){
1282 return 0; /* Different conflict resolution strategies */
1283 }
1284 for(i=0; i<pSrc->nColumn; i++){
1285 if( pSrc->aiColumn[i]!=pDest->aiColumn[i] ){
1286 return 0; /* Different columns indexed */
1287 }
1288 if( pSrc->aSortOrder[i]!=pDest->aSortOrder[i] ){
1289 return 0; /* Different sort orders */
1290 }
1291 if( pSrc->azColl[i]!=pDest->azColl[i] ){
1292 return 0; /* Different sort orders */
1293 }
1294 }
1295
1296 /* If no test above fails then the indices must be compatible */
1297 return 1;
1298}
1299
1300/*
1301** Attempt the transfer optimization on INSERTs of the form
1302**
1303** INSERT INTO tab1 SELECT * FROM tab2;
1304**
1305** This optimization is only attempted if
1306**
1307** (1) tab1 and tab2 have identical schemas including all the
drh8103b7d2007-02-24 13:23:51 +00001308** same indices and constraints
drh9d9cf222007-02-13 15:01:11 +00001309**
1310** (2) tab1 and tab2 are different tables
1311**
1312** (3) There must be no triggers on tab1
1313**
1314** (4) The result set of the SELECT statement is "*"
1315**
1316** (5) The SELECT statement has no WHERE, HAVING, ORDER BY, GROUP BY,
1317** or LIMIT clause.
1318**
1319** (6) The SELECT statement is a simple (not a compound) select that
1320** contains only tab2 in its FROM clause
1321**
1322** This method for implementing the INSERT transfers raw records from
1323** tab2 over to tab1. The columns are not decoded. Raw records from
1324** the indices of tab2 are transfered to tab1 as well. In so doing,
1325** the resulting tab1 has much less fragmentation.
1326**
1327** This routine returns TRUE if the optimization is attempted. If any
1328** of the conditions above fail so that the optimization should not
1329** be attempted, then this routine returns FALSE.
1330*/
1331static int xferOptimization(
1332 Parse *pParse, /* Parser context */
1333 Table *pDest, /* The table we are inserting into */
1334 Select *pSelect, /* A SELECT statement to use as the data source */
1335 int onError, /* How to handle constraint errors */
1336 int iDbDest /* The database of pDest */
1337){
1338 ExprList *pEList; /* The result set of the SELECT */
1339 Table *pSrc; /* The table in the FROM clause of SELECT */
1340 Index *pSrcIdx, *pDestIdx; /* Source and destination indices */
1341 struct SrcList_item *pItem; /* An element of pSelect->pSrc */
1342 int i; /* Loop counter */
1343 int iDbSrc; /* The database of pSrc */
1344 int iSrc, iDest; /* Cursors from source and destination */
1345 int addr1, addr2; /* Loop addresses */
1346 int emptyDestTest; /* Address of test for empty pDest */
1347 int emptySrcTest; /* Address of test for empty pSrc */
1348 int memRowid; /* A memcell containing a rowid from pSrc */
1349 Vdbe *v; /* The VDBE we are building */
1350 KeyInfo *pKey; /* Key information for an index */
1351 int counterMem; /* Memory register used by AUTOINC */
1352
1353 if( pSelect==0 ){
1354 return 0; /* Must be of the form INSERT INTO ... SELECT ... */
1355 }
1356 if( pDest->pTrigger ){
1357 return 0; /* tab1 must not have triggers */
1358 }
1359#ifndef SQLITE_OMIT_VIRTUALTABLE
1360 if( pDest->isVirtual ){
1361 return 0; /* tab1 must not be a virtual table */
1362 }
1363#endif
1364 if( onError==OE_Default ){
1365 onError = OE_Abort;
1366 }
1367 if( onError!=OE_Abort && onError!=OE_Rollback ){
1368 return 0; /* Cannot do OR REPLACE or OR IGNORE or OR FAIL */
1369 }
1370 if( pSelect->pSrc==0 ){
1371 return 0; /* SELECT must have a FROM clause */
1372 }
1373 if( pSelect->pSrc->nSrc!=1 ){
1374 return 0; /* FROM clause must have exactly one term */
1375 }
1376 if( pSelect->pSrc->a[0].pSelect ){
1377 return 0; /* FROM clause cannot contain a subquery */
1378 }
1379 if( pSelect->pWhere ){
1380 return 0; /* SELECT may not have a WHERE clause */
1381 }
1382 if( pSelect->pOrderBy ){
1383 return 0; /* SELECT may not have an ORDER BY clause */
1384 }
drh8103b7d2007-02-24 13:23:51 +00001385 /* Do not need to test for a HAVING clause. If HAVING is present but
1386 ** there is no ORDER BY, we will get an error. */
drh9d9cf222007-02-13 15:01:11 +00001387 if( pSelect->pGroupBy ){
1388 return 0; /* SELECT may not have a GROUP BY clause */
1389 }
1390 if( pSelect->pLimit ){
1391 return 0; /* SELECT may not have a LIMIT clause */
1392 }
drh8103b7d2007-02-24 13:23:51 +00001393 assert( pSelect->pOffset==0 ); /* Must be so if pLimit==0 */
drh9d9cf222007-02-13 15:01:11 +00001394 if( pSelect->pPrior ){
1395 return 0; /* SELECT may not be a compound query */
1396 }
1397 if( pSelect->isDistinct ){
1398 return 0; /* SELECT may not be DISTINCT */
1399 }
1400 pEList = pSelect->pEList;
1401 assert( pEList!=0 );
1402 if( pEList->nExpr!=1 ){
1403 return 0; /* The result set must have exactly one column */
1404 }
1405 assert( pEList->a[0].pExpr );
1406 if( pEList->a[0].pExpr->op!=TK_ALL ){
1407 return 0; /* The result set must be the special operator "*" */
1408 }
1409
1410 /* At this point we have established that the statement is of the
1411 ** correct syntactic form to participate in this optimization. Now
1412 ** we have to check the semantics.
1413 */
1414 pItem = pSelect->pSrc->a;
1415 pSrc = sqlite3LocateTable(pParse, pItem->zName, pItem->zDatabase);
1416 if( pSrc==0 ){
1417 return 0; /* FROM clause does not contain a real table */
1418 }
1419 if( pSrc==pDest ){
1420 return 0; /* tab1 and tab2 may not be the same table */
1421 }
1422#ifndef SQLITE_OMIT_VIRTUALTABLE
1423 if( pSrc->isVirtual ){
1424 return 0; /* tab2 must not be a virtual table */
1425 }
1426#endif
1427 if( pSrc->pSelect ){
1428 return 0; /* tab2 may not be a view */
1429 }
1430 if( pDest->nCol!=pSrc->nCol ){
1431 return 0; /* Number of columns must be the same in tab1 and tab2 */
1432 }
1433 if( pDest->iPKey!=pSrc->iPKey ){
1434 return 0; /* Both tables must have the same INTEGER PRIMARY KEY */
1435 }
1436 for(i=0; i<pDest->nCol; i++){
1437 if( pDest->aCol[i].affinity!=pSrc->aCol[i].affinity ){
1438 return 0; /* Affinity must be the same on all columns */
1439 }
1440 if( !xferCompatibleCollation(pDest->aCol[i].zColl, pSrc->aCol[i].zColl) ){
1441 return 0; /* Collating sequence must be the same on all columns */
1442 }
1443 if( pDest->aCol[i].notNull && !pSrc->aCol[i].notNull ){
1444 return 0; /* tab2 must be NOT NULL if tab1 is */
1445 }
1446 }
1447 for(pDestIdx=pDest->pIndex; pDestIdx; pDestIdx=pDestIdx->pNext){
1448 for(pSrcIdx=pSrc->pIndex; pSrcIdx; pSrcIdx=pSrcIdx->pNext){
1449 if( xferCompatibleIndex(pDestIdx, pSrcIdx) ) break;
1450 }
1451 if( pSrcIdx==0 ){
1452 return 0; /* pDestIdx has no corresponding index in pSrc */
1453 }
1454 }
drh8103b7d2007-02-24 13:23:51 +00001455 if( !sqlite3ExprCompare(pSrc->pCheck, pDest->pCheck) ){
1456 return 0; /* Tables have different CHECK constraints. Ticket #2252 */
1457 }
drh9d9cf222007-02-13 15:01:11 +00001458
1459 /* If we get this far, it means either:
1460 **
1461 ** * We can always do the transfer if the table contains an
1462 ** an integer primary key
1463 **
1464 ** * We can conditionally do the transfer if the destination
1465 ** table is empty.
1466 */
1467 iDbSrc = sqlite3SchemaToIndex(pParse->db, pSrc->pSchema);
1468 v = sqlite3GetVdbe(pParse);
1469 iSrc = pParse->nTab++;
1470 iDest = pParse->nTab++;
1471 counterMem = autoIncBegin(pParse, iDbDest, pDest);
1472 sqlite3OpenTable(pParse, iDest, iDbDest, pDest, OP_OpenWrite);
1473 if( pDest->iPKey<0 ){
1474 /* The tables do not have an INTEGER PRIMARY KEY so that
1475 ** transfer optimization is only allowed if the destination
1476 ** table is initially empty
1477 */
1478 addr1 = sqlite3VdbeAddOp(v, OP_Rewind, iDest, 0);
1479 emptyDestTest = sqlite3VdbeAddOp(v, OP_Goto, 0, 0);
1480 sqlite3VdbeJumpHere(v, addr1);
1481 }else{
1482 emptyDestTest = 0;
1483 }
1484 sqlite3OpenTable(pParse, iSrc, iDbSrc, pSrc, OP_OpenRead);
1485 emptySrcTest = sqlite3VdbeAddOp(v, OP_Rewind, iSrc, 0);
1486 memRowid = pParse->nMem++;
1487 sqlite3VdbeAddOp(v, OP_Rowid, iSrc, 0);
1488 sqlite3VdbeAddOp(v, OP_MemStore, memRowid, 1);
1489 addr1 = sqlite3VdbeAddOp(v, OP_Rowid, iSrc, 0);
1490 sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
1491 addr2 = sqlite3VdbeAddOp(v, OP_NotExists, iDest, 0);
1492 sqlite3VdbeOp3(v, OP_Halt, SQLITE_CONSTRAINT, onError,
1493 "PRIMARY KEY must be unique", P3_STATIC);
1494 sqlite3VdbeJumpHere(v, addr2);
1495 autoIncStep(pParse, counterMem);
1496 sqlite3VdbeAddOp(v, OP_RowData, iSrc, 0);
1497 sqlite3VdbeOp3(v, OP_Insert, iDest, OPFLAG_NCHANGE|OPFLAG_LASTROWID,
1498 pDest->zName, 0);
1499 sqlite3VdbeAddOp(v, OP_Next, iSrc, addr1);
1500 autoIncEnd(pParse, iDbDest, pDest, counterMem);
1501 for(pDestIdx=pDest->pIndex; pDestIdx; pDestIdx=pDestIdx->pNext){
1502 for(pSrcIdx=pSrc->pIndex; pSrcIdx; pSrcIdx=pSrcIdx->pNext){
1503 if( xferCompatibleIndex(pDestIdx, pSrcIdx) ) break;
1504 }
1505 assert( pSrcIdx );
1506 sqlite3VdbeAddOp(v, OP_Close, iSrc, 0);
1507 sqlite3VdbeAddOp(v, OP_Close, iDest, 0);
1508 sqlite3VdbeAddOp(v, OP_Integer, iDbSrc, 0);
1509 pKey = sqlite3IndexKeyinfo(pParse, pSrcIdx);
1510 VdbeComment((v, "# %s", pSrcIdx->zName));
1511 sqlite3VdbeOp3(v, OP_OpenRead, iSrc, pSrcIdx->tnum,
1512 (char*)pKey, P3_KEYINFO_HANDOFF);
1513 sqlite3VdbeAddOp(v, OP_Integer, iDbDest, 0);
1514 pKey = sqlite3IndexKeyinfo(pParse, pDestIdx);
1515 VdbeComment((v, "# %s", pDestIdx->zName));
1516 sqlite3VdbeOp3(v, OP_OpenWrite, iDest, pDestIdx->tnum,
1517 (char*)pKey, P3_KEYINFO_HANDOFF);
1518 addr1 = sqlite3VdbeAddOp(v, OP_Rewind, iSrc, 0);
1519 sqlite3VdbeAddOp(v, OP_RowKey, iSrc, 0);
1520 if( pDestIdx->onError!=OE_None ){
1521 sqlite3VdbeAddOp(v, OP_MemLoad, memRowid, 0);
1522 addr2 = sqlite3VdbeAddOp(v, OP_IsUnique, iDest, 0);
1523 sqlite3VdbeOp3(v, OP_Halt, SQLITE_CONSTRAINT, onError,
1524 "UNIQUE constraint failed", P3_STATIC);
1525 sqlite3VdbeJumpHere(v, addr2);
1526 }
1527 sqlite3VdbeAddOp(v, OP_IdxInsert, iDest, 0);
1528 sqlite3VdbeAddOp(v, OP_Next, iSrc, addr1+1);
1529 sqlite3VdbeJumpHere(v, addr1);
1530 }
1531 sqlite3VdbeJumpHere(v, emptySrcTest);
1532 sqlite3VdbeAddOp(v, OP_Close, iSrc, 0);
1533 sqlite3VdbeAddOp(v, OP_Close, iDest, 0);
1534 if( emptyDestTest ){
1535 sqlite3VdbeAddOp(v, OP_Halt, SQLITE_OK, 0);
1536 sqlite3VdbeJumpHere(v, emptyDestTest);
1537 sqlite3VdbeAddOp(v, OP_Close, iDest, 0);
1538 return 0;
1539 }else{
1540 return 1;
1541 }
1542}
1543#endif /* SQLITE_OMIT_XFER_OPT */