Example #1
0
        private static CommandBuilder GetSelectBuilder(QueryTree queryTree, ConstraintType constraintType)
        {
            SqlServerCommandBuilder commandBuilder = new SqlServerCommandBuilder(queryTree)
            {
                Distinct = queryTree.SelectWithDistinct
            };

            if (queryTree.Localized)
            {
                commandBuilder.JoinInWhereClause = false;
            }

            commandBuilder.Constrain(queryTree.SelectWithPaging ? Constraint.Page(queryTree.StartIndex, queryTree.PageSize) : queryTree.ResultConstraint);

            for (QueryTree current = queryTree; current != null; current = current.Next)
            {
                if (constraintType != ConstraintType.Count)
                {
                    commandBuilder.AppendSelect(current.RetrieveQueryColumns());
                }

                commandBuilder.AppendFrom(current.Table);
                foreach (JoinPredicate joinPredicate in current.Joins)
                {
                    commandBuilder.AppendWhere(joinPredicate);
                }
                foreach (ColumnPredicate columnPredicate in current.WherePredicates)
                {
                    commandBuilder.AppendWhere(columnPredicate);
                }
                foreach (ColumnPredicate columnPredicate in current.HavingPredicates)
                {
                    commandBuilder.AppendHaving((columnPredicate));
                }

                commandBuilder.AppendGroupBy(current.RetrieveGroupByColumns());
                commandBuilder.AppendOrderBy(current.RetrieveOrderByColumns());
            }
            return(commandBuilder);
        }