module Rel8.Statement.SQL
  ( showDelete
  , showInsert
  , showUpdate
  , showStatement
  )
where

-- base
import Prelude

-- opaleye
import qualified Opaleye.Internal.Tag as Opaleye

-- rel8
import Rel8.Statement (Statement, ppDecodeStatement)
import Rel8.Statement.Delete ( Delete, ppDelete )
import Rel8.Statement.Insert ( Insert, ppInsert )
import Rel8.Statement.Rows (Rows (Void))
import Rel8.Statement.Select (ppSelect)
import Rel8.Statement.Update ( Update, ppUpdate )

-- transformers
import Control.Monad.Trans.State.Strict (evalState)


-- | Convert a 'Delete' to a 'String' containing a @DELETE@ statement.
showDelete :: Delete a -> String
showDelete :: forall a. Delete a -> String
showDelete = Doc -> String
forall a. Show a => a -> String
show (Doc -> String) -> (Delete a -> Doc) -> Delete a -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (State Tag Doc -> Tag -> Doc
forall s a. State s a -> s -> a
`evalState` Tag
Opaleye.start) (State Tag Doc -> Doc)
-> (Delete a -> State Tag Doc) -> Delete a -> Doc
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Delete a -> State Tag Doc
forall a. Delete a -> State Tag Doc
ppDelete


-- | Convert an 'Insert' to a 'String' containing an @INSERT@ statement.
showInsert :: Insert a -> String
showInsert :: forall a. Insert a -> String
showInsert = Doc -> String
forall a. Show a => a -> String
show (Doc -> String) -> (Insert a -> Doc) -> Insert a -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (State Tag Doc -> Tag -> Doc
forall s a. State s a -> s -> a
`evalState` Tag
Opaleye.start) (State Tag Doc -> Doc)
-> (Insert a -> State Tag Doc) -> Insert a -> Doc
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Insert a -> State Tag Doc
forall a. Insert a -> State Tag Doc
ppInsert


-- | Convert an 'Update' to a 'String' containing an @UPDATE@ statement.
showUpdate :: Update a -> String
showUpdate :: forall a. Update a -> String
showUpdate = Doc -> String
forall a. Show a => a -> String
show (Doc -> String) -> (Update a -> Doc) -> Update a -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (State Tag Doc -> Tag -> Doc
forall s a. State s a -> s -> a
`evalState` Tag
Opaleye.start) (State Tag Doc -> Doc)
-> (Update a -> State Tag Doc) -> Update a -> Doc
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Update a -> State Tag Doc
forall a. Update a -> State Tag Doc
ppUpdate


-- | Convert a 'Statement' to a 'String' containing an SQL statement.
showStatement :: Statement a -> String
showStatement :: forall a. Statement a -> String
showStatement = Doc -> String
forall a. Show a => a -> String
show (Doc -> String) -> (Statement a -> Doc) -> Statement a -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Doc, Result ()) -> Doc
forall a b. (a, b) -> a
fst ((Doc, Result ()) -> Doc)
-> (Statement a -> (Doc, Result ())) -> Statement a -> Doc
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (forall x. Table Expr x => Query x -> State Tag Doc)
-> Rows a () -> Statement a -> (Doc, Result ())
forall exprs a.
(forall x. Table Expr x => Query x -> State Tag Doc)
-> Rows exprs a -> Statement exprs -> (Doc, Result a)
ppDecodeStatement Query x -> State Tag Doc
forall x. Table Expr x => Query x -> State Tag Doc
ppSelect Rows a ()
forall returning. Rows returning ()
Void