Repository navigation
Conversation
| /// <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 CreateCopyT([NotNull] string storeType, int? size) |
There was a problem hiding this comment.
Also can we name this something different?
There was a problem hiding this comment.
Don't get bogged down in the names of things - they'll probably change anyway. This particular one is just a hold-over from my editing - there will end up being just one CreateCopy() method - though I'm disappointed I have to repeat it everywhere.
| IsUnicode, | ||
| size, | ||
| HasNonDefaultUnicode, | ||
| hasNonDefaultSize: size != Size); |
There was a problem hiding this comment.
Is this correct?
What if you create a nondefault size ByteMapping and then call CreateCopy on that with same size?
There was a problem hiding this comment.
This is how the code works right now. I agree it's not very robust - but the copies are only ever made from the default-sized version - so it works, but as I say not very robust.
| private IReadOnlyDictionary<string, object> _parametersValues; | ||
| private ParameterNameGenerator _parameterNameGenerator; | ||
| private RelationalTypeMapping _typeMapping; | ||
| protected RelationalTypeMapping _typeMapping; |
There was a problem hiding this comment.
Why this became protected?
There was a problem hiding this comment.
Because the SQLServerQuerySqlGenerator needed to do:
var existingTypeMapping = _typeMapping;
_typeMapping = null;
// do whatever needs doing - now will get TypeMapping from value, not from _typeMapping
_typeMapping = existingTypeMapping;
|
@lajones So I think the "ambient" type mapping is there to make sure that we correlate type mappings with metadata from our model for things like string comparisons. For example, if we have a string literal being compared to a non-unicode string property, then we want to make sure that the literal is created as non-unicode. Otherwise the query may not be sargable. Therefore, I think we want to use the ambient mapping whenever we can. However:
|
| } | ||
|
|
||
| return GenerateSqlLiteralValue((dynamic)value); | ||
| return "Unknown type " + value.GetType() + |
There was a problem hiding this comment.
Can this just be something like string.Format(CultureInfo.InvariantCulture, "{0}", value);? If not, should throw an exception? Or be an abstract method maybe using the template method pattern?
There was a problem hiding this comment.
We can talk about what should be there finally. But that's just there right now as an error-catcher while I was updating the code - anything except null should have been handled by the parent class - or we are trying to use the wrong value on the wrong TypeMapping - which is a situation I want to spot.
| public override string GenerateSqlLiteral([CanBeNull]object value) | ||
| { | ||
| return value != null | ||
| ? ((long)value).ToString(CultureInfo.InvariantCulture) |
There was a problem hiding this comment.
Splitting this into a method that external code calls and a template method that can be overridden could be a way to handle the null check in a single place.
There was a problem hiding this comment.
Not following - could you explain? null is a valid value to be passed to all the methods - but I wanted to have only 1 place that actually returned "NULL".
There was a problem hiding this comment.
Something like this:
public virtual string GenerateSqlLiteral([CanBeNull]object value)
=> value == null ? "NULL" : GenerateNonNullSqlLiteral(value);
protected virtual string GenerateNonNullSqlLiteral([NotNull] object value)
{
Check.NotNull(value, nameof(value));
return string.Format(CultureInfo.InvariantCulture, "{0}", value);
}There was a problem hiding this comment.
OK. That makes sense.
| /// </summary> | ||
| public class SqliteDateTimeOffsetTypeMapping : DateTimeOffsetTypeMapping | ||
| { | ||
| private const string DateTimeOffsetFormatConst = @"yyyy\-MM\-dd HH\:mm\:ss.FFFFFFFzzz"; |
There was a problem hiding this comment.
@"yyyy-MM-dd HH:mm:ss.FFFFFFFzzz" [](start = 56, length = 38)
Could this be factored to not need another class, but to allow the format string to be passed to the base class, or overridden in a template method?
There was a problem hiding this comment.
That is already done. Unfortunately the string (and not just the format of the DateTimeOffset) that is output is different too. That could be changed so that the whole string is the format and then what you say would work (the way above is the way we currently do it).
| { typeof(sbyte), new IntTypeMapping(_integerTypeName) }, | ||
| { typeof(short), new ShortTypeMapping(_integerTypeName) }, | ||
| { typeof(uint), new LongTypeMapping(_integerTypeName) }, | ||
| { typeof(ulong), new LongTypeMapping(_integerTypeName) }, //LAJLAJ - ulong does not fit in long?? |
There was a problem hiding this comment.
{ typeof(ulong), new LongTypeMapping(_integerTypeName) } [](start = 20, length = 56)
ULongTypeMapping?
There was a problem hiding this comment.
Yes. I was trying to avoid having another full set for TypeMappings for the ulong|uint... set of types but they might be necessary. This was also just a note to myself to investigate if setting up a ulong property on Sqlite fails right now.
|
|
||
| private readonly SqlServerMaxLengthMapping _nvarchar450 | ||
| = new SqlServerMaxLengthMapping("nvarchar(450)", typeof(string), dbType: null, unicode: true, size: 450); | ||
| private readonly SqlServerStringTypeMapping _nvarchar450 //LAJLAJ - shouldn't dbType be DbType.String? |
There was a problem hiding this comment.
//LAJLAJ - shouldn't dbType be DbType.String? [](start = 64, length = 46)
Nope. Things will break if the DbType is set explicitly. We went though a process to get to figure out which DbType values to use, and when to not use them at all. We might have got some wrong, but we shouldn't change this without being sure the change it correct.
There was a problem hiding this comment.
OK. I'll bear that in mind. Though that is putting functionality on the DbType getter that was not immediately apparent when editing it / to providers who provide their own TypeMappings.
I would like to know why the Guid one wasn't set though - that seems like it would only ever map to DbType.Guid?
There was a problem hiding this comment.
Probably because, for SQL Server, we found the most pragmatic approach to be not setting the DbType unless it was needed.
Can we factor it in a way that the DbType is not assumed? That is, regardless of what CLR type the mapping is for, the DbType is always passed in the constructor? I know this seems wrong, but the way DbTypes is used is very provider-specific, so in reality we can't really assume a DbType just because we know the CLR type.
There was a problem hiding this comment.
OK. I can make the DbType not have a default value in all of the constructors.
Note: as well as the usage of what's really a mapping DbType -> store type mentioned in #8609, DbType is used when creating DbParameter - some of that is shared between providers in RelationalTypeMapping.CreateParameter() so it would be nice to add a comment to the DbType getter along the lines of "a null value implies xyz..." so that anyone so sets it to null knows what others will assume about it.
| @p3: 00000001-0000-0000-0000-000000000001 (DbType = String) | ||
| @p1: 00000000-0000-0000-0003-000000000001 | ||
| @p3: 00000001-0000-0000-0000-000000000001 | ||
|
|
There was a problem hiding this comment.
We shouldn't be seeing any changes in these tests--we should fix the root cause of the DbType being different here.
There was a problem hiding this comment.
This is the ambient _typeMapper issue. As you see the value being passed was a Guid but the TypeMapping that was generating it had a DbType set to String. This only worked because string TypeMappings knew how to generate Guid literals. That will no longer be the case with the new setup. It's clearer in the other test where the value being passed is 1 but it is nevertheless being treated as a string.
|
@ajcvickers All the comments so far are about how to improve what's here. Please note I would also like a general comment on whether to pursue this approach at all or whether to stick with the existing double-dispatch approach given the downsides we encounter in |
|
I'm happy with this approach. |
This is working well. Good suggestion 👍 |
|
This has been replaced by PR #8631 - see there for further comments. |
Note: this is just work in progress - there are still lots of commented out lines, classes that should be deleted, tests which haven't been updated... The purpose of this is just to get feedback on the approach so far - its benefits and costs.
I've been working on removing the double-dispatch that we currently use in
RelationalTypeMappingwhen generating literals, in favor of one where each CLR-type has its own kind ofXyzTypeMappingclass - which knows how to map between CLR type, store type and DbType and knows how to generate SQL (and later C#) literals of its own type. This is in contrast to the existing setup where any TypeMapping class knows how to generate literals for any value that is passed to it regardless of whether the type of the value is a match to that originally intended when the TypeMapping was created.The good things:
This nicely separates out concerns: e.g. IntTypeMapping now knows how to deal with int's and does not have to carry around the baggage of knowing what to do with e.g. strings. If you have to change how mappings/generation of literals are handled for a given provider then you just inherit from the appropriate TypeMapping and specify your own special behavior. This makes each class much cleaner and smaller.
It's arguable, but I also think this also sets us up better/more cleanly to support completely new type mappings in the future.
Things that need work:
I got the
Sqlite.TestsandSqlServer.Teststest projects to work using this new approach. But the functional tests still have ~20 failures. They all seem to be related to the query pipeline specificallyDefaultQuerySqlGeneratorand its subclasses.DefaultQuerySqlGeneratorcontains a_typeMapping. It uses this to store off the "ambient" type mapping it is using. Then there is theGetTypeMapping()method below:This is used in numerous places and, as you see above, will return the "ambient" TypeMapping if one exists rather than a new type mapping based on the value. This often happens when interpreting a
ConstantExpressione.g. the1inTOP(1)or when interpreting the arguments ofSomeFunction(SomeArgs).@maumar, @smitpatel and I talked and we tried to work around this by entries you'll see in e.g.
DefaultQuerySqlGeneratorwhere I've put code like this:The problem is that sometimes you need the ambient type mapping rather than what you might generate from the value by itself. E.g. when trying to produce SQL that looks like this:
(by default if you ask for the SQL store type for a string you get a unicode string which has
N'Values');Whereas sometimes it's important that they not be the "ambient" TypeMapping. E.g. in
even if the 'expression' results in an ambient TypeMapping of
SqlServerStringTypeMappingit's important (in the new setup) thatstartandlengthuse an integer type mapping, e.g.LongTypeMapping. (This works in the existing setup because you'd would have e.g. an ambientSqlServerMaxLengthMappingwhich didn't care when you passed it integer values - the double-dispatch would output the correct SQL literal anyway.)I've not managed to solve this problem yet - I don't think it's insuperable, but it might mean more changes to the query pipeline code than I anticipated when I started this.
So do we want to continue to pursue this, or do we decide that double-dispatch is acceptable (perhaps even preferable?) to help with scenarios like the above?