Skip to content

Commit 793deb7

Browse files
Update source
Update source
1 parent 5ab77cd commit 793deb7

File tree

7 files changed

+136
-16
lines changed

7 files changed

+136
-16
lines changed

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

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,13 +86,14 @@ INNER JOIN ( {Select} ) AS B ON {PrimaryKeys}
8686

8787
/// <summary>The command text template with WHILE loop.</summary>
8888
internal const string CommandTextWhileTemplate = @"
89+
DECLARE @stop int
8990
DECLARE @rowAffected INT
9091
DECLARE @totalRowAffected INT
9192
93+
SET @stop = 0
9294
SET @totalRowAffected = 0
9395
94-
WHILE @rowAffected IS NULL
95-
OR @rowAffected > 0
96+
WHILE @stop=0
9697
BEGIN
9798
DELETE TOP ({Top})
9899
FROM A {Hint}
@@ -102,20 +103,24 @@ INNER JOIN ( {Select}
102103
103104
SET @rowAffected = @@ROWCOUNT
104105
SET @totalRowAffected = @totalRowAffected + @rowAffected
106+
107+
IF @rowAffected < {Top}
108+
SET @stop = 1
105109
END
106110
107111
SELECT @totalRowAffected
108112
";
109113

110114
/// <summary>The command text template with DELAY and WHILE loop</summary>
111115
internal const string CommandTextWhileDelayTemplate = @"
116+
DECLARE @stop int
112117
DECLARE @rowAffected INT
113118
DECLARE @totalRowAffected INT
114119
120+
SET @stop = 0
115121
SET @totalRowAffected = 0
116122
117-
WHILE @rowAffected IS NULL
118-
OR @rowAffected > 0
123+
WHILE @stop=0
119124
BEGIN
120125
IF @rowAffected IS NOT NULL
121126
BEGIN
@@ -130,6 +135,9 @@ INNER JOIN ( {Select}
130135
131136
SET @rowAffected = @@ROWCOUNT
132137
SET @totalRowAffected = @totalRowAffected + @rowAffected
138+
139+
IF @rowAffected < {Top}
140+
SET @stop = 1
133141
END
134142
135143
SELECT @totalRowAffected

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

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -95,13 +95,14 @@ INNER JOIN ( {Select}
9595
#if TODO
9696
/// <summary>The command text template with WHILE loop.</summary>
9797
internal const string CommandTextWhileTemplate = @"
98+
DECLARE @stop int
9899
DECLARE @rowAffected INT
99100
DECLARE @totalRowAffected INT
100101
102+
SET @stop = 0
101103
SET @totalRowAffected = 0
102104
103-
WHILE @rowAffected IS NULL
104-
OR @rowAffected > 0
105+
WHILE @stop=0
105106
BEGIN
106107
DELETE TOP ({Top})
107108
FROM A
@@ -111,20 +112,24 @@ INNER JOIN ( {Select}
111112
112113
SET @rowAffected = @@ROWCOUNT
113114
SET @totalRowAffected = @totalRowAffected + @rowAffected
115+
116+
IF @rowAffected < {Top}
117+
SET @stop = 1
114118
END
115119
116120
SELECT @totalRowAffected
117121
";
118122

119123
/// <summary>The command text template with DELAY and WHILE loop</summary>
120124
internal const string CommandTextWhileDelayTemplate = @"
125+
DECLARE @stop int
121126
DECLARE @rowAffected INT
122127
DECLARE @totalRowAffected INT
123128
129+
SET @stop = 0
124130
SET @totalRowAffected = 0
125131
126-
WHILE @rowAffected IS NULL
127-
OR @rowAffected > 0
132+
WHILE @stop=0
128133
BEGIN
129134
IF @rowAffected IS NOT NULL
130135
BEGIN
@@ -139,6 +144,9 @@ INNER JOIN ( {Select}
139144
140145
SET @rowAffected = @@ROWCOUNT
141146
SET @totalRowAffected = @totalRowAffected + @rowAffected
147+
148+
IF @rowAffected < {Top}
149+
SET @stop = 1
142150
END
143151
144152
SELECT @totalRowAffected
@@ -989,14 +997,14 @@ public List<Tuple<string, object>> GetInnerValues<T>(IQueryable<T> query, Expres
989997

990998
#endif
991999
// GET updateFactory command
992-
var values = ResolveUpdateFromQueryDictValues(updateFactory);
1000+
var values = ResolveUpdateFromQueryDictValues(updateFactory);
9931001
var destinationValues = new List<Tuple<string, object>>();
9941002

9951003
int valueI = -1;
9961004
foreach (var value in values)
997-
{
1005+
{
9981006
valueI++;
999-
1007+
10001008
#if EF5 || EF6
10011009
// FIND the mapped column
10021010
var column = mapping.ScalarProperties.Find(x => x.Name == value.Key);
@@ -1133,8 +1141,7 @@ public List<Tuple<string, object>> GetInnerValues<T>(IQueryable<T> query, Expres
11331141
#elif EFCORE
11341142
RelationalQueryContext queryContext;
11351143
var command = ((IQueryable)result).CreateCommand(out queryContext);
1136-
var commandText = command.CommandText;
1137-
1144+
var commandText = command.CommandText;
11381145
#if NETSTANDARD1_3
11391146
// GET the 'value' part
11401147
var pos = commandText.IndexOf("AS [value]" + Environment.NewLine + "FROM", StringComparison.CurrentCultureIgnoreCase) != -1 ?
@@ -1198,7 +1205,17 @@ public List<Tuple<string, object>> GetInnerValues<T>(IQueryable<T> query, Expres
11981205
}
11991206
else
12001207
{
1201-
destinationValues.Add(new Tuple<string, object>(columnName, value.Value ?? DBNull.Value));
1208+
var val = value.Value;
1209+
#if NETSTANDARD
1210+
var prop = entity.FindProperty(value.Key);
1211+
1212+
if (prop != null && prop.GetAnnotations()
1213+
.Any(x => x.Name == "ProviderClrType" && x.Value == typeof(String)))
1214+
{
1215+
val = val?.ToString();
1216+
}
1217+
#endif
1218+
destinationValues.Add(new Tuple<string, object>(columnName, val ?? DBNull.Value));
12021219
}
12031220
}
12041221

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

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ public static partial class QueryCacheExtensions
3838
/// <returns>The result of the query.</returns>
3939
public static IEnumerable<T> FromCache<T>(this IQueryable<T> query, CacheItemPolicy policy, params string[] tags) where T : class
4040
{
41+
if (!QueryCacheManager.IsEnabled)
42+
{
43+
return query.AsNoTracking().ToList();
44+
}
4145
var key = QueryCacheManager.GetCacheKey(query, tags);
4246

4347
var item = QueryCacheManager.Cache.Get(key);
@@ -67,7 +71,12 @@ public static IEnumerable<T> FromCache<T>(this IQueryable<T> query, CacheItemPol
6771
/// </param>
6872
/// <returns>The result of the query.</returns>
6973
public static IEnumerable<T> FromCache<T>(this IQueryable<T> query, DateTimeOffset absoluteExpiration, params string[] tags) where T : class
70-
{
74+
{
75+
if (!QueryCacheManager.IsEnabled)
76+
{
77+
return query.AsNoTracking().ToList();
78+
}
79+
7180
var key = QueryCacheManager.GetCacheKey(query, tags);
7281

7382
var item = QueryCacheManager.Cache.Get(key);
@@ -114,6 +123,11 @@ public static IEnumerable<T> FromCache<T>(this IQueryable<T> query, params strin
114123
/// <returns>The result of the query.</returns>
115124
public static IEnumerable<T> FromCache<T>(this IQueryable<T> query, MemoryCacheEntryOptions options, params string[] tags) where T : class
116125
{
126+
if (!QueryCacheManager.IsEnabled)
127+
{
128+
return query.AsNoTracking().ToList();
129+
}
130+
117131
var key = QueryCacheManager.GetCacheKey(query, tags);
118132

119133
object item;

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

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,13 @@ public static partial class QueryCacheExtensions
4040
/// <returns>The result of the query.</returns>
4141
public static Task<IEnumerable<T>> FromCacheAsync<T>(this IQueryable<T> query, CacheItemPolicy policy, params string[] tags) where T : class
4242
{
43+
if (!QueryCacheManager.IsEnabled)
44+
{
45+
return Task.Run(() => {
46+
return (IEnumerable<T>)query.AsNoTracking().ToList();
47+
});
48+
}
49+
4350
var key = QueryCacheManager.GetCacheKey(query, tags);
4451

4552
var result = Task.Run(() =>
@@ -75,6 +82,13 @@ public static Task<IEnumerable<T>> FromCacheAsync<T>(this IQueryable<T> query, C
7582
/// <returns>The result of the query.</returns>
7683
public static Task<IEnumerable<T>> FromCacheAsync<T>(this IQueryable<T> query, DateTimeOffset absoluteExpiration, params string[] tags) where T : class
7784
{
85+
if (!QueryCacheManager.IsEnabled)
86+
{
87+
return Task.Run(() => {
88+
return (IEnumerable<T>)query.AsNoTracking().ToList();
89+
});
90+
}
91+
7892
var key = QueryCacheManager.GetCacheKey(query, tags);
7993

8094
var result = Task.Run(() =>
@@ -127,6 +141,11 @@ public static Task<IEnumerable<T>> FromCacheAsync<T>(this IQueryable<T> query, p
127141
/// <returns>The result of the query.</returns>
128142
public static async Task<IEnumerable<T>> FromCacheAsync<T>(this IQueryable<T> query, CacheItemPolicy policy, CancellationToken cancellationToken = default(CancellationToken), params string[] tags) where T : class
129143
{
144+
if (!QueryCacheManager.IsEnabled)
145+
{
146+
return await query.AsNoTracking().ToListAsync(cancellationToken).ConfigureAwait(false);
147+
}
148+
130149
var key = QueryCacheManager.GetCacheKey(query, tags);
131150

132151
var item = QueryCacheManager.Cache.Get(key);
@@ -175,6 +194,11 @@ public static Task<IEnumerable<T>> FromCacheAsync<T>(this IQueryable<T> query, C
175194
/// <returns>The result of the query.</returns>
176195
public static async Task<IEnumerable<T>> FromCacheAsync<T>(this IQueryable<T> query, DateTimeOffset absoluteExpiration, CancellationToken cancellationToken = default(CancellationToken), params string[] tags) where T : class
177196
{
197+
if (!QueryCacheManager.IsEnabled)
198+
{
199+
return await query.AsNoTracking().ToListAsync(cancellationToken).ConfigureAwait(false);
200+
}
201+
178202
var key = QueryCacheManager.GetCacheKey(query, tags);
179203

180204
var item = QueryCacheManager.Cache.Get(key);
@@ -256,6 +280,11 @@ public static Task<IEnumerable<T>> FromCacheAsync<T>(this IQueryable<T> query, p
256280
/// <returns>The result of the query.</returns>
257281
public static async Task<IEnumerable<T>> FromCacheAsync<T>(this IQueryable<T> query, MemoryCacheEntryOptions options, CancellationToken cancellationToken = default(CancellationToken), params string[] tags) where T : class
258282
{
283+
if (!QueryCacheManager.IsEnabled)
284+
{
285+
return await query.AsNoTracking().ToListAsync(cancellationToken).ConfigureAwait(false);
286+
}
287+
259288
var key = QueryCacheManager.GetCacheKey(query, tags);
260289

261290
object item;

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@ public static partial class QueryCacheExtensions
3434
/// <returns>The result of the query.</returns>
3535
public static T FromCache<T>(this QueryDeferred<T> query, CacheItemPolicy policy, params string[] tags)
3636
{
37+
if (!QueryCacheManager.IsEnabled)
38+
{
39+
return query.Execute();
40+
}
41+
3742
var key = QueryCacheManager.GetCacheKey(query, tags);
3843

3944
var item = QueryCacheManager.Cache.Get(key);
@@ -65,6 +70,11 @@ public static T FromCache<T>(this QueryDeferred<T> query, CacheItemPolicy policy
6570
/// <returns>The result of the query.</returns>
6671
public static T FromCache<T>(this QueryDeferred<T> query, DateTimeOffset absoluteExpiration, params string[] tags)
6772
{
73+
if (!QueryCacheManager.IsEnabled)
74+
{
75+
return query.Execute();
76+
}
77+
6878
var key = QueryCacheManager.GetCacheKey(query, tags);
6979

7080
var item = QueryCacheManager.Cache.Get(key);
@@ -112,6 +122,11 @@ public static T FromCache<T>(this QueryDeferred<T> query, params string[] tags)
112122
/// <returns>The result of the query.</returns>
113123
public static T FromCache<T>(this QueryDeferred<T> query, MemoryCacheEntryOptions options, params string[] tags)
114124
{
125+
if (!QueryCacheManager.IsEnabled)
126+
{
127+
return query.Execute();
128+
}
129+
115130
var key = QueryCacheManager.GetCacheKey(query, tags);
116131

117132
object item;

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

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,14 @@ public static partial class QueryCacheExtensions
3636
/// <returns>The result of the query.</returns>
3737
public static Task<T> FromCacheAsync<T>(this QueryDeferred<T> query, CacheItemPolicy policy, params string[] tags)
3838
{
39+
if (!QueryCacheManager.IsEnabled)
40+
{
41+
return Task.Run(() =>
42+
{
43+
return query.Execute();
44+
});
45+
}
46+
3947
var key = QueryCacheManager.GetCacheKey(query, tags);
4048

4149
var result = Task.Run(() =>
@@ -71,6 +79,14 @@ public static Task<T> FromCacheAsync<T>(this QueryDeferred<T> query, CacheItemPo
7179
/// <returns>The result of the query.</returns>
7280
public static Task<T> FromCacheAsync<T>(this QueryDeferred<T> query, DateTimeOffset absoluteExpiration, params string[] tags)
7381
{
82+
if (!QueryCacheManager.IsEnabled)
83+
{
84+
return Task.Run(() =>
85+
{
86+
return query.Execute();
87+
});
88+
}
89+
7490
var key = QueryCacheManager.GetCacheKey(query, tags);
7591

7692
var result = Task.Run(() =>
@@ -122,7 +138,12 @@ public static Task<T> FromCacheAsync<T>(this QueryDeferred<T> query, params stri
122138
/// </param>
123139
/// <returns>The result of the query.</returns>
124140
public static async Task<T> FromCacheAsync<T>(this QueryDeferred<T> query, CacheItemPolicy policy, CancellationToken cancellationToken = default(CancellationToken), params string[] tags)
125-
{
141+
{
142+
if (!QueryCacheManager.IsEnabled)
143+
{
144+
return await query.ExecuteAsync(cancellationToken).ConfigureAwait(false);
145+
}
146+
126147
var key = QueryCacheManager.GetCacheKey(query, tags);
127148

128149
var item = QueryCacheManager.Cache.Get(key);
@@ -171,6 +192,11 @@ public static Task<T> FromCacheAsync<T>(this QueryDeferred<T> query, CacheItemPo
171192
/// <returns>The result of the query.</returns>
172193
public static async Task<T> FromCacheAsync<T>(this QueryDeferred<T> query, DateTimeOffset absoluteExpiration, CancellationToken cancellationToken = default(CancellationToken), params string[] tags)
173194
{
195+
if (!QueryCacheManager.IsEnabled)
196+
{
197+
return await query.ExecuteAsync(cancellationToken).ConfigureAwait(false);
198+
}
199+
174200
var key = QueryCacheManager.GetCacheKey(query, tags);
175201

176202
var item = QueryCacheManager.Cache.Get(key);
@@ -252,6 +278,11 @@ public static Task<T> FromCacheAsync<T>(this QueryDeferred<T> query, params stri
252278
/// <returns>The result of the query.</returns>
253279
public static async Task<T> FromCacheAsync<T>(this QueryDeferred<T> query, MemoryCacheEntryOptions options, CancellationToken cancellationToken = default(CancellationToken), params string[] tags)
254280
{
281+
if (!QueryCacheManager.IsEnabled)
282+
{
283+
return await query.ExecuteAsync(cancellationToken).ConfigureAwait(false);
284+
}
285+
255286
var key = QueryCacheManager.GetCacheKey(query, tags);
256287

257288
object item;

src/shared/Z.EF.Plus.QueryCache.Shared/QueryCacheManager.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,12 @@ public static Func<MemoryCacheEntryOptions> MemoryCacheEntryOptionsFactory
143143
/// <value>The cache key factory.</value>
144144
public static Func<IQueryable, string[], string> CacheKeyFactory { get; set; }
145145

146+
/// <summary>
147+
/// Gets or sets a value indicating whether the Query Cache is enabled
148+
/// </summary>
149+
/// <value>true if the Query Cache is enabled.</value>
150+
public static bool IsEnabled { get; set; } = true;
151+
146152
/// <summary>
147153
/// Gets or sets a value indicating whether the connection in cache key should be included.
148154
/// </summary>

0 commit comments

Comments
 (0)