From 0ddc996d5622f138953ad027cc425713a276b46a Mon Sep 17 00:00:00 2001 From: Michael Orlitzky Date: Tue, 21 Jan 2014 12:34:27 -0500 Subject: [PATCH] Add a unique xml_file_id to News. --- src/TSN/XML/News.hs | 164 ++++++++++++++++++++++++++------------------ 1 file changed, 97 insertions(+), 67 deletions(-) diff --git a/src/TSN/XML/News.hs b/src/TSN/XML/News.hs index 130323c..ea13787 100644 --- a/src/TSN/XML/News.hs +++ b/src/TSN/XML/News.hs @@ -61,74 +61,11 @@ import TSN.XmlImport ( XmlImport(..) ) import Xml ( FromXml(..), ToDb(..), pickle_unpickle, unpickleable ) - --- | The database type for teams as they show up in the news. --- -data NewsTeam = - NewsTeam { team_name :: String } - deriving (Eq, Show) - - -instance ToDb NewsTeam where - -- | The database representaion of a 'NewsTeam' is itself. - type Db NewsTeam = NewsTeam - --- | This is needed to define the XmlImport instance for NewsTeam. --- -instance FromXml NewsTeam where - -- | How to we get a 'NewsTeam' from itself? - from_xml = id - --- | Allow us to call 'insert_xml' on the XML representation of --- NewsTeams. -- -instance XmlImport NewsTeam - - --- | Mapping between News records and NewsTeam records in the --- database. We don't name the fields because we don't use the names --- explicitly; that means we have to give them nice database names --- via groundhog. +-- DB/XML Data types -- -data News_NewsTeam = News_NewsTeam - (DefaultKey News) - (DefaultKey NewsTeam) - - --- | The database type for locations as they show up in the news. --- -data NewsLocation = - NewsLocation { - city :: Maybe String, - state :: Maybe String, - country :: String } - deriving (Eq, Show) - -instance ToDb NewsLocation where - -- | The database representation of a 'NewsLocation' is itself. - type Db NewsLocation = NewsLocation - --- | This is needed to define the XmlImport instance for NewsLocation. --- -instance FromXml NewsLocation where - -- | How to we get a 'NewsLocation' from itself? - from_xml = id - --- | Allow us to call 'insert_xml' on the XML representation of --- NewsLocations. --- -instance XmlImport NewsLocation - - --- | Mapping between News records and NewsLocation records in the --- database. We don't name the fields because we don't use the names --- explicitly; that means we have to give them nice database names --- via groundhog. --- -data News_NewsLocation = News_NewsLocation - (DefaultKey News) - (DefaultKey NewsLocation) +-- * News/Message -- | The msg_id child of contains an event_id attribute; we -- embed it into the 'News' type. We (pointlessly) use the "db_" @@ -167,6 +104,7 @@ data Message = -- data News = News { + db_xml_file_id :: Int, db_mid :: MsgId, db_sport :: String, db_url :: Maybe String, @@ -189,7 +127,8 @@ instance FromXml Message where -- | We use a record wildcard so GHC doesn't complain that we never -- used the field names. -- - from_xml Message{..} = News { db_mid = xml_mid, + from_xml Message{..} = News { db_xml_file_id = xml_xml_file_id, + db_mid = xml_mid, db_sport = xml_sport, db_url = xml_url, db_sms = xml_sms, @@ -202,6 +141,89 @@ instance FromXml Message where -- instance XmlImport Message + +-- * NewsTeam + +-- | The database type for teams as they show up in the news. +-- +data NewsTeam = + NewsTeam { team_name :: String } + deriving (Eq, Show) + + +instance ToDb NewsTeam where + -- | The database representaion of a 'NewsTeam' is itself. + type Db NewsTeam = NewsTeam + +-- | This is needed to define the XmlImport instance for NewsTeam. +-- +instance FromXml NewsTeam where + -- | How to we get a 'NewsTeam' from itself? + from_xml = id + +-- | Allow us to call 'insert_xml' on the XML representation of +-- NewsTeams. +-- +instance XmlImport NewsTeam + + + +-- * News_NewsTeam + +-- | Mapping between News records and NewsTeam records in the +-- database. We don't name the fields because we don't use the names +-- explicitly; that means we have to give them nice database names +-- via groundhog. +-- +data News_NewsTeam = News_NewsTeam + (DefaultKey News) + (DefaultKey NewsTeam) + + +-- * NewsLocation + +-- | The database type for locations as they show up in the news. +-- +data NewsLocation = + NewsLocation { + city :: Maybe String, + state :: Maybe String, + country :: String } + deriving (Eq, Show) + +instance ToDb NewsLocation where + -- | The database representation of a 'NewsLocation' is itself. + type Db NewsLocation = NewsLocation + +-- | This is needed to define the XmlImport instance for NewsLocation. +-- +instance FromXml NewsLocation where + -- | How to we get a 'NewsLocation' from itself? + from_xml = id + +-- | Allow us to call 'insert_xml' on the XML representation of +-- NewsLocations. +-- +instance XmlImport NewsLocation + + +-- * News_NewsLocation + +-- | Mapping between News records and NewsLocation records in the +-- database. We don't name the fields because we don't use the names +-- explicitly; that means we have to give them nice database names +-- via groundhog. +-- +data News_NewsLocation = News_NewsLocation + (DefaultKey News) + (DefaultKey NewsLocation) + + + +-- +-- Database code +-- + -- | Define 'dbmigrate' and 'dbimport' for 'Message's. The import is -- slightly non-generic because of our 'News_NewsTeam' and -- 'News_NewsLocation' join tables. @@ -270,11 +292,17 @@ mkPersist tsn_codegen_config [groundhog| dbName: news constructors: - name: News + uniques: + - name: unique_news + type: constraint + # Prevent multiple imports of the same message. + fields: [db_xml_file_id] fields: - name: db_mid embeddedType: - {name: msg_id, dbName: msg_id} - {name: event_id, dbName: event_id} + - embedded: MsgId fields: - name: db_msg_id @@ -282,7 +310,6 @@ mkPersist tsn_codegen_config [groundhog| - name: db_event_id dbName: event_id - - entity: News_NewsTeam dbName: news__news_teams constructors: @@ -312,6 +339,9 @@ mkPersist tsn_codegen_config [groundhog| onDelete: cascade |] +-- +-- XML Picklers +-- -- | Convert a 'NewsTeam' to/from XML. -- -- 2.44.2