Commit 1cf01f63 authored by Peter van 't Hof's avatar Peter van 't Hof

Fixed filters to support update and delete statements

parent 9cd4ef52
......@@ -164,14 +164,12 @@ class SummaryDb(db: Database) extends Closeable {
private def statsFilter(runId: Option[Int] = None, pipelineId: Option[Int] = None, moduleId: Option[Option[Int]] = None,
sampleId: Option[Option[Int]] = None, libId: Option[Option[Int]] = None) = {
val l: List[Option[Query[Stats, Stats#TableElementType, Seq] => Query[Stats, Stats#TableElementType, Seq]]] = List(
runId.map(x => y => y.filter(_.runId === x)),
pipelineId.map(x => y => y.filter(_.pipelineId === x)),
moduleId.map(x => y => (if (x.isDefined) y.filter(_.moduleId === x) else y.filter(_.moduleId.isEmpty))),
sampleId.map(x => y => (if (x.isDefined) y.filter(_.sampleId === x) else y.filter(_.sampleId.isEmpty))),
libId.map(x => y => (if (x.isDefined) y.filter(_.libraryId === x) else y.filter(_.libraryId.isEmpty)))
)
l.flatten.foldLeft(stats.subquery)((a, b) => b(a))
var f = stats.filter(_.runId === runId)
pipelineId.foreach(r => f = f.filter(_.pipelineId === r))
moduleId.foreach(r => f = (if (r.isDefined) f.filter(_.moduleId === r.get) else f.filter(_.moduleId.isEmpty)))
sampleId.foreach(r => f = (if (r.isDefined) f.filter(_.sampleId === r.get) else f.filter(_.sampleId.isEmpty)))
libId.foreach(r => f = (if (r.isDefined) f.filter(_.libraryId === r.get) else f.filter(_.libraryId.isEmpty)))
f
}
def getStats(runId: Option[Int] = None, pipelineId: Option[Int] = None, moduleId: Option[Option[Int]] = None,
......@@ -200,14 +198,11 @@ class SummaryDb(db: Database) extends Closeable {
def settingsFilter(runId: Option[Int] = None, pipelineId: Option[Int] = None, moduleId: Option[Option[Int]] = None,
sampleId: Option[Option[Int]] = None, libId: Option[Option[Int]] = None) = {
val l: List[Option[Query[Settings, Settings#TableElementType, Seq] => Query[Settings, Settings#TableElementType, Seq]]] = List(
runId.map(x => y => y.filter(_.runId === x)),
pipelineId.map(x => y => y.filter(_.pipelineId === x)),
moduleId.map(x => y => (if (x.isDefined) y.filter(_.moduleId === x) else y.filter(_.moduleId.isEmpty))),
sampleId.map(x => y => (if (x.isDefined) y.filter(_.sampleId === x) else y.filter(_.sampleId.isEmpty))),
libId.map(x => y => (if (x.isDefined) y.filter(_.libraryId === x) else y.filter(_.libraryId.isEmpty)))
)
l.flatten.foldLeft(settings.subquery)((a, b) => b(a))
var f = settings.filter(_.runId === runId).filter(_.pipelineId === pipelineId)
moduleId.foreach(r => f = (if (r.isDefined) f.filter(_.moduleId === r.get) else f.filter(_.moduleId.isEmpty)))
sampleId.foreach(r => f = (if (r.isDefined) f.filter(_.sampleId === r.get) else f.filter(_.sampleId.isEmpty)))
libId.foreach(r => f = (if (r.isDefined) f.filter(_.libraryId === r.get) else f.filter(_.libraryId.isEmpty)))
f
}
def createOrUpdateSetting(runId: Int, pipelineId: Int, moduleId: Option[Int] = None,
......@@ -215,7 +210,7 @@ class SummaryDb(db: Database) extends Closeable {
val filter = settingsFilter(Some(runId), Some(pipelineId), Some(moduleId), Some(sampleId), Some(libId))
val r = Await.result(db.run(filter.size.result), Duration.Inf)
if (r == 0) createSetting(runId, pipelineId, moduleId, sampleId, libId, content)
else db.run(filter.map(_.content).update(content))
else db.run(filter.update(Setting(runId, pipelineId, moduleId, sampleId, libId, content)))
}
def getSettings(runId: Option[Int] = None, pipelineId: Option[Int] = None, moduleId: Option[Option[Int]] = None,
......@@ -240,15 +235,11 @@ class SummaryDb(db: Database) extends Closeable {
def filesFilter(runId: Option[Int] = None, pipelineId: Option[Int] = None, moduleId: Option[Option[Int]],
sampleId: Option[Option[Int]] = None, libId: Option[Option[Int]] = None,
key: Option[String] = None) = {
val l: List[Option[Query[Files, Files#TableElementType, Seq] => Query[Files, Files#TableElementType, Seq]]] = List(
runId.map(x => y => y.filter(_.runId === x)),
pipelineId.map(x => y => y.filter(_.pipelineId === x)),
moduleId.map(x => y => (if (x.isDefined) y.filter(_.moduleId === x) else y.filter(_.moduleId.isEmpty))),
sampleId.map(x => y => (if (x.isDefined) y.filter(_.sampleId === x) else y.filter(_.sampleId.isEmpty))),
libId.map(x => y => (if (x.isDefined) y.filter(_.libraryId === x) else y.filter(_.libraryId.isEmpty))),
key.map(x => y => y.filter(_.key === x))
)
l.flatten.foldLeft(files.subquery)((a, b) => b(a))
var f = files.filter(_.runId === runId).filter(_.pipelineId === pipelineId).filter(_.key === key)
moduleId.foreach(r => f = (if (r.isDefined) f.filter(_.moduleId === r.get) else f.filter(_.moduleId.isEmpty)))
sampleId.foreach(r => f = (if (r.isDefined) f.filter(_.sampleId === r.get) else f.filter(_.sampleId.isEmpty)))
libId.foreach(r => f = (if (r.isDefined) f.filter(_.libraryId === r.get) else f.filter(_.libraryId.isEmpty)))
f
}
def getFiles(runId: Option[Int] = None, pipelineId: Option[Int] = None, moduleId: Option[Option[Int]],
......@@ -273,14 +264,10 @@ class SummaryDb(db: Database) extends Closeable {
}
def executablesFilter(runId: Option[Int], toolName: Option[String]) = {
val l: List[Option[Query[Executables, Executables#TableElementType, Seq] => Query[Executables, Executables#TableElementType, Seq]]] = List(
runId.map(x => y => y.filter(_.runId === x)),
toolName.map(x => y => y.filter(_.toolName === x))
)
l.flatten.foldLeft(executables.subquery)((a, b) => b(a))
executables.filter(_.runId === runId).filter(_.toolName === toolName)
}
def getFiles(runId: Option[Int], toolName: Option[String]) = {
def getExecutables(runId: Option[Int], toolName: Option[String]) = {
db.run(executablesFilter(runId, toolName).result)
}
......
package nl.lumc.sasc.biopet.utils.summary.db
import java.io.File
import org.scalatest.Matchers
import org.scalatest.testng.TestNGSuite
import org.testng.annotations.Test
import scala.concurrent.Await
import scala.concurrent.duration.Duration
/**
* Created by pjvanthof on 14/02/2017.
*/
class SummaryDbTest extends TestNGSuite with Matchers {
@Test
def testSettings: Unit = {
val dbFile = File.createTempFile("summary.", ".db")
dbFile.deleteOnExit()
val db = SummaryDb.openSqliteSummary(dbFile)
db.createTables()
Await.result(db.createOrUpdateSetting(0, 0, None, None, None, """{"content": "test" }"""), Duration.Inf)
val bla = Await.result(db.getSettings(Some(0)), Duration.Inf)
Await.result(db.getSetting(0,0, None, None, None), Duration.Inf) shouldBe Some(Map("content" -> "test"))
Await.result(db.createOrUpdateSetting(0, 0, None, None, None, """{"content": "test2" }"""), Duration.Inf)
Await.result(db.getSetting(0,0, None, None, None), Duration.Inf) shouldBe Some(Map("content" -> "test2"))
}
@Test
def testStats: Unit = {
val dbFile = File.createTempFile("summary.", ".db")
dbFile.deleteOnExit()
val db = SummaryDb.openSqliteSummary(dbFile)
db.createTables()
Await.result(db.createOrUpdateStat(0, 0, None, None, None, """{"content": "test" }"""), Duration.Inf)
Await.result(db.getStat(0, 0, None, None, None), Duration.Inf) shouldBe Some(Map("content" -> "test"))
Await.result(db.createOrUpdateStat(0, 0, None, None, None, """{"content": "test2" }"""), Duration.Inf)
Await.result(db.getStat(0, 0, None, None, None), Duration.Inf) shouldBe Some(Map("content" -> "test2"))
}
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment