com.github.pgasync.impl.conversion.DataConverter Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of postgres-async-driver Show documentation
Show all versions of postgres-async-driver Show documentation
Asynchronous PostgreSQL Java driver
package com.github.pgasync.impl.conversion;
import com.github.pgasync.Converter;
import com.github.pgasync.impl.Oid;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.sql.Date;
import java.sql.Time;
import java.sql.Timestamp;
import java.util.*;
import static java.nio.charset.StandardCharsets.UTF_8;
/**
* @author Antti Laisi
*/
public class DataConverter {
final Map, Converter>> typeToConverter = new HashMap<>();
public DataConverter(List> converters) {
converters.forEach(c -> typeToConverter.put(c.type(), c));
}
public DataConverter() {
this(Collections.emptyList());
}
public String toString(Oid oid, byte[] value) {
return value == null ? null : StringConversions.toString(oid, value);
}
public Character toChar(Oid oid, byte[] value) {
return value == null ? null : StringConversions.toChar(oid, value);
}
public Long toLong(Oid oid, byte[] value) {
return value == null ? null : NumericConversions.toLong(oid, value);
}
public Integer toInteger(Oid oid, byte[] value) {
return value == null ? null : NumericConversions.toInteger(oid, value);
}
public Short toShort(Oid oid, byte[] value) {
return value == null ? null : NumericConversions.toShort(oid, value);
}
public Byte toByte(Oid oid, byte[] value) {
return value == null ? null : NumericConversions.toByte(oid, value);
}
public BigInteger toBigInteger(Oid oid, byte[] value) {
return value == null ? null : NumericConversions.toBigInteger(oid, value);
}
public BigDecimal toBigDecimal(Oid oid, byte[] value) {
return value == null ? null : NumericConversions.toBigDecimal(oid, value);
}
public Double toDouble(Oid oid, byte[] value) {
return value == null ? null : NumericConversions.toDouble(oid, value);
}
public Date toDate(Oid oid, byte[] value) {
return value == null ? null : TemporalConversions.toDate(oid, value);
}
public Time toTime(Oid oid, byte[] value) {
return value == null ? null : TemporalConversions.toTime(oid, value);
}
public Timestamp toTimestamp(Oid oid, byte[] value) {
return value == null ? null : TemporalConversions.toTimestamp(oid, value);
}
public byte[] toBytes(Oid oid, byte[] value) {
return value == null ? null : BlobConversions.toBytes(oid, value);
}
public Boolean toBoolean(Oid oid, byte[] value) {
return value == null ? null : BooleanConversions.toBoolean(oid, value);
}
public TArray toArray(Class arrayType, Oid oid, byte[] value) {
switch(oid) {
case INT2_ARRAY:
return ArrayConversions.toArray(arrayType, oid, value, NumericConversions::toShort);
case INT4_ARRAY:
return ArrayConversions.toArray(arrayType, oid, value, NumericConversions::toInteger);
case INT8_ARRAY:
return ArrayConversions.toArray(arrayType, oid, value, NumericConversions::toLong);
case TEXT_ARRAY:
case CHAR_ARRAY:
case BPCHAR_ARRAY:
case VARCHAR_ARRAY:
return ArrayConversions.toArray(arrayType, oid, value, StringConversions::toString);
case FLOAT4_ARRAY:
case FLOAT8_ARRAY:
return ArrayConversions.toArray(arrayType, oid, value, NumericConversions::toBigDecimal);
case TIMESTAMP_ARRAY:
case TIMESTAMPTZ_ARRAY:
return ArrayConversions.toArray(arrayType, oid, value, TemporalConversions::toTimestamp);
case TIMETZ_ARRAY:
case TIME_ARRAY:
return ArrayConversions.toArray(arrayType, oid, value, TemporalConversions::toTime);
case DATE_ARRAY:
return ArrayConversions.toArray(arrayType, oid, value, TemporalConversions::toDate);
case BOOL_ARRAY:
return ArrayConversions.toArray(arrayType, oid, value, BooleanConversions::toBoolean);
default:
throw new IllegalStateException("Unsupported array type: " + oid);
}
}
@SuppressWarnings("unchecked")
public T toObject(Class type, Oid oid, byte[] value) {
Converter converter = typeToConverter.get(type);
if(converter == null) {
throw new IllegalArgumentException("Unknown conversion target: " + value.getClass());
}
return (T) converter.to(oid, value);
}
public byte[] fromObject(Object o) {
if (o == null) {
return null;
}
if (o instanceof Time) {
return TemporalConversions.fromTime((Time) o);
}
if (o instanceof Timestamp) {
return TemporalConversions.fromTimestamp((Timestamp) o);
}
if (o instanceof Date) {
return TemporalConversions.fromDate((Date) o);
}
if (o instanceof byte[]) {
return BlobConversions.fromBytes((byte[]) o);
}
if(o instanceof Boolean) {
return BooleanConversions.fromBoolean((boolean) o);
}
if (o.getClass().isArray()) {
return ArrayConversions.fromArray(o, this::fromObject);
}
if(o instanceof String || o instanceof Number || o instanceof Character || o instanceof UUID) {
return o.toString().getBytes(UTF_8);
}
return fromConvertable(o);
}
@SuppressWarnings("unchecked")
protected byte[] fromConvertable(Object value) {
Converter converter = typeToConverter.get(value.getClass());
if(converter == null) {
throw new IllegalArgumentException("Unknown conversion target: " + value.getClass());
}
return converter.from(value);
}
public byte[][] fromParameters(List
© 2015 - 2024 Weber Informatics LLC | Privacy Policy