This is one of alternative approaches to a fork that I've been looking at
but it's failing. I hope it's something easily fixed since it's part of the
documented API.
What I want is to be able to run this code and get Joda LocalDate objects
instead of java.sql.Date objects. The cleanest approach is
public class RedshiftRecordMapper implements RecordMapper<Record,
Map<String, Object>> {
@Override
public Map<String, Object> map(final Record record) {
Map<String, Object> map = new LinkedHashMap<>(record.size());
for (Field<?> field : record.fields()) {
Converter converter = field.getDataType().getConverter();
map.put(field.getName(), record.getValue(field, converter));
}
}
I'm using the extra step of retrieving the converter since we're currently
looking up the Converter in our own cache and then doing a substantial
amount of post-processing since we aren't fully populating that cache. It
would be nice if we could have field.getDataType() either return our
DataType or have the standard DataType return our converter.
One of the alternatives I'm trying is to use the Result#field() method:
@Test
public void testDate() throws Exception {
try (DSLContext ctx = DSL.using(connectionProvider,
SQLDialect.POSTGRES)) {
ResultQuery query = ctx.resultQuery("select * from jooqtest");
Result result = ctx.fetch(query);
Field<?> field = result.field(0,
PostgreSQLLocalDateDataType.class);
Object obj = result.getValue(0, field);
System.out.println("object class: " + obj.getClass().getName());
}
}
where PostgreSQLLocalDateDataType extends DefaultDataType (I know!) and
specifies the SQLDialect.POSTGRES dialect.
// based on jOOQ 3.9
public class PostgreSQLLocalDateDataType extends DefaultDataType<LocalDate>
{
private static final long serialVersionUID = 1L;
private static final Binding<Date, LocalDate> BINDING = new
LocalDateBinding();
public PostgreSQLLocalDateDataType() {
super(SQLDialect.POSTGRES, LocalDate.class, BINDING,
"date", "date", 0, 0, 0, true, null);
}
where LocalDateBinding is
public class LocalDateBinding extends DefaultBinding<Date, LocalDate> {
private static final long serialVersionUID = 1L;
public LocalDateBinding() {
super(new DefaultLocalDateConverter());
}
}
and DefaultLocalDateConverter is a simple converter between java.sql.Date
and org.joda.time.LocalDate.
When I run the test method I get
o.j.e.SQLDialectNotSupportedException : Not supported by dialect :
Type class org.jooq.impl.PostgreSQLLocalDateDataType is not supported in
dialect null
Obviously I'm missing something since I would have expected the dialect to
follow from the DSLContext. Do I need to specify an additional value?
FWIW I've also tried it with fetch(ResultSet, DataType) using the same
converter and I get an error saying that a converter for SQLDialect.DEFAULT
isn't available. This happens even if I change my datatype to use
SQLDialect.DEFAULT instead of SQLDialect.POSTGRES.
Thanks,
Bear
--
You received this message because you are subscribed to the Google Groups "jOOQ
User Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.