Extract find-item query condition

This commit is contained in:
Eike Kettner 2020-12-15 20:16:19 +01:00
parent 2dff686fa0
commit f1c4b4adb0
2 changed files with 39 additions and 40 deletions

View File

@ -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 = def appendSelect(e: SelectExpr): WithCte =
copy(query = query.appendSelect(e)) copy(q = q.appendSelect(e))
def changeFrom(f: FromExpr => FromExpr): WithCte = def changeFrom(f: FromExpr => FromExpr): WithCte =
copy(query = query.changeFrom(f)) copy(q = q.changeFrom(f))
def changeWhere(f: Condition => Condition): WithCte = def changeWhere(f: Condition => Condition): WithCte =
copy(query = query.changeWhere(f)) copy(q = q.changeWhere(f))
def orderBy(ob: OrderBy, obs: OrderBy*): WithCte = def orderBy(ob: OrderBy, obs: OrderBy*): WithCte =
copy(query = query.orderBy(ob, obs: _*)) copy(q = q.orderBy(ob, obs: _*))
} }
} }

View File

@ -164,7 +164,7 @@ object QItem {
i.cid === q.account.collective, i.cid === q.account.collective,
GroupBy(a.itemId) GroupBy(a.itemId)
), ),
Attachs.aliasName, //alias, todo improve dsl Attachs.aliasName,
Attachs.itemId === i.id Attachs.itemId === i.id
) )
.leftJoin(pers0, pers0.pid === i.corrPerson && pers0.cid === coll) .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( def findItems(
q: Query, q: Query,
maxNoteLen: Int, maxNoteLen: Int,
batch: Batch batch: Batch
): Stream[ConnectionIO, ListItem] = { ): 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) val sql = findItemsBase(q, maxNoteLen)
.changeWhere(cond) .changeWhere(c => c && queryCondition(q))
.limit(batch) .limit(batch)
.build .build
logger.trace(s"List $batch items: $sql") logger.trace(s"List $batch items: $sql")