添加启动表确认管理者,可以有效的判断当前数据库是否存在对应的表信息
This commit is contained in:
parent
09bb6c2719
commit
772004465f
|
@ -10,7 +10,9 @@ using ShardingCore;
|
|||
using ShardingCore.Sharding.ReadWriteConfigurations;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using ShardingCore.Core;
|
||||
using ShardingCore.TableExists;
|
||||
|
||||
namespace Sample.SqlServer
|
||||
{
|
||||
|
@ -50,7 +52,9 @@ namespace Sample.SqlServer
|
|||
o.AddShardingTableRoute<SysUserModVirtualTableRoute>();
|
||||
o.AddShardingTableRoute<SysUserSalaryVirtualTableRoute>();
|
||||
o.AddShardingTableRoute<TestYearShardingVirtualTableRoute>();
|
||||
}).End();
|
||||
})
|
||||
.AddTableEnsureManager(sp => new SqlServerTableEnsureManager<DefaultShardingDbContext>())
|
||||
.End();
|
||||
//services.AddShardingDbContext<DefaultShardingDbContext1>(
|
||||
// (conn, o) =>
|
||||
// o.UseSqlServer(conn).UseLoggerFactory(efLogger)
|
||||
|
@ -104,8 +108,11 @@ namespace Sample.SqlServer
|
|||
app.UseDeveloperExceptionPage();
|
||||
}
|
||||
|
||||
var startNew = Stopwatch.StartNew();
|
||||
startNew.Start();
|
||||
app.UseShardingCore();
|
||||
|
||||
startNew.Stop();
|
||||
Console.WriteLine($"UseShardingCore:"+startNew.ElapsedMilliseconds+"ms");
|
||||
app.UseRouting();
|
||||
|
||||
app.UseEndpoints(endpoints => { endpoints.MapControllers(); });
|
||||
|
|
|
@ -1,53 +0,0 @@
|
|||
//using System;
|
||||
//using System.Collections.Generic;
|
||||
//using System.Text;
|
||||
|
||||
//namespace ShardingCore.Core
|
||||
//{
|
||||
// /*
|
||||
// * @Author: xjm
|
||||
// * @Description:
|
||||
// * @Date: 2021/3/2 15:06:18
|
||||
// * @Ver: 1.0
|
||||
// * @Email: 326308290@qq.com
|
||||
// */
|
||||
// public class ShardingDataSourceConfig
|
||||
// {
|
||||
// public ShardingDataSourceConfig(string connectKey, string connectionString)
|
||||
// {
|
||||
// ConnectKey = connectKey;
|
||||
// ConnectionString = connectionString;
|
||||
// Entities = new HashSet<Type>();
|
||||
// }
|
||||
|
||||
// /// <summary>
|
||||
// /// 连接标识
|
||||
// /// </summary>
|
||||
// public string ConnectKey { get; }
|
||||
|
||||
// /// <summary>
|
||||
// /// 连接字符串
|
||||
// /// </summary>
|
||||
// public string ConnectionString { get; }
|
||||
|
||||
// public ISet<Type> Entities { get; }
|
||||
|
||||
// public override int GetHashCode()
|
||||
// {
|
||||
// return this.ConnectKey.GetHashCode() ^ 31;
|
||||
// }
|
||||
|
||||
// public override bool Equals(object obj)
|
||||
// {
|
||||
// if (!(obj is ShardingDataSourceConfig))
|
||||
// return false;
|
||||
|
||||
// if (ReferenceEquals(this, obj))
|
||||
// return true;
|
||||
|
||||
// ShardingDataSourceConfig item = (ShardingDataSourceConfig)obj;
|
||||
|
||||
// return item.ConnectKey == this.ConnectKey;
|
||||
// }
|
||||
// }
|
||||
//}
|
|
@ -33,6 +33,7 @@ using ShardingCore.DynamicDataSources;
|
|||
using ShardingCore.Sharding.ParallelTables;
|
||||
using ShardingCore.Sharding.ReadWriteConfigurations;
|
||||
using ShardingCore.Sharding.ReadWriteConfigurations.Abstractions;
|
||||
using ShardingCore.TableExists;
|
||||
|
||||
namespace ShardingCore
|
||||
{
|
||||
|
|
|
@ -12,6 +12,7 @@ using ShardingCore.Sharding.ReadWriteConfigurations;
|
|||
using ShardingCore.Sharding.ReadWriteConfigurations.Abstractions;
|
||||
using ShardingCore.Sharding.ShardingComparision;
|
||||
using ShardingCore.Sharding.ShardingComparision.Abstractions;
|
||||
using ShardingCore.TableExists;
|
||||
|
||||
namespace ShardingCore.DIExtensions
|
||||
{
|
||||
|
@ -45,6 +46,11 @@ namespace ShardingCore.DIExtensions
|
|||
_shardingCoreConfigBuilder.ShardingConfigOption.ReplaceShardingComparer(newShardingComparerFactory);
|
||||
return this;
|
||||
}
|
||||
public ShardingCoreConfigEndBuilder<TShardingDbContext> AddTableEnsureManager(Func<IServiceProvider, ITableEnsureManager<TShardingDbContext>> newTableEnsureManagerFactory)
|
||||
{
|
||||
_shardingCoreConfigBuilder.ShardingConfigOption.AddTableEnsureManager(newTableEnsureManagerFactory);
|
||||
return this;
|
||||
}
|
||||
/// <summary>
|
||||
/// 是否启用读写分离
|
||||
/// </summary>
|
||||
|
@ -69,6 +75,11 @@ namespace ShardingCore.DIExtensions
|
|||
throw new ShardingCoreConfigException($"{nameof(_shardingCoreConfigBuilder.ShardingConfigOption.ReplaceShardingComparerFactory)} is null");
|
||||
}
|
||||
services.AddSingleton<IShardingComparer<TShardingDbContext>>(_shardingCoreConfigBuilder.ShardingConfigOption.ReplaceShardingComparerFactory);
|
||||
if (_shardingCoreConfigBuilder.ShardingConfigOption.TableEnsureManagerFactory == null)
|
||||
{
|
||||
throw new ShardingCoreConfigException($"{nameof(_shardingCoreConfigBuilder.ShardingConfigOption.TableEnsureManagerFactory)} is null");
|
||||
}
|
||||
services.AddSingleton<ITableEnsureManager<TShardingDbContext>>(_shardingCoreConfigBuilder.ShardingConfigOption.TableEnsureManagerFactory);
|
||||
|
||||
|
||||
if (!UseReadWrite)
|
||||
|
|
|
@ -4,6 +4,7 @@ using System.Text;
|
|||
using Microsoft.EntityFrameworkCore;
|
||||
using ShardingCore.Sharding.Abstractions;
|
||||
using ShardingCore.Sharding.ShardingComparision.Abstractions;
|
||||
using ShardingCore.TableExists;
|
||||
|
||||
namespace ShardingCore.DIExtensions
|
||||
{
|
||||
|
@ -46,5 +47,10 @@ namespace ShardingCore.DIExtensions
|
|||
_shardingCoreConfigBuilder.ShardingConfigOption.ReplaceShardingComparer(newShardingComparerFactory);
|
||||
return this;
|
||||
}
|
||||
public ShardingDataBaseOrTableBuilder<TShardingDbContext> AddTableEnsureManager(Func<IServiceProvider, ITableEnsureManager<TShardingDbContext>> newTableEnsureManagerFactory)
|
||||
{
|
||||
_shardingCoreConfigBuilder.ShardingConfigOption.AddTableEnsureManager(newTableEnsureManagerFactory);
|
||||
return this;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -4,6 +4,7 @@ using System.Text;
|
|||
using Microsoft.EntityFrameworkCore;
|
||||
using ShardingCore.Sharding.Abstractions;
|
||||
using ShardingCore.Sharding.ShardingComparision.Abstractions;
|
||||
using ShardingCore.TableExists;
|
||||
|
||||
namespace ShardingCore.DIExtensions
|
||||
{
|
||||
|
@ -42,5 +43,10 @@ namespace ShardingCore.DIExtensions
|
|||
_shardingCoreConfigBuilder.ShardingConfigOption.ReplaceShardingComparer(newShardingComparerFactory);
|
||||
return this;
|
||||
}
|
||||
public ShardingDataSourceRouteBuilder<TShardingDbContext> AddTableEnsureManager(Func<IServiceProvider, ITableEnsureManager<TShardingDbContext>> newTableEnsureManagerFactory)
|
||||
{
|
||||
_shardingCoreConfigBuilder.ShardingConfigOption.AddTableEnsureManager(newTableEnsureManagerFactory);
|
||||
return this;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -5,6 +5,7 @@ using Microsoft.EntityFrameworkCore;
|
|||
using ShardingCore.Sharding.Abstractions;
|
||||
using ShardingCore.Sharding.ReadWriteConfigurations;
|
||||
using ShardingCore.Sharding.ShardingComparision.Abstractions;
|
||||
using ShardingCore.TableExists;
|
||||
|
||||
namespace ShardingCore.DIExtensions
|
||||
{
|
||||
|
@ -40,5 +41,10 @@ namespace ShardingCore.DIExtensions
|
|||
_shardingCoreConfigBuilder.ShardingConfigOption.ReplaceShardingComparer(newShardingComparerFactory);
|
||||
return this;
|
||||
}
|
||||
public ShardingReadWriteSeparationBuilder<TShardingDbContext> AddTableEnsureManager(Func<IServiceProvider, ITableEnsureManager<TShardingDbContext>> newTableEnsureManagerFactory)
|
||||
{
|
||||
_shardingCoreConfigBuilder.ShardingConfigOption.AddTableEnsureManager(newTableEnsureManagerFactory);
|
||||
return this;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -4,6 +4,7 @@ using System.Text;
|
|||
using Microsoft.EntityFrameworkCore;
|
||||
using ShardingCore.Sharding.Abstractions;
|
||||
using ShardingCore.Sharding.ShardingComparision.Abstractions;
|
||||
using ShardingCore.TableExists;
|
||||
|
||||
namespace ShardingCore.DIExtensions
|
||||
{
|
||||
|
@ -41,5 +42,10 @@ namespace ShardingCore.DIExtensions
|
|||
_shardingCoreConfigBuilder.ShardingConfigOption.ReplaceShardingComparer(newShardingComparerFactory);
|
||||
return this;
|
||||
}
|
||||
public ShardingTableBuilder<TShardingDbContext> AddTableEnsureManager(Func<IServiceProvider, ITableEnsureManager<TShardingDbContext>> newTableEnsureManagerFactory)
|
||||
{
|
||||
_shardingCoreConfigBuilder.ShardingConfigOption.AddTableEnsureManager(newTableEnsureManagerFactory);
|
||||
return this;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -20,6 +20,7 @@ using ShardingCore.Extensions;
|
|||
using ShardingCore.Sharding.Abstractions;
|
||||
using ShardingCore.Sharding.ParallelTables;
|
||||
using ShardingCore.TableCreator;
|
||||
using ShardingCore.TableExists;
|
||||
|
||||
namespace ShardingCore.DynamicDataSources
|
||||
{
|
||||
|
@ -27,8 +28,8 @@ namespace ShardingCore.DynamicDataSources
|
|||
{
|
||||
void InitConfigure(string dataSourceName, string connectionString);
|
||||
}
|
||||
|
||||
public class DefaultDataSourceInitializer<TShardingDbContext>: IDefaultDataSourceInitializer<TShardingDbContext> where TShardingDbContext:DbContext,IShardingDbContext
|
||||
|
||||
public class DefaultDataSourceInitializer<TShardingDbContext> : IDefaultDataSourceInitializer<TShardingDbContext> where TShardingDbContext : DbContext, IShardingDbContext
|
||||
{
|
||||
private readonly IRouteTailFactory _routeTailFactory;
|
||||
private readonly IVirtualTableManager<TShardingDbContext> _virtualTableManager;
|
||||
|
@ -45,7 +46,7 @@ namespace ShardingCore.DynamicDataSources
|
|||
ILogger<DefaultDataSourceInitializer<TShardingDbContext>> logger)
|
||||
{
|
||||
_shardingConfigOption =
|
||||
shardingConfigOptions.FirstOrDefault(o => o.ShardingDbContextType == typeof(TShardingDbContext))??throw new ArgumentNullException($"{nameof(IShardingConfigOption)} cant been registered {typeof(TShardingDbContext)}");
|
||||
shardingConfigOptions.FirstOrDefault(o => o.ShardingDbContextType == typeof(TShardingDbContext)) ?? throw new ArgumentNullException($"{nameof(IShardingConfigOption)} cant been registered {typeof(TShardingDbContext)}");
|
||||
_routeTailFactory = routeTailFactory;
|
||||
_virtualTableManager = virtualTableManager;
|
||||
_entityMetadataManager = entityMetadataManager;
|
||||
|
@ -53,15 +54,17 @@ namespace ShardingCore.DynamicDataSources
|
|||
_virtualDataSource = virtualDataSource;
|
||||
_logger = logger;
|
||||
}
|
||||
public void InitConfigure(string dataSourceName,string connectionString)
|
||||
public void InitConfigure(string dataSourceName, string connectionString)
|
||||
{
|
||||
using (var serviceScope = ShardingContainer.ServiceProvider.CreateScope())
|
||||
{
|
||||
_virtualDataSource.AddPhysicDataSource(new DefaultPhysicDataSource(dataSourceName, connectionString, false));
|
||||
using var context =
|
||||
(DbContext)serviceScope.ServiceProvider.GetService(_shardingConfigOption.ShardingDbContextType);
|
||||
using var context = serviceScope.ServiceProvider.GetService<TShardingDbContext>();
|
||||
if (_shardingConfigOption.EnsureCreatedWithOutShardingTable)
|
||||
EnsureCreated(context, dataSourceName);
|
||||
var tableEnsureManager = ShardingContainer.GetService<ITableEnsureManager<TShardingDbContext>>();
|
||||
//获取数据库存在的所有的表
|
||||
var existTables = tableEnsureManager?.GetExistTables(context, dataSourceName)??new HashSet<string>();
|
||||
foreach (var entity in context.Model.GetEntityTypes())
|
||||
{
|
||||
var entityType = entity.ClrType;
|
||||
|
@ -72,7 +75,7 @@ namespace ShardingCore.DynamicDataSources
|
|||
{
|
||||
var virtualTable = _virtualTableManager.GetVirtualTable(entityType);
|
||||
//创建表
|
||||
CreateDataTable(dataSourceName, virtualTable);
|
||||
CreateDataTable(dataSourceName, virtualTable, existTables);
|
||||
}
|
||||
}
|
||||
else
|
||||
|
@ -86,7 +89,7 @@ namespace ShardingCore.DynamicDataSources
|
|||
{
|
||||
var virtualTable = _virtualTableManager.GetVirtualTable(entityType);
|
||||
//创建表
|
||||
CreateDataTable(dataSourceName, virtualTable);
|
||||
CreateDataTable(dataSourceName, virtualTable, existTables);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -98,18 +101,20 @@ namespace ShardingCore.DynamicDataSources
|
|||
}
|
||||
}
|
||||
}
|
||||
private void CreateDataTable(string dataSourceName, IVirtualTable virtualTable)
|
||||
private void CreateDataTable(string dataSourceName, IVirtualTable virtualTable, ISet<string> existTables)
|
||||
{
|
||||
var entityMetadata = virtualTable.EntityMetadata;
|
||||
foreach (var tail in virtualTable.GetVirtualRoute().GetAllTails())
|
||||
{
|
||||
var defaultPhysicTable = new DefaultPhysicTable(virtualTable, tail);
|
||||
if (NeedCreateTable(entityMetadata))
|
||||
{
|
||||
try
|
||||
{
|
||||
//添加物理表
|
||||
virtualTable.AddPhysicTable(new DefaultPhysicTable(virtualTable, tail));
|
||||
_tableCreator.CreateTable(dataSourceName, entityMetadata.EntityType, tail);
|
||||
virtualTable.AddPhysicTable(defaultPhysicTable);
|
||||
if (!existTables.Contains(defaultPhysicTable.FullName))
|
||||
_tableCreator.CreateTable(dataSourceName, entityMetadata.EntityType, tail);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
|
@ -123,9 +128,8 @@ namespace ShardingCore.DynamicDataSources
|
|||
else
|
||||
{
|
||||
//添加物理表
|
||||
virtualTable.AddPhysicTable(new DefaultPhysicTable(virtualTable, tail));
|
||||
virtualTable.AddPhysicTable(defaultPhysicTable);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
private bool NeedCreateTable(EntityMetadata entityMetadata)
|
||||
|
|
|
@ -6,14 +6,14 @@ using ShardingCore.Sharding.Abstractions;
|
|||
using ShardingCore.Sharding.Enumerators.TrackerEnumerators;
|
||||
using ShardingCore.Sharding.ShardingQueryExecutors;
|
||||
|
||||
/*
|
||||
* @Author: xjm
|
||||
* @Description: µü´ú¾ÛºÏÁ÷ʽÒýÇæ
|
||||
* @Date: Saturday, 14 August 2021 22:07:28
|
||||
* @Email: 326308290@qq.com
|
||||
*/
|
||||
namespace ShardingCore.Sharding.MergeEngines.EnumeratorStreamMergeEngines
|
||||
{
|
||||
/*
|
||||
* @Author: xjm
|
||||
* @Description:
|
||||
* @Date: Saturday, 14 August 2021 22:07:28
|
||||
* @Email: 326308290@qq.com
|
||||
*/
|
||||
internal class AsyncEnumeratorStreamMergeEngine<TShardingDbContext,T> : IAsyncEnumerable<T>, IEnumerable<T>
|
||||
where TShardingDbContext:DbContext,IShardingDbContext
|
||||
{
|
||||
|
|
|
@ -14,6 +14,7 @@ using ShardingCore.Extensions;
|
|||
using ShardingCore.Sharding.ParallelTables;
|
||||
using ShardingCore.Sharding.ShardingComparision;
|
||||
using ShardingCore.Sharding.ShardingComparision.Abstractions;
|
||||
using ShardingCore.TableExists;
|
||||
|
||||
namespace ShardingCore
|
||||
{
|
||||
|
@ -62,6 +63,12 @@ namespace ShardingCore
|
|||
ReplaceShardingComparerFactory=newShardingComparerFactory ?? throw new ArgumentNullException(nameof(newShardingComparerFactory));
|
||||
}
|
||||
|
||||
public Func<IServiceProvider, ITableEnsureManager<TShardingDbContext>> TableEnsureManagerFactory { get; private set; } = sp => new EmptyTableEnsureManager< TShardingDbContext>();
|
||||
public void AddTableEnsureManager(Func<IServiceProvider, ITableEnsureManager<TShardingDbContext>> newTableEnsureManagerFactory)
|
||||
{
|
||||
TableEnsureManagerFactory= newTableEnsureManagerFactory?? throw new ArgumentNullException(nameof(newTableEnsureManagerFactory));
|
||||
}
|
||||
|
||||
|
||||
///// <summary>
|
||||
///// 配置数据库分表查询和保存时的DbContext创建方式
|
||||
|
|
|
@ -0,0 +1,44 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data.Common;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using ShardingCore.Core.VirtualRoutes.TableRoutes.RouteTails;
|
||||
using ShardingCore.Core.VirtualRoutes.TableRoutes.RouteTails.Abstractions;
|
||||
using ShardingCore.Sharding.Abstractions;
|
||||
|
||||
namespace ShardingCore.TableExists
|
||||
{
|
||||
public abstract class AbstractTableEnsureManager<TShardingDbContext> : ITableEnsureManager<TShardingDbContext> where TShardingDbContext : DbContext, IShardingDbContext
|
||||
{
|
||||
protected IRouteTailFactory RouteTailFactory { get; }
|
||||
protected AbstractTableEnsureManager()
|
||||
{
|
||||
RouteTailFactory = ShardingContainer.GetService<IRouteTailFactory>();
|
||||
}
|
||||
public ISet<string> GetExistTables(string dataSourceName)
|
||||
{
|
||||
using (var scope = ShardingContainer.ServiceProvider.CreateScope())
|
||||
{
|
||||
var shardingDbContext = scope.ServiceProvider.GetService<TShardingDbContext>();
|
||||
return GetExistTables(shardingDbContext, dataSourceName);
|
||||
}
|
||||
}
|
||||
|
||||
public ISet<string> GetExistTables(IShardingDbContext shardingDbContext, string dataSourceName)
|
||||
{
|
||||
using (var dbContext =
|
||||
shardingDbContext.GetDbContext(dataSourceName, true, RouteTailFactory.Create(string.Empty)))
|
||||
{
|
||||
var dbConnection = dbContext.Database.GetDbConnection();
|
||||
dbConnection.Open();
|
||||
return DoGetExistTables(dbConnection, dataSourceName);
|
||||
}
|
||||
}
|
||||
|
||||
public abstract ISet<string> DoGetExistTables(DbConnection connection, string dataSourceName);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,23 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using ShardingCore.Sharding.Abstractions;
|
||||
|
||||
namespace ShardingCore.TableExists
|
||||
{
|
||||
public class EmptyTableEnsureManager<TShardingDbContext> : ITableEnsureManager<TShardingDbContext> where TShardingDbContext : DbContext, IShardingDbContext
|
||||
{
|
||||
public ISet<string> GetExistTables(string dataSourceName)
|
||||
{
|
||||
return new HashSet<string>();
|
||||
}
|
||||
|
||||
public ISet<string> GetExistTables(IShardingDbContext shardingDbContext, string dataSourceName)
|
||||
{
|
||||
return new HashSet<string>();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,19 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using ShardingCore.Sharding.Abstractions;
|
||||
|
||||
namespace ShardingCore.TableExists
|
||||
{
|
||||
public interface ITableEnsureManager
|
||||
{
|
||||
ISet<string> GetExistTables(string dataSourceName);
|
||||
ISet<string> GetExistTables(IShardingDbContext shardingDbContext, string dataSourceName);
|
||||
}
|
||||
public interface ITableEnsureManager<TShardingDbContext>: ITableEnsureManager where TShardingDbContext : DbContext, IShardingDbContext
|
||||
{
|
||||
}
|
||||
}
|
|
@ -0,0 +1,33 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data.Common;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using ShardingCore.Sharding.Abstractions;
|
||||
|
||||
namespace ShardingCore.TableExists
|
||||
{
|
||||
public class MySqlTableEnsureManager<TShardingDbContext> : AbstractTableEnsureManager<TShardingDbContext> where TShardingDbContext : DbContext, IShardingDbContext
|
||||
{
|
||||
private const string Tables = "Tables";
|
||||
private const string TABLE_SCHEMA = "TABLE_SCHEMA";
|
||||
private const string TABLE_NAME = "TABLE_NAME";
|
||||
|
||||
public override ISet<string> DoGetExistTables(DbConnection connection, string dataSourceName)
|
||||
{
|
||||
var database = connection.Database;
|
||||
ISet<string> result = new HashSet<string>();
|
||||
using (var dataTable = connection.GetSchema(Tables))
|
||||
{
|
||||
for (int i = 0; i < dataTable.Rows.Count; i++)
|
||||
{
|
||||
if (dataTable.Rows[i][TABLE_SCHEMA].Equals(database))
|
||||
result.Add(dataTable.Rows[i][TABLE_NAME].ToString());
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,33 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data;
|
||||
using System.Data.Common;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using ShardingCore.Core.VirtualRoutes.TableRoutes.RouteTails;
|
||||
using ShardingCore.Sharding.Abstractions;
|
||||
|
||||
namespace ShardingCore.TableExists
|
||||
{
|
||||
public class SqlServerTableEnsureManager<TShardingDbContext> : AbstractTableEnsureManager<TShardingDbContext> where TShardingDbContext : DbContext, IShardingDbContext
|
||||
{
|
||||
private const string Tables = "Tables";
|
||||
private const string TABLE_NAME = "TABLE_NAME";
|
||||
|
||||
public override ISet<string> DoGetExistTables(DbConnection connection, string dataSourceName)
|
||||
{
|
||||
ISet<string> result = new HashSet<string>();
|
||||
using (var dataTable = connection.GetSchema(Tables))
|
||||
{
|
||||
for (int i = 0; i < dataTable.Rows.Count; i++)
|
||||
{
|
||||
result.Add(dataTable.Rows[i][TABLE_NAME].ToString());
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -9,6 +9,7 @@ using Microsoft.Extensions.Logging;
|
|||
using ShardingCore.Bootstrapers;
|
||||
using ShardingCore.Helpers;
|
||||
using ShardingCore.Sharding.ReadWriteConfigurations;
|
||||
using ShardingCore.TableExists;
|
||||
using ShardingCore.Test.Domain.Entities;
|
||||
using ShardingCore.Test.Shardings;
|
||||
|
||||
|
@ -37,11 +38,9 @@ namespace ShardingCore.Test
|
|||
o.UseSqlServer(conn).UseLoggerFactory(efLogger))
|
||||
.Begin(o =>
|
||||
{
|
||||
#if DEBUG
|
||||
//o.CreateShardingTableOnStart = true;
|
||||
//o.EnsureCreatedWithOutShardingTable = true;
|
||||
|
||||
#endif //o.MaxQueryConnectionsLimit = 1;
|
||||
o.CreateShardingTableOnStart = true;
|
||||
o.EnsureCreatedWithOutShardingTable = true;
|
||||
//o.MaxQueryConnectionsLimit = 1;
|
||||
o.AutoTrackEntity = true;
|
||||
//o.AddParallelTables(typeof(SysUserMod), typeof(SysUserSalary));
|
||||
})
|
||||
|
@ -84,7 +83,9 @@ namespace ShardingCore.Test
|
|||
}
|
||||
}
|
||||
};
|
||||
},ReadStrategyEnum.Loop,readConnStringGetStrategy:ReadConnStringGetStrategyEnum.LatestEveryTime).End();
|
||||
},ReadStrategyEnum.Loop,readConnStringGetStrategy:ReadConnStringGetStrategyEnum.LatestEveryTime)
|
||||
.AddTableEnsureManager(sp=>new SqlServerTableEnsureManager<ShardingDefaultDbContext>())
|
||||
.End();
|
||||
// services.AddShardingDbContext<ShardingDefaultDbContext, DefaultDbContext>(o => o.UseMySql(hostBuilderContext.Configuration.GetSection("MySql")["ConnectionString"],new MySqlServerVersion("5.7.15"))
|
||||
// ,op =>
|
||||
// {
|
||||
|
|
|
@ -9,6 +9,7 @@ using Microsoft.Extensions.Logging;
|
|||
using ShardingCore.Bootstrapers;
|
||||
using ShardingCore.Helpers;
|
||||
using ShardingCore.Sharding.ReadWriteConfigurations;
|
||||
using ShardingCore.TableExists;
|
||||
using ShardingCore.Test2x.Domain.Entities;
|
||||
using ShardingCore.Test2x.Shardings;
|
||||
|
||||
|
@ -37,6 +38,8 @@ namespace ShardingCore.Test2x
|
|||
o.UseSqlServer(conn).UseLoggerFactory(efLogger))
|
||||
.Begin(o =>
|
||||
{
|
||||
o.CreateShardingTableOnStart = true;
|
||||
o.EnsureCreatedWithOutShardingTable = true;
|
||||
o.AutoTrackEntity = true;
|
||||
})
|
||||
.AddShardingTransaction((connection, builder) =>
|
||||
|
@ -78,7 +81,9 @@ namespace ShardingCore.Test2x
|
|||
}
|
||||
}
|
||||
};
|
||||
},ReadStrategyEnum.Loop,readConnStringGetStrategy:ReadConnStringGetStrategyEnum.LatestEveryTime).End();
|
||||
},ReadStrategyEnum.Loop,readConnStringGetStrategy:ReadConnStringGetStrategyEnum.LatestEveryTime)
|
||||
.AddTableEnsureManager(sp => new SqlServerTableEnsureManager<ShardingDefaultDbContext>())
|
||||
.End();
|
||||
// services.AddShardingDbContext<ShardingDefaultDbContext, DefaultDbContext>(o => o.UseMySql(hostBuilderContext.Configuration.GetSection("MySql")["ConnectionString"],new MySqlServerVersion("5.7.15"))
|
||||
// ,op =>
|
||||
// {
|
||||
|
|
|
@ -9,6 +9,7 @@ using Microsoft.Extensions.Logging;
|
|||
using ShardingCore.Bootstrapers;
|
||||
using ShardingCore.Helpers;
|
||||
using ShardingCore.Sharding.ReadWriteConfigurations;
|
||||
using ShardingCore.TableExists;
|
||||
using ShardingCore.Test3x.Domain.Entities;
|
||||
using ShardingCore.Test3x.Shardings;
|
||||
|
||||
|
@ -37,6 +38,8 @@ namespace ShardingCore.Test3x
|
|||
o.UseSqlServer(conn).UseLoggerFactory(efLogger))
|
||||
.Begin(o =>
|
||||
{
|
||||
o.CreateShardingTableOnStart = true;
|
||||
o.EnsureCreatedWithOutShardingTable = true;
|
||||
o.AutoTrackEntity = true;
|
||||
})
|
||||
.AddShardingTransaction((connection, builder) =>
|
||||
|
@ -78,7 +81,9 @@ namespace ShardingCore.Test3x
|
|||
}
|
||||
}
|
||||
};
|
||||
},ReadStrategyEnum.Loop,readConnStringGetStrategy:ReadConnStringGetStrategyEnum.LatestEveryTime).End();
|
||||
},ReadStrategyEnum.Loop,readConnStringGetStrategy:ReadConnStringGetStrategyEnum.LatestEveryTime)
|
||||
.AddTableEnsureManager(sp => new SqlServerTableEnsureManager<ShardingDefaultDbContext>())
|
||||
.End();
|
||||
// services.AddShardingDbContext<ShardingDefaultDbContext, DefaultDbContext>(o => o.UseMySql(hostBuilderContext.Configuration.GetSection("MySql")["ConnectionString"],new MySqlServerVersion("5.7.15"))
|
||||
// ,op =>
|
||||
// {
|
||||
|
|
|
@ -9,6 +9,7 @@ using Microsoft.Extensions.Logging;
|
|||
using ShardingCore.Bootstrapers;
|
||||
using ShardingCore.Helpers;
|
||||
using ShardingCore.Sharding.ReadWriteConfigurations;
|
||||
using ShardingCore.TableExists;
|
||||
using ShardingCore.Test5x.Domain.Entities;
|
||||
using ShardingCore.Test5x.Shardings;
|
||||
|
||||
|
@ -37,6 +38,8 @@ namespace ShardingCore.Test5x
|
|||
o.UseSqlServer(conn).UseLoggerFactory(efLogger))
|
||||
.Begin(o =>
|
||||
{
|
||||
o.CreateShardingTableOnStart = true;
|
||||
o.EnsureCreatedWithOutShardingTable = true;
|
||||
o.AutoTrackEntity = true;
|
||||
})
|
||||
.AddShardingTransaction((connection, builder) =>
|
||||
|
@ -78,7 +81,9 @@ namespace ShardingCore.Test5x
|
|||
}
|
||||
}
|
||||
};
|
||||
},ReadStrategyEnum.Loop,readConnStringGetStrategy:ReadConnStringGetStrategyEnum.LatestEveryTime).End();
|
||||
},ReadStrategyEnum.Loop,readConnStringGetStrategy:ReadConnStringGetStrategyEnum.LatestEveryTime)
|
||||
.AddTableEnsureManager(sp => new SqlServerTableEnsureManager<ShardingDefaultDbContext>())
|
||||
.End();
|
||||
// services.AddShardingDbContext<ShardingDefaultDbContext, DefaultDbContext>(o => o.UseMySql(hostBuilderContext.Configuration.GetSection("MySql")["ConnectionString"],new MySqlServerVersion("5.7.15"))
|
||||
// ,op =>
|
||||
// {
|
||||
|
|
Loading…
Reference in New Issue