Skip to content

Commit

Permalink
Introduced new GetTopicUpdates stored procedure
Browse files Browse the repository at this point in the history
This retrieves any updates to the database—including new or modified `Topics`, `Attributes`, `ExtendedAttributes`, `Relationships`, or `TopicReferences`—since a given date, assuming that date is within the last twenty four hours.
  • Loading branch information
JeremyCaney committed Jan 19, 2021
1 parent bc8725a commit 0f36947
Show file tree
Hide file tree
Showing 2 changed files with 122 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@
<Build Include="Stored Procedures\UpdateExtendedAttributes.sql" />
<Build Include="Views\ReferenceIndex.sql" />
<Build Include="Views\RelationshipIndex.sql" />
<Build Include="Stored Procedures\GetTopicUpdates.sql" />
</ItemGroup>
<ItemGroup>
<ArtifactReference Include="$(DacPacRootPath)\Extensions\Microsoft\SQLDB\Extensions\SqlServer\130\SqlSchemas\master.dacpac">
Expand Down
121 changes: 121 additions & 0 deletions OnTopic.Data.Sql.Database/Stored Procedures/GetTopicUpdates.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
--------------------------------------------------------------------------------------------------------------------------------
-- GET TOPIC UPDATES
--------------------------------------------------------------------------------------------------------------------------------
-- Retrieves any data persisted to the database since the last query.
--------------------------------------------------------------------------------------------------------------------------------

CREATE PROCEDURE [dbo].[GetTopicUpdates]
@Since DATETIME
AS

--------------------------------------------------------------------------------------------------------------------------------
-- SELECT KEY ATTRIBUTES
--------------------------------------------------------------------------------------------------------------------------------
SELECT TopicID,
ContentType,
ParentID,
TopicKey,
0 AS SortOrder
FROM Topics
WHERE LastModified > @Since

--------------------------------------------------------------------------------------------------------------------------------
-- SELECT TOPIC ATTRIBUTES
--------------------------------------------------------------------------------------------------------------------------------
;WITH TopicAttributes
AS (
SELECT TopicID,
AttributeKey,
AttributeValue,
Version,
RowNumber = ROW_NUMBER() OVER (
PARTITION BY TopicID,
AttributeKey
ORDER BY Version DESC
)
FROM Attributes
WHERE Version > @Since
)
SELECT TopicID,
AttributeKey,
AttributeValue,
Version
FROM TopicAttributes
WHERE RowNumber = 1

--------------------------------------------------------------------------------------------------------------------------------
-- SELECT EXTENDED ATTRIBUTES
--------------------------------------------------------------------------------------------------------------------------------
;WITH TopicExtendedAttributes
AS (
SELECT TopicID,
AttributesXml,
Version,
RowNumber = ROW_NUMBER() OVER (
PARTITION BY TopicID
ORDER BY Version DESC
)
FROM ExtendedAttributes
WHERE Version > @Since
)
SELECT TopicID,
AttributesXml,
Version
FROM TopicExtendedAttributes
WHERE RowNumber = 1

--------------------------------------------------------------------------------------------------------------------------------
-- SELECT RELATIONSHIPS
--------------------------------------------------------------------------------------------------------------------------------
;WITH Relationships AS (
SELECT Source_TopicID,
RelationshipKey,
Target_TopicID,
IsDeleted,
Version,
RowNumber = ROW_NUMBER() OVER (
PARTITION BY Source_TopicID,
RelationshipKey
ORDER BY Version DESC
)
FROM [dbo].[Relationships]
WHERE Version > @Since
)
SELECT Relationships.Source_TopicID,
Relationships.RelationshipKey,
Relationships.Target_TopicID,
Relationships.IsDeleted,
Relationships.Version
FROM Relationships
WHERE RowNumber = 1

--------------------------------------------------------------------------------------------------------------------------------
-- SELECT REFERENCES
--------------------------------------------------------------------------------------------------------------------------------
;WITH TopicReferences AS (
SELECT Source_TopicID,
ReferenceKey,
Target_TopicID,
Version,
RowNumber = ROW_NUMBER() OVER (
PARTITION BY Source_TopicID,
ReferenceKey
ORDER BY Version DESC
)
FROM [dbo].[TopicReferences]
WHERE Version > @Since
)
SELECT TopicReferences.Source_TopicID,
TopicReferences.ReferenceKey,
TopicReferences.Target_TopicID,
TopicReferences.Version
FROM TopicReferences
WHERE RowNumber = 1

--------------------------------------------------------------------------------------------------------------------------------
-- SELECT HISTORY
--------------------------------------------------------------------------------------------------------------------------------
SELECT TopicID,
Version
FROM VersionHistoryIndex
WHERE Version > @Since

0 comments on commit 0f36947

Please # to comment.