KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > h2 > command > ddl > AlterTableRename


1 /*
2  * Copyright 2004-2006 H2 Group. Licensed under the H2 License, Version 1.0 (http://h2database.com/html/license.html).
3  * Initial Developer: H2 Group
4  */

5 package org.h2.command.ddl;
6
7 import java.sql.SQLException JavaDoc;
8
9 import org.h2.engine.Database;
10 import org.h2.engine.Right;
11 import org.h2.engine.Session;
12 import org.h2.message.Message;
13 import org.h2.schema.Schema;
14 import org.h2.table.Table;
15
16
17 /**
18  * @author Thomas
19  */

20 public class AlterTableRename extends SchemaCommand {
21
22     private Table oldTable;
23     private String JavaDoc newTableName;
24
25     public AlterTableRename(Session session, Schema schema) {
26         super(session, schema);
27     }
28
29     public void setOldTable(Table table) {
30         oldTable = table;
31     }
32
33     public void setNewTableName(String JavaDoc name) {
34         newTableName = name;
35     }
36
37     public int update() throws SQLException JavaDoc {
38         session.commit();
39         Database db = session.getDatabase();
40         if(getSchema().findTableOrView(session, newTableName) != null || newTableName.equals(oldTable.getName())) {
41             throw Message.getSQLException(Message.TABLE_OR_VIEW_ALREADY_EXISTS_1, newTableName);
42         }
43         session.getUser().checkRight(oldTable, Right.ALL);
44         if(oldTable.getTemporary()) {
45             // TODO renaming a temporary table is not supported
46
throw Message.getUnsupportedException();
47         }
48         db.renameSchemaObject(session, oldTable, newTableName);
49         return 0;
50     }
51
52 }
53
Popular Tags