| 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.command.query.Query; |
| 11 |
import org.h2.engine.Database; |
| 12 |
import org.h2.engine.SessionLocal; |
| 13 |
import org.h2.message.DbException; |
| 14 |
import org.h2.schema.Schema; |
| 15 |
import org.h2.table.MaterializedView; |
| 16 |
import org.h2.table.Table; |
| 17 |
import org.h2.table.TableType; |
| 18 |
|
| 19 |
/** |
| 20 |
* This class represents the statement CREATE MATERIALIZED VIEW |
| 21 |
*/ |
| 22 |
public class CreateMaterializedView extends SchemaOwnerCommand { |
| 23 |
|
| 24 |
/** Re-use the CREATE TABLE functionality to avoid duplicating a bunch of logic */ |
| 25 |
private final CreateTable createTable; |
| 26 |
private boolean orReplace; |
| 27 |
private boolean ifNotExists; |
| 28 |
private String viewName; |
| 29 |
private String comment; |
| 30 |
private Query select; |
| 31 |
private String selectSQL; |
| 32 |
|
| 33 |
public CreateMaterializedView(SessionLocal session, Schema schema) { |
| 34 |
super(session, schema); |
| 35 |
createTable = new CreateTable(session, schema); |
| 36 |
} |
| 37 |
|
| 38 |
public void setViewName(String name) { |
| 39 |
this.viewName = name; |
| 40 |
this.createTable.setTableName(name + "$1"); |
| 41 |
} |
| 42 |
|
| 43 |
public void setComment(String comment) { |
| 44 |
this.comment = comment; |
| 45 |
} |
| 46 |
|
| 47 |
public void setSelectSQL(String selectSQL) { |
| 48 |
this.selectSQL = selectSQL; |
| 49 |
} |
| 50 |
|
| 51 |
public void setIfNotExists(boolean ifNotExists) { |
| 52 |
this.ifNotExists = ifNotExists; |
| 53 |
this.createTable.setIfNotExists(ifNotExists); |
| 54 |
} |
| 55 |
|
| 56 |
public void setSelect(Query query) { |
| 57 |
this.select = query; |
| 58 |
this.createTable.setQuery(query); |
| 59 |
} |
| 60 |
|
| 61 |
public void setOrReplace(boolean orReplace) { |
| 62 |
this.orReplace = orReplace; |
| 63 |
} |
| 64 |
|
| 65 |
@Override |
| 66 |
long update(Schema schema) { |
| 67 |
final Database db = getDatabase(); |
| 68 |
final Table old = schema.findTableOrView(session, viewName); |
| 69 |
MaterializedView view = null; |
| 70 |
if (old != null) { |
| 71 |
if (ifNotExists) { |
| 72 |
return 0; |
| 73 |
} |
| 74 |
if (!orReplace || TableType.MATERIALIZED_VIEW != old.getTableType()) { |
| 75 |
throw DbException.get(ErrorCode.VIEW_ALREADY_EXISTS_1, viewName); |
| 76 |
} |
| 77 |
view = (MaterializedView) old; |
| 78 |
} |
| 79 |
final int id = getObjectId(); |
| 80 |
// Re-use the CREATE TABLE functionality to avoid duplicating a bunch of logic. |
| 81 |
createTable.update(); |
| 82 |
// Look up the freshly created table. |
| 83 |
final Table underlyingTable = schema.getTableOrView(session, viewName + "$1"); |
| 84 |
if (view == null) { |
| 85 |
view = new MaterializedView(schema, id, viewName, underlyingTable, select, selectSQL); |
| 86 |
} else { |
| 87 |
view.replace(underlyingTable, select, selectSQL); |
| 88 |
view.setModified(); |
| 89 |
} |
| 90 |
if (comment != null) { |
| 91 |
view.setComment(comment); |
| 92 |
} |
| 93 |
for (Table table : select.getTables()) { |
| 94 |
table.addDependentMaterializedView(view); |
| 95 |
} |
| 96 |
if (old == null) { |
| 97 |
db.addSchemaObject(session, view); |
| 98 |
db.unlockMeta(session); |
| 99 |
} else { |
| 100 |
db.updateMeta(session, view); |
| 101 |
} |
| 102 |
return 0; |
| 103 |
} |
| 104 |
|
| 105 |
@Override |
| 106 |
public int getType() { |
| 107 |
return CommandInterface.CREATE_MATERIALIZED_VIEW; |
| 108 |
} |
| 109 |
|
| 110 |
} |