haskellhdbc

Haskell HDBC library: Is there an elegant way to convert between [SqlValue] and records?


Since I can't map over a record, the best method I have found is to just use brute force:

data Item = Item {
  vendor :: Int,
  lotNumber :: Int,
  description :: String,
  reserve :: Maybe Double,
  preSaleBids :: Maybe Double,
  salePrice :: Maybe Double,
  purchaser :: Maybe Int,
  saleID :: Maybe Int
}

hsToDb :: Item -> [SqlValue]
hsToDb (Item a b c d e f g h) = [toSql a, toSql b, toSql c, toSql d, toSql e, toSql f, toSql g, toSql h]

dbToHs :: [SqlValue] -> Item
dbToHs [a,b,c,d,e,f,g,h] = Item (fromSql a) (fromSql b) (fromSql c) (fromSql d) (fromSql e) (fromSql f) (fromSql g) (fromSql h)

This code looks ugly and also requires updating if I change the length of my Item record so I was wondering whether there's a clever way of generalizing this idea.


Solution

  • Depending on what library you're using the type (Int, Int, String, ...) may already have a (as in e.g. sqlite-simple) FromRow instance. If you need Item to be a new type you can use the GeneralizedNewtypeDeriving extension to get that instance for free. Barring this you could of course define your own type class/helper method to make it more DRY.