示例#1
0
        /// <summary>
        /// 获取指定数据库链接的Hash值
        /// </summary>
        /// <param name="connNameOrString">配置名或链接字符串</param>
        /// <returns></returns>
        public static int GetHashCode(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.GetHashCode());
        }
示例#2
0
 public static bool Add(string name, string type, string conn)
 {
     if (!string.IsNullOrEmpty(name) && !string.IsNullOrEmpty(type) && DBSchema.DBScheams.Count > 0)
     {
         int tableHash = TableInfo.GetHashCode(name);
         int dbHash    = ConnBean.GetHashCode(conn);
         if (DBSchema.DBScheams.ContainsKey(dbHash))
         {
             return(DBSchema.DBScheams[dbHash].Add(tableHash, type, name));
         }
     }
     return(false);
 }
示例#3
0
 /// <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);
 }
示例#4
0
 public static bool Add(string name, string type, string conn)
 {
     if (!string.IsNullOrEmpty(name) && !string.IsNullOrEmpty(type) && DBSchema.DBScheams.Count > 0)
     {
         int tableHash = TableSchema.GetTableHash(name);
         Dictionary <string, string> dic = TableSchema.GetSchemas(conn, type);
         if (dic != null && !dic.ContainsKey(name))
         {
             dic.Add(name, name);
         }
         int dbHash = ConnBean.GetHashCode(conn);
         if (DBSchema.DBScheams.ContainsKey(dbHash))
         {
             return(DBSchema.DBScheams[dbHash].Add(tableHash, type, name));
         }
     }
     return(false);
 }
示例#5
0
 /// <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);
 }
示例#6
0
        /// <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);
        }
示例#7
0
 internal static string GetBaseKey(string tableName, string connString)
 {
     return("AutoCache:" + ConnBean.GetHashCode(connString) + "." + tableName);
 }
示例#8
0
        internal static Dictionary <string, string> GetSchemas(string conn, string type)
        {
            ConnBean connBean = ConnBean.Create(conn);
            int      hash     = connBean.GetHashCode();
            Dictionary <int, Dictionary <string, string> > schemaDic = null;

            switch (type)
            {
            case "U":
                schemaDic = _TableCache;
                break;

            case "V":
                schemaDic = _ViewCache;
                break;

            case "P":
                schemaDic = _ProcCache;
                break;
            }
            if (schemaDic.ContainsKey(hash))
            {
                return(schemaDic[hash]);
            }
            Dictionary <string, string> tables = null;

            #region 查看有没有表架构缓存
            if (!string.IsNullOrEmpty(AppConfig.DB.SchemaMapPath))
            {
                string fullPath = AppConfig.RunPath + AppConfig.DB.SchemaMapPath + hash + ".ts";
                if (System.IO.File.Exists(fullPath))
                {
                    tables = JsonHelper.Split(IOHelper.ReadAllText(fullPath));
                }
            }
            #endregion
            if (tables == null)
            {
                lock (ot)
                {
                    if (!schemaDic.ContainsKey(hash))
                    {
                        #region 从数据库读
                        using (DalBase helper = DalCreate.CreateDal(connBean.ConnName))
                        {
                            helper.IsRecordDebugInfo = false;
                            switch (type)
                            {
                            case "U":
                                tables = helper.GetTables();
                                break;

                            case "V":
                                tables = helper.GetViews();
                                break;

                            case "P":
                                tables = helper.GetProcs();
                                break;
                            }
                        }
                    }
                    else
                    {
                        return(schemaDic[hash]);
                    }
                }
                #endregion
            }
            #region 写入表结构映射
            if (!string.IsNullOrEmpty(AppConfig.DB.SchemaMapPath))
            {
                string fullPath = AppConfig.RunPath + AppConfig.DB.SchemaMapPath + hash + ".ts";
                IOHelper.Save(fullPath, JsonHelper.ToJson(tables), false, true);
            }
            #endregion

            #region 写入缓存
            if (!schemaDic.ContainsKey(hash) && tables != null && tables.Count > 0)//读不到表不缓存。
            {
                schemaDic.Add(hash, tables);
            }
            #endregion
            return(tables);
        }