drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 1 | /* |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 2 | ** 2001 September 15 |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 3 | ** |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 4 | ** The author disclaims copyright to this source code. In place of |
| 5 | ** a legal notice, here is a blessing: |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 6 | ** |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 7 | ** May you do good and not evil. |
| 8 | ** May you find forgiveness for yourself and forgive others. |
| 9 | ** May you share freely, never taking more than you give. |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 10 | ** |
| 11 | ************************************************************************* |
| 12 | ** This file contains C code routines that are called by the parser |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 13 | ** to handle INSERT statements in SQLite. |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 14 | */ |
| 15 | #include "sqliteInt.h" |
| 16 | |
| 17 | /* |
drh | 26198bb | 2013-10-31 11:15:09 +0000 | [diff] [blame] | 18 | ** Generate code that will |
drh | dd9930e | 2013-10-23 23:37:02 +0000 | [diff] [blame] | 19 | ** |
drh | 26198bb | 2013-10-31 11:15:09 +0000 | [diff] [blame] | 20 | ** (1) acquire a lock for table pTab then |
| 21 | ** (2) open pTab as cursor iCur. |
| 22 | ** |
| 23 | ** If pTab is a WITHOUT ROWID table, then it is the PRIMARY KEY index |
| 24 | ** for that table that is actually opened. |
drh | bbb5e4e | 2009-04-30 00:11:09 +0000 | [diff] [blame] | 25 | */ |
| 26 | void sqlite3OpenTable( |
drh | 2ec2fb2 | 2013-11-06 19:59:23 +0000 | [diff] [blame] | 27 | Parse *pParse, /* Generate code into this VDBE */ |
drh | bbb5e4e | 2009-04-30 00:11:09 +0000 | [diff] [blame] | 28 | int iCur, /* The cursor number of the table */ |
| 29 | int iDb, /* The database index in sqlite3.aDb[] */ |
| 30 | Table *pTab, /* The table to be opened */ |
| 31 | int opcode /* OP_OpenRead or OP_OpenWrite */ |
| 32 | ){ |
| 33 | Vdbe *v; |
drh | 5f53aac | 2012-12-03 19:42:39 +0000 | [diff] [blame] | 34 | assert( !IsVirtual(pTab) ); |
drh | 2ec2fb2 | 2013-11-06 19:59:23 +0000 | [diff] [blame] | 35 | v = sqlite3GetVdbe(pParse); |
drh | bbb5e4e | 2009-04-30 00:11:09 +0000 | [diff] [blame] | 36 | assert( opcode==OP_OpenWrite || opcode==OP_OpenRead ); |
drh | 2ec2fb2 | 2013-11-06 19:59:23 +0000 | [diff] [blame] | 37 | sqlite3TableLock(pParse, iDb, pTab->tnum, |
| 38 | (opcode==OP_OpenWrite)?1:0, pTab->zName); |
drh | ec95c44 | 2013-10-23 01:57:32 +0000 | [diff] [blame] | 39 | if( HasRowid(pTab) ){ |
drh | 261c02d | 2013-10-25 14:46:15 +0000 | [diff] [blame] | 40 | sqlite3VdbeAddOp4Int(v, opcode, iCur, pTab->tnum, iDb, pTab->nCol); |
drh | dd9930e | 2013-10-23 23:37:02 +0000 | [diff] [blame] | 41 | VdbeComment((v, "%s", pTab->zName)); |
drh | 26198bb | 2013-10-31 11:15:09 +0000 | [diff] [blame] | 42 | }else{ |
drh | dd9930e | 2013-10-23 23:37:02 +0000 | [diff] [blame] | 43 | Index *pPk = sqlite3PrimaryKeyIndex(pTab); |
| 44 | assert( pPk!=0 ); |
drh | afe028a | 2015-05-22 13:09:50 +0000 | [diff] [blame] | 45 | assert( pPk->tnum==pTab->tnum ); |
drh | 2ec2fb2 | 2013-11-06 19:59:23 +0000 | [diff] [blame] | 46 | sqlite3VdbeAddOp3(v, opcode, iCur, pPk->tnum, iDb); |
| 47 | sqlite3VdbeSetP4KeyInfo(pParse, pPk); |
drh | ec95c44 | 2013-10-23 01:57:32 +0000 | [diff] [blame] | 48 | VdbeComment((v, "%s", pTab->zName)); |
| 49 | } |
drh | bbb5e4e | 2009-04-30 00:11:09 +0000 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | /* |
dan | 69f8bb9 | 2009-08-13 19:21:16 +0000 | [diff] [blame] | 53 | ** Return a pointer to the column affinity string associated with index |
| 54 | ** pIdx. A column affinity string has one character for each column in |
| 55 | ** the table, according to the affinity of the column: |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 +0000 | [diff] [blame] | 56 | ** |
| 57 | ** Character Column affinity |
| 58 | ** ------------------------------ |
drh | 05883a3 | 2015-06-02 15:32:08 +0000 | [diff] [blame] | 59 | ** 'A' BLOB |
drh | 4583c37 | 2014-09-19 20:13:25 +0000 | [diff] [blame] | 60 | ** 'B' TEXT |
| 61 | ** 'C' NUMERIC |
| 62 | ** 'D' INTEGER |
| 63 | ** 'F' REAL |
drh | 2d401ab | 2008-01-10 23:50:11 +0000 | [diff] [blame] | 64 | ** |
drh | 4583c37 | 2014-09-19 20:13:25 +0000 | [diff] [blame] | 65 | ** An extra 'D' is appended to the end of the string to cover the |
drh | 2d401ab | 2008-01-10 23:50:11 +0000 | [diff] [blame] | 66 | ** rowid that appears as the last column in every index. |
dan | 69f8bb9 | 2009-08-13 19:21:16 +0000 | [diff] [blame] | 67 | ** |
| 68 | ** Memory for the buffer containing the column index affinity string |
| 69 | ** is managed along with the rest of the Index structure. It will be |
| 70 | ** released when sqlite3DeleteIndex() is called. |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 +0000 | [diff] [blame] | 71 | */ |
drh | e910769 | 2015-08-25 19:20:04 +0000 | [diff] [blame] | 72 | const char *sqlite3IndexAffinityStr(sqlite3 *db, Index *pIdx){ |
danielk1977 | a37cdde | 2004-05-16 11:15:36 +0000 | [diff] [blame] | 73 | if( !pIdx->zColAff ){ |
danielk1977 | e014a83 | 2004-05-17 10:48:57 +0000 | [diff] [blame] | 74 | /* The first time a column affinity string for a particular index is |
danielk1977 | a37cdde | 2004-05-16 11:15:36 +0000 | [diff] [blame] | 75 | ** required, it is allocated and populated here. It is then stored as |
danielk1977 | e014a83 | 2004-05-17 10:48:57 +0000 | [diff] [blame] | 76 | ** a member of the Index structure for subsequent use. |
danielk1977 | a37cdde | 2004-05-16 11:15:36 +0000 | [diff] [blame] | 77 | ** |
| 78 | ** The column affinity string will eventually be deleted by |
danielk1977 | e014a83 | 2004-05-17 10:48:57 +0000 | [diff] [blame] | 79 | ** sqliteDeleteIndex() when the Index structure itself is cleaned |
danielk1977 | a37cdde | 2004-05-16 11:15:36 +0000 | [diff] [blame] | 80 | ** up. |
| 81 | */ |
| 82 | int n; |
| 83 | Table *pTab = pIdx->pTable; |
drh | ad12432 | 2013-10-23 13:30:58 +0000 | [diff] [blame] | 84 | pIdx->zColAff = (char *)sqlite3DbMallocRaw(0, pIdx->nColumn+1); |
danielk1977 | a37cdde | 2004-05-16 11:15:36 +0000 | [diff] [blame] | 85 | if( !pIdx->zColAff ){ |
drh | 4a642b6 | 2016-02-05 01:55:27 +0000 | [diff] [blame] | 86 | sqlite3OomFault(db); |
dan | 69f8bb9 | 2009-08-13 19:21:16 +0000 | [diff] [blame] | 87 | return 0; |
danielk1977 | a37cdde | 2004-05-16 11:15:36 +0000 | [diff] [blame] | 88 | } |
| 89 | for(n=0; n<pIdx->nColumn; n++){ |
drh | ad12432 | 2013-10-23 13:30:58 +0000 | [diff] [blame] | 90 | i16 x = pIdx->aiColumn[n]; |
drh | 1f9ca2c | 2015-08-25 16:57:52 +0000 | [diff] [blame] | 91 | if( x>=0 ){ |
| 92 | pIdx->zColAff[n] = pTab->aCol[x].affinity; |
drh | 4b92f98 | 2015-09-29 17:20:14 +0000 | [diff] [blame] | 93 | }else if( x==XN_ROWID ){ |
drh | 1f9ca2c | 2015-08-25 16:57:52 +0000 | [diff] [blame] | 94 | pIdx->zColAff[n] = SQLITE_AFF_INTEGER; |
| 95 | }else{ |
drh | 6860e6f | 2015-08-27 18:24:02 +0000 | [diff] [blame] | 96 | char aff; |
drh | 4b92f98 | 2015-09-29 17:20:14 +0000 | [diff] [blame] | 97 | assert( x==XN_EXPR ); |
drh | 1f9ca2c | 2015-08-25 16:57:52 +0000 | [diff] [blame] | 98 | assert( pIdx->aColExpr!=0 ); |
drh | 6860e6f | 2015-08-27 18:24:02 +0000 | [diff] [blame] | 99 | aff = sqlite3ExprAffinity(pIdx->aColExpr->a[n].pExpr); |
| 100 | if( aff==0 ) aff = SQLITE_AFF_BLOB; |
| 101 | pIdx->zColAff[n] = aff; |
drh | 1f9ca2c | 2015-08-25 16:57:52 +0000 | [diff] [blame] | 102 | } |
danielk1977 | a37cdde | 2004-05-16 11:15:36 +0000 | [diff] [blame] | 103 | } |
drh | 2d401ab | 2008-01-10 23:50:11 +0000 | [diff] [blame] | 104 | pIdx->zColAff[n] = 0; |
danielk1977 | a37cdde | 2004-05-16 11:15:36 +0000 | [diff] [blame] | 105 | } |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 +0000 | [diff] [blame] | 106 | |
dan | 69f8bb9 | 2009-08-13 19:21:16 +0000 | [diff] [blame] | 107 | return pIdx->zColAff; |
danielk1977 | a37cdde | 2004-05-16 11:15:36 +0000 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | /* |
drh | 57bf4a8 | 2014-02-17 14:59:22 +0000 | [diff] [blame] | 111 | ** Compute the affinity string for table pTab, if it has not already been |
drh | 05883a3 | 2015-06-02 15:32:08 +0000 | [diff] [blame] | 112 | ** computed. As an optimization, omit trailing SQLITE_AFF_BLOB affinities. |
drh | 57bf4a8 | 2014-02-17 14:59:22 +0000 | [diff] [blame] | 113 | ** |
drh | 05883a3 | 2015-06-02 15:32:08 +0000 | [diff] [blame] | 114 | ** If the affinity exists (if it is no entirely SQLITE_AFF_BLOB values) and |
drh | 57bf4a8 | 2014-02-17 14:59:22 +0000 | [diff] [blame] | 115 | ** if iReg>0 then code an OP_Affinity opcode that will set the affinities |
| 116 | ** for register iReg and following. Or if affinities exists and iReg==0, |
| 117 | ** then just set the P4 operand of the previous opcode (which should be |
| 118 | ** an OP_MakeRecord) to the affinity string. |
| 119 | ** |
drh | b6e8fd1 | 2014-03-06 01:56:33 +0000 | [diff] [blame] | 120 | ** A column affinity string has one character per column: |
danielk1977 | a37cdde | 2004-05-16 11:15:36 +0000 | [diff] [blame] | 121 | ** |
| 122 | ** Character Column affinity |
| 123 | ** ------------------------------ |
drh | 05883a3 | 2015-06-02 15:32:08 +0000 | [diff] [blame] | 124 | ** 'A' BLOB |
drh | 4583c37 | 2014-09-19 20:13:25 +0000 | [diff] [blame] | 125 | ** 'B' TEXT |
| 126 | ** 'C' NUMERIC |
| 127 | ** 'D' INTEGER |
| 128 | ** 'E' REAL |
danielk1977 | a37cdde | 2004-05-16 11:15:36 +0000 | [diff] [blame] | 129 | */ |
drh | 57bf4a8 | 2014-02-17 14:59:22 +0000 | [diff] [blame] | 130 | void sqlite3TableAffinity(Vdbe *v, Table *pTab, int iReg){ |
| 131 | int i; |
| 132 | char *zColAff = pTab->zColAff; |
| 133 | if( zColAff==0 ){ |
drh | abb6fca | 2007-08-16 12:24:01 +0000 | [diff] [blame] | 134 | sqlite3 *db = sqlite3VdbeDb(v); |
drh | b975598 | 2010-07-24 16:34:37 +0000 | [diff] [blame] | 135 | zColAff = (char *)sqlite3DbMallocRaw(0, pTab->nCol+1); |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 +0000 | [diff] [blame] | 136 | if( !zColAff ){ |
drh | 4a642b6 | 2016-02-05 01:55:27 +0000 | [diff] [blame] | 137 | sqlite3OomFault(db); |
danielk1977 | a37cdde | 2004-05-16 11:15:36 +0000 | [diff] [blame] | 138 | return; |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 +0000 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | for(i=0; i<pTab->nCol; i++){ |
danielk1977 | a37cdde | 2004-05-16 11:15:36 +0000 | [diff] [blame] | 142 | zColAff[i] = pTab->aCol[i].affinity; |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 +0000 | [diff] [blame] | 143 | } |
drh | 57bf4a8 | 2014-02-17 14:59:22 +0000 | [diff] [blame] | 144 | do{ |
| 145 | zColAff[i--] = 0; |
drh | 05883a3 | 2015-06-02 15:32:08 +0000 | [diff] [blame] | 146 | }while( i>=0 && zColAff[i]==SQLITE_AFF_BLOB ); |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 +0000 | [diff] [blame] | 147 | pTab->zColAff = zColAff; |
| 148 | } |
drh | 57bf4a8 | 2014-02-17 14:59:22 +0000 | [diff] [blame] | 149 | i = sqlite3Strlen30(zColAff); |
| 150 | if( i ){ |
| 151 | if( iReg ){ |
| 152 | sqlite3VdbeAddOp4(v, OP_Affinity, iReg, i, 0, zColAff, i); |
| 153 | }else{ |
| 154 | sqlite3VdbeChangeP4(v, -1, zColAff, i); |
| 155 | } |
| 156 | } |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 +0000 | [diff] [blame] | 157 | } |
| 158 | |
danielk1977 | 4d88778 | 2005-02-08 08:42:27 +0000 | [diff] [blame] | 159 | /* |
drh | 48d1178 | 2007-11-23 15:02:19 +0000 | [diff] [blame] | 160 | ** Return non-zero if the table pTab in database iDb or any of its indices |
drh | b6e8fd1 | 2014-03-06 01:56:33 +0000 | [diff] [blame] | 161 | ** have been opened at any point in the VDBE program. This is used to see if |
drh | 48d1178 | 2007-11-23 15:02:19 +0000 | [diff] [blame] | 162 | ** a statement of the form "INSERT INTO <iDb, pTab> SELECT ..." can |
drh | b6e8fd1 | 2014-03-06 01:56:33 +0000 | [diff] [blame] | 163 | ** run without using a temporary table for the results of the SELECT. |
danielk1977 | 4d88778 | 2005-02-08 08:42:27 +0000 | [diff] [blame] | 164 | */ |
drh | 05a86c5 | 2014-02-16 01:55:49 +0000 | [diff] [blame] | 165 | static int readsTable(Parse *p, int iDb, Table *pTab){ |
danielk1977 | 595a523 | 2009-07-24 17:58:53 +0000 | [diff] [blame] | 166 | Vdbe *v = sqlite3GetVdbe(p); |
danielk1977 | 4d88778 | 2005-02-08 08:42:27 +0000 | [diff] [blame] | 167 | int i; |
drh | 48d1178 | 2007-11-23 15:02:19 +0000 | [diff] [blame] | 168 | int iEnd = sqlite3VdbeCurrentAddr(v); |
danielk1977 | 595a523 | 2009-07-24 17:58:53 +0000 | [diff] [blame] | 169 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
| 170 | VTable *pVTab = IsVirtual(pTab) ? sqlite3GetVTable(p->db, pTab) : 0; |
| 171 | #endif |
| 172 | |
drh | 05a86c5 | 2014-02-16 01:55:49 +0000 | [diff] [blame] | 173 | for(i=1; i<iEnd; i++){ |
drh | 48d1178 | 2007-11-23 15:02:19 +0000 | [diff] [blame] | 174 | VdbeOp *pOp = sqlite3VdbeGetOp(v, i); |
drh | ef0bea9 | 2007-12-14 16:11:09 +0000 | [diff] [blame] | 175 | assert( pOp!=0 ); |
danielk1977 | 207872a | 2008-01-03 07:54:23 +0000 | [diff] [blame] | 176 | if( pOp->opcode==OP_OpenRead && pOp->p3==iDb ){ |
| 177 | Index *pIndex; |
drh | 48d1178 | 2007-11-23 15:02:19 +0000 | [diff] [blame] | 178 | int tnum = pOp->p2; |
danielk1977 | 207872a | 2008-01-03 07:54:23 +0000 | [diff] [blame] | 179 | if( tnum==pTab->tnum ){ |
| 180 | return 1; |
| 181 | } |
| 182 | for(pIndex=pTab->pIndex; pIndex; pIndex=pIndex->pNext){ |
| 183 | if( tnum==pIndex->tnum ){ |
drh | 48d1178 | 2007-11-23 15:02:19 +0000 | [diff] [blame] | 184 | return 1; |
| 185 | } |
drh | 48d1178 | 2007-11-23 15:02:19 +0000 | [diff] [blame] | 186 | } |
| 187 | } |
drh | 543165e | 2007-11-27 14:46:41 +0000 | [diff] [blame] | 188 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
danielk1977 | 595a523 | 2009-07-24 17:58:53 +0000 | [diff] [blame] | 189 | if( pOp->opcode==OP_VOpen && pOp->p4.pVtab==pVTab ){ |
danielk1977 | 2dca4ac | 2008-01-03 11:50:29 +0000 | [diff] [blame] | 190 | assert( pOp->p4.pVtab!=0 ); |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 191 | assert( pOp->p4type==P4_VTAB ); |
drh | 48d1178 | 2007-11-23 15:02:19 +0000 | [diff] [blame] | 192 | return 1; |
danielk1977 | 4d88778 | 2005-02-08 08:42:27 +0000 | [diff] [blame] | 193 | } |
drh | 543165e | 2007-11-27 14:46:41 +0000 | [diff] [blame] | 194 | #endif |
danielk1977 | 4d88778 | 2005-02-08 08:42:27 +0000 | [diff] [blame] | 195 | } |
| 196 | return 0; |
| 197 | } |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 +0000 | [diff] [blame] | 198 | |
drh | 9d9cf22 | 2007-02-13 15:01:11 +0000 | [diff] [blame] | 199 | #ifndef SQLITE_OMIT_AUTOINCREMENT |
| 200 | /* |
drh | 0b9f50d | 2009-06-23 20:28:53 +0000 | [diff] [blame] | 201 | ** Locate or create an AutoincInfo structure associated with table pTab |
| 202 | ** which is in database iDb. Return the register number for the register |
drh | 9ef5e77 | 2016-08-19 14:20:56 +0000 | [diff] [blame] | 203 | ** that holds the maximum rowid. Return zero if pTab is not an AUTOINCREMENT |
| 204 | ** table. (Also return zero when doing a VACUUM since we do not want to |
| 205 | ** update the AUTOINCREMENT counters during a VACUUM.) |
drh | 9d9cf22 | 2007-02-13 15:01:11 +0000 | [diff] [blame] | 206 | ** |
drh | 0b9f50d | 2009-06-23 20:28:53 +0000 | [diff] [blame] | 207 | ** There is at most one AutoincInfo structure per table even if the |
| 208 | ** same table is autoincremented multiple times due to inserts within |
| 209 | ** triggers. A new AutoincInfo structure is created if this is the |
| 210 | ** first use of table pTab. On 2nd and subsequent uses, the original |
| 211 | ** AutoincInfo structure is used. |
drh | 9d9cf22 | 2007-02-13 15:01:11 +0000 | [diff] [blame] | 212 | ** |
drh | c8abbc1 | 2018-03-16 18:46:30 +0000 | [diff] [blame] | 213 | ** Four consecutive registers are allocated: |
drh | 0b9f50d | 2009-06-23 20:28:53 +0000 | [diff] [blame] | 214 | ** |
drh | c8abbc1 | 2018-03-16 18:46:30 +0000 | [diff] [blame] | 215 | ** (1) The name of the pTab table. |
| 216 | ** (2) The maximum ROWID of pTab. |
| 217 | ** (3) The rowid in sqlite_sequence of pTab |
| 218 | ** (4) The original value of the max ROWID in pTab, or NULL if none |
drh | 0b9f50d | 2009-06-23 20:28:53 +0000 | [diff] [blame] | 219 | ** |
| 220 | ** The 2nd register is the one that is returned. That is all the |
| 221 | ** insert routine needs to know about. |
drh | 9d9cf22 | 2007-02-13 15:01:11 +0000 | [diff] [blame] | 222 | */ |
| 223 | static int autoIncBegin( |
| 224 | Parse *pParse, /* Parsing context */ |
| 225 | int iDb, /* Index of the database holding pTab */ |
| 226 | Table *pTab /* The table we are writing to */ |
| 227 | ){ |
drh | 6a288a3 | 2008-01-07 19:20:24 +0000 | [diff] [blame] | 228 | int memId = 0; /* Register holding maximum rowid */ |
drh | 9ef5e77 | 2016-08-19 14:20:56 +0000 | [diff] [blame] | 229 | if( (pTab->tabFlags & TF_Autoincrement)!=0 |
drh | 8257aa8 | 2017-07-26 19:59:13 +0000 | [diff] [blame] | 230 | && (pParse->db->mDbFlags & DBFLAG_Vacuum)==0 |
drh | 9ef5e77 | 2016-08-19 14:20:56 +0000 | [diff] [blame] | 231 | ){ |
dan | 65a7cd1 | 2009-09-01 12:16:01 +0000 | [diff] [blame] | 232 | Parse *pToplevel = sqlite3ParseToplevel(pParse); |
drh | 0b9f50d | 2009-06-23 20:28:53 +0000 | [diff] [blame] | 233 | AutoincInfo *pInfo; |
| 234 | |
dan | 65a7cd1 | 2009-09-01 12:16:01 +0000 | [diff] [blame] | 235 | pInfo = pToplevel->pAinc; |
drh | 0b9f50d | 2009-06-23 20:28:53 +0000 | [diff] [blame] | 236 | while( pInfo && pInfo->pTab!=pTab ){ pInfo = pInfo->pNext; } |
| 237 | if( pInfo==0 ){ |
drh | 575fad6 | 2016-02-05 13:38:36 +0000 | [diff] [blame] | 238 | pInfo = sqlite3DbMallocRawNN(pParse->db, sizeof(*pInfo)); |
drh | 0b9f50d | 2009-06-23 20:28:53 +0000 | [diff] [blame] | 239 | if( pInfo==0 ) return 0; |
dan | 65a7cd1 | 2009-09-01 12:16:01 +0000 | [diff] [blame] | 240 | pInfo->pNext = pToplevel->pAinc; |
| 241 | pToplevel->pAinc = pInfo; |
drh | 0b9f50d | 2009-06-23 20:28:53 +0000 | [diff] [blame] | 242 | pInfo->pTab = pTab; |
| 243 | pInfo->iDb = iDb; |
dan | 65a7cd1 | 2009-09-01 12:16:01 +0000 | [diff] [blame] | 244 | pToplevel->nMem++; /* Register to hold name of table */ |
| 245 | pInfo->regCtr = ++pToplevel->nMem; /* Max rowid register */ |
drh | c8abbc1 | 2018-03-16 18:46:30 +0000 | [diff] [blame] | 246 | pToplevel->nMem +=2; /* Rowid in sqlite_sequence + orig max val */ |
drh | 0b9f50d | 2009-06-23 20:28:53 +0000 | [diff] [blame] | 247 | } |
| 248 | memId = pInfo->regCtr; |
drh | 9d9cf22 | 2007-02-13 15:01:11 +0000 | [diff] [blame] | 249 | } |
| 250 | return memId; |
| 251 | } |
| 252 | |
| 253 | /* |
drh | 0b9f50d | 2009-06-23 20:28:53 +0000 | [diff] [blame] | 254 | ** This routine generates code that will initialize all of the |
| 255 | ** register used by the autoincrement tracker. |
| 256 | */ |
| 257 | void sqlite3AutoincrementBegin(Parse *pParse){ |
| 258 | AutoincInfo *p; /* Information about an AUTOINCREMENT */ |
| 259 | sqlite3 *db = pParse->db; /* The database connection */ |
| 260 | Db *pDb; /* Database only autoinc table */ |
| 261 | int memId; /* Register holding max rowid */ |
drh | 0b9f50d | 2009-06-23 20:28:53 +0000 | [diff] [blame] | 262 | Vdbe *v = pParse->pVdbe; /* VDBE under construction */ |
| 263 | |
drh | 345ba7d | 2009-09-08 13:40:16 +0000 | [diff] [blame] | 264 | /* This routine is never called during trigger-generation. It is |
| 265 | ** only called from the top-level */ |
| 266 | assert( pParse->pTriggerTab==0 ); |
drh | c149f18 | 2015-09-29 13:25:15 +0000 | [diff] [blame] | 267 | assert( sqlite3IsToplevel(pParse) ); |
dan | 76d462e | 2009-08-30 11:42:51 +0000 | [diff] [blame] | 268 | |
drh | 0b9f50d | 2009-06-23 20:28:53 +0000 | [diff] [blame] | 269 | assert( v ); /* We failed long ago if this is not so */ |
| 270 | for(p = pParse->pAinc; p; p = p->pNext){ |
drh | 1b32554 | 2016-02-03 01:55:44 +0000 | [diff] [blame] | 271 | static const int iLn = VDBE_OFFSET_LINENO(2); |
| 272 | static const VdbeOpList autoInc[] = { |
| 273 | /* 0 */ {OP_Null, 0, 0, 0}, |
drh | c8abbc1 | 2018-03-16 18:46:30 +0000 | [diff] [blame] | 274 | /* 1 */ {OP_Rewind, 0, 10, 0}, |
drh | 1b32554 | 2016-02-03 01:55:44 +0000 | [diff] [blame] | 275 | /* 2 */ {OP_Column, 0, 0, 0}, |
drh | c8abbc1 | 2018-03-16 18:46:30 +0000 | [diff] [blame] | 276 | /* 3 */ {OP_Ne, 0, 9, 0}, |
drh | 1b32554 | 2016-02-03 01:55:44 +0000 | [diff] [blame] | 277 | /* 4 */ {OP_Rowid, 0, 0, 0}, |
| 278 | /* 5 */ {OP_Column, 0, 1, 0}, |
drh | c8abbc1 | 2018-03-16 18:46:30 +0000 | [diff] [blame] | 279 | /* 6 */ {OP_AddImm, 0, 0, 0}, |
| 280 | /* 7 */ {OP_Copy, 0, 0, 0}, |
| 281 | /* 8 */ {OP_Goto, 0, 11, 0}, |
| 282 | /* 9 */ {OP_Next, 0, 2, 0}, |
| 283 | /* 10 */ {OP_Integer, 0, 0, 0}, |
| 284 | /* 11 */ {OP_Close, 0, 0, 0} |
drh | 1b32554 | 2016-02-03 01:55:44 +0000 | [diff] [blame] | 285 | }; |
| 286 | VdbeOp *aOp; |
drh | 0b9f50d | 2009-06-23 20:28:53 +0000 | [diff] [blame] | 287 | pDb = &db->aDb[p->iDb]; |
| 288 | memId = p->regCtr; |
drh | 2120608 | 2011-04-04 18:22:02 +0000 | [diff] [blame] | 289 | assert( sqlite3SchemaMutexHeld(db, 0, pDb->pSchema) ); |
drh | 0b9f50d | 2009-06-23 20:28:53 +0000 | [diff] [blame] | 290 | sqlite3OpenTable(pParse, 0, p->iDb, pDb->pSchema->pSeqTab, OP_OpenRead); |
drh | 076e85f | 2015-09-03 13:46:12 +0000 | [diff] [blame] | 291 | sqlite3VdbeLoadString(v, memId-1, p->pTab->zName); |
drh | 1b32554 | 2016-02-03 01:55:44 +0000 | [diff] [blame] | 292 | aOp = sqlite3VdbeAddOpList(v, ArraySize(autoInc), autoInc, iLn); |
| 293 | if( aOp==0 ) break; |
| 294 | aOp[0].p2 = memId; |
drh | c8abbc1 | 2018-03-16 18:46:30 +0000 | [diff] [blame] | 295 | aOp[0].p3 = memId+2; |
drh | 1b32554 | 2016-02-03 01:55:44 +0000 | [diff] [blame] | 296 | aOp[2].p3 = memId; |
| 297 | aOp[3].p1 = memId-1; |
| 298 | aOp[3].p3 = memId; |
| 299 | aOp[3].p5 = SQLITE_JUMPIFNULL; |
| 300 | aOp[4].p2 = memId+1; |
| 301 | aOp[5].p3 = memId; |
drh | c8abbc1 | 2018-03-16 18:46:30 +0000 | [diff] [blame] | 302 | aOp[6].p1 = memId; |
| 303 | aOp[7].p2 = memId+2; |
| 304 | aOp[7].p1 = memId; |
| 305 | aOp[10].p2 = memId; |
drh | 0b9f50d | 2009-06-23 20:28:53 +0000 | [diff] [blame] | 306 | } |
| 307 | } |
| 308 | |
| 309 | /* |
drh | 9d9cf22 | 2007-02-13 15:01:11 +0000 | [diff] [blame] | 310 | ** Update the maximum rowid for an autoincrement calculation. |
| 311 | ** |
drh | 1b32554 | 2016-02-03 01:55:44 +0000 | [diff] [blame] | 312 | ** This routine should be called when the regRowid register holds a |
drh | 9d9cf22 | 2007-02-13 15:01:11 +0000 | [diff] [blame] | 313 | ** new rowid that is about to be inserted. If that new rowid is |
| 314 | ** larger than the maximum rowid in the memId memory cell, then the |
drh | 1b32554 | 2016-02-03 01:55:44 +0000 | [diff] [blame] | 315 | ** memory cell is updated. |
drh | 9d9cf22 | 2007-02-13 15:01:11 +0000 | [diff] [blame] | 316 | */ |
drh | 6a288a3 | 2008-01-07 19:20:24 +0000 | [diff] [blame] | 317 | static void autoIncStep(Parse *pParse, int memId, int regRowid){ |
drh | 9d9cf22 | 2007-02-13 15:01:11 +0000 | [diff] [blame] | 318 | if( memId>0 ){ |
drh | 6a288a3 | 2008-01-07 19:20:24 +0000 | [diff] [blame] | 319 | sqlite3VdbeAddOp2(pParse->pVdbe, OP_MemMax, memId, regRowid); |
drh | 9d9cf22 | 2007-02-13 15:01:11 +0000 | [diff] [blame] | 320 | } |
| 321 | } |
| 322 | |
| 323 | /* |
drh | 0b9f50d | 2009-06-23 20:28:53 +0000 | [diff] [blame] | 324 | ** This routine generates the code needed to write autoincrement |
| 325 | ** maximum rowid values back into the sqlite_sequence register. |
| 326 | ** Every statement that might do an INSERT into an autoincrement |
| 327 | ** table (either directly or through triggers) needs to call this |
| 328 | ** routine just before the "exit" code. |
drh | 9d9cf22 | 2007-02-13 15:01:11 +0000 | [diff] [blame] | 329 | */ |
drh | 1b32554 | 2016-02-03 01:55:44 +0000 | [diff] [blame] | 330 | static SQLITE_NOINLINE void autoIncrementEnd(Parse *pParse){ |
drh | 0b9f50d | 2009-06-23 20:28:53 +0000 | [diff] [blame] | 331 | AutoincInfo *p; |
| 332 | Vdbe *v = pParse->pVdbe; |
| 333 | sqlite3 *db = pParse->db; |
drh | 6a288a3 | 2008-01-07 19:20:24 +0000 | [diff] [blame] | 334 | |
drh | 0b9f50d | 2009-06-23 20:28:53 +0000 | [diff] [blame] | 335 | assert( v ); |
| 336 | for(p = pParse->pAinc; p; p = p->pNext){ |
drh | 1b32554 | 2016-02-03 01:55:44 +0000 | [diff] [blame] | 337 | static const int iLn = VDBE_OFFSET_LINENO(2); |
| 338 | static const VdbeOpList autoIncEnd[] = { |
| 339 | /* 0 */ {OP_NotNull, 0, 2, 0}, |
| 340 | /* 1 */ {OP_NewRowid, 0, 0, 0}, |
| 341 | /* 2 */ {OP_MakeRecord, 0, 2, 0}, |
| 342 | /* 3 */ {OP_Insert, 0, 0, 0}, |
| 343 | /* 4 */ {OP_Close, 0, 0, 0} |
| 344 | }; |
| 345 | VdbeOp *aOp; |
drh | 0b9f50d | 2009-06-23 20:28:53 +0000 | [diff] [blame] | 346 | Db *pDb = &db->aDb[p->iDb]; |
drh | 0b9f50d | 2009-06-23 20:28:53 +0000 | [diff] [blame] | 347 | int iRec; |
| 348 | int memId = p->regCtr; |
| 349 | |
| 350 | iRec = sqlite3GetTempReg(pParse); |
drh | 2120608 | 2011-04-04 18:22:02 +0000 | [diff] [blame] | 351 | assert( sqlite3SchemaMutexHeld(db, 0, pDb->pSchema) ); |
drh | c8abbc1 | 2018-03-16 18:46:30 +0000 | [diff] [blame] | 352 | sqlite3VdbeAddOp3(v, OP_Le, memId+2, sqlite3VdbeCurrentAddr(v)+7, memId); |
| 353 | VdbeCoverage(v); |
drh | 0b9f50d | 2009-06-23 20:28:53 +0000 | [diff] [blame] | 354 | sqlite3OpenTable(pParse, 0, p->iDb, pDb->pSchema->pSeqTab, OP_OpenWrite); |
drh | 1b32554 | 2016-02-03 01:55:44 +0000 | [diff] [blame] | 355 | aOp = sqlite3VdbeAddOpList(v, ArraySize(autoIncEnd), autoIncEnd, iLn); |
| 356 | if( aOp==0 ) break; |
| 357 | aOp[0].p1 = memId+1; |
| 358 | aOp[1].p2 = memId+1; |
| 359 | aOp[2].p1 = memId-1; |
| 360 | aOp[2].p3 = iRec; |
| 361 | aOp[3].p2 = iRec; |
| 362 | aOp[3].p3 = memId+1; |
| 363 | aOp[3].p5 = OPFLAG_APPEND; |
drh | 0b9f50d | 2009-06-23 20:28:53 +0000 | [diff] [blame] | 364 | sqlite3ReleaseTempReg(pParse, iRec); |
drh | 9d9cf22 | 2007-02-13 15:01:11 +0000 | [diff] [blame] | 365 | } |
| 366 | } |
drh | 1b32554 | 2016-02-03 01:55:44 +0000 | [diff] [blame] | 367 | void sqlite3AutoincrementEnd(Parse *pParse){ |
| 368 | if( pParse->pAinc ) autoIncrementEnd(pParse); |
| 369 | } |
drh | 9d9cf22 | 2007-02-13 15:01:11 +0000 | [diff] [blame] | 370 | #else |
| 371 | /* |
| 372 | ** If SQLITE_OMIT_AUTOINCREMENT is defined, then the three routines |
| 373 | ** above are all no-ops |
| 374 | */ |
| 375 | # define autoIncBegin(A,B,C) (0) |
danielk1977 | 287fb61 | 2008-01-04 19:10:28 +0000 | [diff] [blame] | 376 | # define autoIncStep(A,B,C) |
drh | 9d9cf22 | 2007-02-13 15:01:11 +0000 | [diff] [blame] | 377 | #endif /* SQLITE_OMIT_AUTOINCREMENT */ |
| 378 | |
| 379 | |
| 380 | /* Forward declaration */ |
| 381 | static int xferOptimization( |
| 382 | Parse *pParse, /* Parser context */ |
| 383 | Table *pDest, /* The table we are inserting into */ |
| 384 | Select *pSelect, /* A SELECT statement to use as the data source */ |
| 385 | int onError, /* How to handle constraint errors */ |
| 386 | int iDbDest /* The database of pDest */ |
| 387 | ); |
| 388 | |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 +0000 | [diff] [blame] | 389 | /* |
drh | d82b502 | 2013-10-26 00:58:34 +0000 | [diff] [blame] | 390 | ** This routine is called to handle SQL of the following forms: |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 391 | ** |
drh | a21f78b | 2015-04-19 18:32:43 +0000 | [diff] [blame] | 392 | ** insert into TABLE (IDLIST) values(EXPRLIST),(EXPRLIST),... |
drh | 1ccde15 | 2000-06-17 13:12:39 +0000 | [diff] [blame] | 393 | ** insert into TABLE (IDLIST) select |
drh | a21f78b | 2015-04-19 18:32:43 +0000 | [diff] [blame] | 394 | ** insert into TABLE (IDLIST) default values |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 395 | ** |
drh | 1ccde15 | 2000-06-17 13:12:39 +0000 | [diff] [blame] | 396 | ** The IDLIST following the table name is always optional. If omitted, |
drh | a21f78b | 2015-04-19 18:32:43 +0000 | [diff] [blame] | 397 | ** then a list of all (non-hidden) columns for the table is substituted. |
| 398 | ** The IDLIST appears in the pColumn parameter. pColumn is NULL if IDLIST |
| 399 | ** is omitted. |
drh | 1ccde15 | 2000-06-17 13:12:39 +0000 | [diff] [blame] | 400 | ** |
drh | a21f78b | 2015-04-19 18:32:43 +0000 | [diff] [blame] | 401 | ** For the pSelect parameter holds the values to be inserted for the |
| 402 | ** first two forms shown above. A VALUES clause is really just short-hand |
| 403 | ** for a SELECT statement that omits the FROM clause and everything else |
| 404 | ** that follows. If the pSelect parameter is NULL, that means that the |
| 405 | ** DEFAULT VALUES form of the INSERT statement is intended. |
drh | 142e30d | 2002-08-28 03:00:58 +0000 | [diff] [blame] | 406 | ** |
drh | 9d9cf22 | 2007-02-13 15:01:11 +0000 | [diff] [blame] | 407 | ** The code generated follows one of four templates. For a simple |
drh | a21f78b | 2015-04-19 18:32:43 +0000 | [diff] [blame] | 408 | ** insert with data coming from a single-row VALUES clause, the code executes |
drh | e00ee6e | 2008-06-20 15:24:01 +0000 | [diff] [blame] | 409 | ** once straight down through. Pseudo-code follows (we call this |
| 410 | ** the "1st template"): |
drh | 142e30d | 2002-08-28 03:00:58 +0000 | [diff] [blame] | 411 | ** |
| 412 | ** open write cursor to <table> and its indices |
drh | ec95c44 | 2013-10-23 01:57:32 +0000 | [diff] [blame] | 413 | ** put VALUES clause expressions into registers |
drh | 142e30d | 2002-08-28 03:00:58 +0000 | [diff] [blame] | 414 | ** write the resulting record into <table> |
| 415 | ** cleanup |
| 416 | ** |
drh | 9d9cf22 | 2007-02-13 15:01:11 +0000 | [diff] [blame] | 417 | ** The three remaining templates assume the statement is of the form |
drh | 142e30d | 2002-08-28 03:00:58 +0000 | [diff] [blame] | 418 | ** |
| 419 | ** INSERT INTO <table> SELECT ... |
| 420 | ** |
drh | 9d9cf22 | 2007-02-13 15:01:11 +0000 | [diff] [blame] | 421 | ** If the SELECT clause is of the restricted form "SELECT * FROM <table2>" - |
| 422 | ** in other words if the SELECT pulls all columns from a single table |
| 423 | ** and there is no WHERE or LIMIT or GROUP BY or ORDER BY clauses, and |
| 424 | ** if <table2> and <table1> are distinct tables but have identical |
| 425 | ** schemas, including all the same indices, then a special optimization |
| 426 | ** is invoked that copies raw records from <table2> over to <table1>. |
| 427 | ** See the xferOptimization() function for the implementation of this |
drh | e00ee6e | 2008-06-20 15:24:01 +0000 | [diff] [blame] | 428 | ** template. This is the 2nd template. |
drh | 9d9cf22 | 2007-02-13 15:01:11 +0000 | [diff] [blame] | 429 | ** |
| 430 | ** open a write cursor to <table> |
| 431 | ** open read cursor on <table2> |
| 432 | ** transfer all records in <table2> over to <table> |
| 433 | ** close cursors |
| 434 | ** foreach index on <table> |
| 435 | ** open a write cursor on the <table> index |
| 436 | ** open a read cursor on the corresponding <table2> index |
| 437 | ** transfer all records from the read to the write cursors |
| 438 | ** close cursors |
| 439 | ** end foreach |
| 440 | ** |
drh | e00ee6e | 2008-06-20 15:24:01 +0000 | [diff] [blame] | 441 | ** The 3rd template is for when the second template does not apply |
drh | 9d9cf22 | 2007-02-13 15:01:11 +0000 | [diff] [blame] | 442 | ** and the SELECT clause does not read from <table> at any time. |
| 443 | ** The generated code follows this template: |
drh | 142e30d | 2002-08-28 03:00:58 +0000 | [diff] [blame] | 444 | ** |
drh | e00ee6e | 2008-06-20 15:24:01 +0000 | [diff] [blame] | 445 | ** X <- A |
drh | 142e30d | 2002-08-28 03:00:58 +0000 | [diff] [blame] | 446 | ** goto B |
| 447 | ** A: setup for the SELECT |
drh | 9d9cf22 | 2007-02-13 15:01:11 +0000 | [diff] [blame] | 448 | ** loop over the rows in the SELECT |
drh | e00ee6e | 2008-06-20 15:24:01 +0000 | [diff] [blame] | 449 | ** load values into registers R..R+n |
| 450 | ** yield X |
drh | 142e30d | 2002-08-28 03:00:58 +0000 | [diff] [blame] | 451 | ** end loop |
| 452 | ** cleanup after the SELECT |
drh | 81cf13e | 2014-02-07 18:27:53 +0000 | [diff] [blame] | 453 | ** end-coroutine X |
drh | e00ee6e | 2008-06-20 15:24:01 +0000 | [diff] [blame] | 454 | ** B: open write cursor to <table> and its indices |
drh | 81cf13e | 2014-02-07 18:27:53 +0000 | [diff] [blame] | 455 | ** C: yield X, at EOF goto D |
drh | e00ee6e | 2008-06-20 15:24:01 +0000 | [diff] [blame] | 456 | ** insert the select result into <table> from R..R+n |
| 457 | ** goto C |
drh | 142e30d | 2002-08-28 03:00:58 +0000 | [diff] [blame] | 458 | ** D: cleanup |
| 459 | ** |
drh | e00ee6e | 2008-06-20 15:24:01 +0000 | [diff] [blame] | 460 | ** The 4th template is used if the insert statement takes its |
drh | 142e30d | 2002-08-28 03:00:58 +0000 | [diff] [blame] | 461 | ** values from a SELECT but the data is being inserted into a table |
| 462 | ** that is also read as part of the SELECT. In the third form, |
peter.d.reid | 60ec914 | 2014-09-06 16:39:46 +0000 | [diff] [blame] | 463 | ** we have to use an intermediate table to store the results of |
drh | 142e30d | 2002-08-28 03:00:58 +0000 | [diff] [blame] | 464 | ** the select. The template is like this: |
| 465 | ** |
drh | e00ee6e | 2008-06-20 15:24:01 +0000 | [diff] [blame] | 466 | ** X <- A |
drh | 142e30d | 2002-08-28 03:00:58 +0000 | [diff] [blame] | 467 | ** goto B |
| 468 | ** A: setup for the SELECT |
| 469 | ** loop over the tables in the SELECT |
drh | e00ee6e | 2008-06-20 15:24:01 +0000 | [diff] [blame] | 470 | ** load value into register R..R+n |
| 471 | ** yield X |
drh | 142e30d | 2002-08-28 03:00:58 +0000 | [diff] [blame] | 472 | ** end loop |
| 473 | ** cleanup after the SELECT |
drh | 81cf13e | 2014-02-07 18:27:53 +0000 | [diff] [blame] | 474 | ** end co-routine R |
drh | e00ee6e | 2008-06-20 15:24:01 +0000 | [diff] [blame] | 475 | ** B: open temp table |
drh | 81cf13e | 2014-02-07 18:27:53 +0000 | [diff] [blame] | 476 | ** L: yield X, at EOF goto M |
drh | e00ee6e | 2008-06-20 15:24:01 +0000 | [diff] [blame] | 477 | ** insert row from R..R+n into temp table |
| 478 | ** goto L |
| 479 | ** M: open write cursor to <table> and its indices |
| 480 | ** rewind temp table |
| 481 | ** C: loop over rows of intermediate table |
drh | 142e30d | 2002-08-28 03:00:58 +0000 | [diff] [blame] | 482 | ** transfer values form intermediate table into <table> |
drh | e00ee6e | 2008-06-20 15:24:01 +0000 | [diff] [blame] | 483 | ** end loop |
| 484 | ** D: cleanup |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 485 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 486 | void sqlite3Insert( |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 487 | Parse *pParse, /* Parser context */ |
drh | 113088e | 2003-03-20 01:16:58 +0000 | [diff] [blame] | 488 | SrcList *pTabList, /* Name of table into which we are inserting */ |
drh | 5974a30 | 2000-06-07 14:42:26 +0000 | [diff] [blame] | 489 | Select *pSelect, /* A SELECT statement to use as the data source */ |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 490 | IdList *pColumn, /* Column names corresponding to IDLIST. */ |
drh | 2c2e844 | 2018-04-07 15:04:05 +0000 | [diff] [blame] | 491 | int onError, /* How to handle constraint errors */ |
drh | 46d2e5c | 2018-04-12 13:15:43 +0000 | [diff] [blame] | 492 | Upsert *pUpsert /* ON CONFLICT clauses for upsert, or NULL */ |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 493 | ){ |
drh | 6a288a3 | 2008-01-07 19:20:24 +0000 | [diff] [blame] | 494 | sqlite3 *db; /* The main database structure */ |
| 495 | Table *pTab; /* The table to insert into. aka TABLE */ |
drh | 60ffc80 | 2016-11-21 21:33:46 +0000 | [diff] [blame] | 496 | int i, j; /* Loop counters */ |
drh | 5974a30 | 2000-06-07 14:42:26 +0000 | [diff] [blame] | 497 | Vdbe *v; /* Generate code into this virtual machine */ |
| 498 | Index *pIdx; /* For looping over indices of the table */ |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 499 | int nColumn; /* Number of columns in the data */ |
drh | 6a288a3 | 2008-01-07 19:20:24 +0000 | [diff] [blame] | 500 | int nHidden = 0; /* Number of hidden columns if TABLE is virtual */ |
drh | 26198bb | 2013-10-31 11:15:09 +0000 | [diff] [blame] | 501 | int iDataCur = 0; /* VDBE cursor that is the main data repository */ |
| 502 | int iIdxCur = 0; /* First index cursor */ |
drh | d82b502 | 2013-10-26 00:58:34 +0000 | [diff] [blame] | 503 | int ipkColumn = -1; /* Column that is the INTEGER PRIMARY KEY */ |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 504 | int endOfLoop; /* Label for the end of the insertion loop */ |
danielk1977 | cfe9a69 | 2004-06-16 12:00:29 +0000 | [diff] [blame] | 505 | int srcTab = 0; /* Data comes from this temporary cursor if >=0 */ |
drh | e00ee6e | 2008-06-20 15:24:01 +0000 | [diff] [blame] | 506 | int addrInsTop = 0; /* Jump to label "D" */ |
| 507 | int addrCont = 0; /* Top of insert loop. Label "C" in templates 3 and 4 */ |
drh | 2eb9537 | 2008-06-06 15:04:36 +0000 | [diff] [blame] | 508 | SelectDest dest; /* Destination for SELECT on rhs of INSERT */ |
drh | 6a288a3 | 2008-01-07 19:20:24 +0000 | [diff] [blame] | 509 | int iDb; /* Index of database holding TABLE */ |
drh | 05a86c5 | 2014-02-16 01:55:49 +0000 | [diff] [blame] | 510 | u8 useTempTable = 0; /* Store SELECT results in intermediate table */ |
| 511 | u8 appendFlag = 0; /* True if the insert is likely to be an append */ |
| 512 | u8 withoutRowid; /* 0 for normal table. 1 for WITHOUT ROWID table */ |
drh | a21f78b | 2015-04-19 18:32:43 +0000 | [diff] [blame] | 513 | u8 bIdListInOrder; /* True if IDLIST is in table order */ |
drh | 75593d9 | 2014-01-10 20:46:55 +0000 | [diff] [blame] | 514 | ExprList *pList = 0; /* List of VALUES() to be inserted */ |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 515 | |
drh | 6a288a3 | 2008-01-07 19:20:24 +0000 | [diff] [blame] | 516 | /* Register allocations */ |
drh | 1bd10f8 | 2008-12-10 21:19:56 +0000 | [diff] [blame] | 517 | int regFromSelect = 0;/* Base register for data coming from SELECT */ |
drh | 6a288a3 | 2008-01-07 19:20:24 +0000 | [diff] [blame] | 518 | int regAutoinc = 0; /* Register holding the AUTOINCREMENT counter */ |
| 519 | int regRowCount = 0; /* Memory cell used for the row counter */ |
| 520 | int regIns; /* Block of regs holding rowid+data being inserted */ |
| 521 | int regRowid; /* registers holding insert rowid */ |
| 522 | int regData; /* register holding first column to insert */ |
drh | aa9b896 | 2008-01-08 02:57:55 +0000 | [diff] [blame] | 523 | int *aRegIdx = 0; /* One register allocated to each index */ |
drh | 6a288a3 | 2008-01-07 19:20:24 +0000 | [diff] [blame] | 524 | |
drh | 798da52 | 2004-11-04 04:42:28 +0000 | [diff] [blame] | 525 | #ifndef SQLITE_OMIT_TRIGGER |
| 526 | int isView; /* True if attempting to insert into a view */ |
danielk1977 | 2f886d1 | 2009-02-28 10:47:41 +0000 | [diff] [blame] | 527 | Trigger *pTrigger; /* List of triggers on pTab, if required */ |
| 528 | int tmask; /* Mask of trigger times */ |
drh | 798da52 | 2004-11-04 04:42:28 +0000 | [diff] [blame] | 529 | #endif |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 530 | |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 531 | db = pParse->db; |
| 532 | if( pParse->nErr || db->mallocFailed ){ |
drh | 6f7adc8 | 2006-01-11 21:41:20 +0000 | [diff] [blame] | 533 | goto insert_cleanup; |
| 534 | } |
drh | 4c88348 | 2017-06-03 20:09:37 +0000 | [diff] [blame] | 535 | dest.iSDParm = 0; /* Suppress a harmless compiler warning */ |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 536 | |
drh | 75593d9 | 2014-01-10 20:46:55 +0000 | [diff] [blame] | 537 | /* If the Select object is really just a simple VALUES() list with a |
drh | a21f78b | 2015-04-19 18:32:43 +0000 | [diff] [blame] | 538 | ** single row (the common case) then keep that one row of values |
| 539 | ** and discard the other (unused) parts of the pSelect object |
drh | 75593d9 | 2014-01-10 20:46:55 +0000 | [diff] [blame] | 540 | */ |
| 541 | if( pSelect && (pSelect->selFlags & SF_Values)!=0 && pSelect->pPrior==0 ){ |
| 542 | pList = pSelect->pEList; |
| 543 | pSelect->pEList = 0; |
| 544 | sqlite3SelectDelete(db, pSelect); |
| 545 | pSelect = 0; |
| 546 | } |
| 547 | |
drh | 1ccde15 | 2000-06-17 13:12:39 +0000 | [diff] [blame] | 548 | /* Locate the table into which we will be inserting new information. |
| 549 | */ |
drh | 113088e | 2003-03-20 01:16:58 +0000 | [diff] [blame] | 550 | assert( pTabList->nSrc==1 ); |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 551 | pTab = sqlite3SrcListLookup(pParse, pTabList); |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 552 | if( pTab==0 ){ |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 553 | goto insert_cleanup; |
| 554 | } |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 555 | iDb = sqlite3SchemaToIndex(db, pTab->pSchema); |
| 556 | assert( iDb<db->nDb ); |
drh | a0daa75 | 2016-09-16 11:53:10 +0000 | [diff] [blame] | 557 | if( sqlite3AuthCheck(pParse, SQLITE_INSERT, pTab->zName, 0, |
| 558 | db->aDb[iDb].zDbSName) ){ |
drh | 1962bda | 2003-01-12 19:33:52 +0000 | [diff] [blame] | 559 | goto insert_cleanup; |
| 560 | } |
drh | ec95c44 | 2013-10-23 01:57:32 +0000 | [diff] [blame] | 561 | withoutRowid = !HasRowid(pTab); |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 562 | |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 563 | /* Figure out if we have any triggers and if the table being |
| 564 | ** inserted into is a view |
| 565 | */ |
| 566 | #ifndef SQLITE_OMIT_TRIGGER |
danielk1977 | 2f886d1 | 2009-02-28 10:47:41 +0000 | [diff] [blame] | 567 | pTrigger = sqlite3TriggersExist(pParse, pTab, TK_INSERT, 0, &tmask); |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 568 | isView = pTab->pSelect!=0; |
| 569 | #else |
danielk1977 | 2f886d1 | 2009-02-28 10:47:41 +0000 | [diff] [blame] | 570 | # define pTrigger 0 |
| 571 | # define tmask 0 |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 572 | # define isView 0 |
| 573 | #endif |
| 574 | #ifdef SQLITE_OMIT_VIEW |
| 575 | # undef isView |
| 576 | # define isView 0 |
| 577 | #endif |
danielk1977 | 2f886d1 | 2009-02-28 10:47:41 +0000 | [diff] [blame] | 578 | assert( (pTrigger && tmask) || (pTrigger==0 && tmask==0) ); |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 579 | |
drh | f573c99 | 2002-07-31 00:32:50 +0000 | [diff] [blame] | 580 | /* If pTab is really a view, make sure it has been initialized. |
drh | d82b502 | 2013-10-26 00:58:34 +0000 | [diff] [blame] | 581 | ** ViewGetColumnNames() is a no-op if pTab is not a view. |
drh | f573c99 | 2002-07-31 00:32:50 +0000 | [diff] [blame] | 582 | */ |
danielk1977 | b3d24bf | 2006-06-19 03:05:10 +0000 | [diff] [blame] | 583 | if( sqlite3ViewGetColumnNames(pParse, pTab) ){ |
drh | 5cf590c | 2003-04-24 01:45:04 +0000 | [diff] [blame] | 584 | goto insert_cleanup; |
drh | f573c99 | 2002-07-31 00:32:50 +0000 | [diff] [blame] | 585 | } |
| 586 | |
drh | d82b502 | 2013-10-26 00:58:34 +0000 | [diff] [blame] | 587 | /* Cannot insert into a read-only table. |
danielk1977 | 595a523 | 2009-07-24 17:58:53 +0000 | [diff] [blame] | 588 | */ |
| 589 | if( sqlite3IsReadOnly(pParse, pTab, tmask) ){ |
| 590 | goto insert_cleanup; |
| 591 | } |
| 592 | |
drh | 1ccde15 | 2000-06-17 13:12:39 +0000 | [diff] [blame] | 593 | /* Allocate a VDBE |
| 594 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 595 | v = sqlite3GetVdbe(pParse); |
drh | 5974a30 | 2000-06-07 14:42:26 +0000 | [diff] [blame] | 596 | if( v==0 ) goto insert_cleanup; |
drh | 4794f73 | 2004-11-05 17:17:50 +0000 | [diff] [blame] | 597 | if( pParse->nested==0 ) sqlite3VdbeCountChanges(v); |
danielk1977 | 2f886d1 | 2009-02-28 10:47:41 +0000 | [diff] [blame] | 598 | sqlite3BeginWriteOperation(pParse, pSelect || pTrigger, iDb); |
drh | 1ccde15 | 2000-06-17 13:12:39 +0000 | [diff] [blame] | 599 | |
drh | 9d9cf22 | 2007-02-13 15:01:11 +0000 | [diff] [blame] | 600 | #ifndef SQLITE_OMIT_XFER_OPT |
| 601 | /* If the statement is of the form |
| 602 | ** |
| 603 | ** INSERT INTO <table1> SELECT * FROM <table2>; |
| 604 | ** |
| 605 | ** Then special optimizations can be applied that make the transfer |
| 606 | ** very fast and which reduce fragmentation of indices. |
drh | e00ee6e | 2008-06-20 15:24:01 +0000 | [diff] [blame] | 607 | ** |
| 608 | ** This is the 2nd template. |
drh | 9d9cf22 | 2007-02-13 15:01:11 +0000 | [diff] [blame] | 609 | */ |
| 610 | if( pColumn==0 && xferOptimization(pParse, pTab, pSelect, onError, iDb) ){ |
danielk1977 | 2f886d1 | 2009-02-28 10:47:41 +0000 | [diff] [blame] | 611 | assert( !pTrigger ); |
drh | 9d9cf22 | 2007-02-13 15:01:11 +0000 | [diff] [blame] | 612 | assert( pList==0 ); |
drh | 0b9f50d | 2009-06-23 20:28:53 +0000 | [diff] [blame] | 613 | goto insert_end; |
drh | 9d9cf22 | 2007-02-13 15:01:11 +0000 | [diff] [blame] | 614 | } |
| 615 | #endif /* SQLITE_OMIT_XFER_OPT */ |
| 616 | |
drh | 2958a4e | 2004-11-12 03:56:15 +0000 | [diff] [blame] | 617 | /* If this is an AUTOINCREMENT table, look up the sequence number in the |
drh | 6a288a3 | 2008-01-07 19:20:24 +0000 | [diff] [blame] | 618 | ** sqlite_sequence table and store it in memory cell regAutoinc. |
drh | 2958a4e | 2004-11-12 03:56:15 +0000 | [diff] [blame] | 619 | */ |
drh | 6a288a3 | 2008-01-07 19:20:24 +0000 | [diff] [blame] | 620 | regAutoinc = autoIncBegin(pParse, iDb, pTab); |
drh | 2958a4e | 2004-11-12 03:56:15 +0000 | [diff] [blame] | 621 | |
drh | 05a86c5 | 2014-02-16 01:55:49 +0000 | [diff] [blame] | 622 | /* Allocate registers for holding the rowid of the new row, |
peter.d.reid | 60ec914 | 2014-09-06 16:39:46 +0000 | [diff] [blame] | 623 | ** the content of the new row, and the assembled row record. |
drh | 1ccde15 | 2000-06-17 13:12:39 +0000 | [diff] [blame] | 624 | */ |
drh | 05a86c5 | 2014-02-16 01:55:49 +0000 | [diff] [blame] | 625 | regRowid = regIns = pParse->nMem+1; |
| 626 | pParse->nMem += pTab->nCol + 1; |
danielk1977 | 034ca14 | 2007-06-26 10:38:54 +0000 | [diff] [blame] | 627 | if( IsVirtual(pTab) ){ |
drh | 05a86c5 | 2014-02-16 01:55:49 +0000 | [diff] [blame] | 628 | regRowid++; |
| 629 | pParse->nMem++; |
danielk1977 | 034ca14 | 2007-06-26 10:38:54 +0000 | [diff] [blame] | 630 | } |
drh | 05a86c5 | 2014-02-16 01:55:49 +0000 | [diff] [blame] | 631 | regData = regRowid+1; |
drh | 1ccde15 | 2000-06-17 13:12:39 +0000 | [diff] [blame] | 632 | |
| 633 | /* If the INSERT statement included an IDLIST term, then make sure |
| 634 | ** all elements of the IDLIST really are columns of the table and |
| 635 | ** remember the column indices. |
drh | c839258 | 2001-12-31 02:48:51 +0000 | [diff] [blame] | 636 | ** |
| 637 | ** If the table has an INTEGER PRIMARY KEY column and that column |
drh | d82b502 | 2013-10-26 00:58:34 +0000 | [diff] [blame] | 638 | ** is named in the IDLIST, then record in the ipkColumn variable |
| 639 | ** the index into IDLIST of the primary key column. ipkColumn is |
drh | c839258 | 2001-12-31 02:48:51 +0000 | [diff] [blame] | 640 | ** the index of the primary key as it appears in IDLIST, not as |
drh | d82b502 | 2013-10-26 00:58:34 +0000 | [diff] [blame] | 641 | ** is appears in the original table. (The index of the INTEGER |
| 642 | ** PRIMARY KEY in the original table is pTab->iPKey.) |
drh | 1ccde15 | 2000-06-17 13:12:39 +0000 | [diff] [blame] | 643 | */ |
drh | a21f78b | 2015-04-19 18:32:43 +0000 | [diff] [blame] | 644 | bIdListInOrder = (pTab->tabFlags & TF_OOOHidden)==0; |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 645 | if( pColumn ){ |
| 646 | for(i=0; i<pColumn->nId; i++){ |
| 647 | pColumn->a[i].idx = -1; |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 648 | } |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 649 | for(i=0; i<pColumn->nId; i++){ |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 650 | for(j=0; j<pTab->nCol; j++){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 651 | if( sqlite3StrICmp(pColumn->a[i].zName, pTab->aCol[j].zName)==0 ){ |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 652 | pColumn->a[i].idx = j; |
drh | 05a86c5 | 2014-02-16 01:55:49 +0000 | [diff] [blame] | 653 | if( i!=j ) bIdListInOrder = 0; |
drh | 4a32431 | 2001-12-21 14:30:42 +0000 | [diff] [blame] | 654 | if( j==pTab->iPKey ){ |
drh | d82b502 | 2013-10-26 00:58:34 +0000 | [diff] [blame] | 655 | ipkColumn = i; assert( !withoutRowid ); |
drh | 4a32431 | 2001-12-21 14:30:42 +0000 | [diff] [blame] | 656 | } |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 657 | break; |
| 658 | } |
| 659 | } |
| 660 | if( j>=pTab->nCol ){ |
drh | ec95c44 | 2013-10-23 01:57:32 +0000 | [diff] [blame] | 661 | if( sqlite3IsRowid(pColumn->a[i].zName) && !withoutRowid ){ |
drh | d82b502 | 2013-10-26 00:58:34 +0000 | [diff] [blame] | 662 | ipkColumn = i; |
drh | e48ae71 | 2014-05-23 11:48:57 +0000 | [diff] [blame] | 663 | bIdListInOrder = 0; |
drh | a0217ba | 2003-06-01 01:10:33 +0000 | [diff] [blame] | 664 | }else{ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 665 | sqlite3ErrorMsg(pParse, "table %S has no column named %s", |
drh | a0217ba | 2003-06-01 01:10:33 +0000 | [diff] [blame] | 666 | pTabList, 0, pColumn->a[i].zName); |
dan | 1db9510 | 2010-06-28 10:15:19 +0000 | [diff] [blame] | 667 | pParse->checkSchema = 1; |
drh | a0217ba | 2003-06-01 01:10:33 +0000 | [diff] [blame] | 668 | goto insert_cleanup; |
| 669 | } |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 670 | } |
| 671 | } |
| 672 | } |
drh | 1ccde15 | 2000-06-17 13:12:39 +0000 | [diff] [blame] | 673 | |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 674 | /* Figure out how many columns of data are supplied. If the data |
| 675 | ** is coming from a SELECT statement, then generate a co-routine that |
| 676 | ** produces a single row of the SELECT on each invocation. The |
| 677 | ** co-routine is the common header to the 3rd and 4th templates. |
| 678 | */ |
drh | 5f08526 | 2012-09-15 13:29:23 +0000 | [diff] [blame] | 679 | if( pSelect ){ |
drh | a21f78b | 2015-04-19 18:32:43 +0000 | [diff] [blame] | 680 | /* Data is coming from a SELECT or from a multi-row VALUES clause. |
| 681 | ** Generate a co-routine to run the SELECT. */ |
drh | 05a86c5 | 2014-02-16 01:55:49 +0000 | [diff] [blame] | 682 | int regYield; /* Register holding co-routine entry-point */ |
| 683 | int addrTop; /* Top of the co-routine */ |
| 684 | int rc; /* Result code */ |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 685 | |
drh | 05a86c5 | 2014-02-16 01:55:49 +0000 | [diff] [blame] | 686 | regYield = ++pParse->nMem; |
| 687 | addrTop = sqlite3VdbeCurrentAddr(v) + 1; |
| 688 | sqlite3VdbeAddOp3(v, OP_InitCoroutine, regYield, 0, addrTop); |
| 689 | sqlite3SelectDestInit(&dest, SRT_Coroutine, regYield); |
| 690 | dest.iSdst = bIdListInOrder ? regData : 0; |
| 691 | dest.nSdst = pTab->nCol; |
| 692 | rc = sqlite3Select(pParse, pSelect, &dest); |
drh | 2b596da | 2012-07-23 21:43:19 +0000 | [diff] [blame] | 693 | regFromSelect = dest.iSdst; |
drh | 992590b | 2015-04-19 22:41:22 +0000 | [diff] [blame] | 694 | if( rc || db->mallocFailed || pParse->nErr ) goto insert_cleanup; |
drh | 2fade2f | 2016-02-09 02:12:20 +0000 | [diff] [blame] | 695 | sqlite3VdbeEndCoroutine(v, regYield); |
drh | 05a86c5 | 2014-02-16 01:55:49 +0000 | [diff] [blame] | 696 | sqlite3VdbeJumpHere(v, addrTop - 1); /* label B: */ |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 697 | assert( pSelect->pEList ); |
| 698 | nColumn = pSelect->pEList->nExpr; |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 699 | |
| 700 | /* Set useTempTable to TRUE if the result of the SELECT statement |
| 701 | ** should be written into a temporary table (template 4). Set to |
| 702 | ** FALSE if each output row of the SELECT can be written directly into |
| 703 | ** the destination table (template 3). |
| 704 | ** |
| 705 | ** A temp table must be used if the table being updated is also one |
| 706 | ** of the tables being read by the SELECT statement. Also use a |
| 707 | ** temp table in the case of row triggers. |
| 708 | */ |
drh | 05a86c5 | 2014-02-16 01:55:49 +0000 | [diff] [blame] | 709 | if( pTrigger || readsTable(pParse, iDb, pTab) ){ |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 710 | useTempTable = 1; |
| 711 | } |
| 712 | |
| 713 | if( useTempTable ){ |
| 714 | /* Invoke the coroutine to extract information from the SELECT |
| 715 | ** and add it to a transient table srcTab. The code generated |
| 716 | ** here is from the 4th template: |
| 717 | ** |
| 718 | ** B: open temp table |
drh | 81cf13e | 2014-02-07 18:27:53 +0000 | [diff] [blame] | 719 | ** L: yield X, goto M at EOF |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 720 | ** insert row from R..R+n into temp table |
| 721 | ** goto L |
| 722 | ** M: ... |
| 723 | */ |
| 724 | int regRec; /* Register to hold packed record */ |
| 725 | int regTempRowid; /* Register to hold temp table ROWID */ |
drh | 06280ee | 2014-02-20 19:32:38 +0000 | [diff] [blame] | 726 | int addrL; /* Label "L" */ |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 727 | |
| 728 | srcTab = pParse->nTab++; |
| 729 | regRec = sqlite3GetTempReg(pParse); |
| 730 | regTempRowid = sqlite3GetTempReg(pParse); |
| 731 | sqlite3VdbeAddOp2(v, OP_OpenEphemeral, srcTab, nColumn); |
drh | 06280ee | 2014-02-20 19:32:38 +0000 | [diff] [blame] | 732 | addrL = sqlite3VdbeAddOp1(v, OP_Yield, dest.iSDParm); VdbeCoverage(v); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 733 | sqlite3VdbeAddOp3(v, OP_MakeRecord, regFromSelect, nColumn, regRec); |
| 734 | sqlite3VdbeAddOp2(v, OP_NewRowid, srcTab, regTempRowid); |
| 735 | sqlite3VdbeAddOp3(v, OP_Insert, srcTab, regRec, regTempRowid); |
drh | 076e85f | 2015-09-03 13:46:12 +0000 | [diff] [blame] | 736 | sqlite3VdbeGoto(v, addrL); |
drh | 06280ee | 2014-02-20 19:32:38 +0000 | [diff] [blame] | 737 | sqlite3VdbeJumpHere(v, addrL); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 738 | sqlite3ReleaseTempReg(pParse, regRec); |
| 739 | sqlite3ReleaseTempReg(pParse, regTempRowid); |
| 740 | } |
| 741 | }else{ |
drh | a21f78b | 2015-04-19 18:32:43 +0000 | [diff] [blame] | 742 | /* This is the case if the data for the INSERT is coming from a |
| 743 | ** single-row VALUES clause |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 744 | */ |
| 745 | NameContext sNC; |
| 746 | memset(&sNC, 0, sizeof(sNC)); |
| 747 | sNC.pParse = pParse; |
| 748 | srcTab = -1; |
| 749 | assert( useTempTable==0 ); |
drh | fea870b | 2015-08-24 20:54:06 +0000 | [diff] [blame] | 750 | if( pList ){ |
| 751 | nColumn = pList->nExpr; |
| 752 | if( sqlite3ResolveExprListNames(&sNC, pList) ){ |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 753 | goto insert_cleanup; |
| 754 | } |
drh | fea870b | 2015-08-24 20:54:06 +0000 | [diff] [blame] | 755 | }else{ |
| 756 | nColumn = 0; |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 757 | } |
| 758 | } |
| 759 | |
drh | aacc543 | 2002-01-06 17:07:40 +0000 | [diff] [blame] | 760 | /* If there is no IDLIST term but the table has an integer primary |
drh | d82b502 | 2013-10-26 00:58:34 +0000 | [diff] [blame] | 761 | ** key, the set the ipkColumn variable to the integer primary key |
| 762 | ** column index in the original table definition. |
drh | 4a32431 | 2001-12-21 14:30:42 +0000 | [diff] [blame] | 763 | */ |
drh | 147d0cc | 2006-08-25 23:42:53 +0000 | [diff] [blame] | 764 | if( pColumn==0 && nColumn>0 ){ |
drh | d82b502 | 2013-10-26 00:58:34 +0000 | [diff] [blame] | 765 | ipkColumn = pTab->iPKey; |
drh | 4a32431 | 2001-12-21 14:30:42 +0000 | [diff] [blame] | 766 | } |
drh | 05a86c5 | 2014-02-16 01:55:49 +0000 | [diff] [blame] | 767 | |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 768 | /* Make sure the number of columns in the source data matches the number |
| 769 | ** of columns to be inserted into the table. |
| 770 | */ |
drh | f0c9145 | 2015-11-18 18:43:15 +0000 | [diff] [blame] | 771 | for(i=0; i<pTab->nCol; i++){ |
| 772 | nHidden += (IsHiddenColumn(&pTab->aCol[i]) ? 1 : 0); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 773 | } |
| 774 | if( pColumn==0 && nColumn && nColumn!=(pTab->nCol-nHidden) ){ |
| 775 | sqlite3ErrorMsg(pParse, |
| 776 | "table %S has %d columns but %d values were supplied", |
| 777 | pTabList, 0, pTab->nCol-nHidden, nColumn); |
| 778 | goto insert_cleanup; |
| 779 | } |
| 780 | if( pColumn!=0 && nColumn!=pColumn->nId ){ |
| 781 | sqlite3ErrorMsg(pParse, "%d values for %d columns", nColumn, pColumn->nId); |
| 782 | goto insert_cleanup; |
| 783 | } |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 784 | |
drh | feeb139 | 2002-04-09 03:28:01 +0000 | [diff] [blame] | 785 | /* Initialize the count of rows to be inserted |
| 786 | */ |
drh | 142e30d | 2002-08-28 03:00:58 +0000 | [diff] [blame] | 787 | if( db->flags & SQLITE_CountRows ){ |
drh | 6a288a3 | 2008-01-07 19:20:24 +0000 | [diff] [blame] | 788 | regRowCount = ++pParse->nMem; |
| 789 | sqlite3VdbeAddOp2(v, OP_Integer, 0, regRowCount); |
drh | feeb139 | 2002-04-09 03:28:01 +0000 | [diff] [blame] | 790 | } |
| 791 | |
danielk1977 | e448dc4 | 2008-01-02 11:50:51 +0000 | [diff] [blame] | 792 | /* If this is not a view, open the table and and all indices */ |
| 793 | if( !isView ){ |
drh | aa9b896 | 2008-01-08 02:57:55 +0000 | [diff] [blame] | 794 | int nIdx; |
dan | fd261ec | 2015-10-22 20:54:33 +0000 | [diff] [blame] | 795 | nIdx = sqlite3OpenTableAndIndices(pParse, pTab, OP_OpenWrite, 0, -1, 0, |
drh | 26198bb | 2013-10-31 11:15:09 +0000 | [diff] [blame] | 796 | &iDataCur, &iIdxCur); |
drh | 575fad6 | 2016-02-05 13:38:36 +0000 | [diff] [blame] | 797 | aRegIdx = sqlite3DbMallocRawNN(db, sizeof(int)*(nIdx+1)); |
drh | aa9b896 | 2008-01-08 02:57:55 +0000 | [diff] [blame] | 798 | if( aRegIdx==0 ){ |
| 799 | goto insert_cleanup; |
| 800 | } |
drh | 2c4dfc3 | 2016-11-10 14:24:04 +0000 | [diff] [blame] | 801 | for(i=0, pIdx=pTab->pIndex; i<nIdx; pIdx=pIdx->pNext, i++){ |
| 802 | assert( pIdx ); |
drh | aa9b896 | 2008-01-08 02:57:55 +0000 | [diff] [blame] | 803 | aRegIdx[i] = ++pParse->nMem; |
drh | 2c4dfc3 | 2016-11-10 14:24:04 +0000 | [diff] [blame] | 804 | pParse->nMem += pIdx->nColumn; |
drh | aa9b896 | 2008-01-08 02:57:55 +0000 | [diff] [blame] | 805 | } |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 806 | } |
| 807 | |
drh | e00ee6e | 2008-06-20 15:24:01 +0000 | [diff] [blame] | 808 | /* This is the top of the main insertion loop */ |
drh | 142e30d | 2002-08-28 03:00:58 +0000 | [diff] [blame] | 809 | if( useTempTable ){ |
drh | e00ee6e | 2008-06-20 15:24:01 +0000 | [diff] [blame] | 810 | /* This block codes the top of loop only. The complete loop is the |
| 811 | ** following pseudocode (template 4): |
| 812 | ** |
drh | 81cf13e | 2014-02-07 18:27:53 +0000 | [diff] [blame] | 813 | ** rewind temp table, if empty goto D |
drh | e00ee6e | 2008-06-20 15:24:01 +0000 | [diff] [blame] | 814 | ** C: loop over rows of intermediate table |
| 815 | ** transfer values form intermediate table into <table> |
| 816 | ** end loop |
| 817 | ** D: ... |
| 818 | */ |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 819 | addrInsTop = sqlite3VdbeAddOp1(v, OP_Rewind, srcTab); VdbeCoverage(v); |
drh | e00ee6e | 2008-06-20 15:24:01 +0000 | [diff] [blame] | 820 | addrCont = sqlite3VdbeCurrentAddr(v); |
drh | 142e30d | 2002-08-28 03:00:58 +0000 | [diff] [blame] | 821 | }else if( pSelect ){ |
drh | e00ee6e | 2008-06-20 15:24:01 +0000 | [diff] [blame] | 822 | /* This block codes the top of loop only. The complete loop is the |
| 823 | ** following pseudocode (template 3): |
| 824 | ** |
drh | 81cf13e | 2014-02-07 18:27:53 +0000 | [diff] [blame] | 825 | ** C: yield X, at EOF goto D |
drh | e00ee6e | 2008-06-20 15:24:01 +0000 | [diff] [blame] | 826 | ** insert the select result into <table> from R..R+n |
| 827 | ** goto C |
| 828 | ** D: ... |
| 829 | */ |
drh | 81cf13e | 2014-02-07 18:27:53 +0000 | [diff] [blame] | 830 | addrInsTop = addrCont = sqlite3VdbeAddOp1(v, OP_Yield, dest.iSDParm); |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 831 | VdbeCoverage(v); |
drh | 5974a30 | 2000-06-07 14:42:26 +0000 | [diff] [blame] | 832 | } |
drh | 1ccde15 | 2000-06-17 13:12:39 +0000 | [diff] [blame] | 833 | |
drh | 5cf590c | 2003-04-24 01:45:04 +0000 | [diff] [blame] | 834 | /* Run the BEFORE and INSTEAD OF triggers, if there are any |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 835 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 836 | endOfLoop = sqlite3VdbeMakeLabel(v); |
danielk1977 | 2f886d1 | 2009-02-28 10:47:41 +0000 | [diff] [blame] | 837 | if( tmask & TRIGGER_BEFORE ){ |
dan | 76d462e | 2009-08-30 11:42:51 +0000 | [diff] [blame] | 838 | int regCols = sqlite3GetTempRange(pParse, pTab->nCol+1); |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 839 | |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 840 | /* build the NEW.* reference row. Note that if there is an INTEGER |
| 841 | ** PRIMARY KEY into which a NULL is being inserted, that NULL will be |
| 842 | ** translated into a unique ID for the row. But on a BEFORE trigger, |
| 843 | ** we do not know what the unique ID will be (because the insert has |
| 844 | ** not happened yet) so we substitute a rowid of -1 |
| 845 | */ |
drh | d82b502 | 2013-10-26 00:58:34 +0000 | [diff] [blame] | 846 | if( ipkColumn<0 ){ |
dan | 76d462e | 2009-08-30 11:42:51 +0000 | [diff] [blame] | 847 | sqlite3VdbeAddOp2(v, OP_Integer, -1, regCols); |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 848 | }else{ |
drh | 728e0f9 | 2015-10-10 14:41:28 +0000 | [diff] [blame] | 849 | int addr1; |
drh | ec95c44 | 2013-10-23 01:57:32 +0000 | [diff] [blame] | 850 | assert( !withoutRowid ); |
drh | 7fe4590 | 2009-05-01 02:08:04 +0000 | [diff] [blame] | 851 | if( useTempTable ){ |
drh | d82b502 | 2013-10-26 00:58:34 +0000 | [diff] [blame] | 852 | sqlite3VdbeAddOp3(v, OP_Column, srcTab, ipkColumn, regCols); |
drh | 7fe4590 | 2009-05-01 02:08:04 +0000 | [diff] [blame] | 853 | }else{ |
| 854 | assert( pSelect==0 ); /* Otherwise useTempTable is true */ |
drh | d82b502 | 2013-10-26 00:58:34 +0000 | [diff] [blame] | 855 | sqlite3ExprCode(pParse, pList->a[ipkColumn].pExpr, regCols); |
drh | 7fe4590 | 2009-05-01 02:08:04 +0000 | [diff] [blame] | 856 | } |
drh | 728e0f9 | 2015-10-10 14:41:28 +0000 | [diff] [blame] | 857 | addr1 = sqlite3VdbeAddOp1(v, OP_NotNull, regCols); VdbeCoverage(v); |
dan | 76d462e | 2009-08-30 11:42:51 +0000 | [diff] [blame] | 858 | sqlite3VdbeAddOp2(v, OP_Integer, -1, regCols); |
drh | 728e0f9 | 2015-10-10 14:41:28 +0000 | [diff] [blame] | 859 | sqlite3VdbeJumpHere(v, addr1); |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 860 | sqlite3VdbeAddOp1(v, OP_MustBeInt, regCols); VdbeCoverage(v); |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 861 | } |
| 862 | |
danielk1977 | 034ca14 | 2007-06-26 10:38:54 +0000 | [diff] [blame] | 863 | /* Cannot have triggers on a virtual table. If it were possible, |
| 864 | ** this block would have to account for hidden column. |
| 865 | */ |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 866 | assert( !IsVirtual(pTab) ); |
danielk1977 | 034ca14 | 2007-06-26 10:38:54 +0000 | [diff] [blame] | 867 | |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 868 | /* Create the new column data |
| 869 | */ |
drh | b1daa3f | 2015-11-18 21:22:02 +0000 | [diff] [blame] | 870 | for(i=j=0; i<pTab->nCol; i++){ |
| 871 | if( pColumn ){ |
drh | 9adf9ac | 2002-05-15 11:44:13 +0000 | [diff] [blame] | 872 | for(j=0; j<pColumn->nId; j++){ |
| 873 | if( pColumn->a[j].idx==i ) break; |
| 874 | } |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 875 | } |
drh | b1daa3f | 2015-11-18 21:22:02 +0000 | [diff] [blame] | 876 | if( (!useTempTable && !pList) || (pColumn && j>=pColumn->nId) |
drh | 03d69a6 | 2015-11-19 13:53:57 +0000 | [diff] [blame] | 877 | || (pColumn==0 && IsOrdinaryHiddenColumn(&pTab->aCol[i])) ){ |
dan | 76d462e | 2009-08-30 11:42:51 +0000 | [diff] [blame] | 878 | sqlite3ExprCode(pParse, pTab->aCol[i].pDflt, regCols+i+1); |
drh | 142e30d | 2002-08-28 03:00:58 +0000 | [diff] [blame] | 879 | }else if( useTempTable ){ |
dan | 76d462e | 2009-08-30 11:42:51 +0000 | [diff] [blame] | 880 | sqlite3VdbeAddOp3(v, OP_Column, srcTab, j, regCols+i+1); |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 881 | }else{ |
drh | d6fe961 | 2005-01-14 01:22:00 +0000 | [diff] [blame] | 882 | assert( pSelect==0 ); /* Otherwise useTempTable is true */ |
dan | 76d462e | 2009-08-30 11:42:51 +0000 | [diff] [blame] | 883 | sqlite3ExprCodeAndCache(pParse, pList->a[j].pExpr, regCols+i+1); |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 884 | } |
drh | 03d69a6 | 2015-11-19 13:53:57 +0000 | [diff] [blame] | 885 | if( pColumn==0 && !IsOrdinaryHiddenColumn(&pTab->aCol[i]) ) j++; |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 886 | } |
danielk1977 | a37cdde | 2004-05-16 11:15:36 +0000 | [diff] [blame] | 887 | |
| 888 | /* If this is an INSERT on a view with an INSTEAD OF INSERT trigger, |
| 889 | ** do not attempt any conversions before assembling the record. |
| 890 | ** If this is a real table, attempt conversions as required by the |
| 891 | ** table column affinities. |
| 892 | */ |
| 893 | if( !isView ){ |
drh | 57bf4a8 | 2014-02-17 14:59:22 +0000 | [diff] [blame] | 894 | sqlite3TableAffinity(v, pTab, regCols+1); |
danielk1977 | a37cdde | 2004-05-16 11:15:36 +0000 | [diff] [blame] | 895 | } |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 896 | |
drh | 5cf590c | 2003-04-24 01:45:04 +0000 | [diff] [blame] | 897 | /* Fire BEFORE or INSTEAD OF triggers */ |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 898 | sqlite3CodeRowTrigger(pParse, pTrigger, TK_INSERT, 0, TRIGGER_BEFORE, |
dan | 94d7f50 | 2009-09-24 09:05:49 +0000 | [diff] [blame] | 899 | pTab, regCols-pTab->nCol-1, onError, endOfLoop); |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 900 | |
dan | 76d462e | 2009-08-30 11:42:51 +0000 | [diff] [blame] | 901 | sqlite3ReleaseTempRange(pParse, regCols, pTab->nCol+1); |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 902 | } |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 903 | |
drh | d82b502 | 2013-10-26 00:58:34 +0000 | [diff] [blame] | 904 | /* Compute the content of the next row to insert into a range of |
| 905 | ** registers beginning at regIns. |
drh | 1ccde15 | 2000-06-17 13:12:39 +0000 | [diff] [blame] | 906 | */ |
drh | 5cf590c | 2003-04-24 01:45:04 +0000 | [diff] [blame] | 907 | if( !isView ){ |
drh | 4cbdda9 | 2006-06-14 19:00:20 +0000 | [diff] [blame] | 908 | if( IsVirtual(pTab) ){ |
danielk1977 | 287fb61 | 2008-01-04 19:10:28 +0000 | [diff] [blame] | 909 | /* The row that the VUpdate opcode will delete: none */ |
drh | 6a288a3 | 2008-01-07 19:20:24 +0000 | [diff] [blame] | 910 | sqlite3VdbeAddOp2(v, OP_Null, 0, regIns); |
drh | 4cbdda9 | 2006-06-14 19:00:20 +0000 | [diff] [blame] | 911 | } |
drh | d82b502 | 2013-10-26 00:58:34 +0000 | [diff] [blame] | 912 | if( ipkColumn>=0 ){ |
drh | 142e30d | 2002-08-28 03:00:58 +0000 | [diff] [blame] | 913 | if( useTempTable ){ |
drh | d82b502 | 2013-10-26 00:58:34 +0000 | [diff] [blame] | 914 | sqlite3VdbeAddOp3(v, OP_Column, srcTab, ipkColumn, regRowid); |
drh | 142e30d | 2002-08-28 03:00:58 +0000 | [diff] [blame] | 915 | }else if( pSelect ){ |
drh | 05a86c5 | 2014-02-16 01:55:49 +0000 | [diff] [blame] | 916 | sqlite3VdbeAddOp2(v, OP_Copy, regFromSelect+ipkColumn, regRowid); |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 917 | }else{ |
drh | e4d9081 | 2007-03-29 05:51:49 +0000 | [diff] [blame] | 918 | VdbeOp *pOp; |
drh | d82b502 | 2013-10-26 00:58:34 +0000 | [diff] [blame] | 919 | sqlite3ExprCode(pParse, pList->a[ipkColumn].pExpr, regRowid); |
drh | 20411ea | 2009-05-29 19:00:12 +0000 | [diff] [blame] | 920 | pOp = sqlite3VdbeGetOp(v, -1); |
drh | 9d9c41e | 2017-10-31 03:40:15 +0000 | [diff] [blame] | 921 | assert( pOp!=0 ); |
| 922 | if( pOp->opcode==OP_Null && !IsVirtual(pTab) ){ |
drh | e4d9081 | 2007-03-29 05:51:49 +0000 | [diff] [blame] | 923 | appendFlag = 1; |
| 924 | pOp->opcode = OP_NewRowid; |
drh | 26198bb | 2013-10-31 11:15:09 +0000 | [diff] [blame] | 925 | pOp->p1 = iDataCur; |
drh | 6a288a3 | 2008-01-07 19:20:24 +0000 | [diff] [blame] | 926 | pOp->p2 = regRowid; |
| 927 | pOp->p3 = regAutoinc; |
drh | e4d9081 | 2007-03-29 05:51:49 +0000 | [diff] [blame] | 928 | } |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 929 | } |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 930 | /* If the PRIMARY KEY expression is NULL, then use OP_NewRowid |
drh | 27a3278 | 2002-06-19 20:32:43 +0000 | [diff] [blame] | 931 | ** to generate a unique primary key value. |
| 932 | */ |
drh | e4d9081 | 2007-03-29 05:51:49 +0000 | [diff] [blame] | 933 | if( !appendFlag ){ |
drh | 728e0f9 | 2015-10-10 14:41:28 +0000 | [diff] [blame] | 934 | int addr1; |
danielk1977 | bb50e7a | 2008-07-04 10:56:07 +0000 | [diff] [blame] | 935 | if( !IsVirtual(pTab) ){ |
drh | 728e0f9 | 2015-10-10 14:41:28 +0000 | [diff] [blame] | 936 | addr1 = sqlite3VdbeAddOp1(v, OP_NotNull, regRowid); VdbeCoverage(v); |
drh | 26198bb | 2013-10-31 11:15:09 +0000 | [diff] [blame] | 937 | sqlite3VdbeAddOp3(v, OP_NewRowid, iDataCur, regRowid, regAutoinc); |
drh | 728e0f9 | 2015-10-10 14:41:28 +0000 | [diff] [blame] | 938 | sqlite3VdbeJumpHere(v, addr1); |
danielk1977 | bb50e7a | 2008-07-04 10:56:07 +0000 | [diff] [blame] | 939 | }else{ |
drh | 728e0f9 | 2015-10-10 14:41:28 +0000 | [diff] [blame] | 940 | addr1 = sqlite3VdbeCurrentAddr(v); |
| 941 | sqlite3VdbeAddOp2(v, OP_IsNull, regRowid, addr1+2); VdbeCoverage(v); |
danielk1977 | bb50e7a | 2008-07-04 10:56:07 +0000 | [diff] [blame] | 942 | } |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 943 | sqlite3VdbeAddOp1(v, OP_MustBeInt, regRowid); VdbeCoverage(v); |
drh | e4d9081 | 2007-03-29 05:51:49 +0000 | [diff] [blame] | 944 | } |
drh | ec95c44 | 2013-10-23 01:57:32 +0000 | [diff] [blame] | 945 | }else if( IsVirtual(pTab) || withoutRowid ){ |
drh | 6a288a3 | 2008-01-07 19:20:24 +0000 | [diff] [blame] | 946 | sqlite3VdbeAddOp2(v, OP_Null, 0, regRowid); |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 947 | }else{ |
drh | 26198bb | 2013-10-31 11:15:09 +0000 | [diff] [blame] | 948 | sqlite3VdbeAddOp3(v, OP_NewRowid, iDataCur, regRowid, regAutoinc); |
drh | e4d9081 | 2007-03-29 05:51:49 +0000 | [diff] [blame] | 949 | appendFlag = 1; |
drh | 4a32431 | 2001-12-21 14:30:42 +0000 | [diff] [blame] | 950 | } |
drh | 6a288a3 | 2008-01-07 19:20:24 +0000 | [diff] [blame] | 951 | autoIncStep(pParse, regAutoinc, regRowid); |
drh | 4a32431 | 2001-12-21 14:30:42 +0000 | [diff] [blame] | 952 | |
drh | d82b502 | 2013-10-26 00:58:34 +0000 | [diff] [blame] | 953 | /* Compute data for all columns of the new entry, beginning |
danielk1977 | f29ce55 | 2002-05-19 23:43:12 +0000 | [diff] [blame] | 954 | ** with the first column. |
| 955 | */ |
danielk1977 | 034ca14 | 2007-06-26 10:38:54 +0000 | [diff] [blame] | 956 | nHidden = 0; |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 957 | for(i=0; i<pTab->nCol; i++){ |
drh | 6a288a3 | 2008-01-07 19:20:24 +0000 | [diff] [blame] | 958 | int iRegStore = regRowid+1+i; |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 959 | if( i==pTab->iPKey ){ |
drh | 9adf9ac | 2002-05-15 11:44:13 +0000 | [diff] [blame] | 960 | /* The value of the INTEGER PRIMARY KEY column is always a NULL. |
drh | d82b502 | 2013-10-26 00:58:34 +0000 | [diff] [blame] | 961 | ** Whenever this column is read, the rowid will be substituted |
| 962 | ** in its place. Hence, fill this column with a NULL to avoid |
drh | 05a86c5 | 2014-02-16 01:55:49 +0000 | [diff] [blame] | 963 | ** taking up data space with information that will never be used. |
| 964 | ** As there may be shallow copies of this value, make it a soft-NULL */ |
| 965 | sqlite3VdbeAddOp1(v, OP_SoftNull, iRegStore); |
drh | 9adf9ac | 2002-05-15 11:44:13 +0000 | [diff] [blame] | 966 | continue; |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 967 | } |
| 968 | if( pColumn==0 ){ |
danielk1977 | 034ca14 | 2007-06-26 10:38:54 +0000 | [diff] [blame] | 969 | if( IsHiddenColumn(&pTab->aCol[i]) ){ |
danielk1977 | 034ca14 | 2007-06-26 10:38:54 +0000 | [diff] [blame] | 970 | j = -1; |
| 971 | nHidden++; |
| 972 | }else{ |
| 973 | j = i - nHidden; |
| 974 | } |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 975 | }else{ |
drh | 9adf9ac | 2002-05-15 11:44:13 +0000 | [diff] [blame] | 976 | for(j=0; j<pColumn->nId; j++){ |
| 977 | if( pColumn->a[j].idx==i ) break; |
| 978 | } |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 979 | } |
danielk1977 | 034ca14 | 2007-06-26 10:38:54 +0000 | [diff] [blame] | 980 | if( j<0 || nColumn==0 || (pColumn && j>=pColumn->nId) ){ |
drh | 05a86c5 | 2014-02-16 01:55:49 +0000 | [diff] [blame] | 981 | sqlite3ExprCodeFactorable(pParse, pTab->aCol[i].pDflt, iRegStore); |
drh | 142e30d | 2002-08-28 03:00:58 +0000 | [diff] [blame] | 982 | }else if( useTempTable ){ |
danielk1977 | 287fb61 | 2008-01-04 19:10:28 +0000 | [diff] [blame] | 983 | sqlite3VdbeAddOp3(v, OP_Column, srcTab, j, iRegStore); |
drh | 142e30d | 2002-08-28 03:00:58 +0000 | [diff] [blame] | 984 | }else if( pSelect ){ |
drh | 05a86c5 | 2014-02-16 01:55:49 +0000 | [diff] [blame] | 985 | if( regFromSelect!=regData ){ |
| 986 | sqlite3VdbeAddOp2(v, OP_SCopy, regFromSelect+j, iRegStore); |
| 987 | } |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 988 | }else{ |
danielk1977 | 287fb61 | 2008-01-04 19:10:28 +0000 | [diff] [blame] | 989 | sqlite3ExprCode(pParse, pList->a[j].pExpr, iRegStore); |
drh | 5974a30 | 2000-06-07 14:42:26 +0000 | [diff] [blame] | 990 | } |
drh | bed8690 | 2000-06-02 13:27:59 +0000 | [diff] [blame] | 991 | } |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 992 | |
| 993 | /* Generate code to check constraints and generate index keys and |
danielk1977 | f29ce55 | 2002-05-19 23:43:12 +0000 | [diff] [blame] | 994 | ** do the insertion. |
| 995 | */ |
drh | 4cbdda9 | 2006-06-14 19:00:20 +0000 | [diff] [blame] | 996 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
| 997 | if( IsVirtual(pTab) ){ |
danielk1977 | 595a523 | 2009-07-24 17:58:53 +0000 | [diff] [blame] | 998 | const char *pVTab = (const char *)sqlite3GetVTable(db, pTab); |
drh | 4f3dd15 | 2008-04-28 18:46:43 +0000 | [diff] [blame] | 999 | sqlite3VtabMakeWritable(pParse, pTab); |
danielk1977 | 595a523 | 2009-07-24 17:58:53 +0000 | [diff] [blame] | 1000 | sqlite3VdbeAddOp4(v, OP_VUpdate, 1, pTab->nCol+2, regIns, pVTab, P4_VTAB); |
dan | b061d05 | 2011-04-25 18:49:57 +0000 | [diff] [blame] | 1001 | sqlite3VdbeChangeP5(v, onError==OE_Default ? OE_Abort : onError); |
dan | e0af83a | 2009-09-08 19:15:01 +0000 | [diff] [blame] | 1002 | sqlite3MayAbort(pParse); |
drh | 4cbdda9 | 2006-06-14 19:00:20 +0000 | [diff] [blame] | 1003 | }else |
| 1004 | #endif |
| 1005 | { |
danielk1977 | de63035 | 2009-05-04 11:42:29 +0000 | [diff] [blame] | 1006 | int isReplace; /* Set to true if constraints may cause a replace */ |
dan | 3b908d4 | 2016-11-08 19:22:32 +0000 | [diff] [blame] | 1007 | int bUseSeek; /* True to use OPFLAG_SEEKRESULT */ |
drh | f8ffb27 | 2013-11-01 17:08:56 +0000 | [diff] [blame] | 1008 | sqlite3GenerateConstraintChecks(pParse, pTab, aRegIdx, iDataCur, iIdxCur, |
drh | bdb0022 | 2016-02-10 16:03:20 +0000 | [diff] [blame] | 1009 | regIns, 0, ipkColumn>=0, onError, endOfLoop, &isReplace, 0 |
drh | 04adf41 | 2008-01-08 18:57:50 +0000 | [diff] [blame] | 1010 | ); |
dan | 8ff2d95 | 2013-09-05 18:40:29 +0000 | [diff] [blame] | 1011 | sqlite3FkCheck(pParse, pTab, 0, regIns, 0, 0); |
dan | 3b908d4 | 2016-11-08 19:22:32 +0000 | [diff] [blame] | 1012 | |
| 1013 | /* Set the OPFLAG_USESEEKRESULT flag if either (a) there are no REPLACE |
| 1014 | ** constraints or (b) there are no triggers and this table is not a |
| 1015 | ** parent table in a foreign key constraint. It is safe to set the |
| 1016 | ** flag in the second case as if any REPLACE constraint is hit, an |
| 1017 | ** OP_Delete or OP_IdxDelete instruction will be executed on each |
| 1018 | ** cursor that is disturbed. And these instructions both clear the |
| 1019 | ** VdbeCursor.seekResult variable, disabling the OPFLAG_USESEEKRESULT |
| 1020 | ** functionality. */ |
| 1021 | bUseSeek = (isReplace==0 || (pTrigger==0 && |
| 1022 | ((db->flags & SQLITE_ForeignKeys)==0 || sqlite3FkReferences(pTab)==0) |
| 1023 | )); |
drh | 26198bb | 2013-10-31 11:15:09 +0000 | [diff] [blame] | 1024 | sqlite3CompleteInsertion(pParse, pTab, iDataCur, iIdxCur, |
dan | 3b908d4 | 2016-11-08 19:22:32 +0000 | [diff] [blame] | 1025 | regIns, aRegIdx, 0, appendFlag, bUseSeek |
| 1026 | ); |
drh | 4cbdda9 | 2006-06-14 19:00:20 +0000 | [diff] [blame] | 1027 | } |
drh | 5cf590c | 2003-04-24 01:45:04 +0000 | [diff] [blame] | 1028 | } |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 1029 | |
drh | 5cf590c | 2003-04-24 01:45:04 +0000 | [diff] [blame] | 1030 | /* Update the count of rows that are inserted |
| 1031 | */ |
| 1032 | if( (db->flags & SQLITE_CountRows)!=0 ){ |
drh | 6a288a3 | 2008-01-07 19:20:24 +0000 | [diff] [blame] | 1033 | sqlite3VdbeAddOp2(v, OP_AddImm, regRowCount, 1); |
drh | 5974a30 | 2000-06-07 14:42:26 +0000 | [diff] [blame] | 1034 | } |
drh | 1ccde15 | 2000-06-17 13:12:39 +0000 | [diff] [blame] | 1035 | |
danielk1977 | 2f886d1 | 2009-02-28 10:47:41 +0000 | [diff] [blame] | 1036 | if( pTrigger ){ |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 1037 | /* Code AFTER triggers */ |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 1038 | sqlite3CodeRowTrigger(pParse, pTrigger, TK_INSERT, 0, TRIGGER_AFTER, |
dan | 94d7f50 | 2009-09-24 09:05:49 +0000 | [diff] [blame] | 1039 | pTab, regData-2-pTab->nCol, onError, endOfLoop); |
drh | 1bee3d7 | 2001-10-15 00:44:35 +0000 | [diff] [blame] | 1040 | } |
| 1041 | |
drh | e00ee6e | 2008-06-20 15:24:01 +0000 | [diff] [blame] | 1042 | /* The bottom of the main insertion loop, if the data source |
| 1043 | ** is a SELECT statement. |
danielk1977 | f29ce55 | 2002-05-19 23:43:12 +0000 | [diff] [blame] | 1044 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1045 | sqlite3VdbeResolveLabel(v, endOfLoop); |
drh | 142e30d | 2002-08-28 03:00:58 +0000 | [diff] [blame] | 1046 | if( useTempTable ){ |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 1047 | sqlite3VdbeAddOp2(v, OP_Next, srcTab, addrCont); VdbeCoverage(v); |
drh | e00ee6e | 2008-06-20 15:24:01 +0000 | [diff] [blame] | 1048 | sqlite3VdbeJumpHere(v, addrInsTop); |
drh | 2eb9537 | 2008-06-06 15:04:36 +0000 | [diff] [blame] | 1049 | sqlite3VdbeAddOp1(v, OP_Close, srcTab); |
drh | 142e30d | 2002-08-28 03:00:58 +0000 | [diff] [blame] | 1050 | }else if( pSelect ){ |
drh | 076e85f | 2015-09-03 13:46:12 +0000 | [diff] [blame] | 1051 | sqlite3VdbeGoto(v, addrCont); |
drh | e00ee6e | 2008-06-20 15:24:01 +0000 | [diff] [blame] | 1052 | sqlite3VdbeJumpHere(v, addrInsTop); |
drh | 6b56344 | 2001-11-07 16:48:26 +0000 | [diff] [blame] | 1053 | } |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 1054 | |
drh | 0b9f50d | 2009-06-23 20:28:53 +0000 | [diff] [blame] | 1055 | insert_end: |
drh | f338814 | 2004-11-13 03:48:06 +0000 | [diff] [blame] | 1056 | /* Update the sqlite_sequence table by storing the content of the |
drh | 0b9f50d | 2009-06-23 20:28:53 +0000 | [diff] [blame] | 1057 | ** maximum rowid counter values recorded while inserting into |
| 1058 | ** autoincrement tables. |
drh | 2958a4e | 2004-11-12 03:56:15 +0000 | [diff] [blame] | 1059 | */ |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 1060 | if( pParse->nested==0 && pParse->pTriggerTab==0 ){ |
drh | 0b9f50d | 2009-06-23 20:28:53 +0000 | [diff] [blame] | 1061 | sqlite3AutoincrementEnd(pParse); |
| 1062 | } |
drh | 2958a4e | 2004-11-12 03:56:15 +0000 | [diff] [blame] | 1063 | |
drh | 1bee3d7 | 2001-10-15 00:44:35 +0000 | [diff] [blame] | 1064 | /* |
danielk1977 | e7de6f2 | 2004-11-05 06:02:06 +0000 | [diff] [blame] | 1065 | ** Return the number of rows inserted. If this routine is |
| 1066 | ** generating code because of a call to sqlite3NestedParse(), do not |
| 1067 | ** invoke the callback function. |
drh | 1bee3d7 | 2001-10-15 00:44:35 +0000 | [diff] [blame] | 1068 | */ |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 1069 | if( (db->flags&SQLITE_CountRows) && !pParse->nested && !pParse->pTriggerTab ){ |
drh | 6a288a3 | 2008-01-07 19:20:24 +0000 | [diff] [blame] | 1070 | sqlite3VdbeAddOp2(v, OP_ResultRow, regRowCount, 1); |
danielk1977 | 22322fd | 2004-05-25 23:35:17 +0000 | [diff] [blame] | 1071 | sqlite3VdbeSetNumCols(v, 1); |
danielk1977 | 10fb749 | 2008-10-31 10:53:22 +0000 | [diff] [blame] | 1072 | sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "rows inserted", SQLITE_STATIC); |
drh | 1bee3d7 | 2001-10-15 00:44:35 +0000 | [diff] [blame] | 1073 | } |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 1074 | |
| 1075 | insert_cleanup: |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 1076 | sqlite3SrcListDelete(db, pTabList); |
| 1077 | sqlite3ExprListDelete(db, pList); |
drh | 46d2e5c | 2018-04-12 13:15:43 +0000 | [diff] [blame] | 1078 | sqlite3UpsertDelete(db, pUpsert); |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 1079 | sqlite3SelectDelete(db, pSelect); |
| 1080 | sqlite3IdListDelete(db, pColumn); |
| 1081 | sqlite3DbFree(db, aRegIdx); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 1082 | } |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 1083 | |
dan | 75cbd98 | 2009-09-21 16:06:03 +0000 | [diff] [blame] | 1084 | /* Make sure "isView" and other macros defined above are undefined. Otherwise |
peter.d.reid | 60ec914 | 2014-09-06 16:39:46 +0000 | [diff] [blame] | 1085 | ** they may interfere with compilation of other functions in this file |
dan | 75cbd98 | 2009-09-21 16:06:03 +0000 | [diff] [blame] | 1086 | ** (or in another file, if this file becomes part of the amalgamation). */ |
| 1087 | #ifdef isView |
| 1088 | #undef isView |
| 1089 | #endif |
| 1090 | #ifdef pTrigger |
| 1091 | #undef pTrigger |
| 1092 | #endif |
| 1093 | #ifdef tmask |
| 1094 | #undef tmask |
| 1095 | #endif |
| 1096 | |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 1097 | /* |
drh | 98bfa16 | 2016-02-10 18:24:05 +0000 | [diff] [blame] | 1098 | ** Meanings of bits in of pWalker->eCode for checkConstraintUnchanged() |
| 1099 | */ |
| 1100 | #define CKCNSTRNT_COLUMN 0x01 /* CHECK constraint uses a changing column */ |
| 1101 | #define CKCNSTRNT_ROWID 0x02 /* CHECK constraint references the ROWID */ |
| 1102 | |
drh | 2a0b527 | 2016-02-10 16:52:24 +0000 | [diff] [blame] | 1103 | /* This is the Walker callback from checkConstraintUnchanged(). Set |
drh | 98bfa16 | 2016-02-10 18:24:05 +0000 | [diff] [blame] | 1104 | ** bit 0x01 of pWalker->eCode if |
drh | 2a0b527 | 2016-02-10 16:52:24 +0000 | [diff] [blame] | 1105 | ** pWalker->eCode to 0 if this expression node references any of the |
| 1106 | ** columns that are being modifed by an UPDATE statement. |
| 1107 | */ |
| 1108 | static int checkConstraintExprNode(Walker *pWalker, Expr *pExpr){ |
drh | 98bfa16 | 2016-02-10 18:24:05 +0000 | [diff] [blame] | 1109 | if( pExpr->op==TK_COLUMN ){ |
| 1110 | assert( pExpr->iColumn>=0 || pExpr->iColumn==-1 ); |
| 1111 | if( pExpr->iColumn>=0 ){ |
| 1112 | if( pWalker->u.aiCol[pExpr->iColumn]>=0 ){ |
| 1113 | pWalker->eCode |= CKCNSTRNT_COLUMN; |
| 1114 | } |
| 1115 | }else{ |
| 1116 | pWalker->eCode |= CKCNSTRNT_ROWID; |
| 1117 | } |
drh | 2a0b527 | 2016-02-10 16:52:24 +0000 | [diff] [blame] | 1118 | } |
| 1119 | return WRC_Continue; |
| 1120 | } |
| 1121 | |
| 1122 | /* |
| 1123 | ** pExpr is a CHECK constraint on a row that is being UPDATE-ed. The |
| 1124 | ** only columns that are modified by the UPDATE are those for which |
drh | 98bfa16 | 2016-02-10 18:24:05 +0000 | [diff] [blame] | 1125 | ** aiChng[i]>=0, and also the ROWID is modified if chngRowid is true. |
| 1126 | ** |
| 1127 | ** Return true if CHECK constraint pExpr does not use any of the |
| 1128 | ** changing columns (or the rowid if it is changing). In other words, |
| 1129 | ** return true if this CHECK constraint can be skipped when validating |
| 1130 | ** the new row in the UPDATE statement. |
drh | 2a0b527 | 2016-02-10 16:52:24 +0000 | [diff] [blame] | 1131 | */ |
drh | 98bfa16 | 2016-02-10 18:24:05 +0000 | [diff] [blame] | 1132 | static int checkConstraintUnchanged(Expr *pExpr, int *aiChng, int chngRowid){ |
drh | 2a0b527 | 2016-02-10 16:52:24 +0000 | [diff] [blame] | 1133 | Walker w; |
| 1134 | memset(&w, 0, sizeof(w)); |
drh | 98bfa16 | 2016-02-10 18:24:05 +0000 | [diff] [blame] | 1135 | w.eCode = 0; |
drh | 2a0b527 | 2016-02-10 16:52:24 +0000 | [diff] [blame] | 1136 | w.xExprCallback = checkConstraintExprNode; |
| 1137 | w.u.aiCol = aiChng; |
| 1138 | sqlite3WalkExpr(&w, pExpr); |
drh | 05723a9 | 2016-02-10 19:10:50 +0000 | [diff] [blame] | 1139 | if( !chngRowid ){ |
| 1140 | testcase( (w.eCode & CKCNSTRNT_ROWID)!=0 ); |
| 1141 | w.eCode &= ~CKCNSTRNT_ROWID; |
| 1142 | } |
| 1143 | testcase( w.eCode==0 ); |
| 1144 | testcase( w.eCode==CKCNSTRNT_COLUMN ); |
| 1145 | testcase( w.eCode==CKCNSTRNT_ROWID ); |
| 1146 | testcase( w.eCode==(CKCNSTRNT_ROWID|CKCNSTRNT_COLUMN) ); |
drh | 98bfa16 | 2016-02-10 18:24:05 +0000 | [diff] [blame] | 1147 | return !w.eCode; |
drh | 2a0b527 | 2016-02-10 16:52:24 +0000 | [diff] [blame] | 1148 | } |
| 1149 | |
drh | 11e8527 | 2013-10-26 15:40:48 +0000 | [diff] [blame] | 1150 | /* |
drh | 6934fc7 | 2013-10-31 15:37:49 +0000 | [diff] [blame] | 1151 | ** Generate code to do constraint checks prior to an INSERT or an UPDATE |
| 1152 | ** on table pTab. |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 1153 | ** |
drh | 6934fc7 | 2013-10-31 15:37:49 +0000 | [diff] [blame] | 1154 | ** The regNewData parameter is the first register in a range that contains |
| 1155 | ** the data to be inserted or the data after the update. There will be |
| 1156 | ** pTab->nCol+1 registers in this range. The first register (the one |
| 1157 | ** that regNewData points to) will contain the new rowid, or NULL in the |
| 1158 | ** case of a WITHOUT ROWID table. The second register in the range will |
| 1159 | ** contain the content of the first table column. The third register will |
| 1160 | ** contain the content of the second table column. And so forth. |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 1161 | ** |
drh | f8ffb27 | 2013-11-01 17:08:56 +0000 | [diff] [blame] | 1162 | ** The regOldData parameter is similar to regNewData except that it contains |
| 1163 | ** the data prior to an UPDATE rather than afterwards. regOldData is zero |
| 1164 | ** for an INSERT. This routine can distinguish between UPDATE and INSERT by |
| 1165 | ** checking regOldData for zero. |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 1166 | ** |
drh | f8ffb27 | 2013-11-01 17:08:56 +0000 | [diff] [blame] | 1167 | ** For an UPDATE, the pkChng boolean is true if the true primary key (the |
| 1168 | ** rowid for a normal table or the PRIMARY KEY for a WITHOUT ROWID table) |
| 1169 | ** might be modified by the UPDATE. If pkChng is false, then the key of |
| 1170 | ** the iDataCur content table is guaranteed to be unchanged by the UPDATE. |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 1171 | ** |
drh | f8ffb27 | 2013-11-01 17:08:56 +0000 | [diff] [blame] | 1172 | ** For an INSERT, the pkChng boolean indicates whether or not the rowid |
| 1173 | ** was explicitly specified as part of the INSERT statement. If pkChng |
| 1174 | ** is zero, it means that the either rowid is computed automatically or |
| 1175 | ** that the table is a WITHOUT ROWID table and has no rowid. On an INSERT, |
| 1176 | ** pkChng will only be true if the INSERT statement provides an integer |
| 1177 | ** value for either the rowid column or its INTEGER PRIMARY KEY alias. |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 1178 | ** |
drh | 6934fc7 | 2013-10-31 15:37:49 +0000 | [diff] [blame] | 1179 | ** The code generated by this routine will store new index entries into |
drh | aa9b896 | 2008-01-08 02:57:55 +0000 | [diff] [blame] | 1180 | ** registers identified by aRegIdx[]. No index entry is created for |
| 1181 | ** indices where aRegIdx[i]==0. The order of indices in aRegIdx[] is |
| 1182 | ** the same as the order of indices on the linked list of indices |
drh | 6934fc7 | 2013-10-31 15:37:49 +0000 | [diff] [blame] | 1183 | ** at pTab->pIndex. |
| 1184 | ** |
| 1185 | ** The caller must have already opened writeable cursors on the main |
| 1186 | ** table and all applicable indices (that is to say, all indices for which |
| 1187 | ** aRegIdx[] is not zero). iDataCur is the cursor for the main table when |
| 1188 | ** inserting or updating a rowid table, or the cursor for the PRIMARY KEY |
| 1189 | ** index when operating on a WITHOUT ROWID table. iIdxCur is the cursor |
| 1190 | ** for the first index in the pTab->pIndex list. Cursors for other indices |
| 1191 | ** are at iIdxCur+N for the N-th element of the pTab->pIndex list. |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 1192 | ** |
| 1193 | ** This routine also generates code to check constraints. NOT NULL, |
| 1194 | ** CHECK, and UNIQUE constraints are all checked. If a constraint fails, |
drh | 1c92853 | 2002-01-31 15:54:21 +0000 | [diff] [blame] | 1195 | ** then the appropriate action is performed. There are five possible |
| 1196 | ** actions: ROLLBACK, ABORT, FAIL, REPLACE, and IGNORE. |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 1197 | ** |
| 1198 | ** Constraint type Action What Happens |
| 1199 | ** --------------- ---------- ---------------------------------------- |
drh | 1c92853 | 2002-01-31 15:54:21 +0000 | [diff] [blame] | 1200 | ** any ROLLBACK The current transaction is rolled back and |
drh | 6934fc7 | 2013-10-31 15:37:49 +0000 | [diff] [blame] | 1201 | ** sqlite3_step() returns immediately with a |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 1202 | ** return code of SQLITE_CONSTRAINT. |
| 1203 | ** |
drh | 1c92853 | 2002-01-31 15:54:21 +0000 | [diff] [blame] | 1204 | ** any ABORT Back out changes from the current command |
| 1205 | ** only (do not do a complete rollback) then |
drh | 6934fc7 | 2013-10-31 15:37:49 +0000 | [diff] [blame] | 1206 | ** cause sqlite3_step() to return immediately |
drh | 1c92853 | 2002-01-31 15:54:21 +0000 | [diff] [blame] | 1207 | ** with SQLITE_CONSTRAINT. |
| 1208 | ** |
drh | 6934fc7 | 2013-10-31 15:37:49 +0000 | [diff] [blame] | 1209 | ** any FAIL Sqlite3_step() returns immediately with a |
drh | 1c92853 | 2002-01-31 15:54:21 +0000 | [diff] [blame] | 1210 | ** return code of SQLITE_CONSTRAINT. The |
| 1211 | ** transaction is not rolled back and any |
drh | 6934fc7 | 2013-10-31 15:37:49 +0000 | [diff] [blame] | 1212 | ** changes to prior rows are retained. |
drh | 1c92853 | 2002-01-31 15:54:21 +0000 | [diff] [blame] | 1213 | ** |
drh | 6934fc7 | 2013-10-31 15:37:49 +0000 | [diff] [blame] | 1214 | ** any IGNORE The attempt in insert or update the current |
| 1215 | ** row is skipped, without throwing an error. |
| 1216 | ** Processing continues with the next row. |
| 1217 | ** (There is an immediate jump to ignoreDest.) |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 1218 | ** |
| 1219 | ** NOT NULL REPLACE The NULL value is replace by the default |
| 1220 | ** value for that column. If the default value |
| 1221 | ** is NULL, the action is the same as ABORT. |
| 1222 | ** |
| 1223 | ** UNIQUE REPLACE The other row that conflicts with the row |
| 1224 | ** being inserted is removed. |
| 1225 | ** |
| 1226 | ** CHECK REPLACE Illegal. The results in an exception. |
| 1227 | ** |
drh | 1c92853 | 2002-01-31 15:54:21 +0000 | [diff] [blame] | 1228 | ** Which action to take is determined by the overrideError parameter. |
| 1229 | ** Or if overrideError==OE_Default, then the pParse->onError parameter |
| 1230 | ** is used. Or if pParse->onError==OE_Default then the onError value |
| 1231 | ** for the constraint is used. |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 1232 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1233 | void sqlite3GenerateConstraintChecks( |
drh | 6934fc7 | 2013-10-31 15:37:49 +0000 | [diff] [blame] | 1234 | Parse *pParse, /* The parser context */ |
| 1235 | Table *pTab, /* The table being inserted or updated */ |
drh | f8ffb27 | 2013-11-01 17:08:56 +0000 | [diff] [blame] | 1236 | int *aRegIdx, /* Use register aRegIdx[i] for index i. 0 for unused */ |
drh | 6934fc7 | 2013-10-31 15:37:49 +0000 | [diff] [blame] | 1237 | int iDataCur, /* Canonical data cursor (main table or PK index) */ |
| 1238 | int iIdxCur, /* First index cursor */ |
| 1239 | int regNewData, /* First register in a range holding values to insert */ |
drh | f8ffb27 | 2013-11-01 17:08:56 +0000 | [diff] [blame] | 1240 | int regOldData, /* Previous content. 0 for INSERTs */ |
| 1241 | u8 pkChng, /* Non-zero if the rowid or PRIMARY KEY changed */ |
| 1242 | u8 overrideError, /* Override onError to this if not OE_Default */ |
drh | 6934fc7 | 2013-10-31 15:37:49 +0000 | [diff] [blame] | 1243 | int ignoreDest, /* Jump to this label on an OE_Ignore resolution */ |
drh | bdb0022 | 2016-02-10 16:03:20 +0000 | [diff] [blame] | 1244 | int *pbMayReplace, /* OUT: Set to true if constraint may cause a replace */ |
| 1245 | int *aiChng /* column i is unchanged if aiChng[i]<0 */ |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 1246 | ){ |
drh | 6934fc7 | 2013-10-31 15:37:49 +0000 | [diff] [blame] | 1247 | Vdbe *v; /* VDBE under constrution */ |
drh | 1b7ecbb | 2009-05-03 01:00:59 +0000 | [diff] [blame] | 1248 | Index *pIdx; /* Pointer to one of the indices */ |
drh | 11e8527 | 2013-10-26 15:40:48 +0000 | [diff] [blame] | 1249 | Index *pPk = 0; /* The PRIMARY KEY index */ |
drh | 2938f92 | 2012-03-07 19:13:29 +0000 | [diff] [blame] | 1250 | sqlite3 *db; /* Database connection */ |
drh | f8ffb27 | 2013-11-01 17:08:56 +0000 | [diff] [blame] | 1251 | int i; /* loop counter */ |
| 1252 | int ix; /* Index loop counter */ |
| 1253 | int nCol; /* Number of columns */ |
| 1254 | int onError; /* Conflict resolution strategy */ |
drh | 728e0f9 | 2015-10-10 14:41:28 +0000 | [diff] [blame] | 1255 | int addr1; /* Address of jump instruction */ |
drh | 1b7ecbb | 2009-05-03 01:00:59 +0000 | [diff] [blame] | 1256 | int seenReplace = 0; /* True if REPLACE is used to resolve INT PK conflict */ |
drh | 6fbe41a | 2013-10-30 20:22:55 +0000 | [diff] [blame] | 1257 | int nPkField; /* Number of fields in PRIMARY KEY. 1 for ROWID tables */ |
drh | 8d1b82e | 2013-11-05 19:41:32 +0000 | [diff] [blame] | 1258 | int ipkTop = 0; /* Top of the rowid change constraint check */ |
| 1259 | int ipkBottom = 0; /* Bottom of the rowid change constraint check */ |
| 1260 | u8 isUpdate; /* True if this is an UPDATE operation */ |
drh | 57bf4a8 | 2014-02-17 14:59:22 +0000 | [diff] [blame] | 1261 | u8 bAffinityDone = 0; /* True if the OP_Affinity operation has been run */ |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 1262 | |
drh | f8ffb27 | 2013-11-01 17:08:56 +0000 | [diff] [blame] | 1263 | isUpdate = regOldData!=0; |
drh | 2938f92 | 2012-03-07 19:13:29 +0000 | [diff] [blame] | 1264 | db = pParse->db; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1265 | v = sqlite3GetVdbe(pParse); |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 1266 | assert( v!=0 ); |
drh | 417be79 | 2002-03-03 18:59:40 +0000 | [diff] [blame] | 1267 | assert( pTab->pSelect==0 ); /* This table is not a VIEW */ |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 1268 | nCol = pTab->nCol; |
drh | 6934fc7 | 2013-10-31 15:37:49 +0000 | [diff] [blame] | 1269 | |
| 1270 | /* pPk is the PRIMARY KEY index for WITHOUT ROWID tables and NULL for |
| 1271 | ** normal rowid tables. nPkField is the number of key fields in the |
| 1272 | ** pPk index or 1 for a rowid table. In other words, nPkField is the |
| 1273 | ** number of fields in the true primary key of the table. */ |
drh | 26198bb | 2013-10-31 11:15:09 +0000 | [diff] [blame] | 1274 | if( HasRowid(pTab) ){ |
| 1275 | pPk = 0; |
| 1276 | nPkField = 1; |
| 1277 | }else{ |
| 1278 | pPk = sqlite3PrimaryKeyIndex(pTab); |
| 1279 | nPkField = pPk->nKeyCol; |
| 1280 | } |
drh | 6fbe41a | 2013-10-30 20:22:55 +0000 | [diff] [blame] | 1281 | |
| 1282 | /* Record that this module has started */ |
| 1283 | VdbeModuleComment((v, "BEGIN: GenCnstCks(%d,%d,%d,%d,%d)", |
drh | 6934fc7 | 2013-10-31 15:37:49 +0000 | [diff] [blame] | 1284 | iDataCur, iIdxCur, regNewData, regOldData, pkChng)); |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 1285 | |
| 1286 | /* Test all NOT NULL constraints. |
| 1287 | */ |
| 1288 | for(i=0; i<nCol; i++){ |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 1289 | if( i==pTab->iPKey ){ |
drh | bdb0022 | 2016-02-10 16:03:20 +0000 | [diff] [blame] | 1290 | continue; /* ROWID is never NULL */ |
| 1291 | } |
| 1292 | if( aiChng && aiChng[i]<0 ){ |
| 1293 | /* Don't bother checking for NOT NULL on columns that do not change */ |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 1294 | continue; |
| 1295 | } |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 1296 | onError = pTab->aCol[i].notNull; |
drh | bdb0022 | 2016-02-10 16:03:20 +0000 | [diff] [blame] | 1297 | if( onError==OE_None ) continue; /* This column is allowed to be NULL */ |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 1298 | if( overrideError!=OE_Default ){ |
| 1299 | onError = overrideError; |
drh | a996e47 | 2003-05-16 02:30:27 +0000 | [diff] [blame] | 1300 | }else if( onError==OE_Default ){ |
| 1301 | onError = OE_Abort; |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 1302 | } |
danielk1977 | 7977a17 | 2004-11-09 12:44:37 +0000 | [diff] [blame] | 1303 | if( onError==OE_Replace && pTab->aCol[i].pDflt==0 ){ |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 1304 | onError = OE_Abort; |
| 1305 | } |
danielk1977 | b84f96f | 2005-01-20 11:32:23 +0000 | [diff] [blame] | 1306 | assert( onError==OE_Rollback || onError==OE_Abort || onError==OE_Fail |
| 1307 | || onError==OE_Ignore || onError==OE_Replace ); |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 1308 | switch( onError ){ |
drh | 1c92853 | 2002-01-31 15:54:21 +0000 | [diff] [blame] | 1309 | case OE_Abort: |
dan | e0af83a | 2009-09-08 19:15:01 +0000 | [diff] [blame] | 1310 | sqlite3MayAbort(pParse); |
drh | 0978d4f | 2013-10-29 16:14:35 +0000 | [diff] [blame] | 1311 | /* Fall through */ |
dan | e0af83a | 2009-09-08 19:15:01 +0000 | [diff] [blame] | 1312 | case OE_Rollback: |
drh | 1c92853 | 2002-01-31 15:54:21 +0000 | [diff] [blame] | 1313 | case OE_Fail: { |
drh | f9c8ce3 | 2013-11-05 13:33:55 +0000 | [diff] [blame] | 1314 | char *zMsg = sqlite3MPrintf(db, "%s.%s", pTab->zName, |
| 1315 | pTab->aCol[i].zName); |
drh | 2700aca | 2016-12-08 01:38:24 +0000 | [diff] [blame] | 1316 | sqlite3VdbeAddOp3(v, OP_HaltIfNull, SQLITE_CONSTRAINT_NOTNULL, onError, |
| 1317 | regNewData+1+i); |
| 1318 | sqlite3VdbeAppendP4(v, zMsg, P4_DYNAMIC); |
drh | f9c8ce3 | 2013-11-05 13:33:55 +0000 | [diff] [blame] | 1319 | sqlite3VdbeChangeP5(v, P5_ConstraintNotNull); |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 1320 | VdbeCoverage(v); |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 1321 | break; |
| 1322 | } |
| 1323 | case OE_Ignore: { |
drh | 6934fc7 | 2013-10-31 15:37:49 +0000 | [diff] [blame] | 1324 | sqlite3VdbeAddOp2(v, OP_IsNull, regNewData+1+i, ignoreDest); |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 1325 | VdbeCoverage(v); |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 1326 | break; |
| 1327 | } |
drh | 098d168 | 2009-05-02 15:46:46 +0000 | [diff] [blame] | 1328 | default: { |
| 1329 | assert( onError==OE_Replace ); |
drh | 728e0f9 | 2015-10-10 14:41:28 +0000 | [diff] [blame] | 1330 | addr1 = sqlite3VdbeAddOp1(v, OP_NotNull, regNewData+1+i); |
| 1331 | VdbeCoverage(v); |
drh | 6934fc7 | 2013-10-31 15:37:49 +0000 | [diff] [blame] | 1332 | sqlite3ExprCode(pParse, pTab->aCol[i].pDflt, regNewData+1+i); |
drh | 728e0f9 | 2015-10-10 14:41:28 +0000 | [diff] [blame] | 1333 | sqlite3VdbeJumpHere(v, addr1); |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 1334 | break; |
| 1335 | } |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 1336 | } |
| 1337 | } |
| 1338 | |
| 1339 | /* Test all CHECK constraints |
| 1340 | */ |
drh | ffe07b2 | 2005-11-03 00:41:17 +0000 | [diff] [blame] | 1341 | #ifndef SQLITE_OMIT_CHECK |
drh | 2938f92 | 2012-03-07 19:13:29 +0000 | [diff] [blame] | 1342 | if( pTab->pCheck && (db->flags & SQLITE_IgnoreChecks)==0 ){ |
| 1343 | ExprList *pCheck = pTab->pCheck; |
drh | 6e97f8e | 2017-07-20 13:17:08 +0000 | [diff] [blame] | 1344 | pParse->iSelfTab = -(regNewData+1); |
drh | aa01c7e | 2006-03-15 16:26:10 +0000 | [diff] [blame] | 1345 | onError = overrideError!=OE_Default ? overrideError : OE_Abort; |
drh | 2938f92 | 2012-03-07 19:13:29 +0000 | [diff] [blame] | 1346 | for(i=0; i<pCheck->nExpr; i++){ |
drh | 05723a9 | 2016-02-10 19:10:50 +0000 | [diff] [blame] | 1347 | int allOk; |
drh | 2a0b527 | 2016-02-10 16:52:24 +0000 | [diff] [blame] | 1348 | Expr *pExpr = pCheck->a[i].pExpr; |
drh | 98bfa16 | 2016-02-10 18:24:05 +0000 | [diff] [blame] | 1349 | if( aiChng && checkConstraintUnchanged(pExpr, aiChng, pkChng) ) continue; |
drh | 05723a9 | 2016-02-10 19:10:50 +0000 | [diff] [blame] | 1350 | allOk = sqlite3VdbeMakeLabel(v); |
drh | 2a0b527 | 2016-02-10 16:52:24 +0000 | [diff] [blame] | 1351 | sqlite3ExprIfTrue(pParse, pExpr, allOk, SQLITE_JUMPIFNULL); |
drh | 2d8e920 | 2012-12-08 04:10:44 +0000 | [diff] [blame] | 1352 | if( onError==OE_Ignore ){ |
drh | 076e85f | 2015-09-03 13:46:12 +0000 | [diff] [blame] | 1353 | sqlite3VdbeGoto(v, ignoreDest); |
drh | 2d8e920 | 2012-12-08 04:10:44 +0000 | [diff] [blame] | 1354 | }else{ |
drh | f9c8ce3 | 2013-11-05 13:33:55 +0000 | [diff] [blame] | 1355 | char *zName = pCheck->a[i].zName; |
| 1356 | if( zName==0 ) zName = pTab->zName; |
drh | 2d8e920 | 2012-12-08 04:10:44 +0000 | [diff] [blame] | 1357 | if( onError==OE_Replace ) onError = OE_Abort; /* IMP: R-15569-63625 */ |
drh | d91c1a1 | 2013-02-09 13:58:25 +0000 | [diff] [blame] | 1358 | sqlite3HaltConstraint(pParse, SQLITE_CONSTRAINT_CHECK, |
drh | f9c8ce3 | 2013-11-05 13:33:55 +0000 | [diff] [blame] | 1359 | onError, zName, P4_TRANSIENT, |
| 1360 | P5_ConstraintCheck); |
drh | 2938f92 | 2012-03-07 19:13:29 +0000 | [diff] [blame] | 1361 | } |
drh | 2d8e920 | 2012-12-08 04:10:44 +0000 | [diff] [blame] | 1362 | sqlite3VdbeResolveLabel(v, allOk); |
drh | aa01c7e | 2006-03-15 16:26:10 +0000 | [diff] [blame] | 1363 | } |
drh | 6e97f8e | 2017-07-20 13:17:08 +0000 | [diff] [blame] | 1364 | pParse->iSelfTab = 0; |
drh | ffe07b2 | 2005-11-03 00:41:17 +0000 | [diff] [blame] | 1365 | } |
| 1366 | #endif /* !defined(SQLITE_OMIT_CHECK) */ |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 1367 | |
drh | f8ffb27 | 2013-11-01 17:08:56 +0000 | [diff] [blame] | 1368 | /* If rowid is changing, make sure the new rowid does not previously |
| 1369 | ** exist in the table. |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 1370 | */ |
drh | 6fbe41a | 2013-10-30 20:22:55 +0000 | [diff] [blame] | 1371 | if( pkChng && pPk==0 ){ |
drh | 11e8527 | 2013-10-26 15:40:48 +0000 | [diff] [blame] | 1372 | int addrRowidOk = sqlite3VdbeMakeLabel(v); |
| 1373 | |
drh | f8ffb27 | 2013-11-01 17:08:56 +0000 | [diff] [blame] | 1374 | /* Figure out what action to take in case of a rowid collision */ |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 1375 | onError = pTab->keyConf; |
| 1376 | if( overrideError!=OE_Default ){ |
| 1377 | onError = overrideError; |
drh | a996e47 | 2003-05-16 02:30:27 +0000 | [diff] [blame] | 1378 | }else if( onError==OE_Default ){ |
| 1379 | onError = OE_Abort; |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 1380 | } |
drh | 11e8527 | 2013-10-26 15:40:48 +0000 | [diff] [blame] | 1381 | |
dan | e1e2ae2 | 2009-09-24 16:52:28 +0000 | [diff] [blame] | 1382 | if( isUpdate ){ |
drh | 7405fa7 | 2016-11-09 16:03:36 +0000 | [diff] [blame] | 1383 | /* pkChng!=0 does not mean that the rowid has changed, only that |
drh | f8ffb27 | 2013-11-01 17:08:56 +0000 | [diff] [blame] | 1384 | ** it might have changed. Skip the conflict logic below if the rowid |
| 1385 | ** is unchanged. */ |
drh | 6934fc7 | 2013-10-31 15:37:49 +0000 | [diff] [blame] | 1386 | sqlite3VdbeAddOp3(v, OP_Eq, regNewData, addrRowidOk, regOldData); |
drh | 3d77dee | 2014-02-19 14:20:49 +0000 | [diff] [blame] | 1387 | sqlite3VdbeChangeP5(v, SQLITE_NOTNULL); |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 1388 | VdbeCoverage(v); |
dan | e1e2ae2 | 2009-09-24 16:52:28 +0000 | [diff] [blame] | 1389 | } |
drh | f8ffb27 | 2013-11-01 17:08:56 +0000 | [diff] [blame] | 1390 | |
drh | 8d1b82e | 2013-11-05 19:41:32 +0000 | [diff] [blame] | 1391 | /* If the response to a rowid conflict is REPLACE but the response |
| 1392 | ** to some other UNIQUE constraint is FAIL or IGNORE, then we need |
| 1393 | ** to defer the running of the rowid conflict checking until after |
| 1394 | ** the UNIQUE constraints have run. |
| 1395 | */ |
| 1396 | if( onError==OE_Replace && overrideError!=OE_Replace ){ |
| 1397 | for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){ |
| 1398 | if( pIdx->onError==OE_Ignore || pIdx->onError==OE_Fail ){ |
| 1399 | ipkTop = sqlite3VdbeAddOp0(v, OP_Goto); |
| 1400 | break; |
| 1401 | } |
| 1402 | } |
| 1403 | } |
| 1404 | |
drh | f8ffb27 | 2013-11-01 17:08:56 +0000 | [diff] [blame] | 1405 | /* Check to see if the new rowid already exists in the table. Skip |
| 1406 | ** the following conflict logic if it does not. */ |
drh | 6934fc7 | 2013-10-31 15:37:49 +0000 | [diff] [blame] | 1407 | sqlite3VdbeAddOp3(v, OP_NotExists, iDataCur, addrRowidOk, regNewData); |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 1408 | VdbeCoverage(v); |
drh | f8ffb27 | 2013-11-01 17:08:56 +0000 | [diff] [blame] | 1409 | |
| 1410 | /* Generate code that deals with a rowid collision */ |
dan | e1e2ae2 | 2009-09-24 16:52:28 +0000 | [diff] [blame] | 1411 | switch( onError ){ |
| 1412 | default: { |
| 1413 | onError = OE_Abort; |
| 1414 | /* Fall thru into the next case */ |
drh | 79b0c95 | 2002-05-21 12:56:43 +0000 | [diff] [blame] | 1415 | } |
dan | e1e2ae2 | 2009-09-24 16:52:28 +0000 | [diff] [blame] | 1416 | case OE_Rollback: |
| 1417 | case OE_Abort: |
| 1418 | case OE_Fail: { |
drh | f9c8ce3 | 2013-11-05 13:33:55 +0000 | [diff] [blame] | 1419 | sqlite3RowidConstraint(pParse, onError, pTab); |
dan | e1e2ae2 | 2009-09-24 16:52:28 +0000 | [diff] [blame] | 1420 | break; |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 1421 | } |
dan | e1e2ae2 | 2009-09-24 16:52:28 +0000 | [diff] [blame] | 1422 | case OE_Replace: { |
| 1423 | /* If there are DELETE triggers on this table and the |
| 1424 | ** recursive-triggers flag is set, call GenerateRowDelete() to |
mistachkin | d557843 | 2012-08-25 10:01:29 +0000 | [diff] [blame] | 1425 | ** remove the conflicting row from the table. This will fire |
dan | e1e2ae2 | 2009-09-24 16:52:28 +0000 | [diff] [blame] | 1426 | ** the triggers and remove both the table and index b-tree entries. |
| 1427 | ** |
| 1428 | ** Otherwise, if there are no triggers or the recursive-triggers |
dan | da730f6 | 2010-02-18 08:19:19 +0000 | [diff] [blame] | 1429 | ** flag is not set, but the table has one or more indexes, call |
| 1430 | ** GenerateRowIndexDelete(). This removes the index b-tree entries |
| 1431 | ** only. The table b-tree entry will be replaced by the new entry |
| 1432 | ** when it is inserted. |
| 1433 | ** |
| 1434 | ** If either GenerateRowDelete() or GenerateRowIndexDelete() is called, |
| 1435 | ** also invoke MultiWrite() to indicate that this VDBE may require |
| 1436 | ** statement rollback (if the statement is aborted after the delete |
| 1437 | ** takes place). Earlier versions called sqlite3MultiWrite() regardless, |
| 1438 | ** but being more selective here allows statements like: |
| 1439 | ** |
| 1440 | ** REPLACE INTO t(rowid) VALUES($newrowid) |
| 1441 | ** |
| 1442 | ** to run without a statement journal if there are no indexes on the |
| 1443 | ** table. |
| 1444 | */ |
dan | e1e2ae2 | 2009-09-24 16:52:28 +0000 | [diff] [blame] | 1445 | Trigger *pTrigger = 0; |
drh | 2938f92 | 2012-03-07 19:13:29 +0000 | [diff] [blame] | 1446 | if( db->flags&SQLITE_RecTriggers ){ |
dan | e1e2ae2 | 2009-09-24 16:52:28 +0000 | [diff] [blame] | 1447 | pTrigger = sqlite3TriggersExist(pParse, pTab, TK_DELETE, 0, 0); |
| 1448 | } |
dan | e7a94d8 | 2009-10-01 16:09:04 +0000 | [diff] [blame] | 1449 | if( pTrigger || sqlite3FkRequired(pParse, pTab, 0, 0) ){ |
dan | da730f6 | 2010-02-18 08:19:19 +0000 | [diff] [blame] | 1450 | sqlite3MultiWrite(pParse); |
drh | 26198bb | 2013-10-31 11:15:09 +0000 | [diff] [blame] | 1451 | sqlite3GenerateRowDelete(pParse, pTab, pTrigger, iDataCur, iIdxCur, |
dan | 438b881 | 2015-09-15 15:55:15 +0000 | [diff] [blame] | 1452 | regNewData, 1, 0, OE_Replace, 1, -1); |
dan | 46c47d4 | 2011-03-01 18:42:07 +0000 | [diff] [blame] | 1453 | }else{ |
drh | 9b1c62d | 2011-03-30 21:04:43 +0000 | [diff] [blame] | 1454 | #ifdef SQLITE_ENABLE_PREUPDATE_HOOK |
drh | cbf1b8e | 2013-11-11 22:55:26 +0000 | [diff] [blame] | 1455 | if( HasRowid(pTab) ){ |
| 1456 | /* This OP_Delete opcode fires the pre-update-hook only. It does |
| 1457 | ** not modify the b-tree. It is more efficient to let the coming |
| 1458 | ** OP_Insert replace the existing entry than it is to delete the |
| 1459 | ** existing entry and then insert a new one. */ |
| 1460 | sqlite3VdbeAddOp2(v, OP_Delete, iDataCur, OPFLAG_ISNOOP); |
drh | f14b7fb | 2016-12-07 21:35:55 +0000 | [diff] [blame] | 1461 | sqlite3VdbeAppendP4(v, pTab, P4_TABLE); |
drh | cbf1b8e | 2013-11-11 22:55:26 +0000 | [diff] [blame] | 1462 | } |
drh | 9b1c62d | 2011-03-30 21:04:43 +0000 | [diff] [blame] | 1463 | #endif /* SQLITE_ENABLE_PREUPDATE_HOOK */ |
dan | 46c47d4 | 2011-03-01 18:42:07 +0000 | [diff] [blame] | 1464 | if( pTab->pIndex ){ |
| 1465 | sqlite3MultiWrite(pParse); |
drh | b77ebd8 | 2015-09-15 13:42:16 +0000 | [diff] [blame] | 1466 | sqlite3GenerateRowIndexDelete(pParse, pTab, iDataCur, iIdxCur,0,-1); |
dan | 46c47d4 | 2011-03-01 18:42:07 +0000 | [diff] [blame] | 1467 | } |
dan | e1e2ae2 | 2009-09-24 16:52:28 +0000 | [diff] [blame] | 1468 | } |
| 1469 | seenReplace = 1; |
| 1470 | break; |
drh | 5383ae5 | 2003-06-04 12:23:30 +0000 | [diff] [blame] | 1471 | } |
dan | e1e2ae2 | 2009-09-24 16:52:28 +0000 | [diff] [blame] | 1472 | case OE_Ignore: { |
drh | 8d1b82e | 2013-11-05 19:41:32 +0000 | [diff] [blame] | 1473 | /*assert( seenReplace==0 );*/ |
drh | 076e85f | 2015-09-03 13:46:12 +0000 | [diff] [blame] | 1474 | sqlite3VdbeGoto(v, ignoreDest); |
dan | e1e2ae2 | 2009-09-24 16:52:28 +0000 | [diff] [blame] | 1475 | break; |
| 1476 | } |
| 1477 | } |
drh | 11e8527 | 2013-10-26 15:40:48 +0000 | [diff] [blame] | 1478 | sqlite3VdbeResolveLabel(v, addrRowidOk); |
drh | 8d1b82e | 2013-11-05 19:41:32 +0000 | [diff] [blame] | 1479 | if( ipkTop ){ |
| 1480 | ipkBottom = sqlite3VdbeAddOp0(v, OP_Goto); |
| 1481 | sqlite3VdbeJumpHere(v, ipkTop); |
drh | 5383ae5 | 2003-06-04 12:23:30 +0000 | [diff] [blame] | 1482 | } |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 1483 | } |
drh | 0bd1f4e | 2002-06-06 18:54:39 +0000 | [diff] [blame] | 1484 | |
| 1485 | /* Test all UNIQUE constraints by creating entries for each UNIQUE |
| 1486 | ** index and making sure that duplicate entries do not already exist. |
drh | 11e8527 | 2013-10-26 15:40:48 +0000 | [diff] [blame] | 1487 | ** Compute the revised record entries for indices as we go. |
drh | f8ffb27 | 2013-11-01 17:08:56 +0000 | [diff] [blame] | 1488 | ** |
| 1489 | ** This loop also handles the case of the PRIMARY KEY index for a |
| 1490 | ** WITHOUT ROWID table. |
drh | 0bd1f4e | 2002-06-06 18:54:39 +0000 | [diff] [blame] | 1491 | */ |
drh | 26198bb | 2013-10-31 11:15:09 +0000 | [diff] [blame] | 1492 | for(ix=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, ix++){ |
drh | 6934fc7 | 2013-10-31 15:37:49 +0000 | [diff] [blame] | 1493 | int regIdx; /* Range of registers hold conent for pIdx */ |
| 1494 | int regR; /* Range of registers holding conflicting PK */ |
| 1495 | int iThisCur; /* Cursor for this UNIQUE index */ |
| 1496 | int addrUniqueOk; /* Jump here if the UNIQUE constraint is satisfied */ |
drh | 2184fc7 | 2011-04-09 03:04:13 +0000 | [diff] [blame] | 1497 | |
drh | 26198bb | 2013-10-31 11:15:09 +0000 | [diff] [blame] | 1498 | if( aRegIdx[ix]==0 ) continue; /* Skip indices that do not change */ |
drh | 57bf4a8 | 2014-02-17 14:59:22 +0000 | [diff] [blame] | 1499 | if( bAffinityDone==0 ){ |
| 1500 | sqlite3TableAffinity(v, pTab, regNewData+1); |
| 1501 | bAffinityDone = 1; |
| 1502 | } |
drh | 6934fc7 | 2013-10-31 15:37:49 +0000 | [diff] [blame] | 1503 | iThisCur = iIdxCur+ix; |
| 1504 | addrUniqueOk = sqlite3VdbeMakeLabel(v); |
drh | b2fe7d8 | 2003-04-20 17:29:23 +0000 | [diff] [blame] | 1505 | |
drh | f8ffb27 | 2013-11-01 17:08:56 +0000 | [diff] [blame] | 1506 | /* Skip partial indices for which the WHERE clause is not true */ |
drh | b2b9d3d | 2013-08-01 01:14:43 +0000 | [diff] [blame] | 1507 | if( pIdx->pPartIdxWhere ){ |
drh | 26198bb | 2013-10-31 11:15:09 +0000 | [diff] [blame] | 1508 | sqlite3VdbeAddOp2(v, OP_Null, 0, aRegIdx[ix]); |
drh | 6e97f8e | 2017-07-20 13:17:08 +0000 | [diff] [blame] | 1509 | pParse->iSelfTab = -(regNewData+1); |
drh | 72bc820 | 2015-06-11 13:58:35 +0000 | [diff] [blame] | 1510 | sqlite3ExprIfFalseDup(pParse, pIdx->pPartIdxWhere, addrUniqueOk, |
| 1511 | SQLITE_JUMPIFNULL); |
drh | 6e97f8e | 2017-07-20 13:17:08 +0000 | [diff] [blame] | 1512 | pParse->iSelfTab = 0; |
drh | b2b9d3d | 2013-08-01 01:14:43 +0000 | [diff] [blame] | 1513 | } |
| 1514 | |
drh | 6934fc7 | 2013-10-31 15:37:49 +0000 | [diff] [blame] | 1515 | /* Create a record for this index entry as it should appear after |
drh | f8ffb27 | 2013-11-01 17:08:56 +0000 | [diff] [blame] | 1516 | ** the insert or update. Store that record in the aRegIdx[ix] register |
| 1517 | */ |
drh | bf2f573 | 2016-11-10 16:07:43 +0000 | [diff] [blame] | 1518 | regIdx = aRegIdx[ix]+1; |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 1519 | for(i=0; i<pIdx->nColumn; i++){ |
drh | 6934fc7 | 2013-10-31 15:37:49 +0000 | [diff] [blame] | 1520 | int iField = pIdx->aiColumn[i]; |
drh | f82b9af | 2013-11-01 14:03:20 +0000 | [diff] [blame] | 1521 | int x; |
drh | 4b92f98 | 2015-09-29 17:20:14 +0000 | [diff] [blame] | 1522 | if( iField==XN_EXPR ){ |
drh | 6e97f8e | 2017-07-20 13:17:08 +0000 | [diff] [blame] | 1523 | pParse->iSelfTab = -(regNewData+1); |
drh | 1c75c9d | 2015-12-21 15:22:13 +0000 | [diff] [blame] | 1524 | sqlite3ExprCodeCopy(pParse, pIdx->aColExpr->a[i].pExpr, regIdx+i); |
drh | 6e97f8e | 2017-07-20 13:17:08 +0000 | [diff] [blame] | 1525 | pParse->iSelfTab = 0; |
drh | 1f9ca2c | 2015-08-25 16:57:52 +0000 | [diff] [blame] | 1526 | VdbeComment((v, "%s column %d", pIdx->zName, i)); |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 1527 | }else{ |
drh | 4b92f98 | 2015-09-29 17:20:14 +0000 | [diff] [blame] | 1528 | if( iField==XN_ROWID || iField==pTab->iPKey ){ |
drh | 1f9ca2c | 2015-08-25 16:57:52 +0000 | [diff] [blame] | 1529 | x = regNewData; |
drh | 1f9ca2c | 2015-08-25 16:57:52 +0000 | [diff] [blame] | 1530 | }else{ |
| 1531 | x = iField + regNewData + 1; |
| 1532 | } |
drh | fed7ac6 | 2015-10-15 18:04:59 +0000 | [diff] [blame] | 1533 | sqlite3VdbeAddOp2(v, iField<0 ? OP_IntCopy : OP_SCopy, x, regIdx+i); |
drh | 1f9ca2c | 2015-08-25 16:57:52 +0000 | [diff] [blame] | 1534 | VdbeComment((v, "%s", iField<0 ? "rowid" : pTab->aCol[iField].zName)); |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 1535 | } |
| 1536 | } |
drh | 26198bb | 2013-10-31 11:15:09 +0000 | [diff] [blame] | 1537 | sqlite3VdbeAddOp3(v, OP_MakeRecord, regIdx, pIdx->nColumn, aRegIdx[ix]); |
drh | 26198bb | 2013-10-31 11:15:09 +0000 | [diff] [blame] | 1538 | VdbeComment((v, "for %s", pIdx->zName)); |
drh | 7e4acf7 | 2017-02-14 20:00:16 +0000 | [diff] [blame] | 1539 | #ifdef SQLITE_ENABLE_NULL_TRIM |
| 1540 | if( pIdx->idxType==2 ) sqlite3SetMakeRecordP5(v, pIdx->pTable); |
| 1541 | #endif |
drh | b2fe7d8 | 2003-04-20 17:29:23 +0000 | [diff] [blame] | 1542 | |
drh | f8ffb27 | 2013-11-01 17:08:56 +0000 | [diff] [blame] | 1543 | /* In an UPDATE operation, if this index is the PRIMARY KEY index |
| 1544 | ** of a WITHOUT ROWID table and there has been no change the |
| 1545 | ** primary key, then no collision is possible. The collision detection |
| 1546 | ** logic below can all be skipped. */ |
drh | 00012df | 2013-11-05 01:59:07 +0000 | [diff] [blame] | 1547 | if( isUpdate && pPk==pIdx && pkChng==0 ){ |
drh | da475b8 | 2013-11-04 21:44:54 +0000 | [diff] [blame] | 1548 | sqlite3VdbeResolveLabel(v, addrUniqueOk); |
| 1549 | continue; |
| 1550 | } |
drh | f8ffb27 | 2013-11-01 17:08:56 +0000 | [diff] [blame] | 1551 | |
drh | 6934fc7 | 2013-10-31 15:37:49 +0000 | [diff] [blame] | 1552 | /* Find out what action to take in case there is a uniqueness conflict */ |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 1553 | onError = pIdx->onError; |
danielk1977 | de63035 | 2009-05-04 11:42:29 +0000 | [diff] [blame] | 1554 | if( onError==OE_None ){ |
drh | 11e8527 | 2013-10-26 15:40:48 +0000 | [diff] [blame] | 1555 | sqlite3VdbeResolveLabel(v, addrUniqueOk); |
danielk1977 | de63035 | 2009-05-04 11:42:29 +0000 | [diff] [blame] | 1556 | continue; /* pIdx is not a UNIQUE index */ |
| 1557 | } |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 1558 | if( overrideError!=OE_Default ){ |
| 1559 | onError = overrideError; |
drh | a996e47 | 2003-05-16 02:30:27 +0000 | [diff] [blame] | 1560 | }else if( onError==OE_Default ){ |
| 1561 | onError = OE_Abort; |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 1562 | } |
drh | c6c9e15 | 2016-11-10 17:01:36 +0000 | [diff] [blame] | 1563 | |
drh | 801f55d | 2017-01-04 22:02:56 +0000 | [diff] [blame] | 1564 | /* Collision detection may be omitted if all of the following are true: |
| 1565 | ** (1) The conflict resolution algorithm is REPLACE |
| 1566 | ** (2) The table is a WITHOUT ROWID table |
| 1567 | ** (3) There are no secondary indexes on the table |
| 1568 | ** (4) No delete triggers need to be fired if there is a conflict |
dan | f9a12a1 | 2017-01-05 06:57:42 +0000 | [diff] [blame] | 1569 | ** (5) No FK constraint counters need to be updated if a conflict occurs. |
drh | 801f55d | 2017-01-04 22:02:56 +0000 | [diff] [blame] | 1570 | */ |
| 1571 | if( (ix==0 && pIdx->pNext==0) /* Condition 3 */ |
| 1572 | && pPk==pIdx /* Condition 2 */ |
| 1573 | && onError==OE_Replace /* Condition 1 */ |
| 1574 | && ( 0==(db->flags&SQLITE_RecTriggers) || /* Condition 4 */ |
| 1575 | 0==sqlite3TriggersExist(pParse, pTab, TK_DELETE, 0, 0)) |
dan | f9a12a1 | 2017-01-05 06:57:42 +0000 | [diff] [blame] | 1576 | && ( 0==(db->flags&SQLITE_ForeignKeys) || /* Condition 5 */ |
| 1577 | (0==pTab->pFKey && 0==sqlite3FkReferences(pTab))) |
drh | 801f55d | 2017-01-04 22:02:56 +0000 | [diff] [blame] | 1578 | ){ |
| 1579 | sqlite3VdbeResolveLabel(v, addrUniqueOk); |
| 1580 | continue; |
drh | c6c9e15 | 2016-11-10 17:01:36 +0000 | [diff] [blame] | 1581 | } |
| 1582 | |
drh | b2fe7d8 | 2003-04-20 17:29:23 +0000 | [diff] [blame] | 1583 | /* Check to see if the new index entry will be unique */ |
drh | 4d795ef | 2018-01-02 18:11:11 +0000 | [diff] [blame] | 1584 | sqlite3ExprCachePush(pParse); |
drh | 26198bb | 2013-10-31 11:15:09 +0000 | [diff] [blame] | 1585 | sqlite3VdbeAddOp4Int(v, OP_NoConflict, iThisCur, addrUniqueOk, |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 1586 | regIdx, pIdx->nKeyCol); VdbeCoverage(v); |
drh | f8ffb27 | 2013-11-01 17:08:56 +0000 | [diff] [blame] | 1587 | |
| 1588 | /* Generate code to handle collisions */ |
drh | 392ee21 | 2013-11-08 16:54:56 +0000 | [diff] [blame] | 1589 | regR = (pIdx==pPk) ? regIdx : sqlite3GetTempRange(pParse, nPkField); |
drh | 46d03fc | 2013-12-19 02:23:45 +0000 | [diff] [blame] | 1590 | if( isUpdate || onError==OE_Replace ){ |
| 1591 | if( HasRowid(pTab) ){ |
| 1592 | sqlite3VdbeAddOp2(v, OP_IdxRowid, iThisCur, regR); |
| 1593 | /* Conflict only if the rowid of the existing index entry |
| 1594 | ** is different from old-rowid */ |
| 1595 | if( isUpdate ){ |
| 1596 | sqlite3VdbeAddOp3(v, OP_Eq, regR, addrUniqueOk, regOldData); |
drh | 3d77dee | 2014-02-19 14:20:49 +0000 | [diff] [blame] | 1597 | sqlite3VdbeChangeP5(v, SQLITE_NOTNULL); |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 1598 | VdbeCoverage(v); |
drh | da475b8 | 2013-11-04 21:44:54 +0000 | [diff] [blame] | 1599 | } |
drh | 46d03fc | 2013-12-19 02:23:45 +0000 | [diff] [blame] | 1600 | }else{ |
| 1601 | int x; |
| 1602 | /* Extract the PRIMARY KEY from the end of the index entry and |
| 1603 | ** store it in registers regR..regR+nPk-1 */ |
drh | a021f12 | 2013-12-19 14:34:34 +0000 | [diff] [blame] | 1604 | if( pIdx!=pPk ){ |
drh | 46d03fc | 2013-12-19 02:23:45 +0000 | [diff] [blame] | 1605 | for(i=0; i<pPk->nKeyCol; i++){ |
drh | 4b92f98 | 2015-09-29 17:20:14 +0000 | [diff] [blame] | 1606 | assert( pPk->aiColumn[i]>=0 ); |
drh | 46d03fc | 2013-12-19 02:23:45 +0000 | [diff] [blame] | 1607 | x = sqlite3ColumnOfIndex(pIdx, pPk->aiColumn[i]); |
| 1608 | sqlite3VdbeAddOp3(v, OP_Column, iThisCur, x, regR+i); |
| 1609 | VdbeComment((v, "%s.%s", pTab->zName, |
| 1610 | pTab->aCol[pPk->aiColumn[i]].zName)); |
drh | da475b8 | 2013-11-04 21:44:54 +0000 | [diff] [blame] | 1611 | } |
drh | 46d03fc | 2013-12-19 02:23:45 +0000 | [diff] [blame] | 1612 | } |
| 1613 | if( isUpdate ){ |
| 1614 | /* If currently processing the PRIMARY KEY of a WITHOUT ROWID |
| 1615 | ** table, only conflict if the new PRIMARY KEY values are actually |
| 1616 | ** different from the old. |
| 1617 | ** |
| 1618 | ** For a UNIQUE index, only conflict if the PRIMARY KEY values |
| 1619 | ** of the matched index row are different from the original PRIMARY |
| 1620 | ** KEY values of this row before the update. */ |
| 1621 | int addrJump = sqlite3VdbeCurrentAddr(v)+pPk->nKeyCol; |
| 1622 | int op = OP_Ne; |
drh | 48dd1d8 | 2014-05-27 18:18:58 +0000 | [diff] [blame] | 1623 | int regCmp = (IsPrimaryKeyIndex(pIdx) ? regIdx : regR); |
drh | 46d03fc | 2013-12-19 02:23:45 +0000 | [diff] [blame] | 1624 | |
| 1625 | for(i=0; i<pPk->nKeyCol; i++){ |
| 1626 | char *p4 = (char*)sqlite3LocateCollSeq(pParse, pPk->azColl[i]); |
| 1627 | x = pPk->aiColumn[i]; |
drh | 4b92f98 | 2015-09-29 17:20:14 +0000 | [diff] [blame] | 1628 | assert( x>=0 ); |
drh | 46d03fc | 2013-12-19 02:23:45 +0000 | [diff] [blame] | 1629 | if( i==(pPk->nKeyCol-1) ){ |
| 1630 | addrJump = addrUniqueOk; |
| 1631 | op = OP_Eq; |
| 1632 | } |
| 1633 | sqlite3VdbeAddOp4(v, op, |
| 1634 | regOldData+1+x, addrJump, regCmp+i, p4, P4_COLLSEQ |
| 1635 | ); |
drh | 3d77dee | 2014-02-19 14:20:49 +0000 | [diff] [blame] | 1636 | sqlite3VdbeChangeP5(v, SQLITE_NOTNULL); |
| 1637 | VdbeCoverageIf(v, op==OP_Eq); |
| 1638 | VdbeCoverageIf(v, op==OP_Ne); |
drh | 46d03fc | 2013-12-19 02:23:45 +0000 | [diff] [blame] | 1639 | } |
drh | 26198bb | 2013-10-31 11:15:09 +0000 | [diff] [blame] | 1640 | } |
drh | 26198bb | 2013-10-31 11:15:09 +0000 | [diff] [blame] | 1641 | } |
drh | 11e8527 | 2013-10-26 15:40:48 +0000 | [diff] [blame] | 1642 | } |
drh | b2fe7d8 | 2003-04-20 17:29:23 +0000 | [diff] [blame] | 1643 | |
| 1644 | /* Generate code that executes if the new index entry is not unique */ |
danielk1977 | b84f96f | 2005-01-20 11:32:23 +0000 | [diff] [blame] | 1645 | assert( onError==OE_Rollback || onError==OE_Abort || onError==OE_Fail |
| 1646 | || onError==OE_Ignore || onError==OE_Replace ); |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 1647 | switch( onError ){ |
drh | 1c92853 | 2002-01-31 15:54:21 +0000 | [diff] [blame] | 1648 | case OE_Rollback: |
| 1649 | case OE_Abort: |
| 1650 | case OE_Fail: { |
drh | f9c8ce3 | 2013-11-05 13:33:55 +0000 | [diff] [blame] | 1651 | sqlite3UniqueConstraint(pParse, onError, pIdx); |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 1652 | break; |
| 1653 | } |
| 1654 | case OE_Ignore: { |
drh | 076e85f | 2015-09-03 13:46:12 +0000 | [diff] [blame] | 1655 | sqlite3VdbeGoto(v, ignoreDest); |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 1656 | break; |
| 1657 | } |
drh | 098d168 | 2009-05-02 15:46:46 +0000 | [diff] [blame] | 1658 | default: { |
dan | 2283d46 | 2009-09-08 15:55:15 +0000 | [diff] [blame] | 1659 | Trigger *pTrigger = 0; |
drh | 098d168 | 2009-05-02 15:46:46 +0000 | [diff] [blame] | 1660 | assert( onError==OE_Replace ); |
dan | 1bea559 | 2009-09-24 11:31:21 +0000 | [diff] [blame] | 1661 | sqlite3MultiWrite(pParse); |
drh | 2938f92 | 2012-03-07 19:13:29 +0000 | [diff] [blame] | 1662 | if( db->flags&SQLITE_RecTriggers ){ |
dan | 2283d46 | 2009-09-08 15:55:15 +0000 | [diff] [blame] | 1663 | pTrigger = sqlite3TriggersExist(pParse, pTab, TK_DELETE, 0, 0); |
| 1664 | } |
drh | 26198bb | 2013-10-31 11:15:09 +0000 | [diff] [blame] | 1665 | sqlite3GenerateRowDelete(pParse, pTab, pTrigger, iDataCur, iIdxCur, |
drh | b0264ee | 2015-09-14 14:45:50 +0000 | [diff] [blame] | 1666 | regR, nPkField, 0, OE_Replace, |
drh | 6811693 | 2017-01-07 14:47:03 +0000 | [diff] [blame] | 1667 | (pIdx==pPk ? ONEPASS_SINGLE : ONEPASS_OFF), iThisCur); |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 1668 | seenReplace = 1; |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 1669 | break; |
| 1670 | } |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 1671 | } |
drh | 11e8527 | 2013-10-26 15:40:48 +0000 | [diff] [blame] | 1672 | sqlite3VdbeResolveLabel(v, addrUniqueOk); |
drh | 4d795ef | 2018-01-02 18:11:11 +0000 | [diff] [blame] | 1673 | sqlite3ExprCachePop(pParse); |
drh | 392ee21 | 2013-11-08 16:54:56 +0000 | [diff] [blame] | 1674 | if( regR!=regIdx ) sqlite3ReleaseTempRange(pParse, regR, nPkField); |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 1675 | } |
drh | 8d1b82e | 2013-11-05 19:41:32 +0000 | [diff] [blame] | 1676 | if( ipkTop ){ |
drh | 076e85f | 2015-09-03 13:46:12 +0000 | [diff] [blame] | 1677 | sqlite3VdbeGoto(v, ipkTop+1); |
drh | 8d1b82e | 2013-11-05 19:41:32 +0000 | [diff] [blame] | 1678 | sqlite3VdbeJumpHere(v, ipkBottom); |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 1679 | } |
dan | 1da40a3 | 2009-09-19 17:00:31 +0000 | [diff] [blame] | 1680 | |
drh | 4308e34 | 2013-11-11 16:55:52 +0000 | [diff] [blame] | 1681 | *pbMayReplace = seenReplace; |
drh | ce60aa4 | 2013-11-08 01:09:15 +0000 | [diff] [blame] | 1682 | VdbeModuleComment((v, "END: GenCnstCks(%d)", seenReplace)); |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 1683 | } |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 1684 | |
drh | d447dce | 2017-01-25 20:55:11 +0000 | [diff] [blame] | 1685 | #ifdef SQLITE_ENABLE_NULL_TRIM |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 1686 | /* |
drh | 585ce19 | 2017-01-25 14:58:27 +0000 | [diff] [blame] | 1687 | ** Change the P5 operand on the last opcode (which should be an OP_MakeRecord) |
| 1688 | ** to be the number of columns in table pTab that must not be NULL-trimmed. |
| 1689 | ** |
| 1690 | ** Or if no columns of pTab may be NULL-trimmed, leave P5 at zero. |
| 1691 | */ |
| 1692 | void sqlite3SetMakeRecordP5(Vdbe *v, Table *pTab){ |
| 1693 | u16 i; |
| 1694 | |
| 1695 | /* Records with omitted columns are only allowed for schema format |
| 1696 | ** version 2 and later (SQLite version 3.1.4, 2005-02-20). */ |
| 1697 | if( pTab->pSchema->file_format<2 ) return; |
| 1698 | |
drh | 7e4acf7 | 2017-02-14 20:00:16 +0000 | [diff] [blame] | 1699 | for(i=pTab->nCol-1; i>0; i--){ |
| 1700 | if( pTab->aCol[i].pDflt!=0 ) break; |
| 1701 | if( pTab->aCol[i].colFlags & COLFLAG_PRIMKEY ) break; |
| 1702 | } |
| 1703 | sqlite3VdbeChangeP5(v, i+1); |
drh | 585ce19 | 2017-01-25 14:58:27 +0000 | [diff] [blame] | 1704 | } |
drh | d447dce | 2017-01-25 20:55:11 +0000 | [diff] [blame] | 1705 | #endif |
drh | 585ce19 | 2017-01-25 14:58:27 +0000 | [diff] [blame] | 1706 | |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 1707 | /* |
| 1708 | ** This routine generates code to finish the INSERT or UPDATE operation |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1709 | ** that was started by a prior call to sqlite3GenerateConstraintChecks. |
drh | 6934fc7 | 2013-10-31 15:37:49 +0000 | [diff] [blame] | 1710 | ** A consecutive range of registers starting at regNewData contains the |
drh | 04adf41 | 2008-01-08 18:57:50 +0000 | [diff] [blame] | 1711 | ** rowid and the content to be inserted. |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 1712 | ** |
drh | b419a92 | 2002-01-30 16:17:23 +0000 | [diff] [blame] | 1713 | ** The arguments to this routine should be the same as the first six |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1714 | ** arguments to sqlite3GenerateConstraintChecks. |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 1715 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1716 | void sqlite3CompleteInsertion( |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 1717 | Parse *pParse, /* The parser context */ |
| 1718 | Table *pTab, /* the table into which we are inserting */ |
drh | 26198bb | 2013-10-31 11:15:09 +0000 | [diff] [blame] | 1719 | int iDataCur, /* Cursor of the canonical data source */ |
| 1720 | int iIdxCur, /* First index cursor */ |
drh | 6934fc7 | 2013-10-31 15:37:49 +0000 | [diff] [blame] | 1721 | int regNewData, /* Range of content */ |
drh | aa9b896 | 2008-01-08 02:57:55 +0000 | [diff] [blame] | 1722 | int *aRegIdx, /* Register used by each index. 0 for unused indices */ |
dan | f91c131 | 2017-01-10 20:04:38 +0000 | [diff] [blame] | 1723 | int update_flags, /* True for UPDATE, False for INSERT */ |
danielk1977 | de63035 | 2009-05-04 11:42:29 +0000 | [diff] [blame] | 1724 | int appendBias, /* True if this is likely to be an append */ |
| 1725 | int useSeekResult /* True to set the USESEEKRESULT flag on OP_[Idx]Insert */ |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 1726 | ){ |
drh | 6934fc7 | 2013-10-31 15:37:49 +0000 | [diff] [blame] | 1727 | Vdbe *v; /* Prepared statements under construction */ |
| 1728 | Index *pIdx; /* An index being inserted or updated */ |
| 1729 | u8 pik_flags; /* flag values passed to the btree insert */ |
| 1730 | int regData; /* Content registers (after the rowid) */ |
peter.d.reid | 60ec914 | 2014-09-06 16:39:46 +0000 | [diff] [blame] | 1731 | int regRec; /* Register holding assembled record for the table */ |
drh | 6934fc7 | 2013-10-31 15:37:49 +0000 | [diff] [blame] | 1732 | int i; /* Loop counter */ |
drh | 57bf4a8 | 2014-02-17 14:59:22 +0000 | [diff] [blame] | 1733 | u8 bAffinityDone = 0; /* True if OP_Affinity has been run already */ |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 1734 | |
dan | f91c131 | 2017-01-10 20:04:38 +0000 | [diff] [blame] | 1735 | assert( update_flags==0 |
| 1736 | || update_flags==OPFLAG_ISUPDATE |
| 1737 | || update_flags==(OPFLAG_ISUPDATE|OPFLAG_SAVEPOSITION) |
| 1738 | ); |
| 1739 | |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1740 | v = sqlite3GetVdbe(pParse); |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 1741 | assert( v!=0 ); |
drh | 417be79 | 2002-03-03 18:59:40 +0000 | [diff] [blame] | 1742 | assert( pTab->pSelect==0 ); /* This table is not a VIEW */ |
drh | b2b9d3d | 2013-08-01 01:14:43 +0000 | [diff] [blame] | 1743 | for(i=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){ |
drh | aa9b896 | 2008-01-08 02:57:55 +0000 | [diff] [blame] | 1744 | if( aRegIdx[i]==0 ) continue; |
drh | 57bf4a8 | 2014-02-17 14:59:22 +0000 | [diff] [blame] | 1745 | bAffinityDone = 1; |
drh | b2b9d3d | 2013-08-01 01:14:43 +0000 | [diff] [blame] | 1746 | if( pIdx->pPartIdxWhere ){ |
| 1747 | sqlite3VdbeAddOp2(v, OP_IsNull, aRegIdx[i], sqlite3VdbeCurrentAddr(v)+2); |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 1748 | VdbeCoverage(v); |
drh | b2b9d3d | 2013-08-01 01:14:43 +0000 | [diff] [blame] | 1749 | } |
dan | cb9a364 | 2017-01-30 19:44:53 +0000 | [diff] [blame] | 1750 | pik_flags = (useSeekResult ? OPFLAG_USESEEKRESULT : 0); |
drh | 48dd1d8 | 2014-05-27 18:18:58 +0000 | [diff] [blame] | 1751 | if( IsPrimaryKeyIndex(pIdx) && !HasRowid(pTab) ){ |
drh | 4308e34 | 2013-11-11 16:55:52 +0000 | [diff] [blame] | 1752 | assert( pParse->nested==0 ); |
drh | 6546af1 | 2013-11-04 15:23:25 +0000 | [diff] [blame] | 1753 | pik_flags |= OPFLAG_NCHANGE; |
dan | f91c131 | 2017-01-10 20:04:38 +0000 | [diff] [blame] | 1754 | pik_flags |= (update_flags & OPFLAG_SAVEPOSITION); |
dan | cb9a364 | 2017-01-30 19:44:53 +0000 | [diff] [blame] | 1755 | #ifdef SQLITE_ENABLE_PREUPDATE_HOOK |
| 1756 | if( update_flags==0 ){ |
| 1757 | sqlite3VdbeAddOp4(v, OP_InsertInt, |
| 1758 | iIdxCur+i, aRegIdx[i], 0, (char*)pTab, P4_TABLE |
| 1759 | ); |
| 1760 | sqlite3VdbeChangeP5(v, OPFLAG_ISNOOP); |
| 1761 | } |
| 1762 | #endif |
danielk1977 | de63035 | 2009-05-04 11:42:29 +0000 | [diff] [blame] | 1763 | } |
dan | cb9a364 | 2017-01-30 19:44:53 +0000 | [diff] [blame] | 1764 | sqlite3VdbeAddOp4Int(v, OP_IdxInsert, iIdxCur+i, aRegIdx[i], |
| 1765 | aRegIdx[i]+1, |
| 1766 | pIdx->uniqNotNull ? pIdx->nKeyCol: pIdx->nColumn); |
drh | 9b34abe | 2016-01-16 15:12:35 +0000 | [diff] [blame] | 1767 | sqlite3VdbeChangeP5(v, pik_flags); |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 1768 | } |
drh | ec95c44 | 2013-10-23 01:57:32 +0000 | [diff] [blame] | 1769 | if( !HasRowid(pTab) ) return; |
drh | 6934fc7 | 2013-10-31 15:37:49 +0000 | [diff] [blame] | 1770 | regData = regNewData + 1; |
drh | b765411 | 2008-01-12 12:48:07 +0000 | [diff] [blame] | 1771 | regRec = sqlite3GetTempReg(pParse); |
drh | 1db639c | 2008-01-17 02:36:28 +0000 | [diff] [blame] | 1772 | sqlite3VdbeAddOp3(v, OP_MakeRecord, regData, pTab->nCol, regRec); |
drh | 585ce19 | 2017-01-25 14:58:27 +0000 | [diff] [blame] | 1773 | sqlite3SetMakeRecordP5(v, pTab); |
drh | 2adb878 | 2016-11-14 15:28:56 +0000 | [diff] [blame] | 1774 | if( !bAffinityDone ){ |
| 1775 | sqlite3TableAffinity(v, pTab, 0); |
| 1776 | sqlite3ExprCacheAffinityChange(pParse, regData, pTab->nCol); |
| 1777 | } |
drh | 4794f73 | 2004-11-05 17:17:50 +0000 | [diff] [blame] | 1778 | if( pParse->nested ){ |
| 1779 | pik_flags = 0; |
| 1780 | }else{ |
danielk1977 | 94eb6a1 | 2005-12-15 15:22:08 +0000 | [diff] [blame] | 1781 | pik_flags = OPFLAG_NCHANGE; |
dan | f91c131 | 2017-01-10 20:04:38 +0000 | [diff] [blame] | 1782 | pik_flags |= (update_flags?update_flags:OPFLAG_LASTROWID); |
drh | 4794f73 | 2004-11-05 17:17:50 +0000 | [diff] [blame] | 1783 | } |
drh | e4d9081 | 2007-03-29 05:51:49 +0000 | [diff] [blame] | 1784 | if( appendBias ){ |
| 1785 | pik_flags |= OPFLAG_APPEND; |
| 1786 | } |
danielk1977 | de63035 | 2009-05-04 11:42:29 +0000 | [diff] [blame] | 1787 | if( useSeekResult ){ |
| 1788 | pik_flags |= OPFLAG_USESEEKRESULT; |
| 1789 | } |
drh | 6934fc7 | 2013-10-31 15:37:49 +0000 | [diff] [blame] | 1790 | sqlite3VdbeAddOp3(v, OP_Insert, iDataCur, regRec, regNewData); |
danielk1977 | 94eb6a1 | 2005-12-15 15:22:08 +0000 | [diff] [blame] | 1791 | if( !pParse->nested ){ |
drh | f14b7fb | 2016-12-07 21:35:55 +0000 | [diff] [blame] | 1792 | sqlite3VdbeAppendP4(v, pTab, P4_TABLE); |
danielk1977 | 94eb6a1 | 2005-12-15 15:22:08 +0000 | [diff] [blame] | 1793 | } |
drh | b765411 | 2008-01-12 12:48:07 +0000 | [diff] [blame] | 1794 | sqlite3VdbeChangeP5(v, pik_flags); |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 1795 | } |
drh | cd44690 | 2004-02-24 01:05:31 +0000 | [diff] [blame] | 1796 | |
| 1797 | /* |
drh | 26198bb | 2013-10-31 11:15:09 +0000 | [diff] [blame] | 1798 | ** Allocate cursors for the pTab table and all its indices and generate |
| 1799 | ** code to open and initialized those cursors. |
drh | aa9b896 | 2008-01-08 02:57:55 +0000 | [diff] [blame] | 1800 | ** |
drh | 26198bb | 2013-10-31 11:15:09 +0000 | [diff] [blame] | 1801 | ** The cursor for the object that contains the complete data (normally |
| 1802 | ** the table itself, but the PRIMARY KEY index in the case of a WITHOUT |
| 1803 | ** ROWID table) is returned in *piDataCur. The first index cursor is |
| 1804 | ** returned in *piIdxCur. The number of indices is returned. |
| 1805 | ** |
| 1806 | ** Use iBase as the first cursor (either the *piDataCur for rowid tables |
| 1807 | ** or the first index for WITHOUT ROWID tables) if it is non-negative. |
| 1808 | ** If iBase is negative, then allocate the next available cursor. |
| 1809 | ** |
| 1810 | ** For a rowid table, *piDataCur will be exactly one less than *piIdxCur. |
| 1811 | ** For a WITHOUT ROWID table, *piDataCur will be somewhere in the range |
| 1812 | ** of *piIdxCurs, depending on where the PRIMARY KEY index appears on the |
| 1813 | ** pTab->pIndex list. |
drh | b6b4b79 | 2014-08-21 14:10:23 +0000 | [diff] [blame] | 1814 | ** |
| 1815 | ** If pTab is a virtual table, then this routine is a no-op and the |
| 1816 | ** *piDataCur and *piIdxCur values are left uninitialized. |
drh | cd44690 | 2004-02-24 01:05:31 +0000 | [diff] [blame] | 1817 | */ |
drh | aa9b896 | 2008-01-08 02:57:55 +0000 | [diff] [blame] | 1818 | int sqlite3OpenTableAndIndices( |
drh | 290c194 | 2004-08-21 17:54:45 +0000 | [diff] [blame] | 1819 | Parse *pParse, /* Parsing context */ |
| 1820 | Table *pTab, /* Table to be opened */ |
drh | 26198bb | 2013-10-31 11:15:09 +0000 | [diff] [blame] | 1821 | int op, /* OP_OpenRead or OP_OpenWrite */ |
drh | b89aeb6 | 2016-01-27 15:49:32 +0000 | [diff] [blame] | 1822 | u8 p5, /* P5 value for OP_Open* opcodes (except on WITHOUT ROWID) */ |
drh | 26198bb | 2013-10-31 11:15:09 +0000 | [diff] [blame] | 1823 | int iBase, /* Use this for the table cursor, if there is one */ |
drh | 6a53499 | 2013-11-16 20:13:39 +0000 | [diff] [blame] | 1824 | u8 *aToOpen, /* If not NULL: boolean for each table and index */ |
drh | 26198bb | 2013-10-31 11:15:09 +0000 | [diff] [blame] | 1825 | int *piDataCur, /* Write the database source cursor number here */ |
| 1826 | int *piIdxCur /* Write the first index cursor number here */ |
drh | 290c194 | 2004-08-21 17:54:45 +0000 | [diff] [blame] | 1827 | ){ |
drh | cd44690 | 2004-02-24 01:05:31 +0000 | [diff] [blame] | 1828 | int i; |
drh | 4cbdda9 | 2006-06-14 19:00:20 +0000 | [diff] [blame] | 1829 | int iDb; |
drh | 6a53499 | 2013-11-16 20:13:39 +0000 | [diff] [blame] | 1830 | int iDataCur; |
drh | cd44690 | 2004-02-24 01:05:31 +0000 | [diff] [blame] | 1831 | Index *pIdx; |
drh | 4cbdda9 | 2006-06-14 19:00:20 +0000 | [diff] [blame] | 1832 | Vdbe *v; |
| 1833 | |
drh | 26198bb | 2013-10-31 11:15:09 +0000 | [diff] [blame] | 1834 | assert( op==OP_OpenRead || op==OP_OpenWrite ); |
dan | fd261ec | 2015-10-22 20:54:33 +0000 | [diff] [blame] | 1835 | assert( op==OP_OpenWrite || p5==0 ); |
drh | 26198bb | 2013-10-31 11:15:09 +0000 | [diff] [blame] | 1836 | if( IsVirtual(pTab) ){ |
drh | b6b4b79 | 2014-08-21 14:10:23 +0000 | [diff] [blame] | 1837 | /* This routine is a no-op for virtual tables. Leave the output |
| 1838 | ** variables *piDataCur and *piIdxCur uninitialized so that valgrind |
| 1839 | ** can detect if they are used by mistake in the caller. */ |
drh | 26198bb | 2013-10-31 11:15:09 +0000 | [diff] [blame] | 1840 | return 0; |
| 1841 | } |
drh | 4cbdda9 | 2006-06-14 19:00:20 +0000 | [diff] [blame] | 1842 | iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema); |
| 1843 | v = sqlite3GetVdbe(pParse); |
drh | cd44690 | 2004-02-24 01:05:31 +0000 | [diff] [blame] | 1844 | assert( v!=0 ); |
drh | 26198bb | 2013-10-31 11:15:09 +0000 | [diff] [blame] | 1845 | if( iBase<0 ) iBase = pParse->nTab; |
drh | 6a53499 | 2013-11-16 20:13:39 +0000 | [diff] [blame] | 1846 | iDataCur = iBase++; |
| 1847 | if( piDataCur ) *piDataCur = iDataCur; |
| 1848 | if( HasRowid(pTab) && (aToOpen==0 || aToOpen[0]) ){ |
| 1849 | sqlite3OpenTable(pParse, iDataCur, iDb, pTab, op); |
drh | 6fbe41a | 2013-10-30 20:22:55 +0000 | [diff] [blame] | 1850 | }else{ |
drh | 26198bb | 2013-10-31 11:15:09 +0000 | [diff] [blame] | 1851 | sqlite3TableLock(pParse, iDb, pTab->tnum, op==OP_OpenWrite, pTab->zName); |
drh | 6fbe41a | 2013-10-30 20:22:55 +0000 | [diff] [blame] | 1852 | } |
drh | 6a53499 | 2013-11-16 20:13:39 +0000 | [diff] [blame] | 1853 | if( piIdxCur ) *piIdxCur = iBase; |
drh | 26198bb | 2013-10-31 11:15:09 +0000 | [diff] [blame] | 1854 | for(i=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){ |
drh | 26198bb | 2013-10-31 11:15:09 +0000 | [diff] [blame] | 1855 | int iIdxCur = iBase++; |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 1856 | assert( pIdx->pSchema==pTab->pSchema ); |
dan | 61441c3 | 2016-08-26 12:00:50 +0000 | [diff] [blame] | 1857 | if( IsPrimaryKeyIndex(pIdx) && !HasRowid(pTab) ){ |
| 1858 | if( piDataCur ) *piDataCur = iIdxCur; |
| 1859 | p5 = 0; |
| 1860 | } |
drh | 6a53499 | 2013-11-16 20:13:39 +0000 | [diff] [blame] | 1861 | if( aToOpen==0 || aToOpen[i+1] ){ |
| 1862 | sqlite3VdbeAddOp3(v, op, iIdxCur, pIdx->tnum, iDb); |
| 1863 | sqlite3VdbeSetP4KeyInfo(pParse, pIdx); |
drh | b89aeb6 | 2016-01-27 15:49:32 +0000 | [diff] [blame] | 1864 | sqlite3VdbeChangeP5(v, p5); |
dan | 61441c3 | 2016-08-26 12:00:50 +0000 | [diff] [blame] | 1865 | VdbeComment((v, "%s", pIdx->zName)); |
drh | b89aeb6 | 2016-01-27 15:49:32 +0000 | [diff] [blame] | 1866 | } |
drh | cd44690 | 2004-02-24 01:05:31 +0000 | [diff] [blame] | 1867 | } |
drh | 26198bb | 2013-10-31 11:15:09 +0000 | [diff] [blame] | 1868 | if( iBase>pParse->nTab ) pParse->nTab = iBase; |
| 1869 | return i; |
drh | cd44690 | 2004-02-24 01:05:31 +0000 | [diff] [blame] | 1870 | } |
drh | 9d9cf22 | 2007-02-13 15:01:11 +0000 | [diff] [blame] | 1871 | |
drh | 91c58e2 | 2007-03-27 12:04:04 +0000 | [diff] [blame] | 1872 | |
| 1873 | #ifdef SQLITE_TEST |
| 1874 | /* |
| 1875 | ** The following global variable is incremented whenever the |
| 1876 | ** transfer optimization is used. This is used for testing |
| 1877 | ** purposes only - to make sure the transfer optimization really |
peter.d.reid | 60ec914 | 2014-09-06 16:39:46 +0000 | [diff] [blame] | 1878 | ** is happening when it is supposed to. |
drh | 91c58e2 | 2007-03-27 12:04:04 +0000 | [diff] [blame] | 1879 | */ |
| 1880 | int sqlite3_xferopt_count; |
| 1881 | #endif /* SQLITE_TEST */ |
| 1882 | |
| 1883 | |
drh | 9d9cf22 | 2007-02-13 15:01:11 +0000 | [diff] [blame] | 1884 | #ifndef SQLITE_OMIT_XFER_OPT |
| 1885 | /* |
drh | 9d9cf22 | 2007-02-13 15:01:11 +0000 | [diff] [blame] | 1886 | ** Check to see if index pSrc is compatible as a source of data |
| 1887 | ** for index pDest in an insert transfer optimization. The rules |
| 1888 | ** for a compatible index: |
| 1889 | ** |
| 1890 | ** * The index is over the same set of columns |
| 1891 | ** * The same DESC and ASC markings occurs on all columns |
| 1892 | ** * The same onError processing (OE_Abort, OE_Ignore, etc) |
| 1893 | ** * The same collating sequence on each column |
drh | b2b9d3d | 2013-08-01 01:14:43 +0000 | [diff] [blame] | 1894 | ** * The index has the exact same WHERE clause |
drh | 9d9cf22 | 2007-02-13 15:01:11 +0000 | [diff] [blame] | 1895 | */ |
| 1896 | static int xferCompatibleIndex(Index *pDest, Index *pSrc){ |
| 1897 | int i; |
| 1898 | assert( pDest && pSrc ); |
| 1899 | assert( pDest->pTable!=pSrc->pTable ); |
drh | bbbdc83 | 2013-10-22 18:01:40 +0000 | [diff] [blame] | 1900 | if( pDest->nKeyCol!=pSrc->nKeyCol ){ |
drh | 9d9cf22 | 2007-02-13 15:01:11 +0000 | [diff] [blame] | 1901 | return 0; /* Different number of columns */ |
| 1902 | } |
| 1903 | if( pDest->onError!=pSrc->onError ){ |
| 1904 | return 0; /* Different conflict resolution strategies */ |
| 1905 | } |
drh | bbbdc83 | 2013-10-22 18:01:40 +0000 | [diff] [blame] | 1906 | for(i=0; i<pSrc->nKeyCol; i++){ |
drh | 9d9cf22 | 2007-02-13 15:01:11 +0000 | [diff] [blame] | 1907 | if( pSrc->aiColumn[i]!=pDest->aiColumn[i] ){ |
| 1908 | return 0; /* Different columns indexed */ |
| 1909 | } |
drh | 4b92f98 | 2015-09-29 17:20:14 +0000 | [diff] [blame] | 1910 | if( pSrc->aiColumn[i]==XN_EXPR ){ |
drh | 1f9ca2c | 2015-08-25 16:57:52 +0000 | [diff] [blame] | 1911 | assert( pSrc->aColExpr!=0 && pDest->aColExpr!=0 ); |
dan | 5aa550c | 2017-06-24 18:10:29 +0000 | [diff] [blame] | 1912 | if( sqlite3ExprCompare(0, pSrc->aColExpr->a[i].pExpr, |
drh | 1f9ca2c | 2015-08-25 16:57:52 +0000 | [diff] [blame] | 1913 | pDest->aColExpr->a[i].pExpr, -1)!=0 ){ |
| 1914 | return 0; /* Different expressions in the index */ |
| 1915 | } |
| 1916 | } |
drh | 9d9cf22 | 2007-02-13 15:01:11 +0000 | [diff] [blame] | 1917 | if( pSrc->aSortOrder[i]!=pDest->aSortOrder[i] ){ |
| 1918 | return 0; /* Different sort orders */ |
| 1919 | } |
drh | 0472af9 | 2015-12-30 15:18:16 +0000 | [diff] [blame] | 1920 | if( sqlite3_stricmp(pSrc->azColl[i],pDest->azColl[i])!=0 ){ |
drh | 60a713c | 2008-01-21 16:22:45 +0000 | [diff] [blame] | 1921 | return 0; /* Different collating sequences */ |
drh | 9d9cf22 | 2007-02-13 15:01:11 +0000 | [diff] [blame] | 1922 | } |
| 1923 | } |
dan | 5aa550c | 2017-06-24 18:10:29 +0000 | [diff] [blame] | 1924 | if( sqlite3ExprCompare(0, pSrc->pPartIdxWhere, pDest->pPartIdxWhere, -1) ){ |
drh | b2b9d3d | 2013-08-01 01:14:43 +0000 | [diff] [blame] | 1925 | return 0; /* Different WHERE clauses */ |
| 1926 | } |
drh | 9d9cf22 | 2007-02-13 15:01:11 +0000 | [diff] [blame] | 1927 | |
| 1928 | /* If no test above fails then the indices must be compatible */ |
| 1929 | return 1; |
| 1930 | } |
| 1931 | |
| 1932 | /* |
| 1933 | ** Attempt the transfer optimization on INSERTs of the form |
| 1934 | ** |
| 1935 | ** INSERT INTO tab1 SELECT * FROM tab2; |
| 1936 | ** |
drh | ccdf1ba | 2011-11-04 14:36:02 +0000 | [diff] [blame] | 1937 | ** The xfer optimization transfers raw records from tab2 over to tab1. |
peter.d.reid | 60ec914 | 2014-09-06 16:39:46 +0000 | [diff] [blame] | 1938 | ** Columns are not decoded and reassembled, which greatly improves |
drh | ccdf1ba | 2011-11-04 14:36:02 +0000 | [diff] [blame] | 1939 | ** performance. Raw index records are transferred in the same way. |
drh | 9d9cf22 | 2007-02-13 15:01:11 +0000 | [diff] [blame] | 1940 | ** |
drh | ccdf1ba | 2011-11-04 14:36:02 +0000 | [diff] [blame] | 1941 | ** The xfer optimization is only attempted if tab1 and tab2 are compatible. |
| 1942 | ** There are lots of rules for determining compatibility - see comments |
| 1943 | ** embedded in the code for details. |
drh | 9d9cf22 | 2007-02-13 15:01:11 +0000 | [diff] [blame] | 1944 | ** |
drh | ccdf1ba | 2011-11-04 14:36:02 +0000 | [diff] [blame] | 1945 | ** This routine returns TRUE if the optimization is guaranteed to be used. |
| 1946 | ** Sometimes the xfer optimization will only work if the destination table |
| 1947 | ** is empty - a factor that can only be determined at run-time. In that |
| 1948 | ** case, this routine generates code for the xfer optimization but also |
| 1949 | ** does a test to see if the destination table is empty and jumps over the |
| 1950 | ** xfer optimization code if the test fails. In that case, this routine |
| 1951 | ** returns FALSE so that the caller will know to go ahead and generate |
| 1952 | ** an unoptimized transfer. This routine also returns FALSE if there |
| 1953 | ** is no chance that the xfer optimization can be applied. |
drh | 9d9cf22 | 2007-02-13 15:01:11 +0000 | [diff] [blame] | 1954 | ** |
drh | ccdf1ba | 2011-11-04 14:36:02 +0000 | [diff] [blame] | 1955 | ** This optimization is particularly useful at making VACUUM run faster. |
drh | 9d9cf22 | 2007-02-13 15:01:11 +0000 | [diff] [blame] | 1956 | */ |
| 1957 | static int xferOptimization( |
| 1958 | Parse *pParse, /* Parser context */ |
| 1959 | Table *pDest, /* The table we are inserting into */ |
| 1960 | Select *pSelect, /* A SELECT statement to use as the data source */ |
| 1961 | int onError, /* How to handle constraint errors */ |
| 1962 | int iDbDest /* The database of pDest */ |
| 1963 | ){ |
dan | e34162b | 2015-04-01 18:20:25 +0000 | [diff] [blame] | 1964 | sqlite3 *db = pParse->db; |
drh | 9d9cf22 | 2007-02-13 15:01:11 +0000 | [diff] [blame] | 1965 | ExprList *pEList; /* The result set of the SELECT */ |
| 1966 | Table *pSrc; /* The table in the FROM clause of SELECT */ |
| 1967 | Index *pSrcIdx, *pDestIdx; /* Source and destination indices */ |
| 1968 | struct SrcList_item *pItem; /* An element of pSelect->pSrc */ |
| 1969 | int i; /* Loop counter */ |
| 1970 | int iDbSrc; /* The database of pSrc */ |
| 1971 | int iSrc, iDest; /* Cursors from source and destination */ |
| 1972 | int addr1, addr2; /* Loop addresses */ |
drh | da475b8 | 2013-11-04 21:44:54 +0000 | [diff] [blame] | 1973 | int emptyDestTest = 0; /* Address of test for empty pDest */ |
| 1974 | int emptySrcTest = 0; /* Address of test for empty pSrc */ |
drh | 9d9cf22 | 2007-02-13 15:01:11 +0000 | [diff] [blame] | 1975 | Vdbe *v; /* The VDBE we are building */ |
drh | 6a288a3 | 2008-01-07 19:20:24 +0000 | [diff] [blame] | 1976 | int regAutoinc; /* Memory register used by AUTOINC */ |
drh | f33c9fa | 2007-04-10 18:17:55 +0000 | [diff] [blame] | 1977 | int destHasUniqueIdx = 0; /* True if pDest has a UNIQUE index */ |
drh | b765411 | 2008-01-12 12:48:07 +0000 | [diff] [blame] | 1978 | int regData, regRowid; /* Registers holding data and rowid */ |
drh | 9d9cf22 | 2007-02-13 15:01:11 +0000 | [diff] [blame] | 1979 | |
| 1980 | if( pSelect==0 ){ |
| 1981 | return 0; /* Must be of the form INSERT INTO ... SELECT ... */ |
| 1982 | } |
dan | ebbf08a | 2014-01-18 08:27:02 +0000 | [diff] [blame] | 1983 | if( pParse->pWith || pSelect->pWith ){ |
| 1984 | /* Do not attempt to process this query if there are an WITH clauses |
| 1985 | ** attached to it. Proceeding may generate a false "no such table: xxx" |
| 1986 | ** error if pSelect reads from a CTE named "xxx". */ |
| 1987 | return 0; |
| 1988 | } |
danielk1977 | 2f886d1 | 2009-02-28 10:47:41 +0000 | [diff] [blame] | 1989 | if( sqlite3TriggerList(pParse, pDest) ){ |
drh | 9d9cf22 | 2007-02-13 15:01:11 +0000 | [diff] [blame] | 1990 | return 0; /* tab1 must not have triggers */ |
| 1991 | } |
| 1992 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
drh | 44266ec | 2017-02-16 14:48:08 +0000 | [diff] [blame] | 1993 | if( IsVirtual(pDest) ){ |
drh | 9d9cf22 | 2007-02-13 15:01:11 +0000 | [diff] [blame] | 1994 | return 0; /* tab1 must not be a virtual table */ |
| 1995 | } |
| 1996 | #endif |
| 1997 | if( onError==OE_Default ){ |
drh | e7224a0 | 2011-11-04 00:23:53 +0000 | [diff] [blame] | 1998 | if( pDest->iPKey>=0 ) onError = pDest->keyConf; |
| 1999 | if( onError==OE_Default ) onError = OE_Abort; |
drh | 9d9cf22 | 2007-02-13 15:01:11 +0000 | [diff] [blame] | 2000 | } |
danielk1977 | 5ce240a | 2007-09-03 17:30:06 +0000 | [diff] [blame] | 2001 | assert(pSelect->pSrc); /* allocated even if there is no FROM clause */ |
drh | 9d9cf22 | 2007-02-13 15:01:11 +0000 | [diff] [blame] | 2002 | if( pSelect->pSrc->nSrc!=1 ){ |
| 2003 | return 0; /* FROM clause must have exactly one term */ |
| 2004 | } |
| 2005 | if( pSelect->pSrc->a[0].pSelect ){ |
| 2006 | return 0; /* FROM clause cannot contain a subquery */ |
| 2007 | } |
| 2008 | if( pSelect->pWhere ){ |
| 2009 | return 0; /* SELECT may not have a WHERE clause */ |
| 2010 | } |
| 2011 | if( pSelect->pOrderBy ){ |
| 2012 | return 0; /* SELECT may not have an ORDER BY clause */ |
| 2013 | } |
drh | 8103b7d | 2007-02-24 13:23:51 +0000 | [diff] [blame] | 2014 | /* Do not need to test for a HAVING clause. If HAVING is present but |
| 2015 | ** there is no ORDER BY, we will get an error. */ |
drh | 9d9cf22 | 2007-02-13 15:01:11 +0000 | [diff] [blame] | 2016 | if( pSelect->pGroupBy ){ |
| 2017 | return 0; /* SELECT may not have a GROUP BY clause */ |
| 2018 | } |
| 2019 | if( pSelect->pLimit ){ |
| 2020 | return 0; /* SELECT may not have a LIMIT clause */ |
| 2021 | } |
drh | 9d9cf22 | 2007-02-13 15:01:11 +0000 | [diff] [blame] | 2022 | if( pSelect->pPrior ){ |
| 2023 | return 0; /* SELECT may not be a compound query */ |
| 2024 | } |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 2025 | if( pSelect->selFlags & SF_Distinct ){ |
drh | 9d9cf22 | 2007-02-13 15:01:11 +0000 | [diff] [blame] | 2026 | return 0; /* SELECT may not be DISTINCT */ |
| 2027 | } |
| 2028 | pEList = pSelect->pEList; |
| 2029 | assert( pEList!=0 ); |
| 2030 | if( pEList->nExpr!=1 ){ |
| 2031 | return 0; /* The result set must have exactly one column */ |
| 2032 | } |
| 2033 | assert( pEList->a[0].pExpr ); |
drh | 1a1d3cd | 2015-11-19 16:33:31 +0000 | [diff] [blame] | 2034 | if( pEList->a[0].pExpr->op!=TK_ASTERISK ){ |
drh | 9d9cf22 | 2007-02-13 15:01:11 +0000 | [diff] [blame] | 2035 | return 0; /* The result set must be the special operator "*" */ |
| 2036 | } |
| 2037 | |
| 2038 | /* At this point we have established that the statement is of the |
| 2039 | ** correct syntactic form to participate in this optimization. Now |
| 2040 | ** we have to check the semantics. |
| 2041 | */ |
| 2042 | pItem = pSelect->pSrc->a; |
dan | 41fb5cd | 2012-10-04 19:33:00 +0000 | [diff] [blame] | 2043 | pSrc = sqlite3LocateTableItem(pParse, 0, pItem); |
drh | 9d9cf22 | 2007-02-13 15:01:11 +0000 | [diff] [blame] | 2044 | if( pSrc==0 ){ |
| 2045 | return 0; /* FROM clause does not contain a real table */ |
| 2046 | } |
| 2047 | if( pSrc==pDest ){ |
| 2048 | return 0; /* tab1 and tab2 may not be the same table */ |
| 2049 | } |
drh | 5554827 | 2013-10-23 16:03:07 +0000 | [diff] [blame] | 2050 | if( HasRowid(pDest)!=HasRowid(pSrc) ){ |
| 2051 | return 0; /* source and destination must both be WITHOUT ROWID or not */ |
| 2052 | } |
drh | 9d9cf22 | 2007-02-13 15:01:11 +0000 | [diff] [blame] | 2053 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
drh | 44266ec | 2017-02-16 14:48:08 +0000 | [diff] [blame] | 2054 | if( IsVirtual(pSrc) ){ |
drh | 9d9cf22 | 2007-02-13 15:01:11 +0000 | [diff] [blame] | 2055 | return 0; /* tab2 must not be a virtual table */ |
| 2056 | } |
| 2057 | #endif |
| 2058 | if( pSrc->pSelect ){ |
| 2059 | return 0; /* tab2 may not be a view */ |
| 2060 | } |
| 2061 | if( pDest->nCol!=pSrc->nCol ){ |
| 2062 | return 0; /* Number of columns must be the same in tab1 and tab2 */ |
| 2063 | } |
| 2064 | if( pDest->iPKey!=pSrc->iPKey ){ |
| 2065 | return 0; /* Both tables must have the same INTEGER PRIMARY KEY */ |
| 2066 | } |
| 2067 | for(i=0; i<pDest->nCol; i++){ |
dan | 9940e2a | 2014-04-26 14:07:57 +0000 | [diff] [blame] | 2068 | Column *pDestCol = &pDest->aCol[i]; |
| 2069 | Column *pSrcCol = &pSrc->aCol[i]; |
dan | ba68f8f | 2015-11-19 16:46:46 +0000 | [diff] [blame] | 2070 | #ifdef SQLITE_ENABLE_HIDDEN_COLUMNS |
drh | 8257aa8 | 2017-07-26 19:59:13 +0000 | [diff] [blame] | 2071 | if( (db->mDbFlags & DBFLAG_Vacuum)==0 |
dan | aaea314 | 2015-11-19 18:09:05 +0000 | [diff] [blame] | 2072 | && (pDestCol->colFlags | pSrcCol->colFlags) & COLFLAG_HIDDEN |
| 2073 | ){ |
dan | ba68f8f | 2015-11-19 16:46:46 +0000 | [diff] [blame] | 2074 | return 0; /* Neither table may have __hidden__ columns */ |
| 2075 | } |
| 2076 | #endif |
dan | 9940e2a | 2014-04-26 14:07:57 +0000 | [diff] [blame] | 2077 | if( pDestCol->affinity!=pSrcCol->affinity ){ |
drh | 9d9cf22 | 2007-02-13 15:01:11 +0000 | [diff] [blame] | 2078 | return 0; /* Affinity must be the same on all columns */ |
| 2079 | } |
drh | 0472af9 | 2015-12-30 15:18:16 +0000 | [diff] [blame] | 2080 | if( sqlite3_stricmp(pDestCol->zColl, pSrcCol->zColl)!=0 ){ |
drh | 9d9cf22 | 2007-02-13 15:01:11 +0000 | [diff] [blame] | 2081 | return 0; /* Collating sequence must be the same on all columns */ |
| 2082 | } |
dan | 9940e2a | 2014-04-26 14:07:57 +0000 | [diff] [blame] | 2083 | if( pDestCol->notNull && !pSrcCol->notNull ){ |
drh | 9d9cf22 | 2007-02-13 15:01:11 +0000 | [diff] [blame] | 2084 | return 0; /* tab2 must be NOT NULL if tab1 is */ |
| 2085 | } |
drh | 453e026 | 2014-04-26 17:52:08 +0000 | [diff] [blame] | 2086 | /* Default values for second and subsequent columns need to match. */ |
drh | 94fa9c4 | 2016-02-27 21:16:04 +0000 | [diff] [blame] | 2087 | if( i>0 ){ |
| 2088 | assert( pDestCol->pDflt==0 || pDestCol->pDflt->op==TK_SPAN ); |
| 2089 | assert( pSrcCol->pDflt==0 || pSrcCol->pDflt->op==TK_SPAN ); |
| 2090 | if( (pDestCol->pDflt==0)!=(pSrcCol->pDflt==0) |
| 2091 | || (pDestCol->pDflt && strcmp(pDestCol->pDflt->u.zToken, |
| 2092 | pSrcCol->pDflt->u.zToken)!=0) |
| 2093 | ){ |
| 2094 | return 0; /* Default values must be the same for all columns */ |
| 2095 | } |
dan | 9940e2a | 2014-04-26 14:07:57 +0000 | [diff] [blame] | 2096 | } |
drh | 9d9cf22 | 2007-02-13 15:01:11 +0000 | [diff] [blame] | 2097 | } |
| 2098 | for(pDestIdx=pDest->pIndex; pDestIdx; pDestIdx=pDestIdx->pNext){ |
drh | 5f1d1d9 | 2014-07-31 22:59:04 +0000 | [diff] [blame] | 2099 | if( IsUniqueIndex(pDestIdx) ){ |
drh | f33c9fa | 2007-04-10 18:17:55 +0000 | [diff] [blame] | 2100 | destHasUniqueIdx = 1; |
| 2101 | } |
drh | 9d9cf22 | 2007-02-13 15:01:11 +0000 | [diff] [blame] | 2102 | for(pSrcIdx=pSrc->pIndex; pSrcIdx; pSrcIdx=pSrcIdx->pNext){ |
| 2103 | if( xferCompatibleIndex(pDestIdx, pSrcIdx) ) break; |
| 2104 | } |
| 2105 | if( pSrcIdx==0 ){ |
| 2106 | return 0; /* pDestIdx has no corresponding index in pSrc */ |
| 2107 | } |
| 2108 | } |
drh | 7fc2f41 | 2007-03-29 00:08:24 +0000 | [diff] [blame] | 2109 | #ifndef SQLITE_OMIT_CHECK |
drh | 619a130 | 2013-08-01 13:04:46 +0000 | [diff] [blame] | 2110 | if( pDest->pCheck && sqlite3ExprListCompare(pSrc->pCheck,pDest->pCheck,-1) ){ |
drh | 8103b7d | 2007-02-24 13:23:51 +0000 | [diff] [blame] | 2111 | return 0; /* Tables have different CHECK constraints. Ticket #2252 */ |
| 2112 | } |
drh | 7fc2f41 | 2007-03-29 00:08:24 +0000 | [diff] [blame] | 2113 | #endif |
drh | 713de34 | 2011-04-24 22:56:07 +0000 | [diff] [blame] | 2114 | #ifndef SQLITE_OMIT_FOREIGN_KEY |
| 2115 | /* Disallow the transfer optimization if the destination table constains |
| 2116 | ** any foreign key constraints. This is more restrictive than necessary. |
| 2117 | ** But the main beneficiary of the transfer optimization is the VACUUM |
| 2118 | ** command, and the VACUUM command disables foreign key constraints. So |
| 2119 | ** the extra complication to make this rule less restrictive is probably |
| 2120 | ** not worth the effort. Ticket [6284df89debdfa61db8073e062908af0c9b6118e] |
| 2121 | */ |
dan | e34162b | 2015-04-01 18:20:25 +0000 | [diff] [blame] | 2122 | if( (db->flags & SQLITE_ForeignKeys)!=0 && pDest->pFKey!=0 ){ |
drh | 713de34 | 2011-04-24 22:56:07 +0000 | [diff] [blame] | 2123 | return 0; |
| 2124 | } |
| 2125 | #endif |
dan | e34162b | 2015-04-01 18:20:25 +0000 | [diff] [blame] | 2126 | if( (db->flags & SQLITE_CountRows)!=0 ){ |
drh | ccdf1ba | 2011-11-04 14:36:02 +0000 | [diff] [blame] | 2127 | return 0; /* xfer opt does not play well with PRAGMA count_changes */ |
dan | 1696124 | 2011-09-30 12:01:01 +0000 | [diff] [blame] | 2128 | } |
drh | 9d9cf22 | 2007-02-13 15:01:11 +0000 | [diff] [blame] | 2129 | |
drh | ccdf1ba | 2011-11-04 14:36:02 +0000 | [diff] [blame] | 2130 | /* If we get this far, it means that the xfer optimization is at |
| 2131 | ** least a possibility, though it might only work if the destination |
| 2132 | ** table (tab1) is initially empty. |
drh | 9d9cf22 | 2007-02-13 15:01:11 +0000 | [diff] [blame] | 2133 | */ |
drh | dd73521 | 2007-02-24 13:53:05 +0000 | [diff] [blame] | 2134 | #ifdef SQLITE_TEST |
| 2135 | sqlite3_xferopt_count++; |
| 2136 | #endif |
dan | e34162b | 2015-04-01 18:20:25 +0000 | [diff] [blame] | 2137 | iDbSrc = sqlite3SchemaToIndex(db, pSrc->pSchema); |
drh | 9d9cf22 | 2007-02-13 15:01:11 +0000 | [diff] [blame] | 2138 | v = sqlite3GetVdbe(pParse); |
drh | f53e9b5 | 2007-08-29 13:45:58 +0000 | [diff] [blame] | 2139 | sqlite3CodeVerifySchema(pParse, iDbSrc); |
drh | 9d9cf22 | 2007-02-13 15:01:11 +0000 | [diff] [blame] | 2140 | iSrc = pParse->nTab++; |
| 2141 | iDest = pParse->nTab++; |
drh | 6a288a3 | 2008-01-07 19:20:24 +0000 | [diff] [blame] | 2142 | regAutoinc = autoIncBegin(pParse, iDbDest, pDest); |
drh | b765411 | 2008-01-12 12:48:07 +0000 | [diff] [blame] | 2143 | regData = sqlite3GetTempReg(pParse); |
| 2144 | regRowid = sqlite3GetTempReg(pParse); |
drh | 9d9cf22 | 2007-02-13 15:01:11 +0000 | [diff] [blame] | 2145 | sqlite3OpenTable(pParse, iDest, iDbDest, pDest, OP_OpenWrite); |
dan | 427ebba | 2013-11-05 16:39:31 +0000 | [diff] [blame] | 2146 | assert( HasRowid(pDest) || destHasUniqueIdx ); |
drh | 8257aa8 | 2017-07-26 19:59:13 +0000 | [diff] [blame] | 2147 | if( (db->mDbFlags & DBFLAG_Vacuum)==0 && ( |
dan | e34162b | 2015-04-01 18:20:25 +0000 | [diff] [blame] | 2148 | (pDest->iPKey<0 && pDest->pIndex!=0) /* (1) */ |
drh | 9d9cf22 | 2007-02-13 15:01:11 +0000 | [diff] [blame] | 2149 | || destHasUniqueIdx /* (2) */ |
| 2150 | || (onError!=OE_Abort && onError!=OE_Rollback) /* (3) */ |
dan | e34162b | 2015-04-01 18:20:25 +0000 | [diff] [blame] | 2151 | )){ |
drh | 9d9cf22 | 2007-02-13 15:01:11 +0000 | [diff] [blame] | 2152 | /* In some circumstances, we are able to run the xfer optimization |
dan | e34162b | 2015-04-01 18:20:25 +0000 | [diff] [blame] | 2153 | ** only if the destination table is initially empty. Unless the |
drh | 8257aa8 | 2017-07-26 19:59:13 +0000 | [diff] [blame] | 2154 | ** DBFLAG_Vacuum flag is set, this block generates code to make |
| 2155 | ** that determination. If DBFLAG_Vacuum is set, then the destination |
dan | e34162b | 2015-04-01 18:20:25 +0000 | [diff] [blame] | 2156 | ** table is always empty. |
| 2157 | ** |
| 2158 | ** Conditions under which the destination must be empty: |
drh | bd36ba6 | 2007-03-31 13:00:26 +0000 | [diff] [blame] | 2159 | ** |
drh | 9d9cf22 | 2007-02-13 15:01:11 +0000 | [diff] [blame] | 2160 | ** (1) There is no INTEGER PRIMARY KEY but there are indices. |
| 2161 | ** (If the destination is not initially empty, the rowid fields |
| 2162 | ** of index entries might need to change.) |
| 2163 | ** |
| 2164 | ** (2) The destination has a unique index. (The xfer optimization |
drh | bd36ba6 | 2007-03-31 13:00:26 +0000 | [diff] [blame] | 2165 | ** is unable to test uniqueness.) |
| 2166 | ** |
| 2167 | ** (3) onError is something other than OE_Abort and OE_Rollback. |
drh | f33c9fa | 2007-04-10 18:17:55 +0000 | [diff] [blame] | 2168 | */ |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 2169 | addr1 = sqlite3VdbeAddOp2(v, OP_Rewind, iDest, 0); VdbeCoverage(v); |
drh | 2991ba0 | 2015-09-02 18:19:00 +0000 | [diff] [blame] | 2170 | emptyDestTest = sqlite3VdbeAddOp0(v, OP_Goto); |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 2171 | sqlite3VdbeJumpHere(v, addr1); |
drh | 9d9cf22 | 2007-02-13 15:01:11 +0000 | [diff] [blame] | 2172 | } |
drh | 5554827 | 2013-10-23 16:03:07 +0000 | [diff] [blame] | 2173 | if( HasRowid(pSrc) ){ |
drh | c9b9dea | 2016-11-15 02:46:39 +0000 | [diff] [blame] | 2174 | u8 insFlags; |
drh | 5554827 | 2013-10-23 16:03:07 +0000 | [diff] [blame] | 2175 | sqlite3OpenTable(pParse, iSrc, iDbSrc, pSrc, OP_OpenRead); |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 2176 | emptySrcTest = sqlite3VdbeAddOp2(v, OP_Rewind, iSrc, 0); VdbeCoverage(v); |
drh | 5554827 | 2013-10-23 16:03:07 +0000 | [diff] [blame] | 2177 | if( pDest->iPKey>=0 ){ |
| 2178 | addr1 = sqlite3VdbeAddOp2(v, OP_Rowid, iSrc, regRowid); |
| 2179 | addr2 = sqlite3VdbeAddOp3(v, OP_NotExists, iDest, 0, regRowid); |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 2180 | VdbeCoverage(v); |
drh | f9c8ce3 | 2013-11-05 13:33:55 +0000 | [diff] [blame] | 2181 | sqlite3RowidConstraint(pParse, onError, pDest); |
drh | 5554827 | 2013-10-23 16:03:07 +0000 | [diff] [blame] | 2182 | sqlite3VdbeJumpHere(v, addr2); |
| 2183 | autoIncStep(pParse, regAutoinc, regRowid); |
| 2184 | }else if( pDest->pIndex==0 ){ |
| 2185 | addr1 = sqlite3VdbeAddOp2(v, OP_NewRowid, iDest, regRowid); |
| 2186 | }else{ |
| 2187 | addr1 = sqlite3VdbeAddOp2(v, OP_Rowid, iSrc, regRowid); |
| 2188 | assert( (pDest->tabFlags & TF_Autoincrement)==0 ); |
| 2189 | } |
drh | e7b554d | 2017-01-09 15:44:25 +0000 | [diff] [blame] | 2190 | sqlite3VdbeAddOp3(v, OP_RowData, iSrc, regData, 1); |
drh | 8257aa8 | 2017-07-26 19:59:13 +0000 | [diff] [blame] | 2191 | if( db->mDbFlags & DBFLAG_Vacuum ){ |
drh | 86b40df | 2017-08-01 19:53:43 +0000 | [diff] [blame] | 2192 | sqlite3VdbeAddOp1(v, OP_SeekEnd, iDest); |
drh | c9b9dea | 2016-11-15 02:46:39 +0000 | [diff] [blame] | 2193 | insFlags = OPFLAG_NCHANGE|OPFLAG_LASTROWID| |
| 2194 | OPFLAG_APPEND|OPFLAG_USESEEKRESULT; |
| 2195 | }else{ |
| 2196 | insFlags = OPFLAG_NCHANGE|OPFLAG_LASTROWID|OPFLAG_APPEND; |
| 2197 | } |
drh | 9b34abe | 2016-01-16 15:12:35 +0000 | [diff] [blame] | 2198 | sqlite3VdbeAddOp4(v, OP_Insert, iDest, regData, regRowid, |
drh | 20f272c | 2016-01-20 11:33:37 +0000 | [diff] [blame] | 2199 | (char*)pDest, P4_TABLE); |
drh | c9b9dea | 2016-11-15 02:46:39 +0000 | [diff] [blame] | 2200 | sqlite3VdbeChangeP5(v, insFlags); |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 2201 | sqlite3VdbeAddOp2(v, OP_Next, iSrc, addr1); VdbeCoverage(v); |
drh | 5554827 | 2013-10-23 16:03:07 +0000 | [diff] [blame] | 2202 | sqlite3VdbeAddOp2(v, OP_Close, iSrc, 0); |
| 2203 | sqlite3VdbeAddOp2(v, OP_Close, iDest, 0); |
drh | 95bad4c | 2007-03-28 18:04:10 +0000 | [diff] [blame] | 2204 | }else{ |
drh | da475b8 | 2013-11-04 21:44:54 +0000 | [diff] [blame] | 2205 | sqlite3TableLock(pParse, iDbDest, pDest->tnum, 1, pDest->zName); |
| 2206 | sqlite3TableLock(pParse, iDbSrc, pSrc->tnum, 0, pSrc->zName); |
drh | 95bad4c | 2007-03-28 18:04:10 +0000 | [diff] [blame] | 2207 | } |
drh | 9d9cf22 | 2007-02-13 15:01:11 +0000 | [diff] [blame] | 2208 | for(pDestIdx=pDest->pIndex; pDestIdx; pDestIdx=pDestIdx->pNext){ |
drh | 41b9ca2 | 2015-07-28 18:53:37 +0000 | [diff] [blame] | 2209 | u8 idxInsFlags = 0; |
drh | 1b7ecbb | 2009-05-03 01:00:59 +0000 | [diff] [blame] | 2210 | for(pSrcIdx=pSrc->pIndex; ALWAYS(pSrcIdx); pSrcIdx=pSrcIdx->pNext){ |
drh | 9d9cf22 | 2007-02-13 15:01:11 +0000 | [diff] [blame] | 2211 | if( xferCompatibleIndex(pDestIdx, pSrcIdx) ) break; |
| 2212 | } |
| 2213 | assert( pSrcIdx ); |
drh | 2ec2fb2 | 2013-11-06 19:59:23 +0000 | [diff] [blame] | 2214 | sqlite3VdbeAddOp3(v, OP_OpenRead, iSrc, pSrcIdx->tnum, iDbSrc); |
| 2215 | sqlite3VdbeSetP4KeyInfo(pParse, pSrcIdx); |
drh | d4e70eb | 2008-01-02 00:34:36 +0000 | [diff] [blame] | 2216 | VdbeComment((v, "%s", pSrcIdx->zName)); |
drh | 2ec2fb2 | 2013-11-06 19:59:23 +0000 | [diff] [blame] | 2217 | sqlite3VdbeAddOp3(v, OP_OpenWrite, iDest, pDestIdx->tnum, iDbDest); |
| 2218 | sqlite3VdbeSetP4KeyInfo(pParse, pDestIdx); |
dan | 5988572 | 2013-10-04 18:17:18 +0000 | [diff] [blame] | 2219 | sqlite3VdbeChangeP5(v, OPFLAG_BULKCSR); |
danielk1977 | 207872a | 2008-01-03 07:54:23 +0000 | [diff] [blame] | 2220 | VdbeComment((v, "%s", pDestIdx->zName)); |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 2221 | addr1 = sqlite3VdbeAddOp2(v, OP_Rewind, iSrc, 0); VdbeCoverage(v); |
drh | e7b554d | 2017-01-09 15:44:25 +0000 | [diff] [blame] | 2222 | sqlite3VdbeAddOp3(v, OP_RowData, iSrc, regData, 1); |
drh | 8257aa8 | 2017-07-26 19:59:13 +0000 | [diff] [blame] | 2223 | if( db->mDbFlags & DBFLAG_Vacuum ){ |
dan | e34162b | 2015-04-01 18:20:25 +0000 | [diff] [blame] | 2224 | /* This INSERT command is part of a VACUUM operation, which guarantees |
| 2225 | ** that the destination table is empty. If all indexed columns use |
| 2226 | ** collation sequence BINARY, then it can also be assumed that the |
| 2227 | ** index will be populated by inserting keys in strictly sorted |
| 2228 | ** order. In this case, instead of seeking within the b-tree as part |
drh | 86b40df | 2017-08-01 19:53:43 +0000 | [diff] [blame] | 2229 | ** of every OP_IdxInsert opcode, an OP_SeekEnd is added before the |
dan | e34162b | 2015-04-01 18:20:25 +0000 | [diff] [blame] | 2230 | ** OP_IdxInsert to seek to the point within the b-tree where each key |
| 2231 | ** should be inserted. This is faster. |
| 2232 | ** |
| 2233 | ** If any of the indexed columns use a collation sequence other than |
| 2234 | ** BINARY, this optimization is disabled. This is because the user |
| 2235 | ** might change the definition of a collation sequence and then run |
| 2236 | ** a VACUUM command. In that case keys may not be written in strictly |
| 2237 | ** sorted order. */ |
dan | e34162b | 2015-04-01 18:20:25 +0000 | [diff] [blame] | 2238 | for(i=0; i<pSrcIdx->nColumn; i++){ |
drh | f19aa5f | 2015-12-30 16:51:20 +0000 | [diff] [blame] | 2239 | const char *zColl = pSrcIdx->azColl[i]; |
drh | f19aa5f | 2015-12-30 16:51:20 +0000 | [diff] [blame] | 2240 | if( sqlite3_stricmp(sqlite3StrBINARY, zColl) ) break; |
dan | e34162b | 2015-04-01 18:20:25 +0000 | [diff] [blame] | 2241 | } |
| 2242 | if( i==pSrcIdx->nColumn ){ |
drh | 41b9ca2 | 2015-07-28 18:53:37 +0000 | [diff] [blame] | 2243 | idxInsFlags = OPFLAG_USESEEKRESULT; |
drh | 86b40df | 2017-08-01 19:53:43 +0000 | [diff] [blame] | 2244 | sqlite3VdbeAddOp1(v, OP_SeekEnd, iDest); |
dan | e34162b | 2015-04-01 18:20:25 +0000 | [diff] [blame] | 2245 | } |
| 2246 | } |
drh | 41b9ca2 | 2015-07-28 18:53:37 +0000 | [diff] [blame] | 2247 | if( !HasRowid(pSrc) && pDestIdx->idxType==2 ){ |
| 2248 | idxInsFlags |= OPFLAG_NCHANGE; |
| 2249 | } |
drh | 9b4eaeb | 2016-11-09 00:10:33 +0000 | [diff] [blame] | 2250 | sqlite3VdbeAddOp2(v, OP_IdxInsert, iDest, regData); |
| 2251 | sqlite3VdbeChangeP5(v, idxInsFlags|OPFLAG_APPEND); |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 2252 | sqlite3VdbeAddOp2(v, OP_Next, iSrc, addr1+1); VdbeCoverage(v); |
drh | 9d9cf22 | 2007-02-13 15:01:11 +0000 | [diff] [blame] | 2253 | sqlite3VdbeJumpHere(v, addr1); |
drh | 5554827 | 2013-10-23 16:03:07 +0000 | [diff] [blame] | 2254 | sqlite3VdbeAddOp2(v, OP_Close, iSrc, 0); |
| 2255 | sqlite3VdbeAddOp2(v, OP_Close, iDest, 0); |
drh | 9d9cf22 | 2007-02-13 15:01:11 +0000 | [diff] [blame] | 2256 | } |
drh | aceb31b | 2014-02-08 01:40:27 +0000 | [diff] [blame] | 2257 | if( emptySrcTest ) sqlite3VdbeJumpHere(v, emptySrcTest); |
drh | b765411 | 2008-01-12 12:48:07 +0000 | [diff] [blame] | 2258 | sqlite3ReleaseTempReg(pParse, regRowid); |
| 2259 | sqlite3ReleaseTempReg(pParse, regData); |
drh | 9d9cf22 | 2007-02-13 15:01:11 +0000 | [diff] [blame] | 2260 | if( emptyDestTest ){ |
drh | 1dd518c | 2016-10-03 02:59:33 +0000 | [diff] [blame] | 2261 | sqlite3AutoincrementEnd(pParse); |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 2262 | sqlite3VdbeAddOp2(v, OP_Halt, SQLITE_OK, 0); |
drh | 9d9cf22 | 2007-02-13 15:01:11 +0000 | [diff] [blame] | 2263 | sqlite3VdbeJumpHere(v, emptyDestTest); |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 2264 | sqlite3VdbeAddOp2(v, OP_Close, iDest, 0); |
drh | 9d9cf22 | 2007-02-13 15:01:11 +0000 | [diff] [blame] | 2265 | return 0; |
| 2266 | }else{ |
| 2267 | return 1; |
| 2268 | } |
| 2269 | } |
| 2270 | #endif /* SQLITE_OMIT_XFER_OPT */ |