blob: e19aa7da174c2a0d900427a2bbdea7cd6f94cd3a [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**
drhd654be82005-09-20 17:42:23 +000015** $Id: insert.c,v 1.143 2005/09/20 17:42:23 drh Exp $
drhcce7d172000-05-31 15:34:51 +000016*/
17#include "sqliteInt.h"
18
19/*
danielk19773d1bfea2004-05-14 11:00:53 +000020** Set P3 of the most recently inserted opcode to a column affinity
danielk1977a37cdde2004-05-16 11:15:36 +000021** string for index pIdx. A column affinity string has one character
danielk19773d1bfea2004-05-14 11:00:53 +000022** for each column in the table, according to the affinity of the column:
23**
24** Character Column affinity
25** ------------------------------
26** 'n' NUMERIC
27** 'i' INTEGER
28** 't' TEXT
29** 'o' NONE
30*/
danielk1977a37cdde2004-05-16 11:15:36 +000031void sqlite3IndexAffinityStr(Vdbe *v, Index *pIdx){
32 if( !pIdx->zColAff ){
danielk1977e014a832004-05-17 10:48:57 +000033 /* The first time a column affinity string for a particular index is
danielk1977a37cdde2004-05-16 11:15:36 +000034 ** required, it is allocated and populated here. It is then stored as
danielk1977e014a832004-05-17 10:48:57 +000035 ** a member of the Index structure for subsequent use.
danielk1977a37cdde2004-05-16 11:15:36 +000036 **
37 ** The column affinity string will eventually be deleted by
danielk1977e014a832004-05-17 10:48:57 +000038 ** sqliteDeleteIndex() when the Index structure itself is cleaned
danielk1977a37cdde2004-05-16 11:15:36 +000039 ** up.
40 */
41 int n;
42 Table *pTab = pIdx->pTable;
43 pIdx->zColAff = (char *)sqliteMalloc(pIdx->nColumn+1);
44 if( !pIdx->zColAff ){
45 return;
46 }
47 for(n=0; n<pIdx->nColumn; n++){
48 pIdx->zColAff[n] = pTab->aCol[pIdx->aiColumn[n]].affinity;
49 }
50 pIdx->zColAff[pIdx->nColumn] = '\0';
51 }
danielk19773d1bfea2004-05-14 11:00:53 +000052
drh956bc922004-07-24 17:38:29 +000053 sqlite3VdbeChangeP3(v, -1, pIdx->zColAff, 0);
danielk1977a37cdde2004-05-16 11:15:36 +000054}
55
56/*
57** Set P3 of the most recently inserted opcode to a column affinity
58** string for table pTab. A column affinity string has one character
59** for each column indexed by the index, according to the affinity of the
60** column:
61**
62** Character Column affinity
63** ------------------------------
64** 'n' NUMERIC
65** 'i' INTEGER
66** 't' TEXT
67** 'o' NONE
68*/
69void sqlite3TableAffinityStr(Vdbe *v, Table *pTab){
danielk19773d1bfea2004-05-14 11:00:53 +000070 /* The first time a column affinity string for a particular table
71 ** is required, it is allocated and populated here. It is then
72 ** stored as a member of the Table structure for subsequent use.
73 **
74 ** The column affinity string will eventually be deleted by
75 ** sqlite3DeleteTable() when the Table structure itself is cleaned up.
76 */
77 if( !pTab->zColAff ){
78 char *zColAff;
79 int i;
80
danielk1977a37cdde2004-05-16 11:15:36 +000081 zColAff = (char *)sqliteMalloc(pTab->nCol+1);
danielk19773d1bfea2004-05-14 11:00:53 +000082 if( !zColAff ){
danielk1977a37cdde2004-05-16 11:15:36 +000083 return;
danielk19773d1bfea2004-05-14 11:00:53 +000084 }
85
86 for(i=0; i<pTab->nCol; i++){
danielk1977a37cdde2004-05-16 11:15:36 +000087 zColAff[i] = pTab->aCol[i].affinity;
danielk19773d1bfea2004-05-14 11:00:53 +000088 }
89 zColAff[pTab->nCol] = '\0';
90
91 pTab->zColAff = zColAff;
92 }
93
drh956bc922004-07-24 17:38:29 +000094 sqlite3VdbeChangeP3(v, -1, pTab->zColAff, 0);
danielk19773d1bfea2004-05-14 11:00:53 +000095}
96
danielk19774d887782005-02-08 08:42:27 +000097/*
98** Return non-zero if SELECT statement p opens the table with rootpage
99** iTab in database iDb. This is used to see if a statement of the form
100** "INSERT INTO <iDb, iTab> SELECT ..." can run without using temporary
101** table for the results of the SELECT.
102**
103** No checking is done for sub-selects that are part of expressions.
104*/
105static int selectReadsTable(Select *p, int iDb, int iTab){
106 int i;
107 struct SrcList_item *pItem;
108 if( p->pSrc==0 ) return 0;
109 for(i=0, pItem=p->pSrc->a; i<p->pSrc->nSrc; i++, pItem++){
110 if( pItem->pSelect ){
drh38fba692005-03-21 01:20:58 +0000111 if( selectReadsTable(pItem->pSelect, iDb, iTab) ) return 1;
danielk19774d887782005-02-08 08:42:27 +0000112 }else{
113 if( pItem->pTab->iDb==iDb && pItem->pTab->tnum==iTab ) return 1;
114 }
115 }
116 return 0;
117}
danielk19773d1bfea2004-05-14 11:00:53 +0000118
119/*
drh1ccde152000-06-17 13:12:39 +0000120** This routine is call to handle SQL of the following forms:
drhcce7d172000-05-31 15:34:51 +0000121**
122** insert into TABLE (IDLIST) values(EXPRLIST)
drh1ccde152000-06-17 13:12:39 +0000123** insert into TABLE (IDLIST) select
drhcce7d172000-05-31 15:34:51 +0000124**
drh1ccde152000-06-17 13:12:39 +0000125** The IDLIST following the table name is always optional. If omitted,
126** then a list of all columns for the table is substituted. The IDLIST
drh967e8b72000-06-21 13:59:10 +0000127** appears in the pColumn parameter. pColumn is NULL if IDLIST is omitted.
drh1ccde152000-06-17 13:12:39 +0000128**
129** The pList parameter holds EXPRLIST in the first form of the INSERT
130** statement above, and pSelect is NULL. For the second form, pList is
131** NULL and pSelect is a pointer to the select statement used to generate
132** data for the insert.
drh142e30d2002-08-28 03:00:58 +0000133**
134** The code generated follows one of three templates. For a simple
135** select with data coming from a VALUES clause, the code executes
136** once straight down through. The template looks like this:
137**
138** open write cursor to <table> and its indices
139** puts VALUES clause expressions onto the stack
140** write the resulting record into <table>
141** cleanup
142**
143** If the statement is of the form
144**
145** INSERT INTO <table> SELECT ...
146**
147** And the SELECT clause does not read from <table> at any time, then
148** the generated code follows this template:
149**
150** goto B
151** A: setup for the SELECT
152** loop over the tables in the SELECT
153** gosub C
154** end loop
155** cleanup after the SELECT
156** goto D
157** B: open write cursor to <table> and its indices
158** goto A
159** C: insert the select result into <table>
160** return
161** D: cleanup
162**
163** The third template is used if the insert statement takes its
164** values from a SELECT but the data is being inserted into a table
165** that is also read as part of the SELECT. In the third form,
166** we have to use a intermediate table to store the results of
167** the select. The template is like this:
168**
169** goto B
170** A: setup for the SELECT
171** loop over the tables in the SELECT
172** gosub C
173** end loop
174** cleanup after the SELECT
175** goto D
176** C: insert the select result into the intermediate table
177** return
178** B: open a cursor to an intermediate table
179** goto A
180** D: open write cursor to <table> and its indices
181** loop over the intermediate table
182** transfer values form intermediate table into <table>
183** end the loop
184** cleanup
drhcce7d172000-05-31 15:34:51 +0000185*/
danielk19774adee202004-05-08 08:23:19 +0000186void sqlite3Insert(
drhcce7d172000-05-31 15:34:51 +0000187 Parse *pParse, /* Parser context */
drh113088e2003-03-20 01:16:58 +0000188 SrcList *pTabList, /* Name of table into which we are inserting */
drhcce7d172000-05-31 15:34:51 +0000189 ExprList *pList, /* List of values to be inserted */
drh5974a302000-06-07 14:42:26 +0000190 Select *pSelect, /* A SELECT statement to use as the data source */
drh9cfcf5d2002-01-29 18:41:24 +0000191 IdList *pColumn, /* Column names corresponding to IDLIST. */
192 int onError /* How to handle constraint errors */
drhcce7d172000-05-31 15:34:51 +0000193){
drh5974a302000-06-07 14:42:26 +0000194 Table *pTab; /* The table to insert into */
drh113088e2003-03-20 01:16:58 +0000195 char *zTab; /* Name of the table into which we are inserting */
drhe22a3342003-04-22 20:30:37 +0000196 const char *zDb; /* Name of the database holding this table */
drh5974a302000-06-07 14:42:26 +0000197 int i, j, idx; /* Loop counters */
198 Vdbe *v; /* Generate code into this virtual machine */
199 Index *pIdx; /* For looping over indices of the table */
drh967e8b72000-06-21 13:59:10 +0000200 int nColumn; /* Number of columns in the data */
danielk1977cfe9a692004-06-16 12:00:29 +0000201 int base = 0; /* VDBE Cursor number for pTab */
202 int iCont=0,iBreak=0; /* Beginning and end of the loop over srcTab */
drh9bb575f2004-09-06 17:24:11 +0000203 sqlite3 *db; /* The main database structure */
drh4a324312001-12-21 14:30:42 +0000204 int keyColumn = -1; /* Column that is the INTEGER PRIMARY KEY */
drh0ca3e242002-01-29 23:07:02 +0000205 int endOfLoop; /* Label for the end of the insertion loop */
danielk19774d887782005-02-08 08:42:27 +0000206 int useTempTable = 0; /* Store SELECT results in intermediate table */
danielk1977cfe9a692004-06-16 12:00:29 +0000207 int srcTab = 0; /* Data comes from this temporary cursor if >=0 */
208 int iSelectLoop = 0; /* Address of code that implements the SELECT */
209 int iCleanup = 0; /* Address of the cleanup code */
210 int iInsertBlock = 0; /* Address of the subroutine used to insert data */
211 int iCntMem = 0; /* Memory cell used for the row counter */
drh798da522004-11-04 04:42:28 +0000212 int newIdx = -1; /* Cursor for the NEW table */
drh2958a4e2004-11-12 03:56:15 +0000213 Db *pDb; /* The database containing table being inserted into */
214 int counterMem = 0; /* Memory cell holding AUTOINCREMENT counter */
drhcce7d172000-05-31 15:34:51 +0000215
drh798da522004-11-04 04:42:28 +0000216#ifndef SQLITE_OMIT_TRIGGER
217 int isView; /* True if attempting to insert into a view */
drhdca76842004-12-07 14:06:13 +0000218 int triggers_exist = 0; /* True if there are FOR EACH ROW triggers */
drh798da522004-11-04 04:42:28 +0000219#endif
danielk1977c3f9bad2002-05-15 08:30:12 +0000220
drhf3388142004-11-13 03:48:06 +0000221#ifndef SQLITE_OMIT_AUTOINCREMENT
222 int counterRowid; /* Memory cell holding rowid of autoinc counter */
223#endif
224
danielk197724b03fd2004-05-10 10:34:34 +0000225 if( pParse->nErr || sqlite3_malloc_failed ) goto insert_cleanup;
drhecdc7532001-09-23 02:35:53 +0000226 db = pParse->db;
drhdaffd0e2001-04-11 14:28:42 +0000227
drh1ccde152000-06-17 13:12:39 +0000228 /* Locate the table into which we will be inserting new information.
229 */
drh113088e2003-03-20 01:16:58 +0000230 assert( pTabList->nSrc==1 );
231 zTab = pTabList->a[0].zName;
drhdaffd0e2001-04-11 14:28:42 +0000232 if( zTab==0 ) goto insert_cleanup;
danielk19774adee202004-05-08 08:23:19 +0000233 pTab = sqlite3SrcListLookup(pParse, pTabList);
danielk1977c3f9bad2002-05-15 08:30:12 +0000234 if( pTab==0 ){
danielk1977c3f9bad2002-05-15 08:30:12 +0000235 goto insert_cleanup;
236 }
drhe22a3342003-04-22 20:30:37 +0000237 assert( pTab->iDb<db->nDb );
drh2958a4e2004-11-12 03:56:15 +0000238 pDb = &db->aDb[pTab->iDb];
239 zDb = pDb->zName;
danielk19774adee202004-05-08 08:23:19 +0000240 if( sqlite3AuthCheck(pParse, SQLITE_INSERT, pTab->zName, 0, zDb) ){
drh1962bda2003-01-12 19:33:52 +0000241 goto insert_cleanup;
242 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000243
drhb7f91642004-10-31 02:22:47 +0000244 /* Figure out if we have any triggers and if the table being
245 ** inserted into is a view
246 */
247#ifndef SQLITE_OMIT_TRIGGER
drhdca76842004-12-07 14:06:13 +0000248 triggers_exist = sqlite3TriggersExist(pParse, pTab, TK_INSERT, 0);
drhb7f91642004-10-31 02:22:47 +0000249 isView = pTab->pSelect!=0;
250#else
drhdca76842004-12-07 14:06:13 +0000251# define triggers_exist 0
drhb7f91642004-10-31 02:22:47 +0000252# define isView 0
253#endif
254#ifdef SQLITE_OMIT_VIEW
255# undef isView
256# define isView 0
257#endif
258
danielk1977c3f9bad2002-05-15 08:30:12 +0000259 /* Ensure that:
260 * (a) the table is not read-only,
261 * (b) that if it is a view then ON INSERT triggers exist
262 */
drhdca76842004-12-07 14:06:13 +0000263 if( sqlite3IsReadOnly(pParse, pTab, triggers_exist) ){
danielk1977c3f9bad2002-05-15 08:30:12 +0000264 goto insert_cleanup;
265 }
drha76b5df2002-02-23 02:32:10 +0000266 if( pTab==0 ) goto insert_cleanup;
drh1ccde152000-06-17 13:12:39 +0000267
drhf573c992002-07-31 00:32:50 +0000268 /* If pTab is really a view, make sure it has been initialized.
269 */
danielk19774adee202004-05-08 08:23:19 +0000270 if( isView && sqlite3ViewGetColumnNames(pParse, pTab) ){
drh5cf590c2003-04-24 01:45:04 +0000271 goto insert_cleanup;
drhf573c992002-07-31 00:32:50 +0000272 }
273
danielk19777cedc8d2004-06-10 10:50:08 +0000274 /* Ensure all required collation sequences are available. */
275 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
276 if( sqlite3CheckIndexCollSeq(pParse, pIdx) ){
277 goto insert_cleanup;
278 }
279 }
280
drh1ccde152000-06-17 13:12:39 +0000281 /* Allocate a VDBE
282 */
danielk19774adee202004-05-08 08:23:19 +0000283 v = sqlite3GetVdbe(pParse);
drh5974a302000-06-07 14:42:26 +0000284 if( v==0 ) goto insert_cleanup;
drh4794f732004-11-05 17:17:50 +0000285 if( pParse->nested==0 ) sqlite3VdbeCountChanges(v);
drhdca76842004-12-07 14:06:13 +0000286 sqlite3BeginWriteOperation(pParse, pSelect || triggers_exist, pTab->iDb);
drh1ccde152000-06-17 13:12:39 +0000287
danielk1977c3f9bad2002-05-15 08:30:12 +0000288 /* if there are row triggers, allocate a temp table for new.* references. */
drhdca76842004-12-07 14:06:13 +0000289 if( triggers_exist ){
danielk1977c3f9bad2002-05-15 08:30:12 +0000290 newIdx = pParse->nTab++;
danielk1977f29ce552002-05-19 23:43:12 +0000291 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000292
drh2958a4e2004-11-12 03:56:15 +0000293#ifndef SQLITE_OMIT_AUTOINCREMENT
294 /* If this is an AUTOINCREMENT table, look up the sequence number in the
drhf3388142004-11-13 03:48:06 +0000295 ** sqlite_sequence table and store it in memory cell counterMem. Also
296 ** remember the rowid of the sqlite_sequence table entry in memory cell
297 ** counterRowid.
drh2958a4e2004-11-12 03:56:15 +0000298 */
299 if( pTab->autoInc ){
drhf3388142004-11-13 03:48:06 +0000300 int iCur = pParse->nTab;
301 int base = sqlite3VdbeCurrentAddr(v);
302 counterRowid = pParse->nMem++;
303 counterMem = pParse->nMem++;
304 sqlite3VdbeAddOp(v, OP_Integer, pTab->iDb, 0);
305 sqlite3VdbeAddOp(v, OP_OpenRead, iCur, pDb->pSeqTab->tnum);
306 sqlite3VdbeAddOp(v, OP_SetNumColumns, iCur, 2);
307 sqlite3VdbeAddOp(v, OP_Rewind, iCur, base+13);
308 sqlite3VdbeAddOp(v, OP_Column, iCur, 0);
309 sqlite3VdbeOp3(v, OP_String8, 0, 0, pTab->zName, 0);
310 sqlite3VdbeAddOp(v, OP_Ne, 28417, base+12);
drhf0863fe2005-06-12 21:35:51 +0000311 sqlite3VdbeAddOp(v, OP_Rowid, iCur, 0);
drhf3388142004-11-13 03:48:06 +0000312 sqlite3VdbeAddOp(v, OP_MemStore, counterRowid, 1);
313 sqlite3VdbeAddOp(v, OP_Column, iCur, 1);
314 sqlite3VdbeAddOp(v, OP_MemStore, counterMem, 1);
315 sqlite3VdbeAddOp(v, OP_Goto, 0, base+13);
316 sqlite3VdbeAddOp(v, OP_Next, iCur, base+4);
317 sqlite3VdbeAddOp(v, OP_Close, iCur, 0);
drh2958a4e2004-11-12 03:56:15 +0000318 }
319#endif /* SQLITE_OMIT_AUTOINCREMENT */
320
drh1ccde152000-06-17 13:12:39 +0000321 /* Figure out how many columns of data are supplied. If the data
drh142e30d2002-08-28 03:00:58 +0000322 ** is coming from a SELECT statement, then this step also generates
323 ** all the code to implement the SELECT statement and invoke a subroutine
324 ** to process each row of the result. (Template 2.) If the SELECT
325 ** statement uses the the table that is being inserted into, then the
326 ** subroutine is also coded here. That subroutine stores the SELECT
327 ** results in a temporary table. (Template 3.)
drh1ccde152000-06-17 13:12:39 +0000328 */
drh5974a302000-06-07 14:42:26 +0000329 if( pSelect ){
drh142e30d2002-08-28 03:00:58 +0000330 /* Data is coming from a SELECT. Generate code to implement that SELECT
331 */
332 int rc, iInitCode;
danielk19774adee202004-05-08 08:23:19 +0000333 iInitCode = sqlite3VdbeAddOp(v, OP_Goto, 0, 0);
334 iSelectLoop = sqlite3VdbeCurrentAddr(v);
335 iInsertBlock = sqlite3VdbeMakeLabel(v);
danielk1977b3bce662005-01-29 08:32:43 +0000336
337 /* Resolve the expressions in the SELECT statement and execute it. */
338 rc = sqlite3Select(pParse, pSelect, SRT_Subroutine, iInsertBlock,0,0,0,0);
danielk197724b03fd2004-05-10 10:34:34 +0000339 if( rc || pParse->nErr || sqlite3_malloc_failed ) goto insert_cleanup;
danielk1977b3bce662005-01-29 08:32:43 +0000340
danielk19774adee202004-05-08 08:23:19 +0000341 iCleanup = sqlite3VdbeMakeLabel(v);
342 sqlite3VdbeAddOp(v, OP_Goto, 0, iCleanup);
drh5974a302000-06-07 14:42:26 +0000343 assert( pSelect->pEList );
drh967e8b72000-06-21 13:59:10 +0000344 nColumn = pSelect->pEList->nExpr;
drh142e30d2002-08-28 03:00:58 +0000345
346 /* Set useTempTable to TRUE if the result of the SELECT statement
347 ** should be written into a temporary table. Set to FALSE if each
348 ** row of the SELECT can be written directly into the result table.
drh048c5302003-04-03 01:50:44 +0000349 **
350 ** A temp table must be used if the table being updated is also one
351 ** of the tables being read by the SELECT statement. Also use a
352 ** temp table in the case of row triggers.
drh142e30d2002-08-28 03:00:58 +0000353 */
danielk19774d887782005-02-08 08:42:27 +0000354 if( triggers_exist || selectReadsTable(pSelect, pTab->iDb, pTab->tnum) ){
drh048c5302003-04-03 01:50:44 +0000355 useTempTable = 1;
drh048c5302003-04-03 01:50:44 +0000356 }
drh142e30d2002-08-28 03:00:58 +0000357
358 if( useTempTable ){
359 /* Generate the subroutine that SELECT calls to process each row of
360 ** the result. Store the result in a temporary table
361 */
362 srcTab = pParse->nTab++;
danielk19774adee202004-05-08 08:23:19 +0000363 sqlite3VdbeResolveLabel(v, iInsertBlock);
364 sqlite3VdbeAddOp(v, OP_MakeRecord, nColumn, 0);
danielk1977a37cdde2004-05-16 11:15:36 +0000365 sqlite3TableAffinityStr(v, pTab);
drhf0863fe2005-06-12 21:35:51 +0000366 sqlite3VdbeAddOp(v, OP_NewRowid, srcTab, 0);
danielk19774adee202004-05-08 08:23:19 +0000367 sqlite3VdbeAddOp(v, OP_Pull, 1, 0);
drhf0863fe2005-06-12 21:35:51 +0000368 sqlite3VdbeAddOp(v, OP_Insert, srcTab, 0);
danielk19774adee202004-05-08 08:23:19 +0000369 sqlite3VdbeAddOp(v, OP_Return, 0, 0);
drh142e30d2002-08-28 03:00:58 +0000370
371 /* The following code runs first because the GOTO at the very top
372 ** of the program jumps to it. Create the temporary table, then jump
373 ** back up and execute the SELECT code above.
374 */
drhd654be82005-09-20 17:42:23 +0000375 sqlite3VdbeJumpHere(v, iInitCode);
drh9170dd72005-07-08 17:13:46 +0000376 sqlite3VdbeAddOp(v, OP_OpenVirtual, srcTab, 0);
drhb6f54522004-05-20 02:42:16 +0000377 sqlite3VdbeAddOp(v, OP_SetNumColumns, srcTab, nColumn);
danielk19774adee202004-05-08 08:23:19 +0000378 sqlite3VdbeAddOp(v, OP_Goto, 0, iSelectLoop);
379 sqlite3VdbeResolveLabel(v, iCleanup);
drh142e30d2002-08-28 03:00:58 +0000380 }else{
drhd654be82005-09-20 17:42:23 +0000381 sqlite3VdbeJumpHere(v, iInitCode);
drh142e30d2002-08-28 03:00:58 +0000382 }
drh5974a302000-06-07 14:42:26 +0000383 }else{
drh142e30d2002-08-28 03:00:58 +0000384 /* This is the case if the data for the INSERT is coming from a VALUES
385 ** clause
386 */
danielk1977b3bce662005-01-29 08:32:43 +0000387 NameContext sNC;
388 memset(&sNC, 0, sizeof(sNC));
389 sNC.pParse = pParse;
drhdaffd0e2001-04-11 14:28:42 +0000390 assert( pList!=0 );
drh5974a302000-06-07 14:42:26 +0000391 srcTab = -1;
drh142e30d2002-08-28 03:00:58 +0000392 useTempTable = 0;
drh5974a302000-06-07 14:42:26 +0000393 assert( pList );
drh967e8b72000-06-21 13:59:10 +0000394 nColumn = pList->nExpr;
drhe64e7b22002-02-18 13:56:36 +0000395 for(i=0; i<nColumn; i++){
danielk1977b3bce662005-01-29 08:32:43 +0000396 if( sqlite3ExprResolveNames(&sNC, pList->a[i].pExpr) ){
drhb04a5d82002-04-12 03:55:15 +0000397 goto insert_cleanup;
398 }
drhe64e7b22002-02-18 13:56:36 +0000399 }
drh5974a302000-06-07 14:42:26 +0000400 }
drh1ccde152000-06-17 13:12:39 +0000401
402 /* Make sure the number of columns in the source data matches the number
403 ** of columns to be inserted into the table.
404 */
drh967e8b72000-06-21 13:59:10 +0000405 if( pColumn==0 && nColumn!=pTab->nCol ){
danielk19774adee202004-05-08 08:23:19 +0000406 sqlite3ErrorMsg(pParse,
drhda93d232003-03-31 02:12:46 +0000407 "table %S has %d columns but %d values were supplied",
408 pTabList, 0, pTab->nCol, nColumn);
drhcce7d172000-05-31 15:34:51 +0000409 goto insert_cleanup;
410 }
drh967e8b72000-06-21 13:59:10 +0000411 if( pColumn!=0 && nColumn!=pColumn->nId ){
danielk19774adee202004-05-08 08:23:19 +0000412 sqlite3ErrorMsg(pParse, "%d values for %d columns", nColumn, pColumn->nId);
drhcce7d172000-05-31 15:34:51 +0000413 goto insert_cleanup;
414 }
drh1ccde152000-06-17 13:12:39 +0000415
416 /* If the INSERT statement included an IDLIST term, then make sure
417 ** all elements of the IDLIST really are columns of the table and
418 ** remember the column indices.
drhc8392582001-12-31 02:48:51 +0000419 **
420 ** If the table has an INTEGER PRIMARY KEY column and that column
421 ** is named in the IDLIST, then record in the keyColumn variable
422 ** the index into IDLIST of the primary key column. keyColumn is
423 ** the index of the primary key as it appears in IDLIST, not as
424 ** is appears in the original table. (The index of the primary
425 ** key in the original table is pTab->iPKey.)
drh1ccde152000-06-17 13:12:39 +0000426 */
drh967e8b72000-06-21 13:59:10 +0000427 if( pColumn ){
428 for(i=0; i<pColumn->nId; i++){
429 pColumn->a[i].idx = -1;
drhcce7d172000-05-31 15:34:51 +0000430 }
drh967e8b72000-06-21 13:59:10 +0000431 for(i=0; i<pColumn->nId; i++){
drhcce7d172000-05-31 15:34:51 +0000432 for(j=0; j<pTab->nCol; j++){
danielk19774adee202004-05-08 08:23:19 +0000433 if( sqlite3StrICmp(pColumn->a[i].zName, pTab->aCol[j].zName)==0 ){
drh967e8b72000-06-21 13:59:10 +0000434 pColumn->a[i].idx = j;
drh4a324312001-12-21 14:30:42 +0000435 if( j==pTab->iPKey ){
drh9aa028d2001-12-22 21:48:29 +0000436 keyColumn = i;
drh4a324312001-12-21 14:30:42 +0000437 }
drhcce7d172000-05-31 15:34:51 +0000438 break;
439 }
440 }
441 if( j>=pTab->nCol ){
danielk19774adee202004-05-08 08:23:19 +0000442 if( sqlite3IsRowid(pColumn->a[i].zName) ){
drha0217ba2003-06-01 01:10:33 +0000443 keyColumn = i;
444 }else{
danielk19774adee202004-05-08 08:23:19 +0000445 sqlite3ErrorMsg(pParse, "table %S has no column named %s",
drha0217ba2003-06-01 01:10:33 +0000446 pTabList, 0, pColumn->a[i].zName);
447 pParse->nErr++;
448 goto insert_cleanup;
449 }
drhcce7d172000-05-31 15:34:51 +0000450 }
451 }
452 }
drh1ccde152000-06-17 13:12:39 +0000453
drhaacc5432002-01-06 17:07:40 +0000454 /* If there is no IDLIST term but the table has an integer primary
drhc8392582001-12-31 02:48:51 +0000455 ** key, the set the keyColumn variable to the primary key column index
456 ** in the original table definition.
drh4a324312001-12-21 14:30:42 +0000457 */
458 if( pColumn==0 ){
459 keyColumn = pTab->iPKey;
460 }
461
drh142e30d2002-08-28 03:00:58 +0000462 /* Open the temp table for FOR EACH ROW triggers
463 */
drhdca76842004-12-07 14:06:13 +0000464 if( triggers_exist ){
danielk19774adee202004-05-08 08:23:19 +0000465 sqlite3VdbeAddOp(v, OP_OpenPseudo, newIdx, 0);
danielk197784ac9d02004-05-18 09:58:06 +0000466 sqlite3VdbeAddOp(v, OP_SetNumColumns, newIdx, pTab->nCol);
danielk1977f29ce552002-05-19 23:43:12 +0000467 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000468
drhfeeb1392002-04-09 03:28:01 +0000469 /* Initialize the count of rows to be inserted
470 */
drh142e30d2002-08-28 03:00:58 +0000471 if( db->flags & SQLITE_CountRows ){
472 iCntMem = pParse->nMem++;
drhd654be82005-09-20 17:42:23 +0000473 sqlite3VdbeAddOp(v, OP_MemInt, 0, iCntMem);
drhfeeb1392002-04-09 03:28:01 +0000474 }
475
danielk1977c3f9bad2002-05-15 08:30:12 +0000476 /* Open tables and indices if there are no row triggers */
drhdca76842004-12-07 14:06:13 +0000477 if( !triggers_exist ){
danielk1977c3f9bad2002-05-15 08:30:12 +0000478 base = pParse->nTab;
drh290c1942004-08-21 17:54:45 +0000479 sqlite3OpenTableAndIndices(pParse, pTab, base, OP_OpenWrite);
danielk1977c3f9bad2002-05-15 08:30:12 +0000480 }
481
drh142e30d2002-08-28 03:00:58 +0000482 /* If the data source is a temporary table, then we have to create
drh1ccde152000-06-17 13:12:39 +0000483 ** a loop because there might be multiple rows of data. If the data
drh142e30d2002-08-28 03:00:58 +0000484 ** source is a subroutine call from the SELECT statement, then we need
485 ** to launch the SELECT statement processing.
drh1ccde152000-06-17 13:12:39 +0000486 */
drh142e30d2002-08-28 03:00:58 +0000487 if( useTempTable ){
danielk19774adee202004-05-08 08:23:19 +0000488 iBreak = sqlite3VdbeMakeLabel(v);
489 sqlite3VdbeAddOp(v, OP_Rewind, srcTab, iBreak);
490 iCont = sqlite3VdbeCurrentAddr(v);
drh142e30d2002-08-28 03:00:58 +0000491 }else if( pSelect ){
danielk19774adee202004-05-08 08:23:19 +0000492 sqlite3VdbeAddOp(v, OP_Goto, 0, iSelectLoop);
493 sqlite3VdbeResolveLabel(v, iInsertBlock);
drh5974a302000-06-07 14:42:26 +0000494 }
drh1ccde152000-06-17 13:12:39 +0000495
drh5cf590c2003-04-24 01:45:04 +0000496 /* Run the BEFORE and INSTEAD OF triggers, if there are any
drh70ce3f02003-04-15 19:22:22 +0000497 */
danielk19774adee202004-05-08 08:23:19 +0000498 endOfLoop = sqlite3VdbeMakeLabel(v);
drhdca76842004-12-07 14:06:13 +0000499 if( triggers_exist & TRIGGER_BEFORE ){
danielk1977c3f9bad2002-05-15 08:30:12 +0000500
drh70ce3f02003-04-15 19:22:22 +0000501 /* build the NEW.* reference row. Note that if there is an INTEGER
502 ** PRIMARY KEY into which a NULL is being inserted, that NULL will be
503 ** translated into a unique ID for the row. But on a BEFORE trigger,
504 ** we do not know what the unique ID will be (because the insert has
505 ** not happened yet) so we substitute a rowid of -1
506 */
507 if( keyColumn<0 ){
danielk19774adee202004-05-08 08:23:19 +0000508 sqlite3VdbeAddOp(v, OP_Integer, -1, 0);
drh70ce3f02003-04-15 19:22:22 +0000509 }else if( useTempTable ){
danielk19774adee202004-05-08 08:23:19 +0000510 sqlite3VdbeAddOp(v, OP_Column, srcTab, keyColumn);
drh70ce3f02003-04-15 19:22:22 +0000511 }else{
drhd6fe9612005-01-14 01:22:00 +0000512 assert( pSelect==0 ); /* Otherwise useTempTable is true */
danielk19774adee202004-05-08 08:23:19 +0000513 sqlite3ExprCode(pParse, pList->a[keyColumn].pExpr);
514 sqlite3VdbeAddOp(v, OP_NotNull, -1, sqlite3VdbeCurrentAddr(v)+3);
515 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
516 sqlite3VdbeAddOp(v, OP_Integer, -1, 0);
517 sqlite3VdbeAddOp(v, OP_MustBeInt, 0, 0);
drh70ce3f02003-04-15 19:22:22 +0000518 }
519
520 /* Create the new column data
521 */
danielk1977c3f9bad2002-05-15 08:30:12 +0000522 for(i=0; i<pTab->nCol; i++){
523 if( pColumn==0 ){
drh9adf9ac2002-05-15 11:44:13 +0000524 j = i;
danielk1977c3f9bad2002-05-15 08:30:12 +0000525 }else{
drh9adf9ac2002-05-15 11:44:13 +0000526 for(j=0; j<pColumn->nId; j++){
527 if( pColumn->a[j].idx==i ) break;
528 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000529 }
530 if( pColumn && j>=pColumn->nId ){
danielk19777977a172004-11-09 12:44:37 +0000531 sqlite3ExprCode(pParse, pTab->aCol[i].pDflt);
drh142e30d2002-08-28 03:00:58 +0000532 }else if( useTempTable ){
danielk19774adee202004-05-08 08:23:19 +0000533 sqlite3VdbeAddOp(v, OP_Column, srcTab, j);
danielk1977c3f9bad2002-05-15 08:30:12 +0000534 }else{
drhd6fe9612005-01-14 01:22:00 +0000535 assert( pSelect==0 ); /* Otherwise useTempTable is true */
drh25303782004-12-07 15:41:48 +0000536 sqlite3ExprCodeAndCache(pParse, pList->a[j].pExpr);
danielk1977c3f9bad2002-05-15 08:30:12 +0000537 }
538 }
danielk19774adee202004-05-08 08:23:19 +0000539 sqlite3VdbeAddOp(v, OP_MakeRecord, pTab->nCol, 0);
danielk1977a37cdde2004-05-16 11:15:36 +0000540
541 /* If this is an INSERT on a view with an INSTEAD OF INSERT trigger,
542 ** do not attempt any conversions before assembling the record.
543 ** If this is a real table, attempt conversions as required by the
544 ** table column affinities.
545 */
546 if( !isView ){
547 sqlite3TableAffinityStr(v, pTab);
548 }
drhf0863fe2005-06-12 21:35:51 +0000549 sqlite3VdbeAddOp(v, OP_Insert, newIdx, 0);
danielk1977c3f9bad2002-05-15 08:30:12 +0000550
drh5cf590c2003-04-24 01:45:04 +0000551 /* Fire BEFORE or INSTEAD OF triggers */
drhdca76842004-12-07 14:06:13 +0000552 if( sqlite3CodeRowTrigger(pParse, TK_INSERT, 0, TRIGGER_BEFORE, pTab,
drhb2fe7d82003-04-20 17:29:23 +0000553 newIdx, -1, onError, endOfLoop) ){
danielk1977f29ce552002-05-19 23:43:12 +0000554 goto insert_cleanup;
555 }
drh70ce3f02003-04-15 19:22:22 +0000556 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000557
drh70ce3f02003-04-15 19:22:22 +0000558 /* If any triggers exists, the opening of tables and indices is deferred
559 ** until now.
560 */
drhdca76842004-12-07 14:06:13 +0000561 if( triggers_exist && !isView ){
drh5cf590c2003-04-24 01:45:04 +0000562 base = pParse->nTab;
drh290c1942004-08-21 17:54:45 +0000563 sqlite3OpenTableAndIndices(pParse, pTab, base, OP_OpenWrite);
danielk1977c3f9bad2002-05-15 08:30:12 +0000564 }
565
drh4a324312001-12-21 14:30:42 +0000566 /* Push the record number for the new entry onto the stack. The
drhf0863fe2005-06-12 21:35:51 +0000567 ** record number is a randomly generate integer created by NewRowid
drh4a324312001-12-21 14:30:42 +0000568 ** except when the table has an INTEGER PRIMARY KEY column, in which
drhb419a922002-01-30 16:17:23 +0000569 ** case the record number is the same as that column.
drh1ccde152000-06-17 13:12:39 +0000570 */
drh5cf590c2003-04-24 01:45:04 +0000571 if( !isView ){
danielk1977c3f9bad2002-05-15 08:30:12 +0000572 if( keyColumn>=0 ){
drh142e30d2002-08-28 03:00:58 +0000573 if( useTempTable ){
danielk19774adee202004-05-08 08:23:19 +0000574 sqlite3VdbeAddOp(v, OP_Column, srcTab, keyColumn);
drh142e30d2002-08-28 03:00:58 +0000575 }else if( pSelect ){
danielk19774adee202004-05-08 08:23:19 +0000576 sqlite3VdbeAddOp(v, OP_Dup, nColumn - keyColumn - 1, 1);
danielk1977c3f9bad2002-05-15 08:30:12 +0000577 }else{
danielk19774adee202004-05-08 08:23:19 +0000578 sqlite3ExprCode(pParse, pList->a[keyColumn].pExpr);
danielk1977c3f9bad2002-05-15 08:30:12 +0000579 }
drhf0863fe2005-06-12 21:35:51 +0000580 /* If the PRIMARY KEY expression is NULL, then use OP_NewRowid
drh27a32782002-06-19 20:32:43 +0000581 ** to generate a unique primary key value.
582 */
danielk19774adee202004-05-08 08:23:19 +0000583 sqlite3VdbeAddOp(v, OP_NotNull, -1, sqlite3VdbeCurrentAddr(v)+3);
584 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
drhf0863fe2005-06-12 21:35:51 +0000585 sqlite3VdbeAddOp(v, OP_NewRowid, base, counterMem);
danielk19774adee202004-05-08 08:23:19 +0000586 sqlite3VdbeAddOp(v, OP_MustBeInt, 0, 0);
danielk1977c3f9bad2002-05-15 08:30:12 +0000587 }else{
drhf0863fe2005-06-12 21:35:51 +0000588 sqlite3VdbeAddOp(v, OP_NewRowid, base, counterMem);
drh4a324312001-12-21 14:30:42 +0000589 }
drh2958a4e2004-11-12 03:56:15 +0000590#ifndef SQLITE_OMIT_AUTOINCREMENT
591 if( pTab->autoInc ){
592 sqlite3VdbeAddOp(v, OP_MemMax, counterMem, 0);
593 }
594#endif /* SQLITE_OMIT_AUTOINCREMENT */
drh4a324312001-12-21 14:30:42 +0000595
danielk1977c3f9bad2002-05-15 08:30:12 +0000596 /* Push onto the stack, data for all columns of the new entry, beginning
danielk1977f29ce552002-05-19 23:43:12 +0000597 ** with the first column.
598 */
danielk1977c3f9bad2002-05-15 08:30:12 +0000599 for(i=0; i<pTab->nCol; i++){
600 if( i==pTab->iPKey ){
drh9adf9ac2002-05-15 11:44:13 +0000601 /* The value of the INTEGER PRIMARY KEY column is always a NULL.
danielk1977f29ce552002-05-19 23:43:12 +0000602 ** Whenever this column is read, the record number will be substituted
603 ** in its place. So will fill this column with a NULL to avoid
604 ** taking up data space with information that will never be used. */
drhf0863fe2005-06-12 21:35:51 +0000605 sqlite3VdbeAddOp(v, OP_Null, 0, 0);
drh9adf9ac2002-05-15 11:44:13 +0000606 continue;
danielk1977c3f9bad2002-05-15 08:30:12 +0000607 }
608 if( pColumn==0 ){
drh9adf9ac2002-05-15 11:44:13 +0000609 j = i;
danielk1977c3f9bad2002-05-15 08:30:12 +0000610 }else{
drh9adf9ac2002-05-15 11:44:13 +0000611 for(j=0; j<pColumn->nId; j++){
612 if( pColumn->a[j].idx==i ) break;
613 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000614 }
615 if( pColumn && j>=pColumn->nId ){
danielk19777977a172004-11-09 12:44:37 +0000616 sqlite3ExprCode(pParse, pTab->aCol[i].pDflt);
drh142e30d2002-08-28 03:00:58 +0000617 }else if( useTempTable ){
danielk19774adee202004-05-08 08:23:19 +0000618 sqlite3VdbeAddOp(v, OP_Column, srcTab, j);
drh142e30d2002-08-28 03:00:58 +0000619 }else if( pSelect ){
danielk19774adee202004-05-08 08:23:19 +0000620 sqlite3VdbeAddOp(v, OP_Dup, i+nColumn-j, 1);
danielk1977c3f9bad2002-05-15 08:30:12 +0000621 }else{
danielk19774adee202004-05-08 08:23:19 +0000622 sqlite3ExprCode(pParse, pList->a[j].pExpr);
drh5974a302000-06-07 14:42:26 +0000623 }
drhbed86902000-06-02 13:27:59 +0000624 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000625
626 /* Generate code to check constraints and generate index keys and
danielk1977f29ce552002-05-19 23:43:12 +0000627 ** do the insertion.
628 */
danielk19774adee202004-05-08 08:23:19 +0000629 sqlite3GenerateConstraintChecks(pParse, pTab, base, 0, keyColumn>=0,
drha0217ba2003-06-01 01:10:33 +0000630 0, onError, endOfLoop);
danielk19774adee202004-05-08 08:23:19 +0000631 sqlite3CompleteInsertion(pParse, pTab, base, 0,0,0,
drhdca76842004-12-07 14:06:13 +0000632 (triggers_exist & TRIGGER_AFTER)!=0 ? newIdx : -1);
drh5cf590c2003-04-24 01:45:04 +0000633 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000634
drh5cf590c2003-04-24 01:45:04 +0000635 /* Update the count of rows that are inserted
636 */
637 if( (db->flags & SQLITE_CountRows)!=0 ){
danielk19774adee202004-05-08 08:23:19 +0000638 sqlite3VdbeAddOp(v, OP_MemIncr, iCntMem, 0);
drh5974a302000-06-07 14:42:26 +0000639 }
drh1ccde152000-06-17 13:12:39 +0000640
drhdca76842004-12-07 14:06:13 +0000641 if( triggers_exist ){
danielk1977c3f9bad2002-05-15 08:30:12 +0000642 /* Close all tables opened */
drh5cf590c2003-04-24 01:45:04 +0000643 if( !isView ){
danielk19774adee202004-05-08 08:23:19 +0000644 sqlite3VdbeAddOp(v, OP_Close, base, 0);
danielk1977c3f9bad2002-05-15 08:30:12 +0000645 for(idx=1, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, idx++){
danielk19774adee202004-05-08 08:23:19 +0000646 sqlite3VdbeAddOp(v, OP_Close, idx+base, 0);
danielk1977c3f9bad2002-05-15 08:30:12 +0000647 }
648 }
drh1bee3d72001-10-15 00:44:35 +0000649
danielk1977c3f9bad2002-05-15 08:30:12 +0000650 /* Code AFTER triggers */
drhdca76842004-12-07 14:06:13 +0000651 if( sqlite3CodeRowTrigger(pParse, TK_INSERT, 0, TRIGGER_AFTER, pTab,
652 newIdx, -1, onError, endOfLoop) ){
danielk1977f29ce552002-05-19 23:43:12 +0000653 goto insert_cleanup;
654 }
drh1bee3d72001-10-15 00:44:35 +0000655 }
656
drh1ccde152000-06-17 13:12:39 +0000657 /* The bottom of the loop, if the data source is a SELECT statement
danielk1977f29ce552002-05-19 23:43:12 +0000658 */
danielk19774adee202004-05-08 08:23:19 +0000659 sqlite3VdbeResolveLabel(v, endOfLoop);
drh142e30d2002-08-28 03:00:58 +0000660 if( useTempTable ){
danielk19774adee202004-05-08 08:23:19 +0000661 sqlite3VdbeAddOp(v, OP_Next, srcTab, iCont);
662 sqlite3VdbeResolveLabel(v, iBreak);
663 sqlite3VdbeAddOp(v, OP_Close, srcTab, 0);
drh142e30d2002-08-28 03:00:58 +0000664 }else if( pSelect ){
danielk19774adee202004-05-08 08:23:19 +0000665 sqlite3VdbeAddOp(v, OP_Pop, nColumn, 0);
666 sqlite3VdbeAddOp(v, OP_Return, 0, 0);
667 sqlite3VdbeResolveLabel(v, iCleanup);
drh6b563442001-11-07 16:48:26 +0000668 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000669
drhdca76842004-12-07 14:06:13 +0000670 if( !triggers_exist ){
danielk1977c3f9bad2002-05-15 08:30:12 +0000671 /* Close all tables opened */
danielk19774adee202004-05-08 08:23:19 +0000672 sqlite3VdbeAddOp(v, OP_Close, base, 0);
danielk1977c3f9bad2002-05-15 08:30:12 +0000673 for(idx=1, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, idx++){
danielk19774adee202004-05-08 08:23:19 +0000674 sqlite3VdbeAddOp(v, OP_Close, idx+base, 0);
danielk1977c3f9bad2002-05-15 08:30:12 +0000675 }
drhcce7d172000-05-31 15:34:51 +0000676 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000677
drh2958a4e2004-11-12 03:56:15 +0000678#ifndef SQLITE_OMIT_AUTOINCREMENT
drhf3388142004-11-13 03:48:06 +0000679 /* Update the sqlite_sequence table by storing the content of the
680 ** counter value in memory counterMem back into the sqlite_sequence
681 ** table.
drh2958a4e2004-11-12 03:56:15 +0000682 */
683 if( pTab->autoInc ){
drhf3388142004-11-13 03:48:06 +0000684 int iCur = pParse->nTab;
685 int base = sqlite3VdbeCurrentAddr(v);
686 sqlite3VdbeAddOp(v, OP_Integer, pTab->iDb, 0);
687 sqlite3VdbeAddOp(v, OP_OpenWrite, iCur, pDb->pSeqTab->tnum);
688 sqlite3VdbeAddOp(v, OP_SetNumColumns, iCur, 2);
689 sqlite3VdbeAddOp(v, OP_MemLoad, counterRowid, 0);
690 sqlite3VdbeAddOp(v, OP_NotNull, -1, base+7);
691 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
drhf0863fe2005-06-12 21:35:51 +0000692 sqlite3VdbeAddOp(v, OP_NewRowid, iCur, 0);
drhf3388142004-11-13 03:48:06 +0000693 sqlite3VdbeOp3(v, OP_String8, 0, 0, pTab->zName, 0);
694 sqlite3VdbeAddOp(v, OP_MemLoad, counterMem, 0);
695 sqlite3VdbeAddOp(v, OP_MakeRecord, 2, 0);
drhf0863fe2005-06-12 21:35:51 +0000696 sqlite3VdbeAddOp(v, OP_Insert, iCur, 0);
drhf3388142004-11-13 03:48:06 +0000697 sqlite3VdbeAddOp(v, OP_Close, iCur, 0);
drh2958a4e2004-11-12 03:56:15 +0000698 }
699#endif
700
drh1bee3d72001-10-15 00:44:35 +0000701 /*
danielk1977e7de6f22004-11-05 06:02:06 +0000702 ** Return the number of rows inserted. If this routine is
703 ** generating code because of a call to sqlite3NestedParse(), do not
704 ** invoke the callback function.
drh1bee3d72001-10-15 00:44:35 +0000705 */
danielk1977cc6bd382005-01-10 02:48:49 +0000706 if( db->flags & SQLITE_CountRows && pParse->nested==0 && !pParse->trigStack ){
danielk19774adee202004-05-08 08:23:19 +0000707 sqlite3VdbeAddOp(v, OP_MemLoad, iCntMem, 0);
708 sqlite3VdbeAddOp(v, OP_Callback, 1, 0);
danielk197722322fd2004-05-25 23:35:17 +0000709 sqlite3VdbeSetNumCols(v, 1);
danielk19773cf86062004-05-26 10:11:05 +0000710 sqlite3VdbeSetColName(v, 0, "rows inserted", P3_STATIC);
drh1bee3d72001-10-15 00:44:35 +0000711 }
drhcce7d172000-05-31 15:34:51 +0000712
713insert_cleanup:
danielk19774adee202004-05-08 08:23:19 +0000714 sqlite3SrcListDelete(pTabList);
danielk1977d5d56522005-03-16 12:15:20 +0000715 sqlite3ExprListDelete(pList);
716 sqlite3SelectDelete(pSelect);
danielk19774adee202004-05-08 08:23:19 +0000717 sqlite3IdListDelete(pColumn);
drhcce7d172000-05-31 15:34:51 +0000718}
drh9cfcf5d2002-01-29 18:41:24 +0000719
drh9cfcf5d2002-01-29 18:41:24 +0000720/*
721** Generate code to do a constraint check prior to an INSERT or an UPDATE.
722**
723** When this routine is called, the stack contains (from bottom to top)
drh0ca3e242002-01-29 23:07:02 +0000724** the following values:
725**
drhf0863fe2005-06-12 21:35:51 +0000726** 1. The rowid of the row to be updated before the update. This
drhb419a922002-01-30 16:17:23 +0000727** value is omitted unless we are doing an UPDATE that involves a
728** change to the record number.
drh0ca3e242002-01-29 23:07:02 +0000729**
drhf0863fe2005-06-12 21:35:51 +0000730** 2. The rowid of the row after the update.
drh0ca3e242002-01-29 23:07:02 +0000731**
732** 3. The data in the first column of the entry after the update.
733**
734** i. Data from middle columns...
735**
736** N. The data in the last column of the entry after the update.
737**
drhf0863fe2005-06-12 21:35:51 +0000738** The old rowid shown as entry (1) above is omitted unless both isUpdate
739** and rowidChng are 1. isUpdate is true for UPDATEs and false for
740** INSERTs and rowidChng is true if the record number is being changed.
drh0ca3e242002-01-29 23:07:02 +0000741**
742** The code generated by this routine pushes additional entries onto
743** the stack which are the keys for new index entries for the new record.
744** The order of index keys is the same as the order of the indices on
745** the pTable->pIndex list. A key is only created for index i if
746** aIdxUsed!=0 and aIdxUsed[i]!=0.
drh9cfcf5d2002-01-29 18:41:24 +0000747**
748** This routine also generates code to check constraints. NOT NULL,
749** CHECK, and UNIQUE constraints are all checked. If a constraint fails,
drh1c928532002-01-31 15:54:21 +0000750** then the appropriate action is performed. There are five possible
751** actions: ROLLBACK, ABORT, FAIL, REPLACE, and IGNORE.
drh9cfcf5d2002-01-29 18:41:24 +0000752**
753** Constraint type Action What Happens
754** --------------- ---------- ----------------------------------------
drh1c928532002-01-31 15:54:21 +0000755** any ROLLBACK The current transaction is rolled back and
danielk197724b03fd2004-05-10 10:34:34 +0000756** sqlite3_exec() returns immediately with a
drh9cfcf5d2002-01-29 18:41:24 +0000757** return code of SQLITE_CONSTRAINT.
758**
drh1c928532002-01-31 15:54:21 +0000759** any ABORT Back out changes from the current command
760** only (do not do a complete rollback) then
danielk197724b03fd2004-05-10 10:34:34 +0000761** cause sqlite3_exec() to return immediately
drh1c928532002-01-31 15:54:21 +0000762** with SQLITE_CONSTRAINT.
763**
764** any FAIL Sqlite_exec() returns immediately with a
765** return code of SQLITE_CONSTRAINT. The
766** transaction is not rolled back and any
767** prior changes are retained.
768**
drh9cfcf5d2002-01-29 18:41:24 +0000769** any IGNORE The record number and data is popped from
770** the stack and there is an immediate jump
771** to label ignoreDest.
772**
773** NOT NULL REPLACE The NULL value is replace by the default
774** value for that column. If the default value
775** is NULL, the action is the same as ABORT.
776**
777** UNIQUE REPLACE The other row that conflicts with the row
778** being inserted is removed.
779**
780** CHECK REPLACE Illegal. The results in an exception.
781**
drh1c928532002-01-31 15:54:21 +0000782** Which action to take is determined by the overrideError parameter.
783** Or if overrideError==OE_Default, then the pParse->onError parameter
784** is used. Or if pParse->onError==OE_Default then the onError value
785** for the constraint is used.
drh9cfcf5d2002-01-29 18:41:24 +0000786**
drhaaab5722002-02-19 13:39:21 +0000787** The calling routine must open a read/write cursor for pTab with
drh9cfcf5d2002-01-29 18:41:24 +0000788** cursor number "base". All indices of pTab must also have open
789** read/write cursors with cursor number base+i for the i-th cursor.
790** Except, if there is no possibility of a REPLACE action then
791** cursors do not need to be open for indices where aIdxUsed[i]==0.
792**
793** If the isUpdate flag is true, it means that the "base" cursor is
794** initially pointing to an entry that is being updated. The isUpdate
795** flag causes extra code to be generated so that the "base" cursor
796** is still pointing at the same entry after the routine returns.
797** Without the isUpdate flag, the "base" cursor might be moved.
798*/
danielk19774adee202004-05-08 08:23:19 +0000799void sqlite3GenerateConstraintChecks(
drh9cfcf5d2002-01-29 18:41:24 +0000800 Parse *pParse, /* The parser context */
801 Table *pTab, /* the table into which we are inserting */
802 int base, /* Index of a read/write cursor pointing at pTab */
803 char *aIdxUsed, /* Which indices are used. NULL means all are used */
drhf0863fe2005-06-12 21:35:51 +0000804 int rowidChng, /* True if the record number will change */
drhb419a922002-01-30 16:17:23 +0000805 int isUpdate, /* True for UPDATE, False for INSERT */
drh9cfcf5d2002-01-29 18:41:24 +0000806 int overrideError, /* Override onError to this if not OE_Default */
drhb419a922002-01-30 16:17:23 +0000807 int ignoreDest /* Jump to this label on an OE_Ignore resolution */
drh9cfcf5d2002-01-29 18:41:24 +0000808){
809 int i;
810 Vdbe *v;
811 int nCol;
812 int onError;
813 int addr;
814 int extra;
drh0ca3e242002-01-29 23:07:02 +0000815 int iCur;
816 Index *pIdx;
817 int seenReplace = 0;
danielk1977cfe9a692004-06-16 12:00:29 +0000818 int jumpInst1=0, jumpInst2;
drhf0863fe2005-06-12 21:35:51 +0000819 int hasTwoRowids = (isUpdate && rowidChng);
drh9cfcf5d2002-01-29 18:41:24 +0000820
danielk19774adee202004-05-08 08:23:19 +0000821 v = sqlite3GetVdbe(pParse);
drh9cfcf5d2002-01-29 18:41:24 +0000822 assert( v!=0 );
drh417be792002-03-03 18:59:40 +0000823 assert( pTab->pSelect==0 ); /* This table is not a VIEW */
drh9cfcf5d2002-01-29 18:41:24 +0000824 nCol = pTab->nCol;
825
826 /* Test all NOT NULL constraints.
827 */
828 for(i=0; i<nCol; i++){
drh0ca3e242002-01-29 23:07:02 +0000829 if( i==pTab->iPKey ){
drh0ca3e242002-01-29 23:07:02 +0000830 continue;
831 }
drh9cfcf5d2002-01-29 18:41:24 +0000832 onError = pTab->aCol[i].notNull;
drh0ca3e242002-01-29 23:07:02 +0000833 if( onError==OE_None ) continue;
drh9cfcf5d2002-01-29 18:41:24 +0000834 if( overrideError!=OE_Default ){
835 onError = overrideError;
drha996e472003-05-16 02:30:27 +0000836 }else if( onError==OE_Default ){
837 onError = OE_Abort;
drh9cfcf5d2002-01-29 18:41:24 +0000838 }
danielk19777977a172004-11-09 12:44:37 +0000839 if( onError==OE_Replace && pTab->aCol[i].pDflt==0 ){
drh9cfcf5d2002-01-29 18:41:24 +0000840 onError = OE_Abort;
841 }
danielk19774adee202004-05-08 08:23:19 +0000842 sqlite3VdbeAddOp(v, OP_Dup, nCol-1-i, 1);
843 addr = sqlite3VdbeAddOp(v, OP_NotNull, 1, 0);
danielk1977b84f96f2005-01-20 11:32:23 +0000844 assert( onError==OE_Rollback || onError==OE_Abort || onError==OE_Fail
845 || onError==OE_Ignore || onError==OE_Replace );
drh9cfcf5d2002-01-29 18:41:24 +0000846 switch( onError ){
drh1c928532002-01-31 15:54:21 +0000847 case OE_Rollback:
848 case OE_Abort:
849 case OE_Fail: {
drh483750b2003-01-29 18:46:51 +0000850 char *zMsg = 0;
danielk19774adee202004-05-08 08:23:19 +0000851 sqlite3VdbeAddOp(v, OP_Halt, SQLITE_CONSTRAINT, onError);
852 sqlite3SetString(&zMsg, pTab->zName, ".", pTab->aCol[i].zName,
drh41743982003-12-06 21:43:55 +0000853 " may not be NULL", (char*)0);
danielk19774adee202004-05-08 08:23:19 +0000854 sqlite3VdbeChangeP3(v, -1, zMsg, P3_DYNAMIC);
drh9cfcf5d2002-01-29 18:41:24 +0000855 break;
856 }
857 case OE_Ignore: {
drhf0863fe2005-06-12 21:35:51 +0000858 sqlite3VdbeAddOp(v, OP_Pop, nCol+1+hasTwoRowids, 0);
danielk19774adee202004-05-08 08:23:19 +0000859 sqlite3VdbeAddOp(v, OP_Goto, 0, ignoreDest);
drh9cfcf5d2002-01-29 18:41:24 +0000860 break;
861 }
862 case OE_Replace: {
danielk19777977a172004-11-09 12:44:37 +0000863 sqlite3ExprCode(pParse, pTab->aCol[i].pDflt);
danielk19774adee202004-05-08 08:23:19 +0000864 sqlite3VdbeAddOp(v, OP_Push, nCol-i, 0);
drh9cfcf5d2002-01-29 18:41:24 +0000865 break;
866 }
drh9cfcf5d2002-01-29 18:41:24 +0000867 }
drhd654be82005-09-20 17:42:23 +0000868 sqlite3VdbeJumpHere(v, addr);
drh9cfcf5d2002-01-29 18:41:24 +0000869 }
870
871 /* Test all CHECK constraints
872 */
drh0bd1f4e2002-06-06 18:54:39 +0000873 /**** TBD ****/
drh9cfcf5d2002-01-29 18:41:24 +0000874
drh0bd1f4e2002-06-06 18:54:39 +0000875 /* If we have an INTEGER PRIMARY KEY, make sure the primary key
876 ** of the new record does not previously exist. Except, if this
877 ** is an UPDATE and the primary key is not changing, that is OK.
drh9cfcf5d2002-01-29 18:41:24 +0000878 */
drhf0863fe2005-06-12 21:35:51 +0000879 if( rowidChng ){
drh0ca3e242002-01-29 23:07:02 +0000880 onError = pTab->keyConf;
881 if( overrideError!=OE_Default ){
882 onError = overrideError;
drha996e472003-05-16 02:30:27 +0000883 }else if( onError==OE_Default ){
884 onError = OE_Abort;
drh0ca3e242002-01-29 23:07:02 +0000885 }
drha0217ba2003-06-01 01:10:33 +0000886
drh5383ae52003-06-04 12:23:30 +0000887 if( isUpdate ){
danielk19774adee202004-05-08 08:23:19 +0000888 sqlite3VdbeAddOp(v, OP_Dup, nCol+1, 1);
889 sqlite3VdbeAddOp(v, OP_Dup, nCol+1, 1);
890 jumpInst1 = sqlite3VdbeAddOp(v, OP_Eq, 0, 0);
drh5383ae52003-06-04 12:23:30 +0000891 }
danielk19774adee202004-05-08 08:23:19 +0000892 sqlite3VdbeAddOp(v, OP_Dup, nCol, 1);
893 jumpInst2 = sqlite3VdbeAddOp(v, OP_NotExists, base, 0);
drh5383ae52003-06-04 12:23:30 +0000894 switch( onError ){
895 default: {
896 onError = OE_Abort;
897 /* Fall thru into the next case */
drh79b0c952002-05-21 12:56:43 +0000898 }
drh5383ae52003-06-04 12:23:30 +0000899 case OE_Rollback:
900 case OE_Abort:
901 case OE_Fail: {
danielk19774adee202004-05-08 08:23:19 +0000902 sqlite3VdbeOp3(v, OP_Halt, SQLITE_CONSTRAINT, onError,
drh701a0ae2004-02-22 20:05:00 +0000903 "PRIMARY KEY must be unique", P3_STATIC);
drh5383ae52003-06-04 12:23:30 +0000904 break;
drh0ca3e242002-01-29 23:07:02 +0000905 }
drh5383ae52003-06-04 12:23:30 +0000906 case OE_Replace: {
danielk19774adee202004-05-08 08:23:19 +0000907 sqlite3GenerateRowIndexDelete(pParse->db, v, pTab, base, 0);
drh5383ae52003-06-04 12:23:30 +0000908 if( isUpdate ){
drhf0863fe2005-06-12 21:35:51 +0000909 sqlite3VdbeAddOp(v, OP_Dup, nCol+hasTwoRowids, 1);
drh7cf6e4d2004-05-19 14:56:55 +0000910 sqlite3VdbeAddOp(v, OP_MoveGe, base, 0);
drh5383ae52003-06-04 12:23:30 +0000911 }
912 seenReplace = 1;
913 break;
drh0ca3e242002-01-29 23:07:02 +0000914 }
drh5383ae52003-06-04 12:23:30 +0000915 case OE_Ignore: {
916 assert( seenReplace==0 );
drhf0863fe2005-06-12 21:35:51 +0000917 sqlite3VdbeAddOp(v, OP_Pop, nCol+1+hasTwoRowids, 0);
danielk19774adee202004-05-08 08:23:19 +0000918 sqlite3VdbeAddOp(v, OP_Goto, 0, ignoreDest);
drh5383ae52003-06-04 12:23:30 +0000919 break;
920 }
921 }
drhd654be82005-09-20 17:42:23 +0000922 sqlite3VdbeJumpHere(v, jumpInst2);
drh5383ae52003-06-04 12:23:30 +0000923 if( isUpdate ){
drhd654be82005-09-20 17:42:23 +0000924 sqlite3VdbeJumpHere(v, jumpInst1);
danielk19774adee202004-05-08 08:23:19 +0000925 sqlite3VdbeAddOp(v, OP_Dup, nCol+1, 1);
drh7cf6e4d2004-05-19 14:56:55 +0000926 sqlite3VdbeAddOp(v, OP_MoveGe, base, 0);
drh0ca3e242002-01-29 23:07:02 +0000927 }
928 }
drh0bd1f4e2002-06-06 18:54:39 +0000929
930 /* Test all UNIQUE constraints by creating entries for each UNIQUE
931 ** index and making sure that duplicate entries do not already exist.
932 ** Add the new records to the indices as we go.
933 */
drhb2fe7d82003-04-20 17:29:23 +0000934 extra = -1;
935 for(iCur=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, iCur++){
936 if( aIdxUsed && aIdxUsed[iCur]==0 ) continue; /* Skip unused indices */
937 extra++;
938
939 /* Create a key for accessing the index entry */
danielk19774adee202004-05-08 08:23:19 +0000940 sqlite3VdbeAddOp(v, OP_Dup, nCol+extra, 1);
drh9cfcf5d2002-01-29 18:41:24 +0000941 for(i=0; i<pIdx->nColumn; i++){
942 int idx = pIdx->aiColumn[i];
943 if( idx==pTab->iPKey ){
danielk19774adee202004-05-08 08:23:19 +0000944 sqlite3VdbeAddOp(v, OP_Dup, i+extra+nCol+1, 1);
drh9cfcf5d2002-01-29 18:41:24 +0000945 }else{
danielk19774adee202004-05-08 08:23:19 +0000946 sqlite3VdbeAddOp(v, OP_Dup, i+extra+nCol-idx, 1);
drh9cfcf5d2002-01-29 18:41:24 +0000947 }
948 }
drh7f057c92005-06-24 03:53:06 +0000949 jumpInst1 = sqlite3VdbeAddOp(v, OP_MakeIdxRec, pIdx->nColumn, 0);
danielk1977a37cdde2004-05-16 11:15:36 +0000950 sqlite3IndexAffinityStr(v, pIdx);
drhb2fe7d82003-04-20 17:29:23 +0000951
952 /* Find out what action to take in case there is an indexing conflict */
drh9cfcf5d2002-01-29 18:41:24 +0000953 onError = pIdx->onError;
drhb2fe7d82003-04-20 17:29:23 +0000954 if( onError==OE_None ) continue; /* pIdx is not a UNIQUE index */
drh9cfcf5d2002-01-29 18:41:24 +0000955 if( overrideError!=OE_Default ){
956 onError = overrideError;
drha996e472003-05-16 02:30:27 +0000957 }else if( onError==OE_Default ){
958 onError = OE_Abort;
drh9cfcf5d2002-01-29 18:41:24 +0000959 }
drh5383ae52003-06-04 12:23:30 +0000960 if( seenReplace ){
961 if( onError==OE_Ignore ) onError = OE_Replace;
962 else if( onError==OE_Fail ) onError = OE_Abort;
963 }
964
drhb2fe7d82003-04-20 17:29:23 +0000965
966 /* Check to see if the new index entry will be unique */
drhf0863fe2005-06-12 21:35:51 +0000967 sqlite3VdbeAddOp(v, OP_Dup, extra+nCol+1+hasTwoRowids, 1);
danielk19774adee202004-05-08 08:23:19 +0000968 jumpInst2 = sqlite3VdbeAddOp(v, OP_IsUnique, base+iCur+1, 0);
drhb2fe7d82003-04-20 17:29:23 +0000969
970 /* Generate code that executes if the new index entry is not unique */
danielk1977b84f96f2005-01-20 11:32:23 +0000971 assert( onError==OE_Rollback || onError==OE_Abort || onError==OE_Fail
972 || onError==OE_Ignore || onError==OE_Replace );
drh9cfcf5d2002-01-29 18:41:24 +0000973 switch( onError ){
drh1c928532002-01-31 15:54:21 +0000974 case OE_Rollback:
975 case OE_Abort:
976 case OE_Fail: {
drh37ed48e2003-08-05 13:13:38 +0000977 int j, n1, n2;
978 char zErrMsg[200];
979 strcpy(zErrMsg, pIdx->nColumn>1 ? "columns " : "column ");
980 n1 = strlen(zErrMsg);
981 for(j=0; j<pIdx->nColumn && n1<sizeof(zErrMsg)-30; j++){
982 char *zCol = pTab->aCol[pIdx->aiColumn[j]].zName;
983 n2 = strlen(zCol);
984 if( j>0 ){
985 strcpy(&zErrMsg[n1], ", ");
986 n1 += 2;
987 }
988 if( n1+n2>sizeof(zErrMsg)-30 ){
989 strcpy(&zErrMsg[n1], "...");
990 n1 += 3;
991 break;
992 }else{
993 strcpy(&zErrMsg[n1], zCol);
994 n1 += n2;
995 }
996 }
997 strcpy(&zErrMsg[n1],
998 pIdx->nColumn>1 ? " are not unique" : " is not unique");
danielk19774adee202004-05-08 08:23:19 +0000999 sqlite3VdbeOp3(v, OP_Halt, SQLITE_CONSTRAINT, onError, zErrMsg, 0);
drh9cfcf5d2002-01-29 18:41:24 +00001000 break;
1001 }
1002 case OE_Ignore: {
drh0ca3e242002-01-29 23:07:02 +00001003 assert( seenReplace==0 );
drhf0863fe2005-06-12 21:35:51 +00001004 sqlite3VdbeAddOp(v, OP_Pop, nCol+extra+3+hasTwoRowids, 0);
danielk19774adee202004-05-08 08:23:19 +00001005 sqlite3VdbeAddOp(v, OP_Goto, 0, ignoreDest);
drh9cfcf5d2002-01-29 18:41:24 +00001006 break;
1007 }
1008 case OE_Replace: {
danielk19774adee202004-05-08 08:23:19 +00001009 sqlite3GenerateRowDelete(pParse->db, v, pTab, base, 0);
drh9cfcf5d2002-01-29 18:41:24 +00001010 if( isUpdate ){
drhf0863fe2005-06-12 21:35:51 +00001011 sqlite3VdbeAddOp(v, OP_Dup, nCol+extra+1+hasTwoRowids, 1);
drh7cf6e4d2004-05-19 14:56:55 +00001012 sqlite3VdbeAddOp(v, OP_MoveGe, base, 0);
drh9cfcf5d2002-01-29 18:41:24 +00001013 }
drh0ca3e242002-01-29 23:07:02 +00001014 seenReplace = 1;
drh9cfcf5d2002-01-29 18:41:24 +00001015 break;
1016 }
drh9cfcf5d2002-01-29 18:41:24 +00001017 }
drh0bd1f4e2002-06-06 18:54:39 +00001018#if NULL_DISTINCT_FOR_UNIQUE
drhd654be82005-09-20 17:42:23 +00001019 sqlite3VdbeJumpHere(v, jumpInst1);
drh0bd1f4e2002-06-06 18:54:39 +00001020#endif
drhd654be82005-09-20 17:42:23 +00001021 sqlite3VdbeJumpHere(v, jumpInst2);
drh9cfcf5d2002-01-29 18:41:24 +00001022 }
1023}
drh0ca3e242002-01-29 23:07:02 +00001024
1025/*
1026** This routine generates code to finish the INSERT or UPDATE operation
danielk19774adee202004-05-08 08:23:19 +00001027** that was started by a prior call to sqlite3GenerateConstraintChecks.
drh0ca3e242002-01-29 23:07:02 +00001028** The stack must contain keys for all active indices followed by data
drhf0863fe2005-06-12 21:35:51 +00001029** and the rowid for the new entry. This routine creates the new
drh0ca3e242002-01-29 23:07:02 +00001030** entries in all indices and in the main table.
1031**
drhb419a922002-01-30 16:17:23 +00001032** The arguments to this routine should be the same as the first six
danielk19774adee202004-05-08 08:23:19 +00001033** arguments to sqlite3GenerateConstraintChecks.
drh0ca3e242002-01-29 23:07:02 +00001034*/
danielk19774adee202004-05-08 08:23:19 +00001035void sqlite3CompleteInsertion(
drh0ca3e242002-01-29 23:07:02 +00001036 Parse *pParse, /* The parser context */
1037 Table *pTab, /* the table into which we are inserting */
1038 int base, /* Index of a read/write cursor pointing at pTab */
1039 char *aIdxUsed, /* Which indices are used. NULL means all are used */
drhf0863fe2005-06-12 21:35:51 +00001040 int rowidChng, /* True if the record number will change */
drh70ce3f02003-04-15 19:22:22 +00001041 int isUpdate, /* True for UPDATE, False for INSERT */
1042 int newIdx /* Index of NEW table for triggers. -1 if none */
drh0ca3e242002-01-29 23:07:02 +00001043){
1044 int i;
1045 Vdbe *v;
1046 int nIdx;
1047 Index *pIdx;
danielk1977b28af712004-06-21 06:50:26 +00001048 int pik_flags;
drh0ca3e242002-01-29 23:07:02 +00001049
danielk19774adee202004-05-08 08:23:19 +00001050 v = sqlite3GetVdbe(pParse);
drh0ca3e242002-01-29 23:07:02 +00001051 assert( v!=0 );
drh417be792002-03-03 18:59:40 +00001052 assert( pTab->pSelect==0 ); /* This table is not a VIEW */
drh0ca3e242002-01-29 23:07:02 +00001053 for(nIdx=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, nIdx++){}
1054 for(i=nIdx-1; i>=0; i--){
1055 if( aIdxUsed && aIdxUsed[i]==0 ) continue;
drhf0863fe2005-06-12 21:35:51 +00001056 sqlite3VdbeAddOp(v, OP_IdxInsert, base+i+1, 0);
drh0ca3e242002-01-29 23:07:02 +00001057 }
danielk19774adee202004-05-08 08:23:19 +00001058 sqlite3VdbeAddOp(v, OP_MakeRecord, pTab->nCol, 0);
danielk1977a37cdde2004-05-16 11:15:36 +00001059 sqlite3TableAffinityStr(v, pTab);
danielk1977b84f96f2005-01-20 11:32:23 +00001060#ifndef SQLITE_OMIT_TRIGGER
drh70ce3f02003-04-15 19:22:22 +00001061 if( newIdx>=0 ){
danielk19774adee202004-05-08 08:23:19 +00001062 sqlite3VdbeAddOp(v, OP_Dup, 1, 0);
1063 sqlite3VdbeAddOp(v, OP_Dup, 1, 0);
drhf0863fe2005-06-12 21:35:51 +00001064 sqlite3VdbeAddOp(v, OP_Insert, newIdx, 0);
drh70ce3f02003-04-15 19:22:22 +00001065 }
danielk1977b84f96f2005-01-20 11:32:23 +00001066#endif
drh4794f732004-11-05 17:17:50 +00001067 if( pParse->nested ){
1068 pik_flags = 0;
1069 }else{
1070 pik_flags = (OPFLAG_NCHANGE|(isUpdate?0:OPFLAG_LASTROWID));
1071 }
drhf0863fe2005-06-12 21:35:51 +00001072 sqlite3VdbeAddOp(v, OP_Insert, base, pik_flags);
danielk1977b28af712004-06-21 06:50:26 +00001073
drhf0863fe2005-06-12 21:35:51 +00001074 if( isUpdate && rowidChng ){
danielk19774adee202004-05-08 08:23:19 +00001075 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
drh0ca3e242002-01-29 23:07:02 +00001076 }
1077}
drhcd446902004-02-24 01:05:31 +00001078
1079/*
drh290c1942004-08-21 17:54:45 +00001080** Generate code that will open cursors for a table and for all
drhcd446902004-02-24 01:05:31 +00001081** indices of that table. The "base" parameter is the cursor number used
1082** for the table. Indices are opened on subsequent cursors.
drhcd446902004-02-24 01:05:31 +00001083*/
drh290c1942004-08-21 17:54:45 +00001084void sqlite3OpenTableAndIndices(
1085 Parse *pParse, /* Parsing context */
1086 Table *pTab, /* Table to be opened */
1087 int base, /* Cursor number assigned to the table */
1088 int op /* OP_OpenRead or OP_OpenWrite */
1089){
drhcd446902004-02-24 01:05:31 +00001090 int i;
1091 Index *pIdx;
danielk19774adee202004-05-08 08:23:19 +00001092 Vdbe *v = sqlite3GetVdbe(pParse);
drhcd446902004-02-24 01:05:31 +00001093 assert( v!=0 );
danielk19774adee202004-05-08 08:23:19 +00001094 sqlite3VdbeAddOp(v, OP_Integer, pTab->iDb, 0);
drhad6d9462004-09-19 02:15:24 +00001095 VdbeComment((v, "# %s", pTab->zName));
drh29dda4a2005-07-21 18:23:20 +00001096 sqlite3VdbeAddOp(v, op, base, pTab->tnum);
danielk1977b4964b72004-05-18 01:23:38 +00001097 sqlite3VdbeAddOp(v, OP_SetNumColumns, base, pTab->nCol);
drhcd446902004-02-24 01:05:31 +00001098 for(i=1, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){
danielk19774adee202004-05-08 08:23:19 +00001099 sqlite3VdbeAddOp(v, OP_Integer, pIdx->iDb, 0);
drh29dda4a2005-07-21 18:23:20 +00001100 VdbeComment((v, "# %s", pIdx->zName));
drh290c1942004-08-21 17:54:45 +00001101 sqlite3VdbeOp3(v, op, i+base, pIdx->tnum,
drhd3d39e92004-05-20 22:16:29 +00001102 (char*)&pIdx->keyInfo, P3_KEYINFO);
drhcd446902004-02-24 01:05:31 +00001103 }
drh290c1942004-08-21 17:54:45 +00001104 if( pParse->nTab<=base+i ){
1105 pParse->nTab = base+i;
1106 }
drhcd446902004-02-24 01:05:31 +00001107}