Debian Codegraph @

src/main/org/h2/command/ddl/CreateRole.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.RightOwner;
12 import org.h2.engine.Role;
13 import org.h2.engine.SessionLocal;
14 import org.h2.message.DbException;
15
16 /**
17 * This class represents the statement
18 * CREATE ROLE
19 */
20 public class CreateRole extends DefineCommand {
21
22 private String roleName;
23 private boolean ifNotExists;
24
25 public CreateRole(SessionLocal session) {
26 super(session);
27 }
28
29 public void setIfNotExists(boolean ifNotExists) {
30 this.ifNotExists = ifNotExists;
31 }
32
33 public void setRoleName(String name) {
34 this.roleName = name;
35 }
36
37 @Override
38 public long update() {
39 session.getUser().checkAdmin();
40 Database db = getDatabase();
41 RightOwner rightOwner = db.findUserOrRole(roleName);
42 if (rightOwner != null) {
43 if (rightOwner instanceof Role) {
44 if (ifNotExists) {
45 return 0;
46 }
47 throw DbException.get(ErrorCode.ROLE_ALREADY_EXISTS_1, roleName);
48 }
49 throw DbException.get(ErrorCode.USER_ALREADY_EXISTS_1, roleName);
50 }
51 int id = getObjectId();
52 Role role = new Role(db, id, roleName, false);
53 db.addDatabaseObject(session, role);
54 return 0;
55 }
56
57 @Override
58 public int getType() {
59 return CommandInterface.CREATE_ROLE;
60 }
61
62 }