on dev help sign in
With ~> From
a1b7db8Eitan Chatav
squeal-postgresql/src/Squeal/PostgreSQL/Binary.hs+13−13
@@ -68,8 +68,8 @@ We can create the equivalent Postgres types directly from their Haskell types.
68
68
69>>> :{
69>>> :{
70type Schema =
70type Schema =
71− '[ "schwarma" ::: 'Typedef (EnumWith Schwarma)
71+ '[ "schwarma" ::: 'Typedef (EnumFrom Schwarma)
72− , "person" ::: 'Typedef (CompositeWith Person)
72+ , "person" ::: 'Typedef (CompositeFrom Person)
73 ]
73 ]
74:}
74:}
75
75
@@ -77,8 +77,8 @@ type Schema =
77let
77let
78 setup :: Definition '[] Schema
78 setup :: Definition '[] Schema
79 setup =
79 setup =
80− createTypeEnumWith @Schwarma #schwarma >>>
80+ createTypeEnumFrom @Schwarma #schwarma >>>
81− createTypeCompositeWith @Person #person
81+ createTypeCompositeFrom @Person #person
82:}
82:}
83
83
84Then we can perform roundtrip queries;
84Then we can perform roundtrip queries;
@@ -86,16 +86,16 @@ Then we can perform roundtrip queries;
86>>> :{
86>>> :{
87let
87let
88 qry1 :: Query Schema
88 qry1 :: Query Schema
89− '[ 'NotNull (EnumWith Schwarma)]
89+ '[ 'NotNull (EnumFrom Schwarma)]
90− '["fromOnly" ::: 'NotNull (EnumWith Schwarma)]
90+ '["fromOnly" ::: 'NotNull (EnumFrom Schwarma)]
91 qry1 = values_ (parameter @1 #schwarma `As` #fromOnly :* Nil)
91 qry1 = values_ (parameter @1 #schwarma `As` #fromOnly :* Nil)
92:}
92:}
93
93
94>>> :{
94>>> :{
95let
95let
96 qry2 :: Query Schema
96 qry2 :: Query Schema
97− '[ 'NotNull (CompositeWith Person)]
97+ '[ 'NotNull (CompositeFrom Person)]
98− '["fromOnly" ::: 'NotNull (CompositeWith Person)]
98+ '["fromOnly" ::: 'NotNull (CompositeFrom Person)]
99 qry2 = values_ (parameter @1 #person `As` #fromOnly :* Nil)
99 qry2 = values_ (parameter @1 #person `As` #fromOnly :* Nil)
100:}
100:}
101
101
@@ -241,7 +241,7 @@ instance (HasOid pg, ToParam x pg)
241instance
241instance
242 ( IsEnumType x
242 ( IsEnumType x
243 , HasDatatypeInfo x
243 , HasDatatypeInfo x
244− , LabelsWith x ~ labels
244+ , LabelsFrom x ~ labels
245 ) => ToParam x ('PGenum labels) where
245 ) => ToParam x ('PGenum labels) where
246 toParam =
246 toParam =
247 let
247 let
@@ -261,7 +261,7 @@ instance
261 , MapMaybes xs
261 , MapMaybes xs
262 , IsProductType x (Maybes xs)
262 , IsProductType x (Maybes xs)
263 , AllZip ToAliasedParam xs fields
263 , AllZip ToAliasedParam xs fields
264− , FieldNamesWith x ~ AliasesOf fields
264+ , FieldNamesFrom x ~ AliasesOf fields
265 , All HasAliasedOid fields
265 , All HasAliasedOid fields
266 ) => ToParam x ('PGcomposite fields) where
266 ) => ToParam x ('PGcomposite fields) where
267 toParam =
267 toParam =
@@ -397,7 +397,7 @@ instance FromValue pg y => FromValue ('PGfixarray n pg) (Vector (Maybe y)) where
397instance
397instance
398 ( IsEnumType y
398 ( IsEnumType y
399 , HasDatatypeInfo y
399 , HasDatatypeInfo y
400− , LabelsWith y ~ labels
400+ , LabelsFrom y ~ labels
401 ) => FromValue ('PGenum labels) y where
401 ) => FromValue ('PGenum labels) y where
402 fromValue _ =
402 fromValue _ =
403 let
403 let
@@ -422,7 +422,7 @@ instance
422 , MapMaybes ys
422 , MapMaybes ys
423 , IsProductType y (Maybes ys)
423 , IsProductType y (Maybes ys)
424 , AllZip FromAliasedValue fields ys
424 , AllZip FromAliasedValue fields ys
425− , FieldNamesWith y ~ AliasesOf fields
425+ , FieldNamesFrom y ~ AliasesOf fields
426 ) => FromValue ('PGcomposite fields) y where
426 ) => FromValue ('PGcomposite fields) y where
427 fromValue =
427 fromValue =
428 let
428 let
@@ -515,7 +515,7 @@ instance
515 ( SListI results
515 ( SListI results
516 , IsProductType y ys
516 , IsProductType y ys
517 , AllZip FromColumnValue results ys
517 , AllZip FromColumnValue results ys
518− , FieldNamesWith y ~ AliasesOf results
518+ , FieldNamesFrom y ~ AliasesOf results
519 ) => FromRow results y where
519 ) => FromRow results y where
520 fromRow
520 fromRow
521 = to . SOP . Z . htrans (Proxy @FromColumnValue) (I . fromColumnValue)
521 = to . SOP . Z . htrans (Proxy @FromColumnValue) (I . fromColumnValue)
squeal-postgresql/src/Squeal/PostgreSQL/Definition.hs+17−17
@@ -68,9 +68,9 @@ module Squeal.PostgreSQL.Definition
68 , dropView
68 , dropView
69 -- * Types
69 -- * Types
70 , createTypeEnum
70 , createTypeEnum
71− , createTypeEnumWith
71+ , createTypeEnumFrom
72 , createTypeComposite
72 , createTypeComposite
73− , createTypeCompositeWith
73+ , createTypeCompositeFrom
74 , dropType
74 , dropType
75 -- * Columns
75 -- * Columns
76 , ColumnTypeExpression (..)
76 , ColumnTypeExpression (..)
@@ -835,19 +835,19 @@ createTypeEnum enum labels = UnsafeDefinition $
835-- >>> data Schwarma = Beef | Lamb | Chicken deriving GHC.Generic
835-- >>> data Schwarma = Beef | Lamb | Chicken deriving GHC.Generic
836-- >>> instance SOP.Generic Schwarma
836-- >>> instance SOP.Generic Schwarma
837-- >>> instance SOP.HasDatatypeInfo Schwarma
837-- >>> instance SOP.HasDatatypeInfo Schwarma
838−-- >>> printSQL $ createTypeEnumWith @Schwarma #schwarma
838+-- >>> printSQL $ createTypeEnumFrom @Schwarma #schwarma
839-- CREATE TYPE "schwarma" AS ENUM ('Beef', 'Lamb', 'Chicken');
839-- CREATE TYPE "schwarma" AS ENUM ('Beef', 'Lamb', 'Chicken');
840−createTypeEnumWith
840+createTypeEnumFrom
841 :: forall hask enum schema.
841 :: forall hask enum schema.
842 ( SOP.Generic hask
842 ( SOP.Generic hask
843− , SOP.All KnownSymbol (LabelsWith hask)
843+ , SOP.All KnownSymbol (LabelsFrom hask)
844 , KnownSymbol enum
844 , KnownSymbol enum
845 )
845 )
846 => Alias enum
846 => Alias enum
847 -- ^ name of the user defined enumerated type
847 -- ^ name of the user defined enumerated type
848− -> Definition schema (Create enum ('Typedef (EnumWith hask)) schema)
848+ -> Definition schema (Create enum ('Typedef (EnumFrom hask)) schema)
849−createTypeEnumWith enum = createTypeEnum enum
849+createTypeEnumFrom enum = createTypeEnum enum
850− (SOP.hpure label :: NP PGlabel (LabelsWith hask))
850+ (SOP.hpure label :: NP PGlabel (LabelsFrom hask))
851
851
852-- | `createTypeComposite` creates a composite type. The composite type is
852-- | `createTypeComposite` creates a composite type. The composite type is
853-- specified by a list of attribute names and data types.
853-- specified by a list of attribute names and data types.
@@ -874,28 +874,28 @@ createTypeComposite ty fields = UnsafeDefinition $
874-- >>> data Complex = Complex {real :: Maybe Double, imaginary :: Maybe Double} deriving GHC.Generic
874-- >>> data Complex = Complex {real :: Maybe Double, imaginary :: Maybe Double} deriving GHC.Generic
875-- >>> instance SOP.Generic Complex
875-- >>> instance SOP.Generic Complex
876-- >>> instance SOP.HasDatatypeInfo Complex
876-- >>> instance SOP.HasDatatypeInfo Complex
877−-- >>> printSQL $ createTypeCompositeWith @Complex #complex
877+-- >>> printSQL $ createTypeCompositeFrom @Complex #complex
878-- CREATE TYPE "complex" AS ("real" float8, "imaginary" float8);
878-- CREATE TYPE "complex" AS ("real" float8, "imaginary" float8);
879−createTypeCompositeWith
879+createTypeCompositeFrom
880 :: forall hask ty schema.
880 :: forall hask ty schema.
881− ( ZipAliased (FieldNamesWith hask) (FieldTypesWith hask)
881+ ( ZipAliased (FieldNamesFrom hask) (FieldTypesFrom hask)
882− , SOP.All (PGTyped schema) (FieldTypesWith hask)
882+ , SOP.All (PGTyped schema) (FieldTypesFrom hask)
883 , KnownSymbol ty
883 , KnownSymbol ty
884 )
884 )
885 => Alias ty
885 => Alias ty
886 -- ^ name of the user defined composite type
886 -- ^ name of the user defined composite type
887− -> Definition schema (Create ty ( 'Typedef (CompositeWith hask)) schema)
887+ -> Definition schema (Create ty ( 'Typedef (CompositeFrom hask)) schema)
888−createTypeCompositeWith ty = createTypeComposite ty $ zipAs
888+createTypeCompositeFrom ty = createTypeComposite ty $ zipAs
889− (SOP.hpure Alias :: NP Alias (FieldNamesWith hask))
889+ (SOP.hpure Alias :: NP Alias (FieldNamesFrom hask))
890 (SOP.hcpure (SOP.Proxy :: SOP.Proxy (PGTyped schema)) pgtype
890 (SOP.hcpure (SOP.Proxy :: SOP.Proxy (PGTyped schema)) pgtype
891− :: NP (TypeExpression schema) (FieldTypesWith hask))
891+ :: NP (TypeExpression schema) (FieldTypesFrom hask))
892
892
893-- | Drop a type.
893-- | Drop a type.
894--
894--
895-- >>> data Schwarma = Beef | Lamb | Chicken deriving GHC.Generic
895-- >>> data Schwarma = Beef | Lamb | Chicken deriving GHC.Generic
896-- >>> instance SOP.Generic Schwarma
896-- >>> instance SOP.Generic Schwarma
897-- >>> instance SOP.HasDatatypeInfo Schwarma
897-- >>> instance SOP.HasDatatypeInfo Schwarma
898−-- >>> printSQL (dropType #schwarma :: Definition '["schwarma" ::: 'Typedef (EnumWith Schwarma)] '[])
898+-- >>> printSQL (dropType #schwarma :: Definition '["schwarma" ::: 'Typedef (EnumFrom Schwarma)] '[])
899-- DROP TYPE "schwarma";
899-- DROP TYPE "schwarma";
900dropType
900dropType
901 :: Has tydef schema ('Typedef ty)
901 :: Has tydef schema ('Typedef ty)
squeal-postgresql/src/Squeal/PostgreSQL/Schema.hs+29−38
@@ -100,15 +100,13 @@ module Squeal.PostgreSQL.Schema
100 , PGlabel (..)
100 , PGlabel (..)
101 , renderLabel
101 , renderLabel
102 , renderLabels
102 , renderLabels
103− , LabelOf
104− , LabelsOf
105 -- * Embedding
103 -- * Embedding
106 , PG
104 , PG
107− , EnumWith
105+ , EnumFrom
108− , LabelsWith
106+ , LabelsFrom
109− , CompositeWith
107+ , CompositeFrom
110− , FieldNamesWith
108+ , FieldNamesFrom
111− , FieldTypesWith
109+ , FieldTypesFrom
112 , ConstructorsOf
110 , ConstructorsOf
113 , ConstructorNameOf
111 , ConstructorNameOf
114 , ConstructorNamesOf
112 , ConstructorNamesOf
@@ -682,13 +680,6 @@ renderLabels
682 :: All KnownSymbol labels => NP PGlabel labels -> [ByteString]
680 :: All KnownSymbol labels => NP PGlabel labels -> [ByteString]
683renderLabels = hcollapse
681renderLabels = hcollapse
684 . hcmap (Proxy @KnownSymbol) (K . renderLabel)
682 . hcmap (Proxy @KnownSymbol) (K . renderLabel)
685−-- | Gets the name of a type constructor
686−type family LabelOf (cons :: Type.ConstructorInfo) :: Symbol where
687− LabelOf ('Type.Constructor name) = name
688−-- | Gets the names of a list of type constructors
689−type family LabelsOf (conss :: [Type.ConstructorInfo]) :: [Symbol] where
690− LabelsOf '[] = '[]
691− LabelsOf (cons ': conss) = LabelOf cons ': LabelsOf conss
692
683
693-- | The `PG` type family embeds a subset of Haskell types
684-- | The `PG` type family embeds a subset of Haskell types
694-- as Postgres basic types.
685-- as Postgres basic types.
@@ -724,32 +715,32 @@ type family PG (hask :: Type) :: PGType where
724 PG ty = TypeError
715 PG ty = TypeError
725 ('Text "There is no Postgres basic type for " ':<>: 'ShowType ty)
716 ('Text "There is no Postgres basic type for " ':<>: 'ShowType ty)
726
717
727−-- | The `EnumWith` type family embeds Haskell enum types, ADTs with
718+-- | The `EnumFrom` type family embeds Haskell enum types, ADTs with
728-- nullary constructors, as Postgres enum types
719-- nullary constructors, as Postgres enum types
729--
720--
730-- >>> data Schwarma = Beef | Lamb | Chicken deriving GHC.Generic
721-- >>> data Schwarma = Beef | Lamb | Chicken deriving GHC.Generic
731-- >>> instance Generic Schwarma
722-- >>> instance Generic Schwarma
732-- >>> instance HasDatatypeInfo Schwarma
723-- >>> instance HasDatatypeInfo Schwarma
733−-- >>> :kind! EnumWith Schwarma
724+-- >>> :kind! EnumFrom Schwarma
734−-- EnumWith Schwarma :: PGType
725+-- EnumFrom Schwarma :: PGType
735-- = 'PGenum '["Beef", "Lamb", "Chicken"]
726-- = 'PGenum '["Beef", "Lamb", "Chicken"]
736−type family EnumWith (hask :: Type) :: PGType where
727+type family EnumFrom (hask :: Type) :: PGType where
737− EnumWith hask = 'PGenum (LabelsWith hask)
728+ EnumFrom hask = 'PGenum (LabelsFrom hask)
738
729
739−-- | The `LabelsWith` type family calculates the constructors of a
730+-- | The `LabelsFrom` type family calculates the constructors of a
740-- Haskell enum type.
731-- Haskell enum type.
741--
732--
742-- >>> data Schwarma = Beef | Lamb | Chicken deriving GHC.Generic
733-- >>> data Schwarma = Beef | Lamb | Chicken deriving GHC.Generic
743-- >>> instance Generic Schwarma
734-- >>> instance Generic Schwarma
744-- >>> instance HasDatatypeInfo Schwarma
735-- >>> instance HasDatatypeInfo Schwarma
745−-- >>> :kind! LabelsWith Schwarma
736+-- >>> :kind! LabelsFrom Schwarma
746−-- LabelsWith Schwarma :: [Type.ConstructorName]
737+-- LabelsFrom Schwarma :: [Type.ConstructorName]
747-- = '["Beef", "Lamb", "Chicken"]
738-- = '["Beef", "Lamb", "Chicken"]
748−type family LabelsWith (hask :: Type) :: [Type.ConstructorName] where
739+type family LabelsFrom (hask :: Type) :: [Type.ConstructorName] where
749− LabelsWith hask =
740+ LabelsFrom hask =
750 ConstructorNamesOf (ConstructorsOf (DatatypeInfoOf hask))
741 ConstructorNamesOf (ConstructorsOf (DatatypeInfoOf hask))
751
742
752−-- | The `CompositeWith` type family embeds Haskell record types as
743+-- | The `CompositeFrom` type family embeds Haskell record types as
753-- Postgres composite types, as long as the record fields
744-- Postgres composite types, as long as the record fields
754-- are `Maybe`s of Haskell types that can be embedded as basic types
745-- are `Maybe`s of Haskell types that can be embedded as basic types
755-- with the `PG` type family.
746-- with the `PG` type family.
@@ -757,30 +748,30 @@ type family LabelsWith (hask :: Type) :: [Type.ConstructorName] where
757-- >>> data Row = Row { a :: Maybe Int16, b :: Maybe LocalTime } deriving GHC.Generic
748-- >>> data Row = Row { a :: Maybe Int16, b :: Maybe LocalTime } deriving GHC.Generic
758-- >>> instance Generic Row
749-- >>> instance Generic Row
759-- >>> instance HasDatatypeInfo Row
750-- >>> instance HasDatatypeInfo Row
760−-- >>> :kind! CompositeWith Row
751+-- >>> :kind! CompositeFrom Row
761−-- CompositeWith Row :: PGType
752+-- CompositeFrom Row :: PGType
762-- = 'PGcomposite '['("a", 'PGint2), '("b", 'PGtimestamp)]
753-- = 'PGcomposite '['("a", 'PGint2), '("b", 'PGtimestamp)]
763−type family CompositeWith (hask :: Type) :: PGType where
754+type family CompositeFrom (hask :: Type) :: PGType where
764− CompositeWith hask =
755+ CompositeFrom hask =
765− 'PGcomposite (ZipAs (FieldNamesWith hask) (FieldTypesWith hask))
756+ 'PGcomposite (ZipAs (FieldNamesFrom hask) (FieldTypesFrom hask))
766
757
767-- | >>> data Row = Row { a :: Maybe Int16, b :: Maybe LocalTime } deriving GHC.Generic
758-- | >>> data Row = Row { a :: Maybe Int16, b :: Maybe LocalTime } deriving GHC.Generic
768-- >>> instance Generic Row
759-- >>> instance Generic Row
769-- >>> instance HasDatatypeInfo Row
760-- >>> instance HasDatatypeInfo Row
770−-- >>> :kind! FieldNamesWith Row
761+-- >>> :kind! FieldNamesFrom Row
771−-- FieldNamesWith Row :: [Type.FieldName]
762+-- FieldNamesFrom Row :: [Type.FieldName]
772-- = '["a", "b"]
763-- = '["a", "b"]
773−type family FieldNamesWith (hask :: Type) :: [Type.FieldName] where
764+type family FieldNamesFrom (hask :: Type) :: [Type.FieldName] where
774− FieldNamesWith hask = FieldNamesOf (FieldsOf (DatatypeInfoOf hask))
765+ FieldNamesFrom hask = FieldNamesOf (FieldsOf (DatatypeInfoOf hask))
775
766
776-- | >>> data Row = Row { a :: Maybe Int16, b :: Maybe LocalTime } deriving GHC.Generic
767-- | >>> data Row = Row { a :: Maybe Int16, b :: Maybe LocalTime } deriving GHC.Generic
777-- >>> instance Generic Row
768-- >>> instance Generic Row
778-- >>> instance HasDatatypeInfo Row
769-- >>> instance HasDatatypeInfo Row
779−-- >>> :kind! FieldTypesWith Row
770+-- >>> :kind! FieldTypesFrom Row
780−-- FieldTypesWith Row :: [PGType]
771+-- FieldTypesFrom Row :: [PGType]
781-- = '['PGint2, 'PGtimestamp]
772-- = '['PGint2, 'PGtimestamp]
782−type family FieldTypesWith (hask :: Type) :: [PGType] where
773+type family FieldTypesFrom (hask :: Type) :: [PGType] where
783− FieldTypesWith hask = FieldTypesOf (RecordCodeOf hask (Code hask))
774+ FieldTypesFrom hask = FieldTypesOf (RecordCodeOf hask (Code hask))
784
775
785-- | Calculates constructors of a datatype.
776-- | Calculates constructors of a datatype.
786type family ConstructorsOf (datatype :: Type.DatatypeInfo)
777type family ConstructorsOf (datatype :: Type.DatatypeInfo)