-
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.
Add support for UDF Scalar functions
- Loading branch information
1 parent
6a4a951
commit a81805a
Showing
66 changed files
with
3,696 additions
and
768 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
88 changes: 88 additions & 0 deletions
88
...lational.Specification.Tests/TestModels/NorthwindDbFunction/NorthwindDbFunctionContext.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,88 @@ | ||
// 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 Microsoft.EntityFrameworkCore.Metadata; | ||
using Microsoft.EntityFrameworkCore.TestModels.Northwind; | ||
|
||
namespace Microsoft.EntityFrameworkCore | ||
{ | ||
public class NorthwindDbFunctionContext : NorthwindContext | ||
{ | ||
public NorthwindDbFunctionContext(DbContextOptions options, QueryTrackingBehavior queryTrackingBehavior = QueryTrackingBehavior.TrackAll) | ||
: base(options, queryTrackingBehavior) | ||
{ | ||
} | ||
|
||
public enum ReportingPeriod | ||
{ | ||
Winter = 0, | ||
Spring, | ||
Summer, | ||
Fall | ||
} | ||
|
||
public static int MyCustomLength(string s) | ||
{ | ||
throw new Exception(); | ||
} | ||
|
||
[DbFunction(Schema = "dbo", Name = "EmployeeOrderCount")] | ||
public static int EmployeeOrderCount(int employeeId) | ||
{ | ||
throw new NotImplementedException(); | ||
} | ||
|
||
[DbFunction(Schema = "dbo", Name = "EmployeeOrderCount")] | ||
public static int EmployeeOrderCountWithClient(int employeeId) | ||
{ | ||
switch (employeeId) | ||
{ | ||
case 3: return 127; | ||
default: return 1; | ||
} | ||
} | ||
|
||
[DbFunction(Schema = "dbo")] | ||
public static bool IsTopEmployee(int employeeId) | ||
{ | ||
throw new NotImplementedException(); | ||
} | ||
|
||
[DbFunction(Schema = "dbo")] | ||
public static int GetEmployeeWithMostOrdersAfterDate(DateTime? startDate) | ||
{ | ||
throw new NotImplementedException(); | ||
} | ||
|
||
[DbFunction(Schema = "dbo")] | ||
public static DateTime? GetReportingPeriodStartDate(ReportingPeriod periodId) | ||
{ | ||
throw new NotImplementedException(); | ||
} | ||
|
||
[DbFunction(Schema = "dbo")] | ||
public static string StarValue(int starCount, int value) | ||
{ | ||
throw new NotImplementedException(); | ||
} | ||
|
||
[DbFunction(Name = "StarValue", Schema = "dbo")] | ||
public static string StarValueAlternateParamOrder([DbFunctionParameter(ParameterIndex = 1)]int value, [DbFunctionParameter(ParameterIndex = 0)]int starCount) | ||
{ | ||
throw new NotImplementedException(); | ||
} | ||
|
||
[DbFunction(Schema = "dbo")] | ||
public static int AddValues(int a, int b) | ||
{ | ||
throw new NotImplementedException(); | ||
} | ||
|
||
[DbFunction(Schema = "dbo")] | ||
public static DateTime GetBestYearEver() | ||
{ | ||
throw new NotImplementedException(); | ||
} | ||
} | ||
} |
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
72 changes: 72 additions & 0 deletions
72
src/EFCore.Relational/Infrastructure/RelationalModelCustomizer.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,72 @@ | ||
// 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.Linq; | ||
using System.Reflection; | ||
using JetBrains.Annotations; | ||
using Microsoft.EntityFrameworkCore.Internal; | ||
using Microsoft.EntityFrameworkCore.Metadata; | ||
using Microsoft.EntityFrameworkCore.Metadata.Internal; | ||
using Microsoft.EntityFrameworkCore.Utilities; | ||
|
||
namespace Microsoft.EntityFrameworkCore.Infrastructure | ||
{ | ||
/// <summary> | ||
/// This API supports the Entity Framework Core infrastructure and is not intended to be used | ||
/// directly from your code. This API may change or be removed in future releases. | ||
/// </summary> | ||
public class RelationalModelCustomizer : ModelCustomizer | ||
{ | ||
public RelationalModelCustomizer([NotNull] ModelCustomizerDependencies dependencies) | ||
: base(dependencies) | ||
{ | ||
} | ||
|
||
public override void Customize(ModelBuilder modelBuilder, DbContext dbContext) | ||
{ | ||
FindDbFunctions(modelBuilder, dbContext); | ||
|
||
base.Customize(modelBuilder, dbContext); | ||
} | ||
|
||
/// <summary> | ||
/// This API supports the Entity Framework Core infrastructure and is not intended to be used | ||
/// directly from your code. This API may change or be removed in future releases. | ||
/// </summary> | ||
protected virtual void FindDbFunctions([NotNull] ModelBuilder modelBuilder, [NotNull] DbContext context) | ||
{ | ||
Check.NotNull(modelBuilder, nameof(modelBuilder)); | ||
Check.NotNull(context, nameof(context)); | ||
|
||
var functions = context.GetType().GetMethods(BindingFlags.Public | BindingFlags.Static | BindingFlags.FlattenHierarchy) | ||
.Where(mi => mi.IsStatic | ||
&& mi.IsPublic | ||
&& mi.GetCustomAttributes(typeof(DbFunctionAttribute)).Any()); | ||
|
||
foreach (var function in functions) | ||
{ | ||
modelBuilder.HasDbFunction(function); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// This API supports the Entity Framework Core infrastructure and is not intended to be used | ||
/// directly from your code. This API may change or be removed in future releases. | ||
/// </summary> | ||
protected override void FindSets(ModelBuilder modelBuilder, DbContext context) | ||
{ | ||
base.FindSets(modelBuilder, context); | ||
|
||
var sets = Dependencies.SetFinder.CreateClrTypeDbSetMapping(context); | ||
|
||
foreach (var entityType in modelBuilder.Model.GetEntityTypes().Cast<EntityType>()) | ||
{ | ||
if (entityType.BaseType == null | ||
&& sets.ContainsKey(entityType.ClrType)) | ||
{ | ||
entityType.Builder.Relational(ConfigurationSource.Convention).ToTable(sets[entityType.ClrType].Name); | ||
} | ||
} | ||
} | ||
} | ||
} |
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
Oops, something went wrong.