Skip to content

Commit

Permalink
Fix QueryFutre + QueryCache + QueryFilter
Browse files Browse the repository at this point in the history
Fix QueryFutre + QueryCache + QueryFilter
  • Loading branch information
zzzprojects committed Dec 15, 2016
1 parent c0a9fa6 commit e53ca69
Show file tree
Hide file tree
Showing 13 changed files with 67 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,5 @@
[assembly: AssemblyCulture("")]
[assembly: ComVisible(false)]
[assembly: Guid("ac398eb8-0a31-4d06-a804-84d10b6da96d")]
[assembly: AssemblyVersion("1.4.14")]
[assembly: AssemblyFileVersion("1.4.14")]
[assembly: AssemblyVersion("1.4.16")]
[assembly: AssemblyFileVersion("1.4.16")]
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,23 @@ public static Task<IEnumerable<T>> FromCacheAsync<T>(this IQueryable<T> query, C
return result;
}

//public static async Task<IEnumerable<T>> FromCacheAsync2<T>(this IQueryable<T> query, CacheItemPolicy policy, params string[] tags) where T : class
//{
// var key = QueryCacheManager.GetCacheKey(query, tags);

// var item = QueryCacheManager.Cache.Get(key);

// if (item == null)
// {
// item = await query.AsNoTracking().ToListAsync().ConfigureAwait(false);
// item = QueryCacheManager.Cache.AddOrGetExisting(key, item, policy) ?? item;
// QueryCacheManager.AddCacheTag(key, tags);
// }

// var result = (IEnumerable<T>)item;
// return result;
//}

/// <summary>
/// Return the result of the <paramref name="query" /> from the cache. If the query is not cached
/// yet, the query is materialized asynchronously and cached before being returned.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,12 @@ public static IQueryable<T> Filter<T>(this DbSet<T> query, params object[] keys)
#endif
{
var queryFilterQueryable = QueryFilterManager.GetFilterQueryable(query);
var nonQueryFilter = queryFilterQueryable != null ? (IQueryable<T>)queryFilterQueryable.OriginalQuery : query;

var context = queryFilterQueryable != null ? queryFilterQueryable.Context : query.GetDbContext();
var filterContext = QueryFilterManager.AddOrGetFilterContext(context);
return filterContext.ApplyFilter(query, keys);

return filterContext.ApplyFilter(nonQueryFilter, keys);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
// More projects: http://www.zzzprojects.com/
// Copyright © ZZZ Projects Inc. 2014 - 2016. All rights reserved.

#if FULL || AUDIT || QUERY_INCLUDEOPTIMIZED
#if FULL || AUDIT || QUERY_INCLUDEFILTER || QUERY_INCLUDEOPTIMIZED

using System;
using System.Linq;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
// More projects: http://www.zzzprojects.com/
// Copyright © ZZZ Projects Inc. 2014 - 2016. All rights reserved.

#if FULL || AUDIT || QUERY_INCLUDEOPTIMIZED
#if FULL || AUDIT || QUERY_INCLUDEFILTER || QUERY_INCLUDEOPTIMIZED
using System.Linq;
using System.Linq.Expressions;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
// More projects: http://www.zzzprojects.com/
// Copyright © ZZZ Projects Inc. 2014 - 2016. All rights reserved.

#if FULL || AUDIT || QUERY_INCLUDEOPTIMIZED
#if FULL || AUDIT || QUERY_INCLUDEFILTER || QUERY_INCLUDEOPTIMIZED

using System.Collections.ObjectModel;
using System.Linq;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
// More projects: http://www.zzzprojects.com/
// Copyright © ZZZ Projects Inc. 2014 - 2016. All rights reserved.

#if FULL || AUDIT || QUERY_INCLUDEOPTIMIZED
#if FULL || AUDIT || QUERY_INCLUDEFILTER || QUERY_INCLUDEOPTIMIZED
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
Expand Down
4 changes: 2 additions & 2 deletions src/Z.EntityFramework.Plus.EF6/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,5 @@
[assembly: AssemblyCulture("")]
[assembly: ComVisible(false)]
[assembly: Guid("89f47e22-ff83-4ac8-be90-c6755748cf89")]
[assembly: AssemblyVersion("1.4.14")]
[assembly: AssemblyFileVersion("1.4.14")]
[assembly: AssemblyVersion("1.4.16")]
[assembly: AssemblyFileVersion("1.4.16")]
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,23 @@ public static Task<IEnumerable<T>> FromCacheAsync<T>(this IQueryable<T> query, C
return result;
}

//public static async Task<IEnumerable<T>> FromCacheAsync2<T>(this IQueryable<T> query, CacheItemPolicy policy, params string[] tags) where T : class
//{
// var key = QueryCacheManager.GetCacheKey(query, tags);

// var item = QueryCacheManager.Cache.Get(key);

// if (item == null)
// {
// item = await query.AsNoTracking().ToListAsync().ConfigureAwait(false);
// item = QueryCacheManager.Cache.AddOrGetExisting(key, item, policy) ?? item;
// QueryCacheManager.AddCacheTag(key, tags);
// }

// var result = (IEnumerable<T>)item;
// return result;
//}

/// <summary>
/// Return the result of the <paramref name="query" /> from the cache. If the query is not cached
/// yet, the query is materialized asynchronously and cached before being returned.
Expand Down
51 changes: 18 additions & 33 deletions src/Z.EntityFramework.Plus.EF6/QueryFuture/QueryFutureBatch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -158,45 +158,30 @@ protected DbCommand CreateCommandCombined()
sql = sql.Replace("@" + oldValue, "@" + newValue);
}
#elif EF6
var commandTextAndParameter = query.Query.GetCommandTextAndParameters();

var objectQuery = query.Query;
var stateField = objectQuery.GetType().BaseType.GetField("_state", BindingFlags.NonPublic | BindingFlags.Instance);
var state = stateField.GetValue(objectQuery);
var getExecutionPlanMethod = state.GetType().GetMethod("GetExecutionPlan", BindingFlags.NonPublic | BindingFlags.Instance);
var getExecutionPlan = getExecutionPlanMethod.Invoke(state, new object[] { null });
var prepareEntityCommandMethod = getExecutionPlan.GetType().GetMethod("PrepareEntityCommand", BindingFlags.NonPublic | BindingFlags.Instance);

string sql = "";
using (EntityCommand entityCommand = (EntityCommand) prepareEntityCommandMethod.Invoke(getExecutionPlan, new object[] {objectQuery.Context, objectQuery.Parameters}))
{
var getCommandDefinitionMethod = entityCommand.GetType().GetMethod("GetCommandDefinition", BindingFlags.NonPublic | BindingFlags.Instance);
var getCommandDefinition = getCommandDefinitionMethod.Invoke(entityCommand, new object[0]);

var prepareEntityCommandBeforeExecutionMethod = getCommandDefinition.GetType().GetMethod("PrepareEntityCommandBeforeExecution", BindingFlags.NonPublic | BindingFlags.Instance);
var prepareEntityCommandBeforeExecution = (DbCommand)prepareEntityCommandBeforeExecutionMethod.Invoke(getCommandDefinition, new object[] {entityCommand});

sql = prepareEntityCommandBeforeExecution.CommandText;
var parameters = prepareEntityCommandBeforeExecution.Parameters;
var sql = commandTextAndParameter.Item1;
var parameters = commandTextAndParameter.Item2;

// UPDATE parameter name
foreach (DbParameter parameter in parameters)
// UPDATE parameter name
foreach (DbParameter parameter in parameters)
{
var oldValue = parameter.ParameterName;
if (oldValue.StartsWith("@"))
{
var oldValue = parameter.ParameterName;
if (oldValue.StartsWith("@"))
{
oldValue = oldValue.Substring(1);
}
var newValue = string.Concat("Z_", queryCount, "_", oldValue);
oldValue = oldValue.Substring(1);
}
var newValue = string.Concat("Z_", queryCount, "_", oldValue);

// CREATE parameter
var dbParameter = command.CreateParameter();
dbParameter.ParameterName = newValue;
dbParameter.Value = parameter.Value;
command.Parameters.Add(dbParameter);
// CREATE parameter
var dbParameter = command.CreateParameter();
dbParameter.ParameterName = newValue;
dbParameter.Value = parameter.Value;
command.Parameters.Add(dbParameter);

// REPLACE parameter with new value
sql = sql.Replace("@" + oldValue, "@" + newValue);
}
// REPLACE parameter with new value
sql = sql.Replace("@" + oldValue, "@" + newValue);
}
#elif EFCORE

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
// More projects: http://www.zzzprojects.com/
// Copyright © ZZZ Projects Inc. 2014 - 2016. All rights reserved.

#if FULL || QUERY_FUTURE || QUERY_INCLUDEOPTIMIZED
#if FULL || QUERY_CACHE || QUERY_FILTER || QUERY_FUTURE || QUERY_INCLUDEOPTIMIZED
#if EF6
using System.Data.Entity.Core.Objects;
using System.Data.Entity.Infrastructure.Interception;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
// More projects: http://www.zzzprojects.com/
// Copyright © ZZZ Projects Inc. 2014 - 2016. All rights reserved.

#if FULL || QUERY_CACHE || QUERY_FILTER
#if FULL || QUERY_CACHE || QUERY_FILTER || QUERY_FUTURE
#if EF6
using System;
using System.Collections.Generic;
Expand Down
2 changes: 1 addition & 1 deletion version.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
v1.4.15
v1.4.16

0 comments on commit e53ca69

Please sign in to comment.