| 1 |
/* |
| 2 |
* Copyright 2004-2023 H2 Group. Multiple-Licensed under the MPL 2.0, |
| 3 |
* and the EPL 1.0 (https://h2database.com/html/license.html). |
| 4 |
* Initial Developer: H2 Group |
| 5 |
*/ |
| 6 |
package org.h2.expression; |
| 7 |
|
| 8 |
import org.h2.engine.SessionLocal; |
| 9 |
import org.h2.expression.condition.Comparison; |
| 10 |
import org.h2.index.IndexCondition; |
| 11 |
import org.h2.table.TableFilter; |
| 12 |
import org.h2.value.TypeInfo; |
| 13 |
import org.h2.value.Value; |
| 14 |
import org.h2.value.ValueBoolean; |
| 15 |
import org.h2.value.ValueNull; |
| 16 |
|
| 17 |
/** |
| 18 |
* An expression representing a constant value. |
| 19 |
*/ |
| 20 |
public class ValueExpression extends Operation0 { |
| 21 |
|
| 22 |
/** |
| 23 |
* The expression represents ValueNull.INSTANCE. |
| 24 |
*/ |
| 25 |
public static final ValueExpression NULL = new ValueExpression(ValueNull.INSTANCE); |
| 26 |
|
| 27 |
/** |
| 28 |
* This special expression represents the default value. It is used for |
| 29 |
* UPDATE statements of the form SET COLUMN = DEFAULT. The value is |
| 30 |
* ValueNull.INSTANCE, but should never be accessed. |
| 31 |
*/ |
| 32 |
public static final ValueExpression DEFAULT = new ValueExpression(ValueNull.INSTANCE); |
| 33 |
|
| 34 |
/** |
| 35 |
* The expression represents ValueBoolean.TRUE. |
| 36 |
*/ |
| 37 |
public static final ValueExpression TRUE = new ValueExpression(ValueBoolean.TRUE); |
| 38 |
|
| 39 |
/** |
| 40 |
* The expression represents ValueBoolean.FALSE. |
| 41 |
*/ |
| 42 |
public static final ValueExpression FALSE = new ValueExpression(ValueBoolean.FALSE); |
| 43 |
|
| 44 |
/** |
| 45 |
* The value. |
| 46 |
*/ |
| 47 |
final Value value; |
| 48 |
|
| 49 |
ValueExpression(Value value) { |
| 50 |
this.value = value; |
| 51 |
} |
| 52 |
|
| 53 |
/** |
| 54 |
* Create a new expression with the given value. |
| 55 |
* |
| 56 |
* @param value the value |
| 57 |
* @return the expression |
| 58 |
*/ |
| 59 |
public static ValueExpression get(Value value) { |
| 60 |
if (value == ValueNull.INSTANCE) { |
| 61 |
return NULL; |
| 62 |
} |
| 63 |
if (value.getValueType() == Value.BOOLEAN) { |
| 64 |
return getBoolean(value.getBoolean()); |
| 65 |
} |
| 66 |
return new ValueExpression(value); |
| 67 |
} |
| 68 |
|
| 69 |
/** |
| 70 |
* Create a new expression with the given boolean value. |
| 71 |
* |
| 72 |
* @param value the boolean value |
| 73 |
* @return the expression |
| 74 |
*/ |
| 75 |
public static ValueExpression getBoolean(Value value) { |
| 76 |
if (value == ValueNull.INSTANCE) { |
| 77 |
return TypedValueExpression.UNKNOWN; |
| 78 |
} |
| 79 |
return getBoolean(value.getBoolean()); |
| 80 |
} |
| 81 |
|
| 82 |
/** |
| 83 |
* Create a new expression with the given boolean value. |
| 84 |
* |
| 85 |
* @param value the boolean value |
| 86 |
* @return the expression |
| 87 |
*/ |
| 88 |
public static ValueExpression getBoolean(boolean value) { |
| 89 |
return value ? TRUE : FALSE; |
| 90 |
} |
| 91 |
|
| 92 |
@Override |
| 93 |
public Value getValue(SessionLocal session) { |
| 94 |
return value; |
| 95 |
} |
| 96 |
|
| 97 |
@Override |
| 98 |
public TypeInfo getType() { |
| 99 |
return value.getType(); |
| 100 |
} |
| 101 |
|
| 102 |
@Override |
| 103 |
public void createIndexConditions(SessionLocal session, TableFilter filter) { |
| 104 |
if (value.getValueType() == Value.BOOLEAN && !value.getBoolean()) { |
| 105 |
filter.addIndexCondition(IndexCondition.get(Comparison.FALSE, null, this)); |
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
@Override |
| 110 |
public Expression getNotIfPossible(SessionLocal session) { |
| 111 |
if (value == ValueNull.INSTANCE) { |
| 112 |
return TypedValueExpression.UNKNOWN; |
| 113 |
} |
| 114 |
return getBoolean(!value.getBoolean()); |
| 115 |
} |
| 116 |
|
| 117 |
@Override |
| 118 |
public boolean isConstant() { |
| 119 |
return true; |
| 120 |
} |
| 121 |
|
| 122 |
@Override |
| 123 |
public boolean isNullConstant() { |
| 124 |
return this == NULL; |
| 125 |
} |
| 126 |
|
| 127 |
@Override |
| 128 |
public boolean isValueSet() { |
| 129 |
return true; |
| 130 |
} |
| 131 |
|
| 132 |
@Override |
| 133 |
public StringBuilder getUnenclosedSQL(StringBuilder builder, int sqlFlags) { |
| 134 |
if (this == DEFAULT) { |
| 135 |
builder.append("DEFAULT"); |
| 136 |
} else { |
| 137 |
value.getSQL(builder, sqlFlags); |
| 138 |
} |
| 139 |
return builder; |
| 140 |
} |
| 141 |
|
| 142 |
@Override |
| 143 |
public boolean isEverything(ExpressionVisitor visitor) { |
| 144 |
return true; |
| 145 |
} |
| 146 |
|
| 147 |
@Override |
| 148 |
public int getCost() { |
| 149 |
return 0; |
| 150 |
} |
| 151 |
|
| 152 |
} |