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.index.Index; |
14 | import org.h2.message.DbException; |
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 | @Override |
39 | public int update() { |
40 | session.commit(true); |
41 | Database db = session.getDatabase(); |
42 | Schema schema = oldIndex.getSchema(); |
43 | if (schema.findIndex(session, newIndexName) != null || |
44 | newIndexName.equals(oldIndex.getName())) { |
45 | throw DbException.get(ErrorCode.INDEX_ALREADY_EXISTS_1, |
46 | newIndexName); |
47 | } |
48 | session.getUser().checkRight(oldIndex.getTable(), Right.ALL); |
49 | db.renameSchemaObject(session, oldIndex, newIndexName); |
50 | return 0; |
51 | } |
52 | |
53 | @Override |
54 | public int getType() { |
55 | return CommandInterface.ALTER_INDEX_RENAME; |
56 | } |
57 | |
58 | } |