-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
These changes address issue #4425 while at the same time re-introducing the parsing of sizes in store type names such that these can then be used in parameters for #4134. The mapping of strings and binary types based on facets has been split out such that the information provided by the type mapper can be interpreted more easily by the scaffolding code. This also simplifies the type mapper APIs themselves. A new class ScaffoldingTypeMapper has been introduced that can be used to determine what needs to be scaffolded for a given store type. This has not yet been integrated into scaffolding, but has unit tests to check it gives the correct answers. Scaffolding must provide this service with the full store type name (e.g. nvarchar(256)" together with information about whether the property will be a key or index or a rowversion. The service then gives back data indicating whether the type is inferred (need not be explicitly set) and, if so, whether max length and/or unicode factes needs to be set.
- Loading branch information
1 parent
c6e0f04
commit 22b57df
Showing
35 changed files
with
1,338 additions
and
415 deletions.
There are no files selected for viewing
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
84 changes: 84 additions & 0 deletions
84
src/Microsoft.EntityFrameworkCore.Relational.Design/ScaffoldingTypeMapper.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,84 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using System; | ||
using JetBrains.Annotations; | ||
using Microsoft.EntityFrameworkCore.Storage; | ||
using Microsoft.EntityFrameworkCore.Utilities; | ||
|
||
namespace Microsoft.EntityFrameworkCore.Scaffolding | ||
{ | ||
public class ScaffoldingTypeMapper | ||
{ | ||
public ScaffoldingTypeMapper([NotNull] IRelationalTypeMapper typeMapper) | ||
{ | ||
Check.NotNull(typeMapper, nameof(typeMapper)); | ||
|
||
TypeMapper = typeMapper; | ||
} | ||
|
||
protected virtual IRelationalTypeMapper TypeMapper { get; } | ||
|
||
public virtual TypeScaffoldingInfo FindMapping( | ||
[NotNull] string storeType, | ||
bool keyOrIndex, | ||
bool rowVersion) | ||
{ | ||
Check.NotEmpty(storeType, nameof(storeType)); | ||
|
||
var mapping = TypeMapper.FindMapping(storeType); | ||
if (mapping == null) | ||
{ | ||
return null; | ||
} | ||
|
||
if (mapping.ClrType == typeof(byte[]) | ||
&& TypeMapper.ByteArrayMapper != null) | ||
{ | ||
var byteArrayMapping = TypeMapper.ByteArrayMapper.FindMapping(rowVersion, keyOrIndex, mapping.Size); | ||
|
||
if (byteArrayMapping.StoreType.Equals(storeType, StringComparison.OrdinalIgnoreCase)) | ||
{ | ||
return new TypeScaffoldingInfo( | ||
mapping.ClrType, | ||
inferred: true, | ||
scaffoldUnicode: null, | ||
scaffoldMaxLength: byteArrayMapping.HasNonDefaultSize ? byteArrayMapping.Size : null); | ||
} | ||
} | ||
else if (mapping.ClrType == typeof(string) | ||
&& TypeMapper.StringMapper != null) | ||
{ | ||
var stringMapping = TypeMapper.StringMapper.FindMapping(mapping.IsUnicode, keyOrIndex, mapping.Size); | ||
|
||
if (stringMapping.StoreType.Equals(storeType, StringComparison.OrdinalIgnoreCase)) | ||
{ | ||
return new TypeScaffoldingInfo( | ||
mapping.ClrType, | ||
inferred: true, | ||
scaffoldUnicode: stringMapping.HasNonDefaultUnicode ? (bool?)stringMapping.IsUnicode : null, | ||
scaffoldMaxLength: stringMapping.HasNonDefaultSize ? stringMapping.Size : null); | ||
} | ||
} | ||
else | ||
{ | ||
var defaultMapping = TypeMapper.GetMapping(mapping.ClrType); | ||
|
||
if (defaultMapping.StoreType.Equals(storeType, StringComparison.OrdinalIgnoreCase)) | ||
{ | ||
return new TypeScaffoldingInfo( | ||
mapping.ClrType, | ||
inferred: true, | ||
scaffoldUnicode: null, | ||
scaffoldMaxLength: null); | ||
} | ||
} | ||
|
||
return new TypeScaffoldingInfo( | ||
mapping.ClrType, | ||
inferred: false, | ||
scaffoldUnicode: null, | ||
scaffoldMaxLength: null); | ||
} | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
src/Microsoft.EntityFrameworkCore.Relational.Design/TypeScaffoldingInfo.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,27 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using System; | ||
using JetBrains.Annotations; | ||
using Microsoft.EntityFrameworkCore.Utilities; | ||
|
||
namespace Microsoft.EntityFrameworkCore.Scaffolding | ||
{ | ||
public class TypeScaffoldingInfo | ||
{ | ||
public TypeScaffoldingInfo([NotNull] Type clrType, bool inferred, bool? scaffoldUnicode, int? scaffoldMaxLength) | ||
{ | ||
Check.NotNull(clrType, nameof(clrType)); | ||
|
||
IsInferred = inferred; | ||
ScaffoldUnicode = scaffoldUnicode; | ||
ScaffoldMaxLength = scaffoldMaxLength; | ||
ClrType = clrType; | ||
} | ||
|
||
public virtual Type ClrType { get; } | ||
public virtual bool IsInferred { get; } | ||
public virtual bool? ScaffoldUnicode { get; } | ||
public virtual int? ScaffoldMaxLength { get; } | ||
} | ||
} |
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
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
59 changes: 59 additions & 0 deletions
59
src/Microsoft.EntityFrameworkCore.Relational/Storage/ByteArrayRelationalTypeMapper.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,59 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using System; | ||
using System.Collections.Concurrent; | ||
using JetBrains.Annotations; | ||
|
||
namespace Microsoft.EntityFrameworkCore.Storage | ||
{ | ||
public class ByteArrayRelationalTypeMapper : IByteArrayRelationalTypeMapper | ||
{ | ||
private readonly ConcurrentDictionary<int, RelationalTypeMapping> _boundedMappings | ||
= new ConcurrentDictionary<int, RelationalTypeMapping>(); | ||
|
||
public ByteArrayRelationalTypeMapper( | ||
int maxBoundedLength, | ||
[NotNull] RelationalTypeMapping defaultMapping, | ||
[CanBeNull] RelationalTypeMapping unboundedMapping, | ||
[CanBeNull] RelationalTypeMapping keyMapping, | ||
[CanBeNull] RelationalTypeMapping rowVersionMapping, | ||
[NotNull] Func<int, RelationalTypeMapping> createBoundedMapping) | ||
{ | ||
MaxBoundedLength = maxBoundedLength; | ||
DefaultMapping = defaultMapping; | ||
UnboundedMapping = unboundedMapping; | ||
KeyMapping = keyMapping; | ||
RowVersionMapping = rowVersionMapping; | ||
CreateBoundedMapping = createBoundedMapping; | ||
} | ||
|
||
public virtual int MaxBoundedLength { get; } | ||
public virtual RelationalTypeMapping DefaultMapping { get; } | ||
public virtual RelationalTypeMapping UnboundedMapping { get; } | ||
public virtual RelationalTypeMapping KeyMapping { get; } | ||
public virtual RelationalTypeMapping RowVersionMapping { get; } | ||
public virtual Func<int, RelationalTypeMapping> CreateBoundedMapping { get; } | ||
|
||
public virtual RelationalTypeMapping FindMapping(bool rowVersion, bool keyOrIndex, int? size) | ||
{ | ||
if (rowVersion | ||
&& RowVersionMapping != null) | ||
{ | ||
return RowVersionMapping; | ||
} | ||
|
||
var defaultMapping = keyOrIndex && KeyMapping != null ? KeyMapping : DefaultMapping; | ||
|
||
if (size.HasValue | ||
&& size != defaultMapping.Size) | ||
{ | ||
return size <= MaxBoundedLength | ||
? _boundedMappings.GetOrAdd(size.Value, CreateBoundedMapping) | ||
: UnboundedMapping; | ||
} | ||
|
||
return defaultMapping; | ||
} | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
src/Microsoft.EntityFrameworkCore.Relational/Storage/IByteArrayRelationalTypeMapper.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,10 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
namespace Microsoft.EntityFrameworkCore.Storage | ||
{ | ||
public interface IByteArrayRelationalTypeMapper | ||
{ | ||
RelationalTypeMapping FindMapping(bool rowVersion, bool keyOrIndex, int? size); | ||
} | ||
} |
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
10 changes: 10 additions & 0 deletions
10
src/Microsoft.EntityFrameworkCore.Relational/Storage/IStringRelationalTypeMapper.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,10 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
namespace Microsoft.EntityFrameworkCore.Storage | ||
{ | ||
public interface IStringRelationalTypeMapper | ||
{ | ||
RelationalTypeMapping FindMapping(bool unicode, bool keyOrIndex, int? maxLength); | ||
} | ||
} |
40 changes: 0 additions & 40 deletions
40
src/Microsoft.EntityFrameworkCore.Relational/Storage/RelationalSizedTypeMapping.cs
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.