How to Write a Quick and Dirty Converter in jOOQ
Need to convert SQL types to something a little more custom than JDBC or even plain JSR-310 types? Well, jOOQ just released custom converters to give you a hand.
Join the DZone community and get the full member experience.
Join For FreeOne 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 behavior, so you write a Converter
.
To the rescue are 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(...)
));
Published at DZone with permission of Lukas Eder. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments