[#127] 添加IEntityQueryConfiguration支持Enumerator的顺序配置

This commit is contained in:
xuejiaming 2022-03-01 15:39:09 +08:00
parent d289b2a849
commit 5fb0e074fb
6 changed files with 23 additions and 16 deletions

View File

@ -227,7 +227,7 @@ namespace Sample.SqlServer.Controllers
public async Task<IActionResult> Get2a3()
{
Console.WriteLine("Get2a3-------------");
var sysUserMods = await _defaultTableDbContext.Set<SysUserSalary>().UseConnectionMode(2).Skip(2).Take(2).OrderByDescending(o=>o.DateOfMonth).ToListAsync();
var sysUserMods = await _defaultTableDbContext.Set<SysUserSalary>().Skip(2).Take(2).ToListAsync();
return Ok(sysUserMods);
}
[HttpGet]

View File

@ -30,12 +30,13 @@ namespace Sample.SqlServer.Shardings
builder.ShardingTailComparer(Comparer<string>.Default, false);//表示他是倒叙
//DateOfMonth的排序和月份分片的后缀一致所以用true如果false,无果无关就不需要配置
builder.AddOrder(o => o.DateOfMonth, false);
builder.AddDefaultSequenceQueryTrip(false, CircuitBreakerMethodNameEnum.FirstOrDefault);
builder.AddDefaultSequenceQueryTrip(false, CircuitBreakerMethodNameEnum.FirstOrDefault, CircuitBreakerMethodNameEnum.Enumerator);
#endregion
builder.AddConnectionsLimit(2, LimitMethodNameEnum.First, LimitMethodNameEnum.FirstOrDefault, LimitMethodNameEnum.Any, LimitMethodNameEnum.LastOrDefault, LimitMethodNameEnum.Last, LimitMethodNameEnum.Max, LimitMethodNameEnum.Min);
builder.AddConnectionsLimit(1, LimitMethodNameEnum.Enumerator);
}
}

View File

@ -53,6 +53,7 @@ namespace Sample.SqlServer
.AddConfig(op =>
{
op.ConfigId = "c1";
op.MaxQueryConnectionsLimit = 5;
op.UseShardingQuery((conStr, builder) =>
{
builder.UseSqlServer(conStr).UseLoggerFactory(efLogger).ReplaceService<IQuerySqlGeneratorFactory, ShardingSqlServerQuerySqlGeneratorFactory<DefaultShardingDbContext>>();

View File

@ -6,6 +6,7 @@ namespace ShardingCore.Sharding.EntityQueryConfigurations
{
public class EntityQueryMetadata
{
public const string QUERY_ENUMERATOR = "Enumerator";
private static readonly IDictionary<CircuitBreakerMethodNameEnum, string> CircuitBreakerMethodNameSupports;
private static readonly IDictionary<LimitMethodNameEnum, string> LimitMethodNameSupports;
@ -21,7 +22,8 @@ namespace ShardingCore.Sharding.EntityQueryConfigurations
{ CircuitBreakerMethodNameEnum.SingleOrDefault, nameof(Queryable.SingleOrDefault) },
{ CircuitBreakerMethodNameEnum.Any, nameof(Queryable.Any) },
{ CircuitBreakerMethodNameEnum.All, nameof(Queryable.All) },
{ CircuitBreakerMethodNameEnum.Contains, nameof(Queryable.Contains) }
{ CircuitBreakerMethodNameEnum.Contains, nameof(Queryable.Contains) },
{ CircuitBreakerMethodNameEnum.Enumerator,QUERY_ENUMERATOR }
};
LimitMethodNameSupports = new Dictionary<LimitMethodNameEnum, string>()
{
@ -39,7 +41,8 @@ namespace ShardingCore.Sharding.EntityQueryConfigurations
{ LimitMethodNameEnum.Count, nameof(Queryable.Count) },
{ LimitMethodNameEnum.LongCount, nameof(Queryable.LongCount) },
{ LimitMethodNameEnum.Sum, nameof(Queryable.Sum) },
{ LimitMethodNameEnum.Average, nameof(Queryable.Average) }
{ LimitMethodNameEnum.Average, nameof(Queryable.Average) },
{ LimitMethodNameEnum.Enumerator, QUERY_ENUMERATOR }
};
}

View File

@ -16,7 +16,8 @@ namespace ShardingCore.Sharding.EntityQueryConfigurations
SingleOrDefault,
Any,
All,
Contains
Contains,
Enumerator
}
public enum LimitMethodNameEnum
{
@ -34,6 +35,7 @@ namespace ShardingCore.Sharding.EntityQueryConfigurations
Count,
LongCount,
Sum,
Average
Average,
Enumerator
}
}

View File

@ -34,7 +34,7 @@ namespace ShardingCore.Sharding
* @Date: Monday, 25 January 2021 11:38:27
* @Email: 326308290@qq.com
*/
public class StreamMergeContext<TEntity> : ISeqQueryProvider, IParseContext, IDisposable,IPrint
public class StreamMergeContext<TEntity> : ISeqQueryProvider, IParseContext, IDisposable, IPrint
#if !EFCORE2
, IAsyncDisposable
#endif
@ -133,14 +133,13 @@ namespace ShardingCore.Sharding
ShardingTailComparer =
virtualTable.EntityQueryMetadata.DefaultTailComparer ?? Comparer<string>.Default;
TailComparerNeedReverse = virtualTable.EntityQueryMetadata.DefaultTailComparerNeedReverse;
string methodName = null;
if (!MergeQueryCompilerContext.IsEnumerableQuery())
string methodName = MergeQueryCompilerContext.IsEnumerableQuery() ?
EntityQueryMetadata.QUERY_ENUMERATOR :
((MethodCallExpression)MergeQueryCompilerContext.GetQueryExpression()).Method.Name;
if (virtualTable.EntityQueryMetadata.TryGetConnectionsLimit(methodName, out var limit))
{
methodName = ((MethodCallExpression)MergeQueryCompilerContext.GetQueryExpression()).Method.Name;
if (virtualTable.EntityQueryMetadata.TryGetConnectionsLimit(methodName, out var limit))
{
maxParallelExecuteCount = Math.Min(limit, maxParallelExecuteCount);
}
maxParallelExecuteCount = Math.Min(limit, maxParallelExecuteCount);
}
var isSequence = mergeQueryCompilerContext.IsSequence();
@ -184,7 +183,8 @@ namespace ShardingCore.Sharding
nameof(Queryable.Last) == methodName ||
nameof(Queryable.LastOrDefault) == methodName ||
nameof(Queryable.Single) == methodName ||
nameof(Queryable.SingleOrDefault) == methodName) &&
nameof(Queryable.SingleOrDefault) == methodName ||
EntityQueryMetadata.QUERY_ENUMERATOR == methodName) &&
propertyOrders.Length > 0)
return true;
return false;
@ -210,7 +210,7 @@ namespace ShardingCore.Sharding
if (!primaryOrderPropertyName.Contains("."))
{
if (virtualTable.EnableEntityQuery && virtualTable.EntityQueryMetadata.TryContainsComparerOrder(primaryOrderPropertyName, out var seqQueryOrderMatch)
&&(primaryOrder.OwnerType == singleShardingEntityType|| seqQueryOrderMatch.OrderMatch.HasFlag(SeqOrderMatchEnum.Named)))//要么必须是当前对象查询要么就是名称一样
&& (primaryOrder.OwnerType == singleShardingEntityType || seqQueryOrderMatch.OrderMatch.HasFlag(SeqOrderMatchEnum.Named)))//要么必须是当前对象查询要么就是名称一样
{
tailComparerIsAsc = seqQueryOrderMatch.IsSameAsShardingTailComparer ? primaryOrder.IsAsc : !primaryOrder.IsAsc;
//如果是获取最后一个还需要再次翻转