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