@Override public Value add(Value v) { ValueDecimal dec = (ValueDecimal) v; return ValueDecimal.get(value.add(dec.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); }
/** * 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 ZERO; } else if (BigDecimal.ONE.equals(dec)) { return ONE; } return (ValueDecimal) Value.cache(new ValueDecimal(dec)); }
@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)); }
@Override public Value convertScale(boolean onlyToSmallerScale, int targetScale) { if (targetScale >= DEFAULT_SCALE) { return this; } if (targetScale < 0) { throw DbException.getInvalidValueException("scale", targetScale); } long n = nanos; BigDecimal bd = BigDecimal.valueOf(n); bd = bd.movePointLeft(9); bd = ValueDecimal.setScale(bd, targetScale); bd = bd.movePointRight(9); long n2 = bd.longValue(); if (n2 == n) { return this; } return fromDateValueAndNanos(dateValue, n2); }
@Override public boolean checkPrecision(long prec) { if (prec == DEFAULT_PRECISION) { return true; } return getPrecision() <= prec; }
@Override public String getSQL() { return getString(); }
public BigDecimal getBigDecimal() { return ((ValueDecimal) convertTo(Value.DECIMAL)).getBigDecimal(); }
@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)) { String s = bd.toString(); int i = s.length() - 1; while (i >= 0 && s.charAt(i) == '0') { i--; } if (i < s.length() - 1) { s = s.substring(0, i + 1); bd = new BigDecimal(s); } } } return ValueDecimal.get(bd); }
/** * Returns the value of the specified column as a BigDecimal. * * @deprecated use {@link #getBigDecimal(int)} * * @param columnIndex (1,2,...) * @param scale the scale of the returned value * @return the value * @throws SQLException if the column is not found or if the result set is * closed */ @Deprecated @Override public BigDecimal getBigDecimal(int columnIndex, int scale) throws SQLException { try { if (isDebugEnabled()) { debugCode("getBigDecimal(" + columnIndex + ", " + scale + ");"); } if (scale < 0) { throw DbException.getInvalidValueException("scale", scale); } BigDecimal bd = get(columnIndex).getBigDecimal(); return bd == null ? null : ValueDecimal.setScale(bd, scale); } catch (Exception e) { throw logAndConvert(e); } }
@Override public int getDisplaySize() { // add 2 characters for '-' and '.' return MathUtils.convertLongToInt(getPrecision() + 2); }
@Override public Value negate() { return ValueDecimal.get(value.negate()); }
/** * Returns the value of the specified column as a BigDecimal. * * @deprecated use {@link #getBigDecimal(String)} * * @param columnLabel the column label * @param scale the scale of the returned value * @return the value * @throws SQLException if the column is not found or if the result set is * closed */ @Deprecated @Override public BigDecimal getBigDecimal(String columnLabel, int scale) throws SQLException { try { if (isDebugEnabled()) { debugCode("getBigDecimal(" + StringUtils.quoteJavaString(columnLabel) + ", " + scale + ");"); } if (scale < 0) { throw DbException.getInvalidValueException("scale", scale); } BigDecimal bd = get(columnLabel).getBigDecimal(); return bd == null ? null : ValueDecimal.setScale(bd, scale); } catch (Exception e) { throw logAndConvert(e); } }
@Override public Value subtract(Value v) { ValueDecimal dec = (ValueDecimal) v; return ValueDecimal.get(value.subtract(dec.value)); }
@Override public Value multiply(Value v) { ValueDecimal dec = (ValueDecimal) v; return ValueDecimal.get(value.multiply(dec.value)); }
private void readHexDecimal(int start, int i) { char[] chars = sqlCommandChars; char c; do { c = chars[++i]; } while ((c >= '0' && c <= '9') || (c >= 'A' && c <= 'F')); parseIndex = i; String sub = sqlCommand.substring(start, i); BigDecimal bd = new BigDecimal(new BigInteger(sub, 16)); checkLiterals(false); currentValue = ValueDecimal.get(bd); currentTokenType = VALUE; }
@Override public Value readValue(ByteBuffer buff, int tag) { switch (tag) { case TAG_BIG_DECIMAL_0: return ZERO; case TAG_BIG_DECIMAL_1: return ONE; case TAG_BIG_DECIMAL_SMALL: return ValueDecimal.get(BigDecimal.valueOf(DataUtils.readVarLong(buff))); case TAG_BIG_DECIMAL_SMALL_SCALED: int scale = DataUtils.readVarInt(buff); return ValueDecimal.get(BigDecimal.valueOf(DataUtils.readVarLong(buff), scale)); } int scale = DataUtils.readVarInt(buff); int len = DataUtils.readVarInt(buff); byte[] bytes = DataUtils.newBytes(len); buff.get(bytes); BigInteger b = new BigInteger(bytes); return ValueDecimal.get(new BigDecimal(b, scale)); } };
throw DbException.get(ErrorCode.DATA_CONVERSION_ERROR_1, e, sub); currentValue = ValueDecimal.get(bd); currentTokenType = VALUE;
/** * Updates a column in the current or insert row. * * @param columnIndex (1,2,...) * @param x the value * @throws SQLException if the result set is closed or not updatable */ @Override public void updateBigDecimal(int columnIndex, BigDecimal x) throws SQLException { try { if (isDebugEnabled()) { debugCode("updateBigDecimal(" + columnIndex + ", " + quoteBigDecimal(x) + ");"); } update(columnIndex, x == null ? (Value) ValueNull.INSTANCE : ValueDecimal.get(x)); } catch (Exception e) { throw logAndConvert(e); } }