From 95fd386c1444c24676a55baf55d316299a49a530 Mon Sep 17 00:00:00 2001 From: Eike Kettner Date: Sun, 3 Jan 2021 12:27:56 +0100 Subject: [PATCH] Fixing find-by-checksum with exclusions The NOT-IN query must check for null separately, as everything with null evaluates to false in sql resulting in not finding existing duplicates. --- .../scala/docspell/store/queries/QItem.scala | 53 ++++++++++--------- 1 file changed, 28 insertions(+), 25 deletions(-) diff --git a/modules/store/src/main/scala/docspell/store/queries/QItem.scala b/modules/store/src/main/scala/docspell/store/queries/QItem.scala index 80c7bf2d..b4ca2472 100644 --- a/modules/store/src/main/scala/docspell/store/queries/QItem.scala +++ b/modules/store/src/main/scala/docspell/store/queries/QItem.scala @@ -481,31 +481,34 @@ object QItem { collective: Ident, excludeFileMeta: Set[Ident] ): ConnectionIO[Vector[RItem]] = { - val m1 = RFileMeta.as("m1") - val m2 = RFileMeta.as("m2") - val m3 = RFileMeta.as("m3") - val i = RItem.as("i") - val a = RAttachment.as("a") - val s = RAttachmentSource.as("s") - val r = RAttachmentArchive.as("r") - - Select( - select(i.all), - from(i) - .innerJoin(a, a.itemId === i.id) - .innerJoin(s, s.id === a.id) - .innerJoin(m1, m1.id === a.fileId) - .innerJoin(m2, m2.id === s.fileId) - .leftJoin(r, r.id === a.id) - .leftJoin(m3, m3.id === r.fileId), - where( - i.cid === collective && - (m1.checksum === checksum || m2.checksum === checksum || m3.checksum === checksum) &&? - Nel - .fromList(excludeFileMeta.toList) - .map(excl => m1.id.notIn(excl) && m2.id.notIn(excl) && m3.id.notIn(excl)) - ) - ).distinct.build.query[RItem].to[Vector] + val m1 = RFileMeta.as("m1") + val m2 = RFileMeta.as("m2") + val m3 = RFileMeta.as("m3") + val i = RItem.as("i") + val a = RAttachment.as("a") + val s = RAttachmentSource.as("s") + val r = RAttachmentArchive.as("r") + val fms = Nel.of(m1, m2, m3) + val qq = + Select( + select(i.all), + from(i) + .innerJoin(a, a.itemId === i.id) + .innerJoin(s, s.id === a.id) + .innerJoin(m1, m1.id === a.fileId) + .innerJoin(m2, m2.id === s.fileId) + .leftJoin(r, r.id === a.id) + .leftJoin(m3, m3.id === r.fileId), + where( + i.cid === collective && + Condition.Or(fms.map(m => m.checksum === checksum)) &&? + Nel + .fromList(excludeFileMeta.toList) + .map(excl => Condition.And(fms.map(m => m.id.isNull || m.id.notIn(excl)))) + ) + ).distinct.build + logger.debug(s"FindByChecksum: $qq") + qq.query[RItem].to[Vector] } final case class NameAndNotes(