Пример #1
0
  private Object convertToSqlValue(Property property, Object value) throws SQLException {

    if (!property.getType().isDataType())
      throw new IllegalArgumentException("expected data type property, not " + property.toString());
    DataType dataType = DataType.valueOf(property.getType().getName());
    Object result;
    switch (dataType) {
      case String:
      case URI:
      case Month:
      case MonthDay:
      case Day:
      case Time:
      case Year:
      case YearMonth:
      case YearMonthDay:
      case Duration:
        result = DataConverter.INSTANCE.toString(property.getType(), value);
        break;
      case Date:
        // Plasma SDO allows more precision than just month/day/year
        // in an SDO date datatype, and using java.sql.Date will truncate
        // here so use java.sql.Timestamp.
        Date date = DataConverter.INSTANCE.toDate(property.getType(), value);
        result = new java.sql.Timestamp(date.getTime());
        break;
      case DateTime:
        date = DataConverter.INSTANCE.toDate(property.getType(), value);
        result = new java.sql.Timestamp(date.getTime());
        break;
      case Decimal:
        result = DataConverter.INSTANCE.toDecimal(property.getType(), value);
        break;
      case Bytes:
        result = DataConverter.INSTANCE.toBytes(property.getType(), value);
        break;
      case Byte:
        result = DataConverter.INSTANCE.toByte(property.getType(), value);
        break;
      case Boolean:
        result = DataConverter.INSTANCE.toBoolean(property.getType(), value);
        break;
      case Character:
        result = DataConverter.INSTANCE.toString(property.getType(), value);
        break;
      case Double:
        result = DataConverter.INSTANCE.toDouble(property.getType(), value);
        break;
      case Float:
        result = DataConverter.INSTANCE.toDouble(property.getType(), value);
        break;
      case Int:
        result = DataConverter.INSTANCE.toInt(property.getType(), value);
        break;
      case Integer:
        result = DataConverter.INSTANCE.toInteger(property.getType(), value);
        break;
      case Long:
        result = DataConverter.INSTANCE.toLong(property.getType(), value);
        break;
      case Short:
        result = DataConverter.INSTANCE.toShort(property.getType(), value);
        break;
      case Strings:
        result = DataConverter.INSTANCE.toString(property.getType(), value);
        break;
      case Object:
      default:
        result = DataConverter.INSTANCE.toString(property.getType(), value);
        break;
    }

    return result;
  }
Пример #2
0
  /**
   * Converts the given value to bytes based on data type and cardinality information for the given
   * property. For 'many' or multi-valued properties, if the SDO Java type for the property is not
   * already String, the value is first converted to String using the SDO conversion which uses
   * java.util.Arrays formatting. For non 'many' or singular properties, only the HBase Bytes
   * utility is used.
   *
   * @param sourceProperty the source property
   * @param value the value
   * @return the bytes
   * @throws IllegalArgumentException if the given property is not a data type property
   */
  public byte[] toBytes(Property sourceProperty, Object value) {
    byte[] result;

    if (!sourceProperty.getType().isDataType())
      throw new IllegalArgumentException(
          "property " + sourceProperty.toString() + " is not a datatype property");
    DataType dataType = DataType.valueOf(sourceProperty.getType().getName());

    switch (dataType) {
        // Data types stored as String bytes in HBase
      case String:
      case Strings:
      case URI:
      case Month:
      case MonthDay:
      case Day:
      case Time:
      case Year:
      case YearMonth:
      case YearMonthDay:
      case Date:
      case Duration:
        String resultStr = DataConverter.INSTANCE.toString(sourceProperty, value);
        result = Bytes.toBytes(resultStr);
        break;
      case DateTime:
        resultStr = DataConverter.INSTANCE.toString(sourceProperty, value);
        result = Bytes.toBytes(resultStr);
        break;
        // Data types stored by directly converting from primitive types to bytes in HBase. When
        // the given property is a 'many' property, the value is first converted to String so
        // can be delimited.
      case Decimal:
        if (!sourceProperty.isMany()) {
          BigDecimal resultDecimal =
              DataConverter.INSTANCE.toDecimal(sourceProperty.getType(), value);
          result = Bytes.toBytes(resultDecimal);
        } else {
          String strResult = DataConverter.INSTANCE.toString(sourceProperty, value);
          result = Bytes.toBytes(strResult);
        }
        break;
      case Bytes:
        if (!sourceProperty.isMany()) {
          byte[] resultBytes = DataConverter.INSTANCE.toBytes(sourceProperty.getType(), value);
          result = resultBytes;
        } else {
          String strResult = DataConverter.INSTANCE.toString(sourceProperty, value);
          result = Bytes.toBytes(strResult);
        }
        break;
      case Byte:
        if (!sourceProperty.isMany()) {
          byte resultByte = DataConverter.INSTANCE.toByte(sourceProperty.getType(), value);
          result = Bytes.toBytes(resultByte);
        } else {
          String strResult = DataConverter.INSTANCE.toString(sourceProperty, value);
          result = Bytes.toBytes(strResult);
        }
        break;
      case Boolean:
        if (!sourceProperty.isMany()) {
          boolean resultBool = DataConverter.INSTANCE.toBoolean(sourceProperty.getType(), value);
          result = Bytes.toBytes(resultBool);
        } else {
          String strResult = DataConverter.INSTANCE.toString(sourceProperty, value);
          result = Bytes.toBytes(strResult);
        }
        break;
      case Character:
        if (!sourceProperty.isMany()) {
          resultStr = DataConverter.INSTANCE.toString(sourceProperty.getType(), value);
          result = Bytes.toBytes(resultStr);
        } else {
          String strResult = DataConverter.INSTANCE.toString(sourceProperty, value);
          result = Bytes.toBytes(strResult);
        }
        break;
      case Double:
        if (!sourceProperty.isMany()) {
          double resultDouble = DataConverter.INSTANCE.toDouble(sourceProperty.getType(), value);
          result = Bytes.toBytes(resultDouble);
        } else {
          String strResult = DataConverter.INSTANCE.toString(sourceProperty, value);
          result = Bytes.toBytes(strResult);
        }
        break;
      case Float:
        if (!sourceProperty.isMany()) {
          float resultFloat = DataConverter.INSTANCE.toFloat(sourceProperty.getType(), value);
          result = Bytes.toBytes(resultFloat);
        } else {
          String strResult = DataConverter.INSTANCE.toString(sourceProperty, value);
          result = Bytes.toBytes(strResult);
        }
        break;
      case Int:
        if (!sourceProperty.isMany()) {
          int resultInt = DataConverter.INSTANCE.toInt(sourceProperty.getType(), value);
          result = Bytes.toBytes(resultInt);
        } else {
          String strResult = DataConverter.INSTANCE.toString(sourceProperty, value);
          result = Bytes.toBytes(strResult);
        }
        break;
      case Integer:
        if (!sourceProperty.isMany()) {
          BigInteger resultInteger =
              DataConverter.INSTANCE.toInteger(sourceProperty.getType(), value);
          result = resultInteger.toByteArray();
        } else {
          String strResult = DataConverter.INSTANCE.toString(sourceProperty, value);
          result = Bytes.toBytes(strResult);
        }
        break;
      case Long:
        if (!sourceProperty.isMany()) {
          long resultLong = DataConverter.INSTANCE.toLong(sourceProperty.getType(), value);
          result = Bytes.toBytes(resultLong);
        } else {
          String strResult = DataConverter.INSTANCE.toString(sourceProperty, value);
          result = Bytes.toBytes(strResult);
        }
        break;
      case Short:
        if (!sourceProperty.isMany()) {
          short resultShort = DataConverter.INSTANCE.toShort(sourceProperty.getType(), value);
          result = Bytes.toBytes(resultShort);
        } else {
          String strResult = DataConverter.INSTANCE.toString(sourceProperty, value);
          result = Bytes.toBytes(strResult);
        }
        break;
      case Object:
        // FIXME: do we serialize objects in some custom format for hbase
      default:
        resultStr = DataConverter.INSTANCE.toString(sourceProperty.getType(), value);
        result = Bytes.toBytes(resultStr);
        break;
    }
    return result;
  }