//******************************************************************************************************
// AmendExpressionAttribute.cs - Gbtc
//
// Copyright © 2017, Grid Protection Alliance. All Rights Reserved.
//
// Licensed to the Grid Protection Alliance (GPA) under one or more contributor license agreements. See
// the NOTICE file distributed with this work for additional information regarding copyright ownership.
// The GPA licenses this file to you under the MIT License (MIT), the "License"; you may
// not use this file except in compliance with the License. You may obtain a copy of the License at:
//
// http://opensource.org/licenses/MIT
//
// Unless agreed to in writing, the subject software distributed under the License is distributed on an
// "AS-IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. Refer to the
// License for the specific language governing permissions and limitations.
//
// Code Modification History:
// ----------------------------------------------------------------------------------------------------
// 02/24/2017 - J. Ritchie Carroll
// Generated original version of source code.
//
//******************************************************************************************************
using System;
using System.Diagnostics.CodeAnalysis;
namespace GSF.Data.Model
{
#region [ Enumerations ]
///
/// Statement types for amendment application.
///
[Flags]
public enum StatementTypes
{
///
/// Apply amendment to all SELECT statements, i.e., , and .
///
Select = SelectCount | SelectSet | SelectRow,
///
/// Apply amendment to SELECT COUNT statements.
///
SelectCount = (int)Bits.Bit00,
///
/// Apply amendment to SELECT statements that can return multiple rows of data.
///
SelectSet = (int)Bits.Bit01,
///
/// Apply amendment to SELECT statements that will return a single row of data.
///
SelectRow = (int)Bits.Bit02,
///
/// Apply amendment to INSERT statements.
///
Insert = (int)Bits.Bit03,
///
/// Apply amendment to UPDATE statements.
///
Update = (int)Bits.Bit04,
///
/// Apply amendment to DELETE statements.
///
Delete = (int)Bits.Bit05
}
///
/// Target expressions for amendment application.
///
public enum TargetExpression
{
///
/// Apply amendment to table name.
///
TableName,
///
/// Apply amendment to full field list.
///
FieldList
}
///
/// Locations for target expression amendment application.
///
public enum AffixPosition
{
///
/// Apply amendment to beginning of .
///
Prefix,
///
/// Apply amendment to ending of .
///
Suffix
}
#endregion
///
/// Defines an attribute that will request amendment of a table name or field list with the specified text.
///
///
///
/// As an example, this can be used to add table hints, e.g., WITH (NOLOCK), for select statements in
/// SQL Server.
///
///
/// Applying the to a modeled table with no specified database type parameter
/// will be meant to infer that the amendment be used for all database types. Using a specific database type as a
/// parameter to the attribute, e.g., [AmendExpression("WITH (NOLOCK)", DatabaseType.SQLServer)], means the
/// amendment text will only be applied to the specific database - however, the attribute allows multiple instances
/// on the same identifier so you could specify that amendment application only be applied to two databases:
///
/// [AmendExpression("AS T1", DatabaseType.SQLServer), AmendExpression("AS T1", DatabaseType.MySQL)]
///
/// Other parameters exist to fully customize target expression, set affix position and specify statement types:
///
/// [AmendExpression("TOP 200",
/// TargetExpression = TargetExpression.FieldList,
/// AffixPosition = AffixPosition.Prefix,
/// StatementTypes = StatementTypes.SelectSet)]
///
///
///
[SuppressMessage("Microsoft.Design", "CA1019:DefineAccessorsForAttributeArguments")]
[AttributeUsage(AttributeTargets.Class, AllowMultiple = true)]
public sealed class AmendExpressionAttribute : Attribute
{
///
/// Gets or sets amendment text to be applied.
///
public string AmendmentText
{
get;
}
///
/// Gets or sets target expression for amendment application; defaults to .
///
public TargetExpression TargetExpression
{
get;
set;
}
///
/// Gets or sets location for amendment application; defaults to .
///
public AffixPosition AffixPosition
{
get;
set;
}
///
/// Gets or sets statement types for amendment application; defaults to .
///
public StatementTypes StatementTypes
{
get;
set;
}
///
/// Gets target for amendment application.
///
///
/// When value is null, amendment text will be applied to all database types.
///
public DatabaseType? TargetDatabaseType
{
get;
}
///
/// Creates a new that will request application of
/// for any database.
///
/// Amendment text to apply.
public AmendExpressionAttribute(string amendmentText)
{
AmendmentText = amendmentText;
TargetDatabaseType = null;
TargetExpression = TargetExpression.TableName;
AffixPosition = AffixPosition.Suffix;
StatementTypes = StatementTypes.Select;
}
///
/// Creates a new that will request application of
/// for the specified .
///
/// Amendment text to apply.
/// Target for amendment text.
public AmendExpressionAttribute(string amendmentText, DatabaseType targetDatabaseType)
{
AmendmentText = amendmentText;
TargetDatabaseType = targetDatabaseType;
TargetExpression = TargetExpression.TableName;
AffixPosition = AffixPosition.Suffix;
StatementTypes = StatementTypes.Select;
}
}
}