| 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 org.h2.api.ErrorCode; |
| 9 |
import org.h2.command.CommandInterface; |
| 10 |
import org.h2.engine.Database; |
| 11 |
import org.h2.engine.SessionLocal; |
| 12 |
import org.h2.message.DbException; |
| 13 |
import org.h2.schema.Schema; |
| 14 |
import org.h2.table.MaterializedView; |
| 15 |
import org.h2.table.Table; |
| 16 |
import org.h2.table.TableType; |
| 17 |
|
| 18 |
/** |
| 19 |
* This class represents the statement DROP MATERIALIZED VIEW |
| 20 |
*/ |
| 21 |
public class DropMaterializedView extends SchemaCommand { |
| 22 |
|
| 23 |
private String viewName; |
| 24 |
private boolean ifExists; |
| 25 |
|
| 26 |
public DropMaterializedView(SessionLocal session, Schema schema) { |
| 27 |
super(session, schema); |
| 28 |
} |
| 29 |
|
| 30 |
public void setIfExists(boolean b) { |
| 31 |
ifExists = b; |
| 32 |
} |
| 33 |
|
| 34 |
public void setViewName(String viewName) { |
| 35 |
this.viewName = viewName; |
| 36 |
} |
| 37 |
|
| 38 |
@Override |
| 39 |
public long update() { |
| 40 |
Table view = getSchema().findTableOrView(session, viewName); |
| 41 |
if (view == null) { |
| 42 |
if (!ifExists) { |
| 43 |
throw DbException.get(ErrorCode.VIEW_NOT_FOUND_1, viewName); |
| 44 |
} |
| 45 |
} else { |
| 46 |
if (TableType.MATERIALIZED_VIEW != view.getTableType()) { |
| 47 |
throw DbException.get(ErrorCode.VIEW_NOT_FOUND_1, viewName); |
| 48 |
} |
| 49 |
session.getUser().checkSchemaOwner(view.getSchema()); |
| 50 |
|
| 51 |
final MaterializedView materializedView = (MaterializedView) view; |
| 52 |
|
| 53 |
for (Table table : materializedView.getSelect().getTables()) { |
| 54 |
table.removeDependentMaterializedView(materializedView); |
| 55 |
} |
| 56 |
|
| 57 |
final Database database = getDatabase(); |
| 58 |
database.lockMeta(session); |
| 59 |
database.removeSchemaObject(session, view); |
| 60 |
|
| 61 |
// make sure its all unlocked |
| 62 |
database.unlockMeta(session); |
| 63 |
} |
| 64 |
return 0; |
| 65 |
} |
| 66 |
|
| 67 |
@Override |
| 68 |
public int getType() { |
| 69 |
return CommandInterface.DROP_MATERIALIZED_VIEW; |
| 70 |
} |
| 71 |
|
| 72 |
} |