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