use of org.jooq.EnumType in project jOOQ by jOOQ.
the class DefaultBinding method get.
@SuppressWarnings("unchecked")
@Override
public void get(BindingGetResultSetContext<U> ctx) throws SQLException {
T result = null;
if (type == Blob.class) {
result = (T) ctx.resultSet().getBlob(ctx.index());
} else if (type == Boolean.class) {
result = (T) wasNull(ctx.resultSet(), Boolean.valueOf(ctx.resultSet().getBoolean(ctx.index())));
} else if (type == BigInteger.class) {
// The SQLite JDBC driver doesn't support BigDecimals
if (ctx.configuration().dialect() == SQLDialect.SQLITE) {
result = Convert.convert(ctx.resultSet().getString(ctx.index()), (Class<T>) BigInteger.class);
} else {
BigDecimal b = ctx.resultSet().getBigDecimal(ctx.index());
result = (T) (b == null ? null : b.toBigInteger());
}
} else if (type == BigDecimal.class) {
// The SQLite JDBC driver doesn't support BigDecimals
if (ctx.configuration().dialect() == SQLDialect.SQLITE) {
result = Convert.convert(ctx.resultSet().getString(ctx.index()), (Class<T>) BigDecimal.class);
} else {
result = (T) ctx.resultSet().getBigDecimal(ctx.index());
}
} else if (type == Byte.class) {
result = (T) wasNull(ctx.resultSet(), Byte.valueOf(ctx.resultSet().getByte(ctx.index())));
} else if (type == byte[].class) {
result = (T) ctx.resultSet().getBytes(ctx.index());
} else if (type == Clob.class) {
result = (T) ctx.resultSet().getClob(ctx.index());
} else if (type == Date.class) {
result = (T) getDate(ctx.family(), ctx.resultSet(), ctx.index());
} else if (type == Double.class) {
result = (T) wasNull(ctx.resultSet(), Double.valueOf(ctx.resultSet().getDouble(ctx.index())));
} else if (type == Float.class) {
result = (T) wasNull(ctx.resultSet(), Float.valueOf(ctx.resultSet().getFloat(ctx.index())));
} else if (type == Integer.class) {
result = (T) wasNull(ctx.resultSet(), Integer.valueOf(ctx.resultSet().getInt(ctx.index())));
} else if (type == LocalDate.class) {
result = (T) localDate(getDate(ctx.family(), ctx.resultSet(), ctx.index()));
} else if (type == LocalTime.class) {
result = (T) localTime(getTime(ctx.family(), ctx.resultSet(), ctx.index()));
} else if (type == LocalDateTime.class) {
result = (T) localDateTime(getTimestamp(ctx.family(), ctx.resultSet(), ctx.index()));
} else if (type == Long.class) {
result = (T) wasNull(ctx.resultSet(), Long.valueOf(ctx.resultSet().getLong(ctx.index())));
} else if (type == OffsetTime.class) {
result = (T) offsetTime(ctx.resultSet().getString(ctx.index()));
} else if (type == OffsetDateTime.class) {
result = (T) offsetDateTime(ctx.resultSet().getString(ctx.index()));
} else if (type == Short.class) {
result = (T) wasNull(ctx.resultSet(), Short.valueOf(ctx.resultSet().getShort(ctx.index())));
} else if (type == String.class) {
result = (T) ctx.resultSet().getString(ctx.index());
} else if (type == Time.class) {
result = (T) getTime(ctx.family(), ctx.resultSet(), ctx.index());
} else if (type == Timestamp.class) {
result = (T) getTimestamp(ctx.family(), ctx.resultSet(), ctx.index());
} else if (type == YearToMonth.class) {
if (ctx.family() == POSTGRES) {
Object object = ctx.resultSet().getObject(ctx.index());
result = (T) (object == null ? null : PostgresUtils.toYearToMonth(object));
} else {
String string = ctx.resultSet().getString(ctx.index());
result = (T) (string == null ? null : YearToMonth.valueOf(string));
}
} else if (type == DayToSecond.class) {
if (ctx.family() == POSTGRES) {
Object object = ctx.resultSet().getObject(ctx.index());
result = (T) (object == null ? null : PostgresUtils.toDayToSecond(object));
} else {
String string = ctx.resultSet().getString(ctx.index());
result = (T) (string == null ? null : DayToSecond.valueOf(string));
}
} else if (type == UByte.class) {
result = (T) Convert.convert(ctx.resultSet().getString(ctx.index()), UByte.class);
} else if (type == UShort.class) {
result = (T) Convert.convert(ctx.resultSet().getString(ctx.index()), UShort.class);
} else if (type == UInteger.class) {
result = (T) Convert.convert(ctx.resultSet().getString(ctx.index()), UInteger.class);
} else if (type == ULong.class) {
result = (T) Convert.convert(ctx.resultSet().getString(ctx.index()), ULong.class);
} else if (type == UUID.class) {
switch(ctx.family()) {
// java.util.UUID data type
case H2:
case POSTGRES:
{
result = (T) ctx.resultSet().getObject(ctx.index());
break;
}
// emulates the type
default:
{
result = (T) Convert.convert(ctx.resultSet().getString(ctx.index()), UUID.class);
break;
}
}
} else // The type byte[] is handled earlier. byte[][] can be handled here
if (type.isArray()) {
switch(ctx.family()) {
case POSTGRES:
{
result = pgGetArray(ctx, ctx.resultSet(), type, ctx.index());
break;
}
default:
// Note: due to a HSQLDB bug, it is not recommended to call rs.getObject() here:
// See https://sourceforge.net/tracker/?func=detail&aid=3181365&group_id=23316&atid=378131
result = (T) convertArray(ctx.resultSet().getArray(ctx.index()), (Class<? extends Object[]>) type);
break;
}
} else if (EnumType.class.isAssignableFrom(type)) {
result = (T) getEnumType((Class<EnumType>) type, ctx.resultSet().getString(ctx.index()));
} else if (Record.class.isAssignableFrom(type)) {
switch(ctx.family()) {
case POSTGRES:
result = (T) pgNewRecord(type, null, ctx.resultSet().getObject(ctx.index()));
break;
default:
result = (T) ctx.resultSet().getObject(ctx.index(), typeMap(type, ctx.configuration()));
break;
}
} else if (Result.class.isAssignableFrom(type)) {
ResultSet nested = (ResultSet) ctx.resultSet().getObject(ctx.index());
result = (T) DSL.using(ctx.configuration()).fetch(nested);
} else {
result = (T) unlob(ctx.resultSet().getObject(ctx.index()));
}
// [#4372] Attach records if possible / required
if (result instanceof Attachable && attachRecords(ctx.configuration()))
((Attachable) result).attach(ctx.configuration());
ctx.value(converter.from(result));
}
use of org.jooq.EnumType in project jOOQ by jOOQ.
the class DefaultBinding method pgRenderEnumCast.
private static final void pgRenderEnumCast(RenderContext render, Class<?> type) {
@SuppressWarnings("unchecked") Class<? extends EnumType> enumType = (Class<? extends EnumType>) (type.isArray() ? type.getComponentType() : type);
// [#968] Don't cast "synthetic" enum types (note, val can be null!)
// [#4427] Java Enum agnostic implementation will work for Scala also
EnumType[] enums = Tools.enums(enumType);
if (enums == null || enums.length == 0)
throw new IllegalArgumentException("Not a valid EnumType : " + type);
Schema schema = enums[0].getSchema();
if (schema != null) {
render.sql("::");
schema = using(render.configuration()).map(schema);
if (schema != null && TRUE.equals(render.configuration().settings().isRenderSchema())) {
render.visit(schema);
render.sql('.');
}
render.visit(name(enums[0].getName()));
}
if (type.isArray())
render.sql("[]");
}
use of org.jooq.EnumType in project jOOQ by jOOQ.
the class DefaultBinding method set.
@Override
public void set(BindingSetSQLOutputContext<U> ctx) throws SQLException {
Configuration configuration = ctx.configuration();
T value = converter.to(ctx.value());
if (value == null) {
ctx.output().writeObject(null);
} else if (type == Blob.class) {
ctx.output().writeBlob((Blob) value);
} else if (type == Boolean.class) {
ctx.output().writeBoolean((Boolean) value);
} else if (type == BigInteger.class) {
ctx.output().writeBigDecimal(new BigDecimal((BigInteger) value));
} else if (type == BigDecimal.class) {
ctx.output().writeBigDecimal((BigDecimal) value);
} else if (type == Byte.class) {
ctx.output().writeByte((Byte) value);
} else if (type == byte[].class) {
// Use reflection to avoid dependency on OJDBC
if (isLob) {
Blob blob = null;
try {
blob = on("oracle.sql.BLOB").call("createTemporary", on(ctx.output()).call("getSTRUCT").call("getJavaSqlConnection").get(), false, on("oracle.sql.BLOB").get("DURATION_SESSION")).get();
blob.setBytes(1, (byte[]) value);
ctx.output().writeBlob(blob);
} finally {
DefaultExecuteContext.register(blob);
}
} else {
ctx.output().writeBytes((byte[]) value);
}
} else if (type == Clob.class) {
ctx.output().writeClob((Clob) value);
} else if (type == Date.class) {
Date date = (Date) value;
ctx.output().writeDate(date);
} else if (type == Double.class) {
ctx.output().writeDouble((Double) value);
} else if (type == Float.class) {
ctx.output().writeFloat((Float) value);
} else if (type == Integer.class) {
ctx.output().writeInt((Integer) value);
} else if (type == Long.class) {
ctx.output().writeLong((Long) value);
} else if (type == Short.class) {
ctx.output().writeShort((Short) value);
} else if (type == String.class) {
// Use reflection to avoid dependency on OJDBC
if (isLob) {
Clob clob = null;
try {
clob = on("oracle.sql.CLOB").call("createTemporary", on(ctx.output()).call("getSTRUCT").call("getJavaSqlConnection").get(), false, on("oracle.sql.CLOB").get("DURATION_SESSION")).get();
clob.setString(1, (String) value);
ctx.output().writeClob(clob);
} finally {
DefaultExecuteContext.register(clob);
}
} else {
ctx.output().writeString((String) value);
}
} else if (type == Time.class) {
ctx.output().writeTime((Time) value);
} else if (type == Timestamp.class) {
ctx.output().writeTimestamp((Timestamp) value);
} else if (type == YearToMonth.class) {
ctx.output().writeString(value.toString());
} else if (type == DayToSecond.class) {
ctx.output().writeString(value.toString());
} else // }
if (UNumber.class.isAssignableFrom(type)) {
ctx.output().writeString(value.toString());
} else if (type == UUID.class) {
ctx.output().writeString(value.toString());
} else if (EnumType.class.isAssignableFrom(type)) {
ctx.output().writeString(((EnumType) value).getLiteral());
} else if (UDTRecord.class.isAssignableFrom(type)) {
ctx.output().writeObject((UDTRecord<?>) value);
} else {
throw new UnsupportedOperationException("Type " + type + " is not supported");
}
}
use of org.jooq.EnumType in project jOOQ by jOOQ.
the class AbstractResult method format0.
/**
* @param value The value to be formatted
* @param visual Whether the formatted output is to be consumed visually
* (HTML, TEXT) or by a machine (CSV, JSON, XML)
*/
private static final String format0(Object value, boolean changed, boolean visual) {
// [#2741] TODO: This logic will be externalised in new SPI
String formatted = changed && visual ? "*" : "";
if (value == null) {
formatted += visual ? "{null}" : null;
} else if (value.getClass() == byte[].class) {
formatted += DatatypeConverter.printBase64Binary((byte[]) value);
} else if (value.getClass().isArray()) {
// [#6545] Nested arrays are handled recursively
formatted += Arrays.stream((Object[]) value).map(f -> format0(f, false, visual)).collect(joining(", ", "[", "]"));
} else if (value instanceof EnumType) {
EnumType e = (EnumType) value;
formatted += e.getLiteral();
} else if (value instanceof List) {
List<?> l = (List<?>) value;
formatted += l.stream().map(f -> format0(f, false, visual)).collect(joining(", ", "[", "]"));
} else if (value instanceof Record) {
Record r = (Record) value;
formatted += Arrays.stream(r.intoArray()).map(f -> format0(f, false, visual)).collect(joining(", ", "(", ")"));
} else // [#6080] Support formatting of nested ROWs
if (value instanceof Param) {
formatted += format0(((Param<?>) value).getValue(), false, visual);
} else // [#5238] Oracle DATE is really a TIMESTAMP(0)...
if (value instanceof Date) {
Date d = (Date) value;
String date = value.toString();
if (Date.valueOf(date).equals(value))
formatted += date;
else
formatted += new Timestamp(d.getTime());
} else {
formatted += value.toString();
}
return formatted;
}
use of org.jooq.EnumType in project jOOQ by jOOQ.
the class DefaultBinding method get.
@SuppressWarnings("unchecked")
@Override
public void get(BindingGetSQLInputContext<U> ctx) throws SQLException {
T result = null;
if (type == Blob.class) {
result = (T) ctx.input().readBlob();
} else if (type == Boolean.class) {
result = (T) wasNull(ctx.input(), Boolean.valueOf(ctx.input().readBoolean()));
} else if (type == BigInteger.class) {
BigDecimal d = ctx.input().readBigDecimal();
result = (T) (d == null ? null : d.toBigInteger());
} else if (type == BigDecimal.class) {
result = (T) ctx.input().readBigDecimal();
} else if (type == Byte.class) {
result = (T) wasNull(ctx.input(), Byte.valueOf(ctx.input().readByte()));
} else if (type == byte[].class) {
// [#1327] Oracle cannot deserialise BLOBs as byte[] from SQLInput
if (isLob) {
Blob blob = null;
try {
blob = ctx.input().readBlob();
result = (T) (blob == null ? null : blob.getBytes(1, (int) blob.length()));
} finally {
safeFree(blob);
}
} else {
result = (T) ctx.input().readBytes();
}
} else if (type == Clob.class) {
result = (T) ctx.input().readClob();
} else if (type == Date.class) {
result = (T) ctx.input().readDate();
} else if (type == Double.class) {
result = (T) wasNull(ctx.input(), Double.valueOf(ctx.input().readDouble()));
} else if (type == Float.class) {
result = (T) wasNull(ctx.input(), Float.valueOf(ctx.input().readFloat()));
} else if (type == Integer.class) {
result = (T) wasNull(ctx.input(), Integer.valueOf(ctx.input().readInt()));
} else if (type == Long.class) {
result = (T) wasNull(ctx.input(), Long.valueOf(ctx.input().readLong()));
} else if (type == Short.class) {
result = (T) wasNull(ctx.input(), Short.valueOf(ctx.input().readShort()));
} else if (type == String.class) {
result = (T) ctx.input().readString();
} else if (type == Time.class) {
result = (T) ctx.input().readTime();
} else if (type == Timestamp.class) {
result = (T) ctx.input().readTimestamp();
} else if (type == YearToMonth.class) {
String string = ctx.input().readString();
result = (T) (string == null ? null : YearToMonth.valueOf(string));
} else if (type == DayToSecond.class) {
String string = ctx.input().readString();
result = (T) (string == null ? null : DayToSecond.valueOf(string));
} else if (type == UByte.class) {
String string = ctx.input().readString();
result = (T) (string == null ? null : UByte.valueOf(string));
} else if (type == UShort.class) {
String string = ctx.input().readString();
result = (T) (string == null ? null : UShort.valueOf(string));
} else if (type == UInteger.class) {
String string = ctx.input().readString();
result = (T) (string == null ? null : UInteger.valueOf(string));
} else if (type == ULong.class) {
String string = ctx.input().readString();
result = (T) (string == null ? null : ULong.valueOf(string));
} else if (type == UUID.class) {
result = (T) Convert.convert(ctx.input().readString(), UUID.class);
} else // The type byte[] is handled earlier. byte[][] can be handled here
if (type.isArray()) {
Array array = ctx.input().readArray();
result = (T) (array == null ? null : array.getArray());
} else if (EnumType.class.isAssignableFrom(type)) {
result = (T) getEnumType((Class<EnumType>) type, ctx.input().readString());
} else if (UDTRecord.class.isAssignableFrom(type)) {
result = (T) ctx.input().readObject();
} else {
result = (T) unlob(ctx.input().readObject());
}
ctx.value(converter.from(result));
}
Aggregations