在 Slick 中使用 DatabaseConfig 和 Database 有什么区别? [英] What's the difference between using DatabaseConfig and Database in Slick?

查看:31
本文介绍了在 Slick 中使用 DatabaseConfig 和 Database 有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 slick 中阅读了有关 DatabaseConfig 的内容文档:

I was reading about DatabaseConfig in slick's documentation:

Database 的配置语法之上,还有一个DatabaseConfig 形式的层,它允许您配置一个光滑的驱动程序加上一个匹配的Database.这样可以很容易地通过简单地改变一个数据库系统来抽象不同类型的数据库系统配置文件.

On top of the configuration syntax for Database, there is another layer in the form of DatabaseConfig which allows you to configure a Slick driver plus a matching Database together. This makes it easy to abstract over different kinds of database systems by simply changing a configuration file.

我不明白这部分,DatabaseConfig 如何使底层数据库系统比 Database 方法更抽象?假设,我在以下测试中使用 DatabaseConfig:

I don't get this part, how DatabaseConfig makes the underlying database system more abstract than the Database approach? Suppose, i'm using DatabaseConfig in the following test:

import org.scalatest.{Matchers, FlatSpec}
import slick.backend.DatabaseConfig
import slick.driver.JdbcProfile
import slick.driver.PostgresDriver.api._

import scala.concurrent.ExecutionContext.Implicits.global

class DatabaseConfigTest extends FlatSpec with Matchers {
  def withDb(test: DatabaseConfig[JdbcProfile] => Any) = {
    val dbConfig = DatabaseConfig.forConfig[JdbcProfile]("abstract")

    try test(dbConfig)
    finally dbConfig.db.close()
  }

  "DatabaseConfig" should "work" in withDb { dbConfig =>
    import Supplier._

    val cities = suppliers.map(_.city)

    dbConfig.db.run(cities.result).map(_.foreach(println))
  }
}

如您所见,如果我将底层数据库系统从 PostgreSQL 更改为 MySQL,除了配置更改之外,我还需要更改 import 将 postgre API 导入到 mysql 的语句.另一方面,如果我使用 Database:

As you can see, if i change my underlying database system from PostgreSQL to MySQL, in addition to configuration change, i need to change the import statement that imports the postgre API to mysql's. On the other hand, If i was using Database:

import org.scalatest.{FlatSpec, Matchers}
import slick.driver.PostgresDriver.api._
import slick.jdbc.JdbcBackend.Database

import scala.concurrent.ExecutionContext.Implicits.global

class DatabaseTest extends FlatSpec with Matchers {
  def withDb(test: Database => Any) = {
    val db = Database.forConfig("default")

    try test(db)
    finally db.close()
  }

  "Supplier names" should "be fetched" in withDb { db =>
    import Supplier._

    val names = suppliers.map(_.name)

    db.run(names.result).map(_.foreach(println))
  }
}

当我使用 Database 时,对底层数据库的相同更改将导致两个更改:一个在配置文件中,另一个在源代码中.说了这么多,怎么一种方法比另一种方法更抽象呢?我使用 DatabaseConfig 错了吗?

When i'm using Database, same change on the underlying database, would result in two changes: one in configuration file and the other in source code. With all these being said, how one approach is more abstract than the other one? Am i using DatabaseConfig wrong?

推荐答案

您已经很接近了,但是您没有完全正确地使用 DatabaseConfig.您需要导入与配置关联的驱动程序,而不是导入特定的驱动程序.这样的事情应该可以工作:

You are close, but you aren't quite using DatabaseConfig properly. Rather than importing a specific driver, you need to import the driver associated with the config. Something like this should work:

import org.scalatest.{Matchers, FlatSpec}
import slick.backend.DatabaseConfig
import slick.jdbc.JdbcProfile
//import slick.driver.PostgresDriver.api._

import scala.concurrent.ExecutionContext.Implicits.global

class DatabaseConfigTest extends FlatSpec with Matchers {
  def withDb(test: DatabaseConfig[JdbcProfile] => Any) = {
    val dbConfig = DatabaseConfig.forConfig[JdbcProfile]("abstract")

    /* The api for the driver specified in the config is imported here. */
    import dbConfig.driver.api._  

    try test(dbConfig)
    finally dbConfig.db.close()
  }

  "DatabaseConfig" should "work" in withDb { dbConfig =>
    import Supplier._

    val cities = suppliers.map(_.city)

    dbConfig.db.run(cities.result).map(_.foreach(println))
  }
}

这应该允许您在配置中切换数据库,而无需更改任何代码或重新编译.

This should allow you to switch databases in the config without having to change any code or recompile.

这篇关于在 Slick 中使用 DatabaseConfig 和 Database 有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆