| 1 |
/* |
| 2 |
* Copyright 2004-2023 H2 Group. Multiple-Licensed under the MPL 2.0, |
| 3 |
* and the EPL 1.0 (https://h2database.com/html/license.html). |
| 4 |
* Initial Developer: H2 Group |
| 5 |
*/ |
| 6 |
package org.h2.command.dml; |
| 7 |
|
| 8 |
import java.util.HashSet; |
| 9 |
|
| 10 |
import org.h2.api.Trigger; |
| 11 |
import org.h2.command.CommandInterface; |
| 12 |
import org.h2.command.Prepared; |
| 13 |
import org.h2.command.query.AllColumnsForPlan; |
| 14 |
import org.h2.engine.DbObject; |
| 15 |
import org.h2.engine.Right; |
| 16 |
import org.h2.engine.SessionLocal; |
| 17 |
import org.h2.expression.Expression; |
| 18 |
import org.h2.expression.ExpressionVisitor; |
| 19 |
import org.h2.message.DbException; |
| 20 |
import org.h2.result.LocalResult; |
| 21 |
import org.h2.result.ResultTarget; |
| 22 |
import org.h2.result.Row; |
| 23 |
import org.h2.table.DataChangeDeltaTable.ResultOption; |
| 24 |
import org.h2.table.PlanItem; |
| 25 |
import org.h2.table.Table; |
| 26 |
import org.h2.table.TableFilter; |
| 27 |
import org.h2.value.Value; |
| 28 |
import org.h2.value.ValueNull; |
| 29 |
|
| 30 |
/** |
| 31 |
* This class represents the statement |
| 32 |
* UPDATE |
| 33 |
*/ |
| 34 |
public final class Update extends FilteredDataChangeStatement { |
| 35 |
|
| 36 |
private SetClauseList setClauseList; |
| 37 |
|
| 38 |
private Insert onDuplicateKeyInsert; |
| 39 |
|
| 40 |
public Update(SessionLocal session) { |
| 41 |
super(session); |
| 42 |
} |
| 43 |
|
| 44 |
public void setSetClauseList(SetClauseList setClauseList) { |
| 45 |
this.setClauseList = setClauseList; |
| 46 |
} |
| 47 |
|
| 48 |
@Override |
| 49 |
public long update(ResultTarget deltaChangeCollector, ResultOption deltaChangeCollectionMode) { |
| 50 |
targetTableFilter.startQuery(session); |
| 51 |
targetTableFilter.reset(); |
| 52 |
Table table = targetTableFilter.getTable(); |
| 53 |
try (LocalResult rows = LocalResult.forTable(session, table)) { |
| 54 |
session.getUser().checkTableRight(table, Right.UPDATE); |
| 55 |
table.fire(session, Trigger.UPDATE, true); |
| 56 |
table.lock(session, Table.WRITE_LOCK); |
| 57 |
// get the old rows, compute the new rows |
| 58 |
setCurrentRowNumber(0); |
| 59 |
long count = 0; |
| 60 |
long limitRows = -1; |
| 61 |
if (fetchExpr != null) { |
| 62 |
Value v = fetchExpr.getValue(session); |
| 63 |
if (v == ValueNull.INSTANCE || (limitRows = v.getLong()) < 0) { |
| 64 |
throw DbException.getInvalidValueException("FETCH", v); |
| 65 |
} |
| 66 |
} |
| 67 |
while (nextRow(limitRows, count)) { |
| 68 |
Row oldRow = targetTableFilter.get(); |
| 69 |
if (table.isRowLockable()) { |
| 70 |
Row lockedRow = table.lockRow(session, oldRow, -1); |
| 71 |
if (lockedRow == null) { |
| 72 |
continue; |
| 73 |
} |
| 74 |
if (!oldRow.hasSharedData(lockedRow)) { |
| 75 |
oldRow = lockedRow; |
| 76 |
targetTableFilter.set(oldRow); |
| 77 |
if (condition != null && !condition.getBooleanValue(session)) { |
| 78 |
continue; |
| 79 |
} |
| 80 |
} |
| 81 |
} |
| 82 |
if (setClauseList.prepareUpdate(table, session, deltaChangeCollector, deltaChangeCollectionMode, |
| 83 |
rows, oldRow, onDuplicateKeyInsert != null)) { |
| 84 |
count++; |
| 85 |
} |
| 86 |
} |
| 87 |
doUpdate(this, session, table, rows); |
| 88 |
table.fire(session, Trigger.UPDATE, false); |
| 89 |
return count; |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
static void doUpdate(Prepared prepared, SessionLocal session, Table table, LocalResult rows) { |
| 94 |
rows.done(); |
| 95 |
// TODO self referencing referential integrity constraints |
| 96 |
// don't work if update is multi-row and 'inversed' the condition! |
| 97 |
// probably need multi-row triggers with 'deleted' and 'inserted' |
| 98 |
// at the same time. anyway good for sql compatibility |
| 99 |
// TODO update in-place (but if the key changes, |
| 100 |
// we need to update all indexes) before row triggers |
| 101 |
|
| 102 |
// the cached row is already updated - we need the old values |
| 103 |
table.updateRows(prepared, session, rows); |
| 104 |
if (table.fireRow()) { |
| 105 |
for (rows.reset(); rows.next();) { |
| 106 |
Row o = rows.currentRowForTable(); |
| 107 |
rows.next(); |
| 108 |
Row n = rows.currentRowForTable(); |
| 109 |
table.fireAfterRow(session, o, n, false); |
| 110 |
} |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
@Override |
| 115 |
public String getPlanSQL(int sqlFlags) { |
| 116 |
StringBuilder builder = new StringBuilder("UPDATE "); |
| 117 |
targetTableFilter.getPlanSQL(builder, false, sqlFlags); |
| 118 |
setClauseList.getSQL(builder, sqlFlags); |
| 119 |
appendFilterCondition(builder, sqlFlags); |
| 120 |
return builder.toString(); |
| 121 |
} |
| 122 |
|
| 123 |
@Override |
| 124 |
void doPrepare() { |
| 125 |
if (condition != null) { |
| 126 |
condition.mapColumns(targetTableFilter, 0, Expression.MAP_INITIAL); |
| 127 |
condition = condition.optimizeCondition(session); |
| 128 |
if (condition != null) { |
| 129 |
condition.createIndexConditions(session, targetTableFilter); |
| 130 |
} |
| 131 |
} |
| 132 |
setClauseList.mapAndOptimize(session, targetTableFilter, null); |
| 133 |
TableFilter[] filters = new TableFilter[] { targetTableFilter }; |
| 134 |
PlanItem item = targetTableFilter.getBestPlanItem(session, filters, 0, new AllColumnsForPlan(filters)); |
| 135 |
targetTableFilter.setPlanItem(item); |
| 136 |
targetTableFilter.prepare(); |
| 137 |
} |
| 138 |
|
| 139 |
@Override |
| 140 |
public int getType() { |
| 141 |
return CommandInterface.UPDATE; |
| 142 |
} |
| 143 |
|
| 144 |
@Override |
| 145 |
public String getStatementName() { |
| 146 |
return "UPDATE"; |
| 147 |
} |
| 148 |
|
| 149 |
@Override |
| 150 |
public void collectDependencies(HashSet<DbObject> dependencies) { |
| 151 |
ExpressionVisitor visitor = ExpressionVisitor.getDependenciesVisitor(dependencies); |
| 152 |
if (condition != null) { |
| 153 |
condition.isEverything(visitor); |
| 154 |
} |
| 155 |
setClauseList.isEverything(visitor); |
| 156 |
} |
| 157 |
|
| 158 |
public Insert getOnDuplicateKeyInsert() { |
| 159 |
return onDuplicateKeyInsert; |
| 160 |
} |
| 161 |
|
| 162 |
void setOnDuplicateKeyInsert(Insert onDuplicateKeyInsert) { |
| 163 |
this.onDuplicateKeyInsert = onDuplicateKeyInsert; |
| 164 |
} |
| 165 |
|
| 166 |
} |