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

com.scalar.db.sql.statement.builder.UpsertStatementBuilder 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.TableRef;
import com.scalar.db.sql.Term;
import com.scalar.db.sql.statement.UpsertStatement;
import java.util.List;
import javax.annotation.Nullable;

public final class UpsertStatementBuilder {

  private UpsertStatementBuilder() {}

  public static class Start extends UpsertValues implements Columns {

    Start(@Nullable String namespaceName, String tableName) {
      super(namespaceName, tableName, ImmutableList.of());
    }

    @Override
    public UpsertValues columns(String... columnNames) {
      return new UpsertValues(namespaceName, tableName, ImmutableList.copyOf(columnNames));
    }

    @Override
    public UpsertValues columns(List columnNames) {
      return new UpsertValues(namespaceName, tableName, ImmutableList.copyOf(columnNames));
    }
  }

  public static class UpsertValues implements Values {

    @Nullable protected final String namespaceName;
    protected final String tableName;
    protected final ImmutableList columnNames;

    private UpsertValues(
        @Nullable String namespaceName, String tableName, ImmutableList columnNames) {
      this.namespaceName = namespaceName;
      this.tableName = tableName;
      this.columnNames = columnNames;
    }

    @Override
    public Buildable values(Term... values) {
      return new Buildable(
          namespaceName,
          tableName,
          columnNames,
          ImmutableList.>builder().add(ImmutableList.copyOf(values)));
    }

    @Override
    public Buildable values(List values) {
      return new Buildable(
          namespaceName,
          tableName,
          columnNames,
          ImmutableList.>builder().add(ImmutableList.copyOf(values)));
    }
  }

  public static class Buildable implements Values {
    @Nullable protected final String namespaceName;
    protected final String tableName;
    protected final ImmutableList columnNames;
    private final ImmutableList.Builder> valuesListBuilder;

    private Buildable(
        @Nullable String namespaceName,
        String tableName,
        ImmutableList columnNames,
        ImmutableList.Builder> valuesListBuilder) {
      this.namespaceName = namespaceName;
      this.tableName = tableName;
      this.columnNames = columnNames;
      this.valuesListBuilder = valuesListBuilder;
    }

    @Override
    public Buildable values(Term... values) {
      valuesListBuilder.add(ImmutableList.copyOf(values));
      return this;
    }

    @Override
    public Buildable values(List values) {
      valuesListBuilder.add(ImmutableList.copyOf(values));
      return this;
    }

    /**
     * Builds a UpsertStatement object.
     *
     * @return a UpsertStatement object
     */
    public UpsertStatement build() {
      return UpsertStatement.create(
          TableRef.of(namespaceName, tableName), columnNames, valuesListBuilder.build());
    }
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy