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 | 4a32431 | 2001-12-21 14:30:42 +0000 | [diff] [blame^] | 15 | ** $Id: insert.c,v 1.27 2001/12/21 14:30:43 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 | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 39 | IdList *pColumn /* Column names corresponding to IDLIST. */ |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 40 | ){ |
drh | 5974a30 | 2000-06-07 14:42:26 +0000 | [diff] [blame] | 41 | Table *pTab; /* The table to insert into */ |
| 42 | char *zTab; /* Name of the table into which we are inserting */ |
| 43 | int i, j, idx; /* Loop counters */ |
| 44 | Vdbe *v; /* Generate code into this virtual machine */ |
| 45 | Index *pIdx; /* For looping over indices of the table */ |
| 46 | int srcTab; /* Date comes from this temporary cursor if >=0 */ |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 47 | int nColumn; /* Number of columns in the data */ |
drh | 5974a30 | 2000-06-07 14:42:26 +0000 | [diff] [blame] | 48 | int base; /* First available cursor */ |
| 49 | int iCont, iBreak; /* Beginning and end of the loop over srcTab */ |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 50 | sqlite *db; /* The main database structure */ |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 51 | int openOp; /* Opcode used to open cursors */ |
drh | 4a32431 | 2001-12-21 14:30:42 +0000 | [diff] [blame^] | 52 | int keyColumn = -1; /* Column that is the INTEGER PRIMARY KEY */ |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 53 | |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 54 | if( pParse->nErr || sqlite_malloc_failed ) goto insert_cleanup; |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 55 | db = pParse->db; |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 56 | |
drh | 1ccde15 | 2000-06-17 13:12:39 +0000 | [diff] [blame] | 57 | /* Locate the table into which we will be inserting new information. |
| 58 | */ |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 59 | zTab = sqliteTableNameFromToken(pTableName); |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 60 | if( zTab==0 ) goto insert_cleanup; |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 61 | pTab = sqliteFindTable(db, zTab); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 62 | sqliteFree(zTab); |
| 63 | if( pTab==0 ){ |
| 64 | sqliteSetNString(&pParse->zErrMsg, "no such table: ", 0, |
| 65 | pTableName->z, pTableName->n, 0); |
| 66 | pParse->nErr++; |
| 67 | goto insert_cleanup; |
| 68 | } |
| 69 | if( pTab->readOnly ){ |
| 70 | sqliteSetString(&pParse->zErrMsg, "table ", pTab->zName, |
| 71 | " may not be modified", 0); |
| 72 | pParse->nErr++; |
| 73 | goto insert_cleanup; |
| 74 | } |
drh | 1ccde15 | 2000-06-17 13:12:39 +0000 | [diff] [blame] | 75 | |
| 76 | /* Allocate a VDBE |
| 77 | */ |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 78 | v = sqliteGetVdbe(pParse); |
drh | 5974a30 | 2000-06-07 14:42:26 +0000 | [diff] [blame] | 79 | if( v==0 ) goto insert_cleanup; |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 80 | if( (db->flags & SQLITE_InTrans)==0 ){ |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 81 | sqliteVdbeAddOp(v, OP_Transaction, 0, 0); |
| 82 | sqliteVdbeAddOp(v, OP_VerifyCookie, db->schema_cookie, 0); |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 83 | pParse->schemaVerified = 1; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 84 | } |
drh | 1ccde15 | 2000-06-17 13:12:39 +0000 | [diff] [blame] | 85 | |
| 86 | /* Figure out how many columns of data are supplied. If the data |
| 87 | ** is comming from a SELECT statement, then this step has to generate |
| 88 | ** all the code to implement the SELECT statement and leave the data |
| 89 | ** in a temporary table. If data is coming from an expression list, |
| 90 | ** then we just have to count the number of expressions. |
| 91 | */ |
drh | 5974a30 | 2000-06-07 14:42:26 +0000 | [diff] [blame] | 92 | if( pSelect ){ |
| 93 | int rc; |
| 94 | srcTab = pParse->nTab++; |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 95 | sqliteVdbeAddOp(v, OP_OpenTemp, srcTab, 0); |
drh | 5974a30 | 2000-06-07 14:42:26 +0000 | [diff] [blame] | 96 | rc = sqliteSelect(pParse, pSelect, SRT_Table, srcTab); |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 97 | if( rc || pParse->nErr || sqlite_malloc_failed ) goto insert_cleanup; |
drh | 5974a30 | 2000-06-07 14:42:26 +0000 | [diff] [blame] | 98 | assert( pSelect->pEList ); |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 99 | nColumn = pSelect->pEList->nExpr; |
drh | 5974a30 | 2000-06-07 14:42:26 +0000 | [diff] [blame] | 100 | }else{ |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 101 | assert( pList!=0 ); |
drh | 5974a30 | 2000-06-07 14:42:26 +0000 | [diff] [blame] | 102 | srcTab = -1; |
| 103 | assert( pList ); |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 104 | nColumn = pList->nExpr; |
drh | 5974a30 | 2000-06-07 14:42:26 +0000 | [diff] [blame] | 105 | } |
drh | 1ccde15 | 2000-06-17 13:12:39 +0000 | [diff] [blame] | 106 | |
| 107 | /* Make sure the number of columns in the source data matches the number |
| 108 | ** of columns to be inserted into the table. |
| 109 | */ |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 110 | if( pColumn==0 && nColumn!=pTab->nCol ){ |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 111 | char zNum1[30]; |
| 112 | char zNum2[30]; |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 113 | sprintf(zNum1,"%d", nColumn); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 114 | sprintf(zNum2,"%d", pTab->nCol); |
| 115 | sqliteSetString(&pParse->zErrMsg, "table ", pTab->zName, |
| 116 | " has ", zNum2, " columns but ", |
| 117 | zNum1, " values were supplied", 0); |
| 118 | pParse->nErr++; |
| 119 | goto insert_cleanup; |
| 120 | } |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 121 | if( pColumn!=0 && nColumn!=pColumn->nId ){ |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 122 | char zNum1[30]; |
| 123 | char zNum2[30]; |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 124 | sprintf(zNum1,"%d", nColumn); |
| 125 | sprintf(zNum2,"%d", pColumn->nId); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 126 | sqliteSetString(&pParse->zErrMsg, zNum1, " values for ", |
| 127 | zNum2, " columns", 0); |
| 128 | pParse->nErr++; |
| 129 | goto insert_cleanup; |
| 130 | } |
drh | 1ccde15 | 2000-06-17 13:12:39 +0000 | [diff] [blame] | 131 | |
| 132 | /* If the INSERT statement included an IDLIST term, then make sure |
| 133 | ** all elements of the IDLIST really are columns of the table and |
| 134 | ** remember the column indices. |
| 135 | */ |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 136 | if( pColumn ){ |
| 137 | for(i=0; i<pColumn->nId; i++){ |
| 138 | pColumn->a[i].idx = -1; |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 139 | } |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 140 | for(i=0; i<pColumn->nId; i++){ |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 141 | for(j=0; j<pTab->nCol; j++){ |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 142 | if( sqliteStrICmp(pColumn->a[i].zName, pTab->aCol[j].zName)==0 ){ |
| 143 | pColumn->a[i].idx = j; |
drh | 4a32431 | 2001-12-21 14:30:42 +0000 | [diff] [blame^] | 144 | if( j==pTab->iPKey ){ |
| 145 | keyColumn = j; |
| 146 | } |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 147 | break; |
| 148 | } |
| 149 | } |
| 150 | if( j>=pTab->nCol ){ |
| 151 | sqliteSetString(&pParse->zErrMsg, "table ", pTab->zName, |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 152 | " has no column named ", pColumn->a[i].zName, 0); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 153 | pParse->nErr++; |
| 154 | goto insert_cleanup; |
| 155 | } |
| 156 | } |
| 157 | } |
drh | 1ccde15 | 2000-06-17 13:12:39 +0000 | [diff] [blame] | 158 | |
drh | 4a32431 | 2001-12-21 14:30:42 +0000 | [diff] [blame^] | 159 | /* If there is not IDLIST term but the table has an integer primary |
| 160 | ** key, the set the keyColumn variable to the primary key column. |
| 161 | */ |
| 162 | if( pColumn==0 ){ |
| 163 | keyColumn = pTab->iPKey; |
| 164 | } |
| 165 | |
drh | 1ccde15 | 2000-06-17 13:12:39 +0000 | [diff] [blame] | 166 | /* Open cursors into the table that is received the new data and |
| 167 | ** all indices of that table. |
| 168 | */ |
drh | 5974a30 | 2000-06-07 14:42:26 +0000 | [diff] [blame] | 169 | base = pParse->nTab; |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 170 | openOp = pTab->isTemp ? OP_OpenWrAux : OP_OpenWrite; |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 171 | sqliteVdbeAddOp(v, openOp, base, pTab->tnum); |
| 172 | sqliteVdbeChangeP3(v, -1, pTab->zName, P3_STATIC); |
drh | 5974a30 | 2000-06-07 14:42:26 +0000 | [diff] [blame] | 173 | for(idx=1, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, idx++){ |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 174 | sqliteVdbeAddOp(v, openOp, idx+base, pIdx->tnum); |
| 175 | sqliteVdbeChangeP3(v, -1, pIdx->zName, P3_STATIC); |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 176 | } |
drh | 1ccde15 | 2000-06-17 13:12:39 +0000 | [diff] [blame] | 177 | |
| 178 | /* If the data source is a SELECT statement, then we have to create |
| 179 | ** a loop because there might be multiple rows of data. If the data |
| 180 | ** source is an expression list, then exactly one row will be inserted |
| 181 | ** and the loop is not used. |
| 182 | */ |
drh | 5974a30 | 2000-06-07 14:42:26 +0000 | [diff] [blame] | 183 | if( srcTab>=0 ){ |
drh | 1bee3d7 | 2001-10-15 00:44:35 +0000 | [diff] [blame] | 184 | if( db->flags & SQLITE_CountRows ){ |
| 185 | sqliteVdbeAddOp(v, OP_Integer, 0, 0); /* Initialize the row count */ |
| 186 | } |
drh | 5974a30 | 2000-06-07 14:42:26 +0000 | [diff] [blame] | 187 | iBreak = sqliteVdbeMakeLabel(v); |
drh | 6b56344 | 2001-11-07 16:48:26 +0000 | [diff] [blame] | 188 | sqliteVdbeAddOp(v, OP_Rewind, srcTab, iBreak); |
| 189 | iCont = sqliteVdbeCurrentAddr(v); |
drh | 5974a30 | 2000-06-07 14:42:26 +0000 | [diff] [blame] | 190 | } |
drh | 1ccde15 | 2000-06-17 13:12:39 +0000 | [diff] [blame] | 191 | |
drh | 4a32431 | 2001-12-21 14:30:42 +0000 | [diff] [blame^] | 192 | /* Push the record number for the new entry onto the stack. The |
| 193 | ** record number is a randomly generate integer created by NewRecno |
| 194 | ** except when the table has an INTEGER PRIMARY KEY column, in which |
| 195 | ** case the record number is the same as that column. |
drh | 1ccde15 | 2000-06-17 13:12:39 +0000 | [diff] [blame] | 196 | */ |
drh | 4a32431 | 2001-12-21 14:30:42 +0000 | [diff] [blame^] | 197 | if( keyColumn>=0 ){ |
| 198 | if( srcTab>=0 ){ |
| 199 | sqliteVdbeAddOp(v, OP_Column, srcTab, keyColumn); |
| 200 | }else{ |
| 201 | sqliteExprCode(pParse, pList->a[keyColumn].pExpr); |
| 202 | } |
| 203 | sqliteVdbeAddOp(v, OP_AddImm, 0, 0); /* Make sure ROWID is an integer */ |
| 204 | }else{ |
| 205 | sqliteVdbeAddOp(v, OP_NewRecno, base, 0); |
| 206 | } |
| 207 | |
| 208 | /* If there are indices, we'll need this record number again, so make |
| 209 | ** a copy. |
| 210 | */ |
drh | 5974a30 | 2000-06-07 14:42:26 +0000 | [diff] [blame] | 211 | if( pTab->pIndex ){ |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 212 | sqliteVdbeAddOp(v, OP_Dup, 0, 0); |
drh | 5974a30 | 2000-06-07 14:42:26 +0000 | [diff] [blame] | 213 | } |
drh | 4a32431 | 2001-12-21 14:30:42 +0000 | [diff] [blame^] | 214 | |
| 215 | /* Push onto the stack data for all columns of the new entry, beginning |
| 216 | ** with the first column. |
| 217 | */ |
drh | 5974a30 | 2000-06-07 14:42:26 +0000 | [diff] [blame] | 218 | for(i=0; i<pTab->nCol; i++){ |
drh | 4a32431 | 2001-12-21 14:30:42 +0000 | [diff] [blame^] | 219 | if( i==pTab->iPKey ){ |
| 220 | /* The value of the INTEGER PRIMARY KEY column is always a NULL. |
| 221 | ** Whenever this column is used, the record number will be substituted |
| 222 | ** in its place, so there is no point it it taking up space in |
| 223 | ** the data record. */ |
| 224 | sqliteVdbeAddOp(v, OP_String, 0, 0); |
| 225 | continue; |
| 226 | } |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 227 | if( pColumn==0 ){ |
drh | 5974a30 | 2000-06-07 14:42:26 +0000 | [diff] [blame] | 228 | j = i; |
| 229 | }else{ |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 230 | for(j=0; j<pColumn->nId; j++){ |
| 231 | if( pColumn->a[j].idx==i ) break; |
drh | 5974a30 | 2000-06-07 14:42:26 +0000 | [diff] [blame] | 232 | } |
drh | bed8690 | 2000-06-02 13:27:59 +0000 | [diff] [blame] | 233 | } |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 234 | if( pColumn && j>=pColumn->nId ){ |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 235 | sqliteVdbeAddOp(v, OP_String, 0, 0); |
| 236 | sqliteVdbeChangeP3(v, -1, pTab->aCol[i].zDflt, P3_STATIC); |
drh | 5974a30 | 2000-06-07 14:42:26 +0000 | [diff] [blame] | 237 | }else if( srcTab>=0 ){ |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 238 | sqliteVdbeAddOp(v, OP_Column, srcTab, i); |
drh | 5974a30 | 2000-06-07 14:42:26 +0000 | [diff] [blame] | 239 | }else{ |
| 240 | sqliteExprCode(pParse, pList->a[j].pExpr); |
| 241 | } |
| 242 | } |
drh | 1ccde15 | 2000-06-17 13:12:39 +0000 | [diff] [blame] | 243 | |
drh | 4a32431 | 2001-12-21 14:30:42 +0000 | [diff] [blame^] | 244 | /* Create the new record and put it into the database. |
| 245 | */ |
| 246 | sqliteVdbeAddOp(v, OP_MakeRecord, pTab->nCol, 0); |
| 247 | sqliteVdbeAddOp(v, OP_Put, base, keyColumn>=0); |
| 248 | |
drh | 1ccde15 | 2000-06-17 13:12:39 +0000 | [diff] [blame] | 249 | /* Create appropriate entries for the new data row in all indices |
| 250 | ** of the table. |
| 251 | */ |
drh | 5974a30 | 2000-06-07 14:42:26 +0000 | [diff] [blame] | 252 | for(idx=1, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, idx++){ |
| 253 | if( pIdx->pNext ){ |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 254 | sqliteVdbeAddOp(v, OP_Dup, 0, 0); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 255 | } |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 256 | for(i=0; i<pIdx->nColumn; i++){ |
| 257 | int idx = pIdx->aiColumn[i]; |
drh | 4a32431 | 2001-12-21 14:30:42 +0000 | [diff] [blame^] | 258 | if( idx==pTab->iPKey ){ |
| 259 | /* Copy the record number in place of the INTEGER PRIMARY KEY column */ |
| 260 | sqliteVdbeAddOp(v, OP_Dup, i, 0); |
| 261 | continue; |
| 262 | } |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 263 | if( pColumn==0 ){ |
drh | 5974a30 | 2000-06-07 14:42:26 +0000 | [diff] [blame] | 264 | j = idx; |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 265 | }else{ |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 266 | for(j=0; j<pColumn->nId; j++){ |
| 267 | if( pColumn->a[j].idx==idx ) break; |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 268 | } |
| 269 | } |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 270 | if( pColumn && j>=pColumn->nId ){ |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 271 | sqliteVdbeAddOp(v, OP_String, 0, 0); |
| 272 | sqliteVdbeChangeP3(v, -1, pTab->aCol[idx].zDflt, P3_STATIC); |
drh | 5974a30 | 2000-06-07 14:42:26 +0000 | [diff] [blame] | 273 | }else if( srcTab>=0 ){ |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 274 | sqliteVdbeAddOp(v, OP_Column, srcTab, idx); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 275 | }else{ |
| 276 | sqliteExprCode(pParse, pList->a[j].pExpr); |
| 277 | } |
| 278 | } |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 279 | sqliteVdbeAddOp(v, OP_MakeIdxKey, pIdx->nColumn, 0); |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 280 | sqliteVdbeAddOp(v, OP_IdxPut, idx+base, pIdx->isUnique); |
drh | 5974a30 | 2000-06-07 14:42:26 +0000 | [diff] [blame] | 281 | } |
drh | 1ccde15 | 2000-06-17 13:12:39 +0000 | [diff] [blame] | 282 | |
drh | 1bee3d7 | 2001-10-15 00:44:35 +0000 | [diff] [blame] | 283 | |
| 284 | /* If inserting from a SELECT, keep a count of the number of |
| 285 | ** rows inserted. |
| 286 | */ |
| 287 | if( srcTab>=0 && (db->flags & SQLITE_CountRows)!=0 ){ |
| 288 | sqliteVdbeAddOp(v, OP_AddImm, 1, 0); |
| 289 | } |
| 290 | |
drh | 1ccde15 | 2000-06-17 13:12:39 +0000 | [diff] [blame] | 291 | /* The bottom of the loop, if the data source is a SELECT statement |
| 292 | */ |
drh | 5974a30 | 2000-06-07 14:42:26 +0000 | [diff] [blame] | 293 | if( srcTab>=0 ){ |
drh | 6b56344 | 2001-11-07 16:48:26 +0000 | [diff] [blame] | 294 | sqliteVdbeAddOp(v, OP_Next, srcTab, iCont); |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 295 | sqliteVdbeResolveLabel(v, iBreak); |
drh | 6b56344 | 2001-11-07 16:48:26 +0000 | [diff] [blame] | 296 | sqliteVdbeAddOp(v, OP_Close, srcTab, 0); |
| 297 | } |
| 298 | sqliteVdbeAddOp(v, OP_Close, base, 0); |
| 299 | for(idx=1, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, idx++){ |
| 300 | sqliteVdbeAddOp(v, OP_Close, idx+base, 0); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 301 | } |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 302 | if( (db->flags & SQLITE_InTrans)==0 ){ |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 303 | sqliteVdbeAddOp(v, OP_Commit, 0, 0); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 304 | } |
| 305 | |
drh | 1bee3d7 | 2001-10-15 00:44:35 +0000 | [diff] [blame] | 306 | /* |
| 307 | ** Return the number of rows inserted. |
| 308 | */ |
| 309 | if( db->flags & SQLITE_CountRows ){ |
| 310 | sqliteVdbeAddOp(v, OP_ColumnCount, 1, 0); |
| 311 | sqliteVdbeAddOp(v, OP_ColumnName, 0, 0); |
| 312 | sqliteVdbeChangeP3(v, -1, "rows inserted", P3_STATIC); |
| 313 | if( srcTab<0 ){ |
| 314 | sqliteVdbeAddOp(v, OP_Integer, 1, 0); |
| 315 | } |
| 316 | sqliteVdbeAddOp(v, OP_Callback, 1, 0); |
| 317 | } |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 318 | |
| 319 | insert_cleanup: |
drh | 5974a30 | 2000-06-07 14:42:26 +0000 | [diff] [blame] | 320 | if( pList ) sqliteExprListDelete(pList); |
| 321 | if( pSelect ) sqliteSelectDelete(pSelect); |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 322 | sqliteIdListDelete(pColumn); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 323 | } |