139 lines
12 KiB
Plaintext
139 lines
12 KiB
Plaintext
|
|
--This function creates a RegionsOfInterest string from the TestSetupROIs and ROIPeriodChannels tables.
|
|
--It is used by sp_TestSetupsGet when Version 91 client is using a Version 92 or later database.
|
|
CREATE FUNCTION [dbo].[foo_RegionsOfInterestString]
|
|
(
|
|
@TestSetupId int
|
|
)
|
|
RETURNS nvarchar(MAX)
|
|
AS
|
|
BEGIN
|
|
--DECLARE @TestSetupId int = 1 --To debug, uncomment out this line and set to the TestSetupId desired. Also, comment out the lines above here and the RETURN and END at the bottom.
|
|
|
|
DECLARE @TestSetupROIId TABLE (testSetupROIId int)
|
|
DECLARE @ChannelNames TABLE (idx smallint Primary Key IDENTITY(1,1),
|
|
testSetupId int,
|
|
suffix nvarchar(50),
|
|
roiStart float,
|
|
roiEnd float,
|
|
isEnabled bit,
|
|
isDefault bit,
|
|
channelName nvarchar(4000))
|
|
DECLARE @i int
|
|
DECLARE @numrows int
|
|
DECLARE @ReturnString nvarchar(MAX)
|
|
DECLARE @CurrentSuffix nvarchar(50) = ' ' -- So it's not NULL to start
|
|
DECLARE @NewSuffix nvarchar(50)
|
|
DECLARE @NewTestSetupId int
|
|
|
|
INSERT INTO @TestSetupROIId ([testSetupROIId]) (SELECT TestSetupROIId FROM TestSetupROIs WHERE TestSetupId = @TestSetupId)
|
|
--DECLARE @TSRI XML = (SELECT * FROM @TestSetupROIId FOR XML AUTO) -- For debugging
|
|
|
|
INSERT INTO @ChannelNames (testSetupId, suffix, roiStart, roiEnd, isEnabled, isDefault, channelName) (SELECT TestSetupId, Suffix, ROIStart, ROIEnd, IsEnabled, IsDefault, ChannelName FROM TestSetupROIs r INNER JOIN ROIPeriodChannels c ON r.TestSetupROIId = c.TestSetupROIId WHERE r.TestSetupId = @TestSetupId)
|
|
--DECLARE @CN XML = (SELECT * FROM @ChannelNames FOR XML AUTO) -- For debugging
|
|
|
|
SET @ReturnString = CONCAT (@ReturnString, '[')
|
|
|
|
SET @i = 1
|
|
SET @numrows = (SELECT COUNT(*) FROM @ChannelNames)
|
|
IF @numrows = 0
|
|
BEGIN
|
|
-- Since there are no channels in the ROIPeriodChannels table, we only need to look in the TestSetupROIs table
|
|
-- for the Start and End values. This is the case where there is only one ROI, which includes all channels.
|
|
SET @ReturnString = CONCAT (@ReturnString, '{')
|
|
|
|
SET @ReturnString = CONCAT (@ReturnString, '"Suffix":')
|
|
SET @ReturnString = CONCAT (@ReturnString, '"')
|
|
SET @ReturnString = CONCAT (@ReturnString, '"')
|
|
|
|
SET @ReturnString = CONCAT (@ReturnString, ',')
|
|
SET @ReturnString = CONCAT (@ReturnString, '"Start":')
|
|
SET @ReturnString = CONCAT (@ReturnString, (SELECT ROIStart FROM TestSetupROIs WHERE TestSetupId = @TestSetupId))
|
|
|
|
SET @ReturnString = CONCAT (@ReturnString, ',')
|
|
SET @ReturnString = CONCAT (@ReturnString, '"End":')
|
|
SET @ReturnString = CONCAT (@ReturnString, (SELECT ROIEnd FROM TestSetupROIs WHERE TestSetupId = @TestSetupId))
|
|
|
|
SET @ReturnString = CONCAT (@ReturnString, ',')
|
|
SET @ReturnString = CONCAT (@ReturnString, '"IsEnabled":')
|
|
SET @ReturnString = CONCAT (@ReturnString, (SELECT CASE WHEN isEnabled = 1 THEN 'true' ELSE 'false' END FROM TestSetupROIs WHERE TestSetupId = @TestSetupId))
|
|
|
|
SET @ReturnString = CONCAT (@ReturnString, ',')
|
|
SET @ReturnString = CONCAT (@ReturnString, '"IsDefault":')
|
|
SET @ReturnString = CONCAT (@ReturnString, (SELECT CASE WHEN isDefault = 1 THEN 'true' ELSE 'false' END FROM TestSetupROIs WHERE TestSetupId = @TestSetupId))
|
|
|
|
SET @ReturnString = CONCAT (@ReturnString, ',')
|
|
SET @ReturnString = CONCAT (@ReturnString, '"ChannelNames":')
|
|
SET @ReturnString = CONCAT (@ReturnString, '[')
|
|
SET @ReturnString = CONCAT (@ReturnString, ']}')
|
|
SET @ReturnString = CONCAT (@ReturnString, ']')
|
|
END
|
|
ELSE
|
|
BEGIN
|
|
WHILE (@i <= (SELECT MAX(idx) FROM @ChannelNames))
|
|
BEGIN
|
|
SET @NewSuffix = (SELECT Suffix FROM @ChannelNames WHERE idx = @i)
|
|
IF (@NewSuffix <> @CurrentSuffix)
|
|
BEGIN
|
|
SET @CurrentSuffix = @NewSuffix
|
|
|
|
SET @ReturnString = CONCAT (@ReturnString, '{')
|
|
|
|
SET @ReturnString = CONCAT (@ReturnString, '"Suffix":')
|
|
SET @ReturnString = CONCAT (@ReturnString, '"')
|
|
SET @ReturnString = CONCAT (@ReturnString, (SELECT Suffix FROM @ChannelNames WHERE idx = @i))
|
|
SET @ReturnString = CONCAT (@ReturnString, '"')
|
|
|
|
SET @ReturnString = CONCAT (@ReturnString, ',')
|
|
SET @ReturnString = CONCAT (@ReturnString, '"Start":')
|
|
SET @ReturnString = CONCAT (@ReturnString, (SELECT roiStart FROM @ChannelNames WHERE idx = @i))
|
|
|
|
SET @ReturnString = CONCAT (@ReturnString, ',')
|
|
SET @ReturnString = CONCAT (@ReturnString, '"End":')
|
|
SET @ReturnString = CONCAT (@ReturnString, (SELECT roiEnd FROM @ChannelNames WHERE idx = @i))
|
|
|
|
SET @ReturnString = CONCAT (@ReturnString, ',')
|
|
SET @ReturnString = CONCAT (@ReturnString, '"IsEnabled":')
|
|
SET @ReturnString = CONCAT (@ReturnString, (SELECT CASE WHEN isEnabled = 1 THEN 'true' ELSE 'false' END FROM @ChannelNames WHERE idx = @i))
|
|
|
|
SET @ReturnString = CONCAT (@ReturnString, ',')
|
|
SET @ReturnString = CONCAT (@ReturnString, '"IsDefault":')
|
|
SET @ReturnString = CONCAT (@ReturnString, (SELECT CASE WHEN isDefault = 1 THEN 'true' ELSE 'false' END FROM @ChannelNames WHERE idx = @i))
|
|
|
|
SET @ReturnString = CONCAT (@ReturnString, ',')
|
|
SET @ReturnString = CONCAT (@ReturnString, '"ChannelNames":')
|
|
SET @ReturnString = CONCAT (@ReturnString, '[')
|
|
SET @ReturnString = CONCAT (@ReturnString, '"')
|
|
SET @ReturnString = CONCAT (@ReturnString, (SELECT SUBSTRING(ChannelName, CHARINDEX('\', ChannelName) + 1, LEN(ChannelName)) FROM @ChannelNames WHERE idx = @i))
|
|
SET @ReturnString = CONCAT (@ReturnString, '"')
|
|
END
|
|
ELSE
|
|
BEGIN
|
|
SET @ReturnString = CONCAT (@ReturnString, '"')
|
|
SET @ReturnString = CONCAT (@ReturnString, (SELECT SUBSTRING(ChannelName, CHARINDEX('\', ChannelName) + 1, LEN(ChannelName)) FROM @ChannelNames WHERE idx = @i))
|
|
SET @ReturnString = CONCAT (@ReturnString, '"')
|
|
END
|
|
|
|
SET @i = @i + 1
|
|
|
|
SET @NewSuffix = (SELECT Suffix FROM @ChannelNames WHERE idx = @i)
|
|
IF ((@NewSuffix is null) OR (@NewSuffix <> @CurrentSuffix))
|
|
BEGIN
|
|
SET @ReturnString = CONCAT (@ReturnString, ']}')
|
|
END
|
|
|
|
SET @NewTestSetupId = (SELECT TestSetupId FROM @ChannelNames WHERE idx = @i)
|
|
IF ((@NewTestSetupId is null) or (@NewTestSetupId <> @TestSetupId))
|
|
BEGIN
|
|
SET @ReturnString = CONCAT (@ReturnString, ']')
|
|
END
|
|
ELSE
|
|
BEGIN
|
|
SET @ReturnString = CONCAT (@ReturnString, ',')
|
|
END
|
|
END
|
|
END
|
|
|
|
RETURN @ReturnString
|
|
END
|