Slick 3.0.0 数据库不可知论 [英] Slick 3.0.0 database agnostism

查看:32
本文介绍了Slick 3.0.0 数据库不可知论的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开始使用 Slick 3.0.0,我喜欢它简洁的语法.尽管如此,我还是无法找到一种以与数据库无关的方式使用它的方法.

I started to use Slick 3.0.0 and I like it's succinct syntax. Nevertheless, I wasn't able to find a way to use it in a database agnostic way.

在文档中提供的以下示例中:http://slick.typesafe.com/doc/3.0.0/gettingstarted.html

In the following example provided in the documentation: http://slick.typesafe.com/doc/3.0.0/gettingstarted.html

我希望能够以某种方式解耦所用数据库的这段代码,并避免在我的代码中导入特定于数据库(即 slick.driver.H2Driver.api._).

I'd like to be able to decouple somehow this code of the database used and avoid importing database specific in my code (i.e slick.driver.H2Driver.api._).

我试图通过使用蛋糕模式提供连接来摆脱它,但那时.result"成员不可用.

I tried to get rid of it by providing the connection using the cake pattern, but the ".result" member isn't available then.

一种解决方法是导入 slick.driver.JdbcDriver.api._,但它已被弃用,因此不应该是一个好的起点.

A workaround would be to import slick.driver.JdbcDriver.api._, but it is deprecated and thus should not be a good starting point.

有人找到了一种以数据库无关且优雅的方式使用 Slick 3.0.0 的方法吗?

Anyone found a way to use Slick 3.0.0 in a database agnostic and elegant way?

这个问题与如何编写与数据库无关的 Play 应用程序并执行首次数据库初始化?"不远了,但那个专注于 Slick 3.0.0.遗憾的是,前一个问题提供的答案并非针对 Slick 3.0.0,除了使用不推荐使用的代码.

This question isn't far of "How to write database-agnostic Play application and perform first-time database initialization?", but that one focuses on Slick 3.0.0. Sadely the answers provided with that former question aren't targetting Slick 3.0.0 except one which uses deprecated code.

推荐答案

您正在寻找的 slick 驱动程序类是 slick.driver.JdbcProfile.

The slick driver class you're looking for is slick.driver.JdbcProfile.

官方示例项目 slick-multidb 可以通过激活器获取(github).相关代码如下:

There's an official example project slick-multidb which you can obtain through activator (github). Here's the relevant code:

import scala.language.higherKinds
import slick.driver.JdbcProfile

/** All database code goes into the DAO (data access object) class which
  * is parameterized by a Slick driver that implements JdbcProfile.
  */
class DAO(val driver: JdbcProfile) {
  // Import the Scala API from the driver
  import driver.api._

  class Props(tag: Tag) extends Table[(String, String)](tag, "PROPS") {
    def key = column[String]("KEY", O.PrimaryKey)
    def value = column[String]("VALUE")
    def * = (key, value)
  }
  val props = TableQuery[Props]

  /** Create the database schema */
  def create: DBIO[Unit] =
    props.ddl.create

  /** Insert a key/value pair */
  def insert(k: String, v: String): DBIO[Int] =
    props += (k, v)

  /** Get the value for the given key */
  def get(k: String): DBIO[Option[String]] =
    (for(p <- props if p.key === k) yield p.value).result.headOption

  /** Get the first element for a Query from this DAO */
  def getFirst[M, U, C[_]](q: Query[M, U, C]): DBIO[U] =
    q.result.head
}

客户端代码:

val dao = new DAO(H2Driver)
import dao.driver.api._
db.run(dao.insert("foo", "bar"))

这篇关于Slick 3.0.0 数据库不可知论的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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