Skip to content

Commit

Permalink
Merge pull request #1334 from amitaibu/deleteRecordByIds
Browse files Browse the repository at this point in the history
Add `deleteRecordByIds`
  • Loading branch information
mpscholten authored Jan 24, 2022
2 parents e5ff578 + 5b525d3 commit dc0cdb0
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 14 deletions.
6 changes: 6 additions & 0 deletions Guide/database.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -567,6 +567,8 @@ do
deleteRecord user
```
Or pass only the ID with `[`deleteRecordById`](https://ihp.digitallyinduced.com/api-docs/IHP-ModelSupport.html#v:deleteRecordById).
This will execute:
```sql
Expand All @@ -583,10 +585,14 @@ do
deleteRecords users
```
Or pass only the ID with `[`deleteRecordByIds`](https://ihp.digitallyinduced.com/api-docs/IHP-ModelSupport.html#v:deleteRecordByIds).
This will execute:
```sql
DELETE FROM users WHERE id IN (...)
```
### Deleting all records
Expand Down
34 changes: 20 additions & 14 deletions IHP/ModelSupport.hs
Original file line number Diff line number Diff line change
Expand Up @@ -578,14 +578,9 @@ logQuery query parameters time = do
-- DELETE FROM projects WHERE id = '..'
--
-- Use 'deleteRecords' if you want to delete multiple records.
deleteRecord :: forall model. (?modelContext :: ModelContext, Table model) => model -> IO ()
deleteRecord model = do
let condition = primaryKeyCondition model
let whereConditions = condition |> map (\(field, _) -> field <> " = ?") |> intercalate " AND "
let theQuery = "DELETE FROM " <> tableName @model <> " WHERE " <> whereConditions
let theParameters = map snd condition
sqlExec (PG.Query . cs $! theQuery) theParameters
pure ()
deleteRecord :: forall record id. (?modelContext :: ModelContext, Show id, Table record, HasField "id" record id, ToField id) => record -> IO ()
deleteRecord record =
deleteRecordById @record (get #id record)
{-# INLINABLE deleteRecord #-}

-- | Like 'deleteRecord' but using an Id
Expand All @@ -594,10 +589,10 @@ deleteRecord model = do
-- >>> delete projectId
-- DELETE FROM projects WHERE id = '..'
--
deleteRecordById :: forall model id. (?modelContext :: ModelContext, Show id, Table model, HasField "id" model id, ToField id) => id -> IO ()
deleteRecordById :: forall record id. (?modelContext :: ModelContext, Show id, Table record, ToField id) => id -> IO ()
deleteRecordById id = do
let theQuery = "DELETE FROM " <> tableName @model <> " WHERE id = ?"
let theParameters = (PG.Only id)
let theQuery = "DELETE FROM " <> tableName @record <> " WHERE id = ?"
let theParameters = PG.Only id
sqlExec (PG.Query . cs $! theQuery) theParameters
pure ()
{-# INLINABLE deleteRecordById #-}
Expand All @@ -608,12 +603,23 @@ deleteRecordById id = do
-- >>> deleteRecords projects
-- DELETE FROM projects WHERE id IN (..)
deleteRecords :: forall record id. (?modelContext :: ModelContext, Show id, Table record, HasField "id" record id, ToField id) => [record] -> IO ()
deleteRecords records = do
deleteRecords records =
deleteRecordByIds @record (ids records)
{-# INLINABLE deleteRecords #-}

-- | Like 'deleteRecordById' but for a list of Ids.
--
-- >>> let projectIds :: [ Id Project ] = ...
-- >>> delete projectIds
-- DELETE FROM projects WHERE id IN ('..')
--
deleteRecordByIds :: forall record id. (?modelContext :: ModelContext, Show id, Table record, ToField id) => [id] -> IO ()
deleteRecordByIds ids = do
let theQuery = "DELETE FROM " <> tableName @record <> " WHERE id IN ?"
let theParameters = PG.Only (PG.In (ids records))
let theParameters = (PG.Only (PG.In ids))
sqlExec (PG.Query . cs $! theQuery) theParameters
pure ()
{-# INLINABLE deleteRecords #-}
{-# INLINABLE deleteRecordByIds #-}

-- | Runs a @DELETE@ query to delete all rows in a table.
--
Expand Down

0 comments on commit dc0cdb0

Please sign in to comment.