01: /*
02: * Copyright 2004-2008 H2 Group. Licensed under the H2 License, Version 1.0
03: * (http://h2database.com/html/license.html).
04: * Initial Developer: H2 Group
05: */
06: package org.h2.command.ddl;
07:
08: import java.sql.SQLException;
09:
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.Message;
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() throws SQLException {
38: session.getUser().checkAdmin();
39: session.commit(true);
40: Database db = session.getDatabase();
41: if (db.findUser(roleName) != null) {
42: if (ifNotExists) {
43: return 0;
44: }
45: throw Message.getSQLException(
46: ErrorCode.USER_ALREADY_EXISTS_1, roleName);
47: }
48: if (db.findRole(roleName) != null) {
49: if (ifNotExists) {
50: return 0;
51: }
52: throw Message.getSQLException(
53: ErrorCode.ROLE_ALREADY_EXISTS_1, roleName);
54: }
55: int id = getObjectId(false, true);
56: Role role = new Role(db, id, roleName, false);
57: db.addDatabaseObject(session, role);
58: return 0;
59: }
60:
61: }
|