jOOQ不使用自定义数据绑定

时间:2018-12-13 09:57:10

标签: postgresql jooq timescaledb

我正在尝试为PGInterval和Duration编写自定义数据类型绑定,以将jOOQ与TimescaleDB一起使用。遗憾的是,jOOQ在为数据库例程生成函数时不使用它。

这是我的绑定类:

import org.jooq.*
import org.jooq.conf.ParamType
import org.jooq.impl.DSL
import org.postgresql.util.PGInterval
import java.sql.SQLFeatureNotSupportedException
import java.sql.Types
import java.time.Duration
import java.util.*

class PostgresIntervalDurationBinding: Binding<Any, Duration> {
    override fun converter(): Converter<Any, Duration> {
        return object : Converter<Any, Duration> {
            override fun from(t: Any?): Duration {
                return if (t == null) Duration.ZERO else Duration.ofSeconds(pgIntervalToSeconds(t as PGInterval))
            }

            override fun to(u: Duration?): Any? {
                return if (u == null || u === Duration.ZERO) null else PGInterval().seconds = u.seconds.toDouble()
            }

            override fun fromType(): Class<Any> {
                return Any::class.java
            }

            override fun toType(): Class<Duration> {
                return Duration::class.java
            }
        }

    }

    override fun sql(ctx: BindingSQLContext<Duration>?) {
        if (ctx?.render()?.paramType() == ParamType.INLINED)
            ctx.render()?.visit(DSL.inline(ctx.convert(converter()).value()))?.sql("::interval")
        else
            ctx?.render()?.sql("?::interval")
    }

    override fun register(ctx: BindingRegisterContext<Duration>?) {
        ctx?.statement()?.registerOutParameter(ctx.index(), Types.VARCHAR)
    }

    override fun set(ctx: BindingSetStatementContext<Duration>?) {
        ctx?.statement()?.setString(ctx.index(), Objects.toString(ctx.convert(converter()).value(), null))
    }

    override fun get(ctx: BindingGetResultSetContext<Duration>?) {
        ctx?.convert(converter())?.value(ctx.resultSet().getString(ctx.index()))
    }

    override fun get(ctx: BindingGetStatementContext<Duration>?) {
        ctx?.convert(converter())?.value(ctx.statement().getString(ctx.index()))
    }

    override fun set(ctx: BindingSetSQLOutputContext<Duration>?) {
        throw SQLFeatureNotSupportedException()
    }

    override fun get(ctx: BindingGetSQLInputContext<Duration>?) {
        throw SQLFeatureNotSupportedException()
    }

    companion object {
        fun pgIntervalToSeconds(t: PGInterval): Long {
            var seconds = 0L
            with(t){
                seconds += Duration.ofSeconds(this.seconds.toLong()).seconds
                seconds += Duration.ofMinutes(this.minutes.toLong()).seconds
                seconds += Duration.ofHours(this.hours.toLong()).seconds
                seconds += Duration.ofDays(this.days.toLong()).seconds
                if (months > 0 || years > 0) throw SQLFeatureNotSupportedException()
            }
            return seconds
        }
    }
}

这是我在pom中的配置:

<database>
<name>org.jooq.meta.postgres.PostgresDatabase</name>
<includes>.*</includes>
<inputSchema>public</inputSchema>
<excludes>set_adaptive_chunking
    | flyway_schema_history
</excludes>
<forcedTypes>
    <forcedType>
        <userType>java.time.Duration</userType>

        <binding>de.ninjaneers.dmc.jooq.databindings.PostgresIntervalDurationBinding
        </binding>
        <expression>.*interval.*</expression>

        <types>.*</types>
    </forcedType>
</forcedTypes>
</database>

例如,我希望jOOQ生成例程

time_bucket(bucket_with interval, ts timestamp with time zone) 

timeBucket(Field<Duration> bucketWidth, Field<Timestamp> ts) 

但我知道

timeBucket(Field<Object> bucketWidth, Field<Timestamp> ts)

1 个答案:

答案 0 :(得分:0)

您正在混淆两个配置标志:

<expression>.*interval.*</expression>
<types>.*</types>

<expression>是一个正则表达式,与您要替换其类型的列/属性/参数的标识符匹配。 <types>与数据类型匹配。这就是您想要的:

<types>.*interval.*</types>
<expression>.*</expression>