Search in sources :

Example 6 with EnumType

use of org.jooq.EnumType in project jOOQ by jOOQ.

the class DefaultBinding method get.

@SuppressWarnings("unchecked")
@Override
public void get(BindingGetStatementContext<U> ctx) throws SQLException {
    T result = null;
    if (type == Blob.class) {
        result = (T) ctx.statement().getBlob(ctx.index());
    } else if (type == Boolean.class) {
        result = (T) wasNull(ctx.statement(), Boolean.valueOf(ctx.statement().getBoolean(ctx.index())));
    } else if (type == BigInteger.class) {
        BigDecimal d = ctx.statement().getBigDecimal(ctx.index());
        result = (T) (d == null ? null : d.toBigInteger());
    } else if (type == BigDecimal.class) {
        result = (T) ctx.statement().getBigDecimal(ctx.index());
    } else if (type == Byte.class) {
        result = (T) wasNull(ctx.statement(), Byte.valueOf(ctx.statement().getByte(ctx.index())));
    } else if (type == byte[].class) {
        result = (T) ctx.statement().getBytes(ctx.index());
    } else if (type == Clob.class) {
        result = (T) ctx.statement().getClob(ctx.index());
    } else if (Tools.isDate(type)) {
        result = (T) ctx.statement().getDate(ctx.index());
        if (result != null && type == LocalDate.class)
            result = (T) ((Date) result).toLocalDate();
    } else if (type == Double.class) {
        result = (T) wasNull(ctx.statement(), Double.valueOf(ctx.statement().getDouble(ctx.index())));
    } else if (type == Float.class) {
        result = (T) wasNull(ctx.statement(), Float.valueOf(ctx.statement().getFloat(ctx.index())));
    } else if (type == Integer.class) {
        result = (T) wasNull(ctx.statement(), Integer.valueOf(ctx.statement().getInt(ctx.index())));
    } else if (type == Long.class) {
        result = (T) wasNull(ctx.statement(), Long.valueOf(ctx.statement().getLong(ctx.index())));
    } else if (type == Short.class) {
        result = (T) wasNull(ctx.statement(), Short.valueOf(ctx.statement().getShort(ctx.index())));
    } else if (type == String.class) {
        result = (T) ctx.statement().getString(ctx.index());
    } else if (Tools.isTime(type)) {
        result = (T) ctx.statement().getTime(ctx.index());
        if (result != null && type == LocalTime.class)
            result = (T) ((Time) result).toLocalTime();
    } else if (Tools.isTimestamp(type)) {
        result = (T) ctx.statement().getTimestamp(ctx.index());
        if (result != null && type == LocalDateTime.class)
            result = (T) ((Timestamp) result).toLocalDateTime();
    } else if (type == OffsetTime.class) {
        result = (T) offsetTime(ctx.statement().getString(ctx.index()));
    } else if (type == OffsetDateTime.class) {
        result = (T) offsetDateTime(ctx.statement().getString(ctx.index()));
    } else if (type == YearToMonth.class) {
        if (ctx.family() == POSTGRES) {
            Object object = ctx.statement().getObject(ctx.index());
            result = (T) (object == null ? null : PostgresUtils.toYearToMonth(object));
        } else {
            String string = ctx.statement().getString(ctx.index());
            result = (T) (string == null ? null : YearToMonth.valueOf(string));
        }
    } else if (type == DayToSecond.class) {
        if (ctx.family() == POSTGRES) {
            Object object = ctx.statement().getObject(ctx.index());
            result = (T) (object == null ? null : PostgresUtils.toDayToSecond(object));
        } else {
            String string = ctx.statement().getString(ctx.index());
            result = (T) (string == null ? null : DayToSecond.valueOf(string));
        }
    } else if (type == UByte.class) {
        String string = ctx.statement().getString(ctx.index());
        result = (T) (string == null ? null : UByte.valueOf(string));
    } else if (type == UShort.class) {
        String string = ctx.statement().getString(ctx.index());
        result = (T) (string == null ? null : UShort.valueOf(string));
    } else if (type == UInteger.class) {
        String string = ctx.statement().getString(ctx.index());
        result = (T) (string == null ? null : UInteger.valueOf(string));
    } else if (type == ULong.class) {
        String string = ctx.statement().getString(ctx.index());
        result = (T) (string == null ? null : ULong.valueOf(string));
    } else if (type == UUID.class) {
        switch(ctx.family()) {
            // java.util.UUID data type
            case H2:
            case POSTGRES:
                {
                    result = (T) ctx.statement().getObject(ctx.index());
                    break;
                }
            // emulates the type
            default:
                {
                    result = (T) Convert.convert(ctx.statement().getString(ctx.index()), UUID.class);
                    break;
                }
        }
    } else // The type byte[] is handled earlier. byte[][] can be handled here
    if (type.isArray()) {
        result = (T) convertArray(ctx.statement().getObject(ctx.index()), (Class<? extends Object[]>) type);
    } else if (EnumType.class.isAssignableFrom(type)) {
        result = (T) getEnumType((Class<EnumType>) type, ctx.statement().getString(ctx.index()));
    } else if (Record.class.isAssignableFrom(type)) {
        switch(ctx.family()) {
            case POSTGRES:
                result = (T) pgNewRecord(type, null, ctx.statement().getObject(ctx.index()));
                break;
            default:
                result = (T) ctx.statement().getObject(ctx.index(), typeMap(type, ctx.configuration()));
                break;
        }
    } else if (Result.class.isAssignableFrom(type)) {
        ResultSet nested = (ResultSet) ctx.statement().getObject(ctx.index());
        result = (T) DSL.using(ctx.configuration()).fetch(nested);
    } else {
        result = (T) ctx.statement().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));
}
Also used : ULong(org.jooq.types.ULong) LocalTime(java.time.LocalTime) DayToSecond(org.jooq.types.DayToSecond) UShort(org.jooq.types.UShort) PostgresUtils.toPGArrayString(org.jooq.util.postgres.PostgresUtils.toPGArrayString) Timestamp(java.sql.Timestamp) BigDecimal(java.math.BigDecimal) LocalDate(java.time.LocalDate) Date(java.sql.Date) OffsetDateTime(java.time.OffsetDateTime) EnumType(org.jooq.EnumType) ULong(org.jooq.types.ULong) ResultSet(java.sql.ResultSet) MockResultSet(org.jooq.tools.jdbc.MockResultSet) UDTRecord(org.jooq.UDTRecord) Record(org.jooq.Record) UUID(java.util.UUID) Attachable(org.jooq.Attachable)

Example 7 with EnumType

use of org.jooq.EnumType in project jOOQ by jOOQ.

the class ResultImpl 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()) {
        formatted += Arrays.toString((Object[]) value);
    } else if (value instanceof EnumType) {
        formatted += ((EnumType) value).getLiteral();
    } else if (value instanceof Record) {
        formatted += ((Record) value).valuesRow().toString();
    } else // [#5238] Oracle DATE is really a TIMESTAMP(0)...
    if (value instanceof Date) {
        String date = value.toString();
        if (Date.valueOf(date).equals(value))
            formatted += date;
        else
            formatted += new Timestamp(((Date) value).getTime());
    } else {
        formatted += value.toString();
    }
    return formatted;
}
Also used : EnumType(org.jooq.EnumType) TableRecord(org.jooq.TableRecord) UpdatableRecord(org.jooq.UpdatableRecord) Record(org.jooq.Record) Timestamp(java.sql.Timestamp) Date(java.sql.Date)

Aggregations

EnumType (org.jooq.EnumType)7 Date (java.sql.Date)6 LocalDate (java.time.LocalDate)5 BigDecimal (java.math.BigDecimal)4 Timestamp (java.sql.Timestamp)4 LocalTime (java.time.LocalTime)4 OffsetDateTime (java.time.OffsetDateTime)4 UShort (org.jooq.types.UShort)4 PostgresUtils.toPGArrayString (org.jooq.util.postgres.PostgresUtils.toPGArrayString)4 Time (java.sql.Time)3 LocalDateTime (java.time.LocalDateTime)3 OffsetTime (java.time.OffsetTime)3 Blob (java.sql.Blob)2 ResultSet (java.sql.ResultSet)2 UUID (java.util.UUID)2 Attachable (org.jooq.Attachable)2 Record (org.jooq.Record)2 UDTRecord (org.jooq.UDTRecord)2 MockResultSet (org.jooq.tools.jdbc.MockResultSet)2 DayToSecond (org.jooq.types.DayToSecond)2