//递归解析主从查询的从查询 private void ChildSqlBuilderModel(IDictionary <string, object> parameters, SqlBuildModel parentModel, object children) { if (children == null || !(children is IEnumerable <KeyValuePair <string, object> > || children is IList <object>)) { return; } if (children is IEnumerable <KeyValuePair <string, object> > ) { var child = (IDictionary <string, object>)children; var extraParams = !child.ContainsKey("ExtraParams") ? null : (IDictionary <string, object>)child["ExtraParams"]; parameters = RequestDataHelper.MergeDictionary(parameters, extraParams);//合并附加参数到sql执行参数中 IList <object> relationKeys = !child.ContainsKey("RelationKeys") ? null : (IList <object>)child["RelationKeys"]; long codeKind = child.ContainsKey("CodeKind") ? (long)child["CodeKind"] : 0; string key = (string)child["Key"]; var model = new SqlBuildModel { Sql = (string)child["Sql"], Parameters = parameters, CodeKind = codeKind, RelationKeys = relationKeys, Key = key }; if (parentModel.Children == null) { parentModel.Children = new List <SqlBuildModel>(); } parentModel.Children.Add(model); object nextChildren = child.ContainsKey("Children") ? child["Children"] : null; this.ChildSqlBuilderModel(parameters, model, nextChildren); } else { foreach (var child in (IList <object>)children) { this.ChildSqlBuilderModel(parameters, parentModel, child); } } }
private IList <SqlBuildModel> SqlBuilderModel(string scriptCode, IDictionary <string, object> parameters, object bodyJson) { Lua lua = this.GetLua(); var res = LuaScriptRunner.ExecuteLuaScript(lua, scriptCode, parameters, bodyJson); IList <SqlBuildModel> result = new List <SqlBuildModel>(); for (var i = 0; i < res.Length; i++) { var item = res[i]; if (item is LuaTable) { var table = (LuaTable)item; var extraParams = LuaScriptRunner.LuaTableToCSharpData <IDictionary <string, object> >(table["ExtraParams"]); var sqlParameters = RequestDataHelper.MergeDictionary(parameters, extraParams);//合并附加参数到sql执行参数中 IList <object> relationKeys = LuaScriptRunner.LuaTableToCSharpData <IList <object> >(table["RelationKeys"]); long codeKind = table["CodeKind"] == null ? 0 : (long)table["CodeKind"]; string key = (string)table["Key"]; var model = new SqlBuildModel { Sql = (string)table["Sql"], Parameters = sqlParameters, CodeKind = codeKind, RelationKeys = relationKeys, Key = key }; result.Add(model); this.ChildSqlBuilderModel(parameters, model, LuaScriptRunner.LuaTableToCSharpData <object>(table["Children"])); } else { throw new CustomException(99, "构建SQL的脚本返回有误"); } } return(result); }
private void RowMasterDetail(IDictionary <string, object> parent, SqlBuildModel item, IDictionary <string, object> parameters) { if (item.Children != null) { foreach (var child in item.Children) { if (child.RelationKeys?.Count > 0) { foreach (string relationKey in child.RelationKeys) { string key = relationKey.Trim(); if (key == "") { continue; } if (((IDictionary <string, object>)parent).ContainsKey(key)) { object paramValue = ((IDictionary <string, object>)parent)[key]; child.Parameters[relationKey] = paramValue; } else { if (!parameters.ContainsKey(key)) { throw new CustomException(11, "主从查询关联参数有误"); } } } } ((IDictionary <string, object>)parent)[child.Key] = this.MasterDetail(child, parameters); } } }
//主从查询 private object MasterDetail(SqlBuildModel item, IDictionary <string, object> parameters) { object result = this.ExecuteSql(item.Sql, item.CodeKind, item.Parameters); if (item.Children != null && item.Children.Count > 0) { this.ForEachMaster(item, parameters, result); } return(result); }
private void ForEachMaster(SqlBuildModel item, IDictionary <string, object> parameters, object result) { if (result is IDictionary <string, object> ) { this.RowMasterDetail((IDictionary <string, object>)result, item, parameters); } else if (result is IList <IDictionary <string, object> > ) { foreach (var row in (IList <IDictionary <string, object> >)result) { this.RowMasterDetail((IDictionary <string, object>)row, item, parameters); } } }
private object ExecuteScript(string scriptCode, int codeKind, IDictionary <string, object> parameters, object bodyJson) { object result = null; if (codeKind == 4)//脚本结果 { Lua lua = GetLua(); result = LuaScriptRunner.ExecuteLuaScript(lua, scriptCode, parameters, bodyJson); } else { IList <SqlBuildModel> sqlItems = this.SqlBuilderModel(scriptCode, parameters, bodyJson); if (sqlItems.Count == 0) { throw new CustomException(11, "SQL语句配置错误"); } switch (codeKind) { case 1: //分页 { //分页,第一条语句为分页语句,后面如果有其他语句,以字典值的形式返回结果 result = new Dictionary <string, object>(); SqlBuildModel sqlItem = null; for (var i = 0; i < sqlItems.Count; i++) { sqlItem = sqlItems[i]; var key = i.ToString(); if (!string.IsNullOrWhiteSpace(sqlItem.Key)) { key = sqlItem.Key; } object res; if (i == 0) { res = this.Page(sqlItem.Sql, parameters); this.ForEachMaster(sqlItem, parameters, ((IDictionary <string, object>)res)["List"]); } else { res = this.MasterDetail(sqlItem, parameters); } ((IDictionary <string, object>)result)[key] = res; } if (((Dictionary <string, object>)result).Count() == 1) { result = ((Dictionary <string, object>)result).ElementAt(0).Value; } } break; case 2: //结果集 - 字典(支持主从) { int index = 0; result = new Dictionary <string, object>(); foreach (var item in sqlItems) { var key = index.ToString(); if (!string.IsNullOrWhiteSpace(item.Key)) { key = item.Key; } object res = this.MasterDetail(item, parameters); ((Dictionary <string, object>)result)[key] = res; index++; } if (((Dictionary <string, object>)result).Count() == 1) { result = ((Dictionary <string, object>)result).ElementAt(0).Value; } } break; case 3: //结果集-列表(支持主从) { int index = 0; result = new List <object>(); foreach (var item in sqlItems) { var key = index.ToString(); if (!string.IsNullOrWhiteSpace(item.Key)) { key = item.Key; } object res = this.MasterDetail(item, parameters); ((List <object>)result).Add(res); index++; } if (((List <object>)result).Count() == 1) { result = ((List <object>)result).ElementAt(0); } } break; default: throw new CustomException(11, "非有效执行结果类型"); } } return(result); }