packages feed

opaleye 0.7.3.0 → 0.7.4.0

raw patch · 7 files changed

+79/−22 lines, 7 filesPVP ok

version bump matches the API change (PVP)

API changes (from Hackage documentation)

+ Opaleye.Distinct: distinctOnByCorrect :: Default Unpackspec b b => (a -> b) -> Order a -> Select a -> Select a
+ Opaleye.Distinct: distinctOnCorrect :: Default Unpackspec b b => (a -> b) -> Select a -> Select a
+ Opaleye.Internal.Order: distinctOnByCorrect :: Unpackspec b b -> (a -> b) -> Order a -> (a, PrimQuery, Tag) -> (a, PrimQuery, Tag)
+ Opaleye.Internal.Order: distinctOnCorrect :: Unpackspec b b -> (a -> b) -> (a, PrimQuery, Tag) -> (a, PrimQuery, Tag)
+ Opaleye.Order: distinctOnByCorrect :: Default Unpackspec b b => (a -> b) -> Order a -> Select a -> Select a
+ Opaleye.Order: distinctOnCorrect :: Default Unpackspec b b => (a -> b) -> Select a -> Select a

Files

CHANGELOG.md view
@@ -1,3 +1,8 @@+## 0.7.4.0++* Added `distinctOnCorrect` and `distinctOnByCorrect` which will+  replace `distinctOn` and `distinctOnBy` in a future version.+ ## 0.7.3.0  * Added DefaultFromField SqlJson(b) instances for Strict/Lazy
Test/Test.hs view
@@ -432,33 +432,46 @@  testDistinctOn :: Test testDistinctOn = do-    it "distinct on (col1)" $ \conn -> do+    let distinctOn p q = \conn -> do+          let expected = L.nubBy (F.on (==) p) $ L.sortOn p table1data+          testH q (\r -> L.sort r `shouldBe` L.sort expected) conn++        distinctOnBy proj ord q = \conn -> do+          let expected = L.nubBy ((==) `F.on` proj) $ L.sortOn (proj &&& ord) triples+          testH q (\r -> L.sort r `shouldBe` L.sort expected) conn++    it "distinct on ()" $+        let p = const ()+            q = O.distinctOnCorrect p table1Q+        in distinctOn p q+    it "distinct on (col1)" $         let p = fst             q = O.distinctOn p table1Q-            expected = L.nubBy (F.on (==) p) $ L.sortOn p table1data-        testH q (\r -> L.sort r `shouldBe` L.sort expected) conn-    it "distinct on (col1, col2)" $ \conn -> do+        in distinctOn p q+    it "distinct on (col1, col2)" $         let p = fst &&& snd             q = O.distinctOn p table1Q-            expected = L.nubBy (F.on (==) p) $ L.sortOn p table1data-        testH q (\r -> L.sort r `shouldBe` L.sort expected) conn+        in distinctOn p q      let f1 (x,_,_) = x         f2 (_,y,_) = y         f3 (_,_,z) = z -    it "distinct on (col1) order by col2" $ \conn -> do+    it "distinct on () order by col1" $+        let proj = const ()+            ord  = f1+            q = O.distinctOnByCorrect proj (O.asc ord) $ O.values pgTriples+        in distinctOnBy proj ord q+    it "distinct on (col1) order by col2" $         let proj = f1             ord  = f2             q = O.distinctOnBy proj (O.asc ord) $ O.values pgTriples-            expected = L.nubBy ((==) `F.on` proj) $ L.sortOn (proj &&& ord) triples-        testH q (\r -> L.sort r `shouldBe` L.sort expected) conn-    it "distinct on (col1, col2) order by col3" $ \conn -> do+        in distinctOnBy proj ord q+    it "distinct on (col1, col2) order by col3" $         let proj = f1 &&& f2             ord  = f3             q = O.distinctOnBy proj (O.asc ord) $ O.values pgTriples-            expected = L.nubBy ((==) `F.on` proj) $ L.sortOn (proj &&& ord) triples-        testH q (\r -> L.sort r `shouldBe` L.sort expected) conn+        in distinctOnBy proj ord q     it "distinct on (col3) order by col2 desc" $ \conn -> do         let proj = f3             ord  = f2
opaleye.cabal view
@@ -1,6 +1,6 @@ name:            opaleye copyright:       Copyright (c) 2014-2018 Purely Agile Limited; 2019-2021 Tom Ellis-version:         0.7.3.0+version:         0.7.4.0 synopsis:        An SQL-generating DSL targeting PostgreSQL description:     An SQL-generating DSL targeting PostgreSQL.  Allows                  Postgres queries to be written within Haskell in a
src/Opaleye/Distinct.hs view
@@ -1,6 +1,8 @@ {-# LANGUAGE FlexibleContexts #-}  module Opaleye.Distinct (distinct,+                         distinctOnCorrect,+                         distinctOnByCorrect,                          Distinctspec,                          -- * Explicit versions                          distinctExplicit,@@ -12,6 +14,7 @@  import           Opaleye.Select (Select) import           Opaleye.Internal.Distinct+import           Opaleye.Order  import qualified Data.Profunctor.Product.Default as D 
src/Opaleye/Internal/Order.hs view
@@ -67,14 +67,26 @@  distinctOn :: U.Unpackspec b b -> (a -> b)            -> (a, PQ.PrimQuery, T.Tag) -> (a, PQ.PrimQuery, T.Tag)-distinctOn ups proj = distinctOnBy ups proj (Order $ const [])+distinctOn ups proj = distinctOnBy ups proj M.mempty +distinctOnCorrect :: U.Unpackspec b b -> (a -> b)+                  -> (a, PQ.PrimQuery, T.Tag) -> (a, PQ.PrimQuery, T.Tag)+distinctOnCorrect ups proj = distinctOnByCorrect ups proj M.mempty+ distinctOnBy :: U.Unpackspec b b -> (a -> b) -> Order a              -> (a, PQ.PrimQuery, T.Tag) -> (a, PQ.PrimQuery, T.Tag) distinctOnBy ups proj ord (cols, pq, t) = (cols, pqOut, t)     where pqOut = case U.collectPEs ups (proj cols) of             x:xs -> PQ.DistinctOnOrderBy (Just $ x NL.:| xs) (orderExprs cols ord) pq             []   -> pq++distinctOnByCorrect :: U.Unpackspec b b -> (a -> b) -> Order a+             -> (a, PQ.PrimQuery, T.Tag) -> (a, PQ.PrimQuery, T.Tag)+distinctOnByCorrect ups proj ord (cols, pq, t) = (cols, pqOut, t)+    where pqOut = case U.collectPEs ups (proj cols) of+            x:xs -> PQ.DistinctOnOrderBy (Just $ x NL.:| xs) oexprs pq+            []   -> PQ.Limit (PQ.LimitOp 1) (PQ.DistinctOnOrderBy Nothing oexprs pq)+          oexprs = orderExprs cols ord  -- | Order the results of a given query exactly, as determined by the given list -- of input columns. Note that this list does not have to contain an entry for
src/Opaleye/Join.hs view
@@ -36,7 +36,7 @@ -- have one then we'd love to hear about it. Please [open a new issue -- on the Opaleye -- project](http://github.com/tomjaguarpaw/haskell-opaleye/issues/new)--- and tell us about it.)+-- and tell us about it. -- -- - Left/right joins which really must not use @LATERAL@: use 'optionalRestrict' --
src/Opaleye/Order.hs view
@@ -16,13 +16,16 @@                      , limit                      , offset                      -- * Distinct on-                     , distinctOn-                     , distinctOnBy+                     , distinctOnCorrect+                     , distinctOnByCorrect                      -- * Exact ordering                      , O.exact                      -- * Other                      , PGOrd                      , SqlOrd+                     -- * Deprecated+                     , distinctOn+                     , distinctOnBy                      ) where  import qualified Data.Profunctor.Product.Default as D@@ -127,17 +130,23 @@ --   ordering is guaranteed. Multiple fields may be distinguished by projecting out --   tuples of 'Opaleye.Field.Field_'s. Use 'distinctOnBy' to control how the rows --   are chosen.-distinctOn :: D.Default U.Unpackspec b b => (a -> b) -> S.Select a -> S.Select a-distinctOn proj q = Q.productQueryArr (O.distinctOn D.def proj . Q.runSimpleQueryArr q)+distinctOnCorrect :: D.Default U.Unpackspec b b+                  => (a -> b)+                  -> S.Select a+                  -> S.Select a+distinctOnCorrect proj q = Q.productQueryArr (O.distinctOnCorrect D.def proj . Q.runSimpleQueryArr q)   -- | Keep the row from each set where the given function returns the same result. The --   row is chosen according to which comes first by the supplied ordering. However, no --   output ordering is guaranteed. Mutliple fields may be distinguished by projecting --   out tuples of 'Opaleye.Field.Field_'s.-distinctOnBy :: D.Default U.Unpackspec b b => (a -> b) -> O.Order a-             -> S.Select a -> S.Select a-distinctOnBy proj ord q = Q.productQueryArr (O.distinctOnBy D.def proj ord . Q.runSimpleQueryArr q)+distinctOnByCorrect :: D.Default U.Unpackspec b b+                    => (a -> b)+                    -> O.Order a+                    -> S.Select a+                    -> S.Select a+distinctOnByCorrect proj ord q = Q.productQueryArr (O.distinctOnByCorrect D.def proj ord . Q.runSimpleQueryArr q)   -- * Other@@ -163,3 +172,18 @@ instance SqlOrd T.SqlCitext instance SqlOrd T.SqlUuid instance SqlOrd a => SqlOrd (C.Nullable a)++-- | Use 'distinctOnCorrect' instead.  This version has a bug whereby+-- it returns the whole query if zero columns are chosen to be+-- distinct (it should just return the first row).  Will be deprecated+-- in version 0.8.+distinctOn :: D.Default U.Unpackspec b b => (a -> b) -> S.Select a -> S.Select a+distinctOn proj q = Q.productQueryArr (O.distinctOn D.def proj . Q.runSimpleQueryArr q)++-- | Use 'distinctOnByCorrect' instead.  This version has a bug+-- whereby it returns the whole query if zero columns are chosen to be+-- distinct (it should just return the first row).  Will be deprecated+-- in version 0.8.+distinctOnBy :: D.Default U.Unpackspec b b => (a -> b) -> O.Order a+             -> S.Select a -> S.Select a+distinctOnBy proj ord q = Q.productQueryArr (O.distinctOnBy D.def proj ord . Q.runSimpleQueryArr q)