+
{-# LANGUAGE DeriveDataTypeable #-}
+{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE QuasiQuotes #-}
import Control.Monad ( forM_ )
import Data.Time ( UTCTime(..) )
import Data.Tuple.Curry ( uncurryN )
+import qualified Data.Vector.HFixed as H ( HVector, cons, convert )
import Database.Groundhog (
countAll,
deleteAll,
- migrate,
- runMigration,
- silentMigrationLogger )
+ migrate )
import Database.Groundhog.Core ( DefaultKey )
-import Database.Groundhog.Generic ( runDbConn )
+import Database.Groundhog.Generic ( runDbConn, runMigrationSilent )
import Database.Groundhog.Sqlite ( withSqliteConn )
import Database.Groundhog.TH (
groundhog,
mkPersist )
+import qualified GHC.Generics as GHC ( Generic )
import Test.Tasty ( TestTree, testGroup )
import Test.Tasty.HUnit ( (@?=), testCase )
import Text.XML.HXT.Core (
xml_complete_through :: String,
xml_listings :: [AutoRacingScheduleListingXml],
xml_time_stamp :: UTCTime }
- deriving (Eq, Show)
+ deriving (Eq, GHC.Generic, Show)
+
+-- | For 'H.convert'.
+--
+instance H.HVector Message
instance ToDb Message where
-- so we can't do the right thing and use
-- a 'Double'.
xml_race_results :: [AutoRacingScheduleListingRaceResult] }
- deriving (Eq, Show)
+ deriving (Eq, GHC.Generic, Show)
+
+-- | For 'H.convert'.
+--
+instance H.HVector AutoRacingScheduleListingXml
-- | Pseudo-accessor to get the race result listings out of a
-- | Database representation of \<RaceResultListing\> within
-- \<RaceResults\> within \<Listing\> within... \<message\>!
+-- The leading underscores prevent unused field warnings.
--
data AutoRacingScheduleListingRaceResultRaceResultListing =
AutoRacingScheduleListingRaceResultRaceResultListing {
- db_auto_racing_schedules_listings_id ::
+ _db_auto_racing_schedules_listings_id ::
DefaultKey AutoRacingScheduleListing,
- db_finish_position :: Int,
- 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, -- ^ This should be an Int, but can have commas.
- db_status :: String }
+ _db_finish_position :: Int,
+ _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, -- ^ This should be an Int, but can have commas.
+ _db_status :: Maybe String -- ^ They can be empty
+ }
+ deriving ( GHC.Generic )
+
+-- | For 'H.cons'.
+--
+instance H.HVector AutoRacingScheduleListingRaceResultRaceResultListing
-- | XML Representation of an
-- 'AutoRacingScheduleListingRaceResultRaceResultListing'.
+-- The leading underscores prevent unused field warnings.
--
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 can have commas.
- xml_status :: String }
- deriving (Eq, Show)
+ _xml_finish_position :: Int,
+ _xml_driver_id :: Int,
+ _xml_name :: String,
+ _xml_leading_laps :: Int,
+ _xml_listing_laps :: Int, -- ^ Avoids clash with race's \"laps\" field.
+ _xml_earnings :: String, -- ^ Should be an 'Int', but can have commas.
+ _xml_status :: Maybe String -- ^ They can be empty
+ }
+ deriving (Eq, GHC.Generic, Show)
+
+-- | For 'H.convert'.
+--
+instance H.HVector AutoRacingScheduleListingRaceResultRaceResultListingXml
instance ToDb AutoRacingScheduleListingRaceResultRaceResultListingXml where
-- 'AutoRacingScheduleListingRaceResultRaceResultListing', we just
-- add the foreign key to the parent 'AutoRacingScheduleListing'.
--
- from_xml_fk fk AutoRacingScheduleListingRaceResultRaceResultListingXml{..} =
- AutoRacingScheduleListingRaceResultRaceResultListing {
- db_auto_racing_schedules_listings_id = fk,
- db_finish_position = xml_finish_position,
- db_driver_id = xml_driver_id,
- db_name = xml_name,
- db_leading_laps = xml_leading_laps,
- db_listing_laps = xml_listing_laps,
- db_earnings = xml_earnings,
- db_status = xml_earnings }
+ from_xml_fk = H.cons
-- | This allows us to insert the XML representation
constructors:
- name: AutoRacingSchedule
uniques:
- - name: unique_auto_racing_schedule
+ - name: unique_auto_racing_schedules
type: constraint
# Prevent multiple imports of the same message.
fields: [db_xml_file_id]
constructors:
- name: AutoRacingScheduleListingRaceResultRaceResultListing
fields:
- - name: db_auto_racing_schedules_listings_id
+ - name: _db_auto_racing_schedules_listings_id
reference:
onDelete: cascade
|]
pickle_message :: PU Message
pickle_message =
xpElem "message" $
- xpWrap (from_tuple, to_tuple) $
+ xpWrap (from_tuple, H.convert) $
xp8Tuple (xpElem "XML_File_ID" xpInt)
(xpElem "heading" xpText)
(xpElem "category" xpText)
(xpElem "time_stamp" xp_time_stamp)
where
from_tuple = uncurryN Message
- to_tuple m = (xml_xml_file_id m,
- xml_heading m,
- xml_category m,
- xml_sport m,
- xml_title m,
- xml_complete_through m,
- xml_listings m,
- xml_time_stamp m)
-- | Convert an 'AutoRacingScheduleListingXml' to/from XML.
pickle_listing :: PU AutoRacingScheduleListingXml
pickle_listing =
xpElem "Listing" $
- xpWrap (from_tuple, to_tuple) $
+ xpWrap (from_tuple, H.convert) $
xp10Tuple (xpElem "RaceID" xpInt)
(xpElem "Race_Date" xp_date_padded)
(xpElem "Race_Time" xp_tba_time)
(xpList pickle_race_results)
where
from_tuple = uncurryN AutoRacingScheduleListingXml
- to_tuple m = (xml_race_id m,
- xml_race_date m,
- xml_race_time m,
- xml_race_name m,
- xml_track_name m,
- xml_location m,
- xml_tv_listing m,
- xml_laps m,
- xml_track_length m,
- xml_race_results m)
+
-- | Convert an 'AutoRacingScheduleListingRaceResult' to/from XML.
PU AutoRacingScheduleListingRaceResultRaceResultListingXml
pickle_race_results_listing =
xpElem "RaceResultsListing" $
- xpWrap (from_tuple, to_tuple) $
+ xpWrap (from_tuple, H.convert) $
xp7Tuple (xpElem "FinishPosition" xpInt)
(xpElem "DriverID" xpInt)
(xpElem "Name" xpText)
(xpElem "LeadingLaps" xpInt)
(xpElem "Laps" xpInt)
(xpElem "Earnings" xpText)
- (xpElem "Status" xpText)
+ (xpElem "Status" (xpOption xpText))
where
from_tuple =
uncurryN AutoRacingScheduleListingRaceResultRaceResultListingXml
- to_tuple m = (xml_finish_position m,
- xml_driver_id m,
- xml_name m,
- xml_leading_laps m,
- xml_listing_laps m,
- xml_earnings m,
- xml_status m)
-
--
-- Tasty Tests
let c = undefined :: AutoRacingScheduleListingRaceResultRaceResultListing
actual <- withSqliteConn ":memory:" $ runDbConn $ do
- runMigration silentMigrationLogger $ do
+ runMigrationSilent $ do
migrate a
migrate b
migrate c