@Override public Value subtract(Value v) { ValueDecimal dec = (ValueDecimal) v; return ValueDecimal.get(value.subtract(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 (ValueDecimal) ZERO; } else if (BigDecimal.ONE.equals(dec)) { return (ValueDecimal) ONE; } return (ValueDecimal) 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 = timeNanos; 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(); }
@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); }
/** * Returns the value of the specified column as a BigDecimal. * * @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 use {@link #getBigDecimal(int)} */ @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 add(Value v) { ValueDecimal dec = (ValueDecimal) v; return ValueDecimal.get(value.add(dec.value)); }
/** * Returns the value of the specified column as a BigDecimal. * * @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 use {@link #getBigDecimal(String)} */ @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 multiply(Value v) { ValueDecimal dec = (ValueDecimal) v; return ValueDecimal.get(value.multiply(dec.value)); }
@Override public Value negate() { return ValueDecimal.get(value.negate()); }
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; }
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); } }
/** * Sets the value of a parameter. * * @param parameterIndex the parameter index (1, 2, ...) * @param x the value * @throws SQLException if this object is closed */ @Override public void setBigDecimal(int parameterIndex, BigDecimal x) throws SQLException { try { if (isDebugEnabled()) { debugCode("setBigDecimal(" + parameterIndex + ", " + quoteBigDecimal(x) + ");"); } Value v = x == null ? (Value) ValueNull.INSTANCE : ValueDecimal.get(x); setParameter(parameterIndex, v); } catch (Exception e) { throw logAndConvert(e); } }
BigDecimal value = rs.getBigDecimal(columnIndex); v = value == null ? (Value) ValueNull.INSTANCE : ValueDecimal.get(value); break;