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