Skip to content
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -806,6 +806,7 @@
</ItemGroup>
<ItemGroup>
<Compile Include="Microsoft\Data\SqlClient\AAsyncCallContext.cs" />
<Compile Include="Microsoft\Data\SqlClient\GenericCastExtensions.cs" />
<Compile Include="Resources\Strings.Designer.cs">
<DesignTime>True</DesignTime>
<AutoGen>True</AutoGen>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// 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.

namespace Microsoft.Data.SqlClient
{
/// <summary>
/// Serves to convert generic to out type by casting to object first. Relies on JIT to optimize out unneccessary casts and prevent double boxing.
/// </summary>
internal static class GenericCastExtensions
{
public static TOut GenericCast<TIn, TOut>(this TIn value)
{
return (TOut)(object)value;
}
}
}

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -2084,13 +2084,21 @@ private int ValueSizeCore(object value)

// Coerced Value is also used in SqlBulkCopy.ConvertValue(object value, _SqlMetaData metadata)
internal static object CoerceValue(object value, MetaType destinationType, out bool coercedToDataFeed, out bool typeChanged, bool allowStreaming = true)
{
typeChanged = CoerceValueIfNeeded(value, destinationType, out var objValue, out coercedToDataFeed, allowStreaming);

return typeChanged ? objValue : value;
}

internal static bool CoerceValueIfNeeded<T>(T value, MetaType destinationType, out object objValue, out bool coercedToDataFeed, bool allowStreaming = true)
{
Debug.Assert(!(value is DataFeed), "Value provided should not already be a data feed");
Debug.Assert(!ADP.IsNull(value), "Value provided should not be null");
Debug.Assert(null != destinationType, "null destinationType");

objValue = null;
coercedToDataFeed = false;
typeChanged = false;
var typeChanged = false;
Type currentType = value.GetType();

if (
Expand All @@ -2111,61 +2119,61 @@ internal static object CoerceValue(object value, MetaType destinationType, out b
// For Xml data, destination Type is always string
if (currentType == typeof(SqlXml))
{
value = MetaType.GetStringFromXml((XmlReader)(((SqlXml)value).CreateReader()));
objValue = MetaType.GetStringFromXml(value.GenericCast<T, SqlXml>().CreateReader());
}
else if (currentType == typeof(SqlString))
{
typeChanged = false; // Do nothing
}
else if (typeof(XmlReader).IsAssignableFrom(currentType))
else if (value is XmlReader xmlReader)
{
if (allowStreaming)
{
coercedToDataFeed = true;
value = new XmlDataFeed((XmlReader)value);
objValue = new XmlDataFeed(xmlReader);
}
else
{
value = MetaType.GetStringFromXml((XmlReader)value);
objValue = MetaType.GetStringFromXml(xmlReader);
}
}
else if (currentType == typeof(char[]))
{
value = new string((char[])value);
objValue = new string(value.GenericCast<T, char[]>());
}
else if (currentType == typeof(SqlChars))
{
value = new string(((SqlChars)value).Value);
objValue = new string(value.GenericCast<T, SqlChars>().Value);
}
else if (value is TextReader textReader && allowStreaming)
{
coercedToDataFeed = true;
value = new TextDataFeed(textReader);
objValue = new TextDataFeed(textReader);
}
else
{
value = Convert.ChangeType(value, destinationType.ClassType, null);
objValue = Convert.ChangeType(value, destinationType.ClassType, null);
}
}
else if ((destinationType.DbType == DbType.Currency) && (currentType == typeof(string)))
{
value = decimal.Parse((string)value, NumberStyles.Currency, null);
objValue = decimal.Parse(value.GenericCast<T, string>(), NumberStyles.Currency, null);
}
else if ((currentType == typeof(SqlBytes)) && (destinationType.ClassType == typeof(byte[])))
{
typeChanged = false; // Do nothing
}
else if ((currentType == typeof(string)) && (destinationType.SqlDbType == SqlDbType.Time))
{
value = TimeSpan.Parse((string)value);
objValue = TimeSpan.Parse(value.GenericCast<T, string>());
}
else if ((currentType == typeof(string)) && (destinationType.SqlDbType == SqlDbType.DateTimeOffset))
{
value = DateTimeOffset.Parse((string)value, (IFormatProvider)null);
objValue = DateTimeOffset.Parse(value.GenericCast<T, string>(), (IFormatProvider)null);
}
else if ((currentType == typeof(DateTime)) && (destinationType.SqlDbType == SqlDbType.DateTimeOffset))
{
value = new DateTimeOffset((DateTime)value);
objValue = new DateTimeOffset(value.GenericCast<T, DateTime>());
}
else if (
TdsEnums.SQLTABLE == destinationType.TDSType &&
Expand All @@ -2182,11 +2190,11 @@ value is IEnumerable<SqlDataRecord>
else if (destinationType.ClassType == typeof(byte[]) && allowStreaming && value is Stream stream)
{
coercedToDataFeed = true;
value = new StreamDataFeed(stream);
objValue = new StreamDataFeed(stream);
}
else
{
value = Convert.ChangeType(value, destinationType.ClassType, null);
objValue = Convert.ChangeType(value, destinationType.ClassType, null);
}
}
catch (Exception e)
Expand All @@ -2201,8 +2209,8 @@ value is IEnumerable<SqlDataRecord>
}

Debug.Assert(allowStreaming || !coercedToDataFeed, "Streaming is not allowed, but type was coerced into a data feed");
Debug.Assert(value.GetType() == currentType ^ typeChanged, "Incorrect value for typeChanged");
return value;
Debug.Assert(objValue == null || objValue.GetType() == currentType ^ typeChanged, "Incorrect value for typeChanged");
return typeChanged;
}

private static int StringSize(object value, bool isSqlType)
Expand Down
Loading