File D:\maven-qcr-plugin-testsunitaires\qcr4java\h2-1.3.170-sources\target\site\qc\lecode\expression\ValueExpression.java.html

1 /*
2 * Copyright 2004-2011 H2 Group. Multiple-Licensed under the H2 License,
3 * Version 1.0, and under the Eclipse Public License, Version 1.0
4 * (http://h2database.com/html/license.html).
5 * Initial Developer: H2 Group
6 */
7 package org.h2.expression;
8
9 import org.h2.engine.Session;
10 import org.h2.index.IndexCondition;
11 import org.h2.message.DbException;
12 import org.h2.table.ColumnResolver;
13 import org.h2.table.TableFilter;
14 import org.h2.value.Value;
15 import org.h2.value.ValueArray;
16 import org.h2.value.ValueBoolean;
17 import org.h2.value.ValueNull;
18
19 /**
20 * An expression representing a constant value.
21 */
22 public class ValueExpression extends Expression {
23 /**
24 * The expression represents ValueNull.INSTANCE.
25 */
26 private static final Object NULL = new ValueExpression(ValueNull.INSTANCE);
27
28 /**
29 * This special expression represents the default value. It is used for
30 * UPDATE statements of the form SET COLUMN = DEFAULT. The value is
31 * ValueNull.INSTANCE, but should never be accessed.
32 */
33 private static final Object DEFAULT = new ValueExpression(ValueNull.INSTANCE);
34
35 private final Value value;
36
37 private ValueExpression(Value value) {
38 this.value = value;
39 }
40
41 /**
42 * Get the NULL expression.
43 *
44 * @return the NULL expression
45 */
46 public static ValueExpression getNull() {
47 return (ValueExpression) NULL;
48 }
49
50 /**
51 * Get the DEFAULT expression.
52 *
53 * @return the DEFAULT expression
54 */
55 public static ValueExpression getDefault() {
56 return (ValueExpression) DEFAULT;
57 }
58
59 /**
60 * Create a new expression with the given value.
61 *
62 * @param value the value
63 * @return the expression
64 */
65 public static ValueExpression get(Value value) {
66 if (value == ValueNull.INSTANCE) {
67 return getNull();
68 }
69 return new ValueExpression(value);
70 }
71
72 public Value getValue(Session session) {
73 return value;
74 }
75
76 public int getType() {
77 return value.getType();
78 }
79
80 public void createIndexConditions(Session session, TableFilter filter) {
81 if (value.getType() == Value.BOOLEAN) {
82 boolean v = ((ValueBoolean) value).getBoolean().booleanValue();
83 if (!v) {
84 filter.addIndexCondition(IndexCondition.get(Comparison.FALSE, null, this));
85 }
86 }
87 }
88
89 public Expression getNotIfPossible(Session session) {
90 return new Comparison(session, Comparison.EQUAL, this, ValueExpression.get(ValueBoolean.get(false)));
91 }
92
93 public void mapColumns(ColumnResolver resolver, int level) {
94 // nothing to do
95 }
96
97 public Expression optimize(Session session) {
98 return this;
99 }
100
101 public boolean isConstant() {
102 return true;
103 }
104
105 public boolean isValueSet() {
106 return true;
107 }
108
109 public void setEvaluatable(TableFilter tableFilter, boolean b) {
110 // nothing to do
111 }
112
113 public int getScale() {
114 return value.getScale();
115 }
116
117 public long getPrecision() {
118 return value.getPrecision();
119 }
120
121 public int getDisplaySize() {
122 return value.getDisplaySize();
123 }
124
125 public String getSQL() {
126 if (this == DEFAULT) {
127 return "DEFAULT";
128 }
129 return value.getSQL();
130 }
131
132 public void updateAggregate(Session session) {
133 // nothing to do
134 }
135
136 public boolean isEverything(ExpressionVisitor visitor) {
137 switch (visitor.getType()) {
138 case ExpressionVisitor.OPTIMIZABLE_MIN_MAX_COUNT_ALL:
139 case ExpressionVisitor.DETERMINISTIC:
140 case ExpressionVisitor.READONLY:
141 case ExpressionVisitor.INDEPENDENT:
142 case ExpressionVisitor.EVALUATABLE:
143 case ExpressionVisitor.SET_MAX_DATA_MODIFICATION_ID:
144 case ExpressionVisitor.NOT_FROM_RESOLVER:
145 case ExpressionVisitor.GET_DEPENDENCIES:
146 case ExpressionVisitor.QUERY_COMPARABLE:
147 case ExpressionVisitor.GET_COLUMNS:
148 return true;
149 default:
150 throw DbException.throwInternalError("type=" + visitor.getType());
151 }
152 }
153
154 public int getCost() {
155 return 0;
156 }
157
158 public Expression[] getExpressionColumns(Session session) {
159 if (getType() == Value.ARRAY) {
160 return getExpressionColumns(session, (ValueArray) getValue(session));
161 }
162 return super.getExpressionColumns(session);
163 }
164 }
165