blob: 728c06ed924c5ee7ce0d86c6f87f9b3a58dc6c4c [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**
danielk1977595a5232009-07-24 17:58:53 +000015** $Id: insert.c,v 1.270 2009/07/24 17:58:53 danielk1977 Exp $
drhcce7d172000-05-31 15:34:51 +000016*/
17#include "sqliteInt.h"
18
19/*
drhbbb5e4e2009-04-30 00:11:09 +000020** Generate code that will open a table for reading.
21*/
22void sqlite3OpenTable(
23 Parse *p, /* Generate code into this VDBE */
24 int iCur, /* The cursor number of the table */
25 int iDb, /* The database index in sqlite3.aDb[] */
26 Table *pTab, /* The table to be opened */
27 int opcode /* OP_OpenRead or OP_OpenWrite */
28){
29 Vdbe *v;
30 if( IsVirtual(pTab) ) return;
31 v = sqlite3GetVdbe(p);
32 assert( opcode==OP_OpenWrite || opcode==OP_OpenRead );
33 sqlite3TableLock(p, iDb, pTab->tnum, (opcode==OP_OpenWrite)?1:0, pTab->zName);
34 sqlite3VdbeAddOp3(v, opcode, iCur, pTab->tnum, iDb);
35 sqlite3VdbeChangeP4(v, -1, SQLITE_INT_TO_PTR(pTab->nCol), P4_INT32);
36 VdbeComment((v, "%s", pTab->zName));
37}
38
39/*
dan69f8bb92009-08-13 19:21:16 +000040** Return a pointer to the column affinity string associated with index
41** pIdx. A column affinity string has one character for each column in
42** the table, according to the affinity of the column:
danielk19773d1bfea2004-05-14 11:00:53 +000043**
44** Character Column affinity
45** ------------------------------
drh3eda0402005-11-24 13:15:32 +000046** 'a' TEXT
47** 'b' NONE
48** 'c' NUMERIC
49** 'd' INTEGER
50** 'e' REAL
drh2d401ab2008-01-10 23:50:11 +000051**
52** An extra 'b' is appended to the end of the string to cover the
53** rowid that appears as the last column in every index.
dan69f8bb92009-08-13 19:21:16 +000054**
55** Memory for the buffer containing the column index affinity string
56** is managed along with the rest of the Index structure. It will be
57** released when sqlite3DeleteIndex() is called.
danielk19773d1bfea2004-05-14 11:00:53 +000058*/
dan69f8bb92009-08-13 19:21:16 +000059const char *sqlite3IndexAffinityStr(Vdbe *v, Index *pIdx){
danielk1977a37cdde2004-05-16 11:15:36 +000060 if( !pIdx->zColAff ){
danielk1977e014a832004-05-17 10:48:57 +000061 /* The first time a column affinity string for a particular index is
danielk1977a37cdde2004-05-16 11:15:36 +000062 ** required, it is allocated and populated here. It is then stored as
danielk1977e014a832004-05-17 10:48:57 +000063 ** a member of the Index structure for subsequent use.
danielk1977a37cdde2004-05-16 11:15:36 +000064 **
65 ** The column affinity string will eventually be deleted by
danielk1977e014a832004-05-17 10:48:57 +000066 ** sqliteDeleteIndex() when the Index structure itself is cleaned
danielk1977a37cdde2004-05-16 11:15:36 +000067 ** up.
68 */
69 int n;
70 Table *pTab = pIdx->pTable;
drhabb6fca2007-08-16 12:24:01 +000071 sqlite3 *db = sqlite3VdbeDb(v);
drh633e6d52008-07-28 19:34:53 +000072 pIdx->zColAff = (char *)sqlite3Malloc(pIdx->nColumn+2);
danielk1977a37cdde2004-05-16 11:15:36 +000073 if( !pIdx->zColAff ){
drh633e6d52008-07-28 19:34:53 +000074 db->mallocFailed = 1;
dan69f8bb92009-08-13 19:21:16 +000075 return 0;
danielk1977a37cdde2004-05-16 11:15:36 +000076 }
77 for(n=0; n<pIdx->nColumn; n++){
78 pIdx->zColAff[n] = pTab->aCol[pIdx->aiColumn[n]].affinity;
79 }
drh2d401ab2008-01-10 23:50:11 +000080 pIdx->zColAff[n++] = SQLITE_AFF_NONE;
81 pIdx->zColAff[n] = 0;
danielk1977a37cdde2004-05-16 11:15:36 +000082 }
danielk19773d1bfea2004-05-14 11:00:53 +000083
dan69f8bb92009-08-13 19:21:16 +000084 return pIdx->zColAff;
danielk1977a37cdde2004-05-16 11:15:36 +000085}
86
87/*
drh66a51672008-01-03 00:01:23 +000088** Set P4 of the most recently inserted opcode to a column affinity
danielk1977a37cdde2004-05-16 11:15:36 +000089** string for table pTab. A column affinity string has one character
90** for each column indexed by the index, according to the affinity of the
91** column:
92**
93** Character Column affinity
94** ------------------------------
drh3eda0402005-11-24 13:15:32 +000095** 'a' TEXT
96** 'b' NONE
97** 'c' NUMERIC
98** 'd' INTEGER
99** 'e' REAL
danielk1977a37cdde2004-05-16 11:15:36 +0000100*/
101void sqlite3TableAffinityStr(Vdbe *v, Table *pTab){
danielk19773d1bfea2004-05-14 11:00:53 +0000102 /* The first time a column affinity string for a particular table
103 ** is required, it is allocated and populated here. It is then
104 ** stored as a member of the Table structure for subsequent use.
105 **
106 ** The column affinity string will eventually be deleted by
107 ** sqlite3DeleteTable() when the Table structure itself is cleaned up.
108 */
109 if( !pTab->zColAff ){
110 char *zColAff;
111 int i;
drhabb6fca2007-08-16 12:24:01 +0000112 sqlite3 *db = sqlite3VdbeDb(v);
danielk19773d1bfea2004-05-14 11:00:53 +0000113
drh633e6d52008-07-28 19:34:53 +0000114 zColAff = (char *)sqlite3Malloc(pTab->nCol+1);
danielk19773d1bfea2004-05-14 11:00:53 +0000115 if( !zColAff ){
drh633e6d52008-07-28 19:34:53 +0000116 db->mallocFailed = 1;
danielk1977a37cdde2004-05-16 11:15:36 +0000117 return;
danielk19773d1bfea2004-05-14 11:00:53 +0000118 }
119
120 for(i=0; i<pTab->nCol; i++){
danielk1977a37cdde2004-05-16 11:15:36 +0000121 zColAff[i] = pTab->aCol[i].affinity;
danielk19773d1bfea2004-05-14 11:00:53 +0000122 }
123 zColAff[pTab->nCol] = '\0';
124
125 pTab->zColAff = zColAff;
126 }
127
drh66a51672008-01-03 00:01:23 +0000128 sqlite3VdbeChangeP4(v, -1, pTab->zColAff, 0);
danielk19773d1bfea2004-05-14 11:00:53 +0000129}
130
danielk19774d887782005-02-08 08:42:27 +0000131/*
drh48d11782007-11-23 15:02:19 +0000132** Return non-zero if the table pTab in database iDb or any of its indices
133** have been opened at any point in the VDBE program beginning at location
134** iStartAddr throught the end of the program. This is used to see if
135** a statement of the form "INSERT INTO <iDb, pTab> SELECT ..." can
136** run without using temporary table for the results of the SELECT.
danielk19774d887782005-02-08 08:42:27 +0000137*/
danielk1977595a5232009-07-24 17:58:53 +0000138static int readsTable(Parse *p, int iStartAddr, int iDb, Table *pTab){
139 Vdbe *v = sqlite3GetVdbe(p);
danielk19774d887782005-02-08 08:42:27 +0000140 int i;
drh48d11782007-11-23 15:02:19 +0000141 int iEnd = sqlite3VdbeCurrentAddr(v);
danielk1977595a5232009-07-24 17:58:53 +0000142#ifndef SQLITE_OMIT_VIRTUALTABLE
143 VTable *pVTab = IsVirtual(pTab) ? sqlite3GetVTable(p->db, pTab) : 0;
144#endif
145
drh48d11782007-11-23 15:02:19 +0000146 for(i=iStartAddr; i<iEnd; i++){
147 VdbeOp *pOp = sqlite3VdbeGetOp(v, i);
drhef0bea92007-12-14 16:11:09 +0000148 assert( pOp!=0 );
danielk1977207872a2008-01-03 07:54:23 +0000149 if( pOp->opcode==OP_OpenRead && pOp->p3==iDb ){
150 Index *pIndex;
drh48d11782007-11-23 15:02:19 +0000151 int tnum = pOp->p2;
danielk1977207872a2008-01-03 07:54:23 +0000152 if( tnum==pTab->tnum ){
153 return 1;
154 }
155 for(pIndex=pTab->pIndex; pIndex; pIndex=pIndex->pNext){
156 if( tnum==pIndex->tnum ){
drh48d11782007-11-23 15:02:19 +0000157 return 1;
158 }
drh48d11782007-11-23 15:02:19 +0000159 }
160 }
drh543165e2007-11-27 14:46:41 +0000161#ifndef SQLITE_OMIT_VIRTUALTABLE
danielk1977595a5232009-07-24 17:58:53 +0000162 if( pOp->opcode==OP_VOpen && pOp->p4.pVtab==pVTab ){
danielk19772dca4ac2008-01-03 11:50:29 +0000163 assert( pOp->p4.pVtab!=0 );
drh66a51672008-01-03 00:01:23 +0000164 assert( pOp->p4type==P4_VTAB );
drh48d11782007-11-23 15:02:19 +0000165 return 1;
danielk19774d887782005-02-08 08:42:27 +0000166 }
drh543165e2007-11-27 14:46:41 +0000167#endif
danielk19774d887782005-02-08 08:42:27 +0000168 }
169 return 0;
170}
danielk19773d1bfea2004-05-14 11:00:53 +0000171
drh9d9cf222007-02-13 15:01:11 +0000172#ifndef SQLITE_OMIT_AUTOINCREMENT
173/*
drh0b9f50d2009-06-23 20:28:53 +0000174** Locate or create an AutoincInfo structure associated with table pTab
175** which is in database iDb. Return the register number for the register
176** that holds the maximum rowid.
drh9d9cf222007-02-13 15:01:11 +0000177**
drh0b9f50d2009-06-23 20:28:53 +0000178** There is at most one AutoincInfo structure per table even if the
179** same table is autoincremented multiple times due to inserts within
180** triggers. A new AutoincInfo structure is created if this is the
181** first use of table pTab. On 2nd and subsequent uses, the original
182** AutoincInfo structure is used.
drh9d9cf222007-02-13 15:01:11 +0000183**
drh0b9f50d2009-06-23 20:28:53 +0000184** Three memory locations are allocated:
185**
186** (1) Register to hold the name of the pTab table.
187** (2) Register to hold the maximum ROWID of pTab.
188** (3) Register to hold the rowid in sqlite_sequence of pTab
189**
190** The 2nd register is the one that is returned. That is all the
191** insert routine needs to know about.
drh9d9cf222007-02-13 15:01:11 +0000192*/
193static int autoIncBegin(
194 Parse *pParse, /* Parsing context */
195 int iDb, /* Index of the database holding pTab */
196 Table *pTab /* The table we are writing to */
197){
drh6a288a32008-01-07 19:20:24 +0000198 int memId = 0; /* Register holding maximum rowid */
drh7d10d5a2008-08-20 16:35:10 +0000199 if( pTab->tabFlags & TF_Autoincrement ){
drh0b9f50d2009-06-23 20:28:53 +0000200 AutoincInfo *pInfo;
201
202 pInfo = pParse->pAinc;
203 while( pInfo && pInfo->pTab!=pTab ){ pInfo = pInfo->pNext; }
204 if( pInfo==0 ){
205 pInfo = sqlite3DbMallocRaw(pParse->db, sizeof(*pInfo));
206 if( pInfo==0 ) return 0;
207 pInfo->pNext = pParse->pAinc;
208 pParse->pAinc = pInfo;
209 pInfo->pTab = pTab;
210 pInfo->iDb = iDb;
211 pParse->nMem++; /* Register to hold name of table */
212 pInfo->regCtr = ++pParse->nMem; /* Max rowid register */
213 pParse->nMem++; /* Rowid in sqlite_sequence */
214 }
215 memId = pInfo->regCtr;
drh9d9cf222007-02-13 15:01:11 +0000216 }
217 return memId;
218}
219
220/*
drh0b9f50d2009-06-23 20:28:53 +0000221** This routine generates code that will initialize all of the
222** register used by the autoincrement tracker.
223*/
224void sqlite3AutoincrementBegin(Parse *pParse){
225 AutoincInfo *p; /* Information about an AUTOINCREMENT */
226 sqlite3 *db = pParse->db; /* The database connection */
227 Db *pDb; /* Database only autoinc table */
228 int memId; /* Register holding max rowid */
229 int addr; /* A VDBE address */
230 Vdbe *v = pParse->pVdbe; /* VDBE under construction */
231
232 assert( v ); /* We failed long ago if this is not so */
233 for(p = pParse->pAinc; p; p = p->pNext){
234 pDb = &db->aDb[p->iDb];
235 memId = p->regCtr;
236 sqlite3OpenTable(pParse, 0, p->iDb, pDb->pSchema->pSeqTab, OP_OpenRead);
237 addr = sqlite3VdbeCurrentAddr(v);
238 sqlite3VdbeAddOp4(v, OP_String8, 0, memId-1, 0, p->pTab->zName, 0);
239 sqlite3VdbeAddOp2(v, OP_Rewind, 0, addr+9);
240 sqlite3VdbeAddOp3(v, OP_Column, 0, 0, memId);
241 sqlite3VdbeAddOp3(v, OP_Ne, memId-1, addr+7, memId);
242 sqlite3VdbeChangeP5(v, SQLITE_JUMPIFNULL);
243 sqlite3VdbeAddOp2(v, OP_Rowid, 0, memId+1);
244 sqlite3VdbeAddOp3(v, OP_Column, 0, 1, memId);
245 sqlite3VdbeAddOp2(v, OP_Goto, 0, addr+9);
246 sqlite3VdbeAddOp2(v, OP_Next, 0, addr+2);
247 sqlite3VdbeAddOp2(v, OP_Integer, 0, memId);
248 sqlite3VdbeAddOp0(v, OP_Close);
249 }
250}
251
252/*
drh9d9cf222007-02-13 15:01:11 +0000253** Update the maximum rowid for an autoincrement calculation.
254**
255** This routine should be called when the top of the stack holds a
256** new rowid that is about to be inserted. If that new rowid is
257** larger than the maximum rowid in the memId memory cell, then the
258** memory cell is updated. The stack is unchanged.
259*/
drh6a288a32008-01-07 19:20:24 +0000260static void autoIncStep(Parse *pParse, int memId, int regRowid){
drh9d9cf222007-02-13 15:01:11 +0000261 if( memId>0 ){
drh6a288a32008-01-07 19:20:24 +0000262 sqlite3VdbeAddOp2(pParse->pVdbe, OP_MemMax, memId, regRowid);
drh9d9cf222007-02-13 15:01:11 +0000263 }
264}
265
266/*
drh0b9f50d2009-06-23 20:28:53 +0000267** This routine generates the code needed to write autoincrement
268** maximum rowid values back into the sqlite_sequence register.
269** Every statement that might do an INSERT into an autoincrement
270** table (either directly or through triggers) needs to call this
271** routine just before the "exit" code.
drh9d9cf222007-02-13 15:01:11 +0000272*/
drh0b9f50d2009-06-23 20:28:53 +0000273void sqlite3AutoincrementEnd(Parse *pParse){
274 AutoincInfo *p;
275 Vdbe *v = pParse->pVdbe;
276 sqlite3 *db = pParse->db;
drh6a288a32008-01-07 19:20:24 +0000277
drh0b9f50d2009-06-23 20:28:53 +0000278 assert( v );
279 for(p = pParse->pAinc; p; p = p->pNext){
280 Db *pDb = &db->aDb[p->iDb];
281 int j1, j2, j3, j4, j5;
282 int iRec;
283 int memId = p->regCtr;
284
285 iRec = sqlite3GetTempReg(pParse);
286 sqlite3OpenTable(pParse, 0, p->iDb, pDb->pSchema->pSeqTab, OP_OpenWrite);
drh6a288a32008-01-07 19:20:24 +0000287 j1 = sqlite3VdbeAddOp1(v, OP_NotNull, memId+1);
drh0b9f50d2009-06-23 20:28:53 +0000288 j2 = sqlite3VdbeAddOp0(v, OP_Rewind);
289 j3 = sqlite3VdbeAddOp3(v, OP_Column, 0, 0, iRec);
290 j4 = sqlite3VdbeAddOp3(v, OP_Eq, memId-1, 0, iRec);
291 sqlite3VdbeAddOp2(v, OP_Next, 0, j3);
292 sqlite3VdbeJumpHere(v, j2);
293 sqlite3VdbeAddOp2(v, OP_NewRowid, 0, memId+1);
294 j5 = sqlite3VdbeAddOp0(v, OP_Goto);
295 sqlite3VdbeJumpHere(v, j4);
296 sqlite3VdbeAddOp2(v, OP_Rowid, 0, memId+1);
drh6a288a32008-01-07 19:20:24 +0000297 sqlite3VdbeJumpHere(v, j1);
drh0b9f50d2009-06-23 20:28:53 +0000298 sqlite3VdbeJumpHere(v, j5);
danielk1977a7a8e142008-02-13 18:25:27 +0000299 sqlite3VdbeAddOp3(v, OP_MakeRecord, memId-1, 2, iRec);
drh0b9f50d2009-06-23 20:28:53 +0000300 sqlite3VdbeAddOp3(v, OP_Insert, 0, iRec, memId+1);
drh35573352008-01-08 23:54:25 +0000301 sqlite3VdbeChangeP5(v, OPFLAG_APPEND);
drh0b9f50d2009-06-23 20:28:53 +0000302 sqlite3VdbeAddOp0(v, OP_Close);
303 sqlite3ReleaseTempReg(pParse, iRec);
drh9d9cf222007-02-13 15:01:11 +0000304 }
305}
306#else
307/*
308** If SQLITE_OMIT_AUTOINCREMENT is defined, then the three routines
309** above are all no-ops
310*/
311# define autoIncBegin(A,B,C) (0)
danielk1977287fb612008-01-04 19:10:28 +0000312# define autoIncStep(A,B,C)
drh9d9cf222007-02-13 15:01:11 +0000313#endif /* SQLITE_OMIT_AUTOINCREMENT */
314
315
316/* Forward declaration */
317static int xferOptimization(
318 Parse *pParse, /* Parser context */
319 Table *pDest, /* The table we are inserting into */
320 Select *pSelect, /* A SELECT statement to use as the data source */
321 int onError, /* How to handle constraint errors */
322 int iDbDest /* The database of pDest */
323);
324
danielk19773d1bfea2004-05-14 11:00:53 +0000325/*
drh1ccde152000-06-17 13:12:39 +0000326** This routine is call to handle SQL of the following forms:
drhcce7d172000-05-31 15:34:51 +0000327**
328** insert into TABLE (IDLIST) values(EXPRLIST)
drh1ccde152000-06-17 13:12:39 +0000329** insert into TABLE (IDLIST) select
drhcce7d172000-05-31 15:34:51 +0000330**
drh1ccde152000-06-17 13:12:39 +0000331** The IDLIST following the table name is always optional. If omitted,
332** then a list of all columns for the table is substituted. The IDLIST
drh967e8b72000-06-21 13:59:10 +0000333** appears in the pColumn parameter. pColumn is NULL if IDLIST is omitted.
drh1ccde152000-06-17 13:12:39 +0000334**
335** The pList parameter holds EXPRLIST in the first form of the INSERT
336** statement above, and pSelect is NULL. For the second form, pList is
337** NULL and pSelect is a pointer to the select statement used to generate
338** data for the insert.
drh142e30d2002-08-28 03:00:58 +0000339**
drh9d9cf222007-02-13 15:01:11 +0000340** The code generated follows one of four templates. For a simple
drh142e30d2002-08-28 03:00:58 +0000341** select with data coming from a VALUES clause, the code executes
drhe00ee6e2008-06-20 15:24:01 +0000342** once straight down through. Pseudo-code follows (we call this
343** the "1st template"):
drh142e30d2002-08-28 03:00:58 +0000344**
345** open write cursor to <table> and its indices
346** puts VALUES clause expressions onto the stack
347** write the resulting record into <table>
348** cleanup
349**
drh9d9cf222007-02-13 15:01:11 +0000350** The three remaining templates assume the statement is of the form
drh142e30d2002-08-28 03:00:58 +0000351**
352** INSERT INTO <table> SELECT ...
353**
drh9d9cf222007-02-13 15:01:11 +0000354** If the SELECT clause is of the restricted form "SELECT * FROM <table2>" -
355** in other words if the SELECT pulls all columns from a single table
356** and there is no WHERE or LIMIT or GROUP BY or ORDER BY clauses, and
357** if <table2> and <table1> are distinct tables but have identical
358** schemas, including all the same indices, then a special optimization
359** is invoked that copies raw records from <table2> over to <table1>.
360** See the xferOptimization() function for the implementation of this
drhe00ee6e2008-06-20 15:24:01 +0000361** template. This is the 2nd template.
drh9d9cf222007-02-13 15:01:11 +0000362**
363** open a write cursor to <table>
364** open read cursor on <table2>
365** transfer all records in <table2> over to <table>
366** close cursors
367** foreach index on <table>
368** open a write cursor on the <table> index
369** open a read cursor on the corresponding <table2> index
370** transfer all records from the read to the write cursors
371** close cursors
372** end foreach
373**
drhe00ee6e2008-06-20 15:24:01 +0000374** The 3rd template is for when the second template does not apply
drh9d9cf222007-02-13 15:01:11 +0000375** and the SELECT clause does not read from <table> at any time.
376** The generated code follows this template:
drh142e30d2002-08-28 03:00:58 +0000377**
drhe00ee6e2008-06-20 15:24:01 +0000378** EOF <- 0
379** X <- A
drh142e30d2002-08-28 03:00:58 +0000380** goto B
381** A: setup for the SELECT
drh9d9cf222007-02-13 15:01:11 +0000382** loop over the rows in the SELECT
drhe00ee6e2008-06-20 15:24:01 +0000383** load values into registers R..R+n
384** yield X
drh142e30d2002-08-28 03:00:58 +0000385** end loop
386** cleanup after the SELECT
drhe00ee6e2008-06-20 15:24:01 +0000387** EOF <- 1
388** yield X
drh142e30d2002-08-28 03:00:58 +0000389** goto A
drhe00ee6e2008-06-20 15:24:01 +0000390** B: open write cursor to <table> and its indices
391** C: yield X
392** if EOF goto D
393** insert the select result into <table> from R..R+n
394** goto C
drh142e30d2002-08-28 03:00:58 +0000395** D: cleanup
396**
drhe00ee6e2008-06-20 15:24:01 +0000397** The 4th template is used if the insert statement takes its
drh142e30d2002-08-28 03:00:58 +0000398** values from a SELECT but the data is being inserted into a table
399** that is also read as part of the SELECT. In the third form,
400** we have to use a intermediate table to store the results of
401** the select. The template is like this:
402**
drhe00ee6e2008-06-20 15:24:01 +0000403** EOF <- 0
404** X <- A
drh142e30d2002-08-28 03:00:58 +0000405** goto B
406** A: setup for the SELECT
407** loop over the tables in the SELECT
drhe00ee6e2008-06-20 15:24:01 +0000408** load value into register R..R+n
409** yield X
drh142e30d2002-08-28 03:00:58 +0000410** end loop
411** cleanup after the SELECT
drhe00ee6e2008-06-20 15:24:01 +0000412** EOF <- 1
413** yield X
414** halt-error
415** B: open temp table
416** L: yield X
417** if EOF goto M
418** insert row from R..R+n into temp table
419** goto L
420** M: open write cursor to <table> and its indices
421** rewind temp table
422** C: loop over rows of intermediate table
drh142e30d2002-08-28 03:00:58 +0000423** transfer values form intermediate table into <table>
drhe00ee6e2008-06-20 15:24:01 +0000424** end loop
425** D: cleanup
drhcce7d172000-05-31 15:34:51 +0000426*/
danielk19774adee202004-05-08 08:23:19 +0000427void sqlite3Insert(
drhcce7d172000-05-31 15:34:51 +0000428 Parse *pParse, /* Parser context */
drh113088e2003-03-20 01:16:58 +0000429 SrcList *pTabList, /* Name of table into which we are inserting */
drhcce7d172000-05-31 15:34:51 +0000430 ExprList *pList, /* List of values to be inserted */
drh5974a302000-06-07 14:42:26 +0000431 Select *pSelect, /* A SELECT statement to use as the data source */
drh9cfcf5d2002-01-29 18:41:24 +0000432 IdList *pColumn, /* Column names corresponding to IDLIST. */
433 int onError /* How to handle constraint errors */
drhcce7d172000-05-31 15:34:51 +0000434){
drh6a288a32008-01-07 19:20:24 +0000435 sqlite3 *db; /* The main database structure */
436 Table *pTab; /* The table to insert into. aka TABLE */
drh113088e2003-03-20 01:16:58 +0000437 char *zTab; /* Name of the table into which we are inserting */
drhe22a3342003-04-22 20:30:37 +0000438 const char *zDb; /* Name of the database holding this table */
drh5974a302000-06-07 14:42:26 +0000439 int i, j, idx; /* Loop counters */
440 Vdbe *v; /* Generate code into this virtual machine */
441 Index *pIdx; /* For looping over indices of the table */
drh967e8b72000-06-21 13:59:10 +0000442 int nColumn; /* Number of columns in the data */
drh6a288a32008-01-07 19:20:24 +0000443 int nHidden = 0; /* Number of hidden columns if TABLE is virtual */
drh04adf412008-01-08 18:57:50 +0000444 int baseCur = 0; /* VDBE Cursor number for pTab */
drh4a324312001-12-21 14:30:42 +0000445 int keyColumn = -1; /* Column that is the INTEGER PRIMARY KEY */
drh0ca3e242002-01-29 23:07:02 +0000446 int endOfLoop; /* Label for the end of the insertion loop */
danielk19774d887782005-02-08 08:42:27 +0000447 int useTempTable = 0; /* Store SELECT results in intermediate table */
danielk1977cfe9a692004-06-16 12:00:29 +0000448 int srcTab = 0; /* Data comes from this temporary cursor if >=0 */
drhe00ee6e2008-06-20 15:24:01 +0000449 int addrInsTop = 0; /* Jump to label "D" */
450 int addrCont = 0; /* Top of insert loop. Label "C" in templates 3 and 4 */
451 int addrSelect = 0; /* Address of coroutine that implements the SELECT */
drh2eb95372008-06-06 15:04:36 +0000452 SelectDest dest; /* Destination for SELECT on rhs of INSERT */
drh6a288a32008-01-07 19:20:24 +0000453 int newIdx = -1; /* Cursor for the NEW pseudo-table */
454 int iDb; /* Index of database holding TABLE */
drh2958a4e2004-11-12 03:56:15 +0000455 Db *pDb; /* The database containing table being inserted into */
drhe4d90812007-03-29 05:51:49 +0000456 int appendFlag = 0; /* True if the insert is likely to be an append */
drhcce7d172000-05-31 15:34:51 +0000457
drh6a288a32008-01-07 19:20:24 +0000458 /* Register allocations */
drh1bd10f82008-12-10 21:19:56 +0000459 int regFromSelect = 0;/* Base register for data coming from SELECT */
drh6a288a32008-01-07 19:20:24 +0000460 int regAutoinc = 0; /* Register holding the AUTOINCREMENT counter */
461 int regRowCount = 0; /* Memory cell used for the row counter */
462 int regIns; /* Block of regs holding rowid+data being inserted */
463 int regRowid; /* registers holding insert rowid */
464 int regData; /* register holding first column to insert */
465 int regRecord; /* Holds the assemblied row record */
drh1bd10f82008-12-10 21:19:56 +0000466 int regEof = 0; /* Register recording end of SELECT data */
drhaa9b8962008-01-08 02:57:55 +0000467 int *aRegIdx = 0; /* One register allocated to each index */
drh6a288a32008-01-07 19:20:24 +0000468
danielk1977034ca142007-06-26 10:38:54 +0000469
drh798da522004-11-04 04:42:28 +0000470#ifndef SQLITE_OMIT_TRIGGER
471 int isView; /* True if attempting to insert into a view */
danielk19772f886d12009-02-28 10:47:41 +0000472 Trigger *pTrigger; /* List of triggers on pTab, if required */
473 int tmask; /* Mask of trigger times */
drh798da522004-11-04 04:42:28 +0000474#endif
danielk1977c3f9bad2002-05-15 08:30:12 +0000475
drh17435752007-08-16 04:30:38 +0000476 db = pParse->db;
drh1bd10f82008-12-10 21:19:56 +0000477 memset(&dest, 0, sizeof(dest));
drh17435752007-08-16 04:30:38 +0000478 if( pParse->nErr || db->mallocFailed ){
drh6f7adc82006-01-11 21:41:20 +0000479 goto insert_cleanup;
480 }
drhdaffd0e2001-04-11 14:28:42 +0000481
drh1ccde152000-06-17 13:12:39 +0000482 /* Locate the table into which we will be inserting new information.
483 */
drh113088e2003-03-20 01:16:58 +0000484 assert( pTabList->nSrc==1 );
485 zTab = pTabList->a[0].zName;
drh098d1682009-05-02 15:46:46 +0000486 if( NEVER(zTab==0) ) goto insert_cleanup;
danielk19774adee202004-05-08 08:23:19 +0000487 pTab = sqlite3SrcListLookup(pParse, pTabList);
danielk1977c3f9bad2002-05-15 08:30:12 +0000488 if( pTab==0 ){
danielk1977c3f9bad2002-05-15 08:30:12 +0000489 goto insert_cleanup;
490 }
danielk1977da184232006-01-05 11:34:32 +0000491 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
492 assert( iDb<db->nDb );
493 pDb = &db->aDb[iDb];
drh2958a4e2004-11-12 03:56:15 +0000494 zDb = pDb->zName;
danielk19774adee202004-05-08 08:23:19 +0000495 if( sqlite3AuthCheck(pParse, SQLITE_INSERT, pTab->zName, 0, zDb) ){
drh1962bda2003-01-12 19:33:52 +0000496 goto insert_cleanup;
497 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000498
drhb7f91642004-10-31 02:22:47 +0000499 /* Figure out if we have any triggers and if the table being
500 ** inserted into is a view
501 */
502#ifndef SQLITE_OMIT_TRIGGER
danielk19772f886d12009-02-28 10:47:41 +0000503 pTrigger = sqlite3TriggersExist(pParse, pTab, TK_INSERT, 0, &tmask);
drhb7f91642004-10-31 02:22:47 +0000504 isView = pTab->pSelect!=0;
505#else
danielk19772f886d12009-02-28 10:47:41 +0000506# define pTrigger 0
507# define tmask 0
drhb7f91642004-10-31 02:22:47 +0000508# define isView 0
509#endif
510#ifdef SQLITE_OMIT_VIEW
511# undef isView
512# define isView 0
513#endif
danielk19772f886d12009-02-28 10:47:41 +0000514 assert( (pTrigger && tmask) || (pTrigger==0 && tmask==0) );
drhb7f91642004-10-31 02:22:47 +0000515
drhf573c992002-07-31 00:32:50 +0000516 /* If pTab is really a view, make sure it has been initialized.
danielk1977b3d24bf2006-06-19 03:05:10 +0000517 ** ViewGetColumnNames() is a no-op if pTab is not a view (or virtual
518 ** module table).
drhf573c992002-07-31 00:32:50 +0000519 */
danielk1977b3d24bf2006-06-19 03:05:10 +0000520 if( sqlite3ViewGetColumnNames(pParse, pTab) ){
drh5cf590c2003-04-24 01:45:04 +0000521 goto insert_cleanup;
drhf573c992002-07-31 00:32:50 +0000522 }
523
danielk1977595a5232009-07-24 17:58:53 +0000524 /* Ensure that:
525 * (a) the table is not read-only,
526 * (b) that if it is a view then ON INSERT triggers exist
527 */
528 if( sqlite3IsReadOnly(pParse, pTab, tmask) ){
529 goto insert_cleanup;
530 }
531
drh1ccde152000-06-17 13:12:39 +0000532 /* Allocate a VDBE
533 */
danielk19774adee202004-05-08 08:23:19 +0000534 v = sqlite3GetVdbe(pParse);
drh5974a302000-06-07 14:42:26 +0000535 if( v==0 ) goto insert_cleanup;
drh4794f732004-11-05 17:17:50 +0000536 if( pParse->nested==0 ) sqlite3VdbeCountChanges(v);
danielk19772f886d12009-02-28 10:47:41 +0000537 sqlite3BeginWriteOperation(pParse, pSelect || pTrigger, iDb);
drh1ccde152000-06-17 13:12:39 +0000538
danielk1977c3f9bad2002-05-15 08:30:12 +0000539 /* if there are row triggers, allocate a temp table for new.* references. */
danielk19772f886d12009-02-28 10:47:41 +0000540 if( pTrigger ){
danielk1977c3f9bad2002-05-15 08:30:12 +0000541 newIdx = pParse->nTab++;
danielk1977f29ce552002-05-19 23:43:12 +0000542 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000543
drh9d9cf222007-02-13 15:01:11 +0000544#ifndef SQLITE_OMIT_XFER_OPT
545 /* If the statement is of the form
546 **
547 ** INSERT INTO <table1> SELECT * FROM <table2>;
548 **
549 ** Then special optimizations can be applied that make the transfer
550 ** very fast and which reduce fragmentation of indices.
drhe00ee6e2008-06-20 15:24:01 +0000551 **
552 ** This is the 2nd template.
drh9d9cf222007-02-13 15:01:11 +0000553 */
554 if( pColumn==0 && xferOptimization(pParse, pTab, pSelect, onError, iDb) ){
danielk19772f886d12009-02-28 10:47:41 +0000555 assert( !pTrigger );
drh9d9cf222007-02-13 15:01:11 +0000556 assert( pList==0 );
drh0b9f50d2009-06-23 20:28:53 +0000557 goto insert_end;
drh9d9cf222007-02-13 15:01:11 +0000558 }
559#endif /* SQLITE_OMIT_XFER_OPT */
560
drh2958a4e2004-11-12 03:56:15 +0000561 /* If this is an AUTOINCREMENT table, look up the sequence number in the
drh6a288a32008-01-07 19:20:24 +0000562 ** sqlite_sequence table and store it in memory cell regAutoinc.
drh2958a4e2004-11-12 03:56:15 +0000563 */
drh6a288a32008-01-07 19:20:24 +0000564 regAutoinc = autoIncBegin(pParse, iDb, pTab);
drh2958a4e2004-11-12 03:56:15 +0000565
drh1ccde152000-06-17 13:12:39 +0000566 /* Figure out how many columns of data are supplied. If the data
drhe00ee6e2008-06-20 15:24:01 +0000567 ** is coming from a SELECT statement, then generate a co-routine that
568 ** produces a single row of the SELECT on each invocation. The
569 ** co-routine is the common header to the 3rd and 4th templates.
drh1ccde152000-06-17 13:12:39 +0000570 */
drh5974a302000-06-07 14:42:26 +0000571 if( pSelect ){
drh142e30d2002-08-28 03:00:58 +0000572 /* Data is coming from a SELECT. Generate code to implement that SELECT
drhe00ee6e2008-06-20 15:24:01 +0000573 ** as a co-routine. The code is common to both the 3rd and 4th
574 ** templates:
575 **
576 ** EOF <- 0
577 ** X <- A
578 ** goto B
579 ** A: setup for the SELECT
580 ** loop over the tables in the SELECT
581 ** load value into register R..R+n
582 ** yield X
583 ** end loop
584 ** cleanup after the SELECT
585 ** EOF <- 1
586 ** yield X
587 ** halt-error
588 **
589 ** On each invocation of the co-routine, it puts a single row of the
590 ** SELECT result into registers dest.iMem...dest.iMem+dest.nMem-1.
591 ** (These output registers are allocated by sqlite3Select().) When
592 ** the SELECT completes, it sets the EOF flag stored in regEof.
drh142e30d2002-08-28 03:00:58 +0000593 */
drhe00ee6e2008-06-20 15:24:01 +0000594 int rc, j1;
drh1013c932008-01-06 00:25:21 +0000595
drhe00ee6e2008-06-20 15:24:01 +0000596 regEof = ++pParse->nMem;
597 sqlite3VdbeAddOp2(v, OP_Integer, 0, regEof); /* EOF <- 0 */
598 VdbeComment((v, "SELECT eof flag"));
drh92b01d52008-06-24 00:32:35 +0000599 sqlite3SelectDestInit(&dest, SRT_Coroutine, ++pParse->nMem);
drhe00ee6e2008-06-20 15:24:01 +0000600 addrSelect = sqlite3VdbeCurrentAddr(v)+2;
drh92b01d52008-06-24 00:32:35 +0000601 sqlite3VdbeAddOp2(v, OP_Integer, addrSelect-1, dest.iParm);
drhe00ee6e2008-06-20 15:24:01 +0000602 j1 = sqlite3VdbeAddOp2(v, OP_Goto, 0, 0);
603 VdbeComment((v, "Jump over SELECT coroutine"));
danielk1977b3bce662005-01-29 08:32:43 +0000604
605 /* Resolve the expressions in the SELECT statement and execute it. */
drh7d10d5a2008-08-20 16:35:10 +0000606 rc = sqlite3Select(pParse, pSelect, &dest);
drh098d1682009-05-02 15:46:46 +0000607 assert( pParse->nErr==0 || rc );
608 if( rc || NEVER(pParse->nErr) || db->mallocFailed ){
drh6f7adc82006-01-11 21:41:20 +0000609 goto insert_cleanup;
610 }
drhe00ee6e2008-06-20 15:24:01 +0000611 sqlite3VdbeAddOp2(v, OP_Integer, 1, regEof); /* EOF <- 1 */
drh92b01d52008-06-24 00:32:35 +0000612 sqlite3VdbeAddOp1(v, OP_Yield, dest.iParm); /* yield X */
drhe00ee6e2008-06-20 15:24:01 +0000613 sqlite3VdbeAddOp2(v, OP_Halt, SQLITE_INTERNAL, OE_Abort);
614 VdbeComment((v, "End of SELECT coroutine"));
615 sqlite3VdbeJumpHere(v, j1); /* label B: */
danielk1977b3bce662005-01-29 08:32:43 +0000616
drh6a288a32008-01-07 19:20:24 +0000617 regFromSelect = dest.iMem;
drh5974a302000-06-07 14:42:26 +0000618 assert( pSelect->pEList );
drh967e8b72000-06-21 13:59:10 +0000619 nColumn = pSelect->pEList->nExpr;
drhe00ee6e2008-06-20 15:24:01 +0000620 assert( dest.nMem==nColumn );
drh142e30d2002-08-28 03:00:58 +0000621
622 /* Set useTempTable to TRUE if the result of the SELECT statement
drhe00ee6e2008-06-20 15:24:01 +0000623 ** should be written into a temporary table (template 4). Set to
624 ** FALSE if each* row of the SELECT can be written directly into
625 ** the destination table (template 3).
drh048c5302003-04-03 01:50:44 +0000626 **
627 ** A temp table must be used if the table being updated is also one
628 ** of the tables being read by the SELECT statement. Also use a
629 ** temp table in the case of row triggers.
drh142e30d2002-08-28 03:00:58 +0000630 */
danielk1977595a5232009-07-24 17:58:53 +0000631 if( pTrigger || readsTable(pParse, addrSelect, iDb, pTab) ){
drh048c5302003-04-03 01:50:44 +0000632 useTempTable = 1;
drh048c5302003-04-03 01:50:44 +0000633 }
drh142e30d2002-08-28 03:00:58 +0000634
635 if( useTempTable ){
drhe00ee6e2008-06-20 15:24:01 +0000636 /* Invoke the coroutine to extract information from the SELECT
637 ** and add it to a transient table srcTab. The code generated
638 ** here is from the 4th template:
639 **
640 ** B: open temp table
641 ** L: yield X
642 ** if EOF goto M
643 ** insert row from R..R+n into temp table
644 ** goto L
645 ** M: ...
drh142e30d2002-08-28 03:00:58 +0000646 */
drhdc5ea5c2008-12-10 17:19:59 +0000647 int regRec; /* Register to hold packed record */
648 int regTempRowid; /* Register to hold temp table ROWID */
649 int addrTop; /* Label "L" */
650 int addrIf; /* Address of jump to M */
drhb7654112008-01-12 12:48:07 +0000651
drh142e30d2002-08-28 03:00:58 +0000652 srcTab = pParse->nTab++;
drhb7654112008-01-12 12:48:07 +0000653 regRec = sqlite3GetTempReg(pParse);
drhdc5ea5c2008-12-10 17:19:59 +0000654 regTempRowid = sqlite3GetTempReg(pParse);
drhe00ee6e2008-06-20 15:24:01 +0000655 sqlite3VdbeAddOp2(v, OP_OpenEphemeral, srcTab, nColumn);
drh92b01d52008-06-24 00:32:35 +0000656 addrTop = sqlite3VdbeAddOp1(v, OP_Yield, dest.iParm);
drhe00ee6e2008-06-20 15:24:01 +0000657 addrIf = sqlite3VdbeAddOp1(v, OP_If, regEof);
drh1db639c2008-01-17 02:36:28 +0000658 sqlite3VdbeAddOp3(v, OP_MakeRecord, regFromSelect, nColumn, regRec);
drhdc5ea5c2008-12-10 17:19:59 +0000659 sqlite3VdbeAddOp2(v, OP_NewRowid, srcTab, regTempRowid);
660 sqlite3VdbeAddOp3(v, OP_Insert, srcTab, regRec, regTempRowid);
drhe00ee6e2008-06-20 15:24:01 +0000661 sqlite3VdbeAddOp2(v, OP_Goto, 0, addrTop);
662 sqlite3VdbeJumpHere(v, addrIf);
drhb7654112008-01-12 12:48:07 +0000663 sqlite3ReleaseTempReg(pParse, regRec);
drhdc5ea5c2008-12-10 17:19:59 +0000664 sqlite3ReleaseTempReg(pParse, regTempRowid);
drh142e30d2002-08-28 03:00:58 +0000665 }
drh5974a302000-06-07 14:42:26 +0000666 }else{
drh142e30d2002-08-28 03:00:58 +0000667 /* This is the case if the data for the INSERT is coming from a VALUES
668 ** clause
669 */
danielk1977b3bce662005-01-29 08:32:43 +0000670 NameContext sNC;
671 memset(&sNC, 0, sizeof(sNC));
672 sNC.pParse = pParse;
drh5974a302000-06-07 14:42:26 +0000673 srcTab = -1;
drh48d11782007-11-23 15:02:19 +0000674 assert( useTempTable==0 );
drh147d0cc2006-08-25 23:42:53 +0000675 nColumn = pList ? pList->nExpr : 0;
drhe64e7b22002-02-18 13:56:36 +0000676 for(i=0; i<nColumn; i++){
drh7d10d5a2008-08-20 16:35:10 +0000677 if( sqlite3ResolveExprNames(&sNC, pList->a[i].pExpr) ){
drhb04a5d82002-04-12 03:55:15 +0000678 goto insert_cleanup;
679 }
drhe64e7b22002-02-18 13:56:36 +0000680 }
drh5974a302000-06-07 14:42:26 +0000681 }
drh1ccde152000-06-17 13:12:39 +0000682
683 /* Make sure the number of columns in the source data matches the number
684 ** of columns to be inserted into the table.
685 */
danielk1977034ca142007-06-26 10:38:54 +0000686 if( IsVirtual(pTab) ){
687 for(i=0; i<pTab->nCol; i++){
688 nHidden += (IsHiddenColumn(&pTab->aCol[i]) ? 1 : 0);
689 }
690 }
691 if( pColumn==0 && nColumn && nColumn!=(pTab->nCol-nHidden) ){
danielk19774adee202004-05-08 08:23:19 +0000692 sqlite3ErrorMsg(pParse,
drhda93d232003-03-31 02:12:46 +0000693 "table %S has %d columns but %d values were supplied",
drhd51397a2009-05-01 15:17:48 +0000694 pTabList, 0, pTab->nCol-nHidden, nColumn);
drhcce7d172000-05-31 15:34:51 +0000695 goto insert_cleanup;
696 }
drh967e8b72000-06-21 13:59:10 +0000697 if( pColumn!=0 && nColumn!=pColumn->nId ){
danielk19774adee202004-05-08 08:23:19 +0000698 sqlite3ErrorMsg(pParse, "%d values for %d columns", nColumn, pColumn->nId);
drhcce7d172000-05-31 15:34:51 +0000699 goto insert_cleanup;
700 }
drh1ccde152000-06-17 13:12:39 +0000701
702 /* If the INSERT statement included an IDLIST term, then make sure
703 ** all elements of the IDLIST really are columns of the table and
704 ** remember the column indices.
drhc8392582001-12-31 02:48:51 +0000705 **
706 ** If the table has an INTEGER PRIMARY KEY column and that column
707 ** is named in the IDLIST, then record in the keyColumn variable
708 ** the index into IDLIST of the primary key column. keyColumn is
709 ** the index of the primary key as it appears in IDLIST, not as
710 ** is appears in the original table. (The index of the primary
711 ** key in the original table is pTab->iPKey.)
drh1ccde152000-06-17 13:12:39 +0000712 */
drh967e8b72000-06-21 13:59:10 +0000713 if( pColumn ){
714 for(i=0; i<pColumn->nId; i++){
715 pColumn->a[i].idx = -1;
drhcce7d172000-05-31 15:34:51 +0000716 }
drh967e8b72000-06-21 13:59:10 +0000717 for(i=0; i<pColumn->nId; i++){
drhcce7d172000-05-31 15:34:51 +0000718 for(j=0; j<pTab->nCol; j++){
danielk19774adee202004-05-08 08:23:19 +0000719 if( sqlite3StrICmp(pColumn->a[i].zName, pTab->aCol[j].zName)==0 ){
drh967e8b72000-06-21 13:59:10 +0000720 pColumn->a[i].idx = j;
drh4a324312001-12-21 14:30:42 +0000721 if( j==pTab->iPKey ){
drh9aa028d2001-12-22 21:48:29 +0000722 keyColumn = i;
drh4a324312001-12-21 14:30:42 +0000723 }
drhcce7d172000-05-31 15:34:51 +0000724 break;
725 }
726 }
727 if( j>=pTab->nCol ){
danielk19774adee202004-05-08 08:23:19 +0000728 if( sqlite3IsRowid(pColumn->a[i].zName) ){
drha0217ba2003-06-01 01:10:33 +0000729 keyColumn = i;
730 }else{
danielk19774adee202004-05-08 08:23:19 +0000731 sqlite3ErrorMsg(pParse, "table %S has no column named %s",
drha0217ba2003-06-01 01:10:33 +0000732 pTabList, 0, pColumn->a[i].zName);
733 pParse->nErr++;
734 goto insert_cleanup;
735 }
drhcce7d172000-05-31 15:34:51 +0000736 }
737 }
738 }
drh1ccde152000-06-17 13:12:39 +0000739
drhaacc5432002-01-06 17:07:40 +0000740 /* If there is no IDLIST term but the table has an integer primary
drhc8392582001-12-31 02:48:51 +0000741 ** key, the set the keyColumn variable to the primary key column index
742 ** in the original table definition.
drh4a324312001-12-21 14:30:42 +0000743 */
drh147d0cc2006-08-25 23:42:53 +0000744 if( pColumn==0 && nColumn>0 ){
drh4a324312001-12-21 14:30:42 +0000745 keyColumn = pTab->iPKey;
746 }
747
drh142e30d2002-08-28 03:00:58 +0000748 /* Open the temp table for FOR EACH ROW triggers
749 */
danielk19772f886d12009-02-28 10:47:41 +0000750 if( pTrigger ){
danielk1977d336e222009-02-20 10:58:41 +0000751 sqlite3VdbeAddOp3(v, OP_OpenPseudo, newIdx, 0, pTab->nCol);
danielk1977f29ce552002-05-19 23:43:12 +0000752 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000753
drhfeeb1392002-04-09 03:28:01 +0000754 /* Initialize the count of rows to be inserted
755 */
drh142e30d2002-08-28 03:00:58 +0000756 if( db->flags & SQLITE_CountRows ){
drh6a288a32008-01-07 19:20:24 +0000757 regRowCount = ++pParse->nMem;
758 sqlite3VdbeAddOp2(v, OP_Integer, 0, regRowCount);
drhfeeb1392002-04-09 03:28:01 +0000759 }
760
danielk1977e448dc42008-01-02 11:50:51 +0000761 /* If this is not a view, open the table and and all indices */
762 if( !isView ){
drhaa9b8962008-01-08 02:57:55 +0000763 int nIdx;
drhaa9b8962008-01-08 02:57:55 +0000764
drh04adf412008-01-08 18:57:50 +0000765 baseCur = pParse->nTab;
766 nIdx = sqlite3OpenTableAndIndices(pParse, pTab, baseCur, OP_OpenWrite);
drh5c070532008-04-11 15:36:03 +0000767 aRegIdx = sqlite3DbMallocRaw(db, sizeof(int)*(nIdx+1));
drhaa9b8962008-01-08 02:57:55 +0000768 if( aRegIdx==0 ){
769 goto insert_cleanup;
770 }
771 for(i=0; i<nIdx; i++){
772 aRegIdx[i] = ++pParse->nMem;
773 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000774 }
775
drhe00ee6e2008-06-20 15:24:01 +0000776 /* This is the top of the main insertion loop */
drh142e30d2002-08-28 03:00:58 +0000777 if( useTempTable ){
drhe00ee6e2008-06-20 15:24:01 +0000778 /* This block codes the top of loop only. The complete loop is the
779 ** following pseudocode (template 4):
780 **
781 ** rewind temp table
782 ** C: loop over rows of intermediate table
783 ** transfer values form intermediate table into <table>
784 ** end loop
785 ** D: ...
786 */
787 addrInsTop = sqlite3VdbeAddOp1(v, OP_Rewind, srcTab);
788 addrCont = sqlite3VdbeCurrentAddr(v);
drh142e30d2002-08-28 03:00:58 +0000789 }else if( pSelect ){
drhe00ee6e2008-06-20 15:24:01 +0000790 /* This block codes the top of loop only. The complete loop is the
791 ** following pseudocode (template 3):
792 **
793 ** C: yield X
794 ** if EOF goto D
795 ** insert the select result into <table> from R..R+n
796 ** goto C
797 ** D: ...
798 */
drh92b01d52008-06-24 00:32:35 +0000799 addrCont = sqlite3VdbeAddOp1(v, OP_Yield, dest.iParm);
drhe00ee6e2008-06-20 15:24:01 +0000800 addrInsTop = sqlite3VdbeAddOp1(v, OP_If, regEof);
drh5974a302000-06-07 14:42:26 +0000801 }
drh1ccde152000-06-17 13:12:39 +0000802
drh6a288a32008-01-07 19:20:24 +0000803 /* Allocate registers for holding the rowid of the new row,
804 ** the content of the new row, and the assemblied row record.
805 */
806 regRecord = ++pParse->nMem;
807 regRowid = regIns = pParse->nMem+1;
808 pParse->nMem += pTab->nCol + 1;
809 if( IsVirtual(pTab) ){
810 regRowid++;
811 pParse->nMem++;
812 }
813 regData = regRowid+1;
814
drh5cf590c2003-04-24 01:45:04 +0000815 /* Run the BEFORE and INSTEAD OF triggers, if there are any
drh70ce3f02003-04-15 19:22:22 +0000816 */
danielk19774adee202004-05-08 08:23:19 +0000817 endOfLoop = sqlite3VdbeMakeLabel(v);
danielk19772f886d12009-02-28 10:47:41 +0000818 if( tmask & TRIGGER_BEFORE ){
drhdc5ea5c2008-12-10 17:19:59 +0000819 int regTrigRowid;
drh2d401ab2008-01-10 23:50:11 +0000820 int regCols;
821 int regRec;
danielk1977c3f9bad2002-05-15 08:30:12 +0000822
drh70ce3f02003-04-15 19:22:22 +0000823 /* build the NEW.* reference row. Note that if there is an INTEGER
824 ** PRIMARY KEY into which a NULL is being inserted, that NULL will be
825 ** translated into a unique ID for the row. But on a BEFORE trigger,
826 ** we do not know what the unique ID will be (because the insert has
827 ** not happened yet) so we substitute a rowid of -1
828 */
drhdc5ea5c2008-12-10 17:19:59 +0000829 regTrigRowid = sqlite3GetTempReg(pParse);
drh70ce3f02003-04-15 19:22:22 +0000830 if( keyColumn<0 ){
drhdc5ea5c2008-12-10 17:19:59 +0000831 sqlite3VdbeAddOp2(v, OP_Integer, -1, regTrigRowid);
drh70ce3f02003-04-15 19:22:22 +0000832 }else{
drh6a288a32008-01-07 19:20:24 +0000833 int j1;
drh7fe45902009-05-01 02:08:04 +0000834 if( useTempTable ){
835 sqlite3VdbeAddOp3(v, OP_Column, srcTab, keyColumn, regTrigRowid);
836 }else{
837 assert( pSelect==0 ); /* Otherwise useTempTable is true */
838 sqlite3ExprCode(pParse, pList->a[keyColumn].pExpr, regTrigRowid);
839 }
drhdc5ea5c2008-12-10 17:19:59 +0000840 j1 = sqlite3VdbeAddOp1(v, OP_NotNull, regTrigRowid);
841 sqlite3VdbeAddOp2(v, OP_Integer, -1, regTrigRowid);
drh6a288a32008-01-07 19:20:24 +0000842 sqlite3VdbeJumpHere(v, j1);
drhdc5ea5c2008-12-10 17:19:59 +0000843 sqlite3VdbeAddOp1(v, OP_MustBeInt, regTrigRowid);
drh70ce3f02003-04-15 19:22:22 +0000844 }
845
danielk1977034ca142007-06-26 10:38:54 +0000846 /* Cannot have triggers on a virtual table. If it were possible,
847 ** this block would have to account for hidden column.
848 */
849 assert(!IsVirtual(pTab));
850
drh70ce3f02003-04-15 19:22:22 +0000851 /* Create the new column data
852 */
drh2d401ab2008-01-10 23:50:11 +0000853 regCols = sqlite3GetTempRange(pParse, pTab->nCol);
danielk1977c3f9bad2002-05-15 08:30:12 +0000854 for(i=0; i<pTab->nCol; i++){
855 if( pColumn==0 ){
drh9adf9ac2002-05-15 11:44:13 +0000856 j = i;
danielk1977c3f9bad2002-05-15 08:30:12 +0000857 }else{
drh9adf9ac2002-05-15 11:44:13 +0000858 for(j=0; j<pColumn->nId; j++){
859 if( pColumn->a[j].idx==i ) break;
860 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000861 }
862 if( pColumn && j>=pColumn->nId ){
drh2d401ab2008-01-10 23:50:11 +0000863 sqlite3ExprCode(pParse, pTab->aCol[i].pDflt, regCols+i);
drh142e30d2002-08-28 03:00:58 +0000864 }else if( useTempTable ){
drh2d401ab2008-01-10 23:50:11 +0000865 sqlite3VdbeAddOp3(v, OP_Column, srcTab, j, regCols+i);
danielk1977c3f9bad2002-05-15 08:30:12 +0000866 }else{
drhd6fe9612005-01-14 01:22:00 +0000867 assert( pSelect==0 ); /* Otherwise useTempTable is true */
drh2d401ab2008-01-10 23:50:11 +0000868 sqlite3ExprCodeAndCache(pParse, pList->a[j].pExpr, regCols+i);
danielk1977c3f9bad2002-05-15 08:30:12 +0000869 }
870 }
drh2d401ab2008-01-10 23:50:11 +0000871 regRec = sqlite3GetTempReg(pParse);
drh1db639c2008-01-17 02:36:28 +0000872 sqlite3VdbeAddOp3(v, OP_MakeRecord, regCols, pTab->nCol, regRec);
danielk1977a37cdde2004-05-16 11:15:36 +0000873
874 /* If this is an INSERT on a view with an INSTEAD OF INSERT trigger,
875 ** do not attempt any conversions before assembling the record.
876 ** If this is a real table, attempt conversions as required by the
877 ** table column affinities.
878 */
879 if( !isView ){
880 sqlite3TableAffinityStr(v, pTab);
881 }
drhdc5ea5c2008-12-10 17:19:59 +0000882 sqlite3VdbeAddOp3(v, OP_Insert, newIdx, regRec, regTrigRowid);
drh2d401ab2008-01-10 23:50:11 +0000883 sqlite3ReleaseTempReg(pParse, regRec);
drhdc5ea5c2008-12-10 17:19:59 +0000884 sqlite3ReleaseTempReg(pParse, regTrigRowid);
drh2d401ab2008-01-10 23:50:11 +0000885 sqlite3ReleaseTempRange(pParse, regCols, pTab->nCol);
danielk1977c3f9bad2002-05-15 08:30:12 +0000886
drh5cf590c2003-04-24 01:45:04 +0000887 /* Fire BEFORE or INSTEAD OF triggers */
danielk19772f886d12009-02-28 10:47:41 +0000888 if( sqlite3CodeRowTrigger(pParse, pTrigger, TK_INSERT, 0, TRIGGER_BEFORE,
889 pTab, newIdx, -1, onError, endOfLoop, 0, 0) ){
danielk1977f29ce552002-05-19 23:43:12 +0000890 goto insert_cleanup;
891 }
drh70ce3f02003-04-15 19:22:22 +0000892 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000893
drh4a324312001-12-21 14:30:42 +0000894 /* Push the record number for the new entry onto the stack. The
drhf0863fe2005-06-12 21:35:51 +0000895 ** record number is a randomly generate integer created by NewRowid
drh4a324312001-12-21 14:30:42 +0000896 ** except when the table has an INTEGER PRIMARY KEY column, in which
drhb419a922002-01-30 16:17:23 +0000897 ** case the record number is the same as that column.
drh1ccde152000-06-17 13:12:39 +0000898 */
drh5cf590c2003-04-24 01:45:04 +0000899 if( !isView ){
drh4cbdda92006-06-14 19:00:20 +0000900 if( IsVirtual(pTab) ){
danielk1977287fb612008-01-04 19:10:28 +0000901 /* The row that the VUpdate opcode will delete: none */
drh6a288a32008-01-07 19:20:24 +0000902 sqlite3VdbeAddOp2(v, OP_Null, 0, regIns);
drh4cbdda92006-06-14 19:00:20 +0000903 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000904 if( keyColumn>=0 ){
drh142e30d2002-08-28 03:00:58 +0000905 if( useTempTable ){
drh6a288a32008-01-07 19:20:24 +0000906 sqlite3VdbeAddOp3(v, OP_Column, srcTab, keyColumn, regRowid);
drh142e30d2002-08-28 03:00:58 +0000907 }else if( pSelect ){
drhb7654112008-01-12 12:48:07 +0000908 sqlite3VdbeAddOp2(v, OP_SCopy, regFromSelect+keyColumn, regRowid);
danielk1977c3f9bad2002-05-15 08:30:12 +0000909 }else{
drhe4d90812007-03-29 05:51:49 +0000910 VdbeOp *pOp;
drh1db639c2008-01-17 02:36:28 +0000911 sqlite3ExprCode(pParse, pList->a[keyColumn].pExpr, regRowid);
drh20411ea2009-05-29 19:00:12 +0000912 pOp = sqlite3VdbeGetOp(v, -1);
drh1b7ecbb2009-05-03 01:00:59 +0000913 if( ALWAYS(pOp) && pOp->opcode==OP_Null && !IsVirtual(pTab) ){
drhe4d90812007-03-29 05:51:49 +0000914 appendFlag = 1;
915 pOp->opcode = OP_NewRowid;
drh04adf412008-01-08 18:57:50 +0000916 pOp->p1 = baseCur;
drh6a288a32008-01-07 19:20:24 +0000917 pOp->p2 = regRowid;
918 pOp->p3 = regAutoinc;
drhe4d90812007-03-29 05:51:49 +0000919 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000920 }
drhf0863fe2005-06-12 21:35:51 +0000921 /* If the PRIMARY KEY expression is NULL, then use OP_NewRowid
drh27a32782002-06-19 20:32:43 +0000922 ** to generate a unique primary key value.
923 */
drhe4d90812007-03-29 05:51:49 +0000924 if( !appendFlag ){
drh1db639c2008-01-17 02:36:28 +0000925 int j1;
danielk1977bb50e7a2008-07-04 10:56:07 +0000926 if( !IsVirtual(pTab) ){
927 j1 = sqlite3VdbeAddOp1(v, OP_NotNull, regRowid);
928 sqlite3VdbeAddOp3(v, OP_NewRowid, baseCur, regRowid, regAutoinc);
929 sqlite3VdbeJumpHere(v, j1);
930 }else{
931 j1 = sqlite3VdbeCurrentAddr(v);
932 sqlite3VdbeAddOp2(v, OP_IsNull, regRowid, j1+2);
933 }
drh3c84ddf2008-01-09 02:15:38 +0000934 sqlite3VdbeAddOp1(v, OP_MustBeInt, regRowid);
drhe4d90812007-03-29 05:51:49 +0000935 }
drh4cbdda92006-06-14 19:00:20 +0000936 }else if( IsVirtual(pTab) ){
drh6a288a32008-01-07 19:20:24 +0000937 sqlite3VdbeAddOp2(v, OP_Null, 0, regRowid);
danielk1977c3f9bad2002-05-15 08:30:12 +0000938 }else{
drh04adf412008-01-08 18:57:50 +0000939 sqlite3VdbeAddOp3(v, OP_NewRowid, baseCur, regRowid, regAutoinc);
drhe4d90812007-03-29 05:51:49 +0000940 appendFlag = 1;
drh4a324312001-12-21 14:30:42 +0000941 }
drh6a288a32008-01-07 19:20:24 +0000942 autoIncStep(pParse, regAutoinc, regRowid);
drh4a324312001-12-21 14:30:42 +0000943
danielk1977c3f9bad2002-05-15 08:30:12 +0000944 /* Push onto the stack, data for all columns of the new entry, beginning
danielk1977f29ce552002-05-19 23:43:12 +0000945 ** with the first column.
946 */
danielk1977034ca142007-06-26 10:38:54 +0000947 nHidden = 0;
danielk1977c3f9bad2002-05-15 08:30:12 +0000948 for(i=0; i<pTab->nCol; i++){
drh6a288a32008-01-07 19:20:24 +0000949 int iRegStore = regRowid+1+i;
danielk1977c3f9bad2002-05-15 08:30:12 +0000950 if( i==pTab->iPKey ){
drh9adf9ac2002-05-15 11:44:13 +0000951 /* The value of the INTEGER PRIMARY KEY column is always a NULL.
danielk1977f29ce552002-05-19 23:43:12 +0000952 ** Whenever this column is read, the record number will be substituted
953 ** in its place. So will fill this column with a NULL to avoid
954 ** taking up data space with information that will never be used. */
drh4c583122008-01-04 22:01:03 +0000955 sqlite3VdbeAddOp2(v, OP_Null, 0, iRegStore);
drh9adf9ac2002-05-15 11:44:13 +0000956 continue;
danielk1977c3f9bad2002-05-15 08:30:12 +0000957 }
958 if( pColumn==0 ){
danielk1977034ca142007-06-26 10:38:54 +0000959 if( IsHiddenColumn(&pTab->aCol[i]) ){
960 assert( IsVirtual(pTab) );
961 j = -1;
962 nHidden++;
963 }else{
964 j = i - nHidden;
965 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000966 }else{
drh9adf9ac2002-05-15 11:44:13 +0000967 for(j=0; j<pColumn->nId; j++){
968 if( pColumn->a[j].idx==i ) break;
969 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000970 }
danielk1977034ca142007-06-26 10:38:54 +0000971 if( j<0 || nColumn==0 || (pColumn && j>=pColumn->nId) ){
danielk1977287fb612008-01-04 19:10:28 +0000972 sqlite3ExprCode(pParse, pTab->aCol[i].pDflt, iRegStore);
drh142e30d2002-08-28 03:00:58 +0000973 }else if( useTempTable ){
danielk1977287fb612008-01-04 19:10:28 +0000974 sqlite3VdbeAddOp3(v, OP_Column, srcTab, j, iRegStore);
drh142e30d2002-08-28 03:00:58 +0000975 }else if( pSelect ){
drhb7654112008-01-12 12:48:07 +0000976 sqlite3VdbeAddOp2(v, OP_SCopy, regFromSelect+j, iRegStore);
danielk1977c3f9bad2002-05-15 08:30:12 +0000977 }else{
danielk1977287fb612008-01-04 19:10:28 +0000978 sqlite3ExprCode(pParse, pList->a[j].pExpr, iRegStore);
drh5974a302000-06-07 14:42:26 +0000979 }
drhbed86902000-06-02 13:27:59 +0000980 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000981
982 /* Generate code to check constraints and generate index keys and
danielk1977f29ce552002-05-19 23:43:12 +0000983 ** do the insertion.
984 */
drh4cbdda92006-06-14 19:00:20 +0000985#ifndef SQLITE_OMIT_VIRTUALTABLE
986 if( IsVirtual(pTab) ){
danielk1977595a5232009-07-24 17:58:53 +0000987 const char *pVTab = (const char *)sqlite3GetVTable(db, pTab);
drh4f3dd152008-04-28 18:46:43 +0000988 sqlite3VtabMakeWritable(pParse, pTab);
danielk1977595a5232009-07-24 17:58:53 +0000989 sqlite3VdbeAddOp4(v, OP_VUpdate, 1, pTab->nCol+2, regIns, pVTab, P4_VTAB);
drh4cbdda92006-06-14 19:00:20 +0000990 }else
991#endif
992 {
danielk1977de630352009-05-04 11:42:29 +0000993 int isReplace; /* Set to true if constraints may cause a replace */
994 sqlite3GenerateConstraintChecks(pParse, pTab, baseCur, regIns, aRegIdx,
995 keyColumn>=0, 0, onError, endOfLoop, &isReplace
drh04adf412008-01-08 18:57:50 +0000996 );
997 sqlite3CompleteInsertion(
danielk1977de630352009-05-04 11:42:29 +0000998 pParse, pTab, baseCur, regIns, aRegIdx, 0,
999 (tmask&TRIGGER_AFTER) ? newIdx : -1, appendFlag, isReplace==0
1000 );
drh4cbdda92006-06-14 19:00:20 +00001001 }
drh5cf590c2003-04-24 01:45:04 +00001002 }
danielk1977c3f9bad2002-05-15 08:30:12 +00001003
drh5cf590c2003-04-24 01:45:04 +00001004 /* Update the count of rows that are inserted
1005 */
1006 if( (db->flags & SQLITE_CountRows)!=0 ){
drh6a288a32008-01-07 19:20:24 +00001007 sqlite3VdbeAddOp2(v, OP_AddImm, regRowCount, 1);
drh5974a302000-06-07 14:42:26 +00001008 }
drh1ccde152000-06-17 13:12:39 +00001009
danielk19772f886d12009-02-28 10:47:41 +00001010 if( pTrigger ){
danielk1977c3f9bad2002-05-15 08:30:12 +00001011 /* Code AFTER triggers */
danielk19772f886d12009-02-28 10:47:41 +00001012 if( sqlite3CodeRowTrigger(pParse, pTrigger, TK_INSERT, 0, TRIGGER_AFTER,
1013 pTab, newIdx, -1, onError, endOfLoop, 0, 0) ){
danielk1977f29ce552002-05-19 23:43:12 +00001014 goto insert_cleanup;
1015 }
drh1bee3d72001-10-15 00:44:35 +00001016 }
1017
drhe00ee6e2008-06-20 15:24:01 +00001018 /* The bottom of the main insertion loop, if the data source
1019 ** is a SELECT statement.
danielk1977f29ce552002-05-19 23:43:12 +00001020 */
danielk19774adee202004-05-08 08:23:19 +00001021 sqlite3VdbeResolveLabel(v, endOfLoop);
drh142e30d2002-08-28 03:00:58 +00001022 if( useTempTable ){
drhe00ee6e2008-06-20 15:24:01 +00001023 sqlite3VdbeAddOp2(v, OP_Next, srcTab, addrCont);
1024 sqlite3VdbeJumpHere(v, addrInsTop);
drh2eb95372008-06-06 15:04:36 +00001025 sqlite3VdbeAddOp1(v, OP_Close, srcTab);
drh142e30d2002-08-28 03:00:58 +00001026 }else if( pSelect ){
drhe00ee6e2008-06-20 15:24:01 +00001027 sqlite3VdbeAddOp2(v, OP_Goto, 0, addrCont);
1028 sqlite3VdbeJumpHere(v, addrInsTop);
drh6b563442001-11-07 16:48:26 +00001029 }
danielk1977c3f9bad2002-05-15 08:30:12 +00001030
danielk1977e448dc42008-01-02 11:50:51 +00001031 if( !IsVirtual(pTab) && !isView ){
danielk1977c3f9bad2002-05-15 08:30:12 +00001032 /* Close all tables opened */
drh2eb95372008-06-06 15:04:36 +00001033 sqlite3VdbeAddOp1(v, OP_Close, baseCur);
danielk1977c3f9bad2002-05-15 08:30:12 +00001034 for(idx=1, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, idx++){
drh2eb95372008-06-06 15:04:36 +00001035 sqlite3VdbeAddOp1(v, OP_Close, idx+baseCur);
danielk1977c3f9bad2002-05-15 08:30:12 +00001036 }
drhcce7d172000-05-31 15:34:51 +00001037 }
danielk1977c3f9bad2002-05-15 08:30:12 +00001038
drh0b9f50d2009-06-23 20:28:53 +00001039insert_end:
drhf3388142004-11-13 03:48:06 +00001040 /* Update the sqlite_sequence table by storing the content of the
drh0b9f50d2009-06-23 20:28:53 +00001041 ** maximum rowid counter values recorded while inserting into
1042 ** autoincrement tables.
drh2958a4e2004-11-12 03:56:15 +00001043 */
drh0b9f50d2009-06-23 20:28:53 +00001044 if( pParse->nested==0 && pParse->trigStack==0 ){
1045 sqlite3AutoincrementEnd(pParse);
1046 }
drh2958a4e2004-11-12 03:56:15 +00001047
drh1bee3d72001-10-15 00:44:35 +00001048 /*
danielk1977e7de6f22004-11-05 06:02:06 +00001049 ** Return the number of rows inserted. If this routine is
1050 ** generating code because of a call to sqlite3NestedParse(), do not
1051 ** invoke the callback function.
drh1bee3d72001-10-15 00:44:35 +00001052 */
danielk1977cc6bd382005-01-10 02:48:49 +00001053 if( db->flags & SQLITE_CountRows && pParse->nested==0 && !pParse->trigStack ){
drh6a288a32008-01-07 19:20:24 +00001054 sqlite3VdbeAddOp2(v, OP_ResultRow, regRowCount, 1);
danielk197722322fd2004-05-25 23:35:17 +00001055 sqlite3VdbeSetNumCols(v, 1);
danielk197710fb7492008-10-31 10:53:22 +00001056 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "rows inserted", SQLITE_STATIC);
drh1bee3d72001-10-15 00:44:35 +00001057 }
drhcce7d172000-05-31 15:34:51 +00001058
1059insert_cleanup:
drh633e6d52008-07-28 19:34:53 +00001060 sqlite3SrcListDelete(db, pTabList);
1061 sqlite3ExprListDelete(db, pList);
1062 sqlite3SelectDelete(db, pSelect);
1063 sqlite3IdListDelete(db, pColumn);
1064 sqlite3DbFree(db, aRegIdx);
drhcce7d172000-05-31 15:34:51 +00001065}
drh9cfcf5d2002-01-29 18:41:24 +00001066
drh9cfcf5d2002-01-29 18:41:24 +00001067/*
drh6a288a32008-01-07 19:20:24 +00001068** Generate code to do constraint checks prior to an INSERT or an UPDATE.
drh9cfcf5d2002-01-29 18:41:24 +00001069**
drh04adf412008-01-08 18:57:50 +00001070** The input is a range of consecutive registers as follows:
drh0ca3e242002-01-29 23:07:02 +00001071**
drhf0863fe2005-06-12 21:35:51 +00001072** 1. The rowid of the row to be updated before the update. This
drhb419a922002-01-30 16:17:23 +00001073** value is omitted unless we are doing an UPDATE that involves a
drha05a7222008-01-19 03:35:58 +00001074** change to the record number or writing to a virtual table.
drh0ca3e242002-01-29 23:07:02 +00001075**
drhf0863fe2005-06-12 21:35:51 +00001076** 2. The rowid of the row after the update.
drh0ca3e242002-01-29 23:07:02 +00001077**
1078** 3. The data in the first column of the entry after the update.
1079**
1080** i. Data from middle columns...
1081**
1082** N. The data in the last column of the entry after the update.
1083**
drh04adf412008-01-08 18:57:50 +00001084** The regRowid parameter is the index of the register containing (2).
1085**
drhf0863fe2005-06-12 21:35:51 +00001086** The old rowid shown as entry (1) above is omitted unless both isUpdate
1087** and rowidChng are 1. isUpdate is true for UPDATEs and false for
drha05a7222008-01-19 03:35:58 +00001088** INSERTs. RowidChng means that the new rowid is explicitly specified by
1089** the update or insert statement. If rowidChng is false, it means that
1090** the rowid is computed automatically in an insert or that the rowid value
1091** is not modified by the update.
drh0ca3e242002-01-29 23:07:02 +00001092**
drhaa9b8962008-01-08 02:57:55 +00001093** The code generated by this routine store new index entries into
1094** registers identified by aRegIdx[]. No index entry is created for
1095** indices where aRegIdx[i]==0. The order of indices in aRegIdx[] is
1096** the same as the order of indices on the linked list of indices
1097** attached to the table.
drh9cfcf5d2002-01-29 18:41:24 +00001098**
1099** This routine also generates code to check constraints. NOT NULL,
1100** CHECK, and UNIQUE constraints are all checked. If a constraint fails,
drh1c928532002-01-31 15:54:21 +00001101** then the appropriate action is performed. There are five possible
1102** actions: ROLLBACK, ABORT, FAIL, REPLACE, and IGNORE.
drh9cfcf5d2002-01-29 18:41:24 +00001103**
1104** Constraint type Action What Happens
1105** --------------- ---------- ----------------------------------------
drh1c928532002-01-31 15:54:21 +00001106** any ROLLBACK The current transaction is rolled back and
danielk197724b03fd2004-05-10 10:34:34 +00001107** sqlite3_exec() returns immediately with a
drh9cfcf5d2002-01-29 18:41:24 +00001108** return code of SQLITE_CONSTRAINT.
1109**
drh1c928532002-01-31 15:54:21 +00001110** any ABORT Back out changes from the current command
1111** only (do not do a complete rollback) then
danielk197724b03fd2004-05-10 10:34:34 +00001112** cause sqlite3_exec() to return immediately
drh1c928532002-01-31 15:54:21 +00001113** with SQLITE_CONSTRAINT.
1114**
1115** any FAIL Sqlite_exec() returns immediately with a
1116** return code of SQLITE_CONSTRAINT. The
1117** transaction is not rolled back and any
1118** prior changes are retained.
1119**
drh9cfcf5d2002-01-29 18:41:24 +00001120** any IGNORE The record number and data is popped from
1121** the stack and there is an immediate jump
1122** to label ignoreDest.
1123**
1124** NOT NULL REPLACE The NULL value is replace by the default
1125** value for that column. If the default value
1126** is NULL, the action is the same as ABORT.
1127**
1128** UNIQUE REPLACE The other row that conflicts with the row
1129** being inserted is removed.
1130**
1131** CHECK REPLACE Illegal. The results in an exception.
1132**
drh1c928532002-01-31 15:54:21 +00001133** Which action to take is determined by the overrideError parameter.
1134** Or if overrideError==OE_Default, then the pParse->onError parameter
1135** is used. Or if pParse->onError==OE_Default then the onError value
1136** for the constraint is used.
drh9cfcf5d2002-01-29 18:41:24 +00001137**
drhaaab5722002-02-19 13:39:21 +00001138** The calling routine must open a read/write cursor for pTab with
drh04adf412008-01-08 18:57:50 +00001139** cursor number "baseCur". All indices of pTab must also have open
1140** read/write cursors with cursor number baseCur+i for the i-th cursor.
drh9cfcf5d2002-01-29 18:41:24 +00001141** Except, if there is no possibility of a REPLACE action then
drhaa9b8962008-01-08 02:57:55 +00001142** cursors do not need to be open for indices where aRegIdx[i]==0.
drh9cfcf5d2002-01-29 18:41:24 +00001143*/
danielk19774adee202004-05-08 08:23:19 +00001144void sqlite3GenerateConstraintChecks(
drh9cfcf5d2002-01-29 18:41:24 +00001145 Parse *pParse, /* The parser context */
1146 Table *pTab, /* the table into which we are inserting */
drh04adf412008-01-08 18:57:50 +00001147 int baseCur, /* Index of a read/write cursor pointing at pTab */
1148 int regRowid, /* Index of the range of input registers */
drhaa9b8962008-01-08 02:57:55 +00001149 int *aRegIdx, /* Register used by each index. 0 for unused indices */
drha05a7222008-01-19 03:35:58 +00001150 int rowidChng, /* True if the rowid might collide with existing entry */
drhb419a922002-01-30 16:17:23 +00001151 int isUpdate, /* True for UPDATE, False for INSERT */
drh9cfcf5d2002-01-29 18:41:24 +00001152 int overrideError, /* Override onError to this if not OE_Default */
danielk1977de630352009-05-04 11:42:29 +00001153 int ignoreDest, /* Jump to this label on an OE_Ignore resolution */
1154 int *pbMayReplace /* OUT: Set to true if constraint may cause a replace */
drh9cfcf5d2002-01-29 18:41:24 +00001155){
drh1b7ecbb2009-05-03 01:00:59 +00001156 int i; /* loop counter */
1157 Vdbe *v; /* VDBE under constrution */
1158 int nCol; /* Number of columns */
1159 int onError; /* Conflict resolution strategy */
drh1bd10f82008-12-10 21:19:56 +00001160 int j1; /* Addresss of jump instruction */
1161 int j2 = 0, j3; /* Addresses of jump instructions */
drh04adf412008-01-08 18:57:50 +00001162 int regData; /* Register containing first data column */
drh1b7ecbb2009-05-03 01:00:59 +00001163 int iCur; /* Table cursor number */
1164 Index *pIdx; /* Pointer to one of the indices */
1165 int seenReplace = 0; /* True if REPLACE is used to resolve INT PK conflict */
drhf0863fe2005-06-12 21:35:51 +00001166 int hasTwoRowids = (isUpdate && rowidChng);
drh9cfcf5d2002-01-29 18:41:24 +00001167
danielk19774adee202004-05-08 08:23:19 +00001168 v = sqlite3GetVdbe(pParse);
drh9cfcf5d2002-01-29 18:41:24 +00001169 assert( v!=0 );
drh417be792002-03-03 18:59:40 +00001170 assert( pTab->pSelect==0 ); /* This table is not a VIEW */
drh9cfcf5d2002-01-29 18:41:24 +00001171 nCol = pTab->nCol;
drh04adf412008-01-08 18:57:50 +00001172 regData = regRowid + 1;
drh9cfcf5d2002-01-29 18:41:24 +00001173
drhaa9b8962008-01-08 02:57:55 +00001174
drh9cfcf5d2002-01-29 18:41:24 +00001175 /* Test all NOT NULL constraints.
1176 */
1177 for(i=0; i<nCol; i++){
drh0ca3e242002-01-29 23:07:02 +00001178 if( i==pTab->iPKey ){
drh0ca3e242002-01-29 23:07:02 +00001179 continue;
1180 }
drh9cfcf5d2002-01-29 18:41:24 +00001181 onError = pTab->aCol[i].notNull;
drh0ca3e242002-01-29 23:07:02 +00001182 if( onError==OE_None ) continue;
drh9cfcf5d2002-01-29 18:41:24 +00001183 if( overrideError!=OE_Default ){
1184 onError = overrideError;
drha996e472003-05-16 02:30:27 +00001185 }else if( onError==OE_Default ){
1186 onError = OE_Abort;
drh9cfcf5d2002-01-29 18:41:24 +00001187 }
danielk19777977a172004-11-09 12:44:37 +00001188 if( onError==OE_Replace && pTab->aCol[i].pDflt==0 ){
drh9cfcf5d2002-01-29 18:41:24 +00001189 onError = OE_Abort;
1190 }
danielk1977b84f96f2005-01-20 11:32:23 +00001191 assert( onError==OE_Rollback || onError==OE_Abort || onError==OE_Fail
1192 || onError==OE_Ignore || onError==OE_Replace );
drh9cfcf5d2002-01-29 18:41:24 +00001193 switch( onError ){
drh1c928532002-01-31 15:54:21 +00001194 case OE_Rollback:
1195 case OE_Abort:
1196 case OE_Fail: {
drhf089aa42008-07-08 19:34:06 +00001197 char *zMsg;
drh5053a792009-02-20 03:02:23 +00001198 j1 = sqlite3VdbeAddOp3(v, OP_HaltIfNull,
1199 SQLITE_CONSTRAINT, onError, regData+i);
drhf089aa42008-07-08 19:34:06 +00001200 zMsg = sqlite3MPrintf(pParse->db, "%s.%s may not be NULL",
1201 pTab->zName, pTab->aCol[i].zName);
drh66a51672008-01-03 00:01:23 +00001202 sqlite3VdbeChangeP4(v, -1, zMsg, P4_DYNAMIC);
drh9cfcf5d2002-01-29 18:41:24 +00001203 break;
1204 }
1205 case OE_Ignore: {
drh5053a792009-02-20 03:02:23 +00001206 sqlite3VdbeAddOp2(v, OP_IsNull, regData+i, ignoreDest);
drh9cfcf5d2002-01-29 18:41:24 +00001207 break;
1208 }
drh098d1682009-05-02 15:46:46 +00001209 default: {
1210 assert( onError==OE_Replace );
drh5053a792009-02-20 03:02:23 +00001211 j1 = sqlite3VdbeAddOp1(v, OP_NotNull, regData+i);
drh04adf412008-01-08 18:57:50 +00001212 sqlite3ExprCode(pParse, pTab->aCol[i].pDflt, regData+i);
drh5053a792009-02-20 03:02:23 +00001213 sqlite3VdbeJumpHere(v, j1);
drh9cfcf5d2002-01-29 18:41:24 +00001214 break;
1215 }
drh9cfcf5d2002-01-29 18:41:24 +00001216 }
1217 }
1218
1219 /* Test all CHECK constraints
1220 */
drhffe07b22005-11-03 00:41:17 +00001221#ifndef SQLITE_OMIT_CHECK
drh0cd2d4c2005-11-03 02:15:02 +00001222 if( pTab->pCheck && (pParse->db->flags & SQLITE_IgnoreChecks)==0 ){
drhffe07b22005-11-03 00:41:17 +00001223 int allOk = sqlite3VdbeMakeLabel(v);
drhaa9b8962008-01-08 02:57:55 +00001224 pParse->ckBase = regData;
drh35573352008-01-08 23:54:25 +00001225 sqlite3ExprIfTrue(pParse, pTab->pCheck, allOk, SQLITE_JUMPIFNULL);
drhaa01c7e2006-03-15 16:26:10 +00001226 onError = overrideError!=OE_Default ? overrideError : OE_Abort;
drh2e06c672007-07-23 19:39:46 +00001227 if( onError==OE_Ignore ){
drh66a51672008-01-03 00:01:23 +00001228 sqlite3VdbeAddOp2(v, OP_Goto, 0, ignoreDest);
drhaa01c7e2006-03-15 16:26:10 +00001229 }else{
drh66a51672008-01-03 00:01:23 +00001230 sqlite3VdbeAddOp2(v, OP_Halt, SQLITE_CONSTRAINT, onError);
drhaa01c7e2006-03-15 16:26:10 +00001231 }
drhffe07b22005-11-03 00:41:17 +00001232 sqlite3VdbeResolveLabel(v, allOk);
1233 }
1234#endif /* !defined(SQLITE_OMIT_CHECK) */
drh9cfcf5d2002-01-29 18:41:24 +00001235
drh0bd1f4e2002-06-06 18:54:39 +00001236 /* If we have an INTEGER PRIMARY KEY, make sure the primary key
1237 ** of the new record does not previously exist. Except, if this
1238 ** is an UPDATE and the primary key is not changing, that is OK.
drh9cfcf5d2002-01-29 18:41:24 +00001239 */
drhf0863fe2005-06-12 21:35:51 +00001240 if( rowidChng ){
drh0ca3e242002-01-29 23:07:02 +00001241 onError = pTab->keyConf;
1242 if( overrideError!=OE_Default ){
1243 onError = overrideError;
drha996e472003-05-16 02:30:27 +00001244 }else if( onError==OE_Default ){
1245 onError = OE_Abort;
drh0ca3e242002-01-29 23:07:02 +00001246 }
drha0217ba2003-06-01 01:10:33 +00001247
drh60a713c2008-01-21 16:22:45 +00001248 if( onError!=OE_Replace || pTab->pIndex ){
drha05a7222008-01-19 03:35:58 +00001249 if( isUpdate ){
1250 j2 = sqlite3VdbeAddOp3(v, OP_Eq, regRowid, 0, regRowid-1);
drh79b0c952002-05-21 12:56:43 +00001251 }
drha05a7222008-01-19 03:35:58 +00001252 j3 = sqlite3VdbeAddOp3(v, OP_NotExists, baseCur, 0, regRowid);
1253 switch( onError ){
1254 default: {
1255 onError = OE_Abort;
1256 /* Fall thru into the next case */
drh5383ae52003-06-04 12:23:30 +00001257 }
drha05a7222008-01-19 03:35:58 +00001258 case OE_Rollback:
1259 case OE_Abort:
1260 case OE_Fail: {
1261 sqlite3VdbeAddOp4(v, OP_Halt, SQLITE_CONSTRAINT, onError, 0,
1262 "PRIMARY KEY must be unique", P4_STATIC);
1263 break;
1264 }
1265 case OE_Replace: {
1266 sqlite3GenerateRowIndexDelete(pParse, pTab, baseCur, 0);
1267 seenReplace = 1;
1268 break;
1269 }
1270 case OE_Ignore: {
1271 assert( seenReplace==0 );
1272 sqlite3VdbeAddOp2(v, OP_Goto, 0, ignoreDest);
1273 break;
1274 }
drh0ca3e242002-01-29 23:07:02 +00001275 }
drha05a7222008-01-19 03:35:58 +00001276 sqlite3VdbeJumpHere(v, j3);
1277 if( isUpdate ){
1278 sqlite3VdbeJumpHere(v, j2);
drh5383ae52003-06-04 12:23:30 +00001279 }
1280 }
drh0ca3e242002-01-29 23:07:02 +00001281 }
drh0bd1f4e2002-06-06 18:54:39 +00001282
1283 /* Test all UNIQUE constraints by creating entries for each UNIQUE
1284 ** index and making sure that duplicate entries do not already exist.
1285 ** Add the new records to the indices as we go.
1286 */
drhb2fe7d82003-04-20 17:29:23 +00001287 for(iCur=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, iCur++){
drh2d401ab2008-01-10 23:50:11 +00001288 int regIdx;
1289 int regR;
1290
drhaa9b8962008-01-08 02:57:55 +00001291 if( aRegIdx[iCur]==0 ) continue; /* Skip unused indices */
drhb2fe7d82003-04-20 17:29:23 +00001292
1293 /* Create a key for accessing the index entry */
drh2d401ab2008-01-10 23:50:11 +00001294 regIdx = sqlite3GetTempRange(pParse, pIdx->nColumn+1);
drh9cfcf5d2002-01-29 18:41:24 +00001295 for(i=0; i<pIdx->nColumn; i++){
1296 int idx = pIdx->aiColumn[i];
1297 if( idx==pTab->iPKey ){
drh2d401ab2008-01-10 23:50:11 +00001298 sqlite3VdbeAddOp2(v, OP_SCopy, regRowid, regIdx+i);
drh9cfcf5d2002-01-29 18:41:24 +00001299 }else{
drh2d401ab2008-01-10 23:50:11 +00001300 sqlite3VdbeAddOp2(v, OP_SCopy, regData+idx, regIdx+i);
drh9cfcf5d2002-01-29 18:41:24 +00001301 }
1302 }
drh2d401ab2008-01-10 23:50:11 +00001303 sqlite3VdbeAddOp2(v, OP_SCopy, regRowid, regIdx+i);
drh1db639c2008-01-17 02:36:28 +00001304 sqlite3VdbeAddOp3(v, OP_MakeRecord, regIdx, pIdx->nColumn+1, aRegIdx[iCur]);
dan69f8bb92009-08-13 19:21:16 +00001305 sqlite3VdbeChangeP4(v, -1, sqlite3IndexAffinityStr(v, pIdx), 0);
drhda250ea2008-04-01 05:07:14 +00001306 sqlite3ExprCacheAffinityChange(pParse, regIdx, pIdx->nColumn+1);
drhb2fe7d82003-04-20 17:29:23 +00001307
1308 /* Find out what action to take in case there is an indexing conflict */
drh9cfcf5d2002-01-29 18:41:24 +00001309 onError = pIdx->onError;
danielk1977de630352009-05-04 11:42:29 +00001310 if( onError==OE_None ){
1311 sqlite3ReleaseTempRange(pParse, regIdx, pIdx->nColumn+1);
1312 continue; /* pIdx is not a UNIQUE index */
1313 }
drh9cfcf5d2002-01-29 18:41:24 +00001314 if( overrideError!=OE_Default ){
1315 onError = overrideError;
drha996e472003-05-16 02:30:27 +00001316 }else if( onError==OE_Default ){
1317 onError = OE_Abort;
drh9cfcf5d2002-01-29 18:41:24 +00001318 }
drh5383ae52003-06-04 12:23:30 +00001319 if( seenReplace ){
1320 if( onError==OE_Ignore ) onError = OE_Replace;
1321 else if( onError==OE_Fail ) onError = OE_Abort;
1322 }
1323
drhb2fe7d82003-04-20 17:29:23 +00001324
1325 /* Check to see if the new index entry will be unique */
drh2d401ab2008-01-10 23:50:11 +00001326 regR = sqlite3GetTempReg(pParse);
1327 sqlite3VdbeAddOp2(v, OP_SCopy, regRowid-hasTwoRowids, regR);
1328 j3 = sqlite3VdbeAddOp4(v, OP_IsUnique, baseCur+iCur+1, 0,
danielk1977de630352009-05-04 11:42:29 +00001329 regR, SQLITE_INT_TO_PTR(regIdx),
mlcreecha9e852b2008-03-06 09:58:50 +00001330 P4_INT32);
danielk1977de630352009-05-04 11:42:29 +00001331 sqlite3ReleaseTempRange(pParse, regIdx, pIdx->nColumn+1);
drhb2fe7d82003-04-20 17:29:23 +00001332
1333 /* Generate code that executes if the new index entry is not unique */
danielk1977b84f96f2005-01-20 11:32:23 +00001334 assert( onError==OE_Rollback || onError==OE_Abort || onError==OE_Fail
1335 || onError==OE_Ignore || onError==OE_Replace );
drh9cfcf5d2002-01-29 18:41:24 +00001336 switch( onError ){
drh1c928532002-01-31 15:54:21 +00001337 case OE_Rollback:
1338 case OE_Abort:
1339 case OE_Fail: {
drh098d1682009-05-02 15:46:46 +00001340 int j;
1341 StrAccum errMsg;
1342 const char *zSep;
1343 char *zErr;
1344
1345 sqlite3StrAccumInit(&errMsg, 0, 0, 200);
1346 errMsg.db = pParse->db;
1347 zSep = pIdx->nColumn>1 ? "columns " : "column ";
1348 for(j=0; j<pIdx->nColumn; j++){
drh37ed48e2003-08-05 13:13:38 +00001349 char *zCol = pTab->aCol[pIdx->aiColumn[j]].zName;
drh098d1682009-05-02 15:46:46 +00001350 sqlite3StrAccumAppend(&errMsg, zSep, -1);
1351 zSep = ", ";
1352 sqlite3StrAccumAppend(&errMsg, zCol, -1);
drh37ed48e2003-08-05 13:13:38 +00001353 }
drh098d1682009-05-02 15:46:46 +00001354 sqlite3StrAccumAppend(&errMsg,
1355 pIdx->nColumn>1 ? " are not unique" : " is not unique", -1);
1356 zErr = sqlite3StrAccumFinish(&errMsg);
1357 sqlite3VdbeAddOp4(v, OP_Halt, SQLITE_CONSTRAINT, onError, 0, zErr, 0);
1358 sqlite3DbFree(errMsg.db, zErr);
drh9cfcf5d2002-01-29 18:41:24 +00001359 break;
1360 }
1361 case OE_Ignore: {
drh0ca3e242002-01-29 23:07:02 +00001362 assert( seenReplace==0 );
drh66a51672008-01-03 00:01:23 +00001363 sqlite3VdbeAddOp2(v, OP_Goto, 0, ignoreDest);
drh9cfcf5d2002-01-29 18:41:24 +00001364 break;
1365 }
drh098d1682009-05-02 15:46:46 +00001366 default: {
1367 assert( onError==OE_Replace );
drh2d401ab2008-01-10 23:50:11 +00001368 sqlite3GenerateRowDelete(pParse, pTab, baseCur, regR, 0);
drh0ca3e242002-01-29 23:07:02 +00001369 seenReplace = 1;
drh9cfcf5d2002-01-29 18:41:24 +00001370 break;
1371 }
drh9cfcf5d2002-01-29 18:41:24 +00001372 }
drh2d401ab2008-01-10 23:50:11 +00001373 sqlite3VdbeJumpHere(v, j3);
1374 sqlite3ReleaseTempReg(pParse, regR);
drh9cfcf5d2002-01-29 18:41:24 +00001375 }
danielk1977de630352009-05-04 11:42:29 +00001376
1377 if( pbMayReplace ){
1378 *pbMayReplace = seenReplace;
1379 }
drh9cfcf5d2002-01-29 18:41:24 +00001380}
drh0ca3e242002-01-29 23:07:02 +00001381
1382/*
1383** This routine generates code to finish the INSERT or UPDATE operation
danielk19774adee202004-05-08 08:23:19 +00001384** that was started by a prior call to sqlite3GenerateConstraintChecks.
drh04adf412008-01-08 18:57:50 +00001385** A consecutive range of registers starting at regRowid contains the
1386** rowid and the content to be inserted.
drh0ca3e242002-01-29 23:07:02 +00001387**
drhb419a922002-01-30 16:17:23 +00001388** The arguments to this routine should be the same as the first six
danielk19774adee202004-05-08 08:23:19 +00001389** arguments to sqlite3GenerateConstraintChecks.
drh0ca3e242002-01-29 23:07:02 +00001390*/
danielk19774adee202004-05-08 08:23:19 +00001391void sqlite3CompleteInsertion(
drh0ca3e242002-01-29 23:07:02 +00001392 Parse *pParse, /* The parser context */
1393 Table *pTab, /* the table into which we are inserting */
drh04adf412008-01-08 18:57:50 +00001394 int baseCur, /* Index of a read/write cursor pointing at pTab */
1395 int regRowid, /* Range of content */
drhaa9b8962008-01-08 02:57:55 +00001396 int *aRegIdx, /* Register used by each index. 0 for unused indices */
drh70ce3f02003-04-15 19:22:22 +00001397 int isUpdate, /* True for UPDATE, False for INSERT */
drhe4d90812007-03-29 05:51:49 +00001398 int newIdx, /* Index of NEW table for triggers. -1 if none */
danielk1977de630352009-05-04 11:42:29 +00001399 int appendBias, /* True if this is likely to be an append */
1400 int useSeekResult /* True to set the USESEEKRESULT flag on OP_[Idx]Insert */
drh0ca3e242002-01-29 23:07:02 +00001401){
1402 int i;
1403 Vdbe *v;
1404 int nIdx;
1405 Index *pIdx;
drh1bd10f82008-12-10 21:19:56 +00001406 u8 pik_flags;
drh04adf412008-01-08 18:57:50 +00001407 int regData;
drhb7654112008-01-12 12:48:07 +00001408 int regRec;
drh0ca3e242002-01-29 23:07:02 +00001409
danielk19774adee202004-05-08 08:23:19 +00001410 v = sqlite3GetVdbe(pParse);
drh0ca3e242002-01-29 23:07:02 +00001411 assert( v!=0 );
drh417be792002-03-03 18:59:40 +00001412 assert( pTab->pSelect==0 ); /* This table is not a VIEW */
drh0ca3e242002-01-29 23:07:02 +00001413 for(nIdx=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, nIdx++){}
1414 for(i=nIdx-1; i>=0; i--){
drhaa9b8962008-01-08 02:57:55 +00001415 if( aRegIdx[i]==0 ) continue;
drh04adf412008-01-08 18:57:50 +00001416 sqlite3VdbeAddOp2(v, OP_IdxInsert, baseCur+i+1, aRegIdx[i]);
danielk1977de630352009-05-04 11:42:29 +00001417 if( useSeekResult ){
1418 sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT);
1419 }
drh0ca3e242002-01-29 23:07:02 +00001420 }
drh04adf412008-01-08 18:57:50 +00001421 regData = regRowid + 1;
drhb7654112008-01-12 12:48:07 +00001422 regRec = sqlite3GetTempReg(pParse);
drh1db639c2008-01-17 02:36:28 +00001423 sqlite3VdbeAddOp3(v, OP_MakeRecord, regData, pTab->nCol, regRec);
danielk1977a37cdde2004-05-16 11:15:36 +00001424 sqlite3TableAffinityStr(v, pTab);
drhda250ea2008-04-01 05:07:14 +00001425 sqlite3ExprCacheAffinityChange(pParse, regData, pTab->nCol);
danielk1977b84f96f2005-01-20 11:32:23 +00001426#ifndef SQLITE_OMIT_TRIGGER
drh70ce3f02003-04-15 19:22:22 +00001427 if( newIdx>=0 ){
drhb7654112008-01-12 12:48:07 +00001428 sqlite3VdbeAddOp3(v, OP_Insert, newIdx, regRec, regRowid);
drh70ce3f02003-04-15 19:22:22 +00001429 }
danielk1977b84f96f2005-01-20 11:32:23 +00001430#endif
drh4794f732004-11-05 17:17:50 +00001431 if( pParse->nested ){
1432 pik_flags = 0;
1433 }else{
danielk197794eb6a12005-12-15 15:22:08 +00001434 pik_flags = OPFLAG_NCHANGE;
1435 pik_flags |= (isUpdate?OPFLAG_ISUPDATE:OPFLAG_LASTROWID);
drh4794f732004-11-05 17:17:50 +00001436 }
drhe4d90812007-03-29 05:51:49 +00001437 if( appendBias ){
1438 pik_flags |= OPFLAG_APPEND;
1439 }
danielk1977de630352009-05-04 11:42:29 +00001440 if( useSeekResult ){
1441 pik_flags |= OPFLAG_USESEEKRESULT;
1442 }
drhb7654112008-01-12 12:48:07 +00001443 sqlite3VdbeAddOp3(v, OP_Insert, baseCur, regRec, regRowid);
danielk197794eb6a12005-12-15 15:22:08 +00001444 if( !pParse->nested ){
drh66a51672008-01-03 00:01:23 +00001445 sqlite3VdbeChangeP4(v, -1, pTab->zName, P4_STATIC);
danielk197794eb6a12005-12-15 15:22:08 +00001446 }
drhb7654112008-01-12 12:48:07 +00001447 sqlite3VdbeChangeP5(v, pik_flags);
drh0ca3e242002-01-29 23:07:02 +00001448}
drhcd446902004-02-24 01:05:31 +00001449
1450/*
drh290c1942004-08-21 17:54:45 +00001451** Generate code that will open cursors for a table and for all
drh04adf412008-01-08 18:57:50 +00001452** indices of that table. The "baseCur" parameter is the cursor number used
drhcd446902004-02-24 01:05:31 +00001453** for the table. Indices are opened on subsequent cursors.
drhaa9b8962008-01-08 02:57:55 +00001454**
1455** Return the number of indices on the table.
drhcd446902004-02-24 01:05:31 +00001456*/
drhaa9b8962008-01-08 02:57:55 +00001457int sqlite3OpenTableAndIndices(
drh290c1942004-08-21 17:54:45 +00001458 Parse *pParse, /* Parsing context */
1459 Table *pTab, /* Table to be opened */
drhdfe88ec2008-11-03 20:55:06 +00001460 int baseCur, /* Cursor number assigned to the table */
drh290c1942004-08-21 17:54:45 +00001461 int op /* OP_OpenRead or OP_OpenWrite */
1462){
drhcd446902004-02-24 01:05:31 +00001463 int i;
drh4cbdda92006-06-14 19:00:20 +00001464 int iDb;
drhcd446902004-02-24 01:05:31 +00001465 Index *pIdx;
drh4cbdda92006-06-14 19:00:20 +00001466 Vdbe *v;
1467
drhaa9b8962008-01-08 02:57:55 +00001468 if( IsVirtual(pTab) ) return 0;
drh4cbdda92006-06-14 19:00:20 +00001469 iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
1470 v = sqlite3GetVdbe(pParse);
drhcd446902004-02-24 01:05:31 +00001471 assert( v!=0 );
drh04adf412008-01-08 18:57:50 +00001472 sqlite3OpenTable(pParse, baseCur, iDb, pTab, op);
drhcd446902004-02-24 01:05:31 +00001473 for(i=1, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){
danielk1977b3bf5562006-01-10 17:58:23 +00001474 KeyInfo *pKey = sqlite3IndexKeyinfo(pParse, pIdx);
danielk1977da184232006-01-05 11:34:32 +00001475 assert( pIdx->pSchema==pTab->pSchema );
drh04adf412008-01-08 18:57:50 +00001476 sqlite3VdbeAddOp4(v, op, i+baseCur, pIdx->tnum, iDb,
drh66a51672008-01-03 00:01:23 +00001477 (char*)pKey, P4_KEYINFO_HANDOFF);
danielk1977207872a2008-01-03 07:54:23 +00001478 VdbeComment((v, "%s", pIdx->zName));
drhcd446902004-02-24 01:05:31 +00001479 }
drh1b7ecbb2009-05-03 01:00:59 +00001480 if( pParse->nTab<baseCur+i ){
drh04adf412008-01-08 18:57:50 +00001481 pParse->nTab = baseCur+i;
drh290c1942004-08-21 17:54:45 +00001482 }
drhaa9b8962008-01-08 02:57:55 +00001483 return i-1;
drhcd446902004-02-24 01:05:31 +00001484}
drh9d9cf222007-02-13 15:01:11 +00001485
drh91c58e22007-03-27 12:04:04 +00001486
1487#ifdef SQLITE_TEST
1488/*
1489** The following global variable is incremented whenever the
1490** transfer optimization is used. This is used for testing
1491** purposes only - to make sure the transfer optimization really
1492** is happening when it is suppose to.
1493*/
1494int sqlite3_xferopt_count;
1495#endif /* SQLITE_TEST */
1496
1497
drh9d9cf222007-02-13 15:01:11 +00001498#ifndef SQLITE_OMIT_XFER_OPT
1499/*
1500** Check to collation names to see if they are compatible.
1501*/
1502static int xferCompatibleCollation(const char *z1, const char *z2){
1503 if( z1==0 ){
1504 return z2==0;
1505 }
1506 if( z2==0 ){
1507 return 0;
1508 }
1509 return sqlite3StrICmp(z1, z2)==0;
1510}
1511
1512
1513/*
1514** Check to see if index pSrc is compatible as a source of data
1515** for index pDest in an insert transfer optimization. The rules
1516** for a compatible index:
1517**
1518** * The index is over the same set of columns
1519** * The same DESC and ASC markings occurs on all columns
1520** * The same onError processing (OE_Abort, OE_Ignore, etc)
1521** * The same collating sequence on each column
1522*/
1523static int xferCompatibleIndex(Index *pDest, Index *pSrc){
1524 int i;
1525 assert( pDest && pSrc );
1526 assert( pDest->pTable!=pSrc->pTable );
1527 if( pDest->nColumn!=pSrc->nColumn ){
1528 return 0; /* Different number of columns */
1529 }
1530 if( pDest->onError!=pSrc->onError ){
1531 return 0; /* Different conflict resolution strategies */
1532 }
1533 for(i=0; i<pSrc->nColumn; i++){
1534 if( pSrc->aiColumn[i]!=pDest->aiColumn[i] ){
1535 return 0; /* Different columns indexed */
1536 }
1537 if( pSrc->aSortOrder[i]!=pDest->aSortOrder[i] ){
1538 return 0; /* Different sort orders */
1539 }
drh3f6e7812009-05-02 00:28:19 +00001540 if( !xferCompatibleCollation(pSrc->azColl[i],pDest->azColl[i]) ){
drh60a713c2008-01-21 16:22:45 +00001541 return 0; /* Different collating sequences */
drh9d9cf222007-02-13 15:01:11 +00001542 }
1543 }
1544
1545 /* If no test above fails then the indices must be compatible */
1546 return 1;
1547}
1548
1549/*
1550** Attempt the transfer optimization on INSERTs of the form
1551**
1552** INSERT INTO tab1 SELECT * FROM tab2;
1553**
1554** This optimization is only attempted if
1555**
1556** (1) tab1 and tab2 have identical schemas including all the
drh8103b7d2007-02-24 13:23:51 +00001557** same indices and constraints
drh9d9cf222007-02-13 15:01:11 +00001558**
1559** (2) tab1 and tab2 are different tables
1560**
1561** (3) There must be no triggers on tab1
1562**
1563** (4) The result set of the SELECT statement is "*"
1564**
1565** (5) The SELECT statement has no WHERE, HAVING, ORDER BY, GROUP BY,
1566** or LIMIT clause.
1567**
1568** (6) The SELECT statement is a simple (not a compound) select that
1569** contains only tab2 in its FROM clause
1570**
1571** This method for implementing the INSERT transfers raw records from
1572** tab2 over to tab1. The columns are not decoded. Raw records from
1573** the indices of tab2 are transfered to tab1 as well. In so doing,
1574** the resulting tab1 has much less fragmentation.
1575**
1576** This routine returns TRUE if the optimization is attempted. If any
1577** of the conditions above fail so that the optimization should not
1578** be attempted, then this routine returns FALSE.
1579*/
1580static int xferOptimization(
1581 Parse *pParse, /* Parser context */
1582 Table *pDest, /* The table we are inserting into */
1583 Select *pSelect, /* A SELECT statement to use as the data source */
1584 int onError, /* How to handle constraint errors */
1585 int iDbDest /* The database of pDest */
1586){
1587 ExprList *pEList; /* The result set of the SELECT */
1588 Table *pSrc; /* The table in the FROM clause of SELECT */
1589 Index *pSrcIdx, *pDestIdx; /* Source and destination indices */
1590 struct SrcList_item *pItem; /* An element of pSelect->pSrc */
1591 int i; /* Loop counter */
1592 int iDbSrc; /* The database of pSrc */
1593 int iSrc, iDest; /* Cursors from source and destination */
1594 int addr1, addr2; /* Loop addresses */
1595 int emptyDestTest; /* Address of test for empty pDest */
1596 int emptySrcTest; /* Address of test for empty pSrc */
drh9d9cf222007-02-13 15:01:11 +00001597 Vdbe *v; /* The VDBE we are building */
1598 KeyInfo *pKey; /* Key information for an index */
drh6a288a32008-01-07 19:20:24 +00001599 int regAutoinc; /* Memory register used by AUTOINC */
drhf33c9fa2007-04-10 18:17:55 +00001600 int destHasUniqueIdx = 0; /* True if pDest has a UNIQUE index */
drhb7654112008-01-12 12:48:07 +00001601 int regData, regRowid; /* Registers holding data and rowid */
drh9d9cf222007-02-13 15:01:11 +00001602
1603 if( pSelect==0 ){
1604 return 0; /* Must be of the form INSERT INTO ... SELECT ... */
1605 }
danielk19772f886d12009-02-28 10:47:41 +00001606 if( sqlite3TriggerList(pParse, pDest) ){
drh9d9cf222007-02-13 15:01:11 +00001607 return 0; /* tab1 must not have triggers */
1608 }
1609#ifndef SQLITE_OMIT_VIRTUALTABLE
drh7d10d5a2008-08-20 16:35:10 +00001610 if( pDest->tabFlags & TF_Virtual ){
drh9d9cf222007-02-13 15:01:11 +00001611 return 0; /* tab1 must not be a virtual table */
1612 }
1613#endif
1614 if( onError==OE_Default ){
1615 onError = OE_Abort;
1616 }
1617 if( onError!=OE_Abort && onError!=OE_Rollback ){
1618 return 0; /* Cannot do OR REPLACE or OR IGNORE or OR FAIL */
1619 }
danielk19775ce240a2007-09-03 17:30:06 +00001620 assert(pSelect->pSrc); /* allocated even if there is no FROM clause */
drh9d9cf222007-02-13 15:01:11 +00001621 if( pSelect->pSrc->nSrc!=1 ){
1622 return 0; /* FROM clause must have exactly one term */
1623 }
1624 if( pSelect->pSrc->a[0].pSelect ){
1625 return 0; /* FROM clause cannot contain a subquery */
1626 }
1627 if( pSelect->pWhere ){
1628 return 0; /* SELECT may not have a WHERE clause */
1629 }
1630 if( pSelect->pOrderBy ){
1631 return 0; /* SELECT may not have an ORDER BY clause */
1632 }
drh8103b7d2007-02-24 13:23:51 +00001633 /* Do not need to test for a HAVING clause. If HAVING is present but
1634 ** there is no ORDER BY, we will get an error. */
drh9d9cf222007-02-13 15:01:11 +00001635 if( pSelect->pGroupBy ){
1636 return 0; /* SELECT may not have a GROUP BY clause */
1637 }
1638 if( pSelect->pLimit ){
1639 return 0; /* SELECT may not have a LIMIT clause */
1640 }
drh8103b7d2007-02-24 13:23:51 +00001641 assert( pSelect->pOffset==0 ); /* Must be so if pLimit==0 */
drh9d9cf222007-02-13 15:01:11 +00001642 if( pSelect->pPrior ){
1643 return 0; /* SELECT may not be a compound query */
1644 }
drh7d10d5a2008-08-20 16:35:10 +00001645 if( pSelect->selFlags & SF_Distinct ){
drh9d9cf222007-02-13 15:01:11 +00001646 return 0; /* SELECT may not be DISTINCT */
1647 }
1648 pEList = pSelect->pEList;
1649 assert( pEList!=0 );
1650 if( pEList->nExpr!=1 ){
1651 return 0; /* The result set must have exactly one column */
1652 }
1653 assert( pEList->a[0].pExpr );
1654 if( pEList->a[0].pExpr->op!=TK_ALL ){
1655 return 0; /* The result set must be the special operator "*" */
1656 }
1657
1658 /* At this point we have established that the statement is of the
1659 ** correct syntactic form to participate in this optimization. Now
1660 ** we have to check the semantics.
1661 */
1662 pItem = pSelect->pSrc->a;
drhca424112008-01-25 15:04:48 +00001663 pSrc = sqlite3LocateTable(pParse, 0, pItem->zName, pItem->zDatabase);
drh9d9cf222007-02-13 15:01:11 +00001664 if( pSrc==0 ){
1665 return 0; /* FROM clause does not contain a real table */
1666 }
1667 if( pSrc==pDest ){
1668 return 0; /* tab1 and tab2 may not be the same table */
1669 }
1670#ifndef SQLITE_OMIT_VIRTUALTABLE
drh7d10d5a2008-08-20 16:35:10 +00001671 if( pSrc->tabFlags & TF_Virtual ){
drh9d9cf222007-02-13 15:01:11 +00001672 return 0; /* tab2 must not be a virtual table */
1673 }
1674#endif
1675 if( pSrc->pSelect ){
1676 return 0; /* tab2 may not be a view */
1677 }
1678 if( pDest->nCol!=pSrc->nCol ){
1679 return 0; /* Number of columns must be the same in tab1 and tab2 */
1680 }
1681 if( pDest->iPKey!=pSrc->iPKey ){
1682 return 0; /* Both tables must have the same INTEGER PRIMARY KEY */
1683 }
1684 for(i=0; i<pDest->nCol; i++){
1685 if( pDest->aCol[i].affinity!=pSrc->aCol[i].affinity ){
1686 return 0; /* Affinity must be the same on all columns */
1687 }
1688 if( !xferCompatibleCollation(pDest->aCol[i].zColl, pSrc->aCol[i].zColl) ){
1689 return 0; /* Collating sequence must be the same on all columns */
1690 }
1691 if( pDest->aCol[i].notNull && !pSrc->aCol[i].notNull ){
1692 return 0; /* tab2 must be NOT NULL if tab1 is */
1693 }
1694 }
1695 for(pDestIdx=pDest->pIndex; pDestIdx; pDestIdx=pDestIdx->pNext){
drhf33c9fa2007-04-10 18:17:55 +00001696 if( pDestIdx->onError!=OE_None ){
1697 destHasUniqueIdx = 1;
1698 }
drh9d9cf222007-02-13 15:01:11 +00001699 for(pSrcIdx=pSrc->pIndex; pSrcIdx; pSrcIdx=pSrcIdx->pNext){
1700 if( xferCompatibleIndex(pDestIdx, pSrcIdx) ) break;
1701 }
1702 if( pSrcIdx==0 ){
1703 return 0; /* pDestIdx has no corresponding index in pSrc */
1704 }
1705 }
drh7fc2f412007-03-29 00:08:24 +00001706#ifndef SQLITE_OMIT_CHECK
drhfb658de2007-02-24 15:18:49 +00001707 if( pDest->pCheck && !sqlite3ExprCompare(pSrc->pCheck, pDest->pCheck) ){
drh8103b7d2007-02-24 13:23:51 +00001708 return 0; /* Tables have different CHECK constraints. Ticket #2252 */
1709 }
drh7fc2f412007-03-29 00:08:24 +00001710#endif
drh9d9cf222007-02-13 15:01:11 +00001711
1712 /* If we get this far, it means either:
1713 **
1714 ** * We can always do the transfer if the table contains an
1715 ** an integer primary key
1716 **
1717 ** * We can conditionally do the transfer if the destination
1718 ** table is empty.
1719 */
drhdd735212007-02-24 13:53:05 +00001720#ifdef SQLITE_TEST
1721 sqlite3_xferopt_count++;
1722#endif
drh9d9cf222007-02-13 15:01:11 +00001723 iDbSrc = sqlite3SchemaToIndex(pParse->db, pSrc->pSchema);
1724 v = sqlite3GetVdbe(pParse);
drhf53e9b52007-08-29 13:45:58 +00001725 sqlite3CodeVerifySchema(pParse, iDbSrc);
drh9d9cf222007-02-13 15:01:11 +00001726 iSrc = pParse->nTab++;
1727 iDest = pParse->nTab++;
drh6a288a32008-01-07 19:20:24 +00001728 regAutoinc = autoIncBegin(pParse, iDbDest, pDest);
drh9d9cf222007-02-13 15:01:11 +00001729 sqlite3OpenTable(pParse, iDest, iDbDest, pDest, OP_OpenWrite);
drhf33c9fa2007-04-10 18:17:55 +00001730 if( (pDest->iPKey<0 && pDest->pIndex!=0) || destHasUniqueIdx ){
drhbd36ba62007-03-31 13:00:26 +00001731 /* If tables do not have an INTEGER PRIMARY KEY and there
1732 ** are indices to be copied and the destination is not empty,
1733 ** we have to disallow the transfer optimization because the
1734 ** the rowids might change which will mess up indexing.
drhf33c9fa2007-04-10 18:17:55 +00001735 **
1736 ** Or if the destination has a UNIQUE index and is not empty,
1737 ** we also disallow the transfer optimization because we cannot
1738 ** insure that all entries in the union of DEST and SRC will be
1739 ** unique.
drh9d9cf222007-02-13 15:01:11 +00001740 */
drh66a51672008-01-03 00:01:23 +00001741 addr1 = sqlite3VdbeAddOp2(v, OP_Rewind, iDest, 0);
1742 emptyDestTest = sqlite3VdbeAddOp2(v, OP_Goto, 0, 0);
drh9d9cf222007-02-13 15:01:11 +00001743 sqlite3VdbeJumpHere(v, addr1);
1744 }else{
1745 emptyDestTest = 0;
1746 }
1747 sqlite3OpenTable(pParse, iSrc, iDbSrc, pSrc, OP_OpenRead);
drh66a51672008-01-03 00:01:23 +00001748 emptySrcTest = sqlite3VdbeAddOp2(v, OP_Rewind, iSrc, 0);
drhb7654112008-01-12 12:48:07 +00001749 regData = sqlite3GetTempReg(pParse);
1750 regRowid = sqlite3GetTempReg(pParse);
drh42242de2007-03-29 13:35:35 +00001751 if( pDest->iPKey>=0 ){
drhb7654112008-01-12 12:48:07 +00001752 addr1 = sqlite3VdbeAddOp2(v, OP_Rowid, iSrc, regRowid);
1753 addr2 = sqlite3VdbeAddOp3(v, OP_NotExists, iDest, 0, regRowid);
drh66a51672008-01-03 00:01:23 +00001754 sqlite3VdbeAddOp4(v, OP_Halt, SQLITE_CONSTRAINT, onError, 0,
1755 "PRIMARY KEY must be unique", P4_STATIC);
drh95bad4c2007-03-28 18:04:10 +00001756 sqlite3VdbeJumpHere(v, addr2);
drhb7654112008-01-12 12:48:07 +00001757 autoIncStep(pParse, regAutoinc, regRowid);
drhbd36ba62007-03-31 13:00:26 +00001758 }else if( pDest->pIndex==0 ){
drhb7654112008-01-12 12:48:07 +00001759 addr1 = sqlite3VdbeAddOp2(v, OP_NewRowid, iDest, regRowid);
drh95bad4c2007-03-28 18:04:10 +00001760 }else{
drhb7654112008-01-12 12:48:07 +00001761 addr1 = sqlite3VdbeAddOp2(v, OP_Rowid, iSrc, regRowid);
drh7d10d5a2008-08-20 16:35:10 +00001762 assert( (pDest->tabFlags & TF_Autoincrement)==0 );
drh95bad4c2007-03-28 18:04:10 +00001763 }
drhb7654112008-01-12 12:48:07 +00001764 sqlite3VdbeAddOp2(v, OP_RowData, iSrc, regData);
1765 sqlite3VdbeAddOp3(v, OP_Insert, iDest, regData, regRowid);
1766 sqlite3VdbeChangeP5(v, OPFLAG_NCHANGE|OPFLAG_LASTROWID|OPFLAG_APPEND);
danielk19771f4aa332008-01-03 09:51:55 +00001767 sqlite3VdbeChangeP4(v, -1, pDest->zName, 0);
drh66a51672008-01-03 00:01:23 +00001768 sqlite3VdbeAddOp2(v, OP_Next, iSrc, addr1);
drh9d9cf222007-02-13 15:01:11 +00001769 for(pDestIdx=pDest->pIndex; pDestIdx; pDestIdx=pDestIdx->pNext){
drh1b7ecbb2009-05-03 01:00:59 +00001770 for(pSrcIdx=pSrc->pIndex; ALWAYS(pSrcIdx); pSrcIdx=pSrcIdx->pNext){
drh9d9cf222007-02-13 15:01:11 +00001771 if( xferCompatibleIndex(pDestIdx, pSrcIdx) ) break;
1772 }
1773 assert( pSrcIdx );
drh66a51672008-01-03 00:01:23 +00001774 sqlite3VdbeAddOp2(v, OP_Close, iSrc, 0);
1775 sqlite3VdbeAddOp2(v, OP_Close, iDest, 0);
drh9d9cf222007-02-13 15:01:11 +00001776 pKey = sqlite3IndexKeyinfo(pParse, pSrcIdx);
danielk1977207872a2008-01-03 07:54:23 +00001777 sqlite3VdbeAddOp4(v, OP_OpenRead, iSrc, pSrcIdx->tnum, iDbSrc,
1778 (char*)pKey, P4_KEYINFO_HANDOFF);
drhd4e70eb2008-01-02 00:34:36 +00001779 VdbeComment((v, "%s", pSrcIdx->zName));
drh9d9cf222007-02-13 15:01:11 +00001780 pKey = sqlite3IndexKeyinfo(pParse, pDestIdx);
danielk1977207872a2008-01-03 07:54:23 +00001781 sqlite3VdbeAddOp4(v, OP_OpenWrite, iDest, pDestIdx->tnum, iDbDest,
drh66a51672008-01-03 00:01:23 +00001782 (char*)pKey, P4_KEYINFO_HANDOFF);
danielk1977207872a2008-01-03 07:54:23 +00001783 VdbeComment((v, "%s", pDestIdx->zName));
drh66a51672008-01-03 00:01:23 +00001784 addr1 = sqlite3VdbeAddOp2(v, OP_Rewind, iSrc, 0);
drhb7654112008-01-12 12:48:07 +00001785 sqlite3VdbeAddOp2(v, OP_RowKey, iSrc, regData);
1786 sqlite3VdbeAddOp3(v, OP_IdxInsert, iDest, regData, 1);
drh66a51672008-01-03 00:01:23 +00001787 sqlite3VdbeAddOp2(v, OP_Next, iSrc, addr1+1);
drh9d9cf222007-02-13 15:01:11 +00001788 sqlite3VdbeJumpHere(v, addr1);
1789 }
1790 sqlite3VdbeJumpHere(v, emptySrcTest);
drhb7654112008-01-12 12:48:07 +00001791 sqlite3ReleaseTempReg(pParse, regRowid);
1792 sqlite3ReleaseTempReg(pParse, regData);
drh66a51672008-01-03 00:01:23 +00001793 sqlite3VdbeAddOp2(v, OP_Close, iSrc, 0);
1794 sqlite3VdbeAddOp2(v, OP_Close, iDest, 0);
drh9d9cf222007-02-13 15:01:11 +00001795 if( emptyDestTest ){
drh66a51672008-01-03 00:01:23 +00001796 sqlite3VdbeAddOp2(v, OP_Halt, SQLITE_OK, 0);
drh9d9cf222007-02-13 15:01:11 +00001797 sqlite3VdbeJumpHere(v, emptyDestTest);
drh66a51672008-01-03 00:01:23 +00001798 sqlite3VdbeAddOp2(v, OP_Close, iDest, 0);
drh9d9cf222007-02-13 15:01:11 +00001799 return 0;
1800 }else{
1801 return 1;
1802 }
1803}
1804#endif /* SQLITE_OMIT_XFER_OPT */
drhf39d9582008-03-19 20:42:13 +00001805
1806/* Make sure "isView" gets undefined in case this file becomes part of
1807** the amalgamation - so that subsequent files do not see isView as a
1808** macro. */
1809#undef isView