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