Skip to content

Commit cfc71a0

Browse files
Update source with latest
Update source with latest
1 parent 47b7969 commit cfc71a0

File tree

16 files changed

+609
-147
lines changed

16 files changed

+609
-147
lines changed

src/shared/Z.EF.Plus.BatchDelete.Shared/BatchDelete.cs

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ public int Execute<T>(IQueryable<T> query) where T : class
226226
// SELECT keys names
227227
var queryKeys = query.SelectByName(keys.Select(x => x.Name).ToList());
228228
var innerObjectQuery = queryKeys.GetObjectQuery();
229-
229+
230230
// CREATE command
231231
var command = CreateCommand(innerObjectQuery, entity);
232232

@@ -387,7 +387,7 @@ public int Execute<T>(IQueryable<T> query) where T : class
387387
}
388388
}
389389
#endif
390-
}
390+
}
391391

392392
#if EF5 || EF6
393393
/// <summary>Creates a command to execute the batch operation.</summary>
@@ -473,7 +473,7 @@ internal DbCommand CreateCommand<T>(ObjectQuery query, SchemaEntityType<T> entit
473473
}
474474

475475
// GET command text template
476-
var commandTextTemplate =
476+
var commandTextTemplate =
477477
isPostgreSql ? CommandTextPostgreSQLTemplate :
478478
isOracle ? CommandTextOracleTemplate :
479479
isMySql ? CommandTextTemplate_MySql :
@@ -767,16 +767,27 @@ public DbCommand CreateCommand(IQueryable query, IEntityType entity)
767767
// ADD Parameter
768768
foreach (var relationalParameter in relationalCommand.Parameters)
769769
{
770-
var parameter = queryContext.ParameterValues[relationalParameter.InvariantName];
770+
object parameter;
771+
if (!queryContext.ParameterValues.TryGetValue(relationalParameter.InvariantName, out parameter))
772+
{
773+
if (relationalParameter.InvariantName.StartsWith("__ef_filter"))
774+
{
775+
throw new Exception("Please use 'IgnoreQueryFilters()'. The HasQueryFilter is not yet supported.");
776+
}
777+
else
778+
{
779+
throw new Exception("The following parameter could not be found: " + relationalParameter);
780+
}
781+
}
771782

772783
var param = command.CreateParameter();
773784
param.CopyFrom(relationalParameter, parameter);
774785

775786
command.Parameters.Add(param);
776787
}
777788
#else
778-
// ADD Parameter
779-
var parameterCollection = relationalCommand.Parameters;
789+
// ADD Parameter
790+
var parameterCollection = relationalCommand.Parameters;
780791
foreach (var parameter in parameterCollection)
781792
{
782793
var param = command.CreateParameter();

src/shared/Z.EF.Plus.BatchUpdate.Shared/BatchUpdate.cs

Lines changed: 45 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -389,7 +389,7 @@ public int Execute<T>(IQueryable<T> query, Expression<Func<T, T>> updateFactory)
389389
}
390390
}
391391
#endif
392-
}
392+
}
393393

394394
#if EF5 || EF6
395395
/// <summary>Creates a command to execute the batch operation.</summary>
@@ -488,9 +488,9 @@ internal DbCommand CreateCommand<T>(ObjectQuery query, SchemaEntityType<T> entit
488488
CommandTextWhileDelayTemplate :
489489
CommandTextWhileTemplate :
490490
#endif
491-
isPostgreSQL ? CommandTextTemplate_PostgreSQL :
491+
isPostgreSQL ? CommandTextTemplate_PostgreSQL :
492492
isOracle ? CommandTextOracleTemplate :
493-
isMySql ? CommandTextTemplate_MySQL :
493+
isMySql ? CommandTextTemplate_MySQL :
494494
isSqlCe ? CommandTextTemplateSqlCe :
495495
isSQLite ? CommandTextTemplate_SQLite :
496496
isHana ? CommandTextTemplate_Hana :
@@ -515,7 +515,7 @@ internal DbCommand CreateCommand<T>(ObjectQuery query, SchemaEntityType<T> entit
515515
primaryKeys = string.Join(Environment.NewLine + "AND ", columnKeys.Select(x => string.Concat(tableName + ".", EscapeName(x, isMySql, isOracle, isPostgreSQL, isHana), " = B.", EscapeName(x, isMySql, isOracle, isPostgreSQL, isHana), "")));
516516

517517
setValues = string.Join("," + Environment.NewLine, values.Select((x, i) => x.Item2 is ConstantExpression ?
518-
string.Concat(EscapeName(x.Item1, isMySql, isOracle, isPostgreSQL, isHana), " = ", ((ConstantExpression) x.Item2).Value.ToString().Replace("B.[", "[")) :
518+
string.Concat(EscapeName(x.Item1, isMySql, isOracle, isPostgreSQL, isHana), " = ", ((ConstantExpression)x.Item2).Value.ToString().Replace("B.[", "[")) :
519519
string.Concat(EscapeName(x.Item1, isMySql, isOracle, isPostgreSQL, isHana), " = @zzz_BatchUpdate_", i)));
520520
}
521521
else if (isOracle || isPostgreSQL || isHana)
@@ -542,7 +542,7 @@ internal DbCommand CreateCommand<T>(ObjectQuery query, SchemaEntityType<T> entit
542542

543543
// GET updateSetValues
544544
setValues = string.Join("," + Environment.NewLine, values.Select((x, i) => x.Item2 is ConstantExpression ?
545-
string.Concat("A.", EscapeName(x.Item1, isMySql, isOracle, isPostgreSQL, isHana), " = ", ((ConstantExpression) x.Item2).Value) :
545+
string.Concat("A.", EscapeName(x.Item1, isMySql, isOracle, isPostgreSQL, isHana), " = ", ((ConstantExpression)x.Item2).Value) :
546546
string.Concat("A.", EscapeName(x.Item1, isMySql, isOracle, isPostgreSQL, isHana), " = @zzz_BatchUpdate_", i)));
547547
}
548548

@@ -608,7 +608,7 @@ internal DbCommand CreateCommand<T>(ObjectQuery query, SchemaEntityType<T> entit
608608
{
609609
if (isOracle && paramValue is bool)
610610
{
611-
parameter.Value = (Boolean) paramValue ? 1 : 0;
611+
parameter.Value = (Boolean)paramValue ? 1 : 0;
612612
}
613613
else
614614
{
@@ -620,7 +620,7 @@ internal DbCommand CreateCommand<T>(ObjectQuery query, SchemaEntityType<T> entit
620620
if (parameter is SqlParameter)
621621
{
622622
var sqlParameter = (SqlParameter)parameter;
623-
623+
624624
if (paramNullValue != null && paramNullValue.Type == typeof(byte[]))
625625
{
626626
sqlParameter.SqlDbType = SqlDbType.VarBinary;
@@ -891,7 +891,18 @@ public DbCommand CreateCommand(IQueryable query, IEntityType entity, List<Tuple<
891891
// ADD Parameter
892892
foreach (var relationalParameter in relationalCommand.Parameters)
893893
{
894-
var parameter = queryContext.ParameterValues[relationalParameter.InvariantName];
894+
object parameter;
895+
if (!queryContext.ParameterValues.TryGetValue(relationalParameter.InvariantName, out parameter))
896+
{
897+
if (relationalParameter.InvariantName.StartsWith("__ef_filter"))
898+
{
899+
throw new Exception("Please use 'IgnoreQueryFilters()'. The HasQueryFilter is not yet supported.");
900+
}
901+
else
902+
{
903+
throw new Exception("The following parameter could not be found: " + relationalParameter);
904+
}
905+
}
895906

896907
var param = command.CreateParameter();
897908
param.CopyFrom(relationalParameter, parameter);
@@ -1013,36 +1024,36 @@ public List<Tuple<string, object>> GetInnerValues<T>(IQueryable<T> query, Expres
10131024

10141025
#endif
10151026
// GET updateFactory command
1016-
var values = ResolveUpdateFromQueryDictValues(updateFactory);
1027+
var values = ResolveUpdateFromQueryDictValues(updateFactory);
10171028
var destinationValues = new List<Tuple<string, object>>();
10181029

10191030
int valueI = -1;
10201031
foreach (var value in values)
1021-
{
1032+
{
10221033
valueI++;
1023-
1034+
10241035
#if EF5 || EF6
10251036
// FIND the mapped column
10261037
var column = mapping.ScalarProperties.Find(x => x.Name == value.Key);
1027-
string columnName;
1028-
1029-
if (column != null)
1030-
{
1031-
columnName = column.ColumnName;
1032-
}
1033-
else
1034-
{
1035-
var accessor = mapping.ScalarAccessors.Find(x => x.AccessorPath == value.Key);
1036-
if (accessor == null)
1037-
{
1038-
throw new Exception("The destination column could not be found:" + value.Key);
1039-
}
1040-
1041-
columnName = accessor.ColumnName;
1042-
}
1043-
1044-
1045-
1038+
string columnName;
1039+
1040+
if (column != null)
1041+
{
1042+
columnName = column.ColumnName;
1043+
}
1044+
else
1045+
{
1046+
var accessor = mapping.ScalarAccessors.Find(x => x.AccessorPath == value.Key);
1047+
if (accessor == null)
1048+
{
1049+
throw new Exception("The destination column could not be found:" + value.Key);
1050+
}
1051+
1052+
columnName = accessor.ColumnName;
1053+
}
1054+
1055+
1056+
10461057
#elif EFCORE
10471058

10481059
var property = entity.FindProperty(value.Key);
@@ -1077,7 +1088,7 @@ public List<Tuple<string, object>> GetInnerValues<T>(IQueryable<T> query, Expres
10771088
#if EF5 || EF6
10781089
// GET the select command text
10791090
var commandText = ((IQueryable)result).ToString();
1080-
var parameters = ((IQueryable) result).GetObjectQuery().Parameters;
1091+
var parameters = ((IQueryable)result).GetObjectQuery().Parameters;
10811092

10821093
// GET the 'value' part
10831094
var pos = commandText.IndexOf("AS [value]" + Environment.NewLine + "FROM", StringComparison.InvariantCultureIgnoreCase) != -1 ?
@@ -1149,7 +1160,7 @@ public List<Tuple<string, object>> GetInnerValues<T>(IQueryable<T> query, Expres
11491160
valueSql = valueSql.Substring(0, valueSql.LastIndexOf('[') - 4);
11501161
}
11511162

1152-
if (valueSql.LastIndexOf('`') != -1 && !valueSql.Trim().EndsWith(")", StringComparison.InvariantCulture) && valueSql.Substring(0, valueSql.LastIndexOf('`')).EndsWith(" AS ", StringComparison.InvariantCulture))
1163+
if (valueSql.LastIndexOf('`') != -1 && !valueSql.Trim().EndsWith(")", StringComparison.InvariantCulture) && valueSql.Substring(0, valueSql.LastIndexOf('`')).EndsWith(" AS ", StringComparison.InvariantCulture))
11531164
{
11541165
valueSql = valueSql.Substring(0, valueSql.LastIndexOf('`') - 4);
11551166
}
@@ -1297,13 +1308,13 @@ public Dictionary<string, object> ResolveUpdateFromQueryDictValues<T>(Expression
12971308
{
12981309
if (constantExpression.Value == null)
12991310
{
1300-
value = new NullValue() {Type = constantExpression.Type};
1311+
value = new NullValue() { Type = constantExpression.Type };
13011312
}
13021313
else
13031314
{
13041315
value = constantExpression.Value;
13051316
}
1306-
1317+
13071318
}
13081319
else
13091320
{

src/shared/Z.EF.Plus.QueryCache.Shared/Extensions/IQueryable`/FromCache.cs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,17 +44,15 @@ public static IEnumerable<T> FromCache<T>(this IQueryable<T> query, CacheItemPol
4444
}
4545
var key = QueryCacheManager.GetCacheKey(query, tags);
4646

47-
var item = QueryCacheManager.Cache.Get(key);
47+
var item = QueryCacheManager.Get<List<T>>(key);
4848

4949
if (item == null)
5050
{
5151
item = query.AsNoTracking().ToList();
52-
item = QueryCacheManager.Cache.AddOrGetExisting(key, item, policy) ?? item;
52+
item = QueryCacheManager.AddOrGetExisting(key, item, policy) ?? item;
5353
QueryCacheManager.AddCacheTag(key, tags);
5454
}
5555

56-
item = item.IfDbNullThenNull();
57-
5856
return (IEnumerable<T>) item;
5957
}
6058

@@ -79,17 +77,15 @@ public static IEnumerable<T> FromCache<T>(this IQueryable<T> query, DateTimeOffs
7977

8078
var key = QueryCacheManager.GetCacheKey(query, tags);
8179

82-
var item = QueryCacheManager.Cache.Get(key);
80+
var item = QueryCacheManager.Get<List<T>>(key);
8381

8482
if (item == null)
8583
{
8684
item = query.AsNoTracking().ToList();
87-
item = QueryCacheManager.Cache.AddOrGetExisting(key, item, absoluteExpiration) ?? item;
85+
item = QueryCacheManager.AddOrGetExisting(key, item, absoluteExpiration) ?? item;
8886
QueryCacheManager.AddCacheTag(key, tags);
8987
}
9088

91-
item = item.IfDbNullThenNull();
92-
9389
return (IEnumerable<T>) item;
9490
}
9591

src/shared/Z.EF.Plus.QueryCache.Shared/Extensions/IQueryable`/FromCacheAsync.cs

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -51,17 +51,15 @@ public static Task<IEnumerable<T>> FromCacheAsync<T>(this IQueryable<T> query, C
5151

5252
var result = Task.Run(() =>
5353
{
54-
var item = QueryCacheManager.Cache.Get(key);
54+
var item = QueryCacheManager.Get<List<T>>(key);
5555

5656
if (item == null)
5757
{
5858
item = query.AsNoTracking().ToList();
59-
item = QueryCacheManager.Cache.AddOrGetExisting(key, item, policy) ?? item;
59+
item = QueryCacheManager.AddOrGetExisting(key, item, policy) ?? item;
6060
QueryCacheManager.AddCacheTag(key, tags);
6161
}
6262

63-
item = item.IfDbNullThenNull();
64-
6563
return (IEnumerable<T>) item;
6664
});
6765

@@ -93,17 +91,15 @@ public static Task<IEnumerable<T>> FromCacheAsync<T>(this IQueryable<T> query, D
9391

9492
var result = Task.Run(() =>
9593
{
96-
var item = QueryCacheManager.Cache.Get(key);
94+
var item = QueryCacheManager.Get<List<T>>(key);
9795

9896
if (item == null)
9997
{
10098
item = query.AsNoTracking().ToList();
101-
item = QueryCacheManager.Cache.AddOrGetExisting(key, item, absoluteExpiration) ?? item;
99+
item = QueryCacheManager.AddOrGetExisting(key, item, absoluteExpiration) ?? item;
102100
QueryCacheManager.AddCacheTag(key, tags);
103101
}
104102

105-
item = item.IfDbNullThenNull();
106-
107103
return (IEnumerable<T>) item;
108104
});
109105

@@ -148,17 +144,15 @@ public static Task<IEnumerable<T>> FromCacheAsync<T>(this IQueryable<T> query, p
148144

149145
var key = QueryCacheManager.GetCacheKey(query, tags);
150146

151-
var item = QueryCacheManager.Cache.Get(key);
147+
var item = QueryCacheManager.Get<List<T>>(key);
152148

153149
if (item == null)
154150
{
155151
item = await query.AsNoTracking().ToListAsync(cancellationToken).ConfigureAwait(false);
156-
item = QueryCacheManager.Cache.AddOrGetExisting(key, item, policy) ?? item;
152+
item = QueryCacheManager.AddOrGetExisting(key, item, policy) ?? item;
157153
QueryCacheManager.AddCacheTag(key, tags);
158154
}
159155

160-
item = item.IfDbNullThenNull();
161-
162156
return (IEnumerable<T>) item;
163157
}
164158

@@ -201,17 +195,15 @@ public static Task<IEnumerable<T>> FromCacheAsync<T>(this IQueryable<T> query, C
201195

202196
var key = QueryCacheManager.GetCacheKey(query, tags);
203197

204-
var item = QueryCacheManager.Cache.Get(key);
198+
var item = QueryCacheManager.Get<List<T>>(key);
205199

206200
if (item == null)
207201
{
208202
item = await query.AsNoTracking().ToListAsync(cancellationToken).ConfigureAwait(false);
209-
item = QueryCacheManager.Cache.AddOrGetExisting(key, item, absoluteExpiration) ?? item;
203+
item = QueryCacheManager.AddOrGetExisting(key, item, absoluteExpiration) ?? item;
210204
QueryCacheManager.AddCacheTag(key, tags);
211205
}
212206

213-
item = item.IfDbNullThenNull();
214-
215207
return (IEnumerable<T>) item;
216208
}
217209

src/shared/Z.EF.Plus.QueryCache.Shared/Extensions/QueryDeferred`/FromCache.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
// Copyright © ZZZ Projects Inc. 2014 - 2016. All rights reserved.
77

88
using System;
9-
9+
using System.Collections.Generic;
1010
#if EF5 || EF6
1111
using System.Runtime.Caching;
1212

@@ -41,13 +41,13 @@ public static T FromCache<T>(this QueryDeferred<T> query, CacheItemPolicy policy
4141

4242
var key = QueryCacheManager.GetCacheKey(query, tags);
4343

44-
var item = QueryCacheManager.Cache.Get(key);
44+
var item = QueryCacheManager.GetDeferred(key);
4545

4646
if (item == null)
4747
{
4848
item = query.Execute();
4949

50-
item = QueryCacheManager.Cache.AddOrGetExisting(key, item ?? DBNull.Value, policy) ?? item;
50+
item = QueryCacheManager.AddOrGetExistingDeferred<T>(key, item ?? DBNull.Value, policy) ?? item;
5151
QueryCacheManager.AddCacheTag(key, tags);
5252
}
5353

@@ -77,13 +77,13 @@ public static T FromCache<T>(this QueryDeferred<T> query, DateTimeOffset absolut
7777

7878
var key = QueryCacheManager.GetCacheKey(query, tags);
7979

80-
var item = QueryCacheManager.Cache.Get(key);
80+
var item = QueryCacheManager.GetDeferred(key);
8181

8282
if (item == null)
8383
{
8484
item = query.Execute();
8585

86-
item = QueryCacheManager.Cache.AddOrGetExisting(key, item ?? DBNull.Value, absoluteExpiration) ?? item;
86+
item = QueryCacheManager.AddOrGetExistingDeferred<T>(key, item ?? DBNull.Value, absoluteExpiration) ?? item;
8787
QueryCacheManager.AddCacheTag(key, tags);
8888
}
8989

0 commit comments

Comments
 (0)