PlotDirector/PlotLine/Sql/312_CharacterRelationshipGroupBounds.sql

30 lines
3.9 KiB
Transact-SQL
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

-- At most 25 groups × 3 definitions; focused groups page all definitions in batches of 25.
SET ANSI_NULLS ON;
SET QUOTED_IDENTIFIER ON;
GO
CREATE OR ALTER PROCEDURE dbo.CharacterWorkspace_Relationships @CharacterID int,@UserID int,@Page int=1,@Search nvarchar(120)=N'',@RelationshipID int=NULL,@Category nvarchar(100)=NULL,@OtherCharacterID int=NULL,@DefinitionPage int=1
AS
BEGIN
SET NOCOUNT ON;
DECLARE @project int=(SELECT c.ProjectID FROM dbo.Characters c JOIN dbo.Projects p ON p.ProjectID=c.ProjectID WHERE c.CharacterID=@CharacterID AND c.IsArchived=0 AND p.IsArchived=0 AND EXISTS(SELECT 1 FROM dbo.ProjectUserAccess a WHERE a.ProjectID=c.ProjectID AND a.UserID=@UserID AND a.IsActive=1));
IF @project IS NULL THROW 51020,'Character unavailable',1;
IF @DefinitionPage<1 OR @DefinitionPage>100000 OR @Page<1 OR @Page>100000 THROW 51022,'Invalid page',1;
DECLARE @pattern nvarchar(500)=N'%'+REPLACE(REPLACE(REPLACE(REPLACE(@Search,N'~',N'~~'),N'%',N'~%'),N'_',N'~_'),N'[',N'~[')+N'%';
SELECT r.CharacterRelationshipID,other.CharacterID OtherID,other.CharacterName OtherName,t.TypeName INTO #IDs
FROM dbo.CharacterRelationships r JOIN dbo.Characters other ON other.CharacterID=CASE WHEN r.CharacterAID=@CharacterID THEN r.CharacterBID ELSE r.CharacterAID END JOIN dbo.RelationshipTypes t ON t.RelationshipTypeID=r.RelationshipTypeID LEFT JOIN dbo.RelationshipCategories cat ON cat.RelationshipCategoryID=t.RelationshipCategoryID
WHERE r.ProjectID=@project AND r.IsArchived=0 AND (r.CharacterAID=@CharacterID OR r.CharacterBID=@CharacterID) AND (@RelationshipID IS NULL OR r.CharacterRelationshipID=@RelationshipID) AND (@OtherCharacterID IS NULL OR other.CharacterID=@OtherCharacterID)
AND (@Category IS NULL OR @Category=N'' OR (@Category=N'family' AND cat.CategoryName=N'Family') OR (@Category=N'story' AND COALESCE(cat.CategoryName,N'')<>N'Family'))
AND (NULLIF(@Search,N'') IS NULL OR CONCAT(other.CharacterName,N' ',t.TypeName,N' ',r.Notes) LIKE @pattern ESCAPE N'~');
SELECT OtherID,OtherName,COUNT(*) DefinitionCount,ROW_NUMBER() OVER(ORDER BY OtherName COLLATE Latin1_General_100_CI_AS,OtherID) GroupNumber INTO #Groups FROM #IDs GROUP BY OtherID,OtherName;
SELECT COUNT(*) Total FROM #Groups;
SELECT TOP(20001) BookID,BookNumber,SortOrder FROM dbo.Books WHERE ProjectID=@project AND IsArchived=0;
SELECT TOP(20001) ch.ChapterID,ch.BookID,ch.ChapterNumber,ch.SortOrder FROM dbo.Chapters ch JOIN dbo.Books b ON b.BookID=ch.BookID WHERE b.ProjectID=@project AND b.IsArchived=0 AND ch.IsArchived=0;
SELECT TOP(20001) s.SceneID,s.ChapterID,s.SceneNumber,s.SortOrder FROM dbo.Scenes s JOIN dbo.Chapters ch ON ch.ChapterID=s.ChapterID JOIN dbo.Books b ON b.BookID=ch.BookID WHERE b.ProjectID=@project AND b.IsArchived=0 AND ch.IsArchived=0 AND s.IsArchived=0;
;WITH Ranked AS(SELECT i.*,ROW_NUMBER() OVER(PARTITION BY i.OtherID ORDER BY i.TypeName COLLATE Latin1_General_100_CI_AS,i.CharacterRelationshipID) DefinitionNumber FROM #IDs i)
SELECT r.*,g.DefinitionCount WorkspaceDefinitionCount,a.CharacterName CharacterAName,b.CharacterName CharacterBName,t.TypeName RelationshipTypeName,cat.CategoryName RelationshipCategoryName,st.StateName InitialRelationshipStateName
FROM Ranked x JOIN #Groups g ON g.OtherID=x.OtherID JOIN dbo.CharacterRelationships r ON r.CharacterRelationshipID=x.CharacterRelationshipID JOIN dbo.Characters a ON a.CharacterID=r.CharacterAID JOIN dbo.Characters b ON b.CharacterID=r.CharacterBID JOIN dbo.RelationshipTypes t ON t.RelationshipTypeID=r.RelationshipTypeID LEFT JOIN dbo.RelationshipCategories cat ON cat.RelationshipCategoryID=t.RelationshipCategoryID LEFT JOIN dbo.RelationshipStates st ON st.RelationshipStateID=r.InitialRelationshipStateID
WHERE g.GroupNumber BETWEEN ((@Page-1)*25)+1 AND @Page*25 AND x.DefinitionNumber BETWEEN CASE WHEN @OtherCharacterID IS NULL THEN 1 ELSE ((@DefinitionPage-1)*25)+1 END AND CASE WHEN @OtherCharacterID IS NULL THEN 3 ELSE @DefinitionPage*25 END
ORDER BY g.GroupNumber,x.DefinitionNumber;
END;
GO