Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Proposed fix to #7172 - Consider non-default column collation in table variable #29518

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -570,7 +570,6 @@ public override void Generate(IProperty property, CSharpRuntimeAnnotationCodeGen
{
annotations.Remove(RelationalAnnotationNames.ColumnOrder);
annotations.Remove(RelationalAnnotationNames.Comment);
annotations.Remove(RelationalAnnotationNames.Collation);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This preserves the collation annotation in the runtime for all relational providers, to solve a problem that's specific to the SQL Server one. If we want to fix the collation issue, we should try to find a way to preserve the annotation only for SQL Server.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will try to fix this.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I committed a fix that the annotation is only preserved for SQL Server.
I am not 100% happy that the GetCollation-ExtensionMethod does not throw InvalidOperationException(CoreStrings.RuntimeModelMissingData) for non SQL Server but could find a better solution.


if (annotations.TryGetAndRemove(
RelationalAnnotationNames.RelationalOverrides,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1250,9 +1250,7 @@ public static void SetComment(this IMutableProperty property, string? comment)
/// <param name="property">The property.</param>
/// <returns>The collation for the column this property is mapped to.</returns>
public static string? GetCollation(this IReadOnlyProperty property)
=> (property is RuntimeProperty)
? throw new InvalidOperationException(CoreStrings.RuntimeModelMissingData)
: (string?)property.FindAnnotation(RelationalAnnotationNames.Collation)?.Value;
=> (string?)property.FindAnnotation(RelationalAnnotationNames.Collation)?.Value;

/// <summary>
/// Returns the collation to be used for the column.
Expand All @@ -1262,11 +1260,6 @@ public static void SetComment(this IMutableProperty property, string? comment)
/// <returns>The collation for the column this property is mapped to.</returns>
public static string? GetCollation(this IReadOnlyProperty property, in StoreObjectIdentifier storeObject)
{
if (property is RuntimeProperty)
{
throw new InvalidOperationException(CoreStrings.RuntimeModelMissingData);
}

var annotation = property.FindAnnotation(RelationalAnnotationNames.Collation);
return annotation != null
? (string?)annotation.Value
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,6 @@ private static RuntimeSequence Create(ISequence sequence, RuntimeModel runtimeMo
{
annotations.Remove(RelationalAnnotationNames.ColumnOrder);
annotations.Remove(RelationalAnnotationNames.Comment);
annotations.Remove(RelationalAnnotationNames.Collation);

if (annotations.TryGetValue(RelationalAnnotationNames.RelationalOverrides, out var relationalOverrides))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -767,12 +767,15 @@ protected virtual int MergeIntoMinimumThreshold
private static string GetTypeNameForCopy(IProperty property)
{
var typeName = property.GetColumnType();
var collation = property[RelationalAnnotationNames.Collation]?.ToString();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
var collation = property[RelationalAnnotationNames.Collation]?.ToString();
var collation = property.GetCollation();


return property.ClrType == typeof(byte[])
typeName = property.ClrType == typeof(byte[])
&& (typeName.Equals("rowversion", StringComparison.OrdinalIgnoreCase)
|| typeName.Equals("timestamp", StringComparison.OrdinalIgnoreCase))
? property.IsNullable ? "varbinary(8)" : "binary(8)"
: typeName;

return collation is not null ? $"{typeName} COLLATE {collation}" : typeName;
david-wimmer marked this conversation as resolved.
Show resolved Hide resolved
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1290,6 +1290,7 @@ public static RuntimeEntityType Create(RuntimeModel model, RuntimeEntityType? ba
overrides0.Add(StoreObjectIdentifier.Table(""Details"", null), detailsDetails);
details.AddAnnotation(""Relational:RelationalOverrides"", overrides0);

details.AddAnnotation(""Relational:Collation"", ""Latin1_General_CI_AI"");
details.AddAnnotation(""SqlServer:ValueGenerationStrategy"", SqlServerValueGenerationStrategy.None);

var number = runtimeEntityType.AddProperty(
Expand Down Expand Up @@ -1903,10 +1904,7 @@ public static void CreateAnnotations(RuntimeEntityType runtimeEntityType)
Assert.Equal(
CoreStrings.RuntimeModelMissingData,
Assert.Throws<InvalidOperationException>(() => detailsProperty.IsSparse()).Message);
Assert.Null(detailsProperty[RelationalAnnotationNames.Collation]);
Assert.Equal(
CoreStrings.RuntimeModelMissingData,
Assert.Throws<InvalidOperationException>(() => detailsProperty.GetCollation()).Message);
Assert.Equal("Latin1_General_CI_AI", detailsProperty.GetCollation());

var ownedFragment = referenceOwnedType.GetMappingFragments().Single();
Assert.Equal(nameof(OwnedType.Details), detailsProperty.GetColumnName(ownedFragment.StoreObject));
Expand Down