blob: 1c2ce5c091618cccc84ba78e527d30283f01f710 [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 ){
dan65a7cd12009-09-01 12:16:01 +0000200 Parse *pToplevel = sqlite3ParseToplevel(pParse);
drh0b9f50d2009-06-23 20:28:53 +0000201 AutoincInfo *pInfo;
202
dan65a7cd12009-09-01 12:16:01 +0000203 pInfo = pToplevel->pAinc;
drh0b9f50d2009-06-23 20:28:53 +0000204 while( pInfo && pInfo->pTab!=pTab ){ pInfo = pInfo->pNext; }
205 if( pInfo==0 ){
206 pInfo = sqlite3DbMallocRaw(pParse->db, sizeof(*pInfo));
207 if( pInfo==0 ) return 0;
dan65a7cd12009-09-01 12:16:01 +0000208 pInfo->pNext = pToplevel->pAinc;
209 pToplevel->pAinc = pInfo;
drh0b9f50d2009-06-23 20:28:53 +0000210 pInfo->pTab = pTab;
211 pInfo->iDb = iDb;
dan65a7cd12009-09-01 12:16:01 +0000212 pToplevel->nMem++; /* Register to hold name of table */
213 pInfo->regCtr = ++pToplevel->nMem; /* Max rowid register */
214 pToplevel->nMem++; /* Rowid in sqlite_sequence */
drh0b9f50d2009-06-23 20:28:53 +0000215 }
216 memId = pInfo->regCtr;
drh9d9cf222007-02-13 15:01:11 +0000217 }
218 return memId;
219}
220
221/*
drh0b9f50d2009-06-23 20:28:53 +0000222** This routine generates code that will initialize all of the
223** register used by the autoincrement tracker.
224*/
225void sqlite3AutoincrementBegin(Parse *pParse){
226 AutoincInfo *p; /* Information about an AUTOINCREMENT */
227 sqlite3 *db = pParse->db; /* The database connection */
228 Db *pDb; /* Database only autoinc table */
229 int memId; /* Register holding max rowid */
230 int addr; /* A VDBE address */
231 Vdbe *v = pParse->pVdbe; /* VDBE under construction */
232
drh345ba7d2009-09-08 13:40:16 +0000233 /* This routine is never called during trigger-generation. It is
234 ** only called from the top-level */
235 assert( pParse->pTriggerTab==0 );
236 assert( pParse==sqlite3ParseToplevel(pParse) );
dan76d462e2009-08-30 11:42:51 +0000237
drh0b9f50d2009-06-23 20:28:53 +0000238 assert( v ); /* We failed long ago if this is not so */
239 for(p = pParse->pAinc; p; p = p->pNext){
240 pDb = &db->aDb[p->iDb];
241 memId = p->regCtr;
242 sqlite3OpenTable(pParse, 0, p->iDb, pDb->pSchema->pSeqTab, OP_OpenRead);
243 addr = sqlite3VdbeCurrentAddr(v);
244 sqlite3VdbeAddOp4(v, OP_String8, 0, memId-1, 0, p->pTab->zName, 0);
245 sqlite3VdbeAddOp2(v, OP_Rewind, 0, addr+9);
246 sqlite3VdbeAddOp3(v, OP_Column, 0, 0, memId);
247 sqlite3VdbeAddOp3(v, OP_Ne, memId-1, addr+7, memId);
248 sqlite3VdbeChangeP5(v, SQLITE_JUMPIFNULL);
249 sqlite3VdbeAddOp2(v, OP_Rowid, 0, memId+1);
250 sqlite3VdbeAddOp3(v, OP_Column, 0, 1, memId);
251 sqlite3VdbeAddOp2(v, OP_Goto, 0, addr+9);
252 sqlite3VdbeAddOp2(v, OP_Next, 0, addr+2);
253 sqlite3VdbeAddOp2(v, OP_Integer, 0, memId);
254 sqlite3VdbeAddOp0(v, OP_Close);
255 }
256}
257
258/*
drh9d9cf222007-02-13 15:01:11 +0000259** Update the maximum rowid for an autoincrement calculation.
260**
261** This routine should be called when the top of the stack holds a
262** new rowid that is about to be inserted. If that new rowid is
263** larger than the maximum rowid in the memId memory cell, then the
264** memory cell is updated. The stack is unchanged.
265*/
drh6a288a32008-01-07 19:20:24 +0000266static void autoIncStep(Parse *pParse, int memId, int regRowid){
drh9d9cf222007-02-13 15:01:11 +0000267 if( memId>0 ){
drh6a288a32008-01-07 19:20:24 +0000268 sqlite3VdbeAddOp2(pParse->pVdbe, OP_MemMax, memId, regRowid);
drh9d9cf222007-02-13 15:01:11 +0000269 }
270}
271
272/*
drh0b9f50d2009-06-23 20:28:53 +0000273** This routine generates the code needed to write autoincrement
274** maximum rowid values back into the sqlite_sequence register.
275** Every statement that might do an INSERT into an autoincrement
276** table (either directly or through triggers) needs to call this
277** routine just before the "exit" code.
drh9d9cf222007-02-13 15:01:11 +0000278*/
drh0b9f50d2009-06-23 20:28:53 +0000279void sqlite3AutoincrementEnd(Parse *pParse){
280 AutoincInfo *p;
281 Vdbe *v = pParse->pVdbe;
282 sqlite3 *db = pParse->db;
drh6a288a32008-01-07 19:20:24 +0000283
drh0b9f50d2009-06-23 20:28:53 +0000284 assert( v );
285 for(p = pParse->pAinc; p; p = p->pNext){
286 Db *pDb = &db->aDb[p->iDb];
287 int j1, j2, j3, j4, j5;
288 int iRec;
289 int memId = p->regCtr;
290
291 iRec = sqlite3GetTempReg(pParse);
292 sqlite3OpenTable(pParse, 0, p->iDb, pDb->pSchema->pSeqTab, OP_OpenWrite);
drh6a288a32008-01-07 19:20:24 +0000293 j1 = sqlite3VdbeAddOp1(v, OP_NotNull, memId+1);
drh0b9f50d2009-06-23 20:28:53 +0000294 j2 = sqlite3VdbeAddOp0(v, OP_Rewind);
295 j3 = sqlite3VdbeAddOp3(v, OP_Column, 0, 0, iRec);
296 j4 = sqlite3VdbeAddOp3(v, OP_Eq, memId-1, 0, iRec);
297 sqlite3VdbeAddOp2(v, OP_Next, 0, j3);
298 sqlite3VdbeJumpHere(v, j2);
299 sqlite3VdbeAddOp2(v, OP_NewRowid, 0, memId+1);
300 j5 = sqlite3VdbeAddOp0(v, OP_Goto);
301 sqlite3VdbeJumpHere(v, j4);
302 sqlite3VdbeAddOp2(v, OP_Rowid, 0, memId+1);
drh6a288a32008-01-07 19:20:24 +0000303 sqlite3VdbeJumpHere(v, j1);
drh0b9f50d2009-06-23 20:28:53 +0000304 sqlite3VdbeJumpHere(v, j5);
danielk1977a7a8e142008-02-13 18:25:27 +0000305 sqlite3VdbeAddOp3(v, OP_MakeRecord, memId-1, 2, iRec);
drh0b9f50d2009-06-23 20:28:53 +0000306 sqlite3VdbeAddOp3(v, OP_Insert, 0, iRec, memId+1);
drh35573352008-01-08 23:54:25 +0000307 sqlite3VdbeChangeP5(v, OPFLAG_APPEND);
drh0b9f50d2009-06-23 20:28:53 +0000308 sqlite3VdbeAddOp0(v, OP_Close);
309 sqlite3ReleaseTempReg(pParse, iRec);
drh9d9cf222007-02-13 15:01:11 +0000310 }
311}
312#else
313/*
314** If SQLITE_OMIT_AUTOINCREMENT is defined, then the three routines
315** above are all no-ops
316*/
317# define autoIncBegin(A,B,C) (0)
danielk1977287fb612008-01-04 19:10:28 +0000318# define autoIncStep(A,B,C)
drh9d9cf222007-02-13 15:01:11 +0000319#endif /* SQLITE_OMIT_AUTOINCREMENT */
320
321
322/* Forward declaration */
323static int xferOptimization(
324 Parse *pParse, /* Parser context */
325 Table *pDest, /* The table we are inserting into */
326 Select *pSelect, /* A SELECT statement to use as the data source */
327 int onError, /* How to handle constraint errors */
328 int iDbDest /* The database of pDest */
329);
330
danielk19773d1bfea2004-05-14 11:00:53 +0000331/*
drh1ccde152000-06-17 13:12:39 +0000332** This routine is call to handle SQL of the following forms:
drhcce7d172000-05-31 15:34:51 +0000333**
334** insert into TABLE (IDLIST) values(EXPRLIST)
drh1ccde152000-06-17 13:12:39 +0000335** insert into TABLE (IDLIST) select
drhcce7d172000-05-31 15:34:51 +0000336**
drh1ccde152000-06-17 13:12:39 +0000337** The IDLIST following the table name is always optional. If omitted,
338** then a list of all columns for the table is substituted. The IDLIST
drh967e8b72000-06-21 13:59:10 +0000339** appears in the pColumn parameter. pColumn is NULL if IDLIST is omitted.
drh1ccde152000-06-17 13:12:39 +0000340**
341** The pList parameter holds EXPRLIST in the first form of the INSERT
342** statement above, and pSelect is NULL. For the second form, pList is
343** NULL and pSelect is a pointer to the select statement used to generate
344** data for the insert.
drh142e30d2002-08-28 03:00:58 +0000345**
drh9d9cf222007-02-13 15:01:11 +0000346** The code generated follows one of four templates. For a simple
drh142e30d2002-08-28 03:00:58 +0000347** select with data coming from a VALUES clause, the code executes
drhe00ee6e2008-06-20 15:24:01 +0000348** once straight down through. Pseudo-code follows (we call this
349** the "1st template"):
drh142e30d2002-08-28 03:00:58 +0000350**
351** open write cursor to <table> and its indices
352** puts VALUES clause expressions onto the stack
353** write the resulting record into <table>
354** cleanup
355**
drh9d9cf222007-02-13 15:01:11 +0000356** The three remaining templates assume the statement is of the form
drh142e30d2002-08-28 03:00:58 +0000357**
358** INSERT INTO <table> SELECT ...
359**
drh9d9cf222007-02-13 15:01:11 +0000360** If the SELECT clause is of the restricted form "SELECT * FROM <table2>" -
361** in other words if the SELECT pulls all columns from a single table
362** and there is no WHERE or LIMIT or GROUP BY or ORDER BY clauses, and
363** if <table2> and <table1> are distinct tables but have identical
364** schemas, including all the same indices, then a special optimization
365** is invoked that copies raw records from <table2> over to <table1>.
366** See the xferOptimization() function for the implementation of this
drhe00ee6e2008-06-20 15:24:01 +0000367** template. This is the 2nd template.
drh9d9cf222007-02-13 15:01:11 +0000368**
369** open a write cursor to <table>
370** open read cursor on <table2>
371** transfer all records in <table2> over to <table>
372** close cursors
373** foreach index on <table>
374** open a write cursor on the <table> index
375** open a read cursor on the corresponding <table2> index
376** transfer all records from the read to the write cursors
377** close cursors
378** end foreach
379**
drhe00ee6e2008-06-20 15:24:01 +0000380** The 3rd template is for when the second template does not apply
drh9d9cf222007-02-13 15:01:11 +0000381** and the SELECT clause does not read from <table> at any time.
382** The generated code follows this template:
drh142e30d2002-08-28 03:00:58 +0000383**
drhe00ee6e2008-06-20 15:24:01 +0000384** EOF <- 0
385** X <- A
drh142e30d2002-08-28 03:00:58 +0000386** goto B
387** A: setup for the SELECT
drh9d9cf222007-02-13 15:01:11 +0000388** loop over the rows in the SELECT
drhe00ee6e2008-06-20 15:24:01 +0000389** load values into registers R..R+n
390** yield X
drh142e30d2002-08-28 03:00:58 +0000391** end loop
392** cleanup after the SELECT
drhe00ee6e2008-06-20 15:24:01 +0000393** EOF <- 1
394** yield X
drh142e30d2002-08-28 03:00:58 +0000395** goto A
drhe00ee6e2008-06-20 15:24:01 +0000396** B: open write cursor to <table> and its indices
397** C: yield X
398** if EOF goto D
399** insert the select result into <table> from R..R+n
400** goto C
drh142e30d2002-08-28 03:00:58 +0000401** D: cleanup
402**
drhe00ee6e2008-06-20 15:24:01 +0000403** The 4th template is used if the insert statement takes its
drh142e30d2002-08-28 03:00:58 +0000404** values from a SELECT but the data is being inserted into a table
405** that is also read as part of the SELECT. In the third form,
406** we have to use a intermediate table to store the results of
407** the select. The template is like this:
408**
drhe00ee6e2008-06-20 15:24:01 +0000409** EOF <- 0
410** X <- A
drh142e30d2002-08-28 03:00:58 +0000411** goto B
412** A: setup for the SELECT
413** loop over the tables in the SELECT
drhe00ee6e2008-06-20 15:24:01 +0000414** load value into register R..R+n
415** yield X
drh142e30d2002-08-28 03:00:58 +0000416** end loop
417** cleanup after the SELECT
drhe00ee6e2008-06-20 15:24:01 +0000418** EOF <- 1
419** yield X
420** halt-error
421** B: open temp table
422** L: yield X
423** if EOF goto M
424** insert row from R..R+n into temp table
425** goto L
426** M: open write cursor to <table> and its indices
427** rewind temp table
428** C: loop over rows of intermediate table
drh142e30d2002-08-28 03:00:58 +0000429** transfer values form intermediate table into <table>
drhe00ee6e2008-06-20 15:24:01 +0000430** end loop
431** D: cleanup
drhcce7d172000-05-31 15:34:51 +0000432*/
danielk19774adee202004-05-08 08:23:19 +0000433void sqlite3Insert(
drhcce7d172000-05-31 15:34:51 +0000434 Parse *pParse, /* Parser context */
drh113088e2003-03-20 01:16:58 +0000435 SrcList *pTabList, /* Name of table into which we are inserting */
drhcce7d172000-05-31 15:34:51 +0000436 ExprList *pList, /* List of values to be inserted */
drh5974a302000-06-07 14:42:26 +0000437 Select *pSelect, /* A SELECT statement to use as the data source */
drh9cfcf5d2002-01-29 18:41:24 +0000438 IdList *pColumn, /* Column names corresponding to IDLIST. */
439 int onError /* How to handle constraint errors */
drhcce7d172000-05-31 15:34:51 +0000440){
drh6a288a32008-01-07 19:20:24 +0000441 sqlite3 *db; /* The main database structure */
442 Table *pTab; /* The table to insert into. aka TABLE */
drh113088e2003-03-20 01:16:58 +0000443 char *zTab; /* Name of the table into which we are inserting */
drhe22a3342003-04-22 20:30:37 +0000444 const char *zDb; /* Name of the database holding this table */
drh5974a302000-06-07 14:42:26 +0000445 int i, j, idx; /* Loop counters */
446 Vdbe *v; /* Generate code into this virtual machine */
447 Index *pIdx; /* For looping over indices of the table */
drh967e8b72000-06-21 13:59:10 +0000448 int nColumn; /* Number of columns in the data */
drh6a288a32008-01-07 19:20:24 +0000449 int nHidden = 0; /* Number of hidden columns if TABLE is virtual */
drh04adf412008-01-08 18:57:50 +0000450 int baseCur = 0; /* VDBE Cursor number for pTab */
drh4a324312001-12-21 14:30:42 +0000451 int keyColumn = -1; /* Column that is the INTEGER PRIMARY KEY */
drh0ca3e242002-01-29 23:07:02 +0000452 int endOfLoop; /* Label for the end of the insertion loop */
danielk19774d887782005-02-08 08:42:27 +0000453 int useTempTable = 0; /* Store SELECT results in intermediate table */
danielk1977cfe9a692004-06-16 12:00:29 +0000454 int srcTab = 0; /* Data comes from this temporary cursor if >=0 */
drhe00ee6e2008-06-20 15:24:01 +0000455 int addrInsTop = 0; /* Jump to label "D" */
456 int addrCont = 0; /* Top of insert loop. Label "C" in templates 3 and 4 */
457 int addrSelect = 0; /* Address of coroutine that implements the SELECT */
drh2eb95372008-06-06 15:04:36 +0000458 SelectDest dest; /* Destination for SELECT on rhs of INSERT */
drh6a288a32008-01-07 19:20:24 +0000459 int iDb; /* Index of database holding TABLE */
drh2958a4e2004-11-12 03:56:15 +0000460 Db *pDb; /* The database containing table being inserted into */
drhe4d90812007-03-29 05:51:49 +0000461 int appendFlag = 0; /* True if the insert is likely to be an append */
drhcce7d172000-05-31 15:34:51 +0000462
drh6a288a32008-01-07 19:20:24 +0000463 /* Register allocations */
drh1bd10f82008-12-10 21:19:56 +0000464 int regFromSelect = 0;/* Base register for data coming from SELECT */
drh6a288a32008-01-07 19:20:24 +0000465 int regAutoinc = 0; /* Register holding the AUTOINCREMENT counter */
466 int regRowCount = 0; /* Memory cell used for the row counter */
467 int regIns; /* Block of regs holding rowid+data being inserted */
468 int regRowid; /* registers holding insert rowid */
469 int regData; /* register holding first column to insert */
470 int regRecord; /* Holds the assemblied row record */
drh1bd10f82008-12-10 21:19:56 +0000471 int regEof = 0; /* Register recording end of SELECT data */
drhaa9b8962008-01-08 02:57:55 +0000472 int *aRegIdx = 0; /* One register allocated to each index */
drh6a288a32008-01-07 19:20:24 +0000473
drh798da522004-11-04 04:42:28 +0000474#ifndef SQLITE_OMIT_TRIGGER
475 int isView; /* True if attempting to insert into a view */
danielk19772f886d12009-02-28 10:47:41 +0000476 Trigger *pTrigger; /* List of triggers on pTab, if required */
477 int tmask; /* Mask of trigger times */
drh798da522004-11-04 04:42:28 +0000478#endif
danielk1977c3f9bad2002-05-15 08:30:12 +0000479
drh17435752007-08-16 04:30:38 +0000480 db = pParse->db;
drh1bd10f82008-12-10 21:19:56 +0000481 memset(&dest, 0, sizeof(dest));
drh17435752007-08-16 04:30:38 +0000482 if( pParse->nErr || db->mallocFailed ){
drh6f7adc82006-01-11 21:41:20 +0000483 goto insert_cleanup;
484 }
drhdaffd0e2001-04-11 14:28:42 +0000485
drh1ccde152000-06-17 13:12:39 +0000486 /* Locate the table into which we will be inserting new information.
487 */
drh113088e2003-03-20 01:16:58 +0000488 assert( pTabList->nSrc==1 );
489 zTab = pTabList->a[0].zName;
drh098d1682009-05-02 15:46:46 +0000490 if( NEVER(zTab==0) ) goto insert_cleanup;
danielk19774adee202004-05-08 08:23:19 +0000491 pTab = sqlite3SrcListLookup(pParse, pTabList);
danielk1977c3f9bad2002-05-15 08:30:12 +0000492 if( pTab==0 ){
danielk1977c3f9bad2002-05-15 08:30:12 +0000493 goto insert_cleanup;
494 }
danielk1977da184232006-01-05 11:34:32 +0000495 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
496 assert( iDb<db->nDb );
497 pDb = &db->aDb[iDb];
drh2958a4e2004-11-12 03:56:15 +0000498 zDb = pDb->zName;
danielk19774adee202004-05-08 08:23:19 +0000499 if( sqlite3AuthCheck(pParse, SQLITE_INSERT, pTab->zName, 0, zDb) ){
drh1962bda2003-01-12 19:33:52 +0000500 goto insert_cleanup;
501 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000502
drhb7f91642004-10-31 02:22:47 +0000503 /* Figure out if we have any triggers and if the table being
504 ** inserted into is a view
505 */
506#ifndef SQLITE_OMIT_TRIGGER
danielk19772f886d12009-02-28 10:47:41 +0000507 pTrigger = sqlite3TriggersExist(pParse, pTab, TK_INSERT, 0, &tmask);
drhb7f91642004-10-31 02:22:47 +0000508 isView = pTab->pSelect!=0;
509#else
danielk19772f886d12009-02-28 10:47:41 +0000510# define pTrigger 0
511# define tmask 0
drhb7f91642004-10-31 02:22:47 +0000512# define isView 0
513#endif
514#ifdef SQLITE_OMIT_VIEW
515# undef isView
516# define isView 0
517#endif
danielk19772f886d12009-02-28 10:47:41 +0000518 assert( (pTrigger && tmask) || (pTrigger==0 && tmask==0) );
drhb7f91642004-10-31 02:22:47 +0000519
drhf573c992002-07-31 00:32:50 +0000520 /* If pTab is really a view, make sure it has been initialized.
danielk1977b3d24bf2006-06-19 03:05:10 +0000521 ** ViewGetColumnNames() is a no-op if pTab is not a view (or virtual
522 ** module table).
drhf573c992002-07-31 00:32:50 +0000523 */
danielk1977b3d24bf2006-06-19 03:05:10 +0000524 if( sqlite3ViewGetColumnNames(pParse, pTab) ){
drh5cf590c2003-04-24 01:45:04 +0000525 goto insert_cleanup;
drhf573c992002-07-31 00:32:50 +0000526 }
527
danielk1977595a5232009-07-24 17:58:53 +0000528 /* Ensure that:
529 * (a) the table is not read-only,
530 * (b) that if it is a view then ON INSERT triggers exist
531 */
532 if( sqlite3IsReadOnly(pParse, pTab, tmask) ){
533 goto insert_cleanup;
534 }
535
drh1ccde152000-06-17 13:12:39 +0000536 /* Allocate a VDBE
537 */
danielk19774adee202004-05-08 08:23:19 +0000538 v = sqlite3GetVdbe(pParse);
drh5974a302000-06-07 14:42:26 +0000539 if( v==0 ) goto insert_cleanup;
drh4794f732004-11-05 17:17:50 +0000540 if( pParse->nested==0 ) sqlite3VdbeCountChanges(v);
danielk19772f886d12009-02-28 10:47:41 +0000541 sqlite3BeginWriteOperation(pParse, pSelect || pTrigger, iDb);
drh1ccde152000-06-17 13:12:39 +0000542
drh9d9cf222007-02-13 15:01:11 +0000543#ifndef SQLITE_OMIT_XFER_OPT
544 /* If the statement is of the form
545 **
546 ** INSERT INTO <table1> SELECT * FROM <table2>;
547 **
548 ** Then special optimizations can be applied that make the transfer
549 ** very fast and which reduce fragmentation of indices.
drhe00ee6e2008-06-20 15:24:01 +0000550 **
551 ** This is the 2nd template.
drh9d9cf222007-02-13 15:01:11 +0000552 */
553 if( pColumn==0 && xferOptimization(pParse, pTab, pSelect, onError, iDb) ){
danielk19772f886d12009-02-28 10:47:41 +0000554 assert( !pTrigger );
drh9d9cf222007-02-13 15:01:11 +0000555 assert( pList==0 );
drh0b9f50d2009-06-23 20:28:53 +0000556 goto insert_end;
drh9d9cf222007-02-13 15:01:11 +0000557 }
558#endif /* SQLITE_OMIT_XFER_OPT */
559
drh2958a4e2004-11-12 03:56:15 +0000560 /* If this is an AUTOINCREMENT table, look up the sequence number in the
drh6a288a32008-01-07 19:20:24 +0000561 ** sqlite_sequence table and store it in memory cell regAutoinc.
drh2958a4e2004-11-12 03:56:15 +0000562 */
drh6a288a32008-01-07 19:20:24 +0000563 regAutoinc = autoIncBegin(pParse, iDb, pTab);
drh2958a4e2004-11-12 03:56:15 +0000564
drh1ccde152000-06-17 13:12:39 +0000565 /* Figure out how many columns of data are supplied. If the data
drhe00ee6e2008-06-20 15:24:01 +0000566 ** is coming from a SELECT statement, then generate a co-routine that
567 ** produces a single row of the SELECT on each invocation. The
568 ** co-routine is the common header to the 3rd and 4th templates.
drh1ccde152000-06-17 13:12:39 +0000569 */
drh5974a302000-06-07 14:42:26 +0000570 if( pSelect ){
drh142e30d2002-08-28 03:00:58 +0000571 /* Data is coming from a SELECT. Generate code to implement that SELECT
drhe00ee6e2008-06-20 15:24:01 +0000572 ** as a co-routine. The code is common to both the 3rd and 4th
573 ** templates:
574 **
575 ** EOF <- 0
576 ** X <- A
577 ** goto B
578 ** A: setup for the SELECT
579 ** loop over the tables in the SELECT
580 ** load value into register R..R+n
581 ** yield X
582 ** end loop
583 ** cleanup after the SELECT
584 ** EOF <- 1
585 ** yield X
586 ** halt-error
587 **
588 ** On each invocation of the co-routine, it puts a single row of the
589 ** SELECT result into registers dest.iMem...dest.iMem+dest.nMem-1.
590 ** (These output registers are allocated by sqlite3Select().) When
591 ** the SELECT completes, it sets the EOF flag stored in regEof.
drh142e30d2002-08-28 03:00:58 +0000592 */
drhe00ee6e2008-06-20 15:24:01 +0000593 int rc, j1;
drh1013c932008-01-06 00:25:21 +0000594
drhe00ee6e2008-06-20 15:24:01 +0000595 regEof = ++pParse->nMem;
596 sqlite3VdbeAddOp2(v, OP_Integer, 0, regEof); /* EOF <- 0 */
597 VdbeComment((v, "SELECT eof flag"));
drh92b01d52008-06-24 00:32:35 +0000598 sqlite3SelectDestInit(&dest, SRT_Coroutine, ++pParse->nMem);
drhe00ee6e2008-06-20 15:24:01 +0000599 addrSelect = sqlite3VdbeCurrentAddr(v)+2;
drh92b01d52008-06-24 00:32:35 +0000600 sqlite3VdbeAddOp2(v, OP_Integer, addrSelect-1, dest.iParm);
drhe00ee6e2008-06-20 15:24:01 +0000601 j1 = sqlite3VdbeAddOp2(v, OP_Goto, 0, 0);
602 VdbeComment((v, "Jump over SELECT coroutine"));
danielk1977b3bce662005-01-29 08:32:43 +0000603
604 /* Resolve the expressions in the SELECT statement and execute it. */
drh7d10d5a2008-08-20 16:35:10 +0000605 rc = sqlite3Select(pParse, pSelect, &dest);
drh098d1682009-05-02 15:46:46 +0000606 assert( pParse->nErr==0 || rc );
607 if( rc || NEVER(pParse->nErr) || db->mallocFailed ){
drh6f7adc82006-01-11 21:41:20 +0000608 goto insert_cleanup;
609 }
drhe00ee6e2008-06-20 15:24:01 +0000610 sqlite3VdbeAddOp2(v, OP_Integer, 1, regEof); /* EOF <- 1 */
drh92b01d52008-06-24 00:32:35 +0000611 sqlite3VdbeAddOp1(v, OP_Yield, dest.iParm); /* yield X */
drhe00ee6e2008-06-20 15:24:01 +0000612 sqlite3VdbeAddOp2(v, OP_Halt, SQLITE_INTERNAL, OE_Abort);
613 VdbeComment((v, "End of SELECT coroutine"));
614 sqlite3VdbeJumpHere(v, j1); /* label B: */
danielk1977b3bce662005-01-29 08:32:43 +0000615
drh6a288a32008-01-07 19:20:24 +0000616 regFromSelect = dest.iMem;
drh5974a302000-06-07 14:42:26 +0000617 assert( pSelect->pEList );
drh967e8b72000-06-21 13:59:10 +0000618 nColumn = pSelect->pEList->nExpr;
drhe00ee6e2008-06-20 15:24:01 +0000619 assert( dest.nMem==nColumn );
drh142e30d2002-08-28 03:00:58 +0000620
621 /* Set useTempTable to TRUE if the result of the SELECT statement
drhe00ee6e2008-06-20 15:24:01 +0000622 ** should be written into a temporary table (template 4). Set to
623 ** FALSE if each* row of the SELECT can be written directly into
624 ** the destination table (template 3).
drh048c5302003-04-03 01:50:44 +0000625 **
626 ** A temp table must be used if the table being updated is also one
627 ** of the tables being read by the SELECT statement. Also use a
628 ** temp table in the case of row triggers.
drh142e30d2002-08-28 03:00:58 +0000629 */
danielk1977595a5232009-07-24 17:58:53 +0000630 if( pTrigger || readsTable(pParse, addrSelect, iDb, pTab) ){
drh048c5302003-04-03 01:50:44 +0000631 useTempTable = 1;
drh048c5302003-04-03 01:50:44 +0000632 }
drh142e30d2002-08-28 03:00:58 +0000633
634 if( useTempTable ){
drhe00ee6e2008-06-20 15:24:01 +0000635 /* Invoke the coroutine to extract information from the SELECT
636 ** and add it to a transient table srcTab. The code generated
637 ** here is from the 4th template:
638 **
639 ** B: open temp table
640 ** L: yield X
641 ** if EOF goto M
642 ** insert row from R..R+n into temp table
643 ** goto L
644 ** M: ...
drh142e30d2002-08-28 03:00:58 +0000645 */
drhdc5ea5c2008-12-10 17:19:59 +0000646 int regRec; /* Register to hold packed record */
647 int regTempRowid; /* Register to hold temp table ROWID */
648 int addrTop; /* Label "L" */
649 int addrIf; /* Address of jump to M */
drhb7654112008-01-12 12:48:07 +0000650
drh142e30d2002-08-28 03:00:58 +0000651 srcTab = pParse->nTab++;
drhb7654112008-01-12 12:48:07 +0000652 regRec = sqlite3GetTempReg(pParse);
drhdc5ea5c2008-12-10 17:19:59 +0000653 regTempRowid = sqlite3GetTempReg(pParse);
drhe00ee6e2008-06-20 15:24:01 +0000654 sqlite3VdbeAddOp2(v, OP_OpenEphemeral, srcTab, nColumn);
drh92b01d52008-06-24 00:32:35 +0000655 addrTop = sqlite3VdbeAddOp1(v, OP_Yield, dest.iParm);
drhe00ee6e2008-06-20 15:24:01 +0000656 addrIf = sqlite3VdbeAddOp1(v, OP_If, regEof);
drh1db639c2008-01-17 02:36:28 +0000657 sqlite3VdbeAddOp3(v, OP_MakeRecord, regFromSelect, nColumn, regRec);
drhdc5ea5c2008-12-10 17:19:59 +0000658 sqlite3VdbeAddOp2(v, OP_NewRowid, srcTab, regTempRowid);
659 sqlite3VdbeAddOp3(v, OP_Insert, srcTab, regRec, regTempRowid);
drhe00ee6e2008-06-20 15:24:01 +0000660 sqlite3VdbeAddOp2(v, OP_Goto, 0, addrTop);
661 sqlite3VdbeJumpHere(v, addrIf);
drhb7654112008-01-12 12:48:07 +0000662 sqlite3ReleaseTempReg(pParse, regRec);
drhdc5ea5c2008-12-10 17:19:59 +0000663 sqlite3ReleaseTempReg(pParse, regTempRowid);
drh142e30d2002-08-28 03:00:58 +0000664 }
drh5974a302000-06-07 14:42:26 +0000665 }else{
drh142e30d2002-08-28 03:00:58 +0000666 /* This is the case if the data for the INSERT is coming from a VALUES
667 ** clause
668 */
danielk1977b3bce662005-01-29 08:32:43 +0000669 NameContext sNC;
670 memset(&sNC, 0, sizeof(sNC));
671 sNC.pParse = pParse;
drh5974a302000-06-07 14:42:26 +0000672 srcTab = -1;
drh48d11782007-11-23 15:02:19 +0000673 assert( useTempTable==0 );
drh147d0cc2006-08-25 23:42:53 +0000674 nColumn = pList ? pList->nExpr : 0;
drhe64e7b22002-02-18 13:56:36 +0000675 for(i=0; i<nColumn; i++){
drh7d10d5a2008-08-20 16:35:10 +0000676 if( sqlite3ResolveExprNames(&sNC, pList->a[i].pExpr) ){
drhb04a5d82002-04-12 03:55:15 +0000677 goto insert_cleanup;
678 }
drhe64e7b22002-02-18 13:56:36 +0000679 }
drh5974a302000-06-07 14:42:26 +0000680 }
drh1ccde152000-06-17 13:12:39 +0000681
682 /* Make sure the number of columns in the source data matches the number
683 ** of columns to be inserted into the table.
684 */
danielk1977034ca142007-06-26 10:38:54 +0000685 if( IsVirtual(pTab) ){
686 for(i=0; i<pTab->nCol; i++){
687 nHidden += (IsHiddenColumn(&pTab->aCol[i]) ? 1 : 0);
688 }
689 }
690 if( pColumn==0 && nColumn && nColumn!=(pTab->nCol-nHidden) ){
danielk19774adee202004-05-08 08:23:19 +0000691 sqlite3ErrorMsg(pParse,
drhda93d232003-03-31 02:12:46 +0000692 "table %S has %d columns but %d values were supplied",
drhd51397a2009-05-01 15:17:48 +0000693 pTabList, 0, pTab->nCol-nHidden, nColumn);
drhcce7d172000-05-31 15:34:51 +0000694 goto insert_cleanup;
695 }
drh967e8b72000-06-21 13:59:10 +0000696 if( pColumn!=0 && nColumn!=pColumn->nId ){
danielk19774adee202004-05-08 08:23:19 +0000697 sqlite3ErrorMsg(pParse, "%d values for %d columns", nColumn, pColumn->nId);
drhcce7d172000-05-31 15:34:51 +0000698 goto insert_cleanup;
699 }
drh1ccde152000-06-17 13:12:39 +0000700
701 /* If the INSERT statement included an IDLIST term, then make sure
702 ** all elements of the IDLIST really are columns of the table and
703 ** remember the column indices.
drhc8392582001-12-31 02:48:51 +0000704 **
705 ** If the table has an INTEGER PRIMARY KEY column and that column
706 ** is named in the IDLIST, then record in the keyColumn variable
707 ** the index into IDLIST of the primary key column. keyColumn is
708 ** the index of the primary key as it appears in IDLIST, not as
709 ** is appears in the original table. (The index of the primary
710 ** key in the original table is pTab->iPKey.)
drh1ccde152000-06-17 13:12:39 +0000711 */
drh967e8b72000-06-21 13:59:10 +0000712 if( pColumn ){
713 for(i=0; i<pColumn->nId; i++){
714 pColumn->a[i].idx = -1;
drhcce7d172000-05-31 15:34:51 +0000715 }
drh967e8b72000-06-21 13:59:10 +0000716 for(i=0; i<pColumn->nId; i++){
drhcce7d172000-05-31 15:34:51 +0000717 for(j=0; j<pTab->nCol; j++){
danielk19774adee202004-05-08 08:23:19 +0000718 if( sqlite3StrICmp(pColumn->a[i].zName, pTab->aCol[j].zName)==0 ){
drh967e8b72000-06-21 13:59:10 +0000719 pColumn->a[i].idx = j;
drh4a324312001-12-21 14:30:42 +0000720 if( j==pTab->iPKey ){
drh9aa028d2001-12-22 21:48:29 +0000721 keyColumn = i;
drh4a324312001-12-21 14:30:42 +0000722 }
drhcce7d172000-05-31 15:34:51 +0000723 break;
724 }
725 }
726 if( j>=pTab->nCol ){
danielk19774adee202004-05-08 08:23:19 +0000727 if( sqlite3IsRowid(pColumn->a[i].zName) ){
drha0217ba2003-06-01 01:10:33 +0000728 keyColumn = i;
729 }else{
danielk19774adee202004-05-08 08:23:19 +0000730 sqlite3ErrorMsg(pParse, "table %S has no column named %s",
drha0217ba2003-06-01 01:10:33 +0000731 pTabList, 0, pColumn->a[i].zName);
732 pParse->nErr++;
733 goto insert_cleanup;
734 }
drhcce7d172000-05-31 15:34:51 +0000735 }
736 }
737 }
drh1ccde152000-06-17 13:12:39 +0000738
drhaacc5432002-01-06 17:07:40 +0000739 /* If there is no IDLIST term but the table has an integer primary
drhc8392582001-12-31 02:48:51 +0000740 ** key, the set the keyColumn variable to the primary key column index
741 ** in the original table definition.
drh4a324312001-12-21 14:30:42 +0000742 */
drh147d0cc2006-08-25 23:42:53 +0000743 if( pColumn==0 && nColumn>0 ){
drh4a324312001-12-21 14:30:42 +0000744 keyColumn = pTab->iPKey;
745 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000746
drhfeeb1392002-04-09 03:28:01 +0000747 /* Initialize the count of rows to be inserted
748 */
drh142e30d2002-08-28 03:00:58 +0000749 if( db->flags & SQLITE_CountRows ){
drh6a288a32008-01-07 19:20:24 +0000750 regRowCount = ++pParse->nMem;
751 sqlite3VdbeAddOp2(v, OP_Integer, 0, regRowCount);
drhfeeb1392002-04-09 03:28:01 +0000752 }
753
danielk1977e448dc42008-01-02 11:50:51 +0000754 /* If this is not a view, open the table and and all indices */
755 if( !isView ){
drhaa9b8962008-01-08 02:57:55 +0000756 int nIdx;
drhaa9b8962008-01-08 02:57:55 +0000757
drh04adf412008-01-08 18:57:50 +0000758 baseCur = pParse->nTab;
759 nIdx = sqlite3OpenTableAndIndices(pParse, pTab, baseCur, OP_OpenWrite);
drh5c070532008-04-11 15:36:03 +0000760 aRegIdx = sqlite3DbMallocRaw(db, sizeof(int)*(nIdx+1));
drhaa9b8962008-01-08 02:57:55 +0000761 if( aRegIdx==0 ){
762 goto insert_cleanup;
763 }
764 for(i=0; i<nIdx; i++){
765 aRegIdx[i] = ++pParse->nMem;
766 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000767 }
768
drhe00ee6e2008-06-20 15:24:01 +0000769 /* This is the top of the main insertion loop */
drh142e30d2002-08-28 03:00:58 +0000770 if( useTempTable ){
drhe00ee6e2008-06-20 15:24:01 +0000771 /* This block codes the top of loop only. The complete loop is the
772 ** following pseudocode (template 4):
773 **
774 ** rewind temp table
775 ** C: loop over rows of intermediate table
776 ** transfer values form intermediate table into <table>
777 ** end loop
778 ** D: ...
779 */
780 addrInsTop = sqlite3VdbeAddOp1(v, OP_Rewind, srcTab);
781 addrCont = sqlite3VdbeCurrentAddr(v);
drh142e30d2002-08-28 03:00:58 +0000782 }else if( pSelect ){
drhe00ee6e2008-06-20 15:24:01 +0000783 /* This block codes the top of loop only. The complete loop is the
784 ** following pseudocode (template 3):
785 **
786 ** C: yield X
787 ** if EOF goto D
788 ** insert the select result into <table> from R..R+n
789 ** goto C
790 ** D: ...
791 */
drh92b01d52008-06-24 00:32:35 +0000792 addrCont = sqlite3VdbeAddOp1(v, OP_Yield, dest.iParm);
drhe00ee6e2008-06-20 15:24:01 +0000793 addrInsTop = sqlite3VdbeAddOp1(v, OP_If, regEof);
drh5974a302000-06-07 14:42:26 +0000794 }
drh1ccde152000-06-17 13:12:39 +0000795
drh6a288a32008-01-07 19:20:24 +0000796 /* Allocate registers for holding the rowid of the new row,
797 ** the content of the new row, and the assemblied row record.
798 */
799 regRecord = ++pParse->nMem;
800 regRowid = regIns = pParse->nMem+1;
801 pParse->nMem += pTab->nCol + 1;
802 if( IsVirtual(pTab) ){
803 regRowid++;
804 pParse->nMem++;
805 }
806 regData = regRowid+1;
807
drh5cf590c2003-04-24 01:45:04 +0000808 /* Run the BEFORE and INSTEAD OF triggers, if there are any
drh70ce3f02003-04-15 19:22:22 +0000809 */
danielk19774adee202004-05-08 08:23:19 +0000810 endOfLoop = sqlite3VdbeMakeLabel(v);
danielk19772f886d12009-02-28 10:47:41 +0000811 if( tmask & TRIGGER_BEFORE ){
dan76d462e2009-08-30 11:42:51 +0000812 int regCols = sqlite3GetTempRange(pParse, pTab->nCol+1);
danielk1977c3f9bad2002-05-15 08:30:12 +0000813
drh70ce3f02003-04-15 19:22:22 +0000814 /* build the NEW.* reference row. Note that if there is an INTEGER
815 ** PRIMARY KEY into which a NULL is being inserted, that NULL will be
816 ** translated into a unique ID for the row. But on a BEFORE trigger,
817 ** we do not know what the unique ID will be (because the insert has
818 ** not happened yet) so we substitute a rowid of -1
819 */
820 if( keyColumn<0 ){
dan76d462e2009-08-30 11:42:51 +0000821 sqlite3VdbeAddOp2(v, OP_Integer, -1, regCols);
drh70ce3f02003-04-15 19:22:22 +0000822 }else{
drh6a288a32008-01-07 19:20:24 +0000823 int j1;
drh7fe45902009-05-01 02:08:04 +0000824 if( useTempTable ){
dan76d462e2009-08-30 11:42:51 +0000825 sqlite3VdbeAddOp3(v, OP_Column, srcTab, keyColumn, regCols);
drh7fe45902009-05-01 02:08:04 +0000826 }else{
827 assert( pSelect==0 ); /* Otherwise useTempTable is true */
dan76d462e2009-08-30 11:42:51 +0000828 sqlite3ExprCode(pParse, pList->a[keyColumn].pExpr, regCols);
drh7fe45902009-05-01 02:08:04 +0000829 }
dan76d462e2009-08-30 11:42:51 +0000830 j1 = sqlite3VdbeAddOp1(v, OP_NotNull, regCols);
831 sqlite3VdbeAddOp2(v, OP_Integer, -1, regCols);
drh6a288a32008-01-07 19:20:24 +0000832 sqlite3VdbeJumpHere(v, j1);
dan76d462e2009-08-30 11:42:51 +0000833 sqlite3VdbeAddOp1(v, OP_MustBeInt, regCols);
drh70ce3f02003-04-15 19:22:22 +0000834 }
835
danielk1977034ca142007-06-26 10:38:54 +0000836 /* Cannot have triggers on a virtual table. If it were possible,
837 ** this block would have to account for hidden column.
838 */
dan165921a2009-08-28 18:53:45 +0000839 assert( !IsVirtual(pTab) );
danielk1977034ca142007-06-26 10:38:54 +0000840
drh70ce3f02003-04-15 19:22:22 +0000841 /* Create the new column data
842 */
danielk1977c3f9bad2002-05-15 08:30:12 +0000843 for(i=0; i<pTab->nCol; i++){
844 if( pColumn==0 ){
drh9adf9ac2002-05-15 11:44:13 +0000845 j = i;
danielk1977c3f9bad2002-05-15 08:30:12 +0000846 }else{
drh9adf9ac2002-05-15 11:44:13 +0000847 for(j=0; j<pColumn->nId; j++){
848 if( pColumn->a[j].idx==i ) break;
849 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000850 }
851 if( pColumn && j>=pColumn->nId ){
dan76d462e2009-08-30 11:42:51 +0000852 sqlite3ExprCode(pParse, pTab->aCol[i].pDflt, regCols+i+1);
drh142e30d2002-08-28 03:00:58 +0000853 }else if( useTempTable ){
dan76d462e2009-08-30 11:42:51 +0000854 sqlite3VdbeAddOp3(v, OP_Column, srcTab, j, regCols+i+1);
danielk1977c3f9bad2002-05-15 08:30:12 +0000855 }else{
drhd6fe9612005-01-14 01:22:00 +0000856 assert( pSelect==0 ); /* Otherwise useTempTable is true */
dan76d462e2009-08-30 11:42:51 +0000857 sqlite3ExprCodeAndCache(pParse, pList->a[j].pExpr, regCols+i+1);
danielk1977c3f9bad2002-05-15 08:30:12 +0000858 }
859 }
danielk1977a37cdde2004-05-16 11:15:36 +0000860
861 /* If this is an INSERT on a view with an INSTEAD OF INSERT trigger,
862 ** do not attempt any conversions before assembling the record.
863 ** If this is a real table, attempt conversions as required by the
864 ** table column affinities.
865 */
866 if( !isView ){
dan76d462e2009-08-30 11:42:51 +0000867 sqlite3VdbeAddOp2(v, OP_Affinity, regCols+1, pTab->nCol);
danielk1977a37cdde2004-05-16 11:15:36 +0000868 sqlite3TableAffinityStr(v, pTab);
869 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000870
drh5cf590c2003-04-24 01:45:04 +0000871 /* Fire BEFORE or INSTEAD OF triggers */
dan165921a2009-08-28 18:53:45 +0000872 sqlite3CodeRowTrigger(pParse, pTrigger, TK_INSERT, 0, TRIGGER_BEFORE,
dan94d7f502009-09-24 09:05:49 +0000873 pTab, regCols-pTab->nCol-1, onError, endOfLoop);
dan165921a2009-08-28 18:53:45 +0000874
dan76d462e2009-08-30 11:42:51 +0000875 sqlite3ReleaseTempRange(pParse, regCols, pTab->nCol+1);
drh70ce3f02003-04-15 19:22:22 +0000876 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000877
drh4a324312001-12-21 14:30:42 +0000878 /* Push the record number for the new entry onto the stack. The
drhf0863fe2005-06-12 21:35:51 +0000879 ** record number is a randomly generate integer created by NewRowid
drh4a324312001-12-21 14:30:42 +0000880 ** except when the table has an INTEGER PRIMARY KEY column, in which
drhb419a922002-01-30 16:17:23 +0000881 ** case the record number is the same as that column.
drh1ccde152000-06-17 13:12:39 +0000882 */
drh5cf590c2003-04-24 01:45:04 +0000883 if( !isView ){
drh4cbdda92006-06-14 19:00:20 +0000884 if( IsVirtual(pTab) ){
danielk1977287fb612008-01-04 19:10:28 +0000885 /* The row that the VUpdate opcode will delete: none */
drh6a288a32008-01-07 19:20:24 +0000886 sqlite3VdbeAddOp2(v, OP_Null, 0, regIns);
drh4cbdda92006-06-14 19:00:20 +0000887 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000888 if( keyColumn>=0 ){
drh142e30d2002-08-28 03:00:58 +0000889 if( useTempTable ){
drh6a288a32008-01-07 19:20:24 +0000890 sqlite3VdbeAddOp3(v, OP_Column, srcTab, keyColumn, regRowid);
drh142e30d2002-08-28 03:00:58 +0000891 }else if( pSelect ){
drhb7654112008-01-12 12:48:07 +0000892 sqlite3VdbeAddOp2(v, OP_SCopy, regFromSelect+keyColumn, regRowid);
danielk1977c3f9bad2002-05-15 08:30:12 +0000893 }else{
drhe4d90812007-03-29 05:51:49 +0000894 VdbeOp *pOp;
drh1db639c2008-01-17 02:36:28 +0000895 sqlite3ExprCode(pParse, pList->a[keyColumn].pExpr, regRowid);
drh20411ea2009-05-29 19:00:12 +0000896 pOp = sqlite3VdbeGetOp(v, -1);
drh1b7ecbb2009-05-03 01:00:59 +0000897 if( ALWAYS(pOp) && pOp->opcode==OP_Null && !IsVirtual(pTab) ){
drhe4d90812007-03-29 05:51:49 +0000898 appendFlag = 1;
899 pOp->opcode = OP_NewRowid;
drh04adf412008-01-08 18:57:50 +0000900 pOp->p1 = baseCur;
drh6a288a32008-01-07 19:20:24 +0000901 pOp->p2 = regRowid;
902 pOp->p3 = regAutoinc;
drhe4d90812007-03-29 05:51:49 +0000903 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000904 }
drhf0863fe2005-06-12 21:35:51 +0000905 /* If the PRIMARY KEY expression is NULL, then use OP_NewRowid
drh27a32782002-06-19 20:32:43 +0000906 ** to generate a unique primary key value.
907 */
drhe4d90812007-03-29 05:51:49 +0000908 if( !appendFlag ){
drh1db639c2008-01-17 02:36:28 +0000909 int j1;
danielk1977bb50e7a2008-07-04 10:56:07 +0000910 if( !IsVirtual(pTab) ){
911 j1 = sqlite3VdbeAddOp1(v, OP_NotNull, regRowid);
912 sqlite3VdbeAddOp3(v, OP_NewRowid, baseCur, regRowid, regAutoinc);
913 sqlite3VdbeJumpHere(v, j1);
914 }else{
915 j1 = sqlite3VdbeCurrentAddr(v);
916 sqlite3VdbeAddOp2(v, OP_IsNull, regRowid, j1+2);
917 }
drh3c84ddf2008-01-09 02:15:38 +0000918 sqlite3VdbeAddOp1(v, OP_MustBeInt, regRowid);
drhe4d90812007-03-29 05:51:49 +0000919 }
drh4cbdda92006-06-14 19:00:20 +0000920 }else if( IsVirtual(pTab) ){
drh6a288a32008-01-07 19:20:24 +0000921 sqlite3VdbeAddOp2(v, OP_Null, 0, regRowid);
danielk1977c3f9bad2002-05-15 08:30:12 +0000922 }else{
drh04adf412008-01-08 18:57:50 +0000923 sqlite3VdbeAddOp3(v, OP_NewRowid, baseCur, regRowid, regAutoinc);
drhe4d90812007-03-29 05:51:49 +0000924 appendFlag = 1;
drh4a324312001-12-21 14:30:42 +0000925 }
drh6a288a32008-01-07 19:20:24 +0000926 autoIncStep(pParse, regAutoinc, regRowid);
drh4a324312001-12-21 14:30:42 +0000927
danielk1977c3f9bad2002-05-15 08:30:12 +0000928 /* Push onto the stack, data for all columns of the new entry, beginning
danielk1977f29ce552002-05-19 23:43:12 +0000929 ** with the first column.
930 */
danielk1977034ca142007-06-26 10:38:54 +0000931 nHidden = 0;
danielk1977c3f9bad2002-05-15 08:30:12 +0000932 for(i=0; i<pTab->nCol; i++){
drh6a288a32008-01-07 19:20:24 +0000933 int iRegStore = regRowid+1+i;
danielk1977c3f9bad2002-05-15 08:30:12 +0000934 if( i==pTab->iPKey ){
drh9adf9ac2002-05-15 11:44:13 +0000935 /* The value of the INTEGER PRIMARY KEY column is always a NULL.
danielk1977f29ce552002-05-19 23:43:12 +0000936 ** Whenever this column is read, the record number will be substituted
937 ** in its place. So will fill this column with a NULL to avoid
938 ** taking up data space with information that will never be used. */
drh4c583122008-01-04 22:01:03 +0000939 sqlite3VdbeAddOp2(v, OP_Null, 0, iRegStore);
drh9adf9ac2002-05-15 11:44:13 +0000940 continue;
danielk1977c3f9bad2002-05-15 08:30:12 +0000941 }
942 if( pColumn==0 ){
danielk1977034ca142007-06-26 10:38:54 +0000943 if( IsHiddenColumn(&pTab->aCol[i]) ){
944 assert( IsVirtual(pTab) );
945 j = -1;
946 nHidden++;
947 }else{
948 j = i - nHidden;
949 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000950 }else{
drh9adf9ac2002-05-15 11:44:13 +0000951 for(j=0; j<pColumn->nId; j++){
952 if( pColumn->a[j].idx==i ) break;
953 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000954 }
danielk1977034ca142007-06-26 10:38:54 +0000955 if( j<0 || nColumn==0 || (pColumn && j>=pColumn->nId) ){
danielk1977287fb612008-01-04 19:10:28 +0000956 sqlite3ExprCode(pParse, pTab->aCol[i].pDflt, iRegStore);
drh142e30d2002-08-28 03:00:58 +0000957 }else if( useTempTable ){
danielk1977287fb612008-01-04 19:10:28 +0000958 sqlite3VdbeAddOp3(v, OP_Column, srcTab, j, iRegStore);
drh142e30d2002-08-28 03:00:58 +0000959 }else if( pSelect ){
drhb7654112008-01-12 12:48:07 +0000960 sqlite3VdbeAddOp2(v, OP_SCopy, regFromSelect+j, iRegStore);
danielk1977c3f9bad2002-05-15 08:30:12 +0000961 }else{
danielk1977287fb612008-01-04 19:10:28 +0000962 sqlite3ExprCode(pParse, pList->a[j].pExpr, iRegStore);
drh5974a302000-06-07 14:42:26 +0000963 }
drhbed86902000-06-02 13:27:59 +0000964 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000965
966 /* Generate code to check constraints and generate index keys and
danielk1977f29ce552002-05-19 23:43:12 +0000967 ** do the insertion.
968 */
drh4cbdda92006-06-14 19:00:20 +0000969#ifndef SQLITE_OMIT_VIRTUALTABLE
970 if( IsVirtual(pTab) ){
danielk1977595a5232009-07-24 17:58:53 +0000971 const char *pVTab = (const char *)sqlite3GetVTable(db, pTab);
drh4f3dd152008-04-28 18:46:43 +0000972 sqlite3VtabMakeWritable(pParse, pTab);
danielk1977595a5232009-07-24 17:58:53 +0000973 sqlite3VdbeAddOp4(v, OP_VUpdate, 1, pTab->nCol+2, regIns, pVTab, P4_VTAB);
dane0af83a2009-09-08 19:15:01 +0000974 sqlite3MayAbort(pParse);
drh4cbdda92006-06-14 19:00:20 +0000975 }else
976#endif
977 {
danielk1977de630352009-05-04 11:42:29 +0000978 int isReplace; /* Set to true if constraints may cause a replace */
979 sqlite3GenerateConstraintChecks(pParse, pTab, baseCur, regIns, aRegIdx,
980 keyColumn>=0, 0, onError, endOfLoop, &isReplace
drh04adf412008-01-08 18:57:50 +0000981 );
dan1da40a32009-09-19 17:00:31 +0000982 sqlite3FkCheck(pParse, pTab, 0, 0, regIns);
drh04adf412008-01-08 18:57:50 +0000983 sqlite3CompleteInsertion(
dan2832ad42009-08-31 15:27:27 +0000984 pParse, pTab, baseCur, regIns, aRegIdx, 0, appendFlag, isReplace==0
danielk1977de630352009-05-04 11:42:29 +0000985 );
drh4cbdda92006-06-14 19:00:20 +0000986 }
drh5cf590c2003-04-24 01:45:04 +0000987 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000988
drh5cf590c2003-04-24 01:45:04 +0000989 /* Update the count of rows that are inserted
990 */
991 if( (db->flags & SQLITE_CountRows)!=0 ){
drh6a288a32008-01-07 19:20:24 +0000992 sqlite3VdbeAddOp2(v, OP_AddImm, regRowCount, 1);
drh5974a302000-06-07 14:42:26 +0000993 }
drh1ccde152000-06-17 13:12:39 +0000994
danielk19772f886d12009-02-28 10:47:41 +0000995 if( pTrigger ){
danielk1977c3f9bad2002-05-15 08:30:12 +0000996 /* Code AFTER triggers */
dan165921a2009-08-28 18:53:45 +0000997 sqlite3CodeRowTrigger(pParse, pTrigger, TK_INSERT, 0, TRIGGER_AFTER,
dan94d7f502009-09-24 09:05:49 +0000998 pTab, regData-2-pTab->nCol, onError, endOfLoop);
drh1bee3d72001-10-15 00:44:35 +0000999 }
1000
drhe00ee6e2008-06-20 15:24:01 +00001001 /* The bottom of the main insertion loop, if the data source
1002 ** is a SELECT statement.
danielk1977f29ce552002-05-19 23:43:12 +00001003 */
danielk19774adee202004-05-08 08:23:19 +00001004 sqlite3VdbeResolveLabel(v, endOfLoop);
drh142e30d2002-08-28 03:00:58 +00001005 if( useTempTable ){
drhe00ee6e2008-06-20 15:24:01 +00001006 sqlite3VdbeAddOp2(v, OP_Next, srcTab, addrCont);
1007 sqlite3VdbeJumpHere(v, addrInsTop);
drh2eb95372008-06-06 15:04:36 +00001008 sqlite3VdbeAddOp1(v, OP_Close, srcTab);
drh142e30d2002-08-28 03:00:58 +00001009 }else if( pSelect ){
drhe00ee6e2008-06-20 15:24:01 +00001010 sqlite3VdbeAddOp2(v, OP_Goto, 0, addrCont);
1011 sqlite3VdbeJumpHere(v, addrInsTop);
drh6b563442001-11-07 16:48:26 +00001012 }
danielk1977c3f9bad2002-05-15 08:30:12 +00001013
danielk1977e448dc42008-01-02 11:50:51 +00001014 if( !IsVirtual(pTab) && !isView ){
danielk1977c3f9bad2002-05-15 08:30:12 +00001015 /* Close all tables opened */
drh2eb95372008-06-06 15:04:36 +00001016 sqlite3VdbeAddOp1(v, OP_Close, baseCur);
danielk1977c3f9bad2002-05-15 08:30:12 +00001017 for(idx=1, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, idx++){
drh2eb95372008-06-06 15:04:36 +00001018 sqlite3VdbeAddOp1(v, OP_Close, idx+baseCur);
danielk1977c3f9bad2002-05-15 08:30:12 +00001019 }
drhcce7d172000-05-31 15:34:51 +00001020 }
danielk1977c3f9bad2002-05-15 08:30:12 +00001021
drh0b9f50d2009-06-23 20:28:53 +00001022insert_end:
drhf3388142004-11-13 03:48:06 +00001023 /* Update the sqlite_sequence table by storing the content of the
drh0b9f50d2009-06-23 20:28:53 +00001024 ** maximum rowid counter values recorded while inserting into
1025 ** autoincrement tables.
drh2958a4e2004-11-12 03:56:15 +00001026 */
dan165921a2009-08-28 18:53:45 +00001027 if( pParse->nested==0 && pParse->pTriggerTab==0 ){
drh0b9f50d2009-06-23 20:28:53 +00001028 sqlite3AutoincrementEnd(pParse);
1029 }
drh2958a4e2004-11-12 03:56:15 +00001030
drh1bee3d72001-10-15 00:44:35 +00001031 /*
danielk1977e7de6f22004-11-05 06:02:06 +00001032 ** Return the number of rows inserted. If this routine is
1033 ** generating code because of a call to sqlite3NestedParse(), do not
1034 ** invoke the callback function.
drh1bee3d72001-10-15 00:44:35 +00001035 */
dan165921a2009-08-28 18:53:45 +00001036 if( (db->flags&SQLITE_CountRows) && !pParse->nested && !pParse->pTriggerTab ){
drh6a288a32008-01-07 19:20:24 +00001037 sqlite3VdbeAddOp2(v, OP_ResultRow, regRowCount, 1);
danielk197722322fd2004-05-25 23:35:17 +00001038 sqlite3VdbeSetNumCols(v, 1);
danielk197710fb7492008-10-31 10:53:22 +00001039 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "rows inserted", SQLITE_STATIC);
drh1bee3d72001-10-15 00:44:35 +00001040 }
drhcce7d172000-05-31 15:34:51 +00001041
1042insert_cleanup:
drh633e6d52008-07-28 19:34:53 +00001043 sqlite3SrcListDelete(db, pTabList);
1044 sqlite3ExprListDelete(db, pList);
1045 sqlite3SelectDelete(db, pSelect);
1046 sqlite3IdListDelete(db, pColumn);
1047 sqlite3DbFree(db, aRegIdx);
drhcce7d172000-05-31 15:34:51 +00001048}
drh9cfcf5d2002-01-29 18:41:24 +00001049
dan75cbd982009-09-21 16:06:03 +00001050/* Make sure "isView" and other macros defined above are undefined. Otherwise
1051** thely may interfere with compilation of other functions in this file
1052** (or in another file, if this file becomes part of the amalgamation). */
1053#ifdef isView
1054 #undef isView
1055#endif
1056#ifdef pTrigger
1057 #undef pTrigger
1058#endif
1059#ifdef tmask
1060 #undef tmask
1061#endif
1062
1063
drh9cfcf5d2002-01-29 18:41:24 +00001064/*
drh6a288a32008-01-07 19:20:24 +00001065** Generate code to do constraint checks prior to an INSERT or an UPDATE.
drh9cfcf5d2002-01-29 18:41:24 +00001066**
drh04adf412008-01-08 18:57:50 +00001067** The input is a range of consecutive registers as follows:
drh0ca3e242002-01-29 23:07:02 +00001068**
dan65a7cd12009-09-01 12:16:01 +00001069** 1. The rowid of the row after the update.
drh0ca3e242002-01-29 23:07:02 +00001070**
dan65a7cd12009-09-01 12:16:01 +00001071** 2. The data in the first column of the entry after the update.
drh0ca3e242002-01-29 23:07:02 +00001072**
1073** i. Data from middle columns...
1074**
1075** N. The data in the last column of the entry after the update.
1076**
dan65a7cd12009-09-01 12:16:01 +00001077** The regRowid parameter is the index of the register containing (1).
drh04adf412008-01-08 18:57:50 +00001078**
dan65a7cd12009-09-01 12:16:01 +00001079** If isUpdate is true and rowidChng is non-zero, then rowidChng contains
1080** the address of a register containing the rowid before the update takes
1081** place. isUpdate is true for UPDATEs and false for INSERTs. If isUpdate
1082** is false, indicating an INSERT statement, then a non-zero rowidChng
1083** indicates that the rowid was explicitly specified as part of the
1084** INSERT statement. If rowidChng is false, it means that the rowid is
1085** computed automatically in an insert or that the rowid value is not
1086** modified by an update.
drh0ca3e242002-01-29 23:07:02 +00001087**
drhaa9b8962008-01-08 02:57:55 +00001088** The code generated by this routine store new index entries into
1089** registers identified by aRegIdx[]. No index entry is created for
1090** indices where aRegIdx[i]==0. The order of indices in aRegIdx[] is
1091** the same as the order of indices on the linked list of indices
1092** attached to the table.
drh9cfcf5d2002-01-29 18:41:24 +00001093**
1094** This routine also generates code to check constraints. NOT NULL,
1095** CHECK, and UNIQUE constraints are all checked. If a constraint fails,
drh1c928532002-01-31 15:54:21 +00001096** then the appropriate action is performed. There are five possible
1097** actions: ROLLBACK, ABORT, FAIL, REPLACE, and IGNORE.
drh9cfcf5d2002-01-29 18:41:24 +00001098**
1099** Constraint type Action What Happens
1100** --------------- ---------- ----------------------------------------
drh1c928532002-01-31 15:54:21 +00001101** any ROLLBACK The current transaction is rolled back and
danielk197724b03fd2004-05-10 10:34:34 +00001102** sqlite3_exec() returns immediately with a
drh9cfcf5d2002-01-29 18:41:24 +00001103** return code of SQLITE_CONSTRAINT.
1104**
drh1c928532002-01-31 15:54:21 +00001105** any ABORT Back out changes from the current command
1106** only (do not do a complete rollback) then
danielk197724b03fd2004-05-10 10:34:34 +00001107** cause sqlite3_exec() to return immediately
drh1c928532002-01-31 15:54:21 +00001108** with SQLITE_CONSTRAINT.
1109**
1110** any FAIL Sqlite_exec() returns immediately with a
1111** return code of SQLITE_CONSTRAINT. The
1112** transaction is not rolled back and any
1113** prior changes are retained.
1114**
drh9cfcf5d2002-01-29 18:41:24 +00001115** any IGNORE The record number and data is popped from
1116** the stack and there is an immediate jump
1117** to label ignoreDest.
1118**
1119** NOT NULL REPLACE The NULL value is replace by the default
1120** value for that column. If the default value
1121** is NULL, the action is the same as ABORT.
1122**
1123** UNIQUE REPLACE The other row that conflicts with the row
1124** being inserted is removed.
1125**
1126** CHECK REPLACE Illegal. The results in an exception.
1127**
drh1c928532002-01-31 15:54:21 +00001128** Which action to take is determined by the overrideError parameter.
1129** Or if overrideError==OE_Default, then the pParse->onError parameter
1130** is used. Or if pParse->onError==OE_Default then the onError value
1131** for the constraint is used.
drh9cfcf5d2002-01-29 18:41:24 +00001132**
drhaaab5722002-02-19 13:39:21 +00001133** The calling routine must open a read/write cursor for pTab with
drh04adf412008-01-08 18:57:50 +00001134** cursor number "baseCur". All indices of pTab must also have open
1135** read/write cursors with cursor number baseCur+i for the i-th cursor.
drh9cfcf5d2002-01-29 18:41:24 +00001136** Except, if there is no possibility of a REPLACE action then
drhaa9b8962008-01-08 02:57:55 +00001137** cursors do not need to be open for indices where aRegIdx[i]==0.
drh9cfcf5d2002-01-29 18:41:24 +00001138*/
danielk19774adee202004-05-08 08:23:19 +00001139void sqlite3GenerateConstraintChecks(
drh9cfcf5d2002-01-29 18:41:24 +00001140 Parse *pParse, /* The parser context */
1141 Table *pTab, /* the table into which we are inserting */
drh04adf412008-01-08 18:57:50 +00001142 int baseCur, /* Index of a read/write cursor pointing at pTab */
1143 int regRowid, /* Index of the range of input registers */
drhaa9b8962008-01-08 02:57:55 +00001144 int *aRegIdx, /* Register used by each index. 0 for unused indices */
drha05a7222008-01-19 03:35:58 +00001145 int rowidChng, /* True if the rowid might collide with existing entry */
drhb419a922002-01-30 16:17:23 +00001146 int isUpdate, /* True for UPDATE, False for INSERT */
drh9cfcf5d2002-01-29 18:41:24 +00001147 int overrideError, /* Override onError to this if not OE_Default */
danielk1977de630352009-05-04 11:42:29 +00001148 int ignoreDest, /* Jump to this label on an OE_Ignore resolution */
1149 int *pbMayReplace /* OUT: Set to true if constraint may cause a replace */
drh9cfcf5d2002-01-29 18:41:24 +00001150){
drh1b7ecbb2009-05-03 01:00:59 +00001151 int i; /* loop counter */
1152 Vdbe *v; /* VDBE under constrution */
1153 int nCol; /* Number of columns */
1154 int onError; /* Conflict resolution strategy */
drh1bd10f82008-12-10 21:19:56 +00001155 int j1; /* Addresss of jump instruction */
1156 int j2 = 0, j3; /* Addresses of jump instructions */
drh04adf412008-01-08 18:57:50 +00001157 int regData; /* Register containing first data column */
drh1b7ecbb2009-05-03 01:00:59 +00001158 int iCur; /* Table cursor number */
1159 Index *pIdx; /* Pointer to one of the indices */
1160 int seenReplace = 0; /* True if REPLACE is used to resolve INT PK conflict */
dan65a7cd12009-09-01 12:16:01 +00001161 int regOldRowid = (rowidChng && isUpdate) ? rowidChng : regRowid;
drh9cfcf5d2002-01-29 18:41:24 +00001162
danielk19774adee202004-05-08 08:23:19 +00001163 v = sqlite3GetVdbe(pParse);
drh9cfcf5d2002-01-29 18:41:24 +00001164 assert( v!=0 );
drh417be792002-03-03 18:59:40 +00001165 assert( pTab->pSelect==0 ); /* This table is not a VIEW */
drh9cfcf5d2002-01-29 18:41:24 +00001166 nCol = pTab->nCol;
drh04adf412008-01-08 18:57:50 +00001167 regData = regRowid + 1;
drh9cfcf5d2002-01-29 18:41:24 +00001168
1169 /* Test all NOT NULL constraints.
1170 */
1171 for(i=0; i<nCol; i++){
drh0ca3e242002-01-29 23:07:02 +00001172 if( i==pTab->iPKey ){
drh0ca3e242002-01-29 23:07:02 +00001173 continue;
1174 }
drh9cfcf5d2002-01-29 18:41:24 +00001175 onError = pTab->aCol[i].notNull;
drh0ca3e242002-01-29 23:07:02 +00001176 if( onError==OE_None ) continue;
drh9cfcf5d2002-01-29 18:41:24 +00001177 if( overrideError!=OE_Default ){
1178 onError = overrideError;
drha996e472003-05-16 02:30:27 +00001179 }else if( onError==OE_Default ){
1180 onError = OE_Abort;
drh9cfcf5d2002-01-29 18:41:24 +00001181 }
danielk19777977a172004-11-09 12:44:37 +00001182 if( onError==OE_Replace && pTab->aCol[i].pDflt==0 ){
drh9cfcf5d2002-01-29 18:41:24 +00001183 onError = OE_Abort;
1184 }
danielk1977b84f96f2005-01-20 11:32:23 +00001185 assert( onError==OE_Rollback || onError==OE_Abort || onError==OE_Fail
1186 || onError==OE_Ignore || onError==OE_Replace );
drh9cfcf5d2002-01-29 18:41:24 +00001187 switch( onError ){
drh1c928532002-01-31 15:54:21 +00001188 case OE_Abort:
dane0af83a2009-09-08 19:15:01 +00001189 sqlite3MayAbort(pParse);
1190 case OE_Rollback:
drh1c928532002-01-31 15:54:21 +00001191 case OE_Fail: {
drhf089aa42008-07-08 19:34:06 +00001192 char *zMsg;
drh5053a792009-02-20 03:02:23 +00001193 j1 = sqlite3VdbeAddOp3(v, OP_HaltIfNull,
1194 SQLITE_CONSTRAINT, onError, regData+i);
drhf089aa42008-07-08 19:34:06 +00001195 zMsg = sqlite3MPrintf(pParse->db, "%s.%s may not be NULL",
1196 pTab->zName, pTab->aCol[i].zName);
drh66a51672008-01-03 00:01:23 +00001197 sqlite3VdbeChangeP4(v, -1, zMsg, P4_DYNAMIC);
drh9cfcf5d2002-01-29 18:41:24 +00001198 break;
1199 }
1200 case OE_Ignore: {
drh5053a792009-02-20 03:02:23 +00001201 sqlite3VdbeAddOp2(v, OP_IsNull, regData+i, ignoreDest);
drh9cfcf5d2002-01-29 18:41:24 +00001202 break;
1203 }
drh098d1682009-05-02 15:46:46 +00001204 default: {
1205 assert( onError==OE_Replace );
drh5053a792009-02-20 03:02:23 +00001206 j1 = sqlite3VdbeAddOp1(v, OP_NotNull, regData+i);
drh04adf412008-01-08 18:57:50 +00001207 sqlite3ExprCode(pParse, pTab->aCol[i].pDflt, regData+i);
drh5053a792009-02-20 03:02:23 +00001208 sqlite3VdbeJumpHere(v, j1);
drh9cfcf5d2002-01-29 18:41:24 +00001209 break;
1210 }
drh9cfcf5d2002-01-29 18:41:24 +00001211 }
1212 }
1213
1214 /* Test all CHECK constraints
1215 */
drhffe07b22005-11-03 00:41:17 +00001216#ifndef SQLITE_OMIT_CHECK
drh0cd2d4c2005-11-03 02:15:02 +00001217 if( pTab->pCheck && (pParse->db->flags & SQLITE_IgnoreChecks)==0 ){
drhffe07b22005-11-03 00:41:17 +00001218 int allOk = sqlite3VdbeMakeLabel(v);
drhaa9b8962008-01-08 02:57:55 +00001219 pParse->ckBase = regData;
drh35573352008-01-08 23:54:25 +00001220 sqlite3ExprIfTrue(pParse, pTab->pCheck, allOk, SQLITE_JUMPIFNULL);
drhaa01c7e2006-03-15 16:26:10 +00001221 onError = overrideError!=OE_Default ? overrideError : OE_Abort;
drh2e06c672007-07-23 19:39:46 +00001222 if( onError==OE_Ignore ){
drh66a51672008-01-03 00:01:23 +00001223 sqlite3VdbeAddOp2(v, OP_Goto, 0, ignoreDest);
drhaa01c7e2006-03-15 16:26:10 +00001224 }else{
dane0af83a2009-09-08 19:15:01 +00001225 sqlite3HaltConstraint(pParse, onError, 0, 0);
drhaa01c7e2006-03-15 16:26:10 +00001226 }
drhffe07b22005-11-03 00:41:17 +00001227 sqlite3VdbeResolveLabel(v, allOk);
1228 }
1229#endif /* !defined(SQLITE_OMIT_CHECK) */
drh9cfcf5d2002-01-29 18:41:24 +00001230
drh0bd1f4e2002-06-06 18:54:39 +00001231 /* If we have an INTEGER PRIMARY KEY, make sure the primary key
1232 ** of the new record does not previously exist. Except, if this
1233 ** is an UPDATE and the primary key is not changing, that is OK.
drh9cfcf5d2002-01-29 18:41:24 +00001234 */
drhf0863fe2005-06-12 21:35:51 +00001235 if( rowidChng ){
drh0ca3e242002-01-29 23:07:02 +00001236 onError = pTab->keyConf;
1237 if( overrideError!=OE_Default ){
1238 onError = overrideError;
drha996e472003-05-16 02:30:27 +00001239 }else if( onError==OE_Default ){
1240 onError = OE_Abort;
drh0ca3e242002-01-29 23:07:02 +00001241 }
drha0217ba2003-06-01 01:10:33 +00001242
drh60a713c2008-01-21 16:22:45 +00001243 if( onError!=OE_Replace || pTab->pIndex ){
drha05a7222008-01-19 03:35:58 +00001244 if( isUpdate ){
dan76d462e2009-08-30 11:42:51 +00001245 j2 = sqlite3VdbeAddOp3(v, OP_Eq, regRowid, 0, rowidChng);
drh79b0c952002-05-21 12:56:43 +00001246 }
drha05a7222008-01-19 03:35:58 +00001247 j3 = sqlite3VdbeAddOp3(v, OP_NotExists, baseCur, 0, regRowid);
1248 switch( onError ){
1249 default: {
1250 onError = OE_Abort;
1251 /* Fall thru into the next case */
drh5383ae52003-06-04 12:23:30 +00001252 }
drha05a7222008-01-19 03:35:58 +00001253 case OE_Rollback:
1254 case OE_Abort:
1255 case OE_Fail: {
dane0af83a2009-09-08 19:15:01 +00001256 sqlite3HaltConstraint(
1257 pParse, onError, "PRIMARY KEY must be unique", P4_STATIC);
drha05a7222008-01-19 03:35:58 +00001258 break;
1259 }
1260 case OE_Replace: {
dan2283d462009-09-08 15:55:15 +00001261 /* If there are DELETE triggers on this table and the
1262 ** recursive-triggers flag is set, call GenerateRowDelete() to
1263 ** remove the conflicting row from the the table. This will fire
1264 ** the triggers and remove both the table and index b-tree entries.
1265 **
1266 ** Otherwise, if there are no triggers or the recursive-triggers
1267 ** flag is not set, call GenerateRowIndexDelete(). This removes
1268 ** the index b-tree entries only. The table b-tree entry will be
1269 ** replaced by the new entry when it is inserted. */
1270 Trigger *pTrigger = 0;
1271 if( pParse->db->flags&SQLITE_RecTriggers ){
1272 pTrigger = sqlite3TriggersExist(pParse, pTab, TK_DELETE, 0, 0);
1273 }
1274 if( pTrigger ){
1275 sqlite3GenerateRowDelete(
1276 pParse, pTab, baseCur, regRowid, 0, pTrigger, OE_Replace
1277 );
1278 }else{
1279 sqlite3GenerateRowIndexDelete(pParse, pTab, baseCur, 0);
1280 }
drhff738bc2009-09-24 00:09:58 +00001281 sqlite3MultiWrite(pParse);
drha05a7222008-01-19 03:35:58 +00001282 seenReplace = 1;
1283 break;
1284 }
1285 case OE_Ignore: {
1286 assert( seenReplace==0 );
1287 sqlite3VdbeAddOp2(v, OP_Goto, 0, ignoreDest);
1288 break;
1289 }
drh0ca3e242002-01-29 23:07:02 +00001290 }
drha05a7222008-01-19 03:35:58 +00001291 sqlite3VdbeJumpHere(v, j3);
1292 if( isUpdate ){
1293 sqlite3VdbeJumpHere(v, j2);
drh5383ae52003-06-04 12:23:30 +00001294 }
1295 }
drh0ca3e242002-01-29 23:07:02 +00001296 }
drh0bd1f4e2002-06-06 18:54:39 +00001297
1298 /* Test all UNIQUE constraints by creating entries for each UNIQUE
1299 ** index and making sure that duplicate entries do not already exist.
1300 ** Add the new records to the indices as we go.
1301 */
drhb2fe7d82003-04-20 17:29:23 +00001302 for(iCur=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, iCur++){
drh2d401ab2008-01-10 23:50:11 +00001303 int regIdx;
1304 int regR;
1305
drhaa9b8962008-01-08 02:57:55 +00001306 if( aRegIdx[iCur]==0 ) continue; /* Skip unused indices */
drhb2fe7d82003-04-20 17:29:23 +00001307
1308 /* Create a key for accessing the index entry */
drh2d401ab2008-01-10 23:50:11 +00001309 regIdx = sqlite3GetTempRange(pParse, pIdx->nColumn+1);
drh9cfcf5d2002-01-29 18:41:24 +00001310 for(i=0; i<pIdx->nColumn; i++){
1311 int idx = pIdx->aiColumn[i];
1312 if( idx==pTab->iPKey ){
drh2d401ab2008-01-10 23:50:11 +00001313 sqlite3VdbeAddOp2(v, OP_SCopy, regRowid, regIdx+i);
drh9cfcf5d2002-01-29 18:41:24 +00001314 }else{
drh2d401ab2008-01-10 23:50:11 +00001315 sqlite3VdbeAddOp2(v, OP_SCopy, regData+idx, regIdx+i);
drh9cfcf5d2002-01-29 18:41:24 +00001316 }
1317 }
drh2d401ab2008-01-10 23:50:11 +00001318 sqlite3VdbeAddOp2(v, OP_SCopy, regRowid, regIdx+i);
drh1db639c2008-01-17 02:36:28 +00001319 sqlite3VdbeAddOp3(v, OP_MakeRecord, regIdx, pIdx->nColumn+1, aRegIdx[iCur]);
dan69f8bb92009-08-13 19:21:16 +00001320 sqlite3VdbeChangeP4(v, -1, sqlite3IndexAffinityStr(v, pIdx), 0);
drhda250ea2008-04-01 05:07:14 +00001321 sqlite3ExprCacheAffinityChange(pParse, regIdx, pIdx->nColumn+1);
drhb2fe7d82003-04-20 17:29:23 +00001322
1323 /* Find out what action to take in case there is an indexing conflict */
drh9cfcf5d2002-01-29 18:41:24 +00001324 onError = pIdx->onError;
danielk1977de630352009-05-04 11:42:29 +00001325 if( onError==OE_None ){
1326 sqlite3ReleaseTempRange(pParse, regIdx, pIdx->nColumn+1);
1327 continue; /* pIdx is not a UNIQUE index */
1328 }
drh9cfcf5d2002-01-29 18:41:24 +00001329 if( overrideError!=OE_Default ){
1330 onError = overrideError;
drha996e472003-05-16 02:30:27 +00001331 }else if( onError==OE_Default ){
1332 onError = OE_Abort;
drh9cfcf5d2002-01-29 18:41:24 +00001333 }
drh5383ae52003-06-04 12:23:30 +00001334 if( seenReplace ){
1335 if( onError==OE_Ignore ) onError = OE_Replace;
1336 else if( onError==OE_Fail ) onError = OE_Abort;
1337 }
1338
drhb2fe7d82003-04-20 17:29:23 +00001339 /* Check to see if the new index entry will be unique */
drh2d401ab2008-01-10 23:50:11 +00001340 regR = sqlite3GetTempReg(pParse);
dan65a7cd12009-09-01 12:16:01 +00001341 sqlite3VdbeAddOp2(v, OP_SCopy, regOldRowid, regR);
drh2d401ab2008-01-10 23:50:11 +00001342 j3 = sqlite3VdbeAddOp4(v, OP_IsUnique, baseCur+iCur+1, 0,
danielk1977de630352009-05-04 11:42:29 +00001343 regR, SQLITE_INT_TO_PTR(regIdx),
mlcreecha9e852b2008-03-06 09:58:50 +00001344 P4_INT32);
danielk1977de630352009-05-04 11:42:29 +00001345 sqlite3ReleaseTempRange(pParse, regIdx, pIdx->nColumn+1);
drhb2fe7d82003-04-20 17:29:23 +00001346
1347 /* Generate code that executes if the new index entry is not unique */
danielk1977b84f96f2005-01-20 11:32:23 +00001348 assert( onError==OE_Rollback || onError==OE_Abort || onError==OE_Fail
1349 || onError==OE_Ignore || onError==OE_Replace );
drh9cfcf5d2002-01-29 18:41:24 +00001350 switch( onError ){
drh1c928532002-01-31 15:54:21 +00001351 case OE_Rollback:
1352 case OE_Abort:
1353 case OE_Fail: {
drh098d1682009-05-02 15:46:46 +00001354 int j;
1355 StrAccum errMsg;
1356 const char *zSep;
1357 char *zErr;
1358
1359 sqlite3StrAccumInit(&errMsg, 0, 0, 200);
1360 errMsg.db = pParse->db;
1361 zSep = pIdx->nColumn>1 ? "columns " : "column ";
1362 for(j=0; j<pIdx->nColumn; j++){
drh37ed48e2003-08-05 13:13:38 +00001363 char *zCol = pTab->aCol[pIdx->aiColumn[j]].zName;
drh098d1682009-05-02 15:46:46 +00001364 sqlite3StrAccumAppend(&errMsg, zSep, -1);
1365 zSep = ", ";
1366 sqlite3StrAccumAppend(&errMsg, zCol, -1);
drh37ed48e2003-08-05 13:13:38 +00001367 }
drh098d1682009-05-02 15:46:46 +00001368 sqlite3StrAccumAppend(&errMsg,
1369 pIdx->nColumn>1 ? " are not unique" : " is not unique", -1);
1370 zErr = sqlite3StrAccumFinish(&errMsg);
dane0af83a2009-09-08 19:15:01 +00001371 sqlite3HaltConstraint(pParse, onError, zErr, 0);
drh098d1682009-05-02 15:46:46 +00001372 sqlite3DbFree(errMsg.db, zErr);
drh9cfcf5d2002-01-29 18:41:24 +00001373 break;
1374 }
1375 case OE_Ignore: {
drh0ca3e242002-01-29 23:07:02 +00001376 assert( seenReplace==0 );
drh66a51672008-01-03 00:01:23 +00001377 sqlite3VdbeAddOp2(v, OP_Goto, 0, ignoreDest);
drh9cfcf5d2002-01-29 18:41:24 +00001378 break;
1379 }
drh098d1682009-05-02 15:46:46 +00001380 default: {
dan2283d462009-09-08 15:55:15 +00001381 Trigger *pTrigger = 0;
drh098d1682009-05-02 15:46:46 +00001382 assert( onError==OE_Replace );
dan2283d462009-09-08 15:55:15 +00001383 if( pParse->db->flags&SQLITE_RecTriggers ){
1384 pTrigger = sqlite3TriggersExist(pParse, pTab, TK_DELETE, 0, 0);
1385 }
1386 sqlite3GenerateRowDelete(
1387 pParse, pTab, baseCur, regR, 0, pTrigger, OE_Replace
1388 );
drhff738bc2009-09-24 00:09:58 +00001389 sqlite3MultiWrite(pParse);
drh0ca3e242002-01-29 23:07:02 +00001390 seenReplace = 1;
drh9cfcf5d2002-01-29 18:41:24 +00001391 break;
1392 }
drh9cfcf5d2002-01-29 18:41:24 +00001393 }
drh2d401ab2008-01-10 23:50:11 +00001394 sqlite3VdbeJumpHere(v, j3);
1395 sqlite3ReleaseTempReg(pParse, regR);
drh9cfcf5d2002-01-29 18:41:24 +00001396 }
dan1da40a32009-09-19 17:00:31 +00001397
danielk1977de630352009-05-04 11:42:29 +00001398 if( pbMayReplace ){
1399 *pbMayReplace = seenReplace;
1400 }
drh9cfcf5d2002-01-29 18:41:24 +00001401}
drh0ca3e242002-01-29 23:07:02 +00001402
1403/*
1404** This routine generates code to finish the INSERT or UPDATE operation
danielk19774adee202004-05-08 08:23:19 +00001405** that was started by a prior call to sqlite3GenerateConstraintChecks.
drh04adf412008-01-08 18:57:50 +00001406** A consecutive range of registers starting at regRowid contains the
1407** rowid and the content to be inserted.
drh0ca3e242002-01-29 23:07:02 +00001408**
drhb419a922002-01-30 16:17:23 +00001409** The arguments to this routine should be the same as the first six
danielk19774adee202004-05-08 08:23:19 +00001410** arguments to sqlite3GenerateConstraintChecks.
drh0ca3e242002-01-29 23:07:02 +00001411*/
danielk19774adee202004-05-08 08:23:19 +00001412void sqlite3CompleteInsertion(
drh0ca3e242002-01-29 23:07:02 +00001413 Parse *pParse, /* The parser context */
1414 Table *pTab, /* the table into which we are inserting */
drh04adf412008-01-08 18:57:50 +00001415 int baseCur, /* Index of a read/write cursor pointing at pTab */
1416 int regRowid, /* Range of content */
drhaa9b8962008-01-08 02:57:55 +00001417 int *aRegIdx, /* Register used by each index. 0 for unused indices */
drh70ce3f02003-04-15 19:22:22 +00001418 int isUpdate, /* True for UPDATE, False for INSERT */
danielk1977de630352009-05-04 11:42:29 +00001419 int appendBias, /* True if this is likely to be an append */
1420 int useSeekResult /* True to set the USESEEKRESULT flag on OP_[Idx]Insert */
drh0ca3e242002-01-29 23:07:02 +00001421){
1422 int i;
1423 Vdbe *v;
1424 int nIdx;
1425 Index *pIdx;
drh1bd10f82008-12-10 21:19:56 +00001426 u8 pik_flags;
drh04adf412008-01-08 18:57:50 +00001427 int regData;
drhb7654112008-01-12 12:48:07 +00001428 int regRec;
drh0ca3e242002-01-29 23:07:02 +00001429
danielk19774adee202004-05-08 08:23:19 +00001430 v = sqlite3GetVdbe(pParse);
drh0ca3e242002-01-29 23:07:02 +00001431 assert( v!=0 );
drh417be792002-03-03 18:59:40 +00001432 assert( pTab->pSelect==0 ); /* This table is not a VIEW */
drh0ca3e242002-01-29 23:07:02 +00001433 for(nIdx=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, nIdx++){}
1434 for(i=nIdx-1; i>=0; i--){
drhaa9b8962008-01-08 02:57:55 +00001435 if( aRegIdx[i]==0 ) continue;
drh04adf412008-01-08 18:57:50 +00001436 sqlite3VdbeAddOp2(v, OP_IdxInsert, baseCur+i+1, aRegIdx[i]);
danielk1977de630352009-05-04 11:42:29 +00001437 if( useSeekResult ){
1438 sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT);
1439 }
drh0ca3e242002-01-29 23:07:02 +00001440 }
drh04adf412008-01-08 18:57:50 +00001441 regData = regRowid + 1;
drhb7654112008-01-12 12:48:07 +00001442 regRec = sqlite3GetTempReg(pParse);
drh1db639c2008-01-17 02:36:28 +00001443 sqlite3VdbeAddOp3(v, OP_MakeRecord, regData, pTab->nCol, regRec);
danielk1977a37cdde2004-05-16 11:15:36 +00001444 sqlite3TableAffinityStr(v, pTab);
drhda250ea2008-04-01 05:07:14 +00001445 sqlite3ExprCacheAffinityChange(pParse, regData, pTab->nCol);
drh4794f732004-11-05 17:17:50 +00001446 if( pParse->nested ){
1447 pik_flags = 0;
1448 }else{
danielk197794eb6a12005-12-15 15:22:08 +00001449 pik_flags = OPFLAG_NCHANGE;
1450 pik_flags |= (isUpdate?OPFLAG_ISUPDATE:OPFLAG_LASTROWID);
drh4794f732004-11-05 17:17:50 +00001451 }
drhe4d90812007-03-29 05:51:49 +00001452 if( appendBias ){
1453 pik_flags |= OPFLAG_APPEND;
1454 }
danielk1977de630352009-05-04 11:42:29 +00001455 if( useSeekResult ){
1456 pik_flags |= OPFLAG_USESEEKRESULT;
1457 }
drhb7654112008-01-12 12:48:07 +00001458 sqlite3VdbeAddOp3(v, OP_Insert, baseCur, regRec, regRowid);
danielk197794eb6a12005-12-15 15:22:08 +00001459 if( !pParse->nested ){
drh66a51672008-01-03 00:01:23 +00001460 sqlite3VdbeChangeP4(v, -1, pTab->zName, P4_STATIC);
danielk197794eb6a12005-12-15 15:22:08 +00001461 }
drhb7654112008-01-12 12:48:07 +00001462 sqlite3VdbeChangeP5(v, pik_flags);
drh0ca3e242002-01-29 23:07:02 +00001463}
drhcd446902004-02-24 01:05:31 +00001464
1465/*
drh290c1942004-08-21 17:54:45 +00001466** Generate code that will open cursors for a table and for all
drh04adf412008-01-08 18:57:50 +00001467** indices of that table. The "baseCur" parameter is the cursor number used
drhcd446902004-02-24 01:05:31 +00001468** for the table. Indices are opened on subsequent cursors.
drhaa9b8962008-01-08 02:57:55 +00001469**
1470** Return the number of indices on the table.
drhcd446902004-02-24 01:05:31 +00001471*/
drhaa9b8962008-01-08 02:57:55 +00001472int sqlite3OpenTableAndIndices(
drh290c1942004-08-21 17:54:45 +00001473 Parse *pParse, /* Parsing context */
1474 Table *pTab, /* Table to be opened */
drhdfe88ec2008-11-03 20:55:06 +00001475 int baseCur, /* Cursor number assigned to the table */
drh290c1942004-08-21 17:54:45 +00001476 int op /* OP_OpenRead or OP_OpenWrite */
1477){
drhcd446902004-02-24 01:05:31 +00001478 int i;
drh4cbdda92006-06-14 19:00:20 +00001479 int iDb;
drhcd446902004-02-24 01:05:31 +00001480 Index *pIdx;
drh4cbdda92006-06-14 19:00:20 +00001481 Vdbe *v;
1482
drhaa9b8962008-01-08 02:57:55 +00001483 if( IsVirtual(pTab) ) return 0;
drh4cbdda92006-06-14 19:00:20 +00001484 iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
1485 v = sqlite3GetVdbe(pParse);
drhcd446902004-02-24 01:05:31 +00001486 assert( v!=0 );
drh04adf412008-01-08 18:57:50 +00001487 sqlite3OpenTable(pParse, baseCur, iDb, pTab, op);
drhcd446902004-02-24 01:05:31 +00001488 for(i=1, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){
danielk1977b3bf5562006-01-10 17:58:23 +00001489 KeyInfo *pKey = sqlite3IndexKeyinfo(pParse, pIdx);
danielk1977da184232006-01-05 11:34:32 +00001490 assert( pIdx->pSchema==pTab->pSchema );
drh04adf412008-01-08 18:57:50 +00001491 sqlite3VdbeAddOp4(v, op, i+baseCur, pIdx->tnum, iDb,
drh66a51672008-01-03 00:01:23 +00001492 (char*)pKey, P4_KEYINFO_HANDOFF);
danielk1977207872a2008-01-03 07:54:23 +00001493 VdbeComment((v, "%s", pIdx->zName));
drhcd446902004-02-24 01:05:31 +00001494 }
drh1b7ecbb2009-05-03 01:00:59 +00001495 if( pParse->nTab<baseCur+i ){
drh04adf412008-01-08 18:57:50 +00001496 pParse->nTab = baseCur+i;
drh290c1942004-08-21 17:54:45 +00001497 }
drhaa9b8962008-01-08 02:57:55 +00001498 return i-1;
drhcd446902004-02-24 01:05:31 +00001499}
drh9d9cf222007-02-13 15:01:11 +00001500
drh91c58e22007-03-27 12:04:04 +00001501
1502#ifdef SQLITE_TEST
1503/*
1504** The following global variable is incremented whenever the
1505** transfer optimization is used. This is used for testing
1506** purposes only - to make sure the transfer optimization really
1507** is happening when it is suppose to.
1508*/
1509int sqlite3_xferopt_count;
1510#endif /* SQLITE_TEST */
1511
1512
drh9d9cf222007-02-13 15:01:11 +00001513#ifndef SQLITE_OMIT_XFER_OPT
1514/*
1515** Check to collation names to see if they are compatible.
1516*/
1517static int xferCompatibleCollation(const char *z1, const char *z2){
1518 if( z1==0 ){
1519 return z2==0;
1520 }
1521 if( z2==0 ){
1522 return 0;
1523 }
1524 return sqlite3StrICmp(z1, z2)==0;
1525}
1526
1527
1528/*
1529** Check to see if index pSrc is compatible as a source of data
1530** for index pDest in an insert transfer optimization. The rules
1531** for a compatible index:
1532**
1533** * The index is over the same set of columns
1534** * The same DESC and ASC markings occurs on all columns
1535** * The same onError processing (OE_Abort, OE_Ignore, etc)
1536** * The same collating sequence on each column
1537*/
1538static int xferCompatibleIndex(Index *pDest, Index *pSrc){
1539 int i;
1540 assert( pDest && pSrc );
1541 assert( pDest->pTable!=pSrc->pTable );
1542 if( pDest->nColumn!=pSrc->nColumn ){
1543 return 0; /* Different number of columns */
1544 }
1545 if( pDest->onError!=pSrc->onError ){
1546 return 0; /* Different conflict resolution strategies */
1547 }
1548 for(i=0; i<pSrc->nColumn; i++){
1549 if( pSrc->aiColumn[i]!=pDest->aiColumn[i] ){
1550 return 0; /* Different columns indexed */
1551 }
1552 if( pSrc->aSortOrder[i]!=pDest->aSortOrder[i] ){
1553 return 0; /* Different sort orders */
1554 }
drh3f6e7812009-05-02 00:28:19 +00001555 if( !xferCompatibleCollation(pSrc->azColl[i],pDest->azColl[i]) ){
drh60a713c2008-01-21 16:22:45 +00001556 return 0; /* Different collating sequences */
drh9d9cf222007-02-13 15:01:11 +00001557 }
1558 }
1559
1560 /* If no test above fails then the indices must be compatible */
1561 return 1;
1562}
1563
1564/*
1565** Attempt the transfer optimization on INSERTs of the form
1566**
1567** INSERT INTO tab1 SELECT * FROM tab2;
1568**
1569** This optimization is only attempted if
1570**
1571** (1) tab1 and tab2 have identical schemas including all the
drh8103b7d2007-02-24 13:23:51 +00001572** same indices and constraints
drh9d9cf222007-02-13 15:01:11 +00001573**
1574** (2) tab1 and tab2 are different tables
1575**
1576** (3) There must be no triggers on tab1
1577**
1578** (4) The result set of the SELECT statement is "*"
1579**
1580** (5) The SELECT statement has no WHERE, HAVING, ORDER BY, GROUP BY,
1581** or LIMIT clause.
1582**
1583** (6) The SELECT statement is a simple (not a compound) select that
1584** contains only tab2 in its FROM clause
1585**
1586** This method for implementing the INSERT transfers raw records from
1587** tab2 over to tab1. The columns are not decoded. Raw records from
1588** the indices of tab2 are transfered to tab1 as well. In so doing,
1589** the resulting tab1 has much less fragmentation.
1590**
1591** This routine returns TRUE if the optimization is attempted. If any
1592** of the conditions above fail so that the optimization should not
1593** be attempted, then this routine returns FALSE.
1594*/
1595static int xferOptimization(
1596 Parse *pParse, /* Parser context */
1597 Table *pDest, /* The table we are inserting into */
1598 Select *pSelect, /* A SELECT statement to use as the data source */
1599 int onError, /* How to handle constraint errors */
1600 int iDbDest /* The database of pDest */
1601){
1602 ExprList *pEList; /* The result set of the SELECT */
1603 Table *pSrc; /* The table in the FROM clause of SELECT */
1604 Index *pSrcIdx, *pDestIdx; /* Source and destination indices */
1605 struct SrcList_item *pItem; /* An element of pSelect->pSrc */
1606 int i; /* Loop counter */
1607 int iDbSrc; /* The database of pSrc */
1608 int iSrc, iDest; /* Cursors from source and destination */
1609 int addr1, addr2; /* Loop addresses */
1610 int emptyDestTest; /* Address of test for empty pDest */
1611 int emptySrcTest; /* Address of test for empty pSrc */
drh9d9cf222007-02-13 15:01:11 +00001612 Vdbe *v; /* The VDBE we are building */
1613 KeyInfo *pKey; /* Key information for an index */
drh6a288a32008-01-07 19:20:24 +00001614 int regAutoinc; /* Memory register used by AUTOINC */
drhf33c9fa2007-04-10 18:17:55 +00001615 int destHasUniqueIdx = 0; /* True if pDest has a UNIQUE index */
drhb7654112008-01-12 12:48:07 +00001616 int regData, regRowid; /* Registers holding data and rowid */
drh9d9cf222007-02-13 15:01:11 +00001617
1618 if( pSelect==0 ){
1619 return 0; /* Must be of the form INSERT INTO ... SELECT ... */
1620 }
danielk19772f886d12009-02-28 10:47:41 +00001621 if( sqlite3TriggerList(pParse, pDest) ){
drh9d9cf222007-02-13 15:01:11 +00001622 return 0; /* tab1 must not have triggers */
1623 }
1624#ifndef SQLITE_OMIT_VIRTUALTABLE
drh7d10d5a2008-08-20 16:35:10 +00001625 if( pDest->tabFlags & TF_Virtual ){
drh9d9cf222007-02-13 15:01:11 +00001626 return 0; /* tab1 must not be a virtual table */
1627 }
1628#endif
1629 if( onError==OE_Default ){
1630 onError = OE_Abort;
1631 }
1632 if( onError!=OE_Abort && onError!=OE_Rollback ){
1633 return 0; /* Cannot do OR REPLACE or OR IGNORE or OR FAIL */
1634 }
danielk19775ce240a2007-09-03 17:30:06 +00001635 assert(pSelect->pSrc); /* allocated even if there is no FROM clause */
drh9d9cf222007-02-13 15:01:11 +00001636 if( pSelect->pSrc->nSrc!=1 ){
1637 return 0; /* FROM clause must have exactly one term */
1638 }
1639 if( pSelect->pSrc->a[0].pSelect ){
1640 return 0; /* FROM clause cannot contain a subquery */
1641 }
1642 if( pSelect->pWhere ){
1643 return 0; /* SELECT may not have a WHERE clause */
1644 }
1645 if( pSelect->pOrderBy ){
1646 return 0; /* SELECT may not have an ORDER BY clause */
1647 }
drh8103b7d2007-02-24 13:23:51 +00001648 /* Do not need to test for a HAVING clause. If HAVING is present but
1649 ** there is no ORDER BY, we will get an error. */
drh9d9cf222007-02-13 15:01:11 +00001650 if( pSelect->pGroupBy ){
1651 return 0; /* SELECT may not have a GROUP BY clause */
1652 }
1653 if( pSelect->pLimit ){
1654 return 0; /* SELECT may not have a LIMIT clause */
1655 }
drh8103b7d2007-02-24 13:23:51 +00001656 assert( pSelect->pOffset==0 ); /* Must be so if pLimit==0 */
drh9d9cf222007-02-13 15:01:11 +00001657 if( pSelect->pPrior ){
1658 return 0; /* SELECT may not be a compound query */
1659 }
drh7d10d5a2008-08-20 16:35:10 +00001660 if( pSelect->selFlags & SF_Distinct ){
drh9d9cf222007-02-13 15:01:11 +00001661 return 0; /* SELECT may not be DISTINCT */
1662 }
1663 pEList = pSelect->pEList;
1664 assert( pEList!=0 );
1665 if( pEList->nExpr!=1 ){
1666 return 0; /* The result set must have exactly one column */
1667 }
1668 assert( pEList->a[0].pExpr );
1669 if( pEList->a[0].pExpr->op!=TK_ALL ){
1670 return 0; /* The result set must be the special operator "*" */
1671 }
1672
1673 /* At this point we have established that the statement is of the
1674 ** correct syntactic form to participate in this optimization. Now
1675 ** we have to check the semantics.
1676 */
1677 pItem = pSelect->pSrc->a;
drhca424112008-01-25 15:04:48 +00001678 pSrc = sqlite3LocateTable(pParse, 0, pItem->zName, pItem->zDatabase);
drh9d9cf222007-02-13 15:01:11 +00001679 if( pSrc==0 ){
1680 return 0; /* FROM clause does not contain a real table */
1681 }
1682 if( pSrc==pDest ){
1683 return 0; /* tab1 and tab2 may not be the same table */
1684 }
1685#ifndef SQLITE_OMIT_VIRTUALTABLE
drh7d10d5a2008-08-20 16:35:10 +00001686 if( pSrc->tabFlags & TF_Virtual ){
drh9d9cf222007-02-13 15:01:11 +00001687 return 0; /* tab2 must not be a virtual table */
1688 }
1689#endif
1690 if( pSrc->pSelect ){
1691 return 0; /* tab2 may not be a view */
1692 }
1693 if( pDest->nCol!=pSrc->nCol ){
1694 return 0; /* Number of columns must be the same in tab1 and tab2 */
1695 }
1696 if( pDest->iPKey!=pSrc->iPKey ){
1697 return 0; /* Both tables must have the same INTEGER PRIMARY KEY */
1698 }
1699 for(i=0; i<pDest->nCol; i++){
1700 if( pDest->aCol[i].affinity!=pSrc->aCol[i].affinity ){
1701 return 0; /* Affinity must be the same on all columns */
1702 }
1703 if( !xferCompatibleCollation(pDest->aCol[i].zColl, pSrc->aCol[i].zColl) ){
1704 return 0; /* Collating sequence must be the same on all columns */
1705 }
1706 if( pDest->aCol[i].notNull && !pSrc->aCol[i].notNull ){
1707 return 0; /* tab2 must be NOT NULL if tab1 is */
1708 }
1709 }
1710 for(pDestIdx=pDest->pIndex; pDestIdx; pDestIdx=pDestIdx->pNext){
drhf33c9fa2007-04-10 18:17:55 +00001711 if( pDestIdx->onError!=OE_None ){
1712 destHasUniqueIdx = 1;
1713 }
drh9d9cf222007-02-13 15:01:11 +00001714 for(pSrcIdx=pSrc->pIndex; pSrcIdx; pSrcIdx=pSrcIdx->pNext){
1715 if( xferCompatibleIndex(pDestIdx, pSrcIdx) ) break;
1716 }
1717 if( pSrcIdx==0 ){
1718 return 0; /* pDestIdx has no corresponding index in pSrc */
1719 }
1720 }
drh7fc2f412007-03-29 00:08:24 +00001721#ifndef SQLITE_OMIT_CHECK
drhfb658de2007-02-24 15:18:49 +00001722 if( pDest->pCheck && !sqlite3ExprCompare(pSrc->pCheck, pDest->pCheck) ){
drh8103b7d2007-02-24 13:23:51 +00001723 return 0; /* Tables have different CHECK constraints. Ticket #2252 */
1724 }
drh7fc2f412007-03-29 00:08:24 +00001725#endif
drh9d9cf222007-02-13 15:01:11 +00001726
1727 /* If we get this far, it means either:
1728 **
1729 ** * We can always do the transfer if the table contains an
1730 ** an integer primary key
1731 **
1732 ** * We can conditionally do the transfer if the destination
1733 ** table is empty.
1734 */
drhdd735212007-02-24 13:53:05 +00001735#ifdef SQLITE_TEST
1736 sqlite3_xferopt_count++;
1737#endif
drh9d9cf222007-02-13 15:01:11 +00001738 iDbSrc = sqlite3SchemaToIndex(pParse->db, pSrc->pSchema);
1739 v = sqlite3GetVdbe(pParse);
drhf53e9b52007-08-29 13:45:58 +00001740 sqlite3CodeVerifySchema(pParse, iDbSrc);
drh9d9cf222007-02-13 15:01:11 +00001741 iSrc = pParse->nTab++;
1742 iDest = pParse->nTab++;
drh6a288a32008-01-07 19:20:24 +00001743 regAutoinc = autoIncBegin(pParse, iDbDest, pDest);
drh9d9cf222007-02-13 15:01:11 +00001744 sqlite3OpenTable(pParse, iDest, iDbDest, pDest, OP_OpenWrite);
drhf33c9fa2007-04-10 18:17:55 +00001745 if( (pDest->iPKey<0 && pDest->pIndex!=0) || destHasUniqueIdx ){
drhbd36ba62007-03-31 13:00:26 +00001746 /* If tables do not have an INTEGER PRIMARY KEY and there
1747 ** are indices to be copied and the destination is not empty,
1748 ** we have to disallow the transfer optimization because the
1749 ** the rowids might change which will mess up indexing.
drhf33c9fa2007-04-10 18:17:55 +00001750 **
1751 ** Or if the destination has a UNIQUE index and is not empty,
1752 ** we also disallow the transfer optimization because we cannot
1753 ** insure that all entries in the union of DEST and SRC will be
1754 ** unique.
drh9d9cf222007-02-13 15:01:11 +00001755 */
drh66a51672008-01-03 00:01:23 +00001756 addr1 = sqlite3VdbeAddOp2(v, OP_Rewind, iDest, 0);
1757 emptyDestTest = sqlite3VdbeAddOp2(v, OP_Goto, 0, 0);
drh9d9cf222007-02-13 15:01:11 +00001758 sqlite3VdbeJumpHere(v, addr1);
1759 }else{
1760 emptyDestTest = 0;
1761 }
1762 sqlite3OpenTable(pParse, iSrc, iDbSrc, pSrc, OP_OpenRead);
drh66a51672008-01-03 00:01:23 +00001763 emptySrcTest = sqlite3VdbeAddOp2(v, OP_Rewind, iSrc, 0);
drhb7654112008-01-12 12:48:07 +00001764 regData = sqlite3GetTempReg(pParse);
1765 regRowid = sqlite3GetTempReg(pParse);
drh42242de2007-03-29 13:35:35 +00001766 if( pDest->iPKey>=0 ){
drhb7654112008-01-12 12:48:07 +00001767 addr1 = sqlite3VdbeAddOp2(v, OP_Rowid, iSrc, regRowid);
1768 addr2 = sqlite3VdbeAddOp3(v, OP_NotExists, iDest, 0, regRowid);
dane0af83a2009-09-08 19:15:01 +00001769 sqlite3HaltConstraint(
1770 pParse, onError, "PRIMARY KEY must be unique", P4_STATIC);
drh95bad4c2007-03-28 18:04:10 +00001771 sqlite3VdbeJumpHere(v, addr2);
drhb7654112008-01-12 12:48:07 +00001772 autoIncStep(pParse, regAutoinc, regRowid);
drhbd36ba62007-03-31 13:00:26 +00001773 }else if( pDest->pIndex==0 ){
drhb7654112008-01-12 12:48:07 +00001774 addr1 = sqlite3VdbeAddOp2(v, OP_NewRowid, iDest, regRowid);
drh95bad4c2007-03-28 18:04:10 +00001775 }else{
drhb7654112008-01-12 12:48:07 +00001776 addr1 = sqlite3VdbeAddOp2(v, OP_Rowid, iSrc, regRowid);
drh7d10d5a2008-08-20 16:35:10 +00001777 assert( (pDest->tabFlags & TF_Autoincrement)==0 );
drh95bad4c2007-03-28 18:04:10 +00001778 }
drhb7654112008-01-12 12:48:07 +00001779 sqlite3VdbeAddOp2(v, OP_RowData, iSrc, regData);
1780 sqlite3VdbeAddOp3(v, OP_Insert, iDest, regData, regRowid);
1781 sqlite3VdbeChangeP5(v, OPFLAG_NCHANGE|OPFLAG_LASTROWID|OPFLAG_APPEND);
danielk19771f4aa332008-01-03 09:51:55 +00001782 sqlite3VdbeChangeP4(v, -1, pDest->zName, 0);
drh66a51672008-01-03 00:01:23 +00001783 sqlite3VdbeAddOp2(v, OP_Next, iSrc, addr1);
drh9d9cf222007-02-13 15:01:11 +00001784 for(pDestIdx=pDest->pIndex; pDestIdx; pDestIdx=pDestIdx->pNext){
drh1b7ecbb2009-05-03 01:00:59 +00001785 for(pSrcIdx=pSrc->pIndex; ALWAYS(pSrcIdx); pSrcIdx=pSrcIdx->pNext){
drh9d9cf222007-02-13 15:01:11 +00001786 if( xferCompatibleIndex(pDestIdx, pSrcIdx) ) break;
1787 }
1788 assert( pSrcIdx );
drh66a51672008-01-03 00:01:23 +00001789 sqlite3VdbeAddOp2(v, OP_Close, iSrc, 0);
1790 sqlite3VdbeAddOp2(v, OP_Close, iDest, 0);
drh9d9cf222007-02-13 15:01:11 +00001791 pKey = sqlite3IndexKeyinfo(pParse, pSrcIdx);
danielk1977207872a2008-01-03 07:54:23 +00001792 sqlite3VdbeAddOp4(v, OP_OpenRead, iSrc, pSrcIdx->tnum, iDbSrc,
1793 (char*)pKey, P4_KEYINFO_HANDOFF);
drhd4e70eb2008-01-02 00:34:36 +00001794 VdbeComment((v, "%s", pSrcIdx->zName));
drh9d9cf222007-02-13 15:01:11 +00001795 pKey = sqlite3IndexKeyinfo(pParse, pDestIdx);
danielk1977207872a2008-01-03 07:54:23 +00001796 sqlite3VdbeAddOp4(v, OP_OpenWrite, iDest, pDestIdx->tnum, iDbDest,
drh66a51672008-01-03 00:01:23 +00001797 (char*)pKey, P4_KEYINFO_HANDOFF);
danielk1977207872a2008-01-03 07:54:23 +00001798 VdbeComment((v, "%s", pDestIdx->zName));
drh66a51672008-01-03 00:01:23 +00001799 addr1 = sqlite3VdbeAddOp2(v, OP_Rewind, iSrc, 0);
drhb7654112008-01-12 12:48:07 +00001800 sqlite3VdbeAddOp2(v, OP_RowKey, iSrc, regData);
1801 sqlite3VdbeAddOp3(v, OP_IdxInsert, iDest, regData, 1);
drh66a51672008-01-03 00:01:23 +00001802 sqlite3VdbeAddOp2(v, OP_Next, iSrc, addr1+1);
drh9d9cf222007-02-13 15:01:11 +00001803 sqlite3VdbeJumpHere(v, addr1);
1804 }
1805 sqlite3VdbeJumpHere(v, emptySrcTest);
drhb7654112008-01-12 12:48:07 +00001806 sqlite3ReleaseTempReg(pParse, regRowid);
1807 sqlite3ReleaseTempReg(pParse, regData);
drh66a51672008-01-03 00:01:23 +00001808 sqlite3VdbeAddOp2(v, OP_Close, iSrc, 0);
1809 sqlite3VdbeAddOp2(v, OP_Close, iDest, 0);
drh9d9cf222007-02-13 15:01:11 +00001810 if( emptyDestTest ){
drh66a51672008-01-03 00:01:23 +00001811 sqlite3VdbeAddOp2(v, OP_Halt, SQLITE_OK, 0);
drh9d9cf222007-02-13 15:01:11 +00001812 sqlite3VdbeJumpHere(v, emptyDestTest);
drh66a51672008-01-03 00:01:23 +00001813 sqlite3VdbeAddOp2(v, OP_Close, iDest, 0);
drh9d9cf222007-02-13 15:01:11 +00001814 return 0;
1815 }else{
1816 return 1;
1817 }
1818}
1819#endif /* SQLITE_OMIT_XFER_OPT */