LocalDateTime, rather than the JDBC
java.sql.Timestamptype. In jOOQ 3.9+, this is a no brainer, as we’ve finally introduced the
<javaTimeTypes>flag to automatically generate JSR 310 types instead of JDBC types. But sometimes, you want some custom conversion behaviour, so you write a
Converter. To the rescue our new jOOQ 3.9+ converter constructors, which essentially take two lambdas to construct a converter for you. For instance:
And you’re set! Even easier, if you don’t need any special
Converter<Timestamp, LocalDateTime> converter = Converter.of( Timestamp.class, LocalDateTime.class, t -> t == null ? null : t.toLocalDateTime(), u -> u == null ? null : Timestamp.valueOf(u) );
nullencoding (as above), just write this equivalent converter, instead:
Where’s that useful? The code generator needs a concrete converter class, so you cannot use that with the code generator, but there are many other places in the jOOQ API where converters are useful, including when you write plain SQL like this:
Converter<Timestamp, LocalDateTime> converter = Converter.ofNullable( Timestamp.class, LocalDateTime.class, Timestamp::toLocalDateTime Timestamp::valueOf );
DSL.field( "my_table.my_timestamp", SQLDataType.TIMESTAMP.asConvertedDataType( Converter.ofNullable(...) ));
2 thoughts on “How to Write a Quick and Dirty Converter in jOOQ”
Would be nice to see how these could be used to covert result of an expression from SELECT clause. Is there something like this?
dsl.select(nvl2(MY_TABLE.MY_COLUMN, "notnull", "null").convert(myConverter)).from(...)...
Thanks for your message. You always have to work with a DataType. In your case, I would assume this could work:
And now use that field in SQL