Jooq自定义类型,MockResult抛出DataTypeException

时间:2014-03-05 18:05:40

标签: java sql groovy jooq

尝试使用Jooq模拟自定义类型(Joda Time)设置单元测试时出错。我在代码生成期间注册了转换器。它几乎看起来像Jooq找不到/没有看到转换器,并试图回到ConvertAll,这是行不通的。

查询模拟结果会抛出异常(如下)。

转换器:

import org.joda.time.DateTime;
import java.sql.Timestamp;

import org.joda.time.DateTimeZone;
import org.jooq.Converter;

public class DateTimeConverter implements Converter<Timestamp, DateTime> {
    @Override
    public DateTime from(Timestamp databaseObject) {
        return new DateTime(databaseObject.getTime()).withZone(DateTimeZone.UTC);
    }

    @Override
    public Timestamp to(DateTime userObject) {
        return new Timestamp(userObject.getMillis());
    }

    @Override
    public Class<Timestamp> fromType() {
        return Timestamp.class;
    }

    @Override
    public Class<DateTime> toType() {
        return DateTime.class;
    }
}

该类已成功生成:

/**
 * This class is generated by jOOQ
 */
package redacted.generated.jooq.tables;

/**
 * This class is generated by jOOQ.
 */
@javax.annotation.Generated(value    = {"http://www.jooq.org", "3.0.0"},
                            comments = "This class is generated by jOOQ")
@java.lang.SuppressWarnings({ "all", "unchecked" })
public class Bug extends org.jooq.impl.TableImpl<redacted.generated.jooq.tables.records.BugRecord> {

    private static final long serialVersionUID = 1992533553;

    /**
     * The singleton instance of <code>public.bug</code>
     */
    public static final redacted.generated.jooq.tables.Bug BUG = new redacted.generated.jooq.tables.Bug();

    /**
     * The class holding records for this type
     */
    @Override
    public java.lang.Class<redacted.generated.jooq.tables.records.BugRecord> getRecordType() {
        return redacted.generated.jooq.tables.records.BugRecord.class;
    }

    /**
     * The column <code>public.bug.testdate</code>. 
     */
    public final org.jooq.TableField<redacted.generated.jooq.tables.records.BugRecord, org.joda.time.DateTime> TESTDATE = createField("testdate", org.jooq.impl.SQLDataType.TIMESTAMP.asConvertedDataType(new name.benjaminAbbitt.jooqJodaTime.DateTimeConverter()), this);

    /**
     * The column <code>public.bug.id</code>. 
     */
    public final org.jooq.TableField<redacted.generated.jooq.tables.records.BugRecord, java.lang.Integer> ID = createField("id", org.jooq.impl.SQLDataType.INTEGER, this);

    /**
     * Create a <code>public.bug</code> table reference
     */
    public Bug() {
        super("bug", redacted.generated.jooq.Public.PUBLIC);
    }

    /**
     * Create an aliased <code>public.bug</code> table reference
     */
    public Bug(java.lang.String alias) {
        super(alias, redacted.generated.jooq.Public.PUBLIC, redacted.generated.jooq.tables.Bug.BUG);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public org.jooq.Identity<redacted.generated.jooq.tables.records.BugRecord, java.lang.Integer> getIdentity() {
        return redacted.generated.jooq.Keys.IDENTITY_BUG;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public java.util.List<org.jooq.UniqueKey<redacted.generated.jooq.tables.records.BugRecord>> getKeys() {
        return java.util.Arrays.<org.jooq.UniqueKey<redacted.generated.jooq.tables.records.BugRecord>>asList(redacted.generated.jooq.Keys.BUG_ID_KEY);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public redacted.generated.jooq.tables.Bug as(java.lang.String alias) {
        return new redacted.generated.jooq.tables.Bug(alias);
    }
}

例外是:

org.jooq.exception.DataTypeException: Cannot convert from 2014-03-05T17:57:24.668Z (class org.joda.time.DateTime) to class java.sql.Timestamp
at org.jooq.tools.Convert$ConvertAll.fail(Convert.java:809)
at org.jooq.tools.Convert$ConvertAll.from(Convert.java:747)
at org.jooq.tools.Convert.convert0(Convert.java:296)
at org.jooq.tools.Convert.convert(Convert.java:288)
at org.jooq.tools.Convert.convert(Convert.java:349)
at org.jooq.impl.AbstractRecord.getValue(AbstractRecord.java:219)
at org.jooq.tools.jdbc.MockResultSet.getValue(MockResultSet.java:383)
at org.jooq.tools.jdbc.MockResultSet.getTimestamp(MockResultSet.java:566)
at org.jooq.impl.Utils.getTimestamp(Utils.java:2195)
at org.jooq.impl.Utils.getFromResultSet(Utils.java:1952)
at org.jooq.impl.Utils.getFromResultSet(Utils.java:1881)
at org.jooq.impl.CursorImpl$CursorIterator$CursorRecordInitialiser.setValue(CursorImpl.java:1464)
at org.jooq.impl.CursorImpl$CursorIterator$CursorRecordInitialiser.operate(CursorImpl.java:1447)
at org.jooq.impl.CursorImpl$CursorIterator$CursorRecordInitialiser.operate(CursorImpl.java:1439)
at org.jooq.impl.RecordDelegate.operate(RecordDelegate.java:119)
at org.jooq.impl.CursorImpl$CursorIterator.fetchOne(CursorImpl.java:1412)
at org.jooq.impl.CursorImpl$CursorIterator.next(CursorImpl.java:1389)
at org.jooq.impl.CursorImpl$CursorIterator.next(CursorImpl.java:1353)
at org.jooq.impl.CursorImpl.fetch(CursorImpl.java:202)
at org.jooq.impl.CursorImpl.fetch(CursorImpl.java:176)
at org.jooq.impl.AbstractResultQuery.execute(AbstractResultQuery.java:268)
at org.jooq.impl.AbstractQuery.execute(AbstractQuery.java:321)
at org.jooq.impl.AbstractResultQuery.fetch(AbstractResultQuery.java:324)
at org.jooq.impl.SelectImpl.fetch(SelectImpl.java:1034)
at org.jooq.ResultQuery$fetch.call(Unknown Source)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:45)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:108)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:112)
at redacted.bug.BugTest.getTest(BugTest.groovy:47)

BugTest.groovy:

public class BugTest {
    @Test
    public void getTest() {

        DSLContext testContext = setupDSL(new MockDataProvider() {
            @Override
            public MockResult[] execute(MockExecuteContext ctx) throws SQLException {
                DSLContext create = DSL.using(SQLDialect.POSTGRES)
                def result = create.newResult(BUG)

                result.add(create.newRecord(BUG, [id: 0, testdate: new DateTime()]))

                [new MockResult(result.size(), result)]
            }
        })

        testContext.select().from(BUG).fetch()  //this line fails
    }

    private DSLContext setupDSL(MockDataProvider provider) {
        MockConnection connection = new MockConnection(provider)
        return DSL.using(connection, SQLDialect.POSTGRES)
    }
}

1 个答案:

答案 0 :(得分:0)

这是bug #5771。从jOOQ 3.9.0开始,MockResultSet不使用您的转换器将用户定义的类型<U>DateTime)还原为JDBC /数据库类型<T>({ {1}})在通过JDBC API公开它之前。在不使用转换器的情况下,jOOQ不知道如何执行此转换,因此例外。

正确的行为应该是:

  • 您可以使用Timestamp<U>)值创建结果。
  • jOOQ DateTime使用您的转换器将MockResultSet转换为<U><T>),然后通过JDBC API传递该值
  • jOOQ的Timestamp从JDBC API中获取ResultQuery
  • jOOQ使用您的转换器将<T>再次转换回<T>

解决方法:

目前,您不应在<U>中放置任何用户定义的类型,而只应使用JDBC MockResult类型(并且不会生成引用用户定义类型的生成列) )