Scala 类型MapColumn、SetColumn、JsonColumn需要所有者和记录。这些价值观究竟是什么?
例如,我有Scala 类型MapColumn、SetColumn、JsonColumn需要所有者和记录。这些价值观究竟是什么?,scala,cassandra,phantom-dsl,Scala,Cassandra,Phantom Dsl,例如,我有 type MapColumn[Owner <: com.websudos.phantom.dsl.CassandraTable[Owner, Record], Record, K, V] = com.websudos.phantom.column.MapColumn[Owner, Record, K, V] type MapColumn[Ownerphantom的全部功能在于它能够围绕您的数据模型进行映射,并返回类型安全的结果,或者我在编写它时就想到了这一点。Own
type MapColumn[Owner <: com.websudos.phantom.dsl.CassandraTable[Owner, Record], Record, K, V] =
com.websudos.phantom.column.MapColumn[Owner, Record, K, V]
type MapColumn[Ownerphantom的全部功能在于它能够围绕您的数据模型进行映射,并返回类型安全的结果,或者我在编写它时就想到了这一点。Owner
type参数是用户编写的表的类型及其所需参数,因此您可以执行以下操作:
select.where(_.id eqs id)
看起来很简单,但诀窍在于,如果没有精练的类型param,编译器可以通过它“记忆”您在表中任意定义的列,您将永远无法在DSL代码中“知道”用户编写的列
因此DSL必须知道您将通过扩展CassandraTable
创建的表的最终类型
case class MyRecord(id: UUID, name: String)
class MyTable extends CassandraTable[MyTable, MyRecord] {
object id extends UUIDColumn(this) with PartitionKey[UUID]
// MyTable is Owner and MyRecord is Record.
object mapColumn extends MapColumn[MyTable, MyRecord, String, String](this)
}
这就是为什么所有查询生成器都是一个从表:Owner
到其他函数的函数。甚至上面所说的都只是以下的简写符号:
select.where(table => table.id eqs id)
记录
类型是使Cassandra结果类型安全的原因。通过告诉您的表它包装了什么案例类,phantom能够使用隐式api方法将所有结果映射回这个案例类,因此不必处理以下事情:
res.getString("mystring")
这样的事情在幕后是看不见的,幽灵“知道”返回的Cassandra行的结果属于case类
中的哪个字段。它的详细程度大大降低,效率也大大提高,因为您不想真正关心驱动程序如何处理其内部对Netty缓冲区的解析以及客户机和数据库之间的CQL消息交换,您只想返回记录
因此,需要将Record
与fromRow
方法结合使用,它们不仅在这些列中传递,而且在每个列中传递。唯一的区别是,使用StringColumn
,编译器能够为您推断T
和R
的类型,因此您无需键入。
这是因为:
type StringColumn[
Owner <: CassandraTable[Owner, Record],
Record
] = com.websudos.phantom.column.PrimitiveColumn[Owner, Record, String]
谢谢你这么详细的回答!有一个有趣的问题。IntelliJ IDEA用一条消息“不适用”强调了(这一点)。但是代码编译没有任何问题。我还发现嵌套映射不受支持,对吗?
object map extends MapColumn[String, String](this)