Search in sources :

Example 1 with EnumType

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));
}
Also used : LocalDateTime(java.time.LocalDateTime) Time(java.sql.Time) LocalTime(java.time.LocalTime) OffsetTime(java.time.OffsetTime) OffsetDateTime(java.time.OffsetDateTime) LocalDateTime(java.time.LocalDateTime) PostgresUtils.toPGArrayString(org.jooq.util.postgres.PostgresUtils.toPGArrayString) LocalDate(java.time.LocalDate) BigDecimal(java.math.BigDecimal) LocalDate(java.time.LocalDate) Date(java.sql.Date) Result(org.jooq.Result) UByte(org.jooq.types.UByte) OffsetTime(java.time.OffsetTime) EnumType(org.jooq.EnumType) UInteger(org.jooq.types.UInteger) ResultSet(java.sql.ResultSet) MockResultSet(org.jooq.tools.jdbc.MockResultSet) UUID(java.util.UUID) Attachable(org.jooq.Attachable) UShort(org.jooq.types.UShort) YearToMonth(org.jooq.types.YearToMonth)

Example 2 with EnumType

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("[]");
}
Also used : EnumType(org.jooq.EnumType) Schema(org.jooq.Schema)

Example 3 with EnumType

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");
    }
}
Also used : Blob(java.sql.Blob) Configuration(org.jooq.Configuration) Time(java.sql.Time) LocalTime(java.time.LocalTime) OffsetTime(java.time.OffsetTime) OffsetDateTime(java.time.OffsetDateTime) LocalDateTime(java.time.LocalDateTime) BigDecimal(java.math.BigDecimal) LocalDate(java.time.LocalDate) Date(java.sql.Date) UInteger(org.jooq.types.UInteger) BigInteger(java.math.BigInteger) UNumber(org.jooq.types.UNumber) EnumType(org.jooq.EnumType) UByte(org.jooq.types.UByte) BigInteger(java.math.BigInteger) Clob(java.sql.Clob) UShort(org.jooq.types.UShort) YearToMonth(org.jooq.types.YearToMonth)

Example 4 with EnumType

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;
}
Also used : Arrays(java.util.Arrays) Row(org.jooq.Row) Display(org.jooq.ChartFormat.Display) Table(org.jooq.Table) DatatypeConverter(jakarta.xml.bind.DatatypeConverter) VALUE_ELEMENTS_WITH_FIELD_ATTRIBUTE(org.jooq.XMLFormat.RecordFormat.VALUE_ELEMENTS_WITH_FIELD_ATTRIBUTE) IOException(org.jooq.exception.IOException) Document(org.w3c.dom.Document) StringUtils.leftPad(org.jooq.tools.StringUtils.leftPad) DSLContext(org.jooq.DSLContext) AttributesImpl(org.xml.sax.helpers.AttributesImpl) StringUtils.rightPad(org.jooq.tools.StringUtils.rightPad) DSL.name(org.jooq.impl.DSL.name) Timestamp(java.sql.Timestamp) SettingsTools.renderLocale(org.jooq.conf.SettingsTools.renderLocale) Constants(org.jooq.Constants) Field(org.jooq.Field) TableRecord(org.jooq.TableRecord) Math.min(java.lang.Math.min) ChartFormat(org.jooq.ChartFormat) Result(org.jooq.Result) Cursor(org.jooq.Cursor) Collectors.joining(java.util.stream.Collectors.joining) DocumentFragment(org.w3c.dom.DocumentFragment) List(java.util.List) TableField(org.jooq.TableField) COLUMN_NAME_ELEMENTS(org.jooq.XMLFormat.RecordFormat.COLUMN_NAME_ELEMENTS) SAXException(org.xml.sax.SAXException) Writer(java.io.Writer) Math.max(java.lang.Math.max) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) JSONValue(org.jooq.tools.json.JSONValue) XML(org.jooq.XML) EnumType(org.jooq.EnumType) Deque(java.util.Deque) FormattingProvider(org.jooq.FormattingProvider) ArrayList(java.util.ArrayList) JSONFormat(org.jooq.JSONFormat) DSL.insertInto(org.jooq.impl.DSL.insertInto) Schema(org.jooq.Schema) Attributes(org.xml.sax.Attributes) CSVFormat(org.jooq.CSVFormat) ContentHandler(org.xml.sax.ContentHandler) Record(org.jooq.Record) InputSource(org.xml.sax.InputSource) XMLFormat(org.jooq.XMLFormat) NodeList(org.w3c.dom.NodeList) Iterator(java.util.Iterator) JSON(org.jooq.JSON) TXTFormat(org.jooq.TXTFormat) Formattable(org.jooq.Formattable) StringUtils(org.jooq.tools.StringUtils) Date(java.sql.Date) Param(org.jooq.Param) DefaultHandler(org.xml.sax.helpers.DefaultHandler) Configuration(org.jooq.Configuration) StringUtils.abbreviate(org.jooq.tools.StringUtils.abbreviate) Element(org.w3c.dom.Element) StringReader(java.io.StringReader) TreeMap(java.util.TreeMap) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) DocumentBuilder(javax.xml.parsers.DocumentBuilder) JSONB(org.jooq.JSONB) ArrayDeque(java.util.ArrayDeque) DSL.table(org.jooq.impl.DSL.table) Collections(java.util.Collections) EnumType(org.jooq.EnumType) Param(org.jooq.Param) List(java.util.List) ArrayList(java.util.ArrayList) NodeList(org.w3c.dom.NodeList) TableRecord(org.jooq.TableRecord) Record(org.jooq.Record) Timestamp(java.sql.Timestamp) Date(java.sql.Date)

Example 5 with EnumType

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));
}
Also used : Blob(java.sql.Blob) ULong(org.jooq.types.ULong) DayToSecond(org.jooq.types.DayToSecond) UShort(org.jooq.types.UShort) UDTRecord(org.jooq.UDTRecord) PostgresUtils.toPGArrayString(org.jooq.util.postgres.PostgresUtils.toPGArrayString) Timestamp(java.sql.Timestamp) BigDecimal(java.math.BigDecimal) LocalDate(java.time.LocalDate) Date(java.sql.Date) Array(java.sql.Array) MockArray(org.jooq.tools.jdbc.MockArray) EnumType(org.jooq.EnumType) ULong(org.jooq.types.ULong)

Aggregations

EnumType (org.jooq.EnumType)8 Date (java.sql.Date)7 Timestamp (java.sql.Timestamp)5 LocalDate (java.time.LocalDate)5 BigDecimal (java.math.BigDecimal)4 LocalTime (java.time.LocalTime)4 OffsetDateTime (java.time.OffsetDateTime)4 UShort (org.jooq.types.UShort)4 Time (java.sql.Time)3 LocalDateTime (java.time.LocalDateTime)3 OffsetTime (java.time.OffsetTime)3 PostgresUtils.toPGArrayString (org.jooq.util.postgres.PostgresUtils.toPGArrayString)3 Blob (java.sql.Blob)2 ResultSet (java.sql.ResultSet)2 UUID (java.util.UUID)2 Attachable (org.jooq.Attachable)2 Configuration (org.jooq.Configuration)2 Record (org.jooq.Record)2 Result (org.jooq.Result)2 Schema (org.jooq.Schema)2