refactor: use a single function to get the page_total count

This commit is contained in:
Laurence Isla
2026-01-28 18:43:44 -05:00
parent 73a465501e
commit d031bb2df5
2 changed files with 11 additions and 6 deletions
+8
View File
@@ -23,6 +23,7 @@ module PostgREST.Query.SqlFragment
, locationF , locationF
, noLocationF , noLocationF
, orderF , orderF
, pageCountSelectF
, pgFmtColumn , pgFmtColumn
, pgFmtFilter , pgFmtFilter
, pgFmtIdent , pgFmtIdent
@@ -96,6 +97,7 @@ import PostgREST.SchemaCache.Routine (MediaHandler (..),
Routine (..), Routine (..),
funcReturnsScalar, funcReturnsScalar,
funcReturnsSetOfScalar, funcReturnsSetOfScalar,
funcReturnsSingle,
funcReturnsSingleComposite) funcReturnsSingleComposite)
import Protolude hiding (Sum, cast) import Protolude hiding (Sum, cast)
@@ -495,6 +497,12 @@ countF countQuery shouldCount =
mempty mempty
, "null::bigint") , "null::bigint")
pageCountSelectF :: Maybe Routine -> SQL.Snippet
pageCountSelectF rout =
if maybe False funcReturnsSingle rout
then "1"
else "pg_catalog.count(_postgrest_t)"
returningF :: QualifiedIdentifier -> [FieldName] -> SQL.Snippet returningF :: QualifiedIdentifier -> [FieldName] -> SQL.Snippet
returningF qi returnings = returningF qi returnings =
if null returnings if null returnings
+3 -6
View File
@@ -20,8 +20,7 @@ import PostgREST.Plan.MutatePlan as MTPlan
import PostgREST.Plan.ReadPlan import PostgREST.Plan.ReadPlan
import PostgREST.Query.QueryBuilder import PostgREST.Query.QueryBuilder
import PostgREST.Query.SqlFragment import PostgREST.Query.SqlFragment
import PostgREST.SchemaCache.Routine (MediaHandler (..), Routine, import PostgREST.SchemaCache.Routine (MediaHandler (..), Routine)
funcReturnsSingle)
import Protolude import Protolude
@@ -72,7 +71,7 @@ mainRead rPlan countQuery pCount maxRows mt handler = mtSnippet mt snippet
countCTEF <> " " <> countCTEF <> " " <>
"SELECT " <> "SELECT " <>
countResultF <> " AS total_result_set, " <> countResultF <> " AS total_result_set, " <>
"pg_catalog.count(_postgrest_t) AS page_total, " <> pageCountSelectF Nothing <> " AS page_total, " <>
handlerF Nothing handler <> " AS body, " <> handlerF Nothing handler <> " AS body, " <>
responseHeadersF <> " AS response_headers, " <> responseHeadersF <> " AS response_headers, " <>
responseStatusF <> " AS response_status, " <> responseStatusF <> " AS response_status, " <>
@@ -97,9 +96,7 @@ mainCall rout cPlan rPlan pCount mt handler = mtSnippet mt snippet
countCTEF <> countCTEF <>
"SELECT " <> "SELECT " <>
countResultF <> " AS total_result_set, " <> countResultF <> " AS total_result_set, " <>
(if funcReturnsSingle rout pageCountSelectF (Just rout) <> " AS page_total, " <>
then "1"
else "pg_catalog.count(_postgrest_t)") <> " AS page_total, " <>
handlerF (Just rout) handler <> " AS body, " <> handlerF (Just rout) handler <> " AS body, " <>
responseHeadersF <> " AS response_headers, " <> responseHeadersF <> " AS response_headers, " <>
responseStatusF <> " AS response_status, " <> responseStatusF <> " AS response_status, " <>