Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
…mework into tableFuncs5
  • Loading branch information
pmiddleton committed Mar 6, 2018
2 parents e156577 + 6a58596 commit 4e65020
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -121,14 +121,14 @@ protected override Expression VisitMethodCall(MethodCallExpression node)
{
Check.NotNull(node, nameof(node));

var dbFunc = _model.Relational().FindDbFunction(node.Method);

if (dbFunc != null && dbFunc.IsIQueryable)
{
return VisitDbFunctionSourceExpression(new DbFunctionSourceExpression(node, _model));
}
else
{
// var dbFunc = _model.Relational().FindDbFunction(node.Method);

// if (dbFunc != null && dbFunc.IsIQueryable)
// {
// return VisitDbFunctionSourceExpression(new DbFunctionSourceExpression(node, _model));
// }
// else
// {
QueryModelVisitor.BindMethodCallExpression(
node,
(property, querySource, selectExpression)
Expand All @@ -138,7 +138,7 @@ protected override Expression VisitMethodCall(MethodCallExpression node)
bindSubQueries: true);

return base.VisitMethodCall(node);
}
// }
}

/// <summary>
Expand Down
23 changes: 14 additions & 9 deletions src/EFCore/DbContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1426,16 +1426,18 @@ protected virtual T ExecuteScalarMethod<U, T>([NotNull] Expression<Func<U, T>> d
{
Check.NotNull(dbFuncCall, nameof(dbFuncCall));

//todo - verify dbFuncCall contains a method call expression
if (dbFuncCall.Body.NodeType != ExpressionType.Call)
{
throw new InvalidOperationException(CoreStrings.DbContextExecuteInvalidParameter());
}

var dbFuncFac = InternalServiceProvider.GetRequiredService<IDbFunctionSourceFactory>();
var resultsQuery = DbContextDependencies.QueryProvider.Execute(dbFuncFac.GenerateDbFunctionSource(dbFuncCall.Body as MethodCallExpression, Model)) as IEnumerable<T>;
var results = resultsQuery?.ToList();

var results = resultsQuery.ToList();

return results[0];
//how am I going to get the dbFunction from the model here - I can't access FindDbFunction because it is in relational.
//maybe I need to pass a reference to the model and find it later? If I move DbFunctionSourceExpression into relational I can access it, but then how do I create DbFunctionSourceExpression.
//need some kind of factory.....
return results == null || results.Count == 0
? default
: results[0];
}

/// <summary>
Expand All @@ -1450,9 +1452,12 @@ protected virtual IQueryable<T> ExecuteTableValuedFunction<U, T>([NotNull] Expre
{
Check.NotNull(dbFuncCall, nameof(dbFuncCall));

if (dbFuncCall.Body.NodeType != ExpressionType.Call)
{
throw new InvalidOperationException(CoreStrings.DbContextExecuteInvalidParameter());
}

var dbFuncFac = InternalServiceProvider.GetRequiredService<IDbFunctionSourceFactory>();

//todo - verify dbFuncCall contains a method call expression
var resultsQuery = dbFuncFac.GenerateDbFunctionSource(dbFuncCall.Body as MethodCallExpression, Model);

return DbContextDependencies.QueryProvider.CreateQuery<T>(resultsQuery);
Expand Down
8 changes: 8 additions & 0 deletions src/EFCore/Properties/CoreStrings.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions src/EFCore/Properties/CoreStrings.resx
Original file line number Diff line number Diff line change
Expand Up @@ -1075,4 +1075,7 @@
<data name="IdentifyingRelationshipCycle" xml:space="preserve">
<value>The entity type '{entityType}' is part of a relationship cycle involving its primary key.</value>
</data>
<data name="DbContextExecuteInvalidParameter" xml:space="preserve">
<value>The parameter to Execute must be a MethodCallExpression.</value>
</data>
</root>
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,17 @@ public int AddValues(Expression<Func<int>> a, int b)
return ExecuteScalarMethod<UDFSqlContext, int>(db => db.AddValues(a, b));
}

public int BadMethodCallToExecuteScalar()
{
return ExecuteScalarMethod<UDFSqlContext, int>(db => 5);
}

public IQueryable<Customer> BadMethodCallToExecuteTVF()
{
return ExecuteTableValuedFunction<UDFSqlContext, Customer>(db => new UDFSqlContext(null).Customers);
}


#endregion

#region Table Functions
Expand Down Expand Up @@ -1690,12 +1701,36 @@ public void BootstrapScalarFuncParamsConstant()
SELECT [dbo].[AddValues](1, @__b_0)");
}
}

[Fact]
public void BootstrapBadMethodCallToExecuteScalar()
{
using (var context = CreateContext())
{
var expectedMessage = CoreStrings.DbContextExecuteInvalidParameter();

Assert.Equal(expectedMessage, Assert.Throws<InvalidOperationException>(() => context.BadMethodCallToExecuteScalar()).Message);
}
}

#endregion

#endregion

#region Table Valued Tests


[Fact]
public void BootstrapBadMethodCallToExecuteTableValued()
{
using (var context = CreateContext())
{
var expectedMessage = CoreStrings.DbContextExecuteInvalidParameter();

Assert.Equal(expectedMessage, Assert.Throws<InvalidOperationException>(() => context.BadMethodCallToExecuteTVF()).Message);
}
}

[Fact]
public void TV_Function_Stand_Alone()
{
Expand Down

0 comments on commit 4e65020

Please sign in to comment.