Skip to content
Draft
Show file tree
Hide file tree
Changes from all 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 @@ -536,6 +536,7 @@ internal string UdtTypeName
// Construction for all types that do not have variable attributes
private void Construct(string name, SqlDbType dbType, bool useServerDefault, bool isUniqueKey, SortOrder columnSortOrder, int sortOrdinal)
{
ValidateGlobalizationInvariantLcid(dbType);
AssertNameIsValid(name);

ValidateSortOrder(columnSortOrder, sortOrdinal);
Expand Down Expand Up @@ -586,6 +587,7 @@ private void Construct(string name, SqlDbType dbType, bool useServerDefault, boo
// Construction for all types that vary by user-specified length (not Udts)
private void Construct(string name, SqlDbType dbType, long maxLength, bool useServerDefault, bool isUniqueKey, SortOrder columnSortOrder, int sortOrdinal)
{
ValidateGlobalizationInvariantLcid(dbType);
AssertNameIsValid(name);

ValidateSortOrder(columnSortOrder, sortOrdinal);
Expand Down Expand Up @@ -929,6 +931,16 @@ private void ValidateSortOrder(SortOrder columnSortOrder, int sortOrdinal)
}
}

private static void ValidateGlobalizationInvariantLcid(SqlDbType dbType)
{
if (LocalAppContextSwitches.GlobalizationInvariantMode &&
(dbType is SqlDbType.Char or SqlDbType.VarChar or SqlDbType.Text
or SqlDbType.NChar or SqlDbType.NVarChar or SqlDbType.NText))
{
throw SQL.GlobalizationInvariantModeRequiresLcid();
}
}

/// <include file='../../../../../../../doc/snippets/Microsoft.Data.SqlClient.Server/SqlMetaData.xml' path='docs/members[@name="SqlMetaData"]/AdjustValue27/*' />
public short Adjust(short value)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,10 @@ internal static Exception ChangePasswordUseOfUnallowedKey(string key)
{
return ADP.InvalidOperation(StringsHelper.GetString(Strings.SQL_ChangePasswordUseOfUnallowedKey, key));
}
internal static Exception GlobalizationInvariantModeRequiresLcid()
{
return ADP.NotSupported(StringsHelper.GetString(Strings.SQL_GlobalizationInvariantModeRequiresLcid));
}
internal static Exception GlobalizationInvariantModeNotSupported()
{
return ADP.NotSupported(StringsHelper.GetString(Strings.SQL_GlobalizationInvariantModeNotSupported));
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions src/Microsoft.Data.SqlClient/src/Resources/Strings.resx
Original file line number Diff line number Diff line change
Expand Up @@ -2091,6 +2091,9 @@
<data name="SqlAppContextSwitchManager_InvalidValue" xml:space="preserve">
<value>Exception occurred while trying to set the AppContext Switch '{0}'={1}.</value>
</data>
<data name="SQL_GlobalizationInvariantModeRequiresLcid" xml:space="preserve">
<value>When Globalization Invariant Mode is enabled, SqlMetaData instances describing character or text types must specify an LCID.</value>
</data>
<data name="SQL_GlobalizationInvariantModeNotSupported" xml:space="preserve">
<value>Globalization Invariant Mode is not supported.</value>
</data>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using System.Globalization;
using System.Reflection;
using Microsoft.Data.SqlClient.Server;
using Microsoft.Data.SqlClient.Tests.Common;
using Xunit;

namespace Microsoft.Data.SqlClient.Tests
Expand Down Expand Up @@ -228,6 +229,43 @@ public void ConstructorWithDefaultLocale()
Assert.Equal(0, metaData.SortOrdinal);
}

#if NET
[Theory]
[MemberData(nameof(ConstructorCharData))]
[MemberData(nameof(ConstructorTextData))]
public void ConstructorWithDefaultLocale_ThrowsInInvariantGlobalizationMode(SqlDbType dbType)
{
using LocalAppContextSwitchesHelper helper = new();

helper.GlobalizationInvariantMode = true;

Assert.Throws<NotSupportedException>(() => new SqlMetaData("col1", dbType));
Assert.Throws<NotSupportedException>(() => new SqlMetaData("col1", dbType, true, true, SortOrder.Ascending, 0));
Assert.Throws<NotSupportedException>(() => new SqlMetaData("col1", dbType, 0));
Assert.Throws<NotSupportedException>(() => new SqlMetaData("col1", dbType, 0, true, true, SortOrder.Ascending, 0));
}

[Theory]
[MemberData(nameof(ConstructorCharData))]
public void ConstructorWithMaxLengthAndExplicitLocale_DoesNotThrowInInvariantGlobalizationMode(SqlDbType dbType)
{
using LocalAppContextSwitchesHelper helper = new();

helper.GlobalizationInvariantMode = true;
ConstructorWithMaxLengthAndLocale(dbType);
}

[Theory]
[MemberData(nameof(ConstructorTextData))]
public void ConstructorWithMaxLengthTextExplicitLocale_DoesNotThrowInInvariantGlobalizationMode(SqlDbType dbType)
{
using LocalAppContextSwitchesHelper helper = new();

helper.GlobalizationInvariantMode = true;
ConstructorWithMaxLengthTextAndLocale(dbType);
}
#endif

[Fact]
public void ConstructorWithDefaultLocaleInvalidType_Throws()
{
Expand Down
Loading