Skip to content

Commit

Permalink
success
Browse files Browse the repository at this point in the history
  • Loading branch information
ValdemarGr committed Sep 2, 2023
1 parent ce660da commit 7cacc82
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 53 deletions.
33 changes: 33 additions & 0 deletions db.sql
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,21 @@ create table entity (
height int not null
);

create table pet (
id uuid not null,
name text not null
);

create table contract_entity (
contract_id uuid not null,
entity_id uuid not null
);

create table pet_entity (
pet_id uuid not null,
entity_id uuid not null
);

insert into contract (id, name) values (
'1ff0ca77-c13f-4af8-9166-72373f309247',
'Contract 1 BMW X3 Turbo'
Expand All @@ -40,4 +50,27 @@ insert into contract_entity (contract_id, entity_id) values (
), (
'1ff0ca77-c13f-4af8-9166-72373f309247',
'e787e1b5-4edb-4519-8bd7-8018946c1e2a'
);

insert into pet (id, name) values (
'1ff0ca77-c13f-4af8-9166-72373f309247',
'Dog'
), (
'1ff0ca77-c13f-4af8-9166-72373f309248',
'Cat'
);

-- Both john and jane own Dog and Cat
insert into pet_entity (pet_id, entity_id) values (
'1ff0ca77-c13f-4af8-9166-72373f309247',
'c4958a82-7ba8-498e-8220-a5b10c047229'
), (
'1ff0ca77-c13f-4af8-9166-72373f309248',
'c4958a82-7ba8-498e-8220-a5b10c047229'
), (
'1ff0ca77-c13f-4af8-9166-72373f309247',
'e787e1b5-4edb-4519-8bd7-8018946c1e2a'
), (
'1ff0ca77-c13f-4af8-9166-72373f309248',
'e787e1b5-4edb-4519-8bd7-8018946c1e2a'
);
73 changes: 45 additions & 28 deletions modules/relational/src/main/scala/gql/relational/ExampleImpl.scala
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,13 @@ object SkunkSchema extends QueryAlgebra with QueryDsl {

def runField[F[_]: MonadCancelThrow, G[_], I, B, ArgType](pool: Resource[F, Session[F]], arg: Arg[ArgType])(
q: (NonEmptyList[I], ArgType) => Query[G, (Query.Select[I], B)]
)(implicit tpe: => Out[F, G[QueryResult[B]]]) =
)(implicit tpe: => Out[F, G[QueryResult[B]]]) =
Field(resolveQuery(EmptyableArg.Lift(arg), q, SkunkRunQuery(pool)), Eval.later(tpe))

def runField[F[_]: MonadCancelThrow, G[_], I, B](pool: Resource[F, Session[F]])(
q: NonEmptyList[I] => Query[G, (Query.Select[I], B)]
)(implicit tpe: => Out[F, G[QueryResult[B]]]) =
Field(resolveQuery[F, G, I, B, Unit](EmptyableArg.Empty, (i, _) => q(i), SkunkRunQuery(pool)), Eval.later(tpe))
q: NonEmptyList[I] => Query[G, (Query.Select[I], B)]
)(implicit tpe: => Out[F, G[QueryResult[B]]]) =
Field(resolveQuery[F, G, I, B, Unit](EmptyableArg.Empty, (i, _) => q(i), SkunkRunQuery(pool)), Eval.later(tpe))

trait SkunkTable[A] extends Table[A] {
def aliased(x: Fragment[Void]): Fragment[Void] =
Expand Down Expand Up @@ -109,12 +109,52 @@ class MySchema(pool: Resource[IO, Session[IO]]) {
}
val entityTable2 = table(EntityTable2)

case class PetTable(alias: String) extends SkunkTable[UUID] {
def table = void"pet"
def groupingKey = void"id"
def groupingKeyDecoder: Decoder[UUID] = uuid

val (id, selId) = sel("id", uuid)
val (name, selName) = sel("name", text)
}
val petTable = table(PetTable)

case class PetEntityTable(alias: String) extends SkunkTable[UUID] {
def table = void"pet_entity"
def groupingKey = void"pet_id"
def groupingKeyDecoder: Decoder[UUID] = uuid

val (petId, selPetId) = sel("pet_id", uuid)
val (entityId, selEntityId) = sel("entity_id", uuid)
}
val petEntityTable = table(PetEntityTable)

implicit lazy val pet: Type[IO, QueryResult[PetTable]] = tpe[IO, QueryResult[PetTable]](
"Pet",
"name" -> query(_.selName),
"id" -> query(_.selId)
)

implicit lazy val entity2: Type[IO, QueryResult[EntityTable2]] = tpe[IO, QueryResult[EntityTable2]](
"Entity",
"name" -> query(_.selName),
"id" -> query(_.selId),
"age" -> query(_.selAge),
"height" -> query(_.selHeight)
"height" -> query(_.selHeight),
"pets" -> queryAndThen[IO, Lambda[X => X], EntityTable2, UUID, List[QueryResult[PetTable]]](_.selId)(
_.andThen(
resolveQuery(
EmptyableArg.Empty,
{ (is: NonEmptyList[UUID], _: Unit) =>
for {
pe <- petEntityTable.join[List](pe => sql"${pe.entityId} in ${uuid.list(is.size).values}".apply(is.toList))
p <- petTable.join(p => sql"${p.id} = ${pe.petId}".apply(Void))
} yield (pe.selEntityId, p)
},
SkunkRunQuery(pool)
)
)
)
)

implicit lazy val entity: Type[IO, QueryResult[EntityTable]] = tpe[IO, QueryResult[EntityTable]](
Expand All @@ -125,21 +165,6 @@ class MySchema(pool: Resource[IO, Session[IO]]) {
"height" -> query(_.selHeight)
)

// List[ResultSet] => G[A]
// 1:1 List[ResultSet] => (PrimaryKey, List[ResultSet])
// xs => xs.groupBy(_.id).require1Element: Either[String, (PrimaryKey, List[ResultSet])]
// 1:n List[ResultSet] => Map[PrimaryKey, List[ResultSet]]
// xs => xs.groupBy(_.id): Map[PrimaryKey, List[ResultSet]]
// 1:{0,1} List[ResultSet] => Option[(PrimaryKey, List[ResultSet])]
// xs => xs.groupBy(_.id).requireAtMost1Element: Either[String, Option[(PrimaryKey, List[ResultSet])]]
/*
val xs: List[(ContractName, ContractId, ContractEntityContractId, ContractEntityEntityId, EntityName, EntityId, EntityAge, EntityHeight)]
val ys = xs.groupBy(x => x.contractId): Map[ContractId, List[(ContractEntityContractId, ContractEntityEntityId, EntityName, EntityId, EntityAge, EntityHeight)]]
...: Map[ContractId, Map[ContractEntityContractId, List[(EntityName, EntityId, EntityAge, EntityHeight)]]]
...: Map[ContractId, Map[ContractEntityContractId, Map[EntityId, List[(EntityName, EntityAge, EntityHeight)]]]]
*/
implicit lazy val contract: Type[IO, QueryResult[ContractTable]] = tpe[IO, QueryResult[ContractTable]](
"Contract",
"name" -> query(c => (c.selName, c.selId).mapN(_ + _.toString())),
Expand All @@ -150,14 +175,6 @@ class MySchema(pool: Resource[IO, Session[IO]]) {
val extra = void""
sql"${c.id} = ${e.contractId}${extra.fragment}".apply(extra.argument)
}
// for {
// cet <- contractEntityTable.join[List](cet => sql"${cet.contractId} = ${c.id}")
// e <- entityTable.join[List] { e =>
// val _ = ens.foldMap(xs => sql" and ${e.name} in (${text.list(xs)})".apply(xs))
// val extra = void""
// sql"${e.id} = ${cet.entityId}${extra.fragment} and ${c.parent.parent.pk} is not null".apply(extra.argument)
// }
// } yield e
}
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ trait QueryAlgebra {
val decoder = (sel.decoder, done.dec).tupled
val qc2 = Interpreter.QueryContent(sel.cols ++ qc.selections, qc.joins)
val frag = Interpreter.renderQuery(qc2)
println(s"running ${frag.asInstanceOf[skunk.AppliedFragment].fragment}")
val result = runQuery(frag, decoder)
result
.map(_.groupMap { case (k, _) => k } { case (_, v) => v })
Expand Down Expand Up @@ -120,7 +121,7 @@ trait QueryAlgebra {
implicit lazy val applicativeForSelect: Applicative[Select] = new Applicative[Select] {
override def pure[A](x: A): Select[A] =
Select(Chain.empty, Applicative[Decoder].pure(x))

override def ap[A, B](ff: Select[A => B])(fa: Select[A]): Select[B] =
Select(ff.cols ++ fa.cols, ff.decoder ap fa.decoder)
}
Expand Down
28 changes: 4 additions & 24 deletions modules/relational/src/main/scala/gql/relational/a.scala
Original file line number Diff line number Diff line change
Expand Up @@ -659,30 +659,10 @@ object Test7 {
fastEntities {
name
age
}
fe2: fastEntities {
name
age
}
fe3: fastEntities {
name
age
}
fe4: fastEntities {
name
age
}
fe5: fastEntities {
name
age
}
fe6: fastEntities {
name
age
}
fe7: fastEntities {
name
age
pets {
name
id
}
}
}
}
Expand Down

0 comments on commit 7cacc82

Please sign in to comment.