1 | /* |
2 | * Copyright 2004-2014 H2 Group. Multiple-Licensed under the MPL 2.0, |
3 | * and the EPL 1.0 (http://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.Right; |
12 | import org.h2.engine.Session; |
13 | import org.h2.message.DbException; |
14 | import org.h2.schema.Schema; |
15 | import org.h2.table.Table; |
16 | |
17 | /** |
18 | * This class represents the statement |
19 | * ALTER TABLE RENAME |
20 | */ |
21 | public class AlterTableRename extends SchemaCommand { |
22 | |
23 | private Table oldTable; |
24 | private String newTableName; |
25 | private boolean hidden; |
26 | |
27 | public AlterTableRename(Session session, Schema schema) { |
28 | super(session, schema); |
29 | } |
30 | |
31 | public void setOldTable(Table table) { |
32 | oldTable = table; |
33 | } |
34 | |
35 | public void setNewTableName(String name) { |
36 | newTableName = name; |
37 | } |
38 | |
39 | @Override |
40 | public int update() { |
41 | session.commit(true); |
42 | Database db = session.getDatabase(); |
43 | session.getUser().checkRight(oldTable, Right.ALL); |
44 | Table t = getSchema().findTableOrView(session, newTableName); |
45 | if (t != null && hidden && newTableName.equals(oldTable.getName())) { |
46 | if (!t.isHidden()) { |
47 | t.setHidden(hidden); |
48 | oldTable.setHidden(true); |
49 | db.updateMeta(session, oldTable); |
50 | } |
51 | return 0; |
52 | } |
53 | if (t != null || newTableName.equals(oldTable.getName())) { |
54 | throw DbException.get(ErrorCode.TABLE_OR_VIEW_ALREADY_EXISTS_1, newTableName); |
55 | } |
56 | if (oldTable.isTemporary()) { |
57 | throw DbException.getUnsupportedException("temp table"); |
58 | } |
59 | db.renameSchemaObject(session, oldTable, newTableName); |
60 | return 0; |
61 | } |
62 | |
63 | @Override |
64 | public int getType() { |
65 | return CommandInterface.ALTER_TABLE_RENAME; |
66 | } |
67 | |
68 | public void setHidden(boolean hidden) { |
69 | this.hidden = hidden; |
70 | } |
71 | |
72 | } |