Skip to content

Commit

Permalink
Longer names for SelectByUnique
Browse files Browse the repository at this point in the history
Seems it's fairly common that types match, and  I just discovered you also need to take care of making sure two `AnyVal` with same underlying type do not erasure clash.

It'll be just easier and more flexible to revert back to including column names in the method name
  • Loading branch information
oyvindberg committed Mar 21, 2024
1 parent 47bcaca commit acd42f7
Show file tree
Hide file tree
Showing 22 changed files with 24 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ trait DocumentRepo {
def selectAll(implicit c: Connection): List[DocumentRow]
def selectById(documentnode: DocumentId)(implicit c: Connection): Option[DocumentRow]
def selectByIds(documentnodes: Array[DocumentId])(implicit c: Connection): List[DocumentRow]
def selectByUnique(rowguid: TypoUUID)(implicit c: Connection): Option[DocumentRow]
def selectByUniqueRowguid(rowguid: TypoUUID)(implicit c: Connection): Option[DocumentRow]
def update(row: DocumentRow)(implicit c: Connection): Boolean
def update: UpdateBuilder[DocumentFields, DocumentRow]
def upsert(unsaved: DocumentRow)(implicit c: Connection): DocumentRow
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ class DocumentRepoImpl extends DocumentRepo {
""".as(DocumentRow.rowParser(1).*)

}
override def selectByUnique(rowguid: TypoUUID)(implicit c: Connection): Option[DocumentRow] = {
override def selectByUniqueRowguid(rowguid: TypoUUID)(implicit c: Connection): Option[DocumentRow] = {
SQL"""select "title", "owner", "folderflag", "filename", "fileextension", "revision", "changenumber", "status", "documentsummary", "document", "rowguid", "modifieddate"::text, "documentnode"
from production.document
where "rowguid" = ${ParameterValue(rowguid, null, TypoUUID.toStatement)}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ class DocumentRepoMock(toRow: Function1[DocumentRowUnsaved, DocumentRow],
override def selectByIds(documentnodes: Array[DocumentId])(implicit c: Connection): List[DocumentRow] = {
documentnodes.flatMap(map.get).toList
}
override def selectByUnique(rowguid: TypoUUID)(implicit c: Connection): Option[DocumentRow] = {
override def selectByUniqueRowguid(rowguid: TypoUUID)(implicit c: Connection): Option[DocumentRow] = {
map.values.find(v => rowguid == v.rowguid)
}
override def update(row: DocumentRow)(implicit c: Connection): Boolean = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ trait UsersRepo {
def selectAll(implicit c: Connection): List[UsersRow]
def selectById(userId: UsersId)(implicit c: Connection): Option[UsersRow]
def selectByIds(userIds: Array[UsersId])(implicit c: Connection): List[UsersRow]
def selectByUnique(email: TypoUnknownCitext)(implicit c: Connection): Option[UsersRow]
def selectByUniqueEmail(email: TypoUnknownCitext)(implicit c: Connection): Option[UsersRow]
def update(row: UsersRow)(implicit c: Connection): Boolean
def update: UpdateBuilder[UsersFields, UsersRow]
def upsert(unsaved: UsersRow)(implicit c: Connection): UsersRow
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ class UsersRepoImpl extends UsersRepo {
""".as(UsersRow.rowParser(1).*)

}
override def selectByUnique(email: TypoUnknownCitext)(implicit c: Connection): Option[UsersRow] = {
override def selectByUniqueEmail(email: TypoUnknownCitext)(implicit c: Connection): Option[UsersRow] = {
SQL"""select "user_id", "name", "last_name", "email"::text, "password", "created_at"::text, "verified_on"::text
from public.users
where "email" = ${ParameterValue(email, null, TypoUnknownCitext.toStatement)}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ class UsersRepoMock(toRow: Function1[UsersRowUnsaved, UsersRow],
override def selectByIds(userIds: Array[UsersId])(implicit c: Connection): List[UsersRow] = {
userIds.flatMap(map.get).toList
}
override def selectByUnique(email: TypoUnknownCitext)(implicit c: Connection): Option[UsersRow] = {
override def selectByUniqueEmail(email: TypoUnknownCitext)(implicit c: Connection): Option[UsersRow] = {
map.values.find(v => email == v.email)
}
override def update(row: UsersRow)(implicit c: Connection): Boolean = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ trait DocumentRepo {
def selectAll: Stream[ConnectionIO, DocumentRow]
def selectById(documentnode: DocumentId): ConnectionIO[Option[DocumentRow]]
def selectByIds(documentnodes: Array[DocumentId]): Stream[ConnectionIO, DocumentRow]
def selectByUnique(rowguid: TypoUUID): ConnectionIO[Option[DocumentRow]]
def selectByUniqueRowguid(rowguid: TypoUUID): ConnectionIO[Option[DocumentRow]]
def update(row: DocumentRow): ConnectionIO[Boolean]
def update: UpdateBuilder[DocumentFields, DocumentRow]
def upsert(unsaved: DocumentRow): ConnectionIO[DocumentRow]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ class DocumentRepoImpl extends DocumentRepo {
override def selectByIds(documentnodes: Array[DocumentId]): Stream[ConnectionIO, DocumentRow] = {
sql"""select "title", "owner", "folderflag", "filename", "fileextension", "revision", "changenumber", "status", "documentsummary", "document", "rowguid", "modifieddate"::text, "documentnode" from production.document where "documentnode" = ANY(${documentnodes})""".query(using DocumentRow.read).stream
}
override def selectByUnique(rowguid: TypoUUID): ConnectionIO[Option[DocumentRow]] = {
override def selectByUniqueRowguid(rowguid: TypoUUID): ConnectionIO[Option[DocumentRow]] = {
sql"""select "title", "owner", "folderflag", "filename", "fileextension", "revision", "changenumber", "status", "documentsummary", "document", "rowguid", "modifieddate"::text, "documentnode"
from production.document
where "rowguid" = ${fromWrite(rowguid)(Write.fromPut(TypoUUID.put))}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ class DocumentRepoMock(toRow: Function1[DocumentRowUnsaved, DocumentRow],
override def selectByIds(documentnodes: Array[DocumentId]): Stream[ConnectionIO, DocumentRow] = {
Stream.emits(documentnodes.flatMap(map.get).toList)
}
override def selectByUnique(rowguid: TypoUUID): ConnectionIO[Option[DocumentRow]] = {
override def selectByUniqueRowguid(rowguid: TypoUUID): ConnectionIO[Option[DocumentRow]] = {
delay(map.values.find(v => rowguid == v.rowguid))
}
override def update(row: DocumentRow): ConnectionIO[Boolean] = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ trait UsersRepo {
def selectAll: Stream[ConnectionIO, UsersRow]
def selectById(userId: UsersId): ConnectionIO[Option[UsersRow]]
def selectByIds(userIds: Array[UsersId]): Stream[ConnectionIO, UsersRow]
def selectByUnique(email: TypoUnknownCitext): ConnectionIO[Option[UsersRow]]
def selectByUniqueEmail(email: TypoUnknownCitext): ConnectionIO[Option[UsersRow]]
def update(row: UsersRow): ConnectionIO[Boolean]
def update: UpdateBuilder[UsersFields, UsersRow]
def upsert(unsaved: UsersRow): ConnectionIO[UsersRow]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ class UsersRepoImpl extends UsersRepo {
override def selectByIds(userIds: Array[UsersId]): Stream[ConnectionIO, UsersRow] = {
sql"""select "user_id", "name", "last_name", "email"::text, "password", "created_at"::text, "verified_on"::text from public.users where "user_id" = ANY(${userIds})""".query(using UsersRow.read).stream
}
override def selectByUnique(email: TypoUnknownCitext): ConnectionIO[Option[UsersRow]] = {
override def selectByUniqueEmail(email: TypoUnknownCitext): ConnectionIO[Option[UsersRow]] = {
sql"""select "user_id", "name", "last_name", "email"::text, "password", "created_at"::text, "verified_on"::text
from public.users
where "email" = ${fromWrite(email)(Write.fromPut(TypoUnknownCitext.put))}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ class UsersRepoMock(toRow: Function1[UsersRowUnsaved, UsersRow],
override def selectByIds(userIds: Array[UsersId]): Stream[ConnectionIO, UsersRow] = {
Stream.emits(userIds.flatMap(map.get).toList)
}
override def selectByUnique(email: TypoUnknownCitext): ConnectionIO[Option[UsersRow]] = {
override def selectByUniqueEmail(email: TypoUnknownCitext): ConnectionIO[Option[UsersRow]] = {
delay(map.values.find(v => email == v.email))
}
override def update(row: UsersRow): ConnectionIO[Boolean] = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ trait DocumentRepo {
def selectAll: ZStream[ZConnection, Throwable, DocumentRow]
def selectById(documentnode: DocumentId): ZIO[ZConnection, Throwable, Option[DocumentRow]]
def selectByIds(documentnodes: Array[DocumentId]): ZStream[ZConnection, Throwable, DocumentRow]
def selectByUnique(rowguid: TypoUUID): ZIO[ZConnection, Throwable, Option[DocumentRow]]
def selectByUniqueRowguid(rowguid: TypoUUID): ZIO[ZConnection, Throwable, Option[DocumentRow]]
def update(row: DocumentRow): ZIO[ZConnection, Throwable, Boolean]
def update: UpdateBuilder[DocumentFields, DocumentRow]
def upsert(unsaved: DocumentRow): ZIO[ZConnection, Throwable, UpdateResult[DocumentRow]]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ class DocumentRepoImpl extends DocumentRepo {
override def selectByIds(documentnodes: Array[DocumentId]): ZStream[ZConnection, Throwable, DocumentRow] = {
sql"""select "title", "owner", "folderflag", "filename", "fileextension", "revision", "changenumber", "status", "documentsummary", "document", "rowguid", "modifieddate"::text, "documentnode" from production.document where "documentnode" = ANY(${Segment.paramSegment(documentnodes)(DocumentId.arraySetter)})""".query(using DocumentRow.jdbcDecoder).selectStream()
}
override def selectByUnique(rowguid: TypoUUID): ZIO[ZConnection, Throwable, Option[DocumentRow]] = {
override def selectByUniqueRowguid(rowguid: TypoUUID): ZIO[ZConnection, Throwable, Option[DocumentRow]] = {
sql"""select "title", "owner", "folderflag", "filename", "fileextension", "revision", "changenumber", "status", "documentsummary", "document", "rowguid", "modifieddate"::text, "documentnode"
from production.document
where "rowguid" = ${Segment.paramSegment(rowguid)(TypoUUID.setter)}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ class DocumentRepoMock(toRow: Function1[DocumentRowUnsaved, DocumentRow],
override def selectByIds(documentnodes: Array[DocumentId]): ZStream[ZConnection, Throwable, DocumentRow] = {
ZStream.fromIterable(documentnodes.flatMap(map.get))
}
override def selectByUnique(rowguid: TypoUUID): ZIO[ZConnection, Throwable, Option[DocumentRow]] = {
override def selectByUniqueRowguid(rowguid: TypoUUID): ZIO[ZConnection, Throwable, Option[DocumentRow]] = {
ZIO.succeed(map.values.find(v => rowguid == v.rowguid))
}
override def update(row: DocumentRow): ZIO[ZConnection, Throwable, Boolean] = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ trait UsersRepo {
def selectAll: ZStream[ZConnection, Throwable, UsersRow]
def selectById(userId: UsersId): ZIO[ZConnection, Throwable, Option[UsersRow]]
def selectByIds(userIds: Array[UsersId]): ZStream[ZConnection, Throwable, UsersRow]
def selectByUnique(email: TypoUnknownCitext): ZIO[ZConnection, Throwable, Option[UsersRow]]
def selectByUniqueEmail(email: TypoUnknownCitext): ZIO[ZConnection, Throwable, Option[UsersRow]]
def update(row: UsersRow): ZIO[ZConnection, Throwable, Boolean]
def update: UpdateBuilder[UsersFields, UsersRow]
def upsert(unsaved: UsersRow): ZIO[ZConnection, Throwable, UpdateResult[UsersRow]]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ class UsersRepoImpl extends UsersRepo {
override def selectByIds(userIds: Array[UsersId]): ZStream[ZConnection, Throwable, UsersRow] = {
sql"""select "user_id", "name", "last_name", "email"::text, "password", "created_at"::text, "verified_on"::text from public.users where "user_id" = ANY(${Segment.paramSegment(userIds)(UsersId.arraySetter)})""".query(using UsersRow.jdbcDecoder).selectStream()
}
override def selectByUnique(email: TypoUnknownCitext): ZIO[ZConnection, Throwable, Option[UsersRow]] = {
override def selectByUniqueEmail(email: TypoUnknownCitext): ZIO[ZConnection, Throwable, Option[UsersRow]] = {
sql"""select "user_id", "name", "last_name", "email"::text, "password", "created_at"::text, "verified_on"::text
from public.users
where "email" = ${Segment.paramSegment(email)(TypoUnknownCitext.setter)}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ class UsersRepoMock(toRow: Function1[UsersRowUnsaved, UsersRow],
override def selectByIds(userIds: Array[UsersId]): ZStream[ZConnection, Throwable, UsersRow] = {
ZStream.fromIterable(userIds.flatMap(map.get))
}
override def selectByUnique(email: TypoUnknownCitext): ZIO[ZConnection, Throwable, Option[UsersRow]] = {
override def selectByUniqueEmail(email: TypoUnknownCitext): ZIO[ZConnection, Throwable, Option[UsersRow]] = {
ZIO.succeed(map.values.find(v => email == v.email))
}
override def update(row: UsersRow): ZIO[ZConnection, Throwable, Boolean] = {
Expand Down
2 changes: 0 additions & 2 deletions typo/src/scala/typo/internal/ComputedTable.scala
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package typo
package internal

import typo.internal.compat.ListOps
import typo.internal.rewriteDependentData.Eval

case class ComputedTable(
Expand Down Expand Up @@ -201,7 +200,6 @@ case class ComputedTable(
rowType = names.RowName
)
}
.distinctByCompat(x => x.keyColumns.map(_.tpe)) // avoid erasure clashes
)
val valid = maybeMethods.flatten.filter {
case _: RepoMethod.Mutator => !options.readonlyRepo.include(dbTable.name)
Expand Down
3 changes: 2 additions & 1 deletion typo/src/scala/typo/internal/codegen/DbLibAnorm.scala
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,8 @@ class DbLibAnorm(pkg: sc.QIdent, inlineImplicits: Boolean, default: ComputedDefa
code"def selectByIds($idsParam)(implicit c: ${TypesJava.Connection}): ${TypesScala.List.of(rowType)}"
}
case RepoMethod.SelectByUnique(_, keyColumns, _, rowType) =>
code"def selectByUnique(${keyColumns.map(_.param.code).mkCode(", ")})(implicit c: ${TypesJava.Connection}): ${TypesScala.Option.of(rowType)}"
val name = s"selectByUnique${keyColumns.map(x => Naming.titleCase(x.name.value)).mkString("And")}"
code"def $name(${keyColumns.map(_.param.code).mkCode(", ")})(implicit c: ${TypesJava.Connection}): ${TypesScala.Option.of(rowType)}"
case RepoMethod.SelectByFieldValues(_, _, _, fieldValueOrIdsParam, rowType) =>
code"def selectByFieldValues($fieldValueOrIdsParam)(implicit c: ${TypesJava.Connection}): ${TypesScala.List.of(rowType)}"
case RepoMethod.UpdateBuilder(_, fieldsType, rowType) =>
Expand Down
3 changes: 2 additions & 1 deletion typo/src/scala/typo/internal/codegen/DbLibDoobie.scala
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,8 @@ class DbLibDoobie(pkg: sc.QIdent, inlineImplicits: Boolean, default: ComputedDef
code"def selectByIds($idsParam): ${fs2Stream.of(ConnectionIO, rowType)}"
}
case RepoMethod.SelectByUnique(_, keyColumns, _, rowType) =>
code"def selectByUnique(${keyColumns.map(_.param.code).mkCode(", ")}): ${ConnectionIO.of(TypesScala.Option.of(rowType))}"
val name = s"selectByUnique${keyColumns.map(x => Naming.titleCase(x.name.value)).mkString("And")}"
code"def $name(${keyColumns.map(_.param.code).mkCode(", ")}): ${ConnectionIO.of(TypesScala.Option.of(rowType))}"
case RepoMethod.SelectByFieldValues(_, _, _, fieldValueOrIdsParam, rowType) =>
code"def selectByFieldValues($fieldValueOrIdsParam): ${fs2Stream.of(ConnectionIO, rowType)}"
case RepoMethod.UpdateBuilder(_, fieldsType, rowType) =>
Expand Down
3 changes: 2 additions & 1 deletion typo/src/scala/typo/internal/codegen/DbLibZioJdbc.scala
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,8 @@ class DbLibZioJdbc(pkg: sc.QIdent, inlineImplicits: Boolean, dslEnabled: Boolean
code"def selectByIds($idsParam): ${ZStream.of(ZConnection, Throwable, rowType)}"
}
case RepoMethod.SelectByUnique(_, keyColumns, _, rowType) =>
code"def selectByUnique(${keyColumns.map(_.param.code).mkCode(", ")}): ${ZIO.of(ZConnection, Throwable, TypesScala.Option.of(rowType))}"
val name = s"selectByUnique${keyColumns.map(x => Naming.titleCase(x.name.value)).mkString("And")}"
code"def $name(${keyColumns.map(_.param.code).mkCode(", ")}): ${ZIO.of(ZConnection, Throwable, TypesScala.Option.of(rowType))}"
case RepoMethod.SelectByFieldValues(_, _, _, fieldValueOrIdsParam, rowType) =>
code"def selectByFieldValues($fieldValueOrIdsParam): ${ZStream.of(ZConnection, Throwable, rowType)}"
case RepoMethod.UpdateBuilder(_, fieldsType, rowType) =>
Expand Down

0 comments on commit acd42f7

Please sign in to comment.