blob: 025c4205ac5d72df65f48c9e8d4ae9ff1e9cec46 [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**
drhdca76842004-12-07 14:06:13 +000015** $Id: insert.c,v 1.127 2004/12/07 14:06:13 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
97
98/*
drh1ccde152000-06-17 13:12:39 +000099** This routine is call to handle SQL of the following forms:
drhcce7d172000-05-31 15:34:51 +0000100**
101** insert into TABLE (IDLIST) values(EXPRLIST)
drh1ccde152000-06-17 13:12:39 +0000102** insert into TABLE (IDLIST) select
drhcce7d172000-05-31 15:34:51 +0000103**
drh1ccde152000-06-17 13:12:39 +0000104** The IDLIST following the table name is always optional. If omitted,
105** then a list of all columns for the table is substituted. The IDLIST
drh967e8b72000-06-21 13:59:10 +0000106** appears in the pColumn parameter. pColumn is NULL if IDLIST is omitted.
drh1ccde152000-06-17 13:12:39 +0000107**
108** The pList parameter holds EXPRLIST in the first form of the INSERT
109** statement above, and pSelect is NULL. For the second form, pList is
110** NULL and pSelect is a pointer to the select statement used to generate
111** data for the insert.
drh142e30d2002-08-28 03:00:58 +0000112**
113** The code generated follows one of three templates. For a simple
114** select with data coming from a VALUES clause, the code executes
115** once straight down through. The template looks like this:
116**
117** open write cursor to <table> and its indices
118** puts VALUES clause expressions onto the stack
119** write the resulting record into <table>
120** cleanup
121**
122** If the statement is of the form
123**
124** INSERT INTO <table> SELECT ...
125**
126** And the SELECT clause does not read from <table> at any time, then
127** the generated code follows this template:
128**
129** goto B
130** A: setup for the SELECT
131** loop over the tables in the SELECT
132** gosub C
133** end loop
134** cleanup after the SELECT
135** goto D
136** B: open write cursor to <table> and its indices
137** goto A
138** C: insert the select result into <table>
139** return
140** D: cleanup
141**
142** The third template is used if the insert statement takes its
143** values from a SELECT but the data is being inserted into a table
144** that is also read as part of the SELECT. In the third form,
145** we have to use a intermediate table to store the results of
146** the select. The template is like this:
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** C: insert the select result into the intermediate table
156** return
157** B: open a cursor to an intermediate table
158** goto A
159** D: open write cursor to <table> and its indices
160** loop over the intermediate table
161** transfer values form intermediate table into <table>
162** end the loop
163** cleanup
drhcce7d172000-05-31 15:34:51 +0000164*/
danielk19774adee202004-05-08 08:23:19 +0000165void sqlite3Insert(
drhcce7d172000-05-31 15:34:51 +0000166 Parse *pParse, /* Parser context */
drh113088e2003-03-20 01:16:58 +0000167 SrcList *pTabList, /* Name of table into which we are inserting */
drhcce7d172000-05-31 15:34:51 +0000168 ExprList *pList, /* List of values to be inserted */
drh5974a302000-06-07 14:42:26 +0000169 Select *pSelect, /* A SELECT statement to use as the data source */
drh9cfcf5d2002-01-29 18:41:24 +0000170 IdList *pColumn, /* Column names corresponding to IDLIST. */
171 int onError /* How to handle constraint errors */
drhcce7d172000-05-31 15:34:51 +0000172){
drh5974a302000-06-07 14:42:26 +0000173 Table *pTab; /* The table to insert into */
drh113088e2003-03-20 01:16:58 +0000174 char *zTab; /* Name of the table into which we are inserting */
drhe22a3342003-04-22 20:30:37 +0000175 const char *zDb; /* Name of the database holding this table */
drh5974a302000-06-07 14:42:26 +0000176 int i, j, idx; /* Loop counters */
177 Vdbe *v; /* Generate code into this virtual machine */
178 Index *pIdx; /* For looping over indices of the table */
drh967e8b72000-06-21 13:59:10 +0000179 int nColumn; /* Number of columns in the data */
danielk1977cfe9a692004-06-16 12:00:29 +0000180 int base = 0; /* VDBE Cursor number for pTab */
181 int iCont=0,iBreak=0; /* Beginning and end of the loop over srcTab */
drh9bb575f2004-09-06 17:24:11 +0000182 sqlite3 *db; /* The main database structure */
drh4a324312001-12-21 14:30:42 +0000183 int keyColumn = -1; /* Column that is the INTEGER PRIMARY KEY */
drh0ca3e242002-01-29 23:07:02 +0000184 int endOfLoop; /* Label for the end of the insertion loop */
drh142e30d2002-08-28 03:00:58 +0000185 int useTempTable; /* Store SELECT results in intermediate table */
danielk1977cfe9a692004-06-16 12:00:29 +0000186 int srcTab = 0; /* Data comes from this temporary cursor if >=0 */
187 int iSelectLoop = 0; /* Address of code that implements the SELECT */
188 int iCleanup = 0; /* Address of the cleanup code */
189 int iInsertBlock = 0; /* Address of the subroutine used to insert data */
190 int iCntMem = 0; /* Memory cell used for the row counter */
drh798da522004-11-04 04:42:28 +0000191 int newIdx = -1; /* Cursor for the NEW table */
drh2958a4e2004-11-12 03:56:15 +0000192 Db *pDb; /* The database containing table being inserted into */
193 int counterMem = 0; /* Memory cell holding AUTOINCREMENT counter */
drhcce7d172000-05-31 15:34:51 +0000194
drh798da522004-11-04 04:42:28 +0000195#ifndef SQLITE_OMIT_TRIGGER
196 int isView; /* True if attempting to insert into a view */
drhdca76842004-12-07 14:06:13 +0000197 int triggers_exist = 0; /* True if there are FOR EACH ROW triggers */
drh798da522004-11-04 04:42:28 +0000198#endif
danielk1977c3f9bad2002-05-15 08:30:12 +0000199
drhf3388142004-11-13 03:48:06 +0000200#ifndef SQLITE_OMIT_AUTOINCREMENT
201 int counterRowid; /* Memory cell holding rowid of autoinc counter */
202#endif
203
danielk197724b03fd2004-05-10 10:34:34 +0000204 if( pParse->nErr || sqlite3_malloc_failed ) goto insert_cleanup;
drhecdc7532001-09-23 02:35:53 +0000205 db = pParse->db;
drhdaffd0e2001-04-11 14:28:42 +0000206
drh1ccde152000-06-17 13:12:39 +0000207 /* Locate the table into which we will be inserting new information.
208 */
drh113088e2003-03-20 01:16:58 +0000209 assert( pTabList->nSrc==1 );
210 zTab = pTabList->a[0].zName;
drhdaffd0e2001-04-11 14:28:42 +0000211 if( zTab==0 ) goto insert_cleanup;
danielk19774adee202004-05-08 08:23:19 +0000212 pTab = sqlite3SrcListLookup(pParse, pTabList);
danielk1977c3f9bad2002-05-15 08:30:12 +0000213 if( pTab==0 ){
danielk1977c3f9bad2002-05-15 08:30:12 +0000214 goto insert_cleanup;
215 }
drhe22a3342003-04-22 20:30:37 +0000216 assert( pTab->iDb<db->nDb );
drh2958a4e2004-11-12 03:56:15 +0000217 pDb = &db->aDb[pTab->iDb];
218 zDb = pDb->zName;
danielk19774adee202004-05-08 08:23:19 +0000219 if( sqlite3AuthCheck(pParse, SQLITE_INSERT, pTab->zName, 0, zDb) ){
drh1962bda2003-01-12 19:33:52 +0000220 goto insert_cleanup;
221 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000222
drhb7f91642004-10-31 02:22:47 +0000223 /* Figure out if we have any triggers and if the table being
224 ** inserted into is a view
225 */
226#ifndef SQLITE_OMIT_TRIGGER
drhdca76842004-12-07 14:06:13 +0000227 triggers_exist = sqlite3TriggersExist(pParse, pTab, TK_INSERT, 0);
drhb7f91642004-10-31 02:22:47 +0000228 isView = pTab->pSelect!=0;
229#else
drhdca76842004-12-07 14:06:13 +0000230# define triggers_exist 0
drhb7f91642004-10-31 02:22:47 +0000231# define isView 0
232#endif
233#ifdef SQLITE_OMIT_VIEW
234# undef isView
235# define isView 0
236#endif
237
danielk1977c3f9bad2002-05-15 08:30:12 +0000238 /* Ensure that:
239 * (a) the table is not read-only,
240 * (b) that if it is a view then ON INSERT triggers exist
241 */
drhdca76842004-12-07 14:06:13 +0000242 if( sqlite3IsReadOnly(pParse, pTab, triggers_exist) ){
danielk1977c3f9bad2002-05-15 08:30:12 +0000243 goto insert_cleanup;
244 }
drha76b5df2002-02-23 02:32:10 +0000245 if( pTab==0 ) goto insert_cleanup;
drh1ccde152000-06-17 13:12:39 +0000246
drhf573c992002-07-31 00:32:50 +0000247 /* If pTab is really a view, make sure it has been initialized.
248 */
danielk19774adee202004-05-08 08:23:19 +0000249 if( isView && sqlite3ViewGetColumnNames(pParse, pTab) ){
drh5cf590c2003-04-24 01:45:04 +0000250 goto insert_cleanup;
drhf573c992002-07-31 00:32:50 +0000251 }
252
danielk19777cedc8d2004-06-10 10:50:08 +0000253 /* Ensure all required collation sequences are available. */
254 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
255 if( sqlite3CheckIndexCollSeq(pParse, pIdx) ){
256 goto insert_cleanup;
257 }
258 }
259
drh1ccde152000-06-17 13:12:39 +0000260 /* Allocate a VDBE
261 */
danielk19774adee202004-05-08 08:23:19 +0000262 v = sqlite3GetVdbe(pParse);
drh5974a302000-06-07 14:42:26 +0000263 if( v==0 ) goto insert_cleanup;
drh4794f732004-11-05 17:17:50 +0000264 if( pParse->nested==0 ) sqlite3VdbeCountChanges(v);
drhdca76842004-12-07 14:06:13 +0000265 sqlite3BeginWriteOperation(pParse, pSelect || triggers_exist, pTab->iDb);
drh1ccde152000-06-17 13:12:39 +0000266
danielk1977c3f9bad2002-05-15 08:30:12 +0000267 /* if there are row triggers, allocate a temp table for new.* references. */
drhdca76842004-12-07 14:06:13 +0000268 if( triggers_exist ){
danielk1977c3f9bad2002-05-15 08:30:12 +0000269 newIdx = pParse->nTab++;
danielk1977f29ce552002-05-19 23:43:12 +0000270 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000271
drh2958a4e2004-11-12 03:56:15 +0000272#ifndef SQLITE_OMIT_AUTOINCREMENT
273 /* If this is an AUTOINCREMENT table, look up the sequence number in the
drhf3388142004-11-13 03:48:06 +0000274 ** sqlite_sequence table and store it in memory cell counterMem. Also
275 ** remember the rowid of the sqlite_sequence table entry in memory cell
276 ** counterRowid.
drh2958a4e2004-11-12 03:56:15 +0000277 */
278 if( pTab->autoInc ){
drhf3388142004-11-13 03:48:06 +0000279 int iCur = pParse->nTab;
280 int base = sqlite3VdbeCurrentAddr(v);
281 counterRowid = pParse->nMem++;
282 counterMem = pParse->nMem++;
283 sqlite3VdbeAddOp(v, OP_Integer, pTab->iDb, 0);
284 sqlite3VdbeAddOp(v, OP_OpenRead, iCur, pDb->pSeqTab->tnum);
285 sqlite3VdbeAddOp(v, OP_SetNumColumns, iCur, 2);
286 sqlite3VdbeAddOp(v, OP_Rewind, iCur, base+13);
287 sqlite3VdbeAddOp(v, OP_Column, iCur, 0);
288 sqlite3VdbeOp3(v, OP_String8, 0, 0, pTab->zName, 0);
289 sqlite3VdbeAddOp(v, OP_Ne, 28417, base+12);
290 sqlite3VdbeAddOp(v, OP_Recno, iCur, 0);
291 sqlite3VdbeAddOp(v, OP_MemStore, counterRowid, 1);
292 sqlite3VdbeAddOp(v, OP_Column, iCur, 1);
293 sqlite3VdbeAddOp(v, OP_MemStore, counterMem, 1);
294 sqlite3VdbeAddOp(v, OP_Goto, 0, base+13);
295 sqlite3VdbeAddOp(v, OP_Next, iCur, base+4);
296 sqlite3VdbeAddOp(v, OP_Close, iCur, 0);
drh2958a4e2004-11-12 03:56:15 +0000297 }
298#endif /* SQLITE_OMIT_AUTOINCREMENT */
299
drh1ccde152000-06-17 13:12:39 +0000300 /* Figure out how many columns of data are supplied. If the data
drh142e30d2002-08-28 03:00:58 +0000301 ** is coming from a SELECT statement, then this step also generates
302 ** all the code to implement the SELECT statement and invoke a subroutine
303 ** to process each row of the result. (Template 2.) If the SELECT
304 ** statement uses the the table that is being inserted into, then the
305 ** subroutine is also coded here. That subroutine stores the SELECT
306 ** results in a temporary table. (Template 3.)
drh1ccde152000-06-17 13:12:39 +0000307 */
drh5974a302000-06-07 14:42:26 +0000308 if( pSelect ){
drh142e30d2002-08-28 03:00:58 +0000309 /* Data is coming from a SELECT. Generate code to implement that SELECT
310 */
311 int rc, iInitCode;
danielk19774adee202004-05-08 08:23:19 +0000312 iInitCode = sqlite3VdbeAddOp(v, OP_Goto, 0, 0);
313 iSelectLoop = sqlite3VdbeCurrentAddr(v);
314 iInsertBlock = sqlite3VdbeMakeLabel(v);
danielk197784ac9d02004-05-18 09:58:06 +0000315 rc = sqlite3Select(pParse, pSelect, SRT_Subroutine, iInsertBlock, 0,0,0,0);
danielk197724b03fd2004-05-10 10:34:34 +0000316 if( rc || pParse->nErr || sqlite3_malloc_failed ) goto insert_cleanup;
danielk19774adee202004-05-08 08:23:19 +0000317 iCleanup = sqlite3VdbeMakeLabel(v);
318 sqlite3VdbeAddOp(v, OP_Goto, 0, iCleanup);
drh5974a302000-06-07 14:42:26 +0000319 assert( pSelect->pEList );
drh967e8b72000-06-21 13:59:10 +0000320 nColumn = pSelect->pEList->nExpr;
drh142e30d2002-08-28 03:00:58 +0000321
322 /* Set useTempTable to TRUE if the result of the SELECT statement
323 ** should be written into a temporary table. Set to FALSE if each
324 ** row of the SELECT can be written directly into the result table.
drh048c5302003-04-03 01:50:44 +0000325 **
326 ** A temp table must be used if the table being updated is also one
327 ** of the tables being read by the SELECT statement. Also use a
328 ** temp table in the case of row triggers.
drh142e30d2002-08-28 03:00:58 +0000329 */
drhdca76842004-12-07 14:06:13 +0000330 if( triggers_exist ){
drh048c5302003-04-03 01:50:44 +0000331 useTempTable = 1;
332 }else{
drha42707b2004-09-17 17:23:15 +0000333 int addr = 0;
drh048c5302003-04-03 01:50:44 +0000334 useTempTable = 0;
drha42707b2004-09-17 17:23:15 +0000335 while( useTempTable==0 ){
336 VdbeOp *pOp;
337 addr = sqlite3VdbeFindOp(v, addr, OP_OpenRead, pTab->tnum);
338 if( addr==0 ) break;
339 pOp = sqlite3VdbeGetOp(v, addr-2);
drh048c5302003-04-03 01:50:44 +0000340 if( pOp->opcode==OP_Integer && pOp->p1==pTab->iDb ){
341 useTempTable = 1;
342 }
343 }
344 }
drh142e30d2002-08-28 03:00:58 +0000345
346 if( useTempTable ){
347 /* Generate the subroutine that SELECT calls to process each row of
348 ** the result. Store the result in a temporary table
349 */
350 srcTab = pParse->nTab++;
danielk19774adee202004-05-08 08:23:19 +0000351 sqlite3VdbeResolveLabel(v, iInsertBlock);
352 sqlite3VdbeAddOp(v, OP_MakeRecord, nColumn, 0);
danielk1977a37cdde2004-05-16 11:15:36 +0000353 sqlite3TableAffinityStr(v, pTab);
danielk19774adee202004-05-08 08:23:19 +0000354 sqlite3VdbeAddOp(v, OP_NewRecno, srcTab, 0);
355 sqlite3VdbeAddOp(v, OP_Pull, 1, 0);
356 sqlite3VdbeAddOp(v, OP_PutIntKey, srcTab, 0);
357 sqlite3VdbeAddOp(v, OP_Return, 0, 0);
drh142e30d2002-08-28 03:00:58 +0000358
359 /* The following code runs first because the GOTO at the very top
360 ** of the program jumps to it. Create the temporary table, then jump
361 ** back up and execute the SELECT code above.
362 */
danielk19774adee202004-05-08 08:23:19 +0000363 sqlite3VdbeChangeP2(v, iInitCode, sqlite3VdbeCurrentAddr(v));
364 sqlite3VdbeAddOp(v, OP_OpenTemp, srcTab, 0);
drhb6f54522004-05-20 02:42:16 +0000365 sqlite3VdbeAddOp(v, OP_SetNumColumns, srcTab, nColumn);
danielk19774adee202004-05-08 08:23:19 +0000366 sqlite3VdbeAddOp(v, OP_Goto, 0, iSelectLoop);
367 sqlite3VdbeResolveLabel(v, iCleanup);
drh142e30d2002-08-28 03:00:58 +0000368 }else{
danielk19774adee202004-05-08 08:23:19 +0000369 sqlite3VdbeChangeP2(v, iInitCode, sqlite3VdbeCurrentAddr(v));
drh142e30d2002-08-28 03:00:58 +0000370 }
drh5974a302000-06-07 14:42:26 +0000371 }else{
drh142e30d2002-08-28 03:00:58 +0000372 /* This is the case if the data for the INSERT is coming from a VALUES
373 ** clause
374 */
drhad3cab52002-05-24 02:04:32 +0000375 SrcList dummy;
drhdaffd0e2001-04-11 14:28:42 +0000376 assert( pList!=0 );
drh5974a302000-06-07 14:42:26 +0000377 srcTab = -1;
drh142e30d2002-08-28 03:00:58 +0000378 useTempTable = 0;
drh5974a302000-06-07 14:42:26 +0000379 assert( pList );
drh967e8b72000-06-21 13:59:10 +0000380 nColumn = pList->nExpr;
drhad3cab52002-05-24 02:04:32 +0000381 dummy.nSrc = 0;
drhe64e7b22002-02-18 13:56:36 +0000382 for(i=0; i<nColumn; i++){
drh290c1942004-08-21 17:54:45 +0000383 if( sqlite3ExprResolveAndCheck(pParse,&dummy,0,pList->a[i].pExpr,0,0) ){
drhb04a5d82002-04-12 03:55:15 +0000384 goto insert_cleanup;
385 }
drhe64e7b22002-02-18 13:56:36 +0000386 }
drh5974a302000-06-07 14:42:26 +0000387 }
drh1ccde152000-06-17 13:12:39 +0000388
389 /* Make sure the number of columns in the source data matches the number
390 ** of columns to be inserted into the table.
391 */
drh967e8b72000-06-21 13:59:10 +0000392 if( pColumn==0 && nColumn!=pTab->nCol ){
danielk19774adee202004-05-08 08:23:19 +0000393 sqlite3ErrorMsg(pParse,
drhda93d232003-03-31 02:12:46 +0000394 "table %S has %d columns but %d values were supplied",
395 pTabList, 0, pTab->nCol, nColumn);
drhcce7d172000-05-31 15:34:51 +0000396 goto insert_cleanup;
397 }
drh967e8b72000-06-21 13:59:10 +0000398 if( pColumn!=0 && nColumn!=pColumn->nId ){
danielk19774adee202004-05-08 08:23:19 +0000399 sqlite3ErrorMsg(pParse, "%d values for %d columns", nColumn, pColumn->nId);
drhcce7d172000-05-31 15:34:51 +0000400 goto insert_cleanup;
401 }
drh1ccde152000-06-17 13:12:39 +0000402
403 /* If the INSERT statement included an IDLIST term, then make sure
404 ** all elements of the IDLIST really are columns of the table and
405 ** remember the column indices.
drhc8392582001-12-31 02:48:51 +0000406 **
407 ** If the table has an INTEGER PRIMARY KEY column and that column
408 ** is named in the IDLIST, then record in the keyColumn variable
409 ** the index into IDLIST of the primary key column. keyColumn is
410 ** the index of the primary key as it appears in IDLIST, not as
411 ** is appears in the original table. (The index of the primary
412 ** key in the original table is pTab->iPKey.)
drh1ccde152000-06-17 13:12:39 +0000413 */
drh967e8b72000-06-21 13:59:10 +0000414 if( pColumn ){
415 for(i=0; i<pColumn->nId; i++){
416 pColumn->a[i].idx = -1;
drhcce7d172000-05-31 15:34:51 +0000417 }
drh967e8b72000-06-21 13:59:10 +0000418 for(i=0; i<pColumn->nId; i++){
drhcce7d172000-05-31 15:34:51 +0000419 for(j=0; j<pTab->nCol; j++){
danielk19774adee202004-05-08 08:23:19 +0000420 if( sqlite3StrICmp(pColumn->a[i].zName, pTab->aCol[j].zName)==0 ){
drh967e8b72000-06-21 13:59:10 +0000421 pColumn->a[i].idx = j;
drh4a324312001-12-21 14:30:42 +0000422 if( j==pTab->iPKey ){
drh9aa028d2001-12-22 21:48:29 +0000423 keyColumn = i;
drh4a324312001-12-21 14:30:42 +0000424 }
drhcce7d172000-05-31 15:34:51 +0000425 break;
426 }
427 }
428 if( j>=pTab->nCol ){
danielk19774adee202004-05-08 08:23:19 +0000429 if( sqlite3IsRowid(pColumn->a[i].zName) ){
drha0217ba2003-06-01 01:10:33 +0000430 keyColumn = i;
431 }else{
danielk19774adee202004-05-08 08:23:19 +0000432 sqlite3ErrorMsg(pParse, "table %S has no column named %s",
drha0217ba2003-06-01 01:10:33 +0000433 pTabList, 0, pColumn->a[i].zName);
434 pParse->nErr++;
435 goto insert_cleanup;
436 }
drhcce7d172000-05-31 15:34:51 +0000437 }
438 }
439 }
drh1ccde152000-06-17 13:12:39 +0000440
drhaacc5432002-01-06 17:07:40 +0000441 /* If there is no IDLIST term but the table has an integer primary
drhc8392582001-12-31 02:48:51 +0000442 ** key, the set the keyColumn variable to the primary key column index
443 ** in the original table definition.
drh4a324312001-12-21 14:30:42 +0000444 */
445 if( pColumn==0 ){
446 keyColumn = pTab->iPKey;
447 }
448
drh142e30d2002-08-28 03:00:58 +0000449 /* Open the temp table for FOR EACH ROW triggers
450 */
drhdca76842004-12-07 14:06:13 +0000451 if( triggers_exist ){
danielk19774adee202004-05-08 08:23:19 +0000452 sqlite3VdbeAddOp(v, OP_OpenPseudo, newIdx, 0);
danielk197784ac9d02004-05-18 09:58:06 +0000453 sqlite3VdbeAddOp(v, OP_SetNumColumns, newIdx, pTab->nCol);
danielk1977f29ce552002-05-19 23:43:12 +0000454 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000455
drhfeeb1392002-04-09 03:28:01 +0000456 /* Initialize the count of rows to be inserted
457 */
drh142e30d2002-08-28 03:00:58 +0000458 if( db->flags & SQLITE_CountRows ){
459 iCntMem = pParse->nMem++;
danielk19774adee202004-05-08 08:23:19 +0000460 sqlite3VdbeAddOp(v, OP_Integer, 0, 0);
461 sqlite3VdbeAddOp(v, OP_MemStore, iCntMem, 1);
drhfeeb1392002-04-09 03:28:01 +0000462 }
463
danielk1977c3f9bad2002-05-15 08:30:12 +0000464 /* Open tables and indices if there are no row triggers */
drhdca76842004-12-07 14:06:13 +0000465 if( !triggers_exist ){
danielk1977c3f9bad2002-05-15 08:30:12 +0000466 base = pParse->nTab;
drh290c1942004-08-21 17:54:45 +0000467 sqlite3OpenTableAndIndices(pParse, pTab, base, OP_OpenWrite);
danielk1977c3f9bad2002-05-15 08:30:12 +0000468 }
469
drh142e30d2002-08-28 03:00:58 +0000470 /* If the data source is a temporary table, then we have to create
drh1ccde152000-06-17 13:12:39 +0000471 ** a loop because there might be multiple rows of data. If the data
drh142e30d2002-08-28 03:00:58 +0000472 ** source is a subroutine call from the SELECT statement, then we need
473 ** to launch the SELECT statement processing.
drh1ccde152000-06-17 13:12:39 +0000474 */
drh142e30d2002-08-28 03:00:58 +0000475 if( useTempTable ){
danielk19774adee202004-05-08 08:23:19 +0000476 iBreak = sqlite3VdbeMakeLabel(v);
477 sqlite3VdbeAddOp(v, OP_Rewind, srcTab, iBreak);
478 iCont = sqlite3VdbeCurrentAddr(v);
drh142e30d2002-08-28 03:00:58 +0000479 }else if( pSelect ){
danielk19774adee202004-05-08 08:23:19 +0000480 sqlite3VdbeAddOp(v, OP_Goto, 0, iSelectLoop);
481 sqlite3VdbeResolveLabel(v, iInsertBlock);
drh5974a302000-06-07 14:42:26 +0000482 }
drh1ccde152000-06-17 13:12:39 +0000483
drh5cf590c2003-04-24 01:45:04 +0000484 /* Run the BEFORE and INSTEAD OF triggers, if there are any
drh70ce3f02003-04-15 19:22:22 +0000485 */
danielk19774adee202004-05-08 08:23:19 +0000486 endOfLoop = sqlite3VdbeMakeLabel(v);
drhdca76842004-12-07 14:06:13 +0000487 if( triggers_exist & TRIGGER_BEFORE ){
danielk1977c3f9bad2002-05-15 08:30:12 +0000488
drh70ce3f02003-04-15 19:22:22 +0000489 /* build the NEW.* reference row. Note that if there is an INTEGER
490 ** PRIMARY KEY into which a NULL is being inserted, that NULL will be
491 ** translated into a unique ID for the row. But on a BEFORE trigger,
492 ** we do not know what the unique ID will be (because the insert has
493 ** not happened yet) so we substitute a rowid of -1
494 */
495 if( keyColumn<0 ){
danielk19774adee202004-05-08 08:23:19 +0000496 sqlite3VdbeAddOp(v, OP_Integer, -1, 0);
drh70ce3f02003-04-15 19:22:22 +0000497 }else if( useTempTable ){
danielk19774adee202004-05-08 08:23:19 +0000498 sqlite3VdbeAddOp(v, OP_Column, srcTab, keyColumn);
drh70ce3f02003-04-15 19:22:22 +0000499 }else if( pSelect ){
danielk19774adee202004-05-08 08:23:19 +0000500 sqlite3VdbeAddOp(v, OP_Dup, nColumn - keyColumn - 1, 1);
drh70ce3f02003-04-15 19:22:22 +0000501 }else{
danielk19774adee202004-05-08 08:23:19 +0000502 sqlite3ExprCode(pParse, pList->a[keyColumn].pExpr);
503 sqlite3VdbeAddOp(v, OP_NotNull, -1, sqlite3VdbeCurrentAddr(v)+3);
504 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
505 sqlite3VdbeAddOp(v, OP_Integer, -1, 0);
506 sqlite3VdbeAddOp(v, OP_MustBeInt, 0, 0);
drh70ce3f02003-04-15 19:22:22 +0000507 }
508
509 /* Create the new column data
510 */
danielk1977c3f9bad2002-05-15 08:30:12 +0000511 for(i=0; i<pTab->nCol; i++){
512 if( pColumn==0 ){
drh9adf9ac2002-05-15 11:44:13 +0000513 j = i;
danielk1977c3f9bad2002-05-15 08:30:12 +0000514 }else{
drh9adf9ac2002-05-15 11:44:13 +0000515 for(j=0; j<pColumn->nId; j++){
516 if( pColumn->a[j].idx==i ) break;
517 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000518 }
519 if( pColumn && j>=pColumn->nId ){
danielk19777977a172004-11-09 12:44:37 +0000520 sqlite3ExprCode(pParse, pTab->aCol[i].pDflt);
drh142e30d2002-08-28 03:00:58 +0000521 }else if( useTempTable ){
danielk19774adee202004-05-08 08:23:19 +0000522 sqlite3VdbeAddOp(v, OP_Column, srcTab, j);
drh142e30d2002-08-28 03:00:58 +0000523 }else if( pSelect ){
danielk19774adee202004-05-08 08:23:19 +0000524 sqlite3VdbeAddOp(v, OP_Dup, nColumn-j-1, 1);
danielk1977c3f9bad2002-05-15 08:30:12 +0000525 }else{
danielk19774adee202004-05-08 08:23:19 +0000526 sqlite3ExprCode(pParse, pList->a[j].pExpr);
danielk1977c3f9bad2002-05-15 08:30:12 +0000527 }
528 }
danielk19774adee202004-05-08 08:23:19 +0000529 sqlite3VdbeAddOp(v, OP_MakeRecord, pTab->nCol, 0);
danielk1977a37cdde2004-05-16 11:15:36 +0000530
531 /* If this is an INSERT on a view with an INSTEAD OF INSERT trigger,
532 ** do not attempt any conversions before assembling the record.
533 ** If this is a real table, attempt conversions as required by the
534 ** table column affinities.
535 */
536 if( !isView ){
537 sqlite3TableAffinityStr(v, pTab);
538 }
danielk19774adee202004-05-08 08:23:19 +0000539 sqlite3VdbeAddOp(v, OP_PutIntKey, newIdx, 0);
danielk1977c3f9bad2002-05-15 08:30:12 +0000540
drh5cf590c2003-04-24 01:45:04 +0000541 /* Fire BEFORE or INSTEAD OF triggers */
drhdca76842004-12-07 14:06:13 +0000542 if( sqlite3CodeRowTrigger(pParse, TK_INSERT, 0, TRIGGER_BEFORE, pTab,
drhb2fe7d82003-04-20 17:29:23 +0000543 newIdx, -1, onError, endOfLoop) ){
danielk1977f29ce552002-05-19 23:43:12 +0000544 goto insert_cleanup;
545 }
drh70ce3f02003-04-15 19:22:22 +0000546 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000547
drh70ce3f02003-04-15 19:22:22 +0000548 /* If any triggers exists, the opening of tables and indices is deferred
549 ** until now.
550 */
drhdca76842004-12-07 14:06:13 +0000551 if( triggers_exist && !isView ){
drh5cf590c2003-04-24 01:45:04 +0000552 base = pParse->nTab;
drh290c1942004-08-21 17:54:45 +0000553 sqlite3OpenTableAndIndices(pParse, pTab, base, OP_OpenWrite);
danielk1977c3f9bad2002-05-15 08:30:12 +0000554 }
555
drh4a324312001-12-21 14:30:42 +0000556 /* Push the record number for the new entry onto the stack. The
557 ** record number is a randomly generate integer created by NewRecno
558 ** except when the table has an INTEGER PRIMARY KEY column, in which
drhb419a922002-01-30 16:17:23 +0000559 ** case the record number is the same as that column.
drh1ccde152000-06-17 13:12:39 +0000560 */
drh5cf590c2003-04-24 01:45:04 +0000561 if( !isView ){
danielk1977c3f9bad2002-05-15 08:30:12 +0000562 if( keyColumn>=0 ){
drh142e30d2002-08-28 03:00:58 +0000563 if( useTempTable ){
danielk19774adee202004-05-08 08:23:19 +0000564 sqlite3VdbeAddOp(v, OP_Column, srcTab, keyColumn);
drh142e30d2002-08-28 03:00:58 +0000565 }else if( pSelect ){
danielk19774adee202004-05-08 08:23:19 +0000566 sqlite3VdbeAddOp(v, OP_Dup, nColumn - keyColumn - 1, 1);
danielk1977c3f9bad2002-05-15 08:30:12 +0000567 }else{
danielk19774adee202004-05-08 08:23:19 +0000568 sqlite3ExprCode(pParse, pList->a[keyColumn].pExpr);
danielk1977c3f9bad2002-05-15 08:30:12 +0000569 }
drh27a32782002-06-19 20:32:43 +0000570 /* If the PRIMARY KEY expression is NULL, then use OP_NewRecno
571 ** to generate a unique primary key value.
572 */
danielk19774adee202004-05-08 08:23:19 +0000573 sqlite3VdbeAddOp(v, OP_NotNull, -1, sqlite3VdbeCurrentAddr(v)+3);
574 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
drh2958a4e2004-11-12 03:56:15 +0000575 sqlite3VdbeAddOp(v, OP_NewRecno, base, counterMem);
danielk19774adee202004-05-08 08:23:19 +0000576 sqlite3VdbeAddOp(v, OP_MustBeInt, 0, 0);
danielk1977c3f9bad2002-05-15 08:30:12 +0000577 }else{
drhf3388142004-11-13 03:48:06 +0000578 sqlite3VdbeAddOp(v, OP_NewRecno, base, counterMem);
drh4a324312001-12-21 14:30:42 +0000579 }
drh2958a4e2004-11-12 03:56:15 +0000580#ifndef SQLITE_OMIT_AUTOINCREMENT
581 if( pTab->autoInc ){
582 sqlite3VdbeAddOp(v, OP_MemMax, counterMem, 0);
583 }
584#endif /* SQLITE_OMIT_AUTOINCREMENT */
drh4a324312001-12-21 14:30:42 +0000585
danielk1977c3f9bad2002-05-15 08:30:12 +0000586 /* Push onto the stack, data for all columns of the new entry, beginning
danielk1977f29ce552002-05-19 23:43:12 +0000587 ** with the first column.
588 */
danielk1977c3f9bad2002-05-15 08:30:12 +0000589 for(i=0; i<pTab->nCol; i++){
590 if( i==pTab->iPKey ){
drh9adf9ac2002-05-15 11:44:13 +0000591 /* The value of the INTEGER PRIMARY KEY column is always a NULL.
danielk1977f29ce552002-05-19 23:43:12 +0000592 ** Whenever this column is read, the record number will be substituted
593 ** in its place. So will fill this column with a NULL to avoid
594 ** taking up data space with information that will never be used. */
danielk19770f69c1e2004-05-29 11:24:50 +0000595 sqlite3VdbeAddOp(v, OP_String8, 0, 0);
drh9adf9ac2002-05-15 11:44:13 +0000596 continue;
danielk1977c3f9bad2002-05-15 08:30:12 +0000597 }
598 if( pColumn==0 ){
drh9adf9ac2002-05-15 11:44:13 +0000599 j = i;
danielk1977c3f9bad2002-05-15 08:30:12 +0000600 }else{
drh9adf9ac2002-05-15 11:44:13 +0000601 for(j=0; j<pColumn->nId; j++){
602 if( pColumn->a[j].idx==i ) break;
603 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000604 }
605 if( pColumn && j>=pColumn->nId ){
danielk19777977a172004-11-09 12:44:37 +0000606 sqlite3ExprCode(pParse, pTab->aCol[i].pDflt);
drh142e30d2002-08-28 03:00:58 +0000607 }else if( useTempTable ){
danielk19774adee202004-05-08 08:23:19 +0000608 sqlite3VdbeAddOp(v, OP_Column, srcTab, j);
drh142e30d2002-08-28 03:00:58 +0000609 }else if( pSelect ){
danielk19774adee202004-05-08 08:23:19 +0000610 sqlite3VdbeAddOp(v, OP_Dup, i+nColumn-j, 1);
danielk1977c3f9bad2002-05-15 08:30:12 +0000611 }else{
danielk19774adee202004-05-08 08:23:19 +0000612 sqlite3ExprCode(pParse, pList->a[j].pExpr);
drh5974a302000-06-07 14:42:26 +0000613 }
drhbed86902000-06-02 13:27:59 +0000614 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000615
616 /* Generate code to check constraints and generate index keys and
danielk1977f29ce552002-05-19 23:43:12 +0000617 ** do the insertion.
618 */
danielk19774adee202004-05-08 08:23:19 +0000619 sqlite3GenerateConstraintChecks(pParse, pTab, base, 0, keyColumn>=0,
drha0217ba2003-06-01 01:10:33 +0000620 0, onError, endOfLoop);
danielk19774adee202004-05-08 08:23:19 +0000621 sqlite3CompleteInsertion(pParse, pTab, base, 0,0,0,
drhdca76842004-12-07 14:06:13 +0000622 (triggers_exist & TRIGGER_AFTER)!=0 ? newIdx : -1);
drh5cf590c2003-04-24 01:45:04 +0000623 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000624
drh5cf590c2003-04-24 01:45:04 +0000625 /* Update the count of rows that are inserted
626 */
627 if( (db->flags & SQLITE_CountRows)!=0 ){
danielk19774adee202004-05-08 08:23:19 +0000628 sqlite3VdbeAddOp(v, OP_MemIncr, iCntMem, 0);
drh5974a302000-06-07 14:42:26 +0000629 }
drh1ccde152000-06-17 13:12:39 +0000630
drhdca76842004-12-07 14:06:13 +0000631 if( triggers_exist ){
danielk1977c3f9bad2002-05-15 08:30:12 +0000632 /* Close all tables opened */
drh5cf590c2003-04-24 01:45:04 +0000633 if( !isView ){
danielk19774adee202004-05-08 08:23:19 +0000634 sqlite3VdbeAddOp(v, OP_Close, base, 0);
danielk1977c3f9bad2002-05-15 08:30:12 +0000635 for(idx=1, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, idx++){
danielk19774adee202004-05-08 08:23:19 +0000636 sqlite3VdbeAddOp(v, OP_Close, idx+base, 0);
danielk1977c3f9bad2002-05-15 08:30:12 +0000637 }
638 }
drh1bee3d72001-10-15 00:44:35 +0000639
danielk1977c3f9bad2002-05-15 08:30:12 +0000640 /* Code AFTER triggers */
drhdca76842004-12-07 14:06:13 +0000641 if( sqlite3CodeRowTrigger(pParse, TK_INSERT, 0, TRIGGER_AFTER, pTab,
642 newIdx, -1, onError, endOfLoop) ){
danielk1977f29ce552002-05-19 23:43:12 +0000643 goto insert_cleanup;
644 }
drh1bee3d72001-10-15 00:44:35 +0000645 }
646
drh1ccde152000-06-17 13:12:39 +0000647 /* The bottom of the loop, if the data source is a SELECT statement
danielk1977f29ce552002-05-19 23:43:12 +0000648 */
danielk19774adee202004-05-08 08:23:19 +0000649 sqlite3VdbeResolveLabel(v, endOfLoop);
drh142e30d2002-08-28 03:00:58 +0000650 if( useTempTable ){
danielk19774adee202004-05-08 08:23:19 +0000651 sqlite3VdbeAddOp(v, OP_Next, srcTab, iCont);
652 sqlite3VdbeResolveLabel(v, iBreak);
653 sqlite3VdbeAddOp(v, OP_Close, srcTab, 0);
drh142e30d2002-08-28 03:00:58 +0000654 }else if( pSelect ){
danielk19774adee202004-05-08 08:23:19 +0000655 sqlite3VdbeAddOp(v, OP_Pop, nColumn, 0);
656 sqlite3VdbeAddOp(v, OP_Return, 0, 0);
657 sqlite3VdbeResolveLabel(v, iCleanup);
drh6b563442001-11-07 16:48:26 +0000658 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000659
drhdca76842004-12-07 14:06:13 +0000660 if( !triggers_exist ){
danielk1977c3f9bad2002-05-15 08:30:12 +0000661 /* Close all tables opened */
danielk19774adee202004-05-08 08:23:19 +0000662 sqlite3VdbeAddOp(v, OP_Close, base, 0);
danielk1977c3f9bad2002-05-15 08:30:12 +0000663 for(idx=1, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, idx++){
danielk19774adee202004-05-08 08:23:19 +0000664 sqlite3VdbeAddOp(v, OP_Close, idx+base, 0);
danielk1977c3f9bad2002-05-15 08:30:12 +0000665 }
drhcce7d172000-05-31 15:34:51 +0000666 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000667
drh2958a4e2004-11-12 03:56:15 +0000668#ifndef SQLITE_OMIT_AUTOINCREMENT
drhf3388142004-11-13 03:48:06 +0000669 /* Update the sqlite_sequence table by storing the content of the
670 ** counter value in memory counterMem back into the sqlite_sequence
671 ** table.
drh2958a4e2004-11-12 03:56:15 +0000672 */
673 if( pTab->autoInc ){
drhf3388142004-11-13 03:48:06 +0000674 int iCur = pParse->nTab;
675 int base = sqlite3VdbeCurrentAddr(v);
676 sqlite3VdbeAddOp(v, OP_Integer, pTab->iDb, 0);
677 sqlite3VdbeAddOp(v, OP_OpenWrite, iCur, pDb->pSeqTab->tnum);
678 sqlite3VdbeAddOp(v, OP_SetNumColumns, iCur, 2);
679 sqlite3VdbeAddOp(v, OP_MemLoad, counterRowid, 0);
680 sqlite3VdbeAddOp(v, OP_NotNull, -1, base+7);
681 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
682 sqlite3VdbeAddOp(v, OP_NewRecno, iCur, 0);
683 sqlite3VdbeOp3(v, OP_String8, 0, 0, pTab->zName, 0);
684 sqlite3VdbeAddOp(v, OP_MemLoad, counterMem, 0);
685 sqlite3VdbeAddOp(v, OP_MakeRecord, 2, 0);
686 sqlite3VdbeAddOp(v, OP_PutIntKey, iCur, 0);
687 sqlite3VdbeAddOp(v, OP_Close, iCur, 0);
drh2958a4e2004-11-12 03:56:15 +0000688 }
689#endif
690
drh1bee3d72001-10-15 00:44:35 +0000691 /*
danielk1977e7de6f22004-11-05 06:02:06 +0000692 ** Return the number of rows inserted. If this routine is
693 ** generating code because of a call to sqlite3NestedParse(), do not
694 ** invoke the callback function.
drh1bee3d72001-10-15 00:44:35 +0000695 */
danielk1977e7de6f22004-11-05 06:02:06 +0000696 if( db->flags & SQLITE_CountRows && pParse->nested==0 ){
danielk19774adee202004-05-08 08:23:19 +0000697 sqlite3VdbeAddOp(v, OP_MemLoad, iCntMem, 0);
698 sqlite3VdbeAddOp(v, OP_Callback, 1, 0);
danielk197722322fd2004-05-25 23:35:17 +0000699 sqlite3VdbeSetNumCols(v, 1);
danielk19773cf86062004-05-26 10:11:05 +0000700 sqlite3VdbeSetColName(v, 0, "rows inserted", P3_STATIC);
drh1bee3d72001-10-15 00:44:35 +0000701 }
drhcce7d172000-05-31 15:34:51 +0000702
703insert_cleanup:
danielk19774adee202004-05-08 08:23:19 +0000704 sqlite3SrcListDelete(pTabList);
705 if( pList ) sqlite3ExprListDelete(pList);
706 if( pSelect ) sqlite3SelectDelete(pSelect);
707 sqlite3IdListDelete(pColumn);
drhcce7d172000-05-31 15:34:51 +0000708}
drh9cfcf5d2002-01-29 18:41:24 +0000709
drh9cfcf5d2002-01-29 18:41:24 +0000710/*
711** Generate code to do a constraint check prior to an INSERT or an UPDATE.
712**
713** When this routine is called, the stack contains (from bottom to top)
drh0ca3e242002-01-29 23:07:02 +0000714** the following values:
715**
drhb2fe7d82003-04-20 17:29:23 +0000716** 1. The recno of the row to be updated before the update. This
drhb419a922002-01-30 16:17:23 +0000717** value is omitted unless we are doing an UPDATE that involves a
718** change to the record number.
drh0ca3e242002-01-29 23:07:02 +0000719**
drhb419a922002-01-30 16:17:23 +0000720** 2. The recno of the row after the update.
drh0ca3e242002-01-29 23:07:02 +0000721**
722** 3. The data in the first column of the entry after the update.
723**
724** i. Data from middle columns...
725**
726** N. The data in the last column of the entry after the update.
727**
drhb419a922002-01-30 16:17:23 +0000728** The old recno shown as entry (1) above is omitted unless both isUpdate
drh1c928532002-01-31 15:54:21 +0000729** and recnoChng are 1. isUpdate is true for UPDATEs and false for
730** INSERTs and recnoChng is true if the record number is being changed.
drh0ca3e242002-01-29 23:07:02 +0000731**
732** The code generated by this routine pushes additional entries onto
733** the stack which are the keys for new index entries for the new record.
734** The order of index keys is the same as the order of the indices on
735** the pTable->pIndex list. A key is only created for index i if
736** aIdxUsed!=0 and aIdxUsed[i]!=0.
drh9cfcf5d2002-01-29 18:41:24 +0000737**
738** This routine also generates code to check constraints. NOT NULL,
739** CHECK, and UNIQUE constraints are all checked. If a constraint fails,
drh1c928532002-01-31 15:54:21 +0000740** then the appropriate action is performed. There are five possible
741** actions: ROLLBACK, ABORT, FAIL, REPLACE, and IGNORE.
drh9cfcf5d2002-01-29 18:41:24 +0000742**
743** Constraint type Action What Happens
744** --------------- ---------- ----------------------------------------
drh1c928532002-01-31 15:54:21 +0000745** any ROLLBACK The current transaction is rolled back and
danielk197724b03fd2004-05-10 10:34:34 +0000746** sqlite3_exec() returns immediately with a
drh9cfcf5d2002-01-29 18:41:24 +0000747** return code of SQLITE_CONSTRAINT.
748**
drh1c928532002-01-31 15:54:21 +0000749** any ABORT Back out changes from the current command
750** only (do not do a complete rollback) then
danielk197724b03fd2004-05-10 10:34:34 +0000751** cause sqlite3_exec() to return immediately
drh1c928532002-01-31 15:54:21 +0000752** with SQLITE_CONSTRAINT.
753**
754** any FAIL Sqlite_exec() returns immediately with a
755** return code of SQLITE_CONSTRAINT. The
756** transaction is not rolled back and any
757** prior changes are retained.
758**
drh9cfcf5d2002-01-29 18:41:24 +0000759** any IGNORE The record number and data is popped from
760** the stack and there is an immediate jump
761** to label ignoreDest.
762**
763** NOT NULL REPLACE The NULL value is replace by the default
764** value for that column. If the default value
765** is NULL, the action is the same as ABORT.
766**
767** UNIQUE REPLACE The other row that conflicts with the row
768** being inserted is removed.
769**
770** CHECK REPLACE Illegal. The results in an exception.
771**
drh1c928532002-01-31 15:54:21 +0000772** Which action to take is determined by the overrideError parameter.
773** Or if overrideError==OE_Default, then the pParse->onError parameter
774** is used. Or if pParse->onError==OE_Default then the onError value
775** for the constraint is used.
drh9cfcf5d2002-01-29 18:41:24 +0000776**
drhaaab5722002-02-19 13:39:21 +0000777** The calling routine must open a read/write cursor for pTab with
drh9cfcf5d2002-01-29 18:41:24 +0000778** cursor number "base". All indices of pTab must also have open
779** read/write cursors with cursor number base+i for the i-th cursor.
780** Except, if there is no possibility of a REPLACE action then
781** cursors do not need to be open for indices where aIdxUsed[i]==0.
782**
783** If the isUpdate flag is true, it means that the "base" cursor is
784** initially pointing to an entry that is being updated. The isUpdate
785** flag causes extra code to be generated so that the "base" cursor
786** is still pointing at the same entry after the routine returns.
787** Without the isUpdate flag, the "base" cursor might be moved.
788*/
danielk19774adee202004-05-08 08:23:19 +0000789void sqlite3GenerateConstraintChecks(
drh9cfcf5d2002-01-29 18:41:24 +0000790 Parse *pParse, /* The parser context */
791 Table *pTab, /* the table into which we are inserting */
792 int base, /* Index of a read/write cursor pointing at pTab */
793 char *aIdxUsed, /* Which indices are used. NULL means all are used */
drh0ca3e242002-01-29 23:07:02 +0000794 int recnoChng, /* True if the record number will change */
drhb419a922002-01-30 16:17:23 +0000795 int isUpdate, /* True for UPDATE, False for INSERT */
drh9cfcf5d2002-01-29 18:41:24 +0000796 int overrideError, /* Override onError to this if not OE_Default */
drhb419a922002-01-30 16:17:23 +0000797 int ignoreDest /* Jump to this label on an OE_Ignore resolution */
drh9cfcf5d2002-01-29 18:41:24 +0000798){
799 int i;
800 Vdbe *v;
801 int nCol;
802 int onError;
803 int addr;
804 int extra;
drh0ca3e242002-01-29 23:07:02 +0000805 int iCur;
806 Index *pIdx;
807 int seenReplace = 0;
danielk1977cfe9a692004-06-16 12:00:29 +0000808 int jumpInst1=0, jumpInst2;
drh0ca3e242002-01-29 23:07:02 +0000809 int contAddr;
drhb419a922002-01-30 16:17:23 +0000810 int hasTwoRecnos = (isUpdate && recnoChng);
drh9cfcf5d2002-01-29 18:41:24 +0000811
danielk19774adee202004-05-08 08:23:19 +0000812 v = sqlite3GetVdbe(pParse);
drh9cfcf5d2002-01-29 18:41:24 +0000813 assert( v!=0 );
drh417be792002-03-03 18:59:40 +0000814 assert( pTab->pSelect==0 ); /* This table is not a VIEW */
drh9cfcf5d2002-01-29 18:41:24 +0000815 nCol = pTab->nCol;
816
817 /* Test all NOT NULL constraints.
818 */
819 for(i=0; i<nCol; i++){
drh0ca3e242002-01-29 23:07:02 +0000820 if( i==pTab->iPKey ){
drh0ca3e242002-01-29 23:07:02 +0000821 continue;
822 }
drh9cfcf5d2002-01-29 18:41:24 +0000823 onError = pTab->aCol[i].notNull;
drh0ca3e242002-01-29 23:07:02 +0000824 if( onError==OE_None ) continue;
drh9cfcf5d2002-01-29 18:41:24 +0000825 if( overrideError!=OE_Default ){
826 onError = overrideError;
drha996e472003-05-16 02:30:27 +0000827 }else if( onError==OE_Default ){
828 onError = OE_Abort;
drh9cfcf5d2002-01-29 18:41:24 +0000829 }
danielk19777977a172004-11-09 12:44:37 +0000830 if( onError==OE_Replace && pTab->aCol[i].pDflt==0 ){
drh9cfcf5d2002-01-29 18:41:24 +0000831 onError = OE_Abort;
832 }
danielk19774adee202004-05-08 08:23:19 +0000833 sqlite3VdbeAddOp(v, OP_Dup, nCol-1-i, 1);
834 addr = sqlite3VdbeAddOp(v, OP_NotNull, 1, 0);
drh9cfcf5d2002-01-29 18:41:24 +0000835 switch( onError ){
drh1c928532002-01-31 15:54:21 +0000836 case OE_Rollback:
837 case OE_Abort:
838 case OE_Fail: {
drh483750b2003-01-29 18:46:51 +0000839 char *zMsg = 0;
danielk19774adee202004-05-08 08:23:19 +0000840 sqlite3VdbeAddOp(v, OP_Halt, SQLITE_CONSTRAINT, onError);
841 sqlite3SetString(&zMsg, pTab->zName, ".", pTab->aCol[i].zName,
drh41743982003-12-06 21:43:55 +0000842 " may not be NULL", (char*)0);
danielk19774adee202004-05-08 08:23:19 +0000843 sqlite3VdbeChangeP3(v, -1, zMsg, P3_DYNAMIC);
drh9cfcf5d2002-01-29 18:41:24 +0000844 break;
845 }
846 case OE_Ignore: {
danielk19774adee202004-05-08 08:23:19 +0000847 sqlite3VdbeAddOp(v, OP_Pop, nCol+1+hasTwoRecnos, 0);
848 sqlite3VdbeAddOp(v, OP_Goto, 0, ignoreDest);
drh9cfcf5d2002-01-29 18:41:24 +0000849 break;
850 }
851 case OE_Replace: {
danielk19777977a172004-11-09 12:44:37 +0000852 sqlite3ExprCode(pParse, pTab->aCol[i].pDflt);
danielk19774adee202004-05-08 08:23:19 +0000853 sqlite3VdbeAddOp(v, OP_Push, nCol-i, 0);
drh9cfcf5d2002-01-29 18:41:24 +0000854 break;
855 }
drh0ca3e242002-01-29 23:07:02 +0000856 default: assert(0);
drh9cfcf5d2002-01-29 18:41:24 +0000857 }
danielk19774adee202004-05-08 08:23:19 +0000858 sqlite3VdbeChangeP2(v, addr, sqlite3VdbeCurrentAddr(v));
drh9cfcf5d2002-01-29 18:41:24 +0000859 }
860
861 /* Test all CHECK constraints
862 */
drh0bd1f4e2002-06-06 18:54:39 +0000863 /**** TBD ****/
drh9cfcf5d2002-01-29 18:41:24 +0000864
drh0bd1f4e2002-06-06 18:54:39 +0000865 /* If we have an INTEGER PRIMARY KEY, make sure the primary key
866 ** of the new record does not previously exist. Except, if this
867 ** is an UPDATE and the primary key is not changing, that is OK.
drh9cfcf5d2002-01-29 18:41:24 +0000868 */
drh5383ae52003-06-04 12:23:30 +0000869 if( recnoChng ){
drh0ca3e242002-01-29 23:07:02 +0000870 onError = pTab->keyConf;
871 if( overrideError!=OE_Default ){
872 onError = overrideError;
drha996e472003-05-16 02:30:27 +0000873 }else if( onError==OE_Default ){
874 onError = OE_Abort;
drh0ca3e242002-01-29 23:07:02 +0000875 }
drha0217ba2003-06-01 01:10:33 +0000876
drh5383ae52003-06-04 12:23:30 +0000877 if( isUpdate ){
danielk19774adee202004-05-08 08:23:19 +0000878 sqlite3VdbeAddOp(v, OP_Dup, nCol+1, 1);
879 sqlite3VdbeAddOp(v, OP_Dup, nCol+1, 1);
880 jumpInst1 = sqlite3VdbeAddOp(v, OP_Eq, 0, 0);
drh5383ae52003-06-04 12:23:30 +0000881 }
danielk19774adee202004-05-08 08:23:19 +0000882 sqlite3VdbeAddOp(v, OP_Dup, nCol, 1);
883 jumpInst2 = sqlite3VdbeAddOp(v, OP_NotExists, base, 0);
drh5383ae52003-06-04 12:23:30 +0000884 switch( onError ){
885 default: {
886 onError = OE_Abort;
887 /* Fall thru into the next case */
drh79b0c952002-05-21 12:56:43 +0000888 }
drh5383ae52003-06-04 12:23:30 +0000889 case OE_Rollback:
890 case OE_Abort:
891 case OE_Fail: {
danielk19774adee202004-05-08 08:23:19 +0000892 sqlite3VdbeOp3(v, OP_Halt, SQLITE_CONSTRAINT, onError,
drh701a0ae2004-02-22 20:05:00 +0000893 "PRIMARY KEY must be unique", P3_STATIC);
drh5383ae52003-06-04 12:23:30 +0000894 break;
drh0ca3e242002-01-29 23:07:02 +0000895 }
drh5383ae52003-06-04 12:23:30 +0000896 case OE_Replace: {
danielk19774adee202004-05-08 08:23:19 +0000897 sqlite3GenerateRowIndexDelete(pParse->db, v, pTab, base, 0);
drh5383ae52003-06-04 12:23:30 +0000898 if( isUpdate ){
danielk19774adee202004-05-08 08:23:19 +0000899 sqlite3VdbeAddOp(v, OP_Dup, nCol+hasTwoRecnos, 1);
drh7cf6e4d2004-05-19 14:56:55 +0000900 sqlite3VdbeAddOp(v, OP_MoveGe, base, 0);
drh5383ae52003-06-04 12:23:30 +0000901 }
902 seenReplace = 1;
903 break;
drh0ca3e242002-01-29 23:07:02 +0000904 }
drh5383ae52003-06-04 12:23:30 +0000905 case OE_Ignore: {
906 assert( seenReplace==0 );
danielk19774adee202004-05-08 08:23:19 +0000907 sqlite3VdbeAddOp(v, OP_Pop, nCol+1+hasTwoRecnos, 0);
908 sqlite3VdbeAddOp(v, OP_Goto, 0, ignoreDest);
drh5383ae52003-06-04 12:23:30 +0000909 break;
910 }
911 }
danielk19774adee202004-05-08 08:23:19 +0000912 contAddr = sqlite3VdbeCurrentAddr(v);
913 sqlite3VdbeChangeP2(v, jumpInst2, contAddr);
drh5383ae52003-06-04 12:23:30 +0000914 if( isUpdate ){
danielk19774adee202004-05-08 08:23:19 +0000915 sqlite3VdbeChangeP2(v, jumpInst1, contAddr);
916 sqlite3VdbeAddOp(v, OP_Dup, nCol+1, 1);
drh7cf6e4d2004-05-19 14:56:55 +0000917 sqlite3VdbeAddOp(v, OP_MoveGe, base, 0);
drh0ca3e242002-01-29 23:07:02 +0000918 }
919 }
drh0bd1f4e2002-06-06 18:54:39 +0000920
921 /* Test all UNIQUE constraints by creating entries for each UNIQUE
922 ** index and making sure that duplicate entries do not already exist.
923 ** Add the new records to the indices as we go.
924 */
drhb2fe7d82003-04-20 17:29:23 +0000925 extra = -1;
926 for(iCur=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, iCur++){
927 if( aIdxUsed && aIdxUsed[iCur]==0 ) continue; /* Skip unused indices */
928 extra++;
929
930 /* Create a key for accessing the index entry */
danielk19774adee202004-05-08 08:23:19 +0000931 sqlite3VdbeAddOp(v, OP_Dup, nCol+extra, 1);
drh9cfcf5d2002-01-29 18:41:24 +0000932 for(i=0; i<pIdx->nColumn; i++){
933 int idx = pIdx->aiColumn[i];
934 if( idx==pTab->iPKey ){
danielk19774adee202004-05-08 08:23:19 +0000935 sqlite3VdbeAddOp(v, OP_Dup, i+extra+nCol+1, 1);
drh9cfcf5d2002-01-29 18:41:24 +0000936 }else{
danielk19774adee202004-05-08 08:23:19 +0000937 sqlite3VdbeAddOp(v, OP_Dup, i+extra+nCol-idx, 1);
drh9cfcf5d2002-01-29 18:41:24 +0000938 }
939 }
danielk1977ededfd52004-06-17 07:53:01 +0000940 jumpInst1 = sqlite3VdbeAddOp(v, OP_MakeRecord, pIdx->nColumn, (1<<24));
danielk1977a37cdde2004-05-16 11:15:36 +0000941 sqlite3IndexAffinityStr(v, pIdx);
drhb2fe7d82003-04-20 17:29:23 +0000942
943 /* Find out what action to take in case there is an indexing conflict */
drh9cfcf5d2002-01-29 18:41:24 +0000944 onError = pIdx->onError;
drhb2fe7d82003-04-20 17:29:23 +0000945 if( onError==OE_None ) continue; /* pIdx is not a UNIQUE index */
drh9cfcf5d2002-01-29 18:41:24 +0000946 if( overrideError!=OE_Default ){
947 onError = overrideError;
drha996e472003-05-16 02:30:27 +0000948 }else if( onError==OE_Default ){
949 onError = OE_Abort;
drh9cfcf5d2002-01-29 18:41:24 +0000950 }
drh5383ae52003-06-04 12:23:30 +0000951 if( seenReplace ){
952 if( onError==OE_Ignore ) onError = OE_Replace;
953 else if( onError==OE_Fail ) onError = OE_Abort;
954 }
955
drhb2fe7d82003-04-20 17:29:23 +0000956
957 /* Check to see if the new index entry will be unique */
danielk19774adee202004-05-08 08:23:19 +0000958 sqlite3VdbeAddOp(v, OP_Dup, extra+nCol+1+hasTwoRecnos, 1);
959 jumpInst2 = sqlite3VdbeAddOp(v, OP_IsUnique, base+iCur+1, 0);
drhb2fe7d82003-04-20 17:29:23 +0000960
961 /* Generate code that executes if the new index entry is not unique */
drh9cfcf5d2002-01-29 18:41:24 +0000962 switch( onError ){
drh1c928532002-01-31 15:54:21 +0000963 case OE_Rollback:
964 case OE_Abort:
965 case OE_Fail: {
drh37ed48e2003-08-05 13:13:38 +0000966 int j, n1, n2;
967 char zErrMsg[200];
968 strcpy(zErrMsg, pIdx->nColumn>1 ? "columns " : "column ");
969 n1 = strlen(zErrMsg);
970 for(j=0; j<pIdx->nColumn && n1<sizeof(zErrMsg)-30; j++){
971 char *zCol = pTab->aCol[pIdx->aiColumn[j]].zName;
972 n2 = strlen(zCol);
973 if( j>0 ){
974 strcpy(&zErrMsg[n1], ", ");
975 n1 += 2;
976 }
977 if( n1+n2>sizeof(zErrMsg)-30 ){
978 strcpy(&zErrMsg[n1], "...");
979 n1 += 3;
980 break;
981 }else{
982 strcpy(&zErrMsg[n1], zCol);
983 n1 += n2;
984 }
985 }
986 strcpy(&zErrMsg[n1],
987 pIdx->nColumn>1 ? " are not unique" : " is not unique");
danielk19774adee202004-05-08 08:23:19 +0000988 sqlite3VdbeOp3(v, OP_Halt, SQLITE_CONSTRAINT, onError, zErrMsg, 0);
drh9cfcf5d2002-01-29 18:41:24 +0000989 break;
990 }
991 case OE_Ignore: {
drh0ca3e242002-01-29 23:07:02 +0000992 assert( seenReplace==0 );
danielk19774adee202004-05-08 08:23:19 +0000993 sqlite3VdbeAddOp(v, OP_Pop, nCol+extra+3+hasTwoRecnos, 0);
994 sqlite3VdbeAddOp(v, OP_Goto, 0, ignoreDest);
drh9cfcf5d2002-01-29 18:41:24 +0000995 break;
996 }
997 case OE_Replace: {
danielk19774adee202004-05-08 08:23:19 +0000998 sqlite3GenerateRowDelete(pParse->db, v, pTab, base, 0);
drh9cfcf5d2002-01-29 18:41:24 +0000999 if( isUpdate ){
danielk19774adee202004-05-08 08:23:19 +00001000 sqlite3VdbeAddOp(v, OP_Dup, nCol+extra+1+hasTwoRecnos, 1);
drh7cf6e4d2004-05-19 14:56:55 +00001001 sqlite3VdbeAddOp(v, OP_MoveGe, base, 0);
drh9cfcf5d2002-01-29 18:41:24 +00001002 }
drh0ca3e242002-01-29 23:07:02 +00001003 seenReplace = 1;
drh9cfcf5d2002-01-29 18:41:24 +00001004 break;
1005 }
drh0ca3e242002-01-29 23:07:02 +00001006 default: assert(0);
drh9cfcf5d2002-01-29 18:41:24 +00001007 }
danielk19774adee202004-05-08 08:23:19 +00001008 contAddr = sqlite3VdbeCurrentAddr(v);
danielk1977ededfd52004-06-17 07:53:01 +00001009 assert( contAddr<(1<<24) );
drh0bd1f4e2002-06-06 18:54:39 +00001010#if NULL_DISTINCT_FOR_UNIQUE
danielk1977ededfd52004-06-17 07:53:01 +00001011 sqlite3VdbeChangeP2(v, jumpInst1, contAddr | (1<<24));
drh0bd1f4e2002-06-06 18:54:39 +00001012#endif
danielk19774adee202004-05-08 08:23:19 +00001013 sqlite3VdbeChangeP2(v, jumpInst2, contAddr);
drh9cfcf5d2002-01-29 18:41:24 +00001014 }
1015}
drh0ca3e242002-01-29 23:07:02 +00001016
1017/*
1018** This routine generates code to finish the INSERT or UPDATE operation
danielk19774adee202004-05-08 08:23:19 +00001019** that was started by a prior call to sqlite3GenerateConstraintChecks.
drh0ca3e242002-01-29 23:07:02 +00001020** The stack must contain keys for all active indices followed by data
1021** and the recno for the new entry. This routine creates the new
1022** entries in all indices and in the main table.
1023**
drhb419a922002-01-30 16:17:23 +00001024** The arguments to this routine should be the same as the first six
danielk19774adee202004-05-08 08:23:19 +00001025** arguments to sqlite3GenerateConstraintChecks.
drh0ca3e242002-01-29 23:07:02 +00001026*/
danielk19774adee202004-05-08 08:23:19 +00001027void sqlite3CompleteInsertion(
drh0ca3e242002-01-29 23:07:02 +00001028 Parse *pParse, /* The parser context */
1029 Table *pTab, /* the table into which we are inserting */
1030 int base, /* Index of a read/write cursor pointing at pTab */
1031 char *aIdxUsed, /* Which indices are used. NULL means all are used */
drhb419a922002-01-30 16:17:23 +00001032 int recnoChng, /* True if the record number will change */
drh70ce3f02003-04-15 19:22:22 +00001033 int isUpdate, /* True for UPDATE, False for INSERT */
1034 int newIdx /* Index of NEW table for triggers. -1 if none */
drh0ca3e242002-01-29 23:07:02 +00001035){
1036 int i;
1037 Vdbe *v;
1038 int nIdx;
1039 Index *pIdx;
danielk1977b28af712004-06-21 06:50:26 +00001040 int pik_flags;
drh0ca3e242002-01-29 23:07:02 +00001041
danielk19774adee202004-05-08 08:23:19 +00001042 v = sqlite3GetVdbe(pParse);
drh0ca3e242002-01-29 23:07:02 +00001043 assert( v!=0 );
drh417be792002-03-03 18:59:40 +00001044 assert( pTab->pSelect==0 ); /* This table is not a VIEW */
drh0ca3e242002-01-29 23:07:02 +00001045 for(nIdx=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, nIdx++){}
1046 for(i=nIdx-1; i>=0; i--){
1047 if( aIdxUsed && aIdxUsed[i]==0 ) continue;
danielk19774adee202004-05-08 08:23:19 +00001048 sqlite3VdbeAddOp(v, OP_IdxPut, base+i+1, 0);
drh0ca3e242002-01-29 23:07:02 +00001049 }
danielk19774adee202004-05-08 08:23:19 +00001050 sqlite3VdbeAddOp(v, OP_MakeRecord, pTab->nCol, 0);
danielk1977a37cdde2004-05-16 11:15:36 +00001051 sqlite3TableAffinityStr(v, pTab);
drh70ce3f02003-04-15 19:22:22 +00001052 if( newIdx>=0 ){
danielk19774adee202004-05-08 08:23:19 +00001053 sqlite3VdbeAddOp(v, OP_Dup, 1, 0);
1054 sqlite3VdbeAddOp(v, OP_Dup, 1, 0);
1055 sqlite3VdbeAddOp(v, OP_PutIntKey, newIdx, 0);
drh70ce3f02003-04-15 19:22:22 +00001056 }
drh4794f732004-11-05 17:17:50 +00001057 if( pParse->nested ){
1058 pik_flags = 0;
1059 }else{
1060 pik_flags = (OPFLAG_NCHANGE|(isUpdate?0:OPFLAG_LASTROWID));
1061 }
danielk1977b28af712004-06-21 06:50:26 +00001062 sqlite3VdbeAddOp(v, OP_PutIntKey, base, pik_flags);
1063
drhb419a922002-01-30 16:17:23 +00001064 if( isUpdate && recnoChng ){
danielk19774adee202004-05-08 08:23:19 +00001065 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
drh0ca3e242002-01-29 23:07:02 +00001066 }
1067}
drhcd446902004-02-24 01:05:31 +00001068
1069/*
drh290c1942004-08-21 17:54:45 +00001070** Generate code that will open cursors for a table and for all
drhcd446902004-02-24 01:05:31 +00001071** indices of that table. The "base" parameter is the cursor number used
1072** for the table. Indices are opened on subsequent cursors.
drhcd446902004-02-24 01:05:31 +00001073*/
drh290c1942004-08-21 17:54:45 +00001074void sqlite3OpenTableAndIndices(
1075 Parse *pParse, /* Parsing context */
1076 Table *pTab, /* Table to be opened */
1077 int base, /* Cursor number assigned to the table */
1078 int op /* OP_OpenRead or OP_OpenWrite */
1079){
drhcd446902004-02-24 01:05:31 +00001080 int i;
1081 Index *pIdx;
danielk19774adee202004-05-08 08:23:19 +00001082 Vdbe *v = sqlite3GetVdbe(pParse);
drhcd446902004-02-24 01:05:31 +00001083 assert( v!=0 );
danielk19774adee202004-05-08 08:23:19 +00001084 sqlite3VdbeAddOp(v, OP_Integer, pTab->iDb, 0);
drh290c1942004-08-21 17:54:45 +00001085 sqlite3VdbeAddOp(v, op, base, pTab->tnum);
drhad6d9462004-09-19 02:15:24 +00001086 VdbeComment((v, "# %s", pTab->zName));
danielk1977b4964b72004-05-18 01:23:38 +00001087 sqlite3VdbeAddOp(v, OP_SetNumColumns, base, pTab->nCol);
drhcd446902004-02-24 01:05:31 +00001088 for(i=1, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){
danielk19774adee202004-05-08 08:23:19 +00001089 sqlite3VdbeAddOp(v, OP_Integer, pIdx->iDb, 0);
drh290c1942004-08-21 17:54:45 +00001090 sqlite3VdbeOp3(v, op, i+base, pIdx->tnum,
drhd3d39e92004-05-20 22:16:29 +00001091 (char*)&pIdx->keyInfo, P3_KEYINFO);
drhcd446902004-02-24 01:05:31 +00001092 }
drh290c1942004-08-21 17:54:45 +00001093 if( pParse->nTab<=base+i ){
1094 pParse->nTab = base+i;
1095 }
drhcd446902004-02-24 01:05:31 +00001096}