Ejemplo n.º 1
0
        private string BuildOutput(bool includeSelection = true)
        {
            var sb = new StringBuilder();

            var offset = Limit?.Offset ?? 0;
            var fetch  = Limit?.Fetch ?? 0;

            if (includeSelection)
            {
                var topStr = offset == 0 && fetch > 0 ? $"top({fetch}) " : "";
                sb.Append($"select {topStr}{Selection}");
            }

            if (From != null)
            {
                sb.AppendLine();
                sb.Append($"from {From}");
            }

            if (Joins.Count > 0)
            {
                sb.AppendLine();
                sb.Append($"{string.Join(Environment.NewLine, Joins)}");
            }

            if (Where != null)
            {
                sb.AppendLine();
                sb.Append($"where {Where}");
            }

            if (GroupBys.Any())
            {
                sb.AppendLine();
                sb.Append($"group by {GroupBys}");
            }

            if (OrderBys.Any())
            {
                sb.AppendLine();
                sb.Append($"order by {string.Join(", ", OrderBys)}");
            }

            if (Limit != null && Limit.Offset > 0)
            {
                sb.AppendLine();
                sb.Append($"{Limit}");
            }

            sb.AppendLine();

            return(sb.ToString());
        }
Ejemplo n.º 2
0
        private string BuildOutput(bool includeSelection = true)
        {
            var sb = new StringBuilder();

            if (includeSelection)
            {
                sb.Append($"select {Selection}");
            }

            if (From != null)
            {
                sb.AppendLine();
                sb.Append($"from {From}");
            }

            if (Joins.Count > 0)
            {
                sb.AppendLine();
                sb.Append($"{string.Join(Environment.NewLine, Joins)}");
            }

            if (Where != null)
            {
                sb.AppendLine();
                sb.Append($"where {Where}");
            }

            if (GroupBys.Any())
            {
                sb.AppendLine();
                sb.Append($"group by {GroupBys}");
            }

            if (OrderBys.Any())
            {
                sb.AppendLine();
                sb.Append($"order by {string.Join(", ", OrderBys)}");
            }

            sb.AppendLine();

            return(sb.ToString());
        }
Ejemplo n.º 3
0
        public virtual Condition BuildCondition(Type type, VariableResolver variableResolver = null, IEnumerable <string> validProperties = null, IDictionary <string, string> propertyMapping = null)
        {
            if (type == null)
            {
                throw new ArgumentNullException(nameof(type));
            }
            var arg = new BuildArgument
            {
                ValidProperties  = validProperties,
                VariableResolver = variableResolver,
                EvaluationType   = type,
                PropertyMapping  = propertyMapping
            };

            var           error         = new ConditionBase.ErrorInfo();
            var           result        = new Condition();
            var           predicates    = new List <LambdaExpression>();
            var           exceptions    = new List <Exception>();
            OrderByClause orderByClause = null;

            if (Filters != null && Filters.Any())
            {
                var filtersResult = Filters.TryBuildPredicate(type, arg);
                if (filtersResult.Succeeded)
                {
                    predicates.Add(filtersResult.Result);
                }
                else
                {
                    if (filtersResult.Exception != null)
                    {
                        exceptions.Add(filtersResult.Exception);
                    }
                }
            }

            if (FilterGroups != null && FilterGroups.Any())
            {
                var filterGroupsResult = FilterGroups.TryBuildPredicate(type, arg);
                if (filterGroupsResult.Succeeded)
                {
                    predicates.Add(filterGroupsResult.Result);
                }
                else
                {
                    if (filterGroupsResult.Exception != null)
                    {
                        exceptions.Add(filterGroupsResult.Exception);
                    }
                }
            }

            if (!string.IsNullOrEmpty(Where))
            {
                var whereResult = ExpressionParser.Parse(Where, type, arg);
                if (whereResult.Succeeded)
                {
                    predicates.Add(whereResult.Result);
                }
                else
                {
                    if (whereResult.Exception != null)
                    {
                        exceptions.Add(whereResult.Exception);
                    }
                }
            }

            if (!string.IsNullOrWhiteSpace(OrderBy))
            {
                var orderByResult = OrderByParser.Parse(OrderBy, arg);
                if (orderByResult.Succeeded)
                {
                    orderByClause = orderByResult.Result;
                }
                else
                {
                    if (orderByResult.Exception != null)
                    {
                        exceptions.Add(orderByResult.Exception);
                    }
                }
            }
            else if (OrderBys != null && OrderBys.Any())
            {
                var orderBysResult = OrderByParser.Parse(OrderBys.ToArray(), arg);
                if (orderBysResult.Succeeded)
                {
                    orderByClause = orderBysResult.Result;
                }
                else
                {
                    if (orderBysResult.Exception != null)
                    {
                        exceptions.Add(orderBysResult.Exception);
                    }
                }
            }

            var isInvalid = arg.InvalidProperties.Any() || arg.InvalidOperators.Any() || arg.InvalidVariables.Any() || exceptions.Any();

            result.IsValid = !isInvalid;
            if (isInvalid)
            {
                error.EvaluationResult = new EvaluationResultBase
                {
                    InvalidProperties        = arg.InvalidProperties,
                    InvalidValues            = arg.InvalidValues,
                    InvalidOperators         = arg.InvalidOperators,
                    InvalidVariables         = arg.InvalidVariables,
                    InvalidOrderByDirections = arg.InvalidOrderByDirections
                };
                error.Exceptions = exceptions;
                result.Error     = error;
            }
            else
            {
                result.Predicates    = predicates;
                result.OrderByClause = orderByClause;
            }

            return(result);
        }