如何在Anorm中将microsoft.sql.DateTimeOffset转换为Date?

时间:2015-02-25 10:45:16

标签: sql-server anorm

我开发了一个使用Scala,Play framework 2.3.8,Anorm开发的项目,并使用MS SQL数据库。

这里是代码:

val repeatedCardsQuery = SQL("Execute Forms.getListOfRepeatCalls {user}, {cardId}")
DB.withConnection { implicit c =>
  val result = repeatedCardsQuery.on("user" -> user.name, "cardId" -> id)().map(row =>
    Json.obj(
      "Unified_CardNumber" -> row[Long]("scId"),
      "ContentSituation_TypeSituationName" -> row[String]("typeOfEventsName"),
      "Unified_Date" -> row[Date]("creationDate"),
      "InfoPlaceSituation_Address" -> row[String]("address"),
      "DescriptionSituation_DescriptionSituation" -> row[Option[String]]("description")
    )
  ).toList
  Ok(response(id, "repeats", result))
}

它给了我运行时错误:

play.api.Application$$anon$1: Execution exception[[RuntimeException: Left(TypeDoesNotMatch(Cannot convert 2015-02-14 15:38:15.4089363 +03:00: class microsoft.sql.DateTimeOffset to Date for column ColumnName(.creationDate,Some(creationDate))))]]
    at play.api.Application$class.handleError(Application.scala:296) ~[play_2.10-2.3.8.jar:2.3.8]
    at play.api.DefaultApplication.handleError(Application.scala:402) [play_2.10-2.3.8.jar:2.3.8]
    at play.core.server.netty.PlayDefaultUpstreamHandler$$anonfun$14$$anonfun$apply$1.applyOrElse(PlayDefaultUpstreamHandler.scala:205) [play_2.10-2.3.8.jar:2.3.8]
    at play.core.server.netty.PlayDefaultUpstreamHandler$$anonfun$14$$anonfun$apply$1.applyOrElse(PlayDefaultUpstreamHandler.scala:202) [play_2.10-2.3.8.jar:2.3.8]
    at scala.runtime.AbstractPartialFunction.apply(AbstractPartialFunction.scala:33) [scala-library.jar:na]
Caused by: java.lang.RuntimeException: Left(TypeDoesNotMatch(Cannot convert 2015-02-14 15:38:15.4089363 +03:00: class microsoft.sql.DateTimeOffset to Date for column ColumnName(.creationDate,Some(creationDate))))
    at anorm.MayErr$$anonfun$get$1.apply(MayErr.scala:34) ~[anorm_2.10-2.3.8.jar:2.3.8]
    at anorm.MayErr$$anonfun$get$1.apply(MayErr.scala:33) ~[anorm_2.10-2.3.8.jar:2.3.8]
    at scala.util.Either.fold(Either.scala:97) ~[scala-library.jar:na]
    at anorm.MayErr.get(MayErr.scala:33) ~[anorm_2.10-2.3.8.jar:2.3.8]
    at anorm.Row$class.apply(Row.scala:57) ~[anorm_2.10-2.3.8.jar:2.3.8]
[error] application - Left(TypeDoesNotMatch(Cannot convert 2015-02-14 15:38:15.4089363 +03:00: class microsoft.sql.DateTimeOffset to Date for column ColumnName(.creationDate,Some(creationDate))))

实际上,此行发生错误:

"Unified_Date" -> row[Date]("creationDate"),

如何解决这个问题,并在这种情况下将microsoft.sql.DateTimeOffset转换为Date?

1 个答案:

答案 0 :(得分:1)

可以在Anorm调用旁边添加自定义列转换(在需要支持此类型的同一类/对象中定义或导入)。

import java.util.Date
import microsoft.sql.DateTimeOffset
import anorm.Column

implicit def columnToDate: Column[Date] = Column.nonNull { (value, meta) => 
  val MetaDataItem(qualified, nullable, clazz) = meta
  value match {
    case ms: DateTimeOffset =>
      Right(ms.getTimestamp)
    case _ => 
      Left(TypeDoesNotMatch(s"Cannot convert $value: ${value.asInstanceOf[AnyRef].getClass} to Boolean for column $qualified"))
  }
}
  

顺便说一下,使用表示为JDBC日期(Anorm支持的java.sql.Date类型)的DB值会更好。