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