com.scalar.db.sql.Assignment Maven / Gradle / Ivy
package com.scalar.db.sql;
import com.google.common.base.MoreObjects;
import java.util.Objects;
import javax.annotation.Nullable;
import javax.annotation.concurrent.Immutable;
/** An assignment that is used for UPDATE statements. */
@Immutable
public class Assignment {
public final ColumnRef column;
public final Term value;
private Assignment(ColumnRef column, Term value) {
this.column = Objects.requireNonNull(column);
this.value = Objects.requireNonNull(value);
}
public Assignment replaceValue(Term newValue) {
return new Assignment(column, newValue);
}
@Override
public String toString() {
return MoreObjects.toStringHelper(this).add("column", column).add("value", value).toString();
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (!(o instanceof Assignment)) {
return false;
}
Assignment that = (Assignment) o;
return Objects.equals(column, that.column) && Objects.equals(value, that.value);
}
@Override
public int hashCode() {
return Objects.hash(column, value);
}
/**
* Returns a builder object for an assignment 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 an assignment 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 an assignment 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 an assignment 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;
}
/**
* Returns a {code Assignment} object with the specified value.
*
* @param value a value to assignment
* @return a {code Assignment} object
*/
public Assignment value(Term value) {
return new Assignment(column, value);
}
}
}