PlotDirector/PlotLine/Sql/294_NarrativeFocusChapterBoundaries.sql

103 lines
6.8 KiB
Transact-SQL

SET ANSI_NULLS ON;
SET QUOTED_IDENTIFIER ON;
GO
-- Chapter-only positions include empty chapters; scene stepping always uses real manuscript scenes.
CREATE OR ALTER PROCEDURE dbo.NarrativeFocus_Read
@UserID int,@ProjectID int,@BookID int
AS
BEGIN
SET NOCOUNT ON; SET XACT_ABORT ON;
IF NOT EXISTS(SELECT 1 FROM dbo.Books b JOIN dbo.Projects p ON p.ProjectID=b.ProjectID
JOIN dbo.ProjectUserAccess a ON a.ProjectID=p.ProjectID AND a.UserID=@UserID AND a.IsActive=1
WHERE b.BookID=@BookID AND b.ProjectID=@ProjectID AND b.IsArchived=0 AND p.IsArchived=0)
THROW 51000,'Narrative focus is unavailable in this book.',1;
BEGIN TRANSACTION;
IF NOT EXISTS(SELECT 1 FROM dbo.NarrativeFocus WITH(UPDLOCK,HOLDLOCK) WHERE UserID=@UserID AND BookID=@BookID)
BEGIN
-- One-time migration only. Never consult an objective after this row exists.
DECLARE @chapter int,@scene int;
SELECT @chapter=c.ChapterID,@scene=s.SceneID FROM dbo.WriterWorkingObjectives o
JOIN dbo.Chapters c ON c.ChapterID=o.ChapterID AND c.BookID=@BookID AND c.IsArchived=0
LEFT JOIN dbo.Scenes s ON s.SceneID=o.SceneID AND s.ChapterID=c.ChapterID AND s.IsArchived=0
WHERE o.UserID=@UserID AND o.BookID=@BookID;
INSERT dbo.NarrativeFocus(UserID,BookID,ChapterID,SceneID) VALUES(@UserID,@BookID,@chapter,@scene);
IF @chapter IS NOT NULL INSERT dbo.NarrativeFocusHistory VALUES(@UserID,@BookID,1,@chapter,@scene);
END;
-- A deleted/moved scene downgrades to its still-valid chapter. Never follow it into another book.
UPDATE f SET ChapterID=c.ChapterID,SceneID=s.SceneID,Version=Version+1
FROM dbo.NarrativeFocus f
LEFT JOIN dbo.Chapters c ON c.ChapterID=f.ChapterID AND c.BookID=@BookID AND c.IsArchived=0
LEFT JOIN dbo.Scenes s ON s.SceneID=f.SceneID AND s.ChapterID=c.ChapterID AND s.IsArchived=0
WHERE f.UserID=@UserID AND f.BookID=@BookID
AND ((f.ChapterID IS NOT NULL AND c.ChapterID IS NULL) OR(f.SceneID IS NOT NULL AND s.SceneID IS NULL));
DELETE h FROM dbo.NarrativeFocusHistory h
WHERE h.UserID=@UserID AND h.BookID=@BookID AND
(NOT EXISTS(SELECT 1 FROM dbo.Chapters c WHERE c.ChapterID=h.ChapterID AND c.BookID=@BookID AND c.IsArchived=0)
OR(h.SceneID IS NOT NULL AND NOT EXISTS(SELECT 1 FROM dbo.Scenes s WHERE s.SceneID=h.SceneID AND s.ChapterID=h.ChapterID AND s.IsArchived=0)));
COMMIT;
SELECT @ProjectID ProjectID,BookID,ChapterID,SceneID,Version FROM dbo.NarrativeFocus WHERE UserID=@UserID AND BookID=@BookID;
SELECT c.ChapterID,c.ChapterTitle,c.ChapterNumber,n.SceneID,n.SceneTitle,n.SceneNumber,n.BookSceneIndex
FROM dbo.Chapters c LEFT JOIN dbo.NarrativeSceneOrder n ON n.ChapterID=c.ChapterID
WHERE c.BookID=@BookID AND c.IsArchived=0
ORDER BY c.SortOrder,c.ChapterNumber,CASE WHEN n.SceneID IS NULL THEN 1 ELSE 0 END,n.BookSceneIndex,c.ChapterID;
SELECT TOP(20) h.ChapterID,h.SceneID,h.Sequence FROM dbo.NarrativeFocusHistory h
WHERE h.UserID=@UserID AND h.BookID=@BookID ORDER BY h.Sequence DESC;
END;
GO
CREATE OR ALTER PROCEDURE dbo.NarrativeFocus_Set
@UserID int,@ProjectID int,@BookID int,@ChapterID int=NULL,@SceneID int=NULL,@ExpectedVersion bigint,@Direction int=0
AS
BEGIN
SET NOCOUNT ON; SET XACT_ABORT ON;
IF NOT EXISTS(SELECT 1 FROM dbo.Books b JOIN dbo.Projects p ON p.ProjectID=b.ProjectID
JOIN dbo.ProjectUserAccess a ON a.ProjectID=p.ProjectID AND a.UserID=@UserID AND a.IsActive=1
WHERE b.BookID=@BookID AND b.ProjectID=@ProjectID AND b.IsArchived=0 AND p.IsArchived=0)
THROW 51000,'Narrative focus is unavailable in this book.',1;
IF @Direction NOT IN(-1,0,1) THROW 51000,'Invalid narrative direction.',1;
BEGIN TRANSACTION;
DECLARE @version bigint,@oldChapter int,@oldScene int;
SELECT @version=Version,@oldChapter=ChapterID,@oldScene=SceneID FROM dbo.NarrativeFocus WITH(UPDLOCK,HOLDLOCK) WHERE UserID=@UserID AND BookID=@BookID;
IF @version IS NULL OR @version<>@ExpectedVersion THROW 51001,'Current focus changed in another window. Review it and try again.',1;
IF @Direction<>0
BEGIN
DECLARE @index bigint=(SELECT BookSceneIndex FROM dbo.NarrativeSceneOrder WHERE BookID=@BookID AND SceneID=@oldScene AND ChapterID=@oldChapter);
SET @ChapterID=NULL;SET @SceneID=NULL;
IF @index IS NOT NULL
SELECT @ChapterID=ChapterID,@SceneID=SceneID FROM dbo.NarrativeSceneOrder WHERE BookID=@BookID AND BookSceneIndex=@index+@Direction;
ELSE IF @Direction=1
SELECT TOP(1) @ChapterID=n.ChapterID,@SceneID=n.SceneID FROM dbo.NarrativeSceneOrder n
LEFT JOIN dbo.Chapters currentChapter ON currentChapter.ChapterID=@oldChapter AND currentChapter.BookID=@BookID AND currentChapter.IsArchived=0
WHERE n.BookID=@BookID AND(currentChapter.ChapterID IS NULL OR n.ChapterID=currentChapter.ChapterID OR
(NOT EXISTS(SELECT 1 FROM dbo.NarrativeSceneOrder present WHERE present.BookID=@BookID AND present.ChapterID=currentChapter.ChapterID)
AND(n.ChapterSortOrder>currentChapter.SortOrder OR(n.ChapterSortOrder=currentChapter.SortOrder AND n.ChapterNumber>currentChapter.ChapterNumber))))
ORDER BY n.BookSceneIndex;
ELSE
SELECT TOP(1) @ChapterID=n.ChapterID,@SceneID=n.SceneID FROM dbo.NarrativeSceneOrder n
LEFT JOIN dbo.Chapters currentChapter ON currentChapter.ChapterID=@oldChapter AND currentChapter.BookID=@BookID AND currentChapter.IsArchived=0
WHERE n.BookID=@BookID AND(currentChapter.ChapterID IS NULL OR n.ChapterID=currentChapter.ChapterID OR
(NOT EXISTS(SELECT 1 FROM dbo.NarrativeSceneOrder present WHERE present.BookID=@BookID AND present.ChapterID=currentChapter.ChapterID)
AND(n.ChapterSortOrder<currentChapter.SortOrder OR(n.ChapterSortOrder=currentChapter.SortOrder AND n.ChapterNumber<=currentChapter.ChapterNumber))))
ORDER BY n.BookSceneIndex DESC;
IF @SceneID IS NULL BEGIN COMMIT;RETURN;END;
END;
IF @SceneID IS NOT NULL
BEGIN
DECLARE @actualChapter int=(SELECT ChapterID FROM dbo.NarrativeSceneOrder WHERE BookID=@BookID AND SceneID=@SceneID);
IF @actualChapter IS NULL OR(@ChapterID IS NOT NULL AND @actualChapter<>@ChapterID) THROW 51000,'Scene is unavailable in this book and chapter.',1;
SET @ChapterID=@actualChapter;
END;
IF @ChapterID IS NOT NULL AND NOT EXISTS(SELECT 1 FROM dbo.Chapters WHERE ChapterID=@ChapterID AND BookID=@BookID AND IsArchived=0)
THROW 51000,'Chapter is unavailable in this book.',1;
IF ISNULL(@ChapterID,0)<>ISNULL(@oldChapter,0) OR ISNULL(@SceneID,0)<>ISNULL(@oldScene,0)
BEGIN
UPDATE dbo.NarrativeFocus SET ChapterID=@ChapterID,SceneID=@SceneID,Version=Version+1 WHERE UserID=@UserID AND BookID=@BookID;
DELETE dbo.NarrativeFocusHistory WHERE UserID=@UserID AND BookID=@BookID AND ChapterID=@ChapterID AND ISNULL(SceneID,0)=ISNULL(@SceneID,0);
IF @ChapterID IS NOT NULL INSERT dbo.NarrativeFocusHistory VALUES(@UserID,@BookID,@version+1,@ChapterID,@SceneID);
DELETE h FROM dbo.NarrativeFocusHistory h WHERE UserID=@UserID AND BookID=@BookID AND Sequence NOT IN
(SELECT TOP(20) Sequence FROM dbo.NarrativeFocusHistory WHERE UserID=@UserID AND BookID=@BookID ORDER BY Sequence DESC);
END;
COMMIT;
END;
GO