blob: 888f129098ebb343bac573264c1cef30c6c19cfb [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**
drh1962bda2003-01-12 19:33:52 +000015** $Id: insert.c,v 1.70 2003/01/12 19:33:53 drh Exp $
drhcce7d172000-05-31 15:34:51 +000016*/
17#include "sqliteInt.h"
18
19/*
drh1ccde152000-06-17 13:12:39 +000020** This routine is call to handle SQL of the following forms:
drhcce7d172000-05-31 15:34:51 +000021**
22** insert into TABLE (IDLIST) values(EXPRLIST)
drh1ccde152000-06-17 13:12:39 +000023** insert into TABLE (IDLIST) select
drhcce7d172000-05-31 15:34:51 +000024**
drh1ccde152000-06-17 13:12:39 +000025** The IDLIST following the table name is always optional. If omitted,
26** then a list of all columns for the table is substituted. The IDLIST
drh967e8b72000-06-21 13:59:10 +000027** appears in the pColumn parameter. pColumn is NULL if IDLIST is omitted.
drh1ccde152000-06-17 13:12:39 +000028**
29** The pList parameter holds EXPRLIST in the first form of the INSERT
30** statement above, and pSelect is NULL. For the second form, pList is
31** NULL and pSelect is a pointer to the select statement used to generate
32** data for the insert.
drh142e30d2002-08-28 03:00:58 +000033**
34** The code generated follows one of three templates. For a simple
35** select with data coming from a VALUES clause, the code executes
36** once straight down through. The template looks like this:
37**
38** open write cursor to <table> and its indices
39** puts VALUES clause expressions onto the stack
40** write the resulting record into <table>
41** cleanup
42**
43** If the statement is of the form
44**
45** INSERT INTO <table> SELECT ...
46**
47** And the SELECT clause does not read from <table> at any time, then
48** the generated code follows this template:
49**
50** goto B
51** A: setup for the SELECT
52** loop over the tables in the SELECT
53** gosub C
54** end loop
55** cleanup after the SELECT
56** goto D
57** B: open write cursor to <table> and its indices
58** goto A
59** C: insert the select result into <table>
60** return
61** D: cleanup
62**
63** The third template is used if the insert statement takes its
64** values from a SELECT but the data is being inserted into a table
65** that is also read as part of the SELECT. In the third form,
66** we have to use a intermediate table to store the results of
67** the select. The template is like this:
68**
69** goto B
70** A: setup for the SELECT
71** loop over the tables in the SELECT
72** gosub C
73** end loop
74** cleanup after the SELECT
75** goto D
76** C: insert the select result into the intermediate table
77** return
78** B: open a cursor to an intermediate table
79** goto A
80** D: open write cursor to <table> and its indices
81** loop over the intermediate table
82** transfer values form intermediate table into <table>
83** end the loop
84** cleanup
drhcce7d172000-05-31 15:34:51 +000085*/
86void sqliteInsert(
87 Parse *pParse, /* Parser context */
88 Token *pTableName, /* Name of table into which we are inserting */
89 ExprList *pList, /* List of values to be inserted */
drh5974a302000-06-07 14:42:26 +000090 Select *pSelect, /* A SELECT statement to use as the data source */
drh9cfcf5d2002-01-29 18:41:24 +000091 IdList *pColumn, /* Column names corresponding to IDLIST. */
92 int onError /* How to handle constraint errors */
drhcce7d172000-05-31 15:34:51 +000093){
drh5974a302000-06-07 14:42:26 +000094 Table *pTab; /* The table to insert into */
danielk1977c3f9bad2002-05-15 08:30:12 +000095 char *zTab = 0; /* Name of the table into which we are inserting */
drh5974a302000-06-07 14:42:26 +000096 int i, j, idx; /* Loop counters */
97 Vdbe *v; /* Generate code into this virtual machine */
98 Index *pIdx; /* For looping over indices of the table */
drh967e8b72000-06-21 13:59:10 +000099 int nColumn; /* Number of columns in the data */
drh5974a302000-06-07 14:42:26 +0000100 int base; /* First available cursor */
101 int iCont, iBreak; /* Beginning and end of the loop over srcTab */
drhecdc7532001-09-23 02:35:53 +0000102 sqlite *db; /* The main database structure */
drhf57b3392001-10-08 13:22:32 +0000103 int openOp; /* Opcode used to open cursors */
drh4a324312001-12-21 14:30:42 +0000104 int keyColumn = -1; /* Column that is the INTEGER PRIMARY KEY */
drh0ca3e242002-01-29 23:07:02 +0000105 int endOfLoop; /* Label for the end of the insertion loop */
drh142e30d2002-08-28 03:00:58 +0000106 int useTempTable; /* Store SELECT results in intermediate table */
107 int srcTab; /* Data comes from this temporary cursor if >=0 */
108 int iSelectLoop; /* Address of code that implements the SELECT */
109 int iCleanup; /* Address of the cleanup code */
110 int iInsertBlock; /* Address of the subroutine used to insert data */
111 int iCntMem; /* Memory cell used for the row counter */
drhcce7d172000-05-31 15:34:51 +0000112
danielk1977c3f9bad2002-05-15 08:30:12 +0000113 int row_triggers_exist = 0; /* True if there are FOR EACH ROW triggers */
114 int newIdx = -1;
115
drhdaffd0e2001-04-11 14:28:42 +0000116 if( pParse->nErr || sqlite_malloc_failed ) goto insert_cleanup;
drhed6c8672003-01-12 18:02:16 +0000117 if( sqliteAuthCommand(pParse, "INSERT", 0) ) goto insert_cleanup;
drhecdc7532001-09-23 02:35:53 +0000118 db = pParse->db;
drhdaffd0e2001-04-11 14:28:42 +0000119
drh1ccde152000-06-17 13:12:39 +0000120 /* Locate the table into which we will be inserting new information.
121 */
drhcce7d172000-05-31 15:34:51 +0000122 zTab = sqliteTableNameFromToken(pTableName);
drhdaffd0e2001-04-11 14:28:42 +0000123 if( zTab==0 ) goto insert_cleanup;
danielk1977c3f9bad2002-05-15 08:30:12 +0000124 pTab = sqliteFindTable(pParse->db, zTab);
125 if( pTab==0 ){
126 sqliteSetString(&pParse->zErrMsg, "no such table: ", zTab, 0);
127 pParse->nErr++;
128 goto insert_cleanup;
129 }
drh1962bda2003-01-12 19:33:52 +0000130 if( sqliteAuthInsert(pParse, zTab, 0) ){
131 goto insert_cleanup;
132 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000133
134 /* Ensure that:
135 * (a) the table is not read-only,
136 * (b) that if it is a view then ON INSERT triggers exist
137 */
138 row_triggers_exist =
139 sqliteTriggersExist(pParse, pTab->pTrigger, TK_INSERT,
drh9adf9ac2002-05-15 11:44:13 +0000140 TK_BEFORE, TK_ROW, 0) ||
danielk1977c3f9bad2002-05-15 08:30:12 +0000141 sqliteTriggersExist(pParse, pTab->pTrigger, TK_INSERT, TK_AFTER, TK_ROW, 0);
142 if( pTab->readOnly || (pTab->pSelect && !row_triggers_exist) ){
143 sqliteSetString(&pParse->zErrMsg,
144 pTab->pSelect ? "view " : "table ",
145 zTab,
146 " may not be modified", 0);
147 pParse->nErr++;
148 goto insert_cleanup;
149 }
drhcce7d172000-05-31 15:34:51 +0000150 sqliteFree(zTab);
danielk1977c3f9bad2002-05-15 08:30:12 +0000151 zTab = 0;
152
drha76b5df2002-02-23 02:32:10 +0000153 if( pTab==0 ) goto insert_cleanup;
drh1ccde152000-06-17 13:12:39 +0000154
drhf573c992002-07-31 00:32:50 +0000155 /* If pTab is really a view, make sure it has been initialized.
156 */
157 if( pTab->pSelect ){
158 if( sqliteViewGetColumnNames(pParse, pTab) ){
159 goto insert_cleanup;
160 }
161 }
162
drh1ccde152000-06-17 13:12:39 +0000163 /* Allocate a VDBE
164 */
drhd8bc7082000-06-07 23:51:50 +0000165 v = sqliteGetVdbe(pParse);
drh5974a302000-06-07 14:42:26 +0000166 if( v==0 ) goto insert_cleanup;
drhcabb0812002-09-14 13:47:32 +0000167 sqliteBeginWriteOperation(pParse, pSelect || row_triggers_exist,
168 !row_triggers_exist && pTab->isTemp);
drh1ccde152000-06-17 13:12:39 +0000169
danielk1977c3f9bad2002-05-15 08:30:12 +0000170 /* if there are row triggers, allocate a temp table for new.* references. */
danielk1977f29ce552002-05-19 23:43:12 +0000171 if( row_triggers_exist ){
danielk1977c3f9bad2002-05-15 08:30:12 +0000172 newIdx = pParse->nTab++;
danielk1977f29ce552002-05-19 23:43:12 +0000173 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000174
drh1ccde152000-06-17 13:12:39 +0000175 /* Figure out how many columns of data are supplied. If the data
drh142e30d2002-08-28 03:00:58 +0000176 ** is coming from a SELECT statement, then this step also generates
177 ** all the code to implement the SELECT statement and invoke a subroutine
178 ** to process each row of the result. (Template 2.) If the SELECT
179 ** statement uses the the table that is being inserted into, then the
180 ** subroutine is also coded here. That subroutine stores the SELECT
181 ** results in a temporary table. (Template 3.)
drh1ccde152000-06-17 13:12:39 +0000182 */
drh5974a302000-06-07 14:42:26 +0000183 if( pSelect ){
drh142e30d2002-08-28 03:00:58 +0000184 /* Data is coming from a SELECT. Generate code to implement that SELECT
185 */
186 int rc, iInitCode;
187 int opCode;
188 iInitCode = sqliteVdbeAddOp(v, OP_Goto, 0, 0);
189 iSelectLoop = sqliteVdbeCurrentAddr(v);
190 iInsertBlock = sqliteVdbeMakeLabel(v);
191 rc = sqliteSelect(pParse, pSelect, SRT_Subroutine, iInsertBlock, 0,0,0);
drhdaffd0e2001-04-11 14:28:42 +0000192 if( rc || pParse->nErr || sqlite_malloc_failed ) goto insert_cleanup;
drh142e30d2002-08-28 03:00:58 +0000193 iCleanup = sqliteVdbeMakeLabel(v);
194 sqliteVdbeAddOp(v, OP_Goto, 0, iCleanup);
drh5974a302000-06-07 14:42:26 +0000195 assert( pSelect->pEList );
drh967e8b72000-06-21 13:59:10 +0000196 nColumn = pSelect->pEList->nExpr;
drh142e30d2002-08-28 03:00:58 +0000197
198 /* Set useTempTable to TRUE if the result of the SELECT statement
199 ** should be written into a temporary table. Set to FALSE if each
200 ** row of the SELECT can be written directly into the result table.
201 */
202 opCode = pTab->isTemp ? OP_OpenTemp : OP_Open;
203 useTempTable = row_triggers_exist || sqliteVdbeFindOp(v,opCode,pTab->tnum);
204
205 if( useTempTable ){
206 /* Generate the subroutine that SELECT calls to process each row of
207 ** the result. Store the result in a temporary table
208 */
209 srcTab = pParse->nTab++;
210 sqliteVdbeResolveLabel(v, iInsertBlock);
211 sqliteVdbeAddOp(v, OP_MakeRecord, nColumn, 0);
212 sqliteVdbeAddOp(v, OP_NewRecno, srcTab, 0);
213 sqliteVdbeAddOp(v, OP_Pull, 1, 0);
214 sqliteVdbeAddOp(v, OP_PutIntKey, srcTab, 0);
215 sqliteVdbeAddOp(v, OP_Return, 0, 0);
216
217 /* The following code runs first because the GOTO at the very top
218 ** of the program jumps to it. Create the temporary table, then jump
219 ** back up and execute the SELECT code above.
220 */
221 sqliteVdbeChangeP2(v, iInitCode, sqliteVdbeCurrentAddr(v));
222 sqliteVdbeAddOp(v, OP_OpenTemp, srcTab, 0);
223 sqliteVdbeAddOp(v, OP_Goto, 0, iSelectLoop);
224 sqliteVdbeResolveLabel(v, iCleanup);
225 }else{
226 sqliteVdbeChangeP2(v, iInitCode, sqliteVdbeCurrentAddr(v));
227 }
drh5974a302000-06-07 14:42:26 +0000228 }else{
drh142e30d2002-08-28 03:00:58 +0000229 /* This is the case if the data for the INSERT is coming from a VALUES
230 ** clause
231 */
drhad3cab52002-05-24 02:04:32 +0000232 SrcList dummy;
drhdaffd0e2001-04-11 14:28:42 +0000233 assert( pList!=0 );
drh5974a302000-06-07 14:42:26 +0000234 srcTab = -1;
drh142e30d2002-08-28 03:00:58 +0000235 useTempTable = 0;
drh5974a302000-06-07 14:42:26 +0000236 assert( pList );
drh967e8b72000-06-21 13:59:10 +0000237 nColumn = pList->nExpr;
drhad3cab52002-05-24 02:04:32 +0000238 dummy.nSrc = 0;
drhe64e7b22002-02-18 13:56:36 +0000239 for(i=0; i<nColumn; i++){
drh832508b2002-03-02 17:04:07 +0000240 if( sqliteExprResolveIds(pParse, 0, &dummy, 0, pList->a[i].pExpr) ){
drhe64e7b22002-02-18 13:56:36 +0000241 goto insert_cleanup;
242 }
drhb04a5d82002-04-12 03:55:15 +0000243 if( sqliteExprCheck(pParse, pList->a[i].pExpr, 0, 0) ){
244 goto insert_cleanup;
245 }
drhe64e7b22002-02-18 13:56:36 +0000246 }
drh5974a302000-06-07 14:42:26 +0000247 }
drh1ccde152000-06-17 13:12:39 +0000248
249 /* Make sure the number of columns in the source data matches the number
250 ** of columns to be inserted into the table.
251 */
drh967e8b72000-06-21 13:59:10 +0000252 if( pColumn==0 && nColumn!=pTab->nCol ){
drhcce7d172000-05-31 15:34:51 +0000253 char zNum1[30];
254 char zNum2[30];
drh967e8b72000-06-21 13:59:10 +0000255 sprintf(zNum1,"%d", nColumn);
drhcce7d172000-05-31 15:34:51 +0000256 sprintf(zNum2,"%d", pTab->nCol);
257 sqliteSetString(&pParse->zErrMsg, "table ", pTab->zName,
258 " has ", zNum2, " columns but ",
259 zNum1, " values were supplied", 0);
260 pParse->nErr++;
261 goto insert_cleanup;
262 }
drh967e8b72000-06-21 13:59:10 +0000263 if( pColumn!=0 && nColumn!=pColumn->nId ){
drhcce7d172000-05-31 15:34:51 +0000264 char zNum1[30];
265 char zNum2[30];
drh967e8b72000-06-21 13:59:10 +0000266 sprintf(zNum1,"%d", nColumn);
267 sprintf(zNum2,"%d", pColumn->nId);
drhcce7d172000-05-31 15:34:51 +0000268 sqliteSetString(&pParse->zErrMsg, zNum1, " values for ",
269 zNum2, " columns", 0);
270 pParse->nErr++;
271 goto insert_cleanup;
272 }
drh1ccde152000-06-17 13:12:39 +0000273
274 /* If the INSERT statement included an IDLIST term, then make sure
275 ** all elements of the IDLIST really are columns of the table and
276 ** remember the column indices.
drhc8392582001-12-31 02:48:51 +0000277 **
278 ** If the table has an INTEGER PRIMARY KEY column and that column
279 ** is named in the IDLIST, then record in the keyColumn variable
280 ** the index into IDLIST of the primary key column. keyColumn is
281 ** the index of the primary key as it appears in IDLIST, not as
282 ** is appears in the original table. (The index of the primary
283 ** key in the original table is pTab->iPKey.)
drh1ccde152000-06-17 13:12:39 +0000284 */
drh967e8b72000-06-21 13:59:10 +0000285 if( pColumn ){
286 for(i=0; i<pColumn->nId; i++){
287 pColumn->a[i].idx = -1;
drhcce7d172000-05-31 15:34:51 +0000288 }
drh967e8b72000-06-21 13:59:10 +0000289 for(i=0; i<pColumn->nId; i++){
drhcce7d172000-05-31 15:34:51 +0000290 for(j=0; j<pTab->nCol; j++){
drh967e8b72000-06-21 13:59:10 +0000291 if( sqliteStrICmp(pColumn->a[i].zName, pTab->aCol[j].zName)==0 ){
292 pColumn->a[i].idx = j;
drh4a324312001-12-21 14:30:42 +0000293 if( j==pTab->iPKey ){
drh9aa028d2001-12-22 21:48:29 +0000294 keyColumn = i;
drh4a324312001-12-21 14:30:42 +0000295 }
drhcce7d172000-05-31 15:34:51 +0000296 break;
297 }
298 }
299 if( j>=pTab->nCol ){
300 sqliteSetString(&pParse->zErrMsg, "table ", pTab->zName,
drh967e8b72000-06-21 13:59:10 +0000301 " has no column named ", pColumn->a[i].zName, 0);
drhcce7d172000-05-31 15:34:51 +0000302 pParse->nErr++;
303 goto insert_cleanup;
304 }
305 }
306 }
drh1ccde152000-06-17 13:12:39 +0000307
drhaacc5432002-01-06 17:07:40 +0000308 /* If there is no IDLIST term but the table has an integer primary
drhc8392582001-12-31 02:48:51 +0000309 ** key, the set the keyColumn variable to the primary key column index
310 ** in the original table definition.
drh4a324312001-12-21 14:30:42 +0000311 */
312 if( pColumn==0 ){
313 keyColumn = pTab->iPKey;
314 }
315
drh142e30d2002-08-28 03:00:58 +0000316 /* Open the temp table for FOR EACH ROW triggers
317 */
danielk1977f29ce552002-05-19 23:43:12 +0000318 if( row_triggers_exist ){
danielk1977c3f9bad2002-05-15 08:30:12 +0000319 sqliteVdbeAddOp(v, OP_OpenTemp, newIdx, 0);
danielk1977f29ce552002-05-19 23:43:12 +0000320 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000321
drhfeeb1392002-04-09 03:28:01 +0000322 /* Initialize the count of rows to be inserted
323 */
drh142e30d2002-08-28 03:00:58 +0000324 if( db->flags & SQLITE_CountRows ){
325 iCntMem = pParse->nMem++;
326 sqliteVdbeAddOp(v, OP_Integer, 0, 0);
327 sqliteVdbeAddOp(v, OP_MemStore, iCntMem, 1);
drhfeeb1392002-04-09 03:28:01 +0000328 }
329
danielk1977c3f9bad2002-05-15 08:30:12 +0000330 /* Open tables and indices if there are no row triggers */
danielk1977f29ce552002-05-19 23:43:12 +0000331 if( !row_triggers_exist ){
danielk1977c3f9bad2002-05-15 08:30:12 +0000332 base = pParse->nTab;
333 openOp = pTab->isTemp ? OP_OpenWrAux : OP_OpenWrite;
334 sqliteVdbeAddOp(v, openOp, base, pTab->tnum);
335 sqliteVdbeChangeP3(v, -1, pTab->zName, P3_STATIC);
336 for(idx=1, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, idx++){
337 sqliteVdbeAddOp(v, openOp, idx+base, pIdx->tnum);
338 sqliteVdbeChangeP3(v, -1, pIdx->zName, P3_STATIC);
339 }
340 pParse->nTab += idx;
341 }
342
drh142e30d2002-08-28 03:00:58 +0000343 /* If the data source is a temporary table, then we have to create
drh1ccde152000-06-17 13:12:39 +0000344 ** a loop because there might be multiple rows of data. If the data
drh142e30d2002-08-28 03:00:58 +0000345 ** source is a subroutine call from the SELECT statement, then we need
346 ** to launch the SELECT statement processing.
drh1ccde152000-06-17 13:12:39 +0000347 */
drh142e30d2002-08-28 03:00:58 +0000348 if( useTempTable ){
drh5974a302000-06-07 14:42:26 +0000349 iBreak = sqliteVdbeMakeLabel(v);
drh6b563442001-11-07 16:48:26 +0000350 sqliteVdbeAddOp(v, OP_Rewind, srcTab, iBreak);
351 iCont = sqliteVdbeCurrentAddr(v);
drh142e30d2002-08-28 03:00:58 +0000352 }else if( pSelect ){
353 sqliteVdbeAddOp(v, OP_Goto, 0, iSelectLoop);
354 sqliteVdbeResolveLabel(v, iInsertBlock);
drh5974a302000-06-07 14:42:26 +0000355 }
drh1ccde152000-06-17 13:12:39 +0000356
danielk19776f349032002-06-11 02:25:40 +0000357 endOfLoop = sqliteVdbeMakeLabel(v);
danielk1977f29ce552002-05-19 23:43:12 +0000358 if( row_triggers_exist ){
danielk1977c3f9bad2002-05-15 08:30:12 +0000359
360 /* build the new.* reference row */
361 sqliteVdbeAddOp(v, OP_Integer, 13, 0);
362 for(i=0; i<pTab->nCol; i++){
363 if( pColumn==0 ){
drh9adf9ac2002-05-15 11:44:13 +0000364 j = i;
danielk1977c3f9bad2002-05-15 08:30:12 +0000365 }else{
drh9adf9ac2002-05-15 11:44:13 +0000366 for(j=0; j<pColumn->nId; j++){
367 if( pColumn->a[j].idx==i ) break;
368 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000369 }
370 if( pColumn && j>=pColumn->nId ){
drh9adf9ac2002-05-15 11:44:13 +0000371 sqliteVdbeAddOp(v, OP_String, 0, 0);
372 sqliteVdbeChangeP3(v, -1, pTab->aCol[i].zDflt, P3_STATIC);
drh142e30d2002-08-28 03:00:58 +0000373 }else if( useTempTable ){
drh9adf9ac2002-05-15 11:44:13 +0000374 sqliteVdbeAddOp(v, OP_Column, srcTab, j);
drh142e30d2002-08-28 03:00:58 +0000375 }else if( pSelect ){
376 sqliteVdbeAddOp(v, OP_Dup, nColumn-j-1, 1);
danielk1977c3f9bad2002-05-15 08:30:12 +0000377 }else{
drh9adf9ac2002-05-15 11:44:13 +0000378 sqliteExprCode(pParse, pList->a[j].pExpr);
danielk1977c3f9bad2002-05-15 08:30:12 +0000379 }
380 }
381 sqliteVdbeAddOp(v, OP_MakeRecord, pTab->nCol, 0);
382 sqliteVdbeAddOp(v, OP_PutIntKey, newIdx, 0);
383 sqliteVdbeAddOp(v, OP_Rewind, newIdx, 0);
384
385 /* Fire BEFORE triggers */
danielk1977f29ce552002-05-19 23:43:12 +0000386 if( sqliteCodeRowTrigger(pParse, TK_INSERT, 0, TK_BEFORE, pTab, newIdx, -1,
danielk19776f349032002-06-11 02:25:40 +0000387 onError, endOfLoop) ){
danielk1977f29ce552002-05-19 23:43:12 +0000388 goto insert_cleanup;
389 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000390
391 /* Open the tables and indices for the INSERT */
danielk1977f29ce552002-05-19 23:43:12 +0000392 if( !pTab->pSelect ){
danielk1977c3f9bad2002-05-15 08:30:12 +0000393 base = pParse->nTab;
394 openOp = pTab->isTemp ? OP_OpenWrAux : OP_OpenWrite;
395 sqliteVdbeAddOp(v, openOp, base, pTab->tnum);
396 sqliteVdbeChangeP3(v, -1, pTab->zName, P3_STATIC);
397 for(idx=1, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, idx++){
drh9adf9ac2002-05-15 11:44:13 +0000398 sqliteVdbeAddOp(v, openOp, idx+base, pIdx->tnum);
399 sqliteVdbeChangeP3(v, -1, pIdx->zName, P3_STATIC);
danielk1977c3f9bad2002-05-15 08:30:12 +0000400 }
401 pParse->nTab += idx;
402 }
403 }
404
drh4a324312001-12-21 14:30:42 +0000405 /* Push the record number for the new entry onto the stack. The
406 ** record number is a randomly generate integer created by NewRecno
407 ** except when the table has an INTEGER PRIMARY KEY column, in which
drhb419a922002-01-30 16:17:23 +0000408 ** case the record number is the same as that column.
drh1ccde152000-06-17 13:12:39 +0000409 */
danielk1977f29ce552002-05-19 23:43:12 +0000410 if( !pTab->pSelect ){
danielk1977c3f9bad2002-05-15 08:30:12 +0000411 if( keyColumn>=0 ){
drh142e30d2002-08-28 03:00:58 +0000412 if( useTempTable ){
drh9adf9ac2002-05-15 11:44:13 +0000413 sqliteVdbeAddOp(v, OP_Column, srcTab, keyColumn);
drh142e30d2002-08-28 03:00:58 +0000414 }else if( pSelect ){
415 sqliteVdbeAddOp(v, OP_Dup, nColumn - keyColumn - 1, 1);
danielk1977c3f9bad2002-05-15 08:30:12 +0000416 }else{
drh9adf9ac2002-05-15 11:44:13 +0000417 sqliteExprCode(pParse, pList->a[keyColumn].pExpr);
danielk1977c3f9bad2002-05-15 08:30:12 +0000418 }
drh27a32782002-06-19 20:32:43 +0000419 /* If the PRIMARY KEY expression is NULL, then use OP_NewRecno
420 ** to generate a unique primary key value.
421 */
422 sqliteVdbeAddOp(v, OP_NotNull, -1, sqliteVdbeCurrentAddr(v)+3);
423 sqliteVdbeAddOp(v, OP_Pop, 1, 0);
424 sqliteVdbeAddOp(v, OP_NewRecno, base, 0);
danielk1977c3f9bad2002-05-15 08:30:12 +0000425 sqliteVdbeAddOp(v, OP_MustBeInt, 0, 0);
426 }else{
drhe1e68f42002-03-31 18:29:03 +0000427 sqliteVdbeAddOp(v, OP_NewRecno, base, 0);
drh4a324312001-12-21 14:30:42 +0000428 }
drh4a324312001-12-21 14:30:42 +0000429
danielk1977c3f9bad2002-05-15 08:30:12 +0000430 /* Push onto the stack, data for all columns of the new entry, beginning
danielk1977f29ce552002-05-19 23:43:12 +0000431 ** with the first column.
432 */
danielk1977c3f9bad2002-05-15 08:30:12 +0000433 for(i=0; i<pTab->nCol; i++){
434 if( i==pTab->iPKey ){
drh9adf9ac2002-05-15 11:44:13 +0000435 /* The value of the INTEGER PRIMARY KEY column is always a NULL.
danielk1977f29ce552002-05-19 23:43:12 +0000436 ** Whenever this column is read, the record number will be substituted
437 ** in its place. So will fill this column with a NULL to avoid
438 ** taking up data space with information that will never be used. */
drh9adf9ac2002-05-15 11:44:13 +0000439 sqliteVdbeAddOp(v, OP_String, 0, 0);
440 continue;
danielk1977c3f9bad2002-05-15 08:30:12 +0000441 }
442 if( pColumn==0 ){
drh9adf9ac2002-05-15 11:44:13 +0000443 j = i;
danielk1977c3f9bad2002-05-15 08:30:12 +0000444 }else{
drh9adf9ac2002-05-15 11:44:13 +0000445 for(j=0; j<pColumn->nId; j++){
446 if( pColumn->a[j].idx==i ) break;
447 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000448 }
449 if( pColumn && j>=pColumn->nId ){
drh9adf9ac2002-05-15 11:44:13 +0000450 sqliteVdbeAddOp(v, OP_String, 0, 0);
451 sqliteVdbeChangeP3(v, -1, pTab->aCol[i].zDflt, P3_STATIC);
drh142e30d2002-08-28 03:00:58 +0000452 }else if( useTempTable ){
drh9adf9ac2002-05-15 11:44:13 +0000453 sqliteVdbeAddOp(v, OP_Column, srcTab, j);
drh142e30d2002-08-28 03:00:58 +0000454 }else if( pSelect ){
455 sqliteVdbeAddOp(v, OP_Dup, i+nColumn-j, 1);
danielk1977c3f9bad2002-05-15 08:30:12 +0000456 }else{
drh9adf9ac2002-05-15 11:44:13 +0000457 sqliteExprCode(pParse, pList->a[j].pExpr);
drh5974a302000-06-07 14:42:26 +0000458 }
drhbed86902000-06-02 13:27:59 +0000459 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000460
461 /* Generate code to check constraints and generate index keys and
danielk1977f29ce552002-05-19 23:43:12 +0000462 ** do the insertion.
463 */
danielk1977c3f9bad2002-05-15 08:30:12 +0000464 sqliteGenerateConstraintChecks(pParse, pTab, base, 0,0,0,onError,endOfLoop);
465 sqliteCompleteInsertion(pParse, pTab, base, 0,0,0);
466
467 /* Update the count of rows that are inserted
danielk1977f29ce552002-05-19 23:43:12 +0000468 */
drh142e30d2002-08-28 03:00:58 +0000469 if( (db->flags & SQLITE_CountRows)!=0 ){
470 sqliteVdbeAddOp(v, OP_MemIncr, iCntMem, 0);
drh5974a302000-06-07 14:42:26 +0000471 }
472 }
drh1ccde152000-06-17 13:12:39 +0000473
danielk1977f29ce552002-05-19 23:43:12 +0000474 if( row_triggers_exist ){
danielk1977c3f9bad2002-05-15 08:30:12 +0000475 /* Close all tables opened */
danielk1977f29ce552002-05-19 23:43:12 +0000476 if( !pTab->pSelect ){
danielk1977c3f9bad2002-05-15 08:30:12 +0000477 sqliteVdbeAddOp(v, OP_Close, base, 0);
478 for(idx=1, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, idx++){
drh9adf9ac2002-05-15 11:44:13 +0000479 sqliteVdbeAddOp(v, OP_Close, idx+base, 0);
danielk1977c3f9bad2002-05-15 08:30:12 +0000480 }
481 }
drh1bee3d72001-10-15 00:44:35 +0000482
danielk1977c3f9bad2002-05-15 08:30:12 +0000483 /* Code AFTER triggers */
danielk1977f29ce552002-05-19 23:43:12 +0000484 if( sqliteCodeRowTrigger(pParse, TK_INSERT, 0, TK_AFTER, pTab, newIdx, -1,
danielk19776f349032002-06-11 02:25:40 +0000485 onError, endOfLoop) ){
danielk1977f29ce552002-05-19 23:43:12 +0000486 goto insert_cleanup;
487 }
drh1bee3d72001-10-15 00:44:35 +0000488 }
489
drh1ccde152000-06-17 13:12:39 +0000490 /* The bottom of the loop, if the data source is a SELECT statement
danielk1977f29ce552002-05-19 23:43:12 +0000491 */
drh0ca3e242002-01-29 23:07:02 +0000492 sqliteVdbeResolveLabel(v, endOfLoop);
drh142e30d2002-08-28 03:00:58 +0000493 if( useTempTable ){
drh6b563442001-11-07 16:48:26 +0000494 sqliteVdbeAddOp(v, OP_Next, srcTab, iCont);
drh99fcd712001-10-13 01:06:47 +0000495 sqliteVdbeResolveLabel(v, iBreak);
drh6b563442001-11-07 16:48:26 +0000496 sqliteVdbeAddOp(v, OP_Close, srcTab, 0);
drh142e30d2002-08-28 03:00:58 +0000497 }else if( pSelect ){
498 sqliteVdbeAddOp(v, OP_Pop, nColumn, 0);
499 sqliteVdbeAddOp(v, OP_Return, 0, 0);
500 sqliteVdbeResolveLabel(v, iCleanup);
drh6b563442001-11-07 16:48:26 +0000501 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000502
danielk1977f29ce552002-05-19 23:43:12 +0000503 if( !row_triggers_exist ){
danielk1977c3f9bad2002-05-15 08:30:12 +0000504 /* Close all tables opened */
505 sqliteVdbeAddOp(v, OP_Close, base, 0);
506 for(idx=1, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, idx++){
507 sqliteVdbeAddOp(v, OP_Close, idx+base, 0);
508 }
drhcce7d172000-05-31 15:34:51 +0000509 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000510
drh1c928532002-01-31 15:54:21 +0000511 sqliteEndWriteOperation(pParse);
drh5e00f6c2001-09-13 13:46:56 +0000512
drh1bee3d72001-10-15 00:44:35 +0000513 /*
danielk1977f29ce552002-05-19 23:43:12 +0000514 ** Return the number of rows inserted.
drh1bee3d72001-10-15 00:44:35 +0000515 */
drh142e30d2002-08-28 03:00:58 +0000516 if( db->flags & SQLITE_CountRows ){
drh1bee3d72001-10-15 00:44:35 +0000517 sqliteVdbeAddOp(v, OP_ColumnName, 0, 0);
518 sqliteVdbeChangeP3(v, -1, "rows inserted", P3_STATIC);
drh142e30d2002-08-28 03:00:58 +0000519 sqliteVdbeAddOp(v, OP_MemLoad, iCntMem, 0);
drh1bee3d72001-10-15 00:44:35 +0000520 sqliteVdbeAddOp(v, OP_Callback, 1, 0);
521 }
drhcce7d172000-05-31 15:34:51 +0000522
523insert_cleanup:
drh5974a302000-06-07 14:42:26 +0000524 if( pList ) sqliteExprListDelete(pList);
525 if( pSelect ) sqliteSelectDelete(pSelect);
danielk1977c3f9bad2002-05-15 08:30:12 +0000526 if ( zTab ) sqliteFree(zTab);
drh967e8b72000-06-21 13:59:10 +0000527 sqliteIdListDelete(pColumn);
drhcce7d172000-05-31 15:34:51 +0000528}
drh9cfcf5d2002-01-29 18:41:24 +0000529
drh9cfcf5d2002-01-29 18:41:24 +0000530/*
531** Generate code to do a constraint check prior to an INSERT or an UPDATE.
532**
533** When this routine is called, the stack contains (from bottom to top)
drh0ca3e242002-01-29 23:07:02 +0000534** the following values:
535**
drhb419a922002-01-30 16:17:23 +0000536** 1. The recno of the row to be updated before it is updated. This
537** value is omitted unless we are doing an UPDATE that involves a
538** change to the record number.
drh0ca3e242002-01-29 23:07:02 +0000539**
drhb419a922002-01-30 16:17:23 +0000540** 2. The recno of the row after the update.
drh0ca3e242002-01-29 23:07:02 +0000541**
542** 3. The data in the first column of the entry after the update.
543**
544** i. Data from middle columns...
545**
546** N. The data in the last column of the entry after the update.
547**
drhb419a922002-01-30 16:17:23 +0000548** The old recno shown as entry (1) above is omitted unless both isUpdate
drh1c928532002-01-31 15:54:21 +0000549** and recnoChng are 1. isUpdate is true for UPDATEs and false for
550** INSERTs and recnoChng is true if the record number is being changed.
drh0ca3e242002-01-29 23:07:02 +0000551**
552** The code generated by this routine pushes additional entries onto
553** the stack which are the keys for new index entries for the new record.
554** The order of index keys is the same as the order of the indices on
555** the pTable->pIndex list. A key is only created for index i if
556** aIdxUsed!=0 and aIdxUsed[i]!=0.
drh9cfcf5d2002-01-29 18:41:24 +0000557**
558** This routine also generates code to check constraints. NOT NULL,
559** CHECK, and UNIQUE constraints are all checked. If a constraint fails,
drh1c928532002-01-31 15:54:21 +0000560** then the appropriate action is performed. There are five possible
561** actions: ROLLBACK, ABORT, FAIL, REPLACE, and IGNORE.
drh9cfcf5d2002-01-29 18:41:24 +0000562**
563** Constraint type Action What Happens
564** --------------- ---------- ----------------------------------------
drh1c928532002-01-31 15:54:21 +0000565** any ROLLBACK The current transaction is rolled back and
drh9cfcf5d2002-01-29 18:41:24 +0000566** sqlite_exec() returns immediately with a
567** return code of SQLITE_CONSTRAINT.
568**
drh1c928532002-01-31 15:54:21 +0000569** any ABORT Back out changes from the current command
570** only (do not do a complete rollback) then
571** cause sqlite_exec() to return immediately
572** with SQLITE_CONSTRAINT.
573**
574** any FAIL Sqlite_exec() returns immediately with a
575** return code of SQLITE_CONSTRAINT. The
576** transaction is not rolled back and any
577** prior changes are retained.
578**
drh9cfcf5d2002-01-29 18:41:24 +0000579** any IGNORE The record number and data is popped from
580** the stack and there is an immediate jump
581** to label ignoreDest.
582**
583** NOT NULL REPLACE The NULL value is replace by the default
584** value for that column. If the default value
585** is NULL, the action is the same as ABORT.
586**
587** UNIQUE REPLACE The other row that conflicts with the row
588** being inserted is removed.
589**
590** CHECK REPLACE Illegal. The results in an exception.
591**
drh1c928532002-01-31 15:54:21 +0000592** Which action to take is determined by the overrideError parameter.
593** Or if overrideError==OE_Default, then the pParse->onError parameter
594** is used. Or if pParse->onError==OE_Default then the onError value
595** for the constraint is used.
drh9cfcf5d2002-01-29 18:41:24 +0000596**
drhaaab5722002-02-19 13:39:21 +0000597** The calling routine must open a read/write cursor for pTab with
drh9cfcf5d2002-01-29 18:41:24 +0000598** cursor number "base". All indices of pTab must also have open
599** read/write cursors with cursor number base+i for the i-th cursor.
600** Except, if there is no possibility of a REPLACE action then
601** cursors do not need to be open for indices where aIdxUsed[i]==0.
602**
603** If the isUpdate flag is true, it means that the "base" cursor is
604** initially pointing to an entry that is being updated. The isUpdate
605** flag causes extra code to be generated so that the "base" cursor
606** is still pointing at the same entry after the routine returns.
607** Without the isUpdate flag, the "base" cursor might be moved.
608*/
609void sqliteGenerateConstraintChecks(
610 Parse *pParse, /* The parser context */
611 Table *pTab, /* the table into which we are inserting */
612 int base, /* Index of a read/write cursor pointing at pTab */
613 char *aIdxUsed, /* Which indices are used. NULL means all are used */
drh0ca3e242002-01-29 23:07:02 +0000614 int recnoChng, /* True if the record number will change */
drhb419a922002-01-30 16:17:23 +0000615 int isUpdate, /* True for UPDATE, False for INSERT */
drh9cfcf5d2002-01-29 18:41:24 +0000616 int overrideError, /* Override onError to this if not OE_Default */
drhb419a922002-01-30 16:17:23 +0000617 int ignoreDest /* Jump to this label on an OE_Ignore resolution */
drh9cfcf5d2002-01-29 18:41:24 +0000618){
619 int i;
620 Vdbe *v;
621 int nCol;
622 int onError;
623 int addr;
624 int extra;
drh0ca3e242002-01-29 23:07:02 +0000625 int iCur;
626 Index *pIdx;
627 int seenReplace = 0;
drhf5905aa2002-05-26 20:54:33 +0000628 int jumpInst1, jumpInst2;
drh0ca3e242002-01-29 23:07:02 +0000629 int contAddr;
drhb419a922002-01-30 16:17:23 +0000630 int hasTwoRecnos = (isUpdate && recnoChng);
drh9cfcf5d2002-01-29 18:41:24 +0000631
632 v = sqliteGetVdbe(pParse);
633 assert( v!=0 );
drh417be792002-03-03 18:59:40 +0000634 assert( pTab->pSelect==0 ); /* This table is not a VIEW */
drh9cfcf5d2002-01-29 18:41:24 +0000635 nCol = pTab->nCol;
636
637 /* Test all NOT NULL constraints.
638 */
639 for(i=0; i<nCol; i++){
drh0ca3e242002-01-29 23:07:02 +0000640 if( i==pTab->iPKey ){
641 /* Fix me: Make sure the INTEGER PRIMARY KEY is not NULL. */
642 continue;
643 }
drh9cfcf5d2002-01-29 18:41:24 +0000644 onError = pTab->aCol[i].notNull;
drh0ca3e242002-01-29 23:07:02 +0000645 if( onError==OE_None ) continue;
drh9cfcf5d2002-01-29 18:41:24 +0000646 if( overrideError!=OE_Default ){
647 onError = overrideError;
drh1c928532002-01-31 15:54:21 +0000648 }else if( onError==OE_Default ){
drh0d65dc02002-02-03 00:56:09 +0000649 onError = pParse->db->onError;
650 if( onError==OE_Default ) onError = OE_Abort;
drh9cfcf5d2002-01-29 18:41:24 +0000651 }
652 if( onError==OE_Replace && pTab->aCol[i].zDflt==0 ){
653 onError = OE_Abort;
654 }
drhef6764a2002-01-30 04:32:00 +0000655 sqliteVdbeAddOp(v, OP_Dup, nCol-1-i, 1);
drhf5905aa2002-05-26 20:54:33 +0000656 addr = sqliteVdbeAddOp(v, OP_NotNull, 1, 0);
drh9cfcf5d2002-01-29 18:41:24 +0000657 switch( onError ){
drh1c928532002-01-31 15:54:21 +0000658 case OE_Rollback:
659 case OE_Abort:
660 case OE_Fail: {
661 sqliteVdbeAddOp(v, OP_Halt, SQLITE_CONSTRAINT, onError);
drh9cfcf5d2002-01-29 18:41:24 +0000662 break;
663 }
664 case OE_Ignore: {
drhb419a922002-01-30 16:17:23 +0000665 sqliteVdbeAddOp(v, OP_Pop, nCol+1+hasTwoRecnos, 0);
drh0ca3e242002-01-29 23:07:02 +0000666 sqliteVdbeAddOp(v, OP_Goto, 0, ignoreDest);
drh9cfcf5d2002-01-29 18:41:24 +0000667 break;
668 }
669 case OE_Replace: {
670 sqliteVdbeAddOp(v, OP_String, 0, 0);
671 sqliteVdbeChangeP3(v, -1, pTab->aCol[i].zDflt, P3_STATIC);
672 sqliteVdbeAddOp(v, OP_Push, nCol-i, 0);
673 break;
674 }
drh0ca3e242002-01-29 23:07:02 +0000675 default: assert(0);
drh9cfcf5d2002-01-29 18:41:24 +0000676 }
drhef6764a2002-01-30 04:32:00 +0000677 sqliteVdbeChangeP2(v, addr, sqliteVdbeCurrentAddr(v));
drh9cfcf5d2002-01-29 18:41:24 +0000678 }
679
680 /* Test all CHECK constraints
681 */
drh0bd1f4e2002-06-06 18:54:39 +0000682 /**** TBD ****/
drh9cfcf5d2002-01-29 18:41:24 +0000683
drh0bd1f4e2002-06-06 18:54:39 +0000684 /* If we have an INTEGER PRIMARY KEY, make sure the primary key
685 ** of the new record does not previously exist. Except, if this
686 ** is an UPDATE and the primary key is not changing, that is OK.
687 ** Also, if the conflict resolution policy is REPLACE, then we
688 ** can skip this test.
drh9cfcf5d2002-01-29 18:41:24 +0000689 */
drh0d65dc02002-02-03 00:56:09 +0000690 if( (recnoChng || !isUpdate) && pTab->iPKey>=0 ){
drh0ca3e242002-01-29 23:07:02 +0000691 onError = pTab->keyConf;
692 if( overrideError!=OE_Default ){
693 onError = overrideError;
drh1c928532002-01-31 15:54:21 +0000694 }else if( onError==OE_Default ){
drh0d65dc02002-02-03 00:56:09 +0000695 onError = pParse->db->onError;
696 if( onError==OE_Default ) onError = OE_Abort;
drh0ca3e242002-01-29 23:07:02 +0000697 }
drh0d65dc02002-02-03 00:56:09 +0000698 if( onError!=OE_Replace ){
drh79b0c952002-05-21 12:56:43 +0000699 if( isUpdate ){
700 sqliteVdbeAddOp(v, OP_Dup, nCol+1, 1);
701 sqliteVdbeAddOp(v, OP_Dup, nCol+1, 1);
drhf5905aa2002-05-26 20:54:33 +0000702 jumpInst1 = sqliteVdbeAddOp(v, OP_Eq, 0, 0);
drh79b0c952002-05-21 12:56:43 +0000703 }
drh0d65dc02002-02-03 00:56:09 +0000704 sqliteVdbeAddOp(v, OP_Dup, nCol, 1);
drhf5905aa2002-05-26 20:54:33 +0000705 jumpInst2 = sqliteVdbeAddOp(v, OP_NotExists, base, 0);
drh0d65dc02002-02-03 00:56:09 +0000706 switch( onError ){
707 case OE_Rollback:
708 case OE_Abort:
709 case OE_Fail: {
710 sqliteVdbeAddOp(v, OP_Halt, SQLITE_CONSTRAINT, onError);
711 break;
712 }
713 case OE_Ignore: {
714 sqliteVdbeAddOp(v, OP_Pop, nCol+1+hasTwoRecnos, 0);
715 sqliteVdbeAddOp(v, OP_Goto, 0, ignoreDest);
716 break;
717 }
718 default: assert(0);
drh0ca3e242002-01-29 23:07:02 +0000719 }
drh0d65dc02002-02-03 00:56:09 +0000720 contAddr = sqliteVdbeCurrentAddr(v);
drhf5905aa2002-05-26 20:54:33 +0000721 sqliteVdbeChangeP2(v, jumpInst2, contAddr);
drh0d65dc02002-02-03 00:56:09 +0000722 if( isUpdate ){
drhf5905aa2002-05-26 20:54:33 +0000723 sqliteVdbeChangeP2(v, jumpInst1, contAddr);
drh0d65dc02002-02-03 00:56:09 +0000724 sqliteVdbeAddOp(v, OP_Dup, nCol+1, 1);
725 sqliteVdbeAddOp(v, OP_MoveTo, base, 0);
drh0ca3e242002-01-29 23:07:02 +0000726 }
drh0ca3e242002-01-29 23:07:02 +0000727 }
728 }
drh0bd1f4e2002-06-06 18:54:39 +0000729
730 /* Test all UNIQUE constraints by creating entries for each UNIQUE
731 ** index and making sure that duplicate entries do not already exist.
732 ** Add the new records to the indices as we go.
733 */
drh9cfcf5d2002-01-29 18:41:24 +0000734 extra = 0;
735 for(extra=(-1), iCur=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, iCur++){
drh9cfcf5d2002-01-29 18:41:24 +0000736 if( aIdxUsed && aIdxUsed[iCur]==0 ) continue;
737 extra++;
738 sqliteVdbeAddOp(v, OP_Dup, nCol+extra, 1);
739 for(i=0; i<pIdx->nColumn; i++){
740 int idx = pIdx->aiColumn[i];
741 if( idx==pTab->iPKey ){
drh0ca3e242002-01-29 23:07:02 +0000742 sqliteVdbeAddOp(v, OP_Dup, i+extra+nCol+1, 1);
drh9cfcf5d2002-01-29 18:41:24 +0000743 }else{
drh0ca3e242002-01-29 23:07:02 +0000744 sqliteVdbeAddOp(v, OP_Dup, i+extra+nCol-idx, 1);
drh9cfcf5d2002-01-29 18:41:24 +0000745 }
746 }
drhf5905aa2002-05-26 20:54:33 +0000747 jumpInst1 = sqliteVdbeAddOp(v, OP_MakeIdxKey, pIdx->nColumn, 0);
drh491791a2002-07-18 00:34:09 +0000748 if( pParse->db->file_format>=4 ) sqliteAddIdxKeyType(v, pIdx);
drh9cfcf5d2002-01-29 18:41:24 +0000749 onError = pIdx->onError;
750 if( onError==OE_None ) continue;
751 if( overrideError!=OE_Default ){
752 onError = overrideError;
drh1c928532002-01-31 15:54:21 +0000753 }else if( onError==OE_Default ){
drh0d65dc02002-02-03 00:56:09 +0000754 onError = pParse->db->onError;
755 if( onError==OE_Default ) onError = OE_Abort;
drh9cfcf5d2002-01-29 18:41:24 +0000756 }
drhb419a922002-01-30 16:17:23 +0000757 sqliteVdbeAddOp(v, OP_Dup, extra+nCol+1+hasTwoRecnos, 1);
drhf5905aa2002-05-26 20:54:33 +0000758 jumpInst2 = sqliteVdbeAddOp(v, OP_IsUnique, base+iCur+1, 0);
drh9cfcf5d2002-01-29 18:41:24 +0000759 switch( onError ){
drh1c928532002-01-31 15:54:21 +0000760 case OE_Rollback:
761 case OE_Abort:
762 case OE_Fail: {
763 sqliteVdbeAddOp(v, OP_Halt, SQLITE_CONSTRAINT, onError);
drh9cfcf5d2002-01-29 18:41:24 +0000764 break;
765 }
766 case OE_Ignore: {
drh0ca3e242002-01-29 23:07:02 +0000767 assert( seenReplace==0 );
drhfe1a1772002-04-09 03:15:06 +0000768 sqliteVdbeAddOp(v, OP_Pop, nCol+extra+3+hasTwoRecnos, 0);
drh9cfcf5d2002-01-29 18:41:24 +0000769 sqliteVdbeAddOp(v, OP_Goto, 0, ignoreDest);
drh9cfcf5d2002-01-29 18:41:24 +0000770 break;
771 }
772 case OE_Replace: {
drh38640e12002-07-05 21:42:36 +0000773 sqliteGenerateRowDelete(pParse->db, v, pTab, base, 0);
drh9cfcf5d2002-01-29 18:41:24 +0000774 if( isUpdate ){
drhb419a922002-01-30 16:17:23 +0000775 sqliteVdbeAddOp(v, OP_Dup, nCol+extra+1+hasTwoRecnos, 1);
drh0ca3e242002-01-29 23:07:02 +0000776 sqliteVdbeAddOp(v, OP_MoveTo, base, 0);
drh9cfcf5d2002-01-29 18:41:24 +0000777 }
drh0ca3e242002-01-29 23:07:02 +0000778 seenReplace = 1;
drh9cfcf5d2002-01-29 18:41:24 +0000779 break;
780 }
drh0ca3e242002-01-29 23:07:02 +0000781 default: assert(0);
drh9cfcf5d2002-01-29 18:41:24 +0000782 }
783 contAddr = sqliteVdbeCurrentAddr(v);
drh0bd1f4e2002-06-06 18:54:39 +0000784#if NULL_DISTINCT_FOR_UNIQUE
drhf5905aa2002-05-26 20:54:33 +0000785 sqliteVdbeChangeP2(v, jumpInst1, contAddr);
drh0bd1f4e2002-06-06 18:54:39 +0000786#endif
drhf5905aa2002-05-26 20:54:33 +0000787 sqliteVdbeChangeP2(v, jumpInst2, contAddr);
drh9cfcf5d2002-01-29 18:41:24 +0000788 }
789}
drh0ca3e242002-01-29 23:07:02 +0000790
791/*
792** This routine generates code to finish the INSERT or UPDATE operation
793** that was started by a prior call to sqliteGenerateConstraintChecks.
794** The stack must contain keys for all active indices followed by data
795** and the recno for the new entry. This routine creates the new
796** entries in all indices and in the main table.
797**
drhb419a922002-01-30 16:17:23 +0000798** The arguments to this routine should be the same as the first six
drh0ca3e242002-01-29 23:07:02 +0000799** arguments to sqliteGenerateConstraintChecks.
800*/
801void sqliteCompleteInsertion(
802 Parse *pParse, /* The parser context */
803 Table *pTab, /* the table into which we are inserting */
804 int base, /* Index of a read/write cursor pointing at pTab */
805 char *aIdxUsed, /* Which indices are used. NULL means all are used */
drhb419a922002-01-30 16:17:23 +0000806 int recnoChng, /* True if the record number will change */
807 int isUpdate /* True for UPDATE, False for INSERT */
drh0ca3e242002-01-29 23:07:02 +0000808){
809 int i;
810 Vdbe *v;
811 int nIdx;
812 Index *pIdx;
813
814 v = sqliteGetVdbe(pParse);
815 assert( v!=0 );
drh417be792002-03-03 18:59:40 +0000816 assert( pTab->pSelect==0 ); /* This table is not a VIEW */
drh0ca3e242002-01-29 23:07:02 +0000817 for(nIdx=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, nIdx++){}
818 for(i=nIdx-1; i>=0; i--){
819 if( aIdxUsed && aIdxUsed[i]==0 ) continue;
820 sqliteVdbeAddOp(v, OP_IdxPut, base+i+1, 0);
821 }
822 sqliteVdbeAddOp(v, OP_MakeRecord, pTab->nCol, 0);
danielk1977c3f9bad2002-05-15 08:30:12 +0000823 sqliteVdbeAddOp(v, OP_PutIntKey, base, pParse->trigStack?0:1);
drhb419a922002-01-30 16:17:23 +0000824 if( isUpdate && recnoChng ){
drh0ca3e242002-01-29 23:07:02 +0000825 sqliteVdbeAddOp(v, OP_Pop, 1, 0);
826 }
827}