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