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