| 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 org.h2.api.ErrorCode; |
| 11 |
import org.h2.command.CommandInterface; |
| 12 |
import org.h2.command.dml.Insert; |
| 13 |
import org.h2.command.query.Query; |
| 14 |
import org.h2.engine.Database; |
| 15 |
import org.h2.engine.DbObject; |
| 16 |
import org.h2.engine.SessionLocal; |
| 17 |
import org.h2.expression.Expression; |
| 18 |
import org.h2.message.DbException; |
| 19 |
import org.h2.schema.Schema; |
| 20 |
import org.h2.schema.Sequence; |
| 21 |
import org.h2.table.Column; |
| 22 |
import org.h2.table.Table; |
| 23 |
import org.h2.value.Value; |
| 24 |
|
| 25 |
/** |
| 26 |
* This class represents the statement |
| 27 |
* CREATE TABLE |
| 28 |
*/ |
| 29 |
public class CreateTable extends CommandWithColumns { |
| 30 |
|
| 31 |
private final CreateTableData data = new CreateTableData(); |
| 32 |
private boolean ifNotExists; |
| 33 |
private boolean onCommitDrop; |
| 34 |
private boolean onCommitTruncate; |
| 35 |
private Query asQuery; |
| 36 |
private String comment; |
| 37 |
private boolean withNoData; |
| 38 |
|
| 39 |
public CreateTable(SessionLocal session, Schema schema) { |
| 40 |
super(session, schema); |
| 41 |
data.persistIndexes = true; |
| 42 |
data.persistData = true; |
| 43 |
} |
| 44 |
|
| 45 |
public void setQuery(Query query) { |
| 46 |
this.asQuery = query; |
| 47 |
} |
| 48 |
|
| 49 |
public void setTemporary(boolean temporary) { |
| 50 |
data.temporary = temporary; |
| 51 |
} |
| 52 |
|
| 53 |
public void setTableName(String tableName) { |
| 54 |
data.tableName = tableName; |
| 55 |
} |
| 56 |
|
| 57 |
@Override |
| 58 |
public void addColumn(Column column) { |
| 59 |
data.columns.add(column); |
| 60 |
} |
| 61 |
|
| 62 |
public ArrayList<Column> getColumns() { |
| 63 |
return data.columns; |
| 64 |
} |
| 65 |
|
| 66 |
public void setIfNotExists(boolean ifNotExists) { |
| 67 |
this.ifNotExists = ifNotExists; |
| 68 |
} |
| 69 |
|
| 70 |
@Override |
| 71 |
public long update() { |
| 72 |
Schema schema = getSchema(); |
| 73 |
boolean isSessionTemporary = data.temporary && !data.globalTemporary; |
| 74 |
Database db = getDatabase(); |
| 75 |
String tableEngine = data.tableEngine; |
| 76 |
if (tableEngine != null || db.getSettings().defaultTableEngine != null) { |
| 77 |
session.getUser().checkAdmin(); |
| 78 |
} else if (!isSessionTemporary) { |
| 79 |
session.getUser().checkSchemaOwner(schema); |
| 80 |
} |
| 81 |
if (!db.isPersistent()) { |
| 82 |
data.persistIndexes = false; |
| 83 |
} |
| 84 |
if (!isSessionTemporary) { |
| 85 |
db.lockMeta(session); |
| 86 |
} |
| 87 |
if (schema.resolveTableOrView(session, data.tableName) != null) { |
| 88 |
if (ifNotExists) { |
| 89 |
return 0; |
| 90 |
} |
| 91 |
throw DbException.get(ErrorCode.TABLE_OR_VIEW_ALREADY_EXISTS_1, data.tableName); |
| 92 |
} |
| 93 |
if (asQuery != null) { |
| 94 |
asQuery.prepare(); |
| 95 |
if (data.columns.isEmpty()) { |
| 96 |
generateColumnsFromQuery(); |
| 97 |
} else if (data.columns.size() != asQuery.getColumnCount()) { |
| 98 |
throw DbException.get(ErrorCode.COLUMN_COUNT_DOES_NOT_MATCH); |
| 99 |
} else { |
| 100 |
ArrayList<Column> columns = data.columns; |
| 101 |
for (int i = 0; i < columns.size(); i++) { |
| 102 |
Column column = columns.get(i); |
| 103 |
if (column.getType().getValueType() == Value.UNKNOWN) { |
| 104 |
columns.set(i, new Column(column.getName(), asQuery.getExpressions().get(i).getType())); |
| 105 |
} |
| 106 |
} |
| 107 |
} |
| 108 |
} |
| 109 |
changePrimaryKeysToNotNull(data.columns); |
| 110 |
data.id = getObjectId(); |
| 111 |
data.session = session; |
| 112 |
Table table = schema.createTable(data); |
| 113 |
ArrayList<Sequence> sequences = generateSequences(data.columns, data.temporary); |
| 114 |
table.setComment(comment); |
| 115 |
if (isSessionTemporary) { |
| 116 |
if (onCommitDrop) { |
| 117 |
table.setOnCommitDrop(true); |
| 118 |
} |
| 119 |
if (onCommitTruncate) { |
| 120 |
table.setOnCommitTruncate(true); |
| 121 |
} |
| 122 |
session.addLocalTempTable(table); |
| 123 |
} else { |
| 124 |
db.lockMeta(session); |
| 125 |
db.addSchemaObject(session, table); |
| 126 |
} |
| 127 |
try { |
| 128 |
for (Column c : data.columns) { |
| 129 |
c.prepareExpressions(session); |
| 130 |
} |
| 131 |
for (Sequence sequence : sequences) { |
| 132 |
table.addSequence(sequence); |
| 133 |
} |
| 134 |
createConstraints(); |
| 135 |
HashSet<DbObject> set = new HashSet<>(); |
| 136 |
table.addDependencies(set); |
| 137 |
for (DbObject obj : set) { |
| 138 |
if (obj == table) { |
| 139 |
continue; |
| 140 |
} |
| 141 |
if (obj.getType() == DbObject.TABLE_OR_VIEW) { |
| 142 |
if (obj instanceof Table) { |
| 143 |
Table t = (Table) obj; |
| 144 |
if (t.getId() > table.getId()) { |
| 145 |
throw DbException.get( |
| 146 |
ErrorCode.FEATURE_NOT_SUPPORTED_1, |
| 147 |
"Table depends on another table " + |
| 148 |
"with a higher ID: " + t + |
| 149 |
", this is currently not supported, " + |
| 150 |
"as it would prevent the database from " + |
| 151 |
"being re-opened"); |
| 152 |
} |
| 153 |
} |
| 154 |
} |
| 155 |
} |
| 156 |
if (asQuery != null && !withNoData) { |
| 157 |
insertAsData(isSessionTemporary, db, table); |
| 158 |
} |
| 159 |
} catch (DbException e) { |
| 160 |
try { |
| 161 |
db.checkPowerOff(); |
| 162 |
db.removeSchemaObject(session, table); |
| 163 |
if (!transactional) { |
| 164 |
session.commit(true); |
| 165 |
} |
| 166 |
} catch (Throwable ex) { |
| 167 |
e.addSuppressed(ex); |
| 168 |
} |
| 169 |
throw e; |
| 170 |
} |
| 171 |
return 0; |
| 172 |
} |
| 173 |
|
| 174 |
/** This is called from REFRESH MATERIALIZED VIEW */ |
| 175 |
void insertAsData(Table table) { |
| 176 |
insertAsData(false, getDatabase(), table); |
| 177 |
} |
| 178 |
|
| 179 |
/** Insert data for the CREATE TABLE .. AS */ |
| 180 |
private void insertAsData(boolean isSessionTemporary, Database db, Table table) { |
| 181 |
boolean flushSequences = false; |
| 182 |
if (!isSessionTemporary) { |
| 183 |
db.unlockMeta(session); |
| 184 |
for (Column c : table.getColumns()) { |
| 185 |
Sequence s = c.getSequence(); |
| 186 |
if (s != null) { |
| 187 |
flushSequences = true; |
| 188 |
s.setTemporary(true); |
| 189 |
} |
| 190 |
} |
| 191 |
} |
| 192 |
try { |
| 193 |
session.startStatementWithinTransaction(null); |
| 194 |
Insert insert = new Insert(session); |
| 195 |
insert.setQuery(asQuery); |
| 196 |
insert.setTable(table); |
| 197 |
insert.setInsertFromSelect(true); |
| 198 |
insert.prepare(); |
| 199 |
insert.update(); |
| 200 |
} finally { |
| 201 |
session.endStatement(); |
| 202 |
} |
| 203 |
if (flushSequences) { |
| 204 |
db.lockMeta(session); |
| 205 |
for (Column c : table.getColumns()) { |
| 206 |
Sequence s = c.getSequence(); |
| 207 |
if (s != null) { |
| 208 |
s.setTemporary(false); |
| 209 |
s.flush(session); |
| 210 |
} |
| 211 |
} |
| 212 |
} |
| 213 |
} |
| 214 |
|
| 215 |
private void generateColumnsFromQuery() { |
| 216 |
int columnCount = asQuery.getColumnCount(); |
| 217 |
ArrayList<Expression> expressions = asQuery.getExpressions(); |
| 218 |
for (int i = 0; i < columnCount; i++) { |
| 219 |
Expression expr = expressions.get(i); |
| 220 |
addColumn(new Column(expr.getColumnNameForView(session, i), expr.getType())); |
| 221 |
} |
| 222 |
} |
| 223 |
|
| 224 |
public void setPersistIndexes(boolean persistIndexes) { |
| 225 |
data.persistIndexes = persistIndexes; |
| 226 |
} |
| 227 |
|
| 228 |
public void setGlobalTemporary(boolean globalTemporary) { |
| 229 |
data.globalTemporary = globalTemporary; |
| 230 |
} |
| 231 |
|
| 232 |
/** |
| 233 |
* This temporary table is dropped on commit. |
| 234 |
*/ |
| 235 |
public void setOnCommitDrop() { |
| 236 |
this.onCommitDrop = true; |
| 237 |
} |
| 238 |
|
| 239 |
/** |
| 240 |
* This temporary table is truncated on commit. |
| 241 |
*/ |
| 242 |
public void setOnCommitTruncate() { |
| 243 |
this.onCommitTruncate = true; |
| 244 |
} |
| 245 |
|
| 246 |
public void setComment(String comment) { |
| 247 |
this.comment = comment; |
| 248 |
} |
| 249 |
|
| 250 |
public void setPersistData(boolean persistData) { |
| 251 |
data.persistData = persistData; |
| 252 |
if (!persistData) { |
| 253 |
data.persistIndexes = false; |
| 254 |
} |
| 255 |
} |
| 256 |
|
| 257 |
public void setWithNoData(boolean withNoData) { |
| 258 |
this.withNoData = withNoData; |
| 259 |
} |
| 260 |
|
| 261 |
public void setTableEngine(String tableEngine) { |
| 262 |
data.tableEngine = tableEngine; |
| 263 |
} |
| 264 |
|
| 265 |
public void setTableEngineParams(ArrayList<String> tableEngineParams) { |
| 266 |
data.tableEngineParams = tableEngineParams; |
| 267 |
} |
| 268 |
|
| 269 |
public void setHidden(boolean isHidden) { |
| 270 |
data.isHidden = isHidden; |
| 271 |
} |
| 272 |
|
| 273 |
@Override |
| 274 |
public int getType() { |
| 275 |
return CommandInterface.CREATE_TABLE; |
| 276 |
} |
| 277 |
|
| 278 |
} |