blob: 705bd8e9d20c217b9bd7cc6a75b5630d8563c4dc [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**
drhda93d232003-03-31 02:12:46 +000015** $Id: insert.c,v 1.77 2003/03/31 02:12:47 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 */
drh113088e2003-03-20 01:16:58 +000088 SrcList *pTabList, /* Name of table into which we are inserting */
drhcce7d172000-05-31 15:34:51 +000089 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 */
drh113088e2003-03-20 01:16:58 +000095 char *zTab; /* 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 */
drh4a324312001-12-21 14:30:42 +0000103 int keyColumn = -1; /* Column that is the INTEGER PRIMARY KEY */
drh0ca3e242002-01-29 23:07:02 +0000104 int endOfLoop; /* Label for the end of the insertion loop */
drh142e30d2002-08-28 03:00:58 +0000105 int useTempTable; /* Store SELECT results in intermediate table */
106 int srcTab; /* Data comes from this temporary cursor if >=0 */
107 int iSelectLoop; /* Address of code that implements the SELECT */
108 int iCleanup; /* Address of the cleanup code */
109 int iInsertBlock; /* Address of the subroutine used to insert data */
110 int iCntMem; /* Memory cell used for the row counter */
drhcce7d172000-05-31 15:34:51 +0000111
danielk1977c3f9bad2002-05-15 08:30:12 +0000112 int row_triggers_exist = 0; /* True if there are FOR EACH ROW triggers */
113 int newIdx = -1;
114
drhdaffd0e2001-04-11 14:28:42 +0000115 if( pParse->nErr || sqlite_malloc_failed ) goto insert_cleanup;
drhecdc7532001-09-23 02:35:53 +0000116 db = pParse->db;
drhdaffd0e2001-04-11 14:28:42 +0000117
drh1ccde152000-06-17 13:12:39 +0000118 /* Locate the table into which we will be inserting new information.
119 */
drh113088e2003-03-20 01:16:58 +0000120 assert( pTabList->nSrc==1 );
121 zTab = pTabList->a[0].zName;
drhdaffd0e2001-04-11 14:28:42 +0000122 if( zTab==0 ) goto insert_cleanup;
drh812d7a22003-03-27 13:50:00 +0000123 pTab = sqliteSrcListLookup(pParse, pTabList);
danielk1977c3f9bad2002-05-15 08:30:12 +0000124 if( pTab==0 ){
danielk1977c3f9bad2002-05-15 08:30:12 +0000125 goto insert_cleanup;
126 }
drhe5f9c642003-01-13 23:27:31 +0000127 if( sqliteAuthCheck(pParse, SQLITE_INSERT, pTab->zName, 0) ){
drh1962bda2003-01-12 19:33:52 +0000128 goto insert_cleanup;
129 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000130
131 /* Ensure that:
132 * (a) the table is not read-only,
133 * (b) that if it is a view then ON INSERT triggers exist
134 */
135 row_triggers_exist =
136 sqliteTriggersExist(pParse, pTab->pTrigger, TK_INSERT,
drh9adf9ac2002-05-15 11:44:13 +0000137 TK_BEFORE, TK_ROW, 0) ||
danielk1977c3f9bad2002-05-15 08:30:12 +0000138 sqliteTriggersExist(pParse, pTab->pTrigger, TK_INSERT, TK_AFTER, TK_ROW, 0);
139 if( pTab->readOnly || (pTab->pSelect && !row_triggers_exist) ){
drhda93d232003-03-31 02:12:46 +0000140 sqliteErrorMsg(pParse, "%s %s may not be modified",
141 pTab->pSelect ? "view" : "table",
142 zTab);
danielk1977c3f9bad2002-05-15 08:30:12 +0000143 goto insert_cleanup;
144 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000145
drha76b5df2002-02-23 02:32:10 +0000146 if( pTab==0 ) goto insert_cleanup;
drh1ccde152000-06-17 13:12:39 +0000147
drhf573c992002-07-31 00:32:50 +0000148 /* If pTab is really a view, make sure it has been initialized.
149 */
150 if( pTab->pSelect ){
151 if( sqliteViewGetColumnNames(pParse, pTab) ){
152 goto insert_cleanup;
153 }
154 }
155
drh1ccde152000-06-17 13:12:39 +0000156 /* Allocate a VDBE
157 */
drhd8bc7082000-06-07 23:51:50 +0000158 v = sqliteGetVdbe(pParse);
drh5974a302000-06-07 14:42:26 +0000159 if( v==0 ) goto insert_cleanup;
drhcabb0812002-09-14 13:47:32 +0000160 sqliteBeginWriteOperation(pParse, pSelect || row_triggers_exist,
drhd24cc422003-03-27 12:51:24 +0000161 !row_triggers_exist && pTab->iDb==1);
drh1ccde152000-06-17 13:12:39 +0000162
danielk1977c3f9bad2002-05-15 08:30:12 +0000163 /* if there are row triggers, allocate a temp table for new.* references. */
danielk1977f29ce552002-05-19 23:43:12 +0000164 if( row_triggers_exist ){
danielk1977c3f9bad2002-05-15 08:30:12 +0000165 newIdx = pParse->nTab++;
danielk1977f29ce552002-05-19 23:43:12 +0000166 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000167
drh1ccde152000-06-17 13:12:39 +0000168 /* Figure out how many columns of data are supplied. If the data
drh142e30d2002-08-28 03:00:58 +0000169 ** is coming from a SELECT statement, then this step also generates
170 ** all the code to implement the SELECT statement and invoke a subroutine
171 ** to process each row of the result. (Template 2.) If the SELECT
172 ** statement uses the the table that is being inserted into, then the
173 ** subroutine is also coded here. That subroutine stores the SELECT
174 ** results in a temporary table. (Template 3.)
drh1ccde152000-06-17 13:12:39 +0000175 */
drh5974a302000-06-07 14:42:26 +0000176 if( pSelect ){
drh142e30d2002-08-28 03:00:58 +0000177 /* Data is coming from a SELECT. Generate code to implement that SELECT
178 */
179 int rc, iInitCode;
180 int opCode;
181 iInitCode = sqliteVdbeAddOp(v, OP_Goto, 0, 0);
182 iSelectLoop = sqliteVdbeCurrentAddr(v);
183 iInsertBlock = sqliteVdbeMakeLabel(v);
184 rc = sqliteSelect(pParse, pSelect, SRT_Subroutine, iInsertBlock, 0,0,0);
drhdaffd0e2001-04-11 14:28:42 +0000185 if( rc || pParse->nErr || sqlite_malloc_failed ) goto insert_cleanup;
drh142e30d2002-08-28 03:00:58 +0000186 iCleanup = sqliteVdbeMakeLabel(v);
187 sqliteVdbeAddOp(v, OP_Goto, 0, iCleanup);
drh5974a302000-06-07 14:42:26 +0000188 assert( pSelect->pEList );
drh967e8b72000-06-21 13:59:10 +0000189 nColumn = pSelect->pEList->nExpr;
drh142e30d2002-08-28 03:00:58 +0000190
191 /* Set useTempTable to TRUE if the result of the SELECT statement
192 ** should be written into a temporary table. Set to FALSE if each
193 ** row of the SELECT can be written directly into the result table.
194 */
drhd24cc422003-03-27 12:51:24 +0000195 opCode = pTab->iDb==1 ? OP_OpenTemp : OP_OpenRead;
drh142e30d2002-08-28 03:00:58 +0000196 useTempTable = row_triggers_exist || sqliteVdbeFindOp(v,opCode,pTab->tnum);
197
198 if( useTempTable ){
199 /* Generate the subroutine that SELECT calls to process each row of
200 ** the result. Store the result in a temporary table
201 */
202 srcTab = pParse->nTab++;
203 sqliteVdbeResolveLabel(v, iInsertBlock);
204 sqliteVdbeAddOp(v, OP_MakeRecord, nColumn, 0);
205 sqliteVdbeAddOp(v, OP_NewRecno, srcTab, 0);
206 sqliteVdbeAddOp(v, OP_Pull, 1, 0);
207 sqliteVdbeAddOp(v, OP_PutIntKey, srcTab, 0);
208 sqliteVdbeAddOp(v, OP_Return, 0, 0);
209
210 /* The following code runs first because the GOTO at the very top
211 ** of the program jumps to it. Create the temporary table, then jump
212 ** back up and execute the SELECT code above.
213 */
214 sqliteVdbeChangeP2(v, iInitCode, sqliteVdbeCurrentAddr(v));
215 sqliteVdbeAddOp(v, OP_OpenTemp, srcTab, 0);
216 sqliteVdbeAddOp(v, OP_Goto, 0, iSelectLoop);
217 sqliteVdbeResolveLabel(v, iCleanup);
218 }else{
219 sqliteVdbeChangeP2(v, iInitCode, sqliteVdbeCurrentAddr(v));
220 }
drh5974a302000-06-07 14:42:26 +0000221 }else{
drh142e30d2002-08-28 03:00:58 +0000222 /* This is the case if the data for the INSERT is coming from a VALUES
223 ** clause
224 */
drhad3cab52002-05-24 02:04:32 +0000225 SrcList dummy;
drhdaffd0e2001-04-11 14:28:42 +0000226 assert( pList!=0 );
drh5974a302000-06-07 14:42:26 +0000227 srcTab = -1;
drh142e30d2002-08-28 03:00:58 +0000228 useTempTable = 0;
drh5974a302000-06-07 14:42:26 +0000229 assert( pList );
drh967e8b72000-06-21 13:59:10 +0000230 nColumn = pList->nExpr;
drhad3cab52002-05-24 02:04:32 +0000231 dummy.nSrc = 0;
drhe64e7b22002-02-18 13:56:36 +0000232 for(i=0; i<nColumn; i++){
drh832508b2002-03-02 17:04:07 +0000233 if( sqliteExprResolveIds(pParse, 0, &dummy, 0, pList->a[i].pExpr) ){
drhe64e7b22002-02-18 13:56:36 +0000234 goto insert_cleanup;
235 }
drhb04a5d82002-04-12 03:55:15 +0000236 if( sqliteExprCheck(pParse, pList->a[i].pExpr, 0, 0) ){
237 goto insert_cleanup;
238 }
drhe64e7b22002-02-18 13:56:36 +0000239 }
drh5974a302000-06-07 14:42:26 +0000240 }
drh1ccde152000-06-17 13:12:39 +0000241
242 /* Make sure the number of columns in the source data matches the number
243 ** of columns to be inserted into the table.
244 */
drh967e8b72000-06-21 13:59:10 +0000245 if( pColumn==0 && nColumn!=pTab->nCol ){
drhda93d232003-03-31 02:12:46 +0000246 sqliteErrorMsg(pParse,
247 "table %S has %d columns but %d values were supplied",
248 pTabList, 0, pTab->nCol, nColumn);
drhcce7d172000-05-31 15:34:51 +0000249 goto insert_cleanup;
250 }
drh967e8b72000-06-21 13:59:10 +0000251 if( pColumn!=0 && nColumn!=pColumn->nId ){
drhda93d232003-03-31 02:12:46 +0000252 sqliteErrorMsg(pParse, "%d values for %d columns", nColumn, pColumn->nId);
drhcce7d172000-05-31 15:34:51 +0000253 goto insert_cleanup;
254 }
drh1ccde152000-06-17 13:12:39 +0000255
256 /* If the INSERT statement included an IDLIST term, then make sure
257 ** all elements of the IDLIST really are columns of the table and
258 ** remember the column indices.
drhc8392582001-12-31 02:48:51 +0000259 **
260 ** If the table has an INTEGER PRIMARY KEY column and that column
261 ** is named in the IDLIST, then record in the keyColumn variable
262 ** the index into IDLIST of the primary key column. keyColumn is
263 ** the index of the primary key as it appears in IDLIST, not as
264 ** is appears in the original table. (The index of the primary
265 ** key in the original table is pTab->iPKey.)
drh1ccde152000-06-17 13:12:39 +0000266 */
drh967e8b72000-06-21 13:59:10 +0000267 if( pColumn ){
268 for(i=0; i<pColumn->nId; i++){
269 pColumn->a[i].idx = -1;
drhcce7d172000-05-31 15:34:51 +0000270 }
drh967e8b72000-06-21 13:59:10 +0000271 for(i=0; i<pColumn->nId; i++){
drhcce7d172000-05-31 15:34:51 +0000272 for(j=0; j<pTab->nCol; j++){
drh967e8b72000-06-21 13:59:10 +0000273 if( sqliteStrICmp(pColumn->a[i].zName, pTab->aCol[j].zName)==0 ){
274 pColumn->a[i].idx = j;
drh4a324312001-12-21 14:30:42 +0000275 if( j==pTab->iPKey ){
drh9aa028d2001-12-22 21:48:29 +0000276 keyColumn = i;
drh4a324312001-12-21 14:30:42 +0000277 }
drhcce7d172000-05-31 15:34:51 +0000278 break;
279 }
280 }
281 if( j>=pTab->nCol ){
drhda93d232003-03-31 02:12:46 +0000282 sqliteErrorMsg(pParse, "table %S has no column named %s",
283 pTabList, 0, pColumn->a[i].zName);
drhcce7d172000-05-31 15:34:51 +0000284 pParse->nErr++;
285 goto insert_cleanup;
286 }
287 }
288 }
drh1ccde152000-06-17 13:12:39 +0000289
drhaacc5432002-01-06 17:07:40 +0000290 /* If there is no IDLIST term but the table has an integer primary
drhc8392582001-12-31 02:48:51 +0000291 ** key, the set the keyColumn variable to the primary key column index
292 ** in the original table definition.
drh4a324312001-12-21 14:30:42 +0000293 */
294 if( pColumn==0 ){
295 keyColumn = pTab->iPKey;
296 }
297
drh142e30d2002-08-28 03:00:58 +0000298 /* Open the temp table for FOR EACH ROW triggers
299 */
danielk1977f29ce552002-05-19 23:43:12 +0000300 if( row_triggers_exist ){
danielk1977c3f9bad2002-05-15 08:30:12 +0000301 sqliteVdbeAddOp(v, OP_OpenTemp, newIdx, 0);
danielk1977f29ce552002-05-19 23:43:12 +0000302 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000303
drhfeeb1392002-04-09 03:28:01 +0000304 /* Initialize the count of rows to be inserted
305 */
drh142e30d2002-08-28 03:00:58 +0000306 if( db->flags & SQLITE_CountRows ){
307 iCntMem = pParse->nMem++;
308 sqliteVdbeAddOp(v, OP_Integer, 0, 0);
309 sqliteVdbeAddOp(v, OP_MemStore, iCntMem, 1);
drhfeeb1392002-04-09 03:28:01 +0000310 }
311
danielk1977c3f9bad2002-05-15 08:30:12 +0000312 /* Open tables and indices if there are no row triggers */
danielk1977f29ce552002-05-19 23:43:12 +0000313 if( !row_triggers_exist ){
danielk1977c3f9bad2002-05-15 08:30:12 +0000314 base = pParse->nTab;
drhd24cc422003-03-27 12:51:24 +0000315 sqliteVdbeAddOp(v, OP_Integer, pTab->iDb, 0);
drh001bbcb2003-03-19 03:14:00 +0000316 sqliteVdbeAddOp(v, OP_OpenWrite, base, pTab->tnum);
danielk1977c3f9bad2002-05-15 08:30:12 +0000317 sqliteVdbeChangeP3(v, -1, pTab->zName, P3_STATIC);
318 for(idx=1, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, idx++){
drhd24cc422003-03-27 12:51:24 +0000319 sqliteVdbeAddOp(v, OP_Integer, pIdx->iDb, 0);
drh001bbcb2003-03-19 03:14:00 +0000320 sqliteVdbeAddOp(v, OP_OpenWrite, idx+base, pIdx->tnum);
danielk1977c3f9bad2002-05-15 08:30:12 +0000321 sqliteVdbeChangeP3(v, -1, pIdx->zName, P3_STATIC);
322 }
323 pParse->nTab += idx;
324 }
325
drh142e30d2002-08-28 03:00:58 +0000326 /* If the data source is a temporary table, then we have to create
drh1ccde152000-06-17 13:12:39 +0000327 ** a loop because there might be multiple rows of data. If the data
drh142e30d2002-08-28 03:00:58 +0000328 ** source is a subroutine call from the SELECT statement, then we need
329 ** to launch the SELECT statement processing.
drh1ccde152000-06-17 13:12:39 +0000330 */
drh142e30d2002-08-28 03:00:58 +0000331 if( useTempTable ){
drh5974a302000-06-07 14:42:26 +0000332 iBreak = sqliteVdbeMakeLabel(v);
drh6b563442001-11-07 16:48:26 +0000333 sqliteVdbeAddOp(v, OP_Rewind, srcTab, iBreak);
334 iCont = sqliteVdbeCurrentAddr(v);
drh142e30d2002-08-28 03:00:58 +0000335 }else if( pSelect ){
336 sqliteVdbeAddOp(v, OP_Goto, 0, iSelectLoop);
337 sqliteVdbeResolveLabel(v, iInsertBlock);
drh5974a302000-06-07 14:42:26 +0000338 }
drh1ccde152000-06-17 13:12:39 +0000339
danielk19776f349032002-06-11 02:25:40 +0000340 endOfLoop = sqliteVdbeMakeLabel(v);
danielk1977f29ce552002-05-19 23:43:12 +0000341 if( row_triggers_exist ){
danielk1977c3f9bad2002-05-15 08:30:12 +0000342
343 /* build the new.* reference row */
344 sqliteVdbeAddOp(v, OP_Integer, 13, 0);
345 for(i=0; i<pTab->nCol; i++){
346 if( pColumn==0 ){
drh9adf9ac2002-05-15 11:44:13 +0000347 j = i;
danielk1977c3f9bad2002-05-15 08:30:12 +0000348 }else{
drh9adf9ac2002-05-15 11:44:13 +0000349 for(j=0; j<pColumn->nId; j++){
350 if( pColumn->a[j].idx==i ) break;
351 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000352 }
353 if( pColumn && j>=pColumn->nId ){
drh9adf9ac2002-05-15 11:44:13 +0000354 sqliteVdbeAddOp(v, OP_String, 0, 0);
355 sqliteVdbeChangeP3(v, -1, pTab->aCol[i].zDflt, P3_STATIC);
drh142e30d2002-08-28 03:00:58 +0000356 }else if( useTempTable ){
drh9adf9ac2002-05-15 11:44:13 +0000357 sqliteVdbeAddOp(v, OP_Column, srcTab, j);
drh142e30d2002-08-28 03:00:58 +0000358 }else if( pSelect ){
359 sqliteVdbeAddOp(v, OP_Dup, nColumn-j-1, 1);
danielk1977c3f9bad2002-05-15 08:30:12 +0000360 }else{
drh9adf9ac2002-05-15 11:44:13 +0000361 sqliteExprCode(pParse, pList->a[j].pExpr);
danielk1977c3f9bad2002-05-15 08:30:12 +0000362 }
363 }
364 sqliteVdbeAddOp(v, OP_MakeRecord, pTab->nCol, 0);
365 sqliteVdbeAddOp(v, OP_PutIntKey, newIdx, 0);
366 sqliteVdbeAddOp(v, OP_Rewind, newIdx, 0);
367
368 /* Fire BEFORE triggers */
danielk1977f29ce552002-05-19 23:43:12 +0000369 if( sqliteCodeRowTrigger(pParse, TK_INSERT, 0, TK_BEFORE, pTab, newIdx, -1,
danielk19776f349032002-06-11 02:25:40 +0000370 onError, endOfLoop) ){
danielk1977f29ce552002-05-19 23:43:12 +0000371 goto insert_cleanup;
372 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000373
374 /* Open the tables and indices for the INSERT */
danielk1977f29ce552002-05-19 23:43:12 +0000375 if( !pTab->pSelect ){
danielk1977c3f9bad2002-05-15 08:30:12 +0000376 base = pParse->nTab;
drhd24cc422003-03-27 12:51:24 +0000377 sqliteVdbeAddOp(v, OP_Integer, pTab->iDb, 0);
drh001bbcb2003-03-19 03:14:00 +0000378 sqliteVdbeAddOp(v, OP_OpenWrite, base, pTab->tnum);
danielk1977c3f9bad2002-05-15 08:30:12 +0000379 sqliteVdbeChangeP3(v, -1, pTab->zName, P3_STATIC);
380 for(idx=1, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, idx++){
drhd24cc422003-03-27 12:51:24 +0000381 sqliteVdbeAddOp(v, OP_Integer, pIdx->iDb, 0);
drh001bbcb2003-03-19 03:14:00 +0000382 sqliteVdbeAddOp(v, OP_OpenWrite, idx+base, pIdx->tnum);
drh9adf9ac2002-05-15 11:44:13 +0000383 sqliteVdbeChangeP3(v, -1, pIdx->zName, P3_STATIC);
danielk1977c3f9bad2002-05-15 08:30:12 +0000384 }
385 pParse->nTab += idx;
386 }
387 }
388
drh4a324312001-12-21 14:30:42 +0000389 /* Push the record number for the new entry onto the stack. The
390 ** record number is a randomly generate integer created by NewRecno
391 ** except when the table has an INTEGER PRIMARY KEY column, in which
drhb419a922002-01-30 16:17:23 +0000392 ** case the record number is the same as that column.
drh1ccde152000-06-17 13:12:39 +0000393 */
danielk1977f29ce552002-05-19 23:43:12 +0000394 if( !pTab->pSelect ){
danielk1977c3f9bad2002-05-15 08:30:12 +0000395 if( keyColumn>=0 ){
drh142e30d2002-08-28 03:00:58 +0000396 if( useTempTable ){
drh9adf9ac2002-05-15 11:44:13 +0000397 sqliteVdbeAddOp(v, OP_Column, srcTab, keyColumn);
drh142e30d2002-08-28 03:00:58 +0000398 }else if( pSelect ){
399 sqliteVdbeAddOp(v, OP_Dup, nColumn - keyColumn - 1, 1);
danielk1977c3f9bad2002-05-15 08:30:12 +0000400 }else{
drh9adf9ac2002-05-15 11:44:13 +0000401 sqliteExprCode(pParse, pList->a[keyColumn].pExpr);
danielk1977c3f9bad2002-05-15 08:30:12 +0000402 }
drh27a32782002-06-19 20:32:43 +0000403 /* If the PRIMARY KEY expression is NULL, then use OP_NewRecno
404 ** to generate a unique primary key value.
405 */
406 sqliteVdbeAddOp(v, OP_NotNull, -1, sqliteVdbeCurrentAddr(v)+3);
407 sqliteVdbeAddOp(v, OP_Pop, 1, 0);
408 sqliteVdbeAddOp(v, OP_NewRecno, base, 0);
danielk1977c3f9bad2002-05-15 08:30:12 +0000409 sqliteVdbeAddOp(v, OP_MustBeInt, 0, 0);
410 }else{
drhe1e68f42002-03-31 18:29:03 +0000411 sqliteVdbeAddOp(v, OP_NewRecno, base, 0);
drh4a324312001-12-21 14:30:42 +0000412 }
drh4a324312001-12-21 14:30:42 +0000413
danielk1977c3f9bad2002-05-15 08:30:12 +0000414 /* Push onto the stack, data for all columns of the new entry, beginning
danielk1977f29ce552002-05-19 23:43:12 +0000415 ** with the first column.
416 */
danielk1977c3f9bad2002-05-15 08:30:12 +0000417 for(i=0; i<pTab->nCol; i++){
418 if( i==pTab->iPKey ){
drh9adf9ac2002-05-15 11:44:13 +0000419 /* The value of the INTEGER PRIMARY KEY column is always a NULL.
danielk1977f29ce552002-05-19 23:43:12 +0000420 ** Whenever this column is read, the record number will be substituted
421 ** in its place. So will fill this column with a NULL to avoid
422 ** taking up data space with information that will never be used. */
drh9adf9ac2002-05-15 11:44:13 +0000423 sqliteVdbeAddOp(v, OP_String, 0, 0);
424 continue;
danielk1977c3f9bad2002-05-15 08:30:12 +0000425 }
426 if( pColumn==0 ){
drh9adf9ac2002-05-15 11:44:13 +0000427 j = i;
danielk1977c3f9bad2002-05-15 08:30:12 +0000428 }else{
drh9adf9ac2002-05-15 11:44:13 +0000429 for(j=0; j<pColumn->nId; j++){
430 if( pColumn->a[j].idx==i ) break;
431 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000432 }
433 if( pColumn && j>=pColumn->nId ){
drh9adf9ac2002-05-15 11:44:13 +0000434 sqliteVdbeAddOp(v, OP_String, 0, 0);
435 sqliteVdbeChangeP3(v, -1, pTab->aCol[i].zDflt, P3_STATIC);
drh142e30d2002-08-28 03:00:58 +0000436 }else if( useTempTable ){
drh9adf9ac2002-05-15 11:44:13 +0000437 sqliteVdbeAddOp(v, OP_Column, srcTab, j);
drh142e30d2002-08-28 03:00:58 +0000438 }else if( pSelect ){
439 sqliteVdbeAddOp(v, OP_Dup, i+nColumn-j, 1);
danielk1977c3f9bad2002-05-15 08:30:12 +0000440 }else{
drh9adf9ac2002-05-15 11:44:13 +0000441 sqliteExprCode(pParse, pList->a[j].pExpr);
drh5974a302000-06-07 14:42:26 +0000442 }
drhbed86902000-06-02 13:27:59 +0000443 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000444
445 /* Generate code to check constraints and generate index keys and
danielk1977f29ce552002-05-19 23:43:12 +0000446 ** do the insertion.
447 */
danielk1977c3f9bad2002-05-15 08:30:12 +0000448 sqliteGenerateConstraintChecks(pParse, pTab, base, 0,0,0,onError,endOfLoop);
449 sqliteCompleteInsertion(pParse, pTab, base, 0,0,0);
450
451 /* Update the count of rows that are inserted
danielk1977f29ce552002-05-19 23:43:12 +0000452 */
drh142e30d2002-08-28 03:00:58 +0000453 if( (db->flags & SQLITE_CountRows)!=0 ){
454 sqliteVdbeAddOp(v, OP_MemIncr, iCntMem, 0);
drh5974a302000-06-07 14:42:26 +0000455 }
456 }
drh1ccde152000-06-17 13:12:39 +0000457
danielk1977f29ce552002-05-19 23:43:12 +0000458 if( row_triggers_exist ){
danielk1977c3f9bad2002-05-15 08:30:12 +0000459 /* Close all tables opened */
danielk1977f29ce552002-05-19 23:43:12 +0000460 if( !pTab->pSelect ){
danielk1977c3f9bad2002-05-15 08:30:12 +0000461 sqliteVdbeAddOp(v, OP_Close, base, 0);
462 for(idx=1, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, idx++){
drh9adf9ac2002-05-15 11:44:13 +0000463 sqliteVdbeAddOp(v, OP_Close, idx+base, 0);
danielk1977c3f9bad2002-05-15 08:30:12 +0000464 }
465 }
drh1bee3d72001-10-15 00:44:35 +0000466
danielk1977c3f9bad2002-05-15 08:30:12 +0000467 /* Code AFTER triggers */
danielk1977f29ce552002-05-19 23:43:12 +0000468 if( sqliteCodeRowTrigger(pParse, TK_INSERT, 0, TK_AFTER, pTab, newIdx, -1,
danielk19776f349032002-06-11 02:25:40 +0000469 onError, endOfLoop) ){
danielk1977f29ce552002-05-19 23:43:12 +0000470 goto insert_cleanup;
471 }
drh1bee3d72001-10-15 00:44:35 +0000472 }
473
drh1ccde152000-06-17 13:12:39 +0000474 /* The bottom of the loop, if the data source is a SELECT statement
danielk1977f29ce552002-05-19 23:43:12 +0000475 */
drh0ca3e242002-01-29 23:07:02 +0000476 sqliteVdbeResolveLabel(v, endOfLoop);
drh142e30d2002-08-28 03:00:58 +0000477 if( useTempTable ){
drh6b563442001-11-07 16:48:26 +0000478 sqliteVdbeAddOp(v, OP_Next, srcTab, iCont);
drh99fcd712001-10-13 01:06:47 +0000479 sqliteVdbeResolveLabel(v, iBreak);
drh6b563442001-11-07 16:48:26 +0000480 sqliteVdbeAddOp(v, OP_Close, srcTab, 0);
drh142e30d2002-08-28 03:00:58 +0000481 }else if( pSelect ){
482 sqliteVdbeAddOp(v, OP_Pop, nColumn, 0);
483 sqliteVdbeAddOp(v, OP_Return, 0, 0);
484 sqliteVdbeResolveLabel(v, iCleanup);
drh6b563442001-11-07 16:48:26 +0000485 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000486
danielk1977f29ce552002-05-19 23:43:12 +0000487 if( !row_triggers_exist ){
danielk1977c3f9bad2002-05-15 08:30:12 +0000488 /* Close all tables opened */
489 sqliteVdbeAddOp(v, OP_Close, base, 0);
490 for(idx=1, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, idx++){
491 sqliteVdbeAddOp(v, OP_Close, idx+base, 0);
492 }
drhcce7d172000-05-31 15:34:51 +0000493 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000494
drh1c928532002-01-31 15:54:21 +0000495 sqliteEndWriteOperation(pParse);
drh5e00f6c2001-09-13 13:46:56 +0000496
drh1bee3d72001-10-15 00:44:35 +0000497 /*
danielk1977f29ce552002-05-19 23:43:12 +0000498 ** Return the number of rows inserted.
drh1bee3d72001-10-15 00:44:35 +0000499 */
drh142e30d2002-08-28 03:00:58 +0000500 if( db->flags & SQLITE_CountRows ){
drh1bee3d72001-10-15 00:44:35 +0000501 sqliteVdbeAddOp(v, OP_ColumnName, 0, 0);
502 sqliteVdbeChangeP3(v, -1, "rows inserted", P3_STATIC);
drh142e30d2002-08-28 03:00:58 +0000503 sqliteVdbeAddOp(v, OP_MemLoad, iCntMem, 0);
drh1bee3d72001-10-15 00:44:35 +0000504 sqliteVdbeAddOp(v, OP_Callback, 1, 0);
505 }
drhcce7d172000-05-31 15:34:51 +0000506
507insert_cleanup:
drh113088e2003-03-20 01:16:58 +0000508 sqliteSrcListDelete(pTabList);
drh5974a302000-06-07 14:42:26 +0000509 if( pList ) sqliteExprListDelete(pList);
510 if( pSelect ) sqliteSelectDelete(pSelect);
drh967e8b72000-06-21 13:59:10 +0000511 sqliteIdListDelete(pColumn);
drhcce7d172000-05-31 15:34:51 +0000512}
drh9cfcf5d2002-01-29 18:41:24 +0000513
drh9cfcf5d2002-01-29 18:41:24 +0000514/*
515** Generate code to do a constraint check prior to an INSERT or an UPDATE.
516**
517** When this routine is called, the stack contains (from bottom to top)
drh0ca3e242002-01-29 23:07:02 +0000518** the following values:
519**
drhb419a922002-01-30 16:17:23 +0000520** 1. The recno of the row to be updated before it is updated. This
521** value is omitted unless we are doing an UPDATE that involves a
522** change to the record number.
drh0ca3e242002-01-29 23:07:02 +0000523**
drhb419a922002-01-30 16:17:23 +0000524** 2. The recno of the row after the update.
drh0ca3e242002-01-29 23:07:02 +0000525**
526** 3. The data in the first column of the entry after the update.
527**
528** i. Data from middle columns...
529**
530** N. The data in the last column of the entry after the update.
531**
drhb419a922002-01-30 16:17:23 +0000532** The old recno shown as entry (1) above is omitted unless both isUpdate
drh1c928532002-01-31 15:54:21 +0000533** and recnoChng are 1. isUpdate is true for UPDATEs and false for
534** INSERTs and recnoChng is true if the record number is being changed.
drh0ca3e242002-01-29 23:07:02 +0000535**
536** The code generated by this routine pushes additional entries onto
537** the stack which are the keys for new index entries for the new record.
538** The order of index keys is the same as the order of the indices on
539** the pTable->pIndex list. A key is only created for index i if
540** aIdxUsed!=0 and aIdxUsed[i]!=0.
drh9cfcf5d2002-01-29 18:41:24 +0000541**
542** This routine also generates code to check constraints. NOT NULL,
543** CHECK, and UNIQUE constraints are all checked. If a constraint fails,
drh1c928532002-01-31 15:54:21 +0000544** then the appropriate action is performed. There are five possible
545** actions: ROLLBACK, ABORT, FAIL, REPLACE, and IGNORE.
drh9cfcf5d2002-01-29 18:41:24 +0000546**
547** Constraint type Action What Happens
548** --------------- ---------- ----------------------------------------
drh1c928532002-01-31 15:54:21 +0000549** any ROLLBACK The current transaction is rolled back and
drh9cfcf5d2002-01-29 18:41:24 +0000550** sqlite_exec() returns immediately with a
551** return code of SQLITE_CONSTRAINT.
552**
drh1c928532002-01-31 15:54:21 +0000553** any ABORT Back out changes from the current command
554** only (do not do a complete rollback) then
555** cause sqlite_exec() to return immediately
556** with SQLITE_CONSTRAINT.
557**
558** any FAIL Sqlite_exec() returns immediately with a
559** return code of SQLITE_CONSTRAINT. The
560** transaction is not rolled back and any
561** prior changes are retained.
562**
drh9cfcf5d2002-01-29 18:41:24 +0000563** any IGNORE The record number and data is popped from
564** the stack and there is an immediate jump
565** to label ignoreDest.
566**
567** NOT NULL REPLACE The NULL value is replace by the default
568** value for that column. If the default value
569** is NULL, the action is the same as ABORT.
570**
571** UNIQUE REPLACE The other row that conflicts with the row
572** being inserted is removed.
573**
574** CHECK REPLACE Illegal. The results in an exception.
575**
drh1c928532002-01-31 15:54:21 +0000576** Which action to take is determined by the overrideError parameter.
577** Or if overrideError==OE_Default, then the pParse->onError parameter
578** is used. Or if pParse->onError==OE_Default then the onError value
579** for the constraint is used.
drh9cfcf5d2002-01-29 18:41:24 +0000580**
drhaaab5722002-02-19 13:39:21 +0000581** The calling routine must open a read/write cursor for pTab with
drh9cfcf5d2002-01-29 18:41:24 +0000582** cursor number "base". All indices of pTab must also have open
583** read/write cursors with cursor number base+i for the i-th cursor.
584** Except, if there is no possibility of a REPLACE action then
585** cursors do not need to be open for indices where aIdxUsed[i]==0.
586**
587** If the isUpdate flag is true, it means that the "base" cursor is
588** initially pointing to an entry that is being updated. The isUpdate
589** flag causes extra code to be generated so that the "base" cursor
590** is still pointing at the same entry after the routine returns.
591** Without the isUpdate flag, the "base" cursor might be moved.
592*/
593void sqliteGenerateConstraintChecks(
594 Parse *pParse, /* The parser context */
595 Table *pTab, /* the table into which we are inserting */
596 int base, /* Index of a read/write cursor pointing at pTab */
597 char *aIdxUsed, /* Which indices are used. NULL means all are used */
drh0ca3e242002-01-29 23:07:02 +0000598 int recnoChng, /* True if the record number will change */
drhb419a922002-01-30 16:17:23 +0000599 int isUpdate, /* True for UPDATE, False for INSERT */
drh9cfcf5d2002-01-29 18:41:24 +0000600 int overrideError, /* Override onError to this if not OE_Default */
drhb419a922002-01-30 16:17:23 +0000601 int ignoreDest /* Jump to this label on an OE_Ignore resolution */
drh9cfcf5d2002-01-29 18:41:24 +0000602){
603 int i;
604 Vdbe *v;
605 int nCol;
606 int onError;
607 int addr;
608 int extra;
drh0ca3e242002-01-29 23:07:02 +0000609 int iCur;
610 Index *pIdx;
611 int seenReplace = 0;
drhf5905aa2002-05-26 20:54:33 +0000612 int jumpInst1, jumpInst2;
drh0ca3e242002-01-29 23:07:02 +0000613 int contAddr;
drhb419a922002-01-30 16:17:23 +0000614 int hasTwoRecnos = (isUpdate && recnoChng);
drh9cfcf5d2002-01-29 18:41:24 +0000615
616 v = sqliteGetVdbe(pParse);
617 assert( v!=0 );
drh417be792002-03-03 18:59:40 +0000618 assert( pTab->pSelect==0 ); /* This table is not a VIEW */
drh9cfcf5d2002-01-29 18:41:24 +0000619 nCol = pTab->nCol;
620
621 /* Test all NOT NULL constraints.
622 */
623 for(i=0; i<nCol; i++){
drh0ca3e242002-01-29 23:07:02 +0000624 if( i==pTab->iPKey ){
625 /* Fix me: Make sure the INTEGER PRIMARY KEY is not NULL. */
626 continue;
627 }
drh9cfcf5d2002-01-29 18:41:24 +0000628 onError = pTab->aCol[i].notNull;
drh0ca3e242002-01-29 23:07:02 +0000629 if( onError==OE_None ) continue;
drh9cfcf5d2002-01-29 18:41:24 +0000630 if( overrideError!=OE_Default ){
631 onError = overrideError;
drh1c928532002-01-31 15:54:21 +0000632 }else if( onError==OE_Default ){
drh0d65dc02002-02-03 00:56:09 +0000633 onError = pParse->db->onError;
634 if( onError==OE_Default ) onError = OE_Abort;
drh9cfcf5d2002-01-29 18:41:24 +0000635 }
636 if( onError==OE_Replace && pTab->aCol[i].zDflt==0 ){
637 onError = OE_Abort;
638 }
drhef6764a2002-01-30 04:32:00 +0000639 sqliteVdbeAddOp(v, OP_Dup, nCol-1-i, 1);
drhf5905aa2002-05-26 20:54:33 +0000640 addr = sqliteVdbeAddOp(v, OP_NotNull, 1, 0);
drh9cfcf5d2002-01-29 18:41:24 +0000641 switch( onError ){
drh1c928532002-01-31 15:54:21 +0000642 case OE_Rollback:
643 case OE_Abort:
644 case OE_Fail: {
drh483750b2003-01-29 18:46:51 +0000645 char *zMsg = 0;
drh1c928532002-01-31 15:54:21 +0000646 sqliteVdbeAddOp(v, OP_Halt, SQLITE_CONSTRAINT, onError);
drh483750b2003-01-29 18:46:51 +0000647 sqliteSetString(&zMsg, pTab->zName, ".", pTab->aCol[i].zName,
648 " may not be NULL", 0);
649 sqliteVdbeChangeP3(v, -1, zMsg, P3_DYNAMIC);
drh9cfcf5d2002-01-29 18:41:24 +0000650 break;
651 }
652 case OE_Ignore: {
drhb419a922002-01-30 16:17:23 +0000653 sqliteVdbeAddOp(v, OP_Pop, nCol+1+hasTwoRecnos, 0);
drh0ca3e242002-01-29 23:07:02 +0000654 sqliteVdbeAddOp(v, OP_Goto, 0, ignoreDest);
drh9cfcf5d2002-01-29 18:41:24 +0000655 break;
656 }
657 case OE_Replace: {
658 sqliteVdbeAddOp(v, OP_String, 0, 0);
659 sqliteVdbeChangeP3(v, -1, pTab->aCol[i].zDflt, P3_STATIC);
660 sqliteVdbeAddOp(v, OP_Push, nCol-i, 0);
661 break;
662 }
drh0ca3e242002-01-29 23:07:02 +0000663 default: assert(0);
drh9cfcf5d2002-01-29 18:41:24 +0000664 }
drhef6764a2002-01-30 04:32:00 +0000665 sqliteVdbeChangeP2(v, addr, sqliteVdbeCurrentAddr(v));
drh9cfcf5d2002-01-29 18:41:24 +0000666 }
667
668 /* Test all CHECK constraints
669 */
drh0bd1f4e2002-06-06 18:54:39 +0000670 /**** TBD ****/
drh9cfcf5d2002-01-29 18:41:24 +0000671
drh0bd1f4e2002-06-06 18:54:39 +0000672 /* If we have an INTEGER PRIMARY KEY, make sure the primary key
673 ** of the new record does not previously exist. Except, if this
674 ** is an UPDATE and the primary key is not changing, that is OK.
675 ** Also, if the conflict resolution policy is REPLACE, then we
676 ** can skip this test.
drh9cfcf5d2002-01-29 18:41:24 +0000677 */
drh0d65dc02002-02-03 00:56:09 +0000678 if( (recnoChng || !isUpdate) && pTab->iPKey>=0 ){
drh0ca3e242002-01-29 23:07:02 +0000679 onError = pTab->keyConf;
680 if( overrideError!=OE_Default ){
681 onError = overrideError;
drh1c928532002-01-31 15:54:21 +0000682 }else if( onError==OE_Default ){
drh0d65dc02002-02-03 00:56:09 +0000683 onError = pParse->db->onError;
684 if( onError==OE_Default ) onError = OE_Abort;
drh0ca3e242002-01-29 23:07:02 +0000685 }
drh0d65dc02002-02-03 00:56:09 +0000686 if( onError!=OE_Replace ){
drh79b0c952002-05-21 12:56:43 +0000687 if( isUpdate ){
688 sqliteVdbeAddOp(v, OP_Dup, nCol+1, 1);
689 sqliteVdbeAddOp(v, OP_Dup, nCol+1, 1);
drhf5905aa2002-05-26 20:54:33 +0000690 jumpInst1 = sqliteVdbeAddOp(v, OP_Eq, 0, 0);
drh79b0c952002-05-21 12:56:43 +0000691 }
drh0d65dc02002-02-03 00:56:09 +0000692 sqliteVdbeAddOp(v, OP_Dup, nCol, 1);
drhf5905aa2002-05-26 20:54:33 +0000693 jumpInst2 = sqliteVdbeAddOp(v, OP_NotExists, base, 0);
drh0d65dc02002-02-03 00:56:09 +0000694 switch( onError ){
695 case OE_Rollback:
696 case OE_Abort:
697 case OE_Fail: {
698 sqliteVdbeAddOp(v, OP_Halt, SQLITE_CONSTRAINT, onError);
drh483750b2003-01-29 18:46:51 +0000699 sqliteVdbeChangeP3(v, -1, "PRIMARY KEY must be unique", P3_STATIC);
drh0d65dc02002-02-03 00:56:09 +0000700 break;
701 }
702 case OE_Ignore: {
703 sqliteVdbeAddOp(v, OP_Pop, nCol+1+hasTwoRecnos, 0);
704 sqliteVdbeAddOp(v, OP_Goto, 0, ignoreDest);
705 break;
706 }
707 default: assert(0);
drh0ca3e242002-01-29 23:07:02 +0000708 }
drh0d65dc02002-02-03 00:56:09 +0000709 contAddr = sqliteVdbeCurrentAddr(v);
drhf5905aa2002-05-26 20:54:33 +0000710 sqliteVdbeChangeP2(v, jumpInst2, contAddr);
drh0d65dc02002-02-03 00:56:09 +0000711 if( isUpdate ){
drhf5905aa2002-05-26 20:54:33 +0000712 sqliteVdbeChangeP2(v, jumpInst1, contAddr);
drh0d65dc02002-02-03 00:56:09 +0000713 sqliteVdbeAddOp(v, OP_Dup, nCol+1, 1);
714 sqliteVdbeAddOp(v, OP_MoveTo, base, 0);
drh0ca3e242002-01-29 23:07:02 +0000715 }
drh0ca3e242002-01-29 23:07:02 +0000716 }
717 }
drh0bd1f4e2002-06-06 18:54:39 +0000718
719 /* Test all UNIQUE constraints by creating entries for each UNIQUE
720 ** index and making sure that duplicate entries do not already exist.
721 ** Add the new records to the indices as we go.
722 */
drh9cfcf5d2002-01-29 18:41:24 +0000723 extra = 0;
724 for(extra=(-1), iCur=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, iCur++){
drh9cfcf5d2002-01-29 18:41:24 +0000725 if( aIdxUsed && aIdxUsed[iCur]==0 ) continue;
726 extra++;
727 sqliteVdbeAddOp(v, OP_Dup, nCol+extra, 1);
728 for(i=0; i<pIdx->nColumn; i++){
729 int idx = pIdx->aiColumn[i];
730 if( idx==pTab->iPKey ){
drh0ca3e242002-01-29 23:07:02 +0000731 sqliteVdbeAddOp(v, OP_Dup, i+extra+nCol+1, 1);
drh9cfcf5d2002-01-29 18:41:24 +0000732 }else{
drh0ca3e242002-01-29 23:07:02 +0000733 sqliteVdbeAddOp(v, OP_Dup, i+extra+nCol-idx, 1);
drh9cfcf5d2002-01-29 18:41:24 +0000734 }
735 }
drhf5905aa2002-05-26 20:54:33 +0000736 jumpInst1 = sqliteVdbeAddOp(v, OP_MakeIdxKey, pIdx->nColumn, 0);
drh491791a2002-07-18 00:34:09 +0000737 if( pParse->db->file_format>=4 ) sqliteAddIdxKeyType(v, pIdx);
drh9cfcf5d2002-01-29 18:41:24 +0000738 onError = pIdx->onError;
739 if( onError==OE_None ) continue;
740 if( overrideError!=OE_Default ){
741 onError = overrideError;
drh1c928532002-01-31 15:54:21 +0000742 }else if( onError==OE_Default ){
drh0d65dc02002-02-03 00:56:09 +0000743 onError = pParse->db->onError;
744 if( onError==OE_Default ) onError = OE_Abort;
drh9cfcf5d2002-01-29 18:41:24 +0000745 }
drhb419a922002-01-30 16:17:23 +0000746 sqliteVdbeAddOp(v, OP_Dup, extra+nCol+1+hasTwoRecnos, 1);
drhf5905aa2002-05-26 20:54:33 +0000747 jumpInst2 = sqliteVdbeAddOp(v, OP_IsUnique, base+iCur+1, 0);
drh9cfcf5d2002-01-29 18:41:24 +0000748 switch( onError ){
drh1c928532002-01-31 15:54:21 +0000749 case OE_Rollback:
750 case OE_Abort:
751 case OE_Fail: {
752 sqliteVdbeAddOp(v, OP_Halt, SQLITE_CONSTRAINT, onError);
drh483750b2003-01-29 18:46:51 +0000753 sqliteVdbeChangeP3(v, -1, "uniqueness constraint failed", P3_STATIC);
drh9cfcf5d2002-01-29 18:41:24 +0000754 break;
755 }
756 case OE_Ignore: {
drh0ca3e242002-01-29 23:07:02 +0000757 assert( seenReplace==0 );
drhfe1a1772002-04-09 03:15:06 +0000758 sqliteVdbeAddOp(v, OP_Pop, nCol+extra+3+hasTwoRecnos, 0);
drh9cfcf5d2002-01-29 18:41:24 +0000759 sqliteVdbeAddOp(v, OP_Goto, 0, ignoreDest);
drh9cfcf5d2002-01-29 18:41:24 +0000760 break;
761 }
762 case OE_Replace: {
drh38640e12002-07-05 21:42:36 +0000763 sqliteGenerateRowDelete(pParse->db, v, pTab, base, 0);
drh9cfcf5d2002-01-29 18:41:24 +0000764 if( isUpdate ){
drhb419a922002-01-30 16:17:23 +0000765 sqliteVdbeAddOp(v, OP_Dup, nCol+extra+1+hasTwoRecnos, 1);
drh0ca3e242002-01-29 23:07:02 +0000766 sqliteVdbeAddOp(v, OP_MoveTo, base, 0);
drh9cfcf5d2002-01-29 18:41:24 +0000767 }
drh0ca3e242002-01-29 23:07:02 +0000768 seenReplace = 1;
drh9cfcf5d2002-01-29 18:41:24 +0000769 break;
770 }
drh0ca3e242002-01-29 23:07:02 +0000771 default: assert(0);
drh9cfcf5d2002-01-29 18:41:24 +0000772 }
773 contAddr = sqliteVdbeCurrentAddr(v);
drh0bd1f4e2002-06-06 18:54:39 +0000774#if NULL_DISTINCT_FOR_UNIQUE
drhf5905aa2002-05-26 20:54:33 +0000775 sqliteVdbeChangeP2(v, jumpInst1, contAddr);
drh0bd1f4e2002-06-06 18:54:39 +0000776#endif
drhf5905aa2002-05-26 20:54:33 +0000777 sqliteVdbeChangeP2(v, jumpInst2, contAddr);
drh9cfcf5d2002-01-29 18:41:24 +0000778 }
779}
drh0ca3e242002-01-29 23:07:02 +0000780
781/*
782** This routine generates code to finish the INSERT or UPDATE operation
783** that was started by a prior call to sqliteGenerateConstraintChecks.
784** The stack must contain keys for all active indices followed by data
785** and the recno for the new entry. This routine creates the new
786** entries in all indices and in the main table.
787**
drhb419a922002-01-30 16:17:23 +0000788** The arguments to this routine should be the same as the first six
drh0ca3e242002-01-29 23:07:02 +0000789** arguments to sqliteGenerateConstraintChecks.
790*/
791void sqliteCompleteInsertion(
792 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 */
drhb419a922002-01-30 16:17:23 +0000796 int recnoChng, /* True if the record number will change */
797 int isUpdate /* True for UPDATE, False for INSERT */
drh0ca3e242002-01-29 23:07:02 +0000798){
799 int i;
800 Vdbe *v;
801 int nIdx;
802 Index *pIdx;
803
804 v = sqliteGetVdbe(pParse);
805 assert( v!=0 );
drh417be792002-03-03 18:59:40 +0000806 assert( pTab->pSelect==0 ); /* This table is not a VIEW */
drh0ca3e242002-01-29 23:07:02 +0000807 for(nIdx=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, nIdx++){}
808 for(i=nIdx-1; i>=0; i--){
809 if( aIdxUsed && aIdxUsed[i]==0 ) continue;
810 sqliteVdbeAddOp(v, OP_IdxPut, base+i+1, 0);
811 }
812 sqliteVdbeAddOp(v, OP_MakeRecord, pTab->nCol, 0);
danielk1977c3f9bad2002-05-15 08:30:12 +0000813 sqliteVdbeAddOp(v, OP_PutIntKey, base, pParse->trigStack?0:1);
drhb419a922002-01-30 16:17:23 +0000814 if( isUpdate && recnoChng ){
drh0ca3e242002-01-29 23:07:02 +0000815 sqliteVdbeAddOp(v, OP_Pop, 1, 0);
816 }
817}