diff --git a/Database/SQLite/Simple/FromField.hs b/Database/SQLite/Simple/FromField.hs index e52dc1b..e13e04e 100644 --- a/Database/SQLite/Simple/FromField.hs +++ b/Database/SQLite/Simple/FromField.hs @@ -34,7 +34,7 @@ module Database.SQLite.Simple.FromField , returnError ) where -import Control.Applicative (Applicative, (<$>), pure) +import Control.Applicative (Applicative, (<$>), (<|>), pure) import Control.Exception (SomeException(..), Exception) import Data.ByteString (ByteString) import qualified Data.ByteString.Char8 as B @@ -102,6 +102,9 @@ instance (FromField a) => FromField (Maybe a) where fromField (Field SQLNull _) = pure Nothing fromField f = Just <$> fromField f +instance (FromField a, FromField b) => FromField (Either a b) where + fromField f = (Left <$> fromField f) <|> (Right <$> fromField f) + instance FromField Null where fromField (Field SQLNull _) = pure Null fromField f = returnError ConversionFailed f "data is not null" diff --git a/Database/SQLite/Simple/ToField.hs b/Database/SQLite/Simple/ToField.hs index 194f383..66ce3a5 100644 --- a/Database/SQLite/Simple/ToField.hs +++ b/Database/SQLite/Simple/ToField.hs @@ -47,6 +47,11 @@ instance (ToField a) => ToField (Maybe a) where toField (Just a) = toField a {-# INLINE toField #-} +instance (ToField a, ToField b) => ToField (Either a b) where + toField (Left a) = toField a + toField (Right b) = toField b + {-# INLINE toField #-} + instance ToField Null where toField _ = Base.SQLNull {-# INLINE toField #-}