Пример #1
0
  public Object floor(Object a) {

    if (a == null) {
      return null;
    }

    switch (typeCode) {
      case Types.SQL_REAL:
      case Types.SQL_FLOAT:
      case Types.SQL_DOUBLE:
        {
          double value = Math.floor(((Double) a).doubleValue());

          if (Double.isInfinite(value)) {
            throw Error.error(ErrorCode.X_22003);
          }

          return ValuePool.getDouble(Double.doubleToLongBits(value));
        }
      case Types.SQL_NUMERIC:
      case Types.SQL_DECIMAL:
        {
          BigDecimal value = ((BigDecimal) a).setScale(0, BigDecimal.ROUND_FLOOR);

          return value;
        }

        // fall through
      default:
        return a;
    }
  }
Пример #2
0
  @Override
  public Object add(Object a, Object b, Type otherType) {

    if (a == null || b == null) {
      return null;
    }

    switch (typeCode) {
      case Types.SQL_REAL:
      case Types.SQL_FLOAT:
      case Types.SQL_DOUBLE:
        {
          double ad = ((Number) a).doubleValue();
          double bd = ((Number) b).doubleValue();

          return ValuePool.getDouble(Double.doubleToLongBits(ad + bd));

          //                return new Double(ad + bd);
        }
      case Types.SQL_NUMERIC:
      case Types.SQL_DECIMAL:
        {
          a = convertToDefaultType(null, a);
          b = convertToDefaultType(null, b);

          BigDecimal abd = (BigDecimal) a;
          BigDecimal bbd = (BigDecimal) b;

          return abd.add(bbd);
        }
      case Types.TINYINT:
      case Types.SQL_SMALLINT:
      case Types.SQL_INTEGER:
        {
          int ai = ((Number) a).intValue();
          int bi = ((Number) b).intValue();

          return ValuePool.getInt(ai + bi);
        }
      case Types.SQL_BIGINT:
        {
          long longa = ((Number) a).longValue();
          long longb = ((Number) b).longValue();

          return ValuePool.getLong(longa + longb);
        }
      default:
        throw Error.runtimeError(ErrorCode.U_S0500, "NumberType");
    }
  }
Пример #3
0
  /**
   * Converter from a numeric object to Double. Input is checked to be within range represented by
   * Double
   */
  private static Double convertToDouble(Object a) {

    double value;

    if (a instanceof java.lang.Double) {
      return (Double) a;
    } else if (a instanceof BigDecimal) {
      BigDecimal bd = (BigDecimal) a;

      value = bd.doubleValue();

      int signum = bd.signum();
      BigDecimal bdd = new BigDecimal(value + signum);

      if (bdd.compareTo(bd) != signum) {
        throw Error.error(ErrorCode.X_22003);
      }
    } else {
      value = ((Number) a).doubleValue();
    }

    return ValuePool.getDouble(Double.doubleToLongBits(value));
  }
Пример #4
0
  @Override
  public Object negate(Object a) {

    if (a == null) {
      return null;
    }

    switch (typeCode) {
      case Types.SQL_REAL:
      case Types.SQL_FLOAT:
      case Types.SQL_DOUBLE:
        {
          double ad = -((Number) a).doubleValue();

          return ValuePool.getDouble(Double.doubleToLongBits(ad));
        }
      case Types.SQL_NUMERIC:
      case Types.SQL_DECIMAL:
        return ((BigDecimal) a).negate();

      case Types.TINYINT:
        {
          int value = ((Number) a).intValue();

          if (value == Byte.MIN_VALUE) {
            throw Error.error(ErrorCode.X_22003);
          }

          return ValuePool.getInt(-value);
        }
      case Types.SQL_SMALLINT:
        {
          int value = ((Number) a).intValue();

          if (value == Short.MIN_VALUE) {
            throw Error.error(ErrorCode.X_22003);
          }

          return ValuePool.getInt(-value);
        }
      case Types.SQL_INTEGER:
        {
          int value = ((Number) a).intValue();

          if (value == Integer.MIN_VALUE) {
            throw Error.error(ErrorCode.X_22003);
          }

          return ValuePool.getInt(-value);
        }
      case Types.SQL_BIGINT:
        {
          long value = ((Number) a).longValue();

          if (value == Long.MIN_VALUE) {
            throw Error.error(ErrorCode.X_22003);
          }

          return ValuePool.getLong(-value);
        }
      default:
        throw Error.runtimeError(ErrorCode.U_S0500, "NumberType");
    }
  }
Пример #5
0
  @Override
  public Object divide(Object a, Object b) {

    if (a == null || b == null) {
      return null;
    }

    switch (typeCode) {
      case Types.SQL_REAL:
      case Types.SQL_FLOAT:
      case Types.SQL_DOUBLE:
        {
          double ad = ((Number) a).doubleValue();
          double bd = ((Number) b).doubleValue();

          if (bd == 0) {
            throw Error.error(ErrorCode.X_22012);
          }

          return ValuePool.getDouble(Double.doubleToLongBits(ad / bd));
        }
      case Types.SQL_NUMERIC:
      case Types.SQL_DECIMAL:
        {
          a = convertToDefaultType(null, a);
          b = convertToDefaultType(null, b);

          BigDecimal abd = (BigDecimal) a;
          BigDecimal bbd = (BigDecimal) b;
          int scale = abd.scale() > bbd.scale() ? abd.scale() : bbd.scale();

          if (bbd.signum() == 0) {
            throw Error.error(ErrorCode.X_22012);
          }

          return abd.divide(bbd, scale, BigDecimal.ROUND_DOWN);
        }
      case Types.TINYINT:
      case Types.SQL_SMALLINT:
      case Types.SQL_INTEGER:
        {
          int ai = ((Number) a).intValue();
          int bi = ((Number) b).intValue();

          if (bi == 0) {
            throw Error.error(ErrorCode.X_22012);
          }

          return ValuePool.getInt(ai / bi);
        }
      case Types.SQL_BIGINT:
        {
          long al = ((Number) a).longValue();
          long bl = ((Number) b).longValue();

          if (bl == 0) {
            throw Error.error(ErrorCode.X_22012);
          }

          return ValuePool.getLong(al / bl);
        }
      default:
        throw Error.runtimeError(ErrorCode.U_S0500, "NumberType");
    }
  }