protected Double readReal() throws IOException, HsqlException {

    String s = readNumberField();

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

    double i = JavaSystem.parseDouble(s);

    if (tokenizer.isGetThis(Token.T_DIVIDE)) {
      s = tokenizer.getString();

      // parse simply to ensure it's a number
      double ii = JavaSystem.parseDouble(s);

      if (i == 0E0) {
        i = Double.NaN;
      } else if (i == -1E0) {
        i = Double.NEGATIVE_INFINITY;
      } else if (i == 1E0) {
        i = Double.POSITIVE_INFINITY;
      }
    }

    return ValuePool.getDouble(Double.doubleToLongBits(i));
  }
  public Object floor(Object a) throws HsqlException {

    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);

          if (JavaSystem.precision(value) > precision) {
            throw Error.error(ErrorCode.X_22003);
          }
        }

        // fall through
      default:
        return a;
    }
  }
  public String convertToString(Object a) {

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

    switch (this.typeCode) {
      case Types.TINYINT:
      case Types.SQL_SMALLINT:
      case Types.SQL_INTEGER:
      case Types.SQL_BIGINT:
        return a.toString();

      case Types.SQL_REAL:
      case Types.SQL_DOUBLE:
        double value = ((Double) a).doubleValue();

        // todo - java 5 format change
        if (value == Double.NEGATIVE_INFINITY) {
          return "-1E0/0";
        }

        if (value == Double.POSITIVE_INFINITY) {
          return "1E0/0";
        }

        if (Double.isNaN(value)) {
          return "0E0/0E0";
        }

        String s = Double.toString(value);

        // ensure the engine treats the value as a DOUBLE, not DECIMAL
        if (s.indexOf('E') < 0) {
          s = s.concat("E0");
        }

        return s;

      case Types.SQL_NUMERIC:
      case Types.SQL_DECIMAL:
        return JavaSystem.toString((BigDecimal) a);

      default:
        throw Error.runtimeError(ErrorCode.U_S0500, "NumberType");
    }
  }
  /** Converts a value to this type */
  public Object convertToDefaultType(SessionInterface session, Object a) throws HsqlException {

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

    Type otherType;

    if (a instanceof Number) {
      if (a instanceof BigInteger) {
        a = new BigDecimal((BigInteger) a);
      } else if (a instanceof Float) {
        a = new Double(((Float) a).doubleValue());
      } else if (a instanceof Byte) {
        a = ValuePool.getInt(((Byte) a).intValue());
      } else if (a instanceof Short) {
        a = ValuePool.getInt(((Short) a).intValue());
      }

      if (a instanceof Integer) {
        otherType = Type.SQL_INTEGER;
      } else if (a instanceof Long) {
        otherType = Type.SQL_BIGINT;
      } else if (a instanceof Double) {
        otherType = Type.SQL_DOUBLE;
      } else if (a instanceof BigDecimal) {
        if (typeCode == Types.SQL_DECIMAL || typeCode == Types.SQL_NUMERIC) {
          return convertToTypeLimits(a);
        }

        BigDecimal val = (BigDecimal) a;

        otherType = getNumberType(Types.SQL_DECIMAL, JavaSystem.precision(val), scale);
      } else {
        throw Error.error(ErrorCode.X_42561);
      }
    } else if (a instanceof String) {
      otherType = Type.SQL_VARCHAR;
    } else {
      throw Error.error(ErrorCode.X_42561);
    }

    return convertToType(session, a, otherType);
  }
Пример #5
0
  public int read() throws IOException {

    if (currentPosition >= availableLength) {
      return -1;
    }

    if (buffer == null || currentPosition >= bufferOffset + buffer.length) {
      try {
        checkClosed();
        readIntoBuffer();
      } catch (HsqlException e) {
        throw JavaSystem.toIOException(e);
      }
    }

    int val = buffer[(int) (currentPosition - bufferOffset)] & 0xff;

    currentPosition++;

    return val;
  }
Пример #6
0
  /** For literals */
  public static Type getValueType(int type, Object value) {

    long precision = 0;
    int scale = 0;

    if (value != null) {
      switch (type) {
        case Types.SQL_INTEGER:
        case Types.SQL_BIGINT:
        case Types.SQL_DOUBLE:
        case Types.SQL_BOOLEAN:
        case Types.SQL_ALL_TYPES:
          break;

        case Types.SQL_CHAR:
          precision = value.toString().length();
          break;

        case Types.SQL_BINARY:
          precision = ((BinaryData) value).length();
          break;

        case Types.SQL_NUMERIC:
          precision = JavaSystem.precision((BigDecimal) value);
          scale = ((BigDecimal) value).scale();
          break;

        default:
          throw Trace.runtimeError(Trace.UNSUPPORTED_INTERNAL_OPERATION, "Type");
      }
    }

    try {
      return getType(type, 0, precision, scale);
    } catch (HsqlException e) {

      // exception should never be thrown
      throw Trace.runtimeError(Trace.UNSUPPORTED_INTERNAL_OPERATION, "Type");
    }
  }
  /** @todo - review usage to see if range enforcement / java type conversion is necessary */
  public Object convertToTypeLimits(Object a) throws HsqlException {

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

    switch (typeCode) {
      case Types.TINYINT:
      case Types.SQL_SMALLINT:
      case Types.SQL_INTEGER:
      case Types.SQL_BIGINT:
        return a;

      case Types.SQL_REAL:
      case Types.SQL_FLOAT:
      case Types.SQL_DOUBLE:
        return a;

      case Types.SQL_NUMERIC:
      case Types.SQL_DECIMAL:
        BigDecimal dec = (BigDecimal) a;

        if (scale != dec.scale()) {
          dec = dec.setScale(scale, BigDecimal.ROUND_HALF_DOWN);
        }

        int valuePrecision = JavaSystem.precision(dec);

        if (valuePrecision > precision) {
          throw Error.error(ErrorCode.X_22003);
        }

        return dec;

      default:
        throw Error.runtimeError(ErrorCode.U_S0500, "NumberType");
    }
  }