实现 PostgreSQL和 Sqlite 表是否存在的判断 接口
This commit is contained in:
parent
dadeb5086f
commit
d3f346f433
|
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue