加入根据 DbConnection的类型自动猜测TableEnsureManager

This commit is contained in:
MysticBoy 2022-09-28 16:50:21 +08:00
parent d3f346f433
commit c2354bf715
2 changed files with 55 additions and 1 deletions

View File

@ -230,7 +230,7 @@ namespace ShardingCore
services.TryAddSingleton<IShardingReadWriteManager, ShardingReadWriteManager>();
services.TryAddSingleton<IShardingComparer, CSharpLanguageShardingComparer>();
services.TryAddSingleton<ITableEnsureManager, EmptyTableEnsureManager>();
services.TryAddSingleton<ITableEnsureManager, GuessTableEnsureManager>();
services.TryAddShardingJob();
return services;

View File

@ -0,0 +1,54 @@
using ShardingCore.Core.VirtualRoutes.TableRoutes.RouteTails.Abstractions;
using ShardingCore.TableExists.Abstractions;
using System;
using System.Collections.Generic;
using System.Data.Common;
namespace ShardingCore.TableExists
{
public class GuessTableEnsureManager : AbstractTableEnsureManager
{
private readonly MySqlTableEnsureManager _tem_mysql;
private readonly SqliteTableEnsureManager _tem_sqlite;
private readonly PostgreSqlTableEnsureManager _tem_pgsql;
private readonly SqlServerTableEnsureManager _tem_mssql;
public GuessTableEnsureManager(IRouteTailFactory routeTailFactory) : base(routeTailFactory)
{
_tem_mysql = new MySqlTableEnsureManager(routeTailFactory);
_tem_sqlite = new SqliteTableEnsureManager(routeTailFactory);
_tem_pgsql = new PostgreSqlTableEnsureManager(routeTailFactory);
_tem_mssql = new SqlServerTableEnsureManager(routeTailFactory);
}
public override ISet<string> DoGetExistTables(DbConnection connection, string dataSourceName)
{
ISet<string> result = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
var dbctypename = connection.GetType().FullName;
switch (dbctypename)
{
case "Npgsql.NpgsqlConnection":
result = _tem_pgsql.DoGetExistTables(connection, dataSourceName);
break;
case "System.Data.Sqlite.SqliteConnection":
case "Microsoft.Data.Sqlite.SqliteConnection":
result = _tem_sqlite.DoGetExistTables(connection, dataSourceName);
break;
case "MySqlConnector.MySqlConnection":
result = _tem_mysql.DoGetExistTables(connection, dataSourceName);
break;
case "Microsoft.Data.SqlClient.SqlConnection":
case "System.Data.SqlClient.SqlConnection":
result = _tem_mssql.DoGetExistTables(connection, dataSourceName);
break;
default:
break;
}
return result;
}
}
}