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