Skip to content

Commit

Permalink
add test case for count
Browse files Browse the repository at this point in the history
  • Loading branch information
thma committed Nov 20, 2024
1 parent afa810a commit 0abe1ff
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 17 deletions.
1 change: 0 additions & 1 deletion src/Database/GP.hs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ module Database.GP
deleteById,
deleteMany,
deleteManyById,
setupTableFor,
setupTable,
defaultSqliteMapping,
defaultPostgresMapping,
Expand Down
3 changes: 1 addition & 2 deletions src/Database/GP/GenericPersistence.hs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ module Database.GP.GenericPersistence
deleteById,
deleteMany,
deleteManyById,
setupTableFor,
setupTable,
defaultSqliteMapping,
defaultPostgresMapping,
Expand Down Expand Up @@ -68,7 +67,7 @@ import Data.Convertible (Convertible)
import Database.GP.Conn
import Database.GP.Entity
import Database.GP.GenericPersistenceSafe (PersistenceException,
setupTableFor, setupTable, sql)
setupTable, sql)
import qualified Database.GP.GenericPersistenceSafe as GpSafe
import Database.GP.SqlGenerator
import Database.GP.TypeInfo
Expand Down
26 changes: 14 additions & 12 deletions src/Database/GP/GenericPersistenceSafe.hs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ module Database.GP.GenericPersistenceSafe
deleteById,
deleteMany,
deleteManyById,
setupTableFor,
setupTable,
defaultSqliteMapping,
defaultPostgresMapping,
Expand Down Expand Up @@ -165,6 +164,10 @@ count conn whereClause = do
entitiesFromRows :: forall a. (Entity a) => Conn -> [[SqlValue]] -> IO (Either PersistenceException [a])
entitiesFromRows = (tryPE .) . mapM . fromRow

-- | A function that persists an entity to a database.
-- The function takes an HDBC connection and an entity as parameters.
-- The entity is either inserted or updated, depending on whether it already exists in the database.
-- The required SQL statements are generated dynamically using Haskell generics and reflection
upsert :: forall a. (Entity a) => Conn -> a -> IO (Either PersistenceException ())
upsert conn entity = do
eitherExRes <- try $ do
Expand Down Expand Up @@ -195,6 +198,10 @@ commitIfAutoCommit :: Conn -> IO ()
commitIfAutoCommit (Conn autoCommit conn) = when autoCommit $ commit conn

-- | A function that explicitely inserts an entity into a database.
-- The function takes an HDBC connection and an entity as parameters.
-- The entity, as retrieved from the database, is returned as a Right value if the entity was successfully inserted.
-- (this allows to handle automatically updated fields like auto-incremented primary keys)
-- If the entity could not be inserted, a Left value is returned, containing a PersistenceException.
insert :: forall a. (Entity a) => Conn -> a -> IO (Either PersistenceException a)
insert conn entity = do
eitherExOrA <- try $ do
Expand Down Expand Up @@ -245,6 +252,7 @@ insertMany conn entities = do
Right _ -> return $ Right ()

-- | A function that explicitely updates an entity in a database.
-- The function takes an HDBC connection and an entity as parameters.
update :: forall a. (Entity a) => Conn -> a -> IO (Either PersistenceException ())
update conn entity = do
eitherExUnit <- try $ do
Expand Down Expand Up @@ -288,6 +296,8 @@ delete conn entity = do
Left ex -> return $ Left $ fromException ex
Right result -> return result

-- | A function that deletes an entity from a database by its id.
-- The function takes an HDBC connection and an entity id as parameters.
deleteById :: forall a id. (Entity a, Convertible id SqlValue) => Conn -> id -> IO (Either PersistenceException ())
deleteById conn idx = do
eitherExRes <- try $ do
Expand All @@ -302,6 +312,8 @@ deleteById conn idx = do
Left ex -> return $ Left $ fromException ex
Right result -> return result

-- | A function that deletes a list of entities from a database by their ids.
-- The function takes an HDBC connection and a list of entity ids as parameters.
deleteManyById :: forall a id. (Entity a, Convertible id SqlValue) => Conn -> [id] -> IO (Either PersistenceException ())
deleteManyById conn ids = tryPE $ do
stmt <- prepare conn (deleteStmtFor @a)
Expand All @@ -318,16 +330,6 @@ deleteMany conn entities = tryPE $ do
executeMany stmt (map (: []) eids)
commitIfAutoCommit conn

-- | set up a table for a given entity type. The table is dropped (if existing) and recreated.
-- The function takes an HDBC connection and a database type as parameter.
{-# DEPRECATED setupTableFor "use setupTable instead" #-}
setupTableFor :: forall a. (Entity a) => Database -> Conn -> IO ()
setupTableFor db conn = setupTable @a conn mapping
where
mapping = case db of
Postgres -> defaultPostgresMapping
SQLite -> defaultSqliteMapping

-- | set up a table for a given entity type. The table is dropped (if existing) and recreated.
-- The function takes an HDBC connection and a column type mapping as parameters.
setupTable :: forall a. (Entity a) => Conn -> ColumnTypeMapping -> IO ()
Expand All @@ -345,7 +347,7 @@ idValue conn x = do
where
idFieldIndex = fieldIndex @a (idField @a)

-- an alias for a simple quasiqouter
-- | an alias for a simple quasiqouter
sql :: QuasiQuoter
sql = r

Expand Down
7 changes: 6 additions & 1 deletion test/GenericPersistenceSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,11 @@ spec = do
head allPersons `shouldBe` bob
person' <- selectById conn (1 :: Int) :: IO (Maybe Person)
person' `shouldBe` Just bob
it "retrieves the number of selected Entities" $ do
conn <- prepareDB
insertMany conn manyPersons
countPersons <- count @Person conn allEntries
countPersons `shouldBe` 6
it "selectById returns Nothing if no Entity was found" $ do
conn <- prepareDB
person' <- selectById conn (1 :: Int) :: IO (Maybe Person)
Expand Down Expand Up @@ -292,7 +297,7 @@ spec = do
it "provides a Connection Pool" $ do
connPool <- sqlLitePool ":memory:"
withResource connPool $ \conn -> do
setupTableFor @Person SQLite conn
setupTable @Person conn defaultSqliteMapping
_ <- insert conn person
allPersons <- select conn allEntries :: IO [Person]
length allPersons `shouldBe` 1
Expand Down
2 changes: 1 addition & 1 deletion test/PostgresSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ spec = do
it "provides a Connection Pool" $ do
connPool <- postgreSQLPool "host=localhost dbname=postgres user=postgres password=admin port=5431"
withResource connPool $ \conn -> do
setupTableFor @Person Postgres conn
setupTable @Person conn defaultPostgresMapping
_ <- insert conn person
allPersons <- select conn allEntries :: IO [Person]
length allPersons `shouldBe` 1
Expand Down

0 comments on commit 0abe1ff

Please sign in to comment.