[Haskell-cafe] Handling Postgresql array types

info at rotnetix.com info at rotnetix.com
Sat Dec 27 05:31:02 UTC 2014


Thank you for pointing me in the right way.  This does almost what I want. 
 However I want to end up with a [[Double]] rather than (PGArray (PGArray 
Double)). 

I know I can do the conversion using fromPGArray but I was hoping to use 
the fromRow instances to remove some of the boilerplate.

So I have:
data IIM = IIM {key :: String
               ,itype :: String
               ,idet :: Maybe String
               ,imat :: (PGArray (PGArray Double))} deriving (Read, Show, 
Eq)

instance FromRow IIM where 
  fromRow = IIM <$> field <*> field <*> field <*> field

Which works fine, but I would actually like my data IIM to be:

data IIM = IIM {key :: String
               ,itype :: String
               ,idet :: Maybe String
               ,imat :: [[Double]]} deriving (Read, Show, Eq)

But I can't figure out how to get a fromRow instance for that, otherwise I 
need to return SQL results in a (,,,,) and then create the IIM from that 
which seems a lot of extra typing.

PS. I started looking into Opaleye as a possible DSL and would appreciate 
any help you can give me in figuring out how to represent arrays in that. 
 I am assuming I need to do something with queryRunnerColumn but I was not 
able to understand the example.

On Friday, December 26, 2014 10:02:21 PM UTC+11, Tom Ellis wrote:
>
> 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]}]}}] 
> _______________________________________________ 
> Haskell-Cafe mailing list 
> Haskel... at haskell.org <javascript:> 
> http://www.haskell.org/mailman/listinfo/haskell-cafe 
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/haskell-cafe/attachments/20141226/b17f2c37/attachment-0001.html>


More information about the Haskell-Cafe mailing list