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 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 15 | ** $Id: insert.c,v 1.53 2002/05/15 08:30:13 danielk1977 Exp $ |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 16 | */ |
| 17 | #include "sqliteInt.h" |
| 18 | |
| 19 | /* |
drh | 1ccde15 | 2000-06-17 13:12:39 +0000 | [diff] [blame] | 20 | ** This routine is call to handle SQL of the following forms: |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 21 | ** |
| 22 | ** insert into TABLE (IDLIST) values(EXPRLIST) |
drh | 1ccde15 | 2000-06-17 13:12:39 +0000 | [diff] [blame] | 23 | ** insert into TABLE (IDLIST) select |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 24 | ** |
drh | 1ccde15 | 2000-06-17 13:12:39 +0000 | [diff] [blame] | 25 | ** The IDLIST following the table name is always optional. If omitted, |
| 26 | ** then a list of all columns for the table is substituted. The IDLIST |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 27 | ** appears in the pColumn parameter. pColumn is NULL if IDLIST is omitted. |
drh | 1ccde15 | 2000-06-17 13:12:39 +0000 | [diff] [blame] | 28 | ** |
| 29 | ** The pList parameter holds EXPRLIST in the first form of the INSERT |
| 30 | ** statement above, and pSelect is NULL. For the second form, pList is |
| 31 | ** NULL and pSelect is a pointer to the select statement used to generate |
| 32 | ** data for the insert. |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 33 | */ |
| 34 | void sqliteInsert( |
| 35 | Parse *pParse, /* Parser context */ |
| 36 | Token *pTableName, /* Name of table into which we are inserting */ |
| 37 | ExprList *pList, /* List of values to be inserted */ |
drh | 5974a30 | 2000-06-07 14:42:26 +0000 | [diff] [blame] | 38 | Select *pSelect, /* A SELECT statement to use as the data source */ |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 39 | IdList *pColumn, /* Column names corresponding to IDLIST. */ |
| 40 | int onError /* How to handle constraint errors */ |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 41 | ){ |
drh | 5974a30 | 2000-06-07 14:42:26 +0000 | [diff] [blame] | 42 | Table *pTab; /* The table to insert into */ |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 43 | char *zTab = 0; /* Name of the table into which we are inserting */ |
drh | 5974a30 | 2000-06-07 14:42:26 +0000 | [diff] [blame] | 44 | int i, j, idx; /* Loop counters */ |
| 45 | Vdbe *v; /* Generate code into this virtual machine */ |
| 46 | Index *pIdx; /* For looping over indices of the table */ |
| 47 | int srcTab; /* Date comes from this temporary cursor if >=0 */ |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 48 | int nColumn; /* Number of columns in the data */ |
drh | 5974a30 | 2000-06-07 14:42:26 +0000 | [diff] [blame] | 49 | int base; /* First available cursor */ |
| 50 | int iCont, iBreak; /* Beginning and end of the loop over srcTab */ |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 51 | sqlite *db; /* The main database structure */ |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 52 | int openOp; /* Opcode used to open cursors */ |
drh | 4a32431 | 2001-12-21 14:30:42 +0000 | [diff] [blame] | 53 | int keyColumn = -1; /* Column that is the INTEGER PRIMARY KEY */ |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 54 | int endOfLoop; /* Label for the end of the insertion loop */ |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 55 | |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 56 | int row_triggers_exist = 0; /* True if there are FOR EACH ROW triggers */ |
| 57 | int newIdx = -1; |
| 58 | |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 59 | if( pParse->nErr || sqlite_malloc_failed ) goto insert_cleanup; |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 60 | db = pParse->db; |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 61 | |
drh | 1ccde15 | 2000-06-17 13:12:39 +0000 | [diff] [blame] | 62 | /* Locate the table into which we will be inserting new information. |
| 63 | */ |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 64 | zTab = sqliteTableNameFromToken(pTableName); |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 65 | if( zTab==0 ) goto insert_cleanup; |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 66 | pTab = sqliteFindTable(pParse->db, zTab); |
| 67 | if( pTab==0 ){ |
| 68 | sqliteSetString(&pParse->zErrMsg, "no such table: ", zTab, 0); |
| 69 | pParse->nErr++; |
| 70 | goto insert_cleanup; |
| 71 | } |
| 72 | |
| 73 | /* Ensure that: |
| 74 | * (a) the table is not read-only, |
| 75 | * (b) that if it is a view then ON INSERT triggers exist |
| 76 | */ |
| 77 | row_triggers_exist = |
| 78 | sqliteTriggersExist(pParse, pTab->pTrigger, TK_INSERT, |
| 79 | TK_BEFORE, TK_ROW, 0) || |
| 80 | sqliteTriggersExist(pParse, pTab->pTrigger, TK_INSERT, TK_AFTER, TK_ROW, 0); |
| 81 | if( pTab->readOnly || (pTab->pSelect && !row_triggers_exist) ){ |
| 82 | sqliteSetString(&pParse->zErrMsg, |
| 83 | pTab->pSelect ? "view " : "table ", |
| 84 | zTab, |
| 85 | " may not be modified", 0); |
| 86 | pParse->nErr++; |
| 87 | goto insert_cleanup; |
| 88 | } |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 89 | sqliteFree(zTab); |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 90 | zTab = 0; |
| 91 | |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 92 | if( pTab==0 ) goto insert_cleanup; |
drh | 1ccde15 | 2000-06-17 13:12:39 +0000 | [diff] [blame] | 93 | |
| 94 | /* Allocate a VDBE |
| 95 | */ |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 96 | v = sqliteGetVdbe(pParse); |
drh | 5974a30 | 2000-06-07 14:42:26 +0000 | [diff] [blame] | 97 | if( v==0 ) goto insert_cleanup; |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 98 | if( pSelect || row_triggers_exist ){ |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 99 | sqliteBeginMultiWriteOperation(pParse); |
| 100 | }else{ |
| 101 | sqliteBeginWriteOperation(pParse); |
| 102 | } |
drh | 1ccde15 | 2000-06-17 13:12:39 +0000 | [diff] [blame] | 103 | |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 104 | /* if there are row triggers, allocate a temp table for new.* references. */ |
| 105 | if (row_triggers_exist) |
| 106 | newIdx = pParse->nTab++; |
| 107 | |
drh | 1ccde15 | 2000-06-17 13:12:39 +0000 | [diff] [blame] | 108 | /* Figure out how many columns of data are supplied. If the data |
drh | c6b52df | 2002-01-04 03:09:29 +0000 | [diff] [blame] | 109 | ** is coming from a SELECT statement, then this step has to generate |
drh | 1ccde15 | 2000-06-17 13:12:39 +0000 | [diff] [blame] | 110 | ** all the code to implement the SELECT statement and leave the data |
| 111 | ** in a temporary table. If data is coming from an expression list, |
| 112 | ** then we just have to count the number of expressions. |
| 113 | */ |
drh | 5974a30 | 2000-06-07 14:42:26 +0000 | [diff] [blame] | 114 | if( pSelect ){ |
| 115 | int rc; |
| 116 | srcTab = pParse->nTab++; |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 117 | sqliteVdbeAddOp(v, OP_OpenTemp, srcTab, 0); |
drh | 832508b | 2002-03-02 17:04:07 +0000 | [diff] [blame] | 118 | rc = sqliteSelect(pParse, pSelect, SRT_Table, srcTab, 0,0,0); |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 119 | if( rc || pParse->nErr || sqlite_malloc_failed ) goto insert_cleanup; |
drh | 5974a30 | 2000-06-07 14:42:26 +0000 | [diff] [blame] | 120 | assert( pSelect->pEList ); |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 121 | nColumn = pSelect->pEList->nExpr; |
drh | 5974a30 | 2000-06-07 14:42:26 +0000 | [diff] [blame] | 122 | }else{ |
drh | e64e7b2 | 2002-02-18 13:56:36 +0000 | [diff] [blame] | 123 | IdList dummy; |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 124 | assert( pList!=0 ); |
drh | 5974a30 | 2000-06-07 14:42:26 +0000 | [diff] [blame] | 125 | srcTab = -1; |
| 126 | assert( pList ); |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 127 | nColumn = pList->nExpr; |
drh | e64e7b2 | 2002-02-18 13:56:36 +0000 | [diff] [blame] | 128 | dummy.nId = 0; |
| 129 | for(i=0; i<nColumn; i++){ |
drh | 832508b | 2002-03-02 17:04:07 +0000 | [diff] [blame] | 130 | if( sqliteExprResolveIds(pParse, 0, &dummy, 0, pList->a[i].pExpr) ){ |
drh | e64e7b2 | 2002-02-18 13:56:36 +0000 | [diff] [blame] | 131 | goto insert_cleanup; |
| 132 | } |
drh | b04a5d8 | 2002-04-12 03:55:15 +0000 | [diff] [blame] | 133 | if( sqliteExprCheck(pParse, pList->a[i].pExpr, 0, 0) ){ |
| 134 | goto insert_cleanup; |
| 135 | } |
drh | e64e7b2 | 2002-02-18 13:56:36 +0000 | [diff] [blame] | 136 | } |
drh | 5974a30 | 2000-06-07 14:42:26 +0000 | [diff] [blame] | 137 | } |
drh | 1ccde15 | 2000-06-17 13:12:39 +0000 | [diff] [blame] | 138 | |
| 139 | /* Make sure the number of columns in the source data matches the number |
| 140 | ** of columns to be inserted into the table. |
| 141 | */ |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 142 | if( pColumn==0 && nColumn!=pTab->nCol ){ |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 143 | char zNum1[30]; |
| 144 | char zNum2[30]; |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 145 | sprintf(zNum1,"%d", nColumn); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 146 | sprintf(zNum2,"%d", pTab->nCol); |
| 147 | sqliteSetString(&pParse->zErrMsg, "table ", pTab->zName, |
| 148 | " has ", zNum2, " columns but ", |
| 149 | zNum1, " values were supplied", 0); |
| 150 | pParse->nErr++; |
| 151 | goto insert_cleanup; |
| 152 | } |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 153 | if( pColumn!=0 && nColumn!=pColumn->nId ){ |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 154 | char zNum1[30]; |
| 155 | char zNum2[30]; |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 156 | sprintf(zNum1,"%d", nColumn); |
| 157 | sprintf(zNum2,"%d", pColumn->nId); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 158 | sqliteSetString(&pParse->zErrMsg, zNum1, " values for ", |
| 159 | zNum2, " columns", 0); |
| 160 | pParse->nErr++; |
| 161 | goto insert_cleanup; |
| 162 | } |
drh | 1ccde15 | 2000-06-17 13:12:39 +0000 | [diff] [blame] | 163 | |
| 164 | /* If the INSERT statement included an IDLIST term, then make sure |
| 165 | ** all elements of the IDLIST really are columns of the table and |
| 166 | ** remember the column indices. |
drh | c839258 | 2001-12-31 02:48:51 +0000 | [diff] [blame] | 167 | ** |
| 168 | ** If the table has an INTEGER PRIMARY KEY column and that column |
| 169 | ** is named in the IDLIST, then record in the keyColumn variable |
| 170 | ** the index into IDLIST of the primary key column. keyColumn is |
| 171 | ** the index of the primary key as it appears in IDLIST, not as |
| 172 | ** is appears in the original table. (The index of the primary |
| 173 | ** key in the original table is pTab->iPKey.) |
drh | 1ccde15 | 2000-06-17 13:12:39 +0000 | [diff] [blame] | 174 | */ |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 175 | if( pColumn ){ |
| 176 | for(i=0; i<pColumn->nId; i++){ |
| 177 | pColumn->a[i].idx = -1; |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 178 | } |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 179 | for(i=0; i<pColumn->nId; i++){ |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 180 | for(j=0; j<pTab->nCol; j++){ |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 181 | if( sqliteStrICmp(pColumn->a[i].zName, pTab->aCol[j].zName)==0 ){ |
| 182 | pColumn->a[i].idx = j; |
drh | 4a32431 | 2001-12-21 14:30:42 +0000 | [diff] [blame] | 183 | if( j==pTab->iPKey ){ |
drh | 9aa028d | 2001-12-22 21:48:29 +0000 | [diff] [blame] | 184 | keyColumn = i; |
drh | 4a32431 | 2001-12-21 14:30:42 +0000 | [diff] [blame] | 185 | } |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 186 | break; |
| 187 | } |
| 188 | } |
| 189 | if( j>=pTab->nCol ){ |
| 190 | sqliteSetString(&pParse->zErrMsg, "table ", pTab->zName, |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 191 | " has no column named ", pColumn->a[i].zName, 0); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 192 | pParse->nErr++; |
| 193 | goto insert_cleanup; |
| 194 | } |
| 195 | } |
| 196 | } |
drh | 1ccde15 | 2000-06-17 13:12:39 +0000 | [diff] [blame] | 197 | |
drh | aacc543 | 2002-01-06 17:07:40 +0000 | [diff] [blame] | 198 | /* If there is no IDLIST term but the table has an integer primary |
drh | c839258 | 2001-12-31 02:48:51 +0000 | [diff] [blame] | 199 | ** key, the set the keyColumn variable to the primary key column index |
| 200 | ** in the original table definition. |
drh | 4a32431 | 2001-12-21 14:30:42 +0000 | [diff] [blame] | 201 | */ |
| 202 | if( pColumn==0 ){ |
| 203 | keyColumn = pTab->iPKey; |
| 204 | } |
| 205 | |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 206 | /* Open the temp table for FOR EACH ROW triggers */ |
| 207 | if (row_triggers_exist) |
| 208 | sqliteVdbeAddOp(v, OP_OpenTemp, newIdx, 0); |
| 209 | |
drh | feeb139 | 2002-04-09 03:28:01 +0000 | [diff] [blame] | 210 | /* Initialize the count of rows to be inserted |
| 211 | */ |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 212 | if( db->flags & SQLITE_CountRows && !pParse->trigStack){ |
drh | feeb139 | 2002-04-09 03:28:01 +0000 | [diff] [blame] | 213 | sqliteVdbeAddOp(v, OP_Integer, 0, 0); /* Initialize the row count */ |
| 214 | } |
| 215 | |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 216 | /* Open tables and indices if there are no row triggers */ |
| 217 | if (!row_triggers_exist) { |
| 218 | base = pParse->nTab; |
| 219 | openOp = pTab->isTemp ? OP_OpenWrAux : OP_OpenWrite; |
| 220 | sqliteVdbeAddOp(v, openOp, base, pTab->tnum); |
| 221 | sqliteVdbeChangeP3(v, -1, pTab->zName, P3_STATIC); |
| 222 | for(idx=1, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, idx++){ |
| 223 | sqliteVdbeAddOp(v, openOp, idx+base, pIdx->tnum); |
| 224 | sqliteVdbeChangeP3(v, -1, pIdx->zName, P3_STATIC); |
| 225 | } |
| 226 | pParse->nTab += idx; |
| 227 | } |
| 228 | |
drh | 1ccde15 | 2000-06-17 13:12:39 +0000 | [diff] [blame] | 229 | /* If the data source is a SELECT statement, then we have to create |
| 230 | ** a loop because there might be multiple rows of data. If the data |
| 231 | ** source is an expression list, then exactly one row will be inserted |
| 232 | ** and the loop is not used. |
| 233 | */ |
drh | 5974a30 | 2000-06-07 14:42:26 +0000 | [diff] [blame] | 234 | if( srcTab>=0 ){ |
drh | 5974a30 | 2000-06-07 14:42:26 +0000 | [diff] [blame] | 235 | iBreak = sqliteVdbeMakeLabel(v); |
drh | 6b56344 | 2001-11-07 16:48:26 +0000 | [diff] [blame] | 236 | sqliteVdbeAddOp(v, OP_Rewind, srcTab, iBreak); |
| 237 | iCont = sqliteVdbeCurrentAddr(v); |
drh | 5974a30 | 2000-06-07 14:42:26 +0000 | [diff] [blame] | 238 | } |
drh | 1ccde15 | 2000-06-17 13:12:39 +0000 | [diff] [blame] | 239 | |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 240 | if (row_triggers_exist) { |
| 241 | |
| 242 | /* build the new.* reference row */ |
| 243 | sqliteVdbeAddOp(v, OP_Integer, 13, 0); |
| 244 | for(i=0; i<pTab->nCol; i++){ |
| 245 | if( pColumn==0 ){ |
| 246 | j = i; |
| 247 | }else{ |
| 248 | for(j=0; j<pColumn->nId; j++){ |
| 249 | if( pColumn->a[j].idx==i ) break; |
| 250 | } |
| 251 | } |
| 252 | if( pColumn && j>=pColumn->nId ){ |
| 253 | sqliteVdbeAddOp(v, OP_String, 0, 0); |
| 254 | sqliteVdbeChangeP3(v, -1, pTab->aCol[i].zDflt, P3_STATIC); |
| 255 | }else if( srcTab>=0 ){ |
| 256 | sqliteVdbeAddOp(v, OP_Column, srcTab, j); |
| 257 | }else{ |
| 258 | sqliteExprCode(pParse, pList->a[j].pExpr); |
| 259 | } |
| 260 | } |
| 261 | sqliteVdbeAddOp(v, OP_MakeRecord, pTab->nCol, 0); |
| 262 | sqliteVdbeAddOp(v, OP_PutIntKey, newIdx, 0); |
| 263 | sqliteVdbeAddOp(v, OP_Rewind, newIdx, 0); |
| 264 | |
| 265 | /* Fire BEFORE triggers */ |
| 266 | if ( |
| 267 | sqliteCodeRowTrigger(pParse, TK_INSERT, 0, TK_BEFORE, pTab, newIdx, -1, |
| 268 | onError) |
| 269 | ) goto insert_cleanup; |
| 270 | |
| 271 | /* Open the tables and indices for the INSERT */ |
| 272 | if (!pTab->pSelect) { |
| 273 | base = pParse->nTab; |
| 274 | openOp = pTab->isTemp ? OP_OpenWrAux : OP_OpenWrite; |
| 275 | sqliteVdbeAddOp(v, openOp, base, pTab->tnum); |
| 276 | sqliteVdbeChangeP3(v, -1, pTab->zName, P3_STATIC); |
| 277 | for(idx=1, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, idx++){ |
| 278 | sqliteVdbeAddOp(v, openOp, idx+base, pIdx->tnum); |
| 279 | sqliteVdbeChangeP3(v, -1, pIdx->zName, P3_STATIC); |
| 280 | } |
| 281 | pParse->nTab += idx; |
| 282 | } |
| 283 | } |
| 284 | |
drh | 4a32431 | 2001-12-21 14:30:42 +0000 | [diff] [blame] | 285 | /* Push the record number for the new entry onto the stack. The |
| 286 | ** record number is a randomly generate integer created by NewRecno |
| 287 | ** except when the table has an INTEGER PRIMARY KEY column, in which |
drh | b419a92 | 2002-01-30 16:17:23 +0000 | [diff] [blame] | 288 | ** case the record number is the same as that column. |
drh | 1ccde15 | 2000-06-17 13:12:39 +0000 | [diff] [blame] | 289 | */ |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 290 | if (!pTab->pSelect) { |
| 291 | if( keyColumn>=0 ){ |
| 292 | if( srcTab>=0 ){ |
| 293 | sqliteVdbeAddOp(v, OP_Column, srcTab, keyColumn); |
| 294 | }else{ |
| 295 | int addr; |
| 296 | sqliteExprCode(pParse, pList->a[keyColumn].pExpr); |
drh | e1e68f4 | 2002-03-31 18:29:03 +0000 | [diff] [blame] | 297 | |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 298 | /* If the PRIMARY KEY expression is NULL, then use OP_NewRecno |
| 299 | ** to generate a unique primary key value. |
| 300 | */ |
| 301 | addr = sqliteVdbeAddOp(v, OP_Dup, 0, 1); |
| 302 | sqliteVdbeAddOp(v, OP_NotNull, 0, addr+4); |
| 303 | sqliteVdbeAddOp(v, OP_Pop, 1, 0); |
| 304 | sqliteVdbeAddOp(v, OP_NewRecno, base, 0); |
| 305 | } |
| 306 | sqliteVdbeAddOp(v, OP_MustBeInt, 0, 0); |
| 307 | }else{ |
drh | e1e68f4 | 2002-03-31 18:29:03 +0000 | [diff] [blame] | 308 | sqliteVdbeAddOp(v, OP_NewRecno, base, 0); |
drh | 4a32431 | 2001-12-21 14:30:42 +0000 | [diff] [blame] | 309 | } |
drh | 4a32431 | 2001-12-21 14:30:42 +0000 | [diff] [blame] | 310 | |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 311 | /* Push onto the stack, data for all columns of the new entry, beginning |
| 312 | ** with the first column. |
| 313 | */ |
| 314 | for(i=0; i<pTab->nCol; i++){ |
| 315 | if( i==pTab->iPKey ){ |
| 316 | /* The value of the INTEGER PRIMARY KEY column is always a NULL. |
| 317 | ** Whenever this column is read, the record number will be substituted |
| 318 | ** in its place. So will fill this column with a NULL to avoid |
| 319 | ** taking up data space with information that will never be used. */ |
| 320 | sqliteVdbeAddOp(v, OP_String, 0, 0); |
| 321 | continue; |
| 322 | } |
| 323 | if( pColumn==0 ){ |
| 324 | j = i; |
| 325 | }else{ |
| 326 | for(j=0; j<pColumn->nId; j++){ |
| 327 | if( pColumn->a[j].idx==i ) break; |
| 328 | } |
| 329 | } |
| 330 | if( pColumn && j>=pColumn->nId ){ |
| 331 | sqliteVdbeAddOp(v, OP_String, 0, 0); |
| 332 | sqliteVdbeChangeP3(v, -1, pTab->aCol[i].zDflt, P3_STATIC); |
| 333 | }else if( srcTab>=0 ){ |
| 334 | sqliteVdbeAddOp(v, OP_Column, srcTab, j); |
| 335 | }else{ |
| 336 | sqliteExprCode(pParse, pList->a[j].pExpr); |
drh | 5974a30 | 2000-06-07 14:42:26 +0000 | [diff] [blame] | 337 | } |
drh | bed8690 | 2000-06-02 13:27:59 +0000 | [diff] [blame] | 338 | } |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 339 | |
| 340 | /* Generate code to check constraints and generate index keys and |
| 341 | ** do the insertion. |
| 342 | */ |
| 343 | endOfLoop = sqliteVdbeMakeLabel(v); |
| 344 | sqliteGenerateConstraintChecks(pParse, pTab, base, 0,0,0,onError,endOfLoop); |
| 345 | sqliteCompleteInsertion(pParse, pTab, base, 0,0,0); |
| 346 | |
| 347 | /* Update the count of rows that are inserted |
| 348 | */ |
| 349 | if( (db->flags & SQLITE_CountRows)!=0 && !pParse->trigStack){ |
| 350 | sqliteVdbeAddOp(v, OP_AddImm, 1, 0); |
drh | 5974a30 | 2000-06-07 14:42:26 +0000 | [diff] [blame] | 351 | } |
| 352 | } |
drh | 1ccde15 | 2000-06-17 13:12:39 +0000 | [diff] [blame] | 353 | |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 354 | if (row_triggers_exist) { |
| 355 | /* Close all tables opened */ |
| 356 | if (!pTab->pSelect) { |
| 357 | sqliteVdbeAddOp(v, OP_Close, base, 0); |
| 358 | for(idx=1, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, idx++){ |
| 359 | sqliteVdbeAddOp(v, OP_Close, idx+base, 0); |
| 360 | } |
| 361 | } |
drh | 1bee3d7 | 2001-10-15 00:44:35 +0000 | [diff] [blame] | 362 | |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 363 | /* Code AFTER triggers */ |
| 364 | if ( |
| 365 | sqliteCodeRowTrigger(pParse, TK_INSERT, 0, TK_AFTER, pTab, newIdx, -1, |
| 366 | onError) |
| 367 | ) goto insert_cleanup; |
drh | 1bee3d7 | 2001-10-15 00:44:35 +0000 | [diff] [blame] | 368 | } |
| 369 | |
drh | 1ccde15 | 2000-06-17 13:12:39 +0000 | [diff] [blame] | 370 | /* The bottom of the loop, if the data source is a SELECT statement |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 371 | */ |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 372 | sqliteVdbeResolveLabel(v, endOfLoop); |
drh | 5974a30 | 2000-06-07 14:42:26 +0000 | [diff] [blame] | 373 | if( srcTab>=0 ){ |
drh | 6b56344 | 2001-11-07 16:48:26 +0000 | [diff] [blame] | 374 | sqliteVdbeAddOp(v, OP_Next, srcTab, iCont); |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 375 | sqliteVdbeResolveLabel(v, iBreak); |
drh | 6b56344 | 2001-11-07 16:48:26 +0000 | [diff] [blame] | 376 | sqliteVdbeAddOp(v, OP_Close, srcTab, 0); |
| 377 | } |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 378 | |
| 379 | if (!row_triggers_exist) { |
| 380 | /* Close all tables opened */ |
| 381 | sqliteVdbeAddOp(v, OP_Close, base, 0); |
| 382 | for(idx=1, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, idx++){ |
| 383 | sqliteVdbeAddOp(v, OP_Close, idx+base, 0); |
| 384 | } |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 385 | } |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 386 | |
drh | 1c92853 | 2002-01-31 15:54:21 +0000 | [diff] [blame] | 387 | sqliteEndWriteOperation(pParse); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 388 | |
drh | 1bee3d7 | 2001-10-15 00:44:35 +0000 | [diff] [blame] | 389 | /* |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 390 | ** Return the number of rows inserted. |
drh | 1bee3d7 | 2001-10-15 00:44:35 +0000 | [diff] [blame] | 391 | */ |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 392 | if( db->flags & SQLITE_CountRows && !pParse->trigStack ){ |
drh | 1bee3d7 | 2001-10-15 00:44:35 +0000 | [diff] [blame] | 393 | sqliteVdbeAddOp(v, OP_ColumnCount, 1, 0); |
| 394 | sqliteVdbeAddOp(v, OP_ColumnName, 0, 0); |
| 395 | sqliteVdbeChangeP3(v, -1, "rows inserted", P3_STATIC); |
drh | 1bee3d7 | 2001-10-15 00:44:35 +0000 | [diff] [blame] | 396 | sqliteVdbeAddOp(v, OP_Callback, 1, 0); |
| 397 | } |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 398 | |
| 399 | insert_cleanup: |
drh | 5974a30 | 2000-06-07 14:42:26 +0000 | [diff] [blame] | 400 | if( pList ) sqliteExprListDelete(pList); |
| 401 | if( pSelect ) sqliteSelectDelete(pSelect); |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 402 | if ( zTab ) sqliteFree(zTab); |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 403 | sqliteIdListDelete(pColumn); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 404 | } |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 405 | |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 406 | /* |
| 407 | ** Generate code to do a constraint check prior to an INSERT or an UPDATE. |
| 408 | ** |
| 409 | ** When this routine is called, the stack contains (from bottom to top) |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 410 | ** the following values: |
| 411 | ** |
drh | b419a92 | 2002-01-30 16:17:23 +0000 | [diff] [blame] | 412 | ** 1. The recno of the row to be updated before it is updated. This |
| 413 | ** value is omitted unless we are doing an UPDATE that involves a |
| 414 | ** change to the record number. |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 415 | ** |
drh | b419a92 | 2002-01-30 16:17:23 +0000 | [diff] [blame] | 416 | ** 2. The recno of the row after the update. |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 417 | ** |
| 418 | ** 3. The data in the first column of the entry after the update. |
| 419 | ** |
| 420 | ** i. Data from middle columns... |
| 421 | ** |
| 422 | ** N. The data in the last column of the entry after the update. |
| 423 | ** |
drh | b419a92 | 2002-01-30 16:17:23 +0000 | [diff] [blame] | 424 | ** The old recno shown as entry (1) above is omitted unless both isUpdate |
drh | 1c92853 | 2002-01-31 15:54:21 +0000 | [diff] [blame] | 425 | ** and recnoChng are 1. isUpdate is true for UPDATEs and false for |
| 426 | ** INSERTs and recnoChng is true if the record number is being changed. |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 427 | ** |
| 428 | ** The code generated by this routine pushes additional entries onto |
| 429 | ** the stack which are the keys for new index entries for the new record. |
| 430 | ** The order of index keys is the same as the order of the indices on |
| 431 | ** the pTable->pIndex list. A key is only created for index i if |
| 432 | ** aIdxUsed!=0 and aIdxUsed[i]!=0. |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 433 | ** |
| 434 | ** This routine also generates code to check constraints. NOT NULL, |
| 435 | ** CHECK, and UNIQUE constraints are all checked. If a constraint fails, |
drh | 1c92853 | 2002-01-31 15:54:21 +0000 | [diff] [blame] | 436 | ** then the appropriate action is performed. There are five possible |
| 437 | ** actions: ROLLBACK, ABORT, FAIL, REPLACE, and IGNORE. |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 438 | ** |
| 439 | ** Constraint type Action What Happens |
| 440 | ** --------------- ---------- ---------------------------------------- |
drh | 1c92853 | 2002-01-31 15:54:21 +0000 | [diff] [blame] | 441 | ** any ROLLBACK The current transaction is rolled back and |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 442 | ** sqlite_exec() returns immediately with a |
| 443 | ** return code of SQLITE_CONSTRAINT. |
| 444 | ** |
drh | 1c92853 | 2002-01-31 15:54:21 +0000 | [diff] [blame] | 445 | ** any ABORT Back out changes from the current command |
| 446 | ** only (do not do a complete rollback) then |
| 447 | ** cause sqlite_exec() to return immediately |
| 448 | ** with SQLITE_CONSTRAINT. |
| 449 | ** |
| 450 | ** any FAIL Sqlite_exec() returns immediately with a |
| 451 | ** return code of SQLITE_CONSTRAINT. The |
| 452 | ** transaction is not rolled back and any |
| 453 | ** prior changes are retained. |
| 454 | ** |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 455 | ** any IGNORE The record number and data is popped from |
| 456 | ** the stack and there is an immediate jump |
| 457 | ** to label ignoreDest. |
| 458 | ** |
| 459 | ** NOT NULL REPLACE The NULL value is replace by the default |
| 460 | ** value for that column. If the default value |
| 461 | ** is NULL, the action is the same as ABORT. |
| 462 | ** |
| 463 | ** UNIQUE REPLACE The other row that conflicts with the row |
| 464 | ** being inserted is removed. |
| 465 | ** |
| 466 | ** CHECK REPLACE Illegal. The results in an exception. |
| 467 | ** |
drh | 1c92853 | 2002-01-31 15:54:21 +0000 | [diff] [blame] | 468 | ** Which action to take is determined by the overrideError parameter. |
| 469 | ** Or if overrideError==OE_Default, then the pParse->onError parameter |
| 470 | ** is used. Or if pParse->onError==OE_Default then the onError value |
| 471 | ** for the constraint is used. |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 472 | ** |
drh | aaab572 | 2002-02-19 13:39:21 +0000 | [diff] [blame] | 473 | ** The calling routine must open a read/write cursor for pTab with |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 474 | ** cursor number "base". All indices of pTab must also have open |
| 475 | ** read/write cursors with cursor number base+i for the i-th cursor. |
| 476 | ** Except, if there is no possibility of a REPLACE action then |
| 477 | ** cursors do not need to be open for indices where aIdxUsed[i]==0. |
| 478 | ** |
| 479 | ** If the isUpdate flag is true, it means that the "base" cursor is |
| 480 | ** initially pointing to an entry that is being updated. The isUpdate |
| 481 | ** flag causes extra code to be generated so that the "base" cursor |
| 482 | ** is still pointing at the same entry after the routine returns. |
| 483 | ** Without the isUpdate flag, the "base" cursor might be moved. |
| 484 | */ |
| 485 | void sqliteGenerateConstraintChecks( |
| 486 | Parse *pParse, /* The parser context */ |
| 487 | Table *pTab, /* the table into which we are inserting */ |
| 488 | int base, /* Index of a read/write cursor pointing at pTab */ |
| 489 | char *aIdxUsed, /* Which indices are used. NULL means all are used */ |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 490 | int recnoChng, /* True if the record number will change */ |
drh | b419a92 | 2002-01-30 16:17:23 +0000 | [diff] [blame] | 491 | int isUpdate, /* True for UPDATE, False for INSERT */ |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 492 | int overrideError, /* Override onError to this if not OE_Default */ |
drh | b419a92 | 2002-01-30 16:17:23 +0000 | [diff] [blame] | 493 | int ignoreDest /* Jump to this label on an OE_Ignore resolution */ |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 494 | ){ |
| 495 | int i; |
| 496 | Vdbe *v; |
| 497 | int nCol; |
| 498 | int onError; |
| 499 | int addr; |
| 500 | int extra; |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 501 | int iCur; |
| 502 | Index *pIdx; |
| 503 | int seenReplace = 0; |
| 504 | int jumpInst; |
| 505 | int contAddr; |
drh | b419a92 | 2002-01-30 16:17:23 +0000 | [diff] [blame] | 506 | int hasTwoRecnos = (isUpdate && recnoChng); |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 507 | |
| 508 | v = sqliteGetVdbe(pParse); |
| 509 | assert( v!=0 ); |
drh | 417be79 | 2002-03-03 18:59:40 +0000 | [diff] [blame] | 510 | assert( pTab->pSelect==0 ); /* This table is not a VIEW */ |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 511 | nCol = pTab->nCol; |
| 512 | |
| 513 | /* Test all NOT NULL constraints. |
| 514 | */ |
| 515 | for(i=0; i<nCol; i++){ |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 516 | if( i==pTab->iPKey ){ |
| 517 | /* Fix me: Make sure the INTEGER PRIMARY KEY is not NULL. */ |
| 518 | continue; |
| 519 | } |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 520 | onError = pTab->aCol[i].notNull; |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 521 | if( onError==OE_None ) continue; |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 522 | if( overrideError!=OE_Default ){ |
| 523 | onError = overrideError; |
drh | 1c92853 | 2002-01-31 15:54:21 +0000 | [diff] [blame] | 524 | }else if( onError==OE_Default ){ |
drh | 0d65dc0 | 2002-02-03 00:56:09 +0000 | [diff] [blame] | 525 | onError = pParse->db->onError; |
| 526 | if( onError==OE_Default ) onError = OE_Abort; |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 527 | } |
| 528 | if( onError==OE_Replace && pTab->aCol[i].zDflt==0 ){ |
| 529 | onError = OE_Abort; |
| 530 | } |
drh | ef6764a | 2002-01-30 04:32:00 +0000 | [diff] [blame] | 531 | sqliteVdbeAddOp(v, OP_Dup, nCol-1-i, 1); |
| 532 | addr = sqliteVdbeAddOp(v, OP_NotNull, 0, 0); |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 533 | switch( onError ){ |
drh | 1c92853 | 2002-01-31 15:54:21 +0000 | [diff] [blame] | 534 | case OE_Rollback: |
| 535 | case OE_Abort: |
| 536 | case OE_Fail: { |
| 537 | sqliteVdbeAddOp(v, OP_Halt, SQLITE_CONSTRAINT, onError); |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 538 | break; |
| 539 | } |
| 540 | case OE_Ignore: { |
drh | b419a92 | 2002-01-30 16:17:23 +0000 | [diff] [blame] | 541 | sqliteVdbeAddOp(v, OP_Pop, nCol+1+hasTwoRecnos, 0); |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 542 | sqliteVdbeAddOp(v, OP_Goto, 0, ignoreDest); |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 543 | break; |
| 544 | } |
| 545 | case OE_Replace: { |
| 546 | sqliteVdbeAddOp(v, OP_String, 0, 0); |
| 547 | sqliteVdbeChangeP3(v, -1, pTab->aCol[i].zDflt, P3_STATIC); |
| 548 | sqliteVdbeAddOp(v, OP_Push, nCol-i, 0); |
| 549 | break; |
| 550 | } |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 551 | default: assert(0); |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 552 | } |
drh | ef6764a | 2002-01-30 04:32:00 +0000 | [diff] [blame] | 553 | sqliteVdbeChangeP2(v, addr, sqliteVdbeCurrentAddr(v)); |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 554 | } |
| 555 | |
| 556 | /* Test all CHECK constraints |
| 557 | */ |
| 558 | |
| 559 | /* Test all UNIQUE constraints. Add index records as we go. |
| 560 | */ |
drh | 0d65dc0 | 2002-02-03 00:56:09 +0000 | [diff] [blame] | 561 | if( (recnoChng || !isUpdate) && pTab->iPKey>=0 ){ |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 562 | onError = pTab->keyConf; |
| 563 | if( overrideError!=OE_Default ){ |
| 564 | onError = overrideError; |
drh | 1c92853 | 2002-01-31 15:54:21 +0000 | [diff] [blame] | 565 | }else if( onError==OE_Default ){ |
drh | 0d65dc0 | 2002-02-03 00:56:09 +0000 | [diff] [blame] | 566 | onError = pParse->db->onError; |
| 567 | if( onError==OE_Default ) onError = OE_Abort; |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 568 | } |
drh | 0d65dc0 | 2002-02-03 00:56:09 +0000 | [diff] [blame] | 569 | if( onError!=OE_Replace ){ |
| 570 | sqliteVdbeAddOp(v, OP_Dup, nCol, 1); |
| 571 | jumpInst = sqliteVdbeAddOp(v, OP_NotExists, base, 0); |
| 572 | switch( onError ){ |
| 573 | case OE_Rollback: |
| 574 | case OE_Abort: |
| 575 | case OE_Fail: { |
| 576 | sqliteVdbeAddOp(v, OP_Halt, SQLITE_CONSTRAINT, onError); |
| 577 | break; |
| 578 | } |
| 579 | case OE_Ignore: { |
| 580 | sqliteVdbeAddOp(v, OP_Pop, nCol+1+hasTwoRecnos, 0); |
| 581 | sqliteVdbeAddOp(v, OP_Goto, 0, ignoreDest); |
| 582 | break; |
| 583 | } |
| 584 | default: assert(0); |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 585 | } |
drh | 0d65dc0 | 2002-02-03 00:56:09 +0000 | [diff] [blame] | 586 | contAddr = sqliteVdbeCurrentAddr(v); |
| 587 | sqliteVdbeChangeP2(v, jumpInst, contAddr); |
| 588 | if( isUpdate ){ |
| 589 | sqliteVdbeAddOp(v, OP_Dup, nCol+1, 1); |
| 590 | sqliteVdbeAddOp(v, OP_MoveTo, base, 0); |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 591 | } |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 592 | } |
| 593 | } |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 594 | extra = 0; |
| 595 | for(extra=(-1), iCur=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, iCur++){ |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 596 | if( aIdxUsed && aIdxUsed[iCur]==0 ) continue; |
| 597 | extra++; |
| 598 | sqliteVdbeAddOp(v, OP_Dup, nCol+extra, 1); |
| 599 | for(i=0; i<pIdx->nColumn; i++){ |
| 600 | int idx = pIdx->aiColumn[i]; |
| 601 | if( idx==pTab->iPKey ){ |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 602 | sqliteVdbeAddOp(v, OP_Dup, i+extra+nCol+1, 1); |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 603 | }else{ |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 604 | sqliteVdbeAddOp(v, OP_Dup, i+extra+nCol-idx, 1); |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 605 | } |
| 606 | } |
| 607 | sqliteVdbeAddOp(v, OP_MakeIdxKey, pIdx->nColumn, 0); |
| 608 | onError = pIdx->onError; |
| 609 | if( onError==OE_None ) continue; |
| 610 | if( overrideError!=OE_Default ){ |
| 611 | onError = overrideError; |
drh | 1c92853 | 2002-01-31 15:54:21 +0000 | [diff] [blame] | 612 | }else if( onError==OE_Default ){ |
drh | 0d65dc0 | 2002-02-03 00:56:09 +0000 | [diff] [blame] | 613 | onError = pParse->db->onError; |
| 614 | if( onError==OE_Default ) onError = OE_Abort; |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 615 | } |
drh | b419a92 | 2002-01-30 16:17:23 +0000 | [diff] [blame] | 616 | sqliteVdbeAddOp(v, OP_Dup, extra+nCol+1+hasTwoRecnos, 1); |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 617 | jumpInst = sqliteVdbeAddOp(v, OP_IsUnique, base+iCur+1, 0); |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 618 | switch( onError ){ |
drh | 1c92853 | 2002-01-31 15:54:21 +0000 | [diff] [blame] | 619 | case OE_Rollback: |
| 620 | case OE_Abort: |
| 621 | case OE_Fail: { |
| 622 | sqliteVdbeAddOp(v, OP_Halt, SQLITE_CONSTRAINT, onError); |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 623 | break; |
| 624 | } |
| 625 | case OE_Ignore: { |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 626 | assert( seenReplace==0 ); |
drh | fe1a177 | 2002-04-09 03:15:06 +0000 | [diff] [blame] | 627 | sqliteVdbeAddOp(v, OP_Pop, nCol+extra+3+hasTwoRecnos, 0); |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 628 | sqliteVdbeAddOp(v, OP_Goto, 0, ignoreDest); |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 629 | break; |
| 630 | } |
| 631 | case OE_Replace: { |
drh | c8d30ac | 2002-04-12 10:08:59 +0000 | [diff] [blame] | 632 | sqliteGenerateRowDelete(v, pTab, base, 0); |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 633 | if( isUpdate ){ |
drh | b419a92 | 2002-01-30 16:17:23 +0000 | [diff] [blame] | 634 | sqliteVdbeAddOp(v, OP_Dup, nCol+extra+1+hasTwoRecnos, 1); |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 635 | sqliteVdbeAddOp(v, OP_MoveTo, base, 0); |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 636 | } |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 637 | seenReplace = 1; |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 638 | break; |
| 639 | } |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 640 | default: assert(0); |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 641 | } |
| 642 | contAddr = sqliteVdbeCurrentAddr(v); |
| 643 | sqliteVdbeChangeP2(v, jumpInst, contAddr); |
| 644 | } |
| 645 | } |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 646 | |
| 647 | /* |
| 648 | ** This routine generates code to finish the INSERT or UPDATE operation |
| 649 | ** that was started by a prior call to sqliteGenerateConstraintChecks. |
| 650 | ** The stack must contain keys for all active indices followed by data |
| 651 | ** and the recno for the new entry. This routine creates the new |
| 652 | ** entries in all indices and in the main table. |
| 653 | ** |
drh | b419a92 | 2002-01-30 16:17:23 +0000 | [diff] [blame] | 654 | ** The arguments to this routine should be the same as the first six |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 655 | ** arguments to sqliteGenerateConstraintChecks. |
| 656 | */ |
| 657 | void sqliteCompleteInsertion( |
| 658 | Parse *pParse, /* The parser context */ |
| 659 | Table *pTab, /* the table into which we are inserting */ |
| 660 | int base, /* Index of a read/write cursor pointing at pTab */ |
| 661 | char *aIdxUsed, /* Which indices are used. NULL means all are used */ |
drh | b419a92 | 2002-01-30 16:17:23 +0000 | [diff] [blame] | 662 | int recnoChng, /* True if the record number will change */ |
| 663 | int isUpdate /* True for UPDATE, False for INSERT */ |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 664 | ){ |
| 665 | int i; |
| 666 | Vdbe *v; |
| 667 | int nIdx; |
| 668 | Index *pIdx; |
| 669 | |
| 670 | v = sqliteGetVdbe(pParse); |
| 671 | assert( v!=0 ); |
drh | 417be79 | 2002-03-03 18:59:40 +0000 | [diff] [blame] | 672 | assert( pTab->pSelect==0 ); /* This table is not a VIEW */ |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 673 | for(nIdx=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, nIdx++){} |
| 674 | for(i=nIdx-1; i>=0; i--){ |
| 675 | if( aIdxUsed && aIdxUsed[i]==0 ) continue; |
| 676 | sqliteVdbeAddOp(v, OP_IdxPut, base+i+1, 0); |
| 677 | } |
| 678 | sqliteVdbeAddOp(v, OP_MakeRecord, pTab->nCol, 0); |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 679 | sqliteVdbeAddOp(v, OP_PutIntKey, base, pParse->trigStack?0:1); |
drh | b419a92 | 2002-01-30 16:17:23 +0000 | [diff] [blame] | 680 | if( isUpdate && recnoChng ){ |
drh | 0ca3e24 | 2002-01-29 23:07:02 +0000 | [diff] [blame] | 681 | sqliteVdbeAddOp(v, OP_Pop, 1, 0); |
| 682 | } |
| 683 | } |