X-Git-Url: http://gitweb.michael.orlitzky.com/?a=blobdiff_plain;f=src%2FTSN%2FDatabase.hs;fp=src%2FTSN%2FDatabase.hs;h=935033066ae65d416fde566cf1f1b0697637b7fd;hb=c5d61081ad0d95950ca06761978d240632c44510;hp=0000000000000000000000000000000000000000;hpb=7a5034bbe4feb8625b3a6c534f07427ecf848ad8;p=dead%2Fhtsn-import.git diff --git a/src/TSN/Database.hs b/src/TSN/Database.hs new file mode 100644 index 0000000..9350330 --- /dev/null +++ b/src/TSN/Database.hs @@ -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