我需要在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)
Manifest[T].runtimeClass
产生Class[_]
,而不是Class[T]
。将其投射到Class[T]
,你应该没问题:
field(f.valueDef.name, m.runtimeClass.asInstanceOf[Class[T]])