I've a little application, backed by a database (SQLite, but it's not really relevant to the question). I've defined some types like:
data Whatever = Whatever Int Int String String
data ImportantStuff = ImportantStuff { id :: Int, count :: Int, name :: String, description :: String }
The types are mapped to tables in the DB. When I read the data, I end up writing functions like this one:
whateverFromDB :: [SqlValue] -> Whatever
whateverFromDB (a:b:c:d:_) = Whatever (fromSql a) (fromSql b) (fromSql c) (fromSql d)
(I'm ommiting handling errors for the sake of clarity.)
Writing functions like these is really annoing and feels like creating a lot of boilerplate. Is there a more idiomatic way to convert a group of SqlValues into a Haskell data?
There doesn't seem to be any standard way in the
HDBC
library for this. If you're feeling particularly keen, you can roll your own machinery withGHC.Generics
for this, though the cure may be worse than the disease!I also added the reverse conversion, but you can leave that out/split the classes if you want: