01: /*
02: * Copyright 2004-2008 H2 Group. Licensed under the H2 License, Version 1.0
03: * (http://h2database.com/html/license.html).
04: * Initial Developer: H2 Group
05: */
06: package org.h2.command.ddl;
07:
08: import java.sql.SQLException;
09:
10: import org.h2.constant.ErrorCode;
11: import org.h2.engine.Database;
12: import org.h2.engine.Right;
13: import org.h2.engine.Session;
14: import org.h2.message.Message;
15: import org.h2.schema.Schema;
16: import org.h2.table.Table;
17:
18: /**
19: * This class represents the statement
20: * ALTER TABLE RENAME
21: */
22: public class AlterTableRename extends SchemaCommand {
23:
24: private Table oldTable;
25: private String newTableName;
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: public int update() throws SQLException {
40: session.commit(true);
41: Database db = session.getDatabase();
42: if (getSchema().findTableOrView(session, newTableName) != null
43: || newTableName.equals(oldTable.getName())) {
44: throw Message.getSQLException(
45: ErrorCode.TABLE_OR_VIEW_ALREADY_EXISTS_1,
46: newTableName);
47: }
48: session.getUser().checkRight(oldTable, Right.ALL);
49: if (oldTable.getTemporary()) {
50: // TODO renaming a temporary table is not supported
51: throw Message.getUnsupportedException();
52: }
53: db.renameSchemaObject(session, oldTable, newTableName);
54: return 0;
55: }
56:
57: }
|