public TableInfo(string name, string type, string description, DBInfo parent) { this.Name = name; this.Type = type; this.Description = description; this.Parent = parent; }
/// <summary> /// 获取(并缓存)数据库的“表、视图、存储过程”名称列表。 /// </summary> public static DBInfo GetSchema(string conn) { ConnBean cb = ConnBean.Create(conn); if (cb != null) { int hash = cb.GetHashCode(); if (!_DBScheams.ContainsKey(hash)) { lock (o) { if (!_DBScheams.ContainsKey(hash)) { DBInfo dbSchema = GetSchemaDic(cb.ConnName); if (dbSchema != null && (dbSchema.Tables.Count > 0 || dbSchema.Views.Count > 0 || dbSchema.Procs.Count > 0)) { _DBScheams.Add(hash, dbSchema); } return(dbSchema); } } } if (_DBScheams.ContainsKey(hash)) { return(_DBScheams[hash]); } } return(null); }
private static DBInfo GetSchemaDic(string conn) { DBInfo info = new DBInfo(); using (DalBase dal = DalCreate.CreateDal(conn)) { info.ConnName = dal.ConnObj.Master.ConnName; info.ConnString = dal.ConnObj.Master.ConnString; info.DataBaseName = dal.DataBaseName; info.DataBaseType = dal.DataBaseType; info.DataBaseVersion = dal.Version; Dictionary <string, string> tables = dal.GetTables(); if (tables != null && tables.Count > 0) { Dictionary <int, TableInfo> dic = new Dictionary <int, TableInfo>(); foreach (KeyValuePair <string, string> item in tables) { int hash = TableInfo.GetHashCode(item.Key); if (!dic.ContainsKey(hash)) { dic.Add(hash, new TableInfo(item.Key, "U", item.Value, info)); } } info.Tables = dic; } Dictionary <string, string> views = dal.GetViews(); if (views != null && views.Count > 0) { Dictionary <int, TableInfo> dic = new Dictionary <int, TableInfo>(); foreach (KeyValuePair <string, string> item in views) { int hash = TableInfo.GetHashCode(item.Key); if (!dic.ContainsKey(hash)) { dic.Add(hash, new TableInfo(item.Key, "V", item.Value, info)); } } info.Views = dic; } Dictionary <string, string> procs = dal.GetProcs(); if (procs != null && procs.Count > 0) { Dictionary <int, TableInfo> dic = new Dictionary <int, TableInfo>(); foreach (KeyValuePair <string, string> item in procs) { int hash = TableInfo.GetHashCode(item.Key); if (!dic.ContainsKey(hash)) { dic.Add(hash, new TableInfo(item.Key, "P", item.Value, info)); } } info.Procs = dic; } } return(info); }
/// <summary> /// 是否存在指定的表名、视图名、存储过程名 /// </summary> /// <param name="newName"></param> /// <param name="conn"></param> /// <returns></returns> public static bool Exists(string name, string type, string conn) { string newName = name; string newConn = GetConn(newName, out newName, conn); if (string.IsNullOrEmpty(conn) || (name.Contains("..") && newConn.StartsWith(name.Split('.')[0])))//已指定链接,则不切换链接 { conn = newConn; } //if (DBSchema.DBScheams.Count == 0 && !string.IsNullOrEmpty(conn)) //{ // DBSchema.GetSchema(conn); //} if (!string.IsNullOrEmpty(newName))// && DBSchema.DBScheams.Count > 0 { string tableHash = TableInfo.GetHashKey(newName); if (!string.IsNullOrEmpty(conn)) { string dbHash = ConnBean.GetHashKey(conn); if (DBSchema.DBScheams.ContainsKey(dbHash)) { TableInfo info = DBSchema.DBScheams[dbHash].GetTableInfo(tableHash, type); if (info != null) { return(true); } } else { DBInfo dbInfo = DBSchema.GetSchema(conn, false); if (dbInfo != null) { TableInfo info = dbInfo.GetTableInfo(tableHash, type); if (info != null) { return(true); } } } } else { foreach (KeyValuePair <string, DBInfo> item in DBSchema.DBScheams) { TableInfo info = item.Value.GetTableInfo(tableHash, type); if (info != null) { return(true); } } } } return(false); }
/// <summary> /// 获得数据库表相关信息 /// </summary> /// <param name="conn">指定时优先寻找。</param> /// <param name="name">表名</param> /// <returns></returns> public static TableInfo GetTableInfoByName(string name, string conn) { if (!string.IsNullOrEmpty(name)) { name = SqlFormat.NotKeyword(name); string tableHash = TableInfo.GetHashKey(name); if (!string.IsNullOrEmpty(conn)) { string dbHash = ConnBean.GetHashKey(conn); if (DBSchema.DBScheams.Count > 0 && DBSchema.DBScheams.ContainsKey(dbHash)) { TableInfo info = DBSchema.DBScheams[dbHash].GetTableInfo(tableHash); if (info != null) { return(info); } } else { DBInfo dbInfo = DBSchema.GetSchema(conn, false); if (dbInfo != null) { TableInfo info = dbInfo.GetTableInfo(tableHash); if (info != null) { return(info); } } } } else { DBInfo dbInfo = DBSchema.GetSchema(AppConfig.DB.DefaultConn, false);//优先取默认链接 if (dbInfo != null) { TableInfo info = dbInfo.GetTableInfo(tableHash); if (info != null) { return(info); } } } foreach (KeyValuePair <string, DBInfo> item in DBSchema.DBScheams) { TableInfo info = item.Value.GetTableInfo(tableHash); if (info != null) { return(info); } } } return(null); }
/// <summary> /// 是否存在指定的表名、视图名、存储过程名 /// </summary> /// <param name="name"></param> /// <param name="conn"></param> /// <returns></returns> public static bool Exists(string name, string type, string conn) { conn = GetConn(name, out name, conn); if (DBSchema.DBScheams.Count == 0 && !string.IsNullOrEmpty(conn)) { DBSchema.GetSchema(conn); } if (!string.IsNullOrEmpty(name) && DBSchema.DBScheams.Count > 0) { int tableHash = TableInfo.GetHashCode(name); if (!string.IsNullOrEmpty(conn)) { int dbHash = ConnBean.GetHashCode(conn); if (DBSchema.DBScheams.ContainsKey(dbHash)) { TableInfo info = DBSchema.DBScheams[dbHash].GetTableInfo(tableHash, type); if (info != null) { return(true); } } else { DBInfo dbInfo = DBSchema.GetSchema(conn); if (dbInfo != null) { TableInfo info = dbInfo.GetTableInfo(tableHash, type); if (info != null) { return(true); } } } } else { foreach (KeyValuePair <int, DBInfo> item in DBSchema.DBScheams) { TableInfo info = item.Value.GetTableInfo(tableHash, type); if (info != null) { return(true); } } } } return(false); }
/// <summary> /// 获取(并缓存)数据库的“表、视图、存储过程”名称列表。 /// </summary> public static DBInfo GetSchema(string conn, bool isOuterLocking) { ConnBean cb = ConnBean.Create(conn); if (cb != null) { string hash = cb.GetHashKey(); if (!_DBScheams.ContainsKey(hash)) { if (!isOuterLocking && _DBScheamsTemp.ContainsKey(hash)) { return(_DBScheamsTemp[hash]); } lock (o) { if (!_DBScheams.ContainsKey(hash)) { DBInfo dbSchema = GetSchemaDic(cb.ConnName); if (dbSchema != null && (dbSchema.Tables.Count > 0 || dbSchema.Views.Count > 0 || dbSchema.Procs.Count > 0)) { if (isOuterLocking)//外部有lock,外部不会产生foreach代码时。 { if (!_DBScheams.ContainsKey(hash)) { _DBScheams.Add(hash, dbSchema); } } else { if (!_DBScheamsTemp.ContainsKey(hash)) { _DBScheamsTemp.Add(hash, dbSchema); } } } return(dbSchema); } } } if (_DBScheams.ContainsKey(hash)) { return(_DBScheams[hash]); } } return(null); }
private static DBInfo GetSchemaDic(string conn) { DalBase dal = DalCreate.CreateDal(conn); DBInfo info = new DBInfo(); info.ConnName = dal.ConnObj.Master.ConnName; info.ConnString = dal.ConnObj.Master.ConnString; info.DataBaseName = dal.DataBase; Dictionary <string, string> tables = TableSchema.GetTables(conn); if (tables != null && tables.Count > 0) { Dictionary <int, TableInfo> dic = new Dictionary <int, TableInfo>(); foreach (KeyValuePair <string, string> item in tables) { dic.Add(TableSchema.GetTableHash(item.Key), new TableInfo(item.Key, "U", item.Value, info)); } info.Tables = dic; } Dictionary <string, string> views = TableSchema.GetViews(conn); if (views != null && views.Count > 0) { Dictionary <int, TableInfo> dic = new Dictionary <int, TableInfo>(); foreach (KeyValuePair <string, string> item in views) { dic.Add(TableSchema.GetTableHash(item.Key), new TableInfo(item.Key, "V", item.Value, info)); } info.Views = dic; } Dictionary <string, string> procs = TableSchema.GetProcs(conn); if (procs != null && procs.Count > 0) { Dictionary <int, TableInfo> dic = new Dictionary <int, TableInfo>(); foreach (KeyValuePair <string, string> item in procs) { dic.Add(TableSchema.GetTableHash(item.Key), new TableInfo(item.Key, "P", item.Value, info)); } info.Procs = dic; } return(info); }
/// <summary> /// 获得数据库表相关信息 /// </summary> /// <param name="conn">指定时优先寻找。</param> /// <param name="name">表名</param> /// <returns></returns> public static TableInfo GetTableInfoByName(string name, string conn) { if (!string.IsNullOrEmpty(name)) { name = SqlFormat.NotKeyword(name); int tableHash = TableInfo.GetHashCode(name); if (!string.IsNullOrEmpty(conn)) { int dbHash = ConnBean.GetHashCode(conn); if (DBSchema.DBScheams.Count > 0 && DBSchema.DBScheams.ContainsKey(dbHash)) { TableInfo info = DBSchema.DBScheams[dbHash].GetTableInfo(tableHash); if (info != null) { return(info); } } else { DBInfo dbInfo = DBSchema.GetSchema(conn); if (dbInfo != null) { TableInfo info = dbInfo.GetTableInfo(tableHash); if (info != null) { return(info); } } } } foreach (KeyValuePair <int, DBInfo> item in DBSchema.DBScheams) { TableInfo info = item.Value.GetTableInfo(tableHash); if (info != null) { return(info); } } } return(null); }
/// <summary> /// 获取数据库的“表、视图、存储过程”名称列表。 /// </summary> public static DBInfo GetSchema(string conn) { ConnBean cb = ConnBean.Create(conn); int hash = cb.GetHashCode(); if (!_DBScheams.ContainsKey(hash)) { lock (o) { if (!_DBScheams.ContainsKey(hash)) { DBInfo dbSchema = GetSchemaDic(cb.ConnString); if (dbSchema != null) { _DBScheams.Add(hash, dbSchema); } return(dbSchema); } } } return(null); }