我正在使用支持数组列字段的 postgresql。为了解析一行,我使用了这个解析器。它在 Array 对象处有错误。我想我做错了。
case class ServiceRequest(
id: Pk[Long],
firstname: String,
lastname: String,
images: Array[String])
val parser: RowParser[ServiceRequest] = {
get[Pk[Long]]("id") ~
get[String]("firstname") ~
get[String]("lastname") ~
Error here >>> get[Array[String]]("images") map {
case id ~ firstname ~ lastname ~ images=>
ServiceRequest(id, firstname, lastname, images)
}
}
谢谢
最佳答案
Array[T]
类型现在在 play 2.4.x 中得到原生支持,您不必使用自己的转换器。
但是,使用插入或更新语句仍然不太好:
def updateTags(id: Long, values: Seq[String]):Int = {
DB.withConnection { implicit conn =>
SQL("UPDATE entries SET tags = {value} WHERE id = {id}")
.on('value -> values, 'id -> id).executeUpdate
}
会给你一个错误
play - Cannot invoke the action, eventually got an error: org.postgresql.util.PSQLException: ERROR: syntax error at or near "$2"
简单地说,您应该使用java.sql.Array
类型创建PreparedStatement
:
on('value -> conn.createArrayOf("varchar", value.asInstanceOf[Array[AnyRef]]), ...
截至目前(玩2.4-M1),java.sql.Array
默认不转换为ParameterValue
,也就是说
SQL(...).on(`arr -> values:java.sql.Array )
仍然给出编译错误,您需要另一个隐式转换才能编译:
implicit object sqlArrayToStatement extends ToStatement[java.sql.Array] {
def set(s: PreparedStatement, i: Int, n: java.sql.Array) = s.setArray(i, n)
}
关于postgresql - 如何用数组创建异常的解析器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22547951/