forked from brandonchinn178/persistent-mtl
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request brandonchinn178#6 from brandonchinn178/improvements
Break out code into separate modules
- Loading branch information
Showing
9 changed files
with
227 additions
and
114 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
module Database.Persist.Monad.Class | ||
( MonadSqlQuery(..) | ||
) where | ||
|
||
import Control.Monad.Trans.Class (lift) | ||
import qualified Control.Monad.Trans.Except as Except | ||
import qualified Control.Monad.Trans.Identity as Identity | ||
import qualified Control.Monad.Trans.Maybe as Maybe | ||
import qualified Control.Monad.Trans.RWS.Lazy as RWS.Lazy | ||
import qualified Control.Monad.Trans.RWS.Strict as RWS.Strict | ||
import qualified Control.Monad.Trans.Reader as Reader | ||
import qualified Control.Monad.Trans.State.Lazy as State.Lazy | ||
import qualified Control.Monad.Trans.State.Strict as State.Strict | ||
import qualified Control.Monad.Trans.Writer.Lazy as Writer.Lazy | ||
import qualified Control.Monad.Trans.Writer.Strict as Writer.Strict | ||
import Data.Typeable (Typeable) | ||
|
||
import Database.Persist.Monad.SqlQueryRep (SqlQueryRep) | ||
|
||
class Monad m => MonadSqlQuery m where | ||
runQueryRep :: Typeable record => SqlQueryRep record a -> m a | ||
withTransaction :: m a -> m a | ||
|
||
{- Instances for common monad transformers -} | ||
|
||
instance MonadSqlQuery m => MonadSqlQuery (Reader.ReaderT r m) where | ||
runQueryRep = lift . runQueryRep | ||
withTransaction = Reader.mapReaderT withTransaction | ||
|
||
instance MonadSqlQuery m => MonadSqlQuery (Except.ExceptT e m) where | ||
runQueryRep = lift . runQueryRep | ||
withTransaction = Except.mapExceptT withTransaction | ||
|
||
instance MonadSqlQuery m => MonadSqlQuery (Identity.IdentityT m) where | ||
runQueryRep = lift . runQueryRep | ||
withTransaction = Identity.mapIdentityT withTransaction | ||
|
||
instance MonadSqlQuery m => MonadSqlQuery (Maybe.MaybeT m) where | ||
runQueryRep = lift . runQueryRep | ||
withTransaction = Maybe.mapMaybeT withTransaction | ||
|
||
instance (Monoid w, MonadSqlQuery m) => MonadSqlQuery (RWS.Lazy.RWST r w s m) where | ||
runQueryRep = lift . runQueryRep | ||
withTransaction = RWS.Lazy.mapRWST withTransaction | ||
|
||
instance (Monoid w, MonadSqlQuery m) => MonadSqlQuery (RWS.Strict.RWST r w s m) where | ||
runQueryRep = lift . runQueryRep | ||
withTransaction = RWS.Strict.mapRWST withTransaction | ||
|
||
instance MonadSqlQuery m => MonadSqlQuery (State.Lazy.StateT s m) where | ||
runQueryRep = lift . runQueryRep | ||
withTransaction = State.Lazy.mapStateT withTransaction | ||
|
||
instance MonadSqlQuery m => MonadSqlQuery (State.Strict.StateT s m) where | ||
runQueryRep = lift . runQueryRep | ||
withTransaction = State.Strict.mapStateT withTransaction | ||
|
||
instance (Monoid w, MonadSqlQuery m) => MonadSqlQuery (Writer.Lazy.WriterT w m) where | ||
runQueryRep = lift . runQueryRep | ||
withTransaction = Writer.Lazy.mapWriterT withTransaction | ||
|
||
instance (Monoid w, MonadSqlQuery m) => MonadSqlQuery (Writer.Strict.WriterT w m) where | ||
runQueryRep = lift . runQueryRep | ||
withTransaction = Writer.Strict.mapWriterT withTransaction |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
{-# LANGUAGE GADTs #-} | ||
|
||
module Database.Persist.Monad.Shim where | ||
|
||
import Control.Monad.IO.Unlift (MonadUnliftIO) | ||
import Data.Text (Text) | ||
import Data.Typeable (Typeable) | ||
import Database.Persist | ||
import Database.Persist.Sql | ||
|
||
import Database.Persist.Monad.Class (MonadSqlQuery(..)) | ||
import Database.Persist.Monad.SqlQueryRep (SqlQueryRep(..)) | ||
|
||
selectList :: (PersistRecordBackend record SqlBackend, Typeable record, MonadSqlQuery m) => [Filter record] -> [SelectOpt record] -> m [Entity record] | ||
selectList a b = runQueryRep $ SelectList a b | ||
|
||
insert :: (PersistRecordBackend record SqlBackend, Typeable record, MonadSqlQuery m) => record -> m (Key record) | ||
insert a = runQueryRep $ Insert a | ||
|
||
insert_ :: (PersistRecordBackend record SqlBackend, Typeable record, MonadSqlQuery m) => record -> m () | ||
insert_ a = runQueryRep $ Insert_ a | ||
|
||
runMigrationSilent :: (MonadUnliftIO m, MonadSqlQuery m) => Migration -> m [Text] | ||
runMigrationSilent a = runQueryRep $ RunMigrationsSilent a |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
{-# LANGUAGE GADTs #-} | ||
{-# LANGUAGE LambdaCase #-} | ||
{-# LANGUAGE ScopedTypeVariables #-} | ||
{-# LANGUAGE TypeApplications #-} | ||
|
||
module Database.Persist.Monad.SqlQueryRep | ||
( SqlQueryRep(..) | ||
, runSqlQueryRep | ||
) where | ||
|
||
import Control.Monad.IO.Unlift (MonadUnliftIO) | ||
import Data.Proxy (Proxy(..)) | ||
import Data.Text (Text) | ||
import Data.Typeable (Typeable, eqT, typeRep, (:~:)(..)) | ||
import Data.Void (Void) | ||
import Database.Persist (Entity, Filter, Key, PersistRecordBackend, SelectOpt) | ||
import Database.Persist.Sql (Migration, SqlBackend) | ||
import qualified Database.Persist.Sql as Persist | ||
|
||
-- TODO: generate this | ||
data SqlQueryRep record a where | ||
SelectList | ||
:: PersistRecordBackend record SqlBackend | ||
=> [Filter record] -> [SelectOpt record] -> SqlQueryRep record [Entity record] | ||
|
||
Insert | ||
:: PersistRecordBackend record SqlBackend | ||
=> record -> SqlQueryRep record (Key record) | ||
|
||
Insert_ | ||
:: PersistRecordBackend record SqlBackend | ||
=> record -> SqlQueryRep record () | ||
|
||
RunMigrationsSilent | ||
:: Migration -> SqlQueryRep Void [Text] | ||
|
||
instance Typeable record => Show (SqlQueryRep record a) where | ||
show = \case | ||
SelectList{} -> "SelectList{..}" ++ record | ||
Insert{} -> "Insert{..}" ++ record | ||
Insert_{} -> "Insert_{..}" ++ record | ||
RunMigrationsSilent{} -> "RunMigrationsSilent{..}" ++ record | ||
where | ||
record = case recordTypeRep of | ||
Just recordType -> "<" ++ show recordType ++ ">" | ||
Nothing -> "" | ||
recordTypeRep = case eqT @record @Void of | ||
Just Refl -> Nothing | ||
Nothing -> Just $ typeRep $ Proxy @record | ||
|
||
runSqlQueryRep :: MonadUnliftIO m => SqlQueryRep record a -> Persist.SqlPersistT m a | ||
runSqlQueryRep = \case | ||
SelectList a b -> Persist.selectList a b | ||
Insert a -> Persist.insert a | ||
Insert_ a -> Persist.insert_ a | ||
RunMigrationsSilent a -> Persist.runMigrationSilent a |
Oops, something went wrong.