git.iko.soy/typeable/postgresql-simple
on master help sign in
Changed connect to async use of libpq on non-Windows OSes
45ccff4Leon P Smith
src/Database/PostgreSQL/Simple/Internal.hs+33−2
@@ -45,7 +45,7 @@ import Control.Monad.Trans.Reader
45import Control.Monad.Trans.Class
45import Control.Monad.Trans.Class
46import GHC.IO.Exception
46import GHC.IO.Exception
47#if !defined(mingw32_HOST_OS)
47#if !defined(mingw32_HOST_OS)
48−import Control.Concurrent(threadWaitRead)
48+import Control.Concurrent(threadWaitRead, threadWaitWrite)
49#endif
49#endif
50
50
51-- | A Field represents metadata about a particular field
51-- | A Field represents metadata about a particular field
@@ -187,7 +187,7 @@ connect = connectPostgreSQL . postgreSQLConnectionString
187
187
188connectPostgreSQL :: ByteString -> IO Connection
188connectPostgreSQL :: ByteString -> IO Connection
189connectPostgreSQL connstr = do
189connectPostgreSQL connstr = do
190− conn <- PQ.connectdb connstr
190+ conn <- connectdb connstr
191 stat <- PQ.status conn
191 stat <- PQ.status conn
192 case stat of
192 case stat of
193 PQ.ConnectionOk -> do
193 PQ.ConnectionOk -> do
@@ -205,6 +205,37 @@ connectPostgreSQL connstr = do
205 msg <- maybe "connectPostgreSQL error" id <$> PQ.errorMessage conn
205 msg <- maybe "connectPostgreSQL error" id <$> PQ.errorMessage conn
206 throwIO $ fatalError msg
206 throwIO $ fatalError msg
207
207
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+
208-- | Turns a 'ConnectInfo' data structure into a libpq connection string.
239-- | Turns a 'ConnectInfo' data structure into a libpq connection string.
209
240
210postgreSQLConnectionString :: ConnectInfo -> ByteString
241postgreSQLConnectionString :: ConnectInfo -> ByteString