Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(11)

Side by Side Diff: third_party/sqlite/src/src/insert.c

Issue 2751253002: [sql] Import SQLite 3.17.0. (Closed)
Patch Set: Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 /* 1 /*
2 ** 2001 September 15 2 ** 2001 September 15
3 ** 3 **
4 ** The author disclaims copyright to this source code. In place of 4 ** The author disclaims copyright to this source code. In place of
5 ** a legal notice, here is a blessing: 5 ** a legal notice, here is a blessing:
6 ** 6 **
7 ** May you do good and not evil. 7 ** May you do good and not evil.
8 ** May you find forgiveness for yourself and forgive others. 8 ** May you find forgiveness for yourself and forgive others.
9 ** May you share freely, never taking more than you give. 9 ** May you share freely, never taking more than you give.
10 ** 10 **
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after
76 ** a member of the Index structure for subsequent use. 76 ** a member of the Index structure for subsequent use.
77 ** 77 **
78 ** The column affinity string will eventually be deleted by 78 ** The column affinity string will eventually be deleted by
79 ** sqliteDeleteIndex() when the Index structure itself is cleaned 79 ** sqliteDeleteIndex() when the Index structure itself is cleaned
80 ** up. 80 ** up.
81 */ 81 */
82 int n; 82 int n;
83 Table *pTab = pIdx->pTable; 83 Table *pTab = pIdx->pTable;
84 pIdx->zColAff = (char *)sqlite3DbMallocRaw(0, pIdx->nColumn+1); 84 pIdx->zColAff = (char *)sqlite3DbMallocRaw(0, pIdx->nColumn+1);
85 if( !pIdx->zColAff ){ 85 if( !pIdx->zColAff ){
86 db->mallocFailed = 1; 86 sqlite3OomFault(db);
87 return 0; 87 return 0;
88 } 88 }
89 for(n=0; n<pIdx->nColumn; n++){ 89 for(n=0; n<pIdx->nColumn; n++){
90 i16 x = pIdx->aiColumn[n]; 90 i16 x = pIdx->aiColumn[n];
91 if( x>=0 ){ 91 if( x>=0 ){
92 pIdx->zColAff[n] = pTab->aCol[x].affinity; 92 pIdx->zColAff[n] = pTab->aCol[x].affinity;
93 }else if( x==XN_ROWID ){ 93 }else if( x==XN_ROWID ){
94 pIdx->zColAff[n] = SQLITE_AFF_INTEGER; 94 pIdx->zColAff[n] = SQLITE_AFF_INTEGER;
95 }else{ 95 }else{
96 char aff; 96 char aff;
(...skipping 30 matching lines...) Expand all
127 ** 'D' INTEGER 127 ** 'D' INTEGER
128 ** 'E' REAL 128 ** 'E' REAL
129 */ 129 */
130 void sqlite3TableAffinity(Vdbe *v, Table *pTab, int iReg){ 130 void sqlite3TableAffinity(Vdbe *v, Table *pTab, int iReg){
131 int i; 131 int i;
132 char *zColAff = pTab->zColAff; 132 char *zColAff = pTab->zColAff;
133 if( zColAff==0 ){ 133 if( zColAff==0 ){
134 sqlite3 *db = sqlite3VdbeDb(v); 134 sqlite3 *db = sqlite3VdbeDb(v);
135 zColAff = (char *)sqlite3DbMallocRaw(0, pTab->nCol+1); 135 zColAff = (char *)sqlite3DbMallocRaw(0, pTab->nCol+1);
136 if( !zColAff ){ 136 if( !zColAff ){
137 db->mallocFailed = 1; 137 sqlite3OomFault(db);
138 return; 138 return;
139 } 139 }
140 140
141 for(i=0; i<pTab->nCol; i++){ 141 for(i=0; i<pTab->nCol; i++){
142 zColAff[i] = pTab->aCol[i].affinity; 142 zColAff[i] = pTab->aCol[i].affinity;
143 } 143 }
144 do{ 144 do{
145 zColAff[i--] = 0; 145 zColAff[i--] = 0;
146 }while( i>=0 && zColAff[i]==SQLITE_AFF_BLOB ); 146 }while( i>=0 && zColAff[i]==SQLITE_AFF_BLOB );
147 pTab->zColAff = zColAff; 147 pTab->zColAff = zColAff;
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
193 } 193 }
194 #endif 194 #endif
195 } 195 }
196 return 0; 196 return 0;
197 } 197 }
198 198
199 #ifndef SQLITE_OMIT_AUTOINCREMENT 199 #ifndef SQLITE_OMIT_AUTOINCREMENT
200 /* 200 /*
201 ** Locate or create an AutoincInfo structure associated with table pTab 201 ** Locate or create an AutoincInfo structure associated with table pTab
202 ** which is in database iDb. Return the register number for the register 202 ** which is in database iDb. Return the register number for the register
203 ** that holds the maximum rowid. 203 ** that holds the maximum rowid. Return zero if pTab is not an AUTOINCREMENT
204 ** table. (Also return zero when doing a VACUUM since we do not want to
205 ** update the AUTOINCREMENT counters during a VACUUM.)
204 ** 206 **
205 ** There is at most one AutoincInfo structure per table even if the 207 ** There is at most one AutoincInfo structure per table even if the
206 ** same table is autoincremented multiple times due to inserts within 208 ** same table is autoincremented multiple times due to inserts within
207 ** triggers. A new AutoincInfo structure is created if this is the 209 ** triggers. A new AutoincInfo structure is created if this is the
208 ** first use of table pTab. On 2nd and subsequent uses, the original 210 ** first use of table pTab. On 2nd and subsequent uses, the original
209 ** AutoincInfo structure is used. 211 ** AutoincInfo structure is used.
210 ** 212 **
211 ** Three memory locations are allocated: 213 ** Three memory locations are allocated:
212 ** 214 **
213 ** (1) Register to hold the name of the pTab table. 215 ** (1) Register to hold the name of the pTab table.
214 ** (2) Register to hold the maximum ROWID of pTab. 216 ** (2) Register to hold the maximum ROWID of pTab.
215 ** (3) Register to hold the rowid in sqlite_sequence of pTab 217 ** (3) Register to hold the rowid in sqlite_sequence of pTab
216 ** 218 **
217 ** The 2nd register is the one that is returned. That is all the 219 ** The 2nd register is the one that is returned. That is all the
218 ** insert routine needs to know about. 220 ** insert routine needs to know about.
219 */ 221 */
220 static int autoIncBegin( 222 static int autoIncBegin(
221 Parse *pParse, /* Parsing context */ 223 Parse *pParse, /* Parsing context */
222 int iDb, /* Index of the database holding pTab */ 224 int iDb, /* Index of the database holding pTab */
223 Table *pTab /* The table we are writing to */ 225 Table *pTab /* The table we are writing to */
224 ){ 226 ){
225 int memId = 0; /* Register holding maximum rowid */ 227 int memId = 0; /* Register holding maximum rowid */
226 if( pTab->tabFlags & TF_Autoincrement ){ 228 if( (pTab->tabFlags & TF_Autoincrement)!=0
229 && (pParse->db->flags & SQLITE_Vacuum)==0
230 ){
227 Parse *pToplevel = sqlite3ParseToplevel(pParse); 231 Parse *pToplevel = sqlite3ParseToplevel(pParse);
228 AutoincInfo *pInfo; 232 AutoincInfo *pInfo;
229 233
230 pInfo = pToplevel->pAinc; 234 pInfo = pToplevel->pAinc;
231 while( pInfo && pInfo->pTab!=pTab ){ pInfo = pInfo->pNext; } 235 while( pInfo && pInfo->pTab!=pTab ){ pInfo = pInfo->pNext; }
232 if( pInfo==0 ){ 236 if( pInfo==0 ){
233 pInfo = sqlite3DbMallocRaw(pParse->db, sizeof(*pInfo)); 237 pInfo = sqlite3DbMallocRawNN(pParse->db, sizeof(*pInfo));
234 if( pInfo==0 ) return 0; 238 if( pInfo==0 ) return 0;
235 pInfo->pNext = pToplevel->pAinc; 239 pInfo->pNext = pToplevel->pAinc;
236 pToplevel->pAinc = pInfo; 240 pToplevel->pAinc = pInfo;
237 pInfo->pTab = pTab; 241 pInfo->pTab = pTab;
238 pInfo->iDb = iDb; 242 pInfo->iDb = iDb;
239 pToplevel->nMem++; /* Register to hold name of table */ 243 pToplevel->nMem++; /* Register to hold name of table */
240 pInfo->regCtr = ++pToplevel->nMem; /* Max rowid register */ 244 pInfo->regCtr = ++pToplevel->nMem; /* Max rowid register */
241 pToplevel->nMem++; /* Rowid in sqlite_sequence */ 245 pToplevel->nMem++; /* Rowid in sqlite_sequence */
242 } 246 }
243 memId = pInfo->regCtr; 247 memId = pInfo->regCtr;
244 } 248 }
245 return memId; 249 return memId;
246 } 250 }
247 251
248 /* 252 /*
249 ** This routine generates code that will initialize all of the 253 ** This routine generates code that will initialize all of the
250 ** register used by the autoincrement tracker. 254 ** register used by the autoincrement tracker.
251 */ 255 */
252 void sqlite3AutoincrementBegin(Parse *pParse){ 256 void sqlite3AutoincrementBegin(Parse *pParse){
253 AutoincInfo *p; /* Information about an AUTOINCREMENT */ 257 AutoincInfo *p; /* Information about an AUTOINCREMENT */
254 sqlite3 *db = pParse->db; /* The database connection */ 258 sqlite3 *db = pParse->db; /* The database connection */
255 Db *pDb; /* Database only autoinc table */ 259 Db *pDb; /* Database only autoinc table */
256 int memId; /* Register holding max rowid */ 260 int memId; /* Register holding max rowid */
257 int addr; /* A VDBE address */
258 Vdbe *v = pParse->pVdbe; /* VDBE under construction */ 261 Vdbe *v = pParse->pVdbe; /* VDBE under construction */
259 262
260 /* This routine is never called during trigger-generation. It is 263 /* This routine is never called during trigger-generation. It is
261 ** only called from the top-level */ 264 ** only called from the top-level */
262 assert( pParse->pTriggerTab==0 ); 265 assert( pParse->pTriggerTab==0 );
263 assert( sqlite3IsToplevel(pParse) ); 266 assert( sqlite3IsToplevel(pParse) );
264 267
265 assert( v ); /* We failed long ago if this is not so */ 268 assert( v ); /* We failed long ago if this is not so */
266 for(p = pParse->pAinc; p; p = p->pNext){ 269 for(p = pParse->pAinc; p; p = p->pNext){
270 static const int iLn = VDBE_OFFSET_LINENO(2);
271 static const VdbeOpList autoInc[] = {
272 /* 0 */ {OP_Null, 0, 0, 0},
273 /* 1 */ {OP_Rewind, 0, 9, 0},
274 /* 2 */ {OP_Column, 0, 0, 0},
275 /* 3 */ {OP_Ne, 0, 7, 0},
276 /* 4 */ {OP_Rowid, 0, 0, 0},
277 /* 5 */ {OP_Column, 0, 1, 0},
278 /* 6 */ {OP_Goto, 0, 9, 0},
279 /* 7 */ {OP_Next, 0, 2, 0},
280 /* 8 */ {OP_Integer, 0, 0, 0},
281 /* 9 */ {OP_Close, 0, 0, 0}
282 };
283 VdbeOp *aOp;
267 pDb = &db->aDb[p->iDb]; 284 pDb = &db->aDb[p->iDb];
268 memId = p->regCtr; 285 memId = p->regCtr;
269 assert( sqlite3SchemaMutexHeld(db, 0, pDb->pSchema) ); 286 assert( sqlite3SchemaMutexHeld(db, 0, pDb->pSchema) );
270 sqlite3OpenTable(pParse, 0, p->iDb, pDb->pSchema->pSeqTab, OP_OpenRead); 287 sqlite3OpenTable(pParse, 0, p->iDb, pDb->pSchema->pSeqTab, OP_OpenRead);
271 sqlite3VdbeAddOp3(v, OP_Null, 0, memId, memId+1);
272 addr = sqlite3VdbeCurrentAddr(v);
273 sqlite3VdbeLoadString(v, memId-1, p->pTab->zName); 288 sqlite3VdbeLoadString(v, memId-1, p->pTab->zName);
274 sqlite3VdbeAddOp2(v, OP_Rewind, 0, addr+9); VdbeCoverage(v); 289 aOp = sqlite3VdbeAddOpList(v, ArraySize(autoInc), autoInc, iLn);
275 sqlite3VdbeAddOp3(v, OP_Column, 0, 0, memId); 290 if( aOp==0 ) break;
276 sqlite3VdbeAddOp3(v, OP_Ne, memId-1, addr+7, memId); VdbeCoverage(v); 291 aOp[0].p2 = memId;
277 sqlite3VdbeChangeP5(v, SQLITE_JUMPIFNULL); 292 aOp[0].p3 = memId+1;
278 sqlite3VdbeAddOp2(v, OP_Rowid, 0, memId+1); 293 aOp[2].p3 = memId;
279 sqlite3VdbeAddOp3(v, OP_Column, 0, 1, memId); 294 aOp[3].p1 = memId-1;
280 sqlite3VdbeGoto(v, addr+9); 295 aOp[3].p3 = memId;
281 sqlite3VdbeAddOp2(v, OP_Next, 0, addr+2); VdbeCoverage(v); 296 aOp[3].p5 = SQLITE_JUMPIFNULL;
282 sqlite3VdbeAddOp2(v, OP_Integer, 0, memId); 297 aOp[4].p2 = memId+1;
283 sqlite3VdbeAddOp0(v, OP_Close); 298 aOp[5].p3 = memId;
299 aOp[8].p2 = memId;
284 } 300 }
285 } 301 }
286 302
287 /* 303 /*
288 ** Update the maximum rowid for an autoincrement calculation. 304 ** Update the maximum rowid for an autoincrement calculation.
289 ** 305 **
290 ** This routine should be called when the top of the stack holds a 306 ** This routine should be called when the regRowid register holds a
291 ** new rowid that is about to be inserted. If that new rowid is 307 ** new rowid that is about to be inserted. If that new rowid is
292 ** larger than the maximum rowid in the memId memory cell, then the 308 ** larger than the maximum rowid in the memId memory cell, then the
293 ** memory cell is updated. The stack is unchanged. 309 ** memory cell is updated.
294 */ 310 */
295 static void autoIncStep(Parse *pParse, int memId, int regRowid){ 311 static void autoIncStep(Parse *pParse, int memId, int regRowid){
296 if( memId>0 ){ 312 if( memId>0 ){
297 sqlite3VdbeAddOp2(pParse->pVdbe, OP_MemMax, memId, regRowid); 313 sqlite3VdbeAddOp2(pParse->pVdbe, OP_MemMax, memId, regRowid);
298 } 314 }
299 } 315 }
300 316
301 /* 317 /*
302 ** This routine generates the code needed to write autoincrement 318 ** This routine generates the code needed to write autoincrement
303 ** maximum rowid values back into the sqlite_sequence register. 319 ** maximum rowid values back into the sqlite_sequence register.
304 ** Every statement that might do an INSERT into an autoincrement 320 ** Every statement that might do an INSERT into an autoincrement
305 ** table (either directly or through triggers) needs to call this 321 ** table (either directly or through triggers) needs to call this
306 ** routine just before the "exit" code. 322 ** routine just before the "exit" code.
307 */ 323 */
308 void sqlite3AutoincrementEnd(Parse *pParse){ 324 static SQLITE_NOINLINE void autoIncrementEnd(Parse *pParse){
309 AutoincInfo *p; 325 AutoincInfo *p;
310 Vdbe *v = pParse->pVdbe; 326 Vdbe *v = pParse->pVdbe;
311 sqlite3 *db = pParse->db; 327 sqlite3 *db = pParse->db;
312 328
313 assert( v ); 329 assert( v );
314 for(p = pParse->pAinc; p; p = p->pNext){ 330 for(p = pParse->pAinc; p; p = p->pNext){
331 static const int iLn = VDBE_OFFSET_LINENO(2);
332 static const VdbeOpList autoIncEnd[] = {
333 /* 0 */ {OP_NotNull, 0, 2, 0},
334 /* 1 */ {OP_NewRowid, 0, 0, 0},
335 /* 2 */ {OP_MakeRecord, 0, 2, 0},
336 /* 3 */ {OP_Insert, 0, 0, 0},
337 /* 4 */ {OP_Close, 0, 0, 0}
338 };
339 VdbeOp *aOp;
315 Db *pDb = &db->aDb[p->iDb]; 340 Db *pDb = &db->aDb[p->iDb];
316 int addr1;
317 int iRec; 341 int iRec;
318 int memId = p->regCtr; 342 int memId = p->regCtr;
319 343
320 iRec = sqlite3GetTempReg(pParse); 344 iRec = sqlite3GetTempReg(pParse);
321 assert( sqlite3SchemaMutexHeld(db, 0, pDb->pSchema) ); 345 assert( sqlite3SchemaMutexHeld(db, 0, pDb->pSchema) );
322 sqlite3OpenTable(pParse, 0, p->iDb, pDb->pSchema->pSeqTab, OP_OpenWrite); 346 sqlite3OpenTable(pParse, 0, p->iDb, pDb->pSchema->pSeqTab, OP_OpenWrite);
323 addr1 = sqlite3VdbeAddOp1(v, OP_NotNull, memId+1); VdbeCoverage(v); 347 aOp = sqlite3VdbeAddOpList(v, ArraySize(autoIncEnd), autoIncEnd, iLn);
324 sqlite3VdbeAddOp2(v, OP_NewRowid, 0, memId+1); 348 if( aOp==0 ) break;
325 sqlite3VdbeJumpHere(v, addr1); 349 aOp[0].p1 = memId+1;
326 sqlite3VdbeAddOp3(v, OP_MakeRecord, memId-1, 2, iRec); 350 aOp[1].p2 = memId+1;
327 sqlite3VdbeAddOp3(v, OP_Insert, 0, iRec, memId+1); 351 aOp[2].p1 = memId-1;
328 sqlite3VdbeChangeP5(v, OPFLAG_APPEND); 352 aOp[2].p3 = iRec;
329 sqlite3VdbeAddOp0(v, OP_Close); 353 aOp[3].p2 = iRec;
354 aOp[3].p3 = memId+1;
355 aOp[3].p5 = OPFLAG_APPEND;
330 sqlite3ReleaseTempReg(pParse, iRec); 356 sqlite3ReleaseTempReg(pParse, iRec);
331 } 357 }
332 } 358 }
359 void sqlite3AutoincrementEnd(Parse *pParse){
360 if( pParse->pAinc ) autoIncrementEnd(pParse);
361 }
333 #else 362 #else
334 /* 363 /*
335 ** If SQLITE_OMIT_AUTOINCREMENT is defined, then the three routines 364 ** If SQLITE_OMIT_AUTOINCREMENT is defined, then the three routines
336 ** above are all no-ops 365 ** above are all no-ops
337 */ 366 */
338 # define autoIncBegin(A,B,C) (0) 367 # define autoIncBegin(A,B,C) (0)
339 # define autoIncStep(A,B,C) 368 # define autoIncStep(A,B,C)
340 #endif /* SQLITE_OMIT_AUTOINCREMENT */ 369 #endif /* SQLITE_OMIT_AUTOINCREMENT */
341 370
342 371
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after
449 void sqlite3Insert( 478 void sqlite3Insert(
450 Parse *pParse, /* Parser context */ 479 Parse *pParse, /* Parser context */
451 SrcList *pTabList, /* Name of table into which we are inserting */ 480 SrcList *pTabList, /* Name of table into which we are inserting */
452 Select *pSelect, /* A SELECT statement to use as the data source */ 481 Select *pSelect, /* A SELECT statement to use as the data source */
453 IdList *pColumn, /* Column names corresponding to IDLIST. */ 482 IdList *pColumn, /* Column names corresponding to IDLIST. */
454 int onError /* How to handle constraint errors */ 483 int onError /* How to handle constraint errors */
455 ){ 484 ){
456 sqlite3 *db; /* The main database structure */ 485 sqlite3 *db; /* The main database structure */
457 Table *pTab; /* The table to insert into. aka TABLE */ 486 Table *pTab; /* The table to insert into. aka TABLE */
458 char *zTab; /* Name of the table into which we are inserting */ 487 char *zTab; /* Name of the table into which we are inserting */
459 const char *zDb; /* Name of the database holding this table */ 488 int i, j; /* Loop counters */
460 int i, j, idx; /* Loop counters */
461 Vdbe *v; /* Generate code into this virtual machine */ 489 Vdbe *v; /* Generate code into this virtual machine */
462 Index *pIdx; /* For looping over indices of the table */ 490 Index *pIdx; /* For looping over indices of the table */
463 int nColumn; /* Number of columns in the data */ 491 int nColumn; /* Number of columns in the data */
464 int nHidden = 0; /* Number of hidden columns if TABLE is virtual */ 492 int nHidden = 0; /* Number of hidden columns if TABLE is virtual */
465 int iDataCur = 0; /* VDBE cursor that is the main data repository */ 493 int iDataCur = 0; /* VDBE cursor that is the main data repository */
466 int iIdxCur = 0; /* First index cursor */ 494 int iIdxCur = 0; /* First index cursor */
467 int ipkColumn = -1; /* Column that is the INTEGER PRIMARY KEY */ 495 int ipkColumn = -1; /* Column that is the INTEGER PRIMARY KEY */
468 int endOfLoop; /* Label for the end of the insertion loop */ 496 int endOfLoop; /* Label for the end of the insertion loop */
469 int srcTab = 0; /* Data comes from this temporary cursor if >=0 */ 497 int srcTab = 0; /* Data comes from this temporary cursor if >=0 */
470 int addrInsTop = 0; /* Jump to label "D" */ 498 int addrInsTop = 0; /* Jump to label "D" */
471 int addrCont = 0; /* Top of insert loop. Label "C" in templates 3 and 4 */ 499 int addrCont = 0; /* Top of insert loop. Label "C" in templates 3 and 4 */
472 SelectDest dest; /* Destination for SELECT on rhs of INSERT */ 500 SelectDest dest; /* Destination for SELECT on rhs of INSERT */
473 int iDb; /* Index of database holding TABLE */ 501 int iDb; /* Index of database holding TABLE */
474 Db *pDb; /* The database containing table being inserted into */
475 u8 useTempTable = 0; /* Store SELECT results in intermediate table */ 502 u8 useTempTable = 0; /* Store SELECT results in intermediate table */
476 u8 appendFlag = 0; /* True if the insert is likely to be an append */ 503 u8 appendFlag = 0; /* True if the insert is likely to be an append */
477 u8 withoutRowid; /* 0 for normal table. 1 for WITHOUT ROWID table */ 504 u8 withoutRowid; /* 0 for normal table. 1 for WITHOUT ROWID table */
478 u8 bIdListInOrder; /* True if IDLIST is in table order */ 505 u8 bIdListInOrder; /* True if IDLIST is in table order */
479 ExprList *pList = 0; /* List of VALUES() to be inserted */ 506 ExprList *pList = 0; /* List of VALUES() to be inserted */
480 507
481 /* Register allocations */ 508 /* Register allocations */
482 int regFromSelect = 0;/* Base register for data coming from SELECT */ 509 int regFromSelect = 0;/* Base register for data coming from SELECT */
483 int regAutoinc = 0; /* Register holding the AUTOINCREMENT counter */ 510 int regAutoinc = 0; /* Register holding the AUTOINCREMENT counter */
484 int regRowCount = 0; /* Memory cell used for the row counter */ 511 int regRowCount = 0; /* Memory cell used for the row counter */
(...skipping 29 matching lines...) Expand all
514 */ 541 */
515 assert( pTabList->nSrc==1 ); 542 assert( pTabList->nSrc==1 );
516 zTab = pTabList->a[0].zName; 543 zTab = pTabList->a[0].zName;
517 if( NEVER(zTab==0) ) goto insert_cleanup; 544 if( NEVER(zTab==0) ) goto insert_cleanup;
518 pTab = sqlite3SrcListLookup(pParse, pTabList); 545 pTab = sqlite3SrcListLookup(pParse, pTabList);
519 if( pTab==0 ){ 546 if( pTab==0 ){
520 goto insert_cleanup; 547 goto insert_cleanup;
521 } 548 }
522 iDb = sqlite3SchemaToIndex(db, pTab->pSchema); 549 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
523 assert( iDb<db->nDb ); 550 assert( iDb<db->nDb );
524 pDb = &db->aDb[iDb]; 551 if( sqlite3AuthCheck(pParse, SQLITE_INSERT, pTab->zName, 0,
525 zDb = pDb->zName; 552 db->aDb[iDb].zDbSName) ){
526 if( sqlite3AuthCheck(pParse, SQLITE_INSERT, pTab->zName, 0, zDb) ){
527 goto insert_cleanup; 553 goto insert_cleanup;
528 } 554 }
529 withoutRowid = !HasRowid(pTab); 555 withoutRowid = !HasRowid(pTab);
530 556
531 /* Figure out if we have any triggers and if the table being 557 /* Figure out if we have any triggers and if the table being
532 ** inserted into is a view 558 ** inserted into is a view
533 */ 559 */
534 #ifndef SQLITE_OMIT_TRIGGER 560 #ifndef SQLITE_OMIT_TRIGGER
535 pTrigger = sqlite3TriggersExist(pParse, pTab, TK_INSERT, 0, &tmask); 561 pTrigger = sqlite3TriggersExist(pParse, pTab, TK_INSERT, 0, &tmask);
536 isView = pTab->pSelect!=0; 562 isView = pTab->pSelect!=0;
(...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after
653 679
654 regYield = ++pParse->nMem; 680 regYield = ++pParse->nMem;
655 addrTop = sqlite3VdbeCurrentAddr(v) + 1; 681 addrTop = sqlite3VdbeCurrentAddr(v) + 1;
656 sqlite3VdbeAddOp3(v, OP_InitCoroutine, regYield, 0, addrTop); 682 sqlite3VdbeAddOp3(v, OP_InitCoroutine, regYield, 0, addrTop);
657 sqlite3SelectDestInit(&dest, SRT_Coroutine, regYield); 683 sqlite3SelectDestInit(&dest, SRT_Coroutine, regYield);
658 dest.iSdst = bIdListInOrder ? regData : 0; 684 dest.iSdst = bIdListInOrder ? regData : 0;
659 dest.nSdst = pTab->nCol; 685 dest.nSdst = pTab->nCol;
660 rc = sqlite3Select(pParse, pSelect, &dest); 686 rc = sqlite3Select(pParse, pSelect, &dest);
661 regFromSelect = dest.iSdst; 687 regFromSelect = dest.iSdst;
662 if( rc || db->mallocFailed || pParse->nErr ) goto insert_cleanup; 688 if( rc || db->mallocFailed || pParse->nErr ) goto insert_cleanup;
663 sqlite3VdbeAddOp1(v, OP_EndCoroutine, regYield); 689 sqlite3VdbeEndCoroutine(v, regYield);
664 sqlite3VdbeJumpHere(v, addrTop - 1); /* label B: */ 690 sqlite3VdbeJumpHere(v, addrTop - 1); /* label B: */
665 assert( pSelect->pEList ); 691 assert( pSelect->pEList );
666 nColumn = pSelect->pEList->nExpr; 692 nColumn = pSelect->pEList->nExpr;
667 693
668 /* Set useTempTable to TRUE if the result of the SELECT statement 694 /* Set useTempTable to TRUE if the result of the SELECT statement
669 ** should be written into a temporary table (template 4). Set to 695 ** should be written into a temporary table (template 4). Set to
670 ** FALSE if each output row of the SELECT can be written directly into 696 ** FALSE if each output row of the SELECT can be written directly into
671 ** the destination table (template 3). 697 ** the destination table (template 3).
672 ** 698 **
673 ** A temp table must be used if the table being updated is also one 699 ** A temp table must be used if the table being updated is also one
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after
755 if( db->flags & SQLITE_CountRows ){ 781 if( db->flags & SQLITE_CountRows ){
756 regRowCount = ++pParse->nMem; 782 regRowCount = ++pParse->nMem;
757 sqlite3VdbeAddOp2(v, OP_Integer, 0, regRowCount); 783 sqlite3VdbeAddOp2(v, OP_Integer, 0, regRowCount);
758 } 784 }
759 785
760 /* If this is not a view, open the table and and all indices */ 786 /* If this is not a view, open the table and and all indices */
761 if( !isView ){ 787 if( !isView ){
762 int nIdx; 788 int nIdx;
763 nIdx = sqlite3OpenTableAndIndices(pParse, pTab, OP_OpenWrite, 0, -1, 0, 789 nIdx = sqlite3OpenTableAndIndices(pParse, pTab, OP_OpenWrite, 0, -1, 0,
764 &iDataCur, &iIdxCur); 790 &iDataCur, &iIdxCur);
765 aRegIdx = sqlite3DbMallocRaw(db, sizeof(int)*(nIdx+1)); 791 aRegIdx = sqlite3DbMallocRawNN(db, sizeof(int)*(nIdx+1));
766 if( aRegIdx==0 ){ 792 if( aRegIdx==0 ){
767 goto insert_cleanup; 793 goto insert_cleanup;
768 } 794 }
769 for(i=0; i<nIdx; i++){ 795 for(i=0, pIdx=pTab->pIndex; i<nIdx; pIdx=pIdx->pNext, i++){
796 assert( pIdx );
770 aRegIdx[i] = ++pParse->nMem; 797 aRegIdx[i] = ++pParse->nMem;
798 pParse->nMem += pIdx->nColumn;
771 } 799 }
772 } 800 }
773 801
774 /* This is the top of the main insertion loop */ 802 /* This is the top of the main insertion loop */
775 if( useTempTable ){ 803 if( useTempTable ){
776 /* This block codes the top of loop only. The complete loop is the 804 /* This block codes the top of loop only. The complete loop is the
777 ** following pseudocode (template 4): 805 ** following pseudocode (template 4):
778 ** 806 **
779 ** rewind temp table, if empty goto D 807 ** rewind temp table, if empty goto D
780 ** C: loop over rows of intermediate table 808 ** C: loop over rows of intermediate table
(...skipping 181 matching lines...) Expand 10 before | Expand all | Expand 10 after
962 if( IsVirtual(pTab) ){ 990 if( IsVirtual(pTab) ){
963 const char *pVTab = (const char *)sqlite3GetVTable(db, pTab); 991 const char *pVTab = (const char *)sqlite3GetVTable(db, pTab);
964 sqlite3VtabMakeWritable(pParse, pTab); 992 sqlite3VtabMakeWritable(pParse, pTab);
965 sqlite3VdbeAddOp4(v, OP_VUpdate, 1, pTab->nCol+2, regIns, pVTab, P4_VTAB); 993 sqlite3VdbeAddOp4(v, OP_VUpdate, 1, pTab->nCol+2, regIns, pVTab, P4_VTAB);
966 sqlite3VdbeChangeP5(v, onError==OE_Default ? OE_Abort : onError); 994 sqlite3VdbeChangeP5(v, onError==OE_Default ? OE_Abort : onError);
967 sqlite3MayAbort(pParse); 995 sqlite3MayAbort(pParse);
968 }else 996 }else
969 #endif 997 #endif
970 { 998 {
971 int isReplace; /* Set to true if constraints may cause a replace */ 999 int isReplace; /* Set to true if constraints may cause a replace */
1000 int bUseSeek; /* True to use OPFLAG_SEEKRESULT */
972 sqlite3GenerateConstraintChecks(pParse, pTab, aRegIdx, iDataCur, iIdxCur, 1001 sqlite3GenerateConstraintChecks(pParse, pTab, aRegIdx, iDataCur, iIdxCur,
973 regIns, 0, ipkColumn>=0, onError, endOfLoop, &isReplace 1002 regIns, 0, ipkColumn>=0, onError, endOfLoop, &isReplace, 0
974 ); 1003 );
975 sqlite3FkCheck(pParse, pTab, 0, regIns, 0, 0); 1004 sqlite3FkCheck(pParse, pTab, 0, regIns, 0, 0);
1005
1006 /* Set the OPFLAG_USESEEKRESULT flag if either (a) there are no REPLACE
1007 ** constraints or (b) there are no triggers and this table is not a
1008 ** parent table in a foreign key constraint. It is safe to set the
1009 ** flag in the second case as if any REPLACE constraint is hit, an
1010 ** OP_Delete or OP_IdxDelete instruction will be executed on each
1011 ** cursor that is disturbed. And these instructions both clear the
1012 ** VdbeCursor.seekResult variable, disabling the OPFLAG_USESEEKRESULT
1013 ** functionality. */
1014 bUseSeek = (isReplace==0 || (pTrigger==0 &&
1015 ((db->flags & SQLITE_ForeignKeys)==0 || sqlite3FkReferences(pTab)==0)
1016 ));
976 sqlite3CompleteInsertion(pParse, pTab, iDataCur, iIdxCur, 1017 sqlite3CompleteInsertion(pParse, pTab, iDataCur, iIdxCur,
977 regIns, aRegIdx, 0, appendFlag, isReplace==0); 1018 regIns, aRegIdx, 0, appendFlag, bUseSeek
1019 );
978 } 1020 }
979 } 1021 }
980 1022
981 /* Update the count of rows that are inserted 1023 /* Update the count of rows that are inserted
982 */ 1024 */
983 if( (db->flags & SQLITE_CountRows)!=0 ){ 1025 if( (db->flags & SQLITE_CountRows)!=0 ){
984 sqlite3VdbeAddOp2(v, OP_AddImm, regRowCount, 1); 1026 sqlite3VdbeAddOp2(v, OP_AddImm, regRowCount, 1);
985 } 1027 }
986 1028
987 if( pTrigger ){ 1029 if( pTrigger ){
988 /* Code AFTER triggers */ 1030 /* Code AFTER triggers */
989 sqlite3CodeRowTrigger(pParse, pTrigger, TK_INSERT, 0, TRIGGER_AFTER, 1031 sqlite3CodeRowTrigger(pParse, pTrigger, TK_INSERT, 0, TRIGGER_AFTER,
990 pTab, regData-2-pTab->nCol, onError, endOfLoop); 1032 pTab, regData-2-pTab->nCol, onError, endOfLoop);
991 } 1033 }
992 1034
993 /* The bottom of the main insertion loop, if the data source 1035 /* The bottom of the main insertion loop, if the data source
994 ** is a SELECT statement. 1036 ** is a SELECT statement.
995 */ 1037 */
996 sqlite3VdbeResolveLabel(v, endOfLoop); 1038 sqlite3VdbeResolveLabel(v, endOfLoop);
997 if( useTempTable ){ 1039 if( useTempTable ){
998 sqlite3VdbeAddOp2(v, OP_Next, srcTab, addrCont); VdbeCoverage(v); 1040 sqlite3VdbeAddOp2(v, OP_Next, srcTab, addrCont); VdbeCoverage(v);
999 sqlite3VdbeJumpHere(v, addrInsTop); 1041 sqlite3VdbeJumpHere(v, addrInsTop);
1000 sqlite3VdbeAddOp1(v, OP_Close, srcTab); 1042 sqlite3VdbeAddOp1(v, OP_Close, srcTab);
1001 }else if( pSelect ){ 1043 }else if( pSelect ){
1002 sqlite3VdbeGoto(v, addrCont); 1044 sqlite3VdbeGoto(v, addrCont);
1003 sqlite3VdbeJumpHere(v, addrInsTop); 1045 sqlite3VdbeJumpHere(v, addrInsTop);
1004 } 1046 }
1005 1047
1006 if( !IsVirtual(pTab) && !isView ){
1007 /* Close all tables opened */
1008 if( iDataCur<iIdxCur ) sqlite3VdbeAddOp1(v, OP_Close, iDataCur);
1009 for(idx=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, idx++){
1010 sqlite3VdbeAddOp1(v, OP_Close, idx+iIdxCur);
1011 }
1012 }
1013
1014 insert_end: 1048 insert_end:
1015 /* Update the sqlite_sequence table by storing the content of the 1049 /* Update the sqlite_sequence table by storing the content of the
1016 ** maximum rowid counter values recorded while inserting into 1050 ** maximum rowid counter values recorded while inserting into
1017 ** autoincrement tables. 1051 ** autoincrement tables.
1018 */ 1052 */
1019 if( pParse->nested==0 && pParse->pTriggerTab==0 ){ 1053 if( pParse->nested==0 && pParse->pTriggerTab==0 ){
1020 sqlite3AutoincrementEnd(pParse); 1054 sqlite3AutoincrementEnd(pParse);
1021 } 1055 }
1022 1056
1023 /* 1057 /*
(...skipping 22 matching lines...) Expand all
1046 #undef isView 1080 #undef isView
1047 #endif 1081 #endif
1048 #ifdef pTrigger 1082 #ifdef pTrigger
1049 #undef pTrigger 1083 #undef pTrigger
1050 #endif 1084 #endif
1051 #ifdef tmask 1085 #ifdef tmask
1052 #undef tmask 1086 #undef tmask
1053 #endif 1087 #endif
1054 1088
1055 /* 1089 /*
1090 ** Meanings of bits in of pWalker->eCode for checkConstraintUnchanged()
1091 */
1092 #define CKCNSTRNT_COLUMN 0x01 /* CHECK constraint uses a changing column */
1093 #define CKCNSTRNT_ROWID 0x02 /* CHECK constraint references the ROWID */
1094
1095 /* This is the Walker callback from checkConstraintUnchanged(). Set
1096 ** bit 0x01 of pWalker->eCode if
1097 ** pWalker->eCode to 0 if this expression node references any of the
1098 ** columns that are being modifed by an UPDATE statement.
1099 */
1100 static int checkConstraintExprNode(Walker *pWalker, Expr *pExpr){
1101 if( pExpr->op==TK_COLUMN ){
1102 assert( pExpr->iColumn>=0 || pExpr->iColumn==-1 );
1103 if( pExpr->iColumn>=0 ){
1104 if( pWalker->u.aiCol[pExpr->iColumn]>=0 ){
1105 pWalker->eCode |= CKCNSTRNT_COLUMN;
1106 }
1107 }else{
1108 pWalker->eCode |= CKCNSTRNT_ROWID;
1109 }
1110 }
1111 return WRC_Continue;
1112 }
1113
1114 /*
1115 ** pExpr is a CHECK constraint on a row that is being UPDATE-ed. The
1116 ** only columns that are modified by the UPDATE are those for which
1117 ** aiChng[i]>=0, and also the ROWID is modified if chngRowid is true.
1118 **
1119 ** Return true if CHECK constraint pExpr does not use any of the
1120 ** changing columns (or the rowid if it is changing). In other words,
1121 ** return true if this CHECK constraint can be skipped when validating
1122 ** the new row in the UPDATE statement.
1123 */
1124 static int checkConstraintUnchanged(Expr *pExpr, int *aiChng, int chngRowid){
1125 Walker w;
1126 memset(&w, 0, sizeof(w));
1127 w.eCode = 0;
1128 w.xExprCallback = checkConstraintExprNode;
1129 w.u.aiCol = aiChng;
1130 sqlite3WalkExpr(&w, pExpr);
1131 if( !chngRowid ){
1132 testcase( (w.eCode & CKCNSTRNT_ROWID)!=0 );
1133 w.eCode &= ~CKCNSTRNT_ROWID;
1134 }
1135 testcase( w.eCode==0 );
1136 testcase( w.eCode==CKCNSTRNT_COLUMN );
1137 testcase( w.eCode==CKCNSTRNT_ROWID );
1138 testcase( w.eCode==(CKCNSTRNT_ROWID|CKCNSTRNT_COLUMN) );
1139 return !w.eCode;
1140 }
1141
1142 /*
1056 ** Generate code to do constraint checks prior to an INSERT or an UPDATE 1143 ** Generate code to do constraint checks prior to an INSERT or an UPDATE
1057 ** on table pTab. 1144 ** on table pTab.
1058 ** 1145 **
1059 ** The regNewData parameter is the first register in a range that contains 1146 ** The regNewData parameter is the first register in a range that contains
1060 ** the data to be inserted or the data after the update. There will be 1147 ** the data to be inserted or the data after the update. There will be
1061 ** pTab->nCol+1 registers in this range. The first register (the one 1148 ** pTab->nCol+1 registers in this range. The first register (the one
1062 ** that regNewData points to) will contain the new rowid, or NULL in the 1149 ** that regNewData points to) will contain the new rowid, or NULL in the
1063 ** case of a WITHOUT ROWID table. The second register in the range will 1150 ** case of a WITHOUT ROWID table. The second register in the range will
1064 ** contain the content of the first table column. The third register will 1151 ** contain the content of the first table column. The third register will
1065 ** contain the content of the second table column. And so forth. 1152 ** contain the content of the second table column. And so forth.
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after
1139 Parse *pParse, /* The parser context */ 1226 Parse *pParse, /* The parser context */
1140 Table *pTab, /* The table being inserted or updated */ 1227 Table *pTab, /* The table being inserted or updated */
1141 int *aRegIdx, /* Use register aRegIdx[i] for index i. 0 for unused */ 1228 int *aRegIdx, /* Use register aRegIdx[i] for index i. 0 for unused */
1142 int iDataCur, /* Canonical data cursor (main table or PK index) */ 1229 int iDataCur, /* Canonical data cursor (main table or PK index) */
1143 int iIdxCur, /* First index cursor */ 1230 int iIdxCur, /* First index cursor */
1144 int regNewData, /* First register in a range holding values to insert */ 1231 int regNewData, /* First register in a range holding values to insert */
1145 int regOldData, /* Previous content. 0 for INSERTs */ 1232 int regOldData, /* Previous content. 0 for INSERTs */
1146 u8 pkChng, /* Non-zero if the rowid or PRIMARY KEY changed */ 1233 u8 pkChng, /* Non-zero if the rowid or PRIMARY KEY changed */
1147 u8 overrideError, /* Override onError to this if not OE_Default */ 1234 u8 overrideError, /* Override onError to this if not OE_Default */
1148 int ignoreDest, /* Jump to this label on an OE_Ignore resolution */ 1235 int ignoreDest, /* Jump to this label on an OE_Ignore resolution */
1149 int *pbMayReplace /* OUT: Set to true if constraint may cause a replace */ 1236 int *pbMayReplace, /* OUT: Set to true if constraint may cause a replace */
1237 int *aiChng /* column i is unchanged if aiChng[i]<0 */
1150 ){ 1238 ){
1151 Vdbe *v; /* VDBE under constrution */ 1239 Vdbe *v; /* VDBE under constrution */
1152 Index *pIdx; /* Pointer to one of the indices */ 1240 Index *pIdx; /* Pointer to one of the indices */
1153 Index *pPk = 0; /* The PRIMARY KEY index */ 1241 Index *pPk = 0; /* The PRIMARY KEY index */
1154 sqlite3 *db; /* Database connection */ 1242 sqlite3 *db; /* Database connection */
1155 int i; /* loop counter */ 1243 int i; /* loop counter */
1156 int ix; /* Index loop counter */ 1244 int ix; /* Index loop counter */
1157 int nCol; /* Number of columns */ 1245 int nCol; /* Number of columns */
1158 int onError; /* Conflict resolution strategy */ 1246 int onError; /* Conflict resolution strategy */
1159 int addr1; /* Address of jump instruction */ 1247 int addr1; /* Address of jump instruction */
1160 int seenReplace = 0; /* True if REPLACE is used to resolve INT PK conflict */ 1248 int seenReplace = 0; /* True if REPLACE is used to resolve INT PK conflict */
1161 int nPkField; /* Number of fields in PRIMARY KEY. 1 for ROWID tables */ 1249 int nPkField; /* Number of fields in PRIMARY KEY. 1 for ROWID tables */
1162 int ipkTop = 0; /* Top of the rowid change constraint check */ 1250 int ipkTop = 0; /* Top of the rowid change constraint check */
1163 int ipkBottom = 0; /* Bottom of the rowid change constraint check */ 1251 int ipkBottom = 0; /* Bottom of the rowid change constraint check */
1164 u8 isUpdate; /* True if this is an UPDATE operation */ 1252 u8 isUpdate; /* True if this is an UPDATE operation */
1165 u8 bAffinityDone = 0; /* True if the OP_Affinity operation has been run */ 1253 u8 bAffinityDone = 0; /* True if the OP_Affinity operation has been run */
1166 int regRowid = -1; /* Register holding ROWID value */
1167 1254
1168 isUpdate = regOldData!=0; 1255 isUpdate = regOldData!=0;
1169 db = pParse->db; 1256 db = pParse->db;
1170 v = sqlite3GetVdbe(pParse); 1257 v = sqlite3GetVdbe(pParse);
1171 assert( v!=0 ); 1258 assert( v!=0 );
1172 assert( pTab->pSelect==0 ); /* This table is not a VIEW */ 1259 assert( pTab->pSelect==0 ); /* This table is not a VIEW */
1173 nCol = pTab->nCol; 1260 nCol = pTab->nCol;
1174 1261
1175 /* pPk is the PRIMARY KEY index for WITHOUT ROWID tables and NULL for 1262 /* pPk is the PRIMARY KEY index for WITHOUT ROWID tables and NULL for
1176 ** normal rowid tables. nPkField is the number of key fields in the 1263 ** normal rowid tables. nPkField is the number of key fields in the
1177 ** pPk index or 1 for a rowid table. In other words, nPkField is the 1264 ** pPk index or 1 for a rowid table. In other words, nPkField is the
1178 ** number of fields in the true primary key of the table. */ 1265 ** number of fields in the true primary key of the table. */
1179 if( HasRowid(pTab) ){ 1266 if( HasRowid(pTab) ){
1180 pPk = 0; 1267 pPk = 0;
1181 nPkField = 1; 1268 nPkField = 1;
1182 }else{ 1269 }else{
1183 pPk = sqlite3PrimaryKeyIndex(pTab); 1270 pPk = sqlite3PrimaryKeyIndex(pTab);
1184 nPkField = pPk->nKeyCol; 1271 nPkField = pPk->nKeyCol;
1185 } 1272 }
1186 1273
1187 /* Record that this module has started */ 1274 /* Record that this module has started */
1188 VdbeModuleComment((v, "BEGIN: GenCnstCks(%d,%d,%d,%d,%d)", 1275 VdbeModuleComment((v, "BEGIN: GenCnstCks(%d,%d,%d,%d,%d)",
1189 iDataCur, iIdxCur, regNewData, regOldData, pkChng)); 1276 iDataCur, iIdxCur, regNewData, regOldData, pkChng));
1190 1277
1191 /* Test all NOT NULL constraints. 1278 /* Test all NOT NULL constraints.
1192 */ 1279 */
1193 for(i=0; i<nCol; i++){ 1280 for(i=0; i<nCol; i++){
1194 if( i==pTab->iPKey ){ 1281 if( i==pTab->iPKey ){
1282 continue; /* ROWID is never NULL */
1283 }
1284 if( aiChng && aiChng[i]<0 ){
1285 /* Don't bother checking for NOT NULL on columns that do not change */
1195 continue; 1286 continue;
1196 } 1287 }
1197 onError = pTab->aCol[i].notNull; 1288 onError = pTab->aCol[i].notNull;
1198 if( onError==OE_None ) continue; 1289 if( onError==OE_None ) continue; /* This column is allowed to be NULL */
1199 if( overrideError!=OE_Default ){ 1290 if( overrideError!=OE_Default ){
1200 onError = overrideError; 1291 onError = overrideError;
1201 }else if( onError==OE_Default ){ 1292 }else if( onError==OE_Default ){
1202 onError = OE_Abort; 1293 onError = OE_Abort;
1203 } 1294 }
1204 if( onError==OE_Replace && pTab->aCol[i].pDflt==0 ){ 1295 if( onError==OE_Replace && pTab->aCol[i].pDflt==0 ){
1205 onError = OE_Abort; 1296 onError = OE_Abort;
1206 } 1297 }
1207 assert( onError==OE_Rollback || onError==OE_Abort || onError==OE_Fail 1298 assert( onError==OE_Rollback || onError==OE_Abort || onError==OE_Fail
1208 || onError==OE_Ignore || onError==OE_Replace ); 1299 || onError==OE_Ignore || onError==OE_Replace );
1209 switch( onError ){ 1300 switch( onError ){
1210 case OE_Abort: 1301 case OE_Abort:
1211 sqlite3MayAbort(pParse); 1302 sqlite3MayAbort(pParse);
1212 /* Fall through */ 1303 /* Fall through */
1213 case OE_Rollback: 1304 case OE_Rollback:
1214 case OE_Fail: { 1305 case OE_Fail: {
1215 char *zMsg = sqlite3MPrintf(db, "%s.%s", pTab->zName, 1306 char *zMsg = sqlite3MPrintf(db, "%s.%s", pTab->zName,
1216 pTab->aCol[i].zName); 1307 pTab->aCol[i].zName);
1217 sqlite3VdbeAddOp4(v, OP_HaltIfNull, SQLITE_CONSTRAINT_NOTNULL, onError, 1308 sqlite3VdbeAddOp3(v, OP_HaltIfNull, SQLITE_CONSTRAINT_NOTNULL, onError,
1218 regNewData+1+i, zMsg, P4_DYNAMIC); 1309 regNewData+1+i);
1310 sqlite3VdbeAppendP4(v, zMsg, P4_DYNAMIC);
1219 sqlite3VdbeChangeP5(v, P5_ConstraintNotNull); 1311 sqlite3VdbeChangeP5(v, P5_ConstraintNotNull);
1220 VdbeCoverage(v); 1312 VdbeCoverage(v);
1221 break; 1313 break;
1222 } 1314 }
1223 case OE_Ignore: { 1315 case OE_Ignore: {
1224 sqlite3VdbeAddOp2(v, OP_IsNull, regNewData+1+i, ignoreDest); 1316 sqlite3VdbeAddOp2(v, OP_IsNull, regNewData+1+i, ignoreDest);
1225 VdbeCoverage(v); 1317 VdbeCoverage(v);
1226 break; 1318 break;
1227 } 1319 }
1228 default: { 1320 default: {
1229 assert( onError==OE_Replace ); 1321 assert( onError==OE_Replace );
1230 addr1 = sqlite3VdbeAddOp1(v, OP_NotNull, regNewData+1+i); 1322 addr1 = sqlite3VdbeAddOp1(v, OP_NotNull, regNewData+1+i);
1231 VdbeCoverage(v); 1323 VdbeCoverage(v);
1232 sqlite3ExprCode(pParse, pTab->aCol[i].pDflt, regNewData+1+i); 1324 sqlite3ExprCode(pParse, pTab->aCol[i].pDflt, regNewData+1+i);
1233 sqlite3VdbeJumpHere(v, addr1); 1325 sqlite3VdbeJumpHere(v, addr1);
1234 break; 1326 break;
1235 } 1327 }
1236 } 1328 }
1237 } 1329 }
1238 1330
1239 /* Test all CHECK constraints 1331 /* Test all CHECK constraints
1240 */ 1332 */
1241 #ifndef SQLITE_OMIT_CHECK 1333 #ifndef SQLITE_OMIT_CHECK
1242 if( pTab->pCheck && (db->flags & SQLITE_IgnoreChecks)==0 ){ 1334 if( pTab->pCheck && (db->flags & SQLITE_IgnoreChecks)==0 ){
1243 ExprList *pCheck = pTab->pCheck; 1335 ExprList *pCheck = pTab->pCheck;
1244 pParse->ckBase = regNewData+1; 1336 pParse->ckBase = regNewData+1;
1245 onError = overrideError!=OE_Default ? overrideError : OE_Abort; 1337 onError = overrideError!=OE_Default ? overrideError : OE_Abort;
1246 for(i=0; i<pCheck->nExpr; i++){ 1338 for(i=0; i<pCheck->nExpr; i++){
1247 int allOk = sqlite3VdbeMakeLabel(v); 1339 int allOk;
1248 sqlite3ExprIfTrue(pParse, pCheck->a[i].pExpr, allOk, SQLITE_JUMPIFNULL); 1340 Expr *pExpr = pCheck->a[i].pExpr;
1341 if( aiChng && checkConstraintUnchanged(pExpr, aiChng, pkChng) ) continue;
1342 allOk = sqlite3VdbeMakeLabel(v);
1343 sqlite3ExprIfTrue(pParse, pExpr, allOk, SQLITE_JUMPIFNULL);
1249 if( onError==OE_Ignore ){ 1344 if( onError==OE_Ignore ){
1250 sqlite3VdbeGoto(v, ignoreDest); 1345 sqlite3VdbeGoto(v, ignoreDest);
1251 }else{ 1346 }else{
1252 char *zName = pCheck->a[i].zName; 1347 char *zName = pCheck->a[i].zName;
1253 if( zName==0 ) zName = pTab->zName; 1348 if( zName==0 ) zName = pTab->zName;
1254 if( onError==OE_Replace ) onError = OE_Abort; /* IMP: R-15569-63625 */ 1349 if( onError==OE_Replace ) onError = OE_Abort; /* IMP: R-15569-63625 */
1255 sqlite3HaltConstraint(pParse, SQLITE_CONSTRAINT_CHECK, 1350 sqlite3HaltConstraint(pParse, SQLITE_CONSTRAINT_CHECK,
1256 onError, zName, P4_TRANSIENT, 1351 onError, zName, P4_TRANSIENT,
1257 P5_ConstraintCheck); 1352 P5_ConstraintCheck);
1258 } 1353 }
(...skipping 10 matching lines...) Expand all
1269 1364
1270 /* Figure out what action to take in case of a rowid collision */ 1365 /* Figure out what action to take in case of a rowid collision */
1271 onError = pTab->keyConf; 1366 onError = pTab->keyConf;
1272 if( overrideError!=OE_Default ){ 1367 if( overrideError!=OE_Default ){
1273 onError = overrideError; 1368 onError = overrideError;
1274 }else if( onError==OE_Default ){ 1369 }else if( onError==OE_Default ){
1275 onError = OE_Abort; 1370 onError = OE_Abort;
1276 } 1371 }
1277 1372
1278 if( isUpdate ){ 1373 if( isUpdate ){
1279 /* pkChng!=0 does not mean that the rowid has change, only that 1374 /* pkChng!=0 does not mean that the rowid has changed, only that
1280 ** it might have changed. Skip the conflict logic below if the rowid 1375 ** it might have changed. Skip the conflict logic below if the rowid
1281 ** is unchanged. */ 1376 ** is unchanged. */
1282 sqlite3VdbeAddOp3(v, OP_Eq, regNewData, addrRowidOk, regOldData); 1377 sqlite3VdbeAddOp3(v, OP_Eq, regNewData, addrRowidOk, regOldData);
1283 sqlite3VdbeChangeP5(v, SQLITE_NOTNULL); 1378 sqlite3VdbeChangeP5(v, SQLITE_NOTNULL);
1284 VdbeCoverage(v); 1379 VdbeCoverage(v);
1285 } 1380 }
1286 1381
1287 /* If the response to a rowid conflict is REPLACE but the response 1382 /* If the response to a rowid conflict is REPLACE but the response
1288 ** to some other UNIQUE constraint is FAIL or IGNORE, then we need 1383 ** to some other UNIQUE constraint is FAIL or IGNORE, then we need
1289 ** to defer the running of the rowid conflict checking until after 1384 ** to defer the running of the rowid conflict checking until after
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
1338 ** to run without a statement journal if there are no indexes on the 1433 ** to run without a statement journal if there are no indexes on the
1339 ** table. 1434 ** table.
1340 */ 1435 */
1341 Trigger *pTrigger = 0; 1436 Trigger *pTrigger = 0;
1342 if( db->flags&SQLITE_RecTriggers ){ 1437 if( db->flags&SQLITE_RecTriggers ){
1343 pTrigger = sqlite3TriggersExist(pParse, pTab, TK_DELETE, 0, 0); 1438 pTrigger = sqlite3TriggersExist(pParse, pTab, TK_DELETE, 0, 0);
1344 } 1439 }
1345 if( pTrigger || sqlite3FkRequired(pParse, pTab, 0, 0) ){ 1440 if( pTrigger || sqlite3FkRequired(pParse, pTab, 0, 0) ){
1346 sqlite3MultiWrite(pParse); 1441 sqlite3MultiWrite(pParse);
1347 sqlite3GenerateRowDelete(pParse, pTab, pTrigger, iDataCur, iIdxCur, 1442 sqlite3GenerateRowDelete(pParse, pTab, pTrigger, iDataCur, iIdxCur,
1348 regNewData, 1, 0, OE_Replace, 1443 regNewData, 1, 0, OE_Replace, 1, -1);
1349 ONEPASS_SINGLE, -1);
1350 }else{ 1444 }else{
1445 #ifdef SQLITE_ENABLE_PREUPDATE_HOOK
1446 if( HasRowid(pTab) ){
1447 /* This OP_Delete opcode fires the pre-update-hook only. It does
1448 ** not modify the b-tree. It is more efficient to let the coming
1449 ** OP_Insert replace the existing entry than it is to delete the
1450 ** existing entry and then insert a new one. */
1451 sqlite3VdbeAddOp2(v, OP_Delete, iDataCur, OPFLAG_ISNOOP);
1452 sqlite3VdbeAppendP4(v, pTab, P4_TABLE);
1453 }
1454 #endif /* SQLITE_ENABLE_PREUPDATE_HOOK */
1351 if( pTab->pIndex ){ 1455 if( pTab->pIndex ){
1352 sqlite3MultiWrite(pParse); 1456 sqlite3MultiWrite(pParse);
1353 sqlite3GenerateRowIndexDelete(pParse, pTab, iDataCur, iIdxCur,0,-1); 1457 sqlite3GenerateRowIndexDelete(pParse, pTab, iDataCur, iIdxCur,0,-1);
1354 } 1458 }
1355 } 1459 }
1356 seenReplace = 1; 1460 seenReplace = 1;
1357 break; 1461 break;
1358 } 1462 }
1359 case OE_Ignore: { 1463 case OE_Ignore: {
1360 /*assert( seenReplace==0 );*/ 1464 /*assert( seenReplace==0 );*/
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
1395 sqlite3VdbeAddOp2(v, OP_Null, 0, aRegIdx[ix]); 1499 sqlite3VdbeAddOp2(v, OP_Null, 0, aRegIdx[ix]);
1396 pParse->ckBase = regNewData+1; 1500 pParse->ckBase = regNewData+1;
1397 sqlite3ExprIfFalseDup(pParse, pIdx->pPartIdxWhere, addrUniqueOk, 1501 sqlite3ExprIfFalseDup(pParse, pIdx->pPartIdxWhere, addrUniqueOk,
1398 SQLITE_JUMPIFNULL); 1502 SQLITE_JUMPIFNULL);
1399 pParse->ckBase = 0; 1503 pParse->ckBase = 0;
1400 } 1504 }
1401 1505
1402 /* Create a record for this index entry as it should appear after 1506 /* Create a record for this index entry as it should appear after
1403 ** the insert or update. Store that record in the aRegIdx[ix] register 1507 ** the insert or update. Store that record in the aRegIdx[ix] register
1404 */ 1508 */
1405 regIdx = sqlite3GetTempRange(pParse, pIdx->nColumn); 1509 regIdx = aRegIdx[ix]+1;
1406 for(i=0; i<pIdx->nColumn; i++){ 1510 for(i=0; i<pIdx->nColumn; i++){
1407 int iField = pIdx->aiColumn[i]; 1511 int iField = pIdx->aiColumn[i];
1408 int x; 1512 int x;
1409 if( iField==XN_EXPR ){ 1513 if( iField==XN_EXPR ){
1410 pParse->ckBase = regNewData+1; 1514 pParse->ckBase = regNewData+1;
1411 sqlite3ExprCodeCopy(pParse, pIdx->aColExpr->a[i].pExpr, regIdx+i); 1515 sqlite3ExprCodeCopy(pParse, pIdx->aColExpr->a[i].pExpr, regIdx+i);
1412 pParse->ckBase = 0; 1516 pParse->ckBase = 0;
1413 VdbeComment((v, "%s column %d", pIdx->zName, i)); 1517 VdbeComment((v, "%s column %d", pIdx->zName, i));
1414 }else{ 1518 }else{
1415 if( iField==XN_ROWID || iField==pTab->iPKey ){ 1519 if( iField==XN_ROWID || iField==pTab->iPKey ){
1416 if( regRowid==regIdx+i ) continue; /* ROWID already in regIdx+i */
1417 x = regNewData; 1520 x = regNewData;
1418 regRowid = pIdx->pPartIdxWhere ? -1 : regIdx+i;
1419 }else{ 1521 }else{
1420 x = iField + regNewData + 1; 1522 x = iField + regNewData + 1;
1421 } 1523 }
1422 sqlite3VdbeAddOp2(v, iField<0 ? OP_IntCopy : OP_SCopy, x, regIdx+i); 1524 sqlite3VdbeAddOp2(v, iField<0 ? OP_IntCopy : OP_SCopy, x, regIdx+i);
1423 VdbeComment((v, "%s", iField<0 ? "rowid" : pTab->aCol[iField].zName)); 1525 VdbeComment((v, "%s", iField<0 ? "rowid" : pTab->aCol[iField].zName));
1424 } 1526 }
1425 } 1527 }
1426 sqlite3VdbeAddOp3(v, OP_MakeRecord, regIdx, pIdx->nColumn, aRegIdx[ix]); 1528 sqlite3VdbeAddOp3(v, OP_MakeRecord, regIdx, pIdx->nColumn, aRegIdx[ix]);
1427 VdbeComment((v, "for %s", pIdx->zName)); 1529 VdbeComment((v, "for %s", pIdx->zName));
1428 sqlite3ExprCacheAffinityChange(pParse, regIdx, pIdx->nColumn);
1429 1530
1430 /* In an UPDATE operation, if this index is the PRIMARY KEY index 1531 /* In an UPDATE operation, if this index is the PRIMARY KEY index
1431 ** of a WITHOUT ROWID table and there has been no change the 1532 ** of a WITHOUT ROWID table and there has been no change the
1432 ** primary key, then no collision is possible. The collision detection 1533 ** primary key, then no collision is possible. The collision detection
1433 ** logic below can all be skipped. */ 1534 ** logic below can all be skipped. */
1434 if( isUpdate && pPk==pIdx && pkChng==0 ){ 1535 if( isUpdate && pPk==pIdx && pkChng==0 ){
1435 sqlite3VdbeResolveLabel(v, addrUniqueOk); 1536 sqlite3VdbeResolveLabel(v, addrUniqueOk);
1436 continue; 1537 continue;
1437 } 1538 }
1438 1539
1439 /* Find out what action to take in case there is a uniqueness conflict */ 1540 /* Find out what action to take in case there is a uniqueness conflict */
1440 onError = pIdx->onError; 1541 onError = pIdx->onError;
1441 if( onError==OE_None ){ 1542 if( onError==OE_None ){
1442 sqlite3ReleaseTempRange(pParse, regIdx, pIdx->nColumn);
1443 sqlite3VdbeResolveLabel(v, addrUniqueOk); 1543 sqlite3VdbeResolveLabel(v, addrUniqueOk);
1444 continue; /* pIdx is not a UNIQUE index */ 1544 continue; /* pIdx is not a UNIQUE index */
1445 } 1545 }
1446 if( overrideError!=OE_Default ){ 1546 if( overrideError!=OE_Default ){
1447 onError = overrideError; 1547 onError = overrideError;
1448 }else if( onError==OE_Default ){ 1548 }else if( onError==OE_Default ){
1449 onError = OE_Abort; 1549 onError = OE_Abort;
1450 } 1550 }
1451 1551
1552 /* Collision detection may be omitted if all of the following are true:
1553 ** (1) The conflict resolution algorithm is REPLACE
1554 ** (2) The table is a WITHOUT ROWID table
1555 ** (3) There are no secondary indexes on the table
1556 ** (4) No delete triggers need to be fired if there is a conflict
1557 ** (5) No FK constraint counters need to be updated if a conflict occurs.
1558 */
1559 if( (ix==0 && pIdx->pNext==0) /* Condition 3 */
1560 && pPk==pIdx /* Condition 2 */
1561 && onError==OE_Replace /* Condition 1 */
1562 && ( 0==(db->flags&SQLITE_RecTriggers) || /* Condition 4 */
1563 0==sqlite3TriggersExist(pParse, pTab, TK_DELETE, 0, 0))
1564 && ( 0==(db->flags&SQLITE_ForeignKeys) || /* Condition 5 */
1565 (0==pTab->pFKey && 0==sqlite3FkReferences(pTab)))
1566 ){
1567 sqlite3VdbeResolveLabel(v, addrUniqueOk);
1568 continue;
1569 }
1570
1452 /* Check to see if the new index entry will be unique */ 1571 /* Check to see if the new index entry will be unique */
1453 sqlite3VdbeAddOp4Int(v, OP_NoConflict, iThisCur, addrUniqueOk, 1572 sqlite3VdbeAddOp4Int(v, OP_NoConflict, iThisCur, addrUniqueOk,
1454 regIdx, pIdx->nKeyCol); VdbeCoverage(v); 1573 regIdx, pIdx->nKeyCol); VdbeCoverage(v);
1455 1574
1456 /* Generate code to handle collisions */ 1575 /* Generate code to handle collisions */
1457 regR = (pIdx==pPk) ? regIdx : sqlite3GetTempRange(pParse, nPkField); 1576 regR = (pIdx==pPk) ? regIdx : sqlite3GetTempRange(pParse, nPkField);
1458 if( isUpdate || onError==OE_Replace ){ 1577 if( isUpdate || onError==OE_Replace ){
1459 if( HasRowid(pTab) ){ 1578 if( HasRowid(pTab) ){
1460 sqlite3VdbeAddOp2(v, OP_IdxRowid, iThisCur, regR); 1579 sqlite3VdbeAddOp2(v, OP_IdxRowid, iThisCur, regR);
1461 /* Conflict only if the rowid of the existing index entry 1580 /* Conflict only if the rowid of the existing index entry
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after
1525 } 1644 }
1526 default: { 1645 default: {
1527 Trigger *pTrigger = 0; 1646 Trigger *pTrigger = 0;
1528 assert( onError==OE_Replace ); 1647 assert( onError==OE_Replace );
1529 sqlite3MultiWrite(pParse); 1648 sqlite3MultiWrite(pParse);
1530 if( db->flags&SQLITE_RecTriggers ){ 1649 if( db->flags&SQLITE_RecTriggers ){
1531 pTrigger = sqlite3TriggersExist(pParse, pTab, TK_DELETE, 0, 0); 1650 pTrigger = sqlite3TriggersExist(pParse, pTab, TK_DELETE, 0, 0);
1532 } 1651 }
1533 sqlite3GenerateRowDelete(pParse, pTab, pTrigger, iDataCur, iIdxCur, 1652 sqlite3GenerateRowDelete(pParse, pTab, pTrigger, iDataCur, iIdxCur,
1534 regR, nPkField, 0, OE_Replace, 1653 regR, nPkField, 0, OE_Replace,
1535 (pIdx==pPk ? ONEPASS_SINGLE : ONEPASS_OFF), -1); 1654 (pIdx==pPk ? ONEPASS_SINGLE : ONEPASS_OFF), iThisCur);
1536 seenReplace = 1; 1655 seenReplace = 1;
1537 break; 1656 break;
1538 } 1657 }
1539 } 1658 }
1540 sqlite3VdbeResolveLabel(v, addrUniqueOk); 1659 sqlite3VdbeResolveLabel(v, addrUniqueOk);
1541 sqlite3ReleaseTempRange(pParse, regIdx, pIdx->nColumn);
1542 if( regR!=regIdx ) sqlite3ReleaseTempRange(pParse, regR, nPkField); 1660 if( regR!=regIdx ) sqlite3ReleaseTempRange(pParse, regR, nPkField);
1543 } 1661 }
1544 if( ipkTop ){ 1662 if( ipkTop ){
1545 sqlite3VdbeGoto(v, ipkTop+1); 1663 sqlite3VdbeGoto(v, ipkTop+1);
1546 sqlite3VdbeJumpHere(v, ipkBottom); 1664 sqlite3VdbeJumpHere(v, ipkBottom);
1547 } 1665 }
1548 1666
1549 *pbMayReplace = seenReplace; 1667 *pbMayReplace = seenReplace;
1550 VdbeModuleComment((v, "END: GenCnstCks(%d)", seenReplace)); 1668 VdbeModuleComment((v, "END: GenCnstCks(%d)", seenReplace));
1551 } 1669 }
1552 1670
1671 #ifdef SQLITE_ENABLE_NULL_TRIM
1672 /*
1673 ** Change the P5 operand on the last opcode (which should be an OP_MakeRecord)
1674 ** to be the number of columns in table pTab that must not be NULL-trimmed.
1675 **
1676 ** Or if no columns of pTab may be NULL-trimmed, leave P5 at zero.
1677 */
1678 void sqlite3SetMakeRecordP5(Vdbe *v, Table *pTab){
1679 u16 i;
1680
1681 /* Records with omitted columns are only allowed for schema format
1682 ** version 2 and later (SQLite version 3.1.4, 2005-02-20). */
1683 if( pTab->pSchema->file_format<2 ) return;
1684
1685 for(i=pTab->nCol; i>1 && pTab->aCol[i-1].pDflt==0; i--){}
1686 sqlite3VdbeChangeP5(v, i);
1687 }
1688 #endif
1689
1553 /* 1690 /*
1554 ** This routine generates code to finish the INSERT or UPDATE operation 1691 ** This routine generates code to finish the INSERT or UPDATE operation
1555 ** that was started by a prior call to sqlite3GenerateConstraintChecks. 1692 ** that was started by a prior call to sqlite3GenerateConstraintChecks.
1556 ** A consecutive range of registers starting at regNewData contains the 1693 ** A consecutive range of registers starting at regNewData contains the
1557 ** rowid and the content to be inserted. 1694 ** rowid and the content to be inserted.
1558 ** 1695 **
1559 ** The arguments to this routine should be the same as the first six 1696 ** The arguments to this routine should be the same as the first six
1560 ** arguments to sqlite3GenerateConstraintChecks. 1697 ** arguments to sqlite3GenerateConstraintChecks.
1561 */ 1698 */
1562 void sqlite3CompleteInsertion( 1699 void sqlite3CompleteInsertion(
1563 Parse *pParse, /* The parser context */ 1700 Parse *pParse, /* The parser context */
1564 Table *pTab, /* the table into which we are inserting */ 1701 Table *pTab, /* the table into which we are inserting */
1565 int iDataCur, /* Cursor of the canonical data source */ 1702 int iDataCur, /* Cursor of the canonical data source */
1566 int iIdxCur, /* First index cursor */ 1703 int iIdxCur, /* First index cursor */
1567 int regNewData, /* Range of content */ 1704 int regNewData, /* Range of content */
1568 int *aRegIdx, /* Register used by each index. 0 for unused indices */ 1705 int *aRegIdx, /* Register used by each index. 0 for unused indices */
1569 int isUpdate, /* True for UPDATE, False for INSERT */ 1706 int update_flags, /* True for UPDATE, False for INSERT */
1570 int appendBias, /* True if this is likely to be an append */ 1707 int appendBias, /* True if this is likely to be an append */
1571 int useSeekResult /* True to set the USESEEKRESULT flag on OP_[Idx]Insert */ 1708 int useSeekResult /* True to set the USESEEKRESULT flag on OP_[Idx]Insert */
1572 ){ 1709 ){
1573 Vdbe *v; /* Prepared statements under construction */ 1710 Vdbe *v; /* Prepared statements under construction */
1574 Index *pIdx; /* An index being inserted or updated */ 1711 Index *pIdx; /* An index being inserted or updated */
1575 u8 pik_flags; /* flag values passed to the btree insert */ 1712 u8 pik_flags; /* flag values passed to the btree insert */
1576 int regData; /* Content registers (after the rowid) */ 1713 int regData; /* Content registers (after the rowid) */
1577 int regRec; /* Register holding assembled record for the table */ 1714 int regRec; /* Register holding assembled record for the table */
1578 int i; /* Loop counter */ 1715 int i; /* Loop counter */
1579 u8 bAffinityDone = 0; /* True if OP_Affinity has been run already */ 1716 u8 bAffinityDone = 0; /* True if OP_Affinity has been run already */
1580 1717
1718 assert( update_flags==0
1719 || update_flags==OPFLAG_ISUPDATE
1720 || update_flags==(OPFLAG_ISUPDATE|OPFLAG_SAVEPOSITION)
1721 );
1722
1581 v = sqlite3GetVdbe(pParse); 1723 v = sqlite3GetVdbe(pParse);
1582 assert( v!=0 ); 1724 assert( v!=0 );
1583 assert( pTab->pSelect==0 ); /* This table is not a VIEW */ 1725 assert( pTab->pSelect==0 ); /* This table is not a VIEW */
1584 for(i=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){ 1726 for(i=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){
1585 if( aRegIdx[i]==0 ) continue; 1727 if( aRegIdx[i]==0 ) continue;
1586 bAffinityDone = 1; 1728 bAffinityDone = 1;
1587 if( pIdx->pPartIdxWhere ){ 1729 if( pIdx->pPartIdxWhere ){
1588 sqlite3VdbeAddOp2(v, OP_IsNull, aRegIdx[i], sqlite3VdbeCurrentAddr(v)+2); 1730 sqlite3VdbeAddOp2(v, OP_IsNull, aRegIdx[i], sqlite3VdbeCurrentAddr(v)+2);
1589 VdbeCoverage(v); 1731 VdbeCoverage(v);
1590 } 1732 }
1591 sqlite3VdbeAddOp2(v, OP_IdxInsert, iIdxCur+i, aRegIdx[i]); 1733 pik_flags = (useSeekResult ? OPFLAG_USESEEKRESULT : 0);
1592 pik_flags = 0;
1593 if( useSeekResult ) pik_flags = OPFLAG_USESEEKRESULT;
1594 if( IsPrimaryKeyIndex(pIdx) && !HasRowid(pTab) ){ 1734 if( IsPrimaryKeyIndex(pIdx) && !HasRowid(pTab) ){
1595 assert( pParse->nested==0 ); 1735 assert( pParse->nested==0 );
1596 pik_flags |= OPFLAG_NCHANGE; 1736 pik_flags |= OPFLAG_NCHANGE;
1737 pik_flags |= (update_flags & OPFLAG_SAVEPOSITION);
1738 #ifdef SQLITE_ENABLE_PREUPDATE_HOOK
1739 if( update_flags==0 ){
1740 sqlite3VdbeAddOp4(v, OP_InsertInt,
1741 iIdxCur+i, aRegIdx[i], 0, (char*)pTab, P4_TABLE
1742 );
1743 sqlite3VdbeChangeP5(v, OPFLAG_ISNOOP);
1744 }
1745 #endif
1597 } 1746 }
1598 if( pik_flags ) sqlite3VdbeChangeP5(v, pik_flags); 1747 sqlite3VdbeAddOp4Int(v, OP_IdxInsert, iIdxCur+i, aRegIdx[i],
1748 aRegIdx[i]+1,
1749 pIdx->uniqNotNull ? pIdx->nKeyCol: pIdx->nColumn);
1750 sqlite3VdbeChangeP5(v, pik_flags);
1599 } 1751 }
1600 if( !HasRowid(pTab) ) return; 1752 if( !HasRowid(pTab) ) return;
1601 regData = regNewData + 1; 1753 regData = regNewData + 1;
1602 regRec = sqlite3GetTempReg(pParse); 1754 regRec = sqlite3GetTempReg(pParse);
1603 sqlite3VdbeAddOp3(v, OP_MakeRecord, regData, pTab->nCol, regRec); 1755 sqlite3VdbeAddOp3(v, OP_MakeRecord, regData, pTab->nCol, regRec);
1604 if( !bAffinityDone ) sqlite3TableAffinity(v, pTab, 0); 1756 sqlite3SetMakeRecordP5(v, pTab);
1605 sqlite3ExprCacheAffinityChange(pParse, regData, pTab->nCol); 1757 if( !bAffinityDone ){
1758 sqlite3TableAffinity(v, pTab, 0);
1759 sqlite3ExprCacheAffinityChange(pParse, regData, pTab->nCol);
1760 }
1606 if( pParse->nested ){ 1761 if( pParse->nested ){
1607 pik_flags = 0; 1762 pik_flags = 0;
1608 }else{ 1763 }else{
1609 pik_flags = OPFLAG_NCHANGE; 1764 pik_flags = OPFLAG_NCHANGE;
1610 pik_flags |= (isUpdate?OPFLAG_ISUPDATE:OPFLAG_LASTROWID); 1765 pik_flags |= (update_flags?update_flags:OPFLAG_LASTROWID);
1611 } 1766 }
1612 if( appendBias ){ 1767 if( appendBias ){
1613 pik_flags |= OPFLAG_APPEND; 1768 pik_flags |= OPFLAG_APPEND;
1614 } 1769 }
1615 if( useSeekResult ){ 1770 if( useSeekResult ){
1616 pik_flags |= OPFLAG_USESEEKRESULT; 1771 pik_flags |= OPFLAG_USESEEKRESULT;
1617 } 1772 }
1618 sqlite3VdbeAddOp3(v, OP_Insert, iDataCur, regRec, regNewData); 1773 sqlite3VdbeAddOp3(v, OP_Insert, iDataCur, regRec, regNewData);
1619 if( !pParse->nested ){ 1774 if( !pParse->nested ){
1620 sqlite3VdbeChangeP4(v, -1, pTab->zName, P4_TRANSIENT); 1775 sqlite3VdbeAppendP4(v, pTab, P4_TABLE);
1621 } 1776 }
1622 sqlite3VdbeChangeP5(v, pik_flags); 1777 sqlite3VdbeChangeP5(v, pik_flags);
1623 } 1778 }
1624 1779
1625 /* 1780 /*
1626 ** Allocate cursors for the pTab table and all its indices and generate 1781 ** Allocate cursors for the pTab table and all its indices and generate
1627 ** code to open and initialized those cursors. 1782 ** code to open and initialized those cursors.
1628 ** 1783 **
1629 ** The cursor for the object that contains the complete data (normally 1784 ** The cursor for the object that contains the complete data (normally
1630 ** the table itself, but the PRIMARY KEY index in the case of a WITHOUT 1785 ** the table itself, but the PRIMARY KEY index in the case of a WITHOUT
1631 ** ROWID table) is returned in *piDataCur. The first index cursor is 1786 ** ROWID table) is returned in *piDataCur. The first index cursor is
1632 ** returned in *piIdxCur. The number of indices is returned. 1787 ** returned in *piIdxCur. The number of indices is returned.
1633 ** 1788 **
1634 ** Use iBase as the first cursor (either the *piDataCur for rowid tables 1789 ** Use iBase as the first cursor (either the *piDataCur for rowid tables
1635 ** or the first index for WITHOUT ROWID tables) if it is non-negative. 1790 ** or the first index for WITHOUT ROWID tables) if it is non-negative.
1636 ** If iBase is negative, then allocate the next available cursor. 1791 ** If iBase is negative, then allocate the next available cursor.
1637 ** 1792 **
1638 ** For a rowid table, *piDataCur will be exactly one less than *piIdxCur. 1793 ** For a rowid table, *piDataCur will be exactly one less than *piIdxCur.
1639 ** For a WITHOUT ROWID table, *piDataCur will be somewhere in the range 1794 ** For a WITHOUT ROWID table, *piDataCur will be somewhere in the range
1640 ** of *piIdxCurs, depending on where the PRIMARY KEY index appears on the 1795 ** of *piIdxCurs, depending on where the PRIMARY KEY index appears on the
1641 ** pTab->pIndex list. 1796 ** pTab->pIndex list.
1642 ** 1797 **
1643 ** If pTab is a virtual table, then this routine is a no-op and the 1798 ** If pTab is a virtual table, then this routine is a no-op and the
1644 ** *piDataCur and *piIdxCur values are left uninitialized. 1799 ** *piDataCur and *piIdxCur values are left uninitialized.
1645 */ 1800 */
1646 int sqlite3OpenTableAndIndices( 1801 int sqlite3OpenTableAndIndices(
1647 Parse *pParse, /* Parsing context */ 1802 Parse *pParse, /* Parsing context */
1648 Table *pTab, /* Table to be opened */ 1803 Table *pTab, /* Table to be opened */
1649 int op, /* OP_OpenRead or OP_OpenWrite */ 1804 int op, /* OP_OpenRead or OP_OpenWrite */
1650 u8 p5, /* P5 value for OP_Open* instructions */ 1805 u8 p5, /* P5 value for OP_Open* opcodes (except on WITHOUT ROWID) */
1651 int iBase, /* Use this for the table cursor, if there is one */ 1806 int iBase, /* Use this for the table cursor, if there is one */
1652 u8 *aToOpen, /* If not NULL: boolean for each table and index */ 1807 u8 *aToOpen, /* If not NULL: boolean for each table and index */
1653 int *piDataCur, /* Write the database source cursor number here */ 1808 int *piDataCur, /* Write the database source cursor number here */
1654 int *piIdxCur /* Write the first index cursor number here */ 1809 int *piIdxCur /* Write the first index cursor number here */
1655 ){ 1810 ){
1656 int i; 1811 int i;
1657 int iDb; 1812 int iDb;
1658 int iDataCur; 1813 int iDataCur;
1659 Index *pIdx; 1814 Index *pIdx;
1660 Vdbe *v; 1815 Vdbe *v;
(...skipping 14 matching lines...) Expand all
1675 if( piDataCur ) *piDataCur = iDataCur; 1830 if( piDataCur ) *piDataCur = iDataCur;
1676 if( HasRowid(pTab) && (aToOpen==0 || aToOpen[0]) ){ 1831 if( HasRowid(pTab) && (aToOpen==0 || aToOpen[0]) ){
1677 sqlite3OpenTable(pParse, iDataCur, iDb, pTab, op); 1832 sqlite3OpenTable(pParse, iDataCur, iDb, pTab, op);
1678 }else{ 1833 }else{
1679 sqlite3TableLock(pParse, iDb, pTab->tnum, op==OP_OpenWrite, pTab->zName); 1834 sqlite3TableLock(pParse, iDb, pTab->tnum, op==OP_OpenWrite, pTab->zName);
1680 } 1835 }
1681 if( piIdxCur ) *piIdxCur = iBase; 1836 if( piIdxCur ) *piIdxCur = iBase;
1682 for(i=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){ 1837 for(i=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){
1683 int iIdxCur = iBase++; 1838 int iIdxCur = iBase++;
1684 assert( pIdx->pSchema==pTab->pSchema ); 1839 assert( pIdx->pSchema==pTab->pSchema );
1685 if( IsPrimaryKeyIndex(pIdx) && !HasRowid(pTab) && piDataCur ){ 1840 if( IsPrimaryKeyIndex(pIdx) && !HasRowid(pTab) ){
1686 *piDataCur = iIdxCur; 1841 if( piDataCur ) *piDataCur = iIdxCur;
1842 p5 = 0;
1687 } 1843 }
1688 if( aToOpen==0 || aToOpen[i+1] ){ 1844 if( aToOpen==0 || aToOpen[i+1] ){
1689 sqlite3VdbeAddOp3(v, op, iIdxCur, pIdx->tnum, iDb); 1845 sqlite3VdbeAddOp3(v, op, iIdxCur, pIdx->tnum, iDb);
1690 sqlite3VdbeSetP4KeyInfo(pParse, pIdx); 1846 sqlite3VdbeSetP4KeyInfo(pParse, pIdx);
1691 sqlite3VdbeChangeP5(v, p5); 1847 sqlite3VdbeChangeP5(v, p5);
1692 VdbeComment((v, "%s", pIdx->zName)); 1848 VdbeComment((v, "%s", pIdx->zName));
1693 } 1849 }
1694 } 1850 }
1695 if( iBase>pParse->nTab ) pParse->nTab = iBase; 1851 if( iBase>pParse->nTab ) pParse->nTab = iBase;
1696 return i; 1852 return i;
(...skipping 208 matching lines...) Expand 10 before | Expand all | Expand 10 after
1905 if( pDestCol->affinity!=pSrcCol->affinity ){ 2061 if( pDestCol->affinity!=pSrcCol->affinity ){
1906 return 0; /* Affinity must be the same on all columns */ 2062 return 0; /* Affinity must be the same on all columns */
1907 } 2063 }
1908 if( sqlite3_stricmp(pDestCol->zColl, pSrcCol->zColl)!=0 ){ 2064 if( sqlite3_stricmp(pDestCol->zColl, pSrcCol->zColl)!=0 ){
1909 return 0; /* Collating sequence must be the same on all columns */ 2065 return 0; /* Collating sequence must be the same on all columns */
1910 } 2066 }
1911 if( pDestCol->notNull && !pSrcCol->notNull ){ 2067 if( pDestCol->notNull && !pSrcCol->notNull ){
1912 return 0; /* tab2 must be NOT NULL if tab1 is */ 2068 return 0; /* tab2 must be NOT NULL if tab1 is */
1913 } 2069 }
1914 /* Default values for second and subsequent columns need to match. */ 2070 /* Default values for second and subsequent columns need to match. */
1915 if( i>0 2071 if( i>0 ){
1916 && ((pDestCol->zDflt==0)!=(pSrcCol->zDflt==0) 2072 assert( pDestCol->pDflt==0 || pDestCol->pDflt->op==TK_SPAN );
1917 || (pDestCol->zDflt && strcmp(pDestCol->zDflt, pSrcCol->zDflt)!=0)) 2073 assert( pSrcCol->pDflt==0 || pSrcCol->pDflt->op==TK_SPAN );
1918 ){ 2074 if( (pDestCol->pDflt==0)!=(pSrcCol->pDflt==0)
1919 return 0; /* Default values must be the same for all columns */ 2075 || (pDestCol->pDflt && strcmp(pDestCol->pDflt->u.zToken,
2076 pSrcCol->pDflt->u.zToken)!=0)
2077 ){
2078 return 0; /* Default values must be the same for all columns */
2079 }
1920 } 2080 }
1921 } 2081 }
1922 for(pDestIdx=pDest->pIndex; pDestIdx; pDestIdx=pDestIdx->pNext){ 2082 for(pDestIdx=pDest->pIndex; pDestIdx; pDestIdx=pDestIdx->pNext){
1923 if( IsUniqueIndex(pDestIdx) ){ 2083 if( IsUniqueIndex(pDestIdx) ){
1924 destHasUniqueIdx = 1; 2084 destHasUniqueIdx = 1;
1925 } 2085 }
1926 for(pSrcIdx=pSrc->pIndex; pSrcIdx; pSrcIdx=pSrcIdx->pNext){ 2086 for(pSrcIdx=pSrc->pIndex; pSrcIdx; pSrcIdx=pSrcIdx->pNext){
1927 if( xferCompatibleIndex(pDestIdx, pSrcIdx) ) break; 2087 if( xferCompatibleIndex(pDestIdx, pSrcIdx) ) break;
1928 } 2088 }
1929 if( pSrcIdx==0 ){ 2089 if( pSrcIdx==0 ){
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after
1988 ** (2) The destination has a unique index. (The xfer optimization 2148 ** (2) The destination has a unique index. (The xfer optimization
1989 ** is unable to test uniqueness.) 2149 ** is unable to test uniqueness.)
1990 ** 2150 **
1991 ** (3) onError is something other than OE_Abort and OE_Rollback. 2151 ** (3) onError is something other than OE_Abort and OE_Rollback.
1992 */ 2152 */
1993 addr1 = sqlite3VdbeAddOp2(v, OP_Rewind, iDest, 0); VdbeCoverage(v); 2153 addr1 = sqlite3VdbeAddOp2(v, OP_Rewind, iDest, 0); VdbeCoverage(v);
1994 emptyDestTest = sqlite3VdbeAddOp0(v, OP_Goto); 2154 emptyDestTest = sqlite3VdbeAddOp0(v, OP_Goto);
1995 sqlite3VdbeJumpHere(v, addr1); 2155 sqlite3VdbeJumpHere(v, addr1);
1996 } 2156 }
1997 if( HasRowid(pSrc) ){ 2157 if( HasRowid(pSrc) ){
2158 u8 insFlags;
1998 sqlite3OpenTable(pParse, iSrc, iDbSrc, pSrc, OP_OpenRead); 2159 sqlite3OpenTable(pParse, iSrc, iDbSrc, pSrc, OP_OpenRead);
1999 emptySrcTest = sqlite3VdbeAddOp2(v, OP_Rewind, iSrc, 0); VdbeCoverage(v); 2160 emptySrcTest = sqlite3VdbeAddOp2(v, OP_Rewind, iSrc, 0); VdbeCoverage(v);
2000 if( pDest->iPKey>=0 ){ 2161 if( pDest->iPKey>=0 ){
2001 addr1 = sqlite3VdbeAddOp2(v, OP_Rowid, iSrc, regRowid); 2162 addr1 = sqlite3VdbeAddOp2(v, OP_Rowid, iSrc, regRowid);
2002 addr2 = sqlite3VdbeAddOp3(v, OP_NotExists, iDest, 0, regRowid); 2163 addr2 = sqlite3VdbeAddOp3(v, OP_NotExists, iDest, 0, regRowid);
2003 VdbeCoverage(v); 2164 VdbeCoverage(v);
2004 sqlite3RowidConstraint(pParse, onError, pDest); 2165 sqlite3RowidConstraint(pParse, onError, pDest);
2005 sqlite3VdbeJumpHere(v, addr2); 2166 sqlite3VdbeJumpHere(v, addr2);
2006 autoIncStep(pParse, regAutoinc, regRowid); 2167 autoIncStep(pParse, regAutoinc, regRowid);
2007 }else if( pDest->pIndex==0 ){ 2168 }else if( pDest->pIndex==0 ){
2008 addr1 = sqlite3VdbeAddOp2(v, OP_NewRowid, iDest, regRowid); 2169 addr1 = sqlite3VdbeAddOp2(v, OP_NewRowid, iDest, regRowid);
2009 }else{ 2170 }else{
2010 addr1 = sqlite3VdbeAddOp2(v, OP_Rowid, iSrc, regRowid); 2171 addr1 = sqlite3VdbeAddOp2(v, OP_Rowid, iSrc, regRowid);
2011 assert( (pDest->tabFlags & TF_Autoincrement)==0 ); 2172 assert( (pDest->tabFlags & TF_Autoincrement)==0 );
2012 } 2173 }
2013 sqlite3VdbeAddOp2(v, OP_RowData, iSrc, regData); 2174 sqlite3VdbeAddOp3(v, OP_RowData, iSrc, regData, 1);
2014 sqlite3VdbeAddOp3(v, OP_Insert, iDest, regData, regRowid); 2175 if( db->flags & SQLITE_Vacuum ){
2015 sqlite3VdbeChangeP5(v, OPFLAG_NCHANGE|OPFLAG_LASTROWID|OPFLAG_APPEND); 2176 sqlite3VdbeAddOp3(v, OP_Last, iDest, 0, -1);
2016 sqlite3VdbeChangeP4(v, -1, pDest->zName, 0); 2177 insFlags = OPFLAG_NCHANGE|OPFLAG_LASTROWID|
2178 OPFLAG_APPEND|OPFLAG_USESEEKRESULT;
2179 }else{
2180 insFlags = OPFLAG_NCHANGE|OPFLAG_LASTROWID|OPFLAG_APPEND;
2181 }
2182 sqlite3VdbeAddOp4(v, OP_Insert, iDest, regData, regRowid,
2183 (char*)pDest, P4_TABLE);
2184 sqlite3VdbeChangeP5(v, insFlags);
2017 sqlite3VdbeAddOp2(v, OP_Next, iSrc, addr1); VdbeCoverage(v); 2185 sqlite3VdbeAddOp2(v, OP_Next, iSrc, addr1); VdbeCoverage(v);
2018 sqlite3VdbeAddOp2(v, OP_Close, iSrc, 0); 2186 sqlite3VdbeAddOp2(v, OP_Close, iSrc, 0);
2019 sqlite3VdbeAddOp2(v, OP_Close, iDest, 0); 2187 sqlite3VdbeAddOp2(v, OP_Close, iDest, 0);
2020 }else{ 2188 }else{
2021 sqlite3TableLock(pParse, iDbDest, pDest->tnum, 1, pDest->zName); 2189 sqlite3TableLock(pParse, iDbDest, pDest->tnum, 1, pDest->zName);
2022 sqlite3TableLock(pParse, iDbSrc, pSrc->tnum, 0, pSrc->zName); 2190 sqlite3TableLock(pParse, iDbSrc, pSrc->tnum, 0, pSrc->zName);
2023 } 2191 }
2024 for(pDestIdx=pDest->pIndex; pDestIdx; pDestIdx=pDestIdx->pNext){ 2192 for(pDestIdx=pDest->pIndex; pDestIdx; pDestIdx=pDestIdx->pNext){
2025 u8 idxInsFlags = 0; 2193 u8 idxInsFlags = 0;
2026 for(pSrcIdx=pSrc->pIndex; ALWAYS(pSrcIdx); pSrcIdx=pSrcIdx->pNext){ 2194 for(pSrcIdx=pSrc->pIndex; ALWAYS(pSrcIdx); pSrcIdx=pSrcIdx->pNext){
2027 if( xferCompatibleIndex(pDestIdx, pSrcIdx) ) break; 2195 if( xferCompatibleIndex(pDestIdx, pSrcIdx) ) break;
2028 } 2196 }
2029 assert( pSrcIdx ); 2197 assert( pSrcIdx );
2030 sqlite3VdbeAddOp3(v, OP_OpenRead, iSrc, pSrcIdx->tnum, iDbSrc); 2198 sqlite3VdbeAddOp3(v, OP_OpenRead, iSrc, pSrcIdx->tnum, iDbSrc);
2031 sqlite3VdbeSetP4KeyInfo(pParse, pSrcIdx); 2199 sqlite3VdbeSetP4KeyInfo(pParse, pSrcIdx);
2032 VdbeComment((v, "%s", pSrcIdx->zName)); 2200 VdbeComment((v, "%s", pSrcIdx->zName));
2033 sqlite3VdbeAddOp3(v, OP_OpenWrite, iDest, pDestIdx->tnum, iDbDest); 2201 sqlite3VdbeAddOp3(v, OP_OpenWrite, iDest, pDestIdx->tnum, iDbDest);
2034 sqlite3VdbeSetP4KeyInfo(pParse, pDestIdx); 2202 sqlite3VdbeSetP4KeyInfo(pParse, pDestIdx);
2035 sqlite3VdbeChangeP5(v, OPFLAG_BULKCSR); 2203 sqlite3VdbeChangeP5(v, OPFLAG_BULKCSR);
2036 VdbeComment((v, "%s", pDestIdx->zName)); 2204 VdbeComment((v, "%s", pDestIdx->zName));
2037 addr1 = sqlite3VdbeAddOp2(v, OP_Rewind, iSrc, 0); VdbeCoverage(v); 2205 addr1 = sqlite3VdbeAddOp2(v, OP_Rewind, iSrc, 0); VdbeCoverage(v);
2038 sqlite3VdbeAddOp2(v, OP_RowKey, iSrc, regData); 2206 sqlite3VdbeAddOp3(v, OP_RowData, iSrc, regData, 1);
2039 if( db->flags & SQLITE_Vacuum ){ 2207 if( db->flags & SQLITE_Vacuum ){
2040 /* This INSERT command is part of a VACUUM operation, which guarantees 2208 /* This INSERT command is part of a VACUUM operation, which guarantees
2041 ** that the destination table is empty. If all indexed columns use 2209 ** that the destination table is empty. If all indexed columns use
2042 ** collation sequence BINARY, then it can also be assumed that the 2210 ** collation sequence BINARY, then it can also be assumed that the
2043 ** index will be populated by inserting keys in strictly sorted 2211 ** index will be populated by inserting keys in strictly sorted
2044 ** order. In this case, instead of seeking within the b-tree as part 2212 ** order. In this case, instead of seeking within the b-tree as part
2045 ** of every OP_IdxInsert opcode, an OP_Last is added before the 2213 ** of every OP_IdxInsert opcode, an OP_Last is added before the
2046 ** OP_IdxInsert to seek to the point within the b-tree where each key 2214 ** OP_IdxInsert to seek to the point within the b-tree where each key
2047 ** should be inserted. This is faster. 2215 ** should be inserted. This is faster.
2048 ** 2216 **
2049 ** If any of the indexed columns use a collation sequence other than 2217 ** If any of the indexed columns use a collation sequence other than
2050 ** BINARY, this optimization is disabled. This is because the user 2218 ** BINARY, this optimization is disabled. This is because the user
2051 ** might change the definition of a collation sequence and then run 2219 ** might change the definition of a collation sequence and then run
2052 ** a VACUUM command. In that case keys may not be written in strictly 2220 ** a VACUUM command. In that case keys may not be written in strictly
2053 ** sorted order. */ 2221 ** sorted order. */
2054 for(i=0; i<pSrcIdx->nColumn; i++){ 2222 for(i=0; i<pSrcIdx->nColumn; i++){
2055 const char *zColl = pSrcIdx->azColl[i]; 2223 const char *zColl = pSrcIdx->azColl[i];
2056 assert( sqlite3_stricmp(sqlite3StrBINARY, zColl)!=0 2224 assert( sqlite3_stricmp(sqlite3StrBINARY, zColl)!=0
2057 || sqlite3StrBINARY==zColl ); 2225 || sqlite3StrBINARY==zColl );
2058 if( sqlite3_stricmp(sqlite3StrBINARY, zColl) ) break; 2226 if( sqlite3_stricmp(sqlite3StrBINARY, zColl) ) break;
2059 } 2227 }
2060 if( i==pSrcIdx->nColumn ){ 2228 if( i==pSrcIdx->nColumn ){
2061 idxInsFlags = OPFLAG_USESEEKRESULT; 2229 idxInsFlags = OPFLAG_USESEEKRESULT;
2062 sqlite3VdbeAddOp3(v, OP_Last, iDest, 0, -1); 2230 sqlite3VdbeAddOp3(v, OP_Last, iDest, 0, -1);
2063 } 2231 }
2064 } 2232 }
2065 if( !HasRowid(pSrc) && pDestIdx->idxType==2 ){ 2233 if( !HasRowid(pSrc) && pDestIdx->idxType==2 ){
2066 idxInsFlags |= OPFLAG_NCHANGE; 2234 idxInsFlags |= OPFLAG_NCHANGE;
2067 } 2235 }
2068 sqlite3VdbeAddOp3(v, OP_IdxInsert, iDest, regData, 1); 2236 sqlite3VdbeAddOp2(v, OP_IdxInsert, iDest, regData);
2069 sqlite3VdbeChangeP5(v, idxInsFlags); 2237 sqlite3VdbeChangeP5(v, idxInsFlags|OPFLAG_APPEND);
2070 sqlite3VdbeAddOp2(v, OP_Next, iSrc, addr1+1); VdbeCoverage(v); 2238 sqlite3VdbeAddOp2(v, OP_Next, iSrc, addr1+1); VdbeCoverage(v);
2071 sqlite3VdbeJumpHere(v, addr1); 2239 sqlite3VdbeJumpHere(v, addr1);
2072 sqlite3VdbeAddOp2(v, OP_Close, iSrc, 0); 2240 sqlite3VdbeAddOp2(v, OP_Close, iSrc, 0);
2073 sqlite3VdbeAddOp2(v, OP_Close, iDest, 0); 2241 sqlite3VdbeAddOp2(v, OP_Close, iDest, 0);
2074 } 2242 }
2075 if( emptySrcTest ) sqlite3VdbeJumpHere(v, emptySrcTest); 2243 if( emptySrcTest ) sqlite3VdbeJumpHere(v, emptySrcTest);
2076 sqlite3ReleaseTempReg(pParse, regRowid); 2244 sqlite3ReleaseTempReg(pParse, regRowid);
2077 sqlite3ReleaseTempReg(pParse, regData); 2245 sqlite3ReleaseTempReg(pParse, regData);
2078 if( emptyDestTest ){ 2246 if( emptyDestTest ){
2247 sqlite3AutoincrementEnd(pParse);
2079 sqlite3VdbeAddOp2(v, OP_Halt, SQLITE_OK, 0); 2248 sqlite3VdbeAddOp2(v, OP_Halt, SQLITE_OK, 0);
2080 sqlite3VdbeJumpHere(v, emptyDestTest); 2249 sqlite3VdbeJumpHere(v, emptyDestTest);
2081 sqlite3VdbeAddOp2(v, OP_Close, iDest, 0); 2250 sqlite3VdbeAddOp2(v, OP_Close, iDest, 0);
2082 return 0; 2251 return 0;
2083 }else{ 2252 }else{
2084 return 1; 2253 return 1;
2085 } 2254 }
2086 } 2255 }
2087 #endif /* SQLITE_OMIT_XFER_OPT */ 2256 #endif /* SQLITE_OMIT_XFER_OPT */
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698