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