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