Skip to content

Instantly share code, notes, and snippets.

@chrisdone-artificial
Created May 20, 2022 08:11
Show Gist options
  • Save chrisdone-artificial/5f30a6b075815910b2da440e7f99fcb2 to your computer and use it in GitHub Desktop.
Save chrisdone-artificial/5f30a6b075815910b2da440e7f99fcb2 to your computer and use it in GitHub Desktop.
Rel8 sum types demo
{-# language DeriveAnyClass #-}
{-# language DeriveGeneric #-}
{-# language DisambiguateRecordFields #-}
{-# language OverloadedStrings #-}
{-# language TypeApplications #-}
{-# options -Wwarn #-}
module Brossa.API.Internal.Rel8 where
import Data.Int
import Data.Text (Text)
import GHC.Generics ( Generic )
import Rel8
import Prelude
data SomeThing f = SomeThing
{ id :: Column f Int64
, thing :: HADT f Thing
}
deriving stock Generic
deriving anyclass Rel8able
data Thing f = ThingEmployer (Employer f) | ThingPotato (Potato f) | Nullary
deriving stock Generic
data Employer f = Employer { employerId :: f Int32, employerName :: f Text}
deriving stock Generic
deriving anyclass Rel8able
data Potato f = Potato { size :: f Int32, grower :: f Text }
deriving stock Generic
deriving anyclass Rel8able
thingSchema :: TableSchema (ADT Thing Name)
thingSchema =
TableSchema
{ schema = Nothing,
name = "thing",
columns =
nameADT @Thing
"tag"
Employer
{ employerName = "name",
employerId = "id"
}
Potato {size = "size", grower = "Mary"}
}
query :: Query (ADT Thing Expr)
query = do
thingExpr <- each thingSchema
where_ $
deconstructADT @Thing
(\employer -> employerName employer ==. lit "Mary")
(\potato -> grower potato ==. lit "Mary")
(lit False) -- Nullary case
thingExpr
pure thingExpr
main :: IO ()
main =
putStrLn $
showQuery $ query
SELECT
CAST("tag0_1" AS text) as "tag",
CAST("id1_1" AS int4) as "ThingEmployer/_1/employerId",
CAST("name2_1" AS text) as "ThingEmployer/_1/employerName",
CAST("size3_1" AS int4) as "ThingPotato/_1/size",
CAST("Mary4_1" AS text) as "ThingPotato/_1/grower"
FROM (SELECT
*
FROM (SELECT
"tag" as "tag0_1",
"id" as "id1_1",
"name" as "name2_1",
"size" as "size3_1",
"Mary" as "Mary4_1"
FROM "thing" as "T1") as "T1"
WHERE (CASE WHEN ("tag0_1") = (CAST(E'ThingPotato' AS text)) THEN ("Mary4_1") = (CAST(E'Mary' AS text))
WHEN ("tag0_1") = (CAST(E'Nullary' AS text)) THEN CAST(FALSE AS bool) ELSE ("name2_1") = (CAST(E'Mary' AS text)) END)) as "T1"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment