实现 PostgreSQL和 Sqlite 表是否存在的判断 接口

This commit is contained in:
MysticBoy 2022-09-28 16:49:57 +08:00
parent dadeb5086f
commit d3f346f433
2 changed files with 64 additions and 0 deletions

View File

@ -0,0 +1,31 @@
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 PostgreSqlTableEnsureManager : AbstractTableEnsureManager
{
public PostgreSqlTableEnsureManager(IRouteTailFactory routeTailFactory) : base(routeTailFactory)
{
}
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>(StringComparer.OrdinalIgnoreCase);
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;
}
}
}

View File

@ -0,0 +1,33 @@
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 SqliteTableEnsureManager : AbstractTableEnsureManager
{
public SqliteTableEnsureManager(IRouteTailFactory routeTailFactory) : base(routeTailFactory)
{
}
public override ISet<string> DoGetExistTables(DbConnection connection, string dataSourceName)
{
ISet<string> result = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
using (var query = connection.CreateCommand())
{
query.CommandText = "SELECT tbl_name FROM sqlite_master;";
using (var reader = query.ExecuteReader())
{
while (reader.Read())
{
var str = (string)reader["tbl_name"];
result.Add(str);
}
}
}
return result;
}
}
}