Skip to content

Commit

Permalink
Add count in DSL
Browse files Browse the repository at this point in the history
  • Loading branch information
oyvindberg committed Jun 23, 2024
1 parent e9d7165 commit 0b21c2d
Show file tree
Hide file tree
Showing 12 changed files with 31 additions and 0 deletions.
2 changes: 2 additions & 0 deletions typo-dsl-anorm/src/scala/typo/dsl/SelectBuilder.scala
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ trait SelectBuilder[Fields, Row] {
/** Execute the query and return the results as a list */
def toList(implicit c: Connection): List[Row]

def count(implicit c: Connection): Int

/** Return sql for debugging. [[None]] if backed by a mock repository */
def sql: Option[Fragment]

Expand Down
3 changes: 3 additions & 0 deletions typo-dsl-anorm/src/scala/typo/dsl/SelectBuilderMock.scala
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ final case class SelectBuilderMock[Fields, Row](
override def toList(implicit c: Connection): List[Row] =
SelectBuilderMock.applyParams(structure, all(), params)

override def count(implicit c: Connection): Int =
toList(c).length

override def joinOn[Fields2, N[_]: Nullability, Row2](other: SelectBuilder[Fields2, Row2])(pred: (Fields ~ Fields2) => SqlExpr[Boolean, N]): SelectBuilderMock[Fields ~ Fields2, Row ~ Row2] = {
val otherMock: SelectBuilderMock[Fields2, Row2] = other match {
case x: SelectBuilderMock[Fields2, Row2] => x.withPath(Path.RightInJoin)
Expand Down
5 changes: 5 additions & 0 deletions typo-dsl-anorm/src/scala/typo/dsl/SelectBuilderSql.scala
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ sealed trait SelectBuilderSql[Fields, Row] extends SelectBuilder[Fields, Row] {
val (frag, rowParser) = sqlAndRowParser
SimpleSql(SQL(frag.sql), frag.params.map(_.tupled).toMap, RowParser.successful).as(rowParser.*)(c)
}

final override def count(implicit c: Connection): Int = {
val (frag, _) = sqlAndRowParser
SimpleSql(SQL(s"select count(*) from (${frag.sql}) rows"), frag.params.map(_.tupled).toMap, RowParser.successful).as(SqlParser.int(1).single)(c)
}
}

object SelectBuilderSql {
Expand Down
2 changes: 2 additions & 0 deletions typo-dsl-doobie/src/scala/typo/dsl/SelectBuilder.scala
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ trait SelectBuilder[Fields, Row] {
/** Execute the query and return the results as a list */
def toList: ConnectionIO[List[Row]]

def count: ConnectionIO[Int]

/** Return sql for debugging. [[None]] if backed by a mock repository */
def sql: Option[Fragment]

Expand Down
3 changes: 3 additions & 0 deletions typo-dsl-doobie/src/scala/typo/dsl/SelectBuilderMock.scala
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ final case class SelectBuilderMock[Fields, Row](
override def toList: ConnectionIO[List[Row]] =
all.map(all => SelectBuilderMock.applyParams(structure, all, params))

override def count: ConnectionIO[Int] =
all.map(all => SelectBuilderMock.applyParams(structure, all, params)).map(_.length)

override def joinOn[Fields2, N[_]: Nullability, Row2](other: SelectBuilder[Fields2, Row2])(pred: Fields ~ Fields2 => SqlExpr[Boolean, N]): SelectBuilderMock[Fields ~ Fields2, Row ~ Row2] = {
val otherMock: SelectBuilderMock[Fields2, Row2] = other match {
case x: SelectBuilderMock[Fields2, Row2] => x.withPath(Path.RightInJoin)
Expand Down
4 changes: 4 additions & 0 deletions typo-dsl-doobie/src/scala/typo/dsl/SelectBuilderSql.scala
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ sealed trait SelectBuilderSql[Fields, Row] extends SelectBuilder[Fields, Row] {
val (frag, read) = sqlAndRowParser
frag.query(using read).to[List]
}
final override def count: ConnectionIO[Int] = {
val (frag, _) = sqlAndRowParser
fr"select count(*) from ($frag) rows".query[Int].unique
}
}

object SelectBuilderSql {
Expand Down
2 changes: 2 additions & 0 deletions typo-dsl-zio-jdbc/src/scala/typo/dsl/SelectBuilder.scala
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ trait SelectBuilder[Fields, Row] {
/** Execute the query and return the results as a list */
def toChunk: ZIO[ZConnection, Throwable, Chunk[Row]]

def count: ZIO[ZConnection, Throwable, Int]

/** Return sql for debugging. [[None]] if backed by a mock repository */
def sql: Option[SqlFragment]

Expand Down
3 changes: 3 additions & 0 deletions typo-dsl-zio-jdbc/src/scala/typo/dsl/SelectBuilderMock.scala
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ final case class SelectBuilderMock[Fields, Row](
override def toChunk: ZIO[ZConnection, Throwable, Chunk[Row]] =
all.map(all => SelectBuilderMock.applyParams(structure, all, params))

override def count: ZIO[ZConnection, Throwable, Int] =
all.map(all => SelectBuilderMock.applyParams(structure, all, params).length)

override def joinOn[Fields2, N[_]: Nullability, Row2](other: SelectBuilder[Fields2, Row2])(pred: Fields ~ Fields2 => SqlExpr[Boolean, N]): SelectBuilderMock[Fields ~ Fields2, Row ~ Row2] = {
val otherMock: SelectBuilderMock[Fields2, Row2] = other match {
case x: SelectBuilderMock[Fields2, Row2] => x.withPath(Path.RightInJoin)
Expand Down
4 changes: 4 additions & 0 deletions typo-dsl-zio-jdbc/src/scala/typo/dsl/SelectBuilderSql.scala
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ select ${SqlFragment(cols.mkString(","))} from ${SqlFragment(ctes.last.name)}"""
val (frag, read) = sqlAndRowParser
frag.query(using read).selectAll
}
final override def count: ZIO[ZConnection, Throwable, Int] = {
val (frag, _) = sqlAndRowParser
sql"select count(*) from ($frag) rows".query[Int].selectOne.map(_.get)
}
}

object SelectBuilderSql {
Expand Down
1 change: 1 addition & 0 deletions typo-tester-anorm/src/scala/adventureworks/DSLTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ class DSLTest extends SnapshotTest {
val doubled = q.join(q).on { case (((_, e1), _), ((_, e2), _)) => e1.businessentityid === e2.businessentityid }

doubled.toList.foreach(println)
assert(doubled.count == 1): @nowarn

compareFragment("doubled")(doubled.sql)
}
Expand Down
1 change: 1 addition & 0 deletions typo-tester-doobie/src/scala/adventureworks/DSLTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ class DSLTest extends SnapshotTest {
doubled = q.join(q).on { case (((_, e1), _), ((_, e2), _)) => e1.businessentityid === e2.businessentityid }
doubledRes <- doubled.toList
_ <- delay(doubledRes.foreach(println))
_ <- doubled.count.map(v => assert(v == 1))
_ <- delay(compareFragment("doubled")(doubled.sql))
} yield ()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ class DSLTest extends SnapshotTest {
doubled = q.join(q).on { case (((_, e1), _), ((_, e2), _)) => e1.businessentityid === e2.businessentityid }
doubledRes <- doubled.toChunk
_ <- ZIO.succeed(doubledRes.foreach(println))
_ <- doubled.count.map(v => assert(v == 1))
_ <- ZIO.succeed(compareFragment("doubled")(doubled.sql))
} yield ()
}
Expand Down

0 comments on commit 0b21c2d

Please sign in to comment.