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: import org.h2.constant.ErrorCode;
10: import org.h2.engine.Database;
11: import org.h2.engine.Right;
12: import org.h2.engine.Session;
13: import org.h2.index.Index;
14: import org.h2.message.Message;
15: import org.h2.schema.Schema;
16:
17: /**
18: * This class represents the statement
19: * ALTER INDEX RENAME
20: */
21: public class AlterIndexRename extends DefineCommand {
22:
23: private Index oldIndex;
24: private String newIndexName;
25:
26: public AlterIndexRename(Session session) {
27: super (session);
28: }
29:
30: public void setOldIndex(Index index) {
31: oldIndex = index;
32: }
33:
34: public void setNewName(String name) {
35: newIndexName = name;
36: }
37:
38: public int update() throws SQLException {
39: session.commit(true);
40: Database db = session.getDatabase();
41: Schema schema = oldIndex.getSchema();
42: if (schema.findIndex(newIndexName) != null
43: || newIndexName.equals(oldIndex.getName())) {
44: throw Message.getSQLException(
45: ErrorCode.INDEX_ALREADY_EXISTS_1, newIndexName);
46: }
47: session.getUser().checkRight(oldIndex.getTable(), Right.ALL);
48: db.renameSchemaObject(session, oldIndex, newIndexName);
49: return 0;
50: }
51:
52: }
|