Пример #1
0
        protected Dictionary <string, string> GetSchemaDic(string sql)
        {
            if (string.IsNullOrEmpty(sql))
            {
                return(null);
            }
            Dictionary <string, string> dic = null;
            string key = "UVPCache_" + StaticTool.GetHashKey(sql + ConnName);

            #region 缓存检测
            if (!string.IsNullOrEmpty(AppConfig.DB.SchemaMapPath))
            {
                string fullPath = AppConfig.RunPath + AppConfig.DB.SchemaMapPath + key + ".json";
                if (System.IO.File.Exists(fullPath))
                {
                    string json = IOHelper.ReadAllText(fullPath);
                    dic = JsonHelper.ToEntity <Dictionary <string, string> >(json);
                    if (dic != null && dic.Count > 0)
                    {
                        return(dic);
                    }
                }
            }
            #endregion
            dic = new Dictionary <string, string>(StringComparer.OrdinalIgnoreCase);
            IsRecordDebugInfo = false || AppDebug.IsContainSysSql;
            DbDataReader sdr = ExeDataReader(sql, false);
            IsRecordDebugInfo = true;
            if (sdr != null)
            {
                string tableName = string.Empty;
                while (sdr.Read())
                {
                    tableName = Convert.ToString(sdr["TableName"]);
                    if (!dic.ContainsKey(tableName))
                    {
                        dic.Add(tableName, Convert.ToString(sdr["Description"]));
                    }
                }
                sdr.Close();
                sdr = null;
            }
            #region 缓存设置
            if (!string.IsNullOrEmpty(AppConfig.DB.SchemaMapPath) && dic != null && dic.Count > 0)
            {
                string folderPath = AppConfig.RunPath + AppConfig.DB.SchemaMapPath;
                if (!System.IO.Directory.Exists(folderPath))
                {
                    System.IO.Directory.CreateDirectory(folderPath);
                }
                string json = JsonHelper.ToJson(dic);
                IOHelper.Write(folderPath + key + ".json", json);
            }
            #endregion
            return(dic);
        }
Пример #2
0
        /// <summary>
        /// 缓存表架构Key
        /// </summary>
        internal static string GetSchemaKey(string tableName, string dbName, DalType dalType)
        {
            string key   = tableName;
            int    start = key.IndexOf('(');
            int    end   = key.LastIndexOf(')');

            if (start > -1 && end > -1)//自定义table
            {
                key = "View" + StaticTool.GetHashKey(key);
            }
            else
            {
                if (key.IndexOf('.') > 0)
                {
                    dbName = key.Split('.')[0];
                }
                key = SqlFormat.NotKeyword(key);
            }
            return("ColumnsCache:" + dalType + "_" + dbName + "_" + key);
        }
Пример #3
0
        private static string GetKey(AopEnum action, AopInfo aopInfo, string baseKey)
        {
            StringBuilder sb = new StringBuilder();

            sb.Append(baseKey);
            switch (action)
            {
            case AopEnum.ExeNonQuery:
            case AopEnum.Insert:
            case AopEnum.Update:
            case AopEnum.Delete:
                return(sb.ToString());
            }

            #region Key1:DBType
            sb.Append(".");
            sb.Append(action);
            if (aopInfo.DBParameters != null && aopInfo.DBParameters.Count > 0)
            {
                foreach (DbParameter item in aopInfo.DBParameters)
                {
                    sb.Append(item.ParameterName);
                    sb.Append(item.Value);
                }
            }
            if (aopInfo.CustomDbPara != null)
            {
                foreach (AopCustomDbPara item in aopInfo.CustomDbPara)
                {
                    sb.Append(item.ParaName);
                    sb.Append(item.Value);
                }
            }
            if (aopInfo.SelectColumns != null)
            {
                foreach (object item in aopInfo.SelectColumns)
                {
                    sb.Append(item);
                    sb.Append(item);
                }
            }
            #endregion

            switch (action)
            {
            case AopEnum.ExeMDataTableList:
            case AopEnum.ExeMDataTable:
            case AopEnum.ExeScalar:
                sb.Append(aopInfo.IsProc);
                sb.Append(aopInfo.ProcName);
                break;

            case AopEnum.Exists:
            case AopEnum.Fill:
            case AopEnum.GetCount:
                sb.Append(aopInfo.Where);
                break;

            case AopEnum.Select:
                sb.Append(aopInfo.PageIndex);
                sb.Append(aopInfo.PageSize);
                sb.Append(aopInfo.Where);
                break;
            }

            return(StaticTool.GetHashKey(sb.ToString()));
        }
Пример #4
0
 public string GetHashKey()
 {
     return(StaticTool.GetHashKey(ConnString.Replace(" ", "").ToLower()));
 }
Пример #5
0
        /// <summary>
        /// 将原有的初始化改造成延时加载。
        /// </summary>
        private void SetDelayInit(Object entityInstance, string tableName, string conn, AopOp op)
        {
            conn     = string.IsNullOrEmpty(conn) ? AppConfig.DB.DefaultConn : conn;
            entity   = entityInstance;
            typeInfo = entity.GetType();
            try
            {
                if (string.IsNullOrEmpty(tableName))
                {
                    tableName = typeInfo.Name;
                    if (tableName.EndsWith(AppConfig.EntitySuffix))
                    {
                        tableName = tableName.Substring(0, tableName.Length - AppConfig.EntitySuffix.Length);
                    }
                }

                string key = tableName + StaticTool.GetHashKey(conn);
                if (!CacheManage.LocalInstance.Contains(key))
                {
                    DalType dal      = DBTool.GetDalType(conn);
                    bool    isTxtDal = dal == DalType.Txt || dal == DalType.Xml;
                    string  errMsg   = string.Empty;
                    Columns = DBTool.GetColumns(tableName, conn, out errMsg);//内部链接错误时抛异常。
                    if (Columns == null || Columns.Count == 0)
                    {
                        if (errMsg != string.Empty)
                        {
                            Error.Throw(errMsg);
                        }
                        Columns = TableSchema.GetColumns(typeInfo);
                        if (!DBTool.ExistsTable(tableName, conn))
                        {
                            if (!DBTool.CreateTable(tableName, Columns, conn))
                            {
                                Error.Throw("SimpleOrmBase :Create Table Error:" + tableName);
                            }
                        }
                    }
                    else if (isTxtDal)//文本数据库
                    {
                        if (FieldSource != FieldSource.Data)
                        {
                            MDataColumn c2 = TableSchema.GetColumns(typeInfo);
                            if (FieldSource == FieldSource.BothOfAll)
                            {
                                Columns.AddRange(c2);
                            }
                            else
                            {
                                Columns = c2;
                            }
                        }
                    }

                    if (Columns != null && Columns.Count > 0)
                    {
                        CacheManage.LocalInstance.Set(key, Columns, 1440, null);
                    }
                }
                else
                {
                    Columns = CacheManage.LocalInstance.Get(key) as MDataColumn;
                }

                _Action = new MAction(Columns.ToRow(tableName), conn);
                if (typeInfo.Name == "SysLogs")
                {
                    _Action.SetAopState(Aop.AopOp.CloseAll);
                }
                else
                {
                    _Action.SetAopState(op);
                }
                _Action.EndTransation();
            }
            catch (Exception err)
            {
                if (typeInfo.Name != "SysLogs")
                {
                    Log.WriteLogToTxt(err);
                }
                throw;
            }
        }