From e53ca690a56494b087d7e8c8f0c92e3789237206 Mon Sep 17 00:00:00 2001 From: zzzprojects Date: Wed, 14 Dec 2016 21:17:35 -0500 Subject: [PATCH] Fix QueryFutre + QueryCache + QueryFilter Fix QueryFutre + QueryCache + QueryFilter --- .../Properties/AssemblyInfo.cs | 4 +- .../Extensions/IQueryable`/FromCacheAsync.cs | 17 +++++++ .../QueryFilter/Extensions/IDbSet.Filter.cs | 5 +- ...daExpression.GetPropertyOrFieldAccessor.cs | 2 +- ...aExpression.GetPropertyOrFieldAccessors.cs | 2 +- .../PropertyOrFieldAccessor.cs | 2 +- .../_Internal/ExpressionExtensions.cs | 2 +- .../Properties/AssemblyInfo.cs | 4 +- .../Extensions/IQueryable`/FromCacheAsync.cs | 17 +++++++ .../QueryFuture/QueryFutureBatch.cs | 51 +++++++------------ .../ObjectContext/GetInterceptionContext.cs | 2 +- .../GetCommandTextAndParameters.cs | 2 +- version.txt | 2 +- 13 files changed, 67 insertions(+), 45 deletions(-) diff --git a/src/Z.EntityFramework.Plus.EF6.NET40/Properties/AssemblyInfo.cs b/src/Z.EntityFramework.Plus.EF6.NET40/Properties/AssemblyInfo.cs index 46cf9346..f9ec8da9 100644 --- a/src/Z.EntityFramework.Plus.EF6.NET40/Properties/AssemblyInfo.cs +++ b/src/Z.EntityFramework.Plus.EF6.NET40/Properties/AssemblyInfo.cs @@ -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")] \ No newline at end of file +[assembly: AssemblyVersion("1.4.16")] +[assembly: AssemblyFileVersion("1.4.16")] \ No newline at end of file diff --git a/src/Z.EntityFramework.Plus.EF6.NET40/QueryCache/Extensions/IQueryable`/FromCacheAsync.cs b/src/Z.EntityFramework.Plus.EF6.NET40/QueryCache/Extensions/IQueryable`/FromCacheAsync.cs index 552a6bea..5e7b2726 100644 --- a/src/Z.EntityFramework.Plus.EF6.NET40/QueryCache/Extensions/IQueryable`/FromCacheAsync.cs +++ b/src/Z.EntityFramework.Plus.EF6.NET40/QueryCache/Extensions/IQueryable`/FromCacheAsync.cs @@ -58,6 +58,23 @@ public static Task> FromCacheAsync(this IQueryable query, C return result; } + //public static async Task> FromCacheAsync2(this IQueryable 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)item; + // return result; + //} + /// /// Return the result of the from the cache. If the query is not cached /// yet, the query is materialized asynchronously and cached before being returned. diff --git a/src/Z.EntityFramework.Plus.EF6.NET40/QueryFilter/Extensions/IDbSet.Filter.cs b/src/Z.EntityFramework.Plus.EF6.NET40/QueryFilter/Extensions/IDbSet.Filter.cs index 08a4a987..f696daed 100644 --- a/src/Z.EntityFramework.Plus.EF6.NET40/QueryFilter/Extensions/IDbSet.Filter.cs +++ b/src/Z.EntityFramework.Plus.EF6.NET40/QueryFilter/Extensions/IDbSet.Filter.cs @@ -36,9 +36,12 @@ public static IQueryable Filter(this DbSet query, params object[] keys) #endif { var queryFilterQueryable = QueryFilterManager.GetFilterQueryable(query); + var nonQueryFilter = queryFilterQueryable != null ? (IQueryable)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); } } } diff --git a/src/Z.EntityFramework.Plus.EF6.NET40/_Internal/LinqExpressionExtensions/LambdaExpression/LambdaExpression.GetPropertyOrFieldAccessor.cs b/src/Z.EntityFramework.Plus.EF6.NET40/_Internal/LinqExpressionExtensions/LambdaExpression/LambdaExpression.GetPropertyOrFieldAccessor.cs index 9b6b2c4b..bc8f159d 100644 --- a/src/Z.EntityFramework.Plus.EF6.NET40/_Internal/LinqExpressionExtensions/LambdaExpression/LambdaExpression.GetPropertyOrFieldAccessor.cs +++ b/src/Z.EntityFramework.Plus.EF6.NET40/_Internal/LinqExpressionExtensions/LambdaExpression/LambdaExpression.GetPropertyOrFieldAccessor.cs @@ -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; diff --git a/src/Z.EntityFramework.Plus.EF6.NET40/_Internal/LinqExpressionExtensions/LambdaExpression/LambdaExpression.GetPropertyOrFieldAccessors.cs b/src/Z.EntityFramework.Plus.EF6.NET40/_Internal/LinqExpressionExtensions/LambdaExpression/LambdaExpression.GetPropertyOrFieldAccessors.cs index 8bdcc4ea..67151a19 100644 --- a/src/Z.EntityFramework.Plus.EF6.NET40/_Internal/LinqExpressionExtensions/LambdaExpression/LambdaExpression.GetPropertyOrFieldAccessors.cs +++ b/src/Z.EntityFramework.Plus.EF6.NET40/_Internal/LinqExpressionExtensions/LambdaExpression/LambdaExpression.GetPropertyOrFieldAccessors.cs @@ -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; diff --git a/src/Z.EntityFramework.Plus.EF6.NET40/_Internal/LinqExpressionExtensions/PropertyOrFieldAccessor.cs b/src/Z.EntityFramework.Plus.EF6.NET40/_Internal/LinqExpressionExtensions/PropertyOrFieldAccessor.cs index 519ac3eb..6ec63b02 100644 --- a/src/Z.EntityFramework.Plus.EF6.NET40/_Internal/LinqExpressionExtensions/PropertyOrFieldAccessor.cs +++ b/src/Z.EntityFramework.Plus.EF6.NET40/_Internal/LinqExpressionExtensions/PropertyOrFieldAccessor.cs @@ -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; diff --git a/src/Z.EntityFramework.Plus.EF6.NET40/_Internal/LinqExpressionExtensions/_Internal/ExpressionExtensions.cs b/src/Z.EntityFramework.Plus.EF6.NET40/_Internal/LinqExpressionExtensions/_Internal/ExpressionExtensions.cs index 5288bb73..564800d1 100644 --- a/src/Z.EntityFramework.Plus.EF6.NET40/_Internal/LinqExpressionExtensions/_Internal/ExpressionExtensions.cs +++ b/src/Z.EntityFramework.Plus.EF6.NET40/_Internal/LinqExpressionExtensions/_Internal/ExpressionExtensions.cs @@ -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; diff --git a/src/Z.EntityFramework.Plus.EF6/Properties/AssemblyInfo.cs b/src/Z.EntityFramework.Plus.EF6/Properties/AssemblyInfo.cs index ebf0b943..21067278 100644 --- a/src/Z.EntityFramework.Plus.EF6/Properties/AssemblyInfo.cs +++ b/src/Z.EntityFramework.Plus.EF6/Properties/AssemblyInfo.cs @@ -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")] \ No newline at end of file +[assembly: AssemblyVersion("1.4.16")] +[assembly: AssemblyFileVersion("1.4.16")] \ No newline at end of file diff --git a/src/Z.EntityFramework.Plus.EF6/QueryCache/Extensions/IQueryable`/FromCacheAsync.cs b/src/Z.EntityFramework.Plus.EF6/QueryCache/Extensions/IQueryable`/FromCacheAsync.cs index 552a6bea..5e7b2726 100644 --- a/src/Z.EntityFramework.Plus.EF6/QueryCache/Extensions/IQueryable`/FromCacheAsync.cs +++ b/src/Z.EntityFramework.Plus.EF6/QueryCache/Extensions/IQueryable`/FromCacheAsync.cs @@ -58,6 +58,23 @@ public static Task> FromCacheAsync(this IQueryable query, C return result; } + //public static async Task> FromCacheAsync2(this IQueryable 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)item; + // return result; + //} + /// /// Return the result of the from the cache. If the query is not cached /// yet, the query is materialized asynchronously and cached before being returned. diff --git a/src/Z.EntityFramework.Plus.EF6/QueryFuture/QueryFutureBatch.cs b/src/Z.EntityFramework.Plus.EF6/QueryFuture/QueryFutureBatch.cs index 0cb54533..5b1d9dc1 100644 --- a/src/Z.EntityFramework.Plus.EF6/QueryFuture/QueryFutureBatch.cs +++ b/src/Z.EntityFramework.Plus.EF6/QueryFuture/QueryFutureBatch.cs @@ -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 diff --git a/src/Z.EntityFramework.Plus.EF6/_Internal/EF6/ObjectContext/GetInterceptionContext.cs b/src/Z.EntityFramework.Plus.EF6/_Internal/EF6/ObjectContext/GetInterceptionContext.cs index 42342d9d..c1cd95f4 100644 --- a/src/Z.EntityFramework.Plus.EF6/_Internal/EF6/ObjectContext/GetInterceptionContext.cs +++ b/src/Z.EntityFramework.Plus.EF6/_Internal/EF6/ObjectContext/GetInterceptionContext.cs @@ -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; diff --git a/src/Z.EntityFramework.Plus.EF6/_Internal/EF6/ObjectQuery/GetCommandTextAndParameters.cs b/src/Z.EntityFramework.Plus.EF6/_Internal/EF6/ObjectQuery/GetCommandTextAndParameters.cs index d7a8500c..71262a79 100644 --- a/src/Z.EntityFramework.Plus.EF6/_Internal/EF6/ObjectQuery/GetCommandTextAndParameters.cs +++ b/src/Z.EntityFramework.Plus.EF6/_Internal/EF6/ObjectQuery/GetCommandTextAndParameters.cs @@ -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; diff --git a/version.txt b/version.txt index 2c5a71d5..16df7b2a 100644 --- a/version.txt +++ b/version.txt @@ -1 +1 @@ -v1.4.15 \ No newline at end of file +v1.4.16 \ No newline at end of file