refactor: add CallQuery type
Shortens requestToCallProcQuery, which should generate SQL in a more direct way.
This commit is contained in:
committed by
Steve Chavez
parent
c9a60373f6
commit
caaa34b5de
@@ -18,7 +18,7 @@ resource.
|
||||
module PostgREST.Request.DbRequestBuilder
|
||||
( readRequest
|
||||
, mutateRequest
|
||||
, returningCols
|
||||
, callRequest
|
||||
) where
|
||||
|
||||
import qualified Data.HashMap.Strict as M
|
||||
@@ -33,6 +33,9 @@ import Data.Tree (Tree (..))
|
||||
import PostgREST.DbStructure.Identifiers (FieldName,
|
||||
QualifiedIdentifier (..),
|
||||
Schema, TableName)
|
||||
import PostgREST.DbStructure.Proc (ProcDescription (..),
|
||||
ProcParam (..),
|
||||
procReturnsScalar)
|
||||
import PostgREST.DbStructure.Relationship (Cardinality (..),
|
||||
Junction (..),
|
||||
Relationship (..))
|
||||
@@ -343,6 +346,23 @@ mutateRequest schema tName apiRequest pkCols readReq = mapLeft ApiRequestError $
|
||||
onlyRoot = filter (not . ( "." `isInfixOf` ) . fst)
|
||||
body = pjRaw <$> iPayload apiRequest
|
||||
|
||||
callRequest :: ProcDescription -> ApiRequest -> ReadRequest -> CallRequest
|
||||
callRequest proc apiReq readReq = FunctionCall {
|
||||
funCQi = QualifiedIdentifier (pdSchema proc) (pdName proc)
|
||||
, funCParams = specifiedParams
|
||||
, funCArgs = pjRaw <$> iPayload apiReq
|
||||
, funCScalar = procReturnsScalar proc
|
||||
, funCMultipleCall = iPreferParameters apiReq == Just MultipleObjects
|
||||
, funCSingleParam = paramsAsSingleObject
|
||||
, funCReturning = returningCols readReq []
|
||||
}
|
||||
where
|
||||
paramsAsSingleObject = iPreferParameters apiReq == Just SingleObject
|
||||
specifiedParams =
|
||||
if paramsAsSingleObject
|
||||
then pdParams proc
|
||||
else filter (\x -> ppName x `S.member` iColumns apiReq) $ pdParams proc
|
||||
|
||||
returningCols :: ReadRequest -> [FieldName] -> [FieldName]
|
||||
returningCols rr@(Node _ forest) pkCols
|
||||
-- if * is part of the select, we must not add pk or fk columns manually -
|
||||
|
||||
@@ -6,6 +6,8 @@ module PostgREST.Request.Types
|
||||
, EmbedPath
|
||||
, Field
|
||||
, Filter(..)
|
||||
, CallQuery(..)
|
||||
, CallRequest
|
||||
, JoinCondition(..)
|
||||
, JsonOperand(..)
|
||||
, JsonOperation(..)
|
||||
@@ -38,6 +40,7 @@ import qualified GHC.Show (show)
|
||||
|
||||
import PostgREST.DbStructure.Identifiers (FieldName,
|
||||
QualifiedIdentifier)
|
||||
import PostgREST.DbStructure.Proc (ProcParam)
|
||||
import PostgREST.DbStructure.Relationship (Relationship)
|
||||
import PostgREST.RangeQuery (NonnegRange)
|
||||
import PostgREST.Request.Preferences (PreferResolution)
|
||||
@@ -47,6 +50,7 @@ import Protolude
|
||||
|
||||
type ReadRequest = Tree ReadNode
|
||||
type MutateRequest = MutateQuery
|
||||
type CallRequest = CallQuery
|
||||
|
||||
type ReadNode =
|
||||
(ReadQuery, (NodeName, Maybe Relationship, Maybe Alias, Maybe EmbedHint, Depth))
|
||||
@@ -121,6 +125,16 @@ data MutateQuery
|
||||
, returning :: [FieldName]
|
||||
}
|
||||
|
||||
data CallQuery = FunctionCall
|
||||
{ funCQi :: QualifiedIdentifier
|
||||
, funCParams :: [ProcParam]
|
||||
, funCArgs :: Maybe BL.ByteString
|
||||
, funCScalar :: Bool
|
||||
, funCMultipleCall :: Bool
|
||||
, funCSingleParam :: Bool
|
||||
, funCReturning :: [FieldName]
|
||||
}
|
||||
|
||||
-- | The select value in `/tbl?select=alias:field::cast`
|
||||
type SelectItem = (Field, Maybe Cast, Maybe Alias, Maybe EmbedHint)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user