| 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.query.AllColumnsForPlan; |
| 13 |
import org.h2.engine.DbObject; |
| 14 |
import org.h2.engine.Right; |
| 15 |
import org.h2.engine.SessionLocal; |
| 16 |
import org.h2.expression.Expression; |
| 17 |
import org.h2.expression.ExpressionVisitor; |
| 18 |
import org.h2.message.DbException; |
| 19 |
import org.h2.result.LocalResult; |
| 20 |
import org.h2.result.ResultTarget; |
| 21 |
import org.h2.result.Row; |
| 22 |
import org.h2.table.DataChangeDeltaTable.ResultOption; |
| 23 |
import org.h2.table.PlanItem; |
| 24 |
import org.h2.table.Table; |
| 25 |
import org.h2.table.TableFilter; |
| 26 |
import org.h2.value.Value; |
| 27 |
import org.h2.value.ValueNull; |
| 28 |
|
| 29 |
/** |
| 30 |
* This class represents the statement |
| 31 |
* DELETE |
| 32 |
*/ |
| 33 |
public final class Delete extends FilteredDataChangeStatement { |
| 34 |
|
| 35 |
public Delete(SessionLocal session) { |
| 36 |
super(session); |
| 37 |
} |
| 38 |
|
| 39 |
|
| 40 |
@Override |
| 41 |
public long update(ResultTarget deltaChangeCollector, ResultOption deltaChangeCollectionMode) { |
| 42 |
targetTableFilter.startQuery(session); |
| 43 |
targetTableFilter.reset(); |
| 44 |
Table table = targetTableFilter.getTable(); |
| 45 |
session.getUser().checkTableRight(table, Right.DELETE); |
| 46 |
table.fire(session, Trigger.DELETE, true); |
| 47 |
table.lock(session, Table.WRITE_LOCK); |
| 48 |
long limitRows = -1; |
| 49 |
if (fetchExpr != null) { |
| 50 |
Value v = fetchExpr.getValue(session); |
| 51 |
if (v == ValueNull.INSTANCE || (limitRows = v.getLong()) < 0) { |
| 52 |
throw DbException.getInvalidValueException("FETCH", v); |
| 53 |
} |
| 54 |
} |
| 55 |
try (LocalResult rows = LocalResult.forTable(session, table)) { |
| 56 |
setCurrentRowNumber(0); |
| 57 |
long count = 0; |
| 58 |
while (nextRow(limitRows, count)) { |
| 59 |
Row row = targetTableFilter.get(); |
| 60 |
if (table.isRowLockable()) { |
| 61 |
Row lockedRow = table.lockRow(session, row, -1); |
| 62 |
if (lockedRow == null) { |
| 63 |
continue; |
| 64 |
} |
| 65 |
if (!row.hasSharedData(lockedRow)) { |
| 66 |
row = lockedRow; |
| 67 |
targetTableFilter.set(row); |
| 68 |
if (condition != null && !condition.getBooleanValue(session)) { |
| 69 |
continue; |
| 70 |
} |
| 71 |
} |
| 72 |
} |
| 73 |
if (deltaChangeCollectionMode == ResultOption.OLD) { |
| 74 |
deltaChangeCollector.addRow(row.getValueList()); |
| 75 |
} |
| 76 |
if (!table.fireRow() || !table.fireBeforeRow(session, row, null)) { |
| 77 |
rows.addRowForTable(row); |
| 78 |
} |
| 79 |
count++; |
| 80 |
} |
| 81 |
rows.done(); |
| 82 |
long rowScanCount = 0; |
| 83 |
while (rows.next()) { |
| 84 |
if ((++rowScanCount & 127) == 0) { |
| 85 |
checkCanceled(); |
| 86 |
} |
| 87 |
Row row = rows.currentRowForTable(); |
| 88 |
table.removeRow(session, row); |
| 89 |
} |
| 90 |
if (table.fireRow()) { |
| 91 |
for (rows.reset(); rows.next();) { |
| 92 |
table.fireAfterRow(session, rows.currentRowForTable(), null, false); |
| 93 |
} |
| 94 |
} |
| 95 |
table.fire(session, Trigger.DELETE, false); |
| 96 |
return count; |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
@Override |
| 101 |
public String getPlanSQL(int sqlFlags) { |
| 102 |
StringBuilder builder = new StringBuilder("DELETE FROM "); |
| 103 |
targetTableFilter.getPlanSQL(builder, false, sqlFlags); |
| 104 |
appendFilterCondition(builder, sqlFlags); |
| 105 |
return builder.toString(); |
| 106 |
} |
| 107 |
|
| 108 |
@Override |
| 109 |
void doPrepare() { |
| 110 |
if (condition != null) { |
| 111 |
condition.mapColumns(targetTableFilter, 0, Expression.MAP_INITIAL); |
| 112 |
condition = condition.optimizeCondition(session); |
| 113 |
if (condition != null) { |
| 114 |
condition.createIndexConditions(session, targetTableFilter); |
| 115 |
} |
| 116 |
} |
| 117 |
TableFilter[] filters = new TableFilter[] { targetTableFilter }; |
| 118 |
PlanItem item = targetTableFilter.getBestPlanItem(session, filters, 0, new AllColumnsForPlan(filters)); |
| 119 |
targetTableFilter.setPlanItem(item); |
| 120 |
targetTableFilter.prepare(); |
| 121 |
} |
| 122 |
|
| 123 |
@Override |
| 124 |
public int getType() { |
| 125 |
return CommandInterface.DELETE; |
| 126 |
} |
| 127 |
|
| 128 |
@Override |
| 129 |
public String getStatementName() { |
| 130 |
return "DELETE"; |
| 131 |
} |
| 132 |
|
| 133 |
@Override |
| 134 |
public void collectDependencies(HashSet<DbObject> dependencies) { |
| 135 |
ExpressionVisitor visitor = ExpressionVisitor.getDependenciesVisitor(dependencies); |
| 136 |
if (condition != null) { |
| 137 |
condition.isEverything(visitor); |
| 138 |
} |
| 139 |
} |
| 140 |
|
| 141 |
} |