| 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.command.query.Query; |
| 12 |
import org.h2.engine.Database; |
| 13 |
import org.h2.engine.SessionLocal; |
| 14 |
import org.h2.expression.Parameter; |
| 15 |
import org.h2.message.DbException; |
| 16 |
import org.h2.schema.Schema; |
| 17 |
import org.h2.table.Column; |
| 18 |
import org.h2.table.Table; |
| 19 |
import org.h2.table.TableType; |
| 20 |
import org.h2.table.TableView; |
| 21 |
import org.h2.util.HasSQL; |
| 22 |
import org.h2.value.TypeInfo; |
| 23 |
|
| 24 |
/** |
| 25 |
* This class represents the statement |
| 26 |
* CREATE VIEW |
| 27 |
*/ |
| 28 |
public class CreateView extends SchemaOwnerCommand { |
| 29 |
|
| 30 |
private Query select; |
| 31 |
private String viewName; |
| 32 |
private boolean ifNotExists; |
| 33 |
private String selectSQL; |
| 34 |
private String[] columnNames; |
| 35 |
private String comment; |
| 36 |
private boolean orReplace; |
| 37 |
private boolean force; |
| 38 |
private boolean isTableExpression; |
| 39 |
|
| 40 |
public CreateView(SessionLocal session, Schema schema) { |
| 41 |
super(session, schema); |
| 42 |
} |
| 43 |
|
| 44 |
public void setViewName(String name) { |
| 45 |
viewName = name; |
| 46 |
} |
| 47 |
|
| 48 |
public void setSelect(Query select) { |
| 49 |
this.select = select; |
| 50 |
} |
| 51 |
|
| 52 |
public void setIfNotExists(boolean ifNotExists) { |
| 53 |
this.ifNotExists = ifNotExists; |
| 54 |
} |
| 55 |
|
| 56 |
public void setSelectSQL(String selectSQL) { |
| 57 |
this.selectSQL = selectSQL; |
| 58 |
} |
| 59 |
|
| 60 |
public void setColumnNames(String[] cols) { |
| 61 |
this.columnNames = cols; |
| 62 |
} |
| 63 |
|
| 64 |
public void setComment(String comment) { |
| 65 |
this.comment = comment; |
| 66 |
} |
| 67 |
|
| 68 |
public void setOrReplace(boolean orReplace) { |
| 69 |
this.orReplace = orReplace; |
| 70 |
} |
| 71 |
|
| 72 |
public void setForce(boolean force) { |
| 73 |
this.force = force; |
| 74 |
} |
| 75 |
|
| 76 |
public void setTableExpression(boolean isTableExpression) { |
| 77 |
this.isTableExpression = isTableExpression; |
| 78 |
} |
| 79 |
|
| 80 |
@Override |
| 81 |
long update(Schema schema) { |
| 82 |
Database db = getDatabase(); |
| 83 |
TableView view = null; |
| 84 |
Table old = schema.findTableOrView(session, viewName); |
| 85 |
if (old != null) { |
| 86 |
if (ifNotExists) { |
| 87 |
return 0; |
| 88 |
} |
| 89 |
if (!orReplace || TableType.VIEW != old.getTableType()) { |
| 90 |
throw DbException.get(ErrorCode.VIEW_ALREADY_EXISTS_1, viewName); |
| 91 |
} |
| 92 |
view = (TableView) old; |
| 93 |
} |
| 94 |
int id = getObjectId(); |
| 95 |
String querySQL; |
| 96 |
if (select == null) { |
| 97 |
querySQL = selectSQL; |
| 98 |
} else { |
| 99 |
ArrayList<Parameter> params = select.getParameters(); |
| 100 |
if (params != null && !params.isEmpty()) { |
| 101 |
throw DbException.getUnsupportedException("parameters in views"); |
| 102 |
} |
| 103 |
querySQL = select.getPlanSQL(HasSQL.DEFAULT_SQL_FLAGS); |
| 104 |
} |
| 105 |
Column[] columnTemplatesAsUnknowns = null; |
| 106 |
Column[] columnTemplatesAsStrings = null; |
| 107 |
if (columnNames != null) { |
| 108 |
columnTemplatesAsUnknowns = new Column[columnNames.length]; |
| 109 |
columnTemplatesAsStrings = new Column[columnNames.length]; |
| 110 |
for (int i = 0; i < columnNames.length; ++i) { |
| 111 |
// non table expressions are fine to use unknown column type |
| 112 |
columnTemplatesAsUnknowns[i] = new Column(columnNames[i], TypeInfo.TYPE_UNKNOWN); |
| 113 |
// table expressions can't have unknown types - so we use string instead |
| 114 |
columnTemplatesAsStrings[i] = new Column(columnNames[i], TypeInfo.TYPE_VARCHAR); |
| 115 |
} |
| 116 |
} |
| 117 |
if (view == null) { |
| 118 |
if (isTableExpression) { |
| 119 |
view = TableView.createTableViewMaybeRecursive(schema, id, viewName, querySQL, null, |
| 120 |
columnTemplatesAsStrings, session, false /* literalsChecked */, isTableExpression, |
| 121 |
false/*isTemporary*/, db); |
| 122 |
} else { |
| 123 |
view = new TableView(schema, id, viewName, querySQL, null, columnTemplatesAsUnknowns, session, |
| 124 |
false, false, isTableExpression, false); |
| 125 |
} |
| 126 |
} else { |
| 127 |
// TODO support isTableExpression in replace function... |
| 128 |
view.replace(querySQL, columnTemplatesAsUnknowns, session, false, force, false); |
| 129 |
view.setModified(); |
| 130 |
} |
| 131 |
if (comment != null) { |
| 132 |
view.setComment(comment); |
| 133 |
} |
| 134 |
if (old == null) { |
| 135 |
db.addSchemaObject(session, view); |
| 136 |
db.unlockMeta(session); |
| 137 |
} else { |
| 138 |
db.updateMeta(session, view); |
| 139 |
} |
| 140 |
|
| 141 |
// TODO: if we added any table expressions that aren't used by this view, detect them |
| 142 |
// and drop them - otherwise they will leak and never get cleaned up. |
| 143 |
|
| 144 |
return 0; |
| 145 |
} |
| 146 |
|
| 147 |
@Override |
| 148 |
public int getType() { |
| 149 |
return CommandInterface.CREATE_VIEW; |
| 150 |
} |
| 151 |
|
| 152 |
} |