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