Convert more records

This commit is contained in:
Eike Kettner 2020-12-12 12:23:01 +01:00
parent e3f6892abd
commit 87eb8c7f55
2 changed files with 38 additions and 30 deletions

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.Implicits._ import docspell.store.qb.DSL._
import docspell.store.impl._ import docspell.store.qb._
import doobie._ import doobie._
import doobie.implicits._ import doobie.implicits._
@ -30,32 +30,38 @@ object RFtsMigration {
now <- Timestamp.current[F] now <- Timestamp.current[F]
} yield RFtsMigration(newId, version, ftsEngine, description, now) } yield RFtsMigration(newId, version, ftsEngine, description, now)
val table = fr"fts_migration" final case class Table(alias: Option[String]) extends TableDef {
val tableName = "fts_migration"
object Columns { val id = Column[Ident]("id", this)
val id = Column("id") val version = Column[Int]("version", this)
val version = Column("version") val ftsEngine = Column[Ident]("fts_engine", this)
val ftsEngine = Column("fts_engine") val description = Column[String]("description", this)
val description = Column("description") val created = Column[Timestamp]("created", this)
val created = Column("created")
val all = List(id, version, ftsEngine, description, created) val all = List(id, version, ftsEngine, description, created)
} }
import Columns._
val T = Table(None)
def as(alias: String): Table =
Table(Some(alias))
def insert(v: RFtsMigration): ConnectionIO[Int] = def insert(v: RFtsMigration): ConnectionIO[Int] =
insertRow( DML
table, .insertFragment(
all, T,
fr"${v.id},${v.version},${v.ftsEngine},${v.description},${v.created}" T.all,
).updateWithLogHandler(LogHandler.nop).run Seq(fr"${v.id},${v.version},${v.ftsEngine},${v.description},${v.created}")
)
.updateWithLogHandler(LogHandler.nop)
.run
def exists(vers: Int, engine: Ident): ConnectionIO[Boolean] = def exists(vers: Int, engine: Ident): ConnectionIO[Boolean] =
selectCount(id, table, and(version.is(vers), ftsEngine.is(engine))) run(select(count(T.id)), from(T), T.version === vers && T.ftsEngine === engine)
.query[Int] .query[Int]
.unique .unique
.map(_ > 0) .map(_ > 0)
def deleteById(rId: Ident): ConnectionIO[Int] = def deleteById(rId: Ident): ConnectionIO[Int] =
deleteFrom(table, id.is(rId)).update.run DML.delete(T, T.id === rId)
} }

View File

@ -4,8 +4,8 @@ import cats.effect.Sync
import cats.implicits._ import cats.implicits._
import docspell.common._ import docspell.common._
import docspell.store.impl.Implicits._ import docspell.store.qb.DSL._
import docspell.store.impl._ import docspell.store.qb._
import doobie._ import doobie._
import doobie.implicits._ import doobie.implicits._
@ -13,15 +13,17 @@ import doobie.implicits._
case class RInvitation(id: Ident, created: Timestamp) {} case class RInvitation(id: Ident, created: Timestamp) {}
object RInvitation { object RInvitation {
final case class Table(alias: Option[String]) extends TableDef {
val tableName = "invitation"
val table = fr"invitation" val id = Column[Ident]("id", this)
val created = Column[Timestamp]("created", this)
object Columns {
val id = Column("id")
val created = Column("created")
val all = List(id, created) val all = List(id, created)
} }
import Columns._
val T = Table(None)
def as(alias: String): Table =
Table(Some(alias))
def generate[F[_]: Sync]: F[RInvitation] = def generate[F[_]: Sync]: F[RInvitation] =
for { for {
@ -30,19 +32,19 @@ object RInvitation {
} yield RInvitation(i, c) } yield RInvitation(i, c)
def insert(v: RInvitation): ConnectionIO[Int] = def insert(v: RInvitation): ConnectionIO[Int] =
insertRow(table, all, fr"${v.id},${v.created}").update.run DML.insert(T, T.all, fr"${v.id},${v.created}")
def insertNew: ConnectionIO[RInvitation] = def insertNew: ConnectionIO[RInvitation] =
generate[ConnectionIO].flatMap(v => insert(v).map(_ => v)) generate[ConnectionIO].flatMap(v => insert(v).map(_ => v))
def findById(invite: Ident): ConnectionIO[Option[RInvitation]] = def findById(invite: Ident): ConnectionIO[Option[RInvitation]] =
selectSimple(all, table, id.is(invite)).query[RInvitation].option run(select(T.all), from(T), T.id === invite).query[RInvitation].option
def delete(invite: Ident): ConnectionIO[Int] = def delete(invite: Ident): ConnectionIO[Int] =
deleteFrom(table, id.is(invite)).update.run DML.delete(T, T.id === invite)
def useInvite(invite: Ident, minCreated: Timestamp): ConnectionIO[Boolean] = { def useInvite(invite: Ident, minCreated: Timestamp): ConnectionIO[Boolean] = {
val get = selectCount(id, table, and(id.is(invite), created.isGt(minCreated))) val get = run(select(count(T.id)), from(T), T.id === invite && T.created > minCreated)
.query[Int] .query[Int]
.unique .unique
for { for {
@ -52,5 +54,5 @@ object RInvitation {
} }
def deleteOlderThan(ts: Timestamp): ConnectionIO[Int] = def deleteOlderThan(ts: Timestamp): ConnectionIO[Int] =
deleteFrom(table, created.isLt(ts)).update.run DML.delete(T, T.created < ts)
} }