Skip to content

Commit

Permalink
Handle multiple user-defined rows of same type in a row
Browse files Browse the repository at this point in the history
  • Loading branch information
oyvindberg committed Jul 4, 2024
1 parent 2166400 commit 6e060d2
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 14 deletions.
5 changes: 3 additions & 2 deletions typo/src/scala/typo/internal/IdComputed.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ sealed trait IdComputed {
def cols: NonEmptyList[ComputedColumn]
def tpe: sc.Type
final def param: sc.Param = sc.Param(paramName, tpe, None)
final def userDefinedCols: List[ComputedColumn] =
cols.toList.collect { case x if sc.Type.containsUserDefined(x.tpe) => x }

final lazy val userDefinedColTypes: List[sc.Type] =
cols.toList.collect { case x if sc.Type.containsUserDefined(x.tpe) => x }.map(_.tpe).distinctBy(sc.Type.base)
}

object IdComputed {
Expand Down
6 changes: 3 additions & 3 deletions typo/src/scala/typo/internal/codegen/DbLibAnorm.scala
Original file line number Diff line number Diff line change
Expand Up @@ -168,11 +168,11 @@ class DbLibAnorm(pkg: sc.QIdent, inlineImplicits: Boolean, default: ComputedDefa
case RepoMethod.SelectById(_, _, id, rowType) =>
code"def $name(${id.param})(implicit c: ${TypesJava.Connection}): ${TypesScala.Option.of(rowType)}"
case RepoMethod.SelectByIds(_, _, idComputed, idsParam, rowType) =>
val usedDefineds = idComputed.userDefinedCols.zipWithIndex.map { case (col, i) => sc.Param(sc.Ident(s"toStatement$i"), ToStatement.of(sc.Type.ArrayOf(col.tpe)), None) }
val usedDefineds = idComputed.userDefinedColTypes.zipWithIndex.map { case (colType, i) => sc.Param(sc.Ident(s"toStatement$i"), ToStatement.of(sc.Type.ArrayOf(colType)), None) }
val params = sc.Param(sc.Ident("c"), TypesJava.Connection, None) :: usedDefineds
code"def $name($idsParam)(implicit ${params.map(_.code).mkCode(", ")}): ${TypesScala.List.of(rowType)}"
case RepoMethod.SelectByIdsTracked(x) =>
val usedDefineds = x.idComputed.userDefinedCols.zipWithIndex.map { case (col, i) => sc.Param(sc.Ident(s"toStatement$i"), ToStatement.of(sc.Type.ArrayOf(col.tpe)), None) }
val usedDefineds = x.idComputed.userDefinedColTypes.zipWithIndex.map { case (colType, i) => sc.Param(sc.Ident(s"toStatement$i"), ToStatement.of(sc.Type.ArrayOf(colType)), None) }
val params = sc.Param(sc.Ident("c"), TypesJava.Connection, None) :: usedDefineds
code"def $name(${x.idsParam})(implicit ${params.map(_.code).mkCode(", ")}): ${TypesScala.Map.of(x.idComputed.tpe, x.rowType)}"
case RepoMethod.SelectByUnique(_, keyColumns, _, rowType) =>
Expand Down Expand Up @@ -200,7 +200,7 @@ class DbLibAnorm(pkg: sc.QIdent, inlineImplicits: Boolean, default: ComputedDefa
case RepoMethod.Delete(_, id) =>
code"def $name(${id.param})(implicit c: ${TypesJava.Connection}): ${TypesScala.Boolean}"
case RepoMethod.DeleteByIds(_, idComputed, idsParam) =>
val usedDefineds = idComputed.userDefinedCols.zipWithIndex.map { case (col, i) => sc.Param(sc.Ident(s"toStatement$i"), ToStatement.of(sc.Type.ArrayOf(col.tpe)), None) }
val usedDefineds = idComputed.userDefinedColTypes.zipWithIndex.map { case (colType, i) => sc.Param(sc.Ident(s"toStatement$i"), ToStatement.of(sc.Type.ArrayOf(colType)), None) }
val params = sc.Param(sc.Ident("c"), TypesJava.Connection, None) :: usedDefineds
code"def $name($idsParam)(implicit ${params.map(_.code).mkCode(", ")}): ${TypesScala.Int}"
case RepoMethod.SqlFile(sqlScript) =>
Expand Down
8 changes: 3 additions & 5 deletions typo/src/scala/typo/internal/codegen/DbLibDoobie.scala
Original file line number Diff line number Diff line change
Expand Up @@ -71,15 +71,15 @@ class DbLibDoobie(pkg: sc.QIdent, inlineImplicits: Boolean, default: ComputedDef
case RepoMethod.SelectById(_, _, id, rowType) =>
code"def $name(${id.param}): ${ConnectionIO.of(TypesScala.Option.of(rowType))}"
case RepoMethod.SelectByIds(_, _, idComputed, idsParam, rowType) =>
val usedDefineds = idComputed.userDefinedCols.zipWithIndex.map { case (col, i) => sc.Param(sc.Ident(s"puts$i"), Put.of(sc.Type.ArrayOf(col.tpe)), None) }
val usedDefineds = idComputed.userDefinedColTypes.zipWithIndex.map { case (colType, i) => sc.Param(sc.Ident(s"puts$i"), Put.of(sc.Type.ArrayOf(colType)), None) }
usedDefineds match {
case Nil =>
code"def $name($idsParam): ${fs2Stream.of(ConnectionIO, rowType)}"
case nonEmpty =>
code"def $name($idsParam)(implicit ${nonEmpty.map(_.code).mkCode(", ")}): ${fs2Stream.of(ConnectionIO, rowType)}"
}
case RepoMethod.SelectByIdsTracked(x) =>
val usedDefineds = x.idComputed.userDefinedCols.zipWithIndex.map { case (col, i) => sc.Param(sc.Ident(s"puts$i"), Put.of(sc.Type.ArrayOf(col.tpe)), None) }
val usedDefineds = x.idComputed.userDefinedColTypes.zipWithIndex.map { case (colType, i) => sc.Param(sc.Ident(s"puts$i"), Put.of(sc.Type.ArrayOf(colType)), None) }
val returnType = ConnectionIO.of(TypesScala.Map.of(x.idComputed.tpe, x.rowType))
usedDefineds match {
case Nil =>
Expand Down Expand Up @@ -113,9 +113,7 @@ class DbLibDoobie(pkg: sc.QIdent, inlineImplicits: Boolean, default: ComputedDef
case RepoMethod.Delete(_, id) =>
code"def $name(${id.param}): ${ConnectionIO.of(TypesScala.Boolean)}"
case RepoMethod.DeleteByIds(_, idComputed, idsParam) =>
val usedDefineds =
idComputed.userDefinedCols.zipWithIndex
.map { case (col, i) => sc.Param(sc.Ident(s"put$i"), Put.of(sc.Type.ArrayOf(col.tpe)), None) }
val usedDefineds = idComputed.userDefinedColTypes.zipWithIndex.map { case (colType, i) => sc.Param(sc.Ident(s"put$i"), Put.of(sc.Type.ArrayOf(colType)), None) }
usedDefineds match {
case Nil =>
code"def $name(${idsParam}): ${ConnectionIO.of(TypesScala.Int)}"
Expand Down
7 changes: 3 additions & 4 deletions typo/src/scala/typo/internal/codegen/DbLibZioJdbc.scala
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ class DbLibZioJdbc(pkg: sc.QIdent, inlineImplicits: Boolean, dslEnabled: Boolean
case RepoMethod.SelectById(_, _, id, rowType) =>
code"def $name(${id.param}): ${ZIO.of(ZConnection, Throwable, TypesScala.Option.of(rowType))}"
case RepoMethod.SelectByIds(_, _, idComputed, idsParam, rowType) =>
val usedDefineds = idComputed.userDefinedCols.zipWithIndex.map { case (col, i) => sc.Param(sc.Ident(s"encoder$i"), JdbcEncoder.of(sc.Type.ArrayOf(col.tpe)), None) }
val usedDefineds = idComputed.userDefinedColTypes.zipWithIndex.map { case (colType, i) => sc.Param(sc.Ident(s"encoder$i"), JdbcEncoder.of(sc.Type.ArrayOf(colType)), None) }

usedDefineds match {
case Nil =>
Expand All @@ -197,7 +197,7 @@ class DbLibZioJdbc(pkg: sc.QIdent, inlineImplicits: Boolean, dslEnabled: Boolean
code"def $name($idsParam)(implicit ${nonEmpty.map(_.code).mkCode(", ")}): ${ZStream.of(ZConnection, Throwable, rowType)}"
}
case RepoMethod.SelectByIdsTracked(x) =>
val usedDefineds = x.idComputed.userDefinedCols.zipWithIndex.map { case (col, i) => sc.Param(sc.Ident(s"encoder$i"), JdbcEncoder.of(sc.Type.ArrayOf(col.tpe)), None) }
val usedDefineds = x.idComputed.userDefinedColTypes.zipWithIndex.map { case (colType, i) => sc.Param(sc.Ident(s"encoder$i"), JdbcEncoder.of(sc.Type.ArrayOf(colType)), None) }
val returnType = ZIO.of(ZConnection, Throwable, TypesScala.Map.of(x.idComputed.tpe, x.rowType))
usedDefineds match {
case Nil =>
Expand Down Expand Up @@ -235,8 +235,7 @@ class DbLibZioJdbc(pkg: sc.QIdent, inlineImplicits: Boolean, dslEnabled: Boolean
case RepoMethod.Delete(_, id) =>
code"def $name(${id.param}): ${ZIO.of(ZConnection, Throwable, TypesScala.Boolean)}"
case RepoMethod.DeleteByIds(_, idComputed, idsParam) =>
val usedDefineds = idComputed.userDefinedCols.zipWithIndex
.map { case (col, i) => sc.Param(sc.Ident(s"encoder$i"), JdbcEncoder.of(sc.Type.ArrayOf(col.tpe)), None) }
val usedDefineds = idComputed.userDefinedColTypes.zipWithIndex.map { case (colType, i) => sc.Param(sc.Ident(s"encoder$i"), JdbcEncoder.of(sc.Type.ArrayOf(colType)), None) }
usedDefineds match {
case Nil =>
code"def $name(${idsParam}): ${ZIO.of(ZConnection, Throwable, TypesScala.Long)}"
Expand Down

0 comments on commit 6e060d2

Please sign in to comment.