/
Row.hs
47 lines (36 loc) · 1.33 KB
/
Row.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DeriveLift #-}
{-# LANGUAGE TemplateHaskell #-}
module Database.Kosem.PostgreSQL.Internal.Row where
import Data.Data (Proxy (Proxy))
import Data.Kind (Type)
import Database.PostgreSQL.LibPQ (Result)
import GHC.Exts (Any)
import GHC.Records
import GHC.TypeLits
import Language.Haskell.TH.Syntax (Lift (..))
import Unsafe.Coerce (unsafeCoerce)
type (:=) :: Symbol -> Type -> Type
data label := ty = (KnownSymbol label) => Proxy label := ty
-- TODO instances?
-- TODO small array
newtype Row (types :: [Type]) = Row [Any]
instance HasField l (Row ((l := t) : e)) t where
getField (Row xs) = unsafeCoerce $ xs !! 0
{-# INLINE getField #-}
instance HasField l (Row (_0 : (l := t) : e)) t where
getField (Row xs) = unsafeCoerce $ xs !! 1
{-# INLINE getField #-}
instance HasField l (Row (_0 : _1 : (l := t) : e)) t where
getField (Row xs) = unsafeCoerce $ xs !! 2
{-# INLINE getField #-}
instance HasField l (Row (_0 : _1 : _2 : (l := t) : e)) t where
getField (Row xs) = unsafeCoerce $ xs !! 3
{-# INLINE getField #-}
instance HasField l (Row (_0 : _1 : _2 : _3 : (l := t) : e)) t where
getField (Row xs) = unsafeCoerce $ xs !! 4
{-# INLINE getField #-}
class FromRow a where
fromRow :: Result -> Int -> a
instance FromRow (Row '[l := t]) where
fromRow res col = undefined