| 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.ddl; |
| 7 |
|
| 8 |
import java.util.ArrayList; |
| 9 |
import java.util.HashSet; |
| 10 |
import java.util.List; |
| 11 |
import java.util.concurrent.CopyOnWriteArrayList; |
| 12 |
|
| 13 |
import org.h2.api.ErrorCode; |
| 14 |
import org.h2.command.CommandInterface; |
| 15 |
import org.h2.constraint.Constraint; |
| 16 |
import org.h2.constraint.ConstraintActionType; |
| 17 |
import org.h2.engine.Database; |
| 18 |
import org.h2.engine.Right; |
| 19 |
import org.h2.engine.SessionLocal; |
| 20 |
import org.h2.message.DbException; |
| 21 |
import org.h2.schema.Schema; |
| 22 |
import org.h2.table.MaterializedView; |
| 23 |
import org.h2.table.Table; |
| 24 |
import org.h2.table.TableView; |
| 25 |
import org.h2.util.Utils; |
| 26 |
|
| 27 |
/** |
| 28 |
* This class represents the statement |
| 29 |
* DROP TABLE |
| 30 |
*/ |
| 31 |
public class DropTable extends DefineCommand { |
| 32 |
|
| 33 |
private boolean ifExists; |
| 34 |
private ConstraintActionType dropAction; |
| 35 |
|
| 36 |
private final ArrayList<SchemaAndTable> tables = Utils.newSmallArrayList(); |
| 37 |
|
| 38 |
public DropTable(SessionLocal session) { |
| 39 |
super(session); |
| 40 |
dropAction = getDatabase().getSettings().dropRestrict ? |
| 41 |
ConstraintActionType.RESTRICT : |
| 42 |
ConstraintActionType.CASCADE; |
| 43 |
} |
| 44 |
|
| 45 |
public void setIfExists(boolean b) { |
| 46 |
ifExists = b; |
| 47 |
} |
| 48 |
|
| 49 |
/** |
| 50 |
* Add a table to drop. |
| 51 |
* |
| 52 |
* @param schema the schema |
| 53 |
* @param tableName the table name |
| 54 |
*/ |
| 55 |
public void addTable(Schema schema, String tableName) { |
| 56 |
tables.add(new SchemaAndTable(schema, tableName)); |
| 57 |
} |
| 58 |
|
| 59 |
private boolean prepareDrop() { |
| 60 |
HashSet<Table> tablesToDrop = new HashSet<>(); |
| 61 |
for (SchemaAndTable schemaAndTable : tables) { |
| 62 |
String tableName = schemaAndTable.tableName; |
| 63 |
Table table = schemaAndTable.schema.findTableOrView(session, tableName); |
| 64 |
if (table == null) { |
| 65 |
if (!ifExists) { |
| 66 |
throw DbException.get(ErrorCode.TABLE_OR_VIEW_NOT_FOUND_1, tableName); |
| 67 |
} |
| 68 |
} else { |
| 69 |
session.getUser().checkTableRight(table, Right.SCHEMA_OWNER); |
| 70 |
if (!table.canDrop()) { |
| 71 |
throw DbException.get(ErrorCode.CANNOT_DROP_TABLE_1, tableName); |
| 72 |
} |
| 73 |
tablesToDrop.add(table); |
| 74 |
} |
| 75 |
} |
| 76 |
if (tablesToDrop.isEmpty()) { |
| 77 |
return false; |
| 78 |
} |
| 79 |
for (Table table : tablesToDrop) { |
| 80 |
ArrayList<String> dependencies = new ArrayList<>(); |
| 81 |
if (dropAction == ConstraintActionType.RESTRICT) { |
| 82 |
CopyOnWriteArrayList<TableView> dependentViews = table.getDependentViews(); |
| 83 |
if (dependentViews != null && !dependentViews.isEmpty()) { |
| 84 |
for (TableView v : dependentViews) { |
| 85 |
if (!tablesToDrop.contains(v)) { |
| 86 |
dependencies.add(v.getName()); |
| 87 |
} |
| 88 |
} |
| 89 |
} |
| 90 |
CopyOnWriteArrayList<MaterializedView> dependentMaterializedViews = table |
| 91 |
.getDependentMaterializedViews(); |
| 92 |
if (dependentMaterializedViews != null && !dependentMaterializedViews.isEmpty()) { |
| 93 |
for (MaterializedView v : dependentMaterializedViews) { |
| 94 |
if (!tablesToDrop.contains(v)) { |
| 95 |
dependencies.add(v.getName()); |
| 96 |
} |
| 97 |
} |
| 98 |
} |
| 99 |
final List<Constraint> constraints = table.getConstraints(); |
| 100 |
if (constraints != null && !constraints.isEmpty()) { |
| 101 |
for (Constraint c : constraints) { |
| 102 |
if (!tablesToDrop.contains(c.getTable())) { |
| 103 |
dependencies.add(c.getName()); |
| 104 |
} |
| 105 |
} |
| 106 |
} |
| 107 |
if (!dependencies.isEmpty()) { |
| 108 |
throw DbException.get(ErrorCode.CANNOT_DROP_2, table.getName(), |
| 109 |
String.join(", ", new HashSet<>(dependencies))); |
| 110 |
} |
| 111 |
} |
| 112 |
table.lock(session, Table.EXCLUSIVE_LOCK); |
| 113 |
} |
| 114 |
return true; |
| 115 |
} |
| 116 |
|
| 117 |
private void executeDrop() { |
| 118 |
for (SchemaAndTable schemaAndTable : tables) { |
| 119 |
// need to get the table again, because it may be dropped already |
| 120 |
// meanwhile (dependent object, or same object) |
| 121 |
Table table = schemaAndTable.schema.findTableOrView(session, schemaAndTable.tableName); |
| 122 |
if (table != null) { |
| 123 |
table.setModified(); |
| 124 |
Database db = getDatabase(); |
| 125 |
db.lockMeta(session); |
| 126 |
db.removeSchemaObject(session, table); |
| 127 |
} |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
@Override |
| 132 |
public long update() { |
| 133 |
if (prepareDrop()) { |
| 134 |
executeDrop(); |
| 135 |
} |
| 136 |
return 0; |
| 137 |
} |
| 138 |
|
| 139 |
public void setDropAction(ConstraintActionType dropAction) { |
| 140 |
this.dropAction = dropAction; |
| 141 |
} |
| 142 |
|
| 143 |
@Override |
| 144 |
public int getType() { |
| 145 |
return CommandInterface.DROP_TABLE; |
| 146 |
} |
| 147 |
|
| 148 |
private static final class SchemaAndTable { |
| 149 |
|
| 150 |
final Schema schema; |
| 151 |
|
| 152 |
final String tableName; |
| 153 |
|
| 154 |
SchemaAndTable(Schema schema, String tableName) { |
| 155 |
this.schema = schema; |
| 156 |
this.tableName = tableName; |
| 157 |
} |
| 158 |
|
| 159 |
} |
| 160 |
|
| 161 |
} |