Beispiel #1
0
    protected void addFoundRow() {

      if (rangeVar.isRightJoin) {
        try {
          lookupTable.insertData(lookupStore, new Object[] {ValuePool.getInt(currentRow.getPos())});
        } catch (HsqlException e) {
        }
      }
    }
Beispiel #2
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);
  }
Beispiel #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");
    }
  }
Beispiel #4
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;
    }
Beispiel #5
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);
  }
Beispiel #6
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");
    }
  }
Beispiel #7
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");
    }
  }