Convert rest of QItem

This commit is contained in:
Eike Kettner 2020-12-14 17:22:25 +01:00
parent d1606d6f16
commit 2cecd01837

View File

@ -10,7 +10,6 @@ import fs2.Stream
import docspell.common.syntax.all._ import docspell.common.syntax.all._
import docspell.common.{IdRef, _} import docspell.common.{IdRef, _}
import docspell.store.Store import docspell.store.Store
import docspell.store.impl.DoobieMeta._
import docspell.store.qb._ import docspell.store.qb._
import docspell.store.records._ import docspell.store.records._
@ -21,6 +20,7 @@ import org.log4s._
object QItem { object QItem {
private[this] val logger = getLogger private[this] val logger = getLogger
import docspell.store.qb.DSL._
def moveAttachmentBefore( def moveAttachmentBefore(
itemId: Ident, itemId: Ident,
@ -87,57 +87,30 @@ object QItem {
} }
def findItem(id: Ident): ConnectionIO[Option[ItemData]] = { def findItem(id: Ident): ConnectionIO[Option[ItemData]] = {
import docspell.store.impl.Implicits._
val equip = REquipment.as("e") val equip = REquipment.as("e")
val org = ROrganization.as("o") val org = ROrganization.as("o")
val pers0 = RPerson.as("p0") val pers0 = RPerson.as("p0")
val pers1 = RPerson.as("p1") val pers1 = RPerson.as("p1")
val f = RFolder.as("f") val f = RFolder.as("f")
val i = RItem.as("i")
val IC = RItem.Columns.all.map(_.prefix("i")) val ref = RItem.as("ref")
val OC = org.all.map(_.column).toList
val P0C = pers0.all.map(_.column).toList
val P1C = pers1.all.map(_.column).toList
val EC = equip.all.map(_.oldColumn).map(_.prefix("e")).toList
val ICC = List(RItem.Columns.id, RItem.Columns.name).map(_.prefix("ref"))
val FC = List(f.id.column, f.name.column)
val cq = val cq =
selectSimple( Select(
IC ++ OC ++ P0C ++ P1C ++ EC ++ ICC ++ FC, select(i.all, org.all, pers0.all, pers1.all, equip.all)
RItem.table ++ fr"i", .append(ref.id.s)
Fragment.empty .append(ref.name.s)
) ++ .append(f.id.s)
fr"LEFT JOIN" ++ Fragment.const( .append(f.name.s),
org.tableName from(i)
) ++ fr"o ON" ++ RItem.Columns.corrOrg .leftJoin(org, org.oid === i.corrOrg)
.prefix("i") .leftJoin(pers0, pers0.pid === i.corrPerson)
.is(org.oid.column) ++ .leftJoin(pers1, pers1.pid === i.concPerson)
fr"LEFT JOIN" ++ Fragment.const( .leftJoin(equip, equip.eid === i.concEquipment)
pers0.tableName .leftJoin(ref, ref.id === i.inReplyTo)
) ++ fr"p0 ON" ++ RItem.Columns.corrPerson .leftJoin(f, f.id === i.folder),
.prefix("i") i.id === id
.is(pers0.pid.column) ++ ).build
fr"LEFT JOIN" ++ Fragment.const(
pers1.tableName
) ++ fr"p1 ON" ++ RItem.Columns.concPerson
.prefix("i")
.is(pers1.pid.column) ++
fr"LEFT JOIN" ++ Fragment.const(
equip.tableName
) ++ fr"e ON" ++ RItem.Columns.concEquipment
.prefix("i")
.is(equip.eid.oldColumn.prefix("e")) ++
fr"LEFT JOIN" ++ RItem.table ++ fr"ref ON" ++ RItem.Columns.inReplyTo
.prefix("i")
.is(RItem.Columns.id.prefix("ref")) ++
fr"LEFT JOIN" ++ Fragment.const(
RFolder.T.tableName
) ++ fr"f ON" ++ RItem.Columns.folder
.prefix("i")
.is(f.id.column) ++
fr"WHERE" ++ RItem.Columns.id.prefix("i").is(id)
val q = cq val q = cq
.query[ .query[
@ -152,7 +125,7 @@ object QItem {
) )
] ]
.option .option
logger.trace(s"Find item query: $cq") logger.info(s"Find item query: $cq")
val attachs = RAttachment.findByItemWithMeta(id) val attachs = RAttachment.findByItemWithMeta(id)
val sources = RAttachmentSource.findByItemWithMeta(id) val sources = RAttachmentSource.findByItemWithMeta(id)
val archives = RAttachmentArchive.findByItemWithMeta(id) val archives = RAttachmentArchive.findByItemWithMeta(id)
@ -174,8 +147,6 @@ object QItem {
def findCustomFieldValuesForItem( def findCustomFieldValuesForItem(
itemId: Ident itemId: Ident
): ConnectionIO[Vector[ItemFieldValue]] = { ): ConnectionIO[Vector[ItemFieldValue]] = {
import docspell.store.qb.DSL._
val cf = RCustomField.as("cf") val cf = RCustomField.as("cf")
val cv = RCustomFieldValue.as("cvf") val cv = RCustomFieldValue.as("cvf")
@ -264,8 +235,6 @@ object QItem {
coll: Ident, coll: Ident,
values: Seq[CustomValue] values: Seq[CustomValue]
): Option[Select] = { ): Option[Select] = {
import docspell.store.qb.DSL._
val cf = RCustomField.as("cf") val cf = RCustomField.as("cf")
val cv = RCustomFieldValue.as("cv") val cv = RCustomFieldValue.as("cv")
@ -286,8 +255,6 @@ object QItem {
} }
private def findItemsBase(q: Query, noteMaxLen: Int): Select = { private def findItemsBase(q: Query, noteMaxLen: Int): Select = {
import docspell.store.qb.DSL._
object Attachs extends TableDef { object Attachs extends TableDef {
val tableName = "attachs" val tableName = "attachs"
val aliasName = "cta" val aliasName = "cta"
@ -371,8 +338,6 @@ object QItem {
maxNoteLen: Int, maxNoteLen: Int,
batch: Batch batch: Batch
): Stream[ConnectionIO, ListItem] = { ): Stream[ConnectionIO, ListItem] = {
import docspell.store.qb.DSL._
val equip = REquipment.as("e1") val equip = REquipment.as("e1")
val org = ROrganization.as("o0") val org = ROrganization.as("o0")
val pers0 = RPerson.as("p0") val pers0 = RPerson.as("p0")
@ -426,9 +391,7 @@ object QItem {
q: Query, q: Query,
maxNoteLen: Int, maxNoteLen: Int,
items: Set[SelectedItem] items: Set[SelectedItem]
): Stream[ConnectionIO, ListItem] = { ): Stream[ConnectionIO, ListItem] =
import docspell.store.qb.DSL._
if (items.isEmpty) Stream.empty if (items.isEmpty) Stream.empty
else { else {
val i = RItem.as("i") val i = RItem.as("i")
@ -469,7 +432,6 @@ object QItem {
logger.info(s"fts query: $from") logger.info(s"fts query: $from")
from.query[ListItem].stream from.query[ListItem].stream
} }
}
case class AttachmentLight( case class AttachmentLight(
id: Ident, id: Ident,
@ -527,8 +489,6 @@ object QItem {
} }
private def findAttachmentLight(item: Ident): ConnectionIO[List[AttachmentLight]] = { private def findAttachmentLight(item: Ident): ConnectionIO[List[AttachmentLight]] = {
import docspell.store.qb.DSL._
val a = RAttachment.as("a") val a = RAttachment.as("a")
val m = RAttachmentMeta.as("m") val m = RAttachmentMeta.as("m")
@ -553,8 +513,6 @@ object QItem {
fileMetaIds: Nel[Ident], fileMetaIds: Nel[Ident],
states: Option[Nel[ItemState]] states: Option[Nel[ItemState]]
): Select.SimpleSelect = { ): Select.SimpleSelect = {
import docspell.store.qb.DSL._
val i = RItem.as("i") val i = RItem.as("i")
val a = RAttachment.as("a") val a = RAttachment.as("a")
val s = RAttachmentSource.as("s") val s = RAttachmentSource.as("s")
@ -592,8 +550,6 @@ object QItem {
} }
def findByChecksum(checksum: String, collective: Ident): ConnectionIO[Vector[RItem]] = { def findByChecksum(checksum: String, collective: Ident): ConnectionIO[Vector[RItem]] = {
import docspell.store.qb.DSL._
val m1 = RFileMeta.as("m1") val m1 = RFileMeta.as("m1")
val m2 = RFileMeta.as("m2") val m2 = RFileMeta.as("m2")
val m3 = RFileMeta.as("m3") val m3 = RFileMeta.as("m3")
@ -629,9 +585,6 @@ object QItem {
coll: Option[Ident], coll: Option[Ident],
chunkSize: Int chunkSize: Int
): Stream[ConnectionIO, NameAndNotes] = { ): Stream[ConnectionIO, NameAndNotes] = {
import docspell.store.qb._
import docspell.store.qb.DSL._
val i = RItem.as("i") val i = RItem.as("i")
Select( Select(
@ -647,8 +600,6 @@ object QItem {
collective: Ident, collective: Ident,
chunkSize: Int chunkSize: Int
): Stream[ConnectionIO, Ident] = { ): Stream[ConnectionIO, Ident] = {
import docspell.store.qb.DSL._
val i = RItem.as("i") val i = RItem.as("i")
Select(i.id.s, from(i), i.cid === collective && i.state === ItemState.confirmed) Select(i.id.s, from(i), i.cid === collective && i.state === ItemState.confirmed)
.orderBy(i.created.desc) .orderBy(i.created.desc)
@ -666,8 +617,6 @@ object QItem {
tagCategory: String, tagCategory: String,
pageSep: String pageSep: String
): ConnectionIO[TextAndTag] = { ): ConnectionIO[TextAndTag] = {
import docspell.store.qb.DSL._
val tag = RTag.as("t") val tag = RTag.as("t")
val a = RAttachment.as("a") val a = RAttachment.as("a")
val am = RAttachmentMeta.as("m") val am = RAttachmentMeta.as("m")