{-# LANGUAGE
AllowAmbiguousTypes
, ConstraintKinds
, DeriveAnyClass
, DeriveGeneric
, DerivingStrategies
, FlexibleContexts
, FlexibleInstances
, GADTs
, LambdaCase
, MultiParamTypeClasses
, OverloadedLabels
, OverloadedStrings
, RankNTypes
, ScopedTypeVariables
, TypeApplications
, DataKinds
, PolyKinds
, TypeOperators
, UndecidableSuperClasses
#-}
module Squeal.PostgreSQL.Definition.Procedure
(
createProcedure
, createOrReplaceProcedure
, dropProcedure
, dropProcedureIfExists
, ProcedureDefinition(..)
, languageSqlManipulation
) where
import Control.DeepSeq
import Data.ByteString
import GHC.TypeLits
import qualified Generics.SOP as SOP
import qualified GHC.Generics as GHC
import Squeal.PostgreSQL.Type.Alias
import Squeal.PostgreSQL.Definition
import Squeal.PostgreSQL.Expression.Type
import Squeal.PostgreSQL.Type.List
import Squeal.PostgreSQL.Manipulation
import Squeal.PostgreSQL.Render
import Squeal.PostgreSQL.Type.Schema
createProcedure
:: ( Has sch db schema
, KnownSymbol pro
, SOP.SListI args )
=> QualifiedAlias sch pro
-> NP (TypeExpression db) args
-> ProcedureDefinition db args
-> Definition db (Alter sch (Create pro ('Procedure args) schema) db)
createProcedure :: forall (sch :: Symbol) (db :: [(Symbol, SchemaType)])
(schema :: SchemaType) (pro :: Symbol) (args :: [NullType]).
(Has sch db schema, KnownSymbol pro, SListI args) =>
QualifiedAlias sch pro
-> NP (TypeExpression db) args
-> ProcedureDefinition db args
-> Definition
db (Alter sch (Create pro ('Procedure args) schema) db)
createProcedure QualifiedAlias sch pro
pro NP (TypeExpression db) args
args ProcedureDefinition db args
prodef = forall (db0 :: [(Symbol, SchemaType)])
(db1 :: [(Symbol, SchemaType)]).
ByteString -> Definition db0 db1
UnsafeDefinition forall a b. (a -> b) -> a -> b
$
ByteString
"CREATE" ByteString -> ByteString -> ByteString
<+> ByteString
"PROCEDURE" ByteString -> ByteString -> ByteString
<+> forall sql. RenderSQL sql => sql -> ByteString
renderSQL QualifiedAlias sch pro
pro
ByteString -> ByteString -> ByteString
<+> ByteString -> ByteString
parenthesized (forall {k} (xs :: [k]) (expression :: k -> *).
SListI xs =>
(forall (x :: k). expression x -> ByteString)
-> NP expression xs -> ByteString
renderCommaSeparated forall sql. RenderSQL sql => sql -> ByteString
renderSQL NP (TypeExpression db) args
args)
ByteString -> ByteString -> ByteString
<+> forall sql. RenderSQL sql => sql -> ByteString
renderSQL ProcedureDefinition db args
prodef forall a. Semigroup a => a -> a -> a
<> ByteString
";"
createOrReplaceProcedure
:: ( Has sch db schema
, KnownSymbol pro
, SOP.SListI args )
=> QualifiedAlias sch pro
-> NP (TypeExpression db) args
-> ProcedureDefinition db args
-> Definition db (Alter sch (CreateOrReplace pro ('Procedure args) schema) db)
createOrReplaceProcedure :: forall (sch :: Symbol) (db :: [(Symbol, SchemaType)])
(schema :: SchemaType) (pro :: Symbol) (args :: [NullType]).
(Has sch db schema, KnownSymbol pro, SListI args) =>
QualifiedAlias sch pro
-> NP (TypeExpression db) args
-> ProcedureDefinition db args
-> Definition
db (Alter sch (CreateOrReplace pro ('Procedure args) schema) db)
createOrReplaceProcedure QualifiedAlias sch pro
pro NP (TypeExpression db) args
args ProcedureDefinition db args
prodef = forall (db0 :: [(Symbol, SchemaType)])
(db1 :: [(Symbol, SchemaType)]).
ByteString -> Definition db0 db1
UnsafeDefinition forall a b. (a -> b) -> a -> b
$
ByteString
"CREATE" ByteString -> ByteString -> ByteString
<+> ByteString
"OR" ByteString -> ByteString -> ByteString
<+> ByteString
"REPLACE" ByteString -> ByteString -> ByteString
<+> ByteString
"PROCEDURE" ByteString -> ByteString -> ByteString
<+> forall sql. RenderSQL sql => sql -> ByteString
renderSQL QualifiedAlias sch pro
pro
ByteString -> ByteString -> ByteString
<+> ByteString -> ByteString
parenthesized (forall {k} (xs :: [k]) (expression :: k -> *).
SListI xs =>
(forall (x :: k). expression x -> ByteString)
-> NP expression xs -> ByteString
renderCommaSeparated forall sql. RenderSQL sql => sql -> ByteString
renderSQL NP (TypeExpression db) args
args)
ByteString -> ByteString -> ByteString
<+> forall sql. RenderSQL sql => sql -> ByteString
renderSQL ProcedureDefinition db args
prodef forall a. Semigroup a => a -> a -> a
<> ByteString
";"
languageSqlManipulation
:: [Manipulation '[] db args '[]]
-> ProcedureDefinition db args
languageSqlManipulation :: forall (db :: [(Symbol, SchemaType)]) (args :: [NullType]).
[Manipulation '[] db args '[]] -> ProcedureDefinition db args
languageSqlManipulation [Manipulation '[] db args '[]]
mnps = forall {k} {k} (db :: k) (args :: k).
ByteString -> ProcedureDefinition db args
UnsafeProcedureDefinition forall a b. (a -> b) -> a -> b
$
ByteString
"language sql as" ByteString -> ByteString -> ByteString
<+> ByteString
"$$" ByteString -> ByteString -> ByteString
<+> forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
Prelude.foldr ByteString -> ByteString -> ByteString
(<+>) ByteString
"" (forall a b. (a -> b) -> [a] -> [b]
Prelude.map ((forall a. Semigroup a => a -> a -> a
<> ByteString
";") forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall sql. RenderSQL sql => sql -> ByteString
renderSQL) [Manipulation '[] db args '[]]
mnps) forall a. Semigroup a => a -> a -> a
<> ByteString
"$$"
dropProcedure
:: (Has sch db schema, KnownSymbol pro)
=> QualifiedAlias sch pro
-> Definition db (Alter sch (DropSchemum pro 'Procedure schema) db)
dropProcedure :: forall (sch :: Symbol) (db :: [(Symbol, SchemaType)])
(schema :: SchemaType) (pro :: Symbol).
(Has sch db schema, KnownSymbol pro) =>
QualifiedAlias sch pro
-> Definition db (Alter sch (DropSchemum pro 'Procedure schema) db)
dropProcedure QualifiedAlias sch pro
pro = forall (db0 :: [(Symbol, SchemaType)])
(db1 :: [(Symbol, SchemaType)]).
ByteString -> Definition db0 db1
UnsafeDefinition forall a b. (a -> b) -> a -> b
$
ByteString
"DROP PROCEDURE" ByteString -> ByteString -> ByteString
<+> forall sql. RenderSQL sql => sql -> ByteString
renderSQL QualifiedAlias sch pro
pro forall a. Semigroup a => a -> a -> a
<> ByteString
";"
dropProcedureIfExists
:: (Has sch db schema, KnownSymbol pro)
=> QualifiedAlias sch pro
-> Definition db (Alter sch (DropSchemumIfExists pro 'Procedure schema) db)
dropProcedureIfExists :: forall (sch :: Symbol) (db :: [(Symbol, SchemaType)])
(schema :: SchemaType) (pro :: Symbol).
(Has sch db schema, KnownSymbol pro) =>
QualifiedAlias sch pro
-> Definition
db (Alter sch (DropSchemumIfExists pro 'Procedure schema) db)
dropProcedureIfExists QualifiedAlias sch pro
pro = forall (db0 :: [(Symbol, SchemaType)])
(db1 :: [(Symbol, SchemaType)]).
ByteString -> Definition db0 db1
UnsafeDefinition forall a b. (a -> b) -> a -> b
$
ByteString
"DROP PROCEDURE IF EXISTS" ByteString -> ByteString -> ByteString
<+> forall sql. RenderSQL sql => sql -> ByteString
renderSQL QualifiedAlias sch pro
pro forall a. Semigroup a => a -> a -> a
<> ByteString
";"
newtype ProcedureDefinition db args = UnsafeProcedureDefinition
{ forall {k} {k} (db :: k) (args :: k).
ProcedureDefinition db args -> ByteString
renderProcedureDefinition :: ByteString }
deriving (ProcedureDefinition db args -> ProcedureDefinition db args -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
forall k (db :: k) k (args :: k).
ProcedureDefinition db args -> ProcedureDefinition db args -> Bool
/= :: ProcedureDefinition db args -> ProcedureDefinition db args -> Bool
$c/= :: forall k (db :: k) k (args :: k).
ProcedureDefinition db args -> ProcedureDefinition db args -> Bool
== :: ProcedureDefinition db args -> ProcedureDefinition db args -> Bool
$c== :: forall k (db :: k) k (args :: k).
ProcedureDefinition db args -> ProcedureDefinition db args -> Bool
Eq,Int -> ProcedureDefinition db args -> ShowS
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
forall k (db :: k) k (args :: k).
Int -> ProcedureDefinition db args -> ShowS
forall k (db :: k) k (args :: k).
[ProcedureDefinition db args] -> ShowS
forall k (db :: k) k (args :: k).
ProcedureDefinition db args -> String
showList :: [ProcedureDefinition db args] -> ShowS
$cshowList :: forall k (db :: k) k (args :: k).
[ProcedureDefinition db args] -> ShowS
show :: ProcedureDefinition db args -> String
$cshow :: forall k (db :: k) k (args :: k).
ProcedureDefinition db args -> String
showsPrec :: Int -> ProcedureDefinition db args -> ShowS
$cshowsPrec :: forall k (db :: k) k (args :: k).
Int -> ProcedureDefinition db args -> ShowS
Show,forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
forall k (db :: k) k (args :: k) x.
Rep (ProcedureDefinition db args) x -> ProcedureDefinition db args
forall k (db :: k) k (args :: k) x.
ProcedureDefinition db args -> Rep (ProcedureDefinition db args) x
$cto :: forall k (db :: k) k (args :: k) x.
Rep (ProcedureDefinition db args) x -> ProcedureDefinition db args
$cfrom :: forall k (db :: k) k (args :: k) x.
ProcedureDefinition db args -> Rep (ProcedureDefinition db args) x
GHC.Generic,forall a. (a -> ()) -> NFData a
forall k (db :: k) k (args :: k). ProcedureDefinition db args -> ()
rnf :: ProcedureDefinition db args -> ()
$crnf :: forall k (db :: k) k (args :: k). ProcedureDefinition db args -> ()
NFData)
instance RenderSQL (ProcedureDefinition db args) where
renderSQL :: ProcedureDefinition db args -> ByteString
renderSQL = forall {k} {k} (db :: k) (args :: k).
ProcedureDefinition db args -> ByteString
renderProcedureDefinition