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