git.iko.soy/typeable/postgresql-simple
on master help sign in
.Internal: Add newTempName
2a960beJoey Adams
src/Database/PostgreSQL/Simple/Internal.hs+14−1
@@ -1,4 +1,7 @@
1+{-# LANGUAGE BangPatterns #-}
2+{-# LANGUAGE DeriveDataTypeable #-}
1−{-# LANGUAGE RecordWildCards, DeriveDataTypeable, GeneralizedNewtypeDeriving #-}
3+{-# LANGUAGE GeneralizedNewtypeDeriving #-}
4+{-# LANGUAGE RecordWildCards #-}
2------------------------------------------------------------------------------
5------------------------------------------------------------------------------
3-- |
6-- |
4-- Module: Database.PostgreSQL.Simple.Internal
7-- Module: Database.PostgreSQL.Simple.Internal
@@ -30,6 +33,7 @@ import qualified Data.ByteString.Char8 as B8
30import Data.Char (ord)
33import Data.Char (ord)
31import Data.Int (Int64)
34import Data.Int (Int64)
32import qualified Data.IntMap as IntMap
35import qualified Data.IntMap as IntMap
36+import Data.IORef
33import Data.Maybe(fromMaybe)
37import Data.Maybe(fromMaybe)
34import Data.String
38import Data.String
35import Data.Typeable
39import Data.Typeable
@@ -62,6 +66,7 @@ type TypeInfoCache = IntMap.IntMap TypeInfo
62data Connection = Connection {
66data Connection = Connection {
63 connectionHandle :: {-# UNPACK #-} !(MVar PQ.Connection)
67 connectionHandle :: {-# UNPACK #-} !(MVar PQ.Connection)
64 , connectionObjects :: {-# UNPACK #-} !(MVar TypeInfoCache)
68 , connectionObjects :: {-# UNPACK #-} !(MVar TypeInfoCache)
69+ , connectionTempNameCounter :: {-# UNPACK #-} !(IORef Int64)
65 }
70 }
66
71
67data SqlError = SqlError {
72data SqlError = SqlError {
@@ -138,6 +143,7 @@ connectPostgreSQL connstr = do
138 PQ.ConnectionOk -> do
143 PQ.ConnectionOk -> do
139 connectionHandle <- newMVar conn
144 connectionHandle <- newMVar conn
140 connectionObjects <- newMVar (IntMap.empty)
145 connectionObjects <- newMVar (IntMap.empty)
146+ connectionTempNameCounter <- newIORef 0
141 let wconn = Connection{..}
147 let wconn = Connection{..}
142 version <- PQ.serverVersion conn
148 version <- PQ.serverVersion conn
143 let settings
149 let settings
@@ -280,6 +286,7 @@ newNullConnection :: IO Connection
280newNullConnection = do
286newNullConnection = do
281 connectionHandle <- newMVar =<< PQ.newNullConnection
287 connectionHandle <- newMVar =<< PQ.newNullConnection
282 connectionObjects <- newMVar IntMap.empty
288 connectionObjects <- newMVar IntMap.empty
289+ connectionTempNameCounter <- newIORef 0
283 return Connection{..}
290 return Connection{..}
284
291
285data Row = Row {
292data Row = Row {
@@ -328,3 +335,9 @@ conversionMap f m = Conversion $ \conn -> f <$> runConversion m conn
328
335
329conversionError :: Exception err => err -> Conversion a
336conversionError :: Exception err => err -> Conversion a
330conversionError err = Conversion $ \_ -> return (Errors [SomeException err])
337conversionError err = Conversion $ \_ -> return (Errors [SomeException err])
338+
339+newTempName :: Connection -> IO Query
340+newTempName Connection{..} = do
341+ !n <- atomicModifyIORef connectionTempNameCounter
342+ (\n -> let !n' = n+1 in (n', n'))
343+ return $! Query $ B8.pack $ "temp" ++ show n