Skip to content

Commit

Permalink
DSL: hide Structure in Fields.structure so there is less surface (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
oyvindberg authored Mar 12, 2024
1 parent 17d53d1 commit 89a58a0
Show file tree
Hide file tree
Showing 1,810 changed files with 16,513 additions and 21,601 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,35 @@ package hardcoded
package compositepk
package person

import typo.dsl.SqlExpr.FieldLikeNoHkt
import typo.dsl.SqlExpr.IdField
import typo.dsl.SqlExpr.OptField
import typo.dsl.Structure.Relation

trait PersonFields[Row] {
val one: IdField[Long, Row]
val two: IdField[Option[String], Row]
val name: OptField[String, Row]
}
object PersonFields extends PersonStructure[PersonRow](None, identity, (_, x) => x)

object PersonFields {
val structure: Relation[PersonFields, PersonRow, PersonRow] =
new Impl(None, identity, (_, x) => x)

private final class Impl[Row](val prefix: Option[String], val extract: Row => PersonRow, val merge: (Row, PersonRow) => Row)
extends Relation[PersonFields, PersonRow, Row] {

override val fields: PersonFields[Row] = new PersonFields[Row] {
override val one = new IdField[Long, Row](prefix, "one", None, Some("int8"))(x => extract(x).one, (row, value) => merge(row, extract(row).copy(one = value)))
override val two = new IdField[Option[String], Row](prefix, "two", None, None)(x => extract(x).two, (row, value) => merge(row, extract(row).copy(two = value)))
override val name = new OptField[String, Row](prefix, "name", None, None)(x => extract(x).name, (row, value) => merge(row, extract(row).copy(name = value)))
}

override val columns: List[FieldLikeNoHkt[?, Row]] =
List[FieldLikeNoHkt[?, Row]](fields.one, fields.two, fields.name)

override def copy[NewRow](prefix: Option[String], extract: NewRow => PersonRow, merge: (NewRow, PersonRow) => NewRow): Impl[NewRow] =
new Impl(prefix, extract, merge)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class PersonRepoImpl extends PersonRepo {
SQL"""delete from compositepk.person where "one" = ${ParameterValue(compositeId.one, null, ToStatement.longToStatement)} AND "two" = ${ParameterValue(compositeId.two, null, ToStatement.optionToStatement(ToStatement.stringToStatement, ParameterMetaData.StringParameterMetaData))}""".executeUpdate() > 0
}
override def delete: DeleteBuilder[PersonFields, PersonRow] = {
DeleteBuilder("compositepk.person", PersonFields)
DeleteBuilder("compositepk.person", PersonFields.structure)
}
override def insert(unsaved: PersonRow)(implicit c: Connection): PersonRow = {
SQL"""insert into compositepk.person("one", "two", "name")
Expand Down Expand Up @@ -75,7 +75,7 @@ class PersonRepoImpl extends PersonRepo {
streamingInsert(s"""COPY compositepk.person("name", "one", "two") FROM STDIN (DEFAULT '__DEFAULT_VALUE__')""", batchSize, unsaved)(PersonRowUnsaved.text, c)
}
override def select: SelectBuilder[PersonFields, PersonRow] = {
SelectBuilderSql("compositepk.person", PersonFields, PersonRow.rowParser)
SelectBuilderSql("compositepk.person", PersonFields.structure, PersonRow.rowParser)
}
override def selectAll(implicit c: Connection): List[PersonRow] = {
SQL"""select "one", "two", "name"
Expand Down Expand Up @@ -115,7 +115,7 @@ class PersonRepoImpl extends PersonRepo {
""".executeUpdate() > 0
}
override def update: UpdateBuilder[PersonFields, PersonRow] = {
UpdateBuilder("compositepk.person", PersonFields, PersonRow.rowParser)
UpdateBuilder("compositepk.person", PersonFields.structure, PersonRow.rowParser)
}
override def updateFieldValues(compositeId: PersonId, fieldValues: List[PersonFieldValue[?]])(implicit c: Connection): Boolean = {
fieldValues match {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class PersonRepoMock(toRow: Function1[PersonRowUnsaved, PersonRow],
map.remove(compositeId).isDefined
}
override def delete: DeleteBuilder[PersonFields, PersonRow] = {
DeleteBuilderMock(DeleteParams.empty, PersonFields, map)
DeleteBuilderMock(DeleteParams.empty, PersonFields.structure.fields, map)
}
override def insert(unsaved: PersonRow)(implicit c: Connection): PersonRow = {
val _ = if (map.contains(unsaved.compositeId))
Expand Down Expand Up @@ -54,7 +54,7 @@ class PersonRepoMock(toRow: Function1[PersonRowUnsaved, PersonRow],
unsaved.size.toLong
}
override def select: SelectBuilder[PersonFields, PersonRow] = {
SelectBuilderMock(PersonFields, () => map.values.toList, SelectParams.empty)
SelectBuilderMock(PersonFields.structure, () => map.values.toList, SelectParams.empty)
}
override def selectAll(implicit c: Connection): List[PersonRow] = {
map.values.toList
Expand All @@ -79,7 +79,7 @@ class PersonRepoMock(toRow: Function1[PersonRowUnsaved, PersonRow],
}
}
override def update: UpdateBuilder[PersonFields, PersonRow] = {
UpdateBuilderMock(UpdateParams.empty, PersonFields, map)
UpdateBuilderMock(UpdateParams.empty, PersonFields.structure.fields, map)
}
override def updateFieldValues(compositeId: PersonId, fieldValues: List[PersonFieldValue[?]])(implicit c: Connection): Boolean = {
map.get(compositeId) match {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,32 @@ package myschema
package football_club

import typo.dsl.SqlExpr.Field
import typo.dsl.SqlExpr.FieldLikeNoHkt
import typo.dsl.SqlExpr.IdField
import typo.dsl.Structure.Relation

trait FootballClubFields[Row] {
val id: IdField[FootballClubId, Row]
val name: Field[/* max 100 chars */ String, Row]
}
object FootballClubFields extends FootballClubStructure[FootballClubRow](None, identity, (_, x) => x)

object FootballClubFields {
val structure: Relation[FootballClubFields, FootballClubRow, FootballClubRow] =
new Impl(None, identity, (_, x) => x)

private final class Impl[Row](val prefix: Option[String], val extract: Row => FootballClubRow, val merge: (Row, FootballClubRow) => Row)
extends Relation[FootballClubFields, FootballClubRow, Row] {

override val fields: FootballClubFields[Row] = new FootballClubFields[Row] {
override val id = new IdField[FootballClubId, Row](prefix, "id", None, Some("int8"))(x => extract(x).id, (row, value) => merge(row, extract(row).copy(id = value)))
override val name = new Field[/* max 100 chars */ String, Row](prefix, "name", None, None)(x => extract(x).name, (row, value) => merge(row, extract(row).copy(name = value)))
}

override val columns: List[FieldLikeNoHkt[?, Row]] =
List[FieldLikeNoHkt[?, Row]](fields.id, fields.name)

override def copy[NewRow](prefix: Option[String], extract: NewRow => FootballClubRow, merge: (NewRow, FootballClubRow) => NewRow): Impl[NewRow] =
new Impl(prefix, extract, merge)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class FootballClubRepoImpl extends FootballClubRepo {
SQL"""delete from myschema.football_club where "id" = ${ParameterValue(id, null, FootballClubId.toStatement)}""".executeUpdate() > 0
}
override def delete: DeleteBuilder[FootballClubFields, FootballClubRow] = {
DeleteBuilder("myschema.football_club", FootballClubFields)
DeleteBuilder("myschema.football_club", FootballClubFields.structure)
}
override def insert(unsaved: FootballClubRow)(implicit c: Connection): FootballClubRow = {
SQL"""insert into myschema.football_club("id", "name")
Expand All @@ -41,7 +41,7 @@ class FootballClubRepoImpl extends FootballClubRepo {
streamingInsert(s"""COPY myschema.football_club("id", "name") FROM STDIN""", batchSize, unsaved)(FootballClubRow.text, c)
}
override def select: SelectBuilder[FootballClubFields, FootballClubRow] = {
SelectBuilderSql("myschema.football_club", FootballClubFields, FootballClubRow.rowParser)
SelectBuilderSql("myschema.football_club", FootballClubFields.structure, FootballClubRow.rowParser)
}
override def selectAll(implicit c: Connection): List[FootballClubRow] = {
SQL"""select "id", "name"
Expand Down Expand Up @@ -87,7 +87,7 @@ class FootballClubRepoImpl extends FootballClubRepo {
""".executeUpdate() > 0
}
override def update: UpdateBuilder[FootballClubFields, FootballClubRow] = {
UpdateBuilder("myschema.football_club", FootballClubFields, FootballClubRow.rowParser)
UpdateBuilder("myschema.football_club", FootballClubFields.structure, FootballClubRow.rowParser)
}
override def updateFieldValues(id: FootballClubId, fieldValues: List[FootballClubFieldValue[?]])(implicit c: Connection): Boolean = {
fieldValues match {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class FootballClubRepoMock(map: scala.collection.mutable.Map[FootballClubId, Foo
map.remove(id).isDefined
}
override def delete: DeleteBuilder[FootballClubFields, FootballClubRow] = {
DeleteBuilderMock(DeleteParams.empty, FootballClubFields, map)
DeleteBuilderMock(DeleteParams.empty, FootballClubFields.structure.fields, map)
}
override def insert(unsaved: FootballClubRow)(implicit c: Connection): FootballClubRow = {
val _ = if (map.contains(unsaved.id))
Expand All @@ -42,7 +42,7 @@ class FootballClubRepoMock(map: scala.collection.mutable.Map[FootballClubId, Foo
unsaved.size.toLong
}
override def select: SelectBuilder[FootballClubFields, FootballClubRow] = {
SelectBuilderMock(FootballClubFields, () => map.values.toList, SelectParams.empty)
SelectBuilderMock(FootballClubFields.structure, () => map.values.toList, SelectParams.empty)
}
override def selectAll(implicit c: Connection): List[FootballClubRow] = {
map.values.toList
Expand All @@ -69,7 +69,7 @@ class FootballClubRepoMock(map: scala.collection.mutable.Map[FootballClubId, Foo
}
}
override def update: UpdateBuilder[FootballClubFields, FootballClubRow] = {
UpdateBuilderMock(UpdateParams.empty, FootballClubFields, map)
UpdateBuilderMock(UpdateParams.empty, FootballClubFields.structure.fields, map)
}
override def updateFieldValues(id: FootballClubId, fieldValues: List[FootballClubFieldValue[?]])(implicit c: Connection): Boolean = {
map.get(id) match {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,30 @@ package hardcoded
package myschema
package marital_status

import typo.dsl.SqlExpr.FieldLikeNoHkt
import typo.dsl.SqlExpr.IdField
import typo.dsl.Structure.Relation

trait MaritalStatusFields[Row] {
val id: IdField[MaritalStatusId, Row]
}
object MaritalStatusFields extends MaritalStatusStructure[MaritalStatusRow](None, identity, (_, x) => x)

object MaritalStatusFields {
val structure: Relation[MaritalStatusFields, MaritalStatusRow, MaritalStatusRow] =
new Impl(None, identity, (_, x) => x)

private final class Impl[Row](val prefix: Option[String], val extract: Row => MaritalStatusRow, val merge: (Row, MaritalStatusRow) => Row)
extends Relation[MaritalStatusFields, MaritalStatusRow, Row] {

override val fields: MaritalStatusFields[Row] = new MaritalStatusFields[Row] {
override val id = new IdField[MaritalStatusId, Row](prefix, "id", None, Some("int8"))(x => extract(x).id, (row, value) => merge(row, extract(row).copy(id = value)))
}

override val columns: List[FieldLikeNoHkt[?, Row]] =
List[FieldLikeNoHkt[?, Row]](fields.id)

override def copy[NewRow](prefix: Option[String], extract: NewRow => MaritalStatusRow, merge: (NewRow, MaritalStatusRow) => NewRow): Impl[NewRow] =
new Impl(prefix, extract, merge)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class MaritalStatusRepoImpl extends MaritalStatusRepo {
SQL"""delete from myschema.marital_status where "id" = ${ParameterValue(id, null, MaritalStatusId.toStatement)}""".executeUpdate() > 0
}
override def delete: DeleteBuilder[MaritalStatusFields, MaritalStatusRow] = {
DeleteBuilder("myschema.marital_status", MaritalStatusFields)
DeleteBuilder("myschema.marital_status", MaritalStatusFields.structure)
}
override def insert(unsaved: MaritalStatusRow)(implicit c: Connection): MaritalStatusRow = {
SQL"""insert into myschema.marital_status("id")
Expand All @@ -40,7 +40,7 @@ class MaritalStatusRepoImpl extends MaritalStatusRepo {
streamingInsert(s"""COPY myschema.marital_status("id") FROM STDIN""", batchSize, unsaved)(MaritalStatusRow.text, c)
}
override def select: SelectBuilder[MaritalStatusFields, MaritalStatusRow] = {
SelectBuilderSql("myschema.marital_status", MaritalStatusFields, MaritalStatusRow.rowParser)
SelectBuilderSql("myschema.marital_status", MaritalStatusFields.structure, MaritalStatusRow.rowParser)
}
override def selectAll(implicit c: Connection): List[MaritalStatusRow] = {
SQL"""select "id"
Expand Down Expand Up @@ -78,7 +78,7 @@ class MaritalStatusRepoImpl extends MaritalStatusRepo {

}
override def update: UpdateBuilder[MaritalStatusFields, MaritalStatusRow] = {
UpdateBuilder("myschema.marital_status", MaritalStatusFields, MaritalStatusRow.rowParser)
UpdateBuilder("myschema.marital_status", MaritalStatusFields.structure, MaritalStatusRow.rowParser)
}
override def upsert(unsaved: MaritalStatusRow)(implicit c: Connection): MaritalStatusRow = {
SQL"""insert into myschema.marital_status("id")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class MaritalStatusRepoMock(map: scala.collection.mutable.Map[MaritalStatusId, M
map.remove(id).isDefined
}
override def delete: DeleteBuilder[MaritalStatusFields, MaritalStatusRow] = {
DeleteBuilderMock(DeleteParams.empty, MaritalStatusFields, map)
DeleteBuilderMock(DeleteParams.empty, MaritalStatusFields.structure.fields, map)
}
override def insert(unsaved: MaritalStatusRow)(implicit c: Connection): MaritalStatusRow = {
val _ = if (map.contains(unsaved.id))
Expand All @@ -42,7 +42,7 @@ class MaritalStatusRepoMock(map: scala.collection.mutable.Map[MaritalStatusId, M
unsaved.size.toLong
}
override def select: SelectBuilder[MaritalStatusFields, MaritalStatusRow] = {
SelectBuilderMock(MaritalStatusFields, () => map.values.toList, SelectParams.empty)
SelectBuilderMock(MaritalStatusFields.structure, () => map.values.toList, SelectParams.empty)
}
override def selectAll(implicit c: Connection): List[MaritalStatusRow] = {
map.values.toList
Expand All @@ -59,7 +59,7 @@ class MaritalStatusRepoMock(map: scala.collection.mutable.Map[MaritalStatusId, M
}.toList
}
override def update: UpdateBuilder[MaritalStatusFields, MaritalStatusRow] = {
UpdateBuilderMock(UpdateParams.empty, MaritalStatusFields, map)
UpdateBuilderMock(UpdateParams.empty, MaritalStatusFields.structure.fields, map)
}
override def upsert(unsaved: MaritalStatusRow)(implicit c: Connection): MaritalStatusRow = {
map.put(unsaved.id, unsaved): @nowarn
Expand Down

This file was deleted.

Loading

0 comments on commit 89a58a0

Please sign in to comment.