Skip to content

Commit

Permalink
Added support for EF5 queries. Closes #8
Browse files Browse the repository at this point in the history
  • Loading branch information
Giorgi committed Mar 28, 2016
1 parent f0b16e3 commit d2b7858
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 1 deletion.
15 changes: 15 additions & 0 deletions src/QueryPlanVisualizer/Helpers/DatabaseHelper.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Data.Common;
using System.Data.Entity.Infrastructure;
using System.Data.Linq;
using System.Linq;
using System.Reflection;
Expand Down Expand Up @@ -39,6 +40,20 @@ public static DatabaseHelper Create<T>(DataContextBase dataContextBase, IQueryab
}
}

var query = queryable as DbQuery<T>;
if (query != null)
{
var bindingFlags = BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.GetProperty;

var internalQuery = query.GetType().GetProperty("InternalQuery", bindingFlags)?.GetValue(query);
var objectQuery = internalQuery?.GetType().GetProperty("ObjectQuery")?.GetValue(internalQuery) as System.Data.Objects.ObjectQuery<T>;

if (objectQuery != null) //EF5 uses ObjectQuery from System.Data.Objects namespace, EF6 uses System.Data.Entity.Core.Objects so it will be null
{
return new EntityFramework5DatabaseHelper(objectQuery);
}
}

return new EntityFrameworkDatabaseHelper();
}

Expand Down
32 changes: 31 additions & 1 deletion src/QueryPlanVisualizer/Helpers/EntityFrameworkDatabaseHelper.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,41 @@
using System;
using System.Data.Common;
using System.Data.Entity.Infrastructure.Interception;
using System.Data.EntityClient;
using System.Data.Objects;
using System.Linq;

namespace ExecutionPlanVisualizer.Helpers
{
class EntityFramework5DatabaseHelper : DatabaseHelper
{
private ObjectParameterCollection parameters;

public EntityFramework5DatabaseHelper(ObjectQuery objectQuery)
{
Connection = (objectQuery.Context.Connection as EntityConnection)?.StoreConnection;
parameters = objectQuery.Parameters;
}

protected override DbCommand CreateCommand(IQueryable queryable)
{
var command = Connection.CreateCommand();
command.CommandText = queryable.ToString();

var copiedParameters = parameters.Select(parameter =>
{
var parameterCopy = command.CreateParameter();
parameterCopy.ParameterName = parameter.Name;
parameterCopy.Value = parameter.Value;
return parameterCopy;
}).ToArray();

command.Parameters.AddRange(copiedParameters);

return command;
}
}

internal class EntityFrameworkDatabaseHelper : DatabaseHelper
{
protected override DbCommand CreateCommand(IQueryable queryable)
Expand Down Expand Up @@ -47,7 +78,6 @@ protected override DbCommand CreateCommand(IQueryable queryable)

command.Parameters.AddRange(copiedParameters);


return command;
}

Expand Down
1 change: 1 addition & 0 deletions src/QueryPlanVisualizer/QueryPlanVisualizer.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
<Reference Include="System" />
<Reference Include="System.ComponentModel.DataAnnotations" />
<Reference Include="System.Core" />
<Reference Include="System.Data.Entity" />
<Reference Include="System.Data.Linq" />
<Reference Include="System.Drawing" />
<Reference Include="System.Windows.Forms" />
Expand Down

0 comments on commit d2b7858

Please sign in to comment.