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