diff --git a/src/ShardingCore/Core/NotSupportShardingProviders/INotSupportShardingProvider.cs b/src/ShardingCore/Core/NotSupportShardingProviders/INotSupportShardingProvider.cs index 31ae3912..ce7ea51b 100644 --- a/src/ShardingCore/Core/NotSupportShardingProviders/INotSupportShardingProvider.cs +++ b/src/ShardingCore/Core/NotSupportShardingProviders/INotSupportShardingProvider.cs @@ -7,9 +7,12 @@ using ShardingCore.Sharding.ShardingExecutors.Abstractions; namespace ShardingCore.Core.NotSupportShardingProviders { + [Obsolete("plz use NotSupport() eg. dbcontext.Set().NotSupport().Where(...).ToList()")] public interface INotSupportShardingProvider { + [Obsolete("plz use NotSupport() eg. dbcontext.Set().NotSupport().Where(...).ToList()")] void CheckNotSupportSharding(IQueryCompilerContext queryCompilerContext); + [Obsolete("plz use NotSupport() eg. dbcontext.Set().NotSupport().Where(...).ToList()")] bool IsNotSupportSharding(IQueryCompilerContext queryCompilerContext); } } diff --git a/src/ShardingCore/Extensions/EntityFrameworkShardingQueryableExtension.cs b/src/ShardingCore/Extensions/EntityFrameworkShardingQueryableExtension.cs deleted file mode 100644 index f07ae715..00000000 --- a/src/ShardingCore/Extensions/EntityFrameworkShardingQueryableExtension.cs +++ /dev/null @@ -1,37 +0,0 @@ -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 NotSupport(this IQueryable source) - { - Check.NotNull(source, nameof(source)); - - return - source.Provider is EntityQueryProvider - ? source.Provider.CreateQuery( - Expression.Call( - (Expression)null, - NotSupportMethodInfo.MakeGenericMethod(typeof(TEntity)), - source.Expression)) - // source.Provider.CreateQuery( - // (Expression) Expression.Call((Expression) null, NotSupportMethodInfo.MakeGenericMethod(typeof (TEntity)), source.Expression)) - : source; - } - } -} \ No newline at end of file diff --git a/src/ShardingCore/Extensions/ShardingQueryableExtensions/EntityFrameworkShardingQueryableExtension.cs b/src/ShardingCore/Extensions/ShardingQueryableExtensions/EntityFrameworkShardingQueryableExtension.cs new file mode 100644 index 00000000..4c388718 --- /dev/null +++ b/src/ShardingCore/Extensions/ShardingQueryableExtensions/EntityFrameworkShardingQueryableExtension.cs @@ -0,0 +1,123 @@ +using System; +using System.Linq; +using System.Linq.Expressions; +using System.Reflection; +using Microsoft.EntityFrameworkCore.Query.Internal; +using ShardingCore.Core; + +namespace ShardingCore.Extensions.ShardingQueryableExtensions +{ +/* +* @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().GetDeclaredMethods(nameof(NotSupport)).Single(); + internal static readonly MethodInfo AsRouteMethodInfo + = typeof(EntityFrameworkShardingQueryableExtension).GetTypeInfo().GetDeclaredMethods(nameof(AsRoute)).Single(); + internal static readonly MethodInfo UseConfigMethodInfo + = typeof(EntityFrameworkShardingQueryableExtension).GetTypeInfo().GetDeclaredMethods(nameof(UseConfig)).Single(); + internal static readonly MethodInfo UseConnectionModeMethodInfo + = typeof(EntityFrameworkShardingQueryableExtension).GetTypeInfo().GetDeclaredMethods(nameof(UseConnectionMode)).Single(); + internal static readonly MethodInfo ReadWriteSeparationMethodInfo + = typeof(EntityFrameworkShardingQueryableExtension).GetTypeInfo().GetDeclaredMethods(nameof(ReadWriteSeparation)).Single(); + + /// + /// 标记当前操作是不支持分片的可以自行才用union all + /// + /// + /// + /// + public static IQueryable NotSupport(this IQueryable source) + { + Check.NotNull(source, nameof(source)); + + return + source.Provider is EntityQueryProvider + ? source.Provider.CreateQuery( + Expression.Call( + (Expression)null, + NotSupportMethodInfo.MakeGenericMethod(typeof(TEntity)), + source.Expression)) + : source; + } +/// +/// 开启提示路由的前提下手动指定表、手动指定数据源 +/// +/// +/// +/// +/// + public static IQueryable AsRoute(this IQueryable source,Action routeConfigure) + { + Check.NotNull(source, nameof(source)); + Check.NotNull(routeConfigure, nameof(routeConfigure)); + return source; + } + /// + /// 使用哪个配置多配置下有效 + /// + /// + /// + /// + /// + public static IQueryable UseConfig(this IQueryable source,string configId) + { + Check.NotNull(source, nameof(source)); + Check.NotNull(configId, nameof(configId)); + return source; + } + /// + /// 设置连接而模式 + /// + /// + /// + /// + /// + /// + /// + public static IQueryable UseConnectionMode(this IQueryable source,int maxQueryConnectionsLimit,ConnectionModeEnum connectionMode) + { + Check.NotNull(source, nameof(source)); + if (maxQueryConnectionsLimit <= 0) + throw new ArgumentException($"{nameof(UseConnectionMode)} {nameof(maxQueryConnectionsLimit)} should >=1"); + return source; + } + /// + /// 走读库 + /// + /// + /// + /// + public static IQueryable ReadOnly(this IQueryable source) + { + return source.ReadWriteSeparation(true); + } + /// + /// 走写库 + /// + /// + /// + /// + public static IQueryable WriteOnly(this IQueryable source) + { + return source.ReadWriteSeparation(false); + } + /// + /// 自定义读写分离走什么库 + /// + /// + /// + /// + /// + public static IQueryable ReadWriteSeparation(this IQueryable source,bool routeReadConnect) + { + Check.NotNull(source, nameof(source)); + return source; + } + } +} \ No newline at end of file diff --git a/src/ShardingCore/Extensions/ShardingQueryableExtensions/ShardingQueryableAsRouteOptions.cs b/src/ShardingCore/Extensions/ShardingQueryableExtensions/ShardingQueryableAsRouteOptions.cs new file mode 100644 index 00000000..278b1f40 --- /dev/null +++ b/src/ShardingCore/Extensions/ShardingQueryableExtensions/ShardingQueryableAsRouteOptions.cs @@ -0,0 +1,22 @@ +using System; +using ShardingCore.Core.QueryRouteManagers; + +namespace ShardingCore.Extensions.ShardingQueryableExtensions +{ + +/* +* @Author: xjm +* @Description: +* @Date: Monday, 31 January 2022 00:15:37 +* @Email: 326308290@qq.com +*/ + public class ShardingQueryableAsRouteOptions + { + private readonly Action _routeConfigure; + + public ShardingQueryableAsRouteOptions(Action routeConfigure) + { + _routeConfigure = routeConfigure; + } + } +} \ No newline at end of file diff --git a/src/ShardingCore/Sharding/MergeEngines/SingleAsyncInMemoryMergeEngine.cs b/src/ShardingCore/Sharding/MergeEngines/SingleAsyncInMemoryMergeEngine.cs index 1dcf521f..a6b7654f 100644 --- a/src/ShardingCore/Sharding/MergeEngines/SingleAsyncInMemoryMergeEngine.cs +++ b/src/ShardingCore/Sharding/MergeEngines/SingleAsyncInMemoryMergeEngine.cs @@ -29,6 +29,8 @@ namespace ShardingCore.Sharding.StreamMergeEngines var result = await base.ExecuteAsync( queryable => ((IQueryable)queryable).SingleOrDefaultAsync(cancellationToken), cancellationToken); var notNullResult = result.Where(o => o != null&&o.QueryResult!=null).Select(o=>o.QueryResult).ToList(); + if (notNullResult.Count==0) + throw new InvalidOperationException("Sequence on element."); if (notNullResult.Count!=1) throw new InvalidOperationException("Sequence contains more than one element."); diff --git a/src/ShardingCore/Sharding/MergeEngines/SingleOrDefaultAsyncInMemoryMergeEngine.cs b/src/ShardingCore/Sharding/MergeEngines/SingleOrDefaultAsyncInMemoryMergeEngine.cs index 9937757d..f6ee276d 100644 --- a/src/ShardingCore/Sharding/MergeEngines/SingleOrDefaultAsyncInMemoryMergeEngine.cs +++ b/src/ShardingCore/Sharding/MergeEngines/SingleOrDefaultAsyncInMemoryMergeEngine.cs @@ -30,10 +30,6 @@ namespace ShardingCore.Sharding.StreamMergeEngines var result = await base.ExecuteAsync( queryable => ((IQueryable)queryable).SingleOrDefaultAsync(cancellationToken), cancellationToken); var notNullResult = result.Where(o => o != null&&o.QueryResult!=null).Select(o=>o.QueryResult).ToList(); - - if (notNullResult.IsEmpty()) - return default; - if (notNullResult.Count > 1) throw new InvalidOperationException("Sequence contains more than one element."); return notNullResult.SingleOrDefault(); diff --git a/src/ShardingCore/Sharding/ShardingExecutors/Abstractions/IQueryCompilerContext.cs b/src/ShardingCore/Sharding/ShardingExecutors/Abstractions/IQueryCompilerContext.cs index cafa6bc8..c2c869e7 100644 --- a/src/ShardingCore/Sharding/ShardingExecutors/Abstractions/IQueryCompilerContext.cs +++ b/src/ShardingCore/Sharding/ShardingExecutors/Abstractions/IQueryCompilerContext.cs @@ -31,7 +31,7 @@ namespace ShardingCore.Sharding.ShardingExecutors.Abstractions /// /// bool IsQueryTrack(); - + [Obsolete("plz use NotSupport() eg. dbcontext.Set().NotSupport().Where(...).ToList()")] bool IsUnion(); bool IsNotSupport(); } diff --git a/src/ShardingCore/Sharding/Visitors/ShardingQueryableExtractParameter.cs b/src/ShardingCore/Sharding/Visitors/ShardingQueryableExtractParameter.cs index 2ceb0f49..c3c0a559 100644 --- a/src/ShardingCore/Sharding/Visitors/ShardingQueryableExtractParameter.cs +++ b/src/ShardingCore/Sharding/Visitors/ShardingQueryableExtractParameter.cs @@ -1,6 +1,6 @@ using System; using System.Linq.Expressions; -using ShardingCore.Extensions; +using ShardingCore.Extensions.ShardingQueryableExtensions; namespace ShardingCore.Sharding.Visitors {