| 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.value; |
| 7 |
|
| 8 |
import java.math.BigDecimal; |
| 9 |
import java.math.BigInteger; |
| 10 |
import java.math.RoundingMode; |
| 11 |
|
| 12 |
import org.h2.api.ErrorCode; |
| 13 |
import org.h2.engine.CastDataProvider; |
| 14 |
import org.h2.message.DbException; |
| 15 |
|
| 16 |
/** |
| 17 |
* Implementation of the NUMERIC data type. |
| 18 |
*/ |
| 19 |
public final class ValueNumeric extends ValueBigDecimalBase { |
| 20 |
|
| 21 |
/** |
| 22 |
* The value 'zero'. |
| 23 |
*/ |
| 24 |
public static final ValueNumeric ZERO = new ValueNumeric(BigDecimal.ZERO); |
| 25 |
|
| 26 |
/** |
| 27 |
* The value 'one'. |
| 28 |
*/ |
| 29 |
public static final ValueNumeric ONE = new ValueNumeric(BigDecimal.ONE); |
| 30 |
|
| 31 |
/** |
| 32 |
* The default scale for a NUMERIC value. |
| 33 |
*/ |
| 34 |
public static final int DEFAULT_SCALE = 0; |
| 35 |
|
| 36 |
/** |
| 37 |
* The maximum scale. |
| 38 |
*/ |
| 39 |
public static final int MAXIMUM_SCALE = 100_000; |
| 40 |
|
| 41 |
private ValueNumeric(BigDecimal value) { |
| 42 |
super(value); |
| 43 |
if (value == null) { |
| 44 |
throw new IllegalArgumentException("null"); |
| 45 |
} |
| 46 |
int scale = value.scale(); |
| 47 |
if (scale < 0 || scale > MAXIMUM_SCALE) { |
| 48 |
throw DbException.get(ErrorCode.INVALID_VALUE_SCALE, Integer.toString(scale), "0", "" + MAXIMUM_SCALE); |
| 49 |
} |
| 50 |
} |
| 51 |
|
| 52 |
@Override |
| 53 |
public String getString() { |
| 54 |
return value.toPlainString(); |
| 55 |
} |
| 56 |
|
| 57 |
@Override |
| 58 |
public StringBuilder getSQL(StringBuilder builder, int sqlFlags) { |
| 59 |
String s = getString(); |
| 60 |
if ((sqlFlags & NO_CASTS) == 0 && s.indexOf('.') < 0 && value.compareTo(MAX_LONG_DECIMAL) <= 0 |
| 61 |
&& value.compareTo(MIN_LONG_DECIMAL) >= 0) { |
| 62 |
return builder.append("CAST(").append(value).append(" AS NUMERIC(").append(value.precision()).append("))"); |
| 63 |
} |
| 64 |
return builder.append(s); |
| 65 |
} |
| 66 |
|
| 67 |
@Override |
| 68 |
public TypeInfo getType() { |
| 69 |
TypeInfo type = this.type; |
| 70 |
if (type == null) { |
| 71 |
this.type = type = new TypeInfo(NUMERIC, value.precision(), value.scale(), null); |
| 72 |
} |
| 73 |
return type; |
| 74 |
} |
| 75 |
|
| 76 |
@Override |
| 77 |
public int getValueType() { |
| 78 |
return NUMERIC; |
| 79 |
} |
| 80 |
|
| 81 |
@Override |
| 82 |
public Value add(Value v) { |
| 83 |
return get(value.add(((ValueNumeric) v).value)); |
| 84 |
} |
| 85 |
|
| 86 |
@Override |
| 87 |
public Value subtract(Value v) { |
| 88 |
return get(value.subtract(((ValueNumeric) v).value)); |
| 89 |
} |
| 90 |
|
| 91 |
@Override |
| 92 |
public Value negate() { |
| 93 |
return get(value.negate()); |
| 94 |
} |
| 95 |
|
| 96 |
@Override |
| 97 |
public Value multiply(Value v) { |
| 98 |
return get(value.multiply(((ValueNumeric) v).value)); |
| 99 |
} |
| 100 |
|
| 101 |
@Override |
| 102 |
public Value divide(Value v, TypeInfo quotientType) { |
| 103 |
BigDecimal divisor = ((ValueNumeric) v).value; |
| 104 |
if (divisor.signum() == 0) { |
| 105 |
throw DbException.get(ErrorCode.DIVISION_BY_ZERO_1, getTraceSQL()); |
| 106 |
} |
| 107 |
return get(value.divide(divisor, quotientType.getScale(), RoundingMode.HALF_DOWN)); |
| 108 |
} |
| 109 |
|
| 110 |
@Override |
| 111 |
public Value modulus(Value v) { |
| 112 |
ValueBigDecimalBase dec = (ValueNumeric) v; |
| 113 |
if (dec.value.signum() == 0) { |
| 114 |
throw DbException.get(ErrorCode.DIVISION_BY_ZERO_1, getTraceSQL()); |
| 115 |
} |
| 116 |
return get(value.remainder(dec.value)); |
| 117 |
} |
| 118 |
|
| 119 |
@Override |
| 120 |
public int compareTypeSafe(Value o, CompareMode mode, CastDataProvider provider) { |
| 121 |
return value.compareTo(((ValueNumeric) o).value); |
| 122 |
} |
| 123 |
|
| 124 |
@Override |
| 125 |
public int getSignum() { |
| 126 |
return value.signum(); |
| 127 |
} |
| 128 |
|
| 129 |
@Override |
| 130 |
public BigDecimal getBigDecimal() { |
| 131 |
return value; |
| 132 |
} |
| 133 |
|
| 134 |
@Override |
| 135 |
public float getFloat() { |
| 136 |
return value.floatValue(); |
| 137 |
} |
| 138 |
|
| 139 |
@Override |
| 140 |
public double getDouble() { |
| 141 |
return value.doubleValue(); |
| 142 |
} |
| 143 |
|
| 144 |
@Override |
| 145 |
public int hashCode() { |
| 146 |
return getClass().hashCode() * 31 + value.hashCode(); |
| 147 |
} |
| 148 |
|
| 149 |
@Override |
| 150 |
public boolean equals(Object other) { |
| 151 |
return other instanceof ValueNumeric && value.equals(((ValueNumeric) other).value); |
| 152 |
} |
| 153 |
|
| 154 |
@Override |
| 155 |
public int getMemory() { |
| 156 |
return value.precision() + 120; |
| 157 |
} |
| 158 |
|
| 159 |
/** |
| 160 |
* Get or create a NUMERIC value for the given big decimal. |
| 161 |
* |
| 162 |
* @param dec the big decimal |
| 163 |
* @return the value |
| 164 |
*/ |
| 165 |
public static ValueNumeric get(BigDecimal dec) { |
| 166 |
if (BigDecimal.ZERO.equals(dec)) { |
| 167 |
return ZERO; |
| 168 |
} else if (BigDecimal.ONE.equals(dec)) { |
| 169 |
return ONE; |
| 170 |
} |
| 171 |
return (ValueNumeric) Value.cache(new ValueNumeric(dec)); |
| 172 |
} |
| 173 |
|
| 174 |
/** |
| 175 |
* Get or create a NUMERIC value for the given big decimal with possibly |
| 176 |
* negative scale. If scale is negative, it is normalized to 0. |
| 177 |
* |
| 178 |
* @param dec |
| 179 |
* the big decimal |
| 180 |
* @return the value |
| 181 |
*/ |
| 182 |
public static ValueNumeric getAnyScale(BigDecimal dec) { |
| 183 |
if (dec.scale() < 0) { |
| 184 |
dec = dec.setScale(0, RoundingMode.UNNECESSARY); |
| 185 |
} |
| 186 |
return get(dec); |
| 187 |
} |
| 188 |
|
| 189 |
/** |
| 190 |
* Get or create a NUMERIC value for the given big integer. |
| 191 |
* |
| 192 |
* @param bigInteger the big integer |
| 193 |
* @return the value |
| 194 |
*/ |
| 195 |
public static ValueNumeric get(BigInteger bigInteger) { |
| 196 |
if (bigInteger.signum() == 0) { |
| 197 |
return ZERO; |
| 198 |
} else if (BigInteger.ONE.equals(bigInteger)) { |
| 199 |
return ONE; |
| 200 |
} |
| 201 |
return (ValueNumeric) Value.cache(new ValueNumeric(new BigDecimal(bigInteger))); |
| 202 |
} |
| 203 |
|
| 204 |
/** |
| 205 |
* Set the scale of a BigDecimal value. |
| 206 |
* |
| 207 |
* @param bd the BigDecimal value |
| 208 |
* @param scale the new scale |
| 209 |
* @return the scaled value |
| 210 |
*/ |
| 211 |
public static BigDecimal setScale(BigDecimal bd, int scale) { |
| 212 |
if (scale < 0 || scale > MAXIMUM_SCALE) { |
| 213 |
throw DbException.getInvalidValueException("scale", scale); |
| 214 |
} |
| 215 |
return bd.setScale(scale, RoundingMode.HALF_UP); |
| 216 |
} |
| 217 |
|
| 218 |
} |