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

com.scalar.db.sql.LikePredicate Maven / Gradle / Ivy

package com.scalar.db.sql;

import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.MoreObjects;
import com.scalar.db.sql.Predicate.Operator;
import java.util.Objects;
import javax.annotation.concurrent.Immutable;

/** A like predicate used in WHERE clauses. */
@Immutable
public class LikePredicate extends Predicate {
  @VisibleForTesting static final String DEFAULT_ESCAPE_CHAR = "\\";
  public final Term escape;

  LikePredicate(ColumnRef column, Operator operator, Term value) {
    this(column, operator, value, Literal.ofString(DEFAULT_ESCAPE_CHAR));
  }

  LikePredicate(ColumnRef column, Operator operator, Term value, Term escape) {
    super(column, operator, value);
    this.escape = Objects.requireNonNull(escape);
  }

  @Override
  public LikePredicate replaceValue(Term newValue) {
    return replaceValueAndEscape(newValue, escape);
  }

  public LikePredicate replaceEscape(Term newEscape) {
    return replaceValueAndEscape(value, newEscape);
  }

  public LikePredicate replaceValueAndEscape(Term newValue, Term newEscape) {
    return new LikePredicate(column, operator, newValue, newEscape);
  }

  @Override
  public String toString() {
    return MoreObjects.toStringHelper(this)
        .add("column", column)
        .add("operator", operator)
        .add("value", value)
        .add("escape", escape)
        .toString();
  }

  @Override
  public boolean equals(Object o) {
    if (!super.equals(o)) {
      return false;
    }
    if (this == o) {
      return true;
    }
    if (!(o instanceof LikePredicate)) {
      return false;
    }
    LikePredicate predicate = (LikePredicate) o;
    return Objects.equals(escape, predicate.escape);
  }

  @Override
  public int hashCode() {
    return Objects.hash(super.hashCode(), escape);
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy