PlotDirector/PlotLine/Sql/362_ContextIntentAttachments.sql
2026-12-06 17:57:40 +00:00

250 lines
13 KiB
Transact-SQL

IF COL_LENGTH('dbo.StoryElementReferences', 'ChapterID') IS NULL
ALTER TABLE dbo.StoryElementReferences ADD ChapterID int NULL REFERENCES dbo.Chapters(ChapterID) ON DELETE CASCADE;
IF COL_LENGTH('dbo.StoryElementReferences', 'SceneID') IS NULL
ALTER TABLE dbo.StoryElementReferences ADD SceneID int NULL REFERENCES dbo.Scenes(SceneID) ON DELETE CASCADE;
GO
IF OBJECT_ID('dbo.CK_StoryElementReferences_IntentFile', 'C') IS NULL
BEGIN
ALTER TABLE dbo.StoryElementReferences DROP CONSTRAINT CK_StoryElementReferences_Entity;
ALTER TABLE dbo.StoryElementReferences WITH CHECK ADD CONSTRAINT CK_StoryElementReferences_Entity CHECK
((CASE WHEN CharacterID IS NULL THEN 0 ELSE 1 END +
CASE WHEN LocationID IS NULL THEN 0 ELSE 1 END +
CASE WHEN StoryAssetID IS NULL THEN 0 ELSE 1 END +
CASE WHEN PlotThreadID IS NULL THEN 0 ELSE 1 END +
CASE WHEN ChapterID IS NULL THEN 0 ELSE 1 END +
CASE WHEN SceneID IS NULL THEN 0 ELSE 1 END) = 1);
ALTER TABLE dbo.StoryElementReferences WITH CHECK ADD CONSTRAINT CK_StoryElementReferences_IntentFile
CHECK ((ChapterID IS NULL AND SceneID IS NULL) OR (Kind IN ('Image', 'Attachment') AND IsPrimary IS NULL));
CREATE INDEX IX_StoryElementReferences_Chapter ON dbo.StoryElementReferences(ChapterID, ReferenceID) WHERE ChapterID IS NOT NULL;
CREATE INDEX IX_StoryElementReferences_Scene ON dbo.StoryElementReferences(SceneID, ReferenceID) WHERE SceneID IS NOT NULL;
END;
GO
CREATE OR ALTER PROCEDURE dbo.StoryElementReference_Access
@UserID int, @EntityType varchar(12), @EntityID int, @Action varchar(12),
@ReferenceID int = NULL, @Version binary(8) = NULL, @Kind varchar(14) = NULL,
@Note nvarchar(500) = NULL, @StoragePath nvarchar(1000) = NULL,
@OriginalFileName nvarchar(300) = NULL, @FileSizeBytes bigint = NULL,
@Title nvarchar(160) = NULL, @StorageLimitBytes bigint = NULL
AS
BEGIN
SET NOCOUNT ON;
SET XACT_ABORT ON;
DECLARE @ProjectID int, @EntityName nvarchar(300), @CanEdit bit, @OwnerID int,
@CharacterID int, @LocationID int, @AssetID int, @ThreadID int, @ChapterID int, @SceneID int, @LegacyImagePath nvarchar(1000);
BEGIN TRY
BEGIN TRANSACTION;
IF @EntityType = 'Character'
BEGIN
SET @CharacterID = @EntityID;
SELECT @ProjectID = ProjectID, @EntityName = CharacterName
FROM dbo.Characters WITH (HOLDLOCK) WHERE CharacterID = @EntityID AND IsArchived = 0;
END
ELSE IF @EntityType = 'Location'
BEGIN
SET @LocationID = @EntityID;
SELECT @ProjectID = ProjectID, @EntityName = LocationName
FROM dbo.Locations WITH (HOLDLOCK) WHERE LocationID = @EntityID AND IsArchived = 0;
END
ELSE IF @EntityType = 'Asset'
BEGIN
SET @AssetID = @EntityID;
SELECT @ProjectID = ProjectID, @EntityName = AssetName, @LegacyImagePath = ImagePath
FROM dbo.StoryAssets WITH (HOLDLOCK) WHERE StoryAssetID = @EntityID AND IsArchived = 0;
END
ELSE IF @EntityType = 'PlotThread'
BEGIN
SET @ThreadID = @EntityID;
SELECT @ProjectID = l.ProjectID, @EntityName = t.ThreadTitle
FROM dbo.PlotThreads t WITH (HOLDLOCK)
JOIN dbo.PlotLines l WITH (HOLDLOCK) ON l.PlotLineID = t.PlotLineID
WHERE t.PlotThreadID = @EntityID AND t.IsArchived = 0 AND l.IsArchived = 0;
END
ELSE IF @EntityType IN ('Chapter', 'Scene')
BEGIN
SELECT @ProjectID = t.ProjectID, @EntityName = t.Label,
@ChapterID = CASE WHEN @EntityType = 'Chapter' THEN t.ChapterID END,
@SceneID = t.SceneID
FROM dbo.PlotPlanningTargets t
WHERE (@EntityType = 'Chapter' AND t.ChapterID = @EntityID AND t.SceneID IS NULL)
OR (@EntityType = 'Scene' AND t.SceneID = @EntityID);
END
SELECT @CanEdit = CONVERT(bit, MAX(CASE WHEN a.AccessRole IN (N'Owner', N'Collaborator') THEN 1 ELSE 0 END))
FROM dbo.ProjectUserAccess a WITH (HOLDLOCK)
INNER JOIN dbo.Projects p WITH (HOLDLOCK) ON p.ProjectID = a.ProjectID AND p.IsArchived = 0
WHERE a.ProjectID = @ProjectID AND a.UserID = @UserID AND a.IsActive = 1;
IF @CanEdit IS NULL OR (@Action NOT IN ('list', 'file') AND @CanEdit = 0)
BEGIN
IF @Action = 'list'
SELECT @ProjectID AS ProjectID, @EntityName AS EntityName, @CanEdit AS CanEdit,
@LegacyImagePath AS LegacyImagePath WHERE 1 = 0;
COMMIT;
RETURN;
END
IF @Action = 'list'
BEGIN
SELECT @ProjectID AS ProjectID, @EntityName AS EntityName, @CanEdit AS CanEdit, @LegacyImagePath AS LegacyImagePath;
SELECT ReferenceID, Kind, Note, OriginalFileName, Version, Title, CONVERT(bit, COALESCE(IsPrimary, 0)) AS IsPrimary
FROM dbo.StoryElementReferences
WHERE CharacterID = @CharacterID OR LocationID = @LocationID OR StoryAssetID = @AssetID OR PlotThreadID = @ThreadID OR ChapterID = @ChapterID OR SceneID = @SceneID
ORDER BY ReferenceID;
END
ELSE IF @Action = 'file'
BEGIN
SELECT Kind, StoragePath, OriginalFileName
FROM dbo.StoryElementReferences
WHERE ReferenceID = @ReferenceID AND StoragePath IS NOT NULL
AND (CharacterID = @CharacterID OR LocationID = @LocationID OR StoryAssetID = @AssetID OR PlotThreadID = @ThreadID OR ChapterID = @ChapterID OR SceneID = @SceneID);
END
ELSE IF @Action = 'add'
BEGIN
IF @StoragePath IS NOT NULL
BEGIN
SELECT TOP (1) @OwnerID = UserID FROM dbo.ProjectUserAccess WITH (HOLDLOCK)
WHERE ProjectID = @ProjectID AND AccessRole = N'Owner' AND IsActive = 1
ORDER BY ProjectUserAccessID;
IF @OwnerID IS NULL THROW 51010, 'Project owner unavailable.', 1;
IF @StorageLimitBytes IS NOT NULL AND @FileSizeBytes >
@StorageLimitBytes - (SELECT COALESCE(SUM(FileSizeBytes), 0)
FROM dbo.UserFile WITH (UPDLOCK, HOLDLOCK) WHERE UserID = @OwnerID AND IsDeleted = 0)
BEGIN
SELECT @ProjectID AS ProjectID, CONVERT(bit, 0) AS Changed, CONVERT(bit, 1) AS QuotaExceeded;
COMMIT;
RETURN;
END
END
INSERT dbo.StoryElementReferences (CharacterID, LocationID, StoryAssetID, PlotThreadID, ChapterID, SceneID, Kind, Note, StoragePath, OriginalFileName)
VALUES (@CharacterID, @LocationID, @AssetID, @ThreadID, @ChapterID, @SceneID, @Kind, @Note, @StoragePath, @OriginalFileName);
IF @StoragePath IS NOT NULL
INSERT dbo.UserFile (UserID, ProjectID, EntityType, EntityID, FileName, OriginalFileName, ContentType, FileSizeBytes, StoragePath)
VALUES (@OwnerID, @ProjectID, N'StoryElementReference', CONVERT(int, SCOPE_IDENTITY()),
RIGHT(@StoragePath, CHARINDEX('/', REVERSE(@StoragePath)) - 1), @OriginalFileName,
CASE @Kind WHEN 'Image' THEN N'image/webp' ELSE N'application/octet-stream' END, @FileSizeBytes, @StoragePath);
SELECT @ProjectID AS ProjectID, CONVERT(bit, 1) AS Changed;
END
ELSE IF @Action = 'caption'
BEGIN
UPDATE dbo.CharacterImages SET Caption = NULLIF(@Note, N'')
WHERE CharacterImageID = @ReferenceID AND CharacterID = @CharacterID AND ProjectID = @ProjectID AND IsDeleted = 0;
SELECT @ProjectID AS ProjectID, CONVERT(bit, @@ROWCOUNT) AS Changed;
END
ELSE IF @Action IN ('primary', 'clear')
BEGIN
DECLARE @LockResult int, @Resource nvarchar(255) = CONCAT(N'StoryReferencePrimary:', @EntityType, N':', @EntityID);
EXEC @LockResult = sys.sp_getapplock @Resource = @Resource, @LockMode = 'Exclusive',
@LockOwner = 'Transaction', @LockTimeout = 5000;
IF @LockResult < 0 THROW 51010, 'Primary image selection is busy.', 1;
DECLARE @ImageID int;
SELECT @ImageID = ReferenceID FROM dbo.StoryElementReferences WITH (UPDLOCK)
WHERE ReferenceID = @ReferenceID AND Version = @Version AND Kind = 'Image'
AND (@Action = 'primary' OR IsPrimary = 1)
AND (LocationID = @LocationID OR StoryAssetID = @AssetID);
IF @ImageID IS NOT NULL
BEGIN
UPDATE dbo.StoryElementReferences
SET IsPrimary = NULL
WHERE IsPrimary = 1 AND (LocationID = @LocationID OR StoryAssetID = @AssetID);
IF @Action = 'primary'
UPDATE dbo.StoryElementReferences SET IsPrimary = 1 WHERE ReferenceID = @ImageID;
END
SELECT @ProjectID AS ProjectID, CONVERT(bit, CASE WHEN @ImageID IS NULL THEN 0 ELSE 1 END) AS Changed;
END
ELSE IF @Action = 'edit'
BEGIN
UPDATE dbo.StoryElementReferences SET Note = @Note, Title = @Title
WHERE ReferenceID = @ReferenceID AND Version = @Version
AND (Kind <> 'Characteristic' OR LEN(@Note) > 0)
AND (CharacterID = @CharacterID OR LocationID = @LocationID OR StoryAssetID = @AssetID OR PlotThreadID = @ThreadID OR ChapterID = @ChapterID OR SceneID = @SceneID);
SELECT @ProjectID AS ProjectID, CONVERT(bit, @@ROWCOUNT) AS Changed;
END
ELSE IF @Action = 'delete'
BEGIN
DECLARE @Removed TABLE (StoragePath nvarchar(1000));
DELETE dbo.StoryElementReferences
OUTPUT deleted.StoragePath INTO @Removed
WHERE ReferenceID = @ReferenceID AND Version = @Version
AND (CharacterID = @CharacterID OR LocationID = @LocationID OR StoryAssetID = @AssetID OR PlotThreadID = @ThreadID OR ChapterID = @ChapterID OR SceneID = @SceneID);
SELECT @ProjectID AS ProjectID, CONVERT(bit, COUNT(*)) AS Changed, MAX(StoragePath) AS StoragePath
FROM @Removed;
END
COMMIT;
END TRY
BEGIN CATCH
IF XACT_STATE() <> 0 ROLLBACK;
THROW;
END CATCH
END;
GO
CREATE OR ALTER PROCEDURE dbo.StoryElementReference_Backup @ProjectID int
AS
BEGIN
SET NOCOUNT ON;
SELECT r.ReferenceID, r.CharacterID, r.LocationID, r.StoryAssetID, r.PlotThreadID, r.ChapterID, r.SceneID,
r.Kind, r.Note, r.StoragePath, r.OriginalFileName, r.Title, r.IsPrimary
FROM dbo.StoryElementReferences r
LEFT JOIN dbo.Characters c ON c.CharacterID = r.CharacterID
LEFT JOIN dbo.Locations l ON l.LocationID = r.LocationID
LEFT JOIN dbo.StoryAssets a ON a.StoryAssetID = r.StoryAssetID
LEFT JOIN dbo.PlotThreads t ON t.PlotThreadID = r.PlotThreadID
LEFT JOIN dbo.PlotLines pl ON pl.PlotLineID = t.PlotLineID
LEFT JOIN dbo.Scenes sc ON sc.SceneID = r.SceneID
LEFT JOIN dbo.Chapters ch ON ch.ChapterID = COALESCE(r.ChapterID, sc.ChapterID)
LEFT JOIN dbo.Books b ON b.BookID = ch.BookID
WHERE COALESCE(c.ProjectID, l.ProjectID, a.ProjectID, pl.ProjectID, b.ProjectID) = @ProjectID
ORDER BY r.ReferenceID;
END;
GO
CREATE OR ALTER PROCEDURE dbo.StoryElementReference_Restore @ProjectID int, @JsonData nvarchar(max)
AS
BEGIN
SET NOCOUNT ON;
IF EXISTS (
SELECT 1
FROM OPENJSON(@JsonData, '$.Data.StoryElementReferences') WITH
(
ReferenceID int, CharacterID int, LocationID int, StoryAssetID int, PlotThreadID int, ChapterID int, SceneID int, StoragePath nvarchar(1000)
) r
LEFT JOIN dbo.Characters c ON c.CharacterID = r.CharacterID
LEFT JOIN dbo.Locations l ON l.LocationID = r.LocationID
LEFT JOIN dbo.StoryAssets a ON a.StoryAssetID = r.StoryAssetID
LEFT JOIN dbo.PlotThreads t ON t.PlotThreadID = r.PlotThreadID
LEFT JOIN dbo.PlotLines pl ON pl.PlotLineID = t.PlotLineID
LEFT JOIN dbo.Scenes sc ON sc.SceneID = r.SceneID
LEFT JOIN dbo.Chapters ch ON ch.ChapterID = COALESCE(r.ChapterID, sc.ChapterID)
LEFT JOIN dbo.Books b ON b.BookID = ch.BookID
WHERE COALESCE(c.ProjectID, l.ProjectID, a.ProjectID, pl.ProjectID, b.ProjectID, -1) <> @ProjectID
OR (r.StoragePath IS NOT NULL AND NOT EXISTS (
SELECT 1 FROM dbo.UserFile f
WHERE f.ProjectID = @ProjectID AND f.EntityType = N'StoryElementReference'
AND f.EntityID = r.ReferenceID AND f.StoragePath = r.StoragePath AND f.IsDeleted = 0
AND f.FileSizeBytes BETWEEN 1 AND 10485760
AND f.StoragePath LIKE N'/uploads/story-element-references/%.blob'
AND CHARINDEX(N'/', f.StoragePath, LEN(N'/uploads/story-element-references/') + 1) = 0
))
)
THROW 51005, 'Story element reference has an invalid project link or missing staged file.', 1;
EXEC dbo.ProjectRestore_InsertJsonTable N'StoryElementReferences',
N'$.Data.StoryElementReferences', @JsonData, @ProjectID, 0;
UPDATE f SET OriginalFileName = r.OriginalFileName,
ContentType = CASE r.Kind WHEN 'Image' THEN N'image/webp' ELSE N'application/octet-stream' END
FROM dbo.UserFile f
INNER JOIN dbo.StoryElementReferences r ON r.ReferenceID = f.EntityID AND r.StoragePath = f.StoragePath
WHERE f.ProjectID = @ProjectID AND f.EntityType = N'StoryElementReference' AND f.IsDeleted = 0;
END;
GO