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:
- 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.
- 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.