-
Notifications
You must be signed in to change notification settings - Fork 225
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support a few more type mappings in the compiled model
Part of #2949
- Loading branch information
Showing
8 changed files
with
275 additions
and
19 deletions.
There are no files selected for viewing
133 changes: 133 additions & 0 deletions
133
src/EFCore.PG/Design/Internal/NpgsqlCSharpRuntimeAnnotationCodeGenerator.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
using Microsoft.EntityFrameworkCore.Design.Internal; | ||
using Npgsql.EntityFrameworkCore.PostgreSQL.Storage.Internal.Mapping; | ||
|
||
namespace Npgsql.EntityFrameworkCore.PostgreSQL.Design.Internal; | ||
|
||
#pragma warning disable EF1001 // RelationalCSharpRuntimeAnnotationCodeGenerator is pubternal | ||
|
||
// We override RelationalCSharpRuntimeAnnotationCodeGenerator to provide support for some of our type mappings. | ||
// The relational support looks for a Default static property on the type mapping type, and then calls Clone() on it with any facets | ||
// that are different from the default. | ||
// That works well, unless the type mapping type has some additional state outside of the well-known facets (which are in | ||
// RelationalTypeMappingParameters). For example, we have type mappings which have an NpgsqlDbType | ||
|
||
/// <summary> | ||
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to | ||
/// the same compatibility standards as public APIs. It may be changed or removed without notice in | ||
/// any release. You should only use it directly in your code with extreme caution and knowing that | ||
/// doing so can result in application failures when updating to a new Entity Framework Core release. | ||
/// </summary> | ||
public class NpgsqlCSharpRuntimeAnnotationCodeGenerator : RelationalCSharpRuntimeAnnotationCodeGenerator | ||
{ | ||
/// <summary> | ||
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to | ||
/// the same compatibility standards as public APIs. It may be changed or removed without notice in | ||
/// any release. You should only use it directly in your code with extreme caution and knowing that | ||
/// doing so can result in application failures when updating to a new Entity Framework Core release. | ||
/// </summary> | ||
public NpgsqlCSharpRuntimeAnnotationCodeGenerator(CSharpRuntimeAnnotationCodeGeneratorDependencies dependencies, | ||
RelationalCSharpRuntimeAnnotationCodeGeneratorDependencies relationalDependencies) | ||
: base(dependencies, relationalDependencies) | ||
{ | ||
} | ||
|
||
/// <summary> | ||
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to | ||
/// the same compatibility standards as public APIs. It may be changed or removed without notice in | ||
/// any release. You should only use it directly in your code with extreme caution and knowing that | ||
/// doing so can result in application failures when updating to a new Entity Framework Core release. | ||
/// </summary> | ||
public override bool Create( | ||
CoreTypeMapping typeMapping, | ||
CSharpRuntimeAnnotationCodeGeneratorParameters parameters, | ||
ValueComparer? valueComparer = null, | ||
ValueComparer? keyValueComparer = null, | ||
ValueComparer? providerValueComparer = null) | ||
{ | ||
var result = base.Create(typeMapping, parameters, valueComparer, keyValueComparer, providerValueComparer); | ||
|
||
var mainBuilder = parameters.MainBuilder; | ||
|
||
var npgsqlDbTypeBasedDefaultInstance = typeMapping switch | ||
{ | ||
NpgsqlStringTypeMapping => NpgsqlStringTypeMapping.Default, | ||
NpgsqlULongTypeMapping => NpgsqlULongTypeMapping.Default, | ||
// NpgsqlMultirangeTypeMapping => NpgsqlMultirangeTypeMapping.Default, | ||
_ => (INpgsqlTypeMapping?)null | ||
}; | ||
|
||
if (npgsqlDbTypeBasedDefaultInstance is not null) | ||
{ | ||
var npgsqlDbType = ((INpgsqlTypeMapping)typeMapping).NpgsqlDbType; | ||
|
||
if (npgsqlDbType != npgsqlDbTypeBasedDefaultInstance.NpgsqlDbType) | ||
{ | ||
mainBuilder.AppendLine(";"); | ||
|
||
mainBuilder.Append( | ||
$"{parameters.TargetName}.TypeMapping = (({typeMapping.GetType().Name}){parameters.TargetName}.TypeMapping).Clone("); | ||
|
||
mainBuilder | ||
.Append(nameof(NpgsqlTypes)) | ||
.Append(".") | ||
.Append(nameof(NpgsqlDbType)) | ||
.Append(".") | ||
.Append(npgsqlDbType.ToString()); | ||
|
||
mainBuilder | ||
.Append(")") | ||
.DecrementIndent(); | ||
} | ||
|
||
} | ||
|
||
switch (typeMapping) | ||
{ | ||
#pragma warning disable CS0618 // NpgsqlConnection.GlobalTypeMapper is obsolete | ||
case NpgsqlEnumTypeMapping enumTypeMapping: | ||
if (enumTypeMapping.NameTranslator != NpgsqlConnection.GlobalTypeMapper.DefaultNameTranslator) | ||
{ | ||
throw new NotSupportedException( | ||
"Mapped enums are only supported in the compiled model if they use the default name translator"); | ||
} | ||
break; | ||
#pragma warning restore CS0618 | ||
|
||
case NpgsqlRangeTypeMapping rangeTypeMapping: | ||
{ | ||
var defaultInstance = NpgsqlRangeTypeMapping.Default; | ||
|
||
var npgsqlDbTypeDifferent = rangeTypeMapping.NpgsqlDbType != defaultInstance.NpgsqlDbType; | ||
var subtypeTypeMappingIsDifferent = rangeTypeMapping.SubtypeMapping != defaultInstance.SubtypeMapping; | ||
|
||
if (npgsqlDbTypeDifferent || subtypeTypeMappingIsDifferent) | ||
{ | ||
mainBuilder.AppendLine(";"); | ||
|
||
mainBuilder.AppendLine( | ||
$"{parameters.TargetName}.TypeMapping = ((NpgsqlRangeTypeMapping){parameters.TargetName}.TypeMapping).Clone(") | ||
.IncrementIndent(); | ||
|
||
mainBuilder | ||
.Append(nameof(NpgsqlTypes)) | ||
.Append(".") | ||
.Append(nameof(NpgsqlDbType)) | ||
.Append(".") | ||
.Append(rangeTypeMapping.NpgsqlDbType.ToString()) | ||
.AppendLine(","); | ||
|
||
Create(rangeTypeMapping.SubtypeMapping, parameters); | ||
|
||
mainBuilder | ||
.Append(")") | ||
.DecrementIndent(); | ||
} | ||
|
||
break; | ||
} | ||
|
||
} | ||
|
||
return result; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.