blob: f6d8b1c3905eab2225d81c5d804a0354599b750f [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**
drh0b9f50d2009-06-23 20:28:53 +000015** $Id: insert.c,v 1.269 2009/06/23 20:28:54 drh 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/*
drh66a51672008-01-03 00:01:23 +000040** Set P4 of the most recently inserted opcode to a column affinity
danielk1977a37cdde2004-05-16 11:15:36 +000041** string for index pIdx. A column affinity string has one character
danielk19773d1bfea2004-05-14 11:00:53 +000042** for each column in the table, according to the affinity of the column:
43**
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.
danielk19773d1bfea2004-05-14 11:00:53 +000054*/
danielk1977a37cdde2004-05-16 11:15:36 +000055void sqlite3IndexAffinityStr(Vdbe *v, Index *pIdx){
56 if( !pIdx->zColAff ){
danielk1977e014a832004-05-17 10:48:57 +000057 /* The first time a column affinity string for a particular index is
danielk1977a37cdde2004-05-16 11:15:36 +000058 ** required, it is allocated and populated here. It is then stored as
danielk1977e014a832004-05-17 10:48:57 +000059 ** a member of the Index structure for subsequent use.
danielk1977a37cdde2004-05-16 11:15:36 +000060 **
61 ** The column affinity string will eventually be deleted by
danielk1977e014a832004-05-17 10:48:57 +000062 ** sqliteDeleteIndex() when the Index structure itself is cleaned
danielk1977a37cdde2004-05-16 11:15:36 +000063 ** up.
64 */
65 int n;
66 Table *pTab = pIdx->pTable;
drhabb6fca2007-08-16 12:24:01 +000067 sqlite3 *db = sqlite3VdbeDb(v);
drh633e6d52008-07-28 19:34:53 +000068 pIdx->zColAff = (char *)sqlite3Malloc(pIdx->nColumn+2);
danielk1977a37cdde2004-05-16 11:15:36 +000069 if( !pIdx->zColAff ){
drh633e6d52008-07-28 19:34:53 +000070 db->mallocFailed = 1;
danielk1977a37cdde2004-05-16 11:15:36 +000071 return;
72 }
73 for(n=0; n<pIdx->nColumn; n++){
74 pIdx->zColAff[n] = pTab->aCol[pIdx->aiColumn[n]].affinity;
75 }
drh2d401ab2008-01-10 23:50:11 +000076 pIdx->zColAff[n++] = SQLITE_AFF_NONE;
77 pIdx->zColAff[n] = 0;
danielk1977a37cdde2004-05-16 11:15:36 +000078 }
danielk19773d1bfea2004-05-14 11:00:53 +000079
drh66a51672008-01-03 00:01:23 +000080 sqlite3VdbeChangeP4(v, -1, pIdx->zColAff, 0);
danielk1977a37cdde2004-05-16 11:15:36 +000081}
82
83/*
drh66a51672008-01-03 00:01:23 +000084** Set P4 of the most recently inserted opcode to a column affinity
danielk1977a37cdde2004-05-16 11:15:36 +000085** string for table pTab. A column affinity string has one character
86** for each column indexed by the index, according to the affinity of the
87** column:
88**
89** Character Column affinity
90** ------------------------------
drh3eda0402005-11-24 13:15:32 +000091** 'a' TEXT
92** 'b' NONE
93** 'c' NUMERIC
94** 'd' INTEGER
95** 'e' REAL
danielk1977a37cdde2004-05-16 11:15:36 +000096*/
97void sqlite3TableAffinityStr(Vdbe *v, Table *pTab){
danielk19773d1bfea2004-05-14 11:00:53 +000098 /* The first time a column affinity string for a particular table
99 ** is required, it is allocated and populated here. It is then
100 ** stored as a member of the Table structure for subsequent use.
101 **
102 ** The column affinity string will eventually be deleted by
103 ** sqlite3DeleteTable() when the Table structure itself is cleaned up.
104 */
105 if( !pTab->zColAff ){
106 char *zColAff;
107 int i;
drhabb6fca2007-08-16 12:24:01 +0000108 sqlite3 *db = sqlite3VdbeDb(v);
danielk19773d1bfea2004-05-14 11:00:53 +0000109
drh633e6d52008-07-28 19:34:53 +0000110 zColAff = (char *)sqlite3Malloc(pTab->nCol+1);
danielk19773d1bfea2004-05-14 11:00:53 +0000111 if( !zColAff ){
drh633e6d52008-07-28 19:34:53 +0000112 db->mallocFailed = 1;
danielk1977a37cdde2004-05-16 11:15:36 +0000113 return;
danielk19773d1bfea2004-05-14 11:00:53 +0000114 }
115
116 for(i=0; i<pTab->nCol; i++){
danielk1977a37cdde2004-05-16 11:15:36 +0000117 zColAff[i] = pTab->aCol[i].affinity;
danielk19773d1bfea2004-05-14 11:00:53 +0000118 }
119 zColAff[pTab->nCol] = '\0';
120
121 pTab->zColAff = zColAff;
122 }
123
drh66a51672008-01-03 00:01:23 +0000124 sqlite3VdbeChangeP4(v, -1, pTab->zColAff, 0);
danielk19773d1bfea2004-05-14 11:00:53 +0000125}
126
danielk19774d887782005-02-08 08:42:27 +0000127/*
drh48d11782007-11-23 15:02:19 +0000128** Return non-zero if the table pTab in database iDb or any of its indices
129** have been opened at any point in the VDBE program beginning at location
130** iStartAddr throught the end of the program. This is used to see if
131** a statement of the form "INSERT INTO <iDb, pTab> SELECT ..." can
132** run without using temporary table for the results of the SELECT.
danielk19774d887782005-02-08 08:42:27 +0000133*/
drh48d11782007-11-23 15:02:19 +0000134static int readsTable(Vdbe *v, int iStartAddr, int iDb, Table *pTab){
danielk19774d887782005-02-08 08:42:27 +0000135 int i;
drh48d11782007-11-23 15:02:19 +0000136 int iEnd = sqlite3VdbeCurrentAddr(v);
137 for(i=iStartAddr; i<iEnd; i++){
138 VdbeOp *pOp = sqlite3VdbeGetOp(v, i);
drhef0bea92007-12-14 16:11:09 +0000139 assert( pOp!=0 );
danielk1977207872a2008-01-03 07:54:23 +0000140 if( pOp->opcode==OP_OpenRead && pOp->p3==iDb ){
141 Index *pIndex;
drh48d11782007-11-23 15:02:19 +0000142 int tnum = pOp->p2;
danielk1977207872a2008-01-03 07:54:23 +0000143 if( tnum==pTab->tnum ){
144 return 1;
145 }
146 for(pIndex=pTab->pIndex; pIndex; pIndex=pIndex->pNext){
147 if( tnum==pIndex->tnum ){
drh48d11782007-11-23 15:02:19 +0000148 return 1;
149 }
drh48d11782007-11-23 15:02:19 +0000150 }
151 }
drh543165e2007-11-27 14:46:41 +0000152#ifndef SQLITE_OMIT_VIRTUALTABLE
danielk19772dca4ac2008-01-03 11:50:29 +0000153 if( pOp->opcode==OP_VOpen && pOp->p4.pVtab==pTab->pVtab ){
154 assert( pOp->p4.pVtab!=0 );
drh66a51672008-01-03 00:01:23 +0000155 assert( pOp->p4type==P4_VTAB );
drh48d11782007-11-23 15:02:19 +0000156 return 1;
danielk19774d887782005-02-08 08:42:27 +0000157 }
drh543165e2007-11-27 14:46:41 +0000158#endif
danielk19774d887782005-02-08 08:42:27 +0000159 }
160 return 0;
161}
danielk19773d1bfea2004-05-14 11:00:53 +0000162
drh9d9cf222007-02-13 15:01:11 +0000163#ifndef SQLITE_OMIT_AUTOINCREMENT
164/*
drh0b9f50d2009-06-23 20:28:53 +0000165** Locate or create an AutoincInfo structure associated with table pTab
166** which is in database iDb. Return the register number for the register
167** that holds the maximum rowid.
drh9d9cf222007-02-13 15:01:11 +0000168**
drh0b9f50d2009-06-23 20:28:53 +0000169** There is at most one AutoincInfo structure per table even if the
170** same table is autoincremented multiple times due to inserts within
171** triggers. A new AutoincInfo structure is created if this is the
172** first use of table pTab. On 2nd and subsequent uses, the original
173** AutoincInfo structure is used.
drh9d9cf222007-02-13 15:01:11 +0000174**
drh0b9f50d2009-06-23 20:28:53 +0000175** Three memory locations are allocated:
176**
177** (1) Register to hold the name of the pTab table.
178** (2) Register to hold the maximum ROWID of pTab.
179** (3) Register to hold the rowid in sqlite_sequence of pTab
180**
181** The 2nd register is the one that is returned. That is all the
182** insert routine needs to know about.
drh9d9cf222007-02-13 15:01:11 +0000183*/
184static int autoIncBegin(
185 Parse *pParse, /* Parsing context */
186 int iDb, /* Index of the database holding pTab */
187 Table *pTab /* The table we are writing to */
188){
drh6a288a32008-01-07 19:20:24 +0000189 int memId = 0; /* Register holding maximum rowid */
drh7d10d5a2008-08-20 16:35:10 +0000190 if( pTab->tabFlags & TF_Autoincrement ){
drh0b9f50d2009-06-23 20:28:53 +0000191 AutoincInfo *pInfo;
192
193 pInfo = pParse->pAinc;
194 while( pInfo && pInfo->pTab!=pTab ){ pInfo = pInfo->pNext; }
195 if( pInfo==0 ){
196 pInfo = sqlite3DbMallocRaw(pParse->db, sizeof(*pInfo));
197 if( pInfo==0 ) return 0;
198 pInfo->pNext = pParse->pAinc;
199 pParse->pAinc = pInfo;
200 pInfo->pTab = pTab;
201 pInfo->iDb = iDb;
202 pParse->nMem++; /* Register to hold name of table */
203 pInfo->regCtr = ++pParse->nMem; /* Max rowid register */
204 pParse->nMem++; /* Rowid in sqlite_sequence */
205 }
206 memId = pInfo->regCtr;
drh9d9cf222007-02-13 15:01:11 +0000207 }
208 return memId;
209}
210
211/*
drh0b9f50d2009-06-23 20:28:53 +0000212** This routine generates code that will initialize all of the
213** register used by the autoincrement tracker.
214*/
215void sqlite3AutoincrementBegin(Parse *pParse){
216 AutoincInfo *p; /* Information about an AUTOINCREMENT */
217 sqlite3 *db = pParse->db; /* The database connection */
218 Db *pDb; /* Database only autoinc table */
219 int memId; /* Register holding max rowid */
220 int addr; /* A VDBE address */
221 Vdbe *v = pParse->pVdbe; /* VDBE under construction */
222
223 assert( v ); /* We failed long ago if this is not so */
224 for(p = pParse->pAinc; p; p = p->pNext){
225 pDb = &db->aDb[p->iDb];
226 memId = p->regCtr;
227 sqlite3OpenTable(pParse, 0, p->iDb, pDb->pSchema->pSeqTab, OP_OpenRead);
228 addr = sqlite3VdbeCurrentAddr(v);
229 sqlite3VdbeAddOp4(v, OP_String8, 0, memId-1, 0, p->pTab->zName, 0);
230 sqlite3VdbeAddOp2(v, OP_Rewind, 0, addr+9);
231 sqlite3VdbeAddOp3(v, OP_Column, 0, 0, memId);
232 sqlite3VdbeAddOp3(v, OP_Ne, memId-1, addr+7, memId);
233 sqlite3VdbeChangeP5(v, SQLITE_JUMPIFNULL);
234 sqlite3VdbeAddOp2(v, OP_Rowid, 0, memId+1);
235 sqlite3VdbeAddOp3(v, OP_Column, 0, 1, memId);
236 sqlite3VdbeAddOp2(v, OP_Goto, 0, addr+9);
237 sqlite3VdbeAddOp2(v, OP_Next, 0, addr+2);
238 sqlite3VdbeAddOp2(v, OP_Integer, 0, memId);
239 sqlite3VdbeAddOp0(v, OP_Close);
240 }
241}
242
243/*
drh9d9cf222007-02-13 15:01:11 +0000244** Update the maximum rowid for an autoincrement calculation.
245**
246** This routine should be called when the top of the stack holds a
247** new rowid that is about to be inserted. If that new rowid is
248** larger than the maximum rowid in the memId memory cell, then the
249** memory cell is updated. The stack is unchanged.
250*/
drh6a288a32008-01-07 19:20:24 +0000251static void autoIncStep(Parse *pParse, int memId, int regRowid){
drh9d9cf222007-02-13 15:01:11 +0000252 if( memId>0 ){
drh6a288a32008-01-07 19:20:24 +0000253 sqlite3VdbeAddOp2(pParse->pVdbe, OP_MemMax, memId, regRowid);
drh9d9cf222007-02-13 15:01:11 +0000254 }
255}
256
257/*
drh0b9f50d2009-06-23 20:28:53 +0000258** This routine generates the code needed to write autoincrement
259** maximum rowid values back into the sqlite_sequence register.
260** Every statement that might do an INSERT into an autoincrement
261** table (either directly or through triggers) needs to call this
262** routine just before the "exit" code.
drh9d9cf222007-02-13 15:01:11 +0000263*/
drh0b9f50d2009-06-23 20:28:53 +0000264void sqlite3AutoincrementEnd(Parse *pParse){
265 AutoincInfo *p;
266 Vdbe *v = pParse->pVdbe;
267 sqlite3 *db = pParse->db;
drh6a288a32008-01-07 19:20:24 +0000268
drh0b9f50d2009-06-23 20:28:53 +0000269 assert( v );
270 for(p = pParse->pAinc; p; p = p->pNext){
271 Db *pDb = &db->aDb[p->iDb];
272 int j1, j2, j3, j4, j5;
273 int iRec;
274 int memId = p->regCtr;
275
276 iRec = sqlite3GetTempReg(pParse);
277 sqlite3OpenTable(pParse, 0, p->iDb, pDb->pSchema->pSeqTab, OP_OpenWrite);
drh6a288a32008-01-07 19:20:24 +0000278 j1 = sqlite3VdbeAddOp1(v, OP_NotNull, memId+1);
drh0b9f50d2009-06-23 20:28:53 +0000279 j2 = sqlite3VdbeAddOp0(v, OP_Rewind);
280 j3 = sqlite3VdbeAddOp3(v, OP_Column, 0, 0, iRec);
281 j4 = sqlite3VdbeAddOp3(v, OP_Eq, memId-1, 0, iRec);
282 sqlite3VdbeAddOp2(v, OP_Next, 0, j3);
283 sqlite3VdbeJumpHere(v, j2);
284 sqlite3VdbeAddOp2(v, OP_NewRowid, 0, memId+1);
285 j5 = sqlite3VdbeAddOp0(v, OP_Goto);
286 sqlite3VdbeJumpHere(v, j4);
287 sqlite3VdbeAddOp2(v, OP_Rowid, 0, memId+1);
drh6a288a32008-01-07 19:20:24 +0000288 sqlite3VdbeJumpHere(v, j1);
drh0b9f50d2009-06-23 20:28:53 +0000289 sqlite3VdbeJumpHere(v, j5);
danielk1977a7a8e142008-02-13 18:25:27 +0000290 sqlite3VdbeAddOp3(v, OP_MakeRecord, memId-1, 2, iRec);
drh0b9f50d2009-06-23 20:28:53 +0000291 sqlite3VdbeAddOp3(v, OP_Insert, 0, iRec, memId+1);
drh35573352008-01-08 23:54:25 +0000292 sqlite3VdbeChangeP5(v, OPFLAG_APPEND);
drh0b9f50d2009-06-23 20:28:53 +0000293 sqlite3VdbeAddOp0(v, OP_Close);
294 sqlite3ReleaseTempReg(pParse, iRec);
drh9d9cf222007-02-13 15:01:11 +0000295 }
296}
297#else
298/*
299** If SQLITE_OMIT_AUTOINCREMENT is defined, then the three routines
300** above are all no-ops
301*/
302# define autoIncBegin(A,B,C) (0)
danielk1977287fb612008-01-04 19:10:28 +0000303# define autoIncStep(A,B,C)
drh9d9cf222007-02-13 15:01:11 +0000304#endif /* SQLITE_OMIT_AUTOINCREMENT */
305
306
307/* Forward declaration */
308static int xferOptimization(
309 Parse *pParse, /* Parser context */
310 Table *pDest, /* The table we are inserting into */
311 Select *pSelect, /* A SELECT statement to use as the data source */
312 int onError, /* How to handle constraint errors */
313 int iDbDest /* The database of pDest */
314);
315
danielk19773d1bfea2004-05-14 11:00:53 +0000316/*
drh1ccde152000-06-17 13:12:39 +0000317** This routine is call to handle SQL of the following forms:
drhcce7d172000-05-31 15:34:51 +0000318**
319** insert into TABLE (IDLIST) values(EXPRLIST)
drh1ccde152000-06-17 13:12:39 +0000320** insert into TABLE (IDLIST) select
drhcce7d172000-05-31 15:34:51 +0000321**
drh1ccde152000-06-17 13:12:39 +0000322** The IDLIST following the table name is always optional. If omitted,
323** then a list of all columns for the table is substituted. The IDLIST
drh967e8b72000-06-21 13:59:10 +0000324** appears in the pColumn parameter. pColumn is NULL if IDLIST is omitted.
drh1ccde152000-06-17 13:12:39 +0000325**
326** The pList parameter holds EXPRLIST in the first form of the INSERT
327** statement above, and pSelect is NULL. For the second form, pList is
328** NULL and pSelect is a pointer to the select statement used to generate
329** data for the insert.
drh142e30d2002-08-28 03:00:58 +0000330**
drh9d9cf222007-02-13 15:01:11 +0000331** The code generated follows one of four templates. For a simple
drh142e30d2002-08-28 03:00:58 +0000332** select with data coming from a VALUES clause, the code executes
drhe00ee6e2008-06-20 15:24:01 +0000333** once straight down through. Pseudo-code follows (we call this
334** the "1st template"):
drh142e30d2002-08-28 03:00:58 +0000335**
336** open write cursor to <table> and its indices
337** puts VALUES clause expressions onto the stack
338** write the resulting record into <table>
339** cleanup
340**
drh9d9cf222007-02-13 15:01:11 +0000341** The three remaining templates assume the statement is of the form
drh142e30d2002-08-28 03:00:58 +0000342**
343** INSERT INTO <table> SELECT ...
344**
drh9d9cf222007-02-13 15:01:11 +0000345** If the SELECT clause is of the restricted form "SELECT * FROM <table2>" -
346** in other words if the SELECT pulls all columns from a single table
347** and there is no WHERE or LIMIT or GROUP BY or ORDER BY clauses, and
348** if <table2> and <table1> are distinct tables but have identical
349** schemas, including all the same indices, then a special optimization
350** is invoked that copies raw records from <table2> over to <table1>.
351** See the xferOptimization() function for the implementation of this
drhe00ee6e2008-06-20 15:24:01 +0000352** template. This is the 2nd template.
drh9d9cf222007-02-13 15:01:11 +0000353**
354** open a write cursor to <table>
355** open read cursor on <table2>
356** transfer all records in <table2> over to <table>
357** close cursors
358** foreach index on <table>
359** open a write cursor on the <table> index
360** open a read cursor on the corresponding <table2> index
361** transfer all records from the read to the write cursors
362** close cursors
363** end foreach
364**
drhe00ee6e2008-06-20 15:24:01 +0000365** The 3rd template is for when the second template does not apply
drh9d9cf222007-02-13 15:01:11 +0000366** and the SELECT clause does not read from <table> at any time.
367** The generated code follows this template:
drh142e30d2002-08-28 03:00:58 +0000368**
drhe00ee6e2008-06-20 15:24:01 +0000369** EOF <- 0
370** X <- A
drh142e30d2002-08-28 03:00:58 +0000371** goto B
372** A: setup for the SELECT
drh9d9cf222007-02-13 15:01:11 +0000373** loop over the rows in the SELECT
drhe00ee6e2008-06-20 15:24:01 +0000374** load values into registers R..R+n
375** yield X
drh142e30d2002-08-28 03:00:58 +0000376** end loop
377** cleanup after the SELECT
drhe00ee6e2008-06-20 15:24:01 +0000378** EOF <- 1
379** yield X
drh142e30d2002-08-28 03:00:58 +0000380** goto A
drhe00ee6e2008-06-20 15:24:01 +0000381** B: open write cursor to <table> and its indices
382** C: yield X
383** if EOF goto D
384** insert the select result into <table> from R..R+n
385** goto C
drh142e30d2002-08-28 03:00:58 +0000386** D: cleanup
387**
drhe00ee6e2008-06-20 15:24:01 +0000388** The 4th template is used if the insert statement takes its
drh142e30d2002-08-28 03:00:58 +0000389** values from a SELECT but the data is being inserted into a table
390** that is also read as part of the SELECT. In the third form,
391** we have to use a intermediate table to store the results of
392** the select. The template is like this:
393**
drhe00ee6e2008-06-20 15:24:01 +0000394** EOF <- 0
395** X <- A
drh142e30d2002-08-28 03:00:58 +0000396** goto B
397** A: setup for the SELECT
398** loop over the tables in the SELECT
drhe00ee6e2008-06-20 15:24:01 +0000399** load value into register R..R+n
400** yield X
drh142e30d2002-08-28 03:00:58 +0000401** end loop
402** cleanup after the SELECT
drhe00ee6e2008-06-20 15:24:01 +0000403** EOF <- 1
404** yield X
405** halt-error
406** B: open temp table
407** L: yield X
408** if EOF goto M
409** insert row from R..R+n into temp table
410** goto L
411** M: open write cursor to <table> and its indices
412** rewind temp table
413** C: loop over rows of intermediate table
drh142e30d2002-08-28 03:00:58 +0000414** transfer values form intermediate table into <table>
drhe00ee6e2008-06-20 15:24:01 +0000415** end loop
416** D: cleanup
drhcce7d172000-05-31 15:34:51 +0000417*/
danielk19774adee202004-05-08 08:23:19 +0000418void sqlite3Insert(
drhcce7d172000-05-31 15:34:51 +0000419 Parse *pParse, /* Parser context */
drh113088e2003-03-20 01:16:58 +0000420 SrcList *pTabList, /* Name of table into which we are inserting */
drhcce7d172000-05-31 15:34:51 +0000421 ExprList *pList, /* List of values to be inserted */
drh5974a302000-06-07 14:42:26 +0000422 Select *pSelect, /* A SELECT statement to use as the data source */
drh9cfcf5d2002-01-29 18:41:24 +0000423 IdList *pColumn, /* Column names corresponding to IDLIST. */
424 int onError /* How to handle constraint errors */
drhcce7d172000-05-31 15:34:51 +0000425){
drh6a288a32008-01-07 19:20:24 +0000426 sqlite3 *db; /* The main database structure */
427 Table *pTab; /* The table to insert into. aka TABLE */
drh113088e2003-03-20 01:16:58 +0000428 char *zTab; /* Name of the table into which we are inserting */
drhe22a3342003-04-22 20:30:37 +0000429 const char *zDb; /* Name of the database holding this table */
drh5974a302000-06-07 14:42:26 +0000430 int i, j, idx; /* Loop counters */
431 Vdbe *v; /* Generate code into this virtual machine */
432 Index *pIdx; /* For looping over indices of the table */
drh967e8b72000-06-21 13:59:10 +0000433 int nColumn; /* Number of columns in the data */
drh6a288a32008-01-07 19:20:24 +0000434 int nHidden = 0; /* Number of hidden columns if TABLE is virtual */
drh04adf412008-01-08 18:57:50 +0000435 int baseCur = 0; /* VDBE Cursor number for pTab */
drh4a324312001-12-21 14:30:42 +0000436 int keyColumn = -1; /* Column that is the INTEGER PRIMARY KEY */
drh0ca3e242002-01-29 23:07:02 +0000437 int endOfLoop; /* Label for the end of the insertion loop */
danielk19774d887782005-02-08 08:42:27 +0000438 int useTempTable = 0; /* Store SELECT results in intermediate table */
danielk1977cfe9a692004-06-16 12:00:29 +0000439 int srcTab = 0; /* Data comes from this temporary cursor if >=0 */
drhe00ee6e2008-06-20 15:24:01 +0000440 int addrInsTop = 0; /* Jump to label "D" */
441 int addrCont = 0; /* Top of insert loop. Label "C" in templates 3 and 4 */
442 int addrSelect = 0; /* Address of coroutine that implements the SELECT */
drh2eb95372008-06-06 15:04:36 +0000443 SelectDest dest; /* Destination for SELECT on rhs of INSERT */
drh6a288a32008-01-07 19:20:24 +0000444 int newIdx = -1; /* Cursor for the NEW pseudo-table */
445 int iDb; /* Index of database holding TABLE */
drh2958a4e2004-11-12 03:56:15 +0000446 Db *pDb; /* The database containing table being inserted into */
drhe4d90812007-03-29 05:51:49 +0000447 int appendFlag = 0; /* True if the insert is likely to be an append */
drhcce7d172000-05-31 15:34:51 +0000448
drh6a288a32008-01-07 19:20:24 +0000449 /* Register allocations */
drh1bd10f82008-12-10 21:19:56 +0000450 int regFromSelect = 0;/* Base register for data coming from SELECT */
drh6a288a32008-01-07 19:20:24 +0000451 int regAutoinc = 0; /* Register holding the AUTOINCREMENT counter */
452 int regRowCount = 0; /* Memory cell used for the row counter */
453 int regIns; /* Block of regs holding rowid+data being inserted */
454 int regRowid; /* registers holding insert rowid */
455 int regData; /* register holding first column to insert */
456 int regRecord; /* Holds the assemblied row record */
drh1bd10f82008-12-10 21:19:56 +0000457 int regEof = 0; /* Register recording end of SELECT data */
drhaa9b8962008-01-08 02:57:55 +0000458 int *aRegIdx = 0; /* One register allocated to each index */
drh6a288a32008-01-07 19:20:24 +0000459
danielk1977034ca142007-06-26 10:38:54 +0000460
drh798da522004-11-04 04:42:28 +0000461#ifndef SQLITE_OMIT_TRIGGER
462 int isView; /* True if attempting to insert into a view */
danielk19772f886d12009-02-28 10:47:41 +0000463 Trigger *pTrigger; /* List of triggers on pTab, if required */
464 int tmask; /* Mask of trigger times */
drh798da522004-11-04 04:42:28 +0000465#endif
danielk1977c3f9bad2002-05-15 08:30:12 +0000466
drh17435752007-08-16 04:30:38 +0000467 db = pParse->db;
drh1bd10f82008-12-10 21:19:56 +0000468 memset(&dest, 0, sizeof(dest));
drh17435752007-08-16 04:30:38 +0000469 if( pParse->nErr || db->mallocFailed ){
drh6f7adc82006-01-11 21:41:20 +0000470 goto insert_cleanup;
471 }
drhdaffd0e2001-04-11 14:28:42 +0000472
drh1ccde152000-06-17 13:12:39 +0000473 /* Locate the table into which we will be inserting new information.
474 */
drh113088e2003-03-20 01:16:58 +0000475 assert( pTabList->nSrc==1 );
476 zTab = pTabList->a[0].zName;
drh098d1682009-05-02 15:46:46 +0000477 if( NEVER(zTab==0) ) goto insert_cleanup;
danielk19774adee202004-05-08 08:23:19 +0000478 pTab = sqlite3SrcListLookup(pParse, pTabList);
danielk1977c3f9bad2002-05-15 08:30:12 +0000479 if( pTab==0 ){
danielk1977c3f9bad2002-05-15 08:30:12 +0000480 goto insert_cleanup;
481 }
danielk1977da184232006-01-05 11:34:32 +0000482 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
483 assert( iDb<db->nDb );
484 pDb = &db->aDb[iDb];
drh2958a4e2004-11-12 03:56:15 +0000485 zDb = pDb->zName;
danielk19774adee202004-05-08 08:23:19 +0000486 if( sqlite3AuthCheck(pParse, SQLITE_INSERT, pTab->zName, 0, zDb) ){
drh1962bda2003-01-12 19:33:52 +0000487 goto insert_cleanup;
488 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000489
drhb7f91642004-10-31 02:22:47 +0000490 /* Figure out if we have any triggers and if the table being
491 ** inserted into is a view
492 */
493#ifndef SQLITE_OMIT_TRIGGER
danielk19772f886d12009-02-28 10:47:41 +0000494 pTrigger = sqlite3TriggersExist(pParse, pTab, TK_INSERT, 0, &tmask);
drhb7f91642004-10-31 02:22:47 +0000495 isView = pTab->pSelect!=0;
496#else
danielk19772f886d12009-02-28 10:47:41 +0000497# define pTrigger 0
498# define tmask 0
drhb7f91642004-10-31 02:22:47 +0000499# define isView 0
500#endif
501#ifdef SQLITE_OMIT_VIEW
502# undef isView
503# define isView 0
504#endif
danielk19772f886d12009-02-28 10:47:41 +0000505 assert( (pTrigger && tmask) || (pTrigger==0 && tmask==0) );
drhb7f91642004-10-31 02:22:47 +0000506
danielk1977c3f9bad2002-05-15 08:30:12 +0000507 /* Ensure that:
508 * (a) the table is not read-only,
509 * (b) that if it is a view then ON INSERT triggers exist
510 */
danielk19772f886d12009-02-28 10:47:41 +0000511 if( sqlite3IsReadOnly(pParse, pTab, tmask) ){
danielk1977c3f9bad2002-05-15 08:30:12 +0000512 goto insert_cleanup;
513 }
drh43617e92006-03-06 20:55:46 +0000514 assert( pTab!=0 );
drh1ccde152000-06-17 13:12:39 +0000515
drhf573c992002-07-31 00:32:50 +0000516 /* If pTab is really a view, make sure it has been initialized.
danielk1977b3d24bf2006-06-19 03:05:10 +0000517 ** ViewGetColumnNames() is a no-op if pTab is not a view (or virtual
518 ** module table).
drhf573c992002-07-31 00:32:50 +0000519 */
danielk1977b3d24bf2006-06-19 03:05:10 +0000520 if( sqlite3ViewGetColumnNames(pParse, pTab) ){
drh5cf590c2003-04-24 01:45:04 +0000521 goto insert_cleanup;
drhf573c992002-07-31 00:32:50 +0000522 }
523
drh1ccde152000-06-17 13:12:39 +0000524 /* Allocate a VDBE
525 */
danielk19774adee202004-05-08 08:23:19 +0000526 v = sqlite3GetVdbe(pParse);
drh5974a302000-06-07 14:42:26 +0000527 if( v==0 ) goto insert_cleanup;
drh4794f732004-11-05 17:17:50 +0000528 if( pParse->nested==0 ) sqlite3VdbeCountChanges(v);
danielk19772f886d12009-02-28 10:47:41 +0000529 sqlite3BeginWriteOperation(pParse, pSelect || pTrigger, iDb);
drh1ccde152000-06-17 13:12:39 +0000530
danielk1977c3f9bad2002-05-15 08:30:12 +0000531 /* if there are row triggers, allocate a temp table for new.* references. */
danielk19772f886d12009-02-28 10:47:41 +0000532 if( pTrigger ){
danielk1977c3f9bad2002-05-15 08:30:12 +0000533 newIdx = pParse->nTab++;
danielk1977f29ce552002-05-19 23:43:12 +0000534 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000535
drh9d9cf222007-02-13 15:01:11 +0000536#ifndef SQLITE_OMIT_XFER_OPT
537 /* If the statement is of the form
538 **
539 ** INSERT INTO <table1> SELECT * FROM <table2>;
540 **
541 ** Then special optimizations can be applied that make the transfer
542 ** very fast and which reduce fragmentation of indices.
drhe00ee6e2008-06-20 15:24:01 +0000543 **
544 ** This is the 2nd template.
drh9d9cf222007-02-13 15:01:11 +0000545 */
546 if( pColumn==0 && xferOptimization(pParse, pTab, pSelect, onError, iDb) ){
danielk19772f886d12009-02-28 10:47:41 +0000547 assert( !pTrigger );
drh9d9cf222007-02-13 15:01:11 +0000548 assert( pList==0 );
drh0b9f50d2009-06-23 20:28:53 +0000549 goto insert_end;
drh9d9cf222007-02-13 15:01:11 +0000550 }
551#endif /* SQLITE_OMIT_XFER_OPT */
552
drh2958a4e2004-11-12 03:56:15 +0000553 /* If this is an AUTOINCREMENT table, look up the sequence number in the
drh6a288a32008-01-07 19:20:24 +0000554 ** sqlite_sequence table and store it in memory cell regAutoinc.
drh2958a4e2004-11-12 03:56:15 +0000555 */
drh6a288a32008-01-07 19:20:24 +0000556 regAutoinc = autoIncBegin(pParse, iDb, pTab);
drh2958a4e2004-11-12 03:56:15 +0000557
drh1ccde152000-06-17 13:12:39 +0000558 /* Figure out how many columns of data are supplied. If the data
drhe00ee6e2008-06-20 15:24:01 +0000559 ** is coming from a SELECT statement, then generate a co-routine that
560 ** produces a single row of the SELECT on each invocation. The
561 ** co-routine is the common header to the 3rd and 4th templates.
drh1ccde152000-06-17 13:12:39 +0000562 */
drh5974a302000-06-07 14:42:26 +0000563 if( pSelect ){
drh142e30d2002-08-28 03:00:58 +0000564 /* Data is coming from a SELECT. Generate code to implement that SELECT
drhe00ee6e2008-06-20 15:24:01 +0000565 ** as a co-routine. The code is common to both the 3rd and 4th
566 ** templates:
567 **
568 ** EOF <- 0
569 ** X <- A
570 ** goto B
571 ** A: setup for the SELECT
572 ** loop over the tables in the SELECT
573 ** load value into register R..R+n
574 ** yield X
575 ** end loop
576 ** cleanup after the SELECT
577 ** EOF <- 1
578 ** yield X
579 ** halt-error
580 **
581 ** On each invocation of the co-routine, it puts a single row of the
582 ** SELECT result into registers dest.iMem...dest.iMem+dest.nMem-1.
583 ** (These output registers are allocated by sqlite3Select().) When
584 ** the SELECT completes, it sets the EOF flag stored in regEof.
drh142e30d2002-08-28 03:00:58 +0000585 */
drhe00ee6e2008-06-20 15:24:01 +0000586 int rc, j1;
drh1013c932008-01-06 00:25:21 +0000587
drhe00ee6e2008-06-20 15:24:01 +0000588 regEof = ++pParse->nMem;
589 sqlite3VdbeAddOp2(v, OP_Integer, 0, regEof); /* EOF <- 0 */
590 VdbeComment((v, "SELECT eof flag"));
drh92b01d52008-06-24 00:32:35 +0000591 sqlite3SelectDestInit(&dest, SRT_Coroutine, ++pParse->nMem);
drhe00ee6e2008-06-20 15:24:01 +0000592 addrSelect = sqlite3VdbeCurrentAddr(v)+2;
drh92b01d52008-06-24 00:32:35 +0000593 sqlite3VdbeAddOp2(v, OP_Integer, addrSelect-1, dest.iParm);
drhe00ee6e2008-06-20 15:24:01 +0000594 j1 = sqlite3VdbeAddOp2(v, OP_Goto, 0, 0);
595 VdbeComment((v, "Jump over SELECT coroutine"));
danielk1977b3bce662005-01-29 08:32:43 +0000596
597 /* Resolve the expressions in the SELECT statement and execute it. */
drh7d10d5a2008-08-20 16:35:10 +0000598 rc = sqlite3Select(pParse, pSelect, &dest);
drh098d1682009-05-02 15:46:46 +0000599 assert( pParse->nErr==0 || rc );
600 if( rc || NEVER(pParse->nErr) || db->mallocFailed ){
drh6f7adc82006-01-11 21:41:20 +0000601 goto insert_cleanup;
602 }
drhe00ee6e2008-06-20 15:24:01 +0000603 sqlite3VdbeAddOp2(v, OP_Integer, 1, regEof); /* EOF <- 1 */
drh92b01d52008-06-24 00:32:35 +0000604 sqlite3VdbeAddOp1(v, OP_Yield, dest.iParm); /* yield X */
drhe00ee6e2008-06-20 15:24:01 +0000605 sqlite3VdbeAddOp2(v, OP_Halt, SQLITE_INTERNAL, OE_Abort);
606 VdbeComment((v, "End of SELECT coroutine"));
607 sqlite3VdbeJumpHere(v, j1); /* label B: */
danielk1977b3bce662005-01-29 08:32:43 +0000608
drh6a288a32008-01-07 19:20:24 +0000609 regFromSelect = dest.iMem;
drh5974a302000-06-07 14:42:26 +0000610 assert( pSelect->pEList );
drh967e8b72000-06-21 13:59:10 +0000611 nColumn = pSelect->pEList->nExpr;
drhe00ee6e2008-06-20 15:24:01 +0000612 assert( dest.nMem==nColumn );
drh142e30d2002-08-28 03:00:58 +0000613
614 /* Set useTempTable to TRUE if the result of the SELECT statement
drhe00ee6e2008-06-20 15:24:01 +0000615 ** should be written into a temporary table (template 4). Set to
616 ** FALSE if each* row of the SELECT can be written directly into
617 ** the destination table (template 3).
drh048c5302003-04-03 01:50:44 +0000618 **
619 ** A temp table must be used if the table being updated is also one
620 ** of the tables being read by the SELECT statement. Also use a
621 ** temp table in the case of row triggers.
drh142e30d2002-08-28 03:00:58 +0000622 */
danielk19772f886d12009-02-28 10:47:41 +0000623 if( pTrigger || readsTable(v, addrSelect, iDb, pTab) ){
drh048c5302003-04-03 01:50:44 +0000624 useTempTable = 1;
drh048c5302003-04-03 01:50:44 +0000625 }
drh142e30d2002-08-28 03:00:58 +0000626
627 if( useTempTable ){
drhe00ee6e2008-06-20 15:24:01 +0000628 /* Invoke the coroutine to extract information from the SELECT
629 ** and add it to a transient table srcTab. The code generated
630 ** here is from the 4th template:
631 **
632 ** B: open temp table
633 ** L: yield X
634 ** if EOF goto M
635 ** insert row from R..R+n into temp table
636 ** goto L
637 ** M: ...
drh142e30d2002-08-28 03:00:58 +0000638 */
drhdc5ea5c2008-12-10 17:19:59 +0000639 int regRec; /* Register to hold packed record */
640 int regTempRowid; /* Register to hold temp table ROWID */
641 int addrTop; /* Label "L" */
642 int addrIf; /* Address of jump to M */
drhb7654112008-01-12 12:48:07 +0000643
drh142e30d2002-08-28 03:00:58 +0000644 srcTab = pParse->nTab++;
drhb7654112008-01-12 12:48:07 +0000645 regRec = sqlite3GetTempReg(pParse);
drhdc5ea5c2008-12-10 17:19:59 +0000646 regTempRowid = sqlite3GetTempReg(pParse);
drhe00ee6e2008-06-20 15:24:01 +0000647 sqlite3VdbeAddOp2(v, OP_OpenEphemeral, srcTab, nColumn);
drh92b01d52008-06-24 00:32:35 +0000648 addrTop = sqlite3VdbeAddOp1(v, OP_Yield, dest.iParm);
drhe00ee6e2008-06-20 15:24:01 +0000649 addrIf = sqlite3VdbeAddOp1(v, OP_If, regEof);
drh1db639c2008-01-17 02:36:28 +0000650 sqlite3VdbeAddOp3(v, OP_MakeRecord, regFromSelect, nColumn, regRec);
drhdc5ea5c2008-12-10 17:19:59 +0000651 sqlite3VdbeAddOp2(v, OP_NewRowid, srcTab, regTempRowid);
652 sqlite3VdbeAddOp3(v, OP_Insert, srcTab, regRec, regTempRowid);
drhe00ee6e2008-06-20 15:24:01 +0000653 sqlite3VdbeAddOp2(v, OP_Goto, 0, addrTop);
654 sqlite3VdbeJumpHere(v, addrIf);
drhb7654112008-01-12 12:48:07 +0000655 sqlite3ReleaseTempReg(pParse, regRec);
drhdc5ea5c2008-12-10 17:19:59 +0000656 sqlite3ReleaseTempReg(pParse, regTempRowid);
drh142e30d2002-08-28 03:00:58 +0000657 }
drh5974a302000-06-07 14:42:26 +0000658 }else{
drh142e30d2002-08-28 03:00:58 +0000659 /* This is the case if the data for the INSERT is coming from a VALUES
660 ** clause
661 */
danielk1977b3bce662005-01-29 08:32:43 +0000662 NameContext sNC;
663 memset(&sNC, 0, sizeof(sNC));
664 sNC.pParse = pParse;
drh5974a302000-06-07 14:42:26 +0000665 srcTab = -1;
drh48d11782007-11-23 15:02:19 +0000666 assert( useTempTable==0 );
drh147d0cc2006-08-25 23:42:53 +0000667 nColumn = pList ? pList->nExpr : 0;
drhe64e7b22002-02-18 13:56:36 +0000668 for(i=0; i<nColumn; i++){
drh7d10d5a2008-08-20 16:35:10 +0000669 if( sqlite3ResolveExprNames(&sNC, pList->a[i].pExpr) ){
drhb04a5d82002-04-12 03:55:15 +0000670 goto insert_cleanup;
671 }
drhe64e7b22002-02-18 13:56:36 +0000672 }
drh5974a302000-06-07 14:42:26 +0000673 }
drh1ccde152000-06-17 13:12:39 +0000674
675 /* Make sure the number of columns in the source data matches the number
676 ** of columns to be inserted into the table.
677 */
danielk1977034ca142007-06-26 10:38:54 +0000678 if( IsVirtual(pTab) ){
679 for(i=0; i<pTab->nCol; i++){
680 nHidden += (IsHiddenColumn(&pTab->aCol[i]) ? 1 : 0);
681 }
682 }
683 if( pColumn==0 && nColumn && nColumn!=(pTab->nCol-nHidden) ){
danielk19774adee202004-05-08 08:23:19 +0000684 sqlite3ErrorMsg(pParse,
drhda93d232003-03-31 02:12:46 +0000685 "table %S has %d columns but %d values were supplied",
drhd51397a2009-05-01 15:17:48 +0000686 pTabList, 0, pTab->nCol-nHidden, nColumn);
drhcce7d172000-05-31 15:34:51 +0000687 goto insert_cleanup;
688 }
drh967e8b72000-06-21 13:59:10 +0000689 if( pColumn!=0 && nColumn!=pColumn->nId ){
danielk19774adee202004-05-08 08:23:19 +0000690 sqlite3ErrorMsg(pParse, "%d values for %d columns", nColumn, pColumn->nId);
drhcce7d172000-05-31 15:34:51 +0000691 goto insert_cleanup;
692 }
drh1ccde152000-06-17 13:12:39 +0000693
694 /* If the INSERT statement included an IDLIST term, then make sure
695 ** all elements of the IDLIST really are columns of the table and
696 ** remember the column indices.
drhc8392582001-12-31 02:48:51 +0000697 **
698 ** If the table has an INTEGER PRIMARY KEY column and that column
699 ** is named in the IDLIST, then record in the keyColumn variable
700 ** the index into IDLIST of the primary key column. keyColumn is
701 ** the index of the primary key as it appears in IDLIST, not as
702 ** is appears in the original table. (The index of the primary
703 ** key in the original table is pTab->iPKey.)
drh1ccde152000-06-17 13:12:39 +0000704 */
drh967e8b72000-06-21 13:59:10 +0000705 if( pColumn ){
706 for(i=0; i<pColumn->nId; i++){
707 pColumn->a[i].idx = -1;
drhcce7d172000-05-31 15:34:51 +0000708 }
drh967e8b72000-06-21 13:59:10 +0000709 for(i=0; i<pColumn->nId; i++){
drhcce7d172000-05-31 15:34:51 +0000710 for(j=0; j<pTab->nCol; j++){
danielk19774adee202004-05-08 08:23:19 +0000711 if( sqlite3StrICmp(pColumn->a[i].zName, pTab->aCol[j].zName)==0 ){
drh967e8b72000-06-21 13:59:10 +0000712 pColumn->a[i].idx = j;
drh4a324312001-12-21 14:30:42 +0000713 if( j==pTab->iPKey ){
drh9aa028d2001-12-22 21:48:29 +0000714 keyColumn = i;
drh4a324312001-12-21 14:30:42 +0000715 }
drhcce7d172000-05-31 15:34:51 +0000716 break;
717 }
718 }
719 if( j>=pTab->nCol ){
danielk19774adee202004-05-08 08:23:19 +0000720 if( sqlite3IsRowid(pColumn->a[i].zName) ){
drha0217ba2003-06-01 01:10:33 +0000721 keyColumn = i;
722 }else{
danielk19774adee202004-05-08 08:23:19 +0000723 sqlite3ErrorMsg(pParse, "table %S has no column named %s",
drha0217ba2003-06-01 01:10:33 +0000724 pTabList, 0, pColumn->a[i].zName);
725 pParse->nErr++;
726 goto insert_cleanup;
727 }
drhcce7d172000-05-31 15:34:51 +0000728 }
729 }
730 }
drh1ccde152000-06-17 13:12:39 +0000731
drhaacc5432002-01-06 17:07:40 +0000732 /* If there is no IDLIST term but the table has an integer primary
drhc8392582001-12-31 02:48:51 +0000733 ** key, the set the keyColumn variable to the primary key column index
734 ** in the original table definition.
drh4a324312001-12-21 14:30:42 +0000735 */
drh147d0cc2006-08-25 23:42:53 +0000736 if( pColumn==0 && nColumn>0 ){
drh4a324312001-12-21 14:30:42 +0000737 keyColumn = pTab->iPKey;
738 }
739
drh142e30d2002-08-28 03:00:58 +0000740 /* Open the temp table for FOR EACH ROW triggers
741 */
danielk19772f886d12009-02-28 10:47:41 +0000742 if( pTrigger ){
danielk1977d336e222009-02-20 10:58:41 +0000743 sqlite3VdbeAddOp3(v, OP_OpenPseudo, newIdx, 0, pTab->nCol);
danielk1977f29ce552002-05-19 23:43:12 +0000744 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000745
drhfeeb1392002-04-09 03:28:01 +0000746 /* Initialize the count of rows to be inserted
747 */
drh142e30d2002-08-28 03:00:58 +0000748 if( db->flags & SQLITE_CountRows ){
drh6a288a32008-01-07 19:20:24 +0000749 regRowCount = ++pParse->nMem;
750 sqlite3VdbeAddOp2(v, OP_Integer, 0, regRowCount);
drhfeeb1392002-04-09 03:28:01 +0000751 }
752
danielk1977e448dc42008-01-02 11:50:51 +0000753 /* If this is not a view, open the table and and all indices */
754 if( !isView ){
drhaa9b8962008-01-08 02:57:55 +0000755 int nIdx;
drhaa9b8962008-01-08 02:57:55 +0000756
drh04adf412008-01-08 18:57:50 +0000757 baseCur = pParse->nTab;
758 nIdx = sqlite3OpenTableAndIndices(pParse, pTab, baseCur, OP_OpenWrite);
drh5c070532008-04-11 15:36:03 +0000759 aRegIdx = sqlite3DbMallocRaw(db, sizeof(int)*(nIdx+1));
drhaa9b8962008-01-08 02:57:55 +0000760 if( aRegIdx==0 ){
761 goto insert_cleanup;
762 }
763 for(i=0; i<nIdx; i++){
764 aRegIdx[i] = ++pParse->nMem;
765 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000766 }
767
drhe00ee6e2008-06-20 15:24:01 +0000768 /* This is the top of the main insertion loop */
drh142e30d2002-08-28 03:00:58 +0000769 if( useTempTable ){
drhe00ee6e2008-06-20 15:24:01 +0000770 /* This block codes the top of loop only. The complete loop is the
771 ** following pseudocode (template 4):
772 **
773 ** rewind temp table
774 ** C: loop over rows of intermediate table
775 ** transfer values form intermediate table into <table>
776 ** end loop
777 ** D: ...
778 */
779 addrInsTop = sqlite3VdbeAddOp1(v, OP_Rewind, srcTab);
780 addrCont = sqlite3VdbeCurrentAddr(v);
drh142e30d2002-08-28 03:00:58 +0000781 }else if( pSelect ){
drhe00ee6e2008-06-20 15:24:01 +0000782 /* This block codes the top of loop only. The complete loop is the
783 ** following pseudocode (template 3):
784 **
785 ** C: yield X
786 ** if EOF goto D
787 ** insert the select result into <table> from R..R+n
788 ** goto C
789 ** D: ...
790 */
drh92b01d52008-06-24 00:32:35 +0000791 addrCont = sqlite3VdbeAddOp1(v, OP_Yield, dest.iParm);
drhe00ee6e2008-06-20 15:24:01 +0000792 addrInsTop = sqlite3VdbeAddOp1(v, OP_If, regEof);
drh5974a302000-06-07 14:42:26 +0000793 }
drh1ccde152000-06-17 13:12:39 +0000794
drh6a288a32008-01-07 19:20:24 +0000795 /* Allocate registers for holding the rowid of the new row,
796 ** the content of the new row, and the assemblied row record.
797 */
798 regRecord = ++pParse->nMem;
799 regRowid = regIns = pParse->nMem+1;
800 pParse->nMem += pTab->nCol + 1;
801 if( IsVirtual(pTab) ){
802 regRowid++;
803 pParse->nMem++;
804 }
805 regData = regRowid+1;
806
drh5cf590c2003-04-24 01:45:04 +0000807 /* Run the BEFORE and INSTEAD OF triggers, if there are any
drh70ce3f02003-04-15 19:22:22 +0000808 */
danielk19774adee202004-05-08 08:23:19 +0000809 endOfLoop = sqlite3VdbeMakeLabel(v);
danielk19772f886d12009-02-28 10:47:41 +0000810 if( tmask & TRIGGER_BEFORE ){
drhdc5ea5c2008-12-10 17:19:59 +0000811 int regTrigRowid;
drh2d401ab2008-01-10 23:50:11 +0000812 int regCols;
813 int regRec;
danielk1977c3f9bad2002-05-15 08:30:12 +0000814
drh70ce3f02003-04-15 19:22:22 +0000815 /* build the NEW.* reference row. Note that if there is an INTEGER
816 ** PRIMARY KEY into which a NULL is being inserted, that NULL will be
817 ** translated into a unique ID for the row. But on a BEFORE trigger,
818 ** we do not know what the unique ID will be (because the insert has
819 ** not happened yet) so we substitute a rowid of -1
820 */
drhdc5ea5c2008-12-10 17:19:59 +0000821 regTrigRowid = sqlite3GetTempReg(pParse);
drh70ce3f02003-04-15 19:22:22 +0000822 if( keyColumn<0 ){
drhdc5ea5c2008-12-10 17:19:59 +0000823 sqlite3VdbeAddOp2(v, OP_Integer, -1, regTrigRowid);
drh70ce3f02003-04-15 19:22:22 +0000824 }else{
drh6a288a32008-01-07 19:20:24 +0000825 int j1;
drh7fe45902009-05-01 02:08:04 +0000826 if( useTempTable ){
827 sqlite3VdbeAddOp3(v, OP_Column, srcTab, keyColumn, regTrigRowid);
828 }else{
829 assert( pSelect==0 ); /* Otherwise useTempTable is true */
830 sqlite3ExprCode(pParse, pList->a[keyColumn].pExpr, regTrigRowid);
831 }
drhdc5ea5c2008-12-10 17:19:59 +0000832 j1 = sqlite3VdbeAddOp1(v, OP_NotNull, regTrigRowid);
833 sqlite3VdbeAddOp2(v, OP_Integer, -1, regTrigRowid);
drh6a288a32008-01-07 19:20:24 +0000834 sqlite3VdbeJumpHere(v, j1);
drhdc5ea5c2008-12-10 17:19:59 +0000835 sqlite3VdbeAddOp1(v, OP_MustBeInt, regTrigRowid);
drh70ce3f02003-04-15 19:22:22 +0000836 }
837
danielk1977034ca142007-06-26 10:38:54 +0000838 /* Cannot have triggers on a virtual table. If it were possible,
839 ** this block would have to account for hidden column.
840 */
841 assert(!IsVirtual(pTab));
842
drh70ce3f02003-04-15 19:22:22 +0000843 /* Create the new column data
844 */
drh2d401ab2008-01-10 23:50:11 +0000845 regCols = sqlite3GetTempRange(pParse, pTab->nCol);
danielk1977c3f9bad2002-05-15 08:30:12 +0000846 for(i=0; i<pTab->nCol; i++){
847 if( pColumn==0 ){
drh9adf9ac2002-05-15 11:44:13 +0000848 j = i;
danielk1977c3f9bad2002-05-15 08:30:12 +0000849 }else{
drh9adf9ac2002-05-15 11:44:13 +0000850 for(j=0; j<pColumn->nId; j++){
851 if( pColumn->a[j].idx==i ) break;
852 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000853 }
854 if( pColumn && j>=pColumn->nId ){
drh2d401ab2008-01-10 23:50:11 +0000855 sqlite3ExprCode(pParse, pTab->aCol[i].pDflt, regCols+i);
drh142e30d2002-08-28 03:00:58 +0000856 }else if( useTempTable ){
drh2d401ab2008-01-10 23:50:11 +0000857 sqlite3VdbeAddOp3(v, OP_Column, srcTab, j, regCols+i);
danielk1977c3f9bad2002-05-15 08:30:12 +0000858 }else{
drhd6fe9612005-01-14 01:22:00 +0000859 assert( pSelect==0 ); /* Otherwise useTempTable is true */
drh2d401ab2008-01-10 23:50:11 +0000860 sqlite3ExprCodeAndCache(pParse, pList->a[j].pExpr, regCols+i);
danielk1977c3f9bad2002-05-15 08:30:12 +0000861 }
862 }
drh2d401ab2008-01-10 23:50:11 +0000863 regRec = sqlite3GetTempReg(pParse);
drh1db639c2008-01-17 02:36:28 +0000864 sqlite3VdbeAddOp3(v, OP_MakeRecord, regCols, pTab->nCol, regRec);
danielk1977a37cdde2004-05-16 11:15:36 +0000865
866 /* If this is an INSERT on a view with an INSTEAD OF INSERT trigger,
867 ** do not attempt any conversions before assembling the record.
868 ** If this is a real table, attempt conversions as required by the
869 ** table column affinities.
870 */
871 if( !isView ){
872 sqlite3TableAffinityStr(v, pTab);
873 }
drhdc5ea5c2008-12-10 17:19:59 +0000874 sqlite3VdbeAddOp3(v, OP_Insert, newIdx, regRec, regTrigRowid);
drh2d401ab2008-01-10 23:50:11 +0000875 sqlite3ReleaseTempReg(pParse, regRec);
drhdc5ea5c2008-12-10 17:19:59 +0000876 sqlite3ReleaseTempReg(pParse, regTrigRowid);
drh2d401ab2008-01-10 23:50:11 +0000877 sqlite3ReleaseTempRange(pParse, regCols, pTab->nCol);
danielk1977c3f9bad2002-05-15 08:30:12 +0000878
drh5cf590c2003-04-24 01:45:04 +0000879 /* Fire BEFORE or INSTEAD OF triggers */
danielk19772f886d12009-02-28 10:47:41 +0000880 if( sqlite3CodeRowTrigger(pParse, pTrigger, TK_INSERT, 0, TRIGGER_BEFORE,
881 pTab, newIdx, -1, onError, endOfLoop, 0, 0) ){
danielk1977f29ce552002-05-19 23:43:12 +0000882 goto insert_cleanup;
883 }
drh70ce3f02003-04-15 19:22:22 +0000884 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000885
drh4a324312001-12-21 14:30:42 +0000886 /* Push the record number for the new entry onto the stack. The
drhf0863fe2005-06-12 21:35:51 +0000887 ** record number is a randomly generate integer created by NewRowid
drh4a324312001-12-21 14:30:42 +0000888 ** except when the table has an INTEGER PRIMARY KEY column, in which
drhb419a922002-01-30 16:17:23 +0000889 ** case the record number is the same as that column.
drh1ccde152000-06-17 13:12:39 +0000890 */
drh5cf590c2003-04-24 01:45:04 +0000891 if( !isView ){
drh4cbdda92006-06-14 19:00:20 +0000892 if( IsVirtual(pTab) ){
danielk1977287fb612008-01-04 19:10:28 +0000893 /* The row that the VUpdate opcode will delete: none */
drh6a288a32008-01-07 19:20:24 +0000894 sqlite3VdbeAddOp2(v, OP_Null, 0, regIns);
drh4cbdda92006-06-14 19:00:20 +0000895 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000896 if( keyColumn>=0 ){
drh142e30d2002-08-28 03:00:58 +0000897 if( useTempTable ){
drh6a288a32008-01-07 19:20:24 +0000898 sqlite3VdbeAddOp3(v, OP_Column, srcTab, keyColumn, regRowid);
drh142e30d2002-08-28 03:00:58 +0000899 }else if( pSelect ){
drhb7654112008-01-12 12:48:07 +0000900 sqlite3VdbeAddOp2(v, OP_SCopy, regFromSelect+keyColumn, regRowid);
danielk1977c3f9bad2002-05-15 08:30:12 +0000901 }else{
drhe4d90812007-03-29 05:51:49 +0000902 VdbeOp *pOp;
drh1db639c2008-01-17 02:36:28 +0000903 sqlite3ExprCode(pParse, pList->a[keyColumn].pExpr, regRowid);
drh20411ea2009-05-29 19:00:12 +0000904 pOp = sqlite3VdbeGetOp(v, -1);
drh1b7ecbb2009-05-03 01:00:59 +0000905 if( ALWAYS(pOp) && pOp->opcode==OP_Null && !IsVirtual(pTab) ){
drhe4d90812007-03-29 05:51:49 +0000906 appendFlag = 1;
907 pOp->opcode = OP_NewRowid;
drh04adf412008-01-08 18:57:50 +0000908 pOp->p1 = baseCur;
drh6a288a32008-01-07 19:20:24 +0000909 pOp->p2 = regRowid;
910 pOp->p3 = regAutoinc;
drhe4d90812007-03-29 05:51:49 +0000911 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000912 }
drhf0863fe2005-06-12 21:35:51 +0000913 /* If the PRIMARY KEY expression is NULL, then use OP_NewRowid
drh27a32782002-06-19 20:32:43 +0000914 ** to generate a unique primary key value.
915 */
drhe4d90812007-03-29 05:51:49 +0000916 if( !appendFlag ){
drh1db639c2008-01-17 02:36:28 +0000917 int j1;
danielk1977bb50e7a2008-07-04 10:56:07 +0000918 if( !IsVirtual(pTab) ){
919 j1 = sqlite3VdbeAddOp1(v, OP_NotNull, regRowid);
920 sqlite3VdbeAddOp3(v, OP_NewRowid, baseCur, regRowid, regAutoinc);
921 sqlite3VdbeJumpHere(v, j1);
922 }else{
923 j1 = sqlite3VdbeCurrentAddr(v);
924 sqlite3VdbeAddOp2(v, OP_IsNull, regRowid, j1+2);
925 }
drh3c84ddf2008-01-09 02:15:38 +0000926 sqlite3VdbeAddOp1(v, OP_MustBeInt, regRowid);
drhe4d90812007-03-29 05:51:49 +0000927 }
drh4cbdda92006-06-14 19:00:20 +0000928 }else if( IsVirtual(pTab) ){
drh6a288a32008-01-07 19:20:24 +0000929 sqlite3VdbeAddOp2(v, OP_Null, 0, regRowid);
danielk1977c3f9bad2002-05-15 08:30:12 +0000930 }else{
drh04adf412008-01-08 18:57:50 +0000931 sqlite3VdbeAddOp3(v, OP_NewRowid, baseCur, regRowid, regAutoinc);
drhe4d90812007-03-29 05:51:49 +0000932 appendFlag = 1;
drh4a324312001-12-21 14:30:42 +0000933 }
drh6a288a32008-01-07 19:20:24 +0000934 autoIncStep(pParse, regAutoinc, regRowid);
drh4a324312001-12-21 14:30:42 +0000935
danielk1977c3f9bad2002-05-15 08:30:12 +0000936 /* Push onto the stack, data for all columns of the new entry, beginning
danielk1977f29ce552002-05-19 23:43:12 +0000937 ** with the first column.
938 */
danielk1977034ca142007-06-26 10:38:54 +0000939 nHidden = 0;
danielk1977c3f9bad2002-05-15 08:30:12 +0000940 for(i=0; i<pTab->nCol; i++){
drh6a288a32008-01-07 19:20:24 +0000941 int iRegStore = regRowid+1+i;
danielk1977c3f9bad2002-05-15 08:30:12 +0000942 if( i==pTab->iPKey ){
drh9adf9ac2002-05-15 11:44:13 +0000943 /* The value of the INTEGER PRIMARY KEY column is always a NULL.
danielk1977f29ce552002-05-19 23:43:12 +0000944 ** Whenever this column is read, the record number will be substituted
945 ** in its place. So will fill this column with a NULL to avoid
946 ** taking up data space with information that will never be used. */
drh4c583122008-01-04 22:01:03 +0000947 sqlite3VdbeAddOp2(v, OP_Null, 0, iRegStore);
drh9adf9ac2002-05-15 11:44:13 +0000948 continue;
danielk1977c3f9bad2002-05-15 08:30:12 +0000949 }
950 if( pColumn==0 ){
danielk1977034ca142007-06-26 10:38:54 +0000951 if( IsHiddenColumn(&pTab->aCol[i]) ){
952 assert( IsVirtual(pTab) );
953 j = -1;
954 nHidden++;
955 }else{
956 j = i - nHidden;
957 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000958 }else{
drh9adf9ac2002-05-15 11:44:13 +0000959 for(j=0; j<pColumn->nId; j++){
960 if( pColumn->a[j].idx==i ) break;
961 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000962 }
danielk1977034ca142007-06-26 10:38:54 +0000963 if( j<0 || nColumn==0 || (pColumn && j>=pColumn->nId) ){
danielk1977287fb612008-01-04 19:10:28 +0000964 sqlite3ExprCode(pParse, pTab->aCol[i].pDflt, iRegStore);
drh142e30d2002-08-28 03:00:58 +0000965 }else if( useTempTable ){
danielk1977287fb612008-01-04 19:10:28 +0000966 sqlite3VdbeAddOp3(v, OP_Column, srcTab, j, iRegStore);
drh142e30d2002-08-28 03:00:58 +0000967 }else if( pSelect ){
drhb7654112008-01-12 12:48:07 +0000968 sqlite3VdbeAddOp2(v, OP_SCopy, regFromSelect+j, iRegStore);
danielk1977c3f9bad2002-05-15 08:30:12 +0000969 }else{
danielk1977287fb612008-01-04 19:10:28 +0000970 sqlite3ExprCode(pParse, pList->a[j].pExpr, iRegStore);
drh5974a302000-06-07 14:42:26 +0000971 }
drhbed86902000-06-02 13:27:59 +0000972 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000973
974 /* Generate code to check constraints and generate index keys and
danielk1977f29ce552002-05-19 23:43:12 +0000975 ** do the insertion.
976 */
drh4cbdda92006-06-14 19:00:20 +0000977#ifndef SQLITE_OMIT_VIRTUALTABLE
978 if( IsVirtual(pTab) ){
drh4f3dd152008-04-28 18:46:43 +0000979 sqlite3VtabMakeWritable(pParse, pTab);
drh6a288a32008-01-07 19:20:24 +0000980 sqlite3VdbeAddOp4(v, OP_VUpdate, 1, pTab->nCol+2, regIns,
drh66a51672008-01-03 00:01:23 +0000981 (const char*)pTab->pVtab, P4_VTAB);
drh4cbdda92006-06-14 19:00:20 +0000982 }else
983#endif
984 {
danielk1977de630352009-05-04 11:42:29 +0000985 int isReplace; /* Set to true if constraints may cause a replace */
986 sqlite3GenerateConstraintChecks(pParse, pTab, baseCur, regIns, aRegIdx,
987 keyColumn>=0, 0, onError, endOfLoop, &isReplace
drh04adf412008-01-08 18:57:50 +0000988 );
989 sqlite3CompleteInsertion(
danielk1977de630352009-05-04 11:42:29 +0000990 pParse, pTab, baseCur, regIns, aRegIdx, 0,
991 (tmask&TRIGGER_AFTER) ? newIdx : -1, appendFlag, isReplace==0
992 );
drh4cbdda92006-06-14 19:00:20 +0000993 }
drh5cf590c2003-04-24 01:45:04 +0000994 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000995
drh5cf590c2003-04-24 01:45:04 +0000996 /* Update the count of rows that are inserted
997 */
998 if( (db->flags & SQLITE_CountRows)!=0 ){
drh6a288a32008-01-07 19:20:24 +0000999 sqlite3VdbeAddOp2(v, OP_AddImm, regRowCount, 1);
drh5974a302000-06-07 14:42:26 +00001000 }
drh1ccde152000-06-17 13:12:39 +00001001
danielk19772f886d12009-02-28 10:47:41 +00001002 if( pTrigger ){
danielk1977c3f9bad2002-05-15 08:30:12 +00001003 /* Code AFTER triggers */
danielk19772f886d12009-02-28 10:47:41 +00001004 if( sqlite3CodeRowTrigger(pParse, pTrigger, TK_INSERT, 0, TRIGGER_AFTER,
1005 pTab, newIdx, -1, onError, endOfLoop, 0, 0) ){
danielk1977f29ce552002-05-19 23:43:12 +00001006 goto insert_cleanup;
1007 }
drh1bee3d72001-10-15 00:44:35 +00001008 }
1009
drhe00ee6e2008-06-20 15:24:01 +00001010 /* The bottom of the main insertion loop, if the data source
1011 ** is a SELECT statement.
danielk1977f29ce552002-05-19 23:43:12 +00001012 */
danielk19774adee202004-05-08 08:23:19 +00001013 sqlite3VdbeResolveLabel(v, endOfLoop);
drh142e30d2002-08-28 03:00:58 +00001014 if( useTempTable ){
drhe00ee6e2008-06-20 15:24:01 +00001015 sqlite3VdbeAddOp2(v, OP_Next, srcTab, addrCont);
1016 sqlite3VdbeJumpHere(v, addrInsTop);
drh2eb95372008-06-06 15:04:36 +00001017 sqlite3VdbeAddOp1(v, OP_Close, srcTab);
drh142e30d2002-08-28 03:00:58 +00001018 }else if( pSelect ){
drhe00ee6e2008-06-20 15:24:01 +00001019 sqlite3VdbeAddOp2(v, OP_Goto, 0, addrCont);
1020 sqlite3VdbeJumpHere(v, addrInsTop);
drh6b563442001-11-07 16:48:26 +00001021 }
danielk1977c3f9bad2002-05-15 08:30:12 +00001022
danielk1977e448dc42008-01-02 11:50:51 +00001023 if( !IsVirtual(pTab) && !isView ){
danielk1977c3f9bad2002-05-15 08:30:12 +00001024 /* Close all tables opened */
drh2eb95372008-06-06 15:04:36 +00001025 sqlite3VdbeAddOp1(v, OP_Close, baseCur);
danielk1977c3f9bad2002-05-15 08:30:12 +00001026 for(idx=1, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, idx++){
drh2eb95372008-06-06 15:04:36 +00001027 sqlite3VdbeAddOp1(v, OP_Close, idx+baseCur);
danielk1977c3f9bad2002-05-15 08:30:12 +00001028 }
drhcce7d172000-05-31 15:34:51 +00001029 }
danielk1977c3f9bad2002-05-15 08:30:12 +00001030
drh0b9f50d2009-06-23 20:28:53 +00001031insert_end:
drhf3388142004-11-13 03:48:06 +00001032 /* Update the sqlite_sequence table by storing the content of the
drh0b9f50d2009-06-23 20:28:53 +00001033 ** maximum rowid counter values recorded while inserting into
1034 ** autoincrement tables.
drh2958a4e2004-11-12 03:56:15 +00001035 */
drh0b9f50d2009-06-23 20:28:53 +00001036 if( pParse->nested==0 && pParse->trigStack==0 ){
1037 sqlite3AutoincrementEnd(pParse);
1038 }
drh2958a4e2004-11-12 03:56:15 +00001039
drh1bee3d72001-10-15 00:44:35 +00001040 /*
danielk1977e7de6f22004-11-05 06:02:06 +00001041 ** Return the number of rows inserted. If this routine is
1042 ** generating code because of a call to sqlite3NestedParse(), do not
1043 ** invoke the callback function.
drh1bee3d72001-10-15 00:44:35 +00001044 */
danielk1977cc6bd382005-01-10 02:48:49 +00001045 if( db->flags & SQLITE_CountRows && pParse->nested==0 && !pParse->trigStack ){
drh6a288a32008-01-07 19:20:24 +00001046 sqlite3VdbeAddOp2(v, OP_ResultRow, regRowCount, 1);
danielk197722322fd2004-05-25 23:35:17 +00001047 sqlite3VdbeSetNumCols(v, 1);
danielk197710fb7492008-10-31 10:53:22 +00001048 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "rows inserted", SQLITE_STATIC);
drh1bee3d72001-10-15 00:44:35 +00001049 }
drhcce7d172000-05-31 15:34:51 +00001050
1051insert_cleanup:
drh633e6d52008-07-28 19:34:53 +00001052 sqlite3SrcListDelete(db, pTabList);
1053 sqlite3ExprListDelete(db, pList);
1054 sqlite3SelectDelete(db, pSelect);
1055 sqlite3IdListDelete(db, pColumn);
1056 sqlite3DbFree(db, aRegIdx);
drhcce7d172000-05-31 15:34:51 +00001057}
drh9cfcf5d2002-01-29 18:41:24 +00001058
drh9cfcf5d2002-01-29 18:41:24 +00001059/*
drh6a288a32008-01-07 19:20:24 +00001060** Generate code to do constraint checks prior to an INSERT or an UPDATE.
drh9cfcf5d2002-01-29 18:41:24 +00001061**
drh04adf412008-01-08 18:57:50 +00001062** The input is a range of consecutive registers as follows:
drh0ca3e242002-01-29 23:07:02 +00001063**
drhf0863fe2005-06-12 21:35:51 +00001064** 1. The rowid of the row to be updated before the update. This
drhb419a922002-01-30 16:17:23 +00001065** value is omitted unless we are doing an UPDATE that involves a
drha05a7222008-01-19 03:35:58 +00001066** change to the record number or writing to a virtual table.
drh0ca3e242002-01-29 23:07:02 +00001067**
drhf0863fe2005-06-12 21:35:51 +00001068** 2. The rowid of the row after the update.
drh0ca3e242002-01-29 23:07:02 +00001069**
1070** 3. The data in the first column of the entry after the update.
1071**
1072** i. Data from middle columns...
1073**
1074** N. The data in the last column of the entry after the update.
1075**
drh04adf412008-01-08 18:57:50 +00001076** The regRowid parameter is the index of the register containing (2).
1077**
drhf0863fe2005-06-12 21:35:51 +00001078** The old rowid shown as entry (1) above is omitted unless both isUpdate
1079** and rowidChng are 1. isUpdate is true for UPDATEs and false for
drha05a7222008-01-19 03:35:58 +00001080** INSERTs. RowidChng means that the new rowid is explicitly specified by
1081** the update or insert statement. If rowidChng is false, it means that
1082** the rowid is computed automatically in an insert or that the rowid value
1083** is not modified by the update.
drh0ca3e242002-01-29 23:07:02 +00001084**
drhaa9b8962008-01-08 02:57:55 +00001085** The code generated by this routine store new index entries into
1086** registers identified by aRegIdx[]. No index entry is created for
1087** indices where aRegIdx[i]==0. The order of indices in aRegIdx[] is
1088** the same as the order of indices on the linked list of indices
1089** attached to the table.
drh9cfcf5d2002-01-29 18:41:24 +00001090**
1091** This routine also generates code to check constraints. NOT NULL,
1092** CHECK, and UNIQUE constraints are all checked. If a constraint fails,
drh1c928532002-01-31 15:54:21 +00001093** then the appropriate action is performed. There are five possible
1094** actions: ROLLBACK, ABORT, FAIL, REPLACE, and IGNORE.
drh9cfcf5d2002-01-29 18:41:24 +00001095**
1096** Constraint type Action What Happens
1097** --------------- ---------- ----------------------------------------
drh1c928532002-01-31 15:54:21 +00001098** any ROLLBACK The current transaction is rolled back and
danielk197724b03fd2004-05-10 10:34:34 +00001099** sqlite3_exec() returns immediately with a
drh9cfcf5d2002-01-29 18:41:24 +00001100** return code of SQLITE_CONSTRAINT.
1101**
drh1c928532002-01-31 15:54:21 +00001102** any ABORT Back out changes from the current command
1103** only (do not do a complete rollback) then
danielk197724b03fd2004-05-10 10:34:34 +00001104** cause sqlite3_exec() to return immediately
drh1c928532002-01-31 15:54:21 +00001105** with SQLITE_CONSTRAINT.
1106**
1107** any FAIL Sqlite_exec() returns immediately with a
1108** return code of SQLITE_CONSTRAINT. The
1109** transaction is not rolled back and any
1110** prior changes are retained.
1111**
drh9cfcf5d2002-01-29 18:41:24 +00001112** any IGNORE The record number and data is popped from
1113** the stack and there is an immediate jump
1114** to label ignoreDest.
1115**
1116** NOT NULL REPLACE The NULL value is replace by the default
1117** value for that column. If the default value
1118** is NULL, the action is the same as ABORT.
1119**
1120** UNIQUE REPLACE The other row that conflicts with the row
1121** being inserted is removed.
1122**
1123** CHECK REPLACE Illegal. The results in an exception.
1124**
drh1c928532002-01-31 15:54:21 +00001125** Which action to take is determined by the overrideError parameter.
1126** Or if overrideError==OE_Default, then the pParse->onError parameter
1127** is used. Or if pParse->onError==OE_Default then the onError value
1128** for the constraint is used.
drh9cfcf5d2002-01-29 18:41:24 +00001129**
drhaaab5722002-02-19 13:39:21 +00001130** The calling routine must open a read/write cursor for pTab with
drh04adf412008-01-08 18:57:50 +00001131** cursor number "baseCur". All indices of pTab must also have open
1132** read/write cursors with cursor number baseCur+i for the i-th cursor.
drh9cfcf5d2002-01-29 18:41:24 +00001133** Except, if there is no possibility of a REPLACE action then
drhaa9b8962008-01-08 02:57:55 +00001134** cursors do not need to be open for indices where aRegIdx[i]==0.
drh9cfcf5d2002-01-29 18:41:24 +00001135*/
danielk19774adee202004-05-08 08:23:19 +00001136void sqlite3GenerateConstraintChecks(
drh9cfcf5d2002-01-29 18:41:24 +00001137 Parse *pParse, /* The parser context */
1138 Table *pTab, /* the table into which we are inserting */
drh04adf412008-01-08 18:57:50 +00001139 int baseCur, /* Index of a read/write cursor pointing at pTab */
1140 int regRowid, /* Index of the range of input registers */
drhaa9b8962008-01-08 02:57:55 +00001141 int *aRegIdx, /* Register used by each index. 0 for unused indices */
drha05a7222008-01-19 03:35:58 +00001142 int rowidChng, /* True if the rowid might collide with existing entry */
drhb419a922002-01-30 16:17:23 +00001143 int isUpdate, /* True for UPDATE, False for INSERT */
drh9cfcf5d2002-01-29 18:41:24 +00001144 int overrideError, /* Override onError to this if not OE_Default */
danielk1977de630352009-05-04 11:42:29 +00001145 int ignoreDest, /* Jump to this label on an OE_Ignore resolution */
1146 int *pbMayReplace /* OUT: Set to true if constraint may cause a replace */
drh9cfcf5d2002-01-29 18:41:24 +00001147){
drh1b7ecbb2009-05-03 01:00:59 +00001148 int i; /* loop counter */
1149 Vdbe *v; /* VDBE under constrution */
1150 int nCol; /* Number of columns */
1151 int onError; /* Conflict resolution strategy */
drh1bd10f82008-12-10 21:19:56 +00001152 int j1; /* Addresss of jump instruction */
1153 int j2 = 0, j3; /* Addresses of jump instructions */
drh04adf412008-01-08 18:57:50 +00001154 int regData; /* Register containing first data column */
drh1b7ecbb2009-05-03 01:00:59 +00001155 int iCur; /* Table cursor number */
1156 Index *pIdx; /* Pointer to one of the indices */
1157 int seenReplace = 0; /* True if REPLACE is used to resolve INT PK conflict */
drhf0863fe2005-06-12 21:35:51 +00001158 int hasTwoRowids = (isUpdate && rowidChng);
drh9cfcf5d2002-01-29 18:41:24 +00001159
danielk19774adee202004-05-08 08:23:19 +00001160 v = sqlite3GetVdbe(pParse);
drh9cfcf5d2002-01-29 18:41:24 +00001161 assert( v!=0 );
drh417be792002-03-03 18:59:40 +00001162 assert( pTab->pSelect==0 ); /* This table is not a VIEW */
drh9cfcf5d2002-01-29 18:41:24 +00001163 nCol = pTab->nCol;
drh04adf412008-01-08 18:57:50 +00001164 regData = regRowid + 1;
drh9cfcf5d2002-01-29 18:41:24 +00001165
drhaa9b8962008-01-08 02:57:55 +00001166
drh9cfcf5d2002-01-29 18:41:24 +00001167 /* Test all NOT NULL constraints.
1168 */
1169 for(i=0; i<nCol; i++){
drh0ca3e242002-01-29 23:07:02 +00001170 if( i==pTab->iPKey ){
drh0ca3e242002-01-29 23:07:02 +00001171 continue;
1172 }
drh9cfcf5d2002-01-29 18:41:24 +00001173 onError = pTab->aCol[i].notNull;
drh0ca3e242002-01-29 23:07:02 +00001174 if( onError==OE_None ) continue;
drh9cfcf5d2002-01-29 18:41:24 +00001175 if( overrideError!=OE_Default ){
1176 onError = overrideError;
drha996e472003-05-16 02:30:27 +00001177 }else if( onError==OE_Default ){
1178 onError = OE_Abort;
drh9cfcf5d2002-01-29 18:41:24 +00001179 }
danielk19777977a172004-11-09 12:44:37 +00001180 if( onError==OE_Replace && pTab->aCol[i].pDflt==0 ){
drh9cfcf5d2002-01-29 18:41:24 +00001181 onError = OE_Abort;
1182 }
danielk1977b84f96f2005-01-20 11:32:23 +00001183 assert( onError==OE_Rollback || onError==OE_Abort || onError==OE_Fail
1184 || onError==OE_Ignore || onError==OE_Replace );
drh9cfcf5d2002-01-29 18:41:24 +00001185 switch( onError ){
drh1c928532002-01-31 15:54:21 +00001186 case OE_Rollback:
1187 case OE_Abort:
1188 case OE_Fail: {
drhf089aa42008-07-08 19:34:06 +00001189 char *zMsg;
drh5053a792009-02-20 03:02:23 +00001190 j1 = sqlite3VdbeAddOp3(v, OP_HaltIfNull,
1191 SQLITE_CONSTRAINT, onError, regData+i);
drhf089aa42008-07-08 19:34:06 +00001192 zMsg = sqlite3MPrintf(pParse->db, "%s.%s may not be NULL",
1193 pTab->zName, pTab->aCol[i].zName);
drh66a51672008-01-03 00:01:23 +00001194 sqlite3VdbeChangeP4(v, -1, zMsg, P4_DYNAMIC);
drh9cfcf5d2002-01-29 18:41:24 +00001195 break;
1196 }
1197 case OE_Ignore: {
drh5053a792009-02-20 03:02:23 +00001198 sqlite3VdbeAddOp2(v, OP_IsNull, regData+i, ignoreDest);
drh9cfcf5d2002-01-29 18:41:24 +00001199 break;
1200 }
drh098d1682009-05-02 15:46:46 +00001201 default: {
1202 assert( onError==OE_Replace );
drh5053a792009-02-20 03:02:23 +00001203 j1 = sqlite3VdbeAddOp1(v, OP_NotNull, regData+i);
drh04adf412008-01-08 18:57:50 +00001204 sqlite3ExprCode(pParse, pTab->aCol[i].pDflt, regData+i);
drh5053a792009-02-20 03:02:23 +00001205 sqlite3VdbeJumpHere(v, j1);
drh9cfcf5d2002-01-29 18:41:24 +00001206 break;
1207 }
drh9cfcf5d2002-01-29 18:41:24 +00001208 }
1209 }
1210
1211 /* Test all CHECK constraints
1212 */
drhffe07b22005-11-03 00:41:17 +00001213#ifndef SQLITE_OMIT_CHECK
drh0cd2d4c2005-11-03 02:15:02 +00001214 if( pTab->pCheck && (pParse->db->flags & SQLITE_IgnoreChecks)==0 ){
drhffe07b22005-11-03 00:41:17 +00001215 int allOk = sqlite3VdbeMakeLabel(v);
drhaa9b8962008-01-08 02:57:55 +00001216 pParse->ckBase = regData;
drh35573352008-01-08 23:54:25 +00001217 sqlite3ExprIfTrue(pParse, pTab->pCheck, allOk, SQLITE_JUMPIFNULL);
drhaa01c7e2006-03-15 16:26:10 +00001218 onError = overrideError!=OE_Default ? overrideError : OE_Abort;
drh2e06c672007-07-23 19:39:46 +00001219 if( onError==OE_Ignore ){
drh66a51672008-01-03 00:01:23 +00001220 sqlite3VdbeAddOp2(v, OP_Goto, 0, ignoreDest);
drhaa01c7e2006-03-15 16:26:10 +00001221 }else{
drh66a51672008-01-03 00:01:23 +00001222 sqlite3VdbeAddOp2(v, OP_Halt, SQLITE_CONSTRAINT, onError);
drhaa01c7e2006-03-15 16:26:10 +00001223 }
drhffe07b22005-11-03 00:41:17 +00001224 sqlite3VdbeResolveLabel(v, allOk);
1225 }
1226#endif /* !defined(SQLITE_OMIT_CHECK) */
drh9cfcf5d2002-01-29 18:41:24 +00001227
drh0bd1f4e2002-06-06 18:54:39 +00001228 /* If we have an INTEGER PRIMARY KEY, make sure the primary key
1229 ** of the new record does not previously exist. Except, if this
1230 ** is an UPDATE and the primary key is not changing, that is OK.
drh9cfcf5d2002-01-29 18:41:24 +00001231 */
drhf0863fe2005-06-12 21:35:51 +00001232 if( rowidChng ){
drh0ca3e242002-01-29 23:07:02 +00001233 onError = pTab->keyConf;
1234 if( overrideError!=OE_Default ){
1235 onError = overrideError;
drha996e472003-05-16 02:30:27 +00001236 }else if( onError==OE_Default ){
1237 onError = OE_Abort;
drh0ca3e242002-01-29 23:07:02 +00001238 }
drha0217ba2003-06-01 01:10:33 +00001239
drh60a713c2008-01-21 16:22:45 +00001240 if( onError!=OE_Replace || pTab->pIndex ){
drha05a7222008-01-19 03:35:58 +00001241 if( isUpdate ){
1242 j2 = sqlite3VdbeAddOp3(v, OP_Eq, regRowid, 0, regRowid-1);
drh79b0c952002-05-21 12:56:43 +00001243 }
drha05a7222008-01-19 03:35:58 +00001244 j3 = sqlite3VdbeAddOp3(v, OP_NotExists, baseCur, 0, regRowid);
1245 switch( onError ){
1246 default: {
1247 onError = OE_Abort;
1248 /* Fall thru into the next case */
drh5383ae52003-06-04 12:23:30 +00001249 }
drha05a7222008-01-19 03:35:58 +00001250 case OE_Rollback:
1251 case OE_Abort:
1252 case OE_Fail: {
1253 sqlite3VdbeAddOp4(v, OP_Halt, SQLITE_CONSTRAINT, onError, 0,
1254 "PRIMARY KEY must be unique", P4_STATIC);
1255 break;
1256 }
1257 case OE_Replace: {
1258 sqlite3GenerateRowIndexDelete(pParse, pTab, baseCur, 0);
1259 seenReplace = 1;
1260 break;
1261 }
1262 case OE_Ignore: {
1263 assert( seenReplace==0 );
1264 sqlite3VdbeAddOp2(v, OP_Goto, 0, ignoreDest);
1265 break;
1266 }
drh0ca3e242002-01-29 23:07:02 +00001267 }
drha05a7222008-01-19 03:35:58 +00001268 sqlite3VdbeJumpHere(v, j3);
1269 if( isUpdate ){
1270 sqlite3VdbeJumpHere(v, j2);
drh5383ae52003-06-04 12:23:30 +00001271 }
1272 }
drh0ca3e242002-01-29 23:07:02 +00001273 }
drh0bd1f4e2002-06-06 18:54:39 +00001274
1275 /* Test all UNIQUE constraints by creating entries for each UNIQUE
1276 ** index and making sure that duplicate entries do not already exist.
1277 ** Add the new records to the indices as we go.
1278 */
drhb2fe7d82003-04-20 17:29:23 +00001279 for(iCur=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, iCur++){
drh2d401ab2008-01-10 23:50:11 +00001280 int regIdx;
1281 int regR;
1282
drhaa9b8962008-01-08 02:57:55 +00001283 if( aRegIdx[iCur]==0 ) continue; /* Skip unused indices */
drhb2fe7d82003-04-20 17:29:23 +00001284
1285 /* Create a key for accessing the index entry */
drh2d401ab2008-01-10 23:50:11 +00001286 regIdx = sqlite3GetTempRange(pParse, pIdx->nColumn+1);
drh9cfcf5d2002-01-29 18:41:24 +00001287 for(i=0; i<pIdx->nColumn; i++){
1288 int idx = pIdx->aiColumn[i];
1289 if( idx==pTab->iPKey ){
drh2d401ab2008-01-10 23:50:11 +00001290 sqlite3VdbeAddOp2(v, OP_SCopy, regRowid, regIdx+i);
drh9cfcf5d2002-01-29 18:41:24 +00001291 }else{
drh2d401ab2008-01-10 23:50:11 +00001292 sqlite3VdbeAddOp2(v, OP_SCopy, regData+idx, regIdx+i);
drh9cfcf5d2002-01-29 18:41:24 +00001293 }
1294 }
drh2d401ab2008-01-10 23:50:11 +00001295 sqlite3VdbeAddOp2(v, OP_SCopy, regRowid, regIdx+i);
drh1db639c2008-01-17 02:36:28 +00001296 sqlite3VdbeAddOp3(v, OP_MakeRecord, regIdx, pIdx->nColumn+1, aRegIdx[iCur]);
danielk1977a37cdde2004-05-16 11:15:36 +00001297 sqlite3IndexAffinityStr(v, pIdx);
drhda250ea2008-04-01 05:07:14 +00001298 sqlite3ExprCacheAffinityChange(pParse, regIdx, pIdx->nColumn+1);
drhb2fe7d82003-04-20 17:29:23 +00001299
1300 /* Find out what action to take in case there is an indexing conflict */
drh9cfcf5d2002-01-29 18:41:24 +00001301 onError = pIdx->onError;
danielk1977de630352009-05-04 11:42:29 +00001302 if( onError==OE_None ){
1303 sqlite3ReleaseTempRange(pParse, regIdx, pIdx->nColumn+1);
1304 continue; /* pIdx is not a UNIQUE index */
1305 }
drh9cfcf5d2002-01-29 18:41:24 +00001306 if( overrideError!=OE_Default ){
1307 onError = overrideError;
drha996e472003-05-16 02:30:27 +00001308 }else if( onError==OE_Default ){
1309 onError = OE_Abort;
drh9cfcf5d2002-01-29 18:41:24 +00001310 }
drh5383ae52003-06-04 12:23:30 +00001311 if( seenReplace ){
1312 if( onError==OE_Ignore ) onError = OE_Replace;
1313 else if( onError==OE_Fail ) onError = OE_Abort;
1314 }
1315
drhb2fe7d82003-04-20 17:29:23 +00001316
1317 /* Check to see if the new index entry will be unique */
drh2d401ab2008-01-10 23:50:11 +00001318 regR = sqlite3GetTempReg(pParse);
1319 sqlite3VdbeAddOp2(v, OP_SCopy, regRowid-hasTwoRowids, regR);
1320 j3 = sqlite3VdbeAddOp4(v, OP_IsUnique, baseCur+iCur+1, 0,
danielk1977de630352009-05-04 11:42:29 +00001321 regR, SQLITE_INT_TO_PTR(regIdx),
mlcreecha9e852b2008-03-06 09:58:50 +00001322 P4_INT32);
danielk1977de630352009-05-04 11:42:29 +00001323 sqlite3ReleaseTempRange(pParse, regIdx, pIdx->nColumn+1);
drhb2fe7d82003-04-20 17:29:23 +00001324
1325 /* Generate code that executes if the new index entry is not unique */
danielk1977b84f96f2005-01-20 11:32:23 +00001326 assert( onError==OE_Rollback || onError==OE_Abort || onError==OE_Fail
1327 || onError==OE_Ignore || onError==OE_Replace );
drh9cfcf5d2002-01-29 18:41:24 +00001328 switch( onError ){
drh1c928532002-01-31 15:54:21 +00001329 case OE_Rollback:
1330 case OE_Abort:
1331 case OE_Fail: {
drh098d1682009-05-02 15:46:46 +00001332 int j;
1333 StrAccum errMsg;
1334 const char *zSep;
1335 char *zErr;
1336
1337 sqlite3StrAccumInit(&errMsg, 0, 0, 200);
1338 errMsg.db = pParse->db;
1339 zSep = pIdx->nColumn>1 ? "columns " : "column ";
1340 for(j=0; j<pIdx->nColumn; j++){
drh37ed48e2003-08-05 13:13:38 +00001341 char *zCol = pTab->aCol[pIdx->aiColumn[j]].zName;
drh098d1682009-05-02 15:46:46 +00001342 sqlite3StrAccumAppend(&errMsg, zSep, -1);
1343 zSep = ", ";
1344 sqlite3StrAccumAppend(&errMsg, zCol, -1);
drh37ed48e2003-08-05 13:13:38 +00001345 }
drh098d1682009-05-02 15:46:46 +00001346 sqlite3StrAccumAppend(&errMsg,
1347 pIdx->nColumn>1 ? " are not unique" : " is not unique", -1);
1348 zErr = sqlite3StrAccumFinish(&errMsg);
1349 sqlite3VdbeAddOp4(v, OP_Halt, SQLITE_CONSTRAINT, onError, 0, zErr, 0);
1350 sqlite3DbFree(errMsg.db, zErr);
drh9cfcf5d2002-01-29 18:41:24 +00001351 break;
1352 }
1353 case OE_Ignore: {
drh0ca3e242002-01-29 23:07:02 +00001354 assert( seenReplace==0 );
drh66a51672008-01-03 00:01:23 +00001355 sqlite3VdbeAddOp2(v, OP_Goto, 0, ignoreDest);
drh9cfcf5d2002-01-29 18:41:24 +00001356 break;
1357 }
drh098d1682009-05-02 15:46:46 +00001358 default: {
1359 assert( onError==OE_Replace );
drh2d401ab2008-01-10 23:50:11 +00001360 sqlite3GenerateRowDelete(pParse, pTab, baseCur, regR, 0);
drh0ca3e242002-01-29 23:07:02 +00001361 seenReplace = 1;
drh9cfcf5d2002-01-29 18:41:24 +00001362 break;
1363 }
drh9cfcf5d2002-01-29 18:41:24 +00001364 }
drh2d401ab2008-01-10 23:50:11 +00001365 sqlite3VdbeJumpHere(v, j3);
1366 sqlite3ReleaseTempReg(pParse, regR);
drh9cfcf5d2002-01-29 18:41:24 +00001367 }
danielk1977de630352009-05-04 11:42:29 +00001368
1369 if( pbMayReplace ){
1370 *pbMayReplace = seenReplace;
1371 }
drh9cfcf5d2002-01-29 18:41:24 +00001372}
drh0ca3e242002-01-29 23:07:02 +00001373
1374/*
1375** This routine generates code to finish the INSERT or UPDATE operation
danielk19774adee202004-05-08 08:23:19 +00001376** that was started by a prior call to sqlite3GenerateConstraintChecks.
drh04adf412008-01-08 18:57:50 +00001377** A consecutive range of registers starting at regRowid contains the
1378** rowid and the content to be inserted.
drh0ca3e242002-01-29 23:07:02 +00001379**
drhb419a922002-01-30 16:17:23 +00001380** The arguments to this routine should be the same as the first six
danielk19774adee202004-05-08 08:23:19 +00001381** arguments to sqlite3GenerateConstraintChecks.
drh0ca3e242002-01-29 23:07:02 +00001382*/
danielk19774adee202004-05-08 08:23:19 +00001383void sqlite3CompleteInsertion(
drh0ca3e242002-01-29 23:07:02 +00001384 Parse *pParse, /* The parser context */
1385 Table *pTab, /* the table into which we are inserting */
drh04adf412008-01-08 18:57:50 +00001386 int baseCur, /* Index of a read/write cursor pointing at pTab */
1387 int regRowid, /* Range of content */
drhaa9b8962008-01-08 02:57:55 +00001388 int *aRegIdx, /* Register used by each index. 0 for unused indices */
drh70ce3f02003-04-15 19:22:22 +00001389 int isUpdate, /* True for UPDATE, False for INSERT */
drhe4d90812007-03-29 05:51:49 +00001390 int newIdx, /* Index of NEW table for triggers. -1 if none */
danielk1977de630352009-05-04 11:42:29 +00001391 int appendBias, /* True if this is likely to be an append */
1392 int useSeekResult /* True to set the USESEEKRESULT flag on OP_[Idx]Insert */
drh0ca3e242002-01-29 23:07:02 +00001393){
1394 int i;
1395 Vdbe *v;
1396 int nIdx;
1397 Index *pIdx;
drh1bd10f82008-12-10 21:19:56 +00001398 u8 pik_flags;
drh04adf412008-01-08 18:57:50 +00001399 int regData;
drhb7654112008-01-12 12:48:07 +00001400 int regRec;
drh0ca3e242002-01-29 23:07:02 +00001401
danielk19774adee202004-05-08 08:23:19 +00001402 v = sqlite3GetVdbe(pParse);
drh0ca3e242002-01-29 23:07:02 +00001403 assert( v!=0 );
drh417be792002-03-03 18:59:40 +00001404 assert( pTab->pSelect==0 ); /* This table is not a VIEW */
drh0ca3e242002-01-29 23:07:02 +00001405 for(nIdx=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, nIdx++){}
1406 for(i=nIdx-1; i>=0; i--){
drhaa9b8962008-01-08 02:57:55 +00001407 if( aRegIdx[i]==0 ) continue;
drh04adf412008-01-08 18:57:50 +00001408 sqlite3VdbeAddOp2(v, OP_IdxInsert, baseCur+i+1, aRegIdx[i]);
danielk1977de630352009-05-04 11:42:29 +00001409 if( useSeekResult ){
1410 sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT);
1411 }
drh0ca3e242002-01-29 23:07:02 +00001412 }
drh04adf412008-01-08 18:57:50 +00001413 regData = regRowid + 1;
drhb7654112008-01-12 12:48:07 +00001414 regRec = sqlite3GetTempReg(pParse);
drh1db639c2008-01-17 02:36:28 +00001415 sqlite3VdbeAddOp3(v, OP_MakeRecord, regData, pTab->nCol, regRec);
danielk1977a37cdde2004-05-16 11:15:36 +00001416 sqlite3TableAffinityStr(v, pTab);
drhda250ea2008-04-01 05:07:14 +00001417 sqlite3ExprCacheAffinityChange(pParse, regData, pTab->nCol);
danielk1977b84f96f2005-01-20 11:32:23 +00001418#ifndef SQLITE_OMIT_TRIGGER
drh70ce3f02003-04-15 19:22:22 +00001419 if( newIdx>=0 ){
drhb7654112008-01-12 12:48:07 +00001420 sqlite3VdbeAddOp3(v, OP_Insert, newIdx, regRec, regRowid);
drh70ce3f02003-04-15 19:22:22 +00001421 }
danielk1977b84f96f2005-01-20 11:32:23 +00001422#endif
drh4794f732004-11-05 17:17:50 +00001423 if( pParse->nested ){
1424 pik_flags = 0;
1425 }else{
danielk197794eb6a12005-12-15 15:22:08 +00001426 pik_flags = OPFLAG_NCHANGE;
1427 pik_flags |= (isUpdate?OPFLAG_ISUPDATE:OPFLAG_LASTROWID);
drh4794f732004-11-05 17:17:50 +00001428 }
drhe4d90812007-03-29 05:51:49 +00001429 if( appendBias ){
1430 pik_flags |= OPFLAG_APPEND;
1431 }
danielk1977de630352009-05-04 11:42:29 +00001432 if( useSeekResult ){
1433 pik_flags |= OPFLAG_USESEEKRESULT;
1434 }
drhb7654112008-01-12 12:48:07 +00001435 sqlite3VdbeAddOp3(v, OP_Insert, baseCur, regRec, regRowid);
danielk197794eb6a12005-12-15 15:22:08 +00001436 if( !pParse->nested ){
drh66a51672008-01-03 00:01:23 +00001437 sqlite3VdbeChangeP4(v, -1, pTab->zName, P4_STATIC);
danielk197794eb6a12005-12-15 15:22:08 +00001438 }
drhb7654112008-01-12 12:48:07 +00001439 sqlite3VdbeChangeP5(v, pik_flags);
drh0ca3e242002-01-29 23:07:02 +00001440}
drhcd446902004-02-24 01:05:31 +00001441
1442/*
drh290c1942004-08-21 17:54:45 +00001443** Generate code that will open cursors for a table and for all
drh04adf412008-01-08 18:57:50 +00001444** indices of that table. The "baseCur" parameter is the cursor number used
drhcd446902004-02-24 01:05:31 +00001445** for the table. Indices are opened on subsequent cursors.
drhaa9b8962008-01-08 02:57:55 +00001446**
1447** Return the number of indices on the table.
drhcd446902004-02-24 01:05:31 +00001448*/
drhaa9b8962008-01-08 02:57:55 +00001449int sqlite3OpenTableAndIndices(
drh290c1942004-08-21 17:54:45 +00001450 Parse *pParse, /* Parsing context */
1451 Table *pTab, /* Table to be opened */
drhdfe88ec2008-11-03 20:55:06 +00001452 int baseCur, /* Cursor number assigned to the table */
drh290c1942004-08-21 17:54:45 +00001453 int op /* OP_OpenRead or OP_OpenWrite */
1454){
drhcd446902004-02-24 01:05:31 +00001455 int i;
drh4cbdda92006-06-14 19:00:20 +00001456 int iDb;
drhcd446902004-02-24 01:05:31 +00001457 Index *pIdx;
drh4cbdda92006-06-14 19:00:20 +00001458 Vdbe *v;
1459
drhaa9b8962008-01-08 02:57:55 +00001460 if( IsVirtual(pTab) ) return 0;
drh4cbdda92006-06-14 19:00:20 +00001461 iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
1462 v = sqlite3GetVdbe(pParse);
drhcd446902004-02-24 01:05:31 +00001463 assert( v!=0 );
drh04adf412008-01-08 18:57:50 +00001464 sqlite3OpenTable(pParse, baseCur, iDb, pTab, op);
drhcd446902004-02-24 01:05:31 +00001465 for(i=1, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){
danielk1977b3bf5562006-01-10 17:58:23 +00001466 KeyInfo *pKey = sqlite3IndexKeyinfo(pParse, pIdx);
danielk1977da184232006-01-05 11:34:32 +00001467 assert( pIdx->pSchema==pTab->pSchema );
drh04adf412008-01-08 18:57:50 +00001468 sqlite3VdbeAddOp4(v, op, i+baseCur, pIdx->tnum, iDb,
drh66a51672008-01-03 00:01:23 +00001469 (char*)pKey, P4_KEYINFO_HANDOFF);
danielk1977207872a2008-01-03 07:54:23 +00001470 VdbeComment((v, "%s", pIdx->zName));
drhcd446902004-02-24 01:05:31 +00001471 }
drh1b7ecbb2009-05-03 01:00:59 +00001472 if( pParse->nTab<baseCur+i ){
drh04adf412008-01-08 18:57:50 +00001473 pParse->nTab = baseCur+i;
drh290c1942004-08-21 17:54:45 +00001474 }
drhaa9b8962008-01-08 02:57:55 +00001475 return i-1;
drhcd446902004-02-24 01:05:31 +00001476}
drh9d9cf222007-02-13 15:01:11 +00001477
drh91c58e22007-03-27 12:04:04 +00001478
1479#ifdef SQLITE_TEST
1480/*
1481** The following global variable is incremented whenever the
1482** transfer optimization is used. This is used for testing
1483** purposes only - to make sure the transfer optimization really
1484** is happening when it is suppose to.
1485*/
1486int sqlite3_xferopt_count;
1487#endif /* SQLITE_TEST */
1488
1489
drh9d9cf222007-02-13 15:01:11 +00001490#ifndef SQLITE_OMIT_XFER_OPT
1491/*
1492** Check to collation names to see if they are compatible.
1493*/
1494static int xferCompatibleCollation(const char *z1, const char *z2){
1495 if( z1==0 ){
1496 return z2==0;
1497 }
1498 if( z2==0 ){
1499 return 0;
1500 }
1501 return sqlite3StrICmp(z1, z2)==0;
1502}
1503
1504
1505/*
1506** Check to see if index pSrc is compatible as a source of data
1507** for index pDest in an insert transfer optimization. The rules
1508** for a compatible index:
1509**
1510** * The index is over the same set of columns
1511** * The same DESC and ASC markings occurs on all columns
1512** * The same onError processing (OE_Abort, OE_Ignore, etc)
1513** * The same collating sequence on each column
1514*/
1515static int xferCompatibleIndex(Index *pDest, Index *pSrc){
1516 int i;
1517 assert( pDest && pSrc );
1518 assert( pDest->pTable!=pSrc->pTable );
1519 if( pDest->nColumn!=pSrc->nColumn ){
1520 return 0; /* Different number of columns */
1521 }
1522 if( pDest->onError!=pSrc->onError ){
1523 return 0; /* Different conflict resolution strategies */
1524 }
1525 for(i=0; i<pSrc->nColumn; i++){
1526 if( pSrc->aiColumn[i]!=pDest->aiColumn[i] ){
1527 return 0; /* Different columns indexed */
1528 }
1529 if( pSrc->aSortOrder[i]!=pDest->aSortOrder[i] ){
1530 return 0; /* Different sort orders */
1531 }
drh3f6e7812009-05-02 00:28:19 +00001532 if( !xferCompatibleCollation(pSrc->azColl[i],pDest->azColl[i]) ){
drh60a713c2008-01-21 16:22:45 +00001533 return 0; /* Different collating sequences */
drh9d9cf222007-02-13 15:01:11 +00001534 }
1535 }
1536
1537 /* If no test above fails then the indices must be compatible */
1538 return 1;
1539}
1540
1541/*
1542** Attempt the transfer optimization on INSERTs of the form
1543**
1544** INSERT INTO tab1 SELECT * FROM tab2;
1545**
1546** This optimization is only attempted if
1547**
1548** (1) tab1 and tab2 have identical schemas including all the
drh8103b7d2007-02-24 13:23:51 +00001549** same indices and constraints
drh9d9cf222007-02-13 15:01:11 +00001550**
1551** (2) tab1 and tab2 are different tables
1552**
1553** (3) There must be no triggers on tab1
1554**
1555** (4) The result set of the SELECT statement is "*"
1556**
1557** (5) The SELECT statement has no WHERE, HAVING, ORDER BY, GROUP BY,
1558** or LIMIT clause.
1559**
1560** (6) The SELECT statement is a simple (not a compound) select that
1561** contains only tab2 in its FROM clause
1562**
1563** This method for implementing the INSERT transfers raw records from
1564** tab2 over to tab1. The columns are not decoded. Raw records from
1565** the indices of tab2 are transfered to tab1 as well. In so doing,
1566** the resulting tab1 has much less fragmentation.
1567**
1568** This routine returns TRUE if the optimization is attempted. If any
1569** of the conditions above fail so that the optimization should not
1570** be attempted, then this routine returns FALSE.
1571*/
1572static int xferOptimization(
1573 Parse *pParse, /* Parser context */
1574 Table *pDest, /* The table we are inserting into */
1575 Select *pSelect, /* A SELECT statement to use as the data source */
1576 int onError, /* How to handle constraint errors */
1577 int iDbDest /* The database of pDest */
1578){
1579 ExprList *pEList; /* The result set of the SELECT */
1580 Table *pSrc; /* The table in the FROM clause of SELECT */
1581 Index *pSrcIdx, *pDestIdx; /* Source and destination indices */
1582 struct SrcList_item *pItem; /* An element of pSelect->pSrc */
1583 int i; /* Loop counter */
1584 int iDbSrc; /* The database of pSrc */
1585 int iSrc, iDest; /* Cursors from source and destination */
1586 int addr1, addr2; /* Loop addresses */
1587 int emptyDestTest; /* Address of test for empty pDest */
1588 int emptySrcTest; /* Address of test for empty pSrc */
drh9d9cf222007-02-13 15:01:11 +00001589 Vdbe *v; /* The VDBE we are building */
1590 KeyInfo *pKey; /* Key information for an index */
drh6a288a32008-01-07 19:20:24 +00001591 int regAutoinc; /* Memory register used by AUTOINC */
drhf33c9fa2007-04-10 18:17:55 +00001592 int destHasUniqueIdx = 0; /* True if pDest has a UNIQUE index */
drhb7654112008-01-12 12:48:07 +00001593 int regData, regRowid; /* Registers holding data and rowid */
drh9d9cf222007-02-13 15:01:11 +00001594
1595 if( pSelect==0 ){
1596 return 0; /* Must be of the form INSERT INTO ... SELECT ... */
1597 }
danielk19772f886d12009-02-28 10:47:41 +00001598 if( sqlite3TriggerList(pParse, pDest) ){
drh9d9cf222007-02-13 15:01:11 +00001599 return 0; /* tab1 must not have triggers */
1600 }
1601#ifndef SQLITE_OMIT_VIRTUALTABLE
drh7d10d5a2008-08-20 16:35:10 +00001602 if( pDest->tabFlags & TF_Virtual ){
drh9d9cf222007-02-13 15:01:11 +00001603 return 0; /* tab1 must not be a virtual table */
1604 }
1605#endif
1606 if( onError==OE_Default ){
1607 onError = OE_Abort;
1608 }
1609 if( onError!=OE_Abort && onError!=OE_Rollback ){
1610 return 0; /* Cannot do OR REPLACE or OR IGNORE or OR FAIL */
1611 }
danielk19775ce240a2007-09-03 17:30:06 +00001612 assert(pSelect->pSrc); /* allocated even if there is no FROM clause */
drh9d9cf222007-02-13 15:01:11 +00001613 if( pSelect->pSrc->nSrc!=1 ){
1614 return 0; /* FROM clause must have exactly one term */
1615 }
1616 if( pSelect->pSrc->a[0].pSelect ){
1617 return 0; /* FROM clause cannot contain a subquery */
1618 }
1619 if( pSelect->pWhere ){
1620 return 0; /* SELECT may not have a WHERE clause */
1621 }
1622 if( pSelect->pOrderBy ){
1623 return 0; /* SELECT may not have an ORDER BY clause */
1624 }
drh8103b7d2007-02-24 13:23:51 +00001625 /* Do not need to test for a HAVING clause. If HAVING is present but
1626 ** there is no ORDER BY, we will get an error. */
drh9d9cf222007-02-13 15:01:11 +00001627 if( pSelect->pGroupBy ){
1628 return 0; /* SELECT may not have a GROUP BY clause */
1629 }
1630 if( pSelect->pLimit ){
1631 return 0; /* SELECT may not have a LIMIT clause */
1632 }
drh8103b7d2007-02-24 13:23:51 +00001633 assert( pSelect->pOffset==0 ); /* Must be so if pLimit==0 */
drh9d9cf222007-02-13 15:01:11 +00001634 if( pSelect->pPrior ){
1635 return 0; /* SELECT may not be a compound query */
1636 }
drh7d10d5a2008-08-20 16:35:10 +00001637 if( pSelect->selFlags & SF_Distinct ){
drh9d9cf222007-02-13 15:01:11 +00001638 return 0; /* SELECT may not be DISTINCT */
1639 }
1640 pEList = pSelect->pEList;
1641 assert( pEList!=0 );
1642 if( pEList->nExpr!=1 ){
1643 return 0; /* The result set must have exactly one column */
1644 }
1645 assert( pEList->a[0].pExpr );
1646 if( pEList->a[0].pExpr->op!=TK_ALL ){
1647 return 0; /* The result set must be the special operator "*" */
1648 }
1649
1650 /* At this point we have established that the statement is of the
1651 ** correct syntactic form to participate in this optimization. Now
1652 ** we have to check the semantics.
1653 */
1654 pItem = pSelect->pSrc->a;
drhca424112008-01-25 15:04:48 +00001655 pSrc = sqlite3LocateTable(pParse, 0, pItem->zName, pItem->zDatabase);
drh9d9cf222007-02-13 15:01:11 +00001656 if( pSrc==0 ){
1657 return 0; /* FROM clause does not contain a real table */
1658 }
1659 if( pSrc==pDest ){
1660 return 0; /* tab1 and tab2 may not be the same table */
1661 }
1662#ifndef SQLITE_OMIT_VIRTUALTABLE
drh7d10d5a2008-08-20 16:35:10 +00001663 if( pSrc->tabFlags & TF_Virtual ){
drh9d9cf222007-02-13 15:01:11 +00001664 return 0; /* tab2 must not be a virtual table */
1665 }
1666#endif
1667 if( pSrc->pSelect ){
1668 return 0; /* tab2 may not be a view */
1669 }
1670 if( pDest->nCol!=pSrc->nCol ){
1671 return 0; /* Number of columns must be the same in tab1 and tab2 */
1672 }
1673 if( pDest->iPKey!=pSrc->iPKey ){
1674 return 0; /* Both tables must have the same INTEGER PRIMARY KEY */
1675 }
1676 for(i=0; i<pDest->nCol; i++){
1677 if( pDest->aCol[i].affinity!=pSrc->aCol[i].affinity ){
1678 return 0; /* Affinity must be the same on all columns */
1679 }
1680 if( !xferCompatibleCollation(pDest->aCol[i].zColl, pSrc->aCol[i].zColl) ){
1681 return 0; /* Collating sequence must be the same on all columns */
1682 }
1683 if( pDest->aCol[i].notNull && !pSrc->aCol[i].notNull ){
1684 return 0; /* tab2 must be NOT NULL if tab1 is */
1685 }
1686 }
1687 for(pDestIdx=pDest->pIndex; pDestIdx; pDestIdx=pDestIdx->pNext){
drhf33c9fa2007-04-10 18:17:55 +00001688 if( pDestIdx->onError!=OE_None ){
1689 destHasUniqueIdx = 1;
1690 }
drh9d9cf222007-02-13 15:01:11 +00001691 for(pSrcIdx=pSrc->pIndex; pSrcIdx; pSrcIdx=pSrcIdx->pNext){
1692 if( xferCompatibleIndex(pDestIdx, pSrcIdx) ) break;
1693 }
1694 if( pSrcIdx==0 ){
1695 return 0; /* pDestIdx has no corresponding index in pSrc */
1696 }
1697 }
drh7fc2f412007-03-29 00:08:24 +00001698#ifndef SQLITE_OMIT_CHECK
drhfb658de2007-02-24 15:18:49 +00001699 if( pDest->pCheck && !sqlite3ExprCompare(pSrc->pCheck, pDest->pCheck) ){
drh8103b7d2007-02-24 13:23:51 +00001700 return 0; /* Tables have different CHECK constraints. Ticket #2252 */
1701 }
drh7fc2f412007-03-29 00:08:24 +00001702#endif
drh9d9cf222007-02-13 15:01:11 +00001703
1704 /* If we get this far, it means either:
1705 **
1706 ** * We can always do the transfer if the table contains an
1707 ** an integer primary key
1708 **
1709 ** * We can conditionally do the transfer if the destination
1710 ** table is empty.
1711 */
drhdd735212007-02-24 13:53:05 +00001712#ifdef SQLITE_TEST
1713 sqlite3_xferopt_count++;
1714#endif
drh9d9cf222007-02-13 15:01:11 +00001715 iDbSrc = sqlite3SchemaToIndex(pParse->db, pSrc->pSchema);
1716 v = sqlite3GetVdbe(pParse);
drhf53e9b52007-08-29 13:45:58 +00001717 sqlite3CodeVerifySchema(pParse, iDbSrc);
drh9d9cf222007-02-13 15:01:11 +00001718 iSrc = pParse->nTab++;
1719 iDest = pParse->nTab++;
drh6a288a32008-01-07 19:20:24 +00001720 regAutoinc = autoIncBegin(pParse, iDbDest, pDest);
drh9d9cf222007-02-13 15:01:11 +00001721 sqlite3OpenTable(pParse, iDest, iDbDest, pDest, OP_OpenWrite);
drhf33c9fa2007-04-10 18:17:55 +00001722 if( (pDest->iPKey<0 && pDest->pIndex!=0) || destHasUniqueIdx ){
drhbd36ba62007-03-31 13:00:26 +00001723 /* If tables do not have an INTEGER PRIMARY KEY and there
1724 ** are indices to be copied and the destination is not empty,
1725 ** we have to disallow the transfer optimization because the
1726 ** the rowids might change which will mess up indexing.
drhf33c9fa2007-04-10 18:17:55 +00001727 **
1728 ** Or if the destination has a UNIQUE index and is not empty,
1729 ** we also disallow the transfer optimization because we cannot
1730 ** insure that all entries in the union of DEST and SRC will be
1731 ** unique.
drh9d9cf222007-02-13 15:01:11 +00001732 */
drh66a51672008-01-03 00:01:23 +00001733 addr1 = sqlite3VdbeAddOp2(v, OP_Rewind, iDest, 0);
1734 emptyDestTest = sqlite3VdbeAddOp2(v, OP_Goto, 0, 0);
drh9d9cf222007-02-13 15:01:11 +00001735 sqlite3VdbeJumpHere(v, addr1);
1736 }else{
1737 emptyDestTest = 0;
1738 }
1739 sqlite3OpenTable(pParse, iSrc, iDbSrc, pSrc, OP_OpenRead);
drh66a51672008-01-03 00:01:23 +00001740 emptySrcTest = sqlite3VdbeAddOp2(v, OP_Rewind, iSrc, 0);
drhb7654112008-01-12 12:48:07 +00001741 regData = sqlite3GetTempReg(pParse);
1742 regRowid = sqlite3GetTempReg(pParse);
drh42242de2007-03-29 13:35:35 +00001743 if( pDest->iPKey>=0 ){
drhb7654112008-01-12 12:48:07 +00001744 addr1 = sqlite3VdbeAddOp2(v, OP_Rowid, iSrc, regRowid);
1745 addr2 = sqlite3VdbeAddOp3(v, OP_NotExists, iDest, 0, regRowid);
drh66a51672008-01-03 00:01:23 +00001746 sqlite3VdbeAddOp4(v, OP_Halt, SQLITE_CONSTRAINT, onError, 0,
1747 "PRIMARY KEY must be unique", P4_STATIC);
drh95bad4c2007-03-28 18:04:10 +00001748 sqlite3VdbeJumpHere(v, addr2);
drhb7654112008-01-12 12:48:07 +00001749 autoIncStep(pParse, regAutoinc, regRowid);
drhbd36ba62007-03-31 13:00:26 +00001750 }else if( pDest->pIndex==0 ){
drhb7654112008-01-12 12:48:07 +00001751 addr1 = sqlite3VdbeAddOp2(v, OP_NewRowid, iDest, regRowid);
drh95bad4c2007-03-28 18:04:10 +00001752 }else{
drhb7654112008-01-12 12:48:07 +00001753 addr1 = sqlite3VdbeAddOp2(v, OP_Rowid, iSrc, regRowid);
drh7d10d5a2008-08-20 16:35:10 +00001754 assert( (pDest->tabFlags & TF_Autoincrement)==0 );
drh95bad4c2007-03-28 18:04:10 +00001755 }
drhb7654112008-01-12 12:48:07 +00001756 sqlite3VdbeAddOp2(v, OP_RowData, iSrc, regData);
1757 sqlite3VdbeAddOp3(v, OP_Insert, iDest, regData, regRowid);
1758 sqlite3VdbeChangeP5(v, OPFLAG_NCHANGE|OPFLAG_LASTROWID|OPFLAG_APPEND);
danielk19771f4aa332008-01-03 09:51:55 +00001759 sqlite3VdbeChangeP4(v, -1, pDest->zName, 0);
drh66a51672008-01-03 00:01:23 +00001760 sqlite3VdbeAddOp2(v, OP_Next, iSrc, addr1);
drh9d9cf222007-02-13 15:01:11 +00001761 for(pDestIdx=pDest->pIndex; pDestIdx; pDestIdx=pDestIdx->pNext){
drh1b7ecbb2009-05-03 01:00:59 +00001762 for(pSrcIdx=pSrc->pIndex; ALWAYS(pSrcIdx); pSrcIdx=pSrcIdx->pNext){
drh9d9cf222007-02-13 15:01:11 +00001763 if( xferCompatibleIndex(pDestIdx, pSrcIdx) ) break;
1764 }
1765 assert( pSrcIdx );
drh66a51672008-01-03 00:01:23 +00001766 sqlite3VdbeAddOp2(v, OP_Close, iSrc, 0);
1767 sqlite3VdbeAddOp2(v, OP_Close, iDest, 0);
drh9d9cf222007-02-13 15:01:11 +00001768 pKey = sqlite3IndexKeyinfo(pParse, pSrcIdx);
danielk1977207872a2008-01-03 07:54:23 +00001769 sqlite3VdbeAddOp4(v, OP_OpenRead, iSrc, pSrcIdx->tnum, iDbSrc,
1770 (char*)pKey, P4_KEYINFO_HANDOFF);
drhd4e70eb2008-01-02 00:34:36 +00001771 VdbeComment((v, "%s", pSrcIdx->zName));
drh9d9cf222007-02-13 15:01:11 +00001772 pKey = sqlite3IndexKeyinfo(pParse, pDestIdx);
danielk1977207872a2008-01-03 07:54:23 +00001773 sqlite3VdbeAddOp4(v, OP_OpenWrite, iDest, pDestIdx->tnum, iDbDest,
drh66a51672008-01-03 00:01:23 +00001774 (char*)pKey, P4_KEYINFO_HANDOFF);
danielk1977207872a2008-01-03 07:54:23 +00001775 VdbeComment((v, "%s", pDestIdx->zName));
drh66a51672008-01-03 00:01:23 +00001776 addr1 = sqlite3VdbeAddOp2(v, OP_Rewind, iSrc, 0);
drhb7654112008-01-12 12:48:07 +00001777 sqlite3VdbeAddOp2(v, OP_RowKey, iSrc, regData);
1778 sqlite3VdbeAddOp3(v, OP_IdxInsert, iDest, regData, 1);
drh66a51672008-01-03 00:01:23 +00001779 sqlite3VdbeAddOp2(v, OP_Next, iSrc, addr1+1);
drh9d9cf222007-02-13 15:01:11 +00001780 sqlite3VdbeJumpHere(v, addr1);
1781 }
1782 sqlite3VdbeJumpHere(v, emptySrcTest);
drhb7654112008-01-12 12:48:07 +00001783 sqlite3ReleaseTempReg(pParse, regRowid);
1784 sqlite3ReleaseTempReg(pParse, regData);
drh66a51672008-01-03 00:01:23 +00001785 sqlite3VdbeAddOp2(v, OP_Close, iSrc, 0);
1786 sqlite3VdbeAddOp2(v, OP_Close, iDest, 0);
drh9d9cf222007-02-13 15:01:11 +00001787 if( emptyDestTest ){
drh66a51672008-01-03 00:01:23 +00001788 sqlite3VdbeAddOp2(v, OP_Halt, SQLITE_OK, 0);
drh9d9cf222007-02-13 15:01:11 +00001789 sqlite3VdbeJumpHere(v, emptyDestTest);
drh66a51672008-01-03 00:01:23 +00001790 sqlite3VdbeAddOp2(v, OP_Close, iDest, 0);
drh9d9cf222007-02-13 15:01:11 +00001791 return 0;
1792 }else{
1793 return 1;
1794 }
1795}
1796#endif /* SQLITE_OMIT_XFER_OPT */
drhf39d9582008-03-19 20:42:13 +00001797
1798/* Make sure "isView" gets undefined in case this file becomes part of
1799** the amalgamation - so that subsequent files do not see isView as a
1800** macro. */
1801#undef isView