Skip to content

Commit

Permalink
selectByUnique must select all columns (Fixes #84)
Browse files Browse the repository at this point in the history
  • Loading branch information
oyvindberg committed Feb 17, 2024
1 parent 53539fa commit 17d53d1
Show file tree
Hide file tree
Showing 11 changed files with 37 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ class DocumentRepoImpl extends DocumentRepo {

}
override def selectByUnique(rowguid: TypoUUID)(implicit c: Connection): Option[DocumentRow] = {
SQL"""select "rowguid"
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)}
""".as(DocumentRow.rowParser(1).singleOpt)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ class UsersRepoImpl extends UsersRepo {

}
override def selectByUnique(email: TypoUnknownCitext)(implicit c: Connection): Option[UsersRow] = {
SQL"""select "email"::text
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)}
""".as(UsersRow.rowParser(1).singleOpt)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ class DocumentRepoImpl extends DocumentRepo {
sql"""select "title", "owner", "folderflag", "filename", "fileextension", "revision", "changenumber", "status", "documentsummary", "document", "rowguid", "modifieddate"::text, "documentnode" from production.document where "documentnode" = ANY(${documentnodes})""".query(DocumentRow.read).stream
}
override def selectByUnique(rowguid: TypoUUID): ConnectionIO[Option[DocumentRow]] = {
sql"""select "rowguid"
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))}
""".query(DocumentRow.read).option
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ class UsersRepoImpl extends UsersRepo {
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(UsersRow.read).stream
}
override def selectByUnique(email: TypoUnknownCitext): ConnectionIO[Option[UsersRow]] = {
sql"""select "email"::text
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))}
""".query(UsersRow.read).option
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ class DocumentRepoImpl extends DocumentRepo {
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(DocumentRow.jdbcDecoder).selectStream
}
override def selectByUnique(rowguid: TypoUUID): ZIO[ZConnection, Throwable, Option[DocumentRow]] = {
sql"""select "rowguid"
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)}
""".query(DocumentRow.jdbcDecoder).selectOne
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ class UsersRepoImpl extends UsersRepo {
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(UsersRow.jdbcDecoder).selectStream
}
override def selectByUnique(email: TypoUnknownCitext): ZIO[ZConnection, Throwable, Option[UsersRow]] = {
sql"""select "email"::text
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)}
""".query(UsersRow.jdbcDecoder).selectOne
Expand Down
9 changes: 7 additions & 2 deletions typo/src/scala/typo/internal/ComputedTable.scala
Original file line number Diff line number Diff line change
Expand Up @@ -194,9 +194,14 @@ case class ComputedTable(
).flatten,
dbTable.uniqueKeys
.map { uk =>
RepoMethod.SelectByUnique(dbTable.name, uk.cols.map(colName => cols.find(_.dbName == colName).get), names.RowName)
RepoMethod.SelectByUnique(
dbTable.name,
keyColumns = uk.cols.map(colName => cols.find(_.dbName == colName).get),
allColumns = cols,
rowType = names.RowName
)
}
.distinctByCompat(x => x.params.map(_.tpe)) // avoid erasure clashes
.distinctByCompat(x => x.keyColumns.map(_.tpe)) // avoid erasure clashes
)
val valid = maybeMethods.flatten.filter {
case _: RepoMethod.Mutator => !options.readonlyRepo.include(dbTable.name)
Expand Down
5 changes: 3 additions & 2 deletions typo/src/scala/typo/internal/RepoMethod.scala
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ object RepoMethod {

case class SelectByUnique(
relName: db.RelationName,
params: NonEmptyList[ComputedColumn],
keyColumns: NonEmptyList[ComputedColumn],
allColumns: NonEmptyList[ComputedColumn],
rowType: sc.Type
) extends Selector

Expand Down Expand Up @@ -128,7 +129,7 @@ object RepoMethod {
case _: SelectAll => "Select2"
case _: SelectById => "Select3"
case _: SelectAllByIds => "Select4"
case x: SelectByUnique => s"SelectByUnique(${x.params.map(_.name.value).mkString(", ")})"
case x: SelectByUnique => s"SelectByUnique(${x.keyColumns.map(_.name.value).mkString(", ")})"
case _: SelectByFieldValues => "SelectByFieldValues"
case _: UpdateFieldValues => "UpdateFieldValues"
case _: Update => "Update1"
Expand Down
14 changes: 7 additions & 7 deletions typo/src/scala/typo/internal/codegen/DbLibAnorm.scala
Original file line number Diff line number Diff line change
Expand Up @@ -172,8 +172,8 @@ class DbLibAnorm(pkg: sc.QIdent, inlineImplicits: Boolean, default: ComputedDefa
case _ =>
code"def selectByIds($idsParam)(implicit c: ${TypesJava.Connection}): ${TypesScala.List.of(rowType)}"
}
case RepoMethod.SelectByUnique(_, cols, rowType) =>
code"def selectByUnique(${cols.map(_.param.code).mkCode(", ")})(implicit c: ${TypesJava.Connection}): ${TypesScala.Option.of(rowType)}"
case RepoMethod.SelectByUnique(_, keyColumns, _, rowType) =>
code"def selectByUnique(${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 Expand Up @@ -240,11 +240,11 @@ class DbLibAnorm(pkg: sc.QIdent, inlineImplicits: Boolean, default: ComputedDefa
case RepoMethod.UpdateBuilder(relName, fieldsType, rowType) =>
code"${sc.Type.dsl.UpdateBuilder}(${sc.StrLit(relName.value)}, $fieldsType, $rowType.rowParser)"

case RepoMethod.SelectByUnique(relName, cols, rowType) =>
case RepoMethod.SelectByUnique(relName, keyColumns, allCols, rowType) =>
val sql = SQL {
code"""|select ${dbNames(cols, isRead = true)}
code"""|select ${dbNames(allCols, isRead = true)}
|from $relName
|where ${cols.map(c => code"${c.dbName.code} = ${runtimeInterpolateValue(c.name, c.tpe)}").mkCode(" AND ")}
|where ${keyColumns.map(c => code"${c.dbName.code} = ${runtimeInterpolateValue(c.name, c.tpe)}").mkCode(" AND ")}
|""".stripMargin
}

Expand Down Expand Up @@ -466,8 +466,8 @@ class DbLibAnorm(pkg: sc.QIdent, inlineImplicits: Boolean, default: ComputedDefa
code"map.get(${id.paramName})"
case RepoMethod.SelectAllByIds(_, _, _, idsParam, _) =>
code"${idsParam.name}.flatMap(map.get).toList"
case RepoMethod.SelectByUnique(_, cols, _) =>
code"map.values.find(v => ${cols.map(c => code"${c.name} == v.${c.name}").mkCode(" && ")})"
case RepoMethod.SelectByUnique(_, keyColumns, _, _) =>
code"map.values.find(v => ${keyColumns.map(c => code"${c.name} == v.${c.name}").mkCode(" && ")})"

case RepoMethod.SelectByFieldValues(_, cols, fieldValue, fieldValueOrIdsParam, _) =>
val cases = cols.map { col =>
Expand Down
14 changes: 7 additions & 7 deletions typo/src/scala/typo/internal/codegen/DbLibDoobie.scala
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ class DbLibDoobie(pkg: sc.QIdent, inlineImplicits: Boolean, default: ComputedDef
case _ =>
code"def selectByIds($idsParam): ${fs2Stream.of(ConnectionIO, rowType)}"
}
case RepoMethod.SelectByUnique(_, params, rowType) =>
code"def selectByUnique(${params.map(_.param.code).mkCode(", ")}): ${ConnectionIO.of(TypesScala.Option.of(rowType))}"
case RepoMethod.SelectByUnique(_, keyColumns, _, rowType) =>
code"def selectByUnique(${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 Expand Up @@ -135,11 +135,11 @@ class DbLibDoobie(pkg: sc.QIdent, inlineImplicits: Boolean, default: ComputedDef
code"""select $joinedColNames from $relName where ${code"${unaryId.col.dbName.code} = ANY(${runtimeInterpolateValue(idsParam.name, idsParam.tpe, forbidInline = true)})"}"""
)
code"""${query(sql, rowType)}.stream"""
case RepoMethod.SelectByUnique(relName, cols, rowType) =>
case RepoMethod.SelectByUnique(relName, keyColumns, allColumns, rowType) =>
val sql = SQL {
code"""|select ${dbNames(cols, isRead = true)}
code"""|select ${dbNames(allColumns, isRead = true)}
|from $relName
|where ${cols.map(c => code"${c.dbName.code} = ${runtimeInterpolateValue(c.name, c.tpe)}").mkCode(" AND ")}
|where ${keyColumns.map(c => code"${c.dbName.code} = ${runtimeInterpolateValue(c.name, c.tpe)}").mkCode(" AND ")}
|""".stripMargin
}
code"""${query(sql, rowType)}.option"""
Expand Down Expand Up @@ -333,8 +333,8 @@ class DbLibDoobie(pkg: sc.QIdent, inlineImplicits: Boolean, default: ComputedDef
code"$delayCIO(map.get(${id.paramName}))"
case RepoMethod.SelectAllByIds(_, _, _, idsParam, _) =>
code"$fs2Stream.emits(${idsParam.name}.flatMap(map.get).toList)"
case RepoMethod.SelectByUnique(_, cols, _) =>
code"${delayCIO}(map.values.find(v => ${cols.map(c => code"${c.name} == v.${c.name}").mkCode(" && ")}))"
case RepoMethod.SelectByUnique(_, keyColumns, _, _) =>
code"${delayCIO}(map.values.find(v => ${keyColumns.map(c => code"${c.name} == v.${c.name}").mkCode(" && ")}))"

case RepoMethod.SelectByFieldValues(_, cols, fieldValue, fieldValueOrIdsParam, _) =>
val cases = cols.map { col =>
Expand Down
14 changes: 7 additions & 7 deletions typo/src/scala/typo/internal/codegen/DbLibZioJdbc.scala
Original file line number Diff line number Diff line change
Expand Up @@ -193,8 +193,8 @@ class DbLibZioJdbc(pkg: sc.QIdent, inlineImplicits: Boolean, dslEnabled: Boolean
case _ =>
code"def selectByIds($idsParam): ${ZStream.of(ZConnection, Throwable, rowType)}"
}
case RepoMethod.SelectByUnique(_, params, rowType) =>
code"def selectByUnique(${params.map(_.param.code).mkCode(", ")}): ${ZIO.of(ZConnection, Throwable, TypesScala.Option.of(rowType))}"
case RepoMethod.SelectByUnique(_, keyColumns, _, rowType) =>
code"def selectByUnique(${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 Expand Up @@ -254,11 +254,11 @@ class DbLibZioJdbc(pkg: sc.QIdent, inlineImplicits: Boolean, dslEnabled: Boolean
code"""select $joinedColNames from $relName where ${unaryId.col.dbName} = ANY(${runtimeInterpolateValue(idsParam.name, idsParam.tpe)})"""
)
code"""$sql.query(${lookupJdbcDecoder(rowType)}).selectStream"""
case RepoMethod.SelectByUnique(relName, cols, rowType) =>
case RepoMethod.SelectByUnique(relName, keyColumns, allCols, rowType) =>
val sql = SQL {
code"""|select ${dbNames(cols, isRead = true)}
code"""|select ${dbNames(allCols, isRead = true)}
|from $relName
|where ${cols.map(c => code"${c.dbName} = ${runtimeInterpolateValue(c.name, c.tpe)}").mkCode(" AND ")}
|where ${keyColumns.map(c => code"${c.dbName} = ${runtimeInterpolateValue(c.name, c.tpe)}").mkCode(" AND ")}
|""".stripMargin
}
code"""$sql.query(${lookupJdbcDecoder(rowType)}).selectOne"""
Expand Down Expand Up @@ -450,8 +450,8 @@ class DbLibZioJdbc(pkg: sc.QIdent, inlineImplicits: Boolean, dslEnabled: Boolean
code"$ZIO.succeed(map.get(${id.paramName}))"
case RepoMethod.SelectAllByIds(_, _, _, idsParam, _) =>
code"$ZStream.fromIterable(${idsParam.name}.flatMap(map.get))"
case RepoMethod.SelectByUnique(_, cols, _) =>
code"$ZIO.succeed(map.values.find(v => ${cols.map(c => code"${c.name} == v.${c.name}").mkCode(" && ")}))"
case RepoMethod.SelectByUnique(_, keyColumns, _, _) =>
code"$ZIO.succeed(map.values.find(v => ${keyColumns.map(c => code"${c.name} == v.${c.name}").mkCode(" && ")}))"

case RepoMethod.SelectByFieldValues(_, cols, fieldValue, fieldValueOrIdsParam, _) =>
val cases = cols.map { col =>
Expand Down

0 comments on commit 17d53d1

Please sign in to comment.