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