OLD | NEW |
1 /* | 1 /* |
2 ** 2001 September 15 | 2 ** 2001 September 15 |
3 ** | 3 ** |
4 ** The author disclaims copyright to this source code. In place of | 4 ** The author disclaims copyright to this source code. In place of |
5 ** a legal notice, here is a blessing: | 5 ** a legal notice, here is a blessing: |
6 ** | 6 ** |
7 ** May you do good and not evil. | 7 ** May you do good and not evil. |
8 ** May you find forgiveness for yourself and forgive others. | 8 ** May you find forgiveness for yourself and forgive others. |
9 ** May you share freely, never taking more than you give. | 9 ** May you share freely, never taking more than you give. |
10 ** | 10 ** |
(...skipping 24 matching lines...) Expand all Loading... |
35 v = sqlite3GetVdbe(pParse); | 35 v = sqlite3GetVdbe(pParse); |
36 assert( opcode==OP_OpenWrite || opcode==OP_OpenRead ); | 36 assert( opcode==OP_OpenWrite || opcode==OP_OpenRead ); |
37 sqlite3TableLock(pParse, iDb, pTab->tnum, | 37 sqlite3TableLock(pParse, iDb, pTab->tnum, |
38 (opcode==OP_OpenWrite)?1:0, pTab->zName); | 38 (opcode==OP_OpenWrite)?1:0, pTab->zName); |
39 if( HasRowid(pTab) ){ | 39 if( HasRowid(pTab) ){ |
40 sqlite3VdbeAddOp4Int(v, opcode, iCur, pTab->tnum, iDb, pTab->nCol); | 40 sqlite3VdbeAddOp4Int(v, opcode, iCur, pTab->tnum, iDb, pTab->nCol); |
41 VdbeComment((v, "%s", pTab->zName)); | 41 VdbeComment((v, "%s", pTab->zName)); |
42 }else{ | 42 }else{ |
43 Index *pPk = sqlite3PrimaryKeyIndex(pTab); | 43 Index *pPk = sqlite3PrimaryKeyIndex(pTab); |
44 assert( pPk!=0 ); | 44 assert( pPk!=0 ); |
45 assert( pPk->tnum=pTab->tnum ); | 45 assert( pPk->tnum==pTab->tnum ); |
46 sqlite3VdbeAddOp3(v, opcode, iCur, pPk->tnum, iDb); | 46 sqlite3VdbeAddOp3(v, opcode, iCur, pPk->tnum, iDb); |
47 sqlite3VdbeSetP4KeyInfo(pParse, pPk); | 47 sqlite3VdbeSetP4KeyInfo(pParse, pPk); |
48 VdbeComment((v, "%s", pTab->zName)); | 48 VdbeComment((v, "%s", pTab->zName)); |
49 } | 49 } |
50 } | 50 } |
51 | 51 |
52 /* | 52 /* |
53 ** Return a pointer to the column affinity string associated with index | 53 ** Return a pointer to the column affinity string associated with index |
54 ** pIdx. A column affinity string has one character for each column in | 54 ** pIdx. A column affinity string has one character for each column in |
55 ** the table, according to the affinity of the column: | 55 ** the table, according to the affinity of the column: |
56 ** | 56 ** |
57 ** Character Column affinity | 57 ** Character Column affinity |
58 ** ------------------------------ | 58 ** ------------------------------ |
59 ** 'A' NONE | 59 ** 'A' BLOB |
60 ** 'B' TEXT | 60 ** 'B' TEXT |
61 ** 'C' NUMERIC | 61 ** 'C' NUMERIC |
62 ** 'D' INTEGER | 62 ** 'D' INTEGER |
63 ** 'F' REAL | 63 ** 'F' REAL |
64 ** | 64 ** |
65 ** An extra 'D' is appended to the end of the string to cover the | 65 ** An extra 'D' is appended to the end of the string to cover the |
66 ** rowid that appears as the last column in every index. | 66 ** rowid that appears as the last column in every index. |
67 ** | 67 ** |
68 ** Memory for the buffer containing the column index affinity string | 68 ** Memory for the buffer containing the column index affinity string |
69 ** is managed along with the rest of the Index structure. It will be | 69 ** is managed along with the rest of the Index structure. It will be |
70 ** released when sqlite3DeleteIndex() is called. | 70 ** released when sqlite3DeleteIndex() is called. |
71 */ | 71 */ |
72 const char *sqlite3IndexAffinityStr(Vdbe *v, Index *pIdx){ | 72 const char *sqlite3IndexAffinityStr(sqlite3 *db, Index *pIdx){ |
73 if( !pIdx->zColAff ){ | 73 if( !pIdx->zColAff ){ |
74 /* The first time a column affinity string for a particular index is | 74 /* The first time a column affinity string for a particular index is |
75 ** required, it is allocated and populated here. It is then stored as | 75 ** required, it is allocated and populated here. It is then stored as |
76 ** a member of the Index structure for subsequent use. | 76 ** a member of the Index structure for subsequent use. |
77 ** | 77 ** |
78 ** The column affinity string will eventually be deleted by | 78 ** The column affinity string will eventually be deleted by |
79 ** sqliteDeleteIndex() when the Index structure itself is cleaned | 79 ** sqliteDeleteIndex() when the Index structure itself is cleaned |
80 ** up. | 80 ** up. |
81 */ | 81 */ |
82 int n; | 82 int n; |
83 Table *pTab = pIdx->pTable; | 83 Table *pTab = pIdx->pTable; |
84 sqlite3 *db = sqlite3VdbeDb(v); | |
85 pIdx->zColAff = (char *)sqlite3DbMallocRaw(0, pIdx->nColumn+1); | 84 pIdx->zColAff = (char *)sqlite3DbMallocRaw(0, pIdx->nColumn+1); |
86 if( !pIdx->zColAff ){ | 85 if( !pIdx->zColAff ){ |
87 db->mallocFailed = 1; | 86 db->mallocFailed = 1; |
88 return 0; | 87 return 0; |
89 } | 88 } |
90 for(n=0; n<pIdx->nColumn; n++){ | 89 for(n=0; n<pIdx->nColumn; n++){ |
91 i16 x = pIdx->aiColumn[n]; | 90 i16 x = pIdx->aiColumn[n]; |
92 pIdx->zColAff[n] = x<0 ? SQLITE_AFF_INTEGER : pTab->aCol[x].affinity; | 91 if( x>=0 ){ |
| 92 pIdx->zColAff[n] = pTab->aCol[x].affinity; |
| 93 }else if( x==XN_ROWID ){ |
| 94 pIdx->zColAff[n] = SQLITE_AFF_INTEGER; |
| 95 }else{ |
| 96 char aff; |
| 97 assert( x==XN_EXPR ); |
| 98 assert( pIdx->aColExpr!=0 ); |
| 99 aff = sqlite3ExprAffinity(pIdx->aColExpr->a[n].pExpr); |
| 100 if( aff==0 ) aff = SQLITE_AFF_BLOB; |
| 101 pIdx->zColAff[n] = aff; |
| 102 } |
93 } | 103 } |
94 pIdx->zColAff[n] = 0; | 104 pIdx->zColAff[n] = 0; |
95 } | 105 } |
96 | 106 |
97 return pIdx->zColAff; | 107 return pIdx->zColAff; |
98 } | 108 } |
99 | 109 |
100 /* | 110 /* |
101 ** Compute the affinity string for table pTab, if it has not already been | 111 ** Compute the affinity string for table pTab, if it has not already been |
102 ** computed. As an optimization, omit trailing SQLITE_AFF_NONE affinities. | 112 ** computed. As an optimization, omit trailing SQLITE_AFF_BLOB affinities. |
103 ** | 113 ** |
104 ** If the affinity exists (if it is no entirely SQLITE_AFF_NONE values) and | 114 ** If the affinity exists (if it is no entirely SQLITE_AFF_BLOB values) and |
105 ** if iReg>0 then code an OP_Affinity opcode that will set the affinities | 115 ** if iReg>0 then code an OP_Affinity opcode that will set the affinities |
106 ** for register iReg and following. Or if affinities exists and iReg==0, | 116 ** for register iReg and following. Or if affinities exists and iReg==0, |
107 ** then just set the P4 operand of the previous opcode (which should be | 117 ** then just set the P4 operand of the previous opcode (which should be |
108 ** an OP_MakeRecord) to the affinity string. | 118 ** an OP_MakeRecord) to the affinity string. |
109 ** | 119 ** |
110 ** A column affinity string has one character per column: | 120 ** A column affinity string has one character per column: |
111 ** | 121 ** |
112 ** Character Column affinity | 122 ** Character Column affinity |
113 ** ------------------------------ | 123 ** ------------------------------ |
114 ** 'A' NONE | 124 ** 'A' BLOB |
115 ** 'B' TEXT | 125 ** 'B' TEXT |
116 ** 'C' NUMERIC | 126 ** 'C' NUMERIC |
117 ** 'D' INTEGER | 127 ** 'D' INTEGER |
118 ** 'E' REAL | 128 ** 'E' REAL |
119 */ | 129 */ |
120 void sqlite3TableAffinity(Vdbe *v, Table *pTab, int iReg){ | 130 void sqlite3TableAffinity(Vdbe *v, Table *pTab, int iReg){ |
121 int i; | 131 int i; |
122 char *zColAff = pTab->zColAff; | 132 char *zColAff = pTab->zColAff; |
123 if( zColAff==0 ){ | 133 if( zColAff==0 ){ |
124 sqlite3 *db = sqlite3VdbeDb(v); | 134 sqlite3 *db = sqlite3VdbeDb(v); |
125 zColAff = (char *)sqlite3DbMallocRaw(0, pTab->nCol+1); | 135 zColAff = (char *)sqlite3DbMallocRaw(0, pTab->nCol+1); |
126 if( !zColAff ){ | 136 if( !zColAff ){ |
127 db->mallocFailed = 1; | 137 db->mallocFailed = 1; |
128 return; | 138 return; |
129 } | 139 } |
130 | 140 |
131 for(i=0; i<pTab->nCol; i++){ | 141 for(i=0; i<pTab->nCol; i++){ |
132 zColAff[i] = pTab->aCol[i].affinity; | 142 zColAff[i] = pTab->aCol[i].affinity; |
133 } | 143 } |
134 do{ | 144 do{ |
135 zColAff[i--] = 0; | 145 zColAff[i--] = 0; |
136 }while( i>=0 && zColAff[i]==SQLITE_AFF_NONE ); | 146 }while( i>=0 && zColAff[i]==SQLITE_AFF_BLOB ); |
137 pTab->zColAff = zColAff; | 147 pTab->zColAff = zColAff; |
138 } | 148 } |
139 i = sqlite3Strlen30(zColAff); | 149 i = sqlite3Strlen30(zColAff); |
140 if( i ){ | 150 if( i ){ |
141 if( iReg ){ | 151 if( iReg ){ |
142 sqlite3VdbeAddOp4(v, OP_Affinity, iReg, i, 0, zColAff, i); | 152 sqlite3VdbeAddOp4(v, OP_Affinity, iReg, i, 0, zColAff, i); |
143 }else{ | 153 }else{ |
144 sqlite3VdbeChangeP4(v, -1, zColAff, i); | 154 sqlite3VdbeChangeP4(v, -1, zColAff, i); |
145 } | 155 } |
146 } | 156 } |
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
243 AutoincInfo *p; /* Information about an AUTOINCREMENT */ | 253 AutoincInfo *p; /* Information about an AUTOINCREMENT */ |
244 sqlite3 *db = pParse->db; /* The database connection */ | 254 sqlite3 *db = pParse->db; /* The database connection */ |
245 Db *pDb; /* Database only autoinc table */ | 255 Db *pDb; /* Database only autoinc table */ |
246 int memId; /* Register holding max rowid */ | 256 int memId; /* Register holding max rowid */ |
247 int addr; /* A VDBE address */ | 257 int addr; /* A VDBE address */ |
248 Vdbe *v = pParse->pVdbe; /* VDBE under construction */ | 258 Vdbe *v = pParse->pVdbe; /* VDBE under construction */ |
249 | 259 |
250 /* This routine is never called during trigger-generation. It is | 260 /* This routine is never called during trigger-generation. It is |
251 ** only called from the top-level */ | 261 ** only called from the top-level */ |
252 assert( pParse->pTriggerTab==0 ); | 262 assert( pParse->pTriggerTab==0 ); |
253 assert( pParse==sqlite3ParseToplevel(pParse) ); | 263 assert( sqlite3IsToplevel(pParse) ); |
254 | 264 |
255 assert( v ); /* We failed long ago if this is not so */ | 265 assert( v ); /* We failed long ago if this is not so */ |
256 for(p = pParse->pAinc; p; p = p->pNext){ | 266 for(p = pParse->pAinc; p; p = p->pNext){ |
257 pDb = &db->aDb[p->iDb]; | 267 pDb = &db->aDb[p->iDb]; |
258 memId = p->regCtr; | 268 memId = p->regCtr; |
259 assert( sqlite3SchemaMutexHeld(db, 0, pDb->pSchema) ); | 269 assert( sqlite3SchemaMutexHeld(db, 0, pDb->pSchema) ); |
260 sqlite3OpenTable(pParse, 0, p->iDb, pDb->pSchema->pSeqTab, OP_OpenRead); | 270 sqlite3OpenTable(pParse, 0, p->iDb, pDb->pSchema->pSeqTab, OP_OpenRead); |
261 sqlite3VdbeAddOp3(v, OP_Null, 0, memId, memId+1); | 271 sqlite3VdbeAddOp3(v, OP_Null, 0, memId, memId+1); |
262 addr = sqlite3VdbeCurrentAddr(v); | 272 addr = sqlite3VdbeCurrentAddr(v); |
263 sqlite3VdbeAddOp4(v, OP_String8, 0, memId-1, 0, p->pTab->zName, 0); | 273 sqlite3VdbeLoadString(v, memId-1, p->pTab->zName); |
264 sqlite3VdbeAddOp2(v, OP_Rewind, 0, addr+9); VdbeCoverage(v); | 274 sqlite3VdbeAddOp2(v, OP_Rewind, 0, addr+9); VdbeCoverage(v); |
265 sqlite3VdbeAddOp3(v, OP_Column, 0, 0, memId); | 275 sqlite3VdbeAddOp3(v, OP_Column, 0, 0, memId); |
266 sqlite3VdbeAddOp3(v, OP_Ne, memId-1, addr+7, memId); VdbeCoverage(v); | 276 sqlite3VdbeAddOp3(v, OP_Ne, memId-1, addr+7, memId); VdbeCoverage(v); |
267 sqlite3VdbeChangeP5(v, SQLITE_JUMPIFNULL); | 277 sqlite3VdbeChangeP5(v, SQLITE_JUMPIFNULL); |
268 sqlite3VdbeAddOp2(v, OP_Rowid, 0, memId+1); | 278 sqlite3VdbeAddOp2(v, OP_Rowid, 0, memId+1); |
269 sqlite3VdbeAddOp3(v, OP_Column, 0, 1, memId); | 279 sqlite3VdbeAddOp3(v, OP_Column, 0, 1, memId); |
270 sqlite3VdbeAddOp2(v, OP_Goto, 0, addr+9); | 280 sqlite3VdbeGoto(v, addr+9); |
271 sqlite3VdbeAddOp2(v, OP_Next, 0, addr+2); VdbeCoverage(v); | 281 sqlite3VdbeAddOp2(v, OP_Next, 0, addr+2); VdbeCoverage(v); |
272 sqlite3VdbeAddOp2(v, OP_Integer, 0, memId); | 282 sqlite3VdbeAddOp2(v, OP_Integer, 0, memId); |
273 sqlite3VdbeAddOp0(v, OP_Close); | 283 sqlite3VdbeAddOp0(v, OP_Close); |
274 } | 284 } |
275 } | 285 } |
276 | 286 |
277 /* | 287 /* |
278 ** Update the maximum rowid for an autoincrement calculation. | 288 ** Update the maximum rowid for an autoincrement calculation. |
279 ** | 289 ** |
280 ** This routine should be called when the top of the stack holds a | 290 ** This routine should be called when the top of the stack holds a |
(...skipping 15 matching lines...) Expand all Loading... |
296 ** routine just before the "exit" code. | 306 ** routine just before the "exit" code. |
297 */ | 307 */ |
298 void sqlite3AutoincrementEnd(Parse *pParse){ | 308 void sqlite3AutoincrementEnd(Parse *pParse){ |
299 AutoincInfo *p; | 309 AutoincInfo *p; |
300 Vdbe *v = pParse->pVdbe; | 310 Vdbe *v = pParse->pVdbe; |
301 sqlite3 *db = pParse->db; | 311 sqlite3 *db = pParse->db; |
302 | 312 |
303 assert( v ); | 313 assert( v ); |
304 for(p = pParse->pAinc; p; p = p->pNext){ | 314 for(p = pParse->pAinc; p; p = p->pNext){ |
305 Db *pDb = &db->aDb[p->iDb]; | 315 Db *pDb = &db->aDb[p->iDb]; |
306 int j1; | 316 int addr1; |
307 int iRec; | 317 int iRec; |
308 int memId = p->regCtr; | 318 int memId = p->regCtr; |
309 | 319 |
310 iRec = sqlite3GetTempReg(pParse); | 320 iRec = sqlite3GetTempReg(pParse); |
311 assert( sqlite3SchemaMutexHeld(db, 0, pDb->pSchema) ); | 321 assert( sqlite3SchemaMutexHeld(db, 0, pDb->pSchema) ); |
312 sqlite3OpenTable(pParse, 0, p->iDb, pDb->pSchema->pSeqTab, OP_OpenWrite); | 322 sqlite3OpenTable(pParse, 0, p->iDb, pDb->pSchema->pSeqTab, OP_OpenWrite); |
313 j1 = sqlite3VdbeAddOp1(v, OP_NotNull, memId+1); VdbeCoverage(v); | 323 addr1 = sqlite3VdbeAddOp1(v, OP_NotNull, memId+1); VdbeCoverage(v); |
314 sqlite3VdbeAddOp2(v, OP_NewRowid, 0, memId+1); | 324 sqlite3VdbeAddOp2(v, OP_NewRowid, 0, memId+1); |
315 sqlite3VdbeJumpHere(v, j1); | 325 sqlite3VdbeJumpHere(v, addr1); |
316 sqlite3VdbeAddOp3(v, OP_MakeRecord, memId-1, 2, iRec); | 326 sqlite3VdbeAddOp3(v, OP_MakeRecord, memId-1, 2, iRec); |
317 sqlite3VdbeAddOp3(v, OP_Insert, 0, iRec, memId+1); | 327 sqlite3VdbeAddOp3(v, OP_Insert, 0, iRec, memId+1); |
318 sqlite3VdbeChangeP5(v, OPFLAG_APPEND); | 328 sqlite3VdbeChangeP5(v, OPFLAG_APPEND); |
319 sqlite3VdbeAddOp0(v, OP_Close); | 329 sqlite3VdbeAddOp0(v, OP_Close); |
320 sqlite3ReleaseTempReg(pParse, iRec); | 330 sqlite3ReleaseTempReg(pParse, iRec); |
321 } | 331 } |
322 } | 332 } |
323 #else | 333 #else |
324 /* | 334 /* |
325 ** If SQLITE_OMIT_AUTOINCREMENT is defined, then the three routines | 335 ** If SQLITE_OMIT_AUTOINCREMENT is defined, then the three routines |
326 ** above are all no-ops | 336 ** above are all no-ops |
327 */ | 337 */ |
328 # define autoIncBegin(A,B,C) (0) | 338 # define autoIncBegin(A,B,C) (0) |
329 # define autoIncStep(A,B,C) | 339 # define autoIncStep(A,B,C) |
330 #endif /* SQLITE_OMIT_AUTOINCREMENT */ | 340 #endif /* SQLITE_OMIT_AUTOINCREMENT */ |
331 | 341 |
332 | 342 |
333 /* Forward declaration */ | 343 /* Forward declaration */ |
334 static int xferOptimization( | 344 static int xferOptimization( |
335 Parse *pParse, /* Parser context */ | 345 Parse *pParse, /* Parser context */ |
336 Table *pDest, /* The table we are inserting into */ | 346 Table *pDest, /* The table we are inserting into */ |
337 Select *pSelect, /* A SELECT statement to use as the data source */ | 347 Select *pSelect, /* A SELECT statement to use as the data source */ |
338 int onError, /* How to handle constraint errors */ | 348 int onError, /* How to handle constraint errors */ |
339 int iDbDest /* The database of pDest */ | 349 int iDbDest /* The database of pDest */ |
340 ); | 350 ); |
341 | 351 |
342 /* | 352 /* |
343 ** This routine is called to handle SQL of the following forms: | 353 ** This routine is called to handle SQL of the following forms: |
344 ** | 354 ** |
345 ** insert into TABLE (IDLIST) values(EXPRLIST) | 355 ** insert into TABLE (IDLIST) values(EXPRLIST),(EXPRLIST),... |
346 ** insert into TABLE (IDLIST) select | 356 ** insert into TABLE (IDLIST) select |
| 357 ** insert into TABLE (IDLIST) default values |
347 ** | 358 ** |
348 ** The IDLIST following the table name is always optional. If omitted, | 359 ** The IDLIST following the table name is always optional. If omitted, |
349 ** then a list of all columns for the table is substituted. The IDLIST | 360 ** then a list of all (non-hidden) columns for the table is substituted. |
350 ** appears in the pColumn parameter. pColumn is NULL if IDLIST is omitted. | 361 ** The IDLIST appears in the pColumn parameter. pColumn is NULL if IDLIST |
| 362 ** is omitted. |
351 ** | 363 ** |
352 ** The pList parameter holds EXPRLIST in the first form of the INSERT | 364 ** For the pSelect parameter holds the values to be inserted for the |
353 ** statement above, and pSelect is NULL. For the second form, pList is | 365 ** first two forms shown above. A VALUES clause is really just short-hand |
354 ** NULL and pSelect is a pointer to the select statement used to generate | 366 ** for a SELECT statement that omits the FROM clause and everything else |
355 ** data for the insert. | 367 ** that follows. If the pSelect parameter is NULL, that means that the |
| 368 ** DEFAULT VALUES form of the INSERT statement is intended. |
356 ** | 369 ** |
357 ** The code generated follows one of four templates. For a simple | 370 ** The code generated follows one of four templates. For a simple |
358 ** insert with data coming from a VALUES clause, the code executes | 371 ** insert with data coming from a single-row VALUES clause, the code executes |
359 ** once straight down through. Pseudo-code follows (we call this | 372 ** once straight down through. Pseudo-code follows (we call this |
360 ** the "1st template"): | 373 ** the "1st template"): |
361 ** | 374 ** |
362 ** open write cursor to <table> and its indices | 375 ** open write cursor to <table> and its indices |
363 ** put VALUES clause expressions into registers | 376 ** put VALUES clause expressions into registers |
364 ** write the resulting record into <table> | 377 ** write the resulting record into <table> |
365 ** cleanup | 378 ** cleanup |
366 ** | 379 ** |
367 ** The three remaining templates assume the statement is of the form | 380 ** The three remaining templates assume the statement is of the form |
368 ** | 381 ** |
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
455 int endOfLoop; /* Label for the end of the insertion loop */ | 468 int endOfLoop; /* Label for the end of the insertion loop */ |
456 int srcTab = 0; /* Data comes from this temporary cursor if >=0 */ | 469 int srcTab = 0; /* Data comes from this temporary cursor if >=0 */ |
457 int addrInsTop = 0; /* Jump to label "D" */ | 470 int addrInsTop = 0; /* Jump to label "D" */ |
458 int addrCont = 0; /* Top of insert loop. Label "C" in templates 3 and 4 */ | 471 int addrCont = 0; /* Top of insert loop. Label "C" in templates 3 and 4 */ |
459 SelectDest dest; /* Destination for SELECT on rhs of INSERT */ | 472 SelectDest dest; /* Destination for SELECT on rhs of INSERT */ |
460 int iDb; /* Index of database holding TABLE */ | 473 int iDb; /* Index of database holding TABLE */ |
461 Db *pDb; /* The database containing table being inserted into */ | 474 Db *pDb; /* The database containing table being inserted into */ |
462 u8 useTempTable = 0; /* Store SELECT results in intermediate table */ | 475 u8 useTempTable = 0; /* Store SELECT results in intermediate table */ |
463 u8 appendFlag = 0; /* True if the insert is likely to be an append */ | 476 u8 appendFlag = 0; /* True if the insert is likely to be an append */ |
464 u8 withoutRowid; /* 0 for normal table. 1 for WITHOUT ROWID table */ | 477 u8 withoutRowid; /* 0 for normal table. 1 for WITHOUT ROWID table */ |
465 u8 bIdListInOrder = 1; /* True if IDLIST is in table order */ | 478 u8 bIdListInOrder; /* True if IDLIST is in table order */ |
466 ExprList *pList = 0; /* List of VALUES() to be inserted */ | 479 ExprList *pList = 0; /* List of VALUES() to be inserted */ |
467 | 480 |
468 /* Register allocations */ | 481 /* Register allocations */ |
469 int regFromSelect = 0;/* Base register for data coming from SELECT */ | 482 int regFromSelect = 0;/* Base register for data coming from SELECT */ |
470 int regAutoinc = 0; /* Register holding the AUTOINCREMENT counter */ | 483 int regAutoinc = 0; /* Register holding the AUTOINCREMENT counter */ |
471 int regRowCount = 0; /* Memory cell used for the row counter */ | 484 int regRowCount = 0; /* Memory cell used for the row counter */ |
472 int regIns; /* Block of regs holding rowid+data being inserted */ | 485 int regIns; /* Block of regs holding rowid+data being inserted */ |
473 int regRowid; /* registers holding insert rowid */ | 486 int regRowid; /* registers holding insert rowid */ |
474 int regData; /* register holding first column to insert */ | 487 int regData; /* register holding first column to insert */ |
475 int *aRegIdx = 0; /* One register allocated to each index */ | 488 int *aRegIdx = 0; /* One register allocated to each index */ |
476 | 489 |
477 #ifndef SQLITE_OMIT_TRIGGER | 490 #ifndef SQLITE_OMIT_TRIGGER |
478 int isView; /* True if attempting to insert into a view */ | 491 int isView; /* True if attempting to insert into a view */ |
479 Trigger *pTrigger; /* List of triggers on pTab, if required */ | 492 Trigger *pTrigger; /* List of triggers on pTab, if required */ |
480 int tmask; /* Mask of trigger times */ | 493 int tmask; /* Mask of trigger times */ |
481 #endif | 494 #endif |
482 | 495 |
483 db = pParse->db; | 496 db = pParse->db; |
484 memset(&dest, 0, sizeof(dest)); | 497 memset(&dest, 0, sizeof(dest)); |
485 if( pParse->nErr || db->mallocFailed ){ | 498 if( pParse->nErr || db->mallocFailed ){ |
486 goto insert_cleanup; | 499 goto insert_cleanup; |
487 } | 500 } |
488 | 501 |
489 /* If the Select object is really just a simple VALUES() list with a | 502 /* If the Select object is really just a simple VALUES() list with a |
490 ** single row values (the common case) then keep that one row of values | 503 ** single row (the common case) then keep that one row of values |
491 ** and go ahead and discard the Select object | 504 ** and discard the other (unused) parts of the pSelect object |
492 */ | 505 */ |
493 if( pSelect && (pSelect->selFlags & SF_Values)!=0 && pSelect->pPrior==0 ){ | 506 if( pSelect && (pSelect->selFlags & SF_Values)!=0 && pSelect->pPrior==0 ){ |
494 pList = pSelect->pEList; | 507 pList = pSelect->pEList; |
495 pSelect->pEList = 0; | 508 pSelect->pEList = 0; |
496 sqlite3SelectDelete(db, pSelect); | 509 sqlite3SelectDelete(db, pSelect); |
497 pSelect = 0; | 510 pSelect = 0; |
498 } | 511 } |
499 | 512 |
500 /* Locate the table into which we will be inserting new information. | 513 /* Locate the table into which we will be inserting new information. |
501 */ | 514 */ |
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
589 ** all elements of the IDLIST really are columns of the table and | 602 ** all elements of the IDLIST really are columns of the table and |
590 ** remember the column indices. | 603 ** remember the column indices. |
591 ** | 604 ** |
592 ** If the table has an INTEGER PRIMARY KEY column and that column | 605 ** If the table has an INTEGER PRIMARY KEY column and that column |
593 ** is named in the IDLIST, then record in the ipkColumn variable | 606 ** is named in the IDLIST, then record in the ipkColumn variable |
594 ** the index into IDLIST of the primary key column. ipkColumn is | 607 ** the index into IDLIST of the primary key column. ipkColumn is |
595 ** the index of the primary key as it appears in IDLIST, not as | 608 ** the index of the primary key as it appears in IDLIST, not as |
596 ** is appears in the original table. (The index of the INTEGER | 609 ** is appears in the original table. (The index of the INTEGER |
597 ** PRIMARY KEY in the original table is pTab->iPKey.) | 610 ** PRIMARY KEY in the original table is pTab->iPKey.) |
598 */ | 611 */ |
| 612 bIdListInOrder = (pTab->tabFlags & TF_OOOHidden)==0; |
599 if( pColumn ){ | 613 if( pColumn ){ |
600 for(i=0; i<pColumn->nId; i++){ | 614 for(i=0; i<pColumn->nId; i++){ |
601 pColumn->a[i].idx = -1; | 615 pColumn->a[i].idx = -1; |
602 } | 616 } |
603 for(i=0; i<pColumn->nId; i++){ | 617 for(i=0; i<pColumn->nId; i++){ |
604 for(j=0; j<pTab->nCol; j++){ | 618 for(j=0; j<pTab->nCol; j++){ |
605 if( sqlite3StrICmp(pColumn->a[i].zName, pTab->aCol[j].zName)==0 ){ | 619 if( sqlite3StrICmp(pColumn->a[i].zName, pTab->aCol[j].zName)==0 ){ |
606 pColumn->a[i].idx = j; | 620 pColumn->a[i].idx = j; |
607 if( i!=j ) bIdListInOrder = 0; | 621 if( i!=j ) bIdListInOrder = 0; |
608 if( j==pTab->iPKey ){ | 622 if( j==pTab->iPKey ){ |
(...skipping 15 matching lines...) Expand all Loading... |
624 } | 638 } |
625 } | 639 } |
626 } | 640 } |
627 | 641 |
628 /* Figure out how many columns of data are supplied. If the data | 642 /* Figure out how many columns of data are supplied. If the data |
629 ** is coming from a SELECT statement, then generate a co-routine that | 643 ** is coming from a SELECT statement, then generate a co-routine that |
630 ** produces a single row of the SELECT on each invocation. The | 644 ** produces a single row of the SELECT on each invocation. The |
631 ** co-routine is the common header to the 3rd and 4th templates. | 645 ** co-routine is the common header to the 3rd and 4th templates. |
632 */ | 646 */ |
633 if( pSelect ){ | 647 if( pSelect ){ |
634 /* Data is coming from a SELECT. Generate a co-routine to run the SELECT */ | 648 /* Data is coming from a SELECT or from a multi-row VALUES clause. |
| 649 ** Generate a co-routine to run the SELECT. */ |
635 int regYield; /* Register holding co-routine entry-point */ | 650 int regYield; /* Register holding co-routine entry-point */ |
636 int addrTop; /* Top of the co-routine */ | 651 int addrTop; /* Top of the co-routine */ |
637 int rc; /* Result code */ | 652 int rc; /* Result code */ |
638 | 653 |
639 regYield = ++pParse->nMem; | 654 regYield = ++pParse->nMem; |
640 addrTop = sqlite3VdbeCurrentAddr(v) + 1; | 655 addrTop = sqlite3VdbeCurrentAddr(v) + 1; |
641 sqlite3VdbeAddOp3(v, OP_InitCoroutine, regYield, 0, addrTop); | 656 sqlite3VdbeAddOp3(v, OP_InitCoroutine, regYield, 0, addrTop); |
642 sqlite3SelectDestInit(&dest, SRT_Coroutine, regYield); | 657 sqlite3SelectDestInit(&dest, SRT_Coroutine, regYield); |
643 dest.iSdst = bIdListInOrder ? regData : 0; | 658 dest.iSdst = bIdListInOrder ? regData : 0; |
644 dest.nSdst = pTab->nCol; | 659 dest.nSdst = pTab->nCol; |
645 rc = sqlite3Select(pParse, pSelect, &dest); | 660 rc = sqlite3Select(pParse, pSelect, &dest); |
646 regFromSelect = dest.iSdst; | 661 regFromSelect = dest.iSdst; |
647 assert( pParse->nErr==0 || rc ); | 662 if( rc || db->mallocFailed || pParse->nErr ) goto insert_cleanup; |
648 if( rc || db->mallocFailed ) goto insert_cleanup; | |
649 sqlite3VdbeAddOp1(v, OP_EndCoroutine, regYield); | 663 sqlite3VdbeAddOp1(v, OP_EndCoroutine, regYield); |
650 sqlite3VdbeJumpHere(v, addrTop - 1); /* label B: */ | 664 sqlite3VdbeJumpHere(v, addrTop - 1); /* label B: */ |
651 assert( pSelect->pEList ); | 665 assert( pSelect->pEList ); |
652 nColumn = pSelect->pEList->nExpr; | 666 nColumn = pSelect->pEList->nExpr; |
653 | 667 |
654 /* Set useTempTable to TRUE if the result of the SELECT statement | 668 /* Set useTempTable to TRUE if the result of the SELECT statement |
655 ** should be written into a temporary table (template 4). Set to | 669 ** should be written into a temporary table (template 4). Set to |
656 ** FALSE if each output row of the SELECT can be written directly into | 670 ** FALSE if each output row of the SELECT can be written directly into |
657 ** the destination table (template 3). | 671 ** the destination table (template 3). |
658 ** | 672 ** |
(...skipping 21 matching lines...) Expand all Loading... |
680 int addrL; /* Label "L" */ | 694 int addrL; /* Label "L" */ |
681 | 695 |
682 srcTab = pParse->nTab++; | 696 srcTab = pParse->nTab++; |
683 regRec = sqlite3GetTempReg(pParse); | 697 regRec = sqlite3GetTempReg(pParse); |
684 regTempRowid = sqlite3GetTempReg(pParse); | 698 regTempRowid = sqlite3GetTempReg(pParse); |
685 sqlite3VdbeAddOp2(v, OP_OpenEphemeral, srcTab, nColumn); | 699 sqlite3VdbeAddOp2(v, OP_OpenEphemeral, srcTab, nColumn); |
686 addrL = sqlite3VdbeAddOp1(v, OP_Yield, dest.iSDParm); VdbeCoverage(v); | 700 addrL = sqlite3VdbeAddOp1(v, OP_Yield, dest.iSDParm); VdbeCoverage(v); |
687 sqlite3VdbeAddOp3(v, OP_MakeRecord, regFromSelect, nColumn, regRec); | 701 sqlite3VdbeAddOp3(v, OP_MakeRecord, regFromSelect, nColumn, regRec); |
688 sqlite3VdbeAddOp2(v, OP_NewRowid, srcTab, regTempRowid); | 702 sqlite3VdbeAddOp2(v, OP_NewRowid, srcTab, regTempRowid); |
689 sqlite3VdbeAddOp3(v, OP_Insert, srcTab, regRec, regTempRowid); | 703 sqlite3VdbeAddOp3(v, OP_Insert, srcTab, regRec, regTempRowid); |
690 sqlite3VdbeAddOp2(v, OP_Goto, 0, addrL); | 704 sqlite3VdbeGoto(v, addrL); |
691 sqlite3VdbeJumpHere(v, addrL); | 705 sqlite3VdbeJumpHere(v, addrL); |
692 sqlite3ReleaseTempReg(pParse, regRec); | 706 sqlite3ReleaseTempReg(pParse, regRec); |
693 sqlite3ReleaseTempReg(pParse, regTempRowid); | 707 sqlite3ReleaseTempReg(pParse, regTempRowid); |
694 } | 708 } |
695 }else{ | 709 }else{ |
696 /* This is the case if the data for the INSERT is coming from a VALUES | 710 /* This is the case if the data for the INSERT is coming from a |
697 ** clause | 711 ** single-row VALUES clause |
698 */ | 712 */ |
699 NameContext sNC; | 713 NameContext sNC; |
700 memset(&sNC, 0, sizeof(sNC)); | 714 memset(&sNC, 0, sizeof(sNC)); |
701 sNC.pParse = pParse; | 715 sNC.pParse = pParse; |
702 srcTab = -1; | 716 srcTab = -1; |
703 assert( useTempTable==0 ); | 717 assert( useTempTable==0 ); |
704 nColumn = pList ? pList->nExpr : 0; | 718 if( pList ){ |
705 for(i=0; i<nColumn; i++){ | 719 nColumn = pList->nExpr; |
706 if( sqlite3ResolveExprNames(&sNC, pList->a[i].pExpr) ){ | 720 if( sqlite3ResolveExprListNames(&sNC, pList) ){ |
707 goto insert_cleanup; | 721 goto insert_cleanup; |
708 } | 722 } |
| 723 }else{ |
| 724 nColumn = 0; |
709 } | 725 } |
710 } | 726 } |
711 | 727 |
712 /* If there is no IDLIST term but the table has an integer primary | 728 /* If there is no IDLIST term but the table has an integer primary |
713 ** key, the set the ipkColumn variable to the integer primary key | 729 ** key, the set the ipkColumn variable to the integer primary key |
714 ** column index in the original table definition. | 730 ** column index in the original table definition. |
715 */ | 731 */ |
716 if( pColumn==0 && nColumn>0 ){ | 732 if( pColumn==0 && nColumn>0 ){ |
717 ipkColumn = pTab->iPKey; | 733 ipkColumn = pTab->iPKey; |
718 } | 734 } |
719 | 735 |
720 /* Make sure the number of columns in the source data matches the number | 736 /* Make sure the number of columns in the source data matches the number |
721 ** of columns to be inserted into the table. | 737 ** of columns to be inserted into the table. |
722 */ | 738 */ |
723 if( IsVirtual(pTab) ){ | 739 for(i=0; i<pTab->nCol; i++){ |
724 for(i=0; i<pTab->nCol; i++){ | 740 nHidden += (IsHiddenColumn(&pTab->aCol[i]) ? 1 : 0); |
725 nHidden += (IsHiddenColumn(&pTab->aCol[i]) ? 1 : 0); | |
726 } | |
727 } | 741 } |
728 if( pColumn==0 && nColumn && nColumn!=(pTab->nCol-nHidden) ){ | 742 if( pColumn==0 && nColumn && nColumn!=(pTab->nCol-nHidden) ){ |
729 sqlite3ErrorMsg(pParse, | 743 sqlite3ErrorMsg(pParse, |
730 "table %S has %d columns but %d values were supplied", | 744 "table %S has %d columns but %d values were supplied", |
731 pTabList, 0, pTab->nCol-nHidden, nColumn); | 745 pTabList, 0, pTab->nCol-nHidden, nColumn); |
732 goto insert_cleanup; | 746 goto insert_cleanup; |
733 } | 747 } |
734 if( pColumn!=0 && nColumn!=pColumn->nId ){ | 748 if( pColumn!=0 && nColumn!=pColumn->nId ){ |
735 sqlite3ErrorMsg(pParse, "%d values for %d columns", nColumn, pColumn->nId); | 749 sqlite3ErrorMsg(pParse, "%d values for %d columns", nColumn, pColumn->nId); |
736 goto insert_cleanup; | 750 goto insert_cleanup; |
737 } | 751 } |
738 | 752 |
739 /* Initialize the count of rows to be inserted | 753 /* Initialize the count of rows to be inserted |
740 */ | 754 */ |
741 if( db->flags & SQLITE_CountRows ){ | 755 if( db->flags & SQLITE_CountRows ){ |
742 regRowCount = ++pParse->nMem; | 756 regRowCount = ++pParse->nMem; |
743 sqlite3VdbeAddOp2(v, OP_Integer, 0, regRowCount); | 757 sqlite3VdbeAddOp2(v, OP_Integer, 0, regRowCount); |
744 } | 758 } |
745 | 759 |
746 /* If this is not a view, open the table and and all indices */ | 760 /* If this is not a view, open the table and and all indices */ |
747 if( !isView ){ | 761 if( !isView ){ |
748 int nIdx; | 762 int nIdx; |
749 nIdx = sqlite3OpenTableAndIndices(pParse, pTab, OP_OpenWrite, -1, 0, | 763 nIdx = sqlite3OpenTableAndIndices(pParse, pTab, OP_OpenWrite, 0, -1, 0, |
750 &iDataCur, &iIdxCur); | 764 &iDataCur, &iIdxCur); |
751 aRegIdx = sqlite3DbMallocRaw(db, sizeof(int)*(nIdx+1)); | 765 aRegIdx = sqlite3DbMallocRaw(db, sizeof(int)*(nIdx+1)); |
752 if( aRegIdx==0 ){ | 766 if( aRegIdx==0 ){ |
753 goto insert_cleanup; | 767 goto insert_cleanup; |
754 } | 768 } |
755 for(i=0; i<nIdx; i++){ | 769 for(i=0; i<nIdx; i++){ |
756 aRegIdx[i] = ++pParse->nMem; | 770 aRegIdx[i] = ++pParse->nMem; |
757 } | 771 } |
758 } | 772 } |
759 | 773 |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
791 | 805 |
792 /* build the NEW.* reference row. Note that if there is an INTEGER | 806 /* build the NEW.* reference row. Note that if there is an INTEGER |
793 ** PRIMARY KEY into which a NULL is being inserted, that NULL will be | 807 ** PRIMARY KEY into which a NULL is being inserted, that NULL will be |
794 ** translated into a unique ID for the row. But on a BEFORE trigger, | 808 ** translated into a unique ID for the row. But on a BEFORE trigger, |
795 ** we do not know what the unique ID will be (because the insert has | 809 ** we do not know what the unique ID will be (because the insert has |
796 ** not happened yet) so we substitute a rowid of -1 | 810 ** not happened yet) so we substitute a rowid of -1 |
797 */ | 811 */ |
798 if( ipkColumn<0 ){ | 812 if( ipkColumn<0 ){ |
799 sqlite3VdbeAddOp2(v, OP_Integer, -1, regCols); | 813 sqlite3VdbeAddOp2(v, OP_Integer, -1, regCols); |
800 }else{ | 814 }else{ |
801 int j1; | 815 int addr1; |
802 assert( !withoutRowid ); | 816 assert( !withoutRowid ); |
803 if( useTempTable ){ | 817 if( useTempTable ){ |
804 sqlite3VdbeAddOp3(v, OP_Column, srcTab, ipkColumn, regCols); | 818 sqlite3VdbeAddOp3(v, OP_Column, srcTab, ipkColumn, regCols); |
805 }else{ | 819 }else{ |
806 assert( pSelect==0 ); /* Otherwise useTempTable is true */ | 820 assert( pSelect==0 ); /* Otherwise useTempTable is true */ |
807 sqlite3ExprCode(pParse, pList->a[ipkColumn].pExpr, regCols); | 821 sqlite3ExprCode(pParse, pList->a[ipkColumn].pExpr, regCols); |
808 } | 822 } |
809 j1 = sqlite3VdbeAddOp1(v, OP_NotNull, regCols); VdbeCoverage(v); | 823 addr1 = sqlite3VdbeAddOp1(v, OP_NotNull, regCols); VdbeCoverage(v); |
810 sqlite3VdbeAddOp2(v, OP_Integer, -1, regCols); | 824 sqlite3VdbeAddOp2(v, OP_Integer, -1, regCols); |
811 sqlite3VdbeJumpHere(v, j1); | 825 sqlite3VdbeJumpHere(v, addr1); |
812 sqlite3VdbeAddOp1(v, OP_MustBeInt, regCols); VdbeCoverage(v); | 826 sqlite3VdbeAddOp1(v, OP_MustBeInt, regCols); VdbeCoverage(v); |
813 } | 827 } |
814 | 828 |
815 /* Cannot have triggers on a virtual table. If it were possible, | 829 /* Cannot have triggers on a virtual table. If it were possible, |
816 ** this block would have to account for hidden column. | 830 ** this block would have to account for hidden column. |
817 */ | 831 */ |
818 assert( !IsVirtual(pTab) ); | 832 assert( !IsVirtual(pTab) ); |
819 | 833 |
820 /* Create the new column data | 834 /* Create the new column data |
821 */ | 835 */ |
822 for(i=0; i<pTab->nCol; i++){ | 836 for(i=j=0; i<pTab->nCol; i++){ |
823 if( pColumn==0 ){ | 837 if( pColumn ){ |
824 j = i; | |
825 }else{ | |
826 for(j=0; j<pColumn->nId; j++){ | 838 for(j=0; j<pColumn->nId; j++){ |
827 if( pColumn->a[j].idx==i ) break; | 839 if( pColumn->a[j].idx==i ) break; |
828 } | 840 } |
829 } | 841 } |
830 if( (!useTempTable && !pList) || (pColumn && j>=pColumn->nId) ){ | 842 if( (!useTempTable && !pList) || (pColumn && j>=pColumn->nId) |
| 843 || (pColumn==0 && IsOrdinaryHiddenColumn(&pTab->aCol[i])) ){ |
831 sqlite3ExprCode(pParse, pTab->aCol[i].pDflt, regCols+i+1); | 844 sqlite3ExprCode(pParse, pTab->aCol[i].pDflt, regCols+i+1); |
832 }else if( useTempTable ){ | 845 }else if( useTempTable ){ |
833 sqlite3VdbeAddOp3(v, OP_Column, srcTab, j, regCols+i+1); | 846 sqlite3VdbeAddOp3(v, OP_Column, srcTab, j, regCols+i+1); |
834 }else{ | 847 }else{ |
835 assert( pSelect==0 ); /* Otherwise useTempTable is true */ | 848 assert( pSelect==0 ); /* Otherwise useTempTable is true */ |
836 sqlite3ExprCodeAndCache(pParse, pList->a[j].pExpr, regCols+i+1); | 849 sqlite3ExprCodeAndCache(pParse, pList->a[j].pExpr, regCols+i+1); |
837 } | 850 } |
| 851 if( pColumn==0 && !IsOrdinaryHiddenColumn(&pTab->aCol[i]) ) j++; |
838 } | 852 } |
839 | 853 |
840 /* If this is an INSERT on a view with an INSTEAD OF INSERT trigger, | 854 /* If this is an INSERT on a view with an INSTEAD OF INSERT trigger, |
841 ** do not attempt any conversions before assembling the record. | 855 ** do not attempt any conversions before assembling the record. |
842 ** If this is a real table, attempt conversions as required by the | 856 ** If this is a real table, attempt conversions as required by the |
843 ** table column affinities. | 857 ** table column affinities. |
844 */ | 858 */ |
845 if( !isView ){ | 859 if( !isView ){ |
846 sqlite3TableAffinity(v, pTab, regCols+1); | 860 sqlite3TableAffinity(v, pTab, regCols+1); |
847 } | 861 } |
(...skipping 27 matching lines...) Expand all Loading... |
875 pOp->opcode = OP_NewRowid; | 889 pOp->opcode = OP_NewRowid; |
876 pOp->p1 = iDataCur; | 890 pOp->p1 = iDataCur; |
877 pOp->p2 = regRowid; | 891 pOp->p2 = regRowid; |
878 pOp->p3 = regAutoinc; | 892 pOp->p3 = regAutoinc; |
879 } | 893 } |
880 } | 894 } |
881 /* If the PRIMARY KEY expression is NULL, then use OP_NewRowid | 895 /* If the PRIMARY KEY expression is NULL, then use OP_NewRowid |
882 ** to generate a unique primary key value. | 896 ** to generate a unique primary key value. |
883 */ | 897 */ |
884 if( !appendFlag ){ | 898 if( !appendFlag ){ |
885 int j1; | 899 int addr1; |
886 if( !IsVirtual(pTab) ){ | 900 if( !IsVirtual(pTab) ){ |
887 j1 = sqlite3VdbeAddOp1(v, OP_NotNull, regRowid); VdbeCoverage(v); | 901 addr1 = sqlite3VdbeAddOp1(v, OP_NotNull, regRowid); VdbeCoverage(v); |
888 sqlite3VdbeAddOp3(v, OP_NewRowid, iDataCur, regRowid, regAutoinc); | 902 sqlite3VdbeAddOp3(v, OP_NewRowid, iDataCur, regRowid, regAutoinc); |
889 sqlite3VdbeJumpHere(v, j1); | 903 sqlite3VdbeJumpHere(v, addr1); |
890 }else{ | 904 }else{ |
891 j1 = sqlite3VdbeCurrentAddr(v); | 905 addr1 = sqlite3VdbeCurrentAddr(v); |
892 sqlite3VdbeAddOp2(v, OP_IsNull, regRowid, j1+2); VdbeCoverage(v); | 906 sqlite3VdbeAddOp2(v, OP_IsNull, regRowid, addr1+2); VdbeCoverage(v); |
893 } | 907 } |
894 sqlite3VdbeAddOp1(v, OP_MustBeInt, regRowid); VdbeCoverage(v); | 908 sqlite3VdbeAddOp1(v, OP_MustBeInt, regRowid); VdbeCoverage(v); |
895 } | 909 } |
896 }else if( IsVirtual(pTab) || withoutRowid ){ | 910 }else if( IsVirtual(pTab) || withoutRowid ){ |
897 sqlite3VdbeAddOp2(v, OP_Null, 0, regRowid); | 911 sqlite3VdbeAddOp2(v, OP_Null, 0, regRowid); |
898 }else{ | 912 }else{ |
899 sqlite3VdbeAddOp3(v, OP_NewRowid, iDataCur, regRowid, regAutoinc); | 913 sqlite3VdbeAddOp3(v, OP_NewRowid, iDataCur, regRowid, regAutoinc); |
900 appendFlag = 1; | 914 appendFlag = 1; |
901 } | 915 } |
902 autoIncStep(pParse, regAutoinc, regRowid); | 916 autoIncStep(pParse, regAutoinc, regRowid); |
903 | 917 |
904 /* Compute data for all columns of the new entry, beginning | 918 /* Compute data for all columns of the new entry, beginning |
905 ** with the first column. | 919 ** with the first column. |
906 */ | 920 */ |
907 nHidden = 0; | 921 nHidden = 0; |
908 for(i=0; i<pTab->nCol; i++){ | 922 for(i=0; i<pTab->nCol; i++){ |
909 int iRegStore = regRowid+1+i; | 923 int iRegStore = regRowid+1+i; |
910 if( i==pTab->iPKey ){ | 924 if( i==pTab->iPKey ){ |
911 /* The value of the INTEGER PRIMARY KEY column is always a NULL. | 925 /* The value of the INTEGER PRIMARY KEY column is always a NULL. |
912 ** Whenever this column is read, the rowid will be substituted | 926 ** Whenever this column is read, the rowid will be substituted |
913 ** in its place. Hence, fill this column with a NULL to avoid | 927 ** in its place. Hence, fill this column with a NULL to avoid |
914 ** taking up data space with information that will never be used. | 928 ** taking up data space with information that will never be used. |
915 ** As there may be shallow copies of this value, make it a soft-NULL */ | 929 ** As there may be shallow copies of this value, make it a soft-NULL */ |
916 sqlite3VdbeAddOp1(v, OP_SoftNull, iRegStore); | 930 sqlite3VdbeAddOp1(v, OP_SoftNull, iRegStore); |
917 continue; | 931 continue; |
918 } | 932 } |
919 if( pColumn==0 ){ | 933 if( pColumn==0 ){ |
920 if( IsHiddenColumn(&pTab->aCol[i]) ){ | 934 if( IsHiddenColumn(&pTab->aCol[i]) ){ |
921 assert( IsVirtual(pTab) ); | |
922 j = -1; | 935 j = -1; |
923 nHidden++; | 936 nHidden++; |
924 }else{ | 937 }else{ |
925 j = i - nHidden; | 938 j = i - nHidden; |
926 } | 939 } |
927 }else{ | 940 }else{ |
928 for(j=0; j<pColumn->nId; j++){ | 941 for(j=0; j<pColumn->nId; j++){ |
929 if( pColumn->a[j].idx==i ) break; | 942 if( pColumn->a[j].idx==i ) break; |
930 } | 943 } |
931 } | 944 } |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
979 | 992 |
980 /* The bottom of the main insertion loop, if the data source | 993 /* The bottom of the main insertion loop, if the data source |
981 ** is a SELECT statement. | 994 ** is a SELECT statement. |
982 */ | 995 */ |
983 sqlite3VdbeResolveLabel(v, endOfLoop); | 996 sqlite3VdbeResolveLabel(v, endOfLoop); |
984 if( useTempTable ){ | 997 if( useTempTable ){ |
985 sqlite3VdbeAddOp2(v, OP_Next, srcTab, addrCont); VdbeCoverage(v); | 998 sqlite3VdbeAddOp2(v, OP_Next, srcTab, addrCont); VdbeCoverage(v); |
986 sqlite3VdbeJumpHere(v, addrInsTop); | 999 sqlite3VdbeJumpHere(v, addrInsTop); |
987 sqlite3VdbeAddOp1(v, OP_Close, srcTab); | 1000 sqlite3VdbeAddOp1(v, OP_Close, srcTab); |
988 }else if( pSelect ){ | 1001 }else if( pSelect ){ |
989 sqlite3VdbeAddOp2(v, OP_Goto, 0, addrCont); | 1002 sqlite3VdbeGoto(v, addrCont); |
990 sqlite3VdbeJumpHere(v, addrInsTop); | 1003 sqlite3VdbeJumpHere(v, addrInsTop); |
991 } | 1004 } |
992 | 1005 |
993 if( !IsVirtual(pTab) && !isView ){ | 1006 if( !IsVirtual(pTab) && !isView ){ |
994 /* Close all tables opened */ | 1007 /* Close all tables opened */ |
995 if( iDataCur<iIdxCur ) sqlite3VdbeAddOp1(v, OP_Close, iDataCur); | 1008 if( iDataCur<iIdxCur ) sqlite3VdbeAddOp1(v, OP_Close, iDataCur); |
996 for(idx=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, idx++){ | 1009 for(idx=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, idx++){ |
997 sqlite3VdbeAddOp1(v, OP_Close, idx+iIdxCur); | 1010 sqlite3VdbeAddOp1(v, OP_Close, idx+iIdxCur); |
998 } | 1011 } |
999 } | 1012 } |
(...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1136 int *pbMayReplace /* OUT: Set to true if constraint may cause a replace */ | 1149 int *pbMayReplace /* OUT: Set to true if constraint may cause a replace */ |
1137 ){ | 1150 ){ |
1138 Vdbe *v; /* VDBE under constrution */ | 1151 Vdbe *v; /* VDBE under constrution */ |
1139 Index *pIdx; /* Pointer to one of the indices */ | 1152 Index *pIdx; /* Pointer to one of the indices */ |
1140 Index *pPk = 0; /* The PRIMARY KEY index */ | 1153 Index *pPk = 0; /* The PRIMARY KEY index */ |
1141 sqlite3 *db; /* Database connection */ | 1154 sqlite3 *db; /* Database connection */ |
1142 int i; /* loop counter */ | 1155 int i; /* loop counter */ |
1143 int ix; /* Index loop counter */ | 1156 int ix; /* Index loop counter */ |
1144 int nCol; /* Number of columns */ | 1157 int nCol; /* Number of columns */ |
1145 int onError; /* Conflict resolution strategy */ | 1158 int onError; /* Conflict resolution strategy */ |
1146 int j1; /* Address of jump instruction */ | 1159 int addr1; /* Address of jump instruction */ |
1147 int seenReplace = 0; /* True if REPLACE is used to resolve INT PK conflict */ | 1160 int seenReplace = 0; /* True if REPLACE is used to resolve INT PK conflict */ |
1148 int nPkField; /* Number of fields in PRIMARY KEY. 1 for ROWID tables */ | 1161 int nPkField; /* Number of fields in PRIMARY KEY. 1 for ROWID tables */ |
1149 int ipkTop = 0; /* Top of the rowid change constraint check */ | 1162 int ipkTop = 0; /* Top of the rowid change constraint check */ |
1150 int ipkBottom = 0; /* Bottom of the rowid change constraint check */ | 1163 int ipkBottom = 0; /* Bottom of the rowid change constraint check */ |
1151 u8 isUpdate; /* True if this is an UPDATE operation */ | 1164 u8 isUpdate; /* True if this is an UPDATE operation */ |
1152 u8 bAffinityDone = 0; /* True if the OP_Affinity operation has been run */ | 1165 u8 bAffinityDone = 0; /* True if the OP_Affinity operation has been run */ |
1153 int regRowid = -1; /* Register holding ROWID value */ | 1166 int regRowid = -1; /* Register holding ROWID value */ |
1154 | 1167 |
1155 isUpdate = regOldData!=0; | 1168 isUpdate = regOldData!=0; |
1156 db = pParse->db; | 1169 db = pParse->db; |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1207 VdbeCoverage(v); | 1220 VdbeCoverage(v); |
1208 break; | 1221 break; |
1209 } | 1222 } |
1210 case OE_Ignore: { | 1223 case OE_Ignore: { |
1211 sqlite3VdbeAddOp2(v, OP_IsNull, regNewData+1+i, ignoreDest); | 1224 sqlite3VdbeAddOp2(v, OP_IsNull, regNewData+1+i, ignoreDest); |
1212 VdbeCoverage(v); | 1225 VdbeCoverage(v); |
1213 break; | 1226 break; |
1214 } | 1227 } |
1215 default: { | 1228 default: { |
1216 assert( onError==OE_Replace ); | 1229 assert( onError==OE_Replace ); |
1217 j1 = sqlite3VdbeAddOp1(v, OP_NotNull, regNewData+1+i); VdbeCoverage(v); | 1230 addr1 = sqlite3VdbeAddOp1(v, OP_NotNull, regNewData+1+i); |
| 1231 VdbeCoverage(v); |
1218 sqlite3ExprCode(pParse, pTab->aCol[i].pDflt, regNewData+1+i); | 1232 sqlite3ExprCode(pParse, pTab->aCol[i].pDflt, regNewData+1+i); |
1219 sqlite3VdbeJumpHere(v, j1); | 1233 sqlite3VdbeJumpHere(v, addr1); |
1220 break; | 1234 break; |
1221 } | 1235 } |
1222 } | 1236 } |
1223 } | 1237 } |
1224 | 1238 |
1225 /* Test all CHECK constraints | 1239 /* Test all CHECK constraints |
1226 */ | 1240 */ |
1227 #ifndef SQLITE_OMIT_CHECK | 1241 #ifndef SQLITE_OMIT_CHECK |
1228 if( pTab->pCheck && (db->flags & SQLITE_IgnoreChecks)==0 ){ | 1242 if( pTab->pCheck && (db->flags & SQLITE_IgnoreChecks)==0 ){ |
1229 ExprList *pCheck = pTab->pCheck; | 1243 ExprList *pCheck = pTab->pCheck; |
1230 pParse->ckBase = regNewData+1; | 1244 pParse->ckBase = regNewData+1; |
1231 onError = overrideError!=OE_Default ? overrideError : OE_Abort; | 1245 onError = overrideError!=OE_Default ? overrideError : OE_Abort; |
1232 for(i=0; i<pCheck->nExpr; i++){ | 1246 for(i=0; i<pCheck->nExpr; i++){ |
1233 int allOk = sqlite3VdbeMakeLabel(v); | 1247 int allOk = sqlite3VdbeMakeLabel(v); |
1234 sqlite3ExprIfTrue(pParse, pCheck->a[i].pExpr, allOk, SQLITE_JUMPIFNULL); | 1248 sqlite3ExprIfTrue(pParse, pCheck->a[i].pExpr, allOk, SQLITE_JUMPIFNULL); |
1235 if( onError==OE_Ignore ){ | 1249 if( onError==OE_Ignore ){ |
1236 sqlite3VdbeAddOp2(v, OP_Goto, 0, ignoreDest); | 1250 sqlite3VdbeGoto(v, ignoreDest); |
1237 }else{ | 1251 }else{ |
1238 char *zName = pCheck->a[i].zName; | 1252 char *zName = pCheck->a[i].zName; |
1239 if( zName==0 ) zName = pTab->zName; | 1253 if( zName==0 ) zName = pTab->zName; |
1240 if( onError==OE_Replace ) onError = OE_Abort; /* IMP: R-15569-63625 */ | 1254 if( onError==OE_Replace ) onError = OE_Abort; /* IMP: R-15569-63625 */ |
1241 sqlite3HaltConstraint(pParse, SQLITE_CONSTRAINT_CHECK, | 1255 sqlite3HaltConstraint(pParse, SQLITE_CONSTRAINT_CHECK, |
1242 onError, zName, P4_TRANSIENT, | 1256 onError, zName, P4_TRANSIENT, |
1243 P5_ConstraintCheck); | 1257 P5_ConstraintCheck); |
1244 } | 1258 } |
1245 sqlite3VdbeResolveLabel(v, allOk); | 1259 sqlite3VdbeResolveLabel(v, allOk); |
1246 } | 1260 } |
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1324 ** to run without a statement journal if there are no indexes on the | 1338 ** to run without a statement journal if there are no indexes on the |
1325 ** table. | 1339 ** table. |
1326 */ | 1340 */ |
1327 Trigger *pTrigger = 0; | 1341 Trigger *pTrigger = 0; |
1328 if( db->flags&SQLITE_RecTriggers ){ | 1342 if( db->flags&SQLITE_RecTriggers ){ |
1329 pTrigger = sqlite3TriggersExist(pParse, pTab, TK_DELETE, 0, 0); | 1343 pTrigger = sqlite3TriggersExist(pParse, pTab, TK_DELETE, 0, 0); |
1330 } | 1344 } |
1331 if( pTrigger || sqlite3FkRequired(pParse, pTab, 0, 0) ){ | 1345 if( pTrigger || sqlite3FkRequired(pParse, pTab, 0, 0) ){ |
1332 sqlite3MultiWrite(pParse); | 1346 sqlite3MultiWrite(pParse); |
1333 sqlite3GenerateRowDelete(pParse, pTab, pTrigger, iDataCur, iIdxCur, | 1347 sqlite3GenerateRowDelete(pParse, pTab, pTrigger, iDataCur, iIdxCur, |
1334 regNewData, 1, 0, OE_Replace, 1); | 1348 regNewData, 1, 0, OE_Replace, |
1335 }else if( pTab->pIndex ){ | 1349 ONEPASS_SINGLE, -1); |
1336 sqlite3MultiWrite(pParse); | 1350 }else{ |
1337 sqlite3GenerateRowIndexDelete(pParse, pTab, iDataCur, iIdxCur, 0); | 1351 if( pTab->pIndex ){ |
| 1352 sqlite3MultiWrite(pParse); |
| 1353 sqlite3GenerateRowIndexDelete(pParse, pTab, iDataCur, iIdxCur,0,-1); |
| 1354 } |
1338 } | 1355 } |
1339 seenReplace = 1; | 1356 seenReplace = 1; |
1340 break; | 1357 break; |
1341 } | 1358 } |
1342 case OE_Ignore: { | 1359 case OE_Ignore: { |
1343 /*assert( seenReplace==0 );*/ | 1360 /*assert( seenReplace==0 );*/ |
1344 sqlite3VdbeAddOp2(v, OP_Goto, 0, ignoreDest); | 1361 sqlite3VdbeGoto(v, ignoreDest); |
1345 break; | 1362 break; |
1346 } | 1363 } |
1347 } | 1364 } |
1348 sqlite3VdbeResolveLabel(v, addrRowidOk); | 1365 sqlite3VdbeResolveLabel(v, addrRowidOk); |
1349 if( ipkTop ){ | 1366 if( ipkTop ){ |
1350 ipkBottom = sqlite3VdbeAddOp0(v, OP_Goto); | 1367 ipkBottom = sqlite3VdbeAddOp0(v, OP_Goto); |
1351 sqlite3VdbeJumpHere(v, ipkTop); | 1368 sqlite3VdbeJumpHere(v, ipkTop); |
1352 } | 1369 } |
1353 } | 1370 } |
1354 | 1371 |
(...skipping 15 matching lines...) Expand all Loading... |
1370 sqlite3TableAffinity(v, pTab, regNewData+1); | 1387 sqlite3TableAffinity(v, pTab, regNewData+1); |
1371 bAffinityDone = 1; | 1388 bAffinityDone = 1; |
1372 } | 1389 } |
1373 iThisCur = iIdxCur+ix; | 1390 iThisCur = iIdxCur+ix; |
1374 addrUniqueOk = sqlite3VdbeMakeLabel(v); | 1391 addrUniqueOk = sqlite3VdbeMakeLabel(v); |
1375 | 1392 |
1376 /* Skip partial indices for which the WHERE clause is not true */ | 1393 /* Skip partial indices for which the WHERE clause is not true */ |
1377 if( pIdx->pPartIdxWhere ){ | 1394 if( pIdx->pPartIdxWhere ){ |
1378 sqlite3VdbeAddOp2(v, OP_Null, 0, aRegIdx[ix]); | 1395 sqlite3VdbeAddOp2(v, OP_Null, 0, aRegIdx[ix]); |
1379 pParse->ckBase = regNewData+1; | 1396 pParse->ckBase = regNewData+1; |
1380 sqlite3ExprIfFalse(pParse, pIdx->pPartIdxWhere, addrUniqueOk, | 1397 sqlite3ExprIfFalseDup(pParse, pIdx->pPartIdxWhere, addrUniqueOk, |
1381 SQLITE_JUMPIFNULL); | 1398 SQLITE_JUMPIFNULL); |
1382 pParse->ckBase = 0; | 1399 pParse->ckBase = 0; |
1383 } | 1400 } |
1384 | 1401 |
1385 /* Create a record for this index entry as it should appear after | 1402 /* Create a record for this index entry as it should appear after |
1386 ** the insert or update. Store that record in the aRegIdx[ix] register | 1403 ** the insert or update. Store that record in the aRegIdx[ix] register |
1387 */ | 1404 */ |
1388 regIdx = sqlite3GetTempRange(pParse, pIdx->nColumn); | 1405 regIdx = sqlite3GetTempRange(pParse, pIdx->nColumn); |
1389 for(i=0; i<pIdx->nColumn; i++){ | 1406 for(i=0; i<pIdx->nColumn; i++){ |
1390 int iField = pIdx->aiColumn[i]; | 1407 int iField = pIdx->aiColumn[i]; |
1391 int x; | 1408 int x; |
1392 if( iField<0 || iField==pTab->iPKey ){ | 1409 if( iField==XN_EXPR ){ |
1393 if( regRowid==regIdx+i ) continue; /* ROWID already in regIdx+i */ | 1410 pParse->ckBase = regNewData+1; |
1394 x = regNewData; | 1411 sqlite3ExprCodeCopy(pParse, pIdx->aColExpr->a[i].pExpr, regIdx+i); |
1395 regRowid = pIdx->pPartIdxWhere ? -1 : regIdx+i; | 1412 pParse->ckBase = 0; |
| 1413 VdbeComment((v, "%s column %d", pIdx->zName, i)); |
1396 }else{ | 1414 }else{ |
1397 x = iField + regNewData + 1; | 1415 if( iField==XN_ROWID || iField==pTab->iPKey ){ |
| 1416 if( regRowid==regIdx+i ) continue; /* ROWID already in regIdx+i */ |
| 1417 x = regNewData; |
| 1418 regRowid = pIdx->pPartIdxWhere ? -1 : regIdx+i; |
| 1419 }else{ |
| 1420 x = iField + regNewData + 1; |
| 1421 } |
| 1422 sqlite3VdbeAddOp2(v, iField<0 ? OP_IntCopy : OP_SCopy, x, regIdx+i); |
| 1423 VdbeComment((v, "%s", iField<0 ? "rowid" : pTab->aCol[iField].zName)); |
1398 } | 1424 } |
1399 sqlite3VdbeAddOp2(v, OP_SCopy, x, regIdx+i); | |
1400 VdbeComment((v, "%s", iField<0 ? "rowid" : pTab->aCol[iField].zName)); | |
1401 } | 1425 } |
1402 sqlite3VdbeAddOp3(v, OP_MakeRecord, regIdx, pIdx->nColumn, aRegIdx[ix]); | 1426 sqlite3VdbeAddOp3(v, OP_MakeRecord, regIdx, pIdx->nColumn, aRegIdx[ix]); |
1403 VdbeComment((v, "for %s", pIdx->zName)); | 1427 VdbeComment((v, "for %s", pIdx->zName)); |
1404 sqlite3ExprCacheAffinityChange(pParse, regIdx, pIdx->nColumn); | 1428 sqlite3ExprCacheAffinityChange(pParse, regIdx, pIdx->nColumn); |
1405 | 1429 |
1406 /* In an UPDATE operation, if this index is the PRIMARY KEY index | 1430 /* In an UPDATE operation, if this index is the PRIMARY KEY index |
1407 ** of a WITHOUT ROWID table and there has been no change the | 1431 ** of a WITHOUT ROWID table and there has been no change the |
1408 ** primary key, then no collision is possible. The collision detection | 1432 ** primary key, then no collision is possible. The collision detection |
1409 ** logic below can all be skipped. */ | 1433 ** logic below can all be skipped. */ |
1410 if( isUpdate && pPk==pIdx && pkChng==0 ){ | 1434 if( isUpdate && pPk==pIdx && pkChng==0 ){ |
(...skipping 29 matching lines...) Expand all Loading... |
1440 sqlite3VdbeAddOp3(v, OP_Eq, regR, addrUniqueOk, regOldData); | 1464 sqlite3VdbeAddOp3(v, OP_Eq, regR, addrUniqueOk, regOldData); |
1441 sqlite3VdbeChangeP5(v, SQLITE_NOTNULL); | 1465 sqlite3VdbeChangeP5(v, SQLITE_NOTNULL); |
1442 VdbeCoverage(v); | 1466 VdbeCoverage(v); |
1443 } | 1467 } |
1444 }else{ | 1468 }else{ |
1445 int x; | 1469 int x; |
1446 /* Extract the PRIMARY KEY from the end of the index entry and | 1470 /* Extract the PRIMARY KEY from the end of the index entry and |
1447 ** store it in registers regR..regR+nPk-1 */ | 1471 ** store it in registers regR..regR+nPk-1 */ |
1448 if( pIdx!=pPk ){ | 1472 if( pIdx!=pPk ){ |
1449 for(i=0; i<pPk->nKeyCol; i++){ | 1473 for(i=0; i<pPk->nKeyCol; i++){ |
| 1474 assert( pPk->aiColumn[i]>=0 ); |
1450 x = sqlite3ColumnOfIndex(pIdx, pPk->aiColumn[i]); | 1475 x = sqlite3ColumnOfIndex(pIdx, pPk->aiColumn[i]); |
1451 sqlite3VdbeAddOp3(v, OP_Column, iThisCur, x, regR+i); | 1476 sqlite3VdbeAddOp3(v, OP_Column, iThisCur, x, regR+i); |
1452 VdbeComment((v, "%s.%s", pTab->zName, | 1477 VdbeComment((v, "%s.%s", pTab->zName, |
1453 pTab->aCol[pPk->aiColumn[i]].zName)); | 1478 pTab->aCol[pPk->aiColumn[i]].zName)); |
1454 } | 1479 } |
1455 } | 1480 } |
1456 if( isUpdate ){ | 1481 if( isUpdate ){ |
1457 /* If currently processing the PRIMARY KEY of a WITHOUT ROWID | 1482 /* If currently processing the PRIMARY KEY of a WITHOUT ROWID |
1458 ** table, only conflict if the new PRIMARY KEY values are actually | 1483 ** table, only conflict if the new PRIMARY KEY values are actually |
1459 ** different from the old. | 1484 ** different from the old. |
1460 ** | 1485 ** |
1461 ** For a UNIQUE index, only conflict if the PRIMARY KEY values | 1486 ** For a UNIQUE index, only conflict if the PRIMARY KEY values |
1462 ** of the matched index row are different from the original PRIMARY | 1487 ** of the matched index row are different from the original PRIMARY |
1463 ** KEY values of this row before the update. */ | 1488 ** KEY values of this row before the update. */ |
1464 int addrJump = sqlite3VdbeCurrentAddr(v)+pPk->nKeyCol; | 1489 int addrJump = sqlite3VdbeCurrentAddr(v)+pPk->nKeyCol; |
1465 int op = OP_Ne; | 1490 int op = OP_Ne; |
1466 int regCmp = (IsPrimaryKeyIndex(pIdx) ? regIdx : regR); | 1491 int regCmp = (IsPrimaryKeyIndex(pIdx) ? regIdx : regR); |
1467 | 1492 |
1468 for(i=0; i<pPk->nKeyCol; i++){ | 1493 for(i=0; i<pPk->nKeyCol; i++){ |
1469 char *p4 = (char*)sqlite3LocateCollSeq(pParse, pPk->azColl[i]); | 1494 char *p4 = (char*)sqlite3LocateCollSeq(pParse, pPk->azColl[i]); |
1470 x = pPk->aiColumn[i]; | 1495 x = pPk->aiColumn[i]; |
| 1496 assert( x>=0 ); |
1471 if( i==(pPk->nKeyCol-1) ){ | 1497 if( i==(pPk->nKeyCol-1) ){ |
1472 addrJump = addrUniqueOk; | 1498 addrJump = addrUniqueOk; |
1473 op = OP_Eq; | 1499 op = OP_Eq; |
1474 } | 1500 } |
1475 sqlite3VdbeAddOp4(v, op, | 1501 sqlite3VdbeAddOp4(v, op, |
1476 regOldData+1+x, addrJump, regCmp+i, p4, P4_COLLSEQ | 1502 regOldData+1+x, addrJump, regCmp+i, p4, P4_COLLSEQ |
1477 ); | 1503 ); |
1478 sqlite3VdbeChangeP5(v, SQLITE_NOTNULL); | 1504 sqlite3VdbeChangeP5(v, SQLITE_NOTNULL); |
1479 VdbeCoverageIf(v, op==OP_Eq); | 1505 VdbeCoverageIf(v, op==OP_Eq); |
1480 VdbeCoverageIf(v, op==OP_Ne); | 1506 VdbeCoverageIf(v, op==OP_Ne); |
1481 } | 1507 } |
1482 } | 1508 } |
1483 } | 1509 } |
1484 } | 1510 } |
1485 | 1511 |
1486 /* Generate code that executes if the new index entry is not unique */ | 1512 /* Generate code that executes if the new index entry is not unique */ |
1487 assert( onError==OE_Rollback || onError==OE_Abort || onError==OE_Fail | 1513 assert( onError==OE_Rollback || onError==OE_Abort || onError==OE_Fail |
1488 || onError==OE_Ignore || onError==OE_Replace ); | 1514 || onError==OE_Ignore || onError==OE_Replace ); |
1489 switch( onError ){ | 1515 switch( onError ){ |
1490 case OE_Rollback: | 1516 case OE_Rollback: |
1491 case OE_Abort: | 1517 case OE_Abort: |
1492 case OE_Fail: { | 1518 case OE_Fail: { |
1493 sqlite3UniqueConstraint(pParse, onError, pIdx); | 1519 sqlite3UniqueConstraint(pParse, onError, pIdx); |
1494 break; | 1520 break; |
1495 } | 1521 } |
1496 case OE_Ignore: { | 1522 case OE_Ignore: { |
1497 sqlite3VdbeAddOp2(v, OP_Goto, 0, ignoreDest); | 1523 sqlite3VdbeGoto(v, ignoreDest); |
1498 break; | 1524 break; |
1499 } | 1525 } |
1500 default: { | 1526 default: { |
1501 Trigger *pTrigger = 0; | 1527 Trigger *pTrigger = 0; |
1502 assert( onError==OE_Replace ); | 1528 assert( onError==OE_Replace ); |
1503 sqlite3MultiWrite(pParse); | 1529 sqlite3MultiWrite(pParse); |
1504 if( db->flags&SQLITE_RecTriggers ){ | 1530 if( db->flags&SQLITE_RecTriggers ){ |
1505 pTrigger = sqlite3TriggersExist(pParse, pTab, TK_DELETE, 0, 0); | 1531 pTrigger = sqlite3TriggersExist(pParse, pTab, TK_DELETE, 0, 0); |
1506 } | 1532 } |
1507 sqlite3GenerateRowDelete(pParse, pTab, pTrigger, iDataCur, iIdxCur, | 1533 sqlite3GenerateRowDelete(pParse, pTab, pTrigger, iDataCur, iIdxCur, |
1508 regR, nPkField, 0, OE_Replace, pIdx==pPk); | 1534 regR, nPkField, 0, OE_Replace, |
| 1535 (pIdx==pPk ? ONEPASS_SINGLE : ONEPASS_OFF), -1); |
1509 seenReplace = 1; | 1536 seenReplace = 1; |
1510 break; | 1537 break; |
1511 } | 1538 } |
1512 } | 1539 } |
1513 sqlite3VdbeResolveLabel(v, addrUniqueOk); | 1540 sqlite3VdbeResolveLabel(v, addrUniqueOk); |
1514 sqlite3ReleaseTempRange(pParse, regIdx, pIdx->nColumn); | 1541 sqlite3ReleaseTempRange(pParse, regIdx, pIdx->nColumn); |
1515 if( regR!=regIdx ) sqlite3ReleaseTempRange(pParse, regR, nPkField); | 1542 if( regR!=regIdx ) sqlite3ReleaseTempRange(pParse, regR, nPkField); |
1516 } | 1543 } |
1517 if( ipkTop ){ | 1544 if( ipkTop ){ |
1518 sqlite3VdbeAddOp2(v, OP_Goto, 0, ipkTop+1); | 1545 sqlite3VdbeGoto(v, ipkTop+1); |
1519 sqlite3VdbeJumpHere(v, ipkBottom); | 1546 sqlite3VdbeJumpHere(v, ipkBottom); |
1520 } | 1547 } |
1521 | 1548 |
1522 *pbMayReplace = seenReplace; | 1549 *pbMayReplace = seenReplace; |
1523 VdbeModuleComment((v, "END: GenCnstCks(%d)", seenReplace)); | 1550 VdbeModuleComment((v, "END: GenCnstCks(%d)", seenReplace)); |
1524 } | 1551 } |
1525 | 1552 |
1526 /* | 1553 /* |
1527 ** This routine generates code to finish the INSERT or UPDATE operation | 1554 ** This routine generates code to finish the INSERT or UPDATE operation |
1528 ** that was started by a prior call to sqlite3GenerateConstraintChecks. | 1555 ** that was started by a prior call to sqlite3GenerateConstraintChecks. |
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1613 ** of *piIdxCurs, depending on where the PRIMARY KEY index appears on the | 1640 ** of *piIdxCurs, depending on where the PRIMARY KEY index appears on the |
1614 ** pTab->pIndex list. | 1641 ** pTab->pIndex list. |
1615 ** | 1642 ** |
1616 ** If pTab is a virtual table, then this routine is a no-op and the | 1643 ** If pTab is a virtual table, then this routine is a no-op and the |
1617 ** *piDataCur and *piIdxCur values are left uninitialized. | 1644 ** *piDataCur and *piIdxCur values are left uninitialized. |
1618 */ | 1645 */ |
1619 int sqlite3OpenTableAndIndices( | 1646 int sqlite3OpenTableAndIndices( |
1620 Parse *pParse, /* Parsing context */ | 1647 Parse *pParse, /* Parsing context */ |
1621 Table *pTab, /* Table to be opened */ | 1648 Table *pTab, /* Table to be opened */ |
1622 int op, /* OP_OpenRead or OP_OpenWrite */ | 1649 int op, /* OP_OpenRead or OP_OpenWrite */ |
| 1650 u8 p5, /* P5 value for OP_Open* instructions */ |
1623 int iBase, /* Use this for the table cursor, if there is one */ | 1651 int iBase, /* Use this for the table cursor, if there is one */ |
1624 u8 *aToOpen, /* If not NULL: boolean for each table and index */ | 1652 u8 *aToOpen, /* If not NULL: boolean for each table and index */ |
1625 int *piDataCur, /* Write the database source cursor number here */ | 1653 int *piDataCur, /* Write the database source cursor number here */ |
1626 int *piIdxCur /* Write the first index cursor number here */ | 1654 int *piIdxCur /* Write the first index cursor number here */ |
1627 ){ | 1655 ){ |
1628 int i; | 1656 int i; |
1629 int iDb; | 1657 int iDb; |
1630 int iDataCur; | 1658 int iDataCur; |
1631 Index *pIdx; | 1659 Index *pIdx; |
1632 Vdbe *v; | 1660 Vdbe *v; |
1633 | 1661 |
1634 assert( op==OP_OpenRead || op==OP_OpenWrite ); | 1662 assert( op==OP_OpenRead || op==OP_OpenWrite ); |
| 1663 assert( op==OP_OpenWrite || p5==0 ); |
1635 if( IsVirtual(pTab) ){ | 1664 if( IsVirtual(pTab) ){ |
1636 /* This routine is a no-op for virtual tables. Leave the output | 1665 /* This routine is a no-op for virtual tables. Leave the output |
1637 ** variables *piDataCur and *piIdxCur uninitialized so that valgrind | 1666 ** variables *piDataCur and *piIdxCur uninitialized so that valgrind |
1638 ** can detect if they are used by mistake in the caller. */ | 1667 ** can detect if they are used by mistake in the caller. */ |
1639 return 0; | 1668 return 0; |
1640 } | 1669 } |
1641 iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema); | 1670 iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema); |
1642 v = sqlite3GetVdbe(pParse); | 1671 v = sqlite3GetVdbe(pParse); |
1643 assert( v!=0 ); | 1672 assert( v!=0 ); |
1644 if( iBase<0 ) iBase = pParse->nTab; | 1673 if( iBase<0 ) iBase = pParse->nTab; |
1645 iDataCur = iBase++; | 1674 iDataCur = iBase++; |
1646 if( piDataCur ) *piDataCur = iDataCur; | 1675 if( piDataCur ) *piDataCur = iDataCur; |
1647 if( HasRowid(pTab) && (aToOpen==0 || aToOpen[0]) ){ | 1676 if( HasRowid(pTab) && (aToOpen==0 || aToOpen[0]) ){ |
1648 sqlite3OpenTable(pParse, iDataCur, iDb, pTab, op); | 1677 sqlite3OpenTable(pParse, iDataCur, iDb, pTab, op); |
1649 }else{ | 1678 }else{ |
1650 sqlite3TableLock(pParse, iDb, pTab->tnum, op==OP_OpenWrite, pTab->zName); | 1679 sqlite3TableLock(pParse, iDb, pTab->tnum, op==OP_OpenWrite, pTab->zName); |
1651 } | 1680 } |
1652 if( piIdxCur ) *piIdxCur = iBase; | 1681 if( piIdxCur ) *piIdxCur = iBase; |
1653 for(i=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){ | 1682 for(i=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){ |
1654 int iIdxCur = iBase++; | 1683 int iIdxCur = iBase++; |
1655 assert( pIdx->pSchema==pTab->pSchema ); | 1684 assert( pIdx->pSchema==pTab->pSchema ); |
1656 if( IsPrimaryKeyIndex(pIdx) && !HasRowid(pTab) && piDataCur ){ | 1685 if( IsPrimaryKeyIndex(pIdx) && !HasRowid(pTab) && piDataCur ){ |
1657 *piDataCur = iIdxCur; | 1686 *piDataCur = iIdxCur; |
1658 } | 1687 } |
1659 if( aToOpen==0 || aToOpen[i+1] ){ | 1688 if( aToOpen==0 || aToOpen[i+1] ){ |
1660 sqlite3VdbeAddOp3(v, op, iIdxCur, pIdx->tnum, iDb); | 1689 sqlite3VdbeAddOp3(v, op, iIdxCur, pIdx->tnum, iDb); |
1661 sqlite3VdbeSetP4KeyInfo(pParse, pIdx); | 1690 sqlite3VdbeSetP4KeyInfo(pParse, pIdx); |
| 1691 sqlite3VdbeChangeP5(v, p5); |
1662 VdbeComment((v, "%s", pIdx->zName)); | 1692 VdbeComment((v, "%s", pIdx->zName)); |
1663 } | 1693 } |
1664 } | 1694 } |
1665 if( iBase>pParse->nTab ) pParse->nTab = iBase; | 1695 if( iBase>pParse->nTab ) pParse->nTab = iBase; |
1666 return i; | 1696 return i; |
1667 } | 1697 } |
1668 | 1698 |
1669 | 1699 |
1670 #ifdef SQLITE_TEST | 1700 #ifdef SQLITE_TEST |
1671 /* | 1701 /* |
1672 ** The following global variable is incremented whenever the | 1702 ** The following global variable is incremented whenever the |
1673 ** transfer optimization is used. This is used for testing | 1703 ** transfer optimization is used. This is used for testing |
1674 ** purposes only - to make sure the transfer optimization really | 1704 ** purposes only - to make sure the transfer optimization really |
1675 ** is happening when it is supposed to. | 1705 ** is happening when it is supposed to. |
1676 */ | 1706 */ |
1677 int sqlite3_xferopt_count; | 1707 int sqlite3_xferopt_count; |
1678 #endif /* SQLITE_TEST */ | 1708 #endif /* SQLITE_TEST */ |
1679 | 1709 |
1680 | 1710 |
1681 #ifndef SQLITE_OMIT_XFER_OPT | 1711 #ifndef SQLITE_OMIT_XFER_OPT |
1682 /* | 1712 /* |
1683 ** Check to collation names to see if they are compatible. | |
1684 */ | |
1685 static int xferCompatibleCollation(const char *z1, const char *z2){ | |
1686 if( z1==0 ){ | |
1687 return z2==0; | |
1688 } | |
1689 if( z2==0 ){ | |
1690 return 0; | |
1691 } | |
1692 return sqlite3StrICmp(z1, z2)==0; | |
1693 } | |
1694 | |
1695 | |
1696 /* | |
1697 ** Check to see if index pSrc is compatible as a source of data | 1713 ** Check to see if index pSrc is compatible as a source of data |
1698 ** for index pDest in an insert transfer optimization. The rules | 1714 ** for index pDest in an insert transfer optimization. The rules |
1699 ** for a compatible index: | 1715 ** for a compatible index: |
1700 ** | 1716 ** |
1701 ** * The index is over the same set of columns | 1717 ** * The index is over the same set of columns |
1702 ** * The same DESC and ASC markings occurs on all columns | 1718 ** * The same DESC and ASC markings occurs on all columns |
1703 ** * The same onError processing (OE_Abort, OE_Ignore, etc) | 1719 ** * The same onError processing (OE_Abort, OE_Ignore, etc) |
1704 ** * The same collating sequence on each column | 1720 ** * The same collating sequence on each column |
1705 ** * The index has the exact same WHERE clause | 1721 ** * The index has the exact same WHERE clause |
1706 */ | 1722 */ |
1707 static int xferCompatibleIndex(Index *pDest, Index *pSrc){ | 1723 static int xferCompatibleIndex(Index *pDest, Index *pSrc){ |
1708 int i; | 1724 int i; |
1709 assert( pDest && pSrc ); | 1725 assert( pDest && pSrc ); |
1710 assert( pDest->pTable!=pSrc->pTable ); | 1726 assert( pDest->pTable!=pSrc->pTable ); |
1711 if( pDest->nKeyCol!=pSrc->nKeyCol ){ | 1727 if( pDest->nKeyCol!=pSrc->nKeyCol ){ |
1712 return 0; /* Different number of columns */ | 1728 return 0; /* Different number of columns */ |
1713 } | 1729 } |
1714 if( pDest->onError!=pSrc->onError ){ | 1730 if( pDest->onError!=pSrc->onError ){ |
1715 return 0; /* Different conflict resolution strategies */ | 1731 return 0; /* Different conflict resolution strategies */ |
1716 } | 1732 } |
1717 for(i=0; i<pSrc->nKeyCol; i++){ | 1733 for(i=0; i<pSrc->nKeyCol; i++){ |
1718 if( pSrc->aiColumn[i]!=pDest->aiColumn[i] ){ | 1734 if( pSrc->aiColumn[i]!=pDest->aiColumn[i] ){ |
1719 return 0; /* Different columns indexed */ | 1735 return 0; /* Different columns indexed */ |
1720 } | 1736 } |
| 1737 if( pSrc->aiColumn[i]==XN_EXPR ){ |
| 1738 assert( pSrc->aColExpr!=0 && pDest->aColExpr!=0 ); |
| 1739 if( sqlite3ExprCompare(pSrc->aColExpr->a[i].pExpr, |
| 1740 pDest->aColExpr->a[i].pExpr, -1)!=0 ){ |
| 1741 return 0; /* Different expressions in the index */ |
| 1742 } |
| 1743 } |
1721 if( pSrc->aSortOrder[i]!=pDest->aSortOrder[i] ){ | 1744 if( pSrc->aSortOrder[i]!=pDest->aSortOrder[i] ){ |
1722 return 0; /* Different sort orders */ | 1745 return 0; /* Different sort orders */ |
1723 } | 1746 } |
1724 if( !xferCompatibleCollation(pSrc->azColl[i],pDest->azColl[i]) ){ | 1747 if( sqlite3_stricmp(pSrc->azColl[i],pDest->azColl[i])!=0 ){ |
1725 return 0; /* Different collating sequences */ | 1748 return 0; /* Different collating sequences */ |
1726 } | 1749 } |
1727 } | 1750 } |
1728 if( sqlite3ExprCompare(pSrc->pPartIdxWhere, pDest->pPartIdxWhere, -1) ){ | 1751 if( sqlite3ExprCompare(pSrc->pPartIdxWhere, pDest->pPartIdxWhere, -1) ){ |
1729 return 0; /* Different WHERE clauses */ | 1752 return 0; /* Different WHERE clauses */ |
1730 } | 1753 } |
1731 | 1754 |
1732 /* If no test above fails then the indices must be compatible */ | 1755 /* If no test above fails then the indices must be compatible */ |
1733 return 1; | 1756 return 1; |
1734 } | 1757 } |
(...skipping 23 matching lines...) Expand all Loading... |
1758 ** | 1781 ** |
1759 ** This optimization is particularly useful at making VACUUM run faster. | 1782 ** This optimization is particularly useful at making VACUUM run faster. |
1760 */ | 1783 */ |
1761 static int xferOptimization( | 1784 static int xferOptimization( |
1762 Parse *pParse, /* Parser context */ | 1785 Parse *pParse, /* Parser context */ |
1763 Table *pDest, /* The table we are inserting into */ | 1786 Table *pDest, /* The table we are inserting into */ |
1764 Select *pSelect, /* A SELECT statement to use as the data source */ | 1787 Select *pSelect, /* A SELECT statement to use as the data source */ |
1765 int onError, /* How to handle constraint errors */ | 1788 int onError, /* How to handle constraint errors */ |
1766 int iDbDest /* The database of pDest */ | 1789 int iDbDest /* The database of pDest */ |
1767 ){ | 1790 ){ |
| 1791 sqlite3 *db = pParse->db; |
1768 ExprList *pEList; /* The result set of the SELECT */ | 1792 ExprList *pEList; /* The result set of the SELECT */ |
1769 Table *pSrc; /* The table in the FROM clause of SELECT */ | 1793 Table *pSrc; /* The table in the FROM clause of SELECT */ |
1770 Index *pSrcIdx, *pDestIdx; /* Source and destination indices */ | 1794 Index *pSrcIdx, *pDestIdx; /* Source and destination indices */ |
1771 struct SrcList_item *pItem; /* An element of pSelect->pSrc */ | 1795 struct SrcList_item *pItem; /* An element of pSelect->pSrc */ |
1772 int i; /* Loop counter */ | 1796 int i; /* Loop counter */ |
1773 int iDbSrc; /* The database of pSrc */ | 1797 int iDbSrc; /* The database of pSrc */ |
1774 int iSrc, iDest; /* Cursors from source and destination */ | 1798 int iSrc, iDest; /* Cursors from source and destination */ |
1775 int addr1, addr2; /* Loop addresses */ | 1799 int addr1, addr2; /* Loop addresses */ |
1776 int emptyDestTest = 0; /* Address of test for empty pDest */ | 1800 int emptyDestTest = 0; /* Address of test for empty pDest */ |
1777 int emptySrcTest = 0; /* Address of test for empty pSrc */ | 1801 int emptySrcTest = 0; /* Address of test for empty pSrc */ |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1828 } | 1852 } |
1829 if( pSelect->selFlags & SF_Distinct ){ | 1853 if( pSelect->selFlags & SF_Distinct ){ |
1830 return 0; /* SELECT may not be DISTINCT */ | 1854 return 0; /* SELECT may not be DISTINCT */ |
1831 } | 1855 } |
1832 pEList = pSelect->pEList; | 1856 pEList = pSelect->pEList; |
1833 assert( pEList!=0 ); | 1857 assert( pEList!=0 ); |
1834 if( pEList->nExpr!=1 ){ | 1858 if( pEList->nExpr!=1 ){ |
1835 return 0; /* The result set must have exactly one column */ | 1859 return 0; /* The result set must have exactly one column */ |
1836 } | 1860 } |
1837 assert( pEList->a[0].pExpr ); | 1861 assert( pEList->a[0].pExpr ); |
1838 if( pEList->a[0].pExpr->op!=TK_ALL ){ | 1862 if( pEList->a[0].pExpr->op!=TK_ASTERISK ){ |
1839 return 0; /* The result set must be the special operator "*" */ | 1863 return 0; /* The result set must be the special operator "*" */ |
1840 } | 1864 } |
1841 | 1865 |
1842 /* At this point we have established that the statement is of the | 1866 /* At this point we have established that the statement is of the |
1843 ** correct syntactic form to participate in this optimization. Now | 1867 ** correct syntactic form to participate in this optimization. Now |
1844 ** we have to check the semantics. | 1868 ** we have to check the semantics. |
1845 */ | 1869 */ |
1846 pItem = pSelect->pSrc->a; | 1870 pItem = pSelect->pSrc->a; |
1847 pSrc = sqlite3LocateTableItem(pParse, 0, pItem); | 1871 pSrc = sqlite3LocateTableItem(pParse, 0, pItem); |
1848 if( pSrc==0 ){ | 1872 if( pSrc==0 ){ |
(...skipping 15 matching lines...) Expand all Loading... |
1864 } | 1888 } |
1865 if( pDest->nCol!=pSrc->nCol ){ | 1889 if( pDest->nCol!=pSrc->nCol ){ |
1866 return 0; /* Number of columns must be the same in tab1 and tab2 */ | 1890 return 0; /* Number of columns must be the same in tab1 and tab2 */ |
1867 } | 1891 } |
1868 if( pDest->iPKey!=pSrc->iPKey ){ | 1892 if( pDest->iPKey!=pSrc->iPKey ){ |
1869 return 0; /* Both tables must have the same INTEGER PRIMARY KEY */ | 1893 return 0; /* Both tables must have the same INTEGER PRIMARY KEY */ |
1870 } | 1894 } |
1871 for(i=0; i<pDest->nCol; i++){ | 1895 for(i=0; i<pDest->nCol; i++){ |
1872 Column *pDestCol = &pDest->aCol[i]; | 1896 Column *pDestCol = &pDest->aCol[i]; |
1873 Column *pSrcCol = &pSrc->aCol[i]; | 1897 Column *pSrcCol = &pSrc->aCol[i]; |
| 1898 #ifdef SQLITE_ENABLE_HIDDEN_COLUMNS |
| 1899 if( (db->flags & SQLITE_Vacuum)==0 |
| 1900 && (pDestCol->colFlags | pSrcCol->colFlags) & COLFLAG_HIDDEN |
| 1901 ){ |
| 1902 return 0; /* Neither table may have __hidden__ columns */ |
| 1903 } |
| 1904 #endif |
1874 if( pDestCol->affinity!=pSrcCol->affinity ){ | 1905 if( pDestCol->affinity!=pSrcCol->affinity ){ |
1875 return 0; /* Affinity must be the same on all columns */ | 1906 return 0; /* Affinity must be the same on all columns */ |
1876 } | 1907 } |
1877 if( !xferCompatibleCollation(pDestCol->zColl, pSrcCol->zColl) ){ | 1908 if( sqlite3_stricmp(pDestCol->zColl, pSrcCol->zColl)!=0 ){ |
1878 return 0; /* Collating sequence must be the same on all columns */ | 1909 return 0; /* Collating sequence must be the same on all columns */ |
1879 } | 1910 } |
1880 if( pDestCol->notNull && !pSrcCol->notNull ){ | 1911 if( pDestCol->notNull && !pSrcCol->notNull ){ |
1881 return 0; /* tab2 must be NOT NULL if tab1 is */ | 1912 return 0; /* tab2 must be NOT NULL if tab1 is */ |
1882 } | 1913 } |
1883 /* Default values for second and subsequent columns need to match. */ | 1914 /* Default values for second and subsequent columns need to match. */ |
1884 if( i>0 | 1915 if( i>0 |
1885 && ((pDestCol->zDflt==0)!=(pSrcCol->zDflt==0) | 1916 && ((pDestCol->zDflt==0)!=(pSrcCol->zDflt==0) |
1886 || (pDestCol->zDflt && strcmp(pDestCol->zDflt, pSrcCol->zDflt)!=0)) | 1917 || (pDestCol->zDflt && strcmp(pDestCol->zDflt, pSrcCol->zDflt)!=0)) |
1887 ){ | 1918 ){ |
(...skipping 17 matching lines...) Expand all Loading... |
1905 } | 1936 } |
1906 #endif | 1937 #endif |
1907 #ifndef SQLITE_OMIT_FOREIGN_KEY | 1938 #ifndef SQLITE_OMIT_FOREIGN_KEY |
1908 /* Disallow the transfer optimization if the destination table constains | 1939 /* Disallow the transfer optimization if the destination table constains |
1909 ** any foreign key constraints. This is more restrictive than necessary. | 1940 ** any foreign key constraints. This is more restrictive than necessary. |
1910 ** But the main beneficiary of the transfer optimization is the VACUUM | 1941 ** But the main beneficiary of the transfer optimization is the VACUUM |
1911 ** command, and the VACUUM command disables foreign key constraints. So | 1942 ** command, and the VACUUM command disables foreign key constraints. So |
1912 ** the extra complication to make this rule less restrictive is probably | 1943 ** the extra complication to make this rule less restrictive is probably |
1913 ** not worth the effort. Ticket [6284df89debdfa61db8073e062908af0c9b6118e] | 1944 ** not worth the effort. Ticket [6284df89debdfa61db8073e062908af0c9b6118e] |
1914 */ | 1945 */ |
1915 if( (pParse->db->flags & SQLITE_ForeignKeys)!=0 && pDest->pFKey!=0 ){ | 1946 if( (db->flags & SQLITE_ForeignKeys)!=0 && pDest->pFKey!=0 ){ |
1916 return 0; | 1947 return 0; |
1917 } | 1948 } |
1918 #endif | 1949 #endif |
1919 if( (pParse->db->flags & SQLITE_CountRows)!=0 ){ | 1950 if( (db->flags & SQLITE_CountRows)!=0 ){ |
1920 return 0; /* xfer opt does not play well with PRAGMA count_changes */ | 1951 return 0; /* xfer opt does not play well with PRAGMA count_changes */ |
1921 } | 1952 } |
1922 | 1953 |
1923 /* If we get this far, it means that the xfer optimization is at | 1954 /* If we get this far, it means that the xfer optimization is at |
1924 ** least a possibility, though it might only work if the destination | 1955 ** least a possibility, though it might only work if the destination |
1925 ** table (tab1) is initially empty. | 1956 ** table (tab1) is initially empty. |
1926 */ | 1957 */ |
1927 #ifdef SQLITE_TEST | 1958 #ifdef SQLITE_TEST |
1928 sqlite3_xferopt_count++; | 1959 sqlite3_xferopt_count++; |
1929 #endif | 1960 #endif |
1930 iDbSrc = sqlite3SchemaToIndex(pParse->db, pSrc->pSchema); | 1961 iDbSrc = sqlite3SchemaToIndex(db, pSrc->pSchema); |
1931 v = sqlite3GetVdbe(pParse); | 1962 v = sqlite3GetVdbe(pParse); |
1932 sqlite3CodeVerifySchema(pParse, iDbSrc); | 1963 sqlite3CodeVerifySchema(pParse, iDbSrc); |
1933 iSrc = pParse->nTab++; | 1964 iSrc = pParse->nTab++; |
1934 iDest = pParse->nTab++; | 1965 iDest = pParse->nTab++; |
1935 regAutoinc = autoIncBegin(pParse, iDbDest, pDest); | 1966 regAutoinc = autoIncBegin(pParse, iDbDest, pDest); |
1936 regData = sqlite3GetTempReg(pParse); | 1967 regData = sqlite3GetTempReg(pParse); |
1937 regRowid = sqlite3GetTempReg(pParse); | 1968 regRowid = sqlite3GetTempReg(pParse); |
1938 sqlite3OpenTable(pParse, iDest, iDbDest, pDest, OP_OpenWrite); | 1969 sqlite3OpenTable(pParse, iDest, iDbDest, pDest, OP_OpenWrite); |
1939 assert( HasRowid(pDest) || destHasUniqueIdx ); | 1970 assert( HasRowid(pDest) || destHasUniqueIdx ); |
1940 if( (pDest->iPKey<0 && pDest->pIndex!=0) /* (1) */ | 1971 if( (db->flags & SQLITE_Vacuum)==0 && ( |
| 1972 (pDest->iPKey<0 && pDest->pIndex!=0) /* (1) */ |
1941 || destHasUniqueIdx /* (2) */ | 1973 || destHasUniqueIdx /* (2) */ |
1942 || (onError!=OE_Abort && onError!=OE_Rollback) /* (3) */ | 1974 || (onError!=OE_Abort && onError!=OE_Rollback) /* (3) */ |
1943 ){ | 1975 )){ |
1944 /* In some circumstances, we are able to run the xfer optimization | 1976 /* In some circumstances, we are able to run the xfer optimization |
1945 ** only if the destination table is initially empty. This code makes | 1977 ** only if the destination table is initially empty. Unless the |
1946 ** that determination. Conditions under which the destination must | 1978 ** SQLITE_Vacuum flag is set, this block generates code to make |
1947 ** be empty: | 1979 ** that determination. If SQLITE_Vacuum is set, then the destination |
| 1980 ** table is always empty. |
| 1981 ** |
| 1982 ** Conditions under which the destination must be empty: |
1948 ** | 1983 ** |
1949 ** (1) There is no INTEGER PRIMARY KEY but there are indices. | 1984 ** (1) There is no INTEGER PRIMARY KEY but there are indices. |
1950 ** (If the destination is not initially empty, the rowid fields | 1985 ** (If the destination is not initially empty, the rowid fields |
1951 ** of index entries might need to change.) | 1986 ** of index entries might need to change.) |
1952 ** | 1987 ** |
1953 ** (2) The destination has a unique index. (The xfer optimization | 1988 ** (2) The destination has a unique index. (The xfer optimization |
1954 ** is unable to test uniqueness.) | 1989 ** is unable to test uniqueness.) |
1955 ** | 1990 ** |
1956 ** (3) onError is something other than OE_Abort and OE_Rollback. | 1991 ** (3) onError is something other than OE_Abort and OE_Rollback. |
1957 */ | 1992 */ |
1958 addr1 = sqlite3VdbeAddOp2(v, OP_Rewind, iDest, 0); VdbeCoverage(v); | 1993 addr1 = sqlite3VdbeAddOp2(v, OP_Rewind, iDest, 0); VdbeCoverage(v); |
1959 emptyDestTest = sqlite3VdbeAddOp2(v, OP_Goto, 0, 0); | 1994 emptyDestTest = sqlite3VdbeAddOp0(v, OP_Goto); |
1960 sqlite3VdbeJumpHere(v, addr1); | 1995 sqlite3VdbeJumpHere(v, addr1); |
1961 } | 1996 } |
1962 if( HasRowid(pSrc) ){ | 1997 if( HasRowid(pSrc) ){ |
1963 sqlite3OpenTable(pParse, iSrc, iDbSrc, pSrc, OP_OpenRead); | 1998 sqlite3OpenTable(pParse, iSrc, iDbSrc, pSrc, OP_OpenRead); |
1964 emptySrcTest = sqlite3VdbeAddOp2(v, OP_Rewind, iSrc, 0); VdbeCoverage(v); | 1999 emptySrcTest = sqlite3VdbeAddOp2(v, OP_Rewind, iSrc, 0); VdbeCoverage(v); |
1965 if( pDest->iPKey>=0 ){ | 2000 if( pDest->iPKey>=0 ){ |
1966 addr1 = sqlite3VdbeAddOp2(v, OP_Rowid, iSrc, regRowid); | 2001 addr1 = sqlite3VdbeAddOp2(v, OP_Rowid, iSrc, regRowid); |
1967 addr2 = sqlite3VdbeAddOp3(v, OP_NotExists, iDest, 0, regRowid); | 2002 addr2 = sqlite3VdbeAddOp3(v, OP_NotExists, iDest, 0, regRowid); |
1968 VdbeCoverage(v); | 2003 VdbeCoverage(v); |
1969 sqlite3RowidConstraint(pParse, onError, pDest); | 2004 sqlite3RowidConstraint(pParse, onError, pDest); |
(...skipping 10 matching lines...) Expand all Loading... |
1980 sqlite3VdbeChangeP5(v, OPFLAG_NCHANGE|OPFLAG_LASTROWID|OPFLAG_APPEND); | 2015 sqlite3VdbeChangeP5(v, OPFLAG_NCHANGE|OPFLAG_LASTROWID|OPFLAG_APPEND); |
1981 sqlite3VdbeChangeP4(v, -1, pDest->zName, 0); | 2016 sqlite3VdbeChangeP4(v, -1, pDest->zName, 0); |
1982 sqlite3VdbeAddOp2(v, OP_Next, iSrc, addr1); VdbeCoverage(v); | 2017 sqlite3VdbeAddOp2(v, OP_Next, iSrc, addr1); VdbeCoverage(v); |
1983 sqlite3VdbeAddOp2(v, OP_Close, iSrc, 0); | 2018 sqlite3VdbeAddOp2(v, OP_Close, iSrc, 0); |
1984 sqlite3VdbeAddOp2(v, OP_Close, iDest, 0); | 2019 sqlite3VdbeAddOp2(v, OP_Close, iDest, 0); |
1985 }else{ | 2020 }else{ |
1986 sqlite3TableLock(pParse, iDbDest, pDest->tnum, 1, pDest->zName); | 2021 sqlite3TableLock(pParse, iDbDest, pDest->tnum, 1, pDest->zName); |
1987 sqlite3TableLock(pParse, iDbSrc, pSrc->tnum, 0, pSrc->zName); | 2022 sqlite3TableLock(pParse, iDbSrc, pSrc->tnum, 0, pSrc->zName); |
1988 } | 2023 } |
1989 for(pDestIdx=pDest->pIndex; pDestIdx; pDestIdx=pDestIdx->pNext){ | 2024 for(pDestIdx=pDest->pIndex; pDestIdx; pDestIdx=pDestIdx->pNext){ |
| 2025 u8 idxInsFlags = 0; |
1990 for(pSrcIdx=pSrc->pIndex; ALWAYS(pSrcIdx); pSrcIdx=pSrcIdx->pNext){ | 2026 for(pSrcIdx=pSrc->pIndex; ALWAYS(pSrcIdx); pSrcIdx=pSrcIdx->pNext){ |
1991 if( xferCompatibleIndex(pDestIdx, pSrcIdx) ) break; | 2027 if( xferCompatibleIndex(pDestIdx, pSrcIdx) ) break; |
1992 } | 2028 } |
1993 assert( pSrcIdx ); | 2029 assert( pSrcIdx ); |
1994 sqlite3VdbeAddOp3(v, OP_OpenRead, iSrc, pSrcIdx->tnum, iDbSrc); | 2030 sqlite3VdbeAddOp3(v, OP_OpenRead, iSrc, pSrcIdx->tnum, iDbSrc); |
1995 sqlite3VdbeSetP4KeyInfo(pParse, pSrcIdx); | 2031 sqlite3VdbeSetP4KeyInfo(pParse, pSrcIdx); |
1996 VdbeComment((v, "%s", pSrcIdx->zName)); | 2032 VdbeComment((v, "%s", pSrcIdx->zName)); |
1997 sqlite3VdbeAddOp3(v, OP_OpenWrite, iDest, pDestIdx->tnum, iDbDest); | 2033 sqlite3VdbeAddOp3(v, OP_OpenWrite, iDest, pDestIdx->tnum, iDbDest); |
1998 sqlite3VdbeSetP4KeyInfo(pParse, pDestIdx); | 2034 sqlite3VdbeSetP4KeyInfo(pParse, pDestIdx); |
1999 sqlite3VdbeChangeP5(v, OPFLAG_BULKCSR); | 2035 sqlite3VdbeChangeP5(v, OPFLAG_BULKCSR); |
2000 VdbeComment((v, "%s", pDestIdx->zName)); | 2036 VdbeComment((v, "%s", pDestIdx->zName)); |
2001 addr1 = sqlite3VdbeAddOp2(v, OP_Rewind, iSrc, 0); VdbeCoverage(v); | 2037 addr1 = sqlite3VdbeAddOp2(v, OP_Rewind, iSrc, 0); VdbeCoverage(v); |
2002 sqlite3VdbeAddOp2(v, OP_RowKey, iSrc, regData); | 2038 sqlite3VdbeAddOp2(v, OP_RowKey, iSrc, regData); |
| 2039 if( db->flags & SQLITE_Vacuum ){ |
| 2040 /* This INSERT command is part of a VACUUM operation, which guarantees |
| 2041 ** that the destination table is empty. If all indexed columns use |
| 2042 ** collation sequence BINARY, then it can also be assumed that the |
| 2043 ** index will be populated by inserting keys in strictly sorted |
| 2044 ** order. In this case, instead of seeking within the b-tree as part |
| 2045 ** of every OP_IdxInsert opcode, an OP_Last is added before the |
| 2046 ** OP_IdxInsert to seek to the point within the b-tree where each key |
| 2047 ** should be inserted. This is faster. |
| 2048 ** |
| 2049 ** If any of the indexed columns use a collation sequence other than |
| 2050 ** BINARY, this optimization is disabled. This is because the user |
| 2051 ** might change the definition of a collation sequence and then run |
| 2052 ** a VACUUM command. In that case keys may not be written in strictly |
| 2053 ** sorted order. */ |
| 2054 for(i=0; i<pSrcIdx->nColumn; i++){ |
| 2055 const char *zColl = pSrcIdx->azColl[i]; |
| 2056 assert( sqlite3_stricmp(sqlite3StrBINARY, zColl)!=0 |
| 2057 || sqlite3StrBINARY==zColl ); |
| 2058 if( sqlite3_stricmp(sqlite3StrBINARY, zColl) ) break; |
| 2059 } |
| 2060 if( i==pSrcIdx->nColumn ){ |
| 2061 idxInsFlags = OPFLAG_USESEEKRESULT; |
| 2062 sqlite3VdbeAddOp3(v, OP_Last, iDest, 0, -1); |
| 2063 } |
| 2064 } |
| 2065 if( !HasRowid(pSrc) && pDestIdx->idxType==2 ){ |
| 2066 idxInsFlags |= OPFLAG_NCHANGE; |
| 2067 } |
2003 sqlite3VdbeAddOp3(v, OP_IdxInsert, iDest, regData, 1); | 2068 sqlite3VdbeAddOp3(v, OP_IdxInsert, iDest, regData, 1); |
| 2069 sqlite3VdbeChangeP5(v, idxInsFlags); |
2004 sqlite3VdbeAddOp2(v, OP_Next, iSrc, addr1+1); VdbeCoverage(v); | 2070 sqlite3VdbeAddOp2(v, OP_Next, iSrc, addr1+1); VdbeCoverage(v); |
2005 sqlite3VdbeJumpHere(v, addr1); | 2071 sqlite3VdbeJumpHere(v, addr1); |
2006 sqlite3VdbeAddOp2(v, OP_Close, iSrc, 0); | 2072 sqlite3VdbeAddOp2(v, OP_Close, iSrc, 0); |
2007 sqlite3VdbeAddOp2(v, OP_Close, iDest, 0); | 2073 sqlite3VdbeAddOp2(v, OP_Close, iDest, 0); |
2008 } | 2074 } |
2009 if( emptySrcTest ) sqlite3VdbeJumpHere(v, emptySrcTest); | 2075 if( emptySrcTest ) sqlite3VdbeJumpHere(v, emptySrcTest); |
2010 sqlite3ReleaseTempReg(pParse, regRowid); | 2076 sqlite3ReleaseTempReg(pParse, regRowid); |
2011 sqlite3ReleaseTempReg(pParse, regData); | 2077 sqlite3ReleaseTempReg(pParse, regData); |
2012 if( emptyDestTest ){ | 2078 if( emptyDestTest ){ |
2013 sqlite3VdbeAddOp2(v, OP_Halt, SQLITE_OK, 0); | 2079 sqlite3VdbeAddOp2(v, OP_Halt, SQLITE_OK, 0); |
2014 sqlite3VdbeJumpHere(v, emptyDestTest); | 2080 sqlite3VdbeJumpHere(v, emptyDestTest); |
2015 sqlite3VdbeAddOp2(v, OP_Close, iDest, 0); | 2081 sqlite3VdbeAddOp2(v, OP_Close, iDest, 0); |
2016 return 0; | 2082 return 0; |
2017 }else{ | 2083 }else{ |
2018 return 1; | 2084 return 1; |
2019 } | 2085 } |
2020 } | 2086 } |
2021 #endif /* SQLITE_OMIT_XFER_OPT */ | 2087 #endif /* SQLITE_OMIT_XFER_OPT */ |
OLD | NEW |