on dev help sign in
remove EnumFrom and CompositeFrom

they are redundant since we have `PG`

commit04856123fc6ad1ef31e386e7ff5d1343ecfadaa5 authorEitan Chatav <eitan.chatav@gmail.com> dateWed 29 Aug 2018, 12:09 (8 years ago) parentc1968c4
2 files changed+10−39
squeal-postgresql/src/Squeal/PostgreSQL/Binary.hs+9−9 squeal-postgresql/src/Squeal/PostgreSQL/Schema.hs+1−30
squeal-postgresql/src/Squeal/PostgreSQL/Binary.hs+9−9
@@ -103,12 +103,16 @@ all the record fields must be `Maybe`s of basic types.
103103>>> instance Generic Person
104104>>> instance HasDatatypeInfo Person
105105
106−We can create the equivalent Postgres types directly from their Haskell types.
106+We can create the equivalent Postgres types directly from their Haskell types. First,
107+define a type instances for `PG`.
108+
109+>>> type instance PG Schwarma = PG (Composite Schwarma)
110+>>> type instance PG Person = PG (Enumerated Person)
107111
108112>>> :{
109113type Schema =
110− '[ "schwarma" ::: 'Typedef (EnumFrom Schwarma)
111− , "person" ::: 'Typedef (CompositeFrom Person)
114+ '[ "schwarma" ::: 'Typedef (PG Schwarma)
115+ , "person" ::: 'Typedef (PG Person)
112116 ]
113117:}
114118
@@ -124,17 +128,13 @@ Then we can perform roundtrip queries;
124128
125129>>> :{
126130let
127− querySchwarma :: Query Schema
128− '[ 'NotNull (EnumFrom Schwarma)]
129− '["fromOnly" ::: 'NotNull (EnumFrom Schwarma)]
131+ querySchwarma :: Query Schema (TuplePG (Only Schwarma)) (RowPG (Only Schwarma))
130132 querySchwarma = values_ (parameter @1 (typedef #schwarma) `as` #fromOnly)
131133:}
132134
133135>>> :{
134136let
135− queryPerson :: Query Schema
136− '[ 'NotNull (CompositeFrom Person)]
137− '["fromOnly" ::: 'NotNull (CompositeFrom Person)]
137+ queryPerson :: Query Schema (TuplePG (Only Person)) (RowPG (Only Person))
138138 queryPerson = values_ (parameter @1 (typedef #person) `as` #fromOnly)
139139:}
140140
squeal-postgresql/src/Squeal/PostgreSQL/Schema.hs+1−30
@@ -115,9 +115,7 @@ module Squeal.PostgreSQL.Schema
115115 , Composite (..)
116116 , Enumerated (..)
117117 , NullPG
118− , EnumFrom
119118 , LabelsFrom
120− , CompositeFrom
121119 , RowPG
122120 , RowOf
123121 , FieldPG
@@ -722,26 +720,13 @@ type instance PG (hask, hask, hask, hask, hask, hask, hask, hask, hask)
722720type instance PG (hask, hask, hask, hask, hask, hask, hask, hask, hask, hask)
723721 = 'PGfixarray 10 (NullPG hask)
724722type instance PG (Composite hask) = 'PGcomposite (RowOf (RecordCodeOf hask))
725−type instance PG (Enumerated hask)
726− = 'PGenum (ConstructorNamesOf (ConstructorsOf (DatatypeInfoOf hask)))
723+type instance PG (Enumerated hask) = 'PGenum (LabelsFrom hask)
727724
728725newtype Json hask = Json {getJson :: hask}
729726newtype Jsonb hask = Jsonb {getJsonb :: hask}
730727newtype Composite record = Composite {getComposite :: record}
731728newtype Enumerated enum = Enumerated {getEnumerated :: enum}
732729
733−-- | The `EnumFrom` type family embeds Haskell enum types, ADTs with
734−-- nullary constructors, as Postgres enum types
735−--
736−-- >>> data Schwarma = Beef | Lamb | Chicken deriving GHC.Generic
737−-- >>> instance Generic Schwarma
738−-- >>> instance HasDatatypeInfo Schwarma
739−-- >>> :kind! EnumFrom Schwarma
740−-- EnumFrom Schwarma :: PGType
741−-- = 'PGenum '["Beef", "Lamb", "Chicken"]
742−type family EnumFrom (hask :: Type) :: PGType where
743− EnumFrom hask = PG (Enumerated hask)
744−
745730-- | The `LabelsFrom` type family calculates the constructors of a
746731-- Haskell enum type.
747732--
@@ -755,20 +740,6 @@ type family LabelsFrom (hask :: Type) :: [Type.ConstructorName] where
755740 LabelsFrom hask =
756741 ConstructorNamesOf (ConstructorsOf (DatatypeInfoOf hask))
757742
758−-- | The `CompositeFrom` type family embeds Haskell record types as
759−-- Postgres composite types, as long as the record fields
760−-- are `Maybe`s of Haskell types that can be embedded as basic types
761−-- with the `PG` type family.
762−--
763−-- >>> data Row = Row { a :: Maybe Int16, b :: Maybe LocalTime } deriving GHC.Generic
764−-- >>> instance Generic Row
765−-- >>> instance HasDatatypeInfo Row
766−-- >>> :kind! CompositeFrom Row
767−-- CompositeFrom Row :: PGType
768−-- = 'PGcomposite '["a" ::: 'Null 'PGint2, "b" ::: 'Null 'PGtimestamp]
769−type family CompositeFrom (hask :: Type) :: PGType where
770− CompositeFrom hask = PG (Composite hask)
771−
772743type family RowPG (hask :: Type) :: RowType where
773744 RowPG hask = RowOf (RecordCodeOf hask)
774745