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

Support specifying collations on columns and databases #20602

Merged
merged 3 commits into from
Apr 14, 2020
Merged
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 @@ -203,6 +203,14 @@ protected virtual void Generate([NotNull] AddColumnOperation operation, [NotNull
.Append(Code.Literal(operation.Comment));
}

if (operation.Collation != null)
{
builder
.AppendLine(",")
.Append("collation: ")
.Append(Code.Literal(operation.Collation));
}

builder.Append(")");

Annotations(operation.GetAnnotations(), builder);
Expand Down Expand Up @@ -561,6 +569,14 @@ protected virtual void Generate([NotNull] AlterColumnOperation operation, [NotNu
.Append(Code.Literal(operation.Comment));
}

if (operation.Collation != null)
{
builder
.AppendLine(",")
.Append("collation: ")
.Append(Code.Literal(operation.Collation));
}

if (operation.OldColumn.ClrType != null)
{
builder.AppendLine(",")
Expand Down Expand Up @@ -654,6 +670,14 @@ protected virtual void Generate([NotNull] AlterColumnOperation operation, [NotNu
.Append(Code.Literal(operation.OldColumn.Comment));
}

if (operation.OldColumn.Collation != null)
{
builder
.AppendLine(",")
.Append("oldCollation: ")
.Append(Code.Literal(operation.OldColumn.Collation));
}

builder.Append(")");

Annotations(operation.GetAnnotations(), builder);
Expand All @@ -671,10 +695,28 @@ protected virtual void Generate([NotNull] AlterDatabaseOperation operation, [Not
Check.NotNull(operation, nameof(operation));
Check.NotNull(builder, nameof(builder));

builder.Append(".AlterDatabase()");
builder.Append(".AlterDatabase(");

using (builder.Indent())
{
if (operation.Collation != null)
{
builder
.AppendLine()
.Append("collation: ")
.Append(Code.Literal(operation.Collation));
}

if (operation.OldDatabase.Collation != null)
{
builder
.AppendLine(",")
.Append("oldCollation: ")
.Append(Code.Literal(operation.OldDatabase.Collation));
}

builder.Append(")");

Annotations(operation.GetAnnotations(), builder);
OldAnnotations(operation.OldDatabase.GetAnnotations(), builder);
}
Expand Down Expand Up @@ -1125,6 +1167,13 @@ protected virtual void Generate([NotNull] CreateTableOperation operation, [NotNu
.Append(Code.Literal(column.Comment));
}

if (column.Collation != null)
{
builder
.Append(", collation: ")
.Append(Code.Literal(column.Collation));
}

builder.Append(")");

using (builder.Indent())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -570,6 +570,12 @@ protected virtual void GeneratePropertyAnnotations([NotNull] IProperty property,
nameof(RelationalPropertyBuilderExtensions.HasComment),
stringBuilder);

GenerateFluentApiForAnnotation(
ref annotations,
RelationalAnnotationNames.Collation,
nameof(RelationalPropertyBuilderExtensions.UseCollation),
stringBuilder);

GenerateFluentApiForAnnotation(
ref annotations,
CoreAnnotationNames.MaxLength,
Expand Down
14 changes: 11 additions & 3 deletions src/EFCore.Design/Scaffolding/Internal/CSharpDbContextGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -641,6 +641,7 @@ private void GenerateProperty(IProperty property, bool useDataAnnotations)
RemoveAnnotation(ref annotations, RelationalAnnotationNames.DefaultValue);
RemoveAnnotation(ref annotations, RelationalAnnotationNames.DefaultValueSql);
RemoveAnnotation(ref annotations, RelationalAnnotationNames.Comment);
RemoveAnnotation(ref annotations, RelationalAnnotationNames.Collation);
RemoveAnnotation(ref annotations, RelationalAnnotationNames.ComputedColumnSql);
RemoveAnnotation(ref annotations, RelationalAnnotationNames.IsFixedLength);
RemoveAnnotation(ref annotations, RelationalAnnotationNames.TableColumnMappings);
Expand Down Expand Up @@ -737,18 +738,25 @@ private void GenerateProperty(IProperty property, bool useDataAnnotations)
$"({_code.Literal(property.GetDefaultValueSql())})");
}

if (property.GetComputedColumnSql() != null)
{
lines.Add(
$".{nameof(RelationalPropertyBuilderExtensions.HasComputedColumnSql)}" +
$"({_code.Literal(property.GetComputedColumnSql())})");
}

if (property.GetComment() != null)
{
lines.Add(
$".{nameof(RelationalPropertyBuilderExtensions.HasComment)}" +
$"({_code.Literal(property.GetComment())})");
}

if (property.GetComputedColumnSql() != null)
if (property.GetCollation() != null)
{
lines.Add(
$".{nameof(RelationalPropertyBuilderExtensions.HasComputedColumnSql)}" +
$"({_code.Literal(property.GetComputedColumnSql())})");
$".{nameof(RelationalPropertyBuilderExtensions.UseCollation)}" +
$"({_code.Literal(property.GetCollation())})");
}

var valueGenerated = property.ValueGenerated;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -488,6 +488,11 @@ protected virtual PropertyBuilder VisitColumn([NotNull] EntityTypeBuilder builde
property.HasComment(column.Comment);
}

if (column.Collation != null)
{
property.HasComment(column.Collation);
}

if (!(column.Table.PrimaryKey?.Columns.Contains(column) ?? false))
{
property.IsRequired(!column.IsNullable);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -424,5 +424,66 @@ public static bool CanSetMaxIdentifierLength(

return modelBuilder.CanSetAnnotation(RelationalAnnotationNames.MaxIdentifierLength, length, fromDataAnnotation);
}

/// <summary>
/// Configures the database collation, which will be used by all columns without an explicit collation.
/// </summary>
/// <param name="modelBuilder"> The model builder. </param>
/// <param name="collation"> The collation. </param>
/// <returns> The same builder instance so that multiple calls can be chained. </returns>
public static ModelBuilder UseCollation(
[NotNull] this ModelBuilder modelBuilder,
[CanBeNull] string collation)
{
Check.NotNull(modelBuilder, nameof(modelBuilder));
Check.NullButNotEmpty(collation, nameof(collation));

modelBuilder.Model.SetCollation(collation);

return modelBuilder;
}

/// <summary>
/// Configures the database collation, which will be used by all columns without an explicit collation.
/// </summary>
/// <param name="modelBuilder"> The model builder. </param>
/// <param name="collation"> The collation. </param>
/// <param name="fromDataAnnotation"> Indicates whether the configuration was specified using a data annotation. </param>
/// <returns>
/// The same builder instance if the configuration was applied,
/// <c>null</c> otherwise.
/// </returns>
public static IConventionModelBuilder UseCollation(
[NotNull] this IConventionModelBuilder modelBuilder,
[CanBeNull] string collation,
bool fromDataAnnotation = false)
{
if (modelBuilder.CanSetCollation(collation, fromDataAnnotation))
{
modelBuilder.Metadata.SetCollation(collation, fromDataAnnotation);

return modelBuilder;
}

return null;
}

/// <summary>
/// Returns a value indicating whether the given collation can be set as default.
/// </summary>
/// <param name="modelBuilder"> The model builder. </param>
/// <param name="collation"> The collation. </param>
/// <param name="fromDataAnnotation"> Indicates whether the configuration was specified using a data annotation. </param>
/// <returns> <c>true</c> if the given collation can be set as default. </returns>
public static bool CanSetCollation(
[NotNull] this IConventionModelBuilder modelBuilder,
[CanBeNull] string collation,
bool fromDataAnnotation = false)
{
Check.NotNull(modelBuilder, nameof(modelBuilder));
Check.NullButNotEmpty(collation, nameof(collation));

return modelBuilder.CanSetAnnotation(RelationalAnnotationNames.Collation, collation, fromDataAnnotation);
}
}
}
41 changes: 41 additions & 0 deletions src/EFCore.Relational/Extensions/RelationalModelExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -410,5 +410,46 @@ public static IEnumerable<IMutableDbFunction> GetDbFunctions([NotNull] this IMut
/// <param name="model"> The model to get the functions in. </param>
public static IEnumerable<IConventionDbFunction> GetDbFunctions([NotNull] this IConventionModel model)
=> DbFunction.GetDbFunctions((Model)Check.NotNull(model, nameof(model)));

/// <summary>
/// Returns the database collation.
/// </summary>
/// <param name="model"> The model to get the collation for. </param>
/// <returns> The collation. </returns>
public static string GetCollation([NotNull] this IModel model)
=> (string)model[RelationalAnnotationNames.Collation];

/// <summary>
/// Sets the database collation.
/// </summary>
/// <param name="model"> The model to set the collation for. </param>
/// <param name="value"> The value to set. </param>
public static void SetCollation([NotNull] this IMutableModel model, [CanBeNull] string value)
=> model.SetOrRemoveAnnotation(
RelationalAnnotationNames.Collation,
Check.NullButNotEmpty(value, nameof(value)));

/// <summary>
/// Sets the database collation.
/// </summary>
/// <param name="model"> The model to set the collation for. </param>
/// <param name="value"> The value to set. </param>
/// <param name="fromDataAnnotation"> Indicates whether the configuration was specified using a data annotation. </param>
/// <returns> The configured collation. </returns>
public static string SetCollation([NotNull] this IConventionModel model, [CanBeNull] string value, bool fromDataAnnotation = false)
{
model.SetOrRemoveAnnotation(
RelationalAnnotationNames.Collation,
Check.NullButNotEmpty(value, nameof(value)), fromDataAnnotation);
return value;
}

/// <summary>
/// Returns the configuration source for the collation.
/// </summary>
/// <param name="model"> The model to find configuration source for. </param>
/// <returns> The configuration source for the collation. </returns>
public static ConfigurationSource? GetCollationConfigurationSource([NotNull] this IConventionModel model)
=> model.FindAnnotation(RelationalAnnotationNames.Collation)?.GetConfigurationSource();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -578,5 +578,76 @@ public static bool CanSetComment(
RelationalAnnotationNames.Comment,
comment,
fromDataAnnotation);

/// <summary>
/// Configures the property to use the given collation. The database column will be be created with the given
/// collation, and it will be used implicitly in all collation-sensitive operations.
/// </summary>
/// <param name="propertyBuilder"> The builder for the property being configured. </param>
/// <param name="collation"> The collation for the column. </param>
/// <returns>The same builder instance so that multiple calls can be chained.</returns>
public static PropertyBuilder UseCollation([NotNull] this PropertyBuilder propertyBuilder, [CanBeNull] string collation)
{
Check.NotNull(propertyBuilder, nameof(propertyBuilder));
Check.NullButNotEmpty(collation, nameof(collation));

propertyBuilder.Metadata.SetCollation(collation);

return propertyBuilder;
}

/// <summary>
/// Configures the property to use the given collation. The database column will be be created with the given
/// collation, and it will be used implicitly in all collation-sensitive operations.
/// </summary>
/// <param name="propertyBuilder"> The builder for the property being configured. </param>
/// <param name="collation"> The collation for the column. </param>
/// <returns>The same builder instance so that multiple calls can be chained.</returns>
public static PropertyBuilder<TProperty> UseCollation<TProperty>(
[NotNull] this PropertyBuilder<TProperty> propertyBuilder, [CanBeNull] string collation)
=> (PropertyBuilder<TProperty>)UseCollation((PropertyBuilder)propertyBuilder, collation);

/// <summary>
/// Configures the property to use the given collation. The database column will be be created with the given
/// collation, and it will be used implicitly in all collation-sensitive operations.
/// </summary>
/// <param name="propertyBuilder"> The builder for the property being configured. </param>
/// <param name="collation"> The collation. </param>
/// <param name="fromDataAnnotation"> Indicates whether the configuration was specified using a data annotation. </param>
/// <returns>
/// The same builder instance if the configuration was applied,
/// <c>null</c> otherwise.
/// </returns>
public static IConventionPropertyBuilder UseCollation(
[NotNull] this IConventionPropertyBuilder propertyBuilder,
[CanBeNull] string collation,
bool fromDataAnnotation = false)
{
if (propertyBuilder.CanSetCollation(collation, fromDataAnnotation))
{
propertyBuilder.Metadata.SetCollation(collation, fromDataAnnotation);

return propertyBuilder;
}

return null;
}

/// <summary>
/// Returns a value indicating whether the given value can be set as the collation.
/// </summary>
/// <param name="propertyBuilder"> The builder for the property being configured. </param>
/// <param name="collation"> The collation. </param>
/// <param name="fromDataAnnotation"> Indicates whether the configuration was specified using a data annotation. </param>
/// <returns> <c>true</c> if the given value can be set as default for the column. </returns>
public static bool CanSetCollation(
[NotNull] this IConventionPropertyBuilder propertyBuilder,
[CanBeNull] string collation,
bool fromDataAnnotation = false)
{
Check.NotNull(propertyBuilder, nameof(propertyBuilder));

return propertyBuilder.CanSetAnnotation(RelationalAnnotationNames.Collation, collation, fromDataAnnotation);
}
}
}
Loading