Beispiel #1
0
        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());
        }
Beispiel #3
0
        /// <summary>
        /// 获取连接的表
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="model"></param>
        /// <returns></returns>
        internal static string Join <T>(this AttrBaseResult model) where T : AttrBaseResult
        {
            StringBuilder join = new StringBuilder();

            object[] Mainobj = typeof(T).GetCustomAttributes(typeof(MainTableAttribute), true);
            if (Mainobj == null || Mainobj.Length != 1)
            {
                throw new AttrSqlException("未定义主表或定义多个主表,请检查Dto特性配置!");
            }
            MainTableAttribute mainTable = Mainobj[0] as MainTableAttribute;

            join.Append($"FROM {mainTable.GetMainTableName()} {mainTable.GetMainTableByName()} ");
            object[] AllTableObj = typeof(T).GetCustomAttributes(true);
            foreach (var table in AllTableObj)
            {
                if (table.GetType() == typeof(LeftTableAttribute))
                {
                    LeftTableAttribute leftTable = table as LeftTableAttribute;
                    join.Append($"LEFT JOIN {leftTable.GetLeftTableName()} {leftTable.GetLeftTableByName()} ON {leftTable.GetConnectField()} ");
                    if (!string.IsNullOrEmpty(leftTable.GetMainTableByName()))
                    {
                        join.Append($"{leftTable.GetMainTableByName()}.{leftTable.GetMainTableField()} ");
                    }
                    else if (!string.IsNullOrEmpty(mainTable.GetMainTableByName()))
                    {
                        join.Append($"{mainTable.GetMainTableByName()}.{leftTable.GetMainTableField()} ");
                    }
                    else
                    {
                        join.Append($"{leftTable.GetMainTableField()} ");
                    }
                }
                else if (table.GetType() == typeof(RightTableAttribute))
                {
                    RightTableAttribute rightTable = table as RightTableAttribute;
                    join.Append($"RIGHT JOIN {rightTable.GetRightTableName()} {rightTable.GetRightTableByName()} ON {rightTable.GetConnectField()} ");
                    if (!string.IsNullOrEmpty(rightTable.GetMainTableByName()))
                    {
                        join.Append($"{rightTable.GetMainTableByName()}.{rightTable.GetMainTableField()} ");
                    }
                    else if (!string.IsNullOrEmpty(mainTable.GetMainTableByName()))
                    {
                        join.Append($"{mainTable.GetMainTableByName()}.{rightTable.GetMainTableField()} ");
                    }
                    else
                    {
                        join.Append($"{rightTable.GetMainTableField()} ");
                    }
                }
                else if (table.GetType() == typeof(InnerTableAttribute))
                {
                    InnerTableAttribute innerTable = table as InnerTableAttribute;
                    join.Append($"INNER JOIN {innerTable.GetInnerTableName()} {innerTable.GetInnerTableByName()} ON {innerTable.GetConnectField()}");
                    if (!string.IsNullOrEmpty(innerTable.GetMainTableByName()))
                    {
                        join.Append($"{innerTable.GetMainTableByName()}.{innerTable.GetMainTableField()} ");
                    }
                    else if (!string.IsNullOrEmpty(mainTable.GetMainTableByName()))
                    {
                        join.Append($"{mainTable.GetMainTableByName()}.{innerTable.GetMainTableField()} ");
                    }
                    else
                    {
                        join.Append($"{innerTable.GetMainTableField()} ");
                    }
                }
                else if (table.GetType() == typeof(SublistAttribute))
                {
                    SublistAttribute suiblist          = table as SublistAttribute;
                    string           IncidenceRelation = suiblist.GetIncidenceRelation();
                    join.Append($"{IncidenceRelation} JOIN ({suiblist.GetTableSql()}) {suiblist.GetInnerTableByName()} ON {suiblist.GetConnectField()}");
                    if (!string.IsNullOrEmpty(suiblist.GetMainTableByName()))
                    {
                        join.Append($"{suiblist.GetMainTableByName()}.{suiblist.GetMainTableField()} ");
                    }
                    else if (!string.IsNullOrEmpty(mainTable.GetMainTableByName()))
                    {
                        join.Append($"{mainTable.GetMainTableByName()}.{suiblist.GetMainTableField()} ");
                    }
                    else
                    {
                        join.Append($"{suiblist.GetMainTableField()} ");
                    }
                }
            }
            return(join.ToString());
        }