drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 1 | /* |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 2 | ** 2001 September 15 |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 3 | ** |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 4 | ** The author disclaims copyright to this source code. In place of |
| 5 | ** a legal notice, here is a blessing: |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 6 | ** |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 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. |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 10 | ** |
| 11 | ************************************************************************* |
| 12 | ** This file contains C code routines that are called by the parser |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 13 | ** to handle INSERT statements in SQLite. |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 14 | */ |
| 15 | #include "sqliteInt.h" |
| 16 | |
| 17 | /* |
drh | 26198bb | 2013-10-31 11:15:09 +0000 | [diff] [blame] | 18 | ** Generate code that will |
drh | dd9930e | 2013-10-23 23:37:02 +0000 | [diff] [blame] | 19 | ** |
drh | 26198bb | 2013-10-31 11:15:09 +0000 | [diff] [blame] | 20 | ** (1) acquire a lock for table pTab then |
| 21 | ** (2) open pTab as cursor iCur. |
| 22 | ** |
| 23 | ** If pTab is a WITHOUT ROWID table, then it is the PRIMARY KEY index |
| 24 | ** for that table that is actually opened. |
drh | bbb5e4e | 2009-04-30 00:11:09 +0000 | [diff] [blame] | 25 | */ |
| 26 | void sqlite3OpenTable( |
drh | 2ec2fb2 | 2013-11-06 19:59:23 +0000 | [diff] [blame] | 27 | Parse *pParse, /* Generate code into this VDBE */ |
drh | bbb5e4e | 2009-04-30 00:11:09 +0000 | [diff] [blame] | 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 */ |
| 32 | ){ |
| 33 | Vdbe *v; |
drh | 5f53aac | 2012-12-03 19:42:39 +0000 | [diff] [blame] | 34 | assert( !IsVirtual(pTab) ); |
drh | 289a0c8 | 2020-08-15 22:23:00 +0000 | [diff] [blame] | 35 | assert( pParse->pVdbe!=0 ); |
| 36 | v = pParse->pVdbe; |
drh | bbb5e4e | 2009-04-30 00:11:09 +0000 | [diff] [blame] | 37 | assert( opcode==OP_OpenWrite || opcode==OP_OpenRead ); |
drh | 2ec2fb2 | 2013-11-06 19:59:23 +0000 | [diff] [blame] | 38 | sqlite3TableLock(pParse, iDb, pTab->tnum, |
| 39 | (opcode==OP_OpenWrite)?1:0, pTab->zName); |
drh | ec95c44 | 2013-10-23 01:57:32 +0000 | [diff] [blame] | 40 | if( HasRowid(pTab) ){ |
drh | 0b0b3a9 | 2019-10-17 18:35:57 +0000 | [diff] [blame] | 41 | sqlite3VdbeAddOp4Int(v, opcode, iCur, pTab->tnum, iDb, pTab->nNVCol); |
drh | dd9930e | 2013-10-23 23:37:02 +0000 | [diff] [blame] | 42 | VdbeComment((v, "%s", pTab->zName)); |
drh | 26198bb | 2013-10-31 11:15:09 +0000 | [diff] [blame] | 43 | }else{ |
drh | dd9930e | 2013-10-23 23:37:02 +0000 | [diff] [blame] | 44 | Index *pPk = sqlite3PrimaryKeyIndex(pTab); |
| 45 | assert( pPk!=0 ); |
drh | 3907560 | 2022-01-01 12:26:01 +0000 | [diff] [blame] | 46 | assert( pPk->tnum==pTab->tnum || CORRUPT_DB ); |
drh | 2ec2fb2 | 2013-11-06 19:59:23 +0000 | [diff] [blame] | 47 | sqlite3VdbeAddOp3(v, opcode, iCur, pPk->tnum, iDb); |
| 48 | sqlite3VdbeSetP4KeyInfo(pParse, pPk); |
drh | ec95c44 | 2013-10-23 01:57:32 +0000 | [diff] [blame] | 49 | VdbeComment((v, "%s", pTab->zName)); |
| 50 | } |
drh | bbb5e4e | 2009-04-30 00:11:09 +0000 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | /* |
dan | 69f8bb9 | 2009-08-13 19:21:16 +0000 | [diff] [blame] | 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: |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 +0000 | [diff] [blame] | 57 | ** |
| 58 | ** Character Column affinity |
| 59 | ** ------------------------------ |
drh | 05883a3 | 2015-06-02 15:32:08 +0000 | [diff] [blame] | 60 | ** 'A' BLOB |
drh | 4583c37 | 2014-09-19 20:13:25 +0000 | [diff] [blame] | 61 | ** 'B' TEXT |
| 62 | ** 'C' NUMERIC |
| 63 | ** 'D' INTEGER |
| 64 | ** 'F' REAL |
drh | 2d401ab | 2008-01-10 23:50:11 +0000 | [diff] [blame] | 65 | ** |
drh | 4583c37 | 2014-09-19 20:13:25 +0000 | [diff] [blame] | 66 | ** An extra 'D' is appended to the end of the string to cover the |
drh | 2d401ab | 2008-01-10 23:50:11 +0000 | [diff] [blame] | 67 | ** rowid that appears as the last column in every index. |
dan | 69f8bb9 | 2009-08-13 19:21:16 +0000 | [diff] [blame] | 68 | ** |
| 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. |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 +0000 | [diff] [blame] | 72 | */ |
drh | e910769 | 2015-08-25 19:20:04 +0000 | [diff] [blame] | 73 | const char *sqlite3IndexAffinityStr(sqlite3 *db, Index *pIdx){ |
danielk1977 | a37cdde | 2004-05-16 11:15:36 +0000 | [diff] [blame] | 74 | if( !pIdx->zColAff ){ |
danielk1977 | e014a83 | 2004-05-17 10:48:57 +0000 | [diff] [blame] | 75 | /* The first time a column affinity string for a particular index is |
danielk1977 | a37cdde | 2004-05-16 11:15:36 +0000 | [diff] [blame] | 76 | ** required, it is allocated and populated here. It is then stored as |
danielk1977 | e014a83 | 2004-05-17 10:48:57 +0000 | [diff] [blame] | 77 | ** a member of the Index structure for subsequent use. |
danielk1977 | a37cdde | 2004-05-16 11:15:36 +0000 | [diff] [blame] | 78 | ** |
| 79 | ** The column affinity string will eventually be deleted by |
danielk1977 | e014a83 | 2004-05-17 10:48:57 +0000 | [diff] [blame] | 80 | ** sqliteDeleteIndex() when the Index structure itself is cleaned |
danielk1977 | a37cdde | 2004-05-16 11:15:36 +0000 | [diff] [blame] | 81 | ** up. |
| 82 | */ |
| 83 | int n; |
| 84 | Table *pTab = pIdx->pTable; |
drh | ad12432 | 2013-10-23 13:30:58 +0000 | [diff] [blame] | 85 | pIdx->zColAff = (char *)sqlite3DbMallocRaw(0, pIdx->nColumn+1); |
danielk1977 | a37cdde | 2004-05-16 11:15:36 +0000 | [diff] [blame] | 86 | if( !pIdx->zColAff ){ |
drh | 4a642b6 | 2016-02-05 01:55:27 +0000 | [diff] [blame] | 87 | sqlite3OomFault(db); |
dan | 69f8bb9 | 2009-08-13 19:21:16 +0000 | [diff] [blame] | 88 | return 0; |
danielk1977 | a37cdde | 2004-05-16 11:15:36 +0000 | [diff] [blame] | 89 | } |
| 90 | for(n=0; n<pIdx->nColumn; n++){ |
drh | ad12432 | 2013-10-23 13:30:58 +0000 | [diff] [blame] | 91 | i16 x = pIdx->aiColumn[n]; |
drh | 81506b8 | 2019-08-05 19:32:06 +0000 | [diff] [blame] | 92 | char aff; |
drh | 1f9ca2c | 2015-08-25 16:57:52 +0000 | [diff] [blame] | 93 | if( x>=0 ){ |
drh | 81506b8 | 2019-08-05 19:32:06 +0000 | [diff] [blame] | 94 | aff = pTab->aCol[x].affinity; |
drh | 4b92f98 | 2015-09-29 17:20:14 +0000 | [diff] [blame] | 95 | }else if( x==XN_ROWID ){ |
drh | 81506b8 | 2019-08-05 19:32:06 +0000 | [diff] [blame] | 96 | aff = SQLITE_AFF_INTEGER; |
drh | 1f9ca2c | 2015-08-25 16:57:52 +0000 | [diff] [blame] | 97 | }else{ |
drh | 4b92f98 | 2015-09-29 17:20:14 +0000 | [diff] [blame] | 98 | assert( x==XN_EXPR ); |
drh | 4bc1cc1 | 2022-10-13 21:08:34 +0000 | [diff] [blame] | 99 | assert( pIdx->bHasExpr ); |
drh | 1f9ca2c | 2015-08-25 16:57:52 +0000 | [diff] [blame] | 100 | assert( pIdx->aColExpr!=0 ); |
drh | 6860e6f | 2015-08-27 18:24:02 +0000 | [diff] [blame] | 101 | aff = sqlite3ExprAffinity(pIdx->aColExpr->a[n].pExpr); |
drh | 1f9ca2c | 2015-08-25 16:57:52 +0000 | [diff] [blame] | 102 | } |
drh | 96fb16e | 2019-08-06 14:37:24 +0000 | [diff] [blame] | 103 | if( aff<SQLITE_AFF_BLOB ) aff = SQLITE_AFF_BLOB; |
drh | 7314495 | 2019-08-30 23:56:34 +0000 | [diff] [blame] | 104 | if( aff>SQLITE_AFF_NUMERIC) aff = SQLITE_AFF_NUMERIC; |
drh | 81506b8 | 2019-08-05 19:32:06 +0000 | [diff] [blame] | 105 | pIdx->zColAff[n] = aff; |
danielk1977 | a37cdde | 2004-05-16 11:15:36 +0000 | [diff] [blame] | 106 | } |
drh | 2d401ab | 2008-01-10 23:50:11 +0000 | [diff] [blame] | 107 | pIdx->zColAff[n] = 0; |
danielk1977 | a37cdde | 2004-05-16 11:15:36 +0000 | [diff] [blame] | 108 | } |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 +0000 | [diff] [blame] | 109 | |
dan | 69f8bb9 | 2009-08-13 19:21:16 +0000 | [diff] [blame] | 110 | return pIdx->zColAff; |
danielk1977 | a37cdde | 2004-05-16 11:15:36 +0000 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | /* |
drh | 5fdb9a3 | 2022-11-01 00:52:22 +0000 | [diff] [blame] | 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. |
| 117 | */ |
| 118 | char *sqlite3TableAffinityStr(sqlite3 *db, const Table *pTab){ |
| 119 | char *zColAff; |
| 120 | zColAff = (char *)sqlite3DbMallocRaw(db, pTab->nCol+1); |
| 121 | if( zColAff ){ |
| 122 | int i, j; |
| 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; |
| 126 | } |
| 127 | } |
| 128 | do{ |
| 129 | zColAff[j--] = 0; |
| 130 | }while( j>=0 && zColAff[j]<=SQLITE_AFF_BLOB ); |
| 131 | } |
| 132 | return zColAff; |
| 133 | } |
| 134 | |
| 135 | /* |
drh | 71c770f | 2021-08-19 16:29:33 +0000 | [diff] [blame] | 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. |
| 138 | ** |
| 139 | ** For ordinary (legacy, non-strict) tables: |
| 140 | ** ----------------------------------------- |
| 141 | ** |
drh | 57bf4a8 | 2014-02-17 14:59:22 +0000 | [diff] [blame] | 142 | ** Compute the affinity string for table pTab, if it has not already been |
drh | 05883a3 | 2015-06-02 15:32:08 +0000 | [diff] [blame] | 143 | ** computed. As an optimization, omit trailing SQLITE_AFF_BLOB affinities. |
drh | 57bf4a8 | 2014-02-17 14:59:22 +0000 | [diff] [blame] | 144 | ** |
drh | 71c770f | 2021-08-19 16:29:33 +0000 | [diff] [blame] | 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. |
| 147 | ** |
| 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, |
drh | 57bf4a8 | 2014-02-17 14:59:22 +0000 | [diff] [blame] | 150 | ** then just set the P4 operand of the previous opcode (which should be |
| 151 | ** an OP_MakeRecord) to the affinity string. |
| 152 | ** |
drh | b6e8fd1 | 2014-03-06 01:56:33 +0000 | [diff] [blame] | 153 | ** A column affinity string has one character per column: |
danielk1977 | a37cdde | 2004-05-16 11:15:36 +0000 | [diff] [blame] | 154 | ** |
drh | 71c770f | 2021-08-19 16:29:33 +0000 | [diff] [blame] | 155 | ** Character Column affinity |
| 156 | ** --------- --------------- |
| 157 | ** 'A' BLOB |
| 158 | ** 'B' TEXT |
| 159 | ** 'C' NUMERIC |
| 160 | ** 'D' INTEGER |
| 161 | ** 'E' REAL |
| 162 | ** |
| 163 | ** For STRICT tables: |
| 164 | ** ------------------ |
| 165 | ** |
| 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. |
danielk1977 | a37cdde | 2004-05-16 11:15:36 +0000 | [diff] [blame] | 174 | */ |
drh | 57bf4a8 | 2014-02-17 14:59:22 +0000 | [diff] [blame] | 175 | void sqlite3TableAffinity(Vdbe *v, Table *pTab, int iReg){ |
drh | 5fdb9a3 | 2022-11-01 00:52:22 +0000 | [diff] [blame] | 176 | int i; |
drh | 72532f5 | 2021-08-18 19:22:27 +0000 | [diff] [blame] | 177 | char *zColAff; |
| 178 | if( pTab->tabFlags & TF_Strict ){ |
| 179 | if( iReg==0 ){ |
| 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 */ |
| 183 | VdbeOp *pPrev; |
| 184 | sqlite3VdbeAppendP4(v, pTab, P4_TABLE); |
drh | 058e995 | 2022-07-25 19:05:24 +0000 | [diff] [blame] | 185 | pPrev = sqlite3VdbeGetLastOp(v); |
drh | 71c770f | 2021-08-19 16:29:33 +0000 | [diff] [blame] | 186 | assert( pPrev!=0 ); |
| 187 | assert( pPrev->opcode==OP_MakeRecord || sqlite3VdbeDb(v)->mallocFailed ); |
drh | 72532f5 | 2021-08-18 19:22:27 +0000 | [diff] [blame] | 188 | pPrev->opcode = OP_TypeCheck; |
| 189 | sqlite3VdbeAddOp3(v, OP_MakeRecord, pPrev->p1, pPrev->p2, pPrev->p3); |
| 190 | }else{ |
| 191 | /* Insert an isolated OP_Typecheck */ |
| 192 | sqlite3VdbeAddOp2(v, OP_TypeCheck, iReg, pTab->nNVCol); |
| 193 | sqlite3VdbeAppendP4(v, pTab, P4_TABLE); |
| 194 | } |
| 195 | return; |
| 196 | } |
| 197 | zColAff = pTab->zColAff; |
drh | 57bf4a8 | 2014-02-17 14:59:22 +0000 | [diff] [blame] | 198 | if( zColAff==0 ){ |
drh | 5fdb9a3 | 2022-11-01 00:52:22 +0000 | [diff] [blame] | 199 | zColAff = sqlite3TableAffinityStr(0, pTab); |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 +0000 | [diff] [blame] | 200 | if( !zColAff ){ |
drh | 5fdb9a3 | 2022-11-01 00:52:22 +0000 | [diff] [blame] | 201 | sqlite3OomFault(sqlite3VdbeDb(v)); |
danielk1977 | a37cdde | 2004-05-16 11:15:36 +0000 | [diff] [blame] | 202 | return; |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 +0000 | [diff] [blame] | 203 | } |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 +0000 | [diff] [blame] | 204 | pTab->zColAff = zColAff; |
| 205 | } |
drh | 7301e77 | 2018-10-31 20:52:00 +0000 | [diff] [blame] | 206 | assert( zColAff!=0 ); |
| 207 | i = sqlite3Strlen30NN(zColAff); |
drh | 57bf4a8 | 2014-02-17 14:59:22 +0000 | [diff] [blame] | 208 | if( i ){ |
| 209 | if( iReg ){ |
| 210 | sqlite3VdbeAddOp4(v, OP_Affinity, iReg, i, 0, zColAff, i); |
| 211 | }else{ |
drh | 058e995 | 2022-07-25 19:05:24 +0000 | [diff] [blame] | 212 | assert( sqlite3VdbeGetLastOp(v)->opcode==OP_MakeRecord |
drh | 71c770f | 2021-08-19 16:29:33 +0000 | [diff] [blame] | 213 | || sqlite3VdbeDb(v)->mallocFailed ); |
drh | 57bf4a8 | 2014-02-17 14:59:22 +0000 | [diff] [blame] | 214 | sqlite3VdbeChangeP4(v, -1, zColAff, i); |
| 215 | } |
| 216 | } |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 +0000 | [diff] [blame] | 217 | } |
| 218 | |
danielk1977 | 4d88778 | 2005-02-08 08:42:27 +0000 | [diff] [blame] | 219 | /* |
drh | 48d1178 | 2007-11-23 15:02:19 +0000 | [diff] [blame] | 220 | ** Return non-zero if the table pTab in database iDb or any of its indices |
drh | b6e8fd1 | 2014-03-06 01:56:33 +0000 | [diff] [blame] | 221 | ** have been opened at any point in the VDBE program. This is used to see if |
drh | 48d1178 | 2007-11-23 15:02:19 +0000 | [diff] [blame] | 222 | ** a statement of the form "INSERT INTO <iDb, pTab> SELECT ..." can |
drh | b6e8fd1 | 2014-03-06 01:56:33 +0000 | [diff] [blame] | 223 | ** run without using a temporary table for the results of the SELECT. |
danielk1977 | 4d88778 | 2005-02-08 08:42:27 +0000 | [diff] [blame] | 224 | */ |
drh | 05a86c5 | 2014-02-16 01:55:49 +0000 | [diff] [blame] | 225 | static int readsTable(Parse *p, int iDb, Table *pTab){ |
danielk1977 | 595a523 | 2009-07-24 17:58:53 +0000 | [diff] [blame] | 226 | Vdbe *v = sqlite3GetVdbe(p); |
danielk1977 | 4d88778 | 2005-02-08 08:42:27 +0000 | [diff] [blame] | 227 | int i; |
drh | 48d1178 | 2007-11-23 15:02:19 +0000 | [diff] [blame] | 228 | int iEnd = sqlite3VdbeCurrentAddr(v); |
danielk1977 | 595a523 | 2009-07-24 17:58:53 +0000 | [diff] [blame] | 229 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
| 230 | VTable *pVTab = IsVirtual(pTab) ? sqlite3GetVTable(p->db, pTab) : 0; |
| 231 | #endif |
| 232 | |
drh | 05a86c5 | 2014-02-16 01:55:49 +0000 | [diff] [blame] | 233 | for(i=1; i<iEnd; i++){ |
drh | 48d1178 | 2007-11-23 15:02:19 +0000 | [diff] [blame] | 234 | VdbeOp *pOp = sqlite3VdbeGetOp(v, i); |
drh | ef0bea9 | 2007-12-14 16:11:09 +0000 | [diff] [blame] | 235 | assert( pOp!=0 ); |
danielk1977 | 207872a | 2008-01-03 07:54:23 +0000 | [diff] [blame] | 236 | if( pOp->opcode==OP_OpenRead && pOp->p3==iDb ){ |
| 237 | Index *pIndex; |
drh | 8deae5a | 2020-07-29 12:23:20 +0000 | [diff] [blame] | 238 | Pgno tnum = pOp->p2; |
danielk1977 | 207872a | 2008-01-03 07:54:23 +0000 | [diff] [blame] | 239 | if( tnum==pTab->tnum ){ |
| 240 | return 1; |
| 241 | } |
| 242 | for(pIndex=pTab->pIndex; pIndex; pIndex=pIndex->pNext){ |
| 243 | if( tnum==pIndex->tnum ){ |
drh | 48d1178 | 2007-11-23 15:02:19 +0000 | [diff] [blame] | 244 | return 1; |
| 245 | } |
drh | 48d1178 | 2007-11-23 15:02:19 +0000 | [diff] [blame] | 246 | } |
| 247 | } |
drh | 543165e | 2007-11-27 14:46:41 +0000 | [diff] [blame] | 248 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
danielk1977 | 595a523 | 2009-07-24 17:58:53 +0000 | [diff] [blame] | 249 | if( pOp->opcode==OP_VOpen && pOp->p4.pVtab==pVTab ){ |
danielk1977 | 2dca4ac | 2008-01-03 11:50:29 +0000 | [diff] [blame] | 250 | assert( pOp->p4.pVtab!=0 ); |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 251 | assert( pOp->p4type==P4_VTAB ); |
drh | 48d1178 | 2007-11-23 15:02:19 +0000 | [diff] [blame] | 252 | return 1; |
danielk1977 | 4d88778 | 2005-02-08 08:42:27 +0000 | [diff] [blame] | 253 | } |
drh | 543165e | 2007-11-27 14:46:41 +0000 | [diff] [blame] | 254 | #endif |
danielk1977 | 4d88778 | 2005-02-08 08:42:27 +0000 | [diff] [blame] | 255 | } |
| 256 | return 0; |
| 257 | } |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 +0000 | [diff] [blame] | 258 | |
drh | dfa1527 | 2019-11-06 22:19:07 +0000 | [diff] [blame] | 259 | /* This walker callback will compute the union of colFlags flags for all |
drh | 7dc76d8 | 2019-11-21 20:10:31 +0000 | [diff] [blame] | 260 | ** referenced columns in a CHECK constraint or generated column expression. |
drh | dfa1527 | 2019-11-06 22:19:07 +0000 | [diff] [blame] | 261 | */ |
| 262 | static int exprColumnFlagUnion(Walker *pWalker, Expr *pExpr){ |
drh | 7dc76d8 | 2019-11-21 20:10:31 +0000 | [diff] [blame] | 263 | if( pExpr->op==TK_COLUMN && pExpr->iColumn>=0 ){ |
| 264 | assert( pExpr->iColumn < pWalker->u.pTab->nCol ); |
drh | dfa1527 | 2019-11-06 22:19:07 +0000 | [diff] [blame] | 265 | pWalker->eCode |= pWalker->u.pTab->aCol[pExpr->iColumn].colFlags; |
| 266 | } |
| 267 | return WRC_Continue; |
| 268 | } |
| 269 | |
drh | c143114 | 2019-10-17 17:54:05 +0000 | [diff] [blame] | 270 | #ifndef SQLITE_OMIT_GENERATED_COLUMNS |
| 271 | /* |
| 272 | ** All regular columns for table pTab have been puts into registers |
| 273 | ** starting with iRegStore. The registers that correspond to STORED |
drh | dd6cc9b | 2019-10-19 18:47:27 +0000 | [diff] [blame] | 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. |
drh | c143114 | 2019-10-17 17:54:05 +0000 | [diff] [blame] | 277 | */ |
drh | dd6cc9b | 2019-10-19 18:47:27 +0000 | [diff] [blame] | 278 | void sqlite3ComputeGeneratedColumns( |
drh | c143114 | 2019-10-17 17:54:05 +0000 | [diff] [blame] | 279 | Parse *pParse, /* Parsing context */ |
| 280 | int iRegStore, /* Register holding the first column */ |
| 281 | Table *pTab /* The table */ |
| 282 | ){ |
| 283 | int i; |
drh | dfa1527 | 2019-11-06 22:19:07 +0000 | [diff] [blame] | 284 | Walker w; |
| 285 | Column *pRedo; |
| 286 | int eProgress; |
drh | b5f6243 | 2019-12-10 02:48:41 +0000 | [diff] [blame] | 287 | VdbeOp *pOp; |
| 288 | |
| 289 | assert( pTab->tabFlags & TF_HasGenerated ); |
| 290 | testcase( pTab->tabFlags & TF_HasVirtual ); |
| 291 | testcase( pTab->tabFlags & TF_HasStored ); |
| 292 | |
| 293 | /* Before computing generated columns, first go through and make sure |
| 294 | ** that appropriate affinity has been applied to the regular columns |
| 295 | */ |
| 296 | sqlite3TableAffinity(pParse->pVdbe, pTab, iRegStore); |
drh | 926aac5 | 2021-11-03 14:02:48 +0000 | [diff] [blame] | 297 | if( (pTab->tabFlags & TF_HasStored)!=0 ){ |
drh | 058e995 | 2022-07-25 19:05:24 +0000 | [diff] [blame] | 298 | pOp = sqlite3VdbeGetLastOp(pParse->pVdbe); |
drh | 926aac5 | 2021-11-03 14:02:48 +0000 | [diff] [blame] | 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. */ |
| 303 | int ii, jj; |
| 304 | char *zP4 = pOp->p4.z; |
| 305 | assert( zP4!=0 ); |
| 306 | assert( pOp->p4type==P4_DYNAMIC ); |
drh | 926aac5 | 2021-11-03 14:02:48 +0000 | [diff] [blame] | 307 | for(ii=jj=0; zP4[jj]; ii++){ |
| 308 | if( pTab->aCol[ii].colFlags & COLFLAG_VIRTUAL ){ |
| 309 | continue; |
| 310 | } |
| 311 | if( pTab->aCol[ii].colFlags & COLFLAG_STORED ){ |
| 312 | zP4[jj] = SQLITE_AFF_NONE; |
| 313 | } |
| 314 | jj++; |
drh | b5f6243 | 2019-12-10 02:48:41 +0000 | [diff] [blame] | 315 | } |
drh | 926aac5 | 2021-11-03 14:02:48 +0000 | [diff] [blame] | 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 |
| 319 | ** not be checked */ |
drh | 926aac5 | 2021-11-03 14:02:48 +0000 | [diff] [blame] | 320 | pOp->p3 = 1; |
drh | b5f6243 | 2019-12-10 02:48:41 +0000 | [diff] [blame] | 321 | } |
| 322 | } |
drh | dfa1527 | 2019-11-06 22:19:07 +0000 | [diff] [blame] | 323 | |
drh | dd6cc9b | 2019-10-19 18:47:27 +0000 | [diff] [blame] | 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". |
drh | 9942ef0 | 2019-10-18 02:19:18 +0000 | [diff] [blame] | 327 | */ |
| 328 | for(i=0; i<pTab->nCol; i++){ |
drh | dd6cc9b | 2019-10-19 18:47:27 +0000 | [diff] [blame] | 329 | if( pTab->aCol[i].colFlags & COLFLAG_GENERATED ){ |
drh | ab0992f | 2019-10-23 03:53:10 +0000 | [diff] [blame] | 330 | testcase( pTab->aCol[i].colFlags & COLFLAG_VIRTUAL ); |
| 331 | testcase( pTab->aCol[i].colFlags & COLFLAG_STORED ); |
drh | 9942ef0 | 2019-10-18 02:19:18 +0000 | [diff] [blame] | 332 | pTab->aCol[i].colFlags |= COLFLAG_NOTAVAIL; |
| 333 | } |
| 334 | } |
drh | dfa1527 | 2019-11-06 22:19:07 +0000 | [diff] [blame] | 335 | |
| 336 | w.u.pTab = pTab; |
| 337 | w.xExprCallback = exprColumnFlagUnion; |
| 338 | w.xSelectCallback = 0; |
| 339 | w.xSelectCallback2 = 0; |
| 340 | |
drh | 9942ef0 | 2019-10-18 02:19:18 +0000 | [diff] [blame] | 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 |
| 344 | ** they are needed. |
| 345 | */ |
drh | c143114 | 2019-10-17 17:54:05 +0000 | [diff] [blame] | 346 | pParse->iSelfTab = -iRegStore; |
drh | dfa1527 | 2019-11-06 22:19:07 +0000 | [diff] [blame] | 347 | do{ |
| 348 | eProgress = 0; |
| 349 | pRedo = 0; |
| 350 | for(i=0; i<pTab->nCol; i++){ |
| 351 | Column *pCol = pTab->aCol + i; |
| 352 | if( (pCol->colFlags & COLFLAG_NOTAVAIL)!=0 ){ |
| 353 | int x; |
| 354 | pCol->colFlags |= COLFLAG_BUSY; |
| 355 | w.eCode = 0; |
drh | 79cf2b7 | 2021-07-31 20:30:41 +0000 | [diff] [blame] | 356 | sqlite3WalkExpr(&w, sqlite3ColumnExpr(pTab, pCol)); |
drh | dfa1527 | 2019-11-06 22:19:07 +0000 | [diff] [blame] | 357 | pCol->colFlags &= ~COLFLAG_BUSY; |
| 358 | if( w.eCode & COLFLAG_NOTAVAIL ){ |
| 359 | pRedo = pCol; |
| 360 | continue; |
| 361 | } |
| 362 | eProgress = 1; |
| 363 | assert( pCol->colFlags & COLFLAG_GENERATED ); |
| 364 | x = sqlite3TableColumnToStorage(pTab, i) + iRegStore; |
drh | 79cf2b7 | 2021-07-31 20:30:41 +0000 | [diff] [blame] | 365 | sqlite3ExprCodeGeneratedColumn(pParse, pTab, pCol, x); |
drh | dfa1527 | 2019-11-06 22:19:07 +0000 | [diff] [blame] | 366 | pCol->colFlags &= ~COLFLAG_NOTAVAIL; |
drh | dd6cc9b | 2019-10-19 18:47:27 +0000 | [diff] [blame] | 367 | } |
drh | c143114 | 2019-10-17 17:54:05 +0000 | [diff] [blame] | 368 | } |
drh | dfa1527 | 2019-11-06 22:19:07 +0000 | [diff] [blame] | 369 | }while( pRedo && eProgress ); |
| 370 | if( pRedo ){ |
drh | cf9d36d | 2021-08-02 18:03:43 +0000 | [diff] [blame] | 371 | sqlite3ErrorMsg(pParse, "generated column loop on \"%s\"", pRedo->zCnName); |
drh | c143114 | 2019-10-17 17:54:05 +0000 | [diff] [blame] | 372 | } |
| 373 | pParse->iSelfTab = 0; |
| 374 | } |
| 375 | #endif /* SQLITE_OMIT_GENERATED_COLUMNS */ |
| 376 | |
| 377 | |
drh | 9d9cf22 | 2007-02-13 15:01:11 +0000 | [diff] [blame] | 378 | #ifndef SQLITE_OMIT_AUTOINCREMENT |
| 379 | /* |
drh | 0b9f50d | 2009-06-23 20:28:53 +0000 | [diff] [blame] | 380 | ** Locate or create an AutoincInfo structure associated with table pTab |
| 381 | ** which is in database iDb. Return the register number for the register |
drh | 9ef5e77 | 2016-08-19 14:20:56 +0000 | [diff] [blame] | 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.) |
drh | 9d9cf22 | 2007-02-13 15:01:11 +0000 | [diff] [blame] | 385 | ** |
drh | 0b9f50d | 2009-06-23 20:28:53 +0000 | [diff] [blame] | 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. |
drh | 9d9cf22 | 2007-02-13 15:01:11 +0000 | [diff] [blame] | 391 | ** |
drh | c8abbc1 | 2018-03-16 18:46:30 +0000 | [diff] [blame] | 392 | ** Four consecutive registers are allocated: |
drh | 0b9f50d | 2009-06-23 20:28:53 +0000 | [diff] [blame] | 393 | ** |
drh | c8abbc1 | 2018-03-16 18:46:30 +0000 | [diff] [blame] | 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 |
drh | 0b9f50d | 2009-06-23 20:28:53 +0000 | [diff] [blame] | 398 | ** |
| 399 | ** The 2nd register is the one that is returned. That is all the |
| 400 | ** insert routine needs to know about. |
drh | 9d9cf22 | 2007-02-13 15:01:11 +0000 | [diff] [blame] | 401 | */ |
| 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 */ |
| 406 | ){ |
drh | 6a288a3 | 2008-01-07 19:20:24 +0000 | [diff] [blame] | 407 | int memId = 0; /* Register holding maximum rowid */ |
drh | 186ebd4 | 2018-05-23 16:50:21 +0000 | [diff] [blame] | 408 | assert( pParse->db->aDb[iDb].pSchema!=0 ); |
drh | 9ef5e77 | 2016-08-19 14:20:56 +0000 | [diff] [blame] | 409 | if( (pTab->tabFlags & TF_Autoincrement)!=0 |
drh | 8257aa8 | 2017-07-26 19:59:13 +0000 | [diff] [blame] | 410 | && (pParse->db->mDbFlags & DBFLAG_Vacuum)==0 |
drh | 9ef5e77 | 2016-08-19 14:20:56 +0000 | [diff] [blame] | 411 | ){ |
dan | 65a7cd1 | 2009-09-01 12:16:01 +0000 | [diff] [blame] | 412 | Parse *pToplevel = sqlite3ParseToplevel(pParse); |
drh | 0b9f50d | 2009-06-23 20:28:53 +0000 | [diff] [blame] | 413 | AutoincInfo *pInfo; |
drh | 186ebd4 | 2018-05-23 16:50:21 +0000 | [diff] [blame] | 414 | Table *pSeqTab = pParse->db->aDb[iDb].pSchema->pSeqTab; |
| 415 | |
| 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 */ |
| 419 | if( pSeqTab==0 |
| 420 | || !HasRowid(pSeqTab) |
drh | 0003d87 | 2021-04-11 00:11:56 +0000 | [diff] [blame] | 421 | || NEVER(IsVirtual(pSeqTab)) |
drh | 186ebd4 | 2018-05-23 16:50:21 +0000 | [diff] [blame] | 422 | || pSeqTab->nCol!=2 |
| 423 | ){ |
| 424 | pParse->nErr++; |
| 425 | pParse->rc = SQLITE_CORRUPT_SEQUENCE; |
| 426 | return 0; |
| 427 | } |
drh | 0b9f50d | 2009-06-23 20:28:53 +0000 | [diff] [blame] | 428 | |
dan | 65a7cd1 | 2009-09-01 12:16:01 +0000 | [diff] [blame] | 429 | pInfo = pToplevel->pAinc; |
drh | 0b9f50d | 2009-06-23 20:28:53 +0000 | [diff] [blame] | 430 | while( pInfo && pInfo->pTab!=pTab ){ pInfo = pInfo->pNext; } |
| 431 | if( pInfo==0 ){ |
drh | 575fad6 | 2016-02-05 13:38:36 +0000 | [diff] [blame] | 432 | pInfo = sqlite3DbMallocRawNN(pParse->db, sizeof(*pInfo)); |
drh | 21d4f5b | 2021-01-12 15:30:01 +0000 | [diff] [blame] | 433 | sqlite3ParserAddCleanup(pToplevel, sqlite3DbFree, pInfo); |
| 434 | testcase( pParse->earlyCleanup ); |
| 435 | if( pParse->db->mallocFailed ) return 0; |
dan | 65a7cd1 | 2009-09-01 12:16:01 +0000 | [diff] [blame] | 436 | pInfo->pNext = pToplevel->pAinc; |
| 437 | pToplevel->pAinc = pInfo; |
drh | 0b9f50d | 2009-06-23 20:28:53 +0000 | [diff] [blame] | 438 | pInfo->pTab = pTab; |
| 439 | pInfo->iDb = iDb; |
dan | 65a7cd1 | 2009-09-01 12:16:01 +0000 | [diff] [blame] | 440 | pToplevel->nMem++; /* Register to hold name of table */ |
| 441 | pInfo->regCtr = ++pToplevel->nMem; /* Max rowid register */ |
drh | c8abbc1 | 2018-03-16 18:46:30 +0000 | [diff] [blame] | 442 | pToplevel->nMem +=2; /* Rowid in sqlite_sequence + orig max val */ |
drh | 0b9f50d | 2009-06-23 20:28:53 +0000 | [diff] [blame] | 443 | } |
| 444 | memId = pInfo->regCtr; |
drh | 9d9cf22 | 2007-02-13 15:01:11 +0000 | [diff] [blame] | 445 | } |
| 446 | return memId; |
| 447 | } |
| 448 | |
| 449 | /* |
drh | 0b9f50d | 2009-06-23 20:28:53 +0000 | [diff] [blame] | 450 | ** This routine generates code that will initialize all of the |
| 451 | ** register used by the autoincrement tracker. |
| 452 | */ |
| 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 */ |
drh | 0b9f50d | 2009-06-23 20:28:53 +0000 | [diff] [blame] | 458 | Vdbe *v = pParse->pVdbe; /* VDBE under construction */ |
| 459 | |
drh | 345ba7d | 2009-09-08 13:40:16 +0000 | [diff] [blame] | 460 | /* This routine is never called during trigger-generation. It is |
| 461 | ** only called from the top-level */ |
| 462 | assert( pParse->pTriggerTab==0 ); |
drh | c149f18 | 2015-09-29 13:25:15 +0000 | [diff] [blame] | 463 | assert( sqlite3IsToplevel(pParse) ); |
dan | 76d462e | 2009-08-30 11:42:51 +0000 | [diff] [blame] | 464 | |
drh | 0b9f50d | 2009-06-23 20:28:53 +0000 | [diff] [blame] | 465 | assert( v ); /* We failed long ago if this is not so */ |
| 466 | for(p = pParse->pAinc; p; p = p->pNext){ |
drh | 1b32554 | 2016-02-03 01:55:44 +0000 | [diff] [blame] | 467 | static const int iLn = VDBE_OFFSET_LINENO(2); |
| 468 | static const VdbeOpList autoInc[] = { |
| 469 | /* 0 */ {OP_Null, 0, 0, 0}, |
drh | c8abbc1 | 2018-03-16 18:46:30 +0000 | [diff] [blame] | 470 | /* 1 */ {OP_Rewind, 0, 10, 0}, |
drh | 1b32554 | 2016-02-03 01:55:44 +0000 | [diff] [blame] | 471 | /* 2 */ {OP_Column, 0, 0, 0}, |
drh | c8abbc1 | 2018-03-16 18:46:30 +0000 | [diff] [blame] | 472 | /* 3 */ {OP_Ne, 0, 9, 0}, |
drh | 1b32554 | 2016-02-03 01:55:44 +0000 | [diff] [blame] | 473 | /* 4 */ {OP_Rowid, 0, 0, 0}, |
| 474 | /* 5 */ {OP_Column, 0, 1, 0}, |
drh | c8abbc1 | 2018-03-16 18:46:30 +0000 | [diff] [blame] | 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} |
drh | 1b32554 | 2016-02-03 01:55:44 +0000 | [diff] [blame] | 481 | }; |
| 482 | VdbeOp *aOp; |
drh | 0b9f50d | 2009-06-23 20:28:53 +0000 | [diff] [blame] | 483 | pDb = &db->aDb[p->iDb]; |
| 484 | memId = p->regCtr; |
drh | 2120608 | 2011-04-04 18:22:02 +0000 | [diff] [blame] | 485 | assert( sqlite3SchemaMutexHeld(db, 0, pDb->pSchema) ); |
drh | 0b9f50d | 2009-06-23 20:28:53 +0000 | [diff] [blame] | 486 | sqlite3OpenTable(pParse, 0, p->iDb, pDb->pSchema->pSeqTab, OP_OpenRead); |
drh | 076e85f | 2015-09-03 13:46:12 +0000 | [diff] [blame] | 487 | sqlite3VdbeLoadString(v, memId-1, p->pTab->zName); |
drh | 1b32554 | 2016-02-03 01:55:44 +0000 | [diff] [blame] | 488 | aOp = sqlite3VdbeAddOpList(v, ArraySize(autoInc), autoInc, iLn); |
| 489 | if( aOp==0 ) break; |
| 490 | aOp[0].p2 = memId; |
drh | c8abbc1 | 2018-03-16 18:46:30 +0000 | [diff] [blame] | 491 | aOp[0].p3 = memId+2; |
drh | 1b32554 | 2016-02-03 01:55:44 +0000 | [diff] [blame] | 492 | aOp[2].p3 = memId; |
| 493 | aOp[3].p1 = memId-1; |
| 494 | aOp[3].p3 = memId; |
| 495 | aOp[3].p5 = SQLITE_JUMPIFNULL; |
| 496 | aOp[4].p2 = memId+1; |
| 497 | aOp[5].p3 = memId; |
drh | c8abbc1 | 2018-03-16 18:46:30 +0000 | [diff] [blame] | 498 | aOp[6].p1 = memId; |
| 499 | aOp[7].p2 = memId+2; |
| 500 | aOp[7].p1 = memId; |
| 501 | aOp[10].p2 = memId; |
drh | 04ab586 | 2018-12-01 21:13:41 +0000 | [diff] [blame] | 502 | if( pParse->nTab==0 ) pParse->nTab = 1; |
drh | 0b9f50d | 2009-06-23 20:28:53 +0000 | [diff] [blame] | 503 | } |
| 504 | } |
| 505 | |
| 506 | /* |
drh | 9d9cf22 | 2007-02-13 15:01:11 +0000 | [diff] [blame] | 507 | ** Update the maximum rowid for an autoincrement calculation. |
| 508 | ** |
drh | 1b32554 | 2016-02-03 01:55:44 +0000 | [diff] [blame] | 509 | ** This routine should be called when the regRowid register holds a |
drh | 9d9cf22 | 2007-02-13 15:01:11 +0000 | [diff] [blame] | 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 |
drh | 1b32554 | 2016-02-03 01:55:44 +0000 | [diff] [blame] | 512 | ** memory cell is updated. |
drh | 9d9cf22 | 2007-02-13 15:01:11 +0000 | [diff] [blame] | 513 | */ |
drh | 6a288a3 | 2008-01-07 19:20:24 +0000 | [diff] [blame] | 514 | static void autoIncStep(Parse *pParse, int memId, int regRowid){ |
drh | 9d9cf22 | 2007-02-13 15:01:11 +0000 | [diff] [blame] | 515 | if( memId>0 ){ |
drh | 6a288a3 | 2008-01-07 19:20:24 +0000 | [diff] [blame] | 516 | sqlite3VdbeAddOp2(pParse->pVdbe, OP_MemMax, memId, regRowid); |
drh | 9d9cf22 | 2007-02-13 15:01:11 +0000 | [diff] [blame] | 517 | } |
| 518 | } |
| 519 | |
| 520 | /* |
drh | 0b9f50d | 2009-06-23 20:28:53 +0000 | [diff] [blame] | 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. |
drh | 9d9cf22 | 2007-02-13 15:01:11 +0000 | [diff] [blame] | 526 | */ |
drh | 1b32554 | 2016-02-03 01:55:44 +0000 | [diff] [blame] | 527 | static SQLITE_NOINLINE void autoIncrementEnd(Parse *pParse){ |
drh | 0b9f50d | 2009-06-23 20:28:53 +0000 | [diff] [blame] | 528 | AutoincInfo *p; |
| 529 | Vdbe *v = pParse->pVdbe; |
| 530 | sqlite3 *db = pParse->db; |
drh | 6a288a3 | 2008-01-07 19:20:24 +0000 | [diff] [blame] | 531 | |
drh | 0b9f50d | 2009-06-23 20:28:53 +0000 | [diff] [blame] | 532 | assert( v ); |
| 533 | for(p = pParse->pAinc; p; p = p->pNext){ |
drh | 1b32554 | 2016-02-03 01:55:44 +0000 | [diff] [blame] | 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} |
| 541 | }; |
| 542 | VdbeOp *aOp; |
drh | 0b9f50d | 2009-06-23 20:28:53 +0000 | [diff] [blame] | 543 | Db *pDb = &db->aDb[p->iDb]; |
drh | 0b9f50d | 2009-06-23 20:28:53 +0000 | [diff] [blame] | 544 | int iRec; |
| 545 | int memId = p->regCtr; |
| 546 | |
| 547 | iRec = sqlite3GetTempReg(pParse); |
drh | 2120608 | 2011-04-04 18:22:02 +0000 | [diff] [blame] | 548 | assert( sqlite3SchemaMutexHeld(db, 0, pDb->pSchema) ); |
drh | c8abbc1 | 2018-03-16 18:46:30 +0000 | [diff] [blame] | 549 | sqlite3VdbeAddOp3(v, OP_Le, memId+2, sqlite3VdbeCurrentAddr(v)+7, memId); |
| 550 | VdbeCoverage(v); |
drh | 0b9f50d | 2009-06-23 20:28:53 +0000 | [diff] [blame] | 551 | sqlite3OpenTable(pParse, 0, p->iDb, pDb->pSchema->pSeqTab, OP_OpenWrite); |
drh | 1b32554 | 2016-02-03 01:55:44 +0000 | [diff] [blame] | 552 | aOp = sqlite3VdbeAddOpList(v, ArraySize(autoIncEnd), autoIncEnd, iLn); |
| 553 | if( aOp==0 ) break; |
| 554 | aOp[0].p1 = memId+1; |
| 555 | aOp[1].p2 = memId+1; |
| 556 | aOp[2].p1 = memId-1; |
| 557 | aOp[2].p3 = iRec; |
| 558 | aOp[3].p2 = iRec; |
| 559 | aOp[3].p3 = memId+1; |
| 560 | aOp[3].p5 = OPFLAG_APPEND; |
drh | 0b9f50d | 2009-06-23 20:28:53 +0000 | [diff] [blame] | 561 | sqlite3ReleaseTempReg(pParse, iRec); |
drh | 9d9cf22 | 2007-02-13 15:01:11 +0000 | [diff] [blame] | 562 | } |
| 563 | } |
drh | 1b32554 | 2016-02-03 01:55:44 +0000 | [diff] [blame] | 564 | void sqlite3AutoincrementEnd(Parse *pParse){ |
| 565 | if( pParse->pAinc ) autoIncrementEnd(pParse); |
| 566 | } |
drh | 9d9cf22 | 2007-02-13 15:01:11 +0000 | [diff] [blame] | 567 | #else |
| 568 | /* |
| 569 | ** If SQLITE_OMIT_AUTOINCREMENT is defined, then the three routines |
| 570 | ** above are all no-ops |
| 571 | */ |
| 572 | # define autoIncBegin(A,B,C) (0) |
danielk1977 | 287fb61 | 2008-01-04 19:10:28 +0000 | [diff] [blame] | 573 | # define autoIncStep(A,B,C) |
drh | 9d9cf22 | 2007-02-13 15:01:11 +0000 | [diff] [blame] | 574 | #endif /* SQLITE_OMIT_AUTOINCREMENT */ |
| 575 | |
| 576 | |
| 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 */ |
| 584 | ); |
| 585 | |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 +0000 | [diff] [blame] | 586 | /* |
drh | d82b502 | 2013-10-26 00:58:34 +0000 | [diff] [blame] | 587 | ** This routine is called to handle SQL of the following forms: |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 588 | ** |
drh | a21f78b | 2015-04-19 18:32:43 +0000 | [diff] [blame] | 589 | ** insert into TABLE (IDLIST) values(EXPRLIST),(EXPRLIST),... |
drh | 1ccde15 | 2000-06-17 13:12:39 +0000 | [diff] [blame] | 590 | ** insert into TABLE (IDLIST) select |
drh | a21f78b | 2015-04-19 18:32:43 +0000 | [diff] [blame] | 591 | ** insert into TABLE (IDLIST) default values |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 592 | ** |
drh | 1ccde15 | 2000-06-17 13:12:39 +0000 | [diff] [blame] | 593 | ** The IDLIST following the table name is always optional. If omitted, |
drh | a21f78b | 2015-04-19 18:32:43 +0000 | [diff] [blame] | 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 |
| 596 | ** is omitted. |
drh | 1ccde15 | 2000-06-17 13:12:39 +0000 | [diff] [blame] | 597 | ** |
drh | a21f78b | 2015-04-19 18:32:43 +0000 | [diff] [blame] | 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. |
drh | 142e30d | 2002-08-28 03:00:58 +0000 | [diff] [blame] | 603 | ** |
drh | 9d9cf22 | 2007-02-13 15:01:11 +0000 | [diff] [blame] | 604 | ** The code generated follows one of four templates. For a simple |
drh | a21f78b | 2015-04-19 18:32:43 +0000 | [diff] [blame] | 605 | ** insert with data coming from a single-row VALUES clause, the code executes |
drh | e00ee6e | 2008-06-20 15:24:01 +0000 | [diff] [blame] | 606 | ** once straight down through. Pseudo-code follows (we call this |
| 607 | ** the "1st template"): |
drh | 142e30d | 2002-08-28 03:00:58 +0000 | [diff] [blame] | 608 | ** |
| 609 | ** open write cursor to <table> and its indices |
drh | ec95c44 | 2013-10-23 01:57:32 +0000 | [diff] [blame] | 610 | ** put VALUES clause expressions into registers |
drh | 142e30d | 2002-08-28 03:00:58 +0000 | [diff] [blame] | 611 | ** write the resulting record into <table> |
| 612 | ** cleanup |
| 613 | ** |
drh | 9d9cf22 | 2007-02-13 15:01:11 +0000 | [diff] [blame] | 614 | ** The three remaining templates assume the statement is of the form |
drh | 142e30d | 2002-08-28 03:00:58 +0000 | [diff] [blame] | 615 | ** |
| 616 | ** INSERT INTO <table> SELECT ... |
| 617 | ** |
drh | 9d9cf22 | 2007-02-13 15:01:11 +0000 | [diff] [blame] | 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 |
drh | e00ee6e | 2008-06-20 15:24:01 +0000 | [diff] [blame] | 625 | ** template. This is the 2nd template. |
drh | 9d9cf22 | 2007-02-13 15:01:11 +0000 | [diff] [blame] | 626 | ** |
| 627 | ** open a write cursor to <table> |
| 628 | ** open read cursor on <table2> |
| 629 | ** transfer all records in <table2> over to <table> |
| 630 | ** close cursors |
| 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 |
| 635 | ** close cursors |
| 636 | ** end foreach |
| 637 | ** |
drh | e00ee6e | 2008-06-20 15:24:01 +0000 | [diff] [blame] | 638 | ** The 3rd template is for when the second template does not apply |
drh | 9d9cf22 | 2007-02-13 15:01:11 +0000 | [diff] [blame] | 639 | ** and the SELECT clause does not read from <table> at any time. |
| 640 | ** The generated code follows this template: |
drh | 142e30d | 2002-08-28 03:00:58 +0000 | [diff] [blame] | 641 | ** |
drh | e00ee6e | 2008-06-20 15:24:01 +0000 | [diff] [blame] | 642 | ** X <- A |
drh | 142e30d | 2002-08-28 03:00:58 +0000 | [diff] [blame] | 643 | ** goto B |
| 644 | ** A: setup for the SELECT |
drh | 9d9cf22 | 2007-02-13 15:01:11 +0000 | [diff] [blame] | 645 | ** loop over the rows in the SELECT |
drh | e00ee6e | 2008-06-20 15:24:01 +0000 | [diff] [blame] | 646 | ** load values into registers R..R+n |
| 647 | ** yield X |
drh | 142e30d | 2002-08-28 03:00:58 +0000 | [diff] [blame] | 648 | ** end loop |
| 649 | ** cleanup after the SELECT |
drh | 81cf13e | 2014-02-07 18:27:53 +0000 | [diff] [blame] | 650 | ** end-coroutine X |
drh | e00ee6e | 2008-06-20 15:24:01 +0000 | [diff] [blame] | 651 | ** B: open write cursor to <table> and its indices |
drh | 81cf13e | 2014-02-07 18:27:53 +0000 | [diff] [blame] | 652 | ** C: yield X, at EOF goto D |
drh | e00ee6e | 2008-06-20 15:24:01 +0000 | [diff] [blame] | 653 | ** insert the select result into <table> from R..R+n |
| 654 | ** goto C |
drh | 142e30d | 2002-08-28 03:00:58 +0000 | [diff] [blame] | 655 | ** D: cleanup |
| 656 | ** |
drh | e00ee6e | 2008-06-20 15:24:01 +0000 | [diff] [blame] | 657 | ** The 4th template is used if the insert statement takes its |
drh | 142e30d | 2002-08-28 03:00:58 +0000 | [diff] [blame] | 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, |
peter.d.reid | 60ec914 | 2014-09-06 16:39:46 +0000 | [diff] [blame] | 660 | ** we have to use an intermediate table to store the results of |
drh | 142e30d | 2002-08-28 03:00:58 +0000 | [diff] [blame] | 661 | ** the select. The template is like this: |
| 662 | ** |
drh | e00ee6e | 2008-06-20 15:24:01 +0000 | [diff] [blame] | 663 | ** X <- A |
drh | 142e30d | 2002-08-28 03:00:58 +0000 | [diff] [blame] | 664 | ** goto B |
| 665 | ** A: setup for the SELECT |
| 666 | ** loop over the tables in the SELECT |
drh | e00ee6e | 2008-06-20 15:24:01 +0000 | [diff] [blame] | 667 | ** load value into register R..R+n |
| 668 | ** yield X |
drh | 142e30d | 2002-08-28 03:00:58 +0000 | [diff] [blame] | 669 | ** end loop |
| 670 | ** cleanup after the SELECT |
drh | 81cf13e | 2014-02-07 18:27:53 +0000 | [diff] [blame] | 671 | ** end co-routine R |
drh | e00ee6e | 2008-06-20 15:24:01 +0000 | [diff] [blame] | 672 | ** B: open temp table |
drh | 81cf13e | 2014-02-07 18:27:53 +0000 | [diff] [blame] | 673 | ** L: yield X, at EOF goto M |
drh | e00ee6e | 2008-06-20 15:24:01 +0000 | [diff] [blame] | 674 | ** insert row from R..R+n into temp table |
| 675 | ** goto L |
| 676 | ** M: open write cursor to <table> and its indices |
| 677 | ** rewind temp table |
| 678 | ** C: loop over rows of intermediate table |
drh | 142e30d | 2002-08-28 03:00:58 +0000 | [diff] [blame] | 679 | ** transfer values form intermediate table into <table> |
drh | e00ee6e | 2008-06-20 15:24:01 +0000 | [diff] [blame] | 680 | ** end loop |
| 681 | ** D: cleanup |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 682 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 683 | void sqlite3Insert( |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 684 | Parse *pParse, /* Parser context */ |
drh | 113088e | 2003-03-20 01:16:58 +0000 | [diff] [blame] | 685 | SrcList *pTabList, /* Name of table into which we are inserting */ |
drh | 5974a30 | 2000-06-07 14:42:26 +0000 | [diff] [blame] | 686 | Select *pSelect, /* A SELECT statement to use as the data source */ |
drh | f5f1915 | 2019-10-21 01:04:11 +0000 | [diff] [blame] | 687 | IdList *pColumn, /* Column names corresponding to IDLIST, or NULL. */ |
drh | 2c2e844 | 2018-04-07 15:04:05 +0000 | [diff] [blame] | 688 | int onError, /* How to handle constraint errors */ |
drh | 46d2e5c | 2018-04-12 13:15:43 +0000 | [diff] [blame] | 689 | Upsert *pUpsert /* ON CONFLICT clauses for upsert, or NULL */ |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 690 | ){ |
drh | 6a288a3 | 2008-01-07 19:20:24 +0000 | [diff] [blame] | 691 | sqlite3 *db; /* The main database structure */ |
| 692 | Table *pTab; /* The table to insert into. aka TABLE */ |
drh | 60ffc80 | 2016-11-21 21:33:46 +0000 | [diff] [blame] | 693 | int i, j; /* Loop counters */ |
drh | 5974a30 | 2000-06-07 14:42:26 +0000 | [diff] [blame] | 694 | Vdbe *v; /* Generate code into this virtual machine */ |
| 695 | Index *pIdx; /* For looping over indices of the table */ |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 696 | int nColumn; /* Number of columns in the data */ |
drh | 6a288a3 | 2008-01-07 19:20:24 +0000 | [diff] [blame] | 697 | int nHidden = 0; /* Number of hidden columns if TABLE is virtual */ |
drh | 26198bb | 2013-10-31 11:15:09 +0000 | [diff] [blame] | 698 | int iDataCur = 0; /* VDBE cursor that is the main data repository */ |
| 699 | int iIdxCur = 0; /* First index cursor */ |
drh | d82b502 | 2013-10-26 00:58:34 +0000 | [diff] [blame] | 700 | int ipkColumn = -1; /* Column that is the INTEGER PRIMARY KEY */ |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 701 | int endOfLoop; /* Label for the end of the insertion loop */ |
danielk1977 | cfe9a69 | 2004-06-16 12:00:29 +0000 | [diff] [blame] | 702 | int srcTab = 0; /* Data comes from this temporary cursor if >=0 */ |
drh | e00ee6e | 2008-06-20 15:24:01 +0000 | [diff] [blame] | 703 | int addrInsTop = 0; /* Jump to label "D" */ |
| 704 | int addrCont = 0; /* Top of insert loop. Label "C" in templates 3 and 4 */ |
drh | 2eb9537 | 2008-06-06 15:04:36 +0000 | [diff] [blame] | 705 | SelectDest dest; /* Destination for SELECT on rhs of INSERT */ |
drh | 6a288a3 | 2008-01-07 19:20:24 +0000 | [diff] [blame] | 706 | int iDb; /* Index of database holding TABLE */ |
drh | 05a86c5 | 2014-02-16 01:55:49 +0000 | [diff] [blame] | 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 */ |
drh | a21f78b | 2015-04-19 18:32:43 +0000 | [diff] [blame] | 710 | u8 bIdListInOrder; /* True if IDLIST is in table order */ |
drh | 75593d9 | 2014-01-10 20:46:55 +0000 | [diff] [blame] | 711 | ExprList *pList = 0; /* List of VALUES() to be inserted */ |
drh | c27ea2a | 2019-10-16 20:05:56 +0000 | [diff] [blame] | 712 | int iRegStore; /* Register in which to store next column */ |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 713 | |
drh | 6a288a3 | 2008-01-07 19:20:24 +0000 | [diff] [blame] | 714 | /* Register allocations */ |
drh | 1bd10f8 | 2008-12-10 21:19:56 +0000 | [diff] [blame] | 715 | int regFromSelect = 0;/* Base register for data coming from SELECT */ |
drh | 6a288a3 | 2008-01-07 19:20:24 +0000 | [diff] [blame] | 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 */ |
drh | aa9b896 | 2008-01-08 02:57:55 +0000 | [diff] [blame] | 721 | int *aRegIdx = 0; /* One register allocated to each index */ |
drh | 6a288a3 | 2008-01-07 19:20:24 +0000 | [diff] [blame] | 722 | |
drh | 798da52 | 2004-11-04 04:42:28 +0000 | [diff] [blame] | 723 | #ifndef SQLITE_OMIT_TRIGGER |
| 724 | int isView; /* True if attempting to insert into a view */ |
danielk1977 | 2f886d1 | 2009-02-28 10:47:41 +0000 | [diff] [blame] | 725 | Trigger *pTrigger; /* List of triggers on pTab, if required */ |
| 726 | int tmask; /* Mask of trigger times */ |
drh | 798da52 | 2004-11-04 04:42:28 +0000 | [diff] [blame] | 727 | #endif |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 728 | |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 729 | db = pParse->db; |
drh | 0c7d3d3 | 2022-01-24 16:47:12 +0000 | [diff] [blame] | 730 | assert( db->pParse==pParse ); |
| 731 | if( pParse->nErr ){ |
drh | 6f7adc8 | 2006-01-11 21:41:20 +0000 | [diff] [blame] | 732 | goto insert_cleanup; |
| 733 | } |
drh | 0c7d3d3 | 2022-01-24 16:47:12 +0000 | [diff] [blame] | 734 | assert( db->mallocFailed==0 ); |
drh | 4c88348 | 2017-06-03 20:09:37 +0000 | [diff] [blame] | 735 | dest.iSDParm = 0; /* Suppress a harmless compiler warning */ |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 736 | |
drh | 75593d9 | 2014-01-10 20:46:55 +0000 | [diff] [blame] | 737 | /* If the Select object is really just a simple VALUES() list with a |
drh | a21f78b | 2015-04-19 18:32:43 +0000 | [diff] [blame] | 738 | ** single row (the common case) then keep that one row of values |
| 739 | ** and discard the other (unused) parts of the pSelect object |
drh | 75593d9 | 2014-01-10 20:46:55 +0000 | [diff] [blame] | 740 | */ |
| 741 | if( pSelect && (pSelect->selFlags & SF_Values)!=0 && pSelect->pPrior==0 ){ |
| 742 | pList = pSelect->pEList; |
| 743 | pSelect->pEList = 0; |
| 744 | sqlite3SelectDelete(db, pSelect); |
| 745 | pSelect = 0; |
| 746 | } |
| 747 | |
drh | 1ccde15 | 2000-06-17 13:12:39 +0000 | [diff] [blame] | 748 | /* Locate the table into which we will be inserting new information. |
| 749 | */ |
drh | 113088e | 2003-03-20 01:16:58 +0000 | [diff] [blame] | 750 | assert( pTabList->nSrc==1 ); |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 751 | pTab = sqlite3SrcListLookup(pParse, pTabList); |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 752 | if( pTab==0 ){ |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 753 | goto insert_cleanup; |
| 754 | } |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 755 | iDb = sqlite3SchemaToIndex(db, pTab->pSchema); |
| 756 | assert( iDb<db->nDb ); |
drh | a0daa75 | 2016-09-16 11:53:10 +0000 | [diff] [blame] | 757 | if( sqlite3AuthCheck(pParse, SQLITE_INSERT, pTab->zName, 0, |
| 758 | db->aDb[iDb].zDbSName) ){ |
drh | 1962bda | 2003-01-12 19:33:52 +0000 | [diff] [blame] | 759 | goto insert_cleanup; |
| 760 | } |
drh | ec95c44 | 2013-10-23 01:57:32 +0000 | [diff] [blame] | 761 | withoutRowid = !HasRowid(pTab); |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 762 | |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 763 | /* Figure out if we have any triggers and if the table being |
| 764 | ** inserted into is a view |
| 765 | */ |
| 766 | #ifndef SQLITE_OMIT_TRIGGER |
danielk1977 | 2f886d1 | 2009-02-28 10:47:41 +0000 | [diff] [blame] | 767 | pTrigger = sqlite3TriggersExist(pParse, pTab, TK_INSERT, 0, &tmask); |
drh | f38524d | 2021-08-02 16:41:57 +0000 | [diff] [blame] | 768 | isView = IsView(pTab); |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 769 | #else |
danielk1977 | 2f886d1 | 2009-02-28 10:47:41 +0000 | [diff] [blame] | 770 | # define pTrigger 0 |
| 771 | # define tmask 0 |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 772 | # define isView 0 |
| 773 | #endif |
| 774 | #ifdef SQLITE_OMIT_VIEW |
| 775 | # undef isView |
| 776 | # define isView 0 |
| 777 | #endif |
danielk1977 | 2f886d1 | 2009-02-28 10:47:41 +0000 | [diff] [blame] | 778 | assert( (pTrigger && tmask) || (pTrigger==0 && tmask==0) ); |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 779 | |
drh | 2a7dcbf | 2022-04-06 15:41:53 +0000 | [diff] [blame] | 780 | #if TREETRACE_ENABLED |
| 781 | if( sqlite3TreeTrace & 0x10000 ){ |
| 782 | sqlite3TreeViewLine(0, "In sqlite3Insert() at %s:%d", __FILE__, __LINE__); |
drh | c2d0df9 | 2022-04-06 18:30:17 +0000 | [diff] [blame] | 783 | sqlite3TreeViewInsert(pParse->pWith, pTabList, pColumn, pSelect, pList, |
drh | 2a7dcbf | 2022-04-06 15:41:53 +0000 | [diff] [blame] | 784 | onError, pUpsert, pTrigger); |
| 785 | } |
| 786 | #endif |
| 787 | |
drh | f573c99 | 2002-07-31 00:32:50 +0000 | [diff] [blame] | 788 | /* If pTab is really a view, make sure it has been initialized. |
drh | d82b502 | 2013-10-26 00:58:34 +0000 | [diff] [blame] | 789 | ** ViewGetColumnNames() is a no-op if pTab is not a view. |
drh | f573c99 | 2002-07-31 00:32:50 +0000 | [diff] [blame] | 790 | */ |
danielk1977 | b3d24bf | 2006-06-19 03:05:10 +0000 | [diff] [blame] | 791 | if( sqlite3ViewGetColumnNames(pParse, pTab) ){ |
drh | 5cf590c | 2003-04-24 01:45:04 +0000 | [diff] [blame] | 792 | goto insert_cleanup; |
drh | f573c99 | 2002-07-31 00:32:50 +0000 | [diff] [blame] | 793 | } |
| 794 | |
drh | d82b502 | 2013-10-26 00:58:34 +0000 | [diff] [blame] | 795 | /* Cannot insert into a read-only table. |
danielk1977 | 595a523 | 2009-07-24 17:58:53 +0000 | [diff] [blame] | 796 | */ |
| 797 | if( sqlite3IsReadOnly(pParse, pTab, tmask) ){ |
| 798 | goto insert_cleanup; |
| 799 | } |
| 800 | |
drh | 1ccde15 | 2000-06-17 13:12:39 +0000 | [diff] [blame] | 801 | /* Allocate a VDBE |
| 802 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 803 | v = sqlite3GetVdbe(pParse); |
drh | 5974a30 | 2000-06-07 14:42:26 +0000 | [diff] [blame] | 804 | if( v==0 ) goto insert_cleanup; |
drh | 4794f73 | 2004-11-05 17:17:50 +0000 | [diff] [blame] | 805 | if( pParse->nested==0 ) sqlite3VdbeCountChanges(v); |
danielk1977 | 2f886d1 | 2009-02-28 10:47:41 +0000 | [diff] [blame] | 806 | sqlite3BeginWriteOperation(pParse, pSelect || pTrigger, iDb); |
drh | 1ccde15 | 2000-06-17 13:12:39 +0000 | [diff] [blame] | 807 | |
drh | 9d9cf22 | 2007-02-13 15:01:11 +0000 | [diff] [blame] | 808 | #ifndef SQLITE_OMIT_XFER_OPT |
| 809 | /* If the statement is of the form |
| 810 | ** |
| 811 | ** INSERT INTO <table1> SELECT * FROM <table2>; |
| 812 | ** |
| 813 | ** Then special optimizations can be applied that make the transfer |
| 814 | ** very fast and which reduce fragmentation of indices. |
drh | e00ee6e | 2008-06-20 15:24:01 +0000 | [diff] [blame] | 815 | ** |
| 816 | ** This is the 2nd template. |
drh | 9d9cf22 | 2007-02-13 15:01:11 +0000 | [diff] [blame] | 817 | */ |
drh | 935c372 | 2022-02-28 16:44:58 +0000 | [diff] [blame] | 818 | if( pColumn==0 |
| 819 | && pSelect!=0 |
| 820 | && pTrigger==0 |
| 821 | && xferOptimization(pParse, pTab, pSelect, onError, iDb) |
| 822 | ){ |
danielk1977 | 2f886d1 | 2009-02-28 10:47:41 +0000 | [diff] [blame] | 823 | assert( !pTrigger ); |
drh | 9d9cf22 | 2007-02-13 15:01:11 +0000 | [diff] [blame] | 824 | assert( pList==0 ); |
drh | 0b9f50d | 2009-06-23 20:28:53 +0000 | [diff] [blame] | 825 | goto insert_end; |
drh | 9d9cf22 | 2007-02-13 15:01:11 +0000 | [diff] [blame] | 826 | } |
| 827 | #endif /* SQLITE_OMIT_XFER_OPT */ |
| 828 | |
drh | 2958a4e | 2004-11-12 03:56:15 +0000 | [diff] [blame] | 829 | /* If this is an AUTOINCREMENT table, look up the sequence number in the |
drh | 6a288a3 | 2008-01-07 19:20:24 +0000 | [diff] [blame] | 830 | ** sqlite_sequence table and store it in memory cell regAutoinc. |
drh | 2958a4e | 2004-11-12 03:56:15 +0000 | [diff] [blame] | 831 | */ |
drh | 6a288a3 | 2008-01-07 19:20:24 +0000 | [diff] [blame] | 832 | regAutoinc = autoIncBegin(pParse, iDb, pTab); |
drh | 2958a4e | 2004-11-12 03:56:15 +0000 | [diff] [blame] | 833 | |
drh | f5f1915 | 2019-10-21 01:04:11 +0000 | [diff] [blame] | 834 | /* Allocate a block registers to hold the rowid and the values |
| 835 | ** for all columns of the new row. |
drh | 1ccde15 | 2000-06-17 13:12:39 +0000 | [diff] [blame] | 836 | */ |
drh | 05a86c5 | 2014-02-16 01:55:49 +0000 | [diff] [blame] | 837 | regRowid = regIns = pParse->nMem+1; |
| 838 | pParse->nMem += pTab->nCol + 1; |
danielk1977 | 034ca14 | 2007-06-26 10:38:54 +0000 | [diff] [blame] | 839 | if( IsVirtual(pTab) ){ |
drh | 05a86c5 | 2014-02-16 01:55:49 +0000 | [diff] [blame] | 840 | regRowid++; |
| 841 | pParse->nMem++; |
danielk1977 | 034ca14 | 2007-06-26 10:38:54 +0000 | [diff] [blame] | 842 | } |
drh | 05a86c5 | 2014-02-16 01:55:49 +0000 | [diff] [blame] | 843 | regData = regRowid+1; |
drh | 1ccde15 | 2000-06-17 13:12:39 +0000 | [diff] [blame] | 844 | |
| 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. |
drh | c839258 | 2001-12-31 02:48:51 +0000 | [diff] [blame] | 848 | ** |
| 849 | ** If the table has an INTEGER PRIMARY KEY column and that column |
drh | d82b502 | 2013-10-26 00:58:34 +0000 | [diff] [blame] | 850 | ** is named in the IDLIST, then record in the ipkColumn variable |
| 851 | ** the index into IDLIST of the primary key column. ipkColumn is |
drh | c839258 | 2001-12-31 02:48:51 +0000 | [diff] [blame] | 852 | ** the index of the primary key as it appears in IDLIST, not as |
drh | d82b502 | 2013-10-26 00:58:34 +0000 | [diff] [blame] | 853 | ** is appears in the original table. (The index of the INTEGER |
drh | f5f1915 | 2019-10-21 01:04:11 +0000 | [diff] [blame] | 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. |
| 858 | ** |
| 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. |
drh | 1ccde15 | 2000-06-17 13:12:39 +0000 | [diff] [blame] | 863 | */ |
drh | d4cd292 | 2019-10-17 18:07:22 +0000 | [diff] [blame] | 864 | bIdListInOrder = (pTab->tabFlags & (TF_OOOHidden|TF_HasStored))==0; |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 865 | if( pColumn ){ |
drh | a99e325 | 2022-04-15 15:47:14 +0000 | [diff] [blame] | 866 | assert( pColumn->eU4!=EU4_EXPR ); |
| 867 | pColumn->eU4 = EU4_IDX; |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 868 | for(i=0; i<pColumn->nId; i++){ |
drh | a99e325 | 2022-04-15 15:47:14 +0000 | [diff] [blame] | 869 | pColumn->a[i].u4.idx = -1; |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 870 | } |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 871 | for(i=0; i<pColumn->nId; i++){ |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 872 | for(j=0; j<pTab->nCol; j++){ |
drh | cf9d36d | 2021-08-02 18:03:43 +0000 | [diff] [blame] | 873 | if( sqlite3StrICmp(pColumn->a[i].zName, pTab->aCol[j].zCnName)==0 ){ |
drh | a99e325 | 2022-04-15 15:47:14 +0000 | [diff] [blame] | 874 | pColumn->a[i].u4.idx = j; |
drh | 05a86c5 | 2014-02-16 01:55:49 +0000 | [diff] [blame] | 875 | if( i!=j ) bIdListInOrder = 0; |
drh | 4a32431 | 2001-12-21 14:30:42 +0000 | [diff] [blame] | 876 | if( j==pTab->iPKey ){ |
drh | d82b502 | 2013-10-26 00:58:34 +0000 | [diff] [blame] | 877 | ipkColumn = i; assert( !withoutRowid ); |
drh | 4a32431 | 2001-12-21 14:30:42 +0000 | [diff] [blame] | 878 | } |
drh | 7e508f1 | 2019-10-16 19:31:46 +0000 | [diff] [blame] | 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\"", |
drh | cf9d36d | 2021-08-02 18:03:43 +0000 | [diff] [blame] | 883 | pTab->aCol[j].zCnName); |
drh | 7e508f1 | 2019-10-16 19:31:46 +0000 | [diff] [blame] | 884 | goto insert_cleanup; |
| 885 | } |
| 886 | #endif |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 887 | break; |
| 888 | } |
| 889 | } |
| 890 | if( j>=pTab->nCol ){ |
drh | ec95c44 | 2013-10-23 01:57:32 +0000 | [diff] [blame] | 891 | if( sqlite3IsRowid(pColumn->a[i].zName) && !withoutRowid ){ |
drh | d82b502 | 2013-10-26 00:58:34 +0000 | [diff] [blame] | 892 | ipkColumn = i; |
drh | e48ae71 | 2014-05-23 11:48:57 +0000 | [diff] [blame] | 893 | bIdListInOrder = 0; |
drh | a0217ba | 2003-06-01 01:10:33 +0000 | [diff] [blame] | 894 | }else{ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 895 | sqlite3ErrorMsg(pParse, "table %S has no column named %s", |
drh | a979993 | 2021-03-19 13:00:28 +0000 | [diff] [blame] | 896 | pTabList->a, pColumn->a[i].zName); |
dan | 1db9510 | 2010-06-28 10:15:19 +0000 | [diff] [blame] | 897 | pParse->checkSchema = 1; |
drh | a0217ba | 2003-06-01 01:10:33 +0000 | [diff] [blame] | 898 | goto insert_cleanup; |
| 899 | } |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 900 | } |
| 901 | } |
| 902 | } |
drh | 1ccde15 | 2000-06-17 13:12:39 +0000 | [diff] [blame] | 903 | |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 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. |
| 908 | */ |
drh | 5f08526 | 2012-09-15 13:29:23 +0000 | [diff] [blame] | 909 | if( pSelect ){ |
drh | a21f78b | 2015-04-19 18:32:43 +0000 | [diff] [blame] | 910 | /* Data is coming from a SELECT or from a multi-row VALUES clause. |
| 911 | ** Generate a co-routine to run the SELECT. */ |
drh | 05a86c5 | 2014-02-16 01:55:49 +0000 | [diff] [blame] | 912 | int regYield; /* Register holding co-routine entry-point */ |
| 913 | int addrTop; /* Top of the co-routine */ |
| 914 | int rc; /* Result code */ |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 915 | |
drh | 05a86c5 | 2014-02-16 01:55:49 +0000 | [diff] [blame] | 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); |
drh | 2b596da | 2012-07-23 21:43:19 +0000 | [diff] [blame] | 923 | regFromSelect = dest.iSdst; |
drh | 0c7d3d3 | 2022-01-24 16:47:12 +0000 | [diff] [blame] | 924 | assert( db->pParse==pParse ); |
| 925 | if( rc || pParse->nErr ) goto insert_cleanup; |
| 926 | assert( db->mallocFailed==0 ); |
drh | 2fade2f | 2016-02-09 02:12:20 +0000 | [diff] [blame] | 927 | sqlite3VdbeEndCoroutine(v, regYield); |
drh | 05a86c5 | 2014-02-16 01:55:49 +0000 | [diff] [blame] | 928 | sqlite3VdbeJumpHere(v, addrTop - 1); /* label B: */ |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 929 | assert( pSelect->pEList ); |
| 930 | nColumn = pSelect->pEList->nExpr; |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 931 | |
| 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). |
| 936 | ** |
| 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. |
| 940 | */ |
drh | 05a86c5 | 2014-02-16 01:55:49 +0000 | [diff] [blame] | 941 | if( pTrigger || readsTable(pParse, iDb, pTab) ){ |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 942 | useTempTable = 1; |
| 943 | } |
| 944 | |
| 945 | if( useTempTable ){ |
| 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: |
| 949 | ** |
| 950 | ** B: open temp table |
drh | 81cf13e | 2014-02-07 18:27:53 +0000 | [diff] [blame] | 951 | ** L: yield X, goto M at EOF |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 952 | ** insert row from R..R+n into temp table |
| 953 | ** goto L |
| 954 | ** M: ... |
| 955 | */ |
| 956 | int regRec; /* Register to hold packed record */ |
| 957 | int regTempRowid; /* Register to hold temp table ROWID */ |
drh | 06280ee | 2014-02-20 19:32:38 +0000 | [diff] [blame] | 958 | int addrL; /* Label "L" */ |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 959 | |
| 960 | srcTab = pParse->nTab++; |
| 961 | regRec = sqlite3GetTempReg(pParse); |
| 962 | regTempRowid = sqlite3GetTempReg(pParse); |
| 963 | sqlite3VdbeAddOp2(v, OP_OpenEphemeral, srcTab, nColumn); |
drh | 06280ee | 2014-02-20 19:32:38 +0000 | [diff] [blame] | 964 | addrL = sqlite3VdbeAddOp1(v, OP_Yield, dest.iSDParm); VdbeCoverage(v); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 965 | sqlite3VdbeAddOp3(v, OP_MakeRecord, regFromSelect, nColumn, regRec); |
| 966 | sqlite3VdbeAddOp2(v, OP_NewRowid, srcTab, regTempRowid); |
| 967 | sqlite3VdbeAddOp3(v, OP_Insert, srcTab, regRec, regTempRowid); |
drh | 076e85f | 2015-09-03 13:46:12 +0000 | [diff] [blame] | 968 | sqlite3VdbeGoto(v, addrL); |
drh | 06280ee | 2014-02-20 19:32:38 +0000 | [diff] [blame] | 969 | sqlite3VdbeJumpHere(v, addrL); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 970 | sqlite3ReleaseTempReg(pParse, regRec); |
| 971 | sqlite3ReleaseTempReg(pParse, regTempRowid); |
| 972 | } |
| 973 | }else{ |
drh | a21f78b | 2015-04-19 18:32:43 +0000 | [diff] [blame] | 974 | /* This is the case if the data for the INSERT is coming from a |
| 975 | ** single-row VALUES clause |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 976 | */ |
| 977 | NameContext sNC; |
| 978 | memset(&sNC, 0, sizeof(sNC)); |
| 979 | sNC.pParse = pParse; |
| 980 | srcTab = -1; |
| 981 | assert( useTempTable==0 ); |
drh | fea870b | 2015-08-24 20:54:06 +0000 | [diff] [blame] | 982 | if( pList ){ |
| 983 | nColumn = pList->nExpr; |
| 984 | if( sqlite3ResolveExprListNames(&sNC, pList) ){ |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 985 | goto insert_cleanup; |
| 986 | } |
drh | fea870b | 2015-08-24 20:54:06 +0000 | [diff] [blame] | 987 | }else{ |
| 988 | nColumn = 0; |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 989 | } |
| 990 | } |
| 991 | |
drh | aacc543 | 2002-01-06 17:07:40 +0000 | [diff] [blame] | 992 | /* If there is no IDLIST term but the table has an integer primary |
drh | d82b502 | 2013-10-26 00:58:34 +0000 | [diff] [blame] | 993 | ** key, the set the ipkColumn variable to the integer primary key |
| 994 | ** column index in the original table definition. |
drh | 4a32431 | 2001-12-21 14:30:42 +0000 | [diff] [blame] | 995 | */ |
drh | 147d0cc | 2006-08-25 23:42:53 +0000 | [diff] [blame] | 996 | if( pColumn==0 && nColumn>0 ){ |
drh | d82b502 | 2013-10-26 00:58:34 +0000 | [diff] [blame] | 997 | ipkColumn = pTab->iPKey; |
drh | 427b96a | 2019-10-22 13:01:24 +0000 | [diff] [blame] | 998 | #ifndef SQLITE_OMIT_GENERATED_COLUMNS |
drh | 6ab61d7 | 2019-10-23 15:47:33 +0000 | [diff] [blame] | 999 | if( ipkColumn>=0 && (pTab->tabFlags & TF_HasGenerated)!=0 ){ |
drh | 427b96a | 2019-10-22 13:01:24 +0000 | [diff] [blame] | 1000 | testcase( pTab->tabFlags & TF_HasVirtual ); |
drh | 6ab61d7 | 2019-10-23 15:47:33 +0000 | [diff] [blame] | 1001 | testcase( pTab->tabFlags & TF_HasStored ); |
drh | 427b96a | 2019-10-22 13:01:24 +0000 | [diff] [blame] | 1002 | for(i=ipkColumn-1; i>=0; i--){ |
| 1003 | if( pTab->aCol[i].colFlags & COLFLAG_GENERATED ){ |
| 1004 | testcase( pTab->aCol[i].colFlags & COLFLAG_VIRTUAL ); |
drh | 6ab61d7 | 2019-10-23 15:47:33 +0000 | [diff] [blame] | 1005 | testcase( pTab->aCol[i].colFlags & COLFLAG_STORED ); |
drh | 427b96a | 2019-10-22 13:01:24 +0000 | [diff] [blame] | 1006 | ipkColumn--; |
| 1007 | } |
| 1008 | } |
| 1009 | } |
| 1010 | #endif |
drh | 05a86c5 | 2014-02-16 01:55:49 +0000 | [diff] [blame] | 1011 | |
drh | c7e93f5 | 2021-02-18 00:26:11 +0000 | [diff] [blame] | 1012 | /* Make sure the number of columns in the source data matches the number |
| 1013 | ** of columns to be inserted into the table. |
| 1014 | */ |
drh | 6f6e60d | 2021-02-18 15:45:34 +0000 | [diff] [blame] | 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 ){ |
drh | c7e93f5 | 2021-02-18 00:26:11 +0000 | [diff] [blame] | 1019 | for(i=0; i<pTab->nCol; i++){ |
| 1020 | if( pTab->aCol[i].colFlags & COLFLAG_NOINSERT ) nHidden++; |
| 1021 | } |
| 1022 | } |
| 1023 | if( nColumn!=(pTab->nCol-nHidden) ){ |
| 1024 | sqlite3ErrorMsg(pParse, |
| 1025 | "table %S has %d columns but %d values were supplied", |
drh | a979993 | 2021-03-19 13:00:28 +0000 | [diff] [blame] | 1026 | pTabList->a, pTab->nCol-nHidden, nColumn); |
drh | c7e93f5 | 2021-02-18 00:26:11 +0000 | [diff] [blame] | 1027 | goto insert_cleanup; |
| 1028 | } |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 1029 | } |
| 1030 | if( pColumn!=0 && nColumn!=pColumn->nId ){ |
| 1031 | sqlite3ErrorMsg(pParse, "%d values for %d columns", nColumn, pColumn->nId); |
| 1032 | goto insert_cleanup; |
| 1033 | } |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 1034 | |
drh | feeb139 | 2002-04-09 03:28:01 +0000 | [diff] [blame] | 1035 | /* Initialize the count of rows to be inserted |
| 1036 | */ |
drh | 7963691 | 2018-04-19 23:52:39 +0000 | [diff] [blame] | 1037 | if( (db->flags & SQLITE_CountRows)!=0 |
| 1038 | && !pParse->nested |
| 1039 | && !pParse->pTriggerTab |
drh | d086aa0 | 2021-01-29 21:31:59 +0000 | [diff] [blame] | 1040 | && !pParse->bReturning |
drh | 7963691 | 2018-04-19 23:52:39 +0000 | [diff] [blame] | 1041 | ){ |
drh | 6a288a3 | 2008-01-07 19:20:24 +0000 | [diff] [blame] | 1042 | regRowCount = ++pParse->nMem; |
| 1043 | sqlite3VdbeAddOp2(v, OP_Integer, 0, regRowCount); |
drh | feeb139 | 2002-04-09 03:28:01 +0000 | [diff] [blame] | 1044 | } |
| 1045 | |
danielk1977 | e448dc4 | 2008-01-02 11:50:51 +0000 | [diff] [blame] | 1046 | /* If this is not a view, open the table and and all indices */ |
| 1047 | if( !isView ){ |
drh | aa9b896 | 2008-01-08 02:57:55 +0000 | [diff] [blame] | 1048 | int nIdx; |
dan | fd261ec | 2015-10-22 20:54:33 +0000 | [diff] [blame] | 1049 | nIdx = sqlite3OpenTableAndIndices(pParse, pTab, OP_OpenWrite, 0, -1, 0, |
drh | 26198bb | 2013-10-31 11:15:09 +0000 | [diff] [blame] | 1050 | &iDataCur, &iIdxCur); |
drh | a7c3b93 | 2019-05-07 19:13:42 +0000 | [diff] [blame] | 1051 | aRegIdx = sqlite3DbMallocRawNN(db, sizeof(int)*(nIdx+2)); |
drh | aa9b896 | 2008-01-08 02:57:55 +0000 | [diff] [blame] | 1052 | if( aRegIdx==0 ){ |
| 1053 | goto insert_cleanup; |
| 1054 | } |
drh | 2c4dfc3 | 2016-11-10 14:24:04 +0000 | [diff] [blame] | 1055 | for(i=0, pIdx=pTab->pIndex; i<nIdx; pIdx=pIdx->pNext, i++){ |
| 1056 | assert( pIdx ); |
drh | aa9b896 | 2008-01-08 02:57:55 +0000 | [diff] [blame] | 1057 | aRegIdx[i] = ++pParse->nMem; |
drh | 2c4dfc3 | 2016-11-10 14:24:04 +0000 | [diff] [blame] | 1058 | pParse->nMem += pIdx->nColumn; |
drh | aa9b896 | 2008-01-08 02:57:55 +0000 | [diff] [blame] | 1059 | } |
drh | a7c3b93 | 2019-05-07 19:13:42 +0000 | [diff] [blame] | 1060 | aRegIdx[i] = ++pParse->nMem; /* Register to store the table record */ |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 1061 | } |
drh | 788d55a | 2018-04-13 01:15:09 +0000 | [diff] [blame] | 1062 | #ifndef SQLITE_OMIT_UPSERT |
drh | 0b30a11 | 2018-04-13 21:55:22 +0000 | [diff] [blame] | 1063 | if( pUpsert ){ |
drh | 20b8632 | 2020-12-09 01:34:48 +0000 | [diff] [blame] | 1064 | Upsert *pNx; |
drh | b042d92 | 2019-01-04 23:39:37 +0000 | [diff] [blame] | 1065 | if( IsVirtual(pTab) ){ |
| 1066 | sqlite3ErrorMsg(pParse, "UPSERT not implemented for virtual table \"%s\"", |
| 1067 | pTab->zName); |
| 1068 | goto insert_cleanup; |
| 1069 | } |
drh | f38524d | 2021-08-02 16:41:57 +0000 | [diff] [blame] | 1070 | if( IsView(pTab) ){ |
drh | c6b24ab | 2019-12-06 01:23:38 +0000 | [diff] [blame] | 1071 | sqlite3ErrorMsg(pParse, "cannot UPSERT a view"); |
| 1072 | goto insert_cleanup; |
| 1073 | } |
dan | 9105fd5 | 2019-08-19 17:26:32 +0000 | [diff] [blame] | 1074 | if( sqlite3HasExplicitNulls(pParse, pUpsert->pUpsertTarget) ){ |
| 1075 | goto insert_cleanup; |
| 1076 | } |
drh | 788d55a | 2018-04-13 01:15:09 +0000 | [diff] [blame] | 1077 | pTabList->a[0].iCursor = iDataCur; |
drh | 20b8632 | 2020-12-09 01:34:48 +0000 | [diff] [blame] | 1078 | pNx = pUpsert; |
| 1079 | do{ |
| 1080 | pNx->pUpsertSrc = pTabList; |
| 1081 | pNx->regData = regData; |
| 1082 | pNx->iDataCur = iDataCur; |
| 1083 | pNx->iIdxCur = iIdxCur; |
| 1084 | if( pNx->pUpsertTarget ){ |
dan | 93eb906 | 2021-03-19 14:26:24 +0000 | [diff] [blame] | 1085 | if( sqlite3UpsertAnalyzeTarget(pParse, pTabList, pNx) ){ |
| 1086 | goto insert_cleanup; |
| 1087 | } |
drh | 20b8632 | 2020-12-09 01:34:48 +0000 | [diff] [blame] | 1088 | } |
| 1089 | pNx = pNx->pNextUpsert; |
| 1090 | }while( pNx!=0 ); |
drh | 788d55a | 2018-04-13 01:15:09 +0000 | [diff] [blame] | 1091 | } |
| 1092 | #endif |
| 1093 | |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 1094 | |
drh | e00ee6e | 2008-06-20 15:24:01 +0000 | [diff] [blame] | 1095 | /* This is the top of the main insertion loop */ |
drh | 142e30d | 2002-08-28 03:00:58 +0000 | [diff] [blame] | 1096 | if( useTempTable ){ |
drh | e00ee6e | 2008-06-20 15:24:01 +0000 | [diff] [blame] | 1097 | /* This block codes the top of loop only. The complete loop is the |
| 1098 | ** following pseudocode (template 4): |
| 1099 | ** |
drh | 81cf13e | 2014-02-07 18:27:53 +0000 | [diff] [blame] | 1100 | ** rewind temp table, if empty goto D |
drh | e00ee6e | 2008-06-20 15:24:01 +0000 | [diff] [blame] | 1101 | ** C: loop over rows of intermediate table |
| 1102 | ** transfer values form intermediate table into <table> |
| 1103 | ** end loop |
| 1104 | ** D: ... |
| 1105 | */ |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 1106 | addrInsTop = sqlite3VdbeAddOp1(v, OP_Rewind, srcTab); VdbeCoverage(v); |
drh | e00ee6e | 2008-06-20 15:24:01 +0000 | [diff] [blame] | 1107 | addrCont = sqlite3VdbeCurrentAddr(v); |
drh | 142e30d | 2002-08-28 03:00:58 +0000 | [diff] [blame] | 1108 | }else if( pSelect ){ |
drh | e00ee6e | 2008-06-20 15:24:01 +0000 | [diff] [blame] | 1109 | /* This block codes the top of loop only. The complete loop is the |
| 1110 | ** following pseudocode (template 3): |
| 1111 | ** |
drh | 81cf13e | 2014-02-07 18:27:53 +0000 | [diff] [blame] | 1112 | ** C: yield X, at EOF goto D |
drh | e00ee6e | 2008-06-20 15:24:01 +0000 | [diff] [blame] | 1113 | ** insert the select result into <table> from R..R+n |
| 1114 | ** goto C |
| 1115 | ** D: ... |
| 1116 | */ |
drh | 3aef2fb | 2020-01-02 17:46:02 +0000 | [diff] [blame] | 1117 | sqlite3VdbeReleaseRegisters(pParse, regData, pTab->nCol, 0, 0); |
drh | 81cf13e | 2014-02-07 18:27:53 +0000 | [diff] [blame] | 1118 | addrInsTop = addrCont = sqlite3VdbeAddOp1(v, OP_Yield, dest.iSDParm); |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 1119 | VdbeCoverage(v); |
drh | f5f1915 | 2019-10-21 01:04:11 +0000 | [diff] [blame] | 1120 | if( ipkColumn>=0 ){ |
| 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); |
| 1125 | } |
drh | 5974a30 | 2000-06-07 14:42:26 +0000 | [diff] [blame] | 1126 | } |
drh | 1ccde15 | 2000-06-17 13:12:39 +0000 | [diff] [blame] | 1127 | |
drh | f5f1915 | 2019-10-21 01:04:11 +0000 | [diff] [blame] | 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 |
| 1133 | ** of the rowid. |
| 1134 | */ |
| 1135 | nHidden = 0; |
| 1136 | iRegStore = regData; assert( regData==regRowid+1 ); |
| 1137 | for(i=0; i<pTab->nCol; i++, iRegStore++){ |
| 1138 | int k; |
| 1139 | u32 colFlags; |
| 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); |
| 1147 | continue; |
| 1148 | } |
| 1149 | if( ((colFlags = pTab->aCol[i].colFlags) & COLFLAG_NOINSERT)!=0 ){ |
| 1150 | nHidden++; |
| 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 */ |
| 1155 | iRegStore--; |
| 1156 | continue; |
| 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); |
| 1164 | } |
| 1165 | continue; |
| 1166 | }else if( pColumn==0 ){ |
| 1167 | /* Hidden columns that are not explicitly named in the INSERT |
| 1168 | ** get there default value */ |
drh | 79cf2b7 | 2021-07-31 20:30:41 +0000 | [diff] [blame] | 1169 | sqlite3ExprCodeFactorable(pParse, |
| 1170 | sqlite3ColumnExpr(pTab, &pTab->aCol[i]), |
| 1171 | iRegStore); |
drh | f5f1915 | 2019-10-21 01:04:11 +0000 | [diff] [blame] | 1172 | continue; |
| 1173 | } |
| 1174 | } |
| 1175 | if( pColumn ){ |
drh | a99e325 | 2022-04-15 15:47:14 +0000 | [diff] [blame] | 1176 | assert( pColumn->eU4==EU4_IDX ); |
| 1177 | for(j=0; j<pColumn->nId && pColumn->a[j].u4.idx!=i; j++){} |
drh | f5f1915 | 2019-10-21 01:04:11 +0000 | [diff] [blame] | 1178 | if( j>=pColumn->nId ){ |
| 1179 | /* A column not named in the insert column list gets its |
| 1180 | ** default value */ |
drh | 79cf2b7 | 2021-07-31 20:30:41 +0000 | [diff] [blame] | 1181 | sqlite3ExprCodeFactorable(pParse, |
| 1182 | sqlite3ColumnExpr(pTab, &pTab->aCol[i]), |
| 1183 | iRegStore); |
drh | f5f1915 | 2019-10-21 01:04:11 +0000 | [diff] [blame] | 1184 | continue; |
| 1185 | } |
| 1186 | k = j; |
| 1187 | }else if( nColumn==0 ){ |
| 1188 | /* This is INSERT INTO ... DEFAULT VALUES. Load the default value. */ |
drh | 79cf2b7 | 2021-07-31 20:30:41 +0000 | [diff] [blame] | 1189 | sqlite3ExprCodeFactorable(pParse, |
| 1190 | sqlite3ColumnExpr(pTab, &pTab->aCol[i]), |
| 1191 | iRegStore); |
drh | f5f1915 | 2019-10-21 01:04:11 +0000 | [diff] [blame] | 1192 | continue; |
| 1193 | }else{ |
| 1194 | k = i - nHidden; |
| 1195 | } |
| 1196 | |
| 1197 | if( useTempTable ){ |
| 1198 | sqlite3VdbeAddOp3(v, OP_Column, srcTab, k, iRegStore); |
| 1199 | }else if( pSelect ){ |
| 1200 | if( regFromSelect!=regData ){ |
| 1201 | sqlite3VdbeAddOp2(v, OP_SCopy, regFromSelect+k, iRegStore); |
| 1202 | } |
| 1203 | }else{ |
drh | 2d2e528 | 2022-07-25 21:37:13 +0000 | [diff] [blame] | 1204 | Expr *pX = pList->a[k].pExpr; |
| 1205 | int y = sqlite3ExprCodeTarget(pParse, pX, iRegStore); |
| 1206 | if( y!=iRegStore ){ |
| 1207 | sqlite3VdbeAddOp2(v, |
| 1208 | ExprHasProperty(pX, EP_Subquery) ? OP_Copy : OP_SCopy, y, iRegStore); |
| 1209 | } |
drh | f5f1915 | 2019-10-21 01:04:11 +0000 | [diff] [blame] | 1210 | } |
| 1211 | } |
| 1212 | |
| 1213 | |
drh | 5cf590c | 2003-04-24 01:45:04 +0000 | [diff] [blame] | 1214 | /* Run the BEFORE and INSTEAD OF triggers, if there are any |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 1215 | */ |
drh | ec4ccdb | 2018-12-29 02:26:59 +0000 | [diff] [blame] | 1216 | endOfLoop = sqlite3VdbeMakeLabel(pParse); |
danielk1977 | 2f886d1 | 2009-02-28 10:47:41 +0000 | [diff] [blame] | 1217 | if( tmask & TRIGGER_BEFORE ){ |
dan | 76d462e | 2009-08-30 11:42:51 +0000 | [diff] [blame] | 1218 | int regCols = sqlite3GetTempRange(pParse, pTab->nCol+1); |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 1219 | |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 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 |
| 1225 | */ |
drh | d82b502 | 2013-10-26 00:58:34 +0000 | [diff] [blame] | 1226 | if( ipkColumn<0 ){ |
dan | 76d462e | 2009-08-30 11:42:51 +0000 | [diff] [blame] | 1227 | sqlite3VdbeAddOp2(v, OP_Integer, -1, regCols); |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 1228 | }else{ |
drh | 728e0f9 | 2015-10-10 14:41:28 +0000 | [diff] [blame] | 1229 | int addr1; |
drh | ec95c44 | 2013-10-23 01:57:32 +0000 | [diff] [blame] | 1230 | assert( !withoutRowid ); |
drh | 7fe4590 | 2009-05-01 02:08:04 +0000 | [diff] [blame] | 1231 | if( useTempTable ){ |
drh | d82b502 | 2013-10-26 00:58:34 +0000 | [diff] [blame] | 1232 | sqlite3VdbeAddOp3(v, OP_Column, srcTab, ipkColumn, regCols); |
drh | 7fe4590 | 2009-05-01 02:08:04 +0000 | [diff] [blame] | 1233 | }else{ |
| 1234 | assert( pSelect==0 ); /* Otherwise useTempTable is true */ |
drh | d82b502 | 2013-10-26 00:58:34 +0000 | [diff] [blame] | 1235 | sqlite3ExprCode(pParse, pList->a[ipkColumn].pExpr, regCols); |
drh | 7fe4590 | 2009-05-01 02:08:04 +0000 | [diff] [blame] | 1236 | } |
drh | 728e0f9 | 2015-10-10 14:41:28 +0000 | [diff] [blame] | 1237 | addr1 = sqlite3VdbeAddOp1(v, OP_NotNull, regCols); VdbeCoverage(v); |
dan | 76d462e | 2009-08-30 11:42:51 +0000 | [diff] [blame] | 1238 | sqlite3VdbeAddOp2(v, OP_Integer, -1, regCols); |
drh | 728e0f9 | 2015-10-10 14:41:28 +0000 | [diff] [blame] | 1239 | sqlite3VdbeJumpHere(v, addr1); |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 1240 | sqlite3VdbeAddOp1(v, OP_MustBeInt, regCols); VdbeCoverage(v); |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 1241 | } |
| 1242 | |
drh | f5f1915 | 2019-10-21 01:04:11 +0000 | [diff] [blame] | 1243 | /* Copy the new data already generated. */ |
| 1244 | assert( pTab->nNVCol>0 ); |
| 1245 | sqlite3VdbeAddOp3(v, OP_Copy, regRowid+1, regCols+1, pTab->nNVCol-1); |
| 1246 | |
| 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. */ |
drh | 427b96a | 2019-10-22 13:01:24 +0000 | [diff] [blame] | 1252 | if( pTab->tabFlags & TF_HasGenerated ){ |
| 1253 | testcase( pTab->tabFlags & TF_HasVirtual ); |
| 1254 | testcase( pTab->tabFlags & TF_HasStored ); |
drh | f5f1915 | 2019-10-21 01:04:11 +0000 | [diff] [blame] | 1255 | sqlite3ComputeGeneratedColumns(pParse, regCols+1, pTab); |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 1256 | } |
drh | f5f1915 | 2019-10-21 01:04:11 +0000 | [diff] [blame] | 1257 | #endif |
danielk1977 | a37cdde | 2004-05-16 11:15:36 +0000 | [diff] [blame] | 1258 | |
| 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. |
| 1263 | */ |
| 1264 | if( !isView ){ |
drh | 57bf4a8 | 2014-02-17 14:59:22 +0000 | [diff] [blame] | 1265 | sqlite3TableAffinity(v, pTab, regCols+1); |
danielk1977 | a37cdde | 2004-05-16 11:15:36 +0000 | [diff] [blame] | 1266 | } |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 1267 | |
drh | 5cf590c | 2003-04-24 01:45:04 +0000 | [diff] [blame] | 1268 | /* Fire BEFORE or INSTEAD OF triggers */ |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 1269 | sqlite3CodeRowTrigger(pParse, pTrigger, TK_INSERT, 0, TRIGGER_BEFORE, |
dan | 94d7f50 | 2009-09-24 09:05:49 +0000 | [diff] [blame] | 1270 | pTab, regCols-pTab->nCol-1, onError, endOfLoop); |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 1271 | |
dan | 76d462e | 2009-08-30 11:42:51 +0000 | [diff] [blame] | 1272 | sqlite3ReleaseTempRange(pParse, regCols, pTab->nCol+1); |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 1273 | } |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 1274 | |
drh | 5cf590c | 2003-04-24 01:45:04 +0000 | [diff] [blame] | 1275 | if( !isView ){ |
drh | 4cbdda9 | 2006-06-14 19:00:20 +0000 | [diff] [blame] | 1276 | if( IsVirtual(pTab) ){ |
danielk1977 | 287fb61 | 2008-01-04 19:10:28 +0000 | [diff] [blame] | 1277 | /* The row that the VUpdate opcode will delete: none */ |
drh | 6a288a3 | 2008-01-07 19:20:24 +0000 | [diff] [blame] | 1278 | sqlite3VdbeAddOp2(v, OP_Null, 0, regIns); |
drh | 4cbdda9 | 2006-06-14 19:00:20 +0000 | [diff] [blame] | 1279 | } |
drh | d82b502 | 2013-10-26 00:58:34 +0000 | [diff] [blame] | 1280 | if( ipkColumn>=0 ){ |
drh | f5f1915 | 2019-10-21 01:04:11 +0000 | [diff] [blame] | 1281 | /* Compute the new rowid */ |
drh | 142e30d | 2002-08-28 03:00:58 +0000 | [diff] [blame] | 1282 | if( useTempTable ){ |
drh | d82b502 | 2013-10-26 00:58:34 +0000 | [diff] [blame] | 1283 | sqlite3VdbeAddOp3(v, OP_Column, srcTab, ipkColumn, regRowid); |
drh | 142e30d | 2002-08-28 03:00:58 +0000 | [diff] [blame] | 1284 | }else if( pSelect ){ |
drh | f5f1915 | 2019-10-21 01:04:11 +0000 | [diff] [blame] | 1285 | /* Rowid already initialized at tag-20191021-001 */ |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 1286 | }else{ |
drh | 04fcef0 | 2019-01-17 04:40:04 +0000 | [diff] [blame] | 1287 | Expr *pIpk = pList->a[ipkColumn].pExpr; |
| 1288 | if( pIpk->op==TK_NULL && !IsVirtual(pTab) ){ |
| 1289 | sqlite3VdbeAddOp3(v, OP_NewRowid, iDataCur, regRowid, regAutoinc); |
drh | e4d9081 | 2007-03-29 05:51:49 +0000 | [diff] [blame] | 1290 | appendFlag = 1; |
drh | 04fcef0 | 2019-01-17 04:40:04 +0000 | [diff] [blame] | 1291 | }else{ |
| 1292 | sqlite3ExprCode(pParse, pList->a[ipkColumn].pExpr, regRowid); |
drh | e4d9081 | 2007-03-29 05:51:49 +0000 | [diff] [blame] | 1293 | } |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 1294 | } |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 1295 | /* If the PRIMARY KEY expression is NULL, then use OP_NewRowid |
drh | 27a3278 | 2002-06-19 20:32:43 +0000 | [diff] [blame] | 1296 | ** to generate a unique primary key value. |
| 1297 | */ |
drh | e4d9081 | 2007-03-29 05:51:49 +0000 | [diff] [blame] | 1298 | if( !appendFlag ){ |
drh | 728e0f9 | 2015-10-10 14:41:28 +0000 | [diff] [blame] | 1299 | int addr1; |
danielk1977 | bb50e7a | 2008-07-04 10:56:07 +0000 | [diff] [blame] | 1300 | if( !IsVirtual(pTab) ){ |
drh | 728e0f9 | 2015-10-10 14:41:28 +0000 | [diff] [blame] | 1301 | addr1 = sqlite3VdbeAddOp1(v, OP_NotNull, regRowid); VdbeCoverage(v); |
drh | 26198bb | 2013-10-31 11:15:09 +0000 | [diff] [blame] | 1302 | sqlite3VdbeAddOp3(v, OP_NewRowid, iDataCur, regRowid, regAutoinc); |
drh | 728e0f9 | 2015-10-10 14:41:28 +0000 | [diff] [blame] | 1303 | sqlite3VdbeJumpHere(v, addr1); |
danielk1977 | bb50e7a | 2008-07-04 10:56:07 +0000 | [diff] [blame] | 1304 | }else{ |
drh | 728e0f9 | 2015-10-10 14:41:28 +0000 | [diff] [blame] | 1305 | addr1 = sqlite3VdbeCurrentAddr(v); |
| 1306 | sqlite3VdbeAddOp2(v, OP_IsNull, regRowid, addr1+2); VdbeCoverage(v); |
danielk1977 | bb50e7a | 2008-07-04 10:56:07 +0000 | [diff] [blame] | 1307 | } |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 1308 | sqlite3VdbeAddOp1(v, OP_MustBeInt, regRowid); VdbeCoverage(v); |
drh | e4d9081 | 2007-03-29 05:51:49 +0000 | [diff] [blame] | 1309 | } |
drh | ec95c44 | 2013-10-23 01:57:32 +0000 | [diff] [blame] | 1310 | }else if( IsVirtual(pTab) || withoutRowid ){ |
drh | 6a288a3 | 2008-01-07 19:20:24 +0000 | [diff] [blame] | 1311 | sqlite3VdbeAddOp2(v, OP_Null, 0, regRowid); |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 1312 | }else{ |
drh | 26198bb | 2013-10-31 11:15:09 +0000 | [diff] [blame] | 1313 | sqlite3VdbeAddOp3(v, OP_NewRowid, iDataCur, regRowid, regAutoinc); |
drh | e4d9081 | 2007-03-29 05:51:49 +0000 | [diff] [blame] | 1314 | appendFlag = 1; |
drh | 4a32431 | 2001-12-21 14:30:42 +0000 | [diff] [blame] | 1315 | } |
drh | 6a288a3 | 2008-01-07 19:20:24 +0000 | [diff] [blame] | 1316 | autoIncStep(pParse, regAutoinc, regRowid); |
drh | 4a32431 | 2001-12-21 14:30:42 +0000 | [diff] [blame] | 1317 | |
drh | c143114 | 2019-10-17 17:54:05 +0000 | [diff] [blame] | 1318 | #ifndef SQLITE_OMIT_GENERATED_COLUMNS |
drh | dd6cc9b | 2019-10-19 18:47:27 +0000 | [diff] [blame] | 1319 | /* Compute the new value for generated columns after all other |
drh | f5f1915 | 2019-10-21 01:04:11 +0000 | [diff] [blame] | 1320 | ** columns have already been computed. This must be done after |
| 1321 | ** computing the ROWID in case one of the generated columns |
drh | b5f6243 | 2019-12-10 02:48:41 +0000 | [diff] [blame] | 1322 | ** is derived from the INTEGER PRIMARY KEY. */ |
drh | 427b96a | 2019-10-22 13:01:24 +0000 | [diff] [blame] | 1323 | if( pTab->tabFlags & TF_HasGenerated ){ |
drh | dd6cc9b | 2019-10-19 18:47:27 +0000 | [diff] [blame] | 1324 | sqlite3ComputeGeneratedColumns(pParse, regRowid+1, pTab); |
drh | bed8690 | 2000-06-02 13:27:59 +0000 | [diff] [blame] | 1325 | } |
drh | c143114 | 2019-10-17 17:54:05 +0000 | [diff] [blame] | 1326 | #endif |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 1327 | |
| 1328 | /* Generate code to check constraints and generate index keys and |
danielk1977 | f29ce55 | 2002-05-19 23:43:12 +0000 | [diff] [blame] | 1329 | ** do the insertion. |
| 1330 | */ |
drh | 4cbdda9 | 2006-06-14 19:00:20 +0000 | [diff] [blame] | 1331 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
| 1332 | if( IsVirtual(pTab) ){ |
danielk1977 | 595a523 | 2009-07-24 17:58:53 +0000 | [diff] [blame] | 1333 | const char *pVTab = (const char *)sqlite3GetVTable(db, pTab); |
drh | 4f3dd15 | 2008-04-28 18:46:43 +0000 | [diff] [blame] | 1334 | sqlite3VtabMakeWritable(pParse, pTab); |
danielk1977 | 595a523 | 2009-07-24 17:58:53 +0000 | [diff] [blame] | 1335 | sqlite3VdbeAddOp4(v, OP_VUpdate, 1, pTab->nCol+2, regIns, pVTab, P4_VTAB); |
dan | b061d05 | 2011-04-25 18:49:57 +0000 | [diff] [blame] | 1336 | sqlite3VdbeChangeP5(v, onError==OE_Default ? OE_Abort : onError); |
dan | e0af83a | 2009-09-08 19:15:01 +0000 | [diff] [blame] | 1337 | sqlite3MayAbort(pParse); |
drh | 4cbdda9 | 2006-06-14 19:00:20 +0000 | [diff] [blame] | 1338 | }else |
| 1339 | #endif |
| 1340 | { |
dan | 11fbee2 | 2021-04-06 16:42:05 +0000 | [diff] [blame] | 1341 | int isReplace = 0;/* Set to true if constraints may cause a replace */ |
dan | 3b908d4 | 2016-11-08 19:22:32 +0000 | [diff] [blame] | 1342 | int bUseSeek; /* True to use OPFLAG_SEEKRESULT */ |
drh | f8ffb27 | 2013-11-01 17:08:56 +0000 | [diff] [blame] | 1343 | sqlite3GenerateConstraintChecks(pParse, pTab, aRegIdx, iDataCur, iIdxCur, |
drh | 788d55a | 2018-04-13 01:15:09 +0000 | [diff] [blame] | 1344 | regIns, 0, ipkColumn>=0, onError, endOfLoop, &isReplace, 0, pUpsert |
drh | 04adf41 | 2008-01-08 18:57:50 +0000 | [diff] [blame] | 1345 | ); |
drh | 509a630 | 2022-07-25 23:01:41 +0000 | [diff] [blame] | 1346 | if( db->flags & SQLITE_ForeignKeys ){ |
| 1347 | sqlite3FkCheck(pParse, pTab, 0, regIns, 0, 0); |
| 1348 | } |
dan | 3b908d4 | 2016-11-08 19:22:32 +0000 | [diff] [blame] | 1349 | |
| 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. */ |
drh | 06baba5 | 2019-10-24 19:35:26 +0000 | [diff] [blame] | 1358 | bUseSeek = (isReplace==0 || !sqlite3VdbeHasSubProgram(v)); |
drh | 26198bb | 2013-10-31 11:15:09 +0000 | [diff] [blame] | 1359 | sqlite3CompleteInsertion(pParse, pTab, iDataCur, iIdxCur, |
dan | 3b908d4 | 2016-11-08 19:22:32 +0000 | [diff] [blame] | 1360 | regIns, aRegIdx, 0, appendFlag, bUseSeek |
| 1361 | ); |
drh | 4cbdda9 | 2006-06-14 19:00:20 +0000 | [diff] [blame] | 1362 | } |
drh | 6e5020e | 2021-04-07 15:45:01 +0000 | [diff] [blame] | 1363 | #ifdef SQLITE_ALLOW_ROWID_IN_VIEW |
dan | 2a1aeaa | 2021-04-06 13:53:10 +0000 | [diff] [blame] | 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); |
drh | 6e5020e | 2021-04-07 15:45:01 +0000 | [diff] [blame] | 1369 | #endif |
drh | 5cf590c | 2003-04-24 01:45:04 +0000 | [diff] [blame] | 1370 | } |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 1371 | |
drh | 5cf590c | 2003-04-24 01:45:04 +0000 | [diff] [blame] | 1372 | /* Update the count of rows that are inserted |
| 1373 | */ |
drh | 7963691 | 2018-04-19 23:52:39 +0000 | [diff] [blame] | 1374 | if( regRowCount ){ |
drh | 6a288a3 | 2008-01-07 19:20:24 +0000 | [diff] [blame] | 1375 | sqlite3VdbeAddOp2(v, OP_AddImm, regRowCount, 1); |
drh | 5974a30 | 2000-06-07 14:42:26 +0000 | [diff] [blame] | 1376 | } |
drh | 1ccde15 | 2000-06-17 13:12:39 +0000 | [diff] [blame] | 1377 | |
danielk1977 | 2f886d1 | 2009-02-28 10:47:41 +0000 | [diff] [blame] | 1378 | if( pTrigger ){ |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 1379 | /* Code AFTER triggers */ |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 1380 | sqlite3CodeRowTrigger(pParse, pTrigger, TK_INSERT, 0, TRIGGER_AFTER, |
dan | 94d7f50 | 2009-09-24 09:05:49 +0000 | [diff] [blame] | 1381 | pTab, regData-2-pTab->nCol, onError, endOfLoop); |
drh | 1bee3d7 | 2001-10-15 00:44:35 +0000 | [diff] [blame] | 1382 | } |
| 1383 | |
drh | e00ee6e | 2008-06-20 15:24:01 +0000 | [diff] [blame] | 1384 | /* The bottom of the main insertion loop, if the data source |
| 1385 | ** is a SELECT statement. |
danielk1977 | f29ce55 | 2002-05-19 23:43:12 +0000 | [diff] [blame] | 1386 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1387 | sqlite3VdbeResolveLabel(v, endOfLoop); |
drh | 142e30d | 2002-08-28 03:00:58 +0000 | [diff] [blame] | 1388 | if( useTempTable ){ |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 1389 | sqlite3VdbeAddOp2(v, OP_Next, srcTab, addrCont); VdbeCoverage(v); |
drh | e00ee6e | 2008-06-20 15:24:01 +0000 | [diff] [blame] | 1390 | sqlite3VdbeJumpHere(v, addrInsTop); |
drh | 2eb9537 | 2008-06-06 15:04:36 +0000 | [diff] [blame] | 1391 | sqlite3VdbeAddOp1(v, OP_Close, srcTab); |
drh | 142e30d | 2002-08-28 03:00:58 +0000 | [diff] [blame] | 1392 | }else if( pSelect ){ |
drh | 076e85f | 2015-09-03 13:46:12 +0000 | [diff] [blame] | 1393 | sqlite3VdbeGoto(v, addrCont); |
drh | d9670ab | 2019-12-28 01:52:46 +0000 | [diff] [blame] | 1394 | #ifdef SQLITE_DEBUG |
| 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); |
| 1401 | } |
| 1402 | #endif |
drh | e00ee6e | 2008-06-20 15:24:01 +0000 | [diff] [blame] | 1403 | sqlite3VdbeJumpHere(v, addrInsTop); |
drh | 6b56344 | 2001-11-07 16:48:26 +0000 | [diff] [blame] | 1404 | } |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 1405 | |
mistachkin | d6665c5 | 2021-01-18 19:28:56 +0000 | [diff] [blame] | 1406 | #ifndef SQLITE_OMIT_XFER_OPT |
drh | 0b9f50d | 2009-06-23 20:28:53 +0000 | [diff] [blame] | 1407 | insert_end: |
mistachkin | d6665c5 | 2021-01-18 19:28:56 +0000 | [diff] [blame] | 1408 | #endif /* SQLITE_OMIT_XFER_OPT */ |
drh | f338814 | 2004-11-13 03:48:06 +0000 | [diff] [blame] | 1409 | /* Update the sqlite_sequence table by storing the content of the |
drh | 0b9f50d | 2009-06-23 20:28:53 +0000 | [diff] [blame] | 1410 | ** maximum rowid counter values recorded while inserting into |
| 1411 | ** autoincrement tables. |
drh | 2958a4e | 2004-11-12 03:56:15 +0000 | [diff] [blame] | 1412 | */ |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 1413 | if( pParse->nested==0 && pParse->pTriggerTab==0 ){ |
drh | 0b9f50d | 2009-06-23 20:28:53 +0000 | [diff] [blame] | 1414 | sqlite3AutoincrementEnd(pParse); |
| 1415 | } |
drh | 2958a4e | 2004-11-12 03:56:15 +0000 | [diff] [blame] | 1416 | |
drh | 1bee3d7 | 2001-10-15 00:44:35 +0000 | [diff] [blame] | 1417 | /* |
danielk1977 | e7de6f2 | 2004-11-05 06:02:06 +0000 | [diff] [blame] | 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. |
drh | 1bee3d7 | 2001-10-15 00:44:35 +0000 | [diff] [blame] | 1421 | */ |
drh | 7963691 | 2018-04-19 23:52:39 +0000 | [diff] [blame] | 1422 | if( regRowCount ){ |
drh | 3b26b2b | 2021-12-01 19:17:14 +0000 | [diff] [blame] | 1423 | sqlite3CodeChangeCount(v, regRowCount, "rows inserted"); |
drh | 1bee3d7 | 2001-10-15 00:44:35 +0000 | [diff] [blame] | 1424 | } |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 1425 | |
| 1426 | insert_cleanup: |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 1427 | sqlite3SrcListDelete(db, pTabList); |
| 1428 | sqlite3ExprListDelete(db, pList); |
drh | 46d2e5c | 2018-04-12 13:15:43 +0000 | [diff] [blame] | 1429 | sqlite3UpsertDelete(db, pUpsert); |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 1430 | sqlite3SelectDelete(db, pSelect); |
| 1431 | sqlite3IdListDelete(db, pColumn); |
drh | 41ce47c | 2022-08-22 02:00:26 +0000 | [diff] [blame] | 1432 | if( aRegIdx ) sqlite3DbNNFreeNN(db, aRegIdx); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 1433 | } |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 1434 | |
dan | 75cbd98 | 2009-09-21 16:06:03 +0000 | [diff] [blame] | 1435 | /* Make sure "isView" and other macros defined above are undefined. Otherwise |
peter.d.reid | 60ec914 | 2014-09-06 16:39:46 +0000 | [diff] [blame] | 1436 | ** they may interfere with compilation of other functions in this file |
dan | 75cbd98 | 2009-09-21 16:06:03 +0000 | [diff] [blame] | 1437 | ** (or in another file, if this file becomes part of the amalgamation). */ |
| 1438 | #ifdef isView |
| 1439 | #undef isView |
| 1440 | #endif |
| 1441 | #ifdef pTrigger |
| 1442 | #undef pTrigger |
| 1443 | #endif |
| 1444 | #ifdef tmask |
| 1445 | #undef tmask |
| 1446 | #endif |
| 1447 | |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 1448 | /* |
drh | e9816d8 | 2018-09-15 21:38:48 +0000 | [diff] [blame] | 1449 | ** Meanings of bits in of pWalker->eCode for |
| 1450 | ** sqlite3ExprReferencesUpdatedColumn() |
drh | 98bfa16 | 2016-02-10 18:24:05 +0000 | [diff] [blame] | 1451 | */ |
| 1452 | #define CKCNSTRNT_COLUMN 0x01 /* CHECK constraint uses a changing column */ |
| 1453 | #define CKCNSTRNT_ROWID 0x02 /* CHECK constraint references the ROWID */ |
| 1454 | |
drh | e9816d8 | 2018-09-15 21:38:48 +0000 | [diff] [blame] | 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 |
drh | 2a0b527 | 2016-02-10 16:52:24 +0000 | [diff] [blame] | 1458 | ** columns that are being modifed by an UPDATE statement. |
| 1459 | */ |
| 1460 | static int checkConstraintExprNode(Walker *pWalker, Expr *pExpr){ |
drh | 98bfa16 | 2016-02-10 18:24:05 +0000 | [diff] [blame] | 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; |
| 1466 | } |
| 1467 | }else{ |
| 1468 | pWalker->eCode |= CKCNSTRNT_ROWID; |
| 1469 | } |
drh | 2a0b527 | 2016-02-10 16:52:24 +0000 | [diff] [blame] | 1470 | } |
| 1471 | return WRC_Continue; |
| 1472 | } |
| 1473 | |
| 1474 | /* |
| 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 |
drh | 98bfa16 | 2016-02-10 18:24:05 +0000 | [diff] [blame] | 1477 | ** aiChng[i]>=0, and also the ROWID is modified if chngRowid is true. |
| 1478 | ** |
drh | e9816d8 | 2018-09-15 21:38:48 +0000 | [diff] [blame] | 1479 | ** Return true if CHECK constraint pExpr uses any of the |
drh | 98bfa16 | 2016-02-10 18:24:05 +0000 | [diff] [blame] | 1480 | ** changing columns (or the rowid if it is changing). In other words, |
drh | e9816d8 | 2018-09-15 21:38:48 +0000 | [diff] [blame] | 1481 | ** return true if this CHECK constraint must be validated for |
drh | 98bfa16 | 2016-02-10 18:24:05 +0000 | [diff] [blame] | 1482 | ** the new row in the UPDATE statement. |
drh | e9816d8 | 2018-09-15 21:38:48 +0000 | [diff] [blame] | 1483 | ** |
| 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 |
| 1487 | ** third arguments. |
drh | 2a0b527 | 2016-02-10 16:52:24 +0000 | [diff] [blame] | 1488 | */ |
drh | e9816d8 | 2018-09-15 21:38:48 +0000 | [diff] [blame] | 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 */ |
| 1493 | ){ |
drh | 2a0b527 | 2016-02-10 16:52:24 +0000 | [diff] [blame] | 1494 | Walker w; |
| 1495 | memset(&w, 0, sizeof(w)); |
drh | 98bfa16 | 2016-02-10 18:24:05 +0000 | [diff] [blame] | 1496 | w.eCode = 0; |
drh | 2a0b527 | 2016-02-10 16:52:24 +0000 | [diff] [blame] | 1497 | w.xExprCallback = checkConstraintExprNode; |
| 1498 | w.u.aiCol = aiChng; |
| 1499 | sqlite3WalkExpr(&w, pExpr); |
drh | 05723a9 | 2016-02-10 19:10:50 +0000 | [diff] [blame] | 1500 | if( !chngRowid ){ |
| 1501 | testcase( (w.eCode & CKCNSTRNT_ROWID)!=0 ); |
| 1502 | w.eCode &= ~CKCNSTRNT_ROWID; |
| 1503 | } |
| 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) ); |
drh | e9816d8 | 2018-09-15 21:38:48 +0000 | [diff] [blame] | 1508 | return w.eCode!=0; |
drh | 2a0b527 | 2016-02-10 16:52:24 +0000 | [diff] [blame] | 1509 | } |
| 1510 | |
drh | 11e8527 | 2013-10-26 15:40:48 +0000 | [diff] [blame] | 1511 | /* |
drh | daf2761 | 2020-12-10 20:31:25 +0000 | [diff] [blame] | 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 |
| 1516 | ** this. |
| 1517 | ** |
| 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. |
| 1521 | */ |
| 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 */ |
| 1527 | union { |
| 1528 | struct { /* Use this object for eType==0: A Index.pNext list */ |
| 1529 | Index *pIdx; /* The current Index */ |
| 1530 | } lx; |
| 1531 | struct { /* Use this object for eType==1; Array of IndexListTerm */ |
| 1532 | int nIdx; /* Size of the array */ |
| 1533 | IndexListTerm *aIdx; /* Array of IndexListTerms */ |
| 1534 | } ax; |
| 1535 | } u; |
| 1536 | }; |
| 1537 | |
| 1538 | /* When IndexIterator.eType==1, then each index is an array of instances |
| 1539 | ** of the following object |
| 1540 | */ |
| 1541 | struct IndexListTerm { |
| 1542 | Index *p; /* The index */ |
| 1543 | int ix; /* Which entry in the original Table.pIndex list is this index*/ |
| 1544 | }; |
| 1545 | |
| 1546 | /* Return the first index on the list */ |
| 1547 | static Index *indexIteratorFirst(IndexIterator *pIter, int *pIx){ |
drh | ed4c546 | 2020-12-11 16:49:51 +0000 | [diff] [blame] | 1548 | assert( pIter->i==0 ); |
| 1549 | if( pIter->eType ){ |
| 1550 | *pIx = pIter->u.ax.aIdx[0].ix; |
| 1551 | return pIter->u.ax.aIdx[0].p; |
| 1552 | }else{ |
| 1553 | *pIx = 0; |
| 1554 | return pIter->u.lx.pIdx; |
| 1555 | } |
drh | daf2761 | 2020-12-10 20:31:25 +0000 | [diff] [blame] | 1556 | } |
| 1557 | |
| 1558 | /* Return the next index from the list. Return NULL when out of indexes */ |
| 1559 | static Index *indexIteratorNext(IndexIterator *pIter, int *pIx){ |
drh | daf2761 | 2020-12-10 20:31:25 +0000 | [diff] [blame] | 1560 | if( pIter->eType ){ |
drh | d3e21a1 | 2020-12-11 17:11:56 +0000 | [diff] [blame] | 1561 | int i = ++pIter->i; |
drh | 61e280a | 2020-12-11 01:17:06 +0000 | [diff] [blame] | 1562 | if( i>=pIter->u.ax.nIdx ){ |
| 1563 | *pIx = i; |
| 1564 | return 0; |
| 1565 | } |
drh | daf2761 | 2020-12-10 20:31:25 +0000 | [diff] [blame] | 1566 | *pIx = pIter->u.ax.aIdx[i].ix; |
| 1567 | return pIter->u.ax.aIdx[i].p; |
| 1568 | }else{ |
drh | d3e21a1 | 2020-12-11 17:11:56 +0000 | [diff] [blame] | 1569 | ++(*pIx); |
drh | daf2761 | 2020-12-10 20:31:25 +0000 | [diff] [blame] | 1570 | pIter->u.lx.pIdx = pIter->u.lx.pIdx->pNext; |
| 1571 | return pIter->u.lx.pIdx; |
| 1572 | } |
| 1573 | } |
| 1574 | |
| 1575 | /* |
drh | 6934fc7 | 2013-10-31 15:37:49 +0000 | [diff] [blame] | 1576 | ** Generate code to do constraint checks prior to an INSERT or an UPDATE |
| 1577 | ** on table pTab. |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 1578 | ** |
drh | 6934fc7 | 2013-10-31 15:37:49 +0000 | [diff] [blame] | 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. |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 1586 | ** |
drh | f8ffb27 | 2013-11-01 17:08:56 +0000 | [diff] [blame] | 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. |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 1591 | ** |
drh | f8ffb27 | 2013-11-01 17:08:56 +0000 | [diff] [blame] | 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. |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 1596 | ** |
drh | f8ffb27 | 2013-11-01 17:08:56 +0000 | [diff] [blame] | 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. |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 1603 | ** |
drh | 6934fc7 | 2013-10-31 15:37:49 +0000 | [diff] [blame] | 1604 | ** The code generated by this routine will store new index entries into |
drh | aa9b896 | 2008-01-08 02:57:55 +0000 | [diff] [blame] | 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 |
drh | 6934fc7 | 2013-10-31 15:37:49 +0000 | [diff] [blame] | 1608 | ** at pTab->pIndex. |
| 1609 | ** |
drh | a7c3b93 | 2019-05-07 19:13:42 +0000 | [diff] [blame] | 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. |
| 1617 | ** |
drh | 6934fc7 | 2013-10-31 15:37:49 +0000 | [diff] [blame] | 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. |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 1625 | ** |
| 1626 | ** This routine also generates code to check constraints. NOT NULL, |
| 1627 | ** CHECK, and UNIQUE constraints are all checked. If a constraint fails, |
drh | 1c92853 | 2002-01-31 15:54:21 +0000 | [diff] [blame] | 1628 | ** then the appropriate action is performed. There are five possible |
| 1629 | ** actions: ROLLBACK, ABORT, FAIL, REPLACE, and IGNORE. |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 1630 | ** |
| 1631 | ** Constraint type Action What Happens |
| 1632 | ** --------------- ---------- ---------------------------------------- |
drh | 1c92853 | 2002-01-31 15:54:21 +0000 | [diff] [blame] | 1633 | ** any ROLLBACK The current transaction is rolled back and |
drh | 6934fc7 | 2013-10-31 15:37:49 +0000 | [diff] [blame] | 1634 | ** sqlite3_step() returns immediately with a |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 1635 | ** return code of SQLITE_CONSTRAINT. |
| 1636 | ** |
drh | 1c92853 | 2002-01-31 15:54:21 +0000 | [diff] [blame] | 1637 | ** any ABORT Back out changes from the current command |
| 1638 | ** only (do not do a complete rollback) then |
drh | 6934fc7 | 2013-10-31 15:37:49 +0000 | [diff] [blame] | 1639 | ** cause sqlite3_step() to return immediately |
drh | 1c92853 | 2002-01-31 15:54:21 +0000 | [diff] [blame] | 1640 | ** with SQLITE_CONSTRAINT. |
| 1641 | ** |
drh | 6934fc7 | 2013-10-31 15:37:49 +0000 | [diff] [blame] | 1642 | ** any FAIL Sqlite3_step() returns immediately with a |
drh | 1c92853 | 2002-01-31 15:54:21 +0000 | [diff] [blame] | 1643 | ** return code of SQLITE_CONSTRAINT. The |
| 1644 | ** transaction is not rolled back and any |
drh | 6934fc7 | 2013-10-31 15:37:49 +0000 | [diff] [blame] | 1645 | ** changes to prior rows are retained. |
drh | 1c92853 | 2002-01-31 15:54:21 +0000 | [diff] [blame] | 1646 | ** |
drh | 6934fc7 | 2013-10-31 15:37:49 +0000 | [diff] [blame] | 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.) |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 1651 | ** |
| 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. |
| 1655 | ** |
| 1656 | ** UNIQUE REPLACE The other row that conflicts with the row |
| 1657 | ** being inserted is removed. |
| 1658 | ** |
| 1659 | ** CHECK REPLACE Illegal. The results in an exception. |
| 1660 | ** |
drh | 1c92853 | 2002-01-31 15:54:21 +0000 | [diff] [blame] | 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. |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 1665 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1666 | void sqlite3GenerateConstraintChecks( |
drh | 6934fc7 | 2013-10-31 15:37:49 +0000 | [diff] [blame] | 1667 | Parse *pParse, /* The parser context */ |
| 1668 | Table *pTab, /* The table being inserted or updated */ |
drh | f8ffb27 | 2013-11-01 17:08:56 +0000 | [diff] [blame] | 1669 | int *aRegIdx, /* Use register aRegIdx[i] for index i. 0 for unused */ |
drh | 6934fc7 | 2013-10-31 15:37:49 +0000 | [diff] [blame] | 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 */ |
drh | f8ffb27 | 2013-11-01 17:08:56 +0000 | [diff] [blame] | 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 */ |
drh | 6934fc7 | 2013-10-31 15:37:49 +0000 | [diff] [blame] | 1676 | int ignoreDest, /* Jump to this label on an OE_Ignore resolution */ |
drh | bdb0022 | 2016-02-10 16:03:20 +0000 | [diff] [blame] | 1677 | int *pbMayReplace, /* OUT: Set to true if constraint may cause a replace */ |
drh | 788d55a | 2018-04-13 01:15:09 +0000 | [diff] [blame] | 1678 | int *aiChng, /* column i is unchanged if aiChng[i]<0 */ |
| 1679 | Upsert *pUpsert /* ON CONFLICT clauses, if any. NULL otherwise */ |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 1680 | ){ |
drh | 6934fc7 | 2013-10-31 15:37:49 +0000 | [diff] [blame] | 1681 | Vdbe *v; /* VDBE under constrution */ |
drh | 1b7ecbb | 2009-05-03 01:00:59 +0000 | [diff] [blame] | 1682 | Index *pIdx; /* Pointer to one of the indices */ |
drh | e84ad92 | 2020-12-09 20:30:47 +0000 | [diff] [blame] | 1683 | Index *pPk = 0; /* The PRIMARY KEY index for WITHOUT ROWID tables */ |
drh | 2938f92 | 2012-03-07 19:13:29 +0000 | [diff] [blame] | 1684 | sqlite3 *db; /* Database connection */ |
drh | f8ffb27 | 2013-11-01 17:08:56 +0000 | [diff] [blame] | 1685 | int i; /* loop counter */ |
| 1686 | int ix; /* Index loop counter */ |
| 1687 | int nCol; /* Number of columns */ |
| 1688 | int onError; /* Conflict resolution strategy */ |
drh | 1b7ecbb | 2009-05-03 01:00:59 +0000 | [diff] [blame] | 1689 | int seenReplace = 0; /* True if REPLACE is used to resolve INT PK conflict */ |
drh | 6fbe41a | 2013-10-30 20:22:55 +0000 | [diff] [blame] | 1690 | int nPkField; /* Number of fields in PRIMARY KEY. 1 for ROWID tables */ |
drh | 61e280a | 2020-12-11 01:17:06 +0000 | [diff] [blame] | 1691 | Upsert *pUpsertClause = 0; /* The specific ON CONFLICT clause for pIdx */ |
| 1692 | u8 isUpdate; /* True if this is an UPDATE operation */ |
drh | 57bf4a8 | 2014-02-17 14:59:22 +0000 | [diff] [blame] | 1693 | u8 bAffinityDone = 0; /* True if the OP_Affinity operation has been run */ |
drh | 61e280a | 2020-12-11 01:17:06 +0000 | [diff] [blame] | 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 */ |
drh | 8430450 | 2018-08-14 15:12:52 +0000 | [diff] [blame] | 1696 | int ipkTop = 0; /* Top of the IPK uniqueness check */ |
| 1697 | int ipkBottom = 0; /* OP_Goto at the end of the IPK uniqueness check */ |
drh | a407ecc | 2019-10-26 00:04:21 +0000 | [diff] [blame] | 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 */ |
drh | 61e280a | 2020-12-11 01:17:06 +0000 | [diff] [blame] | 1705 | IndexIterator sIdxIter; /* Index iterator */ |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 1706 | |
drh | f8ffb27 | 2013-11-01 17:08:56 +0000 | [diff] [blame] | 1707 | isUpdate = regOldData!=0; |
drh | 2938f92 | 2012-03-07 19:13:29 +0000 | [diff] [blame] | 1708 | db = pParse->db; |
drh | f0b4174 | 2020-08-15 21:55:14 +0000 | [diff] [blame] | 1709 | v = pParse->pVdbe; |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 1710 | assert( v!=0 ); |
drh | f38524d | 2021-08-02 16:41:57 +0000 | [diff] [blame] | 1711 | assert( !IsView(pTab) ); /* This table is not a VIEW */ |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 1712 | nCol = pTab->nCol; |
drh | 6934fc7 | 2013-10-31 15:37:49 +0000 | [diff] [blame] | 1713 | |
| 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. */ |
drh | 26198bb | 2013-10-31 11:15:09 +0000 | [diff] [blame] | 1718 | if( HasRowid(pTab) ){ |
| 1719 | pPk = 0; |
| 1720 | nPkField = 1; |
| 1721 | }else{ |
| 1722 | pPk = sqlite3PrimaryKeyIndex(pTab); |
| 1723 | nPkField = pPk->nKeyCol; |
| 1724 | } |
drh | 6fbe41a | 2013-10-30 20:22:55 +0000 | [diff] [blame] | 1725 | |
| 1726 | /* Record that this module has started */ |
| 1727 | VdbeModuleComment((v, "BEGIN: GenCnstCks(%d,%d,%d,%d,%d)", |
drh | 6934fc7 | 2013-10-31 15:37:49 +0000 | [diff] [blame] | 1728 | iDataCur, iIdxCur, regNewData, regOldData, pkChng)); |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 1729 | |
| 1730 | /* Test all NOT NULL constraints. |
| 1731 | */ |
drh | cbda9c7 | 2019-10-26 17:08:06 +0000 | [diff] [blame] | 1732 | if( pTab->tabFlags & TF_HasNotNull ){ |
drh | ad5f157 | 2019-12-28 00:36:51 +0000 | [diff] [blame] | 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 */ |
| 1745 | } |
| 1746 | isGenerated = pCol->colFlags & COLFLAG_GENERATED; |
| 1747 | if( isGenerated && !b2ndPass ){ |
| 1748 | nGenerated++; |
| 1749 | continue; /* Generated columns processed on 2nd pass */ |
| 1750 | } |
| 1751 | if( aiChng && aiChng[i]<0 && !isGenerated ){ |
| 1752 | /* Do not check NOT NULL on columns that do not change */ |
| 1753 | continue; |
| 1754 | } |
| 1755 | if( overrideError!=OE_Default ){ |
| 1756 | onError = overrideError; |
| 1757 | }else if( onError==OE_Default ){ |
drh | cbda9c7 | 2019-10-26 17:08:06 +0000 | [diff] [blame] | 1758 | onError = OE_Abort; |
drh | cbda9c7 | 2019-10-26 17:08:06 +0000 | [diff] [blame] | 1759 | } |
drh | ad5f157 | 2019-12-28 00:36:51 +0000 | [diff] [blame] | 1760 | if( onError==OE_Replace ){ |
| 1761 | if( b2ndPass /* REPLACE becomes ABORT on the 2nd pass */ |
drh | 79cf2b7 | 2021-07-31 20:30:41 +0000 | [diff] [blame] | 1762 | || pCol->iDflt==0 /* REPLACE is ABORT if no DEFAULT value */ |
drh | ad5f157 | 2019-12-28 00:36:51 +0000 | [diff] [blame] | 1763 | ){ |
| 1764 | testcase( pCol->colFlags & COLFLAG_VIRTUAL ); |
| 1765 | testcase( pCol->colFlags & COLFLAG_STORED ); |
| 1766 | testcase( pCol->colFlags & COLFLAG_GENERATED ); |
| 1767 | onError = OE_Abort; |
| 1768 | }else{ |
| 1769 | assert( !isGenerated ); |
| 1770 | } |
| 1771 | }else if( b2ndPass && !isGenerated ){ |
| 1772 | continue; |
drh | cbda9c7 | 2019-10-26 17:08:06 +0000 | [diff] [blame] | 1773 | } |
drh | ad5f157 | 2019-12-28 00:36:51 +0000 | [diff] [blame] | 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; |
| 1778 | switch( onError ){ |
| 1779 | case OE_Replace: { |
| 1780 | int addr1 = sqlite3VdbeAddOp1(v, OP_NotNull, iReg); |
| 1781 | VdbeCoverage(v); |
| 1782 | assert( (pCol->colFlags & COLFLAG_GENERATED)==0 ); |
| 1783 | nSeenReplace++; |
drh | 79cf2b7 | 2021-07-31 20:30:41 +0000 | [diff] [blame] | 1784 | sqlite3ExprCodeCopy(pParse, |
| 1785 | sqlite3ColumnExpr(pTab, pCol), iReg); |
drh | ad5f157 | 2019-12-28 00:36:51 +0000 | [diff] [blame] | 1786 | sqlite3VdbeJumpHere(v, addr1); |
| 1787 | break; |
| 1788 | } |
| 1789 | case OE_Abort: |
| 1790 | sqlite3MayAbort(pParse); |
drh | 08b9208 | 2020-08-10 14:18:00 +0000 | [diff] [blame] | 1791 | /* no break */ deliberate_fall_through |
drh | ad5f157 | 2019-12-28 00:36:51 +0000 | [diff] [blame] | 1792 | case OE_Rollback: |
| 1793 | case OE_Fail: { |
| 1794 | char *zMsg = sqlite3MPrintf(db, "%s.%s", pTab->zName, |
drh | cf9d36d | 2021-08-02 18:03:43 +0000 | [diff] [blame] | 1795 | pCol->zCnName); |
drh | ad5f157 | 2019-12-28 00:36:51 +0000 | [diff] [blame] | 1796 | sqlite3VdbeAddOp3(v, OP_HaltIfNull, SQLITE_CONSTRAINT_NOTNULL, |
| 1797 | onError, iReg); |
| 1798 | sqlite3VdbeAppendP4(v, zMsg, P4_DYNAMIC); |
| 1799 | sqlite3VdbeChangeP5(v, P5_ConstraintNotNull); |
| 1800 | VdbeCoverage(v); |
| 1801 | break; |
| 1802 | } |
| 1803 | default: { |
| 1804 | assert( onError==OE_Ignore ); |
| 1805 | sqlite3VdbeAddOp2(v, OP_IsNull, iReg, ignoreDest); |
| 1806 | VdbeCoverage(v); |
| 1807 | break; |
| 1808 | } |
| 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 */ |
| 1815 | break; |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 1816 | } |
drh | ad5f157 | 2019-12-28 00:36:51 +0000 | [diff] [blame] | 1817 | if( b2ndPass ) break; /* Never need more than 2 passes */ |
| 1818 | b2ndPass = 1; |
drh | ef9f719 | 2020-01-17 19:14:08 +0000 | [diff] [blame] | 1819 | #ifndef SQLITE_OMIT_GENERATED_COLUMNS |
drh | ad5f157 | 2019-12-28 00:36:51 +0000 | [diff] [blame] | 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. |
| 1824 | */ |
| 1825 | sqlite3ComputeGeneratedColumns(pParse, regNewData+1, pTab); |
| 1826 | } |
drh | ef9f719 | 2020-01-17 19:14:08 +0000 | [diff] [blame] | 1827 | #endif |
drh | ad5f157 | 2019-12-28 00:36:51 +0000 | [diff] [blame] | 1828 | } /* end of 2-pass loop */ |
| 1829 | } /* end if( has-not-null-constraints ) */ |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 1830 | |
| 1831 | /* Test all CHECK constraints |
| 1832 | */ |
drh | ffe07b2 | 2005-11-03 00:41:17 +0000 | [diff] [blame] | 1833 | #ifndef SQLITE_OMIT_CHECK |
drh | 2938f92 | 2012-03-07 19:13:29 +0000 | [diff] [blame] | 1834 | if( pTab->pCheck && (db->flags & SQLITE_IgnoreChecks)==0 ){ |
| 1835 | ExprList *pCheck = pTab->pCheck; |
drh | 6e97f8e | 2017-07-20 13:17:08 +0000 | [diff] [blame] | 1836 | pParse->iSelfTab = -(regNewData+1); |
drh | aa01c7e | 2006-03-15 16:26:10 +0000 | [diff] [blame] | 1837 | onError = overrideError!=OE_Default ? overrideError : OE_Abort; |
drh | 2938f92 | 2012-03-07 19:13:29 +0000 | [diff] [blame] | 1838 | for(i=0; i<pCheck->nExpr; i++){ |
drh | 05723a9 | 2016-02-10 19:10:50 +0000 | [diff] [blame] | 1839 | int allOk; |
drh | 5cf1b61 | 2020-03-10 18:55:41 +0000 | [diff] [blame] | 1840 | Expr *pCopy; |
drh | 2a0b527 | 2016-02-10 16:52:24 +0000 | [diff] [blame] | 1841 | Expr *pExpr = pCheck->a[i].pExpr; |
drh | e9816d8 | 2018-09-15 21:38:48 +0000 | [diff] [blame] | 1842 | if( aiChng |
| 1843 | && !sqlite3ExprReferencesUpdatedColumn(pExpr, aiChng, pkChng) |
| 1844 | ){ |
| 1845 | /* The check constraints do not reference any of the columns being |
| 1846 | ** updated so there is no point it verifying the check constraint */ |
| 1847 | continue; |
| 1848 | } |
drh | 9dce0ef | 2020-02-01 21:03:27 +0000 | [diff] [blame] | 1849 | if( bAffinityDone==0 ){ |
| 1850 | sqlite3TableAffinity(v, pTab, regNewData+1); |
| 1851 | bAffinityDone = 1; |
| 1852 | } |
drh | ec4ccdb | 2018-12-29 02:26:59 +0000 | [diff] [blame] | 1853 | allOk = sqlite3VdbeMakeLabel(pParse); |
drh | 4031baf | 2018-05-28 17:31:20 +0000 | [diff] [blame] | 1854 | sqlite3VdbeVerifyAbortable(v, onError); |
drh | 5cf1b61 | 2020-03-10 18:55:41 +0000 | [diff] [blame] | 1855 | pCopy = sqlite3ExprDup(db, pExpr, 0); |
| 1856 | if( !db->mallocFailed ){ |
| 1857 | sqlite3ExprIfTrue(pParse, pCopy, allOk, SQLITE_JUMPIFNULL); |
| 1858 | } |
| 1859 | sqlite3ExprDelete(db, pCopy); |
drh | 2d8e920 | 2012-12-08 04:10:44 +0000 | [diff] [blame] | 1860 | if( onError==OE_Ignore ){ |
drh | 076e85f | 2015-09-03 13:46:12 +0000 | [diff] [blame] | 1861 | sqlite3VdbeGoto(v, ignoreDest); |
drh | 2d8e920 | 2012-12-08 04:10:44 +0000 | [diff] [blame] | 1862 | }else{ |
drh | 41cee66 | 2019-12-12 20:22:34 +0000 | [diff] [blame] | 1863 | char *zName = pCheck->a[i].zEName; |
drh | e2678b9 | 2020-08-28 12:58:21 +0000 | [diff] [blame] | 1864 | assert( zName!=0 || pParse->db->mallocFailed ); |
drh | 0ce974d | 2019-06-12 22:46:04 +0000 | [diff] [blame] | 1865 | if( onError==OE_Replace ) onError = OE_Abort; /* IMP: R-26383-51744 */ |
drh | d91c1a1 | 2013-02-09 13:58:25 +0000 | [diff] [blame] | 1866 | sqlite3HaltConstraint(pParse, SQLITE_CONSTRAINT_CHECK, |
drh | f9c8ce3 | 2013-11-05 13:33:55 +0000 | [diff] [blame] | 1867 | onError, zName, P4_TRANSIENT, |
| 1868 | P5_ConstraintCheck); |
drh | 2938f92 | 2012-03-07 19:13:29 +0000 | [diff] [blame] | 1869 | } |
drh | 2d8e920 | 2012-12-08 04:10:44 +0000 | [diff] [blame] | 1870 | sqlite3VdbeResolveLabel(v, allOk); |
drh | aa01c7e | 2006-03-15 16:26:10 +0000 | [diff] [blame] | 1871 | } |
drh | 6e97f8e | 2017-07-20 13:17:08 +0000 | [diff] [blame] | 1872 | pParse->iSelfTab = 0; |
drh | ffe07b2 | 2005-11-03 00:41:17 +0000 | [diff] [blame] | 1873 | } |
| 1874 | #endif /* !defined(SQLITE_OMIT_CHECK) */ |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 1875 | |
drh | 096fd47 | 2018-04-14 20:24:36 +0000 | [diff] [blame] | 1876 | /* UNIQUE and PRIMARY KEY constraints should be handled in the following |
| 1877 | ** order: |
| 1878 | ** |
drh | 8430450 | 2018-08-14 15:12:52 +0000 | [diff] [blame] | 1879 | ** (1) OE_Update |
| 1880 | ** (2) OE_Abort, OE_Fail, OE_Rollback, OE_Ignore |
drh | 096fd47 | 2018-04-14 20:24:36 +0000 | [diff] [blame] | 1881 | ** (3) OE_Replace |
| 1882 | ** |
| 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 |
| 1887 | ** convenience. |
| 1888 | ** |
drh | 8430450 | 2018-08-14 15:12:52 +0000 | [diff] [blame] | 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. |
| 1893 | ** |
drh | 096fd47 | 2018-04-14 20:24:36 +0000 | [diff] [blame] | 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. |
| 1899 | ** |
| 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. |
| 1903 | */ |
drh | 61e280a | 2020-12-11 01:17:06 +0000 | [diff] [blame] | 1904 | sIdxIter.eType = 0; |
| 1905 | sIdxIter.i = 0; |
drh | d3e21a1 | 2020-12-11 17:11:56 +0000 | [diff] [blame] | 1906 | sIdxIter.u.ax.aIdx = 0; /* Silence harmless compiler warning */ |
drh | 61e280a | 2020-12-11 01:17:06 +0000 | [diff] [blame] | 1907 | sIdxIter.u.lx.pIdx = pTab->pIndex; |
drh | 096fd47 | 2018-04-14 20:24:36 +0000 | [diff] [blame] | 1908 | if( pUpsert ){ |
| 1909 | if( pUpsert->pUpsertTarget==0 ){ |
drh | 61e280a | 2020-12-11 01:17:06 +0000 | [diff] [blame] | 1910 | /* There is just on ON CONFLICT clause and it has no constraint-target */ |
| 1911 | assert( pUpsert->pNextUpsert==0 ); |
drh | 255c1c1 | 2020-12-12 00:28:15 +0000 | [diff] [blame] | 1912 | if( pUpsert->isDoUpdate==0 ){ |
drh | 61e280a | 2020-12-11 01:17:06 +0000 | [diff] [blame] | 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; |
| 1916 | pUpsert = 0; |
| 1917 | }else{ |
| 1918 | /* A single ON CONFLICT DO UPDATE. Make all resolutions OE_Update */ |
| 1919 | overrideError = OE_Update; |
| 1920 | } |
| 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. */ |
| 1925 | int nIdx, jj; |
| 1926 | u64 nByte; |
| 1927 | Upsert *pTerm; |
| 1928 | u8 *bUsed; |
| 1929 | for(nIdx=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, nIdx++){ |
| 1930 | assert( aRegIdx[nIdx]>0 ); |
| 1931 | } |
| 1932 | sIdxIter.eType = 1; |
| 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 */ |
| 1942 | jj = 0; |
| 1943 | pIdx = pTab->pIndex; |
| 1944 | while( ALWAYS(pIdx!=0) && pIdx!=pTerm->pUpsertIdx ){ |
| 1945 | pIdx = pIdx->pNext; |
| 1946 | jj++; |
| 1947 | } |
| 1948 | if( bUsed[jj] ) continue; /* Duplicate ON CONFLICT clause ignored */ |
| 1949 | bUsed[jj] = 1; |
| 1950 | sIdxIter.u.ax.aIdx[i].p = pIdx; |
| 1951 | sIdxIter.u.ax.aIdx[i].ix = jj; |
| 1952 | i++; |
| 1953 | } |
| 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; |
| 1958 | i++; |
| 1959 | } |
| 1960 | assert( i==nIdx ); |
drh | 096fd47 | 2018-04-14 20:24:36 +0000 | [diff] [blame] | 1961 | } |
| 1962 | } |
| 1963 | |
drh | a407ecc | 2019-10-26 00:04:21 +0000 | [diff] [blame] | 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. |
| 1970 | ** |
| 1971 | ** If replace triggers are a possibility, then |
| 1972 | ** |
| 1973 | ** (1) Allocate register regTrigCnt and initialize it to zero. |
| 1974 | ** That register will count the number of replace triggers that |
drh | d3c468b | 2019-10-26 16:38:49 +0000 | [diff] [blame] | 1975 | ** fire. Constraint recheck only occurs if the number is positive. |
| 1976 | ** (2) Initialize pTrigger to the list of all DELETE triggers on pTab. |
drh | a407ecc | 2019-10-26 00:04:21 +0000 | [diff] [blame] | 1977 | ** (3) Initialize addrRecheck and lblRecheckOk |
| 1978 | ** |
| 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. |
| 1983 | */ |
| 1984 | if( (db->flags & (SQLITE_RecTriggers|SQLITE_ForeignKeys))==0 ){ |
| 1985 | /* There are not DELETE triggers nor FK constraints. No constraint |
| 1986 | ** rechecks are needed. */ |
| 1987 | pTrigger = 0; |
| 1988 | regTrigCnt = 0; |
| 1989 | }else{ |
| 1990 | if( db->flags&SQLITE_RecTriggers ){ |
| 1991 | pTrigger = sqlite3TriggersExist(pParse, pTab, TK_DELETE, 0, 0); |
| 1992 | regTrigCnt = pTrigger!=0 || sqlite3FkRequired(pParse, pTab, 0, 0); |
| 1993 | }else{ |
| 1994 | pTrigger = 0; |
| 1995 | regTrigCnt = sqlite3FkRequired(pParse, pTab, 0, 0); |
| 1996 | } |
| 1997 | if( regTrigCnt ){ |
| 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; |
| 2005 | } |
| 2006 | } |
| 2007 | |
drh | f8ffb27 | 2013-11-01 17:08:56 +0000 | [diff] [blame] | 2008 | /* If rowid is changing, make sure the new rowid does not previously |
| 2009 | ** exist in the table. |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 2010 | */ |
drh | 6fbe41a | 2013-10-30 20:22:55 +0000 | [diff] [blame] | 2011 | if( pkChng && pPk==0 ){ |
drh | ec4ccdb | 2018-12-29 02:26:59 +0000 | [diff] [blame] | 2012 | int addrRowidOk = sqlite3VdbeMakeLabel(pParse); |
drh | 11e8527 | 2013-10-26 15:40:48 +0000 | [diff] [blame] | 2013 | |
drh | f8ffb27 | 2013-11-01 17:08:56 +0000 | [diff] [blame] | 2014 | /* Figure out what action to take in case of a rowid collision */ |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 2015 | onError = pTab->keyConf; |
| 2016 | if( overrideError!=OE_Default ){ |
| 2017 | onError = overrideError; |
drh | a996e47 | 2003-05-16 02:30:27 +0000 | [diff] [blame] | 2018 | }else if( onError==OE_Default ){ |
| 2019 | onError = OE_Abort; |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 2020 | } |
drh | 11e8527 | 2013-10-26 15:40:48 +0000 | [diff] [blame] | 2021 | |
drh | c8a0c90 | 2018-04-13 15:14:33 +0000 | [diff] [blame] | 2022 | /* figure out whether or not upsert applies in this case */ |
drh | 61e280a | 2020-12-11 01:17:06 +0000 | [diff] [blame] | 2023 | if( pUpsert ){ |
| 2024 | pUpsertClause = sqlite3UpsertOfIndex(pUpsert,0); |
| 2025 | if( pUpsertClause!=0 ){ |
drh | 255c1c1 | 2020-12-12 00:28:15 +0000 | [diff] [blame] | 2026 | if( pUpsertClause->isDoUpdate==0 ){ |
drh | 61e280a | 2020-12-11 01:17:06 +0000 | [diff] [blame] | 2027 | onError = OE_Ignore; /* DO NOTHING is the same as INSERT OR IGNORE */ |
| 2028 | }else{ |
| 2029 | onError = OE_Update; /* DO UPDATE */ |
| 2030 | } |
| 2031 | } |
| 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); |
drh | c8a0c90 | 2018-04-13 15:14:33 +0000 | [diff] [blame] | 2037 | } |
| 2038 | } |
| 2039 | |
drh | 8d1b82e | 2013-11-05 19:41:32 +0000 | [diff] [blame] | 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. |
| 2044 | */ |
drh | 8430450 | 2018-08-14 15:12:52 +0000 | [diff] [blame] | 2045 | if( onError==OE_Replace /* IPK rule is REPLACE */ |
mistachkin | 9a60e71 | 2020-12-22 19:57:53 +0000 | [diff] [blame] | 2046 | && onError!=overrideError /* Rules for other constraints are different */ |
drh | 8430450 | 2018-08-14 15:12:52 +0000 | [diff] [blame] | 2047 | && pTab->pIndex /* There exist other constraints */ |
drh | 66306d8 | 2021-12-30 02:38:43 +0000 | [diff] [blame] | 2048 | && !upsertIpkDelay /* IPK check already deferred by UPSERT */ |
drh | 096fd47 | 2018-04-14 20:24:36 +0000 | [diff] [blame] | 2049 | ){ |
drh | 8430450 | 2018-08-14 15:12:52 +0000 | [diff] [blame] | 2050 | ipkTop = sqlite3VdbeAddOp0(v, OP_Goto)+1; |
| 2051 | VdbeComment((v, "defer IPK REPLACE until last")); |
drh | 8d1b82e | 2013-11-05 19:41:32 +0000 | [diff] [blame] | 2052 | } |
| 2053 | |
drh | bb6b1ca | 2018-04-19 13:52:39 +0000 | [diff] [blame] | 2054 | if( isUpdate ){ |
| 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 |
| 2057 | ** is unchanged. */ |
| 2058 | sqlite3VdbeAddOp3(v, OP_Eq, regNewData, addrRowidOk, regOldData); |
| 2059 | sqlite3VdbeChangeP5(v, SQLITE_NOTNULL); |
| 2060 | VdbeCoverage(v); |
| 2061 | } |
| 2062 | |
drh | f8ffb27 | 2013-11-01 17:08:56 +0000 | [diff] [blame] | 2063 | /* Check to see if the new rowid already exists in the table. Skip |
| 2064 | ** the following conflict logic if it does not. */ |
drh | 7f5f306 | 2018-04-17 20:06:24 +0000 | [diff] [blame] | 2065 | VdbeNoopComment((v, "uniqueness check for ROWID")); |
drh | 4031baf | 2018-05-28 17:31:20 +0000 | [diff] [blame] | 2066 | sqlite3VdbeVerifyAbortable(v, onError); |
drh | 6934fc7 | 2013-10-31 15:37:49 +0000 | [diff] [blame] | 2067 | sqlite3VdbeAddOp3(v, OP_NotExists, iDataCur, addrRowidOk, regNewData); |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 2068 | VdbeCoverage(v); |
drh | f8ffb27 | 2013-11-01 17:08:56 +0000 | [diff] [blame] | 2069 | |
dan | e1e2ae2 | 2009-09-24 16:52:28 +0000 | [diff] [blame] | 2070 | switch( onError ){ |
| 2071 | default: { |
| 2072 | onError = OE_Abort; |
drh | 08b9208 | 2020-08-10 14:18:00 +0000 | [diff] [blame] | 2073 | /* no break */ deliberate_fall_through |
drh | 79b0c95 | 2002-05-21 12:56:43 +0000 | [diff] [blame] | 2074 | } |
dan | e1e2ae2 | 2009-09-24 16:52:28 +0000 | [diff] [blame] | 2075 | case OE_Rollback: |
| 2076 | case OE_Abort: |
| 2077 | case OE_Fail: { |
drh | 9916048 | 2018-04-18 01:34:39 +0000 | [diff] [blame] | 2078 | testcase( onError==OE_Rollback ); |
| 2079 | testcase( onError==OE_Abort ); |
| 2080 | testcase( onError==OE_Fail ); |
drh | f9c8ce3 | 2013-11-05 13:33:55 +0000 | [diff] [blame] | 2081 | sqlite3RowidConstraint(pParse, onError, pTab); |
dan | e1e2ae2 | 2009-09-24 16:52:28 +0000 | [diff] [blame] | 2082 | break; |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 2083 | } |
dan | e1e2ae2 | 2009-09-24 16:52:28 +0000 | [diff] [blame] | 2084 | case OE_Replace: { |
| 2085 | /* If there are DELETE triggers on this table and the |
| 2086 | ** recursive-triggers flag is set, call GenerateRowDelete() to |
mistachkin | d557843 | 2012-08-25 10:01:29 +0000 | [diff] [blame] | 2087 | ** remove the conflicting row from the table. This will fire |
dan | e1e2ae2 | 2009-09-24 16:52:28 +0000 | [diff] [blame] | 2088 | ** the triggers and remove both the table and index b-tree entries. |
| 2089 | ** |
| 2090 | ** Otherwise, if there are no triggers or the recursive-triggers |
dan | da730f6 | 2010-02-18 08:19:19 +0000 | [diff] [blame] | 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. |
| 2095 | ** |
| 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: |
| 2101 | ** |
| 2102 | ** REPLACE INTO t(rowid) VALUES($newrowid) |
| 2103 | ** |
| 2104 | ** to run without a statement journal if there are no indexes on the |
| 2105 | ** table. |
| 2106 | */ |
drh | a407ecc | 2019-10-26 00:04:21 +0000 | [diff] [blame] | 2107 | if( regTrigCnt ){ |
dan | da730f6 | 2010-02-18 08:19:19 +0000 | [diff] [blame] | 2108 | sqlite3MultiWrite(pParse); |
drh | 26198bb | 2013-10-31 11:15:09 +0000 | [diff] [blame] | 2109 | sqlite3GenerateRowDelete(pParse, pTab, pTrigger, iDataCur, iIdxCur, |
dan | 438b881 | 2015-09-15 15:55:15 +0000 | [diff] [blame] | 2110 | regNewData, 1, 0, OE_Replace, 1, -1); |
drh | a407ecc | 2019-10-26 00:04:21 +0000 | [diff] [blame] | 2111 | sqlite3VdbeAddOp2(v, OP_AddImm, regTrigCnt, 1); /* incr trigger cnt */ |
| 2112 | nReplaceTrig++; |
dan | 46c47d4 | 2011-03-01 18:42:07 +0000 | [diff] [blame] | 2113 | }else{ |
drh | 9b1c62d | 2011-03-30 21:04:43 +0000 | [diff] [blame] | 2114 | #ifdef SQLITE_ENABLE_PREUPDATE_HOOK |
drh | 54f2cd9 | 2018-04-13 16:23:22 +0000 | [diff] [blame] | 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); |
drh | 9b1c62d | 2011-03-30 21:04:43 +0000 | [diff] [blame] | 2122 | #endif /* SQLITE_ENABLE_PREUPDATE_HOOK */ |
dan | 46c47d4 | 2011-03-01 18:42:07 +0000 | [diff] [blame] | 2123 | if( pTab->pIndex ){ |
| 2124 | sqlite3MultiWrite(pParse); |
drh | b77ebd8 | 2015-09-15 13:42:16 +0000 | [diff] [blame] | 2125 | sqlite3GenerateRowIndexDelete(pParse, pTab, iDataCur, iIdxCur,0,-1); |
dan | 46c47d4 | 2011-03-01 18:42:07 +0000 | [diff] [blame] | 2126 | } |
dan | e1e2ae2 | 2009-09-24 16:52:28 +0000 | [diff] [blame] | 2127 | } |
| 2128 | seenReplace = 1; |
| 2129 | break; |
drh | 5383ae5 | 2003-06-04 12:23:30 +0000 | [diff] [blame] | 2130 | } |
drh | 9eddaca | 2018-04-13 18:59:17 +0000 | [diff] [blame] | 2131 | #ifndef SQLITE_OMIT_UPSERT |
| 2132 | case OE_Update: { |
dan | 2cc0042 | 2018-04-17 18:16:10 +0000 | [diff] [blame] | 2133 | sqlite3UpsertDoUpdate(pParse, pUpsert, pTab, 0, iDataCur); |
drh | 08b9208 | 2020-08-10 14:18:00 +0000 | [diff] [blame] | 2134 | /* no break */ deliberate_fall_through |
drh | 9eddaca | 2018-04-13 18:59:17 +0000 | [diff] [blame] | 2135 | } |
| 2136 | #endif |
dan | e1e2ae2 | 2009-09-24 16:52:28 +0000 | [diff] [blame] | 2137 | case OE_Ignore: { |
drh | 9916048 | 2018-04-18 01:34:39 +0000 | [diff] [blame] | 2138 | testcase( onError==OE_Ignore ); |
drh | 076e85f | 2015-09-03 13:46:12 +0000 | [diff] [blame] | 2139 | sqlite3VdbeGoto(v, ignoreDest); |
dan | e1e2ae2 | 2009-09-24 16:52:28 +0000 | [diff] [blame] | 2140 | break; |
| 2141 | } |
| 2142 | } |
drh | 11e8527 | 2013-10-26 15:40:48 +0000 | [diff] [blame] | 2143 | sqlite3VdbeResolveLabel(v, addrRowidOk); |
drh | 61e280a | 2020-12-11 01:17:06 +0000 | [diff] [blame] | 2144 | if( pUpsert && pUpsertClause!=pUpsert ){ |
| 2145 | upsertIpkReturn = sqlite3VdbeAddOp0(v, OP_Goto); |
| 2146 | }else if( ipkTop ){ |
drh | 8430450 | 2018-08-14 15:12:52 +0000 | [diff] [blame] | 2147 | ipkBottom = sqlite3VdbeAddOp0(v, OP_Goto); |
| 2148 | sqlite3VdbeJumpHere(v, ipkTop-1); |
drh | 5383ae5 | 2003-06-04 12:23:30 +0000 | [diff] [blame] | 2149 | } |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 2150 | } |
drh | 0bd1f4e | 2002-06-06 18:54:39 +0000 | [diff] [blame] | 2151 | |
| 2152 | /* Test all UNIQUE constraints by creating entries for each UNIQUE |
| 2153 | ** index and making sure that duplicate entries do not already exist. |
drh | 11e8527 | 2013-10-26 15:40:48 +0000 | [diff] [blame] | 2154 | ** Compute the revised record entries for indices as we go. |
drh | f8ffb27 | 2013-11-01 17:08:56 +0000 | [diff] [blame] | 2155 | ** |
| 2156 | ** This loop also handles the case of the PRIMARY KEY index for a |
| 2157 | ** WITHOUT ROWID table. |
drh | 0bd1f4e | 2002-06-06 18:54:39 +0000 | [diff] [blame] | 2158 | */ |
drh | 61e280a | 2020-12-11 01:17:06 +0000 | [diff] [blame] | 2159 | for(pIdx = indexIteratorFirst(&sIdxIter, &ix); |
drh | daf2761 | 2020-12-10 20:31:25 +0000 | [diff] [blame] | 2160 | pIdx; |
drh | 61e280a | 2020-12-11 01:17:06 +0000 | [diff] [blame] | 2161 | pIdx = indexIteratorNext(&sIdxIter, &ix) |
drh | daf2761 | 2020-12-10 20:31:25 +0000 | [diff] [blame] | 2162 | ){ |
drh | 6934fc7 | 2013-10-31 15:37:49 +0000 | [diff] [blame] | 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 */ |
drh | a407ecc | 2019-10-26 00:04:21 +0000 | [diff] [blame] | 2167 | int addrConflictCk; /* First opcode in the conflict check logic */ |
drh | 2184fc7 | 2011-04-09 03:04:13 +0000 | [diff] [blame] | 2168 | |
drh | 26198bb | 2013-10-31 11:15:09 +0000 | [diff] [blame] | 2169 | if( aRegIdx[ix]==0 ) continue; /* Skip indices that do not change */ |
drh | 61e280a | 2020-12-11 01:17:06 +0000 | [diff] [blame] | 2170 | if( pUpsert ){ |
| 2171 | pUpsertClause = sqlite3UpsertOfIndex(pUpsert, pIdx); |
| 2172 | if( upsertIpkDelay && pUpsertClause==pUpsert ){ |
| 2173 | sqlite3VdbeJumpHere(v, upsertIpkDelay); |
| 2174 | } |
drh | 7f5f306 | 2018-04-17 20:06:24 +0000 | [diff] [blame] | 2175 | } |
drh | 61e280a | 2020-12-11 01:17:06 +0000 | [diff] [blame] | 2176 | addrUniqueOk = sqlite3VdbeMakeLabel(pParse); |
| 2177 | if( bAffinityDone==0 ){ |
drh | 8430450 | 2018-08-14 15:12:52 +0000 | [diff] [blame] | 2178 | sqlite3TableAffinity(v, pTab, regNewData+1); |
| 2179 | bAffinityDone = 1; |
| 2180 | } |
drh | 8e50d65 | 2020-05-23 17:56:49 +0000 | [diff] [blame] | 2181 | VdbeNoopComment((v, "prep index %s", pIdx->zName)); |
drh | 6934fc7 | 2013-10-31 15:37:49 +0000 | [diff] [blame] | 2182 | iThisCur = iIdxCur+ix; |
drh | 7f5f306 | 2018-04-17 20:06:24 +0000 | [diff] [blame] | 2183 | |
drh | b2fe7d8 | 2003-04-20 17:29:23 +0000 | [diff] [blame] | 2184 | |
drh | f8ffb27 | 2013-11-01 17:08:56 +0000 | [diff] [blame] | 2185 | /* Skip partial indices for which the WHERE clause is not true */ |
drh | b2b9d3d | 2013-08-01 01:14:43 +0000 | [diff] [blame] | 2186 | if( pIdx->pPartIdxWhere ){ |
drh | 26198bb | 2013-10-31 11:15:09 +0000 | [diff] [blame] | 2187 | sqlite3VdbeAddOp2(v, OP_Null, 0, aRegIdx[ix]); |
drh | 6e97f8e | 2017-07-20 13:17:08 +0000 | [diff] [blame] | 2188 | pParse->iSelfTab = -(regNewData+1); |
drh | 72bc820 | 2015-06-11 13:58:35 +0000 | [diff] [blame] | 2189 | sqlite3ExprIfFalseDup(pParse, pIdx->pPartIdxWhere, addrUniqueOk, |
| 2190 | SQLITE_JUMPIFNULL); |
drh | 6e97f8e | 2017-07-20 13:17:08 +0000 | [diff] [blame] | 2191 | pParse->iSelfTab = 0; |
drh | b2b9d3d | 2013-08-01 01:14:43 +0000 | [diff] [blame] | 2192 | } |
| 2193 | |
drh | 6934fc7 | 2013-10-31 15:37:49 +0000 | [diff] [blame] | 2194 | /* Create a record for this index entry as it should appear after |
drh | f8ffb27 | 2013-11-01 17:08:56 +0000 | [diff] [blame] | 2195 | ** the insert or update. Store that record in the aRegIdx[ix] register |
| 2196 | */ |
drh | bf2f573 | 2016-11-10 16:07:43 +0000 | [diff] [blame] | 2197 | regIdx = aRegIdx[ix]+1; |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 2198 | for(i=0; i<pIdx->nColumn; i++){ |
drh | 6934fc7 | 2013-10-31 15:37:49 +0000 | [diff] [blame] | 2199 | int iField = pIdx->aiColumn[i]; |
drh | f82b9af | 2013-11-01 14:03:20 +0000 | [diff] [blame] | 2200 | int x; |
drh | 4b92f98 | 2015-09-29 17:20:14 +0000 | [diff] [blame] | 2201 | if( iField==XN_EXPR ){ |
drh | 6e97f8e | 2017-07-20 13:17:08 +0000 | [diff] [blame] | 2202 | pParse->iSelfTab = -(regNewData+1); |
drh | 1c75c9d | 2015-12-21 15:22:13 +0000 | [diff] [blame] | 2203 | sqlite3ExprCodeCopy(pParse, pIdx->aColExpr->a[i].pExpr, regIdx+i); |
drh | 6e97f8e | 2017-07-20 13:17:08 +0000 | [diff] [blame] | 2204 | pParse->iSelfTab = 0; |
drh | 1f9ca2c | 2015-08-25 16:57:52 +0000 | [diff] [blame] | 2205 | VdbeComment((v, "%s column %d", pIdx->zName, i)); |
drh | 463e76f | 2019-10-18 10:05:06 +0000 | [diff] [blame] | 2206 | }else if( iField==XN_ROWID || iField==pTab->iPKey ){ |
| 2207 | x = regNewData; |
| 2208 | sqlite3VdbeAddOp2(v, OP_IntCopy, x, regIdx+i); |
| 2209 | VdbeComment((v, "rowid")); |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 2210 | }else{ |
drh | c5f808d | 2019-10-19 15:01:52 +0000 | [diff] [blame] | 2211 | testcase( sqlite3TableColumnToStorage(pTab, iField)!=iField ); |
drh | b9bcf7c | 2019-10-19 13:29:10 +0000 | [diff] [blame] | 2212 | x = sqlite3TableColumnToStorage(pTab, iField) + regNewData + 1; |
drh | 463e76f | 2019-10-18 10:05:06 +0000 | [diff] [blame] | 2213 | sqlite3VdbeAddOp2(v, OP_SCopy, x, regIdx+i); |
drh | cf9d36d | 2021-08-02 18:03:43 +0000 | [diff] [blame] | 2214 | VdbeComment((v, "%s", pTab->aCol[iField].zCnName)); |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 2215 | } |
| 2216 | } |
drh | 26198bb | 2013-10-31 11:15:09 +0000 | [diff] [blame] | 2217 | sqlite3VdbeAddOp3(v, OP_MakeRecord, regIdx, pIdx->nColumn, aRegIdx[ix]); |
drh | 26198bb | 2013-10-31 11:15:09 +0000 | [diff] [blame] | 2218 | VdbeComment((v, "for %s", pIdx->zName)); |
drh | 7e4acf7 | 2017-02-14 20:00:16 +0000 | [diff] [blame] | 2219 | #ifdef SQLITE_ENABLE_NULL_TRIM |
drh | 9df385e | 2019-02-19 13:08:35 +0000 | [diff] [blame] | 2220 | if( pIdx->idxType==SQLITE_IDXTYPE_PRIMARYKEY ){ |
| 2221 | sqlite3SetMakeRecordP5(v, pIdx->pTable); |
| 2222 | } |
drh | 7e4acf7 | 2017-02-14 20:00:16 +0000 | [diff] [blame] | 2223 | #endif |
drh | 3aef2fb | 2020-01-02 17:46:02 +0000 | [diff] [blame] | 2224 | sqlite3VdbeReleaseRegisters(pParse, regIdx, pIdx->nColumn, 0, 0); |
drh | b2fe7d8 | 2003-04-20 17:29:23 +0000 | [diff] [blame] | 2225 | |
drh | f8ffb27 | 2013-11-01 17:08:56 +0000 | [diff] [blame] | 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. */ |
drh | 00012df | 2013-11-05 01:59:07 +0000 | [diff] [blame] | 2230 | if( isUpdate && pPk==pIdx && pkChng==0 ){ |
drh | da475b8 | 2013-11-04 21:44:54 +0000 | [diff] [blame] | 2231 | sqlite3VdbeResolveLabel(v, addrUniqueOk); |
| 2232 | continue; |
| 2233 | } |
drh | f8ffb27 | 2013-11-01 17:08:56 +0000 | [diff] [blame] | 2234 | |
drh | 6934fc7 | 2013-10-31 15:37:49 +0000 | [diff] [blame] | 2235 | /* Find out what action to take in case there is a uniqueness conflict */ |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 2236 | onError = pIdx->onError; |
danielk1977 | de63035 | 2009-05-04 11:42:29 +0000 | [diff] [blame] | 2237 | if( onError==OE_None ){ |
drh | 11e8527 | 2013-10-26 15:40:48 +0000 | [diff] [blame] | 2238 | sqlite3VdbeResolveLabel(v, addrUniqueOk); |
danielk1977 | de63035 | 2009-05-04 11:42:29 +0000 | [diff] [blame] | 2239 | continue; /* pIdx is not a UNIQUE index */ |
| 2240 | } |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 2241 | if( overrideError!=OE_Default ){ |
| 2242 | onError = overrideError; |
drh | a996e47 | 2003-05-16 02:30:27 +0000 | [diff] [blame] | 2243 | }else if( onError==OE_Default ){ |
| 2244 | onError = OE_Abort; |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 2245 | } |
drh | c6c9e15 | 2016-11-10 17:01:36 +0000 | [diff] [blame] | 2246 | |
drh | c8a0c90 | 2018-04-13 15:14:33 +0000 | [diff] [blame] | 2247 | /* Figure out if the upsert clause applies to this index */ |
drh | 61e280a | 2020-12-11 01:17:06 +0000 | [diff] [blame] | 2248 | if( pUpsertClause ){ |
drh | 255c1c1 | 2020-12-12 00:28:15 +0000 | [diff] [blame] | 2249 | if( pUpsertClause->isDoUpdate==0 ){ |
drh | c8a0c90 | 2018-04-13 15:14:33 +0000 | [diff] [blame] | 2250 | onError = OE_Ignore; /* DO NOTHING is the same as INSERT OR IGNORE */ |
| 2251 | }else{ |
| 2252 | onError = OE_Update; /* DO UPDATE */ |
| 2253 | } |
| 2254 | } |
| 2255 | |
drh | 801f55d | 2017-01-04 22:02:56 +0000 | [diff] [blame] | 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 |
dan | f9a12a1 | 2017-01-05 06:57:42 +0000 | [diff] [blame] | 2261 | ** (5) No FK constraint counters need to be updated if a conflict occurs. |
dan | 418454c | 2019-01-07 15:57:35 +0000 | [diff] [blame] | 2262 | ** |
| 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 |
drh | 78b2fa8 | 2021-10-07 12:11:20 +0000 | [diff] [blame] | 2265 | ** is invoked. */ |
| 2266 | assert( IsOrdinaryTable(pTab) ); |
dan | 418454c | 2019-01-07 15:57:35 +0000 | [diff] [blame] | 2267 | #ifndef SQLITE_ENABLE_PREUPDATE_HOOK |
drh | 801f55d | 2017-01-04 22:02:56 +0000 | [diff] [blame] | 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)) |
dan | f9a12a1 | 2017-01-05 06:57:42 +0000 | [diff] [blame] | 2273 | && ( 0==(db->flags&SQLITE_ForeignKeys) || /* Condition 5 */ |
drh | f38524d | 2021-08-02 16:41:57 +0000 | [diff] [blame] | 2274 | (0==pTab->u.tab.pFKey && 0==sqlite3FkReferences(pTab))) |
drh | 801f55d | 2017-01-04 22:02:56 +0000 | [diff] [blame] | 2275 | ){ |
| 2276 | sqlite3VdbeResolveLabel(v, addrUniqueOk); |
| 2277 | continue; |
drh | c6c9e15 | 2016-11-10 17:01:36 +0000 | [diff] [blame] | 2278 | } |
dan | 418454c | 2019-01-07 15:57:35 +0000 | [diff] [blame] | 2279 | #endif /* ifndef SQLITE_ENABLE_PREUPDATE_HOOK */ |
drh | c6c9e15 | 2016-11-10 17:01:36 +0000 | [diff] [blame] | 2280 | |
drh | b2fe7d8 | 2003-04-20 17:29:23 +0000 | [diff] [blame] | 2281 | /* Check to see if the new index entry will be unique */ |
drh | 4031baf | 2018-05-28 17:31:20 +0000 | [diff] [blame] | 2282 | sqlite3VdbeVerifyAbortable(v, onError); |
drh | a407ecc | 2019-10-26 00:04:21 +0000 | [diff] [blame] | 2283 | addrConflictCk = |
| 2284 | sqlite3VdbeAddOp4Int(v, OP_NoConflict, iThisCur, addrUniqueOk, |
| 2285 | regIdx, pIdx->nKeyCol); VdbeCoverage(v); |
drh | f8ffb27 | 2013-11-01 17:08:56 +0000 | [diff] [blame] | 2286 | |
| 2287 | /* Generate code to handle collisions */ |
drh | d3e21a1 | 2020-12-11 17:11:56 +0000 | [diff] [blame] | 2288 | regR = pIdx==pPk ? regIdx : sqlite3GetTempRange(pParse, nPkField); |
drh | 46d03fc | 2013-12-19 02:23:45 +0000 | [diff] [blame] | 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 */ |
| 2294 | if( isUpdate ){ |
| 2295 | sqlite3VdbeAddOp3(v, OP_Eq, regR, addrUniqueOk, regOldData); |
drh | 3d77dee | 2014-02-19 14:20:49 +0000 | [diff] [blame] | 2296 | sqlite3VdbeChangeP5(v, SQLITE_NOTNULL); |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 2297 | VdbeCoverage(v); |
drh | da475b8 | 2013-11-04 21:44:54 +0000 | [diff] [blame] | 2298 | } |
drh | 46d03fc | 2013-12-19 02:23:45 +0000 | [diff] [blame] | 2299 | }else{ |
| 2300 | int x; |
| 2301 | /* Extract the PRIMARY KEY from the end of the index entry and |
| 2302 | ** store it in registers regR..regR+nPk-1 */ |
drh | a021f12 | 2013-12-19 14:34:34 +0000 | [diff] [blame] | 2303 | if( pIdx!=pPk ){ |
drh | 46d03fc | 2013-12-19 02:23:45 +0000 | [diff] [blame] | 2304 | for(i=0; i<pPk->nKeyCol; i++){ |
drh | 4b92f98 | 2015-09-29 17:20:14 +0000 | [diff] [blame] | 2305 | assert( pPk->aiColumn[i]>=0 ); |
drh | b9bcf7c | 2019-10-19 13:29:10 +0000 | [diff] [blame] | 2306 | x = sqlite3TableColumnToIndex(pIdx, pPk->aiColumn[i]); |
drh | 46d03fc | 2013-12-19 02:23:45 +0000 | [diff] [blame] | 2307 | sqlite3VdbeAddOp3(v, OP_Column, iThisCur, x, regR+i); |
| 2308 | VdbeComment((v, "%s.%s", pTab->zName, |
drh | cf9d36d | 2021-08-02 18:03:43 +0000 | [diff] [blame] | 2309 | pTab->aCol[pPk->aiColumn[i]].zCnName)); |
drh | da475b8 | 2013-11-04 21:44:54 +0000 | [diff] [blame] | 2310 | } |
drh | 46d03fc | 2013-12-19 02:23:45 +0000 | [diff] [blame] | 2311 | } |
| 2312 | if( isUpdate ){ |
| 2313 | /* If currently processing the PRIMARY KEY of a WITHOUT ROWID |
| 2314 | ** table, only conflict if the new PRIMARY KEY values are actually |
drh | 5a1f761 | 2022-03-11 15:42:05 +0000 | [diff] [blame] | 2315 | ** different from the old. See TH3 withoutrowid04.test. |
drh | 46d03fc | 2013-12-19 02:23:45 +0000 | [diff] [blame] | 2316 | ** |
| 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; |
| 2321 | int op = OP_Ne; |
drh | 48dd1d8 | 2014-05-27 18:18:58 +0000 | [diff] [blame] | 2322 | int regCmp = (IsPrimaryKeyIndex(pIdx) ? regIdx : regR); |
drh | 46d03fc | 2013-12-19 02:23:45 +0000 | [diff] [blame] | 2323 | |
| 2324 | for(i=0; i<pPk->nKeyCol; i++){ |
| 2325 | char *p4 = (char*)sqlite3LocateCollSeq(pParse, pPk->azColl[i]); |
| 2326 | x = pPk->aiColumn[i]; |
drh | 4b92f98 | 2015-09-29 17:20:14 +0000 | [diff] [blame] | 2327 | assert( x>=0 ); |
drh | 46d03fc | 2013-12-19 02:23:45 +0000 | [diff] [blame] | 2328 | if( i==(pPk->nKeyCol-1) ){ |
| 2329 | addrJump = addrUniqueOk; |
| 2330 | op = OP_Eq; |
| 2331 | } |
drh | b6d861e | 2019-10-29 03:30:26 +0000 | [diff] [blame] | 2332 | x = sqlite3TableColumnToStorage(pTab, x); |
drh | 46d03fc | 2013-12-19 02:23:45 +0000 | [diff] [blame] | 2333 | sqlite3VdbeAddOp4(v, op, |
| 2334 | regOldData+1+x, addrJump, regCmp+i, p4, P4_COLLSEQ |
| 2335 | ); |
drh | 3d77dee | 2014-02-19 14:20:49 +0000 | [diff] [blame] | 2336 | sqlite3VdbeChangeP5(v, SQLITE_NOTNULL); |
| 2337 | VdbeCoverageIf(v, op==OP_Eq); |
| 2338 | VdbeCoverageIf(v, op==OP_Ne); |
drh | 46d03fc | 2013-12-19 02:23:45 +0000 | [diff] [blame] | 2339 | } |
drh | 26198bb | 2013-10-31 11:15:09 +0000 | [diff] [blame] | 2340 | } |
drh | 26198bb | 2013-10-31 11:15:09 +0000 | [diff] [blame] | 2341 | } |
drh | 11e8527 | 2013-10-26 15:40:48 +0000 | [diff] [blame] | 2342 | } |
drh | b2fe7d8 | 2003-04-20 17:29:23 +0000 | [diff] [blame] | 2343 | |
| 2344 | /* Generate code that executes if the new index entry is not unique */ |
danielk1977 | b84f96f | 2005-01-20 11:32:23 +0000 | [diff] [blame] | 2345 | assert( onError==OE_Rollback || onError==OE_Abort || onError==OE_Fail |
drh | 9eddaca | 2018-04-13 18:59:17 +0000 | [diff] [blame] | 2346 | || onError==OE_Ignore || onError==OE_Replace || onError==OE_Update ); |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 2347 | switch( onError ){ |
drh | 1c92853 | 2002-01-31 15:54:21 +0000 | [diff] [blame] | 2348 | case OE_Rollback: |
| 2349 | case OE_Abort: |
| 2350 | case OE_Fail: { |
drh | 9916048 | 2018-04-18 01:34:39 +0000 | [diff] [blame] | 2351 | testcase( onError==OE_Rollback ); |
| 2352 | testcase( onError==OE_Abort ); |
| 2353 | testcase( onError==OE_Fail ); |
drh | f9c8ce3 | 2013-11-05 13:33:55 +0000 | [diff] [blame] | 2354 | sqlite3UniqueConstraint(pParse, onError, pIdx); |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 2355 | break; |
| 2356 | } |
drh | 9eddaca | 2018-04-13 18:59:17 +0000 | [diff] [blame] | 2357 | #ifndef SQLITE_OMIT_UPSERT |
| 2358 | case OE_Update: { |
dan | 2cc0042 | 2018-04-17 18:16:10 +0000 | [diff] [blame] | 2359 | sqlite3UpsertDoUpdate(pParse, pUpsert, pTab, pIdx, iIdxCur+ix); |
drh | 08b9208 | 2020-08-10 14:18:00 +0000 | [diff] [blame] | 2360 | /* no break */ deliberate_fall_through |
drh | 9eddaca | 2018-04-13 18:59:17 +0000 | [diff] [blame] | 2361 | } |
| 2362 | #endif |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 2363 | case OE_Ignore: { |
drh | 9916048 | 2018-04-18 01:34:39 +0000 | [diff] [blame] | 2364 | testcase( onError==OE_Ignore ); |
drh | 076e85f | 2015-09-03 13:46:12 +0000 | [diff] [blame] | 2365 | sqlite3VdbeGoto(v, ignoreDest); |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 2366 | break; |
| 2367 | } |
drh | 098d168 | 2009-05-02 15:46:46 +0000 | [diff] [blame] | 2368 | default: { |
drh | a407ecc | 2019-10-26 00:04:21 +0000 | [diff] [blame] | 2369 | int nConflictCk; /* Number of opcodes in conflict check logic */ |
| 2370 | |
drh | 098d168 | 2009-05-02 15:46:46 +0000 | [diff] [blame] | 2371 | assert( onError==OE_Replace ); |
drh | a407ecc | 2019-10-26 00:04:21 +0000 | [diff] [blame] | 2372 | nConflictCk = sqlite3VdbeCurrentAddr(v) - addrConflictCk; |
drh | 362c181 | 2021-10-30 18:17:59 +0000 | [diff] [blame] | 2373 | assert( nConflictCk>0 || db->mallocFailed ); |
| 2374 | testcase( nConflictCk<=0 ); |
drh | d3c468b | 2019-10-26 16:38:49 +0000 | [diff] [blame] | 2375 | testcase( nConflictCk>1 ); |
drh | a407ecc | 2019-10-26 00:04:21 +0000 | [diff] [blame] | 2376 | if( regTrigCnt ){ |
dan | fecfb31 | 2018-05-28 18:29:46 +0000 | [diff] [blame] | 2377 | sqlite3MultiWrite(pParse); |
drh | a407ecc | 2019-10-26 00:04:21 +0000 | [diff] [blame] | 2378 | nReplaceTrig++; |
dan | fecfb31 | 2018-05-28 18:29:46 +0000 | [diff] [blame] | 2379 | } |
drh | 7b14b65 | 2019-12-29 22:08:20 +0000 | [diff] [blame] | 2380 | if( pTrigger && isUpdate ){ |
| 2381 | sqlite3VdbeAddOp1(v, OP_CursorLock, iDataCur); |
| 2382 | } |
drh | 26198bb | 2013-10-31 11:15:09 +0000 | [diff] [blame] | 2383 | sqlite3GenerateRowDelete(pParse, pTab, pTrigger, iDataCur, iIdxCur, |
drh | b0264ee | 2015-09-14 14:45:50 +0000 | [diff] [blame] | 2384 | regR, nPkField, 0, OE_Replace, |
drh | 6811693 | 2017-01-07 14:47:03 +0000 | [diff] [blame] | 2385 | (pIdx==pPk ? ONEPASS_SINGLE : ONEPASS_OFF), iThisCur); |
drh | 7b14b65 | 2019-12-29 22:08:20 +0000 | [diff] [blame] | 2386 | if( pTrigger && isUpdate ){ |
| 2387 | sqlite3VdbeAddOp1(v, OP_CursorUnlock, iDataCur); |
| 2388 | } |
drh | a407ecc | 2019-10-26 00:04:21 +0000 | [diff] [blame] | 2389 | if( regTrigCnt ){ |
drh | a407ecc | 2019-10-26 00:04:21 +0000 | [diff] [blame] | 2390 | int addrBypass; /* Jump destination to bypass recheck logic */ |
| 2391 | |
| 2392 | sqlite3VdbeAddOp2(v, OP_AddImm, regTrigCnt, 1); /* incr trigger cnt */ |
| 2393 | addrBypass = sqlite3VdbeAddOp0(v, OP_Goto); /* Bypass recheck */ |
| 2394 | VdbeComment((v, "bypass recheck")); |
| 2395 | |
| 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 |
| 2398 | ** fired. */ |
| 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 */ |
drh | 0660884 | 2019-10-26 01:43:14 +0000 | [diff] [blame] | 2404 | sqlite3VdbeAddOp2(v, OP_IsNull, regIdx-1, lblRecheckOk); |
drh | a407ecc | 2019-10-26 00:04:21 +0000 | [diff] [blame] | 2405 | VdbeCoverage(v); |
| 2406 | } |
| 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 */ |
drh | d3c468b | 2019-10-26 16:38:49 +0000 | [diff] [blame] | 2410 | while( nConflictCk>0 ){ |
drh | d901b16 | 2019-10-26 12:27:55 +0000 | [diff] [blame] | 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 */ |
drh | b9f2e5f | 2020-01-28 15:02:23 +0000 | [diff] [blame] | 2418 | const char *zP4; |
drh | d901b16 | 2019-10-26 12:27:55 +0000 | [diff] [blame] | 2419 | if( sqlite3OpcodeProperty[x.opcode]&OPFLG_JUMP ){ |
| 2420 | p2 = lblRecheckOk; |
| 2421 | }else{ |
| 2422 | p2 = x.p2; |
| 2423 | } |
drh | b9f2e5f | 2020-01-28 15:02:23 +0000 | [diff] [blame] | 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); |
drh | d901b16 | 2019-10-26 12:27:55 +0000 | [diff] [blame] | 2426 | sqlite3VdbeChangeP5(v, x.p5); |
| 2427 | VdbeCoverageIf(v, p2!=x.p2); |
drh | a407ecc | 2019-10-26 00:04:21 +0000 | [diff] [blame] | 2428 | } |
| 2429 | nConflictCk--; |
drh | d901b16 | 2019-10-26 12:27:55 +0000 | [diff] [blame] | 2430 | addrConflictCk++; |
drh | a407ecc | 2019-10-26 00:04:21 +0000 | [diff] [blame] | 2431 | } |
| 2432 | /* If the retest fails, issue an abort */ |
drh | 2da8d6f | 2019-10-16 14:56:03 +0000 | [diff] [blame] | 2433 | sqlite3UniqueConstraint(pParse, OE_Abort, pIdx); |
drh | a407ecc | 2019-10-26 00:04:21 +0000 | [diff] [blame] | 2434 | |
| 2435 | sqlite3VdbeJumpHere(v, addrBypass); /* Terminate the recheck bypass */ |
drh | 2da8d6f | 2019-10-16 14:56:03 +0000 | [diff] [blame] | 2436 | } |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 2437 | seenReplace = 1; |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 2438 | break; |
| 2439 | } |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 2440 | } |
drh | 61e280a | 2020-12-11 01:17:06 +0000 | [diff] [blame] | 2441 | sqlite3VdbeResolveLabel(v, addrUniqueOk); |
drh | 392ee21 | 2013-11-08 16:54:56 +0000 | [diff] [blame] | 2442 | if( regR!=regIdx ) sqlite3ReleaseTempRange(pParse, regR, nPkField); |
drh | ed4c546 | 2020-12-11 16:49:51 +0000 | [diff] [blame] | 2443 | if( pUpsertClause |
| 2444 | && upsertIpkReturn |
| 2445 | && sqlite3UpsertNextIsIPK(pUpsertClause) |
| 2446 | ){ |
drh | 61e280a | 2020-12-11 01:17:06 +0000 | [diff] [blame] | 2447 | sqlite3VdbeGoto(v, upsertIpkDelay+1); |
| 2448 | sqlite3VdbeJumpHere(v, upsertIpkReturn); |
drh | 58b18a4 | 2020-12-11 19:36:19 +0000 | [diff] [blame] | 2449 | upsertIpkReturn = 0; |
drh | 61e280a | 2020-12-11 01:17:06 +0000 | [diff] [blame] | 2450 | } |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 2451 | } |
drh | 8430450 | 2018-08-14 15:12:52 +0000 | [diff] [blame] | 2452 | |
| 2453 | /* If the IPK constraint is a REPLACE, run it last */ |
| 2454 | if( ipkTop ){ |
drh | 6214d93 | 2019-01-12 16:19:23 +0000 | [diff] [blame] | 2455 | sqlite3VdbeGoto(v, ipkTop); |
drh | 8430450 | 2018-08-14 15:12:52 +0000 | [diff] [blame] | 2456 | VdbeComment((v, "Do IPK REPLACE")); |
drh | 66306d8 | 2021-12-30 02:38:43 +0000 | [diff] [blame] | 2457 | assert( ipkBottom>0 ); |
drh | 8430450 | 2018-08-14 15:12:52 +0000 | [diff] [blame] | 2458 | sqlite3VdbeJumpHere(v, ipkBottom); |
| 2459 | } |
| 2460 | |
drh | a407ecc | 2019-10-26 00:04:21 +0000 | [diff] [blame] | 2461 | /* Recheck all uniqueness constraints after replace triggers have run */ |
| 2462 | testcase( regTrigCnt!=0 && nReplaceTrig==0 ); |
drh | d3c468b | 2019-10-26 16:38:49 +0000 | [diff] [blame] | 2463 | assert( regTrigCnt!=0 || nReplaceTrig==0 ); |
drh | a407ecc | 2019-10-26 00:04:21 +0000 | [diff] [blame] | 2464 | if( nReplaceTrig ){ |
| 2465 | sqlite3VdbeAddOp2(v, OP_IfNot, regTrigCnt, lblRecheckOk);VdbeCoverage(v); |
| 2466 | if( !pPk ){ |
| 2467 | if( isUpdate ){ |
| 2468 | sqlite3VdbeAddOp3(v, OP_Eq, regNewData, addrRecheck, regOldData); |
| 2469 | sqlite3VdbeChangeP5(v, SQLITE_NOTNULL); |
| 2470 | VdbeCoverage(v); |
| 2471 | } |
| 2472 | sqlite3VdbeAddOp3(v, OP_NotExists, iDataCur, addrRecheck, regNewData); |
| 2473 | VdbeCoverage(v); |
| 2474 | sqlite3RowidConstraint(pParse, OE_Abort, pTab); |
| 2475 | }else{ |
| 2476 | sqlite3VdbeGoto(v, addrRecheck); |
| 2477 | } |
| 2478 | sqlite3VdbeResolveLabel(v, lblRecheckOk); |
| 2479 | } |
| 2480 | |
drh | a7c3b93 | 2019-05-07 19:13:42 +0000 | [diff] [blame] | 2481 | /* Generate the table record */ |
| 2482 | if( HasRowid(pTab) ){ |
| 2483 | int regRec = aRegIdx[ix]; |
drh | 0b0b3a9 | 2019-10-17 18:35:57 +0000 | [diff] [blame] | 2484 | sqlite3VdbeAddOp3(v, OP_MakeRecord, regNewData+1, pTab->nNVCol, regRec); |
drh | a7c3b93 | 2019-05-07 19:13:42 +0000 | [diff] [blame] | 2485 | sqlite3SetMakeRecordP5(v, pTab); |
| 2486 | if( !bAffinityDone ){ |
| 2487 | sqlite3TableAffinity(v, pTab, 0); |
| 2488 | } |
| 2489 | } |
| 2490 | |
drh | 4308e34 | 2013-11-11 16:55:52 +0000 | [diff] [blame] | 2491 | *pbMayReplace = seenReplace; |
drh | ce60aa4 | 2013-11-08 01:09:15 +0000 | [diff] [blame] | 2492 | VdbeModuleComment((v, "END: GenCnstCks(%d)", seenReplace)); |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 2493 | } |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 2494 | |
drh | d447dce | 2017-01-25 20:55:11 +0000 | [diff] [blame] | 2495 | #ifdef SQLITE_ENABLE_NULL_TRIM |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 2496 | /* |
drh | 585ce19 | 2017-01-25 14:58:27 +0000 | [diff] [blame] | 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. |
| 2499 | ** |
| 2500 | ** Or if no columns of pTab may be NULL-trimmed, leave P5 at zero. |
| 2501 | */ |
| 2502 | void sqlite3SetMakeRecordP5(Vdbe *v, Table *pTab){ |
| 2503 | u16 i; |
| 2504 | |
| 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; |
| 2508 | |
drh | 7e4acf7 | 2017-02-14 20:00:16 +0000 | [diff] [blame] | 2509 | for(i=pTab->nCol-1; i>0; i--){ |
drh | 79cf2b7 | 2021-07-31 20:30:41 +0000 | [diff] [blame] | 2510 | if( pTab->aCol[i].iDflt!=0 ) break; |
drh | 7e4acf7 | 2017-02-14 20:00:16 +0000 | [diff] [blame] | 2511 | if( pTab->aCol[i].colFlags & COLFLAG_PRIMKEY ) break; |
| 2512 | } |
| 2513 | sqlite3VdbeChangeP5(v, i+1); |
drh | 585ce19 | 2017-01-25 14:58:27 +0000 | [diff] [blame] | 2514 | } |
drh | d447dce | 2017-01-25 20:55:11 +0000 | [diff] [blame] | 2515 | #endif |
drh | 585ce19 | 2017-01-25 14:58:27 +0000 | [diff] [blame] | 2516 | |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 2517 | /* |
dan | fadc0e3 | 2021-02-18 12:18:10 +0000 | [diff] [blame] | 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. |
| 2522 | */ |
| 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 */ |
| 2529 | ){ |
| 2530 | Vdbe *v = pParse->pVdbe; |
| 2531 | int r = sqlite3GetTempReg(pParse); |
| 2532 | assert( !HasRowid(pTab) ); |
drh | d01206f | 2021-03-21 18:23:48 +0000 | [diff] [blame] | 2533 | assert( 0==(pParse->db->mDbFlags & DBFLAG_Vacuum) || CORRUPT_DB ); |
dan | fadc0e3 | 2021-02-18 12:18:10 +0000 | [diff] [blame] | 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); |
| 2538 | } |
| 2539 | #else |
| 2540 | # define codeWithoutRowidPreupdate(a,b,c,d) |
| 2541 | #endif |
| 2542 | |
| 2543 | /* |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 2544 | ** This routine generates code to finish the INSERT or UPDATE operation |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 2545 | ** that was started by a prior call to sqlite3GenerateConstraintChecks. |
drh | 6934fc7 | 2013-10-31 15:37:49 +0000 | [diff] [blame] | 2546 | ** A consecutive range of registers starting at regNewData contains the |
drh | 04adf41 | 2008-01-08 18:57:50 +0000 | [diff] [blame] | 2547 | ** rowid and the content to be inserted. |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 2548 | ** |
drh | b419a92 | 2002-01-30 16:17:23 +0000 | [diff] [blame] | 2549 | ** The arguments to this routine should be the same as the first six |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 2550 | ** arguments to sqlite3GenerateConstraintChecks. |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 2551 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 2552 | void sqlite3CompleteInsertion( |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 2553 | Parse *pParse, /* The parser context */ |
| 2554 | Table *pTab, /* the table into which we are inserting */ |
drh | 26198bb | 2013-10-31 11:15:09 +0000 | [diff] [blame] | 2555 | int iDataCur, /* Cursor of the canonical data source */ |
| 2556 | int iIdxCur, /* First index cursor */ |
drh | 6934fc7 | 2013-10-31 15:37:49 +0000 | [diff] [blame] | 2557 | int regNewData, /* Range of content */ |
drh | aa9b896 | 2008-01-08 02:57:55 +0000 | [diff] [blame] | 2558 | int *aRegIdx, /* Register used by each index. 0 for unused indices */ |
dan | f91c131 | 2017-01-10 20:04:38 +0000 | [diff] [blame] | 2559 | int update_flags, /* True for UPDATE, False for INSERT */ |
danielk1977 | de63035 | 2009-05-04 11:42:29 +0000 | [diff] [blame] | 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 */ |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 2562 | ){ |
drh | 6934fc7 | 2013-10-31 15:37:49 +0000 | [diff] [blame] | 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 */ |
drh | 6934fc7 | 2013-10-31 15:37:49 +0000 | [diff] [blame] | 2566 | int i; /* Loop counter */ |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 2567 | |
dan | f91c131 | 2017-01-10 20:04:38 +0000 | [diff] [blame] | 2568 | assert( update_flags==0 |
| 2569 | || update_flags==OPFLAG_ISUPDATE |
| 2570 | || update_flags==(OPFLAG_ISUPDATE|OPFLAG_SAVEPOSITION) |
| 2571 | ); |
| 2572 | |
drh | f0b4174 | 2020-08-15 21:55:14 +0000 | [diff] [blame] | 2573 | v = pParse->pVdbe; |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 2574 | assert( v!=0 ); |
drh | f38524d | 2021-08-02 16:41:57 +0000 | [diff] [blame] | 2575 | assert( !IsView(pTab) ); /* This table is not a VIEW */ |
drh | b2b9d3d | 2013-08-01 01:14:43 +0000 | [diff] [blame] | 2576 | for(i=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){ |
drh | d35bdd6 | 2019-12-15 02:49:32 +0000 | [diff] [blame] | 2577 | /* All REPLACE indexes are at the end of the list */ |
| 2578 | assert( pIdx->onError!=OE_Replace |
| 2579 | || pIdx->pNext==0 |
| 2580 | || pIdx->pNext->onError==OE_Replace ); |
drh | aa9b896 | 2008-01-08 02:57:55 +0000 | [diff] [blame] | 2581 | if( aRegIdx[i]==0 ) continue; |
drh | b2b9d3d | 2013-08-01 01:14:43 +0000 | [diff] [blame] | 2582 | if( pIdx->pPartIdxWhere ){ |
| 2583 | sqlite3VdbeAddOp2(v, OP_IsNull, aRegIdx[i], sqlite3VdbeCurrentAddr(v)+2); |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 2584 | VdbeCoverage(v); |
drh | b2b9d3d | 2013-08-01 01:14:43 +0000 | [diff] [blame] | 2585 | } |
dan | cb9a364 | 2017-01-30 19:44:53 +0000 | [diff] [blame] | 2586 | pik_flags = (useSeekResult ? OPFLAG_USESEEKRESULT : 0); |
drh | 48dd1d8 | 2014-05-27 18:18:58 +0000 | [diff] [blame] | 2587 | if( IsPrimaryKeyIndex(pIdx) && !HasRowid(pTab) ){ |
drh | 6546af1 | 2013-11-04 15:23:25 +0000 | [diff] [blame] | 2588 | pik_flags |= OPFLAG_NCHANGE; |
dan | f91c131 | 2017-01-10 20:04:38 +0000 | [diff] [blame] | 2589 | pik_flags |= (update_flags & OPFLAG_SAVEPOSITION); |
dan | cb9a364 | 2017-01-30 19:44:53 +0000 | [diff] [blame] | 2590 | if( update_flags==0 ){ |
dan | fadc0e3 | 2021-02-18 12:18:10 +0000 | [diff] [blame] | 2591 | codeWithoutRowidPreupdate(pParse, pTab, iIdxCur+i, aRegIdx[i]); |
dan | cb9a364 | 2017-01-30 19:44:53 +0000 | [diff] [blame] | 2592 | } |
danielk1977 | de63035 | 2009-05-04 11:42:29 +0000 | [diff] [blame] | 2593 | } |
dan | cb9a364 | 2017-01-30 19:44:53 +0000 | [diff] [blame] | 2594 | sqlite3VdbeAddOp4Int(v, OP_IdxInsert, iIdxCur+i, aRegIdx[i], |
| 2595 | aRegIdx[i]+1, |
| 2596 | pIdx->uniqNotNull ? pIdx->nKeyCol: pIdx->nColumn); |
drh | 9b34abe | 2016-01-16 15:12:35 +0000 | [diff] [blame] | 2597 | sqlite3VdbeChangeP5(v, pik_flags); |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 2598 | } |
drh | ec95c44 | 2013-10-23 01:57:32 +0000 | [diff] [blame] | 2599 | if( !HasRowid(pTab) ) return; |
drh | 4794f73 | 2004-11-05 17:17:50 +0000 | [diff] [blame] | 2600 | if( pParse->nested ){ |
| 2601 | pik_flags = 0; |
| 2602 | }else{ |
danielk1977 | 94eb6a1 | 2005-12-15 15:22:08 +0000 | [diff] [blame] | 2603 | pik_flags = OPFLAG_NCHANGE; |
dan | f91c131 | 2017-01-10 20:04:38 +0000 | [diff] [blame] | 2604 | pik_flags |= (update_flags?update_flags:OPFLAG_LASTROWID); |
drh | 4794f73 | 2004-11-05 17:17:50 +0000 | [diff] [blame] | 2605 | } |
drh | e4d9081 | 2007-03-29 05:51:49 +0000 | [diff] [blame] | 2606 | if( appendBias ){ |
| 2607 | pik_flags |= OPFLAG_APPEND; |
| 2608 | } |
danielk1977 | de63035 | 2009-05-04 11:42:29 +0000 | [diff] [blame] | 2609 | if( useSeekResult ){ |
| 2610 | pik_flags |= OPFLAG_USESEEKRESULT; |
| 2611 | } |
drh | a7c3b93 | 2019-05-07 19:13:42 +0000 | [diff] [blame] | 2612 | sqlite3VdbeAddOp3(v, OP_Insert, iDataCur, aRegIdx[i], regNewData); |
danielk1977 | 94eb6a1 | 2005-12-15 15:22:08 +0000 | [diff] [blame] | 2613 | if( !pParse->nested ){ |
drh | f14b7fb | 2016-12-07 21:35:55 +0000 | [diff] [blame] | 2614 | sqlite3VdbeAppendP4(v, pTab, P4_TABLE); |
danielk1977 | 94eb6a1 | 2005-12-15 15:22:08 +0000 | [diff] [blame] | 2615 | } |
drh | b765411 | 2008-01-12 12:48:07 +0000 | [diff] [blame] | 2616 | sqlite3VdbeChangeP5(v, pik_flags); |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 2617 | } |
drh | cd44690 | 2004-02-24 01:05:31 +0000 | [diff] [blame] | 2618 | |
| 2619 | /* |
drh | 26198bb | 2013-10-31 11:15:09 +0000 | [diff] [blame] | 2620 | ** Allocate cursors for the pTab table and all its indices and generate |
| 2621 | ** code to open and initialized those cursors. |
drh | aa9b896 | 2008-01-08 02:57:55 +0000 | [diff] [blame] | 2622 | ** |
drh | 26198bb | 2013-10-31 11:15:09 +0000 | [diff] [blame] | 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. |
| 2627 | ** |
| 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. |
| 2631 | ** |
| 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. |
drh | b6b4b79 | 2014-08-21 14:10:23 +0000 | [diff] [blame] | 2636 | ** |
| 2637 | ** If pTab is a virtual table, then this routine is a no-op and the |
| 2638 | ** *piDataCur and *piIdxCur values are left uninitialized. |
drh | cd44690 | 2004-02-24 01:05:31 +0000 | [diff] [blame] | 2639 | */ |
drh | aa9b896 | 2008-01-08 02:57:55 +0000 | [diff] [blame] | 2640 | int sqlite3OpenTableAndIndices( |
drh | 290c194 | 2004-08-21 17:54:45 +0000 | [diff] [blame] | 2641 | Parse *pParse, /* Parsing context */ |
| 2642 | Table *pTab, /* Table to be opened */ |
drh | 26198bb | 2013-10-31 11:15:09 +0000 | [diff] [blame] | 2643 | int op, /* OP_OpenRead or OP_OpenWrite */ |
drh | b89aeb6 | 2016-01-27 15:49:32 +0000 | [diff] [blame] | 2644 | u8 p5, /* P5 value for OP_Open* opcodes (except on WITHOUT ROWID) */ |
drh | 26198bb | 2013-10-31 11:15:09 +0000 | [diff] [blame] | 2645 | int iBase, /* Use this for the table cursor, if there is one */ |
drh | 6a53499 | 2013-11-16 20:13:39 +0000 | [diff] [blame] | 2646 | u8 *aToOpen, /* If not NULL: boolean for each table and index */ |
drh | 26198bb | 2013-10-31 11:15:09 +0000 | [diff] [blame] | 2647 | int *piDataCur, /* Write the database source cursor number here */ |
| 2648 | int *piIdxCur /* Write the first index cursor number here */ |
drh | 290c194 | 2004-08-21 17:54:45 +0000 | [diff] [blame] | 2649 | ){ |
drh | cd44690 | 2004-02-24 01:05:31 +0000 | [diff] [blame] | 2650 | int i; |
drh | 4cbdda9 | 2006-06-14 19:00:20 +0000 | [diff] [blame] | 2651 | int iDb; |
drh | 6a53499 | 2013-11-16 20:13:39 +0000 | [diff] [blame] | 2652 | int iDataCur; |
drh | cd44690 | 2004-02-24 01:05:31 +0000 | [diff] [blame] | 2653 | Index *pIdx; |
drh | 4cbdda9 | 2006-06-14 19:00:20 +0000 | [diff] [blame] | 2654 | Vdbe *v; |
| 2655 | |
drh | 26198bb | 2013-10-31 11:15:09 +0000 | [diff] [blame] | 2656 | assert( op==OP_OpenRead || op==OP_OpenWrite ); |
dan | fd261ec | 2015-10-22 20:54:33 +0000 | [diff] [blame] | 2657 | assert( op==OP_OpenWrite || p5==0 ); |
drh | 26198bb | 2013-10-31 11:15:09 +0000 | [diff] [blame] | 2658 | if( IsVirtual(pTab) ){ |
drh | b6b4b79 | 2014-08-21 14:10:23 +0000 | [diff] [blame] | 2659 | /* This routine is a no-op for virtual tables. Leave the output |
drh | 33d28ab | 2021-10-28 12:07:43 +0000 | [diff] [blame] | 2660 | ** variables *piDataCur and *piIdxCur set to illegal cursor numbers |
| 2661 | ** for improved error detection. */ |
| 2662 | *piDataCur = *piIdxCur = -999; |
drh | 26198bb | 2013-10-31 11:15:09 +0000 | [diff] [blame] | 2663 | return 0; |
| 2664 | } |
drh | 4cbdda9 | 2006-06-14 19:00:20 +0000 | [diff] [blame] | 2665 | iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema); |
drh | f0b4174 | 2020-08-15 21:55:14 +0000 | [diff] [blame] | 2666 | v = pParse->pVdbe; |
drh | cd44690 | 2004-02-24 01:05:31 +0000 | [diff] [blame] | 2667 | assert( v!=0 ); |
drh | 26198bb | 2013-10-31 11:15:09 +0000 | [diff] [blame] | 2668 | if( iBase<0 ) iBase = pParse->nTab; |
drh | 6a53499 | 2013-11-16 20:13:39 +0000 | [diff] [blame] | 2669 | iDataCur = iBase++; |
| 2670 | if( piDataCur ) *piDataCur = iDataCur; |
| 2671 | if( HasRowid(pTab) && (aToOpen==0 || aToOpen[0]) ){ |
| 2672 | sqlite3OpenTable(pParse, iDataCur, iDb, pTab, op); |
drh | 6fbe41a | 2013-10-30 20:22:55 +0000 | [diff] [blame] | 2673 | }else{ |
drh | 26198bb | 2013-10-31 11:15:09 +0000 | [diff] [blame] | 2674 | sqlite3TableLock(pParse, iDb, pTab->tnum, op==OP_OpenWrite, pTab->zName); |
drh | 6fbe41a | 2013-10-30 20:22:55 +0000 | [diff] [blame] | 2675 | } |
drh | 6a53499 | 2013-11-16 20:13:39 +0000 | [diff] [blame] | 2676 | if( piIdxCur ) *piIdxCur = iBase; |
drh | 26198bb | 2013-10-31 11:15:09 +0000 | [diff] [blame] | 2677 | for(i=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){ |
drh | 26198bb | 2013-10-31 11:15:09 +0000 | [diff] [blame] | 2678 | int iIdxCur = iBase++; |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 2679 | assert( pIdx->pSchema==pTab->pSchema ); |
dan | 61441c3 | 2016-08-26 12:00:50 +0000 | [diff] [blame] | 2680 | if( IsPrimaryKeyIndex(pIdx) && !HasRowid(pTab) ){ |
| 2681 | if( piDataCur ) *piDataCur = iIdxCur; |
| 2682 | p5 = 0; |
| 2683 | } |
drh | 6a53499 | 2013-11-16 20:13:39 +0000 | [diff] [blame] | 2684 | if( aToOpen==0 || aToOpen[i+1] ){ |
| 2685 | sqlite3VdbeAddOp3(v, op, iIdxCur, pIdx->tnum, iDb); |
| 2686 | sqlite3VdbeSetP4KeyInfo(pParse, pIdx); |
drh | b89aeb6 | 2016-01-27 15:49:32 +0000 | [diff] [blame] | 2687 | sqlite3VdbeChangeP5(v, p5); |
dan | 61441c3 | 2016-08-26 12:00:50 +0000 | [diff] [blame] | 2688 | VdbeComment((v, "%s", pIdx->zName)); |
drh | b89aeb6 | 2016-01-27 15:49:32 +0000 | [diff] [blame] | 2689 | } |
drh | cd44690 | 2004-02-24 01:05:31 +0000 | [diff] [blame] | 2690 | } |
drh | 26198bb | 2013-10-31 11:15:09 +0000 | [diff] [blame] | 2691 | if( iBase>pParse->nTab ) pParse->nTab = iBase; |
| 2692 | return i; |
drh | cd44690 | 2004-02-24 01:05:31 +0000 | [diff] [blame] | 2693 | } |
drh | 9d9cf22 | 2007-02-13 15:01:11 +0000 | [diff] [blame] | 2694 | |
drh | 91c58e2 | 2007-03-27 12:04:04 +0000 | [diff] [blame] | 2695 | |
| 2696 | #ifdef SQLITE_TEST |
| 2697 | /* |
| 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 |
peter.d.reid | 60ec914 | 2014-09-06 16:39:46 +0000 | [diff] [blame] | 2701 | ** is happening when it is supposed to. |
drh | 91c58e2 | 2007-03-27 12:04:04 +0000 | [diff] [blame] | 2702 | */ |
| 2703 | int sqlite3_xferopt_count; |
| 2704 | #endif /* SQLITE_TEST */ |
| 2705 | |
| 2706 | |
drh | 9d9cf22 | 2007-02-13 15:01:11 +0000 | [diff] [blame] | 2707 | #ifndef SQLITE_OMIT_XFER_OPT |
| 2708 | /* |
drh | 9d9cf22 | 2007-02-13 15:01:11 +0000 | [diff] [blame] | 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: |
| 2712 | ** |
| 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 |
drh | b2b9d3d | 2013-08-01 01:14:43 +0000 | [diff] [blame] | 2717 | ** * The index has the exact same WHERE clause |
drh | 9d9cf22 | 2007-02-13 15:01:11 +0000 | [diff] [blame] | 2718 | */ |
| 2719 | static int xferCompatibleIndex(Index *pDest, Index *pSrc){ |
| 2720 | int i; |
| 2721 | assert( pDest && pSrc ); |
| 2722 | assert( pDest->pTable!=pSrc->pTable ); |
drh | 1e7c00e | 2019-11-07 14:51:24 +0000 | [diff] [blame] | 2723 | if( pDest->nKeyCol!=pSrc->nKeyCol || pDest->nColumn!=pSrc->nColumn ){ |
drh | 9d9cf22 | 2007-02-13 15:01:11 +0000 | [diff] [blame] | 2724 | return 0; /* Different number of columns */ |
| 2725 | } |
| 2726 | if( pDest->onError!=pSrc->onError ){ |
| 2727 | return 0; /* Different conflict resolution strategies */ |
| 2728 | } |
drh | bbbdc83 | 2013-10-22 18:01:40 +0000 | [diff] [blame] | 2729 | for(i=0; i<pSrc->nKeyCol; i++){ |
drh | 9d9cf22 | 2007-02-13 15:01:11 +0000 | [diff] [blame] | 2730 | if( pSrc->aiColumn[i]!=pDest->aiColumn[i] ){ |
| 2731 | return 0; /* Different columns indexed */ |
| 2732 | } |
drh | 4b92f98 | 2015-09-29 17:20:14 +0000 | [diff] [blame] | 2733 | if( pSrc->aiColumn[i]==XN_EXPR ){ |
drh | 1f9ca2c | 2015-08-25 16:57:52 +0000 | [diff] [blame] | 2734 | assert( pSrc->aColExpr!=0 && pDest->aColExpr!=0 ); |
dan | 5aa550c | 2017-06-24 18:10:29 +0000 | [diff] [blame] | 2735 | if( sqlite3ExprCompare(0, pSrc->aColExpr->a[i].pExpr, |
drh | 1f9ca2c | 2015-08-25 16:57:52 +0000 | [diff] [blame] | 2736 | pDest->aColExpr->a[i].pExpr, -1)!=0 ){ |
| 2737 | return 0; /* Different expressions in the index */ |
| 2738 | } |
| 2739 | } |
drh | 9d9cf22 | 2007-02-13 15:01:11 +0000 | [diff] [blame] | 2740 | if( pSrc->aSortOrder[i]!=pDest->aSortOrder[i] ){ |
| 2741 | return 0; /* Different sort orders */ |
| 2742 | } |
drh | 0472af9 | 2015-12-30 15:18:16 +0000 | [diff] [blame] | 2743 | if( sqlite3_stricmp(pSrc->azColl[i],pDest->azColl[i])!=0 ){ |
drh | 60a713c | 2008-01-21 16:22:45 +0000 | [diff] [blame] | 2744 | return 0; /* Different collating sequences */ |
drh | 9d9cf22 | 2007-02-13 15:01:11 +0000 | [diff] [blame] | 2745 | } |
| 2746 | } |
dan | 5aa550c | 2017-06-24 18:10:29 +0000 | [diff] [blame] | 2747 | if( sqlite3ExprCompare(0, pSrc->pPartIdxWhere, pDest->pPartIdxWhere, -1) ){ |
drh | b2b9d3d | 2013-08-01 01:14:43 +0000 | [diff] [blame] | 2748 | return 0; /* Different WHERE clauses */ |
| 2749 | } |
drh | 9d9cf22 | 2007-02-13 15:01:11 +0000 | [diff] [blame] | 2750 | |
| 2751 | /* If no test above fails then the indices must be compatible */ |
| 2752 | return 1; |
| 2753 | } |
| 2754 | |
| 2755 | /* |
| 2756 | ** Attempt the transfer optimization on INSERTs of the form |
| 2757 | ** |
| 2758 | ** INSERT INTO tab1 SELECT * FROM tab2; |
| 2759 | ** |
drh | ccdf1ba | 2011-11-04 14:36:02 +0000 | [diff] [blame] | 2760 | ** The xfer optimization transfers raw records from tab2 over to tab1. |
peter.d.reid | 60ec914 | 2014-09-06 16:39:46 +0000 | [diff] [blame] | 2761 | ** Columns are not decoded and reassembled, which greatly improves |
drh | ccdf1ba | 2011-11-04 14:36:02 +0000 | [diff] [blame] | 2762 | ** performance. Raw index records are transferred in the same way. |
drh | 9d9cf22 | 2007-02-13 15:01:11 +0000 | [diff] [blame] | 2763 | ** |
drh | ccdf1ba | 2011-11-04 14:36:02 +0000 | [diff] [blame] | 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. |
drh | 9d9cf22 | 2007-02-13 15:01:11 +0000 | [diff] [blame] | 2767 | ** |
drh | ccdf1ba | 2011-11-04 14:36:02 +0000 | [diff] [blame] | 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. |
drh | 9d9cf22 | 2007-02-13 15:01:11 +0000 | [diff] [blame] | 2777 | ** |
drh | ccdf1ba | 2011-11-04 14:36:02 +0000 | [diff] [blame] | 2778 | ** This optimization is particularly useful at making VACUUM run faster. |
drh | 9d9cf22 | 2007-02-13 15:01:11 +0000 | [diff] [blame] | 2779 | */ |
| 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 */ |
| 2786 | ){ |
dan | e34162b | 2015-04-01 18:20:25 +0000 | [diff] [blame] | 2787 | sqlite3 *db = pParse->db; |
drh | 9d9cf22 | 2007-02-13 15:01:11 +0000 | [diff] [blame] | 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 */ |
drh | 7601294 | 2021-02-21 21:04:54 +0000 | [diff] [blame] | 2791 | SrcItem *pItem; /* An element of pSelect->pSrc */ |
drh | 9d9cf22 | 2007-02-13 15:01:11 +0000 | [diff] [blame] | 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 */ |
drh | da475b8 | 2013-11-04 21:44:54 +0000 | [diff] [blame] | 2796 | int emptyDestTest = 0; /* Address of test for empty pDest */ |
| 2797 | int emptySrcTest = 0; /* Address of test for empty pSrc */ |
drh | 9d9cf22 | 2007-02-13 15:01:11 +0000 | [diff] [blame] | 2798 | Vdbe *v; /* The VDBE we are building */ |
drh | 6a288a3 | 2008-01-07 19:20:24 +0000 | [diff] [blame] | 2799 | int regAutoinc; /* Memory register used by AUTOINC */ |
drh | f33c9fa | 2007-04-10 18:17:55 +0000 | [diff] [blame] | 2800 | int destHasUniqueIdx = 0; /* True if pDest has a UNIQUE index */ |
drh | b765411 | 2008-01-12 12:48:07 +0000 | [diff] [blame] | 2801 | int regData, regRowid; /* Registers holding data and rowid */ |
drh | 9d9cf22 | 2007-02-13 15:01:11 +0000 | [diff] [blame] | 2802 | |
drh | 935c372 | 2022-02-28 16:44:58 +0000 | [diff] [blame] | 2803 | assert( pSelect!=0 ); |
dan | ebbf08a | 2014-01-18 08:27:02 +0000 | [diff] [blame] | 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". */ |
| 2808 | return 0; |
| 2809 | } |
drh | 9d9cf22 | 2007-02-13 15:01:11 +0000 | [diff] [blame] | 2810 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
drh | 44266ec | 2017-02-16 14:48:08 +0000 | [diff] [blame] | 2811 | if( IsVirtual(pDest) ){ |
drh | 9d9cf22 | 2007-02-13 15:01:11 +0000 | [diff] [blame] | 2812 | return 0; /* tab1 must not be a virtual table */ |
| 2813 | } |
| 2814 | #endif |
| 2815 | if( onError==OE_Default ){ |
drh | e7224a0 | 2011-11-04 00:23:53 +0000 | [diff] [blame] | 2816 | if( pDest->iPKey>=0 ) onError = pDest->keyConf; |
| 2817 | if( onError==OE_Default ) onError = OE_Abort; |
drh | 9d9cf22 | 2007-02-13 15:01:11 +0000 | [diff] [blame] | 2818 | } |
danielk1977 | 5ce240a | 2007-09-03 17:30:06 +0000 | [diff] [blame] | 2819 | assert(pSelect->pSrc); /* allocated even if there is no FROM clause */ |
drh | 9d9cf22 | 2007-02-13 15:01:11 +0000 | [diff] [blame] | 2820 | if( pSelect->pSrc->nSrc!=1 ){ |
| 2821 | return 0; /* FROM clause must have exactly one term */ |
| 2822 | } |
| 2823 | if( pSelect->pSrc->a[0].pSelect ){ |
| 2824 | return 0; /* FROM clause cannot contain a subquery */ |
| 2825 | } |
| 2826 | if( pSelect->pWhere ){ |
| 2827 | return 0; /* SELECT may not have a WHERE clause */ |
| 2828 | } |
| 2829 | if( pSelect->pOrderBy ){ |
| 2830 | return 0; /* SELECT may not have an ORDER BY clause */ |
| 2831 | } |
drh | 8103b7d | 2007-02-24 13:23:51 +0000 | [diff] [blame] | 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. */ |
drh | 9d9cf22 | 2007-02-13 15:01:11 +0000 | [diff] [blame] | 2834 | if( pSelect->pGroupBy ){ |
| 2835 | return 0; /* SELECT may not have a GROUP BY clause */ |
| 2836 | } |
| 2837 | if( pSelect->pLimit ){ |
| 2838 | return 0; /* SELECT may not have a LIMIT clause */ |
| 2839 | } |
drh | 9d9cf22 | 2007-02-13 15:01:11 +0000 | [diff] [blame] | 2840 | if( pSelect->pPrior ){ |
| 2841 | return 0; /* SELECT may not be a compound query */ |
| 2842 | } |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 2843 | if( pSelect->selFlags & SF_Distinct ){ |
drh | 9d9cf22 | 2007-02-13 15:01:11 +0000 | [diff] [blame] | 2844 | return 0; /* SELECT may not be DISTINCT */ |
| 2845 | } |
| 2846 | pEList = pSelect->pEList; |
| 2847 | assert( pEList!=0 ); |
| 2848 | if( pEList->nExpr!=1 ){ |
| 2849 | return 0; /* The result set must have exactly one column */ |
| 2850 | } |
| 2851 | assert( pEList->a[0].pExpr ); |
drh | 1a1d3cd | 2015-11-19 16:33:31 +0000 | [diff] [blame] | 2852 | if( pEList->a[0].pExpr->op!=TK_ASTERISK ){ |
drh | 9d9cf22 | 2007-02-13 15:01:11 +0000 | [diff] [blame] | 2853 | return 0; /* The result set must be the special operator "*" */ |
| 2854 | } |
| 2855 | |
| 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. |
| 2859 | */ |
| 2860 | pItem = pSelect->pSrc->a; |
dan | 41fb5cd | 2012-10-04 19:33:00 +0000 | [diff] [blame] | 2861 | pSrc = sqlite3LocateTableItem(pParse, 0, pItem); |
drh | 9d9cf22 | 2007-02-13 15:01:11 +0000 | [diff] [blame] | 2862 | if( pSrc==0 ){ |
| 2863 | return 0; /* FROM clause does not contain a real table */ |
| 2864 | } |
drh | 21908b2 | 2019-01-17 20:19:35 +0000 | [diff] [blame] | 2865 | if( pSrc->tnum==pDest->tnum && pSrc->pSchema==pDest->pSchema ){ |
drh | 1e32bed | 2020-06-19 13:33:53 +0000 | [diff] [blame] | 2866 | testcase( pSrc!=pDest ); /* Possible due to bad sqlite_schema.rootpage */ |
drh | 9d9cf22 | 2007-02-13 15:01:11 +0000 | [diff] [blame] | 2867 | return 0; /* tab1 and tab2 may not be the same table */ |
| 2868 | } |
drh | 5554827 | 2013-10-23 16:03:07 +0000 | [diff] [blame] | 2869 | if( HasRowid(pDest)!=HasRowid(pSrc) ){ |
| 2870 | return 0; /* source and destination must both be WITHOUT ROWID or not */ |
| 2871 | } |
drh | f38524d | 2021-08-02 16:41:57 +0000 | [diff] [blame] | 2872 | if( !IsOrdinaryTable(pSrc) ){ |
| 2873 | return 0; /* tab2 may not be a view or virtual table */ |
drh | 9d9cf22 | 2007-02-13 15:01:11 +0000 | [diff] [blame] | 2874 | } |
| 2875 | if( pDest->nCol!=pSrc->nCol ){ |
| 2876 | return 0; /* Number of columns must be the same in tab1 and tab2 */ |
| 2877 | } |
| 2878 | if( pDest->iPKey!=pSrc->iPKey ){ |
| 2879 | return 0; /* Both tables must have the same INTEGER PRIMARY KEY */ |
| 2880 | } |
drh | 7b4b74a | 2021-08-20 01:12:39 +0000 | [diff] [blame] | 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 */ |
| 2883 | } |
drh | 9d9cf22 | 2007-02-13 15:01:11 +0000 | [diff] [blame] | 2884 | for(i=0; i<pDest->nCol; i++){ |
dan | 9940e2a | 2014-04-26 14:07:57 +0000 | [diff] [blame] | 2885 | Column *pDestCol = &pDest->aCol[i]; |
| 2886 | Column *pSrcCol = &pSrc->aCol[i]; |
dan | ba68f8f | 2015-11-19 16:46:46 +0000 | [diff] [blame] | 2887 | #ifdef SQLITE_ENABLE_HIDDEN_COLUMNS |
drh | 8257aa8 | 2017-07-26 19:59:13 +0000 | [diff] [blame] | 2888 | if( (db->mDbFlags & DBFLAG_Vacuum)==0 |
dan | aaea314 | 2015-11-19 18:09:05 +0000 | [diff] [blame] | 2889 | && (pDestCol->colFlags | pSrcCol->colFlags) & COLFLAG_HIDDEN |
| 2890 | ){ |
dan | ba68f8f | 2015-11-19 16:46:46 +0000 | [diff] [blame] | 2891 | return 0; /* Neither table may have __hidden__ columns */ |
| 2892 | } |
| 2893 | #endif |
drh | 6ab61d7 | 2019-10-23 15:47:33 +0000 | [diff] [blame] | 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: |
| 2897 | ** |
| 2898 | ** INSERT INTO t2 SELECT * FROM t1; |
| 2899 | ** |
| 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. |
| 2903 | ** |
| 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. |
| 2906 | ** |
| 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. |
| 2910 | */ |
drh | ae3977a | 2019-10-17 16:16:34 +0000 | [diff] [blame] | 2911 | if( (pDestCol->colFlags & COLFLAG_GENERATED) != |
| 2912 | (pSrcCol->colFlags & COLFLAG_GENERATED) ){ |
drh | 6ab61d7 | 2019-10-23 15:47:33 +0000 | [diff] [blame] | 2913 | return 0; /* Both columns have the same generated-column type */ |
drh | ae3977a | 2019-10-17 16:16:34 +0000 | [diff] [blame] | 2914 | } |
drh | 6ab61d7 | 2019-10-23 15:47:33 +0000 | [diff] [blame] | 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. |
| 2918 | */ |
| 2919 | if( (pDestCol->colFlags & COLFLAG_GENERATED)!=0 ){ |
drh | 79cf2b7 | 2021-07-31 20:30:41 +0000 | [diff] [blame] | 2920 | if( sqlite3ExprCompare(0, |
| 2921 | sqlite3ColumnExpr(pSrc, pSrcCol), |
| 2922 | sqlite3ColumnExpr(pDest, pDestCol), -1)!=0 ){ |
drh | 6ab61d7 | 2019-10-23 15:47:33 +0000 | [diff] [blame] | 2923 | testcase( pDestCol->colFlags & COLFLAG_VIRTUAL ); |
| 2924 | testcase( pDestCol->colFlags & COLFLAG_STORED ); |
| 2925 | return 0; /* Different generator expressions */ |
| 2926 | } |
| 2927 | } |
| 2928 | #endif |
dan | 9940e2a | 2014-04-26 14:07:57 +0000 | [diff] [blame] | 2929 | if( pDestCol->affinity!=pSrcCol->affinity ){ |
drh | 9d9cf22 | 2007-02-13 15:01:11 +0000 | [diff] [blame] | 2930 | return 0; /* Affinity must be the same on all columns */ |
| 2931 | } |
drh | 65b4009 | 2021-08-05 15:27:19 +0000 | [diff] [blame] | 2932 | if( sqlite3_stricmp(sqlite3ColumnColl(pDestCol), |
| 2933 | sqlite3ColumnColl(pSrcCol))!=0 ){ |
drh | 9d9cf22 | 2007-02-13 15:01:11 +0000 | [diff] [blame] | 2934 | return 0; /* Collating sequence must be the same on all columns */ |
| 2935 | } |
dan | 9940e2a | 2014-04-26 14:07:57 +0000 | [diff] [blame] | 2936 | if( pDestCol->notNull && !pSrcCol->notNull ){ |
drh | 9d9cf22 | 2007-02-13 15:01:11 +0000 | [diff] [blame] | 2937 | return 0; /* tab2 must be NOT NULL if tab1 is */ |
| 2938 | } |
drh | 453e026 | 2014-04-26 17:52:08 +0000 | [diff] [blame] | 2939 | /* Default values for second and subsequent columns need to match. */ |
drh | ae3977a | 2019-10-17 16:16:34 +0000 | [diff] [blame] | 2940 | if( (pDestCol->colFlags & COLFLAG_GENERATED)==0 && i>0 ){ |
drh | 79cf2b7 | 2021-07-31 20:30:41 +0000 | [diff] [blame] | 2941 | Expr *pDestExpr = sqlite3ColumnExpr(pDest, pDestCol); |
| 2942 | Expr *pSrcExpr = sqlite3ColumnExpr(pSrc, pSrcCol); |
| 2943 | assert( pDestExpr==0 || pDestExpr->op==TK_SPAN ); |
drh | f975107 | 2021-10-07 13:40:29 +0000 | [diff] [blame] | 2944 | assert( pDestExpr==0 || !ExprHasProperty(pDestExpr, EP_IntValue) ); |
drh | 79cf2b7 | 2021-07-31 20:30:41 +0000 | [diff] [blame] | 2945 | assert( pSrcExpr==0 || pSrcExpr->op==TK_SPAN ); |
drh | f975107 | 2021-10-07 13:40:29 +0000 | [diff] [blame] | 2946 | assert( pSrcExpr==0 || !ExprHasProperty(pSrcExpr, EP_IntValue) ); |
drh | 79cf2b7 | 2021-07-31 20:30:41 +0000 | [diff] [blame] | 2947 | if( (pDestExpr==0)!=(pSrcExpr==0) |
| 2948 | || (pDestExpr!=0 && strcmp(pDestExpr->u.zToken, |
| 2949 | pSrcExpr->u.zToken)!=0) |
drh | 94fa9c4 | 2016-02-27 21:16:04 +0000 | [diff] [blame] | 2950 | ){ |
| 2951 | return 0; /* Default values must be the same for all columns */ |
| 2952 | } |
dan | 9940e2a | 2014-04-26 14:07:57 +0000 | [diff] [blame] | 2953 | } |
drh | 9d9cf22 | 2007-02-13 15:01:11 +0000 | [diff] [blame] | 2954 | } |
| 2955 | for(pDestIdx=pDest->pIndex; pDestIdx; pDestIdx=pDestIdx->pNext){ |
drh | 5f1d1d9 | 2014-07-31 22:59:04 +0000 | [diff] [blame] | 2956 | if( IsUniqueIndex(pDestIdx) ){ |
drh | f33c9fa | 2007-04-10 18:17:55 +0000 | [diff] [blame] | 2957 | destHasUniqueIdx = 1; |
| 2958 | } |
drh | 9d9cf22 | 2007-02-13 15:01:11 +0000 | [diff] [blame] | 2959 | for(pSrcIdx=pSrc->pIndex; pSrcIdx; pSrcIdx=pSrcIdx->pNext){ |
| 2960 | if( xferCompatibleIndex(pDestIdx, pSrcIdx) ) break; |
| 2961 | } |
| 2962 | if( pSrcIdx==0 ){ |
| 2963 | return 0; /* pDestIdx has no corresponding index in pSrc */ |
| 2964 | } |
drh | e3bd232 | 2019-04-05 16:38:12 +0000 | [diff] [blame] | 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. */ |
drh | 86223e8 | 2019-04-05 15:44:55 +0000 | [diff] [blame] | 2970 | return 0; /* Corrupt schema - two indexes on the same btree */ |
| 2971 | } |
drh | 9d9cf22 | 2007-02-13 15:01:11 +0000 | [diff] [blame] | 2972 | } |
drh | 7fc2f41 | 2007-03-29 00:08:24 +0000 | [diff] [blame] | 2973 | #ifndef SQLITE_OMIT_CHECK |
drh | 619a130 | 2013-08-01 13:04:46 +0000 | [diff] [blame] | 2974 | if( pDest->pCheck && sqlite3ExprListCompare(pSrc->pCheck,pDest->pCheck,-1) ){ |
drh | 8103b7d | 2007-02-24 13:23:51 +0000 | [diff] [blame] | 2975 | return 0; /* Tables have different CHECK constraints. Ticket #2252 */ |
| 2976 | } |
drh | 7fc2f41 | 2007-03-29 00:08:24 +0000 | [diff] [blame] | 2977 | #endif |
drh | 713de34 | 2011-04-24 22:56:07 +0000 | [diff] [blame] | 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] |
| 2985 | */ |
drh | 78b2fa8 | 2021-10-07 12:11:20 +0000 | [diff] [blame] | 2986 | assert( IsOrdinaryTable(pDest) ); |
drh | f38524d | 2021-08-02 16:41:57 +0000 | [diff] [blame] | 2987 | if( (db->flags & SQLITE_ForeignKeys)!=0 && pDest->u.tab.pFKey!=0 ){ |
drh | 713de34 | 2011-04-24 22:56:07 +0000 | [diff] [blame] | 2988 | return 0; |
| 2989 | } |
| 2990 | #endif |
dan | e34162b | 2015-04-01 18:20:25 +0000 | [diff] [blame] | 2991 | if( (db->flags & SQLITE_CountRows)!=0 ){ |
drh | ccdf1ba | 2011-11-04 14:36:02 +0000 | [diff] [blame] | 2992 | return 0; /* xfer opt does not play well with PRAGMA count_changes */ |
dan | 1696124 | 2011-09-30 12:01:01 +0000 | [diff] [blame] | 2993 | } |
drh | 9d9cf22 | 2007-02-13 15:01:11 +0000 | [diff] [blame] | 2994 | |
drh | ccdf1ba | 2011-11-04 14:36:02 +0000 | [diff] [blame] | 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. |
drh | 9d9cf22 | 2007-02-13 15:01:11 +0000 | [diff] [blame] | 2998 | */ |
drh | dd73521 | 2007-02-24 13:53:05 +0000 | [diff] [blame] | 2999 | #ifdef SQLITE_TEST |
| 3000 | sqlite3_xferopt_count++; |
| 3001 | #endif |
dan | e34162b | 2015-04-01 18:20:25 +0000 | [diff] [blame] | 3002 | iDbSrc = sqlite3SchemaToIndex(db, pSrc->pSchema); |
drh | 9d9cf22 | 2007-02-13 15:01:11 +0000 | [diff] [blame] | 3003 | v = sqlite3GetVdbe(pParse); |
drh | f53e9b5 | 2007-08-29 13:45:58 +0000 | [diff] [blame] | 3004 | sqlite3CodeVerifySchema(pParse, iDbSrc); |
drh | 9d9cf22 | 2007-02-13 15:01:11 +0000 | [diff] [blame] | 3005 | iSrc = pParse->nTab++; |
| 3006 | iDest = pParse->nTab++; |
drh | 6a288a3 | 2008-01-07 19:20:24 +0000 | [diff] [blame] | 3007 | regAutoinc = autoIncBegin(pParse, iDbDest, pDest); |
drh | b765411 | 2008-01-12 12:48:07 +0000 | [diff] [blame] | 3008 | regData = sqlite3GetTempReg(pParse); |
dan | 7aae735 | 2020-12-10 18:06:24 +0000 | [diff] [blame] | 3009 | sqlite3VdbeAddOp2(v, OP_Null, 0, regData); |
drh | b765411 | 2008-01-12 12:48:07 +0000 | [diff] [blame] | 3010 | regRowid = sqlite3GetTempReg(pParse); |
drh | 9d9cf22 | 2007-02-13 15:01:11 +0000 | [diff] [blame] | 3011 | sqlite3OpenTable(pParse, iDest, iDbDest, pDest, OP_OpenWrite); |
dan | 427ebba | 2013-11-05 16:39:31 +0000 | [diff] [blame] | 3012 | assert( HasRowid(pDest) || destHasUniqueIdx ); |
drh | 8257aa8 | 2017-07-26 19:59:13 +0000 | [diff] [blame] | 3013 | if( (db->mDbFlags & DBFLAG_Vacuum)==0 && ( |
dan | e34162b | 2015-04-01 18:20:25 +0000 | [diff] [blame] | 3014 | (pDest->iPKey<0 && pDest->pIndex!=0) /* (1) */ |
drh | 9d9cf22 | 2007-02-13 15:01:11 +0000 | [diff] [blame] | 3015 | || destHasUniqueIdx /* (2) */ |
| 3016 | || (onError!=OE_Abort && onError!=OE_Rollback) /* (3) */ |
dan | e34162b | 2015-04-01 18:20:25 +0000 | [diff] [blame] | 3017 | )){ |
drh | 9d9cf22 | 2007-02-13 15:01:11 +0000 | [diff] [blame] | 3018 | /* In some circumstances, we are able to run the xfer optimization |
dan | e34162b | 2015-04-01 18:20:25 +0000 | [diff] [blame] | 3019 | ** only if the destination table is initially empty. Unless the |
drh | 8257aa8 | 2017-07-26 19:59:13 +0000 | [diff] [blame] | 3020 | ** DBFLAG_Vacuum flag is set, this block generates code to make |
| 3021 | ** that determination. If DBFLAG_Vacuum is set, then the destination |
dan | e34162b | 2015-04-01 18:20:25 +0000 | [diff] [blame] | 3022 | ** table is always empty. |
| 3023 | ** |
| 3024 | ** Conditions under which the destination must be empty: |
drh | bd36ba6 | 2007-03-31 13:00:26 +0000 | [diff] [blame] | 3025 | ** |
drh | 9d9cf22 | 2007-02-13 15:01:11 +0000 | [diff] [blame] | 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.) |
| 3029 | ** |
| 3030 | ** (2) The destination has a unique index. (The xfer optimization |
drh | bd36ba6 | 2007-03-31 13:00:26 +0000 | [diff] [blame] | 3031 | ** is unable to test uniqueness.) |
| 3032 | ** |
| 3033 | ** (3) onError is something other than OE_Abort and OE_Rollback. |
drh | f33c9fa | 2007-04-10 18:17:55 +0000 | [diff] [blame] | 3034 | */ |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 3035 | addr1 = sqlite3VdbeAddOp2(v, OP_Rewind, iDest, 0); VdbeCoverage(v); |
drh | 2991ba0 | 2015-09-02 18:19:00 +0000 | [diff] [blame] | 3036 | emptyDestTest = sqlite3VdbeAddOp0(v, OP_Goto); |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 3037 | sqlite3VdbeJumpHere(v, addr1); |
drh | 9d9cf22 | 2007-02-13 15:01:11 +0000 | [diff] [blame] | 3038 | } |
drh | 5554827 | 2013-10-23 16:03:07 +0000 | [diff] [blame] | 3039 | if( HasRowid(pSrc) ){ |
drh | c9b9dea | 2016-11-15 02:46:39 +0000 | [diff] [blame] | 3040 | u8 insFlags; |
drh | 5554827 | 2013-10-23 16:03:07 +0000 | [diff] [blame] | 3041 | sqlite3OpenTable(pParse, iSrc, iDbSrc, pSrc, OP_OpenRead); |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 3042 | emptySrcTest = sqlite3VdbeAddOp2(v, OP_Rewind, iSrc, 0); VdbeCoverage(v); |
drh | 5554827 | 2013-10-23 16:03:07 +0000 | [diff] [blame] | 3043 | if( pDest->iPKey>=0 ){ |
| 3044 | addr1 = sqlite3VdbeAddOp2(v, OP_Rowid, iSrc, regRowid); |
dan | 036e067 | 2020-12-08 20:19:07 +0000 | [diff] [blame] | 3045 | if( (db->mDbFlags & DBFLAG_Vacuum)==0 ){ |
| 3046 | sqlite3VdbeVerifyAbortable(v, onError); |
| 3047 | addr2 = sqlite3VdbeAddOp3(v, OP_NotExists, iDest, 0, regRowid); |
| 3048 | VdbeCoverage(v); |
| 3049 | sqlite3RowidConstraint(pParse, onError, pDest); |
| 3050 | sqlite3VdbeJumpHere(v, addr2); |
| 3051 | } |
drh | 5554827 | 2013-10-23 16:03:07 +0000 | [diff] [blame] | 3052 | autoIncStep(pParse, regAutoinc, regRowid); |
drh | 4e61e88 | 2019-04-04 14:00:23 +0000 | [diff] [blame] | 3053 | }else if( pDest->pIndex==0 && !(db->mDbFlags & DBFLAG_VacuumInto) ){ |
drh | 5554827 | 2013-10-23 16:03:07 +0000 | [diff] [blame] | 3054 | addr1 = sqlite3VdbeAddOp2(v, OP_NewRowid, iDest, regRowid); |
| 3055 | }else{ |
| 3056 | addr1 = sqlite3VdbeAddOp2(v, OP_Rowid, iSrc, regRowid); |
| 3057 | assert( (pDest->tabFlags & TF_Autoincrement)==0 ); |
| 3058 | } |
dan | 7aae735 | 2020-12-10 18:06:24 +0000 | [diff] [blame] | 3059 | |
drh | 8257aa8 | 2017-07-26 19:59:13 +0000 | [diff] [blame] | 3060 | if( db->mDbFlags & DBFLAG_Vacuum ){ |
drh | 86b40df | 2017-08-01 19:53:43 +0000 | [diff] [blame] | 3061 | sqlite3VdbeAddOp1(v, OP_SeekEnd, iDest); |
dan | 7aae735 | 2020-12-10 18:06:24 +0000 | [diff] [blame] | 3062 | insFlags = OPFLAG_APPEND|OPFLAG_USESEEKRESULT|OPFLAG_PREFORMAT; |
drh | c9b9dea | 2016-11-15 02:46:39 +0000 | [diff] [blame] | 3063 | }else{ |
dan | 7aae735 | 2020-12-10 18:06:24 +0000 | [diff] [blame] | 3064 | insFlags = OPFLAG_NCHANGE|OPFLAG_LASTROWID|OPFLAG_APPEND|OPFLAG_PREFORMAT; |
drh | c9b9dea | 2016-11-15 02:46:39 +0000 | [diff] [blame] | 3065 | } |
dan | 7aae735 | 2020-12-10 18:06:24 +0000 | [diff] [blame] | 3066 | #ifdef SQLITE_ENABLE_PREUPDATE_HOOK |
dan | a55a839 | 2021-02-18 15:59:37 +0000 | [diff] [blame] | 3067 | if( (db->mDbFlags & DBFLAG_Vacuum)==0 ){ |
| 3068 | sqlite3VdbeAddOp3(v, OP_RowData, iSrc, regData, 1); |
| 3069 | insFlags &= ~OPFLAG_PREFORMAT; |
| 3070 | }else |
dan | 7aae735 | 2020-12-10 18:06:24 +0000 | [diff] [blame] | 3071 | #endif |
dan | a55a839 | 2021-02-18 15:59:37 +0000 | [diff] [blame] | 3072 | { |
| 3073 | sqlite3VdbeAddOp3(v, OP_RowCell, iDest, iSrc, regRowid); |
| 3074 | } |
| 3075 | sqlite3VdbeAddOp3(v, OP_Insert, iDest, regData, regRowid); |
| 3076 | if( (db->mDbFlags & DBFLAG_Vacuum)==0 ){ |
| 3077 | sqlite3VdbeChangeP4(v, -1, (char*)pDest, P4_TABLE); |
| 3078 | } |
dan | 7aae735 | 2020-12-10 18:06:24 +0000 | [diff] [blame] | 3079 | sqlite3VdbeChangeP5(v, insFlags); |
| 3080 | |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 3081 | sqlite3VdbeAddOp2(v, OP_Next, iSrc, addr1); VdbeCoverage(v); |
drh | 5554827 | 2013-10-23 16:03:07 +0000 | [diff] [blame] | 3082 | sqlite3VdbeAddOp2(v, OP_Close, iSrc, 0); |
| 3083 | sqlite3VdbeAddOp2(v, OP_Close, iDest, 0); |
drh | 95bad4c | 2007-03-28 18:04:10 +0000 | [diff] [blame] | 3084 | }else{ |
drh | da475b8 | 2013-11-04 21:44:54 +0000 | [diff] [blame] | 3085 | sqlite3TableLock(pParse, iDbDest, pDest->tnum, 1, pDest->zName); |
| 3086 | sqlite3TableLock(pParse, iDbSrc, pSrc->tnum, 0, pSrc->zName); |
drh | 95bad4c | 2007-03-28 18:04:10 +0000 | [diff] [blame] | 3087 | } |
drh | 9d9cf22 | 2007-02-13 15:01:11 +0000 | [diff] [blame] | 3088 | for(pDestIdx=pDest->pIndex; pDestIdx; pDestIdx=pDestIdx->pNext){ |
drh | 41b9ca2 | 2015-07-28 18:53:37 +0000 | [diff] [blame] | 3089 | u8 idxInsFlags = 0; |
drh | 1b7ecbb | 2009-05-03 01:00:59 +0000 | [diff] [blame] | 3090 | for(pSrcIdx=pSrc->pIndex; ALWAYS(pSrcIdx); pSrcIdx=pSrcIdx->pNext){ |
drh | 9d9cf22 | 2007-02-13 15:01:11 +0000 | [diff] [blame] | 3091 | if( xferCompatibleIndex(pDestIdx, pSrcIdx) ) break; |
| 3092 | } |
| 3093 | assert( pSrcIdx ); |
drh | 2ec2fb2 | 2013-11-06 19:59:23 +0000 | [diff] [blame] | 3094 | sqlite3VdbeAddOp3(v, OP_OpenRead, iSrc, pSrcIdx->tnum, iDbSrc); |
| 3095 | sqlite3VdbeSetP4KeyInfo(pParse, pSrcIdx); |
drh | d4e70eb | 2008-01-02 00:34:36 +0000 | [diff] [blame] | 3096 | VdbeComment((v, "%s", pSrcIdx->zName)); |
drh | 2ec2fb2 | 2013-11-06 19:59:23 +0000 | [diff] [blame] | 3097 | sqlite3VdbeAddOp3(v, OP_OpenWrite, iDest, pDestIdx->tnum, iDbDest); |
| 3098 | sqlite3VdbeSetP4KeyInfo(pParse, pDestIdx); |
dan | 5988572 | 2013-10-04 18:17:18 +0000 | [diff] [blame] | 3099 | sqlite3VdbeChangeP5(v, OPFLAG_BULKCSR); |
danielk1977 | 207872a | 2008-01-03 07:54:23 +0000 | [diff] [blame] | 3100 | VdbeComment((v, "%s", pDestIdx->zName)); |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 3101 | addr1 = sqlite3VdbeAddOp2(v, OP_Rewind, iSrc, 0); VdbeCoverage(v); |
drh | 8257aa8 | 2017-07-26 19:59:13 +0000 | [diff] [blame] | 3102 | if( db->mDbFlags & DBFLAG_Vacuum ){ |
dan | e34162b | 2015-04-01 18:20:25 +0000 | [diff] [blame] | 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 |
drh | 86b40df | 2017-08-01 19:53:43 +0000 | [diff] [blame] | 3108 | ** of every OP_IdxInsert opcode, an OP_SeekEnd is added before the |
dan | e34162b | 2015-04-01 18:20:25 +0000 | [diff] [blame] | 3109 | ** OP_IdxInsert to seek to the point within the b-tree where each key |
| 3110 | ** should be inserted. This is faster. |
| 3111 | ** |
| 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 |
| 3116 | ** sorted order. */ |
dan | a55a839 | 2021-02-18 15:59:37 +0000 | [diff] [blame] | 3117 | for(i=0; i<pSrcIdx->nColumn; i++){ |
| 3118 | const char *zColl = pSrcIdx->azColl[i]; |
| 3119 | if( sqlite3_stricmp(sqlite3StrBINARY, zColl) ) break; |
| 3120 | } |
| 3121 | if( i==pSrcIdx->nColumn ){ |
| 3122 | idxInsFlags = OPFLAG_USESEEKRESULT|OPFLAG_PREFORMAT; |
| 3123 | sqlite3VdbeAddOp1(v, OP_SeekEnd, iDest); |
| 3124 | sqlite3VdbeAddOp2(v, OP_RowCell, iDest, iSrc); |
dan | e34162b | 2015-04-01 18:20:25 +0000 | [diff] [blame] | 3125 | } |
drh | c84ad31 | 2020-02-06 20:46:08 +0000 | [diff] [blame] | 3126 | }else if( !HasRowid(pSrc) && pDestIdx->idxType==SQLITE_IDXTYPE_PRIMARYKEY ){ |
drh | 41b9ca2 | 2015-07-28 18:53:37 +0000 | [diff] [blame] | 3127 | idxInsFlags |= OPFLAG_NCHANGE; |
| 3128 | } |
dan | 7aae735 | 2020-12-10 18:06:24 +0000 | [diff] [blame] | 3129 | if( idxInsFlags!=(OPFLAG_USESEEKRESULT|OPFLAG_PREFORMAT) ){ |
dan | cd1b2d0 | 2020-12-09 20:33:51 +0000 | [diff] [blame] | 3130 | sqlite3VdbeAddOp3(v, OP_RowData, iSrc, regData, 1); |
dan | a55a839 | 2021-02-18 15:59:37 +0000 | [diff] [blame] | 3131 | if( (db->mDbFlags & DBFLAG_Vacuum)==0 |
| 3132 | && !HasRowid(pDest) |
| 3133 | && IsPrimaryKeyIndex(pDestIdx) |
| 3134 | ){ |
dan | fadc0e3 | 2021-02-18 12:18:10 +0000 | [diff] [blame] | 3135 | codeWithoutRowidPreupdate(pParse, pDest, iDest, regData); |
| 3136 | } |
dan | cd1b2d0 | 2020-12-09 20:33:51 +0000 | [diff] [blame] | 3137 | } |
dan | 7aae735 | 2020-12-10 18:06:24 +0000 | [diff] [blame] | 3138 | sqlite3VdbeAddOp2(v, OP_IdxInsert, iDest, regData); |
| 3139 | sqlite3VdbeChangeP5(v, idxInsFlags|OPFLAG_APPEND); |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 3140 | sqlite3VdbeAddOp2(v, OP_Next, iSrc, addr1+1); VdbeCoverage(v); |
drh | 9d9cf22 | 2007-02-13 15:01:11 +0000 | [diff] [blame] | 3141 | sqlite3VdbeJumpHere(v, addr1); |
drh | 5554827 | 2013-10-23 16:03:07 +0000 | [diff] [blame] | 3142 | sqlite3VdbeAddOp2(v, OP_Close, iSrc, 0); |
| 3143 | sqlite3VdbeAddOp2(v, OP_Close, iDest, 0); |
drh | 9d9cf22 | 2007-02-13 15:01:11 +0000 | [diff] [blame] | 3144 | } |
drh | aceb31b | 2014-02-08 01:40:27 +0000 | [diff] [blame] | 3145 | if( emptySrcTest ) sqlite3VdbeJumpHere(v, emptySrcTest); |
drh | b765411 | 2008-01-12 12:48:07 +0000 | [diff] [blame] | 3146 | sqlite3ReleaseTempReg(pParse, regRowid); |
| 3147 | sqlite3ReleaseTempReg(pParse, regData); |
drh | 9d9cf22 | 2007-02-13 15:01:11 +0000 | [diff] [blame] | 3148 | if( emptyDestTest ){ |
drh | 1dd518c | 2016-10-03 02:59:33 +0000 | [diff] [blame] | 3149 | sqlite3AutoincrementEnd(pParse); |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 3150 | sqlite3VdbeAddOp2(v, OP_Halt, SQLITE_OK, 0); |
drh | 9d9cf22 | 2007-02-13 15:01:11 +0000 | [diff] [blame] | 3151 | sqlite3VdbeJumpHere(v, emptyDestTest); |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 3152 | sqlite3VdbeAddOp2(v, OP_Close, iDest, 0); |
drh | 9d9cf22 | 2007-02-13 15:01:11 +0000 | [diff] [blame] | 3153 | return 0; |
| 3154 | }else{ |
| 3155 | return 1; |
| 3156 | } |
| 3157 | } |
| 3158 | #endif /* SQLITE_OMIT_XFER_OPT */ |