blob: e14b1c454c7d64c257bf43b57c10576a8bec8558 [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**
drhbb37fdc2002-01-30 00:54:55 +000015** $Id: insert.c,v 1.36 2002/01/30 00:54:56 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.
drhcce7d172000-05-31 15:34:51 +000033*/
34void sqliteInsert(
35 Parse *pParse, /* Parser context */
36 Token *pTableName, /* Name of table into which we are inserting */
37 ExprList *pList, /* List of values to be inserted */
drh5974a302000-06-07 14:42:26 +000038 Select *pSelect, /* A SELECT statement to use as the data source */
drh9cfcf5d2002-01-29 18:41:24 +000039 IdList *pColumn, /* Column names corresponding to IDLIST. */
40 int onError /* How to handle constraint errors */
drhcce7d172000-05-31 15:34:51 +000041){
drh5974a302000-06-07 14:42:26 +000042 Table *pTab; /* The table to insert into */
43 char *zTab; /* Name of the table into which we are inserting */
44 int i, j, idx; /* Loop counters */
45 Vdbe *v; /* Generate code into this virtual machine */
46 Index *pIdx; /* For looping over indices of the table */
47 int srcTab; /* Date comes from this temporary cursor if >=0 */
drh967e8b72000-06-21 13:59:10 +000048 int nColumn; /* Number of columns in the data */
drh5974a302000-06-07 14:42:26 +000049 int base; /* First available cursor */
50 int iCont, iBreak; /* Beginning and end of the loop over srcTab */
drhecdc7532001-09-23 02:35:53 +000051 sqlite *db; /* The main database structure */
drhf57b3392001-10-08 13:22:32 +000052 int openOp; /* Opcode used to open cursors */
drh4a324312001-12-21 14:30:42 +000053 int keyColumn = -1; /* Column that is the INTEGER PRIMARY KEY */
drh0ca3e242002-01-29 23:07:02 +000054 int endOfLoop; /* Label for the end of the insertion loop */
drhcce7d172000-05-31 15:34:51 +000055
drhdaffd0e2001-04-11 14:28:42 +000056 if( pParse->nErr || sqlite_malloc_failed ) goto insert_cleanup;
drhecdc7532001-09-23 02:35:53 +000057 db = pParse->db;
drhdaffd0e2001-04-11 14:28:42 +000058
drh1ccde152000-06-17 13:12:39 +000059 /* Locate the table into which we will be inserting new information.
60 */
drhcce7d172000-05-31 15:34:51 +000061 zTab = sqliteTableNameFromToken(pTableName);
drhdaffd0e2001-04-11 14:28:42 +000062 if( zTab==0 ) goto insert_cleanup;
drhecdc7532001-09-23 02:35:53 +000063 pTab = sqliteFindTable(db, zTab);
drhcce7d172000-05-31 15:34:51 +000064 sqliteFree(zTab);
65 if( pTab==0 ){
66 sqliteSetNString(&pParse->zErrMsg, "no such table: ", 0,
67 pTableName->z, pTableName->n, 0);
68 pParse->nErr++;
69 goto insert_cleanup;
70 }
71 if( pTab->readOnly ){
72 sqliteSetString(&pParse->zErrMsg, "table ", pTab->zName,
73 " may not be modified", 0);
74 pParse->nErr++;
75 goto insert_cleanup;
76 }
drh1ccde152000-06-17 13:12:39 +000077
78 /* Allocate a VDBE
79 */
drhd8bc7082000-06-07 23:51:50 +000080 v = sqliteGetVdbe(pParse);
drh5974a302000-06-07 14:42:26 +000081 if( v==0 ) goto insert_cleanup;
drhecdc7532001-09-23 02:35:53 +000082 if( (db->flags & SQLITE_InTrans)==0 ){
drh99fcd712001-10-13 01:06:47 +000083 sqliteVdbeAddOp(v, OP_Transaction, 0, 0);
84 sqliteVdbeAddOp(v, OP_VerifyCookie, db->schema_cookie, 0);
drhecdc7532001-09-23 02:35:53 +000085 pParse->schemaVerified = 1;
drh5e00f6c2001-09-13 13:46:56 +000086 }
drh1ccde152000-06-17 13:12:39 +000087
88 /* Figure out how many columns of data are supplied. If the data
drhc6b52df2002-01-04 03:09:29 +000089 ** is coming from a SELECT statement, then this step has to generate
drh1ccde152000-06-17 13:12:39 +000090 ** all the code to implement the SELECT statement and leave the data
91 ** in a temporary table. If data is coming from an expression list,
92 ** then we just have to count the number of expressions.
93 */
drh5974a302000-06-07 14:42:26 +000094 if( pSelect ){
95 int rc;
96 srcTab = pParse->nTab++;
drh99fcd712001-10-13 01:06:47 +000097 sqliteVdbeAddOp(v, OP_OpenTemp, srcTab, 0);
drh5974a302000-06-07 14:42:26 +000098 rc = sqliteSelect(pParse, pSelect, SRT_Table, srcTab);
drhdaffd0e2001-04-11 14:28:42 +000099 if( rc || pParse->nErr || sqlite_malloc_failed ) goto insert_cleanup;
drh5974a302000-06-07 14:42:26 +0000100 assert( pSelect->pEList );
drh967e8b72000-06-21 13:59:10 +0000101 nColumn = pSelect->pEList->nExpr;
drh5974a302000-06-07 14:42:26 +0000102 }else{
drhdaffd0e2001-04-11 14:28:42 +0000103 assert( pList!=0 );
drh5974a302000-06-07 14:42:26 +0000104 srcTab = -1;
105 assert( pList );
drh967e8b72000-06-21 13:59:10 +0000106 nColumn = pList->nExpr;
drh5974a302000-06-07 14:42:26 +0000107 }
drh1ccde152000-06-17 13:12:39 +0000108
109 /* Make sure the number of columns in the source data matches the number
110 ** of columns to be inserted into the table.
111 */
drh967e8b72000-06-21 13:59:10 +0000112 if( pColumn==0 && nColumn!=pTab->nCol ){
drhcce7d172000-05-31 15:34:51 +0000113 char zNum1[30];
114 char zNum2[30];
drh967e8b72000-06-21 13:59:10 +0000115 sprintf(zNum1,"%d", nColumn);
drhcce7d172000-05-31 15:34:51 +0000116 sprintf(zNum2,"%d", pTab->nCol);
117 sqliteSetString(&pParse->zErrMsg, "table ", pTab->zName,
118 " has ", zNum2, " columns but ",
119 zNum1, " values were supplied", 0);
120 pParse->nErr++;
121 goto insert_cleanup;
122 }
drh967e8b72000-06-21 13:59:10 +0000123 if( pColumn!=0 && nColumn!=pColumn->nId ){
drhcce7d172000-05-31 15:34:51 +0000124 char zNum1[30];
125 char zNum2[30];
drh967e8b72000-06-21 13:59:10 +0000126 sprintf(zNum1,"%d", nColumn);
127 sprintf(zNum2,"%d", pColumn->nId);
drhcce7d172000-05-31 15:34:51 +0000128 sqliteSetString(&pParse->zErrMsg, zNum1, " values for ",
129 zNum2, " columns", 0);
130 pParse->nErr++;
131 goto insert_cleanup;
132 }
drh1ccde152000-06-17 13:12:39 +0000133
134 /* If the INSERT statement included an IDLIST term, then make sure
135 ** all elements of the IDLIST really are columns of the table and
136 ** remember the column indices.
drhc8392582001-12-31 02:48:51 +0000137 **
138 ** If the table has an INTEGER PRIMARY KEY column and that column
139 ** is named in the IDLIST, then record in the keyColumn variable
140 ** the index into IDLIST of the primary key column. keyColumn is
141 ** the index of the primary key as it appears in IDLIST, not as
142 ** is appears in the original table. (The index of the primary
143 ** key in the original table is pTab->iPKey.)
drh1ccde152000-06-17 13:12:39 +0000144 */
drh967e8b72000-06-21 13:59:10 +0000145 if( pColumn ){
146 for(i=0; i<pColumn->nId; i++){
147 pColumn->a[i].idx = -1;
drhcce7d172000-05-31 15:34:51 +0000148 }
drh967e8b72000-06-21 13:59:10 +0000149 for(i=0; i<pColumn->nId; i++){
drhcce7d172000-05-31 15:34:51 +0000150 for(j=0; j<pTab->nCol; j++){
drh967e8b72000-06-21 13:59:10 +0000151 if( sqliteStrICmp(pColumn->a[i].zName, pTab->aCol[j].zName)==0 ){
152 pColumn->a[i].idx = j;
drh4a324312001-12-21 14:30:42 +0000153 if( j==pTab->iPKey ){
drh9aa028d2001-12-22 21:48:29 +0000154 keyColumn = i;
drh4a324312001-12-21 14:30:42 +0000155 }
drhcce7d172000-05-31 15:34:51 +0000156 break;
157 }
158 }
159 if( j>=pTab->nCol ){
160 sqliteSetString(&pParse->zErrMsg, "table ", pTab->zName,
drh967e8b72000-06-21 13:59:10 +0000161 " has no column named ", pColumn->a[i].zName, 0);
drhcce7d172000-05-31 15:34:51 +0000162 pParse->nErr++;
163 goto insert_cleanup;
164 }
165 }
166 }
drh1ccde152000-06-17 13:12:39 +0000167
drhaacc5432002-01-06 17:07:40 +0000168 /* If there is no IDLIST term but the table has an integer primary
drhc8392582001-12-31 02:48:51 +0000169 ** key, the set the keyColumn variable to the primary key column index
170 ** in the original table definition.
drh4a324312001-12-21 14:30:42 +0000171 */
172 if( pColumn==0 ){
173 keyColumn = pTab->iPKey;
174 }
175
drh1ccde152000-06-17 13:12:39 +0000176 /* Open cursors into the table that is received the new data and
177 ** all indices of that table.
178 */
drh5974a302000-06-07 14:42:26 +0000179 base = pParse->nTab;
drhf57b3392001-10-08 13:22:32 +0000180 openOp = pTab->isTemp ? OP_OpenWrAux : OP_OpenWrite;
drh99fcd712001-10-13 01:06:47 +0000181 sqliteVdbeAddOp(v, openOp, base, pTab->tnum);
182 sqliteVdbeChangeP3(v, -1, pTab->zName, P3_STATIC);
drh5974a302000-06-07 14:42:26 +0000183 for(idx=1, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, idx++){
drh99fcd712001-10-13 01:06:47 +0000184 sqliteVdbeAddOp(v, openOp, idx+base, pIdx->tnum);
185 sqliteVdbeChangeP3(v, -1, pIdx->zName, P3_STATIC);
drh19a775c2000-06-05 18:54:46 +0000186 }
drh1ccde152000-06-17 13:12:39 +0000187
188 /* If the data source is a SELECT statement, then we have to create
189 ** a loop because there might be multiple rows of data. If the data
190 ** source is an expression list, then exactly one row will be inserted
191 ** and the loop is not used.
192 */
drh5974a302000-06-07 14:42:26 +0000193 if( srcTab>=0 ){
drh1bee3d72001-10-15 00:44:35 +0000194 if( db->flags & SQLITE_CountRows ){
195 sqliteVdbeAddOp(v, OP_Integer, 0, 0); /* Initialize the row count */
196 }
drh5974a302000-06-07 14:42:26 +0000197 iBreak = sqliteVdbeMakeLabel(v);
drh6b563442001-11-07 16:48:26 +0000198 sqliteVdbeAddOp(v, OP_Rewind, srcTab, iBreak);
199 iCont = sqliteVdbeCurrentAddr(v);
drh5974a302000-06-07 14:42:26 +0000200 }
drh1ccde152000-06-17 13:12:39 +0000201
drh4a324312001-12-21 14:30:42 +0000202 /* Push the record number for the new entry onto the stack. The
203 ** record number is a randomly generate integer created by NewRecno
204 ** except when the table has an INTEGER PRIMARY KEY column, in which
drh0ca3e242002-01-29 23:07:02 +0000205 ** case the record number is the same as that column. May a copy
206 ** because sqliteGenerateConstraintChecks() requires two copies of
207 ** the record number.
drh1ccde152000-06-17 13:12:39 +0000208 */
drh4a324312001-12-21 14:30:42 +0000209 if( keyColumn>=0 ){
210 if( srcTab>=0 ){
211 sqliteVdbeAddOp(v, OP_Column, srcTab, keyColumn);
212 }else{
213 sqliteExprCode(pParse, pList->a[keyColumn].pExpr);
214 }
drh8aff1012001-12-22 14:49:24 +0000215 sqliteVdbeAddOp(v, OP_MustBeInt, 0, 0);
drh4a324312001-12-21 14:30:42 +0000216 }else{
217 sqliteVdbeAddOp(v, OP_NewRecno, base, 0);
218 }
drh0ca3e242002-01-29 23:07:02 +0000219 sqliteVdbeAddOp(v, OP_Dup, 0, 0);
drh4a324312001-12-21 14:30:42 +0000220
drhaacc5432002-01-06 17:07:40 +0000221 /* Push onto the stack, data for all columns of the new entry, beginning
drh4a324312001-12-21 14:30:42 +0000222 ** with the first column.
223 */
drh5974a302000-06-07 14:42:26 +0000224 for(i=0; i<pTab->nCol; i++){
drh4a324312001-12-21 14:30:42 +0000225 if( i==pTab->iPKey ){
226 /* The value of the INTEGER PRIMARY KEY column is always a NULL.
drhaacc5432002-01-06 17:07:40 +0000227 ** Whenever this column is read, the record number will be substituted
228 ** in its place. So will fill this column with a NULL to avoid
229 ** taking up data space with information that will never be used. */
drh4a324312001-12-21 14:30:42 +0000230 sqliteVdbeAddOp(v, OP_String, 0, 0);
231 continue;
232 }
drh967e8b72000-06-21 13:59:10 +0000233 if( pColumn==0 ){
drh5974a302000-06-07 14:42:26 +0000234 j = i;
235 }else{
drh967e8b72000-06-21 13:59:10 +0000236 for(j=0; j<pColumn->nId; j++){
237 if( pColumn->a[j].idx==i ) break;
drh5974a302000-06-07 14:42:26 +0000238 }
drhbed86902000-06-02 13:27:59 +0000239 }
drh967e8b72000-06-21 13:59:10 +0000240 if( pColumn && j>=pColumn->nId ){
drh99fcd712001-10-13 01:06:47 +0000241 sqliteVdbeAddOp(v, OP_String, 0, 0);
242 sqliteVdbeChangeP3(v, -1, pTab->aCol[i].zDflt, P3_STATIC);
drh5974a302000-06-07 14:42:26 +0000243 }else if( srcTab>=0 ){
drh99fcd712001-10-13 01:06:47 +0000244 sqliteVdbeAddOp(v, OP_Column, srcTab, i);
drh5974a302000-06-07 14:42:26 +0000245 }else{
246 sqliteExprCode(pParse, pList->a[j].pExpr);
247 }
248 }
drh1ccde152000-06-17 13:12:39 +0000249
drh0ca3e242002-01-29 23:07:02 +0000250 /* Generate code to check constraints and generate index keys and
251 ** do the insertion.
drh4a324312001-12-21 14:30:42 +0000252 */
drh0ca3e242002-01-29 23:07:02 +0000253 endOfLoop = sqliteVdbeMakeLabel(v);
254 sqliteGenerateConstraintChecks(pParse, pTab, base, 0,1,onError,endOfLoop,0);
255 sqliteCompleteInsertion(pParse, pTab, base, 0, 1);
drh1bee3d72001-10-15 00:44:35 +0000256
257 /* If inserting from a SELECT, keep a count of the number of
258 ** rows inserted.
259 */
260 if( srcTab>=0 && (db->flags & SQLITE_CountRows)!=0 ){
261 sqliteVdbeAddOp(v, OP_AddImm, 1, 0);
262 }
263
drh1ccde152000-06-17 13:12:39 +0000264 /* The bottom of the loop, if the data source is a SELECT statement
265 */
drh0ca3e242002-01-29 23:07:02 +0000266 sqliteVdbeResolveLabel(v, endOfLoop);
drh5974a302000-06-07 14:42:26 +0000267 if( srcTab>=0 ){
drh6b563442001-11-07 16:48:26 +0000268 sqliteVdbeAddOp(v, OP_Next, srcTab, iCont);
drh99fcd712001-10-13 01:06:47 +0000269 sqliteVdbeResolveLabel(v, iBreak);
drh6b563442001-11-07 16:48:26 +0000270 sqliteVdbeAddOp(v, OP_Close, srcTab, 0);
271 }
272 sqliteVdbeAddOp(v, OP_Close, base, 0);
273 for(idx=1, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, idx++){
274 sqliteVdbeAddOp(v, OP_Close, idx+base, 0);
drhcce7d172000-05-31 15:34:51 +0000275 }
drhecdc7532001-09-23 02:35:53 +0000276 if( (db->flags & SQLITE_InTrans)==0 ){
drh99fcd712001-10-13 01:06:47 +0000277 sqliteVdbeAddOp(v, OP_Commit, 0, 0);
drh5e00f6c2001-09-13 13:46:56 +0000278 }
279
drh1bee3d72001-10-15 00:44:35 +0000280 /*
281 ** Return the number of rows inserted.
282 */
283 if( db->flags & SQLITE_CountRows ){
284 sqliteVdbeAddOp(v, OP_ColumnCount, 1, 0);
285 sqliteVdbeAddOp(v, OP_ColumnName, 0, 0);
286 sqliteVdbeChangeP3(v, -1, "rows inserted", P3_STATIC);
287 if( srcTab<0 ){
288 sqliteVdbeAddOp(v, OP_Integer, 1, 0);
289 }
290 sqliteVdbeAddOp(v, OP_Callback, 1, 0);
291 }
drhcce7d172000-05-31 15:34:51 +0000292
293insert_cleanup:
drh5974a302000-06-07 14:42:26 +0000294 if( pList ) sqliteExprListDelete(pList);
295 if( pSelect ) sqliteSelectDelete(pSelect);
drh967e8b72000-06-21 13:59:10 +0000296 sqliteIdListDelete(pColumn);
drhcce7d172000-05-31 15:34:51 +0000297}
drh9cfcf5d2002-01-29 18:41:24 +0000298
drh9cfcf5d2002-01-29 18:41:24 +0000299/*
300** Generate code to do a constraint check prior to an INSERT or an UPDATE.
301**
302** When this routine is called, the stack contains (from bottom to top)
drh0ca3e242002-01-29 23:07:02 +0000303** the following values:
304**
305** 1. The recno of the row to be updated before it is updated.
306**
307** 2. The recno of the row after the update. (This is usually the
308** same as (1) but can be different if an UPDATE changes an
309** INTEGER PRIMARY KEY column.)
310**
311** 3. The data in the first column of the entry after the update.
312**
313** i. Data from middle columns...
314**
315** N. The data in the last column of the entry after the update.
316**
317** The old recno shown as entry (1) above is omitted if the recnoChng
318** parameter is 0. recnoChange is true if the record number is changing
319** and false if not.
320**
321** The code generated by this routine pushes additional entries onto
322** the stack which are the keys for new index entries for the new record.
323** The order of index keys is the same as the order of the indices on
324** the pTable->pIndex list. A key is only created for index i if
325** aIdxUsed!=0 and aIdxUsed[i]!=0.
drh9cfcf5d2002-01-29 18:41:24 +0000326**
327** This routine also generates code to check constraints. NOT NULL,
328** CHECK, and UNIQUE constraints are all checked. If a constraint fails,
329** then the appropriate action is performed. The default action is to
330** execute OP_Halt to abort the transaction and cause sqlite_exec() to
331** return SQLITE_CONSTRAINT. This is the so-called "ABORT" action.
332** Other actions are REPLACE and IGNORE. The following table summarizes
333** what happens.
334**
335** Constraint type Action What Happens
336** --------------- ---------- ----------------------------------------
337** any ABORT The current transaction is rolled back and
338** sqlite_exec() returns immediately with a
339** return code of SQLITE_CONSTRAINT.
340**
341** any IGNORE The record number and data is popped from
342** the stack and there is an immediate jump
343** to label ignoreDest.
344**
345** NOT NULL REPLACE The NULL value is replace by the default
346** value for that column. If the default value
347** is NULL, the action is the same as ABORT.
348**
349** UNIQUE REPLACE The other row that conflicts with the row
350** being inserted is removed.
351**
352** CHECK REPLACE Illegal. The results in an exception.
353**
354** The action to take is determined by the constraint itself if
355** overrideError is OE_Default. Otherwise, overrideError determines
356** which action to use.
357**
358** The calling routine must an open read/write cursor for pTab with
359** cursor number "base". All indices of pTab must also have open
360** read/write cursors with cursor number base+i for the i-th cursor.
361** Except, if there is no possibility of a REPLACE action then
362** cursors do not need to be open for indices where aIdxUsed[i]==0.
363**
364** If the isUpdate flag is true, it means that the "base" cursor is
365** initially pointing to an entry that is being updated. The isUpdate
366** flag causes extra code to be generated so that the "base" cursor
367** is still pointing at the same entry after the routine returns.
368** Without the isUpdate flag, the "base" cursor might be moved.
369*/
370void sqliteGenerateConstraintChecks(
371 Parse *pParse, /* The parser context */
372 Table *pTab, /* the table into which we are inserting */
373 int base, /* Index of a read/write cursor pointing at pTab */
374 char *aIdxUsed, /* Which indices are used. NULL means all are used */
drh0ca3e242002-01-29 23:07:02 +0000375 int recnoChng, /* True if the record number will change */
drh9cfcf5d2002-01-29 18:41:24 +0000376 int overrideError, /* Override onError to this if not OE_Default */
377 int ignoreDest, /* Jump to this label on an OE_Ignore resolution */
378 int isUpdate /* True for UPDATE, False for INSERT */
379){
380 int i;
381 Vdbe *v;
382 int nCol;
383 int onError;
384 int addr;
385 int extra;
drh0ca3e242002-01-29 23:07:02 +0000386 int iCur;
387 Index *pIdx;
388 int seenReplace = 0;
389 int jumpInst;
390 int contAddr;
drh9cfcf5d2002-01-29 18:41:24 +0000391
392 v = sqliteGetVdbe(pParse);
393 assert( v!=0 );
394 nCol = pTab->nCol;
drh0ca3e242002-01-29 23:07:02 +0000395 recnoChng = (recnoChng!=0); /* Must be either 1 or 0 */
drh9cfcf5d2002-01-29 18:41:24 +0000396
397 /* Test all NOT NULL constraints.
398 */
399 for(i=0; i<nCol; i++){
drh0ca3e242002-01-29 23:07:02 +0000400 if( i==pTab->iPKey ){
401 /* Fix me: Make sure the INTEGER PRIMARY KEY is not NULL. */
402 continue;
403 }
drh9cfcf5d2002-01-29 18:41:24 +0000404 onError = pTab->aCol[i].notNull;
drh0ca3e242002-01-29 23:07:02 +0000405 if( onError==OE_None ) continue;
drh9cfcf5d2002-01-29 18:41:24 +0000406 if( overrideError!=OE_Default ){
407 onError = overrideError;
408 }
409 if( onError==OE_Replace && pTab->aCol[i].zDflt==0 ){
410 onError = OE_Abort;
411 }
412 addr = sqliteVdbeAddOp(v, OP_Dup, nCol-i, 1);
413 sqliteVdbeAddOp(v, OP_NotNull, 0, addr+1+(onError!=OE_Abort));
414 switch( onError ){
415 case OE_Abort: {
416 sqliteVdbeAddOp(v, OP_Halt, SQLITE_CONSTRAINT, 0);
417 break;
418 }
419 case OE_Ignore: {
drh0ca3e242002-01-29 23:07:02 +0000420 sqliteVdbeAddOp(v, OP_Pop, nCol+1+recnoChng, 0);
421 sqliteVdbeAddOp(v, OP_Goto, 0, ignoreDest);
drh9cfcf5d2002-01-29 18:41:24 +0000422 break;
423 }
424 case OE_Replace: {
425 sqliteVdbeAddOp(v, OP_String, 0, 0);
426 sqliteVdbeChangeP3(v, -1, pTab->aCol[i].zDflt, P3_STATIC);
427 sqliteVdbeAddOp(v, OP_Push, nCol-i, 0);
428 break;
429 }
drh0ca3e242002-01-29 23:07:02 +0000430 default: assert(0);
drh9cfcf5d2002-01-29 18:41:24 +0000431 }
432 }
433
434 /* Test all CHECK constraints
435 */
436
437 /* Test all UNIQUE constraints. Add index records as we go.
438 */
drh0ca3e242002-01-29 23:07:02 +0000439 if( recnoChng && pTab->iPKey>=0 && pTab->keyConf!=OE_Replace
440 && overrideError!=OE_Replace ){
441 sqliteVdbeAddOp(v, OP_Dup, nCol, 1);
442 jumpInst = sqliteVdbeAddOp(v, OP_NotExists, base, 0);
443 onError = pTab->keyConf;
444 if( overrideError!=OE_Default ){
445 onError = overrideError;
446 }
447 switch( onError ){
448 case OE_Abort: {
449 sqliteVdbeAddOp(v, OP_Halt, SQLITE_CONSTRAINT, 0);
450 break;
451 }
452 case OE_Ignore: {
453 sqliteVdbeAddOp(v, OP_Pop, nCol+2, 0);
454 sqliteVdbeAddOp(v, OP_Goto, 0, ignoreDest);
455 break;
456 }
457 default: assert(0);
458 }
459 contAddr = sqliteVdbeCurrentAddr(v);
460 sqliteVdbeChangeP2(v, jumpInst, contAddr);
461 if( isUpdate ){
462 sqliteVdbeAddOp(v, OP_Dup, nCol+1, 1);
463 sqliteVdbeAddOp(v, OP_MoveTo, base, 0);
464 }
465 }
drh9cfcf5d2002-01-29 18:41:24 +0000466 extra = 0;
467 for(extra=(-1), iCur=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, iCur++){
drh9cfcf5d2002-01-29 18:41:24 +0000468 if( aIdxUsed && aIdxUsed[iCur]==0 ) continue;
469 extra++;
470 sqliteVdbeAddOp(v, OP_Dup, nCol+extra, 1);
471 for(i=0; i<pIdx->nColumn; i++){
472 int idx = pIdx->aiColumn[i];
473 if( idx==pTab->iPKey ){
drh0ca3e242002-01-29 23:07:02 +0000474 sqliteVdbeAddOp(v, OP_Dup, i+extra+nCol+1, 1);
drh9cfcf5d2002-01-29 18:41:24 +0000475 }else{
drh0ca3e242002-01-29 23:07:02 +0000476 sqliteVdbeAddOp(v, OP_Dup, i+extra+nCol-idx, 1);
drh9cfcf5d2002-01-29 18:41:24 +0000477 }
478 }
479 sqliteVdbeAddOp(v, OP_MakeIdxKey, pIdx->nColumn, 0);
480 onError = pIdx->onError;
481 if( onError==OE_None ) continue;
482 if( overrideError!=OE_Default ){
483 onError = overrideError;
484 }
drhbb37fdc2002-01-30 00:54:55 +0000485 sqliteVdbeAddOp(v, OP_Dup, extra+nCol+1+recnoChng, 1);
drh0ca3e242002-01-29 23:07:02 +0000486 jumpInst = sqliteVdbeAddOp(v, OP_IsUnique, base+iCur+1, 0);
drh9cfcf5d2002-01-29 18:41:24 +0000487 switch( onError ){
488 case OE_Abort: {
489 sqliteVdbeAddOp(v, OP_Halt, SQLITE_CONSTRAINT, 0);
490 break;
491 }
492 case OE_Ignore: {
drh0ca3e242002-01-29 23:07:02 +0000493 assert( seenReplace==0 );
494 sqliteVdbeAddOp(v, OP_Pop, nCol+extra+2+recnoChng, 0);
drh9cfcf5d2002-01-29 18:41:24 +0000495 sqliteVdbeAddOp(v, OP_Goto, 0, ignoreDest);
drh9cfcf5d2002-01-29 18:41:24 +0000496 break;
497 }
498 case OE_Replace: {
drh9cfcf5d2002-01-29 18:41:24 +0000499 sqliteGenerateRowDelete(v, pTab, base);
500 if( isUpdate ){
drh0ca3e242002-01-29 23:07:02 +0000501 sqliteVdbeAddOp(v, OP_Dup, nCol+extra+recnoChng, 1);
502 sqliteVdbeAddOp(v, OP_MoveTo, base, 0);
drh9cfcf5d2002-01-29 18:41:24 +0000503 }
drh0ca3e242002-01-29 23:07:02 +0000504 seenReplace = 1;
drh9cfcf5d2002-01-29 18:41:24 +0000505 break;
506 }
drh0ca3e242002-01-29 23:07:02 +0000507 default: assert(0);
drh9cfcf5d2002-01-29 18:41:24 +0000508 }
509 contAddr = sqliteVdbeCurrentAddr(v);
510 sqliteVdbeChangeP2(v, jumpInst, contAddr);
511 }
512}
drh0ca3e242002-01-29 23:07:02 +0000513
514/*
515** This routine generates code to finish the INSERT or UPDATE operation
516** that was started by a prior call to sqliteGenerateConstraintChecks.
517** The stack must contain keys for all active indices followed by data
518** and the recno for the new entry. This routine creates the new
519** entries in all indices and in the main table.
520**
521** The arguments to this routine should be the same as the first five
522** arguments to sqliteGenerateConstraintChecks.
523*/
524void sqliteCompleteInsertion(
525 Parse *pParse, /* The parser context */
526 Table *pTab, /* the table into which we are inserting */
527 int base, /* Index of a read/write cursor pointing at pTab */
528 char *aIdxUsed, /* Which indices are used. NULL means all are used */
529 int recnoChng /* True if the record number changed */
530){
531 int i;
532 Vdbe *v;
533 int nIdx;
534 Index *pIdx;
535
536 v = sqliteGetVdbe(pParse);
537 assert( v!=0 );
538 for(nIdx=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, nIdx++){}
539 for(i=nIdx-1; i>=0; i--){
540 if( aIdxUsed && aIdxUsed[i]==0 ) continue;
541 sqliteVdbeAddOp(v, OP_IdxPut, base+i+1, 0);
542 }
543 sqliteVdbeAddOp(v, OP_MakeRecord, pTab->nCol, 0);
544 sqliteVdbeAddOp(v, OP_PutIntKey, base, 0);
545 if( recnoChng ){
546 sqliteVdbeAddOp(v, OP_Pop, 1, 0);
547 }
548}