blob: fab85eddb6ef56f47e4b04d721e402b2da7158c7 [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**
danielk1977f29ce552002-05-19 23:43:12 +000015** $Id: insert.c,v 1.55 2002/05/19 23:43:14 danielk1977 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 */
danielk1977c3f9bad2002-05-15 08:30:12 +000043 char *zTab = 0; /* Name of the table into which we are inserting */
drh5974a302000-06-07 14:42:26 +000044 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
danielk1977c3f9bad2002-05-15 08:30:12 +000056 int row_triggers_exist = 0; /* True if there are FOR EACH ROW triggers */
57 int newIdx = -1;
58
drhdaffd0e2001-04-11 14:28:42 +000059 if( pParse->nErr || sqlite_malloc_failed ) goto insert_cleanup;
drhecdc7532001-09-23 02:35:53 +000060 db = pParse->db;
drhdaffd0e2001-04-11 14:28:42 +000061
drh1ccde152000-06-17 13:12:39 +000062 /* Locate the table into which we will be inserting new information.
63 */
drhcce7d172000-05-31 15:34:51 +000064 zTab = sqliteTableNameFromToken(pTableName);
drhdaffd0e2001-04-11 14:28:42 +000065 if( zTab==0 ) goto insert_cleanup;
danielk1977c3f9bad2002-05-15 08:30:12 +000066 pTab = sqliteFindTable(pParse->db, zTab);
67 if( pTab==0 ){
68 sqliteSetString(&pParse->zErrMsg, "no such table: ", zTab, 0);
69 pParse->nErr++;
70 goto insert_cleanup;
71 }
72
73 /* Ensure that:
74 * (a) the table is not read-only,
75 * (b) that if it is a view then ON INSERT triggers exist
76 */
77 row_triggers_exist =
78 sqliteTriggersExist(pParse, pTab->pTrigger, TK_INSERT,
drh9adf9ac2002-05-15 11:44:13 +000079 TK_BEFORE, TK_ROW, 0) ||
danielk1977c3f9bad2002-05-15 08:30:12 +000080 sqliteTriggersExist(pParse, pTab->pTrigger, TK_INSERT, TK_AFTER, TK_ROW, 0);
81 if( pTab->readOnly || (pTab->pSelect && !row_triggers_exist) ){
82 sqliteSetString(&pParse->zErrMsg,
83 pTab->pSelect ? "view " : "table ",
84 zTab,
85 " may not be modified", 0);
86 pParse->nErr++;
87 goto insert_cleanup;
88 }
drhcce7d172000-05-31 15:34:51 +000089 sqliteFree(zTab);
danielk1977c3f9bad2002-05-15 08:30:12 +000090 zTab = 0;
91
drha76b5df2002-02-23 02:32:10 +000092 if( pTab==0 ) goto insert_cleanup;
drh1ccde152000-06-17 13:12:39 +000093
94 /* Allocate a VDBE
95 */
drhd8bc7082000-06-07 23:51:50 +000096 v = sqliteGetVdbe(pParse);
drh5974a302000-06-07 14:42:26 +000097 if( v==0 ) goto insert_cleanup;
danielk1977c3f9bad2002-05-15 08:30:12 +000098 if( pSelect || row_triggers_exist ){
drh663fc632002-02-02 18:49:19 +000099 sqliteBeginMultiWriteOperation(pParse);
100 }else{
101 sqliteBeginWriteOperation(pParse);
102 }
drh1ccde152000-06-17 13:12:39 +0000103
danielk1977c3f9bad2002-05-15 08:30:12 +0000104 /* if there are row triggers, allocate a temp table for new.* references. */
danielk1977f29ce552002-05-19 23:43:12 +0000105 if( row_triggers_exist ){
danielk1977c3f9bad2002-05-15 08:30:12 +0000106 newIdx = pParse->nTab++;
danielk1977f29ce552002-05-19 23:43:12 +0000107 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000108
drh1ccde152000-06-17 13:12:39 +0000109 /* Figure out how many columns of data are supplied. If the data
drhc6b52df2002-01-04 03:09:29 +0000110 ** is coming from a SELECT statement, then this step has to generate
drh1ccde152000-06-17 13:12:39 +0000111 ** all the code to implement the SELECT statement and leave the data
112 ** in a temporary table. If data is coming from an expression list,
113 ** then we just have to count the number of expressions.
114 */
drh5974a302000-06-07 14:42:26 +0000115 if( pSelect ){
116 int rc;
117 srcTab = pParse->nTab++;
drh99fcd712001-10-13 01:06:47 +0000118 sqliteVdbeAddOp(v, OP_OpenTemp, srcTab, 0);
drh832508b2002-03-02 17:04:07 +0000119 rc = sqliteSelect(pParse, pSelect, SRT_Table, srcTab, 0,0,0);
drhdaffd0e2001-04-11 14:28:42 +0000120 if( rc || pParse->nErr || sqlite_malloc_failed ) goto insert_cleanup;
drh5974a302000-06-07 14:42:26 +0000121 assert( pSelect->pEList );
drh967e8b72000-06-21 13:59:10 +0000122 nColumn = pSelect->pEList->nExpr;
drh5974a302000-06-07 14:42:26 +0000123 }else{
drhe64e7b22002-02-18 13:56:36 +0000124 IdList dummy;
drhdaffd0e2001-04-11 14:28:42 +0000125 assert( pList!=0 );
drh5974a302000-06-07 14:42:26 +0000126 srcTab = -1;
127 assert( pList );
drh967e8b72000-06-21 13:59:10 +0000128 nColumn = pList->nExpr;
drhe64e7b22002-02-18 13:56:36 +0000129 dummy.nId = 0;
130 for(i=0; i<nColumn; i++){
drh832508b2002-03-02 17:04:07 +0000131 if( sqliteExprResolveIds(pParse, 0, &dummy, 0, pList->a[i].pExpr) ){
drhe64e7b22002-02-18 13:56:36 +0000132 goto insert_cleanup;
133 }
drhb04a5d82002-04-12 03:55:15 +0000134 if( sqliteExprCheck(pParse, pList->a[i].pExpr, 0, 0) ){
135 goto insert_cleanup;
136 }
drhe64e7b22002-02-18 13:56:36 +0000137 }
drh5974a302000-06-07 14:42:26 +0000138 }
drh1ccde152000-06-17 13:12:39 +0000139
140 /* Make sure the number of columns in the source data matches the number
141 ** of columns to be inserted into the table.
142 */
drh967e8b72000-06-21 13:59:10 +0000143 if( pColumn==0 && nColumn!=pTab->nCol ){
drhcce7d172000-05-31 15:34:51 +0000144 char zNum1[30];
145 char zNum2[30];
drh967e8b72000-06-21 13:59:10 +0000146 sprintf(zNum1,"%d", nColumn);
drhcce7d172000-05-31 15:34:51 +0000147 sprintf(zNum2,"%d", pTab->nCol);
148 sqliteSetString(&pParse->zErrMsg, "table ", pTab->zName,
149 " has ", zNum2, " columns but ",
150 zNum1, " values were supplied", 0);
151 pParse->nErr++;
152 goto insert_cleanup;
153 }
drh967e8b72000-06-21 13:59:10 +0000154 if( pColumn!=0 && nColumn!=pColumn->nId ){
drhcce7d172000-05-31 15:34:51 +0000155 char zNum1[30];
156 char zNum2[30];
drh967e8b72000-06-21 13:59:10 +0000157 sprintf(zNum1,"%d", nColumn);
158 sprintf(zNum2,"%d", pColumn->nId);
drhcce7d172000-05-31 15:34:51 +0000159 sqliteSetString(&pParse->zErrMsg, zNum1, " values for ",
160 zNum2, " columns", 0);
161 pParse->nErr++;
162 goto insert_cleanup;
163 }
drh1ccde152000-06-17 13:12:39 +0000164
165 /* If the INSERT statement included an IDLIST term, then make sure
166 ** all elements of the IDLIST really are columns of the table and
167 ** remember the column indices.
drhc8392582001-12-31 02:48:51 +0000168 **
169 ** If the table has an INTEGER PRIMARY KEY column and that column
170 ** is named in the IDLIST, then record in the keyColumn variable
171 ** the index into IDLIST of the primary key column. keyColumn is
172 ** the index of the primary key as it appears in IDLIST, not as
173 ** is appears in the original table. (The index of the primary
174 ** key in the original table is pTab->iPKey.)
drh1ccde152000-06-17 13:12:39 +0000175 */
drh967e8b72000-06-21 13:59:10 +0000176 if( pColumn ){
177 for(i=0; i<pColumn->nId; i++){
178 pColumn->a[i].idx = -1;
drhcce7d172000-05-31 15:34:51 +0000179 }
drh967e8b72000-06-21 13:59:10 +0000180 for(i=0; i<pColumn->nId; i++){
drhcce7d172000-05-31 15:34:51 +0000181 for(j=0; j<pTab->nCol; j++){
drh967e8b72000-06-21 13:59:10 +0000182 if( sqliteStrICmp(pColumn->a[i].zName, pTab->aCol[j].zName)==0 ){
183 pColumn->a[i].idx = j;
drh4a324312001-12-21 14:30:42 +0000184 if( j==pTab->iPKey ){
drh9aa028d2001-12-22 21:48:29 +0000185 keyColumn = i;
drh4a324312001-12-21 14:30:42 +0000186 }
drhcce7d172000-05-31 15:34:51 +0000187 break;
188 }
189 }
190 if( j>=pTab->nCol ){
191 sqliteSetString(&pParse->zErrMsg, "table ", pTab->zName,
drh967e8b72000-06-21 13:59:10 +0000192 " has no column named ", pColumn->a[i].zName, 0);
drhcce7d172000-05-31 15:34:51 +0000193 pParse->nErr++;
194 goto insert_cleanup;
195 }
196 }
197 }
drh1ccde152000-06-17 13:12:39 +0000198
drhaacc5432002-01-06 17:07:40 +0000199 /* If there is no IDLIST term but the table has an integer primary
drhc8392582001-12-31 02:48:51 +0000200 ** key, the set the keyColumn variable to the primary key column index
201 ** in the original table definition.
drh4a324312001-12-21 14:30:42 +0000202 */
203 if( pColumn==0 ){
204 keyColumn = pTab->iPKey;
205 }
206
danielk1977c3f9bad2002-05-15 08:30:12 +0000207 /* Open the temp table for FOR EACH ROW triggers */
danielk1977f29ce552002-05-19 23:43:12 +0000208 if( row_triggers_exist ){
danielk1977c3f9bad2002-05-15 08:30:12 +0000209 sqliteVdbeAddOp(v, OP_OpenTemp, newIdx, 0);
danielk1977f29ce552002-05-19 23:43:12 +0000210 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000211
drhfeeb1392002-04-09 03:28:01 +0000212 /* Initialize the count of rows to be inserted
213 */
danielk1977f29ce552002-05-19 23:43:12 +0000214 if( db->flags & SQLITE_CountRows && !pParse->trigStack ){
drhfeeb1392002-04-09 03:28:01 +0000215 sqliteVdbeAddOp(v, OP_Integer, 0, 0); /* Initialize the row count */
216 }
217
danielk1977c3f9bad2002-05-15 08:30:12 +0000218 /* Open tables and indices if there are no row triggers */
danielk1977f29ce552002-05-19 23:43:12 +0000219 if( !row_triggers_exist ){
danielk1977c3f9bad2002-05-15 08:30:12 +0000220 base = pParse->nTab;
221 openOp = pTab->isTemp ? OP_OpenWrAux : OP_OpenWrite;
222 sqliteVdbeAddOp(v, openOp, base, pTab->tnum);
223 sqliteVdbeChangeP3(v, -1, pTab->zName, P3_STATIC);
224 for(idx=1, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, idx++){
225 sqliteVdbeAddOp(v, openOp, idx+base, pIdx->tnum);
226 sqliteVdbeChangeP3(v, -1, pIdx->zName, P3_STATIC);
227 }
228 pParse->nTab += idx;
229 }
230
drh1ccde152000-06-17 13:12:39 +0000231 /* If the data source is a SELECT statement, then we have to create
232 ** a loop because there might be multiple rows of data. If the data
233 ** source is an expression list, then exactly one row will be inserted
234 ** and the loop is not used.
235 */
drh5974a302000-06-07 14:42:26 +0000236 if( srcTab>=0 ){
drh5974a302000-06-07 14:42:26 +0000237 iBreak = sqliteVdbeMakeLabel(v);
drh6b563442001-11-07 16:48:26 +0000238 sqliteVdbeAddOp(v, OP_Rewind, srcTab, iBreak);
239 iCont = sqliteVdbeCurrentAddr(v);
drh5974a302000-06-07 14:42:26 +0000240 }
drh1ccde152000-06-17 13:12:39 +0000241
danielk1977f29ce552002-05-19 23:43:12 +0000242 if( row_triggers_exist ){
danielk1977c3f9bad2002-05-15 08:30:12 +0000243
244 /* build the new.* reference row */
245 sqliteVdbeAddOp(v, OP_Integer, 13, 0);
246 for(i=0; i<pTab->nCol; i++){
247 if( pColumn==0 ){
drh9adf9ac2002-05-15 11:44:13 +0000248 j = i;
danielk1977c3f9bad2002-05-15 08:30:12 +0000249 }else{
drh9adf9ac2002-05-15 11:44:13 +0000250 for(j=0; j<pColumn->nId; j++){
251 if( pColumn->a[j].idx==i ) break;
252 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000253 }
254 if( pColumn && j>=pColumn->nId ){
drh9adf9ac2002-05-15 11:44:13 +0000255 sqliteVdbeAddOp(v, OP_String, 0, 0);
256 sqliteVdbeChangeP3(v, -1, pTab->aCol[i].zDflt, P3_STATIC);
danielk1977c3f9bad2002-05-15 08:30:12 +0000257 }else if( srcTab>=0 ){
drh9adf9ac2002-05-15 11:44:13 +0000258 sqliteVdbeAddOp(v, OP_Column, srcTab, j);
danielk1977c3f9bad2002-05-15 08:30:12 +0000259 }else{
drh9adf9ac2002-05-15 11:44:13 +0000260 sqliteExprCode(pParse, pList->a[j].pExpr);
danielk1977c3f9bad2002-05-15 08:30:12 +0000261 }
262 }
263 sqliteVdbeAddOp(v, OP_MakeRecord, pTab->nCol, 0);
264 sqliteVdbeAddOp(v, OP_PutIntKey, newIdx, 0);
265 sqliteVdbeAddOp(v, OP_Rewind, newIdx, 0);
266
267 /* Fire BEFORE triggers */
danielk1977f29ce552002-05-19 23:43:12 +0000268 if( sqliteCodeRowTrigger(pParse, TK_INSERT, 0, TK_BEFORE, pTab, newIdx, -1,
269 onError) ){
270 goto insert_cleanup;
271 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000272
273 /* Open the tables and indices for the INSERT */
danielk1977f29ce552002-05-19 23:43:12 +0000274 if( !pTab->pSelect ){
danielk1977c3f9bad2002-05-15 08:30:12 +0000275 base = pParse->nTab;
276 openOp = pTab->isTemp ? OP_OpenWrAux : OP_OpenWrite;
277 sqliteVdbeAddOp(v, openOp, base, pTab->tnum);
278 sqliteVdbeChangeP3(v, -1, pTab->zName, P3_STATIC);
279 for(idx=1, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, idx++){
drh9adf9ac2002-05-15 11:44:13 +0000280 sqliteVdbeAddOp(v, openOp, idx+base, pIdx->tnum);
281 sqliteVdbeChangeP3(v, -1, pIdx->zName, P3_STATIC);
danielk1977c3f9bad2002-05-15 08:30:12 +0000282 }
283 pParse->nTab += idx;
284 }
285 }
286
drh4a324312001-12-21 14:30:42 +0000287 /* Push the record number for the new entry onto the stack. The
288 ** record number is a randomly generate integer created by NewRecno
289 ** except when the table has an INTEGER PRIMARY KEY column, in which
drhb419a922002-01-30 16:17:23 +0000290 ** case the record number is the same as that column.
drh1ccde152000-06-17 13:12:39 +0000291 */
danielk1977f29ce552002-05-19 23:43:12 +0000292 if( !pTab->pSelect ){
danielk1977c3f9bad2002-05-15 08:30:12 +0000293 if( keyColumn>=0 ){
294 if( srcTab>=0 ){
drh9adf9ac2002-05-15 11:44:13 +0000295 sqliteVdbeAddOp(v, OP_Column, srcTab, keyColumn);
danielk1977c3f9bad2002-05-15 08:30:12 +0000296 }else{
drh9adf9ac2002-05-15 11:44:13 +0000297 int addr;
298 sqliteExprCode(pParse, pList->a[keyColumn].pExpr);
drhe1e68f42002-03-31 18:29:03 +0000299
drh9adf9ac2002-05-15 11:44:13 +0000300 /* If the PRIMARY KEY expression is NULL, then use OP_NewRecno
danielk1977f29ce552002-05-19 23:43:12 +0000301 ** to generate a unique primary key value.
302 */
drh9adf9ac2002-05-15 11:44:13 +0000303 addr = sqliteVdbeAddOp(v, OP_Dup, 0, 1);
304 sqliteVdbeAddOp(v, OP_NotNull, 0, addr+4);
305 sqliteVdbeAddOp(v, OP_Pop, 1, 0);
306 sqliteVdbeAddOp(v, OP_NewRecno, base, 0);
danielk1977c3f9bad2002-05-15 08:30:12 +0000307 }
308 sqliteVdbeAddOp(v, OP_MustBeInt, 0, 0);
309 }else{
drhe1e68f42002-03-31 18:29:03 +0000310 sqliteVdbeAddOp(v, OP_NewRecno, base, 0);
drh4a324312001-12-21 14:30:42 +0000311 }
drh4a324312001-12-21 14:30:42 +0000312
danielk1977c3f9bad2002-05-15 08:30:12 +0000313 /* Push onto the stack, data for all columns of the new entry, beginning
danielk1977f29ce552002-05-19 23:43:12 +0000314 ** with the first column.
315 */
danielk1977c3f9bad2002-05-15 08:30:12 +0000316 for(i=0; i<pTab->nCol; i++){
317 if( i==pTab->iPKey ){
drh9adf9ac2002-05-15 11:44:13 +0000318 /* The value of the INTEGER PRIMARY KEY column is always a NULL.
danielk1977f29ce552002-05-19 23:43:12 +0000319 ** Whenever this column is read, the record number will be substituted
320 ** in its place. So will fill this column with a NULL to avoid
321 ** taking up data space with information that will never be used. */
drh9adf9ac2002-05-15 11:44:13 +0000322 sqliteVdbeAddOp(v, OP_String, 0, 0);
323 continue;
danielk1977c3f9bad2002-05-15 08:30:12 +0000324 }
325 if( pColumn==0 ){
drh9adf9ac2002-05-15 11:44:13 +0000326 j = i;
danielk1977c3f9bad2002-05-15 08:30:12 +0000327 }else{
drh9adf9ac2002-05-15 11:44:13 +0000328 for(j=0; j<pColumn->nId; j++){
329 if( pColumn->a[j].idx==i ) break;
330 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000331 }
332 if( pColumn && j>=pColumn->nId ){
drh9adf9ac2002-05-15 11:44:13 +0000333 sqliteVdbeAddOp(v, OP_String, 0, 0);
334 sqliteVdbeChangeP3(v, -1, pTab->aCol[i].zDflt, P3_STATIC);
danielk1977c3f9bad2002-05-15 08:30:12 +0000335 }else if( srcTab>=0 ){
drh9adf9ac2002-05-15 11:44:13 +0000336 sqliteVdbeAddOp(v, OP_Column, srcTab, j);
danielk1977c3f9bad2002-05-15 08:30:12 +0000337 }else{
drh9adf9ac2002-05-15 11:44:13 +0000338 sqliteExprCode(pParse, pList->a[j].pExpr);
drh5974a302000-06-07 14:42:26 +0000339 }
drhbed86902000-06-02 13:27:59 +0000340 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000341
342 /* Generate code to check constraints and generate index keys and
danielk1977f29ce552002-05-19 23:43:12 +0000343 ** do the insertion.
344 */
danielk1977c3f9bad2002-05-15 08:30:12 +0000345 endOfLoop = sqliteVdbeMakeLabel(v);
346 sqliteGenerateConstraintChecks(pParse, pTab, base, 0,0,0,onError,endOfLoop);
347 sqliteCompleteInsertion(pParse, pTab, base, 0,0,0);
348
349 /* Update the count of rows that are inserted
danielk1977f29ce552002-05-19 23:43:12 +0000350 */
danielk1977c3f9bad2002-05-15 08:30:12 +0000351 if( (db->flags & SQLITE_CountRows)!=0 && !pParse->trigStack){
352 sqliteVdbeAddOp(v, OP_AddImm, 1, 0);
drh5974a302000-06-07 14:42:26 +0000353 }
354 }
drh1ccde152000-06-17 13:12:39 +0000355
danielk1977f29ce552002-05-19 23:43:12 +0000356 if( row_triggers_exist ){
danielk1977c3f9bad2002-05-15 08:30:12 +0000357 /* Close all tables opened */
danielk1977f29ce552002-05-19 23:43:12 +0000358 if( !pTab->pSelect ){
danielk1977c3f9bad2002-05-15 08:30:12 +0000359 sqliteVdbeAddOp(v, OP_Close, base, 0);
360 for(idx=1, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, idx++){
drh9adf9ac2002-05-15 11:44:13 +0000361 sqliteVdbeAddOp(v, OP_Close, idx+base, 0);
danielk1977c3f9bad2002-05-15 08:30:12 +0000362 }
363 }
drh1bee3d72001-10-15 00:44:35 +0000364
danielk1977c3f9bad2002-05-15 08:30:12 +0000365 /* Code AFTER triggers */
danielk1977f29ce552002-05-19 23:43:12 +0000366 if( sqliteCodeRowTrigger(pParse, TK_INSERT, 0, TK_AFTER, pTab, newIdx, -1,
367 onError) ){
368 goto insert_cleanup;
369 }
drh1bee3d72001-10-15 00:44:35 +0000370 }
371
drh1ccde152000-06-17 13:12:39 +0000372 /* The bottom of the loop, if the data source is a SELECT statement
danielk1977f29ce552002-05-19 23:43:12 +0000373 */
drh0ca3e242002-01-29 23:07:02 +0000374 sqliteVdbeResolveLabel(v, endOfLoop);
drh5974a302000-06-07 14:42:26 +0000375 if( srcTab>=0 ){
drh6b563442001-11-07 16:48:26 +0000376 sqliteVdbeAddOp(v, OP_Next, srcTab, iCont);
drh99fcd712001-10-13 01:06:47 +0000377 sqliteVdbeResolveLabel(v, iBreak);
drh6b563442001-11-07 16:48:26 +0000378 sqliteVdbeAddOp(v, OP_Close, srcTab, 0);
379 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000380
danielk1977f29ce552002-05-19 23:43:12 +0000381 if( !row_triggers_exist ){
danielk1977c3f9bad2002-05-15 08:30:12 +0000382 /* Close all tables opened */
383 sqliteVdbeAddOp(v, OP_Close, base, 0);
384 for(idx=1, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, idx++){
385 sqliteVdbeAddOp(v, OP_Close, idx+base, 0);
386 }
drhcce7d172000-05-31 15:34:51 +0000387 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000388
drh1c928532002-01-31 15:54:21 +0000389 sqliteEndWriteOperation(pParse);
drh5e00f6c2001-09-13 13:46:56 +0000390
drh1bee3d72001-10-15 00:44:35 +0000391 /*
danielk1977f29ce552002-05-19 23:43:12 +0000392 ** Return the number of rows inserted.
drh1bee3d72001-10-15 00:44:35 +0000393 */
danielk1977c3f9bad2002-05-15 08:30:12 +0000394 if( db->flags & SQLITE_CountRows && !pParse->trigStack ){
drh1bee3d72001-10-15 00:44:35 +0000395 sqliteVdbeAddOp(v, OP_ColumnCount, 1, 0);
396 sqliteVdbeAddOp(v, OP_ColumnName, 0, 0);
397 sqliteVdbeChangeP3(v, -1, "rows inserted", P3_STATIC);
drh1bee3d72001-10-15 00:44:35 +0000398 sqliteVdbeAddOp(v, OP_Callback, 1, 0);
399 }
drhcce7d172000-05-31 15:34:51 +0000400
401insert_cleanup:
drh5974a302000-06-07 14:42:26 +0000402 if( pList ) sqliteExprListDelete(pList);
403 if( pSelect ) sqliteSelectDelete(pSelect);
danielk1977c3f9bad2002-05-15 08:30:12 +0000404 if ( zTab ) sqliteFree(zTab);
drh967e8b72000-06-21 13:59:10 +0000405 sqliteIdListDelete(pColumn);
drhcce7d172000-05-31 15:34:51 +0000406}
drh9cfcf5d2002-01-29 18:41:24 +0000407
drh9cfcf5d2002-01-29 18:41:24 +0000408/*
409** Generate code to do a constraint check prior to an INSERT or an UPDATE.
410**
411** When this routine is called, the stack contains (from bottom to top)
drh0ca3e242002-01-29 23:07:02 +0000412** the following values:
413**
drhb419a922002-01-30 16:17:23 +0000414** 1. The recno of the row to be updated before it is updated. This
415** value is omitted unless we are doing an UPDATE that involves a
416** change to the record number.
drh0ca3e242002-01-29 23:07:02 +0000417**
drhb419a922002-01-30 16:17:23 +0000418** 2. The recno of the row after the update.
drh0ca3e242002-01-29 23:07:02 +0000419**
420** 3. The data in the first column of the entry after the update.
421**
422** i. Data from middle columns...
423**
424** N. The data in the last column of the entry after the update.
425**
drhb419a922002-01-30 16:17:23 +0000426** The old recno shown as entry (1) above is omitted unless both isUpdate
drh1c928532002-01-31 15:54:21 +0000427** and recnoChng are 1. isUpdate is true for UPDATEs and false for
428** INSERTs and recnoChng is true if the record number is being changed.
drh0ca3e242002-01-29 23:07:02 +0000429**
430** The code generated by this routine pushes additional entries onto
431** the stack which are the keys for new index entries for the new record.
432** The order of index keys is the same as the order of the indices on
433** the pTable->pIndex list. A key is only created for index i if
434** aIdxUsed!=0 and aIdxUsed[i]!=0.
drh9cfcf5d2002-01-29 18:41:24 +0000435**
436** This routine also generates code to check constraints. NOT NULL,
437** CHECK, and UNIQUE constraints are all checked. If a constraint fails,
drh1c928532002-01-31 15:54:21 +0000438** then the appropriate action is performed. There are five possible
439** actions: ROLLBACK, ABORT, FAIL, REPLACE, and IGNORE.
drh9cfcf5d2002-01-29 18:41:24 +0000440**
441** Constraint type Action What Happens
442** --------------- ---------- ----------------------------------------
drh1c928532002-01-31 15:54:21 +0000443** any ROLLBACK The current transaction is rolled back and
drh9cfcf5d2002-01-29 18:41:24 +0000444** sqlite_exec() returns immediately with a
445** return code of SQLITE_CONSTRAINT.
446**
drh1c928532002-01-31 15:54:21 +0000447** any ABORT Back out changes from the current command
448** only (do not do a complete rollback) then
449** cause sqlite_exec() to return immediately
450** with SQLITE_CONSTRAINT.
451**
452** any FAIL Sqlite_exec() returns immediately with a
453** return code of SQLITE_CONSTRAINT. The
454** transaction is not rolled back and any
455** prior changes are retained.
456**
drh9cfcf5d2002-01-29 18:41:24 +0000457** any IGNORE The record number and data is popped from
458** the stack and there is an immediate jump
459** to label ignoreDest.
460**
461** NOT NULL REPLACE The NULL value is replace by the default
462** value for that column. If the default value
463** is NULL, the action is the same as ABORT.
464**
465** UNIQUE REPLACE The other row that conflicts with the row
466** being inserted is removed.
467**
468** CHECK REPLACE Illegal. The results in an exception.
469**
drh1c928532002-01-31 15:54:21 +0000470** Which action to take is determined by the overrideError parameter.
471** Or if overrideError==OE_Default, then the pParse->onError parameter
472** is used. Or if pParse->onError==OE_Default then the onError value
473** for the constraint is used.
drh9cfcf5d2002-01-29 18:41:24 +0000474**
drhaaab5722002-02-19 13:39:21 +0000475** The calling routine must open a read/write cursor for pTab with
drh9cfcf5d2002-01-29 18:41:24 +0000476** cursor number "base". All indices of pTab must also have open
477** read/write cursors with cursor number base+i for the i-th cursor.
478** Except, if there is no possibility of a REPLACE action then
479** cursors do not need to be open for indices where aIdxUsed[i]==0.
480**
481** If the isUpdate flag is true, it means that the "base" cursor is
482** initially pointing to an entry that is being updated. The isUpdate
483** flag causes extra code to be generated so that the "base" cursor
484** is still pointing at the same entry after the routine returns.
485** Without the isUpdate flag, the "base" cursor might be moved.
486*/
487void sqliteGenerateConstraintChecks(
488 Parse *pParse, /* The parser context */
489 Table *pTab, /* the table into which we are inserting */
490 int base, /* Index of a read/write cursor pointing at pTab */
491 char *aIdxUsed, /* Which indices are used. NULL means all are used */
drh0ca3e242002-01-29 23:07:02 +0000492 int recnoChng, /* True if the record number will change */
drhb419a922002-01-30 16:17:23 +0000493 int isUpdate, /* True for UPDATE, False for INSERT */
drh9cfcf5d2002-01-29 18:41:24 +0000494 int overrideError, /* Override onError to this if not OE_Default */
drhb419a922002-01-30 16:17:23 +0000495 int ignoreDest /* Jump to this label on an OE_Ignore resolution */
drh9cfcf5d2002-01-29 18:41:24 +0000496){
497 int i;
498 Vdbe *v;
499 int nCol;
500 int onError;
501 int addr;
502 int extra;
drh0ca3e242002-01-29 23:07:02 +0000503 int iCur;
504 Index *pIdx;
505 int seenReplace = 0;
506 int jumpInst;
507 int contAddr;
drhb419a922002-01-30 16:17:23 +0000508 int hasTwoRecnos = (isUpdate && recnoChng);
drh9cfcf5d2002-01-29 18:41:24 +0000509
510 v = sqliteGetVdbe(pParse);
511 assert( v!=0 );
drh417be792002-03-03 18:59:40 +0000512 assert( pTab->pSelect==0 ); /* This table is not a VIEW */
drh9cfcf5d2002-01-29 18:41:24 +0000513 nCol = pTab->nCol;
514
515 /* Test all NOT NULL constraints.
516 */
517 for(i=0; i<nCol; i++){
drh0ca3e242002-01-29 23:07:02 +0000518 if( i==pTab->iPKey ){
519 /* Fix me: Make sure the INTEGER PRIMARY KEY is not NULL. */
520 continue;
521 }
drh9cfcf5d2002-01-29 18:41:24 +0000522 onError = pTab->aCol[i].notNull;
drh0ca3e242002-01-29 23:07:02 +0000523 if( onError==OE_None ) continue;
drh9cfcf5d2002-01-29 18:41:24 +0000524 if( overrideError!=OE_Default ){
525 onError = overrideError;
drh1c928532002-01-31 15:54:21 +0000526 }else if( onError==OE_Default ){
drh0d65dc02002-02-03 00:56:09 +0000527 onError = pParse->db->onError;
528 if( onError==OE_Default ) onError = OE_Abort;
drh9cfcf5d2002-01-29 18:41:24 +0000529 }
530 if( onError==OE_Replace && pTab->aCol[i].zDflt==0 ){
531 onError = OE_Abort;
532 }
drhef6764a2002-01-30 04:32:00 +0000533 sqliteVdbeAddOp(v, OP_Dup, nCol-1-i, 1);
534 addr = sqliteVdbeAddOp(v, OP_NotNull, 0, 0);
drh9cfcf5d2002-01-29 18:41:24 +0000535 switch( onError ){
drh1c928532002-01-31 15:54:21 +0000536 case OE_Rollback:
537 case OE_Abort:
538 case OE_Fail: {
539 sqliteVdbeAddOp(v, OP_Halt, SQLITE_CONSTRAINT, onError);
drh9cfcf5d2002-01-29 18:41:24 +0000540 break;
541 }
542 case OE_Ignore: {
drhb419a922002-01-30 16:17:23 +0000543 sqliteVdbeAddOp(v, OP_Pop, nCol+1+hasTwoRecnos, 0);
drh0ca3e242002-01-29 23:07:02 +0000544 sqliteVdbeAddOp(v, OP_Goto, 0, ignoreDest);
drh9cfcf5d2002-01-29 18:41:24 +0000545 break;
546 }
547 case OE_Replace: {
548 sqliteVdbeAddOp(v, OP_String, 0, 0);
549 sqliteVdbeChangeP3(v, -1, pTab->aCol[i].zDflt, P3_STATIC);
550 sqliteVdbeAddOp(v, OP_Push, nCol-i, 0);
551 break;
552 }
drh0ca3e242002-01-29 23:07:02 +0000553 default: assert(0);
drh9cfcf5d2002-01-29 18:41:24 +0000554 }
drhef6764a2002-01-30 04:32:00 +0000555 sqliteVdbeChangeP2(v, addr, sqliteVdbeCurrentAddr(v));
drh9cfcf5d2002-01-29 18:41:24 +0000556 }
557
558 /* Test all CHECK constraints
559 */
560
561 /* Test all UNIQUE constraints. Add index records as we go.
562 */
drh0d65dc02002-02-03 00:56:09 +0000563 if( (recnoChng || !isUpdate) && pTab->iPKey>=0 ){
drh0ca3e242002-01-29 23:07:02 +0000564 onError = pTab->keyConf;
565 if( overrideError!=OE_Default ){
566 onError = overrideError;
drh1c928532002-01-31 15:54:21 +0000567 }else if( onError==OE_Default ){
drh0d65dc02002-02-03 00:56:09 +0000568 onError = pParse->db->onError;
569 if( onError==OE_Default ) onError = OE_Abort;
drh0ca3e242002-01-29 23:07:02 +0000570 }
drh0d65dc02002-02-03 00:56:09 +0000571 if( onError!=OE_Replace ){
572 sqliteVdbeAddOp(v, OP_Dup, nCol, 1);
573 jumpInst = sqliteVdbeAddOp(v, OP_NotExists, base, 0);
574 switch( onError ){
575 case OE_Rollback:
576 case OE_Abort:
577 case OE_Fail: {
578 sqliteVdbeAddOp(v, OP_Halt, SQLITE_CONSTRAINT, onError);
579 break;
580 }
581 case OE_Ignore: {
582 sqliteVdbeAddOp(v, OP_Pop, nCol+1+hasTwoRecnos, 0);
583 sqliteVdbeAddOp(v, OP_Goto, 0, ignoreDest);
584 break;
585 }
586 default: assert(0);
drh0ca3e242002-01-29 23:07:02 +0000587 }
drh0d65dc02002-02-03 00:56:09 +0000588 contAddr = sqliteVdbeCurrentAddr(v);
589 sqliteVdbeChangeP2(v, jumpInst, contAddr);
590 if( isUpdate ){
591 sqliteVdbeAddOp(v, OP_Dup, nCol+1, 1);
592 sqliteVdbeAddOp(v, OP_MoveTo, base, 0);
drh0ca3e242002-01-29 23:07:02 +0000593 }
drh0ca3e242002-01-29 23:07:02 +0000594 }
595 }
drh9cfcf5d2002-01-29 18:41:24 +0000596 extra = 0;
597 for(extra=(-1), iCur=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, iCur++){
drh9cfcf5d2002-01-29 18:41:24 +0000598 if( aIdxUsed && aIdxUsed[iCur]==0 ) continue;
599 extra++;
600 sqliteVdbeAddOp(v, OP_Dup, nCol+extra, 1);
601 for(i=0; i<pIdx->nColumn; i++){
602 int idx = pIdx->aiColumn[i];
603 if( idx==pTab->iPKey ){
drh0ca3e242002-01-29 23:07:02 +0000604 sqliteVdbeAddOp(v, OP_Dup, i+extra+nCol+1, 1);
drh9cfcf5d2002-01-29 18:41:24 +0000605 }else{
drh0ca3e242002-01-29 23:07:02 +0000606 sqliteVdbeAddOp(v, OP_Dup, i+extra+nCol-idx, 1);
drh9cfcf5d2002-01-29 18:41:24 +0000607 }
608 }
609 sqliteVdbeAddOp(v, OP_MakeIdxKey, pIdx->nColumn, 0);
610 onError = pIdx->onError;
611 if( onError==OE_None ) continue;
612 if( overrideError!=OE_Default ){
613 onError = overrideError;
drh1c928532002-01-31 15:54:21 +0000614 }else if( onError==OE_Default ){
drh0d65dc02002-02-03 00:56:09 +0000615 onError = pParse->db->onError;
616 if( onError==OE_Default ) onError = OE_Abort;
drh9cfcf5d2002-01-29 18:41:24 +0000617 }
drhb419a922002-01-30 16:17:23 +0000618 sqliteVdbeAddOp(v, OP_Dup, extra+nCol+1+hasTwoRecnos, 1);
drh0ca3e242002-01-29 23:07:02 +0000619 jumpInst = sqliteVdbeAddOp(v, OP_IsUnique, base+iCur+1, 0);
drh9cfcf5d2002-01-29 18:41:24 +0000620 switch( onError ){
drh1c928532002-01-31 15:54:21 +0000621 case OE_Rollback:
622 case OE_Abort:
623 case OE_Fail: {
624 sqliteVdbeAddOp(v, OP_Halt, SQLITE_CONSTRAINT, onError);
drh9cfcf5d2002-01-29 18:41:24 +0000625 break;
626 }
627 case OE_Ignore: {
drh0ca3e242002-01-29 23:07:02 +0000628 assert( seenReplace==0 );
drhfe1a1772002-04-09 03:15:06 +0000629 sqliteVdbeAddOp(v, OP_Pop, nCol+extra+3+hasTwoRecnos, 0);
drh9cfcf5d2002-01-29 18:41:24 +0000630 sqliteVdbeAddOp(v, OP_Goto, 0, ignoreDest);
drh9cfcf5d2002-01-29 18:41:24 +0000631 break;
632 }
633 case OE_Replace: {
drhc8d30ac2002-04-12 10:08:59 +0000634 sqliteGenerateRowDelete(v, pTab, base, 0);
drh9cfcf5d2002-01-29 18:41:24 +0000635 if( isUpdate ){
drhb419a922002-01-30 16:17:23 +0000636 sqliteVdbeAddOp(v, OP_Dup, nCol+extra+1+hasTwoRecnos, 1);
drh0ca3e242002-01-29 23:07:02 +0000637 sqliteVdbeAddOp(v, OP_MoveTo, base, 0);
drh9cfcf5d2002-01-29 18:41:24 +0000638 }
drh0ca3e242002-01-29 23:07:02 +0000639 seenReplace = 1;
drh9cfcf5d2002-01-29 18:41:24 +0000640 break;
641 }
drh0ca3e242002-01-29 23:07:02 +0000642 default: assert(0);
drh9cfcf5d2002-01-29 18:41:24 +0000643 }
644 contAddr = sqliteVdbeCurrentAddr(v);
645 sqliteVdbeChangeP2(v, jumpInst, contAddr);
646 }
647}
drh0ca3e242002-01-29 23:07:02 +0000648
649/*
650** This routine generates code to finish the INSERT or UPDATE operation
651** that was started by a prior call to sqliteGenerateConstraintChecks.
652** The stack must contain keys for all active indices followed by data
653** and the recno for the new entry. This routine creates the new
654** entries in all indices and in the main table.
655**
drhb419a922002-01-30 16:17:23 +0000656** The arguments to this routine should be the same as the first six
drh0ca3e242002-01-29 23:07:02 +0000657** arguments to sqliteGenerateConstraintChecks.
658*/
659void sqliteCompleteInsertion(
660 Parse *pParse, /* The parser context */
661 Table *pTab, /* the table into which we are inserting */
662 int base, /* Index of a read/write cursor pointing at pTab */
663 char *aIdxUsed, /* Which indices are used. NULL means all are used */
drhb419a922002-01-30 16:17:23 +0000664 int recnoChng, /* True if the record number will change */
665 int isUpdate /* True for UPDATE, False for INSERT */
drh0ca3e242002-01-29 23:07:02 +0000666){
667 int i;
668 Vdbe *v;
669 int nIdx;
670 Index *pIdx;
671
672 v = sqliteGetVdbe(pParse);
673 assert( v!=0 );
drh417be792002-03-03 18:59:40 +0000674 assert( pTab->pSelect==0 ); /* This table is not a VIEW */
drh0ca3e242002-01-29 23:07:02 +0000675 for(nIdx=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, nIdx++){}
676 for(i=nIdx-1; i>=0; i--){
677 if( aIdxUsed && aIdxUsed[i]==0 ) continue;
678 sqliteVdbeAddOp(v, OP_IdxPut, base+i+1, 0);
679 }
680 sqliteVdbeAddOp(v, OP_MakeRecord, pTab->nCol, 0);
danielk1977c3f9bad2002-05-15 08:30:12 +0000681 sqliteVdbeAddOp(v, OP_PutIntKey, base, pParse->trigStack?0:1);
drhb419a922002-01-30 16:17:23 +0000682 if( isUpdate && recnoChng ){
drh0ca3e242002-01-29 23:07:02 +0000683 sqliteVdbeAddOp(v, OP_Pop, 1, 0);
684 }
685}