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

com.scalar.db.sql.statement.builder.UpdateStatementBuilder 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.AndPredicateList;
import com.scalar.db.sql.Assignment;
import com.scalar.db.sql.OrPredicateList;
import com.scalar.db.sql.Predicate;
import com.scalar.db.sql.TableRef;
import com.scalar.db.sql.statement.UpdateStatement;
import java.util.List;
import javax.annotation.Nullable;

public final class UpdateStatementBuilder {

  private UpdateStatementBuilder() {}

  public static class Start extends AsOrSet {
    Start(@Nullable String namespaceName, String tableName) {
      super(namespaceName, tableName);
    }
  }

  public static class AsOrSet extends Set {
    private AsOrSet(@Nullable String namespaceName, String tableName) {
      super(namespaceName, tableName, null);
    }

    /**
     * Specifies the alias of the target table.
     *
     * @param alias the alias of the target table
     * @return a builder object
     */
    public Set as(String alias) {
      return new Set(namespaceName, tableName, alias);
    }
  }

  public static class Set {
    @Nullable protected final String namespaceName;
    protected final String tableName;
    @Nullable private final String alias;

    private Set(@Nullable String namespaceName, String tableName, @Nullable String alias) {
      this.namespaceName = namespaceName;
      this.tableName = tableName;
      this.alias = alias;
    }

    /**
     * Sets assignments to update.
     *
     * @param assignments assignments to update
     * @return a builder object
     */
    public UpdateWhereStart set(Assignment... assignments) {
      return new UpdateWhereStart(
          namespaceName, tableName, alias, ImmutableList.copyOf(assignments));
    }

    /**
     * Sets assignments to update.
     *
     * @param assignments assignments to update
     * @return a builder object
     */
    public UpdateWhereStart set(List assignments) {
      return new UpdateWhereStart(
          namespaceName, tableName, alias, ImmutableList.copyOf(assignments));
    }
  }

  public static class UpdateWhereStart extends Buildable
      implements WhereStart {

    private UpdateWhereStart(
        @Nullable String namespaceName,
        String tableName,
        @Nullable String alias,
        ImmutableList assignments) {
      super(
          namespaceName,
          tableName,
          alias,
          assignments,
          null,
          ImmutableList.builder(),
          ImmutableList.builder());
    }

    @Override
    public UpdateOngoingWhere where(Predicate predicate) {
      return new UpdateOngoingWhere(
          namespaceName,
          tableName,
          alias,
          assignments,
          predicate,
          andPredicateListsBuilder,
          orPredicateListsBuilder);
    }

    @Override
    public UpdateOngoingWhereOr where(List predicates) {
      return where(AndPredicateList.predicates(ImmutableList.copyOf(predicates)).build());
    }

    @Override
    public UpdateOngoingWhereOr where(AndPredicateList andPredicateList) {
      return new UpdateOngoingWhereOr(
          namespaceName,
          tableName,
          alias,
          assignments,
          andPredicateListsBuilder.add(andPredicateList),
          orPredicateListsBuilder);
    }

    @Override
    public UpdateOngoingWhereAnd where(OrPredicateList orPredicateList) {
      return new UpdateOngoingWhereAnd(
          namespaceName,
          tableName,
          alias,
          assignments,
          andPredicateListsBuilder,
          orPredicateListsBuilder.add(orPredicateList));
    }

    @Override
    public UpdateOngoingWhereOr whereOr(List andPredicateLists) {
      return new UpdateOngoingWhereOr(
          namespaceName,
          tableName,
          alias,
          assignments,
          andPredicateListsBuilder.addAll(andPredicateLists),
          orPredicateListsBuilder);
    }

    @Override
    public UpdateOngoingWhereAnd whereAnd(List orPredicateLists) {
      return new UpdateOngoingWhereAnd(
          namespaceName,
          tableName,
          alias,
          assignments,
          andPredicateListsBuilder,
          orPredicateListsBuilder.addAll(orPredicateLists));
    }
  }

  public static class UpdateOngoingWhere extends Buildable
      implements OngoingWhere {

    private UpdateOngoingWhere(
        @Nullable String namespaceName,
        String tableName,
        @Nullable String alias,
        ImmutableList assignments,
        @Nullable Predicate predicate,
        ImmutableList.Builder andPredicateListsBuilder,
        ImmutableList.Builder orPredicateListsBuilder) {
      super(
          namespaceName,
          tableName,
          alias,
          assignments,
          predicate,
          andPredicateListsBuilder,
          orPredicateListsBuilder);
    }

    @Override
    public UpdateOngoingWhereAnd and(Predicate predicate) {
      return new UpdateOngoingWhereAnd(
          namespaceName,
          tableName,
          alias,
          assignments,
          andPredicateListsBuilder,
          orPredicateListsBuilder
              .add(OrPredicateList.predicate(this.predicate).build())
              .add(OrPredicateList.predicate(predicate).build()));
    }

    @Override
    public UpdateOngoingWhereAnd and(OrPredicateList orPredicateList) {
      return new UpdateOngoingWhereAnd(
          namespaceName,
          tableName,
          alias,
          assignments,
          andPredicateListsBuilder,
          orPredicateListsBuilder
              .add(OrPredicateList.predicate(this.predicate).build())
              .add(orPredicateList));
    }

    @Override
    public UpdateOngoingWhereOr or(Predicate predicate) {
      return new UpdateOngoingWhereOr(
          namespaceName,
          tableName,
          alias,
          assignments,
          andPredicateListsBuilder
              .add(AndPredicateList.predicate(this.predicate).build())
              .add(AndPredicateList.predicate(predicate).build()),
          orPredicateListsBuilder);
    }

    @Override
    public UpdateOngoingWhereOr or(AndPredicateList andPredicateList) {
      return new UpdateOngoingWhereOr(
          namespaceName,
          tableName,
          alias,
          assignments,
          andPredicateListsBuilder
              .add(AndPredicateList.predicate(this.predicate).build())
              .add(andPredicateList),
          orPredicateListsBuilder);
    }
  }

  public static class UpdateOngoingWhereAnd extends Buildable
      implements OngoingWhereAnd {
    private UpdateOngoingWhereAnd(
        @Nullable String namespaceName,
        String tableName,
        @Nullable String alias,
        ImmutableList assignments,
        ImmutableList.Builder andPredicateListsBuilder,
        ImmutableList.Builder orPredicateListsBuilder) {
      super(
          namespaceName,
          tableName,
          alias,
          assignments,
          null,
          andPredicateListsBuilder,
          orPredicateListsBuilder);
    }

    @Override
    public UpdateOngoingWhereAnd and(Predicate predicate) {
      orPredicateListsBuilder.add(OrPredicateList.predicate(predicate).build());
      return this;
    }

    @Override
    public UpdateOngoingWhereAnd and(OrPredicateList orPredicateList) {
      orPredicateListsBuilder.add(orPredicateList);
      return this;
    }
  }

  public static class UpdateOngoingWhereOr extends Buildable
      implements OngoingWhereOr {
    private UpdateOngoingWhereOr(
        @Nullable String namespaceName,
        String tableName,
        @Nullable String alias,
        ImmutableList assignments,
        ImmutableList.Builder andPredicateListsBuilder,
        ImmutableList.Builder orPredicateListsBuilder) {
      super(
          namespaceName,
          tableName,
          alias,
          assignments,
          null,
          andPredicateListsBuilder,
          orPredicateListsBuilder);
    }

    @Override
    public UpdateOngoingWhereOr or(Predicate predicate) {
      andPredicateListsBuilder.add(AndPredicateList.predicate(predicate).build());
      return this;
    }

    @Override
    public UpdateOngoingWhereOr or(AndPredicateList andPredicateList) {
      andPredicateListsBuilder.add(andPredicateList);
      return this;
    }
  }

  public static class Buildable {
    @Nullable protected final String namespaceName;
    protected final String tableName;
    @Nullable protected final String alias;
    protected final ImmutableList assignments;
    @Nullable protected final Predicate predicate;
    protected final ImmutableList.Builder andPredicateListsBuilder;
    protected final ImmutableList.Builder orPredicateListsBuilder;

    private Buildable(
        @Nullable String namespaceName,
        String tableName,
        @Nullable String alias,
        ImmutableList assignments,
        @Nullable Predicate predicate,
        ImmutableList.Builder andPredicateListsBuilder,
        ImmutableList.Builder orPredicateListsBuilder) {
      this.namespaceName = namespaceName;
      this.tableName = tableName;
      this.alias = alias;
      this.assignments = assignments;
      this.predicate = predicate;
      this.andPredicateListsBuilder = andPredicateListsBuilder;
      this.orPredicateListsBuilder = orPredicateListsBuilder;
    }

    /**
     * Builds a UpdateStatement object.
     *
     * @return a UpdateStatement object
     */
    public UpdateStatement build() {
      return UpdateStatement.create(
          TableRef.of(namespaceName, tableName),
          alias,
          assignments,
          StatementBuilderUtils.buildAndPredicateLists(predicate, andPredicateListsBuilder),
          StatementBuilderUtils.buildOrPredicateLists(orPredicateListsBuilder));
    }
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy