on dev help sign in
remove EnumFrom and CompositeFrom
0485612Eitan Chatav
squeal-postgresql/src/Squeal/PostgreSQL/Binary.hs+9−9
@@ -103,12 +103,16 @@ all the record fields must be `Maybe`s of basic types.
103>>> instance Generic Person
103>>> instance Generic Person
104>>> instance HasDatatypeInfo Person
104>>> instance HasDatatypeInfo Person
105
105
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)
107
111
108>>> :{
112>>> :{
109type Schema =
113type Schema =
110− '[ "schwarma" ::: 'Typedef (EnumFrom Schwarma)
114+ '[ "schwarma" ::: 'Typedef (PG Schwarma)
111− , "person" ::: 'Typedef (CompositeFrom Person)
115+ , "person" ::: 'Typedef (PG Person)
112 ]
116 ]
113:}
117:}
114
118
@@ -124,17 +128,13 @@ Then we can perform roundtrip queries;
124
128
125>>> :{
129>>> :{
126let
130let
127− querySchwarma :: Query Schema
131+ querySchwarma :: Query Schema (TuplePG (Only Schwarma)) (RowPG (Only Schwarma))
128− '[ 'NotNull (EnumFrom Schwarma)]
129− '["fromOnly" ::: 'NotNull (EnumFrom Schwarma)]
130 querySchwarma = values_ (parameter @1 (typedef #schwarma) `as` #fromOnly)
132 querySchwarma = values_ (parameter @1 (typedef #schwarma) `as` #fromOnly)
131:}
133:}
132
134
133>>> :{
135>>> :{
134let
136let
135− queryPerson :: Query Schema
137+ queryPerson :: Query Schema (TuplePG (Only Person)) (RowPG (Only Person))
136− '[ 'NotNull (CompositeFrom Person)]
137− '["fromOnly" ::: 'NotNull (CompositeFrom Person)]
138 queryPerson = values_ (parameter @1 (typedef #person) `as` #fromOnly)
138 queryPerson = values_ (parameter @1 (typedef #person) `as` #fromOnly)
139:}
139:}
140
140
squeal-postgresql/src/Squeal/PostgreSQL/Schema.hs+1−30
@@ -115,9 +115,7 @@ module Squeal.PostgreSQL.Schema
115 , Composite (..)
115 , Composite (..)
116 , Enumerated (..)
116 , Enumerated (..)
117 , NullPG
117 , NullPG
118− , EnumFrom
119 , LabelsFrom
118 , LabelsFrom
120− , CompositeFrom
121 , RowPG
119 , RowPG
122 , RowOf
120 , RowOf
123 , FieldPG
121 , FieldPG
@@ -722,26 +720,13 @@ type instance PG (hask, hask, hask, hask, hask, hask, hask, hask, hask)
722type instance PG (hask, hask, hask, hask, hask, hask, hask, hask, hask, hask)
720type instance PG (hask, hask, hask, hask, hask, hask, hask, hask, hask, hask)
723 = 'PGfixarray 10 (NullPG hask)
721 = 'PGfixarray 10 (NullPG hask)
724type instance PG (Composite hask) = 'PGcomposite (RowOf (RecordCodeOf hask))
722type instance PG (Composite hask) = 'PGcomposite (RowOf (RecordCodeOf hask))
725−type instance PG (Enumerated hask)
723+type instance PG (Enumerated hask) = 'PGenum (LabelsFrom hask)
726− = 'PGenum (ConstructorNamesOf (ConstructorsOf (DatatypeInfoOf hask)))
727
724
728newtype Json hask = Json {getJson :: hask}
725newtype Json hask = Json {getJson :: hask}
729newtype Jsonb hask = Jsonb {getJsonb :: hask}
726newtype Jsonb hask = Jsonb {getJsonb :: hask}
730newtype Composite record = Composite {getComposite :: record}
727newtype Composite record = Composite {getComposite :: record}
731newtype Enumerated enum = Enumerated {getEnumerated :: enum}
728newtype Enumerated enum = Enumerated {getEnumerated :: enum}
732
729
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−
745-- | The `LabelsFrom` type family calculates the constructors of a
730-- | The `LabelsFrom` type family calculates the constructors of a
746-- Haskell enum type.
731-- Haskell enum type.
747--
732--
@@ -755,20 +740,6 @@ type family LabelsFrom (hask :: Type) :: [Type.ConstructorName] where
755 LabelsFrom hask =
740 LabelsFrom hask =
756 ConstructorNamesOf (ConstructorsOf (DatatypeInfoOf hask))
741 ConstructorNamesOf (ConstructorsOf (DatatypeInfoOf hask))
757
742
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−
772type family RowPG (hask :: Type) :: RowType where
743type family RowPG (hask :: Type) :: RowType where
773 RowPG hask = RowOf (RecordCodeOf hask)
744 RowPG hask = RowOf (RecordCodeOf hask)
774
745