Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Elaborate composite ids #111

Merged
merged 6 commits into from
Jun 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ import scala.collection.immutable.ListMap
import scala.util.Try

/** Type for the composite primary key of table `compositepk.person` */
case class PersonId(one: Long, two: Option[String])
case class PersonId(
one: Long,
two: Option[String]
)
object PersonId {
implicit def ordering(implicit O0: Ordering[Option[String]]): Ordering[PersonId] = Ordering.by(x => (x.one, x.two))
implicit lazy val reads: Reads[PersonId] = Reads[PersonId](json => JsResult.fromTry(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ case class PersonRow(
name: Option[String]
){
val compositeId: PersonId = PersonId(one, two)
val id = compositeId
def toUnsavedRow(one: Defaulted[Long], two: Defaulted[Option[String]]): PersonRowUnsaved =
PersonRowUnsaved(name, one, two)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ class TestInsert(random: Random) {
one: Defaulted[Long] = Defaulted.UseDefault,
two: Defaulted[Option[String]] = Defaulted.UseDefault
)(implicit c: Connection): PersonRow = (new PersonRepoImpl).insert(new PersonRowUnsaved(name = name, one = one, two = two))
def myschemaFootballClub(id: FootballClubId, name: /* max 100 chars */ String = random.alphanumeric.take(20).mkString)(implicit c: Connection): FootballClubRow = (new FootballClubRepoImpl).insert(new FootballClubRow(id = id, name = name))
def myschemaMaritalStatus(id: MaritalStatusId)(implicit c: Connection): MaritalStatusRow = (new MaritalStatusRepoImpl).insert(new MaritalStatusRow(id = id))
def myschemaFootballClub(id: FootballClubId = FootballClubId(random.nextLong()), name: /* max 100 chars */ String = random.alphanumeric.take(20).mkString)(implicit c: Connection): FootballClubRow = (new FootballClubRepoImpl).insert(new FootballClubRow(id = id, name = name))
def myschemaMaritalStatus(id: MaritalStatusId = MaritalStatusId(random.nextLong()))(implicit c: Connection): MaritalStatusRow = (new MaritalStatusRepoImpl).insert(new MaritalStatusRow(id = id))
def myschemaPerson(favouriteFootballClubId: FootballClubId,
name: /* max 100 chars */ String = random.alphanumeric.take(20).mkString,
nickName: Option[/* max 30 chars */ String] = if (random.nextBoolean()) None else Some(random.alphanumeric.take(20).mkString),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ class TestInsert(random: Random) {
one: Defaulted[Long] = Defaulted.UseDefault,
two: Defaulted[Option[String]] = Defaulted.UseDefault
)(implicit c: Connection): PersonRow = (new PersonRepoImpl).insert(new PersonRowUnsaved(name = name, one = one, two = two))
def myschemaFootballClub(id: FootballClubId, name: /* max 100 chars */ String = random.alphanumeric.take(20).mkString)(implicit c: Connection): FootballClubRow = (new FootballClubRepoImpl).insert(new FootballClubRow(id = id, name = name))
def myschemaMaritalStatus(id: MaritalStatusId)(implicit c: Connection): MaritalStatusRow = (new MaritalStatusRepoImpl).insert(new MaritalStatusRow(id = id))
def myschemaFootballClub(id: FootballClubId = FootballClubId(random.nextLong()), name: /* max 100 chars */ String = random.alphanumeric.take(20).mkString)(implicit c: Connection): FootballClubRow = (new FootballClubRepoImpl).insert(new FootballClubRow(id = id, name = name))
def myschemaMaritalStatus(id: MaritalStatusId = MaritalStatusId(random.nextLong()))(implicit c: Connection): MaritalStatusRow = (new MaritalStatusRepoImpl).insert(new MaritalStatusRow(id = id))
def myschemaPerson(favouriteFootballClubId: FootballClubId,
name: /* max 100 chars */ String = random.alphanumeric.take(20).mkString,
nickName: Option[/* max 30 chars */ String] = if (random.nextBoolean()) None else Some(random.alphanumeric.take(20).mkString),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ import scala.collection.immutable.ListMap
import scala.util.Try

/** Type for the composite primary key of table `compositepk.person` */
case class PersonId(one: Long, two: Option[String])
case class PersonId(
one: Long,
two: Option[String]
)
object PersonId {
implicit def ordering(implicit O0: Ordering[Option[String]]): Ordering[PersonId] = Ordering.by(x => (x.one, x.two))
implicit lazy val reads: Reads[PersonId] = Reads[PersonId](json => JsResult.fromTry(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ case class PersonRow(
name: Option[String]
){
val compositeId: PersonId = PersonId(one, two)
val id = compositeId
def toUnsavedRow(one: Defaulted[Long], two: Defaulted[Option[String]]): PersonRowUnsaved =
PersonRowUnsaved(name, one, two)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ class TestInsert(random: Random) {
one: Defaulted[Long] = Defaulted.UseDefault,
two: Defaulted[Option[String]] = Defaulted.UseDefault
): ConnectionIO[PersonRow] = (new PersonRepoImpl).insert(new PersonRowUnsaved(name = name, one = one, two = two))
def myschemaFootballClub(id: FootballClubId, name: /* max 100 chars */ String = random.alphanumeric.take(20).mkString): ConnectionIO[FootballClubRow] = (new FootballClubRepoImpl).insert(new FootballClubRow(id = id, name = name))
def myschemaMaritalStatus(id: MaritalStatusId): ConnectionIO[MaritalStatusRow] = (new MaritalStatusRepoImpl).insert(new MaritalStatusRow(id = id))
def myschemaFootballClub(id: FootballClubId = FootballClubId(random.nextLong()), name: /* max 100 chars */ String = random.alphanumeric.take(20).mkString): ConnectionIO[FootballClubRow] = (new FootballClubRepoImpl).insert(new FootballClubRow(id = id, name = name))
def myschemaMaritalStatus(id: MaritalStatusId = MaritalStatusId(random.nextLong())): ConnectionIO[MaritalStatusRow] = (new MaritalStatusRepoImpl).insert(new MaritalStatusRow(id = id))
def myschemaPerson(favouriteFootballClubId: FootballClubId,
name: /* max 100 chars */ String = random.alphanumeric.take(20).mkString,
nickName: Option[/* max 30 chars */ String] = if (random.nextBoolean()) None else Some(random.alphanumeric.take(20).mkString),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ import io.circe.Decoder
import io.circe.Encoder

/** Type for the composite primary key of table `compositepk.person` */
case class PersonId(one: Long, two: Option[String])
case class PersonId(
one: Long,
two: Option[String]
)
object PersonId {
implicit lazy val decoder: Decoder[PersonId] = Decoder.forProduct2[PersonId, Long, Option[String]]("one", "two")(PersonId.apply)(Decoder.decodeLong, Decoder.decodeOption(Decoder.decodeString))
implicit lazy val encoder: Encoder[PersonId] = Encoder.forProduct2[PersonId, Long, Option[String]]("one", "two")(x => (x.one, x.two))(Encoder.encodeLong, Encoder.encodeOption(Encoder.encodeString))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ case class PersonRow(
name: Option[String]
){
val compositeId: PersonId = PersonId(one, two)
val id = compositeId
def toUnsavedRow(one: Defaulted[Long], two: Defaulted[Option[String]]): PersonRowUnsaved =
PersonRowUnsaved(name, one, two)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ class TestInsert(random: Random) {
one: Defaulted[Long] = Defaulted.UseDefault,
two: Defaulted[Option[String]] = Defaulted.UseDefault
): ConnectionIO[PersonRow] = (new PersonRepoImpl).insert(new PersonRowUnsaved(name = name, one = one, two = two))
def myschemaFootballClub(id: FootballClubId, name: /* max 100 chars */ String = random.alphanumeric.take(20).mkString): ConnectionIO[FootballClubRow] = (new FootballClubRepoImpl).insert(new FootballClubRow(id = id, name = name))
def myschemaMaritalStatus(id: MaritalStatusId): ConnectionIO[MaritalStatusRow] = (new MaritalStatusRepoImpl).insert(new MaritalStatusRow(id = id))
def myschemaFootballClub(id: FootballClubId = FootballClubId(random.nextLong()), name: /* max 100 chars */ String = random.alphanumeric.take(20).mkString): ConnectionIO[FootballClubRow] = (new FootballClubRepoImpl).insert(new FootballClubRow(id = id, name = name))
def myschemaMaritalStatus(id: MaritalStatusId = MaritalStatusId(random.nextLong())): ConnectionIO[MaritalStatusRow] = (new MaritalStatusRepoImpl).insert(new MaritalStatusRow(id = id))
def myschemaPerson(favouriteFootballClubId: FootballClubId,
name: /* max 100 chars */ String = random.alphanumeric.take(20).mkString,
nickName: Option[/* max 30 chars */ String] = if (random.nextBoolean()) None else Some(random.alphanumeric.take(20).mkString),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ import io.circe.Decoder
import io.circe.Encoder

/** Type for the composite primary key of table `compositepk.person` */
case class PersonId(one: Long, two: Option[String])
case class PersonId(
one: Long,
two: Option[String]
)
object PersonId {
implicit lazy val decoder: Decoder[PersonId] = Decoder.forProduct2[PersonId, Long, Option[String]]("one", "two")(PersonId.apply)(Decoder.decodeLong, Decoder.decodeOption(Decoder.decodeString))
implicit lazy val encoder: Encoder[PersonId] = Encoder.forProduct2[PersonId, Long, Option[String]]("one", "two")(x => (x.one, x.two))(Encoder.encodeLong, Encoder.encodeOption(Encoder.encodeString))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ case class PersonRow(
name: Option[String]
){
val compositeId: PersonId = PersonId(one, two)
val id = compositeId
def toUnsavedRow(one: Defaulted[Long], two: Defaulted[Option[String]]): PersonRowUnsaved =
PersonRowUnsaved(name, one, two)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ class TestInsert(random: Random) {
one: Defaulted[Long] = Defaulted.UseDefault,
two: Defaulted[Option[String]] = Defaulted.UseDefault
): ZIO[ZConnection, Throwable, PersonRow] = (new PersonRepoImpl).insert(new PersonRowUnsaved(name = name, one = one, two = two))
def myschemaFootballClub(id: FootballClubId, name: /* max 100 chars */ String = random.alphanumeric.take(20).mkString): ZIO[ZConnection, Throwable, FootballClubRow] = (new FootballClubRepoImpl).insert(new FootballClubRow(id = id, name = name))
def myschemaMaritalStatus(id: MaritalStatusId): ZIO[ZConnection, Throwable, MaritalStatusRow] = (new MaritalStatusRepoImpl).insert(new MaritalStatusRow(id = id))
def myschemaFootballClub(id: FootballClubId = FootballClubId(random.nextLong()), name: /* max 100 chars */ String = random.alphanumeric.take(20).mkString): ZIO[ZConnection, Throwable, FootballClubRow] = (new FootballClubRepoImpl).insert(new FootballClubRow(id = id, name = name))
def myschemaMaritalStatus(id: MaritalStatusId = MaritalStatusId(random.nextLong())): ZIO[ZConnection, Throwable, MaritalStatusRow] = (new MaritalStatusRepoImpl).insert(new MaritalStatusRow(id = id))
def myschemaPerson(favouriteFootballClubId: FootballClubId,
name: /* max 100 chars */ String = random.alphanumeric.take(20).mkString,
nickName: Option[/* max 30 chars */ String] = if (random.nextBoolean()) None else Some(random.alphanumeric.take(20).mkString),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ import zio.json.ast.Json
import zio.json.internal.Write

/** Type for the composite primary key of table `compositepk.person` */
case class PersonId(one: Long, two: Option[String])
case class PersonId(
one: Long,
two: Option[String]
)
object PersonId {
implicit lazy val jsonDecoder: JsonDecoder[PersonId] = JsonDecoder[Json.Obj].mapOrFail { jsonObj =>
val one = jsonObj.get("one").toRight("Missing field 'one'").flatMap(_.as(JsonDecoder.long))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ case class PersonRow(
name: Option[String]
){
val compositeId: PersonId = PersonId(one, two)
val id = compositeId
def toUnsavedRow(one: Defaulted[Long], two: Defaulted[Option[String]]): PersonRowUnsaved =
PersonRowUnsaved(name, one, two)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ class TestInsert(random: Random) {
one: Defaulted[Long] = Defaulted.UseDefault,
two: Defaulted[Option[String]] = Defaulted.UseDefault
): ZIO[ZConnection, Throwable, PersonRow] = (new PersonRepoImpl).insert(new PersonRowUnsaved(name = name, one = one, two = two))
def myschemaFootballClub(id: FootballClubId, name: /* max 100 chars */ String = random.alphanumeric.take(20).mkString): ZIO[ZConnection, Throwable, FootballClubRow] = (new FootballClubRepoImpl).insert(new FootballClubRow(id = id, name = name))
def myschemaMaritalStatus(id: MaritalStatusId): ZIO[ZConnection, Throwable, MaritalStatusRow] = (new MaritalStatusRepoImpl).insert(new MaritalStatusRow(id = id))
def myschemaFootballClub(id: FootballClubId = FootballClubId(random.nextLong()), name: /* max 100 chars */ String = random.alphanumeric.take(20).mkString): ZIO[ZConnection, Throwable, FootballClubRow] = (new FootballClubRepoImpl).insert(new FootballClubRow(id = id, name = name))
def myschemaMaritalStatus(id: MaritalStatusId = MaritalStatusId(random.nextLong())): ZIO[ZConnection, Throwable, MaritalStatusRow] = (new MaritalStatusRepoImpl).insert(new MaritalStatusRow(id = id))
def myschemaPerson(favouriteFootballClubId: FootballClubId,
name: /* max 100 chars */ String = random.alphanumeric.take(20).mkString,
nickName: Option[/* max 30 chars */ String] = if (random.nextBoolean()) None else Some(random.alphanumeric.take(20).mkString),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ import zio.json.ast.Json
import zio.json.internal.Write

/** Type for the composite primary key of table `compositepk.person` */
case class PersonId(one: Long, two: Option[String])
case class PersonId(
one: Long,
two: Option[String]
)
object PersonId {
implicit lazy val jsonDecoder: JsonDecoder[PersonId] = JsonDecoder[Json.Obj].mapOrFail { jsonObj =>
val one = jsonObj.get("one").toRight("Missing field 'one'").flatMap(_.as(JsonDecoder.long))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ case class PersonRow(
name: Option[String]
){
val compositeId: PersonId = PersonId(one, two)
val id = compositeId
def toUnsavedRow(one: Defaulted[Long], two: Defaulted[Option[String]]): PersonRowUnsaved =
PersonRowUnsaved(name, one, two)
}
Expand Down
8 changes: 7 additions & 1 deletion site-in/other-features/testing-with-random-values.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ c.setAutoCommit(false)
```

```scala mdoc
import adventureworks.customtypes.{Defaulted, TypoXml}
import adventureworks.customtypes.{Defaulted, TypoShort, TypoLocalDateTime, TypoXml}
import adventureworks.production.unitmeasure.UnitmeasureId
import adventureworks.TestInsert

Expand All @@ -41,6 +41,12 @@ val productCategory = testInsert.productionProductcategory()
val productSubcategory = testInsert.productionProductsubcategory(productCategory.productcategoryid)
val productModel = testInsert.productionProductmodel(catalogdescription = Some(new TypoXml("<xml/>")), instructions = Some(new TypoXml("<instructions/>")))
testInsert.productionProduct(
safetystocklevel = TypoShort(1),
reorderpoint = TypoShort(1),
standardcost = BigDecimal(1),
listprice = BigDecimal(1),
daystomanufacture = 10,
sellstartdate = TypoLocalDateTime.now,
sizeunitmeasurecode = Some(unitmeasure.unitmeasurecode),
weightunitmeasurecode = Some(unitmeasure.unitmeasurecode),
`class` = Some("H "),
Expand Down
2 changes: 1 addition & 1 deletion site-in/patterns/multi-repo.md
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ import scala.util.Random
val testInsert = new TestInsert(new Random(1))

val businessentityRow = testInsert.personBusinessentity()
val personRow = testInsert.personPerson(businessentityRow.businessentityid, FirstName("name"), persontype = "SC")
val personRow = testInsert.personPerson(businessentityRow.businessentityid, persontype = "SC", FirstName("name"))
val countryregionRow = testInsert.personCountryregion(CountryregionId("NOR"))
val salesterritoryRow = testInsert.salesSalesterritory(countryregionRow.countryregioncode)
val stateprovinceRow = testInsert.personStateprovince(countryregionRow.countryregioncode, salesterritoryRow.territoryid)
Expand Down
2 changes: 1 addition & 1 deletion typo-dsl-anorm/src/scala/typo/dsl/SqlExpr.scala
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ object SqlExpr {
}

object CompositeIn {
case class TuplePart[Tuple, T, Row](field: IdField[T, Row])(val extract: Tuple => T)(implicit val asConst: Const.As[Array[T], Required], val CT: ClassTag[T])
case class TuplePart[Tuple, T, Row](field: FieldLike[T, Required, Row])(val extract: Tuple => T)(implicit val asConst: Const.As[Array[T], Required], val CT: ClassTag[T])
}

case class RowExpr(exprs: List[SqlExpr.SqlExprNoHkt[?]]) extends SqlExpr[List[?], Required] {
Expand Down
2 changes: 1 addition & 1 deletion typo-dsl-doobie/src/scala/typo/dsl/SqlExpr.scala
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ object SqlExpr {
}

object CompositeIn {
case class TuplePart[Tuple, T, Row](field: IdField[T, Row])(val extract: Tuple => T)(implicit val asConst: Const.As[Array[T], Required], val CT: ClassTag[T])
case class TuplePart[Tuple, T, Row](field: FieldLike[T, Required, Row])(val extract: Tuple => T)(implicit val asConst: Const.As[Array[T], Required], val CT: ClassTag[T])
}

case class RowExpr(exprs: List[SqlExpr.SqlExprNoHkt[?]]) extends SqlExpr[List[?], Required] {
Expand Down
2 changes: 1 addition & 1 deletion typo-dsl-zio-jdbc/src/scala/typo/dsl/SqlExpr.scala
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ object SqlExpr {
}

object CompositeIn {
case class TuplePart[Tuple, T, Row](field: IdField[T, Row])(val extract: Tuple => T)(implicit val asConst: Const.As[Array[T], Required], val CT: ClassTag[T])
case class TuplePart[Tuple, T, Row](field: FieldLike[T, Required, Row])(val extract: Tuple => T)(implicit val asConst: Const.As[Array[T], Required], val CT: ClassTag[T])
}

case class RowExpr(exprs: List[SqlExpr.SqlExprNoHkt[?]]) extends SqlExpr[List[?], Required] {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ case class DepartmentRow(
/** Default: now() */
modifieddate: TypoLocalDateTime
){
val id = departmentid
def toUnsavedRow(departmentid: Defaulted[DepartmentId], modifieddate: Defaulted[TypoLocalDateTime] = Defaulted.Provided(this.modifieddate)): DepartmentRowUnsaved =
DepartmentRowUnsaved(name, groupname, departmentid, modifieddate)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ case class EmployeeRow(
Default: '/'::character varying */
organizationnode: Option[String]
){
val id = businessentityid
def toUnsavedRow(salariedflag: Defaulted[Flag] = Defaulted.Provided(this.salariedflag), vacationhours: Defaulted[TypoShort] = Defaulted.Provided(this.vacationhours), sickleavehours: Defaulted[TypoShort] = Defaulted.Provided(this.sickleavehours), currentflag: Defaulted[Flag] = Defaulted.Provided(this.currentflag), rowguid: Defaulted[TypoUUID] = Defaulted.Provided(this.rowguid), modifieddate: Defaulted[TypoLocalDateTime] = Defaulted.Provided(this.modifieddate), organizationnode: Defaulted[Option[String]] = Defaulted.Provided(this.organizationnode)): EmployeeRowUnsaved =
EmployeeRowUnsaved(businessentityid, nationalidnumber, loginid, jobtitle, birthdate, maritalstatus, gender, hiredate, salariedflag, vacationhours, sickleavehours, currentflag, rowguid, modifieddate, organizationnode)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,12 @@ import scala.collection.immutable.ListMap
import scala.util.Try

/** Type for the composite primary key of table `humanresources.employeedepartmenthistory` */
case class EmployeedepartmenthistoryId(businessentityid: BusinessentityId, startdate: TypoLocalDate, departmentid: DepartmentId, shiftid: ShiftId)
case class EmployeedepartmenthistoryId(
businessentityid: BusinessentityId,
startdate: TypoLocalDate,
departmentid: DepartmentId,
shiftid: ShiftId
)
object EmployeedepartmenthistoryId {
implicit def ordering(implicit O0: Ordering[TypoLocalDate]): Ordering[EmployeedepartmenthistoryId] = Ordering.by(x => (x.businessentityid, x.startdate, x.departmentid, x.shiftid))
implicit lazy val reads: Reads[EmployeedepartmenthistoryId] = Reads[EmployeedepartmenthistoryId](json => JsResult.fromTry(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ case class EmployeedepartmenthistoryRow(
modifieddate: TypoLocalDateTime
){
val compositeId: EmployeedepartmenthistoryId = EmployeedepartmenthistoryId(businessentityid, startdate, departmentid, shiftid)
val id = compositeId
def toUnsavedRow(modifieddate: Defaulted[TypoLocalDateTime] = Defaulted.Provided(this.modifieddate)): EmployeedepartmenthistoryRowUnsaved =
EmployeedepartmenthistoryRowUnsaved(businessentityid, departmentid, shiftid, startdate, enddate, modifieddate)
}
Expand Down
Loading
Loading