com.scalar.db.sql.statement.builder.RevokeStatementBuilder Maven / Gradle / Ivy
package com.scalar.db.sql.statement.builder;
import com.google.common.collect.ImmutableList;
import com.scalar.db.sql.Privilege;
import com.scalar.db.sql.TableRef;
import com.scalar.db.sql.statement.RevokeStatement;
import java.util.Arrays;
public class RevokeStatementBuilder {
public static class Start {
private final ImmutableList privileges;
Start(ImmutableList privileges) {
this.privileges = privileges;
}
/**
* Set namespace names and table names to revoke privileges on.
*
* @param namespaceNamesAndTableNames namespace names and table names. A namespace name and a
* table name must be alternately specified. For example, {@code "ns1", "tbl1", "ns2",
* "tbl2"}.
* @return a builder object
*/
public From on(String... namespaceNamesAndTableNames) {
if (namespaceNamesAndTableNames.length % 2 != 0) {
throw new IllegalArgumentException(
"The number of arguments must be even: "
+ Arrays.toString(namespaceNamesAndTableNames));
}
ImmutableList.Builder tablesBuilder = ImmutableList.builder();
for (int i = 0; i < namespaceNamesAndTableNames.length; i += 2) {
tablesBuilder.add(
TableRef.of(namespaceNamesAndTableNames[i], namespaceNamesAndTableNames[i + 1]));
}
return new From(
privileges, RevokeStatement.TargetType.TABLE, tablesBuilder.build(), ImmutableList.of());
}
/**
* Set namespace names to revoke privileges on.
*
* @param namespaceNames table names
* @return a builder object
*/
public From onNamespace(String... namespaceNames) {
return new From(
privileges,
RevokeStatement.TargetType.NAMESPACE,
ImmutableList.of(),
ImmutableList.copyOf(namespaceNames));
}
}
public static class From {
private final ImmutableList privileges;
private final RevokeStatement.TargetType targetType;
private final ImmutableList tables;
private final ImmutableList namespaceNames;
private From(
ImmutableList privileges,
RevokeStatement.TargetType targetType,
ImmutableList tables,
ImmutableList namespaceNames) {
this.privileges = privileges;
this.targetType = targetType;
this.tables = tables;
this.namespaceNames = namespaceNames;
}
/**
* Set usernames to revoke privileges from.
*
* @param usernames usernames
* @return a builder object
*/
public Buildable from(String... usernames) {
return new Buildable(
privileges, targetType, tables, namespaceNames, ImmutableList.copyOf(usernames));
}
}
public static class Buildable {
private final ImmutableList privileges;
private final RevokeStatement.TargetType targetType;
private final ImmutableList tables;
private final ImmutableList namespaceNames;
private final ImmutableList usernames;
private Buildable(
ImmutableList privileges,
RevokeStatement.TargetType targetType,
ImmutableList tables,
ImmutableList namespaceNames,
ImmutableList usernames) {
this.privileges = privileges;
this.targetType = targetType;
this.tables = tables;
this.namespaceNames = namespaceNames;
this.usernames = usernames;
}
/**
* Builds a RevokeStatement object.
*
* @return a RevokeStatement object
*/
public RevokeStatement build() {
return RevokeStatement.create(privileges, targetType, tables, namespaceNames, usernames);
}
}
}