module Hasql.Private.Commands
  ( Commands,
    asBytes,
    setEncodersToUTF8,
    setMinClientMessagesToWarning,
  )
where

import qualified Data.ByteString as B
import qualified Data.ByteString.Builder as BB
import qualified Data.ByteString.Lazy as BL
import Hasql.Private.Prelude

newtype Commands
  = Commands (DList BB.Builder)
  deriving (NonEmpty Commands -> Commands
Commands -> Commands -> Commands
forall b. Integral b => b -> Commands -> Commands
forall a.
(a -> a -> a)
-> (NonEmpty a -> a)
-> (forall b. Integral b => b -> a -> a)
-> Semigroup a
stimes :: forall b. Integral b => b -> Commands -> Commands
$cstimes :: forall b. Integral b => b -> Commands -> Commands
sconcat :: NonEmpty Commands -> Commands
$csconcat :: NonEmpty Commands -> Commands
<> :: Commands -> Commands -> Commands
$c<> :: Commands -> Commands -> Commands
Semigroup, Semigroup Commands
Commands
[Commands] -> Commands
Commands -> Commands -> Commands
forall a.
Semigroup a -> a -> (a -> a -> a) -> ([a] -> a) -> Monoid a
mconcat :: [Commands] -> Commands
$cmconcat :: [Commands] -> Commands
mappend :: Commands -> Commands -> Commands
$cmappend :: Commands -> Commands -> Commands
mempty :: Commands
$cmempty :: Commands
Monoid)

asBytes :: Commands -> ByteString
asBytes :: Commands -> ByteString
asBytes (Commands DList Builder
list) =
  ByteString -> ByteString
BL.toStrict forall a b. (a -> b) -> a -> b
$ Builder -> ByteString
BB.toLazyByteString forall a b. (a -> b) -> a -> b
$ forall (t :: * -> *) m a.
(Foldable t, Monoid m) =>
(a -> m) -> t a -> m
foldMap (forall a. Semigroup a => a -> a -> a
<> Char -> Builder
BB.char7 Char
';') forall a b. (a -> b) -> a -> b
$ DList Builder
list

setEncodersToUTF8 :: Commands
setEncodersToUTF8 :: Commands
setEncodersToUTF8 =
  DList Builder -> Commands
Commands (forall (f :: * -> *) a. Applicative f => a -> f a
pure Builder
"SET client_encoding = 'UTF8'")

setMinClientMessagesToWarning :: Commands
setMinClientMessagesToWarning :: Commands
setMinClientMessagesToWarning =
  DList Builder -> Commands
Commands (forall (f :: * -> *) a. Applicative f => a -> f a
pure Builder
"SET client_min_messages TO WARNING")