Refactor rpc to not use count when returning scalar
This commit is contained in:
@@ -238,8 +238,12 @@ app dbStructure conf apiRequest =
|
|||||||
let p = V.head payload
|
let p = V.head payload
|
||||||
singular = contentType == CTSingularJSON
|
singular = contentType == CTSingularJSON
|
||||||
paramsAsSingleObject = iPreferSingleObjectParameter apiRequest
|
paramsAsSingleObject = iPreferSingleObjectParameter apiRequest
|
||||||
|
proc = M.lookup (qiName qi) allProcs
|
||||||
|
returnsScalar = case proc of
|
||||||
|
Just ProcDescription{pdReturnType = (Single (Scalar _))} -> True
|
||||||
|
_ -> False
|
||||||
row <- H.query () $
|
row <- H.query () $
|
||||||
callProc qi p q cq topLevelRange shouldCount singular
|
callProc qi p returnsScalar q cq topLevelRange shouldCount singular
|
||||||
paramsAsSingleObject (contentType == CTTextCSV)
|
paramsAsSingleObject (contentType == CTTextCSV)
|
||||||
let (tableTotal, queryTotal, body) =
|
let (tableTotal, queryTotal, body) =
|
||||||
fromMaybe (Just 0, 0, "[]") row
|
fromMaybe (Just 0, 0, "[]") row
|
||||||
@@ -257,7 +261,7 @@ app dbStructure conf apiRequest =
|
|||||||
uri Nothing = ("http", host, port, "/")
|
uri Nothing = ("http", host, port, "/")
|
||||||
uri (Just Proxy { proxyScheme = s, proxyHost = h, proxyPort = p, proxyPath = b }) = (s, h, p, b)
|
uri (Just Proxy { proxyScheme = s, proxyHost = h, proxyPort = p, proxyPath = b }) = (s, h, p, b)
|
||||||
uri' = uri proxy
|
uri' = uri proxy
|
||||||
encodeApi ti = encodeOpenAPI (M.elems $ dbProcs dbStructure) ti uri'
|
encodeApi ti = encodeOpenAPI (M.elems allProcs) ti uri'
|
||||||
body <- encodeApi . toTableInfo <$> H.query schema accessibleTables
|
body <- encodeApi . toTableInfo <$> H.query schema accessibleTables
|
||||||
return $ responseLBS status200 [toHeader CTOpenAPI] $ toS body
|
return $ responseLBS status200 [toHeader CTOpenAPI] $ toS body
|
||||||
|
|
||||||
@@ -276,6 +280,7 @@ app dbStructure conf apiRequest =
|
|||||||
filterCol :: Schema -> TableName -> Column -> Bool
|
filterCol :: Schema -> TableName -> Column -> Bool
|
||||||
filterCol sc tb Column{colTable=Table{tableSchema=s, tableName=t}} = s==sc && t==tb
|
filterCol sc tb Column{colTable=Table{tableSchema=s, tableName=t}} = s==sc && t==tb
|
||||||
allPrKeys = dbPrimaryKeys dbStructure
|
allPrKeys = dbPrimaryKeys dbStructure
|
||||||
|
allProcs = dbProcs dbStructure
|
||||||
allOrigins = ("Access-Control-Allow-Origin", "*") :: Header
|
allOrigins = ("Access-Control-Allow-Origin", "*") :: Header
|
||||||
shouldCount = iPreferCount apiRequest
|
shouldCount = iPreferCount apiRequest
|
||||||
schema = toS $ configSchema conf
|
schema = toS $ configSchema conf
|
||||||
@@ -287,7 +292,7 @@ app dbStructure conf apiRequest =
|
|||||||
status = rangeStatus lower upper (toInteger <$> tableTotal)
|
status = rangeStatus lower upper (toInteger <$> tableTotal)
|
||||||
in (status, contentRange)
|
in (status, contentRange)
|
||||||
|
|
||||||
readReq = readRequest (configMaxRows conf) (dbRelations dbStructure) (dbProcs dbStructure) apiRequest
|
readReq = readRequest (configMaxRows conf) (dbRelations dbStructure) allProcs apiRequest
|
||||||
fldNames = fieldNames <$> readReq
|
fldNames = fieldNames <$> readReq
|
||||||
readDbRequest = DbRead <$> readReq
|
readDbRequest = DbRead <$> readReq
|
||||||
mutateDbRequest = DbMutate <$> (mutateRequest apiRequest =<< fldNames)
|
mutateDbRequest = DbMutate <$> (mutateRequest apiRequest =<< fldNames)
|
||||||
|
|||||||
@@ -137,7 +137,9 @@ accessibleProcs =
|
|||||||
qi = QualifiedIdentifier schema name
|
qi = QualifiedIdentifier schema name
|
||||||
pgType = case typ of
|
pgType = case typ of
|
||||||
'c' -> Composite qi
|
'c' -> Composite qi
|
||||||
'p' -> Pseudo name
|
'p' -> if name == "record" -- Only pg pseudo type that is a row type is 'record'
|
||||||
|
then Composite qi
|
||||||
|
else Scalar qi
|
||||||
_ -> Scalar qi -- 'b'ase, 'd'omain, 'e'num, 'r'ange
|
_ -> Scalar qi -- 'b'ase, 'd'omain, 'e'num, 'r'ange
|
||||||
|
|
||||||
parseVolatility :: Char -> ProcVolatility
|
parseVolatility :: Char -> ProcVolatility
|
||||||
|
|||||||
@@ -144,26 +144,27 @@ createWriteStatement selectQuery mutateQuery wantSingle wantHdrs asCsv rep pKeys
|
|||||||
| otherwise = asJsonF
|
| otherwise = asJsonF
|
||||||
|
|
||||||
type ProcResults = (Maybe Int64, Int64, ByteString)
|
type ProcResults = (Maybe Int64, Int64, ByteString)
|
||||||
callProc :: QualifiedIdentifier -> JSON.Object -> SqlQuery -> SqlQuery -> NonnegRange ->
|
callProc :: QualifiedIdentifier -> JSON.Object -> Bool -> SqlQuery -> SqlQuery -> NonnegRange ->
|
||||||
Bool -> Bool -> Bool -> Bool -> H.Query () (Maybe ProcResults)
|
Bool -> Bool -> Bool -> Bool -> H.Query () (Maybe ProcResults)
|
||||||
callProc qi params selectQuery countQuery _ countTotal isSingle paramsAsJson asCsv =
|
callProc qi params returnsScalar selectQuery countQuery _ countTotal isSingle paramsAsJson asCsv =
|
||||||
unicodeStatement sql HE.unit decodeProc True
|
unicodeStatement sql HE.unit decodeProc True
|
||||||
where
|
where
|
||||||
sql = [qc|
|
sql =
|
||||||
WITH {sourceCTEName} AS ({_callSql})
|
if returnsScalar then [qc|
|
||||||
SELECT
|
WITH {sourceCTEName} AS ({_callSql})
|
||||||
{countResultF} AS total_result_set,
|
SELECT
|
||||||
pg_catalog.count(_postgrest_t) AS page_total,
|
{countResultF} AS total_result_set,
|
||||||
case
|
1 AS page_total,
|
||||||
when pg_catalog.count(*) > 1 then
|
(row_to_json(_postgrest_t)->{_procName})::character varying as body
|
||||||
{bodyF}
|
FROM ({selectQuery}) _postgrest_t;|]
|
||||||
else
|
else [qc|
|
||||||
coalesce(((array_agg(row_to_json(_postgrest_t)))[1]->{_procName})::character varying, {bodyF})
|
WITH {sourceCTEName} AS ({_callSql})
|
||||||
|
SELECT
|
||||||
|
{countResultF} AS total_result_set,
|
||||||
|
pg_catalog.count(_postgrest_t) AS page_total,
|
||||||
|
{bodyF} as body
|
||||||
|
FROM ({selectQuery}) _postgrest_t;|]
|
||||||
|
|
||||||
end as body
|
|
||||||
FROM ({selectQuery}) _postgrest_t;
|
|
||||||
|]
|
|
||||||
-- FROM (select * from {sourceCTEName} {limitF range}) t;
|
|
||||||
countResultF = if countTotal then "("<>countQuery<>")" else "null::bigint" :: Text
|
countResultF = if countTotal then "("<>countQuery<>")" else "null::bigint" :: Text
|
||||||
_args = if paramsAsJson
|
_args = if paramsAsJson
|
||||||
then insertableValueWithType "json" $ JSON.Object params
|
then insertableValueWithType "json" $ JSON.Object params
|
||||||
|
|||||||
@@ -38,7 +38,7 @@ data PgArg = PgArg {
|
|||||||
, pgaReq :: Bool
|
, pgaReq :: Bool
|
||||||
} deriving (Show, Eq)
|
} deriving (Show, Eq)
|
||||||
|
|
||||||
data PgType = Scalar QualifiedIdentifier | Composite QualifiedIdentifier | Pseudo Text deriving (Eq, Show)
|
data PgType = Scalar QualifiedIdentifier | Composite QualifiedIdentifier deriving (Eq, Show)
|
||||||
|
|
||||||
data RetType = Single PgType | SetOf PgType deriving (Eq, Show)
|
data RetType = Single PgType | SetOf PgType deriving (Eq, Show)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user