module Hasql.Engine.PqProcedures.SelectTypeInfo
( SelectTypeInfo (..),
SelectTypeInfoResult,
run,
)
where
import Data.HashMap.Strict qualified as HashMap
import Data.HashSet qualified as HashSet
import Hasql.Codecs.Decoders.Value qualified as Decoders.Value
import Hasql.Codecs.Encoders qualified as Encoders
import Hasql.Codecs.Encoders.Params qualified as Encoders.Params
import Hasql.Comms.ResultDecoder qualified
import Hasql.Comms.Roundtrip qualified
import Hasql.Comms.RowDecoder qualified
import Hasql.Engine.Errors qualified as Errors
import Hasql.Platform.Prelude
import Hasql.Pq qualified as Pq
newtype SelectTypeInfo = SelectTypeInfo
{
SelectTypeInfo -> HashSet (Maybe Text, Text)
keys :: HashSet (Maybe Text, Text)
}
type SelectTypeInfoResult =
HashMap (Maybe Text, Text) (Word32, Word32)
run :: Pq.Connection -> SelectTypeInfo -> IO (Either Errors.SessionError SelectTypeInfoResult)
run :: Connection
-> SelectTypeInfo
-> IO
(Either SessionError (HashMap (Maybe Text, Text) (Word32, Word32)))
run Connection
connection (SelectTypeInfo HashSet (Maybe Text, Text)
keys) =
if HashSet (Maybe Text, Text) -> Bool
forall a. HashSet a -> Bool
HashSet.null HashSet (Maybe Text, Text)
keys
then Either SessionError (HashMap (Maybe Text, Text) (Word32, Word32))
-> IO
(Either SessionError (HashMap (Maybe Text, Text) (Word32, Word32)))
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (HashMap (Maybe Text, Text) (Word32, Word32)
-> Either
SessionError (HashMap (Maybe Text, Text) (Word32, Word32))
forall a b. b -> Either a b
Right HashMap (Maybe Text, Text) (Word32, Word32)
forall k v. HashMap k v
HashMap.empty)
else
(Error () -> SessionError)
-> Either (Error ()) (HashMap (Maybe Text, Text) (Word32, Word32))
-> Either
SessionError (HashMap (Maybe Text, Text) (Word32, Word32))
forall a b c. (a -> b) -> Either a c -> Either b c
forall (p :: * -> * -> *) a b c.
Bifunctor p =>
(a -> b) -> p a c -> p b c
first Error () -> SessionError
forall context. Error context -> SessionError
Errors.fromRoundtripError
(Either (Error ()) (HashMap (Maybe Text, Text) (Word32, Word32))
-> Either
SessionError (HashMap (Maybe Text, Text) (Word32, Word32)))
-> IO
(Either (Error ()) (HashMap (Maybe Text, Text) (Word32, Word32)))
-> IO
(Either SessionError (HashMap (Maybe Text, Text) (Word32, Word32)))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Roundtrip () (HashMap (Maybe Text, Text) (Word32, Word32))
-> Connection
-> IO
(Either (Error ()) (HashMap (Maybe Text, Text) (Word32, Word32)))
forall context a.
Roundtrip context a -> Connection -> IO (Either (Error context) a)
Hasql.Comms.Roundtrip.toSerialIO (SelectTypeInfo
-> Roundtrip () (HashMap (Maybe Text, Text) (Word32, Word32))
roundtrip (HashSet (Maybe Text, Text) -> SelectTypeInfo
SelectTypeInfo HashSet (Maybe Text, Text)
keys)) Connection
connection
sql :: ByteString
sql :: ByteString
sql =
ByteString
"with\n\
\ inputs as (\n\
\ select *\n\
\ from unnest($1, $2) as x(schema_name, type_name)\n\
\ ),\n\
\ unnamespaced_results as (\n\
\ select\n\
\ null :: text as schema_name,\n\
\ pg_type.typname :: text as type_name,\n\
\ pg_type.oid :: int4 as type_oid,\n\
\ pg_type.typarray :: int4 as array_oid\n\
\ from inputs\n\
\ join pg_type on pg_type.oid = to_regtype(inputs.type_name)\n\
\ where inputs.schema_name is null\n\
\ ),\n\
\ namespaced_results as (\n\
\ select\n\
\ pg_namespace.nspname :: text as schema_name,\n\
\ pg_type.typname :: text as type_name,\n\
\ pg_type.oid :: int4 as type_oid,\n\
\ pg_type.typarray :: int4 as array_oid\n\
\ from inputs\n\
\ join pg_namespace on pg_namespace.nspname = inputs.schema_name\n\
\ join pg_type\n\
\ on pg_type.typname = inputs.type_name\n\
\ and pg_type.typnamespace = pg_namespace.oid\n\
\ where inputs.schema_name is not null\n\
\ )\n\
\select * from unnamespaced_results\n\
\union\n\
\select * from namespaced_results"
roundtrip :: SelectTypeInfo -> Hasql.Comms.Roundtrip.Roundtrip () SelectTypeInfoResult
roundtrip :: SelectTypeInfo
-> Roundtrip () (HashMap (Maybe Text, Text) (Word32, Word32))
roundtrip SelectTypeInfo
params =
()
-> ByteString
-> [Maybe (Oid, ByteString, Format)]
-> Format
-> ResultDecoder (HashMap (Maybe Text, Text) (Word32, Word32))
-> Roundtrip () (HashMap (Maybe Text, Text) (Word32, Word32))
forall context a.
context
-> ByteString
-> [Maybe (Oid, ByteString, Format)]
-> Format
-> ResultDecoder a
-> Roundtrip context a
Hasql.Comms.Roundtrip.queryParams () ByteString
sql (SelectTypeInfo -> [Maybe (Oid, ByteString, Format)]
encodeParams SelectTypeInfo
params) Format
Pq.Binary ResultDecoder (HashMap (Maybe Text, Text) (Word32, Word32))
decoder
encodeParams :: SelectTypeInfo -> [Maybe (Pq.Oid, ByteString, Pq.Format)]
encodeParams :: SelectTypeInfo -> [Maybe (Oid, ByteString, Format)]
encodeParams =
(Maybe (Word32, ByteString, Bool)
-> Maybe (Oid, ByteString, Format))
-> [Maybe (Word32, ByteString, Bool)]
-> [Maybe (Oid, ByteString, Format)]
forall a b. (a -> b) -> [a] -> [b]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap
( ((Word32, ByteString, Bool) -> (Oid, ByteString, Format))
-> Maybe (Word32, ByteString, Bool)
-> Maybe (Oid, ByteString, Format)
forall a b. (a -> b) -> Maybe a -> Maybe b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap
( \(Word32
oid, ByteString
bytes, Bool
format) ->
( CUInt -> Oid
Pq.Oid (Word32 -> CUInt
forall a b. (Integral a, Num b) => a -> b
fromIntegral Word32
oid),
ByteString
bytes,
Format -> Format -> Bool -> Format
forall a. a -> a -> Bool -> a
bool Format
Pq.Binary Format
Pq.Text Bool
format
)
)
)
([Maybe (Word32, ByteString, Bool)]
-> [Maybe (Oid, ByteString, Format)])
-> (SelectTypeInfo -> [Maybe (Word32, ByteString, Bool)])
-> SelectTypeInfo
-> [Maybe (Oid, ByteString, Format)]
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. Params SelectTypeInfo
-> HashMap (Maybe Text, Text) (Word32, Word32)
-> SelectTypeInfo
-> [Maybe (Word32, ByteString, Bool)]
forall a.
Params a
-> HashMap (Maybe Text, Text) (Word32, Word32)
-> a
-> [Maybe (Word32, ByteString, Bool)]
Encoders.Params.compileUnpreparedStatementData Params SelectTypeInfo
paramsEncoder HashMap (Maybe Text, Text) (Word32, Word32)
forall a. Monoid a => a
mempty
paramsEncoder :: Encoders.Params SelectTypeInfo
paramsEncoder :: Params SelectTypeInfo
paramsEncoder =
(\(SelectTypeInfo HashSet (Maybe Text, Text)
keys) -> [(Maybe Text, Text)] -> ([Maybe Text], [Text])
forall a b. [(a, b)] -> ([a], [b])
unzip (HashSet (Maybe Text, Text) -> [(Maybe Text, Text)]
forall a. HashSet a -> [a]
HashSet.toList HashSet (Maybe Text, Text)
keys))
(SelectTypeInfo -> ([Maybe Text], [Text]))
-> Params ([Maybe Text], [Text]) -> Params SelectTypeInfo
forall (f :: * -> *) a b. Contravariant f => (a -> b) -> f b -> f a
>$< [Params ([Maybe Text], [Text])] -> Params ([Maybe Text], [Text])
forall a. Monoid a => [a] -> a
mconcat
[ ([Maybe Text], [Text]) -> [Maybe Text]
forall a b. (a, b) -> a
fst (([Maybe Text], [Text]) -> [Maybe Text])
-> Params [Maybe Text] -> Params ([Maybe Text], [Text])
forall (f :: * -> *) a b. Contravariant f => (a -> b) -> f b -> f a
>$< NullableOrNot Value [Maybe Text] -> Params [Maybe Text]
forall a. NullableOrNot Value a -> Params a
Encoders.param (Value [Maybe Text] -> NullableOrNot Value [Maybe Text]
forall (encoder :: * -> *) a. encoder a -> NullableOrNot encoder a
Encoders.nonNullable (NullableOrNot Value (Maybe Text) -> Value [Maybe Text]
forall (foldable :: * -> *) element.
Foldable foldable =>
NullableOrNot Value element -> Value (foldable element)
Encoders.foldableArray (Value Text -> NullableOrNot Value (Maybe Text)
forall (encoder :: * -> *) a.
encoder a -> NullableOrNot encoder (Maybe a)
Encoders.nullable Value Text
Encoders.text))),
([Maybe Text], [Text]) -> [Text]
forall a b. (a, b) -> b
snd (([Maybe Text], [Text]) -> [Text])
-> Params [Text] -> Params ([Maybe Text], [Text])
forall (f :: * -> *) a b. Contravariant f => (a -> b) -> f b -> f a
>$< NullableOrNot Value [Text] -> Params [Text]
forall a. NullableOrNot Value a -> Params a
Encoders.param (Value [Text] -> NullableOrNot Value [Text]
forall (encoder :: * -> *) a. encoder a -> NullableOrNot encoder a
Encoders.nonNullable (NullableOrNot Value Text -> Value [Text]
forall (foldable :: * -> *) element.
Foldable foldable =>
NullableOrNot Value element -> Value (foldable element)
Encoders.foldableArray (Value Text -> NullableOrNot Value Text
forall (encoder :: * -> *) a. encoder a -> NullableOrNot encoder a
Encoders.nonNullable Value Text
Encoders.text)))
]
decoder :: Hasql.Comms.ResultDecoder.ResultDecoder SelectTypeInfoResult
decoder :: ResultDecoder (HashMap (Maybe Text, Text) (Word32, Word32))
decoder =
(HashMap (Maybe Text, Text) (Word32, Word32)
-> (Maybe Text, Text, Word32, Word32)
-> HashMap (Maybe Text, Text) (Word32, Word32))
-> HashMap (Maybe Text, Text) (Word32, Word32)
-> RowDecoder (Maybe Text, Text, Word32, Word32)
-> ResultDecoder (HashMap (Maybe Text, Text) (Word32, Word32))
forall a b. (a -> b -> a) -> a -> RowDecoder b -> ResultDecoder a
Hasql.Comms.ResultDecoder.foldl HashMap (Maybe Text, Text) (Word32, Word32)
-> (Maybe Text, Text, Word32, Word32)
-> HashMap (Maybe Text, Text) (Word32, Word32)
forall {a} {b} {a} {b}.
(Hashable a, Hashable b) =>
HashMap (a, b) (a, b) -> (a, b, a, b) -> HashMap (a, b) (a, b)
step HashMap (Maybe Text, Text) (Word32, Word32)
forall k v. HashMap k v
HashMap.empty RowDecoder (Maybe Text, Text, Word32, Word32)
rowDecoder
where
step :: HashMap (a, b) (a, b) -> (a, b, a, b) -> HashMap (a, b) (a, b)
step HashMap (a, b) (a, b)
acc (a
schemaName, b
typeName, a
typeOid, b
arrayOid) =
(a, b) -> (a, b) -> HashMap (a, b) (a, b) -> HashMap (a, b) (a, b)
forall k v. Hashable k => k -> v -> HashMap k v -> HashMap k v
HashMap.insert (a
schemaName, b
typeName) (a
typeOid, b
arrayOid) HashMap (a, b) (a, b)
acc
rowDecoder :: Hasql.Comms.RowDecoder.RowDecoder (Maybe Text, Text, Word32, Word32)
rowDecoder :: RowDecoder (Maybe Text, Text, Word32, Word32)
rowDecoder =
(,,,)
(Maybe Text
-> Text -> Word32 -> Word32 -> (Maybe Text, Text, Word32, Word32))
-> RowDecoder (Maybe Text)
-> RowDecoder
(Text -> Word32 -> Word32 -> (Maybe Text, Text, Word32, Word32))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Value Text -> RowDecoder (Maybe Text)
forall {a}. Value a -> RowDecoder (Maybe a)
nullableColumn Value Text
Decoders.Value.text
RowDecoder
(Text -> Word32 -> Word32 -> (Maybe Text, Text, Word32, Word32))
-> RowDecoder Text
-> RowDecoder
(Word32 -> Word32 -> (Maybe Text, Text, Word32, Word32))
forall a b. RowDecoder (a -> b) -> RowDecoder a -> RowDecoder b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Value Text -> RowDecoder Text
forall {a}. Value a -> RowDecoder a
nonNullableColumn Value Text
Decoders.Value.text
RowDecoder (Word32 -> Word32 -> (Maybe Text, Text, Word32, Word32))
-> RowDecoder Word32
-> RowDecoder (Word32 -> (Maybe Text, Text, Word32, Word32))
forall a b. RowDecoder (a -> b) -> RowDecoder a -> RowDecoder b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Value Word32 -> RowDecoder Word32
forall {a}. Value a -> RowDecoder a
nonNullableColumn (Int32 -> Word32
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int32 -> Word32) -> Value Int32 -> Value Word32
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Value Int32
Decoders.Value.int4)
RowDecoder (Word32 -> (Maybe Text, Text, Word32, Word32))
-> RowDecoder Word32
-> RowDecoder (Maybe Text, Text, Word32, Word32)
forall a b. RowDecoder (a -> b) -> RowDecoder a -> RowDecoder b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Value Word32 -> RowDecoder Word32
forall {a}. Value a -> RowDecoder a
nonNullableColumn (Int32 -> Word32
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int32 -> Word32) -> Value Int32 -> Value Word32
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Value Int32
Decoders.Value.int4)
where
nullableColumn :: Value a -> RowDecoder (Maybe a)
nullableColumn Value a
valueDecoder =
Maybe Word32
-> (ByteString -> Either Text a) -> RowDecoder (Maybe a)
forall a.
Maybe Word32
-> (ByteString -> Either Text a) -> RowDecoder (Maybe a)
Hasql.Comms.RowDecoder.nullableColumn
(Value a -> Maybe Word32
forall a. Value a -> Maybe Word32
Decoders.Value.toBaseOid Value a
valueDecoder)
(Value a
-> HashMap (Maybe Text, Text) (Word32, Word32)
-> ByteString
-> Either Text a
forall a.
Value a
-> HashMap (Maybe Text, Text) (Word32, Word32)
-> ByteString
-> Either Text a
Decoders.Value.toByteStringParser Value a
valueDecoder HashMap (Maybe Text, Text) (Word32, Word32)
forall a. Monoid a => a
mempty)
nonNullableColumn :: Value a -> RowDecoder a
nonNullableColumn Value a
valueDecoder =
Maybe Word32 -> (ByteString -> Either Text a) -> RowDecoder a
forall a.
Maybe Word32 -> (ByteString -> Either Text a) -> RowDecoder a
Hasql.Comms.RowDecoder.nonNullableColumn
(Value a -> Maybe Word32
forall a. Value a -> Maybe Word32
Decoders.Value.toBaseOid Value a
valueDecoder)
(Value a
-> HashMap (Maybe Text, Text) (Word32, Word32)
-> ByteString
-> Either Text a
forall a.
Value a
-> HashMap (Maybe Text, Text) (Word32, Word32)
-> ByteString
-> Either Text a
Decoders.Value.toByteStringParser Value a
valueDecoder HashMap (Maybe Text, Text) (Word32, Word32)
forall a. Monoid a => a
mempty)