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