private static string GetJson(MDataTable configTable) { JsonHelper json = new JsonHelper(false, false); string configKey = string.Empty; bool isKeyChanged = false; List <MDataRow> groupList = new List <MDataRow>(); int count = configTable.Rows.Count; for (int i = 0; i < count; i++) { MDataRow row = configTable.Rows[i]; if (configKey != string.Empty && configKey != row.Get <string>("ConfigKey", string.Empty).Trim()) { isKeyChanged = true; } if (isKeyChanged) { if (groupList.Count > 0) { json.Add(configKey, GetInnerJson(groupList), true); groupList.Clear(); } isKeyChanged = false; } groupList.Add(row); configKey = row.Get <string>("configKey", string.Empty).Trim(); if (i == count - 1 && groupList.Count > 0)//最的一条(最后一组处理) { json.Add(configKey, GetInnerJson(groupList), true); } } json.AddBr(); return(json.ToString()); }
//JsonHelper的实例方法 static void JsonHelperInstance() { JsonHelper js = new JsonHelper(true, true); js.Add("key", "value..."); js.Add("boolKey", "true", true); js.AddBr();//需要构造多个Json时,要换一下行 js.Add("key", "value2..."); js.Add("child", GetJson());//可以构造复杂Json。 js.AddBr(); string value = js.ToString(); OutMsg("JsonHelper:--------------------------------------------"); OutMsg(value); }
private static string GetInnerJson(List <MDataRow> groupList) { JsonHelper json = new JsonHelper(false, false); foreach (MDataRow row in groupList) { json.Add("text", row.Get <string>("ConfigName")); json.Add("value", row.Get <string>("ConfigValue")); json.AddBr(); } return(json.ToString(true)); }
private string GetJson(MDataColumn mdc) { JsonHelper js = new JsonHelper(); foreach (var cell in mdc) { js.Add("Key", cell.ColumnName); js.Add("Value", cell.ColumnName); js.AddBr(); } return(js.ToString(true)); }
private static string GetInnerJson(List <MDataRow> groupList) { MDataTable group = groupList; if (AppConfig.DB.DefaultDalType == DalType.Txt) { group.Rows.Sort("OrderNo ASC");//文本需要再次排序(因为不支持查询的时候多重排序) } JsonHelper json = new JsonHelper(false, false); foreach (MDataRow row in group.Rows) { json.Add("text", row.Get <string>("ConfigName")); json.Add("value", row.Get <string>("ConfigValue")); json.AddBr(); } return(json.ToString(true)); }
internal static void SetCache(AopEnum action, AopInfo aopInfo)//End { if (!IsCanOperateCache(aopInfo)) { return; } string baseKey = GetBaseKey(aopInfo); switch (action) { case AopEnum.ExeNonQuery: case AopEnum.Insert: case AopEnum.Update: case AopEnum.Delete: if (aopInfo.IsSuccess || aopInfo.RowCount > 0) { if (action == AopEnum.Update || action == AopEnum.ExeNonQuery) { //检测是否指定忽略的列名(多数据库的指定?{XXX.XXX}) if (!IsCanRemoveCache(action, aopInfo)) { return; } } ReadyForRemove(baseKey); } return; } if (_MemCache.CacheType == CacheType.LocalCache && _MemCache.Count > 5000000)//数量超过500万 { return; } string key = GetKey(action, aopInfo, baseKey); int flag; //0 正常;1:未识别;2:不允许缓存 SetBaseKeys(aopInfo, key, out flag); //存档Key,后续缓存失效 批量删除 if (flag == 2) { return; // } double cacheTime = AppConfig.Cache.DefaultCacheTime; // Math.Abs(12 - DateTime.Now.Hour) * 60 + DateTime.Now.Second;//缓存中午或到夜里1点 if (flag == 1 || aopInfo.PageIndex > 2) // 后面的页数,缓存时间可以短一些 { cacheTime = 2; //未知道操作何表时,只缓存2分钟(比如存储过程等语句) } switch (action) { case AopEnum.ExeMDataTableList: if (IsCanCache(aopInfo.TableList)) { JsonHelper js = new JsonHelper(false, false); foreach (MDataTable table in aopInfo.TableList) { js.Add(Guid.NewGuid().ToString(), table.ToJson(true, true, RowOp.IgnoreNull, false, EscapeOp.No)); } js.AddBr(); _MemCache.Set(key, js.ToString(), cacheTime); } break; case AopEnum.Select: case AopEnum.ExeMDataTable: if (IsCanCache(aopInfo.Table)) { _MemCache.Set(key, aopInfo.Table.ToJson(true, true, RowOp.IgnoreNull, false, EscapeOp.No), cacheTime); } break; case AopEnum.ExeScalar: _MemCache.Set(key, aopInfo.ExeResult, cacheTime); break; case AopEnum.Fill: _MemCache.Set(key, aopInfo.Row.Clone(), cacheTime); break; case AopEnum.GetCount: _MemCache.Set(key, aopInfo.RowCount, cacheTime); break; case AopEnum.Exists: _MemCache.Set(key, aopInfo.ExeResult, cacheTime); break; } }
//[ActionKey("View")] /// <summary> /// 获取下拉框Json结果集(统一处理) /// </summary> /// <returns></returns> public void GetCombobox() { string itemJson = Query <string>("sys_json"); if (!string.IsNullOrEmpty(itemJson)) { List <ComboboxItem> boxes = JsonHelper.ToList <ComboboxItem>(itemJson); JsonHelper json = new JsonHelper(); if (boxes.Count > 0) { List <MDataTable> dtList = new List <MDataTable>(); StringBuilder sb = new StringBuilder(); string value = null; for (int i = 0; i < boxes.Count; i++) { ComboboxItem item = boxes[i]; string code = SqlCode.GetCode(item.ObjName); if (code != item.ObjName) { #region 核心处理 var sql = SqlCode.GetCode(item.ObjName).ToLower(); //格式化请求参数 string key = "@text"; int index = sql.IndexOf(key); if (index > -1) { value = Query <string>("q"); //easyui远程传递参数 if (string.IsNullOrEmpty(value) && sql.IndexOf('=', index - 5, 5) > -1) //处理成1=1,同时有=号 { int end = index + key.Length; string temp = sql.Substring(0, index - 5); int start = temp.LastIndexOf(' '); sql = sql.Replace(sql.Substring(start + 1, end - start - 1), "1=1"); } else { sql = sql.Replace(key, value); } } sb.Append(sql + ";"); #endregion } else { #region 找不到,则移除。 boxes.RemoveAt(i); //从程序里找。 MDataTable dt = Combobox.Get(item.ObjName, Query <string>("q")); if (dt != null) { dtList.Insert(0, dt); boxes.Insert(0, item); } else { i--; } #endregion } } if (sb.Length > 0) { string sql = sb.ToString().TrimEnd(';'); using (MProc proc = new MProc(null, CrossDb.GetConn(sql))) { if (proc.DalType == DalType.MsSql) { proc.ResetProc(sql); dtList.AddRange(proc.ExeMDataTableList()); } else { string[] items = sql.Split(';'); foreach (string item in items) { proc.ResetProc(item); dtList.Add(proc.ExeMDataTable()); } } } } if (dtList.Count == 1 && Query <string>("q", null) != null) { jsonResult = dtList[0].ToJson(false, false, true); return; } for (int i = 0; i < dtList.Count; i++) { json.Add(boxes[i].ObjName, dtList[i].Rows.Count > 0 ? dtList[i].ToJson(false, false, true) : "[]", true); if (!string.IsNullOrEmpty(boxes[i].Parent)) { json.Add("Target", boxes[i].Parent); } json.AddBr(); } } jsonResult = string.IsNullOrEmpty(json.ToString()) ? "[]" : json.ToString(true); } }
//[ActionKey("View")] /// <summary> /// 获取下拉框Json结果集(统一处理) /// </summary> /// <returns></returns> public void GetCombobox() { string itemJson = Query <string>("sys_json"); if (!string.IsNullOrEmpty(itemJson)) { List <ComboItem> boxes = JsonHelper.ToList <ComboItem>(itemJson); JsonHelper json = new JsonHelper(); if (boxes.Count > 0) { List <MDataTable> dtList = new List <MDataTable>(); StringBuilder sb = new StringBuilder(); for (int i = 0; i < boxes.Count; i++) { ComboItem item = boxes[i]; string code = SqlCode.GetCode(item.ObjName); if (code != item.ObjName) { #region 核心处理 var sql = SqlCode.GetCode(item.ObjName);//已经处理过系统参数和Post参数 sql = ReplacePara(sql, "@para", item.Para); sql = ReplacePara(sql, "@parent", item.Parent); sb.Append(sql + " ; "); #endregion } else { #region 找不到,则移除。 boxes.RemoveAt(i); //从程序里找。 MDataTable dt = Combobox.Get(item.ObjName, Query <string>("q", item.Para)); if (dt != null) { dtList.Insert(0, dt); boxes.Insert(0, item); } else { i--; } #endregion } } if (sb.Length > 0) { string sql = sb.ToString().TrimEnd(';', ' '); using (MProc proc = new MProc(null)) { if (proc.DataBaseType == DataBaseType.MsSql) { proc.ResetProc(sql); dtList.AddRange(proc.ExeMDataTableList()); } else { string[] items = sql.Split(';'); foreach (string item in items) { proc.ResetProc(item); dtList.Add(proc.ExeMDataTable()); } } } } if (dtList.Count == 1 && Query <string>("q", null) != null) { jsonResult = dtList[0].ToJson(false, false, RowOp.None, true); return; } for (int i = 0; i < dtList.Count; i++) { json.Add(boxes[i].ObjName, dtList[i].Rows.Count > 0 ? dtList[i].ToJson(false, false, RowOp.None, true) : "[]", true); } json.AddBr(); } jsonResult = json.ToString(); } }
internal static void SetCache(AopEnum action, AopInfo aopInfo)//End { string baseKey = GetBaseKey(aopInfo); switch (action) { case AopEnum.ExeNonQuery: case AopEnum.Insert: case AopEnum.Update: case AopEnum.Delete: if (aopInfo.IsSuccess || aopInfo.RowCount > 0) { ReadyForRemove(baseKey); } return; } //if (!IsCanOperateCache(baseKey)) //{ // return; //} if (_MemCache.CacheType == CacheType.LocalCache && _MemCache.RemainMemoryPercentage < 15)//可用内存低于15% { return; } string key = GetKey(action, aopInfo, baseKey); int flag; //0 正常;1:未识别;2:不允许缓存 SetBaseKeys(aopInfo, key, out flag); //存档Key,后续缓存失效 批量删除 if (flag == 2) { return; // } double cacheTime = Math.Abs(12 - DateTime.Now.Hour) * 60 + DateTime.Now.Second; //缓存中午或到夜里1点 if (flag == 1 || aopInfo.PageIndex > 2) // 后面的页数,缓存时间可以短一些 { cacheTime = 2; //未知道操作何表时,只缓存2分钟(比如存储过程等语句) } switch (action) { case AopEnum.ExeMDataTableList: if (IsCanCache(aopInfo.TableList)) { JsonHelper js = new JsonHelper(false, false); foreach (MDataTable table in aopInfo.TableList) { js.Add(Guid.NewGuid().ToString(), table.ToJson(true, true, RowOp.IgnoreNull)); } js.AddBr(); _MemCache.Set(key, js.ToString(), cacheTime); } break; case AopEnum.Select: case AopEnum.ExeMDataTable: if (IsCanCache(aopInfo.Table)) { _MemCache.Set(key, aopInfo.Table.ToJson(true, true, RowOp.IgnoreNull), cacheTime); } break; case AopEnum.ExeScalar: _MemCache.Set(key, aopInfo.ExeResult, cacheTime); break; case AopEnum.Fill: _MemCache.Set(key, aopInfo.Row, cacheTime); break; case AopEnum.GetCount: _MemCache.Set(key, aopInfo.RowCount, cacheTime); break; } }