{-# OPTIONS_HADDOCK not-home #-}

{-# LANGUAGE ScopedTypeVariables #-}

module Opaleye.Internal.PGTypes where

import           Opaleye.Internal.Column (Field, Field_(Column))
import qualified Opaleye.Internal.HaskellDB.PrimQuery as HPQ

import           Data.Proxy (Proxy(..))
import qualified Data.Text as SText
import qualified Data.Text.Encoding as STextEncoding
import qualified Data.Text.Lazy as LText
import qualified Data.Text.Lazy.Encoding as LTextEncoding
import qualified Data.ByteString as SByteString
import qualified Data.ByteString.Lazy as LByteString
import qualified Data.Time.Format.ISO8601.Compat as Time

unsafePgFormatTime :: Time.ISO8601 t => HPQ.Name -> t -> Field c
unsafePgFormatTime :: forall t c. ISO8601 t => Name -> t -> Field c
unsafePgFormatTime Name
typeName = forall (n :: Nullability) c. Name -> Name -> Field_ n c
castToType Name
typeName forall b c a. (b -> c) -> (a -> b) -> a -> c
. t -> Name
format
    where
      format :: t -> Name
format  = Name -> Name
quote forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall t. ISO8601 t => t -> Name
Time.iso8601Show
      quote :: Name -> Name
quote Name
s = Name
"'" forall a. [a] -> [a] -> [a]
++ Name
s forall a. [a] -> [a] -> [a]
++ Name
"'"

literalColumn :: forall a. IsSqlType a => HPQ.Literal -> Field a
literalColumn :: forall a. IsSqlType a => Literal -> Field a
literalColumn = forall (n :: Nullability) sqlType. PrimExpr -> Field_ n sqlType
Column forall b c a. (b -> c) -> (a -> b) -> a -> c
. Name -> PrimExpr -> PrimExpr
HPQ.CastExpr (forall sqlType (proxy :: * -> *).
IsSqlType sqlType =>
proxy sqlType -> Name
showSqlType (forall {k} (t :: k). Proxy t
Proxy :: Proxy a)) forall b c a. (b -> c) -> (a -> b) -> a -> c
. Literal -> PrimExpr
HPQ.ConstExpr

castToType :: HPQ.Name -> String -> Field_ n c
castToType :: forall (n :: Nullability) c. Name -> Name -> Field_ n c
castToType Name
typeName =
    forall (n :: Nullability) sqlType. PrimExpr -> Field_ n sqlType
Column forall b c a. (b -> c) -> (a -> b) -> a -> c
. Name -> PrimExpr -> PrimExpr
HPQ.CastExpr Name
typeName forall b c a. (b -> c) -> (a -> b) -> a -> c
. Literal -> PrimExpr
HPQ.ConstExpr forall b c a. (b -> c) -> (a -> b) -> a -> c
. Name -> Literal
HPQ.OtherLit

strictDecodeUtf8 :: SByteString.ByteString -> String
strictDecodeUtf8 :: ByteString -> Name
strictDecodeUtf8 = Text -> Name
SText.unpack forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> Text
STextEncoding.decodeUtf8

lazyDecodeUtf8 :: LByteString.ByteString -> String
lazyDecodeUtf8 :: ByteString -> Name
lazyDecodeUtf8 = Text -> Name
LText.unpack forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> Text
LTextEncoding.decodeUtf8

class IsSqlType sqlType where
  showSqlType :: proxy sqlType -> String

  {-# MINIMAL showSqlType #-}