]> gitweb.michael.orlitzky.com - dead/htsn-import.git/blobdiff - src/TSN/XML/AutoRacingSchedule.hs
Insignificant comment fix.
[dead/htsn-import.git] / src / TSN / XML / AutoRacingSchedule.hs
index 62202ddf88dcf0279423ce70fd52fd55351a4aa1..212eee98a545f5cd106a64c900a136324727d570 100644 (file)
@@ -3,7 +3,6 @@
 {-# LANGUAGE GADTs #-}
 {-# LANGUAGE QuasiQuotes #-}
 {-# LANGUAGE RecordWildCards #-}
-{-# LANGUAGE StandaloneDeriving #-}
 {-# LANGUAGE TemplateHaskell #-}
 {-# LANGUAGE TypeFamilies #-}
 
@@ -13,6 +12,7 @@
 --   containing \<RaceResultsListing\>s.
 --
 module TSN.XML.AutoRacingSchedule (
+  dtd,
   pickle_message,
   -- * Tests
   auto_racing_schedule_tests,
@@ -28,7 +28,7 @@ import Data.Time ( UTCTime(..) )
 import Data.Tuple.Curry ( uncurryN )
 import Database.Groundhog (
   countAll,
-  executeRaw,
+  deleteAll,
   migrate,
   runMigration,
   silentMigrationLogger )
@@ -56,7 +56,7 @@ import Text.XML.HXT.Core (
 import TSN.Codegen (
   tsn_codegen_config )
 import TSN.DbImport ( DbImport(..), ImportResult(..), run_dbmigrate )
-import TSN.Picklers ( xp_date, xp_tba_time, xp_time_stamp )
+import TSN.Picklers ( xp_date_padded, xp_tba_time, xp_time_stamp )
 import TSN.XmlImport ( XmlImport(..), XmlImportFk(..) )
 import Xml (
   FromXml(..),
@@ -67,6 +67,11 @@ import Xml (
   unsafe_unpickle )
 
 
+-- | The DTD to which this module corresponds. Used to invoke dbimport.
+--
+dtd :: String
+dtd = "Auto_Racing_Schedule_XML.dtd"
+
 --
 -- DB/XML data types
 --
@@ -101,10 +106,20 @@ data Message =
     xml_time_stamp :: UTCTime }
   deriving (Eq, Show)
 
+
 instance ToDb Message where
+  -- | The database analogue of a 'Message' is a 'AutoRacingSchedule'.
+  --
   type Db Message = AutoRacingSchedule
 
+
+-- | The 'FromXml' instance for 'Message' is required for the
+--   'XmlImport' instance.
+--
 instance FromXml Message where
+  -- | To convert a 'Message' to an 'AutoRacingSchedule', we just drop
+  --   the 'xml_listings'.
+  --
   from_xml Message{..} =
     AutoRacingSchedule {
       db_xml_file_id = xml_xml_file_id,
@@ -115,15 +130,20 @@ instance FromXml Message where
       db_complete_through = xml_complete_through,
       db_time_stamp = xml_time_stamp }
 
+
+-- | This allows us to insert the XML representation 'Message'
+--   directly.
+--
 instance XmlImport Message
 
 
 -- * AutoRacingScheduleListing/AutoRacingScheduleListingXml
 
 -- | Database representation of a \<Listing\> contained within a
---   \<Message\>. We combine the race date/time into a single
+--   \<message\>. We combine the race date/time into a single
 --   race_time, drop the race results list, and add a foreign key to
 --   our parent.
+--
 data AutoRacingScheduleListing =
   AutoRacingScheduleListing {
     db_auto_racing_schedules_id :: DefaultKey AutoRacingSchedule,
@@ -137,6 +157,7 @@ data AutoRacingScheduleListing =
     db_track_length :: String -- ^ Sometimes the word "miles" shows up.
   }
 
+
 -- | XML representation of a \<Listing\> contained within a
 --   \<message\>.
 --
@@ -150,23 +171,38 @@ data AutoRacingScheduleListingXml =
     xml_location :: String,
     xml_tv_listing :: Maybe String,
     xml_laps :: Int,
-    xml_track_length :: String, -- ^ Sometimes the word \"miles\" shows up.
+    xml_track_length :: String, -- ^ Sometimes the word \"miles\" shows up,
+                                --   so we can't do the right thing and use
+                                --   a 'Double'.
     xml_race_results :: [AutoRacingScheduleListingRaceResult] }
   deriving (Eq, Show)
 
+
 -- | Pseudo-accessor to get the race result listings out of a
---   'AutoRacingScheduleListingXml'.
+--   'AutoRacingScheduleListingXml'. A poor man's lens.
+--
 result_listings :: AutoRacingScheduleListingXml
                 -> [AutoRacingScheduleListingRaceResultRaceResultListingXml]
 result_listings = (concatMap xml_race_result_listing) . xml_race_results
 
 
 instance ToDb AutoRacingScheduleListingXml where
+  -- | The database analogue of an 'AutoRacingScheduleListingXml' is
+  --   an 'AutoRacingScheduleListing'.
+  --
   type Db AutoRacingScheduleListingXml = AutoRacingScheduleListing
 
 instance FromXmlFk AutoRacingScheduleListingXml where
+  -- | Each 'AutoRacingScheduleListingXml' is contained in (i.e. has a
+  --   foreign key to) a 'AutoRacingSchedule'.
+  --
   type Parent AutoRacingScheduleListingXml = AutoRacingSchedule
 
+  -- | To convert an 'AutoRacingScheduleListingXml' to an
+  --   'AutoRacingScheduleListing', we add the foreign key and drop
+  --   the 'xml_race_results'. We also mash the date/time together
+  --   into one field.
+  --
   from_xml_fk fk AutoRacingScheduleListingXml{..} =
     AutoRacingScheduleListing {
       db_auto_racing_schedules_id = fk,
@@ -179,12 +215,21 @@ instance FromXmlFk AutoRacingScheduleListingXml where
       db_laps = xml_laps,
       db_track_length = xml_track_length }
     where
-      -- Take the day part from one, the time from the other.
+      -- | Make the database \"race time\" from the XML
+      --   date/time. Simply take the day part from one and the time
+      --   from the other.
+      --
       make_race_time d Nothing = d
       make_race_time d (Just t) = UTCTime (utctDay d) (utctDayTime t)
 
+
+-- | This allows us to insert the XML representation
+--   'AutoRacingScheduleListingXml' directly.
+--
 instance XmlImportFk AutoRacingScheduleListingXml
 
+
+
 -- * AutoRacingScheduleListingRaceResult
 
 -- | The XML representation of \<message\> -> \<Listing\> ->
@@ -198,9 +243,15 @@ newtype AutoRacingScheduleListingRaceResult =
       [AutoRacingScheduleListingRaceResultRaceResultListingXml] }
   deriving (Eq, Show)
 
--- * AutoRacingScheduleListingRaceResultRaceResultListing /
---   AutoRacingScheduleListingRaceResultRaceResultListingXml
 
+-- * AutoRacingScheduleListingRaceResultRaceResultListing / AutoRacingScheduleListingRaceResultRaceResultListingXml
+--
+--   Sorry about the names yo.
+--
+
+-- | Database representation of \<RaceResultListing\> within
+--   \<RaceResults\> within \<Listing\> within... \<message\>!
+--
 data AutoRacingScheduleListingRaceResultRaceResultListing =
   AutoRacingScheduleListingRaceResultRaceResultListing {
     db_auto_racing_schedules_listings_id ::
@@ -209,29 +260,49 @@ data AutoRacingScheduleListingRaceResultRaceResultListing =
     db_driver_id :: Int,
     db_name :: String,
     db_leading_laps :: Int,
-    db_listing_laps :: Int, -- Avoid clash with race's "laps" field.
-    db_earnings :: String, -- Should be an int, but they use commas.
+    db_listing_laps :: Int, -- ^ Avoid clash with race's \"laps\" field.
+    db_earnings :: String,  -- ^ This should be an Int, but can have commas.
     db_status :: String }
 
+
+-- | XML Representation of an
+--   'AutoRacingScheduleListingRaceResultRaceResultListing'.
+--
 data AutoRacingScheduleListingRaceResultRaceResultListingXml =
   AutoRacingScheduleListingRaceResultRaceResultListingXml {
     xml_finish_position :: Int,
     xml_driver_id :: Int,
     xml_name :: String,
     xml_leading_laps :: Int,
-    xml_listing_laps :: Int, -- Avoid clash with race's "laps" field.
-    xml_earnings :: String, -- Should be an int, but they use commas.
+    xml_listing_laps :: Int, -- ^ Avoid clash with race's \"laps\" field.
+    xml_earnings :: String,  -- ^ Should be an 'Int', but can have commas.
     xml_status :: String }
   deriving (Eq, Show)
 
+
 instance ToDb AutoRacingScheduleListingRaceResultRaceResultListingXml where
+  -- | The database representation of an
+  --   'AutoRacingScheduleListingRaceResultRaceResultListingXml' is an
+  --   'AutoRacingScheduleListingRaceResultRaceResultListing'.
+  --
   type Db AutoRacingScheduleListingRaceResultRaceResultListingXml =
     AutoRacingScheduleListingRaceResultRaceResultListing
 
+
 instance FromXmlFk AutoRacingScheduleListingRaceResultRaceResultListingXml where
+  -- | Each 'AutoRacingScheduleListingRaceResultRaceResultListingXml'
+  --   is contained in (i.e. has a foreign key to) an
+  --   'AutoRacingScheduleListing'. We skip the intermediate
+  --   \<RaceResults\>.
+  --
   type Parent AutoRacingScheduleListingRaceResultRaceResultListingXml =
     AutoRacingScheduleListing
 
+  -- | To convert an
+  --   'AutoRacingScheduleListingRaceResultRaceResultListingXml' to an
+  --   'AutoRacingScheduleListingRaceResultRaceResultListing', we just
+  --   add the foreign key to the parent 'AutoRacingScheduleListing'.
+  --
   from_xml_fk fk AutoRacingScheduleListingRaceResultRaceResultListingXml{..} =
     AutoRacingScheduleListingRaceResultRaceResultListing {
       db_auto_racing_schedules_listings_id = fk,
@@ -243,8 +314,14 @@ instance FromXmlFk AutoRacingScheduleListingRaceResultRaceResultListingXml where
       db_earnings = xml_earnings,
       db_status = xml_earnings }
 
+
+-- | This allows us to insert the XML representation
+--   'AutoRacingScheduleListingRaceResultRaceResultListingXml'
+--   directly.
+--
 instance XmlImportFk AutoRacingScheduleListingRaceResultRaceResultListingXml
 
+
 ---
 --- Database stuff.
 ---
@@ -257,6 +334,10 @@ instance DbImport Message where
       migrate (undefined
                  :: AutoRacingScheduleListingRaceResultRaceResultListing)
 
+
+  -- | We insert the message, then use its ID to insert the listings,
+  --   using their IDs to insert the race result listings.
+  --
   dbimport m = do
     msg_id <- insert_xml m
 
@@ -330,12 +411,14 @@ pickle_message =
                   xml_time_stamp m)
 
 
+-- | Convert an 'AutoRacingScheduleListingXml' to/from XML.
+--
 pickle_listing :: PU AutoRacingScheduleListingXml
 pickle_listing =
   xpElem "Listing" $
     xpWrap (from_tuple, to_tuple) $
       xp10Tuple (xpElem "RaceID" xpInt)
-                (xpElem "Race_Date" xp_date)
+                (xpElem "Race_Date" xp_date_padded)
                 (xpElem "Race_Time" xp_tba_time)
                 (xpElem "RaceName" xpText)
                 (xpElem "TrackName" xpText)
@@ -357,6 +440,9 @@ pickle_listing =
                   xml_track_length m,
                   xml_race_results m)
 
+
+-- | Convert an 'AutoRacingScheduleListingRaceResult' to/from XML.
+--
 pickle_race_results :: PU AutoRacingScheduleListingRaceResult
 pickle_race_results =
   xpElem "RaceResults" $
@@ -366,6 +452,11 @@ pickle_race_results =
     to_result = AutoRacingScheduleListingRaceResult
     from_result = xml_race_result_listing
 
+
+-- | Convert an
+--   'AutoRacingScheduleListingRaceResultRaceResultListingXml' to/from
+--   XML.
+--
 pickle_race_results_listing ::
   PU AutoRacingScheduleListingRaceResultRaceResultListingXml
 pickle_race_results_listing =
@@ -462,8 +553,7 @@ test_on_delete_cascade = testGroup "cascading delete tests"
                     migrate b
                     migrate c
                   _ <- dbimport sched
-                  -- No idea how 'delete' works, so do this instead.
-                  executeRaw False "DELETE FROM auto_racing_schedules;" []
+                  deleteAll a
                   count_a <- countAll a
                   count_b <- countAll b
                   count_c <- countAll c