/// <summary> /// 判断 OrderByClip 是否为null /// </summary> /// <param name="orderByClip"></param> /// <returns></returns> public static bool IsNullOrEmpty(OrderByClip orderByClip) { if ((null == orderByClip) || string.IsNullOrEmpty(orderByClip.ToString())) { return(true); } return(false); }
/// <summary> /// 比较 /// </summary> /// <param name="orderByClip"></param> /// <returns></returns> public bool Equals(OrderByClip orderByClip) { if (null == orderByClip) { return(false); } return(this.ToString().Equals(orderByClip.ToString())); }
/// <summary> /// 去掉的表前缀 /// </summary> /// <returns></returns> public OrderByClip RemovePrefixTableName() { OrderByClip tempOrderByClip = new OrderByClip(); foreach (KeyValuePair <string, OrderByOperater> kv in this.orderByClip) { string keyName = kv.Key; if (kv.Key.IndexOf('.') > 0) { keyName = keyName.Substring(keyName.IndexOf('.') + 1); } tempOrderByClip.orderByClip.Add(keyName, kv.Value); } return(tempOrderByClip); }
}/// <summary> /// /// </summary> /// <param name="exprBody"></param> /// <param name="orderBy"></param> /// <returns></returns> private static OrderByClip ToOrderByClipChild(System.Linq.Expressions.Expression exprBody, OrderByOperater orderBy) { if (exprBody is MemberExpression) { var e = (MemberExpression)exprBody; OrderByClip gb = OrderByClip.None; var filedProp = GetFieldName(e.Member); if (orderBy == OrderByOperater.DESC) { gb = gb && CreateField(filedProp, e.Expression.Type).Desc; } else { gb = gb && CreateField(filedProp, e.Expression.Type).Asc; } return(gb); } if (exprBody is NewExpression) { var exNew = (NewExpression)exprBody; var type = exNew.Constructor.DeclaringType; var list = new List <string>(exNew.Arguments.Count); OrderByClip gb = OrderByClip.None; foreach (MemberExpression member in exNew.Arguments) { var filedProp = GetFieldName(member.Member); if (orderBy == OrderByOperater.DESC) { gb = gb && CreateField(filedProp, member.Expression.Type).Desc; } else { gb = gb && CreateField(filedProp, member.Expression.Type).Asc; } } return(gb); } if (exprBody is UnaryExpression) { var ueEx = (UnaryExpression)exprBody; return(ToOrderByClipChild(ueEx.Operand, orderBy)); } throw new Exception("暂时不支持的Order by lambda写法!请使用经典写法!"); }
/// <summary> /// 创建分页查询 /// </summary> /// <param name="fromSection"></param> /// <param name="startIndex"></param> /// <param name="endIndex"></param> /// <returns></returns> public virtual FromSection CreatePageFromSection(FromSection fromSection, int startIndex, int endIndex) { Check.Require(startIndex, "startIndex", Check.GreaterThanOrEqual <int>(1)); Check.Require(endIndex, "endIndex", Check.GreaterThanOrEqual <int>(1)); Check.Require(startIndex <= endIndex, "startIndex must be less than endIndex!"); Check.Require(fromSection, "fromSection", Check.NotNullOrEmpty); int pageSize = endIndex - startIndex + 1; if (startIndex == 1) { fromSection.PrefixString = string.Concat(" TOP ", pageSize.ToString()); } else { if (OrderByClip.IsNullOrEmpty(fromSection.OrderByClip)) { foreach (Field f in fromSection.Fields) { if (!f.PropertyName.Equals("*") && f.PropertyName.IndexOf('(') == -1) { fromSection.OrderBy(f.Asc); break; } } } Check.Require(!OrderByClip.IsNullOrEmpty(fromSection.OrderByClip), "query.OrderByClip could not be null or empty!"); int count = fromSection.Count(fromSection); List <Parameter> list = fromSection.Parameters; if (endIndex > count) { int lastnumber = count - startIndex + 1; if (startIndex > count) { lastnumber = count % pageSize; } fromSection.PrefixString = string.Concat(" TOP ", lastnumber.ToString()); fromSection.OrderBy(fromSection.OrderByClip.ReverseOrderByClip); // fromSection.TableName = string.Concat(" (", fromSection.SqlString, ") AS temp_table "); fromSection.PrefixString = string.Empty; fromSection.DistinctString = string.Empty; fromSection.GroupBy(GroupByClip.None); fromSection.Select(Field.All); fromSection.OrderBy(fromSection.OrderByClip.ReverseOrderByClip); fromSection.Where(WhereClip.All); } else { if (startIndex < count / 2) { fromSection.PrefixString = string.Concat(" TOP ", endIndex.ToString()); fromSection.TableName = string.Concat(" (", fromSection.SqlString, ") AS tempIntable "); fromSection.PrefixString = string.Concat(" TOP ", pageSize.ToString()); fromSection.DistinctString = string.Empty; fromSection.GroupBy(GroupByClip.None); fromSection.Select(Field.All); fromSection.OrderBy(fromSection.OrderByClip.ReverseOrderByClip); fromSection.Where(WhereClip.All); // fromSection.TableName = string.Concat(" (", fromSection.SqlString, ") AS tempOuttable "); fromSection.PrefixString = string.Empty; fromSection.OrderBy(fromSection.OrderByClip.ReverseOrderByClip); } else { fromSection.PrefixString = string.Concat(" TOP ", (count - startIndex + 1).ToString()); fromSection.OrderBy(fromSection.OrderByClip.ReverseOrderByClip); fromSection.TableName = string.Concat(" (", fromSection.SqlString, ") AS tempIntable "); fromSection.PrefixString = string.Concat(" TOP ", pageSize.ToString()); fromSection.DistinctString = string.Empty; fromSection.GroupBy(GroupByClip.None); fromSection.Select(Field.All); fromSection.OrderBy(fromSection.OrderByClip.ReverseOrderByClip); fromSection.Where(WhereClip.All); } } fromSection.Parameters = list; } return(fromSection); }
/// <summary> /// 两个OrderByClip相加 /// </summary> /// <param name="leftOrderByClip"></param> /// <param name="rightOrderByClip"></param> /// <returns></returns> public static OrderByClip operator &(OrderByClip leftOrderByClip, OrderByClip rightOrderByClip) { if (IsNullOrEmpty(leftOrderByClip) && IsNullOrEmpty(rightOrderByClip)) return None; if (IsNullOrEmpty(leftOrderByClip)) return rightOrderByClip; if (IsNullOrEmpty(rightOrderByClip)) return leftOrderByClip; OrderByClip orderby = new OrderByClip(); foreach (KeyValuePair<string, OrderByOperater> kv in leftOrderByClip.orderByClip) { orderby.orderByClip.Add(kv.Key, kv.Value); } foreach (KeyValuePair<string, OrderByOperater> kv in rightOrderByClip.orderByClip) { if (!orderby.orderByClip.ContainsKey(kv.Key)) orderby.orderByClip.Add(kv.Key, kv.Value); } return orderby; }
/// <summary> /// 判断 OrderByClip 是否为null /// </summary> /// <param name="orderByClip"></param> /// <returns></returns> public static bool IsNullOrEmpty(OrderByClip orderByClip) { if ((null == orderByClip) || string.IsNullOrEmpty(orderByClip.ToString())) return true; return false; }
/// <summary> /// 比较 /// </summary> /// <param name="orderByClip"></param> /// <returns></returns> public bool Equals(OrderByClip orderByClip) { if (null == orderByClip) return false; return this.ToString().Equals(orderByClip.ToString()); }
/// <summary> /// 去掉的表前缀 /// </summary> /// <returns></returns> public OrderByClip RemovePrefixTableName() { OrderByClip tempOrderByClip = new OrderByClip(); foreach (KeyValuePair<string, OrderByOperater> kv in this.orderByClip) { string keyName = kv.Key; if (kv.Key.IndexOf('.') > 0) keyName = keyName.Substring(keyName.IndexOf('.') + 1); tempOrderByClip.orderByClip.Add(keyName, kv.Value); } return tempOrderByClip; }