Ejemplo n.º 1
0
  /**
   * Converter from a numeric object to Long. Input is checked to be within range represented by
   * Long.
   */
  static Long convertToLong(Object a) {

    if (a instanceof Integer) {
      return ValuePool.getLong(((Integer) a).intValue());
    } else if (a instanceof Long) {
      return (Long) a;
    } else if (a instanceof BigDecimal) {
      BigDecimal bd = (BigDecimal) a;

      if (bd.compareTo(MAX_LONG) > 0 || bd.compareTo(MIN_LONG) < 0) {
        throw Error.error(ErrorCode.X_22003);
      }

      return ValuePool.getLong(bd.longValue());
    } else if (a instanceof Double || a instanceof Float) {
      double d = ((Number) a).doubleValue();

      if (Double.isInfinite(d)
          || Double.isNaN(d)
          || d >= (double) Long.MAX_VALUE + 1
          || d <= (double) Long.MIN_VALUE - 1) {
        throw Error.error(ErrorCode.X_22003);
      }

      return ValuePool.getLong((long) d);
    } else {
      throw Error.error(ErrorCode.X_42561);
    }
  }
Ejemplo n.º 2
0
  /**
   * Converter from a numeric object to Long. Input is checked to be within range represented by
   * Long.
   */
  static Long convertToLong(SessionInterface session, Object a) {

    if (a instanceof Integer) {
      return ValuePool.getLong(((Integer) a).intValue());
    } else if (a instanceof Long) {
      return (Long) a;
    } else if (a instanceof BigDecimal) {
      BigDecimal bd = (BigDecimal) a;

      if (bd.compareTo(MAX_LONG) > 0 || bd.compareTo(MIN_LONG) < 0) {
        throw Error.error(ErrorCode.X_22003);
      }

      return ValuePool.getLong(bd.longValue());
    } else if (a instanceof Double || a instanceof Float) {
      double d = ((Number) a).doubleValue();

      if (session instanceof Session) {
        if (!((Session) session).database.sqlConvertTruncate) {
          d = java.lang.Math.rint(d);
        }
      }

      if (Double.isInfinite(d)
          || Double.isNaN(d)
          || d >= (double) Long.MAX_VALUE + 1
          || d <= (double) Long.MIN_VALUE - 1) {
        throw Error.error(ErrorCode.X_22003);
      }

      return ValuePool.getLong((long) d);
    } else {
      throw Error.error(ErrorCode.X_42561);
    }
  }
Ejemplo n.º 3
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");
    }
  }
Ejemplo n.º 4
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;
    }
  }
Ejemplo n.º 5
0
    protected void addFoundRow() {

      if (rangeVar.isRightJoin) {
        try {
          lookupTable.insertData(lookupStore, new Object[] {ValuePool.getInt(currentRow.getPos())});
        } catch (HsqlException e) {
        }
      }
    }
Ejemplo n.º 6
0
  /**
   * Converter from a numeric object to Integer. Input is checked to be within range represented by
   * the given number type.
   */
  static Integer convertToInt(Object a, int type) {

    int value;

    if (a instanceof Integer) {
      if (type == Types.SQL_INTEGER) {
        return (Integer) a;
      }

      value = ((Integer) a).intValue();
    } else if (a instanceof Long) {
      long temp = ((Long) a).longValue();

      if (Integer.MAX_VALUE < temp || temp < Integer.MIN_VALUE) {
        throw Error.error(ErrorCode.X_22003);
      }

      value = (int) temp;
    } else if (a instanceof BigDecimal) {
      BigDecimal bd = ((BigDecimal) a);

      if (bd.compareTo(MAX_INT) > 0 || bd.compareTo(MIN_INT) < 0) {
        throw Error.error(ErrorCode.X_22003);
      }

      value = bd.intValue();
    } else if (a instanceof Double || a instanceof Float) {
      double d = ((Number) a).doubleValue();

      if (Double.isInfinite(d)
          || Double.isNaN(d)
          || d >= (double) Integer.MAX_VALUE + 1
          || d <= (double) Integer.MIN_VALUE - 1) {
        throw Error.error(ErrorCode.X_22003);
      }

      value = (int) d;
    } else {
      throw Error.error(ErrorCode.X_42561);
    }

    if (type == Types.TINYINT) {
      if (Byte.MAX_VALUE < value || value < Byte.MIN_VALUE) {
        throw Error.error(ErrorCode.X_22003);
      }
    } else if (type == Types.SQL_SMALLINT) {
      if (Short.MAX_VALUE < value || value < Short.MIN_VALUE) {
        throw Error.error(ErrorCode.X_22003);
      }
    }

    return ValuePool.getInt(value);
  }
Ejemplo n.º 7
0
    protected boolean findNext() {

      boolean result;

      while (true) {
        currentRow = it.getNextRow();

        if (currentRow == null) {
          result = false;

          break;
        }

        RowIterator lookupIterator =
            lookupTable.indexList[0].findFirstRow(
                session, lookupStore, ValuePool.getInt(currentRow.getPos()), OpTypes.EQUAL);

        result = !lookupIterator.hasNext();

        lookupIterator.release();

        if (result) {
          currentData = currentRow.getData();

          if (rangeVar.nonIndexWhereCondition != null
              && !rangeVar.nonIndexWhereCondition.testCondition(session)) {
            continue;
          }

          isBeforeFirst = false;

          return true;
        }
      }

      it.release();

      currentRow = null;
      currentData = rangeVar.emptyData;

      return result;
    }
Ejemplo n.º 8
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));
  }
Ejemplo n.º 9
0
  /** Converts a value to this type */
  @Override
  public Object convertToDefaultType(SessionInterface session, Object a) {

    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) {
        // BEGIN Cherry-picked code change from hsqldb-2.2.8
        otherType = Type.SQL_DECIMAL_DEFAULT;
        /*
                        if (typeCode == Types.SQL_DECIMAL
                                || typeCode == Types.SQL_NUMERIC) {
                            return convertToTypeLimits(session, a);
                        }

                        BigDecimal val = (BigDecimal) a;

                        otherType = getNumberType(Types.SQL_DECIMAL,
                                                  JavaSystem.precision(val), scale);
        */
        // END Cherry-picked code change from hsqldb-2.2.8
      } else {
        throw Error.error(ErrorCode.X_42561);
      }

      // BEGIN Cherry-picked code change from hsqldb-2.2.8
      switch (typeCode) {
        case Types.TINYINT:
        case Types.SQL_SMALLINT:
        case Types.SQL_INTEGER:
          return convertToInt(session, a, Types.INTEGER);

        case Types.SQL_BIGINT:
          return convertToLong(session, a);

        case Types.SQL_REAL:
        case Types.SQL_FLOAT:
        case Types.SQL_DOUBLE:
          return convertToDouble(a);

        case Types.SQL_NUMERIC:
        case Types.SQL_DECIMAL:
          {
            a = convertToDecimal(a);

            BigDecimal dec = (BigDecimal) a;

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

            return dec;
          }
        default:
          throw Error.error(ErrorCode.X_42561);
      }
      // END Cherry-picked code change from hsqldb-2.2.8
    } else if (a instanceof String) {
      otherType = Type.SQL_VARCHAR;
    } else {
      throw Error.error(ErrorCode.X_42561);
    }

    return convertToType(session, a, otherType);
  }
Ejemplo n.º 10
0
  @Override
  public Object convertToType(SessionInterface session, Object a, Type otherType) {

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

    if (otherType.typeCode == typeCode) {
      switch (typeCode) {
        case Types.SQL_NUMERIC:
        case Types.SQL_DECIMAL:
          if (otherType.scale == scale && otherType.precision <= precision) {
            return a;
          }
          break;

        default:
          return a;
      }
    }

    if (otherType.isIntervalType()) {
      int endType = ((IntervalType) otherType).endIntervalType;

      switch (endType) {
        case Types.SQL_INTERVAL_YEAR:
        case Types.SQL_INTERVAL_MONTH:
        case Types.SQL_INTERVAL_DAY:
        case Types.SQL_INTERVAL_HOUR:
        case Types.SQL_INTERVAL_MINUTE:
          {
            Long value = ValuePool.getLong(((IntervalType) otherType).convertToLong(a));

            return convertToType(session, value, Type.SQL_BIGINT);
          }
        case Types.SQL_INTERVAL_SECOND:
          {
            long seconds = ((IntervalSecondData) a).units;
            long nanos = ((IntervalSecondData) a).nanos;
            BigDecimal value = ((DTIType) otherType).getSecondPart(seconds, nanos);

            return value;
          }
      }
    }

    switch (otherType.typeCode) {
      case Types.SQL_CLOB:
        a = ((ClobData) a).getSubString(session, 0L, (int) ((ClobData) a).length(session));

        // fall through
      case Types.SQL_CHAR:
      case Types.SQL_VARCHAR:
      case Types.VARCHAR_IGNORECASE:
        {
          a = session.getScanner().convertToNumber((String) a, this);

          return convertToDefaultType(session, a);
        }
      case Types.TINYINT:
      case Types.SQL_SMALLINT:
      case Types.SQL_INTEGER:
      case Types.SQL_BIGINT:
      case Types.SQL_REAL:
      case Types.SQL_FLOAT:
      case Types.SQL_DOUBLE:
      case Types.SQL_NUMERIC:
      case Types.SQL_DECIMAL:
        break;

      default:
        throw Error.error(ErrorCode.X_42561);
    }

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

      case Types.SQL_BIGINT:
        return convertToLong(a);

      case Types.SQL_REAL:
      case Types.SQL_FLOAT:
      case Types.SQL_DOUBLE:
        return convertToDouble(a);

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

        return convertToTypeLimits(session, value);

      default:
        throw Error.error(ErrorCode.X_42561);
    }
  }
Ejemplo n.º 11
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");
    }
  }
Ejemplo n.º 12
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");
    }
  }