@@ -25,6 +25,7 @@ module Database.Persist.Postgresql
2525
2626import Database.Persist.Sql
2727import Database.Persist.Sql.Util (dbIdColumnsEsc )
28+ import Database.Persist.Sql.Types.Internal (mkPersistBackend )
2829import Data.Fixed (Pico )
2930
3031import qualified Database.PostgreSQL.Simple as PG
@@ -73,6 +74,7 @@ import Data.Acquire (Acquire, mkAcquire, with)
7374import System.Environment (getEnvironment )
7475import Data.Int (Int64 )
7576import Data.Monoid ((<>) )
77+ import Data.Pool (Pool )
7678import Data.Time (utc , localTimeToUTC )
7779
7880-- | A @libpq@ connection string. A simple example of connection
@@ -89,13 +91,13 @@ type ConnectionString = ByteString
8991-- finishes using it. Note that you should not use the given
9092-- 'ConnectionPool' outside the action since it may be already
9193-- been released.
92- withPostgresqlPool :: (MonadBaseControl IO m , MonadLogger m , MonadIO m )
94+ withPostgresqlPool :: (MonadBaseControl IO m , MonadLogger m , MonadIO m , IsPersistBackend backend , BaseBackend backend ~ SqlBackend )
9395 => ConnectionString
9496 -- ^ Connection string to the database.
9597 -> Int
9698 -- ^ Number of connections to be kept open in
9799 -- the pool.
98- -> (ConnectionPool -> m a )
100+ -> (Pool backend -> m a )
99101 -- ^ Action to be executed that uses the
100102 -- connection pool.
101103 -> m a
@@ -106,13 +108,13 @@ withPostgresqlPool ci = withSqlPool $ open' (const $ return ()) ci
106108-- responsibility to properly close the connection pool when
107109-- unneeded. Use 'withPostgresqlPool' for an automatic resource
108110-- control.
109- createPostgresqlPool :: (MonadIO m , MonadBaseControl IO m , MonadLogger m )
111+ createPostgresqlPool :: (MonadIO m , MonadBaseControl IO m , MonadLogger m , IsPersistBackend backend , BaseBackend backend ~ SqlBackend )
110112 => ConnectionString
111113 -- ^ Connection string to the database.
112114 -> Int
113115 -- ^ Number of connections to be kept open
114116 -- in the pool.
115- -> m ConnectionPool
117+ -> m ( Pool backend )
116118createPostgresqlPool = createPostgresqlPoolModified (const $ return () )
117119
118120-- | Same as 'createPostgresqlPool', but additionally takes a callback function
@@ -124,32 +126,33 @@ createPostgresqlPool = createPostgresqlPoolModified (const $ return ())
124126--
125127-- Since 2.1.3
126128createPostgresqlPoolModified
127- :: (MonadIO m , MonadBaseControl IO m , MonadLogger m )
129+ :: (MonadIO m , MonadBaseControl IO m , MonadLogger m , IsPersistBackend backend , BaseBackend backend ~ SqlBackend )
128130 => (PG. Connection -> IO () ) -- ^ action to perform after connection is created
129131 -> ConnectionString -- ^ Connection string to the database.
130132 -> Int -- ^ Number of connections to be kept open in the pool.
131- -> m ConnectionPool
133+ -> m ( Pool backend )
132134createPostgresqlPoolModified modConn ci = createSqlPool $ open' modConn ci
133135
134136-- | Same as 'withPostgresqlPool', but instead of opening a pool
135137-- of connections, only one connection is opened.
136- withPostgresqlConn :: (MonadIO m , MonadBaseControl IO m , MonadLogger m )
137- => ConnectionString -> (SqlBackend -> m a ) -> m a
138+ withPostgresqlConn :: (MonadIO m , MonadBaseControl IO m , MonadLogger m , IsPersistBackend backend , BaseBackend backend ~ SqlBackend )
139+ => ConnectionString -> (backend -> m a ) -> m a
138140withPostgresqlConn = withSqlConn . open' (const $ return () )
139141
140- open' :: (PG. Connection -> IO () )
141- -> ConnectionString -> LogFunc -> IO SqlBackend
142+ open'
143+ :: (IsPersistBackend backend , BaseBackend backend ~ SqlBackend )
144+ => (PG. Connection -> IO () ) -> ConnectionString -> LogFunc -> IO backend
142145open' modConn cstr logFunc = do
143146 conn <- PG. connectPostgreSQL cstr
144147 modConn conn
145148 openSimpleConn logFunc conn
146149
147150
148151-- | Generate a 'Connection' from a 'PG.Connection'
149- openSimpleConn :: LogFunc -> PG. Connection -> IO SqlBackend
152+ openSimpleConn :: ( IsPersistBackend backend , BaseBackend backend ~ SqlBackend ) => LogFunc -> PG. Connection -> IO backend
150153openSimpleConn logFunc conn = do
151154 smap <- newIORef $ Map. empty
152- return SqlBackend
155+ return . mkPersistBackend $ SqlBackend
153156 { connPrepare = prepare' conn
154157 , connStmtMap = smap
155158 , connInsertSql = insertSql'
@@ -899,18 +902,12 @@ showAlter table (_, DropReference cname) = T.concat
899902
900903-- | get the SQL string for the table that a PeristEntity represents
901904-- Useful for raw SQL queries
902- tableName :: forall record .
903- ( PersistEntity record
904- , PersistEntityBackend record ~ SqlBackend
905- ) => record -> Text
905+ tableName :: (PersistEntity record ) => record -> Text
906906tableName = escape . tableDBName
907907
908908-- | get the SQL string for the field that an EntityField represents
909909-- Useful for raw SQL queries
910- fieldName :: forall record typ .
911- ( PersistEntity record
912- , PersistEntityBackend record ~ SqlBackend
913- ) => EntityField record typ -> Text
910+ fieldName :: (PersistEntity record ) => EntityField record typ -> Text
914911fieldName = escape . fieldDBName
915912
916913escape :: DBName -> Text
0 commit comments