refactor: split pre-request function out of setPgLocals
This commit is contained in:
@@ -223,4 +223,5 @@ handleRequest AuthResult{..} conf appState authenticated prepared jsonDbS pgVer
|
|||||||
runQuery mode query =
|
runQuery mode query =
|
||||||
runDbHandler appState mode authenticated prepared $ do
|
runDbHandler appState mode authenticated prepared $ do
|
||||||
Query.setPgLocals conf authClaims authRole apiReq jsonDbS pgVer
|
Query.setPgLocals conf authClaims authRole apiReq jsonDbS pgVer
|
||||||
|
Query.runPreReq conf
|
||||||
query
|
query
|
||||||
|
|||||||
+12
-7
@@ -9,6 +9,7 @@ module PostgREST.Query
|
|||||||
, singleUpsertQuery
|
, singleUpsertQuery
|
||||||
, updateQuery
|
, updateQuery
|
||||||
, setPgLocals
|
, setPgLocals
|
||||||
|
, runPreReq
|
||||||
, DbHandler
|
, DbHandler
|
||||||
) where
|
) where
|
||||||
|
|
||||||
@@ -232,14 +233,13 @@ optionalRollback AppConfig{..} ApiRequest{..} = do
|
|||||||
shouldRollback =
|
shouldRollback =
|
||||||
configDbTxAllowOverride && iPreferTransaction == Just Rollback
|
configDbTxAllowOverride && iPreferTransaction == Just Rollback
|
||||||
|
|
||||||
-- | Runs local(transaction scoped) GUCs for every request, plus the pre-request function
|
-- | Runs local (transaction scoped) GUCs for every request.
|
||||||
setPgLocals :: AppConfig -> KM.KeyMap JSON.Value -> Text ->
|
setPgLocals :: AppConfig -> KM.KeyMap JSON.Value -> Text ->
|
||||||
ApiRequest -> ByteString -> PgVersion -> DbHandler ()
|
ApiRequest -> ByteString -> PgVersion -> DbHandler ()
|
||||||
setPgLocals conf claims role req jsonDbS actualPgVersion = do
|
setPgLocals conf claims role req jsonDbS actualPgVersion = lift $
|
||||||
lift $ SQL.statement mempty $ SQL.dynamicallyParameterized
|
SQL.statement mempty $ SQL.dynamicallyParameterized
|
||||||
("select " <> intercalateSnippet ", " (searchPathSql : roleSql ++ claimsSql ++ [methodSql, pathSql] ++ headersSql ++ cookiesSql ++ appSettingsSql ++ specSql))
|
("select " <> intercalateSnippet ", " (searchPathSql : roleSql ++ claimsSql ++ [methodSql, pathSql] ++ headersSql ++ cookiesSql ++ appSettingsSql ++ specSql))
|
||||||
HD.noResult (configDbPreparedStatements conf)
|
HD.noResult (configDbPreparedStatements conf)
|
||||||
lift $ traverse_ SQL.sql preReqSql
|
|
||||||
where
|
where
|
||||||
methodSql = setConfigLocal mempty ("request.method", iMethod req)
|
methodSql = setConfigLocal mempty ("request.method", iMethod req)
|
||||||
pathSql = setConfigLocal mempty ("request.path", iPath req)
|
pathSql = setConfigLocal mempty ("request.path", iPath req)
|
||||||
@@ -257,7 +257,6 @@ setPgLocals conf claims role req jsonDbS actualPgVersion = do
|
|||||||
searchPathSql =
|
searchPathSql =
|
||||||
let schemas = pgFmtIdentList (iSchema req : configDbExtraSearchPath conf) in
|
let schemas = pgFmtIdentList (iSchema req : configDbExtraSearchPath conf) in
|
||||||
setConfigLocal mempty ("search_path", schemas)
|
setConfigLocal mempty ("search_path", schemas)
|
||||||
preReqSql = (\f -> "select " <> fromQi f <> "();") <$> configDbPreRequest conf
|
|
||||||
specSql = case iTarget req of
|
specSql = case iTarget req of
|
||||||
TargetProc{tpIsRootSpec=True} -> [setConfigLocal mempty ("request.spec", jsonDbS)]
|
TargetProc{tpIsRootSpec=True} -> [setConfigLocal mempty ("request.spec", jsonDbS)]
|
||||||
_ -> mempty
|
_ -> mempty
|
||||||
@@ -269,3 +268,9 @@ setPgLocals conf claims role req jsonDbS actualPgVersion = do
|
|||||||
toS $ formatScientific Fixed (if isInteger n then Just 0 else Nothing) n
|
toS $ formatScientific Fixed (if isInteger n then Just 0 else Nothing) n
|
||||||
unquoted (JSON.Bool b) = show b
|
unquoted (JSON.Bool b) = show b
|
||||||
unquoted v = T.decodeUtf8 . LBS.toStrict $ JSON.encode v
|
unquoted v = T.decodeUtf8 . LBS.toStrict $ JSON.encode v
|
||||||
|
|
||||||
|
-- | Runs the pre-request function.
|
||||||
|
runPreReq :: AppConfig -> DbHandler ()
|
||||||
|
runPreReq conf = lift $ traverse_ SQL.sql preReqSql
|
||||||
|
where
|
||||||
|
preReqSql = (\f -> "select " <> fromQi f <> "();") <$> configDbPreRequest conf
|
||||||
|
|||||||
Reference in New Issue
Block a user