module Database.SQLite.Simple.FromRow
( FromRow(..)
, RowParser
, field
, fieldWith
, numFieldsRemaining
) where
import Control.Applicative (Applicative(..), (<$>))
import Control.Exception (SomeException(..))
import Control.Monad (replicateM)
import Control.Monad.Trans.State.Strict
import Control.Monad.Trans.Reader
import Control.Monad.Trans.Class
import Data.ByteString (ByteString)
import qualified Data.ByteString.Char8 as B
import Database.SQLite.Simple.FromField
import Database.SQLite.Simple.Internal
import Database.SQLite.Simple.Ok
import Database.SQLite.Simple.Types
import qualified Database.SQLite3 as Base
class FromRow a where
fromRow :: RowParser a
fieldWith :: FieldParser a -> RowParser a
fieldWith fieldP = RP $ do
Row{..} <- ask
column <- lift get
lift (put (column + 1))
let ncols = length rowresult
if column >= ncols
then do
let vals = map (\c -> (gettypename (rowresult !! c)
, ellipsis (rowresult !! c)))
[0..ncols1]
convertError = ConversionFailed
(show ncols ++ " values: " ++ show vals)
("at least " ++ show (column + 1)
++ " slots in target type")
"mismatch between number of columns to \
\convert and number in target type"
lift (lift (Errors [SomeException convertError]))
else do
let r = rowresult !! column
field = Field r column
lift (lift (fieldP field))
field :: FromField a => RowParser a
field = fieldWith fromField
ellipsis :: Base.SQLData -> ByteString
ellipsis sql
| B.length bs > 15 = B.take 10 bs `B.append` "[...]"
| otherwise = bs
where
bs = B.pack $ show sql
numFieldsRemaining :: RowParser Int
numFieldsRemaining = RP $ do
Row{..} <- ask
column <- lift get
return $! length rowresult column
instance (FromField a) => FromRow (Only a) where
fromRow = Only <$> field
instance (FromField a, FromField b) => FromRow (a,b) where
fromRow = (,) <$> field <*> field
instance (FromField a, FromField b, FromField c) => FromRow (a,b,c) where
fromRow = (,,) <$> field <*> field <*> field
instance (FromField a, FromField b, FromField c, FromField d) =>
FromRow (a,b,c,d) where
fromRow = (,,,) <$> field <*> field <*> field <*> field
instance (FromField a, FromField b, FromField c, FromField d, FromField e) =>
FromRow (a,b,c,d,e) where
fromRow = (,,,,) <$> field <*> field <*> field <*> field <*> field
instance (FromField a, FromField b, FromField c, FromField d, FromField e,
FromField f) =>
FromRow (a,b,c,d,e,f) where
fromRow = (,,,,,) <$> field <*> field <*> field <*> field <*> field
<*> field
instance (FromField a, FromField b, FromField c, FromField d, FromField e,
FromField f, FromField g) =>
FromRow (a,b,c,d,e,f,g) where
fromRow = (,,,,,,) <$> field <*> field <*> field <*> field <*> field
<*> field <*> field
instance (FromField a, FromField b, FromField c, FromField d, FromField e,
FromField f, FromField g, FromField h) =>
FromRow (a,b,c,d,e,f,g,h) where
fromRow = (,,,,,,,) <$> field <*> field <*> field <*> field <*> field
<*> field <*> field <*> field
instance (FromField a, FromField b, FromField c, FromField d, FromField e,
FromField f, FromField g, FromField h, FromField i) =>
FromRow (a,b,c,d,e,f,g,h,i) where
fromRow = (,,,,,,,,) <$> field <*> field <*> field <*> field <*> field
<*> field <*> field <*> field <*> field
instance (FromField a, FromField b, FromField c, FromField d, FromField e,
FromField f, FromField g, FromField h, FromField i, FromField j) =>
FromRow (a,b,c,d,e,f,g,h,i,j) where
fromRow = (,,,,,,,,,) <$> field <*> field <*> field <*> field <*> field
<*> field <*> field <*> field <*> field <*> field
instance FromField a => FromRow [a] where
fromRow = do
n <- numFieldsRemaining
replicateM n field
instance (FromRow a, FromRow b) => FromRow (a :. b) where
fromRow = (:.) <$> fromRow <*> fromRow