Convert RSentMail

This commit is contained in:
Eike Kettner 2020-12-10 21:12:00 +01:00
parent 5cbf0d5602
commit fe4815c737
3 changed files with 60 additions and 50 deletions

View File

@ -21,7 +21,8 @@ object QMails {
def findMail(coll: Ident, mailId: Ident): ConnectionIO[Option[(RSentMail, Ident)]] = { def findMail(coll: Ident, mailId: Ident): ConnectionIO[Option[(RSentMail, Ident)]] = {
val iColl = RItem.Columns.cid.prefix("i") val iColl = RItem.Columns.cid.prefix("i")
val mId = RSentMail.Columns.id.prefix("m") val smail = RSentMail.as("m")
val mId = smail.id.column
val (cols, from) = partialFind val (cols, from) = partialFind
@ -31,9 +32,11 @@ object QMails {
} }
def findMails(coll: Ident, itemId: Ident): ConnectionIO[Vector[(RSentMail, Ident)]] = { def findMails(coll: Ident, itemId: Ident): ConnectionIO[Vector[(RSentMail, Ident)]] = {
val iColl = RItem.Columns.cid.prefix("i") val smailitem = RSentMailItem.as("t")
val tItem = RSentMailItem.Columns.itemId.prefix("t") val smail = RSentMail.as("m")
val mCreated = RSentMail.Columns.created.prefix("m") val iColl = RItem.Columns.cid.prefix("i")
val tItem = smailitem.itemId.column
val mCreated = smail.created.column
val (cols, from) = partialFind val (cols, from) = partialFind
@ -45,16 +48,18 @@ object QMails {
} }
private def partialFind: (Seq[Column], Fragment) = { private def partialFind: (Seq[Column], Fragment) = {
val user = RUser.as("u") val user = RUser.as("u")
val iId = RItem.Columns.id.prefix("i") val smailitem = RSentMailItem.as("t")
val tItem = RSentMailItem.Columns.itemId.prefix("t") val smail = RSentMail.as("m")
val tMail = RSentMailItem.Columns.sentMailId.prefix("t") val iId = RItem.Columns.id.prefix("i")
val mId = RSentMail.Columns.id.prefix("m") val tItem = smailitem.itemId.column
val mUser = RSentMail.Columns.uid.prefix("m") val tMail = smailitem.sentMailId.column
val mId = smail.id.column
val mUser = smail.uid.column
val cols = RSentMail.Columns.all.map(_.prefix("m")) :+ user.login.column val cols = smail.all.map(_.column) :+ user.login.column
val from = RSentMail.table ++ fr"m INNER JOIN" ++ val from = Fragment.const(smail.tableName) ++ fr"m INNER JOIN" ++
RSentMailItem.table ++ fr"t ON" ++ tMail.is(mId) ++ Fragment.const(smailitem.tableName) ++ fr"t ON" ++ tMail.is(mId) ++
fr"INNER JOIN" ++ RItem.table ++ fr"i ON" ++ tItem.is(iId) ++ fr"INNER JOIN" ++ RItem.table ++ fr"i ON" ++ tItem.is(iId) ++
fr"INNER JOIN" ++ Fragment.const(user.tableName) ++ fr"u ON" ++ user.uid.column.is( fr"INNER JOIN" ++ Fragment.const(user.tableName) ++ fr"u ON" ++ user.uid.column.is(
mUser mUser

View File

@ -7,8 +7,8 @@ import cats.implicits._
import fs2.Stream import fs2.Stream
import docspell.common._ import docspell.common._
import docspell.store.impl.Column import docspell.store.qb.DSL._
import docspell.store.impl.Implicits._ import docspell.store.qb._
import doobie._ import doobie._
import doobie.implicits._ import doobie.implicits._
@ -78,18 +78,19 @@ object RSentMail {
si <- OptionT.liftF(RSentMailItem[ConnectionIO](itemId, sm.id, Some(sm.created))) si <- OptionT.liftF(RSentMailItem[ConnectionIO](itemId, sm.id, Some(sm.created)))
} yield (sm, si) } yield (sm, si)
val table = fr"sentmail" final case class Table(alias: Option[String]) extends TableDef {
object Columns { val tableName = "sentmail"
val id = Column("id")
val uid = Column("uid") val id = Column[Ident]("id", this)
val messageId = Column("message_id") val uid = Column[Ident]("uid", this)
val sender = Column("sender") val messageId = Column[String]("message_id", this)
val connName = Column("conn_name") val sender = Column[MailAddress]("sender", this)
val subject = Column("subject") val connName = Column[Ident]("conn_name", this)
val recipients = Column("recipients") val subject = Column[String]("subject", this)
val body = Column("body") val recipients = Column[List[MailAddress]]("recipients", this)
val created = Column("created") val body = Column[String]("body", this)
val created = Column[Timestamp]("created", this)
val all = List( val all = List(
id, id,
@ -104,27 +105,29 @@ object RSentMail {
) )
} }
import Columns._ private val T = Table(None)
def as(alias: String): Table =
Table(Some(alias))
def insert(v: RSentMail): ConnectionIO[Int] = def insert(v: RSentMail): ConnectionIO[Int] =
insertRow( DML.insert(
table, T,
all, T.all,
sql"${v.id},${v.uid},${v.messageId},${v.sender},${v.connName},${v.subject},${v.recipients},${v.body},${v.created}" sql"${v.id},${v.uid},${v.messageId},${v.sender},${v.connName},${v.subject},${v.recipients},${v.body},${v.created}"
).update.run )
def findByUser(userId: Ident): Stream[ConnectionIO, RSentMail] = def findByUser(userId: Ident): Stream[ConnectionIO, RSentMail] =
selectSimple(all, table, uid.is(userId)).query[RSentMail].stream run(select(T.all), from(T), T.uid === userId).query[RSentMail].stream
def delete(mailId: Ident): ConnectionIO[Int] = def delete(mailId: Ident): ConnectionIO[Int] =
deleteFrom(table, id.is(mailId)).update.run DML.delete(T, T.id === mailId)
def deleteByItem(item: Ident): ConnectionIO[Int] = def deleteByItem(item: Ident): ConnectionIO[Int] =
for { for {
list <- RSentMailItem.findSentMailIdsByItem(item) list <- RSentMailItem.findSentMailIdsByItem(item)
n1 <- RSentMailItem.deleteAllByItem(item) n1 <- RSentMailItem.deleteAllByItem(item)
n0 <- NonEmptyList.fromList(list.toList) match { n0 <- NonEmptyList.fromList(list.toList) match {
case Some(nel) => deleteFrom(table, id.isIn(nel)).update.run case Some(nel) => DML.delete(T, T.id.in(nel))
case None => 0.pure[ConnectionIO] case None => 0.pure[ConnectionIO]
} }
} yield n0 + n1 } yield n0 + n1

View File

@ -4,8 +4,8 @@ import cats.effect._
import cats.implicits._ import cats.implicits._
import docspell.common._ import docspell.common._
import docspell.store.impl.Column import docspell.store.qb.DSL._
import docspell.store.impl.Implicits._ import docspell.store.qb._
import doobie._ import doobie._
import doobie.implicits._ import doobie.implicits._
@ -29,13 +29,13 @@ object RSentMailItem {
now <- created.map(_.pure[F]).getOrElse(Timestamp.current[F]) now <- created.map(_.pure[F]).getOrElse(Timestamp.current[F])
} yield RSentMailItem(id, itemId, sentmailId, now) } yield RSentMailItem(id, itemId, sentmailId, now)
val table = fr"sentmailitem" final case class Table(alias: Option[String]) extends TableDef {
val tableName = "sentmailitem"
object Columns { val id = Column[Ident]("id", this)
val id = Column("id") val itemId = Column[Ident]("item_id", this)
val itemId = Column("item_id") val sentMailId = Column[Ident]("sentmail_id", this)
val sentMailId = Column("sentmail_id") val created = Column[Timestamp]("created", this)
val created = Column("created")
val all = List( val all = List(
id, id,
@ -45,21 +45,23 @@ object RSentMailItem {
) )
} }
import Columns._ private val T = Table(None)
def as(alias: String): Table =
Table(Some(alias))
def insert(v: RSentMailItem): ConnectionIO[Int] = def insert(v: RSentMailItem): ConnectionIO[Int] =
insertRow( DML.insert(
table, T,
all, T.all,
sql"${v.id},${v.itemId},${v.sentMailId},${v.created}" sql"${v.id},${v.itemId},${v.sentMailId},${v.created}"
).update.run )
def deleteMail(mailId: Ident): ConnectionIO[Int] = def deleteMail(mailId: Ident): ConnectionIO[Int] =
deleteFrom(table, sentMailId.is(mailId)).update.run DML.delete(T, T.sentMailId === mailId)
def findSentMailIdsByItem(item: Ident): ConnectionIO[Set[Ident]] = def findSentMailIdsByItem(item: Ident): ConnectionIO[Set[Ident]] =
selectSimple(Seq(sentMailId), table, itemId.is(item)).query[Ident].to[Set] run(select(Seq(T.sentMailId)), from(T), T.itemId === item).query[Ident].to[Set]
def deleteAllByItem(item: Ident): ConnectionIO[Int] = def deleteAllByItem(item: Ident): ConnectionIO[Int] =
deleteFrom(table, itemId.is(item)).update.run DML.delete(T, T.itemId === item)
} }