4 ** The author disclaims copyright to this source code. In place of
5 ** a legal notice, here is a blessing:
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.
11 *************************************************************************
12 ** This file contains C code routines that are called by the parser
13 ** to handle INSERT statements in SQLite.
15 #include "sqliteInt.h"
18 ** Generate code that will
20 ** (1) acquire a lock for table pTab then
21 ** (2) open pTab as cursor iCur.
23 ** If pTab is a WITHOUT ROWID table, then it is the PRIMARY KEY index
24 ** for that table that is actually opened.
26 void sqlite3OpenTable(
27 Parse
*pParse
, /* Generate code into this VDBE */
28 int iCur
, /* The cursor number of the table */
29 int iDb
, /* The database index in sqlite3.aDb[] */
30 Table
*pTab
, /* The table to be opened */
31 int opcode
/* OP_OpenRead or OP_OpenWrite */
34 assert( !IsVirtual(pTab
) );
35 v
= sqlite3GetVdbe(pParse
);
36 assert( opcode
==OP_OpenWrite
|| opcode
==OP_OpenRead
);
37 sqlite3TableLock(pParse
, iDb
, pTab
->tnum
,
38 (opcode
==OP_OpenWrite
)?1:0, pTab
->zName
);
40 sqlite3VdbeAddOp4Int(v
, opcode
, iCur
, pTab
->tnum
, iDb
, pTab
->nNVCol
);
41 VdbeComment((v
, "%s", pTab
->zName
));
43 Index
*pPk
= sqlite3PrimaryKeyIndex(pTab
);
45 assert( pPk
->tnum
==pTab
->tnum
);
46 sqlite3VdbeAddOp3(v
, opcode
, iCur
, pPk
->tnum
, iDb
);
47 sqlite3VdbeSetP4KeyInfo(pParse
, pPk
);
48 VdbeComment((v
, "%s", pTab
->zName
));
53 ** Return a pointer to the column affinity string associated with index
54 ** pIdx. A column affinity string has one character for each column in
55 ** the table, according to the affinity of the column:
57 ** Character Column affinity
58 ** ------------------------------
65 ** An extra 'D' is appended to the end of the string to cover the
66 ** rowid that appears as the last column in every index.
68 ** Memory for the buffer containing the column index affinity string
69 ** is managed along with the rest of the Index structure. It will be
70 ** released when sqlite3DeleteIndex() is called.
72 const char *sqlite3IndexAffinityStr(sqlite3
*db
, Index
*pIdx
){
74 /* The first time a column affinity string for a particular index is
75 ** required, it is allocated and populated here. It is then stored as
76 ** a member of the Index structure for subsequent use.
78 ** The column affinity string will eventually be deleted by
79 ** sqliteDeleteIndex() when the Index structure itself is cleaned
83 Table
*pTab
= pIdx
->pTable
;
84 pIdx
->zColAff
= (char *)sqlite3DbMallocRaw(0, pIdx
->nColumn
+1);
89 for(n
=0; n
<pIdx
->nColumn
; n
++){
90 i16 x
= pIdx
->aiColumn
[n
];
93 aff
= pTab
->aCol
[x
].affinity
;
94 }else if( x
==XN_ROWID
){
95 aff
= SQLITE_AFF_INTEGER
;
98 assert( pIdx
->aColExpr
!=0 );
99 aff
= sqlite3ExprAffinity(pIdx
->aColExpr
->a
[n
].pExpr
);
101 if( aff
<SQLITE_AFF_BLOB
) aff
= SQLITE_AFF_BLOB
;
102 if( aff
>SQLITE_AFF_NUMERIC
) aff
= SQLITE_AFF_NUMERIC
;
103 pIdx
->zColAff
[n
] = aff
;
105 pIdx
->zColAff
[n
] = 0;
108 return pIdx
->zColAff
;
112 ** Compute the affinity string for table pTab, if it has not already been
113 ** computed. As an optimization, omit trailing SQLITE_AFF_BLOB affinities.
115 ** If the affinity exists (if it is no entirely SQLITE_AFF_BLOB values) and
116 ** if iReg>0 then code an OP_Affinity opcode that will set the affinities
117 ** for register iReg and following. Or if affinities exists and iReg==0,
118 ** then just set the P4 operand of the previous opcode (which should be
119 ** an OP_MakeRecord) to the affinity string.
121 ** A column affinity string has one character per column:
123 ** Character Column affinity
124 ** ------------------------------
131 void sqlite3TableAffinity(Vdbe
*v
, Table
*pTab
, int iReg
){
133 char *zColAff
= pTab
->zColAff
;
135 sqlite3
*db
= sqlite3VdbeDb(v
);
136 zColAff
= (char *)sqlite3DbMallocRaw(0, pTab
->nCol
+1);
142 for(i
=j
=0; i
<pTab
->nCol
; i
++){
143 assert( pTab
->aCol
[i
].affinity
!=0 );
144 if( (pTab
->aCol
[i
].colFlags
& COLFLAG_VIRTUAL
)==0 ){
145 zColAff
[j
++] = pTab
->aCol
[i
].affinity
;
150 }while( j
>=0 && zColAff
[j
]<=SQLITE_AFF_BLOB
);
151 pTab
->zColAff
= zColAff
;
153 assert( zColAff
!=0 );
154 i
= sqlite3Strlen30NN(zColAff
);
157 sqlite3VdbeAddOp4(v
, OP_Affinity
, iReg
, i
, 0, zColAff
, i
);
159 sqlite3VdbeChangeP4(v
, -1, zColAff
, i
);
165 ** Return non-zero if the table pTab in database iDb or any of its indices
166 ** have been opened at any point in the VDBE program. This is used to see if
167 ** a statement of the form "INSERT INTO <iDb, pTab> SELECT ..." can
168 ** run without using a temporary table for the results of the SELECT.
170 static int readsTable(Parse
*p
, int iDb
, Table
*pTab
){
171 Vdbe
*v
= sqlite3GetVdbe(p
);
173 int iEnd
= sqlite3VdbeCurrentAddr(v
);
174 #ifndef SQLITE_OMIT_VIRTUALTABLE
175 VTable
*pVTab
= IsVirtual(pTab
) ? sqlite3GetVTable(p
->db
, pTab
) : 0;
178 for(i
=1; i
<iEnd
; i
++){
179 VdbeOp
*pOp
= sqlite3VdbeGetOp(v
, i
);
181 if( pOp
->opcode
==OP_OpenRead
&& pOp
->p3
==iDb
){
184 if( tnum
==pTab
->tnum
){
187 for(pIndex
=pTab
->pIndex
; pIndex
; pIndex
=pIndex
->pNext
){
188 if( tnum
==pIndex
->tnum
){
193 #ifndef SQLITE_OMIT_VIRTUALTABLE
194 if( pOp
->opcode
==OP_VOpen
&& pOp
->p4
.pVtab
==pVTab
){
195 assert( pOp
->p4
.pVtab
!=0 );
196 assert( pOp
->p4type
==P4_VTAB
);
204 /* This walker callback will compute the union of colFlags flags for all
205 ** referenced columns in a CHECK constraint or generated column expression.
207 static int exprColumnFlagUnion(Walker
*pWalker
, Expr
*pExpr
){
208 if( pExpr
->op
==TK_COLUMN
&& pExpr
->iColumn
>=0 ){
209 assert( pExpr
->iColumn
< pWalker
->u
.pTab
->nCol
);
210 pWalker
->eCode
|= pWalker
->u
.pTab
->aCol
[pExpr
->iColumn
].colFlags
;
215 #ifndef SQLITE_OMIT_GENERATED_COLUMNS
217 ** All regular columns for table pTab have been puts into registers
218 ** starting with iRegStore. The registers that correspond to STORED
219 ** or VIRTUAL columns have not yet been initialized. This routine goes
220 ** back and computes the values for those columns based on the previously
221 ** computed normal columns.
223 void sqlite3ComputeGeneratedColumns(
224 Parse
*pParse
, /* Parsing context */
225 int iRegStore
, /* Register holding the first column */
226 Table
*pTab
/* The table */
234 assert( pTab
->tabFlags
& TF_HasGenerated
);
235 testcase( pTab
->tabFlags
& TF_HasVirtual
);
236 testcase( pTab
->tabFlags
& TF_HasStored
);
238 /* Before computing generated columns, first go through and make sure
239 ** that appropriate affinity has been applied to the regular columns
241 sqlite3TableAffinity(pParse
->pVdbe
, pTab
, iRegStore
);
242 if( (pTab
->tabFlags
& TF_HasStored
)!=0
243 && (pOp
= sqlite3VdbeGetOp(pParse
->pVdbe
,-1))->opcode
==OP_Affinity
245 /* Change the OP_Affinity argument to '@' (NONE) for all stored
246 ** columns. '@' is the no-op affinity and those columns have not
247 ** yet been computed. */
249 char *zP4
= pOp
->p4
.z
;
251 assert( pOp
->p4type
==P4_DYNAMIC
);
252 for(ii
=jj
=0; zP4
[jj
]; ii
++){
253 if( pTab
->aCol
[ii
].colFlags
& COLFLAG_VIRTUAL
){
256 if( pTab
->aCol
[ii
].colFlags
& COLFLAG_STORED
){
257 zP4
[jj
] = SQLITE_AFF_NONE
;
263 /* Because there can be multiple generated columns that refer to one another,
264 ** this is a two-pass algorithm. On the first pass, mark all generated
265 ** columns as "not available".
267 for(i
=0; i
<pTab
->nCol
; i
++){
268 if( pTab
->aCol
[i
].colFlags
& COLFLAG_GENERATED
){
269 testcase( pTab
->aCol
[i
].colFlags
& COLFLAG_VIRTUAL
);
270 testcase( pTab
->aCol
[i
].colFlags
& COLFLAG_STORED
);
271 pTab
->aCol
[i
].colFlags
|= COLFLAG_NOTAVAIL
;
276 w
.xExprCallback
= exprColumnFlagUnion
;
277 w
.xSelectCallback
= 0;
278 w
.xSelectCallback2
= 0;
280 /* On the second pass, compute the value of each NOT-AVAILABLE column.
281 ** Companion code in the TK_COLUMN case of sqlite3ExprCodeTarget() will
282 ** compute dependencies and mark remove the COLSPAN_NOTAVAIL mark, as
285 pParse
->iSelfTab
= -iRegStore
;
289 for(i
=0; i
<pTab
->nCol
; i
++){
290 Column
*pCol
= pTab
->aCol
+ i
;
291 if( (pCol
->colFlags
& COLFLAG_NOTAVAIL
)!=0 ){
293 pCol
->colFlags
|= COLFLAG_BUSY
;
295 sqlite3WalkExpr(&w
, pCol
->pDflt
);
296 pCol
->colFlags
&= ~COLFLAG_BUSY
;
297 if( w
.eCode
& COLFLAG_NOTAVAIL
){
302 assert( pCol
->colFlags
& COLFLAG_GENERATED
);
303 x
= sqlite3TableColumnToStorage(pTab
, i
) + iRegStore
;
304 sqlite3ExprCodeGeneratedColumn(pParse
, pCol
, x
);
305 pCol
->colFlags
&= ~COLFLAG_NOTAVAIL
;
308 }while( pRedo
&& eProgress
);
310 sqlite3ErrorMsg(pParse
, "generated column loop on \"%s\"", pRedo
->zName
);
312 pParse
->iSelfTab
= 0;
314 #endif /* SQLITE_OMIT_GENERATED_COLUMNS */
317 #ifndef SQLITE_OMIT_AUTOINCREMENT
319 ** Locate or create an AutoincInfo structure associated with table pTab
320 ** which is in database iDb. Return the register number for the register
321 ** that holds the maximum rowid. Return zero if pTab is not an AUTOINCREMENT
322 ** table. (Also return zero when doing a VACUUM since we do not want to
323 ** update the AUTOINCREMENT counters during a VACUUM.)
325 ** There is at most one AutoincInfo structure per table even if the
326 ** same table is autoincremented multiple times due to inserts within
327 ** triggers. A new AutoincInfo structure is created if this is the
328 ** first use of table pTab. On 2nd and subsequent uses, the original
329 ** AutoincInfo structure is used.
331 ** Four consecutive registers are allocated:
333 ** (1) The name of the pTab table.
334 ** (2) The maximum ROWID of pTab.
335 ** (3) The rowid in sqlite_sequence of pTab
336 ** (4) The original value of the max ROWID in pTab, or NULL if none
338 ** The 2nd register is the one that is returned. That is all the
339 ** insert routine needs to know about.
341 static int autoIncBegin(
342 Parse
*pParse
, /* Parsing context */
343 int iDb
, /* Index of the database holding pTab */
344 Table
*pTab
/* The table we are writing to */
346 int memId
= 0; /* Register holding maximum rowid */
347 assert( pParse
->db
->aDb
[iDb
].pSchema
!=0 );
348 if( (pTab
->tabFlags
& TF_Autoincrement
)!=0
349 && (pParse
->db
->mDbFlags
& DBFLAG_Vacuum
)==0
351 Parse
*pToplevel
= sqlite3ParseToplevel(pParse
);
353 Table
*pSeqTab
= pParse
->db
->aDb
[iDb
].pSchema
->pSeqTab
;
355 /* Verify that the sqlite_sequence table exists and is an ordinary
356 ** rowid table with exactly two columns.
357 ** Ticket d8dc2b3a58cd5dc2918a1d4acb 2018-05-23 */
359 || !HasRowid(pSeqTab
)
360 || IsVirtual(pSeqTab
)
364 pParse
->rc
= SQLITE_CORRUPT_SEQUENCE
;
368 pInfo
= pToplevel
->pAinc
;
369 while( pInfo
&& pInfo
->pTab
!=pTab
){ pInfo
= pInfo
->pNext
; }
371 pInfo
= sqlite3DbMallocRawNN(pParse
->db
, sizeof(*pInfo
));
372 if( pInfo
==0 ) return 0;
373 pInfo
->pNext
= pToplevel
->pAinc
;
374 pToplevel
->pAinc
= pInfo
;
377 pToplevel
->nMem
++; /* Register to hold name of table */
378 pInfo
->regCtr
= ++pToplevel
->nMem
; /* Max rowid register */
379 pToplevel
->nMem
+=2; /* Rowid in sqlite_sequence + orig max val */
381 memId
= pInfo
->regCtr
;
387 ** This routine generates code that will initialize all of the
388 ** register used by the autoincrement tracker.
390 void sqlite3AutoincrementBegin(Parse
*pParse
){
391 AutoincInfo
*p
; /* Information about an AUTOINCREMENT */
392 sqlite3
*db
= pParse
->db
; /* The database connection */
393 Db
*pDb
; /* Database only autoinc table */
394 int memId
; /* Register holding max rowid */
395 Vdbe
*v
= pParse
->pVdbe
; /* VDBE under construction */
397 /* This routine is never called during trigger-generation. It is
398 ** only called from the top-level */
399 assert( pParse
->pTriggerTab
==0 );
400 assert( sqlite3IsToplevel(pParse
) );
402 assert( v
); /* We failed long ago if this is not so */
403 for(p
= pParse
->pAinc
; p
; p
= p
->pNext
){
404 static const int iLn
= VDBE_OFFSET_LINENO(2);
405 static const VdbeOpList autoInc
[] = {
406 /* 0 */ {OP_Null
, 0, 0, 0},
407 /* 1 */ {OP_Rewind
, 0, 10, 0},
408 /* 2 */ {OP_Column
, 0, 0, 0},
409 /* 3 */ {OP_Ne
, 0, 9, 0},
410 /* 4 */ {OP_Rowid
, 0, 0, 0},
411 /* 5 */ {OP_Column
, 0, 1, 0},
412 /* 6 */ {OP_AddImm
, 0, 0, 0},
413 /* 7 */ {OP_Copy
, 0, 0, 0},
414 /* 8 */ {OP_Goto
, 0, 11, 0},
415 /* 9 */ {OP_Next
, 0, 2, 0},
416 /* 10 */ {OP_Integer
, 0, 0, 0},
417 /* 11 */ {OP_Close
, 0, 0, 0}
420 pDb
= &db
->aDb
[p
->iDb
];
422 assert( sqlite3SchemaMutexHeld(db
, 0, pDb
->pSchema
) );
423 sqlite3OpenTable(pParse
, 0, p
->iDb
, pDb
->pSchema
->pSeqTab
, OP_OpenRead
);
424 sqlite3VdbeLoadString(v
, memId
-1, p
->pTab
->zName
);
425 aOp
= sqlite3VdbeAddOpList(v
, ArraySize(autoInc
), autoInc
, iLn
);
432 aOp
[3].p5
= SQLITE_JUMPIFNULL
;
439 if( pParse
->nTab
==0 ) pParse
->nTab
= 1;
444 ** Update the maximum rowid for an autoincrement calculation.
446 ** This routine should be called when the regRowid register holds a
447 ** new rowid that is about to be inserted. If that new rowid is
448 ** larger than the maximum rowid in the memId memory cell, then the
449 ** memory cell is updated.
451 static void autoIncStep(Parse
*pParse
, int memId
, int regRowid
){
453 sqlite3VdbeAddOp2(pParse
->pVdbe
, OP_MemMax
, memId
, regRowid
);
458 ** This routine generates the code needed to write autoincrement
459 ** maximum rowid values back into the sqlite_sequence register.
460 ** Every statement that might do an INSERT into an autoincrement
461 ** table (either directly or through triggers) needs to call this
462 ** routine just before the "exit" code.
464 static SQLITE_NOINLINE
void autoIncrementEnd(Parse
*pParse
){
466 Vdbe
*v
= pParse
->pVdbe
;
467 sqlite3
*db
= pParse
->db
;
470 for(p
= pParse
->pAinc
; p
; p
= p
->pNext
){
471 static const int iLn
= VDBE_OFFSET_LINENO(2);
472 static const VdbeOpList autoIncEnd
[] = {
473 /* 0 */ {OP_NotNull
, 0, 2, 0},
474 /* 1 */ {OP_NewRowid
, 0, 0, 0},
475 /* 2 */ {OP_MakeRecord
, 0, 2, 0},
476 /* 3 */ {OP_Insert
, 0, 0, 0},
477 /* 4 */ {OP_Close
, 0, 0, 0}
480 Db
*pDb
= &db
->aDb
[p
->iDb
];
482 int memId
= p
->regCtr
;
484 iRec
= sqlite3GetTempReg(pParse
);
485 assert( sqlite3SchemaMutexHeld(db
, 0, pDb
->pSchema
) );
486 sqlite3VdbeAddOp3(v
, OP_Le
, memId
+2, sqlite3VdbeCurrentAddr(v
)+7, memId
);
488 sqlite3OpenTable(pParse
, 0, p
->iDb
, pDb
->pSchema
->pSeqTab
, OP_OpenWrite
);
489 aOp
= sqlite3VdbeAddOpList(v
, ArraySize(autoIncEnd
), autoIncEnd
, iLn
);
497 aOp
[3].p5
= OPFLAG_APPEND
;
498 sqlite3ReleaseTempReg(pParse
, iRec
);
501 void sqlite3AutoincrementEnd(Parse
*pParse
){
502 if( pParse
->pAinc
) autoIncrementEnd(pParse
);
506 ** If SQLITE_OMIT_AUTOINCREMENT is defined, then the three routines
507 ** above are all no-ops
509 # define autoIncBegin(A,B,C) (0)
510 # define autoIncStep(A,B,C)
511 #endif /* SQLITE_OMIT_AUTOINCREMENT */
514 /* Forward declaration */
515 static int xferOptimization(
516 Parse
*pParse
, /* Parser context */
517 Table
*pDest
, /* The table we are inserting into */
518 Select
*pSelect
, /* A SELECT statement to use as the data source */
519 int onError
, /* How to handle constraint errors */
520 int iDbDest
/* The database of pDest */
524 ** This routine is called to handle SQL of the following forms:
526 ** insert into TABLE (IDLIST) values(EXPRLIST),(EXPRLIST),...
527 ** insert into TABLE (IDLIST) select
528 ** insert into TABLE (IDLIST) default values
530 ** The IDLIST following the table name is always optional. If omitted,
531 ** then a list of all (non-hidden) columns for the table is substituted.
532 ** The IDLIST appears in the pColumn parameter. pColumn is NULL if IDLIST
535 ** For the pSelect parameter holds the values to be inserted for the
536 ** first two forms shown above. A VALUES clause is really just short-hand
537 ** for a SELECT statement that omits the FROM clause and everything else
538 ** that follows. If the pSelect parameter is NULL, that means that the
539 ** DEFAULT VALUES form of the INSERT statement is intended.
541 ** The code generated follows one of four templates. For a simple
542 ** insert with data coming from a single-row VALUES clause, the code executes
543 ** once straight down through. Pseudo-code follows (we call this
544 ** the "1st template"):
546 ** open write cursor to <table> and its indices
547 ** put VALUES clause expressions into registers
548 ** write the resulting record into <table>
551 ** The three remaining templates assume the statement is of the form
553 ** INSERT INTO <table> SELECT ...
555 ** If the SELECT clause is of the restricted form "SELECT * FROM <table2>" -
556 ** in other words if the SELECT pulls all columns from a single table
557 ** and there is no WHERE or LIMIT or GROUP BY or ORDER BY clauses, and
558 ** if <table2> and <table1> are distinct tables but have identical
559 ** schemas, including all the same indices, then a special optimization
560 ** is invoked that copies raw records from <table2> over to <table1>.
561 ** See the xferOptimization() function for the implementation of this
562 ** template. This is the 2nd template.
564 ** open a write cursor to <table>
565 ** open read cursor on <table2>
566 ** transfer all records in <table2> over to <table>
568 ** foreach index on <table>
569 ** open a write cursor on the <table> index
570 ** open a read cursor on the corresponding <table2> index
571 ** transfer all records from the read to the write cursors
575 ** The 3rd template is for when the second template does not apply
576 ** and the SELECT clause does not read from <table> at any time.
577 ** The generated code follows this template:
581 ** A: setup for the SELECT
582 ** loop over the rows in the SELECT
583 ** load values into registers R..R+n
586 ** cleanup after the SELECT
588 ** B: open write cursor to <table> and its indices
589 ** C: yield X, at EOF goto D
590 ** insert the select result into <table> from R..R+n
594 ** The 4th template is used if the insert statement takes its
595 ** values from a SELECT but the data is being inserted into a table
596 ** that is also read as part of the SELECT. In the third form,
597 ** we have to use an intermediate table to store the results of
598 ** the select. The template is like this:
602 ** A: setup for the SELECT
603 ** loop over the tables in the SELECT
604 ** load value into register R..R+n
607 ** cleanup after the SELECT
609 ** B: open temp table
610 ** L: yield X, at EOF goto M
611 ** insert row from R..R+n into temp table
613 ** M: open write cursor to <table> and its indices
615 ** C: loop over rows of intermediate table
616 ** transfer values form intermediate table into <table>
621 Parse
*pParse
, /* Parser context */
622 SrcList
*pTabList
, /* Name of table into which we are inserting */
623 Select
*pSelect
, /* A SELECT statement to use as the data source */
624 IdList
*pColumn
, /* Column names corresponding to IDLIST, or NULL. */
625 int onError
, /* How to handle constraint errors */
626 Upsert
*pUpsert
/* ON CONFLICT clauses for upsert, or NULL */
628 sqlite3
*db
; /* The main database structure */
629 Table
*pTab
; /* The table to insert into. aka TABLE */
630 int i
, j
; /* Loop counters */
631 Vdbe
*v
; /* Generate code into this virtual machine */
632 Index
*pIdx
; /* For looping over indices of the table */
633 int nColumn
; /* Number of columns in the data */
634 int nHidden
= 0; /* Number of hidden columns if TABLE is virtual */
635 int iDataCur
= 0; /* VDBE cursor that is the main data repository */
636 int iIdxCur
= 0; /* First index cursor */
637 int ipkColumn
= -1; /* Column that is the INTEGER PRIMARY KEY */
638 int endOfLoop
; /* Label for the end of the insertion loop */
639 int srcTab
= 0; /* Data comes from this temporary cursor if >=0 */
640 int addrInsTop
= 0; /* Jump to label "D" */
641 int addrCont
= 0; /* Top of insert loop. Label "C" in templates 3 and 4 */
642 SelectDest dest
; /* Destination for SELECT on rhs of INSERT */
643 int iDb
; /* Index of database holding TABLE */
644 u8 useTempTable
= 0; /* Store SELECT results in intermediate table */
645 u8 appendFlag
= 0; /* True if the insert is likely to be an append */
646 u8 withoutRowid
; /* 0 for normal table. 1 for WITHOUT ROWID table */
647 u8 bIdListInOrder
; /* True if IDLIST is in table order */
648 ExprList
*pList
= 0; /* List of VALUES() to be inserted */
649 int iRegStore
; /* Register in which to store next column */
651 /* Register allocations */
652 int regFromSelect
= 0;/* Base register for data coming from SELECT */
653 int regAutoinc
= 0; /* Register holding the AUTOINCREMENT counter */
654 int regRowCount
= 0; /* Memory cell used for the row counter */
655 int regIns
; /* Block of regs holding rowid+data being inserted */
656 int regRowid
; /* registers holding insert rowid */
657 int regData
; /* register holding first column to insert */
658 int *aRegIdx
= 0; /* One register allocated to each index */
660 #ifndef SQLITE_OMIT_TRIGGER
661 int isView
; /* True if attempting to insert into a view */
662 Trigger
*pTrigger
; /* List of triggers on pTab, if required */
663 int tmask
; /* Mask of trigger times */
667 if( pParse
->nErr
|| db
->mallocFailed
){
670 dest
.iSDParm
= 0; /* Suppress a harmless compiler warning */
672 /* If the Select object is really just a simple VALUES() list with a
673 ** single row (the common case) then keep that one row of values
674 ** and discard the other (unused) parts of the pSelect object
676 if( pSelect
&& (pSelect
->selFlags
& SF_Values
)!=0 && pSelect
->pPrior
==0 ){
677 pList
= pSelect
->pEList
;
679 sqlite3SelectDelete(db
, pSelect
);
683 /* Locate the table into which we will be inserting new information.
685 assert( pTabList
->nSrc
==1 );
686 pTab
= sqlite3SrcListLookup(pParse
, pTabList
);
690 iDb
= sqlite3SchemaToIndex(db
, pTab
->pSchema
);
691 assert( iDb
<db
->nDb
);
692 if( sqlite3AuthCheck(pParse
, SQLITE_INSERT
, pTab
->zName
, 0,
693 db
->aDb
[iDb
].zDbSName
) ){
696 withoutRowid
= !HasRowid(pTab
);
698 /* Figure out if we have any triggers and if the table being
699 ** inserted into is a view
701 #ifndef SQLITE_OMIT_TRIGGER
702 pTrigger
= sqlite3TriggersExist(pParse
, pTab
, TK_INSERT
, 0, &tmask
);
703 isView
= pTab
->pSelect
!=0;
709 #ifdef SQLITE_OMIT_VIEW
713 assert( (pTrigger
&& tmask
) || (pTrigger
==0 && tmask
==0) );
715 /* If pTab is really a view, make sure it has been initialized.
716 ** ViewGetColumnNames() is a no-op if pTab is not a view.
718 if( sqlite3ViewGetColumnNames(pParse
, pTab
) ){
722 /* Cannot insert into a read-only table.
724 if( sqlite3IsReadOnly(pParse
, pTab
, tmask
) ){
730 v
= sqlite3GetVdbe(pParse
);
731 if( v
==0 ) goto insert_cleanup
;
732 if( pParse
->nested
==0 ) sqlite3VdbeCountChanges(v
);
733 sqlite3BeginWriteOperation(pParse
, pSelect
|| pTrigger
, iDb
);
735 #ifndef SQLITE_OMIT_XFER_OPT
736 /* If the statement is of the form
738 ** INSERT INTO <table1> SELECT * FROM <table2>;
740 ** Then special optimizations can be applied that make the transfer
741 ** very fast and which reduce fragmentation of indices.
743 ** This is the 2nd template.
745 if( pColumn
==0 && xferOptimization(pParse
, pTab
, pSelect
, onError
, iDb
) ){
750 #endif /* SQLITE_OMIT_XFER_OPT */
752 /* If this is an AUTOINCREMENT table, look up the sequence number in the
753 ** sqlite_sequence table and store it in memory cell regAutoinc.
755 regAutoinc
= autoIncBegin(pParse
, iDb
, pTab
);
757 /* Allocate a block registers to hold the rowid and the values
758 ** for all columns of the new row.
760 regRowid
= regIns
= pParse
->nMem
+1;
761 pParse
->nMem
+= pTab
->nCol
+ 1;
762 if( IsVirtual(pTab
) ){
766 regData
= regRowid
+1;
768 /* If the INSERT statement included an IDLIST term, then make sure
769 ** all elements of the IDLIST really are columns of the table and
770 ** remember the column indices.
772 ** If the table has an INTEGER PRIMARY KEY column and that column
773 ** is named in the IDLIST, then record in the ipkColumn variable
774 ** the index into IDLIST of the primary key column. ipkColumn is
775 ** the index of the primary key as it appears in IDLIST, not as
776 ** is appears in the original table. (The index of the INTEGER
777 ** PRIMARY KEY in the original table is pTab->iPKey.) After this
778 ** loop, if ipkColumn==(-1), that means that integer primary key
779 ** is unspecified, and hence the table is either WITHOUT ROWID or
780 ** it will automatically generated an integer primary key.
782 ** bIdListInOrder is true if the columns in IDLIST are in storage
783 ** order. This enables an optimization that avoids shuffling the
784 ** columns into storage order. False negatives are harmless,
785 ** but false positives will cause database corruption.
787 bIdListInOrder
= (pTab
->tabFlags
& (TF_OOOHidden
|TF_HasStored
))==0;
789 for(i
=0; i
<pColumn
->nId
; i
++){
790 pColumn
->a
[i
].idx
= -1;
792 for(i
=0; i
<pColumn
->nId
; i
++){
793 for(j
=0; j
<pTab
->nCol
; j
++){
794 if( sqlite3StrICmp(pColumn
->a
[i
].zName
, pTab
->aCol
[j
].zName
)==0 ){
795 pColumn
->a
[i
].idx
= j
;
796 if( i
!=j
) bIdListInOrder
= 0;
797 if( j
==pTab
->iPKey
){
798 ipkColumn
= i
; assert( !withoutRowid
);
800 #ifndef SQLITE_OMIT_GENERATED_COLUMNS
801 if( pTab
->aCol
[j
].colFlags
& (COLFLAG_STORED
|COLFLAG_VIRTUAL
) ){
802 sqlite3ErrorMsg(pParse
,
803 "cannot INSERT into generated column \"%s\"",
804 pTab
->aCol
[j
].zName
);
812 if( sqlite3IsRowid(pColumn
->a
[i
].zName
) && !withoutRowid
){
816 sqlite3ErrorMsg(pParse
, "table %S has no column named %s",
817 pTabList
, 0, pColumn
->a
[i
].zName
);
818 pParse
->checkSchema
= 1;
825 /* Figure out how many columns of data are supplied. If the data
826 ** is coming from a SELECT statement, then generate a co-routine that
827 ** produces a single row of the SELECT on each invocation. The
828 ** co-routine is the common header to the 3rd and 4th templates.
831 /* Data is coming from a SELECT or from a multi-row VALUES clause.
832 ** Generate a co-routine to run the SELECT. */
833 int regYield
; /* Register holding co-routine entry-point */
834 int addrTop
; /* Top of the co-routine */
835 int rc
; /* Result code */
837 regYield
= ++pParse
->nMem
;
838 addrTop
= sqlite3VdbeCurrentAddr(v
) + 1;
839 sqlite3VdbeAddOp3(v
, OP_InitCoroutine
, regYield
, 0, addrTop
);
840 sqlite3SelectDestInit(&dest
, SRT_Coroutine
, regYield
);
841 dest
.iSdst
= bIdListInOrder
? regData
: 0;
842 dest
.nSdst
= pTab
->nCol
;
843 rc
= sqlite3Select(pParse
, pSelect
, &dest
);
844 regFromSelect
= dest
.iSdst
;
845 if( rc
|| db
->mallocFailed
|| pParse
->nErr
) goto insert_cleanup
;
846 sqlite3VdbeEndCoroutine(v
, regYield
);
847 sqlite3VdbeJumpHere(v
, addrTop
- 1); /* label B: */
848 assert( pSelect
->pEList
);
849 nColumn
= pSelect
->pEList
->nExpr
;
851 /* Set useTempTable to TRUE if the result of the SELECT statement
852 ** should be written into a temporary table (template 4). Set to
853 ** FALSE if each output row of the SELECT can be written directly into
854 ** the destination table (template 3).
856 ** A temp table must be used if the table being updated is also one
857 ** of the tables being read by the SELECT statement. Also use a
858 ** temp table in the case of row triggers.
860 if( pTrigger
|| readsTable(pParse
, iDb
, pTab
) ){
865 /* Invoke the coroutine to extract information from the SELECT
866 ** and add it to a transient table srcTab. The code generated
867 ** here is from the 4th template:
869 ** B: open temp table
870 ** L: yield X, goto M at EOF
871 ** insert row from R..R+n into temp table
875 int regRec
; /* Register to hold packed record */
876 int regTempRowid
; /* Register to hold temp table ROWID */
877 int addrL
; /* Label "L" */
879 srcTab
= pParse
->nTab
++;
880 regRec
= sqlite3GetTempReg(pParse
);
881 regTempRowid
= sqlite3GetTempReg(pParse
);
882 sqlite3VdbeAddOp2(v
, OP_OpenEphemeral
, srcTab
, nColumn
);
883 addrL
= sqlite3VdbeAddOp1(v
, OP_Yield
, dest
.iSDParm
); VdbeCoverage(v
);
884 sqlite3VdbeAddOp3(v
, OP_MakeRecord
, regFromSelect
, nColumn
, regRec
);
885 sqlite3VdbeAddOp2(v
, OP_NewRowid
, srcTab
, regTempRowid
);
886 sqlite3VdbeAddOp3(v
, OP_Insert
, srcTab
, regRec
, regTempRowid
);
887 sqlite3VdbeGoto(v
, addrL
);
888 sqlite3VdbeJumpHere(v
, addrL
);
889 sqlite3ReleaseTempReg(pParse
, regRec
);
890 sqlite3ReleaseTempReg(pParse
, regTempRowid
);
893 /* This is the case if the data for the INSERT is coming from a
894 ** single-row VALUES clause
897 memset(&sNC
, 0, sizeof(sNC
));
900 assert( useTempTable
==0 );
902 nColumn
= pList
->nExpr
;
903 if( sqlite3ResolveExprListNames(&sNC
, pList
) ){
911 /* If there is no IDLIST term but the table has an integer primary
912 ** key, the set the ipkColumn variable to the integer primary key
913 ** column index in the original table definition.
915 if( pColumn
==0 && nColumn
>0 ){
916 ipkColumn
= pTab
->iPKey
;
917 #ifndef SQLITE_OMIT_GENERATED_COLUMNS
918 if( ipkColumn
>=0 && (pTab
->tabFlags
& TF_HasGenerated
)!=0 ){
919 testcase( pTab
->tabFlags
& TF_HasVirtual
);
920 testcase( pTab
->tabFlags
& TF_HasStored
);
921 for(i
=ipkColumn
-1; i
>=0; i
--){
922 if( pTab
->aCol
[i
].colFlags
& COLFLAG_GENERATED
){
923 testcase( pTab
->aCol
[i
].colFlags
& COLFLAG_VIRTUAL
);
924 testcase( pTab
->aCol
[i
].colFlags
& COLFLAG_STORED
);
932 /* Make sure the number of columns in the source data matches the number
933 ** of columns to be inserted into the table.
935 for(i
=0; i
<pTab
->nCol
; i
++){
936 if( pTab
->aCol
[i
].colFlags
& COLFLAG_NOINSERT
) nHidden
++;
938 if( pColumn
==0 && nColumn
&& nColumn
!=(pTab
->nCol
-nHidden
) ){
939 sqlite3ErrorMsg(pParse
,
940 "table %S has %d columns but %d values were supplied",
941 pTabList
, 0, pTab
->nCol
-nHidden
, nColumn
);
944 if( pColumn
!=0 && nColumn
!=pColumn
->nId
){
945 sqlite3ErrorMsg(pParse
, "%d values for %d columns", nColumn
, pColumn
->nId
);
949 /* Initialize the count of rows to be inserted
951 if( (db
->flags
& SQLITE_CountRows
)!=0
953 && !pParse
->pTriggerTab
955 regRowCount
= ++pParse
->nMem
;
956 sqlite3VdbeAddOp2(v
, OP_Integer
, 0, regRowCount
);
959 /* If this is not a view, open the table and and all indices */
962 nIdx
= sqlite3OpenTableAndIndices(pParse
, pTab
, OP_OpenWrite
, 0, -1, 0,
963 &iDataCur
, &iIdxCur
);
964 aRegIdx
= sqlite3DbMallocRawNN(db
, sizeof(int)*(nIdx
+2));
968 for(i
=0, pIdx
=pTab
->pIndex
; i
<nIdx
; pIdx
=pIdx
->pNext
, i
++){
970 aRegIdx
[i
] = ++pParse
->nMem
;
971 pParse
->nMem
+= pIdx
->nColumn
;
973 aRegIdx
[i
] = ++pParse
->nMem
; /* Register to store the table record */
975 #ifndef SQLITE_OMIT_UPSERT
977 if( IsVirtual(pTab
) ){
978 sqlite3ErrorMsg(pParse
, "UPSERT not implemented for virtual table \"%s\"",
983 sqlite3ErrorMsg(pParse
, "cannot UPSERT a view");
986 if( sqlite3HasExplicitNulls(pParse
, pUpsert
->pUpsertTarget
) ){
989 pTabList
->a
[0].iCursor
= iDataCur
;
990 pUpsert
->pUpsertSrc
= pTabList
;
991 pUpsert
->regData
= regData
;
992 pUpsert
->iDataCur
= iDataCur
;
993 pUpsert
->iIdxCur
= iIdxCur
;
994 if( pUpsert
->pUpsertTarget
){
995 sqlite3UpsertAnalyzeTarget(pParse
, pTabList
, pUpsert
);
1001 /* This is the top of the main insertion loop */
1003 /* This block codes the top of loop only. The complete loop is the
1004 ** following pseudocode (template 4):
1006 ** rewind temp table, if empty goto D
1007 ** C: loop over rows of intermediate table
1008 ** transfer values form intermediate table into <table>
1012 addrInsTop
= sqlite3VdbeAddOp1(v
, OP_Rewind
, srcTab
); VdbeCoverage(v
);
1013 addrCont
= sqlite3VdbeCurrentAddr(v
);
1014 }else if( pSelect
){
1015 /* This block codes the top of loop only. The complete loop is the
1016 ** following pseudocode (template 3):
1018 ** C: yield X, at EOF goto D
1019 ** insert the select result into <table> from R..R+n
1023 sqlite3VdbeReleaseRegisters(pParse
, regData
, pTab
->nCol
, 0, 0);
1024 addrInsTop
= addrCont
= sqlite3VdbeAddOp1(v
, OP_Yield
, dest
.iSDParm
);
1027 /* tag-20191021-001: If the INTEGER PRIMARY KEY is being generated by the
1028 ** SELECT, go ahead and copy the value into the rowid slot now, so that
1029 ** the value does not get overwritten by a NULL at tag-20191021-002. */
1030 sqlite3VdbeAddOp2(v
, OP_Copy
, regFromSelect
+ipkColumn
, regRowid
);
1034 /* Compute data for ordinary columns of the new entry. Values
1035 ** are written in storage order into registers starting with regData.
1036 ** Only ordinary columns are computed in this loop. The rowid
1037 ** (if there is one) is computed later and generated columns are
1038 ** computed after the rowid since they might depend on the value
1042 iRegStore
= regData
; assert( regData
==regRowid
+1 );
1043 for(i
=0; i
<pTab
->nCol
; i
++, iRegStore
++){
1046 assert( i
>=nHidden
);
1047 if( i
==pTab
->iPKey
){
1048 /* tag-20191021-002: References to the INTEGER PRIMARY KEY are filled
1049 ** using the rowid. So put a NULL in the IPK slot of the record to avoid
1050 ** using excess space. The file format definition requires this extra
1051 ** NULL - we cannot optimize further by skipping the column completely */
1052 sqlite3VdbeAddOp1(v
, OP_SoftNull
, iRegStore
);
1055 if( ((colFlags
= pTab
->aCol
[i
].colFlags
) & COLFLAG_NOINSERT
)!=0 ){
1057 if( (colFlags
& COLFLAG_VIRTUAL
)!=0 ){
1058 /* Virtual columns do not participate in OP_MakeRecord. So back up
1059 ** iRegStore by one slot to compensate for the iRegStore++ in the
1060 ** outer for() loop */
1063 }else if( (colFlags
& COLFLAG_STORED
)!=0 ){
1064 /* Stored columns are computed later. But if there are BEFORE
1065 ** triggers, the slots used for stored columns will be OP_Copy-ed
1066 ** to a second block of registers, so the register needs to be
1067 ** initialized to NULL to avoid an uninitialized register read */
1068 if( tmask
& TRIGGER_BEFORE
){
1069 sqlite3VdbeAddOp1(v
, OP_SoftNull
, iRegStore
);
1072 }else if( pColumn
==0 ){
1073 /* Hidden columns that are not explicitly named in the INSERT
1074 ** get there default value */
1075 sqlite3ExprCodeFactorable(pParse
, pTab
->aCol
[i
].pDflt
, iRegStore
);
1080 for(j
=0; j
<pColumn
->nId
&& pColumn
->a
[j
].idx
!=i
; j
++){}
1081 if( j
>=pColumn
->nId
){
1082 /* A column not named in the insert column list gets its
1084 sqlite3ExprCodeFactorable(pParse
, pTab
->aCol
[i
].pDflt
, iRegStore
);
1088 }else if( nColumn
==0 ){
1089 /* This is INSERT INTO ... DEFAULT VALUES. Load the default value. */
1090 sqlite3ExprCodeFactorable(pParse
, pTab
->aCol
[i
].pDflt
, iRegStore
);
1097 sqlite3VdbeAddOp3(v
, OP_Column
, srcTab
, k
, iRegStore
);
1098 }else if( pSelect
){
1099 if( regFromSelect
!=regData
){
1100 sqlite3VdbeAddOp2(v
, OP_SCopy
, regFromSelect
+k
, iRegStore
);
1103 sqlite3ExprCode(pParse
, pList
->a
[k
].pExpr
, iRegStore
);
1108 /* Run the BEFORE and INSTEAD OF triggers, if there are any
1110 endOfLoop
= sqlite3VdbeMakeLabel(pParse
);
1111 if( tmask
& TRIGGER_BEFORE
){
1112 int regCols
= sqlite3GetTempRange(pParse
, pTab
->nCol
+1);
1114 /* build the NEW.* reference row. Note that if there is an INTEGER
1115 ** PRIMARY KEY into which a NULL is being inserted, that NULL will be
1116 ** translated into a unique ID for the row. But on a BEFORE trigger,
1117 ** we do not know what the unique ID will be (because the insert has
1118 ** not happened yet) so we substitute a rowid of -1
1121 sqlite3VdbeAddOp2(v
, OP_Integer
, -1, regCols
);
1124 assert( !withoutRowid
);
1126 sqlite3VdbeAddOp3(v
, OP_Column
, srcTab
, ipkColumn
, regCols
);
1128 assert( pSelect
==0 ); /* Otherwise useTempTable is true */
1129 sqlite3ExprCode(pParse
, pList
->a
[ipkColumn
].pExpr
, regCols
);
1131 addr1
= sqlite3VdbeAddOp1(v
, OP_NotNull
, regCols
); VdbeCoverage(v
);
1132 sqlite3VdbeAddOp2(v
, OP_Integer
, -1, regCols
);
1133 sqlite3VdbeJumpHere(v
, addr1
);
1134 sqlite3VdbeAddOp1(v
, OP_MustBeInt
, regCols
); VdbeCoverage(v
);
1137 /* Cannot have triggers on a virtual table. If it were possible,
1138 ** this block would have to account for hidden column.
1140 assert( !IsVirtual(pTab
) );
1142 /* Copy the new data already generated. */
1143 assert( pTab
->nNVCol
>0 );
1144 sqlite3VdbeAddOp3(v
, OP_Copy
, regRowid
+1, regCols
+1, pTab
->nNVCol
-1);
1146 #ifndef SQLITE_OMIT_GENERATED_COLUMNS
1147 /* Compute the new value for generated columns after all other
1148 ** columns have already been computed. This must be done after
1149 ** computing the ROWID in case one of the generated columns
1150 ** refers to the ROWID. */
1151 if( pTab
->tabFlags
& TF_HasGenerated
){
1152 testcase( pTab
->tabFlags
& TF_HasVirtual
);
1153 testcase( pTab
->tabFlags
& TF_HasStored
);
1154 sqlite3ComputeGeneratedColumns(pParse
, regCols
+1, pTab
);
1158 /* If this is an INSERT on a view with an INSTEAD OF INSERT trigger,
1159 ** do not attempt any conversions before assembling the record.
1160 ** If this is a real table, attempt conversions as required by the
1161 ** table column affinities.
1164 sqlite3TableAffinity(v
, pTab
, regCols
+1);
1167 /* Fire BEFORE or INSTEAD OF triggers */
1168 sqlite3CodeRowTrigger(pParse
, pTrigger
, TK_INSERT
, 0, TRIGGER_BEFORE
,
1169 pTab
, regCols
-pTab
->nCol
-1, onError
, endOfLoop
);
1171 sqlite3ReleaseTempRange(pParse
, regCols
, pTab
->nCol
+1);
1175 if( IsVirtual(pTab
) ){
1176 /* The row that the VUpdate opcode will delete: none */
1177 sqlite3VdbeAddOp2(v
, OP_Null
, 0, regIns
);
1180 /* Compute the new rowid */
1182 sqlite3VdbeAddOp3(v
, OP_Column
, srcTab
, ipkColumn
, regRowid
);
1183 }else if( pSelect
){
1184 /* Rowid already initialized at tag-20191021-001 */
1186 Expr
*pIpk
= pList
->a
[ipkColumn
].pExpr
;
1187 if( pIpk
->op
==TK_NULL
&& !IsVirtual(pTab
) ){
1188 sqlite3VdbeAddOp3(v
, OP_NewRowid
, iDataCur
, regRowid
, regAutoinc
);
1191 sqlite3ExprCode(pParse
, pList
->a
[ipkColumn
].pExpr
, regRowid
);
1194 /* If the PRIMARY KEY expression is NULL, then use OP_NewRowid
1195 ** to generate a unique primary key value.
1199 if( !IsVirtual(pTab
) ){
1200 addr1
= sqlite3VdbeAddOp1(v
, OP_NotNull
, regRowid
); VdbeCoverage(v
);
1201 sqlite3VdbeAddOp3(v
, OP_NewRowid
, iDataCur
, regRowid
, regAutoinc
);
1202 sqlite3VdbeJumpHere(v
, addr1
);
1204 addr1
= sqlite3VdbeCurrentAddr(v
);
1205 sqlite3VdbeAddOp2(v
, OP_IsNull
, regRowid
, addr1
+2); VdbeCoverage(v
);
1207 sqlite3VdbeAddOp1(v
, OP_MustBeInt
, regRowid
); VdbeCoverage(v
);
1209 }else if( IsVirtual(pTab
) || withoutRowid
){
1210 sqlite3VdbeAddOp2(v
, OP_Null
, 0, regRowid
);
1212 sqlite3VdbeAddOp3(v
, OP_NewRowid
, iDataCur
, regRowid
, regAutoinc
);
1215 autoIncStep(pParse
, regAutoinc
, regRowid
);
1217 #ifndef SQLITE_OMIT_GENERATED_COLUMNS
1218 /* Compute the new value for generated columns after all other
1219 ** columns have already been computed. This must be done after
1220 ** computing the ROWID in case one of the generated columns
1221 ** is derived from the INTEGER PRIMARY KEY. */
1222 if( pTab
->tabFlags
& TF_HasGenerated
){
1223 sqlite3ComputeGeneratedColumns(pParse
, regRowid
+1, pTab
);
1227 /* Generate code to check constraints and generate index keys and
1228 ** do the insertion.
1230 #ifndef SQLITE_OMIT_VIRTUALTABLE
1231 if( IsVirtual(pTab
) ){
1232 const char *pVTab
= (const char *)sqlite3GetVTable(db
, pTab
);
1233 sqlite3VtabMakeWritable(pParse
, pTab
);
1234 sqlite3VdbeAddOp4(v
, OP_VUpdate
, 1, pTab
->nCol
+2, regIns
, pVTab
, P4_VTAB
);
1235 sqlite3VdbeChangeP5(v
, onError
==OE_Default
? OE_Abort
: onError
);
1236 sqlite3MayAbort(pParse
);
1240 int isReplace
; /* Set to true if constraints may cause a replace */
1241 int bUseSeek
; /* True to use OPFLAG_SEEKRESULT */
1242 sqlite3GenerateConstraintChecks(pParse
, pTab
, aRegIdx
, iDataCur
, iIdxCur
,
1243 regIns
, 0, ipkColumn
>=0, onError
, endOfLoop
, &isReplace
, 0, pUpsert
1245 sqlite3FkCheck(pParse
, pTab
, 0, regIns
, 0, 0);
1247 /* Set the OPFLAG_USESEEKRESULT flag if either (a) there are no REPLACE
1248 ** constraints or (b) there are no triggers and this table is not a
1249 ** parent table in a foreign key constraint. It is safe to set the
1250 ** flag in the second case as if any REPLACE constraint is hit, an
1251 ** OP_Delete or OP_IdxDelete instruction will be executed on each
1252 ** cursor that is disturbed. And these instructions both clear the
1253 ** VdbeCursor.seekResult variable, disabling the OPFLAG_USESEEKRESULT
1254 ** functionality. */
1255 bUseSeek
= (isReplace
==0 || !sqlite3VdbeHasSubProgram(v
));
1256 sqlite3CompleteInsertion(pParse
, pTab
, iDataCur
, iIdxCur
,
1257 regIns
, aRegIdx
, 0, appendFlag
, bUseSeek
1262 /* Update the count of rows that are inserted
1265 sqlite3VdbeAddOp2(v
, OP_AddImm
, regRowCount
, 1);
1269 /* Code AFTER triggers */
1270 sqlite3CodeRowTrigger(pParse
, pTrigger
, TK_INSERT
, 0, TRIGGER_AFTER
,
1271 pTab
, regData
-2-pTab
->nCol
, onError
, endOfLoop
);
1274 /* The bottom of the main insertion loop, if the data source
1275 ** is a SELECT statement.
1277 sqlite3VdbeResolveLabel(v
, endOfLoop
);
1279 sqlite3VdbeAddOp2(v
, OP_Next
, srcTab
, addrCont
); VdbeCoverage(v
);
1280 sqlite3VdbeJumpHere(v
, addrInsTop
);
1281 sqlite3VdbeAddOp1(v
, OP_Close
, srcTab
);
1282 }else if( pSelect
){
1283 sqlite3VdbeGoto(v
, addrCont
);
1285 /* If we are jumping back to an OP_Yield that is preceded by an
1286 ** OP_ReleaseReg, set the p5 flag on the OP_Goto so that the
1287 ** OP_ReleaseReg will be included in the loop. */
1288 if( sqlite3VdbeGetOp(v
, addrCont
-1)->opcode
==OP_ReleaseReg
){
1289 assert( sqlite3VdbeGetOp(v
, addrCont
)->opcode
==OP_Yield
);
1290 sqlite3VdbeChangeP5(v
, 1);
1293 sqlite3VdbeJumpHere(v
, addrInsTop
);
1297 /* Update the sqlite_sequence table by storing the content of the
1298 ** maximum rowid counter values recorded while inserting into
1299 ** autoincrement tables.
1301 if( pParse
->nested
==0 && pParse
->pTriggerTab
==0 ){
1302 sqlite3AutoincrementEnd(pParse
);
1306 ** Return the number of rows inserted. If this routine is
1307 ** generating code because of a call to sqlite3NestedParse(), do not
1308 ** invoke the callback function.
1311 sqlite3VdbeAddOp2(v
, OP_ResultRow
, regRowCount
, 1);
1312 sqlite3VdbeSetNumCols(v
, 1);
1313 sqlite3VdbeSetColName(v
, 0, COLNAME_NAME
, "rows inserted", SQLITE_STATIC
);
1317 sqlite3SrcListDelete(db
, pTabList
);
1318 sqlite3ExprListDelete(db
, pList
);
1319 sqlite3UpsertDelete(db
, pUpsert
);
1320 sqlite3SelectDelete(db
, pSelect
);
1321 sqlite3IdListDelete(db
, pColumn
);
1322 sqlite3DbFree(db
, aRegIdx
);
1325 /* Make sure "isView" and other macros defined above are undefined. Otherwise
1326 ** they may interfere with compilation of other functions in this file
1327 ** (or in another file, if this file becomes part of the amalgamation). */
1339 ** Meanings of bits in of pWalker->eCode for
1340 ** sqlite3ExprReferencesUpdatedColumn()
1342 #define CKCNSTRNT_COLUMN 0x01 /* CHECK constraint uses a changing column */
1343 #define CKCNSTRNT_ROWID 0x02 /* CHECK constraint references the ROWID */
1345 /* This is the Walker callback from sqlite3ExprReferencesUpdatedColumn().
1346 * Set bit 0x01 of pWalker->eCode if pWalker->eCode to 0 and if this
1347 ** expression node references any of the
1348 ** columns that are being modifed by an UPDATE statement.
1350 static int checkConstraintExprNode(Walker
*pWalker
, Expr
*pExpr
){
1351 if( pExpr
->op
==TK_COLUMN
){
1352 assert( pExpr
->iColumn
>=0 || pExpr
->iColumn
==-1 );
1353 if( pExpr
->iColumn
>=0 ){
1354 if( pWalker
->u
.aiCol
[pExpr
->iColumn
]>=0 ){
1355 pWalker
->eCode
|= CKCNSTRNT_COLUMN
;
1358 pWalker
->eCode
|= CKCNSTRNT_ROWID
;
1361 return WRC_Continue
;
1365 ** pExpr is a CHECK constraint on a row that is being UPDATE-ed. The
1366 ** only columns that are modified by the UPDATE are those for which
1367 ** aiChng[i]>=0, and also the ROWID is modified if chngRowid is true.
1369 ** Return true if CHECK constraint pExpr uses any of the
1370 ** changing columns (or the rowid if it is changing). In other words,
1371 ** return true if this CHECK constraint must be validated for
1372 ** the new row in the UPDATE statement.
1374 ** 2018-09-15: pExpr might also be an expression for an index-on-expressions.
1375 ** The operation of this routine is the same - return true if an only if
1376 ** the expression uses one or more of columns identified by the second and
1379 int sqlite3ExprReferencesUpdatedColumn(
1380 Expr
*pExpr
, /* The expression to be checked */
1381 int *aiChng
, /* aiChng[x]>=0 if column x changed by the UPDATE */
1382 int chngRowid
/* True if UPDATE changes the rowid */
1385 memset(&w
, 0, sizeof(w
));
1387 w
.xExprCallback
= checkConstraintExprNode
;
1389 sqlite3WalkExpr(&w
, pExpr
);
1391 testcase( (w
.eCode
& CKCNSTRNT_ROWID
)!=0 );
1392 w
.eCode
&= ~CKCNSTRNT_ROWID
;
1394 testcase( w
.eCode
==0 );
1395 testcase( w
.eCode
==CKCNSTRNT_COLUMN
);
1396 testcase( w
.eCode
==CKCNSTRNT_ROWID
);
1397 testcase( w
.eCode
==(CKCNSTRNT_ROWID
|CKCNSTRNT_COLUMN
) );
1402 ** Generate code to do constraint checks prior to an INSERT or an UPDATE
1405 ** The regNewData parameter is the first register in a range that contains
1406 ** the data to be inserted or the data after the update. There will be
1407 ** pTab->nCol+1 registers in this range. The first register (the one
1408 ** that regNewData points to) will contain the new rowid, or NULL in the
1409 ** case of a WITHOUT ROWID table. The second register in the range will
1410 ** contain the content of the first table column. The third register will
1411 ** contain the content of the second table column. And so forth.
1413 ** The regOldData parameter is similar to regNewData except that it contains
1414 ** the data prior to an UPDATE rather than afterwards. regOldData is zero
1415 ** for an INSERT. This routine can distinguish between UPDATE and INSERT by
1416 ** checking regOldData for zero.
1418 ** For an UPDATE, the pkChng boolean is true if the true primary key (the
1419 ** rowid for a normal table or the PRIMARY KEY for a WITHOUT ROWID table)
1420 ** might be modified by the UPDATE. If pkChng is false, then the key of
1421 ** the iDataCur content table is guaranteed to be unchanged by the UPDATE.
1423 ** For an INSERT, the pkChng boolean indicates whether or not the rowid
1424 ** was explicitly specified as part of the INSERT statement. If pkChng
1425 ** is zero, it means that the either rowid is computed automatically or
1426 ** that the table is a WITHOUT ROWID table and has no rowid. On an INSERT,
1427 ** pkChng will only be true if the INSERT statement provides an integer
1428 ** value for either the rowid column or its INTEGER PRIMARY KEY alias.
1430 ** The code generated by this routine will store new index entries into
1431 ** registers identified by aRegIdx[]. No index entry is created for
1432 ** indices where aRegIdx[i]==0. The order of indices in aRegIdx[] is
1433 ** the same as the order of indices on the linked list of indices
1436 ** (2019-05-07) The generated code also creates a new record for the
1437 ** main table, if pTab is a rowid table, and stores that record in the
1438 ** register identified by aRegIdx[nIdx] - in other words in the first
1439 ** entry of aRegIdx[] past the last index. It is important that the
1440 ** record be generated during constraint checks to avoid affinity changes
1441 ** to the register content that occur after constraint checks but before
1442 ** the new record is inserted.
1444 ** The caller must have already opened writeable cursors on the main
1445 ** table and all applicable indices (that is to say, all indices for which
1446 ** aRegIdx[] is not zero). iDataCur is the cursor for the main table when
1447 ** inserting or updating a rowid table, or the cursor for the PRIMARY KEY
1448 ** index when operating on a WITHOUT ROWID table. iIdxCur is the cursor
1449 ** for the first index in the pTab->pIndex list. Cursors for other indices
1450 ** are at iIdxCur+N for the N-th element of the pTab->pIndex list.
1452 ** This routine also generates code to check constraints. NOT NULL,
1453 ** CHECK, and UNIQUE constraints are all checked. If a constraint fails,
1454 ** then the appropriate action is performed. There are five possible
1455 ** actions: ROLLBACK, ABORT, FAIL, REPLACE, and IGNORE.
1457 ** Constraint type Action What Happens
1458 ** --------------- ---------- ----------------------------------------
1459 ** any ROLLBACK The current transaction is rolled back and
1460 ** sqlite3_step() returns immediately with a
1461 ** return code of SQLITE_CONSTRAINT.
1463 ** any ABORT Back out changes from the current command
1464 ** only (do not do a complete rollback) then
1465 ** cause sqlite3_step() to return immediately
1466 ** with SQLITE_CONSTRAINT.
1468 ** any FAIL Sqlite3_step() returns immediately with a
1469 ** return code of SQLITE_CONSTRAINT. The
1470 ** transaction is not rolled back and any
1471 ** changes to prior rows are retained.
1473 ** any IGNORE The attempt in insert or update the current
1474 ** row is skipped, without throwing an error.
1475 ** Processing continues with the next row.
1476 ** (There is an immediate jump to ignoreDest.)
1478 ** NOT NULL REPLACE The NULL value is replace by the default
1479 ** value for that column. If the default value
1480 ** is NULL, the action is the same as ABORT.
1482 ** UNIQUE REPLACE The other row that conflicts with the row
1483 ** being inserted is removed.
1485 ** CHECK REPLACE Illegal. The results in an exception.
1487 ** Which action to take is determined by the overrideError parameter.
1488 ** Or if overrideError==OE_Default, then the pParse->onError parameter
1489 ** is used. Or if pParse->onError==OE_Default then the onError value
1490 ** for the constraint is used.
1492 void sqlite3GenerateConstraintChecks(
1493 Parse
*pParse
, /* The parser context */
1494 Table
*pTab
, /* The table being inserted or updated */
1495 int *aRegIdx
, /* Use register aRegIdx[i] for index i. 0 for unused */
1496 int iDataCur
, /* Canonical data cursor (main table or PK index) */
1497 int iIdxCur
, /* First index cursor */
1498 int regNewData
, /* First register in a range holding values to insert */
1499 int regOldData
, /* Previous content. 0 for INSERTs */
1500 u8 pkChng
, /* Non-zero if the rowid or PRIMARY KEY changed */
1501 u8 overrideError
, /* Override onError to this if not OE_Default */
1502 int ignoreDest
, /* Jump to this label on an OE_Ignore resolution */
1503 int *pbMayReplace
, /* OUT: Set to true if constraint may cause a replace */
1504 int *aiChng
, /* column i is unchanged if aiChng[i]<0 */
1505 Upsert
*pUpsert
/* ON CONFLICT clauses, if any. NULL otherwise */
1507 Vdbe
*v
; /* VDBE under constrution */
1508 Index
*pIdx
; /* Pointer to one of the indices */
1509 Index
*pPk
= 0; /* The PRIMARY KEY index */
1510 sqlite3
*db
; /* Database connection */
1511 int i
; /* loop counter */
1512 int ix
; /* Index loop counter */
1513 int nCol
; /* Number of columns */
1514 int onError
; /* Conflict resolution strategy */
1515 int seenReplace
= 0; /* True if REPLACE is used to resolve INT PK conflict */
1516 int nPkField
; /* Number of fields in PRIMARY KEY. 1 for ROWID tables */
1517 Index
*pUpIdx
= 0; /* Index to which to apply the upsert */
1518 u8 isUpdate
; /* True if this is an UPDATE operation */
1519 u8 bAffinityDone
= 0; /* True if the OP_Affinity operation has been run */
1520 int upsertBypass
= 0; /* Address of Goto to bypass upsert subroutine */
1521 int upsertJump
= 0; /* Address of Goto that jumps into upsert subroutine */
1522 int ipkTop
= 0; /* Top of the IPK uniqueness check */
1523 int ipkBottom
= 0; /* OP_Goto at the end of the IPK uniqueness check */
1524 /* Variables associated with retesting uniqueness constraints after
1525 ** replace triggers fire have run */
1526 int regTrigCnt
; /* Register used to count replace trigger invocations */
1527 int addrRecheck
= 0; /* Jump here to recheck all uniqueness constraints */
1528 int lblRecheckOk
= 0; /* Each recheck jumps to this label if it passes */
1529 Trigger
*pTrigger
; /* List of DELETE triggers on the table pTab */
1530 int nReplaceTrig
= 0; /* Number of replace triggers coded */
1532 isUpdate
= regOldData
!=0;
1534 v
= sqlite3GetVdbe(pParse
);
1536 assert( pTab
->pSelect
==0 ); /* This table is not a VIEW */
1539 /* pPk is the PRIMARY KEY index for WITHOUT ROWID tables and NULL for
1540 ** normal rowid tables. nPkField is the number of key fields in the
1541 ** pPk index or 1 for a rowid table. In other words, nPkField is the
1542 ** number of fields in the true primary key of the table. */
1543 if( HasRowid(pTab
) ){
1547 pPk
= sqlite3PrimaryKeyIndex(pTab
);
1548 nPkField
= pPk
->nKeyCol
;
1551 /* Record that this module has started */
1552 VdbeModuleComment((v
, "BEGIN: GenCnstCks(%d,%d,%d,%d,%d)",
1553 iDataCur
, iIdxCur
, regNewData
, regOldData
, pkChng
));
1555 /* Test all NOT NULL constraints.
1557 if( pTab
->tabFlags
& TF_HasNotNull
){
1558 int b2ndPass
= 0; /* True if currently running 2nd pass */
1559 int nSeenReplace
= 0; /* Number of ON CONFLICT REPLACE operations */
1560 int nGenerated
= 0; /* Number of generated columns with NOT NULL */
1561 while(1){ /* Make 2 passes over columns. Exit loop via "break" */
1562 for(i
=0; i
<nCol
; i
++){
1563 int iReg
; /* Register holding column value */
1564 Column
*pCol
= &pTab
->aCol
[i
]; /* The column to check for NOT NULL */
1565 int isGenerated
; /* non-zero if column is generated */
1566 onError
= pCol
->notNull
;
1567 if( onError
==OE_None
) continue; /* No NOT NULL on this column */
1568 if( i
==pTab
->iPKey
){
1569 continue; /* ROWID is never NULL */
1571 isGenerated
= pCol
->colFlags
& COLFLAG_GENERATED
;
1572 if( isGenerated
&& !b2ndPass
){
1574 continue; /* Generated columns processed on 2nd pass */
1576 if( aiChng
&& aiChng
[i
]<0 && !isGenerated
){
1577 /* Do not check NOT NULL on columns that do not change */
1580 if( overrideError
!=OE_Default
){
1581 onError
= overrideError
;
1582 }else if( onError
==OE_Default
){
1585 if( onError
==OE_Replace
){
1586 if( b2ndPass
/* REPLACE becomes ABORT on the 2nd pass */
1587 || pCol
->pDflt
==0 /* REPLACE is ABORT if no DEFAULT value */
1589 testcase( pCol
->colFlags
& COLFLAG_VIRTUAL
);
1590 testcase( pCol
->colFlags
& COLFLAG_STORED
);
1591 testcase( pCol
->colFlags
& COLFLAG_GENERATED
);
1594 assert( !isGenerated
);
1596 }else if( b2ndPass
&& !isGenerated
){
1599 assert( onError
==OE_Rollback
|| onError
==OE_Abort
|| onError
==OE_Fail
1600 || onError
==OE_Ignore
|| onError
==OE_Replace
);
1601 testcase( i
!=sqlite3TableColumnToStorage(pTab
, i
) );
1602 iReg
= sqlite3TableColumnToStorage(pTab
, i
) + regNewData
+ 1;
1605 int addr1
= sqlite3VdbeAddOp1(v
, OP_NotNull
, iReg
);
1607 assert( (pCol
->colFlags
& COLFLAG_GENERATED
)==0 );
1609 sqlite3ExprCodeCopy(pParse
, pCol
->pDflt
, iReg
);
1610 sqlite3VdbeJumpHere(v
, addr1
);
1614 sqlite3MayAbort(pParse
);
1615 /* no break */ deliberate_fall_through
1618 char *zMsg
= sqlite3MPrintf(db
, "%s.%s", pTab
->zName
,
1620 sqlite3VdbeAddOp3(v
, OP_HaltIfNull
, SQLITE_CONSTRAINT_NOTNULL
,
1622 sqlite3VdbeAppendP4(v
, zMsg
, P4_DYNAMIC
);
1623 sqlite3VdbeChangeP5(v
, P5_ConstraintNotNull
);
1628 assert( onError
==OE_Ignore
);
1629 sqlite3VdbeAddOp2(v
, OP_IsNull
, iReg
, ignoreDest
);
1633 } /* end switch(onError) */
1634 } /* end loop i over columns */
1635 if( nGenerated
==0 && nSeenReplace
==0 ){
1636 /* If there are no generated columns with NOT NULL constraints
1637 ** and no NOT NULL ON CONFLICT REPLACE constraints, then a single
1638 ** pass is sufficient */
1641 if( b2ndPass
) break; /* Never need more than 2 passes */
1643 #ifndef SQLITE_OMIT_GENERATED_COLUMNS
1644 if( nSeenReplace
>0 && (pTab
->tabFlags
& TF_HasGenerated
)!=0 ){
1645 /* If any NOT NULL ON CONFLICT REPLACE constraints fired on the
1646 ** first pass, recomputed values for all generated columns, as
1647 ** those values might depend on columns affected by the REPLACE.
1649 sqlite3ComputeGeneratedColumns(pParse
, regNewData
+1, pTab
);
1652 } /* end of 2-pass loop */
1653 } /* end if( has-not-null-constraints ) */
1655 /* Test all CHECK constraints
1657 #ifndef SQLITE_OMIT_CHECK
1658 if( pTab
->pCheck
&& (db
->flags
& SQLITE_IgnoreChecks
)==0 ){
1659 ExprList
*pCheck
= pTab
->pCheck
;
1660 pParse
->iSelfTab
= -(regNewData
+1);
1661 onError
= overrideError
!=OE_Default
? overrideError
: OE_Abort
;
1662 for(i
=0; i
<pCheck
->nExpr
; i
++){
1665 Expr
*pExpr
= pCheck
->a
[i
].pExpr
;
1667 && !sqlite3ExprReferencesUpdatedColumn(pExpr
, aiChng
, pkChng
)
1669 /* The check constraints do not reference any of the columns being
1670 ** updated so there is no point it verifying the check constraint */
1673 if( bAffinityDone
==0 ){
1674 sqlite3TableAffinity(v
, pTab
, regNewData
+1);
1677 allOk
= sqlite3VdbeMakeLabel(pParse
);
1678 sqlite3VdbeVerifyAbortable(v
, onError
);
1679 pCopy
= sqlite3ExprDup(db
, pExpr
, 0);
1680 if( !db
->mallocFailed
){
1681 sqlite3ExprIfTrue(pParse
, pCopy
, allOk
, SQLITE_JUMPIFNULL
);
1683 sqlite3ExprDelete(db
, pCopy
);
1684 if( onError
==OE_Ignore
){
1685 sqlite3VdbeGoto(v
, ignoreDest
);
1687 char *zName
= pCheck
->a
[i
].zEName
;
1688 if( zName
==0 ) zName
= pTab
->zName
;
1689 if( onError
==OE_Replace
) onError
= OE_Abort
; /* IMP: R-26383-51744 */
1690 sqlite3HaltConstraint(pParse
, SQLITE_CONSTRAINT_CHECK
,
1691 onError
, zName
, P4_TRANSIENT
,
1692 P5_ConstraintCheck
);
1694 sqlite3VdbeResolveLabel(v
, allOk
);
1696 pParse
->iSelfTab
= 0;
1698 #endif /* !defined(SQLITE_OMIT_CHECK) */
1700 /* UNIQUE and PRIMARY KEY constraints should be handled in the following
1704 ** (2) OE_Abort, OE_Fail, OE_Rollback, OE_Ignore
1707 ** OE_Fail and OE_Ignore must happen before any changes are made.
1708 ** OE_Update guarantees that only a single row will change, so it
1709 ** must happen before OE_Replace. Technically, OE_Abort and OE_Rollback
1710 ** could happen in any order, but they are grouped up front for
1713 ** 2018-08-14: Ticket https://www.sqlite.org/src/info/908f001483982c43
1714 ** The order of constraints used to have OE_Update as (2) and OE_Abort
1715 ** and so forth as (1). But apparently PostgreSQL checks the OE_Update
1716 ** constraint before any others, so it had to be moved.
1718 ** Constraint checking code is generated in this order:
1719 ** (A) The rowid constraint
1720 ** (B) Unique index constraints that do not have OE_Replace as their
1721 ** default conflict resolution strategy
1722 ** (C) Unique index that do use OE_Replace by default.
1724 ** The ordering of (2) and (3) is accomplished by making sure the linked
1725 ** list of indexes attached to a table puts all OE_Replace indexes last
1726 ** in the list. See sqlite3CreateIndex() for where that happens.
1730 if( pUpsert
->pUpsertTarget
==0 ){
1731 /* An ON CONFLICT DO NOTHING clause, without a constraint-target.
1732 ** Make all unique constraint resolution be OE_Ignore */
1733 assert( pUpsert
->pUpsertSet
==0 );
1734 overrideError
= OE_Ignore
;
1736 }else if( (pUpIdx
= pUpsert
->pUpsertIdx
)!=0 ){
1737 /* If the constraint-target uniqueness check must be run first.
1738 ** Jump to that uniqueness check now */
1739 upsertJump
= sqlite3VdbeAddOp0(v
, OP_Goto
);
1740 VdbeComment((v
, "UPSERT constraint goes first"));
1744 /* Determine if it is possible that triggers (either explicitly coded
1745 ** triggers or FK resolution actions) might run as a result of deletes
1746 ** that happen when OE_Replace conflict resolution occurs. (Call these
1747 ** "replace triggers".) If any replace triggers run, we will need to
1748 ** recheck all of the uniqueness constraints after they have all run.
1749 ** But on the recheck, the resolution is OE_Abort instead of OE_Replace.
1751 ** If replace triggers are a possibility, then
1753 ** (1) Allocate register regTrigCnt and initialize it to zero.
1754 ** That register will count the number of replace triggers that
1755 ** fire. Constraint recheck only occurs if the number is positive.
1756 ** (2) Initialize pTrigger to the list of all DELETE triggers on pTab.
1757 ** (3) Initialize addrRecheck and lblRecheckOk
1759 ** The uniqueness rechecking code will create a series of tests to run
1760 ** in a second pass. The addrRecheck and lblRecheckOk variables are
1761 ** used to link together these tests which are separated from each other
1762 ** in the generate bytecode.
1764 if( (db
->flags
& (SQLITE_RecTriggers
|SQLITE_ForeignKeys
))==0 ){
1765 /* There are not DELETE triggers nor FK constraints. No constraint
1766 ** rechecks are needed. */
1770 if( db
->flags
&SQLITE_RecTriggers
){
1771 pTrigger
= sqlite3TriggersExist(pParse
, pTab
, TK_DELETE
, 0, 0);
1772 regTrigCnt
= pTrigger
!=0 || sqlite3FkRequired(pParse
, pTab
, 0, 0);
1775 regTrigCnt
= sqlite3FkRequired(pParse
, pTab
, 0, 0);
1778 /* Replace triggers might exist. Allocate the counter and
1779 ** initialize it to zero. */
1780 regTrigCnt
= ++pParse
->nMem
;
1781 sqlite3VdbeAddOp2(v
, OP_Integer
, 0, regTrigCnt
);
1782 VdbeComment((v
, "trigger count"));
1783 lblRecheckOk
= sqlite3VdbeMakeLabel(pParse
);
1784 addrRecheck
= lblRecheckOk
;
1788 /* If rowid is changing, make sure the new rowid does not previously
1789 ** exist in the table.
1791 if( pkChng
&& pPk
==0 ){
1792 int addrRowidOk
= sqlite3VdbeMakeLabel(pParse
);
1794 /* Figure out what action to take in case of a rowid collision */
1795 onError
= pTab
->keyConf
;
1796 if( overrideError
!=OE_Default
){
1797 onError
= overrideError
;
1798 }else if( onError
==OE_Default
){
1802 /* figure out whether or not upsert applies in this case */
1803 if( pUpsert
&& pUpsert
->pUpsertIdx
==0 ){
1804 if( pUpsert
->pUpsertSet
==0 ){
1805 onError
= OE_Ignore
; /* DO NOTHING is the same as INSERT OR IGNORE */
1807 onError
= OE_Update
; /* DO UPDATE */
1811 /* If the response to a rowid conflict is REPLACE but the response
1812 ** to some other UNIQUE constraint is FAIL or IGNORE, then we need
1813 ** to defer the running of the rowid conflict checking until after
1814 ** the UNIQUE constraints have run.
1816 if( onError
==OE_Replace
/* IPK rule is REPLACE */
1817 && onError
!=overrideError
/* Rules for other contraints are different */
1818 && pTab
->pIndex
/* There exist other constraints */
1820 ipkTop
= sqlite3VdbeAddOp0(v
, OP_Goto
)+1;
1821 VdbeComment((v
, "defer IPK REPLACE until last"));
1825 /* pkChng!=0 does not mean that the rowid has changed, only that
1826 ** it might have changed. Skip the conflict logic below if the rowid
1828 sqlite3VdbeAddOp3(v
, OP_Eq
, regNewData
, addrRowidOk
, regOldData
);
1829 sqlite3VdbeChangeP5(v
, SQLITE_NOTNULL
);
1833 /* Check to see if the new rowid already exists in the table. Skip
1834 ** the following conflict logic if it does not. */
1835 VdbeNoopComment((v
, "uniqueness check for ROWID"));
1836 sqlite3VdbeVerifyAbortable(v
, onError
);
1837 sqlite3VdbeAddOp3(v
, OP_NotExists
, iDataCur
, addrRowidOk
, regNewData
);
1843 /* no break */ deliberate_fall_through
1848 testcase( onError
==OE_Rollback
);
1849 testcase( onError
==OE_Abort
);
1850 testcase( onError
==OE_Fail
);
1851 sqlite3RowidConstraint(pParse
, onError
, pTab
);
1855 /* If there are DELETE triggers on this table and the
1856 ** recursive-triggers flag is set, call GenerateRowDelete() to
1857 ** remove the conflicting row from the table. This will fire
1858 ** the triggers and remove both the table and index b-tree entries.
1860 ** Otherwise, if there are no triggers or the recursive-triggers
1861 ** flag is not set, but the table has one or more indexes, call
1862 ** GenerateRowIndexDelete(). This removes the index b-tree entries
1863 ** only. The table b-tree entry will be replaced by the new entry
1864 ** when it is inserted.
1866 ** If either GenerateRowDelete() or GenerateRowIndexDelete() is called,
1867 ** also invoke MultiWrite() to indicate that this VDBE may require
1868 ** statement rollback (if the statement is aborted after the delete
1869 ** takes place). Earlier versions called sqlite3MultiWrite() regardless,
1870 ** but being more selective here allows statements like:
1872 ** REPLACE INTO t(rowid) VALUES($newrowid)
1874 ** to run without a statement journal if there are no indexes on the
1878 sqlite3MultiWrite(pParse
);
1879 sqlite3GenerateRowDelete(pParse
, pTab
, pTrigger
, iDataCur
, iIdxCur
,
1880 regNewData
, 1, 0, OE_Replace
, 1, -1);
1881 sqlite3VdbeAddOp2(v
, OP_AddImm
, regTrigCnt
, 1); /* incr trigger cnt */
1884 #ifdef SQLITE_ENABLE_PREUPDATE_HOOK
1885 assert( HasRowid(pTab
) );
1886 /* This OP_Delete opcode fires the pre-update-hook only. It does
1887 ** not modify the b-tree. It is more efficient to let the coming
1888 ** OP_Insert replace the existing entry than it is to delete the
1889 ** existing entry and then insert a new one. */
1890 sqlite3VdbeAddOp2(v
, OP_Delete
, iDataCur
, OPFLAG_ISNOOP
);
1891 sqlite3VdbeAppendP4(v
, pTab
, P4_TABLE
);
1892 #endif /* SQLITE_ENABLE_PREUPDATE_HOOK */
1894 sqlite3MultiWrite(pParse
);
1895 sqlite3GenerateRowIndexDelete(pParse
, pTab
, iDataCur
, iIdxCur
,0,-1);
1901 #ifndef SQLITE_OMIT_UPSERT
1903 sqlite3UpsertDoUpdate(pParse
, pUpsert
, pTab
, 0, iDataCur
);
1904 /* no break */ deliberate_fall_through
1908 testcase( onError
==OE_Ignore
);
1909 sqlite3VdbeGoto(v
, ignoreDest
);
1913 sqlite3VdbeResolveLabel(v
, addrRowidOk
);
1915 ipkBottom
= sqlite3VdbeAddOp0(v
, OP_Goto
);
1916 sqlite3VdbeJumpHere(v
, ipkTop
-1);
1920 /* Test all UNIQUE constraints by creating entries for each UNIQUE
1921 ** index and making sure that duplicate entries do not already exist.
1922 ** Compute the revised record entries for indices as we go.
1924 ** This loop also handles the case of the PRIMARY KEY index for a
1925 ** WITHOUT ROWID table.
1927 for(ix
=0, pIdx
=pTab
->pIndex
; pIdx
; pIdx
=pIdx
->pNext
, ix
++){
1928 int regIdx
; /* Range of registers hold conent for pIdx */
1929 int regR
; /* Range of registers holding conflicting PK */
1930 int iThisCur
; /* Cursor for this UNIQUE index */
1931 int addrUniqueOk
; /* Jump here if the UNIQUE constraint is satisfied */
1932 int addrConflictCk
; /* First opcode in the conflict check logic */
1934 if( aRegIdx
[ix
]==0 ) continue; /* Skip indices that do not change */
1936 addrUniqueOk
= upsertJump
+1;
1937 upsertBypass
= sqlite3VdbeGoto(v
, 0);
1938 VdbeComment((v
, "Skip upsert subroutine"));
1939 sqlite3VdbeJumpHere(v
, upsertJump
);
1941 addrUniqueOk
= sqlite3VdbeMakeLabel(pParse
);
1943 if( bAffinityDone
==0 && (pUpIdx
==0 || pUpIdx
==pIdx
) ){
1944 sqlite3TableAffinity(v
, pTab
, regNewData
+1);
1947 VdbeNoopComment((v
, "prep index %s", pIdx
->zName
));
1948 iThisCur
= iIdxCur
+ix
;
1951 /* Skip partial indices for which the WHERE clause is not true */
1952 if( pIdx
->pPartIdxWhere
){
1953 sqlite3VdbeAddOp2(v
, OP_Null
, 0, aRegIdx
[ix
]);
1954 pParse
->iSelfTab
= -(regNewData
+1);
1955 sqlite3ExprIfFalseDup(pParse
, pIdx
->pPartIdxWhere
, addrUniqueOk
,
1957 pParse
->iSelfTab
= 0;
1960 /* Create a record for this index entry as it should appear after
1961 ** the insert or update. Store that record in the aRegIdx[ix] register
1963 regIdx
= aRegIdx
[ix
]+1;
1964 for(i
=0; i
<pIdx
->nColumn
; i
++){
1965 int iField
= pIdx
->aiColumn
[i
];
1967 if( iField
==XN_EXPR
){
1968 pParse
->iSelfTab
= -(regNewData
+1);
1969 sqlite3ExprCodeCopy(pParse
, pIdx
->aColExpr
->a
[i
].pExpr
, regIdx
+i
);
1970 pParse
->iSelfTab
= 0;
1971 VdbeComment((v
, "%s column %d", pIdx
->zName
, i
));
1972 }else if( iField
==XN_ROWID
|| iField
==pTab
->iPKey
){
1974 sqlite3VdbeAddOp2(v
, OP_IntCopy
, x
, regIdx
+i
);
1975 VdbeComment((v
, "rowid"));
1977 testcase( sqlite3TableColumnToStorage(pTab
, iField
)!=iField
);
1978 x
= sqlite3TableColumnToStorage(pTab
, iField
) + regNewData
+ 1;
1979 sqlite3VdbeAddOp2(v
, OP_SCopy
, x
, regIdx
+i
);
1980 VdbeComment((v
, "%s", pTab
->aCol
[iField
].zName
));
1983 sqlite3VdbeAddOp3(v
, OP_MakeRecord
, regIdx
, pIdx
->nColumn
, aRegIdx
[ix
]);
1984 VdbeComment((v
, "for %s", pIdx
->zName
));
1985 #ifdef SQLITE_ENABLE_NULL_TRIM
1986 if( pIdx
->idxType
==SQLITE_IDXTYPE_PRIMARYKEY
){
1987 sqlite3SetMakeRecordP5(v
, pIdx
->pTable
);
1990 sqlite3VdbeReleaseRegisters(pParse
, regIdx
, pIdx
->nColumn
, 0, 0);
1992 /* In an UPDATE operation, if this index is the PRIMARY KEY index
1993 ** of a WITHOUT ROWID table and there has been no change the
1994 ** primary key, then no collision is possible. The collision detection
1995 ** logic below can all be skipped. */
1996 if( isUpdate
&& pPk
==pIdx
&& pkChng
==0 ){
1997 sqlite3VdbeResolveLabel(v
, addrUniqueOk
);
2001 /* Find out what action to take in case there is a uniqueness conflict */
2002 onError
= pIdx
->onError
;
2003 if( onError
==OE_None
){
2004 sqlite3VdbeResolveLabel(v
, addrUniqueOk
);
2005 continue; /* pIdx is not a UNIQUE index */
2007 if( overrideError
!=OE_Default
){
2008 onError
= overrideError
;
2009 }else if( onError
==OE_Default
){
2013 /* Figure out if the upsert clause applies to this index */
2015 if( pUpsert
->pUpsertSet
==0 ){
2016 onError
= OE_Ignore
; /* DO NOTHING is the same as INSERT OR IGNORE */
2018 onError
= OE_Update
; /* DO UPDATE */
2022 /* Collision detection may be omitted if all of the following are true:
2023 ** (1) The conflict resolution algorithm is REPLACE
2024 ** (2) The table is a WITHOUT ROWID table
2025 ** (3) There are no secondary indexes on the table
2026 ** (4) No delete triggers need to be fired if there is a conflict
2027 ** (5) No FK constraint counters need to be updated if a conflict occurs.
2029 ** This is not possible for ENABLE_PREUPDATE_HOOK builds, as the row
2030 ** must be explicitly deleted in order to ensure any pre-update hook
2032 #ifndef SQLITE_ENABLE_PREUPDATE_HOOK
2033 if( (ix
==0 && pIdx
->pNext
==0) /* Condition 3 */
2034 && pPk
==pIdx
/* Condition 2 */
2035 && onError
==OE_Replace
/* Condition 1 */
2036 && ( 0==(db
->flags
&SQLITE_RecTriggers
) || /* Condition 4 */
2037 0==sqlite3TriggersExist(pParse
, pTab
, TK_DELETE
, 0, 0))
2038 && ( 0==(db
->flags
&SQLITE_ForeignKeys
) || /* Condition 5 */
2039 (0==pTab
->pFKey
&& 0==sqlite3FkReferences(pTab
)))
2041 sqlite3VdbeResolveLabel(v
, addrUniqueOk
);
2044 #endif /* ifndef SQLITE_ENABLE_PREUPDATE_HOOK */
2046 /* Check to see if the new index entry will be unique */
2047 sqlite3VdbeVerifyAbortable(v
, onError
);
2049 sqlite3VdbeAddOp4Int(v
, OP_NoConflict
, iThisCur
, addrUniqueOk
,
2050 regIdx
, pIdx
->nKeyCol
); VdbeCoverage(v
);
2052 /* Generate code to handle collisions */
2053 regR
= (pIdx
==pPk
) ? regIdx
: sqlite3GetTempRange(pParse
, nPkField
);
2054 if( isUpdate
|| onError
==OE_Replace
){
2055 if( HasRowid(pTab
) ){
2056 sqlite3VdbeAddOp2(v
, OP_IdxRowid
, iThisCur
, regR
);
2057 /* Conflict only if the rowid of the existing index entry
2058 ** is different from old-rowid */
2060 sqlite3VdbeAddOp3(v
, OP_Eq
, regR
, addrUniqueOk
, regOldData
);
2061 sqlite3VdbeChangeP5(v
, SQLITE_NOTNULL
);
2066 /* Extract the PRIMARY KEY from the end of the index entry and
2067 ** store it in registers regR..regR+nPk-1 */
2069 for(i
=0; i
<pPk
->nKeyCol
; i
++){
2070 assert( pPk
->aiColumn
[i
]>=0 );
2071 x
= sqlite3TableColumnToIndex(pIdx
, pPk
->aiColumn
[i
]);
2072 sqlite3VdbeAddOp3(v
, OP_Column
, iThisCur
, x
, regR
+i
);
2073 VdbeComment((v
, "%s.%s", pTab
->zName
,
2074 pTab
->aCol
[pPk
->aiColumn
[i
]].zName
));
2078 /* If currently processing the PRIMARY KEY of a WITHOUT ROWID
2079 ** table, only conflict if the new PRIMARY KEY values are actually
2080 ** different from the old.
2082 ** For a UNIQUE index, only conflict if the PRIMARY KEY values
2083 ** of the matched index row are different from the original PRIMARY
2084 ** KEY values of this row before the update. */
2085 int addrJump
= sqlite3VdbeCurrentAddr(v
)+pPk
->nKeyCol
;
2087 int regCmp
= (IsPrimaryKeyIndex(pIdx
) ? regIdx
: regR
);
2089 for(i
=0; i
<pPk
->nKeyCol
; i
++){
2090 char *p4
= (char*)sqlite3LocateCollSeq(pParse
, pPk
->azColl
[i
]);
2091 x
= pPk
->aiColumn
[i
];
2093 if( i
==(pPk
->nKeyCol
-1) ){
2094 addrJump
= addrUniqueOk
;
2097 x
= sqlite3TableColumnToStorage(pTab
, x
);
2098 sqlite3VdbeAddOp4(v
, op
,
2099 regOldData
+1+x
, addrJump
, regCmp
+i
, p4
, P4_COLLSEQ
2101 sqlite3VdbeChangeP5(v
, SQLITE_NOTNULL
);
2102 VdbeCoverageIf(v
, op
==OP_Eq
);
2103 VdbeCoverageIf(v
, op
==OP_Ne
);
2109 /* Generate code that executes if the new index entry is not unique */
2110 assert( onError
==OE_Rollback
|| onError
==OE_Abort
|| onError
==OE_Fail
2111 || onError
==OE_Ignore
|| onError
==OE_Replace
|| onError
==OE_Update
);
2116 testcase( onError
==OE_Rollback
);
2117 testcase( onError
==OE_Abort
);
2118 testcase( onError
==OE_Fail
);
2119 sqlite3UniqueConstraint(pParse
, onError
, pIdx
);
2122 #ifndef SQLITE_OMIT_UPSERT
2124 sqlite3UpsertDoUpdate(pParse
, pUpsert
, pTab
, pIdx
, iIdxCur
+ix
);
2125 /* no break */ deliberate_fall_through
2129 testcase( onError
==OE_Ignore
);
2130 sqlite3VdbeGoto(v
, ignoreDest
);
2134 int nConflictCk
; /* Number of opcodes in conflict check logic */
2136 assert( onError
==OE_Replace
);
2137 nConflictCk
= sqlite3VdbeCurrentAddr(v
) - addrConflictCk
;
2138 assert( nConflictCk
>0 );
2139 testcase( nConflictCk
>1 );
2141 sqlite3MultiWrite(pParse
);
2144 if( pTrigger
&& isUpdate
){
2145 sqlite3VdbeAddOp1(v
, OP_CursorLock
, iDataCur
);
2147 sqlite3GenerateRowDelete(pParse
, pTab
, pTrigger
, iDataCur
, iIdxCur
,
2148 regR
, nPkField
, 0, OE_Replace
,
2149 (pIdx
==pPk
? ONEPASS_SINGLE
: ONEPASS_OFF
), iThisCur
);
2150 if( pTrigger
&& isUpdate
){
2151 sqlite3VdbeAddOp1(v
, OP_CursorUnlock
, iDataCur
);
2154 int addrBypass
; /* Jump destination to bypass recheck logic */
2156 sqlite3VdbeAddOp2(v
, OP_AddImm
, regTrigCnt
, 1); /* incr trigger cnt */
2157 addrBypass
= sqlite3VdbeAddOp0(v
, OP_Goto
); /* Bypass recheck */
2158 VdbeComment((v
, "bypass recheck"));
2160 /* Here we insert code that will be invoked after all constraint
2161 ** checks have run, if and only if one or more replace triggers
2163 sqlite3VdbeResolveLabel(v
, lblRecheckOk
);
2164 lblRecheckOk
= sqlite3VdbeMakeLabel(pParse
);
2165 if( pIdx
->pPartIdxWhere
){
2166 /* Bypass the recheck if this partial index is not defined
2167 ** for the current row */
2168 sqlite3VdbeAddOp2(v
, OP_IsNull
, regIdx
-1, lblRecheckOk
);
2171 /* Copy the constraint check code from above, except change
2172 ** the constraint-ok jump destination to be the address of
2173 ** the next retest block */
2174 while( nConflictCk
>0 ){
2175 VdbeOp x
; /* Conflict check opcode to copy */
2176 /* The sqlite3VdbeAddOp4() call might reallocate the opcode array.
2177 ** Hence, make a complete copy of the opcode, rather than using
2178 ** a pointer to the opcode. */
2179 x
= *sqlite3VdbeGetOp(v
, addrConflictCk
);
2180 if( x
.opcode
!=OP_IdxRowid
){
2181 int p2
; /* New P2 value for copied conflict check opcode */
2183 if( sqlite3OpcodeProperty
[x
.opcode
]&OPFLG_JUMP
){
2188 zP4
= x
.p4type
==P4_INT32
? SQLITE_INT_TO_PTR(x
.p4
.i
) : x
.p4
.z
;
2189 sqlite3VdbeAddOp4(v
, x
.opcode
, x
.p1
, p2
, x
.p3
, zP4
, x
.p4type
);
2190 sqlite3VdbeChangeP5(v
, x
.p5
);
2191 VdbeCoverageIf(v
, p2
!=x
.p2
);
2196 /* If the retest fails, issue an abort */
2197 sqlite3UniqueConstraint(pParse
, OE_Abort
, pIdx
);
2199 sqlite3VdbeJumpHere(v
, addrBypass
); /* Terminate the recheck bypass */
2206 sqlite3VdbeGoto(v
, upsertJump
+1);
2207 sqlite3VdbeJumpHere(v
, upsertBypass
);
2209 sqlite3VdbeResolveLabel(v
, addrUniqueOk
);
2211 if( regR
!=regIdx
) sqlite3ReleaseTempRange(pParse
, regR
, nPkField
);
2214 /* If the IPK constraint is a REPLACE, run it last */
2216 sqlite3VdbeGoto(v
, ipkTop
);
2217 VdbeComment((v
, "Do IPK REPLACE"));
2218 sqlite3VdbeJumpHere(v
, ipkBottom
);
2221 /* Recheck all uniqueness constraints after replace triggers have run */
2222 testcase( regTrigCnt
!=0 && nReplaceTrig
==0 );
2223 assert( regTrigCnt
!=0 || nReplaceTrig
==0 );
2225 sqlite3VdbeAddOp2(v
, OP_IfNot
, regTrigCnt
, lblRecheckOk
);VdbeCoverage(v
);
2228 sqlite3VdbeAddOp3(v
, OP_Eq
, regNewData
, addrRecheck
, regOldData
);
2229 sqlite3VdbeChangeP5(v
, SQLITE_NOTNULL
);
2232 sqlite3VdbeAddOp3(v
, OP_NotExists
, iDataCur
, addrRecheck
, regNewData
);
2234 sqlite3RowidConstraint(pParse
, OE_Abort
, pTab
);
2236 sqlite3VdbeGoto(v
, addrRecheck
);
2238 sqlite3VdbeResolveLabel(v
, lblRecheckOk
);
2241 /* Generate the table record */
2242 if( HasRowid(pTab
) ){
2243 int regRec
= aRegIdx
[ix
];
2244 sqlite3VdbeAddOp3(v
, OP_MakeRecord
, regNewData
+1, pTab
->nNVCol
, regRec
);
2245 sqlite3SetMakeRecordP5(v
, pTab
);
2246 if( !bAffinityDone
){
2247 sqlite3TableAffinity(v
, pTab
, 0);
2251 *pbMayReplace
= seenReplace
;
2252 VdbeModuleComment((v
, "END: GenCnstCks(%d)", seenReplace
));
2255 #ifdef SQLITE_ENABLE_NULL_TRIM
2257 ** Change the P5 operand on the last opcode (which should be an OP_MakeRecord)
2258 ** to be the number of columns in table pTab that must not be NULL-trimmed.
2260 ** Or if no columns of pTab may be NULL-trimmed, leave P5 at zero.
2262 void sqlite3SetMakeRecordP5(Vdbe
*v
, Table
*pTab
){
2265 /* Records with omitted columns are only allowed for schema format
2266 ** version 2 and later (SQLite version 3.1.4, 2005-02-20). */
2267 if( pTab
->pSchema
->file_format
<2 ) return;
2269 for(i
=pTab
->nCol
-1; i
>0; i
--){
2270 if( pTab
->aCol
[i
].pDflt
!=0 ) break;
2271 if( pTab
->aCol
[i
].colFlags
& COLFLAG_PRIMKEY
) break;
2273 sqlite3VdbeChangeP5(v
, i
+1);
2278 ** This routine generates code to finish the INSERT or UPDATE operation
2279 ** that was started by a prior call to sqlite3GenerateConstraintChecks.
2280 ** A consecutive range of registers starting at regNewData contains the
2281 ** rowid and the content to be inserted.
2283 ** The arguments to this routine should be the same as the first six
2284 ** arguments to sqlite3GenerateConstraintChecks.
2286 void sqlite3CompleteInsertion(
2287 Parse
*pParse
, /* The parser context */
2288 Table
*pTab
, /* the table into which we are inserting */
2289 int iDataCur
, /* Cursor of the canonical data source */
2290 int iIdxCur
, /* First index cursor */
2291 int regNewData
, /* Range of content */
2292 int *aRegIdx
, /* Register used by each index. 0 for unused indices */
2293 int update_flags
, /* True for UPDATE, False for INSERT */
2294 int appendBias
, /* True if this is likely to be an append */
2295 int useSeekResult
/* True to set the USESEEKRESULT flag on OP_[Idx]Insert */
2297 Vdbe
*v
; /* Prepared statements under construction */
2298 Index
*pIdx
; /* An index being inserted or updated */
2299 u8 pik_flags
; /* flag values passed to the btree insert */
2300 int i
; /* Loop counter */
2302 assert( update_flags
==0
2303 || update_flags
==OPFLAG_ISUPDATE
2304 || update_flags
==(OPFLAG_ISUPDATE
|OPFLAG_SAVEPOSITION
)
2307 v
= sqlite3GetVdbe(pParse
);
2309 assert( pTab
->pSelect
==0 ); /* This table is not a VIEW */
2310 for(i
=0, pIdx
=pTab
->pIndex
; pIdx
; pIdx
=pIdx
->pNext
, i
++){
2311 /* All REPLACE indexes are at the end of the list */
2312 assert( pIdx
->onError
!=OE_Replace
2314 || pIdx
->pNext
->onError
==OE_Replace
);
2315 if( aRegIdx
[i
]==0 ) continue;
2316 if( pIdx
->pPartIdxWhere
){
2317 sqlite3VdbeAddOp2(v
, OP_IsNull
, aRegIdx
[i
], sqlite3VdbeCurrentAddr(v
)+2);
2320 pik_flags
= (useSeekResult
? OPFLAG_USESEEKRESULT
: 0);
2321 if( IsPrimaryKeyIndex(pIdx
) && !HasRowid(pTab
) ){
2322 assert( pParse
->nested
==0 );
2323 pik_flags
|= OPFLAG_NCHANGE
;
2324 pik_flags
|= (update_flags
& OPFLAG_SAVEPOSITION
);
2325 #ifdef SQLITE_ENABLE_PREUPDATE_HOOK
2326 if( update_flags
==0 ){
2327 int r
= sqlite3GetTempReg(pParse
);
2328 sqlite3VdbeAddOp2(v
, OP_Integer
, 0, r
);
2329 sqlite3VdbeAddOp4(v
, OP_Insert
,
2330 iIdxCur
+i
, aRegIdx
[i
], r
, (char*)pTab
, P4_TABLE
2332 sqlite3VdbeChangeP5(v
, OPFLAG_ISNOOP
);
2333 sqlite3ReleaseTempReg(pParse
, r
);
2337 sqlite3VdbeAddOp4Int(v
, OP_IdxInsert
, iIdxCur
+i
, aRegIdx
[i
],
2339 pIdx
->uniqNotNull
? pIdx
->nKeyCol
: pIdx
->nColumn
);
2340 sqlite3VdbeChangeP5(v
, pik_flags
);
2342 if( !HasRowid(pTab
) ) return;
2343 if( pParse
->nested
){
2346 pik_flags
= OPFLAG_NCHANGE
;
2347 pik_flags
|= (update_flags
?update_flags
:OPFLAG_LASTROWID
);
2350 pik_flags
|= OPFLAG_APPEND
;
2352 if( useSeekResult
){
2353 pik_flags
|= OPFLAG_USESEEKRESULT
;
2355 sqlite3VdbeAddOp3(v
, OP_Insert
, iDataCur
, aRegIdx
[i
], regNewData
);
2356 if( !pParse
->nested
){
2357 sqlite3VdbeAppendP4(v
, pTab
, P4_TABLE
);
2359 sqlite3VdbeChangeP5(v
, pik_flags
);
2363 ** Allocate cursors for the pTab table and all its indices and generate
2364 ** code to open and initialized those cursors.
2366 ** The cursor for the object that contains the complete data (normally
2367 ** the table itself, but the PRIMARY KEY index in the case of a WITHOUT
2368 ** ROWID table) is returned in *piDataCur. The first index cursor is
2369 ** returned in *piIdxCur. The number of indices is returned.
2371 ** Use iBase as the first cursor (either the *piDataCur for rowid tables
2372 ** or the first index for WITHOUT ROWID tables) if it is non-negative.
2373 ** If iBase is negative, then allocate the next available cursor.
2375 ** For a rowid table, *piDataCur will be exactly one less than *piIdxCur.
2376 ** For a WITHOUT ROWID table, *piDataCur will be somewhere in the range
2377 ** of *piIdxCurs, depending on where the PRIMARY KEY index appears on the
2378 ** pTab->pIndex list.
2380 ** If pTab is a virtual table, then this routine is a no-op and the
2381 ** *piDataCur and *piIdxCur values are left uninitialized.
2383 int sqlite3OpenTableAndIndices(
2384 Parse
*pParse
, /* Parsing context */
2385 Table
*pTab
, /* Table to be opened */
2386 int op
, /* OP_OpenRead or OP_OpenWrite */
2387 u8 p5
, /* P5 value for OP_Open* opcodes (except on WITHOUT ROWID) */
2388 int iBase
, /* Use this for the table cursor, if there is one */
2389 u8
*aToOpen
, /* If not NULL: boolean for each table and index */
2390 int *piDataCur
, /* Write the database source cursor number here */
2391 int *piIdxCur
/* Write the first index cursor number here */
2399 assert( op
==OP_OpenRead
|| op
==OP_OpenWrite
);
2400 assert( op
==OP_OpenWrite
|| p5
==0 );
2401 if( IsVirtual(pTab
) ){
2402 /* This routine is a no-op for virtual tables. Leave the output
2403 ** variables *piDataCur and *piIdxCur uninitialized so that valgrind
2404 ** can detect if they are used by mistake in the caller. */
2407 iDb
= sqlite3SchemaToIndex(pParse
->db
, pTab
->pSchema
);
2408 v
= sqlite3GetVdbe(pParse
);
2410 if( iBase
<0 ) iBase
= pParse
->nTab
;
2412 if( piDataCur
) *piDataCur
= iDataCur
;
2413 if( HasRowid(pTab
) && (aToOpen
==0 || aToOpen
[0]) ){
2414 sqlite3OpenTable(pParse
, iDataCur
, iDb
, pTab
, op
);
2416 sqlite3TableLock(pParse
, iDb
, pTab
->tnum
, op
==OP_OpenWrite
, pTab
->zName
);
2418 if( piIdxCur
) *piIdxCur
= iBase
;
2419 for(i
=0, pIdx
=pTab
->pIndex
; pIdx
; pIdx
=pIdx
->pNext
, i
++){
2420 int iIdxCur
= iBase
++;
2421 assert( pIdx
->pSchema
==pTab
->pSchema
);
2422 if( IsPrimaryKeyIndex(pIdx
) && !HasRowid(pTab
) ){
2423 if( piDataCur
) *piDataCur
= iIdxCur
;
2426 if( aToOpen
==0 || aToOpen
[i
+1] ){
2427 sqlite3VdbeAddOp3(v
, op
, iIdxCur
, pIdx
->tnum
, iDb
);
2428 sqlite3VdbeSetP4KeyInfo(pParse
, pIdx
);
2429 sqlite3VdbeChangeP5(v
, p5
);
2430 VdbeComment((v
, "%s", pIdx
->zName
));
2433 if( iBase
>pParse
->nTab
) pParse
->nTab
= iBase
;
2440 ** The following global variable is incremented whenever the
2441 ** transfer optimization is used. This is used for testing
2442 ** purposes only - to make sure the transfer optimization really
2443 ** is happening when it is supposed to.
2445 int sqlite3_xferopt_count
;
2446 #endif /* SQLITE_TEST */
2449 #ifndef SQLITE_OMIT_XFER_OPT
2451 ** Check to see if index pSrc is compatible as a source of data
2452 ** for index pDest in an insert transfer optimization. The rules
2453 ** for a compatible index:
2455 ** * The index is over the same set of columns
2456 ** * The same DESC and ASC markings occurs on all columns
2457 ** * The same onError processing (OE_Abort, OE_Ignore, etc)
2458 ** * The same collating sequence on each column
2459 ** * The index has the exact same WHERE clause
2461 static int xferCompatibleIndex(Index
*pDest
, Index
*pSrc
){
2463 assert( pDest
&& pSrc
);
2464 assert( pDest
->pTable
!=pSrc
->pTable
);
2465 if( pDest
->nKeyCol
!=pSrc
->nKeyCol
|| pDest
->nColumn
!=pSrc
->nColumn
){
2466 return 0; /* Different number of columns */
2468 if( pDest
->onError
!=pSrc
->onError
){
2469 return 0; /* Different conflict resolution strategies */
2471 for(i
=0; i
<pSrc
->nKeyCol
; i
++){
2472 if( pSrc
->aiColumn
[i
]!=pDest
->aiColumn
[i
] ){
2473 return 0; /* Different columns indexed */
2475 if( pSrc
->aiColumn
[i
]==XN_EXPR
){
2476 assert( pSrc
->aColExpr
!=0 && pDest
->aColExpr
!=0 );
2477 if( sqlite3ExprCompare(0, pSrc
->aColExpr
->a
[i
].pExpr
,
2478 pDest
->aColExpr
->a
[i
].pExpr
, -1)!=0 ){
2479 return 0; /* Different expressions in the index */
2482 if( pSrc
->aSortOrder
[i
]!=pDest
->aSortOrder
[i
] ){
2483 return 0; /* Different sort orders */
2485 if( sqlite3_stricmp(pSrc
->azColl
[i
],pDest
->azColl
[i
])!=0 ){
2486 return 0; /* Different collating sequences */
2489 if( sqlite3ExprCompare(0, pSrc
->pPartIdxWhere
, pDest
->pPartIdxWhere
, -1) ){
2490 return 0; /* Different WHERE clauses */
2493 /* If no test above fails then the indices must be compatible */
2498 ** Attempt the transfer optimization on INSERTs of the form
2500 ** INSERT INTO tab1 SELECT * FROM tab2;
2502 ** The xfer optimization transfers raw records from tab2 over to tab1.
2503 ** Columns are not decoded and reassembled, which greatly improves
2504 ** performance. Raw index records are transferred in the same way.
2506 ** The xfer optimization is only attempted if tab1 and tab2 are compatible.
2507 ** There are lots of rules for determining compatibility - see comments
2508 ** embedded in the code for details.
2510 ** This routine returns TRUE if the optimization is guaranteed to be used.
2511 ** Sometimes the xfer optimization will only work if the destination table
2512 ** is empty - a factor that can only be determined at run-time. In that
2513 ** case, this routine generates code for the xfer optimization but also
2514 ** does a test to see if the destination table is empty and jumps over the
2515 ** xfer optimization code if the test fails. In that case, this routine
2516 ** returns FALSE so that the caller will know to go ahead and generate
2517 ** an unoptimized transfer. This routine also returns FALSE if there
2518 ** is no chance that the xfer optimization can be applied.
2520 ** This optimization is particularly useful at making VACUUM run faster.
2522 static int xferOptimization(
2523 Parse
*pParse
, /* Parser context */
2524 Table
*pDest
, /* The table we are inserting into */
2525 Select
*pSelect
, /* A SELECT statement to use as the data source */
2526 int onError
, /* How to handle constraint errors */
2527 int iDbDest
/* The database of pDest */
2529 sqlite3
*db
= pParse
->db
;
2530 ExprList
*pEList
; /* The result set of the SELECT */
2531 Table
*pSrc
; /* The table in the FROM clause of SELECT */
2532 Index
*pSrcIdx
, *pDestIdx
; /* Source and destination indices */
2533 struct SrcList_item
*pItem
; /* An element of pSelect->pSrc */
2534 int i
; /* Loop counter */
2535 int iDbSrc
; /* The database of pSrc */
2536 int iSrc
, iDest
; /* Cursors from source and destination */
2537 int addr1
, addr2
; /* Loop addresses */
2538 int emptyDestTest
= 0; /* Address of test for empty pDest */
2539 int emptySrcTest
= 0; /* Address of test for empty pSrc */
2540 Vdbe
*v
; /* The VDBE we are building */
2541 int regAutoinc
; /* Memory register used by AUTOINC */
2542 int destHasUniqueIdx
= 0; /* True if pDest has a UNIQUE index */
2543 int regData
, regRowid
; /* Registers holding data and rowid */
2546 return 0; /* Must be of the form INSERT INTO ... SELECT ... */
2548 if( pParse
->pWith
|| pSelect
->pWith
){
2549 /* Do not attempt to process this query if there are an WITH clauses
2550 ** attached to it. Proceeding may generate a false "no such table: xxx"
2551 ** error if pSelect reads from a CTE named "xxx". */
2554 if( sqlite3TriggerList(pParse
, pDest
) ){
2555 return 0; /* tab1 must not have triggers */
2557 #ifndef SQLITE_OMIT_VIRTUALTABLE
2558 if( IsVirtual(pDest
) ){
2559 return 0; /* tab1 must not be a virtual table */
2562 if( onError
==OE_Default
){
2563 if( pDest
->iPKey
>=0 ) onError
= pDest
->keyConf
;
2564 if( onError
==OE_Default
) onError
= OE_Abort
;
2566 assert(pSelect
->pSrc
); /* allocated even if there is no FROM clause */
2567 if( pSelect
->pSrc
->nSrc
!=1 ){
2568 return 0; /* FROM clause must have exactly one term */
2570 if( pSelect
->pSrc
->a
[0].pSelect
){
2571 return 0; /* FROM clause cannot contain a subquery */
2573 if( pSelect
->pWhere
){
2574 return 0; /* SELECT may not have a WHERE clause */
2576 if( pSelect
->pOrderBy
){
2577 return 0; /* SELECT may not have an ORDER BY clause */
2579 /* Do not need to test for a HAVING clause. If HAVING is present but
2580 ** there is no ORDER BY, we will get an error. */
2581 if( pSelect
->pGroupBy
){
2582 return 0; /* SELECT may not have a GROUP BY clause */
2584 if( pSelect
->pLimit
){
2585 return 0; /* SELECT may not have a LIMIT clause */
2587 if( pSelect
->pPrior
){
2588 return 0; /* SELECT may not be a compound query */
2590 if( pSelect
->selFlags
& SF_Distinct
){
2591 return 0; /* SELECT may not be DISTINCT */
2593 pEList
= pSelect
->pEList
;
2594 assert( pEList
!=0 );
2595 if( pEList
->nExpr
!=1 ){
2596 return 0; /* The result set must have exactly one column */
2598 assert( pEList
->a
[0].pExpr
);
2599 if( pEList
->a
[0].pExpr
->op
!=TK_ASTERISK
){
2600 return 0; /* The result set must be the special operator "*" */
2603 /* At this point we have established that the statement is of the
2604 ** correct syntactic form to participate in this optimization. Now
2605 ** we have to check the semantics.
2607 pItem
= pSelect
->pSrc
->a
;
2608 pSrc
= sqlite3LocateTableItem(pParse
, 0, pItem
);
2610 return 0; /* FROM clause does not contain a real table */
2612 if( pSrc
->tnum
==pDest
->tnum
&& pSrc
->pSchema
==pDest
->pSchema
){
2613 testcase( pSrc
!=pDest
); /* Possible due to bad sqlite_schema.rootpage */
2614 return 0; /* tab1 and tab2 may not be the same table */
2616 if( HasRowid(pDest
)!=HasRowid(pSrc
) ){
2617 return 0; /* source and destination must both be WITHOUT ROWID or not */
2619 #ifndef SQLITE_OMIT_VIRTUALTABLE
2620 if( IsVirtual(pSrc
) ){
2621 return 0; /* tab2 must not be a virtual table */
2624 if( pSrc
->pSelect
){
2625 return 0; /* tab2 may not be a view */
2627 if( pDest
->nCol
!=pSrc
->nCol
){
2628 return 0; /* Number of columns must be the same in tab1 and tab2 */
2630 if( pDest
->iPKey
!=pSrc
->iPKey
){
2631 return 0; /* Both tables must have the same INTEGER PRIMARY KEY */
2633 for(i
=0; i
<pDest
->nCol
; i
++){
2634 Column
*pDestCol
= &pDest
->aCol
[i
];
2635 Column
*pSrcCol
= &pSrc
->aCol
[i
];
2636 #ifdef SQLITE_ENABLE_HIDDEN_COLUMNS
2637 if( (db
->mDbFlags
& DBFLAG_Vacuum
)==0
2638 && (pDestCol
->colFlags
| pSrcCol
->colFlags
) & COLFLAG_HIDDEN
2640 return 0; /* Neither table may have __hidden__ columns */
2643 #ifndef SQLITE_OMIT_GENERATED_COLUMNS
2644 /* Even if tables t1 and t2 have identical schemas, if they contain
2645 ** generated columns, then this statement is semantically incorrect:
2647 ** INSERT INTO t2 SELECT * FROM t1;
2649 ** The reason is that generated column values are returned by the
2650 ** the SELECT statement on the right but the INSERT statement on the
2651 ** left wants them to be omitted.
2653 ** Nevertheless, this is a useful notational shorthand to tell SQLite
2654 ** to do a bulk transfer all of the content from t1 over to t2.
2656 ** We could, in theory, disable this (except for internal use by the
2657 ** VACUUM command where it is actually needed). But why do that? It
2658 ** seems harmless enough, and provides a useful service.
2660 if( (pDestCol
->colFlags
& COLFLAG_GENERATED
) !=
2661 (pSrcCol
->colFlags
& COLFLAG_GENERATED
) ){
2662 return 0; /* Both columns have the same generated-column type */
2664 /* But the transfer is only allowed if both the source and destination
2665 ** tables have the exact same expressions for generated columns.
2666 ** This requirement could be relaxed for VIRTUAL columns, I suppose.
2668 if( (pDestCol
->colFlags
& COLFLAG_GENERATED
)!=0 ){
2669 if( sqlite3ExprCompare(0, pSrcCol
->pDflt
, pDestCol
->pDflt
, -1)!=0 ){
2670 testcase( pDestCol
->colFlags
& COLFLAG_VIRTUAL
);
2671 testcase( pDestCol
->colFlags
& COLFLAG_STORED
);
2672 return 0; /* Different generator expressions */
2676 if( pDestCol
->affinity
!=pSrcCol
->affinity
){
2677 return 0; /* Affinity must be the same on all columns */
2679 if( sqlite3_stricmp(pDestCol
->zColl
, pSrcCol
->zColl
)!=0 ){
2680 return 0; /* Collating sequence must be the same on all columns */
2682 if( pDestCol
->notNull
&& !pSrcCol
->notNull
){
2683 return 0; /* tab2 must be NOT NULL if tab1 is */
2685 /* Default values for second and subsequent columns need to match. */
2686 if( (pDestCol
->colFlags
& COLFLAG_GENERATED
)==0 && i
>0 ){
2687 assert( pDestCol
->pDflt
==0 || pDestCol
->pDflt
->op
==TK_SPAN
);
2688 assert( pSrcCol
->pDflt
==0 || pSrcCol
->pDflt
->op
==TK_SPAN
);
2689 if( (pDestCol
->pDflt
==0)!=(pSrcCol
->pDflt
==0)
2690 || (pDestCol
->pDflt
&& strcmp(pDestCol
->pDflt
->u
.zToken
,
2691 pSrcCol
->pDflt
->u
.zToken
)!=0)
2693 return 0; /* Default values must be the same for all columns */
2697 for(pDestIdx
=pDest
->pIndex
; pDestIdx
; pDestIdx
=pDestIdx
->pNext
){
2698 if( IsUniqueIndex(pDestIdx
) ){
2699 destHasUniqueIdx
= 1;
2701 for(pSrcIdx
=pSrc
->pIndex
; pSrcIdx
; pSrcIdx
=pSrcIdx
->pNext
){
2702 if( xferCompatibleIndex(pDestIdx
, pSrcIdx
) ) break;
2705 return 0; /* pDestIdx has no corresponding index in pSrc */
2707 if( pSrcIdx
->tnum
==pDestIdx
->tnum
&& pSrc
->pSchema
==pDest
->pSchema
2708 && sqlite3FaultSim(411)==SQLITE_OK
){
2709 /* The sqlite3FaultSim() call allows this corruption test to be
2710 ** bypassed during testing, in order to exercise other corruption tests
2711 ** further downstream. */
2712 return 0; /* Corrupt schema - two indexes on the same btree */
2715 #ifndef SQLITE_OMIT_CHECK
2716 if( pDest
->pCheck
&& sqlite3ExprListCompare(pSrc
->pCheck
,pDest
->pCheck
,-1) ){
2717 return 0; /* Tables have different CHECK constraints. Ticket #2252 */
2720 #ifndef SQLITE_OMIT_FOREIGN_KEY
2721 /* Disallow the transfer optimization if the destination table constains
2722 ** any foreign key constraints. This is more restrictive than necessary.
2723 ** But the main beneficiary of the transfer optimization is the VACUUM
2724 ** command, and the VACUUM command disables foreign key constraints. So
2725 ** the extra complication to make this rule less restrictive is probably
2726 ** not worth the effort. Ticket [6284df89debdfa61db8073e062908af0c9b6118e]
2728 if( (db
->flags
& SQLITE_ForeignKeys
)!=0 && pDest
->pFKey
!=0 ){
2732 if( (db
->flags
& SQLITE_CountRows
)!=0 ){
2733 return 0; /* xfer opt does not play well with PRAGMA count_changes */
2736 /* If we get this far, it means that the xfer optimization is at
2737 ** least a possibility, though it might only work if the destination
2738 ** table (tab1) is initially empty.
2741 sqlite3_xferopt_count
++;
2743 iDbSrc
= sqlite3SchemaToIndex(db
, pSrc
->pSchema
);
2744 v
= sqlite3GetVdbe(pParse
);
2745 sqlite3CodeVerifySchema(pParse
, iDbSrc
);
2746 iSrc
= pParse
->nTab
++;
2747 iDest
= pParse
->nTab
++;
2748 regAutoinc
= autoIncBegin(pParse
, iDbDest
, pDest
);
2749 regData
= sqlite3GetTempReg(pParse
);
2750 regRowid
= sqlite3GetTempReg(pParse
);
2751 sqlite3OpenTable(pParse
, iDest
, iDbDest
, pDest
, OP_OpenWrite
);
2752 assert( HasRowid(pDest
) || destHasUniqueIdx
);
2753 if( (db
->mDbFlags
& DBFLAG_Vacuum
)==0 && (
2754 (pDest
->iPKey
<0 && pDest
->pIndex
!=0) /* (1) */
2755 || destHasUniqueIdx
/* (2) */
2756 || (onError
!=OE_Abort
&& onError
!=OE_Rollback
) /* (3) */
2758 /* In some circumstances, we are able to run the xfer optimization
2759 ** only if the destination table is initially empty. Unless the
2760 ** DBFLAG_Vacuum flag is set, this block generates code to make
2761 ** that determination. If DBFLAG_Vacuum is set, then the destination
2762 ** table is always empty.
2764 ** Conditions under which the destination must be empty:
2766 ** (1) There is no INTEGER PRIMARY KEY but there are indices.
2767 ** (If the destination is not initially empty, the rowid fields
2768 ** of index entries might need to change.)
2770 ** (2) The destination has a unique index. (The xfer optimization
2771 ** is unable to test uniqueness.)
2773 ** (3) onError is something other than OE_Abort and OE_Rollback.
2775 addr1
= sqlite3VdbeAddOp2(v
, OP_Rewind
, iDest
, 0); VdbeCoverage(v
);
2776 emptyDestTest
= sqlite3VdbeAddOp0(v
, OP_Goto
);
2777 sqlite3VdbeJumpHere(v
, addr1
);
2779 if( HasRowid(pSrc
) ){
2781 sqlite3OpenTable(pParse
, iSrc
, iDbSrc
, pSrc
, OP_OpenRead
);
2782 emptySrcTest
= sqlite3VdbeAddOp2(v
, OP_Rewind
, iSrc
, 0); VdbeCoverage(v
);
2783 if( pDest
->iPKey
>=0 ){
2784 addr1
= sqlite3VdbeAddOp2(v
, OP_Rowid
, iSrc
, regRowid
);
2785 sqlite3VdbeVerifyAbortable(v
, onError
);
2786 addr2
= sqlite3VdbeAddOp3(v
, OP_NotExists
, iDest
, 0, regRowid
);
2788 sqlite3RowidConstraint(pParse
, onError
, pDest
);
2789 sqlite3VdbeJumpHere(v
, addr2
);
2790 autoIncStep(pParse
, regAutoinc
, regRowid
);
2791 }else if( pDest
->pIndex
==0 && !(db
->mDbFlags
& DBFLAG_VacuumInto
) ){
2792 addr1
= sqlite3VdbeAddOp2(v
, OP_NewRowid
, iDest
, regRowid
);
2794 addr1
= sqlite3VdbeAddOp2(v
, OP_Rowid
, iSrc
, regRowid
);
2795 assert( (pDest
->tabFlags
& TF_Autoincrement
)==0 );
2797 if( db
->mDbFlags
& DBFLAG_Vacuum
){
2798 sqlite3VdbeAddOp1(v
, OP_SeekEnd
, iDest
);
2799 insFlags
= OPFLAG_APPEND
|OPFLAG_USESEEKRESULT
;
2801 insFlags
= OPFLAG_NCHANGE
|OPFLAG_LASTROWID
|OPFLAG_APPEND
;
2803 sqlite3VdbeAddOp3(v
, OP_RowData
, iSrc
, regData
, 1);
2804 sqlite3VdbeAddOp4(v
, OP_Insert
, iDest
, regData
, regRowid
,
2805 (char*)pDest
, P4_TABLE
);
2806 sqlite3VdbeChangeP5(v
, insFlags
);
2807 sqlite3VdbeAddOp2(v
, OP_Next
, iSrc
, addr1
); VdbeCoverage(v
);
2808 sqlite3VdbeAddOp2(v
, OP_Close
, iSrc
, 0);
2809 sqlite3VdbeAddOp2(v
, OP_Close
, iDest
, 0);
2811 sqlite3TableLock(pParse
, iDbDest
, pDest
->tnum
, 1, pDest
->zName
);
2812 sqlite3TableLock(pParse
, iDbSrc
, pSrc
->tnum
, 0, pSrc
->zName
);
2814 for(pDestIdx
=pDest
->pIndex
; pDestIdx
; pDestIdx
=pDestIdx
->pNext
){
2816 for(pSrcIdx
=pSrc
->pIndex
; ALWAYS(pSrcIdx
); pSrcIdx
=pSrcIdx
->pNext
){
2817 if( xferCompatibleIndex(pDestIdx
, pSrcIdx
) ) break;
2820 sqlite3VdbeAddOp3(v
, OP_OpenRead
, iSrc
, pSrcIdx
->tnum
, iDbSrc
);
2821 sqlite3VdbeSetP4KeyInfo(pParse
, pSrcIdx
);
2822 VdbeComment((v
, "%s", pSrcIdx
->zName
));
2823 sqlite3VdbeAddOp3(v
, OP_OpenWrite
, iDest
, pDestIdx
->tnum
, iDbDest
);
2824 sqlite3VdbeSetP4KeyInfo(pParse
, pDestIdx
);
2825 sqlite3VdbeChangeP5(v
, OPFLAG_BULKCSR
);
2826 VdbeComment((v
, "%s", pDestIdx
->zName
));
2827 addr1
= sqlite3VdbeAddOp2(v
, OP_Rewind
, iSrc
, 0); VdbeCoverage(v
);
2828 if( db
->mDbFlags
& DBFLAG_Vacuum
){
2829 /* This INSERT command is part of a VACUUM operation, which guarantees
2830 ** that the destination table is empty. If all indexed columns use
2831 ** collation sequence BINARY, then it can also be assumed that the
2832 ** index will be populated by inserting keys in strictly sorted
2833 ** order. In this case, instead of seeking within the b-tree as part
2834 ** of every OP_IdxInsert opcode, an OP_SeekEnd is added before the
2835 ** OP_IdxInsert to seek to the point within the b-tree where each key
2836 ** should be inserted. This is faster.
2838 ** If any of the indexed columns use a collation sequence other than
2839 ** BINARY, this optimization is disabled. This is because the user
2840 ** might change the definition of a collation sequence and then run
2841 ** a VACUUM command. In that case keys may not be written in strictly
2843 for(i
=0; i
<pSrcIdx
->nColumn
; i
++){
2844 const char *zColl
= pSrcIdx
->azColl
[i
];
2845 if( sqlite3_stricmp(sqlite3StrBINARY
, zColl
) ) break;
2847 if( i
==pSrcIdx
->nColumn
){
2848 idxInsFlags
= OPFLAG_USESEEKRESULT
;
2849 sqlite3VdbeAddOp1(v
, OP_SeekEnd
, iDest
);
2851 }else if( !HasRowid(pSrc
) && pDestIdx
->idxType
==SQLITE_IDXTYPE_PRIMARYKEY
){
2852 idxInsFlags
|= OPFLAG_NCHANGE
;
2854 sqlite3VdbeAddOp3(v
, OP_RowData
, iSrc
, regData
, 1);
2855 sqlite3VdbeAddOp2(v
, OP_IdxInsert
, iDest
, regData
);
2856 sqlite3VdbeChangeP5(v
, idxInsFlags
|OPFLAG_APPEND
);
2857 sqlite3VdbeAddOp2(v
, OP_Next
, iSrc
, addr1
+1); VdbeCoverage(v
);
2858 sqlite3VdbeJumpHere(v
, addr1
);
2859 sqlite3VdbeAddOp2(v
, OP_Close
, iSrc
, 0);
2860 sqlite3VdbeAddOp2(v
, OP_Close
, iDest
, 0);
2862 if( emptySrcTest
) sqlite3VdbeJumpHere(v
, emptySrcTest
);
2863 sqlite3ReleaseTempReg(pParse
, regRowid
);
2864 sqlite3ReleaseTempReg(pParse
, regData
);
2865 if( emptyDestTest
){
2866 sqlite3AutoincrementEnd(pParse
);
2867 sqlite3VdbeAddOp2(v
, OP_Halt
, SQLITE_OK
, 0);
2868 sqlite3VdbeJumpHere(v
, emptyDestTest
);
2869 sqlite3VdbeAddOp2(v
, OP_Close
, iDest
, 0);
2875 #endif /* SQLITE_OMIT_XFER_OPT */