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

com.scalar.db.sql.statement.DeleteStatement 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.AndPredicateList;
import com.scalar.db.sql.OrPredicateList;
import com.scalar.db.sql.Predicate;
import com.scalar.db.sql.TableRef;
import com.scalar.db.sql.Value;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import javax.annotation.Nullable;
import javax.annotation.concurrent.Immutable;

@Immutable
public class DeleteStatement
    implements DmlStatement,
        BindableStatement,
        NamespaceNameOmittable {

  public final TableRef table;
  @Nullable public final String alias;
  public final ImmutableList andPredicateLists; // for DNF, empty when using CNF
  public final ImmutableList orPredicateLists; // for CNF, empty when using DNF

  private DeleteStatement(
      TableRef table,
      @Nullable String alias,
      ImmutableList andPredicateLists,
      ImmutableList orPredicateLists) {
    this.table = Objects.requireNonNull(table);
    this.alias = alias;
    this.andPredicateLists = Objects.requireNonNull(andPredicateLists);
    this.orPredicateLists = Objects.requireNonNull(orPredicateLists);
  }

  @Override
  public DeleteStatement bind(List positionalValues) {
    Iterator positionalValueIterator = positionalValues.iterator();
    return create(
        table,
        alias,
        StatementUtils.bindPredicateLists(andPredicateLists, positionalValueIterator),
        StatementUtils.bindPredicateLists(orPredicateLists, positionalValueIterator));
  }

  @Override
  public DeleteStatement bind(Map namedValues) {
    return create(
        table,
        alias,
        StatementUtils.bindPredicateLists(andPredicateLists, namedValues),
        StatementUtils.bindPredicateLists(orPredicateLists, namedValues));
  }

  @Override
  public String toSql() {
    StringBuilder builder = new StringBuilder("DELETE FROM ");
    StatementUtils.appendTable(builder, table);

    if (alias != null) {
      builder.append(" AS ");
      StatementUtils.appendObjectName(builder, alias);
    }

    StatementUtils.appendWhere(builder, andPredicateLists, orPredicateLists);

    return builder.toString();
  }

  @Override
  public  R accept(StatementVisitor visitor, C context) {
    return visitor.visit(this, context);
  }

  @Override
  public  R accept(DmlStatementVisitor visitor, C context) {
    return visitor.visit(this, context);
  }

  @Override
  public boolean namespaceNameOmitted() {
    return table.namespaceName == null;
  }

  @Override
  public DeleteStatement setNamespaceNameIfOmitted(String namespaceName) {
    if (namespaceNameOmitted()) {
      return create(
          TableRef.of(namespaceName, table.tableName), alias, andPredicateLists, orPredicateLists);
    }
    return this;
  }

  @Override
  public String toString() {
    return toSql();
  }

  @Override
  public boolean equals(Object o) {
    if (this == o) {
      return true;
    }
    if (!(o instanceof DeleteStatement)) {
      return false;
    }
    DeleteStatement that = (DeleteStatement) o;
    return Objects.equals(table, that.table)
        && Objects.equals(andPredicateLists, that.andPredicateLists)
        && Objects.equals(orPredicateLists, that.orPredicateLists);
  }

  @Override
  public int hashCode() {
    return Objects.hash(table, andPredicateLists, andPredicateLists);
  }

  public static DeleteStatement create(
      TableRef table, @Nullable String alias, ImmutableList predicates) {
    return create(
        table,
        alias,
        predicates.isEmpty()
            ? ImmutableList.of()
            : ImmutableList.of(AndPredicateList.predicates(predicates).build()),
        ImmutableList.of());
  }

  public static DeleteStatement create(
      TableRef table,
      @Nullable String alias,
      ImmutableList andPredicateLists,
      ImmutableList orPredicateLists) {
    if (!andPredicateLists.isEmpty() && !orPredicateLists.isEmpty()) {
      throw new IllegalArgumentException(
          "Either andPredicateLists or orPredicateLists can be used");
    }
    return new DeleteStatement(table, alias, andPredicateLists, orPredicateLists);
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy