Search in sources :

Example 51 with OffsetTime

use of java.time.OffsetTime in project jOOQ by jOOQ.

the class DefaultBinding method set.

@SuppressWarnings("unchecked")
@Override
public void set(BindingSetStatementContext<U> ctx) throws SQLException {
    Configuration configuration = ctx.configuration();
    SQLDialect dialect = ctx.dialect();
    T value = converter.to(ctx.value());
    if (log.isTraceEnabled())
        if (value != null && value.getClass().isArray() && value.getClass() != byte[].class)
            log.trace("Binding variable " + ctx.index(), Arrays.asList((Object[]) value) + " (" + type + ")");
        else
            log.trace("Binding variable " + ctx.index(), value + " (" + type + ")");
    // SQL dialect. See the following section for details
    if (value == null) {
        int sqlType = DefaultDataType.getDataType(dialect, type).getSQLType(configuration);
        // [#1126] Oracle's UDTs need to be bound with their type name
        if (UDTRecord.class.isAssignableFrom(type)) {
            ctx.statement().setNull(ctx.index(), sqlType, Tools.getMappedUDTName(configuration, (Class<UDTRecord<?>>) type));
        } else // Some dialects have trouble binding binary data as BLOB
        if (asList(POSTGRES).contains(configuration.family()) && sqlType == Types.BLOB) {
            ctx.statement().setNull(ctx.index(), Types.BINARY);
        } else // All other types can be set to null if the JDBC type is known
        if (sqlType != Types.OTHER) {
            ctx.statement().setNull(ctx.index(), sqlType);
        } else // [#729] In the absence of the correct JDBC type, try setObject
        {
            ctx.statement().setObject(ctx.index(), null);
        }
    } else {
        Class<?> actualType = type;
        // Try to infer the bind value type from the actual bind value if possible.
        if (actualType == Object.class) {
            actualType = value.getClass();
        }
        if (actualType == Blob.class) {
            ctx.statement().setBlob(ctx.index(), (Blob) value);
        } else if (actualType == Boolean.class) {
            ctx.statement().setBoolean(ctx.index(), (Boolean) value);
        } else if (actualType == BigDecimal.class) {
            if (asList(SQLITE).contains(dialect.family())) {
                ctx.statement().setString(ctx.index(), value.toString());
            } else {
                ctx.statement().setBigDecimal(ctx.index(), (BigDecimal) value);
            }
        } else if (actualType == BigInteger.class) {
            if (asList(SQLITE).contains(dialect.family())) {
                ctx.statement().setString(ctx.index(), value.toString());
            } else {
                ctx.statement().setBigDecimal(ctx.index(), new BigDecimal((BigInteger) value));
            }
        } else if (actualType == Byte.class) {
            ctx.statement().setByte(ctx.index(), (Byte) value);
        } else if (actualType == byte[].class) {
            ctx.statement().setBytes(ctx.index(), (byte[]) value);
        } else if (actualType == Clob.class) {
            ctx.statement().setClob(ctx.index(), (Clob) value);
        } else if (actualType == Double.class) {
            ctx.statement().setDouble(ctx.index(), (Double) value);
        } else if (actualType == Float.class) {
            ctx.statement().setFloat(ctx.index(), (Float) value);
        } else if (actualType == Integer.class) {
            ctx.statement().setInt(ctx.index(), (Integer) value);
        } else if (actualType == Long.class) {
            ctx.statement().setLong(ctx.index(), (Long) value);
        } else if (actualType == Short.class) {
            ctx.statement().setShort(ctx.index(), (Short) value);
        } else if (actualType == String.class) {
            ctx.statement().setString(ctx.index(), (String) value);
        } else // -------------------------------------------------------------
        if (Tools.isDate(actualType)) {
            Date date = getDate(actualType, value);
            if (dialect == SQLITE) {
                ctx.statement().setString(ctx.index(), date.toString());
            } else {
                ctx.statement().setDate(ctx.index(), date);
            }
        } else if (Tools.isTime(actualType)) {
            Time time = getTime(actualType, value);
            if (dialect == SQLITE) {
                ctx.statement().setString(ctx.index(), time.toString());
            } else {
                ctx.statement().setTime(ctx.index(), time);
            }
        } else if (Tools.isTimestamp(actualType)) {
            Timestamp timestamp = getTimestamp(actualType, value);
            if (dialect == SQLITE) {
                ctx.statement().setString(ctx.index(), timestamp.toString());
            } else {
                ctx.statement().setTimestamp(ctx.index(), timestamp);
            }
        } else if (actualType == OffsetTime.class) {
            String string = format((OffsetTime) value);
            ctx.statement().setString(ctx.index(), string);
        } else if (actualType == OffsetDateTime.class) {
            ctx.statement().setString(ctx.index(), format((OffsetDateTime) value));
        } else // [#566] Interval data types are best bound as Strings
        if (actualType == YearToMonth.class) {
            if (dialect.family() == POSTGRES) {
                ctx.statement().setObject(ctx.index(), toPGInterval((YearToMonth) value));
            } else {
                ctx.statement().setString(ctx.index(), value.toString());
            }
        } else if (actualType == DayToSecond.class) {
            if (dialect.family() == POSTGRES) {
                ctx.statement().setObject(ctx.index(), toPGInterval((DayToSecond) value));
            } else {
                ctx.statement().setString(ctx.index(), value.toString());
            }
        } else if (actualType == UByte.class) {
            ctx.statement().setShort(ctx.index(), ((UByte) value).shortValue());
        } else if (actualType == UShort.class) {
            ctx.statement().setInt(ctx.index(), ((UShort) value).intValue());
        } else if (actualType == UInteger.class) {
            ctx.statement().setLong(ctx.index(), ((UInteger) value).longValue());
        } else if (actualType == ULong.class) {
            ctx.statement().setBigDecimal(ctx.index(), new BigDecimal(value.toString()));
        } else if (actualType == UUID.class) {
            switch(dialect.family()) {
                // java.util.UUID data type
                case H2:
                case POSTGRES:
                    {
                        ctx.statement().setObject(ctx.index(), value);
                        break;
                    }
                // emulates the type
                default:
                    {
                        ctx.statement().setString(ctx.index(), value.toString());
                        break;
                    }
            }
        } else // The type byte[] is handled earlier. byte[][] can be handled here
        if (actualType.isArray()) {
            switch(dialect.family()) {
                case POSTGRES:
                    {
                        ctx.statement().setString(ctx.index(), toPGArrayString((Object[]) value));
                        break;
                    }
                case HSQLDB:
                    {
                        Object[] a = (Object[]) value;
                        Class<?> t = actualType;
                        // See also: https://sourceforge.net/p/hsqldb/bugs/1466
                        if (actualType == UUID[].class) {
                            a = Convert.convertArray(a, byte[][].class);
                            t = byte[][].class;
                        }
                        ctx.statement().setArray(ctx.index(), new MockArray(dialect, a, t));
                        break;
                    }
                case H2:
                    {
                        ctx.statement().setObject(ctx.index(), value);
                        break;
                    }
                default:
                    throw new SQLDialectNotSupportedException("Cannot bind ARRAY types in dialect " + dialect);
            }
        } else if (EnumType.class.isAssignableFrom(actualType)) {
            ctx.statement().setString(ctx.index(), ((EnumType) value).getLiteral());
        } else {
            ctx.statement().setObject(ctx.index(), value);
        }
    }
}
Also used : Configuration(org.jooq.Configuration) 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) Timestamp(java.sql.Timestamp) UUID(java.util.UUID) UShort(org.jooq.types.UShort) SQLDialectNotSupportedException(org.jooq.exception.SQLDialectNotSupportedException) ULong(org.jooq.types.ULong) DayToSecond(org.jooq.types.DayToSecond) UShort(org.jooq.types.UShort) BigDecimal(java.math.BigDecimal) LocalDate(java.time.LocalDate) Date(java.sql.Date) MockArray(org.jooq.tools.jdbc.MockArray) UInteger(org.jooq.types.UInteger) BigInteger(java.math.BigInteger) OffsetDateTime(java.time.OffsetDateTime) SQLDialect(org.jooq.SQLDialect) BigInteger(java.math.BigInteger) YearToMonth(org.jooq.types.YearToMonth)

Example 52 with OffsetTime

use of java.time.OffsetTime in project fastjson by alibaba.

the class Jdk8DateCodec method deserialze.

@SuppressWarnings("unchecked")
public <T> T deserialze(DefaultJSONParser parser, Type type, Object fieldName, String format, int feature) {
    JSONLexer lexer = parser.lexer;
    if (lexer.token() == JSONToken.NULL) {
        lexer.nextToken();
        return null;
    }
    if (lexer.token() == JSONToken.LITERAL_STRING) {
        String text = lexer.stringVal();
        lexer.nextToken();
        DateTimeFormatter formatter = null;
        if (format != null) {
            if (defaultPatttern.equals(format)) {
                formatter = defaultFormatter;
            } else {
                formatter = DateTimeFormatter.ofPattern(format);
            }
        }
        if ("".equals(text)) {
            return null;
        }
        if (type == LocalDateTime.class) {
            LocalDateTime localDateTime;
            if (text.length() == 10 || text.length() == 8) {
                LocalDate localDate = parseLocalDate(text, format, formatter);
                localDateTime = LocalDateTime.of(localDate, LocalTime.MIN);
            } else {
                localDateTime = parseDateTime(text, formatter);
            }
            return (T) localDateTime;
        } else if (type == LocalDate.class) {
            LocalDate localDate;
            if (text.length() == 23) {
                LocalDateTime localDateTime = LocalDateTime.parse(text);
                localDate = LocalDate.of(localDateTime.getYear(), localDateTime.getMonthValue(), localDateTime.getDayOfMonth());
            } else {
                localDate = parseLocalDate(text, format, formatter);
            }
            return (T) localDate;
        } else if (type == LocalTime.class) {
            LocalTime localDate;
            if (text.length() == 23) {
                LocalDateTime localDateTime = LocalDateTime.parse(text);
                localDate = LocalTime.of(localDateTime.getHour(), localDateTime.getMinute(), localDateTime.getSecond(), localDateTime.getNano());
            } else {
                localDate = LocalTime.parse(text);
            }
            return (T) localDate;
        } else if (type == ZonedDateTime.class) {
            if (formatter == defaultFormatter) {
                formatter = ISO_FIXED_FORMAT;
            }
            ZonedDateTime zonedDateTime = parseZonedDateTime(text, formatter);
            return (T) zonedDateTime;
        } else if (type == OffsetDateTime.class) {
            OffsetDateTime offsetDateTime = OffsetDateTime.parse(text);
            return (T) offsetDateTime;
        } else if (type == OffsetTime.class) {
            OffsetTime offsetTime = OffsetTime.parse(text);
            return (T) offsetTime;
        } else if (type == ZoneId.class) {
            ZoneId offsetTime = ZoneId.of(text);
            return (T) offsetTime;
        } else if (type == Period.class) {
            Period period = Period.parse(text);
            return (T) period;
        } else if (type == Duration.class) {
            Duration duration = Duration.parse(text);
            return (T) duration;
        } else if (type == Instant.class) {
            Instant instant = Instant.parse(text);
            return (T) instant;
        }
    } else {
        throw new UnsupportedOperationException();
    }
    return null;
}
Also used : LocalDateTime(java.time.LocalDateTime) LocalTime(java.time.LocalTime) ZoneId(java.time.ZoneId) Instant(java.time.Instant) Period(java.time.Period) JSONLexer(com.alibaba.fastjson.parser.JSONLexer) Duration(java.time.Duration) LocalDate(java.time.LocalDate) ZonedDateTime(java.time.ZonedDateTime) OffsetDateTime(java.time.OffsetDateTime) OffsetTime(java.time.OffsetTime) DateTimeFormatter(java.time.format.DateTimeFormatter)

Example 53 with OffsetTime

use of java.time.OffsetTime in project querydsl by querydsl.

the class JSR310OffsetTimeTypeTest method set.

@Test
public void set() throws SQLException {
    OffsetTime value = OffsetTime.now();
    OffsetTime normalized = value.withOffsetSameInstant(ZoneOffset.UTC);
    Time time = new Time(normalized.get(ChronoField.MILLI_OF_DAY));
    PreparedStatement stmt = EasyMock.createNiceMock(PreparedStatement.class);
    stmt.setTime(1, time, UTC);
    EasyMock.replay(stmt);
    type.setValue(stmt, 1, value);
    EasyMock.verify(stmt);
}
Also used : OffsetTime(java.time.OffsetTime) OffsetTime(java.time.OffsetTime) Time(java.sql.Time) PreparedStatement(java.sql.PreparedStatement) Test(org.junit.Test)

Example 54 with OffsetTime

use of java.time.OffsetTime in project jdk8u_jdk by JetBrains.

the class TCKOffsetTime method test_withNanoOfSecond_noChange.

@Test
public void test_withNanoOfSecond_noChange() {
    OffsetTime base = OffsetTime.of(11, 30, 59, 1, OFFSET_PONE);
    OffsetTime test = base.withNano(1);
    assertEquals(test, base);
}
Also used : OffsetTime(java.time.OffsetTime) Test(org.testng.annotations.Test)

Example 55 with OffsetTime

use of java.time.OffsetTime in project jdk8u_jdk by JetBrains.

the class TCKOffsetTime method test_equals_false_second_differs.

@Test(dataProvider = "sampleTimes")
public void test_equals_false_second_differs(int h, int m, int s, int n, ZoneOffset ignored) {
    s = (s == 59 ? 58 : s);
    OffsetTime a = OffsetTime.of(h, m, s, n, OFFSET_PONE);
    OffsetTime b = OffsetTime.of(h, m, s + 1, n, OFFSET_PONE);
    assertEquals(a.equals(b), false);
}
Also used : OffsetTime(java.time.OffsetTime) Test(org.testng.annotations.Test)

Aggregations

OffsetTime (java.time.OffsetTime)99 Test (org.testng.annotations.Test)86 ZonedDateTime (java.time.ZonedDateTime)9 LocalTime (java.time.LocalTime)8 Instant (java.time.Instant)7 OffsetDateTime (java.time.OffsetDateTime)7 Time (java.sql.Time)6 LocalDate (java.time.LocalDate)6 LocalDateTime (java.time.LocalDateTime)6 Test (org.junit.Test)6 Date (java.sql.Date)3 ResultSet (java.sql.ResultSet)3 Clock (java.time.Clock)3 PostgresUtils.toPGArrayString (org.jooq.util.postgres.PostgresUtils.toPGArrayString)3 BigDecimal (java.math.BigDecimal)2 PreparedStatement (java.sql.PreparedStatement)2 Timestamp (java.sql.Timestamp)2 Duration (java.time.Duration)2 Period (java.time.Period)2 ZoneId (java.time.ZoneId)2