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