| 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 org.h2.api.ErrorCode; |
| 10 |
import org.h2.command.CommandInterface; |
| 11 |
import org.h2.constraint.ConstraintActionType; |
| 12 |
import org.h2.engine.Database; |
| 13 |
import org.h2.engine.SessionLocal; |
| 14 |
import org.h2.message.DbException; |
| 15 |
import org.h2.schema.Schema; |
| 16 |
import org.h2.schema.SchemaObject; |
| 17 |
|
| 18 |
/** |
| 19 |
* This class represents the statement |
| 20 |
* DROP SCHEMA |
| 21 |
*/ |
| 22 |
public class DropSchema extends DefineCommand { |
| 23 |
|
| 24 |
private String schemaName; |
| 25 |
private boolean ifExists; |
| 26 |
private ConstraintActionType dropAction; |
| 27 |
|
| 28 |
public DropSchema(SessionLocal session) { |
| 29 |
super(session); |
| 30 |
dropAction = getDatabase().getSettings().dropRestrict ? |
| 31 |
ConstraintActionType.RESTRICT : ConstraintActionType.CASCADE; |
| 32 |
} |
| 33 |
|
| 34 |
public void setSchemaName(String name) { |
| 35 |
this.schemaName = name; |
| 36 |
} |
| 37 |
|
| 38 |
@Override |
| 39 |
public long update() { |
| 40 |
Database db = getDatabase(); |
| 41 |
Schema schema = db.findSchema(schemaName); |
| 42 |
if (schema == null) { |
| 43 |
if (!ifExists) { |
| 44 |
throw DbException.get(ErrorCode.SCHEMA_NOT_FOUND_1, schemaName); |
| 45 |
} |
| 46 |
} else { |
| 47 |
session.getUser().checkSchemaOwner(schema); |
| 48 |
if (!schema.canDrop()) { |
| 49 |
throw DbException.get(ErrorCode.SCHEMA_CAN_NOT_BE_DROPPED_1, schemaName); |
| 50 |
} |
| 51 |
if (dropAction == ConstraintActionType.RESTRICT && !schema.isEmpty()) { |
| 52 |
ArrayList<SchemaObject> all = schema.getAll(null); |
| 53 |
int size = all.size(); |
| 54 |
if (size > 0) { |
| 55 |
StringBuilder builder = new StringBuilder(); |
| 56 |
for (int i = 0; i < size; i++) { |
| 57 |
if (i > 0) { |
| 58 |
builder.append(", "); |
| 59 |
} |
| 60 |
builder.append(all.get(i).getName()); |
| 61 |
} |
| 62 |
throw DbException.get(ErrorCode.CANNOT_DROP_2, schemaName, builder.toString()); |
| 63 |
} |
| 64 |
} |
| 65 |
db.removeDatabaseObject(session, schema); |
| 66 |
} |
| 67 |
return 0; |
| 68 |
} |
| 69 |
|
| 70 |
public void setIfExists(boolean ifExists) { |
| 71 |
this.ifExists = ifExists; |
| 72 |
} |
| 73 |
|
| 74 |
public void setDropAction(ConstraintActionType dropAction) { |
| 75 |
this.dropAction = dropAction; |
| 76 |
} |
| 77 |
|
| 78 |
@Override |
| 79 |
public int getType() { |
| 80 |
return CommandInterface.DROP_SCHEMA; |
| 81 |
} |
| 82 |
|
| 83 |
} |