Skip to content

Commit

Permalink
Split out unmapped scalars configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
AndriySvyryd committed Aug 16, 2021
1 parent d4bb806 commit e250649
Show file tree
Hide file tree
Showing 32 changed files with 782 additions and 404 deletions.
11 changes: 5 additions & 6 deletions src/EFCore.Cosmos/Query/Internal/ISqlExpressionFactory.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq.Expressions;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Storage;

#nullable disable warnings

namespace Microsoft.EntityFrameworkCore.Cosmos.Query.Internal
{
/// <summary>
Expand All @@ -25,15 +22,17 @@ public interface ISqlExpressionFactory
/// 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>
SqlExpression ApplyTypeMapping(SqlExpression? sqlExpression, CoreTypeMapping typeMapping);
[return: NotNullIfNotNull("sqlExpression")]
SqlExpression? ApplyTypeMapping(SqlExpression? sqlExpression, CoreTypeMapping? typeMapping);

/// <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>
SqlExpression ApplyDefaultTypeMapping(SqlExpression? sqlExpression);
[return: NotNullIfNotNull("sqlExpression")]
SqlExpression? ApplyDefaultTypeMapping(SqlExpression? sqlExpression);

/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
Expand Down
3 changes: 1 addition & 2 deletions src/EFCore.Cosmos/Query/Internal/InExpression.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Linq.Expressions;
using Microsoft.EntityFrameworkCore.Query;
using Microsoft.EntityFrameworkCore.Storage;
Expand All @@ -27,7 +26,7 @@ public InExpression(
SqlExpression item,
bool negated,
SqlExpression values,
CoreTypeMapping typeMapping)
CoreTypeMapping? typeMapping)
: base(typeof(bool), typeMapping)
{
Item = item;
Expand Down
58 changes: 28 additions & 30 deletions src/EFCore.Cosmos/Query/Internal/SqlExpressionFactory.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Diagnostics.CodeAnalysis;
using System.Linq.Expressions;
using Microsoft.EntityFrameworkCore.Cosmos.Internal;
using Microsoft.EntityFrameworkCore.Infrastructure;
Expand All @@ -12,8 +10,6 @@
using Microsoft.EntityFrameworkCore.Query;
using Microsoft.EntityFrameworkCore.Storage;

#nullable disable

namespace Microsoft.EntityFrameworkCore.Cosmos.Query.Internal
{
/// <summary>
Expand All @@ -38,7 +34,7 @@ public SqlExpressionFactory(ITypeMappingSource typeMappingSource, IModel model)
{
_typeMappingSource = typeMappingSource;
_model = model;
_boolTypeMapping = typeMappingSource.FindMapping(typeof(bool));
_boolTypeMapping = typeMappingSource.FindMapping(typeof(bool), model)!;
}

/// <summary>
Expand All @@ -47,19 +43,21 @@ public SqlExpressionFactory(ITypeMappingSource typeMappingSource, IModel model)
/// 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 virtual SqlExpression ApplyDefaultTypeMapping(SqlExpression sqlExpression)
[return: NotNullIfNotNull("sqlExpression")]
public virtual SqlExpression? ApplyDefaultTypeMapping(SqlExpression? sqlExpression)
=> sqlExpression == null
|| sqlExpression.TypeMapping != null
? sqlExpression
: ApplyTypeMapping(sqlExpression, _model.FindMapping(sqlExpression.Type));
: ApplyTypeMapping(sqlExpression, _typeMappingSource.FindMapping(sqlExpression.Type, _model));

/// <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 virtual SqlExpression ApplyTypeMapping(SqlExpression sqlExpression, CoreTypeMapping typeMapping)
[return: NotNullIfNotNull("sqlExpression")]
public virtual SqlExpression? ApplyTypeMapping(SqlExpression? sqlExpression, CoreTypeMapping? typeMapping)
{
if (sqlExpression == null
|| sqlExpression.TypeMapping != null)
Expand Down Expand Up @@ -96,7 +94,7 @@ public virtual SqlExpression ApplyTypeMapping(SqlExpression sqlExpression, CoreT

private SqlExpression ApplyTypeMappingOnSqlConditional(
SqlConditionalExpression sqlConditionalExpression,
CoreTypeMapping typeMapping)
CoreTypeMapping? typeMapping)
{
return sqlConditionalExpression.Update(
sqlConditionalExpression.Test,
Expand All @@ -106,11 +104,11 @@ private SqlExpression ApplyTypeMappingOnSqlConditional(

private SqlExpression ApplyTypeMappingOnSqlUnary(
SqlUnaryExpression sqlUnaryExpression,
CoreTypeMapping typeMapping)
CoreTypeMapping? typeMapping)
{
SqlExpression operand;
Type resultType;
CoreTypeMapping resultTypeMapping;
CoreTypeMapping? resultTypeMapping;
switch (sqlUnaryExpression.OperatorType)
{
case ExpressionType.Equal:
Expand Down Expand Up @@ -150,14 +148,14 @@ when sqlUnaryExpression.IsLogicalNot():

private SqlExpression ApplyTypeMappingOnSqlBinary(
SqlBinaryExpression sqlBinaryExpression,
CoreTypeMapping typeMapping)
CoreTypeMapping? typeMapping)
{
var left = sqlBinaryExpression.Left;
var right = sqlBinaryExpression.Right;

Type resultType;
CoreTypeMapping resultTypeMapping;
CoreTypeMapping inferredTypeMapping;
CoreTypeMapping? resultTypeMapping;
CoreTypeMapping? inferredTypeMapping;
switch (sqlBinaryExpression.OperatorType)
{
case ExpressionType.Equal:
Expand All @@ -170,8 +168,8 @@ private SqlExpression ApplyTypeMappingOnSqlBinary(
inferredTypeMapping = ExpressionExtensions.InferTypeMapping(left, right)
// We avoid object here since the result does not get typeMapping from outside.
?? (left.Type != typeof(object)
? _model.FindMapping(left.Type)
: _model.FindMapping(right.Type));
? _typeMappingSource.FindMapping(left.Type, _model)
: _typeMappingSource.FindMapping(right.Type, _model));
resultType = typeof(bool);
resultTypeMapping = _boolTypeMapping;
}
Expand Down Expand Up @@ -226,7 +224,7 @@ public virtual SqlBinaryExpression MakeBinary(
ExpressionType operatorType,
SqlExpression left,
SqlExpression right,
CoreTypeMapping typeMapping)
CoreTypeMapping? typeMapping)
{
var returnType = left.Type;
switch (operatorType)
Expand Down Expand Up @@ -325,7 +323,7 @@ public virtual SqlBinaryExpression OrElse(SqlExpression left, SqlExpression righ
/// 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 virtual SqlBinaryExpression Add(SqlExpression left, SqlExpression right, CoreTypeMapping typeMapping = null)
public virtual SqlBinaryExpression Add(SqlExpression left, SqlExpression right, CoreTypeMapping? typeMapping = null)
=> MakeBinary(ExpressionType.Add, left, right, typeMapping);

/// <summary>
Expand All @@ -334,7 +332,7 @@ public virtual SqlBinaryExpression Add(SqlExpression left, SqlExpression right,
/// 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 virtual SqlBinaryExpression Subtract(SqlExpression left, SqlExpression right, CoreTypeMapping typeMapping = null)
public virtual SqlBinaryExpression Subtract(SqlExpression left, SqlExpression right, CoreTypeMapping? typeMapping = null)
=> MakeBinary(ExpressionType.Subtract, left, right, typeMapping);

/// <summary>
Expand All @@ -343,7 +341,7 @@ public virtual SqlBinaryExpression Subtract(SqlExpression left, SqlExpression ri
/// 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 virtual SqlBinaryExpression Multiply(SqlExpression left, SqlExpression right, CoreTypeMapping typeMapping = null)
public virtual SqlBinaryExpression Multiply(SqlExpression left, SqlExpression right, CoreTypeMapping? typeMapping = null)
=> MakeBinary(ExpressionType.Multiply, left, right, typeMapping);

/// <summary>
Expand All @@ -352,7 +350,7 @@ public virtual SqlBinaryExpression Multiply(SqlExpression left, SqlExpression ri
/// 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 virtual SqlBinaryExpression Divide(SqlExpression left, SqlExpression right, CoreTypeMapping typeMapping = null)
public virtual SqlBinaryExpression Divide(SqlExpression left, SqlExpression right, CoreTypeMapping? typeMapping = null)
=> MakeBinary(ExpressionType.Divide, left, right, typeMapping);

/// <summary>
Expand All @@ -361,7 +359,7 @@ public virtual SqlBinaryExpression Divide(SqlExpression left, SqlExpression righ
/// 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 virtual SqlBinaryExpression Modulo(SqlExpression left, SqlExpression right, CoreTypeMapping typeMapping = null)
public virtual SqlBinaryExpression Modulo(SqlExpression left, SqlExpression right, CoreTypeMapping? typeMapping = null)
=> MakeBinary(ExpressionType.Modulo, left, right, typeMapping);

/// <summary>
Expand All @@ -370,7 +368,7 @@ public virtual SqlBinaryExpression Modulo(SqlExpression left, SqlExpression righ
/// 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 virtual SqlBinaryExpression And(SqlExpression left, SqlExpression right, CoreTypeMapping typeMapping = null)
public virtual SqlBinaryExpression And(SqlExpression left, SqlExpression right, CoreTypeMapping? typeMapping = null)
=> MakeBinary(ExpressionType.And, left, right, typeMapping);

/// <summary>
Expand All @@ -379,14 +377,14 @@ public virtual SqlBinaryExpression And(SqlExpression left, SqlExpression right,
/// 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 virtual SqlBinaryExpression Or(SqlExpression left, SqlExpression right, CoreTypeMapping typeMapping = null)
public virtual SqlBinaryExpression Or(SqlExpression left, SqlExpression right, CoreTypeMapping? typeMapping = null)
=> MakeBinary(ExpressionType.Or, left, right, typeMapping);

private SqlUnaryExpression MakeUnary(
ExpressionType operatorType,
SqlExpression operand,
Type type,
CoreTypeMapping typeMapping = null)
CoreTypeMapping? typeMapping = null)
{
return (SqlUnaryExpression)ApplyTypeMapping(new SqlUnaryExpression(operatorType, operand, type, null), typeMapping);
}
Expand Down Expand Up @@ -415,7 +413,7 @@ public virtual SqlBinaryExpression IsNotNull(SqlExpression operand)
/// 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 virtual SqlUnaryExpression Convert(SqlExpression operand, Type type, CoreTypeMapping typeMapping = null)
public virtual SqlUnaryExpression Convert(SqlExpression operand, Type type, CoreTypeMapping? typeMapping = null)
=> MakeUnary(ExpressionType.Convert, operand, type, typeMapping);

/// <summary>
Expand Down Expand Up @@ -446,7 +444,7 @@ public virtual SqlFunctionExpression Function(
string functionName,
IEnumerable<SqlExpression> arguments,
Type returnType,
CoreTypeMapping typeMapping = null)
CoreTypeMapping? typeMapping = null)
{
var typeMappedArguments = new List<SqlExpression>();

Expand Down Expand Up @@ -486,7 +484,7 @@ public virtual SqlConditionalExpression Condition(SqlExpression test, SqlExpress
/// </summary>
public virtual InExpression In(SqlExpression item, SqlExpression values, bool negated)
{
var typeMapping = item.TypeMapping ?? _model.FindMapping(item.Type);
var typeMapping = item.TypeMapping ?? _typeMappingSource.FindMapping(item.Type, _model);

item = ApplyTypeMapping(item, typeMapping);
values = ApplyTypeMapping(values, typeMapping);
Expand All @@ -500,7 +498,7 @@ public virtual InExpression In(SqlExpression item, SqlExpression values, bool ne
/// 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 virtual SqlConstantExpression Constant(object value, CoreTypeMapping typeMapping = null)
public virtual SqlConstantExpression Constant(object? value, CoreTypeMapping? typeMapping = null)
=> new(Expression.Constant(value), typeMapping);

/// <summary>
Expand Down
5 changes: 1 addition & 4 deletions src/EFCore.Design/Design/Internal/DatabaseOperations.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using Microsoft.EntityFrameworkCore.Scaffolding;
using Microsoft.EntityFrameworkCore.Utilities;
Expand Down Expand Up @@ -93,7 +90,7 @@ public virtual SavedModelFiles ScaffoldContext(
: outputDir;

var services = _servicesBuilder.Build(provider);
using var scope = services.CreateScope();
using var scope = services.CreateScope();

var scaffolder = scope.ServiceProvider.GetRequiredService<IReverseEngineerScaffolder>();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
namespace Microsoft.EntityFrameworkCore
{
/// <summary>
/// Relational database specific extension methods for <see cref="PropertyBuilder" />.
/// Relational database specific extension methods for <see cref="PropertiesConfigurationBuilder" />.
/// </summary>
public static class PropertiesConfigurationBuilderExtensions
{
Expand Down
40 changes: 0 additions & 40 deletions src/EFCore.Relational/Extensions/RelationalModelExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,10 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using Microsoft.EntityFrameworkCore.Diagnostics;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Metadata.Internal;
using Microsoft.EntityFrameworkCore.Storage;
using Microsoft.EntityFrameworkCore.Utilities;

// ReSharper disable once CheckNamespace
Expand Down Expand Up @@ -521,40 +516,5 @@ public static void SetCollation(this IMutableModel model, string? value)
/// <returns> The configuration source for the collation. </returns>
public static ConfigurationSource? GetCollationConfigurationSource(this IConventionModel model)
=> model.FindAnnotation(RelationalAnnotationNames.Collation)?.GetConfigurationSource();

/// <summary>
/// Gets the relational database type for a given object, throwing if no mapping is found.
/// </summary>
/// <param name="model"> The model. </param>
/// <param name="value"> The object to get the mapping for. </param>
/// <returns> The type mapping to be used. </returns>
public static RelationalTypeMapping GetMappingForValue(
this IModel model,
object? value)
=> value == null
|| value == DBNull.Value
? RelationalTypeMapping.NullMapping
: model.GetMapping(value.GetType());

/// <summary>
/// Gets the relational database type for a given .NET type, throwing if no mapping is found.
/// </summary>
/// <param name="model"> The model. </param>
/// <param name="clrType"> The type to get the mapping for. </param>
/// <returns> The type mapping to be used. </returns>
public static RelationalTypeMapping GetMapping(
this IModel model,
Type clrType)
{
Check.NotNull(clrType, nameof(clrType));

var mapping = model.FindMapping(clrType);
if (mapping != null)
{
return (RelationalTypeMapping)mapping;
}

throw new InvalidOperationException(RelationalStrings.UnsupportedType(clrType.ShortDisplayName()));
}
}
}
Loading

0 comments on commit e250649

Please sign in to comment.