From 87eb8c7f55d8c02696707293aeb22ff10feeb574 Mon Sep 17 00:00:00 2001 From: Eike Kettner Date: Sat, 12 Dec 2020 12:23:01 +0100 Subject: [PATCH] Convert more records --- .../store/records/RFtsMigration.scala | 40 +++++++++++-------- .../docspell/store/records/RInvitation.scala | 28 +++++++------ 2 files changed, 38 insertions(+), 30 deletions(-) diff --git a/modules/store/src/main/scala/docspell/store/records/RFtsMigration.scala b/modules/store/src/main/scala/docspell/store/records/RFtsMigration.scala index b02d26f7..b2f21930 100644 --- a/modules/store/src/main/scala/docspell/store/records/RFtsMigration.scala +++ b/modules/store/src/main/scala/docspell/store/records/RFtsMigration.scala @@ -4,8 +4,8 @@ import cats.effect._ import cats.implicits._ import docspell.common._ -import docspell.store.impl.Implicits._ -import docspell.store.impl._ +import docspell.store.qb.DSL._ +import docspell.store.qb._ import doobie._ import doobie.implicits._ @@ -30,32 +30,38 @@ object RFtsMigration { now <- Timestamp.current[F] } 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("id") - val version = Column("version") - val ftsEngine = Column("fts_engine") - val description = Column("description") - val created = Column("created") + val id = Column[Ident]("id", this) + val version = Column[Int]("version", this) + val ftsEngine = Column[Ident]("fts_engine", this) + val description = Column[String]("description", this) + val created = Column[Timestamp]("created", this) 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] = - insertRow( - table, - all, - fr"${v.id},${v.version},${v.ftsEngine},${v.description},${v.created}" - ).updateWithLogHandler(LogHandler.nop).run + DML + .insertFragment( + T, + T.all, + Seq(fr"${v.id},${v.version},${v.ftsEngine},${v.description},${v.created}") + ) + .updateWithLogHandler(LogHandler.nop) + .run 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] .unique .map(_ > 0) def deleteById(rId: Ident): ConnectionIO[Int] = - deleteFrom(table, id.is(rId)).update.run + DML.delete(T, T.id === rId) } diff --git a/modules/store/src/main/scala/docspell/store/records/RInvitation.scala b/modules/store/src/main/scala/docspell/store/records/RInvitation.scala index 6a516788..f3243566 100644 --- a/modules/store/src/main/scala/docspell/store/records/RInvitation.scala +++ b/modules/store/src/main/scala/docspell/store/records/RInvitation.scala @@ -4,8 +4,8 @@ import cats.effect.Sync import cats.implicits._ import docspell.common._ -import docspell.store.impl.Implicits._ -import docspell.store.impl._ +import docspell.store.qb.DSL._ +import docspell.store.qb._ import doobie._ import doobie.implicits._ @@ -13,15 +13,17 @@ import doobie.implicits._ case class RInvitation(id: Ident, created: Timestamp) {} object RInvitation { + final case class Table(alias: Option[String]) extends TableDef { + val tableName = "invitation" - val table = fr"invitation" - - object Columns { - val id = Column("id") - val created = Column("created") + val id = Column[Ident]("id", this) + val created = Column[Timestamp]("created", this) 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] = for { @@ -30,19 +32,19 @@ object RInvitation { } yield RInvitation(i, c) 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] = generate[ConnectionIO].flatMap(v => insert(v).map(_ => v)) 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] = - deleteFrom(table, id.is(invite)).update.run + DML.delete(T, T.id === invite) 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] .unique for { @@ -52,5 +54,5 @@ object RInvitation { } def deleteOlderThan(ts: Timestamp): ConnectionIO[Int] = - deleteFrom(table, created.isLt(ts)).update.run + DML.delete(T, T.created < ts) }