[Haskell-cafe] Handling Postgresql array types

Riaan info at rotnetix.com
Sat Dec 27 04:17:56 UTC 2014


Thank you very much.  I have actually started to have a look at opaleye
independently after the reddit discussion and I would like to experiment
with it.

I assume that I somehow have to declare my own queryRunnerColumn for the
array.  I am afraid that I did not understand the example so I was not able
to generalise it.

Date: Fri, 26 Dec 2014 11:02:12 +0000
From: Tom Ellis <tom-lists-haskell-cafe-2013 at jaguarpaw.co.uk>
To: haskell-cafe at haskell.org
Subject: Re: [Haskell-cafe] Handling Postgresql array types
Message-ID: <20141226110212.GJ31575 at weber>
Content-Type: text/plain; charset=us-ascii

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]}]}}]
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/haskell-cafe/attachments/20141227/1d5c082f/attachment.html>


More information about the Haskell-Cafe mailing list