我正在尝试创建一个函数来从通用DbConnection返回所有视图名称。我希望它适用于尽可能多的数据库类型,但如果我排除一些深奥的数据库类型,我不介意。
在我目前的代码中,我对Oracle有一些特殊的逻辑,但我不能动摇我的代码不能以非常强大的方式处理事情的感觉:
Collection<string> Views
{
get
{
using (var dtViews = dbConnection.GetSchema("Views"))
// Determine the proper method to retrieve all tables and views
return ((dbType == dbTypeEnum.Oracle) ? GetAllViewsOracle(dtViews) : GetAllViews(dtViews));
}
}
static Collection<string> GetAllViews(DataTable dt)
{
return GetSingleDataTableCol(dt.Select("", "TABLE_NAME ASC"), dt.Columns["TABLE_NAME"]);
}
static Collection<string> GetAllViewsOracle(DataTable dtViews)
{
if (!dtViews.Columns.Contains("OWNER"))
return GetAllViews(dtViews);
if (!dtViews.Columns.Contains("VIEW_NAME"))
return new Collection<string>();
return GetSingleDataTableCol(dtViews.Select("OWNER NOT IN ('SYS','SYSTEM')", "VIEW_NAME ASC"), dtViews.Columns["VIEW_NAME"]);
}
protected static Collection<string> GetSingleDataTableCol(DataRow[] rows, DataColumn col)
{
var lCol = new List<string>();
if (col != null)
{
foreach (DataRow rowTable in rows)
{
lCol.Add(rowTable[col].ToString());
}
}
return lCol;
}
使用Oracle,我会过滤“SYS”和“SYSTEM”模式/所有者,因为Oracle有一堆我永远不会使用的特殊表。
理想情况下,我甚至不需要使用特殊逻辑来处理Oracle /其他人。我可以比目前处理它更普遍地获得所有视图吗?