]> gitweb.michael.orlitzky.com - dead/htsn-import.git/blobdiff - src/TSN/Database.hs
Add a new module, TSN.Database, and use it to clean up TSN.XML.News.
[dead/htsn-import.git] / src / TSN / Database.hs
diff --git a/src/TSN/Database.hs b/src/TSN/Database.hs
new file mode 100644 (file)
index 0000000..9350330
--- /dev/null
@@ -0,0 +1,18 @@
+-- | Standalone database functions for dealing with Groundhog types.
+--
+module TSN.Database ( insert_or_select )
+where
+
+import Database.Groundhog ( AutoKey, insertByAll )
+import Database.Groundhog.Core ( PersistBackend, PersistEntity )
+
+
+-- | Attempt to insert the given object returning its new primary
+--   key. If any unique constraints would be violated, instead return
+--   the primary key of the existing record that caused the collision.
+insert_or_select :: (PersistBackend m, PersistEntity a)
+                 => a
+                 -> m (AutoKey a)
+insert_or_select x = do
+  tmp <- insertByAll x
+  return $ (either id id) tmp