{-# OPTIONS_GHC -Wno-orphans #-}
module ClickHaskell.Primitive.TNullable where

-- Internal
import ClickHaskell.Primitive.Serialization


{- | ClickHouse Nullable(T) column type
 (type synonym for Maybe)
 -}
type Nullable = Maybe
instance IsChType chType => IsChType (Nullable chType)
  where
  chTypeName :: String
chTypeName = String
"Nullable(" String -> String -> String
forall a. Semigroup a => a -> a -> a
<> forall chType. IsChType chType => String
chTypeName @chType String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
")"
  defaultValueOfTypeName :: Nullable chType
defaultValueOfTypeName = Nullable chType
forall a. Maybe a
Nothing

instance
  ToChType inputType chType
  =>
  ToChType (Nullable inputType) (Nullable chType)
  where
  toChType :: Nullable chType -> Nullable inputType
toChType = (chType -> inputType) -> Nullable chType -> Nullable inputType
forall a b. (a -> b) -> Maybe a -> Maybe b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (forall chType userType.
ToChType chType userType =>
userType -> chType
toChType @inputType @chType)
  fromChType :: Nullable inputType -> Nullable chType
fromChType = (inputType -> chType) -> Nullable inputType -> Nullable chType
forall a b. (a -> b) -> Maybe a -> Maybe b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (forall chType userType.
ToChType chType userType =>
chType -> userType
fromChType @inputType)

instance ToQueryPart chType => ToQueryPart (Nullable chType)
  where
  toQueryPart :: Nullable chType -> Builder
toQueryPart = Builder -> (chType -> Builder) -> Nullable chType -> Builder
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Builder
"null" chType -> Builder
forall chType. ToQueryPart chType => chType -> Builder
toQueryPart