- -- | Takes a 'Doctype', 'XmlTree' pair and uses the 'Doctype' to
- -- determine which function to call on the 'XmlTree'.
- import_with_dtd :: (String, XmlTree) -> IO (Maybe Int)
- import_with_dtd (dtd,xml)
- | dtd == "injuriesxml.dtd" = import_injuries xml
- | dtd == "Injuries_Detail_XML.dtd" = import_injuries_detail xml
- | otherwise = do
- report_info $ "Unrecognized DTD in " ++ path ++ ": " ++ dtd ++ "."
- return Nothing
+ -- | Takes a ('DtdName', 'XmlTree') pair and uses the 'DtdName'
+ -- to determine which function to call on the 'XmlTree'.
+ import_with_dtd :: (DtdName, XmlTree) -> IO ImportResult
+ import_with_dtd (DtdName dtd,xml)
+ -- We special-case the heartbeat so it doesn't have to run in
+ -- the database monad.
+ | dtd == Heartbeat.dtd = Heartbeat.verify xml
+ | otherwise =
+ -- We need NoMonomorphismRestriction here.
+ if backend cfg == Postgres
+ then withPostgresqlConn cs $ runDbConn importer
+ else withSqliteConn cs $ runDbConn importer
+ where
+ -- | Pull the real connection String out of the configuration.
+ --
+ cs :: String
+ cs = get_connection_string $ connection_string cfg
+
+ -- | Convenience; we use this everywhere below in 'importer'.
+ --
+ migrate_and_import m = dbmigrate m >> dbimport m
+
+ -- | The error message we return if unpickling fails.
+ --
+ errmsg = "Could not unpickle " ++ dtd ++ "."
+
+ -- | Try to migrate and import using the given pickler @f@;
+ -- if it works, return the result. Otherwise, return an
+ -- 'ImportFailed' along with our error message.
+ --
+ go f = maybe
+ (return $ ImportFailed errmsg)
+ migrate_and_import
+ (unpickleDoc f xml)
+
+ importer
+ | dtd == AutoRacingResults.dtd =
+ go AutoRacingResults.pickle_message
+
+ | dtd == AutoRacingSchedule.dtd =
+ go AutoRacingSchedule.pickle_message
+
+ -- GameInfo and SportInfo appear last in the guards
+ | dtd == Injuries.dtd = go Injuries.pickle_message
+
+ | dtd == InjuriesDetail.dtd = go InjuriesDetail.pickle_message
+
+ | dtd == JFile.dtd = go JFile.pickle_message
+
+ | dtd == News.dtd =
+ -- Some of the newsxml docs are busted in predictable ways.
+ -- We want them to "succeed" so that they're deleted.
+ -- We already know we can't parse them.
+ if News.has_only_single_sms xml
+ then go News.pickle_message
+ else do
+ let msg = "Unsupported newsxml.dtd with multiple SMS " ++
+ "(" ++ path ++ ")"
+ return $ ImportUnsupported msg
+ | dtd == Odds.dtd = go Odds.pickle_message
+
+ | dtd == ScheduleChanges.dtd = go ScheduleChanges.pickle_message