5

我正在对 postgresql 数据库使用 Play 2.1.4。在 postgresql 数据库中,我使用 uuid 作为我的 pk 数据类型,它与 java.util.UUID 相关。anorm 中的 SqlParser.getT 函数没有 java.util.UUID 的隐式提取器。这是有道理的,因为我认为没有多少人使用它。但是,我似乎找不到有关如何添加的说明。有谁知道如何在 Play 中向 anorm.SqlParser 添加一个额外的隐式提取器?

我得到的错误如下:

找不到参数提取器的隐式值:anorm.Column[java.util.UUID]

我对 Scala 和 Play 真的很陌生,所以如果我的方法完全错误,请告诉我,但我真的很想能够像你在下面看到的那样做一些事情。

case class App(appId: UUID, appName: String, appServerName: String,
               appComponent: String, appDescription: String,
               appDateCreated: DateTime, appDateModified: DateTime,
               appValidated: Boolean)

val app = {
    get[UUID]("app_id") ~
    get[String]("app_name") ~
    get[String]("app_server_name") ~
    get[String]("app_component") ~
    get[String]("app_description") ~
    get[java.util.Date]("app_date_created") ~
    get[java.util.Date]("app_date_modified") ~
    get[Boolean]("app_validated") map {
      case id ~ name ~ serverName ~ component ~ description ~ dateCreated ~
        dateModified ~ validated  => App(id, name, serverName, component,
      description, new DateTime(dateCreated.getTime),
        new DateTime(dateModified.getTime), validated)
    }
  }

def all(): List[App] = DB.withConnection { implicit conn =>
  SQL("SELECT * FROM apps").as(app *)
}
4

2 回答 2

7

这是返回 java.util.UUID 的 JDBC 驱动程序的 @r.piesnikowski 答案的简化变体,就像 PostgreSQL 一样:

  /**
   * Implicit conversion from UUID to Anorm statement value
   */
  implicit def uuidToStatement = new ToStatement[UUID] {
    def set(s: java.sql.PreparedStatement, index: Int, aValue: UUID): Unit = s.setObject(index, aValue)
  }

  /**
   * Implicit conversion from Anorm row to UUID
   */
  implicit def rowToUUID: Column[UUID] = {
    Column.nonNull[UUID] { (value, meta) =>
      value match {
        case v: UUID => Right(v)
        case _ => Left(TypeDoesNotMatch(s"Cannot convert $value:${value.asInstanceOf[AnyRef].getClass} to UUID for column ${meta.column}"))
      }
    }
  }
于 2013-10-31T20:43:19.160 回答
5

也许这篇文章会有所帮助。(在我的项目中使用。工作正常)

 /**
   * Attempt to convert a SQL value into a UUID
   *
   * @param value value to convert
   * @return UUID
   */
  private def valueToUUIDOption(value: Any): Option[UUID] = {
    try {
      valueToByteArrayOption(value) match {
        case Some(bytes) => Some(UUIDHelper.fromByteArray(bytes))
        case _ => None
      }
    }
    catch {
      case e: Exception => None
    }
  }
/**
   * Implicit conversion from UUID to anorm statement value
   */
  implicit def uuidToStatement = new ToStatement[UUID] {
    def set(s: java.sql.PreparedStatement, index: Int, aValue: UUID): Unit = s.setObject(index, aValue)
  }
 /**
   * Implicit converstion from anorm row to uuid
   */
  implicit def rowToUUID: Column[UUID] = {
    Column.nonNull[UUID] { (value, meta) =>
      val u = UUID.fromString(value.toString)
      val MetaDataItem(qualified, nullable, clazz) = meta
      valueToUUIDOption(value) match {
        case Some(uuid) => Right(uuid)
        case _ => Left(TypeDoesNotMatch("Cannot convert " + value + ":" + value.asInstanceOf[AnyRef].getClass + " to UUID for column " + qualified))
      }
    }
  }
于 2013-09-17T10:52:14.940 回答