A collection type that can be converted from a list of strings.
Instances should use the
convert method of the
Result
class to perform conversion of each element of the collection.
This example instance demonstrates how to convert a two-column row
into a Haskell pair. Each field in the metadata is paired up with each
value from the row, and the two are passed to
convert.
instance (Result a, Result b) => QueryResults (a,b) where
convertResults [fa,fb] [va,vb] = (a,b)
where !a = convert fa va
!b = convert fb vb
convertResults fs vs = convertError fs vs 2
Notice that this instance evaluates each element to WHNF before
constructing the pair. By doing this, we guarantee two important
properties:
- Keep resource usage under control by preventing the construction
of potentially long-lived thunks.
- Ensure that any ResultError that might arise is thrown
immediately, rather than some place later in application code that
cannot handle it.
You can also declare Haskell types of your own to be instances of
QueryResults.
data User = User { firstName :: String, lastName :: String }
instance QueryResults User where
convertResults [fa,fb] [va,vb] = User $ a * b
where !a = convert fa va
!b = convert fb vb
convertResults fs vs = convertError fs vs 2