blob: 33090b18e3053655a2672f5469b0311c5f47250c [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**
drh6275b882005-11-03 01:22:30 +000015** $Id: insert.c,v 1.146 2005/11/03 01:22:31 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
danielk19773d1bfea2004-05-14 11:00:53 +000027** 't' TEXT
28** 'o' NONE
29*/
danielk1977a37cdde2004-05-16 11:15:36 +000030void sqlite3IndexAffinityStr(Vdbe *v, Index *pIdx){
31 if( !pIdx->zColAff ){
danielk1977e014a832004-05-17 10:48:57 +000032 /* The first time a column affinity string for a particular index is
danielk1977a37cdde2004-05-16 11:15:36 +000033 ** required, it is allocated and populated here. It is then stored as
danielk1977e014a832004-05-17 10:48:57 +000034 ** a member of the Index structure for subsequent use.
danielk1977a37cdde2004-05-16 11:15:36 +000035 **
36 ** The column affinity string will eventually be deleted by
danielk1977e014a832004-05-17 10:48:57 +000037 ** sqliteDeleteIndex() when the Index structure itself is cleaned
danielk1977a37cdde2004-05-16 11:15:36 +000038 ** up.
39 */
40 int n;
41 Table *pTab = pIdx->pTable;
42 pIdx->zColAff = (char *)sqliteMalloc(pIdx->nColumn+1);
43 if( !pIdx->zColAff ){
44 return;
45 }
46 for(n=0; n<pIdx->nColumn; n++){
47 pIdx->zColAff[n] = pTab->aCol[pIdx->aiColumn[n]].affinity;
48 }
49 pIdx->zColAff[pIdx->nColumn] = '\0';
50 }
danielk19773d1bfea2004-05-14 11:00:53 +000051
drh956bc922004-07-24 17:38:29 +000052 sqlite3VdbeChangeP3(v, -1, pIdx->zColAff, 0);
danielk1977a37cdde2004-05-16 11:15:36 +000053}
54
55/*
56** Set P3 of the most recently inserted opcode to a column affinity
57** string for table pTab. A column affinity string has one character
58** for each column indexed by the index, according to the affinity of the
59** column:
60**
61** Character Column affinity
62** ------------------------------
63** 'n' NUMERIC
danielk1977a37cdde2004-05-16 11:15:36 +000064** 't' TEXT
65** 'o' NONE
66*/
67void sqlite3TableAffinityStr(Vdbe *v, Table *pTab){
danielk19773d1bfea2004-05-14 11:00:53 +000068 /* The first time a column affinity string for a particular table
69 ** is required, it is allocated and populated here. It is then
70 ** stored as a member of the Table structure for subsequent use.
71 **
72 ** The column affinity string will eventually be deleted by
73 ** sqlite3DeleteTable() when the Table structure itself is cleaned up.
74 */
75 if( !pTab->zColAff ){
76 char *zColAff;
77 int i;
78
danielk1977a37cdde2004-05-16 11:15:36 +000079 zColAff = (char *)sqliteMalloc(pTab->nCol+1);
danielk19773d1bfea2004-05-14 11:00:53 +000080 if( !zColAff ){
danielk1977a37cdde2004-05-16 11:15:36 +000081 return;
danielk19773d1bfea2004-05-14 11:00:53 +000082 }
83
84 for(i=0; i<pTab->nCol; i++){
danielk1977a37cdde2004-05-16 11:15:36 +000085 zColAff[i] = pTab->aCol[i].affinity;
danielk19773d1bfea2004-05-14 11:00:53 +000086 }
87 zColAff[pTab->nCol] = '\0';
88
89 pTab->zColAff = zColAff;
90 }
91
drh956bc922004-07-24 17:38:29 +000092 sqlite3VdbeChangeP3(v, -1, pTab->zColAff, 0);
danielk19773d1bfea2004-05-14 11:00:53 +000093}
94
danielk19774d887782005-02-08 08:42:27 +000095/*
96** Return non-zero if SELECT statement p opens the table with rootpage
97** iTab in database iDb. This is used to see if a statement of the form
98** "INSERT INTO <iDb, iTab> SELECT ..." can run without using temporary
99** table for the results of the SELECT.
100**
101** No checking is done for sub-selects that are part of expressions.
102*/
103static int selectReadsTable(Select *p, int iDb, int iTab){
104 int i;
105 struct SrcList_item *pItem;
106 if( p->pSrc==0 ) return 0;
107 for(i=0, pItem=p->pSrc->a; i<p->pSrc->nSrc; i++, pItem++){
108 if( pItem->pSelect ){
drh38fba692005-03-21 01:20:58 +0000109 if( selectReadsTable(pItem->pSelect, iDb, iTab) ) return 1;
danielk19774d887782005-02-08 08:42:27 +0000110 }else{
111 if( pItem->pTab->iDb==iDb && pItem->pTab->tnum==iTab ) return 1;
112 }
113 }
114 return 0;
115}
danielk19773d1bfea2004-05-14 11:00:53 +0000116
117/*
drh1ccde152000-06-17 13:12:39 +0000118** This routine is call to handle SQL of the following forms:
drhcce7d172000-05-31 15:34:51 +0000119**
120** insert into TABLE (IDLIST) values(EXPRLIST)
drh1ccde152000-06-17 13:12:39 +0000121** insert into TABLE (IDLIST) select
drhcce7d172000-05-31 15:34:51 +0000122**
drh1ccde152000-06-17 13:12:39 +0000123** The IDLIST following the table name is always optional. If omitted,
124** then a list of all columns for the table is substituted. The IDLIST
drh967e8b72000-06-21 13:59:10 +0000125** appears in the pColumn parameter. pColumn is NULL if IDLIST is omitted.
drh1ccde152000-06-17 13:12:39 +0000126**
127** The pList parameter holds EXPRLIST in the first form of the INSERT
128** statement above, and pSelect is NULL. For the second form, pList is
129** NULL and pSelect is a pointer to the select statement used to generate
130** data for the insert.
drh142e30d2002-08-28 03:00:58 +0000131**
132** The code generated follows one of three templates. For a simple
133** select with data coming from a VALUES clause, the code executes
134** once straight down through. The template looks like this:
135**
136** open write cursor to <table> and its indices
137** puts VALUES clause expressions onto the stack
138** write the resulting record into <table>
139** cleanup
140**
141** If the statement is of the form
142**
143** INSERT INTO <table> SELECT ...
144**
145** And the SELECT clause does not read from <table> at any time, then
146** the generated code follows this template:
147**
148** goto B
149** A: setup for the SELECT
150** loop over the tables in the SELECT
151** gosub C
152** end loop
153** cleanup after the SELECT
154** goto D
155** B: open write cursor to <table> and its indices
156** goto A
157** C: insert the select result into <table>
158** return
159** D: cleanup
160**
161** The third template is used if the insert statement takes its
162** values from a SELECT but the data is being inserted into a table
163** that is also read as part of the SELECT. In the third form,
164** we have to use a intermediate table to store the results of
165** the select. The template is like this:
166**
167** goto B
168** A: setup for the SELECT
169** loop over the tables in the SELECT
170** gosub C
171** end loop
172** cleanup after the SELECT
173** goto D
174** C: insert the select result into the intermediate table
175** return
176** B: open a cursor to an intermediate table
177** goto A
178** D: open write cursor to <table> and its indices
179** loop over the intermediate table
180** transfer values form intermediate table into <table>
181** end the loop
182** cleanup
drhcce7d172000-05-31 15:34:51 +0000183*/
danielk19774adee202004-05-08 08:23:19 +0000184void sqlite3Insert(
drhcce7d172000-05-31 15:34:51 +0000185 Parse *pParse, /* Parser context */
drh113088e2003-03-20 01:16:58 +0000186 SrcList *pTabList, /* Name of table into which we are inserting */
drhcce7d172000-05-31 15:34:51 +0000187 ExprList *pList, /* List of values to be inserted */
drh5974a302000-06-07 14:42:26 +0000188 Select *pSelect, /* A SELECT statement to use as the data source */
drh9cfcf5d2002-01-29 18:41:24 +0000189 IdList *pColumn, /* Column names corresponding to IDLIST. */
190 int onError /* How to handle constraint errors */
drhcce7d172000-05-31 15:34:51 +0000191){
drh5974a302000-06-07 14:42:26 +0000192 Table *pTab; /* The table to insert into */
drh113088e2003-03-20 01:16:58 +0000193 char *zTab; /* Name of the table into which we are inserting */
drhe22a3342003-04-22 20:30:37 +0000194 const char *zDb; /* Name of the database holding this table */
drh5974a302000-06-07 14:42:26 +0000195 int i, j, idx; /* Loop counters */
196 Vdbe *v; /* Generate code into this virtual machine */
197 Index *pIdx; /* For looping over indices of the table */
drh967e8b72000-06-21 13:59:10 +0000198 int nColumn; /* Number of columns in the data */
danielk1977cfe9a692004-06-16 12:00:29 +0000199 int base = 0; /* VDBE Cursor number for pTab */
200 int iCont=0,iBreak=0; /* Beginning and end of the loop over srcTab */
drh9bb575f2004-09-06 17:24:11 +0000201 sqlite3 *db; /* The main database structure */
drh4a324312001-12-21 14:30:42 +0000202 int keyColumn = -1; /* Column that is the INTEGER PRIMARY KEY */
drh0ca3e242002-01-29 23:07:02 +0000203 int endOfLoop; /* Label for the end of the insertion loop */
danielk19774d887782005-02-08 08:42:27 +0000204 int useTempTable = 0; /* Store SELECT results in intermediate table */
danielk1977cfe9a692004-06-16 12:00:29 +0000205 int srcTab = 0; /* Data comes from this temporary cursor if >=0 */
206 int iSelectLoop = 0; /* Address of code that implements the SELECT */
207 int iCleanup = 0; /* Address of the cleanup code */
208 int iInsertBlock = 0; /* Address of the subroutine used to insert data */
209 int iCntMem = 0; /* Memory cell used for the row counter */
drh798da522004-11-04 04:42:28 +0000210 int newIdx = -1; /* Cursor for the NEW table */
drh2958a4e2004-11-12 03:56:15 +0000211 Db *pDb; /* The database containing table being inserted into */
212 int counterMem = 0; /* Memory cell holding AUTOINCREMENT counter */
drhcce7d172000-05-31 15:34:51 +0000213
drh798da522004-11-04 04:42:28 +0000214#ifndef SQLITE_OMIT_TRIGGER
215 int isView; /* True if attempting to insert into a view */
drhdca76842004-12-07 14:06:13 +0000216 int triggers_exist = 0; /* True if there are FOR EACH ROW triggers */
drh798da522004-11-04 04:42:28 +0000217#endif
danielk1977c3f9bad2002-05-15 08:30:12 +0000218
drhf3388142004-11-13 03:48:06 +0000219#ifndef SQLITE_OMIT_AUTOINCREMENT
220 int counterRowid; /* Memory cell holding rowid of autoinc counter */
221#endif
222
danielk197724b03fd2004-05-10 10:34:34 +0000223 if( pParse->nErr || sqlite3_malloc_failed ) goto insert_cleanup;
drhecdc7532001-09-23 02:35:53 +0000224 db = pParse->db;
drhdaffd0e2001-04-11 14:28:42 +0000225
drh1ccde152000-06-17 13:12:39 +0000226 /* Locate the table into which we will be inserting new information.
227 */
drh113088e2003-03-20 01:16:58 +0000228 assert( pTabList->nSrc==1 );
229 zTab = pTabList->a[0].zName;
drhdaffd0e2001-04-11 14:28:42 +0000230 if( zTab==0 ) goto insert_cleanup;
danielk19774adee202004-05-08 08:23:19 +0000231 pTab = sqlite3SrcListLookup(pParse, pTabList);
danielk1977c3f9bad2002-05-15 08:30:12 +0000232 if( pTab==0 ){
danielk1977c3f9bad2002-05-15 08:30:12 +0000233 goto insert_cleanup;
234 }
drhe22a3342003-04-22 20:30:37 +0000235 assert( pTab->iDb<db->nDb );
drh2958a4e2004-11-12 03:56:15 +0000236 pDb = &db->aDb[pTab->iDb];
237 zDb = pDb->zName;
danielk19774adee202004-05-08 08:23:19 +0000238 if( sqlite3AuthCheck(pParse, SQLITE_INSERT, pTab->zName, 0, zDb) ){
drh1962bda2003-01-12 19:33:52 +0000239 goto insert_cleanup;
240 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000241
drhb7f91642004-10-31 02:22:47 +0000242 /* Figure out if we have any triggers and if the table being
243 ** inserted into is a view
244 */
245#ifndef SQLITE_OMIT_TRIGGER
drhdca76842004-12-07 14:06:13 +0000246 triggers_exist = sqlite3TriggersExist(pParse, pTab, TK_INSERT, 0);
drhb7f91642004-10-31 02:22:47 +0000247 isView = pTab->pSelect!=0;
248#else
drhdca76842004-12-07 14:06:13 +0000249# define triggers_exist 0
drhb7f91642004-10-31 02:22:47 +0000250# define isView 0
251#endif
252#ifdef SQLITE_OMIT_VIEW
253# undef isView
254# define isView 0
255#endif
256
danielk1977c3f9bad2002-05-15 08:30:12 +0000257 /* Ensure that:
258 * (a) the table is not read-only,
259 * (b) that if it is a view then ON INSERT triggers exist
260 */
drhdca76842004-12-07 14:06:13 +0000261 if( sqlite3IsReadOnly(pParse, pTab, triggers_exist) ){
danielk1977c3f9bad2002-05-15 08:30:12 +0000262 goto insert_cleanup;
263 }
drha76b5df2002-02-23 02:32:10 +0000264 if( pTab==0 ) goto insert_cleanup;
drh1ccde152000-06-17 13:12:39 +0000265
drhf573c992002-07-31 00:32:50 +0000266 /* If pTab is really a view, make sure it has been initialized.
267 */
danielk19774adee202004-05-08 08:23:19 +0000268 if( isView && sqlite3ViewGetColumnNames(pParse, pTab) ){
drh5cf590c2003-04-24 01:45:04 +0000269 goto insert_cleanup;
drhf573c992002-07-31 00:32:50 +0000270 }
271
danielk19777cedc8d2004-06-10 10:50:08 +0000272 /* Ensure all required collation sequences are available. */
273 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
274 if( sqlite3CheckIndexCollSeq(pParse, pIdx) ){
275 goto insert_cleanup;
276 }
277 }
278
drh1ccde152000-06-17 13:12:39 +0000279 /* Allocate a VDBE
280 */
danielk19774adee202004-05-08 08:23:19 +0000281 v = sqlite3GetVdbe(pParse);
drh5974a302000-06-07 14:42:26 +0000282 if( v==0 ) goto insert_cleanup;
drh4794f732004-11-05 17:17:50 +0000283 if( pParse->nested==0 ) sqlite3VdbeCountChanges(v);
drhdca76842004-12-07 14:06:13 +0000284 sqlite3BeginWriteOperation(pParse, pSelect || triggers_exist, pTab->iDb);
drh1ccde152000-06-17 13:12:39 +0000285
danielk1977c3f9bad2002-05-15 08:30:12 +0000286 /* if there are row triggers, allocate a temp table for new.* references. */
drhdca76842004-12-07 14:06:13 +0000287 if( triggers_exist ){
danielk1977c3f9bad2002-05-15 08:30:12 +0000288 newIdx = pParse->nTab++;
danielk1977f29ce552002-05-19 23:43:12 +0000289 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000290
drh2958a4e2004-11-12 03:56:15 +0000291#ifndef SQLITE_OMIT_AUTOINCREMENT
292 /* If this is an AUTOINCREMENT table, look up the sequence number in the
drhf3388142004-11-13 03:48:06 +0000293 ** sqlite_sequence table and store it in memory cell counterMem. Also
294 ** remember the rowid of the sqlite_sequence table entry in memory cell
295 ** counterRowid.
drh2958a4e2004-11-12 03:56:15 +0000296 */
297 if( pTab->autoInc ){
drhf3388142004-11-13 03:48:06 +0000298 int iCur = pParse->nTab;
299 int base = sqlite3VdbeCurrentAddr(v);
300 counterRowid = pParse->nMem++;
301 counterMem = pParse->nMem++;
302 sqlite3VdbeAddOp(v, OP_Integer, pTab->iDb, 0);
303 sqlite3VdbeAddOp(v, OP_OpenRead, iCur, pDb->pSeqTab->tnum);
304 sqlite3VdbeAddOp(v, OP_SetNumColumns, iCur, 2);
305 sqlite3VdbeAddOp(v, OP_Rewind, iCur, base+13);
306 sqlite3VdbeAddOp(v, OP_Column, iCur, 0);
307 sqlite3VdbeOp3(v, OP_String8, 0, 0, pTab->zName, 0);
308 sqlite3VdbeAddOp(v, OP_Ne, 28417, base+12);
drhf0863fe2005-06-12 21:35:51 +0000309 sqlite3VdbeAddOp(v, OP_Rowid, iCur, 0);
drhf3388142004-11-13 03:48:06 +0000310 sqlite3VdbeAddOp(v, OP_MemStore, counterRowid, 1);
311 sqlite3VdbeAddOp(v, OP_Column, iCur, 1);
312 sqlite3VdbeAddOp(v, OP_MemStore, counterMem, 1);
313 sqlite3VdbeAddOp(v, OP_Goto, 0, base+13);
314 sqlite3VdbeAddOp(v, OP_Next, iCur, base+4);
315 sqlite3VdbeAddOp(v, OP_Close, iCur, 0);
drh2958a4e2004-11-12 03:56:15 +0000316 }
317#endif /* SQLITE_OMIT_AUTOINCREMENT */
318
drh1ccde152000-06-17 13:12:39 +0000319 /* Figure out how many columns of data are supplied. If the data
drh142e30d2002-08-28 03:00:58 +0000320 ** is coming from a SELECT statement, then this step also generates
321 ** all the code to implement the SELECT statement and invoke a subroutine
322 ** to process each row of the result. (Template 2.) If the SELECT
323 ** statement uses the the table that is being inserted into, then the
324 ** subroutine is also coded here. That subroutine stores the SELECT
325 ** results in a temporary table. (Template 3.)
drh1ccde152000-06-17 13:12:39 +0000326 */
drh5974a302000-06-07 14:42:26 +0000327 if( pSelect ){
drh142e30d2002-08-28 03:00:58 +0000328 /* Data is coming from a SELECT. Generate code to implement that SELECT
329 */
330 int rc, iInitCode;
danielk19774adee202004-05-08 08:23:19 +0000331 iInitCode = sqlite3VdbeAddOp(v, OP_Goto, 0, 0);
332 iSelectLoop = sqlite3VdbeCurrentAddr(v);
333 iInsertBlock = sqlite3VdbeMakeLabel(v);
danielk1977b3bce662005-01-29 08:32:43 +0000334
335 /* Resolve the expressions in the SELECT statement and execute it. */
336 rc = sqlite3Select(pParse, pSelect, SRT_Subroutine, iInsertBlock,0,0,0,0);
danielk197724b03fd2004-05-10 10:34:34 +0000337 if( rc || pParse->nErr || sqlite3_malloc_failed ) goto insert_cleanup;
danielk1977b3bce662005-01-29 08:32:43 +0000338
danielk19774adee202004-05-08 08:23:19 +0000339 iCleanup = sqlite3VdbeMakeLabel(v);
340 sqlite3VdbeAddOp(v, OP_Goto, 0, iCleanup);
drh5974a302000-06-07 14:42:26 +0000341 assert( pSelect->pEList );
drh967e8b72000-06-21 13:59:10 +0000342 nColumn = pSelect->pEList->nExpr;
drh142e30d2002-08-28 03:00:58 +0000343
344 /* Set useTempTable to TRUE if the result of the SELECT statement
345 ** should be written into a temporary table. Set to FALSE if each
346 ** row of the SELECT can be written directly into the result table.
drh048c5302003-04-03 01:50:44 +0000347 **
348 ** A temp table must be used if the table being updated is also one
349 ** of the tables being read by the SELECT statement. Also use a
350 ** temp table in the case of row triggers.
drh142e30d2002-08-28 03:00:58 +0000351 */
danielk19774d887782005-02-08 08:42:27 +0000352 if( triggers_exist || selectReadsTable(pSelect, pTab->iDb, pTab->tnum) ){
drh048c5302003-04-03 01:50:44 +0000353 useTempTable = 1;
drh048c5302003-04-03 01:50:44 +0000354 }
drh142e30d2002-08-28 03:00:58 +0000355
356 if( useTempTable ){
357 /* Generate the subroutine that SELECT calls to process each row of
358 ** the result. Store the result in a temporary table
359 */
360 srcTab = pParse->nTab++;
danielk19774adee202004-05-08 08:23:19 +0000361 sqlite3VdbeResolveLabel(v, iInsertBlock);
362 sqlite3VdbeAddOp(v, OP_MakeRecord, nColumn, 0);
danielk1977a37cdde2004-05-16 11:15:36 +0000363 sqlite3TableAffinityStr(v, pTab);
drhf0863fe2005-06-12 21:35:51 +0000364 sqlite3VdbeAddOp(v, OP_NewRowid, srcTab, 0);
danielk19774adee202004-05-08 08:23:19 +0000365 sqlite3VdbeAddOp(v, OP_Pull, 1, 0);
drhf0863fe2005-06-12 21:35:51 +0000366 sqlite3VdbeAddOp(v, OP_Insert, srcTab, 0);
danielk19774adee202004-05-08 08:23:19 +0000367 sqlite3VdbeAddOp(v, OP_Return, 0, 0);
drh142e30d2002-08-28 03:00:58 +0000368
369 /* The following code runs first because the GOTO at the very top
370 ** of the program jumps to it. Create the temporary table, then jump
371 ** back up and execute the SELECT code above.
372 */
drhd654be82005-09-20 17:42:23 +0000373 sqlite3VdbeJumpHere(v, iInitCode);
drh9170dd72005-07-08 17:13:46 +0000374 sqlite3VdbeAddOp(v, OP_OpenVirtual, srcTab, 0);
drhb6f54522004-05-20 02:42:16 +0000375 sqlite3VdbeAddOp(v, OP_SetNumColumns, srcTab, nColumn);
danielk19774adee202004-05-08 08:23:19 +0000376 sqlite3VdbeAddOp(v, OP_Goto, 0, iSelectLoop);
377 sqlite3VdbeResolveLabel(v, iCleanup);
drh142e30d2002-08-28 03:00:58 +0000378 }else{
drhd654be82005-09-20 17:42:23 +0000379 sqlite3VdbeJumpHere(v, iInitCode);
drh142e30d2002-08-28 03:00:58 +0000380 }
drh5974a302000-06-07 14:42:26 +0000381 }else{
drh142e30d2002-08-28 03:00:58 +0000382 /* This is the case if the data for the INSERT is coming from a VALUES
383 ** clause
384 */
danielk1977b3bce662005-01-29 08:32:43 +0000385 NameContext sNC;
386 memset(&sNC, 0, sizeof(sNC));
387 sNC.pParse = pParse;
drhdaffd0e2001-04-11 14:28:42 +0000388 assert( pList!=0 );
drh5974a302000-06-07 14:42:26 +0000389 srcTab = -1;
drh142e30d2002-08-28 03:00:58 +0000390 useTempTable = 0;
drh5974a302000-06-07 14:42:26 +0000391 assert( pList );
drh967e8b72000-06-21 13:59:10 +0000392 nColumn = pList->nExpr;
drhe64e7b22002-02-18 13:56:36 +0000393 for(i=0; i<nColumn; i++){
danielk1977b3bce662005-01-29 08:32:43 +0000394 if( sqlite3ExprResolveNames(&sNC, pList->a[i].pExpr) ){
drhb04a5d82002-04-12 03:55:15 +0000395 goto insert_cleanup;
396 }
drhe64e7b22002-02-18 13:56:36 +0000397 }
drh5974a302000-06-07 14:42:26 +0000398 }
drh1ccde152000-06-17 13:12:39 +0000399
400 /* Make sure the number of columns in the source data matches the number
401 ** of columns to be inserted into the table.
402 */
drh967e8b72000-06-21 13:59:10 +0000403 if( pColumn==0 && nColumn!=pTab->nCol ){
danielk19774adee202004-05-08 08:23:19 +0000404 sqlite3ErrorMsg(pParse,
drhda93d232003-03-31 02:12:46 +0000405 "table %S has %d columns but %d values were supplied",
406 pTabList, 0, pTab->nCol, nColumn);
drhcce7d172000-05-31 15:34:51 +0000407 goto insert_cleanup;
408 }
drh967e8b72000-06-21 13:59:10 +0000409 if( pColumn!=0 && nColumn!=pColumn->nId ){
danielk19774adee202004-05-08 08:23:19 +0000410 sqlite3ErrorMsg(pParse, "%d values for %d columns", nColumn, pColumn->nId);
drhcce7d172000-05-31 15:34:51 +0000411 goto insert_cleanup;
412 }
drh1ccde152000-06-17 13:12:39 +0000413
414 /* If the INSERT statement included an IDLIST term, then make sure
415 ** all elements of the IDLIST really are columns of the table and
416 ** remember the column indices.
drhc8392582001-12-31 02:48:51 +0000417 **
418 ** If the table has an INTEGER PRIMARY KEY column and that column
419 ** is named in the IDLIST, then record in the keyColumn variable
420 ** the index into IDLIST of the primary key column. keyColumn is
421 ** the index of the primary key as it appears in IDLIST, not as
422 ** is appears in the original table. (The index of the primary
423 ** key in the original table is pTab->iPKey.)
drh1ccde152000-06-17 13:12:39 +0000424 */
drh967e8b72000-06-21 13:59:10 +0000425 if( pColumn ){
426 for(i=0; i<pColumn->nId; i++){
427 pColumn->a[i].idx = -1;
drhcce7d172000-05-31 15:34:51 +0000428 }
drh967e8b72000-06-21 13:59:10 +0000429 for(i=0; i<pColumn->nId; i++){
drhcce7d172000-05-31 15:34:51 +0000430 for(j=0; j<pTab->nCol; j++){
danielk19774adee202004-05-08 08:23:19 +0000431 if( sqlite3StrICmp(pColumn->a[i].zName, pTab->aCol[j].zName)==0 ){
drh967e8b72000-06-21 13:59:10 +0000432 pColumn->a[i].idx = j;
drh4a324312001-12-21 14:30:42 +0000433 if( j==pTab->iPKey ){
drh9aa028d2001-12-22 21:48:29 +0000434 keyColumn = i;
drh4a324312001-12-21 14:30:42 +0000435 }
drhcce7d172000-05-31 15:34:51 +0000436 break;
437 }
438 }
439 if( j>=pTab->nCol ){
danielk19774adee202004-05-08 08:23:19 +0000440 if( sqlite3IsRowid(pColumn->a[i].zName) ){
drha0217ba2003-06-01 01:10:33 +0000441 keyColumn = i;
442 }else{
danielk19774adee202004-05-08 08:23:19 +0000443 sqlite3ErrorMsg(pParse, "table %S has no column named %s",
drha0217ba2003-06-01 01:10:33 +0000444 pTabList, 0, pColumn->a[i].zName);
445 pParse->nErr++;
446 goto insert_cleanup;
447 }
drhcce7d172000-05-31 15:34:51 +0000448 }
449 }
450 }
drh1ccde152000-06-17 13:12:39 +0000451
drhaacc5432002-01-06 17:07:40 +0000452 /* If there is no IDLIST term but the table has an integer primary
drhc8392582001-12-31 02:48:51 +0000453 ** key, the set the keyColumn variable to the primary key column index
454 ** in the original table definition.
drh4a324312001-12-21 14:30:42 +0000455 */
456 if( pColumn==0 ){
457 keyColumn = pTab->iPKey;
458 }
459
drh142e30d2002-08-28 03:00:58 +0000460 /* Open the temp table for FOR EACH ROW triggers
461 */
drhdca76842004-12-07 14:06:13 +0000462 if( triggers_exist ){
danielk19774adee202004-05-08 08:23:19 +0000463 sqlite3VdbeAddOp(v, OP_OpenPseudo, newIdx, 0);
danielk197784ac9d02004-05-18 09:58:06 +0000464 sqlite3VdbeAddOp(v, OP_SetNumColumns, newIdx, pTab->nCol);
danielk1977f29ce552002-05-19 23:43:12 +0000465 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000466
drhfeeb1392002-04-09 03:28:01 +0000467 /* Initialize the count of rows to be inserted
468 */
drh142e30d2002-08-28 03:00:58 +0000469 if( db->flags & SQLITE_CountRows ){
470 iCntMem = pParse->nMem++;
drhd654be82005-09-20 17:42:23 +0000471 sqlite3VdbeAddOp(v, OP_MemInt, 0, iCntMem);
drhfeeb1392002-04-09 03:28:01 +0000472 }
473
danielk1977c3f9bad2002-05-15 08:30:12 +0000474 /* Open tables and indices if there are no row triggers */
drhdca76842004-12-07 14:06:13 +0000475 if( !triggers_exist ){
danielk1977c3f9bad2002-05-15 08:30:12 +0000476 base = pParse->nTab;
drh290c1942004-08-21 17:54:45 +0000477 sqlite3OpenTableAndIndices(pParse, pTab, base, OP_OpenWrite);
danielk1977c3f9bad2002-05-15 08:30:12 +0000478 }
479
drh142e30d2002-08-28 03:00:58 +0000480 /* If the data source is a temporary table, then we have to create
drh1ccde152000-06-17 13:12:39 +0000481 ** a loop because there might be multiple rows of data. If the data
drh142e30d2002-08-28 03:00:58 +0000482 ** source is a subroutine call from the SELECT statement, then we need
483 ** to launch the SELECT statement processing.
drh1ccde152000-06-17 13:12:39 +0000484 */
drh142e30d2002-08-28 03:00:58 +0000485 if( useTempTable ){
danielk19774adee202004-05-08 08:23:19 +0000486 iBreak = sqlite3VdbeMakeLabel(v);
487 sqlite3VdbeAddOp(v, OP_Rewind, srcTab, iBreak);
488 iCont = sqlite3VdbeCurrentAddr(v);
drh142e30d2002-08-28 03:00:58 +0000489 }else if( pSelect ){
danielk19774adee202004-05-08 08:23:19 +0000490 sqlite3VdbeAddOp(v, OP_Goto, 0, iSelectLoop);
491 sqlite3VdbeResolveLabel(v, iInsertBlock);
drh5974a302000-06-07 14:42:26 +0000492 }
drh1ccde152000-06-17 13:12:39 +0000493
drh5cf590c2003-04-24 01:45:04 +0000494 /* Run the BEFORE and INSTEAD OF triggers, if there are any
drh70ce3f02003-04-15 19:22:22 +0000495 */
danielk19774adee202004-05-08 08:23:19 +0000496 endOfLoop = sqlite3VdbeMakeLabel(v);
drhdca76842004-12-07 14:06:13 +0000497 if( triggers_exist & TRIGGER_BEFORE ){
danielk1977c3f9bad2002-05-15 08:30:12 +0000498
drh70ce3f02003-04-15 19:22:22 +0000499 /* build the NEW.* reference row. Note that if there is an INTEGER
500 ** PRIMARY KEY into which a NULL is being inserted, that NULL will be
501 ** translated into a unique ID for the row. But on a BEFORE trigger,
502 ** we do not know what the unique ID will be (because the insert has
503 ** not happened yet) so we substitute a rowid of -1
504 */
505 if( keyColumn<0 ){
danielk19774adee202004-05-08 08:23:19 +0000506 sqlite3VdbeAddOp(v, OP_Integer, -1, 0);
drh70ce3f02003-04-15 19:22:22 +0000507 }else if( useTempTable ){
danielk19774adee202004-05-08 08:23:19 +0000508 sqlite3VdbeAddOp(v, OP_Column, srcTab, keyColumn);
drh70ce3f02003-04-15 19:22:22 +0000509 }else{
drhd6fe9612005-01-14 01:22:00 +0000510 assert( pSelect==0 ); /* Otherwise useTempTable is true */
danielk19774adee202004-05-08 08:23:19 +0000511 sqlite3ExprCode(pParse, pList->a[keyColumn].pExpr);
512 sqlite3VdbeAddOp(v, OP_NotNull, -1, sqlite3VdbeCurrentAddr(v)+3);
513 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
514 sqlite3VdbeAddOp(v, OP_Integer, -1, 0);
515 sqlite3VdbeAddOp(v, OP_MustBeInt, 0, 0);
drh70ce3f02003-04-15 19:22:22 +0000516 }
517
518 /* Create the new column data
519 */
danielk1977c3f9bad2002-05-15 08:30:12 +0000520 for(i=0; i<pTab->nCol; i++){
521 if( pColumn==0 ){
drh9adf9ac2002-05-15 11:44:13 +0000522 j = i;
danielk1977c3f9bad2002-05-15 08:30:12 +0000523 }else{
drh9adf9ac2002-05-15 11:44:13 +0000524 for(j=0; j<pColumn->nId; j++){
525 if( pColumn->a[j].idx==i ) break;
526 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000527 }
528 if( pColumn && j>=pColumn->nId ){
danielk19777977a172004-11-09 12:44:37 +0000529 sqlite3ExprCode(pParse, pTab->aCol[i].pDflt);
drh142e30d2002-08-28 03:00:58 +0000530 }else if( useTempTable ){
danielk19774adee202004-05-08 08:23:19 +0000531 sqlite3VdbeAddOp(v, OP_Column, srcTab, j);
danielk1977c3f9bad2002-05-15 08:30:12 +0000532 }else{
drhd6fe9612005-01-14 01:22:00 +0000533 assert( pSelect==0 ); /* Otherwise useTempTable is true */
drh25303782004-12-07 15:41:48 +0000534 sqlite3ExprCodeAndCache(pParse, pList->a[j].pExpr);
danielk1977c3f9bad2002-05-15 08:30:12 +0000535 }
536 }
danielk19774adee202004-05-08 08:23:19 +0000537 sqlite3VdbeAddOp(v, OP_MakeRecord, pTab->nCol, 0);
danielk1977a37cdde2004-05-16 11:15:36 +0000538
539 /* If this is an INSERT on a view with an INSTEAD OF INSERT trigger,
540 ** do not attempt any conversions before assembling the record.
541 ** If this is a real table, attempt conversions as required by the
542 ** table column affinities.
543 */
544 if( !isView ){
545 sqlite3TableAffinityStr(v, pTab);
546 }
drhf0863fe2005-06-12 21:35:51 +0000547 sqlite3VdbeAddOp(v, OP_Insert, newIdx, 0);
danielk1977c3f9bad2002-05-15 08:30:12 +0000548
drh5cf590c2003-04-24 01:45:04 +0000549 /* Fire BEFORE or INSTEAD OF triggers */
drhdca76842004-12-07 14:06:13 +0000550 if( sqlite3CodeRowTrigger(pParse, TK_INSERT, 0, TRIGGER_BEFORE, pTab,
drhb2fe7d82003-04-20 17:29:23 +0000551 newIdx, -1, onError, endOfLoop) ){
danielk1977f29ce552002-05-19 23:43:12 +0000552 goto insert_cleanup;
553 }
drh70ce3f02003-04-15 19:22:22 +0000554 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000555
drh70ce3f02003-04-15 19:22:22 +0000556 /* If any triggers exists, the opening of tables and indices is deferred
557 ** until now.
558 */
drhdca76842004-12-07 14:06:13 +0000559 if( triggers_exist && !isView ){
drh5cf590c2003-04-24 01:45:04 +0000560 base = pParse->nTab;
drh290c1942004-08-21 17:54:45 +0000561 sqlite3OpenTableAndIndices(pParse, pTab, base, OP_OpenWrite);
danielk1977c3f9bad2002-05-15 08:30:12 +0000562 }
563
drh4a324312001-12-21 14:30:42 +0000564 /* Push the record number for the new entry onto the stack. The
drhf0863fe2005-06-12 21:35:51 +0000565 ** record number is a randomly generate integer created by NewRowid
drh4a324312001-12-21 14:30:42 +0000566 ** except when the table has an INTEGER PRIMARY KEY column, in which
drhb419a922002-01-30 16:17:23 +0000567 ** case the record number is the same as that column.
drh1ccde152000-06-17 13:12:39 +0000568 */
drh5cf590c2003-04-24 01:45:04 +0000569 if( !isView ){
danielk1977c3f9bad2002-05-15 08:30:12 +0000570 if( keyColumn>=0 ){
drh142e30d2002-08-28 03:00:58 +0000571 if( useTempTable ){
danielk19774adee202004-05-08 08:23:19 +0000572 sqlite3VdbeAddOp(v, OP_Column, srcTab, keyColumn);
drh142e30d2002-08-28 03:00:58 +0000573 }else if( pSelect ){
danielk19774adee202004-05-08 08:23:19 +0000574 sqlite3VdbeAddOp(v, OP_Dup, nColumn - keyColumn - 1, 1);
danielk1977c3f9bad2002-05-15 08:30:12 +0000575 }else{
danielk19774adee202004-05-08 08:23:19 +0000576 sqlite3ExprCode(pParse, pList->a[keyColumn].pExpr);
danielk1977c3f9bad2002-05-15 08:30:12 +0000577 }
drhf0863fe2005-06-12 21:35:51 +0000578 /* If the PRIMARY KEY expression is NULL, then use OP_NewRowid
drh27a32782002-06-19 20:32:43 +0000579 ** to generate a unique primary key value.
580 */
danielk19774adee202004-05-08 08:23:19 +0000581 sqlite3VdbeAddOp(v, OP_NotNull, -1, sqlite3VdbeCurrentAddr(v)+3);
582 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
drhf0863fe2005-06-12 21:35:51 +0000583 sqlite3VdbeAddOp(v, OP_NewRowid, base, counterMem);
danielk19774adee202004-05-08 08:23:19 +0000584 sqlite3VdbeAddOp(v, OP_MustBeInt, 0, 0);
danielk1977c3f9bad2002-05-15 08:30:12 +0000585 }else{
drhf0863fe2005-06-12 21:35:51 +0000586 sqlite3VdbeAddOp(v, OP_NewRowid, base, counterMem);
drh4a324312001-12-21 14:30:42 +0000587 }
drh2958a4e2004-11-12 03:56:15 +0000588#ifndef SQLITE_OMIT_AUTOINCREMENT
589 if( pTab->autoInc ){
590 sqlite3VdbeAddOp(v, OP_MemMax, counterMem, 0);
591 }
592#endif /* SQLITE_OMIT_AUTOINCREMENT */
drh4a324312001-12-21 14:30:42 +0000593
danielk1977c3f9bad2002-05-15 08:30:12 +0000594 /* Push onto the stack, data for all columns of the new entry, beginning
danielk1977f29ce552002-05-19 23:43:12 +0000595 ** with the first column.
596 */
danielk1977c3f9bad2002-05-15 08:30:12 +0000597 for(i=0; i<pTab->nCol; i++){
598 if( i==pTab->iPKey ){
drh9adf9ac2002-05-15 11:44:13 +0000599 /* The value of the INTEGER PRIMARY KEY column is always a NULL.
danielk1977f29ce552002-05-19 23:43:12 +0000600 ** Whenever this column is read, the record number will be substituted
601 ** in its place. So will fill this column with a NULL to avoid
602 ** taking up data space with information that will never be used. */
drhf0863fe2005-06-12 21:35:51 +0000603 sqlite3VdbeAddOp(v, OP_Null, 0, 0);
drh9adf9ac2002-05-15 11:44:13 +0000604 continue;
danielk1977c3f9bad2002-05-15 08:30:12 +0000605 }
606 if( pColumn==0 ){
drh9adf9ac2002-05-15 11:44:13 +0000607 j = i;
danielk1977c3f9bad2002-05-15 08:30:12 +0000608 }else{
drh9adf9ac2002-05-15 11:44:13 +0000609 for(j=0; j<pColumn->nId; j++){
610 if( pColumn->a[j].idx==i ) break;
611 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000612 }
613 if( pColumn && j>=pColumn->nId ){
danielk19777977a172004-11-09 12:44:37 +0000614 sqlite3ExprCode(pParse, pTab->aCol[i].pDflt);
drh142e30d2002-08-28 03:00:58 +0000615 }else if( useTempTable ){
danielk19774adee202004-05-08 08:23:19 +0000616 sqlite3VdbeAddOp(v, OP_Column, srcTab, j);
drh142e30d2002-08-28 03:00:58 +0000617 }else if( pSelect ){
danielk19774adee202004-05-08 08:23:19 +0000618 sqlite3VdbeAddOp(v, OP_Dup, i+nColumn-j, 1);
danielk1977c3f9bad2002-05-15 08:30:12 +0000619 }else{
danielk19774adee202004-05-08 08:23:19 +0000620 sqlite3ExprCode(pParse, pList->a[j].pExpr);
drh5974a302000-06-07 14:42:26 +0000621 }
drhbed86902000-06-02 13:27:59 +0000622 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000623
624 /* Generate code to check constraints and generate index keys and
danielk1977f29ce552002-05-19 23:43:12 +0000625 ** do the insertion.
626 */
danielk19774adee202004-05-08 08:23:19 +0000627 sqlite3GenerateConstraintChecks(pParse, pTab, base, 0, keyColumn>=0,
drha0217ba2003-06-01 01:10:33 +0000628 0, onError, endOfLoop);
danielk19774adee202004-05-08 08:23:19 +0000629 sqlite3CompleteInsertion(pParse, pTab, base, 0,0,0,
drhdca76842004-12-07 14:06:13 +0000630 (triggers_exist & TRIGGER_AFTER)!=0 ? newIdx : -1);
drh5cf590c2003-04-24 01:45:04 +0000631 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000632
drh5cf590c2003-04-24 01:45:04 +0000633 /* Update the count of rows that are inserted
634 */
635 if( (db->flags & SQLITE_CountRows)!=0 ){
danielk19774adee202004-05-08 08:23:19 +0000636 sqlite3VdbeAddOp(v, OP_MemIncr, iCntMem, 0);
drh5974a302000-06-07 14:42:26 +0000637 }
drh1ccde152000-06-17 13:12:39 +0000638
drhdca76842004-12-07 14:06:13 +0000639 if( triggers_exist ){
danielk1977c3f9bad2002-05-15 08:30:12 +0000640 /* Close all tables opened */
drh5cf590c2003-04-24 01:45:04 +0000641 if( !isView ){
danielk19774adee202004-05-08 08:23:19 +0000642 sqlite3VdbeAddOp(v, OP_Close, base, 0);
danielk1977c3f9bad2002-05-15 08:30:12 +0000643 for(idx=1, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, idx++){
danielk19774adee202004-05-08 08:23:19 +0000644 sqlite3VdbeAddOp(v, OP_Close, idx+base, 0);
danielk1977c3f9bad2002-05-15 08:30:12 +0000645 }
646 }
drh1bee3d72001-10-15 00:44:35 +0000647
danielk1977c3f9bad2002-05-15 08:30:12 +0000648 /* Code AFTER triggers */
drhdca76842004-12-07 14:06:13 +0000649 if( sqlite3CodeRowTrigger(pParse, TK_INSERT, 0, TRIGGER_AFTER, pTab,
650 newIdx, -1, onError, endOfLoop) ){
danielk1977f29ce552002-05-19 23:43:12 +0000651 goto insert_cleanup;
652 }
drh1bee3d72001-10-15 00:44:35 +0000653 }
654
drh1ccde152000-06-17 13:12:39 +0000655 /* The bottom of the loop, if the data source is a SELECT statement
danielk1977f29ce552002-05-19 23:43:12 +0000656 */
danielk19774adee202004-05-08 08:23:19 +0000657 sqlite3VdbeResolveLabel(v, endOfLoop);
drh142e30d2002-08-28 03:00:58 +0000658 if( useTempTable ){
danielk19774adee202004-05-08 08:23:19 +0000659 sqlite3VdbeAddOp(v, OP_Next, srcTab, iCont);
660 sqlite3VdbeResolveLabel(v, iBreak);
661 sqlite3VdbeAddOp(v, OP_Close, srcTab, 0);
drh142e30d2002-08-28 03:00:58 +0000662 }else if( pSelect ){
danielk19774adee202004-05-08 08:23:19 +0000663 sqlite3VdbeAddOp(v, OP_Pop, nColumn, 0);
664 sqlite3VdbeAddOp(v, OP_Return, 0, 0);
665 sqlite3VdbeResolveLabel(v, iCleanup);
drh6b563442001-11-07 16:48:26 +0000666 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000667
drhdca76842004-12-07 14:06:13 +0000668 if( !triggers_exist ){
danielk1977c3f9bad2002-05-15 08:30:12 +0000669 /* Close all tables opened */
danielk19774adee202004-05-08 08:23:19 +0000670 sqlite3VdbeAddOp(v, OP_Close, base, 0);
danielk1977c3f9bad2002-05-15 08:30:12 +0000671 for(idx=1, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, idx++){
danielk19774adee202004-05-08 08:23:19 +0000672 sqlite3VdbeAddOp(v, OP_Close, idx+base, 0);
danielk1977c3f9bad2002-05-15 08:30:12 +0000673 }
drhcce7d172000-05-31 15:34:51 +0000674 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000675
drh2958a4e2004-11-12 03:56:15 +0000676#ifndef SQLITE_OMIT_AUTOINCREMENT
drhf3388142004-11-13 03:48:06 +0000677 /* Update the sqlite_sequence table by storing the content of the
678 ** counter value in memory counterMem back into the sqlite_sequence
679 ** table.
drh2958a4e2004-11-12 03:56:15 +0000680 */
681 if( pTab->autoInc ){
drhf3388142004-11-13 03:48:06 +0000682 int iCur = pParse->nTab;
683 int base = sqlite3VdbeCurrentAddr(v);
684 sqlite3VdbeAddOp(v, OP_Integer, pTab->iDb, 0);
685 sqlite3VdbeAddOp(v, OP_OpenWrite, iCur, pDb->pSeqTab->tnum);
686 sqlite3VdbeAddOp(v, OP_SetNumColumns, iCur, 2);
687 sqlite3VdbeAddOp(v, OP_MemLoad, counterRowid, 0);
688 sqlite3VdbeAddOp(v, OP_NotNull, -1, base+7);
689 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
drhf0863fe2005-06-12 21:35:51 +0000690 sqlite3VdbeAddOp(v, OP_NewRowid, iCur, 0);
drhf3388142004-11-13 03:48:06 +0000691 sqlite3VdbeOp3(v, OP_String8, 0, 0, pTab->zName, 0);
692 sqlite3VdbeAddOp(v, OP_MemLoad, counterMem, 0);
693 sqlite3VdbeAddOp(v, OP_MakeRecord, 2, 0);
drhf0863fe2005-06-12 21:35:51 +0000694 sqlite3VdbeAddOp(v, OP_Insert, iCur, 0);
drhf3388142004-11-13 03:48:06 +0000695 sqlite3VdbeAddOp(v, OP_Close, iCur, 0);
drh2958a4e2004-11-12 03:56:15 +0000696 }
697#endif
698
drh1bee3d72001-10-15 00:44:35 +0000699 /*
danielk1977e7de6f22004-11-05 06:02:06 +0000700 ** Return the number of rows inserted. If this routine is
701 ** generating code because of a call to sqlite3NestedParse(), do not
702 ** invoke the callback function.
drh1bee3d72001-10-15 00:44:35 +0000703 */
danielk1977cc6bd382005-01-10 02:48:49 +0000704 if( db->flags & SQLITE_CountRows && pParse->nested==0 && !pParse->trigStack ){
danielk19774adee202004-05-08 08:23:19 +0000705 sqlite3VdbeAddOp(v, OP_MemLoad, iCntMem, 0);
706 sqlite3VdbeAddOp(v, OP_Callback, 1, 0);
danielk197722322fd2004-05-25 23:35:17 +0000707 sqlite3VdbeSetNumCols(v, 1);
danielk19773cf86062004-05-26 10:11:05 +0000708 sqlite3VdbeSetColName(v, 0, "rows inserted", P3_STATIC);
drh1bee3d72001-10-15 00:44:35 +0000709 }
drhcce7d172000-05-31 15:34:51 +0000710
711insert_cleanup:
danielk19774adee202004-05-08 08:23:19 +0000712 sqlite3SrcListDelete(pTabList);
danielk1977d5d56522005-03-16 12:15:20 +0000713 sqlite3ExprListDelete(pList);
714 sqlite3SelectDelete(pSelect);
danielk19774adee202004-05-08 08:23:19 +0000715 sqlite3IdListDelete(pColumn);
drhcce7d172000-05-31 15:34:51 +0000716}
drh9cfcf5d2002-01-29 18:41:24 +0000717
drh9cfcf5d2002-01-29 18:41:24 +0000718/*
719** Generate code to do a constraint check prior to an INSERT or an UPDATE.
720**
721** When this routine is called, the stack contains (from bottom to top)
drh0ca3e242002-01-29 23:07:02 +0000722** the following values:
723**
drhf0863fe2005-06-12 21:35:51 +0000724** 1. The rowid of the row to be updated before the update. This
drhb419a922002-01-30 16:17:23 +0000725** value is omitted unless we are doing an UPDATE that involves a
726** change to the record number.
drh0ca3e242002-01-29 23:07:02 +0000727**
drhf0863fe2005-06-12 21:35:51 +0000728** 2. The rowid of the row after the update.
drh0ca3e242002-01-29 23:07:02 +0000729**
730** 3. The data in the first column of the entry after the update.
731**
732** i. Data from middle columns...
733**
734** N. The data in the last column of the entry after the update.
735**
drhf0863fe2005-06-12 21:35:51 +0000736** The old rowid shown as entry (1) above is omitted unless both isUpdate
737** and rowidChng are 1. isUpdate is true for UPDATEs and false for
738** INSERTs and rowidChng is true if the record number is being changed.
drh0ca3e242002-01-29 23:07:02 +0000739**
740** The code generated by this routine pushes additional entries onto
741** the stack which are the keys for new index entries for the new record.
742** The order of index keys is the same as the order of the indices on
743** the pTable->pIndex list. A key is only created for index i if
744** aIdxUsed!=0 and aIdxUsed[i]!=0.
drh9cfcf5d2002-01-29 18:41:24 +0000745**
746** This routine also generates code to check constraints. NOT NULL,
747** CHECK, and UNIQUE constraints are all checked. If a constraint fails,
drh1c928532002-01-31 15:54:21 +0000748** then the appropriate action is performed. There are five possible
749** actions: ROLLBACK, ABORT, FAIL, REPLACE, and IGNORE.
drh9cfcf5d2002-01-29 18:41:24 +0000750**
751** Constraint type Action What Happens
752** --------------- ---------- ----------------------------------------
drh1c928532002-01-31 15:54:21 +0000753** any ROLLBACK The current transaction is rolled back and
danielk197724b03fd2004-05-10 10:34:34 +0000754** sqlite3_exec() returns immediately with a
drh9cfcf5d2002-01-29 18:41:24 +0000755** return code of SQLITE_CONSTRAINT.
756**
drh1c928532002-01-31 15:54:21 +0000757** any ABORT Back out changes from the current command
758** only (do not do a complete rollback) then
danielk197724b03fd2004-05-10 10:34:34 +0000759** cause sqlite3_exec() to return immediately
drh1c928532002-01-31 15:54:21 +0000760** with SQLITE_CONSTRAINT.
761**
762** any FAIL Sqlite_exec() returns immediately with a
763** return code of SQLITE_CONSTRAINT. The
764** transaction is not rolled back and any
765** prior changes are retained.
766**
drh9cfcf5d2002-01-29 18:41:24 +0000767** any IGNORE The record number and data is popped from
768** the stack and there is an immediate jump
769** to label ignoreDest.
770**
771** NOT NULL REPLACE The NULL value is replace by the default
772** value for that column. If the default value
773** is NULL, the action is the same as ABORT.
774**
775** UNIQUE REPLACE The other row that conflicts with the row
776** being inserted is removed.
777**
778** CHECK REPLACE Illegal. The results in an exception.
779**
drh1c928532002-01-31 15:54:21 +0000780** Which action to take is determined by the overrideError parameter.
781** Or if overrideError==OE_Default, then the pParse->onError parameter
782** is used. Or if pParse->onError==OE_Default then the onError value
783** for the constraint is used.
drh9cfcf5d2002-01-29 18:41:24 +0000784**
drhaaab5722002-02-19 13:39:21 +0000785** The calling routine must open a read/write cursor for pTab with
drh9cfcf5d2002-01-29 18:41:24 +0000786** cursor number "base". All indices of pTab must also have open
787** read/write cursors with cursor number base+i for the i-th cursor.
788** Except, if there is no possibility of a REPLACE action then
789** cursors do not need to be open for indices where aIdxUsed[i]==0.
790**
791** If the isUpdate flag is true, it means that the "base" cursor is
792** initially pointing to an entry that is being updated. The isUpdate
793** flag causes extra code to be generated so that the "base" cursor
794** is still pointing at the same entry after the routine returns.
795** Without the isUpdate flag, the "base" cursor might be moved.
796*/
danielk19774adee202004-05-08 08:23:19 +0000797void sqlite3GenerateConstraintChecks(
drh9cfcf5d2002-01-29 18:41:24 +0000798 Parse *pParse, /* The parser context */
799 Table *pTab, /* the table into which we are inserting */
800 int base, /* Index of a read/write cursor pointing at pTab */
801 char *aIdxUsed, /* Which indices are used. NULL means all are used */
drhf0863fe2005-06-12 21:35:51 +0000802 int rowidChng, /* True if the record number will change */
drhb419a922002-01-30 16:17:23 +0000803 int isUpdate, /* True for UPDATE, False for INSERT */
drh9cfcf5d2002-01-29 18:41:24 +0000804 int overrideError, /* Override onError to this if not OE_Default */
drhb419a922002-01-30 16:17:23 +0000805 int ignoreDest /* Jump to this label on an OE_Ignore resolution */
drh9cfcf5d2002-01-29 18:41:24 +0000806){
807 int i;
808 Vdbe *v;
809 int nCol;
810 int onError;
811 int addr;
812 int extra;
drh0ca3e242002-01-29 23:07:02 +0000813 int iCur;
814 Index *pIdx;
815 int seenReplace = 0;
danielk1977cfe9a692004-06-16 12:00:29 +0000816 int jumpInst1=0, jumpInst2;
drhf0863fe2005-06-12 21:35:51 +0000817 int hasTwoRowids = (isUpdate && rowidChng);
drh9cfcf5d2002-01-29 18:41:24 +0000818
danielk19774adee202004-05-08 08:23:19 +0000819 v = sqlite3GetVdbe(pParse);
drh9cfcf5d2002-01-29 18:41:24 +0000820 assert( v!=0 );
drh417be792002-03-03 18:59:40 +0000821 assert( pTab->pSelect==0 ); /* This table is not a VIEW */
drh9cfcf5d2002-01-29 18:41:24 +0000822 nCol = pTab->nCol;
823
824 /* Test all NOT NULL constraints.
825 */
826 for(i=0; i<nCol; i++){
drh0ca3e242002-01-29 23:07:02 +0000827 if( i==pTab->iPKey ){
drh0ca3e242002-01-29 23:07:02 +0000828 continue;
829 }
drh9cfcf5d2002-01-29 18:41:24 +0000830 onError = pTab->aCol[i].notNull;
drh0ca3e242002-01-29 23:07:02 +0000831 if( onError==OE_None ) continue;
drh9cfcf5d2002-01-29 18:41:24 +0000832 if( overrideError!=OE_Default ){
833 onError = overrideError;
drha996e472003-05-16 02:30:27 +0000834 }else if( onError==OE_Default ){
835 onError = OE_Abort;
drh9cfcf5d2002-01-29 18:41:24 +0000836 }
danielk19777977a172004-11-09 12:44:37 +0000837 if( onError==OE_Replace && pTab->aCol[i].pDflt==0 ){
drh9cfcf5d2002-01-29 18:41:24 +0000838 onError = OE_Abort;
839 }
danielk19774adee202004-05-08 08:23:19 +0000840 sqlite3VdbeAddOp(v, OP_Dup, nCol-1-i, 1);
841 addr = sqlite3VdbeAddOp(v, OP_NotNull, 1, 0);
danielk1977b84f96f2005-01-20 11:32:23 +0000842 assert( onError==OE_Rollback || onError==OE_Abort || onError==OE_Fail
843 || onError==OE_Ignore || onError==OE_Replace );
drh9cfcf5d2002-01-29 18:41:24 +0000844 switch( onError ){
drh1c928532002-01-31 15:54:21 +0000845 case OE_Rollback:
846 case OE_Abort:
847 case OE_Fail: {
drh483750b2003-01-29 18:46:51 +0000848 char *zMsg = 0;
danielk19774adee202004-05-08 08:23:19 +0000849 sqlite3VdbeAddOp(v, OP_Halt, SQLITE_CONSTRAINT, onError);
850 sqlite3SetString(&zMsg, pTab->zName, ".", pTab->aCol[i].zName,
drh41743982003-12-06 21:43:55 +0000851 " may not be NULL", (char*)0);
danielk19774adee202004-05-08 08:23:19 +0000852 sqlite3VdbeChangeP3(v, -1, zMsg, P3_DYNAMIC);
drh9cfcf5d2002-01-29 18:41:24 +0000853 break;
854 }
855 case OE_Ignore: {
drhf0863fe2005-06-12 21:35:51 +0000856 sqlite3VdbeAddOp(v, OP_Pop, nCol+1+hasTwoRowids, 0);
danielk19774adee202004-05-08 08:23:19 +0000857 sqlite3VdbeAddOp(v, OP_Goto, 0, ignoreDest);
drh9cfcf5d2002-01-29 18:41:24 +0000858 break;
859 }
860 case OE_Replace: {
danielk19777977a172004-11-09 12:44:37 +0000861 sqlite3ExprCode(pParse, pTab->aCol[i].pDflt);
danielk19774adee202004-05-08 08:23:19 +0000862 sqlite3VdbeAddOp(v, OP_Push, nCol-i, 0);
drh9cfcf5d2002-01-29 18:41:24 +0000863 break;
864 }
drh9cfcf5d2002-01-29 18:41:24 +0000865 }
drhd654be82005-09-20 17:42:23 +0000866 sqlite3VdbeJumpHere(v, addr);
drh9cfcf5d2002-01-29 18:41:24 +0000867 }
868
869 /* Test all CHECK constraints
870 */
drhffe07b22005-11-03 00:41:17 +0000871#ifndef SQLITE_OMIT_CHECK
872 if( pTab->pCheck ){
873 int allOk = sqlite3VdbeMakeLabel(v);
874 assert( pParse->ckOffset==0 );
875 pParse->ckOffset = nCol;
drh6275b882005-11-03 01:22:30 +0000876 sqlite3ExprIfTrue(pParse, pTab->pCheck, allOk, 1);
drhffe07b22005-11-03 00:41:17 +0000877 assert( pParse->ckOffset==nCol );
878 pParse->ckOffset = 0;
879 sqlite3VdbeAddOp(v, OP_Halt, SQLITE_CONSTRAINT, OE_Abort);
880 sqlite3VdbeResolveLabel(v, allOk);
881 }
882#endif /* !defined(SQLITE_OMIT_CHECK) */
drh9cfcf5d2002-01-29 18:41:24 +0000883
drh0bd1f4e2002-06-06 18:54:39 +0000884 /* If we have an INTEGER PRIMARY KEY, make sure the primary key
885 ** of the new record does not previously exist. Except, if this
886 ** is an UPDATE and the primary key is not changing, that is OK.
drh9cfcf5d2002-01-29 18:41:24 +0000887 */
drhf0863fe2005-06-12 21:35:51 +0000888 if( rowidChng ){
drh0ca3e242002-01-29 23:07:02 +0000889 onError = pTab->keyConf;
890 if( overrideError!=OE_Default ){
891 onError = overrideError;
drha996e472003-05-16 02:30:27 +0000892 }else if( onError==OE_Default ){
893 onError = OE_Abort;
drh0ca3e242002-01-29 23:07:02 +0000894 }
drha0217ba2003-06-01 01:10:33 +0000895
drh5383ae52003-06-04 12:23:30 +0000896 if( isUpdate ){
danielk19774adee202004-05-08 08:23:19 +0000897 sqlite3VdbeAddOp(v, OP_Dup, nCol+1, 1);
898 sqlite3VdbeAddOp(v, OP_Dup, nCol+1, 1);
899 jumpInst1 = sqlite3VdbeAddOp(v, OP_Eq, 0, 0);
drh5383ae52003-06-04 12:23:30 +0000900 }
danielk19774adee202004-05-08 08:23:19 +0000901 sqlite3VdbeAddOp(v, OP_Dup, nCol, 1);
902 jumpInst2 = sqlite3VdbeAddOp(v, OP_NotExists, base, 0);
drh5383ae52003-06-04 12:23:30 +0000903 switch( onError ){
904 default: {
905 onError = OE_Abort;
906 /* Fall thru into the next case */
drh79b0c952002-05-21 12:56:43 +0000907 }
drh5383ae52003-06-04 12:23:30 +0000908 case OE_Rollback:
909 case OE_Abort:
910 case OE_Fail: {
danielk19774adee202004-05-08 08:23:19 +0000911 sqlite3VdbeOp3(v, OP_Halt, SQLITE_CONSTRAINT, onError,
drh701a0ae2004-02-22 20:05:00 +0000912 "PRIMARY KEY must be unique", P3_STATIC);
drh5383ae52003-06-04 12:23:30 +0000913 break;
drh0ca3e242002-01-29 23:07:02 +0000914 }
drh5383ae52003-06-04 12:23:30 +0000915 case OE_Replace: {
danielk19774adee202004-05-08 08:23:19 +0000916 sqlite3GenerateRowIndexDelete(pParse->db, v, pTab, base, 0);
drh5383ae52003-06-04 12:23:30 +0000917 if( isUpdate ){
drhf0863fe2005-06-12 21:35:51 +0000918 sqlite3VdbeAddOp(v, OP_Dup, nCol+hasTwoRowids, 1);
drh7cf6e4d2004-05-19 14:56:55 +0000919 sqlite3VdbeAddOp(v, OP_MoveGe, base, 0);
drh5383ae52003-06-04 12:23:30 +0000920 }
921 seenReplace = 1;
922 break;
drh0ca3e242002-01-29 23:07:02 +0000923 }
drh5383ae52003-06-04 12:23:30 +0000924 case OE_Ignore: {
925 assert( seenReplace==0 );
drhf0863fe2005-06-12 21:35:51 +0000926 sqlite3VdbeAddOp(v, OP_Pop, nCol+1+hasTwoRowids, 0);
danielk19774adee202004-05-08 08:23:19 +0000927 sqlite3VdbeAddOp(v, OP_Goto, 0, ignoreDest);
drh5383ae52003-06-04 12:23:30 +0000928 break;
929 }
930 }
drhd654be82005-09-20 17:42:23 +0000931 sqlite3VdbeJumpHere(v, jumpInst2);
drh5383ae52003-06-04 12:23:30 +0000932 if( isUpdate ){
drhd654be82005-09-20 17:42:23 +0000933 sqlite3VdbeJumpHere(v, jumpInst1);
danielk19774adee202004-05-08 08:23:19 +0000934 sqlite3VdbeAddOp(v, OP_Dup, nCol+1, 1);
drh7cf6e4d2004-05-19 14:56:55 +0000935 sqlite3VdbeAddOp(v, OP_MoveGe, base, 0);
drh0ca3e242002-01-29 23:07:02 +0000936 }
937 }
drh0bd1f4e2002-06-06 18:54:39 +0000938
939 /* Test all UNIQUE constraints by creating entries for each UNIQUE
940 ** index and making sure that duplicate entries do not already exist.
941 ** Add the new records to the indices as we go.
942 */
drhb2fe7d82003-04-20 17:29:23 +0000943 extra = -1;
944 for(iCur=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, iCur++){
945 if( aIdxUsed && aIdxUsed[iCur]==0 ) continue; /* Skip unused indices */
946 extra++;
947
948 /* Create a key for accessing the index entry */
danielk19774adee202004-05-08 08:23:19 +0000949 sqlite3VdbeAddOp(v, OP_Dup, nCol+extra, 1);
drh9cfcf5d2002-01-29 18:41:24 +0000950 for(i=0; i<pIdx->nColumn; i++){
951 int idx = pIdx->aiColumn[i];
952 if( idx==pTab->iPKey ){
danielk19774adee202004-05-08 08:23:19 +0000953 sqlite3VdbeAddOp(v, OP_Dup, i+extra+nCol+1, 1);
drh9cfcf5d2002-01-29 18:41:24 +0000954 }else{
danielk19774adee202004-05-08 08:23:19 +0000955 sqlite3VdbeAddOp(v, OP_Dup, i+extra+nCol-idx, 1);
drh9cfcf5d2002-01-29 18:41:24 +0000956 }
957 }
drh7f057c92005-06-24 03:53:06 +0000958 jumpInst1 = sqlite3VdbeAddOp(v, OP_MakeIdxRec, pIdx->nColumn, 0);
danielk1977a37cdde2004-05-16 11:15:36 +0000959 sqlite3IndexAffinityStr(v, pIdx);
drhb2fe7d82003-04-20 17:29:23 +0000960
961 /* Find out what action to take in case there is an indexing conflict */
drh9cfcf5d2002-01-29 18:41:24 +0000962 onError = pIdx->onError;
drhb2fe7d82003-04-20 17:29:23 +0000963 if( onError==OE_None ) continue; /* pIdx is not a UNIQUE index */
drh9cfcf5d2002-01-29 18:41:24 +0000964 if( overrideError!=OE_Default ){
965 onError = overrideError;
drha996e472003-05-16 02:30:27 +0000966 }else if( onError==OE_Default ){
967 onError = OE_Abort;
drh9cfcf5d2002-01-29 18:41:24 +0000968 }
drh5383ae52003-06-04 12:23:30 +0000969 if( seenReplace ){
970 if( onError==OE_Ignore ) onError = OE_Replace;
971 else if( onError==OE_Fail ) onError = OE_Abort;
972 }
973
drhb2fe7d82003-04-20 17:29:23 +0000974
975 /* Check to see if the new index entry will be unique */
drhf0863fe2005-06-12 21:35:51 +0000976 sqlite3VdbeAddOp(v, OP_Dup, extra+nCol+1+hasTwoRowids, 1);
danielk19774adee202004-05-08 08:23:19 +0000977 jumpInst2 = sqlite3VdbeAddOp(v, OP_IsUnique, base+iCur+1, 0);
drhb2fe7d82003-04-20 17:29:23 +0000978
979 /* Generate code that executes if the new index entry is not unique */
danielk1977b84f96f2005-01-20 11:32:23 +0000980 assert( onError==OE_Rollback || onError==OE_Abort || onError==OE_Fail
981 || onError==OE_Ignore || onError==OE_Replace );
drh9cfcf5d2002-01-29 18:41:24 +0000982 switch( onError ){
drh1c928532002-01-31 15:54:21 +0000983 case OE_Rollback:
984 case OE_Abort:
985 case OE_Fail: {
drh37ed48e2003-08-05 13:13:38 +0000986 int j, n1, n2;
987 char zErrMsg[200];
988 strcpy(zErrMsg, pIdx->nColumn>1 ? "columns " : "column ");
989 n1 = strlen(zErrMsg);
990 for(j=0; j<pIdx->nColumn && n1<sizeof(zErrMsg)-30; j++){
991 char *zCol = pTab->aCol[pIdx->aiColumn[j]].zName;
992 n2 = strlen(zCol);
993 if( j>0 ){
994 strcpy(&zErrMsg[n1], ", ");
995 n1 += 2;
996 }
997 if( n1+n2>sizeof(zErrMsg)-30 ){
998 strcpy(&zErrMsg[n1], "...");
999 n1 += 3;
1000 break;
1001 }else{
1002 strcpy(&zErrMsg[n1], zCol);
1003 n1 += n2;
1004 }
1005 }
1006 strcpy(&zErrMsg[n1],
1007 pIdx->nColumn>1 ? " are not unique" : " is not unique");
danielk19774adee202004-05-08 08:23:19 +00001008 sqlite3VdbeOp3(v, OP_Halt, SQLITE_CONSTRAINT, onError, zErrMsg, 0);
drh9cfcf5d2002-01-29 18:41:24 +00001009 break;
1010 }
1011 case OE_Ignore: {
drh0ca3e242002-01-29 23:07:02 +00001012 assert( seenReplace==0 );
drhf0863fe2005-06-12 21:35:51 +00001013 sqlite3VdbeAddOp(v, OP_Pop, nCol+extra+3+hasTwoRowids, 0);
danielk19774adee202004-05-08 08:23:19 +00001014 sqlite3VdbeAddOp(v, OP_Goto, 0, ignoreDest);
drh9cfcf5d2002-01-29 18:41:24 +00001015 break;
1016 }
1017 case OE_Replace: {
danielk19774adee202004-05-08 08:23:19 +00001018 sqlite3GenerateRowDelete(pParse->db, v, pTab, base, 0);
drh9cfcf5d2002-01-29 18:41:24 +00001019 if( isUpdate ){
drhf0863fe2005-06-12 21:35:51 +00001020 sqlite3VdbeAddOp(v, OP_Dup, nCol+extra+1+hasTwoRowids, 1);
drh7cf6e4d2004-05-19 14:56:55 +00001021 sqlite3VdbeAddOp(v, OP_MoveGe, base, 0);
drh9cfcf5d2002-01-29 18:41:24 +00001022 }
drh0ca3e242002-01-29 23:07:02 +00001023 seenReplace = 1;
drh9cfcf5d2002-01-29 18:41:24 +00001024 break;
1025 }
drh9cfcf5d2002-01-29 18:41:24 +00001026 }
drh0bd1f4e2002-06-06 18:54:39 +00001027#if NULL_DISTINCT_FOR_UNIQUE
drhd654be82005-09-20 17:42:23 +00001028 sqlite3VdbeJumpHere(v, jumpInst1);
drh0bd1f4e2002-06-06 18:54:39 +00001029#endif
drhd654be82005-09-20 17:42:23 +00001030 sqlite3VdbeJumpHere(v, jumpInst2);
drh9cfcf5d2002-01-29 18:41:24 +00001031 }
1032}
drh0ca3e242002-01-29 23:07:02 +00001033
1034/*
1035** This routine generates code to finish the INSERT or UPDATE operation
danielk19774adee202004-05-08 08:23:19 +00001036** that was started by a prior call to sqlite3GenerateConstraintChecks.
drh0ca3e242002-01-29 23:07:02 +00001037** The stack must contain keys for all active indices followed by data
drhf0863fe2005-06-12 21:35:51 +00001038** and the rowid for the new entry. This routine creates the new
drh0ca3e242002-01-29 23:07:02 +00001039** entries in all indices and in the main table.
1040**
drhb419a922002-01-30 16:17:23 +00001041** The arguments to this routine should be the same as the first six
danielk19774adee202004-05-08 08:23:19 +00001042** arguments to sqlite3GenerateConstraintChecks.
drh0ca3e242002-01-29 23:07:02 +00001043*/
danielk19774adee202004-05-08 08:23:19 +00001044void sqlite3CompleteInsertion(
drh0ca3e242002-01-29 23:07:02 +00001045 Parse *pParse, /* The parser context */
1046 Table *pTab, /* the table into which we are inserting */
1047 int base, /* Index of a read/write cursor pointing at pTab */
1048 char *aIdxUsed, /* Which indices are used. NULL means all are used */
drhf0863fe2005-06-12 21:35:51 +00001049 int rowidChng, /* True if the record number will change */
drh70ce3f02003-04-15 19:22:22 +00001050 int isUpdate, /* True for UPDATE, False for INSERT */
1051 int newIdx /* Index of NEW table for triggers. -1 if none */
drh0ca3e242002-01-29 23:07:02 +00001052){
1053 int i;
1054 Vdbe *v;
1055 int nIdx;
1056 Index *pIdx;
danielk1977b28af712004-06-21 06:50:26 +00001057 int pik_flags;
drh0ca3e242002-01-29 23:07:02 +00001058
danielk19774adee202004-05-08 08:23:19 +00001059 v = sqlite3GetVdbe(pParse);
drh0ca3e242002-01-29 23:07:02 +00001060 assert( v!=0 );
drh417be792002-03-03 18:59:40 +00001061 assert( pTab->pSelect==0 ); /* This table is not a VIEW */
drh0ca3e242002-01-29 23:07:02 +00001062 for(nIdx=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, nIdx++){}
1063 for(i=nIdx-1; i>=0; i--){
1064 if( aIdxUsed && aIdxUsed[i]==0 ) continue;
drhf0863fe2005-06-12 21:35:51 +00001065 sqlite3VdbeAddOp(v, OP_IdxInsert, base+i+1, 0);
drh0ca3e242002-01-29 23:07:02 +00001066 }
danielk19774adee202004-05-08 08:23:19 +00001067 sqlite3VdbeAddOp(v, OP_MakeRecord, pTab->nCol, 0);
danielk1977a37cdde2004-05-16 11:15:36 +00001068 sqlite3TableAffinityStr(v, pTab);
danielk1977b84f96f2005-01-20 11:32:23 +00001069#ifndef SQLITE_OMIT_TRIGGER
drh70ce3f02003-04-15 19:22:22 +00001070 if( newIdx>=0 ){
danielk19774adee202004-05-08 08:23:19 +00001071 sqlite3VdbeAddOp(v, OP_Dup, 1, 0);
1072 sqlite3VdbeAddOp(v, OP_Dup, 1, 0);
drhf0863fe2005-06-12 21:35:51 +00001073 sqlite3VdbeAddOp(v, OP_Insert, newIdx, 0);
drh70ce3f02003-04-15 19:22:22 +00001074 }
danielk1977b84f96f2005-01-20 11:32:23 +00001075#endif
drh4794f732004-11-05 17:17:50 +00001076 if( pParse->nested ){
1077 pik_flags = 0;
1078 }else{
1079 pik_flags = (OPFLAG_NCHANGE|(isUpdate?0:OPFLAG_LASTROWID));
1080 }
drhf0863fe2005-06-12 21:35:51 +00001081 sqlite3VdbeAddOp(v, OP_Insert, base, pik_flags);
danielk1977b28af712004-06-21 06:50:26 +00001082
drhf0863fe2005-06-12 21:35:51 +00001083 if( isUpdate && rowidChng ){
danielk19774adee202004-05-08 08:23:19 +00001084 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
drh0ca3e242002-01-29 23:07:02 +00001085 }
1086}
drhcd446902004-02-24 01:05:31 +00001087
1088/*
drh290c1942004-08-21 17:54:45 +00001089** Generate code that will open cursors for a table and for all
drhcd446902004-02-24 01:05:31 +00001090** indices of that table. The "base" parameter is the cursor number used
1091** for the table. Indices are opened on subsequent cursors.
drhcd446902004-02-24 01:05:31 +00001092*/
drh290c1942004-08-21 17:54:45 +00001093void sqlite3OpenTableAndIndices(
1094 Parse *pParse, /* Parsing context */
1095 Table *pTab, /* Table to be opened */
1096 int base, /* Cursor number assigned to the table */
1097 int op /* OP_OpenRead or OP_OpenWrite */
1098){
drhcd446902004-02-24 01:05:31 +00001099 int i;
1100 Index *pIdx;
danielk19774adee202004-05-08 08:23:19 +00001101 Vdbe *v = sqlite3GetVdbe(pParse);
drhcd446902004-02-24 01:05:31 +00001102 assert( v!=0 );
danielk19774adee202004-05-08 08:23:19 +00001103 sqlite3VdbeAddOp(v, OP_Integer, pTab->iDb, 0);
drhad6d9462004-09-19 02:15:24 +00001104 VdbeComment((v, "# %s", pTab->zName));
drh29dda4a2005-07-21 18:23:20 +00001105 sqlite3VdbeAddOp(v, op, base, pTab->tnum);
danielk1977b4964b72004-05-18 01:23:38 +00001106 sqlite3VdbeAddOp(v, OP_SetNumColumns, base, pTab->nCol);
drhcd446902004-02-24 01:05:31 +00001107 for(i=1, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){
danielk19774adee202004-05-08 08:23:19 +00001108 sqlite3VdbeAddOp(v, OP_Integer, pIdx->iDb, 0);
drh29dda4a2005-07-21 18:23:20 +00001109 VdbeComment((v, "# %s", pIdx->zName));
drh290c1942004-08-21 17:54:45 +00001110 sqlite3VdbeOp3(v, op, i+base, pIdx->tnum,
drhd3d39e92004-05-20 22:16:29 +00001111 (char*)&pIdx->keyInfo, P3_KEYINFO);
drhcd446902004-02-24 01:05:31 +00001112 }
drh290c1942004-08-21 17:54:45 +00001113 if( pParse->nTab<=base+i ){
1114 pParse->nTab = base+i;
1115 }
drhcd446902004-02-24 01:05:31 +00001116}