Skip to content

Commit 46ba436

Browse files
committed
Introduce (optional, default) support for DeleteUnmatched
Currently, in order to delete existing relationships, callers must trigger `UpdateTopic`'s `@DeleteRelationships` flag—which will delete all relationships for that topic—and _then_ call `UpdateRelationship` with the final relationships set. This is wasteful, as often times the relationships will have overlap, or even be identical. The previous update (e725077) allows doing inserts without recreating existing relationships. This update allows unmatched records to be deleted, thus providing true synchronization with `@RelatedTopics`, without needing to first delete the relationships, nor needing to recreate any relationships. Optionally, this can be disabled by setting the `@DeletedUnmatched` parameter to `0`, thus only appending any new relationships—though, by default, it is enabled (`1`).
1 parent e725077 commit 46ba436

1 file changed

Lines changed: 15 additions & 1 deletion

File tree

OnTopic.Data.Sql.Database/Stored Procedures/UpdateRelationships.sql

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
CREATE PROCEDURE [dbo].[UpdateRelationships]
88
@TopicID INT = -1,
99
@RelationshipKey VARCHAR(255) = 'related',
10-
@RelatedTopics TopicList READONLY
10+
@RelatedTopics TopicList READONLY,
11+
@DeleteUnmatched BIT = 1
1112
AS
1213

1314
--------------------------------------------------------------------------------------------------------------------------------
@@ -28,6 +29,19 @@ LEFT JOIN Relationships Existing
2829
AND Source_TopicID = @TopicID
2930
WHERE Target_TopicID IS NULL
3031

32+
--------------------------------------------------------------------------------------------------------------------------------
33+
-- DELETE UNMATCHED VALUES
34+
--------------------------------------------------------------------------------------------------------------------------------
35+
IF @DeleteUnmatched = 1
36+
BEGIN
37+
DELETE EXISTING
38+
FROM @RelatedTopics Relationships
39+
RIGHT JOIN Relationships Existing
40+
ON Target_TopicID = TopicId
41+
WHERE Source_TopicID = @TopicID
42+
AND ISNULL(TopicID, '') = ''
43+
END
44+
3145
--------------------------------------------------------------------------------------------------------------------------------
3246
-- RETURN TOPIC ID
3347
--------------------------------------------------------------------------------------------------------------------------------

0 commit comments

Comments
 (0)