public static ValueSource getNullSource(TInstance underlying) { Value result = new Value(underlying); result.putNull(); return result; }
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; }
/** * Creates a Value from the given object, deriving the type from the object. * * @throws java.lang.UnsupportedOperationException if the type cannot be inferred */ public static Value fromObject(Object object) { final TInstance type; Value value = null; if (object == null) { value = new Value(null); value.putNull(); } else if (object instanceof String) { String s = (String) object; type = MString.VARCHAR.instance( s.length(), StringFactory.DEFAULT_CHARSET.ordinal(), StringFactory.NULL_COLLATION_ID, false); value = new Value(type, s); } else if (object instanceof Long) { type = MNumeric.BIGINT.instance(false); value = new Value(type, (Long) object); } else if (object instanceof Integer) { type = MNumeric.INT.instance(false); value = new Value(type, (Integer) object); } else if (object instanceof Double) { type = MApproximateNumber.DOUBLE.instance(false); value = new Value(type, (Double) object); } else if (object instanceof Float) { type = MApproximateNumber.FLOAT.instance(false); value = new Value(type, (Float) object); } else if (object instanceof BigDecimalWrapper) { BigDecimalWrapper bdw = (BigDecimalWrapper) object; type = MNumeric.DECIMAL.instance(bdw.getPrecision(), bdw.getScale(), false); value = new Value(type); value.putObject(bdw); } else if (object instanceof BigDecimal) { BigDecimal bd = (BigDecimal) object; type = MNumeric.DECIMAL.instance( BigDecimalWrapperImpl.sqlPrecision(bd), BigDecimalWrapperImpl.sqlScale(bd), false); value = new Value(type); value.putObject(new BigDecimalWrapperImpl(bd)); } else if (object instanceof ByteSource || object instanceof byte[]) { byte[] bytes; if (object instanceof byte[]) { bytes = (byte[]) object; } else { ByteSource source = (ByteSource) object; byte[] srcArray = source.byteArray(); int offset = source.byteArrayOffset(); int end = offset + source.byteArrayLength(); bytes = Arrays.copyOfRange(srcArray, offset, end); } type = MBinary.VARBINARY.instance(bytes.length, false); value = new Value(type, bytes); } else if (object instanceof BigInteger) { type = MNumeric.BIGINT_UNSIGNED.instance(false); BigInteger bi = (BigInteger) object; value = new Value(type, bi.longValue()); } else if (object instanceof Boolean) { type = AkBool.INSTANCE.instance(false); value = new Value(type, (Boolean) object); } else if (object instanceof Character) { type = MString.VARCHAR.instance(1, false); value = new Value(type, object.toString()); } else if (object instanceof Short) { type = MNumeric.SMALLINT.instance(false); value = new Value(type, (Short) object); } else if (object instanceof Byte) { type = MNumeric.TINYINT.instance(false); value = new Value(type, (Byte) object); } else if (object instanceof UUID) { type = AkGUID.INSTANCE.instance(false); value = new Value(type); value.putObject(object); } else if (object instanceof BlobRef) { type = AkBlob.INSTANCE.instance(false); value = new Value(type); value.putObject(type); } else { throw new UnsupportedOperationException( "can't convert " + object + " of type " + object.getClass()); } return value; }