Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion effectful-opaleye/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ and this project adheres to [Haskell Package Versioning Policy](https://pvp.hask

## [Unreleased]

### Added

- Ability to keep a running tally of the SQL operations that are performed by
the `Opaleye` effect in #4.

## [0.1.0.1] - 04.08.2025

### Changed
Expand All @@ -23,5 +28,5 @@ and this project adheres to [Haskell Package Versioning Policy](https://pvp.hask
- CI that builds and tests the packages for each version of GHC in the `tested-with` field.

[unreleased]: https://github.com/fpringle/effectful-postgresql/compare/v0.1.0.1...HEAD
[0.1.0.1]: https://github.com/fpringle/effectful-postgresql/releases/tag/v0.1.0.1
[0.1.0.1]: https://github.com/fpringle/effectful-postgresql/compare/v0.1.0.0...v0.1.0.1
[0.1.0.0]: https://github.com/fpringle/effectful-postgresql/releases/tag/v0.1.0.0
5 changes: 5 additions & 0 deletions effectful-opaleye/effectful-opaleye.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ common deps
, product-profunctors >= 0.9 && < 0.12
, effectful-postgresql >= 0.1 && < 0.2
, postgresql-simple >= 0.7 && < 0.8
, text >= 2.0 && < 2.2
, containers >= 0.6 && < 0.8
, pretty >= 1.1.1.0 && < 1.2

common extensions
default-extensions:
Expand All @@ -60,6 +63,8 @@ library
, extensions
exposed-modules:
Effectful.Opaleye
Effectful.Opaleye.Effect
Effectful.Opaleye.Count
-- other-extensions:
hs-source-dirs: src
default-language: Haskell2010
53 changes: 30 additions & 23 deletions effectful-opaleye/src/Effectful/Opaleye.hs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
{-# LANGUAGE TemplateHaskell #-}

module Effectful.Opaleye
( -- * Effect
Opaleye (..)
Expand All @@ -26,39 +24,28 @@ module Effectful.Opaleye

-- * Interpreters
, runOpaleyeWithConnection
, runOpaleyeWithConnectionCounting
, runOpaleyeConnection
, runOpaleyeConnectionCounting

-- * Counting SQL operations
, SQLOperationCounts (..)
, withCounts
, printCounts
)
where

import Data.Profunctor.Product.Default
import qualified Database.PostgreSQL.Simple as PSQL
import Effectful
import Effectful.Dispatch.Dynamic
import Effectful.Opaleye.Count
import Effectful.Opaleye.Effect
import qualified Effectful.PostgreSQL.Connection as Conn
import Effectful.TH
import Effectful.State.Static.Shared
import qualified Opaleye as O
import qualified Opaleye.Internal.Inferrable as O

-- | A dynamic effect to perform @opaleye@ operations.
data Opaleye :: Effect where
-- | Lifted 'O.RunSelectExplicit'.
RunSelectExplicit :: O.FromFields fields haskells -> O.Select fields -> Opaleye m [haskells]
-- | Lifted 'O.RunSelectFoldExplicit'.
RunSelectFoldExplicit ::
O.FromFields fields haskells ->
O.Select fields ->
b ->
(b -> haskells -> m b) ->
Opaleye m b
-- | Lifted 'O.RunInsert'.
RunInsert :: O.Insert haskells -> Opaleye m haskells
-- | Lifted 'O.RunDelete'.
RunDelete :: O.Delete haskells -> Opaleye m haskells
-- | Lifted 'O.RunUpdate'.
RunUpdate :: O.Update haskells -> Opaleye m haskells

makeEffect ''Opaleye

-- | Lifted 'O.runSelect'.
runSelect ::
(HasCallStack, Opaleye :> es, Default O.FromFields fields haskells) =>
Expand Down Expand Up @@ -115,3 +102,23 @@ runOpaleyeConnection conn = interpret $ \env -> \case
RunInsert sel -> liftIO $ O.runInsert conn sel
RunDelete sel -> liftIO $ O.runDelete conn sel
RunUpdate sel -> liftIO $ O.runUpdate conn sel

{- | Same as 'runOpaleyeWithConnection', but we track the number of SQL operations that
we perform.
-}
runOpaleyeWithConnectionCounting ::
forall a es.
(HasCallStack, State SQLOperationCounts :> es, Conn.WithConnection :> es, IOE :> es) =>
Eff (Opaleye : es) a ->
Eff es a
runOpaleyeWithConnectionCounting = runOpaleyeWithConnection . opaleyeAddCounting

{- | Same as 'runOpaleyeConnection', but we track the number of SQL operations that
we perform.
-}
runOpaleyeConnectionCounting ::
(HasCallStack, State SQLOperationCounts :> es, IOE :> es) =>
PSQL.Connection ->
Eff (Opaleye : es) a ->
Eff es a
runOpaleyeConnectionCounting conn = runOpaleyeConnection conn . opaleyeAddCounting
Loading
Loading