All Downloads are FREE. Search and download functionalities are using the official Maven repository.
Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
com.scalar.db.sql.statement.GrantStatement Maven / Gradle / Ivy
package com.scalar.db.sql.statement;
import com.google.common.collect.ImmutableList;
import com.scalar.db.sql.Privilege;
import com.scalar.db.sql.TableRef;
import java.util.Objects;
import javax.annotation.concurrent.Immutable;
@Immutable
public class GrantStatement implements DclStatement, NamespaceNameOmittable {
public final ImmutableList privileges;
public final TargetType targetType;
public final ImmutableList tables;
public final ImmutableList namespaceNames;
public final ImmutableList usernames;
private GrantStatement(
ImmutableList privileges,
TargetType targetType,
ImmutableList tables,
ImmutableList namespaceNames,
ImmutableList usernames) {
this.privileges = privileges;
this.targetType = targetType;
this.tables = tables;
this.namespaceNames = namespaceNames;
this.usernames = usernames;
}
@Override
public String toSql() {
StringBuilder builder = new StringBuilder("GRANT ");
boolean first = true;
for (Privilege privilege : privileges) {
if (privilege == Privilege.GRANT) {
continue;
}
if (first) {
first = false;
} else {
builder.append(",");
}
builder.append(privilege.name());
}
builder.append(" ON ");
if (targetType == TargetType.TABLE) {
StatementUtils.appendTables(builder, tables);
} else {
assert targetType == TargetType.NAMESPACE;
builder.append("NAMESPACE ");
StatementUtils.appendObjectNames(builder, namespaceNames);
}
builder.append(" TO ");
StatementUtils.appendObjectNames(builder, usernames);
if (privileges.contains(Privilege.GRANT)) {
builder.append(" WITH GRANT OPTION");
}
return builder.toString();
}
@Override
public R accept(DclStatementVisitor visitor, C context) {
return visitor.visit(this, context);
}
@Override
public R accept(StatementVisitor visitor, C context) {
return visitor.visit(this, context);
}
@Override
public boolean namespaceNameOmitted() {
if (targetType == TargetType.NAMESPACE) {
return false;
}
return tables.stream().anyMatch(t -> t.namespaceName == null);
}
@Override
public GrantStatement setNamespaceNameIfOmitted(String namespaceName) {
if (namespaceNameOmitted()) {
return create(
privileges,
targetType,
tables.stream()
.map(t -> t.namespaceName == null ? TableRef.of(namespaceName, t.tableName) : t)
.collect(ImmutableList.toImmutableList()),
namespaceNames,
usernames);
}
return this;
}
@Override
public String toString() {
return toSql();
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (!(o instanceof GrantStatement)) {
return false;
}
GrantStatement that = (GrantStatement) o;
return Objects.equals(privileges, that.privileges)
&& targetType == that.targetType
&& Objects.equals(tables, that.tables)
&& Objects.equals(namespaceNames, that.namespaceNames)
&& Objects.equals(usernames, that.usernames);
}
@Override
public int hashCode() {
return Objects.hash(privileges, targetType, tables, namespaceNames, usernames);
}
public enum TargetType {
TABLE,
NAMESPACE
}
public static GrantStatement create(
ImmutableList privileges,
TargetType targetType,
ImmutableList tables,
ImmutableList namespaceNames,
ImmutableList usernames) {
return new GrantStatement(privileges, targetType, tables, namespaceNames, usernames);
}
}