[Haskell-cafe] Handling Postgresql array types

Tom Ellis tom-lists-haskell-cafe-2013 at jaguarpaw.co.uk
Fri Dec 26 11:02:12 UTC 2014


On Fri, Dec 26, 2014 at 10:52:46AM +0000, Tom Ellis wrote:
> On Fri, Dec 26, 2014 at 02:36:07PM +1100, Riaan wrote:
> > So went back to HDBC.  But now my queries are starting to get fairly long
> > and I have been looking at libraries like Persistent with Esqualeto,
> > HaskellDB and Groundhog to make my queries a little more composable and
> > type safe.
> 
> Don't forget to look at Opaleye too :)  It doesn't really have much support
> for arrays at the moment, but if you let me know what you want to do I'm
> happy to help you.
> 
> > So to my question.  Does anyone have experience with one of these libraries
> > in dealing with postgresql arrays
> 
> postgresql-simple has
> 
>     instance (FromField a, Typeable a) => FromField (PGArray a)
> 
> Does that not do exactly what you want?

import Database.PostgreSQL.Simple (query_, ConnectInfo(..), connect, Only)
import Database.PostgreSQL.Simple.Types (PGArray)

import Data.String (fromString)

connectInfo :: ConnectInfo
connectInfo =  ConnectInfo { connectHost = "localhost"
                           , connectPort = 25433
                           , connectUser = "tom"
                           , connectPassword = "tom"
                           , connectDatabase = "opaleye_test" }

arrayQuery :: String
arrayQuery = "select '{{1,2}, {3,4}}' :: integer[][]"

main :: IO ()
main = do
  conn <- connect connectInfo
  results <- query_ conn (fromString arrayQuery) :: IO [Only (PGArray (PGArray Int))]
  print results
              
-- Output
--
-- ghci> main
-- [Only {fromOnly = PGArray {fromPGArray = [PGArray {fromPGArray = [1,2]},PGArray {fromPGArray = [3,4]}]}}]


More information about the Haskell-Cafe mailing list