@@ -1,6 +1,10 @@
1{-# LANGUAGE
1{-# LANGUAGE
3 , DefaultSignatures
3 , DefaultSignatures
4 , FlexibleContexts
8 , FlexibleContexts
5 , FlexibleInstances
9 , FlexibleInstances
@@ -9,6 +13,7 @@
9 , MultiParamTypeClasses
13 , MultiParamTypeClasses
10 , ScopedTypeVariables
14 , ScopedTypeVariables
11 , TypeApplications
15 , TypeApplications
12 , TypeOperators
17 , TypeOperators
13 , UndecidableInstances
18 , UndecidableInstances
@@ -29,6 +34,8 @@ import qualified Data.ByteString.Lazy as Lazy
29import qualified Data.ByteString as Strict hiding (unpack)
34import qualified Data.ByteString as Strict hiding (unpack)
30import qualified Data.Text.Lazy as Lazy
35import qualified Data.Text.Lazy as Lazy
31import qualified Data.Text as Strict
36import qualified Data.Text as Strict
37+import qualified Generics.SOP.Type.Metadata as Type
38+import qualified GHC.Generics as GHC
32import qualified PostgreSQL.Binary.Decoding as Decoding
39import qualified PostgreSQL.Binary.Decoding as Decoding
33import qualified PostgreSQL.Binary.Encoding as Encoding
40import qualified PostgreSQL.Binary.Encoding as Encoding
@@ -77,8 +84,6 @@ instance ToParam x pg => ToColumnParam (Maybe x) ('Optional (nullity pg)) where
78class SListI tys => ToParams x (tys :: [ColumnType]) where
85class SListI tys => ToParams x (tys :: [ColumnType]) where
79 toParams :: x -> NP (K (Maybe Strict.ByteString)) tys
86 toParams :: x -> NP (K (Maybe Strict.ByteString)) tys
80−instance ToColumnParam x ty => ToParams x '[ty] where
81− toParams x = toColumnParam x :* Nil
82instance (SListI tys, IsProductType x xs, AllZip ToColumnParam xs tys)
87instance (SListI tys, IsProductType x xs, AllZip ToColumnParam xs tys)
83 => ToParams x tys where
88 => ToParams x tys where
@@ -143,11 +148,31 @@ instance FromValue pg y
144class SListI columns => FromRow (columns :: [(Symbol,ColumnType)]) y where
149class SListI columns => FromRow (columns :: [(Symbol,ColumnType)]) y where
145 fromRow :: NP (K (Maybe Strict.ByteString)) columns -> y
150 fromRow :: NP (K (Maybe Strict.ByteString)) columns -> y
146−instance FromColumnValue ty y => FromRow '[ty] y where
147− fromRow (y :* Nil) = fromColumnValue y
149 ( SListI columns, IsProductType y ys
152 ( SListI columns, IsProductType y ys
150 , AllZip FromColumnValue columns ys
153 , AllZip FromColumnValue columns ys
155+ , SameFields (DatatypeInfoOf y) columns
151 ) => FromRow columns y where
156 ) => FromRow columns y where
153 = to . SOP . Z . htrans (Proxy @FromColumnValue) (I . fromColumnValue)
158 = to . SOP . Z . htrans (Proxy @FromColumnValue) (I . fromColumnValue)
161+ (fieldInfo :: Type.FieldInfo) (columnty :: (Symbol,ColumnType)) where
162+instance field ~ column => SameField ('Type.FieldInfo field) (column ::: ty)
164+type family SameFields
165+ (datatypeInfo :: Type.DatatypeInfo) (columns :: [(Symbol,ColumnType)]) where
167+ ('Type.ADT _module _datatype '[ 'Type.Record _constructor fields])
169+ = AllZip SameField fields columns
171+ ('Type.Newtype _module _datatype ('Type.Record _constructor '[field]))
173+ = SameField field column
175+newtype Only x = Only { only :: x }
176+ deriving (Functor,Foldable,Traversable,Eq,Ord,Read,Show,GHC.Generic)
177+instance Generic (Only x)
178+instance HasDatatypeInfo (Only x)