internal static string GroupByHaving(this AttrBaseResult model) { StringBuilder groupbyBuilder = new StringBuilder(); StringBuilder havingBuilder = new StringBuilder(); //拿到所有标记了该特性的字段 groupbyBuilder.Append($" Group By "); havingBuilder.Append($" Having "); foreach (var prop in model.GetType().GetProperties()) { if (prop.IsDefined(typeof(GroupByAttribute), true)) { GroupByAttribute groupBy = prop.GetCustomAttributes(typeof(GroupByAttribute), true)[0] as GroupByAttribute; groupbyBuilder.Append($"{groupBy.GetGroupByField()},"); } if (prop.IsDefined(typeof(HavingAttribute), true)) { HavingAttribute having = prop.GetCustomAttributes(typeof(HavingAttribute), true)[0] as HavingAttribute; if (!string.IsNullOrEmpty(having.GetHavingCondition())) { havingBuilder.Append($" {having.GetHavingCondition()} And"); } } } if (groupbyBuilder.ToString() == " Group By ") { groupbyBuilder.Clear(); return(string.Empty); } else { groupbyBuilder.Remove(groupbyBuilder.Length - 1, 1); } if (havingBuilder.ToString() == " Having ") { havingBuilder.Clear(); } else { havingBuilder.Remove(havingBuilder.Length - 3, 3); //移除最后一个And groupbyBuilder.Append(havingBuilder.ToString()); } //后面继续完善Having部分 return(groupbyBuilder.ToString()); }
/// <summary> /// 获取查询字段 /// </summary> /// <param name="model"></param> /// <returns></returns> internal static string Select(this AttrBaseResult model) { StringBuilder builder = new StringBuilder(); builder.Append("SELECT "); foreach (var prop in model.GetType().GetProperties()) { //如果字段标记非查询属性,则直接跳过 if (prop.IsDefined(typeof(NonSelectAttribute), true)) { continue; } //若该属性没有标记字段,则直接使用该字段 if (prop.GetCustomAttributes(true).Length == 0) { builder.Append($"{prop.Name},"); continue; } //是否使用函数 if (prop.IsDefined(typeof(NonAggregateFuncFieldAttribute), true)) { NonAggregateFuncFieldAttribute funcFieldAttribute = prop.GetCustomAttributes(typeof(NonAggregateFuncFieldAttribute), true)[0] as NonAggregateFuncFieldAttribute; builder.Append($"{funcFieldAttribute.GetNonAggregateFuncField()} AS "); builder.Append($"{prop.Name},"); continue; } //是否使用聚合函数 if (prop.IsDefined(typeof(AggregateFuncFieldAttribute), true)) { AggregateFuncFieldAttribute funcFieldAttribute = prop.GetCustomAttributes(typeof(AggregateFuncFieldAttribute), true)[0] as AggregateFuncFieldAttribute; builder.Append($"{funcFieldAttribute.GetAggregateFuncField()} AS "); builder.Append($"{prop.Name},"); continue; } //是否使用运算符操作 if (prop.IsDefined(typeof(OperationAttribute), true)) { OperationAttribute operation = prop.GetCustomAttributes(typeof(OperationAttribute), true)[0] as OperationAttribute; builder.Append($"{operation.GetExpression()} AS {prop.Name},"); continue; } //表别名.字段名 AS 字段别名 if (prop.IsDefined(typeof(TableByNameAttribute), true)) { TableByNameAttribute byName = prop.GetCustomAttributes(typeof(TableByNameAttribute), true)[0] as TableByNameAttribute; builder.Append($"{byName.GetName()}."); } if (prop.IsDefined(typeof(DbFieldNameAttribute), true)) { DbFieldNameAttribute fieldName = prop.GetCustomAttributes(typeof(DbFieldNameAttribute), true)[0] as DbFieldNameAttribute; builder.Append($"{fieldName.GetDbFieldName()} AS "); builder.Append($"{prop.Name},"); } else { builder.Append($"{prop.Name},"); } } //移除最后一个逗号 builder.Remove(builder.Length - 1, 1); builder.Append(" "); return(builder.ToString()); }