Exemplo n.º 1
0
        private void AddJoins(Wrapper wrapper, SqlQuery dataQuery, string alias, bool sub = false)
        {
            var selectWithAliasWrapper = SelectWithAlias(alias);
            var joins = wrapper.GetJoins();
            var j     = 0;

            foreach (var join in joins)
            {
                var joinWrapper = join.Key;
                var joinAttr    = join.Value.GetCustomAttribute <JoinAttribute>();
                var joinAlias   = alias + ++j;

                var tableWithAlias      = joinWrapper.Table + " " + joinAlias;
                var selectWithAliasJoin = SelectWithAlias(joinAlias);

                Exp on = null;
                for (var k = 0; k < joinAttr.ColumnsFrom.Length; k++)
                {
                    on = on & Exp.EqColumns(selectWithAliasWrapper(joinAttr.ColumnsFrom[k]), selectWithAliasJoin(joinAttr.ColumnsTo[k]));
                }

                if (joinAttr.JoinType == JoinTypeEnum.Sub)
                {
                    var subQuery = new SqlQuery(tableWithAlias).Where(on);
                    var joinCols = joinWrapper.GetColumnNames(joinAlias);
                    if (joinCols.Any())
                    {
                        subQuery.Select("concat(\"[\", group_concat(JSON_ARRAY(" + string.Join(",", joinCols) + ") SEPARATOR ','), \"]\")");
                    }

                    foreach (var con in joinWrapper.GetConditions(joinAlias))
                    {
                        subQuery.Where(con.Key, con.Value);
                    }

                    AddJoins(joinWrapper, subQuery, joinAlias, true);

                    dataQuery.Select(subQuery);
                }
                else
                {
                    if (sub)
                    {
                        var joinCols = joinWrapper.GetColumnNames(joinAlias);
                        if (joinCols.Any())
                        {
                            dataQuery.Select("concat(\"[\", group_concat(JSON_ARRAY(" + string.Join(",", joinCols) + ") SEPARATOR ','), \"]\")");
                        }
                    }
                    else
                    {
                        dataQuery.Select(joinWrapper.GetColumnNames(joinAlias));
                    }

                    switch (joinAttr.JoinType)
                    {
                    //case JoinTypeEnum.Left:
                    //    dataQuery.LeftOuterJoin(tableWithAlias, on);
                    //    break;
                    //case JoinTypeEnum.Right:
                    //    dataQuery.RightOuterJoin(tableWithAlias, on);
                    //    break;
                    case JoinTypeEnum.Inner:
                        dataQuery.InnerJoin(tableWithAlias, on);
                        break;
                    }

                    foreach (var con in joinWrapper.GetConditions(joinAlias))
                    {
                        dataQuery.Where(con.Key, con.Value);
                    }
                }
            }
        }