Scala Slick:如何进行除联接(左外部联接为null)?

时间:2019-04-11 20:09:47

标签: sql postgresql scala slick

我正在使用scala 2.11,slick 2.1.0和postgres 9.6。无法进行主要版本升级。

我有2个相同的表(从相同的模型创建),并且我想做一个除外联接(左侧外部联接的值为null): except join

我的模型如下:

trait Coffee {
  val coffeeLoversId:         Option[Int]
  val taxId:                  Option[Long]
  val internationalCoffeeId:  String
  val providerId:             String
}

class Coffees(tag: Tag, schema: String) extends Table[Coffee](tag, Some(schema), "coffees")

  val coffeeLoversId: Column[Option[Int]] = column[Option[Int]]("coffee_lovers_id")
  val taxId: Column[Option[Long]] = column[Option[Long]]("tax_id")
  val internationalCoffeeId: Column[String] = column[String]("international_coffee_id", O.DBType("VARCHAR"))
  val providerId: Column[String] = column[String]("provider_id", O.DBType("VARCHAR"))

  def * = (coffeeLoversId, taxId, internationalCoffeeId, providerId) <> (Coffee.tupled, Coffee.unapply)

  def ? = (coffeeLoversId, taxId, internationalCoffeeId.?, providerId.?).shaped.<>({r=>import r._; _1.map(_=> Coffee.tupled((_1, _2, _3.get, _4.get)))}, (_:Any) =>  throw new Exception("Inserting into ? projection not supported."))
}


object Coffees {
  def tableQuery(implicit schema: TableSchema) = TableQuery[Coffees]{ tag : Tag => new Coffees(tag, schema.name) }
}

我要运行的SQL查询是:

SELECT * from previous.coffees PRV 
LEFT JOIN current.coffees CUR 
ON PRV.international_coffee_id = CUR.international_coffee_id 
WHERE PRV.international_coffee_id IS NULL;

我能够做到:

for {
  (c,s) <- Coffees.tableQuery(previousSchema).leftJoin(
    Coffees.tableQuery(currentSchema)
    ) on((x,y) => x.internationalCoffeeId === y.internationalCoffeeId)
} yield(c)

这似乎给了我一个左外部联接,但是我在为null服务器端进行过滤时遇到了麻烦(添加WHERE PRV.international_coffee_id IS NULL

如果有任何建议或想法,我将不胜感激。

1 个答案:

答案 0 :(得分:0)

我能够实现自己的目标。首先,我尝试使用scala的条件理解:

for {
  (c,s) <- Coffees.tableQuery(previousSchema).leftJoin(
    Coffees.tableQuery(currentSchema)
    ) on((x,y) => x.internationalCoffeeId === y.internationalCoffeeId)
if(s.internationalCoffeeId.isEmpty)
} yield(c)

这有效,并且在数据库上进行测试时,我获得了预期的结果,因为我知道我是在scala中而不是在数据库服务器上对结果进行过滤。

所以我一直努力,终于到了

for {
  (c,s) <- Coffees.tableQuery(previousSchema).leftJoin(
    Coffees.tableQuery(currentSchema)
    ) on(
    (x,y) => x.internationalCoffeeId === y.internationalCoffeeId
    ).filter(_._2.internationalCoffeeId.isEmpty)
} yield(c)

我还不清楚为什么编译器无法正确选择类型,而我的过滤器中却没有为匿名函数输入参数的类型,但是我直接在元组上进行处理。 我验证了是否可以直接在我的selectStatement(或本例中的Query)上使用CompiledStreamingExecutable获得所需的SQL查询:

val query = Coffees.tableQuery(previousSchema).leftJoin(
    Coffees.tableQuery(currentSchema)
) on(
    (x,y) => x.internationalCoffeeId === y.internationalCoffeeId
).filter(_._2.internationalCoffeeId.isEmpty)
query.selectStatement