opaleye-classy (empty) → 0.1.0.0
raw patch · 5 files changed
+195/−0 lines, 5 filesdep +basedep +bytestringdep +lenssetup-changed
Dependencies added: base, bytestring, lens, mtl, opaleye, postgresql-simple, product-profunctors, transformers
Files
- LICENSE +22/−0
- Opaleye/Classy.hs +122/−0
- README.md +9/−0
- Setup.hs +2/−0
- opaleye-classy.cabal +40/−0
+ LICENSE view
@@ -0,0 +1,22 @@+The MIT License (MIT)++Copyright (c) 2015 Ben Kolera++Permission is hereby granted, free of charge, to any person obtaining a copy+of this software and associated documentation files (the "Software"), to deal+in the Software without restriction, including without limitation the rights+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell+copies of the Software, and to permit persons to whom the Software is+furnished to do so, subject to the following conditions:++The above copyright notice and this permission notice shall be included in all+copies or substantial portions of the Software.++THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE+SOFTWARE.+
+ Opaleye/Classy.hs view
@@ -0,0 +1,122 @@+{-# LANGUAGE ConstraintKinds #-}+{-# LANGUAGE FlexibleContexts #-}+{-# LANGUAGE MultiParamTypeClasses #-}+{-# LANGUAGE RankNTypes #-}+{-# LANGUAGE TemplateHaskell #-}+module Opaleye.Classy+ ( DbEnv(DbEnv,_dbEnvConn)+ , dbEnvConn+ , DbError(DbSqlError,DbQueryError,DbResultError)+ , _DbSqlError, _DbQueryError, _DbResultError+ , CanDb+ , liftQueryFirst+ , liftQuery+ , liftInsert+ , liftInsertReturning+ , liftDelete+ , liftUpdate+ , closeEnv+ , derivePGField+ ) where++import Control.Applicative (Applicative)+import Control.Lens+import Control.Monad.Except (MonadError)+import Control.Monad.Reader (MonadReader)+import Control.Monad.Trans (MonadIO, liftIO)+import Data.ByteString (ByteString)+import Data.Int (Int64)+import Data.Profunctor.Product.Default (Default)+import Database.PostgreSQL.Simple (Connection, QueryError, ResultError, SqlError, close)+import Database.PostgreSQL.Simple.FromField (Conversion, Field, FromField (fromField))+import Opaleye+ ( Column, PGBool, Query, QueryRunner, Table, Unpackspec, runDelete, runInsert+ , runInsertReturning, runQuery, runUpdate+ )++data DbEnv = DbEnv+ { _dbEnvConn :: Connection }+makeClassy ''DbEnv++data DbError+ = DbSqlError SqlError+ | DbQueryError QueryError+ | DbResultError ResultError+ deriving Show+makeClassyPrisms ''DbError++type CanDb m c e =+ ( MonadReader c m+ , MonadError e m+ , MonadIO m+ , Applicative m+ , AsDbError e+ , HasDbEnv c+ )++liftQueryFirst+ :: ( CanDb m c e+ , Default QueryRunner a b+ , Applicative m+ )+ => Query a+ -> m (Maybe b)+liftQueryFirst = fmap (^? _head) . liftQuery++liftQuery+ :: ( CanDb m c e+ , Default QueryRunner a b+ )+ => Query a+ -> m [b]+liftQuery q = withConn (`runQuery` q)++liftInsert+ :: CanDb m c e+ => Table colW colR+ -> colW+ -> m Int64+liftInsert t w = withConn $ \conn -> runInsert conn t w++liftInsertReturning+ :: ( CanDb m c e+ , Default QueryRunner ret hask+ , Default Unpackspec ret ret+ )+ => Table colW colR+ -> (colR -> ret)+ -> colW+ -> m [hask]+liftInsertReturning t f c = withConn $ \conn -> runInsertReturning conn t c f++liftUpdate+ :: CanDb m c e+ => Table colW colR+ -> (colR -> colW)+ -> (colR -> Column PGBool)+ -> m Int64+liftUpdate t u p = withConn $ \conn -> runUpdate conn t u p++liftDelete+ :: CanDb m c e+ => Table colW colR+ -> (colR -> Column PGBool)+ -> m Int64+liftDelete t p = withConn $ \conn -> runDelete conn t p++withConn :: CanDb m c e => (Connection -> IO a) -> m a+withConn f = view dbEnvConn >>= flip withGivenConn f++withGivenConn :: CanDb m c e => Connection -> (Connection -> IO a) -> m a+withGivenConn c f = liftIO $ f c++closeEnv :: DbEnv -> IO ()+closeEnv = close . (^.dbEnvConn)++derivePGField+ :: forall a b. FromField a+ => (a -> b)+ -> Field+ -> Maybe ByteString+ -> Conversion b+derivePGField c f = fmap c . fromField f
+ README.md view
@@ -0,0 +1,9 @@+# opaleye-classy++Classy MTL extension of the lovely Opaleye library, which simply just wraps things up in a MonadReader , MonadError context where the config/error are constrained by Classy Lenses/Prisms rather than by concrete non-extensible types.++More info on this pattern can be found in George Wilson's BFPG talk:++http://talks.bfpg.org/talks/2015-06-09.next_level_mtl.html++Note: This API may not be complete. If you need other functions exported then let me know or drop in a PR.
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ opaleye-classy.cabal view
@@ -0,0 +1,40 @@+-- Initial opaleye-classy.cabal generated by cabal init. For further +-- documentation, see http://haskell.org/cabal/users-guide/++name: opaleye-classy+version: 0.1.0.0+synopsis: Opaleye wrapped up in classy MTL attire.+description: + Classy MTL extension of the lovely Opaleye library, which simply just wraps things up in a MonadReader , MonadError context where the config/error are constrained by Classy Lenses/Prisms rather than by concrete non-extensible types.++ More info on this pattern can be found in George Wilson's BFPG talk:+ + http://talks.bfpg.org/talks/2015-06-09.next_level_mtl.html++ Note: This API may not be complete. If you need other functions exported then let me know or drop in a PR.+license: MIT+license-file: LICENSE+author: Ben Kolera+maintainer: ben.kolera@gmail.com+homepage: https://github.com/benkolera/opaleye-classy/tree/master+category: Database+build-type: Simple+extra-source-files: README.md+cabal-version: >=1.10++source-repository head+ type: git+ location: git@github.com:benkolera/opaleye-classy++library+ exposed-modules: Opaleye.Classy+ -- other-modules: + build-depends: base == 4.*+ , bytestring >= 0.10+ , lens >= 4.0+ , mtl >= 2.2+ , postgresql-simple >= 0.4.8.0 && < 0.5+ , product-profunctors >= 0.6.2 && < 0.7+ , transformers >= 0.4+ , opaleye >= 0.4+ default-language: Haskell2010