git.iko.soy/typeable/postgresql-simple
on master help sign in
Add PGArray Type
0e2492eLeon P Smith
src/Database/PostgreSQL/Simple/FromField.hs+6−3
@@ -111,7 +111,7 @@ import qualified Data.Vector as V
111import Database.PostgreSQL.Simple.Internal
111import Database.PostgreSQL.Simple.Internal
112import Database.PostgreSQL.Simple.Compat
112import Database.PostgreSQL.Simple.Compat
113import Database.PostgreSQL.Simple.Ok
113import Database.PostgreSQL.Simple.Ok
114−import Database.PostgreSQL.Simple.Types (Binary(..), Null(..))
114+import Database.PostgreSQL.Simple.Types
115import Database.PostgreSQL.Simple.TypeInfo as TI
115import Database.PostgreSQL.Simple.TypeInfo as TI
116import qualified Database.PostgreSQL.Simple.TypeInfo.Static as TI
116import qualified Database.PostgreSQL.Simple.TypeInfo.Static as TI
117import Database.PostgreSQL.Simple.TypeInfo.Macro as TI
117import Database.PostgreSQL.Simple.TypeInfo.Macro as TI
@@ -414,7 +414,7 @@ instance (FromField a, FromField b) => FromField (Either a b) where
414 <|> (Left <$> fromField f dat)
414 <|> (Left <$> fromField f dat)
415
415
416-- | any postgresql array whose elements are compatible with type @a@
416-- | any postgresql array whose elements are compatible with type @a@
417−instance (FromField a, Typeable a) => FromField (Vector a) where
417+instance (FromField a, Typeable a) => FromField (PGArray a) where
418 fromField f mdat = do
418 fromField f mdat = do
419 info <- typeInfo f
419 info <- typeInfo f
420 case info of
420 case info of
@@ -424,7 +424,7 @@ instance (FromField a, Typeable a) => FromField (Vector a) where
424 Just dat -> do
424 Just dat -> do
425 case parseOnly (fromArray info f) dat of
425 case parseOnly (fromArray info f) dat of
426 Left err -> returnError ConversionFailed f err
426 Left err -> returnError ConversionFailed f err
427− Right conv -> V.fromList <$> conv
427+ Right conv -> PGArray <$> conv
428 _ -> returnError Incompatible f ""
428 _ -> returnError Incompatible f ""
429
429
430fromArray :: (FromField a)
430fromArray :: (FromField a)
@@ -437,6 +437,9 @@ fromArray typeInfo f = sequence . (parseIt <$>) <$> array delim
437 where f' | Arrays.Array _ <- item = f
437 where f' | Arrays.Array _ <- item = f
438 | otherwise = fElem
438 | otherwise = fElem
439
439
440+instance (FromField a, Typeable a) => FromField (Vector a) where
441+ fromField f v = V.fromList . fromPGArray <$> fromField f v
442+
440-- | uuid
443-- | uuid
441instance FromField UUID where
444instance FromField UUID where
442 fromField f mbs =
445 fromField f mbs =
src/Database/PostgreSQL/Simple/ToField.hs+6−3
@@ -33,7 +33,7 @@ import Data.Monoid (mappend)
33import Data.Time (Day, TimeOfDay, LocalTime, UTCTime, ZonedTime)
33import Data.Time (Day, TimeOfDay, LocalTime, UTCTime, ZonedTime)
34import Data.Typeable (Typeable)
34import Data.Typeable (Typeable)
35import Data.Word (Word, Word8, Word16, Word32, Word64)
35import Data.Word (Word, Word8, Word16, Word32, Word64)
36−import Database.PostgreSQL.Simple.Types (Binary(..), In(..), Null, Default)
36+import Database.PostgreSQL.Simple.Types
37import qualified Blaze.ByteString.Builder.Char.Utf8 as Utf8
37import qualified Blaze.ByteString.Builder.Char.Utf8 as Utf8
38import qualified Data.ByteString as SB
38import qualified Data.ByteString as SB
39import qualified Data.ByteString.Lazy as LB
39import qualified Data.ByteString.Lazy as LB
@@ -230,14 +230,17 @@ instance ToField Date where
230 toField = Plain . inQuotes . dateToBuilder
230 toField = Plain . inQuotes . dateToBuilder
231 {-# INLINE toField #-}
231 {-# INLINE toField #-}
232
232
233−instance (ToField a) => ToField (Vector a) where
233+instance (ToField a) => ToField (PGArray a) where
234 toField xs = Many $
234 toField xs = Many $
235 Plain (fromByteString "ARRAY[") :
235 Plain (fromByteString "ARRAY[") :
236− (intersperse (Plain (fromChar ',')) . map toField $ V.toList xs) ++
236+ (intersperse (Plain (fromChar ',')) . map toField $ fromPGArray xs) ++
237 [Plain (fromChar ']')]
237 [Plain (fromChar ']')]
238 -- Because the ARRAY[...] input syntax is being used, it is possible
238 -- Because the ARRAY[...] input syntax is being used, it is possible
239 -- that the use of type-specific separator characters is unnecessary.
239 -- that the use of type-specific separator characters is unnecessary.
240
240
241+instance (ToField a) => ToField (Vector a) where
242+ toField = toField . PGArray . V.toList
243+
241instance ToField UUID where
244instance ToField UUID where
242 toField = Plain . inQuotes . fromByteString . UUID.toASCIIBytes
245 toField = Plain . inQuotes . fromByteString . UUID.toASCIIBytes
243
246
src/Database/PostgreSQL/Simple/Types.hs+6
@@ -24,6 +24,7 @@ module Database.PostgreSQL.Simple.Types
24 , Oid(..)
24 , Oid(..)
25 , (:.)(..)
25 , (:.)(..)
26 , Savepoint(..)
26 , Savepoint(..)
27+ , PGArray(..)
27 ) where
28 ) where
28
29
29import Blaze.ByteString.Builder (toByteString)
30import Blaze.ByteString.Builder (toByteString)
@@ -117,6 +118,11 @@ newtype In a = In a
117newtype Binary a = Binary {fromBinary :: a}
118newtype Binary a = Binary {fromBinary :: a}
118 deriving (Eq, Ord, Read, Show, Typeable, Functor)
119 deriving (Eq, Ord, Read, Show, Typeable, Functor)
119
120
121+
122+-- | Wrap a list for use as a PostgreSQL array.
123+newtype PGArray a = PGArray {fromPGArray :: [a]}
124+ deriving (Eq, Ord, Read, Show, Typeable, Functor)
125+
120-- | A composite type to parse your custom data structures without
126-- | A composite type to parse your custom data structures without
121-- having to define dummy newtype wrappers every time.
127-- having to define dummy newtype wrappers every time.
122--
128--