Debian Codegraph @

src/main/org/h2/command/ddl/AlterTableRenameConstraint.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.constraint.Constraint;
11 import org.h2.constraint.Constraint.Type;
12 import org.h2.engine.Database;
13 import org.h2.engine.Right;
14 import org.h2.engine.SessionLocal;
15 import org.h2.engine.User;
16 import org.h2.message.DbException;
17 import org.h2.schema.Schema;
18 import org.h2.table.Table;
19
20 /**
21 * This class represents the statement
22 * ALTER TABLE RENAME CONSTRAINT
23 */
24 public class AlterTableRenameConstraint extends AlterTable {
25
26 private String constraintName;
27 private String newConstraintName;
28
29 public AlterTableRenameConstraint(SessionLocal session, Schema schema) {
30 super(session, schema);
31 }
32
33 public void setConstraintName(String string) {
34 constraintName = string;
35 }
36
37 public void setNewConstraintName(String newName) {
38 this.newConstraintName = newName;
39 }
40
41 @Override
42 public long update(Table table) {
43 Constraint constraint = getSchema().findConstraint(session, constraintName);
44 Database db = getDatabase();
45 if (constraint == null || constraint.getConstraintType() == Type.DOMAIN || constraint.getTable() != table) {
46 throw DbException.get(ErrorCode.CONSTRAINT_NOT_FOUND_1, constraintName);
47 }
48 if (getSchema().findConstraint(session, newConstraintName) != null
49 || newConstraintName.equals(constraintName)) {
50 throw DbException.get(ErrorCode.CONSTRAINT_ALREADY_EXISTS_1, newConstraintName);
51 }
52 User user = session.getUser();
53 Table refTable = constraint.getRefTable();
54 if (refTable != table) {
55 user.checkTableRight(refTable, Right.SCHEMA_OWNER);
56 }
57 db.renameSchemaObject(session, constraint, newConstraintName);
58 return 0;
59 }
60
61 @Override
62 public int getType() {
63 return CommandInterface.ALTER_TABLE_RENAME_CONSTRAINT;
64 }
65
66 }