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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
/*
* 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 org.h2.value;
import java.math.BigDecimal;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import org.h2.api.ErrorCode;
import org.h2.message.DbException;
import org.h2.util.MathUtils;
/**
* Implementation of the DECIMAL data type.
*/
public class ValueDecimal extends Value {
/**
* The value 'zero'.
*/
public static final Object ZERO = new ValueDecimal(BigDecimal.ZERO);
/**
* The value 'one'.
*/
public static final Object ONE = new ValueDecimal(BigDecimal.ONE);
/**
* The default precision for a decimal value.
*/
static final int DEFAULT_PRECISION = 65535;
/**
* The default scale for a decimal value.
*/
static final int DEFAULT_SCALE = 32767;
/**
* The default display size for a decimal value.
*/
static final int DEFAULT_DISPLAY_SIZE = 65535;
private static final int DIVIDE_SCALE_ADD = 25;
/**
* The maximum scale of a BigDecimal value.
*/
private static final int BIG_DECIMAL_SCALE_MAX = 100000;
private final BigDecimal value;
private String valueString;
private int precision;
private ValueDecimal(BigDecimal value) {
if (value == null) {
throw new IllegalArgumentException();
} else if (!value.getClass().equals(BigDecimal.class)) {
throw DbException.get(ErrorCode.INVALID_CLASS_2,
BigDecimal.class.getName(), value.getClass().getName());
}
this.value = value;
}
@Override
public Value add(Value v) {
ValueDecimal dec = (ValueDecimal) v;
return ValueDecimal.get(value.add(dec.value));
}
@Override
public Value subtract(Value v) {
ValueDecimal dec = (ValueDecimal) v;
return ValueDecimal.get(value.subtract(dec.value));
}
@Override
public Value negate() {
return ValueDecimal.get(value.negate());
}
@Override
public Value multiply(Value v) {
ValueDecimal dec = (ValueDecimal) v;
return ValueDecimal.get(value.multiply(dec.value));
}
@Override
public Value divide(Value v) {
ValueDecimal dec = (ValueDecimal) v;
if (dec.value.signum() == 0) {
throw DbException.get(ErrorCode.DIVISION_BY_ZERO_1, getSQL());
}
BigDecimal bd = value.divide(dec.value,
value.scale() + DIVIDE_SCALE_ADD,
BigDecimal.ROUND_HALF_DOWN);
if (bd.signum() == 0) {
bd = BigDecimal.ZERO;
} else if (bd.scale() > 0) {
if (!bd.unscaledValue().testBit(0)) {
bd = bd.stripTrailingZeros();
}
}
return ValueDecimal.get(bd);
}
@Override
public ValueDecimal modulus(Value v) {
ValueDecimal dec = (ValueDecimal) v;
if (dec.value.signum() == 0) {
throw DbException.get(ErrorCode.DIVISION_BY_ZERO_1, getSQL());
}
BigDecimal bd = value.remainder(dec.value);
return ValueDecimal.get(bd);
}
@Override
public String getSQL() {
return getString();
}
@Override
public int getType() {
return Value.DECIMAL;
}
@Override
protected int compareSecure(Value o, CompareMode mode) {
ValueDecimal v = (ValueDecimal) o;
return value.compareTo(v.value);
}
@Override
public int getSignum() {
return value.signum();
}
@Override
public BigDecimal getBigDecimal() {
return value;
}
@Override
public String getString() {
if (valueString == null) {
String p = value.toPlainString();
if (p.length() < 40) {
valueString = p;
} else {
valueString = value.toString();
}
}
return valueString;
}
@Override
public long getPrecision() {
if (precision == 0) {
precision = value.precision();
}
return precision;
}
@Override
public boolean checkPrecision(long prec) {
if (prec == DEFAULT_PRECISION) {
return true;
}
return getPrecision() <= prec;
}
@Override
public int getScale() {
return value.scale();
}
@Override
public int hashCode() {
return value.hashCode();
}
@Override
public Object getObject() {
return value;
}
@Override
public void set(PreparedStatement prep, int parameterIndex)
throws SQLException {
prep.setBigDecimal(parameterIndex, value);
}
@Override
public Value convertScale(boolean onlyToSmallerScale, int targetScale) {
if (value.scale() == targetScale) {
return this;
}
if (onlyToSmallerScale || targetScale >= DEFAULT_SCALE) {
if (value.scale() < targetScale) {
return this;
}
}
BigDecimal bd = ValueDecimal.setScale(value, targetScale);
return ValueDecimal.get(bd);
}
@Override
public Value convertPrecision(long precision, boolean force) {
if (getPrecision() <= precision) {
return this;
}
if (force) {
return get(BigDecimal.valueOf(value.doubleValue()));
}
throw DbException.get(
ErrorCode.NUMERIC_VALUE_OUT_OF_RANGE_1,
Long.toString(precision));
}
/**
* Get or create big decimal value for the given big decimal.
*
* @param dec the bit decimal
* @return the value
*/
public static ValueDecimal get(BigDecimal dec) {
if (BigDecimal.ZERO.equals(dec)) {
return (ValueDecimal) ZERO;
} else if (BigDecimal.ONE.equals(dec)) {
return (ValueDecimal) ONE;
}
return (ValueDecimal) Value.cache(new ValueDecimal(dec));
}
@Override
public int getDisplaySize() {
// add 2 characters for '-' and '.'
return MathUtils.convertLongToInt(getPrecision() + 2);
}
@Override
public boolean equals(Object other) {
// Two BigDecimal objects are considered equal only if they are equal in
// value and scale (thus 2.0 is not equal to 2.00 when using equals;
// however -0.0 and 0.0 are). Can not use compareTo because 2.0 and 2.00
// have different hash codes
return other instanceof ValueDecimal &&
value.equals(((ValueDecimal) other).value);
}
@Override
public int getMemory() {
return value.precision() + 120;
}
/**
* Set the scale of a BigDecimal value.
*
* @param bd the BigDecimal value
* @param scale the new scale
* @return the scaled value
*/
public static BigDecimal setScale(BigDecimal bd, int scale) {
if (scale > BIG_DECIMAL_SCALE_MAX || scale < -BIG_DECIMAL_SCALE_MAX) {
throw DbException.getInvalidValueException("scale", scale);
}
return bd.setScale(scale, BigDecimal.ROUND_HALF_UP);
}
}