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