blob: b7617289e5ca480734d8ce6e12651c83c03f8063 [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**
drh832508b2002-03-02 17:04:07 +000015** $Id: insert.c,v 1.46 2002/03/02 17:04:08 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;
drha76b5df2002-02-23 02:32:10 +000063 pTab = sqliteTableNameToTable(pParse, zTab);
drhcce7d172000-05-31 15:34:51 +000064 sqliteFree(zTab);
drha76b5df2002-02-23 02:32:10 +000065 if( pTab==0 ) goto insert_cleanup;
drh1ccde152000-06-17 13:12:39 +000066
67 /* Allocate a VDBE
68 */
drhd8bc7082000-06-07 23:51:50 +000069 v = sqliteGetVdbe(pParse);
drh5974a302000-06-07 14:42:26 +000070 if( v==0 ) goto insert_cleanup;
drh663fc632002-02-02 18:49:19 +000071 if( pSelect ){
72 sqliteBeginMultiWriteOperation(pParse);
73 }else{
74 sqliteBeginWriteOperation(pParse);
75 }
drh1ccde152000-06-17 13:12:39 +000076
77 /* Figure out how many columns of data are supplied. If the data
drhc6b52df2002-01-04 03:09:29 +000078 ** is coming from a SELECT statement, then this step has to generate
drh1ccde152000-06-17 13:12:39 +000079 ** all the code to implement the SELECT statement and leave the data
80 ** in a temporary table. If data is coming from an expression list,
81 ** then we just have to count the number of expressions.
82 */
drh5974a302000-06-07 14:42:26 +000083 if( pSelect ){
84 int rc;
85 srcTab = pParse->nTab++;
drh99fcd712001-10-13 01:06:47 +000086 sqliteVdbeAddOp(v, OP_OpenTemp, srcTab, 0);
drh832508b2002-03-02 17:04:07 +000087 rc = sqliteSelect(pParse, pSelect, SRT_Table, srcTab, 0,0,0);
drhdaffd0e2001-04-11 14:28:42 +000088 if( rc || pParse->nErr || sqlite_malloc_failed ) goto insert_cleanup;
drh5974a302000-06-07 14:42:26 +000089 assert( pSelect->pEList );
drh967e8b72000-06-21 13:59:10 +000090 nColumn = pSelect->pEList->nExpr;
drh5974a302000-06-07 14:42:26 +000091 }else{
drhe64e7b22002-02-18 13:56:36 +000092 IdList dummy;
drhdaffd0e2001-04-11 14:28:42 +000093 assert( pList!=0 );
drh5974a302000-06-07 14:42:26 +000094 srcTab = -1;
95 assert( pList );
drh967e8b72000-06-21 13:59:10 +000096 nColumn = pList->nExpr;
drhe64e7b22002-02-18 13:56:36 +000097 dummy.nId = 0;
98 for(i=0; i<nColumn; i++){
drh832508b2002-03-02 17:04:07 +000099 if( sqliteExprResolveIds(pParse, 0, &dummy, 0, pList->a[i].pExpr) ){
drhe64e7b22002-02-18 13:56:36 +0000100 goto insert_cleanup;
101 }
102 }
drh5974a302000-06-07 14:42:26 +0000103 }
drh1ccde152000-06-17 13:12:39 +0000104
105 /* Make sure the number of columns in the source data matches the number
106 ** of columns to be inserted into the table.
107 */
drh967e8b72000-06-21 13:59:10 +0000108 if( pColumn==0 && nColumn!=pTab->nCol ){
drhcce7d172000-05-31 15:34:51 +0000109 char zNum1[30];
110 char zNum2[30];
drh967e8b72000-06-21 13:59:10 +0000111 sprintf(zNum1,"%d", nColumn);
drhcce7d172000-05-31 15:34:51 +0000112 sprintf(zNum2,"%d", pTab->nCol);
113 sqliteSetString(&pParse->zErrMsg, "table ", pTab->zName,
114 " has ", zNum2, " columns but ",
115 zNum1, " values were supplied", 0);
116 pParse->nErr++;
117 goto insert_cleanup;
118 }
drh967e8b72000-06-21 13:59:10 +0000119 if( pColumn!=0 && nColumn!=pColumn->nId ){
drhcce7d172000-05-31 15:34:51 +0000120 char zNum1[30];
121 char zNum2[30];
drh967e8b72000-06-21 13:59:10 +0000122 sprintf(zNum1,"%d", nColumn);
123 sprintf(zNum2,"%d", pColumn->nId);
drhcce7d172000-05-31 15:34:51 +0000124 sqliteSetString(&pParse->zErrMsg, zNum1, " values for ",
125 zNum2, " columns", 0);
126 pParse->nErr++;
127 goto insert_cleanup;
128 }
drh1ccde152000-06-17 13:12:39 +0000129
130 /* If the INSERT statement included an IDLIST term, then make sure
131 ** all elements of the IDLIST really are columns of the table and
132 ** remember the column indices.
drhc8392582001-12-31 02:48:51 +0000133 **
134 ** If the table has an INTEGER PRIMARY KEY column and that column
135 ** is named in the IDLIST, then record in the keyColumn variable
136 ** the index into IDLIST of the primary key column. keyColumn is
137 ** the index of the primary key as it appears in IDLIST, not as
138 ** is appears in the original table. (The index of the primary
139 ** key in the original table is pTab->iPKey.)
drh1ccde152000-06-17 13:12:39 +0000140 */
drh967e8b72000-06-21 13:59:10 +0000141 if( pColumn ){
142 for(i=0; i<pColumn->nId; i++){
143 pColumn->a[i].idx = -1;
drhcce7d172000-05-31 15:34:51 +0000144 }
drh967e8b72000-06-21 13:59:10 +0000145 for(i=0; i<pColumn->nId; i++){
drhcce7d172000-05-31 15:34:51 +0000146 for(j=0; j<pTab->nCol; j++){
drh967e8b72000-06-21 13:59:10 +0000147 if( sqliteStrICmp(pColumn->a[i].zName, pTab->aCol[j].zName)==0 ){
148 pColumn->a[i].idx = j;
drh4a324312001-12-21 14:30:42 +0000149 if( j==pTab->iPKey ){
drh9aa028d2001-12-22 21:48:29 +0000150 keyColumn = i;
drh4a324312001-12-21 14:30:42 +0000151 }
drhcce7d172000-05-31 15:34:51 +0000152 break;
153 }
154 }
155 if( j>=pTab->nCol ){
156 sqliteSetString(&pParse->zErrMsg, "table ", pTab->zName,
drh967e8b72000-06-21 13:59:10 +0000157 " has no column named ", pColumn->a[i].zName, 0);
drhcce7d172000-05-31 15:34:51 +0000158 pParse->nErr++;
159 goto insert_cleanup;
160 }
161 }
162 }
drh1ccde152000-06-17 13:12:39 +0000163
drhaacc5432002-01-06 17:07:40 +0000164 /* If there is no IDLIST term but the table has an integer primary
drhc8392582001-12-31 02:48:51 +0000165 ** key, the set the keyColumn variable to the primary key column index
166 ** in the original table definition.
drh4a324312001-12-21 14:30:42 +0000167 */
168 if( pColumn==0 ){
169 keyColumn = pTab->iPKey;
170 }
171
drh1ccde152000-06-17 13:12:39 +0000172 /* Open cursors into the table that is received the new data and
173 ** all indices of that table.
174 */
drh5974a302000-06-07 14:42:26 +0000175 base = pParse->nTab;
drhf57b3392001-10-08 13:22:32 +0000176 openOp = pTab->isTemp ? OP_OpenWrAux : OP_OpenWrite;
drh99fcd712001-10-13 01:06:47 +0000177 sqliteVdbeAddOp(v, openOp, base, pTab->tnum);
178 sqliteVdbeChangeP3(v, -1, pTab->zName, P3_STATIC);
drh5974a302000-06-07 14:42:26 +0000179 for(idx=1, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, idx++){
drh99fcd712001-10-13 01:06:47 +0000180 sqliteVdbeAddOp(v, openOp, idx+base, pIdx->tnum);
181 sqliteVdbeChangeP3(v, -1, pIdx->zName, P3_STATIC);
drh19a775c2000-06-05 18:54:46 +0000182 }
drh832508b2002-03-02 17:04:07 +0000183 pParse->nTab += idx;
drh1ccde152000-06-17 13:12:39 +0000184
185 /* If the data source is a SELECT statement, then we have to create
186 ** a loop because there might be multiple rows of data. If the data
187 ** source is an expression list, then exactly one row will be inserted
188 ** and the loop is not used.
189 */
drh5974a302000-06-07 14:42:26 +0000190 if( srcTab>=0 ){
drh1bee3d72001-10-15 00:44:35 +0000191 if( db->flags & SQLITE_CountRows ){
192 sqliteVdbeAddOp(v, OP_Integer, 0, 0); /* Initialize the row count */
193 }
drh5974a302000-06-07 14:42:26 +0000194 iBreak = sqliteVdbeMakeLabel(v);
drh6b563442001-11-07 16:48:26 +0000195 sqliteVdbeAddOp(v, OP_Rewind, srcTab, iBreak);
196 iCont = sqliteVdbeCurrentAddr(v);
drh5974a302000-06-07 14:42:26 +0000197 }
drh1ccde152000-06-17 13:12:39 +0000198
drh4a324312001-12-21 14:30:42 +0000199 /* Push the record number for the new entry onto the stack. The
200 ** record number is a randomly generate integer created by NewRecno
201 ** except when the table has an INTEGER PRIMARY KEY column, in which
drhb419a922002-01-30 16:17:23 +0000202 ** case the record number is the same as that column.
drh1ccde152000-06-17 13:12:39 +0000203 */
drh4a324312001-12-21 14:30:42 +0000204 if( keyColumn>=0 ){
205 if( srcTab>=0 ){
206 sqliteVdbeAddOp(v, OP_Column, srcTab, keyColumn);
207 }else{
208 sqliteExprCode(pParse, pList->a[keyColumn].pExpr);
209 }
drh8aff1012001-12-22 14:49:24 +0000210 sqliteVdbeAddOp(v, OP_MustBeInt, 0, 0);
drh4a324312001-12-21 14:30:42 +0000211 }else{
212 sqliteVdbeAddOp(v, OP_NewRecno, base, 0);
213 }
drh4a324312001-12-21 14:30:42 +0000214
drhaacc5432002-01-06 17:07:40 +0000215 /* Push onto the stack, data for all columns of the new entry, beginning
drh4a324312001-12-21 14:30:42 +0000216 ** with the first column.
217 */
drh5974a302000-06-07 14:42:26 +0000218 for(i=0; i<pTab->nCol; i++){
drh4a324312001-12-21 14:30:42 +0000219 if( i==pTab->iPKey ){
220 /* The value of the INTEGER PRIMARY KEY column is always a NULL.
drhaacc5432002-01-06 17:07:40 +0000221 ** Whenever this column is read, the record number will be substituted
222 ** in its place. So will fill this column with a NULL to avoid
223 ** taking up data space with information that will never be used. */
drh4a324312001-12-21 14:30:42 +0000224 sqliteVdbeAddOp(v, OP_String, 0, 0);
225 continue;
226 }
drh967e8b72000-06-21 13:59:10 +0000227 if( pColumn==0 ){
drh5974a302000-06-07 14:42:26 +0000228 j = i;
229 }else{
drh967e8b72000-06-21 13:59:10 +0000230 for(j=0; j<pColumn->nId; j++){
231 if( pColumn->a[j].idx==i ) break;
drh5974a302000-06-07 14:42:26 +0000232 }
drhbed86902000-06-02 13:27:59 +0000233 }
drh967e8b72000-06-21 13:59:10 +0000234 if( pColumn && j>=pColumn->nId ){
drh99fcd712001-10-13 01:06:47 +0000235 sqliteVdbeAddOp(v, OP_String, 0, 0);
236 sqliteVdbeChangeP3(v, -1, pTab->aCol[i].zDflt, P3_STATIC);
drh5974a302000-06-07 14:42:26 +0000237 }else if( srcTab>=0 ){
drh24e97df2002-02-03 19:06:02 +0000238 sqliteVdbeAddOp(v, OP_Column, srcTab, j);
drh5974a302000-06-07 14:42:26 +0000239 }else{
240 sqliteExprCode(pParse, pList->a[j].pExpr);
241 }
242 }
drh1ccde152000-06-17 13:12:39 +0000243
drh0ca3e242002-01-29 23:07:02 +0000244 /* Generate code to check constraints and generate index keys and
245 ** do the insertion.
drh4a324312001-12-21 14:30:42 +0000246 */
drh0ca3e242002-01-29 23:07:02 +0000247 endOfLoop = sqliteVdbeMakeLabel(v);
drhb419a922002-01-30 16:17:23 +0000248 sqliteGenerateConstraintChecks(pParse, pTab, base, 0,0,0, onError, endOfLoop);
249 sqliteCompleteInsertion(pParse, pTab, base, 0,0,0);
drh1bee3d72001-10-15 00:44:35 +0000250
251 /* If inserting from a SELECT, keep a count of the number of
252 ** rows inserted.
253 */
254 if( srcTab>=0 && (db->flags & SQLITE_CountRows)!=0 ){
255 sqliteVdbeAddOp(v, OP_AddImm, 1, 0);
256 }
257
drh1ccde152000-06-17 13:12:39 +0000258 /* The bottom of the loop, if the data source is a SELECT statement
259 */
drh0ca3e242002-01-29 23:07:02 +0000260 sqliteVdbeResolveLabel(v, endOfLoop);
drh5974a302000-06-07 14:42:26 +0000261 if( srcTab>=0 ){
drh6b563442001-11-07 16:48:26 +0000262 sqliteVdbeAddOp(v, OP_Next, srcTab, iCont);
drh99fcd712001-10-13 01:06:47 +0000263 sqliteVdbeResolveLabel(v, iBreak);
drh6b563442001-11-07 16:48:26 +0000264 sqliteVdbeAddOp(v, OP_Close, srcTab, 0);
265 }
266 sqliteVdbeAddOp(v, OP_Close, base, 0);
267 for(idx=1, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, idx++){
268 sqliteVdbeAddOp(v, OP_Close, idx+base, 0);
drhcce7d172000-05-31 15:34:51 +0000269 }
drh1c928532002-01-31 15:54:21 +0000270 sqliteEndWriteOperation(pParse);
drh5e00f6c2001-09-13 13:46:56 +0000271
drh1bee3d72001-10-15 00:44:35 +0000272 /*
273 ** Return the number of rows inserted.
274 */
275 if( db->flags & SQLITE_CountRows ){
276 sqliteVdbeAddOp(v, OP_ColumnCount, 1, 0);
277 sqliteVdbeAddOp(v, OP_ColumnName, 0, 0);
278 sqliteVdbeChangeP3(v, -1, "rows inserted", P3_STATIC);
279 if( srcTab<0 ){
280 sqliteVdbeAddOp(v, OP_Integer, 1, 0);
281 }
282 sqliteVdbeAddOp(v, OP_Callback, 1, 0);
283 }
drhcce7d172000-05-31 15:34:51 +0000284
285insert_cleanup:
drh5974a302000-06-07 14:42:26 +0000286 if( pList ) sqliteExprListDelete(pList);
287 if( pSelect ) sqliteSelectDelete(pSelect);
drh967e8b72000-06-21 13:59:10 +0000288 sqliteIdListDelete(pColumn);
drhcce7d172000-05-31 15:34:51 +0000289}
drh9cfcf5d2002-01-29 18:41:24 +0000290
drh9cfcf5d2002-01-29 18:41:24 +0000291/*
292** Generate code to do a constraint check prior to an INSERT or an UPDATE.
293**
294** When this routine is called, the stack contains (from bottom to top)
drh0ca3e242002-01-29 23:07:02 +0000295** the following values:
296**
drhb419a922002-01-30 16:17:23 +0000297** 1. The recno of the row to be updated before it is updated. This
298** value is omitted unless we are doing an UPDATE that involves a
299** change to the record number.
drh0ca3e242002-01-29 23:07:02 +0000300**
drhb419a922002-01-30 16:17:23 +0000301** 2. The recno of the row after the update.
drh0ca3e242002-01-29 23:07:02 +0000302**
303** 3. The data in the first column of the entry after the update.
304**
305** i. Data from middle columns...
306**
307** N. The data in the last column of the entry after the update.
308**
drhb419a922002-01-30 16:17:23 +0000309** The old recno shown as entry (1) above is omitted unless both isUpdate
drh1c928532002-01-31 15:54:21 +0000310** and recnoChng are 1. isUpdate is true for UPDATEs and false for
311** INSERTs and recnoChng is true if the record number is being changed.
drh0ca3e242002-01-29 23:07:02 +0000312**
313** The code generated by this routine pushes additional entries onto
314** the stack which are the keys for new index entries for the new record.
315** The order of index keys is the same as the order of the indices on
316** the pTable->pIndex list. A key is only created for index i if
317** aIdxUsed!=0 and aIdxUsed[i]!=0.
drh9cfcf5d2002-01-29 18:41:24 +0000318**
319** This routine also generates code to check constraints. NOT NULL,
320** CHECK, and UNIQUE constraints are all checked. If a constraint fails,
drh1c928532002-01-31 15:54:21 +0000321** then the appropriate action is performed. There are five possible
322** actions: ROLLBACK, ABORT, FAIL, REPLACE, and IGNORE.
drh9cfcf5d2002-01-29 18:41:24 +0000323**
324** Constraint type Action What Happens
325** --------------- ---------- ----------------------------------------
drh1c928532002-01-31 15:54:21 +0000326** any ROLLBACK The current transaction is rolled back and
drh9cfcf5d2002-01-29 18:41:24 +0000327** sqlite_exec() returns immediately with a
328** return code of SQLITE_CONSTRAINT.
329**
drh1c928532002-01-31 15:54:21 +0000330** any ABORT Back out changes from the current command
331** only (do not do a complete rollback) then
332** cause sqlite_exec() to return immediately
333** with SQLITE_CONSTRAINT.
334**
335** any FAIL Sqlite_exec() returns immediately with a
336** return code of SQLITE_CONSTRAINT. The
337** transaction is not rolled back and any
338** prior changes are retained.
339**
drh9cfcf5d2002-01-29 18:41:24 +0000340** any IGNORE The record number and data is popped from
341** the stack and there is an immediate jump
342** to label ignoreDest.
343**
344** NOT NULL REPLACE The NULL value is replace by the default
345** value for that column. If the default value
346** is NULL, the action is the same as ABORT.
347**
348** UNIQUE REPLACE The other row that conflicts with the row
349** being inserted is removed.
350**
351** CHECK REPLACE Illegal. The results in an exception.
352**
drh1c928532002-01-31 15:54:21 +0000353** Which action to take is determined by the overrideError parameter.
354** Or if overrideError==OE_Default, then the pParse->onError parameter
355** is used. Or if pParse->onError==OE_Default then the onError value
356** for the constraint is used.
drh9cfcf5d2002-01-29 18:41:24 +0000357**
drhaaab5722002-02-19 13:39:21 +0000358** The calling routine must open a read/write cursor for pTab with
drh9cfcf5d2002-01-29 18:41:24 +0000359** 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 */
drhb419a922002-01-30 16:17:23 +0000376 int isUpdate, /* True for UPDATE, False for INSERT */
drh9cfcf5d2002-01-29 18:41:24 +0000377 int overrideError, /* Override onError to this if not OE_Default */
drhb419a922002-01-30 16:17:23 +0000378 int ignoreDest /* Jump to this label on an OE_Ignore resolution */
drh9cfcf5d2002-01-29 18:41:24 +0000379){
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;
drhb419a922002-01-30 16:17:23 +0000391 int hasTwoRecnos = (isUpdate && recnoChng);
drh9cfcf5d2002-01-29 18:41:24 +0000392
393 v = sqliteGetVdbe(pParse);
394 assert( v!=0 );
395 nCol = pTab->nCol;
396
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;
drh1c928532002-01-31 15:54:21 +0000408 }else if( onError==OE_Default ){
drh0d65dc02002-02-03 00:56:09 +0000409 onError = pParse->db->onError;
410 if( onError==OE_Default ) onError = OE_Abort;
drh9cfcf5d2002-01-29 18:41:24 +0000411 }
412 if( onError==OE_Replace && pTab->aCol[i].zDflt==0 ){
413 onError = OE_Abort;
414 }
drhef6764a2002-01-30 04:32:00 +0000415 sqliteVdbeAddOp(v, OP_Dup, nCol-1-i, 1);
416 addr = sqliteVdbeAddOp(v, OP_NotNull, 0, 0);
drh9cfcf5d2002-01-29 18:41:24 +0000417 switch( onError ){
drh1c928532002-01-31 15:54:21 +0000418 case OE_Rollback:
419 case OE_Abort:
420 case OE_Fail: {
421 sqliteVdbeAddOp(v, OP_Halt, SQLITE_CONSTRAINT, onError);
drh9cfcf5d2002-01-29 18:41:24 +0000422 break;
423 }
424 case OE_Ignore: {
drhb419a922002-01-30 16:17:23 +0000425 sqliteVdbeAddOp(v, OP_Pop, nCol+1+hasTwoRecnos, 0);
drh0ca3e242002-01-29 23:07:02 +0000426 sqliteVdbeAddOp(v, OP_Goto, 0, ignoreDest);
drh9cfcf5d2002-01-29 18:41:24 +0000427 break;
428 }
429 case OE_Replace: {
430 sqliteVdbeAddOp(v, OP_String, 0, 0);
431 sqliteVdbeChangeP3(v, -1, pTab->aCol[i].zDflt, P3_STATIC);
432 sqliteVdbeAddOp(v, OP_Push, nCol-i, 0);
433 break;
434 }
drh0ca3e242002-01-29 23:07:02 +0000435 default: assert(0);
drh9cfcf5d2002-01-29 18:41:24 +0000436 }
drhef6764a2002-01-30 04:32:00 +0000437 sqliteVdbeChangeP2(v, addr, sqliteVdbeCurrentAddr(v));
drh9cfcf5d2002-01-29 18:41:24 +0000438 }
439
440 /* Test all CHECK constraints
441 */
442
443 /* Test all UNIQUE constraints. Add index records as we go.
444 */
drh0d65dc02002-02-03 00:56:09 +0000445 if( (recnoChng || !isUpdate) && pTab->iPKey>=0 ){
drh0ca3e242002-01-29 23:07:02 +0000446 onError = pTab->keyConf;
447 if( overrideError!=OE_Default ){
448 onError = overrideError;
drh1c928532002-01-31 15:54:21 +0000449 }else if( onError==OE_Default ){
drh0d65dc02002-02-03 00:56:09 +0000450 onError = pParse->db->onError;
451 if( onError==OE_Default ) onError = OE_Abort;
drh0ca3e242002-01-29 23:07:02 +0000452 }
drh0d65dc02002-02-03 00:56:09 +0000453 if( onError!=OE_Replace ){
454 sqliteVdbeAddOp(v, OP_Dup, nCol, 1);
455 jumpInst = sqliteVdbeAddOp(v, OP_NotExists, base, 0);
456 switch( onError ){
457 case OE_Rollback:
458 case OE_Abort:
459 case OE_Fail: {
460 sqliteVdbeAddOp(v, OP_Halt, SQLITE_CONSTRAINT, onError);
461 break;
462 }
463 case OE_Ignore: {
464 sqliteVdbeAddOp(v, OP_Pop, nCol+1+hasTwoRecnos, 0);
465 sqliteVdbeAddOp(v, OP_Goto, 0, ignoreDest);
466 break;
467 }
468 default: assert(0);
drh0ca3e242002-01-29 23:07:02 +0000469 }
drh0d65dc02002-02-03 00:56:09 +0000470 contAddr = sqliteVdbeCurrentAddr(v);
471 sqliteVdbeChangeP2(v, jumpInst, contAddr);
472 if( isUpdate ){
473 sqliteVdbeAddOp(v, OP_Dup, nCol+1, 1);
474 sqliteVdbeAddOp(v, OP_MoveTo, base, 0);
drh0ca3e242002-01-29 23:07:02 +0000475 }
drh0ca3e242002-01-29 23:07:02 +0000476 }
477 }
drh9cfcf5d2002-01-29 18:41:24 +0000478 extra = 0;
479 for(extra=(-1), iCur=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, iCur++){
drh9cfcf5d2002-01-29 18:41:24 +0000480 if( aIdxUsed && aIdxUsed[iCur]==0 ) continue;
481 extra++;
482 sqliteVdbeAddOp(v, OP_Dup, nCol+extra, 1);
483 for(i=0; i<pIdx->nColumn; i++){
484 int idx = pIdx->aiColumn[i];
485 if( idx==pTab->iPKey ){
drh0ca3e242002-01-29 23:07:02 +0000486 sqliteVdbeAddOp(v, OP_Dup, i+extra+nCol+1, 1);
drh9cfcf5d2002-01-29 18:41:24 +0000487 }else{
drh0ca3e242002-01-29 23:07:02 +0000488 sqliteVdbeAddOp(v, OP_Dup, i+extra+nCol-idx, 1);
drh9cfcf5d2002-01-29 18:41:24 +0000489 }
490 }
491 sqliteVdbeAddOp(v, OP_MakeIdxKey, pIdx->nColumn, 0);
492 onError = pIdx->onError;
493 if( onError==OE_None ) continue;
494 if( overrideError!=OE_Default ){
495 onError = overrideError;
drh1c928532002-01-31 15:54:21 +0000496 }else if( onError==OE_Default ){
drh0d65dc02002-02-03 00:56:09 +0000497 onError = pParse->db->onError;
498 if( onError==OE_Default ) onError = OE_Abort;
drh9cfcf5d2002-01-29 18:41:24 +0000499 }
drhb419a922002-01-30 16:17:23 +0000500 sqliteVdbeAddOp(v, OP_Dup, extra+nCol+1+hasTwoRecnos, 1);
drh0ca3e242002-01-29 23:07:02 +0000501 jumpInst = sqliteVdbeAddOp(v, OP_IsUnique, base+iCur+1, 0);
drh9cfcf5d2002-01-29 18:41:24 +0000502 switch( onError ){
drh1c928532002-01-31 15:54:21 +0000503 case OE_Rollback:
504 case OE_Abort:
505 case OE_Fail: {
506 sqliteVdbeAddOp(v, OP_Halt, SQLITE_CONSTRAINT, onError);
drh9cfcf5d2002-01-29 18:41:24 +0000507 break;
508 }
509 case OE_Ignore: {
drh0ca3e242002-01-29 23:07:02 +0000510 assert( seenReplace==0 );
drhb419a922002-01-30 16:17:23 +0000511 sqliteVdbeAddOp(v, OP_Pop, nCol+extra+2+hasTwoRecnos, 0);
drh9cfcf5d2002-01-29 18:41:24 +0000512 sqliteVdbeAddOp(v, OP_Goto, 0, ignoreDest);
drh9cfcf5d2002-01-29 18:41:24 +0000513 break;
514 }
515 case OE_Replace: {
drh9cfcf5d2002-01-29 18:41:24 +0000516 sqliteGenerateRowDelete(v, pTab, base);
517 if( isUpdate ){
drhb419a922002-01-30 16:17:23 +0000518 sqliteVdbeAddOp(v, OP_Dup, nCol+extra+1+hasTwoRecnos, 1);
drh0ca3e242002-01-29 23:07:02 +0000519 sqliteVdbeAddOp(v, OP_MoveTo, base, 0);
drh9cfcf5d2002-01-29 18:41:24 +0000520 }
drh0ca3e242002-01-29 23:07:02 +0000521 seenReplace = 1;
drh9cfcf5d2002-01-29 18:41:24 +0000522 break;
523 }
drh0ca3e242002-01-29 23:07:02 +0000524 default: assert(0);
drh9cfcf5d2002-01-29 18:41:24 +0000525 }
526 contAddr = sqliteVdbeCurrentAddr(v);
527 sqliteVdbeChangeP2(v, jumpInst, contAddr);
528 }
529}
drh0ca3e242002-01-29 23:07:02 +0000530
531/*
532** This routine generates code to finish the INSERT or UPDATE operation
533** that was started by a prior call to sqliteGenerateConstraintChecks.
534** The stack must contain keys for all active indices followed by data
535** and the recno for the new entry. This routine creates the new
536** entries in all indices and in the main table.
537**
drhb419a922002-01-30 16:17:23 +0000538** The arguments to this routine should be the same as the first six
drh0ca3e242002-01-29 23:07:02 +0000539** arguments to sqliteGenerateConstraintChecks.
540*/
541void sqliteCompleteInsertion(
542 Parse *pParse, /* The parser context */
543 Table *pTab, /* the table into which we are inserting */
544 int base, /* Index of a read/write cursor pointing at pTab */
545 char *aIdxUsed, /* Which indices are used. NULL means all are used */
drhb419a922002-01-30 16:17:23 +0000546 int recnoChng, /* True if the record number will change */
547 int isUpdate /* True for UPDATE, False for INSERT */
drh0ca3e242002-01-29 23:07:02 +0000548){
549 int i;
550 Vdbe *v;
551 int nIdx;
552 Index *pIdx;
553
554 v = sqliteGetVdbe(pParse);
555 assert( v!=0 );
556 for(nIdx=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, nIdx++){}
557 for(i=nIdx-1; i>=0; i--){
558 if( aIdxUsed && aIdxUsed[i]==0 ) continue;
559 sqliteVdbeAddOp(v, OP_IdxPut, base+i+1, 0);
560 }
561 sqliteVdbeAddOp(v, OP_MakeRecord, pTab->nCol, 0);
562 sqliteVdbeAddOp(v, OP_PutIntKey, base, 0);
drhb419a922002-01-30 16:17:23 +0000563 if( isUpdate && recnoChng ){
drh0ca3e242002-01-29 23:07:02 +0000564 sqliteVdbeAddOp(v, OP_Pop, 1, 0);
565 }
566}