summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorarybczak <>2020-11-16 12:47:00 (GMT)
committerhdiff <hdiff@hdiff.luite.com>2020-11-16 12:47:00 (GMT)
commitf4df3caaa0231b126063b255b2c5219b0c4f3f30 (patch)
treeaf816dc6c8db5a4926acfddc71d737fdcad7da44
parentcf0ecd1ffe40909cce7d6c5a9d0bc288a4eb9b78 (diff)
version 1.10.3.0HEAD1.10.3.0master
-rwxr-xr-xCHANGELOG.md3
-rw-r--r--hpqtypes-extras.cabal4
-rw-r--r--src/Database/PostgreSQL/PQTypes/SQL/Builder.hs29
3 files changed, 27 insertions, 9 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md
index f9638d1..6705657 100755
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,3 +1,6 @@
+# hpqtypes-extra-1.10.3.0 (2020-11-16)
+* Include LIMIT clause in UNION subqueries of the select
+
# hpqtypes-extras-1.10.2.1 (2020-05-05)
* Add support for GHC 8.10
diff --git a/hpqtypes-extras.cabal b/hpqtypes-extras.cabal
index b8f2fcd..c6076ee 100644
--- a/hpqtypes-extras.cabal
+++ b/hpqtypes-extras.cabal
@@ -1,6 +1,6 @@
cabal-version: 1.18
name: hpqtypes-extras
-version: 1.10.2.1
+version: 1.10.3.0
synopsis: Extra utilities for hpqtypes library
description: The following extras for hpqtypes library:
.
@@ -64,7 +64,7 @@ library
, monad-control >= 1.0 && < 1.1
, semigroups >= 0.16 && < 0.20
, text-show >= 3.7 && < 3.9
- , log-base >= 0.7 && < 0.9
+ , log-base >= 0.7 && < 0.10
, safe >= 0.3 && < 0.4
default-language: Haskell2010
diff --git a/src/Database/PostgreSQL/PQTypes/SQL/Builder.hs b/src/Database/PostgreSQL/PQTypes/SQL/Builder.hs
index c60793e..dea2780 100644
--- a/src/Database/PostgreSQL/PQTypes/SQL/Builder.hs
+++ b/src/Database/PostgreSQL/PQTypes/SQL/Builder.hs
@@ -431,21 +431,36 @@ instance Sqlable SqlSelect where
toSQLCommand cmd = smconcat
[ emitClausesSepComma "WITH" $
map (\(name,command) -> name <+> "AS" <+> parenthesize command) (sqlSelectWith cmd)
- , "SELECT" <+> (if sqlSelectDistinct cmd then "DISTINCT" else mempty)
- , sqlConcatComma (sqlSelectResult cmd)
- , emitClause "FROM" (sqlSelectFrom cmd)
- , emitClausesSep "WHERE" "AND" (map toSQLCommand $ sqlSelectWhere cmd)
- , emitClausesSep "UNION" "UNION" (sqlSelectUnion cmd)
+ , if hasUnion
+ then emitClausesSep "" "UNION" (mainSelectClause : sqlSelectUnion cmd)
+ else mainSelectClause
, emitClausesSepComma "GROUP BY" (sqlSelectGroupBy cmd)
, emitClausesSep "HAVING" "AND" (sqlSelectHaving cmd)
- , emitClausesSepComma "ORDER BY" (sqlSelectOrderBy cmd)
+ , orderByClause
, if sqlSelectOffset cmd > 0
then unsafeSQL ("OFFSET " ++ show (sqlSelectOffset cmd))
else ""
, if sqlSelectLimit cmd >= 0
- then unsafeSQL ("LIMIT " ++ show (sqlSelectLimit cmd))
+ then limitClause
else ""
]
+ where
+ mainSelectClause = smconcat
+ [ "SELECT" <+> (if sqlSelectDistinct cmd then "DISTINCT" else mempty)
+ , sqlConcatComma (sqlSelectResult cmd)
+ , emitClause "FROM" (sqlSelectFrom cmd)
+ , emitClausesSep "WHERE" "AND" (map toSQLCommand $ sqlSelectWhere cmd)
+ -- If there's a union, the result is sorted and has a limit, applying
+ -- the order and limit to the main subquery won't reduce the overall
+ -- query result, but might reduce its processing time.
+ , if hasUnion && not (null $ sqlSelectOrderBy cmd) && sqlSelectLimit cmd >= 0
+ then smconcat [orderByClause, limitClause]
+ else ""
+ ]
+
+ hasUnion = not . null $ sqlSelectUnion cmd
+ orderByClause = emitClausesSepComma "ORDER BY" $ sqlSelectOrderBy cmd
+ limitClause = unsafeSQL $ "LIMIT" <+> show (sqlSelectLimit cmd)
instance Sqlable SqlInsert where
toSQLCommand cmd =