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