git.iko.soy/typeable/postgresql-simple
on master help sign in
Add PGArray Type

Credit to Joey Adams; closes #50

commit0e2492ead4cebf6416d7222887f25cc576d9854c authorLeon P Smith <leon@melding-monads.com> dateSat 21 Dec 2013, 06:32 (13 years ago) parentdc6932a
3 files changed+18−6
src/Database/PostgreSQL/Simple/FromField.hs+6−3 src/Database/PostgreSQL/Simple/ToField.hs+6−3 src/Database/PostgreSQL/Simple/Types.hs+6
src/Database/PostgreSQL/Simple/FromField.hs+6−3
@@ -111,7 +111,7 @@ import qualified Data.Vector as V
111111import Database.PostgreSQL.Simple.Internal
112112import Database.PostgreSQL.Simple.Compat
113113import Database.PostgreSQL.Simple.Ok
114−import Database.PostgreSQL.Simple.Types (Binary(..), Null(..))
114+import Database.PostgreSQL.Simple.Types
115115import Database.PostgreSQL.Simple.TypeInfo as TI
116116import qualified Database.PostgreSQL.Simple.TypeInfo.Static as TI
117117import Database.PostgreSQL.Simple.TypeInfo.Macro as TI
@@ -414,7 +414,7 @@ instance (FromField a, FromField b) => FromField (Either a b) where
414414 <|> (Left <$> fromField f dat)
415415
416416-- | 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
418418 fromField f mdat = do
419419 info <- typeInfo f
420420 case info of
@@ -424,7 +424,7 @@ instance (FromField a, Typeable a) => FromField (Vector a) where
424424 Just dat -> do
425425 case parseOnly (fromArray info f) dat of
426426 Left err -> returnError ConversionFailed f err
427− Right conv -> V.fromList <$> conv
427+ Right conv -> PGArray <$> conv
428428 _ -> returnError Incompatible f ""
429429
430430fromArray :: (FromField a)
@@ -437,6 +437,9 @@ fromArray typeInfo f = sequence . (parseIt <$>) <$> array delim
437437 where f' | Arrays.Array _ <- item = f
438438 | otherwise = fElem
439439
440+instance (FromField a, Typeable a) => FromField (Vector a) where
441+ fromField f v = V.fromList . fromPGArray <$> fromField f v
442+
440443-- | uuid
441444instance FromField UUID where
442445 fromField f mbs =
src/Database/PostgreSQL/Simple/ToField.hs+6−3
@@ -33,7 +33,7 @@ import Data.Monoid (mappend)
3333import Data.Time (Day, TimeOfDay, LocalTime, UTCTime, ZonedTime)
3434import Data.Typeable (Typeable)
3535import Data.Word (Word, Word8, Word16, Word32, Word64)
36−import Database.PostgreSQL.Simple.Types (Binary(..), In(..), Null, Default)
36+import Database.PostgreSQL.Simple.Types
3737import qualified Blaze.ByteString.Builder.Char.Utf8 as Utf8
3838import qualified Data.ByteString as SB
3939import qualified Data.ByteString.Lazy as LB
@@ -230,14 +230,17 @@ instance ToField Date where
230230 toField = Plain . inQuotes . dateToBuilder
231231 {-# INLINE toField #-}
232232
233−instance (ToField a) => ToField (Vector a) where
233+instance (ToField a) => ToField (PGArray a) where
234234 toField xs = Many $
235235 Plain (fromByteString "ARRAY[") :
236− (intersperse (Plain (fromChar ',')) . map toField $ V.toList xs) ++
236+ (intersperse (Plain (fromChar ',')) . map toField $ fromPGArray xs) ++
237237 [Plain (fromChar ']')]
238238 -- Because the ARRAY[...] input syntax is being used, it is possible
239239 -- that the use of type-specific separator characters is unnecessary.
240240
241+instance (ToField a) => ToField (Vector a) where
242+ toField = toField . PGArray . V.toList
243+
241244instance ToField UUID where
242245 toField = Plain . inQuotes . fromByteString . UUID.toASCIIBytes
243246
src/Database/PostgreSQL/Simple/Types.hs+6
@@ -24,6 +24,7 @@ module Database.PostgreSQL.Simple.Types
2424 , Oid(..)
2525 , (:.)(..)
2626 , Savepoint(..)
27+ , PGArray(..)
2728 ) where
2829
2930import Blaze.ByteString.Builder (toByteString)
@@ -117,6 +118,11 @@ newtype In a = In a
117118newtype Binary a = Binary {fromBinary :: a}
118119 deriving (Eq, Ord, Read, Show, Typeable, Functor)
119120
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+
120126-- | A composite type to parse your custom data structures without
121127-- having to define dummy newtype wrappers every time.
122128--