opaleye-trans (empty) → 0.1.0
raw patch · 4 files changed
+194/−0 lines, 4 filesdep +basedep +monad-controldep +mtlsetup-changed
Dependencies added: base, monad-control, mtl, opaleye, postgresql-simple, product-profunctors, transformers-base
Files
- LICENSE +30/−0
- Setup.hs +2/−0
- opaleye-trans.cabal +31/−0
- src/Opaleye/Trans.hs +131/−0
+ LICENSE view
@@ -0,0 +1,30 @@+Copyright (c) 2015, Matthew Wraith++All rights reserved.++Redistribution and use in source and binary forms, with or without+modification, are permitted provided that the following conditions are met:++ * Redistributions of source code must retain the above copyright+ notice, this list of conditions and the following disclaimer.++ * Redistributions in binary form must reproduce the above+ copyright notice, this list of conditions and the following+ disclaimer in the documentation and/or other materials provided+ with the distribution.++ * Neither the name of Matthew Wraith nor the names of other+ contributors may be used to endorse or promote products derived+ from this software without specific prior written permission.++THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS+"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT+LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR+A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT+OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,+SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT+LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,+DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY+THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE+OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ opaleye-trans.cabal view
@@ -0,0 +1,31 @@+name: opaleye-trans+version: 0.1.0+synopsis: A monad transformer for Opaleye+description: A monad transformer for Opaleye+homepage: https://github.com/tomjaguarpaw/haskell-opaleye+license: BSD3+license-file: LICENSE+author: Matthew Wraith+maintainer: wraithm@gmail.com+copyright: (c) 2015 Matthew Wraith+category: Database+build-type: Simple+cabal-version: >=1.10++source-repository head+ type: git+ location: https://github.com/WraithM/opaleye-trans++library+ exposed-modules: Opaleye.Trans+ -- other-modules:+ build-depends:+ base >=4.8 && <4.9,+ mtl >=2.2 && <2.3,+ transformers-base >=0.4 && <0.5,+ monad-control >=1.0 && <1.1,+ opaleye >=0.4 && <0.5,+ postgresql-simple >=0.4 && <0.5,+ product-profunctors >=0.6 && <0.7+ hs-source-dirs: src+ default-language: Haskell2010
+ src/Opaleye/Trans.hs view
@@ -0,0 +1,131 @@+{-# LANGUAGE FlexibleContexts #-}+{-# LANGUAGE FlexibleInstances #-}+{-# LANGUAGE GeneralizedNewtypeDeriving #-}+{-# LANGUAGE MultiParamTypeClasses #-}+{-# LANGUAGE TypeFamilies #-}+{-# LANGUAGE UndecidableInstances #-}++module Opaleye.Trans+ ( OpaleyeT (..)+ , runOpaleyeT++ , -- * Transactions+ transaction++ , -- * Queries+ query+ , queryFirst++ , -- * Inserts+ insert+ , insertMany+ , insertReturning+ , insertReturningFirst+ , insertManyReturning++ , -- * Utilities+ withConn+ , withConnIO++ , -- * Reexports+ liftBase+ , MonadBase+ , ask+ , MonadBaseControl+ , Int64+ ) where++import Control.Monad.Base (MonadBase, liftBase)+import Control.Monad.Reader (MonadReader, ReaderT (..),+ ask)+import Control.Monad.Trans (MonadTrans (..))+import Control.Monad.Trans.Control++import Data.Maybe (listToMaybe)+import Data.Profunctor.Product.Default (Default)++import Database.PostgreSQL.Simple (Connection, withTransaction)+import qualified Database.PostgreSQL.Simple as PSQL++import GHC.Int++import Opaleye+++newtype OpaleyeT m a = OpaleyeT { unOpaleyeT :: ReaderT Connection m a }+ deriving (Functor, Applicative, Monad, MonadTrans, MonadReader Connection)+++instance MonadBase b m => MonadBase b (OpaleyeT m) where+ liftBase = lift . liftBase+++-- | TODO Handle exceptions+runOpaleyeT :: PSQL.Connection -> OpaleyeT m a -> m a+runOpaleyeT c = flip runReaderT c . unOpaleyeT+++withConn :: Monad m => (Connection -> m a) -> OpaleyeT m a+withConn f = do+ conn <- ask+ lift (f conn)+++withConnIO :: MonadBase IO m => (Connection -> IO a) -> OpaleyeT m a+withConnIO f = do+ conn <- ask+ liftBase $ f conn+++liftWithTransaction :: MonadBaseControl IO m => Connection -> m a -> m a+liftWithTransaction conn f =+ control $ \io -> withTransaction conn (io f)+++transaction :: MonadBaseControl IO m => OpaleyeT m a -> OpaleyeT m a+transaction t = withConn $ \conn ->+ liftWithTransaction conn (runOpaleyeT conn t)+++query :: (MonadBase IO m, Default QueryRunner a b) => Query a -> OpaleyeT m [b]+query q = withConnIO (`runQuery` q)+++queryFirst :: (MonadBase IO m, Default QueryRunner a b) => Query a -> OpaleyeT m (Maybe b)+queryFirst q = listToMaybe <$> query q+++insert :: MonadBase IO m => Table w r -> w -> OpaleyeT m Int64+insert t w = withConnIO (\c -> runInsert c t w)+++insertMany :: MonadBase IO m => Table w r -> [w] -> OpaleyeT m Int64+insertMany t ws = withConnIO (\c -> runInsertMany c t ws)+++insertReturning+ :: (MonadBase IO m, Default QueryRunner a b)+ => Table w r+ -> (r -> a)+ -> w+ -> OpaleyeT m [b]+insertReturning t ret w = withConnIO (\c -> runInsertReturning c t w ret)+++insertReturningFirst+ :: (MonadBase IO m, Default QueryRunner a b)+ => Table w r+ -> (r -> a)+ -> w+ -> OpaleyeT m (Maybe b)+insertReturningFirst t ret w = listToMaybe <$> insertReturning t ret w+++insertManyReturning+ :: (MonadBaseControl IO m, Default QueryRunner a b)+ => Table w r+ -> (r -> a)+ -> [w]+ -> OpaleyeT m [[b]]+insertManyReturning t ret ws =+ transaction (mapM (insertReturning t ret) ws)