|
1 /* |
|
2 ** 2001 September 15 |
|
3 ** |
|
4 ** The author disclaims copyright to this source code. In place of |
|
5 ** a legal notice, here is a blessing: |
|
6 ** |
|
7 ** 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. |
|
10 ** |
|
11 ************************************************************************* |
|
12 ** This file contains C code routines that are called by the parser |
|
13 ** to handle INSERT statements in SQLite. |
|
14 ** |
|
15 ** $Id: insert.c,v 1.172 2006/08/29 18:46:14 drh Exp $ |
|
16 */ |
|
17 #include "sqliteInt.h" |
|
18 |
|
19 /* |
|
20 ** Set P3 of the most recently inserted opcode to a column affinity |
|
21 ** string for index pIdx. A column affinity string has one character |
|
22 ** for each column in the table, according to the affinity of the column: |
|
23 ** |
|
24 ** Character Column affinity |
|
25 ** ------------------------------ |
|
26 ** 'a' TEXT |
|
27 ** 'b' NONE |
|
28 ** 'c' NUMERIC |
|
29 ** 'd' INTEGER |
|
30 ** 'e' REAL |
|
31 */ |
|
32 void sqlite3IndexAffinityStr(Vdbe *v, Index *pIdx){ |
|
33 if( !pIdx->zColAff ){ |
|
34 /* The first time a column affinity string for a particular index is |
|
35 ** required, it is allocated and populated here. It is then stored as |
|
36 ** a member of the Index structure for subsequent use. |
|
37 ** |
|
38 ** The column affinity string will eventually be deleted by |
|
39 ** sqliteDeleteIndex() when the Index structure itself is cleaned |
|
40 ** up. |
|
41 */ |
|
42 int n; |
|
43 Table *pTab = pIdx->pTable; |
|
44 pIdx->zColAff = (char *)sqliteMalloc(pIdx->nColumn+1); |
|
45 if( !pIdx->zColAff ){ |
|
46 return; |
|
47 } |
|
48 for(n=0; n<pIdx->nColumn; n++){ |
|
49 pIdx->zColAff[n] = pTab->aCol[pIdx->aiColumn[n]].affinity; |
|
50 } |
|
51 pIdx->zColAff[pIdx->nColumn] = '\0'; |
|
52 } |
|
53 |
|
54 sqlite3VdbeChangeP3(v, -1, pIdx->zColAff, 0); |
|
55 } |
|
56 |
|
57 /* |
|
58 ** Set P3 of the most recently inserted opcode to a column affinity |
|
59 ** string for table pTab. A column affinity string has one character |
|
60 ** for each column indexed by the index, according to the affinity of the |
|
61 ** column: |
|
62 ** |
|
63 ** Character Column affinity |
|
64 ** ------------------------------ |
|
65 ** 'a' TEXT |
|
66 ** 'b' NONE |
|
67 ** 'c' NUMERIC |
|
68 ** 'd' INTEGER |
|
69 ** 'e' REAL |
|
70 */ |
|
71 void sqlite3TableAffinityStr(Vdbe *v, Table *pTab){ |
|
72 /* The first time a column affinity string for a particular table |
|
73 ** is required, it is allocated and populated here. It is then |
|
74 ** stored as a member of the Table structure for subsequent use. |
|
75 ** |
|
76 ** The column affinity string will eventually be deleted by |
|
77 ** sqlite3DeleteTable() when the Table structure itself is cleaned up. |
|
78 */ |
|
79 if( !pTab->zColAff ){ |
|
80 char *zColAff; |
|
81 int i; |
|
82 |
|
83 zColAff = (char *)sqliteMalloc(pTab->nCol+1); |
|
84 if( !zColAff ){ |
|
85 return; |
|
86 } |
|
87 |
|
88 for(i=0; i<pTab->nCol; i++){ |
|
89 zColAff[i] = pTab->aCol[i].affinity; |
|
90 } |
|
91 zColAff[pTab->nCol] = '\0'; |
|
92 |
|
93 pTab->zColAff = zColAff; |
|
94 } |
|
95 |
|
96 sqlite3VdbeChangeP3(v, -1, pTab->zColAff, 0); |
|
97 } |
|
98 |
|
99 /* |
|
100 ** Return non-zero if SELECT statement p opens the table with rootpage |
|
101 ** iTab in database iDb. This is used to see if a statement of the form |
|
102 ** "INSERT INTO <iDb, iTab> SELECT ..." can run without using temporary |
|
103 ** table for the results of the SELECT. |
|
104 ** |
|
105 ** No checking is done for sub-selects that are part of expressions. |
|
106 */ |
|
107 static int selectReadsTable(Select *p, Schema *pSchema, int iTab){ |
|
108 int i; |
|
109 struct SrcList_item *pItem; |
|
110 if( p->pSrc==0 ) return 0; |
|
111 for(i=0, pItem=p->pSrc->a; i<p->pSrc->nSrc; i++, pItem++){ |
|
112 if( pItem->pSelect ){ |
|
113 if( selectReadsTable(pItem->pSelect, pSchema, iTab) ) return 1; |
|
114 }else{ |
|
115 if( pItem->pTab->pSchema==pSchema && pItem->pTab->tnum==iTab ) return 1; |
|
116 } |
|
117 } |
|
118 return 0; |
|
119 } |
|
120 |
|
121 /* |
|
122 ** This routine is call to handle SQL of the following forms: |
|
123 ** |
|
124 ** insert into TABLE (IDLIST) values(EXPRLIST) |
|
125 ** insert into TABLE (IDLIST) select |
|
126 ** |
|
127 ** The IDLIST following the table name is always optional. If omitted, |
|
128 ** then a list of all columns for the table is substituted. The IDLIST |
|
129 ** appears in the pColumn parameter. pColumn is NULL if IDLIST is omitted. |
|
130 ** |
|
131 ** The pList parameter holds EXPRLIST in the first form of the INSERT |
|
132 ** statement above, and pSelect is NULL. For the second form, pList is |
|
133 ** NULL and pSelect is a pointer to the select statement used to generate |
|
134 ** data for the insert. |
|
135 ** |
|
136 ** The code generated follows one of three templates. For a simple |
|
137 ** select with data coming from a VALUES clause, the code executes |
|
138 ** once straight down through. The template looks like this: |
|
139 ** |
|
140 ** open write cursor to <table> and its indices |
|
141 ** puts VALUES clause expressions onto the stack |
|
142 ** write the resulting record into <table> |
|
143 ** cleanup |
|
144 ** |
|
145 ** If the statement is of the form |
|
146 ** |
|
147 ** INSERT INTO <table> SELECT ... |
|
148 ** |
|
149 ** And the SELECT clause does not read from <table> at any time, then |
|
150 ** the generated code follows this template: |
|
151 ** |
|
152 ** goto B |
|
153 ** A: setup for the SELECT |
|
154 ** loop over the tables in the SELECT |
|
155 ** gosub C |
|
156 ** end loop |
|
157 ** cleanup after the SELECT |
|
158 ** goto D |
|
159 ** B: open write cursor to <table> and its indices |
|
160 ** goto A |
|
161 ** C: insert the select result into <table> |
|
162 ** return |
|
163 ** D: cleanup |
|
164 ** |
|
165 ** The third template is used if the insert statement takes its |
|
166 ** values from a SELECT but the data is being inserted into a table |
|
167 ** that is also read as part of the SELECT. In the third form, |
|
168 ** we have to use a intermediate table to store the results of |
|
169 ** the select. The template is like this: |
|
170 ** |
|
171 ** goto B |
|
172 ** A: setup for the SELECT |
|
173 ** loop over the tables in the SELECT |
|
174 ** gosub C |
|
175 ** end loop |
|
176 ** cleanup after the SELECT |
|
177 ** goto D |
|
178 ** C: insert the select result into the intermediate table |
|
179 ** return |
|
180 ** B: open a cursor to an intermediate table |
|
181 ** goto A |
|
182 ** D: open write cursor to <table> and its indices |
|
183 ** loop over the intermediate table |
|
184 ** transfer values form intermediate table into <table> |
|
185 ** end the loop |
|
186 ** cleanup |
|
187 */ |
|
188 void sqlite3Insert( |
|
189 Parse *pParse, /* Parser context */ |
|
190 SrcList *pTabList, /* Name of table into which we are inserting */ |
|
191 ExprList *pList, /* List of values to be inserted */ |
|
192 Select *pSelect, /* A SELECT statement to use as the data source */ |
|
193 IdList *pColumn, /* Column names corresponding to IDLIST. */ |
|
194 int onError /* How to handle constraint errors */ |
|
195 ){ |
|
196 Table *pTab; /* The table to insert into */ |
|
197 char *zTab; /* Name of the table into which we are inserting */ |
|
198 const char *zDb; /* Name of the database holding this table */ |
|
199 int i, j, idx; /* Loop counters */ |
|
200 Vdbe *v; /* Generate code into this virtual machine */ |
|
201 Index *pIdx; /* For looping over indices of the table */ |
|
202 int nColumn; /* Number of columns in the data */ |
|
203 int base = 0; /* VDBE Cursor number for pTab */ |
|
204 int iCont=0,iBreak=0; /* Beginning and end of the loop over srcTab */ |
|
205 sqlite3 *db; /* The main database structure */ |
|
206 int keyColumn = -1; /* Column that is the INTEGER PRIMARY KEY */ |
|
207 int endOfLoop; /* Label for the end of the insertion loop */ |
|
208 int useTempTable = 0; /* Store SELECT results in intermediate table */ |
|
209 int srcTab = 0; /* Data comes from this temporary cursor if >=0 */ |
|
210 int iSelectLoop = 0; /* Address of code that implements the SELECT */ |
|
211 int iCleanup = 0; /* Address of the cleanup code */ |
|
212 int iInsertBlock = 0; /* Address of the subroutine used to insert data */ |
|
213 int iCntMem = 0; /* Memory cell used for the row counter */ |
|
214 int newIdx = -1; /* Cursor for the NEW table */ |
|
215 Db *pDb; /* The database containing table being inserted into */ |
|
216 int counterMem = 0; /* Memory cell holding AUTOINCREMENT counter */ |
|
217 int iDb; |
|
218 |
|
219 #ifndef SQLITE_OMIT_TRIGGER |
|
220 int isView; /* True if attempting to insert into a view */ |
|
221 int triggers_exist = 0; /* True if there are FOR EACH ROW triggers */ |
|
222 #endif |
|
223 |
|
224 #ifndef SQLITE_OMIT_AUTOINCREMENT |
|
225 int counterRowid = 0; /* Memory cell holding rowid of autoinc counter */ |
|
226 #endif |
|
227 |
|
228 if( pParse->nErr || sqlite3MallocFailed() ){ |
|
229 goto insert_cleanup; |
|
230 } |
|
231 db = pParse->db; |
|
232 |
|
233 /* Locate the table into which we will be inserting new information. |
|
234 */ |
|
235 assert( pTabList->nSrc==1 ); |
|
236 zTab = pTabList->a[0].zName; |
|
237 if( zTab==0 ) goto insert_cleanup; |
|
238 pTab = sqlite3SrcListLookup(pParse, pTabList); |
|
239 if( pTab==0 ){ |
|
240 goto insert_cleanup; |
|
241 } |
|
242 iDb = sqlite3SchemaToIndex(db, pTab->pSchema); |
|
243 assert( iDb<db->nDb ); |
|
244 pDb = &db->aDb[iDb]; |
|
245 zDb = pDb->zName; |
|
246 if( sqlite3AuthCheck(pParse, SQLITE_INSERT, pTab->zName, 0, zDb) ){ |
|
247 goto insert_cleanup; |
|
248 } |
|
249 |
|
250 /* Figure out if we have any triggers and if the table being |
|
251 ** inserted into is a view |
|
252 */ |
|
253 #ifndef SQLITE_OMIT_TRIGGER |
|
254 triggers_exist = sqlite3TriggersExist(pParse, pTab, TK_INSERT, 0); |
|
255 isView = pTab->pSelect!=0; |
|
256 #else |
|
257 # define triggers_exist 0 |
|
258 # define isView 0 |
|
259 #endif |
|
260 #ifdef SQLITE_OMIT_VIEW |
|
261 # undef isView |
|
262 # define isView 0 |
|
263 #endif |
|
264 |
|
265 /* Ensure that: |
|
266 * (a) the table is not read-only, |
|
267 * (b) that if it is a view then ON INSERT triggers exist |
|
268 */ |
|
269 if( sqlite3IsReadOnly(pParse, pTab, triggers_exist) ){ |
|
270 goto insert_cleanup; |
|
271 } |
|
272 assert( pTab!=0 ); |
|
273 |
|
274 /* If pTab is really a view, make sure it has been initialized. |
|
275 ** ViewGetColumnNames() is a no-op if pTab is not a view (or virtual |
|
276 ** module table). |
|
277 */ |
|
278 if( sqlite3ViewGetColumnNames(pParse, pTab) ){ |
|
279 goto insert_cleanup; |
|
280 } |
|
281 |
|
282 /* Allocate a VDBE |
|
283 */ |
|
284 v = sqlite3GetVdbe(pParse); |
|
285 if( v==0 ) goto insert_cleanup; |
|
286 if( pParse->nested==0 ) sqlite3VdbeCountChanges(v); |
|
287 sqlite3BeginWriteOperation(pParse, pSelect || triggers_exist, iDb); |
|
288 |
|
289 /* if there are row triggers, allocate a temp table for new.* references. */ |
|
290 if( triggers_exist ){ |
|
291 newIdx = pParse->nTab++; |
|
292 } |
|
293 |
|
294 #ifndef SQLITE_OMIT_AUTOINCREMENT |
|
295 /* If this is an AUTOINCREMENT table, look up the sequence number in the |
|
296 ** sqlite_sequence table and store it in memory cell counterMem. Also |
|
297 ** remember the rowid of the sqlite_sequence table entry in memory cell |
|
298 ** counterRowid. |
|
299 */ |
|
300 if( pTab->autoInc ){ |
|
301 int iCur = pParse->nTab; |
|
302 int addr = sqlite3VdbeCurrentAddr(v); |
|
303 counterRowid = pParse->nMem++; |
|
304 counterMem = pParse->nMem++; |
|
305 sqlite3OpenTable(pParse, iCur, iDb, pDb->pSchema->pSeqTab, OP_OpenRead); |
|
306 sqlite3VdbeAddOp(v, OP_Rewind, iCur, addr+13); |
|
307 sqlite3VdbeAddOp(v, OP_Column, iCur, 0); |
|
308 sqlite3VdbeOp3(v, OP_String8, 0, 0, pTab->zName, 0); |
|
309 sqlite3VdbeAddOp(v, OP_Ne, 0x100, addr+12); |
|
310 sqlite3VdbeAddOp(v, OP_Rowid, iCur, 0); |
|
311 sqlite3VdbeAddOp(v, OP_MemStore, counterRowid, 1); |
|
312 sqlite3VdbeAddOp(v, OP_Column, iCur, 1); |
|
313 sqlite3VdbeAddOp(v, OP_MemStore, counterMem, 1); |
|
314 sqlite3VdbeAddOp(v, OP_Goto, 0, addr+13); |
|
315 sqlite3VdbeAddOp(v, OP_Next, iCur, addr+4); |
|
316 sqlite3VdbeAddOp(v, OP_Close, iCur, 0); |
|
317 } |
|
318 #endif /* SQLITE_OMIT_AUTOINCREMENT */ |
|
319 |
|
320 /* Figure out how many columns of data are supplied. If the data |
|
321 ** is coming from a SELECT statement, then this step also generates |
|
322 ** all the code to implement the SELECT statement and invoke a subroutine |
|
323 ** to process each row of the result. (Template 2.) If the SELECT |
|
324 ** statement uses the the table that is being inserted into, then the |
|
325 ** subroutine is also coded here. That subroutine stores the SELECT |
|
326 ** results in a temporary table. (Template 3.) |
|
327 */ |
|
328 if( pSelect ){ |
|
329 /* Data is coming from a SELECT. Generate code to implement that SELECT |
|
330 */ |
|
331 int rc, iInitCode; |
|
332 iInitCode = sqlite3VdbeAddOp(v, OP_Goto, 0, 0); |
|
333 iSelectLoop = sqlite3VdbeCurrentAddr(v); |
|
334 iInsertBlock = sqlite3VdbeMakeLabel(v); |
|
335 |
|
336 /* Resolve the expressions in the SELECT statement and execute it. */ |
|
337 rc = sqlite3Select(pParse, pSelect, SRT_Subroutine, iInsertBlock,0,0,0,0); |
|
338 if( rc || pParse->nErr || sqlite3MallocFailed() ){ |
|
339 goto insert_cleanup; |
|
340 } |
|
341 |
|
342 iCleanup = sqlite3VdbeMakeLabel(v); |
|
343 sqlite3VdbeAddOp(v, OP_Goto, 0, iCleanup); |
|
344 assert( pSelect->pEList ); |
|
345 nColumn = pSelect->pEList->nExpr; |
|
346 |
|
347 /* Set useTempTable to TRUE if the result of the SELECT statement |
|
348 ** should be written into a temporary table. Set to FALSE if each |
|
349 ** row of the SELECT can be written directly into the result table. |
|
350 ** |
|
351 ** A temp table must be used if the table being updated is also one |
|
352 ** of the tables being read by the SELECT statement. Also use a |
|
353 ** temp table in the case of row triggers. |
|
354 */ |
|
355 if( triggers_exist || selectReadsTable(pSelect,pTab->pSchema,pTab->tnum) ){ |
|
356 useTempTable = 1; |
|
357 } |
|
358 |
|
359 if( useTempTable ){ |
|
360 /* Generate the subroutine that SELECT calls to process each row of |
|
361 ** the result. Store the result in a temporary table |
|
362 */ |
|
363 srcTab = pParse->nTab++; |
|
364 sqlite3VdbeResolveLabel(v, iInsertBlock); |
|
365 sqlite3VdbeAddOp(v, OP_MakeRecord, nColumn, 0); |
|
366 sqlite3VdbeAddOp(v, OP_NewRowid, srcTab, 0); |
|
367 sqlite3VdbeAddOp(v, OP_Pull, 1, 0); |
|
368 sqlite3VdbeAddOp(v, OP_Insert, srcTab, 0); |
|
369 sqlite3VdbeAddOp(v, OP_Return, 0, 0); |
|
370 |
|
371 /* The following code runs first because the GOTO at the very top |
|
372 ** of the program jumps to it. Create the temporary table, then jump |
|
373 ** back up and execute the SELECT code above. |
|
374 */ |
|
375 sqlite3VdbeJumpHere(v, iInitCode); |
|
376 sqlite3VdbeAddOp(v, OP_OpenEphemeral, srcTab, 0); |
|
377 sqlite3VdbeAddOp(v, OP_SetNumColumns, srcTab, nColumn); |
|
378 sqlite3VdbeAddOp(v, OP_Goto, 0, iSelectLoop); |
|
379 sqlite3VdbeResolveLabel(v, iCleanup); |
|
380 }else{ |
|
381 sqlite3VdbeJumpHere(v, iInitCode); |
|
382 } |
|
383 }else{ |
|
384 /* This is the case if the data for the INSERT is coming from a VALUES |
|
385 ** clause |
|
386 */ |
|
387 NameContext sNC; |
|
388 memset(&sNC, 0, sizeof(sNC)); |
|
389 sNC.pParse = pParse; |
|
390 srcTab = -1; |
|
391 useTempTable = 0; |
|
392 nColumn = pList ? pList->nExpr : 0; |
|
393 for(i=0; i<nColumn; i++){ |
|
394 if( sqlite3ExprResolveNames(&sNC, pList->a[i].pExpr) ){ |
|
395 goto insert_cleanup; |
|
396 } |
|
397 } |
|
398 } |
|
399 |
|
400 /* Make sure the number of columns in the source data matches the number |
|
401 ** of columns to be inserted into the table. |
|
402 */ |
|
403 if( pColumn==0 && nColumn && nColumn!=pTab->nCol ){ |
|
404 sqlite3ErrorMsg(pParse, |
|
405 "table %S has %d columns but %d values were supplied", |
|
406 pTabList, 0, pTab->nCol, nColumn); |
|
407 goto insert_cleanup; |
|
408 } |
|
409 if( pColumn!=0 && nColumn!=pColumn->nId ){ |
|
410 sqlite3ErrorMsg(pParse, "%d values for %d columns", nColumn, pColumn->nId); |
|
411 goto insert_cleanup; |
|
412 } |
|
413 |
|
414 /* If the INSERT statement included an IDLIST term, then make sure |
|
415 ** all elements of the IDLIST really are columns of the table and |
|
416 ** remember the column indices. |
|
417 ** |
|
418 ** If the table has an INTEGER PRIMARY KEY column and that column |
|
419 ** is named in the IDLIST, then record in the keyColumn variable |
|
420 ** the index into IDLIST of the primary key column. keyColumn is |
|
421 ** the index of the primary key as it appears in IDLIST, not as |
|
422 ** is appears in the original table. (The index of the primary |
|
423 ** key in the original table is pTab->iPKey.) |
|
424 */ |
|
425 if( pColumn ){ |
|
426 for(i=0; i<pColumn->nId; i++){ |
|
427 pColumn->a[i].idx = -1; |
|
428 } |
|
429 for(i=0; i<pColumn->nId; i++){ |
|
430 for(j=0; j<pTab->nCol; j++){ |
|
431 if( sqlite3StrICmp(pColumn->a[i].zName, pTab->aCol[j].zName)==0 ){ |
|
432 pColumn->a[i].idx = j; |
|
433 if( j==pTab->iPKey ){ |
|
434 keyColumn = i; |
|
435 } |
|
436 break; |
|
437 } |
|
438 } |
|
439 if( j>=pTab->nCol ){ |
|
440 if( sqlite3IsRowid(pColumn->a[i].zName) ){ |
|
441 keyColumn = i; |
|
442 }else{ |
|
443 sqlite3ErrorMsg(pParse, "table %S has no column named %s", |
|
444 pTabList, 0, pColumn->a[i].zName); |
|
445 pParse->nErr++; |
|
446 goto insert_cleanup; |
|
447 } |
|
448 } |
|
449 } |
|
450 } |
|
451 |
|
452 /* If there is no IDLIST term but the table has an integer primary |
|
453 ** key, the set the keyColumn variable to the primary key column index |
|
454 ** in the original table definition. |
|
455 */ |
|
456 if( pColumn==0 && nColumn>0 ){ |
|
457 keyColumn = pTab->iPKey; |
|
458 } |
|
459 |
|
460 /* Open the temp table for FOR EACH ROW triggers |
|
461 */ |
|
462 if( triggers_exist ){ |
|
463 sqlite3VdbeAddOp(v, OP_OpenPseudo, newIdx, 0); |
|
464 sqlite3VdbeAddOp(v, OP_SetNumColumns, newIdx, pTab->nCol); |
|
465 } |
|
466 |
|
467 /* Initialize the count of rows to be inserted |
|
468 */ |
|
469 if( db->flags & SQLITE_CountRows ){ |
|
470 iCntMem = pParse->nMem++; |
|
471 sqlite3VdbeAddOp(v, OP_MemInt, 0, iCntMem); |
|
472 } |
|
473 |
|
474 /* Open tables and indices if there are no row triggers */ |
|
475 if( !triggers_exist ){ |
|
476 base = pParse->nTab; |
|
477 sqlite3OpenTableAndIndices(pParse, pTab, base, OP_OpenWrite); |
|
478 } |
|
479 |
|
480 /* If the data source is a temporary table, then we have to create |
|
481 ** a loop because there might be multiple rows of data. If the data |
|
482 ** source is a subroutine call from the SELECT statement, then we need |
|
483 ** to launch the SELECT statement processing. |
|
484 */ |
|
485 if( useTempTable ){ |
|
486 iBreak = sqlite3VdbeMakeLabel(v); |
|
487 sqlite3VdbeAddOp(v, OP_Rewind, srcTab, iBreak); |
|
488 iCont = sqlite3VdbeCurrentAddr(v); |
|
489 }else if( pSelect ){ |
|
490 sqlite3VdbeAddOp(v, OP_Goto, 0, iSelectLoop); |
|
491 sqlite3VdbeResolveLabel(v, iInsertBlock); |
|
492 } |
|
493 |
|
494 /* Run the BEFORE and INSTEAD OF triggers, if there are any |
|
495 */ |
|
496 endOfLoop = sqlite3VdbeMakeLabel(v); |
|
497 if( triggers_exist & TRIGGER_BEFORE ){ |
|
498 |
|
499 /* build the NEW.* reference row. Note that if there is an INTEGER |
|
500 ** PRIMARY KEY into which a NULL is being inserted, that NULL will be |
|
501 ** translated into a unique ID for the row. But on a BEFORE trigger, |
|
502 ** we do not know what the unique ID will be (because the insert has |
|
503 ** not happened yet) so we substitute a rowid of -1 |
|
504 */ |
|
505 if( keyColumn<0 ){ |
|
506 sqlite3VdbeAddOp(v, OP_Integer, -1, 0); |
|
507 }else if( useTempTable ){ |
|
508 sqlite3VdbeAddOp(v, OP_Column, srcTab, keyColumn); |
|
509 }else{ |
|
510 assert( pSelect==0 ); /* Otherwise useTempTable is true */ |
|
511 sqlite3ExprCode(pParse, pList->a[keyColumn].pExpr); |
|
512 sqlite3VdbeAddOp(v, OP_NotNull, -1, sqlite3VdbeCurrentAddr(v)+3); |
|
513 sqlite3VdbeAddOp(v, OP_Pop, 1, 0); |
|
514 sqlite3VdbeAddOp(v, OP_Integer, -1, 0); |
|
515 sqlite3VdbeAddOp(v, OP_MustBeInt, 0, 0); |
|
516 } |
|
517 |
|
518 /* Create the new column data |
|
519 */ |
|
520 for(i=0; i<pTab->nCol; i++){ |
|
521 if( pColumn==0 ){ |
|
522 j = i; |
|
523 }else{ |
|
524 for(j=0; j<pColumn->nId; j++){ |
|
525 if( pColumn->a[j].idx==i ) break; |
|
526 } |
|
527 } |
|
528 if( pColumn && j>=pColumn->nId ){ |
|
529 sqlite3ExprCode(pParse, pTab->aCol[i].pDflt); |
|
530 }else if( useTempTable ){ |
|
531 sqlite3VdbeAddOp(v, OP_Column, srcTab, j); |
|
532 }else{ |
|
533 assert( pSelect==0 ); /* Otherwise useTempTable is true */ |
|
534 sqlite3ExprCodeAndCache(pParse, pList->a[j].pExpr); |
|
535 } |
|
536 } |
|
537 sqlite3VdbeAddOp(v, OP_MakeRecord, pTab->nCol, 0); |
|
538 |
|
539 /* If this is an INSERT on a view with an INSTEAD OF INSERT trigger, |
|
540 ** do not attempt any conversions before assembling the record. |
|
541 ** If this is a real table, attempt conversions as required by the |
|
542 ** table column affinities. |
|
543 */ |
|
544 if( !isView ){ |
|
545 sqlite3TableAffinityStr(v, pTab); |
|
546 } |
|
547 sqlite3VdbeAddOp(v, OP_Insert, newIdx, 0); |
|
548 |
|
549 /* Fire BEFORE or INSTEAD OF triggers */ |
|
550 if( sqlite3CodeRowTrigger(pParse, TK_INSERT, 0, TRIGGER_BEFORE, pTab, |
|
551 newIdx, -1, onError, endOfLoop) ){ |
|
552 goto insert_cleanup; |
|
553 } |
|
554 } |
|
555 |
|
556 /* If any triggers exists, the opening of tables and indices is deferred |
|
557 ** until now. |
|
558 */ |
|
559 if( triggers_exist && !isView ){ |
|
560 base = pParse->nTab; |
|
561 sqlite3OpenTableAndIndices(pParse, pTab, base, OP_OpenWrite); |
|
562 } |
|
563 |
|
564 /* Push the record number for the new entry onto the stack. The |
|
565 ** record number is a randomly generate integer created by NewRowid |
|
566 ** except when the table has an INTEGER PRIMARY KEY column, in which |
|
567 ** case the record number is the same as that column. |
|
568 */ |
|
569 if( !isView ){ |
|
570 if( IsVirtual(pTab) ){ |
|
571 /* The row that the VUpdate opcode will delete: none */ |
|
572 sqlite3VdbeAddOp(v, OP_Null, 0, 0); |
|
573 } |
|
574 if( keyColumn>=0 ){ |
|
575 if( useTempTable ){ |
|
576 sqlite3VdbeAddOp(v, OP_Column, srcTab, keyColumn); |
|
577 }else if( pSelect ){ |
|
578 sqlite3VdbeAddOp(v, OP_Dup, nColumn - keyColumn - 1, 1); |
|
579 }else{ |
|
580 sqlite3ExprCode(pParse, pList->a[keyColumn].pExpr); |
|
581 } |
|
582 /* If the PRIMARY KEY expression is NULL, then use OP_NewRowid |
|
583 ** to generate a unique primary key value. |
|
584 */ |
|
585 sqlite3VdbeAddOp(v, OP_NotNull, -1, sqlite3VdbeCurrentAddr(v)+3); |
|
586 sqlite3VdbeAddOp(v, OP_Pop, 1, 0); |
|
587 sqlite3VdbeAddOp(v, OP_NewRowid, base, counterMem); |
|
588 sqlite3VdbeAddOp(v, OP_MustBeInt, 0, 0); |
|
589 }else if( IsVirtual(pTab) ){ |
|
590 sqlite3VdbeAddOp(v, OP_Null, 0, 0); |
|
591 }else{ |
|
592 sqlite3VdbeAddOp(v, OP_NewRowid, base, counterMem); |
|
593 } |
|
594 #ifndef SQLITE_OMIT_AUTOINCREMENT |
|
595 if( pTab->autoInc ){ |
|
596 sqlite3VdbeAddOp(v, OP_MemMax, counterMem, 0); |
|
597 } |
|
598 #endif /* SQLITE_OMIT_AUTOINCREMENT */ |
|
599 |
|
600 /* Push onto the stack, data for all columns of the new entry, beginning |
|
601 ** with the first column. |
|
602 */ |
|
603 for(i=0; i<pTab->nCol; i++){ |
|
604 if( i==pTab->iPKey ){ |
|
605 /* The value of the INTEGER PRIMARY KEY column is always a NULL. |
|
606 ** Whenever this column is read, the record number will be substituted |
|
607 ** in its place. So will fill this column with a NULL to avoid |
|
608 ** taking up data space with information that will never be used. */ |
|
609 sqlite3VdbeAddOp(v, OP_Null, 0, 0); |
|
610 continue; |
|
611 } |
|
612 if( pColumn==0 ){ |
|
613 j = i; |
|
614 }else{ |
|
615 for(j=0; j<pColumn->nId; j++){ |
|
616 if( pColumn->a[j].idx==i ) break; |
|
617 } |
|
618 } |
|
619 if( nColumn==0 || (pColumn && j>=pColumn->nId) ){ |
|
620 sqlite3ExprCode(pParse, pTab->aCol[i].pDflt); |
|
621 }else if( useTempTable ){ |
|
622 sqlite3VdbeAddOp(v, OP_Column, srcTab, j); |
|
623 }else if( pSelect ){ |
|
624 sqlite3VdbeAddOp(v, OP_Dup, i+nColumn-j+IsVirtual(pTab), 1); |
|
625 }else{ |
|
626 sqlite3ExprCode(pParse, pList->a[j].pExpr); |
|
627 } |
|
628 } |
|
629 |
|
630 /* Generate code to check constraints and generate index keys and |
|
631 ** do the insertion. |
|
632 */ |
|
633 #ifndef SQLITE_OMIT_VIRTUALTABLE |
|
634 if( IsVirtual(pTab) ){ |
|
635 pParse->pVirtualLock = pTab; |
|
636 sqlite3VdbeOp3(v, OP_VUpdate, 1, pTab->nCol+2, |
|
637 (const char*)pTab->pVtab, P3_VTAB); |
|
638 }else |
|
639 #endif |
|
640 { |
|
641 sqlite3GenerateConstraintChecks(pParse, pTab, base, 0, keyColumn>=0, |
|
642 0, onError, endOfLoop); |
|
643 sqlite3CompleteInsertion(pParse, pTab, base, 0,0,0, |
|
644 (triggers_exist & TRIGGER_AFTER)!=0 ? newIdx : -1); |
|
645 } |
|
646 } |
|
647 |
|
648 /* Update the count of rows that are inserted |
|
649 */ |
|
650 if( (db->flags & SQLITE_CountRows)!=0 ){ |
|
651 sqlite3VdbeAddOp(v, OP_MemIncr, 1, iCntMem); |
|
652 } |
|
653 |
|
654 if( triggers_exist ){ |
|
655 /* Close all tables opened */ |
|
656 if( !isView ){ |
|
657 sqlite3VdbeAddOp(v, OP_Close, base, 0); |
|
658 for(idx=1, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, idx++){ |
|
659 sqlite3VdbeAddOp(v, OP_Close, idx+base, 0); |
|
660 } |
|
661 } |
|
662 |
|
663 /* Code AFTER triggers */ |
|
664 if( sqlite3CodeRowTrigger(pParse, TK_INSERT, 0, TRIGGER_AFTER, pTab, |
|
665 newIdx, -1, onError, endOfLoop) ){ |
|
666 goto insert_cleanup; |
|
667 } |
|
668 } |
|
669 |
|
670 /* The bottom of the loop, if the data source is a SELECT statement |
|
671 */ |
|
672 sqlite3VdbeResolveLabel(v, endOfLoop); |
|
673 if( useTempTable ){ |
|
674 sqlite3VdbeAddOp(v, OP_Next, srcTab, iCont); |
|
675 sqlite3VdbeResolveLabel(v, iBreak); |
|
676 sqlite3VdbeAddOp(v, OP_Close, srcTab, 0); |
|
677 }else if( pSelect ){ |
|
678 sqlite3VdbeAddOp(v, OP_Pop, nColumn, 0); |
|
679 sqlite3VdbeAddOp(v, OP_Return, 0, 0); |
|
680 sqlite3VdbeResolveLabel(v, iCleanup); |
|
681 } |
|
682 |
|
683 if( !triggers_exist && !IsVirtual(pTab) ){ |
|
684 /* Close all tables opened */ |
|
685 sqlite3VdbeAddOp(v, OP_Close, base, 0); |
|
686 for(idx=1, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, idx++){ |
|
687 sqlite3VdbeAddOp(v, OP_Close, idx+base, 0); |
|
688 } |
|
689 } |
|
690 |
|
691 #ifndef SQLITE_OMIT_AUTOINCREMENT |
|
692 /* Update the sqlite_sequence table by storing the content of the |
|
693 ** counter value in memory counterMem back into the sqlite_sequence |
|
694 ** table. |
|
695 */ |
|
696 if( pTab->autoInc ){ |
|
697 int iCur = pParse->nTab; |
|
698 int addr = sqlite3VdbeCurrentAddr(v); |
|
699 sqlite3OpenTable(pParse, iCur, iDb, pDb->pSchema->pSeqTab, OP_OpenWrite); |
|
700 sqlite3VdbeAddOp(v, OP_MemLoad, counterRowid, 0); |
|
701 sqlite3VdbeAddOp(v, OP_NotNull, -1, addr+7); |
|
702 sqlite3VdbeAddOp(v, OP_Pop, 1, 0); |
|
703 sqlite3VdbeAddOp(v, OP_NewRowid, iCur, 0); |
|
704 sqlite3VdbeOp3(v, OP_String8, 0, 0, pTab->zName, 0); |
|
705 sqlite3VdbeAddOp(v, OP_MemLoad, counterMem, 0); |
|
706 sqlite3VdbeAddOp(v, OP_MakeRecord, 2, 0); |
|
707 sqlite3VdbeAddOp(v, OP_Insert, iCur, 0); |
|
708 sqlite3VdbeAddOp(v, OP_Close, iCur, 0); |
|
709 } |
|
710 #endif |
|
711 |
|
712 /* |
|
713 ** Return the number of rows inserted. If this routine is |
|
714 ** generating code because of a call to sqlite3NestedParse(), do not |
|
715 ** invoke the callback function. |
|
716 */ |
|
717 if( db->flags & SQLITE_CountRows && pParse->nested==0 && !pParse->trigStack ){ |
|
718 sqlite3VdbeAddOp(v, OP_MemLoad, iCntMem, 0); |
|
719 sqlite3VdbeAddOp(v, OP_Callback, 1, 0); |
|
720 sqlite3VdbeSetNumCols(v, 1); |
|
721 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "rows inserted", P3_STATIC); |
|
722 } |
|
723 |
|
724 insert_cleanup: |
|
725 sqlite3SrcListDelete(pTabList); |
|
726 sqlite3ExprListDelete(pList); |
|
727 sqlite3SelectDelete(pSelect); |
|
728 sqlite3IdListDelete(pColumn); |
|
729 } |
|
730 |
|
731 /* |
|
732 ** Generate code to do a constraint check prior to an INSERT or an UPDATE. |
|
733 ** |
|
734 ** When this routine is called, the stack contains (from bottom to top) |
|
735 ** the following values: |
|
736 ** |
|
737 ** 1. The rowid of the row to be updated before the update. This |
|
738 ** value is omitted unless we are doing an UPDATE that involves a |
|
739 ** change to the record number. |
|
740 ** |
|
741 ** 2. The rowid of the row after the update. |
|
742 ** |
|
743 ** 3. The data in the first column of the entry after the update. |
|
744 ** |
|
745 ** i. Data from middle columns... |
|
746 ** |
|
747 ** N. The data in the last column of the entry after the update. |
|
748 ** |
|
749 ** The old rowid shown as entry (1) above is omitted unless both isUpdate |
|
750 ** and rowidChng are 1. isUpdate is true for UPDATEs and false for |
|
751 ** INSERTs and rowidChng is true if the record number is being changed. |
|
752 ** |
|
753 ** The code generated by this routine pushes additional entries onto |
|
754 ** the stack which are the keys for new index entries for the new record. |
|
755 ** The order of index keys is the same as the order of the indices on |
|
756 ** the pTable->pIndex list. A key is only created for index i if |
|
757 ** aIdxUsed!=0 and aIdxUsed[i]!=0. |
|
758 ** |
|
759 ** This routine also generates code to check constraints. NOT NULL, |
|
760 ** CHECK, and UNIQUE constraints are all checked. If a constraint fails, |
|
761 ** then the appropriate action is performed. There are five possible |
|
762 ** actions: ROLLBACK, ABORT, FAIL, REPLACE, and IGNORE. |
|
763 ** |
|
764 ** Constraint type Action What Happens |
|
765 ** --------------- ---------- ---------------------------------------- |
|
766 ** any ROLLBACK The current transaction is rolled back and |
|
767 ** sqlite3_exec() returns immediately with a |
|
768 ** return code of SQLITE_CONSTRAINT. |
|
769 ** |
|
770 ** any ABORT Back out changes from the current command |
|
771 ** only (do not do a complete rollback) then |
|
772 ** cause sqlite3_exec() to return immediately |
|
773 ** with SQLITE_CONSTRAINT. |
|
774 ** |
|
775 ** any FAIL Sqlite_exec() returns immediately with a |
|
776 ** return code of SQLITE_CONSTRAINT. The |
|
777 ** transaction is not rolled back and any |
|
778 ** prior changes are retained. |
|
779 ** |
|
780 ** any IGNORE The record number and data is popped from |
|
781 ** the stack and there is an immediate jump |
|
782 ** to label ignoreDest. |
|
783 ** |
|
784 ** NOT NULL REPLACE The NULL value is replace by the default |
|
785 ** value for that column. If the default value |
|
786 ** is NULL, the action is the same as ABORT. |
|
787 ** |
|
788 ** UNIQUE REPLACE The other row that conflicts with the row |
|
789 ** being inserted is removed. |
|
790 ** |
|
791 ** CHECK REPLACE Illegal. The results in an exception. |
|
792 ** |
|
793 ** Which action to take is determined by the overrideError parameter. |
|
794 ** Or if overrideError==OE_Default, then the pParse->onError parameter |
|
795 ** is used. Or if pParse->onError==OE_Default then the onError value |
|
796 ** for the constraint is used. |
|
797 ** |
|
798 ** The calling routine must open a read/write cursor for pTab with |
|
799 ** cursor number "base". All indices of pTab must also have open |
|
800 ** read/write cursors with cursor number base+i for the i-th cursor. |
|
801 ** Except, if there is no possibility of a REPLACE action then |
|
802 ** cursors do not need to be open for indices where aIdxUsed[i]==0. |
|
803 ** |
|
804 ** If the isUpdate flag is true, it means that the "base" cursor is |
|
805 ** initially pointing to an entry that is being updated. The isUpdate |
|
806 ** flag causes extra code to be generated so that the "base" cursor |
|
807 ** is still pointing at the same entry after the routine returns. |
|
808 ** Without the isUpdate flag, the "base" cursor might be moved. |
|
809 */ |
|
810 void sqlite3GenerateConstraintChecks( |
|
811 Parse *pParse, /* The parser context */ |
|
812 Table *pTab, /* the table into which we are inserting */ |
|
813 int base, /* Index of a read/write cursor pointing at pTab */ |
|
814 char *aIdxUsed, /* Which indices are used. NULL means all are used */ |
|
815 int rowidChng, /* True if the record number will change */ |
|
816 int isUpdate, /* True for UPDATE, False for INSERT */ |
|
817 int overrideError, /* Override onError to this if not OE_Default */ |
|
818 int ignoreDest /* Jump to this label on an OE_Ignore resolution */ |
|
819 ){ |
|
820 int i; |
|
821 Vdbe *v; |
|
822 int nCol; |
|
823 int onError; |
|
824 int addr; |
|
825 int extra; |
|
826 int iCur; |
|
827 Index *pIdx; |
|
828 int seenReplace = 0; |
|
829 int jumpInst1=0, jumpInst2; |
|
830 int hasTwoRowids = (isUpdate && rowidChng); |
|
831 |
|
832 v = sqlite3GetVdbe(pParse); |
|
833 assert( v!=0 ); |
|
834 assert( pTab->pSelect==0 ); /* This table is not a VIEW */ |
|
835 nCol = pTab->nCol; |
|
836 |
|
837 /* Test all NOT NULL constraints. |
|
838 */ |
|
839 for(i=0; i<nCol; i++){ |
|
840 if( i==pTab->iPKey ){ |
|
841 continue; |
|
842 } |
|
843 onError = pTab->aCol[i].notNull; |
|
844 if( onError==OE_None ) continue; |
|
845 if( overrideError!=OE_Default ){ |
|
846 onError = overrideError; |
|
847 }else if( onError==OE_Default ){ |
|
848 onError = OE_Abort; |
|
849 } |
|
850 if( onError==OE_Replace && pTab->aCol[i].pDflt==0 ){ |
|
851 onError = OE_Abort; |
|
852 } |
|
853 sqlite3VdbeAddOp(v, OP_Dup, nCol-1-i, 1); |
|
854 addr = sqlite3VdbeAddOp(v, OP_NotNull, 1, 0); |
|
855 assert( onError==OE_Rollback || onError==OE_Abort || onError==OE_Fail |
|
856 || onError==OE_Ignore || onError==OE_Replace ); |
|
857 switch( onError ){ |
|
858 case OE_Rollback: |
|
859 case OE_Abort: |
|
860 case OE_Fail: { |
|
861 char *zMsg = 0; |
|
862 sqlite3VdbeAddOp(v, OP_Halt, SQLITE_CONSTRAINT, onError); |
|
863 sqlite3SetString(&zMsg, pTab->zName, ".", pTab->aCol[i].zName, |
|
864 " may not be NULL", (char*)0); |
|
865 sqlite3VdbeChangeP3(v, -1, zMsg, P3_DYNAMIC); |
|
866 break; |
|
867 } |
|
868 case OE_Ignore: { |
|
869 sqlite3VdbeAddOp(v, OP_Pop, nCol+1+hasTwoRowids, 0); |
|
870 sqlite3VdbeAddOp(v, OP_Goto, 0, ignoreDest); |
|
871 break; |
|
872 } |
|
873 case OE_Replace: { |
|
874 sqlite3ExprCode(pParse, pTab->aCol[i].pDflt); |
|
875 sqlite3VdbeAddOp(v, OP_Push, nCol-i, 0); |
|
876 break; |
|
877 } |
|
878 } |
|
879 sqlite3VdbeJumpHere(v, addr); |
|
880 } |
|
881 |
|
882 /* Test all CHECK constraints |
|
883 */ |
|
884 #ifndef SQLITE_OMIT_CHECK |
|
885 if( pTab->pCheck && (pParse->db->flags & SQLITE_IgnoreChecks)==0 ){ |
|
886 int allOk = sqlite3VdbeMakeLabel(v); |
|
887 assert( pParse->ckOffset==0 ); |
|
888 pParse->ckOffset = nCol; |
|
889 sqlite3ExprIfTrue(pParse, pTab->pCheck, allOk, 1); |
|
890 assert( pParse->ckOffset==nCol ); |
|
891 pParse->ckOffset = 0; |
|
892 onError = overrideError!=OE_Default ? overrideError : OE_Abort; |
|
893 if( onError==OE_Ignore || onError==OE_Replace ){ |
|
894 sqlite3VdbeAddOp(v, OP_Pop, nCol+1+hasTwoRowids, 0); |
|
895 sqlite3VdbeAddOp(v, OP_Goto, 0, ignoreDest); |
|
896 }else{ |
|
897 sqlite3VdbeAddOp(v, OP_Halt, SQLITE_CONSTRAINT, onError); |
|
898 } |
|
899 sqlite3VdbeResolveLabel(v, allOk); |
|
900 } |
|
901 #endif /* !defined(SQLITE_OMIT_CHECK) */ |
|
902 |
|
903 /* If we have an INTEGER PRIMARY KEY, make sure the primary key |
|
904 ** of the new record does not previously exist. Except, if this |
|
905 ** is an UPDATE and the primary key is not changing, that is OK. |
|
906 */ |
|
907 if( rowidChng ){ |
|
908 onError = pTab->keyConf; |
|
909 if( overrideError!=OE_Default ){ |
|
910 onError = overrideError; |
|
911 }else if( onError==OE_Default ){ |
|
912 onError = OE_Abort; |
|
913 } |
|
914 |
|
915 if( isUpdate ){ |
|
916 sqlite3VdbeAddOp(v, OP_Dup, nCol+1, 1); |
|
917 sqlite3VdbeAddOp(v, OP_Dup, nCol+1, 1); |
|
918 jumpInst1 = sqlite3VdbeAddOp(v, OP_Eq, 0, 0); |
|
919 } |
|
920 sqlite3VdbeAddOp(v, OP_Dup, nCol, 1); |
|
921 jumpInst2 = sqlite3VdbeAddOp(v, OP_NotExists, base, 0); |
|
922 switch( onError ){ |
|
923 default: { |
|
924 onError = OE_Abort; |
|
925 /* Fall thru into the next case */ |
|
926 } |
|
927 case OE_Rollback: |
|
928 case OE_Abort: |
|
929 case OE_Fail: { |
|
930 sqlite3VdbeOp3(v, OP_Halt, SQLITE_CONSTRAINT, onError, |
|
931 "PRIMARY KEY must be unique", P3_STATIC); |
|
932 break; |
|
933 } |
|
934 case OE_Replace: { |
|
935 sqlite3GenerateRowIndexDelete(v, pTab, base, 0); |
|
936 if( isUpdate ){ |
|
937 sqlite3VdbeAddOp(v, OP_Dup, nCol+hasTwoRowids, 1); |
|
938 sqlite3VdbeAddOp(v, OP_MoveGe, base, 0); |
|
939 } |
|
940 seenReplace = 1; |
|
941 break; |
|
942 } |
|
943 case OE_Ignore: { |
|
944 assert( seenReplace==0 ); |
|
945 sqlite3VdbeAddOp(v, OP_Pop, nCol+1+hasTwoRowids, 0); |
|
946 sqlite3VdbeAddOp(v, OP_Goto, 0, ignoreDest); |
|
947 break; |
|
948 } |
|
949 } |
|
950 sqlite3VdbeJumpHere(v, jumpInst2); |
|
951 if( isUpdate ){ |
|
952 sqlite3VdbeJumpHere(v, jumpInst1); |
|
953 sqlite3VdbeAddOp(v, OP_Dup, nCol+1, 1); |
|
954 sqlite3VdbeAddOp(v, OP_MoveGe, base, 0); |
|
955 } |
|
956 } |
|
957 |
|
958 /* Test all UNIQUE constraints by creating entries for each UNIQUE |
|
959 ** index and making sure that duplicate entries do not already exist. |
|
960 ** Add the new records to the indices as we go. |
|
961 */ |
|
962 extra = -1; |
|
963 for(iCur=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, iCur++){ |
|
964 if( aIdxUsed && aIdxUsed[iCur]==0 ) continue; /* Skip unused indices */ |
|
965 extra++; |
|
966 |
|
967 /* Create a key for accessing the index entry */ |
|
968 sqlite3VdbeAddOp(v, OP_Dup, nCol+extra, 1); |
|
969 for(i=0; i<pIdx->nColumn; i++){ |
|
970 int idx = pIdx->aiColumn[i]; |
|
971 if( idx==pTab->iPKey ){ |
|
972 sqlite3VdbeAddOp(v, OP_Dup, i+extra+nCol+1, 1); |
|
973 }else{ |
|
974 sqlite3VdbeAddOp(v, OP_Dup, i+extra+nCol-idx, 1); |
|
975 } |
|
976 } |
|
977 jumpInst1 = sqlite3VdbeAddOp(v, OP_MakeIdxRec, pIdx->nColumn, 0); |
|
978 sqlite3IndexAffinityStr(v, pIdx); |
|
979 |
|
980 /* Find out what action to take in case there is an indexing conflict */ |
|
981 onError = pIdx->onError; |
|
982 if( onError==OE_None ) continue; /* pIdx is not a UNIQUE index */ |
|
983 if( overrideError!=OE_Default ){ |
|
984 onError = overrideError; |
|
985 }else if( onError==OE_Default ){ |
|
986 onError = OE_Abort; |
|
987 } |
|
988 if( seenReplace ){ |
|
989 if( onError==OE_Ignore ) onError = OE_Replace; |
|
990 else if( onError==OE_Fail ) onError = OE_Abort; |
|
991 } |
|
992 |
|
993 |
|
994 /* Check to see if the new index entry will be unique */ |
|
995 sqlite3VdbeAddOp(v, OP_Dup, extra+nCol+1+hasTwoRowids, 1); |
|
996 jumpInst2 = sqlite3VdbeAddOp(v, OP_IsUnique, base+iCur+1, 0); |
|
997 |
|
998 /* Generate code that executes if the new index entry is not unique */ |
|
999 assert( onError==OE_Rollback || onError==OE_Abort || onError==OE_Fail |
|
1000 || onError==OE_Ignore || onError==OE_Replace ); |
|
1001 switch( onError ){ |
|
1002 case OE_Rollback: |
|
1003 case OE_Abort: |
|
1004 case OE_Fail: { |
|
1005 int j, n1, n2; |
|
1006 char zErrMsg[200]; |
|
1007 strcpy(zErrMsg, pIdx->nColumn>1 ? "columns " : "column "); |
|
1008 n1 = strlen(zErrMsg); |
|
1009 for(j=0; j<pIdx->nColumn && n1<sizeof(zErrMsg)-30; j++){ |
|
1010 char *zCol = pTab->aCol[pIdx->aiColumn[j]].zName; |
|
1011 n2 = strlen(zCol); |
|
1012 if( j>0 ){ |
|
1013 strcpy(&zErrMsg[n1], ", "); |
|
1014 n1 += 2; |
|
1015 } |
|
1016 if( n1+n2>sizeof(zErrMsg)-30 ){ |
|
1017 strcpy(&zErrMsg[n1], "..."); |
|
1018 n1 += 3; |
|
1019 break; |
|
1020 }else{ |
|
1021 strcpy(&zErrMsg[n1], zCol); |
|
1022 n1 += n2; |
|
1023 } |
|
1024 } |
|
1025 strcpy(&zErrMsg[n1], |
|
1026 pIdx->nColumn>1 ? " are not unique" : " is not unique"); |
|
1027 sqlite3VdbeOp3(v, OP_Halt, SQLITE_CONSTRAINT, onError, zErrMsg, 0); |
|
1028 break; |
|
1029 } |
|
1030 case OE_Ignore: { |
|
1031 assert( seenReplace==0 ); |
|
1032 sqlite3VdbeAddOp(v, OP_Pop, nCol+extra+3+hasTwoRowids, 0); |
|
1033 sqlite3VdbeAddOp(v, OP_Goto, 0, ignoreDest); |
|
1034 break; |
|
1035 } |
|
1036 case OE_Replace: { |
|
1037 sqlite3GenerateRowDelete(pParse->db, v, pTab, base, 0); |
|
1038 if( isUpdate ){ |
|
1039 sqlite3VdbeAddOp(v, OP_Dup, nCol+extra+1+hasTwoRowids, 1); |
|
1040 sqlite3VdbeAddOp(v, OP_MoveGe, base, 0); |
|
1041 } |
|
1042 seenReplace = 1; |
|
1043 break; |
|
1044 } |
|
1045 } |
|
1046 #if NULL_DISTINCT_FOR_UNIQUE |
|
1047 sqlite3VdbeJumpHere(v, jumpInst1); |
|
1048 #endif |
|
1049 sqlite3VdbeJumpHere(v, jumpInst2); |
|
1050 } |
|
1051 } |
|
1052 |
|
1053 /* |
|
1054 ** This routine generates code to finish the INSERT or UPDATE operation |
|
1055 ** that was started by a prior call to sqlite3GenerateConstraintChecks. |
|
1056 ** The stack must contain keys for all active indices followed by data |
|
1057 ** and the rowid for the new entry. This routine creates the new |
|
1058 ** entries in all indices and in the main table. |
|
1059 ** |
|
1060 ** The arguments to this routine should be the same as the first six |
|
1061 ** arguments to sqlite3GenerateConstraintChecks. |
|
1062 */ |
|
1063 void sqlite3CompleteInsertion( |
|
1064 Parse *pParse, /* The parser context */ |
|
1065 Table *pTab, /* the table into which we are inserting */ |
|
1066 int base, /* Index of a read/write cursor pointing at pTab */ |
|
1067 char *aIdxUsed, /* Which indices are used. NULL means all are used */ |
|
1068 int rowidChng, /* True if the record number will change */ |
|
1069 int isUpdate, /* True for UPDATE, False for INSERT */ |
|
1070 int newIdx /* Index of NEW table for triggers. -1 if none */ |
|
1071 ){ |
|
1072 int i; |
|
1073 Vdbe *v; |
|
1074 int nIdx; |
|
1075 Index *pIdx; |
|
1076 int pik_flags; |
|
1077 |
|
1078 v = sqlite3GetVdbe(pParse); |
|
1079 assert( v!=0 ); |
|
1080 assert( pTab->pSelect==0 ); /* This table is not a VIEW */ |
|
1081 for(nIdx=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, nIdx++){} |
|
1082 for(i=nIdx-1; i>=0; i--){ |
|
1083 if( aIdxUsed && aIdxUsed[i]==0 ) continue; |
|
1084 sqlite3VdbeAddOp(v, OP_IdxInsert, base+i+1, 0); |
|
1085 } |
|
1086 sqlite3VdbeAddOp(v, OP_MakeRecord, pTab->nCol, 0); |
|
1087 sqlite3TableAffinityStr(v, pTab); |
|
1088 #ifndef SQLITE_OMIT_TRIGGER |
|
1089 if( newIdx>=0 ){ |
|
1090 sqlite3VdbeAddOp(v, OP_Dup, 1, 0); |
|
1091 sqlite3VdbeAddOp(v, OP_Dup, 1, 0); |
|
1092 sqlite3VdbeAddOp(v, OP_Insert, newIdx, 0); |
|
1093 } |
|
1094 #endif |
|
1095 if( pParse->nested ){ |
|
1096 pik_flags = 0; |
|
1097 }else{ |
|
1098 pik_flags = OPFLAG_NCHANGE; |
|
1099 pik_flags |= (isUpdate?OPFLAG_ISUPDATE:OPFLAG_LASTROWID); |
|
1100 } |
|
1101 sqlite3VdbeAddOp(v, OP_Insert, base, pik_flags); |
|
1102 if( !pParse->nested ){ |
|
1103 sqlite3VdbeChangeP3(v, -1, pTab->zName, P3_STATIC); |
|
1104 } |
|
1105 |
|
1106 if( isUpdate && rowidChng ){ |
|
1107 sqlite3VdbeAddOp(v, OP_Pop, 1, 0); |
|
1108 } |
|
1109 } |
|
1110 |
|
1111 /* |
|
1112 ** Generate code that will open cursors for a table and for all |
|
1113 ** indices of that table. The "base" parameter is the cursor number used |
|
1114 ** for the table. Indices are opened on subsequent cursors. |
|
1115 */ |
|
1116 void sqlite3OpenTableAndIndices( |
|
1117 Parse *pParse, /* Parsing context */ |
|
1118 Table *pTab, /* Table to be opened */ |
|
1119 int base, /* Cursor number assigned to the table */ |
|
1120 int op /* OP_OpenRead or OP_OpenWrite */ |
|
1121 ){ |
|
1122 int i; |
|
1123 int iDb; |
|
1124 Index *pIdx; |
|
1125 Vdbe *v; |
|
1126 |
|
1127 if( IsVirtual(pTab) ) return; |
|
1128 iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema); |
|
1129 v = sqlite3GetVdbe(pParse); |
|
1130 assert( v!=0 ); |
|
1131 sqlite3OpenTable(pParse, base, iDb, pTab, op); |
|
1132 for(i=1, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){ |
|
1133 KeyInfo *pKey = sqlite3IndexKeyinfo(pParse, pIdx); |
|
1134 assert( pIdx->pSchema==pTab->pSchema ); |
|
1135 sqlite3VdbeAddOp(v, OP_Integer, iDb, 0); |
|
1136 VdbeComment((v, "# %s", pIdx->zName)); |
|
1137 sqlite3VdbeOp3(v, op, i+base, pIdx->tnum, (char*)pKey, P3_KEYINFO_HANDOFF); |
|
1138 } |
|
1139 if( pParse->nTab<=base+i ){ |
|
1140 pParse->nTab = base+i; |
|
1141 } |
|
1142 } |