scalaquery中的不同开发/生产数据库

时间:2012-11-17 02:38:35

标签: database scala scalaquery

ScalaQuery要求(AFAIK)在代码中使用特定于提供者的导入,例如:

import org.scalaquery.ql.extended.H2Driver.Implicit._

我们正在尝试在开发模式中使用H2,在生产中使用MySQL。有没有办法实现这个目标?

2 个答案:

答案 0 :(得分:2)

我的方法是:

class Subscribers(database: Database)(profile: ExtendedProfile) {
    import profile.Implicit._
}

订户基本上是我的数据访问对象。 不确定这是最好的方法。它解决了我的问题。

您可以创建如下的DAO:

...在生产代码中:

new Subscribers(database)(MySQLDriver)

...并且在测试代码中:

new Subscribers(database)(H2Driver)

答案 1 :(得分:1)

我在play框架中使用以下内容

object test {
  lazy val extendedProfile = {
    val extendedProfileName = Play.configuration getString "db.default.extendedProfile" get
    companionObjectNamed(extendedProfileName).asInstanceOf[ExtendedProfile]
  }

  def companionObjectNamed(name: String) : AnyRef = {
    val c = Class forName (name + "$")
    c.getField("MODULE$") get c
  }
}

然后导入

import util.extendedProfile.Implicit._

org.scalaquery.ql.extended.MySQLDriver是我在配置中用来使mysql工作的字符串。