2018-09-29 08:42:47 +03:00
|
|
|
{-# LANGUAGE FlexibleContexts #-}
|
|
|
|
{-# LANGUAGE MultiParamTypeClasses #-}
|
|
|
|
{-# LANGUAGE OverloadedStrings #-}
|
|
|
|
{-# LANGUAGE TemplateHaskell #-}
|
2018-08-17 17:44:43 +03:00
|
|
|
|
|
|
|
module Hasura.RQL.DDL.Permission.Triggers where
|
|
|
|
|
|
|
|
import Hasura.Prelude
|
2018-09-29 08:42:47 +03:00
|
|
|
import Hasura.RQL.Types
|
|
|
|
import Hasura.Server.Utils
|
2018-08-17 17:44:43 +03:00
|
|
|
import Hasura.SQL.Types
|
|
|
|
|
|
|
|
import qualified Database.PG.Query as Q
|
|
|
|
import qualified Hasura.SQL.DML as S
|
|
|
|
|
2018-09-29 08:42:47 +03:00
|
|
|
import qualified Data.Aeson as J
|
2018-08-17 17:44:43 +03:00
|
|
|
import qualified Data.ByteString.Builder as BB
|
2018-09-29 08:42:47 +03:00
|
|
|
import qualified Data.FileEmbed as FE
|
|
|
|
import qualified Data.Text as T
|
2018-08-17 17:44:43 +03:00
|
|
|
|
|
|
|
buildInsTrig :: QualifiedTable -> Q.Query
|
|
|
|
buildInsTrig qt@(QualifiedTable _ tn) =
|
|
|
|
Q.fromBuilder $ mconcat
|
|
|
|
[ BB.string7 "CREATE TRIGGER " <> toSQL tn
|
|
|
|
, BB.string7 " INSTEAD OF INSERT ON " <> toSQL qt
|
|
|
|
, BB.string7 " FOR EACH ROW EXECUTE PROCEDURE "
|
|
|
|
, toSQL qt <> BB.string7 "();"
|
|
|
|
]
|
|
|
|
|
|
|
|
dropInsTrigFn :: QualifiedTable -> Q.Query
|
|
|
|
dropInsTrigFn fn =
|
|
|
|
Q.fromBuilder $ BB.string7 "DROP FUNCTION " <> toSQL fn <> "()"
|
|
|
|
|
2018-09-29 08:42:47 +03:00
|
|
|
getInsTrigTmplt :: (MonadError QErr m) => m GingerTmplt
|
|
|
|
getInsTrigTmplt =
|
|
|
|
either throwErr return $ parseGingerTmplt trigFnSrc
|
|
|
|
where
|
|
|
|
trigFnSrc = $(FE.embedStringFile "src-rsr/insert_trigger.sql.j2")
|
|
|
|
|
|
|
|
throwErr e = throw500 $ "cannot render insert trigger function template: "
|
|
|
|
<> T.pack e
|
|
|
|
|
|
|
|
buildInsTrigFn
|
|
|
|
:: (MonadError QErr m)
|
|
|
|
=> QualifiedTable -> QualifiedTable -> S.BoolExp -> m Q.Query
|
|
|
|
buildInsTrigFn fn tn be = do
|
|
|
|
insTmplt <- getInsTrigTmplt
|
|
|
|
return $ Q.fromBuilder $ BB.string7 $ T.unpack $
|
|
|
|
renderGingerTmplt tmpltVals insTmplt
|
|
|
|
where
|
|
|
|
tmpltVals = J.object [ "function_name" J..= toSQLTxt fn
|
|
|
|
, "table_name" J..= toSQLTxt tn
|
|
|
|
, "check_expression" J..= toSQLTxt be
|
|
|
|
]
|