1 -- | Definition of the DbImport typeclass.
5 import Control.Monad.IO.Class ( MonadIO )
6 import Database.Groundhog (
7 defaultMigrationLogger,
11 import Database.Groundhog.Core ( PersistBackend, PersistEntity )
12 import Text.XML.HXT.Core (
19 -- | The type that will be returned from every file import attempt. If
20 -- there was an error, its description will be wrapped in an Err. If
21 -- we successfully imported records, the number of records imported
22 -- will be wrapped in a Succ.
24 -- Anything else will be wrapped in a "Info" constructor;
25 -- i.e. somewhere between success and failure. This is like an
26 -- 'Either' with three choices. A "Info" return value means that
27 -- the XML document *was* processed, so it should be removed.
29 data ImportResult = Err String | Info String | Succ Int
31 -- | Instances of this type know how to insert themselves into a
32 -- Groundhog database.
33 class DbImport a where
34 dbimport :: (MonadIO m, PersistBackend m)
40 -- | We put the 'Configuration' and 'XmlTree' arguments last so that
41 -- it's easy to eta reduce all of the import_foo functions that call
44 import_generic :: (XmlPickler a, MonadIO m, PersistEntity b, PersistBackend m)
45 => (a -> [b]) -- ^ listings getter
46 -> b -- ^ Dummy Listing instance needed for 'migrate'
48 -> m ImportResult -- ^ Return the number of records inserted.
49 import_generic g dummy xml = do
50 runMigration defaultMigrationLogger $ migrate dummy
51 let root_element = unpickleDoc xpickle xml
53 Nothing -> return $ Err "Could not unpickle document in import_generic."
55 ids <- mapM insert (g elt)
56 return $ Succ (length ids)