blob: 06785ed33642129e603d21b9416ab3bdd68f8e49 [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**
danielk19773d68f032004-05-11 07:11:51 +000015** $Id: insert.c,v 1.97 2004/05/11 07:11:53 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.
drh142e30d2002-08-28 03:00:58 +000033**
34** The code generated follows one of three templates. For a simple
35** select with data coming from a VALUES clause, the code executes
36** once straight down through. The template looks like this:
37**
38** open write cursor to <table> and its indices
39** puts VALUES clause expressions onto the stack
40** write the resulting record into <table>
41** cleanup
42**
43** If the statement is of the form
44**
45** INSERT INTO <table> SELECT ...
46**
47** And the SELECT clause does not read from <table> at any time, then
48** the generated code follows this template:
49**
50** goto B
51** A: setup for the SELECT
52** loop over the tables in the SELECT
53** gosub C
54** end loop
55** cleanup after the SELECT
56** goto D
57** B: open write cursor to <table> and its indices
58** goto A
59** C: insert the select result into <table>
60** return
61** D: cleanup
62**
63** The third template is used if the insert statement takes its
64** values from a SELECT but the data is being inserted into a table
65** that is also read as part of the SELECT. In the third form,
66** we have to use a intermediate table to store the results of
67** the select. The template is like this:
68**
69** goto B
70** A: setup for the SELECT
71** loop over the tables in the SELECT
72** gosub C
73** end loop
74** cleanup after the SELECT
75** goto D
76** C: insert the select result into the intermediate table
77** return
78** B: open a cursor to an intermediate table
79** goto A
80** D: open write cursor to <table> and its indices
81** loop over the intermediate table
82** transfer values form intermediate table into <table>
83** end the loop
84** cleanup
drhcce7d172000-05-31 15:34:51 +000085*/
danielk19774adee202004-05-08 08:23:19 +000086void sqlite3Insert(
drhcce7d172000-05-31 15:34:51 +000087 Parse *pParse, /* Parser context */
drh113088e2003-03-20 01:16:58 +000088 SrcList *pTabList, /* Name of table into which we are inserting */
drhcce7d172000-05-31 15:34:51 +000089 ExprList *pList, /* List of values to be inserted */
drh5974a302000-06-07 14:42:26 +000090 Select *pSelect, /* A SELECT statement to use as the data source */
drh9cfcf5d2002-01-29 18:41:24 +000091 IdList *pColumn, /* Column names corresponding to IDLIST. */
92 int onError /* How to handle constraint errors */
drhcce7d172000-05-31 15:34:51 +000093){
drh5974a302000-06-07 14:42:26 +000094 Table *pTab; /* The table to insert into */
drh113088e2003-03-20 01:16:58 +000095 char *zTab; /* Name of the table into which we are inserting */
drhe22a3342003-04-22 20:30:37 +000096 const char *zDb; /* Name of the database holding this table */
drh5974a302000-06-07 14:42:26 +000097 int i, j, idx; /* Loop counters */
98 Vdbe *v; /* Generate code into this virtual machine */
99 Index *pIdx; /* For looping over indices of the table */
drh967e8b72000-06-21 13:59:10 +0000100 int nColumn; /* Number of columns in the data */
drh6a3ea0e2003-05-02 14:32:12 +0000101 int base; /* VDBE Cursor number for pTab */
drh5974a302000-06-07 14:42:26 +0000102 int iCont, iBreak; /* Beginning and end of the loop over srcTab */
drhecdc7532001-09-23 02:35:53 +0000103 sqlite *db; /* The main database structure */
drh4a324312001-12-21 14:30:42 +0000104 int keyColumn = -1; /* Column that is the INTEGER PRIMARY KEY */
drh0ca3e242002-01-29 23:07:02 +0000105 int endOfLoop; /* Label for the end of the insertion loop */
drh142e30d2002-08-28 03:00:58 +0000106 int useTempTable; /* Store SELECT results in intermediate table */
107 int srcTab; /* Data comes from this temporary cursor if >=0 */
108 int iSelectLoop; /* Address of code that implements the SELECT */
109 int iCleanup; /* Address of the cleanup code */
110 int iInsertBlock; /* Address of the subroutine used to insert data */
111 int iCntMem; /* Memory cell used for the row counter */
drh5cf590c2003-04-24 01:45:04 +0000112 int isView; /* True if attempting to insert into a view */
drhcce7d172000-05-31 15:34:51 +0000113
danielk1977c3f9bad2002-05-15 08:30:12 +0000114 int row_triggers_exist = 0; /* True if there are FOR EACH ROW triggers */
drh70ce3f02003-04-15 19:22:22 +0000115 int before_triggers; /* True if there are BEFORE triggers */
116 int after_triggers; /* True if there are AFTER triggers */
117 int newIdx = -1; /* Cursor for the NEW table */
danielk1977c3f9bad2002-05-15 08:30:12 +0000118
danielk197724b03fd2004-05-10 10:34:34 +0000119 if( pParse->nErr || sqlite3_malloc_failed ) goto insert_cleanup;
drhecdc7532001-09-23 02:35:53 +0000120 db = pParse->db;
drhdaffd0e2001-04-11 14:28:42 +0000121
drh1ccde152000-06-17 13:12:39 +0000122 /* Locate the table into which we will be inserting new information.
123 */
drh113088e2003-03-20 01:16:58 +0000124 assert( pTabList->nSrc==1 );
125 zTab = pTabList->a[0].zName;
drhdaffd0e2001-04-11 14:28:42 +0000126 if( zTab==0 ) goto insert_cleanup;
danielk19774adee202004-05-08 08:23:19 +0000127 pTab = sqlite3SrcListLookup(pParse, pTabList);
danielk1977c3f9bad2002-05-15 08:30:12 +0000128 if( pTab==0 ){
danielk1977c3f9bad2002-05-15 08:30:12 +0000129 goto insert_cleanup;
130 }
drhe22a3342003-04-22 20:30:37 +0000131 assert( pTab->iDb<db->nDb );
132 zDb = db->aDb[pTab->iDb].zName;
danielk19774adee202004-05-08 08:23:19 +0000133 if( sqlite3AuthCheck(pParse, SQLITE_INSERT, pTab->zName, 0, zDb) ){
drh1962bda2003-01-12 19:33:52 +0000134 goto insert_cleanup;
135 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000136
137 /* Ensure that:
138 * (a) the table is not read-only,
139 * (b) that if it is a view then ON INSERT triggers exist
140 */
danielk19774adee202004-05-08 08:23:19 +0000141 before_triggers = sqlite3TriggersExist(pParse, pTab->pTrigger, TK_INSERT,
drh70ce3f02003-04-15 19:22:22 +0000142 TK_BEFORE, TK_ROW, 0);
danielk19774adee202004-05-08 08:23:19 +0000143 after_triggers = sqlite3TriggersExist(pParse, pTab->pTrigger, TK_INSERT,
drh70ce3f02003-04-15 19:22:22 +0000144 TK_AFTER, TK_ROW, 0);
145 row_triggers_exist = before_triggers || after_triggers;
drh5cf590c2003-04-24 01:45:04 +0000146 isView = pTab->pSelect!=0;
danielk19774adee202004-05-08 08:23:19 +0000147 if( sqlite3IsReadOnly(pParse, pTab, before_triggers) ){
danielk1977c3f9bad2002-05-15 08:30:12 +0000148 goto insert_cleanup;
149 }
drha76b5df2002-02-23 02:32:10 +0000150 if( pTab==0 ) goto insert_cleanup;
drh1ccde152000-06-17 13:12:39 +0000151
drhf573c992002-07-31 00:32:50 +0000152 /* If pTab is really a view, make sure it has been initialized.
153 */
danielk19774adee202004-05-08 08:23:19 +0000154 if( isView && sqlite3ViewGetColumnNames(pParse, pTab) ){
drh5cf590c2003-04-24 01:45:04 +0000155 goto insert_cleanup;
drhf573c992002-07-31 00:32:50 +0000156 }
157
drh1ccde152000-06-17 13:12:39 +0000158 /* Allocate a VDBE
159 */
danielk19774adee202004-05-08 08:23:19 +0000160 v = sqlite3GetVdbe(pParse);
drh5974a302000-06-07 14:42:26 +0000161 if( v==0 ) goto insert_cleanup;
danielk19774adee202004-05-08 08:23:19 +0000162 sqlite3BeginWriteOperation(pParse, pSelect || row_triggers_exist, pTab->iDb);
drh1ccde152000-06-17 13:12:39 +0000163
danielk1977c3f9bad2002-05-15 08:30:12 +0000164 /* if there are row triggers, allocate a temp table for new.* references. */
danielk1977f29ce552002-05-19 23:43:12 +0000165 if( row_triggers_exist ){
danielk1977c3f9bad2002-05-15 08:30:12 +0000166 newIdx = pParse->nTab++;
danielk1977f29ce552002-05-19 23:43:12 +0000167 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000168
drh1ccde152000-06-17 13:12:39 +0000169 /* Figure out how many columns of data are supplied. If the data
drh142e30d2002-08-28 03:00:58 +0000170 ** is coming from a SELECT statement, then this step also generates
171 ** all the code to implement the SELECT statement and invoke a subroutine
172 ** to process each row of the result. (Template 2.) If the SELECT
173 ** statement uses the the table that is being inserted into, then the
174 ** subroutine is also coded here. That subroutine stores the SELECT
175 ** results in a temporary table. (Template 3.)
drh1ccde152000-06-17 13:12:39 +0000176 */
drh5974a302000-06-07 14:42:26 +0000177 if( pSelect ){
drh142e30d2002-08-28 03:00:58 +0000178 /* Data is coming from a SELECT. Generate code to implement that SELECT
179 */
180 int rc, iInitCode;
danielk19774adee202004-05-08 08:23:19 +0000181 iInitCode = sqlite3VdbeAddOp(v, OP_Goto, 0, 0);
182 iSelectLoop = sqlite3VdbeCurrentAddr(v);
183 iInsertBlock = sqlite3VdbeMakeLabel(v);
184 rc = sqlite3Select(pParse, pSelect, SRT_Subroutine, iInsertBlock, 0,0,0);
danielk197724b03fd2004-05-10 10:34:34 +0000185 if( rc || pParse->nErr || sqlite3_malloc_failed ) goto insert_cleanup;
danielk19774adee202004-05-08 08:23:19 +0000186 iCleanup = sqlite3VdbeMakeLabel(v);
187 sqlite3VdbeAddOp(v, OP_Goto, 0, iCleanup);
drh5974a302000-06-07 14:42:26 +0000188 assert( pSelect->pEList );
drh967e8b72000-06-21 13:59:10 +0000189 nColumn = pSelect->pEList->nExpr;
drh142e30d2002-08-28 03:00:58 +0000190
191 /* Set useTempTable to TRUE if the result of the SELECT statement
192 ** should be written into a temporary table. Set to FALSE if each
193 ** row of the SELECT can be written directly into the result table.
drh048c5302003-04-03 01:50:44 +0000194 **
195 ** A temp table must be used if the table being updated is also one
196 ** of the tables being read by the SELECT statement. Also use a
197 ** temp table in the case of row triggers.
drh142e30d2002-08-28 03:00:58 +0000198 */
drh048c5302003-04-03 01:50:44 +0000199 if( row_triggers_exist ){
200 useTempTable = 1;
201 }else{
danielk19774adee202004-05-08 08:23:19 +0000202 int addr = sqlite3VdbeFindOp(v, OP_OpenRead, pTab->tnum);
drh048c5302003-04-03 01:50:44 +0000203 useTempTable = 0;
204 if( addr>0 ){
danielk19774adee202004-05-08 08:23:19 +0000205 VdbeOp *pOp = sqlite3VdbeGetOp(v, addr-2);
drh048c5302003-04-03 01:50:44 +0000206 if( pOp->opcode==OP_Integer && pOp->p1==pTab->iDb ){
207 useTempTable = 1;
208 }
209 }
210 }
drh142e30d2002-08-28 03:00:58 +0000211
212 if( useTempTable ){
213 /* Generate the subroutine that SELECT calls to process each row of
214 ** the result. Store the result in a temporary table
215 */
216 srcTab = pParse->nTab++;
danielk19774adee202004-05-08 08:23:19 +0000217 sqlite3VdbeResolveLabel(v, iInsertBlock);
218 sqlite3VdbeAddOp(v, OP_MakeRecord, nColumn, 0);
219 sqlite3VdbeAddOp(v, OP_NewRecno, srcTab, 0);
220 sqlite3VdbeAddOp(v, OP_Pull, 1, 0);
221 sqlite3VdbeAddOp(v, OP_PutIntKey, srcTab, 0);
222 sqlite3VdbeAddOp(v, OP_Return, 0, 0);
drh142e30d2002-08-28 03:00:58 +0000223
224 /* The following code runs first because the GOTO at the very top
225 ** of the program jumps to it. Create the temporary table, then jump
226 ** back up and execute the SELECT code above.
227 */
danielk19774adee202004-05-08 08:23:19 +0000228 sqlite3VdbeChangeP2(v, iInitCode, sqlite3VdbeCurrentAddr(v));
229 sqlite3VdbeAddOp(v, OP_OpenTemp, srcTab, 0);
230 sqlite3VdbeAddOp(v, OP_Goto, 0, iSelectLoop);
231 sqlite3VdbeResolveLabel(v, iCleanup);
drh142e30d2002-08-28 03:00:58 +0000232 }else{
danielk19774adee202004-05-08 08:23:19 +0000233 sqlite3VdbeChangeP2(v, iInitCode, sqlite3VdbeCurrentAddr(v));
drh142e30d2002-08-28 03:00:58 +0000234 }
drh5974a302000-06-07 14:42:26 +0000235 }else{
drh142e30d2002-08-28 03:00:58 +0000236 /* This is the case if the data for the INSERT is coming from a VALUES
237 ** clause
238 */
drhad3cab52002-05-24 02:04:32 +0000239 SrcList dummy;
drhdaffd0e2001-04-11 14:28:42 +0000240 assert( pList!=0 );
drh5974a302000-06-07 14:42:26 +0000241 srcTab = -1;
drh142e30d2002-08-28 03:00:58 +0000242 useTempTable = 0;
drh5974a302000-06-07 14:42:26 +0000243 assert( pList );
drh967e8b72000-06-21 13:59:10 +0000244 nColumn = pList->nExpr;
drhad3cab52002-05-24 02:04:32 +0000245 dummy.nSrc = 0;
drhe64e7b22002-02-18 13:56:36 +0000246 for(i=0; i<nColumn; i++){
danielk19774adee202004-05-08 08:23:19 +0000247 if( sqlite3ExprResolveIds(pParse, &dummy, 0, pList->a[i].pExpr) ){
drhe64e7b22002-02-18 13:56:36 +0000248 goto insert_cleanup;
249 }
danielk19774adee202004-05-08 08:23:19 +0000250 if( sqlite3ExprCheck(pParse, pList->a[i].pExpr, 0, 0) ){
drhb04a5d82002-04-12 03:55:15 +0000251 goto insert_cleanup;
252 }
drhe64e7b22002-02-18 13:56:36 +0000253 }
drh5974a302000-06-07 14:42:26 +0000254 }
drh1ccde152000-06-17 13:12:39 +0000255
256 /* Make sure the number of columns in the source data matches the number
257 ** of columns to be inserted into the table.
258 */
drh967e8b72000-06-21 13:59:10 +0000259 if( pColumn==0 && nColumn!=pTab->nCol ){
danielk19774adee202004-05-08 08:23:19 +0000260 sqlite3ErrorMsg(pParse,
drhda93d232003-03-31 02:12:46 +0000261 "table %S has %d columns but %d values were supplied",
262 pTabList, 0, pTab->nCol, nColumn);
drhcce7d172000-05-31 15:34:51 +0000263 goto insert_cleanup;
264 }
drh967e8b72000-06-21 13:59:10 +0000265 if( pColumn!=0 && nColumn!=pColumn->nId ){
danielk19774adee202004-05-08 08:23:19 +0000266 sqlite3ErrorMsg(pParse, "%d values for %d columns", nColumn, pColumn->nId);
drhcce7d172000-05-31 15:34:51 +0000267 goto insert_cleanup;
268 }
drh1ccde152000-06-17 13:12:39 +0000269
270 /* If the INSERT statement included an IDLIST term, then make sure
271 ** all elements of the IDLIST really are columns of the table and
272 ** remember the column indices.
drhc8392582001-12-31 02:48:51 +0000273 **
274 ** If the table has an INTEGER PRIMARY KEY column and that column
275 ** is named in the IDLIST, then record in the keyColumn variable
276 ** the index into IDLIST of the primary key column. keyColumn is
277 ** the index of the primary key as it appears in IDLIST, not as
278 ** is appears in the original table. (The index of the primary
279 ** key in the original table is pTab->iPKey.)
drh1ccde152000-06-17 13:12:39 +0000280 */
drh967e8b72000-06-21 13:59:10 +0000281 if( pColumn ){
282 for(i=0; i<pColumn->nId; i++){
283 pColumn->a[i].idx = -1;
drhcce7d172000-05-31 15:34:51 +0000284 }
drh967e8b72000-06-21 13:59:10 +0000285 for(i=0; i<pColumn->nId; i++){
drhcce7d172000-05-31 15:34:51 +0000286 for(j=0; j<pTab->nCol; j++){
danielk19774adee202004-05-08 08:23:19 +0000287 if( sqlite3StrICmp(pColumn->a[i].zName, pTab->aCol[j].zName)==0 ){
drh967e8b72000-06-21 13:59:10 +0000288 pColumn->a[i].idx = j;
drh4a324312001-12-21 14:30:42 +0000289 if( j==pTab->iPKey ){
drh9aa028d2001-12-22 21:48:29 +0000290 keyColumn = i;
drh4a324312001-12-21 14:30:42 +0000291 }
drhcce7d172000-05-31 15:34:51 +0000292 break;
293 }
294 }
295 if( j>=pTab->nCol ){
danielk19774adee202004-05-08 08:23:19 +0000296 if( sqlite3IsRowid(pColumn->a[i].zName) ){
drha0217ba2003-06-01 01:10:33 +0000297 keyColumn = i;
298 }else{
danielk19774adee202004-05-08 08:23:19 +0000299 sqlite3ErrorMsg(pParse, "table %S has no column named %s",
drha0217ba2003-06-01 01:10:33 +0000300 pTabList, 0, pColumn->a[i].zName);
301 pParse->nErr++;
302 goto insert_cleanup;
303 }
drhcce7d172000-05-31 15:34:51 +0000304 }
305 }
306 }
drh1ccde152000-06-17 13:12:39 +0000307
drhaacc5432002-01-06 17:07:40 +0000308 /* If there is no IDLIST term but the table has an integer primary
drhc8392582001-12-31 02:48:51 +0000309 ** key, the set the keyColumn variable to the primary key column index
310 ** in the original table definition.
drh4a324312001-12-21 14:30:42 +0000311 */
312 if( pColumn==0 ){
313 keyColumn = pTab->iPKey;
314 }
315
drh142e30d2002-08-28 03:00:58 +0000316 /* Open the temp table for FOR EACH ROW triggers
317 */
danielk1977f29ce552002-05-19 23:43:12 +0000318 if( row_triggers_exist ){
danielk19774adee202004-05-08 08:23:19 +0000319 sqlite3VdbeAddOp(v, OP_OpenPseudo, newIdx, 0);
danielk1977f29ce552002-05-19 23:43:12 +0000320 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000321
drhfeeb1392002-04-09 03:28:01 +0000322 /* Initialize the count of rows to be inserted
323 */
drh142e30d2002-08-28 03:00:58 +0000324 if( db->flags & SQLITE_CountRows ){
325 iCntMem = pParse->nMem++;
danielk19774adee202004-05-08 08:23:19 +0000326 sqlite3VdbeAddOp(v, OP_Integer, 0, 0);
327 sqlite3VdbeAddOp(v, OP_MemStore, iCntMem, 1);
drhfeeb1392002-04-09 03:28:01 +0000328 }
329
danielk1977c3f9bad2002-05-15 08:30:12 +0000330 /* Open tables and indices if there are no row triggers */
danielk1977f29ce552002-05-19 23:43:12 +0000331 if( !row_triggers_exist ){
danielk1977c3f9bad2002-05-15 08:30:12 +0000332 base = pParse->nTab;
danielk19774adee202004-05-08 08:23:19 +0000333 idx = sqlite3OpenTableAndIndices(pParse, pTab, base);
danielk1977c3f9bad2002-05-15 08:30:12 +0000334 pParse->nTab += idx;
335 }
336
drh142e30d2002-08-28 03:00:58 +0000337 /* If the data source is a temporary table, then we have to create
drh1ccde152000-06-17 13:12:39 +0000338 ** a loop because there might be multiple rows of data. If the data
drh142e30d2002-08-28 03:00:58 +0000339 ** source is a subroutine call from the SELECT statement, then we need
340 ** to launch the SELECT statement processing.
drh1ccde152000-06-17 13:12:39 +0000341 */
drh142e30d2002-08-28 03:00:58 +0000342 if( useTempTable ){
danielk19774adee202004-05-08 08:23:19 +0000343 iBreak = sqlite3VdbeMakeLabel(v);
344 sqlite3VdbeAddOp(v, OP_Rewind, srcTab, iBreak);
345 iCont = sqlite3VdbeCurrentAddr(v);
drh142e30d2002-08-28 03:00:58 +0000346 }else if( pSelect ){
danielk19774adee202004-05-08 08:23:19 +0000347 sqlite3VdbeAddOp(v, OP_Goto, 0, iSelectLoop);
348 sqlite3VdbeResolveLabel(v, iInsertBlock);
drh5974a302000-06-07 14:42:26 +0000349 }
drh1ccde152000-06-17 13:12:39 +0000350
drh5cf590c2003-04-24 01:45:04 +0000351 /* Run the BEFORE and INSTEAD OF triggers, if there are any
drh70ce3f02003-04-15 19:22:22 +0000352 */
danielk19774adee202004-05-08 08:23:19 +0000353 endOfLoop = sqlite3VdbeMakeLabel(v);
drh70ce3f02003-04-15 19:22:22 +0000354 if( before_triggers ){
danielk1977c3f9bad2002-05-15 08:30:12 +0000355
drh70ce3f02003-04-15 19:22:22 +0000356 /* build the NEW.* reference row. Note that if there is an INTEGER
357 ** PRIMARY KEY into which a NULL is being inserted, that NULL will be
358 ** translated into a unique ID for the row. But on a BEFORE trigger,
359 ** we do not know what the unique ID will be (because the insert has
360 ** not happened yet) so we substitute a rowid of -1
361 */
362 if( keyColumn<0 ){
danielk19774adee202004-05-08 08:23:19 +0000363 sqlite3VdbeAddOp(v, OP_Integer, -1, 0);
drh70ce3f02003-04-15 19:22:22 +0000364 }else if( useTempTable ){
danielk19774adee202004-05-08 08:23:19 +0000365 sqlite3VdbeAddOp(v, OP_Column, srcTab, keyColumn);
drh70ce3f02003-04-15 19:22:22 +0000366 }else if( pSelect ){
danielk19774adee202004-05-08 08:23:19 +0000367 sqlite3VdbeAddOp(v, OP_Dup, nColumn - keyColumn - 1, 1);
drh70ce3f02003-04-15 19:22:22 +0000368 }else{
danielk19774adee202004-05-08 08:23:19 +0000369 sqlite3ExprCode(pParse, pList->a[keyColumn].pExpr);
370 sqlite3VdbeAddOp(v, OP_NotNull, -1, sqlite3VdbeCurrentAddr(v)+3);
371 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
372 sqlite3VdbeAddOp(v, OP_Integer, -1, 0);
373 sqlite3VdbeAddOp(v, OP_MustBeInt, 0, 0);
drh70ce3f02003-04-15 19:22:22 +0000374 }
375
376 /* Create the new column data
377 */
danielk1977c3f9bad2002-05-15 08:30:12 +0000378 for(i=0; i<pTab->nCol; i++){
379 if( pColumn==0 ){
drh9adf9ac2002-05-15 11:44:13 +0000380 j = i;
danielk1977c3f9bad2002-05-15 08:30:12 +0000381 }else{
drh9adf9ac2002-05-15 11:44:13 +0000382 for(j=0; j<pColumn->nId; j++){
383 if( pColumn->a[j].idx==i ) break;
384 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000385 }
386 if( pColumn && j>=pColumn->nId ){
danielk19774adee202004-05-08 08:23:19 +0000387 sqlite3VdbeOp3(v, OP_String, 0, 0, pTab->aCol[i].zDflt, P3_STATIC);
drh142e30d2002-08-28 03:00:58 +0000388 }else if( useTempTable ){
danielk19774adee202004-05-08 08:23:19 +0000389 sqlite3VdbeAddOp(v, OP_Column, srcTab, j);
drh142e30d2002-08-28 03:00:58 +0000390 }else if( pSelect ){
danielk19774adee202004-05-08 08:23:19 +0000391 sqlite3VdbeAddOp(v, OP_Dup, nColumn-j-1, 1);
danielk1977c3f9bad2002-05-15 08:30:12 +0000392 }else{
danielk19774adee202004-05-08 08:23:19 +0000393 sqlite3ExprCode(pParse, pList->a[j].pExpr);
danielk1977c3f9bad2002-05-15 08:30:12 +0000394 }
395 }
danielk19774adee202004-05-08 08:23:19 +0000396 sqlite3VdbeAddOp(v, OP_MakeRecord, pTab->nCol, 0);
397 sqlite3VdbeAddOp(v, OP_PutIntKey, newIdx, 0);
danielk1977c3f9bad2002-05-15 08:30:12 +0000398
drh5cf590c2003-04-24 01:45:04 +0000399 /* Fire BEFORE or INSTEAD OF triggers */
danielk19774adee202004-05-08 08:23:19 +0000400 if( sqlite3CodeRowTrigger(pParse, TK_INSERT, 0, TK_BEFORE, pTab,
drhb2fe7d82003-04-20 17:29:23 +0000401 newIdx, -1, onError, endOfLoop) ){
danielk1977f29ce552002-05-19 23:43:12 +0000402 goto insert_cleanup;
403 }
drh70ce3f02003-04-15 19:22:22 +0000404 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000405
drh70ce3f02003-04-15 19:22:22 +0000406 /* If any triggers exists, the opening of tables and indices is deferred
407 ** until now.
408 */
drh5cf590c2003-04-24 01:45:04 +0000409 if( row_triggers_exist && !isView ){
410 base = pParse->nTab;
danielk19774adee202004-05-08 08:23:19 +0000411 idx = sqlite3OpenTableAndIndices(pParse, pTab, base);
drh5cf590c2003-04-24 01:45:04 +0000412 pParse->nTab += idx;
danielk1977c3f9bad2002-05-15 08:30:12 +0000413 }
414
drh4a324312001-12-21 14:30:42 +0000415 /* Push the record number for the new entry onto the stack. The
416 ** record number is a randomly generate integer created by NewRecno
417 ** except when the table has an INTEGER PRIMARY KEY column, in which
drhb419a922002-01-30 16:17:23 +0000418 ** case the record number is the same as that column.
drh1ccde152000-06-17 13:12:39 +0000419 */
drh5cf590c2003-04-24 01:45:04 +0000420 if( !isView ){
danielk1977c3f9bad2002-05-15 08:30:12 +0000421 if( keyColumn>=0 ){
drh142e30d2002-08-28 03:00:58 +0000422 if( useTempTable ){
danielk19774adee202004-05-08 08:23:19 +0000423 sqlite3VdbeAddOp(v, OP_Column, srcTab, keyColumn);
drh142e30d2002-08-28 03:00:58 +0000424 }else if( pSelect ){
danielk19774adee202004-05-08 08:23:19 +0000425 sqlite3VdbeAddOp(v, OP_Dup, nColumn - keyColumn - 1, 1);
danielk1977c3f9bad2002-05-15 08:30:12 +0000426 }else{
danielk19774adee202004-05-08 08:23:19 +0000427 sqlite3ExprCode(pParse, pList->a[keyColumn].pExpr);
danielk1977c3f9bad2002-05-15 08:30:12 +0000428 }
drh27a32782002-06-19 20:32:43 +0000429 /* If the PRIMARY KEY expression is NULL, then use OP_NewRecno
430 ** to generate a unique primary key value.
431 */
danielk19774adee202004-05-08 08:23:19 +0000432 sqlite3VdbeAddOp(v, OP_NotNull, -1, sqlite3VdbeCurrentAddr(v)+3);
433 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
434 sqlite3VdbeAddOp(v, OP_NewRecno, base, 0);
435 sqlite3VdbeAddOp(v, OP_MustBeInt, 0, 0);
danielk1977c3f9bad2002-05-15 08:30:12 +0000436 }else{
danielk19774adee202004-05-08 08:23:19 +0000437 sqlite3VdbeAddOp(v, OP_NewRecno, base, 0);
drh4a324312001-12-21 14:30:42 +0000438 }
drh4a324312001-12-21 14:30:42 +0000439
danielk1977c3f9bad2002-05-15 08:30:12 +0000440 /* Push onto the stack, data for all columns of the new entry, beginning
danielk1977f29ce552002-05-19 23:43:12 +0000441 ** with the first column.
442 */
danielk1977c3f9bad2002-05-15 08:30:12 +0000443 for(i=0; i<pTab->nCol; i++){
444 if( i==pTab->iPKey ){
drh9adf9ac2002-05-15 11:44:13 +0000445 /* The value of the INTEGER PRIMARY KEY column is always a NULL.
danielk1977f29ce552002-05-19 23:43:12 +0000446 ** Whenever this column is read, the record number will be substituted
447 ** in its place. So will fill this column with a NULL to avoid
448 ** taking up data space with information that will never be used. */
danielk19774adee202004-05-08 08:23:19 +0000449 sqlite3VdbeAddOp(v, OP_String, 0, 0);
drh9adf9ac2002-05-15 11:44:13 +0000450 continue;
danielk1977c3f9bad2002-05-15 08:30:12 +0000451 }
452 if( pColumn==0 ){
drh9adf9ac2002-05-15 11:44:13 +0000453 j = i;
danielk1977c3f9bad2002-05-15 08:30:12 +0000454 }else{
drh9adf9ac2002-05-15 11:44:13 +0000455 for(j=0; j<pColumn->nId; j++){
456 if( pColumn->a[j].idx==i ) break;
457 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000458 }
459 if( pColumn && j>=pColumn->nId ){
danielk19774adee202004-05-08 08:23:19 +0000460 sqlite3VdbeOp3(v, OP_String, 0, 0, pTab->aCol[i].zDflt, P3_STATIC);
drh142e30d2002-08-28 03:00:58 +0000461 }else if( useTempTable ){
danielk19774adee202004-05-08 08:23:19 +0000462 sqlite3VdbeAddOp(v, OP_Column, srcTab, j);
drh142e30d2002-08-28 03:00:58 +0000463 }else if( pSelect ){
danielk19774adee202004-05-08 08:23:19 +0000464 sqlite3VdbeAddOp(v, OP_Dup, i+nColumn-j, 1);
danielk1977c3f9bad2002-05-15 08:30:12 +0000465 }else{
danielk19774adee202004-05-08 08:23:19 +0000466 sqlite3ExprCode(pParse, pList->a[j].pExpr);
drh5974a302000-06-07 14:42:26 +0000467 }
drhbed86902000-06-02 13:27:59 +0000468 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000469
470 /* Generate code to check constraints and generate index keys and
danielk1977f29ce552002-05-19 23:43:12 +0000471 ** do the insertion.
472 */
danielk19774adee202004-05-08 08:23:19 +0000473 sqlite3GenerateConstraintChecks(pParse, pTab, base, 0, keyColumn>=0,
drha0217ba2003-06-01 01:10:33 +0000474 0, onError, endOfLoop);
danielk19774adee202004-05-08 08:23:19 +0000475 sqlite3CompleteInsertion(pParse, pTab, base, 0,0,0,
drh70ce3f02003-04-15 19:22:22 +0000476 after_triggers ? newIdx : -1);
drh5cf590c2003-04-24 01:45:04 +0000477 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000478
drh5cf590c2003-04-24 01:45:04 +0000479 /* Update the count of rows that are inserted
480 */
481 if( (db->flags & SQLITE_CountRows)!=0 ){
danielk19774adee202004-05-08 08:23:19 +0000482 sqlite3VdbeAddOp(v, OP_MemIncr, iCntMem, 0);
drh5974a302000-06-07 14:42:26 +0000483 }
drh1ccde152000-06-17 13:12:39 +0000484
danielk1977f29ce552002-05-19 23:43:12 +0000485 if( row_triggers_exist ){
danielk1977c3f9bad2002-05-15 08:30:12 +0000486 /* Close all tables opened */
drh5cf590c2003-04-24 01:45:04 +0000487 if( !isView ){
danielk19774adee202004-05-08 08:23:19 +0000488 sqlite3VdbeAddOp(v, OP_Close, base, 0);
danielk1977c3f9bad2002-05-15 08:30:12 +0000489 for(idx=1, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, idx++){
danielk19774adee202004-05-08 08:23:19 +0000490 sqlite3VdbeAddOp(v, OP_Close, idx+base, 0);
danielk1977c3f9bad2002-05-15 08:30:12 +0000491 }
492 }
drh1bee3d72001-10-15 00:44:35 +0000493
danielk1977c3f9bad2002-05-15 08:30:12 +0000494 /* Code AFTER triggers */
danielk19774adee202004-05-08 08:23:19 +0000495 if( sqlite3CodeRowTrigger(pParse, TK_INSERT, 0, TK_AFTER, pTab, newIdx, -1,
danielk19776f349032002-06-11 02:25:40 +0000496 onError, endOfLoop) ){
danielk1977f29ce552002-05-19 23:43:12 +0000497 goto insert_cleanup;
498 }
drh1bee3d72001-10-15 00:44:35 +0000499 }
500
drh1ccde152000-06-17 13:12:39 +0000501 /* The bottom of the loop, if the data source is a SELECT statement
danielk1977f29ce552002-05-19 23:43:12 +0000502 */
danielk19774adee202004-05-08 08:23:19 +0000503 sqlite3VdbeResolveLabel(v, endOfLoop);
drh142e30d2002-08-28 03:00:58 +0000504 if( useTempTable ){
danielk19774adee202004-05-08 08:23:19 +0000505 sqlite3VdbeAddOp(v, OP_Next, srcTab, iCont);
506 sqlite3VdbeResolveLabel(v, iBreak);
507 sqlite3VdbeAddOp(v, OP_Close, srcTab, 0);
drh142e30d2002-08-28 03:00:58 +0000508 }else if( pSelect ){
danielk19774adee202004-05-08 08:23:19 +0000509 sqlite3VdbeAddOp(v, OP_Pop, nColumn, 0);
510 sqlite3VdbeAddOp(v, OP_Return, 0, 0);
511 sqlite3VdbeResolveLabel(v, iCleanup);
drh6b563442001-11-07 16:48:26 +0000512 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000513
danielk1977f29ce552002-05-19 23:43:12 +0000514 if( !row_triggers_exist ){
danielk1977c3f9bad2002-05-15 08:30:12 +0000515 /* Close all tables opened */
danielk19774adee202004-05-08 08:23:19 +0000516 sqlite3VdbeAddOp(v, OP_Close, base, 0);
danielk1977c3f9bad2002-05-15 08:30:12 +0000517 for(idx=1, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, idx++){
danielk19774adee202004-05-08 08:23:19 +0000518 sqlite3VdbeAddOp(v, OP_Close, idx+base, 0);
danielk1977c3f9bad2002-05-15 08:30:12 +0000519 }
drhcce7d172000-05-31 15:34:51 +0000520 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000521
danielk19774adee202004-05-08 08:23:19 +0000522 sqlite3VdbeAddOp(v, OP_SetCounts, 0, 0);
523 sqlite3EndWriteOperation(pParse);
drh5e00f6c2001-09-13 13:46:56 +0000524
drh1bee3d72001-10-15 00:44:35 +0000525 /*
danielk1977f29ce552002-05-19 23:43:12 +0000526 ** Return the number of rows inserted.
drh1bee3d72001-10-15 00:44:35 +0000527 */
drh142e30d2002-08-28 03:00:58 +0000528 if( db->flags & SQLITE_CountRows ){
danielk19774adee202004-05-08 08:23:19 +0000529 sqlite3VdbeOp3(v, OP_ColumnName, 0, 1, "rows inserted", P3_STATIC);
530 sqlite3VdbeAddOp(v, OP_MemLoad, iCntMem, 0);
531 sqlite3VdbeAddOp(v, OP_Callback, 1, 0);
drh1bee3d72001-10-15 00:44:35 +0000532 }
drhcce7d172000-05-31 15:34:51 +0000533
534insert_cleanup:
danielk19774adee202004-05-08 08:23:19 +0000535 sqlite3SrcListDelete(pTabList);
536 if( pList ) sqlite3ExprListDelete(pList);
537 if( pSelect ) sqlite3SelectDelete(pSelect);
538 sqlite3IdListDelete(pColumn);
drhcce7d172000-05-31 15:34:51 +0000539}
drh9cfcf5d2002-01-29 18:41:24 +0000540
drh9cfcf5d2002-01-29 18:41:24 +0000541/*
542** Generate code to do a constraint check prior to an INSERT or an UPDATE.
543**
544** When this routine is called, the stack contains (from bottom to top)
drh0ca3e242002-01-29 23:07:02 +0000545** the following values:
546**
drhb2fe7d82003-04-20 17:29:23 +0000547** 1. The recno of the row to be updated before the update. This
drhb419a922002-01-30 16:17:23 +0000548** value is omitted unless we are doing an UPDATE that involves a
549** change to the record number.
drh0ca3e242002-01-29 23:07:02 +0000550**
drhb419a922002-01-30 16:17:23 +0000551** 2. The recno of the row after the update.
drh0ca3e242002-01-29 23:07:02 +0000552**
553** 3. The data in the first column of the entry after the update.
554**
555** i. Data from middle columns...
556**
557** N. The data in the last column of the entry after the update.
558**
drhb419a922002-01-30 16:17:23 +0000559** The old recno shown as entry (1) above is omitted unless both isUpdate
drh1c928532002-01-31 15:54:21 +0000560** and recnoChng are 1. isUpdate is true for UPDATEs and false for
561** INSERTs and recnoChng is true if the record number is being changed.
drh0ca3e242002-01-29 23:07:02 +0000562**
563** The code generated by this routine pushes additional entries onto
564** the stack which are the keys for new index entries for the new record.
565** The order of index keys is the same as the order of the indices on
566** the pTable->pIndex list. A key is only created for index i if
567** aIdxUsed!=0 and aIdxUsed[i]!=0.
drh9cfcf5d2002-01-29 18:41:24 +0000568**
569** This routine also generates code to check constraints. NOT NULL,
570** CHECK, and UNIQUE constraints are all checked. If a constraint fails,
drh1c928532002-01-31 15:54:21 +0000571** then the appropriate action is performed. There are five possible
572** actions: ROLLBACK, ABORT, FAIL, REPLACE, and IGNORE.
drh9cfcf5d2002-01-29 18:41:24 +0000573**
574** Constraint type Action What Happens
575** --------------- ---------- ----------------------------------------
drh1c928532002-01-31 15:54:21 +0000576** any ROLLBACK The current transaction is rolled back and
danielk197724b03fd2004-05-10 10:34:34 +0000577** sqlite3_exec() returns immediately with a
drh9cfcf5d2002-01-29 18:41:24 +0000578** return code of SQLITE_CONSTRAINT.
579**
drh1c928532002-01-31 15:54:21 +0000580** any ABORT Back out changes from the current command
581** only (do not do a complete rollback) then
danielk197724b03fd2004-05-10 10:34:34 +0000582** cause sqlite3_exec() to return immediately
drh1c928532002-01-31 15:54:21 +0000583** with SQLITE_CONSTRAINT.
584**
585** any FAIL Sqlite_exec() returns immediately with a
586** return code of SQLITE_CONSTRAINT. The
587** transaction is not rolled back and any
588** prior changes are retained.
589**
drh9cfcf5d2002-01-29 18:41:24 +0000590** any IGNORE The record number and data is popped from
591** the stack and there is an immediate jump
592** to label ignoreDest.
593**
594** NOT NULL REPLACE The NULL value is replace by the default
595** value for that column. If the default value
596** is NULL, the action is the same as ABORT.
597**
598** UNIQUE REPLACE The other row that conflicts with the row
599** being inserted is removed.
600**
601** CHECK REPLACE Illegal. The results in an exception.
602**
drh1c928532002-01-31 15:54:21 +0000603** Which action to take is determined by the overrideError parameter.
604** Or if overrideError==OE_Default, then the pParse->onError parameter
605** is used. Or if pParse->onError==OE_Default then the onError value
606** for the constraint is used.
drh9cfcf5d2002-01-29 18:41:24 +0000607**
drhaaab5722002-02-19 13:39:21 +0000608** The calling routine must open a read/write cursor for pTab with
drh9cfcf5d2002-01-29 18:41:24 +0000609** cursor number "base". All indices of pTab must also have open
610** read/write cursors with cursor number base+i for the i-th cursor.
611** Except, if there is no possibility of a REPLACE action then
612** cursors do not need to be open for indices where aIdxUsed[i]==0.
613**
614** If the isUpdate flag is true, it means that the "base" cursor is
615** initially pointing to an entry that is being updated. The isUpdate
616** flag causes extra code to be generated so that the "base" cursor
617** is still pointing at the same entry after the routine returns.
618** Without the isUpdate flag, the "base" cursor might be moved.
619*/
danielk19774adee202004-05-08 08:23:19 +0000620void sqlite3GenerateConstraintChecks(
drh9cfcf5d2002-01-29 18:41:24 +0000621 Parse *pParse, /* The parser context */
622 Table *pTab, /* the table into which we are inserting */
623 int base, /* Index of a read/write cursor pointing at pTab */
624 char *aIdxUsed, /* Which indices are used. NULL means all are used */
drh0ca3e242002-01-29 23:07:02 +0000625 int recnoChng, /* True if the record number will change */
drhb419a922002-01-30 16:17:23 +0000626 int isUpdate, /* True for UPDATE, False for INSERT */
drh9cfcf5d2002-01-29 18:41:24 +0000627 int overrideError, /* Override onError to this if not OE_Default */
drhb419a922002-01-30 16:17:23 +0000628 int ignoreDest /* Jump to this label on an OE_Ignore resolution */
drh9cfcf5d2002-01-29 18:41:24 +0000629){
630 int i;
631 Vdbe *v;
632 int nCol;
633 int onError;
634 int addr;
635 int extra;
drh0ca3e242002-01-29 23:07:02 +0000636 int iCur;
637 Index *pIdx;
638 int seenReplace = 0;
drhf5905aa2002-05-26 20:54:33 +0000639 int jumpInst1, jumpInst2;
drh0ca3e242002-01-29 23:07:02 +0000640 int contAddr;
drhb419a922002-01-30 16:17:23 +0000641 int hasTwoRecnos = (isUpdate && recnoChng);
drh9cfcf5d2002-01-29 18:41:24 +0000642
danielk19774adee202004-05-08 08:23:19 +0000643 v = sqlite3GetVdbe(pParse);
drh9cfcf5d2002-01-29 18:41:24 +0000644 assert( v!=0 );
drh417be792002-03-03 18:59:40 +0000645 assert( pTab->pSelect==0 ); /* This table is not a VIEW */
drh9cfcf5d2002-01-29 18:41:24 +0000646 nCol = pTab->nCol;
647
648 /* Test all NOT NULL constraints.
649 */
650 for(i=0; i<nCol; i++){
drh0ca3e242002-01-29 23:07:02 +0000651 if( i==pTab->iPKey ){
drh0ca3e242002-01-29 23:07:02 +0000652 continue;
653 }
drh9cfcf5d2002-01-29 18:41:24 +0000654 onError = pTab->aCol[i].notNull;
drh0ca3e242002-01-29 23:07:02 +0000655 if( onError==OE_None ) continue;
drh9cfcf5d2002-01-29 18:41:24 +0000656 if( overrideError!=OE_Default ){
657 onError = overrideError;
drha996e472003-05-16 02:30:27 +0000658 }else if( pParse->db->onError!=OE_Default ){
drh0d65dc02002-02-03 00:56:09 +0000659 onError = pParse->db->onError;
drha996e472003-05-16 02:30:27 +0000660 }else if( onError==OE_Default ){
661 onError = OE_Abort;
drh9cfcf5d2002-01-29 18:41:24 +0000662 }
663 if( onError==OE_Replace && pTab->aCol[i].zDflt==0 ){
664 onError = OE_Abort;
665 }
danielk19774adee202004-05-08 08:23:19 +0000666 sqlite3VdbeAddOp(v, OP_Dup, nCol-1-i, 1);
667 addr = sqlite3VdbeAddOp(v, OP_NotNull, 1, 0);
drh9cfcf5d2002-01-29 18:41:24 +0000668 switch( onError ){
drh1c928532002-01-31 15:54:21 +0000669 case OE_Rollback:
670 case OE_Abort:
671 case OE_Fail: {
drh483750b2003-01-29 18:46:51 +0000672 char *zMsg = 0;
danielk19774adee202004-05-08 08:23:19 +0000673 sqlite3VdbeAddOp(v, OP_Halt, SQLITE_CONSTRAINT, onError);
674 sqlite3SetString(&zMsg, pTab->zName, ".", pTab->aCol[i].zName,
drh41743982003-12-06 21:43:55 +0000675 " may not be NULL", (char*)0);
danielk19774adee202004-05-08 08:23:19 +0000676 sqlite3VdbeChangeP3(v, -1, zMsg, P3_DYNAMIC);
drh9cfcf5d2002-01-29 18:41:24 +0000677 break;
678 }
679 case OE_Ignore: {
danielk19774adee202004-05-08 08:23:19 +0000680 sqlite3VdbeAddOp(v, OP_Pop, nCol+1+hasTwoRecnos, 0);
681 sqlite3VdbeAddOp(v, OP_Goto, 0, ignoreDest);
drh9cfcf5d2002-01-29 18:41:24 +0000682 break;
683 }
684 case OE_Replace: {
danielk19774adee202004-05-08 08:23:19 +0000685 sqlite3VdbeOp3(v, OP_String, 0, 0, pTab->aCol[i].zDflt, P3_STATIC);
686 sqlite3VdbeAddOp(v, OP_Push, nCol-i, 0);
drh9cfcf5d2002-01-29 18:41:24 +0000687 break;
688 }
drh0ca3e242002-01-29 23:07:02 +0000689 default: assert(0);
drh9cfcf5d2002-01-29 18:41:24 +0000690 }
danielk19774adee202004-05-08 08:23:19 +0000691 sqlite3VdbeChangeP2(v, addr, sqlite3VdbeCurrentAddr(v));
drh9cfcf5d2002-01-29 18:41:24 +0000692 }
693
694 /* Test all CHECK constraints
695 */
drh0bd1f4e2002-06-06 18:54:39 +0000696 /**** TBD ****/
drh9cfcf5d2002-01-29 18:41:24 +0000697
drh0bd1f4e2002-06-06 18:54:39 +0000698 /* If we have an INTEGER PRIMARY KEY, make sure the primary key
699 ** of the new record does not previously exist. Except, if this
700 ** is an UPDATE and the primary key is not changing, that is OK.
drh9cfcf5d2002-01-29 18:41:24 +0000701 */
drh5383ae52003-06-04 12:23:30 +0000702 if( recnoChng ){
drh0ca3e242002-01-29 23:07:02 +0000703 onError = pTab->keyConf;
704 if( overrideError!=OE_Default ){
705 onError = overrideError;
drha996e472003-05-16 02:30:27 +0000706 }else if( pParse->db->onError!=OE_Default ){
drh0d65dc02002-02-03 00:56:09 +0000707 onError = pParse->db->onError;
drha996e472003-05-16 02:30:27 +0000708 }else if( onError==OE_Default ){
709 onError = OE_Abort;
drh0ca3e242002-01-29 23:07:02 +0000710 }
drha0217ba2003-06-01 01:10:33 +0000711
drh5383ae52003-06-04 12:23:30 +0000712 if( isUpdate ){
danielk19774adee202004-05-08 08:23:19 +0000713 sqlite3VdbeAddOp(v, OP_Dup, nCol+1, 1);
714 sqlite3VdbeAddOp(v, OP_Dup, nCol+1, 1);
715 jumpInst1 = sqlite3VdbeAddOp(v, OP_Eq, 0, 0);
drh5383ae52003-06-04 12:23:30 +0000716 }
danielk19774adee202004-05-08 08:23:19 +0000717 sqlite3VdbeAddOp(v, OP_Dup, nCol, 1);
718 jumpInst2 = sqlite3VdbeAddOp(v, OP_NotExists, base, 0);
drh5383ae52003-06-04 12:23:30 +0000719 switch( onError ){
720 default: {
721 onError = OE_Abort;
722 /* Fall thru into the next case */
drh79b0c952002-05-21 12:56:43 +0000723 }
drh5383ae52003-06-04 12:23:30 +0000724 case OE_Rollback:
725 case OE_Abort:
726 case OE_Fail: {
danielk19774adee202004-05-08 08:23:19 +0000727 sqlite3VdbeOp3(v, OP_Halt, SQLITE_CONSTRAINT, onError,
drh701a0ae2004-02-22 20:05:00 +0000728 "PRIMARY KEY must be unique", P3_STATIC);
drh5383ae52003-06-04 12:23:30 +0000729 break;
drh0ca3e242002-01-29 23:07:02 +0000730 }
drh5383ae52003-06-04 12:23:30 +0000731 case OE_Replace: {
danielk19774adee202004-05-08 08:23:19 +0000732 sqlite3GenerateRowIndexDelete(pParse->db, v, pTab, base, 0);
drh5383ae52003-06-04 12:23:30 +0000733 if( isUpdate ){
danielk19774adee202004-05-08 08:23:19 +0000734 sqlite3VdbeAddOp(v, OP_Dup, nCol+hasTwoRecnos, 1);
735 sqlite3VdbeAddOp(v, OP_MoveTo, base, 0);
drh5383ae52003-06-04 12:23:30 +0000736 }
737 seenReplace = 1;
738 break;
drh0ca3e242002-01-29 23:07:02 +0000739 }
drh5383ae52003-06-04 12:23:30 +0000740 case OE_Ignore: {
741 assert( seenReplace==0 );
danielk19774adee202004-05-08 08:23:19 +0000742 sqlite3VdbeAddOp(v, OP_Pop, nCol+1+hasTwoRecnos, 0);
743 sqlite3VdbeAddOp(v, OP_Goto, 0, ignoreDest);
drh5383ae52003-06-04 12:23:30 +0000744 break;
745 }
746 }
danielk19774adee202004-05-08 08:23:19 +0000747 contAddr = sqlite3VdbeCurrentAddr(v);
748 sqlite3VdbeChangeP2(v, jumpInst2, contAddr);
drh5383ae52003-06-04 12:23:30 +0000749 if( isUpdate ){
danielk19774adee202004-05-08 08:23:19 +0000750 sqlite3VdbeChangeP2(v, jumpInst1, contAddr);
751 sqlite3VdbeAddOp(v, OP_Dup, nCol+1, 1);
752 sqlite3VdbeAddOp(v, OP_MoveTo, base, 0);
drh0ca3e242002-01-29 23:07:02 +0000753 }
754 }
drh0bd1f4e2002-06-06 18:54:39 +0000755
756 /* Test all UNIQUE constraints by creating entries for each UNIQUE
757 ** index and making sure that duplicate entries do not already exist.
758 ** Add the new records to the indices as we go.
759 */
drhb2fe7d82003-04-20 17:29:23 +0000760 extra = -1;
761 for(iCur=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, iCur++){
762 if( aIdxUsed && aIdxUsed[iCur]==0 ) continue; /* Skip unused indices */
763 extra++;
764
765 /* Create a key for accessing the index entry */
danielk19774adee202004-05-08 08:23:19 +0000766 sqlite3VdbeAddOp(v, OP_Dup, nCol+extra, 1);
drh9cfcf5d2002-01-29 18:41:24 +0000767 for(i=0; i<pIdx->nColumn; i++){
768 int idx = pIdx->aiColumn[i];
769 if( idx==pTab->iPKey ){
danielk19774adee202004-05-08 08:23:19 +0000770 sqlite3VdbeAddOp(v, OP_Dup, i+extra+nCol+1, 1);
drh9cfcf5d2002-01-29 18:41:24 +0000771 }else{
danielk19774adee202004-05-08 08:23:19 +0000772 sqlite3VdbeAddOp(v, OP_Dup, i+extra+nCol-idx, 1);
drh9cfcf5d2002-01-29 18:41:24 +0000773 }
774 }
danielk19774adee202004-05-08 08:23:19 +0000775 jumpInst1 = sqlite3VdbeAddOp(v, OP_MakeIdxKey, pIdx->nColumn, 0);
danielk19773d68f032004-05-11 07:11:51 +0000776 sqlite3AddIdxKeyType(v, pIdx);
drhb2fe7d82003-04-20 17:29:23 +0000777
778 /* Find out what action to take in case there is an indexing conflict */
drh9cfcf5d2002-01-29 18:41:24 +0000779 onError = pIdx->onError;
drhb2fe7d82003-04-20 17:29:23 +0000780 if( onError==OE_None ) continue; /* pIdx is not a UNIQUE index */
drh9cfcf5d2002-01-29 18:41:24 +0000781 if( overrideError!=OE_Default ){
782 onError = overrideError;
drha996e472003-05-16 02:30:27 +0000783 }else if( pParse->db->onError!=OE_Default ){
drh0d65dc02002-02-03 00:56:09 +0000784 onError = pParse->db->onError;
drha996e472003-05-16 02:30:27 +0000785 }else if( onError==OE_Default ){
786 onError = OE_Abort;
drh9cfcf5d2002-01-29 18:41:24 +0000787 }
drh5383ae52003-06-04 12:23:30 +0000788 if( seenReplace ){
789 if( onError==OE_Ignore ) onError = OE_Replace;
790 else if( onError==OE_Fail ) onError = OE_Abort;
791 }
792
drhb2fe7d82003-04-20 17:29:23 +0000793
794 /* Check to see if the new index entry will be unique */
danielk19774adee202004-05-08 08:23:19 +0000795 sqlite3VdbeAddOp(v, OP_Dup, extra+nCol+1+hasTwoRecnos, 1);
796 jumpInst2 = sqlite3VdbeAddOp(v, OP_IsUnique, base+iCur+1, 0);
drhb2fe7d82003-04-20 17:29:23 +0000797
798 /* Generate code that executes if the new index entry is not unique */
drh9cfcf5d2002-01-29 18:41:24 +0000799 switch( onError ){
drh1c928532002-01-31 15:54:21 +0000800 case OE_Rollback:
801 case OE_Abort:
802 case OE_Fail: {
drh37ed48e2003-08-05 13:13:38 +0000803 int j, n1, n2;
804 char zErrMsg[200];
805 strcpy(zErrMsg, pIdx->nColumn>1 ? "columns " : "column ");
806 n1 = strlen(zErrMsg);
807 for(j=0; j<pIdx->nColumn && n1<sizeof(zErrMsg)-30; j++){
808 char *zCol = pTab->aCol[pIdx->aiColumn[j]].zName;
809 n2 = strlen(zCol);
810 if( j>0 ){
811 strcpy(&zErrMsg[n1], ", ");
812 n1 += 2;
813 }
814 if( n1+n2>sizeof(zErrMsg)-30 ){
815 strcpy(&zErrMsg[n1], "...");
816 n1 += 3;
817 break;
818 }else{
819 strcpy(&zErrMsg[n1], zCol);
820 n1 += n2;
821 }
822 }
823 strcpy(&zErrMsg[n1],
824 pIdx->nColumn>1 ? " are not unique" : " is not unique");
danielk19774adee202004-05-08 08:23:19 +0000825 sqlite3VdbeOp3(v, OP_Halt, SQLITE_CONSTRAINT, onError, zErrMsg, 0);
drh9cfcf5d2002-01-29 18:41:24 +0000826 break;
827 }
828 case OE_Ignore: {
drh0ca3e242002-01-29 23:07:02 +0000829 assert( seenReplace==0 );
danielk19774adee202004-05-08 08:23:19 +0000830 sqlite3VdbeAddOp(v, OP_Pop, nCol+extra+3+hasTwoRecnos, 0);
831 sqlite3VdbeAddOp(v, OP_Goto, 0, ignoreDest);
drh9cfcf5d2002-01-29 18:41:24 +0000832 break;
833 }
834 case OE_Replace: {
danielk19774adee202004-05-08 08:23:19 +0000835 sqlite3GenerateRowDelete(pParse->db, v, pTab, base, 0);
drh9cfcf5d2002-01-29 18:41:24 +0000836 if( isUpdate ){
danielk19774adee202004-05-08 08:23:19 +0000837 sqlite3VdbeAddOp(v, OP_Dup, nCol+extra+1+hasTwoRecnos, 1);
838 sqlite3VdbeAddOp(v, OP_MoveTo, base, 0);
drh9cfcf5d2002-01-29 18:41:24 +0000839 }
drh0ca3e242002-01-29 23:07:02 +0000840 seenReplace = 1;
drh9cfcf5d2002-01-29 18:41:24 +0000841 break;
842 }
drh0ca3e242002-01-29 23:07:02 +0000843 default: assert(0);
drh9cfcf5d2002-01-29 18:41:24 +0000844 }
danielk19774adee202004-05-08 08:23:19 +0000845 contAddr = sqlite3VdbeCurrentAddr(v);
drh0bd1f4e2002-06-06 18:54:39 +0000846#if NULL_DISTINCT_FOR_UNIQUE
danielk19774adee202004-05-08 08:23:19 +0000847 sqlite3VdbeChangeP2(v, jumpInst1, contAddr);
drh0bd1f4e2002-06-06 18:54:39 +0000848#endif
danielk19774adee202004-05-08 08:23:19 +0000849 sqlite3VdbeChangeP2(v, jumpInst2, contAddr);
drh9cfcf5d2002-01-29 18:41:24 +0000850 }
851}
drh0ca3e242002-01-29 23:07:02 +0000852
853/*
854** This routine generates code to finish the INSERT or UPDATE operation
danielk19774adee202004-05-08 08:23:19 +0000855** that was started by a prior call to sqlite3GenerateConstraintChecks.
drh0ca3e242002-01-29 23:07:02 +0000856** The stack must contain keys for all active indices followed by data
857** and the recno for the new entry. This routine creates the new
858** entries in all indices and in the main table.
859**
drhb419a922002-01-30 16:17:23 +0000860** The arguments to this routine should be the same as the first six
danielk19774adee202004-05-08 08:23:19 +0000861** arguments to sqlite3GenerateConstraintChecks.
drh0ca3e242002-01-29 23:07:02 +0000862*/
danielk19774adee202004-05-08 08:23:19 +0000863void sqlite3CompleteInsertion(
drh0ca3e242002-01-29 23:07:02 +0000864 Parse *pParse, /* The parser context */
865 Table *pTab, /* the table into which we are inserting */
866 int base, /* Index of a read/write cursor pointing at pTab */
867 char *aIdxUsed, /* Which indices are used. NULL means all are used */
drhb419a922002-01-30 16:17:23 +0000868 int recnoChng, /* True if the record number will change */
drh70ce3f02003-04-15 19:22:22 +0000869 int isUpdate, /* True for UPDATE, False for INSERT */
870 int newIdx /* Index of NEW table for triggers. -1 if none */
drh0ca3e242002-01-29 23:07:02 +0000871){
872 int i;
873 Vdbe *v;
874 int nIdx;
875 Index *pIdx;
876
danielk19774adee202004-05-08 08:23:19 +0000877 v = sqlite3GetVdbe(pParse);
drh0ca3e242002-01-29 23:07:02 +0000878 assert( v!=0 );
drh417be792002-03-03 18:59:40 +0000879 assert( pTab->pSelect==0 ); /* This table is not a VIEW */
drh0ca3e242002-01-29 23:07:02 +0000880 for(nIdx=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, nIdx++){}
881 for(i=nIdx-1; i>=0; i--){
882 if( aIdxUsed && aIdxUsed[i]==0 ) continue;
danielk19774adee202004-05-08 08:23:19 +0000883 sqlite3VdbeAddOp(v, OP_IdxPut, base+i+1, 0);
drh0ca3e242002-01-29 23:07:02 +0000884 }
danielk19774adee202004-05-08 08:23:19 +0000885 sqlite3VdbeAddOp(v, OP_MakeRecord, pTab->nCol, 0);
drh70ce3f02003-04-15 19:22:22 +0000886 if( newIdx>=0 ){
danielk19774adee202004-05-08 08:23:19 +0000887 sqlite3VdbeAddOp(v, OP_Dup, 1, 0);
888 sqlite3VdbeAddOp(v, OP_Dup, 1, 0);
889 sqlite3VdbeAddOp(v, OP_PutIntKey, newIdx, 0);
drh70ce3f02003-04-15 19:22:22 +0000890 }
danielk19774adee202004-05-08 08:23:19 +0000891 sqlite3VdbeAddOp(v, OP_PutIntKey, base,
rdcb0c374f2004-02-20 22:53:38 +0000892 (pParse->trigStack?0:OPFLAG_NCHANGE) |
893 (isUpdate?0:OPFLAG_LASTROWID) | OPFLAG_CSCHANGE);
drhb419a922002-01-30 16:17:23 +0000894 if( isUpdate && recnoChng ){
danielk19774adee202004-05-08 08:23:19 +0000895 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
drh0ca3e242002-01-29 23:07:02 +0000896 }
897}
drhcd446902004-02-24 01:05:31 +0000898
899/*
900** Generate code that will open write cursors for a table and for all
901** indices of that table. The "base" parameter is the cursor number used
902** for the table. Indices are opened on subsequent cursors.
903**
904** Return the total number of cursors opened. This is always at least
905** 1 (for the main table) plus more for each cursor.
906*/
danielk19774adee202004-05-08 08:23:19 +0000907int sqlite3OpenTableAndIndices(Parse *pParse, Table *pTab, int base){
drhcd446902004-02-24 01:05:31 +0000908 int i;
909 Index *pIdx;
danielk19774adee202004-05-08 08:23:19 +0000910 Vdbe *v = sqlite3GetVdbe(pParse);
drhcd446902004-02-24 01:05:31 +0000911 assert( v!=0 );
danielk19774adee202004-05-08 08:23:19 +0000912 sqlite3VdbeAddOp(v, OP_Integer, pTab->iDb, 0);
913 sqlite3VdbeOp3(v, OP_OpenWrite, base, pTab->tnum, pTab->zName, P3_STATIC);
drhcd446902004-02-24 01:05:31 +0000914 for(i=1, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){
danielk19774adee202004-05-08 08:23:19 +0000915 sqlite3VdbeAddOp(v, OP_Integer, pIdx->iDb, 0);
916 sqlite3VdbeOp3(v, OP_OpenWrite, i+base, pIdx->tnum, pIdx->zName, P3_STATIC);
drhcd446902004-02-24 01:05:31 +0000917 }
918 return i;
919}
danielk19774adee202004-05-08 08:23:19 +0000920
921
922