Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
package com.scalar.db.sql;
import com.google.common.base.MoreObjects;
import java.util.Objects;
import javax.annotation.Nullable;
import javax.annotation.concurrent.Immutable;
/** A predicate used in WHERE clauses. */
@Immutable
public class Predicate {
public final ColumnRef column;
public final Operator operator;
public final Term value;
Predicate(ColumnRef column, Operator operator, Term value) {
this.column = Objects.requireNonNull(column);
this.operator = Objects.requireNonNull(operator);
this.value = Objects.requireNonNull(value);
}
public Predicate replaceValue(Term newValue) {
return new Predicate(column, operator, newValue);
}
@Override
public String toString() {
return MoreObjects.toStringHelper(this)
.add("column", column)
.add("operator", operator)
.add("value", value)
.toString();
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (!(o instanceof Predicate)) {
return false;
}
Predicate predicate = (Predicate) o;
return Objects.equals(column, predicate.column)
&& operator == predicate.operator
&& Objects.equals(value, predicate.value);
}
@Override
public int hashCode() {
return Objects.hash(column, operator, value);
}
/**
* Returns a builder object for a predicate for the specified column.
*
* @param columnName the name of the target column
* @return a builder object
*/
public static Builder column(String columnName) {
return new Builder(ColumnRef.of(columnName));
}
/**
* Returns a builder object for a predicate for the specified column.
*
* @param tableName the name of the table for target column
* @param columnName the name of the target column
* @return a builder object
*/
public static Builder column(@Nullable String tableName, String columnName) {
if (tableName == null) {
return column(columnName);
}
return new Builder(ColumnRef.of(TableRef.of(tableName), columnName));
}
/**
* Returns a builder object for a predicate for the specified column.
*
* @param namespaceName the name of the namespace for target column
* @param tableName the name of the table for target column
* @param columnName the name of the target column
* @return a builder object
*/
public static Builder column(
@Nullable String namespaceName, @Nullable String tableName, String columnName) {
if (namespaceName == null) {
return column(tableName, columnName);
}
return new Builder(ColumnRef.of(TableRef.of(namespaceName, tableName), columnName));
}
/**
* Returns a builder object for a predicate for the specified column.
*
* @param column the target column
* @return a builder object
*/
public static Builder column(ColumnRef column) {
return new Builder(column);
}
public static class Builder {
private final ColumnRef column;
private Builder(ColumnRef column) {
this.column = column;
}
/**
* Builds an "equal to" predicate with the specified value.
*
* @param value a value
* @return a {code Predicate} object with the specified value.
*/
public Predicate isEqualTo(Term value) {
return new Predicate(column, Operator.EQUAL_TO, value);
}
/**
* Builds a "not equal to" predicate with the specified value.
*
* @param value a value
* @return a {code Predicate} object with the specified value.
*/
public Predicate isNotEqualTo(Term value) {
return new Predicate(column, Operator.NOT_EQUAL_TO, value);
}
/**
* Builds a "greater than" predicate with the specified value.
*
* @param value a value
* @return a {code Predicate} object with the specified value.
*/
public Predicate isGreaterThan(Term value) {
return new Predicate(column, Operator.GREATER_THAN, value);
}
/**
* Builds a "greater than or equal to" predicate with the specified value.
*
* @param value a value
* @return a {code Predicate} object with the specified value.
*/
public Predicate isGreaterThanOrEqualTo(Term value) {
return new Predicate(column, Operator.GREATER_THAN_OR_EQUAL_TO, value);
}
/**
* Builds a "less than" predicate with the specified value.
*
* @param value a value
* @return a {code Predicate} object with the specified value.
*/
public Predicate isLessThan(Term value) {
return new Predicate(column, Operator.LESS_THAN, value);
}
/**
* Builds a "less than or equal to" predicate with the specified value.
*
* @param value a value
* @return a {code Predicate} object with the specified value.
*/
public Predicate isLessThanOrEqualTo(Term value) {
return new Predicate(column, Operator.LESS_THAN_OR_EQUAL_TO, value);
}
/**
* Builds a "like" predicate with the specified value. The value must be a Text value. For the
* escape character, the default one ("\", i.e., backslash) is used.
*
* @param value a value
* @return a {code LikePredicate} object with the specified value.
*/
public LikePredicate isLike(Term value) {
return new LikePredicate(column, Operator.LIKE, value);
}
/**
* Builds an "is null" predicate.
*
* @return a {code Predicate} object with the null value}.
*/
public Predicate isNull() {
return new Predicate(column, Operator.IS_NULL, Value.ofNull());
}
/**
* Builds an "is not null" predicate.
*
* @return a {code Predicate} object with the null value}.
*/
public Predicate isNotNull() {
return new Predicate(column, Operator.IS_NOT_NULL, Value.ofNull());
}
/**
* Builds a "like" predicate with the specified value and escape character. The value and escape
* character must be TEXT values. The TEXT value for the escape character must be a string of a
* single character or an empty string. If an empty string is specified, the escape character is
* disabled.
*
* @param value a value
* @param escape an escape character used in the pattern
* @return a {code LikePredicate} object with the specified value.
*/
public LikePredicate isLike(Term value, Term escape) {
return new LikePredicate(column, Operator.LIKE, value, escape);
}
/**
* Builds a "not like" predicate with the specified value. The value must be a TEXT value. For
* the escape character, the default one ("\", i.e., backslash) is used.
*
* @param value a value
* @return a {code LikePredicate} object with the specified value.
*/
public LikePredicate isNotLike(Term value) {
return new LikePredicate(column, Operator.NOT_LIKE, value);
}
/**
* Builds a "not like" predicate with the specified value and escape character. The value and
* escape character must be TEXT values. The TEXT value for the escape character must be a
* string of a single character or an empty string. If an empty string is specified, the escape
* character is disabled.
*
* @param value a value
* @param escape an escape character used in the pattern
* @return a {code LikePredicate} object with the specified value.
*/
public LikePredicate isNotLike(Term value, Term escape) {
return new LikePredicate(column, Operator.NOT_LIKE, value, escape);
}
}
public enum Operator {
EQUAL_TO,
NOT_EQUAL_TO,
GREATER_THAN,
GREATER_THAN_OR_EQUAL_TO,
LESS_THAN,
LESS_THAN_OR_EQUAL_TO,
IS_NULL,
IS_NOT_NULL,
LIKE,
NOT_LIKE
}
}