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

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

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

asBytes :: Commands -> ByteString
asBytes :: Commands -> ByteString
asBytes (Commands DList Builder
list) =
  LazyByteString -> ByteString
BL.toStrict (LazyByteString -> ByteString) -> LazyByteString -> ByteString
forall a b. (a -> b) -> a -> b
$ Builder -> LazyByteString
BB.toLazyByteString (Builder -> LazyByteString) -> Builder -> LazyByteString
forall a b. (a -> b) -> a -> b
$ (Builder -> Builder) -> DList Builder -> Builder
forall m a. Monoid m => (a -> m) -> DList a -> m
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 a. a -> DList a
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 a. a -> DList a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Builder
"SET client_min_messages TO WARNING")