尝试按UUID过滤时出错 - Slick

时间:2013-07-05 20:27:54

标签: scala slick

我正在尝试使用以下内容过滤我的表:

val applicationId = ...
val application = Applications.filter(_.id === applicationId).first

其中idapplicationId是UUID。

我收到的错误是:

scala.slick.SlickException: UUID does not support a literal representation

我发现我需要使用bind

// val applicationId is a UUID
val application = Applications.filter(_.id === applicationId.bind).first

然而,这是抛出异常

java.util.NoSuchElementException: Invoker.first

即使我查询的是我知道的UUID在表中

这是Slick正在制作的selectStatement。我不确定为什么它不包含UUID?

select x2."Id" from "Application" x2 where x2."Id" = ?

1 个答案:

答案 0 :(得分:3)

我今天一直在努力解决这个问题。我不知道我是否有一个好的解决方案,但我可以提供一些选择。

Patch光滑驱动程序允许进行相等测试

我正在使用MySQL,该数据库支持将字节数据作为字符串发送的语法:

mysql> desc user;
+---------------+--------------+------+-----+---------+-------+
| Field         | Type         | Null | Key | Default | Extra |
+---------------+--------------+------+-----+---------+-------+
| user_uuid     | binary(16)   | NO   | PRI | NULL    |       |
| email         | varchar(254) | NO   | UNI | NULL    |       |
| display_name  | varchar(254) | NO   |     | NULL    |       |
| password_hash | varchar(254) | YES  |     | NULL    |       |
+---------------+--------------+------+-----+---------+-------+
4 rows in set (0.01 sec)

mysql> select * from user where user_uuid = x'e4d369040f044b6e9561765c356907d3';
+------------------+-----------------+--------------+---------------+
| user_uuid        | email           | display_name | password_hash |
+------------------+-----------------+--------------+---------------+
| ????????????     | foo@example.com | Foo          | NULL          |
+------------------+-----------------+--------------+---------------+
1 row in set (0.00 sec)

显示UUID是否正确:

mysql> select hex(user_uuid), email, display_name from user where user_uuid = x'e4d369040f044b6e9561765c356907d3';
+----------------------------------+-----------------+--------------+
| hex(user_uuid)                   | email           | display_name |
+----------------------------------+-----------------+--------------+
| E4D369040F044B6E9561765C356907D3 | foo@example.com | Foo          |
+----------------------------------+-----------------+--------------+

在此基础上,MySQLDriver.scala(版本1.0.1)的以下补丁非常有效:

$ git diff
diff --git a/src/main/scala/scala/slick/driver/MySQLDriver.scala b/src/main/scala/scala/slick/driver/
index 84a667e..1aa5cca 100644
--- a/src/main/scala/scala/slick/driver/MySQLDriver.scala
+++ b/src/main/scala/scala/slick/driver/MySQLDriver.scala
@@ -174,9 +174,15 @@ trait MySQLDriver extends ExtendedDriver { driver =>
       }
     }

+    import java.util.UUID
+
     override val uuidTypeMapperDelegate = new UUIDTypeMapperDelegate {
       override def sqlType = java.sql.Types.BINARY
       override def sqlTypeName = "BINARY(16)"
+
+    override def valueToSQLLiteral(value: UUID): String =
+      "x'"+value.toString.replace("-", "")+"'"
     }
   }
 }

有了这个,您可以执行以下代码:

def findUserByUuid(uuid: UUID): Option[UserRecord] = db.withSession {
  // Note: for this to work with slick 1.0.1 requires a tweak to MySQLDriver.scala
  // If you don't, the "===" in the next line will fail with:
  // "UUID does not support a literal representation".
  val query = (for (u <- UserRecordTable if u.uuid === uuid) yield u)
  query.firstOption
}

我不确定您使用的是哪个数据库,但类似的方法可能有效。 我没有对此进行任何性能测试。

将UUID编码为字节数组

有一些选项可以避免光滑的代码补丁。

使用http://nineofclouds.blogspot.com.au/2013/04/storing-uuids-with-anorm.html中的UUIDHelper,您可以将UUID编码为字节数组并执行静态查询。有点不优雅但有点像:

import UuidHelper

implicit object SetUUID extends SetParameter[UUID] {
  def apply(v: UUID, pp: PositionedParameters) { pp.setBytes(UuidHelper.toByteArray(v)) }
}
implicit val getUserRecordResult = GetResult(r => 
  UserRecord(UuidHelper.fromByteArray(r.nextBytes()), r.<<, r.<<, r.<<)
)

val userByUuid = StaticQuery[UUID, UserRecord] + "select * from user where user_uuid = ?"
val user = userByUuid(user.uuid).first
// do what you want with user.

将UUID编码为十六进制字符串

以下可能是最不优雅的,但包括完整性:

implicit val getUserRecordResult = GetResult(r => UserRecord(UuidHelper.fromByteArray(r.nextBytes()), r.<<, r.<<, r.<<))

val uuid = user.uuid.toString.replace("-", "")
val userByUuid = StaticQuery.queryNA[UserRecord](s"select * from user where user_uuid = x'$uuid'")
val user = userByUuid().first

HTH

编辑:下面针对主人的拉取请求。请注意,pull请求是针对master(2.0.0),但我仅针对1.0.1成功测试了它。

https://github.com/slick/slick/pull/240