One of
jOOQ‘s most powerful features is the capability of introducing custom data types, pretending the database actually understands them. For instance, when working with SQL TIMESTAMP types, users mostly want to use the new JSR-310
LocalDateTime
, rather than the JDBC
java.sql.Timestamp
type.
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:
Converter<Timestamp, LocalDateTime> converter =
Converter.of(
Timestamp.class,
LocalDateTime.class,
t -> t == null ? null : t.toLocalDateTime(),
u -> u == null ? null : Timestamp.valueOf(u)
);
And you’re set! Even easier, if you don’t need any special
null
encoding (as above), just write this equivalent converter, instead:
Converter<Timestamp, LocalDateTime> converter =
Converter.ofNullable(
Timestamp.class,
LocalDateTime.class,
Timestamp::toLocalDateTime
Timestamp::valueOf
);
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:
DSL.field(
"my_table.my_timestamp",
SQLDataType.TIMESTAMP.asConvertedDataType(
Converter.ofNullable(...)
));
Like this:
Like Loading...
Published by lukaseder
I made jOOQ
View all posts by lukaseder
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