All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.scalar.db.sql.statement.RevokeStatement Maven / Gradle / Ivy

There is a newer version: 3.14.0
Show newest version
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 RevokeStatement implements DclStatement, NamespaceNameOmittable {

  public final ImmutableList privileges;
  public final TargetType targetType;
  public final ImmutableList tables;
  public final ImmutableList namespaceNames;
  public final ImmutableList usernames;

  private RevokeStatement(
      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("REVOKE ");

    boolean first = true;
    for (Privilege privilege : privileges) {
      if (privilege == Privilege.GRANT) {
        continue;
      }

      if (first) {
        first = false;
      } else {
        builder.append(",");
      }
      builder.append(privilege.name());
    }
    if (privileges.contains(Privilege.GRANT)) {
      if (!first) {
        builder.append(",");
      }
      builder.append("GRANT OPTION");
    }

    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(" FROM ");
    StatementUtils.appendObjectNames(builder, usernames);

    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 RevokeStatement 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 RevokeStatement)) {
      return false;
    }
    RevokeStatement that = (RevokeStatement) 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 RevokeStatement create(
      ImmutableList privileges,
      TargetType targetType,
      ImmutableList tables,
      ImmutableList namespaceNames,
      ImmutableList usernames) {
    return new RevokeStatement(privileges, targetType, tables, namespaceNames, usernames);
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy