blob: 940536f56ff3814b08fa747d60f25ad3ebd8ed86 [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**
drhe22a3342003-04-22 20:30:37 +000015** $Id: insert.c,v 1.81 2003/04/22 20:30:39 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 */
drhe22a3342003-04-22 20:30:37 +000096 const char *zDb; /* Name of the database holding this table */
drh5974a302000-06-07 14:42:26 +000097 int i, j, idx; /* Loop counters */
98 Vdbe *v; /* Generate code into this virtual machine */
99 Index *pIdx; /* For looping over indices of the table */
drh967e8b72000-06-21 13:59:10 +0000100 int nColumn; /* Number of columns in the data */
drh5974a302000-06-07 14:42:26 +0000101 int base; /* First available cursor */
102 int iCont, iBreak; /* Beginning and end of the loop over srcTab */
drhecdc7532001-09-23 02:35:53 +0000103 sqlite *db; /* The main database structure */
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 */
drh70ce3f02003-04-15 19:22:22 +0000114 int before_triggers; /* True if there are BEFORE triggers */
115 int after_triggers; /* True if there are AFTER triggers */
116 int newIdx = -1; /* Cursor for the NEW table */
danielk1977c3f9bad2002-05-15 08:30:12 +0000117
drhdaffd0e2001-04-11 14:28:42 +0000118 if( pParse->nErr || sqlite_malloc_failed ) goto insert_cleanup;
drhecdc7532001-09-23 02:35:53 +0000119 db = pParse->db;
drhdaffd0e2001-04-11 14:28:42 +0000120
drh1ccde152000-06-17 13:12:39 +0000121 /* Locate the table into which we will be inserting new information.
122 */
drh113088e2003-03-20 01:16:58 +0000123 assert( pTabList->nSrc==1 );
124 zTab = pTabList->a[0].zName;
drhdaffd0e2001-04-11 14:28:42 +0000125 if( zTab==0 ) goto insert_cleanup;
drh812d7a22003-03-27 13:50:00 +0000126 pTab = sqliteSrcListLookup(pParse, pTabList);
danielk1977c3f9bad2002-05-15 08:30:12 +0000127 if( pTab==0 ){
danielk1977c3f9bad2002-05-15 08:30:12 +0000128 goto insert_cleanup;
129 }
drhe22a3342003-04-22 20:30:37 +0000130 assert( pTab->iDb<db->nDb );
131 zDb = db->aDb[pTab->iDb].zName;
132 if( sqliteAuthCheck(pParse, SQLITE_INSERT, pTab->zName, 0, zDb) ){
drh1962bda2003-01-12 19:33:52 +0000133 goto insert_cleanup;
134 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000135
136 /* Ensure that:
137 * (a) the table is not read-only,
138 * (b) that if it is a view then ON INSERT triggers exist
139 */
drh70ce3f02003-04-15 19:22:22 +0000140 before_triggers = sqliteTriggersExist(pParse, pTab->pTrigger, TK_INSERT,
141 TK_BEFORE, TK_ROW, 0);
142 after_triggers = sqliteTriggersExist(pParse, pTab->pTrigger, TK_INSERT,
143 TK_AFTER, TK_ROW, 0);
144 row_triggers_exist = before_triggers || after_triggers;
danielk1977c3f9bad2002-05-15 08:30:12 +0000145 if( pTab->readOnly || (pTab->pSelect && !row_triggers_exist) ){
drhda93d232003-03-31 02:12:46 +0000146 sqliteErrorMsg(pParse, "%s %s may not be modified",
147 pTab->pSelect ? "view" : "table",
148 zTab);
danielk1977c3f9bad2002-05-15 08:30:12 +0000149 goto insert_cleanup;
150 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000151
drha76b5df2002-02-23 02:32:10 +0000152 if( pTab==0 ) goto insert_cleanup;
drh1ccde152000-06-17 13:12:39 +0000153
drhf573c992002-07-31 00:32:50 +0000154 /* If pTab is really a view, make sure it has been initialized.
155 */
156 if( pTab->pSelect ){
157 if( sqliteViewGetColumnNames(pParse, pTab) ){
158 goto insert_cleanup;
159 }
160 }
161
drh1ccde152000-06-17 13:12:39 +0000162 /* Allocate a VDBE
163 */
drhd8bc7082000-06-07 23:51:50 +0000164 v = sqliteGetVdbe(pParse);
drh5974a302000-06-07 14:42:26 +0000165 if( v==0 ) goto insert_cleanup;
drhcabb0812002-09-14 13:47:32 +0000166 sqliteBeginWriteOperation(pParse, pSelect || row_triggers_exist,
drhd24cc422003-03-27 12:51:24 +0000167 !row_triggers_exist && pTab->iDb==1);
drh1ccde152000-06-17 13:12:39 +0000168
danielk1977c3f9bad2002-05-15 08:30:12 +0000169 /* if there are row triggers, allocate a temp table for new.* references. */
danielk1977f29ce552002-05-19 23:43:12 +0000170 if( row_triggers_exist ){
danielk1977c3f9bad2002-05-15 08:30:12 +0000171 newIdx = pParse->nTab++;
danielk1977f29ce552002-05-19 23:43:12 +0000172 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000173
drh1ccde152000-06-17 13:12:39 +0000174 /* Figure out how many columns of data are supplied. If the data
drh142e30d2002-08-28 03:00:58 +0000175 ** is coming from a SELECT statement, then this step also generates
176 ** all the code to implement the SELECT statement and invoke a subroutine
177 ** to process each row of the result. (Template 2.) If the SELECT
178 ** statement uses the the table that is being inserted into, then the
179 ** subroutine is also coded here. That subroutine stores the SELECT
180 ** results in a temporary table. (Template 3.)
drh1ccde152000-06-17 13:12:39 +0000181 */
drh5974a302000-06-07 14:42:26 +0000182 if( pSelect ){
drh142e30d2002-08-28 03:00:58 +0000183 /* Data is coming from a SELECT. Generate code to implement that SELECT
184 */
185 int rc, iInitCode;
drh142e30d2002-08-28 03:00:58 +0000186 iInitCode = sqliteVdbeAddOp(v, OP_Goto, 0, 0);
187 iSelectLoop = sqliteVdbeCurrentAddr(v);
188 iInsertBlock = sqliteVdbeMakeLabel(v);
189 rc = sqliteSelect(pParse, pSelect, SRT_Subroutine, iInsertBlock, 0,0,0);
drhdaffd0e2001-04-11 14:28:42 +0000190 if( rc || pParse->nErr || sqlite_malloc_failed ) goto insert_cleanup;
drh142e30d2002-08-28 03:00:58 +0000191 iCleanup = sqliteVdbeMakeLabel(v);
192 sqliteVdbeAddOp(v, OP_Goto, 0, iCleanup);
drh5974a302000-06-07 14:42:26 +0000193 assert( pSelect->pEList );
drh967e8b72000-06-21 13:59:10 +0000194 nColumn = pSelect->pEList->nExpr;
drh142e30d2002-08-28 03:00:58 +0000195
196 /* Set useTempTable to TRUE if the result of the SELECT statement
197 ** should be written into a temporary table. Set to FALSE if each
198 ** row of the SELECT can be written directly into the result table.
drh048c5302003-04-03 01:50:44 +0000199 **
200 ** A temp table must be used if the table being updated is also one
201 ** of the tables being read by the SELECT statement. Also use a
202 ** temp table in the case of row triggers.
drh142e30d2002-08-28 03:00:58 +0000203 */
drh048c5302003-04-03 01:50:44 +0000204 if( row_triggers_exist ){
205 useTempTable = 1;
206 }else{
207 int addr = sqliteVdbeFindOp(v, OP_OpenRead, pTab->tnum);
208 useTempTable = 0;
209 if( addr>0 ){
210 VdbeOp *pOp = sqliteVdbeGetOp(v, addr-2);
211 if( pOp->opcode==OP_Integer && pOp->p1==pTab->iDb ){
212 useTempTable = 1;
213 }
214 }
215 }
drh142e30d2002-08-28 03:00:58 +0000216
217 if( useTempTable ){
218 /* Generate the subroutine that SELECT calls to process each row of
219 ** the result. Store the result in a temporary table
220 */
221 srcTab = pParse->nTab++;
222 sqliteVdbeResolveLabel(v, iInsertBlock);
223 sqliteVdbeAddOp(v, OP_MakeRecord, nColumn, 0);
224 sqliteVdbeAddOp(v, OP_NewRecno, srcTab, 0);
225 sqliteVdbeAddOp(v, OP_Pull, 1, 0);
226 sqliteVdbeAddOp(v, OP_PutIntKey, srcTab, 0);
227 sqliteVdbeAddOp(v, OP_Return, 0, 0);
228
229 /* The following code runs first because the GOTO at the very top
230 ** of the program jumps to it. Create the temporary table, then jump
231 ** back up and execute the SELECT code above.
232 */
233 sqliteVdbeChangeP2(v, iInitCode, sqliteVdbeCurrentAddr(v));
234 sqliteVdbeAddOp(v, OP_OpenTemp, srcTab, 0);
235 sqliteVdbeAddOp(v, OP_Goto, 0, iSelectLoop);
236 sqliteVdbeResolveLabel(v, iCleanup);
237 }else{
238 sqliteVdbeChangeP2(v, iInitCode, sqliteVdbeCurrentAddr(v));
239 }
drh5974a302000-06-07 14:42:26 +0000240 }else{
drh142e30d2002-08-28 03:00:58 +0000241 /* This is the case if the data for the INSERT is coming from a VALUES
242 ** clause
243 */
drhad3cab52002-05-24 02:04:32 +0000244 SrcList dummy;
drhdaffd0e2001-04-11 14:28:42 +0000245 assert( pList!=0 );
drh5974a302000-06-07 14:42:26 +0000246 srcTab = -1;
drh142e30d2002-08-28 03:00:58 +0000247 useTempTable = 0;
drh5974a302000-06-07 14:42:26 +0000248 assert( pList );
drh967e8b72000-06-21 13:59:10 +0000249 nColumn = pList->nExpr;
drhad3cab52002-05-24 02:04:32 +0000250 dummy.nSrc = 0;
drhe64e7b22002-02-18 13:56:36 +0000251 for(i=0; i<nColumn; i++){
drh832508b2002-03-02 17:04:07 +0000252 if( sqliteExprResolveIds(pParse, 0, &dummy, 0, pList->a[i].pExpr) ){
drhe64e7b22002-02-18 13:56:36 +0000253 goto insert_cleanup;
254 }
drhb04a5d82002-04-12 03:55:15 +0000255 if( sqliteExprCheck(pParse, pList->a[i].pExpr, 0, 0) ){
256 goto insert_cleanup;
257 }
drhe64e7b22002-02-18 13:56:36 +0000258 }
drh5974a302000-06-07 14:42:26 +0000259 }
drh1ccde152000-06-17 13:12:39 +0000260
261 /* Make sure the number of columns in the source data matches the number
262 ** of columns to be inserted into the table.
263 */
drh967e8b72000-06-21 13:59:10 +0000264 if( pColumn==0 && nColumn!=pTab->nCol ){
drhda93d232003-03-31 02:12:46 +0000265 sqliteErrorMsg(pParse,
266 "table %S has %d columns but %d values were supplied",
267 pTabList, 0, pTab->nCol, nColumn);
drhcce7d172000-05-31 15:34:51 +0000268 goto insert_cleanup;
269 }
drh967e8b72000-06-21 13:59:10 +0000270 if( pColumn!=0 && nColumn!=pColumn->nId ){
drhda93d232003-03-31 02:12:46 +0000271 sqliteErrorMsg(pParse, "%d values for %d columns", nColumn, pColumn->nId);
drhcce7d172000-05-31 15:34:51 +0000272 goto insert_cleanup;
273 }
drh1ccde152000-06-17 13:12:39 +0000274
275 /* If the INSERT statement included an IDLIST term, then make sure
276 ** all elements of the IDLIST really are columns of the table and
277 ** remember the column indices.
drhc8392582001-12-31 02:48:51 +0000278 **
279 ** If the table has an INTEGER PRIMARY KEY column and that column
280 ** is named in the IDLIST, then record in the keyColumn variable
281 ** the index into IDLIST of the primary key column. keyColumn is
282 ** the index of the primary key as it appears in IDLIST, not as
283 ** is appears in the original table. (The index of the primary
284 ** key in the original table is pTab->iPKey.)
drh1ccde152000-06-17 13:12:39 +0000285 */
drh967e8b72000-06-21 13:59:10 +0000286 if( pColumn ){
287 for(i=0; i<pColumn->nId; i++){
288 pColumn->a[i].idx = -1;
drhcce7d172000-05-31 15:34:51 +0000289 }
drh967e8b72000-06-21 13:59:10 +0000290 for(i=0; i<pColumn->nId; i++){
drhcce7d172000-05-31 15:34:51 +0000291 for(j=0; j<pTab->nCol; j++){
drh967e8b72000-06-21 13:59:10 +0000292 if( sqliteStrICmp(pColumn->a[i].zName, pTab->aCol[j].zName)==0 ){
293 pColumn->a[i].idx = j;
drh4a324312001-12-21 14:30:42 +0000294 if( j==pTab->iPKey ){
drh9aa028d2001-12-22 21:48:29 +0000295 keyColumn = i;
drh4a324312001-12-21 14:30:42 +0000296 }
drhcce7d172000-05-31 15:34:51 +0000297 break;
298 }
299 }
300 if( j>=pTab->nCol ){
drhda93d232003-03-31 02:12:46 +0000301 sqliteErrorMsg(pParse, "table %S has no column named %s",
302 pTabList, 0, pColumn->a[i].zName);
drhcce7d172000-05-31 15:34:51 +0000303 pParse->nErr++;
304 goto insert_cleanup;
305 }
306 }
307 }
drh1ccde152000-06-17 13:12:39 +0000308
drhaacc5432002-01-06 17:07:40 +0000309 /* If there is no IDLIST term but the table has an integer primary
drhc8392582001-12-31 02:48:51 +0000310 ** key, the set the keyColumn variable to the primary key column index
311 ** in the original table definition.
drh4a324312001-12-21 14:30:42 +0000312 */
313 if( pColumn==0 ){
314 keyColumn = pTab->iPKey;
315 }
316
drh142e30d2002-08-28 03:00:58 +0000317 /* Open the temp table for FOR EACH ROW triggers
318 */
danielk1977f29ce552002-05-19 23:43:12 +0000319 if( row_triggers_exist ){
drh70ce3f02003-04-15 19:22:22 +0000320 sqliteVdbeAddOp(v, OP_OpenPseudo, newIdx, 0);
danielk1977f29ce552002-05-19 23:43:12 +0000321 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000322
drhfeeb1392002-04-09 03:28:01 +0000323 /* Initialize the count of rows to be inserted
324 */
drh142e30d2002-08-28 03:00:58 +0000325 if( db->flags & SQLITE_CountRows ){
326 iCntMem = pParse->nMem++;
327 sqliteVdbeAddOp(v, OP_Integer, 0, 0);
328 sqliteVdbeAddOp(v, OP_MemStore, iCntMem, 1);
drhfeeb1392002-04-09 03:28:01 +0000329 }
330
danielk1977c3f9bad2002-05-15 08:30:12 +0000331 /* Open tables and indices if there are no row triggers */
danielk1977f29ce552002-05-19 23:43:12 +0000332 if( !row_triggers_exist ){
danielk1977c3f9bad2002-05-15 08:30:12 +0000333 base = pParse->nTab;
drhd24cc422003-03-27 12:51:24 +0000334 sqliteVdbeAddOp(v, OP_Integer, pTab->iDb, 0);
drh001bbcb2003-03-19 03:14:00 +0000335 sqliteVdbeAddOp(v, OP_OpenWrite, base, pTab->tnum);
danielk1977c3f9bad2002-05-15 08:30:12 +0000336 sqliteVdbeChangeP3(v, -1, pTab->zName, P3_STATIC);
337 for(idx=1, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, idx++){
drhd24cc422003-03-27 12:51:24 +0000338 sqliteVdbeAddOp(v, OP_Integer, pIdx->iDb, 0);
drh001bbcb2003-03-19 03:14:00 +0000339 sqliteVdbeAddOp(v, OP_OpenWrite, idx+base, pIdx->tnum);
danielk1977c3f9bad2002-05-15 08:30:12 +0000340 sqliteVdbeChangeP3(v, -1, pIdx->zName, P3_STATIC);
341 }
342 pParse->nTab += idx;
343 }
344
drh142e30d2002-08-28 03:00:58 +0000345 /* If the data source is a temporary table, then we have to create
drh1ccde152000-06-17 13:12:39 +0000346 ** a loop because there might be multiple rows of data. If the data
drh142e30d2002-08-28 03:00:58 +0000347 ** source is a subroutine call from the SELECT statement, then we need
348 ** to launch the SELECT statement processing.
drh1ccde152000-06-17 13:12:39 +0000349 */
drh142e30d2002-08-28 03:00:58 +0000350 if( useTempTable ){
drh5974a302000-06-07 14:42:26 +0000351 iBreak = sqliteVdbeMakeLabel(v);
drh6b563442001-11-07 16:48:26 +0000352 sqliteVdbeAddOp(v, OP_Rewind, srcTab, iBreak);
353 iCont = sqliteVdbeCurrentAddr(v);
drh142e30d2002-08-28 03:00:58 +0000354 }else if( pSelect ){
355 sqliteVdbeAddOp(v, OP_Goto, 0, iSelectLoop);
356 sqliteVdbeResolveLabel(v, iInsertBlock);
drh5974a302000-06-07 14:42:26 +0000357 }
drh1ccde152000-06-17 13:12:39 +0000358
drh70ce3f02003-04-15 19:22:22 +0000359 /* Run the BEFORE triggers, if there are any
360 */
danielk19776f349032002-06-11 02:25:40 +0000361 endOfLoop = sqliteVdbeMakeLabel(v);
drh70ce3f02003-04-15 19:22:22 +0000362 if( before_triggers ){
danielk1977c3f9bad2002-05-15 08:30:12 +0000363
drh70ce3f02003-04-15 19:22:22 +0000364 /* build the NEW.* reference row. Note that if there is an INTEGER
365 ** PRIMARY KEY into which a NULL is being inserted, that NULL will be
366 ** translated into a unique ID for the row. But on a BEFORE trigger,
367 ** we do not know what the unique ID will be (because the insert has
368 ** not happened yet) so we substitute a rowid of -1
369 */
370 if( keyColumn<0 ){
371 sqliteVdbeAddOp(v, OP_Integer, -1, 0);
372 }else if( useTempTable ){
373 sqliteVdbeAddOp(v, OP_Column, srcTab, keyColumn);
374 }else if( pSelect ){
375 sqliteVdbeAddOp(v, OP_Dup, nColumn - keyColumn - 1, 1);
376 }else{
377 sqliteExprCode(pParse, pList->a[keyColumn].pExpr);
378 sqliteVdbeAddOp(v, OP_NotNull, -1, sqliteVdbeCurrentAddr(v)+3);
379 sqliteVdbeAddOp(v, OP_Pop, 1, 0);
380 sqliteVdbeAddOp(v, OP_Integer, -1, 0);
381 sqliteVdbeAddOp(v, OP_MustBeInt, 0, 0);
382 }
383
384 /* Create the new column data
385 */
danielk1977c3f9bad2002-05-15 08:30:12 +0000386 for(i=0; i<pTab->nCol; i++){
387 if( pColumn==0 ){
drh9adf9ac2002-05-15 11:44:13 +0000388 j = i;
danielk1977c3f9bad2002-05-15 08:30:12 +0000389 }else{
drh9adf9ac2002-05-15 11:44:13 +0000390 for(j=0; j<pColumn->nId; j++){
391 if( pColumn->a[j].idx==i ) break;
392 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000393 }
394 if( pColumn && j>=pColumn->nId ){
drh9adf9ac2002-05-15 11:44:13 +0000395 sqliteVdbeAddOp(v, OP_String, 0, 0);
396 sqliteVdbeChangeP3(v, -1, pTab->aCol[i].zDflt, P3_STATIC);
drh142e30d2002-08-28 03:00:58 +0000397 }else if( useTempTable ){
drh9adf9ac2002-05-15 11:44:13 +0000398 sqliteVdbeAddOp(v, OP_Column, srcTab, j);
drh142e30d2002-08-28 03:00:58 +0000399 }else if( pSelect ){
400 sqliteVdbeAddOp(v, OP_Dup, nColumn-j-1, 1);
danielk1977c3f9bad2002-05-15 08:30:12 +0000401 }else{
drh9adf9ac2002-05-15 11:44:13 +0000402 sqliteExprCode(pParse, pList->a[j].pExpr);
danielk1977c3f9bad2002-05-15 08:30:12 +0000403 }
404 }
405 sqliteVdbeAddOp(v, OP_MakeRecord, pTab->nCol, 0);
406 sqliteVdbeAddOp(v, OP_PutIntKey, newIdx, 0);
danielk1977c3f9bad2002-05-15 08:30:12 +0000407
408 /* Fire BEFORE triggers */
drhb2fe7d82003-04-20 17:29:23 +0000409 if( sqliteCodeRowTrigger(pParse, TK_INSERT, 0, TK_BEFORE, pTab,
410 newIdx, -1, onError, endOfLoop) ){
danielk1977f29ce552002-05-19 23:43:12 +0000411 goto insert_cleanup;
412 }
drh70ce3f02003-04-15 19:22:22 +0000413 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000414
drh70ce3f02003-04-15 19:22:22 +0000415 /* If any triggers exists, the opening of tables and indices is deferred
416 ** until now.
417 */
418 if( row_triggers_exist ){
danielk1977f29ce552002-05-19 23:43:12 +0000419 if( !pTab->pSelect ){
danielk1977c3f9bad2002-05-15 08:30:12 +0000420 base = pParse->nTab;
drhd24cc422003-03-27 12:51:24 +0000421 sqliteVdbeAddOp(v, OP_Integer, pTab->iDb, 0);
drh001bbcb2003-03-19 03:14:00 +0000422 sqliteVdbeAddOp(v, OP_OpenWrite, base, pTab->tnum);
danielk1977c3f9bad2002-05-15 08:30:12 +0000423 sqliteVdbeChangeP3(v, -1, pTab->zName, P3_STATIC);
424 for(idx=1, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, idx++){
drhd24cc422003-03-27 12:51:24 +0000425 sqliteVdbeAddOp(v, OP_Integer, pIdx->iDb, 0);
drh001bbcb2003-03-19 03:14:00 +0000426 sqliteVdbeAddOp(v, OP_OpenWrite, idx+base, pIdx->tnum);
drh9adf9ac2002-05-15 11:44:13 +0000427 sqliteVdbeChangeP3(v, -1, pIdx->zName, P3_STATIC);
danielk1977c3f9bad2002-05-15 08:30:12 +0000428 }
429 pParse->nTab += idx;
430 }
431 }
432
drh4a324312001-12-21 14:30:42 +0000433 /* Push the record number for the new entry onto the stack. The
434 ** record number is a randomly generate integer created by NewRecno
435 ** except when the table has an INTEGER PRIMARY KEY column, in which
drhb419a922002-01-30 16:17:23 +0000436 ** case the record number is the same as that column.
drh1ccde152000-06-17 13:12:39 +0000437 */
danielk1977f29ce552002-05-19 23:43:12 +0000438 if( !pTab->pSelect ){
danielk1977c3f9bad2002-05-15 08:30:12 +0000439 if( keyColumn>=0 ){
drh142e30d2002-08-28 03:00:58 +0000440 if( useTempTable ){
drh9adf9ac2002-05-15 11:44:13 +0000441 sqliteVdbeAddOp(v, OP_Column, srcTab, keyColumn);
drh142e30d2002-08-28 03:00:58 +0000442 }else if( pSelect ){
443 sqliteVdbeAddOp(v, OP_Dup, nColumn - keyColumn - 1, 1);
danielk1977c3f9bad2002-05-15 08:30:12 +0000444 }else{
drh9adf9ac2002-05-15 11:44:13 +0000445 sqliteExprCode(pParse, pList->a[keyColumn].pExpr);
danielk1977c3f9bad2002-05-15 08:30:12 +0000446 }
drh27a32782002-06-19 20:32:43 +0000447 /* If the PRIMARY KEY expression is NULL, then use OP_NewRecno
448 ** to generate a unique primary key value.
449 */
450 sqliteVdbeAddOp(v, OP_NotNull, -1, sqliteVdbeCurrentAddr(v)+3);
451 sqliteVdbeAddOp(v, OP_Pop, 1, 0);
452 sqliteVdbeAddOp(v, OP_NewRecno, base, 0);
danielk1977c3f9bad2002-05-15 08:30:12 +0000453 sqliteVdbeAddOp(v, OP_MustBeInt, 0, 0);
454 }else{
drhe1e68f42002-03-31 18:29:03 +0000455 sqliteVdbeAddOp(v, OP_NewRecno, base, 0);
drh4a324312001-12-21 14:30:42 +0000456 }
drh4a324312001-12-21 14:30:42 +0000457
danielk1977c3f9bad2002-05-15 08:30:12 +0000458 /* Push onto the stack, data for all columns of the new entry, beginning
danielk1977f29ce552002-05-19 23:43:12 +0000459 ** with the first column.
460 */
danielk1977c3f9bad2002-05-15 08:30:12 +0000461 for(i=0; i<pTab->nCol; i++){
462 if( i==pTab->iPKey ){
drh9adf9ac2002-05-15 11:44:13 +0000463 /* The value of the INTEGER PRIMARY KEY column is always a NULL.
danielk1977f29ce552002-05-19 23:43:12 +0000464 ** Whenever this column is read, the record number will be substituted
465 ** in its place. So will fill this column with a NULL to avoid
466 ** taking up data space with information that will never be used. */
drh9adf9ac2002-05-15 11:44:13 +0000467 sqliteVdbeAddOp(v, OP_String, 0, 0);
468 continue;
danielk1977c3f9bad2002-05-15 08:30:12 +0000469 }
470 if( pColumn==0 ){
drh9adf9ac2002-05-15 11:44:13 +0000471 j = i;
danielk1977c3f9bad2002-05-15 08:30:12 +0000472 }else{
drh9adf9ac2002-05-15 11:44:13 +0000473 for(j=0; j<pColumn->nId; j++){
474 if( pColumn->a[j].idx==i ) break;
475 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000476 }
477 if( pColumn && j>=pColumn->nId ){
drh9adf9ac2002-05-15 11:44:13 +0000478 sqliteVdbeAddOp(v, OP_String, 0, 0);
479 sqliteVdbeChangeP3(v, -1, pTab->aCol[i].zDflt, P3_STATIC);
drh142e30d2002-08-28 03:00:58 +0000480 }else if( useTempTable ){
drh9adf9ac2002-05-15 11:44:13 +0000481 sqliteVdbeAddOp(v, OP_Column, srcTab, j);
drh142e30d2002-08-28 03:00:58 +0000482 }else if( pSelect ){
483 sqliteVdbeAddOp(v, OP_Dup, i+nColumn-j, 1);
danielk1977c3f9bad2002-05-15 08:30:12 +0000484 }else{
drh9adf9ac2002-05-15 11:44:13 +0000485 sqliteExprCode(pParse, pList->a[j].pExpr);
drh5974a302000-06-07 14:42:26 +0000486 }
drhbed86902000-06-02 13:27:59 +0000487 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000488
489 /* Generate code to check constraints and generate index keys and
danielk1977f29ce552002-05-19 23:43:12 +0000490 ** do the insertion.
491 */
danielk1977c3f9bad2002-05-15 08:30:12 +0000492 sqliteGenerateConstraintChecks(pParse, pTab, base, 0,0,0,onError,endOfLoop);
drh70ce3f02003-04-15 19:22:22 +0000493 sqliteCompleteInsertion(pParse, pTab, base, 0,0,0,
494 after_triggers ? newIdx : -1);
danielk1977c3f9bad2002-05-15 08:30:12 +0000495
496 /* Update the count of rows that are inserted
danielk1977f29ce552002-05-19 23:43:12 +0000497 */
drh142e30d2002-08-28 03:00:58 +0000498 if( (db->flags & SQLITE_CountRows)!=0 ){
499 sqliteVdbeAddOp(v, OP_MemIncr, iCntMem, 0);
drh5974a302000-06-07 14:42:26 +0000500 }
501 }
drh1ccde152000-06-17 13:12:39 +0000502
danielk1977f29ce552002-05-19 23:43:12 +0000503 if( row_triggers_exist ){
danielk1977c3f9bad2002-05-15 08:30:12 +0000504 /* Close all tables opened */
danielk1977f29ce552002-05-19 23:43:12 +0000505 if( !pTab->pSelect ){
danielk1977c3f9bad2002-05-15 08:30:12 +0000506 sqliteVdbeAddOp(v, OP_Close, base, 0);
507 for(idx=1, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, idx++){
drh9adf9ac2002-05-15 11:44:13 +0000508 sqliteVdbeAddOp(v, OP_Close, idx+base, 0);
danielk1977c3f9bad2002-05-15 08:30:12 +0000509 }
510 }
drh1bee3d72001-10-15 00:44:35 +0000511
danielk1977c3f9bad2002-05-15 08:30:12 +0000512 /* Code AFTER triggers */
danielk1977f29ce552002-05-19 23:43:12 +0000513 if( sqliteCodeRowTrigger(pParse, TK_INSERT, 0, TK_AFTER, pTab, newIdx, -1,
danielk19776f349032002-06-11 02:25:40 +0000514 onError, endOfLoop) ){
danielk1977f29ce552002-05-19 23:43:12 +0000515 goto insert_cleanup;
516 }
drh1bee3d72001-10-15 00:44:35 +0000517 }
518
drh1ccde152000-06-17 13:12:39 +0000519 /* The bottom of the loop, if the data source is a SELECT statement
danielk1977f29ce552002-05-19 23:43:12 +0000520 */
drh0ca3e242002-01-29 23:07:02 +0000521 sqliteVdbeResolveLabel(v, endOfLoop);
drh142e30d2002-08-28 03:00:58 +0000522 if( useTempTable ){
drh6b563442001-11-07 16:48:26 +0000523 sqliteVdbeAddOp(v, OP_Next, srcTab, iCont);
drh99fcd712001-10-13 01:06:47 +0000524 sqliteVdbeResolveLabel(v, iBreak);
drh6b563442001-11-07 16:48:26 +0000525 sqliteVdbeAddOp(v, OP_Close, srcTab, 0);
drh142e30d2002-08-28 03:00:58 +0000526 }else if( pSelect ){
527 sqliteVdbeAddOp(v, OP_Pop, nColumn, 0);
528 sqliteVdbeAddOp(v, OP_Return, 0, 0);
529 sqliteVdbeResolveLabel(v, iCleanup);
drh6b563442001-11-07 16:48:26 +0000530 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000531
danielk1977f29ce552002-05-19 23:43:12 +0000532 if( !row_triggers_exist ){
danielk1977c3f9bad2002-05-15 08:30:12 +0000533 /* Close all tables opened */
534 sqliteVdbeAddOp(v, OP_Close, base, 0);
535 for(idx=1, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, idx++){
536 sqliteVdbeAddOp(v, OP_Close, idx+base, 0);
537 }
drhcce7d172000-05-31 15:34:51 +0000538 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000539
drh1c928532002-01-31 15:54:21 +0000540 sqliteEndWriteOperation(pParse);
drh5e00f6c2001-09-13 13:46:56 +0000541
drh1bee3d72001-10-15 00:44:35 +0000542 /*
danielk1977f29ce552002-05-19 23:43:12 +0000543 ** Return the number of rows inserted.
drh1bee3d72001-10-15 00:44:35 +0000544 */
drh142e30d2002-08-28 03:00:58 +0000545 if( db->flags & SQLITE_CountRows ){
drh1bee3d72001-10-15 00:44:35 +0000546 sqliteVdbeAddOp(v, OP_ColumnName, 0, 0);
547 sqliteVdbeChangeP3(v, -1, "rows inserted", P3_STATIC);
drh142e30d2002-08-28 03:00:58 +0000548 sqliteVdbeAddOp(v, OP_MemLoad, iCntMem, 0);
drh1bee3d72001-10-15 00:44:35 +0000549 sqliteVdbeAddOp(v, OP_Callback, 1, 0);
550 }
drhcce7d172000-05-31 15:34:51 +0000551
552insert_cleanup:
drh113088e2003-03-20 01:16:58 +0000553 sqliteSrcListDelete(pTabList);
drh5974a302000-06-07 14:42:26 +0000554 if( pList ) sqliteExprListDelete(pList);
555 if( pSelect ) sqliteSelectDelete(pSelect);
drh967e8b72000-06-21 13:59:10 +0000556 sqliteIdListDelete(pColumn);
drhcce7d172000-05-31 15:34:51 +0000557}
drh9cfcf5d2002-01-29 18:41:24 +0000558
drh9cfcf5d2002-01-29 18:41:24 +0000559/*
560** Generate code to do a constraint check prior to an INSERT or an UPDATE.
561**
562** When this routine is called, the stack contains (from bottom to top)
drh0ca3e242002-01-29 23:07:02 +0000563** the following values:
564**
drhb2fe7d82003-04-20 17:29:23 +0000565** 1. The recno of the row to be updated before the update. This
drhb419a922002-01-30 16:17:23 +0000566** value is omitted unless we are doing an UPDATE that involves a
567** change to the record number.
drh0ca3e242002-01-29 23:07:02 +0000568**
drhb419a922002-01-30 16:17:23 +0000569** 2. The recno of the row after the update.
drh0ca3e242002-01-29 23:07:02 +0000570**
571** 3. The data in the first column of the entry after the update.
572**
573** i. Data from middle columns...
574**
575** N. The data in the last column of the entry after the update.
576**
drhb419a922002-01-30 16:17:23 +0000577** The old recno shown as entry (1) above is omitted unless both isUpdate
drh1c928532002-01-31 15:54:21 +0000578** and recnoChng are 1. isUpdate is true for UPDATEs and false for
579** INSERTs and recnoChng is true if the record number is being changed.
drh0ca3e242002-01-29 23:07:02 +0000580**
581** The code generated by this routine pushes additional entries onto
582** the stack which are the keys for new index entries for the new record.
583** The order of index keys is the same as the order of the indices on
584** the pTable->pIndex list. A key is only created for index i if
585** aIdxUsed!=0 and aIdxUsed[i]!=0.
drh9cfcf5d2002-01-29 18:41:24 +0000586**
587** This routine also generates code to check constraints. NOT NULL,
588** CHECK, and UNIQUE constraints are all checked. If a constraint fails,
drh1c928532002-01-31 15:54:21 +0000589** then the appropriate action is performed. There are five possible
590** actions: ROLLBACK, ABORT, FAIL, REPLACE, and IGNORE.
drh9cfcf5d2002-01-29 18:41:24 +0000591**
592** Constraint type Action What Happens
593** --------------- ---------- ----------------------------------------
drh1c928532002-01-31 15:54:21 +0000594** any ROLLBACK The current transaction is rolled back and
drh9cfcf5d2002-01-29 18:41:24 +0000595** sqlite_exec() returns immediately with a
596** return code of SQLITE_CONSTRAINT.
597**
drh1c928532002-01-31 15:54:21 +0000598** any ABORT Back out changes from the current command
599** only (do not do a complete rollback) then
600** cause sqlite_exec() to return immediately
601** with SQLITE_CONSTRAINT.
602**
603** any FAIL Sqlite_exec() returns immediately with a
604** return code of SQLITE_CONSTRAINT. The
605** transaction is not rolled back and any
606** prior changes are retained.
607**
drh9cfcf5d2002-01-29 18:41:24 +0000608** any IGNORE The record number and data is popped from
609** the stack and there is an immediate jump
610** to label ignoreDest.
611**
612** NOT NULL REPLACE The NULL value is replace by the default
613** value for that column. If the default value
614** is NULL, the action is the same as ABORT.
615**
616** UNIQUE REPLACE The other row that conflicts with the row
617** being inserted is removed.
618**
619** CHECK REPLACE Illegal. The results in an exception.
620**
drh1c928532002-01-31 15:54:21 +0000621** Which action to take is determined by the overrideError parameter.
622** Or if overrideError==OE_Default, then the pParse->onError parameter
623** is used. Or if pParse->onError==OE_Default then the onError value
624** for the constraint is used.
drh9cfcf5d2002-01-29 18:41:24 +0000625**
drhaaab5722002-02-19 13:39:21 +0000626** The calling routine must open a read/write cursor for pTab with
drh9cfcf5d2002-01-29 18:41:24 +0000627** cursor number "base". All indices of pTab must also have open
628** read/write cursors with cursor number base+i for the i-th cursor.
629** Except, if there is no possibility of a REPLACE action then
630** cursors do not need to be open for indices where aIdxUsed[i]==0.
631**
632** If the isUpdate flag is true, it means that the "base" cursor is
633** initially pointing to an entry that is being updated. The isUpdate
634** flag causes extra code to be generated so that the "base" cursor
635** is still pointing at the same entry after the routine returns.
636** Without the isUpdate flag, the "base" cursor might be moved.
637*/
638void sqliteGenerateConstraintChecks(
639 Parse *pParse, /* The parser context */
640 Table *pTab, /* the table into which we are inserting */
641 int base, /* Index of a read/write cursor pointing at pTab */
642 char *aIdxUsed, /* Which indices are used. NULL means all are used */
drh0ca3e242002-01-29 23:07:02 +0000643 int recnoChng, /* True if the record number will change */
drhb419a922002-01-30 16:17:23 +0000644 int isUpdate, /* True for UPDATE, False for INSERT */
drh9cfcf5d2002-01-29 18:41:24 +0000645 int overrideError, /* Override onError to this if not OE_Default */
drhb419a922002-01-30 16:17:23 +0000646 int ignoreDest /* Jump to this label on an OE_Ignore resolution */
drh9cfcf5d2002-01-29 18:41:24 +0000647){
648 int i;
649 Vdbe *v;
650 int nCol;
651 int onError;
652 int addr;
653 int extra;
drh0ca3e242002-01-29 23:07:02 +0000654 int iCur;
655 Index *pIdx;
656 int seenReplace = 0;
drhf5905aa2002-05-26 20:54:33 +0000657 int jumpInst1, jumpInst2;
drh0ca3e242002-01-29 23:07:02 +0000658 int contAddr;
drhb419a922002-01-30 16:17:23 +0000659 int hasTwoRecnos = (isUpdate && recnoChng);
drh9cfcf5d2002-01-29 18:41:24 +0000660
661 v = sqliteGetVdbe(pParse);
662 assert( v!=0 );
drh417be792002-03-03 18:59:40 +0000663 assert( pTab->pSelect==0 ); /* This table is not a VIEW */
drh9cfcf5d2002-01-29 18:41:24 +0000664 nCol = pTab->nCol;
665
666 /* Test all NOT NULL constraints.
667 */
668 for(i=0; i<nCol; i++){
drh0ca3e242002-01-29 23:07:02 +0000669 if( i==pTab->iPKey ){
670 /* Fix me: Make sure the INTEGER PRIMARY KEY is not NULL. */
671 continue;
672 }
drh9cfcf5d2002-01-29 18:41:24 +0000673 onError = pTab->aCol[i].notNull;
drh0ca3e242002-01-29 23:07:02 +0000674 if( onError==OE_None ) continue;
drh9cfcf5d2002-01-29 18:41:24 +0000675 if( overrideError!=OE_Default ){
676 onError = overrideError;
drh1c928532002-01-31 15:54:21 +0000677 }else if( onError==OE_Default ){
drh0d65dc02002-02-03 00:56:09 +0000678 onError = pParse->db->onError;
679 if( onError==OE_Default ) onError = OE_Abort;
drh9cfcf5d2002-01-29 18:41:24 +0000680 }
681 if( onError==OE_Replace && pTab->aCol[i].zDflt==0 ){
682 onError = OE_Abort;
683 }
drhef6764a2002-01-30 04:32:00 +0000684 sqliteVdbeAddOp(v, OP_Dup, nCol-1-i, 1);
drhf5905aa2002-05-26 20:54:33 +0000685 addr = sqliteVdbeAddOp(v, OP_NotNull, 1, 0);
drh9cfcf5d2002-01-29 18:41:24 +0000686 switch( onError ){
drh1c928532002-01-31 15:54:21 +0000687 case OE_Rollback:
688 case OE_Abort:
689 case OE_Fail: {
drh483750b2003-01-29 18:46:51 +0000690 char *zMsg = 0;
drh1c928532002-01-31 15:54:21 +0000691 sqliteVdbeAddOp(v, OP_Halt, SQLITE_CONSTRAINT, onError);
drh483750b2003-01-29 18:46:51 +0000692 sqliteSetString(&zMsg, pTab->zName, ".", pTab->aCol[i].zName,
693 " may not be NULL", 0);
694 sqliteVdbeChangeP3(v, -1, zMsg, P3_DYNAMIC);
drh9cfcf5d2002-01-29 18:41:24 +0000695 break;
696 }
697 case OE_Ignore: {
drhb419a922002-01-30 16:17:23 +0000698 sqliteVdbeAddOp(v, OP_Pop, nCol+1+hasTwoRecnos, 0);
drh0ca3e242002-01-29 23:07:02 +0000699 sqliteVdbeAddOp(v, OP_Goto, 0, ignoreDest);
drh9cfcf5d2002-01-29 18:41:24 +0000700 break;
701 }
702 case OE_Replace: {
703 sqliteVdbeAddOp(v, OP_String, 0, 0);
704 sqliteVdbeChangeP3(v, -1, pTab->aCol[i].zDflt, P3_STATIC);
705 sqliteVdbeAddOp(v, OP_Push, nCol-i, 0);
706 break;
707 }
drh0ca3e242002-01-29 23:07:02 +0000708 default: assert(0);
drh9cfcf5d2002-01-29 18:41:24 +0000709 }
drhef6764a2002-01-30 04:32:00 +0000710 sqliteVdbeChangeP2(v, addr, sqliteVdbeCurrentAddr(v));
drh9cfcf5d2002-01-29 18:41:24 +0000711 }
712
713 /* Test all CHECK constraints
714 */
drh0bd1f4e2002-06-06 18:54:39 +0000715 /**** TBD ****/
drh9cfcf5d2002-01-29 18:41:24 +0000716
drh0bd1f4e2002-06-06 18:54:39 +0000717 /* If we have an INTEGER PRIMARY KEY, make sure the primary key
718 ** of the new record does not previously exist. Except, if this
719 ** is an UPDATE and the primary key is not changing, that is OK.
720 ** Also, if the conflict resolution policy is REPLACE, then we
721 ** can skip this test.
drh9cfcf5d2002-01-29 18:41:24 +0000722 */
drh0d65dc02002-02-03 00:56:09 +0000723 if( (recnoChng || !isUpdate) && pTab->iPKey>=0 ){
drh0ca3e242002-01-29 23:07:02 +0000724 onError = pTab->keyConf;
725 if( overrideError!=OE_Default ){
726 onError = overrideError;
drh1c928532002-01-31 15:54:21 +0000727 }else if( onError==OE_Default ){
drh0d65dc02002-02-03 00:56:09 +0000728 onError = pParse->db->onError;
729 if( onError==OE_Default ) onError = OE_Abort;
drh0ca3e242002-01-29 23:07:02 +0000730 }
drh0d65dc02002-02-03 00:56:09 +0000731 if( onError!=OE_Replace ){
drh79b0c952002-05-21 12:56:43 +0000732 if( isUpdate ){
733 sqliteVdbeAddOp(v, OP_Dup, nCol+1, 1);
734 sqliteVdbeAddOp(v, OP_Dup, nCol+1, 1);
drhf5905aa2002-05-26 20:54:33 +0000735 jumpInst1 = sqliteVdbeAddOp(v, OP_Eq, 0, 0);
drh79b0c952002-05-21 12:56:43 +0000736 }
drh0d65dc02002-02-03 00:56:09 +0000737 sqliteVdbeAddOp(v, OP_Dup, nCol, 1);
drhf5905aa2002-05-26 20:54:33 +0000738 jumpInst2 = sqliteVdbeAddOp(v, OP_NotExists, base, 0);
drh0d65dc02002-02-03 00:56:09 +0000739 switch( onError ){
740 case OE_Rollback:
741 case OE_Abort:
742 case OE_Fail: {
743 sqliteVdbeAddOp(v, OP_Halt, SQLITE_CONSTRAINT, onError);
drh483750b2003-01-29 18:46:51 +0000744 sqliteVdbeChangeP3(v, -1, "PRIMARY KEY must be unique", P3_STATIC);
drh0d65dc02002-02-03 00:56:09 +0000745 break;
746 }
747 case OE_Ignore: {
748 sqliteVdbeAddOp(v, OP_Pop, nCol+1+hasTwoRecnos, 0);
749 sqliteVdbeAddOp(v, OP_Goto, 0, ignoreDest);
750 break;
751 }
752 default: assert(0);
drh0ca3e242002-01-29 23:07:02 +0000753 }
drh0d65dc02002-02-03 00:56:09 +0000754 contAddr = sqliteVdbeCurrentAddr(v);
drhf5905aa2002-05-26 20:54:33 +0000755 sqliteVdbeChangeP2(v, jumpInst2, contAddr);
drh0d65dc02002-02-03 00:56:09 +0000756 if( isUpdate ){
drhf5905aa2002-05-26 20:54:33 +0000757 sqliteVdbeChangeP2(v, jumpInst1, contAddr);
drh0d65dc02002-02-03 00:56:09 +0000758 sqliteVdbeAddOp(v, OP_Dup, nCol+1, 1);
759 sqliteVdbeAddOp(v, OP_MoveTo, base, 0);
drh0ca3e242002-01-29 23:07:02 +0000760 }
drh0ca3e242002-01-29 23:07:02 +0000761 }
762 }
drh0bd1f4e2002-06-06 18:54:39 +0000763
764 /* Test all UNIQUE constraints by creating entries for each UNIQUE
765 ** index and making sure that duplicate entries do not already exist.
766 ** Add the new records to the indices as we go.
767 */
drhb2fe7d82003-04-20 17:29:23 +0000768 extra = -1;
769 for(iCur=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, iCur++){
770 if( aIdxUsed && aIdxUsed[iCur]==0 ) continue; /* Skip unused indices */
771 extra++;
772
773 /* Create a key for accessing the index entry */
drh9cfcf5d2002-01-29 18:41:24 +0000774 sqliteVdbeAddOp(v, OP_Dup, nCol+extra, 1);
775 for(i=0; i<pIdx->nColumn; i++){
776 int idx = pIdx->aiColumn[i];
777 if( idx==pTab->iPKey ){
drh0ca3e242002-01-29 23:07:02 +0000778 sqliteVdbeAddOp(v, OP_Dup, i+extra+nCol+1, 1);
drh9cfcf5d2002-01-29 18:41:24 +0000779 }else{
drh0ca3e242002-01-29 23:07:02 +0000780 sqliteVdbeAddOp(v, OP_Dup, i+extra+nCol-idx, 1);
drh9cfcf5d2002-01-29 18:41:24 +0000781 }
782 }
drhf5905aa2002-05-26 20:54:33 +0000783 jumpInst1 = sqliteVdbeAddOp(v, OP_MakeIdxKey, pIdx->nColumn, 0);
drh491791a2002-07-18 00:34:09 +0000784 if( pParse->db->file_format>=4 ) sqliteAddIdxKeyType(v, pIdx);
drhb2fe7d82003-04-20 17:29:23 +0000785
786 /* Find out what action to take in case there is an indexing conflict */
drh9cfcf5d2002-01-29 18:41:24 +0000787 onError = pIdx->onError;
drhb2fe7d82003-04-20 17:29:23 +0000788 if( onError==OE_None ) continue; /* pIdx is not a UNIQUE index */
drh9cfcf5d2002-01-29 18:41:24 +0000789 if( overrideError!=OE_Default ){
790 onError = overrideError;
drh1c928532002-01-31 15:54:21 +0000791 }else if( onError==OE_Default ){
drh0d65dc02002-02-03 00:56:09 +0000792 onError = pParse->db->onError;
793 if( onError==OE_Default ) onError = OE_Abort;
drh9cfcf5d2002-01-29 18:41:24 +0000794 }
drhb2fe7d82003-04-20 17:29:23 +0000795
796 /* Check to see if the new index entry will be unique */
drhb419a922002-01-30 16:17:23 +0000797 sqliteVdbeAddOp(v, OP_Dup, extra+nCol+1+hasTwoRecnos, 1);
drhf5905aa2002-05-26 20:54:33 +0000798 jumpInst2 = sqliteVdbeAddOp(v, OP_IsUnique, base+iCur+1, 0);
drhb2fe7d82003-04-20 17:29:23 +0000799
800 /* Generate code that executes if the new index entry is not unique */
drh9cfcf5d2002-01-29 18:41:24 +0000801 switch( onError ){
drh1c928532002-01-31 15:54:21 +0000802 case OE_Rollback:
803 case OE_Abort:
804 case OE_Fail: {
805 sqliteVdbeAddOp(v, OP_Halt, SQLITE_CONSTRAINT, onError);
drh483750b2003-01-29 18:46:51 +0000806 sqliteVdbeChangeP3(v, -1, "uniqueness constraint failed", P3_STATIC);
drh9cfcf5d2002-01-29 18:41:24 +0000807 break;
808 }
809 case OE_Ignore: {
drh0ca3e242002-01-29 23:07:02 +0000810 assert( seenReplace==0 );
drhfe1a1772002-04-09 03:15:06 +0000811 sqliteVdbeAddOp(v, OP_Pop, nCol+extra+3+hasTwoRecnos, 0);
drh9cfcf5d2002-01-29 18:41:24 +0000812 sqliteVdbeAddOp(v, OP_Goto, 0, ignoreDest);
drh9cfcf5d2002-01-29 18:41:24 +0000813 break;
814 }
815 case OE_Replace: {
drh38640e12002-07-05 21:42:36 +0000816 sqliteGenerateRowDelete(pParse->db, v, pTab, base, 0);
drh9cfcf5d2002-01-29 18:41:24 +0000817 if( isUpdate ){
drhb419a922002-01-30 16:17:23 +0000818 sqliteVdbeAddOp(v, OP_Dup, nCol+extra+1+hasTwoRecnos, 1);
drh0ca3e242002-01-29 23:07:02 +0000819 sqliteVdbeAddOp(v, OP_MoveTo, base, 0);
drh9cfcf5d2002-01-29 18:41:24 +0000820 }
drh0ca3e242002-01-29 23:07:02 +0000821 seenReplace = 1;
drh9cfcf5d2002-01-29 18:41:24 +0000822 break;
823 }
drh0ca3e242002-01-29 23:07:02 +0000824 default: assert(0);
drh9cfcf5d2002-01-29 18:41:24 +0000825 }
826 contAddr = sqliteVdbeCurrentAddr(v);
drh0bd1f4e2002-06-06 18:54:39 +0000827#if NULL_DISTINCT_FOR_UNIQUE
drhf5905aa2002-05-26 20:54:33 +0000828 sqliteVdbeChangeP2(v, jumpInst1, contAddr);
drh0bd1f4e2002-06-06 18:54:39 +0000829#endif
drhf5905aa2002-05-26 20:54:33 +0000830 sqliteVdbeChangeP2(v, jumpInst2, contAddr);
drh9cfcf5d2002-01-29 18:41:24 +0000831 }
832}
drh0ca3e242002-01-29 23:07:02 +0000833
834/*
835** This routine generates code to finish the INSERT or UPDATE operation
836** that was started by a prior call to sqliteGenerateConstraintChecks.
837** The stack must contain keys for all active indices followed by data
838** and the recno for the new entry. This routine creates the new
839** entries in all indices and in the main table.
840**
drhb419a922002-01-30 16:17:23 +0000841** The arguments to this routine should be the same as the first six
drh0ca3e242002-01-29 23:07:02 +0000842** arguments to sqliteGenerateConstraintChecks.
843*/
844void sqliteCompleteInsertion(
845 Parse *pParse, /* The parser context */
846 Table *pTab, /* the table into which we are inserting */
847 int base, /* Index of a read/write cursor pointing at pTab */
848 char *aIdxUsed, /* Which indices are used. NULL means all are used */
drhb419a922002-01-30 16:17:23 +0000849 int recnoChng, /* True if the record number will change */
drh70ce3f02003-04-15 19:22:22 +0000850 int isUpdate, /* True for UPDATE, False for INSERT */
851 int newIdx /* Index of NEW table for triggers. -1 if none */
drh0ca3e242002-01-29 23:07:02 +0000852){
853 int i;
854 Vdbe *v;
855 int nIdx;
856 Index *pIdx;
857
858 v = sqliteGetVdbe(pParse);
859 assert( v!=0 );
drh417be792002-03-03 18:59:40 +0000860 assert( pTab->pSelect==0 ); /* This table is not a VIEW */
drh0ca3e242002-01-29 23:07:02 +0000861 for(nIdx=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, nIdx++){}
862 for(i=nIdx-1; i>=0; i--){
863 if( aIdxUsed && aIdxUsed[i]==0 ) continue;
864 sqliteVdbeAddOp(v, OP_IdxPut, base+i+1, 0);
865 }
866 sqliteVdbeAddOp(v, OP_MakeRecord, pTab->nCol, 0);
drh70ce3f02003-04-15 19:22:22 +0000867 if( newIdx>=0 ){
868 sqliteVdbeAddOp(v, OP_Dup, 1, 0);
869 sqliteVdbeAddOp(v, OP_Dup, 1, 0);
870 sqliteVdbeAddOp(v, OP_PutIntKey, newIdx, 0);
871 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000872 sqliteVdbeAddOp(v, OP_PutIntKey, base, pParse->trigStack?0:1);
drhb419a922002-01-30 16:17:23 +0000873 if( isUpdate && recnoChng ){
drh0ca3e242002-01-29 23:07:02 +0000874 sqliteVdbeAddOp(v, OP_Pop, 1, 0);
875 }
876}