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