git.iko.soy/typeable/postgresql-simple
on master help sign in
Changed connect to async use of libpq on non-Windows OSes
commit45ccff4be6bcc8e0f4d70efdaf23a5028a50588c authorLeon P Smith <leon@melding-monads.com> dateThu 10 Jul 2014, 11:47 (12 years ago) parent83d3c38
1 file changed+33−2
src/Database/PostgreSQL/Simple/Internal.hs+33−2
src/Database/PostgreSQL/Simple/Internal.hs+33−2
@@ -45,7 +45,7 @@ import Control.Monad.Trans.Reader
4545import Control.Monad.Trans.Class
4646import GHC.IO.Exception
4747#if !defined(mingw32_HOST_OS)
48−import Control.Concurrent(threadWaitRead)
48+import Control.Concurrent(threadWaitRead, threadWaitWrite)
4949#endif
5050
5151-- | A Field represents metadata about a particular field
@@ -187,7 +187,7 @@ connect = connectPostgreSQL . postgreSQLConnectionString
187187
188188connectPostgreSQL :: ByteString -> IO Connection
189189connectPostgreSQL connstr = do
190− conn <- PQ.connectdb connstr
190+ conn <- connectdb connstr
191191 stat <- PQ.status conn
192192 case stat of
193193 PQ.ConnectionOk -> do
@@ -205,6 +205,37 @@ connectPostgreSQL connstr = do
205205 msg <- maybe "connectPostgreSQL error" id <$> PQ.errorMessage conn
206206 throwIO $ fatalError msg
207207
208+connectdb :: ByteString -> IO PQ.Connection
209+#if defined(mingw32_HOST_OS)
210+connectdb = PQ.connectdb
211+#else
212+connectdb conninfo = do
213+ conn <- PQ.connectStart conninfo
214+ loop conn
215+ where
216+ funcName = "Database.PostgreSQL.Simple.connectPostgreSQL"
217+ loop conn = do
218+ status <- PQ.connectPoll conn
219+ case status of
220+ PQ.PollingFailed -> throwLibPQError conn "connection failed"
221+ PQ.PollingReading -> do
222+ mfd <- PQ.socket conn
223+ case mfd of
224+ Nothing -> throwIO $! fdError funcName
225+ Just fd -> do
226+ threadWaitRead fd
227+ loop conn
228+ PQ.PollingWriting -> do
229+ mfd <- PQ.socket conn
230+ case mfd of
231+ Nothing -> throwIO $! fdError funcName
232+ Just fd -> do
233+ threadWaitWrite fd
234+ loop conn
235+ PQ.PollingOk -> return conn
236+
237+#endif
238+
208239-- | Turns a 'ConnectInfo' data structure into a libpq connection string.
209240
210241postgreSQLConnectionString :: ConnectInfo -> ByteString