From bde7aebc6b17f2f0b3412feec86c8de1fb7f80e0 Mon Sep 17 00:00:00 2001 From: xuejiaming <326308290@qq.com> Date: Thu, 30 Mar 2023 15:37:54 +0800 Subject: [PATCH] =?UTF-8?q?owner=E5=AF=B9=E8=B1=A1=E4=B8=8D=E6=B7=BB?= =?UTF-8?q?=E5=8A=A0=E5=88=B0entitymetadata?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Controllers/WeatherForecastController.cs | 16 ++++ .../Shardings/SysUserLogByMonthRoute.cs | 6 ++ samples/Sample.MySql/Startup.cs | 45 ++++----- .../RuntimeContexts/ShardingRuntimeContext.cs | 91 +++++++++++++------ .../Visitors/QueryableRouteDiscoverVisitor.cs | 7 ++ 5 files changed, 114 insertions(+), 51 deletions(-) diff --git a/samples/Sample.MySql/Controllers/WeatherForecastController.cs b/samples/Sample.MySql/Controllers/WeatherForecastController.cs index a0650fdc..a8560418 100644 --- a/samples/Sample.MySql/Controllers/WeatherForecastController.cs +++ b/samples/Sample.MySql/Controllers/WeatherForecastController.cs @@ -74,6 +74,12 @@ namespace Sample.MySql.Controllers public IQueryable GetAll() { + // DynamicShardingHelper.DynamicAppendDataSource(_shardingRuntimeContext,"ds9","链接字符串",true,true); + //如果你已经添加好了的情况下并且没有生成对应的库和表想要生成表和库 + var dataSourceInitializer = _shardingRuntimeContext.GetDataSourceInitializer(); + dataSourceInitializer.InitConfigure("ds9",true,true); + + var shardingTableCreator = _shardingRuntimeContext.GetShardingTableCreator(); var tableRouteManager = _shardingRuntimeContext.GetTableRouteManager(); //系统的时间分片都会实现 ITailAppendable 如果不是系统的自定义的转成你自己的对象即可 @@ -109,6 +115,16 @@ namespace Sample.MySql.Controllers // { // // } + var dateTime = new DateTime(2021,1,1); + var x211 = await (from ut in _defaultTableDbContext.Set() + join uu in _defaultTableDbContext.Set() + on ut.Id equals uu.Id + where uu.Time > dateTime + select new { a = ut, b = uu }).Select(o=>new {x=o}).Select(o=>new{x=o}) + .Select(o => new + { + o.x.x.a.Id + }).OrderBy(o => o.Id).ToListAsync(); var x2 = await (from ut in _defaultTableDbContext.Set() join uu in _defaultTableDbContext.Set() diff --git a/samples/Sample.MySql/Shardings/SysUserLogByMonthRoute.cs b/samples/Sample.MySql/Shardings/SysUserLogByMonthRoute.cs index b92381ea..1581bd4e 100644 --- a/samples/Sample.MySql/Shardings/SysUserLogByMonthRoute.cs +++ b/samples/Sample.MySql/Shardings/SysUserLogByMonthRoute.cs @@ -59,5 +59,11 @@ namespace Sample.MySql.Shardings Console.WriteLine("AfterShardingRouteUnitFilter:"+shardingRouteUnits.Count); return base.AfterShardingRouteUnitFilter(dataSourceRouteResult, shardingRouteUnits); } + + public override Func GetRouteFilter(object shardingKey, ShardingOperatorEnum shardingOperator, string shardingPropertyName) + { + Console.WriteLine(1); + return base.GetRouteFilter(shardingKey, shardingOperator, shardingPropertyName); + } } } diff --git a/samples/Sample.MySql/Startup.cs b/samples/Sample.MySql/Startup.cs index 594011e0..81813b6f 100644 --- a/samples/Sample.MySql/Startup.cs +++ b/samples/Sample.MySql/Startup.cs @@ -12,6 +12,7 @@ using ShardingCore.Bootstrappers; using ShardingCore.Core; using ShardingCore.Core.ModelCacheLockerProviders; using ShardingCore.Core.RuntimeContexts; +using ShardingCore.DynamicDataSources; using ShardingCore.EFCores; using ShardingCore.Extensions; using ShardingCore.Helpers; @@ -68,13 +69,13 @@ namespace Sample.MySql // optionsBuilder(sp, builder); // }); // - - + + services.AddShardingDbContext() - .UseRouteConfig((sp,o) => + .UseRouteConfig((sp, o) => { o.AddShardingTableRoute(); - o.AddShardingTableRoute(); + o.AddShardingTableRoute(); o.AddShardingTableRoute(); o.AddShardingDataSourceRoute(); o.AddShardingTableRoute(); @@ -85,7 +86,7 @@ namespace Sample.MySql new ParallelTableComparerType(typeof(TestMod)), new ParallelTableComparerType(typeof(TestModItem)), })); - }).UseConfig((sp,o) => + }).UseConfig((sp, o) => { // var memoryCache = sp.ApplicationServiceProvider.GetRequiredService(); // o.UseExecutorDbContextConfigure(b => @@ -97,17 +98,17 @@ namespace Sample.MySql o.CacheEntrySize = 1; o.CacheModelLockObjectSeconds = 10; o.CheckShardingKeyValueGenerated = false; - var loggerFactory1= sp.GetService(); + var loggerFactory1 = sp.GetService(); var loggerFactory2 = sp.ApplicationServiceProvider.GetService(); // o.UseEntityFrameworkCoreProxies = true; - o.ThrowIfQueryRouteNotMatch = false; + o.ThrowIfQueryRouteNotMatch = false; o.AutoUseWriteConnectionStringAfterWriteDb = true; - + o.UseShardingQuery((conStr, builder) => { builder.UseMySql(conStr, new MySqlServerVersion(new Version())) .UseLoggerFactory(loggerFactory1) - .EnableSensitiveDataLogging(); + .EnableSensitiveDataLogging(); //.UseQueryTrackingBehavior(QueryTrackingBehavior.NoTracking); }); o.UseShardingTransaction((connection, builder) => @@ -116,7 +117,7 @@ namespace Sample.MySql .UseMySql(connection, new MySqlServerVersion(new Version())) .UseLoggerFactory(loggerFactory1) .EnableSensitiveDataLogging(); - //.UseQueryTrackingBehavior(QueryTrackingBehavior.NoTracking); + //.UseQueryTrackingBehavior(QueryTrackingBehavior.NoTracking); }); o.AddDefaultDataSource("ds0", "server=127.0.0.1;port=3306;database=dbdbd0;userid=root;password=root;"); @@ -129,7 +130,8 @@ namespace Sample.MySql { b.ReplaceService(); }); - }).ReplaceService() + }).ReplaceService() + .ReplaceService() .AddShardingCore(); // services.AddDbContext(ShardingCoreExtension // .UseMutliDefaultSharding); @@ -178,19 +180,20 @@ namespace Sample.MySql app.UseDeveloperExceptionPage(); } // app.ApplicationServices.UseAutoTryCompensateTable(); - + // var shardingRuntimeContext = app.ApplicationServices.GetRequiredService(); // var entityMetadataManager = shardingRuntimeContext.GetEntityMetadataManager(); // var entityMetadata = entityMetadataManager.TryGet(); - using (var scope = app.ApplicationServices.CreateScope()) - { - var defaultShardingDbContext = scope.ServiceProvider.GetService(); - // if (defaultShardingDbContext.Database.GetPendingMigrations().Any()) - { - defaultShardingDbContext.Database.GenerateCreateScript(); - } - } - app.ApplicationServices.UseAutoTryCompensateTable(); + // using (var scope = app.ApplicationServices.CreateScope()) + // { + // var defaultShardingDbContext = scope.ServiceProvider.GetService(); + // // if (defaultShardingDbContext.Database.GetPendingMigrations().Any()) + // { + // defaultShardingDbContext.Database.GenerateCreateScript(); + // } + // } + + // app.ApplicationServices.UseAutoTryCompensateTable(); // using (var scope = app.ApplicationServices.CreateScope()) // { // var defaultShardingDbContext = scope.ServiceProvider.GetService(); diff --git a/src/ShardingCore/Core/RuntimeContexts/ShardingRuntimeContext.cs b/src/ShardingCore/Core/RuntimeContexts/ShardingRuntimeContext.cs index 6406b876..4d1b0e64 100644 --- a/src/ShardingCore/Core/RuntimeContexts/ShardingRuntimeContext.cs +++ b/src/ShardingCore/Core/RuntimeContexts/ShardingRuntimeContext.cs @@ -22,7 +22,6 @@ using ShardingCore.Core.VirtualRoutes.Abstractions; using ShardingCore.Core.VirtualRoutes.TableRoutes.RouteTails.Abstractions; using ShardingCore.DynamicDataSources; using ShardingCore.Exceptions; - using ShardingCore.Sharding.Abstractions; using ShardingCore.Sharding.MergeEngines.ParallelControl; using ShardingCore.Sharding.ParallelTables; @@ -32,7 +31,8 @@ using ShardingCore.TableCreator; namespace ShardingCore.Core.RuntimeContexts { - public sealed class ShardingRuntimeContext : IShardingRuntimeContext where TDbContext:IShardingDbContext + public sealed class ShardingRuntimeContext : IShardingRuntimeContext + where TDbContext : IShardingDbContext { private bool isInited = false; private object INIT_LOCK = new object(); @@ -45,7 +45,7 @@ namespace ShardingCore.Core.RuntimeContexts private IServiceProvider _serviceProvider; public Type DbContextType => typeof(TDbContext); - + public void AddServiceConfig(Action configure) { @@ -74,16 +74,19 @@ namespace ShardingCore.Core.RuntimeContexts { GetRequiredService().AutoShardingCreate(); } + private IDbContextTypeAware _dbContextTypeAware; + public IDbContextTypeAware GetDbContextTypeAware() { - return _dbContextTypeAware??=GetRequiredService(); + return _dbContextTypeAware ??= GetRequiredService(); } private IModelCacheLockerProvider _modelCacheLockerProvider; + public IModelCacheLockerProvider GetModelCacheLockerProvider() { - return _modelCacheLockerProvider??=GetRequiredService(); + return _modelCacheLockerProvider ??= GetRequiredService(); } @@ -92,16 +95,18 @@ namespace ShardingCore.Core.RuntimeContexts public IShardingProvider GetShardingProvider() { - return _shardingProvider??=GetRequiredService(); + return _shardingProvider ??= GetRequiredService(); } private IDbContextOptionBuilderCreator _dbContextOptionBuilderCreator; + public IDbContextOptionBuilderCreator GetDbContextOptionBuilderCreator() { - return _dbContextOptionBuilderCreator??=GetRequiredService(); + return _dbContextOptionBuilderCreator ??= GetRequiredService(); } private ShardingConfigOptions _shardingConfigOptions; + public ShardingConfigOptions GetShardingConfigOptions() { return _shardingConfigOptions ??= GetRequiredService(); @@ -109,119 +114,138 @@ namespace ShardingCore.Core.RuntimeContexts private IShardingRouteConfigOptions _shardingRouteConfigOptions; + public IShardingRouteConfigOptions GetShardingRouteConfigOptions() { - return _shardingRouteConfigOptions??= GetRequiredService(); + return _shardingRouteConfigOptions ??= GetRequiredService(); } private IShardingMigrationManager _shardingMigrationManager; + public IShardingMigrationManager GetShardingMigrationManager() { - return _shardingMigrationManager??= GetRequiredService(); + return _shardingMigrationManager ??= GetRequiredService(); } private IShardingComparer _shardingComparer; + public IShardingComparer GetShardingComparer() { - return _shardingComparer??=GetRequiredService(); + return _shardingComparer ??= GetRequiredService(); } private IShardingCompilerExecutor _shardingCompilerExecutor; + public IShardingCompilerExecutor GetShardingCompilerExecutor() { - return _shardingCompilerExecutor??=GetRequiredService(); + return _shardingCompilerExecutor ??= GetRequiredService(); } private IShardingReadWriteManager _shardingReadWriteManager; + public IShardingReadWriteManager GetShardingReadWriteManager() { - return _shardingReadWriteManager??=GetRequiredService(); + return _shardingReadWriteManager ??= GetRequiredService(); } private ITrackerManager _trackerManager; + public ITrackerManager GetTrackerManager() { - return _trackerManager??=GetRequiredService(); + return _trackerManager ??= GetRequiredService(); } private IParallelTableManager _parallelTableManager; + public IParallelTableManager GetParallelTableManager() { - return _parallelTableManager??=GetRequiredService(); + return _parallelTableManager ??= GetRequiredService(); } private IDbContextCreator _dbContextCreator; + public IDbContextCreator GetDbContextCreator() { - return _dbContextCreator??=GetRequiredService(); + return _dbContextCreator ??= GetRequiredService(); } private IEntityMetadataManager _entityMetadataManager; + public IEntityMetadataManager GetEntityMetadataManager() { - return _entityMetadataManager??=GetRequiredService(); + return _entityMetadataManager ??= GetRequiredService(); } private IVirtualDataSource _virtualDataSource; + public IVirtualDataSource GetVirtualDataSource() { - return _virtualDataSource??=GetRequiredService(); + return _virtualDataSource ??= GetRequiredService(); } private IDataSourceRouteManager _dataSourceRouteManager; + public IDataSourceRouteManager GetDataSourceRouteManager() { - return _dataSourceRouteManager??=GetRequiredService(); + return _dataSourceRouteManager ??= GetRequiredService(); } private ITableRouteManager _tableRouteManager; + public ITableRouteManager GetTableRouteManager() { - return _tableRouteManager??=GetRequiredService(); + return _tableRouteManager ??= GetRequiredService(); } private IReadWriteConnectorFactory _readWriteConnectorFactory; + public IReadWriteConnectorFactory GetReadWriteConnectorFactory() { - return _readWriteConnectorFactory??=GetRequiredService(); + return _readWriteConnectorFactory ??= GetRequiredService(); } private IShardingTableCreator _shardingTableCreator; + public IShardingTableCreator GetShardingTableCreator() { - return _shardingTableCreator??=GetRequiredService(); + return _shardingTableCreator ??= GetRequiredService(); } private IRouteTailFactory _routeTailFactory; + public IRouteTailFactory GetRouteTailFactory() { - return _routeTailFactory??=GetRequiredService(); + return _routeTailFactory ??= GetRequiredService(); } private IQueryTracker _queryTracker; + public IQueryTracker GetQueryTracker() { - return _queryTracker??=GetRequiredService(); + return _queryTracker ??= GetRequiredService(); } private IUnionAllMergeManager _unionAllMergeManager; + public IUnionAllMergeManager GetUnionAllMergeManager() { - return _unionAllMergeManager??=GetRequiredService(); + return _unionAllMergeManager ??= GetRequiredService(); } private IShardingPageManager _shardingPageManager; + public IShardingPageManager GetShardingPageManager() { - return _shardingPageManager??=GetRequiredService(); + return _shardingPageManager ??= GetRequiredService(); } private IDataSourceInitializer _dataSourceInitializer; + public IDataSourceInitializer GetDataSourceInitializer() { - return _dataSourceInitializer??=GetRequiredService(); + return _dataSourceInitializer ??= GetRequiredService(); } @@ -238,12 +262,17 @@ namespace ShardingCore.Core.RuntimeContexts foreach (var entityType in entityTypes) { trackerManager.AddDbContextModel(entityType.ClrType, entityType.FindPrimaryKey() != null); - if (!entityMetadataManager.IsSharding(entityType.ClrType)) + var isOwned = entityType.IsOwned(); + if (!isOwned) { - var entityMetadata = new EntityMetadata(entityType.ClrType); - entityMetadataManager.AddEntityMetadata(entityMetadata); + if (!entityMetadataManager.IsSharding(entityType.ClrType)) + { + var entityMetadata = new EntityMetadata(entityType.ClrType); + entityMetadataManager.AddEntityMetadata(entityMetadata); + } + + entityMetadataManager.TryInitModel(entityType); } - entityMetadataManager.TryInitModel(entityType); } } } @@ -254,6 +283,7 @@ namespace ShardingCore.Core.RuntimeContexts if (isInited) throw new InvalidOperationException("sharding runtime already build"); } + private void CheckIfNotBuild() { if (!isInited) @@ -283,6 +313,7 @@ namespace ShardingCore.Core.RuntimeContexts CheckIfNotBuild(); return _serviceProvider.GetRequiredService(); } + public IShardingRouteManager GetShardingRouteManager() { return GetRequiredService(); diff --git a/src/ShardingCore/Sharding/Visitors/QueryableRouteDiscoverVisitor.cs b/src/ShardingCore/Sharding/Visitors/QueryableRouteDiscoverVisitor.cs index aad44a34..439a3895 100644 --- a/src/ShardingCore/Sharding/Visitors/QueryableRouteDiscoverVisitor.cs +++ b/src/ShardingCore/Sharding/Visitors/QueryableRouteDiscoverVisitor.cs @@ -246,6 +246,12 @@ namespace ShardingCore.Core.Internal.Visitors return expression is MethodCallExpression; } + public override Expression Visit(Expression node) + { + Console.WriteLine(1); + return base.Visit(node); + } + protected override Expression VisitMethodCall(MethodCallExpression node) { switch (node.Method.Name) @@ -280,6 +286,7 @@ namespace ShardingCore.Core.Internal.Visitors _entityLambdaExpression = lambda; } } + //todo } } }