91 lines
7.7 KiB
Transact-SQL
91 lines
7.7 KiB
Transact-SQL
SET ANSI_NULLS ON;
|
|
GO
|
|
SET QUOTED_IDENTIFIER ON;
|
|
GO
|
|
IF OBJECT_ID(N'dbo.NameVerificationResults',N'U') IS NULL
|
|
BEGIN
|
|
CREATE TABLE dbo.NameVerificationResults(NameVerificationID int IDENTITY(1,1) NOT NULL PRIMARY KEY,ProjectID int NOT NULL REFERENCES dbo.Projects(ProjectID) ON DELETE CASCADE,NormalisedName nvarchar(120) NOT NULL,Fingerprint char(64) NOT NULL,AlgorithmVersion varchar(40) NOT NULL,ScopeKey char(64) NOT NULL,Status varchar(16) NOT NULL,RequestID uniqueidentifier NOT NULL,CreatedByUserID int NOT NULL,CreatedUtc datetime2 NOT NULL DEFAULT SYSUTCDATETIME(),CompletedUtc datetime2 NULL,ExpiresUtc datetime2 NOT NULL,AiReservationID uniqueidentifier NULL,ResultJson nvarchar(max) NULL,ErrorCode varchar(40) NULL,
|
|
CONSTRAINT UX_NameVerification_Cache UNIQUE(ProjectID,NormalisedName,Fingerprint,AlgorithmVersion,ScopeKey),CONSTRAINT CK_NameVerification_Json CHECK(ResultJson IS NULL OR ISJSON(ResultJson)=1),CONSTRAINT CK_NameVerification_Status CHECK(Status IN('Pending','Running','Completed','Failed')));
|
|
CREATE INDEX IX_NameVerification_Read ON dbo.NameVerificationResults(ProjectID,NormalisedName,CompletedUtc DESC) INCLUDE(Status,Fingerprint,AlgorithmVersion,ScopeKey);
|
|
END;
|
|
GO
|
|
CREATE OR ALTER PROCEDURE dbo.NameVerification_Read
|
|
@UserID int,@ProjectID int,@NormalisedName nvarchar(120),@Fingerprint char(64),@AlgorithmVersion varchar(40)
|
|
AS
|
|
BEGIN
|
|
SET NOCOUNT ON;
|
|
IF NOT EXISTS(SELECT 1 FROM dbo.ProjectUserAccess WHERE ProjectID=@ProjectID AND UserID=@UserID AND IsActive=1) THROW 51010,'Series access denied',1;
|
|
SELECT TOP(1) * FROM dbo.NameVerificationResults WHERE ProjectID=@ProjectID AND NormalisedName=@NormalisedName AND ResultJson IS NOT NULL
|
|
ORDER BY CASE WHEN Fingerprint=@Fingerprint AND AlgorithmVersion=@AlgorithmVersion THEN 0 ELSE 1 END,CASE WHEN JSON_VALUE(ResultJson,'$.complete')='true' THEN 0 ELSE 1 END,CompletedUtc DESC,NameVerificationID DESC;
|
|
END;
|
|
GO
|
|
CREATE OR ALTER PROCEDURE dbo.NameVerification_Prepare
|
|
@UserID int,@ProjectID int,@NormalisedName nvarchar(120),@Fingerprint char(64),@AlgorithmVersion varchar(40),@ScopeKey char(64),@RequestID uniqueidentifier,@DurationSeconds int,@RetryInterrupted bit=0
|
|
AS
|
|
BEGIN
|
|
SET NOCOUNT ON;SET XACT_ABORT ON;
|
|
IF NOT EXISTS(SELECT 1 FROM dbo.ProjectUserAccess WHERE ProjectID=@ProjectID AND UserID=@UserID AND IsActive=1 AND AccessRole IN(N'Owner',N'Collaborator')) THROW 51010,'Verification requires project editing access',1;
|
|
IF @DurationSeconds NOT BETWEEN 30 AND 1800 THROW 51022,'Invalid verification duration',1;
|
|
BEGIN TRANSACTION;
|
|
DECLARE @lock int,@resource nvarchar(255)=CONCAT(N'NameVerification:',CONVERT(varchar(64),HASHBYTES('SHA2_256',CONCAT(@ProjectID,N'|',@NormalisedName,N'|',@Fingerprint,N'|',@AlgorithmVersion,N'|',@ScopeKey)),2));
|
|
EXEC @lock=sys.sp_getapplock @Resource=@resource,@LockMode='Exclusive',@LockOwner='Transaction',@LockTimeout=10000;
|
|
IF @lock<0 THROW 51025,'Verification preparation is busy; retry',1;
|
|
DECLARE @id int,@status varchar(16),@expires datetime2;
|
|
SELECT @id=NameVerificationID,@status=Status,@expires=ExpiresUtc FROM dbo.NameVerificationResults WITH(UPDLOCK,HOLDLOCK)
|
|
WHERE ProjectID=@ProjectID AND NormalisedName=@NormalisedName AND Fingerprint=@Fingerprint AND AlgorithmVersion=@AlgorithmVersion AND ScopeKey=@ScopeKey;
|
|
IF @id IS NOT NULL AND ((@status='Completed' AND NOT(@RetryInterrupted=1 AND EXISTS(SELECT 1 FROM dbo.NameVerificationResults WHERE NameVerificationID=@id AND JSON_VALUE(ResultJson,'$.interrupted')='true'))) OR (@status IN('Pending','Running') AND @expires>SYSUTCDATETIME()))
|
|
BEGIN SELECT *,CAST(0 AS bit) AS IsNew FROM dbo.NameVerificationResults WHERE NameVerificationID=@id;COMMIT;RETURN;END;
|
|
IF @id IS NULL
|
|
BEGIN
|
|
INSERT dbo.NameVerificationResults(ProjectID,NormalisedName,Fingerprint,AlgorithmVersion,ScopeKey,Status,RequestID,CreatedByUserID,ExpiresUtc)
|
|
VALUES(@ProjectID,@NormalisedName,@Fingerprint,@AlgorithmVersion,@ScopeKey,'Pending',@RequestID,@UserID,DATEADD(second,@DurationSeconds+120,SYSUTCDATETIME()));
|
|
SET @id=CONVERT(int,SCOPE_IDENTITY());
|
|
END
|
|
ELSE UPDATE dbo.NameVerificationResults SET Status='Pending',RequestID=@RequestID,CreatedByUserID=@UserID,CreatedUtc=SYSUTCDATETIME(),CompletedUtc=CASE WHEN ResultJson IS NULL THEN NULL ELSE CompletedUtc END,ExpiresUtc=DATEADD(second,@DurationSeconds+120,SYSUTCDATETIME()),AiReservationID=NULL,ErrorCode=NULL WHERE NameVerificationID=@id;
|
|
SELECT *,CAST(1 AS bit) AS IsNew FROM dbo.NameVerificationResults WHERE NameVerificationID=@id;
|
|
COMMIT;
|
|
END;
|
|
GO
|
|
CREATE OR ALTER PROCEDURE dbo.NameVerification_Bind
|
|
@UserID int,@NameVerificationID int,@RequestID uniqueidentifier,@ReservationID uniqueidentifier
|
|
AS
|
|
BEGIN
|
|
SET NOCOUNT ON;SET XACT_ABORT ON;BEGIN TRANSACTION;
|
|
IF NOT EXISTS(SELECT 1 FROM dbo.NameVerificationResults WITH(UPDLOCK,HOLDLOCK) WHERE NameVerificationID=@NameVerificationID AND RequestID=@RequestID AND CreatedByUserID=@UserID AND Status='Pending' AND ExpiresUtc>SYSUTCDATETIME()) THROW 51025,'Verification request is no longer current',1;
|
|
UPDATE dbo.AiUsageReservations SET JobKind='NameVerification',JobID=@NameVerificationID
|
|
WHERE ReservationID=@ReservationID AND UserID=@UserID AND OperationID='names.verify' AND Status='Reserved' AND ExpiresUtc>SYSUTCDATETIME() AND JobID IS NULL;
|
|
IF @@ROWCOUNT<>1 THROW 51025,'Verification reservation is unavailable or already bound',1;
|
|
UPDATE dbo.NameVerificationResults SET AiReservationID=@ReservationID,Status='Running' WHERE NameVerificationID=@NameVerificationID;
|
|
COMMIT;
|
|
END;
|
|
GO
|
|
CREATE OR ALTER PROCEDURE dbo.NameVerification_Save
|
|
@UserID int,@NameVerificationID int,@RequestID uniqueidentifier,@ResultJson nvarchar(max),@ReservationID uniqueidentifier=NULL,@LeaseToken uniqueidentifier=NULL
|
|
AS
|
|
BEGIN
|
|
SET NOCOUNT ON;SET XACT_ABORT ON;
|
|
IF ISJSON(@ResultJson)<>1 OR DATALENGTH(@ResultJson)>4194304 THROW 51022,'Verification result exceeds its storage budget',1;
|
|
BEGIN TRANSACTION;
|
|
DECLARE @project int,@bound uniqueidentifier;
|
|
SELECT @project=ProjectID,@bound=AiReservationID FROM dbo.NameVerificationResults WITH(UPDLOCK,HOLDLOCK) WHERE NameVerificationID=@NameVerificationID AND RequestID=@RequestID AND CreatedByUserID=@UserID AND Status IN('Pending','Running') AND ExpiresUtc>SYSUTCDATETIME();
|
|
IF @project IS NULL THROW 51025,'Verification request is no longer current',1;
|
|
IF NOT EXISTS(SELECT 1 FROM dbo.ProjectUserAccess WHERE ProjectID=@project AND UserID=@UserID AND IsActive=1 AND AccessRole IN(N'Owner',N'Collaborator')) THROW 51010,'Verification access changed',1;
|
|
IF @ReservationID IS NULL
|
|
BEGIN
|
|
IF @bound IS NOT NULL OR EXISTS(SELECT 1 FROM OPENJSON(@ResultJson,'$.findings')) THROW 51025,'AI findings require an execution reservation',1;
|
|
END
|
|
ELSE
|
|
BEGIN
|
|
IF @bound IS NULL OR @bound<>@ReservationID OR NOT EXISTS(SELECT 1 FROM dbo.AiUsageReservations WITH(UPDLOCK,HOLDLOCK) WHERE ReservationID=@ReservationID AND UserID=@UserID AND OperationID='names.verify' AND JobID=@NameVerificationID AND LeaseToken=@LeaseToken AND Status='Running' AND ExpiresUtc>SYSUTCDATETIME()) THROW 51025,'AI execution lease is no longer current',1;
|
|
DECLARE @usable bit=CASE WHEN EXISTS(SELECT 1 FROM OPENJSON(@ResultJson,'$.findings')) THEN 1 ELSE 0 END;
|
|
EXEC dbo.AiAllowance_Settle @ReservationID=@ReservationID,@LeaseToken=@LeaseToken,@UsableResult=@usable;
|
|
END;
|
|
UPDATE dbo.NameVerificationResults SET Status='Completed',CompletedUtc=SYSUTCDATETIME(),ResultJson=@ResultJson,ErrorCode=NULL WHERE NameVerificationID=@NameVerificationID;
|
|
COMMIT;
|
|
END;
|
|
GO
|
|
CREATE OR ALTER PROCEDURE dbo.NameVerification_Fail @UserID int,@NameVerificationID int,@RequestID uniqueidentifier
|
|
AS
|
|
BEGIN SET NOCOUNT ON;UPDATE dbo.NameVerificationResults SET Status='Failed',ErrorCode='processing-failed',CompletedUtc=SYSUTCDATETIME() WHERE NameVerificationID=@NameVerificationID AND CreatedByUserID=@UserID AND RequestID=@RequestID AND Status IN('Pending','Running');END;
|
|
GO
|