postgres中的"带时区的时间戳"的Jooq绑定

ass*_*ias 4 java postgresql jooq java-8 java-time

Jooq目前不支持JSR 310类型,直到v3.8才会支持.

使用简单的转换器通常可以工作,除了某些类型,例如postgres' TIMESTAMP WITH TIME ZONE,它需要自定义绑定.所以我尝试编写一个,但生成的XxxRecord类仍然使用Timestamp数据类型作为TIMESTAMP WITH TIME ZONE我的数据库中的字段.

我需要在下面的代码中更改以将postgres' TIMESTAMP WITH TIME ZONE视为Instantjooq生成的类中的内容?

变流器

public class TimestampConverter implements Converter<Timestamp, Instant> {
  @Override public Instant from(Timestamp ts) {
    return ts == null ? null : ts.toInstant();
  }
  @Override public Timestamp to(Instant instant) {
    return instant == null ? null : Timestamp.from(instant);
  }
  @Override public Class<Timestamp> fromType() { return Timestamp.class; }
  @Override public Class<Instant> toType() { return Instant.class; }
}
Run Code Online (Sandbox Code Playgroud)

自定义绑定

public class TimestampBinding implements Binding<Timestamp, Instant> {

  private static final Converter<Timestamp, Instant> converter = new TimestampConverter();

  private final DefaultBinding<Timestamp, Instant> delegate = 
                                                       new DefaultBinding<> (converter());

  @Override public Converter<Timestamp, Instant> converter() { return converter; }

  @Override public void sql(BindingSQLContext<Instant> ctx) throws SQLException {
    delegate.sql(ctx);
  }

  //etc. same for all other overriden methods.
}
Run Code Online (Sandbox Code Playgroud)

pom.xml(摘录)

<customType>
  <name>java.time.Instant</name>
  <type>java.time.Instant</type>
  <binding>xxx.TimestampBinding</binding>
</customType>

...

<forcedType>
  <name>java.time.Instant</name>
  <types>timestamp with time zone</types>
</forcedType>
Run Code Online (Sandbox Code Playgroud)

hee*_*nee 6

一种方法是<types>使用反斜杠来逃避空格,如下所示:

<types>timestamp\ with\ time\ zone</types>
Run Code Online (Sandbox Code Playgroud)

您不能只有常规空格,<types>因为默认情况下,org.jooq.util.AbstractDatabase在COMMENTS模式下解析正则表达式,这使得创建的Pattern对象忽略正则表达式中的空格.您也可以执行类似的操作<types>timestamp.*zone</types>,或指定您自己的操作<regexFlags>.

以下是jooq-codegen-maven适用于我的完整Maven 插件标记.我也发现这<binding>是不必要的.

<plugin>
    <groupId>org.jooq</groupId>
    <artifactId>jooq-codegen-maven</artifactId>
    <version>3.7.0</version>

    <executions>
        <execution>
            <goals>
                <goal>generate</goal>
            </goals>
        </execution>
    </executions>

    <configuration>

        <jdbc>
            <driver>org.postgresql.Driver</driver>
            <url>jdbc:postgresql:postgres</url>
            <user>postgres</user>
            <password>mypass</password>
        </jdbc>

        <generator>
            <database>
                <customTypes>
                    <customType>
                        <name>Instant</name>
                        <type>java.time.Instant</type>
                        <converter>xxx.TimestampConverter</converter>
                    </customType>
                </customTypes>

                <forcedTypes>
                    <forcedType>
                        <name>Instant</name>
                        <types>timestamp\ with\ time\ zone</types>
                    </forcedType>
                </forcedTypes>

                <name>org.jooq.util.postgres.PostgresDatabase</name>
                <includes>author</includes>
                <excludes/>
                <inputSchema>public</inputSchema>
            </database>
            <target>
                <packageName>xxx.table</packageName>
                <directory>target/generated-sources/jooq</directory>
            </target>
        </generator>
    </configuration>
</plugin>
Run Code Online (Sandbox Code Playgroud)