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