{-# LANGUAGE AllowAmbiguousTypes #-}
{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE PolyKinds #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE UndecidableInstances #-}
module Polytropos.Hasql
(
HasqlRow
, recordRow
, AllRowDecoder (..)
, Column (..)
) where
import Data.Functor.Identity (Identity (..))
import Data.Int (Int16, Int32, Int64)
import Data.Kind (Type)
import Data.Proxy (Proxy (..))
import GHC.Exts (Any)
import Polytropos (Record)
import Polytropos.Core (Pair (..), Row, RowRep (..), noInlineUnsafeCo,
productRep)
import Hasql.Decoders (column, nonNullable)
import qualified Hasql.Decoders as HD
type HasqlRow = HD.Row
class Column (a :: Type) where
columnValue :: HD.Value a
instance Column Int16 where
columnValue :: Value Int16
columnValue = Value Int16
HD.int2
instance Column Int32 where
columnValue :: Value Int32
columnValue = Value Int32
HD.int4
instance Column Int64 where
columnValue :: Value Int64
columnValue = Value Int64
HD.int8
class AllRowDecoder (r :: Row) where
genRow :: Proxy r -> HasqlRow [Identity Any]
instance AllRowDecoder '[] where
genRow :: Proxy '[] -> HasqlRow [Identity Any]
genRow Proxy '[]
_ = [Identity Any] -> HasqlRow [Identity Any]
forall a. a -> Row a
forall (f :: * -> *) a. Applicative f => a -> f a
pure []
instance (Column a, AllRowDecoder r) => AllRowDecoder ((n ':= a) ': r) where
genRow :: Proxy ((n ':= a) : r) -> HasqlRow [Identity Any]
genRow Proxy ((n ':= a) : r)
_ =
(\a
v [Identity Any]
vs -> Any -> Identity Any
forall a. a -> Identity a
Identity (a -> Any
forall a b. a -> b
noInlineUnsafeCo a
v :: Any) Identity Any -> [Identity Any] -> [Identity Any]
forall a. a -> [a] -> [a]
: [Identity Any]
vs)
(a -> [Identity Any] -> [Identity Any])
-> Row a -> Row ([Identity Any] -> [Identity Any])
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (NullableOrNot Value a -> Row a
forall a. NullableOrNot Value a -> Row a
column (Value a -> NullableOrNot Value a
forall (decoder :: * -> *) a. decoder a -> NullableOrNot decoder a
nonNullable Value a
forall a. Column a => Value a
columnValue) :: HasqlRow a)
Row ([Identity Any] -> [Identity Any])
-> HasqlRow [Identity Any] -> HasqlRow [Identity Any]
forall a b. Row (a -> b) -> Row a -> Row b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Proxy r -> HasqlRow [Identity Any]
forall (r :: Row).
AllRowDecoder r =>
Proxy r -> HasqlRow [Identity Any]
genRow (forall (t :: Row). Proxy t
forall {k} (t :: k). Proxy t
Proxy @r)
recordRow :: forall r. AllRowDecoder r => HasqlRow (Record r)
recordRow :: forall (r :: Row). AllRowDecoder r => HasqlRow (Record r)
recordRow = Rep Identity () -> RowRep Identity 'ProductRowT r ()
forall {k} k1 (f :: * -> *) (r :: RowType) (row :: k1) (a :: k).
Rep f a -> RowRep f r row a
RowRep (Rep Identity () -> RowRep Identity 'ProductRowT r ())
-> ([Identity Any] -> Rep Identity ())
-> [Identity Any]
-> RowRep Identity 'ProductRowT r ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Identity Any] -> Rep Identity ()
forall {k} (f :: * -> *) (a :: k). [f Any] -> Rep f a
productRep ([Identity Any] -> RowRep Identity 'ProductRowT r ())
-> HasqlRow [Identity Any]
-> Row (RowRep Identity 'ProductRowT r ())
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Proxy r -> HasqlRow [Identity Any]
forall (r :: Row).
AllRowDecoder r =>
Proxy r -> HasqlRow [Identity Any]
genRow (forall (t :: Row). Proxy t
forall {k} (t :: k). Proxy t
Proxy @r)