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