blob: 73ed0563a8ffd0ce4287433f7fc51bdb35f787db [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**
danielk19776f349032002-06-11 02:25:40 +000015** $Id: insert.c,v 1.61 2002/06/11 02:25:42 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;
drhc977f7f2002-05-21 11:38:11 +000098 sqliteBeginWriteOperation(pParse, pSelect || row_triggers_exist);
drh1ccde152000-06-17 13:12:39 +000099
danielk1977c3f9bad2002-05-15 08:30:12 +0000100 /* if there are row triggers, allocate a temp table for new.* references. */
danielk1977f29ce552002-05-19 23:43:12 +0000101 if( row_triggers_exist ){
danielk1977c3f9bad2002-05-15 08:30:12 +0000102 newIdx = pParse->nTab++;
danielk1977f29ce552002-05-19 23:43:12 +0000103 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000104
drh1ccde152000-06-17 13:12:39 +0000105 /* Figure out how many columns of data are supplied. If the data
drhc6b52df2002-01-04 03:09:29 +0000106 ** is coming from a SELECT statement, then this step has to generate
drh1ccde152000-06-17 13:12:39 +0000107 ** all the code to implement the SELECT statement and leave the data
108 ** in a temporary table. If data is coming from an expression list,
109 ** then we just have to count the number of expressions.
110 */
drh5974a302000-06-07 14:42:26 +0000111 if( pSelect ){
112 int rc;
113 srcTab = pParse->nTab++;
drh99fcd712001-10-13 01:06:47 +0000114 sqliteVdbeAddOp(v, OP_OpenTemp, srcTab, 0);
drh832508b2002-03-02 17:04:07 +0000115 rc = sqliteSelect(pParse, pSelect, SRT_Table, srcTab, 0,0,0);
drhdaffd0e2001-04-11 14:28:42 +0000116 if( rc || pParse->nErr || sqlite_malloc_failed ) goto insert_cleanup;
drh5974a302000-06-07 14:42:26 +0000117 assert( pSelect->pEList );
drh967e8b72000-06-21 13:59:10 +0000118 nColumn = pSelect->pEList->nExpr;
drh5974a302000-06-07 14:42:26 +0000119 }else{
drhad3cab52002-05-24 02:04:32 +0000120 SrcList dummy;
drhdaffd0e2001-04-11 14:28:42 +0000121 assert( pList!=0 );
drh5974a302000-06-07 14:42:26 +0000122 srcTab = -1;
123 assert( pList );
drh967e8b72000-06-21 13:59:10 +0000124 nColumn = pList->nExpr;
drhad3cab52002-05-24 02:04:32 +0000125 dummy.nSrc = 0;
drhe64e7b22002-02-18 13:56:36 +0000126 for(i=0; i<nColumn; i++){
drh832508b2002-03-02 17:04:07 +0000127 if( sqliteExprResolveIds(pParse, 0, &dummy, 0, pList->a[i].pExpr) ){
drhe64e7b22002-02-18 13:56:36 +0000128 goto insert_cleanup;
129 }
drhb04a5d82002-04-12 03:55:15 +0000130 if( sqliteExprCheck(pParse, pList->a[i].pExpr, 0, 0) ){
131 goto insert_cleanup;
132 }
drhe64e7b22002-02-18 13:56:36 +0000133 }
drh5974a302000-06-07 14:42:26 +0000134 }
drh1ccde152000-06-17 13:12:39 +0000135
136 /* Make sure the number of columns in the source data matches the number
137 ** of columns to be inserted into the table.
138 */
drh967e8b72000-06-21 13:59:10 +0000139 if( pColumn==0 && nColumn!=pTab->nCol ){
drhcce7d172000-05-31 15:34:51 +0000140 char zNum1[30];
141 char zNum2[30];
drh967e8b72000-06-21 13:59:10 +0000142 sprintf(zNum1,"%d", nColumn);
drhcce7d172000-05-31 15:34:51 +0000143 sprintf(zNum2,"%d", pTab->nCol);
144 sqliteSetString(&pParse->zErrMsg, "table ", pTab->zName,
145 " has ", zNum2, " columns but ",
146 zNum1, " values were supplied", 0);
147 pParse->nErr++;
148 goto insert_cleanup;
149 }
drh967e8b72000-06-21 13:59:10 +0000150 if( pColumn!=0 && nColumn!=pColumn->nId ){
drhcce7d172000-05-31 15:34:51 +0000151 char zNum1[30];
152 char zNum2[30];
drh967e8b72000-06-21 13:59:10 +0000153 sprintf(zNum1,"%d", nColumn);
154 sprintf(zNum2,"%d", pColumn->nId);
drhcce7d172000-05-31 15:34:51 +0000155 sqliteSetString(&pParse->zErrMsg, zNum1, " values for ",
156 zNum2, " columns", 0);
157 pParse->nErr++;
158 goto insert_cleanup;
159 }
drh1ccde152000-06-17 13:12:39 +0000160
161 /* If the INSERT statement included an IDLIST term, then make sure
162 ** all elements of the IDLIST really are columns of the table and
163 ** remember the column indices.
drhc8392582001-12-31 02:48:51 +0000164 **
165 ** If the table has an INTEGER PRIMARY KEY column and that column
166 ** is named in the IDLIST, then record in the keyColumn variable
167 ** the index into IDLIST of the primary key column. keyColumn is
168 ** the index of the primary key as it appears in IDLIST, not as
169 ** is appears in the original table. (The index of the primary
170 ** key in the original table is pTab->iPKey.)
drh1ccde152000-06-17 13:12:39 +0000171 */
drh967e8b72000-06-21 13:59:10 +0000172 if( pColumn ){
173 for(i=0; i<pColumn->nId; i++){
174 pColumn->a[i].idx = -1;
drhcce7d172000-05-31 15:34:51 +0000175 }
drh967e8b72000-06-21 13:59:10 +0000176 for(i=0; i<pColumn->nId; i++){
drhcce7d172000-05-31 15:34:51 +0000177 for(j=0; j<pTab->nCol; j++){
drh967e8b72000-06-21 13:59:10 +0000178 if( sqliteStrICmp(pColumn->a[i].zName, pTab->aCol[j].zName)==0 ){
179 pColumn->a[i].idx = j;
drh4a324312001-12-21 14:30:42 +0000180 if( j==pTab->iPKey ){
drh9aa028d2001-12-22 21:48:29 +0000181 keyColumn = i;
drh4a324312001-12-21 14:30:42 +0000182 }
drhcce7d172000-05-31 15:34:51 +0000183 break;
184 }
185 }
186 if( j>=pTab->nCol ){
187 sqliteSetString(&pParse->zErrMsg, "table ", pTab->zName,
drh967e8b72000-06-21 13:59:10 +0000188 " has no column named ", pColumn->a[i].zName, 0);
drhcce7d172000-05-31 15:34:51 +0000189 pParse->nErr++;
190 goto insert_cleanup;
191 }
192 }
193 }
drh1ccde152000-06-17 13:12:39 +0000194
drhaacc5432002-01-06 17:07:40 +0000195 /* If there is no IDLIST term but the table has an integer primary
drhc8392582001-12-31 02:48:51 +0000196 ** key, the set the keyColumn variable to the primary key column index
197 ** in the original table definition.
drh4a324312001-12-21 14:30:42 +0000198 */
199 if( pColumn==0 ){
200 keyColumn = pTab->iPKey;
201 }
202
danielk1977c3f9bad2002-05-15 08:30:12 +0000203 /* Open the temp table for FOR EACH ROW triggers */
danielk1977f29ce552002-05-19 23:43:12 +0000204 if( row_triggers_exist ){
danielk1977c3f9bad2002-05-15 08:30:12 +0000205 sqliteVdbeAddOp(v, OP_OpenTemp, newIdx, 0);
danielk1977f29ce552002-05-19 23:43:12 +0000206 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000207
drhfeeb1392002-04-09 03:28:01 +0000208 /* Initialize the count of rows to be inserted
209 */
danielk1977f29ce552002-05-19 23:43:12 +0000210 if( db->flags & SQLITE_CountRows && !pParse->trigStack ){
drhfeeb1392002-04-09 03:28:01 +0000211 sqliteVdbeAddOp(v, OP_Integer, 0, 0); /* Initialize the row count */
212 }
213
danielk1977c3f9bad2002-05-15 08:30:12 +0000214 /* Open tables and indices if there are no row triggers */
danielk1977f29ce552002-05-19 23:43:12 +0000215 if( !row_triggers_exist ){
danielk1977c3f9bad2002-05-15 08:30:12 +0000216 base = pParse->nTab;
217 openOp = pTab->isTemp ? OP_OpenWrAux : OP_OpenWrite;
218 sqliteVdbeAddOp(v, openOp, base, pTab->tnum);
219 sqliteVdbeChangeP3(v, -1, pTab->zName, P3_STATIC);
220 for(idx=1, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, idx++){
221 sqliteVdbeAddOp(v, openOp, idx+base, pIdx->tnum);
222 sqliteVdbeChangeP3(v, -1, pIdx->zName, P3_STATIC);
223 }
224 pParse->nTab += idx;
225 }
226
drh1ccde152000-06-17 13:12:39 +0000227 /* If the data source is a SELECT statement, then we have to create
228 ** a loop because there might be multiple rows of data. If the data
229 ** source is an expression list, then exactly one row will be inserted
230 ** and the loop is not used.
231 */
drh5974a302000-06-07 14:42:26 +0000232 if( srcTab>=0 ){
drh5974a302000-06-07 14:42:26 +0000233 iBreak = sqliteVdbeMakeLabel(v);
drh6b563442001-11-07 16:48:26 +0000234 sqliteVdbeAddOp(v, OP_Rewind, srcTab, iBreak);
235 iCont = sqliteVdbeCurrentAddr(v);
drh5974a302000-06-07 14:42:26 +0000236 }
drh1ccde152000-06-17 13:12:39 +0000237
danielk19776f349032002-06-11 02:25:40 +0000238 endOfLoop = sqliteVdbeMakeLabel(v);
danielk1977f29ce552002-05-19 23:43:12 +0000239 if( row_triggers_exist ){
danielk1977c3f9bad2002-05-15 08:30:12 +0000240
241 /* build the new.* reference row */
242 sqliteVdbeAddOp(v, OP_Integer, 13, 0);
243 for(i=0; i<pTab->nCol; i++){
244 if( pColumn==0 ){
drh9adf9ac2002-05-15 11:44:13 +0000245 j = i;
danielk1977c3f9bad2002-05-15 08:30:12 +0000246 }else{
drh9adf9ac2002-05-15 11:44:13 +0000247 for(j=0; j<pColumn->nId; j++){
248 if( pColumn->a[j].idx==i ) break;
249 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000250 }
251 if( pColumn && j>=pColumn->nId ){
drh9adf9ac2002-05-15 11:44:13 +0000252 sqliteVdbeAddOp(v, OP_String, 0, 0);
253 sqliteVdbeChangeP3(v, -1, pTab->aCol[i].zDflt, P3_STATIC);
danielk1977c3f9bad2002-05-15 08:30:12 +0000254 }else if( srcTab>=0 ){
drh9adf9ac2002-05-15 11:44:13 +0000255 sqliteVdbeAddOp(v, OP_Column, srcTab, j);
danielk1977c3f9bad2002-05-15 08:30:12 +0000256 }else{
drh9adf9ac2002-05-15 11:44:13 +0000257 sqliteExprCode(pParse, pList->a[j].pExpr);
danielk1977c3f9bad2002-05-15 08:30:12 +0000258 }
259 }
260 sqliteVdbeAddOp(v, OP_MakeRecord, pTab->nCol, 0);
261 sqliteVdbeAddOp(v, OP_PutIntKey, newIdx, 0);
262 sqliteVdbeAddOp(v, OP_Rewind, newIdx, 0);
263
264 /* Fire BEFORE triggers */
danielk1977f29ce552002-05-19 23:43:12 +0000265 if( sqliteCodeRowTrigger(pParse, TK_INSERT, 0, TK_BEFORE, pTab, newIdx, -1,
danielk19776f349032002-06-11 02:25:40 +0000266 onError, endOfLoop) ){
danielk1977f29ce552002-05-19 23:43:12 +0000267 goto insert_cleanup;
268 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000269
270 /* Open the tables and indices for the INSERT */
danielk1977f29ce552002-05-19 23:43:12 +0000271 if( !pTab->pSelect ){
danielk1977c3f9bad2002-05-15 08:30:12 +0000272 base = pParse->nTab;
273 openOp = pTab->isTemp ? OP_OpenWrAux : OP_OpenWrite;
274 sqliteVdbeAddOp(v, openOp, base, pTab->tnum);
275 sqliteVdbeChangeP3(v, -1, pTab->zName, P3_STATIC);
276 for(idx=1, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, idx++){
drh9adf9ac2002-05-15 11:44:13 +0000277 sqliteVdbeAddOp(v, openOp, idx+base, pIdx->tnum);
278 sqliteVdbeChangeP3(v, -1, pIdx->zName, P3_STATIC);
danielk1977c3f9bad2002-05-15 08:30:12 +0000279 }
280 pParse->nTab += idx;
281 }
282 }
283
drh4a324312001-12-21 14:30:42 +0000284 /* Push the record number for the new entry onto the stack. The
285 ** record number is a randomly generate integer created by NewRecno
286 ** except when the table has an INTEGER PRIMARY KEY column, in which
drhb419a922002-01-30 16:17:23 +0000287 ** case the record number is the same as that column.
drh1ccde152000-06-17 13:12:39 +0000288 */
danielk1977f29ce552002-05-19 23:43:12 +0000289 if( !pTab->pSelect ){
danielk1977c3f9bad2002-05-15 08:30:12 +0000290 if( keyColumn>=0 ){
291 if( srcTab>=0 ){
drh9adf9ac2002-05-15 11:44:13 +0000292 sqliteVdbeAddOp(v, OP_Column, srcTab, keyColumn);
danielk1977c3f9bad2002-05-15 08:30:12 +0000293 }else{
drh9adf9ac2002-05-15 11:44:13 +0000294 sqliteExprCode(pParse, pList->a[keyColumn].pExpr);
drhe1e68f42002-03-31 18:29:03 +0000295
drh9adf9ac2002-05-15 11:44:13 +0000296 /* If the PRIMARY KEY expression is NULL, then use OP_NewRecno
danielk1977f29ce552002-05-19 23:43:12 +0000297 ** to generate a unique primary key value.
298 */
drhf5905aa2002-05-26 20:54:33 +0000299 sqliteVdbeAddOp(v, OP_NotNull, -1, sqliteVdbeCurrentAddr(v)+3);
drh9adf9ac2002-05-15 11:44:13 +0000300 sqliteVdbeAddOp(v, OP_Pop, 1, 0);
301 sqliteVdbeAddOp(v, OP_NewRecno, base, 0);
danielk1977c3f9bad2002-05-15 08:30:12 +0000302 }
303 sqliteVdbeAddOp(v, OP_MustBeInt, 0, 0);
304 }else{
drhe1e68f42002-03-31 18:29:03 +0000305 sqliteVdbeAddOp(v, OP_NewRecno, base, 0);
drh4a324312001-12-21 14:30:42 +0000306 }
drh4a324312001-12-21 14:30:42 +0000307
danielk1977c3f9bad2002-05-15 08:30:12 +0000308 /* Push onto the stack, data for all columns of the new entry, beginning
danielk1977f29ce552002-05-19 23:43:12 +0000309 ** with the first column.
310 */
danielk1977c3f9bad2002-05-15 08:30:12 +0000311 for(i=0; i<pTab->nCol; i++){
312 if( i==pTab->iPKey ){
drh9adf9ac2002-05-15 11:44:13 +0000313 /* The value of the INTEGER PRIMARY KEY column is always a NULL.
danielk1977f29ce552002-05-19 23:43:12 +0000314 ** Whenever this column is read, the record number will be substituted
315 ** in its place. So will fill this column with a NULL to avoid
316 ** taking up data space with information that will never be used. */
drh9adf9ac2002-05-15 11:44:13 +0000317 sqliteVdbeAddOp(v, OP_String, 0, 0);
318 continue;
danielk1977c3f9bad2002-05-15 08:30:12 +0000319 }
320 if( pColumn==0 ){
drh9adf9ac2002-05-15 11:44:13 +0000321 j = i;
danielk1977c3f9bad2002-05-15 08:30:12 +0000322 }else{
drh9adf9ac2002-05-15 11:44:13 +0000323 for(j=0; j<pColumn->nId; j++){
324 if( pColumn->a[j].idx==i ) break;
325 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000326 }
327 if( pColumn && j>=pColumn->nId ){
drh9adf9ac2002-05-15 11:44:13 +0000328 sqliteVdbeAddOp(v, OP_String, 0, 0);
329 sqliteVdbeChangeP3(v, -1, pTab->aCol[i].zDflt, P3_STATIC);
danielk1977c3f9bad2002-05-15 08:30:12 +0000330 }else if( srcTab>=0 ){
drh9adf9ac2002-05-15 11:44:13 +0000331 sqliteVdbeAddOp(v, OP_Column, srcTab, j);
danielk1977c3f9bad2002-05-15 08:30:12 +0000332 }else{
drh9adf9ac2002-05-15 11:44:13 +0000333 sqliteExprCode(pParse, pList->a[j].pExpr);
drh5974a302000-06-07 14:42:26 +0000334 }
drhbed86902000-06-02 13:27:59 +0000335 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000336
337 /* Generate code to check constraints and generate index keys and
danielk1977f29ce552002-05-19 23:43:12 +0000338 ** do the insertion.
339 */
danielk1977c3f9bad2002-05-15 08:30:12 +0000340 sqliteGenerateConstraintChecks(pParse, pTab, base, 0,0,0,onError,endOfLoop);
341 sqliteCompleteInsertion(pParse, pTab, base, 0,0,0);
342
343 /* Update the count of rows that are inserted
danielk1977f29ce552002-05-19 23:43:12 +0000344 */
danielk1977c3f9bad2002-05-15 08:30:12 +0000345 if( (db->flags & SQLITE_CountRows)!=0 && !pParse->trigStack){
346 sqliteVdbeAddOp(v, OP_AddImm, 1, 0);
drh5974a302000-06-07 14:42:26 +0000347 }
348 }
drh1ccde152000-06-17 13:12:39 +0000349
danielk1977f29ce552002-05-19 23:43:12 +0000350 if( row_triggers_exist ){
danielk1977c3f9bad2002-05-15 08:30:12 +0000351 /* Close all tables opened */
danielk1977f29ce552002-05-19 23:43:12 +0000352 if( !pTab->pSelect ){
danielk1977c3f9bad2002-05-15 08:30:12 +0000353 sqliteVdbeAddOp(v, OP_Close, base, 0);
354 for(idx=1, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, idx++){
drh9adf9ac2002-05-15 11:44:13 +0000355 sqliteVdbeAddOp(v, OP_Close, idx+base, 0);
danielk1977c3f9bad2002-05-15 08:30:12 +0000356 }
357 }
drh1bee3d72001-10-15 00:44:35 +0000358
danielk1977c3f9bad2002-05-15 08:30:12 +0000359 /* Code AFTER triggers */
danielk1977f29ce552002-05-19 23:43:12 +0000360 if( sqliteCodeRowTrigger(pParse, TK_INSERT, 0, TK_AFTER, pTab, newIdx, -1,
danielk19776f349032002-06-11 02:25:40 +0000361 onError, endOfLoop) ){
danielk1977f29ce552002-05-19 23:43:12 +0000362 goto insert_cleanup;
363 }
drh1bee3d72001-10-15 00:44:35 +0000364 }
365
drh1ccde152000-06-17 13:12:39 +0000366 /* The bottom of the loop, if the data source is a SELECT statement
danielk1977f29ce552002-05-19 23:43:12 +0000367 */
drh0ca3e242002-01-29 23:07:02 +0000368 sqliteVdbeResolveLabel(v, endOfLoop);
drh5974a302000-06-07 14:42:26 +0000369 if( srcTab>=0 ){
drh6b563442001-11-07 16:48:26 +0000370 sqliteVdbeAddOp(v, OP_Next, srcTab, iCont);
drh99fcd712001-10-13 01:06:47 +0000371 sqliteVdbeResolveLabel(v, iBreak);
drh6b563442001-11-07 16:48:26 +0000372 sqliteVdbeAddOp(v, OP_Close, srcTab, 0);
373 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000374
danielk1977f29ce552002-05-19 23:43:12 +0000375 if( !row_triggers_exist ){
danielk1977c3f9bad2002-05-15 08:30:12 +0000376 /* Close all tables opened */
377 sqliteVdbeAddOp(v, OP_Close, base, 0);
378 for(idx=1, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, idx++){
379 sqliteVdbeAddOp(v, OP_Close, idx+base, 0);
380 }
drhcce7d172000-05-31 15:34:51 +0000381 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000382
drh1c928532002-01-31 15:54:21 +0000383 sqliteEndWriteOperation(pParse);
drh5e00f6c2001-09-13 13:46:56 +0000384
drh1bee3d72001-10-15 00:44:35 +0000385 /*
danielk1977f29ce552002-05-19 23:43:12 +0000386 ** Return the number of rows inserted.
drh1bee3d72001-10-15 00:44:35 +0000387 */
danielk1977c3f9bad2002-05-15 08:30:12 +0000388 if( db->flags & SQLITE_CountRows && !pParse->trigStack ){
drh1bee3d72001-10-15 00:44:35 +0000389 sqliteVdbeAddOp(v, OP_ColumnCount, 1, 0);
390 sqliteVdbeAddOp(v, OP_ColumnName, 0, 0);
391 sqliteVdbeChangeP3(v, -1, "rows inserted", P3_STATIC);
drh1bee3d72001-10-15 00:44:35 +0000392 sqliteVdbeAddOp(v, OP_Callback, 1, 0);
393 }
drhcce7d172000-05-31 15:34:51 +0000394
395insert_cleanup:
drh5974a302000-06-07 14:42:26 +0000396 if( pList ) sqliteExprListDelete(pList);
397 if( pSelect ) sqliteSelectDelete(pSelect);
danielk1977c3f9bad2002-05-15 08:30:12 +0000398 if ( zTab ) sqliteFree(zTab);
drh967e8b72000-06-21 13:59:10 +0000399 sqliteIdListDelete(pColumn);
drhcce7d172000-05-31 15:34:51 +0000400}
drh9cfcf5d2002-01-29 18:41:24 +0000401
drh9cfcf5d2002-01-29 18:41:24 +0000402/*
403** Generate code to do a constraint check prior to an INSERT or an UPDATE.
404**
405** When this routine is called, the stack contains (from bottom to top)
drh0ca3e242002-01-29 23:07:02 +0000406** the following values:
407**
drhb419a922002-01-30 16:17:23 +0000408** 1. The recno of the row to be updated before it is updated. This
409** value is omitted unless we are doing an UPDATE that involves a
410** change to the record number.
drh0ca3e242002-01-29 23:07:02 +0000411**
drhb419a922002-01-30 16:17:23 +0000412** 2. The recno of the row after the update.
drh0ca3e242002-01-29 23:07:02 +0000413**
414** 3. The data in the first column of the entry after the update.
415**
416** i. Data from middle columns...
417**
418** N. The data in the last column of the entry after the update.
419**
drhb419a922002-01-30 16:17:23 +0000420** The old recno shown as entry (1) above is omitted unless both isUpdate
drh1c928532002-01-31 15:54:21 +0000421** and recnoChng are 1. isUpdate is true for UPDATEs and false for
422** INSERTs and recnoChng is true if the record number is being changed.
drh0ca3e242002-01-29 23:07:02 +0000423**
424** The code generated by this routine pushes additional entries onto
425** the stack which are the keys for new index entries for the new record.
426** The order of index keys is the same as the order of the indices on
427** the pTable->pIndex list. A key is only created for index i if
428** aIdxUsed!=0 and aIdxUsed[i]!=0.
drh9cfcf5d2002-01-29 18:41:24 +0000429**
430** This routine also generates code to check constraints. NOT NULL,
431** CHECK, and UNIQUE constraints are all checked. If a constraint fails,
drh1c928532002-01-31 15:54:21 +0000432** then the appropriate action is performed. There are five possible
433** actions: ROLLBACK, ABORT, FAIL, REPLACE, and IGNORE.
drh9cfcf5d2002-01-29 18:41:24 +0000434**
435** Constraint type Action What Happens
436** --------------- ---------- ----------------------------------------
drh1c928532002-01-31 15:54:21 +0000437** any ROLLBACK The current transaction is rolled back and
drh9cfcf5d2002-01-29 18:41:24 +0000438** sqlite_exec() returns immediately with a
439** return code of SQLITE_CONSTRAINT.
440**
drh1c928532002-01-31 15:54:21 +0000441** any ABORT Back out changes from the current command
442** only (do not do a complete rollback) then
443** cause sqlite_exec() to return immediately
444** with SQLITE_CONSTRAINT.
445**
446** any FAIL Sqlite_exec() returns immediately with a
447** return code of SQLITE_CONSTRAINT. The
448** transaction is not rolled back and any
449** prior changes are retained.
450**
drh9cfcf5d2002-01-29 18:41:24 +0000451** any IGNORE The record number and data is popped from
452** the stack and there is an immediate jump
453** to label ignoreDest.
454**
455** NOT NULL REPLACE The NULL value is replace by the default
456** value for that column. If the default value
457** is NULL, the action is the same as ABORT.
458**
459** UNIQUE REPLACE The other row that conflicts with the row
460** being inserted is removed.
461**
462** CHECK REPLACE Illegal. The results in an exception.
463**
drh1c928532002-01-31 15:54:21 +0000464** Which action to take is determined by the overrideError parameter.
465** Or if overrideError==OE_Default, then the pParse->onError parameter
466** is used. Or if pParse->onError==OE_Default then the onError value
467** for the constraint is used.
drh9cfcf5d2002-01-29 18:41:24 +0000468**
drhaaab5722002-02-19 13:39:21 +0000469** The calling routine must open a read/write cursor for pTab with
drh9cfcf5d2002-01-29 18:41:24 +0000470** cursor number "base". All indices of pTab must also have open
471** read/write cursors with cursor number base+i for the i-th cursor.
472** Except, if there is no possibility of a REPLACE action then
473** cursors do not need to be open for indices where aIdxUsed[i]==0.
474**
475** If the isUpdate flag is true, it means that the "base" cursor is
476** initially pointing to an entry that is being updated. The isUpdate
477** flag causes extra code to be generated so that the "base" cursor
478** is still pointing at the same entry after the routine returns.
479** Without the isUpdate flag, the "base" cursor might be moved.
480*/
481void sqliteGenerateConstraintChecks(
482 Parse *pParse, /* The parser context */
483 Table *pTab, /* the table into which we are inserting */
484 int base, /* Index of a read/write cursor pointing at pTab */
485 char *aIdxUsed, /* Which indices are used. NULL means all are used */
drh0ca3e242002-01-29 23:07:02 +0000486 int recnoChng, /* True if the record number will change */
drhb419a922002-01-30 16:17:23 +0000487 int isUpdate, /* True for UPDATE, False for INSERT */
drh9cfcf5d2002-01-29 18:41:24 +0000488 int overrideError, /* Override onError to this if not OE_Default */
drhb419a922002-01-30 16:17:23 +0000489 int ignoreDest /* Jump to this label on an OE_Ignore resolution */
drh9cfcf5d2002-01-29 18:41:24 +0000490){
491 int i;
492 Vdbe *v;
493 int nCol;
494 int onError;
495 int addr;
496 int extra;
drh0ca3e242002-01-29 23:07:02 +0000497 int iCur;
498 Index *pIdx;
499 int seenReplace = 0;
drhf5905aa2002-05-26 20:54:33 +0000500 int jumpInst1, jumpInst2;
drh0ca3e242002-01-29 23:07:02 +0000501 int contAddr;
drhb419a922002-01-30 16:17:23 +0000502 int hasTwoRecnos = (isUpdate && recnoChng);
drh9cfcf5d2002-01-29 18:41:24 +0000503
504 v = sqliteGetVdbe(pParse);
505 assert( v!=0 );
drh417be792002-03-03 18:59:40 +0000506 assert( pTab->pSelect==0 ); /* This table is not a VIEW */
drh9cfcf5d2002-01-29 18:41:24 +0000507 nCol = pTab->nCol;
508
509 /* Test all NOT NULL constraints.
510 */
511 for(i=0; i<nCol; i++){
drh0ca3e242002-01-29 23:07:02 +0000512 if( i==pTab->iPKey ){
513 /* Fix me: Make sure the INTEGER PRIMARY KEY is not NULL. */
514 continue;
515 }
drh9cfcf5d2002-01-29 18:41:24 +0000516 onError = pTab->aCol[i].notNull;
drh0ca3e242002-01-29 23:07:02 +0000517 if( onError==OE_None ) continue;
drh9cfcf5d2002-01-29 18:41:24 +0000518 if( overrideError!=OE_Default ){
519 onError = overrideError;
drh1c928532002-01-31 15:54:21 +0000520 }else if( onError==OE_Default ){
drh0d65dc02002-02-03 00:56:09 +0000521 onError = pParse->db->onError;
522 if( onError==OE_Default ) onError = OE_Abort;
drh9cfcf5d2002-01-29 18:41:24 +0000523 }
524 if( onError==OE_Replace && pTab->aCol[i].zDflt==0 ){
525 onError = OE_Abort;
526 }
drhef6764a2002-01-30 04:32:00 +0000527 sqliteVdbeAddOp(v, OP_Dup, nCol-1-i, 1);
drhf5905aa2002-05-26 20:54:33 +0000528 addr = sqliteVdbeAddOp(v, OP_NotNull, 1, 0);
drh9cfcf5d2002-01-29 18:41:24 +0000529 switch( onError ){
drh1c928532002-01-31 15:54:21 +0000530 case OE_Rollback:
531 case OE_Abort:
532 case OE_Fail: {
533 sqliteVdbeAddOp(v, OP_Halt, SQLITE_CONSTRAINT, onError);
drh9cfcf5d2002-01-29 18:41:24 +0000534 break;
535 }
536 case OE_Ignore: {
drhb419a922002-01-30 16:17:23 +0000537 sqliteVdbeAddOp(v, OP_Pop, nCol+1+hasTwoRecnos, 0);
drh0ca3e242002-01-29 23:07:02 +0000538 sqliteVdbeAddOp(v, OP_Goto, 0, ignoreDest);
drh9cfcf5d2002-01-29 18:41:24 +0000539 break;
540 }
541 case OE_Replace: {
542 sqliteVdbeAddOp(v, OP_String, 0, 0);
543 sqliteVdbeChangeP3(v, -1, pTab->aCol[i].zDflt, P3_STATIC);
544 sqliteVdbeAddOp(v, OP_Push, nCol-i, 0);
545 break;
546 }
drh0ca3e242002-01-29 23:07:02 +0000547 default: assert(0);
drh9cfcf5d2002-01-29 18:41:24 +0000548 }
drhef6764a2002-01-30 04:32:00 +0000549 sqliteVdbeChangeP2(v, addr, sqliteVdbeCurrentAddr(v));
drh9cfcf5d2002-01-29 18:41:24 +0000550 }
551
552 /* Test all CHECK constraints
553 */
drh0bd1f4e2002-06-06 18:54:39 +0000554 /**** TBD ****/
drh9cfcf5d2002-01-29 18:41:24 +0000555
drh0bd1f4e2002-06-06 18:54:39 +0000556 /* If we have an INTEGER PRIMARY KEY, make sure the primary key
557 ** of the new record does not previously exist. Except, if this
558 ** is an UPDATE and the primary key is not changing, that is OK.
559 ** Also, if the conflict resolution policy is REPLACE, then we
560 ** can skip this test.
drh9cfcf5d2002-01-29 18:41:24 +0000561 */
drh0d65dc02002-02-03 00:56:09 +0000562 if( (recnoChng || !isUpdate) && pTab->iPKey>=0 ){
drh0ca3e242002-01-29 23:07:02 +0000563 onError = pTab->keyConf;
564 if( overrideError!=OE_Default ){
565 onError = overrideError;
drh1c928532002-01-31 15:54:21 +0000566 }else if( onError==OE_Default ){
drh0d65dc02002-02-03 00:56:09 +0000567 onError = pParse->db->onError;
568 if( onError==OE_Default ) onError = OE_Abort;
drh0ca3e242002-01-29 23:07:02 +0000569 }
drh0d65dc02002-02-03 00:56:09 +0000570 if( onError!=OE_Replace ){
drh79b0c952002-05-21 12:56:43 +0000571 if( isUpdate ){
572 sqliteVdbeAddOp(v, OP_Dup, nCol+1, 1);
573 sqliteVdbeAddOp(v, OP_Dup, nCol+1, 1);
drhf5905aa2002-05-26 20:54:33 +0000574 jumpInst1 = sqliteVdbeAddOp(v, OP_Eq, 0, 0);
drh79b0c952002-05-21 12:56:43 +0000575 }
drh0d65dc02002-02-03 00:56:09 +0000576 sqliteVdbeAddOp(v, OP_Dup, nCol, 1);
drhf5905aa2002-05-26 20:54:33 +0000577 jumpInst2 = sqliteVdbeAddOp(v, OP_NotExists, base, 0);
drh0d65dc02002-02-03 00:56:09 +0000578 switch( onError ){
579 case OE_Rollback:
580 case OE_Abort:
581 case OE_Fail: {
582 sqliteVdbeAddOp(v, OP_Halt, SQLITE_CONSTRAINT, onError);
583 break;
584 }
585 case OE_Ignore: {
586 sqliteVdbeAddOp(v, OP_Pop, nCol+1+hasTwoRecnos, 0);
587 sqliteVdbeAddOp(v, OP_Goto, 0, ignoreDest);
588 break;
589 }
590 default: assert(0);
drh0ca3e242002-01-29 23:07:02 +0000591 }
drh0d65dc02002-02-03 00:56:09 +0000592 contAddr = sqliteVdbeCurrentAddr(v);
drhf5905aa2002-05-26 20:54:33 +0000593 sqliteVdbeChangeP2(v, jumpInst2, contAddr);
drh0d65dc02002-02-03 00:56:09 +0000594 if( isUpdate ){
drhf5905aa2002-05-26 20:54:33 +0000595 sqliteVdbeChangeP2(v, jumpInst1, contAddr);
drh0d65dc02002-02-03 00:56:09 +0000596 sqliteVdbeAddOp(v, OP_Dup, nCol+1, 1);
597 sqliteVdbeAddOp(v, OP_MoveTo, base, 0);
drh0ca3e242002-01-29 23:07:02 +0000598 }
drh0ca3e242002-01-29 23:07:02 +0000599 }
600 }
drh0bd1f4e2002-06-06 18:54:39 +0000601
602 /* Test all UNIQUE constraints by creating entries for each UNIQUE
603 ** index and making sure that duplicate entries do not already exist.
604 ** Add the new records to the indices as we go.
605 */
drh9cfcf5d2002-01-29 18:41:24 +0000606 extra = 0;
607 for(extra=(-1), iCur=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, iCur++){
drh9cfcf5d2002-01-29 18:41:24 +0000608 if( aIdxUsed && aIdxUsed[iCur]==0 ) continue;
609 extra++;
610 sqliteVdbeAddOp(v, OP_Dup, nCol+extra, 1);
611 for(i=0; i<pIdx->nColumn; i++){
612 int idx = pIdx->aiColumn[i];
613 if( idx==pTab->iPKey ){
drh0ca3e242002-01-29 23:07:02 +0000614 sqliteVdbeAddOp(v, OP_Dup, i+extra+nCol+1, 1);
drh9cfcf5d2002-01-29 18:41:24 +0000615 }else{
drh0ca3e242002-01-29 23:07:02 +0000616 sqliteVdbeAddOp(v, OP_Dup, i+extra+nCol-idx, 1);
drh9cfcf5d2002-01-29 18:41:24 +0000617 }
618 }
drhf5905aa2002-05-26 20:54:33 +0000619 jumpInst1 = sqliteVdbeAddOp(v, OP_MakeIdxKey, pIdx->nColumn, 0);
drh9cfcf5d2002-01-29 18:41:24 +0000620 onError = pIdx->onError;
621 if( onError==OE_None ) continue;
622 if( overrideError!=OE_Default ){
623 onError = overrideError;
drh1c928532002-01-31 15:54:21 +0000624 }else if( onError==OE_Default ){
drh0d65dc02002-02-03 00:56:09 +0000625 onError = pParse->db->onError;
626 if( onError==OE_Default ) onError = OE_Abort;
drh9cfcf5d2002-01-29 18:41:24 +0000627 }
drhb419a922002-01-30 16:17:23 +0000628 sqliteVdbeAddOp(v, OP_Dup, extra+nCol+1+hasTwoRecnos, 1);
drhf5905aa2002-05-26 20:54:33 +0000629 jumpInst2 = sqliteVdbeAddOp(v, OP_IsUnique, base+iCur+1, 0);
drh9cfcf5d2002-01-29 18:41:24 +0000630 switch( onError ){
drh1c928532002-01-31 15:54:21 +0000631 case OE_Rollback:
632 case OE_Abort:
633 case OE_Fail: {
634 sqliteVdbeAddOp(v, OP_Halt, SQLITE_CONSTRAINT, onError);
drh9cfcf5d2002-01-29 18:41:24 +0000635 break;
636 }
637 case OE_Ignore: {
drh0ca3e242002-01-29 23:07:02 +0000638 assert( seenReplace==0 );
drhfe1a1772002-04-09 03:15:06 +0000639 sqliteVdbeAddOp(v, OP_Pop, nCol+extra+3+hasTwoRecnos, 0);
drh9cfcf5d2002-01-29 18:41:24 +0000640 sqliteVdbeAddOp(v, OP_Goto, 0, ignoreDest);
drh9cfcf5d2002-01-29 18:41:24 +0000641 break;
642 }
643 case OE_Replace: {
drhc8d30ac2002-04-12 10:08:59 +0000644 sqliteGenerateRowDelete(v, pTab, base, 0);
drh9cfcf5d2002-01-29 18:41:24 +0000645 if( isUpdate ){
drhb419a922002-01-30 16:17:23 +0000646 sqliteVdbeAddOp(v, OP_Dup, nCol+extra+1+hasTwoRecnos, 1);
drh0ca3e242002-01-29 23:07:02 +0000647 sqliteVdbeAddOp(v, OP_MoveTo, base, 0);
drh9cfcf5d2002-01-29 18:41:24 +0000648 }
drh0ca3e242002-01-29 23:07:02 +0000649 seenReplace = 1;
drh9cfcf5d2002-01-29 18:41:24 +0000650 break;
651 }
drh0ca3e242002-01-29 23:07:02 +0000652 default: assert(0);
drh9cfcf5d2002-01-29 18:41:24 +0000653 }
654 contAddr = sqliteVdbeCurrentAddr(v);
drh0bd1f4e2002-06-06 18:54:39 +0000655#if NULL_DISTINCT_FOR_UNIQUE
drhf5905aa2002-05-26 20:54:33 +0000656 sqliteVdbeChangeP2(v, jumpInst1, contAddr);
drh0bd1f4e2002-06-06 18:54:39 +0000657#endif
drhf5905aa2002-05-26 20:54:33 +0000658 sqliteVdbeChangeP2(v, jumpInst2, contAddr);
drh9cfcf5d2002-01-29 18:41:24 +0000659 }
660}
drh0ca3e242002-01-29 23:07:02 +0000661
662/*
663** This routine generates code to finish the INSERT or UPDATE operation
664** that was started by a prior call to sqliteGenerateConstraintChecks.
665** The stack must contain keys for all active indices followed by data
666** and the recno for the new entry. This routine creates the new
667** entries in all indices and in the main table.
668**
drhb419a922002-01-30 16:17:23 +0000669** The arguments to this routine should be the same as the first six
drh0ca3e242002-01-29 23:07:02 +0000670** arguments to sqliteGenerateConstraintChecks.
671*/
672void sqliteCompleteInsertion(
673 Parse *pParse, /* The parser context */
674 Table *pTab, /* the table into which we are inserting */
675 int base, /* Index of a read/write cursor pointing at pTab */
676 char *aIdxUsed, /* Which indices are used. NULL means all are used */
drhb419a922002-01-30 16:17:23 +0000677 int recnoChng, /* True if the record number will change */
678 int isUpdate /* True for UPDATE, False for INSERT */
drh0ca3e242002-01-29 23:07:02 +0000679){
680 int i;
681 Vdbe *v;
682 int nIdx;
683 Index *pIdx;
684
685 v = sqliteGetVdbe(pParse);
686 assert( v!=0 );
drh417be792002-03-03 18:59:40 +0000687 assert( pTab->pSelect==0 ); /* This table is not a VIEW */
drh0ca3e242002-01-29 23:07:02 +0000688 for(nIdx=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, nIdx++){}
689 for(i=nIdx-1; i>=0; i--){
690 if( aIdxUsed && aIdxUsed[i]==0 ) continue;
691 sqliteVdbeAddOp(v, OP_IdxPut, base+i+1, 0);
692 }
693 sqliteVdbeAddOp(v, OP_MakeRecord, pTab->nCol, 0);
danielk1977c3f9bad2002-05-15 08:30:12 +0000694 sqliteVdbeAddOp(v, OP_PutIntKey, base, pParse->trigStack?0:1);
drhb419a922002-01-30 16:17:23 +0000695 if( isUpdate && recnoChng ){
drh0ca3e242002-01-29 23:07:02 +0000696 sqliteVdbeAddOp(v, OP_Pop, 1, 0);
697 }
698}