drh | cce7d17 | 2000-05-31 15:34:51 | [diff] [blame] | 1 | /* |
drh | b19a2bc | 2001-09-16 00:13:26 | [diff] [blame] | 2 | ** 2001 September 15 |
drh | cce7d17 | 2000-05-31 15:34:51 | [diff] [blame] | 3 | ** |
drh | b19a2bc | 2001-09-16 00:13:26 | [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 | [diff] [blame] | 6 | ** |
drh | b19a2bc | 2001-09-16 00:13:26 | [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 | [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 | [diff] [blame] | 13 | ** to handle INSERT statements in SQLite. |
drh | cce7d17 | 2000-05-31 15:34:51 | [diff] [blame] | 14 | */ |
| 15 | #include "sqliteInt.h" |
| 16 | |
| 17 | /* |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 18 | ** Generate code that will |
drh | dd9930e | 2013-10-23 23:37:02 | [diff] [blame] | 19 | ** |
drh | 26198bb | 2013-10-31 11:15:09 | [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 | [diff] [blame] | 25 | */ |
| 26 | void sqlite3OpenTable( |
drh | 2ec2fb2 | 2013-11-06 19:59:23 | [diff] [blame] | 27 | Parse *pParse, /* Generate code into this VDBE */ |
drh | bbb5e4e | 2009-04-30 00:11:09 | [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 | [diff] [blame] | 34 | assert( !IsVirtual(pTab) ); |
drh | 289a0c8 | 2020-08-15 22:23:00 | [diff] [blame] | 35 | assert( pParse->pVdbe!=0 ); |
| 36 | v = pParse->pVdbe; |
drh | bbb5e4e | 2009-04-30 00:11:09 | [diff] [blame] | 37 | assert( opcode==OP_OpenWrite || opcode==OP_OpenRead ); |
drh | 40ee729 | 2023-06-20 17:45:19 | [diff] [blame] | 38 | if( !pParse->db->noSharedCache ){ |
| 39 | sqlite3TableLock(pParse, iDb, pTab->tnum, |
| 40 | (opcode==OP_OpenWrite)?1:0, pTab->zName); |
| 41 | } |
drh | ec95c44 | 2013-10-23 01:57:32 | [diff] [blame] | 42 | if( HasRowid(pTab) ){ |
drh | 0b0b3a9 | 2019-10-17 18:35:57 | [diff] [blame] | 43 | sqlite3VdbeAddOp4Int(v, opcode, iCur, pTab->tnum, iDb, pTab->nNVCol); |
drh | dd9930e | 2013-10-23 23:37:02 | [diff] [blame] | 44 | VdbeComment((v, "%s", pTab->zName)); |
drh | 26198bb | 2013-10-31 11:15:09 | [diff] [blame] | 45 | }else{ |
drh | dd9930e | 2013-10-23 23:37:02 | [diff] [blame] | 46 | Index *pPk = sqlite3PrimaryKeyIndex(pTab); |
| 47 | assert( pPk!=0 ); |
drh | 3907560 | 2022-01-01 12:26:01 | [diff] [blame] | 48 | assert( pPk->tnum==pTab->tnum || CORRUPT_DB ); |
drh | 2ec2fb2 | 2013-11-06 19:59:23 | [diff] [blame] | 49 | sqlite3VdbeAddOp3(v, opcode, iCur, pPk->tnum, iDb); |
| 50 | sqlite3VdbeSetP4KeyInfo(pParse, pPk); |
drh | ec95c44 | 2013-10-23 01:57:32 | [diff] [blame] | 51 | VdbeComment((v, "%s", pTab->zName)); |
| 52 | } |
drh | bbb5e4e | 2009-04-30 00:11:09 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | /* |
dan | 69f8bb9 | 2009-08-13 19:21:16 | [diff] [blame] | 56 | ** Return a pointer to the column affinity string associated with index |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 57 | ** pIdx. A column affinity string has one character for each column in |
dan | 69f8bb9 | 2009-08-13 19:21:16 | [diff] [blame] | 58 | ** the table, according to the affinity of the column: |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 | [diff] [blame] | 59 | ** |
| 60 | ** Character Column affinity |
| 61 | ** ------------------------------ |
drh | 05883a3 | 2015-06-02 15:32:08 | [diff] [blame] | 62 | ** 'A' BLOB |
drh | 4583c37 | 2014-09-19 20:13:25 | [diff] [blame] | 63 | ** 'B' TEXT |
| 64 | ** 'C' NUMERIC |
| 65 | ** 'D' INTEGER |
| 66 | ** 'F' REAL |
drh | 2d401ab | 2008-01-10 23:50:11 | [diff] [blame] | 67 | ** |
drh | 4583c37 | 2014-09-19 20:13:25 | [diff] [blame] | 68 | ** An extra 'D' is appended to the end of the string to cover the |
drh | 2d401ab | 2008-01-10 23:50:11 | [diff] [blame] | 69 | ** rowid that appears as the last column in every index. |
dan | 69f8bb9 | 2009-08-13 19:21:16 | [diff] [blame] | 70 | ** |
| 71 | ** Memory for the buffer containing the column index affinity string |
| 72 | ** is managed along with the rest of the Index structure. It will be |
| 73 | ** released when sqlite3DeleteIndex() is called. |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 | [diff] [blame] | 74 | */ |
drh | ca4cf12 | 2023-02-27 18:55:37 | [diff] [blame] | 75 | static SQLITE_NOINLINE const char *computeIndexAffStr(sqlite3 *db, Index *pIdx){ |
| 76 | /* The first time a column affinity string for a particular index is |
| 77 | ** required, it is allocated and populated here. It is then stored as |
| 78 | ** a member of the Index structure for subsequent use. |
| 79 | ** |
| 80 | ** The column affinity string will eventually be deleted by |
| 81 | ** sqliteDeleteIndex() when the Index structure itself is cleaned |
| 82 | ** up. |
| 83 | */ |
| 84 | int n; |
| 85 | Table *pTab = pIdx->pTable; |
| 86 | pIdx->zColAff = (char *)sqlite3DbMallocRaw(0, pIdx->nColumn+1); |
danielk1977 | a37cdde | 2004-05-16 11:15:36 | [diff] [blame] | 87 | if( !pIdx->zColAff ){ |
drh | ca4cf12 | 2023-02-27 18:55:37 | [diff] [blame] | 88 | sqlite3OomFault(db); |
| 89 | return 0; |
danielk1977 | a37cdde | 2004-05-16 11:15:36 | [diff] [blame] | 90 | } |
drh | ca4cf12 | 2023-02-27 18:55:37 | [diff] [blame] | 91 | for(n=0; n<pIdx->nColumn; n++){ |
| 92 | i16 x = pIdx->aiColumn[n]; |
| 93 | char aff; |
| 94 | if( x>=0 ){ |
| 95 | aff = pTab->aCol[x].affinity; |
| 96 | }else if( x==XN_ROWID ){ |
| 97 | aff = SQLITE_AFF_INTEGER; |
| 98 | }else{ |
| 99 | assert( x==XN_EXPR ); |
| 100 | assert( pIdx->bHasExpr ); |
| 101 | assert( pIdx->aColExpr!=0 ); |
| 102 | aff = sqlite3ExprAffinity(pIdx->aColExpr->a[n].pExpr); |
| 103 | } |
| 104 | if( aff<SQLITE_AFF_BLOB ) aff = SQLITE_AFF_BLOB; |
| 105 | if( aff>SQLITE_AFF_NUMERIC) aff = SQLITE_AFF_NUMERIC; |
| 106 | pIdx->zColAff[n] = aff; |
| 107 | } |
| 108 | pIdx->zColAff[n] = 0; |
dan | 69f8bb9 | 2009-08-13 19:21:16 | [diff] [blame] | 109 | return pIdx->zColAff; |
danielk1977 | a37cdde | 2004-05-16 11:15:36 | [diff] [blame] | 110 | } |
drh | ca4cf12 | 2023-02-27 18:55:37 | [diff] [blame] | 111 | const char *sqlite3IndexAffinityStr(sqlite3 *db, Index *pIdx){ |
| 112 | if( !pIdx->zColAff ) return computeIndexAffStr(db, pIdx); |
| 113 | return pIdx->zColAff; |
| 114 | } |
| 115 | |
danielk1977 | a37cdde | 2004-05-16 11:15:36 | [diff] [blame] | 116 | |
| 117 | /* |
drh | 5fdb9a3 | 2022-11-01 00:52:22 | [diff] [blame] | 118 | ** Compute an affinity string for a table. Space is obtained |
| 119 | ** from sqlite3DbMalloc(). The caller is responsible for freeing |
| 120 | ** the space when done. |
| 121 | */ |
| 122 | char *sqlite3TableAffinityStr(sqlite3 *db, const Table *pTab){ |
| 123 | char *zColAff; |
| 124 | zColAff = (char *)sqlite3DbMallocRaw(db, pTab->nCol+1); |
| 125 | if( zColAff ){ |
| 126 | int i, j; |
| 127 | for(i=j=0; i<pTab->nCol; i++){ |
| 128 | if( (pTab->aCol[i].colFlags & COLFLAG_VIRTUAL)==0 ){ |
| 129 | zColAff[j++] = pTab->aCol[i].affinity; |
| 130 | } |
| 131 | } |
| 132 | do{ |
| 133 | zColAff[j--] = 0; |
| 134 | }while( j>=0 && zColAff[j]<=SQLITE_AFF_BLOB ); |
| 135 | } |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 136 | return zColAff; |
drh | 5fdb9a3 | 2022-11-01 00:52:22 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | /* |
drh | 71c770f | 2021-08-19 16:29:33 | [diff] [blame] | 140 | ** Make changes to the evolving bytecode to do affinity transformations |
| 141 | ** of values that are about to be gathered into a row for table pTab. |
| 142 | ** |
| 143 | ** For ordinary (legacy, non-strict) tables: |
| 144 | ** ----------------------------------------- |
| 145 | ** |
drh | 57bf4a8 | 2014-02-17 14:59:22 | [diff] [blame] | 146 | ** Compute the affinity string for table pTab, if it has not already been |
drh | 05883a3 | 2015-06-02 15:32:08 | [diff] [blame] | 147 | ** computed. As an optimization, omit trailing SQLITE_AFF_BLOB affinities. |
drh | 57bf4a8 | 2014-02-17 14:59:22 | [diff] [blame] | 148 | ** |
drh | 71c770f | 2021-08-19 16:29:33 | [diff] [blame] | 149 | ** If the affinity string is empty (because it was all SQLITE_AFF_BLOB entries |
| 150 | ** which were then optimized out) then this routine becomes a no-op. |
| 151 | ** |
| 152 | ** Otherwise if iReg>0 then code an OP_Affinity opcode that will set the |
| 153 | ** affinities for register iReg and following. Or if iReg==0, |
drh | 57bf4a8 | 2014-02-17 14:59:22 | [diff] [blame] | 154 | ** then just set the P4 operand of the previous opcode (which should be |
| 155 | ** an OP_MakeRecord) to the affinity string. |
| 156 | ** |
drh | b6e8fd1 | 2014-03-06 01:56:33 | [diff] [blame] | 157 | ** A column affinity string has one character per column: |
danielk1977 | a37cdde | 2004-05-16 11:15:36 | [diff] [blame] | 158 | ** |
drh | 71c770f | 2021-08-19 16:29:33 | [diff] [blame] | 159 | ** Character Column affinity |
| 160 | ** --------- --------------- |
| 161 | ** 'A' BLOB |
| 162 | ** 'B' TEXT |
| 163 | ** 'C' NUMERIC |
| 164 | ** 'D' INTEGER |
| 165 | ** 'E' REAL |
| 166 | ** |
| 167 | ** For STRICT tables: |
| 168 | ** ------------------ |
| 169 | ** |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 170 | ** Generate an appropriate OP_TypeCheck opcode that will verify the |
drh | 71c770f | 2021-08-19 16:29:33 | [diff] [blame] | 171 | ** datatypes against the column definitions in pTab. If iReg==0, that |
| 172 | ** means an OP_MakeRecord opcode has already been generated and should be |
| 173 | ** the last opcode generated. The new OP_TypeCheck needs to be inserted |
| 174 | ** before the OP_MakeRecord. The new OP_TypeCheck should use the same |
| 175 | ** register set as the OP_MakeRecord. If iReg>0 then register iReg is |
| 176 | ** the first of a series of registers that will form the new record. |
| 177 | ** Apply the type checking to that array of registers. |
danielk1977 | a37cdde | 2004-05-16 11:15:36 | [diff] [blame] | 178 | */ |
drh | 57bf4a8 | 2014-02-17 14:59:22 | [diff] [blame] | 179 | void sqlite3TableAffinity(Vdbe *v, Table *pTab, int iReg){ |
drh | 5fdb9a3 | 2022-11-01 00:52:22 | [diff] [blame] | 180 | int i; |
drh | 72532f5 | 2021-08-18 19:22:27 | [diff] [blame] | 181 | char *zColAff; |
| 182 | if( pTab->tabFlags & TF_Strict ){ |
| 183 | if( iReg==0 ){ |
| 184 | /* Move the previous opcode (which should be OP_MakeRecord) forward |
| 185 | ** by one slot and insert a new OP_TypeCheck where the current |
| 186 | ** OP_MakeRecord is found */ |
| 187 | VdbeOp *pPrev; |
drh | bcf25e7 | 2025-06-18 16:17:00 | [diff] [blame] | 188 | int p3; |
drh | 72532f5 | 2021-08-18 19:22:27 | [diff] [blame] | 189 | sqlite3VdbeAppendP4(v, pTab, P4_TABLE); |
drh | 058e995 | 2022-07-25 19:05:24 | [diff] [blame] | 190 | pPrev = sqlite3VdbeGetLastOp(v); |
drh | 71c770f | 2021-08-19 16:29:33 | [diff] [blame] | 191 | assert( pPrev!=0 ); |
| 192 | assert( pPrev->opcode==OP_MakeRecord || sqlite3VdbeDb(v)->mallocFailed ); |
drh | 72532f5 | 2021-08-18 19:22:27 | [diff] [blame] | 193 | pPrev->opcode = OP_TypeCheck; |
drh | bcf25e7 | 2025-06-18 16:17:00 | [diff] [blame] | 194 | p3 = pPrev->p3; |
| 195 | pPrev->p3 = 0; |
| 196 | sqlite3VdbeAddOp3(v, OP_MakeRecord, pPrev->p1, pPrev->p2, p3); |
drh | 72532f5 | 2021-08-18 19:22:27 | [diff] [blame] | 197 | }else{ |
| 198 | /* Insert an isolated OP_Typecheck */ |
| 199 | sqlite3VdbeAddOp2(v, OP_TypeCheck, iReg, pTab->nNVCol); |
| 200 | sqlite3VdbeAppendP4(v, pTab, P4_TABLE); |
| 201 | } |
| 202 | return; |
| 203 | } |
| 204 | zColAff = pTab->zColAff; |
drh | 57bf4a8 | 2014-02-17 14:59:22 | [diff] [blame] | 205 | if( zColAff==0 ){ |
drh | 5fdb9a3 | 2022-11-01 00:52:22 | [diff] [blame] | 206 | zColAff = sqlite3TableAffinityStr(0, pTab); |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 | [diff] [blame] | 207 | if( !zColAff ){ |
drh | 5fdb9a3 | 2022-11-01 00:52:22 | [diff] [blame] | 208 | sqlite3OomFault(sqlite3VdbeDb(v)); |
danielk1977 | a37cdde | 2004-05-16 11:15:36 | [diff] [blame] | 209 | return; |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 | [diff] [blame] | 210 | } |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 | [diff] [blame] | 211 | pTab->zColAff = zColAff; |
| 212 | } |
drh | 7301e77 | 2018-10-31 20:52:00 | [diff] [blame] | 213 | assert( zColAff!=0 ); |
| 214 | i = sqlite3Strlen30NN(zColAff); |
drh | 57bf4a8 | 2014-02-17 14:59:22 | [diff] [blame] | 215 | if( i ){ |
| 216 | if( iReg ){ |
| 217 | sqlite3VdbeAddOp4(v, OP_Affinity, iReg, i, 0, zColAff, i); |
| 218 | }else{ |
drh | 058e995 | 2022-07-25 19:05:24 | [diff] [blame] | 219 | assert( sqlite3VdbeGetLastOp(v)->opcode==OP_MakeRecord |
drh | 71c770f | 2021-08-19 16:29:33 | [diff] [blame] | 220 | || sqlite3VdbeDb(v)->mallocFailed ); |
drh | 57bf4a8 | 2014-02-17 14:59:22 | [diff] [blame] | 221 | sqlite3VdbeChangeP4(v, -1, zColAff, i); |
| 222 | } |
| 223 | } |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 | [diff] [blame] | 224 | } |
| 225 | |
danielk1977 | 4d88778 | 2005-02-08 08:42:27 | [diff] [blame] | 226 | /* |
drh | 48d1178 | 2007-11-23 15:02:19 | [diff] [blame] | 227 | ** Return non-zero if the table pTab in database iDb or any of its indices |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 228 | ** have been opened at any point in the VDBE program. This is used to see if |
| 229 | ** a statement of the form "INSERT INTO <iDb, pTab> SELECT ..." can |
| 230 | ** run without using a temporary table for the results of the SELECT. |
danielk1977 | 4d88778 | 2005-02-08 08:42:27 | [diff] [blame] | 231 | */ |
drh | 05a86c5 | 2014-02-16 01:55:49 | [diff] [blame] | 232 | static int readsTable(Parse *p, int iDb, Table *pTab){ |
danielk1977 | 595a523 | 2009-07-24 17:58:53 | [diff] [blame] | 233 | Vdbe *v = sqlite3GetVdbe(p); |
danielk1977 | 4d88778 | 2005-02-08 08:42:27 | [diff] [blame] | 234 | int i; |
drh | 48d1178 | 2007-11-23 15:02:19 | [diff] [blame] | 235 | int iEnd = sqlite3VdbeCurrentAddr(v); |
danielk1977 | 595a523 | 2009-07-24 17:58:53 | [diff] [blame] | 236 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
| 237 | VTable *pVTab = IsVirtual(pTab) ? sqlite3GetVTable(p->db, pTab) : 0; |
| 238 | #endif |
| 239 | |
drh | 05a86c5 | 2014-02-16 01:55:49 | [diff] [blame] | 240 | for(i=1; i<iEnd; i++){ |
drh | 48d1178 | 2007-11-23 15:02:19 | [diff] [blame] | 241 | VdbeOp *pOp = sqlite3VdbeGetOp(v, i); |
drh | ef0bea9 | 2007-12-14 16:11:09 | [diff] [blame] | 242 | assert( pOp!=0 ); |
danielk1977 | 207872a | 2008-01-03 07:54:23 | [diff] [blame] | 243 | if( pOp->opcode==OP_OpenRead && pOp->p3==iDb ){ |
| 244 | Index *pIndex; |
drh | 8deae5a | 2020-07-29 12:23:20 | [diff] [blame] | 245 | Pgno tnum = pOp->p2; |
danielk1977 | 207872a | 2008-01-03 07:54:23 | [diff] [blame] | 246 | if( tnum==pTab->tnum ){ |
| 247 | return 1; |
| 248 | } |
| 249 | for(pIndex=pTab->pIndex; pIndex; pIndex=pIndex->pNext){ |
| 250 | if( tnum==pIndex->tnum ){ |
drh | 48d1178 | 2007-11-23 15:02:19 | [diff] [blame] | 251 | return 1; |
| 252 | } |
drh | 48d1178 | 2007-11-23 15:02:19 | [diff] [blame] | 253 | } |
| 254 | } |
drh | 543165e | 2007-11-27 14:46:41 | [diff] [blame] | 255 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
danielk1977 | 595a523 | 2009-07-24 17:58:53 | [diff] [blame] | 256 | if( pOp->opcode==OP_VOpen && pOp->p4.pVtab==pVTab ){ |
danielk1977 | 2dca4ac | 2008-01-03 11:50:29 | [diff] [blame] | 257 | assert( pOp->p4.pVtab!=0 ); |
drh | 66a5167 | 2008-01-03 00:01:23 | [diff] [blame] | 258 | assert( pOp->p4type==P4_VTAB ); |
drh | 48d1178 | 2007-11-23 15:02:19 | [diff] [blame] | 259 | return 1; |
danielk1977 | 4d88778 | 2005-02-08 08:42:27 | [diff] [blame] | 260 | } |
drh | 543165e | 2007-11-27 14:46:41 | [diff] [blame] | 261 | #endif |
danielk1977 | 4d88778 | 2005-02-08 08:42:27 | [diff] [blame] | 262 | } |
| 263 | return 0; |
| 264 | } |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 | [diff] [blame] | 265 | |
drh | dfa1527 | 2019-11-06 22:19:07 | [diff] [blame] | 266 | /* This walker callback will compute the union of colFlags flags for all |
drh | 7dc76d8 | 2019-11-21 20:10:31 | [diff] [blame] | 267 | ** referenced columns in a CHECK constraint or generated column expression. |
drh | dfa1527 | 2019-11-06 22:19:07 | [diff] [blame] | 268 | */ |
| 269 | static int exprColumnFlagUnion(Walker *pWalker, Expr *pExpr){ |
drh | 7dc76d8 | 2019-11-21 20:10:31 | [diff] [blame] | 270 | if( pExpr->op==TK_COLUMN && pExpr->iColumn>=0 ){ |
| 271 | assert( pExpr->iColumn < pWalker->u.pTab->nCol ); |
drh | dfa1527 | 2019-11-06 22:19:07 | [diff] [blame] | 272 | pWalker->eCode |= pWalker->u.pTab->aCol[pExpr->iColumn].colFlags; |
| 273 | } |
| 274 | return WRC_Continue; |
| 275 | } |
| 276 | |
drh | c143114 | 2019-10-17 17:54:05 | [diff] [blame] | 277 | #ifndef SQLITE_OMIT_GENERATED_COLUMNS |
| 278 | /* |
| 279 | ** All regular columns for table pTab have been puts into registers |
| 280 | ** starting with iRegStore. The registers that correspond to STORED |
drh | dd6cc9b | 2019-10-19 18:47:27 | [diff] [blame] | 281 | ** or VIRTUAL columns have not yet been initialized. This routine goes |
| 282 | ** back and computes the values for those columns based on the previously |
| 283 | ** computed normal columns. |
drh | c143114 | 2019-10-17 17:54:05 | [diff] [blame] | 284 | */ |
drh | dd6cc9b | 2019-10-19 18:47:27 | [diff] [blame] | 285 | void sqlite3ComputeGeneratedColumns( |
drh | c143114 | 2019-10-17 17:54:05 | [diff] [blame] | 286 | Parse *pParse, /* Parsing context */ |
| 287 | int iRegStore, /* Register holding the first column */ |
| 288 | Table *pTab /* The table */ |
| 289 | ){ |
| 290 | int i; |
drh | dfa1527 | 2019-11-06 22:19:07 | [diff] [blame] | 291 | Walker w; |
| 292 | Column *pRedo; |
| 293 | int eProgress; |
drh | b5f6243 | 2019-12-10 02:48:41 | [diff] [blame] | 294 | VdbeOp *pOp; |
| 295 | |
| 296 | assert( pTab->tabFlags & TF_HasGenerated ); |
| 297 | testcase( pTab->tabFlags & TF_HasVirtual ); |
| 298 | testcase( pTab->tabFlags & TF_HasStored ); |
| 299 | |
| 300 | /* Before computing generated columns, first go through and make sure |
| 301 | ** that appropriate affinity has been applied to the regular columns |
| 302 | */ |
| 303 | sqlite3TableAffinity(pParse->pVdbe, pTab, iRegStore); |
drh | 926aac5 | 2021-11-03 14:02:48 | [diff] [blame] | 304 | if( (pTab->tabFlags & TF_HasStored)!=0 ){ |
drh | 058e995 | 2022-07-25 19:05:24 | [diff] [blame] | 305 | pOp = sqlite3VdbeGetLastOp(pParse->pVdbe); |
drh | 926aac5 | 2021-11-03 14:02:48 | [diff] [blame] | 306 | if( pOp->opcode==OP_Affinity ){ |
| 307 | /* Change the OP_Affinity argument to '@' (NONE) for all stored |
| 308 | ** columns. '@' is the no-op affinity and those columns have not |
| 309 | ** yet been computed. */ |
| 310 | int ii, jj; |
| 311 | char *zP4 = pOp->p4.z; |
| 312 | assert( zP4!=0 ); |
| 313 | assert( pOp->p4type==P4_DYNAMIC ); |
drh | 926aac5 | 2021-11-03 14:02:48 | [diff] [blame] | 314 | for(ii=jj=0; zP4[jj]; ii++){ |
| 315 | if( pTab->aCol[ii].colFlags & COLFLAG_VIRTUAL ){ |
| 316 | continue; |
| 317 | } |
| 318 | if( pTab->aCol[ii].colFlags & COLFLAG_STORED ){ |
| 319 | zP4[jj] = SQLITE_AFF_NONE; |
| 320 | } |
| 321 | jj++; |
drh | b5f6243 | 2019-12-10 02:48:41 | [diff] [blame] | 322 | } |
drh | 926aac5 | 2021-11-03 14:02:48 | [diff] [blame] | 323 | }else if( pOp->opcode==OP_TypeCheck ){ |
| 324 | /* If an OP_TypeCheck was generated because the table is STRICT, |
| 325 | ** then set the P3 operand to indicate that generated columns should |
| 326 | ** not be checked */ |
drh | 926aac5 | 2021-11-03 14:02:48 | [diff] [blame] | 327 | pOp->p3 = 1; |
drh | b5f6243 | 2019-12-10 02:48:41 | [diff] [blame] | 328 | } |
| 329 | } |
drh | dfa1527 | 2019-11-06 22:19:07 | [diff] [blame] | 330 | |
drh | dd6cc9b | 2019-10-19 18:47:27 | [diff] [blame] | 331 | /* Because there can be multiple generated columns that refer to one another, |
| 332 | ** this is a two-pass algorithm. On the first pass, mark all generated |
| 333 | ** columns as "not available". |
drh | 9942ef0 | 2019-10-18 02:19:18 | [diff] [blame] | 334 | */ |
| 335 | for(i=0; i<pTab->nCol; i++){ |
drh | dd6cc9b | 2019-10-19 18:47:27 | [diff] [blame] | 336 | if( pTab->aCol[i].colFlags & COLFLAG_GENERATED ){ |
drh | ab0992f | 2019-10-23 03:53:10 | [diff] [blame] | 337 | testcase( pTab->aCol[i].colFlags & COLFLAG_VIRTUAL ); |
| 338 | testcase( pTab->aCol[i].colFlags & COLFLAG_STORED ); |
drh | 9942ef0 | 2019-10-18 02:19:18 | [diff] [blame] | 339 | pTab->aCol[i].colFlags |= COLFLAG_NOTAVAIL; |
| 340 | } |
| 341 | } |
drh | dfa1527 | 2019-11-06 22:19:07 | [diff] [blame] | 342 | |
| 343 | w.u.pTab = pTab; |
| 344 | w.xExprCallback = exprColumnFlagUnion; |
| 345 | w.xSelectCallback = 0; |
| 346 | w.xSelectCallback2 = 0; |
| 347 | |
drh | 9942ef0 | 2019-10-18 02:19:18 | [diff] [blame] | 348 | /* On the second pass, compute the value of each NOT-AVAILABLE column. |
| 349 | ** Companion code in the TK_COLUMN case of sqlite3ExprCodeTarget() will |
| 350 | ** compute dependencies and mark remove the COLSPAN_NOTAVAIL mark, as |
| 351 | ** they are needed. |
| 352 | */ |
drh | c143114 | 2019-10-17 17:54:05 | [diff] [blame] | 353 | pParse->iSelfTab = -iRegStore; |
drh | dfa1527 | 2019-11-06 22:19:07 | [diff] [blame] | 354 | do{ |
| 355 | eProgress = 0; |
| 356 | pRedo = 0; |
| 357 | for(i=0; i<pTab->nCol; i++){ |
| 358 | Column *pCol = pTab->aCol + i; |
| 359 | if( (pCol->colFlags & COLFLAG_NOTAVAIL)!=0 ){ |
| 360 | int x; |
| 361 | pCol->colFlags |= COLFLAG_BUSY; |
| 362 | w.eCode = 0; |
drh | 79cf2b7 | 2021-07-31 20:30:41 | [diff] [blame] | 363 | sqlite3WalkExpr(&w, sqlite3ColumnExpr(pTab, pCol)); |
drh | dfa1527 | 2019-11-06 22:19:07 | [diff] [blame] | 364 | pCol->colFlags &= ~COLFLAG_BUSY; |
| 365 | if( w.eCode & COLFLAG_NOTAVAIL ){ |
| 366 | pRedo = pCol; |
| 367 | continue; |
| 368 | } |
| 369 | eProgress = 1; |
| 370 | assert( pCol->colFlags & COLFLAG_GENERATED ); |
| 371 | x = sqlite3TableColumnToStorage(pTab, i) + iRegStore; |
drh | 79cf2b7 | 2021-07-31 20:30:41 | [diff] [blame] | 372 | sqlite3ExprCodeGeneratedColumn(pParse, pTab, pCol, x); |
drh | dfa1527 | 2019-11-06 22:19:07 | [diff] [blame] | 373 | pCol->colFlags &= ~COLFLAG_NOTAVAIL; |
drh | dd6cc9b | 2019-10-19 18:47:27 | [diff] [blame] | 374 | } |
drh | c143114 | 2019-10-17 17:54:05 | [diff] [blame] | 375 | } |
drh | dfa1527 | 2019-11-06 22:19:07 | [diff] [blame] | 376 | }while( pRedo && eProgress ); |
| 377 | if( pRedo ){ |
drh | cf9d36d | 2021-08-02 18:03:43 | [diff] [blame] | 378 | sqlite3ErrorMsg(pParse, "generated column loop on \"%s\"", pRedo->zCnName); |
drh | c143114 | 2019-10-17 17:54:05 | [diff] [blame] | 379 | } |
| 380 | pParse->iSelfTab = 0; |
| 381 | } |
| 382 | #endif /* SQLITE_OMIT_GENERATED_COLUMNS */ |
| 383 | |
| 384 | |
drh | 9d9cf22 | 2007-02-13 15:01:11 | [diff] [blame] | 385 | #ifndef SQLITE_OMIT_AUTOINCREMENT |
| 386 | /* |
drh | 0b9f50d | 2009-06-23 20:28:53 | [diff] [blame] | 387 | ** Locate or create an AutoincInfo structure associated with table pTab |
| 388 | ** which is in database iDb. Return the register number for the register |
drh | 9ef5e77 | 2016-08-19 14:20:56 | [diff] [blame] | 389 | ** that holds the maximum rowid. Return zero if pTab is not an AUTOINCREMENT |
| 390 | ** table. (Also return zero when doing a VACUUM since we do not want to |
| 391 | ** update the AUTOINCREMENT counters during a VACUUM.) |
drh | 9d9cf22 | 2007-02-13 15:01:11 | [diff] [blame] | 392 | ** |
drh | 0b9f50d | 2009-06-23 20:28:53 | [diff] [blame] | 393 | ** There is at most one AutoincInfo structure per table even if the |
| 394 | ** same table is autoincremented multiple times due to inserts within |
| 395 | ** triggers. A new AutoincInfo structure is created if this is the |
| 396 | ** first use of table pTab. On 2nd and subsequent uses, the original |
| 397 | ** AutoincInfo structure is used. |
drh | 9d9cf22 | 2007-02-13 15:01:11 | [diff] [blame] | 398 | ** |
drh | c8abbc1 | 2018-03-16 18:46:30 | [diff] [blame] | 399 | ** Four consecutive registers are allocated: |
drh | 0b9f50d | 2009-06-23 20:28:53 | [diff] [blame] | 400 | ** |
drh | c8abbc1 | 2018-03-16 18:46:30 | [diff] [blame] | 401 | ** (1) The name of the pTab table. |
| 402 | ** (2) The maximum ROWID of pTab. |
| 403 | ** (3) The rowid in sqlite_sequence of pTab |
| 404 | ** (4) The original value of the max ROWID in pTab, or NULL if none |
drh | 0b9f50d | 2009-06-23 20:28:53 | [diff] [blame] | 405 | ** |
| 406 | ** The 2nd register is the one that is returned. That is all the |
| 407 | ** insert routine needs to know about. |
drh | 9d9cf22 | 2007-02-13 15:01:11 | [diff] [blame] | 408 | */ |
| 409 | static int autoIncBegin( |
| 410 | Parse *pParse, /* Parsing context */ |
| 411 | int iDb, /* Index of the database holding pTab */ |
| 412 | Table *pTab /* The table we are writing to */ |
| 413 | ){ |
drh | 6a288a3 | 2008-01-07 19:20:24 | [diff] [blame] | 414 | int memId = 0; /* Register holding maximum rowid */ |
drh | 186ebd4 | 2018-05-23 16:50:21 | [diff] [blame] | 415 | assert( pParse->db->aDb[iDb].pSchema!=0 ); |
drh | 9ef5e77 | 2016-08-19 14:20:56 | [diff] [blame] | 416 | if( (pTab->tabFlags & TF_Autoincrement)!=0 |
drh | 8257aa8 | 2017-07-26 19:59:13 | [diff] [blame] | 417 | && (pParse->db->mDbFlags & DBFLAG_Vacuum)==0 |
drh | 9ef5e77 | 2016-08-19 14:20:56 | [diff] [blame] | 418 | ){ |
dan | 65a7cd1 | 2009-09-01 12:16:01 | [diff] [blame] | 419 | Parse *pToplevel = sqlite3ParseToplevel(pParse); |
drh | 0b9f50d | 2009-06-23 20:28:53 | [diff] [blame] | 420 | AutoincInfo *pInfo; |
drh | 186ebd4 | 2018-05-23 16:50:21 | [diff] [blame] | 421 | Table *pSeqTab = pParse->db->aDb[iDb].pSchema->pSeqTab; |
| 422 | |
| 423 | /* Verify that the sqlite_sequence table exists and is an ordinary |
| 424 | ** rowid table with exactly two columns. |
| 425 | ** Ticket d8dc2b3a58cd5dc2918a1d4acb 2018-05-23 */ |
| 426 | if( pSeqTab==0 |
| 427 | || !HasRowid(pSeqTab) |
drh | 0003d87 | 2021-04-11 00:11:56 | [diff] [blame] | 428 | || NEVER(IsVirtual(pSeqTab)) |
drh | 186ebd4 | 2018-05-23 16:50:21 | [diff] [blame] | 429 | || pSeqTab->nCol!=2 |
| 430 | ){ |
| 431 | pParse->nErr++; |
| 432 | pParse->rc = SQLITE_CORRUPT_SEQUENCE; |
| 433 | return 0; |
| 434 | } |
drh | 0b9f50d | 2009-06-23 20:28:53 | [diff] [blame] | 435 | |
dan | 65a7cd1 | 2009-09-01 12:16:01 | [diff] [blame] | 436 | pInfo = pToplevel->pAinc; |
drh | 0b9f50d | 2009-06-23 20:28:53 | [diff] [blame] | 437 | while( pInfo && pInfo->pTab!=pTab ){ pInfo = pInfo->pNext; } |
| 438 | if( pInfo==0 ){ |
drh | 575fad6 | 2016-02-05 13:38:36 | [diff] [blame] | 439 | pInfo = sqlite3DbMallocRawNN(pParse->db, sizeof(*pInfo)); |
drh | 21d4f5b | 2021-01-12 15:30:01 | [diff] [blame] | 440 | sqlite3ParserAddCleanup(pToplevel, sqlite3DbFree, pInfo); |
| 441 | testcase( pParse->earlyCleanup ); |
| 442 | if( pParse->db->mallocFailed ) return 0; |
dan | 65a7cd1 | 2009-09-01 12:16:01 | [diff] [blame] | 443 | pInfo->pNext = pToplevel->pAinc; |
| 444 | pToplevel->pAinc = pInfo; |
drh | 0b9f50d | 2009-06-23 20:28:53 | [diff] [blame] | 445 | pInfo->pTab = pTab; |
| 446 | pInfo->iDb = iDb; |
dan | 65a7cd1 | 2009-09-01 12:16:01 | [diff] [blame] | 447 | pToplevel->nMem++; /* Register to hold name of table */ |
| 448 | pInfo->regCtr = ++pToplevel->nMem; /* Max rowid register */ |
drh | c8abbc1 | 2018-03-16 18:46:30 | [diff] [blame] | 449 | pToplevel->nMem +=2; /* Rowid in sqlite_sequence + orig max val */ |
drh | 0b9f50d | 2009-06-23 20:28:53 | [diff] [blame] | 450 | } |
| 451 | memId = pInfo->regCtr; |
drh | 9d9cf22 | 2007-02-13 15:01:11 | [diff] [blame] | 452 | } |
| 453 | return memId; |
| 454 | } |
| 455 | |
| 456 | /* |
drh | 0b9f50d | 2009-06-23 20:28:53 | [diff] [blame] | 457 | ** This routine generates code that will initialize all of the |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 458 | ** register used by the autoincrement tracker. |
drh | 0b9f50d | 2009-06-23 20:28:53 | [diff] [blame] | 459 | */ |
| 460 | void sqlite3AutoincrementBegin(Parse *pParse){ |
| 461 | AutoincInfo *p; /* Information about an AUTOINCREMENT */ |
| 462 | sqlite3 *db = pParse->db; /* The database connection */ |
| 463 | Db *pDb; /* Database only autoinc table */ |
| 464 | int memId; /* Register holding max rowid */ |
drh | 0b9f50d | 2009-06-23 20:28:53 | [diff] [blame] | 465 | Vdbe *v = pParse->pVdbe; /* VDBE under construction */ |
| 466 | |
drh | 345ba7d | 2009-09-08 13:40:16 | [diff] [blame] | 467 | /* This routine is never called during trigger-generation. It is |
| 468 | ** only called from the top-level */ |
| 469 | assert( pParse->pTriggerTab==0 ); |
drh | c149f18 | 2015-09-29 13:25:15 | [diff] [blame] | 470 | assert( sqlite3IsToplevel(pParse) ); |
dan | 76d462e | 2009-08-30 11:42:51 | [diff] [blame] | 471 | |
drh | 0b9f50d | 2009-06-23 20:28:53 | [diff] [blame] | 472 | assert( v ); /* We failed long ago if this is not so */ |
| 473 | for(p = pParse->pAinc; p; p = p->pNext){ |
drh | 1b32554 | 2016-02-03 01:55:44 | [diff] [blame] | 474 | static const int iLn = VDBE_OFFSET_LINENO(2); |
| 475 | static const VdbeOpList autoInc[] = { |
| 476 | /* 0 */ {OP_Null, 0, 0, 0}, |
drh | c8abbc1 | 2018-03-16 18:46:30 | [diff] [blame] | 477 | /* 1 */ {OP_Rewind, 0, 10, 0}, |
drh | 1b32554 | 2016-02-03 01:55:44 | [diff] [blame] | 478 | /* 2 */ {OP_Column, 0, 0, 0}, |
drh | c8abbc1 | 2018-03-16 18:46:30 | [diff] [blame] | 479 | /* 3 */ {OP_Ne, 0, 9, 0}, |
drh | 1b32554 | 2016-02-03 01:55:44 | [diff] [blame] | 480 | /* 4 */ {OP_Rowid, 0, 0, 0}, |
| 481 | /* 5 */ {OP_Column, 0, 1, 0}, |
drh | c8abbc1 | 2018-03-16 18:46:30 | [diff] [blame] | 482 | /* 6 */ {OP_AddImm, 0, 0, 0}, |
| 483 | /* 7 */ {OP_Copy, 0, 0, 0}, |
| 484 | /* 8 */ {OP_Goto, 0, 11, 0}, |
| 485 | /* 9 */ {OP_Next, 0, 2, 0}, |
| 486 | /* 10 */ {OP_Integer, 0, 0, 0}, |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 487 | /* 11 */ {OP_Close, 0, 0, 0} |
drh | 1b32554 | 2016-02-03 01:55:44 | [diff] [blame] | 488 | }; |
| 489 | VdbeOp *aOp; |
drh | 0b9f50d | 2009-06-23 20:28:53 | [diff] [blame] | 490 | pDb = &db->aDb[p->iDb]; |
| 491 | memId = p->regCtr; |
drh | 2120608 | 2011-04-04 18:22:02 | [diff] [blame] | 492 | assert( sqlite3SchemaMutexHeld(db, 0, pDb->pSchema) ); |
drh | 0b9f50d | 2009-06-23 20:28:53 | [diff] [blame] | 493 | sqlite3OpenTable(pParse, 0, p->iDb, pDb->pSchema->pSeqTab, OP_OpenRead); |
drh | 076e85f | 2015-09-03 13:46:12 | [diff] [blame] | 494 | sqlite3VdbeLoadString(v, memId-1, p->pTab->zName); |
drh | 1b32554 | 2016-02-03 01:55:44 | [diff] [blame] | 495 | aOp = sqlite3VdbeAddOpList(v, ArraySize(autoInc), autoInc, iLn); |
| 496 | if( aOp==0 ) break; |
| 497 | aOp[0].p2 = memId; |
drh | c8abbc1 | 2018-03-16 18:46:30 | [diff] [blame] | 498 | aOp[0].p3 = memId+2; |
drh | 1b32554 | 2016-02-03 01:55:44 | [diff] [blame] | 499 | aOp[2].p3 = memId; |
| 500 | aOp[3].p1 = memId-1; |
| 501 | aOp[3].p3 = memId; |
| 502 | aOp[3].p5 = SQLITE_JUMPIFNULL; |
| 503 | aOp[4].p2 = memId+1; |
| 504 | aOp[5].p3 = memId; |
drh | c8abbc1 | 2018-03-16 18:46:30 | [diff] [blame] | 505 | aOp[6].p1 = memId; |
| 506 | aOp[7].p2 = memId+2; |
| 507 | aOp[7].p1 = memId; |
| 508 | aOp[10].p2 = memId; |
drh | 04ab586 | 2018-12-01 21:13:41 | [diff] [blame] | 509 | if( pParse->nTab==0 ) pParse->nTab = 1; |
drh | 0b9f50d | 2009-06-23 20:28:53 | [diff] [blame] | 510 | } |
| 511 | } |
| 512 | |
| 513 | /* |
drh | 9d9cf22 | 2007-02-13 15:01:11 | [diff] [blame] | 514 | ** Update the maximum rowid for an autoincrement calculation. |
| 515 | ** |
drh | 1b32554 | 2016-02-03 01:55:44 | [diff] [blame] | 516 | ** This routine should be called when the regRowid register holds a |
drh | 9d9cf22 | 2007-02-13 15:01:11 | [diff] [blame] | 517 | ** new rowid that is about to be inserted. If that new rowid is |
| 518 | ** larger than the maximum rowid in the memId memory cell, then the |
drh | 1b32554 | 2016-02-03 01:55:44 | [diff] [blame] | 519 | ** memory cell is updated. |
drh | 9d9cf22 | 2007-02-13 15:01:11 | [diff] [blame] | 520 | */ |
drh | 6a288a3 | 2008-01-07 19:20:24 | [diff] [blame] | 521 | static void autoIncStep(Parse *pParse, int memId, int regRowid){ |
drh | 9d9cf22 | 2007-02-13 15:01:11 | [diff] [blame] | 522 | if( memId>0 ){ |
drh | 6a288a3 | 2008-01-07 19:20:24 | [diff] [blame] | 523 | sqlite3VdbeAddOp2(pParse->pVdbe, OP_MemMax, memId, regRowid); |
drh | 9d9cf22 | 2007-02-13 15:01:11 | [diff] [blame] | 524 | } |
| 525 | } |
| 526 | |
| 527 | /* |
drh | 0b9f50d | 2009-06-23 20:28:53 | [diff] [blame] | 528 | ** This routine generates the code needed to write autoincrement |
| 529 | ** maximum rowid values back into the sqlite_sequence register. |
| 530 | ** Every statement that might do an INSERT into an autoincrement |
| 531 | ** table (either directly or through triggers) needs to call this |
| 532 | ** routine just before the "exit" code. |
drh | 9d9cf22 | 2007-02-13 15:01:11 | [diff] [blame] | 533 | */ |
drh | 1b32554 | 2016-02-03 01:55:44 | [diff] [blame] | 534 | static SQLITE_NOINLINE void autoIncrementEnd(Parse *pParse){ |
drh | 0b9f50d | 2009-06-23 20:28:53 | [diff] [blame] | 535 | AutoincInfo *p; |
| 536 | Vdbe *v = pParse->pVdbe; |
| 537 | sqlite3 *db = pParse->db; |
drh | 6a288a3 | 2008-01-07 19:20:24 | [diff] [blame] | 538 | |
drh | 0b9f50d | 2009-06-23 20:28:53 | [diff] [blame] | 539 | assert( v ); |
| 540 | for(p = pParse->pAinc; p; p = p->pNext){ |
drh | 1b32554 | 2016-02-03 01:55:44 | [diff] [blame] | 541 | static const int iLn = VDBE_OFFSET_LINENO(2); |
| 542 | static const VdbeOpList autoIncEnd[] = { |
| 543 | /* 0 */ {OP_NotNull, 0, 2, 0}, |
| 544 | /* 1 */ {OP_NewRowid, 0, 0, 0}, |
| 545 | /* 2 */ {OP_MakeRecord, 0, 2, 0}, |
| 546 | /* 3 */ {OP_Insert, 0, 0, 0}, |
| 547 | /* 4 */ {OP_Close, 0, 0, 0} |
| 548 | }; |
| 549 | VdbeOp *aOp; |
drh | 0b9f50d | 2009-06-23 20:28:53 | [diff] [blame] | 550 | Db *pDb = &db->aDb[p->iDb]; |
drh | 0b9f50d | 2009-06-23 20:28:53 | [diff] [blame] | 551 | int iRec; |
| 552 | int memId = p->regCtr; |
| 553 | |
| 554 | iRec = sqlite3GetTempReg(pParse); |
drh | 2120608 | 2011-04-04 18:22:02 | [diff] [blame] | 555 | assert( sqlite3SchemaMutexHeld(db, 0, pDb->pSchema) ); |
drh | c8abbc1 | 2018-03-16 18:46:30 | [diff] [blame] | 556 | sqlite3VdbeAddOp3(v, OP_Le, memId+2, sqlite3VdbeCurrentAddr(v)+7, memId); |
| 557 | VdbeCoverage(v); |
drh | 0b9f50d | 2009-06-23 20:28:53 | [diff] [blame] | 558 | sqlite3OpenTable(pParse, 0, p->iDb, pDb->pSchema->pSeqTab, OP_OpenWrite); |
drh | 1b32554 | 2016-02-03 01:55:44 | [diff] [blame] | 559 | aOp = sqlite3VdbeAddOpList(v, ArraySize(autoIncEnd), autoIncEnd, iLn); |
| 560 | if( aOp==0 ) break; |
| 561 | aOp[0].p1 = memId+1; |
| 562 | aOp[1].p2 = memId+1; |
| 563 | aOp[2].p1 = memId-1; |
| 564 | aOp[2].p3 = iRec; |
| 565 | aOp[3].p2 = iRec; |
| 566 | aOp[3].p3 = memId+1; |
| 567 | aOp[3].p5 = OPFLAG_APPEND; |
drh | 0b9f50d | 2009-06-23 20:28:53 | [diff] [blame] | 568 | sqlite3ReleaseTempReg(pParse, iRec); |
drh | 9d9cf22 | 2007-02-13 15:01:11 | [diff] [blame] | 569 | } |
| 570 | } |
drh | 1b32554 | 2016-02-03 01:55:44 | [diff] [blame] | 571 | void sqlite3AutoincrementEnd(Parse *pParse){ |
| 572 | if( pParse->pAinc ) autoIncrementEnd(pParse); |
| 573 | } |
drh | 9d9cf22 | 2007-02-13 15:01:11 | [diff] [blame] | 574 | #else |
| 575 | /* |
| 576 | ** If SQLITE_OMIT_AUTOINCREMENT is defined, then the three routines |
| 577 | ** above are all no-ops |
| 578 | */ |
| 579 | # define autoIncBegin(A,B,C) (0) |
danielk1977 | 287fb61 | 2008-01-04 19:10:28 | [diff] [blame] | 580 | # define autoIncStep(A,B,C) |
drh | 9d9cf22 | 2007-02-13 15:01:11 | [diff] [blame] | 581 | #endif /* SQLITE_OMIT_AUTOINCREMENT */ |
| 582 | |
dan | 56be6f6 | 2024-03-13 20:04:11 | [diff] [blame] | 583 | /* |
| 584 | ** If argument pVal is a Select object returned by an sqlite3MultiValues() |
| 585 | ** that was able to use the co-routine optimization, finish coding the |
| 586 | ** co-routine. |
| 587 | */ |
dan | 815e055 | 2024-03-11 17:27:19 | [diff] [blame] | 588 | void sqlite3MultiValuesEnd(Parse *pParse, Select *pVal){ |
drh | e4a1b4d | 2024-03-17 00:13:12 | [diff] [blame] | 589 | if( ALWAYS(pVal) && pVal->pSrc->nSrc>0 ){ |
dan | 815e055 | 2024-03-11 17:27:19 | [diff] [blame] | 590 | SrcItem *pItem = &pVal->pSrc->a[0]; |
drh | ff4ad29 | 2024-08-20 16:50:21 | [diff] [blame] | 591 | assert( (pItem->fg.isSubquery && pItem->u4.pSubq!=0) || pParse->nErr ); |
| 592 | if( pItem->fg.isSubquery ){ |
| 593 | sqlite3VdbeEndCoroutine(pParse->pVdbe, pItem->u4.pSubq->regReturn); |
| 594 | sqlite3VdbeJumpHere(pParse->pVdbe, pItem->u4.pSubq->addrFillSub - 1); |
| 595 | } |
dan | 815e055 | 2024-03-11 17:27:19 | [diff] [blame] | 596 | } |
| 597 | } |
| 598 | |
dan | 56be6f6 | 2024-03-13 20:04:11 | [diff] [blame] | 599 | /* |
| 600 | ** Return true if all expressions in the expression-list passed as the |
| 601 | ** only argument are constant. |
| 602 | */ |
drh | f696591 | 2024-03-16 13:18:48 | [diff] [blame] | 603 | static int exprListIsConstant(Parse *pParse, ExprList *pRow){ |
dan | 815e055 | 2024-03-11 17:27:19 | [diff] [blame] | 604 | int ii; |
| 605 | for(ii=0; ii<pRow->nExpr; ii++){ |
drh | f696591 | 2024-03-16 13:18:48 | [diff] [blame] | 606 | if( 0==sqlite3ExprIsConstant(pParse, pRow->a[ii].pExpr) ) return 0; |
dan | 815e055 | 2024-03-11 17:27:19 | [diff] [blame] | 607 | } |
| 608 | return 1; |
| 609 | } |
| 610 | |
dan | 56be6f6 | 2024-03-13 20:04:11 | [diff] [blame] | 611 | /* |
| 612 | ** Return true if all expressions in the expression-list passed as the |
| 613 | ** only argument are both constant and have no affinity. |
| 614 | */ |
drh | f696591 | 2024-03-16 13:18:48 | [diff] [blame] | 615 | static int exprListIsNoAffinity(Parse *pParse, ExprList *pRow){ |
dan | 609aba0 | 2024-03-13 15:34:44 | [diff] [blame] | 616 | int ii; |
drh | f696591 | 2024-03-16 13:18:48 | [diff] [blame] | 617 | if( exprListIsConstant(pParse,pRow)==0 ) return 0; |
dan | 609aba0 | 2024-03-13 15:34:44 | [diff] [blame] | 618 | for(ii=0; ii<pRow->nExpr; ii++){ |
drh | a509a90 | 2024-03-25 20:35:14 | [diff] [blame] | 619 | Expr *pExpr = pRow->a[ii].pExpr; |
| 620 | assert( pExpr->op!=TK_RAISE ); |
| 621 | assert( pExpr->affExpr==0 ); |
| 622 | if( 0!=sqlite3ExprAffinity(pExpr) ) return 0; |
dan | 609aba0 | 2024-03-13 15:34:44 | [diff] [blame] | 623 | } |
| 624 | return 1; |
| 625 | |
| 626 | } |
| 627 | |
dan | aa2e244 | 2024-03-13 18:41:05 | [diff] [blame] | 628 | /* |
| 629 | ** This function is called by the parser for the second and subsequent |
dan | 56be6f6 | 2024-03-13 20:04:11 | [diff] [blame] | 630 | ** rows of a multi-row VALUES clause. Argument pLeft is the part of |
| 631 | ** the VALUES clause already parsed, argument pRow is the vector of values |
| 632 | ** for the new row. The Select object returned represents the complete |
| 633 | ** VALUES clause, including the new row. |
| 634 | ** |
| 635 | ** There are two ways in which this may be achieved - by incremental |
| 636 | ** coding of a co-routine (the "co-routine" method) or by returning a |
| 637 | ** Select object equivalent to the following (the "UNION ALL" method): |
| 638 | ** |
| 639 | ** "pLeft UNION ALL SELECT pRow" |
| 640 | ** |
| 641 | ** If the VALUES clause contains a lot of rows, this compound Select |
| 642 | ** object may consume a lot of memory. |
| 643 | ** |
| 644 | ** When the co-routine method is used, each row that will be returned |
| 645 | ** by the VALUES clause is coded into part of a co-routine as it is |
| 646 | ** passed to this function. The returned Select object is equivalent to: |
| 647 | ** |
| 648 | ** SELECT * FROM ( |
| 649 | ** Select object to read co-routine |
| 650 | ** ) |
| 651 | ** |
| 652 | ** The co-routine method is used in most cases. Exceptions are: |
| 653 | ** |
| 654 | ** a) If the current statement has a WITH clause. This is to avoid |
| 655 | ** statements like: |
| 656 | ** |
| 657 | ** WITH cte AS ( VALUES('x'), ('y') ... ) |
| 658 | ** SELECT * FROM cte AS a, cte AS b; |
| 659 | ** |
| 660 | ** This will not work, as the co-routine uses a hard-coded register |
| 661 | ** for its OP_Yield instructions, and so it is not possible for two |
| 662 | ** cursors to iterate through it concurrently. |
| 663 | ** |
| 664 | ** b) The schema is currently being parsed (i.e. the VALUES clause is part |
| 665 | ** of a schema item like a VIEW or TRIGGER). In this case there is no VM |
| 666 | ** being generated when parsing is taking place, and so generating |
| 667 | ** a co-routine is not possible. |
| 668 | ** |
| 669 | ** c) There are non-constant expressions in the VALUES clause (e.g. |
| 670 | ** the VALUES clause is part of a correlated sub-query). |
| 671 | ** |
| 672 | ** d) One or more of the values in the first row of the VALUES clause |
| 673 | ** has an affinity (i.e. is a CAST expression). This causes problems |
| 674 | ** because the complex rules SQLite uses (see function |
| 675 | ** sqlite3SubqueryColumnTypes() in select.c) to determine the effective |
| 676 | ** affinity of such a column for all rows require access to all values in |
| 677 | ** the column simultaneously. |
dan | aa2e244 | 2024-03-13 18:41:05 | [diff] [blame] | 678 | */ |
dan | 815e055 | 2024-03-11 17:27:19 | [diff] [blame] | 679 | Select *sqlite3MultiValues(Parse *pParse, Select *pLeft, ExprList *pRow){ |
dan | 815e055 | 2024-03-11 17:27:19 | [diff] [blame] | 680 | |
drh | c195e23 | 2024-03-18 16:30:00 | [diff] [blame] | 681 | if( pParse->bHasWith /* condition (a) above */ |
drh | f696591 | 2024-03-16 13:18:48 | [diff] [blame] | 682 | || pParse->db->init.busy /* condition (b) above */ |
| 683 | || exprListIsConstant(pParse,pRow)==0 /* condition (c) above */ |
| 684 | || (pLeft->pSrc->nSrc==0 && |
| 685 | exprListIsNoAffinity(pParse,pLeft->pEList)==0) /* condition (d) above */ |
dan | 26a3ef7 | 2024-03-14 19:31:06 | [diff] [blame] | 686 | || IN_SPECIAL_PARSE |
dan | 815e055 | 2024-03-11 17:27:19 | [diff] [blame] | 687 | ){ |
dan | 56be6f6 | 2024-03-13 20:04:11 | [diff] [blame] | 688 | /* The co-routine method cannot be used. Fall back to UNION ALL. */ |
dan | aa2e244 | 2024-03-13 18:41:05 | [diff] [blame] | 689 | Select *pSelect = 0; |
dan | 815e055 | 2024-03-11 17:27:19 | [diff] [blame] | 690 | int f = SF_Values | SF_MultiValue; |
dan | e116fa1 | 2024-03-13 15:44:31 | [diff] [blame] | 691 | if( pLeft->pSrc->nSrc ){ |
dan | 815e055 | 2024-03-11 17:27:19 | [diff] [blame] | 692 | sqlite3MultiValuesEnd(pParse, pLeft); |
| 693 | f = SF_Values; |
dan | e116fa1 | 2024-03-13 15:44:31 | [diff] [blame] | 694 | }else if( pLeft->pPrior ){ |
dan | aa2e244 | 2024-03-13 18:41:05 | [diff] [blame] | 695 | /* In this case set the SF_MultiValue flag only if it was set on pLeft */ |
dan | e116fa1 | 2024-03-13 15:44:31 | [diff] [blame] | 696 | f = (f & pLeft->selFlags); |
dan | 815e055 | 2024-03-11 17:27:19 | [diff] [blame] | 697 | } |
dan | aa2e244 | 2024-03-13 18:41:05 | [diff] [blame] | 698 | pSelect = sqlite3SelectNew(pParse, pRow, 0, 0, 0, 0, 0, f, 0); |
drh | ce25007 | 2025-02-21 17:03:22 | [diff] [blame] | 699 | pLeft->selFlags &= ~(u32)SF_MultiValue; |
dan | 815e055 | 2024-03-11 17:27:19 | [diff] [blame] | 700 | if( pSelect ){ |
| 701 | pSelect->op = TK_ALL; |
| 702 | pSelect->pPrior = pLeft; |
| 703 | pLeft = pSelect; |
| 704 | } |
| 705 | }else{ |
dan | 56be6f6 | 2024-03-13 20:04:11 | [diff] [blame] | 706 | SrcItem *p = 0; /* SrcItem that reads from co-routine */ |
dan | 815e055 | 2024-03-11 17:27:19 | [diff] [blame] | 707 | |
| 708 | if( pLeft->pSrc->nSrc==0 ){ |
dan | 56be6f6 | 2024-03-13 20:04:11 | [diff] [blame] | 709 | /* Co-routine has not yet been started and the special Select object |
| 710 | ** that accesses the co-routine has not yet been created. This block |
| 711 | ** does both those things. */ |
dan | 815e055 | 2024-03-11 17:27:19 | [diff] [blame] | 712 | Vdbe *v = sqlite3GetVdbe(pParse); |
dan | 56be6f6 | 2024-03-13 20:04:11 | [diff] [blame] | 713 | Select *pRet = sqlite3SelectNew(pParse, 0, 0, 0, 0, 0, 0, 0, 0); |
dan | 75924d3 | 2024-03-18 11:12:22 | [diff] [blame] | 714 | |
| 715 | /* Ensure the database schema has been read. This is to ensure we have |
| 716 | ** the correct text encoding. */ |
| 717 | if( (pParse->db->mDbFlags & DBFLAG_SchemaKnownOk)==0 ){ |
| 718 | sqlite3ReadSchema(pParse); |
| 719 | } |
| 720 | |
dan | 35057fb | 2024-03-13 17:33:45 | [diff] [blame] | 721 | if( pRet ){ |
dan | aa2e244 | 2024-03-13 18:41:05 | [diff] [blame] | 722 | SelectDest dest; |
drh | 1521ca4 | 2024-08-19 22:48:30 | [diff] [blame] | 723 | Subquery *pSubq; |
dan | 35057fb | 2024-03-13 17:33:45 | [diff] [blame] | 724 | pRet->pSrc->nSrc = 1; |
drh | c195e23 | 2024-03-18 16:30:00 | [diff] [blame] | 725 | pRet->pPrior = pLeft->pPrior; |
| 726 | pRet->op = pLeft->op; |
drh | 1193e46 | 2024-08-08 14:45:50 | [diff] [blame] | 727 | if( pRet->pPrior ) pRet->selFlags |= SF_Values; |
drh | c195e23 | 2024-03-18 16:30:00 | [diff] [blame] | 728 | pLeft->pPrior = 0; |
| 729 | pLeft->op = TK_SELECT; |
| 730 | assert( pLeft->pNext==0 ); |
| 731 | assert( pRet->pNext==0 ); |
dan | aa2e244 | 2024-03-13 18:41:05 | [diff] [blame] | 732 | p = &pRet->pSrc->a[0]; |
dan | 35057fb | 2024-03-13 17:33:45 | [diff] [blame] | 733 | p->fg.viaCoroutine = 1; |
dan | 35057fb | 2024-03-13 17:33:45 | [diff] [blame] | 734 | p->iCursor = -1; |
drh | 692c160 | 2024-08-20 19:09:59 | [diff] [blame] | 735 | assert( !p->fg.isIndexedBy && !p->fg.isTabFunc ); |
drh | 27a5ee8 | 2024-03-18 12:49:30 | [diff] [blame] | 736 | p->u1.nRow = 2; |
drh | 1521ca4 | 2024-08-19 22:48:30 | [diff] [blame] | 737 | if( sqlite3SrcItemAttachSubquery(pParse, p, pLeft, 0) ){ |
| 738 | pSubq = p->u4.pSubq; |
| 739 | pSubq->addrFillSub = sqlite3VdbeCurrentAddr(v) + 1; |
| 740 | pSubq->regReturn = ++pParse->nMem; |
| 741 | sqlite3VdbeAddOp3(v, OP_InitCoroutine, |
| 742 | pSubq->regReturn, 0, pSubq->addrFillSub); |
| 743 | sqlite3SelectDestInit(&dest, SRT_Coroutine, pSubq->regReturn); |
dan | f0f4323 | 2024-03-14 17:04:18 | [diff] [blame] | 744 | |
drh | 1521ca4 | 2024-08-19 22:48:30 | [diff] [blame] | 745 | /* Allocate registers for the output of the co-routine. Do so so |
| 746 | ** that there are two unused registers immediately before those |
| 747 | ** used by the co-routine. This allows the code in sqlite3Insert() |
| 748 | ** to use these registers directly, instead of copying the output |
| 749 | ** of the co-routine to a separate array for processing. */ |
| 750 | dest.iSdst = pParse->nMem + 3; |
| 751 | dest.nSdst = pLeft->pEList->nExpr; |
| 752 | pParse->nMem += 2 + dest.nSdst; |
dan | f0f4323 | 2024-03-14 17:04:18 | [diff] [blame] | 753 | |
drh | 1521ca4 | 2024-08-19 22:48:30 | [diff] [blame] | 754 | pLeft->selFlags |= SF_MultiValue; |
| 755 | sqlite3Select(pParse, pLeft, &dest); |
| 756 | pSubq->regResult = dest.iSdst; |
| 757 | assert( pParse->nErr || dest.iSdst>0 ); |
| 758 | } |
dan | 35057fb | 2024-03-13 17:33:45 | [diff] [blame] | 759 | pLeft = pRet; |
| 760 | } |
dan | 815e055 | 2024-03-11 17:27:19 | [diff] [blame] | 761 | }else{ |
| 762 | p = &pLeft->pSrc->a[0]; |
drh | ac7c6f5 | 2024-03-18 13:31:24 | [diff] [blame] | 763 | assert( !p->fg.isTabFunc && !p->fg.isIndexedBy ); |
drh | 27a5ee8 | 2024-03-18 12:49:30 | [diff] [blame] | 764 | p->u1.nRow++; |
dan | 815e055 | 2024-03-11 17:27:19 | [diff] [blame] | 765 | } |
| 766 | |
| 767 | if( pParse->nErr==0 ){ |
drh | 1521ca4 | 2024-08-19 22:48:30 | [diff] [blame] | 768 | Subquery *pSubq; |
drh | 2ad2584 | 2024-03-18 17:13:52 | [diff] [blame] | 769 | assert( p!=0 ); |
drh | 1521ca4 | 2024-08-19 22:48:30 | [diff] [blame] | 770 | assert( p->fg.isSubquery ); |
| 771 | pSubq = p->u4.pSubq; |
| 772 | assert( pSubq!=0 ); |
| 773 | assert( pSubq->pSelect!=0 ); |
| 774 | assert( pSubq->pSelect->pEList!=0 ); |
| 775 | if( pSubq->pSelect->pEList->nExpr!=pRow->nExpr ){ |
| 776 | sqlite3SelectWrongNumTermsError(pParse, pSubq->pSelect); |
dan | aa2e244 | 2024-03-13 18:41:05 | [diff] [blame] | 777 | }else{ |
drh | 1521ca4 | 2024-08-19 22:48:30 | [diff] [blame] | 778 | sqlite3ExprCodeExprList(pParse, pRow, pSubq->regResult, 0, 0); |
| 779 | sqlite3VdbeAddOp1(pParse->pVdbe, OP_Yield, pSubq->regReturn); |
dan | 815e055 | 2024-03-11 17:27:19 | [diff] [blame] | 780 | } |
| 781 | } |
dan | aa2e244 | 2024-03-13 18:41:05 | [diff] [blame] | 782 | sqlite3ExprListDelete(pParse->db, pRow); |
dan | 815e055 | 2024-03-11 17:27:19 | [diff] [blame] | 783 | } |
| 784 | |
| 785 | return pLeft; |
| 786 | } |
drh | 9d9cf22 | 2007-02-13 15:01:11 | [diff] [blame] | 787 | |
| 788 | /* Forward declaration */ |
| 789 | static int xferOptimization( |
| 790 | Parse *pParse, /* Parser context */ |
| 791 | Table *pDest, /* The table we are inserting into */ |
| 792 | Select *pSelect, /* A SELECT statement to use as the data source */ |
| 793 | int onError, /* How to handle constraint errors */ |
| 794 | int iDbDest /* The database of pDest */ |
| 795 | ); |
| 796 | |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 | [diff] [blame] | 797 | /* |
drh | d82b502 | 2013-10-26 00:58:34 | [diff] [blame] | 798 | ** This routine is called to handle SQL of the following forms: |
drh | cce7d17 | 2000-05-31 15:34:51 | [diff] [blame] | 799 | ** |
drh | a21f78b | 2015-04-19 18:32:43 | [diff] [blame] | 800 | ** insert into TABLE (IDLIST) values(EXPRLIST),(EXPRLIST),... |
drh | 1ccde15 | 2000-06-17 13:12:39 | [diff] [blame] | 801 | ** insert into TABLE (IDLIST) select |
drh | a21f78b | 2015-04-19 18:32:43 | [diff] [blame] | 802 | ** insert into TABLE (IDLIST) default values |
drh | cce7d17 | 2000-05-31 15:34:51 | [diff] [blame] | 803 | ** |
drh | 1ccde15 | 2000-06-17 13:12:39 | [diff] [blame] | 804 | ** The IDLIST following the table name is always optional. If omitted, |
drh | a21f78b | 2015-04-19 18:32:43 | [diff] [blame] | 805 | ** then a list of all (non-hidden) columns for the table is substituted. |
| 806 | ** The IDLIST appears in the pColumn parameter. pColumn is NULL if IDLIST |
| 807 | ** is omitted. |
drh | 1ccde15 | 2000-06-17 13:12:39 | [diff] [blame] | 808 | ** |
drh | a21f78b | 2015-04-19 18:32:43 | [diff] [blame] | 809 | ** For the pSelect parameter holds the values to be inserted for the |
| 810 | ** first two forms shown above. A VALUES clause is really just short-hand |
| 811 | ** for a SELECT statement that omits the FROM clause and everything else |
| 812 | ** that follows. If the pSelect parameter is NULL, that means that the |
| 813 | ** DEFAULT VALUES form of the INSERT statement is intended. |
drh | 142e30d | 2002-08-28 03:00:58 | [diff] [blame] | 814 | ** |
drh | 9d9cf22 | 2007-02-13 15:01:11 | [diff] [blame] | 815 | ** The code generated follows one of four templates. For a simple |
drh | a21f78b | 2015-04-19 18:32:43 | [diff] [blame] | 816 | ** insert with data coming from a single-row VALUES clause, the code executes |
drh | e00ee6e | 2008-06-20 15:24:01 | [diff] [blame] | 817 | ** once straight down through. Pseudo-code follows (we call this |
| 818 | ** the "1st template"): |
drh | 142e30d | 2002-08-28 03:00:58 | [diff] [blame] | 819 | ** |
| 820 | ** open write cursor to <table> and its indices |
drh | ec95c44 | 2013-10-23 01:57:32 | [diff] [blame] | 821 | ** put VALUES clause expressions into registers |
drh | 142e30d | 2002-08-28 03:00:58 | [diff] [blame] | 822 | ** write the resulting record into <table> |
| 823 | ** cleanup |
| 824 | ** |
drh | 9d9cf22 | 2007-02-13 15:01:11 | [diff] [blame] | 825 | ** The three remaining templates assume the statement is of the form |
drh | 142e30d | 2002-08-28 03:00:58 | [diff] [blame] | 826 | ** |
| 827 | ** INSERT INTO <table> SELECT ... |
| 828 | ** |
drh | 9d9cf22 | 2007-02-13 15:01:11 | [diff] [blame] | 829 | ** If the SELECT clause is of the restricted form "SELECT * FROM <table2>" - |
| 830 | ** in other words if the SELECT pulls all columns from a single table |
| 831 | ** and there is no WHERE or LIMIT or GROUP BY or ORDER BY clauses, and |
| 832 | ** if <table2> and <table1> are distinct tables but have identical |
| 833 | ** schemas, including all the same indices, then a special optimization |
| 834 | ** is invoked that copies raw records from <table2> over to <table1>. |
| 835 | ** See the xferOptimization() function for the implementation of this |
drh | e00ee6e | 2008-06-20 15:24:01 | [diff] [blame] | 836 | ** template. This is the 2nd template. |
drh | 9d9cf22 | 2007-02-13 15:01:11 | [diff] [blame] | 837 | ** |
| 838 | ** open a write cursor to <table> |
| 839 | ** open read cursor on <table2> |
| 840 | ** transfer all records in <table2> over to <table> |
| 841 | ** close cursors |
| 842 | ** foreach index on <table> |
| 843 | ** open a write cursor on the <table> index |
| 844 | ** open a read cursor on the corresponding <table2> index |
| 845 | ** transfer all records from the read to the write cursors |
| 846 | ** close cursors |
| 847 | ** end foreach |
| 848 | ** |
drh | e00ee6e | 2008-06-20 15:24:01 | [diff] [blame] | 849 | ** The 3rd template is for when the second template does not apply |
drh | 9d9cf22 | 2007-02-13 15:01:11 | [diff] [blame] | 850 | ** and the SELECT clause does not read from <table> at any time. |
| 851 | ** The generated code follows this template: |
drh | 142e30d | 2002-08-28 03:00:58 | [diff] [blame] | 852 | ** |
drh | e00ee6e | 2008-06-20 15:24:01 | [diff] [blame] | 853 | ** X <- A |
drh | 142e30d | 2002-08-28 03:00:58 | [diff] [blame] | 854 | ** goto B |
| 855 | ** A: setup for the SELECT |
drh | 9d9cf22 | 2007-02-13 15:01:11 | [diff] [blame] | 856 | ** loop over the rows in the SELECT |
drh | e00ee6e | 2008-06-20 15:24:01 | [diff] [blame] | 857 | ** load values into registers R..R+n |
| 858 | ** yield X |
drh | 142e30d | 2002-08-28 03:00:58 | [diff] [blame] | 859 | ** end loop |
| 860 | ** cleanup after the SELECT |
drh | 81cf13e | 2014-02-07 18:27:53 | [diff] [blame] | 861 | ** end-coroutine X |
drh | e00ee6e | 2008-06-20 15:24:01 | [diff] [blame] | 862 | ** B: open write cursor to <table> and its indices |
drh | 81cf13e | 2014-02-07 18:27:53 | [diff] [blame] | 863 | ** C: yield X, at EOF goto D |
drh | e00ee6e | 2008-06-20 15:24:01 | [diff] [blame] | 864 | ** insert the select result into <table> from R..R+n |
| 865 | ** goto C |
drh | 142e30d | 2002-08-28 03:00:58 | [diff] [blame] | 866 | ** D: cleanup |
| 867 | ** |
drh | e00ee6e | 2008-06-20 15:24:01 | [diff] [blame] | 868 | ** The 4th template is used if the insert statement takes its |
drh | 142e30d | 2002-08-28 03:00:58 | [diff] [blame] | 869 | ** values from a SELECT but the data is being inserted into a table |
| 870 | ** that is also read as part of the SELECT. In the third form, |
peter.d.reid | 60ec914 | 2014-09-06 16:39:46 | [diff] [blame] | 871 | ** we have to use an intermediate table to store the results of |
drh | 142e30d | 2002-08-28 03:00:58 | [diff] [blame] | 872 | ** the select. The template is like this: |
| 873 | ** |
drh | e00ee6e | 2008-06-20 15:24:01 | [diff] [blame] | 874 | ** X <- A |
drh | 142e30d | 2002-08-28 03:00:58 | [diff] [blame] | 875 | ** goto B |
| 876 | ** A: setup for the SELECT |
| 877 | ** loop over the tables in the SELECT |
drh | e00ee6e | 2008-06-20 15:24:01 | [diff] [blame] | 878 | ** load value into register R..R+n |
| 879 | ** yield X |
drh | 142e30d | 2002-08-28 03:00:58 | [diff] [blame] | 880 | ** end loop |
| 881 | ** cleanup after the SELECT |
drh | 81cf13e | 2014-02-07 18:27:53 | [diff] [blame] | 882 | ** end co-routine R |
drh | e00ee6e | 2008-06-20 15:24:01 | [diff] [blame] | 883 | ** B: open temp table |
drh | 81cf13e | 2014-02-07 18:27:53 | [diff] [blame] | 884 | ** L: yield X, at EOF goto M |
drh | e00ee6e | 2008-06-20 15:24:01 | [diff] [blame] | 885 | ** insert row from R..R+n into temp table |
| 886 | ** goto L |
| 887 | ** M: open write cursor to <table> and its indices |
| 888 | ** rewind temp table |
| 889 | ** C: loop over rows of intermediate table |
drh | 142e30d | 2002-08-28 03:00:58 | [diff] [blame] | 890 | ** transfer values form intermediate table into <table> |
drh | e00ee6e | 2008-06-20 15:24:01 | [diff] [blame] | 891 | ** end loop |
| 892 | ** D: cleanup |
drh | cce7d17 | 2000-05-31 15:34:51 | [diff] [blame] | 893 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 | [diff] [blame] | 894 | void sqlite3Insert( |
drh | cce7d17 | 2000-05-31 15:34:51 | [diff] [blame] | 895 | Parse *pParse, /* Parser context */ |
drh | 113088e | 2003-03-20 01:16:58 | [diff] [blame] | 896 | SrcList *pTabList, /* Name of table into which we are inserting */ |
drh | 5974a30 | 2000-06-07 14:42:26 | [diff] [blame] | 897 | Select *pSelect, /* A SELECT statement to use as the data source */ |
drh | f5f1915 | 2019-10-21 01:04:11 | [diff] [blame] | 898 | IdList *pColumn, /* Column names corresponding to IDLIST, or NULL. */ |
drh | 2c2e844 | 2018-04-07 15:04:05 | [diff] [blame] | 899 | int onError, /* How to handle constraint errors */ |
drh | 46d2e5c | 2018-04-12 13:15:43 | [diff] [blame] | 900 | Upsert *pUpsert /* ON CONFLICT clauses for upsert, or NULL */ |
drh | cce7d17 | 2000-05-31 15:34:51 | [diff] [blame] | 901 | ){ |
drh | 6a288a3 | 2008-01-07 19:20:24 | [diff] [blame] | 902 | sqlite3 *db; /* The main database structure */ |
| 903 | Table *pTab; /* The table to insert into. aka TABLE */ |
drh | 60ffc80 | 2016-11-21 21:33:46 | [diff] [blame] | 904 | int i, j; /* Loop counters */ |
drh | 5974a30 | 2000-06-07 14:42:26 | [diff] [blame] | 905 | Vdbe *v; /* Generate code into this virtual machine */ |
| 906 | Index *pIdx; /* For looping over indices of the table */ |
drh | 967e8b7 | 2000-06-21 13:59:10 | [diff] [blame] | 907 | int nColumn; /* Number of columns in the data */ |
drh | 6a288a3 | 2008-01-07 19:20:24 | [diff] [blame] | 908 | int nHidden = 0; /* Number of hidden columns if TABLE is virtual */ |
drh | 26198bb | 2013-10-31 11:15:09 | [diff] [blame] | 909 | int iDataCur = 0; /* VDBE cursor that is the main data repository */ |
| 910 | int iIdxCur = 0; /* First index cursor */ |
drh | d82b502 | 2013-10-26 00:58:34 | [diff] [blame] | 911 | int ipkColumn = -1; /* Column that is the INTEGER PRIMARY KEY */ |
drh | 0ca3e24 | 2002-01-29 23:07:02 | [diff] [blame] | 912 | int endOfLoop; /* Label for the end of the insertion loop */ |
danielk1977 | cfe9a69 | 2004-06-16 12:00:29 | [diff] [blame] | 913 | int srcTab = 0; /* Data comes from this temporary cursor if >=0 */ |
drh | e00ee6e | 2008-06-20 15:24:01 | [diff] [blame] | 914 | int addrInsTop = 0; /* Jump to label "D" */ |
| 915 | int addrCont = 0; /* Top of insert loop. Label "C" in templates 3 and 4 */ |
drh | 2eb9537 | 2008-06-06 15:04:36 | [diff] [blame] | 916 | SelectDest dest; /* Destination for SELECT on rhs of INSERT */ |
drh | 6a288a3 | 2008-01-07 19:20:24 | [diff] [blame] | 917 | int iDb; /* Index of database holding TABLE */ |
drh | 05a86c5 | 2014-02-16 01:55:49 | [diff] [blame] | 918 | u8 useTempTable = 0; /* Store SELECT results in intermediate table */ |
| 919 | u8 appendFlag = 0; /* True if the insert is likely to be an append */ |
| 920 | u8 withoutRowid; /* 0 for normal table. 1 for WITHOUT ROWID table */ |
drh | a21f78b | 2015-04-19 18:32:43 | [diff] [blame] | 921 | u8 bIdListInOrder; /* True if IDLIST is in table order */ |
drh | 75593d9 | 2014-01-10 20:46:55 | [diff] [blame] | 922 | ExprList *pList = 0; /* List of VALUES() to be inserted */ |
drh | c27ea2a | 2019-10-16 20:05:56 | [diff] [blame] | 923 | int iRegStore; /* Register in which to store next column */ |
drh | cce7d17 | 2000-05-31 15:34:51 | [diff] [blame] | 924 | |
drh | 6a288a3 | 2008-01-07 19:20:24 | [diff] [blame] | 925 | /* Register allocations */ |
drh | 1bd10f8 | 2008-12-10 21:19:56 | [diff] [blame] | 926 | int regFromSelect = 0;/* Base register for data coming from SELECT */ |
drh | 6a288a3 | 2008-01-07 19:20:24 | [diff] [blame] | 927 | int regAutoinc = 0; /* Register holding the AUTOINCREMENT counter */ |
| 928 | int regRowCount = 0; /* Memory cell used for the row counter */ |
| 929 | int regIns; /* Block of regs holding rowid+data being inserted */ |
| 930 | int regRowid; /* registers holding insert rowid */ |
| 931 | int regData; /* register holding first column to insert */ |
drh | aa9b896 | 2008-01-08 02:57:55 | [diff] [blame] | 932 | int *aRegIdx = 0; /* One register allocated to each index */ |
drh | 8b62a82 | 2025-01-28 12:50:17 | [diff] [blame] | 933 | int *aTabColMap = 0; /* Mapping from pTab columns to pCol entries */ |
drh | 6a288a3 | 2008-01-07 19:20:24 | [diff] [blame] | 934 | |
drh | 798da52 | 2004-11-04 04:42:28 | [diff] [blame] | 935 | #ifndef SQLITE_OMIT_TRIGGER |
| 936 | int isView; /* True if attempting to insert into a view */ |
danielk1977 | 2f886d1 | 2009-02-28 10:47:41 | [diff] [blame] | 937 | Trigger *pTrigger; /* List of triggers on pTab, if required */ |
| 938 | int tmask; /* Mask of trigger times */ |
drh | 798da52 | 2004-11-04 04:42:28 | [diff] [blame] | 939 | #endif |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 | [diff] [blame] | 940 | |
drh | 1743575 | 2007-08-16 04:30:38 | [diff] [blame] | 941 | db = pParse->db; |
drh | 0c7d3d3 | 2022-01-24 16:47:12 | [diff] [blame] | 942 | assert( db->pParse==pParse ); |
| 943 | if( pParse->nErr ){ |
drh | 6f7adc8 | 2006-01-11 21:41:20 | [diff] [blame] | 944 | goto insert_cleanup; |
| 945 | } |
drh | 0c7d3d3 | 2022-01-24 16:47:12 | [diff] [blame] | 946 | assert( db->mallocFailed==0 ); |
drh | 4c88348 | 2017-06-03 20:09:37 | [diff] [blame] | 947 | dest.iSDParm = 0; /* Suppress a harmless compiler warning */ |
drh | daffd0e | 2001-04-11 14:28:42 | [diff] [blame] | 948 | |
drh | 75593d9 | 2014-01-10 20:46:55 | [diff] [blame] | 949 | /* If the Select object is really just a simple VALUES() list with a |
drh | a21f78b | 2015-04-19 18:32:43 | [diff] [blame] | 950 | ** single row (the common case) then keep that one row of values |
| 951 | ** and discard the other (unused) parts of the pSelect object |
drh | 75593d9 | 2014-01-10 20:46:55 | [diff] [blame] | 952 | */ |
| 953 | if( pSelect && (pSelect->selFlags & SF_Values)!=0 && pSelect->pPrior==0 ){ |
| 954 | pList = pSelect->pEList; |
| 955 | pSelect->pEList = 0; |
| 956 | sqlite3SelectDelete(db, pSelect); |
| 957 | pSelect = 0; |
| 958 | } |
| 959 | |
drh | 1ccde15 | 2000-06-17 13:12:39 | [diff] [blame] | 960 | /* Locate the table into which we will be inserting new information. |
| 961 | */ |
drh | 113088e | 2003-03-20 01:16:58 | [diff] [blame] | 962 | assert( pTabList->nSrc==1 ); |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 | [diff] [blame] | 963 | pTab = sqlite3SrcListLookup(pParse, pTabList); |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 | [diff] [blame] | 964 | if( pTab==0 ){ |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 | [diff] [blame] | 965 | goto insert_cleanup; |
| 966 | } |
danielk1977 | da18423 | 2006-01-05 11:34:32 | [diff] [blame] | 967 | iDb = sqlite3SchemaToIndex(db, pTab->pSchema); |
| 968 | assert( iDb<db->nDb ); |
drh | a0daa75 | 2016-09-16 11:53:10 | [diff] [blame] | 969 | if( sqlite3AuthCheck(pParse, SQLITE_INSERT, pTab->zName, 0, |
| 970 | db->aDb[iDb].zDbSName) ){ |
drh | 1962bda | 2003-01-12 19:33:52 | [diff] [blame] | 971 | goto insert_cleanup; |
| 972 | } |
drh | ec95c44 | 2013-10-23 01:57:32 | [diff] [blame] | 973 | withoutRowid = !HasRowid(pTab); |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 | [diff] [blame] | 974 | |
drh | b7f9164 | 2004-10-31 02:22:47 | [diff] [blame] | 975 | /* Figure out if we have any triggers and if the table being |
| 976 | ** inserted into is a view |
| 977 | */ |
| 978 | #ifndef SQLITE_OMIT_TRIGGER |
danielk1977 | 2f886d1 | 2009-02-28 10:47:41 | [diff] [blame] | 979 | pTrigger = sqlite3TriggersExist(pParse, pTab, TK_INSERT, 0, &tmask); |
drh | f38524d | 2021-08-02 16:41:57 | [diff] [blame] | 980 | isView = IsView(pTab); |
drh | b7f9164 | 2004-10-31 02:22:47 | [diff] [blame] | 981 | #else |
danielk1977 | 2f886d1 | 2009-02-28 10:47:41 | [diff] [blame] | 982 | # define pTrigger 0 |
| 983 | # define tmask 0 |
drh | b7f9164 | 2004-10-31 02:22:47 | [diff] [blame] | 984 | # define isView 0 |
| 985 | #endif |
| 986 | #ifdef SQLITE_OMIT_VIEW |
| 987 | # undef isView |
| 988 | # define isView 0 |
| 989 | #endif |
danielk1977 | 2f886d1 | 2009-02-28 10:47:41 | [diff] [blame] | 990 | assert( (pTrigger && tmask) || (pTrigger==0 && tmask==0) ); |
drh | b7f9164 | 2004-10-31 02:22:47 | [diff] [blame] | 991 | |
drh | 2a7dcbf | 2022-04-06 15:41:53 | [diff] [blame] | 992 | #if TREETRACE_ENABLED |
| 993 | if( sqlite3TreeTrace & 0x10000 ){ |
| 994 | sqlite3TreeViewLine(0, "In sqlite3Insert() at %s:%d", __FILE__, __LINE__); |
drh | c2d0df9 | 2022-04-06 18:30:17 | [diff] [blame] | 995 | sqlite3TreeViewInsert(pParse->pWith, pTabList, pColumn, pSelect, pList, |
drh | 2a7dcbf | 2022-04-06 15:41:53 | [diff] [blame] | 996 | onError, pUpsert, pTrigger); |
| 997 | } |
| 998 | #endif |
| 999 | |
drh | f573c99 | 2002-07-31 00:32:50 | [diff] [blame] | 1000 | /* If pTab is really a view, make sure it has been initialized. |
drh | d82b502 | 2013-10-26 00:58:34 | [diff] [blame] | 1001 | ** ViewGetColumnNames() is a no-op if pTab is not a view. |
drh | f573c99 | 2002-07-31 00:32:50 | [diff] [blame] | 1002 | */ |
danielk1977 | b3d24bf | 2006-06-19 03:05:10 | [diff] [blame] | 1003 | if( sqlite3ViewGetColumnNames(pParse, pTab) ){ |
drh | 5cf590c | 2003-04-24 01:45:04 | [diff] [blame] | 1004 | goto insert_cleanup; |
drh | f573c99 | 2002-07-31 00:32:50 | [diff] [blame] | 1005 | } |
| 1006 | |
drh | d82b502 | 2013-10-26 00:58:34 | [diff] [blame] | 1007 | /* Cannot insert into a read-only table. |
danielk1977 | 595a523 | 2009-07-24 17:58:53 | [diff] [blame] | 1008 | */ |
drh | 3cbf38c | 2023-03-28 11:18:04 | [diff] [blame] | 1009 | if( sqlite3IsReadOnly(pParse, pTab, pTrigger) ){ |
danielk1977 | 595a523 | 2009-07-24 17:58:53 | [diff] [blame] | 1010 | goto insert_cleanup; |
| 1011 | } |
| 1012 | |
drh | 1ccde15 | 2000-06-17 13:12:39 | [diff] [blame] | 1013 | /* Allocate a VDBE |
| 1014 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 | [diff] [blame] | 1015 | v = sqlite3GetVdbe(pParse); |
drh | 5974a30 | 2000-06-07 14:42:26 | [diff] [blame] | 1016 | if( v==0 ) goto insert_cleanup; |
drh | 4794f73 | 2004-11-05 17:17:50 | [diff] [blame] | 1017 | if( pParse->nested==0 ) sqlite3VdbeCountChanges(v); |
danielk1977 | 2f886d1 | 2009-02-28 10:47:41 | [diff] [blame] | 1018 | sqlite3BeginWriteOperation(pParse, pSelect || pTrigger, iDb); |
drh | 1ccde15 | 2000-06-17 13:12:39 | [diff] [blame] | 1019 | |
drh | 9d9cf22 | 2007-02-13 15:01:11 | [diff] [blame] | 1020 | #ifndef SQLITE_OMIT_XFER_OPT |
| 1021 | /* If the statement is of the form |
| 1022 | ** |
| 1023 | ** INSERT INTO <table1> SELECT * FROM <table2>; |
| 1024 | ** |
| 1025 | ** Then special optimizations can be applied that make the transfer |
| 1026 | ** very fast and which reduce fragmentation of indices. |
drh | e00ee6e | 2008-06-20 15:24:01 | [diff] [blame] | 1027 | ** |
| 1028 | ** This is the 2nd template. |
drh | 9d9cf22 | 2007-02-13 15:01:11 | [diff] [blame] | 1029 | */ |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 1030 | if( pColumn==0 |
drh | 935c372 | 2022-02-28 16:44:58 | [diff] [blame] | 1031 | && pSelect!=0 |
| 1032 | && pTrigger==0 |
| 1033 | && xferOptimization(pParse, pTab, pSelect, onError, iDb) |
| 1034 | ){ |
danielk1977 | 2f886d1 | 2009-02-28 10:47:41 | [diff] [blame] | 1035 | assert( !pTrigger ); |
drh | 9d9cf22 | 2007-02-13 15:01:11 | [diff] [blame] | 1036 | assert( pList==0 ); |
drh | 0b9f50d | 2009-06-23 20:28:53 | [diff] [blame] | 1037 | goto insert_end; |
drh | 9d9cf22 | 2007-02-13 15:01:11 | [diff] [blame] | 1038 | } |
| 1039 | #endif /* SQLITE_OMIT_XFER_OPT */ |
| 1040 | |
drh | 2958a4e | 2004-11-12 03:56:15 | [diff] [blame] | 1041 | /* If this is an AUTOINCREMENT table, look up the sequence number in the |
drh | 6a288a3 | 2008-01-07 19:20:24 | [diff] [blame] | 1042 | ** sqlite_sequence table and store it in memory cell regAutoinc. |
drh | 2958a4e | 2004-11-12 03:56:15 | [diff] [blame] | 1043 | */ |
drh | 6a288a3 | 2008-01-07 19:20:24 | [diff] [blame] | 1044 | regAutoinc = autoIncBegin(pParse, iDb, pTab); |
drh | 2958a4e | 2004-11-12 03:56:15 | [diff] [blame] | 1045 | |
drh | f5f1915 | 2019-10-21 01:04:11 | [diff] [blame] | 1046 | /* Allocate a block registers to hold the rowid and the values |
| 1047 | ** for all columns of the new row. |
drh | 1ccde15 | 2000-06-17 13:12:39 | [diff] [blame] | 1048 | */ |
drh | 05a86c5 | 2014-02-16 01:55:49 | [diff] [blame] | 1049 | regRowid = regIns = pParse->nMem+1; |
| 1050 | pParse->nMem += pTab->nCol + 1; |
danielk1977 | 034ca14 | 2007-06-26 10:38:54 | [diff] [blame] | 1051 | if( IsVirtual(pTab) ){ |
drh | 05a86c5 | 2014-02-16 01:55:49 | [diff] [blame] | 1052 | regRowid++; |
| 1053 | pParse->nMem++; |
danielk1977 | 034ca14 | 2007-06-26 10:38:54 | [diff] [blame] | 1054 | } |
drh | 05a86c5 | 2014-02-16 01:55:49 | [diff] [blame] | 1055 | regData = regRowid+1; |
drh | 1ccde15 | 2000-06-17 13:12:39 | [diff] [blame] | 1056 | |
| 1057 | /* If the INSERT statement included an IDLIST term, then make sure |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 1058 | ** all elements of the IDLIST really are columns of the table and |
drh | 1ccde15 | 2000-06-17 13:12:39 | [diff] [blame] | 1059 | ** remember the column indices. |
drh | c839258 | 2001-12-31 02:48:51 | [diff] [blame] | 1060 | ** |
| 1061 | ** If the table has an INTEGER PRIMARY KEY column and that column |
drh | d82b502 | 2013-10-26 00:58:34 | [diff] [blame] | 1062 | ** is named in the IDLIST, then record in the ipkColumn variable |
| 1063 | ** the index into IDLIST of the primary key column. ipkColumn is |
drh | c839258 | 2001-12-31 02:48:51 | [diff] [blame] | 1064 | ** the index of the primary key as it appears in IDLIST, not as |
drh | d82b502 | 2013-10-26 00:58:34 | [diff] [blame] | 1065 | ** is appears in the original table. (The index of the INTEGER |
drh | f5f1915 | 2019-10-21 01:04:11 | [diff] [blame] | 1066 | ** PRIMARY KEY in the original table is pTab->iPKey.) After this |
| 1067 | ** loop, if ipkColumn==(-1), that means that integer primary key |
| 1068 | ** is unspecified, and hence the table is either WITHOUT ROWID or |
| 1069 | ** it will automatically generated an integer primary key. |
| 1070 | ** |
| 1071 | ** bIdListInOrder is true if the columns in IDLIST are in storage |
| 1072 | ** order. This enables an optimization that avoids shuffling the |
| 1073 | ** columns into storage order. False negatives are harmless, |
| 1074 | ** but false positives will cause database corruption. |
drh | 1ccde15 | 2000-06-17 13:12:39 | [diff] [blame] | 1075 | */ |
drh | d4cd292 | 2019-10-17 18:07:22 | [diff] [blame] | 1076 | bIdListInOrder = (pTab->tabFlags & (TF_OOOHidden|TF_HasStored))==0; |
drh | 967e8b7 | 2000-06-21 13:59:10 | [diff] [blame] | 1077 | if( pColumn ){ |
drh | 8b62a82 | 2025-01-28 12:50:17 | [diff] [blame] | 1078 | aTabColMap = sqlite3DbMallocZero(db, pTab->nCol*sizeof(int)); |
| 1079 | if( aTabColMap==0 ) goto insert_cleanup; |
drh | 967e8b7 | 2000-06-21 13:59:10 | [diff] [blame] | 1080 | for(i=0; i<pColumn->nId; i++){ |
drh | 9d90a3a | 2025-02-08 14:15:42 | [diff] [blame] | 1081 | j = sqlite3ColumnIndex(pTab, pColumn->a[i].zName); |
| 1082 | if( j>=0 ){ |
| 1083 | if( aTabColMap[j]==0 ) aTabColMap[j] = i+1; |
| 1084 | if( i!=j ) bIdListInOrder = 0; |
| 1085 | if( j==pTab->iPKey ){ |
| 1086 | ipkColumn = i; assert( !withoutRowid ); |
drh | cce7d17 | 2000-05-31 15:34:51 | [diff] [blame] | 1087 | } |
drh | 9d90a3a | 2025-02-08 14:15:42 | [diff] [blame] | 1088 | #ifndef SQLITE_OMIT_GENERATED_COLUMNS |
| 1089 | if( pTab->aCol[j].colFlags & (COLFLAG_STORED|COLFLAG_VIRTUAL) ){ |
| 1090 | sqlite3ErrorMsg(pParse, |
| 1091 | "cannot INSERT into generated column \"%s\"", |
| 1092 | pTab->aCol[j].zCnName); |
| 1093 | goto insert_cleanup; |
| 1094 | } |
| 1095 | #endif |
| 1096 | }else{ |
drh | ec95c44 | 2013-10-23 01:57:32 | [diff] [blame] | 1097 | if( sqlite3IsRowid(pColumn->a[i].zName) && !withoutRowid ){ |
drh | d82b502 | 2013-10-26 00:58:34 | [diff] [blame] | 1098 | ipkColumn = i; |
drh | e48ae71 | 2014-05-23 11:48:57 | [diff] [blame] | 1099 | bIdListInOrder = 0; |
drh | a0217ba | 2003-06-01 01:10:33 | [diff] [blame] | 1100 | }else{ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 | [diff] [blame] | 1101 | sqlite3ErrorMsg(pParse, "table %S has no column named %s", |
drh | a979993 | 2021-03-19 13:00:28 | [diff] [blame] | 1102 | pTabList->a, pColumn->a[i].zName); |
dan | 1db9510 | 2010-06-28 10:15:19 | [diff] [blame] | 1103 | pParse->checkSchema = 1; |
drh | a0217ba | 2003-06-01 01:10:33 | [diff] [blame] | 1104 | goto insert_cleanup; |
| 1105 | } |
drh | cce7d17 | 2000-05-31 15:34:51 | [diff] [blame] | 1106 | } |
| 1107 | } |
| 1108 | } |
drh | 1ccde15 | 2000-06-17 13:12:39 | [diff] [blame] | 1109 | |
drh | cce7d17 | 2000-05-31 15:34:51 | [diff] [blame] | 1110 | /* Figure out how many columns of data are supplied. If the data |
| 1111 | ** is coming from a SELECT statement, then generate a co-routine that |
| 1112 | ** produces a single row of the SELECT on each invocation. The |
| 1113 | ** co-routine is the common header to the 3rd and 4th templates. |
| 1114 | */ |
drh | 5f08526 | 2012-09-15 13:29:23 | [diff] [blame] | 1115 | if( pSelect ){ |
drh | a21f78b | 2015-04-19 18:32:43 | [diff] [blame] | 1116 | /* Data is coming from a SELECT or from a multi-row VALUES clause. |
| 1117 | ** Generate a co-routine to run the SELECT. */ |
drh | 05a86c5 | 2014-02-16 01:55:49 | [diff] [blame] | 1118 | int rc; /* Result code */ |
drh | cce7d17 | 2000-05-31 15:34:51 | [diff] [blame] | 1119 | |
dan | 400992b | 2024-03-14 19:01:17 | [diff] [blame] | 1120 | if( pSelect->pSrc->nSrc==1 |
| 1121 | && pSelect->pSrc->a[0].fg.viaCoroutine |
| 1122 | && pSelect->pPrior==0 |
| 1123 | ){ |
dan | 815e055 | 2024-03-11 17:27:19 | [diff] [blame] | 1124 | SrcItem *pItem = &pSelect->pSrc->a[0]; |
drh | 1521ca4 | 2024-08-19 22:48:30 | [diff] [blame] | 1125 | Subquery *pSubq; |
| 1126 | assert( pItem->fg.isSubquery ); |
| 1127 | pSubq = pItem->u4.pSubq; |
| 1128 | dest.iSDParm = pSubq->regReturn; |
| 1129 | regFromSelect = pSubq->regResult; |
| 1130 | assert( pSubq->pSelect!=0 ); |
| 1131 | assert( pSubq->pSelect->pEList!=0 ); |
| 1132 | nColumn = pSubq->pSelect->pEList->nExpr; |
drh | 27a5ee8 | 2024-03-18 12:49:30 | [diff] [blame] | 1133 | ExplainQueryPlan((pParse, 0, "SCAN %S", pItem)); |
dan | f0f4323 | 2024-03-14 17:04:18 | [diff] [blame] | 1134 | if( bIdListInOrder && nColumn==pTab->nCol ){ |
| 1135 | regData = regFromSelect; |
| 1136 | regRowid = regData - 1; |
| 1137 | regIns = regRowid - (IsVirtual(pTab) ? 1 : 0); |
| 1138 | } |
dan | 815e055 | 2024-03-11 17:27:19 | [diff] [blame] | 1139 | }else{ |
| 1140 | int addrTop; /* Top of the co-routine */ |
drh | 2ad2584 | 2024-03-18 17:13:52 | [diff] [blame] | 1141 | int regYield = ++pParse->nMem; |
dan | 815e055 | 2024-03-11 17:27:19 | [diff] [blame] | 1142 | addrTop = sqlite3VdbeCurrentAddr(v) + 1; |
| 1143 | sqlite3VdbeAddOp3(v, OP_InitCoroutine, regYield, 0, addrTop); |
| 1144 | sqlite3SelectDestInit(&dest, SRT_Coroutine, regYield); |
| 1145 | dest.iSdst = bIdListInOrder ? regData : 0; |
| 1146 | dest.nSdst = pTab->nCol; |
| 1147 | rc = sqlite3Select(pParse, pSelect, &dest); |
| 1148 | regFromSelect = dest.iSdst; |
| 1149 | assert( db->pParse==pParse ); |
| 1150 | if( rc || pParse->nErr ) goto insert_cleanup; |
| 1151 | assert( db->mallocFailed==0 ); |
| 1152 | sqlite3VdbeEndCoroutine(v, regYield); |
| 1153 | sqlite3VdbeJumpHere(v, addrTop - 1); /* label B: */ |
| 1154 | assert( pSelect->pEList ); |
| 1155 | nColumn = pSelect->pEList->nExpr; |
| 1156 | } |
drh | cce7d17 | 2000-05-31 15:34:51 | [diff] [blame] | 1157 | |
| 1158 | /* Set useTempTable to TRUE if the result of the SELECT statement |
| 1159 | ** should be written into a temporary table (template 4). Set to |
| 1160 | ** FALSE if each output row of the SELECT can be written directly into |
| 1161 | ** the destination table (template 3). |
| 1162 | ** |
| 1163 | ** A temp table must be used if the table being updated is also one |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 1164 | ** of the tables being read by the SELECT statement. Also use a |
drh | cce7d17 | 2000-05-31 15:34:51 | [diff] [blame] | 1165 | ** temp table in the case of row triggers. |
| 1166 | */ |
drh | 05a86c5 | 2014-02-16 01:55:49 | [diff] [blame] | 1167 | if( pTrigger || readsTable(pParse, iDb, pTab) ){ |
drh | cce7d17 | 2000-05-31 15:34:51 | [diff] [blame] | 1168 | useTempTable = 1; |
| 1169 | } |
| 1170 | |
| 1171 | if( useTempTable ){ |
| 1172 | /* Invoke the coroutine to extract information from the SELECT |
| 1173 | ** and add it to a transient table srcTab. The code generated |
| 1174 | ** here is from the 4th template: |
| 1175 | ** |
| 1176 | ** B: open temp table |
drh | 81cf13e | 2014-02-07 18:27:53 | [diff] [blame] | 1177 | ** L: yield X, goto M at EOF |
drh | cce7d17 | 2000-05-31 15:34:51 | [diff] [blame] | 1178 | ** insert row from R..R+n into temp table |
| 1179 | ** goto L |
| 1180 | ** M: ... |
| 1181 | */ |
| 1182 | int regRec; /* Register to hold packed record */ |
| 1183 | int regTempRowid; /* Register to hold temp table ROWID */ |
drh | 06280ee | 2014-02-20 19:32:38 | [diff] [blame] | 1184 | int addrL; /* Label "L" */ |
drh | cce7d17 | 2000-05-31 15:34:51 | [diff] [blame] | 1185 | |
| 1186 | srcTab = pParse->nTab++; |
| 1187 | regRec = sqlite3GetTempReg(pParse); |
| 1188 | regTempRowid = sqlite3GetTempReg(pParse); |
| 1189 | sqlite3VdbeAddOp2(v, OP_OpenEphemeral, srcTab, nColumn); |
drh | 06280ee | 2014-02-20 19:32:38 | [diff] [blame] | 1190 | addrL = sqlite3VdbeAddOp1(v, OP_Yield, dest.iSDParm); VdbeCoverage(v); |
drh | cce7d17 | 2000-05-31 15:34:51 | [diff] [blame] | 1191 | sqlite3VdbeAddOp3(v, OP_MakeRecord, regFromSelect, nColumn, regRec); |
| 1192 | sqlite3VdbeAddOp2(v, OP_NewRowid, srcTab, regTempRowid); |
| 1193 | sqlite3VdbeAddOp3(v, OP_Insert, srcTab, regRec, regTempRowid); |
drh | 076e85f | 2015-09-03 13:46:12 | [diff] [blame] | 1194 | sqlite3VdbeGoto(v, addrL); |
drh | 06280ee | 2014-02-20 19:32:38 | [diff] [blame] | 1195 | sqlite3VdbeJumpHere(v, addrL); |
drh | cce7d17 | 2000-05-31 15:34:51 | [diff] [blame] | 1196 | sqlite3ReleaseTempReg(pParse, regRec); |
| 1197 | sqlite3ReleaseTempReg(pParse, regTempRowid); |
| 1198 | } |
| 1199 | }else{ |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 1200 | /* This is the case if the data for the INSERT is coming from a |
drh | a21f78b | 2015-04-19 18:32:43 | [diff] [blame] | 1201 | ** single-row VALUES clause |
drh | cce7d17 | 2000-05-31 15:34:51 | [diff] [blame] | 1202 | */ |
| 1203 | NameContext sNC; |
| 1204 | memset(&sNC, 0, sizeof(sNC)); |
| 1205 | sNC.pParse = pParse; |
| 1206 | srcTab = -1; |
| 1207 | assert( useTempTable==0 ); |
drh | fea870b | 2015-08-24 20:54:06 | [diff] [blame] | 1208 | if( pList ){ |
| 1209 | nColumn = pList->nExpr; |
| 1210 | if( sqlite3ResolveExprListNames(&sNC, pList) ){ |
drh | cce7d17 | 2000-05-31 15:34:51 | [diff] [blame] | 1211 | goto insert_cleanup; |
| 1212 | } |
drh | fea870b | 2015-08-24 20:54:06 | [diff] [blame] | 1213 | }else{ |
| 1214 | nColumn = 0; |
drh | cce7d17 | 2000-05-31 15:34:51 | [diff] [blame] | 1215 | } |
| 1216 | } |
| 1217 | |
drh | aacc543 | 2002-01-06 17:07:40 | [diff] [blame] | 1218 | /* If there is no IDLIST term but the table has an integer primary |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 1219 | ** key, the set the ipkColumn variable to the integer primary key |
drh | d82b502 | 2013-10-26 00:58:34 | [diff] [blame] | 1220 | ** column index in the original table definition. |
drh | 4a32431 | 2001-12-21 14:30:42 | [diff] [blame] | 1221 | */ |
drh | 147d0cc | 2006-08-25 23:42:53 | [diff] [blame] | 1222 | if( pColumn==0 && nColumn>0 ){ |
drh | d82b502 | 2013-10-26 00:58:34 | [diff] [blame] | 1223 | ipkColumn = pTab->iPKey; |
drh | 427b96a | 2019-10-22 13:01:24 | [diff] [blame] | 1224 | #ifndef SQLITE_OMIT_GENERATED_COLUMNS |
drh | 6ab61d7 | 2019-10-23 15:47:33 | [diff] [blame] | 1225 | if( ipkColumn>=0 && (pTab->tabFlags & TF_HasGenerated)!=0 ){ |
drh | 427b96a | 2019-10-22 13:01:24 | [diff] [blame] | 1226 | testcase( pTab->tabFlags & TF_HasVirtual ); |
drh | 6ab61d7 | 2019-10-23 15:47:33 | [diff] [blame] | 1227 | testcase( pTab->tabFlags & TF_HasStored ); |
drh | 427b96a | 2019-10-22 13:01:24 | [diff] [blame] | 1228 | for(i=ipkColumn-1; i>=0; i--){ |
| 1229 | if( pTab->aCol[i].colFlags & COLFLAG_GENERATED ){ |
| 1230 | testcase( pTab->aCol[i].colFlags & COLFLAG_VIRTUAL ); |
drh | 6ab61d7 | 2019-10-23 15:47:33 | [diff] [blame] | 1231 | testcase( pTab->aCol[i].colFlags & COLFLAG_STORED ); |
drh | 427b96a | 2019-10-22 13:01:24 | [diff] [blame] | 1232 | ipkColumn--; |
| 1233 | } |
| 1234 | } |
| 1235 | } |
| 1236 | #endif |
drh | 05a86c5 | 2014-02-16 01:55:49 | [diff] [blame] | 1237 | |
drh | c7e93f5 | 2021-02-18 00:26:11 | [diff] [blame] | 1238 | /* Make sure the number of columns in the source data matches the number |
| 1239 | ** of columns to be inserted into the table. |
| 1240 | */ |
drh | 6f6e60d | 2021-02-18 15:45:34 | [diff] [blame] | 1241 | assert( TF_HasHidden==COLFLAG_HIDDEN ); |
| 1242 | assert( TF_HasGenerated==COLFLAG_GENERATED ); |
| 1243 | assert( COLFLAG_NOINSERT==(COLFLAG_GENERATED|COLFLAG_HIDDEN) ); |
| 1244 | if( (pTab->tabFlags & (TF_HasGenerated|TF_HasHidden))!=0 ){ |
drh | c7e93f5 | 2021-02-18 00:26:11 | [diff] [blame] | 1245 | for(i=0; i<pTab->nCol; i++){ |
| 1246 | if( pTab->aCol[i].colFlags & COLFLAG_NOINSERT ) nHidden++; |
| 1247 | } |
| 1248 | } |
| 1249 | if( nColumn!=(pTab->nCol-nHidden) ){ |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 1250 | sqlite3ErrorMsg(pParse, |
drh | c7e93f5 | 2021-02-18 00:26:11 | [diff] [blame] | 1251 | "table %S has %d columns but %d values were supplied", |
drh | a979993 | 2021-03-19 13:00:28 | [diff] [blame] | 1252 | pTabList->a, pTab->nCol-nHidden, nColumn); |
drh | c7e93f5 | 2021-02-18 00:26:11 | [diff] [blame] | 1253 | goto insert_cleanup; |
| 1254 | } |
drh | cce7d17 | 2000-05-31 15:34:51 | [diff] [blame] | 1255 | } |
| 1256 | if( pColumn!=0 && nColumn!=pColumn->nId ){ |
| 1257 | sqlite3ErrorMsg(pParse, "%d values for %d columns", nColumn, pColumn->nId); |
| 1258 | goto insert_cleanup; |
| 1259 | } |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 1260 | |
drh | feeb139 | 2002-04-09 03:28:01 | [diff] [blame] | 1261 | /* Initialize the count of rows to be inserted |
| 1262 | */ |
drh | 7963691 | 2018-04-19 23:52:39 | [diff] [blame] | 1263 | if( (db->flags & SQLITE_CountRows)!=0 |
| 1264 | && !pParse->nested |
| 1265 | && !pParse->pTriggerTab |
drh | d086aa0 | 2021-01-29 21:31:59 | [diff] [blame] | 1266 | && !pParse->bReturning |
drh | 7963691 | 2018-04-19 23:52:39 | [diff] [blame] | 1267 | ){ |
drh | 6a288a3 | 2008-01-07 19:20:24 | [diff] [blame] | 1268 | regRowCount = ++pParse->nMem; |
| 1269 | sqlite3VdbeAddOp2(v, OP_Integer, 0, regRowCount); |
drh | feeb139 | 2002-04-09 03:28:01 | [diff] [blame] | 1270 | } |
| 1271 | |
danielk1977 | e448dc4 | 2008-01-02 11:50:51 | [diff] [blame] | 1272 | /* If this is not a view, open the table and and all indices */ |
| 1273 | if( !isView ){ |
drh | aa9b896 | 2008-01-08 02:57:55 | [diff] [blame] | 1274 | int nIdx; |
dan | fd261ec | 2015-10-22 20:54:33 | [diff] [blame] | 1275 | nIdx = sqlite3OpenTableAndIndices(pParse, pTab, OP_OpenWrite, 0, -1, 0, |
drh | 26198bb | 2013-10-31 11:15:09 | [diff] [blame] | 1276 | &iDataCur, &iIdxCur); |
drh | a7c3b93 | 2019-05-07 19:13:42 | [diff] [blame] | 1277 | aRegIdx = sqlite3DbMallocRawNN(db, sizeof(int)*(nIdx+2)); |
drh | aa9b896 | 2008-01-08 02:57:55 | [diff] [blame] | 1278 | if( aRegIdx==0 ){ |
| 1279 | goto insert_cleanup; |
| 1280 | } |
drh | 2c4dfc3 | 2016-11-10 14:24:04 | [diff] [blame] | 1281 | for(i=0, pIdx=pTab->pIndex; i<nIdx; pIdx=pIdx->pNext, i++){ |
| 1282 | assert( pIdx ); |
drh | aa9b896 | 2008-01-08 02:57:55 | [diff] [blame] | 1283 | aRegIdx[i] = ++pParse->nMem; |
drh | 2c4dfc3 | 2016-11-10 14:24:04 | [diff] [blame] | 1284 | pParse->nMem += pIdx->nColumn; |
drh | aa9b896 | 2008-01-08 02:57:55 | [diff] [blame] | 1285 | } |
drh | a7c3b93 | 2019-05-07 19:13:42 | [diff] [blame] | 1286 | aRegIdx[i] = ++pParse->nMem; /* Register to store the table record */ |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 | [diff] [blame] | 1287 | } |
drh | 788d55a | 2018-04-13 01:15:09 | [diff] [blame] | 1288 | #ifndef SQLITE_OMIT_UPSERT |
drh | 0b30a11 | 2018-04-13 21:55:22 | [diff] [blame] | 1289 | if( pUpsert ){ |
drh | 20b8632 | 2020-12-09 01:34:48 | [diff] [blame] | 1290 | Upsert *pNx; |
drh | b042d92 | 2019-01-04 23:39:37 | [diff] [blame] | 1291 | if( IsVirtual(pTab) ){ |
| 1292 | sqlite3ErrorMsg(pParse, "UPSERT not implemented for virtual table \"%s\"", |
| 1293 | pTab->zName); |
| 1294 | goto insert_cleanup; |
| 1295 | } |
drh | f38524d | 2021-08-02 16:41:57 | [diff] [blame] | 1296 | if( IsView(pTab) ){ |
drh | c6b24ab | 2019-12-06 01:23:38 | [diff] [blame] | 1297 | sqlite3ErrorMsg(pParse, "cannot UPSERT a view"); |
| 1298 | goto insert_cleanup; |
| 1299 | } |
dan | 9105fd5 | 2019-08-19 17:26:32 | [diff] [blame] | 1300 | if( sqlite3HasExplicitNulls(pParse, pUpsert->pUpsertTarget) ){ |
| 1301 | goto insert_cleanup; |
| 1302 | } |
drh | 788d55a | 2018-04-13 01:15:09 | [diff] [blame] | 1303 | pTabList->a[0].iCursor = iDataCur; |
drh | 20b8632 | 2020-12-09 01:34:48 | [diff] [blame] | 1304 | pNx = pUpsert; |
| 1305 | do{ |
| 1306 | pNx->pUpsertSrc = pTabList; |
| 1307 | pNx->regData = regData; |
| 1308 | pNx->iDataCur = iDataCur; |
| 1309 | pNx->iIdxCur = iIdxCur; |
| 1310 | if( pNx->pUpsertTarget ){ |
drh | 926fb60 | 2024-03-08 14:01:48 | [diff] [blame] | 1311 | if( sqlite3UpsertAnalyzeTarget(pParse, pTabList, pNx, pUpsert) ){ |
dan | 93eb906 | 2021-03-19 14:26:24 | [diff] [blame] | 1312 | goto insert_cleanup; |
| 1313 | } |
drh | 20b8632 | 2020-12-09 01:34:48 | [diff] [blame] | 1314 | } |
| 1315 | pNx = pNx->pNextUpsert; |
| 1316 | }while( pNx!=0 ); |
drh | 788d55a | 2018-04-13 01:15:09 | [diff] [blame] | 1317 | } |
| 1318 | #endif |
| 1319 | |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 | [diff] [blame] | 1320 | |
drh | e00ee6e | 2008-06-20 15:24:01 | [diff] [blame] | 1321 | /* This is the top of the main insertion loop */ |
drh | 142e30d | 2002-08-28 03:00:58 | [diff] [blame] | 1322 | if( useTempTable ){ |
drh | e00ee6e | 2008-06-20 15:24:01 | [diff] [blame] | 1323 | /* This block codes the top of loop only. The complete loop is the |
| 1324 | ** following pseudocode (template 4): |
| 1325 | ** |
drh | 81cf13e | 2014-02-07 18:27:53 | [diff] [blame] | 1326 | ** rewind temp table, if empty goto D |
drh | e00ee6e | 2008-06-20 15:24:01 | [diff] [blame] | 1327 | ** C: loop over rows of intermediate table |
| 1328 | ** transfer values form intermediate table into <table> |
| 1329 | ** end loop |
| 1330 | ** D: ... |
| 1331 | */ |
drh | 688852a | 2014-02-17 22:40:43 | [diff] [blame] | 1332 | addrInsTop = sqlite3VdbeAddOp1(v, OP_Rewind, srcTab); VdbeCoverage(v); |
drh | e00ee6e | 2008-06-20 15:24:01 | [diff] [blame] | 1333 | addrCont = sqlite3VdbeCurrentAddr(v); |
drh | 142e30d | 2002-08-28 03:00:58 | [diff] [blame] | 1334 | }else if( pSelect ){ |
drh | e00ee6e | 2008-06-20 15:24:01 | [diff] [blame] | 1335 | /* This block codes the top of loop only. The complete loop is the |
| 1336 | ** following pseudocode (template 3): |
| 1337 | ** |
drh | 81cf13e | 2014-02-07 18:27:53 | [diff] [blame] | 1338 | ** C: yield X, at EOF goto D |
drh | e00ee6e | 2008-06-20 15:24:01 | [diff] [blame] | 1339 | ** insert the select result into <table> from R..R+n |
| 1340 | ** goto C |
| 1341 | ** D: ... |
| 1342 | */ |
drh | 3aef2fb | 2020-01-02 17:46:02 | [diff] [blame] | 1343 | sqlite3VdbeReleaseRegisters(pParse, regData, pTab->nCol, 0, 0); |
drh | 81cf13e | 2014-02-07 18:27:53 | [diff] [blame] | 1344 | addrInsTop = addrCont = sqlite3VdbeAddOp1(v, OP_Yield, dest.iSDParm); |
drh | 688852a | 2014-02-17 22:40:43 | [diff] [blame] | 1345 | VdbeCoverage(v); |
drh | f5f1915 | 2019-10-21 01:04:11 | [diff] [blame] | 1346 | if( ipkColumn>=0 ){ |
| 1347 | /* tag-20191021-001: If the INTEGER PRIMARY KEY is being generated by the |
| 1348 | ** SELECT, go ahead and copy the value into the rowid slot now, so that |
| 1349 | ** the value does not get overwritten by a NULL at tag-20191021-002. */ |
| 1350 | sqlite3VdbeAddOp2(v, OP_Copy, regFromSelect+ipkColumn, regRowid); |
| 1351 | } |
drh | 5974a30 | 2000-06-07 14:42:26 | [diff] [blame] | 1352 | } |
drh | 1ccde15 | 2000-06-17 13:12:39 | [diff] [blame] | 1353 | |
drh | f5f1915 | 2019-10-21 01:04:11 | [diff] [blame] | 1354 | /* Compute data for ordinary columns of the new entry. Values |
| 1355 | ** are written in storage order into registers starting with regData. |
| 1356 | ** Only ordinary columns are computed in this loop. The rowid |
| 1357 | ** (if there is one) is computed later and generated columns are |
| 1358 | ** computed after the rowid since they might depend on the value |
| 1359 | ** of the rowid. |
| 1360 | */ |
| 1361 | nHidden = 0; |
| 1362 | iRegStore = regData; assert( regData==regRowid+1 ); |
| 1363 | for(i=0; i<pTab->nCol; i++, iRegStore++){ |
| 1364 | int k; |
| 1365 | u32 colFlags; |
| 1366 | assert( i>=nHidden ); |
| 1367 | if( i==pTab->iPKey ){ |
| 1368 | /* tag-20191021-002: References to the INTEGER PRIMARY KEY are filled |
| 1369 | ** using the rowid. So put a NULL in the IPK slot of the record to avoid |
| 1370 | ** using excess space. The file format definition requires this extra |
| 1371 | ** NULL - we cannot optimize further by skipping the column completely */ |
| 1372 | sqlite3VdbeAddOp1(v, OP_SoftNull, iRegStore); |
| 1373 | continue; |
| 1374 | } |
| 1375 | if( ((colFlags = pTab->aCol[i].colFlags) & COLFLAG_NOINSERT)!=0 ){ |
| 1376 | nHidden++; |
| 1377 | if( (colFlags & COLFLAG_VIRTUAL)!=0 ){ |
| 1378 | /* Virtual columns do not participate in OP_MakeRecord. So back up |
| 1379 | ** iRegStore by one slot to compensate for the iRegStore++ in the |
| 1380 | ** outer for() loop */ |
| 1381 | iRegStore--; |
| 1382 | continue; |
| 1383 | }else if( (colFlags & COLFLAG_STORED)!=0 ){ |
| 1384 | /* Stored columns are computed later. But if there are BEFORE |
| 1385 | ** triggers, the slots used for stored columns will be OP_Copy-ed |
| 1386 | ** to a second block of registers, so the register needs to be |
| 1387 | ** initialized to NULL to avoid an uninitialized register read */ |
| 1388 | if( tmask & TRIGGER_BEFORE ){ |
| 1389 | sqlite3VdbeAddOp1(v, OP_SoftNull, iRegStore); |
| 1390 | } |
| 1391 | continue; |
| 1392 | }else if( pColumn==0 ){ |
| 1393 | /* Hidden columns that are not explicitly named in the INSERT |
drh | 65552a7 | 2025-02-06 17:10:38 | [diff] [blame] | 1394 | ** get their default value */ |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 1395 | sqlite3ExprCodeFactorable(pParse, |
drh | 79cf2b7 | 2021-07-31 20:30:41 | [diff] [blame] | 1396 | sqlite3ColumnExpr(pTab, &pTab->aCol[i]), |
| 1397 | iRegStore); |
drh | f5f1915 | 2019-10-21 01:04:11 | [diff] [blame] | 1398 | continue; |
| 1399 | } |
| 1400 | } |
| 1401 | if( pColumn ){ |
drh | 8b62a82 | 2025-01-28 12:50:17 | [diff] [blame] | 1402 | j = aTabColMap[i]; |
| 1403 | assert( j>=0 && j<=pColumn->nId ); |
| 1404 | if( j==0 ){ |
drh | f5f1915 | 2019-10-21 01:04:11 | [diff] [blame] | 1405 | /* A column not named in the insert column list gets its |
| 1406 | ** default value */ |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 1407 | sqlite3ExprCodeFactorable(pParse, |
drh | 79cf2b7 | 2021-07-31 20:30:41 | [diff] [blame] | 1408 | sqlite3ColumnExpr(pTab, &pTab->aCol[i]), |
| 1409 | iRegStore); |
drh | f5f1915 | 2019-10-21 01:04:11 | [diff] [blame] | 1410 | continue; |
| 1411 | } |
drh | 8b62a82 | 2025-01-28 12:50:17 | [diff] [blame] | 1412 | k = j - 1; |
drh | f5f1915 | 2019-10-21 01:04:11 | [diff] [blame] | 1413 | }else if( nColumn==0 ){ |
| 1414 | /* This is INSERT INTO ... DEFAULT VALUES. Load the default value. */ |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 1415 | sqlite3ExprCodeFactorable(pParse, |
drh | 79cf2b7 | 2021-07-31 20:30:41 | [diff] [blame] | 1416 | sqlite3ColumnExpr(pTab, &pTab->aCol[i]), |
| 1417 | iRegStore); |
drh | f5f1915 | 2019-10-21 01:04:11 | [diff] [blame] | 1418 | continue; |
| 1419 | }else{ |
| 1420 | k = i - nHidden; |
| 1421 | } |
| 1422 | |
| 1423 | if( useTempTable ){ |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 1424 | sqlite3VdbeAddOp3(v, OP_Column, srcTab, k, iRegStore); |
drh | f5f1915 | 2019-10-21 01:04:11 | [diff] [blame] | 1425 | }else if( pSelect ){ |
| 1426 | if( regFromSelect!=regData ){ |
| 1427 | sqlite3VdbeAddOp2(v, OP_SCopy, regFromSelect+k, iRegStore); |
| 1428 | } |
| 1429 | }else{ |
drh | 2d2e528 | 2022-07-25 21:37:13 | [diff] [blame] | 1430 | Expr *pX = pList->a[k].pExpr; |
| 1431 | int y = sqlite3ExprCodeTarget(pParse, pX, iRegStore); |
| 1432 | if( y!=iRegStore ){ |
| 1433 | sqlite3VdbeAddOp2(v, |
| 1434 | ExprHasProperty(pX, EP_Subquery) ? OP_Copy : OP_SCopy, y, iRegStore); |
| 1435 | } |
drh | f5f1915 | 2019-10-21 01:04:11 | [diff] [blame] | 1436 | } |
| 1437 | } |
| 1438 | |
| 1439 | |
drh | 5cf590c | 2003-04-24 01:45:04 | [diff] [blame] | 1440 | /* Run the BEFORE and INSTEAD OF triggers, if there are any |
drh | 70ce3f0 | 2003-04-15 19:22:22 | [diff] [blame] | 1441 | */ |
drh | ec4ccdb | 2018-12-29 02:26:59 | [diff] [blame] | 1442 | endOfLoop = sqlite3VdbeMakeLabel(pParse); |
danielk1977 | 2f886d1 | 2009-02-28 10:47:41 | [diff] [blame] | 1443 | if( tmask & TRIGGER_BEFORE ){ |
dan | 76d462e | 2009-08-30 11:42:51 | [diff] [blame] | 1444 | int regCols = sqlite3GetTempRange(pParse, pTab->nCol+1); |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 | [diff] [blame] | 1445 | |
drh | 70ce3f0 | 2003-04-15 19:22:22 | [diff] [blame] | 1446 | /* build the NEW.* reference row. Note that if there is an INTEGER |
| 1447 | ** PRIMARY KEY into which a NULL is being inserted, that NULL will be |
| 1448 | ** translated into a unique ID for the row. But on a BEFORE trigger, |
| 1449 | ** we do not know what the unique ID will be (because the insert has |
| 1450 | ** not happened yet) so we substitute a rowid of -1 |
| 1451 | */ |
drh | d82b502 | 2013-10-26 00:58:34 | [diff] [blame] | 1452 | if( ipkColumn<0 ){ |
dan | 76d462e | 2009-08-30 11:42:51 | [diff] [blame] | 1453 | sqlite3VdbeAddOp2(v, OP_Integer, -1, regCols); |
drh | 70ce3f0 | 2003-04-15 19:22:22 | [diff] [blame] | 1454 | }else{ |
drh | 728e0f9 | 2015-10-10 14:41:28 | [diff] [blame] | 1455 | int addr1; |
drh | ec95c44 | 2013-10-23 01:57:32 | [diff] [blame] | 1456 | assert( !withoutRowid ); |
drh | 7fe4590 | 2009-05-01 02:08:04 | [diff] [blame] | 1457 | if( useTempTable ){ |
drh | d82b502 | 2013-10-26 00:58:34 | [diff] [blame] | 1458 | sqlite3VdbeAddOp3(v, OP_Column, srcTab, ipkColumn, regCols); |
drh | 7fe4590 | 2009-05-01 02:08:04 | [diff] [blame] | 1459 | }else{ |
| 1460 | assert( pSelect==0 ); /* Otherwise useTempTable is true */ |
drh | d82b502 | 2013-10-26 00:58:34 | [diff] [blame] | 1461 | sqlite3ExprCode(pParse, pList->a[ipkColumn].pExpr, regCols); |
drh | 7fe4590 | 2009-05-01 02:08:04 | [diff] [blame] | 1462 | } |
drh | 728e0f9 | 2015-10-10 14:41:28 | [diff] [blame] | 1463 | addr1 = sqlite3VdbeAddOp1(v, OP_NotNull, regCols); VdbeCoverage(v); |
dan | 76d462e | 2009-08-30 11:42:51 | [diff] [blame] | 1464 | sqlite3VdbeAddOp2(v, OP_Integer, -1, regCols); |
drh | 728e0f9 | 2015-10-10 14:41:28 | [diff] [blame] | 1465 | sqlite3VdbeJumpHere(v, addr1); |
drh | 688852a | 2014-02-17 22:40:43 | [diff] [blame] | 1466 | sqlite3VdbeAddOp1(v, OP_MustBeInt, regCols); VdbeCoverage(v); |
drh | 70ce3f0 | 2003-04-15 19:22:22 | [diff] [blame] | 1467 | } |
| 1468 | |
drh | f5f1915 | 2019-10-21 01:04:11 | [diff] [blame] | 1469 | /* Copy the new data already generated. */ |
drh | 3cbf38c | 2023-03-28 11:18:04 | [diff] [blame] | 1470 | assert( pTab->nNVCol>0 || pParse->nErr>0 ); |
drh | f5f1915 | 2019-10-21 01:04:11 | [diff] [blame] | 1471 | sqlite3VdbeAddOp3(v, OP_Copy, regRowid+1, regCols+1, pTab->nNVCol-1); |
| 1472 | |
| 1473 | #ifndef SQLITE_OMIT_GENERATED_COLUMNS |
| 1474 | /* Compute the new value for generated columns after all other |
| 1475 | ** columns have already been computed. This must be done after |
| 1476 | ** computing the ROWID in case one of the generated columns |
| 1477 | ** refers to the ROWID. */ |
drh | 427b96a | 2019-10-22 13:01:24 | [diff] [blame] | 1478 | if( pTab->tabFlags & TF_HasGenerated ){ |
| 1479 | testcase( pTab->tabFlags & TF_HasVirtual ); |
| 1480 | testcase( pTab->tabFlags & TF_HasStored ); |
drh | f5f1915 | 2019-10-21 01:04:11 | [diff] [blame] | 1481 | sqlite3ComputeGeneratedColumns(pParse, regCols+1, pTab); |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 | [diff] [blame] | 1482 | } |
drh | f5f1915 | 2019-10-21 01:04:11 | [diff] [blame] | 1483 | #endif |
danielk1977 | a37cdde | 2004-05-16 11:15:36 | [diff] [blame] | 1484 | |
| 1485 | /* If this is an INSERT on a view with an INSTEAD OF INSERT trigger, |
| 1486 | ** do not attempt any conversions before assembling the record. |
| 1487 | ** If this is a real table, attempt conversions as required by the |
| 1488 | ** table column affinities. |
| 1489 | */ |
| 1490 | if( !isView ){ |
drh | 57bf4a8 | 2014-02-17 14:59:22 | [diff] [blame] | 1491 | sqlite3TableAffinity(v, pTab, regCols+1); |
danielk1977 | a37cdde | 2004-05-16 11:15:36 | [diff] [blame] | 1492 | } |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 | [diff] [blame] | 1493 | |
drh | 5cf590c | 2003-04-24 01:45:04 | [diff] [blame] | 1494 | /* Fire BEFORE or INSTEAD OF triggers */ |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 1495 | sqlite3CodeRowTrigger(pParse, pTrigger, TK_INSERT, 0, TRIGGER_BEFORE, |
dan | 94d7f50 | 2009-09-24 09:05:49 | [diff] [blame] | 1496 | pTab, regCols-pTab->nCol-1, onError, endOfLoop); |
dan | 165921a | 2009-08-28 18:53:45 | [diff] [blame] | 1497 | |
dan | 76d462e | 2009-08-30 11:42:51 | [diff] [blame] | 1498 | sqlite3ReleaseTempRange(pParse, regCols, pTab->nCol+1); |
drh | 70ce3f0 | 2003-04-15 19:22:22 | [diff] [blame] | 1499 | } |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 | [diff] [blame] | 1500 | |
drh | 5cf590c | 2003-04-24 01:45:04 | [diff] [blame] | 1501 | if( !isView ){ |
drh | 4cbdda9 | 2006-06-14 19:00:20 | [diff] [blame] | 1502 | if( IsVirtual(pTab) ){ |
danielk1977 | 287fb61 | 2008-01-04 19:10:28 | [diff] [blame] | 1503 | /* The row that the VUpdate opcode will delete: none */ |
drh | 6a288a3 | 2008-01-07 19:20:24 | [diff] [blame] | 1504 | sqlite3VdbeAddOp2(v, OP_Null, 0, regIns); |
drh | 4cbdda9 | 2006-06-14 19:00:20 | [diff] [blame] | 1505 | } |
drh | d82b502 | 2013-10-26 00:58:34 | [diff] [blame] | 1506 | if( ipkColumn>=0 ){ |
drh | f5f1915 | 2019-10-21 01:04:11 | [diff] [blame] | 1507 | /* Compute the new rowid */ |
drh | 142e30d | 2002-08-28 03:00:58 | [diff] [blame] | 1508 | if( useTempTable ){ |
drh | d82b502 | 2013-10-26 00:58:34 | [diff] [blame] | 1509 | sqlite3VdbeAddOp3(v, OP_Column, srcTab, ipkColumn, regRowid); |
drh | 142e30d | 2002-08-28 03:00:58 | [diff] [blame] | 1510 | }else if( pSelect ){ |
drh | f5f1915 | 2019-10-21 01:04:11 | [diff] [blame] | 1511 | /* Rowid already initialized at tag-20191021-001 */ |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 | [diff] [blame] | 1512 | }else{ |
drh | 04fcef0 | 2019-01-17 04:40:04 | [diff] [blame] | 1513 | Expr *pIpk = pList->a[ipkColumn].pExpr; |
| 1514 | if( pIpk->op==TK_NULL && !IsVirtual(pTab) ){ |
| 1515 | sqlite3VdbeAddOp3(v, OP_NewRowid, iDataCur, regRowid, regAutoinc); |
drh | e4d9081 | 2007-03-29 05:51:49 | [diff] [blame] | 1516 | appendFlag = 1; |
drh | 04fcef0 | 2019-01-17 04:40:04 | [diff] [blame] | 1517 | }else{ |
| 1518 | sqlite3ExprCode(pParse, pList->a[ipkColumn].pExpr, regRowid); |
drh | e4d9081 | 2007-03-29 05:51:49 | [diff] [blame] | 1519 | } |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 | [diff] [blame] | 1520 | } |
drh | f0863fe | 2005-06-12 21:35:51 | [diff] [blame] | 1521 | /* If the PRIMARY KEY expression is NULL, then use OP_NewRowid |
drh | 27a3278 | 2002-06-19 20:32:43 | [diff] [blame] | 1522 | ** to generate a unique primary key value. |
| 1523 | */ |
drh | e4d9081 | 2007-03-29 05:51:49 | [diff] [blame] | 1524 | if( !appendFlag ){ |
drh | 728e0f9 | 2015-10-10 14:41:28 | [diff] [blame] | 1525 | int addr1; |
danielk1977 | bb50e7a | 2008-07-04 10:56:07 | [diff] [blame] | 1526 | if( !IsVirtual(pTab) ){ |
drh | 728e0f9 | 2015-10-10 14:41:28 | [diff] [blame] | 1527 | addr1 = sqlite3VdbeAddOp1(v, OP_NotNull, regRowid); VdbeCoverage(v); |
drh | 26198bb | 2013-10-31 11:15:09 | [diff] [blame] | 1528 | sqlite3VdbeAddOp3(v, OP_NewRowid, iDataCur, regRowid, regAutoinc); |
drh | 728e0f9 | 2015-10-10 14:41:28 | [diff] [blame] | 1529 | sqlite3VdbeJumpHere(v, addr1); |
danielk1977 | bb50e7a | 2008-07-04 10:56:07 | [diff] [blame] | 1530 | }else{ |
drh | 728e0f9 | 2015-10-10 14:41:28 | [diff] [blame] | 1531 | addr1 = sqlite3VdbeCurrentAddr(v); |
| 1532 | sqlite3VdbeAddOp2(v, OP_IsNull, regRowid, addr1+2); VdbeCoverage(v); |
danielk1977 | bb50e7a | 2008-07-04 10:56:07 | [diff] [blame] | 1533 | } |
drh | 688852a | 2014-02-17 22:40:43 | [diff] [blame] | 1534 | sqlite3VdbeAddOp1(v, OP_MustBeInt, regRowid); VdbeCoverage(v); |
drh | e4d9081 | 2007-03-29 05:51:49 | [diff] [blame] | 1535 | } |
drh | ec95c44 | 2013-10-23 01:57:32 | [diff] [blame] | 1536 | }else if( IsVirtual(pTab) || withoutRowid ){ |
drh | 6a288a3 | 2008-01-07 19:20:24 | [diff] [blame] | 1537 | sqlite3VdbeAddOp2(v, OP_Null, 0, regRowid); |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 | [diff] [blame] | 1538 | }else{ |
drh | 26198bb | 2013-10-31 11:15:09 | [diff] [blame] | 1539 | sqlite3VdbeAddOp3(v, OP_NewRowid, iDataCur, regRowid, regAutoinc); |
drh | e4d9081 | 2007-03-29 05:51:49 | [diff] [blame] | 1540 | appendFlag = 1; |
drh | 4a32431 | 2001-12-21 14:30:42 | [diff] [blame] | 1541 | } |
drh | 6a288a3 | 2008-01-07 19:20:24 | [diff] [blame] | 1542 | autoIncStep(pParse, regAutoinc, regRowid); |
drh | 4a32431 | 2001-12-21 14:30:42 | [diff] [blame] | 1543 | |
drh | c143114 | 2019-10-17 17:54:05 | [diff] [blame] | 1544 | #ifndef SQLITE_OMIT_GENERATED_COLUMNS |
drh | dd6cc9b | 2019-10-19 18:47:27 | [diff] [blame] | 1545 | /* Compute the new value for generated columns after all other |
drh | f5f1915 | 2019-10-21 01:04:11 | [diff] [blame] | 1546 | ** columns have already been computed. This must be done after |
| 1547 | ** computing the ROWID in case one of the generated columns |
drh | b5f6243 | 2019-12-10 02:48:41 | [diff] [blame] | 1548 | ** is derived from the INTEGER PRIMARY KEY. */ |
drh | 427b96a | 2019-10-22 13:01:24 | [diff] [blame] | 1549 | if( pTab->tabFlags & TF_HasGenerated ){ |
drh | dd6cc9b | 2019-10-19 18:47:27 | [diff] [blame] | 1550 | sqlite3ComputeGeneratedColumns(pParse, regRowid+1, pTab); |
drh | bed8690 | 2000-06-02 13:27:59 | [diff] [blame] | 1551 | } |
drh | c143114 | 2019-10-17 17:54:05 | [diff] [blame] | 1552 | #endif |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 | [diff] [blame] | 1553 | |
| 1554 | /* Generate code to check constraints and generate index keys and |
danielk1977 | f29ce55 | 2002-05-19 23:43:12 | [diff] [blame] | 1555 | ** do the insertion. |
| 1556 | */ |
drh | 4cbdda9 | 2006-06-14 19:00:20 | [diff] [blame] | 1557 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
| 1558 | if( IsVirtual(pTab) ){ |
danielk1977 | 595a523 | 2009-07-24 17:58:53 | [diff] [blame] | 1559 | const char *pVTab = (const char *)sqlite3GetVTable(db, pTab); |
drh | 4f3dd15 | 2008-04-28 18:46:43 | [diff] [blame] | 1560 | sqlite3VtabMakeWritable(pParse, pTab); |
danielk1977 | 595a523 | 2009-07-24 17:58:53 | [diff] [blame] | 1561 | sqlite3VdbeAddOp4(v, OP_VUpdate, 1, pTab->nCol+2, regIns, pVTab, P4_VTAB); |
dan | b061d05 | 2011-04-25 18:49:57 | [diff] [blame] | 1562 | sqlite3VdbeChangeP5(v, onError==OE_Default ? OE_Abort : onError); |
dan | e0af83a | 2009-09-08 19:15:01 | [diff] [blame] | 1563 | sqlite3MayAbort(pParse); |
drh | 4cbdda9 | 2006-06-14 19:00:20 | [diff] [blame] | 1564 | }else |
| 1565 | #endif |
| 1566 | { |
dan | 11fbee2 | 2021-04-06 16:42:05 | [diff] [blame] | 1567 | int isReplace = 0;/* Set to true if constraints may cause a replace */ |
dan | 3b908d4 | 2016-11-08 19:22:32 | [diff] [blame] | 1568 | int bUseSeek; /* True to use OPFLAG_SEEKRESULT */ |
drh | f8ffb27 | 2013-11-01 17:08:56 | [diff] [blame] | 1569 | sqlite3GenerateConstraintChecks(pParse, pTab, aRegIdx, iDataCur, iIdxCur, |
drh | 788d55a | 2018-04-13 01:15:09 | [diff] [blame] | 1570 | regIns, 0, ipkColumn>=0, onError, endOfLoop, &isReplace, 0, pUpsert |
drh | 04adf41 | 2008-01-08 18:57:50 | [diff] [blame] | 1571 | ); |
drh | 509a630 | 2022-07-25 23:01:41 | [diff] [blame] | 1572 | if( db->flags & SQLITE_ForeignKeys ){ |
| 1573 | sqlite3FkCheck(pParse, pTab, 0, regIns, 0, 0); |
| 1574 | } |
dan | 3b908d4 | 2016-11-08 19:22:32 | [diff] [blame] | 1575 | |
| 1576 | /* Set the OPFLAG_USESEEKRESULT flag if either (a) there are no REPLACE |
| 1577 | ** constraints or (b) there are no triggers and this table is not a |
| 1578 | ** parent table in a foreign key constraint. It is safe to set the |
| 1579 | ** flag in the second case as if any REPLACE constraint is hit, an |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 1580 | ** OP_Delete or OP_IdxDelete instruction will be executed on each |
dan | 3b908d4 | 2016-11-08 19:22:32 | [diff] [blame] | 1581 | ** cursor that is disturbed. And these instructions both clear the |
| 1582 | ** VdbeCursor.seekResult variable, disabling the OPFLAG_USESEEKRESULT |
| 1583 | ** functionality. */ |
drh | 06baba5 | 2019-10-24 19:35:26 | [diff] [blame] | 1584 | bUseSeek = (isReplace==0 || !sqlite3VdbeHasSubProgram(v)); |
drh | 26198bb | 2013-10-31 11:15:09 | [diff] [blame] | 1585 | sqlite3CompleteInsertion(pParse, pTab, iDataCur, iIdxCur, |
dan | 3b908d4 | 2016-11-08 19:22:32 | [diff] [blame] | 1586 | regIns, aRegIdx, 0, appendFlag, bUseSeek |
| 1587 | ); |
drh | 4cbdda9 | 2006-06-14 19:00:20 | [diff] [blame] | 1588 | } |
drh | 6e5020e | 2021-04-07 15:45:01 | [diff] [blame] | 1589 | #ifdef SQLITE_ALLOW_ROWID_IN_VIEW |
dan | 2a1aeaa | 2021-04-06 13:53:10 | [diff] [blame] | 1590 | }else if( pParse->bReturning ){ |
| 1591 | /* If there is a RETURNING clause, populate the rowid register with |
| 1592 | ** constant value -1, in case one or more of the returned expressions |
| 1593 | ** refer to the "rowid" of the view. */ |
| 1594 | sqlite3VdbeAddOp2(v, OP_Integer, -1, regRowid); |
drh | 6e5020e | 2021-04-07 15:45:01 | [diff] [blame] | 1595 | #endif |
drh | 5cf590c | 2003-04-24 01:45:04 | [diff] [blame] | 1596 | } |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 | [diff] [blame] | 1597 | |
drh | 5cf590c | 2003-04-24 01:45:04 | [diff] [blame] | 1598 | /* Update the count of rows that are inserted |
| 1599 | */ |
drh | 7963691 | 2018-04-19 23:52:39 | [diff] [blame] | 1600 | if( regRowCount ){ |
drh | 6a288a3 | 2008-01-07 19:20:24 | [diff] [blame] | 1601 | sqlite3VdbeAddOp2(v, OP_AddImm, regRowCount, 1); |
drh | 5974a30 | 2000-06-07 14:42:26 | [diff] [blame] | 1602 | } |
drh | 1ccde15 | 2000-06-17 13:12:39 | [diff] [blame] | 1603 | |
danielk1977 | 2f886d1 | 2009-02-28 10:47:41 | [diff] [blame] | 1604 | if( pTrigger ){ |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 | [diff] [blame] | 1605 | /* Code AFTER triggers */ |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 1606 | sqlite3CodeRowTrigger(pParse, pTrigger, TK_INSERT, 0, TRIGGER_AFTER, |
dan | 94d7f50 | 2009-09-24 09:05:49 | [diff] [blame] | 1607 | pTab, regData-2-pTab->nCol, onError, endOfLoop); |
drh | 1bee3d7 | 2001-10-15 00:44:35 | [diff] [blame] | 1608 | } |
| 1609 | |
drh | e00ee6e | 2008-06-20 15:24:01 | [diff] [blame] | 1610 | /* The bottom of the main insertion loop, if the data source |
| 1611 | ** is a SELECT statement. |
danielk1977 | f29ce55 | 2002-05-19 23:43:12 | [diff] [blame] | 1612 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 | [diff] [blame] | 1613 | sqlite3VdbeResolveLabel(v, endOfLoop); |
drh | 142e30d | 2002-08-28 03:00:58 | [diff] [blame] | 1614 | if( useTempTable ){ |
drh | 688852a | 2014-02-17 22:40:43 | [diff] [blame] | 1615 | sqlite3VdbeAddOp2(v, OP_Next, srcTab, addrCont); VdbeCoverage(v); |
drh | e00ee6e | 2008-06-20 15:24:01 | [diff] [blame] | 1616 | sqlite3VdbeJumpHere(v, addrInsTop); |
drh | 2eb9537 | 2008-06-06 15:04:36 | [diff] [blame] | 1617 | sqlite3VdbeAddOp1(v, OP_Close, srcTab); |
drh | 142e30d | 2002-08-28 03:00:58 | [diff] [blame] | 1618 | }else if( pSelect ){ |
drh | 076e85f | 2015-09-03 13:46:12 | [diff] [blame] | 1619 | sqlite3VdbeGoto(v, addrCont); |
drh | d9670ab | 2019-12-28 01:52:46 | [diff] [blame] | 1620 | #ifdef SQLITE_DEBUG |
| 1621 | /* If we are jumping back to an OP_Yield that is preceded by an |
| 1622 | ** OP_ReleaseReg, set the p5 flag on the OP_Goto so that the |
| 1623 | ** OP_ReleaseReg will be included in the loop. */ |
| 1624 | if( sqlite3VdbeGetOp(v, addrCont-1)->opcode==OP_ReleaseReg ){ |
| 1625 | assert( sqlite3VdbeGetOp(v, addrCont)->opcode==OP_Yield ); |
| 1626 | sqlite3VdbeChangeP5(v, 1); |
| 1627 | } |
| 1628 | #endif |
drh | e00ee6e | 2008-06-20 15:24:01 | [diff] [blame] | 1629 | sqlite3VdbeJumpHere(v, addrInsTop); |
drh | 6b56344 | 2001-11-07 16:48:26 | [diff] [blame] | 1630 | } |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 | [diff] [blame] | 1631 | |
mistachkin | d6665c5 | 2021-01-18 19:28:56 | [diff] [blame] | 1632 | #ifndef SQLITE_OMIT_XFER_OPT |
drh | 0b9f50d | 2009-06-23 20:28:53 | [diff] [blame] | 1633 | insert_end: |
mistachkin | d6665c5 | 2021-01-18 19:28:56 | [diff] [blame] | 1634 | #endif /* SQLITE_OMIT_XFER_OPT */ |
drh | f338814 | 2004-11-13 03:48:06 | [diff] [blame] | 1635 | /* Update the sqlite_sequence table by storing the content of the |
drh | 0b9f50d | 2009-06-23 20:28:53 | [diff] [blame] | 1636 | ** maximum rowid counter values recorded while inserting into |
| 1637 | ** autoincrement tables. |
drh | 2958a4e | 2004-11-12 03:56:15 | [diff] [blame] | 1638 | */ |
dan | 165921a | 2009-08-28 18:53:45 | [diff] [blame] | 1639 | if( pParse->nested==0 && pParse->pTriggerTab==0 ){ |
drh | 0b9f50d | 2009-06-23 20:28:53 | [diff] [blame] | 1640 | sqlite3AutoincrementEnd(pParse); |
| 1641 | } |
drh | 2958a4e | 2004-11-12 03:56:15 | [diff] [blame] | 1642 | |
drh | 1bee3d7 | 2001-10-15 00:44:35 | [diff] [blame] | 1643 | /* |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 1644 | ** Return the number of rows inserted. If this routine is |
danielk1977 | e7de6f2 | 2004-11-05 06:02:06 | [diff] [blame] | 1645 | ** generating code because of a call to sqlite3NestedParse(), do not |
| 1646 | ** invoke the callback function. |
drh | 1bee3d7 | 2001-10-15 00:44:35 | [diff] [blame] | 1647 | */ |
drh | 7963691 | 2018-04-19 23:52:39 | [diff] [blame] | 1648 | if( regRowCount ){ |
drh | 3b26b2b | 2021-12-01 19:17:14 | [diff] [blame] | 1649 | sqlite3CodeChangeCount(v, regRowCount, "rows inserted"); |
drh | 1bee3d7 | 2001-10-15 00:44:35 | [diff] [blame] | 1650 | } |
drh | cce7d17 | 2000-05-31 15:34:51 | [diff] [blame] | 1651 | |
| 1652 | insert_cleanup: |
drh | 633e6d5 | 2008-07-28 19:34:53 | [diff] [blame] | 1653 | sqlite3SrcListDelete(db, pTabList); |
| 1654 | sqlite3ExprListDelete(db, pList); |
drh | 46d2e5c | 2018-04-12 13:15:43 | [diff] [blame] | 1655 | sqlite3UpsertDelete(db, pUpsert); |
drh | 633e6d5 | 2008-07-28 19:34:53 | [diff] [blame] | 1656 | sqlite3SelectDelete(db, pSelect); |
drh | 8b62a82 | 2025-01-28 12:50:17 | [diff] [blame] | 1657 | if( pColumn ){ |
| 1658 | sqlite3IdListDelete(db, pColumn); |
| 1659 | sqlite3DbFree(db, aTabColMap); |
| 1660 | } |
drh | 41ce47c | 2022-08-22 02:00:26 | [diff] [blame] | 1661 | if( aRegIdx ) sqlite3DbNNFreeNN(db, aRegIdx); |
drh | cce7d17 | 2000-05-31 15:34:51 | [diff] [blame] | 1662 | } |
drh | 9cfcf5d | 2002-01-29 18:41:24 | [diff] [blame] | 1663 | |
dan | 75cbd98 | 2009-09-21 16:06:03 | [diff] [blame] | 1664 | /* Make sure "isView" and other macros defined above are undefined. Otherwise |
peter.d.reid | 60ec914 | 2014-09-06 16:39:46 | [diff] [blame] | 1665 | ** they may interfere with compilation of other functions in this file |
dan | 75cbd98 | 2009-09-21 16:06:03 | [diff] [blame] | 1666 | ** (or in another file, if this file becomes part of the amalgamation). */ |
| 1667 | #ifdef isView |
| 1668 | #undef isView |
| 1669 | #endif |
| 1670 | #ifdef pTrigger |
| 1671 | #undef pTrigger |
| 1672 | #endif |
| 1673 | #ifdef tmask |
| 1674 | #undef tmask |
| 1675 | #endif |
| 1676 | |
drh | 9cfcf5d | 2002-01-29 18:41:24 | [diff] [blame] | 1677 | /* |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 1678 | ** Meanings of bits in of pWalker->eCode for |
drh | e9816d8 | 2018-09-15 21:38:48 | [diff] [blame] | 1679 | ** sqlite3ExprReferencesUpdatedColumn() |
drh | 98bfa16 | 2016-02-10 18:24:05 | [diff] [blame] | 1680 | */ |
| 1681 | #define CKCNSTRNT_COLUMN 0x01 /* CHECK constraint uses a changing column */ |
| 1682 | #define CKCNSTRNT_ROWID 0x02 /* CHECK constraint references the ROWID */ |
| 1683 | |
drh | e9816d8 | 2018-09-15 21:38:48 | [diff] [blame] | 1684 | /* This is the Walker callback from sqlite3ExprReferencesUpdatedColumn(). |
| 1685 | * Set bit 0x01 of pWalker->eCode if pWalker->eCode to 0 and if this |
| 1686 | ** expression node references any of the |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 1687 | ** columns that are being modified by an UPDATE statement. |
drh | 2a0b527 | 2016-02-10 16:52:24 | [diff] [blame] | 1688 | */ |
| 1689 | static int checkConstraintExprNode(Walker *pWalker, Expr *pExpr){ |
drh | 98bfa16 | 2016-02-10 18:24:05 | [diff] [blame] | 1690 | if( pExpr->op==TK_COLUMN ){ |
| 1691 | assert( pExpr->iColumn>=0 || pExpr->iColumn==-1 ); |
| 1692 | if( pExpr->iColumn>=0 ){ |
| 1693 | if( pWalker->u.aiCol[pExpr->iColumn]>=0 ){ |
| 1694 | pWalker->eCode |= CKCNSTRNT_COLUMN; |
| 1695 | } |
| 1696 | }else{ |
| 1697 | pWalker->eCode |= CKCNSTRNT_ROWID; |
| 1698 | } |
drh | 2a0b527 | 2016-02-10 16:52:24 | [diff] [blame] | 1699 | } |
| 1700 | return WRC_Continue; |
| 1701 | } |
| 1702 | |
| 1703 | /* |
| 1704 | ** pExpr is a CHECK constraint on a row that is being UPDATE-ed. The |
| 1705 | ** only columns that are modified by the UPDATE are those for which |
drh | 98bfa16 | 2016-02-10 18:24:05 | [diff] [blame] | 1706 | ** aiChng[i]>=0, and also the ROWID is modified if chngRowid is true. |
| 1707 | ** |
drh | e9816d8 | 2018-09-15 21:38:48 | [diff] [blame] | 1708 | ** Return true if CHECK constraint pExpr uses any of the |
drh | 98bfa16 | 2016-02-10 18:24:05 | [diff] [blame] | 1709 | ** changing columns (or the rowid if it is changing). In other words, |
drh | e9816d8 | 2018-09-15 21:38:48 | [diff] [blame] | 1710 | ** return true if this CHECK constraint must be validated for |
drh | 98bfa16 | 2016-02-10 18:24:05 | [diff] [blame] | 1711 | ** the new row in the UPDATE statement. |
drh | e9816d8 | 2018-09-15 21:38:48 | [diff] [blame] | 1712 | ** |
| 1713 | ** 2018-09-15: pExpr might also be an expression for an index-on-expressions. |
| 1714 | ** The operation of this routine is the same - return true if an only if |
| 1715 | ** the expression uses one or more of columns identified by the second and |
| 1716 | ** third arguments. |
drh | 2a0b527 | 2016-02-10 16:52:24 | [diff] [blame] | 1717 | */ |
drh | e9816d8 | 2018-09-15 21:38:48 | [diff] [blame] | 1718 | int sqlite3ExprReferencesUpdatedColumn( |
| 1719 | Expr *pExpr, /* The expression to be checked */ |
| 1720 | int *aiChng, /* aiChng[x]>=0 if column x changed by the UPDATE */ |
| 1721 | int chngRowid /* True if UPDATE changes the rowid */ |
| 1722 | ){ |
drh | 2a0b527 | 2016-02-10 16:52:24 | [diff] [blame] | 1723 | Walker w; |
| 1724 | memset(&w, 0, sizeof(w)); |
drh | 98bfa16 | 2016-02-10 18:24:05 | [diff] [blame] | 1725 | w.eCode = 0; |
drh | 2a0b527 | 2016-02-10 16:52:24 | [diff] [blame] | 1726 | w.xExprCallback = checkConstraintExprNode; |
| 1727 | w.u.aiCol = aiChng; |
| 1728 | sqlite3WalkExpr(&w, pExpr); |
drh | 05723a9 | 2016-02-10 19:10:50 | [diff] [blame] | 1729 | if( !chngRowid ){ |
| 1730 | testcase( (w.eCode & CKCNSTRNT_ROWID)!=0 ); |
| 1731 | w.eCode &= ~CKCNSTRNT_ROWID; |
| 1732 | } |
| 1733 | testcase( w.eCode==0 ); |
| 1734 | testcase( w.eCode==CKCNSTRNT_COLUMN ); |
| 1735 | testcase( w.eCode==CKCNSTRNT_ROWID ); |
| 1736 | testcase( w.eCode==(CKCNSTRNT_ROWID|CKCNSTRNT_COLUMN) ); |
drh | e9816d8 | 2018-09-15 21:38:48 | [diff] [blame] | 1737 | return w.eCode!=0; |
drh | 2a0b527 | 2016-02-10 16:52:24 | [diff] [blame] | 1738 | } |
| 1739 | |
drh | 11e8527 | 2013-10-26 15:40:48 | [diff] [blame] | 1740 | /* |
drh | daf2761 | 2020-12-10 20:31:25 | [diff] [blame] | 1741 | ** The sqlite3GenerateConstraintChecks() routine usually wants to visit |
| 1742 | ** the indexes of a table in the order provided in the Table->pIndex list. |
| 1743 | ** However, sometimes (rarely - when there is an upsert) it wants to visit |
| 1744 | ** the indexes in a different order. The following data structures accomplish |
| 1745 | ** this. |
| 1746 | ** |
| 1747 | ** The IndexIterator object is used to walk through all of the indexes |
| 1748 | ** of a table in either Index.pNext order, or in some other order established |
| 1749 | ** by an array of IndexListTerm objects. |
| 1750 | */ |
| 1751 | typedef struct IndexListTerm IndexListTerm; |
| 1752 | typedef struct IndexIterator IndexIterator; |
| 1753 | struct IndexIterator { |
| 1754 | int eType; /* 0 for Index.pNext list. 1 for an array of IndexListTerm */ |
| 1755 | int i; /* Index of the current item from the list */ |
| 1756 | union { |
| 1757 | struct { /* Use this object for eType==0: A Index.pNext list */ |
| 1758 | Index *pIdx; /* The current Index */ |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 1759 | } lx; |
drh | daf2761 | 2020-12-10 20:31:25 | [diff] [blame] | 1760 | struct { /* Use this object for eType==1; Array of IndexListTerm */ |
| 1761 | int nIdx; /* Size of the array */ |
| 1762 | IndexListTerm *aIdx; /* Array of IndexListTerms */ |
| 1763 | } ax; |
| 1764 | } u; |
| 1765 | }; |
| 1766 | |
| 1767 | /* When IndexIterator.eType==1, then each index is an array of instances |
| 1768 | ** of the following object |
| 1769 | */ |
| 1770 | struct IndexListTerm { |
| 1771 | Index *p; /* The index */ |
| 1772 | int ix; /* Which entry in the original Table.pIndex list is this index*/ |
| 1773 | }; |
| 1774 | |
| 1775 | /* Return the first index on the list */ |
| 1776 | static Index *indexIteratorFirst(IndexIterator *pIter, int *pIx){ |
drh | ed4c546 | 2020-12-11 16:49:51 | [diff] [blame] | 1777 | assert( pIter->i==0 ); |
| 1778 | if( pIter->eType ){ |
| 1779 | *pIx = pIter->u.ax.aIdx[0].ix; |
| 1780 | return pIter->u.ax.aIdx[0].p; |
| 1781 | }else{ |
| 1782 | *pIx = 0; |
| 1783 | return pIter->u.lx.pIdx; |
| 1784 | } |
drh | daf2761 | 2020-12-10 20:31:25 | [diff] [blame] | 1785 | } |
| 1786 | |
| 1787 | /* Return the next index from the list. Return NULL when out of indexes */ |
| 1788 | static Index *indexIteratorNext(IndexIterator *pIter, int *pIx){ |
drh | daf2761 | 2020-12-10 20:31:25 | [diff] [blame] | 1789 | if( pIter->eType ){ |
drh | d3e21a1 | 2020-12-11 17:11:56 | [diff] [blame] | 1790 | int i = ++pIter->i; |
drh | 61e280a | 2020-12-11 01:17:06 | [diff] [blame] | 1791 | if( i>=pIter->u.ax.nIdx ){ |
| 1792 | *pIx = i; |
| 1793 | return 0; |
| 1794 | } |
drh | daf2761 | 2020-12-10 20:31:25 | [diff] [blame] | 1795 | *pIx = pIter->u.ax.aIdx[i].ix; |
| 1796 | return pIter->u.ax.aIdx[i].p; |
| 1797 | }else{ |
drh | d3e21a1 | 2020-12-11 17:11:56 | [diff] [blame] | 1798 | ++(*pIx); |
drh | daf2761 | 2020-12-10 20:31:25 | [diff] [blame] | 1799 | pIter->u.lx.pIdx = pIter->u.lx.pIdx->pNext; |
| 1800 | return pIter->u.lx.pIdx; |
| 1801 | } |
| 1802 | } |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 1803 | |
drh | daf2761 | 2020-12-10 20:31:25 | [diff] [blame] | 1804 | /* |
drh | 6934fc7 | 2013-10-31 15:37:49 | [diff] [blame] | 1805 | ** Generate code to do constraint checks prior to an INSERT or an UPDATE |
| 1806 | ** on table pTab. |
drh | 9cfcf5d | 2002-01-29 18:41:24 | [diff] [blame] | 1807 | ** |
drh | 6934fc7 | 2013-10-31 15:37:49 | [diff] [blame] | 1808 | ** The regNewData parameter is the first register in a range that contains |
| 1809 | ** the data to be inserted or the data after the update. There will be |
| 1810 | ** pTab->nCol+1 registers in this range. The first register (the one |
| 1811 | ** that regNewData points to) will contain the new rowid, or NULL in the |
| 1812 | ** case of a WITHOUT ROWID table. The second register in the range will |
| 1813 | ** contain the content of the first table column. The third register will |
| 1814 | ** contain the content of the second table column. And so forth. |
drh | 0ca3e24 | 2002-01-29 23:07:02 | [diff] [blame] | 1815 | ** |
drh | f8ffb27 | 2013-11-01 17:08:56 | [diff] [blame] | 1816 | ** The regOldData parameter is similar to regNewData except that it contains |
| 1817 | ** the data prior to an UPDATE rather than afterwards. regOldData is zero |
| 1818 | ** for an INSERT. This routine can distinguish between UPDATE and INSERT by |
| 1819 | ** checking regOldData for zero. |
drh | 0ca3e24 | 2002-01-29 23:07:02 | [diff] [blame] | 1820 | ** |
drh | f8ffb27 | 2013-11-01 17:08:56 | [diff] [blame] | 1821 | ** For an UPDATE, the pkChng boolean is true if the true primary key (the |
| 1822 | ** rowid for a normal table or the PRIMARY KEY for a WITHOUT ROWID table) |
| 1823 | ** might be modified by the UPDATE. If pkChng is false, then the key of |
| 1824 | ** the iDataCur content table is guaranteed to be unchanged by the UPDATE. |
drh | 0ca3e24 | 2002-01-29 23:07:02 | [diff] [blame] | 1825 | ** |
drh | f8ffb27 | 2013-11-01 17:08:56 | [diff] [blame] | 1826 | ** For an INSERT, the pkChng boolean indicates whether or not the rowid |
| 1827 | ** was explicitly specified as part of the INSERT statement. If pkChng |
| 1828 | ** is zero, it means that the either rowid is computed automatically or |
| 1829 | ** that the table is a WITHOUT ROWID table and has no rowid. On an INSERT, |
| 1830 | ** pkChng will only be true if the INSERT statement provides an integer |
| 1831 | ** value for either the rowid column or its INTEGER PRIMARY KEY alias. |
drh | 0ca3e24 | 2002-01-29 23:07:02 | [diff] [blame] | 1832 | ** |
drh | 6934fc7 | 2013-10-31 15:37:49 | [diff] [blame] | 1833 | ** The code generated by this routine will store new index entries into |
drh | aa9b896 | 2008-01-08 02:57:55 | [diff] [blame] | 1834 | ** registers identified by aRegIdx[]. No index entry is created for |
| 1835 | ** indices where aRegIdx[i]==0. The order of indices in aRegIdx[] is |
| 1836 | ** the same as the order of indices on the linked list of indices |
drh | 6934fc7 | 2013-10-31 15:37:49 | [diff] [blame] | 1837 | ** at pTab->pIndex. |
| 1838 | ** |
drh | a7c3b93 | 2019-05-07 19:13:42 | [diff] [blame] | 1839 | ** (2019-05-07) The generated code also creates a new record for the |
| 1840 | ** main table, if pTab is a rowid table, and stores that record in the |
| 1841 | ** register identified by aRegIdx[nIdx] - in other words in the first |
| 1842 | ** entry of aRegIdx[] past the last index. It is important that the |
| 1843 | ** record be generated during constraint checks to avoid affinity changes |
| 1844 | ** to the register content that occur after constraint checks but before |
| 1845 | ** the new record is inserted. |
| 1846 | ** |
drh | 6934fc7 | 2013-10-31 15:37:49 | [diff] [blame] | 1847 | ** The caller must have already opened writeable cursors on the main |
| 1848 | ** table and all applicable indices (that is to say, all indices for which |
| 1849 | ** aRegIdx[] is not zero). iDataCur is the cursor for the main table when |
| 1850 | ** inserting or updating a rowid table, or the cursor for the PRIMARY KEY |
| 1851 | ** index when operating on a WITHOUT ROWID table. iIdxCur is the cursor |
| 1852 | ** for the first index in the pTab->pIndex list. Cursors for other indices |
| 1853 | ** are at iIdxCur+N for the N-th element of the pTab->pIndex list. |
drh | 9cfcf5d | 2002-01-29 18:41:24 | [diff] [blame] | 1854 | ** |
| 1855 | ** This routine also generates code to check constraints. NOT NULL, |
| 1856 | ** CHECK, and UNIQUE constraints are all checked. If a constraint fails, |
drh | 1c92853 | 2002-01-31 15:54:21 | [diff] [blame] | 1857 | ** then the appropriate action is performed. There are five possible |
| 1858 | ** actions: ROLLBACK, ABORT, FAIL, REPLACE, and IGNORE. |
drh | 9cfcf5d | 2002-01-29 18:41:24 | [diff] [blame] | 1859 | ** |
| 1860 | ** Constraint type Action What Happens |
| 1861 | ** --------------- ---------- ---------------------------------------- |
drh | 1c92853 | 2002-01-31 15:54:21 | [diff] [blame] | 1862 | ** any ROLLBACK The current transaction is rolled back and |
drh | 6934fc7 | 2013-10-31 15:37:49 | [diff] [blame] | 1863 | ** sqlite3_step() returns immediately with a |
drh | 9cfcf5d | 2002-01-29 18:41:24 | [diff] [blame] | 1864 | ** return code of SQLITE_CONSTRAINT. |
| 1865 | ** |
drh | 1c92853 | 2002-01-31 15:54:21 | [diff] [blame] | 1866 | ** any ABORT Back out changes from the current command |
| 1867 | ** only (do not do a complete rollback) then |
drh | 6934fc7 | 2013-10-31 15:37:49 | [diff] [blame] | 1868 | ** cause sqlite3_step() to return immediately |
drh | 1c92853 | 2002-01-31 15:54:21 | [diff] [blame] | 1869 | ** with SQLITE_CONSTRAINT. |
| 1870 | ** |
drh | 6934fc7 | 2013-10-31 15:37:49 | [diff] [blame] | 1871 | ** any FAIL Sqlite3_step() returns immediately with a |
drh | 1c92853 | 2002-01-31 15:54:21 | [diff] [blame] | 1872 | ** return code of SQLITE_CONSTRAINT. The |
| 1873 | ** transaction is not rolled back and any |
drh | 6934fc7 | 2013-10-31 15:37:49 | [diff] [blame] | 1874 | ** changes to prior rows are retained. |
drh | 1c92853 | 2002-01-31 15:54:21 | [diff] [blame] | 1875 | ** |
drh | 6934fc7 | 2013-10-31 15:37:49 | [diff] [blame] | 1876 | ** any IGNORE The attempt in insert or update the current |
| 1877 | ** row is skipped, without throwing an error. |
| 1878 | ** Processing continues with the next row. |
| 1879 | ** (There is an immediate jump to ignoreDest.) |
drh | 9cfcf5d | 2002-01-29 18:41:24 | [diff] [blame] | 1880 | ** |
| 1881 | ** NOT NULL REPLACE The NULL value is replace by the default |
| 1882 | ** value for that column. If the default value |
| 1883 | ** is NULL, the action is the same as ABORT. |
| 1884 | ** |
| 1885 | ** UNIQUE REPLACE The other row that conflicts with the row |
| 1886 | ** being inserted is removed. |
| 1887 | ** |
| 1888 | ** CHECK REPLACE Illegal. The results in an exception. |
| 1889 | ** |
drh | 1c92853 | 2002-01-31 15:54:21 | [diff] [blame] | 1890 | ** Which action to take is determined by the overrideError parameter. |
| 1891 | ** Or if overrideError==OE_Default, then the pParse->onError parameter |
| 1892 | ** is used. Or if pParse->onError==OE_Default then the onError value |
| 1893 | ** for the constraint is used. |
drh | 9cfcf5d | 2002-01-29 18:41:24 | [diff] [blame] | 1894 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 | [diff] [blame] | 1895 | void sqlite3GenerateConstraintChecks( |
drh | 6934fc7 | 2013-10-31 15:37:49 | [diff] [blame] | 1896 | Parse *pParse, /* The parser context */ |
| 1897 | Table *pTab, /* The table being inserted or updated */ |
drh | f8ffb27 | 2013-11-01 17:08:56 | [diff] [blame] | 1898 | int *aRegIdx, /* Use register aRegIdx[i] for index i. 0 for unused */ |
drh | 6934fc7 | 2013-10-31 15:37:49 | [diff] [blame] | 1899 | int iDataCur, /* Canonical data cursor (main table or PK index) */ |
| 1900 | int iIdxCur, /* First index cursor */ |
| 1901 | int regNewData, /* First register in a range holding values to insert */ |
drh | f8ffb27 | 2013-11-01 17:08:56 | [diff] [blame] | 1902 | int regOldData, /* Previous content. 0 for INSERTs */ |
| 1903 | u8 pkChng, /* Non-zero if the rowid or PRIMARY KEY changed */ |
| 1904 | u8 overrideError, /* Override onError to this if not OE_Default */ |
drh | 6934fc7 | 2013-10-31 15:37:49 | [diff] [blame] | 1905 | int ignoreDest, /* Jump to this label on an OE_Ignore resolution */ |
drh | bdb0022 | 2016-02-10 16:03:20 | [diff] [blame] | 1906 | int *pbMayReplace, /* OUT: Set to true if constraint may cause a replace */ |
drh | 788d55a | 2018-04-13 01:15:09 | [diff] [blame] | 1907 | int *aiChng, /* column i is unchanged if aiChng[i]<0 */ |
| 1908 | Upsert *pUpsert /* ON CONFLICT clauses, if any. NULL otherwise */ |
drh | 9cfcf5d | 2002-01-29 18:41:24 | [diff] [blame] | 1909 | ){ |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 1910 | Vdbe *v; /* VDBE under construction */ |
drh | 1b7ecbb | 2009-05-03 01:00:59 | [diff] [blame] | 1911 | Index *pIdx; /* Pointer to one of the indices */ |
drh | e84ad92 | 2020-12-09 20:30:47 | [diff] [blame] | 1912 | Index *pPk = 0; /* The PRIMARY KEY index for WITHOUT ROWID tables */ |
drh | 2938f92 | 2012-03-07 19:13:29 | [diff] [blame] | 1913 | sqlite3 *db; /* Database connection */ |
drh | f8ffb27 | 2013-11-01 17:08:56 | [diff] [blame] | 1914 | int i; /* loop counter */ |
| 1915 | int ix; /* Index loop counter */ |
| 1916 | int nCol; /* Number of columns */ |
| 1917 | int onError; /* Conflict resolution strategy */ |
drh | 1b7ecbb | 2009-05-03 01:00:59 | [diff] [blame] | 1918 | int seenReplace = 0; /* True if REPLACE is used to resolve INT PK conflict */ |
drh | 6fbe41a | 2013-10-30 20:22:55 | [diff] [blame] | 1919 | int nPkField; /* Number of fields in PRIMARY KEY. 1 for ROWID tables */ |
drh | 61e280a | 2020-12-11 01:17:06 | [diff] [blame] | 1920 | Upsert *pUpsertClause = 0; /* The specific ON CONFLICT clause for pIdx */ |
| 1921 | u8 isUpdate; /* True if this is an UPDATE operation */ |
drh | 57bf4a8 | 2014-02-17 14:59:22 | [diff] [blame] | 1922 | u8 bAffinityDone = 0; /* True if the OP_Affinity operation has been run */ |
drh | 61e280a | 2020-12-11 01:17:06 | [diff] [blame] | 1923 | int upsertIpkReturn = 0; /* Address of Goto at end of IPK uniqueness check */ |
| 1924 | int upsertIpkDelay = 0; /* Address of Goto to bypass initial IPK check */ |
drh | 8430450 | 2018-08-14 15:12:52 | [diff] [blame] | 1925 | int ipkTop = 0; /* Top of the IPK uniqueness check */ |
| 1926 | int ipkBottom = 0; /* OP_Goto at the end of the IPK uniqueness check */ |
drh | a407ecc | 2019-10-26 00:04:21 | [diff] [blame] | 1927 | /* Variables associated with retesting uniqueness constraints after |
| 1928 | ** replace triggers fire have run */ |
| 1929 | int regTrigCnt; /* Register used to count replace trigger invocations */ |
| 1930 | int addrRecheck = 0; /* Jump here to recheck all uniqueness constraints */ |
| 1931 | int lblRecheckOk = 0; /* Each recheck jumps to this label if it passes */ |
| 1932 | Trigger *pTrigger; /* List of DELETE triggers on the table pTab */ |
| 1933 | int nReplaceTrig = 0; /* Number of replace triggers coded */ |
drh | 61e280a | 2020-12-11 01:17:06 | [diff] [blame] | 1934 | IndexIterator sIdxIter; /* Index iterator */ |
drh | 9cfcf5d | 2002-01-29 18:41:24 | [diff] [blame] | 1935 | |
drh | f8ffb27 | 2013-11-01 17:08:56 | [diff] [blame] | 1936 | isUpdate = regOldData!=0; |
drh | 2938f92 | 2012-03-07 19:13:29 | [diff] [blame] | 1937 | db = pParse->db; |
drh | f0b4174 | 2020-08-15 21:55:14 | [diff] [blame] | 1938 | v = pParse->pVdbe; |
drh | 9cfcf5d | 2002-01-29 18:41:24 | [diff] [blame] | 1939 | assert( v!=0 ); |
drh | f38524d | 2021-08-02 16:41:57 | [diff] [blame] | 1940 | assert( !IsView(pTab) ); /* This table is not a VIEW */ |
drh | 9cfcf5d | 2002-01-29 18:41:24 | [diff] [blame] | 1941 | nCol = pTab->nCol; |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 1942 | |
drh | 6934fc7 | 2013-10-31 15:37:49 | [diff] [blame] | 1943 | /* pPk is the PRIMARY KEY index for WITHOUT ROWID tables and NULL for |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 1944 | ** normal rowid tables. nPkField is the number of key fields in the |
drh | 6934fc7 | 2013-10-31 15:37:49 | [diff] [blame] | 1945 | ** pPk index or 1 for a rowid table. In other words, nPkField is the |
| 1946 | ** number of fields in the true primary key of the table. */ |
drh | 26198bb | 2013-10-31 11:15:09 | [diff] [blame] | 1947 | if( HasRowid(pTab) ){ |
| 1948 | pPk = 0; |
| 1949 | nPkField = 1; |
| 1950 | }else{ |
| 1951 | pPk = sqlite3PrimaryKeyIndex(pTab); |
| 1952 | nPkField = pPk->nKeyCol; |
| 1953 | } |
drh | 6fbe41a | 2013-10-30 20:22:55 | [diff] [blame] | 1954 | |
| 1955 | /* Record that this module has started */ |
| 1956 | VdbeModuleComment((v, "BEGIN: GenCnstCks(%d,%d,%d,%d,%d)", |
drh | 6934fc7 | 2013-10-31 15:37:49 | [diff] [blame] | 1957 | iDataCur, iIdxCur, regNewData, regOldData, pkChng)); |
drh | 9cfcf5d | 2002-01-29 18:41:24 | [diff] [blame] | 1958 | |
| 1959 | /* Test all NOT NULL constraints. |
| 1960 | */ |
drh | cbda9c7 | 2019-10-26 17:08:06 | [diff] [blame] | 1961 | if( pTab->tabFlags & TF_HasNotNull ){ |
drh | ad5f157 | 2019-12-28 00:36:51 | [diff] [blame] | 1962 | int b2ndPass = 0; /* True if currently running 2nd pass */ |
| 1963 | int nSeenReplace = 0; /* Number of ON CONFLICT REPLACE operations */ |
| 1964 | int nGenerated = 0; /* Number of generated columns with NOT NULL */ |
| 1965 | while(1){ /* Make 2 passes over columns. Exit loop via "break" */ |
| 1966 | for(i=0; i<nCol; i++){ |
| 1967 | int iReg; /* Register holding column value */ |
| 1968 | Column *pCol = &pTab->aCol[i]; /* The column to check for NOT NULL */ |
| 1969 | int isGenerated; /* non-zero if column is generated */ |
| 1970 | onError = pCol->notNull; |
| 1971 | if( onError==OE_None ) continue; /* No NOT NULL on this column */ |
| 1972 | if( i==pTab->iPKey ){ |
| 1973 | continue; /* ROWID is never NULL */ |
| 1974 | } |
| 1975 | isGenerated = pCol->colFlags & COLFLAG_GENERATED; |
| 1976 | if( isGenerated && !b2ndPass ){ |
| 1977 | nGenerated++; |
| 1978 | continue; /* Generated columns processed on 2nd pass */ |
| 1979 | } |
| 1980 | if( aiChng && aiChng[i]<0 && !isGenerated ){ |
| 1981 | /* Do not check NOT NULL on columns that do not change */ |
| 1982 | continue; |
| 1983 | } |
| 1984 | if( overrideError!=OE_Default ){ |
| 1985 | onError = overrideError; |
| 1986 | }else if( onError==OE_Default ){ |
drh | cbda9c7 | 2019-10-26 17:08:06 | [diff] [blame] | 1987 | onError = OE_Abort; |
drh | cbda9c7 | 2019-10-26 17:08:06 | [diff] [blame] | 1988 | } |
drh | ad5f157 | 2019-12-28 00:36:51 | [diff] [blame] | 1989 | if( onError==OE_Replace ){ |
| 1990 | if( b2ndPass /* REPLACE becomes ABORT on the 2nd pass */ |
drh | 79cf2b7 | 2021-07-31 20:30:41 | [diff] [blame] | 1991 | || pCol->iDflt==0 /* REPLACE is ABORT if no DEFAULT value */ |
drh | ad5f157 | 2019-12-28 00:36:51 | [diff] [blame] | 1992 | ){ |
| 1993 | testcase( pCol->colFlags & COLFLAG_VIRTUAL ); |
| 1994 | testcase( pCol->colFlags & COLFLAG_STORED ); |
| 1995 | testcase( pCol->colFlags & COLFLAG_GENERATED ); |
| 1996 | onError = OE_Abort; |
| 1997 | }else{ |
| 1998 | assert( !isGenerated ); |
| 1999 | } |
| 2000 | }else if( b2ndPass && !isGenerated ){ |
| 2001 | continue; |
drh | cbda9c7 | 2019-10-26 17:08:06 | [diff] [blame] | 2002 | } |
drh | ad5f157 | 2019-12-28 00:36:51 | [diff] [blame] | 2003 | assert( onError==OE_Rollback || onError==OE_Abort || onError==OE_Fail |
| 2004 | || onError==OE_Ignore || onError==OE_Replace ); |
| 2005 | testcase( i!=sqlite3TableColumnToStorage(pTab, i) ); |
| 2006 | iReg = sqlite3TableColumnToStorage(pTab, i) + regNewData + 1; |
| 2007 | switch( onError ){ |
| 2008 | case OE_Replace: { |
| 2009 | int addr1 = sqlite3VdbeAddOp1(v, OP_NotNull, iReg); |
| 2010 | VdbeCoverage(v); |
| 2011 | assert( (pCol->colFlags & COLFLAG_GENERATED)==0 ); |
| 2012 | nSeenReplace++; |
drh | 79cf2b7 | 2021-07-31 20:30:41 | [diff] [blame] | 2013 | sqlite3ExprCodeCopy(pParse, |
| 2014 | sqlite3ColumnExpr(pTab, pCol), iReg); |
drh | ad5f157 | 2019-12-28 00:36:51 | [diff] [blame] | 2015 | sqlite3VdbeJumpHere(v, addr1); |
| 2016 | break; |
| 2017 | } |
| 2018 | case OE_Abort: |
| 2019 | sqlite3MayAbort(pParse); |
drh | 08b9208 | 2020-08-10 14:18:00 | [diff] [blame] | 2020 | /* no break */ deliberate_fall_through |
drh | ad5f157 | 2019-12-28 00:36:51 | [diff] [blame] | 2021 | case OE_Rollback: |
| 2022 | case OE_Fail: { |
| 2023 | char *zMsg = sqlite3MPrintf(db, "%s.%s", pTab->zName, |
drh | cf9d36d | 2021-08-02 18:03:43 | [diff] [blame] | 2024 | pCol->zCnName); |
drh | 85ec20a | 2022-11-30 21:18:23 | [diff] [blame] | 2025 | testcase( zMsg==0 && db->mallocFailed==0 ); |
drh | ad5f157 | 2019-12-28 00:36:51 | [diff] [blame] | 2026 | sqlite3VdbeAddOp3(v, OP_HaltIfNull, SQLITE_CONSTRAINT_NOTNULL, |
| 2027 | onError, iReg); |
| 2028 | sqlite3VdbeAppendP4(v, zMsg, P4_DYNAMIC); |
| 2029 | sqlite3VdbeChangeP5(v, P5_ConstraintNotNull); |
| 2030 | VdbeCoverage(v); |
| 2031 | break; |
| 2032 | } |
| 2033 | default: { |
| 2034 | assert( onError==OE_Ignore ); |
| 2035 | sqlite3VdbeAddOp2(v, OP_IsNull, iReg, ignoreDest); |
| 2036 | VdbeCoverage(v); |
| 2037 | break; |
| 2038 | } |
| 2039 | } /* end switch(onError) */ |
| 2040 | } /* end loop i over columns */ |
| 2041 | if( nGenerated==0 && nSeenReplace==0 ){ |
| 2042 | /* If there are no generated columns with NOT NULL constraints |
| 2043 | ** and no NOT NULL ON CONFLICT REPLACE constraints, then a single |
| 2044 | ** pass is sufficient */ |
| 2045 | break; |
drh | 9cfcf5d | 2002-01-29 18:41:24 | [diff] [blame] | 2046 | } |
drh | ad5f157 | 2019-12-28 00:36:51 | [diff] [blame] | 2047 | if( b2ndPass ) break; /* Never need more than 2 passes */ |
| 2048 | b2ndPass = 1; |
drh | ef9f719 | 2020-01-17 19:14:08 | [diff] [blame] | 2049 | #ifndef SQLITE_OMIT_GENERATED_COLUMNS |
drh | ad5f157 | 2019-12-28 00:36:51 | [diff] [blame] | 2050 | if( nSeenReplace>0 && (pTab->tabFlags & TF_HasGenerated)!=0 ){ |
| 2051 | /* If any NOT NULL ON CONFLICT REPLACE constraints fired on the |
| 2052 | ** first pass, recomputed values for all generated columns, as |
| 2053 | ** those values might depend on columns affected by the REPLACE. |
| 2054 | */ |
| 2055 | sqlite3ComputeGeneratedColumns(pParse, regNewData+1, pTab); |
| 2056 | } |
drh | ef9f719 | 2020-01-17 19:14:08 | [diff] [blame] | 2057 | #endif |
drh | ad5f157 | 2019-12-28 00:36:51 | [diff] [blame] | 2058 | } /* end of 2-pass loop */ |
| 2059 | } /* end if( has-not-null-constraints ) */ |
drh | 9cfcf5d | 2002-01-29 18:41:24 | [diff] [blame] | 2060 | |
| 2061 | /* Test all CHECK constraints |
| 2062 | */ |
drh | ffe07b2 | 2005-11-03 00:41:17 | [diff] [blame] | 2063 | #ifndef SQLITE_OMIT_CHECK |
drh | 2938f92 | 2012-03-07 19:13:29 | [diff] [blame] | 2064 | if( pTab->pCheck && (db->flags & SQLITE_IgnoreChecks)==0 ){ |
| 2065 | ExprList *pCheck = pTab->pCheck; |
drh | 6e97f8e | 2017-07-20 13:17:08 | [diff] [blame] | 2066 | pParse->iSelfTab = -(regNewData+1); |
drh | aa01c7e | 2006-03-15 16:26:10 | [diff] [blame] | 2067 | onError = overrideError!=OE_Default ? overrideError : OE_Abort; |
drh | 2938f92 | 2012-03-07 19:13:29 | [diff] [blame] | 2068 | for(i=0; i<pCheck->nExpr; i++){ |
drh | 05723a9 | 2016-02-10 19:10:50 | [diff] [blame] | 2069 | int allOk; |
drh | 5cf1b61 | 2020-03-10 18:55:41 | [diff] [blame] | 2070 | Expr *pCopy; |
drh | 2a0b527 | 2016-02-10 16:52:24 | [diff] [blame] | 2071 | Expr *pExpr = pCheck->a[i].pExpr; |
drh | e9816d8 | 2018-09-15 21:38:48 | [diff] [blame] | 2072 | if( aiChng |
| 2073 | && !sqlite3ExprReferencesUpdatedColumn(pExpr, aiChng, pkChng) |
| 2074 | ){ |
| 2075 | /* The check constraints do not reference any of the columns being |
| 2076 | ** updated so there is no point it verifying the check constraint */ |
| 2077 | continue; |
| 2078 | } |
drh | 9dce0ef | 2020-02-01 21:03:27 | [diff] [blame] | 2079 | if( bAffinityDone==0 ){ |
| 2080 | sqlite3TableAffinity(v, pTab, regNewData+1); |
| 2081 | bAffinityDone = 1; |
| 2082 | } |
drh | ec4ccdb | 2018-12-29 02:26:59 | [diff] [blame] | 2083 | allOk = sqlite3VdbeMakeLabel(pParse); |
drh | 4031baf | 2018-05-28 17:31:20 | [diff] [blame] | 2084 | sqlite3VdbeVerifyAbortable(v, onError); |
drh | 5cf1b61 | 2020-03-10 18:55:41 | [diff] [blame] | 2085 | pCopy = sqlite3ExprDup(db, pExpr, 0); |
| 2086 | if( !db->mallocFailed ){ |
| 2087 | sqlite3ExprIfTrue(pParse, pCopy, allOk, SQLITE_JUMPIFNULL); |
| 2088 | } |
| 2089 | sqlite3ExprDelete(db, pCopy); |
drh | 2d8e920 | 2012-12-08 04:10:44 | [diff] [blame] | 2090 | if( onError==OE_Ignore ){ |
drh | 076e85f | 2015-09-03 13:46:12 | [diff] [blame] | 2091 | sqlite3VdbeGoto(v, ignoreDest); |
drh | 2d8e920 | 2012-12-08 04:10:44 | [diff] [blame] | 2092 | }else{ |
drh | 41cee66 | 2019-12-12 20:22:34 | [diff] [blame] | 2093 | char *zName = pCheck->a[i].zEName; |
drh | e2678b9 | 2020-08-28 12:58:21 | [diff] [blame] | 2094 | assert( zName!=0 || pParse->db->mallocFailed ); |
drh | 0ce974d | 2019-06-12 22:46:04 | [diff] [blame] | 2095 | if( onError==OE_Replace ) onError = OE_Abort; /* IMP: R-26383-51744 */ |
drh | d91c1a1 | 2013-02-09 13:58:25 | [diff] [blame] | 2096 | sqlite3HaltConstraint(pParse, SQLITE_CONSTRAINT_CHECK, |
drh | f9c8ce3 | 2013-11-05 13:33:55 | [diff] [blame] | 2097 | onError, zName, P4_TRANSIENT, |
| 2098 | P5_ConstraintCheck); |
drh | 2938f92 | 2012-03-07 19:13:29 | [diff] [blame] | 2099 | } |
drh | 2d8e920 | 2012-12-08 04:10:44 | [diff] [blame] | 2100 | sqlite3VdbeResolveLabel(v, allOk); |
drh | aa01c7e | 2006-03-15 16:26:10 | [diff] [blame] | 2101 | } |
drh | 6e97f8e | 2017-07-20 13:17:08 | [diff] [blame] | 2102 | pParse->iSelfTab = 0; |
drh | ffe07b2 | 2005-11-03 00:41:17 | [diff] [blame] | 2103 | } |
| 2104 | #endif /* !defined(SQLITE_OMIT_CHECK) */ |
drh | 9cfcf5d | 2002-01-29 18:41:24 | [diff] [blame] | 2105 | |
drh | 096fd47 | 2018-04-14 20:24:36 | [diff] [blame] | 2106 | /* UNIQUE and PRIMARY KEY constraints should be handled in the following |
| 2107 | ** order: |
| 2108 | ** |
drh | 8430450 | 2018-08-14 15:12:52 | [diff] [blame] | 2109 | ** (1) OE_Update |
| 2110 | ** (2) OE_Abort, OE_Fail, OE_Rollback, OE_Ignore |
drh | 096fd47 | 2018-04-14 20:24:36 | [diff] [blame] | 2111 | ** (3) OE_Replace |
| 2112 | ** |
| 2113 | ** OE_Fail and OE_Ignore must happen before any changes are made. |
| 2114 | ** OE_Update guarantees that only a single row will change, so it |
| 2115 | ** must happen before OE_Replace. Technically, OE_Abort and OE_Rollback |
| 2116 | ** could happen in any order, but they are grouped up front for |
| 2117 | ** convenience. |
| 2118 | ** |
drh | 8a6f89c | 2025-04-10 10:18:07 | [diff] [blame] | 2119 | ** 2018-08-14: Ticket https://sqlite.org/src/info/908f001483982c43 |
drh | 8430450 | 2018-08-14 15:12:52 | [diff] [blame] | 2120 | ** The order of constraints used to have OE_Update as (2) and OE_Abort |
| 2121 | ** and so forth as (1). But apparently PostgreSQL checks the OE_Update |
| 2122 | ** constraint before any others, so it had to be moved. |
| 2123 | ** |
drh | 096fd47 | 2018-04-14 20:24:36 | [diff] [blame] | 2124 | ** Constraint checking code is generated in this order: |
| 2125 | ** (A) The rowid constraint |
| 2126 | ** (B) Unique index constraints that do not have OE_Replace as their |
| 2127 | ** default conflict resolution strategy |
| 2128 | ** (C) Unique index that do use OE_Replace by default. |
| 2129 | ** |
| 2130 | ** The ordering of (2) and (3) is accomplished by making sure the linked |
| 2131 | ** list of indexes attached to a table puts all OE_Replace indexes last |
| 2132 | ** in the list. See sqlite3CreateIndex() for where that happens. |
| 2133 | */ |
drh | 61e280a | 2020-12-11 01:17:06 | [diff] [blame] | 2134 | sIdxIter.eType = 0; |
| 2135 | sIdxIter.i = 0; |
drh | d3e21a1 | 2020-12-11 17:11:56 | [diff] [blame] | 2136 | sIdxIter.u.ax.aIdx = 0; /* Silence harmless compiler warning */ |
drh | 61e280a | 2020-12-11 01:17:06 | [diff] [blame] | 2137 | sIdxIter.u.lx.pIdx = pTab->pIndex; |
drh | 096fd47 | 2018-04-14 20:24:36 | [diff] [blame] | 2138 | if( pUpsert ){ |
| 2139 | if( pUpsert->pUpsertTarget==0 ){ |
drh | 61e280a | 2020-12-11 01:17:06 | [diff] [blame] | 2140 | /* There is just on ON CONFLICT clause and it has no constraint-target */ |
| 2141 | assert( pUpsert->pNextUpsert==0 ); |
drh | 255c1c1 | 2020-12-12 00:28:15 | [diff] [blame] | 2142 | if( pUpsert->isDoUpdate==0 ){ |
drh | 61e280a | 2020-12-11 01:17:06 | [diff] [blame] | 2143 | /* A single ON CONFLICT DO NOTHING clause, without a constraint-target. |
| 2144 | ** Make all unique constraint resolution be OE_Ignore */ |
| 2145 | overrideError = OE_Ignore; |
| 2146 | pUpsert = 0; |
| 2147 | }else{ |
| 2148 | /* A single ON CONFLICT DO UPDATE. Make all resolutions OE_Update */ |
| 2149 | overrideError = OE_Update; |
| 2150 | } |
| 2151 | }else if( pTab->pIndex!=0 ){ |
| 2152 | /* Otherwise, we'll need to run the IndexListTerm array version of the |
| 2153 | ** iterator to ensure that all of the ON CONFLICT conditions are |
| 2154 | ** checked first and in order. */ |
| 2155 | int nIdx, jj; |
| 2156 | u64 nByte; |
| 2157 | Upsert *pTerm; |
| 2158 | u8 *bUsed; |
| 2159 | for(nIdx=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, nIdx++){ |
| 2160 | assert( aRegIdx[nIdx]>0 ); |
| 2161 | } |
| 2162 | sIdxIter.eType = 1; |
| 2163 | sIdxIter.u.ax.nIdx = nIdx; |
| 2164 | nByte = (sizeof(IndexListTerm)+1)*nIdx + nIdx; |
| 2165 | sIdxIter.u.ax.aIdx = sqlite3DbMallocZero(db, nByte); |
| 2166 | if( sIdxIter.u.ax.aIdx==0 ) return; /* OOM */ |
| 2167 | bUsed = (u8*)&sIdxIter.u.ax.aIdx[nIdx]; |
| 2168 | pUpsert->pToFree = sIdxIter.u.ax.aIdx; |
| 2169 | for(i=0, pTerm=pUpsert; pTerm; pTerm=pTerm->pNextUpsert){ |
| 2170 | if( pTerm->pUpsertTarget==0 ) break; |
| 2171 | if( pTerm->pUpsertIdx==0 ) continue; /* Skip ON CONFLICT for the IPK */ |
| 2172 | jj = 0; |
| 2173 | pIdx = pTab->pIndex; |
| 2174 | while( ALWAYS(pIdx!=0) && pIdx!=pTerm->pUpsertIdx ){ |
| 2175 | pIdx = pIdx->pNext; |
| 2176 | jj++; |
| 2177 | } |
| 2178 | if( bUsed[jj] ) continue; /* Duplicate ON CONFLICT clause ignored */ |
| 2179 | bUsed[jj] = 1; |
| 2180 | sIdxIter.u.ax.aIdx[i].p = pIdx; |
| 2181 | sIdxIter.u.ax.aIdx[i].ix = jj; |
| 2182 | i++; |
| 2183 | } |
| 2184 | for(jj=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, jj++){ |
| 2185 | if( bUsed[jj] ) continue; |
| 2186 | sIdxIter.u.ax.aIdx[i].p = pIdx; |
| 2187 | sIdxIter.u.ax.aIdx[i].ix = jj; |
| 2188 | i++; |
| 2189 | } |
| 2190 | assert( i==nIdx ); |
drh | 096fd47 | 2018-04-14 20:24:36 | [diff] [blame] | 2191 | } |
| 2192 | } |
| 2193 | |
drh | a407ecc | 2019-10-26 00:04:21 | [diff] [blame] | 2194 | /* Determine if it is possible that triggers (either explicitly coded |
| 2195 | ** triggers or FK resolution actions) might run as a result of deletes |
| 2196 | ** that happen when OE_Replace conflict resolution occurs. (Call these |
| 2197 | ** "replace triggers".) If any replace triggers run, we will need to |
| 2198 | ** recheck all of the uniqueness constraints after they have all run. |
| 2199 | ** But on the recheck, the resolution is OE_Abort instead of OE_Replace. |
| 2200 | ** |
| 2201 | ** If replace triggers are a possibility, then |
| 2202 | ** |
| 2203 | ** (1) Allocate register regTrigCnt and initialize it to zero. |
| 2204 | ** That register will count the number of replace triggers that |
drh | d3c468b | 2019-10-26 16:38:49 | [diff] [blame] | 2205 | ** fire. Constraint recheck only occurs if the number is positive. |
| 2206 | ** (2) Initialize pTrigger to the list of all DELETE triggers on pTab. |
drh | a407ecc | 2019-10-26 00:04:21 | [diff] [blame] | 2207 | ** (3) Initialize addrRecheck and lblRecheckOk |
| 2208 | ** |
| 2209 | ** The uniqueness rechecking code will create a series of tests to run |
| 2210 | ** in a second pass. The addrRecheck and lblRecheckOk variables are |
| 2211 | ** used to link together these tests which are separated from each other |
| 2212 | ** in the generate bytecode. |
| 2213 | */ |
| 2214 | if( (db->flags & (SQLITE_RecTriggers|SQLITE_ForeignKeys))==0 ){ |
| 2215 | /* There are not DELETE triggers nor FK constraints. No constraint |
| 2216 | ** rechecks are needed. */ |
| 2217 | pTrigger = 0; |
| 2218 | regTrigCnt = 0; |
| 2219 | }else{ |
| 2220 | if( db->flags&SQLITE_RecTriggers ){ |
| 2221 | pTrigger = sqlite3TriggersExist(pParse, pTab, TK_DELETE, 0, 0); |
| 2222 | regTrigCnt = pTrigger!=0 || sqlite3FkRequired(pParse, pTab, 0, 0); |
| 2223 | }else{ |
| 2224 | pTrigger = 0; |
| 2225 | regTrigCnt = sqlite3FkRequired(pParse, pTab, 0, 0); |
| 2226 | } |
| 2227 | if( regTrigCnt ){ |
| 2228 | /* Replace triggers might exist. Allocate the counter and |
| 2229 | ** initialize it to zero. */ |
| 2230 | regTrigCnt = ++pParse->nMem; |
| 2231 | sqlite3VdbeAddOp2(v, OP_Integer, 0, regTrigCnt); |
| 2232 | VdbeComment((v, "trigger count")); |
| 2233 | lblRecheckOk = sqlite3VdbeMakeLabel(pParse); |
| 2234 | addrRecheck = lblRecheckOk; |
| 2235 | } |
| 2236 | } |
| 2237 | |
drh | f8ffb27 | 2013-11-01 17:08:56 | [diff] [blame] | 2238 | /* If rowid is changing, make sure the new rowid does not previously |
| 2239 | ** exist in the table. |
drh | 9cfcf5d | 2002-01-29 18:41:24 | [diff] [blame] | 2240 | */ |
drh | 6fbe41a | 2013-10-30 20:22:55 | [diff] [blame] | 2241 | if( pkChng && pPk==0 ){ |
drh | ec4ccdb | 2018-12-29 02:26:59 | [diff] [blame] | 2242 | int addrRowidOk = sqlite3VdbeMakeLabel(pParse); |
drh | 11e8527 | 2013-10-26 15:40:48 | [diff] [blame] | 2243 | |
drh | f8ffb27 | 2013-11-01 17:08:56 | [diff] [blame] | 2244 | /* Figure out what action to take in case of a rowid collision */ |
drh | 0ca3e24 | 2002-01-29 23:07:02 | [diff] [blame] | 2245 | onError = pTab->keyConf; |
| 2246 | if( overrideError!=OE_Default ){ |
| 2247 | onError = overrideError; |
drh | a996e47 | 2003-05-16 02:30:27 | [diff] [blame] | 2248 | }else if( onError==OE_Default ){ |
| 2249 | onError = OE_Abort; |
drh | 0ca3e24 | 2002-01-29 23:07:02 | [diff] [blame] | 2250 | } |
drh | 11e8527 | 2013-10-26 15:40:48 | [diff] [blame] | 2251 | |
drh | c8a0c90 | 2018-04-13 15:14:33 | [diff] [blame] | 2252 | /* figure out whether or not upsert applies in this case */ |
drh | 61e280a | 2020-12-11 01:17:06 | [diff] [blame] | 2253 | if( pUpsert ){ |
| 2254 | pUpsertClause = sqlite3UpsertOfIndex(pUpsert,0); |
| 2255 | if( pUpsertClause!=0 ){ |
drh | 255c1c1 | 2020-12-12 00:28:15 | [diff] [blame] | 2256 | if( pUpsertClause->isDoUpdate==0 ){ |
drh | 61e280a | 2020-12-11 01:17:06 | [diff] [blame] | 2257 | onError = OE_Ignore; /* DO NOTHING is the same as INSERT OR IGNORE */ |
| 2258 | }else{ |
| 2259 | onError = OE_Update; /* DO UPDATE */ |
| 2260 | } |
| 2261 | } |
| 2262 | if( pUpsertClause!=pUpsert ){ |
| 2263 | /* The first ON CONFLICT clause has a conflict target other than |
| 2264 | ** the IPK. We have to jump ahead to that first ON CONFLICT clause |
| 2265 | ** and then come back here and deal with the IPK afterwards */ |
| 2266 | upsertIpkDelay = sqlite3VdbeAddOp0(v, OP_Goto); |
drh | c8a0c90 | 2018-04-13 15:14:33 | [diff] [blame] | 2267 | } |
| 2268 | } |
| 2269 | |
drh | 8d1b82e | 2013-11-05 19:41:32 | [diff] [blame] | 2270 | /* If the response to a rowid conflict is REPLACE but the response |
| 2271 | ** to some other UNIQUE constraint is FAIL or IGNORE, then we need |
| 2272 | ** to defer the running of the rowid conflict checking until after |
| 2273 | ** the UNIQUE constraints have run. |
| 2274 | */ |
drh | 8430450 | 2018-08-14 15:12:52 | [diff] [blame] | 2275 | if( onError==OE_Replace /* IPK rule is REPLACE */ |
mistachkin | 9a60e71 | 2020-12-22 19:57:53 | [diff] [blame] | 2276 | && onError!=overrideError /* Rules for other constraints are different */ |
drh | 8430450 | 2018-08-14 15:12:52 | [diff] [blame] | 2277 | && pTab->pIndex /* There exist other constraints */ |
drh | 66306d8 | 2021-12-30 02:38:43 | [diff] [blame] | 2278 | && !upsertIpkDelay /* IPK check already deferred by UPSERT */ |
drh | 096fd47 | 2018-04-14 20:24:36 | [diff] [blame] | 2279 | ){ |
drh | 8430450 | 2018-08-14 15:12:52 | [diff] [blame] | 2280 | ipkTop = sqlite3VdbeAddOp0(v, OP_Goto)+1; |
| 2281 | VdbeComment((v, "defer IPK REPLACE until last")); |
drh | 8d1b82e | 2013-11-05 19:41:32 | [diff] [blame] | 2282 | } |
| 2283 | |
drh | bb6b1ca | 2018-04-19 13:52:39 | [diff] [blame] | 2284 | if( isUpdate ){ |
| 2285 | /* pkChng!=0 does not mean that the rowid has changed, only that |
| 2286 | ** it might have changed. Skip the conflict logic below if the rowid |
| 2287 | ** is unchanged. */ |
| 2288 | sqlite3VdbeAddOp3(v, OP_Eq, regNewData, addrRowidOk, regOldData); |
| 2289 | sqlite3VdbeChangeP5(v, SQLITE_NOTNULL); |
| 2290 | VdbeCoverage(v); |
| 2291 | } |
| 2292 | |
drh | f8ffb27 | 2013-11-01 17:08:56 | [diff] [blame] | 2293 | /* Check to see if the new rowid already exists in the table. Skip |
| 2294 | ** the following conflict logic if it does not. */ |
drh | 7f5f306 | 2018-04-17 20:06:24 | [diff] [blame] | 2295 | VdbeNoopComment((v, "uniqueness check for ROWID")); |
drh | 4031baf | 2018-05-28 17:31:20 | [diff] [blame] | 2296 | sqlite3VdbeVerifyAbortable(v, onError); |
drh | 6934fc7 | 2013-10-31 15:37:49 | [diff] [blame] | 2297 | sqlite3VdbeAddOp3(v, OP_NotExists, iDataCur, addrRowidOk, regNewData); |
drh | 688852a | 2014-02-17 22:40:43 | [diff] [blame] | 2298 | VdbeCoverage(v); |
drh | f8ffb27 | 2013-11-01 17:08:56 | [diff] [blame] | 2299 | |
dan | e1e2ae2 | 2009-09-24 16:52:28 | [diff] [blame] | 2300 | switch( onError ){ |
| 2301 | default: { |
| 2302 | onError = OE_Abort; |
drh | 08b9208 | 2020-08-10 14:18:00 | [diff] [blame] | 2303 | /* no break */ deliberate_fall_through |
drh | 79b0c95 | 2002-05-21 12:56:43 | [diff] [blame] | 2304 | } |
dan | e1e2ae2 | 2009-09-24 16:52:28 | [diff] [blame] | 2305 | case OE_Rollback: |
| 2306 | case OE_Abort: |
| 2307 | case OE_Fail: { |
drh | 9916048 | 2018-04-18 01:34:39 | [diff] [blame] | 2308 | testcase( onError==OE_Rollback ); |
| 2309 | testcase( onError==OE_Abort ); |
| 2310 | testcase( onError==OE_Fail ); |
drh | f9c8ce3 | 2013-11-05 13:33:55 | [diff] [blame] | 2311 | sqlite3RowidConstraint(pParse, onError, pTab); |
dan | e1e2ae2 | 2009-09-24 16:52:28 | [diff] [blame] | 2312 | break; |
drh | 0ca3e24 | 2002-01-29 23:07:02 | [diff] [blame] | 2313 | } |
dan | e1e2ae2 | 2009-09-24 16:52:28 | [diff] [blame] | 2314 | case OE_Replace: { |
| 2315 | /* If there are DELETE triggers on this table and the |
| 2316 | ** recursive-triggers flag is set, call GenerateRowDelete() to |
mistachkin | d557843 | 2012-08-25 10:01:29 | [diff] [blame] | 2317 | ** remove the conflicting row from the table. This will fire |
dan | e1e2ae2 | 2009-09-24 16:52:28 | [diff] [blame] | 2318 | ** the triggers and remove both the table and index b-tree entries. |
| 2319 | ** |
| 2320 | ** Otherwise, if there are no triggers or the recursive-triggers |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 2321 | ** flag is not set, but the table has one or more indexes, call |
| 2322 | ** GenerateRowIndexDelete(). This removes the index b-tree entries |
| 2323 | ** only. The table b-tree entry will be replaced by the new entry |
| 2324 | ** when it is inserted. |
dan | da730f6 | 2010-02-18 08:19:19 | [diff] [blame] | 2325 | ** |
| 2326 | ** If either GenerateRowDelete() or GenerateRowIndexDelete() is called, |
| 2327 | ** also invoke MultiWrite() to indicate that this VDBE may require |
| 2328 | ** statement rollback (if the statement is aborted after the delete |
| 2329 | ** takes place). Earlier versions called sqlite3MultiWrite() regardless, |
| 2330 | ** but being more selective here allows statements like: |
| 2331 | ** |
| 2332 | ** REPLACE INTO t(rowid) VALUES($newrowid) |
| 2333 | ** |
| 2334 | ** to run without a statement journal if there are no indexes on the |
| 2335 | ** table. |
| 2336 | */ |
drh | a407ecc | 2019-10-26 00:04:21 | [diff] [blame] | 2337 | if( regTrigCnt ){ |
dan | da730f6 | 2010-02-18 08:19:19 | [diff] [blame] | 2338 | sqlite3MultiWrite(pParse); |
drh | 26198bb | 2013-10-31 11:15:09 | [diff] [blame] | 2339 | sqlite3GenerateRowDelete(pParse, pTab, pTrigger, iDataCur, iIdxCur, |
dan | 438b881 | 2015-09-15 15:55:15 | [diff] [blame] | 2340 | regNewData, 1, 0, OE_Replace, 1, -1); |
drh | a407ecc | 2019-10-26 00:04:21 | [diff] [blame] | 2341 | sqlite3VdbeAddOp2(v, OP_AddImm, regTrigCnt, 1); /* incr trigger cnt */ |
| 2342 | nReplaceTrig++; |
dan | 46c47d4 | 2011-03-01 18:42:07 | [diff] [blame] | 2343 | }else{ |
drh | 9b1c62d | 2011-03-30 21:04:43 | [diff] [blame] | 2344 | #ifdef SQLITE_ENABLE_PREUPDATE_HOOK |
drh | 54f2cd9 | 2018-04-13 16:23:22 | [diff] [blame] | 2345 | assert( HasRowid(pTab) ); |
| 2346 | /* This OP_Delete opcode fires the pre-update-hook only. It does |
| 2347 | ** not modify the b-tree. It is more efficient to let the coming |
| 2348 | ** OP_Insert replace the existing entry than it is to delete the |
| 2349 | ** existing entry and then insert a new one. */ |
| 2350 | sqlite3VdbeAddOp2(v, OP_Delete, iDataCur, OPFLAG_ISNOOP); |
| 2351 | sqlite3VdbeAppendP4(v, pTab, P4_TABLE); |
drh | 9b1c62d | 2011-03-30 21:04:43 | [diff] [blame] | 2352 | #endif /* SQLITE_ENABLE_PREUPDATE_HOOK */ |
dan | 46c47d4 | 2011-03-01 18:42:07 | [diff] [blame] | 2353 | if( pTab->pIndex ){ |
| 2354 | sqlite3MultiWrite(pParse); |
drh | b77ebd8 | 2015-09-15 13:42:16 | [diff] [blame] | 2355 | sqlite3GenerateRowIndexDelete(pParse, pTab, iDataCur, iIdxCur,0,-1); |
dan | 46c47d4 | 2011-03-01 18:42:07 | [diff] [blame] | 2356 | } |
dan | e1e2ae2 | 2009-09-24 16:52:28 | [diff] [blame] | 2357 | } |
| 2358 | seenReplace = 1; |
| 2359 | break; |
drh | 5383ae5 | 2003-06-04 12:23:30 | [diff] [blame] | 2360 | } |
drh | 9eddaca | 2018-04-13 18:59:17 | [diff] [blame] | 2361 | #ifndef SQLITE_OMIT_UPSERT |
| 2362 | case OE_Update: { |
dan | 2cc0042 | 2018-04-17 18:16:10 | [diff] [blame] | 2363 | sqlite3UpsertDoUpdate(pParse, pUpsert, pTab, 0, iDataCur); |
drh | 08b9208 | 2020-08-10 14:18:00 | [diff] [blame] | 2364 | /* no break */ deliberate_fall_through |
drh | 9eddaca | 2018-04-13 18:59:17 | [diff] [blame] | 2365 | } |
| 2366 | #endif |
dan | e1e2ae2 | 2009-09-24 16:52:28 | [diff] [blame] | 2367 | case OE_Ignore: { |
drh | 9916048 | 2018-04-18 01:34:39 | [diff] [blame] | 2368 | testcase( onError==OE_Ignore ); |
drh | 076e85f | 2015-09-03 13:46:12 | [diff] [blame] | 2369 | sqlite3VdbeGoto(v, ignoreDest); |
dan | e1e2ae2 | 2009-09-24 16:52:28 | [diff] [blame] | 2370 | break; |
| 2371 | } |
| 2372 | } |
drh | 11e8527 | 2013-10-26 15:40:48 | [diff] [blame] | 2373 | sqlite3VdbeResolveLabel(v, addrRowidOk); |
drh | 61e280a | 2020-12-11 01:17:06 | [diff] [blame] | 2374 | if( pUpsert && pUpsertClause!=pUpsert ){ |
| 2375 | upsertIpkReturn = sqlite3VdbeAddOp0(v, OP_Goto); |
| 2376 | }else if( ipkTop ){ |
drh | 8430450 | 2018-08-14 15:12:52 | [diff] [blame] | 2377 | ipkBottom = sqlite3VdbeAddOp0(v, OP_Goto); |
| 2378 | sqlite3VdbeJumpHere(v, ipkTop-1); |
drh | 5383ae5 | 2003-06-04 12:23:30 | [diff] [blame] | 2379 | } |
drh | 0ca3e24 | 2002-01-29 23:07:02 | [diff] [blame] | 2380 | } |
drh | 0bd1f4e | 2002-06-06 18:54:39 | [diff] [blame] | 2381 | |
| 2382 | /* Test all UNIQUE constraints by creating entries for each UNIQUE |
| 2383 | ** index and making sure that duplicate entries do not already exist. |
drh | 11e8527 | 2013-10-26 15:40:48 | [diff] [blame] | 2384 | ** Compute the revised record entries for indices as we go. |
drh | f8ffb27 | 2013-11-01 17:08:56 | [diff] [blame] | 2385 | ** |
| 2386 | ** This loop also handles the case of the PRIMARY KEY index for a |
| 2387 | ** WITHOUT ROWID table. |
drh | 0bd1f4e | 2002-06-06 18:54:39 | [diff] [blame] | 2388 | */ |
drh | 61e280a | 2020-12-11 01:17:06 | [diff] [blame] | 2389 | for(pIdx = indexIteratorFirst(&sIdxIter, &ix); |
drh | daf2761 | 2020-12-10 20:31:25 | [diff] [blame] | 2390 | pIdx; |
drh | 61e280a | 2020-12-11 01:17:06 | [diff] [blame] | 2391 | pIdx = indexIteratorNext(&sIdxIter, &ix) |
drh | daf2761 | 2020-12-10 20:31:25 | [diff] [blame] | 2392 | ){ |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 2393 | int regIdx; /* Range of registers holding content for pIdx */ |
drh | 6934fc7 | 2013-10-31 15:37:49 | [diff] [blame] | 2394 | int regR; /* Range of registers holding conflicting PK */ |
| 2395 | int iThisCur; /* Cursor for this UNIQUE index */ |
| 2396 | int addrUniqueOk; /* Jump here if the UNIQUE constraint is satisfied */ |
drh | a407ecc | 2019-10-26 00:04:21 | [diff] [blame] | 2397 | int addrConflictCk; /* First opcode in the conflict check logic */ |
drh | 2184fc7 | 2011-04-09 03:04:13 | [diff] [blame] | 2398 | |
drh | 26198bb | 2013-10-31 11:15:09 | [diff] [blame] | 2399 | if( aRegIdx[ix]==0 ) continue; /* Skip indices that do not change */ |
drh | 61e280a | 2020-12-11 01:17:06 | [diff] [blame] | 2400 | if( pUpsert ){ |
| 2401 | pUpsertClause = sqlite3UpsertOfIndex(pUpsert, pIdx); |
| 2402 | if( upsertIpkDelay && pUpsertClause==pUpsert ){ |
| 2403 | sqlite3VdbeJumpHere(v, upsertIpkDelay); |
| 2404 | } |
drh | 7f5f306 | 2018-04-17 20:06:24 | [diff] [blame] | 2405 | } |
drh | 61e280a | 2020-12-11 01:17:06 | [diff] [blame] | 2406 | addrUniqueOk = sqlite3VdbeMakeLabel(pParse); |
| 2407 | if( bAffinityDone==0 ){ |
drh | 8430450 | 2018-08-14 15:12:52 | [diff] [blame] | 2408 | sqlite3TableAffinity(v, pTab, regNewData+1); |
| 2409 | bAffinityDone = 1; |
| 2410 | } |
drh | 8e50d65 | 2020-05-23 17:56:49 | [diff] [blame] | 2411 | VdbeNoopComment((v, "prep index %s", pIdx->zName)); |
drh | 6934fc7 | 2013-10-31 15:37:49 | [diff] [blame] | 2412 | iThisCur = iIdxCur+ix; |
drh | 7f5f306 | 2018-04-17 20:06:24 | [diff] [blame] | 2413 | |
drh | b2fe7d8 | 2003-04-20 17:29:23 | [diff] [blame] | 2414 | |
drh | f8ffb27 | 2013-11-01 17:08:56 | [diff] [blame] | 2415 | /* Skip partial indices for which the WHERE clause is not true */ |
drh | b2b9d3d | 2013-08-01 01:14:43 | [diff] [blame] | 2416 | if( pIdx->pPartIdxWhere ){ |
drh | 26198bb | 2013-10-31 11:15:09 | [diff] [blame] | 2417 | sqlite3VdbeAddOp2(v, OP_Null, 0, aRegIdx[ix]); |
drh | 6e97f8e | 2017-07-20 13:17:08 | [diff] [blame] | 2418 | pParse->iSelfTab = -(regNewData+1); |
drh | 72bc820 | 2015-06-11 13:58:35 | [diff] [blame] | 2419 | sqlite3ExprIfFalseDup(pParse, pIdx->pPartIdxWhere, addrUniqueOk, |
| 2420 | SQLITE_JUMPIFNULL); |
drh | 6e97f8e | 2017-07-20 13:17:08 | [diff] [blame] | 2421 | pParse->iSelfTab = 0; |
drh | b2b9d3d | 2013-08-01 01:14:43 | [diff] [blame] | 2422 | } |
| 2423 | |
drh | 6934fc7 | 2013-10-31 15:37:49 | [diff] [blame] | 2424 | /* Create a record for this index entry as it should appear after |
drh | f8ffb27 | 2013-11-01 17:08:56 | [diff] [blame] | 2425 | ** the insert or update. Store that record in the aRegIdx[ix] register |
| 2426 | */ |
drh | bf2f573 | 2016-11-10 16:07:43 | [diff] [blame] | 2427 | regIdx = aRegIdx[ix]+1; |
drh | 9cfcf5d | 2002-01-29 18:41:24 | [diff] [blame] | 2428 | for(i=0; i<pIdx->nColumn; i++){ |
drh | 6934fc7 | 2013-10-31 15:37:49 | [diff] [blame] | 2429 | int iField = pIdx->aiColumn[i]; |
drh | f82b9af | 2013-11-01 14:03:20 | [diff] [blame] | 2430 | int x; |
drh | 4b92f98 | 2015-09-29 17:20:14 | [diff] [blame] | 2431 | if( iField==XN_EXPR ){ |
drh | 6e97f8e | 2017-07-20 13:17:08 | [diff] [blame] | 2432 | pParse->iSelfTab = -(regNewData+1); |
drh | 1c75c9d | 2015-12-21 15:22:13 | [diff] [blame] | 2433 | sqlite3ExprCodeCopy(pParse, pIdx->aColExpr->a[i].pExpr, regIdx+i); |
drh | 6e97f8e | 2017-07-20 13:17:08 | [diff] [blame] | 2434 | pParse->iSelfTab = 0; |
drh | 1f9ca2c | 2015-08-25 16:57:52 | [diff] [blame] | 2435 | VdbeComment((v, "%s column %d", pIdx->zName, i)); |
drh | 463e76f | 2019-10-18 10:05:06 | [diff] [blame] | 2436 | }else if( iField==XN_ROWID || iField==pTab->iPKey ){ |
| 2437 | x = regNewData; |
| 2438 | sqlite3VdbeAddOp2(v, OP_IntCopy, x, regIdx+i); |
| 2439 | VdbeComment((v, "rowid")); |
drh | 9cfcf5d | 2002-01-29 18:41:24 | [diff] [blame] | 2440 | }else{ |
drh | c5f808d | 2019-10-19 15:01:52 | [diff] [blame] | 2441 | testcase( sqlite3TableColumnToStorage(pTab, iField)!=iField ); |
drh | b9bcf7c | 2019-10-19 13:29:10 | [diff] [blame] | 2442 | x = sqlite3TableColumnToStorage(pTab, iField) + regNewData + 1; |
drh | 463e76f | 2019-10-18 10:05:06 | [diff] [blame] | 2443 | sqlite3VdbeAddOp2(v, OP_SCopy, x, regIdx+i); |
drh | cf9d36d | 2021-08-02 18:03:43 | [diff] [blame] | 2444 | VdbeComment((v, "%s", pTab->aCol[iField].zCnName)); |
drh | 9cfcf5d | 2002-01-29 18:41:24 | [diff] [blame] | 2445 | } |
| 2446 | } |
drh | 26198bb | 2013-10-31 11:15:09 | [diff] [blame] | 2447 | sqlite3VdbeAddOp3(v, OP_MakeRecord, regIdx, pIdx->nColumn, aRegIdx[ix]); |
drh | 26198bb | 2013-10-31 11:15:09 | [diff] [blame] | 2448 | VdbeComment((v, "for %s", pIdx->zName)); |
drh | 7e4acf7 | 2017-02-14 20:00:16 | [diff] [blame] | 2449 | #ifdef SQLITE_ENABLE_NULL_TRIM |
drh | 9df385e | 2019-02-19 13:08:35 | [diff] [blame] | 2450 | if( pIdx->idxType==SQLITE_IDXTYPE_PRIMARYKEY ){ |
| 2451 | sqlite3SetMakeRecordP5(v, pIdx->pTable); |
| 2452 | } |
drh | 7e4acf7 | 2017-02-14 20:00:16 | [diff] [blame] | 2453 | #endif |
drh | 3aef2fb | 2020-01-02 17:46:02 | [diff] [blame] | 2454 | sqlite3VdbeReleaseRegisters(pParse, regIdx, pIdx->nColumn, 0, 0); |
drh | b2fe7d8 | 2003-04-20 17:29:23 | [diff] [blame] | 2455 | |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 2456 | /* In an UPDATE operation, if this index is the PRIMARY KEY index |
drh | f8ffb27 | 2013-11-01 17:08:56 | [diff] [blame] | 2457 | ** of a WITHOUT ROWID table and there has been no change the |
| 2458 | ** primary key, then no collision is possible. The collision detection |
| 2459 | ** logic below can all be skipped. */ |
drh | 00012df | 2013-11-05 01:59:07 | [diff] [blame] | 2460 | if( isUpdate && pPk==pIdx && pkChng==0 ){ |
drh | da475b8 | 2013-11-04 21:44:54 | [diff] [blame] | 2461 | sqlite3VdbeResolveLabel(v, addrUniqueOk); |
| 2462 | continue; |
| 2463 | } |
drh | f8ffb27 | 2013-11-01 17:08:56 | [diff] [blame] | 2464 | |
drh | 6934fc7 | 2013-10-31 15:37:49 | [diff] [blame] | 2465 | /* Find out what action to take in case there is a uniqueness conflict */ |
drh | 9cfcf5d | 2002-01-29 18:41:24 | [diff] [blame] | 2466 | onError = pIdx->onError; |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 2467 | if( onError==OE_None ){ |
drh | 11e8527 | 2013-10-26 15:40:48 | [diff] [blame] | 2468 | sqlite3VdbeResolveLabel(v, addrUniqueOk); |
danielk1977 | de63035 | 2009-05-04 11:42:29 | [diff] [blame] | 2469 | continue; /* pIdx is not a UNIQUE index */ |
| 2470 | } |
drh | 9cfcf5d | 2002-01-29 18:41:24 | [diff] [blame] | 2471 | if( overrideError!=OE_Default ){ |
| 2472 | onError = overrideError; |
drh | a996e47 | 2003-05-16 02:30:27 | [diff] [blame] | 2473 | }else if( onError==OE_Default ){ |
| 2474 | onError = OE_Abort; |
drh | 9cfcf5d | 2002-01-29 18:41:24 | [diff] [blame] | 2475 | } |
drh | c6c9e15 | 2016-11-10 17:01:36 | [diff] [blame] | 2476 | |
drh | c8a0c90 | 2018-04-13 15:14:33 | [diff] [blame] | 2477 | /* Figure out if the upsert clause applies to this index */ |
drh | 61e280a | 2020-12-11 01:17:06 | [diff] [blame] | 2478 | if( pUpsertClause ){ |
drh | 255c1c1 | 2020-12-12 00:28:15 | [diff] [blame] | 2479 | if( pUpsertClause->isDoUpdate==0 ){ |
drh | c8a0c90 | 2018-04-13 15:14:33 | [diff] [blame] | 2480 | onError = OE_Ignore; /* DO NOTHING is the same as INSERT OR IGNORE */ |
| 2481 | }else{ |
| 2482 | onError = OE_Update; /* DO UPDATE */ |
| 2483 | } |
| 2484 | } |
| 2485 | |
drh | 801f55d | 2017-01-04 22:02:56 | [diff] [blame] | 2486 | /* Collision detection may be omitted if all of the following are true: |
| 2487 | ** (1) The conflict resolution algorithm is REPLACE |
| 2488 | ** (2) The table is a WITHOUT ROWID table |
| 2489 | ** (3) There are no secondary indexes on the table |
| 2490 | ** (4) No delete triggers need to be fired if there is a conflict |
dan | f9a12a1 | 2017-01-05 06:57:42 | [diff] [blame] | 2491 | ** (5) No FK constraint counters need to be updated if a conflict occurs. |
dan | 418454c | 2019-01-07 15:57:35 | [diff] [blame] | 2492 | ** |
| 2493 | ** This is not possible for ENABLE_PREUPDATE_HOOK builds, as the row |
| 2494 | ** must be explicitly deleted in order to ensure any pre-update hook |
drh | 78b2fa8 | 2021-10-07 12:11:20 | [diff] [blame] | 2495 | ** is invoked. */ |
| 2496 | assert( IsOrdinaryTable(pTab) ); |
dan | 418454c | 2019-01-07 15:57:35 | [diff] [blame] | 2497 | #ifndef SQLITE_ENABLE_PREUPDATE_HOOK |
drh | 801f55d | 2017-01-04 22:02:56 | [diff] [blame] | 2498 | if( (ix==0 && pIdx->pNext==0) /* Condition 3 */ |
| 2499 | && pPk==pIdx /* Condition 2 */ |
| 2500 | && onError==OE_Replace /* Condition 1 */ |
| 2501 | && ( 0==(db->flags&SQLITE_RecTriggers) || /* Condition 4 */ |
| 2502 | 0==sqlite3TriggersExist(pParse, pTab, TK_DELETE, 0, 0)) |
dan | f9a12a1 | 2017-01-05 06:57:42 | [diff] [blame] | 2503 | && ( 0==(db->flags&SQLITE_ForeignKeys) || /* Condition 5 */ |
drh | f38524d | 2021-08-02 16:41:57 | [diff] [blame] | 2504 | (0==pTab->u.tab.pFKey && 0==sqlite3FkReferences(pTab))) |
drh | 801f55d | 2017-01-04 22:02:56 | [diff] [blame] | 2505 | ){ |
| 2506 | sqlite3VdbeResolveLabel(v, addrUniqueOk); |
| 2507 | continue; |
drh | c6c9e15 | 2016-11-10 17:01:36 | [diff] [blame] | 2508 | } |
dan | 418454c | 2019-01-07 15:57:35 | [diff] [blame] | 2509 | #endif /* ifndef SQLITE_ENABLE_PREUPDATE_HOOK */ |
drh | c6c9e15 | 2016-11-10 17:01:36 | [diff] [blame] | 2510 | |
drh | b2fe7d8 | 2003-04-20 17:29:23 | [diff] [blame] | 2511 | /* Check to see if the new index entry will be unique */ |
drh | 4031baf | 2018-05-28 17:31:20 | [diff] [blame] | 2512 | sqlite3VdbeVerifyAbortable(v, onError); |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 2513 | addrConflictCk = |
drh | a407ecc | 2019-10-26 00:04:21 | [diff] [blame] | 2514 | sqlite3VdbeAddOp4Int(v, OP_NoConflict, iThisCur, addrUniqueOk, |
| 2515 | regIdx, pIdx->nKeyCol); VdbeCoverage(v); |
drh | f8ffb27 | 2013-11-01 17:08:56 | [diff] [blame] | 2516 | |
| 2517 | /* Generate code to handle collisions */ |
drh | d3e21a1 | 2020-12-11 17:11:56 | [diff] [blame] | 2518 | regR = pIdx==pPk ? regIdx : sqlite3GetTempRange(pParse, nPkField); |
drh | 46d03fc | 2013-12-19 02:23:45 | [diff] [blame] | 2519 | if( isUpdate || onError==OE_Replace ){ |
| 2520 | if( HasRowid(pTab) ){ |
| 2521 | sqlite3VdbeAddOp2(v, OP_IdxRowid, iThisCur, regR); |
| 2522 | /* Conflict only if the rowid of the existing index entry |
| 2523 | ** is different from old-rowid */ |
| 2524 | if( isUpdate ){ |
| 2525 | sqlite3VdbeAddOp3(v, OP_Eq, regR, addrUniqueOk, regOldData); |
drh | 3d77dee | 2014-02-19 14:20:49 | [diff] [blame] | 2526 | sqlite3VdbeChangeP5(v, SQLITE_NOTNULL); |
drh | 688852a | 2014-02-17 22:40:43 | [diff] [blame] | 2527 | VdbeCoverage(v); |
drh | da475b8 | 2013-11-04 21:44:54 | [diff] [blame] | 2528 | } |
drh | 46d03fc | 2013-12-19 02:23:45 | [diff] [blame] | 2529 | }else{ |
| 2530 | int x; |
| 2531 | /* Extract the PRIMARY KEY from the end of the index entry and |
| 2532 | ** store it in registers regR..regR+nPk-1 */ |
drh | a021f12 | 2013-12-19 14:34:34 | [diff] [blame] | 2533 | if( pIdx!=pPk ){ |
drh | 46d03fc | 2013-12-19 02:23:45 | [diff] [blame] | 2534 | for(i=0; i<pPk->nKeyCol; i++){ |
drh | 4b92f98 | 2015-09-29 17:20:14 | [diff] [blame] | 2535 | assert( pPk->aiColumn[i]>=0 ); |
drh | b9bcf7c | 2019-10-19 13:29:10 | [diff] [blame] | 2536 | x = sqlite3TableColumnToIndex(pIdx, pPk->aiColumn[i]); |
drh | 46d03fc | 2013-12-19 02:23:45 | [diff] [blame] | 2537 | sqlite3VdbeAddOp3(v, OP_Column, iThisCur, x, regR+i); |
| 2538 | VdbeComment((v, "%s.%s", pTab->zName, |
drh | cf9d36d | 2021-08-02 18:03:43 | [diff] [blame] | 2539 | pTab->aCol[pPk->aiColumn[i]].zCnName)); |
drh | da475b8 | 2013-11-04 21:44:54 | [diff] [blame] | 2540 | } |
drh | 46d03fc | 2013-12-19 02:23:45 | [diff] [blame] | 2541 | } |
| 2542 | if( isUpdate ){ |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 2543 | /* If currently processing the PRIMARY KEY of a WITHOUT ROWID |
drh | 46d03fc | 2013-12-19 02:23:45 | [diff] [blame] | 2544 | ** table, only conflict if the new PRIMARY KEY values are actually |
drh | 5a1f761 | 2022-03-11 15:42:05 | [diff] [blame] | 2545 | ** different from the old. See TH3 withoutrowid04.test. |
drh | 46d03fc | 2013-12-19 02:23:45 | [diff] [blame] | 2546 | ** |
| 2547 | ** For a UNIQUE index, only conflict if the PRIMARY KEY values |
| 2548 | ** of the matched index row are different from the original PRIMARY |
| 2549 | ** KEY values of this row before the update. */ |
| 2550 | int addrJump = sqlite3VdbeCurrentAddr(v)+pPk->nKeyCol; |
| 2551 | int op = OP_Ne; |
drh | 48dd1d8 | 2014-05-27 18:18:58 | [diff] [blame] | 2552 | int regCmp = (IsPrimaryKeyIndex(pIdx) ? regIdx : regR); |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 2553 | |
drh | 46d03fc | 2013-12-19 02:23:45 | [diff] [blame] | 2554 | for(i=0; i<pPk->nKeyCol; i++){ |
| 2555 | char *p4 = (char*)sqlite3LocateCollSeq(pParse, pPk->azColl[i]); |
| 2556 | x = pPk->aiColumn[i]; |
drh | 4b92f98 | 2015-09-29 17:20:14 | [diff] [blame] | 2557 | assert( x>=0 ); |
drh | 46d03fc | 2013-12-19 02:23:45 | [diff] [blame] | 2558 | if( i==(pPk->nKeyCol-1) ){ |
| 2559 | addrJump = addrUniqueOk; |
| 2560 | op = OP_Eq; |
| 2561 | } |
drh | b6d861e | 2019-10-29 03:30:26 | [diff] [blame] | 2562 | x = sqlite3TableColumnToStorage(pTab, x); |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 2563 | sqlite3VdbeAddOp4(v, op, |
drh | 46d03fc | 2013-12-19 02:23:45 | [diff] [blame] | 2564 | regOldData+1+x, addrJump, regCmp+i, p4, P4_COLLSEQ |
| 2565 | ); |
drh | 3d77dee | 2014-02-19 14:20:49 | [diff] [blame] | 2566 | sqlite3VdbeChangeP5(v, SQLITE_NOTNULL); |
| 2567 | VdbeCoverageIf(v, op==OP_Eq); |
| 2568 | VdbeCoverageIf(v, op==OP_Ne); |
drh | 46d03fc | 2013-12-19 02:23:45 | [diff] [blame] | 2569 | } |
drh | 26198bb | 2013-10-31 11:15:09 | [diff] [blame] | 2570 | } |
drh | 26198bb | 2013-10-31 11:15:09 | [diff] [blame] | 2571 | } |
drh | 11e8527 | 2013-10-26 15:40:48 | [diff] [blame] | 2572 | } |
drh | b2fe7d8 | 2003-04-20 17:29:23 | [diff] [blame] | 2573 | |
| 2574 | /* Generate code that executes if the new index entry is not unique */ |
danielk1977 | b84f96f | 2005-01-20 11:32:23 | [diff] [blame] | 2575 | assert( onError==OE_Rollback || onError==OE_Abort || onError==OE_Fail |
drh | 9eddaca | 2018-04-13 18:59:17 | [diff] [blame] | 2576 | || onError==OE_Ignore || onError==OE_Replace || onError==OE_Update ); |
drh | 9cfcf5d | 2002-01-29 18:41:24 | [diff] [blame] | 2577 | switch( onError ){ |
drh | 1c92853 | 2002-01-31 15:54:21 | [diff] [blame] | 2578 | case OE_Rollback: |
| 2579 | case OE_Abort: |
| 2580 | case OE_Fail: { |
drh | 9916048 | 2018-04-18 01:34:39 | [diff] [blame] | 2581 | testcase( onError==OE_Rollback ); |
| 2582 | testcase( onError==OE_Abort ); |
| 2583 | testcase( onError==OE_Fail ); |
drh | f9c8ce3 | 2013-11-05 13:33:55 | [diff] [blame] | 2584 | sqlite3UniqueConstraint(pParse, onError, pIdx); |
drh | 9cfcf5d | 2002-01-29 18:41:24 | [diff] [blame] | 2585 | break; |
| 2586 | } |
drh | 9eddaca | 2018-04-13 18:59:17 | [diff] [blame] | 2587 | #ifndef SQLITE_OMIT_UPSERT |
| 2588 | case OE_Update: { |
dan | 2cc0042 | 2018-04-17 18:16:10 | [diff] [blame] | 2589 | sqlite3UpsertDoUpdate(pParse, pUpsert, pTab, pIdx, iIdxCur+ix); |
drh | 08b9208 | 2020-08-10 14:18:00 | [diff] [blame] | 2590 | /* no break */ deliberate_fall_through |
drh | 9eddaca | 2018-04-13 18:59:17 | [diff] [blame] | 2591 | } |
| 2592 | #endif |
drh | 9cfcf5d | 2002-01-29 18:41:24 | [diff] [blame] | 2593 | case OE_Ignore: { |
drh | 9916048 | 2018-04-18 01:34:39 | [diff] [blame] | 2594 | testcase( onError==OE_Ignore ); |
drh | 076e85f | 2015-09-03 13:46:12 | [diff] [blame] | 2595 | sqlite3VdbeGoto(v, ignoreDest); |
drh | 9cfcf5d | 2002-01-29 18:41:24 | [diff] [blame] | 2596 | break; |
| 2597 | } |
drh | 098d168 | 2009-05-02 15:46:46 | [diff] [blame] | 2598 | default: { |
drh | a407ecc | 2019-10-26 00:04:21 | [diff] [blame] | 2599 | int nConflictCk; /* Number of opcodes in conflict check logic */ |
| 2600 | |
drh | 098d168 | 2009-05-02 15:46:46 | [diff] [blame] | 2601 | assert( onError==OE_Replace ); |
drh | a407ecc | 2019-10-26 00:04:21 | [diff] [blame] | 2602 | nConflictCk = sqlite3VdbeCurrentAddr(v) - addrConflictCk; |
drh | 362c181 | 2021-10-30 18:17:59 | [diff] [blame] | 2603 | assert( nConflictCk>0 || db->mallocFailed ); |
| 2604 | testcase( nConflictCk<=0 ); |
drh | d3c468b | 2019-10-26 16:38:49 | [diff] [blame] | 2605 | testcase( nConflictCk>1 ); |
drh | a407ecc | 2019-10-26 00:04:21 | [diff] [blame] | 2606 | if( regTrigCnt ){ |
dan | fecfb31 | 2018-05-28 18:29:46 | [diff] [blame] | 2607 | sqlite3MultiWrite(pParse); |
drh | a407ecc | 2019-10-26 00:04:21 | [diff] [blame] | 2608 | nReplaceTrig++; |
dan | fecfb31 | 2018-05-28 18:29:46 | [diff] [blame] | 2609 | } |
drh | 7b14b65 | 2019-12-29 22:08:20 | [diff] [blame] | 2610 | if( pTrigger && isUpdate ){ |
| 2611 | sqlite3VdbeAddOp1(v, OP_CursorLock, iDataCur); |
| 2612 | } |
drh | 26198bb | 2013-10-31 11:15:09 | [diff] [blame] | 2613 | sqlite3GenerateRowDelete(pParse, pTab, pTrigger, iDataCur, iIdxCur, |
drh | b0264ee | 2015-09-14 14:45:50 | [diff] [blame] | 2614 | regR, nPkField, 0, OE_Replace, |
drh | 6811693 | 2017-01-07 14:47:03 | [diff] [blame] | 2615 | (pIdx==pPk ? ONEPASS_SINGLE : ONEPASS_OFF), iThisCur); |
drh | 7b14b65 | 2019-12-29 22:08:20 | [diff] [blame] | 2616 | if( pTrigger && isUpdate ){ |
| 2617 | sqlite3VdbeAddOp1(v, OP_CursorUnlock, iDataCur); |
| 2618 | } |
drh | a407ecc | 2019-10-26 00:04:21 | [diff] [blame] | 2619 | if( regTrigCnt ){ |
drh | a407ecc | 2019-10-26 00:04:21 | [diff] [blame] | 2620 | int addrBypass; /* Jump destination to bypass recheck logic */ |
| 2621 | |
| 2622 | sqlite3VdbeAddOp2(v, OP_AddImm, regTrigCnt, 1); /* incr trigger cnt */ |
| 2623 | addrBypass = sqlite3VdbeAddOp0(v, OP_Goto); /* Bypass recheck */ |
| 2624 | VdbeComment((v, "bypass recheck")); |
| 2625 | |
| 2626 | /* Here we insert code that will be invoked after all constraint |
| 2627 | ** checks have run, if and only if one or more replace triggers |
| 2628 | ** fired. */ |
| 2629 | sqlite3VdbeResolveLabel(v, lblRecheckOk); |
| 2630 | lblRecheckOk = sqlite3VdbeMakeLabel(pParse); |
| 2631 | if( pIdx->pPartIdxWhere ){ |
| 2632 | /* Bypass the recheck if this partial index is not defined |
| 2633 | ** for the current row */ |
drh | 0660884 | 2019-10-26 01:43:14 | [diff] [blame] | 2634 | sqlite3VdbeAddOp2(v, OP_IsNull, regIdx-1, lblRecheckOk); |
drh | a407ecc | 2019-10-26 00:04:21 | [diff] [blame] | 2635 | VdbeCoverage(v); |
| 2636 | } |
| 2637 | /* Copy the constraint check code from above, except change |
| 2638 | ** the constraint-ok jump destination to be the address of |
| 2639 | ** the next retest block */ |
drh | d3c468b | 2019-10-26 16:38:49 | [diff] [blame] | 2640 | while( nConflictCk>0 ){ |
drh | d901b16 | 2019-10-26 12:27:55 | [diff] [blame] | 2641 | VdbeOp x; /* Conflict check opcode to copy */ |
| 2642 | /* The sqlite3VdbeAddOp4() call might reallocate the opcode array. |
| 2643 | ** Hence, make a complete copy of the opcode, rather than using |
| 2644 | ** a pointer to the opcode. */ |
| 2645 | x = *sqlite3VdbeGetOp(v, addrConflictCk); |
| 2646 | if( x.opcode!=OP_IdxRowid ){ |
| 2647 | int p2; /* New P2 value for copied conflict check opcode */ |
drh | b9f2e5f | 2020-01-28 15:02:23 | [diff] [blame] | 2648 | const char *zP4; |
drh | d901b16 | 2019-10-26 12:27:55 | [diff] [blame] | 2649 | if( sqlite3OpcodeProperty[x.opcode]&OPFLG_JUMP ){ |
| 2650 | p2 = lblRecheckOk; |
| 2651 | }else{ |
| 2652 | p2 = x.p2; |
| 2653 | } |
drh | b9f2e5f | 2020-01-28 15:02:23 | [diff] [blame] | 2654 | zP4 = x.p4type==P4_INT32 ? SQLITE_INT_TO_PTR(x.p4.i) : x.p4.z; |
| 2655 | sqlite3VdbeAddOp4(v, x.opcode, x.p1, p2, x.p3, zP4, x.p4type); |
drh | d901b16 | 2019-10-26 12:27:55 | [diff] [blame] | 2656 | sqlite3VdbeChangeP5(v, x.p5); |
| 2657 | VdbeCoverageIf(v, p2!=x.p2); |
drh | a407ecc | 2019-10-26 00:04:21 | [diff] [blame] | 2658 | } |
| 2659 | nConflictCk--; |
drh | d901b16 | 2019-10-26 12:27:55 | [diff] [blame] | 2660 | addrConflictCk++; |
drh | a407ecc | 2019-10-26 00:04:21 | [diff] [blame] | 2661 | } |
| 2662 | /* If the retest fails, issue an abort */ |
drh | 2da8d6f | 2019-10-16 14:56:03 | [diff] [blame] | 2663 | sqlite3UniqueConstraint(pParse, OE_Abort, pIdx); |
drh | a407ecc | 2019-10-26 00:04:21 | [diff] [blame] | 2664 | |
| 2665 | sqlite3VdbeJumpHere(v, addrBypass); /* Terminate the recheck bypass */ |
drh | 2da8d6f | 2019-10-16 14:56:03 | [diff] [blame] | 2666 | } |
drh | 0ca3e24 | 2002-01-29 23:07:02 | [diff] [blame] | 2667 | seenReplace = 1; |
drh | 9cfcf5d | 2002-01-29 18:41:24 | [diff] [blame] | 2668 | break; |
| 2669 | } |
drh | 9cfcf5d | 2002-01-29 18:41:24 | [diff] [blame] | 2670 | } |
drh | 61e280a | 2020-12-11 01:17:06 | [diff] [blame] | 2671 | sqlite3VdbeResolveLabel(v, addrUniqueOk); |
drh | 392ee21 | 2013-11-08 16:54:56 | [diff] [blame] | 2672 | if( regR!=regIdx ) sqlite3ReleaseTempRange(pParse, regR, nPkField); |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 2673 | if( pUpsertClause |
drh | ed4c546 | 2020-12-11 16:49:51 | [diff] [blame] | 2674 | && upsertIpkReturn |
| 2675 | && sqlite3UpsertNextIsIPK(pUpsertClause) |
| 2676 | ){ |
drh | 61e280a | 2020-12-11 01:17:06 | [diff] [blame] | 2677 | sqlite3VdbeGoto(v, upsertIpkDelay+1); |
| 2678 | sqlite3VdbeJumpHere(v, upsertIpkReturn); |
drh | 58b18a4 | 2020-12-11 19:36:19 | [diff] [blame] | 2679 | upsertIpkReturn = 0; |
drh | 61e280a | 2020-12-11 01:17:06 | [diff] [blame] | 2680 | } |
drh | 9cfcf5d | 2002-01-29 18:41:24 | [diff] [blame] | 2681 | } |
drh | 8430450 | 2018-08-14 15:12:52 | [diff] [blame] | 2682 | |
| 2683 | /* If the IPK constraint is a REPLACE, run it last */ |
| 2684 | if( ipkTop ){ |
drh | 6214d93 | 2019-01-12 16:19:23 | [diff] [blame] | 2685 | sqlite3VdbeGoto(v, ipkTop); |
drh | 8430450 | 2018-08-14 15:12:52 | [diff] [blame] | 2686 | VdbeComment((v, "Do IPK REPLACE")); |
drh | 66306d8 | 2021-12-30 02:38:43 | [diff] [blame] | 2687 | assert( ipkBottom>0 ); |
drh | 8430450 | 2018-08-14 15:12:52 | [diff] [blame] | 2688 | sqlite3VdbeJumpHere(v, ipkBottom); |
| 2689 | } |
| 2690 | |
drh | a407ecc | 2019-10-26 00:04:21 | [diff] [blame] | 2691 | /* Recheck all uniqueness constraints after replace triggers have run */ |
| 2692 | testcase( regTrigCnt!=0 && nReplaceTrig==0 ); |
drh | d3c468b | 2019-10-26 16:38:49 | [diff] [blame] | 2693 | assert( regTrigCnt!=0 || nReplaceTrig==0 ); |
drh | a407ecc | 2019-10-26 00:04:21 | [diff] [blame] | 2694 | if( nReplaceTrig ){ |
| 2695 | sqlite3VdbeAddOp2(v, OP_IfNot, regTrigCnt, lblRecheckOk);VdbeCoverage(v); |
| 2696 | if( !pPk ){ |
| 2697 | if( isUpdate ){ |
| 2698 | sqlite3VdbeAddOp3(v, OP_Eq, regNewData, addrRecheck, regOldData); |
| 2699 | sqlite3VdbeChangeP5(v, SQLITE_NOTNULL); |
| 2700 | VdbeCoverage(v); |
| 2701 | } |
| 2702 | sqlite3VdbeAddOp3(v, OP_NotExists, iDataCur, addrRecheck, regNewData); |
| 2703 | VdbeCoverage(v); |
| 2704 | sqlite3RowidConstraint(pParse, OE_Abort, pTab); |
| 2705 | }else{ |
| 2706 | sqlite3VdbeGoto(v, addrRecheck); |
| 2707 | } |
| 2708 | sqlite3VdbeResolveLabel(v, lblRecheckOk); |
| 2709 | } |
| 2710 | |
drh | a7c3b93 | 2019-05-07 19:13:42 | [diff] [blame] | 2711 | /* Generate the table record */ |
| 2712 | if( HasRowid(pTab) ){ |
| 2713 | int regRec = aRegIdx[ix]; |
drh | 0b0b3a9 | 2019-10-17 18:35:57 | [diff] [blame] | 2714 | sqlite3VdbeAddOp3(v, OP_MakeRecord, regNewData+1, pTab->nNVCol, regRec); |
drh | a7c3b93 | 2019-05-07 19:13:42 | [diff] [blame] | 2715 | sqlite3SetMakeRecordP5(v, pTab); |
| 2716 | if( !bAffinityDone ){ |
| 2717 | sqlite3TableAffinity(v, pTab, 0); |
| 2718 | } |
| 2719 | } |
| 2720 | |
drh | 4308e34 | 2013-11-11 16:55:52 | [diff] [blame] | 2721 | *pbMayReplace = seenReplace; |
drh | ce60aa4 | 2013-11-08 01:09:15 | [diff] [blame] | 2722 | VdbeModuleComment((v, "END: GenCnstCks(%d)", seenReplace)); |
drh | 9cfcf5d | 2002-01-29 18:41:24 | [diff] [blame] | 2723 | } |
drh | 0ca3e24 | 2002-01-29 23:07:02 | [diff] [blame] | 2724 | |
drh | d447dce | 2017-01-25 20:55:11 | [diff] [blame] | 2725 | #ifdef SQLITE_ENABLE_NULL_TRIM |
drh | 0ca3e24 | 2002-01-29 23:07:02 | [diff] [blame] | 2726 | /* |
drh | 585ce19 | 2017-01-25 14:58:27 | [diff] [blame] | 2727 | ** Change the P5 operand on the last opcode (which should be an OP_MakeRecord) |
| 2728 | ** to be the number of columns in table pTab that must not be NULL-trimmed. |
| 2729 | ** |
| 2730 | ** Or if no columns of pTab may be NULL-trimmed, leave P5 at zero. |
| 2731 | */ |
| 2732 | void sqlite3SetMakeRecordP5(Vdbe *v, Table *pTab){ |
| 2733 | u16 i; |
| 2734 | |
| 2735 | /* Records with omitted columns are only allowed for schema format |
| 2736 | ** version 2 and later (SQLite version 3.1.4, 2005-02-20). */ |
| 2737 | if( pTab->pSchema->file_format<2 ) return; |
| 2738 | |
drh | 7e4acf7 | 2017-02-14 20:00:16 | [diff] [blame] | 2739 | for(i=pTab->nCol-1; i>0; i--){ |
drh | 79cf2b7 | 2021-07-31 20:30:41 | [diff] [blame] | 2740 | if( pTab->aCol[i].iDflt!=0 ) break; |
drh | 7e4acf7 | 2017-02-14 20:00:16 | [diff] [blame] | 2741 | if( pTab->aCol[i].colFlags & COLFLAG_PRIMKEY ) break; |
| 2742 | } |
| 2743 | sqlite3VdbeChangeP5(v, i+1); |
drh | 585ce19 | 2017-01-25 14:58:27 | [diff] [blame] | 2744 | } |
drh | d447dce | 2017-01-25 20:55:11 | [diff] [blame] | 2745 | #endif |
drh | 585ce19 | 2017-01-25 14:58:27 | [diff] [blame] | 2746 | |
drh | 0ca3e24 | 2002-01-29 23:07:02 | [diff] [blame] | 2747 | /* |
dan | fadc0e3 | 2021-02-18 12:18:10 | [diff] [blame] | 2748 | ** Table pTab is a WITHOUT ROWID table that is being written to. The cursor |
| 2749 | ** number is iCur, and register regData contains the new record for the |
| 2750 | ** PK index. This function adds code to invoke the pre-update hook, |
| 2751 | ** if one is registered. |
| 2752 | */ |
| 2753 | #ifdef SQLITE_ENABLE_PREUPDATE_HOOK |
| 2754 | static void codeWithoutRowidPreupdate( |
| 2755 | Parse *pParse, /* Parse context */ |
| 2756 | Table *pTab, /* Table being updated */ |
| 2757 | int iCur, /* Cursor number for table */ |
| 2758 | int regData /* Data containing new record */ |
| 2759 | ){ |
| 2760 | Vdbe *v = pParse->pVdbe; |
| 2761 | int r = sqlite3GetTempReg(pParse); |
| 2762 | assert( !HasRowid(pTab) ); |
drh | d01206f | 2021-03-21 18:23:48 | [diff] [blame] | 2763 | assert( 0==(pParse->db->mDbFlags & DBFLAG_Vacuum) || CORRUPT_DB ); |
dan | fadc0e3 | 2021-02-18 12:18:10 | [diff] [blame] | 2764 | sqlite3VdbeAddOp2(v, OP_Integer, 0, r); |
| 2765 | sqlite3VdbeAddOp4(v, OP_Insert, iCur, regData, r, (char*)pTab, P4_TABLE); |
| 2766 | sqlite3VdbeChangeP5(v, OPFLAG_ISNOOP); |
| 2767 | sqlite3ReleaseTempReg(pParse, r); |
| 2768 | } |
| 2769 | #else |
| 2770 | # define codeWithoutRowidPreupdate(a,b,c,d) |
| 2771 | #endif |
| 2772 | |
| 2773 | /* |
drh | 0ca3e24 | 2002-01-29 23:07:02 | [diff] [blame] | 2774 | ** This routine generates code to finish the INSERT or UPDATE operation |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 | [diff] [blame] | 2775 | ** that was started by a prior call to sqlite3GenerateConstraintChecks. |
drh | 6934fc7 | 2013-10-31 15:37:49 | [diff] [blame] | 2776 | ** A consecutive range of registers starting at regNewData contains the |
drh | 04adf41 | 2008-01-08 18:57:50 | [diff] [blame] | 2777 | ** rowid and the content to be inserted. |
drh | 0ca3e24 | 2002-01-29 23:07:02 | [diff] [blame] | 2778 | ** |
drh | b419a92 | 2002-01-30 16:17:23 | [diff] [blame] | 2779 | ** The arguments to this routine should be the same as the first six |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 | [diff] [blame] | 2780 | ** arguments to sqlite3GenerateConstraintChecks. |
drh | 0ca3e24 | 2002-01-29 23:07:02 | [diff] [blame] | 2781 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 | [diff] [blame] | 2782 | void sqlite3CompleteInsertion( |
drh | 0ca3e24 | 2002-01-29 23:07:02 | [diff] [blame] | 2783 | Parse *pParse, /* The parser context */ |
| 2784 | Table *pTab, /* the table into which we are inserting */ |
drh | 26198bb | 2013-10-31 11:15:09 | [diff] [blame] | 2785 | int iDataCur, /* Cursor of the canonical data source */ |
| 2786 | int iIdxCur, /* First index cursor */ |
drh | 6934fc7 | 2013-10-31 15:37:49 | [diff] [blame] | 2787 | int regNewData, /* Range of content */ |
drh | aa9b896 | 2008-01-08 02:57:55 | [diff] [blame] | 2788 | int *aRegIdx, /* Register used by each index. 0 for unused indices */ |
dan | f91c131 | 2017-01-10 20:04:38 | [diff] [blame] | 2789 | int update_flags, /* True for UPDATE, False for INSERT */ |
danielk1977 | de63035 | 2009-05-04 11:42:29 | [diff] [blame] | 2790 | int appendBias, /* True if this is likely to be an append */ |
| 2791 | int useSeekResult /* True to set the USESEEKRESULT flag on OP_[Idx]Insert */ |
drh | 0ca3e24 | 2002-01-29 23:07:02 | [diff] [blame] | 2792 | ){ |
drh | 6934fc7 | 2013-10-31 15:37:49 | [diff] [blame] | 2793 | Vdbe *v; /* Prepared statements under construction */ |
| 2794 | Index *pIdx; /* An index being inserted or updated */ |
| 2795 | u8 pik_flags; /* flag values passed to the btree insert */ |
drh | 6934fc7 | 2013-10-31 15:37:49 | [diff] [blame] | 2796 | int i; /* Loop counter */ |
drh | 0ca3e24 | 2002-01-29 23:07:02 | [diff] [blame] | 2797 | |
dan | f91c131 | 2017-01-10 20:04:38 | [diff] [blame] | 2798 | assert( update_flags==0 |
| 2799 | || update_flags==OPFLAG_ISUPDATE |
| 2800 | || update_flags==(OPFLAG_ISUPDATE|OPFLAG_SAVEPOSITION) |
| 2801 | ); |
| 2802 | |
drh | f0b4174 | 2020-08-15 21:55:14 | [diff] [blame] | 2803 | v = pParse->pVdbe; |
drh | 0ca3e24 | 2002-01-29 23:07:02 | [diff] [blame] | 2804 | assert( v!=0 ); |
drh | f38524d | 2021-08-02 16:41:57 | [diff] [blame] | 2805 | assert( !IsView(pTab) ); /* This table is not a VIEW */ |
drh | b2b9d3d | 2013-08-01 01:14:43 | [diff] [blame] | 2806 | for(i=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){ |
drh | d35bdd6 | 2019-12-15 02:49:32 | [diff] [blame] | 2807 | /* All REPLACE indexes are at the end of the list */ |
| 2808 | assert( pIdx->onError!=OE_Replace |
| 2809 | || pIdx->pNext==0 |
| 2810 | || pIdx->pNext->onError==OE_Replace ); |
drh | aa9b896 | 2008-01-08 02:57:55 | [diff] [blame] | 2811 | if( aRegIdx[i]==0 ) continue; |
drh | b2b9d3d | 2013-08-01 01:14:43 | [diff] [blame] | 2812 | if( pIdx->pPartIdxWhere ){ |
| 2813 | sqlite3VdbeAddOp2(v, OP_IsNull, aRegIdx[i], sqlite3VdbeCurrentAddr(v)+2); |
drh | 688852a | 2014-02-17 22:40:43 | [diff] [blame] | 2814 | VdbeCoverage(v); |
drh | b2b9d3d | 2013-08-01 01:14:43 | [diff] [blame] | 2815 | } |
dan | cb9a364 | 2017-01-30 19:44:53 | [diff] [blame] | 2816 | pik_flags = (useSeekResult ? OPFLAG_USESEEKRESULT : 0); |
drh | 48dd1d8 | 2014-05-27 18:18:58 | [diff] [blame] | 2817 | if( IsPrimaryKeyIndex(pIdx) && !HasRowid(pTab) ){ |
drh | 6546af1 | 2013-11-04 15:23:25 | [diff] [blame] | 2818 | pik_flags |= OPFLAG_NCHANGE; |
dan | f91c131 | 2017-01-10 20:04:38 | [diff] [blame] | 2819 | pik_flags |= (update_flags & OPFLAG_SAVEPOSITION); |
dan | cb9a364 | 2017-01-30 19:44:53 | [diff] [blame] | 2820 | if( update_flags==0 ){ |
dan | fadc0e3 | 2021-02-18 12:18:10 | [diff] [blame] | 2821 | codeWithoutRowidPreupdate(pParse, pTab, iIdxCur+i, aRegIdx[i]); |
dan | cb9a364 | 2017-01-30 19:44:53 | [diff] [blame] | 2822 | } |
danielk1977 | de63035 | 2009-05-04 11:42:29 | [diff] [blame] | 2823 | } |
dan | cb9a364 | 2017-01-30 19:44:53 | [diff] [blame] | 2824 | sqlite3VdbeAddOp4Int(v, OP_IdxInsert, iIdxCur+i, aRegIdx[i], |
| 2825 | aRegIdx[i]+1, |
| 2826 | pIdx->uniqNotNull ? pIdx->nKeyCol: pIdx->nColumn); |
drh | 9b34abe | 2016-01-16 15:12:35 | [diff] [blame] | 2827 | sqlite3VdbeChangeP5(v, pik_flags); |
drh | 0ca3e24 | 2002-01-29 23:07:02 | [diff] [blame] | 2828 | } |
drh | ec95c44 | 2013-10-23 01:57:32 | [diff] [blame] | 2829 | if( !HasRowid(pTab) ) return; |
drh | 4794f73 | 2004-11-05 17:17:50 | [diff] [blame] | 2830 | if( pParse->nested ){ |
| 2831 | pik_flags = 0; |
| 2832 | }else{ |
danielk1977 | 94eb6a1 | 2005-12-15 15:22:08 | [diff] [blame] | 2833 | pik_flags = OPFLAG_NCHANGE; |
dan | f91c131 | 2017-01-10 20:04:38 | [diff] [blame] | 2834 | pik_flags |= (update_flags?update_flags:OPFLAG_LASTROWID); |
drh | 4794f73 | 2004-11-05 17:17:50 | [diff] [blame] | 2835 | } |
drh | e4d9081 | 2007-03-29 05:51:49 | [diff] [blame] | 2836 | if( appendBias ){ |
| 2837 | pik_flags |= OPFLAG_APPEND; |
| 2838 | } |
danielk1977 | de63035 | 2009-05-04 11:42:29 | [diff] [blame] | 2839 | if( useSeekResult ){ |
| 2840 | pik_flags |= OPFLAG_USESEEKRESULT; |
| 2841 | } |
drh | a7c3b93 | 2019-05-07 19:13:42 | [diff] [blame] | 2842 | sqlite3VdbeAddOp3(v, OP_Insert, iDataCur, aRegIdx[i], regNewData); |
danielk1977 | 94eb6a1 | 2005-12-15 15:22:08 | [diff] [blame] | 2843 | if( !pParse->nested ){ |
drh | f14b7fb | 2016-12-07 21:35:55 | [diff] [blame] | 2844 | sqlite3VdbeAppendP4(v, pTab, P4_TABLE); |
danielk1977 | 94eb6a1 | 2005-12-15 15:22:08 | [diff] [blame] | 2845 | } |
drh | b765411 | 2008-01-12 12:48:07 | [diff] [blame] | 2846 | sqlite3VdbeChangeP5(v, pik_flags); |
drh | 0ca3e24 | 2002-01-29 23:07:02 | [diff] [blame] | 2847 | } |
drh | cd44690 | 2004-02-24 01:05:31 | [diff] [blame] | 2848 | |
| 2849 | /* |
drh | 26198bb | 2013-10-31 11:15:09 | [diff] [blame] | 2850 | ** Allocate cursors for the pTab table and all its indices and generate |
| 2851 | ** code to open and initialized those cursors. |
drh | aa9b896 | 2008-01-08 02:57:55 | [diff] [blame] | 2852 | ** |
drh | 26198bb | 2013-10-31 11:15:09 | [diff] [blame] | 2853 | ** The cursor for the object that contains the complete data (normally |
| 2854 | ** the table itself, but the PRIMARY KEY index in the case of a WITHOUT |
| 2855 | ** ROWID table) is returned in *piDataCur. The first index cursor is |
| 2856 | ** returned in *piIdxCur. The number of indices is returned. |
| 2857 | ** |
| 2858 | ** Use iBase as the first cursor (either the *piDataCur for rowid tables |
| 2859 | ** or the first index for WITHOUT ROWID tables) if it is non-negative. |
| 2860 | ** If iBase is negative, then allocate the next available cursor. |
| 2861 | ** |
| 2862 | ** For a rowid table, *piDataCur will be exactly one less than *piIdxCur. |
| 2863 | ** For a WITHOUT ROWID table, *piDataCur will be somewhere in the range |
| 2864 | ** of *piIdxCurs, depending on where the PRIMARY KEY index appears on the |
| 2865 | ** pTab->pIndex list. |
drh | b6b4b79 | 2014-08-21 14:10:23 | [diff] [blame] | 2866 | ** |
| 2867 | ** If pTab is a virtual table, then this routine is a no-op and the |
| 2868 | ** *piDataCur and *piIdxCur values are left uninitialized. |
drh | cd44690 | 2004-02-24 01:05:31 | [diff] [blame] | 2869 | */ |
drh | aa9b896 | 2008-01-08 02:57:55 | [diff] [blame] | 2870 | int sqlite3OpenTableAndIndices( |
drh | 290c194 | 2004-08-21 17:54:45 | [diff] [blame] | 2871 | Parse *pParse, /* Parsing context */ |
| 2872 | Table *pTab, /* Table to be opened */ |
drh | 26198bb | 2013-10-31 11:15:09 | [diff] [blame] | 2873 | int op, /* OP_OpenRead or OP_OpenWrite */ |
drh | b89aeb6 | 2016-01-27 15:49:32 | [diff] [blame] | 2874 | u8 p5, /* P5 value for OP_Open* opcodes (except on WITHOUT ROWID) */ |
drh | 26198bb | 2013-10-31 11:15:09 | [diff] [blame] | 2875 | int iBase, /* Use this for the table cursor, if there is one */ |
drh | 6a53499 | 2013-11-16 20:13:39 | [diff] [blame] | 2876 | u8 *aToOpen, /* If not NULL: boolean for each table and index */ |
drh | 26198bb | 2013-10-31 11:15:09 | [diff] [blame] | 2877 | int *piDataCur, /* Write the database source cursor number here */ |
| 2878 | int *piIdxCur /* Write the first index cursor number here */ |
drh | 290c194 | 2004-08-21 17:54:45 | [diff] [blame] | 2879 | ){ |
drh | cd44690 | 2004-02-24 01:05:31 | [diff] [blame] | 2880 | int i; |
drh | 4cbdda9 | 2006-06-14 19:00:20 | [diff] [blame] | 2881 | int iDb; |
drh | 6a53499 | 2013-11-16 20:13:39 | [diff] [blame] | 2882 | int iDataCur; |
drh | cd44690 | 2004-02-24 01:05:31 | [diff] [blame] | 2883 | Index *pIdx; |
drh | 4cbdda9 | 2006-06-14 19:00:20 | [diff] [blame] | 2884 | Vdbe *v; |
| 2885 | |
drh | 26198bb | 2013-10-31 11:15:09 | [diff] [blame] | 2886 | assert( op==OP_OpenRead || op==OP_OpenWrite ); |
dan | fd261ec | 2015-10-22 20:54:33 | [diff] [blame] | 2887 | assert( op==OP_OpenWrite || p5==0 ); |
drh | 56a4107 | 2023-06-16 14:39:21 | [diff] [blame] | 2888 | assert( piDataCur!=0 ); |
| 2889 | assert( piIdxCur!=0 ); |
drh | 26198bb | 2013-10-31 11:15:09 | [diff] [blame] | 2890 | if( IsVirtual(pTab) ){ |
drh | b6b4b79 | 2014-08-21 14:10:23 | [diff] [blame] | 2891 | /* This routine is a no-op for virtual tables. Leave the output |
drh | 33d28ab | 2021-10-28 12:07:43 | [diff] [blame] | 2892 | ** variables *piDataCur and *piIdxCur set to illegal cursor numbers |
| 2893 | ** for improved error detection. */ |
| 2894 | *piDataCur = *piIdxCur = -999; |
drh | 26198bb | 2013-10-31 11:15:09 | [diff] [blame] | 2895 | return 0; |
| 2896 | } |
drh | 4cbdda9 | 2006-06-14 19:00:20 | [diff] [blame] | 2897 | iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema); |
drh | f0b4174 | 2020-08-15 21:55:14 | [diff] [blame] | 2898 | v = pParse->pVdbe; |
drh | cd44690 | 2004-02-24 01:05:31 | [diff] [blame] | 2899 | assert( v!=0 ); |
drh | 26198bb | 2013-10-31 11:15:09 | [diff] [blame] | 2900 | if( iBase<0 ) iBase = pParse->nTab; |
drh | 6a53499 | 2013-11-16 20:13:39 | [diff] [blame] | 2901 | iDataCur = iBase++; |
drh | 56a4107 | 2023-06-16 14:39:21 | [diff] [blame] | 2902 | *piDataCur = iDataCur; |
drh | 6a53499 | 2013-11-16 20:13:39 | [diff] [blame] | 2903 | if( HasRowid(pTab) && (aToOpen==0 || aToOpen[0]) ){ |
| 2904 | sqlite3OpenTable(pParse, iDataCur, iDb, pTab, op); |
drh | 40ee729 | 2023-06-20 17:45:19 | [diff] [blame] | 2905 | }else if( pParse->db->noSharedCache==0 ){ |
drh | 26198bb | 2013-10-31 11:15:09 | [diff] [blame] | 2906 | sqlite3TableLock(pParse, iDb, pTab->tnum, op==OP_OpenWrite, pTab->zName); |
drh | 6fbe41a | 2013-10-30 20:22:55 | [diff] [blame] | 2907 | } |
drh | 56a4107 | 2023-06-16 14:39:21 | [diff] [blame] | 2908 | *piIdxCur = iBase; |
drh | 26198bb | 2013-10-31 11:15:09 | [diff] [blame] | 2909 | for(i=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){ |
drh | 26198bb | 2013-10-31 11:15:09 | [diff] [blame] | 2910 | int iIdxCur = iBase++; |
danielk1977 | da18423 | 2006-01-05 11:34:32 | [diff] [blame] | 2911 | assert( pIdx->pSchema==pTab->pSchema ); |
dan | 61441c3 | 2016-08-26 12:00:50 | [diff] [blame] | 2912 | if( IsPrimaryKeyIndex(pIdx) && !HasRowid(pTab) ){ |
drh | 56a4107 | 2023-06-16 14:39:21 | [diff] [blame] | 2913 | *piDataCur = iIdxCur; |
dan | 61441c3 | 2016-08-26 12:00:50 | [diff] [blame] | 2914 | p5 = 0; |
| 2915 | } |
drh | 6a53499 | 2013-11-16 20:13:39 | [diff] [blame] | 2916 | if( aToOpen==0 || aToOpen[i+1] ){ |
| 2917 | sqlite3VdbeAddOp3(v, op, iIdxCur, pIdx->tnum, iDb); |
| 2918 | sqlite3VdbeSetP4KeyInfo(pParse, pIdx); |
drh | b89aeb6 | 2016-01-27 15:49:32 | [diff] [blame] | 2919 | sqlite3VdbeChangeP5(v, p5); |
dan | 61441c3 | 2016-08-26 12:00:50 | [diff] [blame] | 2920 | VdbeComment((v, "%s", pIdx->zName)); |
drh | b89aeb6 | 2016-01-27 15:49:32 | [diff] [blame] | 2921 | } |
drh | cd44690 | 2004-02-24 01:05:31 | [diff] [blame] | 2922 | } |
drh | 26198bb | 2013-10-31 11:15:09 | [diff] [blame] | 2923 | if( iBase>pParse->nTab ) pParse->nTab = iBase; |
| 2924 | return i; |
drh | cd44690 | 2004-02-24 01:05:31 | [diff] [blame] | 2925 | } |
drh | 9d9cf22 | 2007-02-13 15:01:11 | [diff] [blame] | 2926 | |
drh | 91c58e2 | 2007-03-27 12:04:04 | [diff] [blame] | 2927 | |
| 2928 | #ifdef SQLITE_TEST |
| 2929 | /* |
| 2930 | ** The following global variable is incremented whenever the |
| 2931 | ** transfer optimization is used. This is used for testing |
| 2932 | ** purposes only - to make sure the transfer optimization really |
peter.d.reid | 60ec914 | 2014-09-06 16:39:46 | [diff] [blame] | 2933 | ** is happening when it is supposed to. |
drh | 91c58e2 | 2007-03-27 12:04:04 | [diff] [blame] | 2934 | */ |
| 2935 | int sqlite3_xferopt_count; |
| 2936 | #endif /* SQLITE_TEST */ |
| 2937 | |
| 2938 | |
drh | 9d9cf22 | 2007-02-13 15:01:11 | [diff] [blame] | 2939 | #ifndef SQLITE_OMIT_XFER_OPT |
| 2940 | /* |
drh | 9d9cf22 | 2007-02-13 15:01:11 | [diff] [blame] | 2941 | ** Check to see if index pSrc is compatible as a source of data |
| 2942 | ** for index pDest in an insert transfer optimization. The rules |
| 2943 | ** for a compatible index: |
| 2944 | ** |
| 2945 | ** * The index is over the same set of columns |
| 2946 | ** * The same DESC and ASC markings occurs on all columns |
| 2947 | ** * The same onError processing (OE_Abort, OE_Ignore, etc) |
| 2948 | ** * The same collating sequence on each column |
drh | b2b9d3d | 2013-08-01 01:14:43 | [diff] [blame] | 2949 | ** * The index has the exact same WHERE clause |
drh | 9d9cf22 | 2007-02-13 15:01:11 | [diff] [blame] | 2950 | */ |
| 2951 | static int xferCompatibleIndex(Index *pDest, Index *pSrc){ |
| 2952 | int i; |
| 2953 | assert( pDest && pSrc ); |
| 2954 | assert( pDest->pTable!=pSrc->pTable ); |
drh | 1e7c00e | 2019-11-07 14:51:24 | [diff] [blame] | 2955 | if( pDest->nKeyCol!=pSrc->nKeyCol || pDest->nColumn!=pSrc->nColumn ){ |
drh | 9d9cf22 | 2007-02-13 15:01:11 | [diff] [blame] | 2956 | return 0; /* Different number of columns */ |
| 2957 | } |
| 2958 | if( pDest->onError!=pSrc->onError ){ |
| 2959 | return 0; /* Different conflict resolution strategies */ |
| 2960 | } |
drh | bbbdc83 | 2013-10-22 18:01:40 | [diff] [blame] | 2961 | for(i=0; i<pSrc->nKeyCol; i++){ |
drh | 9d9cf22 | 2007-02-13 15:01:11 | [diff] [blame] | 2962 | if( pSrc->aiColumn[i]!=pDest->aiColumn[i] ){ |
| 2963 | return 0; /* Different columns indexed */ |
| 2964 | } |
drh | 4b92f98 | 2015-09-29 17:20:14 | [diff] [blame] | 2965 | if( pSrc->aiColumn[i]==XN_EXPR ){ |
drh | 1f9ca2c | 2015-08-25 16:57:52 | [diff] [blame] | 2966 | assert( pSrc->aColExpr!=0 && pDest->aColExpr!=0 ); |
dan | 5aa550c | 2017-06-24 18:10:29 | [diff] [blame] | 2967 | if( sqlite3ExprCompare(0, pSrc->aColExpr->a[i].pExpr, |
drh | 1f9ca2c | 2015-08-25 16:57:52 | [diff] [blame] | 2968 | pDest->aColExpr->a[i].pExpr, -1)!=0 ){ |
| 2969 | return 0; /* Different expressions in the index */ |
| 2970 | } |
| 2971 | } |
drh | 9d9cf22 | 2007-02-13 15:01:11 | [diff] [blame] | 2972 | if( pSrc->aSortOrder[i]!=pDest->aSortOrder[i] ){ |
| 2973 | return 0; /* Different sort orders */ |
| 2974 | } |
drh | 0472af9 | 2015-12-30 15:18:16 | [diff] [blame] | 2975 | if( sqlite3_stricmp(pSrc->azColl[i],pDest->azColl[i])!=0 ){ |
drh | 60a713c | 2008-01-21 16:22:45 | [diff] [blame] | 2976 | return 0; /* Different collating sequences */ |
drh | 9d9cf22 | 2007-02-13 15:01:11 | [diff] [blame] | 2977 | } |
| 2978 | } |
dan | 5aa550c | 2017-06-24 18:10:29 | [diff] [blame] | 2979 | if( sqlite3ExprCompare(0, pSrc->pPartIdxWhere, pDest->pPartIdxWhere, -1) ){ |
drh | b2b9d3d | 2013-08-01 01:14:43 | [diff] [blame] | 2980 | return 0; /* Different WHERE clauses */ |
| 2981 | } |
drh | 9d9cf22 | 2007-02-13 15:01:11 | [diff] [blame] | 2982 | |
| 2983 | /* If no test above fails then the indices must be compatible */ |
| 2984 | return 1; |
| 2985 | } |
| 2986 | |
| 2987 | /* |
| 2988 | ** Attempt the transfer optimization on INSERTs of the form |
| 2989 | ** |
| 2990 | ** INSERT INTO tab1 SELECT * FROM tab2; |
| 2991 | ** |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 2992 | ** The xfer optimization transfers raw records from tab2 over to tab1. |
peter.d.reid | 60ec914 | 2014-09-06 16:39:46 | [diff] [blame] | 2993 | ** Columns are not decoded and reassembled, which greatly improves |
drh | ccdf1ba | 2011-11-04 14:36:02 | [diff] [blame] | 2994 | ** performance. Raw index records are transferred in the same way. |
drh | 9d9cf22 | 2007-02-13 15:01:11 | [diff] [blame] | 2995 | ** |
drh | ccdf1ba | 2011-11-04 14:36:02 | [diff] [blame] | 2996 | ** The xfer optimization is only attempted if tab1 and tab2 are compatible. |
| 2997 | ** There are lots of rules for determining compatibility - see comments |
| 2998 | ** embedded in the code for details. |
drh | 9d9cf22 | 2007-02-13 15:01:11 | [diff] [blame] | 2999 | ** |
drh | ccdf1ba | 2011-11-04 14:36:02 | [diff] [blame] | 3000 | ** This routine returns TRUE if the optimization is guaranteed to be used. |
| 3001 | ** Sometimes the xfer optimization will only work if the destination table |
| 3002 | ** is empty - a factor that can only be determined at run-time. In that |
| 3003 | ** case, this routine generates code for the xfer optimization but also |
| 3004 | ** does a test to see if the destination table is empty and jumps over the |
| 3005 | ** xfer optimization code if the test fails. In that case, this routine |
| 3006 | ** returns FALSE so that the caller will know to go ahead and generate |
| 3007 | ** an unoptimized transfer. This routine also returns FALSE if there |
| 3008 | ** is no chance that the xfer optimization can be applied. |
drh | 9d9cf22 | 2007-02-13 15:01:11 | [diff] [blame] | 3009 | ** |
drh | ccdf1ba | 2011-11-04 14:36:02 | [diff] [blame] | 3010 | ** This optimization is particularly useful at making VACUUM run faster. |
drh | 9d9cf22 | 2007-02-13 15:01:11 | [diff] [blame] | 3011 | */ |
| 3012 | static int xferOptimization( |
| 3013 | Parse *pParse, /* Parser context */ |
| 3014 | Table *pDest, /* The table we are inserting into */ |
| 3015 | Select *pSelect, /* A SELECT statement to use as the data source */ |
| 3016 | int onError, /* How to handle constraint errors */ |
| 3017 | int iDbDest /* The database of pDest */ |
| 3018 | ){ |
dan | e34162b | 2015-04-01 18:20:25 | [diff] [blame] | 3019 | sqlite3 *db = pParse->db; |
drh | 9d9cf22 | 2007-02-13 15:01:11 | [diff] [blame] | 3020 | ExprList *pEList; /* The result set of the SELECT */ |
| 3021 | Table *pSrc; /* The table in the FROM clause of SELECT */ |
| 3022 | Index *pSrcIdx, *pDestIdx; /* Source and destination indices */ |
drh | 7601294 | 2021-02-21 21:04:54 | [diff] [blame] | 3023 | SrcItem *pItem; /* An element of pSelect->pSrc */ |
drh | 9d9cf22 | 2007-02-13 15:01:11 | [diff] [blame] | 3024 | int i; /* Loop counter */ |
| 3025 | int iDbSrc; /* The database of pSrc */ |
| 3026 | int iSrc, iDest; /* Cursors from source and destination */ |
| 3027 | int addr1, addr2; /* Loop addresses */ |
drh | da475b8 | 2013-11-04 21:44:54 | [diff] [blame] | 3028 | int emptyDestTest = 0; /* Address of test for empty pDest */ |
| 3029 | int emptySrcTest = 0; /* Address of test for empty pSrc */ |
drh | 9d9cf22 | 2007-02-13 15:01:11 | [diff] [blame] | 3030 | Vdbe *v; /* The VDBE we are building */ |
drh | 6a288a3 | 2008-01-07 19:20:24 | [diff] [blame] | 3031 | int regAutoinc; /* Memory register used by AUTOINC */ |
drh | f33c9fa | 2007-04-10 18:17:55 | [diff] [blame] | 3032 | int destHasUniqueIdx = 0; /* True if pDest has a UNIQUE index */ |
drh | b765411 | 2008-01-12 12:48:07 | [diff] [blame] | 3033 | int regData, regRowid; /* Registers holding data and rowid */ |
drh | 9d9cf22 | 2007-02-13 15:01:11 | [diff] [blame] | 3034 | |
drh | 935c372 | 2022-02-28 16:44:58 | [diff] [blame] | 3035 | assert( pSelect!=0 ); |
dan | ebbf08a | 2014-01-18 08:27:02 | [diff] [blame] | 3036 | if( pParse->pWith || pSelect->pWith ){ |
| 3037 | /* Do not attempt to process this query if there are an WITH clauses |
| 3038 | ** attached to it. Proceeding may generate a false "no such table: xxx" |
| 3039 | ** error if pSelect reads from a CTE named "xxx". */ |
| 3040 | return 0; |
| 3041 | } |
drh | 9d9cf22 | 2007-02-13 15:01:11 | [diff] [blame] | 3042 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
drh | 44266ec | 2017-02-16 14:48:08 | [diff] [blame] | 3043 | if( IsVirtual(pDest) ){ |
drh | 9d9cf22 | 2007-02-13 15:01:11 | [diff] [blame] | 3044 | return 0; /* tab1 must not be a virtual table */ |
| 3045 | } |
| 3046 | #endif |
| 3047 | if( onError==OE_Default ){ |
drh | e7224a0 | 2011-11-04 00:23:53 | [diff] [blame] | 3048 | if( pDest->iPKey>=0 ) onError = pDest->keyConf; |
| 3049 | if( onError==OE_Default ) onError = OE_Abort; |
drh | 9d9cf22 | 2007-02-13 15:01:11 | [diff] [blame] | 3050 | } |
danielk1977 | 5ce240a | 2007-09-03 17:30:06 | [diff] [blame] | 3051 | assert(pSelect->pSrc); /* allocated even if there is no FROM clause */ |
drh | 9d9cf22 | 2007-02-13 15:01:11 | [diff] [blame] | 3052 | if( pSelect->pSrc->nSrc!=1 ){ |
| 3053 | return 0; /* FROM clause must have exactly one term */ |
| 3054 | } |
drh | 1521ca4 | 2024-08-19 22:48:30 | [diff] [blame] | 3055 | if( pSelect->pSrc->a[0].fg.isSubquery ){ |
drh | 9d9cf22 | 2007-02-13 15:01:11 | [diff] [blame] | 3056 | return 0; /* FROM clause cannot contain a subquery */ |
| 3057 | } |
| 3058 | if( pSelect->pWhere ){ |
| 3059 | return 0; /* SELECT may not have a WHERE clause */ |
| 3060 | } |
| 3061 | if( pSelect->pOrderBy ){ |
| 3062 | return 0; /* SELECT may not have an ORDER BY clause */ |
| 3063 | } |
drh | 8103b7d | 2007-02-24 13:23:51 | [diff] [blame] | 3064 | /* Do not need to test for a HAVING clause. If HAVING is present but |
| 3065 | ** there is no ORDER BY, we will get an error. */ |
drh | 9d9cf22 | 2007-02-13 15:01:11 | [diff] [blame] | 3066 | if( pSelect->pGroupBy ){ |
| 3067 | return 0; /* SELECT may not have a GROUP BY clause */ |
| 3068 | } |
| 3069 | if( pSelect->pLimit ){ |
| 3070 | return 0; /* SELECT may not have a LIMIT clause */ |
| 3071 | } |
drh | 9d9cf22 | 2007-02-13 15:01:11 | [diff] [blame] | 3072 | if( pSelect->pPrior ){ |
| 3073 | return 0; /* SELECT may not be a compound query */ |
| 3074 | } |
drh | 7d10d5a | 2008-08-20 16:35:10 | [diff] [blame] | 3075 | if( pSelect->selFlags & SF_Distinct ){ |
drh | 9d9cf22 | 2007-02-13 15:01:11 | [diff] [blame] | 3076 | return 0; /* SELECT may not be DISTINCT */ |
| 3077 | } |
| 3078 | pEList = pSelect->pEList; |
| 3079 | assert( pEList!=0 ); |
| 3080 | if( pEList->nExpr!=1 ){ |
| 3081 | return 0; /* The result set must have exactly one column */ |
| 3082 | } |
| 3083 | assert( pEList->a[0].pExpr ); |
drh | 1a1d3cd | 2015-11-19 16:33:31 | [diff] [blame] | 3084 | if( pEList->a[0].pExpr->op!=TK_ASTERISK ){ |
drh | 9d9cf22 | 2007-02-13 15:01:11 | [diff] [blame] | 3085 | return 0; /* The result set must be the special operator "*" */ |
| 3086 | } |
| 3087 | |
| 3088 | /* At this point we have established that the statement is of the |
| 3089 | ** correct syntactic form to participate in this optimization. Now |
| 3090 | ** we have to check the semantics. |
| 3091 | */ |
| 3092 | pItem = pSelect->pSrc->a; |
dan | 41fb5cd | 2012-10-04 19:33:00 | [diff] [blame] | 3093 | pSrc = sqlite3LocateTableItem(pParse, 0, pItem); |
drh | 9d9cf22 | 2007-02-13 15:01:11 | [diff] [blame] | 3094 | if( pSrc==0 ){ |
| 3095 | return 0; /* FROM clause does not contain a real table */ |
| 3096 | } |
drh | 21908b2 | 2019-01-17 20:19:35 | [diff] [blame] | 3097 | if( pSrc->tnum==pDest->tnum && pSrc->pSchema==pDest->pSchema ){ |
drh | 1e32bed | 2020-06-19 13:33:53 | [diff] [blame] | 3098 | testcase( pSrc!=pDest ); /* Possible due to bad sqlite_schema.rootpage */ |
drh | 9d9cf22 | 2007-02-13 15:01:11 | [diff] [blame] | 3099 | return 0; /* tab1 and tab2 may not be the same table */ |
| 3100 | } |
drh | 5554827 | 2013-10-23 16:03:07 | [diff] [blame] | 3101 | if( HasRowid(pDest)!=HasRowid(pSrc) ){ |
| 3102 | return 0; /* source and destination must both be WITHOUT ROWID or not */ |
| 3103 | } |
drh | f38524d | 2021-08-02 16:41:57 | [diff] [blame] | 3104 | if( !IsOrdinaryTable(pSrc) ){ |
| 3105 | return 0; /* tab2 may not be a view or virtual table */ |
drh | 9d9cf22 | 2007-02-13 15:01:11 | [diff] [blame] | 3106 | } |
| 3107 | if( pDest->nCol!=pSrc->nCol ){ |
| 3108 | return 0; /* Number of columns must be the same in tab1 and tab2 */ |
| 3109 | } |
| 3110 | if( pDest->iPKey!=pSrc->iPKey ){ |
| 3111 | return 0; /* Both tables must have the same INTEGER PRIMARY KEY */ |
| 3112 | } |
drh | 7b4b74a | 2021-08-20 01:12:39 | [diff] [blame] | 3113 | if( (pDest->tabFlags & TF_Strict)!=0 && (pSrc->tabFlags & TF_Strict)==0 ){ |
| 3114 | return 0; /* Cannot feed from a non-strict into a strict table */ |
| 3115 | } |
drh | 9d9cf22 | 2007-02-13 15:01:11 | [diff] [blame] | 3116 | for(i=0; i<pDest->nCol; i++){ |
dan | 9940e2a | 2014-04-26 14:07:57 | [diff] [blame] | 3117 | Column *pDestCol = &pDest->aCol[i]; |
| 3118 | Column *pSrcCol = &pSrc->aCol[i]; |
dan | ba68f8f | 2015-11-19 16:46:46 | [diff] [blame] | 3119 | #ifdef SQLITE_ENABLE_HIDDEN_COLUMNS |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 3120 | if( (db->mDbFlags & DBFLAG_Vacuum)==0 |
| 3121 | && (pDestCol->colFlags | pSrcCol->colFlags) & COLFLAG_HIDDEN |
dan | aaea314 | 2015-11-19 18:09:05 | [diff] [blame] | 3122 | ){ |
dan | ba68f8f | 2015-11-19 16:46:46 | [diff] [blame] | 3123 | return 0; /* Neither table may have __hidden__ columns */ |
| 3124 | } |
| 3125 | #endif |
drh | 6ab61d7 | 2019-10-23 15:47:33 | [diff] [blame] | 3126 | #ifndef SQLITE_OMIT_GENERATED_COLUMNS |
| 3127 | /* Even if tables t1 and t2 have identical schemas, if they contain |
| 3128 | ** generated columns, then this statement is semantically incorrect: |
| 3129 | ** |
| 3130 | ** INSERT INTO t2 SELECT * FROM t1; |
| 3131 | ** |
| 3132 | ** The reason is that generated column values are returned by the |
| 3133 | ** the SELECT statement on the right but the INSERT statement on the |
| 3134 | ** left wants them to be omitted. |
| 3135 | ** |
| 3136 | ** Nevertheless, this is a useful notational shorthand to tell SQLite |
| 3137 | ** to do a bulk transfer all of the content from t1 over to t2. |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 3138 | ** |
drh | 6ab61d7 | 2019-10-23 15:47:33 | [diff] [blame] | 3139 | ** We could, in theory, disable this (except for internal use by the |
| 3140 | ** VACUUM command where it is actually needed). But why do that? It |
| 3141 | ** seems harmless enough, and provides a useful service. |
| 3142 | */ |
drh | ae3977a | 2019-10-17 16:16:34 | [diff] [blame] | 3143 | if( (pDestCol->colFlags & COLFLAG_GENERATED) != |
| 3144 | (pSrcCol->colFlags & COLFLAG_GENERATED) ){ |
drh | 6ab61d7 | 2019-10-23 15:47:33 | [diff] [blame] | 3145 | return 0; /* Both columns have the same generated-column type */ |
drh | ae3977a | 2019-10-17 16:16:34 | [diff] [blame] | 3146 | } |
drh | 6ab61d7 | 2019-10-23 15:47:33 | [diff] [blame] | 3147 | /* But the transfer is only allowed if both the source and destination |
| 3148 | ** tables have the exact same expressions for generated columns. |
| 3149 | ** This requirement could be relaxed for VIRTUAL columns, I suppose. |
| 3150 | */ |
| 3151 | if( (pDestCol->colFlags & COLFLAG_GENERATED)!=0 ){ |
drh | 79cf2b7 | 2021-07-31 20:30:41 | [diff] [blame] | 3152 | if( sqlite3ExprCompare(0, |
| 3153 | sqlite3ColumnExpr(pSrc, pSrcCol), |
| 3154 | sqlite3ColumnExpr(pDest, pDestCol), -1)!=0 ){ |
drh | 6ab61d7 | 2019-10-23 15:47:33 | [diff] [blame] | 3155 | testcase( pDestCol->colFlags & COLFLAG_VIRTUAL ); |
| 3156 | testcase( pDestCol->colFlags & COLFLAG_STORED ); |
| 3157 | return 0; /* Different generator expressions */ |
| 3158 | } |
| 3159 | } |
| 3160 | #endif |
dan | 9940e2a | 2014-04-26 14:07:57 | [diff] [blame] | 3161 | if( pDestCol->affinity!=pSrcCol->affinity ){ |
drh | 9d9cf22 | 2007-02-13 15:01:11 | [diff] [blame] | 3162 | return 0; /* Affinity must be the same on all columns */ |
| 3163 | } |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 3164 | if( sqlite3_stricmp(sqlite3ColumnColl(pDestCol), |
drh | 65b4009 | 2021-08-05 15:27:19 | [diff] [blame] | 3165 | sqlite3ColumnColl(pSrcCol))!=0 ){ |
drh | 9d9cf22 | 2007-02-13 15:01:11 | [diff] [blame] | 3166 | return 0; /* Collating sequence must be the same on all columns */ |
| 3167 | } |
dan | 9940e2a | 2014-04-26 14:07:57 | [diff] [blame] | 3168 | if( pDestCol->notNull && !pSrcCol->notNull ){ |
drh | 9d9cf22 | 2007-02-13 15:01:11 | [diff] [blame] | 3169 | return 0; /* tab2 must be NOT NULL if tab1 is */ |
| 3170 | } |
drh | 453e026 | 2014-04-26 17:52:08 | [diff] [blame] | 3171 | /* Default values for second and subsequent columns need to match. */ |
drh | ae3977a | 2019-10-17 16:16:34 | [diff] [blame] | 3172 | if( (pDestCol->colFlags & COLFLAG_GENERATED)==0 && i>0 ){ |
drh | 79cf2b7 | 2021-07-31 20:30:41 | [diff] [blame] | 3173 | Expr *pDestExpr = sqlite3ColumnExpr(pDest, pDestCol); |
| 3174 | Expr *pSrcExpr = sqlite3ColumnExpr(pSrc, pSrcCol); |
| 3175 | assert( pDestExpr==0 || pDestExpr->op==TK_SPAN ); |
drh | f975107 | 2021-10-07 13:40:29 | [diff] [blame] | 3176 | assert( pDestExpr==0 || !ExprHasProperty(pDestExpr, EP_IntValue) ); |
drh | 79cf2b7 | 2021-07-31 20:30:41 | [diff] [blame] | 3177 | assert( pSrcExpr==0 || pSrcExpr->op==TK_SPAN ); |
drh | f975107 | 2021-10-07 13:40:29 | [diff] [blame] | 3178 | assert( pSrcExpr==0 || !ExprHasProperty(pSrcExpr, EP_IntValue) ); |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 3179 | if( (pDestExpr==0)!=(pSrcExpr==0) |
drh | 79cf2b7 | 2021-07-31 20:30:41 | [diff] [blame] | 3180 | || (pDestExpr!=0 && strcmp(pDestExpr->u.zToken, |
| 3181 | pSrcExpr->u.zToken)!=0) |
drh | 94fa9c4 | 2016-02-27 21:16:04 | [diff] [blame] | 3182 | ){ |
| 3183 | return 0; /* Default values must be the same for all columns */ |
| 3184 | } |
dan | 9940e2a | 2014-04-26 14:07:57 | [diff] [blame] | 3185 | } |
drh | 9d9cf22 | 2007-02-13 15:01:11 | [diff] [blame] | 3186 | } |
| 3187 | for(pDestIdx=pDest->pIndex; pDestIdx; pDestIdx=pDestIdx->pNext){ |
drh | 5f1d1d9 | 2014-07-31 22:59:04 | [diff] [blame] | 3188 | if( IsUniqueIndex(pDestIdx) ){ |
drh | f33c9fa | 2007-04-10 18:17:55 | [diff] [blame] | 3189 | destHasUniqueIdx = 1; |
| 3190 | } |
drh | 9d9cf22 | 2007-02-13 15:01:11 | [diff] [blame] | 3191 | for(pSrcIdx=pSrc->pIndex; pSrcIdx; pSrcIdx=pSrcIdx->pNext){ |
| 3192 | if( xferCompatibleIndex(pDestIdx, pSrcIdx) ) break; |
| 3193 | } |
| 3194 | if( pSrcIdx==0 ){ |
| 3195 | return 0; /* pDestIdx has no corresponding index in pSrc */ |
| 3196 | } |
drh | e3bd232 | 2019-04-05 16:38:12 | [diff] [blame] | 3197 | if( pSrcIdx->tnum==pDestIdx->tnum && pSrc->pSchema==pDest->pSchema |
| 3198 | && sqlite3FaultSim(411)==SQLITE_OK ){ |
| 3199 | /* The sqlite3FaultSim() call allows this corruption test to be |
| 3200 | ** bypassed during testing, in order to exercise other corruption tests |
| 3201 | ** further downstream. */ |
drh | 86223e8 | 2019-04-05 15:44:55 | [diff] [blame] | 3202 | return 0; /* Corrupt schema - two indexes on the same btree */ |
| 3203 | } |
drh | 9d9cf22 | 2007-02-13 15:01:11 | [diff] [blame] | 3204 | } |
drh | 7fc2f41 | 2007-03-29 00:08:24 | [diff] [blame] | 3205 | #ifndef SQLITE_OMIT_CHECK |
drh | d21d5b2 | 2024-04-09 13:57:27 | [diff] [blame] | 3206 | if( pDest->pCheck |
| 3207 | && (db->mDbFlags & DBFLAG_Vacuum)==0 |
| 3208 | && sqlite3ExprListCompare(pSrc->pCheck,pDest->pCheck,-1) |
| 3209 | ){ |
drh | 8103b7d | 2007-02-24 13:23:51 | [diff] [blame] | 3210 | return 0; /* Tables have different CHECK constraints. Ticket #2252 */ |
| 3211 | } |
drh | 7fc2f41 | 2007-03-29 00:08:24 | [diff] [blame] | 3212 | #endif |
drh | 713de34 | 2011-04-24 22:56:07 | [diff] [blame] | 3213 | #ifndef SQLITE_OMIT_FOREIGN_KEY |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 3214 | /* Disallow the transfer optimization if the destination table contains |
drh | 713de34 | 2011-04-24 22:56:07 | [diff] [blame] | 3215 | ** any foreign key constraints. This is more restrictive than necessary. |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 3216 | ** But the main beneficiary of the transfer optimization is the VACUUM |
drh | 713de34 | 2011-04-24 22:56:07 | [diff] [blame] | 3217 | ** command, and the VACUUM command disables foreign key constraints. So |
| 3218 | ** the extra complication to make this rule less restrictive is probably |
| 3219 | ** not worth the effort. Ticket [6284df89debdfa61db8073e062908af0c9b6118e] |
| 3220 | */ |
drh | 78b2fa8 | 2021-10-07 12:11:20 | [diff] [blame] | 3221 | assert( IsOrdinaryTable(pDest) ); |
drh | f38524d | 2021-08-02 16:41:57 | [diff] [blame] | 3222 | if( (db->flags & SQLITE_ForeignKeys)!=0 && pDest->u.tab.pFKey!=0 ){ |
drh | 713de34 | 2011-04-24 22:56:07 | [diff] [blame] | 3223 | return 0; |
| 3224 | } |
| 3225 | #endif |
dan | e34162b | 2015-04-01 18:20:25 | [diff] [blame] | 3226 | if( (db->flags & SQLITE_CountRows)!=0 ){ |
drh | ccdf1ba | 2011-11-04 14:36:02 | [diff] [blame] | 3227 | return 0; /* xfer opt does not play well with PRAGMA count_changes */ |
dan | 1696124 | 2011-09-30 12:01:01 | [diff] [blame] | 3228 | } |
drh | 9d9cf22 | 2007-02-13 15:01:11 | [diff] [blame] | 3229 | |
drh | ccdf1ba | 2011-11-04 14:36:02 | [diff] [blame] | 3230 | /* If we get this far, it means that the xfer optimization is at |
| 3231 | ** least a possibility, though it might only work if the destination |
| 3232 | ** table (tab1) is initially empty. |
drh | 9d9cf22 | 2007-02-13 15:01:11 | [diff] [blame] | 3233 | */ |
drh | dd73521 | 2007-02-24 13:53:05 | [diff] [blame] | 3234 | #ifdef SQLITE_TEST |
| 3235 | sqlite3_xferopt_count++; |
| 3236 | #endif |
dan | e34162b | 2015-04-01 18:20:25 | [diff] [blame] | 3237 | iDbSrc = sqlite3SchemaToIndex(db, pSrc->pSchema); |
drh | 9d9cf22 | 2007-02-13 15:01:11 | [diff] [blame] | 3238 | v = sqlite3GetVdbe(pParse); |
drh | f53e9b5 | 2007-08-29 13:45:58 | [diff] [blame] | 3239 | sqlite3CodeVerifySchema(pParse, iDbSrc); |
drh | 9d9cf22 | 2007-02-13 15:01:11 | [diff] [blame] | 3240 | iSrc = pParse->nTab++; |
| 3241 | iDest = pParse->nTab++; |
drh | 6a288a3 | 2008-01-07 19:20:24 | [diff] [blame] | 3242 | regAutoinc = autoIncBegin(pParse, iDbDest, pDest); |
drh | b765411 | 2008-01-12 12:48:07 | [diff] [blame] | 3243 | regData = sqlite3GetTempReg(pParse); |
dan | 7aae735 | 2020-12-10 18:06:24 | [diff] [blame] | 3244 | sqlite3VdbeAddOp2(v, OP_Null, 0, regData); |
drh | b765411 | 2008-01-12 12:48:07 | [diff] [blame] | 3245 | regRowid = sqlite3GetTempReg(pParse); |
drh | 9d9cf22 | 2007-02-13 15:01:11 | [diff] [blame] | 3246 | sqlite3OpenTable(pParse, iDest, iDbDest, pDest, OP_OpenWrite); |
dan | 427ebba | 2013-11-05 16:39:31 | [diff] [blame] | 3247 | assert( HasRowid(pDest) || destHasUniqueIdx ); |
drh | 8257aa8 | 2017-07-26 19:59:13 | [diff] [blame] | 3248 | if( (db->mDbFlags & DBFLAG_Vacuum)==0 && ( |
dan | e34162b | 2015-04-01 18:20:25 | [diff] [blame] | 3249 | (pDest->iPKey<0 && pDest->pIndex!=0) /* (1) */ |
drh | 9d9cf22 | 2007-02-13 15:01:11 | [diff] [blame] | 3250 | || destHasUniqueIdx /* (2) */ |
| 3251 | || (onError!=OE_Abort && onError!=OE_Rollback) /* (3) */ |
dan | e34162b | 2015-04-01 18:20:25 | [diff] [blame] | 3252 | )){ |
drh | 9d9cf22 | 2007-02-13 15:01:11 | [diff] [blame] | 3253 | /* In some circumstances, we are able to run the xfer optimization |
dan | e34162b | 2015-04-01 18:20:25 | [diff] [blame] | 3254 | ** only if the destination table is initially empty. Unless the |
drh | 8257aa8 | 2017-07-26 19:59:13 | [diff] [blame] | 3255 | ** DBFLAG_Vacuum flag is set, this block generates code to make |
| 3256 | ** that determination. If DBFLAG_Vacuum is set, then the destination |
dan | e34162b | 2015-04-01 18:20:25 | [diff] [blame] | 3257 | ** table is always empty. |
| 3258 | ** |
| 3259 | ** Conditions under which the destination must be empty: |
drh | bd36ba6 | 2007-03-31 13:00:26 | [diff] [blame] | 3260 | ** |
drh | 9d9cf22 | 2007-02-13 15:01:11 | [diff] [blame] | 3261 | ** (1) There is no INTEGER PRIMARY KEY but there are indices. |
| 3262 | ** (If the destination is not initially empty, the rowid fields |
| 3263 | ** of index entries might need to change.) |
| 3264 | ** |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 3265 | ** (2) The destination has a unique index. (The xfer optimization |
drh | 9d9cf22 | 2007-02-13 15:01:11 | [diff] [blame] | 3266 | ** is unable to test uniqueness.) |
drh | bd36ba6 | 2007-03-31 13:00:26 | [diff] [blame] | 3267 | ** |
| 3268 | ** (3) onError is something other than OE_Abort and OE_Rollback. |
drh | f33c9fa | 2007-04-10 18:17:55 | [diff] [blame] | 3269 | */ |
drh | 688852a | 2014-02-17 22:40:43 | [diff] [blame] | 3270 | addr1 = sqlite3VdbeAddOp2(v, OP_Rewind, iDest, 0); VdbeCoverage(v); |
drh | 2991ba0 | 2015-09-02 18:19:00 | [diff] [blame] | 3271 | emptyDestTest = sqlite3VdbeAddOp0(v, OP_Goto); |
drh | 66a5167 | 2008-01-03 00:01:23 | [diff] [blame] | 3272 | sqlite3VdbeJumpHere(v, addr1); |
drh | 9d9cf22 | 2007-02-13 15:01:11 | [diff] [blame] | 3273 | } |
drh | 5554827 | 2013-10-23 16:03:07 | [diff] [blame] | 3274 | if( HasRowid(pSrc) ){ |
drh | c9b9dea | 2016-11-15 02:46:39 | [diff] [blame] | 3275 | u8 insFlags; |
drh | 5554827 | 2013-10-23 16:03:07 | [diff] [blame] | 3276 | sqlite3OpenTable(pParse, iSrc, iDbSrc, pSrc, OP_OpenRead); |
drh | 688852a | 2014-02-17 22:40:43 | [diff] [blame] | 3277 | emptySrcTest = sqlite3VdbeAddOp2(v, OP_Rewind, iSrc, 0); VdbeCoverage(v); |
drh | 5554827 | 2013-10-23 16:03:07 | [diff] [blame] | 3278 | if( pDest->iPKey>=0 ){ |
| 3279 | addr1 = sqlite3VdbeAddOp2(v, OP_Rowid, iSrc, regRowid); |
dan | 036e067 | 2020-12-08 20:19:07 | [diff] [blame] | 3280 | if( (db->mDbFlags & DBFLAG_Vacuum)==0 ){ |
| 3281 | sqlite3VdbeVerifyAbortable(v, onError); |
| 3282 | addr2 = sqlite3VdbeAddOp3(v, OP_NotExists, iDest, 0, regRowid); |
| 3283 | VdbeCoverage(v); |
| 3284 | sqlite3RowidConstraint(pParse, onError, pDest); |
| 3285 | sqlite3VdbeJumpHere(v, addr2); |
| 3286 | } |
drh | 5554827 | 2013-10-23 16:03:07 | [diff] [blame] | 3287 | autoIncStep(pParse, regAutoinc, regRowid); |
drh | 4e61e88 | 2019-04-04 14:00:23 | [diff] [blame] | 3288 | }else if( pDest->pIndex==0 && !(db->mDbFlags & DBFLAG_VacuumInto) ){ |
drh | 5554827 | 2013-10-23 16:03:07 | [diff] [blame] | 3289 | addr1 = sqlite3VdbeAddOp2(v, OP_NewRowid, iDest, regRowid); |
| 3290 | }else{ |
| 3291 | addr1 = sqlite3VdbeAddOp2(v, OP_Rowid, iSrc, regRowid); |
| 3292 | assert( (pDest->tabFlags & TF_Autoincrement)==0 ); |
| 3293 | } |
dan | 7aae735 | 2020-12-10 18:06:24 | [diff] [blame] | 3294 | |
drh | 8257aa8 | 2017-07-26 19:59:13 | [diff] [blame] | 3295 | if( db->mDbFlags & DBFLAG_Vacuum ){ |
drh | 86b40df | 2017-08-01 19:53:43 | [diff] [blame] | 3296 | sqlite3VdbeAddOp1(v, OP_SeekEnd, iDest); |
dan | 7aae735 | 2020-12-10 18:06:24 | [diff] [blame] | 3297 | insFlags = OPFLAG_APPEND|OPFLAG_USESEEKRESULT|OPFLAG_PREFORMAT; |
drh | c9b9dea | 2016-11-15 02:46:39 | [diff] [blame] | 3298 | }else{ |
dan | 7aae735 | 2020-12-10 18:06:24 | [diff] [blame] | 3299 | insFlags = OPFLAG_NCHANGE|OPFLAG_LASTROWID|OPFLAG_APPEND|OPFLAG_PREFORMAT; |
drh | c9b9dea | 2016-11-15 02:46:39 | [diff] [blame] | 3300 | } |
dan | 7aae735 | 2020-12-10 18:06:24 | [diff] [blame] | 3301 | #ifdef SQLITE_ENABLE_PREUPDATE_HOOK |
dan | a55a839 | 2021-02-18 15:59:37 | [diff] [blame] | 3302 | if( (db->mDbFlags & DBFLAG_Vacuum)==0 ){ |
| 3303 | sqlite3VdbeAddOp3(v, OP_RowData, iSrc, regData, 1); |
| 3304 | insFlags &= ~OPFLAG_PREFORMAT; |
| 3305 | }else |
dan | 7aae735 | 2020-12-10 18:06:24 | [diff] [blame] | 3306 | #endif |
dan | a55a839 | 2021-02-18 15:59:37 | [diff] [blame] | 3307 | { |
| 3308 | sqlite3VdbeAddOp3(v, OP_RowCell, iDest, iSrc, regRowid); |
| 3309 | } |
| 3310 | sqlite3VdbeAddOp3(v, OP_Insert, iDest, regData, regRowid); |
| 3311 | if( (db->mDbFlags & DBFLAG_Vacuum)==0 ){ |
| 3312 | sqlite3VdbeChangeP4(v, -1, (char*)pDest, P4_TABLE); |
| 3313 | } |
dan | 7aae735 | 2020-12-10 18:06:24 | [diff] [blame] | 3314 | sqlite3VdbeChangeP5(v, insFlags); |
| 3315 | |
drh | 688852a | 2014-02-17 22:40:43 | [diff] [blame] | 3316 | sqlite3VdbeAddOp2(v, OP_Next, iSrc, addr1); VdbeCoverage(v); |
drh | 5554827 | 2013-10-23 16:03:07 | [diff] [blame] | 3317 | sqlite3VdbeAddOp2(v, OP_Close, iSrc, 0); |
| 3318 | sqlite3VdbeAddOp2(v, OP_Close, iDest, 0); |
drh | 95bad4c | 2007-03-28 18:04:10 | [diff] [blame] | 3319 | }else{ |
drh | da475b8 | 2013-11-04 21:44:54 | [diff] [blame] | 3320 | sqlite3TableLock(pParse, iDbDest, pDest->tnum, 1, pDest->zName); |
| 3321 | sqlite3TableLock(pParse, iDbSrc, pSrc->tnum, 0, pSrc->zName); |
drh | 95bad4c | 2007-03-28 18:04:10 | [diff] [blame] | 3322 | } |
drh | 9d9cf22 | 2007-02-13 15:01:11 | [diff] [blame] | 3323 | for(pDestIdx=pDest->pIndex; pDestIdx; pDestIdx=pDestIdx->pNext){ |
drh | 41b9ca2 | 2015-07-28 18:53:37 | [diff] [blame] | 3324 | u8 idxInsFlags = 0; |
drh | 1b7ecbb | 2009-05-03 01:00:59 | [diff] [blame] | 3325 | for(pSrcIdx=pSrc->pIndex; ALWAYS(pSrcIdx); pSrcIdx=pSrcIdx->pNext){ |
drh | 9d9cf22 | 2007-02-13 15:01:11 | [diff] [blame] | 3326 | if( xferCompatibleIndex(pDestIdx, pSrcIdx) ) break; |
| 3327 | } |
| 3328 | assert( pSrcIdx ); |
drh | 2ec2fb2 | 2013-11-06 19:59:23 | [diff] [blame] | 3329 | sqlite3VdbeAddOp3(v, OP_OpenRead, iSrc, pSrcIdx->tnum, iDbSrc); |
| 3330 | sqlite3VdbeSetP4KeyInfo(pParse, pSrcIdx); |
drh | d4e70eb | 2008-01-02 00:34:36 | [diff] [blame] | 3331 | VdbeComment((v, "%s", pSrcIdx->zName)); |
drh | 2ec2fb2 | 2013-11-06 19:59:23 | [diff] [blame] | 3332 | sqlite3VdbeAddOp3(v, OP_OpenWrite, iDest, pDestIdx->tnum, iDbDest); |
| 3333 | sqlite3VdbeSetP4KeyInfo(pParse, pDestIdx); |
dan | 5988572 | 2013-10-04 18:17:18 | [diff] [blame] | 3334 | sqlite3VdbeChangeP5(v, OPFLAG_BULKCSR); |
danielk1977 | 207872a | 2008-01-03 07:54:23 | [diff] [blame] | 3335 | VdbeComment((v, "%s", pDestIdx->zName)); |
drh | 688852a | 2014-02-17 22:40:43 | [diff] [blame] | 3336 | addr1 = sqlite3VdbeAddOp2(v, OP_Rewind, iSrc, 0); VdbeCoverage(v); |
drh | 8257aa8 | 2017-07-26 19:59:13 | [diff] [blame] | 3337 | if( db->mDbFlags & DBFLAG_Vacuum ){ |
dan | e34162b | 2015-04-01 18:20:25 | [diff] [blame] | 3338 | /* This INSERT command is part of a VACUUM operation, which guarantees |
| 3339 | ** that the destination table is empty. If all indexed columns use |
| 3340 | ** collation sequence BINARY, then it can also be assumed that the |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 3341 | ** index will be populated by inserting keys in strictly sorted |
dan | e34162b | 2015-04-01 18:20:25 | [diff] [blame] | 3342 | ** order. In this case, instead of seeking within the b-tree as part |
drh | 86b40df | 2017-08-01 19:53:43 | [diff] [blame] | 3343 | ** of every OP_IdxInsert opcode, an OP_SeekEnd is added before the |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 3344 | ** OP_IdxInsert to seek to the point within the b-tree where each key |
dan | e34162b | 2015-04-01 18:20:25 | [diff] [blame] | 3345 | ** should be inserted. This is faster. |
| 3346 | ** |
| 3347 | ** If any of the indexed columns use a collation sequence other than |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 3348 | ** BINARY, this optimization is disabled. This is because the user |
dan | e34162b | 2015-04-01 18:20:25 | [diff] [blame] | 3349 | ** might change the definition of a collation sequence and then run |
| 3350 | ** a VACUUM command. In that case keys may not be written in strictly |
| 3351 | ** sorted order. */ |
dan | a55a839 | 2021-02-18 15:59:37 | [diff] [blame] | 3352 | for(i=0; i<pSrcIdx->nColumn; i++){ |
| 3353 | const char *zColl = pSrcIdx->azColl[i]; |
| 3354 | if( sqlite3_stricmp(sqlite3StrBINARY, zColl) ) break; |
| 3355 | } |
| 3356 | if( i==pSrcIdx->nColumn ){ |
| 3357 | idxInsFlags = OPFLAG_USESEEKRESULT|OPFLAG_PREFORMAT; |
| 3358 | sqlite3VdbeAddOp1(v, OP_SeekEnd, iDest); |
| 3359 | sqlite3VdbeAddOp2(v, OP_RowCell, iDest, iSrc); |
dan | e34162b | 2015-04-01 18:20:25 | [diff] [blame] | 3360 | } |
drh | c84ad31 | 2020-02-06 20:46:08 | [diff] [blame] | 3361 | }else if( !HasRowid(pSrc) && pDestIdx->idxType==SQLITE_IDXTYPE_PRIMARYKEY ){ |
drh | 41b9ca2 | 2015-07-28 18:53:37 | [diff] [blame] | 3362 | idxInsFlags |= OPFLAG_NCHANGE; |
| 3363 | } |
dan | 7aae735 | 2020-12-10 18:06:24 | [diff] [blame] | 3364 | if( idxInsFlags!=(OPFLAG_USESEEKRESULT|OPFLAG_PREFORMAT) ){ |
dan | cd1b2d0 | 2020-12-09 20:33:51 | [diff] [blame] | 3365 | sqlite3VdbeAddOp3(v, OP_RowData, iSrc, regData, 1); |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 3366 | if( (db->mDbFlags & DBFLAG_Vacuum)==0 |
| 3367 | && !HasRowid(pDest) |
| 3368 | && IsPrimaryKeyIndex(pDestIdx) |
dan | a55a839 | 2021-02-18 15:59:37 | [diff] [blame] | 3369 | ){ |
dan | fadc0e3 | 2021-02-18 12:18:10 | [diff] [blame] | 3370 | codeWithoutRowidPreupdate(pParse, pDest, iDest, regData); |
| 3371 | } |
dan | cd1b2d0 | 2020-12-09 20:33:51 | [diff] [blame] | 3372 | } |
dan | 7aae735 | 2020-12-10 18:06:24 | [diff] [blame] | 3373 | sqlite3VdbeAddOp2(v, OP_IdxInsert, iDest, regData); |
| 3374 | sqlite3VdbeChangeP5(v, idxInsFlags|OPFLAG_APPEND); |
drh | 688852a | 2014-02-17 22:40:43 | [diff] [blame] | 3375 | sqlite3VdbeAddOp2(v, OP_Next, iSrc, addr1+1); VdbeCoverage(v); |
drh | 9d9cf22 | 2007-02-13 15:01:11 | [diff] [blame] | 3376 | sqlite3VdbeJumpHere(v, addr1); |
drh | 5554827 | 2013-10-23 16:03:07 | [diff] [blame] | 3377 | sqlite3VdbeAddOp2(v, OP_Close, iSrc, 0); |
| 3378 | sqlite3VdbeAddOp2(v, OP_Close, iDest, 0); |
drh | 9d9cf22 | 2007-02-13 15:01:11 | [diff] [blame] | 3379 | } |
drh | aceb31b | 2014-02-08 01:40:27 | [diff] [blame] | 3380 | if( emptySrcTest ) sqlite3VdbeJumpHere(v, emptySrcTest); |
drh | b765411 | 2008-01-12 12:48:07 | [diff] [blame] | 3381 | sqlite3ReleaseTempReg(pParse, regRowid); |
| 3382 | sqlite3ReleaseTempReg(pParse, regData); |
drh | 9d9cf22 | 2007-02-13 15:01:11 | [diff] [blame] | 3383 | if( emptyDestTest ){ |
drh | 1dd518c | 2016-10-03 02:59:33 | [diff] [blame] | 3384 | sqlite3AutoincrementEnd(pParse); |
drh | 66a5167 | 2008-01-03 00:01:23 | [diff] [blame] | 3385 | sqlite3VdbeAddOp2(v, OP_Halt, SQLITE_OK, 0); |
drh | 9d9cf22 | 2007-02-13 15:01:11 | [diff] [blame] | 3386 | sqlite3VdbeJumpHere(v, emptyDestTest); |
drh | 66a5167 | 2008-01-03 00:01:23 | [diff] [blame] | 3387 | sqlite3VdbeAddOp2(v, OP_Close, iDest, 0); |
drh | 9d9cf22 | 2007-02-13 15:01:11 | [diff] [blame] | 3388 | return 0; |
| 3389 | }else{ |
| 3390 | return 1; |
| 3391 | } |
| 3392 | } |
| 3393 | #endif /* SQLITE_OMIT_XFER_OPT */ |