| OLD | NEW |
| 1 /* | 1 /* |
| 2 ** 2001 September 15 | 2 ** 2001 September 15 |
| 3 ** | 3 ** |
| 4 ** The author disclaims copyright to this source code. In place of | 4 ** The author disclaims copyright to this source code. In place of |
| 5 ** a legal notice, here is a blessing: | 5 ** a legal notice, here is a blessing: |
| 6 ** | 6 ** |
| 7 ** May you do good and not evil. | 7 ** May you do good and not evil. |
| 8 ** May you find forgiveness for yourself and forgive others. | 8 ** May you find forgiveness for yourself and forgive others. |
| 9 ** May you share freely, never taking more than you give. | 9 ** May you share freely, never taking more than you give. |
| 10 ** | 10 ** |
| 11 ************************************************************************* | 11 ************************************************************************* |
| 12 ** This file contains C code routines that are called by the parser | 12 ** This file contains C code routines that are called by the parser |
| 13 ** to handle INSERT statements in SQLite. | 13 ** to handle INSERT statements in SQLite. |
| 14 ** | |
| 15 ** $Id: insert.c,v 1.270 2009/07/24 17:58:53 danielk1977 Exp $ | |
| 16 */ | 14 */ |
| 17 #include "sqliteInt.h" | 15 #include "sqliteInt.h" |
| 18 | 16 |
| 19 /* | 17 /* |
| 20 ** Generate code that will open a table for reading. | 18 ** Generate code that will open a table for reading. |
| 21 */ | 19 */ |
| 22 void sqlite3OpenTable( | 20 void sqlite3OpenTable( |
| 23 Parse *p, /* Generate code into this VDBE */ | 21 Parse *p, /* Generate code into this VDBE */ |
| 24 int iCur, /* The cursor number of the table */ | 22 int iCur, /* The cursor number of the table */ |
| 25 int iDb, /* The database index in sqlite3.aDb[] */ | 23 int iDb, /* The database index in sqlite3.aDb[] */ |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 62 ** required, it is allocated and populated here. It is then stored as | 60 ** required, it is allocated and populated here. It is then stored as |
| 63 ** a member of the Index structure for subsequent use. | 61 ** a member of the Index structure for subsequent use. |
| 64 ** | 62 ** |
| 65 ** The column affinity string will eventually be deleted by | 63 ** The column affinity string will eventually be deleted by |
| 66 ** sqliteDeleteIndex() when the Index structure itself is cleaned | 64 ** sqliteDeleteIndex() when the Index structure itself is cleaned |
| 67 ** up. | 65 ** up. |
| 68 */ | 66 */ |
| 69 int n; | 67 int n; |
| 70 Table *pTab = pIdx->pTable; | 68 Table *pTab = pIdx->pTable; |
| 71 sqlite3 *db = sqlite3VdbeDb(v); | 69 sqlite3 *db = sqlite3VdbeDb(v); |
| 72 pIdx->zColAff = (char *)sqlite3Malloc(pIdx->nColumn+2); | 70 pIdx->zColAff = (char *)sqlite3DbMallocRaw(0, pIdx->nColumn+2); |
| 73 if( !pIdx->zColAff ){ | 71 if( !pIdx->zColAff ){ |
| 74 db->mallocFailed = 1; | 72 db->mallocFailed = 1; |
| 75 return 0; | 73 return 0; |
| 76 } | 74 } |
| 77 for(n=0; n<pIdx->nColumn; n++){ | 75 for(n=0; n<pIdx->nColumn; n++){ |
| 78 pIdx->zColAff[n] = pTab->aCol[pIdx->aiColumn[n]].affinity; | 76 pIdx->zColAff[n] = pTab->aCol[pIdx->aiColumn[n]].affinity; |
| 79 } | 77 } |
| 80 pIdx->zColAff[n++] = SQLITE_AFF_NONE; | 78 pIdx->zColAff[n++] = SQLITE_AFF_NONE; |
| 81 pIdx->zColAff[n] = 0; | 79 pIdx->zColAff[n] = 0; |
| 82 } | 80 } |
| (...skipping 21 matching lines...) Expand all Loading... |
| 104 ** stored as a member of the Table structure for subsequent use. | 102 ** stored as a member of the Table structure for subsequent use. |
| 105 ** | 103 ** |
| 106 ** The column affinity string will eventually be deleted by | 104 ** The column affinity string will eventually be deleted by |
| 107 ** sqlite3DeleteTable() when the Table structure itself is cleaned up. | 105 ** sqlite3DeleteTable() when the Table structure itself is cleaned up. |
| 108 */ | 106 */ |
| 109 if( !pTab->zColAff ){ | 107 if( !pTab->zColAff ){ |
| 110 char *zColAff; | 108 char *zColAff; |
| 111 int i; | 109 int i; |
| 112 sqlite3 *db = sqlite3VdbeDb(v); | 110 sqlite3 *db = sqlite3VdbeDb(v); |
| 113 | 111 |
| 114 zColAff = (char *)sqlite3Malloc(pTab->nCol+1); | 112 zColAff = (char *)sqlite3DbMallocRaw(0, pTab->nCol+1); |
| 115 if( !zColAff ){ | 113 if( !zColAff ){ |
| 116 db->mallocFailed = 1; | 114 db->mallocFailed = 1; |
| 117 return; | 115 return; |
| 118 } | 116 } |
| 119 | 117 |
| 120 for(i=0; i<pTab->nCol; i++){ | 118 for(i=0; i<pTab->nCol; i++){ |
| 121 zColAff[i] = pTab->aCol[i].affinity; | 119 zColAff[i] = pTab->aCol[i].affinity; |
| 122 } | 120 } |
| 123 zColAff[pTab->nCol] = '\0'; | 121 zColAff[pTab->nCol] = '\0'; |
| 124 | 122 |
| (...skipping 597 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 722 } | 720 } |
| 723 break; | 721 break; |
| 724 } | 722 } |
| 725 } | 723 } |
| 726 if( j>=pTab->nCol ){ | 724 if( j>=pTab->nCol ){ |
| 727 if( sqlite3IsRowid(pColumn->a[i].zName) ){ | 725 if( sqlite3IsRowid(pColumn->a[i].zName) ){ |
| 728 keyColumn = i; | 726 keyColumn = i; |
| 729 }else{ | 727 }else{ |
| 730 sqlite3ErrorMsg(pParse, "table %S has no column named %s", | 728 sqlite3ErrorMsg(pParse, "table %S has no column named %s", |
| 731 pTabList, 0, pColumn->a[i].zName); | 729 pTabList, 0, pColumn->a[i].zName); |
| 732 pParse->nErr++; | 730 pParse->checkSchema = 1; |
| 733 goto insert_cleanup; | 731 goto insert_cleanup; |
| 734 } | 732 } |
| 735 } | 733 } |
| 736 } | 734 } |
| 737 } | 735 } |
| 738 | 736 |
| 739 /* If there is no IDLIST term but the table has an integer primary | 737 /* If there is no IDLIST term but the table has an integer primary |
| 740 ** key, the set the keyColumn variable to the primary key column index | 738 ** key, the set the keyColumn variable to the primary key column index |
| 741 ** in the original table definition. | 739 ** in the original table definition. |
| 742 */ | 740 */ |
| (...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 841 /* Create the new column data | 839 /* Create the new column data |
| 842 */ | 840 */ |
| 843 for(i=0; i<pTab->nCol; i++){ | 841 for(i=0; i<pTab->nCol; i++){ |
| 844 if( pColumn==0 ){ | 842 if( pColumn==0 ){ |
| 845 j = i; | 843 j = i; |
| 846 }else{ | 844 }else{ |
| 847 for(j=0; j<pColumn->nId; j++){ | 845 for(j=0; j<pColumn->nId; j++){ |
| 848 if( pColumn->a[j].idx==i ) break; | 846 if( pColumn->a[j].idx==i ) break; |
| 849 } | 847 } |
| 850 } | 848 } |
| 851 if( pColumn && j>=pColumn->nId ){ | 849 if( (!useTempTable && !pList) || (pColumn && j>=pColumn->nId) ){ |
| 852 sqlite3ExprCode(pParse, pTab->aCol[i].pDflt, regCols+i+1); | 850 sqlite3ExprCode(pParse, pTab->aCol[i].pDflt, regCols+i+1); |
| 853 }else if( useTempTable ){ | 851 }else if( useTempTable ){ |
| 854 sqlite3VdbeAddOp3(v, OP_Column, srcTab, j, regCols+i+1); | 852 sqlite3VdbeAddOp3(v, OP_Column, srcTab, j, regCols+i+1); |
| 855 }else{ | 853 }else{ |
| 856 assert( pSelect==0 ); /* Otherwise useTempTable is true */ | 854 assert( pSelect==0 ); /* Otherwise useTempTable is true */ |
| 857 sqlite3ExprCodeAndCache(pParse, pList->a[j].pExpr, regCols+i+1); | 855 sqlite3ExprCodeAndCache(pParse, pList->a[j].pExpr, regCols+i+1); |
| 858 } | 856 } |
| 859 } | 857 } |
| 860 | 858 |
| 861 /* If this is an INSERT on a view with an INSTEAD OF INSERT trigger, | 859 /* If this is an INSERT on a view with an INSTEAD OF INSERT trigger, |
| 862 ** do not attempt any conversions before assembling the record. | 860 ** do not attempt any conversions before assembling the record. |
| 863 ** If this is a real table, attempt conversions as required by the | 861 ** If this is a real table, attempt conversions as required by the |
| 864 ** table column affinities. | 862 ** table column affinities. |
| 865 */ | 863 */ |
| 866 if( !isView ){ | 864 if( !isView ){ |
| 867 sqlite3VdbeAddOp2(v, OP_Affinity, regCols+1, pTab->nCol); | 865 sqlite3VdbeAddOp2(v, OP_Affinity, regCols+1, pTab->nCol); |
| 868 sqlite3TableAffinityStr(v, pTab); | 866 sqlite3TableAffinityStr(v, pTab); |
| 869 } | 867 } |
| 870 | 868 |
| 871 /* Fire BEFORE or INSTEAD OF triggers */ | 869 /* Fire BEFORE or INSTEAD OF triggers */ |
| 872 sqlite3CodeRowTrigger(pParse, pTrigger, TK_INSERT, 0, TRIGGER_BEFORE, | 870 sqlite3CodeRowTrigger(pParse, pTrigger, TK_INSERT, 0, TRIGGER_BEFORE, |
| 873 pTab, -1, regCols-pTab->nCol-1, onError, endOfLoop); | 871 pTab, regCols-pTab->nCol-1, onError, endOfLoop); |
| 874 | 872 |
| 875 sqlite3ReleaseTempRange(pParse, regCols, pTab->nCol+1); | 873 sqlite3ReleaseTempRange(pParse, regCols, pTab->nCol+1); |
| 876 } | 874 } |
| 877 | 875 |
| 878 /* Push the record number for the new entry onto the stack. The | 876 /* Push the record number for the new entry onto the stack. The |
| 879 ** record number is a randomly generate integer created by NewRowid | 877 ** record number is a randomly generate integer created by NewRowid |
| 880 ** except when the table has an INTEGER PRIMARY KEY column, in which | 878 ** except when the table has an INTEGER PRIMARY KEY column, in which |
| 881 ** case the record number is the same as that column. | 879 ** case the record number is the same as that column. |
| 882 */ | 880 */ |
| 883 if( !isView ){ | 881 if( !isView ){ |
| (...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 972 sqlite3VtabMakeWritable(pParse, pTab); | 970 sqlite3VtabMakeWritable(pParse, pTab); |
| 973 sqlite3VdbeAddOp4(v, OP_VUpdate, 1, pTab->nCol+2, regIns, pVTab, P4_VTAB); | 971 sqlite3VdbeAddOp4(v, OP_VUpdate, 1, pTab->nCol+2, regIns, pVTab, P4_VTAB); |
| 974 sqlite3MayAbort(pParse); | 972 sqlite3MayAbort(pParse); |
| 975 }else | 973 }else |
| 976 #endif | 974 #endif |
| 977 { | 975 { |
| 978 int isReplace; /* Set to true if constraints may cause a replace */ | 976 int isReplace; /* Set to true if constraints may cause a replace */ |
| 979 sqlite3GenerateConstraintChecks(pParse, pTab, baseCur, regIns, aRegIdx, | 977 sqlite3GenerateConstraintChecks(pParse, pTab, baseCur, regIns, aRegIdx, |
| 980 keyColumn>=0, 0, onError, endOfLoop, &isReplace | 978 keyColumn>=0, 0, onError, endOfLoop, &isReplace |
| 981 ); | 979 ); |
| 980 sqlite3FkCheck(pParse, pTab, 0, regIns); |
| 982 sqlite3CompleteInsertion( | 981 sqlite3CompleteInsertion( |
| 983 pParse, pTab, baseCur, regIns, aRegIdx, 0, appendFlag, isReplace==0 | 982 pParse, pTab, baseCur, regIns, aRegIdx, 0, appendFlag, isReplace==0 |
| 984 ); | 983 ); |
| 985 } | 984 } |
| 986 } | 985 } |
| 987 | 986 |
| 988 /* Update the count of rows that are inserted | 987 /* Update the count of rows that are inserted |
| 989 */ | 988 */ |
| 990 if( (db->flags & SQLITE_CountRows)!=0 ){ | 989 if( (db->flags & SQLITE_CountRows)!=0 ){ |
| 991 sqlite3VdbeAddOp2(v, OP_AddImm, regRowCount, 1); | 990 sqlite3VdbeAddOp2(v, OP_AddImm, regRowCount, 1); |
| 992 } | 991 } |
| 993 | 992 |
| 994 if( pTrigger ){ | 993 if( pTrigger ){ |
| 995 /* Code AFTER triggers */ | 994 /* Code AFTER triggers */ |
| 996 sqlite3CodeRowTrigger(pParse, pTrigger, TK_INSERT, 0, TRIGGER_AFTER, | 995 sqlite3CodeRowTrigger(pParse, pTrigger, TK_INSERT, 0, TRIGGER_AFTER, |
| 997 pTab, -1, regData-2-pTab->nCol, onError, endOfLoop); | 996 pTab, regData-2-pTab->nCol, onError, endOfLoop); |
| 998 } | 997 } |
| 999 | 998 |
| 1000 /* The bottom of the main insertion loop, if the data source | 999 /* The bottom of the main insertion loop, if the data source |
| 1001 ** is a SELECT statement. | 1000 ** is a SELECT statement. |
| 1002 */ | 1001 */ |
| 1003 sqlite3VdbeResolveLabel(v, endOfLoop); | 1002 sqlite3VdbeResolveLabel(v, endOfLoop); |
| 1004 if( useTempTable ){ | 1003 if( useTempTable ){ |
| 1005 sqlite3VdbeAddOp2(v, OP_Next, srcTab, addrCont); | 1004 sqlite3VdbeAddOp2(v, OP_Next, srcTab, addrCont); |
| 1006 sqlite3VdbeJumpHere(v, addrInsTop); | 1005 sqlite3VdbeJumpHere(v, addrInsTop); |
| 1007 sqlite3VdbeAddOp1(v, OP_Close, srcTab); | 1006 sqlite3VdbeAddOp1(v, OP_Close, srcTab); |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1039 } | 1038 } |
| 1040 | 1039 |
| 1041 insert_cleanup: | 1040 insert_cleanup: |
| 1042 sqlite3SrcListDelete(db, pTabList); | 1041 sqlite3SrcListDelete(db, pTabList); |
| 1043 sqlite3ExprListDelete(db, pList); | 1042 sqlite3ExprListDelete(db, pList); |
| 1044 sqlite3SelectDelete(db, pSelect); | 1043 sqlite3SelectDelete(db, pSelect); |
| 1045 sqlite3IdListDelete(db, pColumn); | 1044 sqlite3IdListDelete(db, pColumn); |
| 1046 sqlite3DbFree(db, aRegIdx); | 1045 sqlite3DbFree(db, aRegIdx); |
| 1047 } | 1046 } |
| 1048 | 1047 |
| 1048 /* Make sure "isView" and other macros defined above are undefined. Otherwise |
| 1049 ** thely may interfere with compilation of other functions in this file |
| 1050 ** (or in another file, if this file becomes part of the amalgamation). */ |
| 1051 #ifdef isView |
| 1052 #undef isView |
| 1053 #endif |
| 1054 #ifdef pTrigger |
| 1055 #undef pTrigger |
| 1056 #endif |
| 1057 #ifdef tmask |
| 1058 #undef tmask |
| 1059 #endif |
| 1060 |
| 1061 |
| 1049 /* | 1062 /* |
| 1050 ** Generate code to do constraint checks prior to an INSERT or an UPDATE. | 1063 ** Generate code to do constraint checks prior to an INSERT or an UPDATE. |
| 1051 ** | 1064 ** |
| 1052 ** The input is a range of consecutive registers as follows: | 1065 ** The input is a range of consecutive registers as follows: |
| 1053 ** | 1066 ** |
| 1054 ** 1. The rowid of the row after the update. | 1067 ** 1. The rowid of the row after the update. |
| 1055 ** | 1068 ** |
| 1056 ** 2. The data in the first column of the entry after the update. | 1069 ** 2. The data in the first column of the entry after the update. |
| 1057 ** | 1070 ** |
| 1058 ** i. Data from middle columns... | 1071 ** i. Data from middle columns... |
| (...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1200 */ | 1213 */ |
| 1201 #ifndef SQLITE_OMIT_CHECK | 1214 #ifndef SQLITE_OMIT_CHECK |
| 1202 if( pTab->pCheck && (pParse->db->flags & SQLITE_IgnoreChecks)==0 ){ | 1215 if( pTab->pCheck && (pParse->db->flags & SQLITE_IgnoreChecks)==0 ){ |
| 1203 int allOk = sqlite3VdbeMakeLabel(v); | 1216 int allOk = sqlite3VdbeMakeLabel(v); |
| 1204 pParse->ckBase = regData; | 1217 pParse->ckBase = regData; |
| 1205 sqlite3ExprIfTrue(pParse, pTab->pCheck, allOk, SQLITE_JUMPIFNULL); | 1218 sqlite3ExprIfTrue(pParse, pTab->pCheck, allOk, SQLITE_JUMPIFNULL); |
| 1206 onError = overrideError!=OE_Default ? overrideError : OE_Abort; | 1219 onError = overrideError!=OE_Default ? overrideError : OE_Abort; |
| 1207 if( onError==OE_Ignore ){ | 1220 if( onError==OE_Ignore ){ |
| 1208 sqlite3VdbeAddOp2(v, OP_Goto, 0, ignoreDest); | 1221 sqlite3VdbeAddOp2(v, OP_Goto, 0, ignoreDest); |
| 1209 }else{ | 1222 }else{ |
| 1223 if( onError==OE_Replace ) onError = OE_Abort; /* IMP: R-15569-63625 */ |
| 1210 sqlite3HaltConstraint(pParse, onError, 0, 0); | 1224 sqlite3HaltConstraint(pParse, onError, 0, 0); |
| 1211 } | 1225 } |
| 1212 sqlite3VdbeResolveLabel(v, allOk); | 1226 sqlite3VdbeResolveLabel(v, allOk); |
| 1213 } | 1227 } |
| 1214 #endif /* !defined(SQLITE_OMIT_CHECK) */ | 1228 #endif /* !defined(SQLITE_OMIT_CHECK) */ |
| 1215 | 1229 |
| 1216 /* If we have an INTEGER PRIMARY KEY, make sure the primary key | 1230 /* If we have an INTEGER PRIMARY KEY, make sure the primary key |
| 1217 ** of the new record does not previously exist. Except, if this | 1231 ** of the new record does not previously exist. Except, if this |
| 1218 ** is an UPDATE and the primary key is not changing, that is OK. | 1232 ** is an UPDATE and the primary key is not changing, that is OK. |
| 1219 */ | 1233 */ |
| 1220 if( rowidChng ){ | 1234 if( rowidChng ){ |
| 1221 onError = pTab->keyConf; | 1235 onError = pTab->keyConf; |
| 1222 if( overrideError!=OE_Default ){ | 1236 if( overrideError!=OE_Default ){ |
| 1223 onError = overrideError; | 1237 onError = overrideError; |
| 1224 }else if( onError==OE_Default ){ | 1238 }else if( onError==OE_Default ){ |
| 1225 onError = OE_Abort; | 1239 onError = OE_Abort; |
| 1226 } | 1240 } |
| 1227 | 1241 |
| 1228 if( onError!=OE_Replace || pTab->pIndex ){ | 1242 if( isUpdate ){ |
| 1229 if( isUpdate ){ | 1243 j2 = sqlite3VdbeAddOp3(v, OP_Eq, regRowid, 0, rowidChng); |
| 1230 j2 = sqlite3VdbeAddOp3(v, OP_Eq, regRowid, 0, rowidChng); | 1244 } |
| 1245 j3 = sqlite3VdbeAddOp3(v, OP_NotExists, baseCur, 0, regRowid); |
| 1246 switch( onError ){ |
| 1247 default: { |
| 1248 onError = OE_Abort; |
| 1249 /* Fall thru into the next case */ |
| 1231 } | 1250 } |
| 1232 j3 = sqlite3VdbeAddOp3(v, OP_NotExists, baseCur, 0, regRowid); | 1251 case OE_Rollback: |
| 1233 switch( onError ){ | 1252 case OE_Abort: |
| 1234 default: { | 1253 case OE_Fail: { |
| 1235 onError = OE_Abort; | 1254 sqlite3HaltConstraint( |
| 1236 /* Fall thru into the next case */ | 1255 pParse, onError, "PRIMARY KEY must be unique", P4_STATIC); |
| 1256 break; |
| 1257 } |
| 1258 case OE_Replace: { |
| 1259 /* If there are DELETE triggers on this table and the |
| 1260 ** recursive-triggers flag is set, call GenerateRowDelete() to |
| 1261 ** remove the conflicting row from the the table. This will fire |
| 1262 ** the triggers and remove both the table and index b-tree entries. |
| 1263 ** |
| 1264 ** Otherwise, if there are no triggers or the recursive-triggers |
| 1265 ** flag is not set, but the table has one or more indexes, call |
| 1266 ** GenerateRowIndexDelete(). This removes the index b-tree entries |
| 1267 ** only. The table b-tree entry will be replaced by the new entry |
| 1268 ** when it is inserted. |
| 1269 ** |
| 1270 ** If either GenerateRowDelete() or GenerateRowIndexDelete() is called, |
| 1271 ** also invoke MultiWrite() to indicate that this VDBE may require |
| 1272 ** statement rollback (if the statement is aborted after the delete |
| 1273 ** takes place). Earlier versions called sqlite3MultiWrite() regardless, |
| 1274 ** but being more selective here allows statements like: |
| 1275 ** |
| 1276 ** REPLACE INTO t(rowid) VALUES($newrowid) |
| 1277 ** |
| 1278 ** to run without a statement journal if there are no indexes on the |
| 1279 ** table. |
| 1280 */ |
| 1281 Trigger *pTrigger = 0; |
| 1282 if( pParse->db->flags&SQLITE_RecTriggers ){ |
| 1283 pTrigger = sqlite3TriggersExist(pParse, pTab, TK_DELETE, 0, 0); |
| 1237 } | 1284 } |
| 1238 case OE_Rollback: | 1285 if( pTrigger || sqlite3FkRequired(pParse, pTab, 0, 0) ){ |
| 1239 case OE_Abort: | 1286 sqlite3MultiWrite(pParse); |
| 1240 case OE_Fail: { | 1287 sqlite3GenerateRowDelete( |
| 1241 sqlite3HaltConstraint( | 1288 pParse, pTab, baseCur, regRowid, 0, pTrigger, OE_Replace |
| 1242 pParse, onError, "PRIMARY KEY must be unique", P4_STATIC); | 1289 ); |
| 1243 break; | 1290 }else if( pTab->pIndex ){ |
| 1291 sqlite3MultiWrite(pParse); |
| 1292 sqlite3GenerateRowIndexDelete(pParse, pTab, baseCur, 0); |
| 1244 } | 1293 } |
| 1245 case OE_Replace: { | 1294 seenReplace = 1; |
| 1246 /* If there are DELETE triggers on this table and the | 1295 break; |
| 1247 ** recursive-triggers flag is set, call GenerateRowDelete() to | |
| 1248 ** remove the conflicting row from the the table. This will fire | |
| 1249 ** the triggers and remove both the table and index b-tree entries. | |
| 1250 ** | |
| 1251 ** Otherwise, if there are no triggers or the recursive-triggers | |
| 1252 ** flag is not set, call GenerateRowIndexDelete(). This removes | |
| 1253 ** the index b-tree entries only. The table b-tree entry will be | |
| 1254 ** replaced by the new entry when it is inserted. */ | |
| 1255 Trigger *pTrigger = 0; | |
| 1256 if( pParse->db->flags&SQLITE_RecTriggers ){ | |
| 1257 pTrigger = sqlite3TriggersExist(pParse, pTab, TK_DELETE, 0, 0); | |
| 1258 } | |
| 1259 if( pTrigger ){ | |
| 1260 sqlite3GenerateRowDelete( | |
| 1261 pParse, pTab, baseCur, regRowid, 0, pTrigger, OE_Replace | |
| 1262 ); | |
| 1263 }else{ | |
| 1264 sqlite3GenerateRowIndexDelete(pParse, pTab, baseCur, 0); | |
| 1265 } | |
| 1266 seenReplace = 1; | |
| 1267 break; | |
| 1268 } | |
| 1269 case OE_Ignore: { | |
| 1270 assert( seenReplace==0 ); | |
| 1271 sqlite3VdbeAddOp2(v, OP_Goto, 0, ignoreDest); | |
| 1272 break; | |
| 1273 } | |
| 1274 } | 1296 } |
| 1275 sqlite3VdbeJumpHere(v, j3); | 1297 case OE_Ignore: { |
| 1276 if( isUpdate ){ | 1298 assert( seenReplace==0 ); |
| 1277 sqlite3VdbeJumpHere(v, j2); | 1299 sqlite3VdbeAddOp2(v, OP_Goto, 0, ignoreDest); |
| 1300 break; |
| 1278 } | 1301 } |
| 1279 } | 1302 } |
| 1303 sqlite3VdbeJumpHere(v, j3); |
| 1304 if( isUpdate ){ |
| 1305 sqlite3VdbeJumpHere(v, j2); |
| 1306 } |
| 1280 } | 1307 } |
| 1281 | 1308 |
| 1282 /* Test all UNIQUE constraints by creating entries for each UNIQUE | 1309 /* Test all UNIQUE constraints by creating entries for each UNIQUE |
| 1283 ** index and making sure that duplicate entries do not already exist. | 1310 ** index and making sure that duplicate entries do not already exist. |
| 1284 ** Add the new records to the indices as we go. | 1311 ** Add the new records to the indices as we go. |
| 1285 */ | 1312 */ |
| 1286 for(iCur=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, iCur++){ | 1313 for(iCur=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, iCur++){ |
| 1287 int regIdx; | 1314 int regIdx; |
| 1288 int regR; | 1315 int regR; |
| 1289 | 1316 |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1357 break; | 1384 break; |
| 1358 } | 1385 } |
| 1359 case OE_Ignore: { | 1386 case OE_Ignore: { |
| 1360 assert( seenReplace==0 ); | 1387 assert( seenReplace==0 ); |
| 1361 sqlite3VdbeAddOp2(v, OP_Goto, 0, ignoreDest); | 1388 sqlite3VdbeAddOp2(v, OP_Goto, 0, ignoreDest); |
| 1362 break; | 1389 break; |
| 1363 } | 1390 } |
| 1364 default: { | 1391 default: { |
| 1365 Trigger *pTrigger = 0; | 1392 Trigger *pTrigger = 0; |
| 1366 assert( onError==OE_Replace ); | 1393 assert( onError==OE_Replace ); |
| 1394 sqlite3MultiWrite(pParse); |
| 1367 if( pParse->db->flags&SQLITE_RecTriggers ){ | 1395 if( pParse->db->flags&SQLITE_RecTriggers ){ |
| 1368 pTrigger = sqlite3TriggersExist(pParse, pTab, TK_DELETE, 0, 0); | 1396 pTrigger = sqlite3TriggersExist(pParse, pTab, TK_DELETE, 0, 0); |
| 1369 } | 1397 } |
| 1370 sqlite3GenerateRowDelete( | 1398 sqlite3GenerateRowDelete( |
| 1371 pParse, pTab, baseCur, regR, 0, pTrigger, OE_Replace | 1399 pParse, pTab, baseCur, regR, 0, pTrigger, OE_Replace |
| 1372 ); | 1400 ); |
| 1373 seenReplace = 1; | 1401 seenReplace = 1; |
| 1374 break; | 1402 break; |
| 1375 } | 1403 } |
| 1376 } | 1404 } |
| 1377 sqlite3VdbeJumpHere(v, j3); | 1405 sqlite3VdbeJumpHere(v, j3); |
| 1378 sqlite3ReleaseTempReg(pParse, regR); | 1406 sqlite3ReleaseTempReg(pParse, regR); |
| 1379 } | 1407 } |
| 1380 | 1408 |
| 1381 if( pbMayReplace ){ | 1409 if( pbMayReplace ){ |
| 1382 *pbMayReplace = seenReplace; | 1410 *pbMayReplace = seenReplace; |
| 1383 } | 1411 } |
| 1384 } | 1412 } |
| 1385 | 1413 |
| 1386 /* | 1414 /* |
| 1387 ** This routine generates code to finish the INSERT or UPDATE operation | 1415 ** This routine generates code to finish the INSERT or UPDATE operation |
| 1388 ** that was started by a prior call to sqlite3GenerateConstraintChecks. | 1416 ** that was started by a prior call to sqlite3GenerateConstraintChecks. |
| 1389 ** A consecutive range of registers starting at regRowid contains the | 1417 ** A consecutive range of registers starting at regRowid contains the |
| 1390 ** rowid and the content to be inserted. | 1418 ** rowid and the content to be inserted. |
| (...skipping 304 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1695 destHasUniqueIdx = 1; | 1723 destHasUniqueIdx = 1; |
| 1696 } | 1724 } |
| 1697 for(pSrcIdx=pSrc->pIndex; pSrcIdx; pSrcIdx=pSrcIdx->pNext){ | 1725 for(pSrcIdx=pSrc->pIndex; pSrcIdx; pSrcIdx=pSrcIdx->pNext){ |
| 1698 if( xferCompatibleIndex(pDestIdx, pSrcIdx) ) break; | 1726 if( xferCompatibleIndex(pDestIdx, pSrcIdx) ) break; |
| 1699 } | 1727 } |
| 1700 if( pSrcIdx==0 ){ | 1728 if( pSrcIdx==0 ){ |
| 1701 return 0; /* pDestIdx has no corresponding index in pSrc */ | 1729 return 0; /* pDestIdx has no corresponding index in pSrc */ |
| 1702 } | 1730 } |
| 1703 } | 1731 } |
| 1704 #ifndef SQLITE_OMIT_CHECK | 1732 #ifndef SQLITE_OMIT_CHECK |
| 1705 if( pDest->pCheck && !sqlite3ExprCompare(pSrc->pCheck, pDest->pCheck) ){ | 1733 if( pDest->pCheck && sqlite3ExprCompare(pSrc->pCheck, pDest->pCheck) ){ |
| 1706 return 0; /* Tables have different CHECK constraints. Ticket #2252 */ | 1734 return 0; /* Tables have different CHECK constraints. Ticket #2252 */ |
| 1707 } | 1735 } |
| 1708 #endif | 1736 #endif |
| 1709 | 1737 |
| 1710 /* If we get this far, it means either: | 1738 /* If we get this far, it means either: |
| 1711 ** | 1739 ** |
| 1712 ** * We can always do the transfer if the table contains an | 1740 ** * We can always do the transfer if the table contains an |
| 1713 ** an integer primary key | 1741 ** an integer primary key |
| 1714 ** | 1742 ** |
| 1715 ** * We can conditionally do the transfer if the destination | 1743 ** * We can conditionally do the transfer if the destination |
| (...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1793 if( emptyDestTest ){ | 1821 if( emptyDestTest ){ |
| 1794 sqlite3VdbeAddOp2(v, OP_Halt, SQLITE_OK, 0); | 1822 sqlite3VdbeAddOp2(v, OP_Halt, SQLITE_OK, 0); |
| 1795 sqlite3VdbeJumpHere(v, emptyDestTest); | 1823 sqlite3VdbeJumpHere(v, emptyDestTest); |
| 1796 sqlite3VdbeAddOp2(v, OP_Close, iDest, 0); | 1824 sqlite3VdbeAddOp2(v, OP_Close, iDest, 0); |
| 1797 return 0; | 1825 return 0; |
| 1798 }else{ | 1826 }else{ |
| 1799 return 1; | 1827 return 1; |
| 1800 } | 1828 } |
| 1801 } | 1829 } |
| 1802 #endif /* SQLITE_OMIT_XFER_OPT */ | 1830 #endif /* SQLITE_OMIT_XFER_OPT */ |
| 1803 | |
| 1804 /* Make sure "isView" gets undefined in case this file becomes part of | |
| 1805 ** the amalgamation - so that subsequent files do not see isView as a | |
| 1806 ** macro. */ | |
| 1807 #undef isView | |
| OLD | NEW |