blob: 3d4c460b65e3bd53f64342d4cf38d32716bcc226 [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**
danielk1977b3bf5562006-01-10 17:58:23 +000015** $Id: insert.c,v 1.156 2006/01/10 17:58:23 danielk1977 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** ------------------------------
drh3eda0402005-11-24 13:15:32 +000026** 'a' TEXT
27** 'b' NONE
28** 'c' NUMERIC
29** 'd' INTEGER
30** 'e' REAL
danielk19773d1bfea2004-05-14 11:00:53 +000031*/
danielk1977a37cdde2004-05-16 11:15:36 +000032void sqlite3IndexAffinityStr(Vdbe *v, Index *pIdx){
33 if( !pIdx->zColAff ){
danielk1977e014a832004-05-17 10:48:57 +000034 /* The first time a column affinity string for a particular index is
danielk1977a37cdde2004-05-16 11:15:36 +000035 ** required, it is allocated and populated here. It is then stored as
danielk1977e014a832004-05-17 10:48:57 +000036 ** a member of the Index structure for subsequent use.
danielk1977a37cdde2004-05-16 11:15:36 +000037 **
38 ** The column affinity string will eventually be deleted by
danielk1977e014a832004-05-17 10:48:57 +000039 ** sqliteDeleteIndex() when the Index structure itself is cleaned
danielk1977a37cdde2004-05-16 11:15:36 +000040 ** up.
41 */
42 int n;
43 Table *pTab = pIdx->pTable;
44 pIdx->zColAff = (char *)sqliteMalloc(pIdx->nColumn+1);
45 if( !pIdx->zColAff ){
46 return;
47 }
48 for(n=0; n<pIdx->nColumn; n++){
49 pIdx->zColAff[n] = pTab->aCol[pIdx->aiColumn[n]].affinity;
50 }
51 pIdx->zColAff[pIdx->nColumn] = '\0';
52 }
danielk19773d1bfea2004-05-14 11:00:53 +000053
drh956bc922004-07-24 17:38:29 +000054 sqlite3VdbeChangeP3(v, -1, pIdx->zColAff, 0);
danielk1977a37cdde2004-05-16 11:15:36 +000055}
56
57/*
58** Set P3 of the most recently inserted opcode to a column affinity
59** string for table pTab. A column affinity string has one character
60** for each column indexed by the index, according to the affinity of the
61** column:
62**
63** Character Column affinity
64** ------------------------------
drh3eda0402005-11-24 13:15:32 +000065** 'a' TEXT
66** 'b' NONE
67** 'c' NUMERIC
68** 'd' INTEGER
69** 'e' REAL
danielk1977a37cdde2004-05-16 11:15:36 +000070*/
71void sqlite3TableAffinityStr(Vdbe *v, Table *pTab){
danielk19773d1bfea2004-05-14 11:00:53 +000072 /* The first time a column affinity string for a particular table
73 ** is required, it is allocated and populated here. It is then
74 ** stored as a member of the Table structure for subsequent use.
75 **
76 ** The column affinity string will eventually be deleted by
77 ** sqlite3DeleteTable() when the Table structure itself is cleaned up.
78 */
79 if( !pTab->zColAff ){
80 char *zColAff;
81 int i;
82
danielk1977a37cdde2004-05-16 11:15:36 +000083 zColAff = (char *)sqliteMalloc(pTab->nCol+1);
danielk19773d1bfea2004-05-14 11:00:53 +000084 if( !zColAff ){
danielk1977a37cdde2004-05-16 11:15:36 +000085 return;
danielk19773d1bfea2004-05-14 11:00:53 +000086 }
87
88 for(i=0; i<pTab->nCol; i++){
danielk1977a37cdde2004-05-16 11:15:36 +000089 zColAff[i] = pTab->aCol[i].affinity;
danielk19773d1bfea2004-05-14 11:00:53 +000090 }
91 zColAff[pTab->nCol] = '\0';
92
93 pTab->zColAff = zColAff;
94 }
95
drh956bc922004-07-24 17:38:29 +000096 sqlite3VdbeChangeP3(v, -1, pTab->zColAff, 0);
danielk19773d1bfea2004-05-14 11:00:53 +000097}
98
danielk19774d887782005-02-08 08:42:27 +000099/*
100** Return non-zero if SELECT statement p opens the table with rootpage
101** iTab in database iDb. This is used to see if a statement of the form
102** "INSERT INTO <iDb, iTab> SELECT ..." can run without using temporary
103** table for the results of the SELECT.
104**
105** No checking is done for sub-selects that are part of expressions.
106*/
danielk1977e501b892006-01-09 06:29:47 +0000107static int selectReadsTable(Select *p, Schema *pSchema, int iTab){
danielk19774d887782005-02-08 08:42:27 +0000108 int i;
109 struct SrcList_item *pItem;
110 if( p->pSrc==0 ) return 0;
111 for(i=0, pItem=p->pSrc->a; i<p->pSrc->nSrc; i++, pItem++){
112 if( pItem->pSelect ){
danielk1977da184232006-01-05 11:34:32 +0000113 if( selectReadsTable(pItem->pSelect, pSchema, iTab) ) return 1;
danielk19774d887782005-02-08 08:42:27 +0000114 }else{
danielk1977da184232006-01-05 11:34:32 +0000115 if( pItem->pTab->pSchema==pSchema && pItem->pTab->tnum==iTab ) return 1;
danielk19774d887782005-02-08 08:42:27 +0000116 }
117 }
118 return 0;
119}
danielk19773d1bfea2004-05-14 11:00:53 +0000120
121/*
drh1ccde152000-06-17 13:12:39 +0000122** This routine is call to handle SQL of the following forms:
drhcce7d172000-05-31 15:34:51 +0000123**
124** insert into TABLE (IDLIST) values(EXPRLIST)
drh1ccde152000-06-17 13:12:39 +0000125** insert into TABLE (IDLIST) select
drhcce7d172000-05-31 15:34:51 +0000126**
drh1ccde152000-06-17 13:12:39 +0000127** The IDLIST following the table name is always optional. If omitted,
128** then a list of all columns for the table is substituted. The IDLIST
drh967e8b72000-06-21 13:59:10 +0000129** appears in the pColumn parameter. pColumn is NULL if IDLIST is omitted.
drh1ccde152000-06-17 13:12:39 +0000130**
131** The pList parameter holds EXPRLIST in the first form of the INSERT
132** statement above, and pSelect is NULL. For the second form, pList is
133** NULL and pSelect is a pointer to the select statement used to generate
134** data for the insert.
drh142e30d2002-08-28 03:00:58 +0000135**
136** The code generated follows one of three templates. For a simple
137** select with data coming from a VALUES clause, the code executes
138** once straight down through. The template looks like this:
139**
140** open write cursor to <table> and its indices
141** puts VALUES clause expressions onto the stack
142** write the resulting record into <table>
143** cleanup
144**
145** If the statement is of the form
146**
147** INSERT INTO <table> SELECT ...
148**
149** And the SELECT clause does not read from <table> at any time, then
150** the generated code follows this template:
151**
152** goto B
153** A: setup for the SELECT
154** loop over the tables in the SELECT
155** gosub C
156** end loop
157** cleanup after the SELECT
158** goto D
159** B: open write cursor to <table> and its indices
160** goto A
161** C: insert the select result into <table>
162** return
163** D: cleanup
164**
165** The third template is used if the insert statement takes its
166** values from a SELECT but the data is being inserted into a table
167** that is also read as part of the SELECT. In the third form,
168** we have to use a intermediate table to store the results of
169** the select. The template is like this:
170**
171** goto B
172** A: setup for the SELECT
173** loop over the tables in the SELECT
174** gosub C
175** end loop
176** cleanup after the SELECT
177** goto D
178** C: insert the select result into the intermediate table
179** return
180** B: open a cursor to an intermediate table
181** goto A
182** D: open write cursor to <table> and its indices
183** loop over the intermediate table
184** transfer values form intermediate table into <table>
185** end the loop
186** cleanup
drhcce7d172000-05-31 15:34:51 +0000187*/
danielk19774adee202004-05-08 08:23:19 +0000188void sqlite3Insert(
drhcce7d172000-05-31 15:34:51 +0000189 Parse *pParse, /* Parser context */
drh113088e2003-03-20 01:16:58 +0000190 SrcList *pTabList, /* Name of table into which we are inserting */
drhcce7d172000-05-31 15:34:51 +0000191 ExprList *pList, /* List of values to be inserted */
drh5974a302000-06-07 14:42:26 +0000192 Select *pSelect, /* A SELECT statement to use as the data source */
drh9cfcf5d2002-01-29 18:41:24 +0000193 IdList *pColumn, /* Column names corresponding to IDLIST. */
194 int onError /* How to handle constraint errors */
drhcce7d172000-05-31 15:34:51 +0000195){
drh5974a302000-06-07 14:42:26 +0000196 Table *pTab; /* The table to insert into */
drh113088e2003-03-20 01:16:58 +0000197 char *zTab; /* Name of the table into which we are inserting */
drhe22a3342003-04-22 20:30:37 +0000198 const char *zDb; /* Name of the database holding this table */
drh5974a302000-06-07 14:42:26 +0000199 int i, j, idx; /* Loop counters */
200 Vdbe *v; /* Generate code into this virtual machine */
201 Index *pIdx; /* For looping over indices of the table */
drh967e8b72000-06-21 13:59:10 +0000202 int nColumn; /* Number of columns in the data */
danielk1977cfe9a692004-06-16 12:00:29 +0000203 int base = 0; /* VDBE Cursor number for pTab */
204 int iCont=0,iBreak=0; /* Beginning and end of the loop over srcTab */
drh9bb575f2004-09-06 17:24:11 +0000205 sqlite3 *db; /* The main database structure */
drh4a324312001-12-21 14:30:42 +0000206 int keyColumn = -1; /* Column that is the INTEGER PRIMARY KEY */
drh0ca3e242002-01-29 23:07:02 +0000207 int endOfLoop; /* Label for the end of the insertion loop */
danielk19774d887782005-02-08 08:42:27 +0000208 int useTempTable = 0; /* Store SELECT results in intermediate table */
danielk1977cfe9a692004-06-16 12:00:29 +0000209 int srcTab = 0; /* Data comes from this temporary cursor if >=0 */
210 int iSelectLoop = 0; /* Address of code that implements the SELECT */
211 int iCleanup = 0; /* Address of the cleanup code */
212 int iInsertBlock = 0; /* Address of the subroutine used to insert data */
213 int iCntMem = 0; /* Memory cell used for the row counter */
drh798da522004-11-04 04:42:28 +0000214 int newIdx = -1; /* Cursor for the NEW table */
drh2958a4e2004-11-12 03:56:15 +0000215 Db *pDb; /* The database containing table being inserted into */
216 int counterMem = 0; /* Memory cell holding AUTOINCREMENT counter */
danielk1977da184232006-01-05 11:34:32 +0000217 int iDb;
drhcce7d172000-05-31 15:34:51 +0000218
drh798da522004-11-04 04:42:28 +0000219#ifndef SQLITE_OMIT_TRIGGER
220 int isView; /* True if attempting to insert into a view */
drhdca76842004-12-07 14:06:13 +0000221 int triggers_exist = 0; /* True if there are FOR EACH ROW triggers */
drh798da522004-11-04 04:42:28 +0000222#endif
danielk1977c3f9bad2002-05-15 08:30:12 +0000223
drhf3388142004-11-13 03:48:06 +0000224#ifndef SQLITE_OMIT_AUTOINCREMENT
225 int counterRowid; /* Memory cell holding rowid of autoinc counter */
226#endif
227
danielk1977e501b892006-01-09 06:29:47 +0000228 if( pParse->nErr || sqlite3ThreadData()->mallocFailed ) goto insert_cleanup;
drhecdc7532001-09-23 02:35:53 +0000229 db = pParse->db;
drhdaffd0e2001-04-11 14:28:42 +0000230
drh1ccde152000-06-17 13:12:39 +0000231 /* Locate the table into which we will be inserting new information.
232 */
drh113088e2003-03-20 01:16:58 +0000233 assert( pTabList->nSrc==1 );
234 zTab = pTabList->a[0].zName;
drhdaffd0e2001-04-11 14:28:42 +0000235 if( zTab==0 ) goto insert_cleanup;
danielk19774adee202004-05-08 08:23:19 +0000236 pTab = sqlite3SrcListLookup(pParse, pTabList);
danielk1977c3f9bad2002-05-15 08:30:12 +0000237 if( pTab==0 ){
danielk1977c3f9bad2002-05-15 08:30:12 +0000238 goto insert_cleanup;
239 }
danielk1977da184232006-01-05 11:34:32 +0000240 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
241 assert( iDb<db->nDb );
242 pDb = &db->aDb[iDb];
drh2958a4e2004-11-12 03:56:15 +0000243 zDb = pDb->zName;
danielk19774adee202004-05-08 08:23:19 +0000244 if( sqlite3AuthCheck(pParse, SQLITE_INSERT, pTab->zName, 0, zDb) ){
drh1962bda2003-01-12 19:33:52 +0000245 goto insert_cleanup;
246 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000247
drhb7f91642004-10-31 02:22:47 +0000248 /* Figure out if we have any triggers and if the table being
249 ** inserted into is a view
250 */
251#ifndef SQLITE_OMIT_TRIGGER
drhdca76842004-12-07 14:06:13 +0000252 triggers_exist = sqlite3TriggersExist(pParse, pTab, TK_INSERT, 0);
drhb7f91642004-10-31 02:22:47 +0000253 isView = pTab->pSelect!=0;
254#else
drhdca76842004-12-07 14:06:13 +0000255# define triggers_exist 0
drhb7f91642004-10-31 02:22:47 +0000256# define isView 0
257#endif
258#ifdef SQLITE_OMIT_VIEW
259# undef isView
260# define isView 0
261#endif
262
danielk1977c3f9bad2002-05-15 08:30:12 +0000263 /* Ensure that:
264 * (a) the table is not read-only,
265 * (b) that if it is a view then ON INSERT triggers exist
266 */
drhdca76842004-12-07 14:06:13 +0000267 if( sqlite3IsReadOnly(pParse, pTab, triggers_exist) ){
danielk1977c3f9bad2002-05-15 08:30:12 +0000268 goto insert_cleanup;
269 }
drha76b5df2002-02-23 02:32:10 +0000270 if( pTab==0 ) goto insert_cleanup;
drh1ccde152000-06-17 13:12:39 +0000271
drhf573c992002-07-31 00:32:50 +0000272 /* If pTab is really a view, make sure it has been initialized.
273 */
danielk19774adee202004-05-08 08:23:19 +0000274 if( isView && sqlite3ViewGetColumnNames(pParse, pTab) ){
drh5cf590c2003-04-24 01:45:04 +0000275 goto insert_cleanup;
drhf573c992002-07-31 00:32:50 +0000276 }
277
drh1ccde152000-06-17 13:12:39 +0000278 /* Allocate a VDBE
279 */
danielk19774adee202004-05-08 08:23:19 +0000280 v = sqlite3GetVdbe(pParse);
drh5974a302000-06-07 14:42:26 +0000281 if( v==0 ) goto insert_cleanup;
drh4794f732004-11-05 17:17:50 +0000282 if( pParse->nested==0 ) sqlite3VdbeCountChanges(v);
danielk1977da184232006-01-05 11:34:32 +0000283 sqlite3BeginWriteOperation(pParse, pSelect || triggers_exist, iDb);
drh1ccde152000-06-17 13:12:39 +0000284
danielk1977c3f9bad2002-05-15 08:30:12 +0000285 /* if there are row triggers, allocate a temp table for new.* references. */
drhdca76842004-12-07 14:06:13 +0000286 if( triggers_exist ){
danielk1977c3f9bad2002-05-15 08:30:12 +0000287 newIdx = pParse->nTab++;
danielk1977f29ce552002-05-19 23:43:12 +0000288 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000289
drh2958a4e2004-11-12 03:56:15 +0000290#ifndef SQLITE_OMIT_AUTOINCREMENT
291 /* If this is an AUTOINCREMENT table, look up the sequence number in the
drhf3388142004-11-13 03:48:06 +0000292 ** sqlite_sequence table and store it in memory cell counterMem. Also
293 ** remember the rowid of the sqlite_sequence table entry in memory cell
294 ** counterRowid.
drh2958a4e2004-11-12 03:56:15 +0000295 */
296 if( pTab->autoInc ){
drhf3388142004-11-13 03:48:06 +0000297 int iCur = pParse->nTab;
298 int base = sqlite3VdbeCurrentAddr(v);
299 counterRowid = pParse->nMem++;
300 counterMem = pParse->nMem++;
danielk1977c00da102006-01-07 13:21:04 +0000301 sqlite3OpenTable(pParse, iCur, iDb, pDb->pSchema->pSeqTab, OP_OpenRead);
drhf3388142004-11-13 03:48:06 +0000302 sqlite3VdbeAddOp(v, OP_Rewind, iCur, base+13);
303 sqlite3VdbeAddOp(v, OP_Column, iCur, 0);
304 sqlite3VdbeOp3(v, OP_String8, 0, 0, pTab->zName, 0);
drh8a512562005-11-14 22:29:05 +0000305 sqlite3VdbeAddOp(v, OP_Ne, 0x100, base+12);
drhf0863fe2005-06-12 21:35:51 +0000306 sqlite3VdbeAddOp(v, OP_Rowid, iCur, 0);
drhf3388142004-11-13 03:48:06 +0000307 sqlite3VdbeAddOp(v, OP_MemStore, counterRowid, 1);
308 sqlite3VdbeAddOp(v, OP_Column, iCur, 1);
309 sqlite3VdbeAddOp(v, OP_MemStore, counterMem, 1);
310 sqlite3VdbeAddOp(v, OP_Goto, 0, base+13);
311 sqlite3VdbeAddOp(v, OP_Next, iCur, base+4);
312 sqlite3VdbeAddOp(v, OP_Close, iCur, 0);
drh2958a4e2004-11-12 03:56:15 +0000313 }
314#endif /* SQLITE_OMIT_AUTOINCREMENT */
315
drh1ccde152000-06-17 13:12:39 +0000316 /* Figure out how many columns of data are supplied. If the data
drh142e30d2002-08-28 03:00:58 +0000317 ** is coming from a SELECT statement, then this step also generates
318 ** all the code to implement the SELECT statement and invoke a subroutine
319 ** to process each row of the result. (Template 2.) If the SELECT
320 ** statement uses the the table that is being inserted into, then the
321 ** subroutine is also coded here. That subroutine stores the SELECT
322 ** results in a temporary table. (Template 3.)
drh1ccde152000-06-17 13:12:39 +0000323 */
drh5974a302000-06-07 14:42:26 +0000324 if( pSelect ){
drh142e30d2002-08-28 03:00:58 +0000325 /* Data is coming from a SELECT. Generate code to implement that SELECT
326 */
327 int rc, iInitCode;
danielk19774adee202004-05-08 08:23:19 +0000328 iInitCode = sqlite3VdbeAddOp(v, OP_Goto, 0, 0);
329 iSelectLoop = sqlite3VdbeCurrentAddr(v);
330 iInsertBlock = sqlite3VdbeMakeLabel(v);
danielk1977b3bce662005-01-29 08:32:43 +0000331
332 /* Resolve the expressions in the SELECT statement and execute it. */
333 rc = sqlite3Select(pParse, pSelect, SRT_Subroutine, iInsertBlock,0,0,0,0);
danielk1977e501b892006-01-09 06:29:47 +0000334 if( rc || pParse->nErr || sqlite3ThreadData()->mallocFailed ) goto insert_cleanup;
danielk1977b3bce662005-01-29 08:32:43 +0000335
danielk19774adee202004-05-08 08:23:19 +0000336 iCleanup = sqlite3VdbeMakeLabel(v);
337 sqlite3VdbeAddOp(v, OP_Goto, 0, iCleanup);
drh5974a302000-06-07 14:42:26 +0000338 assert( pSelect->pEList );
drh967e8b72000-06-21 13:59:10 +0000339 nColumn = pSelect->pEList->nExpr;
drh142e30d2002-08-28 03:00:58 +0000340
341 /* Set useTempTable to TRUE if the result of the SELECT statement
342 ** should be written into a temporary table. Set to FALSE if each
343 ** row of the SELECT can be written directly into the result table.
drh048c5302003-04-03 01:50:44 +0000344 **
345 ** A temp table must be used if the table being updated is also one
346 ** of the tables being read by the SELECT statement. Also use a
347 ** temp table in the case of row triggers.
drh142e30d2002-08-28 03:00:58 +0000348 */
danielk1977da184232006-01-05 11:34:32 +0000349 if( triggers_exist || selectReadsTable(pSelect,pTab->pSchema,pTab->tnum) ){
drh048c5302003-04-03 01:50:44 +0000350 useTempTable = 1;
drh048c5302003-04-03 01:50:44 +0000351 }
drh142e30d2002-08-28 03:00:58 +0000352
353 if( useTempTable ){
354 /* Generate the subroutine that SELECT calls to process each row of
355 ** the result. Store the result in a temporary table
356 */
357 srcTab = pParse->nTab++;
danielk19774adee202004-05-08 08:23:19 +0000358 sqlite3VdbeResolveLabel(v, iInsertBlock);
359 sqlite3VdbeAddOp(v, OP_MakeRecord, nColumn, 0);
drhf0863fe2005-06-12 21:35:51 +0000360 sqlite3VdbeAddOp(v, OP_NewRowid, srcTab, 0);
danielk19774adee202004-05-08 08:23:19 +0000361 sqlite3VdbeAddOp(v, OP_Pull, 1, 0);
drhf0863fe2005-06-12 21:35:51 +0000362 sqlite3VdbeAddOp(v, OP_Insert, srcTab, 0);
danielk19774adee202004-05-08 08:23:19 +0000363 sqlite3VdbeAddOp(v, OP_Return, 0, 0);
drh142e30d2002-08-28 03:00:58 +0000364
365 /* The following code runs first because the GOTO at the very top
366 ** of the program jumps to it. Create the temporary table, then jump
367 ** back up and execute the SELECT code above.
368 */
drhd654be82005-09-20 17:42:23 +0000369 sqlite3VdbeJumpHere(v, iInitCode);
drh9170dd72005-07-08 17:13:46 +0000370 sqlite3VdbeAddOp(v, OP_OpenVirtual, srcTab, 0);
drhb6f54522004-05-20 02:42:16 +0000371 sqlite3VdbeAddOp(v, OP_SetNumColumns, srcTab, nColumn);
danielk19774adee202004-05-08 08:23:19 +0000372 sqlite3VdbeAddOp(v, OP_Goto, 0, iSelectLoop);
373 sqlite3VdbeResolveLabel(v, iCleanup);
drh142e30d2002-08-28 03:00:58 +0000374 }else{
drhd654be82005-09-20 17:42:23 +0000375 sqlite3VdbeJumpHere(v, iInitCode);
drh142e30d2002-08-28 03:00:58 +0000376 }
drh5974a302000-06-07 14:42:26 +0000377 }else{
drh142e30d2002-08-28 03:00:58 +0000378 /* This is the case if the data for the INSERT is coming from a VALUES
379 ** clause
380 */
danielk1977b3bce662005-01-29 08:32:43 +0000381 NameContext sNC;
382 memset(&sNC, 0, sizeof(sNC));
383 sNC.pParse = pParse;
drhdaffd0e2001-04-11 14:28:42 +0000384 assert( pList!=0 );
drh5974a302000-06-07 14:42:26 +0000385 srcTab = -1;
drh142e30d2002-08-28 03:00:58 +0000386 useTempTable = 0;
drh5974a302000-06-07 14:42:26 +0000387 assert( pList );
drh967e8b72000-06-21 13:59:10 +0000388 nColumn = pList->nExpr;
drhe64e7b22002-02-18 13:56:36 +0000389 for(i=0; i<nColumn; i++){
danielk1977b3bce662005-01-29 08:32:43 +0000390 if( sqlite3ExprResolveNames(&sNC, pList->a[i].pExpr) ){
drhb04a5d82002-04-12 03:55:15 +0000391 goto insert_cleanup;
392 }
drhe64e7b22002-02-18 13:56:36 +0000393 }
drh5974a302000-06-07 14:42:26 +0000394 }
drh1ccde152000-06-17 13:12:39 +0000395
396 /* Make sure the number of columns in the source data matches the number
397 ** of columns to be inserted into the table.
398 */
drh967e8b72000-06-21 13:59:10 +0000399 if( pColumn==0 && nColumn!=pTab->nCol ){
danielk19774adee202004-05-08 08:23:19 +0000400 sqlite3ErrorMsg(pParse,
drhda93d232003-03-31 02:12:46 +0000401 "table %S has %d columns but %d values were supplied",
402 pTabList, 0, pTab->nCol, nColumn);
drhcce7d172000-05-31 15:34:51 +0000403 goto insert_cleanup;
404 }
drh967e8b72000-06-21 13:59:10 +0000405 if( pColumn!=0 && nColumn!=pColumn->nId ){
danielk19774adee202004-05-08 08:23:19 +0000406 sqlite3ErrorMsg(pParse, "%d values for %d columns", nColumn, pColumn->nId);
drhcce7d172000-05-31 15:34:51 +0000407 goto insert_cleanup;
408 }
drh1ccde152000-06-17 13:12:39 +0000409
410 /* If the INSERT statement included an IDLIST term, then make sure
411 ** all elements of the IDLIST really are columns of the table and
412 ** remember the column indices.
drhc8392582001-12-31 02:48:51 +0000413 **
414 ** If the table has an INTEGER PRIMARY KEY column and that column
415 ** is named in the IDLIST, then record in the keyColumn variable
416 ** the index into IDLIST of the primary key column. keyColumn is
417 ** the index of the primary key as it appears in IDLIST, not as
418 ** is appears in the original table. (The index of the primary
419 ** key in the original table is pTab->iPKey.)
drh1ccde152000-06-17 13:12:39 +0000420 */
drh967e8b72000-06-21 13:59:10 +0000421 if( pColumn ){
422 for(i=0; i<pColumn->nId; i++){
423 pColumn->a[i].idx = -1;
drhcce7d172000-05-31 15:34:51 +0000424 }
drh967e8b72000-06-21 13:59:10 +0000425 for(i=0; i<pColumn->nId; i++){
drhcce7d172000-05-31 15:34:51 +0000426 for(j=0; j<pTab->nCol; j++){
danielk19774adee202004-05-08 08:23:19 +0000427 if( sqlite3StrICmp(pColumn->a[i].zName, pTab->aCol[j].zName)==0 ){
drh967e8b72000-06-21 13:59:10 +0000428 pColumn->a[i].idx = j;
drh4a324312001-12-21 14:30:42 +0000429 if( j==pTab->iPKey ){
drh9aa028d2001-12-22 21:48:29 +0000430 keyColumn = i;
drh4a324312001-12-21 14:30:42 +0000431 }
drhcce7d172000-05-31 15:34:51 +0000432 break;
433 }
434 }
435 if( j>=pTab->nCol ){
danielk19774adee202004-05-08 08:23:19 +0000436 if( sqlite3IsRowid(pColumn->a[i].zName) ){
drha0217ba2003-06-01 01:10:33 +0000437 keyColumn = i;
438 }else{
danielk19774adee202004-05-08 08:23:19 +0000439 sqlite3ErrorMsg(pParse, "table %S has no column named %s",
drha0217ba2003-06-01 01:10:33 +0000440 pTabList, 0, pColumn->a[i].zName);
441 pParse->nErr++;
442 goto insert_cleanup;
443 }
drhcce7d172000-05-31 15:34:51 +0000444 }
445 }
446 }
drh1ccde152000-06-17 13:12:39 +0000447
drhaacc5432002-01-06 17:07:40 +0000448 /* If there is no IDLIST term but the table has an integer primary
drhc8392582001-12-31 02:48:51 +0000449 ** key, the set the keyColumn variable to the primary key column index
450 ** in the original table definition.
drh4a324312001-12-21 14:30:42 +0000451 */
452 if( pColumn==0 ){
453 keyColumn = pTab->iPKey;
454 }
455
drh142e30d2002-08-28 03:00:58 +0000456 /* Open the temp table for FOR EACH ROW triggers
457 */
drhdca76842004-12-07 14:06:13 +0000458 if( triggers_exist ){
danielk19774adee202004-05-08 08:23:19 +0000459 sqlite3VdbeAddOp(v, OP_OpenPseudo, newIdx, 0);
danielk197784ac9d02004-05-18 09:58:06 +0000460 sqlite3VdbeAddOp(v, OP_SetNumColumns, newIdx, pTab->nCol);
danielk1977f29ce552002-05-19 23:43:12 +0000461 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000462
drhfeeb1392002-04-09 03:28:01 +0000463 /* Initialize the count of rows to be inserted
464 */
drh142e30d2002-08-28 03:00:58 +0000465 if( db->flags & SQLITE_CountRows ){
466 iCntMem = pParse->nMem++;
drhd654be82005-09-20 17:42:23 +0000467 sqlite3VdbeAddOp(v, OP_MemInt, 0, iCntMem);
drhfeeb1392002-04-09 03:28:01 +0000468 }
469
danielk1977c3f9bad2002-05-15 08:30:12 +0000470 /* Open tables and indices if there are no row triggers */
drhdca76842004-12-07 14:06:13 +0000471 if( !triggers_exist ){
danielk1977c3f9bad2002-05-15 08:30:12 +0000472 base = pParse->nTab;
drh290c1942004-08-21 17:54:45 +0000473 sqlite3OpenTableAndIndices(pParse, pTab, base, OP_OpenWrite);
danielk1977c3f9bad2002-05-15 08:30:12 +0000474 }
475
drh142e30d2002-08-28 03:00:58 +0000476 /* If the data source is a temporary table, then we have to create
drh1ccde152000-06-17 13:12:39 +0000477 ** a loop because there might be multiple rows of data. If the data
drh142e30d2002-08-28 03:00:58 +0000478 ** source is a subroutine call from the SELECT statement, then we need
479 ** to launch the SELECT statement processing.
drh1ccde152000-06-17 13:12:39 +0000480 */
drh142e30d2002-08-28 03:00:58 +0000481 if( useTempTable ){
danielk19774adee202004-05-08 08:23:19 +0000482 iBreak = sqlite3VdbeMakeLabel(v);
483 sqlite3VdbeAddOp(v, OP_Rewind, srcTab, iBreak);
484 iCont = sqlite3VdbeCurrentAddr(v);
drh142e30d2002-08-28 03:00:58 +0000485 }else if( pSelect ){
danielk19774adee202004-05-08 08:23:19 +0000486 sqlite3VdbeAddOp(v, OP_Goto, 0, iSelectLoop);
487 sqlite3VdbeResolveLabel(v, iInsertBlock);
drh5974a302000-06-07 14:42:26 +0000488 }
drh1ccde152000-06-17 13:12:39 +0000489
drh5cf590c2003-04-24 01:45:04 +0000490 /* Run the BEFORE and INSTEAD OF triggers, if there are any
drh70ce3f02003-04-15 19:22:22 +0000491 */
danielk19774adee202004-05-08 08:23:19 +0000492 endOfLoop = sqlite3VdbeMakeLabel(v);
drhdca76842004-12-07 14:06:13 +0000493 if( triggers_exist & TRIGGER_BEFORE ){
danielk1977c3f9bad2002-05-15 08:30:12 +0000494
drh70ce3f02003-04-15 19:22:22 +0000495 /* build the NEW.* reference row. Note that if there is an INTEGER
496 ** PRIMARY KEY into which a NULL is being inserted, that NULL will be
497 ** translated into a unique ID for the row. But on a BEFORE trigger,
498 ** we do not know what the unique ID will be (because the insert has
499 ** not happened yet) so we substitute a rowid of -1
500 */
501 if( keyColumn<0 ){
danielk19774adee202004-05-08 08:23:19 +0000502 sqlite3VdbeAddOp(v, OP_Integer, -1, 0);
drh70ce3f02003-04-15 19:22:22 +0000503 }else if( useTempTable ){
danielk19774adee202004-05-08 08:23:19 +0000504 sqlite3VdbeAddOp(v, OP_Column, srcTab, keyColumn);
drh70ce3f02003-04-15 19:22:22 +0000505 }else{
drhd6fe9612005-01-14 01:22:00 +0000506 assert( pSelect==0 ); /* Otherwise useTempTable is true */
danielk19774adee202004-05-08 08:23:19 +0000507 sqlite3ExprCode(pParse, pList->a[keyColumn].pExpr);
508 sqlite3VdbeAddOp(v, OP_NotNull, -1, sqlite3VdbeCurrentAddr(v)+3);
509 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
510 sqlite3VdbeAddOp(v, OP_Integer, -1, 0);
511 sqlite3VdbeAddOp(v, OP_MustBeInt, 0, 0);
drh70ce3f02003-04-15 19:22:22 +0000512 }
513
514 /* Create the new column data
515 */
danielk1977c3f9bad2002-05-15 08:30:12 +0000516 for(i=0; i<pTab->nCol; i++){
517 if( pColumn==0 ){
drh9adf9ac2002-05-15 11:44:13 +0000518 j = i;
danielk1977c3f9bad2002-05-15 08:30:12 +0000519 }else{
drh9adf9ac2002-05-15 11:44:13 +0000520 for(j=0; j<pColumn->nId; j++){
521 if( pColumn->a[j].idx==i ) break;
522 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000523 }
524 if( pColumn && j>=pColumn->nId ){
danielk19777977a172004-11-09 12:44:37 +0000525 sqlite3ExprCode(pParse, pTab->aCol[i].pDflt);
drh142e30d2002-08-28 03:00:58 +0000526 }else if( useTempTable ){
danielk19774adee202004-05-08 08:23:19 +0000527 sqlite3VdbeAddOp(v, OP_Column, srcTab, j);
danielk1977c3f9bad2002-05-15 08:30:12 +0000528 }else{
drhd6fe9612005-01-14 01:22:00 +0000529 assert( pSelect==0 ); /* Otherwise useTempTable is true */
drh25303782004-12-07 15:41:48 +0000530 sqlite3ExprCodeAndCache(pParse, pList->a[j].pExpr);
danielk1977c3f9bad2002-05-15 08:30:12 +0000531 }
532 }
danielk19774adee202004-05-08 08:23:19 +0000533 sqlite3VdbeAddOp(v, OP_MakeRecord, pTab->nCol, 0);
danielk1977a37cdde2004-05-16 11:15:36 +0000534
535 /* If this is an INSERT on a view with an INSTEAD OF INSERT trigger,
536 ** do not attempt any conversions before assembling the record.
537 ** If this is a real table, attempt conversions as required by the
538 ** table column affinities.
539 */
540 if( !isView ){
541 sqlite3TableAffinityStr(v, pTab);
542 }
drhf0863fe2005-06-12 21:35:51 +0000543 sqlite3VdbeAddOp(v, OP_Insert, newIdx, 0);
danielk1977c3f9bad2002-05-15 08:30:12 +0000544
drh5cf590c2003-04-24 01:45:04 +0000545 /* Fire BEFORE or INSTEAD OF triggers */
drhdca76842004-12-07 14:06:13 +0000546 if( sqlite3CodeRowTrigger(pParse, TK_INSERT, 0, TRIGGER_BEFORE, pTab,
drhb2fe7d82003-04-20 17:29:23 +0000547 newIdx, -1, onError, endOfLoop) ){
danielk1977f29ce552002-05-19 23:43:12 +0000548 goto insert_cleanup;
549 }
drh70ce3f02003-04-15 19:22:22 +0000550 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000551
drh70ce3f02003-04-15 19:22:22 +0000552 /* If any triggers exists, the opening of tables and indices is deferred
553 ** until now.
554 */
drhdca76842004-12-07 14:06:13 +0000555 if( triggers_exist && !isView ){
drh5cf590c2003-04-24 01:45:04 +0000556 base = pParse->nTab;
drh290c1942004-08-21 17:54:45 +0000557 sqlite3OpenTableAndIndices(pParse, pTab, base, OP_OpenWrite);
danielk1977c3f9bad2002-05-15 08:30:12 +0000558 }
559
drh4a324312001-12-21 14:30:42 +0000560 /* Push the record number for the new entry onto the stack. The
drhf0863fe2005-06-12 21:35:51 +0000561 ** record number is a randomly generate integer created by NewRowid
drh4a324312001-12-21 14:30:42 +0000562 ** except when the table has an INTEGER PRIMARY KEY column, in which
drhb419a922002-01-30 16:17:23 +0000563 ** case the record number is the same as that column.
drh1ccde152000-06-17 13:12:39 +0000564 */
drh5cf590c2003-04-24 01:45:04 +0000565 if( !isView ){
danielk1977c3f9bad2002-05-15 08:30:12 +0000566 if( keyColumn>=0 ){
drh142e30d2002-08-28 03:00:58 +0000567 if( useTempTable ){
danielk19774adee202004-05-08 08:23:19 +0000568 sqlite3VdbeAddOp(v, OP_Column, srcTab, keyColumn);
drh142e30d2002-08-28 03:00:58 +0000569 }else if( pSelect ){
danielk19774adee202004-05-08 08:23:19 +0000570 sqlite3VdbeAddOp(v, OP_Dup, nColumn - keyColumn - 1, 1);
danielk1977c3f9bad2002-05-15 08:30:12 +0000571 }else{
danielk19774adee202004-05-08 08:23:19 +0000572 sqlite3ExprCode(pParse, pList->a[keyColumn].pExpr);
danielk1977c3f9bad2002-05-15 08:30:12 +0000573 }
drhf0863fe2005-06-12 21:35:51 +0000574 /* If the PRIMARY KEY expression is NULL, then use OP_NewRowid
drh27a32782002-06-19 20:32:43 +0000575 ** to generate a unique primary key value.
576 */
danielk19774adee202004-05-08 08:23:19 +0000577 sqlite3VdbeAddOp(v, OP_NotNull, -1, sqlite3VdbeCurrentAddr(v)+3);
578 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
drhf0863fe2005-06-12 21:35:51 +0000579 sqlite3VdbeAddOp(v, OP_NewRowid, base, counterMem);
danielk19774adee202004-05-08 08:23:19 +0000580 sqlite3VdbeAddOp(v, OP_MustBeInt, 0, 0);
danielk1977c3f9bad2002-05-15 08:30:12 +0000581 }else{
drhf0863fe2005-06-12 21:35:51 +0000582 sqlite3VdbeAddOp(v, OP_NewRowid, base, counterMem);
drh4a324312001-12-21 14:30:42 +0000583 }
drh2958a4e2004-11-12 03:56:15 +0000584#ifndef SQLITE_OMIT_AUTOINCREMENT
585 if( pTab->autoInc ){
586 sqlite3VdbeAddOp(v, OP_MemMax, counterMem, 0);
587 }
588#endif /* SQLITE_OMIT_AUTOINCREMENT */
drh4a324312001-12-21 14:30:42 +0000589
danielk1977c3f9bad2002-05-15 08:30:12 +0000590 /* Push onto the stack, data for all columns of the new entry, beginning
danielk1977f29ce552002-05-19 23:43:12 +0000591 ** with the first column.
592 */
danielk1977c3f9bad2002-05-15 08:30:12 +0000593 for(i=0; i<pTab->nCol; i++){
594 if( i==pTab->iPKey ){
drh9adf9ac2002-05-15 11:44:13 +0000595 /* The value of the INTEGER PRIMARY KEY column is always a NULL.
danielk1977f29ce552002-05-19 23:43:12 +0000596 ** Whenever this column is read, the record number will be substituted
597 ** in its place. So will fill this column with a NULL to avoid
598 ** taking up data space with information that will never be used. */
drhf0863fe2005-06-12 21:35:51 +0000599 sqlite3VdbeAddOp(v, OP_Null, 0, 0);
drh9adf9ac2002-05-15 11:44:13 +0000600 continue;
danielk1977c3f9bad2002-05-15 08:30:12 +0000601 }
602 if( pColumn==0 ){
drh9adf9ac2002-05-15 11:44:13 +0000603 j = i;
danielk1977c3f9bad2002-05-15 08:30:12 +0000604 }else{
drh9adf9ac2002-05-15 11:44:13 +0000605 for(j=0; j<pColumn->nId; j++){
606 if( pColumn->a[j].idx==i ) break;
607 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000608 }
609 if( pColumn && j>=pColumn->nId ){
danielk19777977a172004-11-09 12:44:37 +0000610 sqlite3ExprCode(pParse, pTab->aCol[i].pDflt);
drh142e30d2002-08-28 03:00:58 +0000611 }else if( useTempTable ){
danielk19774adee202004-05-08 08:23:19 +0000612 sqlite3VdbeAddOp(v, OP_Column, srcTab, j);
drh142e30d2002-08-28 03:00:58 +0000613 }else if( pSelect ){
danielk19774adee202004-05-08 08:23:19 +0000614 sqlite3VdbeAddOp(v, OP_Dup, i+nColumn-j, 1);
danielk1977c3f9bad2002-05-15 08:30:12 +0000615 }else{
danielk19774adee202004-05-08 08:23:19 +0000616 sqlite3ExprCode(pParse, pList->a[j].pExpr);
drh5974a302000-06-07 14:42:26 +0000617 }
drhbed86902000-06-02 13:27:59 +0000618 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000619
620 /* Generate code to check constraints and generate index keys and
danielk1977f29ce552002-05-19 23:43:12 +0000621 ** do the insertion.
622 */
danielk19774adee202004-05-08 08:23:19 +0000623 sqlite3GenerateConstraintChecks(pParse, pTab, base, 0, keyColumn>=0,
drha0217ba2003-06-01 01:10:33 +0000624 0, onError, endOfLoop);
danielk19774adee202004-05-08 08:23:19 +0000625 sqlite3CompleteInsertion(pParse, pTab, base, 0,0,0,
drhdca76842004-12-07 14:06:13 +0000626 (triggers_exist & TRIGGER_AFTER)!=0 ? newIdx : -1);
drh5cf590c2003-04-24 01:45:04 +0000627 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000628
drh5cf590c2003-04-24 01:45:04 +0000629 /* Update the count of rows that are inserted
630 */
631 if( (db->flags & SQLITE_CountRows)!=0 ){
drh15007a92006-01-08 18:10:17 +0000632 sqlite3VdbeAddOp(v, OP_MemIncr, 1, iCntMem);
drh5974a302000-06-07 14:42:26 +0000633 }
drh1ccde152000-06-17 13:12:39 +0000634
drhdca76842004-12-07 14:06:13 +0000635 if( triggers_exist ){
danielk1977c3f9bad2002-05-15 08:30:12 +0000636 /* Close all tables opened */
drh5cf590c2003-04-24 01:45:04 +0000637 if( !isView ){
danielk19774adee202004-05-08 08:23:19 +0000638 sqlite3VdbeAddOp(v, OP_Close, base, 0);
danielk1977c3f9bad2002-05-15 08:30:12 +0000639 for(idx=1, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, idx++){
danielk19774adee202004-05-08 08:23:19 +0000640 sqlite3VdbeAddOp(v, OP_Close, idx+base, 0);
danielk1977c3f9bad2002-05-15 08:30:12 +0000641 }
642 }
drh1bee3d72001-10-15 00:44:35 +0000643
danielk1977c3f9bad2002-05-15 08:30:12 +0000644 /* Code AFTER triggers */
drhdca76842004-12-07 14:06:13 +0000645 if( sqlite3CodeRowTrigger(pParse, TK_INSERT, 0, TRIGGER_AFTER, pTab,
646 newIdx, -1, onError, endOfLoop) ){
danielk1977f29ce552002-05-19 23:43:12 +0000647 goto insert_cleanup;
648 }
drh1bee3d72001-10-15 00:44:35 +0000649 }
650
drh1ccde152000-06-17 13:12:39 +0000651 /* The bottom of the loop, if the data source is a SELECT statement
danielk1977f29ce552002-05-19 23:43:12 +0000652 */
danielk19774adee202004-05-08 08:23:19 +0000653 sqlite3VdbeResolveLabel(v, endOfLoop);
drh142e30d2002-08-28 03:00:58 +0000654 if( useTempTable ){
danielk19774adee202004-05-08 08:23:19 +0000655 sqlite3VdbeAddOp(v, OP_Next, srcTab, iCont);
656 sqlite3VdbeResolveLabel(v, iBreak);
657 sqlite3VdbeAddOp(v, OP_Close, srcTab, 0);
drh142e30d2002-08-28 03:00:58 +0000658 }else if( pSelect ){
danielk19774adee202004-05-08 08:23:19 +0000659 sqlite3VdbeAddOp(v, OP_Pop, nColumn, 0);
660 sqlite3VdbeAddOp(v, OP_Return, 0, 0);
661 sqlite3VdbeResolveLabel(v, iCleanup);
drh6b563442001-11-07 16:48:26 +0000662 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000663
drhdca76842004-12-07 14:06:13 +0000664 if( !triggers_exist ){
danielk1977c3f9bad2002-05-15 08:30:12 +0000665 /* Close all tables opened */
danielk19774adee202004-05-08 08:23:19 +0000666 sqlite3VdbeAddOp(v, OP_Close, base, 0);
danielk1977c3f9bad2002-05-15 08:30:12 +0000667 for(idx=1, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, idx++){
danielk19774adee202004-05-08 08:23:19 +0000668 sqlite3VdbeAddOp(v, OP_Close, idx+base, 0);
danielk1977c3f9bad2002-05-15 08:30:12 +0000669 }
drhcce7d172000-05-31 15:34:51 +0000670 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000671
drh2958a4e2004-11-12 03:56:15 +0000672#ifndef SQLITE_OMIT_AUTOINCREMENT
drhf3388142004-11-13 03:48:06 +0000673 /* Update the sqlite_sequence table by storing the content of the
674 ** counter value in memory counterMem back into the sqlite_sequence
675 ** table.
drh2958a4e2004-11-12 03:56:15 +0000676 */
677 if( pTab->autoInc ){
drhf3388142004-11-13 03:48:06 +0000678 int iCur = pParse->nTab;
679 int base = sqlite3VdbeCurrentAddr(v);
danielk1977c00da102006-01-07 13:21:04 +0000680 sqlite3OpenTable(pParse, iCur, iDb, pDb->pSchema->pSeqTab, OP_OpenWrite);
drhf3388142004-11-13 03:48:06 +0000681 sqlite3VdbeAddOp(v, OP_MemLoad, counterRowid, 0);
682 sqlite3VdbeAddOp(v, OP_NotNull, -1, base+7);
683 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
drhf0863fe2005-06-12 21:35:51 +0000684 sqlite3VdbeAddOp(v, OP_NewRowid, iCur, 0);
drhf3388142004-11-13 03:48:06 +0000685 sqlite3VdbeOp3(v, OP_String8, 0, 0, pTab->zName, 0);
686 sqlite3VdbeAddOp(v, OP_MemLoad, counterMem, 0);
687 sqlite3VdbeAddOp(v, OP_MakeRecord, 2, 0);
drhf0863fe2005-06-12 21:35:51 +0000688 sqlite3VdbeAddOp(v, OP_Insert, iCur, 0);
drhf3388142004-11-13 03:48:06 +0000689 sqlite3VdbeAddOp(v, OP_Close, iCur, 0);
drh2958a4e2004-11-12 03:56:15 +0000690 }
691#endif
692
drh1bee3d72001-10-15 00:44:35 +0000693 /*
danielk1977e7de6f22004-11-05 06:02:06 +0000694 ** Return the number of rows inserted. If this routine is
695 ** generating code because of a call to sqlite3NestedParse(), do not
696 ** invoke the callback function.
drh1bee3d72001-10-15 00:44:35 +0000697 */
danielk1977cc6bd382005-01-10 02:48:49 +0000698 if( db->flags & SQLITE_CountRows && pParse->nested==0 && !pParse->trigStack ){
danielk19774adee202004-05-08 08:23:19 +0000699 sqlite3VdbeAddOp(v, OP_MemLoad, iCntMem, 0);
700 sqlite3VdbeAddOp(v, OP_Callback, 1, 0);
danielk197722322fd2004-05-25 23:35:17 +0000701 sqlite3VdbeSetNumCols(v, 1);
danielk19773cf86062004-05-26 10:11:05 +0000702 sqlite3VdbeSetColName(v, 0, "rows inserted", P3_STATIC);
drh1bee3d72001-10-15 00:44:35 +0000703 }
drhcce7d172000-05-31 15:34:51 +0000704
705insert_cleanup:
danielk19774adee202004-05-08 08:23:19 +0000706 sqlite3SrcListDelete(pTabList);
danielk1977d5d56522005-03-16 12:15:20 +0000707 sqlite3ExprListDelete(pList);
708 sqlite3SelectDelete(pSelect);
danielk19774adee202004-05-08 08:23:19 +0000709 sqlite3IdListDelete(pColumn);
drhcce7d172000-05-31 15:34:51 +0000710}
drh9cfcf5d2002-01-29 18:41:24 +0000711
drh9cfcf5d2002-01-29 18:41:24 +0000712/*
713** Generate code to do a constraint check prior to an INSERT or an UPDATE.
714**
715** When this routine is called, the stack contains (from bottom to top)
drh0ca3e242002-01-29 23:07:02 +0000716** the following values:
717**
drhf0863fe2005-06-12 21:35:51 +0000718** 1. The rowid of the row to be updated before the update. This
drhb419a922002-01-30 16:17:23 +0000719** value is omitted unless we are doing an UPDATE that involves a
720** change to the record number.
drh0ca3e242002-01-29 23:07:02 +0000721**
drhf0863fe2005-06-12 21:35:51 +0000722** 2. The rowid of the row after the update.
drh0ca3e242002-01-29 23:07:02 +0000723**
724** 3. The data in the first column of the entry after the update.
725**
726** i. Data from middle columns...
727**
728** N. The data in the last column of the entry after the update.
729**
drhf0863fe2005-06-12 21:35:51 +0000730** The old rowid shown as entry (1) above is omitted unless both isUpdate
731** and rowidChng are 1. isUpdate is true for UPDATEs and false for
732** INSERTs and rowidChng is true if the record number is being changed.
drh0ca3e242002-01-29 23:07:02 +0000733**
734** The code generated by this routine pushes additional entries onto
735** the stack which are the keys for new index entries for the new record.
736** The order of index keys is the same as the order of the indices on
737** the pTable->pIndex list. A key is only created for index i if
738** aIdxUsed!=0 and aIdxUsed[i]!=0.
drh9cfcf5d2002-01-29 18:41:24 +0000739**
740** This routine also generates code to check constraints. NOT NULL,
741** CHECK, and UNIQUE constraints are all checked. If a constraint fails,
drh1c928532002-01-31 15:54:21 +0000742** then the appropriate action is performed. There are five possible
743** actions: ROLLBACK, ABORT, FAIL, REPLACE, and IGNORE.
drh9cfcf5d2002-01-29 18:41:24 +0000744**
745** Constraint type Action What Happens
746** --------------- ---------- ----------------------------------------
drh1c928532002-01-31 15:54:21 +0000747** any ROLLBACK The current transaction is rolled back and
danielk197724b03fd2004-05-10 10:34:34 +0000748** sqlite3_exec() returns immediately with a
drh9cfcf5d2002-01-29 18:41:24 +0000749** return code of SQLITE_CONSTRAINT.
750**
drh1c928532002-01-31 15:54:21 +0000751** any ABORT Back out changes from the current command
752** only (do not do a complete rollback) then
danielk197724b03fd2004-05-10 10:34:34 +0000753** cause sqlite3_exec() to return immediately
drh1c928532002-01-31 15:54:21 +0000754** with SQLITE_CONSTRAINT.
755**
756** any FAIL Sqlite_exec() returns immediately with a
757** return code of SQLITE_CONSTRAINT. The
758** transaction is not rolled back and any
759** prior changes are retained.
760**
drh9cfcf5d2002-01-29 18:41:24 +0000761** any IGNORE The record number and data is popped from
762** the stack and there is an immediate jump
763** to label ignoreDest.
764**
765** NOT NULL REPLACE The NULL value is replace by the default
766** value for that column. If the default value
767** is NULL, the action is the same as ABORT.
768**
769** UNIQUE REPLACE The other row that conflicts with the row
770** being inserted is removed.
771**
772** CHECK REPLACE Illegal. The results in an exception.
773**
drh1c928532002-01-31 15:54:21 +0000774** Which action to take is determined by the overrideError parameter.
775** Or if overrideError==OE_Default, then the pParse->onError parameter
776** is used. Or if pParse->onError==OE_Default then the onError value
777** for the constraint is used.
drh9cfcf5d2002-01-29 18:41:24 +0000778**
drhaaab5722002-02-19 13:39:21 +0000779** The calling routine must open a read/write cursor for pTab with
drh9cfcf5d2002-01-29 18:41:24 +0000780** cursor number "base". All indices of pTab must also have open
781** read/write cursors with cursor number base+i for the i-th cursor.
782** Except, if there is no possibility of a REPLACE action then
783** cursors do not need to be open for indices where aIdxUsed[i]==0.
784**
785** If the isUpdate flag is true, it means that the "base" cursor is
786** initially pointing to an entry that is being updated. The isUpdate
787** flag causes extra code to be generated so that the "base" cursor
788** is still pointing at the same entry after the routine returns.
789** Without the isUpdate flag, the "base" cursor might be moved.
790*/
danielk19774adee202004-05-08 08:23:19 +0000791void sqlite3GenerateConstraintChecks(
drh9cfcf5d2002-01-29 18:41:24 +0000792 Parse *pParse, /* The parser context */
793 Table *pTab, /* the table into which we are inserting */
794 int base, /* Index of a read/write cursor pointing at pTab */
795 char *aIdxUsed, /* Which indices are used. NULL means all are used */
drhf0863fe2005-06-12 21:35:51 +0000796 int rowidChng, /* True if the record number will change */
drhb419a922002-01-30 16:17:23 +0000797 int isUpdate, /* True for UPDATE, False for INSERT */
drh9cfcf5d2002-01-29 18:41:24 +0000798 int overrideError, /* Override onError to this if not OE_Default */
drhb419a922002-01-30 16:17:23 +0000799 int ignoreDest /* Jump to this label on an OE_Ignore resolution */
drh9cfcf5d2002-01-29 18:41:24 +0000800){
801 int i;
802 Vdbe *v;
803 int nCol;
804 int onError;
805 int addr;
806 int extra;
drh0ca3e242002-01-29 23:07:02 +0000807 int iCur;
808 Index *pIdx;
809 int seenReplace = 0;
danielk1977cfe9a692004-06-16 12:00:29 +0000810 int jumpInst1=0, jumpInst2;
drhf0863fe2005-06-12 21:35:51 +0000811 int hasTwoRowids = (isUpdate && rowidChng);
drh9cfcf5d2002-01-29 18:41:24 +0000812
danielk19774adee202004-05-08 08:23:19 +0000813 v = sqlite3GetVdbe(pParse);
drh9cfcf5d2002-01-29 18:41:24 +0000814 assert( v!=0 );
drh417be792002-03-03 18:59:40 +0000815 assert( pTab->pSelect==0 ); /* This table is not a VIEW */
drh9cfcf5d2002-01-29 18:41:24 +0000816 nCol = pTab->nCol;
817
818 /* Test all NOT NULL constraints.
819 */
820 for(i=0; i<nCol; i++){
drh0ca3e242002-01-29 23:07:02 +0000821 if( i==pTab->iPKey ){
drh0ca3e242002-01-29 23:07:02 +0000822 continue;
823 }
drh9cfcf5d2002-01-29 18:41:24 +0000824 onError = pTab->aCol[i].notNull;
drh0ca3e242002-01-29 23:07:02 +0000825 if( onError==OE_None ) continue;
drh9cfcf5d2002-01-29 18:41:24 +0000826 if( overrideError!=OE_Default ){
827 onError = overrideError;
drha996e472003-05-16 02:30:27 +0000828 }else if( onError==OE_Default ){
829 onError = OE_Abort;
drh9cfcf5d2002-01-29 18:41:24 +0000830 }
danielk19777977a172004-11-09 12:44:37 +0000831 if( onError==OE_Replace && pTab->aCol[i].pDflt==0 ){
drh9cfcf5d2002-01-29 18:41:24 +0000832 onError = OE_Abort;
833 }
danielk19774adee202004-05-08 08:23:19 +0000834 sqlite3VdbeAddOp(v, OP_Dup, nCol-1-i, 1);
835 addr = sqlite3VdbeAddOp(v, OP_NotNull, 1, 0);
danielk1977b84f96f2005-01-20 11:32:23 +0000836 assert( onError==OE_Rollback || onError==OE_Abort || onError==OE_Fail
837 || onError==OE_Ignore || onError==OE_Replace );
drh9cfcf5d2002-01-29 18:41:24 +0000838 switch( onError ){
drh1c928532002-01-31 15:54:21 +0000839 case OE_Rollback:
840 case OE_Abort:
841 case OE_Fail: {
drh483750b2003-01-29 18:46:51 +0000842 char *zMsg = 0;
danielk19774adee202004-05-08 08:23:19 +0000843 sqlite3VdbeAddOp(v, OP_Halt, SQLITE_CONSTRAINT, onError);
844 sqlite3SetString(&zMsg, pTab->zName, ".", pTab->aCol[i].zName,
drh41743982003-12-06 21:43:55 +0000845 " may not be NULL", (char*)0);
danielk19774adee202004-05-08 08:23:19 +0000846 sqlite3VdbeChangeP3(v, -1, zMsg, P3_DYNAMIC);
drh9cfcf5d2002-01-29 18:41:24 +0000847 break;
848 }
849 case OE_Ignore: {
drhf0863fe2005-06-12 21:35:51 +0000850 sqlite3VdbeAddOp(v, OP_Pop, nCol+1+hasTwoRowids, 0);
danielk19774adee202004-05-08 08:23:19 +0000851 sqlite3VdbeAddOp(v, OP_Goto, 0, ignoreDest);
drh9cfcf5d2002-01-29 18:41:24 +0000852 break;
853 }
854 case OE_Replace: {
danielk19777977a172004-11-09 12:44:37 +0000855 sqlite3ExprCode(pParse, pTab->aCol[i].pDflt);
danielk19774adee202004-05-08 08:23:19 +0000856 sqlite3VdbeAddOp(v, OP_Push, nCol-i, 0);
drh9cfcf5d2002-01-29 18:41:24 +0000857 break;
858 }
drh9cfcf5d2002-01-29 18:41:24 +0000859 }
drhd654be82005-09-20 17:42:23 +0000860 sqlite3VdbeJumpHere(v, addr);
drh9cfcf5d2002-01-29 18:41:24 +0000861 }
862
863 /* Test all CHECK constraints
864 */
drhffe07b22005-11-03 00:41:17 +0000865#ifndef SQLITE_OMIT_CHECK
drh0cd2d4c2005-11-03 02:15:02 +0000866 if( pTab->pCheck && (pParse->db->flags & SQLITE_IgnoreChecks)==0 ){
drhffe07b22005-11-03 00:41:17 +0000867 int allOk = sqlite3VdbeMakeLabel(v);
868 assert( pParse->ckOffset==0 );
869 pParse->ckOffset = nCol;
drh6275b882005-11-03 01:22:30 +0000870 sqlite3ExprIfTrue(pParse, pTab->pCheck, allOk, 1);
drhffe07b22005-11-03 00:41:17 +0000871 assert( pParse->ckOffset==nCol );
872 pParse->ckOffset = 0;
873 sqlite3VdbeAddOp(v, OP_Halt, SQLITE_CONSTRAINT, OE_Abort);
874 sqlite3VdbeResolveLabel(v, allOk);
875 }
876#endif /* !defined(SQLITE_OMIT_CHECK) */
drh9cfcf5d2002-01-29 18:41:24 +0000877
drh0bd1f4e2002-06-06 18:54:39 +0000878 /* If we have an INTEGER PRIMARY KEY, make sure the primary key
879 ** of the new record does not previously exist. Except, if this
880 ** is an UPDATE and the primary key is not changing, that is OK.
drh9cfcf5d2002-01-29 18:41:24 +0000881 */
drhf0863fe2005-06-12 21:35:51 +0000882 if( rowidChng ){
drh0ca3e242002-01-29 23:07:02 +0000883 onError = pTab->keyConf;
884 if( overrideError!=OE_Default ){
885 onError = overrideError;
drha996e472003-05-16 02:30:27 +0000886 }else if( onError==OE_Default ){
887 onError = OE_Abort;
drh0ca3e242002-01-29 23:07:02 +0000888 }
drha0217ba2003-06-01 01:10:33 +0000889
drh5383ae52003-06-04 12:23:30 +0000890 if( isUpdate ){
danielk19774adee202004-05-08 08:23:19 +0000891 sqlite3VdbeAddOp(v, OP_Dup, nCol+1, 1);
892 sqlite3VdbeAddOp(v, OP_Dup, nCol+1, 1);
893 jumpInst1 = sqlite3VdbeAddOp(v, OP_Eq, 0, 0);
drh5383ae52003-06-04 12:23:30 +0000894 }
danielk19774adee202004-05-08 08:23:19 +0000895 sqlite3VdbeAddOp(v, OP_Dup, nCol, 1);
896 jumpInst2 = sqlite3VdbeAddOp(v, OP_NotExists, base, 0);
drh5383ae52003-06-04 12:23:30 +0000897 switch( onError ){
898 default: {
899 onError = OE_Abort;
900 /* Fall thru into the next case */
drh79b0c952002-05-21 12:56:43 +0000901 }
drh5383ae52003-06-04 12:23:30 +0000902 case OE_Rollback:
903 case OE_Abort:
904 case OE_Fail: {
danielk19774adee202004-05-08 08:23:19 +0000905 sqlite3VdbeOp3(v, OP_Halt, SQLITE_CONSTRAINT, onError,
drh701a0ae2004-02-22 20:05:00 +0000906 "PRIMARY KEY must be unique", P3_STATIC);
drh5383ae52003-06-04 12:23:30 +0000907 break;
drh0ca3e242002-01-29 23:07:02 +0000908 }
drh5383ae52003-06-04 12:23:30 +0000909 case OE_Replace: {
danielk19774adee202004-05-08 08:23:19 +0000910 sqlite3GenerateRowIndexDelete(pParse->db, v, pTab, base, 0);
drh5383ae52003-06-04 12:23:30 +0000911 if( isUpdate ){
drhf0863fe2005-06-12 21:35:51 +0000912 sqlite3VdbeAddOp(v, OP_Dup, nCol+hasTwoRowids, 1);
drh7cf6e4d2004-05-19 14:56:55 +0000913 sqlite3VdbeAddOp(v, OP_MoveGe, base, 0);
drh5383ae52003-06-04 12:23:30 +0000914 }
915 seenReplace = 1;
916 break;
drh0ca3e242002-01-29 23:07:02 +0000917 }
drh5383ae52003-06-04 12:23:30 +0000918 case OE_Ignore: {
919 assert( seenReplace==0 );
drhf0863fe2005-06-12 21:35:51 +0000920 sqlite3VdbeAddOp(v, OP_Pop, nCol+1+hasTwoRowids, 0);
danielk19774adee202004-05-08 08:23:19 +0000921 sqlite3VdbeAddOp(v, OP_Goto, 0, ignoreDest);
drh5383ae52003-06-04 12:23:30 +0000922 break;
923 }
924 }
drhd654be82005-09-20 17:42:23 +0000925 sqlite3VdbeJumpHere(v, jumpInst2);
drh5383ae52003-06-04 12:23:30 +0000926 if( isUpdate ){
drhd654be82005-09-20 17:42:23 +0000927 sqlite3VdbeJumpHere(v, jumpInst1);
danielk19774adee202004-05-08 08:23:19 +0000928 sqlite3VdbeAddOp(v, OP_Dup, nCol+1, 1);
drh7cf6e4d2004-05-19 14:56:55 +0000929 sqlite3VdbeAddOp(v, OP_MoveGe, base, 0);
drh0ca3e242002-01-29 23:07:02 +0000930 }
931 }
drh0bd1f4e2002-06-06 18:54:39 +0000932
933 /* Test all UNIQUE constraints by creating entries for each UNIQUE
934 ** index and making sure that duplicate entries do not already exist.
935 ** Add the new records to the indices as we go.
936 */
drhb2fe7d82003-04-20 17:29:23 +0000937 extra = -1;
938 for(iCur=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, iCur++){
939 if( aIdxUsed && aIdxUsed[iCur]==0 ) continue; /* Skip unused indices */
940 extra++;
941
942 /* Create a key for accessing the index entry */
danielk19774adee202004-05-08 08:23:19 +0000943 sqlite3VdbeAddOp(v, OP_Dup, nCol+extra, 1);
drh9cfcf5d2002-01-29 18:41:24 +0000944 for(i=0; i<pIdx->nColumn; i++){
945 int idx = pIdx->aiColumn[i];
946 if( idx==pTab->iPKey ){
danielk19774adee202004-05-08 08:23:19 +0000947 sqlite3VdbeAddOp(v, OP_Dup, i+extra+nCol+1, 1);
drh9cfcf5d2002-01-29 18:41:24 +0000948 }else{
danielk19774adee202004-05-08 08:23:19 +0000949 sqlite3VdbeAddOp(v, OP_Dup, i+extra+nCol-idx, 1);
drh9cfcf5d2002-01-29 18:41:24 +0000950 }
951 }
drh7f057c92005-06-24 03:53:06 +0000952 jumpInst1 = sqlite3VdbeAddOp(v, OP_MakeIdxRec, pIdx->nColumn, 0);
danielk1977a37cdde2004-05-16 11:15:36 +0000953 sqlite3IndexAffinityStr(v, pIdx);
drhb2fe7d82003-04-20 17:29:23 +0000954
955 /* Find out what action to take in case there is an indexing conflict */
drh9cfcf5d2002-01-29 18:41:24 +0000956 onError = pIdx->onError;
drhb2fe7d82003-04-20 17:29:23 +0000957 if( onError==OE_None ) continue; /* pIdx is not a UNIQUE index */
drh9cfcf5d2002-01-29 18:41:24 +0000958 if( overrideError!=OE_Default ){
959 onError = overrideError;
drha996e472003-05-16 02:30:27 +0000960 }else if( onError==OE_Default ){
961 onError = OE_Abort;
drh9cfcf5d2002-01-29 18:41:24 +0000962 }
drh5383ae52003-06-04 12:23:30 +0000963 if( seenReplace ){
964 if( onError==OE_Ignore ) onError = OE_Replace;
965 else if( onError==OE_Fail ) onError = OE_Abort;
966 }
967
drhb2fe7d82003-04-20 17:29:23 +0000968
969 /* Check to see if the new index entry will be unique */
drhf0863fe2005-06-12 21:35:51 +0000970 sqlite3VdbeAddOp(v, OP_Dup, extra+nCol+1+hasTwoRowids, 1);
danielk19774adee202004-05-08 08:23:19 +0000971 jumpInst2 = sqlite3VdbeAddOp(v, OP_IsUnique, base+iCur+1, 0);
drhb2fe7d82003-04-20 17:29:23 +0000972
973 /* Generate code that executes if the new index entry is not unique */
danielk1977b84f96f2005-01-20 11:32:23 +0000974 assert( onError==OE_Rollback || onError==OE_Abort || onError==OE_Fail
975 || onError==OE_Ignore || onError==OE_Replace );
drh9cfcf5d2002-01-29 18:41:24 +0000976 switch( onError ){
drh1c928532002-01-31 15:54:21 +0000977 case OE_Rollback:
978 case OE_Abort:
979 case OE_Fail: {
drh37ed48e2003-08-05 13:13:38 +0000980 int j, n1, n2;
981 char zErrMsg[200];
982 strcpy(zErrMsg, pIdx->nColumn>1 ? "columns " : "column ");
983 n1 = strlen(zErrMsg);
984 for(j=0; j<pIdx->nColumn && n1<sizeof(zErrMsg)-30; j++){
985 char *zCol = pTab->aCol[pIdx->aiColumn[j]].zName;
986 n2 = strlen(zCol);
987 if( j>0 ){
988 strcpy(&zErrMsg[n1], ", ");
989 n1 += 2;
990 }
991 if( n1+n2>sizeof(zErrMsg)-30 ){
992 strcpy(&zErrMsg[n1], "...");
993 n1 += 3;
994 break;
995 }else{
996 strcpy(&zErrMsg[n1], zCol);
997 n1 += n2;
998 }
999 }
1000 strcpy(&zErrMsg[n1],
1001 pIdx->nColumn>1 ? " are not unique" : " is not unique");
danielk19774adee202004-05-08 08:23:19 +00001002 sqlite3VdbeOp3(v, OP_Halt, SQLITE_CONSTRAINT, onError, zErrMsg, 0);
drh9cfcf5d2002-01-29 18:41:24 +00001003 break;
1004 }
1005 case OE_Ignore: {
drh0ca3e242002-01-29 23:07:02 +00001006 assert( seenReplace==0 );
drhf0863fe2005-06-12 21:35:51 +00001007 sqlite3VdbeAddOp(v, OP_Pop, nCol+extra+3+hasTwoRowids, 0);
danielk19774adee202004-05-08 08:23:19 +00001008 sqlite3VdbeAddOp(v, OP_Goto, 0, ignoreDest);
drh9cfcf5d2002-01-29 18:41:24 +00001009 break;
1010 }
1011 case OE_Replace: {
danielk19774adee202004-05-08 08:23:19 +00001012 sqlite3GenerateRowDelete(pParse->db, v, pTab, base, 0);
drh9cfcf5d2002-01-29 18:41:24 +00001013 if( isUpdate ){
drhf0863fe2005-06-12 21:35:51 +00001014 sqlite3VdbeAddOp(v, OP_Dup, nCol+extra+1+hasTwoRowids, 1);
drh7cf6e4d2004-05-19 14:56:55 +00001015 sqlite3VdbeAddOp(v, OP_MoveGe, base, 0);
drh9cfcf5d2002-01-29 18:41:24 +00001016 }
drh0ca3e242002-01-29 23:07:02 +00001017 seenReplace = 1;
drh9cfcf5d2002-01-29 18:41:24 +00001018 break;
1019 }
drh9cfcf5d2002-01-29 18:41:24 +00001020 }
drh0bd1f4e2002-06-06 18:54:39 +00001021#if NULL_DISTINCT_FOR_UNIQUE
drhd654be82005-09-20 17:42:23 +00001022 sqlite3VdbeJumpHere(v, jumpInst1);
drh0bd1f4e2002-06-06 18:54:39 +00001023#endif
drhd654be82005-09-20 17:42:23 +00001024 sqlite3VdbeJumpHere(v, jumpInst2);
drh9cfcf5d2002-01-29 18:41:24 +00001025 }
1026}
drh0ca3e242002-01-29 23:07:02 +00001027
1028/*
1029** This routine generates code to finish the INSERT or UPDATE operation
danielk19774adee202004-05-08 08:23:19 +00001030** that was started by a prior call to sqlite3GenerateConstraintChecks.
drh0ca3e242002-01-29 23:07:02 +00001031** The stack must contain keys for all active indices followed by data
drhf0863fe2005-06-12 21:35:51 +00001032** and the rowid for the new entry. This routine creates the new
drh0ca3e242002-01-29 23:07:02 +00001033** entries in all indices and in the main table.
1034**
drhb419a922002-01-30 16:17:23 +00001035** The arguments to this routine should be the same as the first six
danielk19774adee202004-05-08 08:23:19 +00001036** arguments to sqlite3GenerateConstraintChecks.
drh0ca3e242002-01-29 23:07:02 +00001037*/
danielk19774adee202004-05-08 08:23:19 +00001038void sqlite3CompleteInsertion(
drh0ca3e242002-01-29 23:07:02 +00001039 Parse *pParse, /* The parser context */
1040 Table *pTab, /* the table into which we are inserting */
1041 int base, /* Index of a read/write cursor pointing at pTab */
1042 char *aIdxUsed, /* Which indices are used. NULL means all are used */
drhf0863fe2005-06-12 21:35:51 +00001043 int rowidChng, /* True if the record number will change */
drh70ce3f02003-04-15 19:22:22 +00001044 int isUpdate, /* True for UPDATE, False for INSERT */
1045 int newIdx /* Index of NEW table for triggers. -1 if none */
drh0ca3e242002-01-29 23:07:02 +00001046){
1047 int i;
1048 Vdbe *v;
1049 int nIdx;
1050 Index *pIdx;
danielk1977b28af712004-06-21 06:50:26 +00001051 int pik_flags;
drh0ca3e242002-01-29 23:07:02 +00001052
danielk19774adee202004-05-08 08:23:19 +00001053 v = sqlite3GetVdbe(pParse);
drh0ca3e242002-01-29 23:07:02 +00001054 assert( v!=0 );
drh417be792002-03-03 18:59:40 +00001055 assert( pTab->pSelect==0 ); /* This table is not a VIEW */
drh0ca3e242002-01-29 23:07:02 +00001056 for(nIdx=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, nIdx++){}
1057 for(i=nIdx-1; i>=0; i--){
1058 if( aIdxUsed && aIdxUsed[i]==0 ) continue;
drhf0863fe2005-06-12 21:35:51 +00001059 sqlite3VdbeAddOp(v, OP_IdxInsert, base+i+1, 0);
drh0ca3e242002-01-29 23:07:02 +00001060 }
danielk19774adee202004-05-08 08:23:19 +00001061 sqlite3VdbeAddOp(v, OP_MakeRecord, pTab->nCol, 0);
danielk1977a37cdde2004-05-16 11:15:36 +00001062 sqlite3TableAffinityStr(v, pTab);
danielk1977b84f96f2005-01-20 11:32:23 +00001063#ifndef SQLITE_OMIT_TRIGGER
drh70ce3f02003-04-15 19:22:22 +00001064 if( newIdx>=0 ){
danielk19774adee202004-05-08 08:23:19 +00001065 sqlite3VdbeAddOp(v, OP_Dup, 1, 0);
1066 sqlite3VdbeAddOp(v, OP_Dup, 1, 0);
drhf0863fe2005-06-12 21:35:51 +00001067 sqlite3VdbeAddOp(v, OP_Insert, newIdx, 0);
drh70ce3f02003-04-15 19:22:22 +00001068 }
danielk1977b84f96f2005-01-20 11:32:23 +00001069#endif
drh4794f732004-11-05 17:17:50 +00001070 if( pParse->nested ){
1071 pik_flags = 0;
1072 }else{
danielk197794eb6a12005-12-15 15:22:08 +00001073 pik_flags = OPFLAG_NCHANGE;
1074 pik_flags |= (isUpdate?OPFLAG_ISUPDATE:OPFLAG_LASTROWID);
drh4794f732004-11-05 17:17:50 +00001075 }
drhf0863fe2005-06-12 21:35:51 +00001076 sqlite3VdbeAddOp(v, OP_Insert, base, pik_flags);
danielk197794eb6a12005-12-15 15:22:08 +00001077 if( !pParse->nested ){
1078 sqlite3VdbeChangeP3(v, -1, pTab->zName, P3_STATIC);
1079 }
danielk1977b28af712004-06-21 06:50:26 +00001080
drhf0863fe2005-06-12 21:35:51 +00001081 if( isUpdate && rowidChng ){
danielk19774adee202004-05-08 08:23:19 +00001082 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
drh0ca3e242002-01-29 23:07:02 +00001083 }
1084}
drhcd446902004-02-24 01:05:31 +00001085
1086/*
drh290c1942004-08-21 17:54:45 +00001087** Generate code that will open cursors for a table and for all
drhcd446902004-02-24 01:05:31 +00001088** indices of that table. The "base" parameter is the cursor number used
1089** for the table. Indices are opened on subsequent cursors.
drhcd446902004-02-24 01:05:31 +00001090*/
drh290c1942004-08-21 17:54:45 +00001091void sqlite3OpenTableAndIndices(
1092 Parse *pParse, /* Parsing context */
1093 Table *pTab, /* Table to be opened */
1094 int base, /* Cursor number assigned to the table */
1095 int op /* OP_OpenRead or OP_OpenWrite */
1096){
drhcd446902004-02-24 01:05:31 +00001097 int i;
danielk1977da184232006-01-05 11:34:32 +00001098 int iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
drhcd446902004-02-24 01:05:31 +00001099 Index *pIdx;
danielk19774adee202004-05-08 08:23:19 +00001100 Vdbe *v = sqlite3GetVdbe(pParse);
drhcd446902004-02-24 01:05:31 +00001101 assert( v!=0 );
danielk1977c00da102006-01-07 13:21:04 +00001102 sqlite3OpenTable(pParse, base, iDb, pTab, op);
drhcd446902004-02-24 01:05:31 +00001103 for(i=1, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){
danielk1977b3bf5562006-01-10 17:58:23 +00001104 KeyInfo *pKey = sqlite3IndexKeyinfo(pParse, pIdx);
danielk1977da184232006-01-05 11:34:32 +00001105 assert( pIdx->pSchema==pTab->pSchema );
1106 sqlite3VdbeAddOp(v, OP_Integer, iDb, 0);
drh29dda4a2005-07-21 18:23:20 +00001107 VdbeComment((v, "# %s", pIdx->zName));
danielk1977b3bf5562006-01-10 17:58:23 +00001108 sqlite3VdbeOp3(v, op, i+base, pIdx->tnum, (char*)pKey, P3_KEYINFO_HANDOFF);
drhcd446902004-02-24 01:05:31 +00001109 }
drh290c1942004-08-21 17:54:45 +00001110 if( pParse->nTab<=base+i ){
1111 pParse->nTab = base+i;
1112 }
drhcd446902004-02-24 01:05:31 +00001113}