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