using CYQ.Data.SQL;
using System.Collections.Generic;
namespace CYQ.Data.Tool
{
///
/// 数据库信息
///
public class DBInfo
{
internal DBInfo()
{
}
private string _ConnName;
public string ConnName
{
get
{
return _ConnName;
}
internal set
{
_ConnName = value;
}
}
private string _ConnString;
public string ConnString
{
get
{
return _ConnString;
}
internal set
{
_ConnString = value;
}
}
private string _DataBaseName;
public string DataBaseName
{
get
{
return _DataBaseName;
}
internal set
{
_DataBaseName = value;
}
}
private string _DataBaseVersion;
public string DataBaseVersion
{
get
{
if (!isGetVersion && string.IsNullOrEmpty(_DataBaseVersion))
{
GetVersion();
}
return _DataBaseVersion;
}
internal set
{
_DataBaseVersion = value;
}
}
private DataBaseType _DataBaseType;
public DataBaseType DataBaseType
{
get
{
return _DataBaseType;
}
internal set
{
_DataBaseType = value;
}
}
private MDictionary _Tables = new MDictionary();
public MDictionary Tables
{
get
{
return _Tables;
}
internal set
{
_Tables = value;
}
}
private MDictionary _Views = new MDictionary();
public MDictionary Views
{
get
{
if (!isGetViews && _Views.Count == 0)
{
isGetViews = true;
InitInfos("V", false);//延迟加载
}
return _Views;
}
internal set
{
_Views = value;
}
}
///
/// 用于遍历使用(多线程下)
///
private MDictionary _Procs = new MDictionary();
public MDictionary Procs
{
get
{
if (!isGetProcs && _Procs.Count == 0)
{
isGetProcs = true;
InitInfos("P", false);//延迟加载
}
return _Procs;
}
internal set
{
_Procs = value;
}
}
///
/// 根据名称(按顺序)获取表、视图、存储过程信息。
///
/// 名称
///
public TableInfo GetTableInfo(string name)
{
string key = TableInfo.GetHashKey(name);
return GetTableInfoByHash(key, null);
}
///
/// 根据名称获取指定类型(表、视图、存储过程)的信息。
///
/// 名称
/// 指定类型:U、V或P
///
public TableInfo GetTableInfo(string name, string type)
{
string key = TableInfo.GetHashKey(name);
return GetTableInfoByHash(key, type);
}
internal TableInfo GetTableInfoByHash(string tableHash)
{
return GetTableInfoByHash(tableHash, null);
}
internal TableInfo GetTableInfoByHash(string tableHash, string type)
{
if ((type == null || type == "U") && Tables != null && Tables.ContainsKey(tableHash))
{
return Tables[tableHash];
}
if ((type == null || type == "V") && Views != null && Views.ContainsKey(tableHash))
{
return Views[tableHash];
}
if ((type == null || type == "P") && Procs != null && Procs.ContainsKey(tableHash))
{
return Procs[tableHash];
}
return null;
}
///
/// DBTool Drop Table 时调用
///
///
///
///
internal bool Remove(string tableHash, string type)
{
if (Tables != null && (type == null || type == "U") && Tables.ContainsKey(tableHash))
{
Tables[tableHash].RemoveCache();
return Tables.Remove(tableHash);
}
if (Views != null && (type == null || type == "V") && Views.ContainsKey(tableHash))
{
Views[tableHash].RemoveCache();
return Views.Remove(tableHash);
}
if (Procs != null && (type == null || type == "P") && Procs.ContainsKey(tableHash))
{
Procs[tableHash].RemoveCache();
return Procs.Remove(tableHash);
}
return false;
}
///
/// DBTool Add Table 时调用
///
///
///
///
///
internal bool Add(string tableHash, string type, string name)
{
try
{
if (Tables != null && (type == null || type == "U") && !Tables.ContainsKey(tableHash))
{
Tables.Add(tableHash, new TableInfo(name, type, null, this));
}
if (Views != null && (type == null || type == "V") && !Views.ContainsKey(tableHash))
{
Views.Add(tableHash, new TableInfo(name, type, null, this));
}
if (Procs != null && (type == null || type == "P") && !Procs.ContainsKey(tableHash))
{
Procs.Add(tableHash, new TableInfo(name, type, null, this));
}
return true;
}
catch
{
return false;
}
}
///
/// 获取指定数据库链接的HashKey
///
/// 配置名或链接字符串
///
public static string GetHashKey(string connNameOrString)
{
ConnBean connBean = ConnBean.Create(connNameOrString);
if (connBean == null)
{
string err = "DBInfo.GetHashCode ConnBean can't create by " + connNameOrString;
Log.Write(err, LogType.DataBase);
Error.Throw(err);
}
return connBean.GetHashKey();
}
///
/// 刷新:表、视图、存储过程 缓存。
///
public void Reflesh()
{
Reflesh("All");
}
///
/// 刷新:表、视图或存储过程列表 缓存。
/// 指定类型:U、V或P
///
public void Reflesh(string type)
{
if ((type == "All" || type == "U") && _Tables != null && _Tables.Count > 0)
{
InitInfos("U", true);
}
if ((type == "All" || type == "V") && _Views != null && _Views.Count > 0)
{
InitInfos("V", true);
}
if ((type == "All" || type == "P") && _Procs != null && _Procs.Count > 0)
{
InitInfos("P", true);
}
}
#region 延迟加载
internal bool isGetViews = false, isGetProcs = false, isGetVersion = false;
private void InitInfos(string type, bool isIgnoreCache)
{
using (DalBase dal = DalCreate.CreateDal(ConnString))
{
Dictionary infoDic = null;
switch (type)
{
case "U":
infoDic = dal.GetTables(isIgnoreCache);
break;
case "V":
infoDic = dal.GetViews(isIgnoreCache);
break;
case "P":
infoDic = dal.GetProcs(isIgnoreCache);
break;
}
if (infoDic != null && infoDic.Count > 0)
{
MDictionary dic = new MDictionary();
foreach (KeyValuePair item in infoDic)
{
string hash = TableInfo.GetHashKey(item.Key);
if (!dic.ContainsKey(hash))
{
dic.Add(hash, new TableInfo(item.Key, type, item.Value, this));
}
}
switch (type)
{
case "U":
_Tables = dic;
break;
case "V":
_Views = dic;
break;
case "P":
_Procs = dic;
break;
}
}
}
}
private void GetVersion()
{
if (string.IsNullOrEmpty(_DataBaseVersion))
{
using (DalBase dal = DalCreate.CreateDal(ConnString))
{
_DataBaseVersion = dal.Version;
}
}
}
#endregion
}
}