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

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


newtype Commands =
  Commands (DList BB.Builder)
  deriving (b -> Commands -> Commands
NonEmpty Commands -> Commands
Commands -> Commands -> Commands
(Commands -> Commands -> Commands)
-> (NonEmpty Commands -> Commands)
-> (forall b. Integral b => b -> Commands -> Commands)
-> Semigroup 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 :: 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
Semigroup Commands
-> Commands
-> (Commands -> Commands -> Commands)
-> ([Commands] -> Commands)
-> Monoid 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
$cp1Monoid :: Semigroup Commands
Monoid)

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

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

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