studio.raptor.sqlparser.fast.expression.ValueExpression Maven / Gradle / Ivy
/*
* Copyright 2004-2014 H2 Group. Multiple-Licensed under the MPL 2.0,
* and the EPL 1.0 (http://h2database.com/html/license.html).
* Initial Developer: H2 Group
*/
package studio.raptor.sqlparser.fast.expression;
import studio.raptor.sqlparser.fast.message.ParseException;
import studio.raptor.sqlparser.fast.table.ColumnResolver;
import studio.raptor.sqlparser.fast.value.Value;
import studio.raptor.sqlparser.fast.value.ValueNull;
/**
* An expression representing a constant value.
*/
public class ValueExpression extends Expression {
/**
* The expression represents ValueNull.INSTANCE.
*/
private static final Object NULL = new ValueExpression(ValueNull.INSTANCE);
/**
* This special expression represents the default value. It is used for
* UPDATE statements of the form SET COLUMN = DEFAULT. The value is
* ValueNull.INSTANCE, but should never be accessed.
*/
private static final Object DEFAULT = new ValueExpression(ValueNull.INSTANCE);
private final Value value;
private ValueExpression(Value value) {
this.value = value;
}
/**
* Get the NULL expression.
*
* @return the NULL expression
*/
public static ValueExpression getNull() {
return (ValueExpression) NULL;
}
/**
* Get the DEFAULT expression.
*
* @return the DEFAULT expression
*/
public static ValueExpression getDefault() {
return (ValueExpression) DEFAULT;
}
/**
* Create a new expression with the given value.
*
* @param value the value
* @return the expression
*/
public static ValueExpression get(Value value) {
if (value == ValueNull.INSTANCE) {
return getNull();
}
return new ValueExpression(value);
}
@Override
public Value getValue() {
return value;
}
@Override
public int getType() {
return value.getType();
}
@Override
public void mapColumns(ColumnResolver resolver, int level) {
// nothing to do
}
@Override
public Expression optimize() {
return this;
}
@Override
public boolean isConstant() {
return true;
}
@Override
public boolean isValueSet() {
return true;
}
@Override
public String getSQL() {
if (this == DEFAULT) {
return "DEFAULT";
}
return value.getSQL();
}
@Override
public boolean isEverything(ExpressionVisitor visitor) {
switch (visitor.getType()) {
case ExpressionVisitor.OPTIMIZABLE_MIN_MAX_COUNT_ALL:
case ExpressionVisitor.DETERMINISTIC:
case ExpressionVisitor.READONLY:
case ExpressionVisitor.INDEPENDENT:
case ExpressionVisitor.EVALUATABLE:
case ExpressionVisitor.SET_MAX_DATA_MODIFICATION_ID:
case ExpressionVisitor.NOT_FROM_RESOLVER:
case ExpressionVisitor.GET_DEPENDENCIES:
case ExpressionVisitor.QUERY_COMPARABLE:
case ExpressionVisitor.GET_COLUMNS:
return true;
default:
throw ParseException.throwInternalError("type=" + visitor.getType());
}
}
}