添加notsupport的支持[#104]

This commit is contained in:
xuejmnet 2022-01-30 16:52:31 +08:00
parent 582d692dd1
commit ed64badf90
11 changed files with 125 additions and 11 deletions

View File

@ -148,6 +148,7 @@ namespace Sample.SqlServer.Controllers
}
[HttpGet]
public async Task<IActionResult> Get1([FromQuery] int p, [FromQuery] int s)
{
Stopwatch sp = new Stopwatch();
sp.Start();
@ -160,6 +161,19 @@ namespace Sample.SqlServer.Controllers
});
}
[HttpGet]
public async Task<IActionResult> Get1a([FromQuery] int p, [FromQuery] int s)
{
Stopwatch sp = new Stopwatch();
sp.Start();
var shardingPageResultAsync = await _defaultTableDbContext.Set<SysUserMod>().NotSupport().OrderBy(o => o.Age).ToShardingPageAsync(p, s);
sp.Stop();
return Ok(new
{
sp.ElapsedMilliseconds,
shardingPageResultAsync
});
}
[HttpGet]
public IActionResult Get2([FromQuery] int p, [FromQuery] int s)
{
Stopwatch sp = new Stopwatch();

View File

@ -63,7 +63,9 @@ namespace Sample.SqlServer
});
op.ReplaceTableEnsureManager(sp => new SqlServerTableEnsureManager<DefaultShardingDbContext>());
op.AddDefaultDataSource("A",
"Data Source=localhost;Initial Catalog=ShardingCoreDBXA;Integrated Security=True;");
// "Data Source=localhost;Initial Catalog=ShardingCoreDBXA;Integrated Security=True;"
"Data Source = 101.37.117.55;persist security info=True;Initial Catalog=ShardingCoreDBXA;uid=sa;pwd=xjmumixl7610#;Max Pool Size=100;"
);
}).EnsureConfig();
services.TryAddSingleton<INotSupportShardingProvider, UnionSupportShardingProvider>();
//services.AddShardingDbContext<DefaultShardingDbContext1>(

View File

@ -0,0 +1,37 @@
using System;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using Microsoft.EntityFrameworkCore.Query.Internal;
using ShardingCore.Core;
namespace ShardingCore.Extensions
{
/*
* @Author: xjm
* @Description:
* @Date: Sunday, 30 January 2022 00:12:37
* @Email: 326308290@qq.com
*/
public static class EntityFrameworkShardingQueryableExtension
{
internal static readonly MethodInfo NotSupportMethodInfo
= typeof(EntityFrameworkShardingQueryableExtension).GetTypeInfo().GetDeclaredMethod(nameof(NotSupport)) ?? throw new InvalidOperationException($"not found method {nameof(NotSupport)}");
public static IQueryable<TEntity> NotSupport<TEntity>(this IQueryable<TEntity> source)
{
Check.NotNull(source, nameof(source));
return
source.Provider is EntityQueryProvider
? source.Provider.CreateQuery<TEntity>(
Expression.Call(
(Expression)null,
NotSupportMethodInfo.MakeGenericMethod(typeof(TEntity)),
source.Expression))
// source.Provider.CreateQuery<TEntity>(
// (Expression) Expression.Call((Expression) null, NotSupportMethodInfo.MakeGenericMethod(typeof (TEntity)), source.Expression))
: source;
}
}
}

View File

@ -25,7 +25,7 @@ namespace ShardingCore.Sharding.MergeEngines.Abstractions
protected abstract StreamMergeContext<TEntity> GetStreamMergeContext();
protected bool IsUnSupport()
{
return GetStreamMergeContext().IsUnSupportSharding();
return GetStreamMergeContext().IsNotSupportSharding();
}
/// <summary>
/// 将查询分表分库结果按每个数据源进行分组

View File

@ -64,6 +64,11 @@ namespace ShardingCore.Sharding.MergeEngines.EnumeratorStreamMergeEngines
return new SingleQueryEnumeratorAsyncStreamMergeEngine<TShardingDbContext, TEntity>(_streamMergeContext);
}
if (_streamMergeContext.IsNotSupportSharding())
{
return new DefaultShardingEnumeratorAsyncStreamMergeEngine<TShardingDbContext, TEntity>(_streamMergeContext);
}
//未开启系统分表或者本次查询涉及多张分表
if (_streamMergeContext.IsPaginationQuery() && _streamMergeContext.IsSingleShardingEntityQuery() && _shardingPageManager.Current != null)
{

View File

@ -33,5 +33,6 @@ namespace ShardingCore.Sharding.ShardingExecutors.Abstractions
bool IsQueryTrack();
bool IsUnion();
bool IsNotSupport();
}
}

View File

@ -109,6 +109,11 @@ namespace ShardingCore.Sharding.ShardingExecutors
return _queryCompilerContext.IsUnion();
}
public bool IsNotSupport()
{
return _queryCompilerContext.IsNotSupport();
}
public QueryCompilerExecutor GetQueryCompilerExecutor()
{
if (!hasQueryCompilerExecutor.HasValue)

View File

@ -10,6 +10,7 @@ using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using ShardingCore.Sharding.Visitors;
namespace ShardingCore.Sharding.ShardingExecutors
{
@ -25,16 +26,20 @@ namespace ShardingCore.Sharding.ShardingExecutors
private readonly bool? _isNoTracking;
private readonly bool _isUnion;
private readonly bool _isParallelQuery;
private readonly bool _isNotSupport;
private QueryCompilerContext(IShardingDbContext shardingDbContext, Expression queryExpression)
{
var shardingQueryableExtractParameter = new ShardingQueryableExtractParameter();
var expression = shardingQueryableExtractParameter.Visit(queryExpression);
_shardingDbContextType = shardingDbContext.GetType();
var compileParseResult = ShardingUtil.GetQueryCompileParseResultByExpression(queryExpression, _shardingDbContextType);
var compileParseResult = ShardingUtil.GetQueryCompileParseResultByExpression(expression, _shardingDbContextType);
_queryEntities = compileParseResult.QueryEntities;
_isNoTracking = compileParseResult.IsNoTracking;
_isUnion = compileParseResult.IsUnion;
_shardingDbContext = shardingDbContext;
_queryExpression = queryExpression;
_queryExpression = expression;
_isNotSupport = shardingQueryableExtractParameter.IsNotSupportQuery();
_entityMetadataManager = ShardingContainer.GetRequiredEntityMetadataManager(_shardingDbContextType);
//原生对象的原生查询如果是读写分离就需要启用并行查询
@ -97,6 +102,11 @@ namespace ShardingCore.Sharding.ShardingExecutors
return _isUnion;
}
public bool IsNotSupport()
{
return _isNotSupport;
}
public QueryCompilerExecutor GetQueryCompilerExecutor()
{

View File

@ -12,6 +12,7 @@ using ShardingCore.Extensions;
using ShardingCore.Sharding.Abstractions;
using ShardingCore.Sharding.ShardingExecutors.Abstractions;
using ShardingCore.Sharding.ShardingExecutors.QueryableCombines;
using ShardingCore.Sharding.Visitors;
namespace ShardingCore.Sharding.ShardingExecutors
{

View File

@ -121,7 +121,7 @@ namespace ShardingCore.Sharding
_maxParallelExecuteCount = _shardingDbContext.GetVirtualDataSource().ConfigurationParams.MaxQueryConnectionsLimit;
if (IsSingleShardingEntityQuery() && !Skip.HasValue && IsCrossTable && !IsUnSupportSharding())
if (IsSingleShardingEntityQuery() && !Skip.HasValue && IsCrossTable && !IsNotSupportSharding())
{
var singleShardingEntityType = GetSingleShardingEntityType();
var virtualTableManager = (IVirtualTableManager)ShardingContainer.GetService(typeof(IVirtualTableManager<>).GetGenericType0(MergeQueryCompilerContext.GetShardingDbContextType()));
@ -436,18 +436,20 @@ namespace ShardingCore.Sharding
return _shardingEntityConfigOptions.ThrowIfQueryRouteNotMatch;
}
private bool? _isUnSupport;
public bool IsUnSupportSharding()
private bool? _isNotSupport;
public bool IsNotSupportSharding()
{
if (!_isUnSupport.HasValue)
if (MergeQueryCompilerContext.IsNotSupport())
return true;
if (!_isNotSupport.HasValue)
{
_isUnSupport = _notSupportShardingProvider.IsNotSupportSharding(MergeQueryCompilerContext);
if (_isUnSupport.Value)
_isNotSupport = _notSupportShardingProvider.IsNotSupportSharding(MergeQueryCompilerContext);
if (_isNotSupport.Value)
{
_notSupportShardingProvider.CheckNotSupportSharding(MergeQueryCompilerContext);
}
}
return _isUnSupport.Value;
return _isNotSupport.Value;
}
public void Dispose()
{

View File

@ -0,0 +1,37 @@
using System;
using System.Linq.Expressions;
using ShardingCore.Extensions;
namespace ShardingCore.Sharding.Visitors
{
/*
* @Author: xjm
* @Description:
* @Date: Sunday, 30 January 2022 00:48:30
* @Email: 326308290@qq.com
*/
public class ShardingQueryableExtractParameter:ExpressionVisitor
{
private bool isNotSupport;
public bool IsNotSupportQuery()
{
return isNotSupport;
}
protected override Expression VisitMethodCall(MethodCallExpression node)
{
if (node.Method.IsGenericMethod)
{
var genericMethodDefinition = node.Method.GetGenericMethodDefinition();
// find cachable query extention calls
if (genericMethodDefinition == EntityFrameworkShardingQueryableExtension.NotSupportMethodInfo)
{
isNotSupport = true;
// cut out extension expression
return Visit(node.Arguments[0]);
}
}
return base.VisitMethodCall(node);
}
}
}