+ "WorldBaseballPreviewXML.dtd" -- missing DTD
+ ]
+
+
+-- | XML representation of a GameInfo \<message\>.
+--
+data Message =
+ Message {
+ xml_dtd :: String,
+ xml_xml_file_id :: Int,
+ xml_time_stamp :: UTCTime,
+ xml_xml :: String }
+ deriving (Eq, Show)
+
+
+-- | Attempt to parse a 'Message' from an 'XmlTree'. If we cannot,
+-- we fail with an error message.
+--
+parse_xml :: String -> XmlTree -> Either String Message
+parse_xml dtdname xmltree = do
+ xmlfid <- parse_xmlfid xmltree
+ timestamp <- parse_xml_time_stamp xmltree
+ message <- parse_message xmltree
+ return $ Message dtdname (fromInteger xmlfid) timestamp (xshow [message])
+
+
+-- | Database representation of a 'Message'.
+--
+data GameInfo =
+ GameInfo {
+ db_dtd :: String,
+ db_xml_file_id :: Int,
+ db_time_stamp :: UTCTime,
+ db_xml :: String }
+
+
+instance ToDb Message where
+ -- | The database analogue of a 'Message' is an 'GameInfo'.
+ type Db Message = GameInfo
+
+instance FromXml Message where
+ -- | The XML to DB conversion is trivial here.
+ --
+ from_xml Message{..} = GameInfo {
+ db_dtd = xml_dtd,
+ db_xml_file_id = xml_xml_file_id,
+ db_time_stamp = xml_time_stamp,
+ db_xml = xml_xml }
+
+
+-- | This allows us to insert the XML representation 'Message'
+-- directly.
+--
+instance XmlImport Message
+
+
+--
+-- Database code
+--
+
+instance DbImport Message where
+ dbmigrate _ =
+ run_dbmigrate $ migrate (undefined :: GameInfo)
+
+ -- | We import a 'Message' by inserting the whole thing at
+ -- once. Nothing fancy going on here.
+ dbimport msg = do
+ insert_xml_ msg
+ return ImportSucceeded
+
+mkPersist tsn_codegen_config [groundhog|
+- entity: GameInfo
+ constructors:
+ - name: GameInfo
+ uniques:
+ - name: unique_game_info
+ type: constraint
+ # Prevent multiple imports of the same message.
+ fields: [db_xml_file_id]
+|]