blob: b5d74f14bd54b2821850681ee43626acbcefa205 [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**
drhf573c992002-07-31 00:32:50 +000015** $Id: insert.c,v 1.65 2002/07/31 00:32:50 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 */
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
drhf573c992002-07-31 00:32:50 +000094 /* If pTab is really a view, make sure it has been initialized.
95 */
96 if( pTab->pSelect ){
97 if( sqliteViewGetColumnNames(pParse, pTab) ){
98 goto insert_cleanup;
99 }
100 }
101
drh1ccde152000-06-17 13:12:39 +0000102 /* Allocate a VDBE
103 */
drhd8bc7082000-06-07 23:51:50 +0000104 v = sqliteGetVdbe(pParse);
drh5974a302000-06-07 14:42:26 +0000105 if( v==0 ) goto insert_cleanup;
drhc977f7f2002-05-21 11:38:11 +0000106 sqliteBeginWriteOperation(pParse, pSelect || row_triggers_exist);
drh1ccde152000-06-17 13:12:39 +0000107
danielk1977c3f9bad2002-05-15 08:30:12 +0000108 /* if there are row triggers, allocate a temp table for new.* references. */
danielk1977f29ce552002-05-19 23:43:12 +0000109 if( row_triggers_exist ){
danielk1977c3f9bad2002-05-15 08:30:12 +0000110 newIdx = pParse->nTab++;
danielk1977f29ce552002-05-19 23:43:12 +0000111 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000112
drh1ccde152000-06-17 13:12:39 +0000113 /* Figure out how many columns of data are supplied. If the data
drhc6b52df2002-01-04 03:09:29 +0000114 ** is coming from a SELECT statement, then this step has to generate
drh1ccde152000-06-17 13:12:39 +0000115 ** all the code to implement the SELECT statement and leave the data
116 ** in a temporary table. If data is coming from an expression list,
117 ** then we just have to count the number of expressions.
118 */
drh5974a302000-06-07 14:42:26 +0000119 if( pSelect ){
120 int rc;
121 srcTab = pParse->nTab++;
drh99fcd712001-10-13 01:06:47 +0000122 sqliteVdbeAddOp(v, OP_OpenTemp, srcTab, 0);
drh832508b2002-03-02 17:04:07 +0000123 rc = sqliteSelect(pParse, pSelect, SRT_Table, srcTab, 0,0,0);
drhdaffd0e2001-04-11 14:28:42 +0000124 if( rc || pParse->nErr || sqlite_malloc_failed ) goto insert_cleanup;
drh5974a302000-06-07 14:42:26 +0000125 assert( pSelect->pEList );
drh967e8b72000-06-21 13:59:10 +0000126 nColumn = pSelect->pEList->nExpr;
drh5974a302000-06-07 14:42:26 +0000127 }else{
drhad3cab52002-05-24 02:04:32 +0000128 SrcList dummy;
drhdaffd0e2001-04-11 14:28:42 +0000129 assert( pList!=0 );
drh5974a302000-06-07 14:42:26 +0000130 srcTab = -1;
131 assert( pList );
drh967e8b72000-06-21 13:59:10 +0000132 nColumn = pList->nExpr;
drhad3cab52002-05-24 02:04:32 +0000133 dummy.nSrc = 0;
drhe64e7b22002-02-18 13:56:36 +0000134 for(i=0; i<nColumn; i++){
drh832508b2002-03-02 17:04:07 +0000135 if( sqliteExprResolveIds(pParse, 0, &dummy, 0, pList->a[i].pExpr) ){
drhe64e7b22002-02-18 13:56:36 +0000136 goto insert_cleanup;
137 }
drhb04a5d82002-04-12 03:55:15 +0000138 if( sqliteExprCheck(pParse, pList->a[i].pExpr, 0, 0) ){
139 goto insert_cleanup;
140 }
drhe64e7b22002-02-18 13:56:36 +0000141 }
drh5974a302000-06-07 14:42:26 +0000142 }
drh1ccde152000-06-17 13:12:39 +0000143
144 /* Make sure the number of columns in the source data matches the number
145 ** of columns to be inserted into the table.
146 */
drh967e8b72000-06-21 13:59:10 +0000147 if( pColumn==0 && nColumn!=pTab->nCol ){
drhcce7d172000-05-31 15:34:51 +0000148 char zNum1[30];
149 char zNum2[30];
drh967e8b72000-06-21 13:59:10 +0000150 sprintf(zNum1,"%d", nColumn);
drhcce7d172000-05-31 15:34:51 +0000151 sprintf(zNum2,"%d", pTab->nCol);
152 sqliteSetString(&pParse->zErrMsg, "table ", pTab->zName,
153 " has ", zNum2, " columns but ",
154 zNum1, " values were supplied", 0);
155 pParse->nErr++;
156 goto insert_cleanup;
157 }
drh967e8b72000-06-21 13:59:10 +0000158 if( pColumn!=0 && nColumn!=pColumn->nId ){
drhcce7d172000-05-31 15:34:51 +0000159 char zNum1[30];
160 char zNum2[30];
drh967e8b72000-06-21 13:59:10 +0000161 sprintf(zNum1,"%d", nColumn);
162 sprintf(zNum2,"%d", pColumn->nId);
drhcce7d172000-05-31 15:34:51 +0000163 sqliteSetString(&pParse->zErrMsg, zNum1, " values for ",
164 zNum2, " columns", 0);
165 pParse->nErr++;
166 goto insert_cleanup;
167 }
drh1ccde152000-06-17 13:12:39 +0000168
169 /* If the INSERT statement included an IDLIST term, then make sure
170 ** all elements of the IDLIST really are columns of the table and
171 ** remember the column indices.
drhc8392582001-12-31 02:48:51 +0000172 **
173 ** If the table has an INTEGER PRIMARY KEY column and that column
174 ** is named in the IDLIST, then record in the keyColumn variable
175 ** the index into IDLIST of the primary key column. keyColumn is
176 ** the index of the primary key as it appears in IDLIST, not as
177 ** is appears in the original table. (The index of the primary
178 ** key in the original table is pTab->iPKey.)
drh1ccde152000-06-17 13:12:39 +0000179 */
drh967e8b72000-06-21 13:59:10 +0000180 if( pColumn ){
181 for(i=0; i<pColumn->nId; i++){
182 pColumn->a[i].idx = -1;
drhcce7d172000-05-31 15:34:51 +0000183 }
drh967e8b72000-06-21 13:59:10 +0000184 for(i=0; i<pColumn->nId; i++){
drhcce7d172000-05-31 15:34:51 +0000185 for(j=0; j<pTab->nCol; j++){
drh967e8b72000-06-21 13:59:10 +0000186 if( sqliteStrICmp(pColumn->a[i].zName, pTab->aCol[j].zName)==0 ){
187 pColumn->a[i].idx = j;
drh4a324312001-12-21 14:30:42 +0000188 if( j==pTab->iPKey ){
drh9aa028d2001-12-22 21:48:29 +0000189 keyColumn = i;
drh4a324312001-12-21 14:30:42 +0000190 }
drhcce7d172000-05-31 15:34:51 +0000191 break;
192 }
193 }
194 if( j>=pTab->nCol ){
195 sqliteSetString(&pParse->zErrMsg, "table ", pTab->zName,
drh967e8b72000-06-21 13:59:10 +0000196 " has no column named ", pColumn->a[i].zName, 0);
drhcce7d172000-05-31 15:34:51 +0000197 pParse->nErr++;
198 goto insert_cleanup;
199 }
200 }
201 }
drh1ccde152000-06-17 13:12:39 +0000202
drhaacc5432002-01-06 17:07:40 +0000203 /* If there is no IDLIST term but the table has an integer primary
drhc8392582001-12-31 02:48:51 +0000204 ** key, the set the keyColumn variable to the primary key column index
205 ** in the original table definition.
drh4a324312001-12-21 14:30:42 +0000206 */
207 if( pColumn==0 ){
208 keyColumn = pTab->iPKey;
209 }
210
danielk1977c3f9bad2002-05-15 08:30:12 +0000211 /* Open the temp table for FOR EACH ROW triggers */
danielk1977f29ce552002-05-19 23:43:12 +0000212 if( row_triggers_exist ){
danielk1977c3f9bad2002-05-15 08:30:12 +0000213 sqliteVdbeAddOp(v, OP_OpenTemp, newIdx, 0);
danielk1977f29ce552002-05-19 23:43:12 +0000214 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000215
drhfeeb1392002-04-09 03:28:01 +0000216 /* Initialize the count of rows to be inserted
217 */
danielk1977f29ce552002-05-19 23:43:12 +0000218 if( db->flags & SQLITE_CountRows && !pParse->trigStack ){
drhfeeb1392002-04-09 03:28:01 +0000219 sqliteVdbeAddOp(v, OP_Integer, 0, 0); /* Initialize the row count */
220 }
221
danielk1977c3f9bad2002-05-15 08:30:12 +0000222 /* Open tables and indices if there are no row triggers */
danielk1977f29ce552002-05-19 23:43:12 +0000223 if( !row_triggers_exist ){
danielk1977c3f9bad2002-05-15 08:30:12 +0000224 base = pParse->nTab;
225 openOp = pTab->isTemp ? OP_OpenWrAux : OP_OpenWrite;
226 sqliteVdbeAddOp(v, openOp, base, pTab->tnum);
227 sqliteVdbeChangeP3(v, -1, pTab->zName, P3_STATIC);
228 for(idx=1, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, idx++){
229 sqliteVdbeAddOp(v, openOp, idx+base, pIdx->tnum);
230 sqliteVdbeChangeP3(v, -1, pIdx->zName, P3_STATIC);
231 }
232 pParse->nTab += idx;
233 }
234
drh1ccde152000-06-17 13:12:39 +0000235 /* If the data source is a SELECT statement, then we have to create
236 ** a loop because there might be multiple rows of data. If the data
237 ** source is an expression list, then exactly one row will be inserted
238 ** and the loop is not used.
239 */
drh5974a302000-06-07 14:42:26 +0000240 if( srcTab>=0 ){
drh5974a302000-06-07 14:42:26 +0000241 iBreak = sqliteVdbeMakeLabel(v);
drh6b563442001-11-07 16:48:26 +0000242 sqliteVdbeAddOp(v, OP_Rewind, srcTab, iBreak);
243 iCont = sqliteVdbeCurrentAddr(v);
drh5974a302000-06-07 14:42:26 +0000244 }
drh1ccde152000-06-17 13:12:39 +0000245
danielk19776f349032002-06-11 02:25:40 +0000246 endOfLoop = sqliteVdbeMakeLabel(v);
danielk1977f29ce552002-05-19 23:43:12 +0000247 if( row_triggers_exist ){
danielk1977c3f9bad2002-05-15 08:30:12 +0000248
249 /* build the new.* reference row */
250 sqliteVdbeAddOp(v, OP_Integer, 13, 0);
251 for(i=0; i<pTab->nCol; i++){
252 if( pColumn==0 ){
drh9adf9ac2002-05-15 11:44:13 +0000253 j = i;
danielk1977c3f9bad2002-05-15 08:30:12 +0000254 }else{
drh9adf9ac2002-05-15 11:44:13 +0000255 for(j=0; j<pColumn->nId; j++){
256 if( pColumn->a[j].idx==i ) break;
257 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000258 }
259 if( pColumn && j>=pColumn->nId ){
drh9adf9ac2002-05-15 11:44:13 +0000260 sqliteVdbeAddOp(v, OP_String, 0, 0);
261 sqliteVdbeChangeP3(v, -1, pTab->aCol[i].zDflt, P3_STATIC);
danielk1977c3f9bad2002-05-15 08:30:12 +0000262 }else if( srcTab>=0 ){
drh9adf9ac2002-05-15 11:44:13 +0000263 sqliteVdbeAddOp(v, OP_Column, srcTab, j);
danielk1977c3f9bad2002-05-15 08:30:12 +0000264 }else{
drh9adf9ac2002-05-15 11:44:13 +0000265 sqliteExprCode(pParse, pList->a[j].pExpr);
danielk1977c3f9bad2002-05-15 08:30:12 +0000266 }
267 }
268 sqliteVdbeAddOp(v, OP_MakeRecord, pTab->nCol, 0);
269 sqliteVdbeAddOp(v, OP_PutIntKey, newIdx, 0);
270 sqliteVdbeAddOp(v, OP_Rewind, newIdx, 0);
271
272 /* Fire BEFORE triggers */
danielk1977f29ce552002-05-19 23:43:12 +0000273 if( sqliteCodeRowTrigger(pParse, TK_INSERT, 0, TK_BEFORE, pTab, newIdx, -1,
danielk19776f349032002-06-11 02:25:40 +0000274 onError, endOfLoop) ){
danielk1977f29ce552002-05-19 23:43:12 +0000275 goto insert_cleanup;
276 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000277
278 /* Open the tables and indices for the INSERT */
danielk1977f29ce552002-05-19 23:43:12 +0000279 if( !pTab->pSelect ){
danielk1977c3f9bad2002-05-15 08:30:12 +0000280 base = pParse->nTab;
281 openOp = pTab->isTemp ? OP_OpenWrAux : OP_OpenWrite;
282 sqliteVdbeAddOp(v, openOp, base, pTab->tnum);
283 sqliteVdbeChangeP3(v, -1, pTab->zName, P3_STATIC);
284 for(idx=1, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, idx++){
drh9adf9ac2002-05-15 11:44:13 +0000285 sqliteVdbeAddOp(v, openOp, idx+base, pIdx->tnum);
286 sqliteVdbeChangeP3(v, -1, pIdx->zName, P3_STATIC);
danielk1977c3f9bad2002-05-15 08:30:12 +0000287 }
288 pParse->nTab += idx;
289 }
290 }
291
drh4a324312001-12-21 14:30:42 +0000292 /* Push the record number for the new entry onto the stack. The
293 ** record number is a randomly generate integer created by NewRecno
294 ** except when the table has an INTEGER PRIMARY KEY column, in which
drhb419a922002-01-30 16:17:23 +0000295 ** case the record number is the same as that column.
drh1ccde152000-06-17 13:12:39 +0000296 */
danielk1977f29ce552002-05-19 23:43:12 +0000297 if( !pTab->pSelect ){
danielk1977c3f9bad2002-05-15 08:30:12 +0000298 if( keyColumn>=0 ){
299 if( srcTab>=0 ){
drh9adf9ac2002-05-15 11:44:13 +0000300 sqliteVdbeAddOp(v, OP_Column, srcTab, keyColumn);
danielk1977c3f9bad2002-05-15 08:30:12 +0000301 }else{
drh9adf9ac2002-05-15 11:44:13 +0000302 sqliteExprCode(pParse, pList->a[keyColumn].pExpr);
danielk1977c3f9bad2002-05-15 08:30:12 +0000303 }
drh27a32782002-06-19 20:32:43 +0000304 /* If the PRIMARY KEY expression is NULL, then use OP_NewRecno
305 ** to generate a unique primary key value.
306 */
307 sqliteVdbeAddOp(v, OP_NotNull, -1, sqliteVdbeCurrentAddr(v)+3);
308 sqliteVdbeAddOp(v, OP_Pop, 1, 0);
309 sqliteVdbeAddOp(v, OP_NewRecno, base, 0);
danielk1977c3f9bad2002-05-15 08:30:12 +0000310 sqliteVdbeAddOp(v, OP_MustBeInt, 0, 0);
311 }else{
drhe1e68f42002-03-31 18:29:03 +0000312 sqliteVdbeAddOp(v, OP_NewRecno, base, 0);
drh4a324312001-12-21 14:30:42 +0000313 }
drh4a324312001-12-21 14:30:42 +0000314
danielk1977c3f9bad2002-05-15 08:30:12 +0000315 /* Push onto the stack, data for all columns of the new entry, beginning
danielk1977f29ce552002-05-19 23:43:12 +0000316 ** with the first column.
317 */
danielk1977c3f9bad2002-05-15 08:30:12 +0000318 for(i=0; i<pTab->nCol; i++){
319 if( i==pTab->iPKey ){
drh9adf9ac2002-05-15 11:44:13 +0000320 /* The value of the INTEGER PRIMARY KEY column is always a NULL.
danielk1977f29ce552002-05-19 23:43:12 +0000321 ** Whenever this column is read, the record number will be substituted
322 ** in its place. So will fill this column with a NULL to avoid
323 ** taking up data space with information that will never be used. */
drh9adf9ac2002-05-15 11:44:13 +0000324 sqliteVdbeAddOp(v, OP_String, 0, 0);
325 continue;
danielk1977c3f9bad2002-05-15 08:30:12 +0000326 }
327 if( pColumn==0 ){
drh9adf9ac2002-05-15 11:44:13 +0000328 j = i;
danielk1977c3f9bad2002-05-15 08:30:12 +0000329 }else{
drh9adf9ac2002-05-15 11:44:13 +0000330 for(j=0; j<pColumn->nId; j++){
331 if( pColumn->a[j].idx==i ) break;
332 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000333 }
334 if( pColumn && j>=pColumn->nId ){
drh9adf9ac2002-05-15 11:44:13 +0000335 sqliteVdbeAddOp(v, OP_String, 0, 0);
336 sqliteVdbeChangeP3(v, -1, pTab->aCol[i].zDflt, P3_STATIC);
danielk1977c3f9bad2002-05-15 08:30:12 +0000337 }else if( srcTab>=0 ){
drh9adf9ac2002-05-15 11:44:13 +0000338 sqliteVdbeAddOp(v, OP_Column, srcTab, j);
danielk1977c3f9bad2002-05-15 08:30:12 +0000339 }else{
drh9adf9ac2002-05-15 11:44:13 +0000340 sqliteExprCode(pParse, pList->a[j].pExpr);
drh5974a302000-06-07 14:42:26 +0000341 }
drhbed86902000-06-02 13:27:59 +0000342 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000343
344 /* Generate code to check constraints and generate index keys and
danielk1977f29ce552002-05-19 23:43:12 +0000345 ** do the insertion.
346 */
danielk1977c3f9bad2002-05-15 08:30:12 +0000347 sqliteGenerateConstraintChecks(pParse, pTab, base, 0,0,0,onError,endOfLoop);
348 sqliteCompleteInsertion(pParse, pTab, base, 0,0,0);
349
350 /* Update the count of rows that are inserted
danielk1977f29ce552002-05-19 23:43:12 +0000351 */
danielk1977c3f9bad2002-05-15 08:30:12 +0000352 if( (db->flags & SQLITE_CountRows)!=0 && !pParse->trigStack){
353 sqliteVdbeAddOp(v, OP_AddImm, 1, 0);
drh5974a302000-06-07 14:42:26 +0000354 }
355 }
drh1ccde152000-06-17 13:12:39 +0000356
danielk1977f29ce552002-05-19 23:43:12 +0000357 if( row_triggers_exist ){
danielk1977c3f9bad2002-05-15 08:30:12 +0000358 /* Close all tables opened */
danielk1977f29ce552002-05-19 23:43:12 +0000359 if( !pTab->pSelect ){
danielk1977c3f9bad2002-05-15 08:30:12 +0000360 sqliteVdbeAddOp(v, OP_Close, base, 0);
361 for(idx=1, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, idx++){
drh9adf9ac2002-05-15 11:44:13 +0000362 sqliteVdbeAddOp(v, OP_Close, idx+base, 0);
danielk1977c3f9bad2002-05-15 08:30:12 +0000363 }
364 }
drh1bee3d72001-10-15 00:44:35 +0000365
danielk1977c3f9bad2002-05-15 08:30:12 +0000366 /* Code AFTER triggers */
danielk1977f29ce552002-05-19 23:43:12 +0000367 if( sqliteCodeRowTrigger(pParse, TK_INSERT, 0, TK_AFTER, pTab, newIdx, -1,
danielk19776f349032002-06-11 02:25:40 +0000368 onError, endOfLoop) ){
danielk1977f29ce552002-05-19 23:43:12 +0000369 goto insert_cleanup;
370 }
drh1bee3d72001-10-15 00:44:35 +0000371 }
372
drh1ccde152000-06-17 13:12:39 +0000373 /* The bottom of the loop, if the data source is a SELECT statement
danielk1977f29ce552002-05-19 23:43:12 +0000374 */
drh0ca3e242002-01-29 23:07:02 +0000375 sqliteVdbeResolveLabel(v, endOfLoop);
drh5974a302000-06-07 14:42:26 +0000376 if( srcTab>=0 ){
drh6b563442001-11-07 16:48:26 +0000377 sqliteVdbeAddOp(v, OP_Next, srcTab, iCont);
drh99fcd712001-10-13 01:06:47 +0000378 sqliteVdbeResolveLabel(v, iBreak);
drh6b563442001-11-07 16:48:26 +0000379 sqliteVdbeAddOp(v, OP_Close, srcTab, 0);
380 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000381
danielk1977f29ce552002-05-19 23:43:12 +0000382 if( !row_triggers_exist ){
danielk1977c3f9bad2002-05-15 08:30:12 +0000383 /* Close all tables opened */
384 sqliteVdbeAddOp(v, OP_Close, base, 0);
385 for(idx=1, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, idx++){
386 sqliteVdbeAddOp(v, OP_Close, idx+base, 0);
387 }
drhcce7d172000-05-31 15:34:51 +0000388 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000389
drh1c928532002-01-31 15:54:21 +0000390 sqliteEndWriteOperation(pParse);
drh5e00f6c2001-09-13 13:46:56 +0000391
drh1bee3d72001-10-15 00:44:35 +0000392 /*
danielk1977f29ce552002-05-19 23:43:12 +0000393 ** Return the number of rows inserted.
drh1bee3d72001-10-15 00:44:35 +0000394 */
danielk1977c3f9bad2002-05-15 08:30:12 +0000395 if( db->flags & SQLITE_CountRows && !pParse->trigStack ){
drh1bee3d72001-10-15 00:44:35 +0000396 sqliteVdbeAddOp(v, OP_ColumnCount, 1, 0);
397 sqliteVdbeAddOp(v, OP_ColumnName, 0, 0);
398 sqliteVdbeChangeP3(v, -1, "rows inserted", P3_STATIC);
drh1bee3d72001-10-15 00:44:35 +0000399 sqliteVdbeAddOp(v, OP_Callback, 1, 0);
400 }
drhcce7d172000-05-31 15:34:51 +0000401
402insert_cleanup:
drh5974a302000-06-07 14:42:26 +0000403 if( pList ) sqliteExprListDelete(pList);
404 if( pSelect ) sqliteSelectDelete(pSelect);
danielk1977c3f9bad2002-05-15 08:30:12 +0000405 if ( zTab ) sqliteFree(zTab);
drh967e8b72000-06-21 13:59:10 +0000406 sqliteIdListDelete(pColumn);
drhcce7d172000-05-31 15:34:51 +0000407}
drh9cfcf5d2002-01-29 18:41:24 +0000408
drh9cfcf5d2002-01-29 18:41:24 +0000409/*
410** Generate code to do a constraint check prior to an INSERT or an UPDATE.
411**
412** When this routine is called, the stack contains (from bottom to top)
drh0ca3e242002-01-29 23:07:02 +0000413** the following values:
414**
drhb419a922002-01-30 16:17:23 +0000415** 1. The recno of the row to be updated before it is updated. This
416** value is omitted unless we are doing an UPDATE that involves a
417** change to the record number.
drh0ca3e242002-01-29 23:07:02 +0000418**
drhb419a922002-01-30 16:17:23 +0000419** 2. The recno of the row after the update.
drh0ca3e242002-01-29 23:07:02 +0000420**
421** 3. The data in the first column of the entry after the update.
422**
423** i. Data from middle columns...
424**
425** N. The data in the last column of the entry after the update.
426**
drhb419a922002-01-30 16:17:23 +0000427** The old recno shown as entry (1) above is omitted unless both isUpdate
drh1c928532002-01-31 15:54:21 +0000428** and recnoChng are 1. isUpdate is true for UPDATEs and false for
429** INSERTs and recnoChng is true if the record number is being changed.
drh0ca3e242002-01-29 23:07:02 +0000430**
431** The code generated by this routine pushes additional entries onto
432** the stack which are the keys for new index entries for the new record.
433** The order of index keys is the same as the order of the indices on
434** the pTable->pIndex list. A key is only created for index i if
435** aIdxUsed!=0 and aIdxUsed[i]!=0.
drh9cfcf5d2002-01-29 18:41:24 +0000436**
437** This routine also generates code to check constraints. NOT NULL,
438** CHECK, and UNIQUE constraints are all checked. If a constraint fails,
drh1c928532002-01-31 15:54:21 +0000439** then the appropriate action is performed. There are five possible
440** actions: ROLLBACK, ABORT, FAIL, REPLACE, and IGNORE.
drh9cfcf5d2002-01-29 18:41:24 +0000441**
442** Constraint type Action What Happens
443** --------------- ---------- ----------------------------------------
drh1c928532002-01-31 15:54:21 +0000444** any ROLLBACK The current transaction is rolled back and
drh9cfcf5d2002-01-29 18:41:24 +0000445** sqlite_exec() returns immediately with a
446** return code of SQLITE_CONSTRAINT.
447**
drh1c928532002-01-31 15:54:21 +0000448** any ABORT Back out changes from the current command
449** only (do not do a complete rollback) then
450** cause sqlite_exec() to return immediately
451** with SQLITE_CONSTRAINT.
452**
453** any FAIL Sqlite_exec() returns immediately with a
454** return code of SQLITE_CONSTRAINT. The
455** transaction is not rolled back and any
456** prior changes are retained.
457**
drh9cfcf5d2002-01-29 18:41:24 +0000458** any IGNORE The record number and data is popped from
459** the stack and there is an immediate jump
460** to label ignoreDest.
461**
462** NOT NULL REPLACE The NULL value is replace by the default
463** value for that column. If the default value
464** is NULL, the action is the same as ABORT.
465**
466** UNIQUE REPLACE The other row that conflicts with the row
467** being inserted is removed.
468**
469** CHECK REPLACE Illegal. The results in an exception.
470**
drh1c928532002-01-31 15:54:21 +0000471** Which action to take is determined by the overrideError parameter.
472** Or if overrideError==OE_Default, then the pParse->onError parameter
473** is used. Or if pParse->onError==OE_Default then the onError value
474** for the constraint is used.
drh9cfcf5d2002-01-29 18:41:24 +0000475**
drhaaab5722002-02-19 13:39:21 +0000476** The calling routine must open a read/write cursor for pTab with
drh9cfcf5d2002-01-29 18:41:24 +0000477** cursor number "base". All indices of pTab must also have open
478** read/write cursors with cursor number base+i for the i-th cursor.
479** Except, if there is no possibility of a REPLACE action then
480** cursors do not need to be open for indices where aIdxUsed[i]==0.
481**
482** If the isUpdate flag is true, it means that the "base" cursor is
483** initially pointing to an entry that is being updated. The isUpdate
484** flag causes extra code to be generated so that the "base" cursor
485** is still pointing at the same entry after the routine returns.
486** Without the isUpdate flag, the "base" cursor might be moved.
487*/
488void sqliteGenerateConstraintChecks(
489 Parse *pParse, /* The parser context */
490 Table *pTab, /* the table into which we are inserting */
491 int base, /* Index of a read/write cursor pointing at pTab */
492 char *aIdxUsed, /* Which indices are used. NULL means all are used */
drh0ca3e242002-01-29 23:07:02 +0000493 int recnoChng, /* True if the record number will change */
drhb419a922002-01-30 16:17:23 +0000494 int isUpdate, /* True for UPDATE, False for INSERT */
drh9cfcf5d2002-01-29 18:41:24 +0000495 int overrideError, /* Override onError to this if not OE_Default */
drhb419a922002-01-30 16:17:23 +0000496 int ignoreDest /* Jump to this label on an OE_Ignore resolution */
drh9cfcf5d2002-01-29 18:41:24 +0000497){
498 int i;
499 Vdbe *v;
500 int nCol;
501 int onError;
502 int addr;
503 int extra;
drh0ca3e242002-01-29 23:07:02 +0000504 int iCur;
505 Index *pIdx;
506 int seenReplace = 0;
drhf5905aa2002-05-26 20:54:33 +0000507 int jumpInst1, jumpInst2;
drh0ca3e242002-01-29 23:07:02 +0000508 int contAddr;
drhb419a922002-01-30 16:17:23 +0000509 int hasTwoRecnos = (isUpdate && recnoChng);
drh9cfcf5d2002-01-29 18:41:24 +0000510
511 v = sqliteGetVdbe(pParse);
512 assert( v!=0 );
drh417be792002-03-03 18:59:40 +0000513 assert( pTab->pSelect==0 ); /* This table is not a VIEW */
drh9cfcf5d2002-01-29 18:41:24 +0000514 nCol = pTab->nCol;
515
516 /* Test all NOT NULL constraints.
517 */
518 for(i=0; i<nCol; i++){
drh0ca3e242002-01-29 23:07:02 +0000519 if( i==pTab->iPKey ){
520 /* Fix me: Make sure the INTEGER PRIMARY KEY is not NULL. */
521 continue;
522 }
drh9cfcf5d2002-01-29 18:41:24 +0000523 onError = pTab->aCol[i].notNull;
drh0ca3e242002-01-29 23:07:02 +0000524 if( onError==OE_None ) continue;
drh9cfcf5d2002-01-29 18:41:24 +0000525 if( overrideError!=OE_Default ){
526 onError = overrideError;
drh1c928532002-01-31 15:54:21 +0000527 }else if( onError==OE_Default ){
drh0d65dc02002-02-03 00:56:09 +0000528 onError = pParse->db->onError;
529 if( onError==OE_Default ) onError = OE_Abort;
drh9cfcf5d2002-01-29 18:41:24 +0000530 }
531 if( onError==OE_Replace && pTab->aCol[i].zDflt==0 ){
532 onError = OE_Abort;
533 }
drhef6764a2002-01-30 04:32:00 +0000534 sqliteVdbeAddOp(v, OP_Dup, nCol-1-i, 1);
drhf5905aa2002-05-26 20:54:33 +0000535 addr = sqliteVdbeAddOp(v, OP_NotNull, 1, 0);
drh9cfcf5d2002-01-29 18:41:24 +0000536 switch( onError ){
drh1c928532002-01-31 15:54:21 +0000537 case OE_Rollback:
538 case OE_Abort:
539 case OE_Fail: {
540 sqliteVdbeAddOp(v, OP_Halt, SQLITE_CONSTRAINT, onError);
drh9cfcf5d2002-01-29 18:41:24 +0000541 break;
542 }
543 case OE_Ignore: {
drhb419a922002-01-30 16:17:23 +0000544 sqliteVdbeAddOp(v, OP_Pop, nCol+1+hasTwoRecnos, 0);
drh0ca3e242002-01-29 23:07:02 +0000545 sqliteVdbeAddOp(v, OP_Goto, 0, ignoreDest);
drh9cfcf5d2002-01-29 18:41:24 +0000546 break;
547 }
548 case OE_Replace: {
549 sqliteVdbeAddOp(v, OP_String, 0, 0);
550 sqliteVdbeChangeP3(v, -1, pTab->aCol[i].zDflt, P3_STATIC);
551 sqliteVdbeAddOp(v, OP_Push, nCol-i, 0);
552 break;
553 }
drh0ca3e242002-01-29 23:07:02 +0000554 default: assert(0);
drh9cfcf5d2002-01-29 18:41:24 +0000555 }
drhef6764a2002-01-30 04:32:00 +0000556 sqliteVdbeChangeP2(v, addr, sqliteVdbeCurrentAddr(v));
drh9cfcf5d2002-01-29 18:41:24 +0000557 }
558
559 /* Test all CHECK constraints
560 */
drh0bd1f4e2002-06-06 18:54:39 +0000561 /**** TBD ****/
drh9cfcf5d2002-01-29 18:41:24 +0000562
drh0bd1f4e2002-06-06 18:54:39 +0000563 /* If we have an INTEGER PRIMARY KEY, make sure the primary key
564 ** of the new record does not previously exist. Except, if this
565 ** is an UPDATE and the primary key is not changing, that is OK.
566 ** Also, if the conflict resolution policy is REPLACE, then we
567 ** can skip this test.
drh9cfcf5d2002-01-29 18:41:24 +0000568 */
drh0d65dc02002-02-03 00:56:09 +0000569 if( (recnoChng || !isUpdate) && pTab->iPKey>=0 ){
drh0ca3e242002-01-29 23:07:02 +0000570 onError = pTab->keyConf;
571 if( overrideError!=OE_Default ){
572 onError = overrideError;
drh1c928532002-01-31 15:54:21 +0000573 }else if( onError==OE_Default ){
drh0d65dc02002-02-03 00:56:09 +0000574 onError = pParse->db->onError;
575 if( onError==OE_Default ) onError = OE_Abort;
drh0ca3e242002-01-29 23:07:02 +0000576 }
drh0d65dc02002-02-03 00:56:09 +0000577 if( onError!=OE_Replace ){
drh79b0c952002-05-21 12:56:43 +0000578 if( isUpdate ){
579 sqliteVdbeAddOp(v, OP_Dup, nCol+1, 1);
580 sqliteVdbeAddOp(v, OP_Dup, nCol+1, 1);
drhf5905aa2002-05-26 20:54:33 +0000581 jumpInst1 = sqliteVdbeAddOp(v, OP_Eq, 0, 0);
drh79b0c952002-05-21 12:56:43 +0000582 }
drh0d65dc02002-02-03 00:56:09 +0000583 sqliteVdbeAddOp(v, OP_Dup, nCol, 1);
drhf5905aa2002-05-26 20:54:33 +0000584 jumpInst2 = sqliteVdbeAddOp(v, OP_NotExists, base, 0);
drh0d65dc02002-02-03 00:56:09 +0000585 switch( onError ){
586 case OE_Rollback:
587 case OE_Abort:
588 case OE_Fail: {
589 sqliteVdbeAddOp(v, OP_Halt, SQLITE_CONSTRAINT, onError);
590 break;
591 }
592 case OE_Ignore: {
593 sqliteVdbeAddOp(v, OP_Pop, nCol+1+hasTwoRecnos, 0);
594 sqliteVdbeAddOp(v, OP_Goto, 0, ignoreDest);
595 break;
596 }
597 default: assert(0);
drh0ca3e242002-01-29 23:07:02 +0000598 }
drh0d65dc02002-02-03 00:56:09 +0000599 contAddr = sqliteVdbeCurrentAddr(v);
drhf5905aa2002-05-26 20:54:33 +0000600 sqliteVdbeChangeP2(v, jumpInst2, contAddr);
drh0d65dc02002-02-03 00:56:09 +0000601 if( isUpdate ){
drhf5905aa2002-05-26 20:54:33 +0000602 sqliteVdbeChangeP2(v, jumpInst1, contAddr);
drh0d65dc02002-02-03 00:56:09 +0000603 sqliteVdbeAddOp(v, OP_Dup, nCol+1, 1);
604 sqliteVdbeAddOp(v, OP_MoveTo, base, 0);
drh0ca3e242002-01-29 23:07:02 +0000605 }
drh0ca3e242002-01-29 23:07:02 +0000606 }
607 }
drh0bd1f4e2002-06-06 18:54:39 +0000608
609 /* Test all UNIQUE constraints by creating entries for each UNIQUE
610 ** index and making sure that duplicate entries do not already exist.
611 ** Add the new records to the indices as we go.
612 */
drh9cfcf5d2002-01-29 18:41:24 +0000613 extra = 0;
614 for(extra=(-1), iCur=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, iCur++){
drh9cfcf5d2002-01-29 18:41:24 +0000615 if( aIdxUsed && aIdxUsed[iCur]==0 ) continue;
616 extra++;
617 sqliteVdbeAddOp(v, OP_Dup, nCol+extra, 1);
618 for(i=0; i<pIdx->nColumn; i++){
619 int idx = pIdx->aiColumn[i];
620 if( idx==pTab->iPKey ){
drh0ca3e242002-01-29 23:07:02 +0000621 sqliteVdbeAddOp(v, OP_Dup, i+extra+nCol+1, 1);
drh9cfcf5d2002-01-29 18:41:24 +0000622 }else{
drh0ca3e242002-01-29 23:07:02 +0000623 sqliteVdbeAddOp(v, OP_Dup, i+extra+nCol-idx, 1);
drh9cfcf5d2002-01-29 18:41:24 +0000624 }
625 }
drhf5905aa2002-05-26 20:54:33 +0000626 jumpInst1 = sqliteVdbeAddOp(v, OP_MakeIdxKey, pIdx->nColumn, 0);
drh491791a2002-07-18 00:34:09 +0000627 if( pParse->db->file_format>=4 ) sqliteAddIdxKeyType(v, pIdx);
drh9cfcf5d2002-01-29 18:41:24 +0000628 onError = pIdx->onError;
629 if( onError==OE_None ) continue;
630 if( overrideError!=OE_Default ){
631 onError = overrideError;
drh1c928532002-01-31 15:54:21 +0000632 }else if( onError==OE_Default ){
drh0d65dc02002-02-03 00:56:09 +0000633 onError = pParse->db->onError;
634 if( onError==OE_Default ) onError = OE_Abort;
drh9cfcf5d2002-01-29 18:41:24 +0000635 }
drhb419a922002-01-30 16:17:23 +0000636 sqliteVdbeAddOp(v, OP_Dup, extra+nCol+1+hasTwoRecnos, 1);
drhf5905aa2002-05-26 20:54:33 +0000637 jumpInst2 = sqliteVdbeAddOp(v, OP_IsUnique, base+iCur+1, 0);
drh9cfcf5d2002-01-29 18:41:24 +0000638 switch( onError ){
drh1c928532002-01-31 15:54:21 +0000639 case OE_Rollback:
640 case OE_Abort:
641 case OE_Fail: {
642 sqliteVdbeAddOp(v, OP_Halt, SQLITE_CONSTRAINT, onError);
drh9cfcf5d2002-01-29 18:41:24 +0000643 break;
644 }
645 case OE_Ignore: {
drh0ca3e242002-01-29 23:07:02 +0000646 assert( seenReplace==0 );
drhfe1a1772002-04-09 03:15:06 +0000647 sqliteVdbeAddOp(v, OP_Pop, nCol+extra+3+hasTwoRecnos, 0);
drh9cfcf5d2002-01-29 18:41:24 +0000648 sqliteVdbeAddOp(v, OP_Goto, 0, ignoreDest);
drh9cfcf5d2002-01-29 18:41:24 +0000649 break;
650 }
651 case OE_Replace: {
drh38640e12002-07-05 21:42:36 +0000652 sqliteGenerateRowDelete(pParse->db, v, pTab, base, 0);
drh9cfcf5d2002-01-29 18:41:24 +0000653 if( isUpdate ){
drhb419a922002-01-30 16:17:23 +0000654 sqliteVdbeAddOp(v, OP_Dup, nCol+extra+1+hasTwoRecnos, 1);
drh0ca3e242002-01-29 23:07:02 +0000655 sqliteVdbeAddOp(v, OP_MoveTo, base, 0);
drh9cfcf5d2002-01-29 18:41:24 +0000656 }
drh0ca3e242002-01-29 23:07:02 +0000657 seenReplace = 1;
drh9cfcf5d2002-01-29 18:41:24 +0000658 break;
659 }
drh0ca3e242002-01-29 23:07:02 +0000660 default: assert(0);
drh9cfcf5d2002-01-29 18:41:24 +0000661 }
662 contAddr = sqliteVdbeCurrentAddr(v);
drh0bd1f4e2002-06-06 18:54:39 +0000663#if NULL_DISTINCT_FOR_UNIQUE
drhf5905aa2002-05-26 20:54:33 +0000664 sqliteVdbeChangeP2(v, jumpInst1, contAddr);
drh0bd1f4e2002-06-06 18:54:39 +0000665#endif
drhf5905aa2002-05-26 20:54:33 +0000666 sqliteVdbeChangeP2(v, jumpInst2, contAddr);
drh9cfcf5d2002-01-29 18:41:24 +0000667 }
668}
drh0ca3e242002-01-29 23:07:02 +0000669
670/*
671** This routine generates code to finish the INSERT or UPDATE operation
672** that was started by a prior call to sqliteGenerateConstraintChecks.
673** The stack must contain keys for all active indices followed by data
674** and the recno for the new entry. This routine creates the new
675** entries in all indices and in the main table.
676**
drhb419a922002-01-30 16:17:23 +0000677** The arguments to this routine should be the same as the first six
drh0ca3e242002-01-29 23:07:02 +0000678** arguments to sqliteGenerateConstraintChecks.
679*/
680void sqliteCompleteInsertion(
681 Parse *pParse, /* The parser context */
682 Table *pTab, /* the table into which we are inserting */
683 int base, /* Index of a read/write cursor pointing at pTab */
684 char *aIdxUsed, /* Which indices are used. NULL means all are used */
drhb419a922002-01-30 16:17:23 +0000685 int recnoChng, /* True if the record number will change */
686 int isUpdate /* True for UPDATE, False for INSERT */
drh0ca3e242002-01-29 23:07:02 +0000687){
688 int i;
689 Vdbe *v;
690 int nIdx;
691 Index *pIdx;
692
693 v = sqliteGetVdbe(pParse);
694 assert( v!=0 );
drh417be792002-03-03 18:59:40 +0000695 assert( pTab->pSelect==0 ); /* This table is not a VIEW */
drh0ca3e242002-01-29 23:07:02 +0000696 for(nIdx=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, nIdx++){}
697 for(i=nIdx-1; i>=0; i--){
698 if( aIdxUsed && aIdxUsed[i]==0 ) continue;
699 sqliteVdbeAddOp(v, OP_IdxPut, base+i+1, 0);
700 }
701 sqliteVdbeAddOp(v, OP_MakeRecord, pTab->nCol, 0);
danielk1977c3f9bad2002-05-15 08:30:12 +0000702 sqliteVdbeAddOp(v, OP_PutIntKey, base, pParse->trigStack?0:1);
drhb419a922002-01-30 16:17:23 +0000703 if( isUpdate && recnoChng ){
drh0ca3e242002-01-29 23:07:02 +0000704 sqliteVdbeAddOp(v, OP_Pop, 1, 0);
705 }
706}