我今天一直在为此苦苦挣扎。我不知道我是否有一个好的解决方案,但我可以提供一些选择。
修补光滑的驱动程序以允许平等测试
我正在使用 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
高温高压
编辑:对下面的主人提出拉取请求。请注意,拉取请求是针对 master (2.0.0),但我仅针对 1.0.1 成功测试了它。
https://github.com/slick/slick/pull/240