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 | b28af71 | 2004-06-21 06:50:26 +0000 | [diff] [blame] | 15 | ** $Id: insert.c,v 1.113 2004/06/21 06:50:28 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 | ** ------------------------------ |
| 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 | |
danielk1977 | a37cdde | 2004-05-16 11:15:36 +0000 | [diff] [blame] | 53 | sqlite3VdbeChangeP3(v, -1, pIdx->zColAff, P3_STATIC); |
| 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 | |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 +0000 | [diff] [blame] | 94 | sqlite3VdbeChangeP3(v, -1, pTab->zColAff, P3_STATIC); |
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 | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 182 | sqlite *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 | 5cf590c | 2003-04-24 01:45:04 +0000 | [diff] [blame] | 191 | int isView; /* True if attempting to insert into a view */ |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 192 | |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 193 | int row_triggers_exist = 0; /* True if there are FOR EACH ROW triggers */ |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 194 | int before_triggers; /* True if there are BEFORE triggers */ |
| 195 | int after_triggers; /* True if there are AFTER triggers */ |
| 196 | int newIdx = -1; /* Cursor for the NEW table */ |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 197 | |
danielk1977 | 24b03fd | 2004-05-10 10:34:34 +0000 | [diff] [blame] | 198 | if( pParse->nErr || sqlite3_malloc_failed ) goto insert_cleanup; |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 199 | db = pParse->db; |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 200 | |
drh | 1ccde15 | 2000-06-17 13:12:39 +0000 | [diff] [blame] | 201 | /* Locate the table into which we will be inserting new information. |
| 202 | */ |
drh | 113088e | 2003-03-20 01:16:58 +0000 | [diff] [blame] | 203 | assert( pTabList->nSrc==1 ); |
| 204 | zTab = pTabList->a[0].zName; |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 205 | if( zTab==0 ) goto insert_cleanup; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 206 | pTab = sqlite3SrcListLookup(pParse, pTabList); |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 207 | if( pTab==0 ){ |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 208 | goto insert_cleanup; |
| 209 | } |
drh | e22a334 | 2003-04-22 20:30:37 +0000 | [diff] [blame] | 210 | assert( pTab->iDb<db->nDb ); |
| 211 | zDb = db->aDb[pTab->iDb].zName; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 212 | if( sqlite3AuthCheck(pParse, SQLITE_INSERT, pTab->zName, 0, zDb) ){ |
drh | 1962bda | 2003-01-12 19:33:52 +0000 | [diff] [blame] | 213 | goto insert_cleanup; |
| 214 | } |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 215 | |
| 216 | /* Ensure that: |
| 217 | * (a) the table is not read-only, |
| 218 | * (b) that if it is a view then ON INSERT triggers exist |
| 219 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 220 | before_triggers = sqlite3TriggersExist(pParse, pTab->pTrigger, TK_INSERT, |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 221 | TK_BEFORE, TK_ROW, 0); |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 222 | after_triggers = sqlite3TriggersExist(pParse, pTab->pTrigger, TK_INSERT, |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 223 | TK_AFTER, TK_ROW, 0); |
| 224 | row_triggers_exist = before_triggers || after_triggers; |
drh | 5cf590c | 2003-04-24 01:45:04 +0000 | [diff] [blame] | 225 | isView = pTab->pSelect!=0; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 226 | if( sqlite3IsReadOnly(pParse, pTab, before_triggers) ){ |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 227 | goto insert_cleanup; |
| 228 | } |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 229 | if( pTab==0 ) goto insert_cleanup; |
drh | 1ccde15 | 2000-06-17 13:12:39 +0000 | [diff] [blame] | 230 | |
drh | f573c99 | 2002-07-31 00:32:50 +0000 | [diff] [blame] | 231 | /* If pTab is really a view, make sure it has been initialized. |
| 232 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 233 | if( isView && sqlite3ViewGetColumnNames(pParse, pTab) ){ |
drh | 5cf590c | 2003-04-24 01:45:04 +0000 | [diff] [blame] | 234 | goto insert_cleanup; |
drh | f573c99 | 2002-07-31 00:32:50 +0000 | [diff] [blame] | 235 | } |
| 236 | |
danielk1977 | 7cedc8d | 2004-06-10 10:50:08 +0000 | [diff] [blame] | 237 | /* Ensure all required collation sequences are available. */ |
| 238 | for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){ |
| 239 | if( sqlite3CheckIndexCollSeq(pParse, pIdx) ){ |
| 240 | goto insert_cleanup; |
| 241 | } |
| 242 | } |
| 243 | |
drh | 1ccde15 | 2000-06-17 13:12:39 +0000 | [diff] [blame] | 244 | /* Allocate a VDBE |
| 245 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 246 | v = sqlite3GetVdbe(pParse); |
drh | 5974a30 | 2000-06-07 14:42:26 +0000 | [diff] [blame] | 247 | if( v==0 ) goto insert_cleanup; |
danielk1977 | b28af71 | 2004-06-21 06:50:26 +0000 | [diff] [blame] | 248 | sqlite3VdbeCountChanges(v); |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 249 | sqlite3BeginWriteOperation(pParse, pSelect || row_triggers_exist, pTab->iDb); |
drh | 1ccde15 | 2000-06-17 13:12:39 +0000 | [diff] [blame] | 250 | |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 251 | /* if there are row triggers, allocate a temp table for new.* references. */ |
danielk1977 | f29ce55 | 2002-05-19 23:43:12 +0000 | [diff] [blame] | 252 | if( row_triggers_exist ){ |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 253 | newIdx = pParse->nTab++; |
danielk1977 | f29ce55 | 2002-05-19 23:43:12 +0000 | [diff] [blame] | 254 | } |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 255 | |
drh | 1ccde15 | 2000-06-17 13:12:39 +0000 | [diff] [blame] | 256 | /* Figure out how many columns of data are supplied. If the data |
drh | 142e30d | 2002-08-28 03:00:58 +0000 | [diff] [blame] | 257 | ** is coming from a SELECT statement, then this step also generates |
| 258 | ** all the code to implement the SELECT statement and invoke a subroutine |
| 259 | ** to process each row of the result. (Template 2.) If the SELECT |
| 260 | ** statement uses the the table that is being inserted into, then the |
| 261 | ** subroutine is also coded here. That subroutine stores the SELECT |
| 262 | ** results in a temporary table. (Template 3.) |
drh | 1ccde15 | 2000-06-17 13:12:39 +0000 | [diff] [blame] | 263 | */ |
drh | 5974a30 | 2000-06-07 14:42:26 +0000 | [diff] [blame] | 264 | if( pSelect ){ |
drh | 142e30d | 2002-08-28 03:00:58 +0000 | [diff] [blame] | 265 | /* Data is coming from a SELECT. Generate code to implement that SELECT |
| 266 | */ |
| 267 | int rc, iInitCode; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 268 | iInitCode = sqlite3VdbeAddOp(v, OP_Goto, 0, 0); |
| 269 | iSelectLoop = sqlite3VdbeCurrentAddr(v); |
| 270 | iInsertBlock = sqlite3VdbeMakeLabel(v); |
danielk1977 | 84ac9d0 | 2004-05-18 09:58:06 +0000 | [diff] [blame] | 271 | rc = sqlite3Select(pParse, pSelect, SRT_Subroutine, iInsertBlock, 0,0,0,0); |
danielk1977 | 24b03fd | 2004-05-10 10:34:34 +0000 | [diff] [blame] | 272 | if( rc || pParse->nErr || sqlite3_malloc_failed ) goto insert_cleanup; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 273 | iCleanup = sqlite3VdbeMakeLabel(v); |
| 274 | sqlite3VdbeAddOp(v, OP_Goto, 0, iCleanup); |
drh | 5974a30 | 2000-06-07 14:42:26 +0000 | [diff] [blame] | 275 | assert( pSelect->pEList ); |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 276 | nColumn = pSelect->pEList->nExpr; |
drh | 142e30d | 2002-08-28 03:00:58 +0000 | [diff] [blame] | 277 | |
| 278 | /* Set useTempTable to TRUE if the result of the SELECT statement |
| 279 | ** should be written into a temporary table. Set to FALSE if each |
| 280 | ** row of the SELECT can be written directly into the result table. |
drh | 048c530 | 2003-04-03 01:50:44 +0000 | [diff] [blame] | 281 | ** |
| 282 | ** A temp table must be used if the table being updated is also one |
| 283 | ** of the tables being read by the SELECT statement. Also use a |
| 284 | ** temp table in the case of row triggers. |
drh | 142e30d | 2002-08-28 03:00:58 +0000 | [diff] [blame] | 285 | */ |
drh | 048c530 | 2003-04-03 01:50:44 +0000 | [diff] [blame] | 286 | if( row_triggers_exist ){ |
| 287 | useTempTable = 1; |
| 288 | }else{ |
danielk1977 | 84ac9d0 | 2004-05-18 09:58:06 +0000 | [diff] [blame] | 289 | int addr = sqlite3VdbeFindOp(v, 0, OP_OpenRead, pTab->tnum); |
drh | 048c530 | 2003-04-03 01:50:44 +0000 | [diff] [blame] | 290 | useTempTable = 0; |
| 291 | if( addr>0 ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 292 | VdbeOp *pOp = sqlite3VdbeGetOp(v, addr-2); |
drh | 048c530 | 2003-04-03 01:50:44 +0000 | [diff] [blame] | 293 | if( pOp->opcode==OP_Integer && pOp->p1==pTab->iDb ){ |
| 294 | useTempTable = 1; |
| 295 | } |
| 296 | } |
| 297 | } |
drh | 142e30d | 2002-08-28 03:00:58 +0000 | [diff] [blame] | 298 | |
| 299 | if( useTempTable ){ |
| 300 | /* Generate the subroutine that SELECT calls to process each row of |
| 301 | ** the result. Store the result in a temporary table |
| 302 | */ |
| 303 | srcTab = pParse->nTab++; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 304 | sqlite3VdbeResolveLabel(v, iInsertBlock); |
| 305 | sqlite3VdbeAddOp(v, OP_MakeRecord, nColumn, 0); |
danielk1977 | a37cdde | 2004-05-16 11:15:36 +0000 | [diff] [blame] | 306 | sqlite3TableAffinityStr(v, pTab); |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 307 | sqlite3VdbeAddOp(v, OP_NewRecno, srcTab, 0); |
| 308 | sqlite3VdbeAddOp(v, OP_Pull, 1, 0); |
| 309 | sqlite3VdbeAddOp(v, OP_PutIntKey, srcTab, 0); |
| 310 | sqlite3VdbeAddOp(v, OP_Return, 0, 0); |
drh | 142e30d | 2002-08-28 03:00:58 +0000 | [diff] [blame] | 311 | |
| 312 | /* The following code runs first because the GOTO at the very top |
| 313 | ** of the program jumps to it. Create the temporary table, then jump |
| 314 | ** back up and execute the SELECT code above. |
| 315 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 316 | sqlite3VdbeChangeP2(v, iInitCode, sqlite3VdbeCurrentAddr(v)); |
| 317 | sqlite3VdbeAddOp(v, OP_OpenTemp, srcTab, 0); |
drh | b6f5452 | 2004-05-20 02:42:16 +0000 | [diff] [blame] | 318 | sqlite3VdbeAddOp(v, OP_SetNumColumns, srcTab, nColumn); |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 319 | sqlite3VdbeAddOp(v, OP_Goto, 0, iSelectLoop); |
| 320 | sqlite3VdbeResolveLabel(v, iCleanup); |
drh | 142e30d | 2002-08-28 03:00:58 +0000 | [diff] [blame] | 321 | }else{ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 322 | sqlite3VdbeChangeP2(v, iInitCode, sqlite3VdbeCurrentAddr(v)); |
drh | 142e30d | 2002-08-28 03:00:58 +0000 | [diff] [blame] | 323 | } |
drh | 5974a30 | 2000-06-07 14:42:26 +0000 | [diff] [blame] | 324 | }else{ |
drh | 142e30d | 2002-08-28 03:00:58 +0000 | [diff] [blame] | 325 | /* This is the case if the data for the INSERT is coming from a VALUES |
| 326 | ** clause |
| 327 | */ |
drh | ad3cab5 | 2002-05-24 02:04:32 +0000 | [diff] [blame] | 328 | SrcList dummy; |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 329 | assert( pList!=0 ); |
drh | 5974a30 | 2000-06-07 14:42:26 +0000 | [diff] [blame] | 330 | srcTab = -1; |
drh | 142e30d | 2002-08-28 03:00:58 +0000 | [diff] [blame] | 331 | useTempTable = 0; |
drh | 5974a30 | 2000-06-07 14:42:26 +0000 | [diff] [blame] | 332 | assert( pList ); |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 333 | nColumn = pList->nExpr; |
drh | ad3cab5 | 2002-05-24 02:04:32 +0000 | [diff] [blame] | 334 | dummy.nSrc = 0; |
drh | e64e7b2 | 2002-02-18 13:56:36 +0000 | [diff] [blame] | 335 | for(i=0; i<nColumn; i++){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 336 | if( sqlite3ExprResolveIds(pParse, &dummy, 0, pList->a[i].pExpr) ){ |
drh | e64e7b2 | 2002-02-18 13:56:36 +0000 | [diff] [blame] | 337 | goto insert_cleanup; |
| 338 | } |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 339 | if( sqlite3ExprCheck(pParse, pList->a[i].pExpr, 0, 0) ){ |
drh | b04a5d8 | 2002-04-12 03:55:15 +0000 | [diff] [blame] | 340 | goto insert_cleanup; |
| 341 | } |
drh | e64e7b2 | 2002-02-18 13:56:36 +0000 | [diff] [blame] | 342 | } |
drh | 5974a30 | 2000-06-07 14:42:26 +0000 | [diff] [blame] | 343 | } |
drh | 1ccde15 | 2000-06-17 13:12:39 +0000 | [diff] [blame] | 344 | |
| 345 | /* Make sure the number of columns in the source data matches the number |
| 346 | ** of columns to be inserted into the table. |
| 347 | */ |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 348 | if( pColumn==0 && nColumn!=pTab->nCol ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 349 | sqlite3ErrorMsg(pParse, |
drh | da93d23 | 2003-03-31 02:12:46 +0000 | [diff] [blame] | 350 | "table %S has %d columns but %d values were supplied", |
| 351 | pTabList, 0, pTab->nCol, nColumn); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 352 | goto insert_cleanup; |
| 353 | } |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 354 | if( pColumn!=0 && nColumn!=pColumn->nId ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 355 | sqlite3ErrorMsg(pParse, "%d values for %d columns", nColumn, pColumn->nId); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 356 | goto insert_cleanup; |
| 357 | } |
drh | 1ccde15 | 2000-06-17 13:12:39 +0000 | [diff] [blame] | 358 | |
| 359 | /* If the INSERT statement included an IDLIST term, then make sure |
| 360 | ** all elements of the IDLIST really are columns of the table and |
| 361 | ** remember the column indices. |
drh | c839258 | 2001-12-31 02:48:51 +0000 | [diff] [blame] | 362 | ** |
| 363 | ** If the table has an INTEGER PRIMARY KEY column and that column |
| 364 | ** is named in the IDLIST, then record in the keyColumn variable |
| 365 | ** the index into IDLIST of the primary key column. keyColumn is |
| 366 | ** the index of the primary key as it appears in IDLIST, not as |
| 367 | ** is appears in the original table. (The index of the primary |
| 368 | ** key in the original table is pTab->iPKey.) |
drh | 1ccde15 | 2000-06-17 13:12:39 +0000 | [diff] [blame] | 369 | */ |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 370 | if( pColumn ){ |
| 371 | for(i=0; i<pColumn->nId; i++){ |
| 372 | pColumn->a[i].idx = -1; |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 373 | } |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 374 | for(i=0; i<pColumn->nId; i++){ |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 375 | for(j=0; j<pTab->nCol; j++){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 376 | if( sqlite3StrICmp(pColumn->a[i].zName, pTab->aCol[j].zName)==0 ){ |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 377 | pColumn->a[i].idx = j; |
drh | 4a32431 | 2001-12-21 14:30:42 +0000 | [diff] [blame] | 378 | if( j==pTab->iPKey ){ |
drh | 9aa028d | 2001-12-22 21:48:29 +0000 | [diff] [blame] | 379 | keyColumn = i; |
drh | 4a32431 | 2001-12-21 14:30:42 +0000 | [diff] [blame] | 380 | } |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 381 | break; |
| 382 | } |
| 383 | } |
| 384 | if( j>=pTab->nCol ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 385 | if( sqlite3IsRowid(pColumn->a[i].zName) ){ |
drh | a0217ba | 2003-06-01 01:10:33 +0000 | [diff] [blame] | 386 | keyColumn = i; |
| 387 | }else{ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 388 | sqlite3ErrorMsg(pParse, "table %S has no column named %s", |
drh | a0217ba | 2003-06-01 01:10:33 +0000 | [diff] [blame] | 389 | pTabList, 0, pColumn->a[i].zName); |
| 390 | pParse->nErr++; |
| 391 | goto insert_cleanup; |
| 392 | } |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 393 | } |
| 394 | } |
| 395 | } |
drh | 1ccde15 | 2000-06-17 13:12:39 +0000 | [diff] [blame] | 396 | |
drh | aacc543 | 2002-01-06 17:07:40 +0000 | [diff] [blame] | 397 | /* If there is no IDLIST term but the table has an integer primary |
drh | c839258 | 2001-12-31 02:48:51 +0000 | [diff] [blame] | 398 | ** key, the set the keyColumn variable to the primary key column index |
| 399 | ** in the original table definition. |
drh | 4a32431 | 2001-12-21 14:30:42 +0000 | [diff] [blame] | 400 | */ |
| 401 | if( pColumn==0 ){ |
| 402 | keyColumn = pTab->iPKey; |
| 403 | } |
| 404 | |
drh | 142e30d | 2002-08-28 03:00:58 +0000 | [diff] [blame] | 405 | /* Open the temp table for FOR EACH ROW triggers |
| 406 | */ |
danielk1977 | f29ce55 | 2002-05-19 23:43:12 +0000 | [diff] [blame] | 407 | if( row_triggers_exist ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 408 | sqlite3VdbeAddOp(v, OP_OpenPseudo, newIdx, 0); |
danielk1977 | 84ac9d0 | 2004-05-18 09:58:06 +0000 | [diff] [blame] | 409 | sqlite3VdbeAddOp(v, OP_SetNumColumns, newIdx, pTab->nCol); |
danielk1977 | f29ce55 | 2002-05-19 23:43:12 +0000 | [diff] [blame] | 410 | } |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 411 | |
drh | feeb139 | 2002-04-09 03:28:01 +0000 | [diff] [blame] | 412 | /* Initialize the count of rows to be inserted |
| 413 | */ |
drh | 142e30d | 2002-08-28 03:00:58 +0000 | [diff] [blame] | 414 | if( db->flags & SQLITE_CountRows ){ |
| 415 | iCntMem = pParse->nMem++; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 416 | sqlite3VdbeAddOp(v, OP_Integer, 0, 0); |
| 417 | sqlite3VdbeAddOp(v, OP_MemStore, iCntMem, 1); |
drh | feeb139 | 2002-04-09 03:28:01 +0000 | [diff] [blame] | 418 | } |
| 419 | |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 420 | /* Open tables and indices if there are no row triggers */ |
danielk1977 | f29ce55 | 2002-05-19 23:43:12 +0000 | [diff] [blame] | 421 | if( !row_triggers_exist ){ |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 422 | base = pParse->nTab; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 423 | idx = sqlite3OpenTableAndIndices(pParse, pTab, base); |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 424 | pParse->nTab += idx; |
| 425 | } |
| 426 | |
drh | 142e30d | 2002-08-28 03:00:58 +0000 | [diff] [blame] | 427 | /* If the data source is a temporary table, then we have to create |
drh | 1ccde15 | 2000-06-17 13:12:39 +0000 | [diff] [blame] | 428 | ** a loop because there might be multiple rows of data. If the data |
drh | 142e30d | 2002-08-28 03:00:58 +0000 | [diff] [blame] | 429 | ** source is a subroutine call from the SELECT statement, then we need |
| 430 | ** to launch the SELECT statement processing. |
drh | 1ccde15 | 2000-06-17 13:12:39 +0000 | [diff] [blame] | 431 | */ |
drh | 142e30d | 2002-08-28 03:00:58 +0000 | [diff] [blame] | 432 | if( useTempTable ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 433 | iBreak = sqlite3VdbeMakeLabel(v); |
| 434 | sqlite3VdbeAddOp(v, OP_Rewind, srcTab, iBreak); |
| 435 | iCont = sqlite3VdbeCurrentAddr(v); |
drh | 142e30d | 2002-08-28 03:00:58 +0000 | [diff] [blame] | 436 | }else if( pSelect ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 437 | sqlite3VdbeAddOp(v, OP_Goto, 0, iSelectLoop); |
| 438 | sqlite3VdbeResolveLabel(v, iInsertBlock); |
drh | 5974a30 | 2000-06-07 14:42:26 +0000 | [diff] [blame] | 439 | } |
drh | 1ccde15 | 2000-06-17 13:12:39 +0000 | [diff] [blame] | 440 | |
drh | 5cf590c | 2003-04-24 01:45:04 +0000 | [diff] [blame] | 441 | /* Run the BEFORE and INSTEAD OF triggers, if there are any |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 442 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 443 | endOfLoop = sqlite3VdbeMakeLabel(v); |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 444 | if( before_triggers ){ |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 445 | |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 446 | /* build the NEW.* reference row. Note that if there is an INTEGER |
| 447 | ** PRIMARY KEY into which a NULL is being inserted, that NULL will be |
| 448 | ** translated into a unique ID for the row. But on a BEFORE trigger, |
| 449 | ** we do not know what the unique ID will be (because the insert has |
| 450 | ** not happened yet) so we substitute a rowid of -1 |
| 451 | */ |
| 452 | if( keyColumn<0 ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 453 | sqlite3VdbeAddOp(v, OP_Integer, -1, 0); |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 454 | }else if( useTempTable ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 455 | sqlite3VdbeAddOp(v, OP_Column, srcTab, keyColumn); |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 456 | }else if( pSelect ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 457 | sqlite3VdbeAddOp(v, OP_Dup, nColumn - keyColumn - 1, 1); |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 458 | }else{ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 459 | sqlite3ExprCode(pParse, pList->a[keyColumn].pExpr); |
| 460 | sqlite3VdbeAddOp(v, OP_NotNull, -1, sqlite3VdbeCurrentAddr(v)+3); |
| 461 | sqlite3VdbeAddOp(v, OP_Pop, 1, 0); |
| 462 | sqlite3VdbeAddOp(v, OP_Integer, -1, 0); |
| 463 | sqlite3VdbeAddOp(v, OP_MustBeInt, 0, 0); |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 464 | } |
| 465 | |
| 466 | /* Create the new column data |
| 467 | */ |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 468 | for(i=0; i<pTab->nCol; i++){ |
| 469 | if( pColumn==0 ){ |
drh | 9adf9ac | 2002-05-15 11:44:13 +0000 | [diff] [blame] | 470 | j = i; |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 471 | }else{ |
drh | 9adf9ac | 2002-05-15 11:44:13 +0000 | [diff] [blame] | 472 | for(j=0; j<pColumn->nId; j++){ |
| 473 | if( pColumn->a[j].idx==i ) break; |
| 474 | } |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 475 | } |
| 476 | if( pColumn && j>=pColumn->nId ){ |
danielk1977 | 0f69c1e | 2004-05-29 11:24:50 +0000 | [diff] [blame] | 477 | sqlite3VdbeOp3(v, OP_String8, 0, 0, pTab->aCol[i].zDflt, P3_STATIC); |
drh | 142e30d | 2002-08-28 03:00:58 +0000 | [diff] [blame] | 478 | }else if( useTempTable ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 479 | sqlite3VdbeAddOp(v, OP_Column, srcTab, j); |
drh | 142e30d | 2002-08-28 03:00:58 +0000 | [diff] [blame] | 480 | }else if( pSelect ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 481 | sqlite3VdbeAddOp(v, OP_Dup, nColumn-j-1, 1); |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 482 | }else{ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 483 | sqlite3ExprCode(pParse, pList->a[j].pExpr); |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 484 | } |
| 485 | } |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 486 | sqlite3VdbeAddOp(v, OP_MakeRecord, pTab->nCol, 0); |
danielk1977 | a37cdde | 2004-05-16 11:15:36 +0000 | [diff] [blame] | 487 | |
| 488 | /* If this is an INSERT on a view with an INSTEAD OF INSERT trigger, |
| 489 | ** do not attempt any conversions before assembling the record. |
| 490 | ** If this is a real table, attempt conversions as required by the |
| 491 | ** table column affinities. |
| 492 | */ |
| 493 | if( !isView ){ |
| 494 | sqlite3TableAffinityStr(v, pTab); |
| 495 | } |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 496 | sqlite3VdbeAddOp(v, OP_PutIntKey, newIdx, 0); |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 497 | |
drh | 5cf590c | 2003-04-24 01:45:04 +0000 | [diff] [blame] | 498 | /* Fire BEFORE or INSTEAD OF triggers */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 499 | if( sqlite3CodeRowTrigger(pParse, TK_INSERT, 0, TK_BEFORE, pTab, |
drh | b2fe7d8 | 2003-04-20 17:29:23 +0000 | [diff] [blame] | 500 | newIdx, -1, onError, endOfLoop) ){ |
danielk1977 | f29ce55 | 2002-05-19 23:43:12 +0000 | [diff] [blame] | 501 | goto insert_cleanup; |
| 502 | } |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 503 | } |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 504 | |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 505 | /* If any triggers exists, the opening of tables and indices is deferred |
| 506 | ** until now. |
| 507 | */ |
drh | 5cf590c | 2003-04-24 01:45:04 +0000 | [diff] [blame] | 508 | if( row_triggers_exist && !isView ){ |
| 509 | base = pParse->nTab; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 510 | idx = sqlite3OpenTableAndIndices(pParse, pTab, base); |
drh | 5cf590c | 2003-04-24 01:45:04 +0000 | [diff] [blame] | 511 | pParse->nTab += idx; |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 512 | } |
| 513 | |
drh | 4a32431 | 2001-12-21 14:30:42 +0000 | [diff] [blame] | 514 | /* Push the record number for the new entry onto the stack. The |
| 515 | ** record number is a randomly generate integer created by NewRecno |
| 516 | ** except when the table has an INTEGER PRIMARY KEY column, in which |
drh | b419a92 | 2002-01-30 16:17:23 +0000 | [diff] [blame] | 517 | ** case the record number is the same as that column. |
drh | 1ccde15 | 2000-06-17 13:12:39 +0000 | [diff] [blame] | 518 | */ |
drh | 5cf590c | 2003-04-24 01:45:04 +0000 | [diff] [blame] | 519 | if( !isView ){ |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 520 | if( keyColumn>=0 ){ |
drh | 142e30d | 2002-08-28 03:00:58 +0000 | [diff] [blame] | 521 | if( useTempTable ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 522 | sqlite3VdbeAddOp(v, OP_Column, srcTab, keyColumn); |
drh | 142e30d | 2002-08-28 03:00:58 +0000 | [diff] [blame] | 523 | }else if( pSelect ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 524 | sqlite3VdbeAddOp(v, OP_Dup, nColumn - keyColumn - 1, 1); |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 525 | }else{ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 526 | sqlite3ExprCode(pParse, pList->a[keyColumn].pExpr); |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 527 | } |
drh | 27a3278 | 2002-06-19 20:32:43 +0000 | [diff] [blame] | 528 | /* If the PRIMARY KEY expression is NULL, then use OP_NewRecno |
| 529 | ** to generate a unique primary key value. |
| 530 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 531 | sqlite3VdbeAddOp(v, OP_NotNull, -1, sqlite3VdbeCurrentAddr(v)+3); |
| 532 | sqlite3VdbeAddOp(v, OP_Pop, 1, 0); |
| 533 | sqlite3VdbeAddOp(v, OP_NewRecno, base, 0); |
| 534 | sqlite3VdbeAddOp(v, OP_MustBeInt, 0, 0); |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 535 | }else{ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 536 | sqlite3VdbeAddOp(v, OP_NewRecno, base, 0); |
drh | 4a32431 | 2001-12-21 14:30:42 +0000 | [diff] [blame] | 537 | } |
drh | 4a32431 | 2001-12-21 14:30:42 +0000 | [diff] [blame] | 538 | |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 539 | /* Push onto the stack, data for all columns of the new entry, beginning |
danielk1977 | f29ce55 | 2002-05-19 23:43:12 +0000 | [diff] [blame] | 540 | ** with the first column. |
| 541 | */ |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 542 | for(i=0; i<pTab->nCol; i++){ |
| 543 | if( i==pTab->iPKey ){ |
drh | 9adf9ac | 2002-05-15 11:44:13 +0000 | [diff] [blame] | 544 | /* The value of the INTEGER PRIMARY KEY column is always a NULL. |
danielk1977 | f29ce55 | 2002-05-19 23:43:12 +0000 | [diff] [blame] | 545 | ** Whenever this column is read, the record number will be substituted |
| 546 | ** in its place. So will fill this column with a NULL to avoid |
| 547 | ** taking up data space with information that will never be used. */ |
danielk1977 | 0f69c1e | 2004-05-29 11:24:50 +0000 | [diff] [blame] | 548 | sqlite3VdbeAddOp(v, OP_String8, 0, 0); |
drh | 9adf9ac | 2002-05-15 11:44:13 +0000 | [diff] [blame] | 549 | continue; |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 550 | } |
| 551 | if( pColumn==0 ){ |
drh | 9adf9ac | 2002-05-15 11:44:13 +0000 | [diff] [blame] | 552 | j = i; |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 553 | }else{ |
drh | 9adf9ac | 2002-05-15 11:44:13 +0000 | [diff] [blame] | 554 | for(j=0; j<pColumn->nId; j++){ |
| 555 | if( pColumn->a[j].idx==i ) break; |
| 556 | } |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 557 | } |
| 558 | if( pColumn && j>=pColumn->nId ){ |
danielk1977 | 0f69c1e | 2004-05-29 11:24:50 +0000 | [diff] [blame] | 559 | sqlite3VdbeOp3(v, OP_String8, 0, 0, pTab->aCol[i].zDflt, P3_STATIC); |
drh | 142e30d | 2002-08-28 03:00:58 +0000 | [diff] [blame] | 560 | }else if( useTempTable ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 561 | sqlite3VdbeAddOp(v, OP_Column, srcTab, j); |
drh | 142e30d | 2002-08-28 03:00:58 +0000 | [diff] [blame] | 562 | }else if( pSelect ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 563 | sqlite3VdbeAddOp(v, OP_Dup, i+nColumn-j, 1); |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 564 | }else{ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 565 | sqlite3ExprCode(pParse, pList->a[j].pExpr); |
drh | 5974a30 | 2000-06-07 14:42:26 +0000 | [diff] [blame] | 566 | } |
drh | bed8690 | 2000-06-02 13:27:59 +0000 | [diff] [blame] | 567 | } |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 568 | |
| 569 | /* Generate code to check constraints and generate index keys and |
danielk1977 | f29ce55 | 2002-05-19 23:43:12 +0000 | [diff] [blame] | 570 | ** do the insertion. |
| 571 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 572 | sqlite3GenerateConstraintChecks(pParse, pTab, base, 0, keyColumn>=0, |
drh | a0217ba | 2003-06-01 01:10:33 +0000 | [diff] [blame] | 573 | 0, onError, endOfLoop); |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 574 | sqlite3CompleteInsertion(pParse, pTab, base, 0,0,0, |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 575 | after_triggers ? newIdx : -1); |
drh | 5cf590c | 2003-04-24 01:45:04 +0000 | [diff] [blame] | 576 | } |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 577 | |
drh | 5cf590c | 2003-04-24 01:45:04 +0000 | [diff] [blame] | 578 | /* Update the count of rows that are inserted |
| 579 | */ |
| 580 | if( (db->flags & SQLITE_CountRows)!=0 ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 581 | sqlite3VdbeAddOp(v, OP_MemIncr, iCntMem, 0); |
drh | 5974a30 | 2000-06-07 14:42:26 +0000 | [diff] [blame] | 582 | } |
drh | 1ccde15 | 2000-06-17 13:12:39 +0000 | [diff] [blame] | 583 | |
danielk1977 | f29ce55 | 2002-05-19 23:43:12 +0000 | [diff] [blame] | 584 | if( row_triggers_exist ){ |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 585 | /* Close all tables opened */ |
drh | 5cf590c | 2003-04-24 01:45:04 +0000 | [diff] [blame] | 586 | if( !isView ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 587 | sqlite3VdbeAddOp(v, OP_Close, base, 0); |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 588 | for(idx=1, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, idx++){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 589 | sqlite3VdbeAddOp(v, OP_Close, idx+base, 0); |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 590 | } |
| 591 | } |
drh | 1bee3d7 | 2001-10-15 00:44:35 +0000 | [diff] [blame] | 592 | |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 593 | /* Code AFTER triggers */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 594 | if( sqlite3CodeRowTrigger(pParse, TK_INSERT, 0, TK_AFTER, pTab, newIdx, -1, |
danielk1977 | 6f34903 | 2002-06-11 02:25:40 +0000 | [diff] [blame] | 595 | onError, endOfLoop) ){ |
danielk1977 | f29ce55 | 2002-05-19 23:43:12 +0000 | [diff] [blame] | 596 | goto insert_cleanup; |
| 597 | } |
drh | 1bee3d7 | 2001-10-15 00:44:35 +0000 | [diff] [blame] | 598 | } |
| 599 | |
drh | 1ccde15 | 2000-06-17 13:12:39 +0000 | [diff] [blame] | 600 | /* The bottom of the loop, if the data source is a SELECT statement |
danielk1977 | f29ce55 | 2002-05-19 23:43:12 +0000 | [diff] [blame] | 601 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 602 | sqlite3VdbeResolveLabel(v, endOfLoop); |
drh | 142e30d | 2002-08-28 03:00:58 +0000 | [diff] [blame] | 603 | if( useTempTable ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 604 | sqlite3VdbeAddOp(v, OP_Next, srcTab, iCont); |
| 605 | sqlite3VdbeResolveLabel(v, iBreak); |
| 606 | sqlite3VdbeAddOp(v, OP_Close, srcTab, 0); |
drh | 142e30d | 2002-08-28 03:00:58 +0000 | [diff] [blame] | 607 | }else if( pSelect ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 608 | sqlite3VdbeAddOp(v, OP_Pop, nColumn, 0); |
| 609 | sqlite3VdbeAddOp(v, OP_Return, 0, 0); |
| 610 | sqlite3VdbeResolveLabel(v, iCleanup); |
drh | 6b56344 | 2001-11-07 16:48:26 +0000 | [diff] [blame] | 611 | } |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 612 | |
danielk1977 | f29ce55 | 2002-05-19 23:43:12 +0000 | [diff] [blame] | 613 | if( !row_triggers_exist ){ |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 614 | /* Close all tables opened */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 615 | sqlite3VdbeAddOp(v, OP_Close, base, 0); |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 616 | for(idx=1, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, idx++){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 617 | sqlite3VdbeAddOp(v, OP_Close, idx+base, 0); |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 618 | } |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 619 | } |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 620 | |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 621 | sqlite3EndWriteOperation(pParse); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 622 | |
drh | 1bee3d7 | 2001-10-15 00:44:35 +0000 | [diff] [blame] | 623 | /* |
danielk1977 | f29ce55 | 2002-05-19 23:43:12 +0000 | [diff] [blame] | 624 | ** Return the number of rows inserted. |
drh | 1bee3d7 | 2001-10-15 00:44:35 +0000 | [diff] [blame] | 625 | */ |
drh | 142e30d | 2002-08-28 03:00:58 +0000 | [diff] [blame] | 626 | if( db->flags & SQLITE_CountRows ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 627 | sqlite3VdbeAddOp(v, OP_MemLoad, iCntMem, 0); |
| 628 | sqlite3VdbeAddOp(v, OP_Callback, 1, 0); |
danielk1977 | 22322fd | 2004-05-25 23:35:17 +0000 | [diff] [blame] | 629 | sqlite3VdbeSetNumCols(v, 1); |
danielk1977 | 3cf8606 | 2004-05-26 10:11:05 +0000 | [diff] [blame] | 630 | sqlite3VdbeSetColName(v, 0, "rows inserted", P3_STATIC); |
drh | 1bee3d7 | 2001-10-15 00:44:35 +0000 | [diff] [blame] | 631 | } |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 632 | |
| 633 | insert_cleanup: |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 634 | sqlite3SrcListDelete(pTabList); |
| 635 | if( pList ) sqlite3ExprListDelete(pList); |
| 636 | if( pSelect ) sqlite3SelectDelete(pSelect); |
| 637 | sqlite3IdListDelete(pColumn); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 638 | } |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 639 | |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 640 | /* |
| 641 | ** Generate code to do a constraint check prior to an INSERT or an UPDATE. |
| 642 | ** |
| 643 | ** When this routine is called, the stack contains (from bottom to top) |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 644 | ** the following values: |
| 645 | ** |
drh | b2fe7d8 | 2003-04-20 17:29:23 +0000 | [diff] [blame] | 646 | ** 1. The recno of the row to be updated before the update. This |
drh | b419a92 | 2002-01-30 16:17:23 +0000 | [diff] [blame] | 647 | ** value is omitted unless we are doing an UPDATE that involves a |
| 648 | ** change to the record number. |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 649 | ** |
drh | b419a92 | 2002-01-30 16:17:23 +0000 | [diff] [blame] | 650 | ** 2. The recno of the row after the update. |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 651 | ** |
| 652 | ** 3. The data in the first column of the entry after the update. |
| 653 | ** |
| 654 | ** i. Data from middle columns... |
| 655 | ** |
| 656 | ** N. The data in the last column of the entry after the update. |
| 657 | ** |
drh | b419a92 | 2002-01-30 16:17:23 +0000 | [diff] [blame] | 658 | ** The old recno shown as entry (1) above is omitted unless both isUpdate |
drh | 1c92853 | 2002-01-31 15:54:21 +0000 | [diff] [blame] | 659 | ** and recnoChng are 1. isUpdate is true for UPDATEs and false for |
| 660 | ** INSERTs and recnoChng is true if the record number is being changed. |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 661 | ** |
| 662 | ** The code generated by this routine pushes additional entries onto |
| 663 | ** the stack which are the keys for new index entries for the new record. |
| 664 | ** The order of index keys is the same as the order of the indices on |
| 665 | ** the pTable->pIndex list. A key is only created for index i if |
| 666 | ** aIdxUsed!=0 and aIdxUsed[i]!=0. |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 667 | ** |
| 668 | ** This routine also generates code to check constraints. NOT NULL, |
| 669 | ** CHECK, and UNIQUE constraints are all checked. If a constraint fails, |
drh | 1c92853 | 2002-01-31 15:54:21 +0000 | [diff] [blame] | 670 | ** then the appropriate action is performed. There are five possible |
| 671 | ** actions: ROLLBACK, ABORT, FAIL, REPLACE, and IGNORE. |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 672 | ** |
| 673 | ** Constraint type Action What Happens |
| 674 | ** --------------- ---------- ---------------------------------------- |
drh | 1c92853 | 2002-01-31 15:54:21 +0000 | [diff] [blame] | 675 | ** any ROLLBACK The current transaction is rolled back and |
danielk1977 | 24b03fd | 2004-05-10 10:34:34 +0000 | [diff] [blame] | 676 | ** sqlite3_exec() returns immediately with a |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 677 | ** return code of SQLITE_CONSTRAINT. |
| 678 | ** |
drh | 1c92853 | 2002-01-31 15:54:21 +0000 | [diff] [blame] | 679 | ** any ABORT Back out changes from the current command |
| 680 | ** only (do not do a complete rollback) then |
danielk1977 | 24b03fd | 2004-05-10 10:34:34 +0000 | [diff] [blame] | 681 | ** cause sqlite3_exec() to return immediately |
drh | 1c92853 | 2002-01-31 15:54:21 +0000 | [diff] [blame] | 682 | ** with SQLITE_CONSTRAINT. |
| 683 | ** |
| 684 | ** any FAIL Sqlite_exec() returns immediately with a |
| 685 | ** return code of SQLITE_CONSTRAINT. The |
| 686 | ** transaction is not rolled back and any |
| 687 | ** prior changes are retained. |
| 688 | ** |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 689 | ** any IGNORE The record number and data is popped from |
| 690 | ** the stack and there is an immediate jump |
| 691 | ** to label ignoreDest. |
| 692 | ** |
| 693 | ** NOT NULL REPLACE The NULL value is replace by the default |
| 694 | ** value for that column. If the default value |
| 695 | ** is NULL, the action is the same as ABORT. |
| 696 | ** |
| 697 | ** UNIQUE REPLACE The other row that conflicts with the row |
| 698 | ** being inserted is removed. |
| 699 | ** |
| 700 | ** CHECK REPLACE Illegal. The results in an exception. |
| 701 | ** |
drh | 1c92853 | 2002-01-31 15:54:21 +0000 | [diff] [blame] | 702 | ** Which action to take is determined by the overrideError parameter. |
| 703 | ** Or if overrideError==OE_Default, then the pParse->onError parameter |
| 704 | ** is used. Or if pParse->onError==OE_Default then the onError value |
| 705 | ** for the constraint is used. |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 706 | ** |
drh | aaab572 | 2002-02-19 13:39:21 +0000 | [diff] [blame] | 707 | ** The calling routine must open a read/write cursor for pTab with |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 708 | ** cursor number "base". All indices of pTab must also have open |
| 709 | ** read/write cursors with cursor number base+i for the i-th cursor. |
| 710 | ** Except, if there is no possibility of a REPLACE action then |
| 711 | ** cursors do not need to be open for indices where aIdxUsed[i]==0. |
| 712 | ** |
| 713 | ** If the isUpdate flag is true, it means that the "base" cursor is |
| 714 | ** initially pointing to an entry that is being updated. The isUpdate |
| 715 | ** flag causes extra code to be generated so that the "base" cursor |
| 716 | ** is still pointing at the same entry after the routine returns. |
| 717 | ** Without the isUpdate flag, the "base" cursor might be moved. |
| 718 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 719 | void sqlite3GenerateConstraintChecks( |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 720 | Parse *pParse, /* The parser context */ |
| 721 | Table *pTab, /* the table into which we are inserting */ |
| 722 | int base, /* Index of a read/write cursor pointing at pTab */ |
| 723 | char *aIdxUsed, /* Which indices are used. NULL means all are used */ |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 724 | int recnoChng, /* True if the record number will change */ |
drh | b419a92 | 2002-01-30 16:17:23 +0000 | [diff] [blame] | 725 | int isUpdate, /* True for UPDATE, False for INSERT */ |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 726 | int overrideError, /* Override onError to this if not OE_Default */ |
drh | b419a92 | 2002-01-30 16:17:23 +0000 | [diff] [blame] | 727 | int ignoreDest /* Jump to this label on an OE_Ignore resolution */ |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 728 | ){ |
| 729 | int i; |
| 730 | Vdbe *v; |
| 731 | int nCol; |
| 732 | int onError; |
| 733 | int addr; |
| 734 | int extra; |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 735 | int iCur; |
| 736 | Index *pIdx; |
| 737 | int seenReplace = 0; |
danielk1977 | cfe9a69 | 2004-06-16 12:00:29 +0000 | [diff] [blame] | 738 | int jumpInst1=0, jumpInst2; |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 739 | int contAddr; |
drh | b419a92 | 2002-01-30 16:17:23 +0000 | [diff] [blame] | 740 | int hasTwoRecnos = (isUpdate && recnoChng); |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 741 | |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 742 | v = sqlite3GetVdbe(pParse); |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 743 | assert( v!=0 ); |
drh | 417be79 | 2002-03-03 18:59:40 +0000 | [diff] [blame] | 744 | assert( pTab->pSelect==0 ); /* This table is not a VIEW */ |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 745 | nCol = pTab->nCol; |
| 746 | |
| 747 | /* Test all NOT NULL constraints. |
| 748 | */ |
| 749 | for(i=0; i<nCol; i++){ |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 750 | if( i==pTab->iPKey ){ |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 751 | continue; |
| 752 | } |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 753 | onError = pTab->aCol[i].notNull; |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 754 | if( onError==OE_None ) continue; |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 755 | if( overrideError!=OE_Default ){ |
| 756 | onError = overrideError; |
drh | a996e47 | 2003-05-16 02:30:27 +0000 | [diff] [blame] | 757 | }else if( onError==OE_Default ){ |
| 758 | onError = OE_Abort; |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 759 | } |
| 760 | if( onError==OE_Replace && pTab->aCol[i].zDflt==0 ){ |
| 761 | onError = OE_Abort; |
| 762 | } |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 763 | sqlite3VdbeAddOp(v, OP_Dup, nCol-1-i, 1); |
| 764 | addr = sqlite3VdbeAddOp(v, OP_NotNull, 1, 0); |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 765 | switch( onError ){ |
drh | 1c92853 | 2002-01-31 15:54:21 +0000 | [diff] [blame] | 766 | case OE_Rollback: |
| 767 | case OE_Abort: |
| 768 | case OE_Fail: { |
drh | 483750b | 2003-01-29 18:46:51 +0000 | [diff] [blame] | 769 | char *zMsg = 0; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 770 | sqlite3VdbeAddOp(v, OP_Halt, SQLITE_CONSTRAINT, onError); |
| 771 | sqlite3SetString(&zMsg, pTab->zName, ".", pTab->aCol[i].zName, |
drh | 4174398 | 2003-12-06 21:43:55 +0000 | [diff] [blame] | 772 | " may not be NULL", (char*)0); |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 773 | sqlite3VdbeChangeP3(v, -1, zMsg, P3_DYNAMIC); |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 774 | break; |
| 775 | } |
| 776 | case OE_Ignore: { |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 777 | sqlite3VdbeAddOp(v, OP_Pop, nCol+1+hasTwoRecnos, 0); |
| 778 | sqlite3VdbeAddOp(v, OP_Goto, 0, ignoreDest); |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 779 | break; |
| 780 | } |
| 781 | case OE_Replace: { |
danielk1977 | 0f69c1e | 2004-05-29 11:24:50 +0000 | [diff] [blame] | 782 | sqlite3VdbeOp3(v, OP_String8, 0, 0, pTab->aCol[i].zDflt, P3_STATIC); |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 783 | sqlite3VdbeAddOp(v, OP_Push, nCol-i, 0); |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 784 | break; |
| 785 | } |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 786 | default: assert(0); |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 787 | } |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 788 | sqlite3VdbeChangeP2(v, addr, sqlite3VdbeCurrentAddr(v)); |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 789 | } |
| 790 | |
| 791 | /* Test all CHECK constraints |
| 792 | */ |
drh | 0bd1f4e | 2002-06-06 18:54:39 +0000 | [diff] [blame] | 793 | /**** TBD ****/ |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 794 | |
drh | 0bd1f4e | 2002-06-06 18:54:39 +0000 | [diff] [blame] | 795 | /* If we have an INTEGER PRIMARY KEY, make sure the primary key |
| 796 | ** of the new record does not previously exist. Except, if this |
| 797 | ** is an UPDATE and the primary key is not changing, that is OK. |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 798 | */ |
drh | 5383ae5 | 2003-06-04 12:23:30 +0000 | [diff] [blame] | 799 | if( recnoChng ){ |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 800 | onError = pTab->keyConf; |
| 801 | if( overrideError!=OE_Default ){ |
| 802 | onError = overrideError; |
drh | a996e47 | 2003-05-16 02:30:27 +0000 | [diff] [blame] | 803 | }else if( onError==OE_Default ){ |
| 804 | onError = OE_Abort; |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 805 | } |
drh | a0217ba | 2003-06-01 01:10:33 +0000 | [diff] [blame] | 806 | |
drh | 5383ae5 | 2003-06-04 12:23:30 +0000 | [diff] [blame] | 807 | if( isUpdate ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 808 | sqlite3VdbeAddOp(v, OP_Dup, nCol+1, 1); |
| 809 | sqlite3VdbeAddOp(v, OP_Dup, nCol+1, 1); |
| 810 | jumpInst1 = sqlite3VdbeAddOp(v, OP_Eq, 0, 0); |
drh | 5383ae5 | 2003-06-04 12:23:30 +0000 | [diff] [blame] | 811 | } |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 812 | sqlite3VdbeAddOp(v, OP_Dup, nCol, 1); |
| 813 | jumpInst2 = sqlite3VdbeAddOp(v, OP_NotExists, base, 0); |
drh | 5383ae5 | 2003-06-04 12:23:30 +0000 | [diff] [blame] | 814 | switch( onError ){ |
| 815 | default: { |
| 816 | onError = OE_Abort; |
| 817 | /* Fall thru into the next case */ |
drh | 79b0c95 | 2002-05-21 12:56:43 +0000 | [diff] [blame] | 818 | } |
drh | 5383ae5 | 2003-06-04 12:23:30 +0000 | [diff] [blame] | 819 | case OE_Rollback: |
| 820 | case OE_Abort: |
| 821 | case OE_Fail: { |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 822 | sqlite3VdbeOp3(v, OP_Halt, SQLITE_CONSTRAINT, onError, |
drh | 701a0ae | 2004-02-22 20:05:00 +0000 | [diff] [blame] | 823 | "PRIMARY KEY must be unique", P3_STATIC); |
drh | 5383ae5 | 2003-06-04 12:23:30 +0000 | [diff] [blame] | 824 | break; |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 825 | } |
drh | 5383ae5 | 2003-06-04 12:23:30 +0000 | [diff] [blame] | 826 | case OE_Replace: { |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 827 | sqlite3GenerateRowIndexDelete(pParse->db, v, pTab, base, 0); |
drh | 5383ae5 | 2003-06-04 12:23:30 +0000 | [diff] [blame] | 828 | if( isUpdate ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 829 | sqlite3VdbeAddOp(v, OP_Dup, nCol+hasTwoRecnos, 1); |
drh | 7cf6e4d | 2004-05-19 14:56:55 +0000 | [diff] [blame] | 830 | sqlite3VdbeAddOp(v, OP_MoveGe, base, 0); |
drh | 5383ae5 | 2003-06-04 12:23:30 +0000 | [diff] [blame] | 831 | } |
| 832 | seenReplace = 1; |
| 833 | break; |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 834 | } |
drh | 5383ae5 | 2003-06-04 12:23:30 +0000 | [diff] [blame] | 835 | case OE_Ignore: { |
| 836 | assert( seenReplace==0 ); |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 837 | sqlite3VdbeAddOp(v, OP_Pop, nCol+1+hasTwoRecnos, 0); |
| 838 | sqlite3VdbeAddOp(v, OP_Goto, 0, ignoreDest); |
drh | 5383ae5 | 2003-06-04 12:23:30 +0000 | [diff] [blame] | 839 | break; |
| 840 | } |
| 841 | } |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 842 | contAddr = sqlite3VdbeCurrentAddr(v); |
| 843 | sqlite3VdbeChangeP2(v, jumpInst2, contAddr); |
drh | 5383ae5 | 2003-06-04 12:23:30 +0000 | [diff] [blame] | 844 | if( isUpdate ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 845 | sqlite3VdbeChangeP2(v, jumpInst1, contAddr); |
| 846 | sqlite3VdbeAddOp(v, OP_Dup, nCol+1, 1); |
drh | 7cf6e4d | 2004-05-19 14:56:55 +0000 | [diff] [blame] | 847 | sqlite3VdbeAddOp(v, OP_MoveGe, base, 0); |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 848 | } |
| 849 | } |
drh | 0bd1f4e | 2002-06-06 18:54:39 +0000 | [diff] [blame] | 850 | |
| 851 | /* Test all UNIQUE constraints by creating entries for each UNIQUE |
| 852 | ** index and making sure that duplicate entries do not already exist. |
| 853 | ** Add the new records to the indices as we go. |
| 854 | */ |
drh | b2fe7d8 | 2003-04-20 17:29:23 +0000 | [diff] [blame] | 855 | extra = -1; |
| 856 | for(iCur=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, iCur++){ |
| 857 | if( aIdxUsed && aIdxUsed[iCur]==0 ) continue; /* Skip unused indices */ |
| 858 | extra++; |
| 859 | |
| 860 | /* Create a key for accessing the index entry */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 861 | sqlite3VdbeAddOp(v, OP_Dup, nCol+extra, 1); |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 862 | for(i=0; i<pIdx->nColumn; i++){ |
| 863 | int idx = pIdx->aiColumn[i]; |
| 864 | if( idx==pTab->iPKey ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 865 | sqlite3VdbeAddOp(v, OP_Dup, i+extra+nCol+1, 1); |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 866 | }else{ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 867 | sqlite3VdbeAddOp(v, OP_Dup, i+extra+nCol-idx, 1); |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 868 | } |
| 869 | } |
danielk1977 | ededfd5 | 2004-06-17 07:53:01 +0000 | [diff] [blame] | 870 | jumpInst1 = sqlite3VdbeAddOp(v, OP_MakeRecord, pIdx->nColumn, (1<<24)); |
danielk1977 | a37cdde | 2004-05-16 11:15:36 +0000 | [diff] [blame] | 871 | sqlite3IndexAffinityStr(v, pIdx); |
drh | b2fe7d8 | 2003-04-20 17:29:23 +0000 | [diff] [blame] | 872 | |
| 873 | /* Find out what action to take in case there is an indexing conflict */ |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 874 | onError = pIdx->onError; |
drh | b2fe7d8 | 2003-04-20 17:29:23 +0000 | [diff] [blame] | 875 | if( onError==OE_None ) continue; /* pIdx is not a UNIQUE index */ |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 876 | if( overrideError!=OE_Default ){ |
| 877 | onError = overrideError; |
drh | a996e47 | 2003-05-16 02:30:27 +0000 | [diff] [blame] | 878 | }else if( onError==OE_Default ){ |
| 879 | onError = OE_Abort; |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 880 | } |
drh | 5383ae5 | 2003-06-04 12:23:30 +0000 | [diff] [blame] | 881 | if( seenReplace ){ |
| 882 | if( onError==OE_Ignore ) onError = OE_Replace; |
| 883 | else if( onError==OE_Fail ) onError = OE_Abort; |
| 884 | } |
| 885 | |
drh | b2fe7d8 | 2003-04-20 17:29:23 +0000 | [diff] [blame] | 886 | |
| 887 | /* Check to see if the new index entry will be unique */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 888 | sqlite3VdbeAddOp(v, OP_Dup, extra+nCol+1+hasTwoRecnos, 1); |
| 889 | jumpInst2 = sqlite3VdbeAddOp(v, OP_IsUnique, base+iCur+1, 0); |
drh | b2fe7d8 | 2003-04-20 17:29:23 +0000 | [diff] [blame] | 890 | |
| 891 | /* Generate code that executes if the new index entry is not unique */ |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 892 | switch( onError ){ |
drh | 1c92853 | 2002-01-31 15:54:21 +0000 | [diff] [blame] | 893 | case OE_Rollback: |
| 894 | case OE_Abort: |
| 895 | case OE_Fail: { |
drh | 37ed48e | 2003-08-05 13:13:38 +0000 | [diff] [blame] | 896 | int j, n1, n2; |
| 897 | char zErrMsg[200]; |
| 898 | strcpy(zErrMsg, pIdx->nColumn>1 ? "columns " : "column "); |
| 899 | n1 = strlen(zErrMsg); |
| 900 | for(j=0; j<pIdx->nColumn && n1<sizeof(zErrMsg)-30; j++){ |
| 901 | char *zCol = pTab->aCol[pIdx->aiColumn[j]].zName; |
| 902 | n2 = strlen(zCol); |
| 903 | if( j>0 ){ |
| 904 | strcpy(&zErrMsg[n1], ", "); |
| 905 | n1 += 2; |
| 906 | } |
| 907 | if( n1+n2>sizeof(zErrMsg)-30 ){ |
| 908 | strcpy(&zErrMsg[n1], "..."); |
| 909 | n1 += 3; |
| 910 | break; |
| 911 | }else{ |
| 912 | strcpy(&zErrMsg[n1], zCol); |
| 913 | n1 += n2; |
| 914 | } |
| 915 | } |
| 916 | strcpy(&zErrMsg[n1], |
| 917 | pIdx->nColumn>1 ? " are not unique" : " is not unique"); |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 918 | sqlite3VdbeOp3(v, OP_Halt, SQLITE_CONSTRAINT, onError, zErrMsg, 0); |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 919 | break; |
| 920 | } |
| 921 | case OE_Ignore: { |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 922 | assert( seenReplace==0 ); |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 923 | sqlite3VdbeAddOp(v, OP_Pop, nCol+extra+3+hasTwoRecnos, 0); |
| 924 | sqlite3VdbeAddOp(v, OP_Goto, 0, ignoreDest); |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 925 | break; |
| 926 | } |
| 927 | case OE_Replace: { |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 928 | sqlite3GenerateRowDelete(pParse->db, v, pTab, base, 0); |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 929 | if( isUpdate ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 930 | sqlite3VdbeAddOp(v, OP_Dup, nCol+extra+1+hasTwoRecnos, 1); |
drh | 7cf6e4d | 2004-05-19 14:56:55 +0000 | [diff] [blame] | 931 | sqlite3VdbeAddOp(v, OP_MoveGe, base, 0); |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 932 | } |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 933 | seenReplace = 1; |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 934 | break; |
| 935 | } |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 936 | default: assert(0); |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 937 | } |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 938 | contAddr = sqlite3VdbeCurrentAddr(v); |
danielk1977 | ededfd5 | 2004-06-17 07:53:01 +0000 | [diff] [blame] | 939 | assert( contAddr<(1<<24) ); |
drh | 0bd1f4e | 2002-06-06 18:54:39 +0000 | [diff] [blame] | 940 | #if NULL_DISTINCT_FOR_UNIQUE |
danielk1977 | ededfd5 | 2004-06-17 07:53:01 +0000 | [diff] [blame] | 941 | sqlite3VdbeChangeP2(v, jumpInst1, contAddr | (1<<24)); |
drh | 0bd1f4e | 2002-06-06 18:54:39 +0000 | [diff] [blame] | 942 | #endif |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 943 | sqlite3VdbeChangeP2(v, jumpInst2, contAddr); |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 944 | } |
| 945 | } |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 946 | |
| 947 | /* |
| 948 | ** This routine generates code to finish the INSERT or UPDATE operation |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 949 | ** that was started by a prior call to sqlite3GenerateConstraintChecks. |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 950 | ** The stack must contain keys for all active indices followed by data |
| 951 | ** and the recno for the new entry. This routine creates the new |
| 952 | ** entries in all indices and in the main table. |
| 953 | ** |
drh | b419a92 | 2002-01-30 16:17:23 +0000 | [diff] [blame] | 954 | ** The arguments to this routine should be the same as the first six |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 955 | ** arguments to sqlite3GenerateConstraintChecks. |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 956 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 957 | void sqlite3CompleteInsertion( |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 958 | Parse *pParse, /* The parser context */ |
| 959 | Table *pTab, /* the table into which we are inserting */ |
| 960 | int base, /* Index of a read/write cursor pointing at pTab */ |
| 961 | char *aIdxUsed, /* Which indices are used. NULL means all are used */ |
drh | b419a92 | 2002-01-30 16:17:23 +0000 | [diff] [blame] | 962 | int recnoChng, /* True if the record number will change */ |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 963 | int isUpdate, /* True for UPDATE, False for INSERT */ |
| 964 | int newIdx /* Index of NEW table for triggers. -1 if none */ |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 965 | ){ |
| 966 | int i; |
| 967 | Vdbe *v; |
| 968 | int nIdx; |
| 969 | Index *pIdx; |
danielk1977 | b28af71 | 2004-06-21 06:50:26 +0000 | [diff] [blame] | 970 | int pik_flags; |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 971 | |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 972 | v = sqlite3GetVdbe(pParse); |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 973 | assert( v!=0 ); |
drh | 417be79 | 2002-03-03 18:59:40 +0000 | [diff] [blame] | 974 | assert( pTab->pSelect==0 ); /* This table is not a VIEW */ |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 975 | for(nIdx=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, nIdx++){} |
| 976 | for(i=nIdx-1; i>=0; i--){ |
| 977 | if( aIdxUsed && aIdxUsed[i]==0 ) continue; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 978 | sqlite3VdbeAddOp(v, OP_IdxPut, base+i+1, 0); |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 979 | } |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 980 | sqlite3VdbeAddOp(v, OP_MakeRecord, pTab->nCol, 0); |
danielk1977 | a37cdde | 2004-05-16 11:15:36 +0000 | [diff] [blame] | 981 | sqlite3TableAffinityStr(v, pTab); |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 982 | if( newIdx>=0 ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 983 | sqlite3VdbeAddOp(v, OP_Dup, 1, 0); |
| 984 | sqlite3VdbeAddOp(v, OP_Dup, 1, 0); |
| 985 | sqlite3VdbeAddOp(v, OP_PutIntKey, newIdx, 0); |
drh | 70ce3f0 | 2003-04-15 19:22:22 +0000 | [diff] [blame] | 986 | } |
danielk1977 | b28af71 | 2004-06-21 06:50:26 +0000 | [diff] [blame] | 987 | pik_flags = (OPFLAG_NCHANGE|(isUpdate?0:OPFLAG_LASTROWID)); |
| 988 | sqlite3VdbeAddOp(v, OP_PutIntKey, base, pik_flags); |
| 989 | |
drh | b419a92 | 2002-01-30 16:17:23 +0000 | [diff] [blame] | 990 | if( isUpdate && recnoChng ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 991 | sqlite3VdbeAddOp(v, OP_Pop, 1, 0); |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 992 | } |
| 993 | } |
drh | cd44690 | 2004-02-24 01:05:31 +0000 | [diff] [blame] | 994 | |
| 995 | /* |
| 996 | ** Generate code that will open write cursors for a table and for all |
| 997 | ** indices of that table. The "base" parameter is the cursor number used |
| 998 | ** for the table. Indices are opened on subsequent cursors. |
| 999 | ** |
| 1000 | ** Return the total number of cursors opened. This is always at least |
| 1001 | ** 1 (for the main table) plus more for each cursor. |
| 1002 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1003 | int sqlite3OpenTableAndIndices(Parse *pParse, Table *pTab, int base){ |
drh | cd44690 | 2004-02-24 01:05:31 +0000 | [diff] [blame] | 1004 | int i; |
| 1005 | Index *pIdx; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1006 | Vdbe *v = sqlite3GetVdbe(pParse); |
drh | cd44690 | 2004-02-24 01:05:31 +0000 | [diff] [blame] | 1007 | assert( v!=0 ); |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1008 | sqlite3VdbeAddOp(v, OP_Integer, pTab->iDb, 0); |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 1009 | sqlite3VdbeAddOp(v, OP_OpenWrite, base, pTab->tnum); |
danielk1977 | b4964b7 | 2004-05-18 01:23:38 +0000 | [diff] [blame] | 1010 | sqlite3VdbeAddOp(v, OP_SetNumColumns, base, pTab->nCol); |
drh | cd44690 | 2004-02-24 01:05:31 +0000 | [diff] [blame] | 1011 | for(i=1, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1012 | sqlite3VdbeAddOp(v, OP_Integer, pIdx->iDb, 0); |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 1013 | sqlite3VdbeOp3(v, OP_OpenWrite, i+base, pIdx->tnum, |
| 1014 | (char*)&pIdx->keyInfo, P3_KEYINFO); |
drh | cd44690 | 2004-02-24 01:05:31 +0000 | [diff] [blame] | 1015 | } |
| 1016 | return i; |
| 1017 | } |