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 assert( pParse
->pVdbe
!=0 );
37 assert( opcode
==OP_OpenWrite
|| opcode
==OP_OpenRead
);
38 sqlite3TableLock(pParse
, iDb
, pTab
->tnum
,
39 (opcode
==OP_OpenWrite
)?1:0, pTab
->zName
);
41 sqlite3VdbeAddOp4Int(v
, opcode
, iCur
, pTab
->tnum
, iDb
, pTab
->nNVCol
);
42 VdbeComment((v
, "%s", pTab
->zName
));
44 Index
*pPk
= sqlite3PrimaryKeyIndex(pTab
);
46 assert( pPk
->tnum
==pTab
->tnum
|| CORRUPT_DB
);
47 sqlite3VdbeAddOp3(v
, opcode
, iCur
, pPk
->tnum
, iDb
);
48 sqlite3VdbeSetP4KeyInfo(pParse
, pPk
);
49 VdbeComment((v
, "%s", pTab
->zName
));
54 ** Return a pointer to the column affinity string associated with index
55 ** pIdx. A column affinity string has one character for each column in
56 ** the table, according to the affinity of the column:
58 ** Character Column affinity
59 ** ------------------------------
66 ** An extra 'D' is appended to the end of the string to cover the
67 ** rowid that appears as the last column in every index.
69 ** Memory for the buffer containing the column index affinity string
70 ** is managed along with the rest of the Index structure. It will be
71 ** released when sqlite3DeleteIndex() is called.
73 const char *sqlite3IndexAffinityStr(sqlite3
*db
, Index
*pIdx
){
75 /* The first time a column affinity string for a particular index is
76 ** required, it is allocated and populated here. It is then stored as
77 ** a member of the Index structure for subsequent use.
79 ** The column affinity string will eventually be deleted by
80 ** sqliteDeleteIndex() when the Index structure itself is cleaned
84 Table
*pTab
= pIdx
->pTable
;
85 pIdx
->zColAff
= (char *)sqlite3DbMallocRaw(0, pIdx
->nColumn
+1);
90 for(n
=0; n
<pIdx
->nColumn
; n
++){
91 i16 x
= pIdx
->aiColumn
[n
];
94 aff
= pTab
->aCol
[x
].affinity
;
95 }else if( x
==XN_ROWID
){
96 aff
= SQLITE_AFF_INTEGER
;
99 assert( pIdx
->bHasExpr
);
100 assert( pIdx
->aColExpr
!=0 );
101 aff
= sqlite3ExprAffinity(pIdx
->aColExpr
->a
[n
].pExpr
);
103 if( aff
<SQLITE_AFF_BLOB
) aff
= SQLITE_AFF_BLOB
;
104 if( aff
>SQLITE_AFF_NUMERIC
) aff
= SQLITE_AFF_NUMERIC
;
105 pIdx
->zColAff
[n
] = aff
;
107 pIdx
->zColAff
[n
] = 0;
110 return pIdx
->zColAff
;
114 ** Compute an affinity string for a table. Space is obtained
115 ** from sqlite3DbMalloc(). The caller is responsible for freeing
116 ** the space when done.
118 char *sqlite3TableAffinityStr(sqlite3
*db
, const Table
*pTab
){
120 zColAff
= (char *)sqlite3DbMallocRaw(db
, pTab
->nCol
+1);
123 for(i
=j
=0; i
<pTab
->nCol
; i
++){
124 if( (pTab
->aCol
[i
].colFlags
& COLFLAG_VIRTUAL
)==0 ){
125 zColAff
[j
++] = pTab
->aCol
[i
].affinity
;
130 }while( j
>=0 && zColAff
[j
]<=SQLITE_AFF_BLOB
);
136 ** Make changes to the evolving bytecode to do affinity transformations
137 ** of values that are about to be gathered into a row for table pTab.
139 ** For ordinary (legacy, non-strict) tables:
140 ** -----------------------------------------
142 ** Compute the affinity string for table pTab, if it has not already been
143 ** computed. As an optimization, omit trailing SQLITE_AFF_BLOB affinities.
145 ** If the affinity string is empty (because it was all SQLITE_AFF_BLOB entries
146 ** which were then optimized out) then this routine becomes a no-op.
148 ** Otherwise if iReg>0 then code an OP_Affinity opcode that will set the
149 ** affinities for register iReg and following. Or if iReg==0,
150 ** then just set the P4 operand of the previous opcode (which should be
151 ** an OP_MakeRecord) to the affinity string.
153 ** A column affinity string has one character per column:
155 ** Character Column affinity
156 ** --------- ---------------
163 ** For STRICT tables:
164 ** ------------------
166 ** Generate an appropropriate OP_TypeCheck opcode that will verify the
167 ** datatypes against the column definitions in pTab. If iReg==0, that
168 ** means an OP_MakeRecord opcode has already been generated and should be
169 ** the last opcode generated. The new OP_TypeCheck needs to be inserted
170 ** before the OP_MakeRecord. The new OP_TypeCheck should use the same
171 ** register set as the OP_MakeRecord. If iReg>0 then register iReg is
172 ** the first of a series of registers that will form the new record.
173 ** Apply the type checking to that array of registers.
175 void sqlite3TableAffinity(Vdbe
*v
, Table
*pTab
, int iReg
){
178 if( pTab
->tabFlags
& TF_Strict
){
180 /* Move the previous opcode (which should be OP_MakeRecord) forward
181 ** by one slot and insert a new OP_TypeCheck where the current
182 ** OP_MakeRecord is found */
184 sqlite3VdbeAppendP4(v
, pTab
, P4_TABLE
);
185 pPrev
= sqlite3VdbeGetLastOp(v
);
187 assert( pPrev
->opcode
==OP_MakeRecord
|| sqlite3VdbeDb(v
)->mallocFailed
);
188 pPrev
->opcode
= OP_TypeCheck
;
189 sqlite3VdbeAddOp3(v
, OP_MakeRecord
, pPrev
->p1
, pPrev
->p2
, pPrev
->p3
);
191 /* Insert an isolated OP_Typecheck */
192 sqlite3VdbeAddOp2(v
, OP_TypeCheck
, iReg
, pTab
->nNVCol
);
193 sqlite3VdbeAppendP4(v
, pTab
, P4_TABLE
);
197 zColAff
= pTab
->zColAff
;
199 zColAff
= sqlite3TableAffinityStr(0, pTab
);
201 sqlite3OomFault(sqlite3VdbeDb(v
));
204 pTab
->zColAff
= zColAff
;
206 assert( zColAff
!=0 );
207 i
= sqlite3Strlen30NN(zColAff
);
210 sqlite3VdbeAddOp4(v
, OP_Affinity
, iReg
, i
, 0, zColAff
, i
);
212 assert( sqlite3VdbeGetLastOp(v
)->opcode
==OP_MakeRecord
213 || sqlite3VdbeDb(v
)->mallocFailed
);
214 sqlite3VdbeChangeP4(v
, -1, zColAff
, i
);
220 ** Return non-zero if the table pTab in database iDb or any of its indices
221 ** have been opened at any point in the VDBE program. This is used to see if
222 ** a statement of the form "INSERT INTO <iDb, pTab> SELECT ..." can
223 ** run without using a temporary table for the results of the SELECT.
225 static int readsTable(Parse
*p
, int iDb
, Table
*pTab
){
226 Vdbe
*v
= sqlite3GetVdbe(p
);
228 int iEnd
= sqlite3VdbeCurrentAddr(v
);
229 #ifndef SQLITE_OMIT_VIRTUALTABLE
230 VTable
*pVTab
= IsVirtual(pTab
) ? sqlite3GetVTable(p
->db
, pTab
) : 0;
233 for(i
=1; i
<iEnd
; i
++){
234 VdbeOp
*pOp
= sqlite3VdbeGetOp(v
, i
);
236 if( pOp
->opcode
==OP_OpenRead
&& pOp
->p3
==iDb
){
239 if( tnum
==pTab
->tnum
){
242 for(pIndex
=pTab
->pIndex
; pIndex
; pIndex
=pIndex
->pNext
){
243 if( tnum
==pIndex
->tnum
){
248 #ifndef SQLITE_OMIT_VIRTUALTABLE
249 if( pOp
->opcode
==OP_VOpen
&& pOp
->p4
.pVtab
==pVTab
){
250 assert( pOp
->p4
.pVtab
!=0 );
251 assert( pOp
->p4type
==P4_VTAB
);
259 /* This walker callback will compute the union of colFlags flags for all
260 ** referenced columns in a CHECK constraint or generated column expression.
262 static int exprColumnFlagUnion(Walker
*pWalker
, Expr
*pExpr
){
263 if( pExpr
->op
==TK_COLUMN
&& pExpr
->iColumn
>=0 ){
264 assert( pExpr
->iColumn
< pWalker
->u
.pTab
->nCol
);
265 pWalker
->eCode
|= pWalker
->u
.pTab
->aCol
[pExpr
->iColumn
].colFlags
;
270 #ifndef SQLITE_OMIT_GENERATED_COLUMNS
272 ** All regular columns for table pTab have been puts into registers
273 ** starting with iRegStore. The registers that correspond to STORED
274 ** or VIRTUAL columns have not yet been initialized. This routine goes
275 ** back and computes the values for those columns based on the previously
276 ** computed normal columns.
278 void sqlite3ComputeGeneratedColumns(
279 Parse
*pParse
, /* Parsing context */
280 int iRegStore
, /* Register holding the first column */
281 Table
*pTab
/* The table */
289 assert( pTab
->tabFlags
& TF_HasGenerated
);
290 testcase( pTab
->tabFlags
& TF_HasVirtual
);
291 testcase( pTab
->tabFlags
& TF_HasStored
);
293 /* Before computing generated columns, first go through and make sure
294 ** that appropriate affinity has been applied to the regular columns
296 sqlite3TableAffinity(pParse
->pVdbe
, pTab
, iRegStore
);
297 if( (pTab
->tabFlags
& TF_HasStored
)!=0 ){
298 pOp
= sqlite3VdbeGetLastOp(pParse
->pVdbe
);
299 if( pOp
->opcode
==OP_Affinity
){
300 /* Change the OP_Affinity argument to '@' (NONE) for all stored
301 ** columns. '@' is the no-op affinity and those columns have not
302 ** yet been computed. */
304 char *zP4
= pOp
->p4
.z
;
306 assert( pOp
->p4type
==P4_DYNAMIC
);
307 for(ii
=jj
=0; zP4
[jj
]; ii
++){
308 if( pTab
->aCol
[ii
].colFlags
& COLFLAG_VIRTUAL
){
311 if( pTab
->aCol
[ii
].colFlags
& COLFLAG_STORED
){
312 zP4
[jj
] = SQLITE_AFF_NONE
;
316 }else if( pOp
->opcode
==OP_TypeCheck
){
317 /* If an OP_TypeCheck was generated because the table is STRICT,
318 ** then set the P3 operand to indicate that generated columns should
324 /* Because there can be multiple generated columns that refer to one another,
325 ** this is a two-pass algorithm. On the first pass, mark all generated
326 ** columns as "not available".
328 for(i
=0; i
<pTab
->nCol
; i
++){
329 if( pTab
->aCol
[i
].colFlags
& COLFLAG_GENERATED
){
330 testcase( pTab
->aCol
[i
].colFlags
& COLFLAG_VIRTUAL
);
331 testcase( pTab
->aCol
[i
].colFlags
& COLFLAG_STORED
);
332 pTab
->aCol
[i
].colFlags
|= COLFLAG_NOTAVAIL
;
337 w
.xExprCallback
= exprColumnFlagUnion
;
338 w
.xSelectCallback
= 0;
339 w
.xSelectCallback2
= 0;
341 /* On the second pass, compute the value of each NOT-AVAILABLE column.
342 ** Companion code in the TK_COLUMN case of sqlite3ExprCodeTarget() will
343 ** compute dependencies and mark remove the COLSPAN_NOTAVAIL mark, as
346 pParse
->iSelfTab
= -iRegStore
;
350 for(i
=0; i
<pTab
->nCol
; i
++){
351 Column
*pCol
= pTab
->aCol
+ i
;
352 if( (pCol
->colFlags
& COLFLAG_NOTAVAIL
)!=0 ){
354 pCol
->colFlags
|= COLFLAG_BUSY
;
356 sqlite3WalkExpr(&w
, sqlite3ColumnExpr(pTab
, pCol
));
357 pCol
->colFlags
&= ~COLFLAG_BUSY
;
358 if( w
.eCode
& COLFLAG_NOTAVAIL
){
363 assert( pCol
->colFlags
& COLFLAG_GENERATED
);
364 x
= sqlite3TableColumnToStorage(pTab
, i
) + iRegStore
;
365 sqlite3ExprCodeGeneratedColumn(pParse
, pTab
, pCol
, x
);
366 pCol
->colFlags
&= ~COLFLAG_NOTAVAIL
;
369 }while( pRedo
&& eProgress
);
371 sqlite3ErrorMsg(pParse
, "generated column loop on \"%s\"", pRedo
->zCnName
);
373 pParse
->iSelfTab
= 0;
375 #endif /* SQLITE_OMIT_GENERATED_COLUMNS */
378 #ifndef SQLITE_OMIT_AUTOINCREMENT
380 ** Locate or create an AutoincInfo structure associated with table pTab
381 ** which is in database iDb. Return the register number for the register
382 ** that holds the maximum rowid. Return zero if pTab is not an AUTOINCREMENT
383 ** table. (Also return zero when doing a VACUUM since we do not want to
384 ** update the AUTOINCREMENT counters during a VACUUM.)
386 ** There is at most one AutoincInfo structure per table even if the
387 ** same table is autoincremented multiple times due to inserts within
388 ** triggers. A new AutoincInfo structure is created if this is the
389 ** first use of table pTab. On 2nd and subsequent uses, the original
390 ** AutoincInfo structure is used.
392 ** Four consecutive registers are allocated:
394 ** (1) The name of the pTab table.
395 ** (2) The maximum ROWID of pTab.
396 ** (3) The rowid in sqlite_sequence of pTab
397 ** (4) The original value of the max ROWID in pTab, or NULL if none
399 ** The 2nd register is the one that is returned. That is all the
400 ** insert routine needs to know about.
402 static int autoIncBegin(
403 Parse
*pParse
, /* Parsing context */
404 int iDb
, /* Index of the database holding pTab */
405 Table
*pTab
/* The table we are writing to */
407 int memId
= 0; /* Register holding maximum rowid */
408 assert( pParse
->db
->aDb
[iDb
].pSchema
!=0 );
409 if( (pTab
->tabFlags
& TF_Autoincrement
)!=0
410 && (pParse
->db
->mDbFlags
& DBFLAG_Vacuum
)==0
412 Parse
*pToplevel
= sqlite3ParseToplevel(pParse
);
414 Table
*pSeqTab
= pParse
->db
->aDb
[iDb
].pSchema
->pSeqTab
;
416 /* Verify that the sqlite_sequence table exists and is an ordinary
417 ** rowid table with exactly two columns.
418 ** Ticket d8dc2b3a58cd5dc2918a1d4acb 2018-05-23 */
420 || !HasRowid(pSeqTab
)
421 || NEVER(IsVirtual(pSeqTab
))
425 pParse
->rc
= SQLITE_CORRUPT_SEQUENCE
;
429 pInfo
= pToplevel
->pAinc
;
430 while( pInfo
&& pInfo
->pTab
!=pTab
){ pInfo
= pInfo
->pNext
; }
432 pInfo
= sqlite3DbMallocRawNN(pParse
->db
, sizeof(*pInfo
));
433 sqlite3ParserAddCleanup(pToplevel
, sqlite3DbFree
, pInfo
);
434 testcase( pParse
->earlyCleanup
);
435 if( pParse
->db
->mallocFailed
) return 0;
436 pInfo
->pNext
= pToplevel
->pAinc
;
437 pToplevel
->pAinc
= pInfo
;
440 pToplevel
->nMem
++; /* Register to hold name of table */
441 pInfo
->regCtr
= ++pToplevel
->nMem
; /* Max rowid register */
442 pToplevel
->nMem
+=2; /* Rowid in sqlite_sequence + orig max val */
444 memId
= pInfo
->regCtr
;
450 ** This routine generates code that will initialize all of the
451 ** register used by the autoincrement tracker.
453 void sqlite3AutoincrementBegin(Parse
*pParse
){
454 AutoincInfo
*p
; /* Information about an AUTOINCREMENT */
455 sqlite3
*db
= pParse
->db
; /* The database connection */
456 Db
*pDb
; /* Database only autoinc table */
457 int memId
; /* Register holding max rowid */
458 Vdbe
*v
= pParse
->pVdbe
; /* VDBE under construction */
460 /* This routine is never called during trigger-generation. It is
461 ** only called from the top-level */
462 assert( pParse
->pTriggerTab
==0 );
463 assert( sqlite3IsToplevel(pParse
) );
465 assert( v
); /* We failed long ago if this is not so */
466 for(p
= pParse
->pAinc
; p
; p
= p
->pNext
){
467 static const int iLn
= VDBE_OFFSET_LINENO(2);
468 static const VdbeOpList autoInc
[] = {
469 /* 0 */ {OP_Null
, 0, 0, 0},
470 /* 1 */ {OP_Rewind
, 0, 10, 0},
471 /* 2 */ {OP_Column
, 0, 0, 0},
472 /* 3 */ {OP_Ne
, 0, 9, 0},
473 /* 4 */ {OP_Rowid
, 0, 0, 0},
474 /* 5 */ {OP_Column
, 0, 1, 0},
475 /* 6 */ {OP_AddImm
, 0, 0, 0},
476 /* 7 */ {OP_Copy
, 0, 0, 0},
477 /* 8 */ {OP_Goto
, 0, 11, 0},
478 /* 9 */ {OP_Next
, 0, 2, 0},
479 /* 10 */ {OP_Integer
, 0, 0, 0},
480 /* 11 */ {OP_Close
, 0, 0, 0}
483 pDb
= &db
->aDb
[p
->iDb
];
485 assert( sqlite3SchemaMutexHeld(db
, 0, pDb
->pSchema
) );
486 sqlite3OpenTable(pParse
, 0, p
->iDb
, pDb
->pSchema
->pSeqTab
, OP_OpenRead
);
487 sqlite3VdbeLoadString(v
, memId
-1, p
->pTab
->zName
);
488 aOp
= sqlite3VdbeAddOpList(v
, ArraySize(autoInc
), autoInc
, iLn
);
495 aOp
[3].p5
= SQLITE_JUMPIFNULL
;
502 if( pParse
->nTab
==0 ) pParse
->nTab
= 1;
507 ** Update the maximum rowid for an autoincrement calculation.
509 ** This routine should be called when the regRowid register holds a
510 ** new rowid that is about to be inserted. If that new rowid is
511 ** larger than the maximum rowid in the memId memory cell, then the
512 ** memory cell is updated.
514 static void autoIncStep(Parse
*pParse
, int memId
, int regRowid
){
516 sqlite3VdbeAddOp2(pParse
->pVdbe
, OP_MemMax
, memId
, regRowid
);
521 ** This routine generates the code needed to write autoincrement
522 ** maximum rowid values back into the sqlite_sequence register.
523 ** Every statement that might do an INSERT into an autoincrement
524 ** table (either directly or through triggers) needs to call this
525 ** routine just before the "exit" code.
527 static SQLITE_NOINLINE
void autoIncrementEnd(Parse
*pParse
){
529 Vdbe
*v
= pParse
->pVdbe
;
530 sqlite3
*db
= pParse
->db
;
533 for(p
= pParse
->pAinc
; p
; p
= p
->pNext
){
534 static const int iLn
= VDBE_OFFSET_LINENO(2);
535 static const VdbeOpList autoIncEnd
[] = {
536 /* 0 */ {OP_NotNull
, 0, 2, 0},
537 /* 1 */ {OP_NewRowid
, 0, 0, 0},
538 /* 2 */ {OP_MakeRecord
, 0, 2, 0},
539 /* 3 */ {OP_Insert
, 0, 0, 0},
540 /* 4 */ {OP_Close
, 0, 0, 0}
543 Db
*pDb
= &db
->aDb
[p
->iDb
];
545 int memId
= p
->regCtr
;
547 iRec
= sqlite3GetTempReg(pParse
);
548 assert( sqlite3SchemaMutexHeld(db
, 0, pDb
->pSchema
) );
549 sqlite3VdbeAddOp3(v
, OP_Le
, memId
+2, sqlite3VdbeCurrentAddr(v
)+7, memId
);
551 sqlite3OpenTable(pParse
, 0, p
->iDb
, pDb
->pSchema
->pSeqTab
, OP_OpenWrite
);
552 aOp
= sqlite3VdbeAddOpList(v
, ArraySize(autoIncEnd
), autoIncEnd
, iLn
);
560 aOp
[3].p5
= OPFLAG_APPEND
;
561 sqlite3ReleaseTempReg(pParse
, iRec
);
564 void sqlite3AutoincrementEnd(Parse
*pParse
){
565 if( pParse
->pAinc
) autoIncrementEnd(pParse
);
569 ** If SQLITE_OMIT_AUTOINCREMENT is defined, then the three routines
570 ** above are all no-ops
572 # define autoIncBegin(A,B,C) (0)
573 # define autoIncStep(A,B,C)
574 #endif /* SQLITE_OMIT_AUTOINCREMENT */
577 /* Forward declaration */
578 static int xferOptimization(
579 Parse
*pParse
, /* Parser context */
580 Table
*pDest
, /* The table we are inserting into */
581 Select
*pSelect
, /* A SELECT statement to use as the data source */
582 int onError
, /* How to handle constraint errors */
583 int iDbDest
/* The database of pDest */
587 ** This routine is called to handle SQL of the following forms:
589 ** insert into TABLE (IDLIST) values(EXPRLIST),(EXPRLIST),...
590 ** insert into TABLE (IDLIST) select
591 ** insert into TABLE (IDLIST) default values
593 ** The IDLIST following the table name is always optional. If omitted,
594 ** then a list of all (non-hidden) columns for the table is substituted.
595 ** The IDLIST appears in the pColumn parameter. pColumn is NULL if IDLIST
598 ** For the pSelect parameter holds the values to be inserted for the
599 ** first two forms shown above. A VALUES clause is really just short-hand
600 ** for a SELECT statement that omits the FROM clause and everything else
601 ** that follows. If the pSelect parameter is NULL, that means that the
602 ** DEFAULT VALUES form of the INSERT statement is intended.
604 ** The code generated follows one of four templates. For a simple
605 ** insert with data coming from a single-row VALUES clause, the code executes
606 ** once straight down through. Pseudo-code follows (we call this
607 ** the "1st template"):
609 ** open write cursor to <table> and its indices
610 ** put VALUES clause expressions into registers
611 ** write the resulting record into <table>
614 ** The three remaining templates assume the statement is of the form
616 ** INSERT INTO <table> SELECT ...
618 ** If the SELECT clause is of the restricted form "SELECT * FROM <table2>" -
619 ** in other words if the SELECT pulls all columns from a single table
620 ** and there is no WHERE or LIMIT or GROUP BY or ORDER BY clauses, and
621 ** if <table2> and <table1> are distinct tables but have identical
622 ** schemas, including all the same indices, then a special optimization
623 ** is invoked that copies raw records from <table2> over to <table1>.
624 ** See the xferOptimization() function for the implementation of this
625 ** template. This is the 2nd template.
627 ** open a write cursor to <table>
628 ** open read cursor on <table2>
629 ** transfer all records in <table2> over to <table>
631 ** foreach index on <table>
632 ** open a write cursor on the <table> index
633 ** open a read cursor on the corresponding <table2> index
634 ** transfer all records from the read to the write cursors
638 ** The 3rd template is for when the second template does not apply
639 ** and the SELECT clause does not read from <table> at any time.
640 ** The generated code follows this template:
644 ** A: setup for the SELECT
645 ** loop over the rows in the SELECT
646 ** load values into registers R..R+n
649 ** cleanup after the SELECT
651 ** B: open write cursor to <table> and its indices
652 ** C: yield X, at EOF goto D
653 ** insert the select result into <table> from R..R+n
657 ** The 4th template is used if the insert statement takes its
658 ** values from a SELECT but the data is being inserted into a table
659 ** that is also read as part of the SELECT. In the third form,
660 ** we have to use an intermediate table to store the results of
661 ** the select. The template is like this:
665 ** A: setup for the SELECT
666 ** loop over the tables in the SELECT
667 ** load value into register R..R+n
670 ** cleanup after the SELECT
672 ** B: open temp table
673 ** L: yield X, at EOF goto M
674 ** insert row from R..R+n into temp table
676 ** M: open write cursor to <table> and its indices
678 ** C: loop over rows of intermediate table
679 ** transfer values form intermediate table into <table>
684 Parse
*pParse
, /* Parser context */
685 SrcList
*pTabList
, /* Name of table into which we are inserting */
686 Select
*pSelect
, /* A SELECT statement to use as the data source */
687 IdList
*pColumn
, /* Column names corresponding to IDLIST, or NULL. */
688 int onError
, /* How to handle constraint errors */
689 Upsert
*pUpsert
/* ON CONFLICT clauses for upsert, or NULL */
691 sqlite3
*db
; /* The main database structure */
692 Table
*pTab
; /* The table to insert into. aka TABLE */
693 int i
, j
; /* Loop counters */
694 Vdbe
*v
; /* Generate code into this virtual machine */
695 Index
*pIdx
; /* For looping over indices of the table */
696 int nColumn
; /* Number of columns in the data */
697 int nHidden
= 0; /* Number of hidden columns if TABLE is virtual */
698 int iDataCur
= 0; /* VDBE cursor that is the main data repository */
699 int iIdxCur
= 0; /* First index cursor */
700 int ipkColumn
= -1; /* Column that is the INTEGER PRIMARY KEY */
701 int endOfLoop
; /* Label for the end of the insertion loop */
702 int srcTab
= 0; /* Data comes from this temporary cursor if >=0 */
703 int addrInsTop
= 0; /* Jump to label "D" */
704 int addrCont
= 0; /* Top of insert loop. Label "C" in templates 3 and 4 */
705 SelectDest dest
; /* Destination for SELECT on rhs of INSERT */
706 int iDb
; /* Index of database holding TABLE */
707 u8 useTempTable
= 0; /* Store SELECT results in intermediate table */
708 u8 appendFlag
= 0; /* True if the insert is likely to be an append */
709 u8 withoutRowid
; /* 0 for normal table. 1 for WITHOUT ROWID table */
710 u8 bIdListInOrder
; /* True if IDLIST is in table order */
711 ExprList
*pList
= 0; /* List of VALUES() to be inserted */
712 int iRegStore
; /* Register in which to store next column */
714 /* Register allocations */
715 int regFromSelect
= 0;/* Base register for data coming from SELECT */
716 int regAutoinc
= 0; /* Register holding the AUTOINCREMENT counter */
717 int regRowCount
= 0; /* Memory cell used for the row counter */
718 int regIns
; /* Block of regs holding rowid+data being inserted */
719 int regRowid
; /* registers holding insert rowid */
720 int regData
; /* register holding first column to insert */
721 int *aRegIdx
= 0; /* One register allocated to each index */
723 #ifndef SQLITE_OMIT_TRIGGER
724 int isView
; /* True if attempting to insert into a view */
725 Trigger
*pTrigger
; /* List of triggers on pTab, if required */
726 int tmask
; /* Mask of trigger times */
730 assert( db
->pParse
==pParse
);
734 assert( db
->mallocFailed
==0 );
735 dest
.iSDParm
= 0; /* Suppress a harmless compiler warning */
737 /* If the Select object is really just a simple VALUES() list with a
738 ** single row (the common case) then keep that one row of values
739 ** and discard the other (unused) parts of the pSelect object
741 if( pSelect
&& (pSelect
->selFlags
& SF_Values
)!=0 && pSelect
->pPrior
==0 ){
742 pList
= pSelect
->pEList
;
744 sqlite3SelectDelete(db
, pSelect
);
748 /* Locate the table into which we will be inserting new information.
750 assert( pTabList
->nSrc
==1 );
751 pTab
= sqlite3SrcListLookup(pParse
, pTabList
);
755 iDb
= sqlite3SchemaToIndex(db
, pTab
->pSchema
);
756 assert( iDb
<db
->nDb
);
757 if( sqlite3AuthCheck(pParse
, SQLITE_INSERT
, pTab
->zName
, 0,
758 db
->aDb
[iDb
].zDbSName
) ){
761 withoutRowid
= !HasRowid(pTab
);
763 /* Figure out if we have any triggers and if the table being
764 ** inserted into is a view
766 #ifndef SQLITE_OMIT_TRIGGER
767 pTrigger
= sqlite3TriggersExist(pParse
, pTab
, TK_INSERT
, 0, &tmask
);
768 isView
= IsView(pTab
);
774 #ifdef SQLITE_OMIT_VIEW
778 assert( (pTrigger
&& tmask
) || (pTrigger
==0 && tmask
==0) );
780 #if TREETRACE_ENABLED
781 if( sqlite3TreeTrace
& 0x10000 ){
782 sqlite3TreeViewLine(0, "In sqlite3Insert() at %s:%d", __FILE__
, __LINE__
);
783 sqlite3TreeViewInsert(pParse
->pWith
, pTabList
, pColumn
, pSelect
, pList
,
784 onError
, pUpsert
, pTrigger
);
788 /* If pTab is really a view, make sure it has been initialized.
789 ** ViewGetColumnNames() is a no-op if pTab is not a view.
791 if( sqlite3ViewGetColumnNames(pParse
, pTab
) ){
795 /* Cannot insert into a read-only table.
797 if( sqlite3IsReadOnly(pParse
, pTab
, tmask
) ){
803 v
= sqlite3GetVdbe(pParse
);
804 if( v
==0 ) goto insert_cleanup
;
805 if( pParse
->nested
==0 ) sqlite3VdbeCountChanges(v
);
806 sqlite3BeginWriteOperation(pParse
, pSelect
|| pTrigger
, iDb
);
808 #ifndef SQLITE_OMIT_XFER_OPT
809 /* If the statement is of the form
811 ** INSERT INTO <table1> SELECT * FROM <table2>;
813 ** Then special optimizations can be applied that make the transfer
814 ** very fast and which reduce fragmentation of indices.
816 ** This is the 2nd template.
821 && xferOptimization(pParse
, pTab
, pSelect
, onError
, iDb
)
827 #endif /* SQLITE_OMIT_XFER_OPT */
829 /* If this is an AUTOINCREMENT table, look up the sequence number in the
830 ** sqlite_sequence table and store it in memory cell regAutoinc.
832 regAutoinc
= autoIncBegin(pParse
, iDb
, pTab
);
834 /* Allocate a block registers to hold the rowid and the values
835 ** for all columns of the new row.
837 regRowid
= regIns
= pParse
->nMem
+1;
838 pParse
->nMem
+= pTab
->nCol
+ 1;
839 if( IsVirtual(pTab
) ){
843 regData
= regRowid
+1;
845 /* If the INSERT statement included an IDLIST term, then make sure
846 ** all elements of the IDLIST really are columns of the table and
847 ** remember the column indices.
849 ** If the table has an INTEGER PRIMARY KEY column and that column
850 ** is named in the IDLIST, then record in the ipkColumn variable
851 ** the index into IDLIST of the primary key column. ipkColumn is
852 ** the index of the primary key as it appears in IDLIST, not as
853 ** is appears in the original table. (The index of the INTEGER
854 ** PRIMARY KEY in the original table is pTab->iPKey.) After this
855 ** loop, if ipkColumn==(-1), that means that integer primary key
856 ** is unspecified, and hence the table is either WITHOUT ROWID or
857 ** it will automatically generated an integer primary key.
859 ** bIdListInOrder is true if the columns in IDLIST are in storage
860 ** order. This enables an optimization that avoids shuffling the
861 ** columns into storage order. False negatives are harmless,
862 ** but false positives will cause database corruption.
864 bIdListInOrder
= (pTab
->tabFlags
& (TF_OOOHidden
|TF_HasStored
))==0;
866 assert( pColumn
->eU4
!=EU4_EXPR
);
867 pColumn
->eU4
= EU4_IDX
;
868 for(i
=0; i
<pColumn
->nId
; i
++){
869 pColumn
->a
[i
].u4
.idx
= -1;
871 for(i
=0; i
<pColumn
->nId
; i
++){
872 for(j
=0; j
<pTab
->nCol
; j
++){
873 if( sqlite3StrICmp(pColumn
->a
[i
].zName
, pTab
->aCol
[j
].zCnName
)==0 ){
874 pColumn
->a
[i
].u4
.idx
= j
;
875 if( i
!=j
) bIdListInOrder
= 0;
876 if( j
==pTab
->iPKey
){
877 ipkColumn
= i
; assert( !withoutRowid
);
879 #ifndef SQLITE_OMIT_GENERATED_COLUMNS
880 if( pTab
->aCol
[j
].colFlags
& (COLFLAG_STORED
|COLFLAG_VIRTUAL
) ){
881 sqlite3ErrorMsg(pParse
,
882 "cannot INSERT into generated column \"%s\"",
883 pTab
->aCol
[j
].zCnName
);
891 if( sqlite3IsRowid(pColumn
->a
[i
].zName
) && !withoutRowid
){
895 sqlite3ErrorMsg(pParse
, "table %S has no column named %s",
896 pTabList
->a
, pColumn
->a
[i
].zName
);
897 pParse
->checkSchema
= 1;
904 /* Figure out how many columns of data are supplied. If the data
905 ** is coming from a SELECT statement, then generate a co-routine that
906 ** produces a single row of the SELECT on each invocation. The
907 ** co-routine is the common header to the 3rd and 4th templates.
910 /* Data is coming from a SELECT or from a multi-row VALUES clause.
911 ** Generate a co-routine to run the SELECT. */
912 int regYield
; /* Register holding co-routine entry-point */
913 int addrTop
; /* Top of the co-routine */
914 int rc
; /* Result code */
916 regYield
= ++pParse
->nMem
;
917 addrTop
= sqlite3VdbeCurrentAddr(v
) + 1;
918 sqlite3VdbeAddOp3(v
, OP_InitCoroutine
, regYield
, 0, addrTop
);
919 sqlite3SelectDestInit(&dest
, SRT_Coroutine
, regYield
);
920 dest
.iSdst
= bIdListInOrder
? regData
: 0;
921 dest
.nSdst
= pTab
->nCol
;
922 rc
= sqlite3Select(pParse
, pSelect
, &dest
);
923 regFromSelect
= dest
.iSdst
;
924 assert( db
->pParse
==pParse
);
925 if( rc
|| pParse
->nErr
) goto insert_cleanup
;
926 assert( db
->mallocFailed
==0 );
927 sqlite3VdbeEndCoroutine(v
, regYield
);
928 sqlite3VdbeJumpHere(v
, addrTop
- 1); /* label B: */
929 assert( pSelect
->pEList
);
930 nColumn
= pSelect
->pEList
->nExpr
;
932 /* Set useTempTable to TRUE if the result of the SELECT statement
933 ** should be written into a temporary table (template 4). Set to
934 ** FALSE if each output row of the SELECT can be written directly into
935 ** the destination table (template 3).
937 ** A temp table must be used if the table being updated is also one
938 ** of the tables being read by the SELECT statement. Also use a
939 ** temp table in the case of row triggers.
941 if( pTrigger
|| readsTable(pParse
, iDb
, pTab
) ){
946 /* Invoke the coroutine to extract information from the SELECT
947 ** and add it to a transient table srcTab. The code generated
948 ** here is from the 4th template:
950 ** B: open temp table
951 ** L: yield X, goto M at EOF
952 ** insert row from R..R+n into temp table
956 int regRec
; /* Register to hold packed record */
957 int regTempRowid
; /* Register to hold temp table ROWID */
958 int addrL
; /* Label "L" */
960 srcTab
= pParse
->nTab
++;
961 regRec
= sqlite3GetTempReg(pParse
);
962 regTempRowid
= sqlite3GetTempReg(pParse
);
963 sqlite3VdbeAddOp2(v
, OP_OpenEphemeral
, srcTab
, nColumn
);
964 addrL
= sqlite3VdbeAddOp1(v
, OP_Yield
, dest
.iSDParm
); VdbeCoverage(v
);
965 sqlite3VdbeAddOp3(v
, OP_MakeRecord
, regFromSelect
, nColumn
, regRec
);
966 sqlite3VdbeAddOp2(v
, OP_NewRowid
, srcTab
, regTempRowid
);
967 sqlite3VdbeAddOp3(v
, OP_Insert
, srcTab
, regRec
, regTempRowid
);
968 sqlite3VdbeGoto(v
, addrL
);
969 sqlite3VdbeJumpHere(v
, addrL
);
970 sqlite3ReleaseTempReg(pParse
, regRec
);
971 sqlite3ReleaseTempReg(pParse
, regTempRowid
);
974 /* This is the case if the data for the INSERT is coming from a
975 ** single-row VALUES clause
978 memset(&sNC
, 0, sizeof(sNC
));
981 assert( useTempTable
==0 );
983 nColumn
= pList
->nExpr
;
984 if( sqlite3ResolveExprListNames(&sNC
, pList
) ){
992 /* If there is no IDLIST term but the table has an integer primary
993 ** key, the set the ipkColumn variable to the integer primary key
994 ** column index in the original table definition.
996 if( pColumn
==0 && nColumn
>0 ){
997 ipkColumn
= pTab
->iPKey
;
998 #ifndef SQLITE_OMIT_GENERATED_COLUMNS
999 if( ipkColumn
>=0 && (pTab
->tabFlags
& TF_HasGenerated
)!=0 ){
1000 testcase( pTab
->tabFlags
& TF_HasVirtual
);
1001 testcase( pTab
->tabFlags
& TF_HasStored
);
1002 for(i
=ipkColumn
-1; i
>=0; i
--){
1003 if( pTab
->aCol
[i
].colFlags
& COLFLAG_GENERATED
){
1004 testcase( pTab
->aCol
[i
].colFlags
& COLFLAG_VIRTUAL
);
1005 testcase( pTab
->aCol
[i
].colFlags
& COLFLAG_STORED
);
1012 /* Make sure the number of columns in the source data matches the number
1013 ** of columns to be inserted into the table.
1015 assert( TF_HasHidden
==COLFLAG_HIDDEN
);
1016 assert( TF_HasGenerated
==COLFLAG_GENERATED
);
1017 assert( COLFLAG_NOINSERT
==(COLFLAG_GENERATED
|COLFLAG_HIDDEN
) );
1018 if( (pTab
->tabFlags
& (TF_HasGenerated
|TF_HasHidden
))!=0 ){
1019 for(i
=0; i
<pTab
->nCol
; i
++){
1020 if( pTab
->aCol
[i
].colFlags
& COLFLAG_NOINSERT
) nHidden
++;
1023 if( nColumn
!=(pTab
->nCol
-nHidden
) ){
1024 sqlite3ErrorMsg(pParse
,
1025 "table %S has %d columns but %d values were supplied",
1026 pTabList
->a
, pTab
->nCol
-nHidden
, nColumn
);
1027 goto insert_cleanup
;
1030 if( pColumn
!=0 && nColumn
!=pColumn
->nId
){
1031 sqlite3ErrorMsg(pParse
, "%d values for %d columns", nColumn
, pColumn
->nId
);
1032 goto insert_cleanup
;
1035 /* Initialize the count of rows to be inserted
1037 if( (db
->flags
& SQLITE_CountRows
)!=0
1039 && !pParse
->pTriggerTab
1040 && !pParse
->bReturning
1042 regRowCount
= ++pParse
->nMem
;
1043 sqlite3VdbeAddOp2(v
, OP_Integer
, 0, regRowCount
);
1046 /* If this is not a view, open the table and and all indices */
1049 nIdx
= sqlite3OpenTableAndIndices(pParse
, pTab
, OP_OpenWrite
, 0, -1, 0,
1050 &iDataCur
, &iIdxCur
);
1051 aRegIdx
= sqlite3DbMallocRawNN(db
, sizeof(int)*(nIdx
+2));
1053 goto insert_cleanup
;
1055 for(i
=0, pIdx
=pTab
->pIndex
; i
<nIdx
; pIdx
=pIdx
->pNext
, i
++){
1057 aRegIdx
[i
] = ++pParse
->nMem
;
1058 pParse
->nMem
+= pIdx
->nColumn
;
1060 aRegIdx
[i
] = ++pParse
->nMem
; /* Register to store the table record */
1062 #ifndef SQLITE_OMIT_UPSERT
1065 if( IsVirtual(pTab
) ){
1066 sqlite3ErrorMsg(pParse
, "UPSERT not implemented for virtual table \"%s\"",
1068 goto insert_cleanup
;
1071 sqlite3ErrorMsg(pParse
, "cannot UPSERT a view");
1072 goto insert_cleanup
;
1074 if( sqlite3HasExplicitNulls(pParse
, pUpsert
->pUpsertTarget
) ){
1075 goto insert_cleanup
;
1077 pTabList
->a
[0].iCursor
= iDataCur
;
1080 pNx
->pUpsertSrc
= pTabList
;
1081 pNx
->regData
= regData
;
1082 pNx
->iDataCur
= iDataCur
;
1083 pNx
->iIdxCur
= iIdxCur
;
1084 if( pNx
->pUpsertTarget
){
1085 if( sqlite3UpsertAnalyzeTarget(pParse
, pTabList
, pNx
) ){
1086 goto insert_cleanup
;
1089 pNx
= pNx
->pNextUpsert
;
1095 /* This is the top of the main insertion loop */
1097 /* This block codes the top of loop only. The complete loop is the
1098 ** following pseudocode (template 4):
1100 ** rewind temp table, if empty goto D
1101 ** C: loop over rows of intermediate table
1102 ** transfer values form intermediate table into <table>
1106 addrInsTop
= sqlite3VdbeAddOp1(v
, OP_Rewind
, srcTab
); VdbeCoverage(v
);
1107 addrCont
= sqlite3VdbeCurrentAddr(v
);
1108 }else if( pSelect
){
1109 /* This block codes the top of loop only. The complete loop is the
1110 ** following pseudocode (template 3):
1112 ** C: yield X, at EOF goto D
1113 ** insert the select result into <table> from R..R+n
1117 sqlite3VdbeReleaseRegisters(pParse
, regData
, pTab
->nCol
, 0, 0);
1118 addrInsTop
= addrCont
= sqlite3VdbeAddOp1(v
, OP_Yield
, dest
.iSDParm
);
1121 /* tag-20191021-001: If the INTEGER PRIMARY KEY is being generated by the
1122 ** SELECT, go ahead and copy the value into the rowid slot now, so that
1123 ** the value does not get overwritten by a NULL at tag-20191021-002. */
1124 sqlite3VdbeAddOp2(v
, OP_Copy
, regFromSelect
+ipkColumn
, regRowid
);
1128 /* Compute data for ordinary columns of the new entry. Values
1129 ** are written in storage order into registers starting with regData.
1130 ** Only ordinary columns are computed in this loop. The rowid
1131 ** (if there is one) is computed later and generated columns are
1132 ** computed after the rowid since they might depend on the value
1136 iRegStore
= regData
; assert( regData
==regRowid
+1 );
1137 for(i
=0; i
<pTab
->nCol
; i
++, iRegStore
++){
1140 assert( i
>=nHidden
);
1141 if( i
==pTab
->iPKey
){
1142 /* tag-20191021-002: References to the INTEGER PRIMARY KEY are filled
1143 ** using the rowid. So put a NULL in the IPK slot of the record to avoid
1144 ** using excess space. The file format definition requires this extra
1145 ** NULL - we cannot optimize further by skipping the column completely */
1146 sqlite3VdbeAddOp1(v
, OP_SoftNull
, iRegStore
);
1149 if( ((colFlags
= pTab
->aCol
[i
].colFlags
) & COLFLAG_NOINSERT
)!=0 ){
1151 if( (colFlags
& COLFLAG_VIRTUAL
)!=0 ){
1152 /* Virtual columns do not participate in OP_MakeRecord. So back up
1153 ** iRegStore by one slot to compensate for the iRegStore++ in the
1154 ** outer for() loop */
1157 }else if( (colFlags
& COLFLAG_STORED
)!=0 ){
1158 /* Stored columns are computed later. But if there are BEFORE
1159 ** triggers, the slots used for stored columns will be OP_Copy-ed
1160 ** to a second block of registers, so the register needs to be
1161 ** initialized to NULL to avoid an uninitialized register read */
1162 if( tmask
& TRIGGER_BEFORE
){
1163 sqlite3VdbeAddOp1(v
, OP_SoftNull
, iRegStore
);
1166 }else if( pColumn
==0 ){
1167 /* Hidden columns that are not explicitly named in the INSERT
1168 ** get there default value */
1169 sqlite3ExprCodeFactorable(pParse
,
1170 sqlite3ColumnExpr(pTab
, &pTab
->aCol
[i
]),
1176 assert( pColumn
->eU4
==EU4_IDX
);
1177 for(j
=0; j
<pColumn
->nId
&& pColumn
->a
[j
].u4
.idx
!=i
; j
++){}
1178 if( j
>=pColumn
->nId
){
1179 /* A column not named in the insert column list gets its
1181 sqlite3ExprCodeFactorable(pParse
,
1182 sqlite3ColumnExpr(pTab
, &pTab
->aCol
[i
]),
1187 }else if( nColumn
==0 ){
1188 /* This is INSERT INTO ... DEFAULT VALUES. Load the default value. */
1189 sqlite3ExprCodeFactorable(pParse
,
1190 sqlite3ColumnExpr(pTab
, &pTab
->aCol
[i
]),
1198 sqlite3VdbeAddOp3(v
, OP_Column
, srcTab
, k
, iRegStore
);
1199 }else if( pSelect
){
1200 if( regFromSelect
!=regData
){
1201 sqlite3VdbeAddOp2(v
, OP_SCopy
, regFromSelect
+k
, iRegStore
);
1204 Expr
*pX
= pList
->a
[k
].pExpr
;
1205 int y
= sqlite3ExprCodeTarget(pParse
, pX
, iRegStore
);
1207 sqlite3VdbeAddOp2(v
,
1208 ExprHasProperty(pX
, EP_Subquery
) ? OP_Copy
: OP_SCopy
, y
, iRegStore
);
1214 /* Run the BEFORE and INSTEAD OF triggers, if there are any
1216 endOfLoop
= sqlite3VdbeMakeLabel(pParse
);
1217 if( tmask
& TRIGGER_BEFORE
){
1218 int regCols
= sqlite3GetTempRange(pParse
, pTab
->nCol
+1);
1220 /* build the NEW.* reference row. Note that if there is an INTEGER
1221 ** PRIMARY KEY into which a NULL is being inserted, that NULL will be
1222 ** translated into a unique ID for the row. But on a BEFORE trigger,
1223 ** we do not know what the unique ID will be (because the insert has
1224 ** not happened yet) so we substitute a rowid of -1
1227 sqlite3VdbeAddOp2(v
, OP_Integer
, -1, regCols
);
1230 assert( !withoutRowid
);
1232 sqlite3VdbeAddOp3(v
, OP_Column
, srcTab
, ipkColumn
, regCols
);
1234 assert( pSelect
==0 ); /* Otherwise useTempTable is true */
1235 sqlite3ExprCode(pParse
, pList
->a
[ipkColumn
].pExpr
, regCols
);
1237 addr1
= sqlite3VdbeAddOp1(v
, OP_NotNull
, regCols
); VdbeCoverage(v
);
1238 sqlite3VdbeAddOp2(v
, OP_Integer
, -1, regCols
);
1239 sqlite3VdbeJumpHere(v
, addr1
);
1240 sqlite3VdbeAddOp1(v
, OP_MustBeInt
, regCols
); VdbeCoverage(v
);
1243 /* Copy the new data already generated. */
1244 assert( pTab
->nNVCol
>0 );
1245 sqlite3VdbeAddOp3(v
, OP_Copy
, regRowid
+1, regCols
+1, pTab
->nNVCol
-1);
1247 #ifndef SQLITE_OMIT_GENERATED_COLUMNS
1248 /* Compute the new value for generated columns after all other
1249 ** columns have already been computed. This must be done after
1250 ** computing the ROWID in case one of the generated columns
1251 ** refers to the ROWID. */
1252 if( pTab
->tabFlags
& TF_HasGenerated
){
1253 testcase( pTab
->tabFlags
& TF_HasVirtual
);
1254 testcase( pTab
->tabFlags
& TF_HasStored
);
1255 sqlite3ComputeGeneratedColumns(pParse
, regCols
+1, pTab
);
1259 /* If this is an INSERT on a view with an INSTEAD OF INSERT trigger,
1260 ** do not attempt any conversions before assembling the record.
1261 ** If this is a real table, attempt conversions as required by the
1262 ** table column affinities.
1265 sqlite3TableAffinity(v
, pTab
, regCols
+1);
1268 /* Fire BEFORE or INSTEAD OF triggers */
1269 sqlite3CodeRowTrigger(pParse
, pTrigger
, TK_INSERT
, 0, TRIGGER_BEFORE
,
1270 pTab
, regCols
-pTab
->nCol
-1, onError
, endOfLoop
);
1272 sqlite3ReleaseTempRange(pParse
, regCols
, pTab
->nCol
+1);
1276 if( IsVirtual(pTab
) ){
1277 /* The row that the VUpdate opcode will delete: none */
1278 sqlite3VdbeAddOp2(v
, OP_Null
, 0, regIns
);
1281 /* Compute the new rowid */
1283 sqlite3VdbeAddOp3(v
, OP_Column
, srcTab
, ipkColumn
, regRowid
);
1284 }else if( pSelect
){
1285 /* Rowid already initialized at tag-20191021-001 */
1287 Expr
*pIpk
= pList
->a
[ipkColumn
].pExpr
;
1288 if( pIpk
->op
==TK_NULL
&& !IsVirtual(pTab
) ){
1289 sqlite3VdbeAddOp3(v
, OP_NewRowid
, iDataCur
, regRowid
, regAutoinc
);
1292 sqlite3ExprCode(pParse
, pList
->a
[ipkColumn
].pExpr
, regRowid
);
1295 /* If the PRIMARY KEY expression is NULL, then use OP_NewRowid
1296 ** to generate a unique primary key value.
1300 if( !IsVirtual(pTab
) ){
1301 addr1
= sqlite3VdbeAddOp1(v
, OP_NotNull
, regRowid
); VdbeCoverage(v
);
1302 sqlite3VdbeAddOp3(v
, OP_NewRowid
, iDataCur
, regRowid
, regAutoinc
);
1303 sqlite3VdbeJumpHere(v
, addr1
);
1305 addr1
= sqlite3VdbeCurrentAddr(v
);
1306 sqlite3VdbeAddOp2(v
, OP_IsNull
, regRowid
, addr1
+2); VdbeCoverage(v
);
1308 sqlite3VdbeAddOp1(v
, OP_MustBeInt
, regRowid
); VdbeCoverage(v
);
1310 }else if( IsVirtual(pTab
) || withoutRowid
){
1311 sqlite3VdbeAddOp2(v
, OP_Null
, 0, regRowid
);
1313 sqlite3VdbeAddOp3(v
, OP_NewRowid
, iDataCur
, regRowid
, regAutoinc
);
1316 autoIncStep(pParse
, regAutoinc
, regRowid
);
1318 #ifndef SQLITE_OMIT_GENERATED_COLUMNS
1319 /* Compute the new value for generated columns after all other
1320 ** columns have already been computed. This must be done after
1321 ** computing the ROWID in case one of the generated columns
1322 ** is derived from the INTEGER PRIMARY KEY. */
1323 if( pTab
->tabFlags
& TF_HasGenerated
){
1324 sqlite3ComputeGeneratedColumns(pParse
, regRowid
+1, pTab
);
1328 /* Generate code to check constraints and generate index keys and
1329 ** do the insertion.
1331 #ifndef SQLITE_OMIT_VIRTUALTABLE
1332 if( IsVirtual(pTab
) ){
1333 const char *pVTab
= (const char *)sqlite3GetVTable(db
, pTab
);
1334 sqlite3VtabMakeWritable(pParse
, pTab
);
1335 sqlite3VdbeAddOp4(v
, OP_VUpdate
, 1, pTab
->nCol
+2, regIns
, pVTab
, P4_VTAB
);
1336 sqlite3VdbeChangeP5(v
, onError
==OE_Default
? OE_Abort
: onError
);
1337 sqlite3MayAbort(pParse
);
1341 int isReplace
= 0;/* Set to true if constraints may cause a replace */
1342 int bUseSeek
; /* True to use OPFLAG_SEEKRESULT */
1343 sqlite3GenerateConstraintChecks(pParse
, pTab
, aRegIdx
, iDataCur
, iIdxCur
,
1344 regIns
, 0, ipkColumn
>=0, onError
, endOfLoop
, &isReplace
, 0, pUpsert
1346 if( db
->flags
& SQLITE_ForeignKeys
){
1347 sqlite3FkCheck(pParse
, pTab
, 0, regIns
, 0, 0);
1350 /* Set the OPFLAG_USESEEKRESULT flag if either (a) there are no REPLACE
1351 ** constraints or (b) there are no triggers and this table is not a
1352 ** parent table in a foreign key constraint. It is safe to set the
1353 ** flag in the second case as if any REPLACE constraint is hit, an
1354 ** OP_Delete or OP_IdxDelete instruction will be executed on each
1355 ** cursor that is disturbed. And these instructions both clear the
1356 ** VdbeCursor.seekResult variable, disabling the OPFLAG_USESEEKRESULT
1357 ** functionality. */
1358 bUseSeek
= (isReplace
==0 || !sqlite3VdbeHasSubProgram(v
));
1359 sqlite3CompleteInsertion(pParse
, pTab
, iDataCur
, iIdxCur
,
1360 regIns
, aRegIdx
, 0, appendFlag
, bUseSeek
1363 #ifdef SQLITE_ALLOW_ROWID_IN_VIEW
1364 }else if( pParse
->bReturning
){
1365 /* If there is a RETURNING clause, populate the rowid register with
1366 ** constant value -1, in case one or more of the returned expressions
1367 ** refer to the "rowid" of the view. */
1368 sqlite3VdbeAddOp2(v
, OP_Integer
, -1, regRowid
);
1372 /* Update the count of rows that are inserted
1375 sqlite3VdbeAddOp2(v
, OP_AddImm
, regRowCount
, 1);
1379 /* Code AFTER triggers */
1380 sqlite3CodeRowTrigger(pParse
, pTrigger
, TK_INSERT
, 0, TRIGGER_AFTER
,
1381 pTab
, regData
-2-pTab
->nCol
, onError
, endOfLoop
);
1384 /* The bottom of the main insertion loop, if the data source
1385 ** is a SELECT statement.
1387 sqlite3VdbeResolveLabel(v
, endOfLoop
);
1389 sqlite3VdbeAddOp2(v
, OP_Next
, srcTab
, addrCont
); VdbeCoverage(v
);
1390 sqlite3VdbeJumpHere(v
, addrInsTop
);
1391 sqlite3VdbeAddOp1(v
, OP_Close
, srcTab
);
1392 }else if( pSelect
){
1393 sqlite3VdbeGoto(v
, addrCont
);
1395 /* If we are jumping back to an OP_Yield that is preceded by an
1396 ** OP_ReleaseReg, set the p5 flag on the OP_Goto so that the
1397 ** OP_ReleaseReg will be included in the loop. */
1398 if( sqlite3VdbeGetOp(v
, addrCont
-1)->opcode
==OP_ReleaseReg
){
1399 assert( sqlite3VdbeGetOp(v
, addrCont
)->opcode
==OP_Yield
);
1400 sqlite3VdbeChangeP5(v
, 1);
1403 sqlite3VdbeJumpHere(v
, addrInsTop
);
1406 #ifndef SQLITE_OMIT_XFER_OPT
1408 #endif /* SQLITE_OMIT_XFER_OPT */
1409 /* Update the sqlite_sequence table by storing the content of the
1410 ** maximum rowid counter values recorded while inserting into
1411 ** autoincrement tables.
1413 if( pParse
->nested
==0 && pParse
->pTriggerTab
==0 ){
1414 sqlite3AutoincrementEnd(pParse
);
1418 ** Return the number of rows inserted. If this routine is
1419 ** generating code because of a call to sqlite3NestedParse(), do not
1420 ** invoke the callback function.
1423 sqlite3CodeChangeCount(v
, regRowCount
, "rows inserted");
1427 sqlite3SrcListDelete(db
, pTabList
);
1428 sqlite3ExprListDelete(db
, pList
);
1429 sqlite3UpsertDelete(db
, pUpsert
);
1430 sqlite3SelectDelete(db
, pSelect
);
1431 sqlite3IdListDelete(db
, pColumn
);
1432 if( aRegIdx
) sqlite3DbNNFreeNN(db
, aRegIdx
);
1435 /* Make sure "isView" and other macros defined above are undefined. Otherwise
1436 ** they may interfere with compilation of other functions in this file
1437 ** (or in another file, if this file becomes part of the amalgamation). */
1449 ** Meanings of bits in of pWalker->eCode for
1450 ** sqlite3ExprReferencesUpdatedColumn()
1452 #define CKCNSTRNT_COLUMN 0x01 /* CHECK constraint uses a changing column */
1453 #define CKCNSTRNT_ROWID 0x02 /* CHECK constraint references the ROWID */
1455 /* This is the Walker callback from sqlite3ExprReferencesUpdatedColumn().
1456 * Set bit 0x01 of pWalker->eCode if pWalker->eCode to 0 and if this
1457 ** expression node references any of the
1458 ** columns that are being modifed by an UPDATE statement.
1460 static int checkConstraintExprNode(Walker
*pWalker
, Expr
*pExpr
){
1461 if( pExpr
->op
==TK_COLUMN
){
1462 assert( pExpr
->iColumn
>=0 || pExpr
->iColumn
==-1 );
1463 if( pExpr
->iColumn
>=0 ){
1464 if( pWalker
->u
.aiCol
[pExpr
->iColumn
]>=0 ){
1465 pWalker
->eCode
|= CKCNSTRNT_COLUMN
;
1468 pWalker
->eCode
|= CKCNSTRNT_ROWID
;
1471 return WRC_Continue
;
1475 ** pExpr is a CHECK constraint on a row that is being UPDATE-ed. The
1476 ** only columns that are modified by the UPDATE are those for which
1477 ** aiChng[i]>=0, and also the ROWID is modified if chngRowid is true.
1479 ** Return true if CHECK constraint pExpr uses any of the
1480 ** changing columns (or the rowid if it is changing). In other words,
1481 ** return true if this CHECK constraint must be validated for
1482 ** the new row in the UPDATE statement.
1484 ** 2018-09-15: pExpr might also be an expression for an index-on-expressions.
1485 ** The operation of this routine is the same - return true if an only if
1486 ** the expression uses one or more of columns identified by the second and
1489 int sqlite3ExprReferencesUpdatedColumn(
1490 Expr
*pExpr
, /* The expression to be checked */
1491 int *aiChng
, /* aiChng[x]>=0 if column x changed by the UPDATE */
1492 int chngRowid
/* True if UPDATE changes the rowid */
1495 memset(&w
, 0, sizeof(w
));
1497 w
.xExprCallback
= checkConstraintExprNode
;
1499 sqlite3WalkExpr(&w
, pExpr
);
1501 testcase( (w
.eCode
& CKCNSTRNT_ROWID
)!=0 );
1502 w
.eCode
&= ~CKCNSTRNT_ROWID
;
1504 testcase( w
.eCode
==0 );
1505 testcase( w
.eCode
==CKCNSTRNT_COLUMN
);
1506 testcase( w
.eCode
==CKCNSTRNT_ROWID
);
1507 testcase( w
.eCode
==(CKCNSTRNT_ROWID
|CKCNSTRNT_COLUMN
) );
1512 ** The sqlite3GenerateConstraintChecks() routine usually wants to visit
1513 ** the indexes of a table in the order provided in the Table->pIndex list.
1514 ** However, sometimes (rarely - when there is an upsert) it wants to visit
1515 ** the indexes in a different order. The following data structures accomplish
1518 ** The IndexIterator object is used to walk through all of the indexes
1519 ** of a table in either Index.pNext order, or in some other order established
1520 ** by an array of IndexListTerm objects.
1522 typedef struct IndexListTerm IndexListTerm
;
1523 typedef struct IndexIterator IndexIterator
;
1524 struct IndexIterator
{
1525 int eType
; /* 0 for Index.pNext list. 1 for an array of IndexListTerm */
1526 int i
; /* Index of the current item from the list */
1528 struct { /* Use this object for eType==0: A Index.pNext list */
1529 Index
*pIdx
; /* The current Index */
1531 struct { /* Use this object for eType==1; Array of IndexListTerm */
1532 int nIdx
; /* Size of the array */
1533 IndexListTerm
*aIdx
; /* Array of IndexListTerms */
1538 /* When IndexIterator.eType==1, then each index is an array of instances
1539 ** of the following object
1541 struct IndexListTerm
{
1542 Index
*p
; /* The index */
1543 int ix
; /* Which entry in the original Table.pIndex list is this index*/
1546 /* Return the first index on the list */
1547 static Index
*indexIteratorFirst(IndexIterator
*pIter
, int *pIx
){
1548 assert( pIter
->i
==0 );
1550 *pIx
= pIter
->u
.ax
.aIdx
[0].ix
;
1551 return pIter
->u
.ax
.aIdx
[0].p
;
1554 return pIter
->u
.lx
.pIdx
;
1558 /* Return the next index from the list. Return NULL when out of indexes */
1559 static Index
*indexIteratorNext(IndexIterator
*pIter
, int *pIx
){
1562 if( i
>=pIter
->u
.ax
.nIdx
){
1566 *pIx
= pIter
->u
.ax
.aIdx
[i
].ix
;
1567 return pIter
->u
.ax
.aIdx
[i
].p
;
1570 pIter
->u
.lx
.pIdx
= pIter
->u
.lx
.pIdx
->pNext
;
1571 return pIter
->u
.lx
.pIdx
;
1576 ** Generate code to do constraint checks prior to an INSERT or an UPDATE
1579 ** The regNewData parameter is the first register in a range that contains
1580 ** the data to be inserted or the data after the update. There will be
1581 ** pTab->nCol+1 registers in this range. The first register (the one
1582 ** that regNewData points to) will contain the new rowid, or NULL in the
1583 ** case of a WITHOUT ROWID table. The second register in the range will
1584 ** contain the content of the first table column. The third register will
1585 ** contain the content of the second table column. And so forth.
1587 ** The regOldData parameter is similar to regNewData except that it contains
1588 ** the data prior to an UPDATE rather than afterwards. regOldData is zero
1589 ** for an INSERT. This routine can distinguish between UPDATE and INSERT by
1590 ** checking regOldData for zero.
1592 ** For an UPDATE, the pkChng boolean is true if the true primary key (the
1593 ** rowid for a normal table or the PRIMARY KEY for a WITHOUT ROWID table)
1594 ** might be modified by the UPDATE. If pkChng is false, then the key of
1595 ** the iDataCur content table is guaranteed to be unchanged by the UPDATE.
1597 ** For an INSERT, the pkChng boolean indicates whether or not the rowid
1598 ** was explicitly specified as part of the INSERT statement. If pkChng
1599 ** is zero, it means that the either rowid is computed automatically or
1600 ** that the table is a WITHOUT ROWID table and has no rowid. On an INSERT,
1601 ** pkChng will only be true if the INSERT statement provides an integer
1602 ** value for either the rowid column or its INTEGER PRIMARY KEY alias.
1604 ** The code generated by this routine will store new index entries into
1605 ** registers identified by aRegIdx[]. No index entry is created for
1606 ** indices where aRegIdx[i]==0. The order of indices in aRegIdx[] is
1607 ** the same as the order of indices on the linked list of indices
1610 ** (2019-05-07) The generated code also creates a new record for the
1611 ** main table, if pTab is a rowid table, and stores that record in the
1612 ** register identified by aRegIdx[nIdx] - in other words in the first
1613 ** entry of aRegIdx[] past the last index. It is important that the
1614 ** record be generated during constraint checks to avoid affinity changes
1615 ** to the register content that occur after constraint checks but before
1616 ** the new record is inserted.
1618 ** The caller must have already opened writeable cursors on the main
1619 ** table and all applicable indices (that is to say, all indices for which
1620 ** aRegIdx[] is not zero). iDataCur is the cursor for the main table when
1621 ** inserting or updating a rowid table, or the cursor for the PRIMARY KEY
1622 ** index when operating on a WITHOUT ROWID table. iIdxCur is the cursor
1623 ** for the first index in the pTab->pIndex list. Cursors for other indices
1624 ** are at iIdxCur+N for the N-th element of the pTab->pIndex list.
1626 ** This routine also generates code to check constraints. NOT NULL,
1627 ** CHECK, and UNIQUE constraints are all checked. If a constraint fails,
1628 ** then the appropriate action is performed. There are five possible
1629 ** actions: ROLLBACK, ABORT, FAIL, REPLACE, and IGNORE.
1631 ** Constraint type Action What Happens
1632 ** --------------- ---------- ----------------------------------------
1633 ** any ROLLBACK The current transaction is rolled back and
1634 ** sqlite3_step() returns immediately with a
1635 ** return code of SQLITE_CONSTRAINT.
1637 ** any ABORT Back out changes from the current command
1638 ** only (do not do a complete rollback) then
1639 ** cause sqlite3_step() to return immediately
1640 ** with SQLITE_CONSTRAINT.
1642 ** any FAIL Sqlite3_step() returns immediately with a
1643 ** return code of SQLITE_CONSTRAINT. The
1644 ** transaction is not rolled back and any
1645 ** changes to prior rows are retained.
1647 ** any IGNORE The attempt in insert or update the current
1648 ** row is skipped, without throwing an error.
1649 ** Processing continues with the next row.
1650 ** (There is an immediate jump to ignoreDest.)
1652 ** NOT NULL REPLACE The NULL value is replace by the default
1653 ** value for that column. If the default value
1654 ** is NULL, the action is the same as ABORT.
1656 ** UNIQUE REPLACE The other row that conflicts with the row
1657 ** being inserted is removed.
1659 ** CHECK REPLACE Illegal. The results in an exception.
1661 ** Which action to take is determined by the overrideError parameter.
1662 ** Or if overrideError==OE_Default, then the pParse->onError parameter
1663 ** is used. Or if pParse->onError==OE_Default then the onError value
1664 ** for the constraint is used.
1666 void sqlite3GenerateConstraintChecks(
1667 Parse
*pParse
, /* The parser context */
1668 Table
*pTab
, /* The table being inserted or updated */
1669 int *aRegIdx
, /* Use register aRegIdx[i] for index i. 0 for unused */
1670 int iDataCur
, /* Canonical data cursor (main table or PK index) */
1671 int iIdxCur
, /* First index cursor */
1672 int regNewData
, /* First register in a range holding values to insert */
1673 int regOldData
, /* Previous content. 0 for INSERTs */
1674 u8 pkChng
, /* Non-zero if the rowid or PRIMARY KEY changed */
1675 u8 overrideError
, /* Override onError to this if not OE_Default */
1676 int ignoreDest
, /* Jump to this label on an OE_Ignore resolution */
1677 int *pbMayReplace
, /* OUT: Set to true if constraint may cause a replace */
1678 int *aiChng
, /* column i is unchanged if aiChng[i]<0 */
1679 Upsert
*pUpsert
/* ON CONFLICT clauses, if any. NULL otherwise */
1681 Vdbe
*v
; /* VDBE under constrution */
1682 Index
*pIdx
; /* Pointer to one of the indices */
1683 Index
*pPk
= 0; /* The PRIMARY KEY index for WITHOUT ROWID tables */
1684 sqlite3
*db
; /* Database connection */
1685 int i
; /* loop counter */
1686 int ix
; /* Index loop counter */
1687 int nCol
; /* Number of columns */
1688 int onError
; /* Conflict resolution strategy */
1689 int seenReplace
= 0; /* True if REPLACE is used to resolve INT PK conflict */
1690 int nPkField
; /* Number of fields in PRIMARY KEY. 1 for ROWID tables */
1691 Upsert
*pUpsertClause
= 0; /* The specific ON CONFLICT clause for pIdx */
1692 u8 isUpdate
; /* True if this is an UPDATE operation */
1693 u8 bAffinityDone
= 0; /* True if the OP_Affinity operation has been run */
1694 int upsertIpkReturn
= 0; /* Address of Goto at end of IPK uniqueness check */
1695 int upsertIpkDelay
= 0; /* Address of Goto to bypass initial IPK check */
1696 int ipkTop
= 0; /* Top of the IPK uniqueness check */
1697 int ipkBottom
= 0; /* OP_Goto at the end of the IPK uniqueness check */
1698 /* Variables associated with retesting uniqueness constraints after
1699 ** replace triggers fire have run */
1700 int regTrigCnt
; /* Register used to count replace trigger invocations */
1701 int addrRecheck
= 0; /* Jump here to recheck all uniqueness constraints */
1702 int lblRecheckOk
= 0; /* Each recheck jumps to this label if it passes */
1703 Trigger
*pTrigger
; /* List of DELETE triggers on the table pTab */
1704 int nReplaceTrig
= 0; /* Number of replace triggers coded */
1705 IndexIterator sIdxIter
; /* Index iterator */
1707 isUpdate
= regOldData
!=0;
1711 assert( !IsView(pTab
) ); /* This table is not a VIEW */
1714 /* pPk is the PRIMARY KEY index for WITHOUT ROWID tables and NULL for
1715 ** normal rowid tables. nPkField is the number of key fields in the
1716 ** pPk index or 1 for a rowid table. In other words, nPkField is the
1717 ** number of fields in the true primary key of the table. */
1718 if( HasRowid(pTab
) ){
1722 pPk
= sqlite3PrimaryKeyIndex(pTab
);
1723 nPkField
= pPk
->nKeyCol
;
1726 /* Record that this module has started */
1727 VdbeModuleComment((v
, "BEGIN: GenCnstCks(%d,%d,%d,%d,%d)",
1728 iDataCur
, iIdxCur
, regNewData
, regOldData
, pkChng
));
1730 /* Test all NOT NULL constraints.
1732 if( pTab
->tabFlags
& TF_HasNotNull
){
1733 int b2ndPass
= 0; /* True if currently running 2nd pass */
1734 int nSeenReplace
= 0; /* Number of ON CONFLICT REPLACE operations */
1735 int nGenerated
= 0; /* Number of generated columns with NOT NULL */
1736 while(1){ /* Make 2 passes over columns. Exit loop via "break" */
1737 for(i
=0; i
<nCol
; i
++){
1738 int iReg
; /* Register holding column value */
1739 Column
*pCol
= &pTab
->aCol
[i
]; /* The column to check for NOT NULL */
1740 int isGenerated
; /* non-zero if column is generated */
1741 onError
= pCol
->notNull
;
1742 if( onError
==OE_None
) continue; /* No NOT NULL on this column */
1743 if( i
==pTab
->iPKey
){
1744 continue; /* ROWID is never NULL */
1746 isGenerated
= pCol
->colFlags
& COLFLAG_GENERATED
;
1747 if( isGenerated
&& !b2ndPass
){
1749 continue; /* Generated columns processed on 2nd pass */
1751 if( aiChng
&& aiChng
[i
]<0 && !isGenerated
){
1752 /* Do not check NOT NULL on columns that do not change */
1755 if( overrideError
!=OE_Default
){
1756 onError
= overrideError
;
1757 }else if( onError
==OE_Default
){
1760 if( onError
==OE_Replace
){
1761 if( b2ndPass
/* REPLACE becomes ABORT on the 2nd pass */
1762 || pCol
->iDflt
==0 /* REPLACE is ABORT if no DEFAULT value */
1764 testcase( pCol
->colFlags
& COLFLAG_VIRTUAL
);
1765 testcase( pCol
->colFlags
& COLFLAG_STORED
);
1766 testcase( pCol
->colFlags
& COLFLAG_GENERATED
);
1769 assert( !isGenerated
);
1771 }else if( b2ndPass
&& !isGenerated
){
1774 assert( onError
==OE_Rollback
|| onError
==OE_Abort
|| onError
==OE_Fail
1775 || onError
==OE_Ignore
|| onError
==OE_Replace
);
1776 testcase( i
!=sqlite3TableColumnToStorage(pTab
, i
) );
1777 iReg
= sqlite3TableColumnToStorage(pTab
, i
) + regNewData
+ 1;
1780 int addr1
= sqlite3VdbeAddOp1(v
, OP_NotNull
, iReg
);
1782 assert( (pCol
->colFlags
& COLFLAG_GENERATED
)==0 );
1784 sqlite3ExprCodeCopy(pParse
,
1785 sqlite3ColumnExpr(pTab
, pCol
), iReg
);
1786 sqlite3VdbeJumpHere(v
, addr1
);
1790 sqlite3MayAbort(pParse
);
1791 /* no break */ deliberate_fall_through
1794 char *zMsg
= sqlite3MPrintf(db
, "%s.%s", pTab
->zName
,
1796 sqlite3VdbeAddOp3(v
, OP_HaltIfNull
, SQLITE_CONSTRAINT_NOTNULL
,
1798 sqlite3VdbeAppendP4(v
, zMsg
, P4_DYNAMIC
);
1799 sqlite3VdbeChangeP5(v
, P5_ConstraintNotNull
);
1804 assert( onError
==OE_Ignore
);
1805 sqlite3VdbeAddOp2(v
, OP_IsNull
, iReg
, ignoreDest
);
1809 } /* end switch(onError) */
1810 } /* end loop i over columns */
1811 if( nGenerated
==0 && nSeenReplace
==0 ){
1812 /* If there are no generated columns with NOT NULL constraints
1813 ** and no NOT NULL ON CONFLICT REPLACE constraints, then a single
1814 ** pass is sufficient */
1817 if( b2ndPass
) break; /* Never need more than 2 passes */
1819 #ifndef SQLITE_OMIT_GENERATED_COLUMNS
1820 if( nSeenReplace
>0 && (pTab
->tabFlags
& TF_HasGenerated
)!=0 ){
1821 /* If any NOT NULL ON CONFLICT REPLACE constraints fired on the
1822 ** first pass, recomputed values for all generated columns, as
1823 ** those values might depend on columns affected by the REPLACE.
1825 sqlite3ComputeGeneratedColumns(pParse
, regNewData
+1, pTab
);
1828 } /* end of 2-pass loop */
1829 } /* end if( has-not-null-constraints ) */
1831 /* Test all CHECK constraints
1833 #ifndef SQLITE_OMIT_CHECK
1834 if( pTab
->pCheck
&& (db
->flags
& SQLITE_IgnoreChecks
)==0 ){
1835 ExprList
*pCheck
= pTab
->pCheck
;
1836 pParse
->iSelfTab
= -(regNewData
+1);
1837 onError
= overrideError
!=OE_Default
? overrideError
: OE_Abort
;
1838 for(i
=0; i
<pCheck
->nExpr
; i
++){
1841 Expr
*pExpr
= pCheck
->a
[i
].pExpr
;
1843 && !sqlite3ExprReferencesUpdatedColumn(pExpr
, aiChng
, pkChng
)
1845 /* The check constraints do not reference any of the columns being
1846 ** updated so there is no point it verifying the check constraint */
1849 if( bAffinityDone
==0 ){
1850 sqlite3TableAffinity(v
, pTab
, regNewData
+1);
1853 allOk
= sqlite3VdbeMakeLabel(pParse
);
1854 sqlite3VdbeVerifyAbortable(v
, onError
);
1855 pCopy
= sqlite3ExprDup(db
, pExpr
, 0);
1856 if( !db
->mallocFailed
){
1857 sqlite3ExprIfTrue(pParse
, pCopy
, allOk
, SQLITE_JUMPIFNULL
);
1859 sqlite3ExprDelete(db
, pCopy
);
1860 if( onError
==OE_Ignore
){
1861 sqlite3VdbeGoto(v
, ignoreDest
);
1863 char *zName
= pCheck
->a
[i
].zEName
;
1864 assert( zName
!=0 || pParse
->db
->mallocFailed
);
1865 if( onError
==OE_Replace
) onError
= OE_Abort
; /* IMP: R-26383-51744 */
1866 sqlite3HaltConstraint(pParse
, SQLITE_CONSTRAINT_CHECK
,
1867 onError
, zName
, P4_TRANSIENT
,
1868 P5_ConstraintCheck
);
1870 sqlite3VdbeResolveLabel(v
, allOk
);
1872 pParse
->iSelfTab
= 0;
1874 #endif /* !defined(SQLITE_OMIT_CHECK) */
1876 /* UNIQUE and PRIMARY KEY constraints should be handled in the following
1880 ** (2) OE_Abort, OE_Fail, OE_Rollback, OE_Ignore
1883 ** OE_Fail and OE_Ignore must happen before any changes are made.
1884 ** OE_Update guarantees that only a single row will change, so it
1885 ** must happen before OE_Replace. Technically, OE_Abort and OE_Rollback
1886 ** could happen in any order, but they are grouped up front for
1889 ** 2018-08-14: Ticket https://www.sqlite.org/src/info/908f001483982c43
1890 ** The order of constraints used to have OE_Update as (2) and OE_Abort
1891 ** and so forth as (1). But apparently PostgreSQL checks the OE_Update
1892 ** constraint before any others, so it had to be moved.
1894 ** Constraint checking code is generated in this order:
1895 ** (A) The rowid constraint
1896 ** (B) Unique index constraints that do not have OE_Replace as their
1897 ** default conflict resolution strategy
1898 ** (C) Unique index that do use OE_Replace by default.
1900 ** The ordering of (2) and (3) is accomplished by making sure the linked
1901 ** list of indexes attached to a table puts all OE_Replace indexes last
1902 ** in the list. See sqlite3CreateIndex() for where that happens.
1906 sIdxIter
.u
.ax
.aIdx
= 0; /* Silence harmless compiler warning */
1907 sIdxIter
.u
.lx
.pIdx
= pTab
->pIndex
;
1909 if( pUpsert
->pUpsertTarget
==0 ){
1910 /* There is just on ON CONFLICT clause and it has no constraint-target */
1911 assert( pUpsert
->pNextUpsert
==0 );
1912 if( pUpsert
->isDoUpdate
==0 ){
1913 /* A single ON CONFLICT DO NOTHING clause, without a constraint-target.
1914 ** Make all unique constraint resolution be OE_Ignore */
1915 overrideError
= OE_Ignore
;
1918 /* A single ON CONFLICT DO UPDATE. Make all resolutions OE_Update */
1919 overrideError
= OE_Update
;
1921 }else if( pTab
->pIndex
!=0 ){
1922 /* Otherwise, we'll need to run the IndexListTerm array version of the
1923 ** iterator to ensure that all of the ON CONFLICT conditions are
1924 ** checked first and in order. */
1929 for(nIdx
=0, pIdx
=pTab
->pIndex
; pIdx
; pIdx
=pIdx
->pNext
, nIdx
++){
1930 assert( aRegIdx
[nIdx
]>0 );
1933 sIdxIter
.u
.ax
.nIdx
= nIdx
;
1934 nByte
= (sizeof(IndexListTerm
)+1)*nIdx
+ nIdx
;
1935 sIdxIter
.u
.ax
.aIdx
= sqlite3DbMallocZero(db
, nByte
);
1936 if( sIdxIter
.u
.ax
.aIdx
==0 ) return; /* OOM */
1937 bUsed
= (u8
*)&sIdxIter
.u
.ax
.aIdx
[nIdx
];
1938 pUpsert
->pToFree
= sIdxIter
.u
.ax
.aIdx
;
1939 for(i
=0, pTerm
=pUpsert
; pTerm
; pTerm
=pTerm
->pNextUpsert
){
1940 if( pTerm
->pUpsertTarget
==0 ) break;
1941 if( pTerm
->pUpsertIdx
==0 ) continue; /* Skip ON CONFLICT for the IPK */
1943 pIdx
= pTab
->pIndex
;
1944 while( ALWAYS(pIdx
!=0) && pIdx
!=pTerm
->pUpsertIdx
){
1948 if( bUsed
[jj
] ) continue; /* Duplicate ON CONFLICT clause ignored */
1950 sIdxIter
.u
.ax
.aIdx
[i
].p
= pIdx
;
1951 sIdxIter
.u
.ax
.aIdx
[i
].ix
= jj
;
1954 for(jj
=0, pIdx
=pTab
->pIndex
; pIdx
; pIdx
=pIdx
->pNext
, jj
++){
1955 if( bUsed
[jj
] ) continue;
1956 sIdxIter
.u
.ax
.aIdx
[i
].p
= pIdx
;
1957 sIdxIter
.u
.ax
.aIdx
[i
].ix
= jj
;
1964 /* Determine if it is possible that triggers (either explicitly coded
1965 ** triggers or FK resolution actions) might run as a result of deletes
1966 ** that happen when OE_Replace conflict resolution occurs. (Call these
1967 ** "replace triggers".) If any replace triggers run, we will need to
1968 ** recheck all of the uniqueness constraints after they have all run.
1969 ** But on the recheck, the resolution is OE_Abort instead of OE_Replace.
1971 ** If replace triggers are a possibility, then
1973 ** (1) Allocate register regTrigCnt and initialize it to zero.
1974 ** That register will count the number of replace triggers that
1975 ** fire. Constraint recheck only occurs if the number is positive.
1976 ** (2) Initialize pTrigger to the list of all DELETE triggers on pTab.
1977 ** (3) Initialize addrRecheck and lblRecheckOk
1979 ** The uniqueness rechecking code will create a series of tests to run
1980 ** in a second pass. The addrRecheck and lblRecheckOk variables are
1981 ** used to link together these tests which are separated from each other
1982 ** in the generate bytecode.
1984 if( (db
->flags
& (SQLITE_RecTriggers
|SQLITE_ForeignKeys
))==0 ){
1985 /* There are not DELETE triggers nor FK constraints. No constraint
1986 ** rechecks are needed. */
1990 if( db
->flags
&SQLITE_RecTriggers
){
1991 pTrigger
= sqlite3TriggersExist(pParse
, pTab
, TK_DELETE
, 0, 0);
1992 regTrigCnt
= pTrigger
!=0 || sqlite3FkRequired(pParse
, pTab
, 0, 0);
1995 regTrigCnt
= sqlite3FkRequired(pParse
, pTab
, 0, 0);
1998 /* Replace triggers might exist. Allocate the counter and
1999 ** initialize it to zero. */
2000 regTrigCnt
= ++pParse
->nMem
;
2001 sqlite3VdbeAddOp2(v
, OP_Integer
, 0, regTrigCnt
);
2002 VdbeComment((v
, "trigger count"));
2003 lblRecheckOk
= sqlite3VdbeMakeLabel(pParse
);
2004 addrRecheck
= lblRecheckOk
;
2008 /* If rowid is changing, make sure the new rowid does not previously
2009 ** exist in the table.
2011 if( pkChng
&& pPk
==0 ){
2012 int addrRowidOk
= sqlite3VdbeMakeLabel(pParse
);
2014 /* Figure out what action to take in case of a rowid collision */
2015 onError
= pTab
->keyConf
;
2016 if( overrideError
!=OE_Default
){
2017 onError
= overrideError
;
2018 }else if( onError
==OE_Default
){
2022 /* figure out whether or not upsert applies in this case */
2024 pUpsertClause
= sqlite3UpsertOfIndex(pUpsert
,0);
2025 if( pUpsertClause
!=0 ){
2026 if( pUpsertClause
->isDoUpdate
==0 ){
2027 onError
= OE_Ignore
; /* DO NOTHING is the same as INSERT OR IGNORE */
2029 onError
= OE_Update
; /* DO UPDATE */
2032 if( pUpsertClause
!=pUpsert
){
2033 /* The first ON CONFLICT clause has a conflict target other than
2034 ** the IPK. We have to jump ahead to that first ON CONFLICT clause
2035 ** and then come back here and deal with the IPK afterwards */
2036 upsertIpkDelay
= sqlite3VdbeAddOp0(v
, OP_Goto
);
2040 /* If the response to a rowid conflict is REPLACE but the response
2041 ** to some other UNIQUE constraint is FAIL or IGNORE, then we need
2042 ** to defer the running of the rowid conflict checking until after
2043 ** the UNIQUE constraints have run.
2045 if( onError
==OE_Replace
/* IPK rule is REPLACE */
2046 && onError
!=overrideError
/* Rules for other constraints are different */
2047 && pTab
->pIndex
/* There exist other constraints */
2048 && !upsertIpkDelay
/* IPK check already deferred by UPSERT */
2050 ipkTop
= sqlite3VdbeAddOp0(v
, OP_Goto
)+1;
2051 VdbeComment((v
, "defer IPK REPLACE until last"));
2055 /* pkChng!=0 does not mean that the rowid has changed, only that
2056 ** it might have changed. Skip the conflict logic below if the rowid
2058 sqlite3VdbeAddOp3(v
, OP_Eq
, regNewData
, addrRowidOk
, regOldData
);
2059 sqlite3VdbeChangeP5(v
, SQLITE_NOTNULL
);
2063 /* Check to see if the new rowid already exists in the table. Skip
2064 ** the following conflict logic if it does not. */
2065 VdbeNoopComment((v
, "uniqueness check for ROWID"));
2066 sqlite3VdbeVerifyAbortable(v
, onError
);
2067 sqlite3VdbeAddOp3(v
, OP_NotExists
, iDataCur
, addrRowidOk
, regNewData
);
2073 /* no break */ deliberate_fall_through
2078 testcase( onError
==OE_Rollback
);
2079 testcase( onError
==OE_Abort
);
2080 testcase( onError
==OE_Fail
);
2081 sqlite3RowidConstraint(pParse
, onError
, pTab
);
2085 /* If there are DELETE triggers on this table and the
2086 ** recursive-triggers flag is set, call GenerateRowDelete() to
2087 ** remove the conflicting row from the table. This will fire
2088 ** the triggers and remove both the table and index b-tree entries.
2090 ** Otherwise, if there are no triggers or the recursive-triggers
2091 ** flag is not set, but the table has one or more indexes, call
2092 ** GenerateRowIndexDelete(). This removes the index b-tree entries
2093 ** only. The table b-tree entry will be replaced by the new entry
2094 ** when it is inserted.
2096 ** If either GenerateRowDelete() or GenerateRowIndexDelete() is called,
2097 ** also invoke MultiWrite() to indicate that this VDBE may require
2098 ** statement rollback (if the statement is aborted after the delete
2099 ** takes place). Earlier versions called sqlite3MultiWrite() regardless,
2100 ** but being more selective here allows statements like:
2102 ** REPLACE INTO t(rowid) VALUES($newrowid)
2104 ** to run without a statement journal if there are no indexes on the
2108 sqlite3MultiWrite(pParse
);
2109 sqlite3GenerateRowDelete(pParse
, pTab
, pTrigger
, iDataCur
, iIdxCur
,
2110 regNewData
, 1, 0, OE_Replace
, 1, -1);
2111 sqlite3VdbeAddOp2(v
, OP_AddImm
, regTrigCnt
, 1); /* incr trigger cnt */
2114 #ifdef SQLITE_ENABLE_PREUPDATE_HOOK
2115 assert( HasRowid(pTab
) );
2116 /* This OP_Delete opcode fires the pre-update-hook only. It does
2117 ** not modify the b-tree. It is more efficient to let the coming
2118 ** OP_Insert replace the existing entry than it is to delete the
2119 ** existing entry and then insert a new one. */
2120 sqlite3VdbeAddOp2(v
, OP_Delete
, iDataCur
, OPFLAG_ISNOOP
);
2121 sqlite3VdbeAppendP4(v
, pTab
, P4_TABLE
);
2122 #endif /* SQLITE_ENABLE_PREUPDATE_HOOK */
2124 sqlite3MultiWrite(pParse
);
2125 sqlite3GenerateRowIndexDelete(pParse
, pTab
, iDataCur
, iIdxCur
,0,-1);
2131 #ifndef SQLITE_OMIT_UPSERT
2133 sqlite3UpsertDoUpdate(pParse
, pUpsert
, pTab
, 0, iDataCur
);
2134 /* no break */ deliberate_fall_through
2138 testcase( onError
==OE_Ignore
);
2139 sqlite3VdbeGoto(v
, ignoreDest
);
2143 sqlite3VdbeResolveLabel(v
, addrRowidOk
);
2144 if( pUpsert
&& pUpsertClause
!=pUpsert
){
2145 upsertIpkReturn
= sqlite3VdbeAddOp0(v
, OP_Goto
);
2147 ipkBottom
= sqlite3VdbeAddOp0(v
, OP_Goto
);
2148 sqlite3VdbeJumpHere(v
, ipkTop
-1);
2152 /* Test all UNIQUE constraints by creating entries for each UNIQUE
2153 ** index and making sure that duplicate entries do not already exist.
2154 ** Compute the revised record entries for indices as we go.
2156 ** This loop also handles the case of the PRIMARY KEY index for a
2157 ** WITHOUT ROWID table.
2159 for(pIdx
= indexIteratorFirst(&sIdxIter
, &ix
);
2161 pIdx
= indexIteratorNext(&sIdxIter
, &ix
)
2163 int regIdx
; /* Range of registers hold conent for pIdx */
2164 int regR
; /* Range of registers holding conflicting PK */
2165 int iThisCur
; /* Cursor for this UNIQUE index */
2166 int addrUniqueOk
; /* Jump here if the UNIQUE constraint is satisfied */
2167 int addrConflictCk
; /* First opcode in the conflict check logic */
2169 if( aRegIdx
[ix
]==0 ) continue; /* Skip indices that do not change */
2171 pUpsertClause
= sqlite3UpsertOfIndex(pUpsert
, pIdx
);
2172 if( upsertIpkDelay
&& pUpsertClause
==pUpsert
){
2173 sqlite3VdbeJumpHere(v
, upsertIpkDelay
);
2176 addrUniqueOk
= sqlite3VdbeMakeLabel(pParse
);
2177 if( bAffinityDone
==0 ){
2178 sqlite3TableAffinity(v
, pTab
, regNewData
+1);
2181 VdbeNoopComment((v
, "prep index %s", pIdx
->zName
));
2182 iThisCur
= iIdxCur
+ix
;
2185 /* Skip partial indices for which the WHERE clause is not true */
2186 if( pIdx
->pPartIdxWhere
){
2187 sqlite3VdbeAddOp2(v
, OP_Null
, 0, aRegIdx
[ix
]);
2188 pParse
->iSelfTab
= -(regNewData
+1);
2189 sqlite3ExprIfFalseDup(pParse
, pIdx
->pPartIdxWhere
, addrUniqueOk
,
2191 pParse
->iSelfTab
= 0;
2194 /* Create a record for this index entry as it should appear after
2195 ** the insert or update. Store that record in the aRegIdx[ix] register
2197 regIdx
= aRegIdx
[ix
]+1;
2198 for(i
=0; i
<pIdx
->nColumn
; i
++){
2199 int iField
= pIdx
->aiColumn
[i
];
2201 if( iField
==XN_EXPR
){
2202 pParse
->iSelfTab
= -(regNewData
+1);
2203 sqlite3ExprCodeCopy(pParse
, pIdx
->aColExpr
->a
[i
].pExpr
, regIdx
+i
);
2204 pParse
->iSelfTab
= 0;
2205 VdbeComment((v
, "%s column %d", pIdx
->zName
, i
));
2206 }else if( iField
==XN_ROWID
|| iField
==pTab
->iPKey
){
2208 sqlite3VdbeAddOp2(v
, OP_IntCopy
, x
, regIdx
+i
);
2209 VdbeComment((v
, "rowid"));
2211 testcase( sqlite3TableColumnToStorage(pTab
, iField
)!=iField
);
2212 x
= sqlite3TableColumnToStorage(pTab
, iField
) + regNewData
+ 1;
2213 sqlite3VdbeAddOp2(v
, OP_SCopy
, x
, regIdx
+i
);
2214 VdbeComment((v
, "%s", pTab
->aCol
[iField
].zCnName
));
2217 sqlite3VdbeAddOp3(v
, OP_MakeRecord
, regIdx
, pIdx
->nColumn
, aRegIdx
[ix
]);
2218 VdbeComment((v
, "for %s", pIdx
->zName
));
2219 #ifdef SQLITE_ENABLE_NULL_TRIM
2220 if( pIdx
->idxType
==SQLITE_IDXTYPE_PRIMARYKEY
){
2221 sqlite3SetMakeRecordP5(v
, pIdx
->pTable
);
2224 sqlite3VdbeReleaseRegisters(pParse
, regIdx
, pIdx
->nColumn
, 0, 0);
2226 /* In an UPDATE operation, if this index is the PRIMARY KEY index
2227 ** of a WITHOUT ROWID table and there has been no change the
2228 ** primary key, then no collision is possible. The collision detection
2229 ** logic below can all be skipped. */
2230 if( isUpdate
&& pPk
==pIdx
&& pkChng
==0 ){
2231 sqlite3VdbeResolveLabel(v
, addrUniqueOk
);
2235 /* Find out what action to take in case there is a uniqueness conflict */
2236 onError
= pIdx
->onError
;
2237 if( onError
==OE_None
){
2238 sqlite3VdbeResolveLabel(v
, addrUniqueOk
);
2239 continue; /* pIdx is not a UNIQUE index */
2241 if( overrideError
!=OE_Default
){
2242 onError
= overrideError
;
2243 }else if( onError
==OE_Default
){
2247 /* Figure out if the upsert clause applies to this index */
2248 if( pUpsertClause
){
2249 if( pUpsertClause
->isDoUpdate
==0 ){
2250 onError
= OE_Ignore
; /* DO NOTHING is the same as INSERT OR IGNORE */
2252 onError
= OE_Update
; /* DO UPDATE */
2256 /* Collision detection may be omitted if all of the following are true:
2257 ** (1) The conflict resolution algorithm is REPLACE
2258 ** (2) The table is a WITHOUT ROWID table
2259 ** (3) There are no secondary indexes on the table
2260 ** (4) No delete triggers need to be fired if there is a conflict
2261 ** (5) No FK constraint counters need to be updated if a conflict occurs.
2263 ** This is not possible for ENABLE_PREUPDATE_HOOK builds, as the row
2264 ** must be explicitly deleted in order to ensure any pre-update hook
2266 assert( IsOrdinaryTable(pTab
) );
2267 #ifndef SQLITE_ENABLE_PREUPDATE_HOOK
2268 if( (ix
==0 && pIdx
->pNext
==0) /* Condition 3 */
2269 && pPk
==pIdx
/* Condition 2 */
2270 && onError
==OE_Replace
/* Condition 1 */
2271 && ( 0==(db
->flags
&SQLITE_RecTriggers
) || /* Condition 4 */
2272 0==sqlite3TriggersExist(pParse
, pTab
, TK_DELETE
, 0, 0))
2273 && ( 0==(db
->flags
&SQLITE_ForeignKeys
) || /* Condition 5 */
2274 (0==pTab
->u
.tab
.pFKey
&& 0==sqlite3FkReferences(pTab
)))
2276 sqlite3VdbeResolveLabel(v
, addrUniqueOk
);
2279 #endif /* ifndef SQLITE_ENABLE_PREUPDATE_HOOK */
2281 /* Check to see if the new index entry will be unique */
2282 sqlite3VdbeVerifyAbortable(v
, onError
);
2284 sqlite3VdbeAddOp4Int(v
, OP_NoConflict
, iThisCur
, addrUniqueOk
,
2285 regIdx
, pIdx
->nKeyCol
); VdbeCoverage(v
);
2287 /* Generate code to handle collisions */
2288 regR
= pIdx
==pPk
? regIdx
: sqlite3GetTempRange(pParse
, nPkField
);
2289 if( isUpdate
|| onError
==OE_Replace
){
2290 if( HasRowid(pTab
) ){
2291 sqlite3VdbeAddOp2(v
, OP_IdxRowid
, iThisCur
, regR
);
2292 /* Conflict only if the rowid of the existing index entry
2293 ** is different from old-rowid */
2295 sqlite3VdbeAddOp3(v
, OP_Eq
, regR
, addrUniqueOk
, regOldData
);
2296 sqlite3VdbeChangeP5(v
, SQLITE_NOTNULL
);
2301 /* Extract the PRIMARY KEY from the end of the index entry and
2302 ** store it in registers regR..regR+nPk-1 */
2304 for(i
=0; i
<pPk
->nKeyCol
; i
++){
2305 assert( pPk
->aiColumn
[i
]>=0 );
2306 x
= sqlite3TableColumnToIndex(pIdx
, pPk
->aiColumn
[i
]);
2307 sqlite3VdbeAddOp3(v
, OP_Column
, iThisCur
, x
, regR
+i
);
2308 VdbeComment((v
, "%s.%s", pTab
->zName
,
2309 pTab
->aCol
[pPk
->aiColumn
[i
]].zCnName
));
2313 /* If currently processing the PRIMARY KEY of a WITHOUT ROWID
2314 ** table, only conflict if the new PRIMARY KEY values are actually
2315 ** different from the old. See TH3 withoutrowid04.test.
2317 ** For a UNIQUE index, only conflict if the PRIMARY KEY values
2318 ** of the matched index row are different from the original PRIMARY
2319 ** KEY values of this row before the update. */
2320 int addrJump
= sqlite3VdbeCurrentAddr(v
)+pPk
->nKeyCol
;
2322 int regCmp
= (IsPrimaryKeyIndex(pIdx
) ? regIdx
: regR
);
2324 for(i
=0; i
<pPk
->nKeyCol
; i
++){
2325 char *p4
= (char*)sqlite3LocateCollSeq(pParse
, pPk
->azColl
[i
]);
2326 x
= pPk
->aiColumn
[i
];
2328 if( i
==(pPk
->nKeyCol
-1) ){
2329 addrJump
= addrUniqueOk
;
2332 x
= sqlite3TableColumnToStorage(pTab
, x
);
2333 sqlite3VdbeAddOp4(v
, op
,
2334 regOldData
+1+x
, addrJump
, regCmp
+i
, p4
, P4_COLLSEQ
2336 sqlite3VdbeChangeP5(v
, SQLITE_NOTNULL
);
2337 VdbeCoverageIf(v
, op
==OP_Eq
);
2338 VdbeCoverageIf(v
, op
==OP_Ne
);
2344 /* Generate code that executes if the new index entry is not unique */
2345 assert( onError
==OE_Rollback
|| onError
==OE_Abort
|| onError
==OE_Fail
2346 || onError
==OE_Ignore
|| onError
==OE_Replace
|| onError
==OE_Update
);
2351 testcase( onError
==OE_Rollback
);
2352 testcase( onError
==OE_Abort
);
2353 testcase( onError
==OE_Fail
);
2354 sqlite3UniqueConstraint(pParse
, onError
, pIdx
);
2357 #ifndef SQLITE_OMIT_UPSERT
2359 sqlite3UpsertDoUpdate(pParse
, pUpsert
, pTab
, pIdx
, iIdxCur
+ix
);
2360 /* no break */ deliberate_fall_through
2364 testcase( onError
==OE_Ignore
);
2365 sqlite3VdbeGoto(v
, ignoreDest
);
2369 int nConflictCk
; /* Number of opcodes in conflict check logic */
2371 assert( onError
==OE_Replace
);
2372 nConflictCk
= sqlite3VdbeCurrentAddr(v
) - addrConflictCk
;
2373 assert( nConflictCk
>0 || db
->mallocFailed
);
2374 testcase( nConflictCk
<=0 );
2375 testcase( nConflictCk
>1 );
2377 sqlite3MultiWrite(pParse
);
2380 if( pTrigger
&& isUpdate
){
2381 sqlite3VdbeAddOp1(v
, OP_CursorLock
, iDataCur
);
2383 sqlite3GenerateRowDelete(pParse
, pTab
, pTrigger
, iDataCur
, iIdxCur
,
2384 regR
, nPkField
, 0, OE_Replace
,
2385 (pIdx
==pPk
? ONEPASS_SINGLE
: ONEPASS_OFF
), iThisCur
);
2386 if( pTrigger
&& isUpdate
){
2387 sqlite3VdbeAddOp1(v
, OP_CursorUnlock
, iDataCur
);
2390 int addrBypass
; /* Jump destination to bypass recheck logic */
2392 sqlite3VdbeAddOp2(v
, OP_AddImm
, regTrigCnt
, 1); /* incr trigger cnt */
2393 addrBypass
= sqlite3VdbeAddOp0(v
, OP_Goto
); /* Bypass recheck */
2394 VdbeComment((v
, "bypass recheck"));
2396 /* Here we insert code that will be invoked after all constraint
2397 ** checks have run, if and only if one or more replace triggers
2399 sqlite3VdbeResolveLabel(v
, lblRecheckOk
);
2400 lblRecheckOk
= sqlite3VdbeMakeLabel(pParse
);
2401 if( pIdx
->pPartIdxWhere
){
2402 /* Bypass the recheck if this partial index is not defined
2403 ** for the current row */
2404 sqlite3VdbeAddOp2(v
, OP_IsNull
, regIdx
-1, lblRecheckOk
);
2407 /* Copy the constraint check code from above, except change
2408 ** the constraint-ok jump destination to be the address of
2409 ** the next retest block */
2410 while( nConflictCk
>0 ){
2411 VdbeOp x
; /* Conflict check opcode to copy */
2412 /* The sqlite3VdbeAddOp4() call might reallocate the opcode array.
2413 ** Hence, make a complete copy of the opcode, rather than using
2414 ** a pointer to the opcode. */
2415 x
= *sqlite3VdbeGetOp(v
, addrConflictCk
);
2416 if( x
.opcode
!=OP_IdxRowid
){
2417 int p2
; /* New P2 value for copied conflict check opcode */
2419 if( sqlite3OpcodeProperty
[x
.opcode
]&OPFLG_JUMP
){
2424 zP4
= x
.p4type
==P4_INT32
? SQLITE_INT_TO_PTR(x
.p4
.i
) : x
.p4
.z
;
2425 sqlite3VdbeAddOp4(v
, x
.opcode
, x
.p1
, p2
, x
.p3
, zP4
, x
.p4type
);
2426 sqlite3VdbeChangeP5(v
, x
.p5
);
2427 VdbeCoverageIf(v
, p2
!=x
.p2
);
2432 /* If the retest fails, issue an abort */
2433 sqlite3UniqueConstraint(pParse
, OE_Abort
, pIdx
);
2435 sqlite3VdbeJumpHere(v
, addrBypass
); /* Terminate the recheck bypass */
2441 sqlite3VdbeResolveLabel(v
, addrUniqueOk
);
2442 if( regR
!=regIdx
) sqlite3ReleaseTempRange(pParse
, regR
, nPkField
);
2445 && sqlite3UpsertNextIsIPK(pUpsertClause
)
2447 sqlite3VdbeGoto(v
, upsertIpkDelay
+1);
2448 sqlite3VdbeJumpHere(v
, upsertIpkReturn
);
2449 upsertIpkReturn
= 0;
2453 /* If the IPK constraint is a REPLACE, run it last */
2455 sqlite3VdbeGoto(v
, ipkTop
);
2456 VdbeComment((v
, "Do IPK REPLACE"));
2457 assert( ipkBottom
>0 );
2458 sqlite3VdbeJumpHere(v
, ipkBottom
);
2461 /* Recheck all uniqueness constraints after replace triggers have run */
2462 testcase( regTrigCnt
!=0 && nReplaceTrig
==0 );
2463 assert( regTrigCnt
!=0 || nReplaceTrig
==0 );
2465 sqlite3VdbeAddOp2(v
, OP_IfNot
, regTrigCnt
, lblRecheckOk
);VdbeCoverage(v
);
2468 sqlite3VdbeAddOp3(v
, OP_Eq
, regNewData
, addrRecheck
, regOldData
);
2469 sqlite3VdbeChangeP5(v
, SQLITE_NOTNULL
);
2472 sqlite3VdbeAddOp3(v
, OP_NotExists
, iDataCur
, addrRecheck
, regNewData
);
2474 sqlite3RowidConstraint(pParse
, OE_Abort
, pTab
);
2476 sqlite3VdbeGoto(v
, addrRecheck
);
2478 sqlite3VdbeResolveLabel(v
, lblRecheckOk
);
2481 /* Generate the table record */
2482 if( HasRowid(pTab
) ){
2483 int regRec
= aRegIdx
[ix
];
2484 sqlite3VdbeAddOp3(v
, OP_MakeRecord
, regNewData
+1, pTab
->nNVCol
, regRec
);
2485 sqlite3SetMakeRecordP5(v
, pTab
);
2486 if( !bAffinityDone
){
2487 sqlite3TableAffinity(v
, pTab
, 0);
2491 *pbMayReplace
= seenReplace
;
2492 VdbeModuleComment((v
, "END: GenCnstCks(%d)", seenReplace
));
2495 #ifdef SQLITE_ENABLE_NULL_TRIM
2497 ** Change the P5 operand on the last opcode (which should be an OP_MakeRecord)
2498 ** to be the number of columns in table pTab that must not be NULL-trimmed.
2500 ** Or if no columns of pTab may be NULL-trimmed, leave P5 at zero.
2502 void sqlite3SetMakeRecordP5(Vdbe
*v
, Table
*pTab
){
2505 /* Records with omitted columns are only allowed for schema format
2506 ** version 2 and later (SQLite version 3.1.4, 2005-02-20). */
2507 if( pTab
->pSchema
->file_format
<2 ) return;
2509 for(i
=pTab
->nCol
-1; i
>0; i
--){
2510 if( pTab
->aCol
[i
].iDflt
!=0 ) break;
2511 if( pTab
->aCol
[i
].colFlags
& COLFLAG_PRIMKEY
) break;
2513 sqlite3VdbeChangeP5(v
, i
+1);
2518 ** Table pTab is a WITHOUT ROWID table that is being written to. The cursor
2519 ** number is iCur, and register regData contains the new record for the
2520 ** PK index. This function adds code to invoke the pre-update hook,
2521 ** if one is registered.
2523 #ifdef SQLITE_ENABLE_PREUPDATE_HOOK
2524 static void codeWithoutRowidPreupdate(
2525 Parse
*pParse
, /* Parse context */
2526 Table
*pTab
, /* Table being updated */
2527 int iCur
, /* Cursor number for table */
2528 int regData
/* Data containing new record */
2530 Vdbe
*v
= pParse
->pVdbe
;
2531 int r
= sqlite3GetTempReg(pParse
);
2532 assert( !HasRowid(pTab
) );
2533 assert( 0==(pParse
->db
->mDbFlags
& DBFLAG_Vacuum
) || CORRUPT_DB
);
2534 sqlite3VdbeAddOp2(v
, OP_Integer
, 0, r
);
2535 sqlite3VdbeAddOp4(v
, OP_Insert
, iCur
, regData
, r
, (char*)pTab
, P4_TABLE
);
2536 sqlite3VdbeChangeP5(v
, OPFLAG_ISNOOP
);
2537 sqlite3ReleaseTempReg(pParse
, r
);
2540 # define codeWithoutRowidPreupdate(a,b,c,d)
2544 ** This routine generates code to finish the INSERT or UPDATE operation
2545 ** that was started by a prior call to sqlite3GenerateConstraintChecks.
2546 ** A consecutive range of registers starting at regNewData contains the
2547 ** rowid and the content to be inserted.
2549 ** The arguments to this routine should be the same as the first six
2550 ** arguments to sqlite3GenerateConstraintChecks.
2552 void sqlite3CompleteInsertion(
2553 Parse
*pParse
, /* The parser context */
2554 Table
*pTab
, /* the table into which we are inserting */
2555 int iDataCur
, /* Cursor of the canonical data source */
2556 int iIdxCur
, /* First index cursor */
2557 int regNewData
, /* Range of content */
2558 int *aRegIdx
, /* Register used by each index. 0 for unused indices */
2559 int update_flags
, /* True for UPDATE, False for INSERT */
2560 int appendBias
, /* True if this is likely to be an append */
2561 int useSeekResult
/* True to set the USESEEKRESULT flag on OP_[Idx]Insert */
2563 Vdbe
*v
; /* Prepared statements under construction */
2564 Index
*pIdx
; /* An index being inserted or updated */
2565 u8 pik_flags
; /* flag values passed to the btree insert */
2566 int i
; /* Loop counter */
2568 assert( update_flags
==0
2569 || update_flags
==OPFLAG_ISUPDATE
2570 || update_flags
==(OPFLAG_ISUPDATE
|OPFLAG_SAVEPOSITION
)
2575 assert( !IsView(pTab
) ); /* This table is not a VIEW */
2576 for(i
=0, pIdx
=pTab
->pIndex
; pIdx
; pIdx
=pIdx
->pNext
, i
++){
2577 /* All REPLACE indexes are at the end of the list */
2578 assert( pIdx
->onError
!=OE_Replace
2580 || pIdx
->pNext
->onError
==OE_Replace
);
2581 if( aRegIdx
[i
]==0 ) continue;
2582 if( pIdx
->pPartIdxWhere
){
2583 sqlite3VdbeAddOp2(v
, OP_IsNull
, aRegIdx
[i
], sqlite3VdbeCurrentAddr(v
)+2);
2586 pik_flags
= (useSeekResult
? OPFLAG_USESEEKRESULT
: 0);
2587 if( IsPrimaryKeyIndex(pIdx
) && !HasRowid(pTab
) ){
2588 pik_flags
|= OPFLAG_NCHANGE
;
2589 pik_flags
|= (update_flags
& OPFLAG_SAVEPOSITION
);
2590 if( update_flags
==0 ){
2591 codeWithoutRowidPreupdate(pParse
, pTab
, iIdxCur
+i
, aRegIdx
[i
]);
2594 sqlite3VdbeAddOp4Int(v
, OP_IdxInsert
, iIdxCur
+i
, aRegIdx
[i
],
2596 pIdx
->uniqNotNull
? pIdx
->nKeyCol
: pIdx
->nColumn
);
2597 sqlite3VdbeChangeP5(v
, pik_flags
);
2599 if( !HasRowid(pTab
) ) return;
2600 if( pParse
->nested
){
2603 pik_flags
= OPFLAG_NCHANGE
;
2604 pik_flags
|= (update_flags
?update_flags
:OPFLAG_LASTROWID
);
2607 pik_flags
|= OPFLAG_APPEND
;
2609 if( useSeekResult
){
2610 pik_flags
|= OPFLAG_USESEEKRESULT
;
2612 sqlite3VdbeAddOp3(v
, OP_Insert
, iDataCur
, aRegIdx
[i
], regNewData
);
2613 if( !pParse
->nested
){
2614 sqlite3VdbeAppendP4(v
, pTab
, P4_TABLE
);
2616 sqlite3VdbeChangeP5(v
, pik_flags
);
2620 ** Allocate cursors for the pTab table and all its indices and generate
2621 ** code to open and initialized those cursors.
2623 ** The cursor for the object that contains the complete data (normally
2624 ** the table itself, but the PRIMARY KEY index in the case of a WITHOUT
2625 ** ROWID table) is returned in *piDataCur. The first index cursor is
2626 ** returned in *piIdxCur. The number of indices is returned.
2628 ** Use iBase as the first cursor (either the *piDataCur for rowid tables
2629 ** or the first index for WITHOUT ROWID tables) if it is non-negative.
2630 ** If iBase is negative, then allocate the next available cursor.
2632 ** For a rowid table, *piDataCur will be exactly one less than *piIdxCur.
2633 ** For a WITHOUT ROWID table, *piDataCur will be somewhere in the range
2634 ** of *piIdxCurs, depending on where the PRIMARY KEY index appears on the
2635 ** pTab->pIndex list.
2637 ** If pTab is a virtual table, then this routine is a no-op and the
2638 ** *piDataCur and *piIdxCur values are left uninitialized.
2640 int sqlite3OpenTableAndIndices(
2641 Parse
*pParse
, /* Parsing context */
2642 Table
*pTab
, /* Table to be opened */
2643 int op
, /* OP_OpenRead or OP_OpenWrite */
2644 u8 p5
, /* P5 value for OP_Open* opcodes (except on WITHOUT ROWID) */
2645 int iBase
, /* Use this for the table cursor, if there is one */
2646 u8
*aToOpen
, /* If not NULL: boolean for each table and index */
2647 int *piDataCur
, /* Write the database source cursor number here */
2648 int *piIdxCur
/* Write the first index cursor number here */
2656 assert( op
==OP_OpenRead
|| op
==OP_OpenWrite
);
2657 assert( op
==OP_OpenWrite
|| p5
==0 );
2658 if( IsVirtual(pTab
) ){
2659 /* This routine is a no-op for virtual tables. Leave the output
2660 ** variables *piDataCur and *piIdxCur set to illegal cursor numbers
2661 ** for improved error detection. */
2662 *piDataCur
= *piIdxCur
= -999;
2665 iDb
= sqlite3SchemaToIndex(pParse
->db
, pTab
->pSchema
);
2668 if( iBase
<0 ) iBase
= pParse
->nTab
;
2670 if( piDataCur
) *piDataCur
= iDataCur
;
2671 if( HasRowid(pTab
) && (aToOpen
==0 || aToOpen
[0]) ){
2672 sqlite3OpenTable(pParse
, iDataCur
, iDb
, pTab
, op
);
2674 sqlite3TableLock(pParse
, iDb
, pTab
->tnum
, op
==OP_OpenWrite
, pTab
->zName
);
2676 if( piIdxCur
) *piIdxCur
= iBase
;
2677 for(i
=0, pIdx
=pTab
->pIndex
; pIdx
; pIdx
=pIdx
->pNext
, i
++){
2678 int iIdxCur
= iBase
++;
2679 assert( pIdx
->pSchema
==pTab
->pSchema
);
2680 if( IsPrimaryKeyIndex(pIdx
) && !HasRowid(pTab
) ){
2681 if( piDataCur
) *piDataCur
= iIdxCur
;
2684 if( aToOpen
==0 || aToOpen
[i
+1] ){
2685 sqlite3VdbeAddOp3(v
, op
, iIdxCur
, pIdx
->tnum
, iDb
);
2686 sqlite3VdbeSetP4KeyInfo(pParse
, pIdx
);
2687 sqlite3VdbeChangeP5(v
, p5
);
2688 VdbeComment((v
, "%s", pIdx
->zName
));
2691 if( iBase
>pParse
->nTab
) pParse
->nTab
= iBase
;
2698 ** The following global variable is incremented whenever the
2699 ** transfer optimization is used. This is used for testing
2700 ** purposes only - to make sure the transfer optimization really
2701 ** is happening when it is supposed to.
2703 int sqlite3_xferopt_count
;
2704 #endif /* SQLITE_TEST */
2707 #ifndef SQLITE_OMIT_XFER_OPT
2709 ** Check to see if index pSrc is compatible as a source of data
2710 ** for index pDest in an insert transfer optimization. The rules
2711 ** for a compatible index:
2713 ** * The index is over the same set of columns
2714 ** * The same DESC and ASC markings occurs on all columns
2715 ** * The same onError processing (OE_Abort, OE_Ignore, etc)
2716 ** * The same collating sequence on each column
2717 ** * The index has the exact same WHERE clause
2719 static int xferCompatibleIndex(Index
*pDest
, Index
*pSrc
){
2721 assert( pDest
&& pSrc
);
2722 assert( pDest
->pTable
!=pSrc
->pTable
);
2723 if( pDest
->nKeyCol
!=pSrc
->nKeyCol
|| pDest
->nColumn
!=pSrc
->nColumn
){
2724 return 0; /* Different number of columns */
2726 if( pDest
->onError
!=pSrc
->onError
){
2727 return 0; /* Different conflict resolution strategies */
2729 for(i
=0; i
<pSrc
->nKeyCol
; i
++){
2730 if( pSrc
->aiColumn
[i
]!=pDest
->aiColumn
[i
] ){
2731 return 0; /* Different columns indexed */
2733 if( pSrc
->aiColumn
[i
]==XN_EXPR
){
2734 assert( pSrc
->aColExpr
!=0 && pDest
->aColExpr
!=0 );
2735 if( sqlite3ExprCompare(0, pSrc
->aColExpr
->a
[i
].pExpr
,
2736 pDest
->aColExpr
->a
[i
].pExpr
, -1)!=0 ){
2737 return 0; /* Different expressions in the index */
2740 if( pSrc
->aSortOrder
[i
]!=pDest
->aSortOrder
[i
] ){
2741 return 0; /* Different sort orders */
2743 if( sqlite3_stricmp(pSrc
->azColl
[i
],pDest
->azColl
[i
])!=0 ){
2744 return 0; /* Different collating sequences */
2747 if( sqlite3ExprCompare(0, pSrc
->pPartIdxWhere
, pDest
->pPartIdxWhere
, -1) ){
2748 return 0; /* Different WHERE clauses */
2751 /* If no test above fails then the indices must be compatible */
2756 ** Attempt the transfer optimization on INSERTs of the form
2758 ** INSERT INTO tab1 SELECT * FROM tab2;
2760 ** The xfer optimization transfers raw records from tab2 over to tab1.
2761 ** Columns are not decoded and reassembled, which greatly improves
2762 ** performance. Raw index records are transferred in the same way.
2764 ** The xfer optimization is only attempted if tab1 and tab2 are compatible.
2765 ** There are lots of rules for determining compatibility - see comments
2766 ** embedded in the code for details.
2768 ** This routine returns TRUE if the optimization is guaranteed to be used.
2769 ** Sometimes the xfer optimization will only work if the destination table
2770 ** is empty - a factor that can only be determined at run-time. In that
2771 ** case, this routine generates code for the xfer optimization but also
2772 ** does a test to see if the destination table is empty and jumps over the
2773 ** xfer optimization code if the test fails. In that case, this routine
2774 ** returns FALSE so that the caller will know to go ahead and generate
2775 ** an unoptimized transfer. This routine also returns FALSE if there
2776 ** is no chance that the xfer optimization can be applied.
2778 ** This optimization is particularly useful at making VACUUM run faster.
2780 static int xferOptimization(
2781 Parse
*pParse
, /* Parser context */
2782 Table
*pDest
, /* The table we are inserting into */
2783 Select
*pSelect
, /* A SELECT statement to use as the data source */
2784 int onError
, /* How to handle constraint errors */
2785 int iDbDest
/* The database of pDest */
2787 sqlite3
*db
= pParse
->db
;
2788 ExprList
*pEList
; /* The result set of the SELECT */
2789 Table
*pSrc
; /* The table in the FROM clause of SELECT */
2790 Index
*pSrcIdx
, *pDestIdx
; /* Source and destination indices */
2791 SrcItem
*pItem
; /* An element of pSelect->pSrc */
2792 int i
; /* Loop counter */
2793 int iDbSrc
; /* The database of pSrc */
2794 int iSrc
, iDest
; /* Cursors from source and destination */
2795 int addr1
, addr2
; /* Loop addresses */
2796 int emptyDestTest
= 0; /* Address of test for empty pDest */
2797 int emptySrcTest
= 0; /* Address of test for empty pSrc */
2798 Vdbe
*v
; /* The VDBE we are building */
2799 int regAutoinc
; /* Memory register used by AUTOINC */
2800 int destHasUniqueIdx
= 0; /* True if pDest has a UNIQUE index */
2801 int regData
, regRowid
; /* Registers holding data and rowid */
2803 assert( pSelect
!=0 );
2804 if( pParse
->pWith
|| pSelect
->pWith
){
2805 /* Do not attempt to process this query if there are an WITH clauses
2806 ** attached to it. Proceeding may generate a false "no such table: xxx"
2807 ** error if pSelect reads from a CTE named "xxx". */
2810 #ifndef SQLITE_OMIT_VIRTUALTABLE
2811 if( IsVirtual(pDest
) ){
2812 return 0; /* tab1 must not be a virtual table */
2815 if( onError
==OE_Default
){
2816 if( pDest
->iPKey
>=0 ) onError
= pDest
->keyConf
;
2817 if( onError
==OE_Default
) onError
= OE_Abort
;
2819 assert(pSelect
->pSrc
); /* allocated even if there is no FROM clause */
2820 if( pSelect
->pSrc
->nSrc
!=1 ){
2821 return 0; /* FROM clause must have exactly one term */
2823 if( pSelect
->pSrc
->a
[0].pSelect
){
2824 return 0; /* FROM clause cannot contain a subquery */
2826 if( pSelect
->pWhere
){
2827 return 0; /* SELECT may not have a WHERE clause */
2829 if( pSelect
->pOrderBy
){
2830 return 0; /* SELECT may not have an ORDER BY clause */
2832 /* Do not need to test for a HAVING clause. If HAVING is present but
2833 ** there is no ORDER BY, we will get an error. */
2834 if( pSelect
->pGroupBy
){
2835 return 0; /* SELECT may not have a GROUP BY clause */
2837 if( pSelect
->pLimit
){
2838 return 0; /* SELECT may not have a LIMIT clause */
2840 if( pSelect
->pPrior
){
2841 return 0; /* SELECT may not be a compound query */
2843 if( pSelect
->selFlags
& SF_Distinct
){
2844 return 0; /* SELECT may not be DISTINCT */
2846 pEList
= pSelect
->pEList
;
2847 assert( pEList
!=0 );
2848 if( pEList
->nExpr
!=1 ){
2849 return 0; /* The result set must have exactly one column */
2851 assert( pEList
->a
[0].pExpr
);
2852 if( pEList
->a
[0].pExpr
->op
!=TK_ASTERISK
){
2853 return 0; /* The result set must be the special operator "*" */
2856 /* At this point we have established that the statement is of the
2857 ** correct syntactic form to participate in this optimization. Now
2858 ** we have to check the semantics.
2860 pItem
= pSelect
->pSrc
->a
;
2861 pSrc
= sqlite3LocateTableItem(pParse
, 0, pItem
);
2863 return 0; /* FROM clause does not contain a real table */
2865 if( pSrc
->tnum
==pDest
->tnum
&& pSrc
->pSchema
==pDest
->pSchema
){
2866 testcase( pSrc
!=pDest
); /* Possible due to bad sqlite_schema.rootpage */
2867 return 0; /* tab1 and tab2 may not be the same table */
2869 if( HasRowid(pDest
)!=HasRowid(pSrc
) ){
2870 return 0; /* source and destination must both be WITHOUT ROWID or not */
2872 if( !IsOrdinaryTable(pSrc
) ){
2873 return 0; /* tab2 may not be a view or virtual table */
2875 if( pDest
->nCol
!=pSrc
->nCol
){
2876 return 0; /* Number of columns must be the same in tab1 and tab2 */
2878 if( pDest
->iPKey
!=pSrc
->iPKey
){
2879 return 0; /* Both tables must have the same INTEGER PRIMARY KEY */
2881 if( (pDest
->tabFlags
& TF_Strict
)!=0 && (pSrc
->tabFlags
& TF_Strict
)==0 ){
2882 return 0; /* Cannot feed from a non-strict into a strict table */
2884 for(i
=0; i
<pDest
->nCol
; i
++){
2885 Column
*pDestCol
= &pDest
->aCol
[i
];
2886 Column
*pSrcCol
= &pSrc
->aCol
[i
];
2887 #ifdef SQLITE_ENABLE_HIDDEN_COLUMNS
2888 if( (db
->mDbFlags
& DBFLAG_Vacuum
)==0
2889 && (pDestCol
->colFlags
| pSrcCol
->colFlags
) & COLFLAG_HIDDEN
2891 return 0; /* Neither table may have __hidden__ columns */
2894 #ifndef SQLITE_OMIT_GENERATED_COLUMNS
2895 /* Even if tables t1 and t2 have identical schemas, if they contain
2896 ** generated columns, then this statement is semantically incorrect:
2898 ** INSERT INTO t2 SELECT * FROM t1;
2900 ** The reason is that generated column values are returned by the
2901 ** the SELECT statement on the right but the INSERT statement on the
2902 ** left wants them to be omitted.
2904 ** Nevertheless, this is a useful notational shorthand to tell SQLite
2905 ** to do a bulk transfer all of the content from t1 over to t2.
2907 ** We could, in theory, disable this (except for internal use by the
2908 ** VACUUM command where it is actually needed). But why do that? It
2909 ** seems harmless enough, and provides a useful service.
2911 if( (pDestCol
->colFlags
& COLFLAG_GENERATED
) !=
2912 (pSrcCol
->colFlags
& COLFLAG_GENERATED
) ){
2913 return 0; /* Both columns have the same generated-column type */
2915 /* But the transfer is only allowed if both the source and destination
2916 ** tables have the exact same expressions for generated columns.
2917 ** This requirement could be relaxed for VIRTUAL columns, I suppose.
2919 if( (pDestCol
->colFlags
& COLFLAG_GENERATED
)!=0 ){
2920 if( sqlite3ExprCompare(0,
2921 sqlite3ColumnExpr(pSrc
, pSrcCol
),
2922 sqlite3ColumnExpr(pDest
, pDestCol
), -1)!=0 ){
2923 testcase( pDestCol
->colFlags
& COLFLAG_VIRTUAL
);
2924 testcase( pDestCol
->colFlags
& COLFLAG_STORED
);
2925 return 0; /* Different generator expressions */
2929 if( pDestCol
->affinity
!=pSrcCol
->affinity
){
2930 return 0; /* Affinity must be the same on all columns */
2932 if( sqlite3_stricmp(sqlite3ColumnColl(pDestCol
),
2933 sqlite3ColumnColl(pSrcCol
))!=0 ){
2934 return 0; /* Collating sequence must be the same on all columns */
2936 if( pDestCol
->notNull
&& !pSrcCol
->notNull
){
2937 return 0; /* tab2 must be NOT NULL if tab1 is */
2939 /* Default values for second and subsequent columns need to match. */
2940 if( (pDestCol
->colFlags
& COLFLAG_GENERATED
)==0 && i
>0 ){
2941 Expr
*pDestExpr
= sqlite3ColumnExpr(pDest
, pDestCol
);
2942 Expr
*pSrcExpr
= sqlite3ColumnExpr(pSrc
, pSrcCol
);
2943 assert( pDestExpr
==0 || pDestExpr
->op
==TK_SPAN
);
2944 assert( pDestExpr
==0 || !ExprHasProperty(pDestExpr
, EP_IntValue
) );
2945 assert( pSrcExpr
==0 || pSrcExpr
->op
==TK_SPAN
);
2946 assert( pSrcExpr
==0 || !ExprHasProperty(pSrcExpr
, EP_IntValue
) );
2947 if( (pDestExpr
==0)!=(pSrcExpr
==0)
2948 || (pDestExpr
!=0 && strcmp(pDestExpr
->u
.zToken
,
2949 pSrcExpr
->u
.zToken
)!=0)
2951 return 0; /* Default values must be the same for all columns */
2955 for(pDestIdx
=pDest
->pIndex
; pDestIdx
; pDestIdx
=pDestIdx
->pNext
){
2956 if( IsUniqueIndex(pDestIdx
) ){
2957 destHasUniqueIdx
= 1;
2959 for(pSrcIdx
=pSrc
->pIndex
; pSrcIdx
; pSrcIdx
=pSrcIdx
->pNext
){
2960 if( xferCompatibleIndex(pDestIdx
, pSrcIdx
) ) break;
2963 return 0; /* pDestIdx has no corresponding index in pSrc */
2965 if( pSrcIdx
->tnum
==pDestIdx
->tnum
&& pSrc
->pSchema
==pDest
->pSchema
2966 && sqlite3FaultSim(411)==SQLITE_OK
){
2967 /* The sqlite3FaultSim() call allows this corruption test to be
2968 ** bypassed during testing, in order to exercise other corruption tests
2969 ** further downstream. */
2970 return 0; /* Corrupt schema - two indexes on the same btree */
2973 #ifndef SQLITE_OMIT_CHECK
2974 if( pDest
->pCheck
&& sqlite3ExprListCompare(pSrc
->pCheck
,pDest
->pCheck
,-1) ){
2975 return 0; /* Tables have different CHECK constraints. Ticket #2252 */
2978 #ifndef SQLITE_OMIT_FOREIGN_KEY
2979 /* Disallow the transfer optimization if the destination table constains
2980 ** any foreign key constraints. This is more restrictive than necessary.
2981 ** But the main beneficiary of the transfer optimization is the VACUUM
2982 ** command, and the VACUUM command disables foreign key constraints. So
2983 ** the extra complication to make this rule less restrictive is probably
2984 ** not worth the effort. Ticket [6284df89debdfa61db8073e062908af0c9b6118e]
2986 assert( IsOrdinaryTable(pDest
) );
2987 if( (db
->flags
& SQLITE_ForeignKeys
)!=0 && pDest
->u
.tab
.pFKey
!=0 ){
2991 if( (db
->flags
& SQLITE_CountRows
)!=0 ){
2992 return 0; /* xfer opt does not play well with PRAGMA count_changes */
2995 /* If we get this far, it means that the xfer optimization is at
2996 ** least a possibility, though it might only work if the destination
2997 ** table (tab1) is initially empty.
3000 sqlite3_xferopt_count
++;
3002 iDbSrc
= sqlite3SchemaToIndex(db
, pSrc
->pSchema
);
3003 v
= sqlite3GetVdbe(pParse
);
3004 sqlite3CodeVerifySchema(pParse
, iDbSrc
);
3005 iSrc
= pParse
->nTab
++;
3006 iDest
= pParse
->nTab
++;
3007 regAutoinc
= autoIncBegin(pParse
, iDbDest
, pDest
);
3008 regData
= sqlite3GetTempReg(pParse
);
3009 sqlite3VdbeAddOp2(v
, OP_Null
, 0, regData
);
3010 regRowid
= sqlite3GetTempReg(pParse
);
3011 sqlite3OpenTable(pParse
, iDest
, iDbDest
, pDest
, OP_OpenWrite
);
3012 assert( HasRowid(pDest
) || destHasUniqueIdx
);
3013 if( (db
->mDbFlags
& DBFLAG_Vacuum
)==0 && (
3014 (pDest
->iPKey
<0 && pDest
->pIndex
!=0) /* (1) */
3015 || destHasUniqueIdx
/* (2) */
3016 || (onError
!=OE_Abort
&& onError
!=OE_Rollback
) /* (3) */
3018 /* In some circumstances, we are able to run the xfer optimization
3019 ** only if the destination table is initially empty. Unless the
3020 ** DBFLAG_Vacuum flag is set, this block generates code to make
3021 ** that determination. If DBFLAG_Vacuum is set, then the destination
3022 ** table is always empty.
3024 ** Conditions under which the destination must be empty:
3026 ** (1) There is no INTEGER PRIMARY KEY but there are indices.
3027 ** (If the destination is not initially empty, the rowid fields
3028 ** of index entries might need to change.)
3030 ** (2) The destination has a unique index. (The xfer optimization
3031 ** is unable to test uniqueness.)
3033 ** (3) onError is something other than OE_Abort and OE_Rollback.
3035 addr1
= sqlite3VdbeAddOp2(v
, OP_Rewind
, iDest
, 0); VdbeCoverage(v
);
3036 emptyDestTest
= sqlite3VdbeAddOp0(v
, OP_Goto
);
3037 sqlite3VdbeJumpHere(v
, addr1
);
3039 if( HasRowid(pSrc
) ){
3041 sqlite3OpenTable(pParse
, iSrc
, iDbSrc
, pSrc
, OP_OpenRead
);
3042 emptySrcTest
= sqlite3VdbeAddOp2(v
, OP_Rewind
, iSrc
, 0); VdbeCoverage(v
);
3043 if( pDest
->iPKey
>=0 ){
3044 addr1
= sqlite3VdbeAddOp2(v
, OP_Rowid
, iSrc
, regRowid
);
3045 if( (db
->mDbFlags
& DBFLAG_Vacuum
)==0 ){
3046 sqlite3VdbeVerifyAbortable(v
, onError
);
3047 addr2
= sqlite3VdbeAddOp3(v
, OP_NotExists
, iDest
, 0, regRowid
);
3049 sqlite3RowidConstraint(pParse
, onError
, pDest
);
3050 sqlite3VdbeJumpHere(v
, addr2
);
3052 autoIncStep(pParse
, regAutoinc
, regRowid
);
3053 }else if( pDest
->pIndex
==0 && !(db
->mDbFlags
& DBFLAG_VacuumInto
) ){
3054 addr1
= sqlite3VdbeAddOp2(v
, OP_NewRowid
, iDest
, regRowid
);
3056 addr1
= sqlite3VdbeAddOp2(v
, OP_Rowid
, iSrc
, regRowid
);
3057 assert( (pDest
->tabFlags
& TF_Autoincrement
)==0 );
3060 if( db
->mDbFlags
& DBFLAG_Vacuum
){
3061 sqlite3VdbeAddOp1(v
, OP_SeekEnd
, iDest
);
3062 insFlags
= OPFLAG_APPEND
|OPFLAG_USESEEKRESULT
|OPFLAG_PREFORMAT
;
3064 insFlags
= OPFLAG_NCHANGE
|OPFLAG_LASTROWID
|OPFLAG_APPEND
|OPFLAG_PREFORMAT
;
3066 #ifdef SQLITE_ENABLE_PREUPDATE_HOOK
3067 if( (db
->mDbFlags
& DBFLAG_Vacuum
)==0 ){
3068 sqlite3VdbeAddOp3(v
, OP_RowData
, iSrc
, regData
, 1);
3069 insFlags
&= ~OPFLAG_PREFORMAT
;
3073 sqlite3VdbeAddOp3(v
, OP_RowCell
, iDest
, iSrc
, regRowid
);
3075 sqlite3VdbeAddOp3(v
, OP_Insert
, iDest
, regData
, regRowid
);
3076 if( (db
->mDbFlags
& DBFLAG_Vacuum
)==0 ){
3077 sqlite3VdbeChangeP4(v
, -1, (char*)pDest
, P4_TABLE
);
3079 sqlite3VdbeChangeP5(v
, insFlags
);
3081 sqlite3VdbeAddOp2(v
, OP_Next
, iSrc
, addr1
); VdbeCoverage(v
);
3082 sqlite3VdbeAddOp2(v
, OP_Close
, iSrc
, 0);
3083 sqlite3VdbeAddOp2(v
, OP_Close
, iDest
, 0);
3085 sqlite3TableLock(pParse
, iDbDest
, pDest
->tnum
, 1, pDest
->zName
);
3086 sqlite3TableLock(pParse
, iDbSrc
, pSrc
->tnum
, 0, pSrc
->zName
);
3088 for(pDestIdx
=pDest
->pIndex
; pDestIdx
; pDestIdx
=pDestIdx
->pNext
){
3090 for(pSrcIdx
=pSrc
->pIndex
; ALWAYS(pSrcIdx
); pSrcIdx
=pSrcIdx
->pNext
){
3091 if( xferCompatibleIndex(pDestIdx
, pSrcIdx
) ) break;
3094 sqlite3VdbeAddOp3(v
, OP_OpenRead
, iSrc
, pSrcIdx
->tnum
, iDbSrc
);
3095 sqlite3VdbeSetP4KeyInfo(pParse
, pSrcIdx
);
3096 VdbeComment((v
, "%s", pSrcIdx
->zName
));
3097 sqlite3VdbeAddOp3(v
, OP_OpenWrite
, iDest
, pDestIdx
->tnum
, iDbDest
);
3098 sqlite3VdbeSetP4KeyInfo(pParse
, pDestIdx
);
3099 sqlite3VdbeChangeP5(v
, OPFLAG_BULKCSR
);
3100 VdbeComment((v
, "%s", pDestIdx
->zName
));
3101 addr1
= sqlite3VdbeAddOp2(v
, OP_Rewind
, iSrc
, 0); VdbeCoverage(v
);
3102 if( db
->mDbFlags
& DBFLAG_Vacuum
){
3103 /* This INSERT command is part of a VACUUM operation, which guarantees
3104 ** that the destination table is empty. If all indexed columns use
3105 ** collation sequence BINARY, then it can also be assumed that the
3106 ** index will be populated by inserting keys in strictly sorted
3107 ** order. In this case, instead of seeking within the b-tree as part
3108 ** of every OP_IdxInsert opcode, an OP_SeekEnd is added before the
3109 ** OP_IdxInsert to seek to the point within the b-tree where each key
3110 ** should be inserted. This is faster.
3112 ** If any of the indexed columns use a collation sequence other than
3113 ** BINARY, this optimization is disabled. This is because the user
3114 ** might change the definition of a collation sequence and then run
3115 ** a VACUUM command. In that case keys may not be written in strictly
3117 for(i
=0; i
<pSrcIdx
->nColumn
; i
++){
3118 const char *zColl
= pSrcIdx
->azColl
[i
];
3119 if( sqlite3_stricmp(sqlite3StrBINARY
, zColl
) ) break;
3121 if( i
==pSrcIdx
->nColumn
){
3122 idxInsFlags
= OPFLAG_USESEEKRESULT
|OPFLAG_PREFORMAT
;
3123 sqlite3VdbeAddOp1(v
, OP_SeekEnd
, iDest
);
3124 sqlite3VdbeAddOp2(v
, OP_RowCell
, iDest
, iSrc
);
3126 }else if( !HasRowid(pSrc
) && pDestIdx
->idxType
==SQLITE_IDXTYPE_PRIMARYKEY
){
3127 idxInsFlags
|= OPFLAG_NCHANGE
;
3129 if( idxInsFlags
!=(OPFLAG_USESEEKRESULT
|OPFLAG_PREFORMAT
) ){
3130 sqlite3VdbeAddOp3(v
, OP_RowData
, iSrc
, regData
, 1);
3131 if( (db
->mDbFlags
& DBFLAG_Vacuum
)==0
3133 && IsPrimaryKeyIndex(pDestIdx
)
3135 codeWithoutRowidPreupdate(pParse
, pDest
, iDest
, regData
);
3138 sqlite3VdbeAddOp2(v
, OP_IdxInsert
, iDest
, regData
);
3139 sqlite3VdbeChangeP5(v
, idxInsFlags
|OPFLAG_APPEND
);
3140 sqlite3VdbeAddOp2(v
, OP_Next
, iSrc
, addr1
+1); VdbeCoverage(v
);
3141 sqlite3VdbeJumpHere(v
, addr1
);
3142 sqlite3VdbeAddOp2(v
, OP_Close
, iSrc
, 0);
3143 sqlite3VdbeAddOp2(v
, OP_Close
, iDest
, 0);
3145 if( emptySrcTest
) sqlite3VdbeJumpHere(v
, emptySrcTest
);
3146 sqlite3ReleaseTempReg(pParse
, regRowid
);
3147 sqlite3ReleaseTempReg(pParse
, regData
);
3148 if( emptyDestTest
){
3149 sqlite3AutoincrementEnd(pParse
);
3150 sqlite3VdbeAddOp2(v
, OP_Halt
, SQLITE_OK
, 0);
3151 sqlite3VdbeJumpHere(v
, emptyDestTest
);
3152 sqlite3VdbeAddOp2(v
, OP_Close
, iDest
, 0);
3158 #endif /* SQLITE_OMIT_XFER_OPT */