Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Metadata: Bring back SqlServerDbFunctionConvention #21388

Merged
merged 1 commit into from
Jun 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ public override ConventionSet CreateConventionSet()
ReplaceConvention(conventionSet.ModelFinalizingConventions, storeGenerationConvention);
ReplaceConvention(conventionSet.ModelFinalizingConventions,
(SharedTableConvention)new SqlServerSharedTableConvention(Dependencies, RelationalDependencies));
conventionSet.ModelFinalizingConventions.Add(new SqlServerDbFunctionConvention(Dependencies, RelationalDependencies));

return conventionSet;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// 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 JetBrains.Annotations;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using Microsoft.EntityFrameworkCore.Metadata.Conventions.Infrastructure;
using Microsoft.EntityFrameworkCore.Utilities;

// ReSharper disable once CheckNamespace
namespace Microsoft.EntityFrameworkCore.Metadata.Conventions
{
/// <summary>
/// A convention that ensures that <see cref="IDbFunction.Schema"/> is populated for database functions which are not built-in.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How do you determine that they are built-in?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is coming in #17268 (submitting PR for that once this is approved).

Just future-proof documentation. 😄

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can improve this in next PR by referring the IsBuiltIn property too.

/// </summary>
public class SqlServerDbFunctionConvention : IModelFinalizingConvention
{
/// <summary>
/// Creates a new instance of <see cref="SqlServerDbFunctionConvention" />.
/// </summary>
/// <param name="dependencies"> Parameter object containing dependencies for this convention. </param>
/// <param name="relationalDependencies"> Parameter object containing relational dependencies for this convention. </param>
public SqlServerDbFunctionConvention(
[NotNull] ProviderConventionSetBuilderDependencies dependencies,
[NotNull] RelationalConventionSetBuilderDependencies relationalDependencies)
{
Check.NotNull(dependencies, nameof(dependencies));
Check.NotNull(relationalDependencies, nameof(relationalDependencies));

Dependencies = dependencies;
}

/// <summary>
/// Parameter object containing service dependencies.
/// </summary>
protected virtual ProviderConventionSetBuilderDependencies Dependencies { get; }

/// <inheritdoc />
public virtual void ProcessModelFinalizing(IConventionModelBuilder modelBuilder, IConventionContext<IConventionModelBuilder> context)
{
foreach (var dbFunction in modelBuilder.Metadata.GetDbFunctions())
{
if (string.IsNullOrEmpty(dbFunction.Schema))
{
dbFunction.SetSchema("dbo");
}
}
}
}
}
55 changes: 0 additions & 55 deletions src/EFCore.SqlServer/Query/Internal/SqlServerQuerySqlGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,60 +99,5 @@ protected override void GenerateLimitOffset(SelectExpression selectExpression)
}
}
}

/// <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 Expression VisitSqlFunction(SqlFunctionExpression sqlFunctionExpression)
{
Check.NotNull(sqlFunctionExpression, nameof(sqlFunctionExpression));

if (!sqlFunctionExpression.IsBuiltIn
&& string.IsNullOrEmpty(sqlFunctionExpression.Schema))
{
sqlFunctionExpression = sqlFunctionExpression.IsNiladic
? new SqlFunctionExpression(
schema: "dbo",
sqlFunctionExpression.Name,
sqlFunctionExpression.IsNullable,
sqlFunctionExpression.Type,
sqlFunctionExpression.TypeMapping)
: new SqlFunctionExpression(
schema: "dbo",
sqlFunctionExpression.Name,
sqlFunctionExpression.Arguments,
sqlFunctionExpression.IsNullable,
sqlFunctionExpression.ArgumentsPropagateNullability,
sqlFunctionExpression.Type,
sqlFunctionExpression.TypeMapping);
}

return base.VisitSqlFunction(sqlFunctionExpression);
}

/// <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 Expression VisitTableValuedFunction(TableValuedFunctionExpression tableValuedFunctionExpression)
{
Check.NotNull(tableValuedFunctionExpression, nameof(tableValuedFunctionExpression));

if (string.IsNullOrEmpty(tableValuedFunctionExpression.Schema))
{
tableValuedFunctionExpression = new TableValuedFunctionExpression(
tableValuedFunctionExpression.Alias,
schema: "dbo",
tableValuedFunctionExpression.Name,
tableValuedFunctionExpression.Arguments);
}

return base.VisitTableValuedFunction(tableValuedFunctionExpression);
}
}
}