[Haskell-cafe] Generic use of queries postgresql-simple

Pietro Grandinetti pietro.gra at hotmail.it
Thu Feb 9 18:31:48 UTC 2023


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

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/haskell-cafe/attachments/20230209/cba11528/attachment.html>


More information about the Haskell-Cafe mailing list