Skip to content

Commit 25fa51e

Browse files
author
zzzprojects
committed
Add UseLock and AllowQueryBatch options
Add UseLock and AllowQueryBatch options
1 parent 2eb509a commit 25fa51e

File tree

10 files changed

+72
-9
lines changed

10 files changed

+72
-9
lines changed

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

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public class BatchDelete
3939
/// <summary>The command text template.</summary>
4040
internal const string CommandTextTemplate = @"
4141
DELETE
42-
FROM A
42+
FROM A {Hint}
4343
FROM {TableName} AS A
4444
INNER JOIN ( {Select}
4545
) AS B ON {PrimaryKeys}
@@ -85,7 +85,7 @@ WHILE @rowAffected IS NULL
8585
OR @rowAffected > 0
8686
BEGIN
8787
DELETE TOP ({Top})
88-
FROM A
88+
FROM A {Hint}
8989
FROM {TableName} AS A
9090
INNER JOIN ( {Select}
9191
) AS B ON {PrimaryKeys}
@@ -113,7 +113,7 @@ WAITFOR DELAY '{Delay}'
113113
END
114114
115115
DELETE TOP ({Top})
116-
FROM A
116+
FROM A {Hint}
117117
FROM {TableName} AS A
118118
INNER JOIN ( {Select}
119119
) AS B ON {PrimaryKeys}
@@ -139,6 +139,10 @@ public BatchDelete()
139139
/// <value>The batch delay interval in milliseconds (The wait time between batch).</value>
140140
public int BatchDelayInterval { get; set; }
141141

142+
/// <summary>Gets or sets a value indicating whether the query use table lock.</summary>
143+
/// <value>True if use table lock, false if not.</value>
144+
public bool UseTableLock { get; set; }
145+
142146
/// <summary>Gets or sets the DbCommand before being executed.</summary>
143147
/// <value>The DbCommand before being executed.</value>
144148
public Action<DbCommand> Executing { get; set; }
@@ -444,7 +448,8 @@ internal DbCommand CreateCommand<T>(ObjectQuery query, SchemaEntityType<T> entit
444448
.Replace("{Select}", querySelect)
445449
.Replace("{PrimaryKeys}", primaryKeys)
446450
.Replace("{Top}", BatchSize.ToString())
447-
.Replace("{Delay}", TimeSpan.FromMilliseconds(BatchDelayInterval).ToString(@"hh\:mm\:ss\:fff"));
451+
.Replace("{Delay}", TimeSpan.FromMilliseconds(BatchDelayInterval).ToString(@"hh\:mm\:ss\:fff"))
452+
.Replace("{Hint}", UseTableLock ? "WITH ( TABLOCK )" : "");
448453

449454
// CREATE command
450455
command.CommandText = commandTextTemplate;
@@ -639,7 +644,8 @@ public DbCommand CreateCommand(IQueryable query, IEntityType entity)
639644
.Replace("{Select}", querySelect)
640645
.Replace("{PrimaryKeys}", primaryKeys)
641646
.Replace("{Top}", BatchSize.ToString())
642-
.Replace("{Delay}", TimeSpan.FromMilliseconds(BatchDelayInterval).ToString(@"hh\:mm\:ss\:fff"));
647+
.Replace("{Delay}", TimeSpan.FromMilliseconds(BatchDelayInterval).ToString(@"hh\:mm\:ss\:fff"))
648+
.Replace("{Hint}", UseTableLock ? "WITH ( TABLOCK )" : "");
643649

644650
// CREATE command
645651
var command = query.GetDbContext().CreateStoreCommand();

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

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public class BatchUpdate
3636
{
3737
/// <summary>The command text template.</summary>
3838
internal const string CommandTextTemplate = @"
39-
UPDATE A
39+
UPDATE A {Hint}
4040
SET {SetValue}
4141
FROM {TableName} AS A
4242
INNER JOIN ( {Select}
@@ -144,6 +144,10 @@ public BatchUpdate()
144144
/// <value>The DbCommand before being executed.</value>
145145
public Action<DbCommand> Executing { get; set; }
146146

147+
/// <summary>Gets or sets a value indicating whether the query use table lock.</summary>
148+
/// <value>True if use table lock, false if not.</value>
149+
public bool UseTableLock { get; set; }
150+
147151
/// <summary>Executes the batch delete operation.</summary>
148152
/// <typeparam name="T">The type of elements of the query.</typeparam>
149153
/// <param name="query">The query used to execute the batch operation.</param>
@@ -429,7 +433,8 @@ internal DbCommand CreateCommand<T>(ObjectQuery query, SchemaEntityType<T> entit
429433
commandTextTemplate = commandTextTemplate.Replace("{TableName}", tableName)
430434
.Replace("{Select}", querySelect)
431435
.Replace("{PrimaryKeys}", primaryKeys)
432-
.Replace("{SetValue}", setValues);
436+
.Replace("{SetValue}", setValues)
437+
.Replace("{Hint}", UseTableLock ? "WITH ( TABLOCK )" : "");
433438

434439
// CREATE command
435440
command.CommandText = commandTextTemplate;
@@ -660,7 +665,8 @@ public DbCommand CreateCommand(IQueryable query, IEntityType entity, List<Tuple<
660665
commandTextTemplate = commandTextTemplate.Replace("{TableName}", tableName)
661666
.Replace("{Select}", querySelect)
662667
.Replace("{PrimaryKeys}", primaryKeys)
663-
.Replace("{SetValue}", setValues);
668+
.Replace("{SetValue}", setValues)
669+
.Replace("{Hint}", UseTableLock ? "WITH ( TABLOCK )" : "");
664670

665671
// CREATE command
666672
var command = query.GetDbContext().CreateStoreCommand();

src/shared/Z.EF.Plus.QueryFuture.Shared/Extensions/IQueryable`/Future.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,13 @@ public static partial class QueryFutureExtensions
2929
/// </returns>
3030
public static QueryFutureEnumerable<T> Future<T>(this IQueryable<T> query)
3131
{
32+
if (!QueryFutureManager.AllowQueryBatch)
33+
{
34+
var queryFuture = new QueryFutureEnumerable<T>(null, null);
35+
queryFuture.GetResultDirectly(query);
36+
return queryFuture;
37+
}
38+
3239
#if EF5 || EF6
3340
var objectQuery = query.GetObjectQuery();
3441
var futureBatch = QueryFutureManager.AddOrGetBatch(objectQuery.Context);

src/shared/Z.EF.Plus.QueryFuture.Shared/Extensions/IQueryable`/FutureValue.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,13 @@ public static partial class QueryFutureExtensions
2929
/// </returns>
3030
public static QueryFutureValue<TResult> FutureValue<TResult>(this IQueryable<TResult> query)
3131
{
32+
if (!QueryFutureManager.AllowQueryBatch)
33+
{
34+
var futureValue = new QueryFutureValue<TResult>(null, null);
35+
futureValue.GetResultDirectly(query);
36+
return futureValue;
37+
}
38+
3239
#if EF5 || EF6
3340
var objectQuery = query.GetObjectQuery();
3441
var futureBatch = QueryFutureManager.AddOrGetBatch(objectQuery.Context);

src/shared/Z.EF.Plus.QueryFuture.Shared/Extensions/QueryDeferred/FutureValue.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,13 @@ public static partial class QueryFutureExtensions
2121
/// </returns>
2222
public static QueryFutureValue<TResult> FutureValue<TResult>(this QueryDeferred<TResult> query)
2323
{
24+
if (!QueryFutureManager.AllowQueryBatch)
25+
{
26+
var futureValue = new QueryFutureValue<TResult>(null, null);
27+
futureValue.GetResultDirectly(query.Query);
28+
return futureValue;
29+
}
30+
2431
#if EF5 || EF6
2532
var objectQuery = query.Query.GetObjectQuery();
2633
var futureBatch = QueryFutureManager.AddOrGetBatch(objectQuery.Context);

src/shared/Z.EF.Plus.QueryFuture.Shared/QueryFutureBatch.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,17 @@ public void ExecuteQueries()
9393
return;
9494
}
9595

96+
if (!QueryFutureManager.AllowQueryBatch)
97+
{
98+
foreach (var query in Queries)
99+
{
100+
query.GetResultDirectly();
101+
}
102+
103+
Queries.Clear();
104+
return;
105+
}
106+
96107
#if EF5 || EF6
97108
var connection = (EntityConnection)Context.Connection;
98109
#elif EFCORE

src/shared/Z.EF.Plus.QueryFuture.Shared/QueryFutureEnumerable.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,5 +165,12 @@ public override void GetResultDirectly()
165165

166166
SetResult(enumerator);
167167
}
168+
169+
internal void GetResultDirectly(IQueryable<T> query)
170+
{
171+
var enumerator = query.GetEnumerator();
172+
173+
SetResult(enumerator);
174+
}
168175
}
169176
}

src/shared/Z.EF.Plus.QueryFuture.Shared/QueryFutureManager.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ static QueryFutureManager()
3838
#endif
3939
}
4040

41+
/// <summary>Gets or sets a value indicating whether we allow query batch.</summary>
42+
/// <value>True if allow query batch, false if not.</value>
43+
public static bool AllowQueryBatch { get; set; } = true;
44+
4145
/// <summary>Gets or sets the weak table used to cache future batch associated to a context.</summary>
4246
/// <value>The weak table used to cache future batch associated to a context.</value>
4347
#if EF5 || EF6

src/shared/Z.EF.Plus.QueryFuture.Shared/QueryFutureValue.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,14 @@ public override void GetResultDirectly()
131131
HasValue = true;
132132
}
133133

134+
internal void GetResultDirectly(IQueryable<TResult> query)
135+
{
136+
var value = query.Provider.Execute<TResult>(query.Expression);
137+
138+
_result = value;
139+
HasValue = true;
140+
}
141+
134142
/// <summary>
135143
/// Performs an implicit conversion from QueryFutureValue to TResult.
136144
/// </summary>

version.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
v1.6.9
1+
v1.6.10

0 commit comments

Comments
 (0)