/// <summary> /// 构造函数 /// </summary> /// <param name="relation">条件组和其他条件组或条件的关系</param> /// <param name="condition">条件组和其他条件组或条件的关系</param> /// <param name="conditions">条件组中所包含的各种条件对象</param> public ConditionGroup(ConditionRelation relation, ICondition condition, params ICondition[] conditions) : this() { ConditionGroup group = null; if (condition != null) { if (condition is Condition) { this.ConditionCollection.Add((Condition)condition); } else if (condition is ConditionGroup) { group = (ConditionGroup)condition; group.GroupIndex = this.subGroup.Count + 1; this.AddSubGroup(group); } } if (conditions != null) { foreach (ICondition paramCond in conditions) { if (paramCond is Condition) { this.ConditionCollection.Add((Condition)paramCond); } else if (paramCond is ConditionGroup) { group = (ConditionGroup)paramCond; group.GroupIndex = this.subGroup.Count + 1; this.AddSubGroup(group); } } } }
/// <summary> /// 查找根组,即最外层的条件组 /// </summary> /// <param name="group">需要查找的条件组对象</param> /// <returns>返回最外层的条件组</returns> protected ConditionGroup GetRootGroup(ConditionGroup group) { if (group.ParentGroup == null) { return(group); } return(this.GetRootGroup(group.ParentGroup)); }
/// <summary> /// 递归解析组,即将Searcher对象中的条件,按照他们的组别进行里外层的组划分,好为条件添加括号来判定他们的执行顺序 /// </summary> /// <param name="searcher">需要解析的查询对象</param> /// <param name="mainGroup">用于返回排好层级的条件组,并填充好各级的条件对象</param> /// <param name="sortColumnList">用于返回所有需要排序的字段</param> protected void ParseGroup(Searcher searcher, ConditionGroup mainGroup, List <SearchColumn> sortColumnList) { if (searcher == null) { return; } IList <SearchColumn> conditionColumnList = searcher.ConditionColumnList; IList <Searcher> relationSearcherList = searcher.RelationSearcherList; IEnumerable <ConditionGroup> mainSubGroupList = mainGroup.SubGroup; IList <Condition> mainConditionList = mainGroup.ConditionCollection; ConditionGroup rootGroup = null; IList <Condition> conditionList = null; foreach (SearchColumn column in conditionColumnList) { if (column.SortOrder != SortOrder.None) { sortColumnList.Add(column); } if (column.ConditionCollection.Count == 0) { continue; } conditionList = column.ConditionCollection; foreach (Condition condition in conditionList) { if (condition.Group == null) { mainConditionList.Add(condition); continue; } rootGroup = this.GetRootGroup(condition.Group); if (!mainGroup.GroupId.Equals(rootGroup.GroupId) && !mainSubGroupList.Contains(rootGroup)) { mainGroup.AddSubGroup(rootGroup); } } } if (rootGroup == null) { rootGroup = mainGroup; } foreach (Searcher relationSearcher in relationSearcherList) { this.ParseGroup(relationSearcher, rootGroup, sortColumnList); } }
/// <summary> /// 将子组对象的添加到子组的集合中 /// </summary> /// <param name="subGroup">需要添加的子组</param> public void AddSubGroup(ConditionGroup subGroup) { // 如果要添加的子组的ID和当前组的ID相同,则为同一个组,不能将同组加到自己的子组集合中 if (this.groupId.Equals(subGroup.GroupId)) { throw new ArgumentException("不能将当前组加到自己的子组列表中"); } // 将当前组赋值给子组的父组 subGroup.ParentGroup = this; this.subGroup.Add(subGroup); }
/// <summary> /// 递归解析条件组,进行条件语句的拼接 /// </summary> /// <param name="group">需要解析的条件组</param> /// <returns>返回拼接后的条件字符串</returns> protected string ParseConditionString(ConditionGroup group) { List <Condition> conditionCollection = group.ConditionCollection; StringBuilder currCondStr = new StringBuilder(""); int count = 0; foreach (Condition condition in conditionCollection) { if (count != 0) { currCondStr.Append(this.RelationParse(condition.Relation)); currCondStr.Append(" "); } currCondStr.Append(this.ConditionParse(condition)); currCondStr.Append(" "); count++; } List <ConditionGroup> sortedSubGroupList = null; if (group.SubGroup.Count <ConditionGroup>() > 0) { sortedSubGroupList = group.SubGroup.OrderBy(g => g.GroupIndex).ToList <ConditionGroup>(); foreach (ConditionGroup subGroup in sortedSubGroupList) { if (count > 0) { currCondStr.Append(this.RelationParse(subGroup.GroupRelation)); } currCondStr.Append(this.ParseConditionString(subGroup)); count++; } } if (currCondStr.Length > 0) { currCondStr.Insert(0, " ("); currCondStr.Append(") "); } return(currCondStr.ToString()); }
/// <summary> /// 进行查询对象的解析 /// </summary> /// <param name="searcher">传入需要解析的查询对象</param> public void SearcherParse(Searcher searcher) { if (searcher == null) { this.conditionString = string.Empty; this.sortString = string.Empty; return; } ConditionGroup mainGroup = new ConditionGroup(0); List <SearchColumn> sortColumnList = new List <SearchColumn>(); this.ParamCollection = new DBParamCollection(); this.ParseGroup(searcher, mainGroup, sortColumnList); this.conditionString = this.ParseConditionString(mainGroup); this.ParseSortString(sortColumnList); this.sortString = string.IsNullOrEmpty(this.sortString) ? string.Empty : this.sortString + " "; }
private static ConditionGroup AddCondition(ConditionRelation relation, ICondition condition1, ICondition condition2, params ICondition[] conditions) { ConditionGroup newGroup = new ConditionGroup(); List <ICondition> conditionList = new List <ICondition>(); conditionList.Add(condition1); conditionList.Add(condition2); if (conditions != null) { conditionList.AddRange(conditions); } Condition paramCondition = null; ConditionGroup conditionGroup = null; int groupIndex = 1; int conditionIndex = 1; foreach (ICondition con in conditionList) { if (con is ConditionGroup) { conditionGroup = (ConditionGroup)con; conditionGroup.GroupIndex = groupIndex; conditionGroup.GroupRelation = relation; newGroup.AddSubGroup(conditionGroup); groupIndex++; } else if (con is Condition) { paramCondition = (Condition)con; paramCondition.Index = conditionIndex; paramCondition.Group = newGroup; paramCondition.Relation = relation; conditionIndex++; } } return(newGroup); }
/// <summary> /// 将子组对象的添加到子组的集合中 /// </summary> /// <param name="subGroup">需要添加的子组</param> public void AddSubGroup(ConditionGroup subGroup) { // 如果要添加的子组的ID和当前组的ID相同,则为同一个组,不能将同组加到自己的子组集合中 if (this.groupId.Equals(subGroup.GroupId)) { throw new ArgumentException("不能将当前组加到自己的子组列表中"); } // 将当前组赋值给子组的父组 subGroup.ParentGroup = this; this.subGroup.Add(subGroup); }
/// <summary> /// 进行查询对象的解析 /// </summary> /// <param name="searcher">传入需要解析的查询对象</param> public void SearcherParse(Searcher searcher) { if (searcher == null) { this.conditionString = string.Empty; this.sortString = string.Empty; return; } ConditionGroup mainGroup = new ConditionGroup(0); List<SearchColumn> sortColumnList = new List<SearchColumn>(); this.ParamCollection = new DBParamCollection(); this.ParseGroup(searcher, mainGroup, sortColumnList); this.conditionString = this.ParseConditionString(mainGroup); this.ParseSortString(sortColumnList); this.sortString = string.IsNullOrEmpty(this.sortString) ? string.Empty : this.sortString + " "; }
/// <summary> /// 递归解析组,即将Searcher对象中的条件,按照他们的组别进行里外层的组划分,好为条件添加括号来判定他们的执行顺序 /// </summary> /// <param name="searcher">需要解析的查询对象</param> /// <param name="mainGroup">用于返回排好层级的条件组,并填充好各级的条件对象</param> /// <param name="sortColumnList">用于返回所有需要排序的字段</param> protected void ParseGroup(Searcher searcher, ConditionGroup mainGroup, List<SearchColumn> sortColumnList) { if (searcher == null) { return; } IList<SearchColumn> conditionColumnList = searcher.ConditionColumnList; IList<Searcher> relationSearcherList = searcher.RelationSearcherList; IEnumerable<ConditionGroup> mainSubGroupList = mainGroup.SubGroup; IList<Condition> mainConditionList = mainGroup.ConditionCollection; ConditionGroup rootGroup = null; IList<Condition> conditionList = null; foreach (SearchColumn column in conditionColumnList) { if (column.SortOrder != SortOrder.None) { sortColumnList.Add(column); } if (column.ConditionCollection.Count == 0) { continue; } conditionList = column.ConditionCollection; foreach (Condition condition in conditionList) { if (condition.Group == null) { mainConditionList.Add(condition); continue; } rootGroup = this.GetRootGroup(condition.Group); if (!mainGroup.GroupId.Equals(rootGroup.GroupId) && !mainSubGroupList.Contains(rootGroup)) { mainGroup.AddSubGroup(rootGroup); } } } if(rootGroup == null) { rootGroup = mainGroup; } foreach (Searcher relationSearcher in relationSearcherList) { this.ParseGroup(relationSearcher, rootGroup, sortColumnList); } }
/// <summary> /// 递归解析条件组,进行条件语句的拼接 /// </summary> /// <param name="group">需要解析的条件组</param> /// <returns>返回拼接后的条件字符串</returns> protected string ParseConditionString(ConditionGroup group) { List<Condition> conditionCollection = group.ConditionCollection; StringBuilder currCondStr = new StringBuilder(""); int count = 0; foreach (Condition condition in conditionCollection) { if (count != 0) { currCondStr.Append(this.RelationParse(condition.Relation)); currCondStr.Append(" "); } currCondStr.Append(this.ConditionParse(condition)); currCondStr.Append(" "); count++; } List<ConditionGroup> sortedSubGroupList = null; if (group.SubGroup.Count<ConditionGroup>() > 0) { sortedSubGroupList = group.SubGroup.OrderBy(g => g.GroupIndex).ToList<ConditionGroup>(); foreach (ConditionGroup subGroup in sortedSubGroupList) { if(count > 0) { currCondStr.Append(this.RelationParse(subGroup.GroupRelation)); } currCondStr.Append(this.ParseConditionString(subGroup)); count++; } } if(currCondStr.Length > 0) { currCondStr.Insert(0, " ("); currCondStr.Append(") "); } return currCondStr.ToString(); }
/// <summary> /// 查找根组,即最外层的条件组 /// </summary> /// <param name="group">需要查找的条件组对象</param> /// <returns>返回最外层的条件组</returns> protected ConditionGroup GetRootGroup(ConditionGroup group) { if (group.ParentGroup == null) { return group; } return this.GetRootGroup(group.ParentGroup); }