X-Git-Url: http://gitweb.michael.orlitzky.com/?a=blobdiff_plain;f=src%2FTSN%2FXmlImport.hs;h=4300e87ff4d6182475fcda1f30071b2082a43030;hb=59d56f0ae0da1d4b1898b3c7536ae38473306319;hp=3791ab56ef19add5ec4d0638d94a4e26cec51b51;hpb=a913a63f7679b59f9b650619e8fbfc6e81f3f8d3;p=dead%2Fhtsn-import.git diff --git a/src/TSN/XmlImport.hs b/src/TSN/XmlImport.hs index 3791ab5..4300e87 100644 --- a/src/TSN/XmlImport.hs +++ b/src/TSN/XmlImport.hs @@ -26,3 +26,18 @@ class (FromXml a, PersistEntity (Db a)) => XmlImport a where => a -> m ( Either (AutoKey (Db a)) (AutoKey (Db a)) ) insertByAll_xml x = insertByAll (from_xml x) + + + -- | Try to insert the given object and get its primary key + -- back. Or, if there's a unique constraint violation, get the + -- primary key of the unique thing already present. + -- + -- Note: we can switch to using fmap here as soon as Functor is a + -- superclass of Monad (PersistBackend is a Monad). + -- + insert_xml_or_select :: (PersistBackend m) + => a + -> m (AutoKey (Db a)) + insert_xml_or_select x = do + tmp <- insertByAll_xml x + return $ (either id id) tmp