问题
I have a statement like this:
SelectSelectStep<Record14<
String, String, String, Timestamp, String, String, String, Integer,
Timestamp, String, String, String, Integer, DayToSecond>> select = create.select(
TECHNICAL_WORK.NUMBER, TECHNICAL_CODE.CODE, TECHNICAL_CODE.DESCRIPTION,
twLog.START_TIME, twStartLog.CODE, twStartLog.FIRST_NAME, twStartLog.LAST_NAME, twStartLog.CATEGORY,
twLog.STOP_TIME, twEndLog.CODE, twEndLog.FIRST_NAME, twEndLog.LAST_NAME, twEndLog.CATEGORY,
DSL.timestampDiff(twLog.STOP_TIME, twLog.START_TIME)
);
the last field is of type Field<DayToSecond>
and I need to convert it to Field<Double>
using DayToSecond.totalMinutes()
I can't find an api like this, eg:
DSL.timestampDiff(twLog.STOP_TIME, twLog.START_TIME).map( from -> from.totalMinutes() )
Can I do this conversion somehow?
回答1:
As you've mentioned in the comments, you cannot convert the type of a jOOQ expression by providing instructions on how to map types after fetching (although that does look interesting, I have created a feature request for this: #7100).
You have several options:
Attach a converter to your expression
This works like the feature request #7100 but is more clumsy:
Field<DayToSecond> diff = DSL.timestampDiff(
DSL.currentTimestamp(),
DSL.timestamp("2018-01-01 00:00:00")
);
Field<Double> d = DSL.field("{0}", diff.getDataType()
.asConvertedDataType(Converter.ofNullable(
DayToSecond.class,
Double.class,
DayToSecond::getTotalMinutes,
m -> DayToSecond.valueOf(m * 60000) // Optional implementation here
)), diff);
System.out.println(create.select(d).fetchOne());
Convert the value after fetching
Of course, you can also simply fetch a DayToSecond
value and then convert the jOOQ Result
using any tool of your preference, including:
- Result.map(RecordMapper)
Result.stream()
来源:https://stackoverflow.com/questions/48448131/jooq-convert-field-in-select-statement-from-type1-to-type2