Skip to content

Commit

Permalink
Fix Delete Extended Query Tag Sproc (#1274)
Browse files Browse the repository at this point in the history
* Fix delete sproc

* Minor edit

* Add version 8

* Minor refactor

* Update comment
  • Loading branch information
wsugarman authored Jan 14, 2022
1 parent 62dfe7c commit 3eb2988
Show file tree
Hide file tree
Showing 16 changed files with 4,450 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// -------------------------------------------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License (MIT). See LICENSE in the repo root for license information.
// -------------------------------------------------------------------------------------------------

using System.Globalization;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Data.SqlClient;
using Microsoft.Extensions.Logging;
using Microsoft.Health.Dicom.Core.Exceptions;
using Microsoft.Health.Dicom.SqlServer.Features.Schema;
using Microsoft.Health.Dicom.SqlServer.Features.Schema.Model;
using Microsoft.Health.SqlServer.Features.Client;
using Microsoft.Health.SqlServer.Features.Storage;

namespace Microsoft.Health.Dicom.SqlServer.Features.ExtendedQueryTag
{
internal class SqlExtendedQueryTagStoreV8 : SqlExtendedQueryTagStoreV4
{
public SqlExtendedQueryTagStoreV8(
SqlConnectionWrapperFactory sqlConnectionWrapperFactory,
ILogger<SqlExtendedQueryTagStoreV8> logger)
: base(sqlConnectionWrapperFactory, logger)
{
}

public override SchemaVersion Version => SchemaVersion.V8;

public override async Task DeleteExtendedQueryTagAsync(string tagPath, string vr, CancellationToken cancellationToken = default)
{
using (SqlConnectionWrapper sqlConnectionWrapper = await ConnectionWrapperFactory.ObtainSqlConnectionWrapperAsync(cancellationToken))
using (SqlCommandWrapper sqlCommandWrapper = sqlConnectionWrapper.CreateSqlCommand())
{
VLatest.DeleteExtendedQueryTagV8.PopulateCommand(sqlCommandWrapper, tagPath, (byte)ExtendedQueryTagLimit.ExtendedQueryTagVRAndDataTypeMapping[vr]);

try
{
await sqlCommandWrapper.ExecuteNonQueryAsync(cancellationToken);
}
catch (SqlException ex)
{
throw ex.Number switch
{
SqlErrorCodes.NotFound => new ExtendedQueryTagNotFoundException(
string.Format(CultureInfo.InvariantCulture, DicomSqlServerResource.ExtendedQueryTagNotFound, tagPath)),
_ => new DataStoreException(ex),
};
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/****************************************************************************************
Guidelines to create migration scripts - https://github.com/microsoft/healthcare-shared-components/tree/master/src/Microsoft.Health.SqlServer/SqlSchemaScriptsGuidelines.md
This diff is broken up into several sections:
- The first transaction contains changes to tables and stored procedures.
- The second transaction contains updates to indexes.
- IMPORTANT: Avoid rebuiling indexes inside the transaction, it locks the table during the transaction.
******************************************************************************************/
SET XACT_ABORT ON

/*************************************************************
Configure database
**************************************************************/

-- Enable RCSI
IF ((SELECT is_read_committed_snapshot_on FROM sys.databases WHERE database_id = DB_ID()) = 0) BEGIN
ALTER DATABASE CURRENT SET READ_COMMITTED_SNAPSHOT ON
END

-- Avoid blocking queries when statistics need to be rebuilt
IF ((SELECT is_auto_update_stats_async_on FROM sys.databases WHERE database_id = DB_ID()) = 0) BEGIN
ALTER DATABASE CURRENT SET AUTO_UPDATE_STATISTICS_ASYNC ON
END

-- Use ANSI behavior for null values
IF ((SELECT is_ansi_nulls_on FROM sys.databases WHERE database_id = DB_ID()) = 0) BEGIN
ALTER DATABASE CURRENT SET ANSI_NULLS ON
END

GO

/***************************************************************************************/
-- STORED PROCEDURE
-- DeleteExtendedQueryTagV8
--
-- DESCRIPTION
-- Delete specific extended query tag
--
-- PARAMETERS
-- @tagPath
-- * The extended query tag path
-- @dataType
-- * the data type of extended query tag. 0 -- String, 1 -- Long, 2 -- Double, 3 -- DateTime, 4 -- PersonName
/***************************************************************************************/
CREATE OR ALTER PROCEDURE dbo.DeleteExtendedQueryTagV8
@tagPath VARCHAR(64),
@dataType TINYINT
AS
BEGIN
SET NOCOUNT ON
SET XACT_ABORT ON

BEGIN TRANSACTION

DECLARE @tagKey INT

SELECT @tagKey = TagKey
FROM dbo.ExtendedQueryTag WITH(XLOCK)
WHERE dbo.ExtendedQueryTag.TagPath = @tagPath

-- Check existence
IF @@ROWCOUNT = 0
THROW 50404, 'extended query tag not found', 1

-- Update status to Deleting
UPDATE dbo.ExtendedQueryTag
SET TagStatus = 2
WHERE dbo.ExtendedQueryTag.TagKey = @tagKey

COMMIT TRANSACTION

BEGIN TRANSACTION

-- Delete index data
IF @dataType = 0
DELETE FROM dbo.ExtendedQueryTagString WHERE TagKey = @tagKey
ELSE IF @dataType = 1
DELETE FROM dbo.ExtendedQueryTagLong WHERE TagKey = @tagKey
ELSE IF @dataType = 2
DELETE FROM dbo.ExtendedQueryTagDouble WHERE TagKey = @tagKey
ELSE IF @dataType = 3
DELETE FROM dbo.ExtendedQueryTagDateTime WHERE TagKey = @tagKey
ELSE
DELETE FROM dbo.ExtendedQueryTagPersonName WHERE TagKey = @tagKey

-- Delete errors
DELETE FROM dbo.ExtendedQueryTagError
WHERE TagKey = @tagKey

-- Delete tag
DELETE FROM dbo.ExtendedQueryTag
WHERE TagKey = @tagKey

COMMIT TRANSACTION
END
GO
Loading

0 comments on commit 3eb2988

Please sign in to comment.