| 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 UPDATE statements. | 13 ** to handle UPDATE statements. |
| 14 */ | 14 */ |
| 15 #include "sqliteInt.h" | 15 #include "sqliteInt.h" |
| 16 | 16 |
| 17 #ifndef SQLITE_OMIT_VIRTUALTABLE | 17 #ifndef SQLITE_OMIT_VIRTUALTABLE |
| 18 /* Forward declaration */ | 18 /* Forward declaration */ |
| 19 static void updateVirtualTable( | 19 static void updateVirtualTable( |
| 20 Parse *pParse, /* The parsing context */ | 20 Parse *pParse, /* The parsing context */ |
| 21 SrcList *pSrc, /* The virtual table to be modified */ | 21 SrcList *pSrc, /* The virtual table to be modified */ |
| 22 Table *pTab, /* The virtual table */ | 22 Table *pTab, /* The virtual table */ |
| 23 ExprList *pChanges, /* The columns to change in the UPDATE statement */ | 23 ExprList *pChanges, /* The columns to change in the UPDATE statement */ |
| 24 Expr *pRowidExpr, /* Expression used to recompute the rowid */ | 24 Expr *pRowidExpr, /* Expression used to recompute the rowid */ |
| 25 int *aXRef, /* Mapping from columns of pTab to entries in pChanges */ | 25 int *aXRef, /* Mapping from columns of pTab to entries in pChanges */ |
| 26 Expr *pWhere /* WHERE clause of the UPDATE statement */ | 26 Expr *pWhere, /* WHERE clause of the UPDATE statement */ |
| 27 int onError /* ON CONFLICT strategy */ |
| 27 ); | 28 ); |
| 28 #endif /* SQLITE_OMIT_VIRTUALTABLE */ | 29 #endif /* SQLITE_OMIT_VIRTUALTABLE */ |
| 29 | 30 |
| 30 /* | 31 /* |
| 31 ** The most recently coded instruction was an OP_Column to retrieve the | 32 ** The most recently coded instruction was an OP_Column to retrieve the |
| 32 ** i-th column of table pTab. This routine sets the P4 parameter of the | 33 ** i-th column of table pTab. This routine sets the P4 parameter of the |
| 33 ** OP_Column to the default value, if any. | 34 ** OP_Column to the default value, if any. |
| 34 ** | 35 ** |
| 35 ** The default value of a column is specified by a DEFAULT clause in the | 36 ** The default value of a column is specified by a DEFAULT clause in the |
| 36 ** column definition. This was either supplied by the user when the table | 37 ** column definition. This was either supplied by the user when the table |
| (...skipping 16 matching lines...) Expand all Loading... |
| 53 ** sqlite3_value objects. | 54 ** sqlite3_value objects. |
| 54 ** | 55 ** |
| 55 ** If parameter iReg is not negative, code an OP_RealAffinity instruction | 56 ** If parameter iReg is not negative, code an OP_RealAffinity instruction |
| 56 ** on register iReg. This is used when an equivalent integer value is | 57 ** on register iReg. This is used when an equivalent integer value is |
| 57 ** stored in place of an 8-byte floating point value in order to save | 58 ** stored in place of an 8-byte floating point value in order to save |
| 58 ** space. | 59 ** space. |
| 59 */ | 60 */ |
| 60 void sqlite3ColumnDefault(Vdbe *v, Table *pTab, int i, int iReg){ | 61 void sqlite3ColumnDefault(Vdbe *v, Table *pTab, int i, int iReg){ |
| 61 assert( pTab!=0 ); | 62 assert( pTab!=0 ); |
| 62 if( !pTab->pSelect ){ | 63 if( !pTab->pSelect ){ |
| 63 sqlite3_value *pValue; | 64 sqlite3_value *pValue = 0; |
| 64 u8 enc = ENC(sqlite3VdbeDb(v)); | 65 u8 enc = ENC(sqlite3VdbeDb(v)); |
| 65 Column *pCol = &pTab->aCol[i]; | 66 Column *pCol = &pTab->aCol[i]; |
| 66 VdbeComment((v, "%s.%s", pTab->zName, pCol->zName)); | 67 VdbeComment((v, "%s.%s", pTab->zName, pCol->zName)); |
| 67 assert( i<pTab->nCol ); | 68 assert( i<pTab->nCol ); |
| 68 sqlite3ValueFromExpr(sqlite3VdbeDb(v), pCol->pDflt, enc, | 69 sqlite3ValueFromExpr(sqlite3VdbeDb(v), pCol->pDflt, enc, |
| 69 pCol->affinity, &pValue); | 70 pCol->affinity, &pValue); |
| 70 if( pValue ){ | 71 if( pValue ){ |
| 71 sqlite3VdbeChangeP4(v, -1, (const char *)pValue, P4_MEM); | 72 sqlite3VdbeChangeP4(v, -1, (const char *)pValue, P4_MEM); |
| 72 } | 73 } |
| 73 #ifndef SQLITE_OMIT_FLOATING_POINT | 74 #ifndef SQLITE_OMIT_FLOATING_POINT |
| 74 if( iReg>=0 && pTab->aCol[i].affinity==SQLITE_AFF_REAL ){ | 75 if( pTab->aCol[i].affinity==SQLITE_AFF_REAL ){ |
| 75 sqlite3VdbeAddOp1(v, OP_RealAffinity, iReg); | 76 sqlite3VdbeAddOp1(v, OP_RealAffinity, iReg); |
| 76 } | 77 } |
| 77 #endif | 78 #endif |
| 78 } | 79 } |
| 79 } | 80 } |
| 80 | 81 |
| 81 /* | 82 /* |
| 82 ** Process an UPDATE statement. | 83 ** Process an UPDATE statement. |
| 83 ** | 84 ** |
| 84 ** UPDATE OR IGNORE table_wxyz SET a=b, c=d WHERE e<5 AND f NOT NULL; | 85 ** UPDATE OR IGNORE table_wxyz SET a=b, c=d WHERE e<5 AND f NOT NULL; |
| 85 ** \_______/ \________/ \______/ \________________/ | 86 ** \_______/ \________/ \______/ \________________/ |
| 86 * onError pTabList pChanges pWhere | 87 * onError pTabList pChanges pWhere |
| 87 */ | 88 */ |
| 88 void sqlite3Update( | 89 void sqlite3Update( |
| 89 Parse *pParse, /* The parser context */ | 90 Parse *pParse, /* The parser context */ |
| 90 SrcList *pTabList, /* The table in which we should change things */ | 91 SrcList *pTabList, /* The table in which we should change things */ |
| 91 ExprList *pChanges, /* Things to be changed */ | 92 ExprList *pChanges, /* Things to be changed */ |
| 92 Expr *pWhere, /* The WHERE clause. May be null */ | 93 Expr *pWhere, /* The WHERE clause. May be null */ |
| 93 int onError /* How to handle constraint errors */ | 94 int onError /* How to handle constraint errors */ |
| 94 ){ | 95 ){ |
| 95 int i, j; /* Loop counters */ | 96 int i, j; /* Loop counters */ |
| 96 Table *pTab; /* The table to be updated */ | 97 Table *pTab; /* The table to be updated */ |
| 97 int addr = 0; /* VDBE instruction address of the start of the loop */ | 98 int addrTop = 0; /* VDBE instruction address of the start of the loop */ |
| 98 WhereInfo *pWInfo; /* Information about the WHERE clause */ | 99 WhereInfo *pWInfo; /* Information about the WHERE clause */ |
| 99 Vdbe *v; /* The virtual database engine */ | 100 Vdbe *v; /* The virtual database engine */ |
| 100 Index *pIdx; /* For looping over indices */ | 101 Index *pIdx; /* For looping over indices */ |
| 102 Index *pPk; /* The PRIMARY KEY index for WITHOUT ROWID tables */ |
| 101 int nIdx; /* Number of indices that need updating */ | 103 int nIdx; /* Number of indices that need updating */ |
| 102 int iCur; /* VDBE Cursor number of pTab */ | 104 int iBaseCur; /* Base cursor number */ |
| 105 int iDataCur; /* Cursor for the canonical data btree */ |
| 106 int iIdxCur; /* Cursor for the first index */ |
| 103 sqlite3 *db; /* The database structure */ | 107 sqlite3 *db; /* The database structure */ |
| 104 int *aRegIdx = 0; /* One register assigned to each index to be updated */ | 108 int *aRegIdx = 0; /* One register assigned to each index to be updated */ |
| 105 int *aXRef = 0; /* aXRef[i] is the index in pChanges->a[] of the | 109 int *aXRef = 0; /* aXRef[i] is the index in pChanges->a[] of the |
| 106 ** an expression for the i-th column of the table. | 110 ** an expression for the i-th column of the table. |
| 107 ** aXRef[i]==-1 if the i-th column is not changed. */ | 111 ** aXRef[i]==-1 if the i-th column is not changed. */ |
| 108 int chngRowid; /* True if the record number is being changed */ | 112 u8 *aToOpen; /* 1 for tables and indices to be opened */ |
| 113 u8 chngPk; /* PRIMARY KEY changed in a WITHOUT ROWID table */ |
| 114 u8 chngRowid; /* Rowid changed in a normal table */ |
| 115 u8 chngKey; /* Either chngPk or chngRowid */ |
| 109 Expr *pRowidExpr = 0; /* Expression defining the new record number */ | 116 Expr *pRowidExpr = 0; /* Expression defining the new record number */ |
| 110 int openAll = 0; /* True if all indices need to be opened */ | |
| 111 AuthContext sContext; /* The authorization context */ | 117 AuthContext sContext; /* The authorization context */ |
| 112 NameContext sNC; /* The name-context to resolve expressions in */ | 118 NameContext sNC; /* The name-context to resolve expressions in */ |
| 113 int iDb; /* Database containing the table being updated */ | 119 int iDb; /* Database containing the table being updated */ |
| 114 int okOnePass; /* True for one-pass algorithm without the FIFO */ | 120 int okOnePass; /* True for one-pass algorithm without the FIFO */ |
| 115 int hasFK; /* True if foreign key processing is required */ | 121 int hasFK; /* True if foreign key processing is required */ |
| 122 int labelBreak; /* Jump here to break out of UPDATE loop */ |
| 123 int labelContinue; /* Jump here to continue next step of UPDATE loop */ |
| 116 | 124 |
| 117 #ifndef SQLITE_OMIT_TRIGGER | 125 #ifndef SQLITE_OMIT_TRIGGER |
| 118 int isView; /* True when updating a view (INSTEAD OF trigger) */ | 126 int isView; /* True when updating a view (INSTEAD OF trigger) */ |
| 119 Trigger *pTrigger; /* List of triggers on pTab, if required */ | 127 Trigger *pTrigger; /* List of triggers on pTab, if required */ |
| 120 int tmask; /* Mask of TRIGGER_BEFORE|TRIGGER_AFTER */ | 128 int tmask; /* Mask of TRIGGER_BEFORE|TRIGGER_AFTER */ |
| 121 #endif | 129 #endif |
| 122 int newmask; /* Mask of NEW.* columns accessed by BEFORE triggers */ | 130 int newmask; /* Mask of NEW.* columns accessed by BEFORE triggers */ |
| 131 int iEph = 0; /* Ephemeral table holding all primary key values */ |
| 132 int nKey = 0; /* Number of elements in regKey for WITHOUT ROWID */ |
| 133 int aiCurOnePass[2]; /* The write cursors opened by WHERE_ONEPASS */ |
| 123 | 134 |
| 124 /* Register Allocations */ | 135 /* Register Allocations */ |
| 125 int regRowCount = 0; /* A count of rows changed */ | 136 int regRowCount = 0; /* A count of rows changed */ |
| 126 int regOldRowid; /* The old rowid */ | 137 int regOldRowid; /* The old rowid */ |
| 127 int regNewRowid; /* The new rowid */ | 138 int regNewRowid; /* The new rowid */ |
| 128 int regNew; | 139 int regNew; /* Content of the NEW.* table in triggers */ |
| 129 int regOld = 0; | 140 int regOld = 0; /* Content of OLD.* table in triggers */ |
| 130 int regRowSet = 0; /* Rowset of rows to be updated */ | 141 int regRowSet = 0; /* Rowset of rows to be updated */ |
| 142 int regKey = 0; /* composite PRIMARY KEY value */ |
| 131 | 143 |
| 132 memset(&sContext, 0, sizeof(sContext)); | 144 memset(&sContext, 0, sizeof(sContext)); |
| 133 db = pParse->db; | 145 db = pParse->db; |
| 134 if( pParse->nErr || db->mallocFailed ){ | 146 if( pParse->nErr || db->mallocFailed ){ |
| 135 goto update_cleanup; | 147 goto update_cleanup; |
| 136 } | 148 } |
| 137 assert( pTabList->nSrc==1 ); | 149 assert( pTabList->nSrc==1 ); |
| 138 | 150 |
| 139 /* Locate the table which we want to update. | 151 /* Locate the table which we want to update. |
| 140 */ | 152 */ |
| (...skipping 17 matching lines...) Expand all Loading... |
| 158 # undef isView | 170 # undef isView |
| 159 # define isView 0 | 171 # define isView 0 |
| 160 #endif | 172 #endif |
| 161 | 173 |
| 162 if( sqlite3ViewGetColumnNames(pParse, pTab) ){ | 174 if( sqlite3ViewGetColumnNames(pParse, pTab) ){ |
| 163 goto update_cleanup; | 175 goto update_cleanup; |
| 164 } | 176 } |
| 165 if( sqlite3IsReadOnly(pParse, pTab, tmask) ){ | 177 if( sqlite3IsReadOnly(pParse, pTab, tmask) ){ |
| 166 goto update_cleanup; | 178 goto update_cleanup; |
| 167 } | 179 } |
| 168 aXRef = sqlite3DbMallocRaw(db, sizeof(int) * pTab->nCol ); | |
| 169 if( aXRef==0 ) goto update_cleanup; | |
| 170 for(i=0; i<pTab->nCol; i++) aXRef[i] = -1; | |
| 171 | 180 |
| 172 /* Allocate a cursors for the main database table and for all indices. | 181 /* Allocate a cursors for the main database table and for all indices. |
| 173 ** The index cursors might not be used, but if they are used they | 182 ** The index cursors might not be used, but if they are used they |
| 174 ** need to occur right after the database cursor. So go ahead and | 183 ** need to occur right after the database cursor. So go ahead and |
| 175 ** allocate enough space, just in case. | 184 ** allocate enough space, just in case. |
| 176 */ | 185 */ |
| 177 pTabList->a[0].iCursor = iCur = pParse->nTab++; | 186 pTabList->a[0].iCursor = iBaseCur = iDataCur = pParse->nTab++; |
| 178 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){ | 187 iIdxCur = iDataCur+1; |
| 188 pPk = HasRowid(pTab) ? 0 : sqlite3PrimaryKeyIndex(pTab); |
| 189 for(nIdx=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, nIdx++){ |
| 190 if( IsPrimaryKeyIndex(pIdx) && pPk!=0 ){ |
| 191 iDataCur = pParse->nTab; |
| 192 pTabList->a[0].iCursor = iDataCur; |
| 193 } |
| 179 pParse->nTab++; | 194 pParse->nTab++; |
| 180 } | 195 } |
| 181 | 196 |
| 197 /* Allocate space for aXRef[], aRegIdx[], and aToOpen[]. |
| 198 ** Initialize aXRef[] and aToOpen[] to their default values. |
| 199 */ |
| 200 aXRef = sqlite3DbMallocRaw(db, sizeof(int) * (pTab->nCol+nIdx) + nIdx+2 ); |
| 201 if( aXRef==0 ) goto update_cleanup; |
| 202 aRegIdx = aXRef+pTab->nCol; |
| 203 aToOpen = (u8*)(aRegIdx+nIdx); |
| 204 memset(aToOpen, 1, nIdx+1); |
| 205 aToOpen[nIdx+1] = 0; |
| 206 for(i=0; i<pTab->nCol; i++) aXRef[i] = -1; |
| 207 |
| 182 /* Initialize the name-context */ | 208 /* Initialize the name-context */ |
| 183 memset(&sNC, 0, sizeof(sNC)); | 209 memset(&sNC, 0, sizeof(sNC)); |
| 184 sNC.pParse = pParse; | 210 sNC.pParse = pParse; |
| 185 sNC.pSrcList = pTabList; | 211 sNC.pSrcList = pTabList; |
| 186 | 212 |
| 187 /* Resolve the column names in all the expressions of the | 213 /* Resolve the column names in all the expressions of the |
| 188 ** of the UPDATE statement. Also find the column index | 214 ** of the UPDATE statement. Also find the column index |
| 189 ** for each column to be updated in the pChanges array. For each | 215 ** for each column to be updated in the pChanges array. For each |
| 190 ** column to be updated, make sure we have authorization to change | 216 ** column to be updated, make sure we have authorization to change |
| 191 ** that column. | 217 ** that column. |
| 192 */ | 218 */ |
| 193 chngRowid = 0; | 219 chngRowid = chngPk = 0; |
| 194 for(i=0; i<pChanges->nExpr; i++){ | 220 for(i=0; i<pChanges->nExpr; i++){ |
| 195 if( sqlite3ResolveExprNames(&sNC, pChanges->a[i].pExpr) ){ | 221 if( sqlite3ResolveExprNames(&sNC, pChanges->a[i].pExpr) ){ |
| 196 goto update_cleanup; | 222 goto update_cleanup; |
| 197 } | 223 } |
| 198 for(j=0; j<pTab->nCol; j++){ | 224 for(j=0; j<pTab->nCol; j++){ |
| 199 if( sqlite3StrICmp(pTab->aCol[j].zName, pChanges->a[i].zName)==0 ){ | 225 if( sqlite3StrICmp(pTab->aCol[j].zName, pChanges->a[i].zName)==0 ){ |
| 200 if( j==pTab->iPKey ){ | 226 if( j==pTab->iPKey ){ |
| 201 chngRowid = 1; | 227 chngRowid = 1; |
| 202 pRowidExpr = pChanges->a[i].pExpr; | 228 pRowidExpr = pChanges->a[i].pExpr; |
| 229 }else if( pPk && (pTab->aCol[j].colFlags & COLFLAG_PRIMKEY)!=0 ){ |
| 230 chngPk = 1; |
| 203 } | 231 } |
| 204 aXRef[j] = i; | 232 aXRef[j] = i; |
| 205 break; | 233 break; |
| 206 } | 234 } |
| 207 } | 235 } |
| 208 if( j>=pTab->nCol ){ | 236 if( j>=pTab->nCol ){ |
| 209 if( sqlite3IsRowid(pChanges->a[i].zName) ){ | 237 if( pPk==0 && sqlite3IsRowid(pChanges->a[i].zName) ){ |
| 238 j = -1; |
| 210 chngRowid = 1; | 239 chngRowid = 1; |
| 211 pRowidExpr = pChanges->a[i].pExpr; | 240 pRowidExpr = pChanges->a[i].pExpr; |
| 212 }else{ | 241 }else{ |
| 213 sqlite3ErrorMsg(pParse, "no such column: %s", pChanges->a[i].zName); | 242 sqlite3ErrorMsg(pParse, "no such column: %s", pChanges->a[i].zName); |
| 214 pParse->checkSchema = 1; | 243 pParse->checkSchema = 1; |
| 215 goto update_cleanup; | 244 goto update_cleanup; |
| 216 } | 245 } |
| 217 } | 246 } |
| 218 #ifndef SQLITE_OMIT_AUTHORIZATION | 247 #ifndef SQLITE_OMIT_AUTHORIZATION |
| 219 { | 248 { |
| 220 int rc; | 249 int rc; |
| 221 rc = sqlite3AuthCheck(pParse, SQLITE_UPDATE, pTab->zName, | 250 rc = sqlite3AuthCheck(pParse, SQLITE_UPDATE, pTab->zName, |
| 222 pTab->aCol[j].zName, db->aDb[iDb].zName); | 251 j<0 ? "ROWID" : pTab->aCol[j].zName, |
| 252 db->aDb[iDb].zName); |
| 223 if( rc==SQLITE_DENY ){ | 253 if( rc==SQLITE_DENY ){ |
| 224 goto update_cleanup; | 254 goto update_cleanup; |
| 225 }else if( rc==SQLITE_IGNORE ){ | 255 }else if( rc==SQLITE_IGNORE ){ |
| 226 aXRef[j] = -1; | 256 aXRef[j] = -1; |
| 227 } | 257 } |
| 228 } | 258 } |
| 229 #endif | 259 #endif |
| 230 } | 260 } |
| 261 assert( (chngRowid & chngPk)==0 ); |
| 262 assert( chngRowid==0 || chngRowid==1 ); |
| 263 assert( chngPk==0 || chngPk==1 ); |
| 264 chngKey = chngRowid + chngPk; |
| 231 | 265 |
| 232 hasFK = sqlite3FkRequired(pParse, pTab, aXRef, chngRowid); | 266 /* The SET expressions are not actually used inside the WHERE loop. |
| 267 ** So reset the colUsed mask |
| 268 */ |
| 269 pTabList->a[0].colUsed = 0; |
| 233 | 270 |
| 234 /* Allocate memory for the array aRegIdx[]. There is one entry in the | 271 hasFK = sqlite3FkRequired(pParse, pTab, aXRef, chngKey); |
| 235 ** array for each index associated with table being updated. Fill in | 272 |
| 236 ** the value with a register number for indices that are to be used | 273 /* There is one entry in the aRegIdx[] array for each index on the table |
| 237 ** and with zero for unused indices. | 274 ** being updated. Fill in aRegIdx[] with a register number that will hold |
| 275 ** the key for accessing each index. |
| 238 */ | 276 */ |
| 239 for(nIdx=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, nIdx++){} | |
| 240 if( nIdx>0 ){ | |
| 241 aRegIdx = sqlite3DbMallocRaw(db, sizeof(Index*) * nIdx ); | |
| 242 if( aRegIdx==0 ) goto update_cleanup; | |
| 243 } | |
| 244 for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){ | 277 for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){ |
| 245 int reg; | 278 int reg; |
| 246 if( chngRowid ){ | 279 if( chngKey || hasFK || pIdx->pPartIdxWhere || pIdx==pPk ){ |
| 247 reg = ++pParse->nMem; | 280 reg = ++pParse->nMem; |
| 248 }else{ | 281 }else{ |
| 249 reg = 0; | 282 reg = 0; |
| 250 for(i=0; i<pIdx->nColumn; i++){ | 283 for(i=0; i<pIdx->nKeyCol; i++){ |
| 251 if( aXRef[pIdx->aiColumn[i]]>=0 ){ | 284 if( aXRef[pIdx->aiColumn[i]]>=0 ){ |
| 252 reg = ++pParse->nMem; | 285 reg = ++pParse->nMem; |
| 253 break; | 286 break; |
| 254 } | 287 } |
| 255 } | 288 } |
| 256 } | 289 } |
| 290 if( reg==0 ) aToOpen[j+1] = 0; |
| 257 aRegIdx[j] = reg; | 291 aRegIdx[j] = reg; |
| 258 } | 292 } |
| 259 | 293 |
| 260 /* Begin generating code. */ | 294 /* Begin generating code. */ |
| 261 v = sqlite3GetVdbe(pParse); | 295 v = sqlite3GetVdbe(pParse); |
| 262 if( v==0 ) goto update_cleanup; | 296 if( v==0 ) goto update_cleanup; |
| 263 if( pParse->nested==0 ) sqlite3VdbeCountChanges(v); | 297 if( pParse->nested==0 ) sqlite3VdbeCountChanges(v); |
| 264 sqlite3BeginWriteOperation(pParse, 1, iDb); | 298 sqlite3BeginWriteOperation(pParse, 1, iDb); |
| 265 | 299 |
| 266 #ifndef SQLITE_OMIT_VIRTUALTABLE | 300 #ifndef SQLITE_OMIT_VIRTUALTABLE |
| 267 /* Virtual tables must be handled separately */ | 301 /* Virtual tables must be handled separately */ |
| 268 if( IsVirtual(pTab) ){ | 302 if( IsVirtual(pTab) ){ |
| 269 updateVirtualTable(pParse, pTabList, pTab, pChanges, pRowidExpr, aXRef, | 303 updateVirtualTable(pParse, pTabList, pTab, pChanges, pRowidExpr, aXRef, |
| 270 pWhere); | 304 pWhere, onError); |
| 271 pWhere = 0; | 305 pWhere = 0; |
| 272 pTabList = 0; | 306 pTabList = 0; |
| 273 goto update_cleanup; | 307 goto update_cleanup; |
| 274 } | 308 } |
| 275 #endif | 309 #endif |
| 276 | 310 |
| 277 /* Allocate required registers. */ | 311 /* Allocate required registers. */ |
| 312 regRowSet = ++pParse->nMem; |
| 278 regOldRowid = regNewRowid = ++pParse->nMem; | 313 regOldRowid = regNewRowid = ++pParse->nMem; |
| 279 if( pTrigger || hasFK ){ | 314 if( chngPk || pTrigger || hasFK ){ |
| 280 regOld = pParse->nMem + 1; | 315 regOld = pParse->nMem + 1; |
| 281 pParse->nMem += pTab->nCol; | 316 pParse->nMem += pTab->nCol; |
| 282 } | 317 } |
| 283 if( chngRowid || pTrigger || hasFK ){ | 318 if( chngKey || pTrigger || hasFK ){ |
| 284 regNewRowid = ++pParse->nMem; | 319 regNewRowid = ++pParse->nMem; |
| 285 } | 320 } |
| 286 regNew = pParse->nMem + 1; | 321 regNew = pParse->nMem + 1; |
| 287 pParse->nMem += pTab->nCol; | 322 pParse->nMem += pTab->nCol; |
| 288 | 323 |
| 289 /* Start the view context. */ | 324 /* Start the view context. */ |
| 290 if( isView ){ | 325 if( isView ){ |
| 291 sqlite3AuthContextPush(pParse, &sContext, pTab->zName); | 326 sqlite3AuthContextPush(pParse, &sContext, pTab->zName); |
| 292 } | 327 } |
| 293 | 328 |
| 294 /* If we are trying to update a view, realize that view into | 329 /* If we are trying to update a view, realize that view into |
| 295 ** a ephemeral table. | 330 ** an ephemeral table. |
| 296 */ | 331 */ |
| 297 #if !defined(SQLITE_OMIT_VIEW) && !defined(SQLITE_OMIT_TRIGGER) | 332 #if !defined(SQLITE_OMIT_VIEW) && !defined(SQLITE_OMIT_TRIGGER) |
| 298 if( isView ){ | 333 if( isView ){ |
| 299 sqlite3MaterializeView(pParse, pTab, pWhere, iCur); | 334 sqlite3MaterializeView(pParse, pTab, pWhere, iDataCur); |
| 300 } | 335 } |
| 301 #endif | 336 #endif |
| 302 | 337 |
| 303 /* Resolve the column names in all the expressions in the | 338 /* Resolve the column names in all the expressions in the |
| 304 ** WHERE clause. | 339 ** WHERE clause. |
| 305 */ | 340 */ |
| 306 if( sqlite3ResolveExprNames(&sNC, pWhere) ){ | 341 if( sqlite3ResolveExprNames(&sNC, pWhere) ){ |
| 307 goto update_cleanup; | 342 goto update_cleanup; |
| 308 } | 343 } |
| 309 | 344 |
| 310 /* Begin the database scan | 345 /* Begin the database scan |
| 311 */ | 346 */ |
| 312 sqlite3VdbeAddOp2(v, OP_Null, 0, regOldRowid); | 347 if( HasRowid(pTab) ){ |
| 313 pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere,0, WHERE_ONEPASS_DESIRED); | 348 sqlite3VdbeAddOp3(v, OP_Null, 0, regRowSet, regOldRowid); |
| 314 if( pWInfo==0 ) goto update_cleanup; | 349 pWInfo = sqlite3WhereBegin( |
| 315 okOnePass = pWInfo->okOnePass; | 350 pParse, pTabList, pWhere, 0, 0, WHERE_ONEPASS_DESIRED, iIdxCur |
| 351 ); |
| 352 if( pWInfo==0 ) goto update_cleanup; |
| 353 okOnePass = sqlite3WhereOkOnePass(pWInfo, aiCurOnePass); |
| 354 |
| 355 /* Remember the rowid of every item to be updated. |
| 356 */ |
| 357 sqlite3VdbeAddOp2(v, OP_Rowid, iDataCur, regOldRowid); |
| 358 if( !okOnePass ){ |
| 359 sqlite3VdbeAddOp2(v, OP_RowSetAdd, regRowSet, regOldRowid); |
| 360 } |
| 361 |
| 362 /* End the database scan loop. |
| 363 */ |
| 364 sqlite3WhereEnd(pWInfo); |
| 365 }else{ |
| 366 int iPk; /* First of nPk memory cells holding PRIMARY KEY value */ |
| 367 i16 nPk; /* Number of components of the PRIMARY KEY */ |
| 368 int addrOpen; /* Address of the OpenEphemeral instruction */ |
| 316 | 369 |
| 317 /* Remember the rowid of every item to be updated. | 370 assert( pPk!=0 ); |
| 318 */ | 371 nPk = pPk->nKeyCol; |
| 319 sqlite3VdbeAddOp2(v, OP_Rowid, iCur, regOldRowid); | 372 iPk = pParse->nMem+1; |
| 320 if( !okOnePass ){ | 373 pParse->nMem += nPk; |
| 321 regRowSet = ++pParse->nMem; | 374 regKey = ++pParse->nMem; |
| 322 sqlite3VdbeAddOp2(v, OP_RowSetAdd, regRowSet, regOldRowid); | 375 iEph = pParse->nTab++; |
| 376 sqlite3VdbeAddOp2(v, OP_Null, 0, iPk); |
| 377 addrOpen = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, iEph, nPk); |
| 378 sqlite3VdbeSetP4KeyInfo(pParse, pPk); |
| 379 pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, 0, 0, |
| 380 WHERE_ONEPASS_DESIRED, iIdxCur); |
| 381 if( pWInfo==0 ) goto update_cleanup; |
| 382 okOnePass = sqlite3WhereOkOnePass(pWInfo, aiCurOnePass); |
| 383 for(i=0; i<nPk; i++){ |
| 384 sqlite3ExprCodeGetColumnOfTable(v, pTab, iDataCur, pPk->aiColumn[i], |
| 385 iPk+i); |
| 386 } |
| 387 if( okOnePass ){ |
| 388 sqlite3VdbeChangeToNoop(v, addrOpen); |
| 389 nKey = nPk; |
| 390 regKey = iPk; |
| 391 }else{ |
| 392 sqlite3VdbeAddOp4(v, OP_MakeRecord, iPk, nPk, regKey, |
| 393 sqlite3IndexAffinityStr(v, pPk), nPk); |
| 394 sqlite3VdbeAddOp2(v, OP_IdxInsert, iEph, regKey); |
| 395 } |
| 396 sqlite3WhereEnd(pWInfo); |
| 323 } | 397 } |
| 324 | 398 |
| 325 /* End the database scan loop. | |
| 326 */ | |
| 327 sqlite3WhereEnd(pWInfo); | |
| 328 | |
| 329 /* Initialize the count of updated rows | 399 /* Initialize the count of updated rows |
| 330 */ | 400 */ |
| 331 if( (db->flags & SQLITE_CountRows) && !pParse->pTriggerTab ){ | 401 if( (db->flags & SQLITE_CountRows) && !pParse->pTriggerTab ){ |
| 332 regRowCount = ++pParse->nMem; | 402 regRowCount = ++pParse->nMem; |
| 333 sqlite3VdbeAddOp2(v, OP_Integer, 0, regRowCount); | 403 sqlite3VdbeAddOp2(v, OP_Integer, 0, regRowCount); |
| 334 } | 404 } |
| 335 | 405 |
| 406 labelBreak = sqlite3VdbeMakeLabel(v); |
| 336 if( !isView ){ | 407 if( !isView ){ |
| 337 /* | 408 /* |
| 338 ** Open every index that needs updating. Note that if any | 409 ** Open every index that needs updating. Note that if any |
| 339 ** index could potentially invoke a REPLACE conflict resolution | 410 ** index could potentially invoke a REPLACE conflict resolution |
| 340 ** action, then we need to open all indices because we might need | 411 ** action, then we need to open all indices because we might need |
| 341 ** to be deleting some records. | 412 ** to be deleting some records. |
| 342 */ | 413 */ |
| 343 if( !okOnePass ) sqlite3OpenTable(pParse, iCur, iDb, pTab, OP_OpenWrite); | |
| 344 if( onError==OE_Replace ){ | 414 if( onError==OE_Replace ){ |
| 345 openAll = 1; | 415 memset(aToOpen, 1, nIdx+1); |
| 346 }else{ | 416 }else{ |
| 347 openAll = 0; | |
| 348 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){ | 417 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){ |
| 349 if( pIdx->onError==OE_Replace ){ | 418 if( pIdx->onError==OE_Replace ){ |
| 350 openAll = 1; | 419 memset(aToOpen, 1, nIdx+1); |
| 351 break; | 420 break; |
| 352 } | 421 } |
| 353 } | 422 } |
| 354 } | 423 } |
| 355 for(i=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){ | 424 if( okOnePass ){ |
| 356 if( openAll || aRegIdx[i]>0 ){ | 425 if( aiCurOnePass[0]>=0 ) aToOpen[aiCurOnePass[0]-iBaseCur] = 0; |
| 357 KeyInfo *pKey = sqlite3IndexKeyinfo(pParse, pIdx); | 426 if( aiCurOnePass[1]>=0 ) aToOpen[aiCurOnePass[1]-iBaseCur] = 0; |
| 358 sqlite3VdbeAddOp4(v, OP_OpenWrite, iCur+i+1, pIdx->tnum, iDb, | |
| 359 (char*)pKey, P4_KEYINFO_HANDOFF); | |
| 360 assert( pParse->nTab>iCur+i+1 ); | |
| 361 } | |
| 362 } | 427 } |
| 428 sqlite3OpenTableAndIndices(pParse, pTab, OP_OpenWrite, iBaseCur, aToOpen, |
| 429 0, 0); |
| 363 } | 430 } |
| 364 | 431 |
| 365 /* Top of the update loop */ | 432 /* Top of the update loop */ |
| 366 if( okOnePass ){ | 433 if( okOnePass ){ |
| 367 int a1 = sqlite3VdbeAddOp1(v, OP_NotNull, regOldRowid); | 434 if( aToOpen[iDataCur-iBaseCur] && !isView ){ |
| 368 addr = sqlite3VdbeAddOp0(v, OP_Goto); | 435 assert( pPk ); |
| 369 sqlite3VdbeJumpHere(v, a1); | 436 sqlite3VdbeAddOp4Int(v, OP_NotFound, iDataCur, labelBreak, regKey, nKey); |
| 437 VdbeCoverageNeverTaken(v); |
| 438 } |
| 439 labelContinue = labelBreak; |
| 440 sqlite3VdbeAddOp2(v, OP_IsNull, pPk ? regKey : regOldRowid, labelBreak); |
| 441 VdbeCoverageIf(v, pPk==0); |
| 442 VdbeCoverageIf(v, pPk!=0); |
| 443 }else if( pPk ){ |
| 444 labelContinue = sqlite3VdbeMakeLabel(v); |
| 445 sqlite3VdbeAddOp2(v, OP_Rewind, iEph, labelBreak); VdbeCoverage(v); |
| 446 addrTop = sqlite3VdbeAddOp2(v, OP_RowKey, iEph, regKey); |
| 447 sqlite3VdbeAddOp4Int(v, OP_NotFound, iDataCur, labelContinue, regKey, 0); |
| 448 VdbeCoverage(v); |
| 370 }else{ | 449 }else{ |
| 371 addr = sqlite3VdbeAddOp3(v, OP_RowSetRead, regRowSet, 0, regOldRowid); | 450 labelContinue = sqlite3VdbeAddOp3(v, OP_RowSetRead, regRowSet, labelBreak, |
| 451 regOldRowid); |
| 452 VdbeCoverage(v); |
| 453 sqlite3VdbeAddOp3(v, OP_NotExists, iDataCur, labelContinue, regOldRowid); |
| 454 VdbeCoverage(v); |
| 372 } | 455 } |
| 373 | 456 |
| 374 /* Make cursor iCur point to the record that is being updated. If | |
| 375 ** this record does not exist for some reason (deleted by a trigger, | |
| 376 ** for example, then jump to the next iteration of the RowSet loop. */ | |
| 377 sqlite3VdbeAddOp3(v, OP_NotExists, iCur, addr, regOldRowid); | |
| 378 | |
| 379 /* If the record number will change, set register regNewRowid to | 457 /* If the record number will change, set register regNewRowid to |
| 380 ** contain the new value. If the record number is not being modified, | 458 ** contain the new value. If the record number is not being modified, |
| 381 ** then regNewRowid is the same register as regOldRowid, which is | 459 ** then regNewRowid is the same register as regOldRowid, which is |
| 382 ** already populated. */ | 460 ** already populated. */ |
| 383 assert( chngRowid || pTrigger || hasFK || regOldRowid==regNewRowid ); | 461 assert( chngKey || pTrigger || hasFK || regOldRowid==regNewRowid ); |
| 384 if( chngRowid ){ | 462 if( chngRowid ){ |
| 385 sqlite3ExprCode(pParse, pRowidExpr, regNewRowid); | 463 sqlite3ExprCode(pParse, pRowidExpr, regNewRowid); |
| 386 sqlite3VdbeAddOp1(v, OP_MustBeInt, regNewRowid); | 464 sqlite3VdbeAddOp1(v, OP_MustBeInt, regNewRowid); VdbeCoverage(v); |
| 387 } | 465 } |
| 388 | 466 |
| 389 /* If there are triggers on this table, populate an array of registers | 467 /* Compute the old pre-UPDATE content of the row being changed, if that |
| 390 ** with the required old.* column data. */ | 468 ** information is needed */ |
| 391 if( hasFK || pTrigger ){ | 469 if( chngPk || hasFK || pTrigger ){ |
| 392 u32 oldmask = (hasFK ? sqlite3FkOldmask(pParse, pTab) : 0); | 470 u32 oldmask = (hasFK ? sqlite3FkOldmask(pParse, pTab) : 0); |
| 393 oldmask |= sqlite3TriggerColmask(pParse, | 471 oldmask |= sqlite3TriggerColmask(pParse, |
| 394 pTrigger, pChanges, 0, TRIGGER_BEFORE|TRIGGER_AFTER, pTab, onError | 472 pTrigger, pChanges, 0, TRIGGER_BEFORE|TRIGGER_AFTER, pTab, onError |
| 395 ); | 473 ); |
| 396 for(i=0; i<pTab->nCol; i++){ | 474 for(i=0; i<pTab->nCol; i++){ |
| 397 if( aXRef[i]<0 || oldmask==0xffffffff || (i<32 && (oldmask & (1<<i))) ){ | 475 if( oldmask==0xffffffff |
| 398 sqlite3ExprCodeGetColumnOfTable(v, pTab, iCur, i, regOld+i); | 476 || (i<32 && (oldmask & MASKBIT32(i))!=0) |
| 477 || (pTab->aCol[i].colFlags & COLFLAG_PRIMKEY)!=0 |
| 478 ){ |
| 479 testcase( oldmask!=0xffffffff && i==31 ); |
| 480 sqlite3ExprCodeGetColumnOfTable(v, pTab, iDataCur, i, regOld+i); |
| 399 }else{ | 481 }else{ |
| 400 sqlite3VdbeAddOp2(v, OP_Null, 0, regOld+i); | 482 sqlite3VdbeAddOp2(v, OP_Null, 0, regOld+i); |
| 401 } | 483 } |
| 402 } | 484 } |
| 403 if( chngRowid==0 ){ | 485 if( chngRowid==0 && pPk==0 ){ |
| 404 sqlite3VdbeAddOp2(v, OP_Copy, regOldRowid, regNewRowid); | 486 sqlite3VdbeAddOp2(v, OP_Copy, regOldRowid, regNewRowid); |
| 405 } | 487 } |
| 406 } | 488 } |
| 407 | 489 |
| 408 /* Populate the array of registers beginning at regNew with the new | 490 /* Populate the array of registers beginning at regNew with the new |
| 409 ** row data. This array is used to check constaints, create the new | 491 ** row data. This array is used to check constants, create the new |
| 410 ** table and index records, and as the values for any new.* references | 492 ** table and index records, and as the values for any new.* references |
| 411 ** made by triggers. | 493 ** made by triggers. |
| 412 ** | 494 ** |
| 413 ** If there are one or more BEFORE triggers, then do not populate the | 495 ** If there are one or more BEFORE triggers, then do not populate the |
| 414 ** registers associated with columns that are (a) not modified by | 496 ** registers associated with columns that are (a) not modified by |
| 415 ** this UPDATE statement and (b) not accessed by new.* references. The | 497 ** this UPDATE statement and (b) not accessed by new.* references. The |
| 416 ** values for registers not modified by the UPDATE must be reloaded from | 498 ** values for registers not modified by the UPDATE must be reloaded from |
| 417 ** the database after the BEFORE triggers are fired anyway (as the trigger | 499 ** the database after the BEFORE triggers are fired anyway (as the trigger |
| 418 ** may have modified them). So not loading those that are not going to | 500 ** may have modified them). So not loading those that are not going to |
| 419 ** be used eliminates some redundant opcodes. | 501 ** be used eliminates some redundant opcodes. |
| 420 */ | 502 */ |
| 421 newmask = sqlite3TriggerColmask( | 503 newmask = sqlite3TriggerColmask( |
| 422 pParse, pTrigger, pChanges, 1, TRIGGER_BEFORE, pTab, onError | 504 pParse, pTrigger, pChanges, 1, TRIGGER_BEFORE, pTab, onError |
| 423 ); | 505 ); |
| 506 /*sqlite3VdbeAddOp3(v, OP_Null, 0, regNew, regNew+pTab->nCol-1);*/ |
| 424 for(i=0; i<pTab->nCol; i++){ | 507 for(i=0; i<pTab->nCol; i++){ |
| 425 if( i==pTab->iPKey ){ | 508 if( i==pTab->iPKey ){ |
| 426 sqlite3VdbeAddOp2(v, OP_Null, 0, regNew+i); | 509 sqlite3VdbeAddOp2(v, OP_Null, 0, regNew+i); |
| 427 }else{ | 510 }else{ |
| 428 j = aXRef[i]; | 511 j = aXRef[i]; |
| 429 if( j>=0 ){ | 512 if( j>=0 ){ |
| 430 sqlite3ExprCode(pParse, pChanges->a[j].pExpr, regNew+i); | 513 sqlite3ExprCode(pParse, pChanges->a[j].pExpr, regNew+i); |
| 431 }else if( 0==(tmask&TRIGGER_BEFORE) || i>31 || (newmask&(1<<i)) ){ | 514 }else if( 0==(tmask&TRIGGER_BEFORE) || i>31 || (newmask & MASKBIT32(i)) ){ |
| 432 /* This branch loads the value of a column that will not be changed | 515 /* This branch loads the value of a column that will not be changed |
| 433 ** into a register. This is done if there are no BEFORE triggers, or | 516 ** into a register. This is done if there are no BEFORE triggers, or |
| 434 ** if there are one or more BEFORE triggers that use this value via | 517 ** if there are one or more BEFORE triggers that use this value via |
| 435 ** a new.* reference in a trigger program. | 518 ** a new.* reference in a trigger program. |
| 436 */ | 519 */ |
| 437 testcase( i==31 ); | 520 testcase( i==31 ); |
| 438 testcase( i==32 ); | 521 testcase( i==32 ); |
| 439 sqlite3VdbeAddOp3(v, OP_Column, iCur, i, regNew+i); | 522 sqlite3ExprCodeGetColumnOfTable(v, pTab, iDataCur, i, regNew+i); |
| 440 sqlite3ColumnDefault(v, pTab, i, regNew+i); | 523 }else{ |
| 524 sqlite3VdbeAddOp2(v, OP_Null, 0, regNew+i); |
| 441 } | 525 } |
| 442 } | 526 } |
| 443 } | 527 } |
| 444 | 528 |
| 445 /* Fire any BEFORE UPDATE triggers. This happens before constraints are | 529 /* Fire any BEFORE UPDATE triggers. This happens before constraints are |
| 446 ** verified. One could argue that this is wrong. | 530 ** verified. One could argue that this is wrong. |
| 447 */ | 531 */ |
| 448 if( tmask&TRIGGER_BEFORE ){ | 532 if( tmask&TRIGGER_BEFORE ){ |
| 449 sqlite3VdbeAddOp2(v, OP_Affinity, regNew, pTab->nCol); | 533 sqlite3TableAffinity(v, pTab, regNew); |
| 450 sqlite3TableAffinityStr(v, pTab); | |
| 451 sqlite3CodeRowTrigger(pParse, pTrigger, TK_UPDATE, pChanges, | 534 sqlite3CodeRowTrigger(pParse, pTrigger, TK_UPDATE, pChanges, |
| 452 TRIGGER_BEFORE, pTab, regOldRowid, onError, addr); | 535 TRIGGER_BEFORE, pTab, regOldRowid, onError, labelContinue); |
| 453 | 536 |
| 454 /* The row-trigger may have deleted the row being updated. In this | 537 /* The row-trigger may have deleted the row being updated. In this |
| 455 ** case, jump to the next row. No updates or AFTER triggers are | 538 ** case, jump to the next row. No updates or AFTER triggers are |
| 456 ** required. This behaviour - what happens when the row being updated | 539 ** required. This behavior - what happens when the row being updated |
| 457 ** is deleted or renamed by a BEFORE trigger - is left undefined in the | 540 ** is deleted or renamed by a BEFORE trigger - is left undefined in the |
| 458 ** documentation. | 541 ** documentation. |
| 459 */ | 542 */ |
| 460 sqlite3VdbeAddOp3(v, OP_NotExists, iCur, addr, regOldRowid); | 543 if( pPk ){ |
| 544 sqlite3VdbeAddOp4Int(v, OP_NotFound, iDataCur, labelContinue,regKey,nKey); |
| 545 VdbeCoverage(v); |
| 546 }else{ |
| 547 sqlite3VdbeAddOp3(v, OP_NotExists, iDataCur, labelContinue, regOldRowid); |
| 548 VdbeCoverage(v); |
| 549 } |
| 461 | 550 |
| 462 /* If it did not delete it, the row-trigger may still have modified | 551 /* If it did not delete it, the row-trigger may still have modified |
| 463 ** some of the columns of the row being updated. Load the values for | 552 ** some of the columns of the row being updated. Load the values for |
| 464 ** all columns not modified by the update statement into their | 553 ** all columns not modified by the update statement into their |
| 465 ** registers in case this has happened. | 554 ** registers in case this has happened. |
| 466 */ | 555 */ |
| 467 for(i=0; i<pTab->nCol; i++){ | 556 for(i=0; i<pTab->nCol; i++){ |
| 468 if( aXRef[i]<0 && i!=pTab->iPKey ){ | 557 if( aXRef[i]<0 && i!=pTab->iPKey ){ |
| 469 sqlite3VdbeAddOp3(v, OP_Column, iCur, i, regNew+i); | 558 sqlite3ExprCodeGetColumnOfTable(v, pTab, iDataCur, i, regNew+i); |
| 470 sqlite3ColumnDefault(v, pTab, i, regNew+i); | |
| 471 } | 559 } |
| 472 } | 560 } |
| 473 } | 561 } |
| 474 | 562 |
| 475 if( !isView ){ | 563 if( !isView ){ |
| 476 int j1; /* Address of jump instruction */ | 564 int j1 = 0; /* Address of jump instruction */ |
| 565 int bReplace = 0; /* True if REPLACE conflict resolution might happen */ |
| 477 | 566 |
| 478 /* Do constraint checks. */ | 567 /* Do constraint checks. */ |
| 479 sqlite3GenerateConstraintChecks(pParse, pTab, iCur, regNewRowid, | 568 assert( regOldRowid>0 ); |
| 480 aRegIdx, (chngRowid?regOldRowid:0), 1, onError, addr, 0); | 569 sqlite3GenerateConstraintChecks(pParse, pTab, aRegIdx, iDataCur, iIdxCur, |
| 570 regNewRowid, regOldRowid, chngKey, onError, labelContinue, &bReplace); |
| 481 | 571 |
| 482 /* Do FK constraint checks. */ | 572 /* Do FK constraint checks. */ |
| 483 if( hasFK ){ | 573 if( hasFK ){ |
| 484 sqlite3FkCheck(pParse, pTab, regOldRowid, 0); | 574 sqlite3FkCheck(pParse, pTab, regOldRowid, 0, aXRef, chngKey); |
| 485 } | 575 } |
| 486 | 576 |
| 487 /* Delete the index entries associated with the current record. */ | 577 /* Delete the index entries associated with the current record. */ |
| 488 j1 = sqlite3VdbeAddOp3(v, OP_NotExists, iCur, 0, regOldRowid); | 578 if( bReplace || chngKey ){ |
| 489 sqlite3GenerateRowIndexDelete(pParse, pTab, iCur, aRegIdx); | 579 if( pPk ){ |
| 580 j1 = sqlite3VdbeAddOp4Int(v, OP_NotFound, iDataCur, 0, regKey, nKey); |
| 581 }else{ |
| 582 j1 = sqlite3VdbeAddOp3(v, OP_NotExists, iDataCur, 0, regOldRowid); |
| 583 } |
| 584 VdbeCoverageNeverTaken(v); |
| 585 } |
| 586 sqlite3GenerateRowIndexDelete(pParse, pTab, iDataCur, iIdxCur, aRegIdx); |
| 490 | 587 |
| 491 /* If changing the record number, delete the old record. */ | 588 /* If changing the record number, delete the old record. */ |
| 492 if( hasFK || chngRowid ){ | 589 if( hasFK || chngKey || pPk!=0 ){ |
| 493 sqlite3VdbeAddOp2(v, OP_Delete, iCur, 0); | 590 sqlite3VdbeAddOp2(v, OP_Delete, iDataCur, 0); |
| 494 } | 591 } |
| 495 sqlite3VdbeJumpHere(v, j1); | 592 if( bReplace || chngKey ){ |
| 593 sqlite3VdbeJumpHere(v, j1); |
| 594 } |
| 496 | 595 |
| 497 if( hasFK ){ | 596 if( hasFK ){ |
| 498 sqlite3FkCheck(pParse, pTab, 0, regNewRowid); | 597 sqlite3FkCheck(pParse, pTab, 0, regNewRowid, aXRef, chngKey); |
| 499 } | 598 } |
| 500 | 599 |
| 501 /* Insert the new index entries and the new record. */ | 600 /* Insert the new index entries and the new record. */ |
| 502 sqlite3CompleteInsertion(pParse, pTab, iCur, regNewRowid, aRegIdx, 1, 0, 0); | 601 sqlite3CompleteInsertion(pParse, pTab, iDataCur, iIdxCur, |
| 602 regNewRowid, aRegIdx, 1, 0, 0); |
| 503 | 603 |
| 504 /* Do any ON CASCADE, SET NULL or SET DEFAULT operations required to | 604 /* Do any ON CASCADE, SET NULL or SET DEFAULT operations required to |
| 505 ** handle rows (possibly in other tables) that refer via a foreign key | 605 ** handle rows (possibly in other tables) that refer via a foreign key |
| 506 ** to the row just updated. */ | 606 ** to the row just updated. */ |
| 507 if( hasFK ){ | 607 if( hasFK ){ |
| 508 sqlite3FkActions(pParse, pTab, pChanges, regOldRowid); | 608 sqlite3FkActions(pParse, pTab, pChanges, regOldRowid, aXRef, chngKey); |
| 509 } | 609 } |
| 510 } | 610 } |
| 511 | 611 |
| 512 /* Increment the row counter | 612 /* Increment the row counter |
| 513 */ | 613 */ |
| 514 if( (db->flags & SQLITE_CountRows) && !pParse->pTriggerTab){ | 614 if( (db->flags & SQLITE_CountRows) && !pParse->pTriggerTab){ |
| 515 sqlite3VdbeAddOp2(v, OP_AddImm, regRowCount, 1); | 615 sqlite3VdbeAddOp2(v, OP_AddImm, regRowCount, 1); |
| 516 } | 616 } |
| 517 | 617 |
| 518 sqlite3CodeRowTrigger(pParse, pTrigger, TK_UPDATE, pChanges, | 618 sqlite3CodeRowTrigger(pParse, pTrigger, TK_UPDATE, pChanges, |
| 519 TRIGGER_AFTER, pTab, regOldRowid, onError, addr); | 619 TRIGGER_AFTER, pTab, regOldRowid, onError, labelContinue); |
| 520 | 620 |
| 521 /* Repeat the above with the next record to be updated, until | 621 /* Repeat the above with the next record to be updated, until |
| 522 ** all record selected by the WHERE clause have been updated. | 622 ** all record selected by the WHERE clause have been updated. |
| 523 */ | 623 */ |
| 524 sqlite3VdbeAddOp2(v, OP_Goto, 0, addr); | 624 if( okOnePass ){ |
| 525 sqlite3VdbeJumpHere(v, addr); | 625 /* Nothing to do at end-of-loop for a single-pass */ |
| 626 }else if( pPk ){ |
| 627 sqlite3VdbeResolveLabel(v, labelContinue); |
| 628 sqlite3VdbeAddOp2(v, OP_Next, iEph, addrTop); VdbeCoverage(v); |
| 629 }else{ |
| 630 sqlite3VdbeAddOp2(v, OP_Goto, 0, labelContinue); |
| 631 } |
| 632 sqlite3VdbeResolveLabel(v, labelBreak); |
| 526 | 633 |
| 527 /* Close all tables */ | 634 /* Close all tables */ |
| 528 for(i=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){ | 635 for(i=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){ |
| 529 if( openAll || aRegIdx[i]>0 ){ | 636 assert( aRegIdx ); |
| 530 sqlite3VdbeAddOp2(v, OP_Close, iCur+i+1, 0); | 637 if( aToOpen[i+1] ){ |
| 638 sqlite3VdbeAddOp2(v, OP_Close, iIdxCur+i, 0); |
| 531 } | 639 } |
| 532 } | 640 } |
| 533 sqlite3VdbeAddOp2(v, OP_Close, iCur, 0); | 641 if( iDataCur<iIdxCur ) sqlite3VdbeAddOp2(v, OP_Close, iDataCur, 0); |
| 534 | 642 |
| 535 /* Update the sqlite_sequence table by storing the content of the | 643 /* Update the sqlite_sequence table by storing the content of the |
| 536 ** maximum rowid counter values recorded while inserting into | 644 ** maximum rowid counter values recorded while inserting into |
| 537 ** autoincrement tables. | 645 ** autoincrement tables. |
| 538 */ | 646 */ |
| 539 if( pParse->nested==0 && pParse->pTriggerTab==0 ){ | 647 if( pParse->nested==0 && pParse->pTriggerTab==0 ){ |
| 540 sqlite3AutoincrementEnd(pParse); | 648 sqlite3AutoincrementEnd(pParse); |
| 541 } | 649 } |
| 542 | 650 |
| 543 /* | 651 /* |
| 544 ** Return the number of rows that were changed. If this routine is | 652 ** Return the number of rows that were changed. If this routine is |
| 545 ** generating code because of a call to sqlite3NestedParse(), do not | 653 ** generating code because of a call to sqlite3NestedParse(), do not |
| 546 ** invoke the callback function. | 654 ** invoke the callback function. |
| 547 */ | 655 */ |
| 548 if( (db->flags&SQLITE_CountRows) && !pParse->pTriggerTab && !pParse->nested ){ | 656 if( (db->flags&SQLITE_CountRows) && !pParse->pTriggerTab && !pParse->nested ){ |
| 549 sqlite3VdbeAddOp2(v, OP_ResultRow, regRowCount, 1); | 657 sqlite3VdbeAddOp2(v, OP_ResultRow, regRowCount, 1); |
| 550 sqlite3VdbeSetNumCols(v, 1); | 658 sqlite3VdbeSetNumCols(v, 1); |
| 551 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "rows updated", SQLITE_STATIC); | 659 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "rows updated", SQLITE_STATIC); |
| 552 } | 660 } |
| 553 | 661 |
| 554 update_cleanup: | 662 update_cleanup: |
| 555 sqlite3AuthContextPop(&sContext); | 663 sqlite3AuthContextPop(&sContext); |
| 556 sqlite3DbFree(db, aRegIdx); | 664 sqlite3DbFree(db, aXRef); /* Also frees aRegIdx[] and aToOpen[] */ |
| 557 sqlite3DbFree(db, aXRef); | |
| 558 sqlite3SrcListDelete(db, pTabList); | 665 sqlite3SrcListDelete(db, pTabList); |
| 559 sqlite3ExprListDelete(db, pChanges); | 666 sqlite3ExprListDelete(db, pChanges); |
| 560 sqlite3ExprDelete(db, pWhere); | 667 sqlite3ExprDelete(db, pWhere); |
| 561 return; | 668 return; |
| 562 } | 669 } |
| 563 /* Make sure "isView" and other macros defined above are undefined. Otherwise | 670 /* Make sure "isView" and other macros defined above are undefined. Otherwise |
| 564 ** thely may interfere with compilation of other functions in this file | 671 ** they may interfere with compilation of other functions in this file |
| 565 ** (or in another file, if this file becomes part of the amalgamation). */ | 672 ** (or in another file, if this file becomes part of the amalgamation). */ |
| 566 #ifdef isView | 673 #ifdef isView |
| 567 #undef isView | 674 #undef isView |
| 568 #endif | 675 #endif |
| 569 #ifdef pTrigger | 676 #ifdef pTrigger |
| 570 #undef pTrigger | 677 #undef pTrigger |
| 571 #endif | 678 #endif |
| 572 | 679 |
| 573 #ifndef SQLITE_OMIT_VIRTUALTABLE | 680 #ifndef SQLITE_OMIT_VIRTUALTABLE |
| 574 /* | 681 /* |
| 575 ** Generate code for an UPDATE of a virtual table. | 682 ** Generate code for an UPDATE of a virtual table. |
| 576 ** | 683 ** |
| 577 ** The strategy is that we create an ephemerial table that contains | 684 ** The strategy is that we create an ephemeral table that contains |
| 578 ** for each row to be changed: | 685 ** for each row to be changed: |
| 579 ** | 686 ** |
| 580 ** (A) The original rowid of that row. | 687 ** (A) The original rowid of that row. |
| 581 ** (B) The revised rowid for the row. (note1) | 688 ** (B) The revised rowid for the row. (note1) |
| 582 ** (C) The content of every column in the row. | 689 ** (C) The content of every column in the row. |
| 583 ** | 690 ** |
| 584 ** Then we loop over this ephemeral table and for each row in | 691 ** Then we loop over this ephemeral table and for each row in |
| 585 ** the ephermeral table call VUpdate. | 692 ** the ephemeral table call VUpdate. |
| 586 ** | 693 ** |
| 587 ** When finished, drop the ephemeral table. | 694 ** When finished, drop the ephemeral table. |
| 588 ** | 695 ** |
| 589 ** (note1) Actually, if we know in advance that (A) is always the same | 696 ** (note1) Actually, if we know in advance that (A) is always the same |
| 590 ** as (B) we only store (A), then duplicate (A) when pulling | 697 ** as (B) we only store (A), then duplicate (A) when pulling |
| 591 ** it out of the ephemeral table before calling VUpdate. | 698 ** it out of the ephemeral table before calling VUpdate. |
| 592 */ | 699 */ |
| 593 static void updateVirtualTable( | 700 static void updateVirtualTable( |
| 594 Parse *pParse, /* The parsing context */ | 701 Parse *pParse, /* The parsing context */ |
| 595 SrcList *pSrc, /* The virtual table to be modified */ | 702 SrcList *pSrc, /* The virtual table to be modified */ |
| 596 Table *pTab, /* The virtual table */ | 703 Table *pTab, /* The virtual table */ |
| 597 ExprList *pChanges, /* The columns to change in the UPDATE statement */ | 704 ExprList *pChanges, /* The columns to change in the UPDATE statement */ |
| 598 Expr *pRowid, /* Expression used to recompute the rowid */ | 705 Expr *pRowid, /* Expression used to recompute the rowid */ |
| 599 int *aXRef, /* Mapping from columns of pTab to entries in pChanges */ | 706 int *aXRef, /* Mapping from columns of pTab to entries in pChanges */ |
| 600 Expr *pWhere /* WHERE clause of the UPDATE statement */ | 707 Expr *pWhere, /* WHERE clause of the UPDATE statement */ |
| 708 int onError /* ON CONFLICT strategy */ |
| 601 ){ | 709 ){ |
| 602 Vdbe *v = pParse->pVdbe; /* Virtual machine under construction */ | 710 Vdbe *v = pParse->pVdbe; /* Virtual machine under construction */ |
| 603 ExprList *pEList = 0; /* The result set of the SELECT statement */ | 711 ExprList *pEList = 0; /* The result set of the SELECT statement */ |
| 604 Select *pSelect = 0; /* The SELECT statement */ | 712 Select *pSelect = 0; /* The SELECT statement */ |
| 605 Expr *pExpr; /* Temporary expression */ | 713 Expr *pExpr; /* Temporary expression */ |
| 606 int ephemTab; /* Table holding the result of the SELECT */ | 714 int ephemTab; /* Table holding the result of the SELECT */ |
| 607 int i; /* Loop counter */ | 715 int i; /* Loop counter */ |
| 608 int addr; /* Address of top of loop */ | 716 int addr; /* Address of top of loop */ |
| 609 int iReg; /* First register in set passed to OP_VUpdate */ | 717 int iReg; /* First register in set passed to OP_VUpdate */ |
| 610 sqlite3 *db = pParse->db; /* Database connection */ | 718 sqlite3 *db = pParse->db; /* Database connection */ |
| (...skipping 28 matching lines...) Expand all Loading... |
| 639 sqlite3VdbeChangeP5(v, BTREE_UNORDERED); | 747 sqlite3VdbeChangeP5(v, BTREE_UNORDERED); |
| 640 | 748 |
| 641 /* fill the ephemeral table | 749 /* fill the ephemeral table |
| 642 */ | 750 */ |
| 643 sqlite3SelectDestInit(&dest, SRT_Table, ephemTab); | 751 sqlite3SelectDestInit(&dest, SRT_Table, ephemTab); |
| 644 sqlite3Select(pParse, pSelect, &dest); | 752 sqlite3Select(pParse, pSelect, &dest); |
| 645 | 753 |
| 646 /* Generate code to scan the ephemeral table and call VUpdate. */ | 754 /* Generate code to scan the ephemeral table and call VUpdate. */ |
| 647 iReg = ++pParse->nMem; | 755 iReg = ++pParse->nMem; |
| 648 pParse->nMem += pTab->nCol+1; | 756 pParse->nMem += pTab->nCol+1; |
| 649 addr = sqlite3VdbeAddOp2(v, OP_Rewind, ephemTab, 0); | 757 addr = sqlite3VdbeAddOp2(v, OP_Rewind, ephemTab, 0); VdbeCoverage(v); |
| 650 sqlite3VdbeAddOp3(v, OP_Column, ephemTab, 0, iReg); | 758 sqlite3VdbeAddOp3(v, OP_Column, ephemTab, 0, iReg); |
| 651 sqlite3VdbeAddOp3(v, OP_Column, ephemTab, (pRowid?1:0), iReg+1); | 759 sqlite3VdbeAddOp3(v, OP_Column, ephemTab, (pRowid?1:0), iReg+1); |
| 652 for(i=0; i<pTab->nCol; i++){ | 760 for(i=0; i<pTab->nCol; i++){ |
| 653 sqlite3VdbeAddOp3(v, OP_Column, ephemTab, i+1+(pRowid!=0), iReg+2+i); | 761 sqlite3VdbeAddOp3(v, OP_Column, ephemTab, i+1+(pRowid!=0), iReg+2+i); |
| 654 } | 762 } |
| 655 sqlite3VtabMakeWritable(pParse, pTab); | 763 sqlite3VtabMakeWritable(pParse, pTab); |
| 656 sqlite3VdbeAddOp4(v, OP_VUpdate, 0, pTab->nCol+2, iReg, pVTab, P4_VTAB); | 764 sqlite3VdbeAddOp4(v, OP_VUpdate, 0, pTab->nCol+2, iReg, pVTab, P4_VTAB); |
| 765 sqlite3VdbeChangeP5(v, onError==OE_Default ? OE_Abort : onError); |
| 657 sqlite3MayAbort(pParse); | 766 sqlite3MayAbort(pParse); |
| 658 sqlite3VdbeAddOp2(v, OP_Next, ephemTab, addr+1); | 767 sqlite3VdbeAddOp2(v, OP_Next, ephemTab, addr+1); VdbeCoverage(v); |
| 659 sqlite3VdbeJumpHere(v, addr); | 768 sqlite3VdbeJumpHere(v, addr); |
| 660 sqlite3VdbeAddOp2(v, OP_Close, ephemTab, 0); | 769 sqlite3VdbeAddOp2(v, OP_Close, ephemTab, 0); |
| 661 | 770 |
| 662 /* Cleanup */ | 771 /* Cleanup */ |
| 663 sqlite3SelectDelete(db, pSelect); | 772 sqlite3SelectDelete(db, pSelect); |
| 664 } | 773 } |
| 665 #endif /* SQLITE_OMIT_VIRTUALTABLE */ | 774 #endif /* SQLITE_OMIT_VIRTUALTABLE */ |
| OLD | NEW |