Debian Codegraph @

src/main/org/h2/command/ddl/DropRole.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.Role;
12 import org.h2.engine.SessionLocal;
13 import org.h2.message.DbException;
14
15 /**
16 * This class represents the statement
17 * DROP ROLE
18 */
19 public class DropRole extends DefineCommand {
20
21 private String roleName;
22 private boolean ifExists;
23
24 public DropRole(SessionLocal session) {
25 super(session);
26 }
27
28 public void setRoleName(String roleName) {
29 this.roleName = roleName;
30 }
31
32 @Override
33 public long update() {
34 session.getUser().checkAdmin();
35 Database db = getDatabase();
36 Role role = db.findRole(roleName);
37 if (role == null) {
38 if (!ifExists) {
39 throw DbException.get(ErrorCode.ROLE_NOT_FOUND_1, roleName);
40 }
41 } else {
42 if (role == db.getPublicRole()) {
43 throw DbException.get(ErrorCode.ROLE_CAN_NOT_BE_DROPPED_1, roleName);
44 }
45 role.checkOwnsNoSchemas();
46 db.removeDatabaseObject(session, role);
47 }
48 return 0;
49 }
50
51 public void setIfExists(boolean ifExists) {
52 this.ifExists = ifExists;
53 }
54
55 @Override
56 public int getType() {
57 return CommandInterface.DROP_ROLE;
58 }
59
60 }