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