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