Send Beginners mailing list submissions to
        beginners@haskell.org

To subscribe or unsubscribe via the World Wide Web, visit
        http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
or, via email, send a message with subject or body 'help' to
        beginners-requ...@haskell.org

You can reach the person managing the list at
        beginners-ow...@haskell.org

When replying, please edit your Subject line so it is more specific
than "Re: Contents of Beginners digest..."


Today's Topics:

   1.  Generic use of queries postgresql-simple (Pietro Grandinetti)


----------------------------------------------------------------------

Message: 1
Date: Thu, 9 Feb 2023 14:08:05 +0000
From: Pietro Grandinetti <pietro....@hotmail.it>
To: "beginners@haskell.org" <beginners@haskell.org>
Subject: [Haskell-beginners] Generic use of queries postgresql-simple
Message-ID:
        
<db9p191mb15142db64147c59d40685aeefc...@db9p191mb1514.eurp191.prod.outlook.com>
        
Content-Type: text/plain; charset="iso-8859-1"

Hello--

I use postgresql-simple and for selecting rows from one table I have the 
following function.

runMyQuery :: Connection
           -> String           -- query with '?' params to bind
           -> Int              -- first param
           -> Int              -- second param
           -> IO [MyTable1]
runMyQuery conn dbQuery param1 param2 = query conn dbQuery $ (param2, param2)

data MyTable1 = MyTable1 {col1 :: Int, col2 :: Int, col3 :: String}

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

There are some things I'd like to improve:

  1.  I will have to write "MyTable1", "MyTable2", ... datatypes, one for each 
table, but would like to have only one function runMyQuery where the return 
type is a generic IO [MyModel]. My problem is that I don't know how to write it 
for tables with different number of columns and different SQL types.
  2.  The 2nd to the penultimate arguments of runMyQuery are the arguments to 
bind into the SQL string query. This means that I should write several 
different version of "runMyQuery" each with a different number of arguments, 
but would like to have only one.

Can you recommend some approaches?

Thanks.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://mail.haskell.org/pipermail/beginners/attachments/20230209/021c8509/attachment-0001.html>

------------------------------

Subject: Digest Footer

_______________________________________________
Beginners mailing list
Beginners@haskell.org
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners


------------------------------

End of Beginners Digest, Vol 169, Issue 1
*****************************************

Reply via email to