From 7b3cb37131561e4d66cf1f40d773321cd4e3dd66 Mon Sep 17 00:00:00 2001 From: "brady.ouren" Date: Fri, 25 Oct 2019 11:58:54 -0700 Subject: [PATCH] move function and bump version - moves associateJoin to Database.Esqueleto - relaxes bounds on containers dep - --- changelog.md | 6 +++++ esqueleto.cabal | 4 +-- src/Database/Esqueleto.hs | 28 ++++++++++++++++++++- src/Database/Esqueleto/Internal/Internal.hs | 25 ------------------ src/Database/Esqueleto/Internal/Language.hs | 2 +- 5 files changed, 36 insertions(+), 29 deletions(-) diff --git a/changelog.md b/changelog.md index 0dcf46d..4556731 100644 --- a/changelog.md +++ b/changelog.md @@ -1,3 +1,9 @@ +3.1.2 +======== + +- @tippenein + - [#149](https://github.com/bitemyapp/esqueleto/pull/157): Added `associateJoin` query helpers. + 3.1.1 ======== diff --git a/esqueleto.cabal b/esqueleto.cabal index ad008f6..13211a6 100644 --- a/esqueleto.cabal +++ b/esqueleto.cabal @@ -1,7 +1,7 @@ cabal-version: 1.12 name: esqueleto -version: 3.1.1 +version: 3.1.2 synopsis: Type-safe EDSL for SQL queries on persistent backends. description: @esqueleto@ is a bare bones, type-safe EDSL for SQL queries that works with unmodified @persistent@ SQL backends. Its language closely resembles SQL, so you don't have to learn new concepts, just new syntax, and it's fairly easy to predict the generated SQL and optimize it for your backend. Most kinds of errors committed when writing SQL are caught as compile-time errors---although it is possible to write type-checked @esqueleto@ queries that fail at runtime. . @@ -47,7 +47,7 @@ library , aeson >=1.0 , blaze-html , bytestring - , containers >=0.6 + , containers , conduit >=1.3 , monad-logger , persistent >=2.10.0 && <2.11 diff --git a/src/Database/Esqueleto.hs b/src/Database/Esqueleto.hs index d082191..9dce2ff 100644 --- a/src/Database/Esqueleto.hs +++ b/src/Database/Esqueleto.hs @@ -1,4 +1,4 @@ -{-# LANGUAGE FlexibleContexts, FlexibleInstances, GADTs #-} +{-# LANGUAGE FlexibleContexts, FlexibleInstances, GADTs, RankNTypes #-} -- | The @esqueleto@ EDSL (embedded domain specific language). -- This module replaces @Database.Persist@, so instead of -- importing that module you should just import this one: @@ -100,6 +100,7 @@ module Database.Esqueleto -- * Helpers , valkey , valJ + , associateJoin -- * Re-exports -- $reexports @@ -110,6 +111,7 @@ module Database.Esqueleto import Control.Monad.IO.Class (MonadIO) import Control.Monad.Trans.Reader (ReaderT) import Data.Int (Int64) +import qualified Data.Map.Strict as Map import Database.Esqueleto.Internal.Language import Database.Esqueleto.Internal.Sql import Database.Esqueleto.Internal.PersistentImport @@ -439,3 +441,27 @@ deleteKey :: ( PersistStore backend , PersistEntity val ) => Key val -> ReaderT backend m () deleteKey = Database.Persist.delete + +-- | Avoid N+1 queries and join entities into a map structure +-- @ +-- getFoosAndNestedBarsFromParent :: ParentId -> (Map (Key Foo) (Foo, [Maybe (Entity Bar)])) +-- getFoosAndNestedBarsFromParent parentId = 'fmap' associateJoin $ 'select' $ +-- 'from' $ \\(foo `'LeftOuterJoin`` bar) -> do +-- 'on' (bar '?.' BarFooId '==.' foo '^.' FooId) +-- 'where_' (foo '^.' FooParentId '==.' 'val' parentId) +-- 'pure' (foo, bar) +-- @ +-- /Since: 3.1.2/ +associateJoin + :: forall e1 e0 + . Ord (Key e0) + => [(Entity e0, e1)] + -> Map.Map (Key e0) (e0, [e1]) +associateJoin = foldr f start + where + start = Map.empty + f (one, many) = + Map.insertWith + (\(oneOld, manyOld) (_, manyNew) -> (oneOld, manyNew ++ manyOld )) + (entityKey one) + (entityVal one, [many]) diff --git a/src/Database/Esqueleto/Internal/Internal.hs b/src/Database/Esqueleto/Internal/Internal.hs index 34fcab6..d6d165b 100644 --- a/src/Database/Esqueleto/Internal/Internal.hs +++ b/src/Database/Esqueleto/Internal/Internal.hs @@ -50,7 +50,6 @@ import qualified Data.ByteString as B import qualified Data.Conduit as C import qualified Data.Conduit.List as CL import qualified Data.HashSet as HS -import qualified Data.Map.Strict as Map import qualified Data.Text as T import qualified Data.Text.Lazy as TL import qualified Data.Text.Lazy.Builder as TLB @@ -2885,27 +2884,3 @@ insertSelect = void . insertSelectCount insertSelectCount :: (MonadIO m, PersistEntity a) => SqlQuery (SqlExpr (Insertion a)) -> SqlWriteT m Int64 insertSelectCount = rawEsqueleto INSERT_INTO . fmap EInsertFinal - --- | Avoid N+1 queries and join entities into a map structure --- @ --- getFoosAndNestedBarsFromParent :: ParentId -> (Map (Key Foo) (Foo, [Entity Bar])) --- getFoosAndNestedBarsFromParent parentId = 'fmap' associateJoin $ 'select' $ --- 'from' $ \\(foo `'LeftOuterJoin`` bar) -> do --- 'on' (bar '^.' BarFooId '==.' foo '^.' FooId) --- 'where_' (foo '^.' FooParentId '==.' 'val' parentId) --- 'pure' (foo, bar) --- @ - -associateJoin - :: forall e1 e0 - . Ord (Key e0) - => [(Entity e0, e1)] - -> Map.Map (Key e0) (e0, [e1]) -associateJoin = foldr f start - where - start = Map.empty - f (one, many) = - Map.insertWith - (\(oneOld, manyOld) (_, manyNew) -> (oneOld, manyNew ++ manyOld )) - (entityKey one) - (entityVal one, [many]) diff --git a/src/Database/Esqueleto/Internal/Language.hs b/src/Database/Esqueleto/Internal/Language.hs index 254362c..dfa4985 100644 --- a/src/Database/Esqueleto/Internal/Language.hs +++ b/src/Database/Esqueleto/Internal/Language.hs @@ -55,7 +55,7 @@ module Database.Esqueleto.Internal.Language , subList_select, valList, justList , in_, notIn, exists, notExists , set, (=.), (+=.), (-=.), (*=.), (/=.) - , case_, toBaseId, (<#), (<&>), associateJoin + , case_, toBaseId, (<#), (<&>) ) where import Database.Esqueleto.Internal.PersistentImport