File D:\maven-qcr-plugin-testsunitaires\qcr4java\h2-1.3.170-sources\target\site\qc\lecode\command\ddl\CreateRole.java.html

1 /*
2 * Copyright 2004-2011 H2 Group. Multiple-Licensed under the H2 License,
3 * Version 1.0, and under the Eclipse Public License, Version 1.0
4 * (http://h2database.com/html/license.html).
5 * Initial Developer: H2 Group
6 */
7 package org.h2.command.ddl;
8
9 import org.h2.command.CommandInterface;
10 import org.h2.constant.ErrorCode;
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 * CREATE ROLE
19 */
20 public class CreateRole extends DefineCommand {
21
22 private String roleName;
23 private boolean ifNotExists;
24
25 public CreateRole(Session 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 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 public int getType() {
57 return CommandInterface.CREATE_ROLE;
58 }
59
60 }
61