Skip to content

Commit a482524

Browse files
committed
refactor: use a single function to get the page_total count
1 parent eed018f commit a482524

File tree

2 files changed

+11
-6
lines changed

2 files changed

+11
-6
lines changed

src/PostgREST/Query/SqlFragment.hs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ module PostgREST.Query.SqlFragment
2323
, locationF
2424
, noLocationF
2525
, orderF
26+
, pageCountSelectF
2627
, pgFmtColumn
2728
, pgFmtFilter
2829
, pgFmtIdent
@@ -96,6 +97,7 @@ import PostgREST.SchemaCache.Routine (MediaHandler (..),
9697
Routine (..),
9798
funcReturnsScalar,
9899
funcReturnsSetOfScalar,
100+
funcReturnsSingle,
99101
funcReturnsSingleComposite)
100102

101103
import Protolude hiding (Sum, cast)
@@ -495,6 +497,12 @@ countF countQuery shouldCount =
495497
mempty
496498
, "null::bigint")
497499

500+
pageCountSelectF :: Maybe Routine -> SQL.Snippet
501+
pageCountSelectF rout =
502+
if maybe False funcReturnsSingle rout
503+
then "1"
504+
else "pg_catalog.count(_postgrest_t)"
505+
498506
returningF :: QualifiedIdentifier -> [FieldName] -> SQL.Snippet
499507
returningF qi returnings =
500508
if null returnings

src/PostgREST/Query/Statements.hs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,7 @@ import PostgREST.Plan.MutatePlan as MTPlan
2020
import PostgREST.Plan.ReadPlan
2121
import PostgREST.Query.QueryBuilder
2222
import PostgREST.Query.SqlFragment
23-
import PostgREST.SchemaCache.Routine (MediaHandler (..), Routine,
24-
funcReturnsSingle)
23+
import PostgREST.SchemaCache.Routine (MediaHandler (..), Routine)
2524

2625
import Protolude
2726

@@ -72,7 +71,7 @@ mainRead rPlan countQuery pCount maxRows mt handler = mtSnippet mt snippet
7271
countCTEF <> " " <>
7372
"SELECT " <>
7473
countResultF <> " AS total_result_set, " <>
75-
"pg_catalog.count(_postgrest_t) AS page_total, " <>
74+
pageCountSelectF Nothing <> " AS page_total, " <>
7675
handlerF Nothing handler <> " AS body, " <>
7776
responseHeadersF <> " AS response_headers, " <>
7877
responseStatusF <> " AS response_status, " <>
@@ -97,9 +96,7 @@ mainCall rout cPlan rPlan pCount mt handler = mtSnippet mt snippet
9796
countCTEF <>
9897
"SELECT " <>
9998
countResultF <> " AS total_result_set, " <>
100-
(if funcReturnsSingle rout
101-
then "1"
102-
else "pg_catalog.count(_postgrest_t)") <> " AS page_total, " <>
99+
pageCountSelectF (Just rout) <> " AS page_total, " <>
103100
handlerF (Just rout) handler <> " AS body, " <>
104101
responseHeadersF <> " AS response_headers, " <>
105102
responseStatusF <> " AS response_status, " <>

0 commit comments

Comments
 (0)