Migrating to anorm2.4 (with play 2.4): ToStatement[T] and ToStatement[Option[T]]

孤人 提交于 2019-12-02 05:01:48

问题


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:


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]



来源:https://stackoverflow.com/questions/33809995/migrating-to-anorm2-4-with-play-2-4-tostatementt-and-tostatementoptiont

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!