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.256 2008/12/10 21:19:57 drh Exp $ 16 */ 17 #include "sqliteInt.h" 18 19 /* 20 ** Set P4 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 ** An extra 'b' is appended to the end of the string to cover the 33 ** rowid that appears as the last column in every index. 34 */ 35 void sqlite3IndexAffinityStr(Vdbe *v, Index *pIdx){ 36 if( !pIdx->zColAff ){ 37 /* The first time a column affinity string for a particular index is 38 ** required, it is allocated and populated here. It is then stored as 39 ** a member of the Index structure for subsequent use. 40 ** 41 ** The column affinity string will eventually be deleted by 42 ** sqliteDeleteIndex() when the Index structure itself is cleaned 43 ** up. 44 */ 45 int n; 46 Table *pTab = pIdx->pTable; 47 sqlite3 *db = sqlite3VdbeDb(v); 48 pIdx->zColAff = (char *)sqlite3Malloc(pIdx->nColumn+2); 49 if( !pIdx->zColAff ){ 50 db->mallocFailed = 1; 51 return; 52 } 53 for(n=0; n<pIdx->nColumn; n++){ 54 pIdx->zColAff[n] = pTab->aCol[pIdx->aiColumn[n]].affinity; 55 } 56 pIdx->zColAff[n++] = SQLITE_AFF_NONE; 57 pIdx->zColAff[n] = 0; 58 } 59 60 sqlite3VdbeChangeP4(v, -1, pIdx->zColAff, 0); 61 } 62 63 /* 64 ** Set P4 of the most recently inserted opcode to a column affinity 65 ** string for table pTab. A column affinity string has one character 66 ** for each column indexed by the index, according to the affinity of the 67 ** column: 68 ** 69 ** Character Column affinity 70 ** ------------------------------ 71 ** 'a' TEXT 72 ** 'b' NONE 73 ** 'c' NUMERIC 74 ** 'd' INTEGER 75 ** 'e' REAL 76 */ 77 void sqlite3TableAffinityStr(Vdbe *v, Table *pTab){ 78 /* The first time a column affinity string for a particular table 79 ** is required, it is allocated and populated here. It is then 80 ** stored as a member of the Table structure for subsequent use. 81 ** 82 ** The column affinity string will eventually be deleted by 83 ** sqlite3DeleteTable() when the Table structure itself is cleaned up. 84 */ 85 if( !pTab->zColAff ){ 86 char *zColAff; 87 int i; 88 sqlite3 *db = sqlite3VdbeDb(v); 89 90 zColAff = (char *)sqlite3Malloc(pTab->nCol+1); 91 if( !zColAff ){ 92 db->mallocFailed = 1; 93 return; 94 } 95 96 for(i=0; i<pTab->nCol; i++){ 97 zColAff[i] = pTab->aCol[i].affinity; 98 } 99 zColAff[pTab->nCol] = '\0'; 100 101 pTab->zColAff = zColAff; 102 } 103 104 sqlite3VdbeChangeP4(v, -1, pTab->zColAff, 0); 105 } 106 107 /* 108 ** Return non-zero if the table pTab in database iDb or any of its indices 109 ** have been opened at any point in the VDBE program beginning at location 110 ** iStartAddr throught the end of the program. This is used to see if 111 ** a statement of the form "INSERT INTO <iDb, pTab> SELECT ..." can 112 ** run without using temporary table for the results of the SELECT. 113 */ 114 static int readsTable(Vdbe *v, int iStartAddr, int iDb, Table *pTab){ 115 int i; 116 int iEnd = sqlite3VdbeCurrentAddr(v); 117 for(i=iStartAddr; i<iEnd; i++){ 118 VdbeOp *pOp = sqlite3VdbeGetOp(v, i); 119 assert( pOp!=0 ); 120 if( pOp->opcode==OP_OpenRead && pOp->p3==iDb ){ 121 Index *pIndex; 122 int tnum = pOp->p2; 123 if( tnum==pTab->tnum ){ 124 return 1; 125 } 126 for(pIndex=pTab->pIndex; pIndex; pIndex=pIndex->pNext){ 127 if( tnum==pIndex->tnum ){ 128 return 1; 129 } 130 } 131 } 132 #ifndef SQLITE_OMIT_VIRTUALTABLE 133 if( pOp->opcode==OP_VOpen && pOp->p4.pVtab==pTab->pVtab ){ 134 assert( pOp->p4.pVtab!=0 ); 135 assert( pOp->p4type==P4_VTAB ); 136 return 1; 137 } 138 #endif 139 } 140 return 0; 141 } 142 143 #ifndef SQLITE_OMIT_AUTOINCREMENT 144 /* 145 ** Write out code to initialize the autoincrement logic. This code 146 ** looks up the current autoincrement value in the sqlite_sequence 147 ** table and stores that value in a register. Code generated by 148 ** autoIncStep() will keep that register holding the largest 149 ** rowid value. Code generated by autoIncEnd() will write the new 150 ** largest value of the counter back into the sqlite_sequence table. 151 ** 152 ** This routine returns the index of the mem[] cell that contains 153 ** the maximum rowid counter. 154 ** 155 ** Three consecutive registers are allocated by this routine. The 156 ** first two hold the name of the target table and the maximum rowid 157 ** inserted into the target table, respectively. 158 ** The third holds the rowid in sqlite_sequence where we will 159 ** write back the revised maximum rowid. This routine returns the 160 ** index of the second of these three registers. 161 */ 162 static int autoIncBegin( 163 Parse *pParse, /* Parsing context */ 164 int iDb, /* Index of the database holding pTab */ 165 Table *pTab /* The table we are writing to */ 166 ){ 167 int memId = 0; /* Register holding maximum rowid */ 168 if( pTab->tabFlags & TF_Autoincrement ){ 169 Vdbe *v = pParse->pVdbe; 170 Db *pDb = &pParse->db->aDb[iDb]; 171 int iCur = pParse->nTab; 172 int addr; /* Address of the top of the loop */ 173 assert( v ); 174 pParse->nMem++; /* Holds name of table */ 175 memId = ++pParse->nMem; 176 pParse->nMem++; 177 sqlite3OpenTable(pParse, iCur, iDb, pDb->pSchema->pSeqTab, OP_OpenRead); 178 addr = sqlite3VdbeCurrentAddr(v); 179 sqlite3VdbeAddOp4(v, OP_String8, 0, memId-1, 0, pTab->zName, 0); 180 sqlite3VdbeAddOp2(v, OP_Rewind, iCur, addr+9); 181 sqlite3VdbeAddOp3(v, OP_Column, iCur, 0, memId); 182 sqlite3VdbeAddOp3(v, OP_Ne, memId-1, addr+7, memId); 183 sqlite3VdbeChangeP5(v, SQLITE_JUMPIFNULL); 184 sqlite3VdbeAddOp2(v, OP_Rowid, iCur, memId+1); 185 sqlite3VdbeAddOp3(v, OP_Column, iCur, 1, memId); 186 sqlite3VdbeAddOp2(v, OP_Goto, 0, addr+9); 187 sqlite3VdbeAddOp2(v, OP_Next, iCur, addr+2); 188 sqlite3VdbeAddOp2(v, OP_Integer, 0, memId); 189 sqlite3VdbeAddOp2(v, OP_Close, iCur, 0); 190 } 191 return memId; 192 } 193 194 /* 195 ** Update the maximum rowid for an autoincrement calculation. 196 ** 197 ** This routine should be called when the top of the stack holds a 198 ** new rowid that is about to be inserted. If that new rowid is 199 ** larger than the maximum rowid in the memId memory cell, then the 200 ** memory cell is updated. The stack is unchanged. 201 */ 202 static void autoIncStep(Parse *pParse, int memId, int regRowid){ 203 if( memId>0 ){ 204 sqlite3VdbeAddOp2(pParse->pVdbe, OP_MemMax, memId, regRowid); 205 } 206 } 207 208 /* 209 ** After doing one or more inserts, the maximum rowid is stored 210 ** in reg[memId]. Generate code to write this value back into the 211 ** the sqlite_sequence table. 212 */ 213 static void autoIncEnd( 214 Parse *pParse, /* The parsing context */ 215 int iDb, /* Index of the database holding pTab */ 216 Table *pTab, /* Table we are inserting into */ 217 int memId /* Memory cell holding the maximum rowid */ 218 ){ 219 if( pTab->tabFlags & TF_Autoincrement ){ 220 int iCur = pParse->nTab; 221 Vdbe *v = pParse->pVdbe; 222 Db *pDb = &pParse->db->aDb[iDb]; 223 int j1; 224 int iRec = ++pParse->nMem; /* Memory cell used for record */ 225 226 assert( v ); 227 sqlite3OpenTable(pParse, iCur, iDb, pDb->pSchema->pSeqTab, OP_OpenWrite); 228 j1 = sqlite3VdbeAddOp1(v, OP_NotNull, memId+1); 229 sqlite3VdbeAddOp2(v, OP_NewRowid, iCur, memId+1); 230 sqlite3VdbeJumpHere(v, j1); 231 sqlite3VdbeAddOp3(v, OP_MakeRecord, memId-1, 2, iRec); 232 sqlite3VdbeAddOp3(v, OP_Insert, iCur, iRec, memId+1); 233 sqlite3VdbeChangeP5(v, OPFLAG_APPEND); 234 sqlite3VdbeAddOp1(v, OP_Close, iCur); 235 } 236 } 237 #else 238 /* 239 ** If SQLITE_OMIT_AUTOINCREMENT is defined, then the three routines 240 ** above are all no-ops 241 */ 242 # define autoIncBegin(A,B,C) (0) 243 # define autoIncStep(A,B,C) 244 # define autoIncEnd(A,B,C,D) 245 #endif /* SQLITE_OMIT_AUTOINCREMENT */ 246 247 248 /* Forward declaration */ 249 static int xferOptimization( 250 Parse *pParse, /* Parser context */ 251 Table *pDest, /* The table we are inserting into */ 252 Select *pSelect, /* A SELECT statement to use as the data source */ 253 int onError, /* How to handle constraint errors */ 254 int iDbDest /* The database of pDest */ 255 ); 256 257 /* 258 ** This routine is call to handle SQL of the following forms: 259 ** 260 ** insert into TABLE (IDLIST) values(EXPRLIST) 261 ** insert into TABLE (IDLIST) select 262 ** 263 ** The IDLIST following the table name is always optional. If omitted, 264 ** then a list of all columns for the table is substituted. The IDLIST 265 ** appears in the pColumn parameter. pColumn is NULL if IDLIST is omitted. 266 ** 267 ** The pList parameter holds EXPRLIST in the first form of the INSERT 268 ** statement above, and pSelect is NULL. For the second form, pList is 269 ** NULL and pSelect is a pointer to the select statement used to generate 270 ** data for the insert. 271 ** 272 ** The code generated follows one of four templates. For a simple 273 ** select with data coming from a VALUES clause, the code executes 274 ** once straight down through. Pseudo-code follows (we call this 275 ** the "1st template"): 276 ** 277 ** open write cursor to <table> and its indices 278 ** puts VALUES clause expressions onto the stack 279 ** write the resulting record into <table> 280 ** cleanup 281 ** 282 ** The three remaining templates assume the statement is of the form 283 ** 284 ** INSERT INTO <table> SELECT ... 285 ** 286 ** If the SELECT clause is of the restricted form "SELECT * FROM <table2>" - 287 ** in other words if the SELECT pulls all columns from a single table 288 ** and there is no WHERE or LIMIT or GROUP BY or ORDER BY clauses, and 289 ** if <table2> and <table1> are distinct tables but have identical 290 ** schemas, including all the same indices, then a special optimization 291 ** is invoked that copies raw records from <table2> over to <table1>. 292 ** See the xferOptimization() function for the implementation of this 293 ** template. This is the 2nd template. 294 ** 295 ** open a write cursor to <table> 296 ** open read cursor on <table2> 297 ** transfer all records in <table2> over to <table> 298 ** close cursors 299 ** foreach index on <table> 300 ** open a write cursor on the <table> index 301 ** open a read cursor on the corresponding <table2> index 302 ** transfer all records from the read to the write cursors 303 ** close cursors 304 ** end foreach 305 ** 306 ** The 3rd template is for when the second template does not apply 307 ** and the SELECT clause does not read from <table> at any time. 308 ** The generated code follows this template: 309 ** 310 ** EOF <- 0 311 ** X <- A 312 ** goto B 313 ** A: setup for the SELECT 314 ** loop over the rows in the SELECT 315 ** load values into registers R..R+n 316 ** yield X 317 ** end loop 318 ** cleanup after the SELECT 319 ** EOF <- 1 320 ** yield X 321 ** goto A 322 ** B: open write cursor to <table> and its indices 323 ** C: yield X 324 ** if EOF goto D 325 ** insert the select result into <table> from R..R+n 326 ** goto C 327 ** D: cleanup 328 ** 329 ** The 4th template is used if the insert statement takes its 330 ** values from a SELECT but the data is being inserted into a table 331 ** that is also read as part of the SELECT. In the third form, 332 ** we have to use a intermediate table to store the results of 333 ** the select. The template is like this: 334 ** 335 ** EOF <- 0 336 ** X <- A 337 ** goto B 338 ** A: setup for the SELECT 339 ** loop over the tables in the SELECT 340 ** load value into register R..R+n 341 ** yield X 342 ** end loop 343 ** cleanup after the SELECT 344 ** EOF <- 1 345 ** yield X 346 ** halt-error 347 ** B: open temp table 348 ** L: yield X 349 ** if EOF goto M 350 ** insert row from R..R+n into temp table 351 ** goto L 352 ** M: open write cursor to <table> and its indices 353 ** rewind temp table 354 ** C: loop over rows of intermediate table 355 ** transfer values form intermediate table into <table> 356 ** end loop 357 ** D: cleanup 358 */ 359 void sqlite3Insert( 360 Parse *pParse, /* Parser context */ 361 SrcList *pTabList, /* Name of table into which we are inserting */ 362 ExprList *pList, /* List of values to be inserted */ 363 Select *pSelect, /* A SELECT statement to use as the data source */ 364 IdList *pColumn, /* Column names corresponding to IDLIST. */ 365 int onError /* How to handle constraint errors */ 366 ){ 367 sqlite3 *db; /* The main database structure */ 368 Table *pTab; /* The table to insert into. aka TABLE */ 369 char *zTab; /* Name of the table into which we are inserting */ 370 const char *zDb; /* Name of the database holding this table */ 371 int i, j, idx; /* Loop counters */ 372 Vdbe *v; /* Generate code into this virtual machine */ 373 Index *pIdx; /* For looping over indices of the table */ 374 int nColumn; /* Number of columns in the data */ 375 int nHidden = 0; /* Number of hidden columns if TABLE is virtual */ 376 int baseCur = 0; /* VDBE Cursor number for pTab */ 377 int keyColumn = -1; /* Column that is the INTEGER PRIMARY KEY */ 378 int endOfLoop; /* Label for the end of the insertion loop */ 379 int useTempTable = 0; /* Store SELECT results in intermediate table */ 380 int srcTab = 0; /* Data comes from this temporary cursor if >=0 */ 381 int addrInsTop = 0; /* Jump to label "D" */ 382 int addrCont = 0; /* Top of insert loop. Label "C" in templates 3 and 4 */ 383 int addrSelect = 0; /* Address of coroutine that implements the SELECT */ 384 SelectDest dest; /* Destination for SELECT on rhs of INSERT */ 385 int newIdx = -1; /* Cursor for the NEW pseudo-table */ 386 int iDb; /* Index of database holding TABLE */ 387 Db *pDb; /* The database containing table being inserted into */ 388 int appendFlag = 0; /* True if the insert is likely to be an append */ 389 390 /* Register allocations */ 391 int regFromSelect = 0;/* Base register for data coming from SELECT */ 392 int regAutoinc = 0; /* Register holding the AUTOINCREMENT counter */ 393 int regRowCount = 0; /* Memory cell used for the row counter */ 394 int regIns; /* Block of regs holding rowid+data being inserted */ 395 int regRowid; /* registers holding insert rowid */ 396 int regData; /* register holding first column to insert */ 397 int regRecord; /* Holds the assemblied row record */ 398 int regEof = 0; /* Register recording end of SELECT data */ 399 int *aRegIdx = 0; /* One register allocated to each index */ 400 401 402 #ifndef SQLITE_OMIT_TRIGGER 403 int isView; /* True if attempting to insert into a view */ 404 int triggers_exist = 0; /* True if there are FOR EACH ROW triggers */ 405 #endif 406 407 db = pParse->db; 408 memset(&dest, 0, sizeof(dest)); 409 if( pParse->nErr || db->mallocFailed ){ 410 goto insert_cleanup; 411 } 412 413 /* Locate the table into which we will be inserting new information. 414 */ 415 assert( pTabList->nSrc==1 ); 416 zTab = pTabList->a[0].zName; 417 if( zTab==0 ) goto insert_cleanup; 418 pTab = sqlite3SrcListLookup(pParse, pTabList); 419 if( pTab==0 ){ 420 goto insert_cleanup; 421 } 422 iDb = sqlite3SchemaToIndex(db, pTab->pSchema); 423 assert( iDb<db->nDb ); 424 pDb = &db->aDb[iDb]; 425 zDb = pDb->zName; 426 if( sqlite3AuthCheck(pParse, SQLITE_INSERT, pTab->zName, 0, zDb) ){ 427 goto insert_cleanup; 428 } 429 430 /* Figure out if we have any triggers and if the table being 431 ** inserted into is a view 432 */ 433 #ifndef SQLITE_OMIT_TRIGGER 434 triggers_exist = sqlite3TriggersExist(pTab, TK_INSERT, 0); 435 isView = pTab->pSelect!=0; 436 #else 437 # define triggers_exist 0 438 # define isView 0 439 #endif 440 #ifdef SQLITE_OMIT_VIEW 441 # undef isView 442 # define isView 0 443 #endif 444 445 /* Ensure that: 446 * (a) the table is not read-only, 447 * (b) that if it is a view then ON INSERT triggers exist 448 */ 449 if( sqlite3IsReadOnly(pParse, pTab, triggers_exist) ){ 450 goto insert_cleanup; 451 } 452 assert( pTab!=0 ); 453 454 /* If pTab is really a view, make sure it has been initialized. 455 ** ViewGetColumnNames() is a no-op if pTab is not a view (or virtual 456 ** module table). 457 */ 458 if( sqlite3ViewGetColumnNames(pParse, pTab) ){ 459 goto insert_cleanup; 460 } 461 462 /* Allocate a VDBE 463 */ 464 v = sqlite3GetVdbe(pParse); 465 if( v==0 ) goto insert_cleanup; 466 if( pParse->nested==0 ) sqlite3VdbeCountChanges(v); 467 sqlite3BeginWriteOperation(pParse, pSelect || triggers_exist, iDb); 468 469 /* if there are row triggers, allocate a temp table for new.* references. */ 470 if( triggers_exist ){ 471 newIdx = pParse->nTab++; 472 } 473 474 #ifndef SQLITE_OMIT_XFER_OPT 475 /* If the statement is of the form 476 ** 477 ** INSERT INTO <table1> SELECT * FROM <table2>; 478 ** 479 ** Then special optimizations can be applied that make the transfer 480 ** very fast and which reduce fragmentation of indices. 481 ** 482 ** This is the 2nd template. 483 */ 484 if( pColumn==0 && xferOptimization(pParse, pTab, pSelect, onError, iDb) ){ 485 assert( !triggers_exist ); 486 assert( pList==0 ); 487 goto insert_cleanup; 488 } 489 #endif /* SQLITE_OMIT_XFER_OPT */ 490 491 /* If this is an AUTOINCREMENT table, look up the sequence number in the 492 ** sqlite_sequence table and store it in memory cell regAutoinc. 493 */ 494 regAutoinc = autoIncBegin(pParse, iDb, pTab); 495 496 /* Figure out how many columns of data are supplied. If the data 497 ** is coming from a SELECT statement, then generate a co-routine that 498 ** produces a single row of the SELECT on each invocation. The 499 ** co-routine is the common header to the 3rd and 4th templates. 500 */ 501 if( pSelect ){ 502 /* Data is coming from a SELECT. Generate code to implement that SELECT 503 ** as a co-routine. The code is common to both the 3rd and 4th 504 ** templates: 505 ** 506 ** EOF <- 0 507 ** X <- A 508 ** goto B 509 ** A: setup for the SELECT 510 ** loop over the tables in the SELECT 511 ** load value into register R..R+n 512 ** yield X 513 ** end loop 514 ** cleanup after the SELECT 515 ** EOF <- 1 516 ** yield X 517 ** halt-error 518 ** 519 ** On each invocation of the co-routine, it puts a single row of the 520 ** SELECT result into registers dest.iMem...dest.iMem+dest.nMem-1. 521 ** (These output registers are allocated by sqlite3Select().) When 522 ** the SELECT completes, it sets the EOF flag stored in regEof. 523 */ 524 int rc, j1; 525 526 regEof = ++pParse->nMem; 527 sqlite3VdbeAddOp2(v, OP_Integer, 0, regEof); /* EOF <- 0 */ 528 VdbeComment((v, "SELECT eof flag")); 529 sqlite3SelectDestInit(&dest, SRT_Coroutine, ++pParse->nMem); 530 addrSelect = sqlite3VdbeCurrentAddr(v)+2; 531 sqlite3VdbeAddOp2(v, OP_Integer, addrSelect-1, dest.iParm); 532 j1 = sqlite3VdbeAddOp2(v, OP_Goto, 0, 0); 533 VdbeComment((v, "Jump over SELECT coroutine")); 534 535 /* Resolve the expressions in the SELECT statement and execute it. */ 536 rc = sqlite3Select(pParse, pSelect, &dest); 537 if( rc || pParse->nErr || db->mallocFailed ){ 538 goto insert_cleanup; 539 } 540 sqlite3VdbeAddOp2(v, OP_Integer, 1, regEof); /* EOF <- 1 */ 541 sqlite3VdbeAddOp1(v, OP_Yield, dest.iParm); /* yield X */ 542 sqlite3VdbeAddOp2(v, OP_Halt, SQLITE_INTERNAL, OE_Abort); 543 VdbeComment((v, "End of SELECT coroutine")); 544 sqlite3VdbeJumpHere(v, j1); /* label B: */ 545 546 regFromSelect = dest.iMem; 547 assert( pSelect->pEList ); 548 nColumn = pSelect->pEList->nExpr; 549 assert( dest.nMem==nColumn ); 550 551 /* Set useTempTable to TRUE if the result of the SELECT statement 552 ** should be written into a temporary table (template 4). Set to 553 ** FALSE if each* row of the SELECT can be written directly into 554 ** the destination table (template 3). 555 ** 556 ** A temp table must be used if the table being updated is also one 557 ** of the tables being read by the SELECT statement. Also use a 558 ** temp table in the case of row triggers. 559 */ 560 if( triggers_exist || readsTable(v, addrSelect, iDb, pTab) ){ 561 useTempTable = 1; 562 } 563 564 if( useTempTable ){ 565 /* Invoke the coroutine to extract information from the SELECT 566 ** and add it to a transient table srcTab. The code generated 567 ** here is from the 4th template: 568 ** 569 ** B: open temp table 570 ** L: yield X 571 ** if EOF goto M 572 ** insert row from R..R+n into temp table 573 ** goto L 574 ** M: ... 575 */ 576 int regRec; /* Register to hold packed record */ 577 int regTempRowid; /* Register to hold temp table ROWID */ 578 int addrTop; /* Label "L" */ 579 int addrIf; /* Address of jump to M */ 580 581 srcTab = pParse->nTab++; 582 regRec = sqlite3GetTempReg(pParse); 583 regTempRowid = sqlite3GetTempReg(pParse); 584 sqlite3VdbeAddOp2(v, OP_OpenEphemeral, srcTab, nColumn); 585 addrTop = sqlite3VdbeAddOp1(v, OP_Yield, dest.iParm); 586 addrIf = sqlite3VdbeAddOp1(v, OP_If, regEof); 587 sqlite3VdbeAddOp3(v, OP_MakeRecord, regFromSelect, nColumn, regRec); 588 sqlite3VdbeAddOp2(v, OP_NewRowid, srcTab, regTempRowid); 589 sqlite3VdbeAddOp3(v, OP_Insert, srcTab, regRec, regTempRowid); 590 sqlite3VdbeAddOp2(v, OP_Goto, 0, addrTop); 591 sqlite3VdbeJumpHere(v, addrIf); 592 sqlite3ReleaseTempReg(pParse, regRec); 593 sqlite3ReleaseTempReg(pParse, regTempRowid); 594 } 595 }else{ 596 /* This is the case if the data for the INSERT is coming from a VALUES 597 ** clause 598 */ 599 NameContext sNC; 600 memset(&sNC, 0, sizeof(sNC)); 601 sNC.pParse = pParse; 602 srcTab = -1; 603 assert( useTempTable==0 ); 604 nColumn = pList ? pList->nExpr : 0; 605 for(i=0; i<nColumn; i++){ 606 if( sqlite3ResolveExprNames(&sNC, pList->a[i].pExpr) ){ 607 goto insert_cleanup; 608 } 609 } 610 } 611 612 /* Make sure the number of columns in the source data matches the number 613 ** of columns to be inserted into the table. 614 */ 615 if( IsVirtual(pTab) ){ 616 for(i=0; i<pTab->nCol; i++){ 617 nHidden += (IsHiddenColumn(&pTab->aCol[i]) ? 1 : 0); 618 } 619 } 620 if( pColumn==0 && nColumn && nColumn!=(pTab->nCol-nHidden) ){ 621 sqlite3ErrorMsg(pParse, 622 "table %S has %d columns but %d values were supplied", 623 pTabList, 0, pTab->nCol, nColumn); 624 goto insert_cleanup; 625 } 626 if( pColumn!=0 && nColumn!=pColumn->nId ){ 627 sqlite3ErrorMsg(pParse, "%d values for %d columns", nColumn, pColumn->nId); 628 goto insert_cleanup; 629 } 630 631 /* If the INSERT statement included an IDLIST term, then make sure 632 ** all elements of the IDLIST really are columns of the table and 633 ** remember the column indices. 634 ** 635 ** If the table has an INTEGER PRIMARY KEY column and that column 636 ** is named in the IDLIST, then record in the keyColumn variable 637 ** the index into IDLIST of the primary key column. keyColumn is 638 ** the index of the primary key as it appears in IDLIST, not as 639 ** is appears in the original table. (The index of the primary 640 ** key in the original table is pTab->iPKey.) 641 */ 642 if( pColumn ){ 643 for(i=0; i<pColumn->nId; i++){ 644 pColumn->a[i].idx = -1; 645 } 646 for(i=0; i<pColumn->nId; i++){ 647 for(j=0; j<pTab->nCol; j++){ 648 if( sqlite3StrICmp(pColumn->a[i].zName, pTab->aCol[j].zName)==0 ){ 649 pColumn->a[i].idx = j; 650 if( j==pTab->iPKey ){ 651 keyColumn = i; 652 } 653 break; 654 } 655 } 656 if( j>=pTab->nCol ){ 657 if( sqlite3IsRowid(pColumn->a[i].zName) ){ 658 keyColumn = i; 659 }else{ 660 sqlite3ErrorMsg(pParse, "table %S has no column named %s", 661 pTabList, 0, pColumn->a[i].zName); 662 pParse->nErr++; 663 goto insert_cleanup; 664 } 665 } 666 } 667 } 668 669 /* If there is no IDLIST term but the table has an integer primary 670 ** key, the set the keyColumn variable to the primary key column index 671 ** in the original table definition. 672 */ 673 if( pColumn==0 && nColumn>0 ){ 674 keyColumn = pTab->iPKey; 675 } 676 677 /* Open the temp table for FOR EACH ROW triggers 678 */ 679 if( triggers_exist ){ 680 sqlite3VdbeAddOp2(v, OP_SetNumColumns, 0, pTab->nCol); 681 sqlite3VdbeAddOp2(v, OP_OpenPseudo, newIdx, 0); 682 } 683 684 /* Initialize the count of rows to be inserted 685 */ 686 if( db->flags & SQLITE_CountRows ){ 687 regRowCount = ++pParse->nMem; 688 sqlite3VdbeAddOp2(v, OP_Integer, 0, regRowCount); 689 } 690 691 /* If this is not a view, open the table and and all indices */ 692 if( !isView ){ 693 int nIdx; 694 695 baseCur = pParse->nTab; 696 nIdx = sqlite3OpenTableAndIndices(pParse, pTab, baseCur, OP_OpenWrite); 697 aRegIdx = sqlite3DbMallocRaw(db, sizeof(int)*(nIdx+1)); 698 if( aRegIdx==0 ){ 699 goto insert_cleanup; 700 } 701 for(i=0; i<nIdx; i++){ 702 aRegIdx[i] = ++pParse->nMem; 703 } 704 } 705 706 /* This is the top of the main insertion loop */ 707 if( useTempTable ){ 708 /* This block codes the top of loop only. The complete loop is the 709 ** following pseudocode (template 4): 710 ** 711 ** rewind temp table 712 ** C: loop over rows of intermediate table 713 ** transfer values form intermediate table into <table> 714 ** end loop 715 ** D: ... 716 */ 717 addrInsTop = sqlite3VdbeAddOp1(v, OP_Rewind, srcTab); 718 addrCont = sqlite3VdbeCurrentAddr(v); 719 }else if( pSelect ){ 720 /* This block codes the top of loop only. The complete loop is the 721 ** following pseudocode (template 3): 722 ** 723 ** C: yield X 724 ** if EOF goto D 725 ** insert the select result into <table> from R..R+n 726 ** goto C 727 ** D: ... 728 */ 729 addrCont = sqlite3VdbeAddOp1(v, OP_Yield, dest.iParm); 730 addrInsTop = sqlite3VdbeAddOp1(v, OP_If, regEof); 731 } 732 733 /* Allocate registers for holding the rowid of the new row, 734 ** the content of the new row, and the assemblied row record. 735 */ 736 regRecord = ++pParse->nMem; 737 regRowid = regIns = pParse->nMem+1; 738 pParse->nMem += pTab->nCol + 1; 739 if( IsVirtual(pTab) ){ 740 regRowid++; 741 pParse->nMem++; 742 } 743 regData = regRowid+1; 744 745 /* Run the BEFORE and INSTEAD OF triggers, if there are any 746 */ 747 endOfLoop = sqlite3VdbeMakeLabel(v); 748 if( triggers_exist & TRIGGER_BEFORE ){ 749 int regTrigRowid; 750 int regCols; 751 int regRec; 752 753 /* build the NEW.* reference row. Note that if there is an INTEGER 754 ** PRIMARY KEY into which a NULL is being inserted, that NULL will be 755 ** translated into a unique ID for the row. But on a BEFORE trigger, 756 ** we do not know what the unique ID will be (because the insert has 757 ** not happened yet) so we substitute a rowid of -1 758 */ 759 regTrigRowid = sqlite3GetTempReg(pParse); 760 if( keyColumn<0 ){ 761 sqlite3VdbeAddOp2(v, OP_Integer, -1, regTrigRowid); 762 }else if( useTempTable ){ 763 sqlite3VdbeAddOp3(v, OP_Column, srcTab, keyColumn, regTrigRowid); 764 }else{ 765 int j1; 766 assert( pSelect==0 ); /* Otherwise useTempTable is true */ 767 sqlite3ExprCode(pParse, pList->a[keyColumn].pExpr, regTrigRowid); 768 j1 = sqlite3VdbeAddOp1(v, OP_NotNull, regTrigRowid); 769 sqlite3VdbeAddOp2(v, OP_Integer, -1, regTrigRowid); 770 sqlite3VdbeJumpHere(v, j1); 771 sqlite3VdbeAddOp1(v, OP_MustBeInt, regTrigRowid); 772 } 773 774 /* Cannot have triggers on a virtual table. If it were possible, 775 ** this block would have to account for hidden column. 776 */ 777 assert(!IsVirtual(pTab)); 778 779 /* Create the new column data 780 */ 781 regCols = sqlite3GetTempRange(pParse, pTab->nCol); 782 for(i=0; i<pTab->nCol; i++){ 783 if( pColumn==0 ){ 784 j = i; 785 }else{ 786 for(j=0; j<pColumn->nId; j++){ 787 if( pColumn->a[j].idx==i ) break; 788 } 789 } 790 if( pColumn && j>=pColumn->nId ){ 791 sqlite3ExprCode(pParse, pTab->aCol[i].pDflt, regCols+i); 792 }else if( useTempTable ){ 793 sqlite3VdbeAddOp3(v, OP_Column, srcTab, j, regCols+i); 794 }else{ 795 assert( pSelect==0 ); /* Otherwise useTempTable is true */ 796 sqlite3ExprCodeAndCache(pParse, pList->a[j].pExpr, regCols+i); 797 } 798 } 799 regRec = sqlite3GetTempReg(pParse); 800 sqlite3VdbeAddOp3(v, OP_MakeRecord, regCols, pTab->nCol, regRec); 801 802 /* If this is an INSERT on a view with an INSTEAD OF INSERT trigger, 803 ** do not attempt any conversions before assembling the record. 804 ** If this is a real table, attempt conversions as required by the 805 ** table column affinities. 806 */ 807 if( !isView ){ 808 sqlite3TableAffinityStr(v, pTab); 809 } 810 sqlite3VdbeAddOp3(v, OP_Insert, newIdx, regRec, regTrigRowid); 811 sqlite3ReleaseTempReg(pParse, regRec); 812 sqlite3ReleaseTempReg(pParse, regTrigRowid); 813 sqlite3ReleaseTempRange(pParse, regCols, pTab->nCol); 814 815 /* Fire BEFORE or INSTEAD OF triggers */ 816 if( sqlite3CodeRowTrigger(pParse, TK_INSERT, 0, TRIGGER_BEFORE, pTab, 817 newIdx, -1, onError, endOfLoop, 0, 0) ){ 818 goto insert_cleanup; 819 } 820 } 821 822 /* Push the record number for the new entry onto the stack. The 823 ** record number is a randomly generate integer created by NewRowid 824 ** except when the table has an INTEGER PRIMARY KEY column, in which 825 ** case the record number is the same as that column. 826 */ 827 if( !isView ){ 828 if( IsVirtual(pTab) ){ 829 /* The row that the VUpdate opcode will delete: none */ 830 sqlite3VdbeAddOp2(v, OP_Null, 0, regIns); 831 } 832 if( keyColumn>=0 ){ 833 if( useTempTable ){ 834 sqlite3VdbeAddOp3(v, OP_Column, srcTab, keyColumn, regRowid); 835 }else if( pSelect ){ 836 sqlite3VdbeAddOp2(v, OP_SCopy, regFromSelect+keyColumn, regRowid); 837 }else{ 838 VdbeOp *pOp; 839 sqlite3ExprCode(pParse, pList->a[keyColumn].pExpr, regRowid); 840 pOp = sqlite3VdbeGetOp(v, sqlite3VdbeCurrentAddr(v) - 1); 841 if( pOp && pOp->opcode==OP_Null && !IsVirtual(pTab) ){ 842 appendFlag = 1; 843 pOp->opcode = OP_NewRowid; 844 pOp->p1 = baseCur; 845 pOp->p2 = regRowid; 846 pOp->p3 = regAutoinc; 847 } 848 } 849 /* If the PRIMARY KEY expression is NULL, then use OP_NewRowid 850 ** to generate a unique primary key value. 851 */ 852 if( !appendFlag ){ 853 int j1; 854 if( !IsVirtual(pTab) ){ 855 j1 = sqlite3VdbeAddOp1(v, OP_NotNull, regRowid); 856 sqlite3VdbeAddOp3(v, OP_NewRowid, baseCur, regRowid, regAutoinc); 857 sqlite3VdbeJumpHere(v, j1); 858 }else{ 859 j1 = sqlite3VdbeCurrentAddr(v); 860 sqlite3VdbeAddOp2(v, OP_IsNull, regRowid, j1+2); 861 } 862 sqlite3VdbeAddOp1(v, OP_MustBeInt, regRowid); 863 } 864 }else if( IsVirtual(pTab) ){ 865 sqlite3VdbeAddOp2(v, OP_Null, 0, regRowid); 866 }else{ 867 sqlite3VdbeAddOp3(v, OP_NewRowid, baseCur, regRowid, regAutoinc); 868 appendFlag = 1; 869 } 870 autoIncStep(pParse, regAutoinc, regRowid); 871 872 /* Push onto the stack, data for all columns of the new entry, beginning 873 ** with the first column. 874 */ 875 nHidden = 0; 876 for(i=0; i<pTab->nCol; i++){ 877 int iRegStore = regRowid+1+i; 878 if( i==pTab->iPKey ){ 879 /* The value of the INTEGER PRIMARY KEY column is always a NULL. 880 ** Whenever this column is read, the record number will be substituted 881 ** in its place. So will fill this column with a NULL to avoid 882 ** taking up data space with information that will never be used. */ 883 sqlite3VdbeAddOp2(v, OP_Null, 0, iRegStore); 884 continue; 885 } 886 if( pColumn==0 ){ 887 if( IsHiddenColumn(&pTab->aCol[i]) ){ 888 assert( IsVirtual(pTab) ); 889 j = -1; 890 nHidden++; 891 }else{ 892 j = i - nHidden; 893 } 894 }else{ 895 for(j=0; j<pColumn->nId; j++){ 896 if( pColumn->a[j].idx==i ) break; 897 } 898 } 899 if( j<0 || nColumn==0 || (pColumn && j>=pColumn->nId) ){ 900 sqlite3ExprCode(pParse, pTab->aCol[i].pDflt, iRegStore); 901 }else if( useTempTable ){ 902 sqlite3VdbeAddOp3(v, OP_Column, srcTab, j, iRegStore); 903 }else if( pSelect ){ 904 sqlite3VdbeAddOp2(v, OP_SCopy, regFromSelect+j, iRegStore); 905 }else{ 906 sqlite3ExprCode(pParse, pList->a[j].pExpr, iRegStore); 907 } 908 } 909 910 /* Generate code to check constraints and generate index keys and 911 ** do the insertion. 912 */ 913 #ifndef SQLITE_OMIT_VIRTUALTABLE 914 if( IsVirtual(pTab) ){ 915 sqlite3VtabMakeWritable(pParse, pTab); 916 sqlite3VdbeAddOp4(v, OP_VUpdate, 1, pTab->nCol+2, regIns, 917 (const char*)pTab->pVtab, P4_VTAB); 918 }else 919 #endif 920 { 921 sqlite3GenerateConstraintChecks( 922 pParse, 923 pTab, 924 baseCur, 925 regIns, 926 aRegIdx, 927 keyColumn>=0, 928 0, 929 onError, 930 endOfLoop 931 ); 932 sqlite3CompleteInsertion( 933 pParse, 934 pTab, 935 baseCur, 936 regIns, 937 aRegIdx, 938 0, 939 (triggers_exist & TRIGGER_AFTER)!=0 ? newIdx : -1, 940 appendFlag 941 ); 942 } 943 } 944 945 /* Update the count of rows that are inserted 946 */ 947 if( (db->flags & SQLITE_CountRows)!=0 ){ 948 sqlite3VdbeAddOp2(v, OP_AddImm, regRowCount, 1); 949 } 950 951 if( triggers_exist ){ 952 /* Code AFTER triggers */ 953 if( sqlite3CodeRowTrigger(pParse, TK_INSERT, 0, TRIGGER_AFTER, pTab, 954 newIdx, -1, onError, endOfLoop, 0, 0) ){ 955 goto insert_cleanup; 956 } 957 } 958 959 /* The bottom of the main insertion loop, if the data source 960 ** is a SELECT statement. 961 */ 962 sqlite3VdbeResolveLabel(v, endOfLoop); 963 if( useTempTable ){ 964 sqlite3VdbeAddOp2(v, OP_Next, srcTab, addrCont); 965 sqlite3VdbeJumpHere(v, addrInsTop); 966 sqlite3VdbeAddOp1(v, OP_Close, srcTab); 967 }else if( pSelect ){ 968 sqlite3VdbeAddOp2(v, OP_Goto, 0, addrCont); 969 sqlite3VdbeJumpHere(v, addrInsTop); 970 } 971 972 if( !IsVirtual(pTab) && !isView ){ 973 /* Close all tables opened */ 974 sqlite3VdbeAddOp1(v, OP_Close, baseCur); 975 for(idx=1, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, idx++){ 976 sqlite3VdbeAddOp1(v, OP_Close, idx+baseCur); 977 } 978 } 979 980 /* Update the sqlite_sequence table by storing the content of the 981 ** counter value in memory regAutoinc back into the sqlite_sequence 982 ** table. 983 */ 984 autoIncEnd(pParse, iDb, pTab, regAutoinc); 985 986 /* 987 ** Return the number of rows inserted. If this routine is 988 ** generating code because of a call to sqlite3NestedParse(), do not 989 ** invoke the callback function. 990 */ 991 if( db->flags & SQLITE_CountRows && pParse->nested==0 && !pParse->trigStack ){ 992 sqlite3VdbeAddOp2(v, OP_ResultRow, regRowCount, 1); 993 sqlite3VdbeSetNumCols(v, 1); 994 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "rows inserted", SQLITE_STATIC); 995 } 996 997 insert_cleanup: 998 sqlite3SrcListDelete(db, pTabList); 999 sqlite3ExprListDelete(db, pList); 1000 sqlite3SelectDelete(db, pSelect); 1001 sqlite3IdListDelete(db, pColumn); 1002 sqlite3DbFree(db, aRegIdx); 1003 } 1004 1005 /* 1006 ** Generate code to do constraint checks prior to an INSERT or an UPDATE. 1007 ** 1008 ** The input is a range of consecutive registers as follows: 1009 ** 1010 ** 1. The rowid of the row to be updated before the update. This 1011 ** value is omitted unless we are doing an UPDATE that involves a 1012 ** change to the record number or writing to a virtual table. 1013 ** 1014 ** 2. The rowid of the row after the update. 1015 ** 1016 ** 3. The data in the first column of the entry after the update. 1017 ** 1018 ** i. Data from middle columns... 1019 ** 1020 ** N. The data in the last column of the entry after the update. 1021 ** 1022 ** The regRowid parameter is the index of the register containing (2). 1023 ** 1024 ** The old rowid shown as entry (1) above is omitted unless both isUpdate 1025 ** and rowidChng are 1. isUpdate is true for UPDATEs and false for 1026 ** INSERTs. RowidChng means that the new rowid is explicitly specified by 1027 ** the update or insert statement. If rowidChng is false, it means that 1028 ** the rowid is computed automatically in an insert or that the rowid value 1029 ** is not modified by the update. 1030 ** 1031 ** The code generated by this routine store new index entries into 1032 ** registers identified by aRegIdx[]. No index entry is created for 1033 ** indices where aRegIdx[i]==0. The order of indices in aRegIdx[] is 1034 ** the same as the order of indices on the linked list of indices 1035 ** attached to the table. 1036 ** 1037 ** This routine also generates code to check constraints. NOT NULL, 1038 ** CHECK, and UNIQUE constraints are all checked. If a constraint fails, 1039 ** then the appropriate action is performed. There are five possible 1040 ** actions: ROLLBACK, ABORT, FAIL, REPLACE, and IGNORE. 1041 ** 1042 ** Constraint type Action What Happens 1043 ** --------------- ---------- ---------------------------------------- 1044 ** any ROLLBACK The current transaction is rolled back and 1045 ** sqlite3_exec() returns immediately with a 1046 ** return code of SQLITE_CONSTRAINT. 1047 ** 1048 ** any ABORT Back out changes from the current command 1049 ** only (do not do a complete rollback) then 1050 ** cause sqlite3_exec() to return immediately 1051 ** with SQLITE_CONSTRAINT. 1052 ** 1053 ** any FAIL Sqlite_exec() returns immediately with a 1054 ** return code of SQLITE_CONSTRAINT. The 1055 ** transaction is not rolled back and any 1056 ** prior changes are retained. 1057 ** 1058 ** any IGNORE The record number and data is popped from 1059 ** the stack and there is an immediate jump 1060 ** to label ignoreDest. 1061 ** 1062 ** NOT NULL REPLACE The NULL value is replace by the default 1063 ** value for that column. If the default value 1064 ** is NULL, the action is the same as ABORT. 1065 ** 1066 ** UNIQUE REPLACE The other row that conflicts with the row 1067 ** being inserted is removed. 1068 ** 1069 ** CHECK REPLACE Illegal. The results in an exception. 1070 ** 1071 ** Which action to take is determined by the overrideError parameter. 1072 ** Or if overrideError==OE_Default, then the pParse->onError parameter 1073 ** is used. Or if pParse->onError==OE_Default then the onError value 1074 ** for the constraint is used. 1075 ** 1076 ** The calling routine must open a read/write cursor for pTab with 1077 ** cursor number "baseCur". All indices of pTab must also have open 1078 ** read/write cursors with cursor number baseCur+i for the i-th cursor. 1079 ** Except, if there is no possibility of a REPLACE action then 1080 ** cursors do not need to be open for indices where aRegIdx[i]==0. 1081 */ 1082 void sqlite3GenerateConstraintChecks( 1083 Parse *pParse, /* The parser context */ 1084 Table *pTab, /* the table into which we are inserting */ 1085 int baseCur, /* Index of a read/write cursor pointing at pTab */ 1086 int regRowid, /* Index of the range of input registers */ 1087 int *aRegIdx, /* Register used by each index. 0 for unused indices */ 1088 int rowidChng, /* True if the rowid might collide with existing entry */ 1089 int isUpdate, /* True for UPDATE, False for INSERT */ 1090 int overrideError, /* Override onError to this if not OE_Default */ 1091 int ignoreDest /* Jump to this label on an OE_Ignore resolution */ 1092 ){ 1093 int i; 1094 Vdbe *v; 1095 int nCol; 1096 int onError; 1097 int j1; /* Addresss of jump instruction */ 1098 int j2 = 0, j3; /* Addresses of jump instructions */ 1099 int regData; /* Register containing first data column */ 1100 int iCur; 1101 Index *pIdx; 1102 int seenReplace = 0; 1103 int hasTwoRowids = (isUpdate && rowidChng); 1104 1105 v = sqlite3GetVdbe(pParse); 1106 assert( v!=0 ); 1107 assert( pTab->pSelect==0 ); /* This table is not a VIEW */ 1108 nCol = pTab->nCol; 1109 regData = regRowid + 1; 1110 1111 1112 /* Test all NOT NULL constraints. 1113 */ 1114 for(i=0; i<nCol; i++){ 1115 if( i==pTab->iPKey ){ 1116 continue; 1117 } 1118 onError = pTab->aCol[i].notNull; 1119 if( onError==OE_None ) continue; 1120 if( overrideError!=OE_Default ){ 1121 onError = overrideError; 1122 }else if( onError==OE_Default ){ 1123 onError = OE_Abort; 1124 } 1125 if( onError==OE_Replace && pTab->aCol[i].pDflt==0 ){ 1126 onError = OE_Abort; 1127 } 1128 j1 = sqlite3VdbeAddOp1(v, OP_NotNull, regData+i); 1129 assert( onError==OE_Rollback || onError==OE_Abort || onError==OE_Fail 1130 || onError==OE_Ignore || onError==OE_Replace ); 1131 switch( onError ){ 1132 case OE_Rollback: 1133 case OE_Abort: 1134 case OE_Fail: { 1135 char *zMsg; 1136 sqlite3VdbeAddOp2(v, OP_Halt, SQLITE_CONSTRAINT, onError); 1137 zMsg = sqlite3MPrintf(pParse->db, "%s.%s may not be NULL", 1138 pTab->zName, pTab->aCol[i].zName); 1139 sqlite3VdbeChangeP4(v, -1, zMsg, P4_DYNAMIC); 1140 break; 1141 } 1142 case OE_Ignore: { 1143 sqlite3VdbeAddOp2(v, OP_Goto, 0, ignoreDest); 1144 break; 1145 } 1146 case OE_Replace: { 1147 sqlite3ExprCode(pParse, pTab->aCol[i].pDflt, regData+i); 1148 break; 1149 } 1150 } 1151 sqlite3VdbeJumpHere(v, j1); 1152 } 1153 1154 /* Test all CHECK constraints 1155 */ 1156 #ifndef SQLITE_OMIT_CHECK 1157 if( pTab->pCheck && (pParse->db->flags & SQLITE_IgnoreChecks)==0 ){ 1158 int allOk = sqlite3VdbeMakeLabel(v); 1159 pParse->ckBase = regData; 1160 sqlite3ExprIfTrue(pParse, pTab->pCheck, allOk, SQLITE_JUMPIFNULL); 1161 onError = overrideError!=OE_Default ? overrideError : OE_Abort; 1162 if( onError==OE_Ignore ){ 1163 sqlite3VdbeAddOp2(v, OP_Goto, 0, ignoreDest); 1164 }else{ 1165 sqlite3VdbeAddOp2(v, OP_Halt, SQLITE_CONSTRAINT, onError); 1166 } 1167 sqlite3VdbeResolveLabel(v, allOk); 1168 } 1169 #endif /* !defined(SQLITE_OMIT_CHECK) */ 1170 1171 /* If we have an INTEGER PRIMARY KEY, make sure the primary key 1172 ** of the new record does not previously exist. Except, if this 1173 ** is an UPDATE and the primary key is not changing, that is OK. 1174 */ 1175 if( rowidChng ){ 1176 onError = pTab->keyConf; 1177 if( overrideError!=OE_Default ){ 1178 onError = overrideError; 1179 }else if( onError==OE_Default ){ 1180 onError = OE_Abort; 1181 } 1182 1183 if( onError!=OE_Replace || pTab->pIndex ){ 1184 if( isUpdate ){ 1185 j2 = sqlite3VdbeAddOp3(v, OP_Eq, regRowid, 0, regRowid-1); 1186 } 1187 j3 = sqlite3VdbeAddOp3(v, OP_NotExists, baseCur, 0, regRowid); 1188 switch( onError ){ 1189 default: { 1190 onError = OE_Abort; 1191 /* Fall thru into the next case */ 1192 } 1193 case OE_Rollback: 1194 case OE_Abort: 1195 case OE_Fail: { 1196 sqlite3VdbeAddOp4(v, OP_Halt, SQLITE_CONSTRAINT, onError, 0, 1197 "PRIMARY KEY must be unique", P4_STATIC); 1198 break; 1199 } 1200 case OE_Replace: { 1201 sqlite3GenerateRowIndexDelete(pParse, pTab, baseCur, 0); 1202 seenReplace = 1; 1203 break; 1204 } 1205 case OE_Ignore: { 1206 assert( seenReplace==0 ); 1207 sqlite3VdbeAddOp2(v, OP_Goto, 0, ignoreDest); 1208 break; 1209 } 1210 } 1211 sqlite3VdbeJumpHere(v, j3); 1212 if( isUpdate ){ 1213 sqlite3VdbeJumpHere(v, j2); 1214 } 1215 } 1216 } 1217 1218 /* Test all UNIQUE constraints by creating entries for each UNIQUE 1219 ** index and making sure that duplicate entries do not already exist. 1220 ** Add the new records to the indices as we go. 1221 */ 1222 for(iCur=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, iCur++){ 1223 int regIdx; 1224 int regR; 1225 1226 if( aRegIdx[iCur]==0 ) continue; /* Skip unused indices */ 1227 1228 /* Create a key for accessing the index entry */ 1229 regIdx = sqlite3GetTempRange(pParse, pIdx->nColumn+1); 1230 for(i=0; i<pIdx->nColumn; i++){ 1231 int idx = pIdx->aiColumn[i]; 1232 if( idx==pTab->iPKey ){ 1233 sqlite3VdbeAddOp2(v, OP_SCopy, regRowid, regIdx+i); 1234 }else{ 1235 sqlite3VdbeAddOp2(v, OP_SCopy, regData+idx, regIdx+i); 1236 } 1237 } 1238 sqlite3VdbeAddOp2(v, OP_SCopy, regRowid, regIdx+i); 1239 sqlite3VdbeAddOp3(v, OP_MakeRecord, regIdx, pIdx->nColumn+1, aRegIdx[iCur]); 1240 sqlite3IndexAffinityStr(v, pIdx); 1241 sqlite3ExprCacheAffinityChange(pParse, regIdx, pIdx->nColumn+1); 1242 sqlite3ReleaseTempRange(pParse, regIdx, pIdx->nColumn+1); 1243 1244 /* Find out what action to take in case there is an indexing conflict */ 1245 onError = pIdx->onError; 1246 if( onError==OE_None ) continue; /* pIdx is not a UNIQUE index */ 1247 if( overrideError!=OE_Default ){ 1248 onError = overrideError; 1249 }else if( onError==OE_Default ){ 1250 onError = OE_Abort; 1251 } 1252 if( seenReplace ){ 1253 if( onError==OE_Ignore ) onError = OE_Replace; 1254 else if( onError==OE_Fail ) onError = OE_Abort; 1255 } 1256 1257 1258 /* Check to see if the new index entry will be unique */ 1259 j2 = sqlite3VdbeAddOp3(v, OP_IsNull, regIdx, 0, pIdx->nColumn); 1260 regR = sqlite3GetTempReg(pParse); 1261 sqlite3VdbeAddOp2(v, OP_SCopy, regRowid-hasTwoRowids, regR); 1262 j3 = sqlite3VdbeAddOp4(v, OP_IsUnique, baseCur+iCur+1, 0, 1263 regR, SQLITE_INT_TO_PTR(aRegIdx[iCur]), 1264 P4_INT32); 1265 1266 /* Generate code that executes if the new index entry is not unique */ 1267 assert( onError==OE_Rollback || onError==OE_Abort || onError==OE_Fail 1268 || onError==OE_Ignore || onError==OE_Replace ); 1269 switch( onError ){ 1270 case OE_Rollback: 1271 case OE_Abort: 1272 case OE_Fail: { 1273 int j, n1, n2; 1274 char zErrMsg[200]; 1275 sqlite3_snprintf(ArraySize(zErrMsg), zErrMsg, 1276 pIdx->nColumn>1 ? "columns " : "column "); 1277 n1 = sqlite3Strlen30(zErrMsg); 1278 for(j=0; j<pIdx->nColumn && n1<ArraySize(zErrMsg)-30; j++){ 1279 char *zCol = pTab->aCol[pIdx->aiColumn[j]].zName; 1280 n2 = sqlite3Strlen30(zCol); 1281 if( j>0 ){ 1282 sqlite3_snprintf(ArraySize(zErrMsg)-n1, &zErrMsg[n1], ", "); 1283 n1 += 2; 1284 } 1285 if( n1+n2>ArraySize(zErrMsg)-30 ){ 1286 sqlite3_snprintf(ArraySize(zErrMsg)-n1, &zErrMsg[n1], "..."); 1287 n1 += 3; 1288 break; 1289 }else{ 1290 sqlite3_snprintf(ArraySize(zErrMsg)-n1, &zErrMsg[n1], "%s", zCol); 1291 n1 += n2; 1292 } 1293 } 1294 sqlite3_snprintf(ArraySize(zErrMsg)-n1, &zErrMsg[n1], 1295 pIdx->nColumn>1 ? " are not unique" : " is not unique"); 1296 sqlite3VdbeAddOp4(v, OP_Halt, SQLITE_CONSTRAINT, onError, 0, zErrMsg,0); 1297 break; 1298 } 1299 case OE_Ignore: { 1300 assert( seenReplace==0 ); 1301 sqlite3VdbeAddOp2(v, OP_Goto, 0, ignoreDest); 1302 break; 1303 } 1304 case OE_Replace: { 1305 sqlite3GenerateRowDelete(pParse, pTab, baseCur, regR, 0); 1306 seenReplace = 1; 1307 break; 1308 } 1309 } 1310 sqlite3VdbeJumpHere(v, j2); 1311 sqlite3VdbeJumpHere(v, j3); 1312 sqlite3ReleaseTempReg(pParse, regR); 1313 } 1314 } 1315 1316 /* 1317 ** This routine generates code to finish the INSERT or UPDATE operation 1318 ** that was started by a prior call to sqlite3GenerateConstraintChecks. 1319 ** A consecutive range of registers starting at regRowid contains the 1320 ** rowid and the content to be inserted. 1321 ** 1322 ** The arguments to this routine should be the same as the first six 1323 ** arguments to sqlite3GenerateConstraintChecks. 1324 */ 1325 void sqlite3CompleteInsertion( 1326 Parse *pParse, /* The parser context */ 1327 Table *pTab, /* the table into which we are inserting */ 1328 int baseCur, /* Index of a read/write cursor pointing at pTab */ 1329 int regRowid, /* Range of content */ 1330 int *aRegIdx, /* Register used by each index. 0 for unused indices */ 1331 int isUpdate, /* True for UPDATE, False for INSERT */ 1332 int newIdx, /* Index of NEW table for triggers. -1 if none */ 1333 int appendBias /* True if this is likely to be an append */ 1334 ){ 1335 int i; 1336 Vdbe *v; 1337 int nIdx; 1338 Index *pIdx; 1339 u8 pik_flags; 1340 int regData; 1341 int regRec; 1342 1343 v = sqlite3GetVdbe(pParse); 1344 assert( v!=0 ); 1345 assert( pTab->pSelect==0 ); /* This table is not a VIEW */ 1346 for(nIdx=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, nIdx++){} 1347 for(i=nIdx-1; i>=0; i--){ 1348 if( aRegIdx[i]==0 ) continue; 1349 sqlite3VdbeAddOp2(v, OP_IdxInsert, baseCur+i+1, aRegIdx[i]); 1350 } 1351 regData = regRowid + 1; 1352 regRec = sqlite3GetTempReg(pParse); 1353 sqlite3VdbeAddOp3(v, OP_MakeRecord, regData, pTab->nCol, regRec); 1354 sqlite3TableAffinityStr(v, pTab); 1355 sqlite3ExprCacheAffinityChange(pParse, regData, pTab->nCol); 1356 #ifndef SQLITE_OMIT_TRIGGER 1357 if( newIdx>=0 ){ 1358 sqlite3VdbeAddOp3(v, OP_Insert, newIdx, regRec, regRowid); 1359 } 1360 #endif 1361 if( pParse->nested ){ 1362 pik_flags = 0; 1363 }else{ 1364 pik_flags = OPFLAG_NCHANGE; 1365 pik_flags |= (isUpdate?OPFLAG_ISUPDATE:OPFLAG_LASTROWID); 1366 } 1367 if( appendBias ){ 1368 pik_flags |= OPFLAG_APPEND; 1369 } 1370 sqlite3VdbeAddOp3(v, OP_Insert, baseCur, regRec, regRowid); 1371 if( !pParse->nested ){ 1372 sqlite3VdbeChangeP4(v, -1, pTab->zName, P4_STATIC); 1373 } 1374 sqlite3VdbeChangeP5(v, pik_flags); 1375 } 1376 1377 /* 1378 ** Generate code that will open cursors for a table and for all 1379 ** indices of that table. The "baseCur" parameter is the cursor number used 1380 ** for the table. Indices are opened on subsequent cursors. 1381 ** 1382 ** Return the number of indices on the table. 1383 */ 1384 int sqlite3OpenTableAndIndices( 1385 Parse *pParse, /* Parsing context */ 1386 Table *pTab, /* Table to be opened */ 1387 int baseCur, /* Cursor number assigned to the table */ 1388 int op /* OP_OpenRead or OP_OpenWrite */ 1389 ){ 1390 int i; 1391 int iDb; 1392 Index *pIdx; 1393 Vdbe *v; 1394 1395 if( IsVirtual(pTab) ) return 0; 1396 iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema); 1397 v = sqlite3GetVdbe(pParse); 1398 assert( v!=0 ); 1399 sqlite3OpenTable(pParse, baseCur, iDb, pTab, op); 1400 for(i=1, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){ 1401 KeyInfo *pKey = sqlite3IndexKeyinfo(pParse, pIdx); 1402 assert( pIdx->pSchema==pTab->pSchema ); 1403 sqlite3VdbeAddOp4(v, op, i+baseCur, pIdx->tnum, iDb, 1404 (char*)pKey, P4_KEYINFO_HANDOFF); 1405 VdbeComment((v, "%s", pIdx->zName)); 1406 } 1407 if( pParse->nTab<=baseCur+i ){ 1408 pParse->nTab = baseCur+i; 1409 } 1410 return i-1; 1411 } 1412 1413 1414 #ifdef SQLITE_TEST 1415 /* 1416 ** The following global variable is incremented whenever the 1417 ** transfer optimization is used. This is used for testing 1418 ** purposes only - to make sure the transfer optimization really 1419 ** is happening when it is suppose to. 1420 */ 1421 int sqlite3_xferopt_count; 1422 #endif /* SQLITE_TEST */ 1423 1424 1425 #ifndef SQLITE_OMIT_XFER_OPT 1426 /* 1427 ** Check to collation names to see if they are compatible. 1428 */ 1429 static int xferCompatibleCollation(const char *z1, const char *z2){ 1430 if( z1==0 ){ 1431 return z2==0; 1432 } 1433 if( z2==0 ){ 1434 return 0; 1435 } 1436 return sqlite3StrICmp(z1, z2)==0; 1437 } 1438 1439 1440 /* 1441 ** Check to see if index pSrc is compatible as a source of data 1442 ** for index pDest in an insert transfer optimization. The rules 1443 ** for a compatible index: 1444 ** 1445 ** * The index is over the same set of columns 1446 ** * The same DESC and ASC markings occurs on all columns 1447 ** * The same onError processing (OE_Abort, OE_Ignore, etc) 1448 ** * The same collating sequence on each column 1449 */ 1450 static int xferCompatibleIndex(Index *pDest, Index *pSrc){ 1451 int i; 1452 assert( pDest && pSrc ); 1453 assert( pDest->pTable!=pSrc->pTable ); 1454 if( pDest->nColumn!=pSrc->nColumn ){ 1455 return 0; /* Different number of columns */ 1456 } 1457 if( pDest->onError!=pSrc->onError ){ 1458 return 0; /* Different conflict resolution strategies */ 1459 } 1460 for(i=0; i<pSrc->nColumn; i++){ 1461 if( pSrc->aiColumn[i]!=pDest->aiColumn[i] ){ 1462 return 0; /* Different columns indexed */ 1463 } 1464 if( pSrc->aSortOrder[i]!=pDest->aSortOrder[i] ){ 1465 return 0; /* Different sort orders */ 1466 } 1467 if( pSrc->azColl[i]!=pDest->azColl[i] ){ 1468 return 0; /* Different collating sequences */ 1469 } 1470 } 1471 1472 /* If no test above fails then the indices must be compatible */ 1473 return 1; 1474 } 1475 1476 /* 1477 ** Attempt the transfer optimization on INSERTs of the form 1478 ** 1479 ** INSERT INTO tab1 SELECT * FROM tab2; 1480 ** 1481 ** This optimization is only attempted if 1482 ** 1483 ** (1) tab1 and tab2 have identical schemas including all the 1484 ** same indices and constraints 1485 ** 1486 ** (2) tab1 and tab2 are different tables 1487 ** 1488 ** (3) There must be no triggers on tab1 1489 ** 1490 ** (4) The result set of the SELECT statement is "*" 1491 ** 1492 ** (5) The SELECT statement has no WHERE, HAVING, ORDER BY, GROUP BY, 1493 ** or LIMIT clause. 1494 ** 1495 ** (6) The SELECT statement is a simple (not a compound) select that 1496 ** contains only tab2 in its FROM clause 1497 ** 1498 ** This method for implementing the INSERT transfers raw records from 1499 ** tab2 over to tab1. The columns are not decoded. Raw records from 1500 ** the indices of tab2 are transfered to tab1 as well. In so doing, 1501 ** the resulting tab1 has much less fragmentation. 1502 ** 1503 ** This routine returns TRUE if the optimization is attempted. If any 1504 ** of the conditions above fail so that the optimization should not 1505 ** be attempted, then this routine returns FALSE. 1506 */ 1507 static int xferOptimization( 1508 Parse *pParse, /* Parser context */ 1509 Table *pDest, /* The table we are inserting into */ 1510 Select *pSelect, /* A SELECT statement to use as the data source */ 1511 int onError, /* How to handle constraint errors */ 1512 int iDbDest /* The database of pDest */ 1513 ){ 1514 ExprList *pEList; /* The result set of the SELECT */ 1515 Table *pSrc; /* The table in the FROM clause of SELECT */ 1516 Index *pSrcIdx, *pDestIdx; /* Source and destination indices */ 1517 struct SrcList_item *pItem; /* An element of pSelect->pSrc */ 1518 int i; /* Loop counter */ 1519 int iDbSrc; /* The database of pSrc */ 1520 int iSrc, iDest; /* Cursors from source and destination */ 1521 int addr1, addr2; /* Loop addresses */ 1522 int emptyDestTest; /* Address of test for empty pDest */ 1523 int emptySrcTest; /* Address of test for empty pSrc */ 1524 Vdbe *v; /* The VDBE we are building */ 1525 KeyInfo *pKey; /* Key information for an index */ 1526 int regAutoinc; /* Memory register used by AUTOINC */ 1527 int destHasUniqueIdx = 0; /* True if pDest has a UNIQUE index */ 1528 int regData, regRowid; /* Registers holding data and rowid */ 1529 1530 if( pSelect==0 ){ 1531 return 0; /* Must be of the form INSERT INTO ... SELECT ... */ 1532 } 1533 if( pDest->pTrigger ){ 1534 return 0; /* tab1 must not have triggers */ 1535 } 1536 #ifndef SQLITE_OMIT_VIRTUALTABLE 1537 if( pDest->tabFlags & TF_Virtual ){ 1538 return 0; /* tab1 must not be a virtual table */ 1539 } 1540 #endif 1541 if( onError==OE_Default ){ 1542 onError = OE_Abort; 1543 } 1544 if( onError!=OE_Abort && onError!=OE_Rollback ){ 1545 return 0; /* Cannot do OR REPLACE or OR IGNORE or OR FAIL */ 1546 } 1547 assert(pSelect->pSrc); /* allocated even if there is no FROM clause */ 1548 if( pSelect->pSrc->nSrc!=1 ){ 1549 return 0; /* FROM clause must have exactly one term */ 1550 } 1551 if( pSelect->pSrc->a[0].pSelect ){ 1552 return 0; /* FROM clause cannot contain a subquery */ 1553 } 1554 if( pSelect->pWhere ){ 1555 return 0; /* SELECT may not have a WHERE clause */ 1556 } 1557 if( pSelect->pOrderBy ){ 1558 return 0; /* SELECT may not have an ORDER BY clause */ 1559 } 1560 /* Do not need to test for a HAVING clause. If HAVING is present but 1561 ** there is no ORDER BY, we will get an error. */ 1562 if( pSelect->pGroupBy ){ 1563 return 0; /* SELECT may not have a GROUP BY clause */ 1564 } 1565 if( pSelect->pLimit ){ 1566 return 0; /* SELECT may not have a LIMIT clause */ 1567 } 1568 assert( pSelect->pOffset==0 ); /* Must be so if pLimit==0 */ 1569 if( pSelect->pPrior ){ 1570 return 0; /* SELECT may not be a compound query */ 1571 } 1572 if( pSelect->selFlags & SF_Distinct ){ 1573 return 0; /* SELECT may not be DISTINCT */ 1574 } 1575 pEList = pSelect->pEList; 1576 assert( pEList!=0 ); 1577 if( pEList->nExpr!=1 ){ 1578 return 0; /* The result set must have exactly one column */ 1579 } 1580 assert( pEList->a[0].pExpr ); 1581 if( pEList->a[0].pExpr->op!=TK_ALL ){ 1582 return 0; /* The result set must be the special operator "*" */ 1583 } 1584 1585 /* At this point we have established that the statement is of the 1586 ** correct syntactic form to participate in this optimization. Now 1587 ** we have to check the semantics. 1588 */ 1589 pItem = pSelect->pSrc->a; 1590 pSrc = sqlite3LocateTable(pParse, 0, pItem->zName, pItem->zDatabase); 1591 if( pSrc==0 ){ 1592 return 0; /* FROM clause does not contain a real table */ 1593 } 1594 if( pSrc==pDest ){ 1595 return 0; /* tab1 and tab2 may not be the same table */ 1596 } 1597 #ifndef SQLITE_OMIT_VIRTUALTABLE 1598 if( pSrc->tabFlags & TF_Virtual ){ 1599 return 0; /* tab2 must not be a virtual table */ 1600 } 1601 #endif 1602 if( pSrc->pSelect ){ 1603 return 0; /* tab2 may not be a view */ 1604 } 1605 if( pDest->nCol!=pSrc->nCol ){ 1606 return 0; /* Number of columns must be the same in tab1 and tab2 */ 1607 } 1608 if( pDest->iPKey!=pSrc->iPKey ){ 1609 return 0; /* Both tables must have the same INTEGER PRIMARY KEY */ 1610 } 1611 for(i=0; i<pDest->nCol; i++){ 1612 if( pDest->aCol[i].affinity!=pSrc->aCol[i].affinity ){ 1613 return 0; /* Affinity must be the same on all columns */ 1614 } 1615 if( !xferCompatibleCollation(pDest->aCol[i].zColl, pSrc->aCol[i].zColl) ){ 1616 return 0; /* Collating sequence must be the same on all columns */ 1617 } 1618 if( pDest->aCol[i].notNull && !pSrc->aCol[i].notNull ){ 1619 return 0; /* tab2 must be NOT NULL if tab1 is */ 1620 } 1621 } 1622 for(pDestIdx=pDest->pIndex; pDestIdx; pDestIdx=pDestIdx->pNext){ 1623 if( pDestIdx->onError!=OE_None ){ 1624 destHasUniqueIdx = 1; 1625 } 1626 for(pSrcIdx=pSrc->pIndex; pSrcIdx; pSrcIdx=pSrcIdx->pNext){ 1627 if( xferCompatibleIndex(pDestIdx, pSrcIdx) ) break; 1628 } 1629 if( pSrcIdx==0 ){ 1630 return 0; /* pDestIdx has no corresponding index in pSrc */ 1631 } 1632 } 1633 #ifndef SQLITE_OMIT_CHECK 1634 if( pDest->pCheck && !sqlite3ExprCompare(pSrc->pCheck, pDest->pCheck) ){ 1635 return 0; /* Tables have different CHECK constraints. Ticket #2252 */ 1636 } 1637 #endif 1638 1639 /* If we get this far, it means either: 1640 ** 1641 ** * We can always do the transfer if the table contains an 1642 ** an integer primary key 1643 ** 1644 ** * We can conditionally do the transfer if the destination 1645 ** table is empty. 1646 */ 1647 #ifdef SQLITE_TEST 1648 sqlite3_xferopt_count++; 1649 #endif 1650 iDbSrc = sqlite3SchemaToIndex(pParse->db, pSrc->pSchema); 1651 v = sqlite3GetVdbe(pParse); 1652 sqlite3CodeVerifySchema(pParse, iDbSrc); 1653 iSrc = pParse->nTab++; 1654 iDest = pParse->nTab++; 1655 regAutoinc = autoIncBegin(pParse, iDbDest, pDest); 1656 sqlite3OpenTable(pParse, iDest, iDbDest, pDest, OP_OpenWrite); 1657 if( (pDest->iPKey<0 && pDest->pIndex!=0) || destHasUniqueIdx ){ 1658 /* If tables do not have an INTEGER PRIMARY KEY and there 1659 ** are indices to be copied and the destination is not empty, 1660 ** we have to disallow the transfer optimization because the 1661 ** the rowids might change which will mess up indexing. 1662 ** 1663 ** Or if the destination has a UNIQUE index and is not empty, 1664 ** we also disallow the transfer optimization because we cannot 1665 ** insure that all entries in the union of DEST and SRC will be 1666 ** unique. 1667 */ 1668 addr1 = sqlite3VdbeAddOp2(v, OP_Rewind, iDest, 0); 1669 emptyDestTest = sqlite3VdbeAddOp2(v, OP_Goto, 0, 0); 1670 sqlite3VdbeJumpHere(v, addr1); 1671 }else{ 1672 emptyDestTest = 0; 1673 } 1674 sqlite3OpenTable(pParse, iSrc, iDbSrc, pSrc, OP_OpenRead); 1675 emptySrcTest = sqlite3VdbeAddOp2(v, OP_Rewind, iSrc, 0); 1676 regData = sqlite3GetTempReg(pParse); 1677 regRowid = sqlite3GetTempReg(pParse); 1678 if( pDest->iPKey>=0 ){ 1679 addr1 = sqlite3VdbeAddOp2(v, OP_Rowid, iSrc, regRowid); 1680 addr2 = sqlite3VdbeAddOp3(v, OP_NotExists, iDest, 0, regRowid); 1681 sqlite3VdbeAddOp4(v, OP_Halt, SQLITE_CONSTRAINT, onError, 0, 1682 "PRIMARY KEY must be unique", P4_STATIC); 1683 sqlite3VdbeJumpHere(v, addr2); 1684 autoIncStep(pParse, regAutoinc, regRowid); 1685 }else if( pDest->pIndex==0 ){ 1686 addr1 = sqlite3VdbeAddOp2(v, OP_NewRowid, iDest, regRowid); 1687 }else{ 1688 addr1 = sqlite3VdbeAddOp2(v, OP_Rowid, iSrc, regRowid); 1689 assert( (pDest->tabFlags & TF_Autoincrement)==0 ); 1690 } 1691 sqlite3VdbeAddOp2(v, OP_RowData, iSrc, regData); 1692 sqlite3VdbeAddOp3(v, OP_Insert, iDest, regData, regRowid); 1693 sqlite3VdbeChangeP5(v, OPFLAG_NCHANGE|OPFLAG_LASTROWID|OPFLAG_APPEND); 1694 sqlite3VdbeChangeP4(v, -1, pDest->zName, 0); 1695 sqlite3VdbeAddOp2(v, OP_Next, iSrc, addr1); 1696 autoIncEnd(pParse, iDbDest, pDest, regAutoinc); 1697 for(pDestIdx=pDest->pIndex; pDestIdx; pDestIdx=pDestIdx->pNext){ 1698 for(pSrcIdx=pSrc->pIndex; pSrcIdx; pSrcIdx=pSrcIdx->pNext){ 1699 if( xferCompatibleIndex(pDestIdx, pSrcIdx) ) break; 1700 } 1701 assert( pSrcIdx ); 1702 sqlite3VdbeAddOp2(v, OP_Close, iSrc, 0); 1703 sqlite3VdbeAddOp2(v, OP_Close, iDest, 0); 1704 pKey = sqlite3IndexKeyinfo(pParse, pSrcIdx); 1705 sqlite3VdbeAddOp4(v, OP_OpenRead, iSrc, pSrcIdx->tnum, iDbSrc, 1706 (char*)pKey, P4_KEYINFO_HANDOFF); 1707 VdbeComment((v, "%s", pSrcIdx->zName)); 1708 pKey = sqlite3IndexKeyinfo(pParse, pDestIdx); 1709 sqlite3VdbeAddOp4(v, OP_OpenWrite, iDest, pDestIdx->tnum, iDbDest, 1710 (char*)pKey, P4_KEYINFO_HANDOFF); 1711 VdbeComment((v, "%s", pDestIdx->zName)); 1712 addr1 = sqlite3VdbeAddOp2(v, OP_Rewind, iSrc, 0); 1713 sqlite3VdbeAddOp2(v, OP_RowKey, iSrc, regData); 1714 sqlite3VdbeAddOp3(v, OP_IdxInsert, iDest, regData, 1); 1715 sqlite3VdbeAddOp2(v, OP_Next, iSrc, addr1+1); 1716 sqlite3VdbeJumpHere(v, addr1); 1717 } 1718 sqlite3VdbeJumpHere(v, emptySrcTest); 1719 sqlite3ReleaseTempReg(pParse, regRowid); 1720 sqlite3ReleaseTempReg(pParse, regData); 1721 sqlite3VdbeAddOp2(v, OP_Close, iSrc, 0); 1722 sqlite3VdbeAddOp2(v, OP_Close, iDest, 0); 1723 if( emptyDestTest ){ 1724 sqlite3VdbeAddOp2(v, OP_Halt, SQLITE_OK, 0); 1725 sqlite3VdbeJumpHere(v, emptyDestTest); 1726 sqlite3VdbeAddOp2(v, OP_Close, iDest, 0); 1727 return 0; 1728 }else{ 1729 return 1; 1730 } 1731 } 1732 #endif /* SQLITE_OMIT_XFER_OPT */ 1733 1734 /* Make sure "isView" gets undefined in case this file becomes part of 1735 ** the amalgamation - so that subsequent files do not see isView as a 1736 ** macro. */ 1737 #undef isView 1738