Ejemplo n.º 1
0
 public static Value valuefromObject(Object object, TInstance type, QueryContext queryContext) {
   Value value = new Value(type);
   if (object == null) {
     value.putNull();
   } else if (object instanceof String) {
     // This is the common case, so let's test for it first
     if (TInstance.underlyingType(type) == UnderlyingType.STRING)
       value.putString((String) object, null);
     else if (type == null) {
       type =
           MString.VARCHAR.instance(
               ((String) object).length(),
               StringFactory.DEFAULT_CHARSET.ordinal(),
               StringFactory.NULL_COLLATION_ID,
               false);
       value = new Value(type, (String) object);
     }
   } else if (type == null) {
     value = fromObject(object);
   } else if (object instanceof BlobRef) {
     value.putObject(object);
   } else {
     switch (TInstance.underlyingType(type)) {
       case INT_8:
       case INT_16:
       case UINT_16:
       case INT_32:
       case INT_64:
         if (object instanceof Number) valueFromLong(((Number) object).longValue(), value);
         break;
       case FLOAT:
         if (object instanceof Number) value.putFloat(((Number) object).floatValue());
         break;
       case DOUBLE:
         if (object instanceof Number) value.putDouble(((Number) object).doubleValue());
         break;
       case BYTES:
         if (object instanceof byte[]) value.putBytes((byte[]) object);
         else if (object instanceof ByteSource)
           value.putBytes(((ByteSource) object).toByteSubarray());
         else if (object instanceof JTSSpatialObject) {
           if (AkBlob.isBlob(type.typeClass())) {
             byte[] content = Spatial.serializeWKB((JTSSpatialObject) object);
             value.putObject(new BlobRef(content));
           }
         }
         break;
       case STRING:
         if ((object instanceof JTSSpatialObject)) {
           value = new Value(type, Spatial.serializeWKT((JTSSpatialObject) object));
         } else {
           throw new IllegalArgumentException("Unsafe toString(): " + object.getClass());
         }
         break;
       case BOOL:
         if (object instanceof Boolean) value.putBool((Boolean) object);
         break;
     }
   }
   if (!value.hasAnyValue()) {
     if (type == null) {
       value = fromObject(object);
     } else {
       value = convertFromObject(object, type, queryContext);
     }
   }
   return value;
 }
Ejemplo n.º 2
0
  public static Object toObject(ValueSource source) {
    if (source == null || source.isNull()) {
      return null;
    }
    if (source.getType().typeClass() == MNumeric.DECIMAL
        || source.getType().typeClass() == MNumeric.DECIMAL_UNSIGNED) {
      if (source.getObject() instanceof BigDecimalWrapperImpl) {
        return ((BigDecimalWrapperImpl) source.getObject()).asBigDecimal();
      }
      logger.error("MDecimal with underlying object of : {}", source.getObject().getClass());
    }

    if (source.getType().typeClass() == AkGUID.INSTANCE.widestComparable()) {
      if (source.getObject() instanceof UUID) {
        return (UUID) source.getObject();
      }
      logger.error("GUID with underlying object of : {}", source.getObject().getClass());
    }

    if (AkBlob.isBlob(source.getType().typeClass())) {
      if (source.getObject() instanceof BlobRef) {
        return ((BlobRef) source.getObject()).getValue();
      }
      logger.error("Blob with underlying object of : {}", source.getObject().getClass());
    }

    if (source.getType().typeClass() == AkGeometry.INSTANCE.widestComparable()) {
      if (source.getObject() instanceof Geometry) {
        return (Geometry) source.getObject();
      }
      logger.error("Geometry with underlying object of : {}", source.getObject().getClass());
    }

    if (source.getType().typeClass() == MBinary.VARBINARY) {
      return new WrappingByteSource(source.getBytes());
    }

    UnderlyingType underlying = underlyingType(source);
    switch (underlying) {
      case BOOL:
        return source.getBoolean();
      case INT_8:
        return source.getInt8();
      case INT_16:
        return source.getInt16();
      case UINT_16:
        return source.getUInt16();
      case INT_32:
        return source.getInt32();
      case INT_64:
        return source.getInt64();
      case FLOAT:
        return source.getFloat();
      case DOUBLE:
        return source.getDouble();
      case BYTES:
        return source.getBytes();
      case STRING:
        return source.getString();
      default:
        throw new AssertionError(underlying);
    }
  }