Jooq 和 scala 中的通用条件生成器

问题描述 投票:0回答:1

我需要在scala中生成jooq条件。值可以是

Long
java.math.BigDecimal
编译失败,不太明白能不能实现

通用方法

  private def genericValueFilterCondition[T: ClassTag](filters: List[(ValueFilter, List[T])])(implicit m: Manifest[T]): Condition = {
    filters.foldLeft(DSL.noCondition()) { (cond, filterAndValues) =>
      val (f, values) = filters
      f.operator match {
        // some cases omitted for brevity 
        case ValueOperator.LESS =>
          cond.and(field(f.valueDef.name, m.runtimeClass).lessThan(values.head))
        case ValueOperator.EQUAL if f.values.size > 1 =>
          cond.and(field(f.valueDef.name, m.runtimeClass).in(values: _*))
    
      }
    }
  }

调用

    genericValueFilterCondition[Long](longValues.map(f => f -> f.values.map(_.toLong))).and(
      genericValueFilterCondition[java.math.BigDecimal](decimalValues.map(f => f -> f.values.map(v => toJavaBigDecimal(v))))

编译错误:

overloaded method value lessThan with alternatives:
  (x$1: org.jooq.QuantifiedSelect[_ <: org.jooq.Record1[_$1]])org.jooq.Condition <and>
  (x$1: org.jooq.Select[_ <: org.jooq.Record1[_$1]])org.jooq.Condition <and>
  (x$1: org.jooq.Field[_$1])org.jooq.Condition <and>
  (x$1: _$1)org.jooq.Condition
 cannot be applied to (T)
...


overloaded method value in with alternatives:
  (x$1: org.jooq.Select[_ <: org.jooq.Record1[_$1]])org.jooq.Condition <and>
  (x$1: org.jooq.Field[_]*)org.jooq.Condition <and>
  (x$1: _$1*)org.jooq.Condition <and>
  (x$1: org.jooq.Result[_ <: org.jooq.Record1[_$1]])org.jooq.Condition <and>
  (x$1: java.util.Collection[_])org.jooq.Condition
 cannot be applied to (T)
scala jooq
1个回答
0
投票

据我了解,

Manifest[T].runtimeClass
产生
Class[_]
,而不是
Class[T]
。将其投射到
Class[T]
,你应该没问题:

field(f.valueDef.name, m.runtimeClass.asInstanceOf[Class[T]])
© www.soinside.com 2019 - 2024. All rights reserved.