Debian Codegraph @

src/main/org/h2/command/ddl/AlterIndexRename.java

raw
1 /*
2 * Copyright 2004-2023 H2 Group. Multiple-Licensed under the MPL 2.0,
3 * and the EPL 1.0 (https://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.SessionLocal;
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 boolean ifExists;
24 private Schema oldSchema;
25 private String oldIndexName;
26 private String newIndexName;
27
28 public AlterIndexRename(SessionLocal session) {
29 super(session);
30 }
31
32 public void setIfExists(boolean b) {
33 ifExists = b;
34 }
35
36 public void setOldSchema(Schema old) {
37 oldSchema = old;
38 }
39
40 public void setOldName(String name) {
41 oldIndexName = name;
42 }
43
44 public void setNewName(String name) {
45 newIndexName = name;
46 }
47
48 @Override
49 public long update() {
50 Database db = getDatabase();
51 Index oldIndex = oldSchema.findIndex(session, oldIndexName);
52 if (oldIndex == null) {
53 if (!ifExists) {
54 throw DbException.get(ErrorCode.INDEX_NOT_FOUND_1,
55 newIndexName);
56 }
57 return 0;
58 }
59 if (oldSchema.findIndex(session, newIndexName) != null ||
60 newIndexName.equals(oldIndexName)) {
61 throw DbException.get(ErrorCode.INDEX_ALREADY_EXISTS_1,
62 newIndexName);
63 }
64 session.getUser().checkTableRight(oldIndex.getTable(), Right.SCHEMA_OWNER);
65 db.renameSchemaObject(session, oldIndex, newIndexName);
66 return 0;
67 }
68
69 @Override
70 public int getType() {
71 return CommandInterface.ALTER_INDEX_RENAME;
72 }
73
74 }