- -> IO ()
-import_generic dummy g xml =
- withSqliteConn "foo.sqlite3" $ runDbConn $ do
- runMigration defaultMigrationLogger $ do
- migrate dummy
- let root_element = unpickleDoc xpickle xml
- case root_element of
- Nothing -> let msg = "Could not unpickle document in import_generic."
- in liftIO $ report_error msg
- Just elt -> mapM_ (\l -> insert l) (g elt)
+ -> IO (Maybe Int) -- ^ Return the number of records inserted.
+import_generic dummy g cfg xml
+ | backend cfg == Postgres = withPostgresqlConn cs go
+ | otherwise = withSqliteConn cs go
+ where
+ -- | Pull the real connection String out of the configuration.
+ cs :: String
+ cs = get_connection_string $ connection_string cfg
+
+ -- Needs NoMonomorphismRestriction to be allowed to return
+ -- different types in the two cases above.
+ go = runDbConn $ do
+ runMigration defaultMigrationLogger $ migrate dummy
+ let root_element = unpickleDoc xpickle xml
+ case root_element of
+ Nothing -> do
+ let msg = "Could not unpickle document in import_generic."
+ liftIO $ report_error msg
+ return Nothing
+ Just elt -> do
+ ids <- mapM insert (g elt)
+ return $ Just (length ids)
+
+