/*
* Copyright 2014-2015 the original author or authors
*
* Licensed under the Apache License, Version 2.0 (the “License”);
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an “AS IS” BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.wplatform.ddal.dbobject;
import com.wplatform.ddal.engine.Database;
import com.wplatform.ddal.engine.Session;
import com.wplatform.ddal.message.Trace;
/**
* Represents a role. Roles can be granted to users, and to other roles.
*/
public class Role extends RightOwner {
private final boolean system;
public Role(Database database, int id, String roleName, boolean system) {
super(database, id, roleName, Trace.USER);
this.system = system;
}
/**
* Get the CREATE SQL statement for this object.
*
* @param ifNotExists true if IF NOT EXISTS should be used
* @return the SQL statement
*/
public String getCreateSQL(boolean ifNotExists) {
if (system) {
return null;
}
StringBuilder buff = new StringBuilder("CREATE ROLE ");
if (ifNotExists) {
buff.append("IF NOT EXISTS ");
}
buff.append(getSQL());
return buff.toString();
}
@Override
public int getType() {
return DbObject.ROLE;
}
@Override
public void removeChildrenAndResources(Session session) {
for (User user : database.getAllUsers()) {
Right right = user.getRightForRole(this);
if (right != null) {
database.removeDatabaseObject(session, right);
}
}
for (Role r2 : database.getAllRoles()) {
Right right = r2.getRightForRole(this);
if (right != null) {
database.removeDatabaseObject(session, right);
}
}
for (Right right : database.getAllRights()) {
if (right.getGrantee() == this) {
database.removeDatabaseObject(session, right);
}
}
invalidate();
}
@Override
public void checkRename() {
// ok
}
}