1 {-# LANGUAGE FlexibleContexts #-}
8 import Database.Groundhog (
12 import Database.Groundhog.Core ( PersistBackend, PersistEntity )
14 import Xml ( FromXml(..) )
17 class (FromXml a, PersistEntity (Db a)) => XmlImport a where
18 -- | This is similar to the signature for Groundhog's 'insert'
19 -- function, except the 'AutoKey' we return is for our 'Db'
21 insert_xml :: (PersistBackend m) => a -> m (AutoKey (Db a))
22 insert_xml x = insert (from_xml x)
24 -- | Same rationale as 'insert_xml', except it uses 'insertByAll'.
25 insertByAll_xml :: (PersistBackend m)
27 -> m ( Either (AutoKey (Db a)) (AutoKey (Db a)) )
28 insertByAll_xml x = insertByAll (from_xml x)
31 -- | Try to insert the given object and get its primary key
32 -- back. Or, if there's a unique constraint violation, get the
33 -- primary key of the unique thing already present.
35 -- Note: we can switch to using fmap here as soon as Functor is a
36 -- superclass of Monad (PersistBackend is a Monad).
38 insert_xml_or_select :: (PersistBackend m)
41 insert_xml_or_select x = do
42 tmp <- insertByAll_xml x
43 return $ (either id id) tmp