我是Scala
和Slick
的新手。我正在尝试按对象对分组进行查询,并为每个对象创建两个序列。
这是我的代码:
val firstQuery = {
for {
table1Id <- Table1.filter(_.someid === someid).map(_.someid)
table2 <- Table2.filter(_.someid === table1Id)
table3 <- Table3.filter(_.someId === someid)
_ <- {
Table4.filter(table4 => table4.someid === table3.someid &&
table4.name === "$$$")
}
table5 <- Table5.filter(_.someid === table2.someid)
} yield {
(table2, table3, table5)
}
}
val finalQuery = {
for {
(table6, firstQueryTuple) <- {
Table6 joinRight firstQuery on
((firstTable, secondTable) => firstTable.someid === secondTable._1.someid)
}
} yield {
(table6, firstQueryTuple._1, firstQueryTuple._2, firstQueryTuple._3)
}
}
dataBaseConnection.run(finalQuery.result).map { resultSequence =>
val grouping = resultSequence.groupBy { case (table6, table2, _, _) => (table6, table2) }
val groupingFiltered = grouping.map { case (table6table2tuple, sequence) => (table6table2tuple, sequence.map(_._4) (which is Table5), sequence.map(_._3) (which is table3)) }.toSeq
groupingFiltered match {
case tupleQuery if tupleQuery.nonEmpty => Success(tupleQuery.map(x => (x._1._2, x._1._1, x._2, x._3) ))
case _ => Failure(NonEmptyList(DBMissingRecordProblem(s"Error")))
}
}
现在我需要达到的结果是
Seq[(Table2, Option[Table6], Seq[Table5], Seq[Table3])]
有没有一种方法可以分组并实现两个序列?我知道可以完成一个序列,我的意思是按对象分组并实现(object, Seq[anotherObject])
如果可以的话,请帮助我。