From f1c4b4adb04d990542d7d2462522ef2ed0bd9f8f Mon Sep 17 00:00:00 2001 From: Eike Kettner Date: Tue, 15 Dec 2020 20:16:19 +0100 Subject: [PATCH] Extract find-item query condition --- .../main/scala/docspell/store/qb/Select.scala | 10 +-- .../scala/docspell/store/queries/QItem.scala | 69 +++++++++---------- 2 files changed, 39 insertions(+), 40 deletions(-) diff --git a/modules/store/src/main/scala/docspell/store/qb/Select.scala b/modules/store/src/main/scala/docspell/store/qb/Select.scala index ca64d218..227c6266 100644 --- a/modules/store/src/main/scala/docspell/store/qb/Select.scala +++ b/modules/store/src/main/scala/docspell/store/qb/Select.scala @@ -170,17 +170,17 @@ object Select { } - case class WithCte(cte: CteBind, ctes: Vector[CteBind], query: Select) extends Select { + case class WithCte(cte: CteBind, ctes: Vector[CteBind], q: Select) extends Select { def appendSelect(e: SelectExpr): WithCte = - copy(query = query.appendSelect(e)) + copy(q = q.appendSelect(e)) def changeFrom(f: FromExpr => FromExpr): WithCte = - copy(query = query.changeFrom(f)) + copy(q = q.changeFrom(f)) def changeWhere(f: Condition => Condition): WithCte = - copy(query = query.changeWhere(f)) + copy(q = q.changeWhere(f)) def orderBy(ob: OrderBy, obs: OrderBy*): WithCte = - copy(query = query.orderBy(ob, obs: _*)) + copy(q = q.orderBy(ob, obs: _*)) } } 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 1d82714d..456fc69f 100644 --- a/modules/store/src/main/scala/docspell/store/queries/QItem.scala +++ b/modules/store/src/main/scala/docspell/store/queries/QItem.scala @@ -164,7 +164,7 @@ object QItem { i.cid === q.account.collective, GroupBy(a.itemId) ), - Attachs.aliasName, //alias, todo improve dsl + Attachs.aliasName, Attachs.itemId === i.id ) .leftJoin(pers0, pers0.pid === i.corrPerson && pers0.cid === coll) @@ -189,46 +189,45 @@ object QItem { } } + def queryCondition(q: Query): Condition = + Condition.unit &&? + q.direction.map(d => i.incoming === d) &&? + q.name.map(n => i.name.like(QueryWildcard.lower(n))) &&? + q.allNames + .map(QueryWildcard.lower) + .map(n => + org.name.like(n) || + pers0.name.like(n) || + pers1.name.like(n) || + equip.name.like(n) || + i.name.like(n) || + i.notes.like(n) + ) &&? + q.corrPerson.map(p => pers0.pid === p) &&? + q.corrOrg.map(o => org.oid === o) &&? + q.concPerson.map(p => pers1.pid === p) &&? + q.concEquip.map(e => equip.eid === e) &&? + q.folder.map(fid => f.id === fid) &&? + q.dateFrom.map(d => coalesce(i.itemDate.s, i.created.s) >= d) &&? + q.dateTo.map(d => coalesce(i.itemDate.s, i.created.s) <= d) &&? + q.dueDateFrom.map(d => i.dueDate > d) &&? + q.dueDateTo.map(d => i.dueDate < d) &&? + q.source.map(n => i.source.like(QueryWildcard.lower(n))) &&? + q.itemIds.flatMap(s => Nel.fromList(s.toList)).map(nel => i.id.in(nel)) &&? + TagItemName + .itemsWithAllTagAndCategory(q.tagsInclude, q.tagCategoryIncl) + .map(subsel => i.id.in(subsel)) &&? + TagItemName + .itemsWithEitherTagOrCategory(q.tagsExclude, q.tagCategoryExcl) + .map(subsel => i.id.notIn(subsel)) + def findItems( q: Query, maxNoteLen: Int, batch: Batch ): Stream[ConnectionIO, ListItem] = { - val cond: Condition => Condition = - c => - c &&? - q.direction.map(d => i.incoming === d) &&? - q.name.map(n => i.name.like(QueryWildcard.lower(n))) &&? - q.allNames - .map(QueryWildcard.lower) - .map(n => - org.name.like(n) || - pers0.name.like(n) || - pers1.name.like(n) || - equip.name.like(n) || - i.name.like(n) || - i.notes.like(n) - ) &&? - q.corrPerson.map(p => pers0.pid === p) &&? - q.corrOrg.map(o => org.oid === o) &&? - q.concPerson.map(p => pers1.pid === p) &&? - q.concEquip.map(e => equip.eid === e) &&? - q.folder.map(fid => f.id === fid) &&? - q.dateFrom.map(d => coalesce(i.itemDate.s, i.created.s) >= d) &&? - q.dateTo.map(d => coalesce(i.itemDate.s, i.created.s) <= d) &&? - q.dueDateFrom.map(d => i.dueDate > d) &&? - q.dueDateTo.map(d => i.dueDate < d) &&? - q.source.map(n => i.source.like(QueryWildcard.lower(n))) &&? - q.itemIds.flatMap(s => Nel.fromList(s.toList)).map(nel => i.id.in(nel)) &&? - TagItemName - .itemsWithAllTagAndCategory(q.tagsInclude, q.tagCategoryIncl) - .map(subsel => i.id.in(subsel)) &&? - TagItemName - .itemsWithEitherTagOrCategory(q.tagsExclude, q.tagCategoryExcl) - .map(subsel => i.id.notIn(subsel)) - val sql = findItemsBase(q, maxNoteLen) - .changeWhere(cond) + .changeWhere(c => c && queryCondition(q)) .limit(batch) .build logger.trace(s"List $batch items: $sql")