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