mysql-simple / Database / MySQL / Simple / QueryResults.hs

-- |
-- Module:      Database.MySQL.Simpe.QueryResults
-- Copyright:   (c) 2011 MailRank, Inc.
-- License:     BSD3
-- Maintainer:  Bryan O'Sullivan <bos@mailrank.com>
-- Stability:   experimental
-- Portability: portable
--
-- The 'QueryResults' typeclass, for converting a row of results
-- returned by a SQL query into a more useful Haskell representation.

module Database.MySQL.Simple.QueryResults
    (
      QueryResults(..)
    ) where

import Control.Exception (throw)
import Data.ByteString (ByteString)
import Database.MySQL.Base.Types (Field)
import Database.MySQL.Simple.Result (ResultError(..), Result(..))
import Database.MySQL.Simple.Types (Only(..))

-- | A collection type that can be converted from a list of strings.
class QueryResults a where
    convertResults :: [Field] -> [Maybe ByteString] -> a
    -- ^ Convert values from a row into a Haskell collection.
    --
    -- This function will throw an exception if conversion of any
    -- element of the collection fails.

instance (Result a) => QueryResults (Only a) where
    convertResults [fa] [va] = Only (convert fa va)
    convertResults fs vs  = convError fs vs

instance (Result a, Result b) => QueryResults (a,b) where
    convertResults [fa,fb] [va,vb] = (convert fa va, convert fb vb)
    convertResults fs vs  = convError fs vs

instance (Result a, Result b, Result c) => QueryResults (a,b,c) where
    convertResults [fa,fb,fc] [va,vb,vc] =
        (convert fa va, convert fb vb, convert fc vc)
    convertResults fs vs  = convError fs vs

instance (Result a, Result b, Result c, Result d) =>
    QueryResults (a,b,c,d) where
    convertResults [fa,fb,fc,fd] [va,vb,vc,vd] =
        (convert fa va, convert fb vb, convert fc vc, convert fd vd)
    convertResults fs vs  = convError fs vs

instance (Result a, Result b, Result c, Result d, Result e) =>
    QueryResults (a,b,c,d,e) where
    convertResults [fa,fb,fc,fd,fe] [va,vb,vc,vd,ve] =
        (convert fa va, convert fb vb, convert fc vc, convert fd vd,
         convert fe ve)
    convertResults fs vs  = convError fs vs

instance (Result a, Result b, Result c, Result d, Result e, Result f) =>
    QueryResults (a,b,c,d,e,f) where
    convertResults [fa,fb,fc,fd,fe,ff] [va,vb,vc,vd,ve,vf] =
        (convert fa va, convert fb vb, convert fc vc, convert fd vd,
         convert fe ve, convert ff vf)
    convertResults fs vs  = convError fs vs

instance (Result a, Result b, Result c, Result d, Result e, Result f,
          Result g) =>
    QueryResults (a,b,c,d,e,f,g) where
    convertResults [fa,fb,fc,fd,fe,ff,fg] [va,vb,vc,vd,ve,vf,vg] =
        (convert fa va, convert fb vb, convert fc vc, convert fd vd,
         convert fe ve, convert ff vf, convert fg vg)
    convertResults fs vs  = convError fs vs

instance (Result a, Result b, Result c, Result d, Result e, Result f,
          Result g, Result h) =>
    QueryResults (a,b,c,d,e,f,g,h) where
    convertResults [fa,fb,fc,fd,fe,ff,fg,fh] [va,vb,vc,vd,ve,vf,vg,vh] =
        (convert fa va, convert fb vb, convert fc vc, convert fd vd,
         convert fe ve, convert ff vf, convert fg vg, convert fh vh)
    convertResults fs vs  = convError fs vs

instance (Result a, Result b, Result c, Result d, Result e, Result f,
          Result g, Result h, Result i) =>
    QueryResults (a,b,c,d,e,f,g,h,i) where
    convertResults [fa,fb,fc,fd,fe,ff,fg,fh,fi] [va,vb,vc,vd,ve,vf,vg,vh,vi] =
        (convert fa va, convert fb vb, convert fc vc, convert fd vd,
         convert fe ve, convert ff vf, convert fg vg, convert fh vh,
         convert fi vi)
    convertResults fs vs  = convError fs vs

instance (Result a, Result b, Result c, Result d, Result e, Result f,
          Result g, Result h, Result i, Result j) =>
    QueryResults (a,b,c,d,e,f,g,h,i,j) where
    convertResults [fa,fb,fc,fd,fe,ff,fg,fh,fi,fj]
                   [va,vb,vc,vd,ve,vf,vg,vh,vi,vj] =
        (convert fa va, convert fb vb, convert fc vc, convert fd vd,
         convert fe ve, convert ff vf, convert fg vg, convert fh vh,
         convert fi vi, convert fj vj)
    convertResults fs vs  = convError fs vs

convError :: [Field] -> [Maybe ByteString] -> a
convError fs vs = throw $ ConversionFailed
                  (show (length fs) ++ " columns left in result")
                  (show (length vs) ++ " values left in row")
                  "mismatch between number of columns to convert"
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.