Repository navigation
Conversation
classes for each C# type.
|
Note: @smitpatel to make the |
| /// </summary> | ||
| /// <param name="storeType"> The name of the database type. </param> | ||
| public ByteTypeMapping([NotNull] string storeType) | ||
| : this(storeType, System.Data.DbType.Byte, unicode: false, size: null) |
There was a problem hiding this comment.
@ajcvickers Re your comment about requiring DbType on all the constructors. The 6-arg constructor below appears on every TypeMapping and that can always be used to specify the DbType if needed. But I found that I needed to pass in the DbType only for any DateTime or string type mappings. For all the rest there was only 1 suitable choice of DbType. So for the "convenience" constructors like the above I decided to not have it. Let me know if you strongly disagree though - I can put it back the other way, but I was always passing the same thing so I thought this was better for the "convenience" constructors.
There was a problem hiding this comment.
I don't think that there should be any place where we specify a DbType in our provider-agnostic code. I think that the DbType should be null by default for any type mapping. Then if SQL Server, or some other provider, needs to specify a DbType that should be done when it is constructed, in the code for that provider. In other words, I think we should maintain the previous approach which is that DbType is null unless some provider decides to explicitly set it.
There was a problem hiding this comment.
OK. I think they're useful. But I'll put them back the other way.
| //LAJLAJ What should we throw here? | ||
| return "ERROR: RelationalTypeMapping.GenerateSqlLiteral() received object of type " + value.GetType() +", with value " + value; | ||
| } | ||
|
|
There was a problem hiding this comment.
I'd like to throw here, but can I get opinions about the type of exception?
You should never reach here (we don't in any of our tests). If you do it's a sign that your provider has set up a TypeMapping badly.
While we could just always return "NULL" or we could just do ToString(CultureInfo.InvariantCulture) on whatever is passed in, that looks like it might lead to data corruption to me: any non-null value should have been handled by GenerateNonNullSqlLiteral() in whatever your current TypeMapping is. So I'd prefer to throw to alert the providers (and their users) to what's going wrong.
There was a problem hiding this comment.
ToString(CultureInfo.InvariantCulture) seems like a reasonable default here. It seems fine to say that this is what this method does if there is no override. In other words, provider code is not required to override this unless it needs to do something different for the literal. Maybe this could be combined with a string format as a constructor parameter at this level such that many types will not need to override this?
| { | ||
| private readonly SqlServerMaxLengthMapping _nvarcharmax | ||
| = new SqlServerMaxLengthMapping("nvarchar(max)", typeof(string), dbType: null, unicode: true, size: null); | ||
| private readonly SqlServerStringTypeMapping _nvarcharmax |
There was a problem hiding this comment.
I'd like to rename these to e.g. _unboundedString, _keyString, _unboundedAnsiString etc as in TestRelationalTypeMapper. Any objections?
There was a problem hiding this comment.
I'm going to go ahead and do this since I haven't seen any objections.
| => _typeMapping ?? Dependencies.RelationalTypeMapper.GetMappingForValue(value); | ||
| { | ||
| if (_typeMapping != null | ||
| && _typeMapping.ClrType.IsAssignableFrom(value.GetType())) |
There was a problem hiding this comment.
Is value known to be non-null?
There was a problem hiding this comment.
Good catch - we'd get an NRE. There is one case where it is not guaranteed so I'll fix to just return _typeMapper in that case. (It shouldn't matter which one we use as all TypeMappers know how to respond to a null value.)
| private RelationalTypeMapping GetTypeMapping(object value) | ||
| => _typeMapping ?? Dependencies.RelationalTypeMapper.GetMappingForValue(value); | ||
| { | ||
| if (_typeMapping != null |
There was a problem hiding this comment.
Consider conditional operator and expression body.
| /// The generated string. | ||
| /// </returns> | ||
| protected override string GenerateNonNullSqlLiteral([NotNull]char value) | ||
| => string.Format(CultureInfo.InvariantCulture, "'{0}'", value); |
There was a problem hiding this comment.
It seems that many of these subclasses do the same thing here--that is, use string.Format (or something that can be replaced with string.Format) to generate a value, but with a different format string. I wonder if we encapsulate this into a common class, then we will be able to remove a lot of these subclasses.
There was a problem hiding this comment.
Yes, we can do that. BTW do you know why the relational (i.e. non-provider specific) TypeMappings for the DateTime types return TIMESTAMP '...'? The addition of the TIMESTAMP literal seems odd.
There was a problem hiding this comment.
@lajones re TIMESTAMP '...' notation, it is a SQL standard thing. It can be used for literals in several common databases, e.g. MySQL and PostgreSQL, but doesn't work in SQL Server. The latter even assigns a completely different meaning to the TIMESTAMP type from the standard.
| /// not used in application code. | ||
| /// </para> | ||
| /// </summary> | ||
| public abstract class RelationalTypeMapping<T> : RelationalTypeMapping |
There was a problem hiding this comment.
Note sure the generic is needed. I can see how it is nice in some places, but on the fence as to whether it really adds enough value.
There was a problem hiding this comment.
I liked it as it helped produce compile-time rather than runtime errors. I've now found all of the compile-time errors I need for below, but it might be useful in the future when we or provider-writers produce new Type Mappings for other types.
There was a problem hiding this comment.
@ajcvickers I'm in the process of removing this. Let me know if my comment above changed your mind.
There was a problem hiding this comment.
I'm fine with leaving it or removing it. Up to you.
There was a problem hiding this comment.
OK. I'll keep it then - since I found it useful. Thanks.
|
Played with the code a bit this morning. I think this general approach is fine, but I think the amount of code could be reduced quite a bit by:
|
|
@ajcvickers OK. The approach you put forward above is clear (and answers a lot of my other q's above). Want to make sure that we envisage that as a good approach when, later on, we include generating C# literals and/or any special operations/functions/LINQ translators for each type mapping as well - as mentioned as one of the original goals of #7434. I'd rather not drop all those classes now only to find we need them again for that work... |
|
@bricelam Do you think we should keep all the subtypes now so that they are there for C# literal generation later? |
| .Append(typeMapping.StoreType); | ||
|
|
||
| // reset typeMapping so that we can use it with GenerateSqlLiteral() below | ||
| typeMapping = Dependencies.TypeMapper.GetMapping(typeof(long)); |
There was a problem hiding this comment.
While this is micro-optimization perhaps, but we should either move this outside of if or add better comment.
We are not resetting the type mapping but we are setting it as per operation.StartValue, which is long.
There was a problem hiding this comment.
I originally had it outside, but then we regen the TypeMapping even if it was the correct one to begin with. I can change the comment to "Set the typeMapping for use with operation.StartValue below" if you like?
There was a problem hiding this comment.
Hence "micro-optimization" 😉
I am fine with just updating comment. So that at least it becomes clearer why we update it and why we use typeof(long).
There was a problem hiding this comment.
Yeah - but I'd have had the complaint the other way around if I hadn't done it. I thought the comment was clear but I'll rephrase as mentioned above.
|
Re comment above about the |
| { typeof(DateTimeOffset), _datetimeoffset }, | ||
| { typeof(char), _int }, | ||
| { typeof(short), _smallint }, | ||
| { typeof(char), new CharTypeMapping("char(1)") }, |
There was a problem hiding this comment.
Why did this change from int? I don't think we should change it, but if we do, this should be nchar.
There was a problem hiding this comment.
Agree with @bricelam This isn't a string mapping, it's a numeric mapping.
There was a problem hiding this comment.
Agree on nchar vs char comment. I don't understand the rest though - why do we want to map to an integer when we have a SQL Server data type which maps directly to a single character?
Yes. I think we're mostly settled on the design for code literals, and it will require the subtypes. Doing this now prevents us from breaking providers that need to override built-in type mappings. |
| /// <param name="storeType"> The name of the database type. </param> | ||
| /// <param name="size"> The size of data the property is configured to store, or null if no size is configured. </param> | ||
| /// <returns> The newly created mapping. </returns> | ||
| public override RelationalTypeMapping CreateCopy([NotNull] string storeType, int? size) |
There was a problem hiding this comment.
Overriding methods should not have [NotNull] attributes. These only go where the method is defined--in the interface or base implementation of a method. R# will show them grayed out here.
|
Based on @bricelam's comment, let's leave the sub-classes. |
|
@ajcvickers @bricelam I will leave the sub-classes. |
|
@bricelam @ajcvickers @smitpatel Latest commit addresses all comments above except the outstanding one of to what underlying SQL Server type to map a |
| int? size, | ||
| bool hasNonDefaultUnicode = false, | ||
| bool hasNonDefaultSize = false) | ||
| : base(storeType, dbType, unicode, size, hasNonDefaultUnicode, hasNonDefaultSize) |
There was a problem hiding this comment.
This constructor seems unnecessary because bool mappings can never be Unicode or have size.
| /// <param name="size"> The size of data the property is configured to store, or null if no size is configured. </param> | ||
| /// <param name="hasNonDefaultUnicode"> A value indicating whether the Unicode setting has been manually configured to a non-default value. </param> | ||
| /// <param name="hasNonDefaultSize"> A value indicating whether the size setting has been manually configured to a non-default value. </param> | ||
| public ByteArrayTypeMapping( |
There was a problem hiding this comment.
Remove Unicode facets. Maybe collapse into one constructor with default values?
| /// <param name="size"> The size of data the property is configured to store, or null if no size is configured. </param> | ||
| /// <param name="hasNonDefaultUnicode"> A value indicating whether the Unicode setting has been manually configured to a non-default value. </param> | ||
| /// <param name="hasNonDefaultSize"> A value indicating whether the size setting has been manually configured to a non-default value. </param> | ||
| public ByteTypeMapping( |
There was a problem hiding this comment.
Not needed for same reasons.
| int? size, | ||
| bool hasNonDefaultUnicode = false, | ||
| bool hasNonDefaultSize = false) | ||
| : base(storeType, dbType, unicode, size, hasNonDefaultUnicode, hasNonDefaultSize) |
| int? size, | ||
| bool hasNonDefaultUnicode = false, | ||
| bool hasNonDefaultSize = false) | ||
| : base(storeType, dbType, unicode, size, hasNonDefaultUnicode, hasNonDefaultSize) |
There was a problem hiding this comment.
Not needed for same reasons.
| int? size, | ||
| bool hasNonDefaultUnicode = false, | ||
| bool hasNonDefaultSize = false) | ||
| : base(storeType, dbType, unicode, size, hasNonDefaultUnicode, hasNonDefaultSize) |
There was a problem hiding this comment.
Not needed for same reasons.
| int? size, | ||
| bool hasNonDefaultUnicode = false, | ||
| bool hasNonDefaultSize = false) | ||
| : base(storeType, dbType, unicode, size, hasNonDefaultUnicode, hasNonDefaultSize) |
There was a problem hiding this comment.
Not needed for same reasons.
| int? size, | ||
| bool hasNonDefaultUnicode = false, | ||
| bool hasNonDefaultSize = false) | ||
| : base(storeType, dbType, unicode, size, hasNonDefaultUnicode, hasNonDefaultSize) |
There was a problem hiding this comment.
Not needed for same reasons.
| int? size, | ||
| bool hasNonDefaultUnicode = false, | ||
| bool hasNonDefaultSize = false) | ||
| : base(storeType, dbType, unicode, size, hasNonDefaultUnicode, hasNonDefaultSize) |
There was a problem hiding this comment.
Not needed for same reasons.
|
@ajcvickers - Do we really need |
| /// <param name="size"> The size of data the property is configured to store, or null if no size is configured. </param> | ||
| /// <param name="hasNonDefaultUnicode"> A value indicating whether the Unicode setting has been manually configured to a non-default value. </param> | ||
| /// <param name="hasNonDefaultSize"> A value indicating whether the size setting has been manually configured to a non-default value. </param> | ||
| public SqlServerByteArrayTypeMapping( |
There was a problem hiding this comment.
We generally don't have doc comments like this on internal classes, but doesn't matter much.
| private static readonly LongTypeMapping _integer = new LongTypeMapping(_integerTypeName, dbType: null); | ||
| private static readonly DoubleTypeMapping _real = new DoubleTypeMapping(_realTypeName, dbType: null); | ||
| private static readonly ByteArrayTypeMapping _blob = new ByteArrayTypeMapping(_blobTypeName, dbType: null); | ||
| private static readonly StringTypeMapping _text = new StringTypeMapping(_textTypeName, dbType: null); |
There was a problem hiding this comment.
Can these now be moved inline? Or are the referenced elsewhere still?
There was a problem hiding this comment.
They are referenced in a couple of places.
| public virtual void GenerateSqlLiteral_returns_bool_literal_when_true() | ||
| { | ||
| var literal = new RelationalTypeMapping("bool", typeof(bool)).GenerateSqlLiteral(true); | ||
| var literal = new BoolTypeMapping("bool", null).GenerateSqlLiteral(true); |
There was a problem hiding this comment.
Is the null needed here? Does (or can) the parameter have a default?
| @p6: (DbType = DateTime) | ||
| @p7: (DbType = DateTime) | ||
| @p8: (DbType = DateTime2) | ||
| @p9: (DbType = String) |
There was a problem hiding this comment.
Do we know why this changed?
There was a problem hiding this comment.
Because DbType.String is the default (for SQL Server) if you don't specify a parameter type. But for SQL Server we now specify that properties of type DateTimeOffset now correspond to parameters of type DbType.DateTimeOffset. Is this wrong? I know we're not specifying in Relational but this is SQL Server and I thought it was right to be specific.
|
@smitpatel I'm fine with them going away if it's handled somewhere else. |
|
@smitpatel We agreed not to try to change the |
|
@lajones - After discussing with @ajcvickers about it. Certainly we don't need to block this PR for changes around HasNonDefaultUnicode/Size part. But we want to get away from it overall since its scaffolding only info. Instead scaffolding (ScaffoldingTypeMapper precisely), which already has information about the current store type and what would be the model generated data type for the property, can diff both of them to figure out whether to scaffold those facets or not. |
|
@smitpatel Yes. That's what we agreed yesterday. Do it separately after this is in. |
…e sbyte, ushort, uint and ulong mappings. Remove unnecessary constructors and allow DbType to be defaulted in constructors.
|
@ajcvickers All review comments actioned. New commit has all constructors updated as asked for above. This commit includes putting the SQL Server TypeMapping for |
|
@ajcvickers Missed updates to remove unicode references in |
ajcvickers
left a comment
There was a problem hiding this comment.
Please get into the preview2 branch so we can gather feedback.
|
@ajcvickers Thanks. I'll set about rebasing this based on preview2 and will submit asap. |
... for each C# type.
This is the replacement for PR #8612. Removing the double-dispatch method we had and using separate TypeMapping classes for each C# type. Sets us up to better address issue #7434 in the future.