| 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.Table; |
| 15 |
|
| 16 |
/** |
| 17 |
* This class represents the statement |
| 18 |
* ALTER TABLE RENAME |
| 19 |
*/ |
| 20 |
public class AlterTableRename extends AlterTable { |
| 21 |
|
| 22 |
private String newTableName; |
| 23 |
private boolean hidden; |
| 24 |
|
| 25 |
public AlterTableRename(SessionLocal session, Schema schema) { |
| 26 |
super(session, schema); |
| 27 |
} |
| 28 |
|
| 29 |
public void setNewTableName(String name) { |
| 30 |
newTableName = name; |
| 31 |
} |
| 32 |
|
| 33 |
@Override |
| 34 |
public long update(Table table) { |
| 35 |
Database db = getDatabase(); |
| 36 |
Table t = getSchema().findTableOrView(session, newTableName); |
| 37 |
if (t != null && hidden && newTableName.equals(table.getName())) { |
| 38 |
if (!t.isHidden()) { |
| 39 |
t.setHidden(hidden); |
| 40 |
table.setHidden(true); |
| 41 |
db.updateMeta(session, table); |
| 42 |
} |
| 43 |
return 0; |
| 44 |
} |
| 45 |
if (t != null || newTableName.equals(table.getName())) { |
| 46 |
throw DbException.get(ErrorCode.TABLE_OR_VIEW_ALREADY_EXISTS_1, newTableName); |
| 47 |
} |
| 48 |
if (table.isTemporary()) { |
| 49 |
throw DbException.getUnsupportedException("temp table"); |
| 50 |
} |
| 51 |
db.renameSchemaObject(session, table, newTableName); |
| 52 |
return 0; |
| 53 |
} |
| 54 |
|
| 55 |
@Override |
| 56 |
public int getType() { |
| 57 |
return CommandInterface.ALTER_TABLE_RENAME; |
| 58 |
} |
| 59 |
|
| 60 |
public void setHidden(boolean hidden) { |
| 61 |
this.hidden = hidden; |
| 62 |
} |
| 63 |
|
| 64 |
} |