Skip to content

Commit

Permalink
Generate compiled relational model
Browse files Browse the repository at this point in the history
Fixes #24896
  • Loading branch information
AndriySvyryd committed Mar 11, 2023
1 parent 383a92a commit 3b66bed
Show file tree
Hide file tree
Showing 33 changed files with 2,246 additions and 310 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -345,8 +345,7 @@ private string CreateModelBuilder(
var methods = methodBuilder.ToString();
if (!string.IsNullOrEmpty(methods))
{
mainBuilder.AppendLine()
.AppendLines(methods);
mainBuilder.AppendLines(methods);
}
}

Expand Down Expand Up @@ -1465,13 +1464,18 @@ private static void CreateAnnotations<TAnnotatable>(
{
process(
annotatable,
parameters with { Annotations = annotatable.GetAnnotations().ToDictionary(a => a.Name, a => a.Value), IsRuntime = false });
parameters with
{
Annotations = annotatable.GetAnnotations().ToDictionary(a => a.Name, a => a.Value),
IsRuntime = false
});

process(
annotatable,
parameters with
{
Annotations = annotatable.GetRuntimeAnnotations().ToDictionary(a => a.Name, a => a.Value), IsRuntime = true
Annotations = annotatable.GetRuntimeAnnotations().ToDictionary(a => a.Name, a => a.Value),
IsRuntime = true
});
}

Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ protected override void InitializeModel(IModel model, bool designTime, bool prev
{
model.SetRuntimeAnnotation(RelationalAnnotationNames.ModelDependencies, RelationalDependencies.RelationalModelDependencies);
}
else
else if (model.FindRuntimeAnnotation(RelationalAnnotationNames.RelationalModel) == null)
{
RelationalModel.Add(
model,
Expand Down
3 changes: 1 addition & 2 deletions src/EFCore.Relational/Metadata/IColumnBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,7 @@ public interface IColumnBase : IAnnotatable
/// </summary>
/// <returns>The comparer.</returns>
ValueComparer ProviderValueComparer
=> PropertyMappings.First().Property
.GetProviderValueComparer();
=> PropertyMappings.First().Property.GetProviderValueComparer();

/// <summary>
/// Returns the property mapping for the given entity type.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,14 @@ public interface IRelationalAnnotationProvider
/// <param name="designTime">Whether the model should contain design-time configuration.</param>
IEnumerable<IAnnotation> For(IStoreFunction function, bool designTime);

/// <summary>
/// Gets provider-specific annotations for the given <see cref="IStoreFunctionParameter" />.
/// </summary>
/// <param name="parameter">The parameter.</param>
/// <returns>The annotations.</returns>
/// <param name="designTime">Whether the model should contain design-time configuration.</param>
IEnumerable<IAnnotation> For(IStoreFunctionParameter parameter, bool designTime);

/// <summary>
/// Gets provider-specific annotations for the given <see cref="IFunctionColumn" />.
/// </summary>
Expand Down
8 changes: 6 additions & 2 deletions src/EFCore.Relational/Metadata/Internal/Column.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,13 @@ public class Column : ColumnBase<ColumnMapping>, IColumn
/// 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 Column(string name, string type, Table table, RelationalTypeMapping? storeTypeMapping = null)
: base(name, type, table, storeTypeMapping)
public Column(string name, string type, Table table,
RelationalTypeMapping? storeTypeMapping = null,
ValueComparer? providerValueComparer = null,
ColumnAccessors? accessors = null)
: base(name, type, table, storeTypeMapping, providerValueComparer)
{
_accessors = accessors;
}

/// <summary>
Expand Down
27 changes: 25 additions & 2 deletions src/EFCore.Relational/Metadata/Internal/ColumnBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,23 @@ public class ColumnBase<TColumnMappingBase> : Annotatable, IColumnBase
{
private Type? _providerClrType;
private RelationalTypeMapping? _storeTypeMapping;
private ValueComparer? _providerValueComparer;

/// <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 ColumnBase(string name, string type, TableBase table, RelationalTypeMapping? storeTypeMapping = null)
public ColumnBase(string name, string type, TableBase table,
RelationalTypeMapping? storeTypeMapping = null,
ValueComparer? providerValueComparer = null)
{
Name = name;
StoreType = type;
Table = table;
_storeTypeMapping = storeTypeMapping;
_providerValueComparer = providerValueComparer;
}

/// <summary>
Expand Down Expand Up @@ -99,7 +103,16 @@ public virtual Type ProviderClrType
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public virtual RelationalTypeMapping StoreTypeMapping
=> _storeTypeMapping ??= PropertyMappings.First().TypeMapping;
=> _storeTypeMapping ??= GetDefaultStoreTypeMapping();

/// <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>
protected virtual RelationalTypeMapping GetDefaultStoreTypeMapping()
=> PropertyMappings.First().TypeMapping;

/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
Expand Down Expand Up @@ -155,4 +168,14 @@ ITableBase IColumnBase.Table
[DebuggerStepThrough]
get => Table;
}

/// <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>
ValueComparer IColumnBase.ProviderValueComparer
=> _providerValueComparer ??=
(PropertyMappings.FirstOrDefault()?.Property.GetProviderValueComparer() ?? StoreTypeMapping.ProviderValueComparer);
}
28 changes: 17 additions & 11 deletions src/EFCore.Relational/Metadata/Internal/JsonColumn.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Text.Json;
using Microsoft.EntityFrameworkCore.Update.Internal;

namespace Microsoft.EntityFrameworkCore.Metadata.Internal;

/// <summary>
Expand All @@ -17,11 +20,23 @@ public class JsonColumn : Column, IColumn
/// 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 JsonColumn(string name, Table table, RelationalTypeMapping storeTypeMapping)
: base(name, storeTypeMapping.StoreType, table, storeTypeMapping)
public JsonColumn(string name, string type, Table table,
RelationalTypeMapping? storeTypeMapping = null,
ValueComparer? providerValueComparer = null,
ColumnAccessors? accessors = null)
: base(name, type, table, storeTypeMapping, providerValueComparer, accessors)
{
}

/// <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>
protected override RelationalTypeMapping GetDefaultStoreTypeMapping()
=> (RelationalTypeMapping)Table.Model.Model.GetModelDependencies().TypeMappingSource.FindMapping(typeof(JsonElement))!;

/// <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
Expand Down Expand Up @@ -139,15 +154,6 @@ bool IColumn.IsRowVersion
string? IColumn.Collation
=> null;

/// <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>
ValueComparer IColumnBase.ProviderValueComparer
=> StoreTypeMapping.ProviderValueComparer;

/// <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
Expand Down
38 changes: 38 additions & 0 deletions src/EFCore.Relational/Metadata/Internal/JsonColumnBase.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Text.Json;
using Microsoft.EntityFrameworkCore.Update.Internal;

namespace Microsoft.EntityFrameworkCore.Metadata.Internal;

/// <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 JsonColumnBase : ColumnBase<ColumnMappingBase>
{
/// <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 JsonColumnBase(string name, string type, TableBase table,
RelationalTypeMapping? storeTypeMapping = null,
ValueComparer? providerValueComparer = null)
: base(name, type, table, storeTypeMapping, providerValueComparer)
{
}

/// <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>
protected override RelationalTypeMapping GetDefaultStoreTypeMapping()
=> (RelationalTypeMapping)Table.Model.Model.GetModelDependencies().TypeMappingSource.FindMapping(typeof(JsonElement))!;
}
17 changes: 15 additions & 2 deletions src/EFCore.Relational/Metadata/Internal/JsonViewColumn.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Text.Json;

namespace Microsoft.EntityFrameworkCore.Metadata.Internal;

/// <summary>
Expand All @@ -17,8 +19,19 @@ public class JsonViewColumn : ViewColumn, IViewColumn
/// 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 JsonViewColumn(string name, View view, RelationalTypeMapping storeTypeMapping)
: base(name, storeTypeMapping.StoreType, view, storeTypeMapping)
public JsonViewColumn(string name, string type, View view,
RelationalTypeMapping? storeTypeMapping = null,
ValueComparer? providerValueComparer = null)
: base(name, type, view, storeTypeMapping, providerValueComparer)
{
}

/// <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>
protected override RelationalTypeMapping GetDefaultStoreTypeMapping()
=> (RelationalTypeMapping)Table.Model.Model.GetModelDependencies().TypeMappingSource.FindMapping(typeof(JsonElement))!;
}
Loading

0 comments on commit 3b66bed

Please sign in to comment.