Skip to content

Commit

Permalink
Add InvalidUdtException
Browse files Browse the repository at this point in the history
  • Loading branch information
DavoudEshtehari committed Dec 18, 2021
1 parent 4e81503 commit d9e0720
Show file tree
Hide file tree
Showing 10 changed files with 152 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ internal static SqlUserDefinedTypeAttribute GetUdtAttribute(Type t)
}
else
{
throw InvalidUdtException.Create(t, Strings.SqlUdtReason_NoUdtAttribute);
throw ADP.CreateInvalidUdtException(t, nameof(Strings.SqlUdtReason_NoUdtAttribute));
}
return udtAttr;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -430,9 +430,6 @@
<Compile Include="..\..\src\Microsoft\Data\DataException.cs">
<Link>Microsoft\Data\DataException.cs</Link>
</Compile>
<Compile Include="..\..\src\Microsoft\Data\SqlClient\Server\InvalidUdtException.cs">
<Link>Microsoft\Data\SqlClient\Server\InvalidUdtException.cs</Link>
</Compile>
<Compile Include="..\..\src\Microsoft\Data\SqlClient\Server\SmiTypedGetterSetter.cs">
<Link>Microsoft\Data\SqlClient\Server\SmiTypedGetterSetter.cs</Link>
</Compile>
Expand Down
104 changes: 63 additions & 41 deletions src/Microsoft.Data.SqlClient/src/Microsoft/Data/Common/AdapterUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,14 @@
using System.Threading.Tasks;
using System.Transactions;
using Microsoft.Data.SqlClient;
using Microsoft.Data.SqlClient.Server;
using Microsoft.Win32;
using IsolationLevel = System.Data.IsolationLevel;

#if NETFRAMEWORK
using Microsoft.SqlServer.Server;
using System.Reflection;
#else
using Microsoft.Data.SqlClient.Server;
#endif

namespace Microsoft.Data.Common
Expand Down Expand Up @@ -61,6 +63,26 @@ internal static class ADP
/// </summary>
internal const int MaxBufferAccessTokenExpiry = 600;

#region Microsoft.Sqlserver.Server
#if NETFRAMEWORK
private static readonly MethodInfo s_method = typeof(InvalidUdtException).GetMethod("Create", BindingFlags.NonPublic | BindingFlags.Static);
#endif
/// <summary>
/// Calls "InvalidUdtException.Create" method when an invalid UDT occurs.
/// </summary>
internal static InvalidUdtException CreateInvalidUdtException(Type udtType, string resourceReasonName)
{
InvalidUdtException e =
#if NETFRAMEWORK
(InvalidUdtException)s_method.Invoke(null, new object[] { udtType, resourceReasonName });
ADP.TraceExceptionAsReturnValue(e);
#else
InvalidUdtException.Create(udtType, resourceReasonName);
#endif
return e;
}
#endregion

static private void TraceException(string trace, Exception e)
{
Debug.Assert(null != e, "TraceException: null Exception");
Expand Down Expand Up @@ -105,7 +127,7 @@ internal static Exception ExceptionWithStackTrace(Exception e)
}
}

#region COM+ exceptions
#region COM+ exceptions
internal static ArgumentException Argument(string error)
{
ArgumentException e = new(error);
Expand Down Expand Up @@ -304,9 +326,9 @@ internal static ArgumentOutOfRangeException ArgumentOutOfRange(string message, s
TraceExceptionAsReturnValue(e);
return e;
}
#endregion
#endregion

#region Helper Functions
#region Helper Functions
internal static ArgumentOutOfRangeException NotSupportedEnumerationValue(Type type, string value, string method)
=> ArgumentOutOfRange(StringsHelper.GetString(Strings.ADP_NotSupportedEnumerationValue, type.Name, value, method), type.Name);

Expand Down Expand Up @@ -394,9 +416,9 @@ internal static ArgumentException InvalidArgumentLength(string argumentName, int
=> Argument(StringsHelper.GetString(Strings.ADP_InvalidArgumentLength, argumentName, limit));

internal static ArgumentException MustBeReadOnly(string argumentName) => Argument(StringsHelper.GetString(Strings.ADP_MustBeReadOnly, argumentName));
#endregion
#endregion

#region CommandBuilder, Command, BulkCopy
#region CommandBuilder, Command, BulkCopy
/// <summary>
/// This allows the caller to determine if it is an error or not for the quotedString to not be quoted
/// </summary>
Expand Down Expand Up @@ -729,9 +751,9 @@ internal static ArgumentException InvalidPrefixSuffix()
TraceExceptionAsReturnValue(e);
return e;
}
#endregion
#endregion

#region DbConnectionOptions, DataAccess
#region DbConnectionOptions, DataAccess
internal static ArgumentException ConnectionStringSyntax(int index) => Argument(StringsHelper.GetString(Strings.ADP_ConnectionStringSyntax, index));

internal static ArgumentException KeywordNotSupported(string keyword) => Argument(StringsHelper.GetString(Strings.ADP_KeywordNotSupported, keyword));
Expand Down Expand Up @@ -769,9 +791,9 @@ internal static ArgumentException ConvertFailed(Type fromType, Type toType, Exce

internal static ArgumentException InvalidMinMaxPoolSizeValues()
=> ADP.Argument(StringsHelper.GetString(Strings.ADP_InvalidMinMaxPoolSizeValues));
#endregion
#endregion

#region DbConnection
#region DbConnection
private static string ConnectionStateMsg(ConnectionState state)
{ // MDAC 82165, if the ConnectionState enum to msg the localization looks weird
return state switch
Expand All @@ -794,25 +816,25 @@ internal static NotImplementedException MethodNotImplemented([CallerMemberName]
TraceExceptionAsReturnValue(e);
return e;
}
#endregion
#endregion

#region Stream
#region Stream
internal static Exception StreamClosed([CallerMemberName] string method = "") => InvalidOperation(StringsHelper.GetString(Strings.ADP_StreamClosed, method));

static internal Exception InvalidSeekOrigin(string parameterName) => ArgumentOutOfRange(StringsHelper.GetString(Strings.ADP_InvalidSeekOrigin), parameterName);

internal static IOException ErrorReadingFromStream(Exception internalException) => IO(StringsHelper.GetString(Strings.SqlMisc_StreamErrorMessage), internalException);
#endregion
#endregion

#region Generic Data Provider Collection
#region Generic Data Provider Collection
internal static ArgumentException ParametersIsNotParent(Type parameterType, ICollection collection)
=> Argument(StringsHelper.GetString(Strings.ADP_CollectionIsNotParent, parameterType.Name, collection.GetType().Name));

internal static ArgumentException ParametersIsParent(Type parameterType, ICollection collection)
=> Argument(StringsHelper.GetString(Strings.ADP_CollectionIsNotParent, parameterType.Name, collection.GetType().Name));
#endregion
#endregion

#region ConnectionUtil
#region ConnectionUtil
internal enum InternalErrorCode
{
UnpooledObjectHasOwner = 0,
Expand Down Expand Up @@ -883,9 +905,9 @@ internal static Exception InternalConnectionError(ConnectionError internalError)
internal static Exception InvalidConnectRetryCountValue() => Argument(StringsHelper.GetString(Strings.SQLCR_InvalidConnectRetryCountValue));

internal static Exception InvalidConnectRetryIntervalValue() => Argument(StringsHelper.GetString(Strings.SQLCR_InvalidConnectRetryIntervalValue));
#endregion
#endregion

#region DbDataReader
#region DbDataReader
internal static Exception DataReaderClosed([CallerMemberName] string method = "")
=> InvalidOperation(StringsHelper.GetString(Strings.ADP_DataReaderClosed, method));

Expand Down Expand Up @@ -927,9 +949,9 @@ internal static Exception InvalidXmlMissingColumn(string collectionName, string
=> Argument(StringsHelper.GetString(Strings.MDF_InvalidXmlMissingColumn, collectionName, columnName));

internal static InvalidOperationException AsyncOperationPending() => InvalidOperation(StringsHelper.GetString(Strings.ADP_PendingAsyncOperation));
#endregion
#endregion

#region IDbCommand
#region IDbCommand
// IDbCommand.CommandType
static internal ArgumentOutOfRangeException InvalidCommandType(CommandType value)
{
Expand Down Expand Up @@ -998,9 +1020,9 @@ internal static Exception DeriveParametersNotSupported(IDbCommand value)
=> DataAdapter(StringsHelper.GetString(Strings.ADP_DeriveParametersNotSupported, value.GetType().Name, value.CommandType.ToString()));

internal static Exception NoStoredProcedureExists(string sproc) => InvalidOperation(StringsHelper.GetString(Strings.ADP_NoStoredProcedureExists, sproc));
#endregion
#endregion

#region DbMetaDataFactory
#region DbMetaDataFactory
internal static Exception DataTableDoesNotExist(string collectionName)
=> Argument(StringsHelper.GetString(Strings.MDF_DataTableDoesNotExist, collectionName));

Expand Down Expand Up @@ -1071,17 +1093,17 @@ internal static Exception UndefinedPopulationMechanism(string populationMechanis
#else
=> throw new NotImplementedException();
#endif
#endregion
#endregion

#region DbConnectionPool and related
#region DbConnectionPool and related
internal static Exception PooledOpenTimeout()
=> ADP.InvalidOperation(StringsHelper.GetString(Strings.ADP_PooledOpenTimeout));

internal static Exception NonPooledOpenTimeout()
=> ADP.TimeoutException(StringsHelper.GetString(Strings.ADP_NonPooledOpenTimeout));
#endregion
#endregion

#region DbProviderException
#region DbProviderException
internal static InvalidOperationException TransactionConnectionMismatch()
=> Provider(StringsHelper.GetString(Strings.ADP_TransactionConnectionMismatch));

Expand All @@ -1090,18 +1112,18 @@ internal static InvalidOperationException TransactionRequired(string method)

internal static InvalidOperationException TransactionCompletedButNotDisposed() => Provider(StringsHelper.GetString(Strings.ADP_TransactionCompletedButNotDisposed));

#endregion
#endregion

#region SqlMetaData, SqlTypes
#region SqlMetaData, SqlTypes
internal static Exception InvalidMetaDataValue() => ADP.Argument(StringsHelper.GetString(Strings.ADP_InvalidMetaDataValue));

internal static InvalidOperationException NonSequentialColumnAccess(int badCol, int currCol)
=> InvalidOperation(StringsHelper.GetString(Strings.ADP_NonSequentialColumnAccess,
badCol.ToString(CultureInfo.InvariantCulture),
currCol.ToString(CultureInfo.InvariantCulture)));
#endregion
#endregion

#region IDataParameter
#region IDataParameter
internal static ArgumentException InvalidDataType(TypeCode typecode) => Argument(StringsHelper.GetString(Strings.ADP_InvalidDataType, typecode.ToString()));

internal static ArgumentException UnknownDataType(Type dataType) => Argument(StringsHelper.GetString(Strings.ADP_UnknownDataType, dataType.FullName));
Expand Down Expand Up @@ -1157,9 +1179,9 @@ internal static Exception ParameterConversionFailed(object value, Type destType,
TraceExceptionAsReturnValue(e);
return e;
}
#endregion
#endregion

#region IDataParameterCollection
#region IDataParameterCollection
internal static Exception ParametersMappingIndex(int index, DbParameterCollection collection) => CollectionIndexInt32(index, collection.GetType(), collection.Count);

internal static Exception ParametersSourceIndex(string parameterName, DbParameterCollection collection, Type parameterType)
Expand All @@ -1170,16 +1192,16 @@ internal static Exception ParameterNull(string parameter, DbParameterCollection

internal static Exception InvalidParameterType(DbParameterCollection collection, Type parameterType, object invalidValue)
=> CollectionInvalidType(collection.GetType(), parameterType, invalidValue);
#endregion
#endregion

#region IDbTransaction
#region IDbTransaction
internal static Exception ParallelTransactionsNotSupported(DbConnection obj)
=> InvalidOperation(StringsHelper.GetString(Strings.ADP_ParallelTransactionsNotSupported, obj.GetType().Name));

internal static Exception TransactionZombied(DbTransaction obj) => InvalidOperation(StringsHelper.GetString(Strings.ADP_TransactionZombied, obj.GetType().Name));
#endregion
#endregion

#region DbProviderConfigurationHandler
#region DbProviderConfigurationHandler
internal static InvalidOperationException InvalidMixedUsageOfSecureAndClearCredential()
=> InvalidOperation(StringsHelper.GetString(Strings.ADP_InvalidMixedUsageOfSecureAndClearCredential));

Expand All @@ -1203,10 +1225,10 @@ static internal InvalidOperationException InvalidMixedUsageOfAccessTokenAndAuthe

static internal Exception InvalidMixedUsageOfCredentialAndAccessToken()
=> InvalidOperation(StringsHelper.GetString(Strings.ADP_InvalidMixedUsageOfCredentialAndAccessToken));
#endregion
#endregion

#if NETFRAMEWORK
#region netfx project only
#region netfx project only
internal static Task<T> CreatedTaskWithException<T>(Exception ex)
{
TaskCompletionSource<T> completion = new();
Expand Down Expand Up @@ -1451,9 +1473,9 @@ internal static IntPtr IntPtrOffset(IntPtr pbase, int offset)
}

internal static bool IsEmpty(string str) => string.IsNullOrEmpty(str);
#endregion
#endregion
#else
#region netcore project only
#region netcore project only
internal static Timer UnsafeCreateTimer(TimerCallback callback, object state, int dueTime, int period)
{
// Don't capture the current ExecutionContext and its AsyncLocals onto
Expand Down Expand Up @@ -1524,7 +1546,7 @@ internal static ArgumentOutOfRangeException InvalidParameterDirection(ParameterD
//
internal static Exception InvalidCommandTimeout(int value, [CallerMemberName] string property = "")
=> Argument(StringsHelper.GetString(Strings.ADP_InvalidCommandTimeout, value.ToString(CultureInfo.InvariantCulture)), property);
#endregion
#endregion
#endif
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@

using System;
using System.Collections.Generic;
using Microsoft.Data.SqlClient.Server;
using Microsoft.Data.Common;
#if NETFRAMEWORK
using Microsoft.SqlServer.Server;
#else
using Microsoft.Data.SqlClient.Server;
#endif

namespace Microsoft.Data.SqlClient
Expand Down Expand Up @@ -35,7 +37,7 @@ internal static SqlUdtInfo GetFromType(Type target)
SqlUdtInfo udtAttr = TryGetFromType(target);
if (udtAttr == null)
{
throw Server.InvalidUdtException.Create(target, Strings.SqlUdtReason_NoUdtAttribute);
throw ADP.CreateInvalidUdtException(target, nameof(Strings.SqlUdtReason_NoUdtAttribute));
}
return udtAttr;
}
Expand Down
50 changes: 50 additions & 0 deletions src/Microsoft.SqlServer.Server/InvalidUdtException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System;
using System.Runtime.Serialization;

namespace Microsoft.SqlServer.Server
{
/// <include file='../../doc/snippets/Microsoft.Data.SqlClient.Server/InvalidUdtException.xml' path='docs/members[@name="InvalidUdtException"]/InvalidUdtException/*' />
[Serializable]
public sealed class InvalidUdtException : SystemException
{
private const int InvalidUdtHResult = unchecked((int)0x80131937);

internal InvalidUdtException() : base()
{
HResult = InvalidUdtHResult;
}

internal InvalidUdtException(string message) : base(message)
{
HResult = InvalidUdtHResult;
}

internal InvalidUdtException(string message, Exception innerException) : base(message, innerException)
{
HResult = InvalidUdtHResult;
}

private InvalidUdtException(SerializationInfo si, StreamingContext sc) : base(si, sc)
{
}

/// <include file='../../doc/snippets/Microsoft.Data.SqlClient.Server/InvalidUdtException.xml' path='docs/members[@name="InvalidUdtException"]/GetObjectData/*' />
[System.Security.Permissions.SecurityPermissionAttribute(System.Security.Permissions.SecurityAction.LinkDemand, Flags = System.Security.Permissions.SecurityPermissionFlag.SerializationFormatter)]
public override void GetObjectData(SerializationInfo si, StreamingContext context)
{
base.GetObjectData(si, context);
}

internal static InvalidUdtException Create(Type udtType, string resourceReason)
{
string reason = StringsHelper.GetString(resourceReason);
string message = StringsHelper.GetString(Strings.SqlUdt_InvalidUdtMessage, udtType.FullName, reason);
InvalidUdtException e = new InvalidUdtException(message);
return e;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

<ItemGroup Condition="'$(TargetFramework)'!='net46'">
<Compile Include="IBinarySerialize.cs" />
<Compile Include="InvalidUdtException.cs" />
<Compile Include="SqlFacetAttribute.cs" />
<Compile Include="SqlFunctionAttribute.cs" />
<Compile Include="SqlMethodAttribute.cs" />
Expand Down
Loading

0 comments on commit d9e0720

Please sign in to comment.