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

com.scalar.db.sql.statement.builder.GrantStatementBuilder Maven / Gradle / Ivy

There is a newer version: 3.14.0
Show newest version
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.GrantStatement;
import java.util.Arrays;

public class GrantStatementBuilder {

  public static class Start {

    private final ImmutableList.Builder privilegesBuilder = ImmutableList.builder();

    Start(Privilege... privileges) {
      privilegesBuilder.add(privileges);
    }

    /**
     * Set namespace names and table names to grant 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 To 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 To(
          privilegesBuilder,
          GrantStatement.TargetType.TABLE,
          tablesBuilder.build(),
          ImmutableList.of());
    }

    /**
     * Set namespace names to grant privileges on.
     *
     * @param namespaceNames table names
     * @return a builder object
     */
    public To onNamespace(String... namespaceNames) {
      return new To(
          privilegesBuilder,
          GrantStatement.TargetType.NAMESPACE,
          ImmutableList.of(),
          ImmutableList.copyOf(namespaceNames));
    }
  }

  public static class To {
    private final ImmutableList.Builder privilegesBuilder;
    private final GrantStatement.TargetType targetType;
    private final ImmutableList tables;
    private final ImmutableList namespaceNames;

    private To(
        ImmutableList.Builder privilegesBuilder,
        GrantStatement.TargetType targetType,
        ImmutableList tables,
        ImmutableList namespaceNames) {
      this.privilegesBuilder = privilegesBuilder;
      this.targetType = targetType;
      this.tables = tables;
      this.namespaceNames = namespaceNames;
    }

    /**
     * Set usernames to grant privileges to.
     *
     * @param usernames usernames
     * @return a builder object
     */
    public WithGrantOption to(String... usernames) {
      return new WithGrantOption(
          privilegesBuilder, targetType, tables, namespaceNames, ImmutableList.copyOf(usernames));
    }
  }

  public static class WithGrantOption extends Buildable {

    private WithGrantOption(
        ImmutableList.Builder privilegesBuilder,
        GrantStatement.TargetType targetType,
        ImmutableList tables,
        ImmutableList namespaceNames,
        ImmutableList usernames) {
      super(privilegesBuilder, targetType, tables, namespaceNames, usernames);
    }

    public Buildable withGrantOption() {
      privilegesBuilder.add(Privilege.GRANT);
      return this;
    }
  }

  public static class Buildable {

    protected final ImmutableList.Builder privilegesBuilder;
    private final GrantStatement.TargetType targetType;
    private final ImmutableList tables;
    private final ImmutableList namespaceNames;
    private final ImmutableList usernames;

    private Buildable(
        ImmutableList.Builder privilegesBuilder,
        GrantStatement.TargetType targetType,
        ImmutableList tables,
        ImmutableList namespaceNames,
        ImmutableList usernames) {
      this.privilegesBuilder = privilegesBuilder;
      this.targetType = targetType;
      this.tables = tables;
      this.namespaceNames = namespaceNames;
      this.usernames = usernames;
    }

    /**
     * Builds a GrantStatement object.
     *
     * @return a GrantStatement object
     */
    public GrantStatement build() {
      return GrantStatement.create(
          privilegesBuilder.build(), targetType, tables, namespaceNames, usernames);
    }
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy