com.scalar.db.sql.ColumnRef 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;
/** A column reference. */
@Immutable
public class ColumnRef {
@Nullable public final TableRef table;
public final String columnName;
private ColumnRef(@Nullable TableRef table, String columnName) {
this.table = table;
this.columnName = Objects.requireNonNull(columnName);
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (!(o instanceof ColumnRef)) {
return false;
}
ColumnRef column = (ColumnRef) o;
return Objects.equals(table, column.table) && columnName.equals(column.columnName);
}
@Override
public int hashCode() {
return Objects.hash(table, columnName);
}
@Override
public String toString() {
return MoreObjects.toStringHelper(this)
.add("table", table)
.add("columnName", columnName)
.toString();
}
/**
* Returns a {@code ColumnRef} object with the specified column name.
*
* @param columnName a column name
* @return a {@code ColumnRef} object
*/
public static ColumnRef of(String columnName) {
return new ColumnRef(null, columnName);
}
/**
* Returns a {@code ColumnRef} object with the specified table and column name.
*
* @param tableName a table name of the column
* @param columnName a column name
* @return a {@code ColumnRef} object
*/
public static ColumnRef of(@Nullable String tableName, String columnName) {
if (tableName == null) {
return of(columnName);
}
return new ColumnRef(TableRef.of(tableName), columnName);
}
/**
* Returns a {@code ColumnRef} object with the specified table and column name.
*
* @param namespaceName a namespace name of the table
* @param tableName a table name of the column
* @param columnName a column name
* @return a {@code ColumnRef} object
*/
public static ColumnRef of(
@Nullable String namespaceName, @Nullable String tableName, String columnName) {
if (namespaceName == null) {
return of(tableName, columnName);
}
return new ColumnRef(TableRef.of(namespaceName, tableName), columnName);
}
/**
* Returns a {@code ColumnRef} object with the specified table and column name.
*
* @param table a table
* @param columnName a column name
* @return a {@code ColumnRef} object
*/
public static ColumnRef of(@Nullable TableRef table, String columnName) {
return new ColumnRef(table, columnName);
}
}