KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > h2 > command > ddl > CreateRole


1 /*
2  * Copyright 2004-2006 H2 Group. Licensed under the H2 License, Version 1.0 (http://h2database.com/html/license.html).
3  * Initial Developer: H2 Group
4  */

5 package org.h2.command.ddl;
6
7 import java.sql.SQLException JavaDoc;
8
9 import org.h2.engine.Database;
10 import org.h2.engine.Role;
11 import org.h2.engine.Session;
12 import org.h2.message.Message;
13
14 public class CreateRole extends DefineCommand {
15
16     private String JavaDoc roleName;
17     private boolean ifNotExists;
18     
19     public CreateRole(Session session) {
20         super(session);
21     }
22     
23     public void setIfNotExists(boolean ifNotExists) {
24         this.ifNotExists = ifNotExists;
25     }
26
27     public void setRoleName(String JavaDoc name) {
28         this.roleName = name;
29     }
30     
31     public int update() throws SQLException JavaDoc {
32         session.getUser().checkAdmin();
33         session.commit();
34         Database db = session.getDatabase();
35         if(db.findUser(roleName)!=null) {
36             if (ifNotExists) {
37                 return 0;
38             }
39             throw Message.getSQLException(Message.USER_ALREADY_EXISTS_1, roleName);
40         }
41         if(db.findRole(roleName)!=null) {
42             if (ifNotExists) {
43                 return 0;
44             }
45             throw Message.getSQLException(Message.ROLE_ALREADY_EXISTS_1, roleName);
46         }
47         int id = getObjectId(false, true);
48         Role role = new Role(db, id, roleName, false);
49         db.addDatabaseObject(session, role);
50         return 0;
51     }
52
53 }
54
Popular Tags