drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 1 | /* |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 2 | ** 2001 September 15 |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 3 | ** |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 4 | ** The author disclaims copyright to this source code. In place of |
| 5 | ** a legal notice, here is a blessing: |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 6 | ** |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 7 | ** May you do good and not evil. |
| 8 | ** May you find forgiveness for yourself and forgive others. |
| 9 | ** May you share freely, never taking more than you give. |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 10 | ** |
| 11 | ************************************************************************* |
| 12 | ** This file contains C code routines that are called by the parser |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 13 | ** to handle INSERT statements in SQLite. |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 14 | ** |
danielk1977 | 1f6eec5 | 2006-06-16 06:17:47 +0000 | [diff] [blame^] | 15 | ** $Id: insert.c,v 1.168 2006/06/16 06:17:47 danielk1977 Exp $ |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 16 | */ |
| 17 | #include "sqliteInt.h" |
| 18 | |
| 19 | /* |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 +0000 | [diff] [blame] | 20 | ** Set P3 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; |
| 44 | pIdx->zColAff = (char *)sqliteMalloc(pIdx->nColumn+1); |
| 45 | if( !pIdx->zColAff ){ |
| 46 | return; |
| 47 | } |
| 48 | for(n=0; n<pIdx->nColumn; n++){ |
| 49 | pIdx->zColAff[n] = pTab->aCol[pIdx->aiColumn[n]].affinity; |
| 50 | } |
| 51 | pIdx->zColAff[pIdx->nColumn] = '\0'; |
| 52 | } |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 +0000 | [diff] [blame] | 53 | |
drh | 956bc92 | 2004-07-24 17:38:29 +0000 | [diff] [blame] | 54 | sqlite3VdbeChangeP3(v, -1, pIdx->zColAff, 0); |
danielk1977 | a37cdde | 2004-05-16 11:15:36 +0000 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | /* |
| 58 | ** Set P3 of the most recently inserted opcode to a column affinity |
| 59 | ** string for table pTab. A column affinity string has one character |
| 60 | ** for each column indexed by the index, according to the affinity of the |
| 61 | ** column: |
| 62 | ** |
| 63 | ** Character Column affinity |
| 64 | ** ------------------------------ |
drh | 3eda040 | 2005-11-24 13:15:32 +0000 | [diff] [blame] | 65 | ** 'a' TEXT |
| 66 | ** 'b' NONE |
| 67 | ** 'c' NUMERIC |
| 68 | ** 'd' INTEGER |
| 69 | ** 'e' REAL |
danielk1977 | a37cdde | 2004-05-16 11:15:36 +0000 | [diff] [blame] | 70 | */ |
| 71 | void sqlite3TableAffinityStr(Vdbe *v, Table *pTab){ |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 +0000 | [diff] [blame] | 72 | /* The first time a column affinity string for a particular table |
| 73 | ** is required, it is allocated and populated here. It is then |
| 74 | ** stored as a member of the Table structure for subsequent use. |
| 75 | ** |
| 76 | ** The column affinity string will eventually be deleted by |
| 77 | ** sqlite3DeleteTable() when the Table structure itself is cleaned up. |
| 78 | */ |
| 79 | if( !pTab->zColAff ){ |
| 80 | char *zColAff; |
| 81 | int i; |
| 82 | |
danielk1977 | a37cdde | 2004-05-16 11:15:36 +0000 | [diff] [blame] | 83 | zColAff = (char *)sqliteMalloc(pTab->nCol+1); |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 +0000 | [diff] [blame] | 84 | if( !zColAff ){ |
danielk1977 | a37cdde | 2004-05-16 11:15:36 +0000 | [diff] [blame] | 85 | return; |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 +0000 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | for(i=0; i<pTab->nCol; i++){ |
danielk1977 | a37cdde | 2004-05-16 11:15:36 +0000 | [diff] [blame] | 89 | zColAff[i] = pTab->aCol[i].affinity; |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 +0000 | [diff] [blame] | 90 | } |
| 91 | zColAff[pTab->nCol] = '\0'; |
| 92 | |
| 93 | pTab->zColAff = zColAff; |
| 94 | } |
| 95 | |
drh | 956bc92 | 2004-07-24 17:38:29 +0000 | [diff] [blame] | 96 | sqlite3VdbeChangeP3(v, -1, pTab->zColAff, 0); |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 +0000 | [diff] [blame] | 97 | } |
| 98 | |
danielk1977 | 4d88778 | 2005-02-08 08:42:27 +0000 | [diff] [blame] | 99 | /* |
| 100 | ** Return non-zero if SELECT statement p opens the table with rootpage |
| 101 | ** iTab in database iDb. This is used to see if a statement of the form |
| 102 | ** "INSERT INTO <iDb, iTab> SELECT ..." can run without using temporary |
| 103 | ** table for the results of the SELECT. |
| 104 | ** |
| 105 | ** No checking is done for sub-selects that are part of expressions. |
| 106 | */ |
danielk1977 | e501b89 | 2006-01-09 06:29:47 +0000 | [diff] [blame] | 107 | static int selectReadsTable(Select *p, Schema *pSchema, int iTab){ |
danielk1977 | 4d88778 | 2005-02-08 08:42:27 +0000 | [diff] [blame] | 108 | int i; |
| 109 | struct SrcList_item *pItem; |
| 110 | if( p->pSrc==0 ) return 0; |
| 111 | for(i=0, pItem=p->pSrc->a; i<p->pSrc->nSrc; i++, pItem++){ |
| 112 | if( pItem->pSelect ){ |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 113 | if( selectReadsTable(pItem->pSelect, pSchema, iTab) ) return 1; |
danielk1977 | 4d88778 | 2005-02-08 08:42:27 +0000 | [diff] [blame] | 114 | }else{ |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 115 | if( pItem->pTab->pSchema==pSchema && pItem->pTab->tnum==iTab ) return 1; |
danielk1977 | 4d88778 | 2005-02-08 08:42:27 +0000 | [diff] [blame] | 116 | } |
| 117 | } |
| 118 | return 0; |
| 119 | } |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 +0000 | [diff] [blame] | 120 | |
| 121 | /* |
drh | 1ccde15 | 2000-06-17 13:12:39 +0000 | [diff] [blame] | 122 | ** This routine is call to handle SQL of the following forms: |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 123 | ** |
| 124 | ** insert into TABLE (IDLIST) values(EXPRLIST) |
drh | 1ccde15 | 2000-06-17 13:12:39 +0000 | [diff] [blame] | 125 | ** insert into TABLE (IDLIST) select |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 126 | ** |
drh | 1ccde15 | 2000-06-17 13:12:39 +0000 | [diff] [blame] | 127 | ** The IDLIST following the table name is always optional. If omitted, |
| 128 | ** then a list of all columns for the table is substituted. The IDLIST |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 129 | ** appears in the pColumn parameter. pColumn is NULL if IDLIST is omitted. |
drh | 1ccde15 | 2000-06-17 13:12:39 +0000 | [diff] [blame] | 130 | ** |
| 131 | ** The pList parameter holds EXPRLIST in the first form of the INSERT |
| 132 | ** statement above, and pSelect is NULL. For the second form, pList is |
| 133 | ** NULL and pSelect is a pointer to the select statement used to generate |
| 134 | ** data for the insert. |
drh | 142e30d | 2002-08-28 03:00:58 +0000 | [diff] [blame] | 135 | ** |
| 136 | ** The code generated follows one of three templates. For a simple |
| 137 | ** select with data coming from a VALUES clause, the code executes |
| 138 | ** once straight down through. The template looks like this: |
| 139 | ** |
| 140 | ** open write cursor to <table> and its indices |
| 141 | ** puts VALUES clause expressions onto the stack |
| 142 | ** write the resulting record into <table> |
| 143 | ** cleanup |
| 144 | ** |
| 145 | ** If the statement is of the form |
| 146 | ** |
| 147 | ** INSERT INTO <table> SELECT ... |
| 148 | ** |
| 149 | ** And the SELECT clause does not read from <table> at any time, then |
| 150 | ** the generated code follows this template: |
| 151 | ** |
| 152 | ** goto B |
| 153 | ** A: setup for the SELECT |
| 154 | ** loop over the tables in the SELECT |
| 155 | ** gosub C |
| 156 | ** end loop |
| 157 | ** cleanup after the SELECT |
| 158 | ** goto D |
| 159 | ** B: open write cursor to <table> and its indices |
| 160 | ** goto A |
| 161 | ** C: insert the select result into <table> |
| 162 | ** return |
| 163 | ** D: cleanup |
| 164 | ** |
| 165 | ** The third template is used if the insert statement takes its |
| 166 | ** values from a SELECT but the data is being inserted into a table |
| 167 | ** that is also read as part of the SELECT. In the third form, |
| 168 | ** we have to use a intermediate table to store the results of |
| 169 | ** the select. The template is like this: |
| 170 | ** |
| 171 | ** goto B |
| 172 | ** A: setup for the SELECT |
| 173 | ** loop over the tables in the SELECT |
| 174 | ** gosub C |
| 175 | ** end loop |
| 176 | ** cleanup after the SELECT |
| 177 | ** goto D |
| 178 | ** C: insert the select result into the intermediate table |
| 179 | ** return |
| 180 | ** B: open a cursor to an intermediate table |
| 181 | ** goto A |
| 182 | ** D: open write cursor to <table> and its indices |
| 183 | ** loop over the intermediate table |
| 184 | ** transfer values form intermediate table into <table> |
| 185 | ** end the loop |
| 186 | ** cleanup |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 187 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 188 | void sqlite3Insert( |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 189 | Parse *pParse, /* Parser context */ |
drh | 113088e | 2003-03-20 01:16:58 +0000 | [diff] [blame] | 190 | SrcList *pTabList, /* Name of table into which we are inserting */ |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 191 | ExprList *pList, /* List of values to be inserted */ |
drh | 5974a30 | 2000-06-07 14:42:26 +0000 | [diff] [blame] | 192 | Select *pSelect, /* A SELECT statement to use as the data source */ |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 193 | IdList *pColumn, /* Column names corresponding to IDLIST. */ |
| 194 | int onError /* How to handle constraint errors */ |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 195 | ){ |
drh | 5974a30 | 2000-06-07 14:42:26 +0000 | [diff] [blame] | 196 | Table *pTab; /* The table to insert into */ |
drh | 113088e | 2003-03-20 01:16:58 +0000 | [diff] [blame] | 197 | char *zTab; /* Name of the table into which we are inserting */ |
drh | e22a334 | 2003-04-22 20:30:37 +0000 | [diff] [blame] | 198 | const char *zDb; /* Name of the database holding this table */ |
drh | 5974a30 | 2000-06-07 14:42:26 +0000 | [diff] [blame] | 199 | int i, j, idx; /* Loop counters */ |
| 200 | Vdbe *v; /* Generate code into this virtual machine */ |
| 201 | Index *pIdx; /* For looping over indices of the table */ |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 202 | int nColumn; /* Number of columns in the data */ |
danielk1977 | cfe9a69 | 2004-06-16 12:00:29 +0000 | [diff] [blame] | 203 | int base = 0; /* VDBE Cursor number for pTab */ |
| 204 | int iCont=0,iBreak=0; /* Beginning and end of the loop over srcTab */ |
drh | 9bb575f | 2004-09-06 17:24:11 +0000 | [diff] [blame] | 205 | sqlite3 *db; /* The main database structure */ |
drh | 4a32431 | 2001-12-21 14:30:42 +0000 | [diff] [blame] | 206 | int keyColumn = -1; /* Column that is the INTEGER PRIMARY KEY */ |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 207 | int endOfLoop; /* Label for the end of the insertion loop */ |
danielk1977 | 4d88778 | 2005-02-08 08:42:27 +0000 | [diff] [blame] | 208 | int useTempTable = 0; /* Store SELECT results in intermediate table */ |
danielk1977 | cfe9a69 | 2004-06-16 12:00:29 +0000 | [diff] [blame] | 209 | int srcTab = 0; /* Data comes from this temporary cursor if >=0 */ |
| 210 | int iSelectLoop = 0; /* Address of code that implements the SELECT */ |
| 211 | int iCleanup = 0; /* Address of the cleanup code */ |
| 212 | int iInsertBlock = 0; /* Address of the subroutine used to insert data */ |
| 213 | int iCntMem = 0; /* Memory cell used for the row counter */ |
drh | 798da52 | 2004-11-04 04:42:28 +0000 | [diff] [blame] | 214 | int newIdx = -1; /* Cursor for the NEW table */ |
drh | 2958a4e | 2004-11-12 03:56:15 +0000 | [diff] [blame] | 215 | Db *pDb; /* The database containing table being inserted into */ |
| 216 | int counterMem = 0; /* Memory cell holding AUTOINCREMENT counter */ |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 217 | int iDb; |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 218 | |
drh | 798da52 | 2004-11-04 04:42:28 +0000 | [diff] [blame] | 219 | #ifndef SQLITE_OMIT_TRIGGER |
| 220 | int isView; /* True if attempting to insert into a view */ |
drh | dca7684 | 2004-12-07 14:06:13 +0000 | [diff] [blame] | 221 | int triggers_exist = 0; /* True if there are FOR EACH ROW triggers */ |
drh | 798da52 | 2004-11-04 04:42:28 +0000 | [diff] [blame] | 222 | #endif |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 223 | |
drh | f338814 | 2004-11-13 03:48:06 +0000 | [diff] [blame] | 224 | #ifndef SQLITE_OMIT_AUTOINCREMENT |
drh | 02afc86 | 2006-01-20 18:10:57 +0000 | [diff] [blame] | 225 | int counterRowid = 0; /* Memory cell holding rowid of autoinc counter */ |
drh | f338814 | 2004-11-13 03:48:06 +0000 | [diff] [blame] | 226 | #endif |
| 227 | |
danielk1977 | 9e12800 | 2006-01-18 16:51:35 +0000 | [diff] [blame] | 228 | if( pParse->nErr || sqlite3MallocFailed() ){ |
drh | 6f7adc8 | 2006-01-11 21:41:20 +0000 | [diff] [blame] | 229 | goto insert_cleanup; |
| 230 | } |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 231 | db = pParse->db; |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 232 | |
drh | 1ccde15 | 2000-06-17 13:12:39 +0000 | [diff] [blame] | 233 | /* Locate the table into which we will be inserting new information. |
| 234 | */ |
drh | 113088e | 2003-03-20 01:16:58 +0000 | [diff] [blame] | 235 | assert( pTabList->nSrc==1 ); |
| 236 | zTab = pTabList->a[0].zName; |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 237 | if( zTab==0 ) goto insert_cleanup; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 238 | pTab = sqlite3SrcListLookup(pParse, pTabList); |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 239 | if( pTab==0 ){ |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 240 | goto insert_cleanup; |
| 241 | } |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 242 | iDb = sqlite3SchemaToIndex(db, pTab->pSchema); |
| 243 | assert( iDb<db->nDb ); |
| 244 | pDb = &db->aDb[iDb]; |
drh | 2958a4e | 2004-11-12 03:56:15 +0000 | [diff] [blame] | 245 | zDb = pDb->zName; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 246 | if( sqlite3AuthCheck(pParse, SQLITE_INSERT, pTab->zName, 0, zDb) ){ |
drh | 1962bda | 2003-01-12 19:33:52 +0000 | [diff] [blame] | 247 | goto insert_cleanup; |
| 248 | } |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 249 | |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 250 | /* Figure out if we have any triggers and if the table being |
| 251 | ** inserted into is a view |
| 252 | */ |
| 253 | #ifndef SQLITE_OMIT_TRIGGER |
drh | dca7684 | 2004-12-07 14:06:13 +0000 | [diff] [blame] | 254 | triggers_exist = sqlite3TriggersExist(pParse, pTab, TK_INSERT, 0); |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 255 | isView = pTab->pSelect!=0; |
| 256 | #else |
drh | dca7684 | 2004-12-07 14:06:13 +0000 | [diff] [blame] | 257 | # define triggers_exist 0 |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 258 | # define isView 0 |
| 259 | #endif |
| 260 | #ifdef SQLITE_OMIT_VIEW |
| 261 | # undef isView |
| 262 | # define isView 0 |
| 263 | #endif |
| 264 | |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 265 | /* Ensure that: |
| 266 | * (a) the table is not read-only, |
| 267 | * (b) that if it is a view then ON INSERT triggers exist |
| 268 | */ |
drh | dca7684 | 2004-12-07 14:06:13 +0000 | [diff] [blame] | 269 | if( sqlite3IsReadOnly(pParse, pTab, triggers_exist) ){ |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 270 | goto insert_cleanup; |
| 271 | } |
drh | 43617e9 | 2006-03-06 20:55:46 +0000 | [diff] [blame] | 272 | assert( pTab!=0 ); |
drh | 1ccde15 | 2000-06-17 13:12:39 +0000 | [diff] [blame] | 273 | |
drh | f573c99 | 2002-07-31 00:32:50 +0000 | [diff] [blame] | 274 | /* If pTab is really a view, make sure it has been initialized. |
| 275 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 276 | if( isView && sqlite3ViewGetColumnNames(pParse, pTab) ){ |
drh | 5cf590c | 2003-04-24 01:45:04 +0000 | [diff] [blame] | 277 | goto insert_cleanup; |
drh | f573c99 | 2002-07-31 00:32:50 +0000 | [diff] [blame] | 278 | } |
| 279 | |
drh | 1ccde15 | 2000-06-17 13:12:39 +0000 | [diff] [blame] | 280 | /* Allocate a VDBE |
| 281 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 282 | v = sqlite3GetVdbe(pParse); |
drh | 5974a30 | 2000-06-07 14:42:26 +0000 | [diff] [blame] | 283 | if( v==0 ) goto insert_cleanup; |
drh | 4794f73 | 2004-11-05 17:17:50 +0000 | [diff] [blame] | 284 | if( pParse->nested==0 ) sqlite3VdbeCountChanges(v); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 285 | sqlite3BeginWriteOperation(pParse, pSelect || triggers_exist, iDb); |
drh | 1ccde15 | 2000-06-17 13:12:39 +0000 | [diff] [blame] | 286 | |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 287 | /* if there are row triggers, allocate a temp table for new.* references. */ |
drh | dca7684 | 2004-12-07 14:06:13 +0000 | [diff] [blame] | 288 | if( triggers_exist ){ |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 289 | newIdx = pParse->nTab++; |
danielk1977 | f29ce55 | 2002-05-19 23:43:12 +0000 | [diff] [blame] | 290 | } |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 291 | |
drh | 2958a4e | 2004-11-12 03:56:15 +0000 | [diff] [blame] | 292 | #ifndef SQLITE_OMIT_AUTOINCREMENT |
| 293 | /* If this is an AUTOINCREMENT table, look up the sequence number in the |
drh | f338814 | 2004-11-13 03:48:06 +0000 | [diff] [blame] | 294 | ** sqlite_sequence table and store it in memory cell counterMem. Also |
| 295 | ** remember the rowid of the sqlite_sequence table entry in memory cell |
| 296 | ** counterRowid. |
drh | 2958a4e | 2004-11-12 03:56:15 +0000 | [diff] [blame] | 297 | */ |
| 298 | if( pTab->autoInc ){ |
drh | f338814 | 2004-11-13 03:48:06 +0000 | [diff] [blame] | 299 | int iCur = pParse->nTab; |
danielk1977 | f011300 | 2006-01-24 12:09:17 +0000 | [diff] [blame] | 300 | int addr = sqlite3VdbeCurrentAddr(v); |
drh | f338814 | 2004-11-13 03:48:06 +0000 | [diff] [blame] | 301 | counterRowid = pParse->nMem++; |
| 302 | counterMem = pParse->nMem++; |
danielk1977 | c00da10 | 2006-01-07 13:21:04 +0000 | [diff] [blame] | 303 | sqlite3OpenTable(pParse, iCur, iDb, pDb->pSchema->pSeqTab, OP_OpenRead); |
danielk1977 | f011300 | 2006-01-24 12:09:17 +0000 | [diff] [blame] | 304 | sqlite3VdbeAddOp(v, OP_Rewind, iCur, addr+13); |
drh | f338814 | 2004-11-13 03:48:06 +0000 | [diff] [blame] | 305 | sqlite3VdbeAddOp(v, OP_Column, iCur, 0); |
| 306 | sqlite3VdbeOp3(v, OP_String8, 0, 0, pTab->zName, 0); |
danielk1977 | f011300 | 2006-01-24 12:09:17 +0000 | [diff] [blame] | 307 | sqlite3VdbeAddOp(v, OP_Ne, 0x100, addr+12); |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 308 | sqlite3VdbeAddOp(v, OP_Rowid, iCur, 0); |
drh | f338814 | 2004-11-13 03:48:06 +0000 | [diff] [blame] | 309 | sqlite3VdbeAddOp(v, OP_MemStore, counterRowid, 1); |
| 310 | sqlite3VdbeAddOp(v, OP_Column, iCur, 1); |
| 311 | sqlite3VdbeAddOp(v, OP_MemStore, counterMem, 1); |
danielk1977 | f011300 | 2006-01-24 12:09:17 +0000 | [diff] [blame] | 312 | sqlite3VdbeAddOp(v, OP_Goto, 0, addr+13); |
| 313 | sqlite3VdbeAddOp(v, OP_Next, iCur, addr+4); |
drh | f338814 | 2004-11-13 03:48:06 +0000 | [diff] [blame] | 314 | sqlite3VdbeAddOp(v, OP_Close, iCur, 0); |
drh | 2958a4e | 2004-11-12 03:56:15 +0000 | [diff] [blame] | 315 | } |
| 316 | #endif /* SQLITE_OMIT_AUTOINCREMENT */ |
| 317 | |
drh | 1ccde15 | 2000-06-17 13:12:39 +0000 | [diff] [blame] | 318 | /* Figure out how many columns of data are supplied. If the data |
drh | 142e30d | 2002-08-28 03:00:58 +0000 | [diff] [blame] | 319 | ** is coming from a SELECT statement, then this step also generates |
| 320 | ** all the code to implement the SELECT statement and invoke a subroutine |
| 321 | ** to process each row of the result. (Template 2.) If the SELECT |
| 322 | ** statement uses the the table that is being inserted into, then the |
| 323 | ** subroutine is also coded here. That subroutine stores the SELECT |
| 324 | ** results in a temporary table. (Template 3.) |
drh | 1ccde15 | 2000-06-17 13:12:39 +0000 | [diff] [blame] | 325 | */ |
drh | 5974a30 | 2000-06-07 14:42:26 +0000 | [diff] [blame] | 326 | if( pSelect ){ |
drh | 142e30d | 2002-08-28 03:00:58 +0000 | [diff] [blame] | 327 | /* Data is coming from a SELECT. Generate code to implement that SELECT |
| 328 | */ |
| 329 | int rc, iInitCode; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 330 | iInitCode = sqlite3VdbeAddOp(v, OP_Goto, 0, 0); |
| 331 | iSelectLoop = sqlite3VdbeCurrentAddr(v); |
| 332 | iInsertBlock = sqlite3VdbeMakeLabel(v); |
danielk1977 | b3bce66 | 2005-01-29 08:32:43 +0000 | [diff] [blame] | 333 | |
| 334 | /* Resolve the expressions in the SELECT statement and execute it. */ |
| 335 | rc = sqlite3Select(pParse, pSelect, SRT_Subroutine, iInsertBlock,0,0,0,0); |
danielk1977 | 9e12800 | 2006-01-18 16:51:35 +0000 | [diff] [blame] | 336 | if( rc || pParse->nErr || sqlite3MallocFailed() ){ |
drh | 6f7adc8 | 2006-01-11 21:41:20 +0000 | [diff] [blame] | 337 | goto insert_cleanup; |
| 338 | } |
danielk1977 | b3bce66 | 2005-01-29 08:32:43 +0000 | [diff] [blame] | 339 | |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 340 | iCleanup = sqlite3VdbeMakeLabel(v); |
| 341 | sqlite3VdbeAddOp(v, OP_Goto, 0, iCleanup); |
drh | 5974a30 | 2000-06-07 14:42:26 +0000 | [diff] [blame] | 342 | assert( pSelect->pEList ); |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 343 | nColumn = pSelect->pEList->nExpr; |
drh | 142e30d | 2002-08-28 03:00:58 +0000 | [diff] [blame] | 344 | |
| 345 | /* Set useTempTable to TRUE if the result of the SELECT statement |
| 346 | ** should be written into a temporary table. Set to FALSE if each |
| 347 | ** row of the SELECT can be written directly into the result table. |
drh | 048c530 | 2003-04-03 01:50:44 +0000 | [diff] [blame] | 348 | ** |
| 349 | ** A temp table must be used if the table being updated is also one |
| 350 | ** of the tables being read by the SELECT statement. Also use a |
| 351 | ** temp table in the case of row triggers. |
drh | 142e30d | 2002-08-28 03:00:58 +0000 | [diff] [blame] | 352 | */ |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 353 | if( triggers_exist || selectReadsTable(pSelect,pTab->pSchema,pTab->tnum) ){ |
drh | 048c530 | 2003-04-03 01:50:44 +0000 | [diff] [blame] | 354 | useTempTable = 1; |
drh | 048c530 | 2003-04-03 01:50:44 +0000 | [diff] [blame] | 355 | } |
drh | 142e30d | 2002-08-28 03:00:58 +0000 | [diff] [blame] | 356 | |
| 357 | if( useTempTable ){ |
| 358 | /* Generate the subroutine that SELECT calls to process each row of |
| 359 | ** the result. Store the result in a temporary table |
| 360 | */ |
| 361 | srcTab = pParse->nTab++; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 362 | sqlite3VdbeResolveLabel(v, iInsertBlock); |
| 363 | sqlite3VdbeAddOp(v, OP_MakeRecord, nColumn, 0); |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 364 | sqlite3VdbeAddOp(v, OP_NewRowid, srcTab, 0); |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 365 | sqlite3VdbeAddOp(v, OP_Pull, 1, 0); |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 366 | sqlite3VdbeAddOp(v, OP_Insert, srcTab, 0); |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 367 | sqlite3VdbeAddOp(v, OP_Return, 0, 0); |
drh | 142e30d | 2002-08-28 03:00:58 +0000 | [diff] [blame] | 368 | |
| 369 | /* The following code runs first because the GOTO at the very top |
| 370 | ** of the program jumps to it. Create the temporary table, then jump |
| 371 | ** back up and execute the SELECT code above. |
| 372 | */ |
drh | d654be8 | 2005-09-20 17:42:23 +0000 | [diff] [blame] | 373 | sqlite3VdbeJumpHere(v, iInitCode); |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 374 | sqlite3VdbeAddOp(v, OP_OpenEphemeral, srcTab, 0); |
drh | b6f5452 | 2004-05-20 02:42:16 +0000 | [diff] [blame] | 375 | sqlite3VdbeAddOp(v, OP_SetNumColumns, srcTab, nColumn); |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 376 | sqlite3VdbeAddOp(v, OP_Goto, 0, iSelectLoop); |
| 377 | sqlite3VdbeResolveLabel(v, iCleanup); |
drh | 142e30d | 2002-08-28 03:00:58 +0000 | [diff] [blame] | 378 | }else{ |
drh | d654be8 | 2005-09-20 17:42:23 +0000 | [diff] [blame] | 379 | sqlite3VdbeJumpHere(v, iInitCode); |
drh | 142e30d | 2002-08-28 03:00:58 +0000 | [diff] [blame] | 380 | } |
drh | 5974a30 | 2000-06-07 14:42:26 +0000 | [diff] [blame] | 381 | }else{ |
drh | 142e30d | 2002-08-28 03:00:58 +0000 | [diff] [blame] | 382 | /* This is the case if the data for the INSERT is coming from a VALUES |
| 383 | ** clause |
| 384 | */ |
danielk1977 | b3bce66 | 2005-01-29 08:32:43 +0000 | [diff] [blame] | 385 | NameContext sNC; |
| 386 | memset(&sNC, 0, sizeof(sNC)); |
| 387 | sNC.pParse = pParse; |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 388 | assert( pList!=0 ); |
drh | 5974a30 | 2000-06-07 14:42:26 +0000 | [diff] [blame] | 389 | srcTab = -1; |
drh | 142e30d | 2002-08-28 03:00:58 +0000 | [diff] [blame] | 390 | useTempTable = 0; |
drh | 5974a30 | 2000-06-07 14:42:26 +0000 | [diff] [blame] | 391 | assert( pList ); |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 392 | nColumn = pList->nExpr; |
drh | e64e7b2 | 2002-02-18 13:56:36 +0000 | [diff] [blame] | 393 | for(i=0; i<nColumn; i++){ |
danielk1977 | b3bce66 | 2005-01-29 08:32:43 +0000 | [diff] [blame] | 394 | if( sqlite3ExprResolveNames(&sNC, pList->a[i].pExpr) ){ |
drh | b04a5d8 | 2002-04-12 03:55:15 +0000 | [diff] [blame] | 395 | goto insert_cleanup; |
| 396 | } |
drh | e64e7b2 | 2002-02-18 13:56:36 +0000 | [diff] [blame] | 397 | } |
drh | 5974a30 | 2000-06-07 14:42:26 +0000 | [diff] [blame] | 398 | } |
drh | 1ccde15 | 2000-06-17 13:12:39 +0000 | [diff] [blame] | 399 | |
| 400 | /* Make sure the number of columns in the source data matches the number |
| 401 | ** of columns to be inserted into the table. |
| 402 | */ |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 403 | if( pColumn==0 && nColumn!=pTab->nCol ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 404 | sqlite3ErrorMsg(pParse, |
drh | da93d23 | 2003-03-31 02:12:46 +0000 | [diff] [blame] | 405 | "table %S has %d columns but %d values were supplied", |
| 406 | pTabList, 0, pTab->nCol, nColumn); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 407 | goto insert_cleanup; |
| 408 | } |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 409 | if( pColumn!=0 && nColumn!=pColumn->nId ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 410 | sqlite3ErrorMsg(pParse, "%d values for %d columns", nColumn, pColumn->nId); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 411 | goto insert_cleanup; |
| 412 | } |
drh | 1ccde15 | 2000-06-17 13:12:39 +0000 | [diff] [blame] | 413 | |
| 414 | /* If the INSERT statement included an IDLIST term, then make sure |
| 415 | ** all elements of the IDLIST really are columns of the table and |
| 416 | ** remember the column indices. |
drh | c839258 | 2001-12-31 02:48:51 +0000 | [diff] [blame] | 417 | ** |
| 418 | ** If the table has an INTEGER PRIMARY KEY column and that column |
| 419 | ** is named in the IDLIST, then record in the keyColumn variable |
| 420 | ** the index into IDLIST of the primary key column. keyColumn is |
| 421 | ** the index of the primary key as it appears in IDLIST, not as |
| 422 | ** is appears in the original table. (The index of the primary |
| 423 | ** key in the original table is pTab->iPKey.) |
drh | 1ccde15 | 2000-06-17 13:12:39 +0000 | [diff] [blame] | 424 | */ |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 425 | if( pColumn ){ |
| 426 | for(i=0; i<pColumn->nId; i++){ |
| 427 | pColumn->a[i].idx = -1; |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 428 | } |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 429 | for(i=0; i<pColumn->nId; i++){ |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 430 | for(j=0; j<pTab->nCol; j++){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 431 | if( sqlite3StrICmp(pColumn->a[i].zName, pTab->aCol[j].zName)==0 ){ |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 432 | pColumn->a[i].idx = j; |
drh | 4a32431 | 2001-12-21 14:30:42 +0000 | [diff] [blame] | 433 | if( j==pTab->iPKey ){ |
drh | 9aa028d | 2001-12-22 21:48:29 +0000 | [diff] [blame] | 434 | keyColumn = i; |
drh | 4a32431 | 2001-12-21 14:30:42 +0000 | [diff] [blame] | 435 | } |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 436 | break; |
| 437 | } |
| 438 | } |
| 439 | if( j>=pTab->nCol ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 440 | if( sqlite3IsRowid(pColumn->a[i].zName) ){ |
drh | a0217ba | 2003-06-01 01:10:33 +0000 | [diff] [blame] | 441 | keyColumn = i; |
| 442 | }else{ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 443 | sqlite3ErrorMsg(pParse, "table %S has no column named %s", |
drh | a0217ba | 2003-06-01 01:10:33 +0000 | [diff] [blame] | 444 | pTabList, 0, pColumn->a[i].zName); |
| 445 | pParse->nErr++; |
| 446 | goto insert_cleanup; |
| 447 | } |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 448 | } |
| 449 | } |
| 450 | } |
drh | 1ccde15 | 2000-06-17 13:12:39 +0000 | [diff] [blame] | 451 | |
drh | aacc543 | 2002-01-06 17:07:40 +0000 | [diff] [blame] | 452 | /* If there is no IDLIST term but the table has an integer primary |
drh | c839258 | 2001-12-31 02:48:51 +0000 | [diff] [blame] | 453 | ** key, the set the keyColumn variable to the primary key column index |
| 454 | ** in the original table definition. |
drh | 4a32431 | 2001-12-21 14:30:42 +0000 | [diff] [blame] | 455 | */ |
| 456 | if( pColumn==0 ){ |
| 457 | keyColumn = pTab->iPKey; |
| 458 | } |
| 459 | |
drh | 142e30d | 2002-08-28 03:00:58 +0000 | [diff] [blame] | 460 | /* Open the temp table for FOR EACH ROW triggers |
| 461 | */ |
drh | dca7684 | 2004-12-07 14:06:13 +0000 | [diff] [blame] | 462 | if( triggers_exist ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 463 | sqlite3VdbeAddOp(v, OP_OpenPseudo, newIdx, 0); |
danielk1977 | 84ac9d0 | 2004-05-18 09:58:06 +0000 | [diff] [blame] | 464 | sqlite3VdbeAddOp(v, OP_SetNumColumns, newIdx, pTab->nCol); |
danielk1977 | f29ce55 | 2002-05-19 23:43:12 +0000 | [diff] [blame] | 465 | } |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 466 | |
drh | feeb139 | 2002-04-09 03:28:01 +0000 | [diff] [blame] | 467 | /* Initialize the count of rows to be inserted |
| 468 | */ |
drh | 142e30d | 2002-08-28 03:00:58 +0000 | [diff] [blame] | 469 | if( db->flags & SQLITE_CountRows ){ |
| 470 | iCntMem = pParse->nMem++; |
drh | d654be8 | 2005-09-20 17:42:23 +0000 | [diff] [blame] | 471 | sqlite3VdbeAddOp(v, OP_MemInt, 0, iCntMem); |
drh | feeb139 | 2002-04-09 03:28:01 +0000 | [diff] [blame] | 472 | } |
| 473 | |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 474 | /* Open tables and indices if there are no row triggers */ |
drh | dca7684 | 2004-12-07 14:06:13 +0000 | [diff] [blame] | 475 | if( !triggers_exist ){ |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 476 | base = pParse->nTab; |
drh | 290c194 | 2004-08-21 17:54:45 +0000 | [diff] [blame] | 477 | sqlite3OpenTableAndIndices(pParse, pTab, base, OP_OpenWrite); |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 478 | } |
| 479 | |
drh | 142e30d | 2002-08-28 03:00:58 +0000 | [diff] [blame] | 480 | /* If the data source is a temporary table, then we have to create |
drh | 1ccde15 | 2000-06-17 13:12:39 +0000 | [diff] [blame] | 481 | ** a loop because there might be multiple rows of data. If the data |
drh | 142e30d | 2002-08-28 03:00:58 +0000 | [diff] [blame] | 482 | ** source is a subroutine call from the SELECT statement, then we need |
| 483 | ** to launch the SELECT statement processing. |
drh | 1ccde15 | 2000-06-17 13:12:39 +0000 | [diff] [blame] | 484 | */ |
drh | 142e30d | 2002-08-28 03:00:58 +0000 | [diff] [blame] | 485 | if( useTempTable ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 486 | iBreak = sqlite3VdbeMakeLabel(v); |
| 487 | sqlite3VdbeAddOp(v, OP_Rewind, srcTab, iBreak); |
| 488 | iCont = sqlite3VdbeCurrentAddr(v); |
drh | 142e30d | 2002-08-28 03:00:58 +0000 | [diff] [blame] | 489 | }else if( pSelect ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 490 | sqlite3VdbeAddOp(v, OP_Goto, 0, iSelectLoop); |
| 491 | sqlite3VdbeResolveLabel(v, iInsertBlock); |
drh | 5974a30 | 2000-06-07 14:42:26 +0000 | [diff] [blame] | 492 | } |
drh | 1ccde15 | 2000-06-17 13:12:39 +0000 | [diff] [blame] | 493 | |
drh | 5cf590c | 2003-04-24 01:45:04 +0000 | [diff] [blame] | 494 | /* Run the BEFORE and INSTEAD OF triggers, if there are any |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 495 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 496 | endOfLoop = sqlite3VdbeMakeLabel(v); |
drh | dca7684 | 2004-12-07 14:06:13 +0000 | [diff] [blame] | 497 | if( triggers_exist & TRIGGER_BEFORE ){ |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 498 | |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 499 | /* build the NEW.* reference row. Note that if there is an INTEGER |
| 500 | ** PRIMARY KEY into which a NULL is being inserted, that NULL will be |
| 501 | ** translated into a unique ID for the row. But on a BEFORE trigger, |
| 502 | ** we do not know what the unique ID will be (because the insert has |
| 503 | ** not happened yet) so we substitute a rowid of -1 |
| 504 | */ |
| 505 | if( keyColumn<0 ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 506 | sqlite3VdbeAddOp(v, OP_Integer, -1, 0); |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 507 | }else if( useTempTable ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 508 | sqlite3VdbeAddOp(v, OP_Column, srcTab, keyColumn); |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 509 | }else{ |
drh | d6fe961 | 2005-01-14 01:22:00 +0000 | [diff] [blame] | 510 | assert( pSelect==0 ); /* Otherwise useTempTable is true */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 511 | sqlite3ExprCode(pParse, pList->a[keyColumn].pExpr); |
| 512 | sqlite3VdbeAddOp(v, OP_NotNull, -1, sqlite3VdbeCurrentAddr(v)+3); |
| 513 | sqlite3VdbeAddOp(v, OP_Pop, 1, 0); |
| 514 | sqlite3VdbeAddOp(v, OP_Integer, -1, 0); |
| 515 | sqlite3VdbeAddOp(v, OP_MustBeInt, 0, 0); |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 516 | } |
| 517 | |
| 518 | /* Create the new column data |
| 519 | */ |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 520 | for(i=0; i<pTab->nCol; i++){ |
| 521 | if( pColumn==0 ){ |
drh | 9adf9ac | 2002-05-15 11:44:13 +0000 | [diff] [blame] | 522 | j = i; |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 523 | }else{ |
drh | 9adf9ac | 2002-05-15 11:44:13 +0000 | [diff] [blame] | 524 | for(j=0; j<pColumn->nId; j++){ |
| 525 | if( pColumn->a[j].idx==i ) break; |
| 526 | } |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 527 | } |
| 528 | if( pColumn && j>=pColumn->nId ){ |
danielk1977 | 7977a17 | 2004-11-09 12:44:37 +0000 | [diff] [blame] | 529 | sqlite3ExprCode(pParse, pTab->aCol[i].pDflt); |
drh | 142e30d | 2002-08-28 03:00:58 +0000 | [diff] [blame] | 530 | }else if( useTempTable ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 531 | sqlite3VdbeAddOp(v, OP_Column, srcTab, j); |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 532 | }else{ |
drh | d6fe961 | 2005-01-14 01:22:00 +0000 | [diff] [blame] | 533 | assert( pSelect==0 ); /* Otherwise useTempTable is true */ |
drh | 2530378 | 2004-12-07 15:41:48 +0000 | [diff] [blame] | 534 | sqlite3ExprCodeAndCache(pParse, pList->a[j].pExpr); |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 535 | } |
| 536 | } |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 537 | sqlite3VdbeAddOp(v, OP_MakeRecord, pTab->nCol, 0); |
danielk1977 | a37cdde | 2004-05-16 11:15:36 +0000 | [diff] [blame] | 538 | |
| 539 | /* If this is an INSERT on a view with an INSTEAD OF INSERT trigger, |
| 540 | ** do not attempt any conversions before assembling the record. |
| 541 | ** If this is a real table, attempt conversions as required by the |
| 542 | ** table column affinities. |
| 543 | */ |
| 544 | if( !isView ){ |
| 545 | sqlite3TableAffinityStr(v, pTab); |
| 546 | } |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 547 | sqlite3VdbeAddOp(v, OP_Insert, newIdx, 0); |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 548 | |
drh | 5cf590c | 2003-04-24 01:45:04 +0000 | [diff] [blame] | 549 | /* Fire BEFORE or INSTEAD OF triggers */ |
drh | dca7684 | 2004-12-07 14:06:13 +0000 | [diff] [blame] | 550 | if( sqlite3CodeRowTrigger(pParse, TK_INSERT, 0, TRIGGER_BEFORE, pTab, |
drh | b2fe7d8 | 2003-04-20 17:29:23 +0000 | [diff] [blame] | 551 | newIdx, -1, onError, endOfLoop) ){ |
danielk1977 | f29ce55 | 2002-05-19 23:43:12 +0000 | [diff] [blame] | 552 | goto insert_cleanup; |
| 553 | } |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 554 | } |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 555 | |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 556 | /* If any triggers exists, the opening of tables and indices is deferred |
| 557 | ** until now. |
| 558 | */ |
drh | dca7684 | 2004-12-07 14:06:13 +0000 | [diff] [blame] | 559 | if( triggers_exist && !isView ){ |
drh | 5cf590c | 2003-04-24 01:45:04 +0000 | [diff] [blame] | 560 | base = pParse->nTab; |
drh | 290c194 | 2004-08-21 17:54:45 +0000 | [diff] [blame] | 561 | sqlite3OpenTableAndIndices(pParse, pTab, base, OP_OpenWrite); |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 562 | } |
| 563 | |
drh | 4a32431 | 2001-12-21 14:30:42 +0000 | [diff] [blame] | 564 | /* Push the record number for the new entry onto the stack. The |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 565 | ** record number is a randomly generate integer created by NewRowid |
drh | 4a32431 | 2001-12-21 14:30:42 +0000 | [diff] [blame] | 566 | ** except when the table has an INTEGER PRIMARY KEY column, in which |
drh | b419a92 | 2002-01-30 16:17:23 +0000 | [diff] [blame] | 567 | ** case the record number is the same as that column. |
drh | 1ccde15 | 2000-06-17 13:12:39 +0000 | [diff] [blame] | 568 | */ |
drh | 5cf590c | 2003-04-24 01:45:04 +0000 | [diff] [blame] | 569 | if( !isView ){ |
drh | 4cbdda9 | 2006-06-14 19:00:20 +0000 | [diff] [blame] | 570 | if( IsVirtual(pTab) ){ |
| 571 | /* The row that the VUpdate opcode will delete: none */ |
| 572 | sqlite3VdbeAddOp(v, OP_Null, 0, 0); |
| 573 | } |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 574 | if( keyColumn>=0 ){ |
drh | 142e30d | 2002-08-28 03:00:58 +0000 | [diff] [blame] | 575 | if( useTempTable ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 576 | sqlite3VdbeAddOp(v, OP_Column, srcTab, keyColumn); |
drh | 142e30d | 2002-08-28 03:00:58 +0000 | [diff] [blame] | 577 | }else if( pSelect ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 578 | sqlite3VdbeAddOp(v, OP_Dup, nColumn - keyColumn - 1, 1); |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 579 | }else{ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 580 | sqlite3ExprCode(pParse, pList->a[keyColumn].pExpr); |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 581 | } |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 582 | /* If the PRIMARY KEY expression is NULL, then use OP_NewRowid |
drh | 27a3278 | 2002-06-19 20:32:43 +0000 | [diff] [blame] | 583 | ** to generate a unique primary key value. |
| 584 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 585 | sqlite3VdbeAddOp(v, OP_NotNull, -1, sqlite3VdbeCurrentAddr(v)+3); |
| 586 | sqlite3VdbeAddOp(v, OP_Pop, 1, 0); |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 587 | sqlite3VdbeAddOp(v, OP_NewRowid, base, counterMem); |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 588 | sqlite3VdbeAddOp(v, OP_MustBeInt, 0, 0); |
drh | 4cbdda9 | 2006-06-14 19:00:20 +0000 | [diff] [blame] | 589 | }else if( IsVirtual(pTab) ){ |
| 590 | sqlite3VdbeAddOp(v, OP_Null, 0, 0); |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 591 | }else{ |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 592 | sqlite3VdbeAddOp(v, OP_NewRowid, base, counterMem); |
drh | 4a32431 | 2001-12-21 14:30:42 +0000 | [diff] [blame] | 593 | } |
drh | 2958a4e | 2004-11-12 03:56:15 +0000 | [diff] [blame] | 594 | #ifndef SQLITE_OMIT_AUTOINCREMENT |
| 595 | if( pTab->autoInc ){ |
| 596 | sqlite3VdbeAddOp(v, OP_MemMax, counterMem, 0); |
| 597 | } |
| 598 | #endif /* SQLITE_OMIT_AUTOINCREMENT */ |
drh | 4a32431 | 2001-12-21 14:30:42 +0000 | [diff] [blame] | 599 | |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 600 | /* Push onto the stack, data for all columns of the new entry, beginning |
danielk1977 | f29ce55 | 2002-05-19 23:43:12 +0000 | [diff] [blame] | 601 | ** with the first column. |
| 602 | */ |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 603 | for(i=0; i<pTab->nCol; i++){ |
| 604 | if( i==pTab->iPKey ){ |
drh | 9adf9ac | 2002-05-15 11:44:13 +0000 | [diff] [blame] | 605 | /* The value of the INTEGER PRIMARY KEY column is always a NULL. |
danielk1977 | f29ce55 | 2002-05-19 23:43:12 +0000 | [diff] [blame] | 606 | ** Whenever this column is read, the record number will be substituted |
| 607 | ** in its place. So will fill this column with a NULL to avoid |
| 608 | ** taking up data space with information that will never be used. */ |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 609 | sqlite3VdbeAddOp(v, OP_Null, 0, 0); |
drh | 9adf9ac | 2002-05-15 11:44:13 +0000 | [diff] [blame] | 610 | continue; |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 611 | } |
| 612 | if( pColumn==0 ){ |
drh | 9adf9ac | 2002-05-15 11:44:13 +0000 | [diff] [blame] | 613 | j = i; |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 614 | }else{ |
drh | 9adf9ac | 2002-05-15 11:44:13 +0000 | [diff] [blame] | 615 | for(j=0; j<pColumn->nId; j++){ |
| 616 | if( pColumn->a[j].idx==i ) break; |
| 617 | } |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 618 | } |
| 619 | if( pColumn && j>=pColumn->nId ){ |
danielk1977 | 7977a17 | 2004-11-09 12:44:37 +0000 | [diff] [blame] | 620 | sqlite3ExprCode(pParse, pTab->aCol[i].pDflt); |
drh | 142e30d | 2002-08-28 03:00:58 +0000 | [diff] [blame] | 621 | }else if( useTempTable ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 622 | sqlite3VdbeAddOp(v, OP_Column, srcTab, j); |
drh | 142e30d | 2002-08-28 03:00:58 +0000 | [diff] [blame] | 623 | }else if( pSelect ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 624 | sqlite3VdbeAddOp(v, OP_Dup, i+nColumn-j, 1); |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 625 | }else{ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 626 | sqlite3ExprCode(pParse, pList->a[j].pExpr); |
drh | 5974a30 | 2000-06-07 14:42:26 +0000 | [diff] [blame] | 627 | } |
drh | bed8690 | 2000-06-02 13:27:59 +0000 | [diff] [blame] | 628 | } |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 629 | |
| 630 | /* Generate code to check constraints and generate index keys and |
danielk1977 | f29ce55 | 2002-05-19 23:43:12 +0000 | [diff] [blame] | 631 | ** do the insertion. |
| 632 | */ |
drh | 4cbdda9 | 2006-06-14 19:00:20 +0000 | [diff] [blame] | 633 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
| 634 | if( IsVirtual(pTab) ){ |
danielk1977 | 1f6eec5 | 2006-06-16 06:17:47 +0000 | [diff] [blame^] | 635 | sqlite3VdbeOp3(v, OP_VUpdate, 1, pTab->nCol+2, |
drh | 4cbdda9 | 2006-06-14 19:00:20 +0000 | [diff] [blame] | 636 | (const char*)pTab->pVtab, P3_VTAB); |
| 637 | }else |
| 638 | #endif |
| 639 | { |
| 640 | sqlite3GenerateConstraintChecks(pParse, pTab, base, 0, keyColumn>=0, |
| 641 | 0, onError, endOfLoop); |
| 642 | sqlite3CompleteInsertion(pParse, pTab, base, 0,0,0, |
drh | dca7684 | 2004-12-07 14:06:13 +0000 | [diff] [blame] | 643 | (triggers_exist & TRIGGER_AFTER)!=0 ? newIdx : -1); |
drh | 4cbdda9 | 2006-06-14 19:00:20 +0000 | [diff] [blame] | 644 | } |
drh | 5cf590c | 2003-04-24 01:45:04 +0000 | [diff] [blame] | 645 | } |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 646 | |
drh | 5cf590c | 2003-04-24 01:45:04 +0000 | [diff] [blame] | 647 | /* Update the count of rows that are inserted |
| 648 | */ |
| 649 | if( (db->flags & SQLITE_CountRows)!=0 ){ |
drh | 15007a9 | 2006-01-08 18:10:17 +0000 | [diff] [blame] | 650 | sqlite3VdbeAddOp(v, OP_MemIncr, 1, iCntMem); |
drh | 5974a30 | 2000-06-07 14:42:26 +0000 | [diff] [blame] | 651 | } |
drh | 1ccde15 | 2000-06-17 13:12:39 +0000 | [diff] [blame] | 652 | |
drh | dca7684 | 2004-12-07 14:06:13 +0000 | [diff] [blame] | 653 | if( triggers_exist ){ |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 654 | /* Close all tables opened */ |
drh | 5cf590c | 2003-04-24 01:45:04 +0000 | [diff] [blame] | 655 | if( !isView ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 656 | sqlite3VdbeAddOp(v, OP_Close, base, 0); |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 657 | for(idx=1, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, idx++){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 658 | sqlite3VdbeAddOp(v, OP_Close, idx+base, 0); |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 659 | } |
| 660 | } |
drh | 1bee3d7 | 2001-10-15 00:44:35 +0000 | [diff] [blame] | 661 | |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 662 | /* Code AFTER triggers */ |
drh | dca7684 | 2004-12-07 14:06:13 +0000 | [diff] [blame] | 663 | if( sqlite3CodeRowTrigger(pParse, TK_INSERT, 0, TRIGGER_AFTER, pTab, |
| 664 | newIdx, -1, onError, endOfLoop) ){ |
danielk1977 | f29ce55 | 2002-05-19 23:43:12 +0000 | [diff] [blame] | 665 | goto insert_cleanup; |
| 666 | } |
drh | 1bee3d7 | 2001-10-15 00:44:35 +0000 | [diff] [blame] | 667 | } |
| 668 | |
drh | 1ccde15 | 2000-06-17 13:12:39 +0000 | [diff] [blame] | 669 | /* The bottom of the loop, if the data source is a SELECT statement |
danielk1977 | f29ce55 | 2002-05-19 23:43:12 +0000 | [diff] [blame] | 670 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 671 | sqlite3VdbeResolveLabel(v, endOfLoop); |
drh | 142e30d | 2002-08-28 03:00:58 +0000 | [diff] [blame] | 672 | if( useTempTable ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 673 | sqlite3VdbeAddOp(v, OP_Next, srcTab, iCont); |
| 674 | sqlite3VdbeResolveLabel(v, iBreak); |
| 675 | sqlite3VdbeAddOp(v, OP_Close, srcTab, 0); |
drh | 142e30d | 2002-08-28 03:00:58 +0000 | [diff] [blame] | 676 | }else if( pSelect ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 677 | sqlite3VdbeAddOp(v, OP_Pop, nColumn, 0); |
| 678 | sqlite3VdbeAddOp(v, OP_Return, 0, 0); |
| 679 | sqlite3VdbeResolveLabel(v, iCleanup); |
drh | 6b56344 | 2001-11-07 16:48:26 +0000 | [diff] [blame] | 680 | } |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 681 | |
drh | 4cbdda9 | 2006-06-14 19:00:20 +0000 | [diff] [blame] | 682 | if( !triggers_exist && !IsVirtual(pTab) ){ |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 683 | /* Close all tables opened */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 684 | sqlite3VdbeAddOp(v, OP_Close, base, 0); |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 685 | for(idx=1, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, idx++){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 686 | sqlite3VdbeAddOp(v, OP_Close, idx+base, 0); |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 687 | } |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 688 | } |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 689 | |
drh | 2958a4e | 2004-11-12 03:56:15 +0000 | [diff] [blame] | 690 | #ifndef SQLITE_OMIT_AUTOINCREMENT |
drh | f338814 | 2004-11-13 03:48:06 +0000 | [diff] [blame] | 691 | /* Update the sqlite_sequence table by storing the content of the |
| 692 | ** counter value in memory counterMem back into the sqlite_sequence |
| 693 | ** table. |
drh | 2958a4e | 2004-11-12 03:56:15 +0000 | [diff] [blame] | 694 | */ |
| 695 | if( pTab->autoInc ){ |
drh | f338814 | 2004-11-13 03:48:06 +0000 | [diff] [blame] | 696 | int iCur = pParse->nTab; |
danielk1977 | f011300 | 2006-01-24 12:09:17 +0000 | [diff] [blame] | 697 | int addr = sqlite3VdbeCurrentAddr(v); |
danielk1977 | c00da10 | 2006-01-07 13:21:04 +0000 | [diff] [blame] | 698 | sqlite3OpenTable(pParse, iCur, iDb, pDb->pSchema->pSeqTab, OP_OpenWrite); |
drh | f338814 | 2004-11-13 03:48:06 +0000 | [diff] [blame] | 699 | sqlite3VdbeAddOp(v, OP_MemLoad, counterRowid, 0); |
danielk1977 | f011300 | 2006-01-24 12:09:17 +0000 | [diff] [blame] | 700 | sqlite3VdbeAddOp(v, OP_NotNull, -1, addr+7); |
drh | f338814 | 2004-11-13 03:48:06 +0000 | [diff] [blame] | 701 | sqlite3VdbeAddOp(v, OP_Pop, 1, 0); |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 702 | sqlite3VdbeAddOp(v, OP_NewRowid, iCur, 0); |
drh | f338814 | 2004-11-13 03:48:06 +0000 | [diff] [blame] | 703 | sqlite3VdbeOp3(v, OP_String8, 0, 0, pTab->zName, 0); |
| 704 | sqlite3VdbeAddOp(v, OP_MemLoad, counterMem, 0); |
| 705 | sqlite3VdbeAddOp(v, OP_MakeRecord, 2, 0); |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 706 | sqlite3VdbeAddOp(v, OP_Insert, iCur, 0); |
drh | f338814 | 2004-11-13 03:48:06 +0000 | [diff] [blame] | 707 | sqlite3VdbeAddOp(v, OP_Close, iCur, 0); |
drh | 2958a4e | 2004-11-12 03:56:15 +0000 | [diff] [blame] | 708 | } |
| 709 | #endif |
| 710 | |
drh | 1bee3d7 | 2001-10-15 00:44:35 +0000 | [diff] [blame] | 711 | /* |
danielk1977 | e7de6f2 | 2004-11-05 06:02:06 +0000 | [diff] [blame] | 712 | ** Return the number of rows inserted. If this routine is |
| 713 | ** generating code because of a call to sqlite3NestedParse(), do not |
| 714 | ** invoke the callback function. |
drh | 1bee3d7 | 2001-10-15 00:44:35 +0000 | [diff] [blame] | 715 | */ |
danielk1977 | cc6bd38 | 2005-01-10 02:48:49 +0000 | [diff] [blame] | 716 | if( db->flags & SQLITE_CountRows && pParse->nested==0 && !pParse->trigStack ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 717 | sqlite3VdbeAddOp(v, OP_MemLoad, iCntMem, 0); |
| 718 | sqlite3VdbeAddOp(v, OP_Callback, 1, 0); |
danielk1977 | 22322fd | 2004-05-25 23:35:17 +0000 | [diff] [blame] | 719 | sqlite3VdbeSetNumCols(v, 1); |
danielk1977 | 955de52 | 2006-02-10 02:27:42 +0000 | [diff] [blame] | 720 | sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "rows inserted", P3_STATIC); |
drh | 1bee3d7 | 2001-10-15 00:44:35 +0000 | [diff] [blame] | 721 | } |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 722 | |
| 723 | insert_cleanup: |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 724 | sqlite3SrcListDelete(pTabList); |
danielk1977 | d5d5652 | 2005-03-16 12:15:20 +0000 | [diff] [blame] | 725 | sqlite3ExprListDelete(pList); |
| 726 | sqlite3SelectDelete(pSelect); |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 727 | sqlite3IdListDelete(pColumn); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 728 | } |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 729 | |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 730 | /* |
| 731 | ** Generate code to do a constraint check prior to an INSERT or an UPDATE. |
| 732 | ** |
| 733 | ** When this routine is called, the stack contains (from bottom to top) |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 734 | ** the following values: |
| 735 | ** |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 736 | ** 1. The rowid of the row to be updated before the update. This |
drh | b419a92 | 2002-01-30 16:17:23 +0000 | [diff] [blame] | 737 | ** value is omitted unless we are doing an UPDATE that involves a |
| 738 | ** change to the record number. |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 739 | ** |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 740 | ** 2. The rowid of the row after the update. |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 741 | ** |
| 742 | ** 3. The data in the first column of the entry after the update. |
| 743 | ** |
| 744 | ** i. Data from middle columns... |
| 745 | ** |
| 746 | ** N. The data in the last column of the entry after the update. |
| 747 | ** |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 748 | ** The old rowid shown as entry (1) above is omitted unless both isUpdate |
| 749 | ** and rowidChng are 1. isUpdate is true for UPDATEs and false for |
| 750 | ** INSERTs and rowidChng is true if the record number is being changed. |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 751 | ** |
| 752 | ** The code generated by this routine pushes additional entries onto |
| 753 | ** the stack which are the keys for new index entries for the new record. |
| 754 | ** The order of index keys is the same as the order of the indices on |
| 755 | ** the pTable->pIndex list. A key is only created for index i if |
| 756 | ** aIdxUsed!=0 and aIdxUsed[i]!=0. |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 757 | ** |
| 758 | ** This routine also generates code to check constraints. NOT NULL, |
| 759 | ** CHECK, and UNIQUE constraints are all checked. If a constraint fails, |
drh | 1c92853 | 2002-01-31 15:54:21 +0000 | [diff] [blame] | 760 | ** then the appropriate action is performed. There are five possible |
| 761 | ** actions: ROLLBACK, ABORT, FAIL, REPLACE, and IGNORE. |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 762 | ** |
| 763 | ** Constraint type Action What Happens |
| 764 | ** --------------- ---------- ---------------------------------------- |
drh | 1c92853 | 2002-01-31 15:54:21 +0000 | [diff] [blame] | 765 | ** any ROLLBACK The current transaction is rolled back and |
danielk1977 | 24b03fd | 2004-05-10 10:34:34 +0000 | [diff] [blame] | 766 | ** sqlite3_exec() returns immediately with a |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 767 | ** return code of SQLITE_CONSTRAINT. |
| 768 | ** |
drh | 1c92853 | 2002-01-31 15:54:21 +0000 | [diff] [blame] | 769 | ** any ABORT Back out changes from the current command |
| 770 | ** only (do not do a complete rollback) then |
danielk1977 | 24b03fd | 2004-05-10 10:34:34 +0000 | [diff] [blame] | 771 | ** cause sqlite3_exec() to return immediately |
drh | 1c92853 | 2002-01-31 15:54:21 +0000 | [diff] [blame] | 772 | ** with SQLITE_CONSTRAINT. |
| 773 | ** |
| 774 | ** any FAIL Sqlite_exec() returns immediately with a |
| 775 | ** return code of SQLITE_CONSTRAINT. The |
| 776 | ** transaction is not rolled back and any |
| 777 | ** prior changes are retained. |
| 778 | ** |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 779 | ** any IGNORE The record number and data is popped from |
| 780 | ** the stack and there is an immediate jump |
| 781 | ** to label ignoreDest. |
| 782 | ** |
| 783 | ** NOT NULL REPLACE The NULL value is replace by the default |
| 784 | ** value for that column. If the default value |
| 785 | ** is NULL, the action is the same as ABORT. |
| 786 | ** |
| 787 | ** UNIQUE REPLACE The other row that conflicts with the row |
| 788 | ** being inserted is removed. |
| 789 | ** |
| 790 | ** CHECK REPLACE Illegal. The results in an exception. |
| 791 | ** |
drh | 1c92853 | 2002-01-31 15:54:21 +0000 | [diff] [blame] | 792 | ** Which action to take is determined by the overrideError parameter. |
| 793 | ** Or if overrideError==OE_Default, then the pParse->onError parameter |
| 794 | ** is used. Or if pParse->onError==OE_Default then the onError value |
| 795 | ** for the constraint is used. |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 796 | ** |
drh | aaab572 | 2002-02-19 13:39:21 +0000 | [diff] [blame] | 797 | ** The calling routine must open a read/write cursor for pTab with |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 798 | ** cursor number "base". All indices of pTab must also have open |
| 799 | ** read/write cursors with cursor number base+i for the i-th cursor. |
| 800 | ** Except, if there is no possibility of a REPLACE action then |
| 801 | ** cursors do not need to be open for indices where aIdxUsed[i]==0. |
| 802 | ** |
| 803 | ** If the isUpdate flag is true, it means that the "base" cursor is |
| 804 | ** initially pointing to an entry that is being updated. The isUpdate |
| 805 | ** flag causes extra code to be generated so that the "base" cursor |
| 806 | ** is still pointing at the same entry after the routine returns. |
| 807 | ** Without the isUpdate flag, the "base" cursor might be moved. |
| 808 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 809 | void sqlite3GenerateConstraintChecks( |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 810 | Parse *pParse, /* The parser context */ |
| 811 | Table *pTab, /* the table into which we are inserting */ |
| 812 | int base, /* Index of a read/write cursor pointing at pTab */ |
| 813 | char *aIdxUsed, /* Which indices are used. NULL means all are used */ |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 814 | int rowidChng, /* True if the record number will change */ |
drh | b419a92 | 2002-01-30 16:17:23 +0000 | [diff] [blame] | 815 | int isUpdate, /* True for UPDATE, False for INSERT */ |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 816 | int overrideError, /* Override onError to this if not OE_Default */ |
drh | b419a92 | 2002-01-30 16:17:23 +0000 | [diff] [blame] | 817 | int ignoreDest /* Jump to this label on an OE_Ignore resolution */ |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 818 | ){ |
| 819 | int i; |
| 820 | Vdbe *v; |
| 821 | int nCol; |
| 822 | int onError; |
| 823 | int addr; |
| 824 | int extra; |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 825 | int iCur; |
| 826 | Index *pIdx; |
| 827 | int seenReplace = 0; |
danielk1977 | cfe9a69 | 2004-06-16 12:00:29 +0000 | [diff] [blame] | 828 | int jumpInst1=0, jumpInst2; |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 829 | int hasTwoRowids = (isUpdate && rowidChng); |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 830 | |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 831 | v = sqlite3GetVdbe(pParse); |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 832 | assert( v!=0 ); |
drh | 417be79 | 2002-03-03 18:59:40 +0000 | [diff] [blame] | 833 | assert( pTab->pSelect==0 ); /* This table is not a VIEW */ |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 834 | nCol = pTab->nCol; |
| 835 | |
| 836 | /* Test all NOT NULL constraints. |
| 837 | */ |
| 838 | for(i=0; i<nCol; i++){ |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 839 | if( i==pTab->iPKey ){ |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 840 | continue; |
| 841 | } |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 842 | onError = pTab->aCol[i].notNull; |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 843 | if( onError==OE_None ) continue; |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 844 | if( overrideError!=OE_Default ){ |
| 845 | onError = overrideError; |
drh | a996e47 | 2003-05-16 02:30:27 +0000 | [diff] [blame] | 846 | }else if( onError==OE_Default ){ |
| 847 | onError = OE_Abort; |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 848 | } |
danielk1977 | 7977a17 | 2004-11-09 12:44:37 +0000 | [diff] [blame] | 849 | if( onError==OE_Replace && pTab->aCol[i].pDflt==0 ){ |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 850 | onError = OE_Abort; |
| 851 | } |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 852 | sqlite3VdbeAddOp(v, OP_Dup, nCol-1-i, 1); |
| 853 | addr = sqlite3VdbeAddOp(v, OP_NotNull, 1, 0); |
danielk1977 | b84f96f | 2005-01-20 11:32:23 +0000 | [diff] [blame] | 854 | assert( onError==OE_Rollback || onError==OE_Abort || onError==OE_Fail |
| 855 | || onError==OE_Ignore || onError==OE_Replace ); |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 856 | switch( onError ){ |
drh | 1c92853 | 2002-01-31 15:54:21 +0000 | [diff] [blame] | 857 | case OE_Rollback: |
| 858 | case OE_Abort: |
| 859 | case OE_Fail: { |
drh | 483750b | 2003-01-29 18:46:51 +0000 | [diff] [blame] | 860 | char *zMsg = 0; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 861 | sqlite3VdbeAddOp(v, OP_Halt, SQLITE_CONSTRAINT, onError); |
| 862 | sqlite3SetString(&zMsg, pTab->zName, ".", pTab->aCol[i].zName, |
drh | 4174398 | 2003-12-06 21:43:55 +0000 | [diff] [blame] | 863 | " may not be NULL", (char*)0); |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 864 | sqlite3VdbeChangeP3(v, -1, zMsg, P3_DYNAMIC); |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 865 | break; |
| 866 | } |
| 867 | case OE_Ignore: { |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 868 | sqlite3VdbeAddOp(v, OP_Pop, nCol+1+hasTwoRowids, 0); |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 869 | sqlite3VdbeAddOp(v, OP_Goto, 0, ignoreDest); |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 870 | break; |
| 871 | } |
| 872 | case OE_Replace: { |
danielk1977 | 7977a17 | 2004-11-09 12:44:37 +0000 | [diff] [blame] | 873 | sqlite3ExprCode(pParse, pTab->aCol[i].pDflt); |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 874 | sqlite3VdbeAddOp(v, OP_Push, nCol-i, 0); |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 875 | break; |
| 876 | } |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 877 | } |
drh | d654be8 | 2005-09-20 17:42:23 +0000 | [diff] [blame] | 878 | sqlite3VdbeJumpHere(v, addr); |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 879 | } |
| 880 | |
| 881 | /* Test all CHECK constraints |
| 882 | */ |
drh | ffe07b2 | 2005-11-03 00:41:17 +0000 | [diff] [blame] | 883 | #ifndef SQLITE_OMIT_CHECK |
drh | 0cd2d4c | 2005-11-03 02:15:02 +0000 | [diff] [blame] | 884 | if( pTab->pCheck && (pParse->db->flags & SQLITE_IgnoreChecks)==0 ){ |
drh | ffe07b2 | 2005-11-03 00:41:17 +0000 | [diff] [blame] | 885 | int allOk = sqlite3VdbeMakeLabel(v); |
| 886 | assert( pParse->ckOffset==0 ); |
| 887 | pParse->ckOffset = nCol; |
drh | 6275b88 | 2005-11-03 01:22:30 +0000 | [diff] [blame] | 888 | sqlite3ExprIfTrue(pParse, pTab->pCheck, allOk, 1); |
drh | ffe07b2 | 2005-11-03 00:41:17 +0000 | [diff] [blame] | 889 | assert( pParse->ckOffset==nCol ); |
| 890 | pParse->ckOffset = 0; |
drh | aa01c7e | 2006-03-15 16:26:10 +0000 | [diff] [blame] | 891 | onError = overrideError!=OE_Default ? overrideError : OE_Abort; |
| 892 | if( onError==OE_Ignore || onError==OE_Replace ){ |
| 893 | sqlite3VdbeAddOp(v, OP_Pop, nCol+1+hasTwoRowids, 0); |
| 894 | sqlite3VdbeAddOp(v, OP_Goto, 0, ignoreDest); |
| 895 | }else{ |
| 896 | sqlite3VdbeAddOp(v, OP_Halt, SQLITE_CONSTRAINT, onError); |
| 897 | } |
drh | ffe07b2 | 2005-11-03 00:41:17 +0000 | [diff] [blame] | 898 | sqlite3VdbeResolveLabel(v, allOk); |
| 899 | } |
| 900 | #endif /* !defined(SQLITE_OMIT_CHECK) */ |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 901 | |
drh | 0bd1f4e | 2002-06-06 18:54:39 +0000 | [diff] [blame] | 902 | /* If we have an INTEGER PRIMARY KEY, make sure the primary key |
| 903 | ** of the new record does not previously exist. Except, if this |
| 904 | ** is an UPDATE and the primary key is not changing, that is OK. |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 905 | */ |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 906 | if( rowidChng ){ |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 907 | onError = pTab->keyConf; |
| 908 | if( overrideError!=OE_Default ){ |
| 909 | onError = overrideError; |
drh | a996e47 | 2003-05-16 02:30:27 +0000 | [diff] [blame] | 910 | }else if( onError==OE_Default ){ |
| 911 | onError = OE_Abort; |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 912 | } |
drh | a0217ba | 2003-06-01 01:10:33 +0000 | [diff] [blame] | 913 | |
drh | 5383ae5 | 2003-06-04 12:23:30 +0000 | [diff] [blame] | 914 | if( isUpdate ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 915 | sqlite3VdbeAddOp(v, OP_Dup, nCol+1, 1); |
| 916 | sqlite3VdbeAddOp(v, OP_Dup, nCol+1, 1); |
| 917 | jumpInst1 = sqlite3VdbeAddOp(v, OP_Eq, 0, 0); |
drh | 5383ae5 | 2003-06-04 12:23:30 +0000 | [diff] [blame] | 918 | } |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 919 | sqlite3VdbeAddOp(v, OP_Dup, nCol, 1); |
| 920 | jumpInst2 = sqlite3VdbeAddOp(v, OP_NotExists, base, 0); |
drh | 5383ae5 | 2003-06-04 12:23:30 +0000 | [diff] [blame] | 921 | switch( onError ){ |
| 922 | default: { |
| 923 | onError = OE_Abort; |
| 924 | /* Fall thru into the next case */ |
drh | 79b0c95 | 2002-05-21 12:56:43 +0000 | [diff] [blame] | 925 | } |
drh | 5383ae5 | 2003-06-04 12:23:30 +0000 | [diff] [blame] | 926 | case OE_Rollback: |
| 927 | case OE_Abort: |
| 928 | case OE_Fail: { |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 929 | sqlite3VdbeOp3(v, OP_Halt, SQLITE_CONSTRAINT, onError, |
drh | 701a0ae | 2004-02-22 20:05:00 +0000 | [diff] [blame] | 930 | "PRIMARY KEY must be unique", P3_STATIC); |
drh | 5383ae5 | 2003-06-04 12:23:30 +0000 | [diff] [blame] | 931 | break; |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 932 | } |
drh | 5383ae5 | 2003-06-04 12:23:30 +0000 | [diff] [blame] | 933 | case OE_Replace: { |
drh | 7416170 | 2006-02-24 02:53:49 +0000 | [diff] [blame] | 934 | sqlite3GenerateRowIndexDelete(v, pTab, base, 0); |
drh | 5383ae5 | 2003-06-04 12:23:30 +0000 | [diff] [blame] | 935 | if( isUpdate ){ |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 936 | sqlite3VdbeAddOp(v, OP_Dup, nCol+hasTwoRowids, 1); |
drh | 7cf6e4d | 2004-05-19 14:56:55 +0000 | [diff] [blame] | 937 | sqlite3VdbeAddOp(v, OP_MoveGe, base, 0); |
drh | 5383ae5 | 2003-06-04 12:23:30 +0000 | [diff] [blame] | 938 | } |
| 939 | seenReplace = 1; |
| 940 | break; |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 941 | } |
drh | 5383ae5 | 2003-06-04 12:23:30 +0000 | [diff] [blame] | 942 | case OE_Ignore: { |
| 943 | assert( seenReplace==0 ); |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 944 | sqlite3VdbeAddOp(v, OP_Pop, nCol+1+hasTwoRowids, 0); |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 945 | sqlite3VdbeAddOp(v, OP_Goto, 0, ignoreDest); |
drh | 5383ae5 | 2003-06-04 12:23:30 +0000 | [diff] [blame] | 946 | break; |
| 947 | } |
| 948 | } |
drh | d654be8 | 2005-09-20 17:42:23 +0000 | [diff] [blame] | 949 | sqlite3VdbeJumpHere(v, jumpInst2); |
drh | 5383ae5 | 2003-06-04 12:23:30 +0000 | [diff] [blame] | 950 | if( isUpdate ){ |
drh | d654be8 | 2005-09-20 17:42:23 +0000 | [diff] [blame] | 951 | sqlite3VdbeJumpHere(v, jumpInst1); |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 952 | sqlite3VdbeAddOp(v, OP_Dup, nCol+1, 1); |
drh | 7cf6e4d | 2004-05-19 14:56:55 +0000 | [diff] [blame] | 953 | sqlite3VdbeAddOp(v, OP_MoveGe, base, 0); |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 954 | } |
| 955 | } |
drh | 0bd1f4e | 2002-06-06 18:54:39 +0000 | [diff] [blame] | 956 | |
| 957 | /* Test all UNIQUE constraints by creating entries for each UNIQUE |
| 958 | ** index and making sure that duplicate entries do not already exist. |
| 959 | ** Add the new records to the indices as we go. |
| 960 | */ |
drh | b2fe7d8 | 2003-04-20 17:29:23 +0000 | [diff] [blame] | 961 | extra = -1; |
| 962 | for(iCur=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, iCur++){ |
| 963 | if( aIdxUsed && aIdxUsed[iCur]==0 ) continue; /* Skip unused indices */ |
| 964 | extra++; |
| 965 | |
| 966 | /* Create a key for accessing the index entry */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 967 | sqlite3VdbeAddOp(v, OP_Dup, nCol+extra, 1); |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 968 | for(i=0; i<pIdx->nColumn; i++){ |
| 969 | int idx = pIdx->aiColumn[i]; |
| 970 | if( idx==pTab->iPKey ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 971 | sqlite3VdbeAddOp(v, OP_Dup, i+extra+nCol+1, 1); |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 972 | }else{ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 973 | sqlite3VdbeAddOp(v, OP_Dup, i+extra+nCol-idx, 1); |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 974 | } |
| 975 | } |
drh | 7f057c9 | 2005-06-24 03:53:06 +0000 | [diff] [blame] | 976 | jumpInst1 = sqlite3VdbeAddOp(v, OP_MakeIdxRec, pIdx->nColumn, 0); |
danielk1977 | a37cdde | 2004-05-16 11:15:36 +0000 | [diff] [blame] | 977 | sqlite3IndexAffinityStr(v, pIdx); |
drh | b2fe7d8 | 2003-04-20 17:29:23 +0000 | [diff] [blame] | 978 | |
| 979 | /* Find out what action to take in case there is an indexing conflict */ |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 980 | onError = pIdx->onError; |
drh | b2fe7d8 | 2003-04-20 17:29:23 +0000 | [diff] [blame] | 981 | if( onError==OE_None ) continue; /* pIdx is not a UNIQUE index */ |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 982 | if( overrideError!=OE_Default ){ |
| 983 | onError = overrideError; |
drh | a996e47 | 2003-05-16 02:30:27 +0000 | [diff] [blame] | 984 | }else if( onError==OE_Default ){ |
| 985 | onError = OE_Abort; |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 986 | } |
drh | 5383ae5 | 2003-06-04 12:23:30 +0000 | [diff] [blame] | 987 | if( seenReplace ){ |
| 988 | if( onError==OE_Ignore ) onError = OE_Replace; |
| 989 | else if( onError==OE_Fail ) onError = OE_Abort; |
| 990 | } |
| 991 | |
drh | b2fe7d8 | 2003-04-20 17:29:23 +0000 | [diff] [blame] | 992 | |
| 993 | /* Check to see if the new index entry will be unique */ |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 994 | sqlite3VdbeAddOp(v, OP_Dup, extra+nCol+1+hasTwoRowids, 1); |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 995 | jumpInst2 = sqlite3VdbeAddOp(v, OP_IsUnique, base+iCur+1, 0); |
drh | b2fe7d8 | 2003-04-20 17:29:23 +0000 | [diff] [blame] | 996 | |
| 997 | /* Generate code that executes if the new index entry is not unique */ |
danielk1977 | b84f96f | 2005-01-20 11:32:23 +0000 | [diff] [blame] | 998 | assert( onError==OE_Rollback || onError==OE_Abort || onError==OE_Fail |
| 999 | || onError==OE_Ignore || onError==OE_Replace ); |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 1000 | switch( onError ){ |
drh | 1c92853 | 2002-01-31 15:54:21 +0000 | [diff] [blame] | 1001 | case OE_Rollback: |
| 1002 | case OE_Abort: |
| 1003 | case OE_Fail: { |
drh | 37ed48e | 2003-08-05 13:13:38 +0000 | [diff] [blame] | 1004 | int j, n1, n2; |
| 1005 | char zErrMsg[200]; |
| 1006 | strcpy(zErrMsg, pIdx->nColumn>1 ? "columns " : "column "); |
| 1007 | n1 = strlen(zErrMsg); |
| 1008 | for(j=0; j<pIdx->nColumn && n1<sizeof(zErrMsg)-30; j++){ |
| 1009 | char *zCol = pTab->aCol[pIdx->aiColumn[j]].zName; |
| 1010 | n2 = strlen(zCol); |
| 1011 | if( j>0 ){ |
| 1012 | strcpy(&zErrMsg[n1], ", "); |
| 1013 | n1 += 2; |
| 1014 | } |
| 1015 | if( n1+n2>sizeof(zErrMsg)-30 ){ |
| 1016 | strcpy(&zErrMsg[n1], "..."); |
| 1017 | n1 += 3; |
| 1018 | break; |
| 1019 | }else{ |
| 1020 | strcpy(&zErrMsg[n1], zCol); |
| 1021 | n1 += n2; |
| 1022 | } |
| 1023 | } |
| 1024 | strcpy(&zErrMsg[n1], |
| 1025 | pIdx->nColumn>1 ? " are not unique" : " is not unique"); |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1026 | sqlite3VdbeOp3(v, OP_Halt, SQLITE_CONSTRAINT, onError, zErrMsg, 0); |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 1027 | break; |
| 1028 | } |
| 1029 | case OE_Ignore: { |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 1030 | assert( seenReplace==0 ); |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 1031 | sqlite3VdbeAddOp(v, OP_Pop, nCol+extra+3+hasTwoRowids, 0); |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1032 | sqlite3VdbeAddOp(v, OP_Goto, 0, ignoreDest); |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 1033 | break; |
| 1034 | } |
| 1035 | case OE_Replace: { |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1036 | sqlite3GenerateRowDelete(pParse->db, v, pTab, base, 0); |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 1037 | if( isUpdate ){ |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 1038 | sqlite3VdbeAddOp(v, OP_Dup, nCol+extra+1+hasTwoRowids, 1); |
drh | 7cf6e4d | 2004-05-19 14:56:55 +0000 | [diff] [blame] | 1039 | sqlite3VdbeAddOp(v, OP_MoveGe, base, 0); |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 1040 | } |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 1041 | seenReplace = 1; |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 1042 | break; |
| 1043 | } |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 1044 | } |
drh | 0bd1f4e | 2002-06-06 18:54:39 +0000 | [diff] [blame] | 1045 | #if NULL_DISTINCT_FOR_UNIQUE |
drh | d654be8 | 2005-09-20 17:42:23 +0000 | [diff] [blame] | 1046 | sqlite3VdbeJumpHere(v, jumpInst1); |
drh | 0bd1f4e | 2002-06-06 18:54:39 +0000 | [diff] [blame] | 1047 | #endif |
drh | d654be8 | 2005-09-20 17:42:23 +0000 | [diff] [blame] | 1048 | sqlite3VdbeJumpHere(v, jumpInst2); |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 1049 | } |
| 1050 | } |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 1051 | |
| 1052 | /* |
| 1053 | ** This routine generates code to finish the INSERT or UPDATE operation |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1054 | ** that was started by a prior call to sqlite3GenerateConstraintChecks. |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 1055 | ** The stack must contain keys for all active indices followed by data |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 1056 | ** and the rowid for the new entry. This routine creates the new |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 1057 | ** entries in all indices and in the main table. |
| 1058 | ** |
drh | b419a92 | 2002-01-30 16:17:23 +0000 | [diff] [blame] | 1059 | ** The arguments to this routine should be the same as the first six |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1060 | ** arguments to sqlite3GenerateConstraintChecks. |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 1061 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1062 | void sqlite3CompleteInsertion( |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 1063 | Parse *pParse, /* The parser context */ |
| 1064 | Table *pTab, /* the table into which we are inserting */ |
| 1065 | int base, /* Index of a read/write cursor pointing at pTab */ |
| 1066 | char *aIdxUsed, /* Which indices are used. NULL means all are used */ |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 1067 | int rowidChng, /* True if the record number will change */ |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 1068 | int isUpdate, /* True for UPDATE, False for INSERT */ |
| 1069 | int newIdx /* Index of NEW table for triggers. -1 if none */ |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 1070 | ){ |
| 1071 | int i; |
| 1072 | Vdbe *v; |
| 1073 | int nIdx; |
| 1074 | Index *pIdx; |
danielk1977 | b28af71 | 2004-06-21 06:50:26 +0000 | [diff] [blame] | 1075 | int pik_flags; |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 1076 | |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1077 | v = sqlite3GetVdbe(pParse); |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 1078 | assert( v!=0 ); |
drh | 417be79 | 2002-03-03 18:59:40 +0000 | [diff] [blame] | 1079 | assert( pTab->pSelect==0 ); /* This table is not a VIEW */ |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 1080 | for(nIdx=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, nIdx++){} |
| 1081 | for(i=nIdx-1; i>=0; i--){ |
| 1082 | if( aIdxUsed && aIdxUsed[i]==0 ) continue; |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 1083 | sqlite3VdbeAddOp(v, OP_IdxInsert, base+i+1, 0); |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 1084 | } |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1085 | sqlite3VdbeAddOp(v, OP_MakeRecord, pTab->nCol, 0); |
danielk1977 | a37cdde | 2004-05-16 11:15:36 +0000 | [diff] [blame] | 1086 | sqlite3TableAffinityStr(v, pTab); |
danielk1977 | b84f96f | 2005-01-20 11:32:23 +0000 | [diff] [blame] | 1087 | #ifndef SQLITE_OMIT_TRIGGER |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 1088 | if( newIdx>=0 ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1089 | sqlite3VdbeAddOp(v, OP_Dup, 1, 0); |
| 1090 | sqlite3VdbeAddOp(v, OP_Dup, 1, 0); |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 1091 | sqlite3VdbeAddOp(v, OP_Insert, newIdx, 0); |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 1092 | } |
danielk1977 | b84f96f | 2005-01-20 11:32:23 +0000 | [diff] [blame] | 1093 | #endif |
drh | 4794f73 | 2004-11-05 17:17:50 +0000 | [diff] [blame] | 1094 | if( pParse->nested ){ |
| 1095 | pik_flags = 0; |
| 1096 | }else{ |
danielk1977 | 94eb6a1 | 2005-12-15 15:22:08 +0000 | [diff] [blame] | 1097 | pik_flags = OPFLAG_NCHANGE; |
| 1098 | pik_flags |= (isUpdate?OPFLAG_ISUPDATE:OPFLAG_LASTROWID); |
drh | 4794f73 | 2004-11-05 17:17:50 +0000 | [diff] [blame] | 1099 | } |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 1100 | sqlite3VdbeAddOp(v, OP_Insert, base, pik_flags); |
danielk1977 | 94eb6a1 | 2005-12-15 15:22:08 +0000 | [diff] [blame] | 1101 | if( !pParse->nested ){ |
| 1102 | sqlite3VdbeChangeP3(v, -1, pTab->zName, P3_STATIC); |
| 1103 | } |
danielk1977 | b28af71 | 2004-06-21 06:50:26 +0000 | [diff] [blame] | 1104 | |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 1105 | if( isUpdate && rowidChng ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1106 | sqlite3VdbeAddOp(v, OP_Pop, 1, 0); |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 1107 | } |
| 1108 | } |
drh | cd44690 | 2004-02-24 01:05:31 +0000 | [diff] [blame] | 1109 | |
| 1110 | /* |
drh | 290c194 | 2004-08-21 17:54:45 +0000 | [diff] [blame] | 1111 | ** Generate code that will open cursors for a table and for all |
drh | cd44690 | 2004-02-24 01:05:31 +0000 | [diff] [blame] | 1112 | ** indices of that table. The "base" parameter is the cursor number used |
| 1113 | ** for the table. Indices are opened on subsequent cursors. |
drh | cd44690 | 2004-02-24 01:05:31 +0000 | [diff] [blame] | 1114 | */ |
drh | 290c194 | 2004-08-21 17:54:45 +0000 | [diff] [blame] | 1115 | void sqlite3OpenTableAndIndices( |
| 1116 | Parse *pParse, /* Parsing context */ |
| 1117 | Table *pTab, /* Table to be opened */ |
| 1118 | int base, /* Cursor number assigned to the table */ |
| 1119 | int op /* OP_OpenRead or OP_OpenWrite */ |
| 1120 | ){ |
drh | cd44690 | 2004-02-24 01:05:31 +0000 | [diff] [blame] | 1121 | int i; |
drh | 4cbdda9 | 2006-06-14 19:00:20 +0000 | [diff] [blame] | 1122 | int iDb; |
drh | cd44690 | 2004-02-24 01:05:31 +0000 | [diff] [blame] | 1123 | Index *pIdx; |
drh | 4cbdda9 | 2006-06-14 19:00:20 +0000 | [diff] [blame] | 1124 | Vdbe *v; |
| 1125 | |
| 1126 | if( IsVirtual(pTab) ) return; |
| 1127 | iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema); |
| 1128 | v = sqlite3GetVdbe(pParse); |
drh | cd44690 | 2004-02-24 01:05:31 +0000 | [diff] [blame] | 1129 | assert( v!=0 ); |
danielk1977 | c00da10 | 2006-01-07 13:21:04 +0000 | [diff] [blame] | 1130 | sqlite3OpenTable(pParse, base, iDb, pTab, op); |
drh | cd44690 | 2004-02-24 01:05:31 +0000 | [diff] [blame] | 1131 | for(i=1, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){ |
danielk1977 | b3bf556 | 2006-01-10 17:58:23 +0000 | [diff] [blame] | 1132 | KeyInfo *pKey = sqlite3IndexKeyinfo(pParse, pIdx); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 1133 | assert( pIdx->pSchema==pTab->pSchema ); |
| 1134 | sqlite3VdbeAddOp(v, OP_Integer, iDb, 0); |
drh | 29dda4a | 2005-07-21 18:23:20 +0000 | [diff] [blame] | 1135 | VdbeComment((v, "# %s", pIdx->zName)); |
danielk1977 | b3bf556 | 2006-01-10 17:58:23 +0000 | [diff] [blame] | 1136 | sqlite3VdbeOp3(v, op, i+base, pIdx->tnum, (char*)pKey, P3_KEYINFO_HANDOFF); |
drh | cd44690 | 2004-02-24 01:05:31 +0000 | [diff] [blame] | 1137 | } |
drh | 290c194 | 2004-08-21 17:54:45 +0000 | [diff] [blame] | 1138 | if( pParse->nTab<=base+i ){ |
| 1139 | pParse->nTab = base+i; |
| 1140 | } |
drh | cd44690 | 2004-02-24 01:05:31 +0000 | [diff] [blame] | 1141 | } |