class Right extends DbObjectBase {

    /**
     * The right bit mask that means: selecting from a table is allowed.
     */
    public static final int SELECT = 1;

    /**
     * The right bit mask that means: deleting rows from a table is allowed.
     */
    public static final int DELETE = 2;

    /**
     * The right bit mask that means: inserting rows into a table is allowed.
     */
    public static final int INSERT = 4;

    /**
     * The right bit mask that means: updating data is allowed.
     */
    public static final int UPDATE = 8;

    /**
     * The right bit mask that means: create/alter/drop schema is allowed.
     */
    public static final int ALTER_ANY_SCHEMA = 16;

    /**
     * The right bit mask that means: execute service is allowed.
     */
    public static final int EXECUTE = 32;

    /**
     * The right bit mask that means: select, insert, update, delete, execute and update
     * for this object is allowed.
     */
    public static final int ALL = SELECT | DELETE | INSERT | UPDATE | EXECUTE;

    /**
     * To whom the right is granted.
     */
    private RightOwner grantee;

    /**
     * The granted role, or null if a right was granted.
     */
    private Role grantedRole;

    /**
     * The granted right.
     */
    private int grantedRight;

    /**
     * The object. If the right is global, this is null.
     */
    private DbObject grantedObject;

    public Right(Database db, int id, RightOwner grantee, Role grantedRole) {
        super(db, id, "RIGHT_" + id);
        this.grantee = grantee;
        this.grantedRole = grantedRole;
    }

    public Right(Database db, int id, RightOwner grantee, int grantedRight, DbObject grantedObject) {
        super(db, id, "" + id);
        this.grantee = grantee;
        this.grantedRight = grantedRight;
        this.grantedObject = grantedObject;

        // TODO 如何更优雅的处理临时对象的授权(或者内存数据库中的所有对象的授权)
        // grantedObject有可能为null，如: GRANT ALTER ANY SCHEMA
        if (grantedObject != null
                && (grantedObject.isTemporary() || !grantedObject.getDatabase().isPersistent()))
            setTemporary(true);
    }

    @Override
    public DbObjectType getType() {
        return DbObjectType.RIGHT;
    }

    private static boolean appendRight(StringBuilder buff, int right, int mask, String name,
            boolean comma) {
        if ((right & mask) != 0) {
            if (comma) {
                buff.append(", ");
            }
            buff.append(name);
            return true;
        }
        return comma;
    }

    public String getRights() {<FILL_FUNCTION_BODY>}

    public Role getGrantedRole() {
        return grantedRole;
    }

    public DbObject getGrantedObject() {
        return grantedObject;
    }

    public DbObject getGrantee() {
        return grantee;
    }

    @Override
    public String getCreateSQL() {
        StringBuilder buff = new StringBuilder();
        buff.append("GRANT ");
        if (grantedRole != null) {
            buff.append(grantedRole.getSQL());
        } else {
            buff.append(getRights());
            if (grantedObject != null) {
                if (grantedObject instanceof Schema) {
                    buff.append(" ON SCHEMA ").append(grantedObject.getSQL());
                } else if (grantedObject instanceof Service) {
                    buff.append(" ON SERVICE ").append(grantedObject.getSQL());
                } else if (grantedObject instanceof Table) {
                    buff.append(" ON ").append(grantedObject.getSQL());
                }
            }
        }
        buff.append(" TO ").append(grantee.getSQL());
        return buff.toString();
    }

    @Override
    public void removeChildrenAndResources(ServerSession session, DbObjectLock lock) {
        if (grantedRole != null) {
            grantee.revokeRole(grantedRole);
        } else {
            grantee.revokeRight(grantedObject);
        }
        super.removeChildrenAndResources(session, lock);
    }

    @Override
    public void invalidate() {
        grantedRole = null;
        grantedObject = null;
        grantee = null;
        super.invalidate();
    }

    @Override
    public void checkRename() {
        DbException.throwInternalError();
    }

    public void setRightMask(int rightMask) {
        grantedRight = rightMask;
    }

    public int getRightMask() {
        return grantedRight;
    }
}

        StringBuilder buff = new StringBuilder();
        if (grantedRight == ALL) {
            buff.append("ALL");
        } else {
            boolean comma = false;
            comma = appendRight(buff, grantedRight, SELECT, "SELECT", comma);
            comma = appendRight(buff, grantedRight, DELETE, "DELETE", comma);
            comma = appendRight(buff, grantedRight, INSERT, "INSERT", comma);
            comma = appendRight(buff, grantedRight, EXECUTE, "EXECUTE", comma);
            comma = appendRight(buff, grantedRight, ALTER_ANY_SCHEMA, "ALTER ANY SCHEMA", comma);
            appendRight(buff, grantedRight, UPDATE, "UPDATE", comma);
        }
        return buff.toString();
    
<methods>public void checkRename() ,public List<? extends com.lealone.db.DbObject> getChildren() ,public java.lang.String getComment() ,public com.lealone.db.Database getDatabase() ,public java.lang.String getDropSQL() ,public int getId() ,public long getModificationId() ,public java.lang.String getName() ,public java.lang.String getSQL() ,public void invalidate() ,public boolean isTemporary() ,public void removeChildrenAndResources(com.lealone.db.session.ServerSession, com.lealone.db.lock.DbObjectLock) ,public void rename(java.lang.String) ,public void setComment(java.lang.String) ,public void setDatabase(com.lealone.db.Database) ,public void setModified() ,public void setTemporary(boolean) ,public java.lang.String toString() <variables>protected java.lang.String comment,protected com.lealone.db.Database database,protected int id,protected long modificationId,protected java.lang.String name,protected boolean temporary