{-# LANGUAGE
AllowAmbiguousTypes
, ConstraintKinds
, DeriveAnyClass
, DeriveGeneric
, DerivingStrategies
, FlexibleContexts
, FlexibleInstances
, GADTs
, LambdaCase
, MultiParamTypeClasses
, OverloadedLabels
, OverloadedStrings
, RankNTypes
, ScopedTypeVariables
, TypeApplications
, TypeInType
, 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 :: 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 = ByteString
-> Definition
db (Alter sch (Create pro ('Procedure args) schema) db)
forall (db0 :: [(Symbol, SchemaType)])
(db1 :: [(Symbol, SchemaType)]).
ByteString -> Definition db0 db1
UnsafeDefinition (ByteString
-> Definition
db (Alter sch (Create pro ('Procedure args) schema) db))
-> ByteString
-> Definition
db (Alter sch (Create pro ('Procedure args) schema) db)
forall a b. (a -> b) -> a -> b
$
ByteString
"CREATE" ByteString -> ByteString -> ByteString
<+> ByteString
"PROCEDURE" ByteString -> ByteString -> ByteString
<+> QualifiedAlias sch pro -> ByteString
forall sql. RenderSQL sql => sql -> ByteString
renderSQL QualifiedAlias sch pro
pro
ByteString -> ByteString -> ByteString
<+> ByteString -> ByteString
parenthesized ((forall (x :: NullType). TypeExpression db x -> ByteString)
-> NP (TypeExpression db) args -> ByteString
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
forall (x :: NullType). TypeExpression db x -> ByteString
renderSQL NP (TypeExpression db) args
args)
ByteString -> ByteString -> ByteString
<+> ProcedureDefinition db args -> ByteString
forall sql. RenderSQL sql => sql -> ByteString
renderSQL ProcedureDefinition db args
prodef ByteString -> ByteString -> ByteString
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 :: 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 = ByteString
-> Definition
db (Alter sch (CreateOrReplace pro ('Procedure args) schema) db)
forall (db0 :: [(Symbol, SchemaType)])
(db1 :: [(Symbol, SchemaType)]).
ByteString -> Definition db0 db1
UnsafeDefinition (ByteString
-> Definition
db (Alter sch (CreateOrReplace pro ('Procedure args) schema) db))
-> ByteString
-> Definition
db (Alter sch (CreateOrReplace pro ('Procedure args) schema) db)
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
<+> QualifiedAlias sch pro -> ByteString
forall sql. RenderSQL sql => sql -> ByteString
renderSQL QualifiedAlias sch pro
pro
ByteString -> ByteString -> ByteString
<+> ByteString -> ByteString
parenthesized ((forall (x :: NullType). TypeExpression db x -> ByteString)
-> NP (TypeExpression db) args -> ByteString
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
forall (x :: NullType). TypeExpression db x -> ByteString
renderSQL NP (TypeExpression db) args
args)
ByteString -> ByteString -> ByteString
<+> ProcedureDefinition db args -> ByteString
forall sql. RenderSQL sql => sql -> ByteString
renderSQL ProcedureDefinition db args
prodef ByteString -> ByteString -> ByteString
forall a. Semigroup a => a -> a -> a
<> ByteString
";"
languageSqlManipulation
:: [Manipulation '[] db args '[]]
-> ProcedureDefinition db args
languageSqlManipulation :: [Manipulation '[] db args '[]] -> ProcedureDefinition db args
languageSqlManipulation [Manipulation '[] db args '[]]
mnps = ByteString -> ProcedureDefinition db args
forall k k (db :: k) (args :: k).
ByteString -> ProcedureDefinition db args
UnsafeProcedureDefinition (ByteString -> ProcedureDefinition db args)
-> ByteString -> ProcedureDefinition db args
forall a b. (a -> b) -> a -> b
$
ByteString
"language sql as" ByteString -> ByteString -> ByteString
<+> ByteString
"$$" ByteString -> ByteString -> 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
"" ((Manipulation '[] db args '[] -> ByteString)
-> [Manipulation '[] db args '[]] -> [ByteString]
forall a b. (a -> b) -> [a] -> [b]
Prelude.map ((ByteString -> ByteString -> ByteString
forall a. Semigroup a => a -> a -> a
<> ByteString
";") (ByteString -> ByteString)
-> (Manipulation '[] db args '[] -> ByteString)
-> Manipulation '[] db args '[]
-> ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Manipulation '[] db args '[] -> ByteString
forall sql. RenderSQL sql => sql -> ByteString
renderSQL) [Manipulation '[] db args '[]]
mnps) ByteString -> ByteString -> ByteString
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 :: QualifiedAlias sch pro
-> Definition db (Alter sch (DropSchemum pro 'Procedure schema) db)
dropProcedure QualifiedAlias sch pro
pro = ByteString
-> Definition db (Alter sch (DropSchemum pro 'Procedure schema) db)
forall (db0 :: [(Symbol, SchemaType)])
(db1 :: [(Symbol, SchemaType)]).
ByteString -> Definition db0 db1
UnsafeDefinition (ByteString
-> Definition
db (Alter sch (DropSchemum pro 'Procedure schema) db))
-> ByteString
-> Definition db (Alter sch (DropSchemum pro 'Procedure schema) db)
forall a b. (a -> b) -> a -> b
$
ByteString
"DROP PROCEDURE" ByteString -> ByteString -> ByteString
<+> QualifiedAlias sch pro -> ByteString
forall sql. RenderSQL sql => sql -> ByteString
renderSQL QualifiedAlias sch pro
pro ByteString -> ByteString -> ByteString
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 :: QualifiedAlias sch pro
-> Definition
db (Alter sch (DropSchemumIfExists pro 'Procedure schema) db)
dropProcedureIfExists QualifiedAlias sch pro
pro = ByteString
-> Definition
db (Alter sch (DropSchemumIfExists pro 'Procedure schema) db)
forall (db0 :: [(Symbol, SchemaType)])
(db1 :: [(Symbol, SchemaType)]).
ByteString -> Definition db0 db1
UnsafeDefinition (ByteString
-> Definition
db (Alter sch (DropSchemumIfExists pro 'Procedure schema) db))
-> ByteString
-> Definition
db (Alter sch (DropSchemumIfExists pro 'Procedure schema) db)
forall a b. (a -> b) -> a -> b
$
ByteString
"DROP PROCEDURE IF EXISTS" ByteString -> ByteString -> ByteString
<+> QualifiedAlias sch pro -> ByteString
forall sql. RenderSQL sql => sql -> ByteString
renderSQL QualifiedAlias sch pro
pro ByteString -> ByteString -> ByteString
forall a. Semigroup a => a -> a -> a
<> ByteString
";"
newtype ProcedureDefinition db args = UnsafeProcedureDefinition
{ ProcedureDefinition db args -> ByteString
renderProcedureDefinition :: ByteString }
deriving (ProcedureDefinition db args -> ProcedureDefinition db args -> Bool
(ProcedureDefinition db args
-> ProcedureDefinition db args -> Bool)
-> (ProcedureDefinition db args
-> ProcedureDefinition db args -> Bool)
-> Eq (ProcedureDefinition db args)
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
[ProcedureDefinition db args] -> ShowS
ProcedureDefinition db args -> String
(Int -> ProcedureDefinition db args -> ShowS)
-> (ProcedureDefinition db args -> String)
-> ([ProcedureDefinition db args] -> ShowS)
-> Show (ProcedureDefinition db args)
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 x.
ProcedureDefinition db args -> Rep (ProcedureDefinition db args) x)
-> (forall x.
Rep (ProcedureDefinition db args) x -> ProcedureDefinition db args)
-> Generic (ProcedureDefinition db args)
forall x.
Rep (ProcedureDefinition db args) x -> ProcedureDefinition db args
forall x.
ProcedureDefinition db args -> Rep (ProcedureDefinition db args) x
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,ProcedureDefinition db args -> ()
(ProcedureDefinition db args -> ())
-> NFData (ProcedureDefinition db args)
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 = ProcedureDefinition db args -> ByteString
forall k (db :: k) k (args :: k).
ProcedureDefinition db args -> ByteString
renderProcedureDefinition