以干燥方式扩展SLICK表

时间:2013-06-21 21:23:17

标签: scala slick

我对Slick / Scala有一个有趣的问题,我希望你们中的一个好伙伴可以帮助我。

我在SLICK案例类中有几个表格和扩展名

case class A(...)
case class B(...)
case class C(...)
分享这些共同领域的

(id: String, livemode: Boolean, created: DateTime, createdBy : Option[Account]) . 

因为在每个案例类中重复这些字段,所以我想探索将它们提取到单个对象或类型中的可能性。

但是,在创建SLICK表对象时,我希望最终包含这些公共字段的位置,以便我可以在每个表中保留它们各自的值。

object AsTable extends Table[A]("a_table") { 
  ...
  def id = column[String]("id", O.PrimaryKey)
  def livemode = column[Boolean]("livemode", O.NotNull)
  def created = column[DateTime]("created", O.NotNull)
  def createdBy = column[Account]("created_by", O.NotNull)
  ... 
} 

实际上,我正在寻找的最终结果是允许我更改公共字段而无需更新每个表。

有办法做到这一点吗?

提前致谢

1 个答案:

答案 0 :(得分:18)

我没有试过这个,但是你混入的特性如何:

trait CommonFields { this: Table[_] =>
  def id = column[String]("id", O.PrimaryKey)
  def livemode = column[Boolean]("livemode", O.NotNull)
  def created = column[DateTime]("created", O.NotNull)
  def createdBy = column[Account]("created_by", O.NotNull)

  protected common_* = id ~ livemode ~ created ~ createdBy 
}

然后你可以这样做:

object AsTable extends Table[(String,Boolean,DateTime,Account,String)]("a_table") 
    with CommonFields { 
  def foo = column[String]("foo", O.NotNull)
  def * = common_* ~ foo
} 

你现在唯一需要重复的是元素的类型。

<强>更新

如果你想做对象映射并且:

  1. 您映射到案例类
  2. 案例类中的字段顺序相同
  3. 只是做:

    case class A(
        id: String,
        livemode: Boolean,
        created: DateTime,
        createdBy: Account,
        foo: String)
    
    object AsTable extends Table[A]("a_table") with CommonFields { 
      def foo = column[String]("foo", O.NotNull)
      def * = common_* ~ foo <> (A.apply _, A.unapply _)
    }
    

    这似乎是最经济的解决方案(而不是尝试在*中定义CommonFields并添加类型参数)。但是,如果字段发生更改,则需要更改所有案例类。

    我们可以尝试通过在案例类上使用组合来缓解这种情况:

    case class Common(
        id: String,
        livemode: Boolean,
        created: DateTime,
        createdBy: Account)
    
    case class A(
        common: Common,
        foo: String)
    

    但是,在构造映射器函数时,我们(某处)最终必须转换形式的元组:

    (CT_1, CT_2, ... CT_N, ST_1, ST_2, ..., ST_M)
    

    CT普通类型(CommonFields中已知)
    ST特定类型(在AsTable中已知)

    要:

    (CT_1, CT_2, ... CT_N), (ST_1, ST_2, ..., ST_M)
    

    为了将它们传递给子程序,将CommonA分别转换为元组。

    我们必须在不知道CT(在AsTable中实施)或ST(在CommonFields中实施时)的数量或确切类型的情况下执行此操作。 Scala标准库中的元组无法执行此操作。您需要使用HLists作为shapeless提供的示例来执行此操作。

    这是否值得努力值得怀疑。

    基本轮廓可能看起来像这样(没有所需的所有隐含的混乱)。这段代码不会像这样编译。

    trait CommonFields { this: Table[_] =>
      // like before
    
      type ElList = String :: Boolean :: DateTime :: Account :: HNil
    
      protected def toCommon(els: ElList) = Common.apply.tupled(els.tupled)
      protected def fromCommon(c: Common) = HList(Common.unapply(c))
    }
    
    object AsTable extends Table[A] with CommonFields {
      def foo = column[String]("foo", O.NotNull)
    
      def * = common_* ~ foo <> (x => toA(HList(x)), x => fromA(x) tupled)
    
      // convert HList to A
      protected def toA[L <: HList](els: L) = {
        // Values for Common
        val c_els = els.take[Length[ElList]]
        // Values for A
        val a_els = toCommon(c_els) :: els.drop[Length[ElList]]
    
        A.apply.tupled(a_els.tupled)
      }
    
      // convert A to HList
      protected def fromA(a: A) =
        fromCommon(a.common) :: HList(A.unapply(a)).drop[One]
    
    }
    

    使用更多类型的魔法你可以解决最后两个问题:

    1. toAfromA放入基本特征(通过使用特征中的类型参数,或使用抽象类型成员)
    2. 避免使用this technique
    3. ElList中提取Common.apply,明确定义{{1}}