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