Migrating to anorm2.4 (with play 2.4): ToStatement

2019-02-26 11:03发布

Up until the anorm included with play 2.3, I could write the following:

implicit val arbitraryClassToStatement = new ToStatement[ArbitraryClass] {
  def set( 
    s: java.sql.PreparedStatement, 
    index: Int, 
    aValue: ArbitraryClass
  )
  : Unit = {
    s.setString(
      index, 
      ArbitraryClass.definingString
    )
  }
}

and this would help insert the

SQL("INSERT INTO SomeTable Values( {nonNullAc}, {possiblyNullAc} )" ).on(
 'nonNullAc -> ArbitraryClass( "abcd" ),
 'possiblyNullAc -> Option( ArbitraryClass( "abcd" ) )
)

meaning that both ArbitraryClass and Option[ ArbitraryClass ] would be satisfied by it. This seems to no longer be the case as I get the following error:

[error]  found   : (Symbol, Option[models.Misc.Url])
[error]     (which expands to)  (Symbol, Option[java.net.URL])
[error]  required: anorm.NamedParameter

Can someone please point me to what's the right way to handle this? I'd want minimal duplication of code..

1条回答
叛逆
2楼-- · 2019-02-26 12:01

You need to create a ParameterMetaData[ArbitraryClass] for this to work

implicit object ArbitraryClassMetaData extends ParameterMetaData[ArbitraryClass] {
  val sqlType = ParameterMetaData.StringParameterMetaData.sqlType
  val jdbcType = ParameterMetaData.StringParameterMetaData.jdbcType 
}

Here I just retook the values of ParameterMetaData[String]

查看更多
登录 后发表回答